From 87a52776905540406d45911565b8515dadba50b3 Mon Sep 17 00:00:00 2001 From: Ben Cumming <louncharf@gmail.com> Date: Mon, 20 Nov 2017 09:49:48 +0100 Subject: [PATCH] modcc now enforces derivatives only on state variables (#392) Derivatives should only appear on the left hand side of expressions that describe the time evolution of state variables. Without this check `modcc` segfaulted when processing a derivative expression of a non-state variable. * Specialize the semantic analysis of `DerivativeExpression` to enforce that derivatives are only applied to state variables. --- modcc/expression.cpp | 10 ++++++++++ modcc/expression.hpp | 2 ++ 2 files changed, 12 insertions(+) diff --git a/modcc/expression.cpp b/modcc/expression.cpp index 2fcf79e3..dbd6ae35 100644 --- a/modcc/expression.cpp +++ b/modcc/expression.cpp @@ -127,6 +127,16 @@ expression_ptr DerivativeExpression::clone() const { return make_expression<DerivativeExpression>(location_, spelling_); } +void DerivativeExpression::semantic(scope_ptr scp) { + IdentifierExpression::semantic(scp); + auto v = symbol_->is_variable(); + if (!v || !v->is_state()) { + error( pprintf("the variable '%' must be a state variable to be differentiated", + yellow(spelling_), location_)); + return; + } +} + /******************************************************************************* NumberExpression ********************************************************************************/ diff --git a/modcc/expression.hpp b/modcc/expression.hpp index 84eb7d09..704f3618 100644 --- a/modcc/expression.hpp +++ b/modcc/expression.hpp @@ -315,6 +315,8 @@ public: expression_ptr clone() const override; + void semantic(scope_ptr scp) override; + DerivativeExpression* is_derivative() override { return this; } ~DerivativeExpression() {} -- GitLab