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