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