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