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