From acc415d0f723442b910dd82bcf21f1890a654e10 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
(cherry picked from commit feee0ffa1252949e23b91c833b1b960527f36f14)
---
 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 c0177d18..7c25804a 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -7179,3 +7179,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 1fd3caf0..51b5fae1 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -557,6 +557,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 049ac456..cf940e5f 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -502,6 +502,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