vrq
csymtab.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (C) 1997-2007, Mark Hummel
3  * This file is part of Vrq.
4  *
5  * Vrq is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * Vrq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA
19  *****************************************************************************
20  */
21 /******************************************************************************
22  *
23  *
24  * csymtab.hpp
25  * - class definition for symbol tables
26  *
27  *
28  ******************************************************************************
29  */
30 
31 #ifndef CSYMTAB_HPP
32 #define CSYMTAB_HPP
33 
34 #include "glue.h"
35 #include "cdecl.h"
36 #include <map>
37 #include <list>
38 #include <algorithm>
39 
40 
41 
42 class CSymbol;
43 
49 template<class T1>
50 class CSymtabEntry : public map<CSymbol*,T1*>
51 {
52 private:
53  CSymtabEntry* previous;
54 public:
55 
56 /*********************************************************
57  Constructor
58 **********************************************************/
59 CSymtabEntry( CSymtabEntry* parent )
60 {
61  previous = parent;
62 }
63 
64 /*********************************************************
65  GetPrevious
66  - return previous level
67 **********************************************************/
68 CSymtabEntry* GetPrevious() {
69  return previous;
70 }
71 
72 /*********************************************************
73  Lookup
74  - find symbol by recursively searching table
75 **********************************************************/
76 
77 T1* Lookup( CSymbol* key, int skip=0 )
78 {
79  T1* result;
80  typename map<CSymbol*,T1*>::iterator ptr;
81 
82  result = NULL;
83  ptr = this->find( key );
84  if( skip==0 && ptr != this->end() ) {
85  return ptr->second;
86  }
87  if( previous ) {
88  result = previous->Lookup( key, skip>0 ? skip-1 : 0 );
89  }
90  return result;
91 }
92 
93 /*********************************************************
94  LookupTop
95  - find symbol at top level only
96 **********************************************************/
97 
98 T1* LookupTop( CSymbol* key )
99 {
100  T1* result;
101  typename map<CSymbol*,T1*>::iterator ptr;
102 
103  result = NULL;
104  ptr = this->find( key );
105  if( ptr != this->end() ) {
106  result = ptr->second;
107  }
108  return result;
109 }
110 
111 void Dump( FILE *f, int recurse, int level = 0 )
112 {
113  typename map<CSymbol*,T1*>::iterator ptr;
114 
115  for( ptr = this->begin(); ptr != this->end(); ++ptr) {
116  printf( "\t%d::%s => ", level, ptr->first->GetName() );
117  ptr->second->DumpDeclInfo( f );
118  }
119  if( recurse && previous != NULL ) {
120  previous->Dump( f, recurse, level+1 );
121  }
122 }
123 
124 };
135 template<class T1>
136 class CSymtab
137 {
138 private:
139  CSymtabEntry<T1>* table;
140  list<CSymtab<T1> > importPath;
141 public:
142 
146 static T1* Resolve( CSymbol* sym );
147 
151 CSymtab()
152 {
153  table = new CSymtabEntry<T1>( NULL );
154 }
158 void PopScope()
159 {
160  MASSERT( table != NULL );
161 
162  table = table->GetPrevious();
163 }
167 void PushScope()
168 {
169  table = new CSymtabEntry<T1>( table );
170 }
171 
178 void Add( CSymbol* sym, T1* obj )
179 {
180  (*table)[sym] = obj;
181 }
182 
189 T1* LookupTop( CSymbol* sym )
190 {
191 
192  return table->LookupTop( sym );
193 }
194 
202 T1* Lookup( CSymbol* sym, int skip = 0 )
203 {
204  T1* result = table->Lookup( sym, skip );
205  if( !result ) {
206  typename list<CSymtab<T1> >::iterator ptr;
207  for( ptr = importPath.begin(); ptr != importPath.end(); ++ptr ) {
208  result = ptr->Lookup( sym );
209  if( result ) {
210  break;
211  }
212  }
213  }
214  if( !result ) {
215  /*
216  * if symbol can be resolved externally, replicate
217  * entry with new symbol.
218  */
219  result = Resolve( sym );
220  if( result ) {
221  Add( sym, result );
222  }
223  }
224  return result;
225 }
226 
231 void ImportSearchTable( CSymtab<T1>& table )
232 {
233  importPath.push_back( table );
234 }
235 
241 void Dump( FILE *f, int recurse )
242 {
243  table->Dump( f, recurse );
244 }
245 
246 };
247 
248 #endif // CSYMTAB_HPP
void Add(double *r, double *a, double *b)
Definition: cnode.h:682
Aux class used to create symbol table scoping.
Definition: csymtab.h:135
Holder for character strings.
Definition: csymbol.h:44
void Dump(FILE *f, int recurse)
Dump all symbols in table to file descriptor.
Definition: csymtab.h:240