NEST  2.6.0,not_revisioned_source_dir@0
typechk.h
Go to the documentation of this file.
1 /*
2  * typechk.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef TYPECHECK_H
24 #define TYPECHECK_H
25 /*
26  classes for dynamic type checking in SLI
27 */
28 
29 /******************************************************************
30 Project: SYNOD/SLI 2.0
31 
32 Task: With a TypeTrie it will be possible to perfrom a type
33  check of (SLI) function input parameters. A TypeNode
34  represents the position and the datatype of a single
35  input parameter. The leaves of the tree will contain the
36  interpreter function of correct input parameters.
37 
38  A simple add type tree:
39  -----------------------
40 
41  root
42  |
43  long -----------------> double -|
44  | |
45  long -> double -| long -> double -|
46  (add) (add) (add) (add)
47 
48 Baseclass: None
49 
50 Inherit :
51 
52 History: This is a revised version of the type checking mechanism based on
53  tries.
54 Author: Marc-Oliver Gewaltig, Thomas Matyak
55 
56 Date: 18.11.95
57 
58 *******************************************************************/
59 
60 
61 #include <typeinfo>
62 #include <iostream>
63 #include <vector>
64 #include "slifunction.h"
65 #include "tokenarray.h"
66 #include "tokenstack.h"
67 #include "typearray.h"
68 #include "slinames.h"
69 
70 class TypeTrie {
71 private:
72  class TypeNode
73  {
74  private:
75  unsigned int refs; // number of references to this Node
76 
77  public:
78 
79  Name type; // expected type at this stack level
80  Token func; // points to the operator or an error func.
81 
82  TypeNode *alt; // points to the next parameter alternative
83  TypeNode *next; // points to the next stack level for this path
84 
85 
86  void addreference(void)
87  { ++refs; }
88 
89  void removereference(void)
90  {
91  if(--refs == 0)
92  delete this;
93  }
94 
95  TypeNode(const Name &n)
96  : refs(1), type(n),func(),alt(NULL),next(NULL) {}
97 
98  TypeNode(const Name &n, Token f)
99  : refs(1), type(n),func(f),alt(NULL),next(NULL) {}
100 
102  {
103  if (next != NULL)
105  if (alt != NULL)
106  alt->removereference();
107  }
108  void toTokenArray(TokenArray &) const;
109  void info(std::ostream &, std::vector<TypeNode const *> &) const;
110 
111  };
112 
114 
115 // TypeTrie operator=(const TypeTrie &){}; // disable this operator
116  TypeNode * getalternative(TypeNode *, const Name &);
117 
118  TypeNode* newnode(const TokenArray &) const;
119 
120 public:
121 
123  : root(new TypeNode(Name()))
124  {}
125 
126  TypeTrie(const TokenArray &ta)
127  : root(NULL)
128  {
129  root= newnode(ta);
130  }
131 
132  TypeTrie(const TypeTrie &tt)
133  :root(tt.root)
134  {
135  if(root !=NULL)
136  root->addreference();
137  }
138 
139  ~TypeTrie();
140 
141  void insert_move(const TypeArray& , Token &);
142  void insert(const TypeArray&a , const Token &t)
143  {
144  Token tmp(t);
145  // We have no insert variant, that's why we copy the token
146  // to a temporary and then move it to the trie.
147  insert_move(a,tmp);
148  }
149 
150  const Token& lookup(const TokenStack &st) const;
151 
152  bool operator == (const TypeTrie &) const;
153 
154  inline bool equals(const Name &, const Name &) const;
155  void toTokenArray(TokenArray &) const;
156  void info(std::ostream &) const;
157 };
158 
159 inline
161 {
162  if (root != NULL)
164 }
165 /*_____ end ~TypeTrie() __________________________________________*/
166 
167 
168 // Typename comparison including /anytype which compares
169 // positively for all other typenames
170 
171 inline
172 bool TypeTrie::equals(const Name &t1, const Name &t2) const
173 {
174  return(t1==t2 || t2==sli::any || t1==sli::any);
175 }
176 
177 inline
178 const Token& TypeTrie::lookup(const TokenStack &st) const
179 {
180 /*
181 Task: Tokens on stack 'st' will be compared with the TypeTrie.
182  Each stack element must have an equivalent type on the
183  current tree level. By reaching a leaf the interpreter
184  function will be returned. If an error occurs the
185  'ErrorFunction' will be returned.
186 
187 Author: Marc Oliver Gewaltig
188 
189 Date: 18.11.95, rewritten on Apr. 16 1998
190 
191 Parameter: st = stack
192 
193 */
194  const unsigned int load =st.load();
195  unsigned int level=0;
196 
197  TypeNode *pos=root;
198 
199  while(level<load)
200  {
201  Name find_type=st.pick(level)->gettypename();
202 
203  // Step 1: find the type at the current stack level in the
204  // list of alternatives. Unfortunately, this search is O(n).
205 
206  while (! equals(find_type, pos->type))
207  if (pos->alt != NULL)
208  pos = pos->alt;
209  else
210  throw ArgumentType(level);
211 
212  // Now go to the next argument.
213  pos = pos->next;
214 
215  if(pos->type == sli::object)
216  return pos->func;
217 
218  ++level;
219  }
220 
221  throw StackUnderflow(level+1, load) ;
222 }
223 
224 
225 inline
226 bool TypeTrie::operator == (const TypeTrie &tt) const
227 {
228  return (root == tt.root);
229 }
230 
231 #endif
232 
233 
TypeNode * alt
Definition: typechk.h:82
Exception to be thrown if an error occured while accessing the stack.
Definition: sliexceptions.h:303
~TypeNode()
Definition: typechk.h:101
Name type
Definition: typechk.h:79
unsigned int refs
Definition: typechk.h:75
Definition: tokenstack.h:38
Represent strings by ints to facilitate fast comparison.
Definition: name.h:53
const Token & pick(size_t i) const
Definition: tokenstack.h:111
Definition: tokenarray.h:62
TypeTrie(const TypeTrie &tt)
Definition: typechk.h:132
bool equals(const Name &, const Name &) const
Definition: typechk.h:172
const Name a("a")
Specific to Brette & Gerstner 2005 (aeif_cond-*)
Definition: nest_names.h:41
TypeNode * newnode(const TokenArray &) const
Definition: typechk.cc:114
TypeTrie()
Definition: typechk.h:122
TypeNode(const Name &n)
Definition: typechk.h:95
TypeNode * getalternative(TypeNode *, const Name &)
Definition: typechk.cc:155
const Token & lookup(const TokenStack &st) const
Definition: typechk.h:178
Definition: typechk.h:70
bool operator==(const TypeTrie &) const
Definition: typechk.h:226
TypeNode * root
Definition: typechk.h:113
Definition: typechk.h:72
void toTokenArray(TokenArray &) const
Definition: typechk.cc:269
void removereference(void)
Definition: typechk.h:89
const Name & gettypename(void) const
Definition: datum.h:148
void addreference(void)
Definition: typechk.h:86
Name object
Name any
TypeNode(const Name &n, Token f)
Definition: typechk.h:98
~TypeTrie()
Definition: typechk.h:160
void info(std::ostream &) const
Definition: typechk.cc:276
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
void toTokenArray(TokenArray &) const
Definition: typechk.cc:65
void insert_move(const TypeArray &, Token &)
Definition: typechk.cc:201
void info(std::ostream &, std::vector< TypeNode const * > &) const
Definition: typechk.cc:89
Index load(void) const
Definition: tokenstack.h:156
void insert(const TypeArray &a, const Token &t)
Definition: typechk.h:142
A type-independent container for C++-types.
Definition: token.h:68
std::vector< Name > TypeArray
Definition: typearray.h:30
Definition: sliexceptions.h:204
Token func
Definition: typechk.h:80
TypeNode * next
Definition: typechk.h:83
TypeTrie(const TokenArray &ta)
Definition: typechk.h:126