Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic analysis check for USEION variables in CONSTANT block #908

Merged
merged 3 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ int main(int argc, const char* argv[]) {
/// just visit the ast
AstVisitor().visit_program(*ast);

/// construct symbol table
{
logger->info("Running symtab visitor");
SymtabVisitor(update_symtab).visit_program(*ast);
}

/// Check some rules that ast should follow
{
logger->info("Running semantic analysis visitor");
Expand All @@ -337,12 +343,6 @@ int main(int argc, const char* argv[]) {
}
}

/// construct symbol table
{
logger->info("Running symtab visitor");
SymtabVisitor(update_symtab).visit_program(*ast);
}

/// use cnexp instead of after_cvode solve method
{
logger->info("Running CVode to cnexp visitor");
Expand Down
23 changes: 23 additions & 0 deletions src/visitors/semantic_analysis_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ast/program.hpp"
#include "ast/suffix.hpp"
#include "ast/table_statement.hpp"
#include "symtab/symbol_properties.hpp"
#include "utils/logger.hpp"
#include "visitors/visitor_utils.hpp"

Expand All @@ -22,6 +23,28 @@ bool SemanticAnalysisVisitor::check(const ast::Program& node) {
}
/// -->

/// <-- This code is for check 4
using namespace symtab::syminfo;
const auto& with_prop = NmodlType::read_ion_var | NmodlType::write_ion_var;

const auto& sym_table = node.get_symbol_table();
assert(sym_table != nullptr);

// get all ion variables
const auto& ion_variables = sym_table->get_variables_with_properties(with_prop, false);

/// make sure ion variables aren't redefined in a `CONSTANT` block.
for (const auto& var: ion_variables) {
if (var->has_any_property(NmodlType::constant_var)) {
logger->critical(
fmt::format("SemanticAnalysisVisitor :: ion variable {} from the USEION statement "
"can not be re-declared in a CONSTANT block",
var->get_name()));
check_fail = true;
}
}
/// -->

visit_program(node);
return check_fail;
}
Expand Down
5 changes: 3 additions & 2 deletions src/visitors/semantic_analysis_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
*
* 1. Check that a function or a procedure containing a TABLE statement contains only one argument
* (mandatory in mod2c).
* 2. Check that destructor blocks are only inside mod file that are point_process
* 3. A TABLE statement in functions cannot have name list, and should have one in procedures
* 2. Check that destructor blocks are only inside mod file that are point_process.
* 3. A TABLE statement in functions cannot have name list, and should have one in procedures.
* 4. Check if ion variables from a `USEION` statement are not declared in `CONSTANT` block.
*/
#include "ast/ast.hpp"
#include "visitors/ast_visitor.hpp"
Expand Down
17 changes: 17 additions & 0 deletions test/unit/visitor/semantic_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "parser/nmodl_driver.hpp"
#include "test/unit/utils/test_utils.hpp"
#include "visitors/semantic_analysis_visitor.hpp"
#include "visitors/symtab_visitor.hpp"


using namespace nmodl;
Expand All @@ -26,6 +27,7 @@ using nmodl::parser::NmodlDriver;
bool run_semantic_analysis_visitor(const std::string& text) {
NmodlDriver driver;
const auto& ast = driver.parse_string(text);
SymtabVisitor().visit_program(*ast);
return SemanticAnalysisVisitor{}.check(*ast);
}

Expand Down Expand Up @@ -105,3 +107,18 @@ SCENARIO("Destructor block", "[visitor][semantic_analysis]") {
}
}
}

SCENARIO("Ion variable in CONSTANT block", "[visitor][semantic_analysis]") {
GIVEN("A mod file with ion variable redeclared in a CONSTANT block") {
std::string nmodl_text = R"(
NEURON {
SUFFIX cdp4Nsp
USEION ca READ cao, cai, ica WRITE cai
}
CONSTANT { cao = 2 (mM) }
)";
THEN("Semantic analysis fails") {
REQUIRE(run_semantic_analysis_visitor(nmodl_text));
}
}
}