Skip to content

Commit

Permalink
Merge pull request #2014 from lf-lang/2013-parent-reactors-parameters…
Browse files Browse the repository at this point in the history
…-not-usable-inside-code-expression

[C] Self struct made available to initializers of state variables, parameters
  • Loading branch information
lhstrh authored Nov 3, 2023
2 parents f92b6d9 + 7b0bd7e commit 63a3d9c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,14 @@ public void generateReactorInstance(ReactorInstance instance) {
// Need to do this for each of the builders into which the code writes.
startTimeStep.startScopedBlock(child);
initializeTriggerObjects.startScopedBlock(child);
// Generate the parent self struct for children to access its params
initializeTriggerObjects.pr(
CUtil.selfType(instance)
+ " *self = "
+ CUtil.reactorRefName(instance)
+ "["
+ CUtil.runtimeIndex(instance)
+ "];");
generateReactorInstance(child);
initializeTriggerObjects.endScopedBlock();
startTimeStep.endScopedBlock();
Expand Down
31 changes: 31 additions & 0 deletions test/C/src/ParentParamsAccessToChildInit.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
target C

preamble {=
extern int child_ids[10];
=}

reactor Child(bank_index: int = 0, parent_index: int = 0, value: int = 0) {
preamble {=
int child_ids[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
=}

reaction(startup) {=
printf("Child bank_index:%d parent_index:%d value:%d\n", self->bank_index, self->parent_index, self->value);
=}
}

reactor Parent(bank_index: int = 0, n_parents: int = 0, n_children: int = 1) {
c = new[n_children] Child(
parent_index=bank_index,
value = {=
child_ids[(self->bank_index * self->n_children + bank_index) % (sizeof(child_ids) / sizeof(*child_ids))]
=})

reaction(startup) {=
printf("Parent[%d/%d] bank_index:%d\n", self->bank_index + 1, self->n_parents, self->bank_index);
=}
}

main reactor ParentParamsAccessToChildInit(n_parents: int = 2, per_parent_n_children: int = 3) {
p = new[n_parents] Parent(n_parents=n_parents, n_children=per_parent_n_children)
}

0 comments on commit 63a3d9c

Please sign in to comment.