- File:
-
- 1 edited
-
src/ControlStruct/ExceptTranslateNew.cpp (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/ExceptTranslateNew.cpp
r400b8be r5f3ba11 10 10 // Created On : Mon Nov 8 11:53:00 2021 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Mar 11 17:51:00 202213 // Update Count : 212 // Last Modified On : Mon Nov 8 16:50:00 2021 13 // Update Count : 0 14 14 // 15 15 … … 26 26 namespace { 27 27 28 typedef std::list<ast::CatchClause*> CatchList; 28 typedef std::list<ast::CatchStmt*> CatchList; 29 30 void split( CatchList& allHandlers, CatchList& terHandlers, 31 CatchList& resHandlers ) { 32 while ( !allHandlers.empty() ) { 33 ast::CatchStmt * stmt = allHandlers.front(); 34 allHandlers.pop_front(); 35 if (stmt->kind == ast::ExceptionKind::Terminate) { 36 terHandlers.push_back(stmt); 37 } else { 38 resHandlers.push_back(stmt); 39 } 40 } 41 } 29 42 30 43 void appendDeclStmt( ast::CompoundStmt * block, ast::DeclWithType * item ) { … … 45 58 {} 46 59 47 void previsit( const ast::Catch Clause* stmt );60 void previsit( const ast::CatchStmt * stmt ); 48 61 const ast::Stmt * postvisit( const ast::ThrowStmt * stmt ); 49 62 }; … … 88 101 } 89 102 90 void TranslateThrowsCore::previsit( const ast::Catch Clause* stmt ) {103 void TranslateThrowsCore::previsit( const ast::CatchStmt * stmt ) { 91 104 // Validate the statement's form. 92 105 const ast::ObjectDecl * decl = stmt->decl.as<ast::ObjectDecl>(); … … 147 160 ast::FunctionDecl * create_terminate_catch( CatchList &handlers ); 148 161 ast::CompoundStmt * create_single_matcher( 149 const ast::DeclWithType * except_obj, ast::Catch Clause* modded_handler );162 const ast::DeclWithType * except_obj, ast::CatchStmt * modded_handler ); 150 163 ast::FunctionDecl * create_terminate_match( CatchList &handlers ); 151 164 ast::CompoundStmt * create_terminate_caller( CodeLocation loc, ast::FunctionDecl * try_wrapper, … … 158 171 ast::Stmt * create_resume_rethrow( const ast::ThrowStmt * throwStmt ); 159 172 160 // Types used in translation, first group are internal. 161 ast::ObjectDecl * make_index_object( CodeLocation const & ) const; 162 ast::ObjectDecl * make_exception_object( CodeLocation const & ) const; 163 ast::ObjectDecl * make_bool_object( CodeLocation const & ) const; 164 ast::ObjectDecl * make_voidptr_object( CodeLocation const & ) const; 165 ast::ObjectDecl * make_unused_index_object( CodeLocation const & ) const; 173 // Types used in translation, make sure to use clone. 166 174 // void (*function)(); 167 ast::FunctionDecl * make_try_function( CodeLocation const & ) const;175 ast::FunctionDecl * try_func_t; 168 176 // void (*function)(int, exception); 169 ast::FunctionDecl * make_catch_function( CodeLocation const & ) const;177 ast::FunctionDecl * catch_func_t; 170 178 // int (*function)(exception); 171 ast::FunctionDecl * ma ke_match_function( CodeLocation const & ) const;179 ast::FunctionDecl * match_func_t; 172 180 // bool (*function)(exception); 173 ast::FunctionDecl * make_handle_function( CodeLocation const & ) const;181 ast::FunctionDecl * handle_func_t; 174 182 // void (*function)(__attribute__((unused)) void *); 175 ast::FunctionDecl * make_finally_function( CodeLocation const & ) const; 183 ast::FunctionDecl * finally_func_t; 184 185 ast::StructInstType * create_except_type() { 186 assert( except_decl ); 187 return new ast::StructInstType( except_decl ); 188 } 189 void init_func_types(); 176 190 177 191 public: … … 185 199 }; 186 200 187 ast::ObjectDecl * TryMutatorCore::make_index_object( 188 CodeLocation const & location ) const { 189 return new ast::ObjectDecl( 190 location, 201 void TryMutatorCore::init_func_types() { 202 assert( except_decl ); 203 204 ast::ObjectDecl index_obj( 205 {}, 191 206 "__handler_index", 192 new ast::BasicType(ast::BasicType::SignedInt), 193 nullptr, //init 194 ast::Storage::Classes{}, 195 ast::Linkage::Cforall 196 ); 197 } 198 199 ast::ObjectDecl * TryMutatorCore::make_exception_object( 200 CodeLocation const & location ) const { 201 assert( except_decl ); 202 return new ast::ObjectDecl( 203 location, 207 new ast::BasicType(ast::BasicType::SignedInt) 208 ); 209 ast::ObjectDecl exception_obj( 210 {}, 204 211 "__exception_inst", 205 212 new ast::PointerType( 206 213 new ast::StructInstType( except_decl ) 207 214 ), 208 nullptr, //init 209 ast::Storage::Classes{}, 210 ast::Linkage::Cforall 211 ); 212 } 213 214 ast::ObjectDecl * TryMutatorCore::make_bool_object( 215 CodeLocation const & location ) const { 216 return new ast::ObjectDecl( 217 location, 215 NULL 216 ); 217 ast::ObjectDecl bool_obj( 218 {}, 218 219 "__ret_bool", 219 220 new ast::BasicType( ast::BasicType::Bool ), … … 224 225 std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) } 225 226 ); 226 } 227 228 ast::ObjectDecl * TryMutatorCore::make_voidptr_object( 229 CodeLocation const & location ) const { 230 return new ast::ObjectDecl( 231 location, 227 ast::ObjectDecl voidptr_obj( 228 {}, 232 229 "__hook", 233 230 new ast::PointerType( … … 240 237 std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) } 241 238 ); 242 } 243 244 ast::ObjectDecl * TryMutatorCore::make_unused_index_object( 245 CodeLocation const & location ) const { 246 return new ast::ObjectDecl( 247 location, 239 240 ast::ObjectDecl unused_index_obj( 241 {}, 248 242 "__handler_index", 249 243 new ast::BasicType(ast::BasicType::SignedInt), … … 254 248 std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) } 255 249 ); 256 } 257 258 ast::FunctionDecl * TryMutatorCore::make_try_function( 259 CodeLocation const & location ) const { 260 return new ast::FunctionDecl( 261 location, 250 //unused_index_obj->attributes.push_back( new Attribute( "unused" ) ); 251 252 try_func_t = new ast::FunctionDecl( 253 {}, 262 254 "try", 263 255 {}, //forall … … 268 260 ast::Linkage::Cforall 269 261 ); 270 } 271 272 ast::FunctionDecl * TryMutatorCore::make_catch_function( 273 CodeLocation const & location ) const { 274 return new ast::FunctionDecl( 275 location, 262 263 catch_func_t = new ast::FunctionDecl( 264 {}, 276 265 "catch", 277 266 {}, //forall 278 { make_index_object( location ), make_exception_object( location ) },267 {ast::deepCopy(&index_obj), ast::deepCopy(&exception_obj)},//param 279 268 {}, //return void 280 269 nullptr, … … 282 271 ast::Linkage::Cforall 283 272 ); 284 } 285 286 ast::FunctionDecl * TryMutatorCore::make_match_function( 287 CodeLocation const & location ) const { 288 return new ast::FunctionDecl( 289 location, 273 274 match_func_t = new ast::FunctionDecl( 275 {}, 290 276 "match", 291 277 {}, //forall 292 { make_exception_object( location )},293 { make_unused_index_object( location )},278 {ast::deepCopy(&exception_obj)}, 279 {ast::deepCopy(&unused_index_obj)}, 294 280 nullptr, 295 281 ast::Storage::Classes{}, 296 282 ast::Linkage::Cforall 297 283 ); 298 } 299 300 ast::FunctionDecl * TryMutatorCore::make_handle_function( 301 CodeLocation const & location ) const { 302 return new ast::FunctionDecl( 303 location, 284 285 handle_func_t = new ast::FunctionDecl( 286 {}, 304 287 "handle", 305 288 {}, //forall 306 { make_exception_object( location )},307 { make_bool_object( location )},289 {ast::deepCopy(&exception_obj)}, 290 {ast::deepCopy(&bool_obj)}, 308 291 nullptr, 309 292 ast::Storage::Classes{}, 310 293 ast::Linkage::Cforall 311 294 ); 312 } 313 314 ast::FunctionDecl * TryMutatorCore::make_finally_function( 315 CodeLocation const & location ) const { 316 return new ast::FunctionDecl( 317 location, 295 296 finally_func_t = new ast::FunctionDecl( 297 {}, 318 298 "finally", 319 299 {}, //forall 320 { make_voidptr_object( location )},300 {ast::deepCopy(&voidptr_obj)}, 321 301 {}, //return void 322 302 nullptr, … … 324 304 ast::Linkage::Cforall 325 305 ); 306 307 //catch_func_t.get_parameters().push_back( index_obj.clone() ); 308 //catch_func_t.get_parameters().push_back( exception_obj.clone() ); 309 //match_func_t.get_returnVals().push_back( unused_index_obj ); 310 //match_func_t.get_parameters().push_back( exception_obj.clone() ); 311 //handle_func_t.get_returnVals().push_back( bool_obj.clone() ); 312 //handle_func_t.get_parameters().push_back( exception_obj.clone() ); 313 //finally_func_t.get_parameters().push_back( voidptr_obj.clone() ); 326 314 } 327 315 328 316 // TryStmt Mutation Helpers 317 318 /* 319 ast::CompoundStmt * TryMutatorCore::take_try_block( ast::TryStmt *tryStmt ) { 320 ast::CompoundStmt * block = tryStmt->body; 321 tryStmt->body = nullptr; 322 return block; 323 } 324 */ 329 325 330 326 ast::FunctionDecl * TryMutatorCore::create_try_wrapper( 331 327 const ast::CompoundStmt *body ) { 332 328 333 ast::FunctionDecl * ret = make_try_function( body->location);329 ast::FunctionDecl * ret = ast::deepCopy(try_func_t); 334 330 ret->stmts = body; 335 331 return ret; … … 338 334 ast::FunctionDecl * TryMutatorCore::create_terminate_catch( 339 335 CatchList &handlers ) { 340 std::vector<ast::ptr<ast:: CaseClause>> handler_wrappers;336 std::vector<ast::ptr<ast::Stmt>> handler_wrappers; 341 337 342 338 assert (!handlers.empty()); 343 339 const CodeLocation loc = handlers.front()->location; 344 340 345 ast::FunctionDecl * func_t = make_catch_function( loc);341 ast::FunctionDecl * func_t = ast::deepCopy(catch_func_t); 346 342 const ast::DeclWithType * index_obj = func_t->params.front(); 347 343 const ast::DeclWithType * except_obj = func_t->params.back(); … … 352 348 for ( ; it != handlers.end() ; ++it ) { 353 349 ++index; 354 ast::Catch Clause* handler = *it;350 ast::CatchStmt * handler = *it; 355 351 const CodeLocation loc = handler->location; 356 352 … … 390 386 // handler->body = nullptr; 391 387 392 handler_wrappers.push_back( new ast::Case Clause(loc,388 handler_wrappers.push_back( new ast::CaseStmt(loc, 393 389 ast::ConstantExpr::from_int(loc, index) , 394 390 { block, new ast::ReturnStmt( loc, nullptr ) } … … 397 393 // TODO: Some sort of meaningful error on default perhaps? 398 394 399 ast::SwitchStmt * handler_lookup = new ast::SwitchStmt( loc, 395 /* 396 std::list<Statement*> stmt_handlers; 397 while ( !handler_wrappers.empty() ) { 398 stmt_handlers.push_back( handler_wrappers.front() ); 399 handler_wrappers.pop_front(); 400 } 401 */ 402 403 ast::SwitchStmt * handler_lookup = new ast::SwitchStmt(loc, 400 404 new ast::VariableExpr( loc, index_obj ), 401 405 std::move(handler_wrappers) 402 406 ); 403 ast::CompoundStmt * body = new ast::CompoundStmt( loc, {handler_lookup} ); 407 ast::CompoundStmt * body = new ast::CompoundStmt(loc, 408 {handler_lookup}); 404 409 405 410 func_t->stmts = body; … … 410 415 // except_obj is referenced, modded_handler will be freed. 411 416 ast::CompoundStmt * TryMutatorCore::create_single_matcher( 412 const ast::DeclWithType * except_obj, ast::Catch Clause* modded_handler ) {417 const ast::DeclWithType * except_obj, ast::CatchStmt * modded_handler ) { 413 418 // { 414 419 // `modded_handler.decl` … … 428 433 429 434 // Check for type match. 430 ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc, 435 ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc, 431 436 new ast::VariableExpr(loc, except_obj ), 432 437 local_except->get_type() … … 440 445 } 441 446 // Construct the match condition. 442 block->push_back( new ast::IfStmt(loc, 447 block->push_back( new ast::IfStmt(loc, 443 448 cond, modded_handler->body, nullptr ) ); 444 449 450 // xxx - how does this work in new ast 451 //modded_handler->set_decl( nullptr ); 452 //modded_handler->set_cond( nullptr ); 453 //modded_handler->set_body( nullptr ); 454 //delete modded_handler; 445 455 return block; 446 456 } … … 457 467 ast::CompoundStmt * body = new ast::CompoundStmt(loc); 458 468 459 ast::FunctionDecl * func_t = make_match_function( loc);469 ast::FunctionDecl * func_t = ast::deepCopy(match_func_t); 460 470 const ast::DeclWithType * except_obj = func_t->params.back(); 461 471 … … 465 475 for ( it = handlers.begin() ; it != handlers.end() ; ++it ) { 466 476 ++index; 467 ast::Catch Clause* handler = *it;477 ast::CatchStmt * handler = *it; 468 478 469 479 // Body should have been taken by create_terminate_catch. … … 480 490 } 481 491 482 body->push_back( new ast::ReturnStmt(loc, 492 body->push_back( new ast::ReturnStmt(loc, 483 493 ast::ConstantExpr::from_int( loc, 0 ) )); 484 494 … … 515 525 ast::CompoundStmt * body = new ast::CompoundStmt(loc); 516 526 517 ast::FunctionDecl * func_t = make_handle_function( loc);527 ast::FunctionDecl * func_t = ast::deepCopy(handle_func_t); 518 528 const ast::DeclWithType * except_obj = func_t->params.back(); 519 529 520 530 CatchList::iterator it; 521 531 for ( it = handlers.begin() ; it != handlers.end() ; ++it ) { 522 ast::Catch Clause* handler = *it;532 ast::CatchStmt * handler = *it; 523 533 const CodeLocation loc = handler->location; 524 534 // Modifiy body. 525 535 ast::CompoundStmt * handling_code; 526 536 if (handler->body.as<ast::CompoundStmt>()) { 527 handling_code = strict_dynamic_cast<ast::CompoundStmt*>(528 handler->body.get_and_mutate() );537 handling_code = 538 strict_dynamic_cast<ast::CompoundStmt*>( handler->body.get_and_mutate() ); 529 539 } else { 530 540 handling_code = new ast::CompoundStmt(loc); … … 587 597 ast::TryStmt * tryStmt ) { 588 598 // void finally() { `finally->block` } 589 const ast::Finally Clause* finally = tryStmt->finally;599 const ast::FinallyStmt * finally = tryStmt->finally; 590 600 const ast::CompoundStmt * body = finally->body; 591 601 592 ast::FunctionDecl * func_t = make_finally_function( tryStmt->location);602 ast::FunctionDecl * func_t = ast::deepCopy(finally_func_t); 593 603 func_t->stmts = body; 594 604 605 // finally->set_block( nullptr ); 606 // delete finally; 595 607 tryStmt->finally = nullptr; 608 596 609 597 610 return func_t; … … 604 617 605 618 const CodeLocation loc = finally_wrapper->location; 619 // Make Cleanup Attribute. 620 /* 621 std::list< ast::Attribute * > attributes; 622 { 623 std::list< > attr_params; 624 attr_params.push_back( nameOf( finally_wrapper ) ); 625 attributes.push_back( new Attribute( "cleanup", attr_params ) ); 626 } 627 */ 628 606 629 return new ast::ObjectDecl( 607 630 loc, … … 621 644 // return false; 622 645 const CodeLocation loc = throwStmt->location; 623 ast::Stmt * result = new ast::ReturnStmt(loc, 646 ast::Stmt * result = new ast::ReturnStmt(loc, 624 647 ast::ConstantExpr::from_bool( loc, false ) 625 648 ); 626 649 result->labels = throwStmt->labels; 650 // delete throwStmt; done by postvisit 627 651 return result; 628 652 } … … 636 660 assert( nullptr == except_decl ); 637 661 except_decl = structDecl; 662 init_func_types(); 638 663 } else if ( structDecl->name == "__cfaehm_try_resume_node" ) { 639 664 assert( nullptr == node_decl ); … … 681 706 } 682 707 } 708 // split( mutStmt->handlers, 709 // termination_handlers, resumption_handlers ); 683 710 684 711 if ( resumption_handlers.size() ) {
Note:
See TracChangeset
for help on using the changeset viewer.