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 )
78 {
79  T1* result;
80  typename map<CSymbol*,T1*>::iterator ptr;
81 
82  result = NULL;
83  ptr = this->find( key );
84  if( ptr != this->end() ) {
85  return ptr->second;
86  }
87  if( previous ) {
88  result = previous->Lookup( key );
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 )
112 {
113  typename map<CSymbol*,T1*>::iterator ptr;
114 
115  for( ptr = this->begin(); ptr != this->end(); ++ptr) {
116  printf( "\t%s => ", ptr->first->GetName() );
117  ptr->second->DumpDeclInfo( f );
118  }
119  if( recurse && previous != NULL ) {
120  previous->Dump( f, recurse );
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 
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 
201 T1* Lookup( CSymbol* sym )
202 {
203  T1* result = table->Lookup( sym );
204  if( !result ) {
205  typename list<CSymtab<T1> >::iterator ptr;
206  for( ptr = importPath.begin(); ptr != importPath.end(); ++ptr ) {
207  result = ptr->Lookup( sym );
208  if( result ) {
209  break;
210  }
211  }
212  }
213  if( !result ) {
214  /*
215  * if symbol can be resolved externally, replicate
216  * entry with new symbol.
217  */
218  result = Resolve( sym );
219  if( result ) {
220  Add( sym, result );
221  }
222  }
223  return result;
224 }
225 
231 {
232  importPath.push_back( table );
233 }
234 
240 void Dump( FILE *f, int recurse )
241 {
242  table->Dump( f, recurse );
243 }
244 
245 };
246 
247 #endif // CSYMTAB_HPP
CSymtab()
Create a symbol table.
Definition: csymtab.h:151
void ImportSearchTable(CSymtab< T1 > &table)
Import all symbols given package.
Definition: csymtab.h:230
T1 * LookupTop(CSymbol *sym)
Lookup symbol only in current scope.
Definition: csymtab.h:189
static T1 * Resolve(CSymbol *sym)
hook for namespaces
void PopScope()
Jump back to parent scope.
Definition: csymtab.h:158
T1 * Lookup(CSymbol *sym)
Lookup symbol in all scopes starting at the current scope.
Definition: csymtab.h:201
Aux class used to create symbol table scoping.
Definition: csymtab.h:136
void Add(CSymbol *sym, T1 *obj)
Add a symbol and it's assocated object at the current level.
Definition: csymtab.h:178
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
void PushScope()
Create a new scope for table.
Definition: csymtab.h:167