1 /* 2 symtab.h 3 4 Symbol table management 5 6 Copyright (C) 2011 Bill Currie <bill@taniwha.org> 7 8 Author: Bill Currie <bill@taniwha.org> 9 Date: 2011/01/04 10 11 This program is free software; you can redistribute it and/or 12 modify it under the terms of the GNU General Public License 13 as published by the Free Software Foundation; either version 2 14 of the License, or (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 19 20 See the GNU General Public License for more details. 21 22 You should have received a copy of the GNU General Public License 23 along with this program; if not, write to: 24 25 Free Software Foundation, Inc. 26 59 Temple Place - Suite 330 27 Boston, MA 02111-1307, USA 28 29 */ 30 31 #ifndef __symtab_h 32 #define __symtab_h 33 34 #include "expr.h" 35 36 struct defspace_s; 37 enum storage_class_e; 38 39 /** \defgroup qfcc_symtab Symbol Table Management 40 \ingroup qfcc 41 */ 42 //@{ 43 44 typedef enum vis_e { 45 vis_public, 46 vis_protected, 47 vis_private, 48 } vis_t; 49 50 typedef enum { 51 sy_var, ///< symbol refers to a variable 52 sy_const, ///< symbol refers to a constant 53 sy_type, ///< symbol refers to a type 54 sy_expr, ///< symbol refers to an expression 55 sy_func, ///< symbol refers to a function 56 sy_class, ///< symbol refers to a class 57 } sy_type_e; 58 59 typedef struct symbol_s { 60 struct symbol_s *next; ///< chain of symbols in symbol table 61 struct symtab_s *table; ///< symbol table that owns this symbol 62 vis_t visibility; ///< symbol visiblity. defaults to public 63 const char *name; ///< symbol name 64 sy_type_e sy_type; ///< symbol type 65 struct type_s *type; ///< type of object to which symbol refers 66 struct param_s *params; ///< the parameters if a function 67 union { 68 int offset; ///< sy_var (in a struct/union) 69 struct def_s *def; ///< sy_var 70 struct ex_value_s *value; ///< sy_const 71 struct expr_s *expr; ///< sy_expr 72 struct function_s *func; ///< sy_func 73 } s; 74 } symbol_t; 75 76 typedef enum { 77 stab_global, ///< global (many symbols) 78 stab_local, ///< local (few symbols: func) 79 stab_struct, 80 stab_union, 81 stab_enum, 82 } stab_type_e; 83 84 typedef struct symtab_s { 85 struct symtab_s *parent; ///< points to parent table 86 struct symtab_s *next; ///< next in global collection of symtabs 87 stab_type_e type; ///< type of symbol table 88 int size; ///< size of structure represented by symtab 89 struct hashtab_s *tab; ///< symbols defined in this table 90 symbol_t *symbols; ///< chain of symbols in this table 91 symbol_t **symtail; ///< keep chain in declaration order 92 struct defspace_s *space; ///< storage for vars in scope symtabs 93 } symtab_t; 94 95 const char *symtype_str (sy_type_e type); 96 97 /** Create a new, empty named symbol. 98 99 Only the symbol name field will be filled in. \a name will be copied 100 using save_string(). 101 102 \param name The name of the symbol. 103 \return The new symbol. 104 */ 105 symbol_t *new_symbol (const char *name); 106 107 /** Create a new, typed, named symbol. 108 109 Only the symbol name and type fields will be filled in. \a name will 110 be copied using save_string(). 111 112 \param name The name of the symbol. 113 \param type The type of the symbol. 114 \return The new symbol. 115 */ 116 symbol_t *new_symbol_type (const char *name, struct type_s *type); 117 118 /** Create a new, empty symbol table. 119 120 The symbol tables support scoping via their \c parent pointer. This 121 supports both code block scoping and ivar inheritance. 122 123 \param parent Pointer to parent scope symbol table. 124 \param type The type of symbol table. Currently governs expected size. 125 \return The new, empty symbol table. 126 */ 127 symtab_t *new_symtab (symtab_t *parent, stab_type_e type); 128 129 /** Lookup a name in the symbol table. 130 131 The entire symbol table chain (symtab_t::parent) starting at symtab 132 will be checked for \a name. 133 134 \param symtab The symbol table to search for \a name. If \a name is 135 not in the symbol table, the tables's parent, if it 136 exists, will be checked, and then its parent, until the 137 end of the chain. 138 \param name The name to look up in the symbol table chain. 139 */ 140 symbol_t *symtab_lookup (symtab_t *symtab, const char *name); 141 142 /** Add a symbol to the symbol table. 143 144 If there is already a symbol with the same name in the symbol table, 145 the symbol will not be added to the table, and the symbol that was 146 found in the table witll be returned. 147 148 \param symtab The symol table to which the symbol will be added. 149 \param symbol The symbol to be added to the symbol table. 150 \return The symbol as in the table, either \a symbol if no 151 symbol with the same name is already in the symbol 152 table, or the symbol that was found in the table. 153 */ 154 symbol_t *symtab_addsymbol (symtab_t *symtab, symbol_t *symbol); 155 156 /** Remove a symbol from the symbol table. 157 158 \param symtab The symol table from which the symbol will be removed. 159 \param symbol The symbol to be removed from the symbol table. 160 \return The symbol as was in the table, or NULL if not found. 161 */ 162 symbol_t *symtab_removesymbol (symtab_t *symtab, symbol_t *symbol); 163 164 /** Make a copy of a symbol. 165 166 The new symbol will not be associated with any table. 167 168 \param symbol The symbol to be copied. 169 \return The new symbol. 170 */ 171 symbol_t *copy_symbol (symbol_t *symbol); 172 173 /** Create a new single symbol table from the given symbol table chain. 174 175 Create a new symbol table and add all of the symbols from the given 176 symbol table chain to the new symbol table. However, in order to 177 preserve scoping rules, duplicate names in ancestor tables will not be 178 added to the new table. 179 180 The new symbol table will be "local". 181 182 The intended use is for creating the ivar scope for methods. 183 184 \param symtab The symbol table chain to be copied. 185 \param parent The parent symbol table of the new symbol table, or 186 null. 187 \return The new symbol table. 188 189 \dot 190 digraph symtab_flat_copy { 191 layout=dot; rankdir=LR; compound=true; nodesep=1.0; 192 subgraph clusterI { 193 node [shape=record]; 194 root [label="<p>parent|integer\ x;|integer\ y;|float\ z;"]; 195 base [label="<p>parent|float\ w;|float\ x;"]; 196 cur [label="<p>parent|float\ y;"]; 197 symtab [shape=ellipse]; 198 cur:p -> base; 199 base:p -> root; 200 } 201 subgraph clusterO { 202 node [shape=record]; 203 out [label="<p>parent|float\ z;|float\ w;|float\ x;|float\ y;"]; 204 return [shape=ellipse]; 205 parent [shape=ellipse]; 206 } 207 symtab -> cur; 208 cur -> out [ltail=clusterI,lhead=clusterO]; 209 out:p -> parent; 210 return -> out; 211 } 212 \enddot 213 */ 214 symtab_t *symtab_flat_copy (symtab_t *symtab, symtab_t *parent); 215 216 /** Create a global symbol and allocate space for a variable. 217 218 If the symbol already exists, it must be of the correct type. If it 219 is external, it will be converted to the given storage class and 220 allocatged space from the given defspace, otherwise it will be 221 returned. 222 223 If the symbol is new, it will be allocated space from the given 224 defspace with the given storage class. 225 226 \param name The name of the symbol. 227 \param type The type of the symbol. 228 \param space The defspace from which space will be allocated for the 229 symbol. Ignored for sc_extern, must not be null for 230 others. 231 \param storage The storage class for the symbol. Only sc_extern, 232 sc_global, and sc_static are valid. 233 */ 234 symbol_t *make_symbol (const char *name, struct type_s *type, 235 struct defspace_s *space, enum storage_class_e storage); 236 237 //@} 238 239 #endif//__symtab_h 240