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