User Guide
nmodl::visitor Namespace Reference

Implementation of different AST visitors. More...

Detailed Description

Implementation of different AST visitors.

Namespaces

 test
 

Classes

class  AfterCVodeToCnexpVisitor
 Visitor to change usage of after_cvode solver to cnexp. More...
 
class  AstVisitor
 Concrete visitor for all AST classes. More...
 
class  ConstantFolderVisitor
 Perform constant folding of integer/float/double expressions. More...
 
class  ConstAstVisitor
 Concrete constant visitor for all AST classes. More...
 
class  ConstVisitor
 Abstract base class for all constant visitors implementation. More...
 
class  DefUseAnalyzeVisitor
 Visitor to return Def-Use chain for a given variable in the block/node More...
 
class  DUChain
 Def-Use chain for an AST node. More...
 
class  DUInstance
 Represent use of a variable in the statement. More...
 
class  FunctionCallpathVisitor
 Visitor for traversing FunctionBlock s and ProcedureBlocks through their FunctionCall s More...
 
class  GlobalToRangeVisitor
 Visitor to convert GLOBAL variables to RANGE variables. More...
 
class  ImplicitArgumentVisitor
 Visitor for adding implicit arguments to [Core]NEURON functions. More...
 
class  IndexedNameVisitor
 Get node name with indexed for the IndexedName node and the dependencies of DiffEqExpression node. More...
 
class  IndexRemover
 Helper visitor to replace index of array variable with integer. More...
 
class  InlineVisitor
 Visitor to inline local procedure and function calls More...
 
class  JSONVisitor
 Visitor for printing AST in JSON format More...
 
class  KineticBlockVisitor
 Visitor for kinetic block statements More...
 
class  LocalizeVisitor
 Visitor to transform global variable usage to local More...
 
class  LocalToAssignedVisitor
 Visitor to convert top level LOCAL variables to ASSIGNED variables. More...
 
class  LocalVarRenameVisitor
 Visitor to rename local variables conflicting with global scope More...
 
class  LoopUnrollVisitor
 Unroll for loop in the AST. More...
 
class  MetaAstLookupVisitor
 
class  NeuronSolveVisitor
 Visitor that solves ODEs using old solvers of NEURON More...
 
class  NmodlPrintVisitor
 Visitor for printing AST back to NMODL More...
 
class  PerfVisitor
 Visitor for measuring performance related information More...
 
class  RenameVisitor
 Blindly rename given variable to new name More...
 
class  SemanticAnalysisVisitor
 
class  SolveBlockVisitor
 Replace solve block statements with actual solution node in the AST. More...
 
class  SteadystateVisitor
 Visitor for STEADYSTATE solve statements More...
 
class  SympyConductanceVisitor
 Visitor for generating CONDUCTANCE statements for ions More...
 
class  SympyReplaceSolutionsVisitor
 Replace statements in node with pre_solve_statements, tmp_statements, and solutions. More...
 
class  SympySolverVisitor
 Visitor for systems of algebraic and differential equations More...
 
class  SymtabVisitor
 Concrete visitor for constructing symbol table from AST. More...
 
class  UnitsVisitor
 Visitor for Units blocks of AST. More...
 
class  VarUsageVisitor
 Check if variable is used in given block. More...
 
class  VerbatimVarRenameVisitor
 Rename variable in verbatim block. More...
 
class  VerbatimVisitor
 Visitor for verbatim blocks of AST More...
 
class  Visitor
 Abstract base class for all visitors implementation. More...
 

Typedefs

using AstLookupVisitor = MetaAstLookupVisitor< Visitor >
 
using ConstAstLookupVisitor = MetaAstLookupVisitor< ConstVisitor >
 

Enumerations

enum  DUState {
  DUState::U, DUState::D, DUState::CD, DUState::LU,
  DUState::LD, DUState::UNKNOWN, DUState::CONDITIONAL_BLOCK, DUState::IF,
  DUState::ELSEIF, DUState::ELSE, DUState::NONE
}
 Represent a state in Def-Use chain. More...
 
enum  DUVariableType { DUVariableType::Local, DUVariableType::Global }
 Variable type processed by DefUseAnalyzeVisitor. More...
 

Functions

static bool is_number (const std::shared_ptr< ast::Expression > &node)
 check if given expression is a number note that the DEFINE node is already expanded to integer More...
 
static double get_value (const std::shared_ptr< ast::Expression > &node)
 get value of a number node TODO : eval method can be added to virtual base class More...
 
static bool supported_operator (ast::BinaryOp op)
 operators that currently implemented More...
 
static double compute (double lhs, ast::BinaryOp op, double rhs)
 Evaluate binary operation TODO : add support for other binary operators like ^ (pow) More...
 
std::string to_string (DUState state)
 DUState to string conversion for pretty-printing. More...
 
std::ostream & operator<< (std::ostream &os, DUState state)
 
std::shared_ptr< ast::Expressioncreate_expr (const std::string &str_expr)
 
static std::shared_ptr< ast::Expressionunwrap (const std::shared_ptr< ast::Expression > &expr)
 return underlying expression wrapped by WrappedExpression More...
 
static std::shared_ptr< ast::ExpressionStatementunroll_for_loop (const std::shared_ptr< ast::FromStatement > &node)
 Unroll given for loop. More...
 
static bool has_sympy_solution (const ast::Ast &node)
 check if given node contains sympy solution More...
 
static bool conductance_statement_possible (const ast::BreakpointBlock &node)
 Analyse breakpoint block to check if it is safe to insert CONDUCTANCE statements. More...
 
static bool is_local_statement (const std::shared_ptr< ast::Statement > &statement)
 Check if provided statement is local variable declaration statement. More...
 
static std::shared_ptr< Symbolcreate_symbol_for_node (ast::Node *node, NmodlType property, bool under_state_block)
 
static void add_external_symbols (symtab::ModelSymbolTable *symtab)
 
std::string suffix_random_string (const std::set< std::string > &vars, const std::string &original_string, const UseNumbersInString use_num=nmodl::utils::UseNumbersInString::WithNumbers)
 Return the "original_string" with a random suffix if "original_string" exists in "vars". More...
 
std::string get_new_name (const std::string &name, const std::string &suffix, std::map< std::string, int > &variables)
 Return new name variable by appending _suffix_COUNT where COUNT is number of times the given variable is already used. More...
 
std::shared_ptr< ast::LocalListStatementget_local_list_statement (const ast::StatementBlock &node)
 Return pointer to local statement in the given block, otherwise nullptr. More...
 
void add_local_statement (ast::StatementBlock &node)
 Add empty local statement to given block if already doesn't exist. More...
 
LocalVaradd_local_variable (StatementBlock &node, Identifier *varname)
 
LocalVaradd_local_variable (ast::StatementBlock &node, const std::string &varname)
 Add new local variable to the block. More...
 
LocalVaradd_local_variable (StatementBlock &node, const std::string &varname, int dim)
 
std::shared_ptr< Statementcreate_statement (const std::string &code_statement)
 Convert given code statement (in string format) to corresponding ast node. More...
 
std::vector< std::shared_ptr< Statement > > create_statements (const std::vector< std::string >::const_iterator &code_statements_beg, const std::vector< std::string >::const_iterator &code_statements_end)
 Same as for create_statement but for vectors of strings. More...
 
std::shared_ptr< StatementBlockcreate_statement_block (const std::vector< std::string > &code_statements)
 Convert given code statement (in string format) to corresponding ast node. More...
 
std::set< std::string > get_global_vars (const ast::Program &node)
 Return set of strings with the names of all global variables. More...
 
bool calls_function (const ast::Ast &node, const std::string &name)
 Checks whether block contains a call to a particular function. More...
 
void remove_statements_from_block (ast::StatementBlock &block, const std::set< ast::Node * > &statements)
 Remove statements from given statement block if they exist. More...
 

Enumeration Type Documentation

◆ DUState

Represent a state in Def-Use chain.

Enumerator

global variable is used

global variable is defined

CD 

global or local variable is conditionally defined

LU 

local variable is used

LD 

local variable is defined

UNKNOWN 

state not known

CONDITIONAL_BLOCK 

conditional block

IF 

if sub-block

ELSEIF 

elseif sub-block

ELSE 

else sub-block

NONE 

variable is not used

Definition at line 28 of file defuse_analyze_visitor.hpp.

◆ DUVariableType

Variable type processed by DefUseAnalyzeVisitor.

DUVariableType::Local means that we are looking for LD, LU and CD DUStates, while Global means we are looking for U, D and CD DUStates.

Enumerator
Local 
Global 

Definition at line 59 of file defuse_analyze_visitor.hpp.

Function Documentation

◆ add_external_symbols()

static void nmodl::visitor::add_external_symbols ( symtab::ModelSymbolTable symtab)
static

Definition at line 168 of file symtab_visitor_helper.hpp.

◆ add_local_statement()

void nmodl::visitor::add_local_statement ( StatementBlock node)

Add empty local statement to given block if already doesn't exist.

Definition at line 83 of file visitor_utils.cpp.

◆ add_local_variable() [1/3]

ast::LocalVar * nmodl::visitor::add_local_variable ( StatementBlock node,
const std::string &  varname 
)

Add new local variable to the block.

Definition at line 106 of file visitor_utils.cpp.

◆ add_local_variable() [2/3]

ast::LocalVar * nmodl::visitor::add_local_variable ( StatementBlock node,
const std::string &  varname,
int  dim 
)

Definition at line 111 of file visitor_utils.cpp.

◆ add_local_variable() [3/3]

ast::LocalVar * nmodl::visitor::add_local_variable ( StatementBlock node,
Identifier varname 
)

each block should already have local statement

Definition at line 92 of file visitor_utils.cpp.

◆ calls_function()

bool nmodl::visitor::calls_function ( const ast::Ast node,
const std::string &  name 
)

Checks whether block contains a call to a particular function.

Definition at line 193 of file visitor_utils.cpp.

◆ compute()

static double nmodl::visitor::compute ( double  lhs,
ast::BinaryOp  op,
double  rhs 
)
static

Evaluate binary operation TODO : add support for other binary operators like ^ (pow)

Definition at line 45 of file constant_folder_visitor.cpp.

◆ conductance_statement_possible()

static bool nmodl::visitor::conductance_statement_possible ( const ast::BreakpointBlock node)
static

Analyse breakpoint block to check if it is safe to insert CONDUCTANCE statements.

Most of the mod files have simple breakpoint blocks without any control flow statements. SympyConductanceVisitor just collects all the statements in the breakpoint block and insert conductance statements. If there are control flow statements like IF { a } ELSE { b } block with conflicting current statements inside IF and ELSE blocks or VERBATIM block then the resulting CONDUCTANCE statements may be incorrect. For now the simple approach is to not generate CONDUCTANCE statements if if-else statements exist in the block.

Parameters
nodeAst node for breakpoint block
Returns
true if it is safe to insert conductance statements otherwise false

Definition at line 41 of file sympy_conductance_visitor.cpp.

◆ create_expr()

std::shared_ptr<ast::Expression> nmodl::visitor::create_expr ( const std::string &  str_expr)

Definition at line 96 of file kinetic_block_visitor.cpp.

◆ create_statement()

std::shared_ptr< ast::Statement > nmodl::visitor::create_statement ( const std::string &  code_statement)

Convert given code statement (in string format) to corresponding ast node.

Create ast statement node from given code in string format.

We create dummy nmodl procedure containing given code statement and then parse it using NMODL parser. As there will be only one block with single statement, we return first statement.

Todo:
Need to revisit this during code generation passes to make sure if all statements can be part of procedure block.

Definition at line 126 of file visitor_utils.cpp.

◆ create_statement_block()

std::shared_ptr< ast::StatementBlock > nmodl::visitor::create_statement_block ( const std::vector< std::string > &  code_statements)

Convert given code statement (in string format) to corresponding ast node.

Create ast statement block node from given code in string format.

We create dummy nmodl procedure containing given code statement and then parse it using NMODL parser. As there will be only one block with single statement, we return first statement.

Definition at line 155 of file visitor_utils.cpp.

◆ create_statements()

std::vector< std::shared_ptr< ast::Statement > > nmodl::visitor::create_statements ( const std::vector< std::string >::const_iterator &  code_statements_beg,
const std::vector< std::string >::const_iterator &  code_statements_end 
)

Same as for create_statement but for vectors of strings.

Definition at line 136 of file visitor_utils.cpp.

◆ create_symbol_for_node()

static std::shared_ptr<Symbol> nmodl::visitor::create_symbol_for_node ( ast::Node node,
NmodlType  property,
bool  under_state_block 
)
static

add new symbol

extra property for state variables

Definition at line 25 of file symtab_visitor_helper.hpp.

◆ get_global_vars()

std::set< std::string > nmodl::visitor::get_global_vars ( const Program node)

Return set of strings with the names of all global variables.

Definition at line 170 of file visitor_utils.cpp.

◆ get_local_list_statement()

std::shared_ptr< ast::LocalListStatement > nmodl::visitor::get_local_list_statement ( const StatementBlock node)

Return pointer to local statement in the given block, otherwise nullptr.

Definition at line 73 of file visitor_utils.cpp.

◆ get_new_name()

std::string nmodl::visitor::get_new_name ( const std::string &  name,
const std::string &  suffix,
std::map< std::string, int > &  variables 
)

Return new name variable by appending _suffix_COUNT where COUNT is number of times the given variable is already used.

Definition at line 61 of file visitor_utils.cpp.

◆ get_value()

static double nmodl::visitor::get_value ( const std::shared_ptr< ast::Expression > &  node)
static

get value of a number node TODO : eval method can be added to virtual base class

Definition at line 26 of file constant_folder_visitor.cpp.

◆ has_sympy_solution()

static bool nmodl::visitor::has_sympy_solution ( const ast::Ast node)
static

check if given node contains sympy solution

Definition at line 27 of file solve_block_visitor.cpp.

◆ is_local_statement()

static bool nmodl::visitor::is_local_statement ( const std::shared_ptr< ast::Statement > &  statement)
static

Check if provided statement is local variable declaration statement.

Parameters
statementAST node representing statement in the MOD file
Returns
True if statement is local variable declaration else False

Statement declaration could be wrapped into another statement type like expression statement and hence we try to look inside if it's really a variable declaration.

Definition at line 120 of file sympy_solver_visitor.cpp.

◆ is_number()

static bool nmodl::visitor::is_number ( const std::shared_ptr< ast::Expression > &  node)
inlinestatic

check if given expression is a number note that the DEFINE node is already expanded to integer

Definition at line 20 of file constant_folder_visitor.cpp.

◆ operator<<()

std::ostream & nmodl::visitor::operator<< ( std::ostream &  os,
DUState  state 
)

Definition at line 52 of file defuse_analyze_visitor.cpp.

◆ remove_statements_from_block()

void nmodl::visitor::remove_statements_from_block ( ast::StatementBlock block,
const std::set< ast::Node * > &  statements 
)

Remove statements from given statement block if they exist.

◆ suffix_random_string()

std::string nmodl::visitor::suffix_random_string ( const std::set< std::string > &  vars,
const std::string &  original_string,
const UseNumbersInString  use_num = nmodl::utils::UseNumbersInString::WithNumbers 
)

Return the "original_string" with a random suffix if "original_string" exists in "vars".

Return a std::string in the form "original_string"_"random_string", where random_string is a string defined in the nmodl::utils::SingletonRandomString for the original_string. Vars is a const ref to std::set<std::string> which holds the names that need to be checked for uniqueness. Choose if the "random_string" will include numbers using "use_num"

Parameters
varsa const ref to std::set<std::string> which holds the names of the variables we should check for uniqueness. Normally this is a vector of all the variables defined in the mod file.
original_stringthe original string to be suffixed with a random string
use_numa UseNumbersInString enum value to choose if the random string will include numbers
Returns
std::string the new string with the proper suffix if needed

Definition at line 32 of file visitor_utils.cpp.

◆ supported_operator()

static bool nmodl::visitor::supported_operator ( ast::BinaryOp  op)
inlinestatic

operators that currently implemented

Definition at line 38 of file constant_folder_visitor.cpp.

◆ to_string()

std::string nmodl::visitor::to_string ( DUState  state)

DUState to string conversion for pretty-printing.

Definition at line 23 of file defuse_analyze_visitor.cpp.

◆ unroll_for_loop()

static std::shared_ptr<ast::ExpressionStatement> nmodl::visitor::unroll_for_loop ( const std::shared_ptr< ast::FromStatement > &  node)
static

Unroll given for loop.

Parameters
nodeFrom loop node in the AST
Returns
expression statement representing unrolled loop if successful otherwise nullptr

loop can be in the form of FROM i=(0) TO (10) so first unwrap all elements of the loop

we can unroll if iteration space of the loop is known after constant folding start, end and increment must be known

duplicate loop body and copy all statements to new vector

create new statement representing unrolled loop

Definition at line 98 of file loop_unroll_visitor.cpp.

◆ unwrap()

static std::shared_ptr<ast::Expression> nmodl::visitor::unwrap ( const std::shared_ptr< ast::Expression > &  expr)
static

return underlying expression wrapped by WrappedExpression

Definition at line 83 of file loop_unroll_visitor.cpp.