Changes in src/ResolvExpr/Resolver.cc [08da53d:3c398b6]
- File:
-
- 1 edited
-
src/ResolvExpr/Resolver.cc (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Resolver.cc
r08da53d r3c398b6 109 109 110 110 namespace { 111 void finishExpr( Expression *expr, const TypeEnvironment &env , TypeSubstitution * oldenv = nullptr) {112 expr-> env = oldenv ? oldenv->clone() : new TypeSubstitution;111 void finishExpr( Expression *expr, const TypeEnvironment &env ) { 112 expr->set_env( new TypeSubstitution ); 113 113 env.makeSubstitution( *expr->get_env() ); 114 114 } 115 115 } // namespace 116 116 117 void findVoidExpression( Expression *&untyped, const SymTab::Indexer &indexer ) {117 Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { 118 118 global_renamer.reset(); 119 119 TypeEnvironment env; 120 120 Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); 121 finishExpr( newExpr, env, untyped->env ); 122 delete untyped; 123 untyped = newExpr; 124 } 125 126 void findSingleExpression( Expression *&untyped, const SymTab::Indexer &indexer ) { 127 if ( ! untyped ) return; 121 finishExpr( newExpr, env ); 122 return newExpr; 123 } 124 125 Expression * findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { 128 126 TypeEnvironment env; 129 127 AlternativeFinder finder( indexer, env ); … … 131 129 #if 0 132 130 if ( finder.get_alternatives().size() != 1 ) { 133 std::c err<< "untyped expr is ";134 untyped->print( std::c err);135 std::c err<< std::endl << "alternatives are:";136 for ( const Alternative & alt : finder.get_alternatives()) {137 alt.print( std::cerr);131 std::cout << "untyped expr is "; 132 untyped->print( std::cout ); 133 std::cout << std::endl << "alternatives are:"; 134 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { 135 i->print( std::cout ); 138 136 } // for 139 137 } // if … … 142 140 Alternative &choice = finder.get_alternatives().front(); 143 141 Expression *newExpr = choice.expr->clone(); 144 finishExpr( newExpr, choice.env, untyped->env ); 145 delete untyped; 146 untyped = newExpr; 147 } 148 149 void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) { 150 assert( untyped && type ); 151 untyped = new CastExpr( untyped, type ); 152 findSingleExpression( untyped, indexer ); 153 if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( untyped ) ) { 154 if ( ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) { 155 // cast is to the same type as its argument, so it's unnecessary -- remove it 156 untyped = castExpr->arg; 157 castExpr->arg = nullptr; 158 delete castExpr; 159 } 160 } 142 finishExpr( newExpr, choice.env ); 143 return newExpr; 161 144 } 162 145 … … 174 157 } 175 158 176 void findIntegralExpression( Expression *&untyped, const SymTab::Indexer &indexer ) {159 Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { 177 160 TypeEnvironment env; 178 161 AlternativeFinder finder( indexer, env ); … … 203 186 throw SemanticError( "No interpretations for case control expression", untyped ); 204 187 } // if 205 finishExpr( newExpr, *newEnv, untyped->env ); 206 delete untyped; 207 untyped = newExpr; 188 finishExpr( newExpr, *newEnv ); 189 return newExpr; 208 190 } 209 191 … … 230 212 void Resolver::handlePtrType( PtrType * type ) { 231 213 if ( type->get_dimension() ) { 232 findSingleExpression( type->dimension, SymTab::SizeType->clone(), indexer ); 214 CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); 215 Expression *newExpr = findSingleExpression( castExpr, indexer ); 216 delete type->get_dimension(); 217 type->set_dimension( newExpr ); 233 218 } 234 219 } … … 283 268 void Resolver::previsit( ExprStmt *exprStmt ) { 284 269 visit_children = false; 285 assertf( exprStmt->expr, "ExprStmt has null Expression in resolver" ); 286 findVoidExpression( exprStmt->expr, indexer ); 270 assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" ); 271 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), indexer ); 272 delete exprStmt->get_expr(); 273 exprStmt->set_expr( newExpr ); 287 274 } 288 275 289 276 void Resolver::previsit( AsmExpr *asmExpr ) { 290 277 visit_children = false; 291 findVoidExpression( asmExpr->operand, indexer ); 278 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), indexer ); 279 delete asmExpr->get_operand(); 280 asmExpr->set_operand( newExpr ); 292 281 if ( asmExpr->get_inout() ) { 293 findVoidExpression( asmExpr->inout, indexer ); 282 newExpr = findVoidExpression( asmExpr->get_inout(), indexer ); 283 delete asmExpr->get_inout(); 284 asmExpr->set_inout( newExpr ); 294 285 } // if 295 286 } … … 302 293 303 294 void Resolver::previsit( IfStmt *ifStmt ) { 304 findSingleExpression( ifStmt->condition, indexer ); 295 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), indexer ); 296 delete ifStmt->get_condition(); 297 ifStmt->set_condition( newExpr ); 305 298 } 306 299 307 300 void Resolver::previsit( WhileStmt *whileStmt ) { 308 findSingleExpression( whileStmt->condition, indexer ); 301 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), indexer ); 302 delete whileStmt->get_condition(); 303 whileStmt->set_condition( newExpr ); 309 304 } 310 305 311 306 void Resolver::previsit( ForStmt *forStmt ) { 312 if ( forStmt->condition ) { 313 findSingleExpression( forStmt->condition, indexer ); 307 if ( forStmt->get_condition() ) { 308 Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer ); 309 delete forStmt->get_condition(); 310 forStmt->set_condition( newExpr ); 314 311 } // if 315 312 316 if ( forStmt->increment ) { 317 findVoidExpression( forStmt->increment, indexer ); 313 if ( forStmt->get_increment() ) { 314 Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer ); 315 delete forStmt->get_increment(); 316 forStmt->set_increment( newExpr ); 318 317 } // if 319 318 } … … 321 320 void Resolver::previsit( SwitchStmt *switchStmt ) { 322 321 GuardValue( currentObject ); 323 findIntegralExpression( switchStmt->condition, indexer ); 324 325 currentObject = CurrentObject( switchStmt->condition->result ); 322 Expression *newExpr; 323 newExpr = findIntegralExpression( switchStmt->get_condition(), indexer ); 324 delete switchStmt->get_condition(); 325 switchStmt->set_condition( newExpr ); 326 327 currentObject = CurrentObject( newExpr->get_result() ); 326 328 } 327 329 … … 330 332 std::list< InitAlternative > initAlts = currentObject.getOptions(); 331 333 assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); 332 // must remove cast from case statement because RangeExpr cannot be cast. 333 Expression * newExpr = new CastExpr( caseStmt->condition, initAlts.front().type->clone() ); 334 findSingleExpression( newExpr, indexer ); 335 CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( newExpr ); 336 caseStmt->condition = castExpr->arg; 337 castExpr->arg = nullptr; 334 CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() ); 335 Expression * newExpr = findSingleExpression( castExpr, indexer ); 336 castExpr = strict_dynamic_cast< CastExpr * >( newExpr ); 337 caseStmt->set_condition( castExpr->get_arg() ); 338 castExpr->set_arg( nullptr ); 338 339 delete castExpr; 339 340 } … … 344 345 // must resolve the argument for a computed goto 345 346 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement 346 if ( branchStmt->computedTarget ) { 347 // computed goto argument is void * 348 findSingleExpression( branchStmt->computedTarget, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), indexer ); 347 if ( Expression * arg = branchStmt->get_computedTarget() ) { 348 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder 349 PointerType pt( Type::Qualifiers(), v.clone() ); 350 CastExpr * castExpr = new CastExpr( arg, pt.clone() ); 351 Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression 352 branchStmt->set_target( newExpr ); 349 353 } // if 350 354 } // if … … 353 357 void Resolver::previsit( ReturnStmt *returnStmt ) { 354 358 visit_children = false; 355 if ( returnStmt->expr ) { 356 findSingleExpression( returnStmt->expr, functionReturn->clone(), indexer ); 359 if ( returnStmt->get_expr() ) { 360 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); 361 Expression *newExpr = findSingleExpression( castExpr, indexer ); 362 delete castExpr; 363 returnStmt->set_expr( newExpr ); 357 364 } // if 358 365 } … … 365 372 indexer.lookupStruct( "__cfaehm__base_exception_t" ); 366 373 assert( exception_decl ); 367 Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, exception_decl ) ); 368 findSingleExpression( throwStmt->expr, exceptType, indexer ); 374 Expression * wrapped = new CastExpr( 375 throwStmt->get_expr(), 376 new PointerType( 377 noQualifiers, 378 new StructInstType( 379 noQualifiers, 380 exception_decl 381 ) 382 ) 383 ); 384 Expression * newExpr = findSingleExpression( wrapped, indexer ); 385 throwStmt->set_expr( newExpr ); 369 386 } 370 387 } 371 388 372 389 void Resolver::previsit( CatchStmt *catchStmt ) { 373 if ( catchStmt->cond ) { 374 findSingleExpression( catchStmt->cond, new BasicType( noQualifiers, BasicType::Bool ), indexer ); 375 } 390 if ( catchStmt->get_cond() ) { 391 Expression * wrapped = new CastExpr( 392 catchStmt->get_cond(), 393 new BasicType( noQualifiers, BasicType::Bool ) 394 ); 395 catchStmt->set_cond( findSingleExpression( wrapped, indexer ) ); 396 } 397 } 398 399 inline void resolveAsIf( Expression *& expr, SymTab::Indexer & indexer ) { 400 if( !expr ) return; 401 Expression * newExpr = findSingleExpression( expr, indexer ); 402 delete expr; 403 expr = newExpr; 404 } 405 406 inline void resolveAsType( Expression *& expr, Type * type, SymTab::Indexer & indexer ) { 407 if( !expr ) return; 408 Expression * newExpr = findSingleExpression( new CastExpr( expr, type ), indexer ); 409 delete expr; 410 expr = newExpr; 376 411 } 377 412 … … 543 578 // Resolve the conditions as if it were an IfStmt 544 579 // Resolve the statments normally 545 findSingleExpression( clause.condition, this->indexer );580 resolveAsIf( clause.condition, this->indexer ); 546 581 clause.statement->accept( *visitor ); 547 582 } … … 552 587 // Resolve the conditions as if it were an IfStmt 553 588 // Resolve the statments normally 554 findSingleExpression( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer );555 findSingleExpression( stmt->timeout.condition, this->indexer );589 resolveAsType( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer ); 590 resolveAsIf ( stmt->timeout.condition, this->indexer ); 556 591 stmt->timeout.statement->accept( *visitor ); 557 592 } … … 560 595 // Resolve the conditions as if it were an IfStmt 561 596 // Resolve the statments normally 562 findSingleExpression( stmt->orelse.condition, this->indexer );597 resolveAsIf( stmt->orelse.condition, this->indexer ); 563 598 stmt->orelse.statement->accept( *visitor ); 564 599 } … … 577 612 visit_children = false; 578 613 // resolve initialization using the possibilities as determined by the currentObject cursor 579 Expression * newExpr= new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );580 findSingleExpression( newExpr, indexer );614 UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() ); 615 Expression * newExpr = findSingleExpression( untyped, indexer ); 581 616 InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); 582 617 … … 585 620 586 621 // discard InitExpr wrapper and retain relevant pieces 587 newExpr = initExpr->expr; 588 initExpr->expr = nullptr; 589 std::swap( initExpr->env, newExpr->env ); 622 newExpr = initExpr->get_expr(); 623 newExpr->set_env( initExpr->get_env() ); 624 initExpr->set_expr( nullptr ); 625 initExpr->set_env( nullptr ); 590 626 delete initExpr; 591 627
Note:
See TracChangeset
for help on using the changeset viewer.