NEURON
consist.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 /*
4  * Check that names do not have conflicting types. This is done after the
5  * entire file is read in and allows declaring a variable after its use (very
6  * bad style) to work in some circumstances.
7  */
8 #include "modl.h"
9 #include "parse1.hpp"
10 #include "symbol.h"
11 
12 #include <cstdlib> // for exit()
13 
14 extern Symbol* scop_indep;
15 extern Symbol* indepsym;
16 extern int vectorize;
17 
18 #define con(arg1, arg2, arg3) \
19  if (t & (arg2)) \
20  if (t & (~(arg2 | arg3))) { \
21  Fprintf(stderr, "%s is a %s\n", s->name, arg1); \
22  err = 1; \
23  }
24 
25 /**
26  * Check if variable declared in NEURON block is conflicting with a PROCEDURE or FUNCTION
27  *
28  * In order to avoid "declaring" variable in NEURON block and then defining
29  * PROCEDURE or FUNCTION with the same name, we check if the variable is
30  * function and has one of the NRN* type.
31  */
33  int type = s->nrntype;
34  int usage = s->usage;
35 
36  // if not function or procedure name then return already
37  if (!(usage & FUNCT)) {
38  return 0;
39  }
40 
41  // conflicting name if presence of NEURON block variable type
42  return (type & NRNRANGE || type & NRNGLOBAL || type & NRNPOINTER || type & NRNBBCOREPOINTER ||
43  type & NRNEXTRN);
44 }
45 
46 void consistency() {
47  Symbol* s;
48  Item* qs;
49  int i, tu, err = 0;
50  long t;
51 
52 
53  /* the scop_indep can also be a automatic parameter */
54  if (scop_indep != indepsym && scop_indep->subtype == (PARM | INDEP)) {
55  scop_indep->subtype = INDEP;
56  }
57 
58  SYMITER(NAME) {
59  t = s->subtype;
60  con("KEYWORD", KEYWORD, 0);
61  con("RESERVED WORD", EXTDEF | EXTDEF2 | EXTDEF3 | EXTDEF4 | EXTDEF5, 0);
62  con("CONSTANT", nmodlCONST, 0);
63  con("PARAMETER", PARM, ARRAY);
64  con("ASSIGNED", DEP, ARRAY);
65  con("INDEPENDENT", INDEP, 0);
66  con("STATE", STAT, ARRAY);
67  con("FUNCTION", FUNCT, 0);
68  con("PROCEDURE", PROCED, 0);
69  con("DERIVATIVE", DERF, 0);
70  con(" KINETIC", KINF, 0);
71  con("LINEAR", LINF, 0);
72  con("NONLINEAR", NLINF, 0);
73  con("DISCRETE", DISCF, 0);
74  con("PARTIAL", PARF, 0);
75  con("CONSTANT UNITS FACTOR", UNITDEF, 0);
76  tu = s->usage;
77  if ((tu & DEP) && (tu & FUNCT)) {
78  diag(s->name, " used as both variable and function");
79  }
80  // check for conflicting variable declaration with function
81  // do not use diag() because line number might be misleading
83  Fprintf(stderr,
84  "Error: %s used as both variable and function in file %s\n",
85  s->name,
86  finname);
87  exit(1);
88  }
89  if ((t == 0) && tu) {
90  // variable `v` is always defined in data array for vectorized mod files
91  if (!(vectorize && strcmp(s->name, "v") == 0)) {
92  Fprintf(stderr, "Warning: %s undefined. (declared within VERBATIM?)\n", s->name);
93  }
94  }
95  }
96  if (err) {
97  diag("multiple uses for same variable", (char*) 0);
98  }
99  if (indepsym == SYM0) {
100  diag("Independent variable is not defined", (char*) 0);
101  }
102  /* avoid the problem where person inadvertently is using Dstate as a state*/
103  SYMITER(NAME) {
104  if ((s->subtype & STAT) && (s->name[0] == 'D')) {
105  Symbol* s1 = lookup(s->name + 1);
106  if (s1 && s->type == NAME && (s->subtype & STAT)) {
107  fprintf(stderr, "%s is a STATE so %s is a %s' and", s1->name, s->name, s1->name);
108  diag(" cannot be declared as a STATE\n", (char*) 0);
109  }
110  }
111  }
112 }
#define i
Definition: md1redef.h:19
#define NRNGLOBAL
Definition: modl.h:218
#define EXTDEF4
Definition: modl.h:206
#define EXTDEF5
Definition: modl.h:207
#define PARM
Definition: modl.h:183
#define EXTDEF2
Definition: modl.h:202
#define nmodlCONST
Definition: modl.h:203
char finname[NRN_BUFSIZE]
Definition: model.cpp:38
#define NRNBBCOREPOINTER
Definition: modl.h:225
#define NRNRANGE
Definition: modl.h:215
#define NRNEXTRN
Definition: modl.h:212
#define EXTDEF3
Definition: modl.h:204
#define UNITDEF
Definition: modl.h:201
#define NRNPOINTER
Definition: membfunc.hpp:83
#define DEP
Definition: membfunc.hpp:64
#define DERF
Definition: model.h:114
#define FUNCT
Definition: model.h:108
#define SYM0
Definition: model.h:63
#define LINF
Definition: model.h:115
#define STAT
Definition: model.h:106
#define DISCF
Definition: model.h:117
#define INDEP
Definition: model.h:104
#define PARF
Definition: model.h:118
#define EXTDEF
Definition: model.h:119
#define KINF
Definition: model.h:120
#define KEYWORD
Definition: model.h:102
Symbol * lookup(const char *)
#define NLINF
Definition: model.h:116
#define ARRAY
Definition: model.h:107
#define PROCED
Definition: model.h:109
Item ** scop_indep
Definition: declare.cpp:9
void consistency()
Definition: consist.cpp:22
#define SYMITER(arg1)
Definition: symbol.h:13
#define con(arg1, arg2, arg3)
Definition: consist.cpp:18
int vectorize
Definition: nocpout.cpp:78
int is_var_declared_as_function(Symbol *s)
Check if variable declared in NEURON block is conflicting with a PROCEDURE or FUNCTION.
Definition: consist.cpp:32
Symbol * indepsym
Definition: declare.cpp:8
NMODL parser global flags / functions.
#define diag(s)
Definition: nonlin.cpp:19
s
Definition: multisend.cpp:521
short type
Definition: cabvars.h:10
Definition: model.h:8
Definition: model.h:47
char * name
Definition: model.h:61
int Fprintf(FILE *stream, const char *fmt, Args... args)
Definition: logger.hpp:8