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  // This function is traditionally used in MOD files with four arguments, but
23  // its value also depends on the global celsius variable so the real
24  // function in CoreNEURON has a 5th argument for that.
25  if (arguments.size() == 4) {
26  auto new_arguments = arguments;
27  new_arguments.insert(new_arguments.end(),
28  std::make_shared<ast::Name>(std::make_shared<ast::String>(
30  node.set_arguments(std::move(new_arguments));
31  }
32  } else if (nmodl::details::needs_neuron_thread_first_arg(function_name)) {
33  // We need to insert `nt` as the first argument if it's not already
34  // there
35  auto const is_nt = [](auto const& arg) {
36  // Not all node types implement get_node_name(); for example if the
37  // first argument is `a+b` then calling `get_node_name` would throw
38  auto const node_type = arg.get_node_type();
39  using ast::AstNodeType;
40  if (node_type == AstNodeType::NAME || node_type == AstNodeType::STRING ||
41  node_type == AstNodeType::CONSTANT_VAR || node_type == AstNodeType::VAR_NAME ||
42  node_type == AstNodeType::LOCAL_VAR) {
43  return arg.get_node_name() == "nt";
44  }
45  return false;
46  };
47  if (arguments.empty() || !is_nt(*arguments.front())) {
48  auto new_arguments = arguments;
49  new_arguments.insert(new_arguments.begin(), std::make_shared<ast::String>("nt"));
50  node.set_arguments(std::move(new_arguments));
51  }
52  }
53  node.visit_children(*this);
54 }
55 
56 } // namespace visitor
57 } // namespace nmodl
nmodl::ast::FunctionCall::get_node_name
std::string get_node_name() const override
Return name of the node.
Definition: ast.cpp:7061
nmodl::codegen::naming::CELSIUS_VARIABLE
static constexpr char CELSIUS_VARIABLE[]
global temperature variable
Definition: codegen_naming.hpp:90
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:7162
nmodl::ast::AstNodeType
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:164
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::ast::FunctionCall::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:7068
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