NEURON
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
#define rhs
Definition: lineq.h:6
T & operator|=(T &lhs, T rhs)
T & operator&=(T &lhs, T rhs)
Access
variable usage within a mod file
@ read
variable is ready only
@ write
variable is written only
Status
state during various compiler passes
@ from_state
derived from state
@ thread_safe
variable marked as thread safe
@ localized
converted to local
@ globalized
converted to global
Scope
scope within a mod file
std::string to_string(const T &obj)
std::ostream & operator<<(std::ostream &os, const NmodlType &obj)
bool has_property(const NmodlType &obj, NmodlType property)
check if any property is set
NmodlType
NMODL variable properties.
@ use_range_ptr_var
FUNCTION or PROCEDURE needs setdata check.
@ factor_def
factor in unit block
@ table_statement_var
variable is used in table statement
@ non_linear_block
NonLinear Block.
@ to_solve
need to solve : used in solve statement
@ extern_neuron_variable
neuron variable accessible in mod file
@ longitudinal_diffusion_block
Internal LONGITUDINAL_DIFFUSION block.
@ nonspecific_cur_var
Non Specific Current.
@ bbcore_pointer_var
Bbcorepointer Type.
@ electrode_cur_var
Electrode Current.
@ table_assigned_var
variable is used in table as assigned
@ derivative_block
Derivative Block.
@ extern_method
neuron solver methods and math functions
@ param_assign
Parameter Variable.
@ function_table_block
FunctionTable Block.
@ assigned_definition
Assigned Definition.
@ define
Define variable / macro.
@ codegen_var
Codegen specific variable.
bool has_status(const Status &obj, Status state)
check if any status is set
VariableType
usage of mod file as array or scalar
std::vector< std::string > to_string_vector(const NmodlType &obj)
helper function to convert nmodl properties to string
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
#define text
Definition: plot.cpp:60
Implement string manipulation functions.