[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 |
---|
[44b5ca0] | 12 | // Last Modified On : Fri Jun 5 08:05:17 2015 |
---|
| 13 | // Update Count : 5 |
---|
[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 | |
---|
[44b5ca0] | 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. This is ok, since there is |
---|
| 57 | // no such thing as a recursive type or typedef. |
---|
| 58 | // |
---|
| 59 | // typedef struct { T *x; } T; // never allowed |
---|
| 60 | // |
---|
| 61 | // for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the |
---|
| 62 | // members are traversed, and then the complete type should be added (assuming the type is completed by this particular |
---|
| 63 | // declaration). |
---|
| 64 | // |
---|
| 65 | // struct T { struct T *x; }; // allowed |
---|
| 66 | // |
---|
| 67 | // It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that |
---|
| 68 | // traversal may modify the definition of the type and these modifications should be visible when the symbol table is |
---|
| 69 | // queried later in this pass. |
---|
| 70 | // |
---|
| 71 | // TODO: figure out whether recursive contexts are sensible/possible/reasonable. |
---|
| 72 | |
---|
[51b7345] | 73 | |
---|
[a08ba92] | 74 | void Indexer::visit( TypeDecl *typeDecl ) { |
---|
[0dd3a2f] | 75 | // see A NOTE ON THE ORDER OF TRAVERSAL, above |
---|
[44b5ca0] | 76 | // note that assertions come after the type is added to the symtab, since they are not part of the type proper |
---|
| 77 | // and may depend on the type itself |
---|
[0dd3a2f] | 78 | enterScope(); |
---|
| 79 | acceptAll( typeDecl->get_parameters(), *this ); |
---|
| 80 | maybeAccept( typeDecl->get_base(), *this ); |
---|
| 81 | leaveScope(); |
---|
| 82 | debugPrint( "Adding type " << typeDecl->get_name() << std::endl ); |
---|
| 83 | typeTable.add( typeDecl ); |
---|
| 84 | acceptAll( typeDecl->get_assertions(), *this ); |
---|
[a08ba92] | 85 | } |
---|
[17cd4eb] | 86 | |
---|
[a08ba92] | 87 | void Indexer::visit( TypedefDecl *typeDecl ) { |
---|
[0dd3a2f] | 88 | enterScope(); |
---|
| 89 | acceptAll( typeDecl->get_parameters(), *this ); |
---|
| 90 | maybeAccept( typeDecl->get_base(), *this ); |
---|
| 91 | leaveScope(); |
---|
| 92 | debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl ); |
---|
| 93 | typeTable.add( typeDecl ); |
---|
[a08ba92] | 94 | } |
---|
[17cd4eb] | 95 | |
---|
[a08ba92] | 96 | void Indexer::visit( StructDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 97 | // make up a forward declaration and add it before processing the members |
---|
| 98 | StructDecl fwdDecl( aggregateDecl->get_name() ); |
---|
| 99 | cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); |
---|
| 100 | debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); |
---|
| 101 | structTable.add( &fwdDecl ); |
---|
[51b7345] | 102 | |
---|
[0dd3a2f] | 103 | enterScope(); |
---|
| 104 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 105 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 106 | leaveScope(); |
---|
[51b7345] | 107 | |
---|
[0dd3a2f] | 108 | debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); |
---|
| 109 | // this addition replaces the forward declaration |
---|
| 110 | structTable.add( aggregateDecl ); |
---|
[a08ba92] | 111 | } |
---|
[17cd4eb] | 112 | |
---|
[a08ba92] | 113 | void Indexer::visit( UnionDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 114 | // make up a forward declaration and add it before processing the members |
---|
| 115 | UnionDecl fwdDecl( aggregateDecl->get_name() ); |
---|
| 116 | cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); |
---|
| 117 | debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); |
---|
| 118 | unionTable.add( &fwdDecl ); |
---|
[51b7345] | 119 | |
---|
[0dd3a2f] | 120 | enterScope(); |
---|
| 121 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 122 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 123 | leaveScope(); |
---|
[51b7345] | 124 | |
---|
[0dd3a2f] | 125 | debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); |
---|
| 126 | unionTable.add( aggregateDecl ); |
---|
[a08ba92] | 127 | } |
---|
[17cd4eb] | 128 | |
---|
[a08ba92] | 129 | void Indexer::visit( EnumDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 130 | debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl ); |
---|
| 131 | enumTable.add( aggregateDecl ); |
---|
| 132 | // unlike structs, contexts, and unions, enums inject their members into the global scope |
---|
| 133 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
[a08ba92] | 134 | } |
---|
[17cd4eb] | 135 | |
---|
[a08ba92] | 136 | void Indexer::visit( ContextDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 137 | enterScope(); |
---|
| 138 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 139 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 140 | leaveScope(); |
---|
[51b7345] | 141 | |
---|
[0dd3a2f] | 142 | debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); |
---|
| 143 | contextTable.add( aggregateDecl ); |
---|
[a08ba92] | 144 | } |
---|
[17cd4eb] | 145 | |
---|
[a08ba92] | 146 | void Indexer::visit( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 147 | enterScope(); |
---|
| 148 | acceptAll( compoundStmt->get_kids(), *this ); |
---|
| 149 | leaveScope(); |
---|
[a08ba92] | 150 | } |
---|
[17cd4eb] | 151 | |
---|
[a08ba92] | 152 | void Indexer::visit( ContextInstType *contextInst ) { |
---|
[0dd3a2f] | 153 | acceptAll( contextInst->get_parameters(), *this ); |
---|
| 154 | acceptAll( contextInst->get_members(), *this ); |
---|
[a08ba92] | 155 | } |
---|
[17cd4eb] | 156 | |
---|
[a08ba92] | 157 | void Indexer::visit( StructInstType *structInst ) { |
---|
[0dd3a2f] | 158 | if ( ! structTable.lookup( structInst->get_name() ) ) { |
---|
| 159 | debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl ); |
---|
| 160 | structTable.add( structInst->get_name() ); |
---|
| 161 | } |
---|
| 162 | enterScope(); |
---|
| 163 | acceptAll( structInst->get_parameters(), *this ); |
---|
| 164 | leaveScope(); |
---|
[a08ba92] | 165 | } |
---|
[17cd4eb] | 166 | |
---|
[a08ba92] | 167 | void Indexer::visit( UnionInstType *unionInst ) { |
---|
[0dd3a2f] | 168 | if ( ! unionTable.lookup( unionInst->get_name() ) ) { |
---|
| 169 | debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl ); |
---|
| 170 | unionTable.add( unionInst->get_name() ); |
---|
| 171 | } |
---|
| 172 | enterScope(); |
---|
| 173 | acceptAll( unionInst->get_parameters(), *this ); |
---|
| 174 | leaveScope(); |
---|
[a08ba92] | 175 | } |
---|
[17cd4eb] | 176 | |
---|
[a08ba92] | 177 | void Indexer::visit( ForStmt *forStmt ) { |
---|
| 178 | // for statements introduce a level of scope |
---|
| 179 | enterScope(); |
---|
| 180 | Visitor::visit( forStmt ); |
---|
| 181 | leaveScope(); |
---|
| 182 | } |
---|
[d4778a6] | 183 | |
---|
| 184 | |
---|
[a08ba92] | 185 | void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const { |
---|
[0dd3a2f] | 186 | idTable.lookupId( id, list ); |
---|
[a08ba92] | 187 | } |
---|
[17cd4eb] | 188 | |
---|
[a08ba92] | 189 | DeclarationWithType* Indexer::lookupId( const std::string &id) const { |
---|
[0dd3a2f] | 190 | return idTable.lookupId(id); |
---|
[a08ba92] | 191 | } |
---|
[bdd516a] | 192 | |
---|
[a08ba92] | 193 | NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { |
---|
[0dd3a2f] | 194 | return typeTable.lookup( id ); |
---|
[a08ba92] | 195 | } |
---|
[17cd4eb] | 196 | |
---|
[a08ba92] | 197 | StructDecl *Indexer::lookupStruct( const std::string &id ) const { |
---|
[0dd3a2f] | 198 | return structTable.lookup( id ); |
---|
[a08ba92] | 199 | } |
---|
[17cd4eb] | 200 | |
---|
[a08ba92] | 201 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { |
---|
[0dd3a2f] | 202 | return enumTable.lookup( id ); |
---|
[a08ba92] | 203 | } |
---|
[17cd4eb] | 204 | |
---|
[a08ba92] | 205 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { |
---|
[0dd3a2f] | 206 | return unionTable.lookup( id ); |
---|
[a08ba92] | 207 | } |
---|
[17cd4eb] | 208 | |
---|
[a08ba92] | 209 | ContextDecl * Indexer::lookupContext( const std::string &id ) const { |
---|
[0dd3a2f] | 210 | return contextTable.lookup( id ); |
---|
[a08ba92] | 211 | } |
---|
[17cd4eb] | 212 | |
---|
[a08ba92] | 213 | void Indexer::enterScope() { |
---|
[0dd3a2f] | 214 | if ( doDebug ) { |
---|
| 215 | std::cout << "--- Entering scope" << std::endl; |
---|
| 216 | } |
---|
| 217 | idTable.enterScope(); |
---|
| 218 | typeTable.enterScope(); |
---|
| 219 | structTable.enterScope(); |
---|
| 220 | enumTable.enterScope(); |
---|
| 221 | unionTable.enterScope(); |
---|
| 222 | contextTable.enterScope(); |
---|
[a08ba92] | 223 | } |
---|
[17cd4eb] | 224 | |
---|
[a08ba92] | 225 | void Indexer::leaveScope() { |
---|
[0dd3a2f] | 226 | using std::cout; |
---|
| 227 | using std::endl; |
---|
[51b7345] | 228 | |
---|
[0dd3a2f] | 229 | if ( doDebug ) { |
---|
| 230 | cout << "--- Leaving scope containing" << endl; |
---|
| 231 | idTable.dump( cout ); |
---|
| 232 | typeTable.dump( cout ); |
---|
| 233 | structTable.dump( cout ); |
---|
| 234 | enumTable.dump( cout ); |
---|
| 235 | unionTable.dump( cout ); |
---|
| 236 | contextTable.dump( cout ); |
---|
| 237 | } |
---|
| 238 | idTable.leaveScope(); |
---|
| 239 | typeTable.leaveScope(); |
---|
| 240 | structTable.leaveScope(); |
---|
| 241 | enumTable.leaveScope(); |
---|
| 242 | unionTable.leaveScope(); |
---|
| 243 | contextTable.leaveScope(); |
---|
[a08ba92] | 244 | } |
---|
[17cd4eb] | 245 | |
---|
[a08ba92] | 246 | void Indexer::print( std::ostream &os, int indent ) const { |
---|
| 247 | using std::cerr; |
---|
| 248 | using std::endl; |
---|
[bdd516a] | 249 | |
---|
[a08ba92] | 250 | cerr << "===idTable===" << endl; |
---|
| 251 | idTable.dump( os ); |
---|
| 252 | cerr << "===typeTable===" << endl; |
---|
| 253 | typeTable.dump( os ); |
---|
| 254 | cerr << "===structTable===" << endl; |
---|
| 255 | structTable.dump( os ); |
---|
| 256 | cerr << "===enumTable===" << endl; |
---|
| 257 | enumTable.dump( os ); |
---|
| 258 | cerr << "===unionTable===" << endl; |
---|
| 259 | unionTable.dump( os ); |
---|
| 260 | cerr << "===contextTable===" << endl; |
---|
| 261 | contextTable.dump( os ); |
---|
[bdd516a] | 262 | #if 0 |
---|
[0dd3a2f] | 263 | idTable.dump( os ); |
---|
| 264 | typeTable.dump( os ); |
---|
| 265 | structTable.dump( os ); |
---|
| 266 | enumTable.dump( os ); |
---|
| 267 | unionTable.dump( os ); |
---|
| 268 | contextTable.dump( os ); |
---|
[bdd516a] | 269 | #endif |
---|
[a08ba92] | 270 | } |
---|
[51b7345] | 271 | } // namespace SymTab |
---|
[0dd3a2f] | 272 | |
---|
| 273 | // Local Variables: // |
---|
| 274 | // tab-width: 4 // |
---|
| 275 | // mode: c++ // |
---|
| 276 | // compile-command: "make install" // |
---|
| 277 | // End: // |
---|