libutap  0.93
Uppaal Timed Automata Parser
symbols.cpp
Go to the documentation of this file.
1 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 
3 /* libutap - Uppaal Timed Automata Parser.
4  Copyright (C) 2002-2006 Uppsala University and Aalborg University.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public License
8  as published by the Free Software Foundation; either version 2.1 of
9  the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  USA
20 */
21 
22 #include <cstdlib>
23 #include <cassert>
24 #include <vector>
25 #include <map>
26 #include <stdexcept>
27 
28 #include "utap/symbols.h"
29 #include "utap/expression.h"
30 
31 using std::vector;
32 using std::map;
33 using std::ostream;
34 using std::pair;
35 using std::make_pair;
36 using std::max;
37 using std::min;
38 using std::string;
39 
40 // The base types
41 
42 using namespace UTAP;
43 using namespace Constants;
44 
46 
48 {
49  // Default is the empty range
50  lower = INT_MAX;
51  upper = INT_MIN;
52 }
53 
54 range_t::range_t(int value) : lower(value), upper(value)
55 {
56 
57 }
58 
59 range_t::range_t(int l, int u) : lower(l), upper(u)
60 {
61 
62 }
63 
64 range_t::range_t(const pair<int,int> &r) : lower(r.first), upper(r.second)
65 {
66 
67 }
68 
69 bool range_t::operator == (const range_t &r) const
70 {
71  return lower == r.lower && upper == r.upper;
72 }
73 
74 bool range_t::operator != (const range_t &r) const
75 {
76  return lower != r.lower || upper != r.upper;
77 }
78 
80 {
81  return range_t(max(lower, r.lower), min(upper, r.upper));
82 }
83 
84 range_t range_t::join(const range_t &r) const
85 {
86  return range_t(min(lower, r.lower), max(upper, r.upper));
87 }
88 
90 {
91  return range_t(min(lower, r.lower), max(upper, r.upper));
92 }
93 
94 range_t range_t::operator& (const range_t &r) const
95 {
96  return range_t(max(lower, r.lower), min(upper, r.upper));
97 }
98 
99 bool range_t::contains(const range_t &r) const
100 {
101  return lower <= r.lower && r.upper <= upper;
102 }
103 
104 bool range_t::contains(int32_t value) const
105 {
106  return lower <= value && value <= upper;
107 }
108 
109 bool range_t::isEmpty() const
110 {
111  return lower > upper;
112 }
113 
114 uint32_t range_t::size() const
115 {
116  return isEmpty() ? 0 : upper - lower + 1;
117 }
118 
120 
121 struct symbol_t::symbol_data
122 {
123  int32_t count; // Reference counter
124  void *frame; // Uncounted pointer to containing frame
125  type_t type; // The type of the symbol
126  void *user; // User data
127  string name; // The name of the symbol
128 };
129 
130 symbol_t::symbol_t(void *frame, type_t type, const string& name, void *user)
131 {
132  data = new symbol_data;
133  data->count = 1;
134  data->frame = frame;
135  data->user = user;
136  data->type = type;
137  data->name = name;
138 }
139 
140 /* Copy constructor */
142 {
143  data = symbol.data;
144  if (data)
145  {
146  data->count++;
147  }
148 }
149 
150 /* Destructor */
152 {
153  if (data)
154  {
155  data->count--;
156  if (data->count == 0)
157  {
158  delete data;
159  }
160  }
161 }
162 
163 /* Assignment operator */
165 {
166  if (data)
167  {
168  data->count--;
169  if (data->count == 0)
170  {
171  delete data;
172  }
173  }
174  data = symbol.data;
175  if (data)
176  {
177  data->count++;
178  }
179  return *this;
180 }
181 
183 {
184  return data == symbol.data;
185 }
186 
187 /* Inequality operator */
189 {
190  return data != symbol.data;
191 }
192 
194 {
195  return data < symbol.data;
196 }
197 
198 /* Get frame this symbol belongs to */
200 {
201  return frame_t(data->frame);
202 }
203 
204 /* Returns the type of this symbol. */
206 {
207  return data->type;
208 }
209 
211 {
212  data->type = type;
213 }
214 
215 /* Returns the user data of this symbol */
217 {
218  return data->user;
219 }
220 
221 /* Returns the user data of this symbol */
222 const void *symbol_t::getData() const
223 {
224  return data->user;
225 }
226 
227 /* Returns the name (identifier) of this symbol */
228 string symbol_t::getName() const
229 {
230  return data->name;
231 }
232 
233 void symbol_t::setName(const string& name)
234 {
235  data->name = name;
236 }
237 
238 /* Sets the user data of this symbol */
239 void symbol_t::setData(void *value)
240 {
241  data->user = value;
242 }
243 
244 std::ostream &operator << (std::ostream &o, UTAP::symbol_t t)
245 {
246  return o << t.getType() << " " << t.getName();
247 }
248 
250 
251 struct frame_t::frame_data
252 {
253  int32_t count; // Reference count
254  bool hasParent; // True if there is a parent
255  frame_data *parent; // The parent frame data
256  vector<symbol_t> symbols; // The symbols in the frame
257  map<string, int32_t> mapping; // Mapping from names to indices
258 };
259 
261 {
262  data = NULL;
263 }
264 
266 {
267  data = (frame_data*)p;
268  if (data)
269  {
270  data->count++;
271  }
272 }
273 
274 /* Copy constructor */
276 {
277  data = frame.data;
278  if (data)
279  {
280  data->count++;
281  }
282 }
283 
284 /* Destructor */
286 {
287  if (data)
288  {
289  data->count--;
290  if (data->count == 0)
291  {
292  delete data;
293  }
294  }
295 }
296 
298 {
299  if (data)
300  {
301  data->count--;
302  if (data->count == 0)
303  {
304  delete data;
305  }
306  }
307  data = frame.data;
308  if (data)
309  {
310  data->count++;
311  }
312  return *this;
313 }
314 
315 /* Equality operator */
316 bool frame_t::operator == (const frame_t &frame) const
317 {
318  return data == frame.data;
319 }
320 
321 /* Inequality operator */
322 bool frame_t::operator != (const frame_t &frame) const
323 {
324  return data != frame.data;
325 }
326 
327 /* Returns the number of symbols in this frame */
328 uint32_t frame_t::getSize() const
329 {
330  return data->symbols.size();
331 }
332 
333 /* Returns the Nth symbol in this frame (counting from 0) */
335 {
336  return data->symbols[n];
337 }
338 
339 /* Returns the Nth symbol in this frame (counting from 0) */
341 {
342  return data->symbols[n];
343 }
344 
345 /* Returns the Nth symbol in this frame (counting from 0) */
346 const symbol_t frame_t::operator[](int32_t n) const
347 {
348  return data->symbols[n];
349 }
350 
351 /* Adds a symbol of the given name and type to the frame */
352 symbol_t frame_t::addSymbol(const string& name, type_t type, void *user)
353 {
354  symbol_t symbol(data, type, name, user);
355  data->symbols.push_back(symbol);
356  if (!name.empty())
357  {
358  data->mapping[symbol.getName()] = data->symbols.size() - 1;
359  }
360  return symbol;
361 }
362 
368 {
369  data->symbols.push_back(symbol);
370  if (!symbol.getName().empty())
371  {
372  data->mapping[symbol.getName()] = data->symbols.size() - 1;
373  }
374 }
375 
381 {
382  for (uint32_t i = 0; i < frame.getSize(); i++)
383  {
384  add(frame[i]);
385  }
386 }
387 
393 {
394  for (uint32_t i = 0; i < data->symbols.size(); i++)
395  {
396  symbol_t symbol = data->symbols[i];
397  frame.add(symbol);
398  symbol.data->frame = frame.data;
399  }
400  data->symbols.clear();
401  data->mapping.clear();
402 }
403 
406 {
407  vector<symbol_t> symbols = data->symbols;
408  data->symbols.clear();
409  data->mapping.clear();
410  for (uint32_t i = 0; i < symbols.size(); i++)
411  {
412  symbol_t symbol = symbols[i];
413  if (symbol != s)
414  {
415  add(symbol);
416  symbol.data->frame = data;
417  }
418  }
419 }
420 
421 int32_t frame_t::getIndexOf(const string& name) const
422 {
423  map<string, int32_t>::const_iterator i = data->mapping.find(name);
424  return (i == data->mapping.end() ? -1 : i->second);
425 }
426 
428 {
429  int32_t index = 0;
430  vector<symbol_t>::const_iterator first = data->symbols.begin();
431  vector<symbol_t>::const_iterator last = data->symbols.end();
432  for( ; first != last; ++first, ++index)
433  {
434  if (*first == symbol)
435  {
436  return index;
437  }
438  }
439  return -1;
440 }
441 
446 bool frame_t::resolve(const string& name, symbol_t &symbol)
447 {
448  int32_t idx = getIndexOf(name);
449  if (idx == -1)
450  {
451  return (data->hasParent ? getParent().resolve(name, symbol) : false);
452  }
453  symbol = data->symbols[idx];
454  return true;
455 }
456 
457 /* Returns the parent frame */
458 frame_t frame_t::getParent() // throw (NoParentException)
459 {
460  if (!data->hasParent)
461  {
462  throw NoParentException();
463  }
464  return frame_t(data->parent);
465 }
466 
467 /* Returns true if this frame has a parent */
468 bool frame_t::hasParent() const
469 {
470  return data->hasParent;
471 }
472 
473 /* Creates and returns a new frame without a parent */
475 {
476  frame_data *data = new frame_data;
477  data->count = 0;
478  data->hasParent = false;
479  data->parent = 0;
480  return frame_t(data);
481 }
482 
483 /* Creates and returns new frame with the given parent */
485 {
486  frame_data *data = new frame_data;
487  data->count = 0;
488  data->hasParent = true;
489  data->parent = parent.data;
490  return frame_t(data);
491 }
492 
493 std::ostream &operator << (std::ostream &o, UTAP::frame_t t)
494 {
495  o << "{";
496  for(uint32_t i = 0; i < t.getSize(); ++i)
497  {
498  if (i > 0)
499  {
500  o << ", ";
501  }
502  o << t[i];
503  }
504  return o << "}";
505 }
symbol_t getSymbol(int32_t)
Returns the Nth symbol in this frame.
Definition: symbols.cpp:334
bool resolve(const std::string &name, symbol_t &symbol)
Resolves a name in this frame or a parent frame.
Definition: symbols.cpp:446
range_t operator|(const range_t &) const
Constructs the union of two ranges.
Definition: symbols.cpp:89
~frame_t()
Destructor.
Definition: symbols.cpp:285
bool operator==(const range_t &) const
Equallity operator.
Definition: symbols.cpp:69
bool hasParent() const
Returns true if this frame has a parent.
Definition: symbols.cpp:468
range_t intersect(const range_t &) const
Constructs the intersection of two ranges.
Definition: symbols.cpp:79
uint32_t getSize() const
Returns the number of symbols in this frame.
Definition: symbols.cpp:328
A reference to a symbol.
Definition: symbols.h:107
std::string getName() const
Returns the name (identifier) of this symbol.
Definition: symbols.cpp:228
bool operator!=(const frame_t &) const
Inequality operator.
Definition: symbols.cpp:322
uint32_t size() const
Definition: symbols.cpp:114
bool operator!=(const range_t &) const
Inequallity operator.
Definition: symbols.cpp:74
bool operator==(const frame_t &) const
Equality operator.
Definition: symbols.cpp:316
bool contains(const range_t &) const
Returns true if the argument is contained in the range.
Definition: symbols.cpp:99
void remove(symbol_t s)
removes the given symbol
Definition: symbols.cpp:405
std::ostream & operator<<(std::ostream &os, const SignalFlow::strs_t &s)
Definition: signalflow.h:189
static frame_t createFrame()
Creates and returns a new root-frame.
Definition: symbols.cpp:474
const frame_t & operator=(const frame_t &)
Assignment operator.
Definition: symbols.cpp:297
bool operator==(const symbol_t &) const
Equality operator.
Definition: symbols.cpp:182
bool isEmpty() const
Returns true if and only if the range is empty.
Definition: symbols.cpp:109
symbol_t()
Default constructor.
Definition: symbols.h:117
range_t operator &(const range_t &) const
Constructs the intersection of two ranges.
static string symbol(const char *str)
Extracts the alpha-numerical symbol used for variable/type identifiers.
Definition: xmlreader.cpp:107
An integer range.
Definition: symbols.h:41
frame_t getParent()
Returns the parent frame.
Definition: symbols.cpp:458
void setName(const std::string &)
Alters the name of this symbol.
Definition: symbols.cpp:233
const symbol_t & operator=(const symbol_t &)
Assignment operator.
Definition: symbols.cpp:164
A reference to a frame.
Definition: symbols.h:183
A reference to a type.
Definition: type.h:93
type_t getType() const
Returns the type of this symbol.
Definition: symbols.cpp:205
void setType(type_t)
Alters the type of this symbol.
Definition: symbols.cpp:210
range_t()
Constructs the empty range.
Definition: symbols.cpp:47
symbol_t operator[](int32_t)
Returns the Nth symbol in this frame.
Definition: symbols.cpp:340
void * getData()
Returns the user data of this symbol.
Definition: symbols.cpp:216
void setData(void *)
Sets the user data of this symbol.
Definition: symbols.cpp:239
bool operator<(const symbol_t &) const
Less-than operator.
Definition: symbols.cpp:193
void moveTo(frame_t)
Move all symbols from this to a given one (leaving this empty).
Definition: symbols.cpp:392
range_t join(const range_t &) const
Constructs the union of two ranges.
Definition: symbols.cpp:84
symbol_t addSymbol(const std::string &name, type_t, void *user=NULL)
Adds a symbol of the given name and type to the frame.
Definition: symbols.cpp:352
Definition: lexer.cc:817
frame_t getFrame()
Get frame this symbol belongs to.
Definition: symbols.cpp:199
frame_t()
Default constructor.
Definition: symbols.cpp:260
~symbol_t()
Destructor.
Definition: symbols.cpp:151
int32_t getIndexOf(const std::string &name) const
Returns the index of the symbol with the given name.
void add(symbol_t)
Add all symbols from the given frame.
Definition: symbols.cpp:367
bool operator!=(const symbol_t &) const
Inequality operator.
Definition: symbols.cpp:188