User Guide
implicit_argument_visitor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
9 #include "ast/ast_decl.hpp"
10 #include "ast/function_call.hpp"
11 #include "ast/string.hpp"
13 #include "lexer/token_mapping.hpp"
14 
15 namespace nmodl {
16 namespace visitor {
17 
19  auto function_name = node.get_node_name();
20  auto const& arguments = node.get_arguments();
21  if (function_name == "nrn_ghk") {
22  if (simulator == "neuron") {
23  return;
24  }
25  // This function is traditionally used in MOD files with four arguments, but
26  // its value also depends on the global celsius variable so the real
27  // function in CoreNEURON has a 5th argument for that.
28  if (arguments.size() == 4) {
29  auto new_arguments = arguments;
30  new_arguments.insert(new_arguments.end(),
31  std::make_shared<ast::Name>(std::make_shared<ast::String>(
33  node.set_arguments(std::move(new_arguments));
34  }
35  } else if (nmodl::details::needs_neuron_thread_first_arg(function_name)) {
36  // We need to insert `nt` as the first argument if it's not already
37  // there
38  auto const is_nt = [](auto const& arg) {
39  // Not all node types implement get_node_name(); for example if the
40  // first argument is `a+b` then calling `get_node_name` would throw
41  auto const node_type = arg.get_node_type();
42  using ast::AstNodeType;
43  if (node_type == AstNodeType::NAME || node_type == AstNodeType::STRING ||
44  node_type == AstNodeType::CONSTANT_VAR || node_type == AstNodeType::VAR_NAME ||
45  node_type == AstNodeType::LOCAL_VAR) {
46  return arg.get_node_name() == "nt";
47  }
48  return false;
49  };
50  if (arguments.empty() || !is_nt(*arguments.front())) {
51  auto new_arguments = arguments;
52  new_arguments.insert(new_arguments.begin(), std::make_shared<ast::String>("nt"));
53  node.set_arguments(std::move(new_arguments));
54  }
55  }
56  node.visit_children(*this);
57 }
58 
59 } // namespace visitor
60 } // namespace nmodl
nmodl::ast::FunctionCall::get_node_name
std::string get_node_name() const override
Return name of the node.
Definition: ast.cpp:7065
nmodl::codegen::naming::CELSIUS_VARIABLE
static constexpr char CELSIUS_VARIABLE[]
global temperature variable
Definition: codegen_naming.hpp:99
implicit_argument_visitor.hpp
Visitor for adding implicit arguments to [Core]NEURON functions.
ast_decl.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::details::needs_neuron_thread_first_arg
bool needs_neuron_thread_first_arg(const std::string &token)
Checks if token is one of the functions coming from NEURON/CoreNEURON and needs passing NrnThread* as...
Definition: token_mapping.cpp:235
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
token_mapping.hpp
Map different tokens from lexer to token types.
nmodl::ast::FunctionCall::set_arguments
void set_arguments(ExpressionVector &&arguments)
Setter for member variable FunctionCall::arguments (rvalue reference)
Definition: ast.cpp:7166
nmodl::ast::AstNodeType
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:166
codegen_naming.hpp
string.hpp
Auto generated AST classes declaration.
nmodl::ast::FunctionCall
TODO.
Definition: function_call.hpp:38
nmodl::ast::FunctionCall::get_arguments
const ExpressionVector & get_arguments() const noexcept
Getter for member variable FunctionCall::arguments.
Definition: function_call.hpp:166
function_call.hpp
Auto generated AST classes declaration.
nmodl::visitor::ImplicitArgumentVisitor::simulator
std::string simulator
Definition: implicit_argument_visitor.hpp:44
nmodl::ast::FunctionCall::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:7072
nmodl::visitor::ImplicitArgumentVisitor::visit_function_call
void visit_function_call(ast::FunctionCall &node) override
visit node of type ast::FunctionCall
Definition: implicit_argument_visitor.cpp:18