NEURON
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
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
#define STRING
Definition: bbslsrv.cpp:9
Auto generated AST classes declaration.
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:166
Visitor for adding implicit arguments to [Core]NEURON functions.
void move(Item *q1, Item *q2, Item *q3)
Definition: list.cpp:200
static constexpr char CELSIUS_VARIABLE[]
global temperature variable
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...
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
static Node * node(Object *)
Definition: netcvode.cpp:291
Auto generated AST classes declaration.
void visit_function_call(ast::FunctionCall &node) override
visit node of type ast::FunctionCall
Map different tokens from lexer to token types.