From 2aae03185b17444505618468b9bf27f3e0435fa8 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 31 Dec 2024 10:45:45 -0800 Subject: [PATCH 1/2] Allow derived classes to override parameter default values --- .../lflang/generator/c/CParameterGenerator.java | 9 +++++++++ test/C/src/ParameterOverride.lf | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 test/C/src/ParameterOverride.lf diff --git a/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java b/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java index 273f058d57..f73f3aefb5 100644 --- a/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java @@ -6,6 +6,8 @@ import org.lflang.lf.Initializer; import org.lflang.lf.Parameter; +import java.util.HashSet; + /** * Generates C code to declare and initialize parameters. * @@ -41,7 +43,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(); 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))) diff --git a/test/C/src/ParameterOverride.lf b/test/C/src/ParameterOverride.lf new file mode 100644 index 0000000000..d639eea3af --- /dev/null +++ b/test/C/src/ParameterOverride.lf @@ -0,0 +1,15 @@ +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() +} From 9c8a6f00fe09b5257ad3446d0126eb63fa9c59f9 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 31 Dec 2024 11:10:29 -0800 Subject: [PATCH 2/2] Spotless --- .../java/org/lflang/generator/c/CParameterGenerator.java | 3 +-- test/C/src/ParameterOverride.lf | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java b/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java index f73f3aefb5..bcf03ea33a 100644 --- a/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CParameterGenerator.java @@ -1,13 +1,12 @@ package org.lflang.generator.c; +import java.util.HashSet; import org.lflang.ast.ASTUtils; import org.lflang.generator.CodeBuilder; import org.lflang.generator.ParameterInstance; import org.lflang.lf.Initializer; import org.lflang.lf.Parameter; -import java.util.HashSet; - /** * Generates C code to declare and initialize parameters. * diff --git a/test/C/src/ParameterOverride.lf b/test/C/src/ParameterOverride.lf index d639eea3af..b586281dee 100644 --- a/test/C/src/ParameterOverride.lf +++ b/test/C/src/ParameterOverride.lf @@ -1,6 +1,6 @@ target C -reactor A(x:int = 0) { +reactor A(x: int = 0) { reaction(startup) {= if (self->x != 1) { lf_print_error_and_exit("x is %d. Should be 1.", self->x); @@ -8,7 +8,8 @@ reactor A(x:int = 0) { =} } -reactor B(x:int = 1) extends A { } +reactor B(x: int = 1) extends A { +} main reactor { b = new B()