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