[a32b204] | 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 | // |
---|
[71f4e4f] | 7 | // Resolver.cc -- |
---|
[a32b204] | 8 | // |
---|
[d76c588] | 9 | // Author : Aaron B. Moss |
---|
[a32b204] | 10 | // Created On : Sun May 17 12:17:01 2015 |
---|
[d76c588] | 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Wed May 29 11:00:00 2019 |
---|
| 13 | // Update Count : 241 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[e3e16bc] | 16 | #include <cassert> // for strict_dynamic_cast, assert |
---|
[ea6332d] | 17 | #include <memory> // for allocator, allocator_traits<... |
---|
| 18 | #include <tuple> // for get |
---|
[6d6e829] | 19 | #include <vector> // for vector |
---|
[ea6332d] | 20 | |
---|
| 21 | #include "Alternative.h" // for Alternative, AltList |
---|
| 22 | #include "AlternativeFinder.h" // for AlternativeFinder, resolveIn... |
---|
[99d4584] | 23 | #include "Candidate.hpp" |
---|
| 24 | #include "CandidateFinder.hpp" |
---|
[d76c588] | 25 | #include "CurrentObject.h" // for CurrentObject |
---|
| 26 | #include "RenameVars.h" // for RenameVars, global_renamer |
---|
| 27 | #include "Resolver.h" |
---|
| 28 | #include "ResolvMode.h" // for ResolvMode |
---|
| 29 | #include "typeops.h" // for extractResultType |
---|
| 30 | #include "Unify.h" // for unify |
---|
[4864a73] | 31 | #include "AST/Chain.hpp" |
---|
[2a8f0c1] | 32 | #include "AST/Decl.hpp" |
---|
| 33 | #include "AST/Init.hpp" |
---|
[d76c588] | 34 | #include "AST/Pass.hpp" |
---|
[99d4584] | 35 | #include "AST/Print.hpp" |
---|
[d76c588] | 36 | #include "AST/SymbolTable.hpp" |
---|
[a4ca48c] | 37 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
[ea6332d] | 38 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 39 | #include "Common/utility.h" // for ValueGuard, group_iterate |
---|
[0a60c04] | 40 | #include "InitTweak/GenInit.h" |
---|
[ea6332d] | 41 | #include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt |
---|
| 42 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment |
---|
| 43 | #include "SymTab/Autogen.h" // for SizeType |
---|
| 44 | #include "SymTab/Indexer.h" // for Indexer |
---|
| 45 | #include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar... |
---|
| 46 | #include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr |
---|
| 47 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit |
---|
| 48 | #include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt |
---|
| 49 | #include "SynTree/Type.h" // for Type, BasicType, PointerType |
---|
| 50 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
| 51 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
[0a60c04] | 52 | #include "Tuples/Tuples.h" |
---|
[2bfc6b2] | 53 | #include "Validate/FindSpecialDecls.h" // for SizeType |
---|
[51b7345] | 54 | |
---|
[d9a0e76] | 55 | using namespace std; |
---|
[51b7345] | 56 | |
---|
[d9a0e76] | 57 | namespace ResolvExpr { |
---|
[d76c588] | 58 | struct Resolver_old final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver_old>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
| 59 | Resolver_old() {} |
---|
| 60 | Resolver_old( const SymTab::Indexer & other ) { |
---|
[a4ca48c] | 61 | indexer = other; |
---|
[1d2b64f] | 62 | } |
---|
[71f4e4f] | 63 | |
---|
[5170d95] | 64 | void previsit( FunctionDecl * functionDecl ); |
---|
| 65 | void postvisit( FunctionDecl * functionDecl ); |
---|
| 66 | void previsit( ObjectDecl * objectDecll ); |
---|
[a4ca48c] | 67 | void previsit( EnumDecl * enumDecl ); |
---|
[bd87b138] | 68 | void previsit( StaticAssertDecl * assertDecl ); |
---|
[a4ca48c] | 69 | |
---|
| 70 | void previsit( ArrayType * at ); |
---|
| 71 | void previsit( PointerType * at ); |
---|
| 72 | |
---|
[5170d95] | 73 | void previsit( ExprStmt * exprStmt ); |
---|
| 74 | void previsit( AsmExpr * asmExpr ); |
---|
| 75 | void previsit( AsmStmt * asmStmt ); |
---|
| 76 | void previsit( IfStmt * ifStmt ); |
---|
| 77 | void previsit( WhileStmt * whileStmt ); |
---|
| 78 | void previsit( ForStmt * forStmt ); |
---|
| 79 | void previsit( SwitchStmt * switchStmt ); |
---|
| 80 | void previsit( CaseStmt * caseStmt ); |
---|
| 81 | void previsit( BranchStmt * branchStmt ); |
---|
| 82 | void previsit( ReturnStmt * returnStmt ); |
---|
| 83 | void previsit( ThrowStmt * throwStmt ); |
---|
| 84 | void previsit( CatchStmt * catchStmt ); |
---|
[695e00d] | 85 | void previsit( WaitForStmt * stmt ); |
---|
[a4ca48c] | 86 | |
---|
[5170d95] | 87 | void previsit( SingleInit * singleInit ); |
---|
| 88 | void previsit( ListInit * listInit ); |
---|
| 89 | void previsit( ConstructorInit * ctorInit ); |
---|
[a32b204] | 90 | private: |
---|
[c28a038d] | 91 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
[94b4364] | 92 | |
---|
[40e636a] | 93 | template< typename PtrType > |
---|
| 94 | void handlePtrType( PtrType * type ); |
---|
| 95 | |
---|
[c28a038d] | 96 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
[b726084] | 97 | |
---|
[77971f6] | 98 | Type * functionReturn = nullptr; |
---|
[e4d829b] | 99 | CurrentObject currentObject = nullptr; |
---|
[a436947] | 100 | bool inEnumDecl = false; |
---|
[a32b204] | 101 | }; |
---|
[d9a0e76] | 102 | |
---|
[2a6292d] | 103 | struct ResolveWithExprs : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveWithExprs>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
| 104 | void previsit( FunctionDecl * ); |
---|
| 105 | void previsit( WithStmt * ); |
---|
| 106 | |
---|
| 107 | void resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ); |
---|
| 108 | }; |
---|
| 109 | |
---|
[a32b204] | 110 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
[d76c588] | 111 | PassVisitor<Resolver_old> resolver; |
---|
[a32b204] | 112 | acceptAll( translationUnit, resolver ); |
---|
[d9a0e76] | 113 | } |
---|
| 114 | |
---|
[5170d95] | 115 | void resolveDecl( Declaration * decl, const SymTab::Indexer & indexer ) { |
---|
[d76c588] | 116 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
[8b11840] | 117 | maybeAccept( decl, resolver ); |
---|
| 118 | } |
---|
| 119 | |
---|
[c71b256] | 120 | namespace { |
---|
[99d4584] | 121 | struct DeleteFinder_old : public WithShortCircuiting { |
---|
[c71b256] | 122 | DeletedExpr * delExpr = nullptr; |
---|
| 123 | void previsit( DeletedExpr * expr ) { |
---|
| 124 | if ( delExpr ) visit_children = false; |
---|
| 125 | else delExpr = expr; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void previsit( Expression * ) { |
---|
| 129 | if ( delExpr ) visit_children = false; |
---|
| 130 | } |
---|
| 131 | }; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | DeletedExpr * findDeletedExpr( Expression * expr ) { |
---|
[99d4584] | 135 | PassVisitor<DeleteFinder_old> finder; |
---|
[c71b256] | 136 | expr->accept( finder ); |
---|
| 137 | return finder.pass.delExpr; |
---|
[d9a0e76] | 138 | } |
---|
[a32b204] | 139 | |
---|
| 140 | namespace { |
---|
[99d4584] | 141 | struct StripCasts_old { |
---|
[cdb990a] | 142 | Expression * postmutate( CastExpr * castExpr ) { |
---|
| 143 | if ( castExpr->isGenerated && ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, SymTab::Indexer() ) ) { |
---|
| 144 | // generated cast is to the same type as its argument, so it's unnecessary -- remove it |
---|
| 145 | Expression * expr = castExpr->arg; |
---|
| 146 | castExpr->arg = nullptr; |
---|
| 147 | std::swap( expr->env, castExpr->env ); |
---|
| 148 | return expr; |
---|
| 149 | } |
---|
| 150 | return castExpr; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | static void strip( Expression *& expr ) { |
---|
[99d4584] | 154 | PassVisitor<StripCasts_old> stripper; |
---|
[cdb990a] | 155 | expr = expr->acceptMutator( stripper ); |
---|
| 156 | } |
---|
| 157 | }; |
---|
| 158 | |
---|
[5170d95] | 159 | void finishExpr( Expression *& expr, const TypeEnvironment & env, TypeSubstitution * oldenv = nullptr ) { |
---|
[7664fad] | 160 | expr->env = oldenv ? oldenv->clone() : new TypeSubstitution; |
---|
[cdb990a] | 161 | env.makeSubstitution( *expr->env ); |
---|
[99d4584] | 162 | StripCasts_old::strip( expr ); // remove unnecessary casts that may be buried in an expression |
---|
[a32b204] | 163 | } |
---|
[0a22cda] | 164 | |
---|
| 165 | void removeExtraneousCast( Expression *& expr, const SymTab::Indexer & indexer ) { |
---|
| 166 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
[b7d92b96] | 167 | if ( typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) { |
---|
[0a22cda] | 168 | // cast is to the same type as its argument, so it's unnecessary -- remove it |
---|
| 169 | expr = castExpr->arg; |
---|
| 170 | castExpr->arg = nullptr; |
---|
| 171 | std::swap( expr->env, castExpr->env ); |
---|
| 172 | delete castExpr; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
[db4ecc5] | 176 | } // namespace |
---|
[a32b204] | 177 | |
---|
[8f98b78] | 178 | namespace { |
---|
[59cf83b] | 179 | void findUnfinishedKindExpression(Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{} ) { |
---|
[c71b256] | 180 | assertf( untyped, "expected a non-null expression." ); |
---|
[6d6e829] | 181 | |
---|
| 182 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver |
---|
| 183 | static unsigned recursion_level = 0; |
---|
| 184 | |
---|
| 185 | ++recursion_level; |
---|
[8587878e] | 186 | TypeEnvironment env; |
---|
| 187 | AlternativeFinder finder( indexer, env ); |
---|
[6d6e829] | 188 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); |
---|
| 189 | --recursion_level; |
---|
[c71b256] | 190 | |
---|
| 191 | #if 0 |
---|
| 192 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 193 | std::cerr << "untyped expr is "; |
---|
| 194 | untyped->print( std::cerr ); |
---|
| 195 | std::cerr << std::endl << "alternatives are:"; |
---|
| 196 | for ( const Alternative & alt : finder.get_alternatives() ) { |
---|
| 197 | alt.print( std::cerr ); |
---|
| 198 | } // for |
---|
| 199 | } // if |
---|
| 200 | #endif |
---|
[8587878e] | 201 | |
---|
[6d6e829] | 202 | // produce filtered list of alternatives |
---|
[8587878e] | 203 | AltList candidates; |
---|
| 204 | for ( Alternative & alt : finder.get_alternatives() ) { |
---|
[c71b256] | 205 | if ( pred( alt ) ) { |
---|
[8587878e] | 206 | candidates.push_back( std::move( alt ) ); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
[6d6e829] | 210 | // produce invalid error if no candidates |
---|
| 211 | if ( candidates.empty() ) { |
---|
[a16764a6] | 212 | SemanticError( untyped, toString( "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""), "expression: ") ); |
---|
[6d6e829] | 213 | } |
---|
| 214 | |
---|
| 215 | // search for cheapest candidate |
---|
| 216 | AltList winners; |
---|
| 217 | bool seen_undeleted = false; |
---|
| 218 | for ( unsigned i = 0; i < candidates.size(); ++i ) { |
---|
| 219 | int c = winners.empty() ? -1 : candidates[i].cost.compare( winners.front().cost ); |
---|
| 220 | |
---|
| 221 | if ( c > 0 ) continue; // skip more expensive than winner |
---|
| 222 | |
---|
| 223 | if ( c < 0 ) { |
---|
| 224 | // reset on new cheapest |
---|
| 225 | seen_undeleted = ! findDeletedExpr( candidates[i].expr ); |
---|
| 226 | winners.clear(); |
---|
| 227 | } else /* if ( c == 0 ) */ { |
---|
| 228 | if ( findDeletedExpr( candidates[i].expr ) ) { |
---|
| 229 | // skip deleted expression if already seen one equivalent-cost not |
---|
| 230 | if ( seen_undeleted ) continue; |
---|
| 231 | } else if ( ! seen_undeleted ) { |
---|
| 232 | // replace list of equivalent-cost deleted expressions with one non-deleted |
---|
| 233 | winners.clear(); |
---|
| 234 | seen_undeleted = true; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
[2fd9f24] | 238 | winners.emplace_back( std::move( candidates[i] ) ); |
---|
[6d6e829] | 239 | } |
---|
| 240 | |
---|
| 241 | // promote alternative.cvtCost to .cost |
---|
| 242 | // xxx - I don't know why this is done, but I'm keeping the behaviour from findMinCost |
---|
| 243 | for ( Alternative& winner : winners ) { |
---|
| 244 | winner.cost = winner.cvtCost; |
---|
| 245 | } |
---|
[cde3891] | 246 | |
---|
[6d6e829] | 247 | // produce ambiguous errors, if applicable |
---|
| 248 | if ( winners.size() != 1 ) { |
---|
[8587878e] | 249 | std::ostringstream stream; |
---|
[c71b256] | 250 | stream << "Cannot choose between " << winners.size() << " alternatives for " << kindStr << (kindStr != "" ? " " : "") << "expression\n"; |
---|
[8587878e] | 251 | untyped->print( stream ); |
---|
[93401f8] | 252 | stream << " Alternatives are:\n"; |
---|
[8587878e] | 253 | printAlts( winners, stream, 1 ); |
---|
[a16764a6] | 254 | SemanticError( untyped->location, stream.str() ); |
---|
[8587878e] | 255 | } |
---|
| 256 | |
---|
[6d6e829] | 257 | // single selected choice |
---|
| 258 | Alternative& choice = winners.front(); |
---|
| 259 | |
---|
| 260 | // fail on only expression deleted |
---|
| 261 | if ( ! seen_undeleted ) { |
---|
[2a08c25] | 262 | SemanticError( untyped->location, choice.expr, "Unique best alternative includes deleted identifier in " ); |
---|
[c71b256] | 263 | } |
---|
[6d6e829] | 264 | |
---|
| 265 | // xxx - check for ambiguous expressions |
---|
[cde3891] | 266 | |
---|
[6d6e829] | 267 | // output selected choice |
---|
[c71b256] | 268 | alt = std::move( choice ); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /// resolve `untyped` to the expression whose alternative satisfies `pred` with the lowest cost; kindStr is used for providing better error messages |
---|
[59cf83b] | 272 | void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{}) { |
---|
[c71b256] | 273 | if ( ! untyped ) return; |
---|
| 274 | Alternative choice; |
---|
[59cf83b] | 275 | findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, mode ); |
---|
[c71b256] | 276 | finishExpr( choice.expr, choice.env, untyped->env ); |
---|
[8587878e] | 277 | delete untyped; |
---|
[c71b256] | 278 | untyped = choice.expr; |
---|
| 279 | choice.expr = nullptr; |
---|
[8587878e] | 280 | } |
---|
| 281 | |
---|
[c71b256] | 282 | bool standardAlternativeFilter( const Alternative & ) { |
---|
| 283 | // currently don't need to filter, under normal circumstances. |
---|
| 284 | // in the future, this may be useful for removing deleted expressions |
---|
| 285 | return true; |
---|
| 286 | } |
---|
| 287 | } // namespace |
---|
| 288 | |
---|
| 289 | // used in resolveTypeof |
---|
[5170d95] | 290 | Expression * resolveInVoidContext( Expression * expr, const SymTab::Indexer & indexer ) { |
---|
[c71b256] | 291 | TypeEnvironment env; |
---|
| 292 | return resolveInVoidContext( expr, indexer, env ); |
---|
| 293 | } |
---|
| 294 | |
---|
[5170d95] | 295 | Expression * resolveInVoidContext( Expression * expr, const SymTab::Indexer & indexer, TypeEnvironment & env ) { |
---|
[c71b256] | 296 | // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0 |
---|
| 297 | // interpretations, an exception has already been thrown. |
---|
| 298 | assertf( expr, "expected a non-null expression." ); |
---|
| 299 | |
---|
[5170d95] | 300 | CastExpr * untyped = new CastExpr( expr ); // cast to void |
---|
| 301 | untyped->location = expr->location; |
---|
[c71b256] | 302 | |
---|
| 303 | // set up and resolve expression cast to void |
---|
| 304 | Alternative choice; |
---|
[5170d95] | 305 | findUnfinishedKindExpression( untyped, choice, indexer, "", standardAlternativeFilter, ResolvMode::withAdjustment() ); |
---|
[c71b256] | 306 | CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( choice.expr ); |
---|
[5170d95] | 307 | assert( castExpr ); |
---|
[c71b256] | 308 | env = std::move( choice.env ); |
---|
| 309 | |
---|
| 310 | // clean up resolved expression |
---|
| 311 | Expression * ret = castExpr->arg; |
---|
| 312 | castExpr->arg = nullptr; |
---|
| 313 | |
---|
| 314 | // unlink the arg so that it isn't deleted twice at the end of the program |
---|
[5170d95] | 315 | untyped->arg = nullptr; |
---|
[c71b256] | 316 | return ret; |
---|
| 317 | } |
---|
| 318 | |
---|
[5170d95] | 319 | void findVoidExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
[c71b256] | 320 | resetTyVarRenaming(); |
---|
| 321 | TypeEnvironment env; |
---|
| 322 | Expression * newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
| 323 | finishExpr( newExpr, env, untyped->env ); |
---|
| 324 | delete untyped; |
---|
| 325 | untyped = newExpr; |
---|
| 326 | } |
---|
| 327 | |
---|
[5170d95] | 328 | void findSingleExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
[c71b256] | 329 | findKindExpression( untyped, indexer, "", standardAlternativeFilter ); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) { |
---|
| 333 | assert( untyped && type ); |
---|
[2a08c25] | 334 | // transfer location to generated cast for error purposes |
---|
| 335 | CodeLocation location = untyped->location; |
---|
[c71b256] | 336 | untyped = new CastExpr( untyped, type ); |
---|
[2a08c25] | 337 | untyped->location = location; |
---|
[c71b256] | 338 | findSingleExpression( untyped, indexer ); |
---|
| 339 | removeExtraneousCast( untyped, indexer ); |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | namespace { |
---|
| 343 | bool isIntegralType( const Alternative & alt ) { |
---|
| 344 | Type * type = alt.expr->result; |
---|
[a32b204] | 345 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 346 | return true; |
---|
[5170d95] | 347 | } else if ( BasicType * bt = dynamic_cast< BasicType * >( type ) ) { |
---|
[a32b204] | 348 | return bt->isInteger(); |
---|
[89e6ffc] | 349 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { |
---|
| 350 | return true; |
---|
[a32b204] | 351 | } else { |
---|
| 352 | return false; |
---|
| 353 | } // if |
---|
| 354 | } |
---|
[71f4e4f] | 355 | |
---|
[5170d95] | 356 | void findIntegralExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
[8587878e] | 357 | findKindExpression( untyped, indexer, "condition", isIntegralType ); |
---|
[a32b204] | 358 | } |
---|
| 359 | } |
---|
[71f4e4f] | 360 | |
---|
[2a6292d] | 361 | |
---|
| 362 | bool isStructOrUnion( const Alternative & alt ) { |
---|
| 363 | Type * t = alt.expr->result->stripReferences(); |
---|
| 364 | return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | void resolveWithExprs( std::list< Declaration * > & translationUnit ) { |
---|
| 368 | PassVisitor<ResolveWithExprs> resolver; |
---|
| 369 | acceptAll( translationUnit, resolver ); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | void ResolveWithExprs::resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ) { |
---|
| 373 | for ( Expression *& expr : withExprs ) { |
---|
| 374 | // only struct- and union-typed expressions are viable candidates |
---|
| 375 | findKindExpression( expr, indexer, "with statement", isStructOrUnion ); |
---|
| 376 | |
---|
| 377 | // if with expression might be impure, create a temporary so that it is evaluated once |
---|
| 378 | if ( Tuples::maybeImpure( expr ) ) { |
---|
| 379 | static UniqueName tmpNamer( "_with_tmp_" ); |
---|
| 380 | ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) ); |
---|
| 381 | expr = new VariableExpr( tmp ); |
---|
| 382 | newStmts.push_back( new DeclStmt( tmp ) ); |
---|
| 383 | if ( InitTweak::isConstructable( tmp->type ) ) { |
---|
| 384 | // generate ctor/dtor and resolve them |
---|
| 385 | tmp->init = InitTweak::genCtorInit( tmp ); |
---|
| 386 | tmp->accept( *visitor ); |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | void ResolveWithExprs::previsit( WithStmt * withStmt ) { |
---|
| 393 | resolveWithExprs( withStmt->exprs, stmtsToAddBefore ); |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | void ResolveWithExprs::previsit( FunctionDecl * functionDecl ) { |
---|
| 397 | { |
---|
| 398 | // resolve with-exprs with parameters in scope and add any newly generated declarations to the |
---|
| 399 | // front of the function body. |
---|
| 400 | auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this](){ indexer.leaveScope(); } ); |
---|
| 401 | indexer.addFunctionType( functionDecl->type ); |
---|
| 402 | std::list< Statement * > newStmts; |
---|
| 403 | resolveWithExprs( functionDecl->withExprs, newStmts ); |
---|
| 404 | if ( functionDecl->statements ) { |
---|
| 405 | functionDecl->statements->kids.splice( functionDecl->statements->kids.begin(), newStmts ); |
---|
| 406 | } else { |
---|
| 407 | assertf( functionDecl->withExprs.empty() && newStmts.empty(), "Function %s without a body has with-clause and/or generated with declarations.", functionDecl->name.c_str() ); |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
[d76c588] | 412 | void Resolver_old::previsit( ObjectDecl * objectDecl ) { |
---|
[4864a73] | 413 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that |
---|
| 414 | // class-variable initContext is changed multiple time because the LHS is analysed twice. |
---|
| 415 | // The second analysis changes initContext because of a function type can contain object |
---|
| 416 | // declarations in the return and parameter types. So each value of initContext is |
---|
[6d6e829] | 417 | // retained, so the type on the first analysis is preserved and used for selecting the RHS. |
---|
[a4ca48c] | 418 | GuardValue( currentObject ); |
---|
[e4d829b] | 419 | currentObject = CurrentObject( objectDecl->get_type() ); |
---|
| 420 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
[a436947] | 421 | // enumerator initializers should not use the enum type to initialize, since |
---|
| 422 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
[e4d829b] | 423 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
[a436947] | 424 | } |
---|
[bfbf97f] | 425 | } |
---|
| 426 | |
---|
[40e636a] | 427 | template< typename PtrType > |
---|
[d76c588] | 428 | void Resolver_old::handlePtrType( PtrType * type ) { |
---|
[40e636a] | 429 | if ( type->get_dimension() ) { |
---|
[2bfc6b2] | 430 | findSingleExpression( type->dimension, Validate::SizeType->clone(), indexer ); |
---|
[d1d17f5] | 431 | } |
---|
[40e636a] | 432 | } |
---|
| 433 | |
---|
[d76c588] | 434 | void Resolver_old::previsit( ArrayType * at ) { |
---|
[40e636a] | 435 | handlePtrType( at ); |
---|
[a32b204] | 436 | } |
---|
[94b4364] | 437 | |
---|
[d76c588] | 438 | void Resolver_old::previsit( PointerType * pt ) { |
---|
[40e636a] | 439 | handlePtrType( pt ); |
---|
| 440 | } |
---|
| 441 | |
---|
[d76c588] | 442 | void Resolver_old::previsit( FunctionDecl * functionDecl ) { |
---|
[d9a0e76] | 443 | #if 0 |
---|
[a4ca48c] | 444 | std::cerr << "resolver visiting functiondecl "; |
---|
| 445 | functionDecl->print( std::cerr ); |
---|
| 446 | std::cerr << std::endl; |
---|
[d9a0e76] | 447 | #endif |
---|
[a4ca48c] | 448 | GuardValue( functionReturn ); |
---|
[60914351] | 449 | functionReturn = ResolvExpr::extractResultType( functionDecl->type ); |
---|
[a4ca48c] | 450 | } |
---|
[88d1066] | 451 | |
---|
[d76c588] | 452 | void Resolver_old::postvisit( FunctionDecl * functionDecl ) { |
---|
[4864a73] | 453 | // default value expressions have an environment which shouldn't be there and trips up |
---|
[6d6e829] | 454 | // later passes. |
---|
[cde3891] | 455 | // xxx - it might be necessary to somehow keep the information from this environment, but I |
---|
[6d6e829] | 456 | // can't currently see how it's useful. |
---|
[c28a038d] | 457 | for ( Declaration * d : functionDecl->type->parameters ) { |
---|
[88d1066] | 458 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { |
---|
[c28a038d] | 459 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->init ) ) { |
---|
| 460 | delete init->value->env; |
---|
| 461 | init->value->env = nullptr; |
---|
[88d1066] | 462 | } |
---|
| 463 | } |
---|
| 464 | } |
---|
[a32b204] | 465 | } |
---|
[51b7345] | 466 | |
---|
[d76c588] | 467 | void Resolver_old::previsit( EnumDecl * ) { |
---|
[a436947] | 468 | // in case we decide to allow nested enums |
---|
[a4ca48c] | 469 | GuardValue( inEnumDecl ); |
---|
[a436947] | 470 | inEnumDecl = true; |
---|
| 471 | } |
---|
| 472 | |
---|
[d76c588] | 473 | void Resolver_old::previsit( StaticAssertDecl * assertDecl ) { |
---|
[bd87b138] | 474 | findIntegralExpression( assertDecl->condition, indexer ); |
---|
| 475 | } |
---|
| 476 | |
---|
[d76c588] | 477 | void Resolver_old::previsit( ExprStmt * exprStmt ) { |
---|
[a4ca48c] | 478 | visit_children = false; |
---|
[08da53d] | 479 | assertf( exprStmt->expr, "ExprStmt has null Expression in resolver" ); |
---|
| 480 | findVoidExpression( exprStmt->expr, indexer ); |
---|
[a32b204] | 481 | } |
---|
[51b7345] | 482 | |
---|
[d76c588] | 483 | void Resolver_old::previsit( AsmExpr * asmExpr ) { |
---|
[a4ca48c] | 484 | visit_children = false; |
---|
[08da53d] | 485 | findVoidExpression( asmExpr->operand, indexer ); |
---|
[7f5566b] | 486 | if ( asmExpr->get_inout() ) { |
---|
[08da53d] | 487 | findVoidExpression( asmExpr->inout, indexer ); |
---|
[7f5566b] | 488 | } // if |
---|
| 489 | } |
---|
| 490 | |
---|
[d76c588] | 491 | void Resolver_old::previsit( AsmStmt * asmStmt ) { |
---|
[a4ca48c] | 492 | visit_children = false; |
---|
| 493 | acceptAll( asmStmt->get_input(), *visitor ); |
---|
| 494 | acceptAll( asmStmt->get_output(), *visitor ); |
---|
[7f5566b] | 495 | } |
---|
| 496 | |
---|
[d76c588] | 497 | void Resolver_old::previsit( IfStmt * ifStmt ) { |
---|
[8587878e] | 498 | findIntegralExpression( ifStmt->condition, indexer ); |
---|
[a32b204] | 499 | } |
---|
[51b7345] | 500 | |
---|
[d76c588] | 501 | void Resolver_old::previsit( WhileStmt * whileStmt ) { |
---|
[8587878e] | 502 | findIntegralExpression( whileStmt->condition, indexer ); |
---|
[a32b204] | 503 | } |
---|
[51b7345] | 504 | |
---|
[d76c588] | 505 | void Resolver_old::previsit( ForStmt * forStmt ) { |
---|
[08da53d] | 506 | if ( forStmt->condition ) { |
---|
[8587878e] | 507 | findIntegralExpression( forStmt->condition, indexer ); |
---|
[a32b204] | 508 | } // if |
---|
[71f4e4f] | 509 | |
---|
[08da53d] | 510 | if ( forStmt->increment ) { |
---|
| 511 | findVoidExpression( forStmt->increment, indexer ); |
---|
[a32b204] | 512 | } // if |
---|
| 513 | } |
---|
[51b7345] | 514 | |
---|
[d76c588] | 515 | void Resolver_old::previsit( SwitchStmt * switchStmt ) { |
---|
[a4ca48c] | 516 | GuardValue( currentObject ); |
---|
[08da53d] | 517 | findIntegralExpression( switchStmt->condition, indexer ); |
---|
[71f4e4f] | 518 | |
---|
[08da53d] | 519 | currentObject = CurrentObject( switchStmt->condition->result ); |
---|
[a32b204] | 520 | } |
---|
[51b7345] | 521 | |
---|
[d76c588] | 522 | void Resolver_old::previsit( CaseStmt * caseStmt ) { |
---|
[cdb990a] | 523 | if ( caseStmt->condition ) { |
---|
[e4d829b] | 524 | std::list< InitAlternative > initAlts = currentObject.getOptions(); |
---|
| 525 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); |
---|
[08da53d] | 526 | // must remove cast from case statement because RangeExpr cannot be cast. |
---|
| 527 | Expression * newExpr = new CastExpr( caseStmt->condition, initAlts.front().type->clone() ); |
---|
| 528 | findSingleExpression( newExpr, indexer ); |
---|
[cdb990a] | 529 | // case condition cannot have a cast in C, so it must be removed, regardless of whether it performs a conversion. |
---|
| 530 | // Ideally we would perform the conversion internally here. |
---|
| 531 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( newExpr ) ) { |
---|
| 532 | newExpr = castExpr->arg; |
---|
| 533 | castExpr->arg = nullptr; |
---|
| 534 | std::swap( newExpr->env, castExpr->env ); |
---|
| 535 | delete castExpr; |
---|
| 536 | } |
---|
| 537 | caseStmt->condition = newExpr; |
---|
[32b8144] | 538 | } |
---|
[a32b204] | 539 | } |
---|
[51b7345] | 540 | |
---|
[d76c588] | 541 | void Resolver_old::previsit( BranchStmt * branchStmt ) { |
---|
[a4ca48c] | 542 | visit_children = false; |
---|
[de62360d] | 543 | // must resolve the argument for a computed goto |
---|
| 544 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
[08da53d] | 545 | if ( branchStmt->computedTarget ) { |
---|
| 546 | // computed goto argument is void * |
---|
| 547 | findSingleExpression( branchStmt->computedTarget, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), indexer ); |
---|
[de62360d] | 548 | } // if |
---|
| 549 | } // if |
---|
| 550 | } |
---|
| 551 | |
---|
[d76c588] | 552 | void Resolver_old::previsit( ReturnStmt * returnStmt ) { |
---|
[a4ca48c] | 553 | visit_children = false; |
---|
[08da53d] | 554 | if ( returnStmt->expr ) { |
---|
| 555 | findSingleExpression( returnStmt->expr, functionReturn->clone(), indexer ); |
---|
[a32b204] | 556 | } // if |
---|
| 557 | } |
---|
[51b7345] | 558 | |
---|
[d76c588] | 559 | void Resolver_old::previsit( ThrowStmt * throwStmt ) { |
---|
[a4ca48c] | 560 | visit_children = false; |
---|
[cbce272] | 561 | // TODO: Replace *exception type with &exception type. |
---|
[307a732] | 562 | if ( throwStmt->get_expr() ) { |
---|
[cbce272] | 563 | StructDecl * exception_decl = |
---|
[36982fc] | 564 | indexer.lookupStruct( "__cfaabi_ehm__base_exception_t" ); |
---|
[cbce272] | 565 | assert( exception_decl ); |
---|
[08da53d] | 566 | Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, exception_decl ) ); |
---|
| 567 | findSingleExpression( throwStmt->expr, exceptType, indexer ); |
---|
[307a732] | 568 | } |
---|
| 569 | } |
---|
| 570 | |
---|
[d76c588] | 571 | void Resolver_old::previsit( CatchStmt * catchStmt ) { |
---|
[08da53d] | 572 | if ( catchStmt->cond ) { |
---|
[e15853c] | 573 | findSingleExpression( catchStmt->cond, new BasicType( noQualifiers, BasicType::Bool ), indexer ); |
---|
[cbce272] | 574 | } |
---|
| 575 | } |
---|
| 576 | |
---|
[1dcd9554] | 577 | template< typename iterator_t > |
---|
| 578 | inline bool advance_to_mutex( iterator_t & it, const iterator_t & end ) { |
---|
| 579 | while( it != end && !(*it)->get_type()->get_mutex() ) { |
---|
| 580 | it++; |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | return it != end; |
---|
| 584 | } |
---|
| 585 | |
---|
[d76c588] | 586 | void Resolver_old::previsit( WaitForStmt * stmt ) { |
---|
[8f98b78] | 587 | visit_children = false; |
---|
[1dcd9554] | 588 | |
---|
| 589 | // Resolve all clauses first |
---|
| 590 | for( auto& clause : stmt->clauses ) { |
---|
| 591 | |
---|
| 592 | TypeEnvironment env; |
---|
[8f98b78] | 593 | AlternativeFinder funcFinder( indexer, env ); |
---|
[1dcd9554] | 594 | |
---|
| 595 | // Find all alternatives for a function in canonical form |
---|
| 596 | funcFinder.findWithAdjustment( clause.target.function ); |
---|
| 597 | |
---|
| 598 | if ( funcFinder.get_alternatives().empty() ) { |
---|
| 599 | stringstream ss; |
---|
| 600 | ss << "Use of undeclared indentifier '"; |
---|
| 601 | ss << strict_dynamic_cast<NameExpr*>( clause.target.function )->name; |
---|
| 602 | ss << "' in call to waitfor"; |
---|
[a16764a6] | 603 | SemanticError( stmt->location, ss.str() ); |
---|
[1dcd9554] | 604 | } |
---|
| 605 | |
---|
[b9f383f] | 606 | if(clause.target.arguments.empty()) { |
---|
| 607 | SemanticError( stmt->location, "Waitfor clause must have at least one mutex parameter"); |
---|
| 608 | } |
---|
| 609 | |
---|
[1dcd9554] | 610 | // Find all alternatives for all arguments in canonical form |
---|
[bd4f2e9] | 611 | std::vector< AlternativeFinder > argAlternatives; |
---|
[1dcd9554] | 612 | funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) ); |
---|
| 613 | |
---|
| 614 | // List all combinations of arguments |
---|
[bd4f2e9] | 615 | std::vector< AltList > possibilities; |
---|
[1dcd9554] | 616 | combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); |
---|
| 617 | |
---|
| 618 | AltList func_candidates; |
---|
| 619 | std::vector< AltList > args_candidates; |
---|
| 620 | |
---|
| 621 | // For every possible function : |
---|
| 622 | // try matching the arguments to the parameters |
---|
| 623 | // not the other way around because we have more arguments than parameters |
---|
[a16764a6] | 624 | SemanticErrorException errors; |
---|
[1dcd9554] | 625 | for ( Alternative & func : funcFinder.get_alternatives() ) { |
---|
| 626 | try { |
---|
| 627 | PointerType * pointer = dynamic_cast< PointerType* >( func.expr->get_result()->stripReferences() ); |
---|
| 628 | if( !pointer ) { |
---|
[a16764a6] | 629 | SemanticError( func.expr->get_result(), "candidate not viable: not a pointer type\n" ); |
---|
[1dcd9554] | 630 | } |
---|
| 631 | |
---|
| 632 | FunctionType * function = dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
| 633 | if( !function ) { |
---|
[a16764a6] | 634 | SemanticError( pointer->get_base(), "candidate not viable: not a function type\n" ); |
---|
[1dcd9554] | 635 | } |
---|
| 636 | |
---|
| 637 | |
---|
| 638 | { |
---|
| 639 | auto param = function->parameters.begin(); |
---|
| 640 | auto param_end = function->parameters.end(); |
---|
| 641 | |
---|
| 642 | if( !advance_to_mutex( param, param_end ) ) { |
---|
[a16764a6] | 643 | SemanticError(function, "candidate function not viable: no mutex parameters\n"); |
---|
[1dcd9554] | 644 | } |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | Alternative newFunc( func ); |
---|
| 648 | // Strip reference from function |
---|
[a181494] | 649 | referenceToRvalueConversion( newFunc.expr, newFunc.cost ); |
---|
[1dcd9554] | 650 | |
---|
| 651 | // For all the set of arguments we have try to match it with the parameter of the current function alternative |
---|
| 652 | for ( auto & argsList : possibilities ) { |
---|
| 653 | |
---|
| 654 | try { |
---|
| 655 | // Declare data structures need for resolution |
---|
| 656 | OpenVarSet openVars; |
---|
| 657 | AssertionSet resultNeed, resultHave; |
---|
[6f326b1] | 658 | TypeEnvironment resultEnv( func.env ); |
---|
| 659 | makeUnifiableVars( function, openVars, resultNeed ); |
---|
| 660 | // add all type variables as open variables now so that those not used in the parameter |
---|
| 661 | // list are still considered open. |
---|
| 662 | resultEnv.add( function->forall ); |
---|
[1dcd9554] | 663 | |
---|
| 664 | // Load type variables from arguemnts into one shared space |
---|
| 665 | simpleCombineEnvironments( argsList.begin(), argsList.end(), resultEnv ); |
---|
| 666 | |
---|
| 667 | // Make sure we don't widen any existing bindings |
---|
[d286cf68] | 668 | resultEnv.forbidWidening(); |
---|
[c5283ba] | 669 | |
---|
[1dcd9554] | 670 | // Find any unbound type variables |
---|
| 671 | resultEnv.extractOpenVars( openVars ); |
---|
| 672 | |
---|
| 673 | auto param = function->parameters.begin(); |
---|
| 674 | auto param_end = function->parameters.end(); |
---|
| 675 | |
---|
[c5283ba] | 676 | int n_mutex_param = 0; |
---|
[b9f383f] | 677 | |
---|
[1dcd9554] | 678 | // For every arguments of its set, check if it matches one of the parameter |
---|
| 679 | // The order is important |
---|
| 680 | for( auto & arg : argsList ) { |
---|
| 681 | |
---|
| 682 | // Ignore non-mutex arguments |
---|
| 683 | if( !advance_to_mutex( param, param_end ) ) { |
---|
| 684 | // We ran out of parameters but still have arguments |
---|
| 685 | // this function doesn't match |
---|
[c5283ba] | 686 | SemanticError( function, toString("candidate function not viable: too many mutex arguments, expected ", n_mutex_param, "\n" )); |
---|
[1dcd9554] | 687 | } |
---|
| 688 | |
---|
[c5283ba] | 689 | n_mutex_param++; |
---|
[b9f383f] | 690 | |
---|
[1dcd9554] | 691 | // Check if the argument matches the parameter type in the current scope |
---|
[b9f383f] | 692 | if( ! unify( arg.expr->get_result(), (*param)->get_type(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) { |
---|
[1dcd9554] | 693 | // Type doesn't match |
---|
| 694 | stringstream ss; |
---|
| 695 | ss << "candidate function not viable: no known convertion from '"; |
---|
| 696 | (*param)->get_type()->print( ss ); |
---|
[b9f383f] | 697 | ss << "' to '"; |
---|
| 698 | arg.expr->get_result()->print( ss ); |
---|
[5248789] | 699 | ss << "' with env '"; |
---|
| 700 | resultEnv.print(ss); |
---|
[1dcd9554] | 701 | ss << "'\n"; |
---|
[a16764a6] | 702 | SemanticError( function, ss.str() ); |
---|
[1dcd9554] | 703 | } |
---|
| 704 | |
---|
| 705 | param++; |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | // All arguments match ! |
---|
| 709 | |
---|
| 710 | // Check if parameters are missing |
---|
| 711 | if( advance_to_mutex( param, param_end ) ) { |
---|
[c5283ba] | 712 | do { |
---|
| 713 | n_mutex_param++; |
---|
| 714 | param++; |
---|
| 715 | } while( advance_to_mutex( param, param_end ) ); |
---|
| 716 | |
---|
[1dcd9554] | 717 | // We ran out of arguments but still have parameters left |
---|
| 718 | // this function doesn't match |
---|
[c5283ba] | 719 | SemanticError( function, toString("candidate function not viable: too few mutex arguments, expected ", n_mutex_param, "\n" )); |
---|
[1dcd9554] | 720 | } |
---|
| 721 | |
---|
| 722 | // All parameters match ! |
---|
| 723 | |
---|
| 724 | // Finish the expressions to tie in the proper environments |
---|
| 725 | finishExpr( newFunc.expr, resultEnv ); |
---|
| 726 | for( Alternative & alt : argsList ) { |
---|
| 727 | finishExpr( alt.expr, resultEnv ); |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | // This is a match store it and save it for later |
---|
| 731 | func_candidates.push_back( newFunc ); |
---|
| 732 | args_candidates.push_back( argsList ); |
---|
| 733 | |
---|
| 734 | } |
---|
[5170d95] | 735 | catch( SemanticErrorException & e ) { |
---|
[1dcd9554] | 736 | errors.append( e ); |
---|
| 737 | } |
---|
| 738 | } |
---|
| 739 | } |
---|
[5170d95] | 740 | catch( SemanticErrorException & e ) { |
---|
[1dcd9554] | 741 | errors.append( e ); |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | // Make sure we got the right number of arguments |
---|
[a16764a6] | 746 | if( func_candidates.empty() ) { SemanticErrorException top( stmt->location, "No alternatives for function in call to waitfor" ); top.append( errors ); throw top; } |
---|
| 747 | if( args_candidates.empty() ) { SemanticErrorException top( stmt->location, "No alternatives for arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
| 748 | if( func_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous function in call to waitfor" ); top.append( errors ); throw top; } |
---|
| 749 | if( args_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
[c71b256] | 750 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used. |
---|
[1dcd9554] | 751 | |
---|
| 752 | // Swap the results from the alternative with the unresolved values. |
---|
| 753 | // Alternatives will handle deletion on destruction |
---|
| 754 | std::swap( clause.target.function, func_candidates.front().expr ); |
---|
| 755 | for( auto arg_pair : group_iterate( clause.target.arguments, args_candidates.front() ) ) { |
---|
| 756 | std::swap ( std::get<0>( arg_pair), std::get<1>( arg_pair).expr ); |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | // Resolve the conditions as if it were an IfStmt |
---|
| 760 | // Resolve the statments normally |
---|
[08da53d] | 761 | findSingleExpression( clause.condition, this->indexer ); |
---|
[8f98b78] | 762 | clause.statement->accept( *visitor ); |
---|
[1dcd9554] | 763 | } |
---|
| 764 | |
---|
| 765 | |
---|
| 766 | if( stmt->timeout.statement ) { |
---|
| 767 | // Resolve the timeout as an size_t for now |
---|
| 768 | // Resolve the conditions as if it were an IfStmt |
---|
| 769 | // Resolve the statments normally |
---|
[08da53d] | 770 | findSingleExpression( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer ); |
---|
| 771 | findSingleExpression( stmt->timeout.condition, this->indexer ); |
---|
[8f98b78] | 772 | stmt->timeout.statement->accept( *visitor ); |
---|
[1dcd9554] | 773 | } |
---|
| 774 | |
---|
| 775 | if( stmt->orelse.statement ) { |
---|
| 776 | // Resolve the conditions as if it were an IfStmt |
---|
| 777 | // Resolve the statments normally |
---|
[08da53d] | 778 | findSingleExpression( stmt->orelse.condition, this->indexer ); |
---|
[8f98b78] | 779 | stmt->orelse.statement->accept( *visitor ); |
---|
[1dcd9554] | 780 | } |
---|
| 781 | } |
---|
| 782 | |
---|
[60aaa51d] | 783 | bool isCharType( Type * t ) { |
---|
[b5c5684] | 784 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
[71f4e4f] | 785 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
[b5c5684] | 786 | bt->get_kind() == BasicType::UnsignedChar; |
---|
| 787 | } |
---|
| 788 | return false; |
---|
| 789 | } |
---|
| 790 | |
---|
[d76c588] | 791 | void Resolver_old::previsit( SingleInit * singleInit ) { |
---|
[a4ca48c] | 792 | visit_children = false; |
---|
[62423350] | 793 | // resolve initialization using the possibilities as determined by the currentObject cursor |
---|
[0a22cda] | 794 | Expression * newExpr = new UntypedInitExpr( singleInit->value, currentObject.getOptions() ); |
---|
[08da53d] | 795 | findSingleExpression( newExpr, indexer ); |
---|
[e3e16bc] | 796 | InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); |
---|
[62423350] | 797 | |
---|
| 798 | // move cursor to the object that is actually initialized |
---|
[e4d829b] | 799 | currentObject.setNext( initExpr->get_designation() ); |
---|
[62423350] | 800 | |
---|
| 801 | // discard InitExpr wrapper and retain relevant pieces |
---|
[08da53d] | 802 | newExpr = initExpr->expr; |
---|
| 803 | initExpr->expr = nullptr; |
---|
| 804 | std::swap( initExpr->env, newExpr->env ); |
---|
[cde3891] | 805 | // InitExpr may have inferParams in the case where the expression specializes a function |
---|
| 806 | // pointer, and newExpr may already have inferParams of its own, so a simple swap is not |
---|
[6d6e829] | 807 | // sufficient. |
---|
[cdb990a] | 808 | newExpr->spliceInferParams( initExpr ); |
---|
[e4d829b] | 809 | delete initExpr; |
---|
| 810 | |
---|
[cde3891] | 811 | // get the actual object's type (may not exactly match what comes back from the resolver |
---|
[6d6e829] | 812 | // due to conversions) |
---|
[62423350] | 813 | Type * initContext = currentObject.getCurrentType(); |
---|
| 814 | |
---|
[0a22cda] | 815 | removeExtraneousCast( newExpr, indexer ); |
---|
| 816 | |
---|
[62423350] | 817 | // check if actual object's type is char[] |
---|
| 818 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
| 819 | if ( isCharType( at->get_base() ) ) { |
---|
| 820 | // check if the resolved type is char * |
---|
| 821 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
| 822 | if ( isCharType( pt->get_base() ) ) { |
---|
[5170d95] | 823 | if ( CastExpr * ce = dynamic_cast< CastExpr * >( newExpr ) ) { |
---|
[cde3891] | 824 | // strip cast if we're initializing a char[] with a char *, |
---|
[6d6e829] | 825 | // e.g. char x[] = "hello"; |
---|
[0a22cda] | 826 | newExpr = ce->get_arg(); |
---|
| 827 | ce->set_arg( nullptr ); |
---|
| 828 | std::swap( ce->env, newExpr->env ); |
---|
| 829 | delete ce; |
---|
| 830 | } |
---|
[62423350] | 831 | } |
---|
| 832 | } |
---|
| 833 | } |
---|
| 834 | } |
---|
[94b4364] | 835 | |
---|
[62423350] | 836 | // set initializer expr to resolved express |
---|
[0a22cda] | 837 | singleInit->value = newExpr; |
---|
[62423350] | 838 | |
---|
| 839 | // move cursor to next object in preparation for next initializer |
---|
| 840 | currentObject.increment(); |
---|
| 841 | } |
---|
[94b4364] | 842 | |
---|
[d76c588] | 843 | void Resolver_old::previsit( ListInit * listInit ) { |
---|
[a4ca48c] | 844 | visit_children = false; |
---|
[62423350] | 845 | // move cursor into brace-enclosed initializer-list |
---|
[e4d829b] | 846 | currentObject.enterListInit(); |
---|
[cde3891] | 847 | // xxx - fix this so that the list isn't copied, iterator should be used to change current |
---|
[6d6e829] | 848 | // element |
---|
[e4d829b] | 849 | std::list<Designation *> newDesignations; |
---|
| 850 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { |
---|
[cde3891] | 851 | // iterate designations and initializers in pairs, moving the cursor to the current |
---|
[6d6e829] | 852 | // designated object and resolving the initializer against that object. |
---|
[e4d829b] | 853 | Designation * des = std::get<0>(p); |
---|
| 854 | Initializer * init = std::get<1>(p); |
---|
| 855 | newDesignations.push_back( currentObject.findNext( des ) ); |
---|
[a4ca48c] | 856 | init->accept( *visitor ); |
---|
[b5c5684] | 857 | } |
---|
[62423350] | 858 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list |
---|
[e4d829b] | 859 | listInit->get_designations() = newDesignations; // xxx - memory management |
---|
| 860 | currentObject.exitListInit(); |
---|
| 861 | |
---|
[62423350] | 862 | // xxx - this part has not be folded into CurrentObject yet |
---|
[e4d829b] | 863 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
| 864 | // Type * base = tt->get_baseType()->get_base(); |
---|
| 865 | // if ( base ) { |
---|
| 866 | // // know the implementation type, so try using that as the initContext |
---|
| 867 | // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); |
---|
| 868 | // currentObject = &tmpObj; |
---|
| 869 | // visit( listInit ); |
---|
| 870 | // } else { |
---|
| 871 | // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
| 872 | // Parent::visit( listInit ); |
---|
| 873 | // } |
---|
| 874 | // } else { |
---|
[a32b204] | 875 | } |
---|
[71f4e4f] | 876 | |
---|
[f1e012b] | 877 | // ConstructorInit - fall back on C-style initializer |
---|
[d76c588] | 878 | void Resolver_old::fallbackInit( ConstructorInit * ctorInit ) { |
---|
[f1e012b] | 879 | // could not find valid constructor, or found an intrinsic constructor |
---|
| 880 | // fall back on C-style initializer |
---|
| 881 | delete ctorInit->get_ctor(); |
---|
[6d6e829] | 882 | ctorInit->set_ctor( nullptr ); |
---|
[71a145de] | 883 | delete ctorInit->get_dtor(); |
---|
[6d6e829] | 884 | ctorInit->set_dtor( nullptr ); |
---|
[a4ca48c] | 885 | maybeAccept( ctorInit->get_init(), *visitor ); |
---|
[f1e012b] | 886 | } |
---|
| 887 | |
---|
[1d2b64f] | 888 | // needs to be callable from outside the resolver, so this is a standalone function |
---|
| 889 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { |
---|
| 890 | assert( ctorInit ); |
---|
[d76c588] | 891 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
[1d2b64f] | 892 | ctorInit->accept( resolver ); |
---|
| 893 | } |
---|
| 894 | |
---|
| 895 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { |
---|
| 896 | assert( stmtExpr ); |
---|
[d76c588] | 897 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
[1d2b64f] | 898 | stmtExpr->accept( resolver ); |
---|
[5e2c348] | 899 | stmtExpr->computeResult(); |
---|
[dd05e12] | 900 | // xxx - aggregate the environments from all statements? Possibly in AlternativeFinder instead? |
---|
[1d2b64f] | 901 | } |
---|
| 902 | |
---|
[d76c588] | 903 | void Resolver_old::previsit( ConstructorInit * ctorInit ) { |
---|
[a4ca48c] | 904 | visit_children = false; |
---|
[1ba88a0] | 905 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
[dd05e12] | 906 | maybeAccept( ctorInit->ctor, *visitor ); |
---|
| 907 | maybeAccept( ctorInit->dtor, *visitor ); |
---|
[071a31a] | 908 | |
---|
[5b2f5bb] | 909 | // found a constructor - can get rid of C-style initializer |
---|
[dd05e12] | 910 | delete ctorInit->init; |
---|
| 911 | ctorInit->init = nullptr; |
---|
[ec79847] | 912 | |
---|
| 913 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
| 914 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
| 915 | // to clean up generated code. |
---|
[dd05e12] | 916 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { |
---|
| 917 | delete ctorInit->ctor; |
---|
| 918 | ctorInit->ctor = nullptr; |
---|
[ec79847] | 919 | } |
---|
[f9cebb5] | 920 | |
---|
[dd05e12] | 921 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { |
---|
| 922 | delete ctorInit->dtor; |
---|
| 923 | ctorInit->dtor = nullptr; |
---|
[ec79847] | 924 | } |
---|
[a465caf] | 925 | |
---|
| 926 | // xxx - todo -- what about arrays? |
---|
[6d6e829] | 927 | // if ( dtor == nullptr && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
[a465caf] | 928 | // // can reduce the constructor down to a SingleInit using the |
---|
| 929 | // // second argument from the ctor call, since |
---|
| 930 | // delete ctorInit->get_ctor(); |
---|
[6d6e829] | 931 | // ctorInit->set_ctor( nullptr ); |
---|
[a465caf] | 932 | |
---|
| 933 | // Expression * arg = |
---|
| 934 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
| 935 | // } |
---|
[71f4e4f] | 936 | } |
---|
[d76c588] | 937 | |
---|
| 938 | /////////////////////////////////////////////////////////////////////////// |
---|
| 939 | // |
---|
| 940 | // *** NEW RESOLVER *** |
---|
| 941 | // |
---|
| 942 | /////////////////////////////////////////////////////////////////////////// |
---|
| 943 | |
---|
[99d4584] | 944 | namespace { |
---|
| 945 | /// Finds deleted expressions in an expression tree |
---|
| 946 | struct DeleteFinder_new final : public ast::WithShortCircuiting { |
---|
| 947 | const ast::DeletedExpr * delExpr = nullptr; |
---|
| 948 | |
---|
| 949 | void previsit( const ast::DeletedExpr * expr ) { |
---|
| 950 | if ( delExpr ) { visit_children = false; } |
---|
| 951 | else { delExpr = expr; } |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | void previsit( const ast::Expr * ) { |
---|
| 955 | if ( delExpr ) { visit_children = false; } |
---|
| 956 | } |
---|
| 957 | }; |
---|
| 958 | |
---|
| 959 | /// Check if this expression is or includes a deleted expression |
---|
| 960 | const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ) { |
---|
| 961 | ast::Pass<DeleteFinder_new> finder; |
---|
| 962 | expr->accept( finder ); |
---|
| 963 | return finder.pass.delExpr; |
---|
| 964 | } |
---|
| 965 | |
---|
[b7d92b96] | 966 | /// always-accept candidate filter |
---|
| 967 | bool anyCandidate( const Candidate & ) { return true; } |
---|
| 968 | |
---|
[99d4584] | 969 | /// Calls the CandidateFinder and finds the single best candidate |
---|
| 970 | CandidateRef findUnfinishedKindExpression( |
---|
| 971 | const ast::Expr * untyped, const ast::SymbolTable & symtab, const std::string & kind, |
---|
[b7d92b96] | 972 | std::function<bool(const Candidate &)> pred = anyCandidate, ResolvMode mode = {} |
---|
[99d4584] | 973 | ) { |
---|
| 974 | if ( ! untyped ) return nullptr; |
---|
| 975 | |
---|
| 976 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver |
---|
| 977 | static unsigned recursion_level = 0; |
---|
| 978 | |
---|
| 979 | ++recursion_level; |
---|
| 980 | ast::TypeEnvironment env; |
---|
| 981 | CandidateFinder finder{ symtab, env }; |
---|
| 982 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); |
---|
| 983 | --recursion_level; |
---|
| 984 | |
---|
| 985 | // produce a filtered list of candidates |
---|
| 986 | CandidateList candidates; |
---|
| 987 | for ( auto & cand : finder.candidates ) { |
---|
| 988 | if ( pred( *cand ) ) { candidates.emplace_back( cand ); } |
---|
| 989 | } |
---|
| 990 | |
---|
| 991 | // produce invalid error if no candidates |
---|
| 992 | if ( candidates.empty() ) { |
---|
| 993 | SemanticError( untyped, |
---|
| 994 | toString( "No reasonable alternatives for ", kind, (kind != "" ? " " : ""), |
---|
| 995 | "expression: ") ); |
---|
| 996 | } |
---|
| 997 | |
---|
| 998 | // search for cheapest candidate |
---|
| 999 | CandidateList winners; |
---|
| 1000 | bool seen_undeleted = false; |
---|
| 1001 | for ( CandidateRef & cand : candidates ) { |
---|
| 1002 | int c = winners.empty() ? -1 : cand->cost.compare( winners.front()->cost ); |
---|
| 1003 | |
---|
| 1004 | if ( c > 0 ) continue; // skip more expensive than winner |
---|
| 1005 | |
---|
| 1006 | if ( c < 0 ) { |
---|
| 1007 | // reset on new cheapest |
---|
| 1008 | seen_undeleted = ! findDeletedExpr( cand->expr ); |
---|
| 1009 | winners.clear(); |
---|
| 1010 | } else /* if ( c == 0 ) */ { |
---|
| 1011 | if ( findDeletedExpr( cand->expr ) ) { |
---|
| 1012 | // skip deleted expression if already seen one equivalent-cost not |
---|
| 1013 | if ( seen_undeleted ) continue; |
---|
| 1014 | } else if ( ! seen_undeleted ) { |
---|
| 1015 | // replace list of equivalent-cost deleted expressions with one non-deleted |
---|
| 1016 | winners.clear(); |
---|
| 1017 | seen_undeleted = true; |
---|
| 1018 | } |
---|
| 1019 | } |
---|
| 1020 | |
---|
| 1021 | winners.emplace_back( std::move( cand ) ); |
---|
| 1022 | } |
---|
| 1023 | |
---|
| 1024 | // promote candidate.cvtCost to .cost |
---|
| 1025 | for ( CandidateRef & cand : winners ) { |
---|
| 1026 | cand->cost = cand->cvtCost; |
---|
| 1027 | } |
---|
| 1028 | |
---|
| 1029 | // produce ambiguous errors, if applicable |
---|
| 1030 | if ( winners.size() != 1 ) { |
---|
| 1031 | std::ostringstream stream; |
---|
| 1032 | stream << "Cannot choose between " << winners.size() << " alternatives for " |
---|
| 1033 | << kind << (kind != "" ? " " : "") << "expression\n"; |
---|
| 1034 | ast::print( stream, untyped ); |
---|
| 1035 | stream << " Alternatives are:\n"; |
---|
| 1036 | print( stream, winners, 1 ); |
---|
| 1037 | SemanticError( untyped->location, stream.str() ); |
---|
| 1038 | } |
---|
| 1039 | |
---|
| 1040 | // single selected choice |
---|
| 1041 | CandidateRef & choice = winners.front(); |
---|
| 1042 | |
---|
| 1043 | // fail on only expression deleted |
---|
| 1044 | if ( ! seen_undeleted ) { |
---|
| 1045 | SemanticError( untyped->location, choice->expr.get(), "Unique best alternative " |
---|
| 1046 | "includes deleted identifier in " ); |
---|
| 1047 | } |
---|
| 1048 | |
---|
| 1049 | return std::move( choice ); |
---|
| 1050 | } |
---|
| 1051 | |
---|
| 1052 | /// Strips extraneous casts out of an expression |
---|
| 1053 | struct StripCasts_new final { |
---|
| 1054 | const ast::Expr * postmutate( const ast::CastExpr * castExpr ) { |
---|
| 1055 | if ( |
---|
| 1056 | castExpr->isGenerated |
---|
| 1057 | && typesCompatible( castExpr->arg->result, castExpr->result ) |
---|
| 1058 | ) { |
---|
| 1059 | // generated cast is the same type as its argument, remove it after keeping env |
---|
[b7d92b96] | 1060 | return ast::mutate_field( |
---|
| 1061 | castExpr->arg.get(), &ast::Expr::env, castExpr->env ); |
---|
[99d4584] | 1062 | } |
---|
| 1063 | return castExpr; |
---|
| 1064 | } |
---|
| 1065 | |
---|
| 1066 | static void strip( ast::ptr< ast::Expr > & expr ) { |
---|
| 1067 | ast::Pass< StripCasts_new > stripper; |
---|
| 1068 | expr = expr->accept( stripper ); |
---|
| 1069 | } |
---|
| 1070 | }; |
---|
| 1071 | |
---|
[60aaa51d] | 1072 | /// Swaps argument into expression pointer, saving original environment |
---|
| 1073 | void swap_and_save_env( ast::ptr< ast::Expr > & expr, const ast::Expr * newExpr ) { |
---|
| 1074 | ast::ptr< ast::TypeSubstitution > env = expr->env; |
---|
| 1075 | expr.set_and_mutate( newExpr )->env = env; |
---|
| 1076 | } |
---|
| 1077 | |
---|
[b7d92b96] | 1078 | /// Removes cast to type of argument (unlike StripCasts, also handles non-generated casts) |
---|
| 1079 | void removeExtraneousCast( ast::ptr<ast::Expr> & expr, const ast::SymbolTable & symtab ) { |
---|
| 1080 | if ( const ast::CastExpr * castExpr = expr.as< ast::CastExpr >() ) { |
---|
| 1081 | if ( typesCompatible( castExpr->arg->result, castExpr->result, symtab ) ) { |
---|
| 1082 | // cast is to the same type as its argument, remove it |
---|
[60aaa51d] | 1083 | swap_and_save_env( expr, castExpr->arg ); |
---|
[b7d92b96] | 1084 | } |
---|
| 1085 | } |
---|
| 1086 | } |
---|
| 1087 | |
---|
[99d4584] | 1088 | /// Establish post-resolver invariants for expressions |
---|
| 1089 | void finishExpr( |
---|
| 1090 | ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env, |
---|
| 1091 | const ast::TypeSubstitution * oldenv = nullptr |
---|
| 1092 | ) { |
---|
| 1093 | // set up new type substitution for expression |
---|
| 1094 | ast::ptr< ast::TypeSubstitution > newenv = |
---|
| 1095 | oldenv ? oldenv : new ast::TypeSubstitution{}; |
---|
| 1096 | env.writeToSubstitution( *newenv.get_and_mutate() ); |
---|
| 1097 | expr.get_and_mutate()->env = std::move( newenv ); |
---|
| 1098 | // remove unncecessary casts |
---|
| 1099 | StripCasts_new::strip( expr ); |
---|
| 1100 | } |
---|
[b7d92b96] | 1101 | |
---|
| 1102 | /// Find the expression candidate that is the unique best match for `untyped` in a `void` |
---|
| 1103 | /// context. |
---|
| 1104 | ast::ptr< ast::Expr > resolveInVoidContext( |
---|
| 1105 | const ast::Expr * expr, const ast::SymbolTable & symtab, ast::TypeEnvironment & env |
---|
| 1106 | ) { |
---|
| 1107 | assertf( expr, "expected a non-null expression" ); |
---|
| 1108 | |
---|
| 1109 | // set up and resolve expression cast to void |
---|
| 1110 | ast::CastExpr * untyped = new ast::CastExpr{ expr->location, expr }; |
---|
| 1111 | CandidateRef choice = findUnfinishedKindExpression( |
---|
| 1112 | untyped, symtab, "", anyCandidate, ResolvMode::withAdjustment() ); |
---|
| 1113 | |
---|
| 1114 | // a cast expression has either 0 or 1 interpretations (by language rules); |
---|
| 1115 | // if 0, an exception has already been thrown, and this code will not run |
---|
| 1116 | const ast::CastExpr * castExpr = choice->expr.strict_as< ast::CastExpr >(); |
---|
| 1117 | env = std::move( choice->env ); |
---|
| 1118 | |
---|
| 1119 | return castExpr->arg; |
---|
| 1120 | } |
---|
| 1121 | |
---|
| 1122 | /// Resolve `untyped` to the expression whose candidate is the best match for a `void` |
---|
| 1123 | /// context. |
---|
| 1124 | ast::ptr< ast::Expr > findVoidExpression( |
---|
| 1125 | const ast::Expr * untyped, const ast::SymbolTable & symtab |
---|
| 1126 | ) { |
---|
| 1127 | resetTyVarRenaming(); |
---|
| 1128 | ast::TypeEnvironment env; |
---|
| 1129 | ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, symtab, env ); |
---|
| 1130 | finishExpr( newExpr, env, untyped->env ); |
---|
| 1131 | return newExpr; |
---|
| 1132 | } |
---|
| 1133 | |
---|
[99d4584] | 1134 | /// resolve `untyped` to the expression whose candidate satisfies `pred` with the |
---|
| 1135 | /// lowest cost, returning the resolved version |
---|
| 1136 | ast::ptr< ast::Expr > findKindExpression( |
---|
[2b59f55] | 1137 | const ast::Expr * untyped, const ast::SymbolTable & symtab, |
---|
| 1138 | std::function<bool(const Candidate &)> pred = anyCandidate, |
---|
| 1139 | const std::string & kind = "", ResolvMode mode = {} |
---|
[99d4584] | 1140 | ) { |
---|
| 1141 | if ( ! untyped ) return {}; |
---|
| 1142 | CandidateRef choice = |
---|
| 1143 | findUnfinishedKindExpression( untyped, symtab, kind, pred, mode ); |
---|
| 1144 | finishExpr( choice->expr, choice->env, untyped->env ); |
---|
| 1145 | return std::move( choice->expr ); |
---|
| 1146 | } |
---|
| 1147 | |
---|
[b7d92b96] | 1148 | /// Resolve `untyped` to the single expression whose candidate is the best match for the |
---|
| 1149 | /// given type. |
---|
| 1150 | ast::ptr< ast::Expr > findSingleExpression( |
---|
| 1151 | const ast::Expr * untyped, const ast::Type * type, const ast::SymbolTable & symtab |
---|
| 1152 | ) { |
---|
| 1153 | assert( untyped && type ); |
---|
| 1154 | const ast::Expr * castExpr = new ast::CastExpr{ untyped->location, untyped, type }; |
---|
[2b59f55] | 1155 | ast::ptr< ast::Expr > newExpr = findKindExpression( castExpr, symtab ); |
---|
[b7d92b96] | 1156 | removeExtraneousCast( newExpr, symtab ); |
---|
| 1157 | return newExpr; |
---|
| 1158 | } |
---|
| 1159 | |
---|
[99d4584] | 1160 | /// Predicate for "Candidate has integral type" |
---|
| 1161 | bool hasIntegralType( const Candidate & i ) { |
---|
| 1162 | const ast::Type * type = i.expr->result; |
---|
| 1163 | |
---|
| 1164 | if ( auto bt = dynamic_cast< const ast::BasicType * >( type ) ) { |
---|
| 1165 | return bt->isInteger(); |
---|
| 1166 | } else if ( |
---|
| 1167 | dynamic_cast< const ast::EnumInstType * >( type ) |
---|
| 1168 | || dynamic_cast< const ast::ZeroType * >( type ) |
---|
| 1169 | || dynamic_cast< const ast::OneType * >( type ) |
---|
| 1170 | ) { |
---|
| 1171 | return true; |
---|
| 1172 | } else return false; |
---|
| 1173 | } |
---|
| 1174 | |
---|
| 1175 | /// Resolve `untyped` as an integral expression, returning the resolved version |
---|
| 1176 | ast::ptr< ast::Expr > findIntegralExpression( |
---|
| 1177 | const ast::Expr * untyped, const ast::SymbolTable & symtab |
---|
| 1178 | ) { |
---|
[2b59f55] | 1179 | return findKindExpression( untyped, symtab, hasIntegralType, "condition" ); |
---|
[99d4584] | 1180 | } |
---|
[60aaa51d] | 1181 | |
---|
| 1182 | /// check if a type is a character type |
---|
| 1183 | bool isCharType( const ast::Type * t ) { |
---|
| 1184 | if ( auto bt = dynamic_cast< const ast::BasicType * >( t ) ) { |
---|
| 1185 | return bt->kind == ast::BasicType::Char |
---|
| 1186 | || bt->kind == ast::BasicType::SignedChar |
---|
| 1187 | || bt->kind == ast::BasicType::UnsignedChar; |
---|
| 1188 | } |
---|
| 1189 | return false; |
---|
| 1190 | } |
---|
[99d4584] | 1191 | } |
---|
| 1192 | |
---|
[4864a73] | 1193 | class Resolver_new final |
---|
| 1194 | : public ast::WithSymbolTable, public ast::WithGuards, |
---|
| 1195 | public ast::WithVisitorRef<Resolver_new>, public ast::WithShortCircuiting, |
---|
[0e42794] | 1196 | public ast::WithStmtsToAdd<> { |
---|
[4864a73] | 1197 | |
---|
[2a8f0c1] | 1198 | ast::ptr< ast::Type > functionReturn = nullptr; |
---|
[2b59f55] | 1199 | ast::CurrentObject currentObject; |
---|
[99d4584] | 1200 | bool inEnumDecl = false; |
---|
[2a8f0c1] | 1201 | |
---|
[4864a73] | 1202 | public: |
---|
[d76c588] | 1203 | Resolver_new() = default; |
---|
[0e42794] | 1204 | Resolver_new( const ast::SymbolTable & syms ) { symtab = syms; } |
---|
[d76c588] | 1205 | |
---|
[99d4584] | 1206 | void previsit( const ast::FunctionDecl * ); |
---|
| 1207 | const ast::FunctionDecl * postvisit( const ast::FunctionDecl * ); |
---|
| 1208 | void previsit( const ast::ObjectDecl * ); |
---|
| 1209 | void previsit( const ast::EnumDecl * ); |
---|
| 1210 | const ast::StaticAssertDecl * previsit( const ast::StaticAssertDecl * ); |
---|
| 1211 | |
---|
| 1212 | void previsit( const ast::ArrayType * ); |
---|
| 1213 | void previsit( const ast::PointerType * ); |
---|
| 1214 | |
---|
[2b59f55] | 1215 | const ast::ExprStmt * previsit( const ast::ExprStmt * ); |
---|
| 1216 | const ast::AsmExpr * previsit( const ast::AsmExpr * ); |
---|
| 1217 | const ast::AsmStmt * previsit( const ast::AsmStmt * ); |
---|
| 1218 | const ast::IfStmt * previsit( const ast::IfStmt * ); |
---|
| 1219 | const ast::WhileStmt * previsit( const ast::WhileStmt * ); |
---|
| 1220 | const ast::ForStmt * previsit( const ast::ForStmt * ); |
---|
[b7d92b96] | 1221 | const ast::SwitchStmt * previsit( const ast::SwitchStmt * ); |
---|
[2b59f55] | 1222 | const ast::CaseStmt * previsit( const ast::CaseStmt * ); |
---|
[b7d92b96] | 1223 | const ast::BranchStmt * previsit( const ast::BranchStmt * ); |
---|
[2b59f55] | 1224 | const ast::ReturnStmt * previsit( const ast::ReturnStmt * ); |
---|
| 1225 | const ast::ThrowStmt * previsit( const ast::ThrowStmt * ); |
---|
| 1226 | const ast::CatchStmt * previsit( const ast::CatchStmt * ); |
---|
[99d4584] | 1227 | void previsit( const ast::WaitForStmt * ); |
---|
| 1228 | |
---|
[2d11663] | 1229 | const ast::SingleInit * previsit( const ast::SingleInit * ); |
---|
| 1230 | const ast::ListInit * previsit( const ast::ListInit * ); |
---|
| 1231 | const ast::ConstructorInit * previsit( const ast::ConstructorInit * ); |
---|
[d76c588] | 1232 | }; |
---|
| 1233 | |
---|
| 1234 | void resolve( std::list< ast::ptr<ast::Decl> >& translationUnit ) { |
---|
| 1235 | ast::Pass<Resolver_new> resolver; |
---|
| 1236 | accept_all( translationUnit, resolver ); |
---|
| 1237 | } |
---|
| 1238 | |
---|
[2a8f0c1] | 1239 | void Resolver_new::previsit( const ast::FunctionDecl * functionDecl ) { |
---|
| 1240 | GuardValue( functionReturn ); |
---|
| 1241 | functionReturn = extractResultType( functionDecl->type ); |
---|
[d76c588] | 1242 | } |
---|
| 1243 | |
---|
[2a8f0c1] | 1244 | const ast::FunctionDecl * Resolver_new::postvisit( const ast::FunctionDecl * functionDecl ) { |
---|
[4864a73] | 1245 | // default value expressions have an environment which shouldn't be there and trips up |
---|
[2a8f0c1] | 1246 | // later passes. |
---|
| 1247 | ast::ptr< ast::FunctionDecl > ret = functionDecl; |
---|
| 1248 | for ( unsigned i = 0; i < functionDecl->type->params.size(); ++i ) { |
---|
| 1249 | const ast::ptr<ast::DeclWithType> & d = functionDecl->type->params[i]; |
---|
[4864a73] | 1250 | |
---|
[2a8f0c1] | 1251 | if ( const ast::ObjectDecl * obj = d.as< ast::ObjectDecl >() ) { |
---|
| 1252 | if ( const ast::SingleInit * init = obj->init.as< ast::SingleInit >() ) { |
---|
| 1253 | if ( init->value->env == nullptr ) continue; |
---|
| 1254 | // clone initializer minus the initializer environment |
---|
[4864a73] | 1255 | ast::chain_mutate( ret ) |
---|
| 1256 | ( &ast::FunctionDecl::type ) |
---|
[3cd5fdd] | 1257 | ( &ast::FunctionType::params )[i] |
---|
[4864a73] | 1258 | ( &ast::ObjectDecl::init ) |
---|
| 1259 | ( &ast::SingleInit::value )->env = nullptr; |
---|
| 1260 | |
---|
| 1261 | assert( functionDecl != ret.get() || functionDecl->unique() ); |
---|
| 1262 | assert( ! ret->type->params[i].strict_as< ast::ObjectDecl >()->init.strict_as< ast::SingleInit >()->value->env ); |
---|
[2a8f0c1] | 1263 | } |
---|
| 1264 | } |
---|
| 1265 | } |
---|
| 1266 | return ret.get(); |
---|
[d76c588] | 1267 | } |
---|
| 1268 | |
---|
[2a8f0c1] | 1269 | void Resolver_new::previsit( const ast::ObjectDecl * objectDecl ) { |
---|
[b7d92b96] | 1270 | // To handle initialization of routine pointers [e.g. int (*fp)(int) = foo()], |
---|
| 1271 | // class-variable `initContext` is changed multiple times because the LHS is analyzed |
---|
| 1272 | // twice. The second analysis changes `initContext` because a function type can contain |
---|
| 1273 | // object declarations in the return and parameter types. Therefore each value of |
---|
| 1274 | // `initContext` is retained so the type on the first analysis is preserved and used for |
---|
| 1275 | // selecting the RHS. |
---|
| 1276 | GuardValue( currentObject ); |
---|
[2b59f55] | 1277 | currentObject = ast::CurrentObject{ objectDecl->location, objectDecl->get_type() }; |
---|
[b7d92b96] | 1278 | if ( inEnumDecl && dynamic_cast< const ast::EnumInstType * >( objectDecl->get_type() ) ) { |
---|
| 1279 | // enumerator initializers should not use the enum type to initialize, since the |
---|
| 1280 | // enum type is still incomplete at this point. Use `int` instead. |
---|
[2b59f55] | 1281 | currentObject = ast::CurrentObject{ |
---|
| 1282 | objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } }; |
---|
[b7d92b96] | 1283 | } |
---|
[d76c588] | 1284 | } |
---|
| 1285 | |
---|
[99d4584] | 1286 | void Resolver_new::previsit( const ast::EnumDecl * ) { |
---|
| 1287 | // in case we decide to allow nested enums |
---|
| 1288 | GuardValue( inEnumDecl ); |
---|
| 1289 | inEnumDecl = false; |
---|
[d76c588] | 1290 | } |
---|
| 1291 | |
---|
[99d4584] | 1292 | const ast::StaticAssertDecl * Resolver_new::previsit( |
---|
| 1293 | const ast::StaticAssertDecl * assertDecl |
---|
| 1294 | ) { |
---|
[b7d92b96] | 1295 | return ast::mutate_field( |
---|
| 1296 | assertDecl, &ast::StaticAssertDecl::cond, |
---|
| 1297 | findIntegralExpression( assertDecl->cond, symtab ) ); |
---|
| 1298 | } |
---|
| 1299 | |
---|
| 1300 | template< typename PtrType > |
---|
| 1301 | void handlePtrType( const PtrType * type, const ast::SymbolTable & symtab ) { |
---|
| 1302 | #warning unimplemented; needs support for new Validate::SizeType global |
---|
| 1303 | (void)type; (void)symtab; |
---|
| 1304 | assert( false ); |
---|
[d76c588] | 1305 | } |
---|
| 1306 | |
---|
[2a8f0c1] | 1307 | void Resolver_new::previsit( const ast::ArrayType * at ) { |
---|
[b7d92b96] | 1308 | handlePtrType( at, symtab ); |
---|
[d76c588] | 1309 | } |
---|
| 1310 | |
---|
[2a8f0c1] | 1311 | void Resolver_new::previsit( const ast::PointerType * pt ) { |
---|
[b7d92b96] | 1312 | handlePtrType( pt, symtab ); |
---|
[d76c588] | 1313 | } |
---|
| 1314 | |
---|
[b7d92b96] | 1315 | const ast::ExprStmt * Resolver_new::previsit( const ast::ExprStmt * exprStmt ) { |
---|
| 1316 | visit_children = false; |
---|
| 1317 | assertf( exprStmt->expr, "ExprStmt has null expression in resolver" ); |
---|
| 1318 | |
---|
| 1319 | return ast::mutate_field( |
---|
| 1320 | exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, symtab ) ); |
---|
[d76c588] | 1321 | } |
---|
| 1322 | |
---|
[b7d92b96] | 1323 | const ast::AsmExpr * Resolver_new::previsit( const ast::AsmExpr * asmExpr ) { |
---|
| 1324 | visit_children = false; |
---|
| 1325 | |
---|
| 1326 | asmExpr = ast::mutate_field( |
---|
| 1327 | asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, symtab ) ); |
---|
| 1328 | |
---|
| 1329 | if ( asmExpr->inout ) { |
---|
| 1330 | asmExpr = ast::mutate_field( |
---|
| 1331 | asmExpr, &ast::AsmExpr::inout, findVoidExpression( asmExpr->inout, symtab ) ); |
---|
| 1332 | } |
---|
| 1333 | |
---|
| 1334 | return asmExpr; |
---|
[d76c588] | 1335 | } |
---|
| 1336 | |
---|
[2b59f55] | 1337 | const ast::AsmStmt * Resolver_new::previsit( const ast::AsmStmt * asmStmt ) { |
---|
| 1338 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::input ); |
---|
| 1339 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::output ); |
---|
| 1340 | visit_children = false; |
---|
| 1341 | return asmStmt; |
---|
[d76c588] | 1342 | } |
---|
| 1343 | |
---|
[b7d92b96] | 1344 | const ast::IfStmt * Resolver_new::previsit( const ast::IfStmt * ifStmt ) { |
---|
| 1345 | return ast::mutate_field( |
---|
| 1346 | ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, symtab ) ); |
---|
[d76c588] | 1347 | } |
---|
| 1348 | |
---|
[b7d92b96] | 1349 | const ast::WhileStmt * Resolver_new::previsit( const ast::WhileStmt * whileStmt ) { |
---|
| 1350 | return ast::mutate_field( |
---|
| 1351 | whileStmt, &ast::WhileStmt::cond, findIntegralExpression( whileStmt->cond, symtab ) ); |
---|
[d76c588] | 1352 | } |
---|
| 1353 | |
---|
[b7d92b96] | 1354 | const ast::ForStmt * Resolver_new::previsit( const ast::ForStmt * forStmt ) { |
---|
| 1355 | if ( forStmt->cond ) { |
---|
| 1356 | forStmt = ast::mutate_field( |
---|
| 1357 | forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, symtab ) ); |
---|
| 1358 | } |
---|
| 1359 | |
---|
| 1360 | if ( forStmt->inc ) { |
---|
| 1361 | forStmt = ast::mutate_field( |
---|
| 1362 | forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, symtab ) ); |
---|
| 1363 | } |
---|
| 1364 | |
---|
| 1365 | return forStmt; |
---|
[d76c588] | 1366 | } |
---|
| 1367 | |
---|
[b7d92b96] | 1368 | const ast::SwitchStmt * Resolver_new::previsit( const ast::SwitchStmt * switchStmt ) { |
---|
| 1369 | GuardValue( currentObject ); |
---|
| 1370 | switchStmt = ast::mutate_field( |
---|
| 1371 | switchStmt, &ast::SwitchStmt::cond, |
---|
| 1372 | findIntegralExpression( switchStmt->cond, symtab ) ); |
---|
[2b59f55] | 1373 | currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result }; |
---|
[b7d92b96] | 1374 | return switchStmt; |
---|
[d76c588] | 1375 | } |
---|
| 1376 | |
---|
[b7d92b96] | 1377 | const ast::CaseStmt * Resolver_new::previsit( const ast::CaseStmt * caseStmt ) { |
---|
| 1378 | if ( caseStmt->cond ) { |
---|
[60aaa51d] | 1379 | std::deque< ast::InitAlternative > initAlts = currentObject.getOptions(); |
---|
[2b59f55] | 1380 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral " |
---|
| 1381 | "expression." ); |
---|
| 1382 | |
---|
| 1383 | const ast::Expr * untyped = |
---|
| 1384 | new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type }; |
---|
| 1385 | ast::ptr< ast::Expr > newExpr = findKindExpression( untyped, symtab ); |
---|
| 1386 | |
---|
| 1387 | // case condition cannot have a cast in C, so it must be removed here, regardless of |
---|
| 1388 | // whether it would perform a conversion. |
---|
| 1389 | if ( const ast::CastExpr * castExpr = newExpr.as< ast::CastExpr >() ) { |
---|
[60aaa51d] | 1390 | swap_and_save_env( newExpr, castExpr->arg ); |
---|
[2b59f55] | 1391 | } |
---|
| 1392 | |
---|
| 1393 | caseStmt = ast::mutate_field( caseStmt, &ast::CaseStmt::cond, newExpr ); |
---|
[b7d92b96] | 1394 | } |
---|
| 1395 | return caseStmt; |
---|
[d76c588] | 1396 | } |
---|
| 1397 | |
---|
[b7d92b96] | 1398 | const ast::BranchStmt * Resolver_new::previsit( const ast::BranchStmt * branchStmt ) { |
---|
| 1399 | visit_children = false; |
---|
| 1400 | // must resolve the argument of a computed goto |
---|
| 1401 | if ( branchStmt->kind == ast::BranchStmt::Goto && branchStmt->computedTarget ) { |
---|
| 1402 | // computed goto argument is void* |
---|
| 1403 | branchStmt = ast::mutate_field( |
---|
| 1404 | branchStmt, &ast::BranchStmt::computedTarget, |
---|
| 1405 | findSingleExpression( |
---|
| 1406 | branchStmt->computedTarget, new ast::PointerType{ new ast::VoidType{} }, |
---|
| 1407 | symtab ) ); |
---|
| 1408 | } |
---|
| 1409 | return branchStmt; |
---|
[d76c588] | 1410 | } |
---|
| 1411 | |
---|
[2b59f55] | 1412 | const ast::ReturnStmt * Resolver_new::previsit( const ast::ReturnStmt * returnStmt ) { |
---|
| 1413 | visit_children = false; |
---|
| 1414 | if ( returnStmt->expr ) { |
---|
| 1415 | returnStmt = ast::mutate_field( |
---|
| 1416 | returnStmt, &ast::ReturnStmt::expr, |
---|
| 1417 | findSingleExpression( returnStmt->expr, functionReturn, symtab ) ); |
---|
| 1418 | } |
---|
| 1419 | return returnStmt; |
---|
[d76c588] | 1420 | } |
---|
| 1421 | |
---|
[2b59f55] | 1422 | const ast::ThrowStmt * Resolver_new::previsit( const ast::ThrowStmt * throwStmt ) { |
---|
| 1423 | visit_children = false; |
---|
| 1424 | if ( throwStmt->expr ) { |
---|
| 1425 | const ast::StructDecl * exceptionDecl = |
---|
| 1426 | symtab.lookupStruct( "__cfaabi_ehm__base_exception_t" ); |
---|
| 1427 | assert( exceptionDecl ); |
---|
| 1428 | ast::ptr< ast::Type > exceptType = |
---|
| 1429 | new ast::PointerType{ new ast::StructInstType{ exceptionDecl } }; |
---|
| 1430 | throwStmt = ast::mutate_field( |
---|
| 1431 | throwStmt, &ast::ThrowStmt::expr, |
---|
| 1432 | findSingleExpression( throwStmt->expr, exceptType, symtab ) ); |
---|
| 1433 | } |
---|
| 1434 | return throwStmt; |
---|
[d76c588] | 1435 | } |
---|
| 1436 | |
---|
[2b59f55] | 1437 | const ast::CatchStmt * Resolver_new::previsit( const ast::CatchStmt * catchStmt ) { |
---|
| 1438 | if ( catchStmt->cond ) { |
---|
| 1439 | ast::ptr< ast::Type > boolType = new ast::BasicType{ ast::BasicType::Bool }; |
---|
| 1440 | catchStmt = ast::mutate_field( |
---|
| 1441 | catchStmt, &ast::CatchStmt::cond, |
---|
| 1442 | findSingleExpression( catchStmt->cond, boolType, symtab ) ); |
---|
| 1443 | } |
---|
| 1444 | return catchStmt; |
---|
[d76c588] | 1445 | } |
---|
| 1446 | |
---|
[2a8f0c1] | 1447 | void Resolver_new::previsit( const ast::WaitForStmt * stmt ) { |
---|
[d76c588] | 1448 | #warning unimplemented; Resolver port in progress |
---|
| 1449 | (void)stmt; |
---|
| 1450 | assert(false); |
---|
| 1451 | } |
---|
| 1452 | |
---|
[60aaa51d] | 1453 | |
---|
| 1454 | |
---|
| 1455 | const ast::SingleInit * Resolver_new::previsit( const ast::SingleInit * singleInit ) { |
---|
| 1456 | visit_children = false; |
---|
| 1457 | // resolve initialization using the possibilities as determined by the `currentObject` |
---|
| 1458 | // cursor. |
---|
| 1459 | ast::Expr * untyped = new ast::UntypedInitExpr{ |
---|
| 1460 | singleInit->location, singleInit->value, currentObject.getOptions() }; |
---|
| 1461 | ast::ptr<ast::Expr> newExpr = findKindExpression( untyped, symtab ); |
---|
| 1462 | const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >(); |
---|
| 1463 | |
---|
| 1464 | // move cursor to the object that is actually initialized |
---|
| 1465 | currentObject.setNext( initExpr->designation ); |
---|
| 1466 | |
---|
| 1467 | // discard InitExpr wrapper and retain relevant pieces. |
---|
| 1468 | // `initExpr` may have inferred params in the case where the expression specialized a |
---|
| 1469 | // function pointer, and newExpr may already have inferParams of its own, so a simple |
---|
| 1470 | // swap is not sufficient |
---|
| 1471 | ast::Expr::InferUnion inferred = initExpr->inferred; |
---|
| 1472 | swap_and_save_env( newExpr, initExpr->expr ); |
---|
| 1473 | newExpr.get_and_mutate()->inferred.splice( std::move(inferred) ); |
---|
| 1474 | |
---|
| 1475 | // get the actual object's type (may not exactly match what comes back from the resolver |
---|
| 1476 | // due to conversions) |
---|
| 1477 | const ast::Type * initContext = currentObject.getCurrentType(); |
---|
| 1478 | |
---|
| 1479 | removeExtraneousCast( newExpr, symtab ); |
---|
| 1480 | |
---|
| 1481 | // check if actual object's type is char[] |
---|
| 1482 | if ( auto at = dynamic_cast< const ast::ArrayType * >( initContext ) ) { |
---|
| 1483 | if ( isCharType( at->base ) ) { |
---|
| 1484 | // check if the resolved type is char* |
---|
| 1485 | if ( auto pt = newExpr->result.as< ast::PointerType >() ) { |
---|
| 1486 | if ( isCharType( pt->base ) ) { |
---|
| 1487 | // strip cast if we're initializing a char[] with a char* |
---|
| 1488 | // e.g. char x[] = "hello" |
---|
| 1489 | if ( auto ce = newExpr.as< ast::CastExpr >() ) { |
---|
| 1490 | swap_and_save_env( newExpr, ce->arg ); |
---|
| 1491 | } |
---|
| 1492 | } |
---|
| 1493 | } |
---|
| 1494 | } |
---|
| 1495 | } |
---|
| 1496 | |
---|
| 1497 | // move cursor to next object in preparation for next initializer |
---|
| 1498 | currentObject.increment(); |
---|
| 1499 | |
---|
| 1500 | // set initializer expression to resolved expression |
---|
| 1501 | return ast::mutate_field( singleInit, &ast::SingleInit::value, std::move(newExpr) ); |
---|
[d76c588] | 1502 | } |
---|
| 1503 | |
---|
[60aaa51d] | 1504 | const ast::ListInit * Resolver_new::previsit( const ast::ListInit * listInit ) { |
---|
| 1505 | // move cursor into brace-enclosed initializer-list |
---|
| 1506 | currentObject.enterListInit( listInit->location ); |
---|
| 1507 | |
---|
| 1508 | assert( listInit->designations.size() == listInit->initializers.size() ); |
---|
| 1509 | for ( unsigned i = 0; i < listInit->designations.size(); ++i ) { |
---|
| 1510 | // iterate designations and initializers in pairs, moving the cursor to the current |
---|
| 1511 | // designated object and resolving the initializer against that object |
---|
[2d11663] | 1512 | listInit = ast::mutate_field_index( |
---|
| 1513 | listInit, &ast::ListInit::designations, i, |
---|
| 1514 | currentObject.findNext( listInit->designations[i] ) ); |
---|
| 1515 | listInit = ast::mutate_field_index( |
---|
| 1516 | listInit, &ast::ListInit::initializers, i, |
---|
| 1517 | listInit->initializers[i]->accept( *visitor ) ); |
---|
[60aaa51d] | 1518 | } |
---|
| 1519 | |
---|
[2d11663] | 1520 | // move cursor out of brace-enclosed initializer-list |
---|
| 1521 | currentObject.exitListInit(); |
---|
| 1522 | |
---|
[60aaa51d] | 1523 | visit_children = false; |
---|
| 1524 | return listInit; |
---|
[d76c588] | 1525 | } |
---|
| 1526 | |
---|
[2d11663] | 1527 | const ast::ConstructorInit * Resolver_new::previsit( const ast::ConstructorInit * ctorInit ) { |
---|
| 1528 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::ctor ); |
---|
| 1529 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::dtor ); |
---|
| 1530 | |
---|
| 1531 | // found a constructor - can get rid of C-style initializer |
---|
| 1532 | // xxx - Rob suggests this field is dead code |
---|
| 1533 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::init, nullptr ); |
---|
| 1534 | |
---|
| 1535 | // intrinsic single-parameter constructors and destructors do nothing. Since this was |
---|
| 1536 | // implicitly generated, there's no way for it to have side effects, so get rid of it to |
---|
| 1537 | // clean up generated code |
---|
| 1538 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { |
---|
| 1539 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::ctor, nullptr ); |
---|
| 1540 | } |
---|
| 1541 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { |
---|
| 1542 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::dtor, nullptr ); |
---|
| 1543 | } |
---|
| 1544 | |
---|
| 1545 | return ctorInit; |
---|
[d76c588] | 1546 | } |
---|
| 1547 | |
---|
[51b7345] | 1548 | } // namespace ResolvExpr |
---|
[a32b204] | 1549 | |
---|
| 1550 | // Local Variables: // |
---|
| 1551 | // tab-width: 4 // |
---|
| 1552 | // mode: c++ // |
---|
| 1553 | // compile-command: "make install" // |
---|
| 1554 | // End: // |
---|