vrq
/usr/src/RPM/BUILD/vrq-1.0.96/src/csymtab.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1997-2007, Mark Hummel
00003  * This file is part of Vrq.
00004  *
00005  * Vrq is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * Vrq is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00018  * Boston, MA  02110-1301  USA
00019  *****************************************************************************
00020  */
00021 /******************************************************************************
00022  *
00023  *
00024  *         csymtab.hpp
00025  *              - class definition for symbol tables 
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         CSymtabEntry*   previous;
00053 public:
00054 
00055 /*********************************************************
00056         Constructor
00057 **********************************************************/
00058 CSymtabEntry( CSymtabEntry* parent ) 
00059 {
00060         previous     = parent;
00061 }
00062 
00063 /*********************************************************
00064         GetPrevious
00065         - return previous level 
00066 **********************************************************/
00067 CSymtabEntry*   GetPrevious() {
00068         return previous;
00069 }
00070 
00071 /*********************************************************
00072         Lookup
00073         - find symbol by recursively searching table 
00074 **********************************************************/
00075 
00076 T1* Lookup( CSymbol* key )
00077 {
00078         T1*                         result;
00079         typename map<CSymbol*,T1*>::iterator ptr;
00080 
00081         result = NULL;
00082         ptr = this->find( key );
00083         if( ptr != this->end() ) {
00084                 return ptr->second;
00085         }
00086         if( previous ) {
00087                 result = previous->Lookup( key );
00088         }
00089         return result;
00090 }       
00091 
00092 /*********************************************************
00093         LookupTop
00094         - find symbol at top level only 
00095 **********************************************************/
00096 
00097 T1* LookupTop( CSymbol* key )
00098 {
00099         T1*                         result;
00100         typename map<CSymbol*,T1*>::iterator ptr;
00101 
00102         result = NULL;
00103         ptr = this->find( key );
00104         if( ptr != this->end() ) {
00105                 result = ptr->second;
00106         } 
00107         return result;
00108 }       
00109 
00110 void    Dump( FILE *f, int recurse ) 
00111 {
00112         typename map<CSymbol*,T1*>::iterator ptr;
00113 
00114         for( ptr = this->begin(); ptr != this->end(); ++ptr) {
00115                 printf( "\t%s => ", ptr->first->GetName() );
00116                 ptr->second->DumpDeclInfo( f );
00117         }
00118         if( recurse && previous != NULL ) {
00119                 previous->Dump( f, recurse );
00120         }
00121 }
00122                 
00123 };
00134 template<class T1>
00135 class CSymtab
00136 {
00137 private:
00138         CSymtabEntry<T1>*       table;          
00139 public:
00140 
00144 CSymtab() 
00145 {
00146         table        = new CSymtabEntry<T1>( NULL );
00147 }
00151 void PopScope()
00152 {
00153         MASSERT( table != NULL );
00154 
00155         table = table->GetPrevious();
00156 }       
00160 void PushScope()
00161 {
00162         table = new CSymtabEntry<T1>( table );
00163 }
00164 
00171 void    Add( CSymbol* sym, T1* obj )
00172 {
00173         (*table)[sym] = obj;
00174 }
00175                 
00182 T1*     LookupTop( CSymbol* sym )
00183 {
00184 
00185         return table->LookupTop( sym );
00186 }
00187                 
00194 T1*     Lookup( CSymbol* sym )
00195 {
00196         return table->Lookup( sym );
00197 }
00198                 
00204 void    Dump( FILE *f, int recurse ) 
00205 {
00206         table->Dump( f, recurse );
00207 }
00208 
00209 };
00210 
00211 #endif // CSYMTAB_HPP