From 1f3844dc13ddb6906efc877b8ef150f583f2b041 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Sat, 28 Dec 2024 10:36:09 -0800 Subject: [PATCH] Support banks for full name --- .../generator/c/CTriggerObjectsGenerator.java | 34 ++++++++++++++++++- test/C/src/ReactorName.lf | 16 +++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/c/CTriggerObjectsGenerator.java b/core/src/main/java/org/lflang/generator/c/CTriggerObjectsGenerator.java index fd0c3be1d5..5ff6a65200 100644 --- a/core/src/main/java/org/lflang/generator/c/CTriggerObjectsGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CTriggerObjectsGenerator.java @@ -1095,7 +1095,39 @@ private static String deferredAllocationForEffectsOnInputs(ReactorInstance react */ private static String deferredSetParentAndName(ReactorInstance reactor) { var code = new CodeBuilder(); - code.pr(CUtil.reactorRef(reactor) + "->base.name = \"" + reactor.getName() + "\";"); + if (reactor.isBank()) { + // First, generate code to determine the size of the memory needed for the name. + code.pr("char* format = \"%s[%d]\";"); + code.pr( + "int length = snprintf(NULL, 0, format, \"" + + reactor.getName() + + "\", " + + CUtil.bankIndexName(reactor) + + ");\n"); + code.pr( + CUtil.reactorRef(reactor) + + "->base.name = (char*)lf_allocate(length + 1, sizeof(char)," + + " (allocation_record_t**)&((self_base_t*)" + + CUtil.reactorRef(reactor) + + ")->allocations);"); + code.pr( + "if(" + + CUtil.reactorRef(reactor) + + "->base.name != NULL) {"); // Will be NULL if lf_allocate fails. + code.indent(); + code.pr( + "snprintf(" + + CUtil.reactorRef(reactor) + + "->base.name, length + 1, format, \"" + + reactor.getName() + + "\", " + + CUtil.bankIndexName(reactor) + + ");"); + code.unindent(); + code.pr("}"); + } else { + code.pr(CUtil.reactorRef(reactor) + "->base.name = \"" + reactor.getName() + "\";"); + } ReactorInstance parent = reactor.getParent(); if (parent == null) { code.pr(CUtil.reactorRef(reactor) + "->base.parent = (self_base_t*)NULL;"); diff --git a/test/C/src/ReactorName.lf b/test/C/src/ReactorName.lf index a518108eca..8d14f13f41 100644 --- a/test/C/src/ReactorName.lf +++ b/test/C/src/ReactorName.lf @@ -6,30 +6,34 @@ preamble {= #include =} -reactor A { +reactor A(parent_bank_index: size_t = 0) { reaction(startup) {= const char* name = lf_reactor_full_name(self); lf_print("name: %s", name); - if (strcmp("ReactorName.b.a", name) != 0) { + char buffer[20]; + snprintf(buffer, 20, "ReactorName.b[%zu].a", self->parent_bank_index); + if (strcmp(buffer, name) != 0) { lf_print_error_and_exit("name does not match"); } =} } -reactor B { - a = new A() +reactor B(bank_index: size_t = 0) { + a = new A(parent_bank_index=bank_index) reaction(startup) {= const char* name = lf_reactor_full_name(self); lf_print("name: %s", name); - if (strcmp("ReactorName.b", name) != 0) { + char buffer[20]; + snprintf(buffer, 20, "ReactorName.b[%zu]", self->bank_index); + if (strcmp(buffer, name) != 0) { lf_print_error_and_exit("name does not match"); } =} } main reactor { - b = new B() + b = new[3] B() reaction(startup) {= const char* name = lf_reactor_full_name(self);