[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 |
---|
[6d49ea3] | 12 | // Last Modified On : Thu Aug 17 16:08:40 2017 |
---|
| 13 | // Update Count : 20 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[e8032b0] | 16 | #include "Indexer.h" |
---|
| 17 | |
---|
[e3e16bc] | 18 | #include <cassert> // for assert, strict_dynamic_cast |
---|
[30f9072] | 19 | #include <iostream> // for operator<<, basic_ostream, ostream |
---|
| 20 | #include <string> // for string, operator<<, operator!= |
---|
| 21 | #include <unordered_map> // for operator!=, unordered_map<>::const... |
---|
| 22 | #include <unordered_set> // for unordered_set |
---|
| 23 | #include <utility> // for pair, make_pair, move |
---|
| 24 | |
---|
[9236060] | 25 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign |
---|
[30f9072] | 26 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 27 | #include "Common/utility.h" // for cloneAll |
---|
| 28 | #include "InitTweak/InitTweak.h" // for isConstructor, isCopyFunction, isC... |
---|
| 29 | #include "Mangler.h" // for Mangler |
---|
| 30 | #include "Parser/LinkageSpec.h" // for isMangled, isOverridable, Spec |
---|
| 31 | #include "ResolvExpr/typeops.h" // for typesCompatible |
---|
| 32 | #include "SynTree/Constant.h" // for Constant |
---|
| 33 | #include "SynTree/Declaration.h" // for DeclarationWithType, FunctionDecl |
---|
| 34 | #include "SynTree/Expression.h" // for Expression, ImplicitCopyCtorExpr |
---|
| 35 | #include "SynTree/Initializer.h" // for Initializer |
---|
| 36 | #include "SynTree/Statement.h" // for CompoundStmt, Statement, ForStmt (... |
---|
| 37 | #include "SynTree/Type.h" // for Type, StructInstType, UnionInstType |
---|
[1ba88a0] | 38 | |
---|
[522363e] | 39 | #define debugPrint(x) if ( doDebug ) { std::cerr << x; } |
---|
[51b7345] | 40 | |
---|
| 41 | namespace SymTab { |
---|
[bed4c63e] | 42 | typedef std::unordered_map< std::string, DeclarationWithType* > MangleTable; |
---|
| 43 | typedef std::unordered_map< std::string, MangleTable > IdTable; |
---|
[52c2a72] | 44 | typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable; |
---|
| 45 | typedef std::unordered_map< std::string, StructDecl* > StructTable; |
---|
| 46 | typedef std::unordered_map< std::string, EnumDecl* > EnumTable; |
---|
| 47 | typedef std::unordered_map< std::string, UnionDecl* > UnionTable; |
---|
| 48 | typedef std::unordered_map< std::string, TraitDecl* > TraitTable; |
---|
| 49 | |
---|
[bed4c63e] | 50 | void dump( const IdTable &table, std::ostream &os ) { |
---|
| 51 | for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) { |
---|
| 52 | for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) { |
---|
| 53 | os << mangle->second << std::endl; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | } |
---|
[743fbda] | 57 | |
---|
[52c2a72] | 58 | template< typename Decl > |
---|
| 59 | void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) { |
---|
| 60 | for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) { |
---|
| 61 | os << it->second << std::endl; |
---|
| 62 | } // for |
---|
| 63 | } |
---|
[743fbda] | 64 | |
---|
[e8032b0] | 65 | struct Indexer::Impl { |
---|
[52c2a72] | 66 | Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(), |
---|
[e8032b0] | 67 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} |
---|
[52c2a72] | 68 | Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ), |
---|
[e8032b0] | 69 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} |
---|
| 70 | unsigned long refCount; ///< Number of references to these tables |
---|
[52c2a72] | 71 | unsigned long scope; ///< Scope these tables are associated with |
---|
[e8032b0] | 72 | unsigned long size; ///< Number of elements stored in this table |
---|
| 73 | const Indexer base; ///< Base indexer this extends |
---|
[743fbda] | 74 | |
---|
[e8032b0] | 75 | IdTable idTable; ///< Identifier namespace |
---|
| 76 | TypeTable typeTable; ///< Type namespace |
---|
| 77 | StructTable structTable; ///< Struct namespace |
---|
| 78 | EnumTable enumTable; ///< Enum namespace |
---|
| 79 | UnionTable unionTable; ///< Union namespace |
---|
| 80 | TraitTable traitTable; ///< Trait namespace |
---|
| 81 | }; |
---|
| 82 | |
---|
| 83 | Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) { |
---|
| 84 | if ( ! toClone ) return 0; |
---|
| 85 | |
---|
| 86 | // shorten the search chain by skipping empty links |
---|
| 87 | Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone; |
---|
| 88 | if ( ret ) { ++ret->refCount; } |
---|
| 89 | |
---|
| 90 | return ret; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void Indexer::deleteRef( Indexer::Impl *toFree ) { |
---|
| 94 | if ( ! toFree ) return; |
---|
| 95 | |
---|
| 96 | if ( --toFree->refCount == 0 ) delete toFree; |
---|
| 97 | } |
---|
| 98 | |
---|
[1ba88a0] | 99 | void Indexer::removeSpecialOverrides( const std::string &id, std::list< DeclarationWithType * > & out ) const { |
---|
[ee1635c8] | 100 | // only need to perform this step for constructors, destructors, and assignment functions |
---|
[bff227f] | 101 | if ( ! CodeGen::isCtorDtorAssign( id ) ) return; |
---|
[1ba88a0] | 102 | |
---|
| 103 | // helpful data structure |
---|
| 104 | struct ValueType { |
---|
| 105 | struct DeclBall { |
---|
| 106 | FunctionDecl * decl; |
---|
| 107 | bool isUserDefinedFunc; // properties for this particular decl |
---|
[235114f] | 108 | bool isDefaultCtor; |
---|
| 109 | bool isDtor; |
---|
[1ba88a0] | 110 | bool isCopyFunc; |
---|
| 111 | }; |
---|
| 112 | // properties for this type |
---|
[ff03f5c] | 113 | bool existsUserDefinedFunc = false; // any user-defined function found |
---|
| 114 | bool existsUserDefinedCtor = false; // any user-defined constructor found |
---|
| 115 | bool existsUserDefinedDtor = false; // any user-defined destructor found |
---|
| 116 | bool existsUserDefinedCopyFunc = false; // user-defined copy ctor found |
---|
| 117 | bool existsUserDefinedDefaultCtor = false; // user-defined default ctor found |
---|
[1ba88a0] | 118 | std::list< DeclBall > decls; |
---|
| 119 | |
---|
| 120 | // another FunctionDecl for the current type was found - determine |
---|
| 121 | // if it has special properties and update data structure accordingly |
---|
| 122 | ValueType & operator+=( FunctionDecl * function ) { |
---|
| 123 | bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->get_linkage() ); |
---|
[235114f] | 124 | bool isDefaultCtor = InitTweak::isDefaultConstructor( function ); |
---|
| 125 | bool isDtor = InitTweak::isDestructor( function ); |
---|
[ee1635c8] | 126 | bool isCopyFunc = InitTweak::isCopyFunction( function, function->get_name() ); |
---|
[235114f] | 127 | decls.push_back( DeclBall{ function, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } ); |
---|
[ff03f5c] | 128 | existsUserDefinedFunc = existsUserDefinedFunc || isUserDefinedFunc; |
---|
[bff227f] | 129 | existsUserDefinedCtor = existsUserDefinedCtor || (isUserDefinedFunc && CodeGen::isConstructor( function->get_name() ) ); |
---|
[ff03f5c] | 130 | existsUserDefinedDtor = existsUserDefinedDtor || (isUserDefinedFunc && isDtor); |
---|
| 131 | existsUserDefinedCopyFunc = existsUserDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc); |
---|
| 132 | existsUserDefinedDefaultCtor = existsUserDefinedDefaultCtor || (isUserDefinedFunc && isDefaultCtor); |
---|
[1ba88a0] | 133 | return *this; |
---|
| 134 | } |
---|
| 135 | }; // ValueType |
---|
| 136 | |
---|
| 137 | std::list< DeclarationWithType * > copy; |
---|
| 138 | copy.splice( copy.end(), out ); |
---|
| 139 | |
---|
| 140 | // organize discovered declarations by type |
---|
| 141 | std::unordered_map< std::string, ValueType > funcMap; |
---|
| 142 | for ( DeclarationWithType * decl : copy ) { |
---|
| 143 | if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
[8c49c0e] | 144 | std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters(); |
---|
[1ba88a0] | 145 | assert( ! params.empty() ); |
---|
[24670d2] | 146 | // use base type of pointer, so that qualifiers on the pointer type aren't considered. |
---|
[ce8c12f] | 147 | Type * base = InitTweak::getPointerBase( params.front()->get_type() ); |
---|
| 148 | assert( base ); |
---|
[24670d2] | 149 | funcMap[ Mangler::mangle( base ) ] += function; |
---|
[1ba88a0] | 150 | } else { |
---|
| 151 | out.push_back( decl ); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
[ff03f5c] | 155 | // if a type contains user defined ctor/dtor/assign, then special rules trigger, which determine |
---|
| 156 | // the set of ctor/dtor/assign that are seen by the requester. In particular, if the user defines |
---|
[1ba88a0] | 157 | // a default ctor, then the generated default ctor should never be seen, likewise for copy ctor |
---|
| 158 | // and dtor. If the user defines any ctor/dtor, then no generated field ctors should be seen. |
---|
[ff03f5c] | 159 | // If the user defines any ctor then the generated default ctor should not be seen (intrinsic default |
---|
| 160 | // ctor must be overridden exactly). |
---|
[1ba88a0] | 161 | for ( std::pair< const std::string, ValueType > & pair : funcMap ) { |
---|
| 162 | ValueType & val = pair.second; |
---|
| 163 | for ( ValueType::DeclBall ball : val.decls ) { |
---|
[ff03f5c] | 164 | bool noUserDefinedFunc = ! val.existsUserDefinedFunc; |
---|
| 165 | bool isUserDefinedFunc = ball.isUserDefinedFunc; |
---|
| 166 | bool isAcceptableDefaultCtor = (! val.existsUserDefinedCtor || (! val.existsUserDefinedDefaultCtor && ball.decl->get_linkage() == LinkageSpec::Intrinsic)) && ball.isDefaultCtor; // allow default constructors only when no user-defined constructors exist, except in the case of intrinsics, which require exact overrides |
---|
| 167 | bool isAcceptableCopyFunc = ! val.existsUserDefinedCopyFunc && ball.isCopyFunc; // handles copy ctor and assignment operator |
---|
| 168 | bool isAcceptableDtor = ! val.existsUserDefinedDtor && ball.isDtor; |
---|
| 169 | if ( noUserDefinedFunc || isUserDefinedFunc || isAcceptableDefaultCtor || isAcceptableCopyFunc || isAcceptableDtor ) { |
---|
[1ba88a0] | 170 | // decl conforms to the rules described above, so it should be seen by the requester |
---|
| 171 | out.push_back( ball.decl ); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
[e8032b0] | 177 | void Indexer::makeWritable() { |
---|
| 178 | if ( ! tables ) { |
---|
[52c2a72] | 179 | // create indexer if not yet set |
---|
| 180 | tables = new Indexer::Impl( scope ); |
---|
| 181 | } else if ( tables->refCount > 1 || tables->scope != scope ) { |
---|
| 182 | // make this indexer the base of a fresh indexer at the current scope |
---|
| 183 | tables = new Indexer::Impl( scope, std::move( *this ) ); |
---|
[e8032b0] | 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
[8b11840] | 187 | Indexer::Indexer() : tables( 0 ), scope( 0 ) {} |
---|
[e8032b0] | 188 | |
---|
[8b11840] | 189 | Indexer::Indexer( const Indexer &that ) : doDebug( that.doDebug ), tables( newRef( that.tables ) ), scope( that.scope ) {} |
---|
[e8032b0] | 190 | |
---|
[8b11840] | 191 | Indexer::Indexer( Indexer &&that ) : doDebug( that.doDebug ), tables( that.tables ), scope( that.scope ) { |
---|
[e8032b0] | 192 | that.tables = 0; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | Indexer::~Indexer() { |
---|
| 196 | deleteRef( tables ); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | Indexer& Indexer::operator= ( const Indexer &that ) { |
---|
| 200 | deleteRef( tables ); |
---|
| 201 | |
---|
| 202 | tables = newRef( that.tables ); |
---|
[52c2a72] | 203 | scope = that.scope; |
---|
[e8032b0] | 204 | doDebug = that.doDebug; |
---|
| 205 | |
---|
| 206 | return *this; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | Indexer& Indexer::operator= ( Indexer &&that ) { |
---|
| 210 | deleteRef( tables ); |
---|
| 211 | |
---|
| 212 | tables = that.tables; |
---|
[52c2a72] | 213 | scope = that.scope; |
---|
[e8032b0] | 214 | doDebug = that.doDebug; |
---|
[17cd4eb] | 215 | |
---|
[e8032b0] | 216 | that.tables = 0; |
---|
| 217 | |
---|
| 218 | return *this; |
---|
| 219 | } |
---|
[17cd4eb] | 220 | |
---|
[bed4c63e] | 221 | void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const { |
---|
[5447e09] | 222 | std::unordered_set< std::string > foundMangleNames; |
---|
[743fbda] | 223 | |
---|
[5447e09] | 224 | Indexer::Impl *searchTables = tables; |
---|
| 225 | while ( searchTables ) { |
---|
| 226 | |
---|
| 227 | IdTable::const_iterator decls = searchTables->idTable.find( id ); |
---|
| 228 | if ( decls != searchTables->idTable.end() ) { |
---|
| 229 | const MangleTable &mangleTable = decls->second; |
---|
| 230 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 231 | // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found |
---|
| 232 | if ( foundMangleNames.insert( decl->first ).second == false ) continue; |
---|
[743fbda] | 233 | |
---|
[5447e09] | 234 | out.push_back( decl->second ); |
---|
| 235 | } |
---|
[bed4c63e] | 236 | } |
---|
[743fbda] | 237 | |
---|
[5447e09] | 238 | // get declarations from base indexers |
---|
| 239 | searchTables = searchTables->base.tables; |
---|
[bed4c63e] | 240 | } |
---|
[1ba88a0] | 241 | |
---|
| 242 | // some special functions, e.g. constructors and destructors |
---|
| 243 | // remove autogenerated functions when they are defined so that |
---|
| 244 | // they can never be matched |
---|
| 245 | removeSpecialOverrides( id, out ); |
---|
[a08ba92] | 246 | } |
---|
[bdd516a] | 247 | |
---|
[a08ba92] | 248 | NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { |
---|
[e8032b0] | 249 | if ( ! tables ) return 0; |
---|
| 250 | |
---|
[52c2a72] | 251 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 252 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id ); |
---|
[a08ba92] | 253 | } |
---|
[17cd4eb] | 254 | |
---|
[a08ba92] | 255 | StructDecl *Indexer::lookupStruct( const std::string &id ) const { |
---|
[e8032b0] | 256 | if ( ! tables ) return 0; |
---|
| 257 | |
---|
[52c2a72] | 258 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 259 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id ); |
---|
[a08ba92] | 260 | } |
---|
[17cd4eb] | 261 | |
---|
[a08ba92] | 262 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { |
---|
[e8032b0] | 263 | if ( ! tables ) return 0; |
---|
| 264 | |
---|
[52c2a72] | 265 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 266 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id ); |
---|
[a08ba92] | 267 | } |
---|
[17cd4eb] | 268 | |
---|
[a08ba92] | 269 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { |
---|
[e8032b0] | 270 | if ( ! tables ) return 0; |
---|
| 271 | |
---|
[52c2a72] | 272 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 273 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id ); |
---|
[e8032b0] | 274 | } |
---|
| 275 | |
---|
| 276 | TraitDecl *Indexer::lookupTrait( const std::string &id ) const { |
---|
| 277 | if ( ! tables ) return 0; |
---|
| 278 | |
---|
[52c2a72] | 279 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 280 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id ); |
---|
| 281 | } |
---|
| 282 | |
---|
[bed4c63e] | 283 | DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 284 | if ( ! tables ) return 0; |
---|
| 285 | if ( tables->scope < scope ) return 0; |
---|
| 286 | |
---|
| 287 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 288 | if ( decls != tables->idTable.end() ) { |
---|
| 289 | const MangleTable &mangleTable = decls->second; |
---|
| 290 | MangleTable::const_iterator decl = mangleTable.find( mangleName ); |
---|
| 291 | if ( decl != mangleTable.end() ) return decl->second; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | return tables->base.lookupIdAtScope( id, mangleName, scope ); |
---|
| 295 | } |
---|
| 296 | |
---|
[09d1ad0] | 297 | bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
[bed4c63e] | 298 | if ( ! tables ) return false; |
---|
[09d1ad0] | 299 | if ( tables->scope < scope ) return false; |
---|
[bed4c63e] | 300 | |
---|
| 301 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 302 | if ( decls != tables->idTable.end() ) { |
---|
| 303 | const MangleTable &mangleTable = decls->second; |
---|
| 304 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
[4e06c1e] | 305 | // check for C decls with the same name, skipping those with a compatible type (by mangleName) |
---|
[307a732] | 306 | if ( ! LinkageSpec::isMangled( decl->second->get_linkage() ) && decl->first != mangleName ) return true; |
---|
[bed4c63e] | 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
[09d1ad0] | 310 | return tables->base.hasIncompatibleCDecl( id, mangleName, scope ); |
---|
[bed4c63e] | 311 | } |
---|
[743fbda] | 312 | |
---|
[8884112] | 313 | bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 314 | if ( ! tables ) return false; |
---|
| 315 | if ( tables->scope < scope ) return false; |
---|
| 316 | |
---|
| 317 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 318 | if ( decls != tables->idTable.end() ) { |
---|
| 319 | const MangleTable &mangleTable = decls->second; |
---|
| 320 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 321 | // check for C decls with the same name, skipping |
---|
| 322 | // those with an incompatible type (by mangleName) |
---|
[307a732] | 323 | if ( ! LinkageSpec::isMangled( decl->second->get_linkage() ) && decl->first == mangleName ) return true; |
---|
[8884112] | 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | return tables->base.hasCompatibleCDecl( id, mangleName, scope ); |
---|
| 328 | } |
---|
| 329 | |
---|
[52c2a72] | 330 | NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 331 | if ( ! tables ) return 0; |
---|
| 332 | if ( tables->scope < scope ) return 0; |
---|
| 333 | |
---|
| 334 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 335 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); |
---|
| 336 | } |
---|
[743fbda] | 337 | |
---|
[52c2a72] | 338 | StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 339 | if ( ! tables ) return 0; |
---|
| 340 | if ( tables->scope < scope ) return 0; |
---|
| 341 | |
---|
| 342 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 343 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); |
---|
| 344 | } |
---|
[743fbda] | 345 | |
---|
[52c2a72] | 346 | EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 347 | if ( ! tables ) return 0; |
---|
| 348 | if ( tables->scope < scope ) return 0; |
---|
| 349 | |
---|
| 350 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 351 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); |
---|
| 352 | } |
---|
[743fbda] | 353 | |
---|
[52c2a72] | 354 | UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 355 | if ( ! tables ) return 0; |
---|
| 356 | if ( tables->scope < scope ) return 0; |
---|
| 357 | |
---|
| 358 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 359 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); |
---|
| 360 | } |
---|
[743fbda] | 361 | |
---|
[52c2a72] | 362 | TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 363 | if ( ! tables ) return 0; |
---|
| 364 | if ( tables->scope < scope ) return 0; |
---|
| 365 | |
---|
| 366 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 367 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope ); |
---|
[a08ba92] | 368 | } |
---|
[17cd4eb] | 369 | |
---|
[bed4c63e] | 370 | bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) { |
---|
| 371 | // if we're giving the same name mangling to things of different types then there is something wrong |
---|
| 372 | assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing ) ) |
---|
| 373 | || (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing ) ) ); |
---|
| 374 | |
---|
| 375 | if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) { |
---|
| 376 | // new definition shadows the autogenerated one, even at the same scope |
---|
| 377 | return false; |
---|
[307a732] | 378 | } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) { |
---|
[bed4c63e] | 379 | // typesCompatible doesn't really do the right thing here. When checking compatibility of function types, |
---|
| 380 | // we should ignore outermost pointer qualifiers, except _Atomic? |
---|
| 381 | FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added ); |
---|
| 382 | FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing ); |
---|
| 383 | if ( newentry && oldentry ) { |
---|
| 384 | if ( newentry->get_statements() && oldentry->get_statements() ) { |
---|
| 385 | throw SemanticError( "duplicate function definition for ", added ); |
---|
| 386 | } // if |
---|
| 387 | } else { |
---|
| 388 | // two objects with the same mangled name defined in the same scope. |
---|
| 389 | // both objects must be marked extern or both must be intrinsic for this to be okay |
---|
| 390 | // xxx - perhaps it's actually if either is intrinsic then this is okay? |
---|
| 391 | // might also need to be same storage class? |
---|
| 392 | ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added ); |
---|
| 393 | ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing ); |
---|
[08d5507b] | 394 | if ( ! newobj->get_storageClasses().is_extern && ! oldobj->get_storageClasses().is_extern ) { |
---|
[bed4c63e] | 395 | throw SemanticError( "duplicate object definition for ", added ); |
---|
| 396 | } // if |
---|
| 397 | } // if |
---|
| 398 | } else { |
---|
| 399 | throw SemanticError( "duplicate definition for ", added ); |
---|
| 400 | } // if |
---|
| 401 | |
---|
| 402 | return true; |
---|
| 403 | } |
---|
[743fbda] | 404 | |
---|
[e8032b0] | 405 | void Indexer::addId( DeclarationWithType *decl ) { |
---|
[33a25f9] | 406 | debugPrint( "Adding Id " << decl->name << std::endl ); |
---|
[e8032b0] | 407 | makeWritable(); |
---|
[bed4c63e] | 408 | |
---|
[6fc5c14] | 409 | const std::string &name = decl->name; |
---|
[bed4c63e] | 410 | std::string mangleName; |
---|
[6fc5c14] | 411 | if ( LinkageSpec::isOverridable( decl->linkage ) ) { |
---|
[bed4c63e] | 412 | // mangle the name without including the appropriate suffix, so overridable routines are placed into the |
---|
| 413 | // same "bucket" as their user defined versions. |
---|
| 414 | mangleName = Mangler::mangle( decl, false ); |
---|
| 415 | } else { |
---|
| 416 | mangleName = Mangler::mangle( decl ); |
---|
| 417 | } // if |
---|
| 418 | |
---|
[8884112] | 419 | // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage |
---|
[6fc5c14] | 420 | if ( ! LinkageSpec::isMangled( decl->linkage ) ) { |
---|
[8884112] | 421 | // NOTE this is broken in Richard's original code in such a way that it never triggers (it |
---|
| 422 | // doesn't check decls that have the same manglename, and all C-linkage decls are defined to |
---|
| 423 | // have their name as their manglename, hence the error can never trigger). |
---|
| 424 | // The code here is closer to correct, but name mangling would have to be completely |
---|
| 425 | // isomorphic to C type-compatibility, which it may not be. |
---|
| 426 | if ( hasIncompatibleCDecl( name, mangleName, scope ) ) { |
---|
| 427 | throw SemanticError( "conflicting overload of C function ", decl ); |
---|
| 428 | } |
---|
| 429 | } else { |
---|
| 430 | // Check that a Cforall declaration doesn't overload any C declaration |
---|
| 431 | if ( hasCompatibleCDecl( name, mangleName, scope ) ) { |
---|
| 432 | throw SemanticError( "Cforall declaration hides C function ", decl ); |
---|
| 433 | } |
---|
[bed4c63e] | 434 | } |
---|
[8884112] | 435 | |
---|
| 436 | // Skip repeat declarations of the same identifier |
---|
| 437 | DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope ); |
---|
| 438 | if ( existing && addedIdConflicts( existing, decl ) ) return; |
---|
| 439 | |
---|
| 440 | // add to indexer |
---|
| 441 | tables->idTable[ name ][ mangleName ] = decl; |
---|
| 442 | ++tables->size; |
---|
[e8032b0] | 443 | } |
---|
[52c2a72] | 444 | |
---|
| 445 | bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { |
---|
| 446 | if ( existing->get_base() == 0 ) { |
---|
| 447 | return false; |
---|
| 448 | } else if ( added->get_base() == 0 ) { |
---|
| 449 | return true; |
---|
| 450 | } else { |
---|
| 451 | throw SemanticError( "redeclaration of ", added ); |
---|
| 452 | } |
---|
| 453 | } |
---|
[743fbda] | 454 | |
---|
[e8032b0] | 455 | void Indexer::addType( NamedTypeDecl *decl ) { |
---|
[33a25f9] | 456 | debugPrint( "Adding type " << decl->name << std::endl ); |
---|
[e8032b0] | 457 | makeWritable(); |
---|
[52c2a72] | 458 | |
---|
| 459 | const std::string &id = decl->get_name(); |
---|
| 460 | TypeTable::iterator existing = tables->typeTable.find( id ); |
---|
| 461 | if ( existing == tables->typeTable.end() ) { |
---|
| 462 | NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope ); |
---|
| 463 | if ( ! parent || ! addedTypeConflicts( parent, decl ) ) { |
---|
| 464 | tables->typeTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 465 | ++tables->size; |
---|
| 466 | } |
---|
| 467 | } else { |
---|
| 468 | if ( ! addedTypeConflicts( existing->second, decl ) ) { |
---|
| 469 | existing->second = decl; |
---|
| 470 | } |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) { |
---|
| 475 | if ( existing->get_members().empty() ) { |
---|
| 476 | return false; |
---|
| 477 | } else if ( ! added->get_members().empty() ) { |
---|
| 478 | throw SemanticError( "redeclaration of ", added ); |
---|
| 479 | } // if |
---|
| 480 | return true; |
---|
[e8032b0] | 481 | } |
---|
| 482 | |
---|
| 483 | void Indexer::addStruct( const std::string &id ) { |
---|
[33a25f9] | 484 | debugPrint( "Adding fwd decl for struct " << id << std::endl ); |
---|
[52c2a72] | 485 | addStruct( new StructDecl( id ) ); |
---|
[e8032b0] | 486 | } |
---|
[743fbda] | 487 | |
---|
[e8032b0] | 488 | void Indexer::addStruct( StructDecl *decl ) { |
---|
[33a25f9] | 489 | debugPrint( "Adding struct " << decl->name << std::endl ); |
---|
[e8032b0] | 490 | makeWritable(); |
---|
[52c2a72] | 491 | |
---|
| 492 | const std::string &id = decl->get_name(); |
---|
| 493 | StructTable::iterator existing = tables->structTable.find( id ); |
---|
| 494 | if ( existing == tables->structTable.end() ) { |
---|
| 495 | StructDecl *parent = tables->base.lookupStructAtScope( id, scope ); |
---|
| 496 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 497 | tables->structTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 498 | ++tables->size; |
---|
| 499 | } |
---|
| 500 | } else { |
---|
| 501 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 502 | existing->second = decl; |
---|
| 503 | } |
---|
| 504 | } |
---|
[e8032b0] | 505 | } |
---|
[743fbda] | 506 | |
---|
[e8032b0] | 507 | void Indexer::addEnum( EnumDecl *decl ) { |
---|
[33a25f9] | 508 | debugPrint( "Adding enum " << decl->name << std::endl ); |
---|
[e8032b0] | 509 | makeWritable(); |
---|
[52c2a72] | 510 | |
---|
| 511 | const std::string &id = decl->get_name(); |
---|
| 512 | EnumTable::iterator existing = tables->enumTable.find( id ); |
---|
| 513 | if ( existing == tables->enumTable.end() ) { |
---|
| 514 | EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope ); |
---|
| 515 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 516 | tables->enumTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 517 | ++tables->size; |
---|
| 518 | } |
---|
| 519 | } else { |
---|
| 520 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 521 | existing->second = decl; |
---|
| 522 | } |
---|
| 523 | } |
---|
[e8032b0] | 524 | } |
---|
| 525 | |
---|
| 526 | void Indexer::addUnion( const std::string &id ) { |
---|
[33a25f9] | 527 | debugPrint( "Adding fwd decl for union " << id << std::endl ); |
---|
[52c2a72] | 528 | addUnion( new UnionDecl( id ) ); |
---|
[e8032b0] | 529 | } |
---|
[743fbda] | 530 | |
---|
[e8032b0] | 531 | void Indexer::addUnion( UnionDecl *decl ) { |
---|
[33a25f9] | 532 | debugPrint( "Adding union " << decl->name << std::endl ); |
---|
[e8032b0] | 533 | makeWritable(); |
---|
[52c2a72] | 534 | |
---|
| 535 | const std::string &id = decl->get_name(); |
---|
| 536 | UnionTable::iterator existing = tables->unionTable.find( id ); |
---|
| 537 | if ( existing == tables->unionTable.end() ) { |
---|
| 538 | UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope ); |
---|
| 539 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 540 | tables->unionTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 541 | ++tables->size; |
---|
| 542 | } |
---|
| 543 | } else { |
---|
| 544 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 545 | existing->second = decl; |
---|
| 546 | } |
---|
| 547 | } |
---|
[e8032b0] | 548 | } |
---|
[743fbda] | 549 | |
---|
[e8032b0] | 550 | void Indexer::addTrait( TraitDecl *decl ) { |
---|
[33a25f9] | 551 | debugPrint( "Adding trait " << decl->name << std::endl ); |
---|
[e8032b0] | 552 | makeWritable(); |
---|
[52c2a72] | 553 | |
---|
| 554 | const std::string &id = decl->get_name(); |
---|
| 555 | TraitTable::iterator existing = tables->traitTable.find( id ); |
---|
| 556 | if ( existing == tables->traitTable.end() ) { |
---|
| 557 | TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope ); |
---|
| 558 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 559 | tables->traitTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 560 | ++tables->size; |
---|
| 561 | } |
---|
| 562 | } else { |
---|
| 563 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 564 | existing->second = decl; |
---|
| 565 | } |
---|
| 566 | } |
---|
[a08ba92] | 567 | } |
---|
[17cd4eb] | 568 | |
---|
[a08ba92] | 569 | void Indexer::enterScope() { |
---|
[52c2a72] | 570 | ++scope; |
---|
[743fbda] | 571 | |
---|
[0dd3a2f] | 572 | if ( doDebug ) { |
---|
[6137fbb] | 573 | std::cerr << "--- Entering scope " << scope << std::endl; |
---|
[0dd3a2f] | 574 | } |
---|
[a08ba92] | 575 | } |
---|
[17cd4eb] | 576 | |
---|
[a08ba92] | 577 | void Indexer::leaveScope() { |
---|
[6137fbb] | 578 | using std::cerr; |
---|
[52c2a72] | 579 | |
---|
| 580 | assert( scope > 0 && "cannot leave initial scope" ); |
---|
[6137fbb] | 581 | if ( doDebug ) { |
---|
| 582 | cerr << "--- Leaving scope " << scope << " containing" << std::endl; |
---|
| 583 | } |
---|
[52c2a72] | 584 | --scope; |
---|
| 585 | |
---|
| 586 | while ( tables && tables->scope > scope ) { |
---|
| 587 | if ( doDebug ) { |
---|
[6137fbb] | 588 | dump( tables->idTable, cerr ); |
---|
| 589 | dump( tables->typeTable, cerr ); |
---|
| 590 | dump( tables->structTable, cerr ); |
---|
| 591 | dump( tables->enumTable, cerr ); |
---|
| 592 | dump( tables->unionTable, cerr ); |
---|
| 593 | dump( tables->traitTable, cerr ); |
---|
[52c2a72] | 594 | } |
---|
| 595 | |
---|
| 596 | // swap tables for base table until we find one at an appropriate scope |
---|
| 597 | Indexer::Impl *base = newRef( tables->base.tables ); |
---|
| 598 | deleteRef( tables ); |
---|
| 599 | tables = base; |
---|
[0dd3a2f] | 600 | } |
---|
[a08ba92] | 601 | } |
---|
[17cd4eb] | 602 | |
---|
[a08ba92] | 603 | void Indexer::print( std::ostream &os, int indent ) const { |
---|
| 604 | using std::cerr; |
---|
[e8032b0] | 605 | |
---|
[0cb1d61] | 606 | if ( tables ) { |
---|
| 607 | os << "--- scope " << tables->scope << " ---" << std::endl; |
---|
| 608 | |
---|
| 609 | os << "===idTable===" << std::endl; |
---|
| 610 | dump( tables->idTable, os ); |
---|
| 611 | os << "===typeTable===" << std::endl; |
---|
| 612 | dump( tables->typeTable, os ); |
---|
| 613 | os << "===structTable===" << std::endl; |
---|
| 614 | dump( tables->structTable, os ); |
---|
| 615 | os << "===enumTable===" << std::endl; |
---|
| 616 | dump( tables->enumTable, os ); |
---|
| 617 | os << "===unionTable===" << std::endl; |
---|
| 618 | dump( tables->unionTable, os ); |
---|
| 619 | os << "===contextTable===" << std::endl; |
---|
| 620 | dump( tables->traitTable, os ); |
---|
| 621 | |
---|
| 622 | tables->base.print( os, indent ); |
---|
| 623 | } else { |
---|
| 624 | os << "--- end ---" << std::endl; |
---|
| 625 | } |
---|
[743fbda] | 626 | |
---|
[a08ba92] | 627 | } |
---|
[51b7345] | 628 | } // namespace SymTab |
---|
[0dd3a2f] | 629 | |
---|
| 630 | // Local Variables: // |
---|
| 631 | // tab-width: 4 // |
---|
| 632 | // mode: c++ // |
---|
| 633 | // compile-command: "make install" // |
---|
| 634 | // End: // |
---|