| [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 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Sun May 17 12:17:01 2015 | 
|---|
| [93401f8] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Sat Feb 17 11:19:40 2018 | 
|---|
|  | 13 | // Update Count     : 213 | 
|---|
| [a32b204] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [ea6332d] | 16 | #include <stddef.h>                      // for NULL | 
|---|
| [e3e16bc] | 17 | #include <cassert>                       // for strict_dynamic_cast, assert | 
|---|
| [ea6332d] | 18 | #include <memory>                        // for allocator, allocator_traits<... | 
|---|
|  | 19 | #include <tuple>                         // for get | 
|---|
| [bd4f2e9] | 20 | #include <vector> | 
|---|
| [ea6332d] | 21 |  | 
|---|
|  | 22 | #include "Alternative.h"                 // for Alternative, AltList | 
|---|
|  | 23 | #include "AlternativeFinder.h"           // for AlternativeFinder, resolveIn... | 
|---|
| [a4ca48c] | 24 | #include "Common/PassVisitor.h"          // for PassVisitor | 
|---|
| [ea6332d] | 25 | #include "Common/SemanticError.h"        // for SemanticError | 
|---|
|  | 26 | #include "Common/utility.h"              // for ValueGuard, group_iterate | 
|---|
|  | 27 | #include "CurrentObject.h"               // for CurrentObject | 
|---|
| [0a60c04] | 28 | #include "InitTweak/GenInit.h" | 
|---|
| [ea6332d] | 29 | #include "InitTweak/InitTweak.h"         // for isIntrinsicSingleArgCallStmt | 
|---|
|  | 30 | #include "RenameVars.h"                  // for RenameVars, global_renamer | 
|---|
|  | 31 | #include "ResolvExpr/TypeEnvironment.h"  // for TypeEnvironment | 
|---|
|  | 32 | #include "ResolveTypeof.h"               // for resolveTypeof | 
|---|
| [e4d829b] | 33 | #include "Resolver.h" | 
|---|
| [ea6332d] | 34 | #include "SymTab/Autogen.h"              // for SizeType | 
|---|
|  | 35 | #include "SymTab/Indexer.h"              // for Indexer | 
|---|
|  | 36 | #include "SynTree/Declaration.h"         // for ObjectDecl, TypeDecl, Declar... | 
|---|
|  | 37 | #include "SynTree/Expression.h"          // for Expression, CastExpr, InitExpr | 
|---|
|  | 38 | #include "SynTree/Initializer.h"         // for ConstructorInit, SingleInit | 
|---|
|  | 39 | #include "SynTree/Statement.h"           // for ForStmt, Statement, BranchStmt | 
|---|
|  | 40 | #include "SynTree/Type.h"                // for Type, BasicType, PointerType | 
|---|
|  | 41 | #include "SynTree/TypeSubstitution.h"    // for TypeSubstitution | 
|---|
|  | 42 | #include "SynTree/Visitor.h"             // for acceptAll, maybeAccept | 
|---|
| [0a60c04] | 43 | #include "Tuples/Tuples.h" | 
|---|
| [ea6332d] | 44 | #include "typeops.h"                     // for extractResultType | 
|---|
| [1dcd9554] | 45 | #include "Unify.h"                       // for unify | 
|---|
| [51b73452] | 46 |  | 
|---|
| [d9a0e76] | 47 | using namespace std; | 
|---|
| [51b73452] | 48 |  | 
|---|
| [d9a0e76] | 49 | namespace ResolvExpr { | 
|---|
| [0a60c04] | 50 | struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting, public WithStmtsToAdd { | 
|---|
| [a4ca48c] | 51 | Resolver() {} | 
|---|
|  | 52 | Resolver( const SymTab::Indexer & other ) { | 
|---|
|  | 53 | indexer = other; | 
|---|
| [1d2b64f] | 54 | } | 
|---|
| [71f4e4f] | 55 |  | 
|---|
| [a4ca48c] | 56 | void previsit( FunctionDecl *functionDecl ); | 
|---|
|  | 57 | void postvisit( FunctionDecl *functionDecl ); | 
|---|
| [3c398b6] | 58 | void previsit( ObjectDecl *objectDecll ); | 
|---|
| [a4ca48c] | 59 | void previsit( TypeDecl *typeDecl ); | 
|---|
|  | 60 | void previsit( EnumDecl * enumDecl ); | 
|---|
| [bd87b138] | 61 | void previsit( StaticAssertDecl * assertDecl ); | 
|---|
| [a4ca48c] | 62 |  | 
|---|
|  | 63 | void previsit( ArrayType * at ); | 
|---|
|  | 64 | void previsit( PointerType * at ); | 
|---|
|  | 65 |  | 
|---|
|  | 66 | void previsit( ExprStmt *exprStmt ); | 
|---|
|  | 67 | void previsit( AsmExpr *asmExpr ); | 
|---|
|  | 68 | void previsit( AsmStmt *asmStmt ); | 
|---|
|  | 69 | void previsit( IfStmt *ifStmt ); | 
|---|
|  | 70 | void previsit( WhileStmt *whileStmt ); | 
|---|
|  | 71 | void previsit( ForStmt *forStmt ); | 
|---|
|  | 72 | void previsit( SwitchStmt *switchStmt ); | 
|---|
|  | 73 | void previsit( CaseStmt *caseStmt ); | 
|---|
|  | 74 | void previsit( BranchStmt *branchStmt ); | 
|---|
|  | 75 | void previsit( ReturnStmt *returnStmt ); | 
|---|
|  | 76 | void previsit( ThrowStmt *throwStmt ); | 
|---|
|  | 77 | void previsit( CatchStmt *catchStmt ); | 
|---|
| [695e00d] | 78 | void previsit( WaitForStmt * stmt ); | 
|---|
| [882ad37] | 79 | void previsit( WithStmt * withStmt ); | 
|---|
| [a4ca48c] | 80 |  | 
|---|
|  | 81 | void previsit( SingleInit *singleInit ); | 
|---|
|  | 82 | void previsit( ListInit *listInit ); | 
|---|
|  | 83 | void previsit( ConstructorInit *ctorInit ); | 
|---|
| [a32b204] | 84 | private: | 
|---|
| [c28a038d] | 85 | typedef std::list< Initializer * >::iterator InitIterator; | 
|---|
| [94b4364] | 86 |  | 
|---|
| [40e636a] | 87 | template< typename PtrType > | 
|---|
|  | 88 | void handlePtrType( PtrType * type ); | 
|---|
|  | 89 |  | 
|---|
| [c28a038d] | 90 | void resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ); | 
|---|
|  | 91 | void fallbackInit( ConstructorInit * ctorInit ); | 
|---|
| [b726084] | 92 |  | 
|---|
| [77971f6] | 93 | Type * functionReturn = nullptr; | 
|---|
| [e4d829b] | 94 | CurrentObject currentObject = nullptr; | 
|---|
| [a436947] | 95 | bool inEnumDecl = false; | 
|---|
| [a32b204] | 96 | }; | 
|---|
| [d9a0e76] | 97 |  | 
|---|
| [a32b204] | 98 | void resolve( std::list< Declaration * > translationUnit ) { | 
|---|
| [a4ca48c] | 99 | PassVisitor<Resolver> resolver; | 
|---|
| [a32b204] | 100 | acceptAll( translationUnit, resolver ); | 
|---|
| [d9a0e76] | 101 | } | 
|---|
|  | 102 |  | 
|---|
| [8b11840] | 103 | void resolveDecl( Declaration * decl, const SymTab::Indexer &indexer ) { | 
|---|
|  | 104 | PassVisitor<Resolver> resolver( indexer ); | 
|---|
|  | 105 | maybeAccept( decl, resolver ); | 
|---|
|  | 106 | } | 
|---|
|  | 107 |  | 
|---|
| [c71b256] | 108 | namespace { | 
|---|
|  | 109 | struct DeleteFinder : public WithShortCircuiting        { | 
|---|
|  | 110 | DeletedExpr * delExpr = nullptr; | 
|---|
|  | 111 | void previsit( DeletedExpr * expr ) { | 
|---|
|  | 112 | if ( delExpr ) visit_children = false; | 
|---|
|  | 113 | else delExpr = expr; | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | void previsit( Expression * ) { | 
|---|
|  | 117 | if ( delExpr ) visit_children = false; | 
|---|
|  | 118 | } | 
|---|
|  | 119 | }; | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | DeletedExpr * findDeletedExpr( Expression * expr ) { | 
|---|
|  | 123 | PassVisitor<DeleteFinder> finder; | 
|---|
|  | 124 | expr->accept( finder ); | 
|---|
|  | 125 | return finder.pass.delExpr; | 
|---|
| [d9a0e76] | 126 | } | 
|---|
| [a32b204] | 127 |  | 
|---|
|  | 128 | namespace { | 
|---|
| [cdb990a] | 129 | struct StripCasts { | 
|---|
|  | 130 | Expression * postmutate( CastExpr * castExpr ) { | 
|---|
|  | 131 | if ( castExpr->isGenerated && ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, SymTab::Indexer() ) ) { | 
|---|
|  | 132 | // generated cast is to the same type as its argument, so it's unnecessary -- remove it | 
|---|
|  | 133 | Expression * expr = castExpr->arg; | 
|---|
|  | 134 | castExpr->arg = nullptr; | 
|---|
|  | 135 | std::swap( expr->env, castExpr->env ); | 
|---|
|  | 136 | return expr; | 
|---|
|  | 137 | } | 
|---|
|  | 138 | return castExpr; | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
|  | 141 | static void strip( Expression *& expr ) { | 
|---|
|  | 142 | PassVisitor<StripCasts> stripper; | 
|---|
|  | 143 | expr = expr->acceptMutator( stripper ); | 
|---|
|  | 144 | } | 
|---|
|  | 145 | }; | 
|---|
|  | 146 |  | 
|---|
|  | 147 | void finishExpr( Expression *&expr, const TypeEnvironment &env, TypeSubstitution * oldenv = nullptr ) { | 
|---|
| [7664fad] | 148 | expr->env = oldenv ? oldenv->clone() : new TypeSubstitution; | 
|---|
| [cdb990a] | 149 | env.makeSubstitution( *expr->env ); | 
|---|
|  | 150 | StripCasts::strip( expr ); // remove unnecessary casts that may be buried in an expression | 
|---|
| [a32b204] | 151 | } | 
|---|
| [0a22cda] | 152 |  | 
|---|
|  | 153 | void removeExtraneousCast( Expression *& expr, const SymTab::Indexer & indexer ) { | 
|---|
|  | 154 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { | 
|---|
|  | 155 | if ( ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) { | 
|---|
|  | 156 | // cast is to the same type as its argument, so it's unnecessary -- remove it | 
|---|
|  | 157 | expr = castExpr->arg; | 
|---|
|  | 158 | castExpr->arg = nullptr; | 
|---|
|  | 159 | std::swap( expr->env, castExpr->env ); | 
|---|
|  | 160 | delete castExpr; | 
|---|
|  | 161 | } | 
|---|
|  | 162 | } | 
|---|
|  | 163 | } | 
|---|
| [db4ecc5] | 164 | } // namespace | 
|---|
| [a32b204] | 165 |  | 
|---|
| [8f98b78] | 166 | namespace { | 
|---|
| [c71b256] | 167 | void findUnfinishedKindExpression(Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, bool adjust = false, bool prune = true, bool failFast = true) { | 
|---|
|  | 168 | assertf( untyped, "expected a non-null expression." ); | 
|---|
| [8587878e] | 169 | TypeEnvironment env; | 
|---|
|  | 170 | AlternativeFinder finder( indexer, env ); | 
|---|
| [c71b256] | 171 | finder.find( untyped, adjust, prune, failFast ); | 
|---|
|  | 172 |  | 
|---|
|  | 173 | #if 0 | 
|---|
|  | 174 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
|  | 175 | std::cerr << "untyped expr is "; | 
|---|
|  | 176 | untyped->print( std::cerr ); | 
|---|
|  | 177 | std::cerr << std::endl << "alternatives are:"; | 
|---|
|  | 178 | for ( const Alternative & alt : finder.get_alternatives() ) { | 
|---|
|  | 179 | alt.print( std::cerr ); | 
|---|
|  | 180 | } // for | 
|---|
|  | 181 | } // if | 
|---|
|  | 182 | #endif | 
|---|
| [8587878e] | 183 |  | 
|---|
|  | 184 | AltList candidates; | 
|---|
|  | 185 | for ( Alternative & alt : finder.get_alternatives() ) { | 
|---|
| [c71b256] | 186 | if ( pred( alt ) ) { | 
|---|
| [8587878e] | 187 | candidates.push_back( std::move( alt ) ); | 
|---|
|  | 188 | } | 
|---|
|  | 189 | } | 
|---|
|  | 190 |  | 
|---|
| [c71b256] | 191 | // xxx - if > 1 alternative with same cost, ignore deleted and pick from remaining | 
|---|
| [8587878e] | 192 | // choose the lowest cost expression among the candidates | 
|---|
|  | 193 | AltList winners; | 
|---|
|  | 194 | findMinCost( candidates.begin(), candidates.end(), back_inserter( winners ) ); | 
|---|
|  | 195 | if ( winners.size() == 0 ) { | 
|---|
| [a16764a6] | 196 | SemanticError( untyped, toString( "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""), "expression: ") ); | 
|---|
| [8587878e] | 197 | } else if ( winners.size() != 1 ) { | 
|---|
|  | 198 | std::ostringstream stream; | 
|---|
| [c71b256] | 199 | stream << "Cannot choose between " << winners.size() << " alternatives for " << kindStr << (kindStr != "" ? " " : "") << "expression\n"; | 
|---|
| [8587878e] | 200 | untyped->print( stream ); | 
|---|
| [93401f8] | 201 | stream << " Alternatives are:\n"; | 
|---|
| [8587878e] | 202 | printAlts( winners, stream, 1 ); | 
|---|
| [a16764a6] | 203 | SemanticError( untyped->location, stream.str() ); | 
|---|
| [8587878e] | 204 | } | 
|---|
|  | 205 |  | 
|---|
|  | 206 | // there is one unambiguous interpretation - move the expression into the with statement | 
|---|
| [c71b256] | 207 | Alternative & choice = winners.front(); | 
|---|
|  | 208 | if ( findDeletedExpr( choice.expr ) ) { | 
|---|
| [2a08c25] | 209 | SemanticError( untyped->location, choice.expr, "Unique best alternative includes deleted identifier in " ); | 
|---|
| [c71b256] | 210 | } | 
|---|
|  | 211 | alt = std::move( choice ); | 
|---|
|  | 212 | } | 
|---|
|  | 213 |  | 
|---|
|  | 214 | /// resolve `untyped` to the expression whose alternative satisfies `pred` with the lowest cost; kindStr is used for providing better error messages | 
|---|
|  | 215 | void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, bool adjust = false, bool prune = true, bool failFast = true) { | 
|---|
|  | 216 | if ( ! untyped ) return; | 
|---|
|  | 217 | Alternative choice; | 
|---|
|  | 218 | findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, adjust, prune, failFast ); | 
|---|
|  | 219 | finishExpr( choice.expr, choice.env, untyped->env ); | 
|---|
| [8587878e] | 220 | delete untyped; | 
|---|
| [c71b256] | 221 | untyped = choice.expr; | 
|---|
|  | 222 | choice.expr = nullptr; | 
|---|
| [8587878e] | 223 | } | 
|---|
|  | 224 |  | 
|---|
| [c71b256] | 225 | bool standardAlternativeFilter( const Alternative & ) { | 
|---|
|  | 226 | // currently don't need to filter, under normal circumstances. | 
|---|
|  | 227 | // in the future, this may be useful for removing deleted expressions | 
|---|
|  | 228 | return true; | 
|---|
|  | 229 | } | 
|---|
|  | 230 | } // namespace | 
|---|
|  | 231 |  | 
|---|
|  | 232 | // used in resolveTypeof | 
|---|
|  | 233 | Expression * resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { | 
|---|
|  | 234 | TypeEnvironment env; | 
|---|
|  | 235 | return resolveInVoidContext( expr, indexer, env ); | 
|---|
|  | 236 | } | 
|---|
|  | 237 |  | 
|---|
|  | 238 | Expression * resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ) { | 
|---|
|  | 239 | // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0 | 
|---|
|  | 240 | // interpretations, an exception has already been thrown. | 
|---|
|  | 241 | assertf( expr, "expected a non-null expression." ); | 
|---|
|  | 242 |  | 
|---|
|  | 243 | static CastExpr untyped( nullptr ); // cast to void | 
|---|
| [2a08c25] | 244 | untyped.location = expr->location; | 
|---|
| [c71b256] | 245 |  | 
|---|
|  | 246 | // set up and resolve expression cast to void | 
|---|
|  | 247 | untyped.arg = expr; | 
|---|
|  | 248 | Alternative choice; | 
|---|
|  | 249 | findUnfinishedKindExpression( &untyped, choice, indexer, "", standardAlternativeFilter, true ); | 
|---|
|  | 250 | CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( choice.expr ); | 
|---|
|  | 251 | env = std::move( choice.env ); | 
|---|
|  | 252 |  | 
|---|
|  | 253 | // clean up resolved expression | 
|---|
|  | 254 | Expression * ret = castExpr->arg; | 
|---|
|  | 255 | castExpr->arg = nullptr; | 
|---|
|  | 256 |  | 
|---|
|  | 257 | // unlink the arg so that it isn't deleted twice at the end of the program | 
|---|
|  | 258 | untyped.arg = nullptr; | 
|---|
|  | 259 | return ret; | 
|---|
|  | 260 | } | 
|---|
|  | 261 |  | 
|---|
|  | 262 | void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 263 | resetTyVarRenaming(); | 
|---|
|  | 264 | TypeEnvironment env; | 
|---|
|  | 265 | Expression * newExpr = resolveInVoidContext( untyped, indexer, env ); | 
|---|
|  | 266 | finishExpr( newExpr, env, untyped->env ); | 
|---|
|  | 267 | delete untyped; | 
|---|
|  | 268 | untyped = newExpr; | 
|---|
|  | 269 | } | 
|---|
|  | 270 |  | 
|---|
|  | 271 | void findSingleExpression( Expression *&untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 272 | findKindExpression( untyped, indexer, "", standardAlternativeFilter ); | 
|---|
|  | 273 | } | 
|---|
|  | 274 |  | 
|---|
|  | 275 | void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) { | 
|---|
|  | 276 | assert( untyped && type ); | 
|---|
| [2a08c25] | 277 | // transfer location to generated cast for error purposes | 
|---|
|  | 278 | CodeLocation location = untyped->location; | 
|---|
| [c71b256] | 279 | untyped = new CastExpr( untyped, type ); | 
|---|
| [2a08c25] | 280 | untyped->location = location; | 
|---|
| [c71b256] | 281 | findSingleExpression( untyped, indexer ); | 
|---|
|  | 282 | removeExtraneousCast( untyped, indexer ); | 
|---|
|  | 283 | } | 
|---|
|  | 284 |  | 
|---|
|  | 285 | namespace { | 
|---|
|  | 286 | bool isIntegralType( const Alternative & alt ) { | 
|---|
|  | 287 | Type * type = alt.expr->result; | 
|---|
| [a32b204] | 288 | if ( dynamic_cast< EnumInstType * >( type ) ) { | 
|---|
|  | 289 | return true; | 
|---|
|  | 290 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { | 
|---|
|  | 291 | return bt->isInteger(); | 
|---|
| [89e6ffc] | 292 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { | 
|---|
|  | 293 | return true; | 
|---|
| [a32b204] | 294 | } else { | 
|---|
|  | 295 | return false; | 
|---|
|  | 296 | } // if | 
|---|
|  | 297 | } | 
|---|
| [71f4e4f] | 298 |  | 
|---|
| [08da53d] | 299 | void findIntegralExpression( Expression *& untyped, const SymTab::Indexer &indexer ) { | 
|---|
| [8587878e] | 300 | findKindExpression( untyped, indexer, "condition", isIntegralType ); | 
|---|
| [a32b204] | 301 | } | 
|---|
|  | 302 | } | 
|---|
| [71f4e4f] | 303 |  | 
|---|
| [a4ca48c] | 304 | void Resolver::previsit( ObjectDecl *objectDecl ) { | 
|---|
|  | 305 | Type *new_type = resolveTypeof( objectDecl->get_type(), indexer ); | 
|---|
| [a32b204] | 306 | objectDecl->set_type( new_type ); | 
|---|
| [3cfe27f] | 307 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable | 
|---|
|  | 308 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes | 
|---|
|  | 309 | // initContext because of a function type can contain object declarations in the return and parameter types. So | 
|---|
|  | 310 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting | 
|---|
|  | 311 | // the RHS. | 
|---|
| [a4ca48c] | 312 | GuardValue( currentObject ); | 
|---|
| [e4d829b] | 313 | currentObject = CurrentObject( objectDecl->get_type() ); | 
|---|
|  | 314 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { | 
|---|
| [a436947] | 315 | // enumerator initializers should not use the enum type to initialize, since | 
|---|
|  | 316 | // the enum type is still incomplete at this point. Use signed int instead. | 
|---|
| [e4d829b] | 317 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); | 
|---|
| [a436947] | 318 | } | 
|---|
| [bfbf97f] | 319 | } | 
|---|
|  | 320 |  | 
|---|
| [40e636a] | 321 | template< typename PtrType > | 
|---|
|  | 322 | void Resolver::handlePtrType( PtrType * type ) { | 
|---|
|  | 323 | if ( type->get_dimension() ) { | 
|---|
| [08da53d] | 324 | findSingleExpression( type->dimension, SymTab::SizeType->clone(), indexer ); | 
|---|
| [d1d17f5] | 325 | } | 
|---|
| [40e636a] | 326 | } | 
|---|
|  | 327 |  | 
|---|
| [a4ca48c] | 328 | void Resolver::previsit( ArrayType * at ) { | 
|---|
| [40e636a] | 329 | handlePtrType( at ); | 
|---|
| [a32b204] | 330 | } | 
|---|
| [94b4364] | 331 |  | 
|---|
| [a4ca48c] | 332 | void Resolver::previsit( PointerType * pt ) { | 
|---|
| [40e636a] | 333 | handlePtrType( pt ); | 
|---|
|  | 334 | } | 
|---|
|  | 335 |  | 
|---|
| [a4ca48c] | 336 | void Resolver::previsit( TypeDecl *typeDecl ) { | 
|---|
| [a32b204] | 337 | if ( typeDecl->get_base() ) { | 
|---|
| [a4ca48c] | 338 | Type *new_type = resolveTypeof( typeDecl->get_base(), indexer ); | 
|---|
| [a32b204] | 339 | typeDecl->set_base( new_type ); | 
|---|
|  | 340 | } // if | 
|---|
|  | 341 | } | 
|---|
| [94b4364] | 342 |  | 
|---|
| [a4ca48c] | 343 | void Resolver::previsit( FunctionDecl *functionDecl ) { | 
|---|
| [d9a0e76] | 344 | #if 0 | 
|---|
| [a4ca48c] | 345 | std::cerr << "resolver visiting functiondecl "; | 
|---|
|  | 346 | functionDecl->print( std::cerr ); | 
|---|
|  | 347 | std::cerr << std::endl; | 
|---|
| [d9a0e76] | 348 | #endif | 
|---|
| [c28a038d] | 349 | Type *new_type = resolveTypeof( functionDecl->type, indexer ); | 
|---|
| [a32b204] | 350 | functionDecl->set_type( new_type ); | 
|---|
| [a4ca48c] | 351 | GuardValue( functionReturn ); | 
|---|
| [60914351] | 352 | functionReturn = ResolvExpr::extractResultType( functionDecl->type ); | 
|---|
|  | 353 |  | 
|---|
|  | 354 | { | 
|---|
|  | 355 | // resolve with-exprs with parameters in scope and add any newly generated declarations to the | 
|---|
|  | 356 | // front of the function body. | 
|---|
|  | 357 | auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this](){ indexer.leaveScope(); } ); | 
|---|
|  | 358 | indexer.addFunctionType( functionDecl->type ); | 
|---|
|  | 359 | std::list< Statement * > newStmts; | 
|---|
|  | 360 | resolveWithExprs( functionDecl->withExprs, newStmts ); | 
|---|
| [a33fdbe] | 361 | if ( functionDecl->statements ) { | 
|---|
|  | 362 | functionDecl->statements->kids.splice( functionDecl->statements->kids.begin(), newStmts ); | 
|---|
|  | 363 | } else { | 
|---|
|  | 364 | assertf( functionDecl->withExprs.empty() && newStmts.empty(), "Function %s without a body has with-clause and/or generated with declarations.", functionDecl->name.c_str() ); | 
|---|
|  | 365 | } | 
|---|
| [60914351] | 366 | } | 
|---|
| [a4ca48c] | 367 | } | 
|---|
| [88d1066] | 368 |  | 
|---|
| [a4ca48c] | 369 | void Resolver::postvisit( FunctionDecl *functionDecl ) { | 
|---|
| [88d1066] | 370 | // default value expressions have an environment which shouldn't be there and trips up later passes. | 
|---|
|  | 371 | // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently | 
|---|
|  | 372 | // see how it's useful. | 
|---|
| [c28a038d] | 373 | for ( Declaration * d : functionDecl->type->parameters ) { | 
|---|
| [88d1066] | 374 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { | 
|---|
| [c28a038d] | 375 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->init ) ) { | 
|---|
|  | 376 | delete init->value->env; | 
|---|
|  | 377 | init->value->env = nullptr; | 
|---|
| [88d1066] | 378 | } | 
|---|
|  | 379 | } | 
|---|
|  | 380 | } | 
|---|
| [a32b204] | 381 | } | 
|---|
| [51b73452] | 382 |  | 
|---|
| [a4ca48c] | 383 | void Resolver::previsit( EnumDecl * ) { | 
|---|
| [a436947] | 384 | // in case we decide to allow nested enums | 
|---|
| [a4ca48c] | 385 | GuardValue( inEnumDecl ); | 
|---|
| [a436947] | 386 | inEnumDecl = true; | 
|---|
|  | 387 | } | 
|---|
|  | 388 |  | 
|---|
| [bd87b138] | 389 | void Resolver::previsit( StaticAssertDecl * assertDecl ) { | 
|---|
|  | 390 | findIntegralExpression( assertDecl->condition, indexer ); | 
|---|
|  | 391 | } | 
|---|
|  | 392 |  | 
|---|
| [a4ca48c] | 393 | void Resolver::previsit( ExprStmt *exprStmt ) { | 
|---|
|  | 394 | visit_children = false; | 
|---|
| [08da53d] | 395 | assertf( exprStmt->expr, "ExprStmt has null Expression in resolver" ); | 
|---|
|  | 396 | findVoidExpression( exprStmt->expr, indexer ); | 
|---|
| [a32b204] | 397 | } | 
|---|
| [51b73452] | 398 |  | 
|---|
| [a4ca48c] | 399 | void Resolver::previsit( AsmExpr *asmExpr ) { | 
|---|
|  | 400 | visit_children = false; | 
|---|
| [08da53d] | 401 | findVoidExpression( asmExpr->operand, indexer ); | 
|---|
| [7f5566b] | 402 | if ( asmExpr->get_inout() ) { | 
|---|
| [08da53d] | 403 | findVoidExpression( asmExpr->inout, indexer ); | 
|---|
| [7f5566b] | 404 | } // if | 
|---|
|  | 405 | } | 
|---|
|  | 406 |  | 
|---|
| [a4ca48c] | 407 | void Resolver::previsit( AsmStmt *asmStmt ) { | 
|---|
|  | 408 | visit_children = false; | 
|---|
|  | 409 | acceptAll( asmStmt->get_input(), *visitor ); | 
|---|
|  | 410 | acceptAll( asmStmt->get_output(), *visitor ); | 
|---|
| [7f5566b] | 411 | } | 
|---|
|  | 412 |  | 
|---|
| [a4ca48c] | 413 | void Resolver::previsit( IfStmt *ifStmt ) { | 
|---|
| [8587878e] | 414 | findIntegralExpression( ifStmt->condition, indexer ); | 
|---|
| [a32b204] | 415 | } | 
|---|
| [51b73452] | 416 |  | 
|---|
| [a4ca48c] | 417 | void Resolver::previsit( WhileStmt *whileStmt ) { | 
|---|
| [8587878e] | 418 | findIntegralExpression( whileStmt->condition, indexer ); | 
|---|
| [a32b204] | 419 | } | 
|---|
| [51b73452] | 420 |  | 
|---|
| [a4ca48c] | 421 | void Resolver::previsit( ForStmt *forStmt ) { | 
|---|
| [08da53d] | 422 | if ( forStmt->condition ) { | 
|---|
| [8587878e] | 423 | findIntegralExpression( forStmt->condition, indexer ); | 
|---|
| [a32b204] | 424 | } // if | 
|---|
| [71f4e4f] | 425 |  | 
|---|
| [08da53d] | 426 | if ( forStmt->increment ) { | 
|---|
|  | 427 | findVoidExpression( forStmt->increment, indexer ); | 
|---|
| [a32b204] | 428 | } // if | 
|---|
|  | 429 | } | 
|---|
| [51b73452] | 430 |  | 
|---|
| [a4ca48c] | 431 | void Resolver::previsit( SwitchStmt *switchStmt ) { | 
|---|
|  | 432 | GuardValue( currentObject ); | 
|---|
| [08da53d] | 433 | findIntegralExpression( switchStmt->condition, indexer ); | 
|---|
| [71f4e4f] | 434 |  | 
|---|
| [08da53d] | 435 | currentObject = CurrentObject( switchStmt->condition->result ); | 
|---|
| [a32b204] | 436 | } | 
|---|
| [51b73452] | 437 |  | 
|---|
| [a4ca48c] | 438 | void Resolver::previsit( CaseStmt *caseStmt ) { | 
|---|
| [cdb990a] | 439 | if ( caseStmt->condition ) { | 
|---|
| [e4d829b] | 440 | std::list< InitAlternative > initAlts = currentObject.getOptions(); | 
|---|
|  | 441 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); | 
|---|
| [08da53d] | 442 | // must remove cast from case statement because RangeExpr cannot be cast. | 
|---|
|  | 443 | Expression * newExpr = new CastExpr( caseStmt->condition, initAlts.front().type->clone() ); | 
|---|
|  | 444 | findSingleExpression( newExpr, indexer ); | 
|---|
| [cdb990a] | 445 | // case condition cannot have a cast in C, so it must be removed, regardless of whether it performs a conversion. | 
|---|
|  | 446 | // Ideally we would perform the conversion internally here. | 
|---|
|  | 447 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( newExpr ) ) { | 
|---|
|  | 448 | newExpr = castExpr->arg; | 
|---|
|  | 449 | castExpr->arg = nullptr; | 
|---|
|  | 450 | std::swap( newExpr->env, castExpr->env ); | 
|---|
|  | 451 | delete castExpr; | 
|---|
|  | 452 | } | 
|---|
|  | 453 | caseStmt->condition = newExpr; | 
|---|
| [32b8144] | 454 | } | 
|---|
| [a32b204] | 455 | } | 
|---|
| [51b73452] | 456 |  | 
|---|
| [a4ca48c] | 457 | void Resolver::previsit( BranchStmt *branchStmt ) { | 
|---|
|  | 458 | visit_children = false; | 
|---|
| [de62360d] | 459 | // must resolve the argument for a computed goto | 
|---|
|  | 460 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement | 
|---|
| [08da53d] | 461 | if ( branchStmt->computedTarget ) { | 
|---|
|  | 462 | // computed goto argument is void * | 
|---|
|  | 463 | findSingleExpression( branchStmt->computedTarget, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), indexer ); | 
|---|
| [de62360d] | 464 | } // if | 
|---|
|  | 465 | } // if | 
|---|
|  | 466 | } | 
|---|
|  | 467 |  | 
|---|
| [a4ca48c] | 468 | void Resolver::previsit( ReturnStmt *returnStmt ) { | 
|---|
|  | 469 | visit_children = false; | 
|---|
| [08da53d] | 470 | if ( returnStmt->expr ) { | 
|---|
|  | 471 | findSingleExpression( returnStmt->expr, functionReturn->clone(), indexer ); | 
|---|
| [a32b204] | 472 | } // if | 
|---|
|  | 473 | } | 
|---|
| [51b73452] | 474 |  | 
|---|
| [a4ca48c] | 475 | void Resolver::previsit( ThrowStmt *throwStmt ) { | 
|---|
|  | 476 | visit_children = false; | 
|---|
| [cbce272] | 477 | // TODO: Replace *exception type with &exception type. | 
|---|
| [307a732] | 478 | if ( throwStmt->get_expr() ) { | 
|---|
| [cbce272] | 479 | StructDecl * exception_decl = | 
|---|
| [36982fc] | 480 | indexer.lookupStruct( "__cfaabi_ehm__base_exception_t" ); | 
|---|
| [cbce272] | 481 | assert( exception_decl ); | 
|---|
| [08da53d] | 482 | Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, exception_decl ) ); | 
|---|
|  | 483 | findSingleExpression( throwStmt->expr, exceptType, indexer ); | 
|---|
| [307a732] | 484 | } | 
|---|
|  | 485 | } | 
|---|
|  | 486 |  | 
|---|
| [a4ca48c] | 487 | void Resolver::previsit( CatchStmt *catchStmt ) { | 
|---|
| [08da53d] | 488 | if ( catchStmt->cond ) { | 
|---|
|  | 489 | findSingleExpression( catchStmt->cond, new BasicType( noQualifiers, BasicType::Bool ), indexer ); | 
|---|
| [cbce272] | 490 | } | 
|---|
|  | 491 | } | 
|---|
|  | 492 |  | 
|---|
| [1dcd9554] | 493 | template< typename iterator_t > | 
|---|
|  | 494 | inline bool advance_to_mutex( iterator_t & it, const iterator_t & end ) { | 
|---|
|  | 495 | while( it != end && !(*it)->get_type()->get_mutex() ) { | 
|---|
|  | 496 | it++; | 
|---|
|  | 497 | } | 
|---|
|  | 498 |  | 
|---|
|  | 499 | return it != end; | 
|---|
|  | 500 | } | 
|---|
|  | 501 |  | 
|---|
| [695e00d] | 502 | void Resolver::previsit( WaitForStmt * stmt ) { | 
|---|
| [8f98b78] | 503 | visit_children = false; | 
|---|
| [1dcd9554] | 504 |  | 
|---|
|  | 505 | // Resolve all clauses first | 
|---|
|  | 506 | for( auto& clause : stmt->clauses ) { | 
|---|
|  | 507 |  | 
|---|
|  | 508 | TypeEnvironment env; | 
|---|
| [8f98b78] | 509 | AlternativeFinder funcFinder( indexer, env ); | 
|---|
| [1dcd9554] | 510 |  | 
|---|
|  | 511 | // Find all alternatives for a function in canonical form | 
|---|
|  | 512 | funcFinder.findWithAdjustment( clause.target.function ); | 
|---|
|  | 513 |  | 
|---|
|  | 514 | if ( funcFinder.get_alternatives().empty() ) { | 
|---|
|  | 515 | stringstream ss; | 
|---|
|  | 516 | ss << "Use of undeclared indentifier '"; | 
|---|
|  | 517 | ss << strict_dynamic_cast<NameExpr*>( clause.target.function )->name; | 
|---|
|  | 518 | ss << "' in call to waitfor"; | 
|---|
| [a16764a6] | 519 | SemanticError( stmt->location, ss.str() ); | 
|---|
| [1dcd9554] | 520 | } | 
|---|
|  | 521 |  | 
|---|
| [b9f383f] | 522 | if(clause.target.arguments.empty()) { | 
|---|
|  | 523 | SemanticError( stmt->location, "Waitfor clause must have at least one mutex parameter"); | 
|---|
|  | 524 | } | 
|---|
|  | 525 |  | 
|---|
| [1dcd9554] | 526 | // Find all alternatives for all arguments in canonical form | 
|---|
| [bd4f2e9] | 527 | std::vector< AlternativeFinder > argAlternatives; | 
|---|
| [1dcd9554] | 528 | funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) ); | 
|---|
|  | 529 |  | 
|---|
|  | 530 | // List all combinations of arguments | 
|---|
| [bd4f2e9] | 531 | std::vector< AltList > possibilities; | 
|---|
| [1dcd9554] | 532 | combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); | 
|---|
|  | 533 |  | 
|---|
|  | 534 | AltList                func_candidates; | 
|---|
|  | 535 | std::vector< AltList > args_candidates; | 
|---|
|  | 536 |  | 
|---|
|  | 537 | // For every possible function : | 
|---|
|  | 538 | //      try matching the arguments to the parameters | 
|---|
|  | 539 | //      not the other way around because we have more arguments than parameters | 
|---|
| [a16764a6] | 540 | SemanticErrorException errors; | 
|---|
| [1dcd9554] | 541 | for ( Alternative & func : funcFinder.get_alternatives() ) { | 
|---|
|  | 542 | try { | 
|---|
|  | 543 | PointerType * pointer = dynamic_cast< PointerType* >( func.expr->get_result()->stripReferences() ); | 
|---|
|  | 544 | if( !pointer ) { | 
|---|
| [a16764a6] | 545 | SemanticError( func.expr->get_result(), "candidate not viable: not a pointer type\n" ); | 
|---|
| [1dcd9554] | 546 | } | 
|---|
|  | 547 |  | 
|---|
|  | 548 | FunctionType * function = dynamic_cast< FunctionType* >( pointer->get_base() ); | 
|---|
|  | 549 | if( !function ) { | 
|---|
| [a16764a6] | 550 | SemanticError( pointer->get_base(), "candidate not viable: not a function type\n" ); | 
|---|
| [1dcd9554] | 551 | } | 
|---|
|  | 552 |  | 
|---|
|  | 553 |  | 
|---|
|  | 554 | { | 
|---|
|  | 555 | auto param     = function->parameters.begin(); | 
|---|
|  | 556 | auto param_end = function->parameters.end(); | 
|---|
|  | 557 |  | 
|---|
|  | 558 | if( !advance_to_mutex( param, param_end ) ) { | 
|---|
| [a16764a6] | 559 | SemanticError(function, "candidate function not viable: no mutex parameters\n"); | 
|---|
| [1dcd9554] | 560 | } | 
|---|
|  | 561 | } | 
|---|
|  | 562 |  | 
|---|
|  | 563 | Alternative newFunc( func ); | 
|---|
|  | 564 | // Strip reference from function | 
|---|
| [a181494] | 565 | referenceToRvalueConversion( newFunc.expr, newFunc.cost ); | 
|---|
| [1dcd9554] | 566 |  | 
|---|
|  | 567 | // For all the set of arguments we have try to match it with the parameter of the current function alternative | 
|---|
|  | 568 | for ( auto & argsList : possibilities ) { | 
|---|
|  | 569 |  | 
|---|
|  | 570 | try { | 
|---|
|  | 571 | // Declare data structures need for resolution | 
|---|
|  | 572 | OpenVarSet openVars; | 
|---|
|  | 573 | AssertionSet resultNeed, resultHave; | 
|---|
| [6f326b1] | 574 | TypeEnvironment resultEnv( func.env ); | 
|---|
|  | 575 | makeUnifiableVars( function, openVars, resultNeed ); | 
|---|
|  | 576 | // add all type variables as open variables now so that those not used in the parameter | 
|---|
|  | 577 | // list are still considered open. | 
|---|
|  | 578 | resultEnv.add( function->forall ); | 
|---|
| [1dcd9554] | 579 |  | 
|---|
|  | 580 | // Load type variables from arguemnts into one shared space | 
|---|
|  | 581 | simpleCombineEnvironments( argsList.begin(), argsList.end(), resultEnv ); | 
|---|
|  | 582 |  | 
|---|
|  | 583 | // Make sure we don't widen any existing bindings | 
|---|
|  | 584 | for ( auto & i : resultEnv ) { | 
|---|
|  | 585 | i.allowWidening = false; | 
|---|
|  | 586 | } | 
|---|
|  | 587 |  | 
|---|
|  | 588 | // Find any unbound type variables | 
|---|
|  | 589 | resultEnv.extractOpenVars( openVars ); | 
|---|
|  | 590 |  | 
|---|
|  | 591 | auto param     = function->parameters.begin(); | 
|---|
|  | 592 | auto param_end = function->parameters.end(); | 
|---|
|  | 593 |  | 
|---|
| [b9f383f] | 594 | int n_mutex_arg = 0; | 
|---|
|  | 595 |  | 
|---|
| [1dcd9554] | 596 | // For every arguments of its set, check if it matches one of the parameter | 
|---|
|  | 597 | // The order is important | 
|---|
|  | 598 | for( auto & arg : argsList ) { | 
|---|
|  | 599 |  | 
|---|
|  | 600 | // Ignore non-mutex arguments | 
|---|
|  | 601 | if( !advance_to_mutex( param, param_end ) ) { | 
|---|
|  | 602 | // We ran out of parameters but still have arguments | 
|---|
|  | 603 | // this function doesn't match | 
|---|
| [b9f383f] | 604 | SemanticError( function, toString("candidate function not viable: too many mutex arguments, expected ", n_mutex_arg, "\n" )); | 
|---|
| [1dcd9554] | 605 | } | 
|---|
|  | 606 |  | 
|---|
| [b9f383f] | 607 | n_mutex_arg++; | 
|---|
|  | 608 |  | 
|---|
| [1dcd9554] | 609 | // Check if the argument matches the parameter type in the current scope | 
|---|
| [b9f383f] | 610 | if( ! unify( arg.expr->get_result(), (*param)->get_type(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) { | 
|---|
| [1dcd9554] | 611 | // Type doesn't match | 
|---|
|  | 612 | stringstream ss; | 
|---|
|  | 613 | ss << "candidate function not viable: no known convertion from '"; | 
|---|
|  | 614 | (*param)->get_type()->print( ss ); | 
|---|
| [b9f383f] | 615 | ss << "' to '"; | 
|---|
|  | 616 | arg.expr->get_result()->print( ss ); | 
|---|
| [5248789] | 617 | ss << "' with env '"; | 
|---|
|  | 618 | resultEnv.print(ss); | 
|---|
| [1dcd9554] | 619 | ss << "'\n"; | 
|---|
| [a16764a6] | 620 | SemanticError( function, ss.str() ); | 
|---|
| [1dcd9554] | 621 | } | 
|---|
|  | 622 |  | 
|---|
|  | 623 | param++; | 
|---|
|  | 624 | } | 
|---|
|  | 625 |  | 
|---|
|  | 626 | // All arguments match ! | 
|---|
|  | 627 |  | 
|---|
|  | 628 | // Check if parameters are missing | 
|---|
|  | 629 | if( advance_to_mutex( param, param_end ) ) { | 
|---|
|  | 630 | // We ran out of arguments but still have parameters left | 
|---|
|  | 631 | // this function doesn't match | 
|---|
| [b9f383f] | 632 | SemanticError( function, toString("candidate function not viable: too few mutex arguments, expected ", n_mutex_arg, "\n" )); | 
|---|
| [1dcd9554] | 633 | } | 
|---|
|  | 634 |  | 
|---|
|  | 635 | // All parameters match ! | 
|---|
|  | 636 |  | 
|---|
|  | 637 | // Finish the expressions to tie in the proper environments | 
|---|
|  | 638 | finishExpr( newFunc.expr, resultEnv ); | 
|---|
|  | 639 | for( Alternative & alt : argsList ) { | 
|---|
|  | 640 | finishExpr( alt.expr, resultEnv ); | 
|---|
|  | 641 | } | 
|---|
|  | 642 |  | 
|---|
|  | 643 | // This is a match store it and save it for later | 
|---|
|  | 644 | func_candidates.push_back( newFunc ); | 
|---|
|  | 645 | args_candidates.push_back( argsList ); | 
|---|
|  | 646 |  | 
|---|
|  | 647 | } | 
|---|
| [a16764a6] | 648 | catch( SemanticErrorException &e ) { | 
|---|
| [1dcd9554] | 649 | errors.append( e ); | 
|---|
|  | 650 | } | 
|---|
|  | 651 | } | 
|---|
|  | 652 | } | 
|---|
| [a16764a6] | 653 | catch( SemanticErrorException &e ) { | 
|---|
| [1dcd9554] | 654 | errors.append( e ); | 
|---|
|  | 655 | } | 
|---|
|  | 656 | } | 
|---|
|  | 657 |  | 
|---|
|  | 658 | // Make sure we got the right number of arguments | 
|---|
| [a16764a6] | 659 | if( func_candidates.empty() )    { SemanticErrorException top( stmt->location, "No alternatives for function in call to waitfor"  ); top.append( errors ); throw top; } | 
|---|
|  | 660 | if( args_candidates.empty() )    { SemanticErrorException top( stmt->location, "No alternatives for arguments in call to waitfor" ); top.append( errors ); throw top; } | 
|---|
|  | 661 | if( func_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous function in call to waitfor"            ); top.append( errors ); throw top; } | 
|---|
|  | 662 | if( args_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous arguments in call to waitfor"           ); top.append( errors ); throw top; } | 
|---|
| [c71b256] | 663 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used. | 
|---|
| [1dcd9554] | 664 |  | 
|---|
|  | 665 | // Swap the results from the alternative with the unresolved values. | 
|---|
|  | 666 | // Alternatives will handle deletion on destruction | 
|---|
|  | 667 | std::swap( clause.target.function, func_candidates.front().expr ); | 
|---|
|  | 668 | for( auto arg_pair : group_iterate( clause.target.arguments, args_candidates.front() ) ) { | 
|---|
|  | 669 | std::swap ( std::get<0>( arg_pair), std::get<1>( arg_pair).expr ); | 
|---|
|  | 670 | } | 
|---|
|  | 671 |  | 
|---|
|  | 672 | // Resolve the conditions as if it were an IfStmt | 
|---|
|  | 673 | // Resolve the statments normally | 
|---|
| [08da53d] | 674 | findSingleExpression( clause.condition, this->indexer ); | 
|---|
| [8f98b78] | 675 | clause.statement->accept( *visitor ); | 
|---|
| [1dcd9554] | 676 | } | 
|---|
|  | 677 |  | 
|---|
|  | 678 |  | 
|---|
|  | 679 | if( stmt->timeout.statement ) { | 
|---|
|  | 680 | // Resolve the timeout as an size_t for now | 
|---|
|  | 681 | // Resolve the conditions as if it were an IfStmt | 
|---|
|  | 682 | // Resolve the statments normally | 
|---|
| [08da53d] | 683 | findSingleExpression( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer ); | 
|---|
|  | 684 | findSingleExpression( stmt->timeout.condition, this->indexer ); | 
|---|
| [8f98b78] | 685 | stmt->timeout.statement->accept( *visitor ); | 
|---|
| [1dcd9554] | 686 | } | 
|---|
|  | 687 |  | 
|---|
|  | 688 | if( stmt->orelse.statement ) { | 
|---|
|  | 689 | // Resolve the conditions as if it were an IfStmt | 
|---|
|  | 690 | // Resolve the statments normally | 
|---|
| [08da53d] | 691 | findSingleExpression( stmt->orelse.condition, this->indexer ); | 
|---|
| [8f98b78] | 692 | stmt->orelse.statement->accept( *visitor ); | 
|---|
| [1dcd9554] | 693 | } | 
|---|
|  | 694 | } | 
|---|
|  | 695 |  | 
|---|
| [c71b256] | 696 | bool isStructOrUnion( const Alternative & alt ) { | 
|---|
|  | 697 | Type * t = alt.expr->result->stripReferences(); | 
|---|
| [882ad37] | 698 | return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ); | 
|---|
|  | 699 | } | 
|---|
|  | 700 |  | 
|---|
| [c28a038d] | 701 | void Resolver::resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ) { | 
|---|
|  | 702 | for ( Expression *& expr : withExprs )  { | 
|---|
| [882ad37] | 703 | // only struct- and union-typed expressions are viable candidates | 
|---|
| [8587878e] | 704 | findKindExpression( expr, indexer, "with statement", isStructOrUnion ); | 
|---|
| [0a60c04] | 705 |  | 
|---|
|  | 706 | // if with expression might be impure, create a temporary so that it is evaluated once | 
|---|
|  | 707 | if ( Tuples::maybeImpure( expr ) ) { | 
|---|
|  | 708 | static UniqueName tmpNamer( "_with_tmp_" ); | 
|---|
|  | 709 | ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) ); | 
|---|
|  | 710 | expr = new VariableExpr( tmp ); | 
|---|
| [c28a038d] | 711 | newStmts.push_back( new DeclStmt( tmp ) ); | 
|---|
| [0a60c04] | 712 | if ( InitTweak::isConstructable( tmp->type ) ) { | 
|---|
|  | 713 | // generate ctor/dtor and resolve them | 
|---|
|  | 714 | tmp->init = InitTweak::genCtorInit( tmp ); | 
|---|
|  | 715 | tmp->accept( *visitor ); | 
|---|
|  | 716 | } | 
|---|
|  | 717 | } | 
|---|
| [882ad37] | 718 | } | 
|---|
|  | 719 | } | 
|---|
|  | 720 |  | 
|---|
| [c28a038d] | 721 | void Resolver::previsit( WithStmt * withStmt ) { | 
|---|
|  | 722 | resolveWithExprs( withStmt->exprs, stmtsToAddBefore ); | 
|---|
|  | 723 | } | 
|---|
|  | 724 |  | 
|---|
| [b5c5684] | 725 | template< typename T > | 
|---|
|  | 726 | bool isCharType( T t ) { | 
|---|
|  | 727 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { | 
|---|
| [71f4e4f] | 728 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || | 
|---|
| [b5c5684] | 729 | bt->get_kind() == BasicType::UnsignedChar; | 
|---|
|  | 730 | } | 
|---|
|  | 731 | return false; | 
|---|
|  | 732 | } | 
|---|
|  | 733 |  | 
|---|
| [a4ca48c] | 734 | void Resolver::previsit( SingleInit *singleInit ) { | 
|---|
|  | 735 | visit_children = false; | 
|---|
| [62423350] | 736 | // resolve initialization using the possibilities as determined by the currentObject cursor | 
|---|
| [0a22cda] | 737 | Expression * newExpr = new UntypedInitExpr( singleInit->value, currentObject.getOptions() ); | 
|---|
| [08da53d] | 738 | findSingleExpression( newExpr, indexer ); | 
|---|
| [e3e16bc] | 739 | InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); | 
|---|
| [62423350] | 740 |  | 
|---|
|  | 741 | // move cursor to the object that is actually initialized | 
|---|
| [e4d829b] | 742 | currentObject.setNext( initExpr->get_designation() ); | 
|---|
| [62423350] | 743 |  | 
|---|
|  | 744 | // discard InitExpr wrapper and retain relevant pieces | 
|---|
| [08da53d] | 745 | newExpr = initExpr->expr; | 
|---|
|  | 746 | initExpr->expr = nullptr; | 
|---|
|  | 747 | std::swap( initExpr->env, newExpr->env ); | 
|---|
| [cdb990a] | 748 | // InitExpr may have inferParams in the case where the expression specializes a function pointer, | 
|---|
|  | 749 | // and newExpr may already have inferParams of its own, so a simple swap is not sufficient. | 
|---|
|  | 750 | newExpr->spliceInferParams( initExpr ); | 
|---|
| [e4d829b] | 751 | delete initExpr; | 
|---|
|  | 752 |  | 
|---|
| [62423350] | 753 | // get the actual object's type (may not exactly match what comes back from the resolver due to conversions) | 
|---|
|  | 754 | Type * initContext = currentObject.getCurrentType(); | 
|---|
|  | 755 |  | 
|---|
| [0a22cda] | 756 | removeExtraneousCast( newExpr, indexer ); | 
|---|
|  | 757 |  | 
|---|
| [62423350] | 758 | // check if actual object's type is char[] | 
|---|
|  | 759 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
|  | 760 | if ( isCharType( at->get_base() ) ) { | 
|---|
|  | 761 | // check if the resolved type is char * | 
|---|
|  | 762 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { | 
|---|
|  | 763 | if ( isCharType( pt->get_base() ) ) { | 
|---|
| [0a22cda] | 764 | if ( CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ) ) { | 
|---|
|  | 765 | // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello"; | 
|---|
|  | 766 | newExpr = ce->get_arg(); | 
|---|
|  | 767 | ce->set_arg( nullptr ); | 
|---|
|  | 768 | std::swap( ce->env, newExpr->env ); | 
|---|
|  | 769 | delete ce; | 
|---|
|  | 770 | } | 
|---|
| [62423350] | 771 | } | 
|---|
|  | 772 | } | 
|---|
|  | 773 | } | 
|---|
|  | 774 | } | 
|---|
| [94b4364] | 775 |  | 
|---|
| [62423350] | 776 | // set initializer expr to resolved express | 
|---|
| [0a22cda] | 777 | singleInit->value = newExpr; | 
|---|
| [62423350] | 778 |  | 
|---|
|  | 779 | // move cursor to next object in preparation for next initializer | 
|---|
|  | 780 | currentObject.increment(); | 
|---|
|  | 781 | } | 
|---|
| [94b4364] | 782 |  | 
|---|
| [a4ca48c] | 783 | void Resolver::previsit( ListInit * listInit ) { | 
|---|
|  | 784 | visit_children = false; | 
|---|
| [62423350] | 785 | // move cursor into brace-enclosed initializer-list | 
|---|
| [e4d829b] | 786 | currentObject.enterListInit(); | 
|---|
|  | 787 | // xxx - fix this so that the list isn't copied, iterator should be used to change current element | 
|---|
|  | 788 | std::list<Designation *> newDesignations; | 
|---|
|  | 789 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { | 
|---|
| [62423350] | 790 | // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving | 
|---|
|  | 791 | // the initializer against that object. | 
|---|
| [e4d829b] | 792 | Designation * des = std::get<0>(p); | 
|---|
|  | 793 | Initializer * init = std::get<1>(p); | 
|---|
|  | 794 | newDesignations.push_back( currentObject.findNext( des ) ); | 
|---|
| [a4ca48c] | 795 | init->accept( *visitor ); | 
|---|
| [b5c5684] | 796 | } | 
|---|
| [62423350] | 797 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list | 
|---|
| [e4d829b] | 798 | listInit->get_designations() = newDesignations; // xxx - memory management | 
|---|
|  | 799 | currentObject.exitListInit(); | 
|---|
|  | 800 |  | 
|---|
| [62423350] | 801 | // xxx - this part has not be folded into CurrentObject yet | 
|---|
| [e4d829b] | 802 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { | 
|---|
|  | 803 | //      Type * base = tt->get_baseType()->get_base(); | 
|---|
|  | 804 | //      if ( base ) { | 
|---|
|  | 805 | //              // know the implementation type, so try using that as the initContext | 
|---|
|  | 806 | //              ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); | 
|---|
|  | 807 | //              currentObject = &tmpObj; | 
|---|
|  | 808 | //              visit( listInit ); | 
|---|
|  | 809 | //      } else { | 
|---|
|  | 810 | //              // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context | 
|---|
|  | 811 | //              Parent::visit( listInit ); | 
|---|
|  | 812 | //      } | 
|---|
|  | 813 | // } else { | 
|---|
| [a32b204] | 814 | } | 
|---|
| [71f4e4f] | 815 |  | 
|---|
| [f1e012b] | 816 | // ConstructorInit - fall back on C-style initializer | 
|---|
|  | 817 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { | 
|---|
|  | 818 | // could not find valid constructor, or found an intrinsic constructor | 
|---|
|  | 819 | // fall back on C-style initializer | 
|---|
|  | 820 | delete ctorInit->get_ctor(); | 
|---|
|  | 821 | ctorInit->set_ctor( NULL ); | 
|---|
| [71a145de] | 822 | delete ctorInit->get_dtor(); | 
|---|
|  | 823 | ctorInit->set_dtor( NULL ); | 
|---|
| [a4ca48c] | 824 | maybeAccept( ctorInit->get_init(), *visitor ); | 
|---|
| [f1e012b] | 825 | } | 
|---|
|  | 826 |  | 
|---|
| [1d2b64f] | 827 | // needs to be callable from outside the resolver, so this is a standalone function | 
|---|
|  | 828 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { | 
|---|
|  | 829 | assert( ctorInit ); | 
|---|
| [a4ca48c] | 830 | PassVisitor<Resolver> resolver( indexer ); | 
|---|
| [1d2b64f] | 831 | ctorInit->accept( resolver ); | 
|---|
|  | 832 | } | 
|---|
|  | 833 |  | 
|---|
|  | 834 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { | 
|---|
|  | 835 | assert( stmtExpr ); | 
|---|
| [a4ca48c] | 836 | PassVisitor<Resolver> resolver( indexer ); | 
|---|
| [1d2b64f] | 837 | stmtExpr->accept( resolver ); | 
|---|
| [5e2c348] | 838 | stmtExpr->computeResult(); | 
|---|
| [dd05e12] | 839 | // xxx - aggregate the environments from all statements? Possibly in AlternativeFinder instead? | 
|---|
| [1d2b64f] | 840 | } | 
|---|
|  | 841 |  | 
|---|
| [a4ca48c] | 842 | void Resolver::previsit( ConstructorInit *ctorInit ) { | 
|---|
|  | 843 | visit_children = false; | 
|---|
| [1ba88a0] | 844 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit | 
|---|
| [dd05e12] | 845 | maybeAccept( ctorInit->ctor, *visitor ); | 
|---|
|  | 846 | maybeAccept( ctorInit->dtor, *visitor ); | 
|---|
| [071a31a] | 847 |  | 
|---|
| [5b2f5bb] | 848 | // found a constructor - can get rid of C-style initializer | 
|---|
| [dd05e12] | 849 | delete ctorInit->init; | 
|---|
|  | 850 | ctorInit->init = nullptr; | 
|---|
| [ec79847] | 851 |  | 
|---|
|  | 852 | // intrinsic single parameter constructors and destructors do nothing. Since this was | 
|---|
|  | 853 | // implicitly generated, there's no way for it to have side effects, so get rid of it | 
|---|
|  | 854 | // to clean up generated code. | 
|---|
| [dd05e12] | 855 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { | 
|---|
|  | 856 | delete ctorInit->ctor; | 
|---|
|  | 857 | ctorInit->ctor = nullptr; | 
|---|
| [ec79847] | 858 | } | 
|---|
| [f9cebb5] | 859 |  | 
|---|
| [dd05e12] | 860 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { | 
|---|
|  | 861 | delete ctorInit->dtor; | 
|---|
|  | 862 | ctorInit->dtor = nullptr; | 
|---|
| [ec79847] | 863 | } | 
|---|
| [a465caff] | 864 |  | 
|---|
|  | 865 | // xxx - todo -- what about arrays? | 
|---|
|  | 866 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { | 
|---|
|  | 867 | //      // can reduce the constructor down to a SingleInit using the | 
|---|
|  | 868 | //      // second argument from the ctor call, since | 
|---|
|  | 869 | //      delete ctorInit->get_ctor(); | 
|---|
|  | 870 | //      ctorInit->set_ctor( NULL ); | 
|---|
|  | 871 |  | 
|---|
|  | 872 | //      Expression * arg = | 
|---|
|  | 873 | //      ctorInit->set_init( new SingleInit( arg ) ); | 
|---|
|  | 874 | // } | 
|---|
| [71f4e4f] | 875 | } | 
|---|
| [51b73452] | 876 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 877 |  | 
|---|
|  | 878 | // Local Variables: // | 
|---|
|  | 879 | // tab-width: 4 // | 
|---|
|  | 880 | // mode: c++ // | 
|---|
|  | 881 | // compile-command: "make install" // | 
|---|
|  | 882 | // End: // | 
|---|