[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 |
---|
[39d8950] | 11 | // Last Modified By : Andrew Beach |
---|
[f6e6a55] | 12 | // Last Modified On : Wed Apr 20 10:41:00 2022 |
---|
| 13 | // Update Count : 248 |
---|
[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" |
---|
[16ba4a6f] | 28 | #include "ResolveTypeof.h" |
---|
[d76c588] | 29 | #include "ResolvMode.h" // for ResolvMode |
---|
| 30 | #include "typeops.h" // for extractResultType |
---|
| 31 | #include "Unify.h" // for unify |
---|
[16ba4a6f] | 32 | #include "CompilationState.h" |
---|
[4864a73] | 33 | #include "AST/Chain.hpp" |
---|
[2a8f0c1] | 34 | #include "AST/Decl.hpp" |
---|
| 35 | #include "AST/Init.hpp" |
---|
[d76c588] | 36 | #include "AST/Pass.hpp" |
---|
[99d4584] | 37 | #include "AST/Print.hpp" |
---|
[d76c588] | 38 | #include "AST/SymbolTable.hpp" |
---|
[2773ab8] | 39 | #include "AST/Type.hpp" |
---|
[a4ca48c] | 40 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
[ea6332d] | 41 | #include "Common/SemanticError.h" // for SemanticError |
---|
[57e0289] | 42 | #include "Common/Stats/ResolveTime.h" // for ResolveTime::start(), ResolveTime::stop() |
---|
[ea6332d] | 43 | #include "Common/utility.h" // for ValueGuard, group_iterate |
---|
[0a60c04] | 44 | #include "InitTweak/GenInit.h" |
---|
[ea6332d] | 45 | #include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt |
---|
| 46 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment |
---|
| 47 | #include "SymTab/Autogen.h" // for SizeType |
---|
| 48 | #include "SymTab/Indexer.h" // for Indexer |
---|
[16ba4a6f] | 49 | #include "SymTab/Mangler.h" // for Mangler |
---|
[ea6332d] | 50 | #include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar... |
---|
| 51 | #include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr |
---|
| 52 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit |
---|
| 53 | #include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt |
---|
| 54 | #include "SynTree/Type.h" // for Type, BasicType, PointerType |
---|
| 55 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
| 56 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
[0a60c04] | 57 | #include "Tuples/Tuples.h" |
---|
[2bfc6b2] | 58 | #include "Validate/FindSpecialDecls.h" // for SizeType |
---|
[51b7345] | 59 | |
---|
[d9a0e76] | 60 | using namespace std; |
---|
[51b7345] | 61 | |
---|
[d9a0e76] | 62 | namespace ResolvExpr { |
---|
[d76c588] | 63 | struct Resolver_old final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver_old>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
| 64 | Resolver_old() {} |
---|
| 65 | Resolver_old( const SymTab::Indexer & other ) { |
---|
[a4ca48c] | 66 | indexer = other; |
---|
[1d2b64f] | 67 | } |
---|
[71f4e4f] | 68 | |
---|
[5170d95] | 69 | void previsit( FunctionDecl * functionDecl ); |
---|
| 70 | void postvisit( FunctionDecl * functionDecl ); |
---|
| 71 | void previsit( ObjectDecl * objectDecll ); |
---|
[a4ca48c] | 72 | void previsit( EnumDecl * enumDecl ); |
---|
[bd87b138] | 73 | void previsit( StaticAssertDecl * assertDecl ); |
---|
[a4ca48c] | 74 | |
---|
| 75 | void previsit( ArrayType * at ); |
---|
| 76 | void previsit( PointerType * at ); |
---|
| 77 | |
---|
[5170d95] | 78 | void previsit( ExprStmt * exprStmt ); |
---|
| 79 | void previsit( AsmExpr * asmExpr ); |
---|
| 80 | void previsit( AsmStmt * asmStmt ); |
---|
| 81 | void previsit( IfStmt * ifStmt ); |
---|
[3b0bc16] | 82 | void previsit( WhileDoStmt * whileDoStmt ); |
---|
[5170d95] | 83 | void previsit( ForStmt * forStmt ); |
---|
| 84 | void previsit( SwitchStmt * switchStmt ); |
---|
| 85 | void previsit( CaseStmt * caseStmt ); |
---|
| 86 | void previsit( BranchStmt * branchStmt ); |
---|
| 87 | void previsit( ReturnStmt * returnStmt ); |
---|
| 88 | void previsit( ThrowStmt * throwStmt ); |
---|
| 89 | void previsit( CatchStmt * catchStmt ); |
---|
[3b9c674] | 90 | void postvisit( CatchStmt * catchStmt ); |
---|
[695e00d] | 91 | void previsit( WaitForStmt * stmt ); |
---|
[a4ca48c] | 92 | |
---|
[5170d95] | 93 | void previsit( SingleInit * singleInit ); |
---|
| 94 | void previsit( ListInit * listInit ); |
---|
| 95 | void previsit( ConstructorInit * ctorInit ); |
---|
[a32b204] | 96 | private: |
---|
[c28a038d] | 97 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
[94b4364] | 98 | |
---|
[40e636a] | 99 | template< typename PtrType > |
---|
| 100 | void handlePtrType( PtrType * type ); |
---|
| 101 | |
---|
[c28a038d] | 102 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
[b726084] | 103 | |
---|
[77971f6] | 104 | Type * functionReturn = nullptr; |
---|
[e4d829b] | 105 | CurrentObject currentObject = nullptr; |
---|
[a436947] | 106 | bool inEnumDecl = false; |
---|
[a32b204] | 107 | }; |
---|
[d9a0e76] | 108 | |
---|
[2a6292d] | 109 | struct ResolveWithExprs : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveWithExprs>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
| 110 | void previsit( FunctionDecl * ); |
---|
| 111 | void previsit( WithStmt * ); |
---|
| 112 | |
---|
| 113 | void resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ); |
---|
| 114 | }; |
---|
| 115 | |
---|
[a32b204] | 116 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
[d76c588] | 117 | PassVisitor<Resolver_old> resolver; |
---|
[a32b204] | 118 | acceptAll( translationUnit, resolver ); |
---|
[d9a0e76] | 119 | } |
---|
| 120 | |
---|
[5170d95] | 121 | void resolveDecl( Declaration * decl, const SymTab::Indexer & indexer ) { |
---|
[d76c588] | 122 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
[8b11840] | 123 | maybeAccept( decl, resolver ); |
---|
| 124 | } |
---|
| 125 | |
---|
[c71b256] | 126 | namespace { |
---|
[99d4584] | 127 | struct DeleteFinder_old : public WithShortCircuiting { |
---|
[c71b256] | 128 | DeletedExpr * delExpr = nullptr; |
---|
| 129 | void previsit( DeletedExpr * expr ) { |
---|
| 130 | if ( delExpr ) visit_children = false; |
---|
| 131 | else delExpr = expr; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | void previsit( Expression * ) { |
---|
| 135 | if ( delExpr ) visit_children = false; |
---|
| 136 | } |
---|
| 137 | }; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | DeletedExpr * findDeletedExpr( Expression * expr ) { |
---|
[99d4584] | 141 | PassVisitor<DeleteFinder_old> finder; |
---|
[c71b256] | 142 | expr->accept( finder ); |
---|
| 143 | return finder.pass.delExpr; |
---|
[d9a0e76] | 144 | } |
---|
[a32b204] | 145 | |
---|
| 146 | namespace { |
---|
[99d4584] | 147 | struct StripCasts_old { |
---|
[cdb990a] | 148 | Expression * postmutate( CastExpr * castExpr ) { |
---|
| 149 | if ( castExpr->isGenerated && ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, SymTab::Indexer() ) ) { |
---|
| 150 | // generated cast is to the same type as its argument, so it's unnecessary -- remove it |
---|
| 151 | Expression * expr = castExpr->arg; |
---|
| 152 | castExpr->arg = nullptr; |
---|
| 153 | std::swap( expr->env, castExpr->env ); |
---|
| 154 | return expr; |
---|
| 155 | } |
---|
| 156 | return castExpr; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | static void strip( Expression *& expr ) { |
---|
[99d4584] | 160 | PassVisitor<StripCasts_old> stripper; |
---|
[cdb990a] | 161 | expr = expr->acceptMutator( stripper ); |
---|
| 162 | } |
---|
| 163 | }; |
---|
| 164 | |
---|
[5170d95] | 165 | void finishExpr( Expression *& expr, const TypeEnvironment & env, TypeSubstitution * oldenv = nullptr ) { |
---|
[7664fad] | 166 | expr->env = oldenv ? oldenv->clone() : new TypeSubstitution; |
---|
[cdb990a] | 167 | env.makeSubstitution( *expr->env ); |
---|
[99d4584] | 168 | StripCasts_old::strip( expr ); // remove unnecessary casts that may be buried in an expression |
---|
[a32b204] | 169 | } |
---|
[0a22cda] | 170 | |
---|
| 171 | void removeExtraneousCast( Expression *& expr, const SymTab::Indexer & indexer ) { |
---|
| 172 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
[b7d92b96] | 173 | if ( typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) { |
---|
[0a22cda] | 174 | // cast is to the same type as its argument, so it's unnecessary -- remove it |
---|
| 175 | expr = castExpr->arg; |
---|
| 176 | castExpr->arg = nullptr; |
---|
| 177 | std::swap( expr->env, castExpr->env ); |
---|
| 178 | delete castExpr; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | } |
---|
[db4ecc5] | 182 | } // namespace |
---|
[a32b204] | 183 | |
---|
[8f98b78] | 184 | namespace { |
---|
[59cf83b] | 185 | void findUnfinishedKindExpression(Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{} ) { |
---|
[c71b256] | 186 | assertf( untyped, "expected a non-null expression." ); |
---|
[6d6e829] | 187 | |
---|
| 188 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver |
---|
| 189 | static unsigned recursion_level = 0; |
---|
| 190 | |
---|
| 191 | ++recursion_level; |
---|
[8587878e] | 192 | TypeEnvironment env; |
---|
| 193 | AlternativeFinder finder( indexer, env ); |
---|
[6d6e829] | 194 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); |
---|
| 195 | --recursion_level; |
---|
[c71b256] | 196 | |
---|
| 197 | #if 0 |
---|
| 198 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 199 | std::cerr << "untyped expr is "; |
---|
| 200 | untyped->print( std::cerr ); |
---|
| 201 | std::cerr << std::endl << "alternatives are:"; |
---|
| 202 | for ( const Alternative & alt : finder.get_alternatives() ) { |
---|
| 203 | alt.print( std::cerr ); |
---|
| 204 | } // for |
---|
| 205 | } // if |
---|
| 206 | #endif |
---|
[8587878e] | 207 | |
---|
[6d6e829] | 208 | // produce filtered list of alternatives |
---|
[8587878e] | 209 | AltList candidates; |
---|
| 210 | for ( Alternative & alt : finder.get_alternatives() ) { |
---|
[c71b256] | 211 | if ( pred( alt ) ) { |
---|
[8587878e] | 212 | candidates.push_back( std::move( alt ) ); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
[6d6e829] | 216 | // produce invalid error if no candidates |
---|
| 217 | if ( candidates.empty() ) { |
---|
[a16764a6] | 218 | SemanticError( untyped, toString( "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""), "expression: ") ); |
---|
[6d6e829] | 219 | } |
---|
| 220 | |
---|
| 221 | // search for cheapest candidate |
---|
| 222 | AltList winners; |
---|
| 223 | bool seen_undeleted = false; |
---|
| 224 | for ( unsigned i = 0; i < candidates.size(); ++i ) { |
---|
| 225 | int c = winners.empty() ? -1 : candidates[i].cost.compare( winners.front().cost ); |
---|
| 226 | |
---|
| 227 | if ( c > 0 ) continue; // skip more expensive than winner |
---|
| 228 | |
---|
| 229 | if ( c < 0 ) { |
---|
| 230 | // reset on new cheapest |
---|
| 231 | seen_undeleted = ! findDeletedExpr( candidates[i].expr ); |
---|
| 232 | winners.clear(); |
---|
| 233 | } else /* if ( c == 0 ) */ { |
---|
| 234 | if ( findDeletedExpr( candidates[i].expr ) ) { |
---|
| 235 | // skip deleted expression if already seen one equivalent-cost not |
---|
| 236 | if ( seen_undeleted ) continue; |
---|
| 237 | } else if ( ! seen_undeleted ) { |
---|
| 238 | // replace list of equivalent-cost deleted expressions with one non-deleted |
---|
| 239 | winners.clear(); |
---|
| 240 | seen_undeleted = true; |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
[2fd9f24] | 244 | winners.emplace_back( std::move( candidates[i] ) ); |
---|
[6d6e829] | 245 | } |
---|
| 246 | |
---|
| 247 | // promote alternative.cvtCost to .cost |
---|
| 248 | // xxx - I don't know why this is done, but I'm keeping the behaviour from findMinCost |
---|
| 249 | for ( Alternative& winner : winners ) { |
---|
| 250 | winner.cost = winner.cvtCost; |
---|
| 251 | } |
---|
[cde3891] | 252 | |
---|
[6d6e829] | 253 | // produce ambiguous errors, if applicable |
---|
| 254 | if ( winners.size() != 1 ) { |
---|
[8587878e] | 255 | std::ostringstream stream; |
---|
[c71b256] | 256 | stream << "Cannot choose between " << winners.size() << " alternatives for " << kindStr << (kindStr != "" ? " " : "") << "expression\n"; |
---|
[8587878e] | 257 | untyped->print( stream ); |
---|
[93401f8] | 258 | stream << " Alternatives are:\n"; |
---|
[8587878e] | 259 | printAlts( winners, stream, 1 ); |
---|
[a16764a6] | 260 | SemanticError( untyped->location, stream.str() ); |
---|
[8587878e] | 261 | } |
---|
| 262 | |
---|
[6d6e829] | 263 | // single selected choice |
---|
| 264 | Alternative& choice = winners.front(); |
---|
| 265 | |
---|
| 266 | // fail on only expression deleted |
---|
| 267 | if ( ! seen_undeleted ) { |
---|
[2a08c25] | 268 | SemanticError( untyped->location, choice.expr, "Unique best alternative includes deleted identifier in " ); |
---|
[c71b256] | 269 | } |
---|
[6d6e829] | 270 | |
---|
| 271 | // xxx - check for ambiguous expressions |
---|
[cde3891] | 272 | |
---|
[6d6e829] | 273 | // output selected choice |
---|
[c71b256] | 274 | alt = std::move( choice ); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /// resolve `untyped` to the expression whose alternative satisfies `pred` with the lowest cost; kindStr is used for providing better error messages |
---|
[59cf83b] | 278 | void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{}) { |
---|
[c71b256] | 279 | if ( ! untyped ) return; |
---|
| 280 | Alternative choice; |
---|
[59cf83b] | 281 | findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, mode ); |
---|
[c71b256] | 282 | finishExpr( choice.expr, choice.env, untyped->env ); |
---|
[8587878e] | 283 | delete untyped; |
---|
[c71b256] | 284 | untyped = choice.expr; |
---|
| 285 | choice.expr = nullptr; |
---|
[8587878e] | 286 | } |
---|
| 287 | |
---|
[c71b256] | 288 | bool standardAlternativeFilter( const Alternative & ) { |
---|
| 289 | // currently don't need to filter, under normal circumstances. |
---|
| 290 | // in the future, this may be useful for removing deleted expressions |
---|
| 291 | return true; |
---|
| 292 | } |
---|
| 293 | } // namespace |
---|
| 294 | |
---|
| 295 | // used in resolveTypeof |
---|
[5170d95] | 296 | Expression * resolveInVoidContext( Expression * expr, const SymTab::Indexer & indexer ) { |
---|
[c71b256] | 297 | TypeEnvironment env; |
---|
| 298 | return resolveInVoidContext( expr, indexer, env ); |
---|
| 299 | } |
---|
| 300 | |
---|
[5170d95] | 301 | Expression * resolveInVoidContext( Expression * expr, const SymTab::Indexer & indexer, TypeEnvironment & env ) { |
---|
[c71b256] | 302 | // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0 |
---|
| 303 | // interpretations, an exception has already been thrown. |
---|
| 304 | assertf( expr, "expected a non-null expression." ); |
---|
| 305 | |
---|
[5170d95] | 306 | CastExpr * untyped = new CastExpr( expr ); // cast to void |
---|
| 307 | untyped->location = expr->location; |
---|
[c71b256] | 308 | |
---|
| 309 | // set up and resolve expression cast to void |
---|
| 310 | Alternative choice; |
---|
[5170d95] | 311 | findUnfinishedKindExpression( untyped, choice, indexer, "", standardAlternativeFilter, ResolvMode::withAdjustment() ); |
---|
[c71b256] | 312 | CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( choice.expr ); |
---|
[5170d95] | 313 | assert( castExpr ); |
---|
[c71b256] | 314 | env = std::move( choice.env ); |
---|
| 315 | |
---|
| 316 | // clean up resolved expression |
---|
| 317 | Expression * ret = castExpr->arg; |
---|
| 318 | castExpr->arg = nullptr; |
---|
| 319 | |
---|
| 320 | // unlink the arg so that it isn't deleted twice at the end of the program |
---|
[5170d95] | 321 | untyped->arg = nullptr; |
---|
[c71b256] | 322 | return ret; |
---|
| 323 | } |
---|
| 324 | |
---|
[5170d95] | 325 | void findVoidExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
[c71b256] | 326 | resetTyVarRenaming(); |
---|
| 327 | TypeEnvironment env; |
---|
| 328 | Expression * newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
| 329 | finishExpr( newExpr, env, untyped->env ); |
---|
| 330 | delete untyped; |
---|
| 331 | untyped = newExpr; |
---|
| 332 | } |
---|
| 333 | |
---|
[5170d95] | 334 | void findSingleExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
[c71b256] | 335 | findKindExpression( untyped, indexer, "", standardAlternativeFilter ); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) { |
---|
| 339 | assert( untyped && type ); |
---|
[2a08c25] | 340 | // transfer location to generated cast for error purposes |
---|
| 341 | CodeLocation location = untyped->location; |
---|
[c71b256] | 342 | untyped = new CastExpr( untyped, type ); |
---|
[2a08c25] | 343 | untyped->location = location; |
---|
[c71b256] | 344 | findSingleExpression( untyped, indexer ); |
---|
| 345 | removeExtraneousCast( untyped, indexer ); |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | namespace { |
---|
| 349 | bool isIntegralType( const Alternative & alt ) { |
---|
| 350 | Type * type = alt.expr->result; |
---|
[a32b204] | 351 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 352 | return true; |
---|
[5170d95] | 353 | } else if ( BasicType * bt = dynamic_cast< BasicType * >( type ) ) { |
---|
[a32b204] | 354 | return bt->isInteger(); |
---|
[89e6ffc] | 355 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { |
---|
| 356 | return true; |
---|
[a32b204] | 357 | } else { |
---|
| 358 | return false; |
---|
| 359 | } // if |
---|
| 360 | } |
---|
[71f4e4f] | 361 | |
---|
[5170d95] | 362 | void findIntegralExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
[8587878e] | 363 | findKindExpression( untyped, indexer, "condition", isIntegralType ); |
---|
[a32b204] | 364 | } |
---|
| 365 | } |
---|
[71f4e4f] | 366 | |
---|
[2a6292d] | 367 | |
---|
| 368 | bool isStructOrUnion( const Alternative & alt ) { |
---|
| 369 | Type * t = alt.expr->result->stripReferences(); |
---|
| 370 | return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | void resolveWithExprs( std::list< Declaration * > & translationUnit ) { |
---|
| 374 | PassVisitor<ResolveWithExprs> resolver; |
---|
| 375 | acceptAll( translationUnit, resolver ); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | void ResolveWithExprs::resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ) { |
---|
| 379 | for ( Expression *& expr : withExprs ) { |
---|
| 380 | // only struct- and union-typed expressions are viable candidates |
---|
| 381 | findKindExpression( expr, indexer, "with statement", isStructOrUnion ); |
---|
| 382 | |
---|
| 383 | // if with expression might be impure, create a temporary so that it is evaluated once |
---|
| 384 | if ( Tuples::maybeImpure( expr ) ) { |
---|
| 385 | static UniqueName tmpNamer( "_with_tmp_" ); |
---|
| 386 | ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) ); |
---|
| 387 | expr = new VariableExpr( tmp ); |
---|
| 388 | newStmts.push_back( new DeclStmt( tmp ) ); |
---|
| 389 | if ( InitTweak::isConstructable( tmp->type ) ) { |
---|
| 390 | // generate ctor/dtor and resolve them |
---|
| 391 | tmp->init = InitTweak::genCtorInit( tmp ); |
---|
| 392 | tmp->accept( *visitor ); |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | void ResolveWithExprs::previsit( WithStmt * withStmt ) { |
---|
| 399 | resolveWithExprs( withStmt->exprs, stmtsToAddBefore ); |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | void ResolveWithExprs::previsit( FunctionDecl * functionDecl ) { |
---|
| 403 | { |
---|
| 404 | // resolve with-exprs with parameters in scope and add any newly generated declarations to the |
---|
| 405 | // front of the function body. |
---|
| 406 | auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this](){ indexer.leaveScope(); } ); |
---|
| 407 | indexer.addFunctionType( functionDecl->type ); |
---|
| 408 | std::list< Statement * > newStmts; |
---|
| 409 | resolveWithExprs( functionDecl->withExprs, newStmts ); |
---|
| 410 | if ( functionDecl->statements ) { |
---|
| 411 | functionDecl->statements->kids.splice( functionDecl->statements->kids.begin(), newStmts ); |
---|
| 412 | } else { |
---|
| 413 | assertf( functionDecl->withExprs.empty() && newStmts.empty(), "Function %s without a body has with-clause and/or generated with declarations.", functionDecl->name.c_str() ); |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | |
---|
[d76c588] | 418 | void Resolver_old::previsit( ObjectDecl * objectDecl ) { |
---|
[4864a73] | 419 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that |
---|
| 420 | // class-variable initContext is changed multiple time because the LHS is analysed twice. |
---|
| 421 | // The second analysis changes initContext because of a function type can contain object |
---|
| 422 | // declarations in the return and parameter types. So each value of initContext is |
---|
[6d6e829] | 423 | // retained, so the type on the first analysis is preserved and used for selecting the RHS. |
---|
[a4ca48c] | 424 | GuardValue( currentObject ); |
---|
[e4d829b] | 425 | currentObject = CurrentObject( objectDecl->get_type() ); |
---|
| 426 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
[a436947] | 427 | // enumerator initializers should not use the enum type to initialize, since |
---|
| 428 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
[9e7236f4] | 429 | // TODO: BasicType::SignedInt may not longer be true |
---|
[e4d829b] | 430 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
[a436947] | 431 | } |
---|
[bfbf97f] | 432 | } |
---|
| 433 | |
---|
[40e636a] | 434 | template< typename PtrType > |
---|
[d76c588] | 435 | void Resolver_old::handlePtrType( PtrType * type ) { |
---|
[40e636a] | 436 | if ( type->get_dimension() ) { |
---|
[2bfc6b2] | 437 | findSingleExpression( type->dimension, Validate::SizeType->clone(), indexer ); |
---|
[d1d17f5] | 438 | } |
---|
[40e636a] | 439 | } |
---|
| 440 | |
---|
[d76c588] | 441 | void Resolver_old::previsit( ArrayType * at ) { |
---|
[40e636a] | 442 | handlePtrType( at ); |
---|
[a32b204] | 443 | } |
---|
[94b4364] | 444 | |
---|
[d76c588] | 445 | void Resolver_old::previsit( PointerType * pt ) { |
---|
[40e636a] | 446 | handlePtrType( pt ); |
---|
| 447 | } |
---|
| 448 | |
---|
[d76c588] | 449 | void Resolver_old::previsit( FunctionDecl * functionDecl ) { |
---|
[d9a0e76] | 450 | #if 0 |
---|
[a4ca48c] | 451 | std::cerr << "resolver visiting functiondecl "; |
---|
| 452 | functionDecl->print( std::cerr ); |
---|
| 453 | std::cerr << std::endl; |
---|
[d9a0e76] | 454 | #endif |
---|
[a4ca48c] | 455 | GuardValue( functionReturn ); |
---|
[60914351] | 456 | functionReturn = ResolvExpr::extractResultType( functionDecl->type ); |
---|
[a4ca48c] | 457 | } |
---|
[88d1066] | 458 | |
---|
[d76c588] | 459 | void Resolver_old::postvisit( FunctionDecl * functionDecl ) { |
---|
[4864a73] | 460 | // default value expressions have an environment which shouldn't be there and trips up |
---|
[6d6e829] | 461 | // later passes. |
---|
[cde3891] | 462 | // xxx - it might be necessary to somehow keep the information from this environment, but I |
---|
[6d6e829] | 463 | // can't currently see how it's useful. |
---|
[c28a038d] | 464 | for ( Declaration * d : functionDecl->type->parameters ) { |
---|
[88d1066] | 465 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { |
---|
[c28a038d] | 466 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->init ) ) { |
---|
| 467 | delete init->value->env; |
---|
| 468 | init->value->env = nullptr; |
---|
[88d1066] | 469 | } |
---|
| 470 | } |
---|
| 471 | } |
---|
[a32b204] | 472 | } |
---|
[51b7345] | 473 | |
---|
[d76c588] | 474 | void Resolver_old::previsit( EnumDecl * ) { |
---|
[a436947] | 475 | // in case we decide to allow nested enums |
---|
[a4ca48c] | 476 | GuardValue( inEnumDecl ); |
---|
[a436947] | 477 | inEnumDecl = true; |
---|
| 478 | } |
---|
| 479 | |
---|
[d76c588] | 480 | void Resolver_old::previsit( StaticAssertDecl * assertDecl ) { |
---|
[bd87b138] | 481 | findIntegralExpression( assertDecl->condition, indexer ); |
---|
| 482 | } |
---|
| 483 | |
---|
[d76c588] | 484 | void Resolver_old::previsit( ExprStmt * exprStmt ) { |
---|
[a4ca48c] | 485 | visit_children = false; |
---|
[08da53d] | 486 | assertf( exprStmt->expr, "ExprStmt has null Expression in resolver" ); |
---|
| 487 | findVoidExpression( exprStmt->expr, indexer ); |
---|
[a32b204] | 488 | } |
---|
[51b7345] | 489 | |
---|
[d76c588] | 490 | void Resolver_old::previsit( AsmExpr * asmExpr ) { |
---|
[a4ca48c] | 491 | visit_children = false; |
---|
[08da53d] | 492 | findVoidExpression( asmExpr->operand, indexer ); |
---|
[7f5566b] | 493 | } |
---|
| 494 | |
---|
[d76c588] | 495 | void Resolver_old::previsit( AsmStmt * asmStmt ) { |
---|
[a4ca48c] | 496 | visit_children = false; |
---|
| 497 | acceptAll( asmStmt->get_input(), *visitor ); |
---|
| 498 | acceptAll( asmStmt->get_output(), *visitor ); |
---|
[7f5566b] | 499 | } |
---|
| 500 | |
---|
[d76c588] | 501 | void Resolver_old::previsit( IfStmt * ifStmt ) { |
---|
[8587878e] | 502 | findIntegralExpression( ifStmt->condition, indexer ); |
---|
[a32b204] | 503 | } |
---|
[51b7345] | 504 | |
---|
[3b0bc16] | 505 | void Resolver_old::previsit( WhileDoStmt * whileDoStmt ) { |
---|
| 506 | findIntegralExpression( whileDoStmt->condition, indexer ); |
---|
[a32b204] | 507 | } |
---|
[51b7345] | 508 | |
---|
[d76c588] | 509 | void Resolver_old::previsit( ForStmt * forStmt ) { |
---|
[08da53d] | 510 | if ( forStmt->condition ) { |
---|
[8587878e] | 511 | findIntegralExpression( forStmt->condition, indexer ); |
---|
[a32b204] | 512 | } // if |
---|
[71f4e4f] | 513 | |
---|
[08da53d] | 514 | if ( forStmt->increment ) { |
---|
| 515 | findVoidExpression( forStmt->increment, indexer ); |
---|
[a32b204] | 516 | } // if |
---|
| 517 | } |
---|
[51b7345] | 518 | |
---|
[d76c588] | 519 | void Resolver_old::previsit( SwitchStmt * switchStmt ) { |
---|
[a4ca48c] | 520 | GuardValue( currentObject ); |
---|
[08da53d] | 521 | findIntegralExpression( switchStmt->condition, indexer ); |
---|
[71f4e4f] | 522 | |
---|
[08da53d] | 523 | currentObject = CurrentObject( switchStmt->condition->result ); |
---|
[a32b204] | 524 | } |
---|
[51b7345] | 525 | |
---|
[d76c588] | 526 | void Resolver_old::previsit( CaseStmt * caseStmt ) { |
---|
[cdb990a] | 527 | if ( caseStmt->condition ) { |
---|
[e4d829b] | 528 | std::list< InitAlternative > initAlts = currentObject.getOptions(); |
---|
| 529 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); |
---|
[08da53d] | 530 | // must remove cast from case statement because RangeExpr cannot be cast. |
---|
| 531 | Expression * newExpr = new CastExpr( caseStmt->condition, initAlts.front().type->clone() ); |
---|
| 532 | findSingleExpression( newExpr, indexer ); |
---|
[cdb990a] | 533 | // case condition cannot have a cast in C, so it must be removed, regardless of whether it performs a conversion. |
---|
| 534 | // Ideally we would perform the conversion internally here. |
---|
| 535 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( newExpr ) ) { |
---|
| 536 | newExpr = castExpr->arg; |
---|
| 537 | castExpr->arg = nullptr; |
---|
| 538 | std::swap( newExpr->env, castExpr->env ); |
---|
| 539 | delete castExpr; |
---|
| 540 | } |
---|
| 541 | caseStmt->condition = newExpr; |
---|
[32b8144] | 542 | } |
---|
[a32b204] | 543 | } |
---|
[51b7345] | 544 | |
---|
[d76c588] | 545 | void Resolver_old::previsit( BranchStmt * branchStmt ) { |
---|
[a4ca48c] | 546 | visit_children = false; |
---|
[de62360d] | 547 | // must resolve the argument for a computed goto |
---|
| 548 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
[08da53d] | 549 | if ( branchStmt->computedTarget ) { |
---|
| 550 | // computed goto argument is void * |
---|
| 551 | findSingleExpression( branchStmt->computedTarget, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), indexer ); |
---|
[de62360d] | 552 | } // if |
---|
| 553 | } // if |
---|
| 554 | } |
---|
| 555 | |
---|
[d76c588] | 556 | void Resolver_old::previsit( ReturnStmt * returnStmt ) { |
---|
[a4ca48c] | 557 | visit_children = false; |
---|
[08da53d] | 558 | if ( returnStmt->expr ) { |
---|
| 559 | findSingleExpression( returnStmt->expr, functionReturn->clone(), indexer ); |
---|
[a32b204] | 560 | } // if |
---|
| 561 | } |
---|
[51b7345] | 562 | |
---|
[d76c588] | 563 | void Resolver_old::previsit( ThrowStmt * throwStmt ) { |
---|
[a4ca48c] | 564 | visit_children = false; |
---|
[cbce272] | 565 | // TODO: Replace *exception type with &exception type. |
---|
[307a732] | 566 | if ( throwStmt->get_expr() ) { |
---|
[3090127] | 567 | const StructDecl * exception_decl = indexer.lookupStruct( "__cfaehm_base_exception_t" ); |
---|
[cbce272] | 568 | assert( exception_decl ); |
---|
[8fd52e90] | 569 | Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, const_cast<StructDecl *>(exception_decl) ) ); |
---|
[08da53d] | 570 | findSingleExpression( throwStmt->expr, exceptType, indexer ); |
---|
[307a732] | 571 | } |
---|
| 572 | } |
---|
| 573 | |
---|
[d76c588] | 574 | void Resolver_old::previsit( CatchStmt * catchStmt ) { |
---|
[3b0bc16] | 575 | // Until we are very sure this invarent (ifs that move between passes have then) |
---|
[3b9c674] | 576 | // holds, check it. This allows a check for when to decode the mangling. |
---|
| 577 | if ( IfStmt * ifStmt = dynamic_cast<IfStmt *>( catchStmt->body ) ) { |
---|
[3b0bc16] | 578 | assert( ifStmt->then ); |
---|
[3b9c674] | 579 | } |
---|
| 580 | // Encode the catchStmt so the condition can see the declaration. |
---|
[08da53d] | 581 | if ( catchStmt->cond ) { |
---|
[3b9c674] | 582 | IfStmt * ifStmt = new IfStmt( catchStmt->cond, nullptr, catchStmt->body ); |
---|
| 583 | catchStmt->cond = nullptr; |
---|
| 584 | catchStmt->body = ifStmt; |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | void Resolver_old::postvisit( CatchStmt * catchStmt ) { |
---|
| 589 | // Decode the catchStmt so everything is stored properly. |
---|
| 590 | IfStmt * ifStmt = dynamic_cast<IfStmt *>( catchStmt->body ); |
---|
[3b0bc16] | 591 | if ( nullptr != ifStmt && nullptr == ifStmt->then ) { |
---|
[3b9c674] | 592 | assert( ifStmt->condition ); |
---|
[3b0bc16] | 593 | assert( ifStmt->else_ ); |
---|
[3b9c674] | 594 | catchStmt->cond = ifStmt->condition; |
---|
[3b0bc16] | 595 | catchStmt->body = ifStmt->else_; |
---|
[3b9c674] | 596 | ifStmt->condition = nullptr; |
---|
[3b0bc16] | 597 | ifStmt->else_ = nullptr; |
---|
[3b9c674] | 598 | delete ifStmt; |
---|
[cbce272] | 599 | } |
---|
| 600 | } |
---|
| 601 | |
---|
[1dcd9554] | 602 | template< typename iterator_t > |
---|
| 603 | inline bool advance_to_mutex( iterator_t & it, const iterator_t & end ) { |
---|
| 604 | while( it != end && !(*it)->get_type()->get_mutex() ) { |
---|
| 605 | it++; |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | return it != end; |
---|
| 609 | } |
---|
| 610 | |
---|
[d76c588] | 611 | void Resolver_old::previsit( WaitForStmt * stmt ) { |
---|
[8f98b78] | 612 | visit_children = false; |
---|
[1dcd9554] | 613 | |
---|
| 614 | // Resolve all clauses first |
---|
| 615 | for( auto& clause : stmt->clauses ) { |
---|
| 616 | |
---|
| 617 | TypeEnvironment env; |
---|
[8f98b78] | 618 | AlternativeFinder funcFinder( indexer, env ); |
---|
[1dcd9554] | 619 | |
---|
| 620 | // Find all alternatives for a function in canonical form |
---|
| 621 | funcFinder.findWithAdjustment( clause.target.function ); |
---|
| 622 | |
---|
| 623 | if ( funcFinder.get_alternatives().empty() ) { |
---|
| 624 | stringstream ss; |
---|
| 625 | ss << "Use of undeclared indentifier '"; |
---|
| 626 | ss << strict_dynamic_cast<NameExpr*>( clause.target.function )->name; |
---|
| 627 | ss << "' in call to waitfor"; |
---|
[a16764a6] | 628 | SemanticError( stmt->location, ss.str() ); |
---|
[1dcd9554] | 629 | } |
---|
| 630 | |
---|
[b9f383f] | 631 | if(clause.target.arguments.empty()) { |
---|
| 632 | SemanticError( stmt->location, "Waitfor clause must have at least one mutex parameter"); |
---|
| 633 | } |
---|
| 634 | |
---|
[1dcd9554] | 635 | // Find all alternatives for all arguments in canonical form |
---|
[bd4f2e9] | 636 | std::vector< AlternativeFinder > argAlternatives; |
---|
[1dcd9554] | 637 | funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) ); |
---|
| 638 | |
---|
| 639 | // List all combinations of arguments |
---|
[bd4f2e9] | 640 | std::vector< AltList > possibilities; |
---|
[1dcd9554] | 641 | combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); |
---|
| 642 | |
---|
| 643 | AltList func_candidates; |
---|
| 644 | std::vector< AltList > args_candidates; |
---|
| 645 | |
---|
| 646 | // For every possible function : |
---|
| 647 | // try matching the arguments to the parameters |
---|
| 648 | // not the other way around because we have more arguments than parameters |
---|
[a16764a6] | 649 | SemanticErrorException errors; |
---|
[1dcd9554] | 650 | for ( Alternative & func : funcFinder.get_alternatives() ) { |
---|
| 651 | try { |
---|
| 652 | PointerType * pointer = dynamic_cast< PointerType* >( func.expr->get_result()->stripReferences() ); |
---|
| 653 | if( !pointer ) { |
---|
[a16764a6] | 654 | SemanticError( func.expr->get_result(), "candidate not viable: not a pointer type\n" ); |
---|
[1dcd9554] | 655 | } |
---|
| 656 | |
---|
| 657 | FunctionType * function = dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
| 658 | if( !function ) { |
---|
[a16764a6] | 659 | SemanticError( pointer->get_base(), "candidate not viable: not a function type\n" ); |
---|
[1dcd9554] | 660 | } |
---|
| 661 | |
---|
| 662 | |
---|
| 663 | { |
---|
| 664 | auto param = function->parameters.begin(); |
---|
| 665 | auto param_end = function->parameters.end(); |
---|
| 666 | |
---|
| 667 | if( !advance_to_mutex( param, param_end ) ) { |
---|
[a16764a6] | 668 | SemanticError(function, "candidate function not viable: no mutex parameters\n"); |
---|
[1dcd9554] | 669 | } |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | Alternative newFunc( func ); |
---|
| 673 | // Strip reference from function |
---|
[a181494] | 674 | referenceToRvalueConversion( newFunc.expr, newFunc.cost ); |
---|
[1dcd9554] | 675 | |
---|
| 676 | // For all the set of arguments we have try to match it with the parameter of the current function alternative |
---|
| 677 | for ( auto & argsList : possibilities ) { |
---|
| 678 | |
---|
| 679 | try { |
---|
| 680 | // Declare data structures need for resolution |
---|
| 681 | OpenVarSet openVars; |
---|
| 682 | AssertionSet resultNeed, resultHave; |
---|
[6f326b1] | 683 | TypeEnvironment resultEnv( func.env ); |
---|
| 684 | makeUnifiableVars( function, openVars, resultNeed ); |
---|
| 685 | // add all type variables as open variables now so that those not used in the parameter |
---|
| 686 | // list are still considered open. |
---|
| 687 | resultEnv.add( function->forall ); |
---|
[1dcd9554] | 688 | |
---|
| 689 | // Load type variables from arguemnts into one shared space |
---|
| 690 | simpleCombineEnvironments( argsList.begin(), argsList.end(), resultEnv ); |
---|
| 691 | |
---|
| 692 | // Make sure we don't widen any existing bindings |
---|
[d286cf68] | 693 | resultEnv.forbidWidening(); |
---|
[c5283ba] | 694 | |
---|
[1dcd9554] | 695 | // Find any unbound type variables |
---|
| 696 | resultEnv.extractOpenVars( openVars ); |
---|
| 697 | |
---|
| 698 | auto param = function->parameters.begin(); |
---|
| 699 | auto param_end = function->parameters.end(); |
---|
| 700 | |
---|
[c5283ba] | 701 | int n_mutex_param = 0; |
---|
[b9f383f] | 702 | |
---|
[1dcd9554] | 703 | // For every arguments of its set, check if it matches one of the parameter |
---|
| 704 | // The order is important |
---|
| 705 | for( auto & arg : argsList ) { |
---|
| 706 | |
---|
| 707 | // Ignore non-mutex arguments |
---|
| 708 | if( !advance_to_mutex( param, param_end ) ) { |
---|
| 709 | // We ran out of parameters but still have arguments |
---|
| 710 | // this function doesn't match |
---|
[c5283ba] | 711 | SemanticError( function, toString("candidate function not viable: too many mutex arguments, expected ", n_mutex_param, "\n" )); |
---|
[1dcd9554] | 712 | } |
---|
| 713 | |
---|
[c5283ba] | 714 | n_mutex_param++; |
---|
[b9f383f] | 715 | |
---|
[1dcd9554] | 716 | // Check if the argument matches the parameter type in the current scope |
---|
[b9f383f] | 717 | if( ! unify( arg.expr->get_result(), (*param)->get_type(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) { |
---|
[1dcd9554] | 718 | // Type doesn't match |
---|
| 719 | stringstream ss; |
---|
| 720 | ss << "candidate function not viable: no known convertion from '"; |
---|
| 721 | (*param)->get_type()->print( ss ); |
---|
[b9f383f] | 722 | ss << "' to '"; |
---|
| 723 | arg.expr->get_result()->print( ss ); |
---|
[5248789] | 724 | ss << "' with env '"; |
---|
| 725 | resultEnv.print(ss); |
---|
[1dcd9554] | 726 | ss << "'\n"; |
---|
[a16764a6] | 727 | SemanticError( function, ss.str() ); |
---|
[1dcd9554] | 728 | } |
---|
| 729 | |
---|
| 730 | param++; |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | // All arguments match ! |
---|
| 734 | |
---|
| 735 | // Check if parameters are missing |
---|
| 736 | if( advance_to_mutex( param, param_end ) ) { |
---|
[c5283ba] | 737 | do { |
---|
| 738 | n_mutex_param++; |
---|
| 739 | param++; |
---|
| 740 | } while( advance_to_mutex( param, param_end ) ); |
---|
| 741 | |
---|
[1dcd9554] | 742 | // We ran out of arguments but still have parameters left |
---|
| 743 | // this function doesn't match |
---|
[c5283ba] | 744 | SemanticError( function, toString("candidate function not viable: too few mutex arguments, expected ", n_mutex_param, "\n" )); |
---|
[1dcd9554] | 745 | } |
---|
| 746 | |
---|
| 747 | // All parameters match ! |
---|
| 748 | |
---|
| 749 | // Finish the expressions to tie in the proper environments |
---|
| 750 | finishExpr( newFunc.expr, resultEnv ); |
---|
| 751 | for( Alternative & alt : argsList ) { |
---|
| 752 | finishExpr( alt.expr, resultEnv ); |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | // This is a match store it and save it for later |
---|
| 756 | func_candidates.push_back( newFunc ); |
---|
| 757 | args_candidates.push_back( argsList ); |
---|
| 758 | |
---|
| 759 | } |
---|
[5170d95] | 760 | catch( SemanticErrorException & e ) { |
---|
[1dcd9554] | 761 | errors.append( e ); |
---|
| 762 | } |
---|
| 763 | } |
---|
| 764 | } |
---|
[5170d95] | 765 | catch( SemanticErrorException & e ) { |
---|
[1dcd9554] | 766 | errors.append( e ); |
---|
| 767 | } |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | // Make sure we got the right number of arguments |
---|
[a16764a6] | 771 | if( func_candidates.empty() ) { SemanticErrorException top( stmt->location, "No alternatives for function in call to waitfor" ); top.append( errors ); throw top; } |
---|
| 772 | if( args_candidates.empty() ) { SemanticErrorException top( stmt->location, "No alternatives for arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
| 773 | if( func_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous function in call to waitfor" ); top.append( errors ); throw top; } |
---|
| 774 | if( args_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
[c71b256] | 775 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used. |
---|
[1dcd9554] | 776 | |
---|
| 777 | // Swap the results from the alternative with the unresolved values. |
---|
| 778 | // Alternatives will handle deletion on destruction |
---|
| 779 | std::swap( clause.target.function, func_candidates.front().expr ); |
---|
| 780 | for( auto arg_pair : group_iterate( clause.target.arguments, args_candidates.front() ) ) { |
---|
| 781 | std::swap ( std::get<0>( arg_pair), std::get<1>( arg_pair).expr ); |
---|
| 782 | } |
---|
| 783 | |
---|
| 784 | // Resolve the conditions as if it were an IfStmt |
---|
| 785 | // Resolve the statments normally |
---|
[08da53d] | 786 | findSingleExpression( clause.condition, this->indexer ); |
---|
[8f98b78] | 787 | clause.statement->accept( *visitor ); |
---|
[1dcd9554] | 788 | } |
---|
| 789 | |
---|
| 790 | |
---|
| 791 | if( stmt->timeout.statement ) { |
---|
| 792 | // Resolve the timeout as an size_t for now |
---|
| 793 | // Resolve the conditions as if it were an IfStmt |
---|
| 794 | // Resolve the statments normally |
---|
[08da53d] | 795 | findSingleExpression( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer ); |
---|
| 796 | findSingleExpression( stmt->timeout.condition, this->indexer ); |
---|
[8f98b78] | 797 | stmt->timeout.statement->accept( *visitor ); |
---|
[1dcd9554] | 798 | } |
---|
| 799 | |
---|
| 800 | if( stmt->orelse.statement ) { |
---|
| 801 | // Resolve the conditions as if it were an IfStmt |
---|
| 802 | // Resolve the statments normally |
---|
[08da53d] | 803 | findSingleExpression( stmt->orelse.condition, this->indexer ); |
---|
[8f98b78] | 804 | stmt->orelse.statement->accept( *visitor ); |
---|
[1dcd9554] | 805 | } |
---|
| 806 | } |
---|
| 807 | |
---|
[60aaa51d] | 808 | bool isCharType( Type * t ) { |
---|
[b5c5684] | 809 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
[71f4e4f] | 810 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
[b5c5684] | 811 | bt->get_kind() == BasicType::UnsignedChar; |
---|
| 812 | } |
---|
| 813 | return false; |
---|
| 814 | } |
---|
| 815 | |
---|
[d76c588] | 816 | void Resolver_old::previsit( SingleInit * singleInit ) { |
---|
[a4ca48c] | 817 | visit_children = false; |
---|
[62423350] | 818 | // resolve initialization using the possibilities as determined by the currentObject cursor |
---|
[0a22cda] | 819 | Expression * newExpr = new UntypedInitExpr( singleInit->value, currentObject.getOptions() ); |
---|
[08da53d] | 820 | findSingleExpression( newExpr, indexer ); |
---|
[e3e16bc] | 821 | InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); |
---|
[62423350] | 822 | |
---|
| 823 | // move cursor to the object that is actually initialized |
---|
[e4d829b] | 824 | currentObject.setNext( initExpr->get_designation() ); |
---|
[62423350] | 825 | |
---|
| 826 | // discard InitExpr wrapper and retain relevant pieces |
---|
[08da53d] | 827 | newExpr = initExpr->expr; |
---|
| 828 | initExpr->expr = nullptr; |
---|
| 829 | std::swap( initExpr->env, newExpr->env ); |
---|
[cde3891] | 830 | // InitExpr may have inferParams in the case where the expression specializes a function |
---|
| 831 | // pointer, and newExpr may already have inferParams of its own, so a simple swap is not |
---|
[6d6e829] | 832 | // sufficient. |
---|
[cdb990a] | 833 | newExpr->spliceInferParams( initExpr ); |
---|
[e4d829b] | 834 | delete initExpr; |
---|
| 835 | |
---|
[cde3891] | 836 | // get the actual object's type (may not exactly match what comes back from the resolver |
---|
[6d6e829] | 837 | // due to conversions) |
---|
[62423350] | 838 | Type * initContext = currentObject.getCurrentType(); |
---|
| 839 | |
---|
[0a22cda] | 840 | removeExtraneousCast( newExpr, indexer ); |
---|
| 841 | |
---|
[62423350] | 842 | // check if actual object's type is char[] |
---|
| 843 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
| 844 | if ( isCharType( at->get_base() ) ) { |
---|
| 845 | // check if the resolved type is char * |
---|
| 846 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
| 847 | if ( isCharType( pt->get_base() ) ) { |
---|
[5170d95] | 848 | if ( CastExpr * ce = dynamic_cast< CastExpr * >( newExpr ) ) { |
---|
[cde3891] | 849 | // strip cast if we're initializing a char[] with a char *, |
---|
[6d6e829] | 850 | // e.g. char x[] = "hello"; |
---|
[0a22cda] | 851 | newExpr = ce->get_arg(); |
---|
| 852 | ce->set_arg( nullptr ); |
---|
| 853 | std::swap( ce->env, newExpr->env ); |
---|
| 854 | delete ce; |
---|
| 855 | } |
---|
[62423350] | 856 | } |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | } |
---|
[94b4364] | 860 | |
---|
[62423350] | 861 | // set initializer expr to resolved express |
---|
[0a22cda] | 862 | singleInit->value = newExpr; |
---|
[62423350] | 863 | |
---|
| 864 | // move cursor to next object in preparation for next initializer |
---|
| 865 | currentObject.increment(); |
---|
| 866 | } |
---|
[94b4364] | 867 | |
---|
[d76c588] | 868 | void Resolver_old::previsit( ListInit * listInit ) { |
---|
[a4ca48c] | 869 | visit_children = false; |
---|
[62423350] | 870 | // move cursor into brace-enclosed initializer-list |
---|
[e4d829b] | 871 | currentObject.enterListInit(); |
---|
[cde3891] | 872 | // xxx - fix this so that the list isn't copied, iterator should be used to change current |
---|
[6d6e829] | 873 | // element |
---|
[e4d829b] | 874 | std::list<Designation *> newDesignations; |
---|
| 875 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { |
---|
[cde3891] | 876 | // iterate designations and initializers in pairs, moving the cursor to the current |
---|
[6d6e829] | 877 | // designated object and resolving the initializer against that object. |
---|
[e4d829b] | 878 | Designation * des = std::get<0>(p); |
---|
| 879 | Initializer * init = std::get<1>(p); |
---|
| 880 | newDesignations.push_back( currentObject.findNext( des ) ); |
---|
[a4ca48c] | 881 | init->accept( *visitor ); |
---|
[b5c5684] | 882 | } |
---|
[62423350] | 883 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list |
---|
[e4d829b] | 884 | listInit->get_designations() = newDesignations; // xxx - memory management |
---|
| 885 | currentObject.exitListInit(); |
---|
| 886 | |
---|
[62423350] | 887 | // xxx - this part has not be folded into CurrentObject yet |
---|
[e4d829b] | 888 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
| 889 | // Type * base = tt->get_baseType()->get_base(); |
---|
| 890 | // if ( base ) { |
---|
| 891 | // // know the implementation type, so try using that as the initContext |
---|
| 892 | // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); |
---|
| 893 | // currentObject = &tmpObj; |
---|
| 894 | // visit( listInit ); |
---|
| 895 | // } else { |
---|
| 896 | // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
| 897 | // Parent::visit( listInit ); |
---|
| 898 | // } |
---|
| 899 | // } else { |
---|
[a32b204] | 900 | } |
---|
[71f4e4f] | 901 | |
---|
[f1e012b] | 902 | // ConstructorInit - fall back on C-style initializer |
---|
[d76c588] | 903 | void Resolver_old::fallbackInit( ConstructorInit * ctorInit ) { |
---|
[f1e012b] | 904 | // could not find valid constructor, or found an intrinsic constructor |
---|
| 905 | // fall back on C-style initializer |
---|
| 906 | delete ctorInit->get_ctor(); |
---|
[6d6e829] | 907 | ctorInit->set_ctor( nullptr ); |
---|
[71a145de] | 908 | delete ctorInit->get_dtor(); |
---|
[6d6e829] | 909 | ctorInit->set_dtor( nullptr ); |
---|
[a4ca48c] | 910 | maybeAccept( ctorInit->get_init(), *visitor ); |
---|
[f1e012b] | 911 | } |
---|
| 912 | |
---|
[1d2b64f] | 913 | // needs to be callable from outside the resolver, so this is a standalone function |
---|
| 914 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { |
---|
| 915 | assert( ctorInit ); |
---|
[d76c588] | 916 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
[1d2b64f] | 917 | ctorInit->accept( resolver ); |
---|
| 918 | } |
---|
| 919 | |
---|
| 920 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { |
---|
| 921 | assert( stmtExpr ); |
---|
[d76c588] | 922 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
[1d2b64f] | 923 | stmtExpr->accept( resolver ); |
---|
[5e2c348] | 924 | stmtExpr->computeResult(); |
---|
[dd05e12] | 925 | // xxx - aggregate the environments from all statements? Possibly in AlternativeFinder instead? |
---|
[1d2b64f] | 926 | } |
---|
| 927 | |
---|
[d76c588] | 928 | void Resolver_old::previsit( ConstructorInit * ctorInit ) { |
---|
[a4ca48c] | 929 | visit_children = false; |
---|
[1ba88a0] | 930 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
[dd05e12] | 931 | maybeAccept( ctorInit->ctor, *visitor ); |
---|
| 932 | maybeAccept( ctorInit->dtor, *visitor ); |
---|
[071a31a] | 933 | |
---|
[5b2f5bb] | 934 | // found a constructor - can get rid of C-style initializer |
---|
[dd05e12] | 935 | delete ctorInit->init; |
---|
| 936 | ctorInit->init = nullptr; |
---|
[ec79847] | 937 | |
---|
| 938 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
| 939 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
| 940 | // to clean up generated code. |
---|
[dd05e12] | 941 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { |
---|
| 942 | delete ctorInit->ctor; |
---|
| 943 | ctorInit->ctor = nullptr; |
---|
[ec79847] | 944 | } |
---|
[f9cebb5] | 945 | |
---|
[dd05e12] | 946 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { |
---|
| 947 | delete ctorInit->dtor; |
---|
| 948 | ctorInit->dtor = nullptr; |
---|
[ec79847] | 949 | } |
---|
[a465caf] | 950 | |
---|
| 951 | // xxx - todo -- what about arrays? |
---|
[6d6e829] | 952 | // if ( dtor == nullptr && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
[a465caf] | 953 | // // can reduce the constructor down to a SingleInit using the |
---|
| 954 | // // second argument from the ctor call, since |
---|
| 955 | // delete ctorInit->get_ctor(); |
---|
[6d6e829] | 956 | // ctorInit->set_ctor( nullptr ); |
---|
[a465caf] | 957 | |
---|
| 958 | // Expression * arg = |
---|
| 959 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
| 960 | // } |
---|
[71f4e4f] | 961 | } |
---|
[d76c588] | 962 | |
---|
| 963 | /////////////////////////////////////////////////////////////////////////// |
---|
| 964 | // |
---|
| 965 | // *** NEW RESOLVER *** |
---|
| 966 | // |
---|
| 967 | /////////////////////////////////////////////////////////////////////////// |
---|
| 968 | |
---|
[99d4584] | 969 | namespace { |
---|
| 970 | /// Finds deleted expressions in an expression tree |
---|
[2dda05d] | 971 | struct DeleteFinder_new final : public ast::WithShortCircuiting, public ast::WithVisitorRef<DeleteFinder_new> { |
---|
[e6b42e7] | 972 | const ast::DeletedExpr * result = nullptr; |
---|
[99d4584] | 973 | |
---|
| 974 | void previsit( const ast::DeletedExpr * expr ) { |
---|
[e6b42e7] | 975 | if ( result ) { visit_children = false; } |
---|
| 976 | else { result = expr; } |
---|
[99d4584] | 977 | } |
---|
| 978 | |
---|
[361bf01] | 979 | void previsit( const ast::Expr * expr ) { |
---|
[e6b42e7] | 980 | if ( result ) { visit_children = false; } |
---|
[361bf01] | 981 | if (expr->inferred.hasParams()) { |
---|
| 982 | for (auto & imp : expr->inferred.inferParams() ) { |
---|
[2dda05d] | 983 | imp.second.expr->accept(*visitor); |
---|
[361bf01] | 984 | } |
---|
| 985 | } |
---|
[99d4584] | 986 | } |
---|
| 987 | }; |
---|
[d57e349] | 988 | } // anonymous namespace |
---|
| 989 | /// Check if this expression is or includes a deleted expression |
---|
| 990 | const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ) { |
---|
[e6b42e7] | 991 | return ast::Pass<DeleteFinder_new>::read( expr ); |
---|
[d57e349] | 992 | } |
---|
[99d4584] | 993 | |
---|
[d57e349] | 994 | namespace { |
---|
[b7d92b96] | 995 | /// always-accept candidate filter |
---|
| 996 | bool anyCandidate( const Candidate & ) { return true; } |
---|
| 997 | |
---|
[99d4584] | 998 | /// Calls the CandidateFinder and finds the single best candidate |
---|
| 999 | CandidateRef findUnfinishedKindExpression( |
---|
[39d8950] | 1000 | const ast::Expr * untyped, const ResolveContext & context, const std::string & kind, |
---|
[b7d92b96] | 1001 | std::function<bool(const Candidate &)> pred = anyCandidate, ResolvMode mode = {} |
---|
[99d4584] | 1002 | ) { |
---|
| 1003 | if ( ! untyped ) return nullptr; |
---|
| 1004 | |
---|
| 1005 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver |
---|
| 1006 | static unsigned recursion_level = 0; |
---|
| 1007 | |
---|
| 1008 | ++recursion_level; |
---|
| 1009 | ast::TypeEnvironment env; |
---|
[39d8950] | 1010 | CandidateFinder finder( context, env ); |
---|
[99d4584] | 1011 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); |
---|
| 1012 | --recursion_level; |
---|
| 1013 | |
---|
| 1014 | // produce a filtered list of candidates |
---|
| 1015 | CandidateList candidates; |
---|
| 1016 | for ( auto & cand : finder.candidates ) { |
---|
| 1017 | if ( pred( *cand ) ) { candidates.emplace_back( cand ); } |
---|
| 1018 | } |
---|
| 1019 | |
---|
| 1020 | // produce invalid error if no candidates |
---|
| 1021 | if ( candidates.empty() ) { |
---|
[ef5b828] | 1022 | SemanticError( untyped, |
---|
| 1023 | toString( "No reasonable alternatives for ", kind, (kind != "" ? " " : ""), |
---|
[99d4584] | 1024 | "expression: ") ); |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | // search for cheapest candidate |
---|
| 1028 | CandidateList winners; |
---|
| 1029 | bool seen_undeleted = false; |
---|
| 1030 | for ( CandidateRef & cand : candidates ) { |
---|
| 1031 | int c = winners.empty() ? -1 : cand->cost.compare( winners.front()->cost ); |
---|
| 1032 | |
---|
| 1033 | if ( c > 0 ) continue; // skip more expensive than winner |
---|
| 1034 | |
---|
| 1035 | if ( c < 0 ) { |
---|
| 1036 | // reset on new cheapest |
---|
| 1037 | seen_undeleted = ! findDeletedExpr( cand->expr ); |
---|
| 1038 | winners.clear(); |
---|
| 1039 | } else /* if ( c == 0 ) */ { |
---|
| 1040 | if ( findDeletedExpr( cand->expr ) ) { |
---|
| 1041 | // skip deleted expression if already seen one equivalent-cost not |
---|
| 1042 | if ( seen_undeleted ) continue; |
---|
| 1043 | } else if ( ! seen_undeleted ) { |
---|
| 1044 | // replace list of equivalent-cost deleted expressions with one non-deleted |
---|
| 1045 | winners.clear(); |
---|
| 1046 | seen_undeleted = true; |
---|
| 1047 | } |
---|
| 1048 | } |
---|
| 1049 | |
---|
| 1050 | winners.emplace_back( std::move( cand ) ); |
---|
| 1051 | } |
---|
| 1052 | |
---|
| 1053 | // promote candidate.cvtCost to .cost |
---|
[d57e349] | 1054 | promoteCvtCost( winners ); |
---|
[99d4584] | 1055 | |
---|
| 1056 | // produce ambiguous errors, if applicable |
---|
| 1057 | if ( winners.size() != 1 ) { |
---|
| 1058 | std::ostringstream stream; |
---|
[ef5b828] | 1059 | stream << "Cannot choose between " << winners.size() << " alternatives for " |
---|
[99d4584] | 1060 | << kind << (kind != "" ? " " : "") << "expression\n"; |
---|
| 1061 | ast::print( stream, untyped ); |
---|
| 1062 | stream << " Alternatives are:\n"; |
---|
| 1063 | print( stream, winners, 1 ); |
---|
| 1064 | SemanticError( untyped->location, stream.str() ); |
---|
| 1065 | } |
---|
| 1066 | |
---|
| 1067 | // single selected choice |
---|
| 1068 | CandidateRef & choice = winners.front(); |
---|
| 1069 | |
---|
| 1070 | // fail on only expression deleted |
---|
| 1071 | if ( ! seen_undeleted ) { |
---|
| 1072 | SemanticError( untyped->location, choice->expr.get(), "Unique best alternative " |
---|
| 1073 | "includes deleted identifier in " ); |
---|
| 1074 | } |
---|
| 1075 | |
---|
| 1076 | return std::move( choice ); |
---|
| 1077 | } |
---|
| 1078 | |
---|
| 1079 | /// Strips extraneous casts out of an expression |
---|
| 1080 | struct StripCasts_new final { |
---|
[c408483] | 1081 | const ast::Expr * postvisit( const ast::CastExpr * castExpr ) { |
---|
[ef5b828] | 1082 | if ( |
---|
[2890212] | 1083 | castExpr->isGenerated == ast::GeneratedCast |
---|
[ef5b828] | 1084 | && typesCompatible( castExpr->arg->result, castExpr->result ) |
---|
[99d4584] | 1085 | ) { |
---|
| 1086 | // generated cast is the same type as its argument, remove it after keeping env |
---|
[ef5b828] | 1087 | return ast::mutate_field( |
---|
[b7d92b96] | 1088 | castExpr->arg.get(), &ast::Expr::env, castExpr->env ); |
---|
[99d4584] | 1089 | } |
---|
| 1090 | return castExpr; |
---|
| 1091 | } |
---|
| 1092 | |
---|
| 1093 | static void strip( ast::ptr< ast::Expr > & expr ) { |
---|
| 1094 | ast::Pass< StripCasts_new > stripper; |
---|
| 1095 | expr = expr->accept( stripper ); |
---|
| 1096 | } |
---|
| 1097 | }; |
---|
| 1098 | |
---|
[60aaa51d] | 1099 | /// Swaps argument into expression pointer, saving original environment |
---|
| 1100 | void swap_and_save_env( ast::ptr< ast::Expr > & expr, const ast::Expr * newExpr ) { |
---|
| 1101 | ast::ptr< ast::TypeSubstitution > env = expr->env; |
---|
| 1102 | expr.set_and_mutate( newExpr )->env = env; |
---|
| 1103 | } |
---|
| 1104 | |
---|
[b7d92b96] | 1105 | /// Removes cast to type of argument (unlike StripCasts, also handles non-generated casts) |
---|
| 1106 | void removeExtraneousCast( ast::ptr<ast::Expr> & expr, const ast::SymbolTable & symtab ) { |
---|
| 1107 | if ( const ast::CastExpr * castExpr = expr.as< ast::CastExpr >() ) { |
---|
| 1108 | if ( typesCompatible( castExpr->arg->result, castExpr->result, symtab ) ) { |
---|
| 1109 | // cast is to the same type as its argument, remove it |
---|
[60aaa51d] | 1110 | swap_and_save_env( expr, castExpr->arg ); |
---|
[b7d92b96] | 1111 | } |
---|
| 1112 | } |
---|
| 1113 | } |
---|
| 1114 | |
---|
[6668a3e] | 1115 | |
---|
[490fb92e] | 1116 | } // anonymous namespace |
---|
| 1117 | /// Establish post-resolver invariants for expressions |
---|
[ef5b828] | 1118 | void finishExpr( |
---|
| 1119 | ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env, |
---|
[99d4584] | 1120 | const ast::TypeSubstitution * oldenv = nullptr |
---|
| 1121 | ) { |
---|
| 1122 | // set up new type substitution for expression |
---|
[ef5b828] | 1123 | ast::ptr< ast::TypeSubstitution > newenv = |
---|
[99d4584] | 1124 | oldenv ? oldenv : new ast::TypeSubstitution{}; |
---|
| 1125 | env.writeToSubstitution( *newenv.get_and_mutate() ); |
---|
| 1126 | expr.get_and_mutate()->env = std::move( newenv ); |
---|
| 1127 | // remove unncecessary casts |
---|
| 1128 | StripCasts_new::strip( expr ); |
---|
| 1129 | } |
---|
[ef5b828] | 1130 | |
---|
[4b7cce6] | 1131 | ast::ptr< ast::Expr > resolveInVoidContext( |
---|
[39d8950] | 1132 | const ast::Expr * expr, const ResolveContext & context, |
---|
| 1133 | ast::TypeEnvironment & env |
---|
[4b7cce6] | 1134 | ) { |
---|
| 1135 | assertf( expr, "expected a non-null expression" ); |
---|
[ef5b828] | 1136 | |
---|
[4b7cce6] | 1137 | // set up and resolve expression cast to void |
---|
[417117e] | 1138 | ast::ptr< ast::CastExpr > untyped = new ast::CastExpr{ expr }; |
---|
[ef5b828] | 1139 | CandidateRef choice = findUnfinishedKindExpression( |
---|
[39d8950] | 1140 | untyped, context, "", anyCandidate, ResolvMode::withAdjustment() ); |
---|
[ef5b828] | 1141 | |
---|
[4b7cce6] | 1142 | // a cast expression has either 0 or 1 interpretations (by language rules); |
---|
| 1143 | // if 0, an exception has already been thrown, and this code will not run |
---|
| 1144 | const ast::CastExpr * castExpr = choice->expr.strict_as< ast::CastExpr >(); |
---|
| 1145 | env = std::move( choice->env ); |
---|
| 1146 | |
---|
| 1147 | return castExpr->arg; |
---|
| 1148 | } |
---|
[b7d92b96] | 1149 | |
---|
[490fb92e] | 1150 | /// Resolve `untyped` to the expression whose candidate is the best match for a `void` |
---|
[b7d92b96] | 1151 | /// context. |
---|
[ef5b828] | 1152 | ast::ptr< ast::Expr > findVoidExpression( |
---|
[39d8950] | 1153 | const ast::Expr * untyped, const ResolveContext & context |
---|
[b7d92b96] | 1154 | ) { |
---|
| 1155 | ast::TypeEnvironment env; |
---|
[39d8950] | 1156 | ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, context, env ); |
---|
[b7d92b96] | 1157 | finishExpr( newExpr, env, untyped->env ); |
---|
| 1158 | return newExpr; |
---|
| 1159 | } |
---|
| 1160 | |
---|
[490fb92e] | 1161 | namespace { |
---|
[6668a3e] | 1162 | |
---|
[490fb92e] | 1163 | |
---|
[ef5b828] | 1164 | /// resolve `untyped` to the expression whose candidate satisfies `pred` with the |
---|
[99d4584] | 1165 | /// lowest cost, returning the resolved version |
---|
| 1166 | ast::ptr< ast::Expr > findKindExpression( |
---|
[39d8950] | 1167 | const ast::Expr * untyped, const ResolveContext & context, |
---|
[ef5b828] | 1168 | std::function<bool(const Candidate &)> pred = anyCandidate, |
---|
[2b59f55] | 1169 | const std::string & kind = "", ResolvMode mode = {} |
---|
[99d4584] | 1170 | ) { |
---|
| 1171 | if ( ! untyped ) return {}; |
---|
[ef5b828] | 1172 | CandidateRef choice = |
---|
[39d8950] | 1173 | findUnfinishedKindExpression( untyped, context, kind, pred, mode ); |
---|
[490fb92e] | 1174 | ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env ); |
---|
[99d4584] | 1175 | return std::move( choice->expr ); |
---|
| 1176 | } |
---|
| 1177 | |
---|
[2773ab8] | 1178 | /// Resolve `untyped` to the single expression whose candidate is the best match |
---|
[ef5b828] | 1179 | ast::ptr< ast::Expr > findSingleExpression( |
---|
[39d8950] | 1180 | const ast::Expr * untyped, const ResolveContext & context |
---|
[2773ab8] | 1181 | ) { |
---|
[57e0289] | 1182 | Stats::ResolveTime::start( untyped ); |
---|
[39d8950] | 1183 | auto res = findKindExpression( untyped, context ); |
---|
[57e0289] | 1184 | Stats::ResolveTime::stop(); |
---|
| 1185 | return res; |
---|
[2773ab8] | 1186 | } |
---|
[18e683b] | 1187 | } // anonymous namespace |
---|
[2773ab8] | 1188 | |
---|
[16ba4a6f] | 1189 | ast::ptr< ast::Expr > findSingleExpression( |
---|
[39d8950] | 1190 | const ast::Expr * untyped, const ast::Type * type, |
---|
| 1191 | const ResolveContext & context |
---|
[16ba4a6f] | 1192 | ) { |
---|
| 1193 | assert( untyped && type ); |
---|
| 1194 | ast::ptr< ast::Expr > castExpr = new ast::CastExpr{ untyped, type }; |
---|
[39d8950] | 1195 | ast::ptr< ast::Expr > newExpr = findSingleExpression( castExpr, context ); |
---|
| 1196 | removeExtraneousCast( newExpr, context.symtab ); |
---|
[16ba4a6f] | 1197 | return newExpr; |
---|
| 1198 | } |
---|
[b7d92b96] | 1199 | |
---|
[18e683b] | 1200 | namespace { |
---|
[16ba4a6f] | 1201 | bool structOrUnion( const Candidate & i ) { |
---|
| 1202 | const ast::Type * t = i.expr->result->stripReferences(); |
---|
| 1203 | return dynamic_cast< const ast::StructInstType * >( t ) || dynamic_cast< const ast::UnionInstType * >( t ); |
---|
| 1204 | } |
---|
[99d4584] | 1205 | /// Predicate for "Candidate has integral type" |
---|
| 1206 | bool hasIntegralType( const Candidate & i ) { |
---|
| 1207 | const ast::Type * type = i.expr->result; |
---|
[ef5b828] | 1208 | |
---|
[99d4584] | 1209 | if ( auto bt = dynamic_cast< const ast::BasicType * >( type ) ) { |
---|
| 1210 | return bt->isInteger(); |
---|
[ef5b828] | 1211 | } else if ( |
---|
| 1212 | dynamic_cast< const ast::EnumInstType * >( type ) |
---|
[99d4584] | 1213 | || dynamic_cast< const ast::ZeroType * >( type ) |
---|
| 1214 | || dynamic_cast< const ast::OneType * >( type ) |
---|
| 1215 | ) { |
---|
| 1216 | return true; |
---|
| 1217 | } else return false; |
---|
| 1218 | } |
---|
| 1219 | |
---|
| 1220 | /// Resolve `untyped` as an integral expression, returning the resolved version |
---|
[ef5b828] | 1221 | ast::ptr< ast::Expr > findIntegralExpression( |
---|
[39d8950] | 1222 | const ast::Expr * untyped, const ResolveContext & context |
---|
[99d4584] | 1223 | ) { |
---|
[39d8950] | 1224 | return findKindExpression( untyped, context, hasIntegralType, "condition" ); |
---|
[99d4584] | 1225 | } |
---|
[60aaa51d] | 1226 | |
---|
| 1227 | /// check if a type is a character type |
---|
| 1228 | bool isCharType( const ast::Type * t ) { |
---|
| 1229 | if ( auto bt = dynamic_cast< const ast::BasicType * >( t ) ) { |
---|
[ef5b828] | 1230 | return bt->kind == ast::BasicType::Char |
---|
| 1231 | || bt->kind == ast::BasicType::SignedChar |
---|
[60aaa51d] | 1232 | || bt->kind == ast::BasicType::UnsignedChar; |
---|
| 1233 | } |
---|
| 1234 | return false; |
---|
| 1235 | } |
---|
[2773ab8] | 1236 | |
---|
| 1237 | /// Advance a type itertor to the next mutex parameter |
---|
| 1238 | template<typename Iter> |
---|
| 1239 | inline bool nextMutex( Iter & it, const Iter & end ) { |
---|
[954c954] | 1240 | while ( it != end && ! (*it)->is_mutex() ) { ++it; } |
---|
[2773ab8] | 1241 | return it != end; |
---|
| 1242 | } |
---|
[99d4584] | 1243 | } |
---|
| 1244 | |
---|
[4864a73] | 1245 | class Resolver_new final |
---|
| 1246 | : public ast::WithSymbolTable, public ast::WithGuards, |
---|
| 1247 | public ast::WithVisitorRef<Resolver_new>, public ast::WithShortCircuiting, |
---|
[0e42794] | 1248 | public ast::WithStmtsToAdd<> { |
---|
[4864a73] | 1249 | |
---|
[2a8f0c1] | 1250 | ast::ptr< ast::Type > functionReturn = nullptr; |
---|
[2b59f55] | 1251 | ast::CurrentObject currentObject; |
---|
[16ba4a6f] | 1252 | // for work previously in GenInit |
---|
| 1253 | static InitTweak::ManagedTypes_new managedTypes; |
---|
[3bc69f2] | 1254 | ResolveContext context; |
---|
[16ba4a6f] | 1255 | |
---|
[99d4584] | 1256 | bool inEnumDecl = false; |
---|
[2a8f0c1] | 1257 | |
---|
[4864a73] | 1258 | public: |
---|
[c15085d] | 1259 | static size_t traceId; |
---|
[3bc69f2] | 1260 | Resolver_new( const ast::TranslationGlobal & global ) : |
---|
| 1261 | context{ symtab, global } {} |
---|
| 1262 | Resolver_new( const ResolveContext & context ) : |
---|
| 1263 | ast::WithSymbolTable{ context.symtab }, |
---|
| 1264 | context{ symtab, context.global } {} |
---|
[d76c588] | 1265 | |
---|
[16ba4a6f] | 1266 | const ast::FunctionDecl * previsit( const ast::FunctionDecl * ); |
---|
[99d4584] | 1267 | const ast::FunctionDecl * postvisit( const ast::FunctionDecl * ); |
---|
[16ba4a6f] | 1268 | const ast::ObjectDecl * previsit( const ast::ObjectDecl * ); |
---|
| 1269 | void previsit( const ast::AggregateDecl * ); |
---|
| 1270 | void previsit( const ast::StructDecl * ); |
---|
[99d4584] | 1271 | void previsit( const ast::EnumDecl * ); |
---|
| 1272 | const ast::StaticAssertDecl * previsit( const ast::StaticAssertDecl * ); |
---|
| 1273 | |
---|
[0f6a7752] | 1274 | const ast::ArrayType * previsit( const ast::ArrayType * ); |
---|
| 1275 | const ast::PointerType * previsit( const ast::PointerType * ); |
---|
[99d4584] | 1276 | |
---|
[2773ab8] | 1277 | const ast::ExprStmt * previsit( const ast::ExprStmt * ); |
---|
| 1278 | const ast::AsmExpr * previsit( const ast::AsmExpr * ); |
---|
| 1279 | const ast::AsmStmt * previsit( const ast::AsmStmt * ); |
---|
| 1280 | const ast::IfStmt * previsit( const ast::IfStmt * ); |
---|
[3bc69f2] | 1281 | const ast::WhileDoStmt * previsit( const ast::WhileDoStmt * ); |
---|
[2773ab8] | 1282 | const ast::ForStmt * previsit( const ast::ForStmt * ); |
---|
| 1283 | const ast::SwitchStmt * previsit( const ast::SwitchStmt * ); |
---|
[400b8be] | 1284 | const ast::CaseClause * previsit( const ast::CaseClause * ); |
---|
[2773ab8] | 1285 | const ast::BranchStmt * previsit( const ast::BranchStmt * ); |
---|
| 1286 | const ast::ReturnStmt * previsit( const ast::ReturnStmt * ); |
---|
| 1287 | const ast::ThrowStmt * previsit( const ast::ThrowStmt * ); |
---|
[400b8be] | 1288 | const ast::CatchClause * previsit( const ast::CatchClause * ); |
---|
| 1289 | const ast::CatchClause * postvisit( const ast::CatchClause * ); |
---|
[2773ab8] | 1290 | const ast::WaitForStmt * previsit( const ast::WaitForStmt * ); |
---|
[16ba4a6f] | 1291 | const ast::WithStmt * previsit( const ast::WithStmt * ); |
---|
[99d4584] | 1292 | |
---|
[2d11663] | 1293 | const ast::SingleInit * previsit( const ast::SingleInit * ); |
---|
| 1294 | const ast::ListInit * previsit( const ast::ListInit * ); |
---|
| 1295 | const ast::ConstructorInit * previsit( const ast::ConstructorInit * ); |
---|
[16ba4a6f] | 1296 | |
---|
| 1297 | void resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd); |
---|
| 1298 | |
---|
| 1299 | void beginScope() { managedTypes.beginScope(); } |
---|
| 1300 | void endScope() { managedTypes.endScope(); } |
---|
[e00c22f] | 1301 | bool on_error(ast::ptr<ast::Decl> & decl); |
---|
[d76c588] | 1302 | }; |
---|
[0d070ca] | 1303 | // size_t Resolver_new::traceId = Stats::Heap::new_stacktrace_id("Resolver"); |
---|
[d76c588] | 1304 | |
---|
[16ba4a6f] | 1305 | InitTweak::ManagedTypes_new Resolver_new::managedTypes; |
---|
| 1306 | |
---|
[293dc1c] | 1307 | void resolve( ast::TranslationUnit& translationUnit ) { |
---|
[3bc69f2] | 1308 | ast::Pass< Resolver_new >::run( translationUnit, translationUnit.global ); |
---|
[d76c588] | 1309 | } |
---|
| 1310 | |
---|
[ef5b828] | 1311 | ast::ptr< ast::Init > resolveCtorInit( |
---|
[39d8950] | 1312 | const ast::ConstructorInit * ctorInit, const ResolveContext & context |
---|
[234b1cb] | 1313 | ) { |
---|
| 1314 | assert( ctorInit ); |
---|
[3bc69f2] | 1315 | ast::Pass< Resolver_new > resolver( context ); |
---|
[234b1cb] | 1316 | return ctorInit->accept( resolver ); |
---|
| 1317 | } |
---|
| 1318 | |
---|
[302ef2a] | 1319 | const ast::Expr * resolveStmtExpr( |
---|
[39d8950] | 1320 | const ast::StmtExpr * stmtExpr, const ResolveContext & context |
---|
[17a0ede2] | 1321 | ) { |
---|
| 1322 | assert( stmtExpr ); |
---|
[3bc69f2] | 1323 | ast::Pass< Resolver_new > resolver( context ); |
---|
[302ef2a] | 1324 | auto ret = mutate(stmtExpr->accept(resolver)); |
---|
| 1325 | strict_dynamic_cast< ast::StmtExpr * >( ret )->computeResult(); |
---|
[17a0ede2] | 1326 | return ret; |
---|
| 1327 | } |
---|
| 1328 | |
---|
[16ba4a6f] | 1329 | namespace { |
---|
[39d8950] | 1330 | const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ResolveContext & context) { |
---|
[16ba4a6f] | 1331 | std::string name = attr->normalizedName(); |
---|
| 1332 | if (name == "constructor" || name == "destructor") { |
---|
| 1333 | if (attr->params.size() == 1) { |
---|
| 1334 | auto arg = attr->params.front(); |
---|
[39d8950] | 1335 | auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicType::LongLongSignedInt ), context ); |
---|
[16ba4a6f] | 1336 | auto result = eval(arg); |
---|
| 1337 | |
---|
| 1338 | auto mutAttr = mutate(attr); |
---|
| 1339 | mutAttr->params.front() = resolved; |
---|
| 1340 | if (! result.second) { |
---|
| 1341 | SemanticWarning(loc, Warning::GccAttributes, |
---|
| 1342 | toCString( name, " priorities must be integers from 0 to 65535 inclusive: ", arg ) ); |
---|
| 1343 | } |
---|
| 1344 | else { |
---|
| 1345 | auto priority = result.first; |
---|
| 1346 | if (priority < 101) { |
---|
| 1347 | SemanticWarning(loc, Warning::GccAttributes, |
---|
| 1348 | toCString( name, " priorities from 0 to 100 are reserved for the implementation" ) ); |
---|
| 1349 | } else if (priority < 201 && ! buildingLibrary()) { |
---|
| 1350 | SemanticWarning(loc, Warning::GccAttributes, |
---|
| 1351 | toCString( name, " priorities from 101 to 200 are reserved for the implementation" ) ); |
---|
| 1352 | } |
---|
| 1353 | } |
---|
| 1354 | return mutAttr; |
---|
| 1355 | } else if (attr->params.size() > 1) { |
---|
| 1356 | SemanticWarning(loc, Warning::GccAttributes, toCString( "too many arguments to ", name, " attribute" ) ); |
---|
| 1357 | } else { |
---|
| 1358 | SemanticWarning(loc, Warning::GccAttributes, toCString( "too few arguments to ", name, " attribute" ) ); |
---|
| 1359 | } |
---|
| 1360 | } |
---|
| 1361 | return attr; |
---|
| 1362 | } |
---|
| 1363 | } |
---|
| 1364 | |
---|
| 1365 | const ast::FunctionDecl * Resolver_new::previsit( const ast::FunctionDecl * functionDecl ) { |
---|
[2a8f0c1] | 1366 | GuardValue( functionReturn ); |
---|
[16ba4a6f] | 1367 | |
---|
| 1368 | assert (functionDecl->unique()); |
---|
| 1369 | if (!functionDecl->has_body() && !functionDecl->withExprs.empty()) { |
---|
| 1370 | SemanticError(functionDecl->location, functionDecl, "Function without body has with declarations"); |
---|
| 1371 | } |
---|
| 1372 | |
---|
| 1373 | if (!functionDecl->isTypeFixed) { |
---|
| 1374 | auto mutDecl = mutate(functionDecl); |
---|
| 1375 | auto mutType = mutDecl->type.get_and_mutate(); |
---|
| 1376 | |
---|
| 1377 | for (auto & attr: mutDecl->attributes) { |
---|
[3bc69f2] | 1378 | attr = handleAttribute(mutDecl->location, attr, context ); |
---|
[16ba4a6f] | 1379 | } |
---|
| 1380 | |
---|
[3e5dd913] | 1381 | // handle assertions |
---|
[16ba4a6f] | 1382 | |
---|
| 1383 | symtab.enterScope(); |
---|
[3e5dd913] | 1384 | mutType->forall.clear(); |
---|
| 1385 | mutType->assertions.clear(); |
---|
| 1386 | for (auto & typeParam : mutDecl->type_params) { |
---|
| 1387 | symtab.addType(typeParam); |
---|
[b230091] | 1388 | mutType->forall.emplace_back(new ast::TypeInstType(typeParam)); |
---|
[3e5dd913] | 1389 | } |
---|
| 1390 | for (auto & asst : mutDecl->assertions) { |
---|
[3bc69f2] | 1391 | asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), context); |
---|
[3e5dd913] | 1392 | symtab.addId(asst); |
---|
| 1393 | mutType->assertions.emplace_back(new ast::VariableExpr(functionDecl->location, asst)); |
---|
[16ba4a6f] | 1394 | } |
---|
| 1395 | |
---|
| 1396 | // temporarily adds params to symbol table. |
---|
| 1397 | // actual scoping rules for params and withexprs differ - see Pass::visit(FunctionDecl) |
---|
| 1398 | |
---|
| 1399 | std::vector<ast::ptr<ast::Type>> paramTypes; |
---|
| 1400 | std::vector<ast::ptr<ast::Type>> returnTypes; |
---|
| 1401 | |
---|
| 1402 | for (auto & param : mutDecl->params) { |
---|
[3bc69f2] | 1403 | param = fixObjectType(param.strict_as<ast::ObjectDecl>(), context); |
---|
[16ba4a6f] | 1404 | symtab.addId(param); |
---|
| 1405 | paramTypes.emplace_back(param->get_type()); |
---|
| 1406 | } |
---|
| 1407 | for (auto & ret : mutDecl->returns) { |
---|
[3bc69f2] | 1408 | ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), context); |
---|
[16ba4a6f] | 1409 | returnTypes.emplace_back(ret->get_type()); |
---|
| 1410 | } |
---|
| 1411 | // since function type in decl is just a view of param types, need to update that as well |
---|
| 1412 | mutType->params = std::move(paramTypes); |
---|
| 1413 | mutType->returns = std::move(returnTypes); |
---|
| 1414 | |
---|
[3e5dd913] | 1415 | auto renamedType = strict_dynamic_cast<const ast::FunctionType *>(renameTyVars(mutType, RenameMode::GEN_EXPR_ID)); |
---|
| 1416 | |
---|
[16ba4a6f] | 1417 | std::list<ast::ptr<ast::Stmt>> newStmts; |
---|
| 1418 | resolveWithExprs (mutDecl->withExprs, newStmts); |
---|
| 1419 | |
---|
| 1420 | if (mutDecl->stmts) { |
---|
| 1421 | auto mutStmt = mutDecl->stmts.get_and_mutate(); |
---|
| 1422 | mutStmt->kids.splice(mutStmt->kids.begin(), std::move(newStmts)); |
---|
| 1423 | mutDecl->stmts = mutStmt; |
---|
| 1424 | } |
---|
| 1425 | |
---|
| 1426 | symtab.leaveScope(); |
---|
| 1427 | |
---|
[3e5dd913] | 1428 | mutDecl->type = renamedType; |
---|
[16ba4a6f] | 1429 | mutDecl->mangleName = Mangle::mangle(mutDecl); |
---|
| 1430 | mutDecl->isTypeFixed = true; |
---|
| 1431 | functionDecl = mutDecl; |
---|
| 1432 | } |
---|
| 1433 | managedTypes.handleDWT(functionDecl); |
---|
| 1434 | |
---|
[2a8f0c1] | 1435 | functionReturn = extractResultType( functionDecl->type ); |
---|
[16ba4a6f] | 1436 | return functionDecl; |
---|
[d76c588] | 1437 | } |
---|
| 1438 | |
---|
[2a8f0c1] | 1439 | const ast::FunctionDecl * Resolver_new::postvisit( const ast::FunctionDecl * functionDecl ) { |
---|
[4864a73] | 1440 | // default value expressions have an environment which shouldn't be there and trips up |
---|
[2a8f0c1] | 1441 | // later passes. |
---|
[e068c8a] | 1442 | assert( functionDecl->unique() ); |
---|
| 1443 | ast::FunctionType * mutType = mutate( functionDecl->type.get() ); |
---|
| 1444 | |
---|
| 1445 | for ( unsigned i = 0 ; i < mutType->params.size() ; ++i ) { |
---|
| 1446 | if ( const ast::ObjectDecl * obj = mutType->params[i].as< ast::ObjectDecl >() ) { |
---|
| 1447 | if ( const ast::SingleInit * init = obj->init.as< ast::SingleInit >() ) { |
---|
| 1448 | if ( init->value->env == nullptr ) continue; |
---|
| 1449 | // clone initializer minus the initializer environment |
---|
| 1450 | auto mutParam = mutate( mutType->params[i].strict_as< ast::ObjectDecl >() ); |
---|
| 1451 | auto mutInit = mutate( mutParam->init.strict_as< ast::SingleInit >() ); |
---|
| 1452 | auto mutValue = mutate( mutInit->value.get() ); |
---|
| 1453 | |
---|
| 1454 | mutValue->env = nullptr; |
---|
| 1455 | mutInit->value = mutValue; |
---|
| 1456 | mutParam->init = mutInit; |
---|
| 1457 | mutType->params[i] = mutParam; |
---|
| 1458 | |
---|
| 1459 | assert( ! mutType->params[i].strict_as< ast::ObjectDecl >()->init.strict_as< ast::SingleInit >()->value->env); |
---|
| 1460 | } |
---|
| 1461 | } |
---|
[2a8f0c1] | 1462 | } |
---|
[73973b6] | 1463 | mutate_field(functionDecl, &ast::FunctionDecl::type, mutType); |
---|
| 1464 | return functionDecl; |
---|
[d76c588] | 1465 | } |
---|
| 1466 | |
---|
[16ba4a6f] | 1467 | const ast::ObjectDecl * Resolver_new::previsit( const ast::ObjectDecl * objectDecl ) { |
---|
[ef5b828] | 1468 | // To handle initialization of routine pointers [e.g. int (*fp)(int) = foo()], |
---|
| 1469 | // class-variable `initContext` is changed multiple times because the LHS is analyzed |
---|
| 1470 | // twice. The second analysis changes `initContext` because a function type can contain |
---|
| 1471 | // object declarations in the return and parameter types. Therefore each value of |
---|
| 1472 | // `initContext` is retained so the type on the first analysis is preserved and used for |
---|
[b7d92b96] | 1473 | // selecting the RHS. |
---|
| 1474 | GuardValue( currentObject ); |
---|
[16ba4a6f] | 1475 | |
---|
[b7d92b96] | 1476 | if ( inEnumDecl && dynamic_cast< const ast::EnumInstType * >( objectDecl->get_type() ) ) { |
---|
[ef5b828] | 1477 | // enumerator initializers should not use the enum type to initialize, since the |
---|
[b7d92b96] | 1478 | // enum type is still incomplete at this point. Use `int` instead. |
---|
[4559b34] | 1479 | |
---|
[12df6fe] | 1480 | if ( auto enumBase = dynamic_cast< const ast::EnumInstType * > |
---|
| 1481 | ( objectDecl->get_type() )->base->base ) { |
---|
[5bb1ac1] | 1482 | objectDecl = fixObjectType( objectDecl, context ); |
---|
| 1483 | currentObject = ast::CurrentObject{ |
---|
| 1484 | objectDecl->location, |
---|
| 1485 | enumBase |
---|
| 1486 | }; |
---|
[4559b34] | 1487 | } else { |
---|
[5bb1ac1] | 1488 | objectDecl = fixObjectType( objectDecl, context ); |
---|
[4559b34] | 1489 | currentObject = ast::CurrentObject{ |
---|
| 1490 | objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } }; |
---|
| 1491 | } |
---|
| 1492 | |
---|
[b7d92b96] | 1493 | } |
---|
[16ba4a6f] | 1494 | else { |
---|
[12df6fe] | 1495 | if ( !objectDecl->isTypeFixed ) { |
---|
[3bc69f2] | 1496 | auto newDecl = fixObjectType(objectDecl, context); |
---|
[16ba4a6f] | 1497 | auto mutDecl = mutate(newDecl); |
---|
[4a8f150] | 1498 | |
---|
[16ba4a6f] | 1499 | // generate CtorInit wrapper when necessary. |
---|
| 1500 | // in certain cases, fixObjectType is called before reaching |
---|
| 1501 | // this object in visitor pass, thus disabling CtorInit codegen. |
---|
| 1502 | // this happens on aggregate members and function parameters. |
---|
| 1503 | if ( InitTweak::tryConstruct( mutDecl ) && ( managedTypes.isManaged( mutDecl ) || ((! isInFunction() || mutDecl->storage.is_static ) && ! InitTweak::isConstExpr( mutDecl->init ) ) ) ) { |
---|
| 1504 | // constructed objects cannot be designated |
---|
| 1505 | if ( InitTweak::isDesignated( mutDecl->init ) ) SemanticError( mutDecl, "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.\n" ); |
---|
| 1506 | // constructed objects should not have initializers nested too deeply |
---|
| 1507 | if ( ! InitTweak::checkInitDepth( mutDecl ) ) SemanticError( mutDecl, "Managed object's initializer is too deep " ); |
---|
| 1508 | |
---|
| 1509 | mutDecl->init = InitTweak::genCtorInit( mutDecl->location, mutDecl ); |
---|
| 1510 | } |
---|
| 1511 | |
---|
| 1512 | objectDecl = mutDecl; |
---|
| 1513 | } |
---|
| 1514 | currentObject = ast::CurrentObject{ objectDecl->location, objectDecl->get_type() }; |
---|
| 1515 | } |
---|
[4a8f150] | 1516 | |
---|
[16ba4a6f] | 1517 | return objectDecl; |
---|
| 1518 | } |
---|
| 1519 | |
---|
| 1520 | void Resolver_new::previsit( const ast::AggregateDecl * _aggDecl ) { |
---|
| 1521 | auto aggDecl = mutate(_aggDecl); |
---|
| 1522 | assertf(aggDecl == _aggDecl, "type declarations must be unique"); |
---|
| 1523 | |
---|
| 1524 | for (auto & member: aggDecl->members) { |
---|
| 1525 | // nested type decls are hoisted already. no need to do anything |
---|
| 1526 | if (auto obj = member.as<ast::ObjectDecl>()) { |
---|
[3bc69f2] | 1527 | member = fixObjectType(obj, context); |
---|
[16ba4a6f] | 1528 | } |
---|
| 1529 | } |
---|
| 1530 | } |
---|
| 1531 | |
---|
| 1532 | void Resolver_new::previsit( const ast::StructDecl * structDecl ) { |
---|
| 1533 | previsit(static_cast<const ast::AggregateDecl *>(structDecl)); |
---|
| 1534 | managedTypes.handleStruct(structDecl); |
---|
[d76c588] | 1535 | } |
---|
| 1536 | |
---|
[99d4584] | 1537 | void Resolver_new::previsit( const ast::EnumDecl * ) { |
---|
| 1538 | // in case we decide to allow nested enums |
---|
| 1539 | GuardValue( inEnumDecl ); |
---|
[2890212] | 1540 | inEnumDecl = true; |
---|
[16ba4a6f] | 1541 | // don't need to fix types for enum fields |
---|
[d76c588] | 1542 | } |
---|
| 1543 | |
---|
[ef5b828] | 1544 | const ast::StaticAssertDecl * Resolver_new::previsit( |
---|
| 1545 | const ast::StaticAssertDecl * assertDecl |
---|
[99d4584] | 1546 | ) { |
---|
[ef5b828] | 1547 | return ast::mutate_field( |
---|
| 1548 | assertDecl, &ast::StaticAssertDecl::cond, |
---|
[3bc69f2] | 1549 | findIntegralExpression( assertDecl->cond, context ) ); |
---|
[b7d92b96] | 1550 | } |
---|
| 1551 | |
---|
| 1552 | template< typename PtrType > |
---|
[39d8950] | 1553 | const PtrType * handlePtrType( const PtrType * type, const ResolveContext & context ) { |
---|
[0f6a7752] | 1554 | if ( type->dimension ) { |
---|
[5cf1228] | 1555 | const ast::Type * sizeType = context.global.sizeType.get(); |
---|
[9e23b446] | 1556 | ast::ptr< ast::Expr > dimension = findSingleExpression( type->dimension, sizeType, context ); |
---|
| 1557 | assertf(dimension->env->empty(), "array dimension expr has nonempty env"); |
---|
| 1558 | dimension.get_and_mutate()->env = nullptr; |
---|
[5cf1228] | 1559 | ast::mutate_field( type, &PtrType::dimension, dimension ); |
---|
[0f6a7752] | 1560 | } |
---|
| 1561 | return type; |
---|
[d76c588] | 1562 | } |
---|
| 1563 | |
---|
[0f6a7752] | 1564 | const ast::ArrayType * Resolver_new::previsit( const ast::ArrayType * at ) { |
---|
[3bc69f2] | 1565 | return handlePtrType( at, context ); |
---|
[d76c588] | 1566 | } |
---|
| 1567 | |
---|
[0f6a7752] | 1568 | const ast::PointerType * Resolver_new::previsit( const ast::PointerType * pt ) { |
---|
[3bc69f2] | 1569 | return handlePtrType( pt, context ); |
---|
[d76c588] | 1570 | } |
---|
| 1571 | |
---|
[b7d92b96] | 1572 | const ast::ExprStmt * Resolver_new::previsit( const ast::ExprStmt * exprStmt ) { |
---|
| 1573 | visit_children = false; |
---|
| 1574 | assertf( exprStmt->expr, "ExprStmt has null expression in resolver" ); |
---|
[ef5b828] | 1575 | |
---|
| 1576 | return ast::mutate_field( |
---|
[3bc69f2] | 1577 | exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, context ) ); |
---|
[d76c588] | 1578 | } |
---|
| 1579 | |
---|
[b7d92b96] | 1580 | const ast::AsmExpr * Resolver_new::previsit( const ast::AsmExpr * asmExpr ) { |
---|
| 1581 | visit_children = false; |
---|
| 1582 | |
---|
[ef5b828] | 1583 | asmExpr = ast::mutate_field( |
---|
[3bc69f2] | 1584 | asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, context ) ); |
---|
[ef5b828] | 1585 | |
---|
[b7d92b96] | 1586 | return asmExpr; |
---|
[d76c588] | 1587 | } |
---|
| 1588 | |
---|
[2b59f55] | 1589 | const ast::AsmStmt * Resolver_new::previsit( const ast::AsmStmt * asmStmt ) { |
---|
| 1590 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::input ); |
---|
| 1591 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::output ); |
---|
| 1592 | visit_children = false; |
---|
| 1593 | return asmStmt; |
---|
[d76c588] | 1594 | } |
---|
| 1595 | |
---|
[b7d92b96] | 1596 | const ast::IfStmt * Resolver_new::previsit( const ast::IfStmt * ifStmt ) { |
---|
| 1597 | return ast::mutate_field( |
---|
[3bc69f2] | 1598 | ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, context ) ); |
---|
[d76c588] | 1599 | } |
---|
| 1600 | |
---|
[3b0bc16] | 1601 | const ast::WhileDoStmt * Resolver_new::previsit( const ast::WhileDoStmt * whileDoStmt ) { |
---|
[ef5b828] | 1602 | return ast::mutate_field( |
---|
[3bc69f2] | 1603 | whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, context ) ); |
---|
[d76c588] | 1604 | } |
---|
| 1605 | |
---|
[b7d92b96] | 1606 | const ast::ForStmt * Resolver_new::previsit( const ast::ForStmt * forStmt ) { |
---|
| 1607 | if ( forStmt->cond ) { |
---|
| 1608 | forStmt = ast::mutate_field( |
---|
[3bc69f2] | 1609 | forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, context ) ); |
---|
[b7d92b96] | 1610 | } |
---|
| 1611 | |
---|
| 1612 | if ( forStmt->inc ) { |
---|
| 1613 | forStmt = ast::mutate_field( |
---|
[3bc69f2] | 1614 | forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, context ) ); |
---|
[b7d92b96] | 1615 | } |
---|
| 1616 | |
---|
| 1617 | return forStmt; |
---|
[d76c588] | 1618 | } |
---|
| 1619 | |
---|
[b7d92b96] | 1620 | const ast::SwitchStmt * Resolver_new::previsit( const ast::SwitchStmt * switchStmt ) { |
---|
| 1621 | GuardValue( currentObject ); |
---|
| 1622 | switchStmt = ast::mutate_field( |
---|
[ef5b828] | 1623 | switchStmt, &ast::SwitchStmt::cond, |
---|
[3bc69f2] | 1624 | findIntegralExpression( switchStmt->cond, context ) ); |
---|
[2b59f55] | 1625 | currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result }; |
---|
[b7d92b96] | 1626 | return switchStmt; |
---|
[d76c588] | 1627 | } |
---|
| 1628 | |
---|
[400b8be] | 1629 | const ast::CaseClause * Resolver_new::previsit( const ast::CaseClause * caseStmt ) { |
---|
[b7d92b96] | 1630 | if ( caseStmt->cond ) { |
---|
[60aaa51d] | 1631 | std::deque< ast::InitAlternative > initAlts = currentObject.getOptions(); |
---|
[2b59f55] | 1632 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral " |
---|
| 1633 | "expression." ); |
---|
[ef5b828] | 1634 | |
---|
| 1635 | ast::ptr< ast::Expr > untyped = |
---|
[2b59f55] | 1636 | new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type }; |
---|
[3bc69f2] | 1637 | ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, context ); |
---|
[ef5b828] | 1638 | |
---|
| 1639 | // case condition cannot have a cast in C, so it must be removed here, regardless of |
---|
[2b59f55] | 1640 | // whether it would perform a conversion. |
---|
| 1641 | if ( const ast::CastExpr * castExpr = newExpr.as< ast::CastExpr >() ) { |
---|
[60aaa51d] | 1642 | swap_and_save_env( newExpr, castExpr->arg ); |
---|
[2b59f55] | 1643 | } |
---|
[ef5b828] | 1644 | |
---|
[400b8be] | 1645 | caseStmt = ast::mutate_field( caseStmt, &ast::CaseClause::cond, newExpr ); |
---|
[b7d92b96] | 1646 | } |
---|
| 1647 | return caseStmt; |
---|
[d76c588] | 1648 | } |
---|
| 1649 | |
---|
[b7d92b96] | 1650 | const ast::BranchStmt * Resolver_new::previsit( const ast::BranchStmt * branchStmt ) { |
---|
| 1651 | visit_children = false; |
---|
| 1652 | // must resolve the argument of a computed goto |
---|
| 1653 | if ( branchStmt->kind == ast::BranchStmt::Goto && branchStmt->computedTarget ) { |
---|
| 1654 | // computed goto argument is void* |
---|
[2773ab8] | 1655 | ast::ptr< ast::Type > target = new ast::PointerType{ new ast::VoidType{} }; |
---|
[b7d92b96] | 1656 | branchStmt = ast::mutate_field( |
---|
[ef5b828] | 1657 | branchStmt, &ast::BranchStmt::computedTarget, |
---|
[3bc69f2] | 1658 | findSingleExpression( branchStmt->computedTarget, target, context ) ); |
---|
[b7d92b96] | 1659 | } |
---|
| 1660 | return branchStmt; |
---|
[d76c588] | 1661 | } |
---|
| 1662 | |
---|
[2b59f55] | 1663 | const ast::ReturnStmt * Resolver_new::previsit( const ast::ReturnStmt * returnStmt ) { |
---|
| 1664 | visit_children = false; |
---|
| 1665 | if ( returnStmt->expr ) { |
---|
| 1666 | returnStmt = ast::mutate_field( |
---|
[ef5b828] | 1667 | returnStmt, &ast::ReturnStmt::expr, |
---|
[3bc69f2] | 1668 | findSingleExpression( returnStmt->expr, functionReturn, context ) ); |
---|
[2b59f55] | 1669 | } |
---|
| 1670 | return returnStmt; |
---|
[d76c588] | 1671 | } |
---|
| 1672 | |
---|
[2b59f55] | 1673 | const ast::ThrowStmt * Resolver_new::previsit( const ast::ThrowStmt * throwStmt ) { |
---|
| 1674 | visit_children = false; |
---|
| 1675 | if ( throwStmt->expr ) { |
---|
[ef5b828] | 1676 | const ast::StructDecl * exceptionDecl = |
---|
[3090127] | 1677 | symtab.lookupStruct( "__cfaehm_base_exception_t" ); |
---|
[2b59f55] | 1678 | assert( exceptionDecl ); |
---|
[ef5b828] | 1679 | ast::ptr< ast::Type > exceptType = |
---|
[2b59f55] | 1680 | new ast::PointerType{ new ast::StructInstType{ exceptionDecl } }; |
---|
| 1681 | throwStmt = ast::mutate_field( |
---|
[ef5b828] | 1682 | throwStmt, &ast::ThrowStmt::expr, |
---|
[3bc69f2] | 1683 | findSingleExpression( throwStmt->expr, exceptType, context ) ); |
---|
[2b59f55] | 1684 | } |
---|
| 1685 | return throwStmt; |
---|
[d76c588] | 1686 | } |
---|
| 1687 | |
---|
[400b8be] | 1688 | const ast::CatchClause * Resolver_new::previsit( const ast::CatchClause * catchClause ) { |
---|
[3b0bc16] | 1689 | // Until we are very sure this invarent (ifs that move between passes have then) |
---|
[b9fa85b] | 1690 | // holds, check it. This allows a check for when to decode the mangling. |
---|
[400b8be] | 1691 | if ( auto ifStmt = catchClause->body.as<ast::IfStmt>() ) { |
---|
[3b0bc16] | 1692 | assert( ifStmt->then ); |
---|
[b9fa85b] | 1693 | } |
---|
| 1694 | // Encode the catchStmt so the condition can see the declaration. |
---|
[400b8be] | 1695 | if ( catchClause->cond ) { |
---|
| 1696 | ast::CatchClause * clause = mutate( catchClause ); |
---|
| 1697 | clause->body = new ast::IfStmt( clause->location, clause->cond, nullptr, clause->body ); |
---|
| 1698 | clause->cond = nullptr; |
---|
| 1699 | return clause; |
---|
[b9fa85b] | 1700 | } |
---|
[400b8be] | 1701 | return catchClause; |
---|
[b9fa85b] | 1702 | } |
---|
| 1703 | |
---|
[400b8be] | 1704 | const ast::CatchClause * Resolver_new::postvisit( const ast::CatchClause * catchClause ) { |
---|
[b9fa85b] | 1705 | // Decode the catchStmt so everything is stored properly. |
---|
[400b8be] | 1706 | const ast::IfStmt * ifStmt = catchClause->body.as<ast::IfStmt>(); |
---|
[3b0bc16] | 1707 | if ( nullptr != ifStmt && nullptr == ifStmt->then ) { |
---|
[b9fa85b] | 1708 | assert( ifStmt->cond ); |
---|
[3b0bc16] | 1709 | assert( ifStmt->else_ ); |
---|
[400b8be] | 1710 | ast::CatchClause * clause = ast::mutate( catchClause ); |
---|
| 1711 | clause->cond = ifStmt->cond; |
---|
| 1712 | clause->body = ifStmt->else_; |
---|
[b9fa85b] | 1713 | // ifStmt should be implicately deleted here. |
---|
[400b8be] | 1714 | return clause; |
---|
[2b59f55] | 1715 | } |
---|
[400b8be] | 1716 | return catchClause; |
---|
[d76c588] | 1717 | } |
---|
| 1718 | |
---|
[2773ab8] | 1719 | const ast::WaitForStmt * Resolver_new::previsit( const ast::WaitForStmt * stmt ) { |
---|
| 1720 | visit_children = false; |
---|
| 1721 | |
---|
| 1722 | // Resolve all clauses first |
---|
| 1723 | for ( unsigned i = 0; i < stmt->clauses.size(); ++i ) { |
---|
[f6e6a55] | 1724 | const ast::WaitForClause & clause = *stmt->clauses[i]; |
---|
[2773ab8] | 1725 | |
---|
| 1726 | ast::TypeEnvironment env; |
---|
[3bc69f2] | 1727 | CandidateFinder funcFinder( context, env ); |
---|
[2773ab8] | 1728 | |
---|
| 1729 | // Find all candidates for a function in canonical form |
---|
[f6e6a55] | 1730 | funcFinder.find( clause.target_func, ResolvMode::withAdjustment() ); |
---|
[2773ab8] | 1731 | |
---|
| 1732 | if ( funcFinder.candidates.empty() ) { |
---|
| 1733 | stringstream ss; |
---|
| 1734 | ss << "Use of undeclared indentifier '"; |
---|
[f6e6a55] | 1735 | ss << clause.target_func.strict_as< ast::NameExpr >()->name; |
---|
[2773ab8] | 1736 | ss << "' in call to waitfor"; |
---|
| 1737 | SemanticError( stmt->location, ss.str() ); |
---|
| 1738 | } |
---|
| 1739 | |
---|
[f6e6a55] | 1740 | if ( clause.target_args.empty() ) { |
---|
[ef5b828] | 1741 | SemanticError( stmt->location, |
---|
[2773ab8] | 1742 | "Waitfor clause must have at least one mutex parameter"); |
---|
| 1743 | } |
---|
| 1744 | |
---|
| 1745 | // Find all alternatives for all arguments in canonical form |
---|
[ef5b828] | 1746 | std::vector< CandidateFinder > argFinders = |
---|
[f6e6a55] | 1747 | funcFinder.findSubExprs( clause.target_args ); |
---|
[ef5b828] | 1748 | |
---|
[2773ab8] | 1749 | // List all combinations of arguments |
---|
| 1750 | std::vector< CandidateList > possibilities; |
---|
| 1751 | combos( argFinders.begin(), argFinders.end(), back_inserter( possibilities ) ); |
---|
| 1752 | |
---|
| 1753 | // For every possible function: |
---|
[ef5b828] | 1754 | // * try matching the arguments to the parameters, not the other way around because |
---|
[2773ab8] | 1755 | // more arguments than parameters |
---|
| 1756 | CandidateList funcCandidates; |
---|
| 1757 | std::vector< CandidateList > argsCandidates; |
---|
| 1758 | SemanticErrorException errors; |
---|
| 1759 | for ( CandidateRef & func : funcFinder.candidates ) { |
---|
| 1760 | try { |
---|
[ef5b828] | 1761 | auto pointerType = dynamic_cast< const ast::PointerType * >( |
---|
[2773ab8] | 1762 | func->expr->result->stripReferences() ); |
---|
| 1763 | if ( ! pointerType ) { |
---|
[ef5b828] | 1764 | SemanticError( stmt->location, func->expr->result.get(), |
---|
[2773ab8] | 1765 | "candidate not viable: not a pointer type\n" ); |
---|
| 1766 | } |
---|
| 1767 | |
---|
| 1768 | auto funcType = pointerType->base.as< ast::FunctionType >(); |
---|
| 1769 | if ( ! funcType ) { |
---|
[ef5b828] | 1770 | SemanticError( stmt->location, func->expr->result.get(), |
---|
[2773ab8] | 1771 | "candidate not viable: not a function type\n" ); |
---|
| 1772 | } |
---|
| 1773 | |
---|
| 1774 | { |
---|
| 1775 | auto param = funcType->params.begin(); |
---|
| 1776 | auto paramEnd = funcType->params.end(); |
---|
| 1777 | |
---|
| 1778 | if( ! nextMutex( param, paramEnd ) ) { |
---|
[ef5b828] | 1779 | SemanticError( stmt->location, funcType, |
---|
[2773ab8] | 1780 | "candidate function not viable: no mutex parameters\n"); |
---|
| 1781 | } |
---|
| 1782 | } |
---|
| 1783 | |
---|
| 1784 | CandidateRef func2{ new Candidate{ *func } }; |
---|
| 1785 | // strip reference from function |
---|
| 1786 | func2->expr = referenceToRvalueConversion( func->expr, func2->cost ); |
---|
| 1787 | |
---|
| 1788 | // Each argument must be matched with a parameter of the current candidate |
---|
| 1789 | for ( auto & argsList : possibilities ) { |
---|
| 1790 | try { |
---|
| 1791 | // Declare data structures needed for resolution |
---|
| 1792 | ast::OpenVarSet open; |
---|
| 1793 | ast::AssertionSet need, have; |
---|
| 1794 | ast::TypeEnvironment resultEnv{ func->env }; |
---|
[ef5b828] | 1795 | // Add all type variables as open so that those not used in the |
---|
[2773ab8] | 1796 | // parameter list are still considered open |
---|
| 1797 | resultEnv.add( funcType->forall ); |
---|
| 1798 | |
---|
| 1799 | // load type variables from arguments into one shared space |
---|
| 1800 | for ( auto & arg : argsList ) { |
---|
| 1801 | resultEnv.simpleCombine( arg->env ); |
---|
| 1802 | } |
---|
| 1803 | |
---|
| 1804 | // Make sure we don't widen any existing bindings |
---|
| 1805 | resultEnv.forbidWidening(); |
---|
| 1806 | |
---|
| 1807 | // Find any unbound type variables |
---|
| 1808 | resultEnv.extractOpenVars( open ); |
---|
| 1809 | |
---|
| 1810 | auto param = funcType->params.begin(); |
---|
| 1811 | auto paramEnd = funcType->params.end(); |
---|
| 1812 | |
---|
| 1813 | unsigned n_mutex_param = 0; |
---|
| 1814 | |
---|
[ef5b828] | 1815 | // For every argument of its set, check if it matches one of the |
---|
[2773ab8] | 1816 | // parameters. The order is important |
---|
| 1817 | for ( auto & arg : argsList ) { |
---|
| 1818 | // Ignore non-mutex arguments |
---|
| 1819 | if ( ! nextMutex( param, paramEnd ) ) { |
---|
| 1820 | // We ran out of parameters but still have arguments. |
---|
| 1821 | // This function doesn't match |
---|
[ef5b828] | 1822 | SemanticError( stmt->location, funcType, |
---|
[2773ab8] | 1823 | toString("candidate function not viable: too many mutex " |
---|
| 1824 | "arguments, expected ", n_mutex_param, "\n" ) ); |
---|
| 1825 | } |
---|
| 1826 | |
---|
| 1827 | ++n_mutex_param; |
---|
| 1828 | |
---|
[ef5b828] | 1829 | // Check if the argument matches the parameter type in the current |
---|
[2773ab8] | 1830 | // scope |
---|
[954c954] | 1831 | // ast::ptr< ast::Type > paramType = (*param)->get_type(); |
---|
[ef5b828] | 1832 | if ( |
---|
| 1833 | ! unify( |
---|
[954c954] | 1834 | arg->expr->result, *param, resultEnv, need, have, open, |
---|
[ef5b828] | 1835 | symtab ) |
---|
[2773ab8] | 1836 | ) { |
---|
| 1837 | // Type doesn't match |
---|
| 1838 | stringstream ss; |
---|
| 1839 | ss << "candidate function not viable: no known conversion " |
---|
| 1840 | "from '"; |
---|
[954c954] | 1841 | ast::print( ss, *param ); |
---|
[2773ab8] | 1842 | ss << "' to '"; |
---|
| 1843 | ast::print( ss, arg->expr->result ); |
---|
| 1844 | ss << "' with env '"; |
---|
| 1845 | ast::print( ss, resultEnv ); |
---|
| 1846 | ss << "'\n"; |
---|
| 1847 | SemanticError( stmt->location, funcType, ss.str() ); |
---|
| 1848 | } |
---|
| 1849 | |
---|
| 1850 | ++param; |
---|
| 1851 | } |
---|
| 1852 | |
---|
| 1853 | // All arguments match! |
---|
| 1854 | |
---|
| 1855 | // Check if parameters are missing |
---|
| 1856 | if ( nextMutex( param, paramEnd ) ) { |
---|
| 1857 | do { |
---|
| 1858 | ++n_mutex_param; |
---|
| 1859 | ++param; |
---|
| 1860 | } while ( nextMutex( param, paramEnd ) ); |
---|
| 1861 | |
---|
[ef5b828] | 1862 | // We ran out of arguments but still have parameters left; this |
---|
[2773ab8] | 1863 | // function doesn't match |
---|
[ef5b828] | 1864 | SemanticError( stmt->location, funcType, |
---|
[2773ab8] | 1865 | toString( "candidate function not viable: too few mutex " |
---|
| 1866 | "arguments, expected ", n_mutex_param, "\n" ) ); |
---|
| 1867 | } |
---|
| 1868 | |
---|
| 1869 | // All parameters match! |
---|
| 1870 | |
---|
| 1871 | // Finish the expressions to tie in proper environments |
---|
| 1872 | finishExpr( func2->expr, resultEnv ); |
---|
| 1873 | for ( CandidateRef & arg : argsList ) { |
---|
| 1874 | finishExpr( arg->expr, resultEnv ); |
---|
| 1875 | } |
---|
| 1876 | |
---|
| 1877 | // This is a match, store it and save it for later |
---|
| 1878 | funcCandidates.emplace_back( std::move( func2 ) ); |
---|
| 1879 | argsCandidates.emplace_back( std::move( argsList ) ); |
---|
| 1880 | |
---|
| 1881 | } catch ( SemanticErrorException & e ) { |
---|
| 1882 | errors.append( e ); |
---|
| 1883 | } |
---|
| 1884 | } |
---|
| 1885 | } catch ( SemanticErrorException & e ) { |
---|
| 1886 | errors.append( e ); |
---|
| 1887 | } |
---|
| 1888 | } |
---|
| 1889 | |
---|
| 1890 | // Make sure correct number of arguments |
---|
| 1891 | if( funcCandidates.empty() ) { |
---|
[ef5b828] | 1892 | SemanticErrorException top( stmt->location, |
---|
[2773ab8] | 1893 | "No alternatives for function in call to waitfor" ); |
---|
| 1894 | top.append( errors ); |
---|
| 1895 | throw top; |
---|
| 1896 | } |
---|
| 1897 | |
---|
| 1898 | if( argsCandidates.empty() ) { |
---|
[ef5b828] | 1899 | SemanticErrorException top( stmt->location, |
---|
| 1900 | "No alternatives for arguments in call to waitfor" ); |
---|
[2773ab8] | 1901 | top.append( errors ); |
---|
| 1902 | throw top; |
---|
| 1903 | } |
---|
| 1904 | |
---|
| 1905 | if( funcCandidates.size() > 1 ) { |
---|
[ef5b828] | 1906 | SemanticErrorException top( stmt->location, |
---|
[2773ab8] | 1907 | "Ambiguous function in call to waitfor" ); |
---|
| 1908 | top.append( errors ); |
---|
| 1909 | throw top; |
---|
| 1910 | } |
---|
| 1911 | if( argsCandidates.size() > 1 ) { |
---|
| 1912 | SemanticErrorException top( stmt->location, |
---|
| 1913 | "Ambiguous arguments in call to waitfor" ); |
---|
| 1914 | top.append( errors ); |
---|
| 1915 | throw top; |
---|
| 1916 | } |
---|
| 1917 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used. |
---|
| 1918 | |
---|
| 1919 | // build new clause |
---|
[f6e6a55] | 1920 | auto clause2 = new ast::WaitForClause( clause.location ); |
---|
[ef5b828] | 1921 | |
---|
[f6e6a55] | 1922 | clause2->target_func = funcCandidates.front()->expr; |
---|
[ef5b828] | 1923 | |
---|
[f6e6a55] | 1924 | clause2->target_args.reserve( clause.target_args.size() ); |
---|
[6668a3e] | 1925 | const ast::StructDecl * decl_monitor = symtab.lookupStruct( "monitor$" ); |
---|
[2773ab8] | 1926 | for ( auto arg : argsCandidates.front() ) { |
---|
[6668a3e] | 1927 | const auto & loc = stmt->location; |
---|
| 1928 | |
---|
| 1929 | ast::Expr * init = new ast::CastExpr( loc, |
---|
| 1930 | new ast::UntypedExpr( loc, |
---|
| 1931 | new ast::NameExpr( loc, "get_monitor" ), |
---|
| 1932 | { arg->expr } |
---|
| 1933 | ), |
---|
| 1934 | new ast::PointerType( |
---|
| 1935 | new ast::StructInstType( |
---|
| 1936 | decl_monitor |
---|
| 1937 | ) |
---|
| 1938 | ) |
---|
| 1939 | ); |
---|
| 1940 | |
---|
[f6e6a55] | 1941 | clause2->target_args.emplace_back( findSingleExpression( init, context ) ); |
---|
[2773ab8] | 1942 | } |
---|
| 1943 | |
---|
| 1944 | // Resolve the conditions as if it were an IfStmt, statements normally |
---|
[f6e6a55] | 1945 | clause2->cond = findSingleExpression( clause.cond, context ); |
---|
| 1946 | clause2->stmt = clause.stmt->accept( *visitor ); |
---|
[2773ab8] | 1947 | |
---|
| 1948 | // set results into stmt |
---|
| 1949 | auto n = mutate( stmt ); |
---|
[f6e6a55] | 1950 | n->clauses[i] = clause2; |
---|
[2773ab8] | 1951 | stmt = n; |
---|
| 1952 | } |
---|
| 1953 | |
---|
[f6e6a55] | 1954 | if ( stmt->timeout_stmt ) { |
---|
[2773ab8] | 1955 | // resolve the timeout as a size_t, the conditions like IfStmt, and stmts normally |
---|
[ef5b828] | 1956 | ast::ptr< ast::Type > target = |
---|
[2773ab8] | 1957 | new ast::BasicType{ ast::BasicType::LongLongUnsignedInt }; |
---|
[f6e6a55] | 1958 | auto timeout_time = findSingleExpression( stmt->timeout_time, target, context ); |
---|
| 1959 | auto timeout_cond = findSingleExpression( stmt->timeout_cond, context ); |
---|
| 1960 | auto timeout_stmt = stmt->timeout_stmt->accept( *visitor ); |
---|
[2773ab8] | 1961 | |
---|
| 1962 | // set results into stmt |
---|
| 1963 | auto n = mutate( stmt ); |
---|
[f6e6a55] | 1964 | n->timeout_time = std::move( timeout_time ); |
---|
| 1965 | n->timeout_cond = std::move( timeout_cond ); |
---|
| 1966 | n->timeout_stmt = std::move( timeout_stmt ); |
---|
[2773ab8] | 1967 | stmt = n; |
---|
| 1968 | } |
---|
| 1969 | |
---|
[f6e6a55] | 1970 | if ( stmt->else_stmt ) { |
---|
[2773ab8] | 1971 | // resolve the condition like IfStmt, stmts normally |
---|
[f6e6a55] | 1972 | auto else_cond = findSingleExpression( stmt->else_cond, context ); |
---|
| 1973 | auto else_stmt = stmt->else_stmt->accept( *visitor ); |
---|
[2773ab8] | 1974 | |
---|
| 1975 | // set results into stmt |
---|
| 1976 | auto n = mutate( stmt ); |
---|
[f6e6a55] | 1977 | n->else_cond = std::move( else_cond ); |
---|
| 1978 | n->else_stmt = std::move( else_stmt ); |
---|
[2773ab8] | 1979 | stmt = n; |
---|
| 1980 | } |
---|
| 1981 | |
---|
| 1982 | return stmt; |
---|
[d76c588] | 1983 | } |
---|
| 1984 | |
---|
[16ba4a6f] | 1985 | const ast::WithStmt * Resolver_new::previsit( const ast::WithStmt * withStmt ) { |
---|
| 1986 | auto mutStmt = mutate(withStmt); |
---|
| 1987 | resolveWithExprs(mutStmt->exprs, stmtsToAddBefore); |
---|
| 1988 | return mutStmt; |
---|
| 1989 | } |
---|
| 1990 | |
---|
| 1991 | void Resolver_new::resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd) { |
---|
| 1992 | for (auto & expr : exprs) { |
---|
| 1993 | // only struct- and union-typed expressions are viable candidates |
---|
[3bc69f2] | 1994 | expr = findKindExpression( expr, context, structOrUnion, "with expression" ); |
---|
[16ba4a6f] | 1995 | |
---|
| 1996 | // if with expression might be impure, create a temporary so that it is evaluated once |
---|
| 1997 | if ( Tuples::maybeImpure( expr ) ) { |
---|
| 1998 | static UniqueName tmpNamer( "_with_tmp_" ); |
---|
| 1999 | const CodeLocation loc = expr->location; |
---|
| 2000 | auto tmp = new ast::ObjectDecl(loc, tmpNamer.newName(), expr->result, new ast::SingleInit(loc, expr ) ); |
---|
| 2001 | expr = new ast::VariableExpr( loc, tmp ); |
---|
| 2002 | stmtsToAdd.push_back( new ast::DeclStmt(loc, tmp ) ); |
---|
| 2003 | if ( InitTweak::isConstructable( tmp->type ) ) { |
---|
| 2004 | // generate ctor/dtor and resolve them |
---|
| 2005 | tmp->init = InitTweak::genCtorInit( loc, tmp ); |
---|
| 2006 | } |
---|
| 2007 | // since tmp is freshly created, this should modify tmp in-place |
---|
| 2008 | tmp->accept( *visitor ); |
---|
| 2009 | } |
---|
[9e23b446] | 2010 | else if (expr->env && expr->env->empty()) { |
---|
| 2011 | expr = ast::mutate_field(expr.get(), &ast::Expr::env, nullptr); |
---|
| 2012 | } |
---|
[16ba4a6f] | 2013 | } |
---|
| 2014 | } |
---|
[60aaa51d] | 2015 | |
---|
| 2016 | |
---|
| 2017 | const ast::SingleInit * Resolver_new::previsit( const ast::SingleInit * singleInit ) { |
---|
| 2018 | visit_children = false; |
---|
[ef5b828] | 2019 | // resolve initialization using the possibilities as determined by the `currentObject` |
---|
[60aaa51d] | 2020 | // cursor. |
---|
[ef5b828] | 2021 | ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{ |
---|
[60aaa51d] | 2022 | singleInit->location, singleInit->value, currentObject.getOptions() }; |
---|
[3bc69f2] | 2023 | ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, context ); |
---|
[60aaa51d] | 2024 | const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >(); |
---|
| 2025 | |
---|
| 2026 | // move cursor to the object that is actually initialized |
---|
| 2027 | currentObject.setNext( initExpr->designation ); |
---|
| 2028 | |
---|
| 2029 | // discard InitExpr wrapper and retain relevant pieces. |
---|
[ef5b828] | 2030 | // `initExpr` may have inferred params in the case where the expression specialized a |
---|
| 2031 | // function pointer, and newExpr may already have inferParams of its own, so a simple |
---|
[60aaa51d] | 2032 | // swap is not sufficient |
---|
| 2033 | ast::Expr::InferUnion inferred = initExpr->inferred; |
---|
| 2034 | swap_and_save_env( newExpr, initExpr->expr ); |
---|
| 2035 | newExpr.get_and_mutate()->inferred.splice( std::move(inferred) ); |
---|
| 2036 | |
---|
[ef5b828] | 2037 | // get the actual object's type (may not exactly match what comes back from the resolver |
---|
[60aaa51d] | 2038 | // due to conversions) |
---|
| 2039 | const ast::Type * initContext = currentObject.getCurrentType(); |
---|
| 2040 | |
---|
| 2041 | removeExtraneousCast( newExpr, symtab ); |
---|
| 2042 | |
---|
| 2043 | // check if actual object's type is char[] |
---|
| 2044 | if ( auto at = dynamic_cast< const ast::ArrayType * >( initContext ) ) { |
---|
| 2045 | if ( isCharType( at->base ) ) { |
---|
| 2046 | // check if the resolved type is char* |
---|
| 2047 | if ( auto pt = newExpr->result.as< ast::PointerType >() ) { |
---|
| 2048 | if ( isCharType( pt->base ) ) { |
---|
[ef5b828] | 2049 | // strip cast if we're initializing a char[] with a char* |
---|
[60aaa51d] | 2050 | // e.g. char x[] = "hello" |
---|
| 2051 | if ( auto ce = newExpr.as< ast::CastExpr >() ) { |
---|
| 2052 | swap_and_save_env( newExpr, ce->arg ); |
---|
| 2053 | } |
---|
| 2054 | } |
---|
| 2055 | } |
---|
| 2056 | } |
---|
| 2057 | } |
---|
| 2058 | |
---|
| 2059 | // move cursor to next object in preparation for next initializer |
---|
| 2060 | currentObject.increment(); |
---|
| 2061 | |
---|
| 2062 | // set initializer expression to resolved expression |
---|
| 2063 | return ast::mutate_field( singleInit, &ast::SingleInit::value, std::move(newExpr) ); |
---|
[d76c588] | 2064 | } |
---|
| 2065 | |
---|
[60aaa51d] | 2066 | const ast::ListInit * Resolver_new::previsit( const ast::ListInit * listInit ) { |
---|
| 2067 | // move cursor into brace-enclosed initializer-list |
---|
| 2068 | currentObject.enterListInit( listInit->location ); |
---|
| 2069 | |
---|
| 2070 | assert( listInit->designations.size() == listInit->initializers.size() ); |
---|
| 2071 | for ( unsigned i = 0; i < listInit->designations.size(); ++i ) { |
---|
[ef5b828] | 2072 | // iterate designations and initializers in pairs, moving the cursor to the current |
---|
[60aaa51d] | 2073 | // designated object and resolving the initializer against that object |
---|
[2d11663] | 2074 | listInit = ast::mutate_field_index( |
---|
[ef5b828] | 2075 | listInit, &ast::ListInit::designations, i, |
---|
[2d11663] | 2076 | currentObject.findNext( listInit->designations[i] ) ); |
---|
| 2077 | listInit = ast::mutate_field_index( |
---|
| 2078 | listInit, &ast::ListInit::initializers, i, |
---|
| 2079 | listInit->initializers[i]->accept( *visitor ) ); |
---|
[60aaa51d] | 2080 | } |
---|
| 2081 | |
---|
[2d11663] | 2082 | // move cursor out of brace-enclosed initializer-list |
---|
| 2083 | currentObject.exitListInit(); |
---|
| 2084 | |
---|
[60aaa51d] | 2085 | visit_children = false; |
---|
| 2086 | return listInit; |
---|
[d76c588] | 2087 | } |
---|
| 2088 | |
---|
[2d11663] | 2089 | const ast::ConstructorInit * Resolver_new::previsit( const ast::ConstructorInit * ctorInit ) { |
---|
| 2090 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::ctor ); |
---|
| 2091 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::dtor ); |
---|
| 2092 | |
---|
| 2093 | // found a constructor - can get rid of C-style initializer |
---|
| 2094 | // xxx - Rob suggests this field is dead code |
---|
| 2095 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::init, nullptr ); |
---|
| 2096 | |
---|
[ef5b828] | 2097 | // intrinsic single-parameter constructors and destructors do nothing. Since this was |
---|
| 2098 | // implicitly generated, there's no way for it to have side effects, so get rid of it to |
---|
[2d11663] | 2099 | // clean up generated code |
---|
| 2100 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { |
---|
| 2101 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::ctor, nullptr ); |
---|
| 2102 | } |
---|
| 2103 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { |
---|
| 2104 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::dtor, nullptr ); |
---|
| 2105 | } |
---|
| 2106 | |
---|
| 2107 | return ctorInit; |
---|
[d76c588] | 2108 | } |
---|
| 2109 | |
---|
[0dd9a5e] | 2110 | // suppress error on autogen functions and mark invalid autogen as deleted. |
---|
[e00c22f] | 2111 | bool Resolver_new::on_error(ast::ptr<ast::Decl> & decl) { |
---|
[0dd9a5e] | 2112 | if (auto functionDecl = decl.as<ast::FunctionDecl>()) { |
---|
| 2113 | // xxx - can intrinsic gen ever fail? |
---|
[6668a3e] | 2114 | if (functionDecl->linkage == ast::Linkage::AutoGen) { |
---|
[0dd9a5e] | 2115 | auto mutDecl = mutate(functionDecl); |
---|
| 2116 | mutDecl->isDeleted = true; |
---|
| 2117 | mutDecl->stmts = nullptr; |
---|
| 2118 | decl = mutDecl; |
---|
| 2119 | return false; |
---|
| 2120 | } |
---|
| 2121 | } |
---|
| 2122 | return true; |
---|
| 2123 | } |
---|
| 2124 | |
---|
[51b7345] | 2125 | } // namespace ResolvExpr |
---|
[a32b204] | 2126 | |
---|
| 2127 | // Local Variables: // |
---|
| 2128 | // tab-width: 4 // |
---|
| 2129 | // mode: c++ // |
---|
| 2130 | // compile-command: "make install" // |
---|
| 2131 | // End: // |
---|