source: src/ControlStruct/ExceptTranslate.cc @ 9f2012f

new-envwith_gc
Last change on this file since 9f2012f was bfc7811, checked in by Aaron Moss <a3moss@…>, 6 years ago

Fix some GC bugs

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