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