source: src/ControlStruct/ExceptTranslate.cc @ 2daea42

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 2daea42 was 7f9968a, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Fixed a problem with 'throwResume;' translation and added some tests to check for similar problems.

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