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