[0dd3a2f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // Indexer.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 21:37:33 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[a08ba92] | 12 | // Last Modified On : Tue May 19 16:49:55 2015 |
---|
| 13 | // Update Count : 3 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include "SynTree/Declaration.h" |
---|
| 17 | #include "SynTree/Type.h" |
---|
| 18 | #include "SynTree/Expression.h" |
---|
| 19 | #include "SynTree/Initializer.h" |
---|
| 20 | #include "SynTree/Statement.h" |
---|
| 21 | #include "Indexer.h" |
---|
| 22 | #include <typeinfo> |
---|
| 23 | #include "utility.h" |
---|
| 24 | |
---|
[17cd4eb] | 25 | #define debugPrint(x) if ( doDebug ) { std::cout << x; } |
---|
[51b7345] | 26 | |
---|
| 27 | namespace SymTab { |
---|
[a08ba92] | 28 | Indexer::Indexer( bool useDebug ) : doDebug( useDebug ) {} |
---|
[17cd4eb] | 29 | |
---|
[a08ba92] | 30 | Indexer::~Indexer() {} |
---|
[17cd4eb] | 31 | |
---|
[a08ba92] | 32 | void Indexer::visit( ObjectDecl *objectDecl ) { |
---|
[0dd3a2f] | 33 | maybeAccept( objectDecl->get_type(), *this ); |
---|
| 34 | maybeAccept( objectDecl->get_init(), *this ); |
---|
| 35 | maybeAccept( objectDecl->get_bitfieldWidth(), *this ); |
---|
| 36 | if ( objectDecl->get_name() != "" ) { |
---|
| 37 | debugPrint( "Adding object " << objectDecl->get_name() << std::endl ); |
---|
| 38 | idTable.addDecl( objectDecl ); |
---|
| 39 | } // if |
---|
[a08ba92] | 40 | } |
---|
[17cd4eb] | 41 | |
---|
[a08ba92] | 42 | void Indexer::visit( FunctionDecl *functionDecl ) { |
---|
[0dd3a2f] | 43 | if ( functionDecl->get_name() == "" ) return; |
---|
| 44 | debugPrint( "Adding function " << functionDecl->get_name() << std::endl ); |
---|
| 45 | idTable.addDecl( functionDecl ); |
---|
| 46 | enterScope(); |
---|
| 47 | maybeAccept( functionDecl->get_functionType(), *this ); |
---|
| 48 | acceptAll( functionDecl->get_oldDecls(), *this ); |
---|
| 49 | maybeAccept( functionDecl->get_statements(), *this ); |
---|
| 50 | leaveScope(); |
---|
[a08ba92] | 51 | } |
---|
[51b7345] | 52 | |
---|
| 53 | /******** |
---|
| 54 | * A NOTE ON THE ORDER OF TRAVERSAL |
---|
| 55 | * |
---|
| 56 | * Types and typedefs have their base types visited before they are added to the type table. |
---|
| 57 | * This is ok, since there is no such thing as a recursive type or typedef. |
---|
| 58 | * typedef struct { T *x; } T; // never allowed |
---|
| 59 | * |
---|
| 60 | * for structs/unions, it is possible to have recursion, so the decl should be added as if it's |
---|
| 61 | * incomplete to begin, the members are traversed, and then the complete type should be added |
---|
| 62 | * (assuming the type is completed by this particular declaration). |
---|
| 63 | * struct T { struct T *x; }; // allowed |
---|
| 64 | * |
---|
| 65 | * It's important to add the complete type to the symbol table *after* the members/base has been |
---|
| 66 | * traversed, since that traversal may modify the definition of the type and these modifications |
---|
| 67 | * should be visible when the symbol table is queried later in this pass. |
---|
| 68 | * |
---|
| 69 | * TODO: figure out whether recursive contexts are sensible/possible/reasonable. |
---|
| 70 | */ |
---|
| 71 | |
---|
[a08ba92] | 72 | void Indexer::visit( TypeDecl *typeDecl ) { |
---|
[0dd3a2f] | 73 | // see A NOTE ON THE ORDER OF TRAVERSAL, above |
---|
| 74 | // note that assertions come after the type is added to the symtab, since they aren't part |
---|
| 75 | // of the type proper and may depend on the type itself |
---|
| 76 | enterScope(); |
---|
| 77 | acceptAll( typeDecl->get_parameters(), *this ); |
---|
| 78 | maybeAccept( typeDecl->get_base(), *this ); |
---|
| 79 | leaveScope(); |
---|
| 80 | debugPrint( "Adding type " << typeDecl->get_name() << std::endl ); |
---|
| 81 | typeTable.add( typeDecl ); |
---|
| 82 | acceptAll( typeDecl->get_assertions(), *this ); |
---|
[a08ba92] | 83 | } |
---|
[17cd4eb] | 84 | |
---|
[a08ba92] | 85 | void Indexer::visit( TypedefDecl *typeDecl ) { |
---|
[0dd3a2f] | 86 | enterScope(); |
---|
| 87 | acceptAll( typeDecl->get_parameters(), *this ); |
---|
| 88 | maybeAccept( typeDecl->get_base(), *this ); |
---|
| 89 | leaveScope(); |
---|
| 90 | debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl ); |
---|
| 91 | typeTable.add( typeDecl ); |
---|
[a08ba92] | 92 | } |
---|
[17cd4eb] | 93 | |
---|
[a08ba92] | 94 | void Indexer::visit( StructDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 95 | // make up a forward declaration and add it before processing the members |
---|
| 96 | StructDecl fwdDecl( aggregateDecl->get_name() ); |
---|
| 97 | cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); |
---|
| 98 | debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); |
---|
| 99 | structTable.add( &fwdDecl ); |
---|
[51b7345] | 100 | |
---|
[0dd3a2f] | 101 | enterScope(); |
---|
| 102 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 103 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 104 | leaveScope(); |
---|
[51b7345] | 105 | |
---|
[0dd3a2f] | 106 | debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); |
---|
| 107 | // this addition replaces the forward declaration |
---|
| 108 | structTable.add( aggregateDecl ); |
---|
[a08ba92] | 109 | } |
---|
[17cd4eb] | 110 | |
---|
[a08ba92] | 111 | void Indexer::visit( UnionDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 112 | // make up a forward declaration and add it before processing the members |
---|
| 113 | UnionDecl fwdDecl( aggregateDecl->get_name() ); |
---|
| 114 | cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); |
---|
| 115 | debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); |
---|
| 116 | unionTable.add( &fwdDecl ); |
---|
[51b7345] | 117 | |
---|
[0dd3a2f] | 118 | enterScope(); |
---|
| 119 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 120 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 121 | leaveScope(); |
---|
[51b7345] | 122 | |
---|
[0dd3a2f] | 123 | debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); |
---|
| 124 | unionTable.add( aggregateDecl ); |
---|
[a08ba92] | 125 | } |
---|
[17cd4eb] | 126 | |
---|
[a08ba92] | 127 | void Indexer::visit( EnumDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 128 | debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl ); |
---|
| 129 | enumTable.add( aggregateDecl ); |
---|
| 130 | // unlike structs, contexts, and unions, enums inject their members into the global scope |
---|
| 131 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
[a08ba92] | 132 | } |
---|
[17cd4eb] | 133 | |
---|
[a08ba92] | 134 | void Indexer::visit( ContextDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 135 | enterScope(); |
---|
| 136 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 137 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 138 | leaveScope(); |
---|
[51b7345] | 139 | |
---|
[0dd3a2f] | 140 | debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); |
---|
| 141 | contextTable.add( aggregateDecl ); |
---|
[a08ba92] | 142 | } |
---|
[17cd4eb] | 143 | |
---|
[a08ba92] | 144 | void Indexer::visit( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 145 | enterScope(); |
---|
| 146 | acceptAll( compoundStmt->get_kids(), *this ); |
---|
| 147 | leaveScope(); |
---|
[a08ba92] | 148 | } |
---|
[17cd4eb] | 149 | |
---|
[a08ba92] | 150 | void Indexer::visit( ContextInstType *contextInst ) { |
---|
[0dd3a2f] | 151 | acceptAll( contextInst->get_parameters(), *this ); |
---|
| 152 | acceptAll( contextInst->get_members(), *this ); |
---|
[a08ba92] | 153 | } |
---|
[17cd4eb] | 154 | |
---|
[a08ba92] | 155 | void Indexer::visit( StructInstType *structInst ) { |
---|
[0dd3a2f] | 156 | if ( ! structTable.lookup( structInst->get_name() ) ) { |
---|
| 157 | debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl ); |
---|
| 158 | structTable.add( structInst->get_name() ); |
---|
| 159 | } |
---|
| 160 | enterScope(); |
---|
| 161 | acceptAll( structInst->get_parameters(), *this ); |
---|
| 162 | leaveScope(); |
---|
[a08ba92] | 163 | } |
---|
[17cd4eb] | 164 | |
---|
[a08ba92] | 165 | void Indexer::visit( UnionInstType *unionInst ) { |
---|
[0dd3a2f] | 166 | if ( ! unionTable.lookup( unionInst->get_name() ) ) { |
---|
| 167 | debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl ); |
---|
| 168 | unionTable.add( unionInst->get_name() ); |
---|
| 169 | } |
---|
| 170 | enterScope(); |
---|
| 171 | acceptAll( unionInst->get_parameters(), *this ); |
---|
| 172 | leaveScope(); |
---|
[a08ba92] | 173 | } |
---|
[17cd4eb] | 174 | |
---|
[a08ba92] | 175 | void Indexer::visit( ForStmt *forStmt ) { |
---|
| 176 | // for statements introduce a level of scope |
---|
| 177 | enterScope(); |
---|
| 178 | Visitor::visit( forStmt ); |
---|
| 179 | leaveScope(); |
---|
| 180 | } |
---|
[d4778a6] | 181 | |
---|
| 182 | |
---|
[a08ba92] | 183 | void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const { |
---|
[0dd3a2f] | 184 | idTable.lookupId( id, list ); |
---|
[a08ba92] | 185 | } |
---|
[17cd4eb] | 186 | |
---|
[a08ba92] | 187 | DeclarationWithType* Indexer::lookupId( const std::string &id) const { |
---|
[0dd3a2f] | 188 | return idTable.lookupId(id); |
---|
[a08ba92] | 189 | } |
---|
[bdd516a] | 190 | |
---|
[a08ba92] | 191 | NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { |
---|
[0dd3a2f] | 192 | return typeTable.lookup( id ); |
---|
[a08ba92] | 193 | } |
---|
[17cd4eb] | 194 | |
---|
[a08ba92] | 195 | StructDecl *Indexer::lookupStruct( const std::string &id ) const { |
---|
[0dd3a2f] | 196 | return structTable.lookup( id ); |
---|
[a08ba92] | 197 | } |
---|
[17cd4eb] | 198 | |
---|
[a08ba92] | 199 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { |
---|
[0dd3a2f] | 200 | return enumTable.lookup( id ); |
---|
[a08ba92] | 201 | } |
---|
[17cd4eb] | 202 | |
---|
[a08ba92] | 203 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { |
---|
[0dd3a2f] | 204 | return unionTable.lookup( id ); |
---|
[a08ba92] | 205 | } |
---|
[17cd4eb] | 206 | |
---|
[a08ba92] | 207 | ContextDecl * Indexer::lookupContext( const std::string &id ) const { |
---|
[0dd3a2f] | 208 | return contextTable.lookup( id ); |
---|
[a08ba92] | 209 | } |
---|
[17cd4eb] | 210 | |
---|
[a08ba92] | 211 | void Indexer::enterScope() { |
---|
[0dd3a2f] | 212 | if ( doDebug ) { |
---|
| 213 | std::cout << "--- Entering scope" << std::endl; |
---|
| 214 | } |
---|
| 215 | idTable.enterScope(); |
---|
| 216 | typeTable.enterScope(); |
---|
| 217 | structTable.enterScope(); |
---|
| 218 | enumTable.enterScope(); |
---|
| 219 | unionTable.enterScope(); |
---|
| 220 | contextTable.enterScope(); |
---|
[a08ba92] | 221 | } |
---|
[17cd4eb] | 222 | |
---|
[a08ba92] | 223 | void Indexer::leaveScope() { |
---|
[0dd3a2f] | 224 | using std::cout; |
---|
| 225 | using std::endl; |
---|
[51b7345] | 226 | |
---|
[0dd3a2f] | 227 | if ( doDebug ) { |
---|
| 228 | cout << "--- Leaving scope containing" << endl; |
---|
| 229 | idTable.dump( cout ); |
---|
| 230 | typeTable.dump( cout ); |
---|
| 231 | structTable.dump( cout ); |
---|
| 232 | enumTable.dump( cout ); |
---|
| 233 | unionTable.dump( cout ); |
---|
| 234 | contextTable.dump( cout ); |
---|
| 235 | } |
---|
| 236 | idTable.leaveScope(); |
---|
| 237 | typeTable.leaveScope(); |
---|
| 238 | structTable.leaveScope(); |
---|
| 239 | enumTable.leaveScope(); |
---|
| 240 | unionTable.leaveScope(); |
---|
| 241 | contextTable.leaveScope(); |
---|
[a08ba92] | 242 | } |
---|
[17cd4eb] | 243 | |
---|
[a08ba92] | 244 | void Indexer::print( std::ostream &os, int indent ) const { |
---|
| 245 | using std::cerr; |
---|
| 246 | using std::endl; |
---|
[bdd516a] | 247 | |
---|
[a08ba92] | 248 | cerr << "===idTable===" << endl; |
---|
| 249 | idTable.dump( os ); |
---|
| 250 | cerr << "===typeTable===" << endl; |
---|
| 251 | typeTable.dump( os ); |
---|
| 252 | cerr << "===structTable===" << endl; |
---|
| 253 | structTable.dump( os ); |
---|
| 254 | cerr << "===enumTable===" << endl; |
---|
| 255 | enumTable.dump( os ); |
---|
| 256 | cerr << "===unionTable===" << endl; |
---|
| 257 | unionTable.dump( os ); |
---|
| 258 | cerr << "===contextTable===" << endl; |
---|
| 259 | contextTable.dump( os ); |
---|
[bdd516a] | 260 | #if 0 |
---|
[0dd3a2f] | 261 | idTable.dump( os ); |
---|
| 262 | typeTable.dump( os ); |
---|
| 263 | structTable.dump( os ); |
---|
| 264 | enumTable.dump( os ); |
---|
| 265 | unionTable.dump( os ); |
---|
| 266 | contextTable.dump( os ); |
---|
[bdd516a] | 267 | #endif |
---|
[a08ba92] | 268 | } |
---|
[51b7345] | 269 | } // namespace SymTab |
---|
[0dd3a2f] | 270 | |
---|
| 271 | // Local Variables: // |
---|
| 272 | // tab-width: 4 // |
---|
| 273 | // mode: c++ // |
---|
| 274 | // compile-command: "make install" // |
---|
| 275 | // End: // |
---|