From c6cb9aa1b8db8cf61883097d1c747a3b05eb7e4e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 25 Sep 2018 15:56:52 +0200
Subject: [PATCH] Fix bug in option notmpterms with model local variables

Since model local variables are now treated as temporary terms, they must be
marked as such even when option notmpterms is present.
---
 src/DynamicModel.cc | 11 +++++------
 src/ModelTree.cc    |  7 ++++++-
 src/ModelTree.hh    |  2 +-
 src/StaticModel.cc  |  9 +++------
 4 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index a4303bd9..a877d860 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -4286,12 +4286,11 @@ DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivative
         }
     }
   else
-    if (!no_tmp_terms)
-      {
-        computeTemporaryTerms(!use_dll);
-        if (bytecode)
-          computeTemporaryTermsMapping();
-      }
+    {
+      computeTemporaryTerms(!use_dll, no_tmp_terms);
+      if (bytecode && !no_tmp_terms)
+        computeTemporaryTermsMapping();
+    }
 }
 
 void
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index 3cc8d6bb..91e126f6 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1106,7 +1106,7 @@ ModelTree::computeThirdDerivatives(const set<int> &vars)
 }
 
 void
-ModelTree::computeTemporaryTerms(bool is_matlab)
+ModelTree::computeTemporaryTerms(bool is_matlab, bool no_tmp_terms)
 {
   map<expr_t, pair<int, NodeTreeReference>> reference_count;
   temporary_terms.clear();
@@ -1129,6 +1129,11 @@ ModelTree::computeTemporaryTerms(bool is_matlab)
       reference_count[v] = { ExprNode::min_cost(is_matlab)+1, NodeTreeReference::residuals };
     }
 
+  /* When option notmpterms is set, we only need to process model local
+     variables (and turn them into temporary terms); no need to go further */
+  if (no_tmp_terms)
+    return;
+
   map<NodeTreeReference, temporary_terms_t> temp_terms_map;
   temp_terms_map[NodeTreeReference::residuals] = temporary_terms_res;
   temp_terms_map[NodeTreeReference::firstDeriv] = temporary_terms_g1;
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 1ec33831..92adc710 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -186,7 +186,7 @@ protected:
   //! Write derivative of an equation w.r. to a variable
   void writeDerivative(ostream &output, int eq, int symb_id, int lag, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const;
   //! Computes temporary terms (for all equations and derivatives)
-  void computeTemporaryTerms(bool is_matlab);
+  void computeTemporaryTerms(bool is_matlab, bool no_tmp_terms);
   //! Computes temporary terms for the file containing parameters derivatives
   void computeParamsDerivativesTemporaryTerms();
   //! Writes temporary terms
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index 4b339a5d..22870c3f 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -1139,12 +1139,9 @@ StaticModel::computingPass(const eval_context_t &eval_context, bool no_tmp_terms
     }
   else
     {
-      if (!no_tmp_terms)
-        {
-          computeTemporaryTerms(true);
-          if (bytecode)
-            computeTemporaryTermsMapping(temporary_terms, map_idx);
-        }
+      computeTemporaryTerms(true, no_tmp_terms);
+      if (bytecode && !no_tmp_terms)
+        computeTemporaryTermsMapping(temporary_terms, map_idx);
     }
 }
 
-- 
GitLab