Skip to content

Commit

Permalink
Merge pull request #2452 from lf-lang/parameter-override
Browse files Browse the repository at this point in the history
Allow derived classes to override parameter default values
  • Loading branch information
edwardalee authored Jan 3, 2025
2 parents 566bac5 + 9c8a6f0 commit 74e13e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lflang.generator.c;

import java.util.HashSet;
import org.lflang.ast.ASTUtils;
import org.lflang.generator.CodeBuilder;
import org.lflang.generator.ParameterInstance;
Expand Down Expand Up @@ -41,7 +42,14 @@ public static String getInitializer(ParameterInstance p) {
public static String generateDeclarations(
TypeParameterizedReactor reactor, CTypes types, boolean suppressLineDirectives) {
CodeBuilder code = new CodeBuilder();
// Allow derived classes to override base class parameter definitions.
// Assume that the validator has checked that types match.
var declared = new HashSet<String>();
for (Parameter parameter : ASTUtils.allParameters(reactor.reactor())) {
// If the parameter name has been seen already, assume it is an override of the default value
// in a derived class. The validator should check.
if (declared.contains(parameter.getName())) continue;
declared.add(parameter.getName());
code.prSourceLineNumber(parameter, suppressLineDirectives);
code.pr(
types.getTargetType(reactor.resolveType(ASTUtils.getInferredType(parameter)))
Expand Down
16 changes: 16 additions & 0 deletions test/C/src/ParameterOverride.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
target C

reactor A(x: int = 0) {
reaction(startup) {=
if (self->x != 1) {
lf_print_error_and_exit("x is %d. Should be 1.", self->x);
}
=}
}

reactor B(x: int = 1) extends A {
}

main reactor {
b = new B()
}

0 comments on commit 74e13e0

Please sign in to comment.