From f05b552f31153e3f204f8202ea254830a527641d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 17 Jan 2022 12:57:37 +0100
Subject: [PATCH] PAC: correctly handle log() and diff() operators in the
 equation defining the target

By default, the preprocessor substitutes logs and diffs by auxiliary variables
only in equations of auxiliary models (VAR, PAC). Hence they are not
substituted in the equation defining the target, since it is outside the
auxiliary model.

The fix consists in doing the substitution in that equation as well, when we
parse the linear combination within that equation.

Ref. dynare#1837
---
 src/SubModel.cc | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/SubModel.cc b/src/SubModel.cc
index 73826dec..3df70279 100644
--- a/src/SubModel.cc
+++ b/src/SubModel.cc
@@ -991,6 +991,20 @@ PacModelTable::transformPass(const lag_equivalence_table_t &unary_ops_nodes,
               exit(EXIT_FAILURE);
             }
 
+          // Substitute unary ops and diffs in that equation, before parsing (see dynare#1837)
+          target_expr = target_expr->substituteUnaryOpNodes(unary_ops_nodes, unary_ops_subst_table, neweqs);
+          if (neweqs.size() > 0)
+            {
+              cerr << "ERROR: the equation defining the target of 'pac_target_info(" << name << ")' contains a variable with a unary operator that is not present in the model" << endl;
+              exit(EXIT_FAILURE);
+            }
+          target_expr = target_expr->substituteDiff(diff_nodes, diff_subst_table, neweqs);
+          if (neweqs.size() > 0)
+            {
+              cerr << "ERROR: the equation defining the target of 'pac_target_info(" << name << ")' contains a diff'd variable that is not present in the model" << endl;
+              exit(EXIT_FAILURE);
+            }
+
           // Parse that model equation
           vector<pair<int, expr_t>> terms;
           expr_t constant;
-- 
GitLab