From 3c66c9ae9cb5c5bb0c64f5fa3ce684fa13d32027 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 19 Jun 2018 15:44:01 +0200
Subject: [PATCH] get eqtags to substitute unary ops at parsing

---
 src/ModFile.cc       | 20 +++-----------------
 src/ModFile.hh       |  2 ++
 src/ParsingDriver.cc | 15 ++++++++++-----
 3 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/src/ModFile.cc b/src/ModFile.cc
index 57cc6b78..2855643e 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -363,33 +363,19 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const
         }
     }
 
-  // Get all equation tags associated with VARs and Pac Models
-  string var_model_name;
-  set<string> eqtags;
-  map<string, vector<string>> var_model_eq_tags;
-  map<string, pair<SymbolList, int>> var_model_info_var_expectation;
-  for (auto & statement : statements)
-    {
-      auto *vms = dynamic_cast<VarModelStatement *>(statement);
-      if (vms != nullptr)
-        {
-          vms->getVarModelInfo(var_model_name, var_model_info_var_expectation, var_model_eq_tags);
-          for (auto & eqtag : var_model_eq_tags[var_model_name])
-            eqtags.insert(eqtag);
-        }
-    }
-
   if (transform_unary_ops)
     dynamic_model.substituteUnaryOps(diff_static_model);
   else
     // substitute only those unary ops that appear in VAR equations
-    dynamic_model.substituteUnaryOps(diff_static_model, eqtags);
+    dynamic_model.substituteUnaryOps(diff_static_model, substitute_unary_ops_in_equation_tags);
 
   // Create auxiliary variable and equations for Diff operators that appear in VAR equations
   ExprNode::subst_table_t diff_subst_table;
   dynamic_model.substituteDiff(diff_static_model, diff_subst_table);
 
   // Var Model
+  map<string, vector<string>> var_model_eq_tags;
+  map<string, pair<SymbolList, int>> var_model_info_var_expectation;
   map<string, tuple<vector<int>, vector<expr_t>, vector<bool>, vector<int>, int, vector<bool>, vector<int>>>
     var_model_info_pac_expectation;
   for (auto & statement : statements)
diff --git a/src/ModFile.hh b/src/ModFile.hh
index 5f76da3f..0b02a139 100644
--- a/src/ModFile.hh
+++ b/src/ModFile.hh
@@ -112,6 +112,8 @@ public:
   /*! (i.e. option parallel_local_files of model block) */
   vector<string> parallel_local_files;
 
+  set<string> substitute_unary_ops_in_equation_tags;
+
 private:
   //! List of statements
   vector<Statement *> statements;
diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc
index 1549aba6..8784976e 100644
--- a/src/ParsingDriver.cc
+++ b/src/ParsingDriver.cc
@@ -1638,11 +1638,16 @@ ParsingDriver::var_model()
     error("You must pass the model_name option to the var_model statement.");
   const string *name = new string(it->second);
 
-  if (options_list.vector_str_options.find("var.eqtags") != options_list.vector_str_options.end())
-    if (!symbol_list.empty())
-      error("You cannot pass a symbol list when passing equation tags to the var_model statement");
-    else if (options_list.num_options.find("var.order") != options_list.num_options.end())
-      error("You cannot pass the order option when passing equation tags to the var_model statement");
+  OptionsList::vec_str_options_t::iterator mit =
+    options_list.vector_str_options.find("var.eqtags");
+  if (mit != options_list.vector_str_options.end())
+    {
+      if (!symbol_list.empty())
+        error("You cannot pass a symbol list when passing equation tags to the var_model statement");
+      else if (options_list.num_options.find("var.order") != options_list.num_options.end())
+        error("You cannot pass the order option when passing equation tags to the var_model statement");
+      mod_file->substitute_unary_ops_in_equation_tags.insert(mit->second.begin(), mit->second.end());
+    }
 
   if (!symbol_list.empty())
     if (options_list.num_options.find("var.order") == options_list.num_options.end())
-- 
GitLab