User Guide
nmodl::symtab::SymbolTable Class Reference

Represent symbol table for a NMODL block. More...

Detailed Description

Represent symbol table for a NMODL block.

Symbol Table is used to track information about every block construct encountered in the nmodl. In NMODL, block constructs are NEURON, PARAMETER NET_RECEIVE etc. Each block is considered a new scope.

NMODL supports nested block definitions (i.e. nested blocks). One specific example of this is INITIAL block in NET_RECEIVE block. In this case we need multiple scopes for single top level block of NMODL. In the future if we enable block level scopes, we will need symbol tables per block. Hence we are implementing BlockSymbolTable which stores all symbol table information for specific NMODL block. Note that each BlockSymbolTable implementation is recursive because while symbol lookup we have to search first into local/current block. If lookup is unsuccessful then we have to traverse parent blocks until the end.

Todo:
  • Revisit when clone method is used and implementation of copy constructor
  • Name may not require as we have added AST node

Definition at line 57 of file symbol_table.hpp.

#include <symbol_table.hpp>

Classes

class  Table
 Helper class for implementing symbol table. More...
 

Public Member Functions

std::string to_string () const
 convert symbol table to string More...
 
const std::string & name () const noexcept
 
bool global_scope () const noexcept
 
void insert (const std::shared_ptr< Symbol > &symbol)
 
void set_parent_table (SymbolTable *block)
 
int symbol_count () const
 
SymbolTableclone () const
 Create a copy of symbol table. More...
 
std::shared_ptr< Symbollookup (const std::string &name) const
 check if symbol with given name exist in the current table (but not in parents) More...
 
std::shared_ptr< Symbollookup_in_scope (const std::string &name) const
 check if symbol with given name exist in the current table (including all parents) More...
 
bool under_global_scope ()
 check if currently we are visiting global scope node More...
 
void insert_table (const std::string &name, const std::shared_ptr< SymbolTable > &table)
 insert new symbol table as one of the children block More...
 
void print (std::ostream &ss, int level) const
 
std::string title () const
 construct title for symbol table More...
 
std::string position () const
 
bool is_method_defined (const std::string &name) const
 check if procedure/function with given name is defined More...
 
Ctor & dtor
 SymbolTable (std::string name, const ast::Ast *node, bool global=false)
 
 SymbolTable (const SymbolTable &table)
 
Getter
SymbolTableget_parent_table () const noexcept
 
std::string get_parent_table_name () const
 
std::vector< std::shared_ptr< Symbol > > get_variables (syminfo::NmodlType with=syminfo::NmodlType::empty, syminfo::NmodlType without=syminfo::NmodlType::empty) const
 get variables More...
 
std::vector< std::shared_ptr< Symbol > > get_variables_with_properties (syminfo::NmodlType properties, bool all=false) const
 get variables with properties More...
 
std::vector< std::shared_ptr< Symbol > > get_variables_with_status (syminfo::Status status, bool all=false) const
 

Private Attributes

const std::string symtab_name
 name of the block More...
 
Table table
 table holding all symbols in the current block More...
 
const ast::Astnode = nullptr
 pointer to ast node for which current symbol table created More...
 
bool global = false
 true if current symbol table is global. More...
 
SymbolTableparent = nullptr
 pointer to the symbol table of parent block in the mod file More...
 
std::map< std::string, std::shared_ptr< SymbolTable > > children
 symbol table for each enclosing block in the current nmodl block construct. More...
 

Constructor & Destructor Documentation

◆ SymbolTable() [1/2]

nmodl::symtab::SymbolTable::SymbolTable ( std::string  name,
const ast::Ast node,
bool  global = false 
)
inline

Definition at line 113 of file symbol_table.hpp.

◆ SymbolTable() [2/2]

nmodl::symtab::SymbolTable::SymbolTable ( const SymbolTable table)

Definition at line 50 of file symbol_table.cpp.

Member Function Documentation

◆ clone()

SymbolTable* nmodl::symtab::SymbolTable::clone ( ) const
inline

Create a copy of symbol table.

Todo:
Revisit the usage as tokens will be pointing to old nodes

Definition at line 194 of file symbol_table.hpp.

◆ get_parent_table()

SymbolTable* nmodl::symtab::SymbolTable::get_parent_table ( ) const
inlinenoexcept

Definition at line 126 of file symbol_table.hpp.

◆ get_parent_table_name()

std::string nmodl::symtab::SymbolTable::get_parent_table_name ( ) const
inline

Definition at line 130 of file symbol_table.hpp.

◆ get_variables()

std::vector< std::shared_ptr< Symbol > > nmodl::symtab::SymbolTable::get_variables ( syminfo::NmodlType  with = syminfo::NmodlType::empty,
syminfo::NmodlType  without = syminfo::NmodlType::empty 
) const

get variables

return all symbol which has all "with" properties and none of the "without" properties

Parameters
withvariables with properties. 0 matches everything
withoutvariables without properties. 0 matches nothing

The two different behaviors for 0 depend on the fact that we get get variables with ALL the with properties and without ANY of the without properties

Definition at line 107 of file symbol_table.cpp.

◆ get_variables_with_properties()

std::vector< std::shared_ptr< Symbol > > nmodl::symtab::SymbolTable::get_variables_with_properties ( syminfo::NmodlType  properties,
bool  all = false 
) const

get variables with properties

return all symbol having any of the provided properties

Parameters
propertiesvariables with properties. -1 matches everything
allall/any

Definition at line 88 of file symbol_table.cpp.

◆ get_variables_with_status()

std::vector< std::shared_ptr< Symbol > > nmodl::symtab::SymbolTable::get_variables_with_status ( syminfo::Status  status,
bool  all = false 
) const

Definition at line 120 of file symbol_table.cpp.

◆ global_scope()

bool nmodl::symtab::SymbolTable::global_scope ( ) const
inlinenoexcept

Definition at line 174 of file symbol_table.hpp.

◆ insert()

void nmodl::symtab::SymbolTable::insert ( const std::shared_ptr< Symbol > &  symbol)
inline

Definition at line 178 of file symbol_table.hpp.

◆ insert_table()

void nmodl::symtab::SymbolTable::insert_table ( const std::string &  name,
const std::shared_ptr< SymbolTable > &  table 
)

insert new symbol table as one of the children block

Definition at line 79 of file symbol_table.cpp.

◆ is_method_defined()

bool nmodl::symtab::SymbolTable::is_method_defined ( const std::string &  name) const

check if procedure/function with given name is defined

Definition at line 56 of file symbol_table.cpp.

◆ lookup()

std::shared_ptr<Symbol> nmodl::symtab::SymbolTable::lookup ( const std::string &  name) const
inline

check if symbol with given name exist in the current table (but not in parents)

Definition at line 199 of file symbol_table.hpp.

◆ lookup_in_scope()

std::shared_ptr< Symbol > nmodl::symtab::SymbolTable::lookup_in_scope ( const std::string &  name) const

check if symbol with given name exist in the current table (including all parents)

lookup for symbol in current scope as well as all parents

Definition at line 164 of file symbol_table.cpp.

◆ name()

const std::string& nmodl::symtab::SymbolTable::name ( ) const
inlinenoexcept

Definition at line 170 of file symbol_table.hpp.

◆ position()

std::string nmodl::symtab::SymbolTable::position ( ) const

Definition at line 67 of file symbol_table.cpp.

◆ print()

void nmodl::symtab::SymbolTable::print ( std::ostream &  ss,
int  level 
) const

when current symbol table is empty, the children can be printed from the same indentation level (this is to avoid unnecessary empty indentations)

recursively print all children tables

Definition at line 510 of file symbol_table.cpp.

◆ set_parent_table()

void nmodl::symtab::SymbolTable::set_parent_table ( SymbolTable block)
inline

Definition at line 182 of file symbol_table.hpp.

◆ symbol_count()

int nmodl::symtab::SymbolTable::symbol_count ( ) const
inline

Definition at line 186 of file symbol_table.hpp.

◆ title()

std::string nmodl::symtab::SymbolTable::title ( ) const

construct title for symbol table

Definition at line 501 of file symbol_table.cpp.

◆ to_string()

std::string nmodl::symtab::SymbolTable::to_string ( ) const
inline

convert symbol table to string

Definition at line 164 of file symbol_table.hpp.

◆ under_global_scope()

bool nmodl::symtab::SymbolTable::under_global_scope ( )

check if currently we are visiting global scope node

Check if current symbol table is in global scope.

We create program scope at the top level and it has global scope. It contains neuron variables like t, dt, celsius etc. Then each nmodl block defining variables are added under this program's symbol table. Hence there are multiple levels of global scopes. In this helper function we make sure current block as well as it's parent are under global scopes.

Definition at line 148 of file symbol_table.cpp.

Member Data Documentation

◆ children

std::map<std::string, std::shared_ptr<SymbolTable> > nmodl::symtab::SymbolTable::children
private

symbol table for each enclosing block in the current nmodl block construct.

for example, for every block statement (like if, while, for) within function we append new symbol table. note that this is also required for nested blocks like INITIAL in NET_RECEIVE.

Definition at line 107 of file symbol_table.hpp.

◆ global

bool nmodl::symtab::SymbolTable::global = false
private

true if current symbol table is global.

blocks like NEURON, PARAMETER defines global variables and hence they go into single global symbol table

Definition at line 98 of file symbol_table.hpp.

◆ node

const ast::Ast* nmodl::symtab::SymbolTable::node = nullptr
private

pointer to ast node for which current symbol table created

Definition at line 93 of file symbol_table.hpp.

◆ parent

SymbolTable* nmodl::symtab::SymbolTable::parent = nullptr
private

pointer to the symbol table of parent block in the mod file

Definition at line 101 of file symbol_table.hpp.

◆ symtab_name

const std::string nmodl::symtab::SymbolTable::symtab_name
private

name of the block

Definition at line 87 of file symbol_table.hpp.

◆ table

Table nmodl::symtab::SymbolTable::table
private

table holding all symbols in the current block

Definition at line 90 of file symbol_table.hpp.


The documentation for this class was generated from the following files: