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