C-Breeze
C Compiler Infrastructure

[ Project home page]

symbol.h

Go to the documentation of this file.
00001 // $Id: symbol.h,v 1.6 2003/09/19 17:04:56 toktb Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #ifndef CBZ_SYMBOL_H
00039 #define CBZ_SYMBOL_H
00040 
00041 
00042 // ------------------------------------------------------------
00043 //   Symbol Table
00044 // ------------------------------------------------------------
00045 
00046 template <class T>
00047 class SymbolTable {
00048 
00049 private:
00050 
00051   // -- Nested or flat symbol table
00052   bool _is_nested;
00053 
00054   // -- A symbol table is a list of scopes
00055 
00056   typedef map<string,
00057               T,
00058               less< string > > scope;
00059 
00060   typedef typename scope::iterator scope_p;
00061 
00062   typedef list< scope * > table;
00063   typedef typename table::iterator table_p;
00064 
00065   table _table;
00066 
00067   virtual void shadow(T create, T shadowed) =0;
00068   virtual void notify_exit_scope(T dead) =0;
00069 
00070 public:
00071 
00072   SymbolTable(bool is_nested)
00073     : _is_nested(is_nested), _table()
00074   { _table.push_front(new scope()); }
00075 
00076   ~SymbolTable();
00077 
00078   void mark_nodes (void);
00079   void reset();
00080   T insert(const string & name, T sym); // Returns any conflicting object
00081   T lookup(const string & name, bool cur_scope_only = false);
00082   void print(FILE * out);
00083   string insert_unique(const string & root, T sym); // Returns new name
00084   void enter_scope();
00085   void exit_scope();
00086 
00087 };
00088 
00089 // ------------------------------------------------------------
00090 //   Subclass the symbol-table for each type of table
00091 // ------------------------------------------------------------
00092 
00093 class Identifiers_table : public SymbolTable< declNode * > {
00094 
00095 public:
00096 
00097   Identifiers_table();
00098   bool is_a_type(const string & name);
00099 
00100 private:
00101 
00102   void shadow(declNode * create, declNode * shadowed);
00103   void notify_exit_scope(declNode * dead);
00104 };
00105 
00106 class Labels_table : public SymbolTable< labelNode * > {
00107 
00108 public:
00109 
00110   Labels_table();
00111 
00112 private:
00113 
00114   void shadow(labelNode * create, labelNode * shadowed);
00115   void notify_exit_scope(labelNode * dead);
00116 };
00117 
00118 class Tags_table : public SymbolTable< suespecNode * > {
00119 
00120 public:
00121 
00122   Tags_table();
00123 
00124 private:
00125 
00126   void shadow(suespecNode * create, suespecNode * shadowed);
00127   void notify_exit_scope(suespecNode * dead);
00128 };
00129 
00130 class Externals_table : public SymbolTable< declNode * > {
00131 
00132 public:
00133 
00134   Externals_table();
00135 
00136 private:
00137 
00138   void shadow(declNode * create, declNode * shadowed);
00139   void notify_exit_scope(declNode * dead);
00140 };
00141 
00142 // ------------------------------------------------------------
00143 //   Symbols class holds the global information and static
00144 //   symbol table functions.
00145 // ------------------------------------------------------------
00146 
00147 class Symbols {
00148 
00149 public:
00150 
00151   /* Moved to unitNode...
00152   // -- Static functions -------------------
00153 
00154   static void EnterScope();
00155   static void ExitScope();
00156 
00157   // -- Static members ---------------------
00158 
00159   // -- ANSI defines the following name spaces (K&R A11.1, pg 227): 
00160 
00161   static Identifiers_table  * Identifiers;
00162   static Labels_table       * Labels; 
00163   static Tags_table         * Tags;
00164 
00165   // -- This table is used to ensure consistency across the translation unit 
00166 
00167   static Externals_table    * Externals;
00168   */
00169 
00170   static bool TrackScopeExits;
00171   static bool TrackInsertSymbol;
00172   static bool TrackIds;
00173 
00174   static int Level;
00175 };
00176 
00177 
00178 #endif // CBZ_SYMBOL_H

Generated on February 1, 2006
Back to the C-Breeze home page