User Guide
symbol_properties.hpp
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 
8 #pragma once
9 
10 /**
11  * \file
12  * \brief Implement various classes to represent various Symbol properties
13  */
14 
15 #include <sstream>
16 
17 #include "utils/string_utils.hpp"
18 
19 
20 namespace nmodl {
21 namespace symtab {
22 
23 /// %Symbol information
24 namespace syminfo {
25 
26 /// \todo Error with pybind if std::underlying_typ is used
27 using enum_type = long long;
28 
29 /// kind of symbol
30 enum class DeclarationType : enum_type {
31  /// variable
32  variable,
33 
34  /// function
35  function
36 };
37 
38 /// scope within a mod file
39 enum class Scope : enum_type {
40  /// local variable
41  local,
42 
43  /// global variable
44  global,
45 
46  /// neuron variable
47  neuron,
48 
49  /// extern variable
50  external
51 };
52 
53 /// state during various compiler passes
54 enum class Status : enum_type {
55  empty = 0,
56 
57  /// converted to local
58  localized = 1LL << 0,
59 
60  /// converted to global
61  globalized = 1LL << 1,
62 
63  /// inlined
64  inlined = 1LL << 2,
65 
66  /// renamed
67  renamed = 1LL << 3,
68 
69  /// created
70  created = 1LL << 4,
71 
72  /// derived from state
73  from_state = 1LL << 5,
74 
75  /// variable marked as thread safe
76  thread_safe = 1LL << 6
77 };
78 
79 /// usage of mod file as array or scalar
80 enum class VariableType : enum_type {
81  /// scalar / single value
82  scalar,
83 
84  /// vector type
85  array
86 };
87 
88 /// variable usage within a mod file
89 enum class Access : enum_type {
90  /// variable is ready only
91  read = 1LL << 0,
92 
93  /// variable is written only
94  write = 1LL << 1
95 };
96 
97 
98 /**
99  * \brief NMODL variable properties
100  *
101  * Certain variables/symbols specified in various places in the same mod file.
102  * For example, RANGE variable could be in PARAMETER bloc, ASSIGNED block etc.
103  * In this case, the symbol in global scope need to have multiple properties.
104  * This is also important while code generation. Hence, in addition to AST node
105  * pointer, we define NmodlType so that we can set multiple properties. Note
106  * that AST pointer is no longer pointing to all pointers. Same applies for
107  * ModToken.
108  *
109  * These is some redundancy between NmodlType and other enums like
110  * syminfo::DeclarationType and syminfo::Scope. But as AST will become more
111  * abstract from NMODL (towards C/C++) then other types will be useful.
112  *
113  * \todo
114  * - Rename param_assign to parameter_var
115  */
116 enum class NmodlType : enum_type {
117 
118  empty = 0,
119 
120  /// Local Variable
121  local_var = 1LL << 0,
122 
123  /// Global Variable
124  global_var = 1LL << 1,
125 
126  /// Range Variable
127  range_var = 1LL << 2,
128 
129  /// Parameter Variable
130  param_assign = 1LL << 3,
131 
132  /// Pointer Type
133  pointer_var = 1LL << 4,
134 
135  /// Bbcorepointer Type
136  bbcore_pointer_var = 1LL << 5,
137 
138  /// Extern Type
139  extern_var = 1LL << 6,
140 
141  /// Prime Type
142  prime_name = 1LL << 7,
143 
144  /// Assigned Definition
145  assigned_definition = 1LL << 8,
146 
147  /// Unit Def
148  unit_def = 1LL << 9,
149 
150  /// Read Ion
151  read_ion_var = 1LL << 10,
152 
153  /// Write Ion
154  write_ion_var = 1LL << 11,
155 
156  /// Non Specific Current
157  nonspecific_cur_var = 1LL << 12,
158 
159  /// Electrode Current
160  electrode_cur_var = 1LL << 13,
161 
162  /// Argument Type
163  argument = 1LL << 14,
164 
165  /// Function Type
166  function_block = 1LL << 15,
167 
168  /// Procedure Type
169  procedure_block = 1LL << 16,
170 
171  /// Derivative Block
172  derivative_block = 1LL << 17,
173 
174  /// Linear Block
175  linear_block = 1LL << 18,
176 
177  /// NonLinear Block
178  non_linear_block = 1LL << 19,
179 
180  /// constant variable
181  constant_var = 1LL << 20,
182 
183  /// Kinetic Block
184  kinetic_block = 1LL << 21,
185 
186  /// FunctionTable Block
187  function_table_block = 1LL << 22,
188 
189  /// factor in unit block
190  factor_def = 1LL << 23,
191 
192  /// neuron variable accessible in mod file
193  extern_neuron_variable = 1LL << 24,
194 
195  /// neuron solver methods and math functions
196  extern_method = 1LL << 25,
197 
198  /// state variable
199  state_var = 1LL << 26,
200 
201  /// need to solve : used in solve statement
202  to_solve = 1LL << 27,
203 
204  /// ion type
205  useion = 1LL << 28,
206 
207  /// variable is used in table statement
208  table_statement_var = 1LL << 29,
209 
210  /// variable is used in table as assigned
211  table_assigned_var = 1LL << 30,
212 
213  /// Discrete Block
214  discrete_block = 1LL << 31,
215 
216  /// Define variable / macro
217  define = 1LL << 32,
218 
219  /// Codegen specific variable
220  codegen_var = 1LL << 33,
221 
222  /// Randomvar Type
223  random_var = 1LL << 34,
224 
225  /// FUNCTION or PROCEDURE needs setdata check
226  use_range_ptr_var = 1LL << 35,
227 
228  /// Internal LONGITUDINAL_DIFFUSION block
229  longitudinal_diffusion_block = 1LL << 36
230 };
231 
232 template <typename T>
233 inline T operator~(T arg) {
234  using utype = enum_type;
235  return static_cast<T>(~static_cast<utype>(arg));
236 }
237 
238 template <typename T>
239 inline T operator|(T lhs, T rhs) {
240  using utype = enum_type;
241  return static_cast<T>(static_cast<utype>(lhs) | static_cast<utype>(rhs));
242 }
243 
244 template <typename T>
245 inline T operator&(T lhs, T rhs) {
246  using utype = enum_type;
247  return static_cast<T>(static_cast<utype>(lhs) & static_cast<utype>(rhs));
248 }
249 
250 template <typename T>
251 inline T& operator|=(T& lhs, T rhs) {
252  lhs = lhs | rhs;
253  return lhs;
254 }
255 
256 template <typename T>
257 inline T& operator&=(T& lhs, T rhs) {
258  lhs = lhs & rhs;
259  return lhs;
260 }
261 
262 /// check if any property is set
263 inline bool has_property(const NmodlType& obj, NmodlType property) {
264  return static_cast<bool>(obj & property);
265 }
266 
267 /// check if any status is set
268 inline bool has_status(const Status& obj, Status state) {
269  return static_cast<bool>(obj & state);
270 }
271 
272 std::ostream& operator<<(std::ostream& os, const syminfo::NmodlType& obj);
273 std::ostream& operator<<(std::ostream& os, const syminfo::Status& obj);
274 
275 /// helper function to convert nmodl properties to string
276 std::vector<std::string> to_string_vector(const syminfo::NmodlType& obj);
277 
278 /// helper function to convert symbol status to string
279 std::vector<std::string> to_string_vector(const syminfo::Status& obj);
280 
281 template <typename T>
282 std::string to_string(const T& obj) {
283  std::string text;
284  bool is_first{true};
285  for (const auto& element: to_string_vector(obj)) {
286  if (is_first) {
287  text += element;
288  is_first = false;
289  } else {
290  text += " " + element;
291  }
292  }
293  return text;
294 }
295 
296 } // namespace syminfo
297 } // namespace symtab
298 } // namespace nmodl
nmodl::symtab::syminfo::has_status
bool has_status(const Status &obj, Status state)
check if any status is set
Definition: symbol_properties.hpp:268
nmodl::symtab::syminfo::VariableType
VariableType
usage of mod file as array or scalar
Definition: symbol_properties.hpp:80
nmodl::symtab::syminfo::Status::from_state
@ from_state
derived from state
nmodl::symtab::syminfo::Access::write
@ write
variable is written only
nmodl::symtab::syminfo::NmodlType::write_ion_var
@ write_ion_var
Write Ion.
nmodl::symtab::syminfo::DeclarationType
DeclarationType
kind of symbol
Definition: symbol_properties.hpp:30
nmodl::symtab::syminfo::NmodlType::use_range_ptr_var
@ use_range_ptr_var
FUNCTION or PROCEDURE needs setdata check.
nmodl::symtab::syminfo::NmodlType::extern_method
@ extern_method
neuron solver methods and math functions
nmodl::symtab::syminfo::Status::globalized
@ globalized
converted to global
nmodl::symtab::syminfo::Status::thread_safe
@ thread_safe
variable marked as thread safe
nmodl::symtab::syminfo::NmodlType::extern_var
@ extern_var
Extern Type.
nmodl::symtab::syminfo::NmodlType::table_assigned_var
@ table_assigned_var
variable is used in table as assigned
nmodl::symtab::syminfo::DeclarationType::variable
@ variable
variable
nmodl::symtab::syminfo::NmodlType::linear_block
@ linear_block
Linear Block.
nmodl::symtab::syminfo::Access::read
@ read
variable is ready only
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::symtab::syminfo::NmodlType::constant_var
@ constant_var
constant variable
nmodl::symtab::syminfo::NmodlType::to_solve
@ to_solve
need to solve : used in solve statement
nmodl::symtab::syminfo::NmodlType::function_table_block
@ function_table_block
FunctionTable Block.
nmodl::symtab::syminfo::operator&
T operator&(T lhs, T rhs)
Definition: symbol_properties.hpp:245
nmodl::symtab::syminfo::NmodlType::local_var
@ local_var
Local Variable.
nmodl::symtab::syminfo::Status
Status
state during various compiler passes
Definition: symbol_properties.hpp:54
string_utils.hpp
Implement string manipulation functions.
nmodl::symtab::syminfo::NmodlType::useion
@ useion
ion type
nmodl::symtab::syminfo::NmodlType::codegen_var
@ codegen_var
Codegen specific variable.
nmodl::symtab::syminfo::VariableType::scalar
@ scalar
scalar / single value
nmodl::symtab::syminfo::NmodlType::range_var
@ range_var
Range Variable.
nmodl::symtab::syminfo::Status::created
@ created
created
nmodl::symtab::syminfo::operator<<
std::ostream & operator<<(std::ostream &os, const NmodlType &obj)
Definition: symbol_properties.cpp:211
nmodl::symtab::syminfo::Status::inlined
@ inlined
inlined
nmodl::symtab::syminfo::NmodlType::prime_name
@ prime_name
Prime Type.
nmodl::symtab::syminfo::NmodlType::kinetic_block
@ kinetic_block
Kinetic Block.
nmodl::symtab::syminfo::NmodlType::function_block
@ function_block
Function Type.
nmodl::symtab::syminfo::VariableType::array
@ array
vector type
nmodl::symtab::syminfo::NmodlType::table_statement_var
@ table_statement_var
variable is used in table statement
nmodl::symtab::syminfo::NmodlType::procedure_block
@ procedure_block
Procedure Type.
nmodl::symtab::syminfo::NmodlType::define
@ define
Define variable / macro.
nmodl::symtab::syminfo::NmodlType::argument
@ argument
Argument Type.
nmodl::symtab::syminfo::NmodlType::factor_def
@ factor_def
factor in unit block
nmodl::symtab::syminfo::to_string
std::string to_string(const T &obj)
Definition: symbol_properties.hpp:282
nmodl::symtab::syminfo::Scope::global
@ global
global variable
nmodl::symtab::syminfo::operator&=
T & operator&=(T &lhs, T rhs)
Definition: symbol_properties.hpp:257
nmodl::symtab::syminfo::Scope
Scope
scope within a mod file
Definition: symbol_properties.hpp:39
nmodl::symtab::syminfo::NmodlType::nonspecific_cur_var
@ nonspecific_cur_var
Non Specific Current.
nmodl::symtab::syminfo::NmodlType::empty
@ empty
nmodl::symtab::syminfo::Access
Access
variable usage within a mod file
Definition: symbol_properties.hpp:89
nmodl::symtab::syminfo::NmodlType::assigned_definition
@ assigned_definition
Assigned Definition.
nmodl::symtab::syminfo::NmodlType::param_assign
@ param_assign
Parameter Variable.
nmodl::symtab::syminfo::NmodlType
NmodlType
NMODL variable properties.
Definition: symbol_properties.hpp:116
nmodl::symtab::syminfo::NmodlType::pointer_var
@ pointer_var
Pointer Type.
nmodl::symtab::syminfo::Scope::local
@ local
local variable
nmodl::symtab::syminfo::operator|=
T & operator|=(T &lhs, T rhs)
Definition: symbol_properties.hpp:251
nmodl::symtab::syminfo::NmodlType::longitudinal_diffusion_block
@ longitudinal_diffusion_block
Internal LONGITUDINAL_DIFFUSION block.
nmodl::symtab::syminfo::NmodlType::bbcore_pointer_var
@ bbcore_pointer_var
Bbcorepointer Type.
nmodl::symtab::syminfo::NmodlType::read_ion_var
@ read_ion_var
Read Ion.
nmodl::symtab::syminfo::NmodlType::state_var
@ state_var
state variable
nmodl::symtab::syminfo::Status::empty
@ empty
nmodl::symtab::syminfo::NmodlType::derivative_block
@ derivative_block
Derivative Block.
nmodl::symtab::syminfo::Scope::external
@ external
extern variable
nmodl::symtab::syminfo::NmodlType::non_linear_block
@ non_linear_block
NonLinear Block.
nmodl::symtab::syminfo::NmodlType::global_var
@ global_var
Global Variable.
nmodl::symtab::syminfo::to_string_vector
std::vector< std::string > to_string_vector(const NmodlType &obj)
helper function to convert nmodl properties to string
Definition: symbol_properties.cpp:23
nmodl::symtab::syminfo::NmodlType::random_var
@ random_var
Randomvar Type.
nmodl::symtab::syminfo::NmodlType::unit_def
@ unit_def
Unit Def.
nmodl::symtab::syminfo::operator|
T operator|(T lhs, T rhs)
Definition: symbol_properties.hpp:239
nmodl::symtab::syminfo::Status::renamed
@ renamed
renamed
nmodl::symtab::syminfo::operator~
T operator~(T arg)
Definition: symbol_properties.hpp:233
nmodl::symtab::syminfo::Status::localized
@ localized
converted to local
nmodl::symtab::syminfo::NmodlType::discrete_block
@ discrete_block
Discrete Block.
nmodl::symtab::syminfo::enum_type
long long enum_type
Definition: symbol_properties.hpp:27
nmodl::symtab::syminfo::Scope::neuron
@ neuron
neuron variable
nmodl::symtab::syminfo::NmodlType::extern_neuron_variable
@ extern_neuron_variable
neuron variable accessible in mod file
nmodl::symtab::syminfo::NmodlType::electrode_cur_var
@ electrode_cur_var
Electrode Current.
nmodl::symtab::syminfo::has_property
bool has_property(const NmodlType &obj, NmodlType property)
check if any property is set
Definition: symbol_properties.hpp:263