| [d76c588] | 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 | // | 
|---|
|  | 7 | // SymbolTable.cpp -- | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Aaron B. Moss | 
|---|
|  | 10 | // Created On       : Wed May 29 11:00:00 2019 | 
|---|
|  | 11 | // Last Modified By : Aaron B. Moss | 
|---|
|  | 12 | // Last Modified On : Wed May 29 11:00:00 2019 | 
|---|
|  | 13 | // Update Count     : 1 | 
|---|
|  | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "SymbolTable.hpp" | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include <cassert> | 
|---|
|  | 19 |  | 
|---|
| [bccd70a] | 20 | #include "Copy.hpp" | 
|---|
| [d76c588] | 21 | #include "Decl.hpp" | 
|---|
|  | 22 | #include "Expr.hpp" | 
|---|
| [e01eb4a] | 23 | #include "Inspect.hpp" | 
|---|
| [d76c588] | 24 | #include "Type.hpp" | 
|---|
| [c92bdcc] | 25 | #include "CodeGen/OperatorTable.hpp"       // for isCtorDtorAssign | 
|---|
|  | 26 | #include "Common/SemanticError.hpp" | 
|---|
|  | 27 | #include "Common/Stats/Counter.hpp" | 
|---|
|  | 28 | #include "GenPoly/GenPoly.hpp" | 
|---|
|  | 29 | #include "InitTweak/InitTweak.hpp" | 
|---|
|  | 30 | #include "ResolvExpr/Cost.hpp" | 
|---|
| [fed6a0f] | 31 | #include "ResolvExpr/CandidateFinder.hpp"  // for referenceToRvalueConversion | 
|---|
| [c92bdcc] | 32 | #include "ResolvExpr/Unify.hpp" | 
|---|
|  | 33 | #include "SymTab/Mangler.hpp" | 
|---|
| [d76c588] | 34 |  | 
|---|
|  | 35 | namespace ast { | 
|---|
|  | 36 |  | 
|---|
|  | 37 | // Statistics block | 
|---|
|  | 38 | namespace { | 
|---|
|  | 39 | static inline auto stats() { | 
|---|
|  | 40 | using namespace Stats::Counters; | 
|---|
| [0bd3faf] | 41 | static auto group   = build<CounterGroup>("Symbol Tables"); | 
|---|
| [d76c588] | 42 | static struct { | 
|---|
|  | 43 | SimpleCounter * count; | 
|---|
|  | 44 | AverageCounter<double> * size; | 
|---|
|  | 45 | SimpleCounter * new_scopes; | 
|---|
|  | 46 | SimpleCounter * lazy_scopes; | 
|---|
|  | 47 | AverageCounter<double> * avg_scope_depth; | 
|---|
|  | 48 | MaxCounter<size_t> * max_scope_depth; | 
|---|
|  | 49 | SimpleCounter * add_calls; | 
|---|
|  | 50 | SimpleCounter * lookup_calls; | 
|---|
|  | 51 | SimpleCounter * map_lookups; | 
|---|
|  | 52 | SimpleCounter * map_mutations; | 
|---|
|  | 53 | } ret = { | 
|---|
|  | 54 | .count   = build<SimpleCounter>("Count", group), | 
|---|
|  | 55 | .size    = build<AverageCounter<double>>("Average Size", group), | 
|---|
|  | 56 | .new_scopes = build<SimpleCounter>("Scopes", group), | 
|---|
|  | 57 | .lazy_scopes = build<SimpleCounter>("Lazy Scopes", group), | 
|---|
|  | 58 | .avg_scope_depth = build<AverageCounter<double>>("Average Scope", group), | 
|---|
|  | 59 | .max_scope_depth = build<MaxCounter<size_t>>("Max Scope", group), | 
|---|
|  | 60 | .add_calls = build<SimpleCounter>("Add Calls", group), | 
|---|
|  | 61 | .lookup_calls = build<SimpleCounter>("Lookup Calls", group), | 
|---|
|  | 62 | .map_lookups = build<SimpleCounter>("Map Lookups", group), | 
|---|
|  | 63 | .map_mutations = build<SimpleCounter>("Map Mutations", group) | 
|---|
|  | 64 | }; | 
|---|
|  | 65 | return ret; | 
|---|
|  | 66 | } | 
|---|
|  | 67 | } | 
|---|
|  | 68 |  | 
|---|
|  | 69 | Expr * SymbolTable::IdData::combine( const CodeLocation & loc, ResolvExpr::Cost & cost ) const { | 
|---|
| [9e23b446] | 70 | Expr * ret; | 
|---|
|  | 71 | if ( baseExpr ) { | 
|---|
|  | 72 | if (baseExpr->env) { | 
|---|
| [bb7422a] | 73 | Expr * base = deepCopy(baseExpr); | 
|---|
| [9e23b446] | 74 | const TypeSubstitution * subs = baseExpr->env; | 
|---|
|  | 75 | base->env = nullptr; | 
|---|
|  | 76 | ret = new MemberExpr{loc, id, referenceToRvalueConversion( base, cost )}; | 
|---|
|  | 77 | ret->env = subs; | 
|---|
| [6a0b043] | 78 | } else { | 
|---|
| [9e23b446] | 79 | ret = new MemberExpr{ loc, id, referenceToRvalueConversion( baseExpr, cost ) }; | 
|---|
|  | 80 | } | 
|---|
| [6a0b043] | 81 | } else { | 
|---|
| [9e23b446] | 82 | ret = new VariableExpr{ loc, id }; | 
|---|
|  | 83 | } | 
|---|
| [d76c588] | 84 | if ( deleter ) { ret = new DeletedExpr{ loc, ret, deleter }; } | 
|---|
|  | 85 | return ret; | 
|---|
|  | 86 | } | 
|---|
|  | 87 |  | 
|---|
| [b9fe89b] | 88 | SymbolTable::SymbolTable( ErrorDetection errorMode ) | 
|---|
| [e67991f] | 89 | : idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable(), | 
|---|
| [b9fe89b] | 90 | prevScope(), scope( 0 ), repScope( 0 ), errorMode(errorMode) { ++*stats().count; } | 
|---|
| [d76c588] | 91 |  | 
|---|
|  | 92 | SymbolTable::~SymbolTable() { stats().size->push( idTable ? idTable->size() : 0 ); } | 
|---|
|  | 93 |  | 
|---|
| [b9fe89b] | 94 | void SymbolTable::OnFindError( CodeLocation location, std::string error ) const { | 
|---|
|  | 95 | assertf( errorMode != AssertClean, "Name collision/redefinition, found during a compilation phase where none should be possible.  Detail: %s", error.c_str() ); | 
|---|
|  | 96 | if (errorMode == ValidateOnAdd) { | 
|---|
|  | 97 | SemanticError(location, error); | 
|---|
|  | 98 | } | 
|---|
|  | 99 | assertf( errorMode == IgnoreErrors, "Unrecognized symbol-table error mode %d", errorMode ); | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
| [d76c588] | 102 | void SymbolTable::enterScope() { | 
|---|
|  | 103 | ++scope; | 
|---|
|  | 104 |  | 
|---|
|  | 105 | ++*stats().new_scopes; | 
|---|
|  | 106 | stats().avg_scope_depth->push( scope ); | 
|---|
|  | 107 | stats().max_scope_depth->push( scope ); | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | void SymbolTable::leaveScope() { | 
|---|
|  | 111 | if ( repScope == scope ) { | 
|---|
|  | 112 | Ptr prev = prevScope;           // make sure prevScope stays live | 
|---|
|  | 113 | *this = std::move(*prevScope);  // replace with previous scope | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | --scope; | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
| [e5c3811] | 119 | SymbolTable::SpecialFunctionKind SymbolTable::getSpecialFunctionKind(const std::string & name) { | 
|---|
|  | 120 | if (name == "?{}") return CTOR; | 
|---|
|  | 121 | if (name == "^?{}") return DTOR; | 
|---|
|  | 122 | if (name == "?=?") return ASSIGN; | 
|---|
|  | 123 | return NUMBER_OF_KINDS; | 
|---|
|  | 124 | } | 
|---|
|  | 125 |  | 
|---|
| [d76c588] | 126 | std::vector<SymbolTable::IdData> SymbolTable::lookupId( const std::string &id ) const { | 
|---|
| [e5c3811] | 127 | static Stats::Counters::CounterGroup * name_lookup_stats = Stats::Counters::build<Stats::Counters::CounterGroup>("Name Lookup Stats"); | 
|---|
|  | 128 | static std::map<std::string, Stats::Counters::SimpleCounter *> lookups_by_name; | 
|---|
|  | 129 | static std::map<std::string, Stats::Counters::SimpleCounter *> candidates_by_name; | 
|---|
|  | 130 |  | 
|---|
|  | 131 | SpecialFunctionKind kind = getSpecialFunctionKind(id); | 
|---|
|  | 132 | if (kind != NUMBER_OF_KINDS) return specialLookupId(kind); | 
|---|
|  | 133 |  | 
|---|
| [d76c588] | 134 | ++*stats().lookup_calls; | 
|---|
|  | 135 | if ( ! idTable ) return {}; | 
|---|
|  | 136 |  | 
|---|
|  | 137 | ++*stats().map_lookups; | 
|---|
|  | 138 | auto decls = idTable->find( id ); | 
|---|
|  | 139 | if ( decls == idTable->end() ) return {}; | 
|---|
|  | 140 |  | 
|---|
|  | 141 | std::vector<IdData> out; | 
|---|
|  | 142 | for ( auto decl : *(decls->second) ) { | 
|---|
|  | 143 | out.push_back( decl.second ); | 
|---|
|  | 144 | } | 
|---|
| [e5c3811] | 145 |  | 
|---|
|  | 146 | if (Stats::Counters::enabled) { | 
|---|
|  | 147 | if (! lookups_by_name.count(id)) { | 
|---|
|  | 148 | // leaks some strings, but it is because Counters do not hold them | 
|---|
|  | 149 | auto lookupCounterName = new std::string(id + "%count"); | 
|---|
|  | 150 | auto candidatesCounterName = new std::string(id + "%candidate"); | 
|---|
|  | 151 | lookups_by_name.emplace(id, new Stats::Counters::SimpleCounter(lookupCounterName->c_str(), name_lookup_stats)); | 
|---|
|  | 152 | candidates_by_name.emplace(id, new Stats::Counters::SimpleCounter(candidatesCounterName->c_str(), name_lookup_stats)); | 
|---|
|  | 153 | } | 
|---|
|  | 154 | (*lookups_by_name[id]) ++; | 
|---|
|  | 155 | *candidates_by_name[id] += out.size(); | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
|  | 158 | return out; | 
|---|
|  | 159 | } | 
|---|
|  | 160 |  | 
|---|
|  | 161 | std::vector<SymbolTable::IdData> SymbolTable::specialLookupId( SymbolTable::SpecialFunctionKind kind, const std::string & otypeKey ) const { | 
|---|
|  | 162 | static Stats::Counters::CounterGroup * special_stats = Stats::Counters::build<Stats::Counters::CounterGroup>("Special Lookups"); | 
|---|
|  | 163 | static Stats::Counters::SimpleCounter * stat_counts[3] = { | 
|---|
|  | 164 | Stats::Counters::build<Stats::Counters::SimpleCounter>("constructor - count", special_stats), | 
|---|
|  | 165 | Stats::Counters::build<Stats::Counters::SimpleCounter>("destructor - count", special_stats), | 
|---|
|  | 166 | Stats::Counters::build<Stats::Counters::SimpleCounter>("assignment - count", special_stats) | 
|---|
|  | 167 | }; | 
|---|
|  | 168 |  | 
|---|
|  | 169 | static Stats::Counters::SimpleCounter * stat_candidates[3] = { | 
|---|
|  | 170 | Stats::Counters::build<Stats::Counters::SimpleCounter>("constructor - candidates", special_stats), | 
|---|
|  | 171 | Stats::Counters::build<Stats::Counters::SimpleCounter>("destructor - candidates", special_stats), | 
|---|
|  | 172 | Stats::Counters::build<Stats::Counters::SimpleCounter>("assignment - candidates", special_stats) | 
|---|
|  | 173 | }; | 
|---|
|  | 174 |  | 
|---|
|  | 175 | static Stats::Counters::SimpleCounter * num_lookup_with_key | 
|---|
|  | 176 | = Stats::Counters::build<Stats::Counters::SimpleCounter>("keyed lookups", special_stats); | 
|---|
|  | 177 | static Stats::Counters::SimpleCounter * num_lookup_without_key | 
|---|
|  | 178 | = Stats::Counters::build<Stats::Counters::SimpleCounter>("unkeyed lookups", special_stats); | 
|---|
|  | 179 |  | 
|---|
|  | 180 | assert (kind != NUMBER_OF_KINDS); | 
|---|
|  | 181 | ++*stats().lookup_calls; | 
|---|
|  | 182 | if ( ! specialFunctionTable[kind] ) return {}; | 
|---|
|  | 183 |  | 
|---|
|  | 184 | std::vector<IdData> out; | 
|---|
|  | 185 |  | 
|---|
|  | 186 | if (otypeKey.empty()) { // returns everything | 
|---|
|  | 187 | ++*num_lookup_without_key; | 
|---|
|  | 188 | for (auto & table : *specialFunctionTable[kind]) { | 
|---|
|  | 189 | for (auto & decl : *table.second) { | 
|---|
|  | 190 | out.push_back(decl.second); | 
|---|
|  | 191 | } | 
|---|
|  | 192 | } | 
|---|
| [6a0b043] | 193 | } else { | 
|---|
| [e5c3811] | 194 | ++*num_lookup_with_key; | 
|---|
|  | 195 | ++*stats().map_lookups; | 
|---|
|  | 196 | auto decls = specialFunctionTable[kind]->find(otypeKey); | 
|---|
|  | 197 | if (decls == specialFunctionTable[kind]->end()) return {}; | 
|---|
|  | 198 |  | 
|---|
|  | 199 | for (auto decl : *(decls->second)) { | 
|---|
|  | 200 | out.push_back(decl.second); | 
|---|
|  | 201 | } | 
|---|
|  | 202 | } | 
|---|
|  | 203 |  | 
|---|
|  | 204 | ++*stat_counts[kind]; | 
|---|
|  | 205 | *stat_candidates[kind] += out.size(); | 
|---|
|  | 206 |  | 
|---|
| [d76c588] | 207 | return out; | 
|---|
|  | 208 | } | 
|---|
|  | 209 |  | 
|---|
|  | 210 | const NamedTypeDecl * SymbolTable::lookupType( const std::string &id ) const { | 
|---|
|  | 211 | ++*stats().lookup_calls; | 
|---|
|  | 212 | if ( ! typeTable ) return nullptr; | 
|---|
|  | 213 | ++*stats().map_lookups; | 
|---|
|  | 214 | auto it = typeTable->find( id ); | 
|---|
|  | 215 | return it == typeTable->end() ? nullptr : it->second.decl; | 
|---|
|  | 216 | } | 
|---|
|  | 217 |  | 
|---|
|  | 218 | const StructDecl * SymbolTable::lookupStruct( const std::string &id ) const { | 
|---|
|  | 219 | ++*stats().lookup_calls; | 
|---|
|  | 220 | if ( ! structTable ) return nullptr; | 
|---|
|  | 221 | ++*stats().map_lookups; | 
|---|
|  | 222 | auto it = structTable->find( id ); | 
|---|
|  | 223 | return it == structTable->end() ? nullptr : it->second.decl; | 
|---|
|  | 224 | } | 
|---|
|  | 225 |  | 
|---|
|  | 226 | const EnumDecl * SymbolTable::lookupEnum( const std::string &id ) const { | 
|---|
|  | 227 | ++*stats().lookup_calls; | 
|---|
|  | 228 | if ( ! enumTable ) return nullptr; | 
|---|
|  | 229 | ++*stats().map_lookups; | 
|---|
|  | 230 | auto it = enumTable->find( id ); | 
|---|
|  | 231 | return it == enumTable->end() ? nullptr : it->second.decl; | 
|---|
|  | 232 | } | 
|---|
|  | 233 |  | 
|---|
|  | 234 | const UnionDecl * SymbolTable::lookupUnion( const std::string &id ) const { | 
|---|
|  | 235 | ++*stats().lookup_calls; | 
|---|
|  | 236 | if ( ! unionTable ) return nullptr; | 
|---|
|  | 237 | ++*stats().map_lookups; | 
|---|
|  | 238 | auto it = unionTable->find( id ); | 
|---|
|  | 239 | return it == unionTable->end() ? nullptr : it->second.decl; | 
|---|
|  | 240 | } | 
|---|
|  | 241 |  | 
|---|
|  | 242 | const TraitDecl * SymbolTable::lookupTrait( const std::string &id ) const { | 
|---|
|  | 243 | ++*stats().lookup_calls; | 
|---|
|  | 244 | if ( ! traitTable ) return nullptr; | 
|---|
|  | 245 | ++*stats().map_lookups; | 
|---|
|  | 246 | auto it = traitTable->find( id ); | 
|---|
|  | 247 | return it == traitTable->end() ? nullptr : it->second.decl; | 
|---|
|  | 248 | } | 
|---|
|  | 249 |  | 
|---|
|  | 250 | const NamedTypeDecl * SymbolTable::globalLookupType( const std::string &id ) const { | 
|---|
|  | 251 | return atScope( 0 )->lookupType( id ); | 
|---|
|  | 252 | } | 
|---|
|  | 253 |  | 
|---|
|  | 254 | const StructDecl * SymbolTable::globalLookupStruct( const std::string &id ) const { | 
|---|
|  | 255 | return atScope( 0 )->lookupStruct( id ); | 
|---|
|  | 256 | } | 
|---|
|  | 257 |  | 
|---|
|  | 258 | const UnionDecl * SymbolTable::globalLookupUnion( const std::string &id ) const { | 
|---|
|  | 259 | return atScope( 0 )->lookupUnion( id ); | 
|---|
|  | 260 | } | 
|---|
|  | 261 |  | 
|---|
|  | 262 | const EnumDecl * SymbolTable::globalLookupEnum( const std::string &id ) const { | 
|---|
|  | 263 | return atScope( 0 )->lookupEnum( id ); | 
|---|
|  | 264 | } | 
|---|
|  | 265 |  | 
|---|
|  | 266 | void SymbolTable::addId( const DeclWithType * decl, const Expr * baseExpr ) { | 
|---|
|  | 267 | // default handling of conflicts is to raise an error | 
|---|
| [d859a30] | 268 | addIdCommon( decl, OnConflict::error(), baseExpr, decl->isDeleted ? decl : nullptr ); | 
|---|
| [d76c588] | 269 | } | 
|---|
|  | 270 |  | 
|---|
| [e67991f] | 271 | void SymbolTable::addDeletedId( const DeclWithType * decl, const Decl * deleter ) { | 
|---|
| [d76c588] | 272 | // default handling of conflicts is to raise an error | 
|---|
| [d859a30] | 273 | addIdCommon( decl, OnConflict::error(), nullptr, deleter ); | 
|---|
| [d76c588] | 274 | } | 
|---|
|  | 275 |  | 
|---|
| [b9fe89b] | 276 | bool SymbolTable::addedTypeConflicts( | 
|---|
|  | 277 | const NamedTypeDecl * existing, const NamedTypeDecl * added ) const { | 
|---|
|  | 278 | if ( existing->base == nullptr ) { | 
|---|
|  | 279 | return false; | 
|---|
|  | 280 | } else if ( added->base == nullptr ) { | 
|---|
| [d76c588] | 281 | return true; | 
|---|
| [b9fe89b] | 282 | } else { | 
|---|
|  | 283 | // typedef redeclarations are errors only if types are different | 
|---|
|  | 284 | if ( ! ResolvExpr::typesCompatible( existing->base, added->base ) ) { | 
|---|
|  | 285 | OnFindError( added->location, "redeclaration of " + added->name ); | 
|---|
|  | 286 | } | 
|---|
| [d76c588] | 287 | } | 
|---|
| [b9fe89b] | 288 | // does not need to be added to the table if both existing and added have a base that are | 
|---|
|  | 289 | // the same | 
|---|
|  | 290 | return true; | 
|---|
|  | 291 | } | 
|---|
| [d76c588] | 292 |  | 
|---|
| [6a0b043] | 293 | bool SymbolTable::addedDeclConflicts( | 
|---|
| [b9fe89b] | 294 | const AggregateDecl * existing, const AggregateDecl * added ) const { | 
|---|
|  | 295 | if ( ! existing->body ) { | 
|---|
|  | 296 | return false; | 
|---|
|  | 297 | } else if ( added->body ) { | 
|---|
|  | 298 | OnFindError( added, "redeclaration of " ); | 
|---|
| [d76c588] | 299 | } | 
|---|
| [b9fe89b] | 300 | return true; | 
|---|
| [d76c588] | 301 | } | 
|---|
|  | 302 |  | 
|---|
|  | 303 | void SymbolTable::addType( const NamedTypeDecl * decl ) { | 
|---|
|  | 304 | ++*stats().add_calls; | 
|---|
|  | 305 | const std::string &id = decl->name; | 
|---|
|  | 306 |  | 
|---|
| [e67991f] | 307 | if ( ! typeTable ) { | 
|---|
| [d76c588] | 308 | typeTable = TypeTable::new_ptr(); | 
|---|
|  | 309 | } else { | 
|---|
|  | 310 | ++*stats().map_lookups; | 
|---|
|  | 311 | auto existing = typeTable->find( id ); | 
|---|
| [e67991f] | 312 | if ( existing != typeTable->end() | 
|---|
|  | 313 | && existing->second.scope == scope | 
|---|
| [d76c588] | 314 | && addedTypeConflicts( existing->second.decl, decl ) ) return; | 
|---|
|  | 315 | } | 
|---|
| [e67991f] | 316 |  | 
|---|
| [d76c588] | 317 | lazyInitScope(); | 
|---|
|  | 318 | ++*stats().map_mutations; | 
|---|
|  | 319 | typeTable = typeTable->set( id, scoped<NamedTypeDecl>{ decl, scope } ); | 
|---|
|  | 320 | } | 
|---|
|  | 321 |  | 
|---|
| [e0069bd] | 322 | void SymbolTable::addStructId( const std::string &id ) { | 
|---|
| [4b8b2a4] | 323 | addStruct( new StructDecl( CodeLocation(), id ) ); | 
|---|
| [d76c588] | 324 | } | 
|---|
|  | 325 |  | 
|---|
|  | 326 | void SymbolTable::addStruct( const StructDecl * decl ) { | 
|---|
|  | 327 | ++*stats().add_calls; | 
|---|
|  | 328 | const std::string &id = decl->name; | 
|---|
|  | 329 |  | 
|---|
|  | 330 | if ( ! structTable ) { | 
|---|
|  | 331 | structTable = StructTable::new_ptr(); | 
|---|
|  | 332 | } else { | 
|---|
|  | 333 | ++*stats().map_lookups; | 
|---|
|  | 334 | auto existing = structTable->find( id ); | 
|---|
| [e67991f] | 335 | if ( existing != structTable->end() | 
|---|
|  | 336 | && existing->second.scope == scope | 
|---|
| [d76c588] | 337 | && addedDeclConflicts( existing->second.decl, decl ) ) return; | 
|---|
|  | 338 | } | 
|---|
|  | 339 |  | 
|---|
|  | 340 | lazyInitScope(); | 
|---|
|  | 341 | ++*stats().map_mutations; | 
|---|
|  | 342 | structTable = structTable->set( id, scoped<StructDecl>{ decl, scope } ); | 
|---|
|  | 343 | } | 
|---|
|  | 344 |  | 
|---|
|  | 345 | void SymbolTable::addEnum( const EnumDecl *decl ) { | 
|---|
|  | 346 | ++*stats().add_calls; | 
|---|
|  | 347 | const std::string &id = decl->name; | 
|---|
|  | 348 |  | 
|---|
|  | 349 | if ( ! enumTable ) { | 
|---|
|  | 350 | enumTable = EnumTable::new_ptr(); | 
|---|
|  | 351 | } else { | 
|---|
|  | 352 | ++*stats().map_lookups; | 
|---|
|  | 353 | auto existing = enumTable->find( id ); | 
|---|
| [e67991f] | 354 | if ( existing != enumTable->end() | 
|---|
|  | 355 | && existing->second.scope == scope | 
|---|
| [d76c588] | 356 | && addedDeclConflicts( existing->second.decl, decl ) ) return; | 
|---|
|  | 357 | } | 
|---|
| [e67991f] | 358 |  | 
|---|
| [d76c588] | 359 | lazyInitScope(); | 
|---|
|  | 360 | ++*stats().map_mutations; | 
|---|
|  | 361 | enumTable = enumTable->set( id, scoped<EnumDecl>{ decl, scope } ); | 
|---|
|  | 362 | } | 
|---|
|  | 363 |  | 
|---|
| [e0069bd] | 364 | void SymbolTable::addUnionId( const std::string &id ) { | 
|---|
| [4b8b2a4] | 365 | addUnion( new UnionDecl( CodeLocation(), id ) ); | 
|---|
| [d76c588] | 366 | } | 
|---|
|  | 367 |  | 
|---|
|  | 368 | void SymbolTable::addUnion( const UnionDecl * decl ) { | 
|---|
|  | 369 | ++*stats().add_calls; | 
|---|
|  | 370 | const std::string &id = decl->name; | 
|---|
|  | 371 |  | 
|---|
|  | 372 | if ( ! unionTable ) { | 
|---|
|  | 373 | unionTable = UnionTable::new_ptr(); | 
|---|
|  | 374 | } else { | 
|---|
|  | 375 | ++*stats().map_lookups; | 
|---|
|  | 376 | auto existing = unionTable->find( id ); | 
|---|
| [e67991f] | 377 | if ( existing != unionTable->end() | 
|---|
|  | 378 | && existing->second.scope == scope | 
|---|
| [d76c588] | 379 | && addedDeclConflicts( existing->second.decl, decl ) ) return; | 
|---|
|  | 380 | } | 
|---|
|  | 381 |  | 
|---|
|  | 382 | lazyInitScope(); | 
|---|
|  | 383 | ++*stats().map_mutations; | 
|---|
|  | 384 | unionTable = unionTable->set( id, scoped<UnionDecl>{ decl, scope } ); | 
|---|
|  | 385 | } | 
|---|
|  | 386 |  | 
|---|
|  | 387 | void SymbolTable::addTrait( const TraitDecl * decl ) { | 
|---|
|  | 388 | ++*stats().add_calls; | 
|---|
|  | 389 | const std::string &id = decl->name; | 
|---|
|  | 390 |  | 
|---|
|  | 391 | if ( ! traitTable ) { | 
|---|
|  | 392 | traitTable = TraitTable::new_ptr(); | 
|---|
|  | 393 | } else { | 
|---|
|  | 394 | ++*stats().map_lookups; | 
|---|
|  | 395 | auto existing = traitTable->find( id ); | 
|---|
| [e67991f] | 396 | if ( existing != traitTable->end() | 
|---|
|  | 397 | && existing->second.scope == scope | 
|---|
| [d76c588] | 398 | && addedDeclConflicts( existing->second.decl, decl ) ) return; | 
|---|
|  | 399 | } | 
|---|
|  | 400 |  | 
|---|
|  | 401 | lazyInitScope(); | 
|---|
|  | 402 | ++*stats().map_mutations; | 
|---|
|  | 403 | traitTable = traitTable->set( id, scoped<TraitDecl>{ decl, scope } ); | 
|---|
|  | 404 | } | 
|---|
|  | 405 |  | 
|---|
|  | 406 |  | 
|---|
| [e67991f] | 407 | void SymbolTable::addWith( const std::vector< ptr<Expr> > & withExprs, const Decl * withStmt ) { | 
|---|
| [d76c588] | 408 | for ( const Expr * expr : withExprs ) { | 
|---|
|  | 409 | if ( ! expr->result ) continue; | 
|---|
|  | 410 | const Type * resTy = expr->result->stripReferences(); | 
|---|
| [98e8b3b] | 411 | auto aggrType = dynamic_cast< const BaseInstType * >( resTy ); | 
|---|
| [e67991f] | 412 | assertf( aggrType, "WithStmt expr has non-aggregate type: %s", | 
|---|
| [d76c588] | 413 | toString( expr->result ).c_str() ); | 
|---|
|  | 414 | const AggregateDecl * aggr = aggrType->aggr(); | 
|---|
| [e67991f] | 415 | assertf( aggr, "WithStmt has null aggregate from type: %s", | 
|---|
| [d76c588] | 416 | toString( expr->result ).c_str() ); | 
|---|
| [e67991f] | 417 |  | 
|---|
| [d76c588] | 418 | addMembers( aggr, expr, OnConflict::deleteWith( withStmt ) ); | 
|---|
|  | 419 | } | 
|---|
|  | 420 | } | 
|---|
|  | 421 |  | 
|---|
|  | 422 | void SymbolTable::addIds( const std::vector< ptr<DeclWithType> > & decls ) { | 
|---|
|  | 423 | for ( const DeclWithType * decl : decls ) { addId( decl ); } | 
|---|
|  | 424 | } | 
|---|
|  | 425 |  | 
|---|
|  | 426 | void SymbolTable::addTypes( const std::vector< ptr<TypeDecl> > & tds ) { | 
|---|
|  | 427 | for ( const TypeDecl * td : tds ) { | 
|---|
|  | 428 | addType( td ); | 
|---|
|  | 429 | addIds( td->assertions ); | 
|---|
|  | 430 | } | 
|---|
|  | 431 | } | 
|---|
|  | 432 |  | 
|---|
| [490fb92e] | 433 |  | 
|---|
|  | 434 | void SymbolTable::addFunction( const FunctionDecl * func ) { | 
|---|
| [3e5dd913] | 435 | for (auto & td : func->type_params) { | 
|---|
|  | 436 | addType(td); | 
|---|
|  | 437 | } | 
|---|
|  | 438 | for (auto & asst : func->assertions) { | 
|---|
|  | 439 | addId(asst); | 
|---|
|  | 440 | } | 
|---|
|  | 441 | // addTypes( func->type->forall ); | 
|---|
| [490fb92e] | 442 | addIds( func->returns ); | 
|---|
|  | 443 | addIds( func->params ); | 
|---|
| [d76c588] | 444 | } | 
|---|
| [490fb92e] | 445 |  | 
|---|
| [d76c588] | 446 |  | 
|---|
|  | 447 | void SymbolTable::lazyInitScope() { | 
|---|
|  | 448 | // do nothing if already in represented scope | 
|---|
|  | 449 | if ( repScope == scope ) return; | 
|---|
|  | 450 |  | 
|---|
|  | 451 | ++*stats().lazy_scopes; | 
|---|
|  | 452 | // create rollback | 
|---|
|  | 453 | prevScope = std::make_shared<SymbolTable>( *this ); | 
|---|
|  | 454 | // update repScope | 
|---|
|  | 455 | repScope = scope; | 
|---|
|  | 456 | } | 
|---|
|  | 457 |  | 
|---|
|  | 458 | const ast::SymbolTable * SymbolTable::atScope( unsigned long target ) const { | 
|---|
|  | 459 | // by lazy construction, final symtab in list has repScope 0, cannot be > target | 
|---|
|  | 460 | // otherwise, will find first scope representing the target | 
|---|
|  | 461 | const SymbolTable * symtab = this; | 
|---|
|  | 462 | while ( symtab->repScope > target ) { | 
|---|
|  | 463 | symtab = symtab->prevScope.get(); | 
|---|
|  | 464 | } | 
|---|
|  | 465 | return symtab; | 
|---|
|  | 466 | } | 
|---|
|  | 467 |  | 
|---|
|  | 468 | namespace { | 
|---|
|  | 469 | /// gets the base type of the first parameter; decl must be a ctor/dtor/assignment function | 
|---|
| [e5c3811] | 470 | std::string getOtypeKey( const FunctionType * ftype, bool stripParams = true ) { | 
|---|
|  | 471 | const auto & params = ftype->params; | 
|---|
| [d76c588] | 472 | assert( ! params.empty() ); | 
|---|
|  | 473 | // use base type of pointer, so that qualifiers on the pointer type aren't considered. | 
|---|
| [e01eb4a] | 474 | const Type * base = ast::getPointerBase( params.front() ); | 
|---|
| [d76c588] | 475 | assert( base ); | 
|---|
| [e5c3811] | 476 | if (stripParams) { | 
|---|
|  | 477 | if (dynamic_cast<const PointerType *>(base)) return Mangle::Encoding::pointer; | 
|---|
|  | 478 | return Mangle::mangle( base, Mangle::Type | Mangle::NoGenericParams ); | 
|---|
| [6a0b043] | 479 | } else { | 
|---|
|  | 480 | return Mangle::mangle( base ); | 
|---|
| [e5c3811] | 481 | } | 
|---|
| [d76c588] | 482 | } | 
|---|
|  | 483 |  | 
|---|
| [e67991f] | 484 | /// gets the declaration for the function acting on a type specified by otype key, | 
|---|
| [d76c588] | 485 | /// nullptr if none such | 
|---|
| [e67991f] | 486 | const FunctionDecl * getFunctionForOtype( | 
|---|
| [d76c588] | 487 | const DeclWithType * decl, const std::string & otypeKey ) { | 
|---|
|  | 488 | auto func = dynamic_cast< const FunctionDecl * >( decl ); | 
|---|
| [e5c3811] | 489 | if ( ! func || otypeKey != getOtypeKey( func->type, false ) ) return nullptr; | 
|---|
| [d76c588] | 490 | return func; | 
|---|
|  | 491 | } | 
|---|
|  | 492 | } | 
|---|
|  | 493 |  | 
|---|
| [e67991f] | 494 | bool SymbolTable::removeSpecialOverrides( | 
|---|
| [d76c588] | 495 | SymbolTable::IdData & data, SymbolTable::MangleTable::Ptr & mangleTable ) { | 
|---|
| [e67991f] | 496 | // if a type contains user defined ctor/dtor/assign, then special rules trigger, which | 
|---|
|  | 497 | // determine the set of ctor/dtor/assign that can be used  by the requester. In particular, | 
|---|
|  | 498 | // if the user defines a default ctor, then the generated default ctor is unavailable, | 
|---|
|  | 499 | // likewise for copy ctor and dtor. If the user defines any ctor/dtor, then no generated | 
|---|
|  | 500 | // field ctors are available. If the user defines any ctor then the generated default ctor | 
|---|
|  | 501 | // is unavailable (intrinsic default ctor must be overridden exactly). If the user defines | 
|---|
|  | 502 | // anything that looks like a copy constructor, then the generated copy constructor is | 
|---|
| [d76c588] | 503 | // unavailable, and likewise for the assignment operator. | 
|---|
|  | 504 |  | 
|---|
|  | 505 | // only relevant on function declarations | 
|---|
|  | 506 | const FunctionDecl * function = data.id.as< FunctionDecl >(); | 
|---|
|  | 507 | if ( ! function ) return true; | 
|---|
|  | 508 | // only need to perform this check for constructors, destructors, and assignment functions | 
|---|
|  | 509 | if ( ! CodeGen::isCtorDtorAssign( data.id->name ) ) return true; | 
|---|
|  | 510 |  | 
|---|
|  | 511 | // set up information for this type | 
|---|
|  | 512 | bool dataIsUserDefinedFunc = ! function->linkage.is_overrideable; | 
|---|
|  | 513 | bool dataIsCopyFunc = InitTweak::isCopyFunction( function ); | 
|---|
| [e5c3811] | 514 | std::string dataOtypeKey = getOtypeKey( function->type, false ); // requires exact match to override autogen | 
|---|
| [d76c588] | 515 |  | 
|---|
|  | 516 | if ( dataIsUserDefinedFunc && dataIsCopyFunc ) { | 
|---|
|  | 517 | // this is a user-defined copy function | 
|---|
|  | 518 | // if this is the first such, delete/remove non-user-defined overloads as needed | 
|---|
|  | 519 | std::vector< std::string > removed; | 
|---|
|  | 520 | std::vector< MangleTable::value_type > deleted; | 
|---|
|  | 521 | bool alreadyUserDefinedFunc = false; | 
|---|
|  | 522 |  | 
|---|
|  | 523 | for ( const auto& entry : *mangleTable ) { | 
|---|
|  | 524 | // skip decls that aren't functions or are for the wrong type | 
|---|
|  | 525 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); | 
|---|
|  | 526 | if ( ! decl ) continue; | 
|---|
|  | 527 |  | 
|---|
|  | 528 | bool isCopyFunc = InitTweak::isCopyFunction( decl ); | 
|---|
|  | 529 | if ( ! decl->linkage.is_overrideable ) { | 
|---|
|  | 530 | // matching user-defined function | 
|---|
|  | 531 | if ( isCopyFunc ) { | 
|---|
|  | 532 | // mutation already performed, return early | 
|---|
|  | 533 | return true; | 
|---|
|  | 534 | } else { | 
|---|
|  | 535 | // note that non-copy deletions already performed | 
|---|
|  | 536 | alreadyUserDefinedFunc = true; | 
|---|
|  | 537 | } | 
|---|
|  | 538 | } else { | 
|---|
|  | 539 | // non-user-defined function; mark for deletion/removal as appropriate | 
|---|
|  | 540 | if ( isCopyFunc ) { | 
|---|
|  | 541 | removed.push_back( entry.first ); | 
|---|
|  | 542 | } else if ( ! alreadyUserDefinedFunc ) { | 
|---|
|  | 543 | deleted.push_back( entry ); | 
|---|
|  | 544 | } | 
|---|
|  | 545 | } | 
|---|
|  | 546 | } | 
|---|
|  | 547 |  | 
|---|
|  | 548 | // perform removals from mangle table, and deletions if necessary | 
|---|
|  | 549 | for ( const auto& key : removed ) { | 
|---|
|  | 550 | ++*stats().map_mutations; | 
|---|
|  | 551 | mangleTable = mangleTable->erase( key ); | 
|---|
|  | 552 | } | 
|---|
|  | 553 | if ( ! alreadyUserDefinedFunc ) for ( const auto& entry : deleted ) { | 
|---|
|  | 554 | ++*stats().map_mutations; | 
|---|
| [6a0b043] | 555 | mangleTable = mangleTable->set( entry.first, entry.second.withDeleter( function ) ); | 
|---|
| [d76c588] | 556 | } | 
|---|
|  | 557 | } else if ( dataIsUserDefinedFunc ) { | 
|---|
|  | 558 | // this is a user-defined non-copy function | 
|---|
|  | 559 | // if this is the first user-defined function, delete non-user-defined overloads | 
|---|
|  | 560 | std::vector< MangleTable::value_type > deleted; | 
|---|
| [e67991f] | 561 |  | 
|---|
| [d76c588] | 562 | for ( const auto& entry : *mangleTable ) { | 
|---|
|  | 563 | // skip decls that aren't functions or are for the wrong type | 
|---|
|  | 564 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); | 
|---|
|  | 565 | if ( ! decl ) continue; | 
|---|
|  | 566 |  | 
|---|
|  | 567 | // exit early if already a matching user-defined function; | 
|---|
|  | 568 | // earlier function will have mutated table | 
|---|
|  | 569 | if ( ! decl->linkage.is_overrideable ) return true; | 
|---|
|  | 570 |  | 
|---|
|  | 571 | // skip mutating intrinsic functions | 
|---|
|  | 572 | if ( decl->linkage == Linkage::Intrinsic ) continue; | 
|---|
|  | 573 |  | 
|---|
|  | 574 | // user-defined non-copy functions do not override copy functions | 
|---|
|  | 575 | if ( InitTweak::isCopyFunction( decl ) ) continue; | 
|---|
|  | 576 |  | 
|---|
|  | 577 | // this function to be deleted after mangleTable iteration is complete | 
|---|
|  | 578 | deleted.push_back( entry ); | 
|---|
|  | 579 | } | 
|---|
|  | 580 |  | 
|---|
|  | 581 | // mark deletions to update mangle table | 
|---|
|  | 582 | // this needs to be a separate loop because of iterator invalidation | 
|---|
|  | 583 | for ( const auto& entry : deleted ) { | 
|---|
|  | 584 | ++*stats().map_mutations; | 
|---|
| [6a0b043] | 585 | mangleTable = mangleTable->set( entry.first, entry.second.withDeleter( function ) ); | 
|---|
| [d76c588] | 586 | } | 
|---|
|  | 587 | } else if ( function->linkage != Linkage::Intrinsic ) { | 
|---|
|  | 588 | // this is an overridable generated function | 
|---|
|  | 589 | // if there already exists a matching user-defined function, delete this appropriately | 
|---|
|  | 590 | for ( const auto& entry : *mangleTable ) { | 
|---|
|  | 591 | // skip decls that aren't functions or are for the wrong type | 
|---|
|  | 592 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); | 
|---|
|  | 593 | if ( ! decl ) continue; | 
|---|
|  | 594 |  | 
|---|
|  | 595 | // skip non-user-defined functions | 
|---|
|  | 596 | if ( decl->linkage.is_overrideable ) continue; | 
|---|
|  | 597 |  | 
|---|
|  | 598 | if ( dataIsCopyFunc ) { | 
|---|
|  | 599 | // remove current function if exists a user-defined copy function | 
|---|
| [e67991f] | 600 | // since the signatures for copy functions don't need to match exactly, using | 
|---|
| [d76c588] | 601 | // a delete statement is the wrong approach | 
|---|
|  | 602 | if ( InitTweak::isCopyFunction( decl ) ) return false; | 
|---|
|  | 603 | } else { | 
|---|
|  | 604 | // mark current function deleted by first user-defined function found | 
|---|
|  | 605 | data.deleter = decl; | 
|---|
|  | 606 | return true; | 
|---|
|  | 607 | } | 
|---|
|  | 608 | } | 
|---|
|  | 609 | } | 
|---|
| [e67991f] | 610 |  | 
|---|
| [d76c588] | 611 | // nothing (more) to fix, return true | 
|---|
|  | 612 | return true; | 
|---|
|  | 613 | } | 
|---|
|  | 614 |  | 
|---|
|  | 615 | namespace { | 
|---|
|  | 616 | /// true iff the declaration represents a function | 
|---|
|  | 617 | bool isFunction( const DeclWithType * decl ) { | 
|---|
|  | 618 | return GenPoly::getFunctionType( decl->get_type() ); | 
|---|
|  | 619 | } | 
|---|
|  | 620 |  | 
|---|
|  | 621 | bool isObject( const DeclWithType * decl ) { return ! isFunction( decl ); } | 
|---|
|  | 622 |  | 
|---|
|  | 623 | /// true if the declaration represents a definition instead of a forward decl | 
|---|
|  | 624 | bool isDefinition( const DeclWithType * decl ) { | 
|---|
|  | 625 | if ( auto func = dynamic_cast< const FunctionDecl * >( decl ) ) { | 
|---|
|  | 626 | // a function is a definition if it has a body | 
|---|
|  | 627 | return func->stmts; | 
|---|
|  | 628 | } else { | 
|---|
|  | 629 | // an object is a definition if it is not marked extern | 
|---|
|  | 630 | return ! decl->storage.is_extern; | 
|---|
|  | 631 | } | 
|---|
|  | 632 | } | 
|---|
|  | 633 | } | 
|---|
|  | 634 |  | 
|---|
|  | 635 | bool SymbolTable::addedIdConflicts( | 
|---|
| [e67991f] | 636 | const SymbolTable::IdData & existing, const DeclWithType * added, | 
|---|
|  | 637 | SymbolTable::OnConflict handleConflicts, const Decl * deleter ) { | 
|---|
|  | 638 | // if we're giving the same name mangling to things of different types then there is something | 
|---|
| [d76c588] | 639 | // wrong | 
|---|
|  | 640 | assert( (isObject( added ) && isObject( existing.id ) ) | 
|---|
|  | 641 | || ( isFunction( added ) && isFunction( existing.id ) ) ); | 
|---|
| [e67991f] | 642 |  | 
|---|
| [d76c588] | 643 | if ( existing.id->linkage.is_overrideable ) { | 
|---|
|  | 644 | // new definition shadows the autogenerated one, even at the same scope | 
|---|
|  | 645 | return false; | 
|---|
| [c7ebbec] | 646 | } else if ( existing.id->linkage.is_overloadable | 
|---|
| [e67991f] | 647 | || ResolvExpr::typesCompatible( | 
|---|
| [251ce80] | 648 | added->get_type(), existing.id->get_type() ) ) { | 
|---|
| [e67991f] | 649 |  | 
|---|
| [d76c588] | 650 | // it is a conflict if one declaration is deleted and the other is not | 
|---|
|  | 651 | if ( deleter && ! existing.deleter ) { | 
|---|
|  | 652 | if ( handleConflicts.mode == OnConflict::Error ) { | 
|---|
| [b9fe89b] | 653 | OnFindError( added, "deletion of defined identifier " ); | 
|---|
| [d76c588] | 654 | } | 
|---|
|  | 655 | return true; | 
|---|
|  | 656 | } else if ( ! deleter && existing.deleter ) { | 
|---|
|  | 657 | if ( handleConflicts.mode == OnConflict::Error ) { | 
|---|
| [b9fe89b] | 658 | OnFindError( added, "definition of deleted identifier " ); | 
|---|
| [d76c588] | 659 | } | 
|---|
|  | 660 | return true; | 
|---|
|  | 661 | } | 
|---|
|  | 662 |  | 
|---|
|  | 663 | // it is a conflict if both declarations are definitions | 
|---|
|  | 664 | if ( isDefinition( added ) && isDefinition( existing.id ) ) { | 
|---|
|  | 665 | if ( handleConflicts.mode == OnConflict::Error ) { | 
|---|
| [b9fe89b] | 666 | OnFindError( added, | 
|---|
| [e67991f] | 667 | isFunction( added ) ? | 
|---|
|  | 668 | "duplicate function definition for " : | 
|---|
| [d76c588] | 669 | "duplicate object definition for " ); | 
|---|
|  | 670 | } | 
|---|
|  | 671 | return true; | 
|---|
|  | 672 | } | 
|---|
|  | 673 | } else { | 
|---|
|  | 674 | if ( handleConflicts.mode == OnConflict::Error ) { | 
|---|
| [b9fe89b] | 675 | OnFindError( added, "duplicate definition for " ); | 
|---|
| [d76c588] | 676 | } | 
|---|
|  | 677 | return true; | 
|---|
|  | 678 | } | 
|---|
|  | 679 |  | 
|---|
|  | 680 | return true; | 
|---|
|  | 681 | } | 
|---|
|  | 682 |  | 
|---|
| [d859a30] | 683 | void SymbolTable::addIdCommon( | 
|---|
|  | 684 | const DeclWithType * decl, SymbolTable::OnConflict handleConflicts, | 
|---|
|  | 685 | const Expr * baseExpr, const Decl * deleter ) { | 
|---|
| [e5c3811] | 686 | SpecialFunctionKind kind = getSpecialFunctionKind(decl->name); | 
|---|
|  | 687 | if (kind == NUMBER_OF_KINDS) { // not a special decl | 
|---|
| [d859a30] | 688 | addIdToTable(decl, decl->name, idTable, handleConflicts, baseExpr, deleter); | 
|---|
| [6a0b043] | 689 | } else { | 
|---|
| [e5c3811] | 690 | std::string key; | 
|---|
|  | 691 | if (auto func = dynamic_cast<const FunctionDecl *>(decl)) { | 
|---|
|  | 692 | key = getOtypeKey(func->type); | 
|---|
| [6a0b043] | 693 | } else if (auto obj = dynamic_cast<const ObjectDecl *>(decl)) { | 
|---|
| [e5c3811] | 694 | key = getOtypeKey(obj->type.strict_as<PointerType>()->base.strict_as<FunctionType>()); | 
|---|
| [6a0b043] | 695 | } else { | 
|---|
| [e5c3811] | 696 | assertf(false, "special decl with non-function type"); | 
|---|
|  | 697 | } | 
|---|
| [d859a30] | 698 | addIdToTable(decl, key, specialFunctionTable[kind], handleConflicts, baseExpr, deleter); | 
|---|
| [e5c3811] | 699 | } | 
|---|
|  | 700 | } | 
|---|
|  | 701 |  | 
|---|
| [d859a30] | 702 | void SymbolTable::addIdToTable( | 
|---|
|  | 703 | const DeclWithType * decl, const std::string & lookupKey, | 
|---|
|  | 704 | IdTable::Ptr & table, SymbolTable::OnConflict handleConflicts, | 
|---|
|  | 705 | const Expr * baseExpr, const Decl * deleter ) { | 
|---|
| [d76c588] | 706 | ++*stats().add_calls; | 
|---|
|  | 707 | const std::string &name = decl->name; | 
|---|
|  | 708 | if ( name == "" ) return; | 
|---|
|  | 709 |  | 
|---|
|  | 710 | std::string mangleName; | 
|---|
|  | 711 | if ( decl->linkage.is_overrideable ) { | 
|---|
| [e67991f] | 712 | // mangle the name without including the appropriate suffix, so overridable routines | 
|---|
| [d76c588] | 713 | // are placed into the same "bucket" as their user defined versions. | 
|---|
|  | 714 | mangleName = Mangle::mangle( decl, Mangle::Mode{ Mangle::NoOverrideable } ); | 
|---|
|  | 715 | } else { | 
|---|
|  | 716 | mangleName = Mangle::mangle( decl ); | 
|---|
|  | 717 | } | 
|---|
|  | 718 |  | 
|---|
| [e67991f] | 719 | // this ensures that no two declarations with the same unmangled name at the same scope | 
|---|
| [d76c588] | 720 | // both have C linkage | 
|---|
| [c7ebbec] | 721 | if ( decl->linkage.is_overloadable ) { | 
|---|
| [d76c588] | 722 | // Check that a Cforall declaration doesn't override any C declaration | 
|---|
|  | 723 | if ( hasCompatibleCDecl( name, mangleName ) ) { | 
|---|
| [b9fe89b] | 724 | OnFindError( decl, "Cforall declaration hides C function " ); | 
|---|
| [d76c588] | 725 | } | 
|---|
|  | 726 | } else { | 
|---|
| [e67991f] | 727 | // NOTE: only correct if name mangling is completely isomorphic to C | 
|---|
| [d76c588] | 728 | // type-compatibility, which it may not be. | 
|---|
|  | 729 | if ( hasIncompatibleCDecl( name, mangleName ) ) { | 
|---|
| [b9fe89b] | 730 | OnFindError( decl, "conflicting overload of C function " ); | 
|---|
| [d76c588] | 731 | } | 
|---|
|  | 732 | } | 
|---|
|  | 733 |  | 
|---|
|  | 734 | // ensure tables exist and add identifier | 
|---|
|  | 735 | MangleTable::Ptr mangleTable; | 
|---|
| [e5c3811] | 736 | if ( ! table ) { | 
|---|
|  | 737 | table = IdTable::new_ptr(); | 
|---|
| [d76c588] | 738 | mangleTable = MangleTable::new_ptr(); | 
|---|
|  | 739 | } else { | 
|---|
|  | 740 | ++*stats().map_lookups; | 
|---|
| [e5c3811] | 741 | auto decls = table->find( lookupKey ); | 
|---|
|  | 742 | if ( decls == table->end() ) { | 
|---|
| [d76c588] | 743 | mangleTable = MangleTable::new_ptr(); | 
|---|
|  | 744 | } else { | 
|---|
|  | 745 | mangleTable = decls->second; | 
|---|
|  | 746 | // skip in-scope repeat declarations of same identifier | 
|---|
|  | 747 | ++*stats().map_lookups; | 
|---|
|  | 748 | auto existing = mangleTable->find( mangleName ); | 
|---|
|  | 749 | if ( existing != mangleTable->end() | 
|---|
|  | 750 | && existing->second.scope == scope | 
|---|
| [6a0b043] | 751 | && existing->second.id | 
|---|
|  | 752 | && addedIdConflicts( existing->second, decl, handleConflicts, deleter ) ) { | 
|---|
|  | 753 | if ( handleConflicts.mode == OnConflict::Delete ) { | 
|---|
|  | 754 | // set delete expression for conflicting identifier | 
|---|
|  | 755 | lazyInitScope(); | 
|---|
|  | 756 | *stats().map_mutations += 2; | 
|---|
|  | 757 | table = table->set( | 
|---|
|  | 758 | lookupKey, | 
|---|
|  | 759 | mangleTable->set( | 
|---|
|  | 760 | mangleName, | 
|---|
|  | 761 | existing->second.withDeleter( handleConflicts.deleter ) ) ); | 
|---|
| [d76c588] | 762 | } | 
|---|
| [6a0b043] | 763 | return; | 
|---|
| [d76c588] | 764 | } | 
|---|
|  | 765 | } | 
|---|
|  | 766 | } | 
|---|
|  | 767 |  | 
|---|
|  | 768 | // add/overwrite with new identifier | 
|---|
|  | 769 | lazyInitScope(); | 
|---|
|  | 770 | IdData data{ decl, baseExpr, deleter, scope }; | 
|---|
|  | 771 | // Ensure that auto-generated ctor/dtor/assignment are deleted if necessary | 
|---|
| [e5c3811] | 772 | if (table != idTable) { // adding to special table | 
|---|
|  | 773 | if ( ! removeSpecialOverrides( data, mangleTable ) ) return; | 
|---|
|  | 774 | } | 
|---|
| [d76c588] | 775 | *stats().map_mutations += 2; | 
|---|
| [e5c3811] | 776 | table = table->set( lookupKey, mangleTable->set( mangleName, std::move(data) ) ); | 
|---|
| [d76c588] | 777 | } | 
|---|
|  | 778 |  | 
|---|
| [e67991f] | 779 | void SymbolTable::addMembers( | 
|---|
| [d76c588] | 780 | const AggregateDecl * aggr, const Expr * expr, SymbolTable::OnConflict handleConflicts ) { | 
|---|
| [d859a30] | 781 | for ( const ptr<Decl> & decl : aggr->members ) { | 
|---|
|  | 782 | auto dwt = decl.as<DeclWithType>(); | 
|---|
|  | 783 | if ( nullptr == dwt ) continue; | 
|---|
|  | 784 | addIdCommon( dwt, handleConflicts, expr ); | 
|---|
|  | 785 | // Inline through unnamed struct/union members. | 
|---|
|  | 786 | if ( "" != dwt->name ) continue; | 
|---|
|  | 787 | const Type * t = dwt->get_type()->stripReferences(); | 
|---|
|  | 788 | if ( auto rty = dynamic_cast<const BaseInstType *>( t ) ) { | 
|---|
|  | 789 | if ( ! dynamic_cast<const StructInstType *>(rty) | 
|---|
|  | 790 | && ! dynamic_cast<const UnionInstType *>(rty) ) continue; | 
|---|
|  | 791 | ResolvExpr::Cost cost = ResolvExpr::Cost::zero; | 
|---|
|  | 792 | ast::ptr<ast::TypeSubstitution> tmp = expr->env; | 
|---|
|  | 793 | expr = mutate_field(expr, &Expr::env, nullptr); | 
|---|
|  | 794 | const Expr * base = ResolvExpr::referenceToRvalueConversion( expr, cost ); | 
|---|
|  | 795 | base = mutate_field(base, &Expr::env, tmp); | 
|---|
|  | 796 |  | 
|---|
|  | 797 | addMembers( | 
|---|
|  | 798 | rty->aggr(), new MemberExpr{ base->location, dwt, base }, handleConflicts ); | 
|---|
| [d76c588] | 799 | } | 
|---|
|  | 800 | } | 
|---|
|  | 801 | } | 
|---|
|  | 802 |  | 
|---|
|  | 803 | bool SymbolTable::hasCompatibleCDecl( const std::string &id, const std::string &mangleName ) const { | 
|---|
|  | 804 | if ( ! idTable ) return false; | 
|---|
|  | 805 |  | 
|---|
|  | 806 | ++*stats().map_lookups; | 
|---|
|  | 807 | auto decls = idTable->find( id ); | 
|---|
|  | 808 | if ( decls == idTable->end() ) return false; | 
|---|
|  | 809 |  | 
|---|
|  | 810 | for ( auto decl : *(decls->second) ) { | 
|---|
|  | 811 | // skip other scopes (hidden by this decl) | 
|---|
|  | 812 | if ( decl.second.scope != scope ) continue; | 
|---|
|  | 813 | // check for C decl with compatible type (by mangleName) | 
|---|
| [c7ebbec] | 814 | if ( !decl.second.id->linkage.is_overloadable && decl.first == mangleName ) return true; | 
|---|
| [d76c588] | 815 | } | 
|---|
| [e67991f] | 816 |  | 
|---|
| [d76c588] | 817 | return false; | 
|---|
|  | 818 | } | 
|---|
|  | 819 |  | 
|---|
|  | 820 | bool SymbolTable::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const { | 
|---|
|  | 821 | if ( ! idTable ) return false; | 
|---|
|  | 822 |  | 
|---|
|  | 823 | ++*stats().map_lookups; | 
|---|
|  | 824 | auto decls = idTable->find( id ); | 
|---|
|  | 825 | if ( decls == idTable->end() ) return false; | 
|---|
|  | 826 |  | 
|---|
|  | 827 | for ( auto decl : *(decls->second) ) { | 
|---|
|  | 828 | // skip other scopes (hidden by this decl) | 
|---|
|  | 829 | if ( decl.second.scope != scope ) continue; | 
|---|
|  | 830 | // check for C decl with incompatible type (by manglename) | 
|---|
| [c7ebbec] | 831 | if ( !decl.second.id->linkage.is_overloadable && decl.first != mangleName ) return true; | 
|---|
| [d76c588] | 832 | } | 
|---|
|  | 833 |  | 
|---|
|  | 834 | return false; | 
|---|
|  | 835 | } | 
|---|
|  | 836 |  | 
|---|
|  | 837 | } | 
|---|
|  | 838 |  | 
|---|
|  | 839 | // Local Variables: // | 
|---|
|  | 840 | // tab-width: 4 // | 
|---|
|  | 841 | // mode: c++ // | 
|---|
|  | 842 | // compile-command: "make install" // | 
|---|
| [98e8b3b] | 843 | // End: // | 
|---|