Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

symboltable.cpp

00001 /*
00002  * Copyright (c) 2003, Raymond Bosman
00003  * Copyright (c) 2003, Frederik Holljen
00004  * All Rights Reserved.
00005  *
00006  * See COPYING for licensing.
00007  */
00008 
00009 
00010 #include "symboltable.hpp"
00011 
00012 using namespace std;
00014 
00017 template<class T>
00018 SymbolTable<T>::SymbolTable<T>()
00019 {
00020     CurrentLevel = 0;
00021     Scopes.push_back( new map<string, T*> );
00022 }
00023 
00025 
00028 template<class T>
00029 SymbolTable<T>::~SymbolTable<T>()
00030 {
00031     // typename tells the compiler that T is a type
00032     // loop through all scopes
00033     for( typename vector<map<string, T*>*>::const_iterator scope_it = Scopes.begin();
00034          scope_it != Scopes.end(); ++scope_it )
00035     {
00036         // loop through all entries in the scope
00037         for( typename map<string, T*>::const_iterator entry_it = (*scope_it)->begin();
00038                  entry_it != (*scope_it)->end(); ++entry_it )
00039         {
00040             delete entry_it->second;
00041         }
00042         delete (*scope_it);
00043     }
00044 }
00045 
00047 template<class T>
00048 void SymbolTable<T>::openScope()
00049 {
00050     ++CurrentLevel;
00051     Scopes.push_back( new map<string, T*> );
00052 }
00053 
00055 
00059 template<class T>
00060 void SymbolTable<T>::closeScope()
00061 {
00062     if( CurrentLevel == 0 )
00063         throw SymbolTableException();
00064 
00065     // delete all entries in current scope
00066             // loop through all entries in the scope
00067     for( typename map<string, T*>::const_iterator entry_it = Scopes[CurrentLevel]->begin();
00068          entry_it != Scopes[CurrentLevel]->begin(); ++entry_it )
00069     {
00070         delete entry_it->second;
00071     }
00072 
00073     delete Scopes[CurrentLevel];
00074     Scopes.pop_back();
00075     --CurrentLevel;
00076 }
00077 
00079 
00082 template<class T>
00083 T *SymbolTable<T>::retrieve( const string &id )
00084 {
00085     for( int i = CurrentLevel; i >= 0; --i )
00086     {
00087         typename map<string, T*>::const_iterator entry_it = Scopes[i]->find( id );
00088         if( entry_it != Scopes[i]->end() ) return entry_it->second;
00089     }
00090     return 0;
00091 }
00092 
00094 
00101 template<class T>
00102 void SymbolTable<T>::enter( const string &id, T *newEntry )
00103 {
00104     if( Scopes[CurrentLevel]->find( id ) != Scopes[CurrentLevel]->end() )
00105         throw SymbolTableException();
00106     else
00107         (*Scopes[CurrentLevel])[id] = newEntry;
00108 }
00109 
00110 // explicit initialization. Without this the template code is not generated
00111 // an option is to put all the template code in de header of the symbol table class (this is what STL does)
00112 template class SymbolTable<CheckerIdEntry>;
00113 template class SymbolTable<CGIdEntry>;
00114 template class SymbolTable<FunctionIdEntry>;
00115 template class SymbolTable<CGFunctionEntry>;

Generated on Mon Dec 1 14:26:27 2003 for Ck by doxygen 1.3.3