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