Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/ExceptTranslateNew.cpp

    r400b8be r5f3ba11  
    1010// Created On       : Mon Nov  8 11:53:00 2021
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Mar 11 17:51:00 2022
    13 // Update Count     : 2
     12// Last Modified On : Mon Nov  8 16:50:00 2021
     13// Update Count     : 0
    1414//
    1515
     
    2626namespace {
    2727
    28         typedef std::list<ast::CatchClause*> CatchList;
     28        typedef std::list<ast::CatchStmt*> CatchList;
     29
     30        void split( CatchList& allHandlers, CatchList& terHandlers,
     31                                CatchList& resHandlers ) {
     32                while ( !allHandlers.empty() ) {
     33                        ast::CatchStmt * stmt = allHandlers.front();
     34                        allHandlers.pop_front();
     35                        if (stmt->kind == ast::ExceptionKind::Terminate) {
     36                                terHandlers.push_back(stmt);
     37                        } else {
     38                                resHandlers.push_back(stmt);
     39                        }
     40                }
     41        }
    2942
    3043        void appendDeclStmt( ast::CompoundStmt * block, ast::DeclWithType * item ) {
     
    4558        {}
    4659
    47         void previsit( const ast::CatchClause * stmt );
     60        void previsit( const ast::CatchStmt * stmt );
    4861        const ast::Stmt * postvisit( const ast::ThrowStmt * stmt );
    4962};
     
    88101}
    89102
    90 void TranslateThrowsCore::previsit( const ast::CatchClause * stmt ) {
     103void TranslateThrowsCore::previsit( const ast::CatchStmt * stmt ) {
    91104        // Validate the statement's form.
    92105        const ast::ObjectDecl * decl = stmt->decl.as<ast::ObjectDecl>();
     
    147160        ast::FunctionDecl * create_terminate_catch( CatchList &handlers );
    148161        ast::CompoundStmt * create_single_matcher(
    149                 const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler );
     162                const ast::DeclWithType * except_obj, ast::CatchStmt * modded_handler );
    150163        ast::FunctionDecl * create_terminate_match( CatchList &handlers );
    151164        ast::CompoundStmt * create_terminate_caller( CodeLocation loc, ast::FunctionDecl * try_wrapper,
     
    158171        ast::Stmt * create_resume_rethrow( const ast::ThrowStmt * throwStmt );
    159172
    160         // Types used in translation, first group are internal.
    161         ast::ObjectDecl * make_index_object( CodeLocation const & ) const;
    162         ast::ObjectDecl * make_exception_object( CodeLocation const & ) const;
    163         ast::ObjectDecl * make_bool_object( CodeLocation const & ) const;
    164         ast::ObjectDecl * make_voidptr_object( CodeLocation const & ) const;
    165         ast::ObjectDecl * make_unused_index_object( CodeLocation const & ) const;
     173        // Types used in translation, make sure to use clone.
    166174        // void (*function)();
    167         ast::FunctionDecl * make_try_function( CodeLocation const & ) const;
     175        ast::FunctionDecl * try_func_t;
    168176        // void (*function)(int, exception);
    169         ast::FunctionDecl * make_catch_function( CodeLocation const & ) const;
     177        ast::FunctionDecl * catch_func_t;
    170178        // int (*function)(exception);
    171         ast::FunctionDecl * make_match_function( CodeLocation const & ) const;
     179        ast::FunctionDecl * match_func_t;
    172180        // bool (*function)(exception);
    173         ast::FunctionDecl * make_handle_function( CodeLocation const & ) const;
     181        ast::FunctionDecl * handle_func_t;
    174182        // void (*function)(__attribute__((unused)) void *);
    175         ast::FunctionDecl * make_finally_function( CodeLocation const & ) const;
     183        ast::FunctionDecl * finally_func_t;
     184
     185        ast::StructInstType * create_except_type() {
     186                assert( except_decl );
     187                return new ast::StructInstType( except_decl );
     188        }
     189        void init_func_types();
    176190
    177191public:
     
    185199};
    186200
    187 ast::ObjectDecl * TryMutatorCore::make_index_object(
    188                 CodeLocation const & location ) const {
    189         return new ast::ObjectDecl(
    190                 location,
     201void TryMutatorCore::init_func_types() {
     202        assert( except_decl );
     203
     204        ast::ObjectDecl index_obj(
     205                {},
    191206                "__handler_index",
    192                 new ast::BasicType(ast::BasicType::SignedInt),
    193                 nullptr, //init
    194                 ast::Storage::Classes{},
    195                 ast::Linkage::Cforall
    196                 );
    197 }
    198 
    199 ast::ObjectDecl * TryMutatorCore::make_exception_object(
    200                 CodeLocation const & location ) const {
    201         assert( except_decl );
    202         return new ast::ObjectDecl(
    203                 location,
     207                new ast::BasicType(ast::BasicType::SignedInt)
     208                );
     209        ast::ObjectDecl exception_obj(
     210                {},
    204211                "__exception_inst",
    205212                new ast::PointerType(
    206213                        new ast::StructInstType( except_decl )
    207214                        ),
    208                 nullptr, //init
    209                 ast::Storage::Classes{},
    210                 ast::Linkage::Cforall
    211                 );
    212 }
    213 
    214 ast::ObjectDecl * TryMutatorCore::make_bool_object(
    215                 CodeLocation const & location ) const {
    216         return new ast::ObjectDecl(
    217                 location,
     215                NULL
     216                );
     217        ast::ObjectDecl bool_obj(
     218                {},
    218219                "__ret_bool",
    219220                new ast::BasicType( ast::BasicType::Bool ),
     
    224225                std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
    225226                );
    226 }
    227 
    228 ast::ObjectDecl * TryMutatorCore::make_voidptr_object(
    229                 CodeLocation const & location ) const {
    230         return new ast::ObjectDecl(
    231                 location,
     227        ast::ObjectDecl voidptr_obj(
     228                {},
    232229                "__hook",
    233230                new ast::PointerType(
     
    240237                std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
    241238                );
    242 }
    243 
    244 ast::ObjectDecl * TryMutatorCore::make_unused_index_object(
    245                 CodeLocation const & location ) const {
    246         return new ast::ObjectDecl(
    247                 location,
     239
     240        ast::ObjectDecl unused_index_obj(
     241                {},
    248242                "__handler_index",
    249243                new ast::BasicType(ast::BasicType::SignedInt),
     
    254248                std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
    255249        );
    256 }
    257 
    258 ast::FunctionDecl * TryMutatorCore::make_try_function(
    259                 CodeLocation const & location ) const {
    260         return new ast::FunctionDecl(
    261                 location,
     250        //unused_index_obj->attributes.push_back( new Attribute( "unused" ) );
     251
     252        try_func_t = new ast::FunctionDecl(
     253                {},
    262254                "try",
    263255                {}, //forall
     
    268260                ast::Linkage::Cforall
    269261        );
    270 }
    271 
    272 ast::FunctionDecl * TryMutatorCore::make_catch_function(
    273                 CodeLocation const & location ) const {
    274         return new ast::FunctionDecl(
    275                 location,
     262
     263        catch_func_t = new ast::FunctionDecl(
     264                {},
    276265                "catch",
    277266                {}, //forall
    278                 { make_index_object( location ), make_exception_object( location ) },
     267                {ast::deepCopy(&index_obj), ast::deepCopy(&exception_obj)},//param
    279268                {}, //return void
    280269                nullptr,
     
    282271                ast::Linkage::Cforall
    283272        );
    284 }
    285 
    286 ast::FunctionDecl * TryMutatorCore::make_match_function(
    287                 CodeLocation const & location ) const {
    288         return new ast::FunctionDecl(
    289                 location,
     273
     274        match_func_t = new ast::FunctionDecl(
     275                {},
    290276                "match",
    291277                {}, //forall
    292                 { make_exception_object( location ) },
    293                 { make_unused_index_object( location ) },
     278                {ast::deepCopy(&exception_obj)},
     279                {ast::deepCopy(&unused_index_obj)},
    294280                nullptr,
    295281                ast::Storage::Classes{},
    296282                ast::Linkage::Cforall
    297283        );
    298 }
    299 
    300 ast::FunctionDecl * TryMutatorCore::make_handle_function(
    301                 CodeLocation const & location ) const {
    302         return new ast::FunctionDecl(
    303                 location,
     284
     285        handle_func_t = new ast::FunctionDecl(
     286                {},
    304287                "handle",
    305288                {}, //forall
    306                 { make_exception_object( location ) },
    307                 { make_bool_object( location ) },
     289                {ast::deepCopy(&exception_obj)},
     290                {ast::deepCopy(&bool_obj)},
    308291                nullptr,
    309292                ast::Storage::Classes{},
    310293                ast::Linkage::Cforall
    311294        );
    312 }
    313 
    314 ast::FunctionDecl * TryMutatorCore::make_finally_function(
    315                 CodeLocation const & location ) const {
    316         return new ast::FunctionDecl(
    317                 location,
     295
     296        finally_func_t = new ast::FunctionDecl(
     297                {},
    318298                "finally",
    319299                {}, //forall
    320                 { make_voidptr_object( location ) },
     300                {ast::deepCopy(&voidptr_obj)},
    321301                {}, //return void
    322302                nullptr,
     
    324304                ast::Linkage::Cforall
    325305        );
     306
     307        //catch_func_t.get_parameters().push_back( index_obj.clone() );
     308        //catch_func_t.get_parameters().push_back( exception_obj.clone() );
     309        //match_func_t.get_returnVals().push_back( unused_index_obj );
     310        //match_func_t.get_parameters().push_back( exception_obj.clone() );
     311        //handle_func_t.get_returnVals().push_back( bool_obj.clone() );
     312        //handle_func_t.get_parameters().push_back( exception_obj.clone() );
     313        //finally_func_t.get_parameters().push_back( voidptr_obj.clone() );
    326314}
    327315
    328316// TryStmt Mutation Helpers
     317
     318/*
     319ast::CompoundStmt * TryMutatorCore::take_try_block( ast::TryStmt *tryStmt ) {
     320        ast::CompoundStmt * block = tryStmt->body;
     321        tryStmt->body = nullptr;
     322        return block;
     323}
     324*/
    329325
    330326ast::FunctionDecl * TryMutatorCore::create_try_wrapper(
    331327                const ast::CompoundStmt *body ) {
    332328
    333         ast::FunctionDecl * ret = make_try_function( body->location );
     329        ast::FunctionDecl * ret = ast::deepCopy(try_func_t);
    334330        ret->stmts = body;
    335331        return ret;
     
    338334ast::FunctionDecl * TryMutatorCore::create_terminate_catch(
    339335                CatchList &handlers ) {
    340         std::vector<ast::ptr<ast::CaseClause>> handler_wrappers;
     336        std::vector<ast::ptr<ast::Stmt>> handler_wrappers;
    341337
    342338        assert (!handlers.empty());
    343339        const CodeLocation loc = handlers.front()->location;
    344340
    345         ast::FunctionDecl * func_t = make_catch_function( loc );
     341        ast::FunctionDecl * func_t = ast::deepCopy(catch_func_t);
    346342        const ast::DeclWithType * index_obj = func_t->params.front();
    347343        const ast::DeclWithType * except_obj = func_t->params.back();
     
    352348        for ( ; it != handlers.end() ; ++it ) {
    353349                ++index;
    354                 ast::CatchClause * handler = *it;
     350                ast::CatchStmt * handler = *it;
    355351                const CodeLocation loc = handler->location;
    356352
     
    390386                // handler->body = nullptr;
    391387
    392                 handler_wrappers.push_back( new ast::CaseClause(loc,
     388                handler_wrappers.push_back( new ast::CaseStmt(loc,
    393389                        ast::ConstantExpr::from_int(loc, index) ,
    394390                        { block, new ast::ReturnStmt( loc, nullptr ) }
     
    397393        // TODO: Some sort of meaningful error on default perhaps?
    398394
    399         ast::SwitchStmt * handler_lookup = new ast::SwitchStmt( loc,
     395        /*
     396        std::list<Statement*> stmt_handlers;
     397        while ( !handler_wrappers.empty() ) {
     398                stmt_handlers.push_back( handler_wrappers.front() );
     399                handler_wrappers.pop_front();
     400        }
     401        */
     402
     403        ast::SwitchStmt * handler_lookup = new ast::SwitchStmt(loc,
    400404                new ast::VariableExpr( loc, index_obj ),
    401405                std::move(handler_wrappers)
    402406                );
    403         ast::CompoundStmt * body = new ast::CompoundStmt( loc, {handler_lookup} );
     407        ast::CompoundStmt * body = new ast::CompoundStmt(loc,
     408                {handler_lookup});
    404409
    405410        func_t->stmts = body;
     
    410415// except_obj is referenced, modded_handler will be freed.
    411416ast::CompoundStmt * TryMutatorCore::create_single_matcher(
    412                 const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler ) {
     417                const ast::DeclWithType * except_obj, ast::CatchStmt * modded_handler ) {
    413418        // {
    414419        //     `modded_handler.decl`
     
    428433
    429434        // Check for type match.
    430         ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc,
     435        ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc, 
    431436                new ast::VariableExpr(loc, except_obj ),
    432437                local_except->get_type()
     
    440445        }
    441446        // Construct the match condition.
    442         block->push_back( new ast::IfStmt(loc,
     447        block->push_back( new ast::IfStmt(loc, 
    443448                cond, modded_handler->body, nullptr ) );
    444449
     450        // xxx - how does this work in new ast
     451        //modded_handler->set_decl( nullptr );
     452        //modded_handler->set_cond( nullptr );
     453        //modded_handler->set_body( nullptr );
     454        //delete modded_handler;
    445455        return block;
    446456}
     
    457467        ast::CompoundStmt * body = new ast::CompoundStmt(loc);
    458468
    459         ast::FunctionDecl * func_t = make_match_function( loc );
     469        ast::FunctionDecl * func_t = ast::deepCopy(match_func_t);
    460470        const ast::DeclWithType * except_obj = func_t->params.back();
    461471
     
    465475        for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
    466476                ++index;
    467                 ast::CatchClause * handler = *it;
     477                ast::CatchStmt * handler = *it;
    468478
    469479                // Body should have been taken by create_terminate_catch.
     
    480490        }
    481491
    482         body->push_back( new ast::ReturnStmt(loc,
     492        body->push_back( new ast::ReturnStmt(loc, 
    483493                ast::ConstantExpr::from_int( loc, 0 ) ));
    484494
     
    515525        ast::CompoundStmt * body = new ast::CompoundStmt(loc);
    516526
    517         ast::FunctionDecl * func_t = make_handle_function( loc );
     527        ast::FunctionDecl * func_t = ast::deepCopy(handle_func_t);
    518528        const ast::DeclWithType * except_obj = func_t->params.back();
    519529
    520530        CatchList::iterator it;
    521531        for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
    522                 ast::CatchClause * handler = *it;
     532                ast::CatchStmt * handler = *it;
    523533                const CodeLocation loc = handler->location;
    524534                // Modifiy body.
    525535                ast::CompoundStmt * handling_code;
    526536                if (handler->body.as<ast::CompoundStmt>()) {
    527                         handling_code = strict_dynamic_cast<ast::CompoundStmt*>(
    528                                 handler->body.get_and_mutate() );
     537                        handling_code =
     538                        strict_dynamic_cast<ast::CompoundStmt*>( handler->body.get_and_mutate() );
    529539                } else {
    530540                        handling_code = new ast::CompoundStmt(loc);
     
    587597                ast::TryStmt * tryStmt ) {
    588598        // void finally() { `finally->block` }
    589         const ast::FinallyClause * finally = tryStmt->finally;
     599        const ast::FinallyStmt * finally = tryStmt->finally;
    590600        const ast::CompoundStmt * body = finally->body;
    591601
    592         ast::FunctionDecl * func_t = make_finally_function( tryStmt->location );
     602        ast::FunctionDecl * func_t = ast::deepCopy(finally_func_t);
    593603        func_t->stmts = body;
    594604
     605        // finally->set_block( nullptr );
     606        // delete finally;
    595607        tryStmt->finally = nullptr;
     608
    596609
    597610        return func_t;
     
    604617
    605618        const CodeLocation loc = finally_wrapper->location;
     619        // Make Cleanup Attribute.
     620        /*
     621        std::list< ast::Attribute * > attributes;
     622        {
     623                std::list<  > attr_params;
     624                attr_params.push_back( nameOf( finally_wrapper ) );
     625                attributes.push_back( new Attribute( "cleanup", attr_params ) );
     626        }
     627        */
     628
    606629        return new ast::ObjectDecl(
    607630                loc,
     
    621644        // return false;
    622645        const CodeLocation loc = throwStmt->location;
    623         ast::Stmt * result = new ast::ReturnStmt(loc,
     646        ast::Stmt * result = new ast::ReturnStmt(loc, 
    624647                ast::ConstantExpr::from_bool( loc, false )
    625648                );
    626649        result->labels = throwStmt->labels;
     650        // delete throwStmt; done by postvisit
    627651        return result;
    628652}
     
    636660                assert( nullptr == except_decl );
    637661                except_decl = structDecl;
     662                init_func_types();
    638663        } else if ( structDecl->name == "__cfaehm_try_resume_node" ) {
    639664                assert( nullptr == node_decl );
     
    681706                }
    682707        }
     708        // split( mutStmt->handlers,
     709        //              termination_handlers, resumption_handlers );
    683710
    684711        if ( resumption_handlers.size() ) {
Note: See TracChangeset for help on using the changeset viewer.