source: src/ControlStruct/ExceptTranslate.cc @ e195093

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since e195093 was 948b0c8, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Reorginization of ExceptTranslate? to make the next steps easier.

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