| [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 | //
 | 
|---|
| [743fbda] | 7 | // Indexer.cc --
 | 
|---|
| [0dd3a2f] | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Sun May 17 21:37:33 2015
 | 
|---|
| [743fbda] | 11 | // Last Modified By : Rob Schluntz
 | 
|---|
 | 12 | // Last Modified On : Fri Apr 22 15:25:43 2016
 | 
|---|
| [4040425] | 13 | // Update Count     : 11
 | 
|---|
| [0dd3a2f] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [e8032b0] | 16 | #include "Indexer.h"
 | 
|---|
 | 17 | 
 | 
|---|
| [52c2a72] | 18 | #include <string>
 | 
|---|
 | 19 | #include <typeinfo>
 | 
|---|
 | 20 | #include <unordered_map>
 | 
|---|
| [5447e09] | 21 | #include <unordered_set>
 | 
|---|
| [52c2a72] | 22 | #include <utility>
 | 
|---|
 | 23 | 
 | 
|---|
| [bed4c63e] | 24 | #include "Mangler.h"
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | #include "Common/utility.h"
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #include "ResolvExpr/typeops.h"
 | 
|---|
| [e8032b0] | 29 | 
 | 
|---|
| [51b73452] | 30 | #include "SynTree/Declaration.h"
 | 
|---|
 | 31 | #include "SynTree/Type.h"
 | 
|---|
 | 32 | #include "SynTree/Expression.h"
 | 
|---|
 | 33 | #include "SynTree/Initializer.h"
 | 
|---|
 | 34 | #include "SynTree/Statement.h"
 | 
|---|
| [e8032b0] | 35 | 
 | 
|---|
| [17cd4eb] | 36 | #define debugPrint(x) if ( doDebug ) { std::cout << x; }
 | 
|---|
| [51b73452] | 37 | 
 | 
|---|
 | 38 | namespace SymTab {
 | 
|---|
| [ae4c85a] | 39 |         template< typename Container, typename VisitorType >
 | 
|---|
 | 40 |         inline void acceptAllNewScope( Container &container, VisitorType &visitor ) {
 | 
|---|
 | 41 |                 visitor.enterScope();
 | 
|---|
 | 42 |                 acceptAll( container, visitor );
 | 
|---|
 | 43 |                 visitor.leaveScope();
 | 
|---|
 | 44 |         }
 | 
|---|
 | 45 | 
 | 
|---|
| [bed4c63e] | 46 |         typedef std::unordered_map< std::string, DeclarationWithType* > MangleTable;
 | 
|---|
 | 47 |         typedef std::unordered_map< std::string, MangleTable > IdTable;
 | 
|---|
| [52c2a72] | 48 |         typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable;
 | 
|---|
 | 49 |         typedef std::unordered_map< std::string, StructDecl* > StructTable;
 | 
|---|
 | 50 |         typedef std::unordered_map< std::string, EnumDecl* > EnumTable;
 | 
|---|
 | 51 |         typedef std::unordered_map< std::string, UnionDecl* > UnionTable;
 | 
|---|
 | 52 |         typedef std::unordered_map< std::string, TraitDecl* > TraitTable;
 | 
|---|
 | 53 | 
 | 
|---|
| [bed4c63e] | 54 |         void dump( const IdTable &table, std::ostream &os ) {
 | 
|---|
 | 55 |                 for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) {
 | 
|---|
 | 56 |                         for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) {
 | 
|---|
 | 57 |                                 os << mangle->second << std::endl;
 | 
|---|
 | 58 |                         }
 | 
|---|
 | 59 |                 }
 | 
|---|
 | 60 |         }
 | 
|---|
| [743fbda] | 61 | 
 | 
|---|
| [52c2a72] | 62 |         template< typename Decl >
 | 
|---|
 | 63 |         void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) {
 | 
|---|
 | 64 |                 for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) {
 | 
|---|
 | 65 |                         os << it->second << std::endl;
 | 
|---|
 | 66 |                 } // for
 | 
|---|
 | 67 |         }
 | 
|---|
| [743fbda] | 68 | 
 | 
|---|
| [e8032b0] | 69 |         struct Indexer::Impl {
 | 
|---|
| [52c2a72] | 70 |                 Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(),
 | 
|---|
| [e8032b0] | 71 |                                 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
 | 
|---|
| [52c2a72] | 72 |                 Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ),
 | 
|---|
| [e8032b0] | 73 |                                 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
 | 
|---|
 | 74 |                 unsigned long refCount;   ///< Number of references to these tables
 | 
|---|
| [52c2a72] | 75 |                 unsigned long scope;      ///< Scope these tables are associated with
 | 
|---|
| [e8032b0] | 76 |                 unsigned long size;       ///< Number of elements stored in this table
 | 
|---|
 | 77 |                 const Indexer base;       ///< Base indexer this extends
 | 
|---|
| [743fbda] | 78 | 
 | 
|---|
| [e8032b0] | 79 |                 IdTable idTable;          ///< Identifier namespace
 | 
|---|
 | 80 |                 TypeTable typeTable;      ///< Type namespace
 | 
|---|
 | 81 |                 StructTable structTable;  ///< Struct namespace
 | 
|---|
 | 82 |                 EnumTable enumTable;      ///< Enum namespace
 | 
|---|
 | 83 |                 UnionTable unionTable;    ///< Union namespace
 | 
|---|
 | 84 |                 TraitTable traitTable;    ///< Trait namespace
 | 
|---|
 | 85 |         };
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |         Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) {
 | 
|---|
 | 88 |                 if ( ! toClone ) return 0;
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 |                 // shorten the search chain by skipping empty links
 | 
|---|
 | 91 |                 Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone;
 | 
|---|
 | 92 |                 if ( ret ) { ++ret->refCount; }
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |                 return ret;
 | 
|---|
 | 95 |         }
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 |         void Indexer::deleteRef( Indexer::Impl *toFree ) {
 | 
|---|
 | 98 |                 if ( ! toFree ) return;
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |                 if ( --toFree->refCount == 0 ) delete toFree;
 | 
|---|
 | 101 |         }
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 |         void Indexer::makeWritable() {
 | 
|---|
 | 104 |                 if ( ! tables ) {
 | 
|---|
| [52c2a72] | 105 |                         // create indexer if not yet set
 | 
|---|
 | 106 |                         tables = new Indexer::Impl( scope );
 | 
|---|
 | 107 |                 } else if ( tables->refCount > 1 || tables->scope != scope ) {
 | 
|---|
 | 108 |                         // make this indexer the base of a fresh indexer at the current scope
 | 
|---|
 | 109 |                         tables = new Indexer::Impl( scope, std::move( *this ) );
 | 
|---|
| [e8032b0] | 110 |                 }
 | 
|---|
 | 111 |         }
 | 
|---|
 | 112 | 
 | 
|---|
| [52c2a72] | 113 |         Indexer::Indexer( bool _doDebug ) : tables( 0 ), scope( 0 ), doDebug( _doDebug ) {}
 | 
|---|
| [e8032b0] | 114 | 
 | 
|---|
| [52c2a72] | 115 |         Indexer::Indexer( const Indexer &that ) : tables( newRef( that.tables ) ), scope( that.scope ), doDebug( that.doDebug ) {}
 | 
|---|
| [e8032b0] | 116 | 
 | 
|---|
| [52c2a72] | 117 |         Indexer::Indexer( Indexer &&that ) : tables( that.tables ), scope( that.scope ), doDebug( that.doDebug ) {
 | 
|---|
| [e8032b0] | 118 |                 that.tables = 0;
 | 
|---|
 | 119 |         }
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 |         Indexer::~Indexer() {
 | 
|---|
 | 122 |                 deleteRef( tables );
 | 
|---|
 | 123 |         }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |         Indexer& Indexer::operator= ( const Indexer &that ) {
 | 
|---|
 | 126 |                 deleteRef( tables );
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |                 tables = newRef( that.tables );
 | 
|---|
| [52c2a72] | 129 |                 scope = that.scope;
 | 
|---|
| [e8032b0] | 130 |                 doDebug = that.doDebug;
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 |                 return *this;
 | 
|---|
 | 133 |         }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 |         Indexer& Indexer::operator= ( Indexer &&that ) {
 | 
|---|
 | 136 |                 deleteRef( tables );
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 |                 tables = that.tables;
 | 
|---|
| [52c2a72] | 139 |                 scope = that.scope;
 | 
|---|
| [e8032b0] | 140 |                 doDebug = that.doDebug;
 | 
|---|
| [17cd4eb] | 141 | 
 | 
|---|
| [e8032b0] | 142 |                 that.tables = 0;
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 |                 return *this;
 | 
|---|
 | 145 |         }
 | 
|---|
| [17cd4eb] | 146 | 
 | 
|---|
| [a08ba92] | 147 |         void Indexer::visit( ObjectDecl *objectDecl ) {
 | 
|---|
| [ae4c85a] | 148 |                 enterScope();
 | 
|---|
| [0dd3a2f] | 149 |                 maybeAccept( objectDecl->get_type(), *this );
 | 
|---|
| [ae4c85a] | 150 |                 leaveScope();
 | 
|---|
| [0dd3a2f] | 151 |                 maybeAccept( objectDecl->get_init(), *this );
 | 
|---|
 | 152 |                 maybeAccept( objectDecl->get_bitfieldWidth(), *this );
 | 
|---|
 | 153 |                 if ( objectDecl->get_name() != "" ) {
 | 
|---|
 | 154 |                         debugPrint( "Adding object " << objectDecl->get_name() << std::endl );
 | 
|---|
| [e8032b0] | 155 |                         addId( objectDecl );
 | 
|---|
| [0dd3a2f] | 156 |                 } // if
 | 
|---|
| [a08ba92] | 157 |         }
 | 
|---|
| [17cd4eb] | 158 | 
 | 
|---|
| [a08ba92] | 159 |         void Indexer::visit( FunctionDecl *functionDecl ) {
 | 
|---|
| [0dd3a2f] | 160 |                 if ( functionDecl->get_name() == "" ) return;
 | 
|---|
 | 161 |                 debugPrint( "Adding function " << functionDecl->get_name() << std::endl );
 | 
|---|
| [e8032b0] | 162 |                 addId( functionDecl );
 | 
|---|
| [0dd3a2f] | 163 |                 enterScope();
 | 
|---|
 | 164 |                 maybeAccept( functionDecl->get_functionType(), *this );
 | 
|---|
 | 165 |                 acceptAll( functionDecl->get_oldDecls(), *this );
 | 
|---|
 | 166 |                 maybeAccept( functionDecl->get_statements(), *this );
 | 
|---|
 | 167 |                 leaveScope();
 | 
|---|
| [a08ba92] | 168 |         }
 | 
|---|
| [51b73452] | 169 | 
 | 
|---|
| [44b5ca0] | 170 | 
 | 
|---|
 | 171 | // A NOTE ON THE ORDER OF TRAVERSAL
 | 
|---|
 | 172 | //
 | 
|---|
 | 173 | // Types and typedefs have their base types visited before they are added to the type table.  This is ok, since there is
 | 
|---|
 | 174 | // no such thing as a recursive type or typedef.
 | 
|---|
 | 175 | //
 | 
|---|
 | 176 | //             typedef struct { T *x; } T; // never allowed
 | 
|---|
 | 177 | //
 | 
|---|
 | 178 | // for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the
 | 
|---|
 | 179 | // members are traversed, and then the complete type should be added (assuming the type is completed by this particular
 | 
|---|
 | 180 | // declaration).
 | 
|---|
 | 181 | //
 | 
|---|
 | 182 | //             struct T { struct T *x; }; // allowed
 | 
|---|
 | 183 | //
 | 
|---|
 | 184 | // It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that
 | 
|---|
 | 185 | // traversal may modify the definition of the type and these modifications should be visible when the symbol table is
 | 
|---|
 | 186 | // queried later in this pass.
 | 
|---|
 | 187 | //
 | 
|---|
 | 188 | // TODO: figure out whether recursive contexts are sensible/possible/reasonable.
 | 
|---|
 | 189 | 
 | 
|---|
| [51b73452] | 190 | 
 | 
|---|
| [a08ba92] | 191 |         void Indexer::visit( TypeDecl *typeDecl ) {
 | 
|---|
| [0dd3a2f] | 192 |                 // see A NOTE ON THE ORDER OF TRAVERSAL, above
 | 
|---|
| [44b5ca0] | 193 |                 // note that assertions come after the type is added to the symtab, since they are not part of the type proper
 | 
|---|
 | 194 |                 // and may depend on the type itself
 | 
|---|
| [0dd3a2f] | 195 |                 enterScope();
 | 
|---|
 | 196 |                 acceptAll( typeDecl->get_parameters(), *this );
 | 
|---|
 | 197 |                 maybeAccept( typeDecl->get_base(), *this );
 | 
|---|
 | 198 |                 leaveScope();
 | 
|---|
 | 199 |                 debugPrint( "Adding type " << typeDecl->get_name() << std::endl );
 | 
|---|
| [e8032b0] | 200 |                 addType( typeDecl );
 | 
|---|
| [0dd3a2f] | 201 |                 acceptAll( typeDecl->get_assertions(), *this );
 | 
|---|
| [a08ba92] | 202 |         }
 | 
|---|
| [17cd4eb] | 203 | 
 | 
|---|
| [a08ba92] | 204 |         void Indexer::visit( TypedefDecl *typeDecl ) {
 | 
|---|
| [0dd3a2f] | 205 |                 enterScope();
 | 
|---|
 | 206 |                 acceptAll( typeDecl->get_parameters(), *this );
 | 
|---|
 | 207 |                 maybeAccept( typeDecl->get_base(), *this );
 | 
|---|
 | 208 |                 leaveScope();
 | 
|---|
 | 209 |                 debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl );
 | 
|---|
| [e8032b0] | 210 |                 addType( typeDecl );
 | 
|---|
| [a08ba92] | 211 |         }
 | 
|---|
| [17cd4eb] | 212 | 
 | 
|---|
| [a08ba92] | 213 |         void Indexer::visit( StructDecl *aggregateDecl ) {
 | 
|---|
| [0dd3a2f] | 214 |                 // make up a forward declaration and add it before processing the members
 | 
|---|
| [743fbda] | 215 |                 // needs to be on the heap because addStruct saves the pointer
 | 
|---|
 | 216 |                 StructDecl &fwdDecl = *new StructDecl( aggregateDecl->get_name() );
 | 
|---|
| [0dd3a2f] | 217 |                 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
 | 
|---|
 | 218 |                 debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
 | 
|---|
| [e8032b0] | 219 |                 addStruct( &fwdDecl );
 | 
|---|
| [743fbda] | 220 | 
 | 
|---|
| [0dd3a2f] | 221 |                 enterScope();
 | 
|---|
 | 222 |                 acceptAll( aggregateDecl->get_parameters(), *this );
 | 
|---|
 | 223 |                 acceptAll( aggregateDecl->get_members(), *this );
 | 
|---|
 | 224 |                 leaveScope();
 | 
|---|
| [743fbda] | 225 | 
 | 
|---|
| [0dd3a2f] | 226 |                 debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
 | 
|---|
 | 227 |                 // this addition replaces the forward declaration
 | 
|---|
| [e8032b0] | 228 |                 addStruct( aggregateDecl );
 | 
|---|
| [a08ba92] | 229 |         }
 | 
|---|
| [17cd4eb] | 230 | 
 | 
|---|
| [a08ba92] | 231 |         void Indexer::visit( UnionDecl *aggregateDecl ) {
 | 
|---|
| [0dd3a2f] | 232 |                 // make up a forward declaration and add it before processing the members
 | 
|---|
 | 233 |                 UnionDecl fwdDecl( aggregateDecl->get_name() );
 | 
|---|
 | 234 |                 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
 | 
|---|
 | 235 |                 debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
 | 
|---|
| [e8032b0] | 236 |                 addUnion( &fwdDecl );
 | 
|---|
| [743fbda] | 237 | 
 | 
|---|
| [0dd3a2f] | 238 |                 enterScope();
 | 
|---|
 | 239 |                 acceptAll( aggregateDecl->get_parameters(), *this );
 | 
|---|
 | 240 |                 acceptAll( aggregateDecl->get_members(), *this );
 | 
|---|
 | 241 |                 leaveScope();
 | 
|---|
| [743fbda] | 242 | 
 | 
|---|
| [0dd3a2f] | 243 |                 debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
 | 
|---|
| [e8032b0] | 244 |                 addUnion( aggregateDecl );
 | 
|---|
| [a08ba92] | 245 |         }
 | 
|---|
| [17cd4eb] | 246 | 
 | 
|---|
| [a08ba92] | 247 |         void Indexer::visit( EnumDecl *aggregateDecl ) {
 | 
|---|
| [0dd3a2f] | 248 |                 debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl );
 | 
|---|
| [e8032b0] | 249 |                 addEnum( aggregateDecl );
 | 
|---|
| [0dd3a2f] | 250 |                 // unlike structs, contexts, and unions, enums inject their members into the global scope
 | 
|---|
 | 251 |                 acceptAll( aggregateDecl->get_members(), *this );
 | 
|---|
| [a08ba92] | 252 |         }
 | 
|---|
| [17cd4eb] | 253 | 
 | 
|---|
| [4040425] | 254 |         void Indexer::visit( TraitDecl *aggregateDecl ) {
 | 
|---|
| [0dd3a2f] | 255 |                 enterScope();
 | 
|---|
 | 256 |                 acceptAll( aggregateDecl->get_parameters(), *this );
 | 
|---|
 | 257 |                 acceptAll( aggregateDecl->get_members(), *this );
 | 
|---|
 | 258 |                 leaveScope();
 | 
|---|
| [743fbda] | 259 | 
 | 
|---|
| [0dd3a2f] | 260 |                 debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
 | 
|---|
| [e8032b0] | 261 |                 addTrait( aggregateDecl );
 | 
|---|
| [a08ba92] | 262 |         }
 | 
|---|
| [17cd4eb] | 263 | 
 | 
|---|
| [a08ba92] | 264 |         void Indexer::visit( CompoundStmt *compoundStmt ) {
 | 
|---|
| [0dd3a2f] | 265 |                 enterScope();
 | 
|---|
 | 266 |                 acceptAll( compoundStmt->get_kids(), *this );
 | 
|---|
 | 267 |                 leaveScope();
 | 
|---|
| [a08ba92] | 268 |         }
 | 
|---|
| [17cd4eb] | 269 | 
 | 
|---|
| [ae4c85a] | 270 | 
 | 
|---|
 | 271 |         void Indexer::visit( ApplicationExpr *applicationExpr ) {
 | 
|---|
 | 272 |                 acceptAllNewScope( applicationExpr->get_results(), *this );
 | 
|---|
 | 273 |                 maybeAccept( applicationExpr->get_function(), *this );
 | 
|---|
 | 274 |                 acceptAll( applicationExpr->get_args(), *this );
 | 
|---|
 | 275 |         }
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 |         void Indexer::visit( UntypedExpr *untypedExpr ) {
 | 
|---|
 | 278 |                 acceptAllNewScope( untypedExpr->get_results(), *this );
 | 
|---|
 | 279 |                 acceptAll( untypedExpr->get_args(), *this );
 | 
|---|
 | 280 |         }
 | 
|---|
 | 281 | 
 | 
|---|
 | 282 |         void Indexer::visit( NameExpr *nameExpr ) {
 | 
|---|
 | 283 |                 acceptAllNewScope( nameExpr->get_results(), *this );
 | 
|---|
 | 284 |         }
 | 
|---|
 | 285 | 
 | 
|---|
 | 286 |         void Indexer::visit( AddressExpr *addressExpr ) {
 | 
|---|
 | 287 |                 acceptAllNewScope( addressExpr->get_results(), *this );
 | 
|---|
 | 288 |                 maybeAccept( addressExpr->get_arg(), *this );
 | 
|---|
 | 289 |         }
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 |         void Indexer::visit( LabelAddressExpr *labAddressExpr ) {
 | 
|---|
 | 292 |                 acceptAllNewScope( labAddressExpr->get_results(), *this );
 | 
|---|
 | 293 |                 maybeAccept( labAddressExpr->get_arg(), *this );
 | 
|---|
 | 294 |         }
 | 
|---|
 | 295 | 
 | 
|---|
 | 296 |         void Indexer::visit( CastExpr *castExpr ) {
 | 
|---|
 | 297 |                 acceptAllNewScope( castExpr->get_results(), *this );
 | 
|---|
 | 298 |                 maybeAccept( castExpr->get_arg(), *this );
 | 
|---|
 | 299 |         }
 | 
|---|
 | 300 | 
 | 
|---|
 | 301 |         void Indexer::visit( UntypedMemberExpr *memberExpr ) {
 | 
|---|
 | 302 |                 acceptAllNewScope( memberExpr->get_results(), *this );
 | 
|---|
 | 303 |                 maybeAccept( memberExpr->get_aggregate(), *this );
 | 
|---|
 | 304 |         }
 | 
|---|
 | 305 | 
 | 
|---|
 | 306 |         void Indexer::visit( MemberExpr *memberExpr ) {
 | 
|---|
 | 307 |                 acceptAllNewScope( memberExpr->get_results(), *this );
 | 
|---|
 | 308 |                 maybeAccept( memberExpr->get_aggregate(), *this );
 | 
|---|
 | 309 |         }
 | 
|---|
 | 310 | 
 | 
|---|
 | 311 |         void Indexer::visit( VariableExpr *variableExpr ) {
 | 
|---|
 | 312 |                 acceptAllNewScope( variableExpr->get_results(), *this );
 | 
|---|
 | 313 |         }
 | 
|---|
 | 314 | 
 | 
|---|
 | 315 |         void Indexer::visit( ConstantExpr *constantExpr ) {
 | 
|---|
 | 316 |                 acceptAllNewScope( constantExpr->get_results(), *this );
 | 
|---|
 | 317 |                 maybeAccept( constantExpr->get_constant(), *this );
 | 
|---|
 | 318 |         }
 | 
|---|
 | 319 | 
 | 
|---|
 | 320 |         void Indexer::visit( SizeofExpr *sizeofExpr ) {
 | 
|---|
 | 321 |                 acceptAllNewScope( sizeofExpr->get_results(), *this );
 | 
|---|
 | 322 |                 if ( sizeofExpr->get_isType() ) {
 | 
|---|
 | 323 |                         maybeAccept( sizeofExpr->get_type(), *this );
 | 
|---|
 | 324 |                 } else {
 | 
|---|
 | 325 |                         maybeAccept( sizeofExpr->get_expr(), *this );
 | 
|---|
 | 326 |                 }
 | 
|---|
 | 327 |         }
 | 
|---|
 | 328 | 
 | 
|---|
| [47534159] | 329 |         void Indexer::visit( AlignofExpr *alignofExpr ) {
 | 
|---|
 | 330 |                 acceptAllNewScope( alignofExpr->get_results(), *this );
 | 
|---|
 | 331 |                 if ( alignofExpr->get_isType() ) {
 | 
|---|
 | 332 |                         maybeAccept( alignofExpr->get_type(), *this );
 | 
|---|
 | 333 |                 } else {
 | 
|---|
 | 334 |                         maybeAccept( alignofExpr->get_expr(), *this );
 | 
|---|
 | 335 |                 }
 | 
|---|
 | 336 |         }
 | 
|---|
 | 337 | 
 | 
|---|
| [2a4b088] | 338 |         void Indexer::visit( UntypedOffsetofExpr *offsetofExpr ) {
 | 
|---|
 | 339 |                 acceptAllNewScope( offsetofExpr->get_results(), *this );
 | 
|---|
 | 340 |                 maybeAccept( offsetofExpr->get_type(), *this );
 | 
|---|
 | 341 |         }
 | 
|---|
 | 342 | 
 | 
|---|
| [25a054f] | 343 |         void Indexer::visit( OffsetofExpr *offsetofExpr ) {
 | 
|---|
 | 344 |                 acceptAllNewScope( offsetofExpr->get_results(), *this );
 | 
|---|
 | 345 |                 maybeAccept( offsetofExpr->get_type(), *this );
 | 
|---|
 | 346 |                 maybeAccept( offsetofExpr->get_member(), *this );
 | 
|---|
 | 347 |         }
 | 
|---|
 | 348 | 
 | 
|---|
| [afc1045] | 349 |         void Indexer::visit( OffsetPackExpr *offsetPackExpr ) {
 | 
|---|
 | 350 |                 acceptAllNewScope( offsetPackExpr->get_results(), *this );
 | 
|---|
 | 351 |                 maybeAccept( offsetPackExpr->get_type(), *this );
 | 
|---|
 | 352 |         }
 | 
|---|
 | 353 | 
 | 
|---|
| [ae4c85a] | 354 |         void Indexer::visit( AttrExpr *attrExpr ) {
 | 
|---|
 | 355 |                 acceptAllNewScope( attrExpr->get_results(), *this );
 | 
|---|
 | 356 |                 if ( attrExpr->get_isType() ) {
 | 
|---|
 | 357 |                         maybeAccept( attrExpr->get_type(), *this );
 | 
|---|
 | 358 |                 } else {
 | 
|---|
 | 359 |                         maybeAccept( attrExpr->get_expr(), *this );
 | 
|---|
 | 360 |                 }
 | 
|---|
 | 361 |         }
 | 
|---|
 | 362 | 
 | 
|---|
 | 363 |         void Indexer::visit( LogicalExpr *logicalExpr ) {
 | 
|---|
 | 364 |                 acceptAllNewScope( logicalExpr->get_results(), *this );
 | 
|---|
 | 365 |                 maybeAccept( logicalExpr->get_arg1(), *this );
 | 
|---|
 | 366 |                 maybeAccept( logicalExpr->get_arg2(), *this );
 | 
|---|
 | 367 |         }
 | 
|---|
 | 368 | 
 | 
|---|
 | 369 |         void Indexer::visit( ConditionalExpr *conditionalExpr ) {
 | 
|---|
 | 370 |                 acceptAllNewScope( conditionalExpr->get_results(), *this );
 | 
|---|
 | 371 |                 maybeAccept( conditionalExpr->get_arg1(), *this );
 | 
|---|
 | 372 |                 maybeAccept( conditionalExpr->get_arg2(), *this );
 | 
|---|
 | 373 |                 maybeAccept( conditionalExpr->get_arg3(), *this );
 | 
|---|
 | 374 |         }
 | 
|---|
 | 375 | 
 | 
|---|
 | 376 |         void Indexer::visit( CommaExpr *commaExpr ) {
 | 
|---|
 | 377 |                 acceptAllNewScope( commaExpr->get_results(), *this );
 | 
|---|
 | 378 |                 maybeAccept( commaExpr->get_arg1(), *this );
 | 
|---|
 | 379 |                 maybeAccept( commaExpr->get_arg2(), *this );
 | 
|---|
 | 380 |         }
 | 
|---|
 | 381 | 
 | 
|---|
 | 382 |         void Indexer::visit( TupleExpr *tupleExpr ) {
 | 
|---|
 | 383 |                 acceptAllNewScope( tupleExpr->get_results(), *this );
 | 
|---|
 | 384 |                 acceptAll( tupleExpr->get_exprs(), *this );
 | 
|---|
 | 385 |         }
 | 
|---|
 | 386 | 
 | 
|---|
 | 387 |         void Indexer::visit( SolvedTupleExpr *tupleExpr ) {
 | 
|---|
 | 388 |                 acceptAllNewScope( tupleExpr->get_results(), *this );
 | 
|---|
 | 389 |                 acceptAll( tupleExpr->get_exprs(), *this );
 | 
|---|
 | 390 |         }
 | 
|---|
 | 391 | 
 | 
|---|
 | 392 |         void Indexer::visit( TypeExpr *typeExpr ) {
 | 
|---|
 | 393 |                 acceptAllNewScope( typeExpr->get_results(), *this );
 | 
|---|
 | 394 |                 maybeAccept( typeExpr->get_type(), *this );
 | 
|---|
 | 395 |         }
 | 
|---|
 | 396 | 
 | 
|---|
 | 397 |         void Indexer::visit( AsmExpr *asmExpr ) {
 | 
|---|
 | 398 |                 maybeAccept( asmExpr->get_inout(), *this );
 | 
|---|
 | 399 |                 maybeAccept( asmExpr->get_constraint(), *this );
 | 
|---|
 | 400 |                 maybeAccept( asmExpr->get_operand(), *this );
 | 
|---|
 | 401 |         }
 | 
|---|
 | 402 | 
 | 
|---|
 | 403 |         void Indexer::visit( UntypedValofExpr *valofExpr ) {
 | 
|---|
 | 404 |                 acceptAllNewScope( valofExpr->get_results(), *this );
 | 
|---|
 | 405 |                 maybeAccept( valofExpr->get_body(), *this );
 | 
|---|
 | 406 |         }
 | 
|---|
 | 407 | 
 | 
|---|
 | 408 | 
 | 
|---|
| [4040425] | 409 |         void Indexer::visit( TraitInstType *contextInst ) {
 | 
|---|
| [0dd3a2f] | 410 |                 acceptAll( contextInst->get_parameters(), *this );
 | 
|---|
 | 411 |                 acceptAll( contextInst->get_members(), *this );
 | 
|---|
| [a08ba92] | 412 |         }
 | 
|---|
| [17cd4eb] | 413 | 
 | 
|---|
| [a08ba92] | 414 |         void Indexer::visit( StructInstType *structInst ) {
 | 
|---|
| [e8032b0] | 415 |                 if ( ! lookupStruct( structInst->get_name() ) ) {
 | 
|---|
| [0dd3a2f] | 416 |                         debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl );
 | 
|---|
| [e8032b0] | 417 |                         addStruct( structInst->get_name() );
 | 
|---|
| [0dd3a2f] | 418 |                 }
 | 
|---|
 | 419 |                 enterScope();
 | 
|---|
 | 420 |                 acceptAll( structInst->get_parameters(), *this );
 | 
|---|
 | 421 |                 leaveScope();
 | 
|---|
| [a08ba92] | 422 |         }
 | 
|---|
| [17cd4eb] | 423 | 
 | 
|---|
| [a08ba92] | 424 |         void Indexer::visit( UnionInstType *unionInst ) {
 | 
|---|
| [e8032b0] | 425 |                 if ( ! lookupUnion( unionInst->get_name() ) ) {
 | 
|---|
| [0dd3a2f] | 426 |                         debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl );
 | 
|---|
| [e8032b0] | 427 |                         addUnion( unionInst->get_name() );
 | 
|---|
| [0dd3a2f] | 428 |                 }
 | 
|---|
 | 429 |                 enterScope();
 | 
|---|
 | 430 |                 acceptAll( unionInst->get_parameters(), *this );
 | 
|---|
 | 431 |                 leaveScope();
 | 
|---|
| [a08ba92] | 432 |         }
 | 
|---|
| [17cd4eb] | 433 | 
 | 
|---|
| [a08ba92] | 434 |         void Indexer::visit( ForStmt *forStmt ) {
 | 
|---|
 | 435 |             // for statements introduce a level of scope
 | 
|---|
 | 436 |             enterScope();
 | 
|---|
 | 437 |             Visitor::visit( forStmt );
 | 
|---|
 | 438 |             leaveScope();
 | 
|---|
 | 439 |         }
 | 
|---|
| [d4778a6] | 440 | 
 | 
|---|
| [743fbda] | 441 | 
 | 
|---|
| [d4778a6] | 442 | 
 | 
|---|
| [bed4c63e] | 443 |         void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const {
 | 
|---|
| [5447e09] | 444 |                 std::unordered_set< std::string > foundMangleNames;
 | 
|---|
| [743fbda] | 445 | 
 | 
|---|
| [5447e09] | 446 |                 Indexer::Impl *searchTables = tables;
 | 
|---|
 | 447 |                 while ( searchTables ) {
 | 
|---|
 | 448 | 
 | 
|---|
 | 449 |                         IdTable::const_iterator decls = searchTables->idTable.find( id );
 | 
|---|
 | 450 |                         if ( decls != searchTables->idTable.end() ) {
 | 
|---|
 | 451 |                                 const MangleTable &mangleTable = decls->second;
 | 
|---|
 | 452 |                                 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
 | 
|---|
 | 453 |                                         // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found
 | 
|---|
 | 454 |                                         if ( foundMangleNames.insert( decl->first ).second == false ) continue;
 | 
|---|
| [743fbda] | 455 | 
 | 
|---|
| [5447e09] | 456 |                                         out.push_back( decl->second );
 | 
|---|
 | 457 |                                 }
 | 
|---|
| [bed4c63e] | 458 |                         }
 | 
|---|
| [743fbda] | 459 | 
 | 
|---|
| [5447e09] | 460 |                         // get declarations from base indexers
 | 
|---|
 | 461 |                         searchTables = searchTables->base.tables;
 | 
|---|
| [bed4c63e] | 462 |                 }
 | 
|---|
| [a08ba92] | 463 |         }
 | 
|---|
| [bdd516a] | 464 | 
 | 
|---|
| [a08ba92] | 465 |         NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
 | 
|---|
| [e8032b0] | 466 |                 if ( ! tables ) return 0;
 | 
|---|
 | 467 | 
 | 
|---|
| [52c2a72] | 468 |                 TypeTable::const_iterator ret = tables->typeTable.find( id );
 | 
|---|
 | 469 |                 return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id );
 | 
|---|
| [a08ba92] | 470 |         }
 | 
|---|
| [17cd4eb] | 471 | 
 | 
|---|
| [a08ba92] | 472 |         StructDecl *Indexer::lookupStruct( const std::string &id ) const {
 | 
|---|
| [e8032b0] | 473 |                 if ( ! tables ) return 0;
 | 
|---|
 | 474 | 
 | 
|---|
| [52c2a72] | 475 |                 StructTable::const_iterator ret = tables->structTable.find( id );
 | 
|---|
 | 476 |                 return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id );
 | 
|---|
| [a08ba92] | 477 |         }
 | 
|---|
| [17cd4eb] | 478 | 
 | 
|---|
| [a08ba92] | 479 |         EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
 | 
|---|
| [e8032b0] | 480 |                 if ( ! tables ) return 0;
 | 
|---|
 | 481 | 
 | 
|---|
| [52c2a72] | 482 |                 EnumTable::const_iterator ret = tables->enumTable.find( id );
 | 
|---|
 | 483 |                 return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id );
 | 
|---|
| [a08ba92] | 484 |         }
 | 
|---|
| [17cd4eb] | 485 | 
 | 
|---|
| [a08ba92] | 486 |         UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
 | 
|---|
| [e8032b0] | 487 |                 if ( ! tables ) return 0;
 | 
|---|
 | 488 | 
 | 
|---|
| [52c2a72] | 489 |                 UnionTable::const_iterator ret = tables->unionTable.find( id );
 | 
|---|
 | 490 |                 return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id );
 | 
|---|
| [e8032b0] | 491 |         }
 | 
|---|
 | 492 | 
 | 
|---|
 | 493 |         TraitDecl *Indexer::lookupTrait( const std::string &id ) const {
 | 
|---|
 | 494 |                 if ( ! tables ) return 0;
 | 
|---|
 | 495 | 
 | 
|---|
| [52c2a72] | 496 |                 TraitTable::const_iterator ret = tables->traitTable.find( id );
 | 
|---|
 | 497 |                 return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id );
 | 
|---|
 | 498 |         }
 | 
|---|
 | 499 | 
 | 
|---|
| [bed4c63e] | 500 |         DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
 | 
|---|
 | 501 |                 if ( ! tables ) return 0;
 | 
|---|
 | 502 |                 if ( tables->scope < scope ) return 0;
 | 
|---|
 | 503 | 
 | 
|---|
 | 504 |                 IdTable::const_iterator decls = tables->idTable.find( id );
 | 
|---|
 | 505 |                 if ( decls != tables->idTable.end() ) {
 | 
|---|
 | 506 |                         const MangleTable &mangleTable = decls->second;
 | 
|---|
 | 507 |                         MangleTable::const_iterator decl = mangleTable.find( mangleName );
 | 
|---|
 | 508 |                         if ( decl != mangleTable.end() ) return decl->second;
 | 
|---|
 | 509 |                 }
 | 
|---|
 | 510 | 
 | 
|---|
 | 511 |                 return tables->base.lookupIdAtScope( id, mangleName, scope );
 | 
|---|
 | 512 |         }
 | 
|---|
 | 513 | 
 | 
|---|
| [09d1ad0] | 514 |         bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
 | 
|---|
| [bed4c63e] | 515 |                 if ( ! tables ) return false;
 | 
|---|
| [09d1ad0] | 516 |                 if ( tables->scope < scope ) return false;
 | 
|---|
| [bed4c63e] | 517 | 
 | 
|---|
 | 518 |                 IdTable::const_iterator decls = tables->idTable.find( id );
 | 
|---|
 | 519 |                 if ( decls != tables->idTable.end() ) {
 | 
|---|
 | 520 |                         const MangleTable &mangleTable = decls->second;
 | 
|---|
 | 521 |                         for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
 | 
|---|
| [743fbda] | 522 |                                 // check for C decls with the same name, skipping
 | 
|---|
| [5447e09] | 523 |                                 // those with a compatible type (by mangleName)
 | 
|---|
 | 524 |                                 if ( decl->second->get_linkage() == LinkageSpec::C && decl->first != mangleName ) return true;
 | 
|---|
| [bed4c63e] | 525 |                         }
 | 
|---|
 | 526 |                 }
 | 
|---|
 | 527 | 
 | 
|---|
| [09d1ad0] | 528 |                 return tables->base.hasIncompatibleCDecl( id, mangleName, scope );
 | 
|---|
| [bed4c63e] | 529 |         }
 | 
|---|
| [743fbda] | 530 | 
 | 
|---|
| [52c2a72] | 531 |         NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const {
 | 
|---|
 | 532 |                 if ( ! tables ) return 0;
 | 
|---|
 | 533 |                 if ( tables->scope < scope ) return 0;
 | 
|---|
 | 534 | 
 | 
|---|
 | 535 |                 TypeTable::const_iterator ret = tables->typeTable.find( id );
 | 
|---|
 | 536 |                 return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope );
 | 
|---|
 | 537 |         }
 | 
|---|
| [743fbda] | 538 | 
 | 
|---|
| [52c2a72] | 539 |         StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const {
 | 
|---|
 | 540 |                 if ( ! tables ) return 0;
 | 
|---|
 | 541 |                 if ( tables->scope < scope ) return 0;
 | 
|---|
 | 542 | 
 | 
|---|
 | 543 |                 StructTable::const_iterator ret = tables->structTable.find( id );
 | 
|---|
 | 544 |                 return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope );
 | 
|---|
 | 545 |         }
 | 
|---|
| [743fbda] | 546 | 
 | 
|---|
| [52c2a72] | 547 |         EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const {
 | 
|---|
 | 548 |                 if ( ! tables ) return 0;
 | 
|---|
 | 549 |                 if ( tables->scope < scope ) return 0;
 | 
|---|
 | 550 | 
 | 
|---|
 | 551 |                 EnumTable::const_iterator ret = tables->enumTable.find( id );
 | 
|---|
 | 552 |                 return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope );
 | 
|---|
 | 553 |         }
 | 
|---|
| [743fbda] | 554 | 
 | 
|---|
| [52c2a72] | 555 |         UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const {
 | 
|---|
 | 556 |                 if ( ! tables ) return 0;
 | 
|---|
 | 557 |                 if ( tables->scope < scope ) return 0;
 | 
|---|
 | 558 | 
 | 
|---|
 | 559 |                 UnionTable::const_iterator ret = tables->unionTable.find( id );
 | 
|---|
 | 560 |                 return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope );
 | 
|---|
 | 561 |         }
 | 
|---|
| [743fbda] | 562 | 
 | 
|---|
| [52c2a72] | 563 |         TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const {
 | 
|---|
 | 564 |                 if ( ! tables ) return 0;
 | 
|---|
 | 565 |                 if ( tables->scope < scope ) return 0;
 | 
|---|
 | 566 | 
 | 
|---|
 | 567 |                 TraitTable::const_iterator ret = tables->traitTable.find( id );
 | 
|---|
 | 568 |                 return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope );
 | 
|---|
| [a08ba92] | 569 |         }
 | 
|---|
| [17cd4eb] | 570 | 
 | 
|---|
| [bed4c63e] | 571 |         bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) {
 | 
|---|
 | 572 |                 // if we're giving the same name mangling to things of different types then there is something wrong
 | 
|---|
 | 573 |                 assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing ) )
 | 
|---|
 | 574 |                         || (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing ) ) );
 | 
|---|
 | 575 | 
 | 
|---|
 | 576 |                 if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) {
 | 
|---|
 | 577 |                         // new definition shadows the autogenerated one, even at the same scope
 | 
|---|
 | 578 |                         return false;
 | 
|---|
 | 579 |                 } else if ( added->get_linkage() != LinkageSpec::C || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) {
 | 
|---|
 | 580 |                         // typesCompatible doesn't really do the right thing here. When checking compatibility of function types,
 | 
|---|
 | 581 |                         // we should ignore outermost pointer qualifiers, except _Atomic?
 | 
|---|
 | 582 |                         FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added );
 | 
|---|
 | 583 |                         FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing );
 | 
|---|
 | 584 |                         if ( newentry && oldentry ) {
 | 
|---|
 | 585 |                                 if ( newentry->get_statements() && oldentry->get_statements() ) {
 | 
|---|
 | 586 |                                         throw SemanticError( "duplicate function definition for ", added );
 | 
|---|
 | 587 |                                 } // if
 | 
|---|
 | 588 |                         } else {
 | 
|---|
 | 589 |                                 // two objects with the same mangled name defined in the same scope.
 | 
|---|
 | 590 |                                 // both objects must be marked extern or both must be intrinsic for this to be okay
 | 
|---|
 | 591 |                                 // xxx - perhaps it's actually if either is intrinsic then this is okay?
 | 
|---|
 | 592 |                                 //       might also need to be same storage class?
 | 
|---|
 | 593 |                                 ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added );
 | 
|---|
 | 594 |                                 ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing );
 | 
|---|
 | 595 |                                 if ( newobj->get_storageClass() != DeclarationNode::Extern && oldobj->get_storageClass() != DeclarationNode::Extern ) {
 | 
|---|
 | 596 |                                         throw SemanticError( "duplicate object definition for ", added );
 | 
|---|
 | 597 |                                 } // if
 | 
|---|
 | 598 |                         } // if
 | 
|---|
 | 599 |                 } else {
 | 
|---|
 | 600 |                         throw SemanticError( "duplicate definition for ", added );
 | 
|---|
 | 601 |                 } // if
 | 
|---|
 | 602 | 
 | 
|---|
 | 603 |                 return true;
 | 
|---|
 | 604 |         }
 | 
|---|
| [743fbda] | 605 | 
 | 
|---|
| [e8032b0] | 606 |         void Indexer::addId( DeclarationWithType *decl ) {
 | 
|---|
 | 607 |                 makeWritable();
 | 
|---|
| [bed4c63e] | 608 | 
 | 
|---|
 | 609 |                 const std::string &name = decl->get_name();
 | 
|---|
 | 610 |                 std::string mangleName;
 | 
|---|
| [6a57da5] | 611 |                 if ( LinkageSpec::isOverridable( decl->get_linkage() ) ) {
 | 
|---|
| [bed4c63e] | 612 |                         // mangle the name without including the appropriate suffix, so overridable routines are placed into the
 | 
|---|
 | 613 |                         // same "bucket" as their user defined versions.
 | 
|---|
 | 614 |                         mangleName = Mangler::mangle( decl, false );
 | 
|---|
 | 615 |                 } else {
 | 
|---|
 | 616 |                         mangleName = Mangler::mangle( decl );
 | 
|---|
 | 617 |                 } // if
 | 
|---|
 | 618 | 
 | 
|---|
 | 619 |                 DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope );
 | 
|---|
 | 620 |                 if ( ! existing || ! addedIdConflicts( existing, decl ) ) {
 | 
|---|
| [09d1ad0] | 621 |                         // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage
 | 
|---|
 | 622 |                         if ( decl->get_linkage() == LinkageSpec::C && hasIncompatibleCDecl( name, mangleName, scope ) ) {
 | 
|---|
| [bed4c63e] | 623 |                                 throw SemanticError( "invalid overload of C function ", decl );
 | 
|---|
| [743fbda] | 624 |                         } // NOTE this is broken in Richard's original code in such a way that it never triggers (it
 | 
|---|
 | 625 |                           // doesn't check decls that have the same manglename, and all C-linkage decls are defined to
 | 
|---|
| [5447e09] | 626 |                           // have their name as their manglename, hence the error can never trigger).
 | 
|---|
| [743fbda] | 627 |                           // The code here is closer to correct, but name mangling would have to be completely
 | 
|---|
| [5447e09] | 628 |                           // isomorphic to C type-compatibility, which it may not be.
 | 
|---|
| [743fbda] | 629 | 
 | 
|---|
| [bed4c63e] | 630 |                         tables->idTable[ name ][ mangleName ] = decl;
 | 
|---|
 | 631 |                         ++tables->size;
 | 
|---|
 | 632 |                 }
 | 
|---|
| [e8032b0] | 633 |         }
 | 
|---|
| [52c2a72] | 634 | 
 | 
|---|
 | 635 |         bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
 | 
|---|
 | 636 |                 if ( existing->get_base() == 0 ) {
 | 
|---|
 | 637 |                         return false;
 | 
|---|
 | 638 |                 } else if ( added->get_base() == 0 ) {
 | 
|---|
 | 639 |                         return true;
 | 
|---|
 | 640 |                 } else {
 | 
|---|
 | 641 |                         throw SemanticError( "redeclaration of ", added );
 | 
|---|
 | 642 |                 }
 | 
|---|
 | 643 |         }
 | 
|---|
| [743fbda] | 644 | 
 | 
|---|
| [e8032b0] | 645 |         void Indexer::addType( NamedTypeDecl *decl ) {
 | 
|---|
 | 646 |                 makeWritable();
 | 
|---|
| [52c2a72] | 647 | 
 | 
|---|
 | 648 |                 const std::string &id = decl->get_name();
 | 
|---|
 | 649 |                 TypeTable::iterator existing = tables->typeTable.find( id );
 | 
|---|
 | 650 |                 if ( existing == tables->typeTable.end() ) {
 | 
|---|
 | 651 |                         NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope );
 | 
|---|
 | 652 |                         if ( ! parent || ! addedTypeConflicts( parent, decl ) ) {
 | 
|---|
 | 653 |                                 tables->typeTable.insert( existing, std::make_pair( id, decl ) );
 | 
|---|
 | 654 |                                 ++tables->size;
 | 
|---|
 | 655 |                         }
 | 
|---|
 | 656 |                 } else {
 | 
|---|
 | 657 |                         if ( ! addedTypeConflicts( existing->second, decl ) ) {
 | 
|---|
 | 658 |                                 existing->second = decl;
 | 
|---|
 | 659 |                         }
 | 
|---|
 | 660 |                 }
 | 
|---|
 | 661 |         }
 | 
|---|
 | 662 | 
 | 
|---|
 | 663 |         bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) {
 | 
|---|
 | 664 |                 if ( existing->get_members().empty() ) {
 | 
|---|
 | 665 |                         return false;
 | 
|---|
 | 666 |                 } else if ( ! added->get_members().empty() ) {
 | 
|---|
 | 667 |                         throw SemanticError( "redeclaration of ", added );
 | 
|---|
 | 668 |                 } // if
 | 
|---|
 | 669 |                 return true;
 | 
|---|
| [e8032b0] | 670 |         }
 | 
|---|
 | 671 | 
 | 
|---|
 | 672 |         void Indexer::addStruct( const std::string &id ) {
 | 
|---|
| [52c2a72] | 673 |                 addStruct( new StructDecl( id ) );
 | 
|---|
| [e8032b0] | 674 |         }
 | 
|---|
| [743fbda] | 675 | 
 | 
|---|
| [e8032b0] | 676 |         void Indexer::addStruct( StructDecl *decl ) {
 | 
|---|
 | 677 |                 makeWritable();
 | 
|---|
| [52c2a72] | 678 | 
 | 
|---|
 | 679 |                 const std::string &id = decl->get_name();
 | 
|---|
 | 680 |                 StructTable::iterator existing = tables->structTable.find( id );
 | 
|---|
 | 681 |                 if ( existing == tables->structTable.end() ) {
 | 
|---|
 | 682 |                         StructDecl *parent = tables->base.lookupStructAtScope( id, scope );
 | 
|---|
 | 683 |                         if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
 | 
|---|
 | 684 |                                 tables->structTable.insert( existing, std::make_pair( id, decl ) );
 | 
|---|
 | 685 |                                 ++tables->size;
 | 
|---|
 | 686 |                         }
 | 
|---|
 | 687 |                 } else {
 | 
|---|
 | 688 |                         if ( ! addedDeclConflicts( existing->second, decl ) ) {
 | 
|---|
 | 689 |                                 existing->second = decl;
 | 
|---|
 | 690 |                         }
 | 
|---|
 | 691 |                 }
 | 
|---|
| [e8032b0] | 692 |         }
 | 
|---|
| [743fbda] | 693 | 
 | 
|---|
| [e8032b0] | 694 |         void Indexer::addEnum( EnumDecl *decl ) {
 | 
|---|
 | 695 |                 makeWritable();
 | 
|---|
| [52c2a72] | 696 | 
 | 
|---|
 | 697 |                 const std::string &id = decl->get_name();
 | 
|---|
 | 698 |                 EnumTable::iterator existing = tables->enumTable.find( id );
 | 
|---|
 | 699 |                 if ( existing == tables->enumTable.end() ) {
 | 
|---|
 | 700 |                         EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope );
 | 
|---|
 | 701 |                         if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
 | 
|---|
 | 702 |                                 tables->enumTable.insert( existing, std::make_pair( id, decl ) );
 | 
|---|
 | 703 |                                 ++tables->size;
 | 
|---|
 | 704 |                         }
 | 
|---|
 | 705 |                 } else {
 | 
|---|
 | 706 |                         if ( ! addedDeclConflicts( existing->second, decl ) ) {
 | 
|---|
 | 707 |                                 existing->second = decl;
 | 
|---|
 | 708 |                         }
 | 
|---|
 | 709 |                 }
 | 
|---|
| [e8032b0] | 710 |         }
 | 
|---|
 | 711 | 
 | 
|---|
 | 712 |         void Indexer::addUnion( const std::string &id ) {
 | 
|---|
| [52c2a72] | 713 |                 addUnion( new UnionDecl( id ) );
 | 
|---|
| [e8032b0] | 714 |         }
 | 
|---|
| [743fbda] | 715 | 
 | 
|---|
| [e8032b0] | 716 |         void Indexer::addUnion( UnionDecl *decl ) {
 | 
|---|
 | 717 |                 makeWritable();
 | 
|---|
| [52c2a72] | 718 | 
 | 
|---|
 | 719 |                 const std::string &id = decl->get_name();
 | 
|---|
 | 720 |                 UnionTable::iterator existing = tables->unionTable.find( id );
 | 
|---|
 | 721 |                 if ( existing == tables->unionTable.end() ) {
 | 
|---|
 | 722 |                         UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope );
 | 
|---|
 | 723 |                         if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
 | 
|---|
 | 724 |                                 tables->unionTable.insert( existing, std::make_pair( id, decl ) );
 | 
|---|
 | 725 |                                 ++tables->size;
 | 
|---|
 | 726 |                         }
 | 
|---|
 | 727 |                 } else {
 | 
|---|
 | 728 |                         if ( ! addedDeclConflicts( existing->second, decl ) ) {
 | 
|---|
 | 729 |                                 existing->second = decl;
 | 
|---|
 | 730 |                         }
 | 
|---|
 | 731 |                 }
 | 
|---|
| [e8032b0] | 732 |         }
 | 
|---|
| [743fbda] | 733 | 
 | 
|---|
| [e8032b0] | 734 |         void Indexer::addTrait( TraitDecl *decl ) {
 | 
|---|
 | 735 |                 makeWritable();
 | 
|---|
| [52c2a72] | 736 | 
 | 
|---|
 | 737 |                 const std::string &id = decl->get_name();
 | 
|---|
 | 738 |                 TraitTable::iterator existing = tables->traitTable.find( id );
 | 
|---|
 | 739 |                 if ( existing == tables->traitTable.end() ) {
 | 
|---|
 | 740 |                         TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope );
 | 
|---|
 | 741 |                         if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
 | 
|---|
 | 742 |                                 tables->traitTable.insert( existing, std::make_pair( id, decl ) );
 | 
|---|
 | 743 |                                 ++tables->size;
 | 
|---|
 | 744 |                         }
 | 
|---|
 | 745 |                 } else {
 | 
|---|
 | 746 |                         if ( ! addedDeclConflicts( existing->second, decl ) ) {
 | 
|---|
 | 747 |                                 existing->second = decl;
 | 
|---|
 | 748 |                         }
 | 
|---|
 | 749 |                 }
 | 
|---|
| [a08ba92] | 750 |         }
 | 
|---|
| [17cd4eb] | 751 | 
 | 
|---|
| [a08ba92] | 752 |         void Indexer::enterScope() {
 | 
|---|
| [52c2a72] | 753 |                 ++scope;
 | 
|---|
| [743fbda] | 754 | 
 | 
|---|
| [0dd3a2f] | 755 |                 if ( doDebug ) {
 | 
|---|
| [52c2a72] | 756 |                         std::cout << "--- Entering scope " << scope << std::endl;
 | 
|---|
| [0dd3a2f] | 757 |                 }
 | 
|---|
| [a08ba92] | 758 |         }
 | 
|---|
| [17cd4eb] | 759 | 
 | 
|---|
| [a08ba92] | 760 |         void Indexer::leaveScope() {
 | 
|---|
| [0dd3a2f] | 761 |                 using std::cout;
 | 
|---|
| [52c2a72] | 762 | 
 | 
|---|
 | 763 |                 assert( scope > 0 && "cannot leave initial scope" );
 | 
|---|
 | 764 |                 --scope;
 | 
|---|
 | 765 | 
 | 
|---|
 | 766 |                 while ( tables && tables->scope > scope ) {
 | 
|---|
 | 767 |                         if ( doDebug ) {
 | 
|---|
 | 768 |                                 cout << "--- Leaving scope " << tables->scope << " containing" << std::endl;
 | 
|---|
| [bed4c63e] | 769 |                                 dump( tables->idTable, cout );
 | 
|---|
| [52c2a72] | 770 |                                 dump( tables->typeTable, cout );
 | 
|---|
 | 771 |                                 dump( tables->structTable, cout );
 | 
|---|
 | 772 |                                 dump( tables->enumTable, cout );
 | 
|---|
 | 773 |                                 dump( tables->unionTable, cout );
 | 
|---|
 | 774 |                                 dump( tables->traitTable, cout );
 | 
|---|
 | 775 |                         }
 | 
|---|
 | 776 | 
 | 
|---|
 | 777 |                         // swap tables for base table until we find one at an appropriate scope
 | 
|---|
 | 778 |                         Indexer::Impl *base = newRef( tables->base.tables );
 | 
|---|
 | 779 |                         deleteRef( tables );
 | 
|---|
 | 780 |                         tables = base;
 | 
|---|
| [0dd3a2f] | 781 |                 }
 | 
|---|
| [a08ba92] | 782 |         }
 | 
|---|
| [17cd4eb] | 783 | 
 | 
|---|
| [a08ba92] | 784 |         void Indexer::print( std::ostream &os, int indent ) const {
 | 
|---|
 | 785 |             using std::cerr;
 | 
|---|
| [e8032b0] | 786 | 
 | 
|---|
| [0cb1d61] | 787 |                 if ( tables ) {
 | 
|---|
 | 788 |                         os << "--- scope " << tables->scope << " ---" << std::endl;
 | 
|---|
 | 789 | 
 | 
|---|
 | 790 |                         os << "===idTable===" << std::endl;
 | 
|---|
 | 791 |                         dump( tables->idTable, os );
 | 
|---|
 | 792 |                         os << "===typeTable===" << std::endl;
 | 
|---|
 | 793 |                         dump( tables->typeTable, os );
 | 
|---|
 | 794 |                         os << "===structTable===" << std::endl;
 | 
|---|
 | 795 |                         dump( tables->structTable, os );
 | 
|---|
 | 796 |                         os << "===enumTable===" << std::endl;
 | 
|---|
 | 797 |                         dump( tables->enumTable, os );
 | 
|---|
 | 798 |                         os << "===unionTable===" << std::endl;
 | 
|---|
 | 799 |                         dump( tables->unionTable, os );
 | 
|---|
 | 800 |                         os << "===contextTable===" << std::endl;
 | 
|---|
 | 801 |                         dump( tables->traitTable, os );
 | 
|---|
 | 802 | 
 | 
|---|
 | 803 |                         tables->base.print( os, indent );
 | 
|---|
 | 804 |                 } else {
 | 
|---|
 | 805 |                         os << "--- end ---" << std::endl;
 | 
|---|
 | 806 |                 }
 | 
|---|
| [743fbda] | 807 | 
 | 
|---|
| [a08ba92] | 808 |         }
 | 
|---|
| [51b73452] | 809 | } // namespace SymTab
 | 
|---|
| [0dd3a2f] | 810 | 
 | 
|---|
 | 811 | // Local Variables: //
 | 
|---|
 | 812 | // tab-width: 4 //
 | 
|---|
 | 813 | // mode: c++ //
 | 
|---|
 | 814 | // compile-command: "make install" //
 | 
|---|
 | 815 | // End: //
 | 
|---|