source: src/ControlStruct/ExceptTranslate.cpp@ 83fd57d

Last change on this file since 83fd57d was 83fd57d, checked in by Andrew Beach <ajbeach@…>, 22 months ago

Removed 'New' suffixes, they are no longer needed for disambiguation.

  • Property mode set to 100644
File size: 22.0 KB
Line 
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// ExceptTranslate.cpp -- Conversion of exception control flow structures.
8//
9// Author : Andrew Beach
10// Created On : Mon Nov 8 11:53:00 2021
11// Last Modified By : Andrew Beach
12// Last Modified On : Fri Mar 11 17:51:00 2022
13// Update Count : 2
14//
15
16#include "ExceptTranslate.h"
17
18#include "AST/Expr.hpp"
19#include "AST/Pass.hpp"
20#include "AST/Stmt.hpp"
21#include "AST/TranslationUnit.hpp"
22#include "AST/DeclReplacer.hpp"
23
24namespace ControlStruct {
25
26namespace {
27
28 typedef std::list<ast::CatchClause*> CatchList;
29
30 void appendDeclStmt( ast::CompoundStmt * block, ast::DeclWithType * item ) {
31 block->push_back(new ast::DeclStmt(block->location, item));
32 }
33
34class TranslateThrowsCore final : public ast::WithGuards {
35 const ast::ObjectDecl * terminateHandlerExcept;
36 enum Context { NoHandler, TerHandler, ResHandler } currentContext;
37
38 const ast::Stmt * createEitherThrow(
39 const ast::ThrowStmt * throwStmt, const char * funcName );
40 const ast::Stmt * createTerminateRethrow( const ast::ThrowStmt * );
41
42public:
43 TranslateThrowsCore() :
44 terminateHandlerExcept( nullptr ), currentContext( NoHandler )
45 {}
46
47 void previsit( const ast::CatchClause * stmt );
48 const ast::Stmt * postvisit( const ast::ThrowStmt * stmt );
49};
50
51const ast::Stmt * TranslateThrowsCore::createEitherThrow(
52 const ast::ThrowStmt * throwStmt, const char * funcName ) {
53 // `throwFunc`( `throwStmt->name` );
54 ast::UntypedExpr * call = new ast::UntypedExpr( throwStmt->location,
55 new ast::NameExpr( throwStmt->location, funcName )
56 );
57 call->args.push_back( throwStmt->expr );
58 return new ast::ExprStmt( throwStmt->location, call );
59}
60
61ast::VariableExpr * varOf( const ast::DeclWithType * decl ) {
62 return new ast::VariableExpr( decl->location, decl );
63}
64
65const ast::Stmt * TranslateThrowsCore::createTerminateRethrow(
66 const ast::ThrowStmt * stmt ) {
67 // { `terminate_handler_except` = 0p; __rethrow_terminate(); }
68 assert( nullptr == stmt->expr );
69 assert( terminateHandlerExcept );
70
71 ast::CompoundStmt * result = new ast::CompoundStmt(
72 stmt->location, {}, std::vector<ast::Label>( stmt->labels ) );
73 result->push_back( new ast::ExprStmt( stmt->location,
74 ast::UntypedExpr::createAssign(
75 stmt->location,
76 varOf( terminateHandlerExcept ),
77 ast::ConstantExpr::null(
78 stmt->location,
79 terminateHandlerExcept->type
80 )
81 )
82 ) );
83 result->push_back( new ast::ExprStmt( stmt->location, new ast::UntypedExpr(
84 stmt->location,
85 new ast::NameExpr( stmt->location, "__cfaehm_rethrow_terminate" )
86 ) ) );
87 return result;
88}
89
90void TranslateThrowsCore::previsit( const ast::CatchClause * stmt ) {
91 // Validate the statement's form.
92 const ast::ObjectDecl * decl = stmt->decl.as<ast::ObjectDecl>();
93 // Also checking the type would be nice.
94 if ( !decl || !decl->type.as<ast::PointerType>() ) {
95 std::string kind = (ast::Terminate == stmt->kind) ? "catch" : "catchResume";
96 SemanticError( stmt->location, kind + " must have pointer to an exception type" );
97 }
98
99 // Track the handler context.
100 if ( ast::Terminate == stmt->kind ) {
101 GuardValue( currentContext ) = TerHandler;
102 GuardValue( terminateHandlerExcept ) = decl;
103 } else {
104 GuardValue( currentContext ) = ResHandler;
105 }
106}
107
108const ast::Stmt * TranslateThrowsCore::postvisit(
109 const ast::ThrowStmt * stmt ) {
110 // Ignoring ThrowStmt::target for now.
111 // Handle Termination (Raise, Reraise, Error):
112 if ( ast::Terminate == stmt->kind ) {
113 if ( stmt->expr ) {
114 return createEitherThrow( stmt, "$throw" );
115 } else if ( TerHandler == currentContext ) {
116 return createTerminateRethrow( stmt );
117 } else {
118 abort( "Invalid throw in %s at %i\n",
119 stmt->location.filename.c_str(),
120 stmt->location.first_line);
121 }
122 // Handle Resumption (Raise, Reraise, Error):
123 } else {
124 if ( stmt->expr ) {
125 return createEitherThrow( stmt, "$throwResume" );
126 } else if ( ResHandler == currentContext ) {
127 // This has to be handled later.
128 return stmt;
129 } else {
130 abort( "Invalid throwResume in %s at %i\n",
131 stmt->location.filename.c_str(),
132 stmt->location.first_line);
133 }
134 }
135}
136
137
138class TryMutatorCore final {
139 // The built in types used in translation.
140 const ast::StructDecl * except_decl;
141 const ast::StructDecl * node_decl;
142 const ast::StructDecl * hook_decl;
143
144 // The many helper functions for code/syntree generation.
145 ast::CompoundStmt * take_try_block( ast::TryStmt * tryStmt );
146 ast::FunctionDecl * create_try_wrapper( const ast::CompoundStmt * body );
147 ast::FunctionDecl * create_terminate_catch( CatchList &handlers );
148 ast::CompoundStmt * create_single_matcher(
149 const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler );
150 ast::FunctionDecl * create_terminate_match( CatchList &handlers );
151 ast::CompoundStmt * create_terminate_caller( CodeLocation loc, ast::FunctionDecl * try_wrapper,
152 ast::FunctionDecl * terminate_catch, ast::FunctionDecl * terminate_match );
153 ast::FunctionDecl * create_resume_handler( CatchList &handlers );
154 ast::CompoundStmt * create_resume_wrapper(
155 const ast::Stmt * wraps, const ast::FunctionDecl * resume_handler );
156 ast::FunctionDecl * create_finally_wrapper( ast::TryStmt * tryStmt );
157 ast::ObjectDecl * create_finally_hook( ast::FunctionDecl * finally_wrapper );
158 ast::Stmt * create_resume_rethrow( const ast::ThrowStmt * throwStmt );
159
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;
166 // void (*function)();
167 ast::FunctionDecl * make_try_function( CodeLocation const & ) const;
168 // void (*function)(int, exception);
169 ast::FunctionDecl * make_catch_function( CodeLocation const & ) const;
170 // int (*function)(exception);
171 ast::FunctionDecl * make_match_function( CodeLocation const & ) const;
172 // bool (*function)(exception);
173 ast::FunctionDecl * make_handle_function( CodeLocation const & ) const;
174 // void (*function)(__attribute__((unused)) void *);
175 ast::FunctionDecl * make_finally_function( CodeLocation const & ) const;
176
177public:
178 TryMutatorCore() :
179 except_decl( nullptr ), node_decl( nullptr ), hook_decl( nullptr )
180 {}
181
182 void previsit( const ast::StructDecl *structDecl );
183 ast::Stmt * postvisit( const ast::TryStmt *tryStmt );
184 ast::Stmt * postvisit( const ast::ThrowStmt *throwStmt );
185};
186
187ast::ObjectDecl * TryMutatorCore::make_index_object(
188 CodeLocation const & location ) const {
189 return new ast::ObjectDecl(
190 location,
191 "__handler_index",
192 new ast::BasicType( ast::BasicType::SignedInt )
193 );
194}
195
196ast::ObjectDecl * TryMutatorCore::make_exception_object(
197 CodeLocation const & location ) const {
198 assert( except_decl );
199 return new ast::ObjectDecl(
200 location,
201 "__exception_inst",
202 new ast::PointerType( new ast::StructInstType( except_decl ) )
203 );
204}
205
206ast::ObjectDecl * TryMutatorCore::make_bool_object(
207 CodeLocation const & location ) const {
208 return new ast::ObjectDecl(
209 location,
210 "__ret_bool",
211 new ast::BasicType( ast::BasicType::Bool ),
212 nullptr, //init
213 ast::Storage::Classes{},
214 ast::Linkage::Cforall,
215 nullptr, //width
216 std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
217 );
218}
219
220ast::ObjectDecl * TryMutatorCore::make_voidptr_object(
221 CodeLocation const & location ) const {
222 return new ast::ObjectDecl(
223 location,
224 "__hook",
225 new ast::PointerType(
226 new ast::VoidType()
227 ),
228 nullptr, //init
229 ast::Storage::Classes{},
230 ast::Linkage::Cforall,
231 nullptr, //width
232 std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
233 );
234}
235
236ast::ObjectDecl * TryMutatorCore::make_unused_index_object(
237 CodeLocation const & location ) const {
238 return new ast::ObjectDecl(
239 location,
240 "__handler_index",
241 new ast::BasicType(ast::BasicType::SignedInt),
242 nullptr,
243 ast::Storage::Classes{},
244 ast::Linkage::Cforall,
245 nullptr, //width
246 std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
247 );
248}
249
250ast::FunctionDecl * TryMutatorCore::make_try_function(
251 CodeLocation const & location ) const {
252 return new ast::FunctionDecl(
253 location,
254 "try",
255 {}, //no param
256 {}, //no return
257 nullptr,
258 ast::Storage::Classes{},
259 ast::Linkage::Cforall
260 );
261}
262
263ast::FunctionDecl * TryMutatorCore::make_catch_function(
264 CodeLocation const & location ) const {
265 return new ast::FunctionDecl(
266 location,
267 "catch",
268 { make_index_object( location ), make_exception_object( location ) },
269 {}, //return void
270 nullptr,
271 ast::Storage::Classes{},
272 ast::Linkage::Cforall
273 );
274}
275
276ast::FunctionDecl * TryMutatorCore::make_match_function(
277 CodeLocation const & location ) const {
278 return new ast::FunctionDecl(
279 location,
280 "match",
281 { make_exception_object( location ) },
282 { make_unused_index_object( location ) },
283 nullptr,
284 ast::Storage::Classes{},
285 ast::Linkage::Cforall
286 );
287}
288
289ast::FunctionDecl * TryMutatorCore::make_handle_function(
290 CodeLocation const & location ) const {
291 return new ast::FunctionDecl(
292 location,
293 "handle",
294 { make_exception_object( location ) },
295 { make_bool_object( location ) },
296 nullptr,
297 ast::Storage::Classes{},
298 ast::Linkage::Cforall
299 );
300}
301
302ast::FunctionDecl * TryMutatorCore::make_finally_function(
303 CodeLocation const & location ) const {
304 return new ast::FunctionDecl(
305 location,
306 "finally",
307 { make_voidptr_object( location ) },
308 {}, //return void
309 nullptr,
310 ast::Storage::Classes{},
311 ast::Linkage::Cforall,
312 {},
313 { ast::Function::Inline }
314 );
315}
316
317// TryStmt Mutation Helpers
318
319ast::FunctionDecl * TryMutatorCore::create_try_wrapper(
320 const ast::CompoundStmt *body ) {
321
322 ast::FunctionDecl * ret = make_try_function( body->location );
323 ret->stmts = body;
324 return ret;
325}
326
327ast::FunctionDecl * TryMutatorCore::create_terminate_catch(
328 CatchList &handlers ) {
329 std::vector<ast::ptr<ast::CaseClause>> handler_wrappers;
330
331 assert (!handlers.empty());
332 const CodeLocation loc = handlers.front()->location;
333
334 ast::FunctionDecl * func_t = make_catch_function( loc );
335 const ast::DeclWithType * index_obj = func_t->params.front();
336 const ast::DeclWithType * except_obj = func_t->params.back();
337
338 // Index 1..{number of handlers}
339 int index = 0;
340 CatchList::iterator it = handlers.begin();
341 for ( ; it != handlers.end() ; ++it ) {
342 ++index;
343 ast::CatchClause * handler = *it;
344 const CodeLocation loc = handler->location;
345
346 // case `index`:
347 // {
348 // `handler.decl` = { (virtual `decl.type`)`except` };
349 // `handler.body`;
350 // }
351 // return;
352 ast::CompoundStmt * block = new ast::CompoundStmt(loc);
353
354 // Just copy the exception value. (Post Validation)
355 const ast::ObjectDecl * handler_decl =
356 handler->decl.strict_as<ast::ObjectDecl>();
357 ast::ObjectDecl * local_except = ast::deepCopy(handler_decl);
358 ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc,
359 new ast::VariableExpr( loc, except_obj ),
360 local_except->get_type()
361 );
362 vcex->location = handler->location;
363 local_except->init = new ast::ListInit(loc, { new ast::SingleInit( loc, vcex ) });
364 block->push_back( new ast::DeclStmt( loc, local_except ) );
365
366 // Add the cleanup attribute.
367 local_except->attributes.push_back( new ast::Attribute(
368 "cleanup",
369 { new ast::NameExpr( loc, "__cfaehm_cleanup_terminate" ) }
370 ) );
371
372 ast::DeclReplacer::DeclMap mapping;
373 mapping[handler_decl] = local_except;
374 const ast::Stmt * mutBody = strict_dynamic_cast<const ast::Stmt *>(
375 ast::DeclReplacer::replace(handler->body, mapping));
376
377
378 block->push_back( mutBody );
379 // handler->body = nullptr;
380
381 handler_wrappers.push_back( new ast::CaseClause(loc,
382 ast::ConstantExpr::from_int(loc, index) ,
383 { block, new ast::ReturnStmt( loc, nullptr ) }
384 ));
385 }
386 // TODO: Some sort of meaningful error on default perhaps?
387
388 ast::SwitchStmt * handler_lookup = new ast::SwitchStmt( loc,
389 new ast::VariableExpr( loc, index_obj ),
390 std::move(handler_wrappers)
391 );
392 ast::CompoundStmt * body = new ast::CompoundStmt( loc, {handler_lookup} );
393
394 func_t->stmts = body;
395 return func_t;
396}
397
398// Create a single check from a moddified handler.
399// except_obj is referenced, modded_handler will be freed.
400ast::CompoundStmt * TryMutatorCore::create_single_matcher(
401 const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler ) {
402 // {
403 // `modded_handler.decl`
404 // if ( `decl.name = (virtual `decl.type`)`except`
405 // [&& `modded_handler.cond`] ) {
406 // `modded_handler.body`
407 // }
408 // }
409
410 const CodeLocation loc = modded_handler->location;
411 ast::CompoundStmt * block = new ast::CompoundStmt(loc);
412
413 // Local Declaration
414 const ast::ObjectDecl * local_except =
415 modded_handler->decl.strict_as<ast::ObjectDecl>();
416 block->push_back( new ast::DeclStmt( loc, local_except ) );
417
418 // Check for type match.
419 ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc,
420 new ast::VariableExpr(loc, except_obj ),
421 local_except->get_type()
422 );
423 ast::Expr * cond = ast::UntypedExpr::createAssign(loc,
424 new ast::VariableExpr(loc, local_except ), vcex );
425
426 // Add the check on the conditional if it is provided.
427 if ( modded_handler->cond ) {
428 cond = new ast::LogicalExpr( loc, cond, modded_handler->cond, ast::LogicalFlag::AndExpr );
429 }
430 // Construct the match condition.
431 block->push_back( new ast::IfStmt(loc,
432 cond, modded_handler->body, nullptr ) );
433
434 return block;
435}
436
437ast::FunctionDecl * TryMutatorCore::create_terminate_match(
438 CatchList &handlers ) {
439 // int match(exception * except) {
440 // HANDLER WRAPPERS { return `index`; }
441 // }
442
443 assert (!handlers.empty());
444 const CodeLocation loc = handlers.front()->location;
445
446 ast::CompoundStmt * body = new ast::CompoundStmt(loc);
447
448 ast::FunctionDecl * func_t = make_match_function( loc );
449 const ast::DeclWithType * except_obj = func_t->params.back();
450
451 // Index 1..{number of handlers}
452 int index = 0;
453 CatchList::iterator it;
454 for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
455 ++index;
456 ast::CatchClause * handler = *it;
457
458 // Body should have been taken by create_terminate_catch.
459 // xxx - just ignore it?
460 // assert( nullptr == handler->get_body() );
461
462 // Create new body.
463 handler->body = new ast::ReturnStmt( handler->location,
464 ast::ConstantExpr::from_int( handler->location, index ) );
465
466 // Create the handler.
467 body->push_back( create_single_matcher( except_obj, handler ) );
468 *it = nullptr;
469 }
470
471 body->push_back( new ast::ReturnStmt(loc,
472 ast::ConstantExpr::from_int( loc, 0 ) ));
473
474 func_t->stmts = body;
475
476 return func_t;
477}
478
479ast::CompoundStmt * TryMutatorCore::create_terminate_caller(
480 CodeLocation loc,
481 ast::FunctionDecl * try_wrapper,
482 ast::FunctionDecl * terminate_catch,
483 ast::FunctionDecl * terminate_match ) {
484 // { __cfaehm_try_terminate(`try`, `catch`, `match`); }
485
486 ast::UntypedExpr * caller = new ast::UntypedExpr(loc, new ast::NameExpr(loc,
487 "__cfaehm_try_terminate" ) );
488 caller->args.push_back( new ast::VariableExpr(loc, try_wrapper ) );
489 caller->args.push_back( new ast::VariableExpr(loc, terminate_catch ) );
490 caller->args.push_back( new ast::VariableExpr(loc, terminate_match ) );
491
492 ast::CompoundStmt * callStmt = new ast::CompoundStmt(loc);
493 callStmt->push_back( new ast::ExprStmt( loc, caller ) );
494 return callStmt;
495}
496
497ast::FunctionDecl * TryMutatorCore::create_resume_handler(
498 CatchList &handlers ) {
499 // bool handle(exception * except) {
500 // HANDLER WRAPPERS { `hander->body`; return true; }
501 // }
502 assert (!handlers.empty());
503 const CodeLocation loc = handlers.front()->location;
504 ast::CompoundStmt * body = new ast::CompoundStmt(loc);
505
506 ast::FunctionDecl * func_t = make_handle_function( loc );
507 const ast::DeclWithType * except_obj = func_t->params.back();
508
509 CatchList::iterator it;
510 for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
511 ast::CatchClause * handler = *it;
512 const CodeLocation loc = handler->location;
513 // Modifiy body.
514 ast::CompoundStmt * handling_code;
515 if (handler->body.as<ast::CompoundStmt>()) {
516 handling_code = strict_dynamic_cast<ast::CompoundStmt*>(
517 handler->body.get_and_mutate() );
518 } else {
519 handling_code = new ast::CompoundStmt(loc);
520 handling_code->push_back( handler->body );
521 }
522 handling_code->push_back( new ast::ReturnStmt(loc,
523 ast::ConstantExpr::from_bool(loc, true ) ) );
524 handler->body = handling_code;
525
526 // Create the handler.
527 body->push_back( create_single_matcher( except_obj, handler ) );
528 *it = nullptr;
529 }
530
531 body->push_back( new ast::ReturnStmt(loc,
532 ast::ConstantExpr::from_bool(loc, false ) ) );
533 func_t->stmts = body;
534
535 return func_t;
536}
537
538ast::CompoundStmt * TryMutatorCore::create_resume_wrapper(
539 const ast::Stmt * wraps,
540 const ast::FunctionDecl * resume_handler ) {
541 const CodeLocation loc = wraps->location;
542 ast::CompoundStmt * body = new ast::CompoundStmt(loc);
543
544 // struct __try_resume_node __resume_node
545 // __attribute__((cleanup( __cfaehm_try_resume_cleanup )));
546 // ** unwinding of the stack here could cause problems **
547 // ** however I don't think that can happen currently **
548 // __cfaehm_try_resume_setup( &__resume_node, resume_handler );
549
550 ast::ObjectDecl * obj = new ast::ObjectDecl(
551 loc,
552 "__resume_node",
553 new ast::StructInstType(
554 node_decl
555 ),
556 nullptr,
557 ast::Storage::Classes{},
558 ast::Linkage::Cforall,
559 nullptr,
560 {new ast::Attribute("cleanup", {new ast::NameExpr(loc, "__cfaehm_try_resume_cleanup")})}
561 );
562 appendDeclStmt( body, obj );
563
564 ast::UntypedExpr *setup = new ast::UntypedExpr(loc, new ast::NameExpr(loc,
565 "__cfaehm_try_resume_setup" ) );
566 setup->args.push_back( new ast::AddressExpr( loc, new ast::VariableExpr(loc, obj ) ) );
567 setup->args.push_back( new ast::VariableExpr( loc, resume_handler ) );
568
569 body->push_back( new ast::ExprStmt(loc, setup ) );
570
571 body->push_back( wraps );
572 return body;
573}
574
575ast::FunctionDecl * TryMutatorCore::create_finally_wrapper(
576 ast::TryStmt * tryStmt ) {
577 // void finally() { `finally->block` }
578 const ast::FinallyClause * finally = tryStmt->finally;
579 const ast::CompoundStmt * body = finally->body;
580
581 ast::FunctionDecl * func_t = make_finally_function( tryStmt->location );
582 func_t->stmts = body;
583
584 tryStmt->finally = nullptr;
585
586 return func_t;
587}
588
589ast::ObjectDecl * TryMutatorCore::create_finally_hook(
590 ast::FunctionDecl * finally_wrapper ) {
591 // struct __cfaehm_cleanup_hook __finally_hook
592 // __attribute__((cleanup( `finally_wrapper` )));
593
594 const CodeLocation loc = finally_wrapper->location;
595 return new ast::ObjectDecl(
596 loc,
597 "__finally_hook",
598 new ast::StructInstType(
599 hook_decl
600 ),
601 nullptr,
602 ast::Storage::Classes{},
603 ast::Linkage::Cforall,
604 nullptr,
605 {new ast::Attribute("cleanup", {new ast::VariableExpr{loc, finally_wrapper}})}
606 );
607}
608
609ast::Stmt * TryMutatorCore::create_resume_rethrow( const ast::ThrowStmt *throwStmt ) {
610 // return false;
611 const CodeLocation loc = throwStmt->location;
612 ast::Stmt * result = new ast::ReturnStmt(loc,
613 ast::ConstantExpr::from_bool( loc, false )
614 );
615 result->labels = throwStmt->labels;
616 return result;
617}
618
619// Visiting/Mutating Functions
620void TryMutatorCore::previsit( const ast::StructDecl *structDecl ) {
621 if ( !structDecl->body ) {
622 // Skip children?
623 return;
624 } else if ( structDecl->name == "__cfaehm_base_exception_t" ) {
625 assert( nullptr == except_decl );
626 except_decl = structDecl;
627 } else if ( structDecl->name == "__cfaehm_try_resume_node" ) {
628 assert( nullptr == node_decl );
629 node_decl = structDecl;
630 } else if ( structDecl->name == "__cfaehm_cleanup_hook" ) {
631 assert( nullptr == hook_decl );
632 hook_decl = structDecl;
633 }
634}
635
636ast::Stmt * TryMutatorCore::postvisit( const ast::TryStmt *tryStmt ) {
637 assert( except_decl );
638 assert( node_decl );
639 assert( hook_decl );
640
641 const CodeLocation loc = tryStmt->location;
642 ast::TryStmt * mutStmt = mutate(tryStmt);
643 // Generate a prefix for the function names?
644
645 ast::CompoundStmt * block = new ast::CompoundStmt( loc );
646 // ast::CompoundStmt * inner = take_try_block( mutStmt );
647 // this is never mutated so let node deletion do its job?
648 const ast::CompoundStmt * inner = mutStmt->body;
649
650 if ( mutStmt->finally ) {
651 // Define the helper function.
652 ast::FunctionDecl * finally_block =
653 create_finally_wrapper( mutStmt );
654 appendDeclStmt( block, finally_block );
655 // Create and add the finally cleanup hook.
656 appendDeclStmt( block, create_finally_hook( finally_block ) );
657 }
658
659 CatchList termination_handlers;
660 CatchList resumption_handlers;
661
662 for (auto & handler: mutStmt->handlers) {
663 // xxx - should always be unique? mutate as safe const-cast
664 assert(handler->unique());
665 if (handler->kind == ast::ExceptionKind::Resume) {
666 resumption_handlers.push_back(handler.get_and_mutate());
667 }
668 else {
669 termination_handlers.push_back(handler.get_and_mutate());
670 }
671 }
672
673 if ( resumption_handlers.size() ) {
674 // Define the helper function.
675 ast::FunctionDecl * resume_handler =
676 create_resume_handler( resumption_handlers );
677 appendDeclStmt( block, resume_handler );
678 // Prepare hooks
679 inner = create_resume_wrapper( inner, resume_handler );
680 }
681
682 if ( termination_handlers.size() ) {
683 // Define the three helper functions.
684 ast::FunctionDecl * try_wrapper = create_try_wrapper( inner );
685 appendDeclStmt( block, try_wrapper );
686 ast::FunctionDecl * terminate_catch =
687 create_terminate_catch( termination_handlers );
688 appendDeclStmt( block, terminate_catch );
689 ast::FunctionDecl * terminate_match =
690 create_terminate_match( termination_handlers );
691 appendDeclStmt( block, terminate_match );
692 // Build the call to the try wrapper.
693 inner = create_terminate_caller(inner->location,
694 try_wrapper, terminate_catch, terminate_match );
695 }
696
697 // Embed the try block.
698 block->push_back( inner );
699
700 return block;
701}
702
703ast::Stmt * TryMutatorCore::postvisit( const ast::ThrowStmt *throwStmt ) {
704 // Only valid `throwResume;` statements should remain. (2/3 checks)
705 assert( ast::ExceptionKind::Resume == throwStmt->kind && ! throwStmt->expr );
706 return create_resume_rethrow( throwStmt );
707}
708
709} // namespace
710
711void translateThrows( ast::TranslationUnit & transUnit ) {
712 ast::Pass<TranslateThrowsCore>::run( transUnit );
713}
714
715void translateTries( ast::TranslationUnit & transUnit ) {
716 ast::Pass<TryMutatorCore>::run(transUnit);
717}
718
719} // namespace ControlStruct
720
721// Local Variables: //
722// tab-width: 4 //
723// mode: c++ //
724// compile-command: "make install" //
725// End: //
Note: See TracBrowser for help on using the repository browser.