00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef CSYMTAB_HPP
00032 #define CSYMTAB_HPP
00033
00034 #include "glue.h"
00035 #include "cdecl.h"
00036 #include <map>
00037 #include <algorithm>
00038
00039
00040
00041 class CSymbol;
00042
00048 template<class T1>
00049 class CSymtabEntry : public map<CSymbol*,T1*>
00050 {
00051 private:
00052 INT32 depth;
00053 CSymtabEntry* previous;
00054 public:
00055
00056
00057
00058
00059 CSymtabEntry( INT32 aDepth, CSymtabEntry* parent )
00060 {
00061 depth = aDepth;
00062 previous = parent;
00063 }
00064
00065
00066
00067
00068 INT32 GetDepth() {
00069 return depth;
00070 }
00071
00072
00073
00074
00075 CSymtabEntry* GetPrevious() {
00076 return previous;
00077 }
00078
00079
00080
00081
00082
00083
00084 T1* Lookup( CSymbol* key )
00085 {
00086 T1* result;
00087 typename map<CSymbol*,T1*>::iterator ptr;
00088
00089 result = NULL;
00090 ptr = this->find( key );
00091 if( ptr != this->end() ) {
00092 return ptr->second;
00093 }
00094 if( previous ) {
00095 result = previous->Lookup( key );
00096 }
00097 return result;
00098 }
00099
00100
00101
00102
00103
00104
00105 T1* LookupTop( CSymbol* key )
00106 {
00107 T1* result;
00108 typename map<CSymbol*,T1*>::iterator ptr;
00109
00110 result = NULL;
00111 ptr = this->find( key );
00112 if( ptr != this->end() ) {
00113 result = ptr->second;
00114 }
00115 return result;
00116 }
00117
00118 void Dump( FILE *f, int recurse )
00119 {
00120 typename map<CSymbol*,T1*>::iterator ptr;
00121
00122 for( ptr = this->begin(); ptr != this->end(); ++ptr) {
00123 printf( "\t%s => ", ptr->first->GetName() );
00124 ptr->second->DumpDeclInfo( f );
00125 }
00126 if( recurse && previous != NULL ) {
00127 previous->Dump( f, recurse );
00128 }
00129 }
00130
00131 };
00142 template<class T1>
00143 class CSymtab
00144 {
00145 private:
00146 INT32 currentDepth;
00147 CSymtabEntry<T1>* table;
00148 public:
00149
00153 CSymtab()
00154 {
00155 currentDepth = 0;
00156 table = new CSymtabEntry<T1>( 0, NULL );
00157 }
00161 void PopScope()
00162 {
00163 MASSERT( currentDepth > 0 );
00164 MASSERT( table != NULL );
00165
00166 currentDepth--;
00167 if( table->GetDepth() <= currentDepth ) {
00168 return;
00169 }
00170
00171 table = table->GetPrevious();
00172 }
00178 void PushScope( int lazy )
00179 {
00180 currentDepth++;
00181 if( !lazy ) {
00182 table = new CSymtabEntry<T1>( currentDepth, table );
00183 }
00184 }
00189 INT32 GetDepth() { return currentDepth; }
00190
00197 void Add( CSymbol* sym, T1* obj )
00198 {
00199 if( table->GetDepth() < currentDepth ) {
00200 table = new CSymtabEntry<T1>( currentDepth, table );
00201 }
00202
00203 (*table)[sym] = obj;
00204 }
00205
00212 T1* LookupTop( CSymbol* sym )
00213 {
00214 if( currentDepth != table->GetDepth() ) {
00215 return NULL;
00216 }
00217 return table->LookupTop( sym );
00218 }
00219
00226 T1* Lookup( CSymbol* sym )
00227 {
00228 return table->Lookup( sym );
00229 }
00230
00236 void Dump( FILE *f, int recurse )
00237 {
00238 table->Dump( f, recurse );
00239 }
00240
00241 };
00242
00243 #endif // CSYMTAB_HPP