From 125add749369298a40e81b935b9d3c6912c3e51a Mon Sep 17 00:00:00 2001
From: Marius Bozga <Marius.Bozga@univ-grenoble-alpes.fr>
Date: Thu, 18 Nov 2021 14:11:57 +0100
Subject: [PATCH] ensure unique state names in auto-generated code

---
 src/model/action.C     |  6 +++---
 src/model/object.C     |  6 ++++++
 src/model/object.h     |  1 +
 src/model/state.C      | 12 +++---------
 src/model/transition.C |  2 +-
 5 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/model/action.C b/src/model/action.C
index c6a0f14..f43c7a9 100644
--- a/src/model/action.C
+++ b/src/model/action.C
@@ -66,7 +66,7 @@ void IfAction::Alpha(FILE* file) const {
 
 void IfAction::Code(FILE* file) const {
   fprintf(file, "code(action,%s,%d,",
-          TRANSITION->GetSource()->GetName(),
+          TRANSITION->GetSource()->GetNameIndexed(),
           TRANSITION->GetIndex());
   Alpha(file);
   fprintf(file, ",\n\t");
@@ -873,7 +873,7 @@ void IfNextstateAction::PreCompile() {
   }
 
   int ok = 1;
-
+  
   IfState* context_state = NULL;
   for(int i = CONTEXT.GetCount() - 1; i >= 0; i--)
     if (CONTEXT[i]->IsState()) {
@@ -882,7 +882,7 @@ void IfNextstateAction::PreCompile() {
     }
   
   m_pState = context_state->FindVisible(m_pStateName);
-  
+
   ok &= m_pState != NULL;
       
   SetFlag(CORRECT, ok);
diff --git a/src/model/object.C b/src/model/object.C
index c9db09f..8d6c05a 100644
--- a/src/model/object.C
+++ b/src/model/object.C
@@ -96,6 +96,12 @@ void IfObject::Code(FILE*) const {
   // pure virtual
 }
 
+const char* IfObject::GetNameIndexed() const {
+  static char buffer[256];
+  sprintf(buffer,"%s_%d", m_pName, m_iIndex);
+  return buffer;
+}
+
 void IfObject::Initialize() {
   TYPES.Add(new IfBasicType(IfBasicType::BOOLEAN, strdup("boolean")));
   TYPES.Add(new IfBasicType(IfBasicType::INTEGER, strdup("integer")));
diff --git a/src/model/object.h b/src/model/object.h
index f0b5dc9..1b654e1 100644
--- a/src/model/object.h
+++ b/src/model/object.h
@@ -116,6 +116,7 @@ class IfObject {
   
  public:
   virtual void Code(FILE* file) const;
+  virtual const char* GetNameIndexed() const;
 
  protected:
   char* m_pName;
diff --git a/src/model/state.C b/src/model/state.C
index 007ef79..98d9f3f 100644
--- a/src/model/state.C
+++ b/src/model/state.C
@@ -257,15 +257,9 @@ void IfState::SetIndex(int* Index) {
 }
 
 void IfState::Code(FILE* file) const {
-  // force unique state names here, but this method is const...
-  // char suffix[4];
-  // sprintf(suffix, "%03d", m_iIndex);
-  // m_pName = (char*) realloc(m_pName, strlen(m_pName) + 4);
-  // strcat(m_pName, suffix);
-
-  fprintf(file, "code(dispatch,%s,",m_pName);
+  fprintf(file, "code(dispatch,%s,", GetNameIndexed());
   if (IsStable() && !IsControl())
-    fprintf(file, "%s", m_pParent->GetName());
+    fprintf(file, "%s", m_pParent->GetNameIndexed());
   
   for(int i = 0; i < m_pInputs->GetCount(); i++) {
     IfSignal* signal = m_pInputs->GetAt(i);
@@ -366,7 +360,7 @@ void IfState::CodeTable(FILE* file) const {
     if ((i + 1) % 32 == 0) fprintf(file, "\n    ");
   }
   fprintf(file, "},\n    &if_%s_instance::_%s_dispatch},\n",
-          m_pProcess->GetName(), m_pName);
+          m_pProcess->GetName(), GetNameIndexed());
   
   for(i = 0; i < m_pStates->GetCount(); i++)
     m_pStates->GetAt(i)->CodeTable(file);
diff --git a/src/model/transition.C b/src/model/transition.C
index dff703f..8d0751e 100644
--- a/src/model/transition.C
+++ b/src/model/transition.C
@@ -195,7 +195,7 @@ void IfTransition::Code(FILE* file) const {
   IfAction::TRANSITION = (IfTransition*) this;
   
   fprintf(file, "code(fire,%s,%d,",
-          m_pSource->GetName(), m_iIndex);
+          m_pSource->GetNameIndexed(), m_iIndex);
 
   switch (m_eDeadline) {
   case EAGER: fprintf(file, "EAGER,"); break;
-- 
GitLab