From feee0ffa1252949e23b91c833b1b960527f36f14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 10 Sep 2020 14:24:20 +0200
Subject: [PATCH] PAC model: better error message when pac_expectation refers
 to a non-existent pac_model

Closes: #53
---
 src/DynamicModel.cc | 12 ++++++++++++
 src/DynamicModel.hh |  4 ++++
 src/ModFile.cc      |  1 +
 3 files changed, 17 insertions(+)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 76b6671e..858c7087 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -6542,3 +6542,15 @@ DynamicModel::substituteVarExpectation(const map<string, expr_t> &subst_table)
   for (auto &equation : equations)
     equation = dynamic_cast<BinaryOpNode *>(equation->substituteVarExpectation(subst_table));
 }
+
+void
+DynamicModel::checkNoRemainingPacExpectation() const
+{
+  for (size_t eq = 0; eq < equations.size(); eq++)
+    if (equations[eq]->containsPacExpectation())
+      {
+        cerr << "ERROR: in equation " << equation_tags.getTagValueByEqnAndKey(eq, "name")
+             << ", the pac_expectation operator references an unknown pac_model" << endl;
+        exit(EXIT_FAILURE);
+      }
+}
diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh
index 3cb99412..07c7b853 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -570,6 +570,10 @@ public:
   //! Fills eval context with values of model local variables and auxiliary variables
   void fillEvalContext(eval_context_t &eval_context) const;
 
+  /*! Checks that all pac_expectation operators have been substituted, error
+    out otherwise */
+  void checkNoRemainingPacExpectation() const;
+
   auto
   getStaticOnlyEquationsInfo() const
   {
diff --git a/src/ModFile.cc b/src/ModFile.cc
index 2e74af53..4d528ebb 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -503,6 +503,7 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, bool
                                          eqtag_and_lag, nonstationary, pms->growth);
         dynamic_model.substitutePacExpectation(pms->name);
       }
+  dynamic_model.checkNoRemainingPacExpectation();
 
   dynamic_model.addEquationsForVar();
 
-- 
GitLab