| [ba912706] | 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 | 
|---|
| [3090127] | 11 | // Last Modified By : Andrew Beach | 
|---|
| [7f9968ad] | 12 | // Last Modified On : Wed Jun 24 11:18:00 2020 | 
|---|
|  | 13 | // Update Count     : 17 | 
|---|
| [ba912706] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "ExceptTranslate.h" | 
|---|
| [d180746] | 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 | 
|---|
| [07de76b] | 26 | #include "SynTree/LinkageSpec.h"      // for Cforall | 
|---|
| [d180746] | 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 | 
|---|
| [ba3706f] | 32 | #include "SynTree/Label.h"            // for Label | 
|---|
| [d180746] | 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 | 
|---|
| [7862059] | 36 | #include "SynTree/DeclReplacer.h"     // for DeclReplacer | 
|---|
| [d180746] | 37 | #include "SynTree/Visitor.h"          // for acceptAll | 
|---|
| [ba912706] | 38 |  | 
|---|
| [307a732] | 39 | namespace ControlStruct { | 
|---|
| [ba912706] | 40 |  | 
|---|
| [948b0c8] | 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 ) { | 
|---|
| [ba3706f] | 59 | block->push_back(new DeclStmt(item)); | 
|---|
| [948b0c8] | 60 | } | 
|---|
|  | 61 |  | 
|---|
|  | 62 | Expression * nameOf( DeclarationWithType * decl ) { | 
|---|
|  | 63 | return new VariableExpr( decl ); | 
|---|
|  | 64 | } | 
|---|
|  | 65 |  | 
|---|
| [046a890] | 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( | 
|---|
| [7119daa] | 108 | terminate_handler_except->get_type()->clone() | 
|---|
| [046a890] | 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. | 
|---|
| [b2de2e0] | 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" ); | 
|---|
| [046a890] | 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 ) { | 
|---|
| [7f9968ad] | 157 | // This has to be handled later. | 
|---|
|  | 158 | return throwStmt; | 
|---|
| [046a890] | 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 |  | 
|---|
| [66ba544] | 167 | class TryMutatorCore { | 
|---|
| [948b0c8] | 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 ); | 
|---|
| [7f9968ad] | 187 | Statement * create_resume_rethrow( ThrowStmt * throwStmt ); | 
|---|
| [948b0c8] | 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: | 
|---|
| [046a890] | 208 | TryMutatorCore() : | 
|---|
| [948b0c8] | 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 ) | 
|---|
| [cbce272] | 215 | {} | 
|---|
| [948b0c8] | 216 |  | 
|---|
|  | 217 | void premutate( StructDecl *structDecl ); | 
|---|
|  | 218 | Statement * postmutate( TryStmt *tryStmt ); | 
|---|
| [7f9968ad] | 219 | Statement * postmutate( ThrowStmt *throwStmt ); | 
|---|
| [948b0c8] | 220 | }; | 
|---|
|  | 221 |  | 
|---|
| [046a890] | 222 | void TryMutatorCore::init_func_types() { | 
|---|
| [cbce272] | 223 | assert( except_decl ); | 
|---|
|  | 224 |  | 
|---|
| [ba912706] | 225 | ObjectDecl index_obj( | 
|---|
| [288eede] | 226 | "__handler_index", | 
|---|
| [ba912706] | 227 | Type::StorageClasses(), | 
|---|
|  | 228 | LinkageSpec::Cforall, | 
|---|
|  | 229 | /*bitfieldWidth*/ NULL, | 
|---|
| [ac10576] | 230 | new BasicType( noQualifiers, BasicType::SignedInt ), | 
|---|
| [ba912706] | 231 | /*init*/ NULL | 
|---|
| [307a732] | 232 | ); | 
|---|
| [ba912706] | 233 | ObjectDecl exception_obj( | 
|---|
| [288eede] | 234 | "__exception_inst", | 
|---|
| [ba912706] | 235 | Type::StorageClasses(), | 
|---|
|  | 236 | LinkageSpec::Cforall, | 
|---|
|  | 237 | /*bitfieldWidth*/ NULL, | 
|---|
| [307a732] | 238 | new PointerType( | 
|---|
| [ac10576] | 239 | noQualifiers, | 
|---|
| [cbce272] | 240 | new StructInstType( noQualifiers, except_decl ) | 
|---|
| [307a732] | 241 | ), | 
|---|
| [ba912706] | 242 | /*init*/ NULL | 
|---|
| [307a732] | 243 | ); | 
|---|
| [ba912706] | 244 | ObjectDecl bool_obj( | 
|---|
| [288eede] | 245 | "__ret_bool", | 
|---|
| [ba912706] | 246 | Type::StorageClasses(), | 
|---|
|  | 247 | LinkageSpec::Cforall, | 
|---|
|  | 248 | /*bitfieldWidth*/ NULL, | 
|---|
| [948b0c8] | 249 | new BasicType( noQualifiers, BasicType::Bool ), | 
|---|
| [8f6dfe7] | 250 | /*init*/ NULL, | 
|---|
|  | 251 | std::list<Attribute *>{ new Attribute( "unused" ) } | 
|---|
| [307a732] | 252 | ); | 
|---|
|  | 253 | ObjectDecl voidptr_obj( | 
|---|
|  | 254 | "__hook", | 
|---|
|  | 255 | Type::StorageClasses(), | 
|---|
|  | 256 | LinkageSpec::Cforall, | 
|---|
|  | 257 | NULL, | 
|---|
|  | 258 | new PointerType( | 
|---|
| [ac10576] | 259 | noQualifiers, | 
|---|
| [307a732] | 260 | new VoidType( | 
|---|
| [ac10576] | 261 | noQualifiers | 
|---|
| [307a732] | 262 | ), | 
|---|
| [948b0c8] | 263 | std::list<Attribute *>{ new Attribute( "unused" ) } | 
|---|
| [307a732] | 264 | ), | 
|---|
|  | 265 | NULL | 
|---|
|  | 266 | ); | 
|---|
| [ba912706] | 267 |  | 
|---|
| [8f6dfe7] | 268 | ObjectDecl * unused_index_obj = index_obj.clone(); | 
|---|
|  | 269 | unused_index_obj->attributes.push_back( new Attribute( "unused" ) ); | 
|---|
|  | 270 |  | 
|---|
| [307a732] | 271 | catch_func_t.get_parameters().push_back( index_obj.clone() ); | 
|---|
|  | 272 | catch_func_t.get_parameters().push_back( exception_obj.clone() ); | 
|---|
| [8f6dfe7] | 273 | match_func_t.get_returnVals().push_back( unused_index_obj ); | 
|---|
| [307a732] | 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() ); | 
|---|
| [ba912706] | 278 | } | 
|---|
|  | 279 |  | 
|---|
|  | 280 | // TryStmt Mutation Helpers | 
|---|
|  | 281 |  | 
|---|
| [046a890] | 282 | CompoundStmt * TryMutatorCore::take_try_block( TryStmt *tryStmt ) { | 
|---|
| [ba912706] | 283 | CompoundStmt * block = tryStmt->get_block(); | 
|---|
|  | 284 | tryStmt->set_block( nullptr ); | 
|---|
|  | 285 | return block; | 
|---|
|  | 286 | } | 
|---|
| [948b0c8] | 287 |  | 
|---|
| [046a890] | 288 | FunctionDecl * TryMutatorCore::create_try_wrapper( | 
|---|
| [948b0c8] | 289 | CompoundStmt *body ) { | 
|---|
| [ba912706] | 290 |  | 
|---|
| [288eede] | 291 | return new FunctionDecl( "try", Type::StorageClasses(), | 
|---|
| [307a732] | 292 | LinkageSpec::Cforall, try_func_t.clone(), body ); | 
|---|
| [ba912706] | 293 | } | 
|---|
|  | 294 |  | 
|---|
| [046a890] | 295 | FunctionDecl * TryMutatorCore::create_terminate_catch( | 
|---|
| [948b0c8] | 296 | CatchList &handlers ) { | 
|---|
| [ba912706] | 297 | std::list<CaseStmt *> handler_wrappers; | 
|---|
|  | 298 |  | 
|---|
| [288eede] | 299 | FunctionType *func_type = catch_func_t.clone(); | 
|---|
|  | 300 | DeclarationWithType * index_obj = func_type->get_parameters().front(); | 
|---|
| [86d5ba7c] | 301 | DeclarationWithType * except_obj = func_type->get_parameters().back(); | 
|---|
| [288eede] | 302 |  | 
|---|
| [ba912706] | 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 |  | 
|---|
| [288eede] | 310 | // case `index`: | 
|---|
|  | 311 | // { | 
|---|
| [cbce272] | 312 | //     `handler.decl` = { (virtual `decl.type`)`except` }; | 
|---|
|  | 313 | //     `handler.body`; | 
|---|
| [288eede] | 314 | // } | 
|---|
|  | 315 | // return; | 
|---|
| [ba3706f] | 316 | CompoundStmt * block = new CompoundStmt(); | 
|---|
| [86d5ba7c] | 317 |  | 
|---|
| [03eedd5] | 318 | // Just copy the exception value. (Post Validation) | 
|---|
| [86d5ba7c] | 319 | ObjectDecl * handler_decl = | 
|---|
| [03eedd5] | 320 | static_cast<ObjectDecl *>( handler->get_decl() ); | 
|---|
| [86d5ba7c] | 321 | ObjectDecl * local_except = handler_decl->clone(); | 
|---|
| [03eedd5] | 322 | local_except->set_init( | 
|---|
| [86d5ba7c] | 323 | new ListInit({ new SingleInit( | 
|---|
|  | 324 | new VirtualCastExpr( nameOf( except_obj ), | 
|---|
|  | 325 | local_except->get_type() | 
|---|
|  | 326 | ) | 
|---|
| [03eedd5] | 327 | ) }) | 
|---|
|  | 328 | ); | 
|---|
| [ba3706f] | 329 | block->push_back( new DeclStmt( local_except ) ); | 
|---|
| [86d5ba7c] | 330 |  | 
|---|
|  | 331 | // Add the cleanup attribute. | 
|---|
|  | 332 | local_except->get_attributes().push_back( new Attribute( | 
|---|
|  | 333 | "cleanup", | 
|---|
| [3090127] | 334 | { new NameExpr( "__cfaehm_cleanup_terminate" ) } | 
|---|
| [86d5ba7c] | 335 | ) ); | 
|---|
|  | 336 |  | 
|---|
|  | 337 | // Update variables in the body to point to this local copy. | 
|---|
|  | 338 | { | 
|---|
| [7862059] | 339 | DeclReplacer::DeclMap mapping; | 
|---|
| [86d5ba7c] | 340 | mapping[ handler_decl ] = local_except; | 
|---|
| [7862059] | 341 | DeclReplacer::replace( handler->body, mapping ); | 
|---|
| [86d5ba7c] | 342 | } | 
|---|
|  | 343 |  | 
|---|
| [7543dec] | 344 | block->push_back( handler->body ); | 
|---|
|  | 345 | handler->body = nullptr; | 
|---|
| [288eede] | 346 |  | 
|---|
| [86d5ba7c] | 347 | std::list<Statement *> caseBody | 
|---|
| [ba3706f] | 348 | { block, new ReturnStmt( nullptr ) }; | 
|---|
| [288eede] | 349 | handler_wrappers.push_back( new CaseStmt( | 
|---|
| [ba912706] | 350 | new ConstantExpr( Constant::from_int( index ) ), | 
|---|
| [288eede] | 351 | caseBody | 
|---|
|  | 352 | ) ); | 
|---|
| [ba912706] | 353 | } | 
|---|
|  | 354 | // TODO: Some sort of meaningful error on default perhaps? | 
|---|
|  | 355 |  | 
|---|
| [288eede] | 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 |  | 
|---|
| [ba912706] | 362 | SwitchStmt * handler_lookup = new SwitchStmt( | 
|---|
| [288eede] | 363 | nameOf( index_obj ), | 
|---|
|  | 364 | stmt_handlers | 
|---|
| [ba912706] | 365 | ); | 
|---|
| [ba3706f] | 366 | CompoundStmt * body = new CompoundStmt(); | 
|---|
| [ba912706] | 367 | body->push_back( handler_lookup ); | 
|---|
|  | 368 |  | 
|---|
|  | 369 | return new FunctionDecl("catch", Type::StorageClasses(), | 
|---|
| [288eede] | 370 | LinkageSpec::Cforall, func_type, body); | 
|---|
| [ba912706] | 371 | } | 
|---|
|  | 372 |  | 
|---|
|  | 373 | // Create a single check from a moddified handler. | 
|---|
| [288eede] | 374 | // except_obj is referenced, modded_handler will be freed. | 
|---|
| [046a890] | 375 | CompoundStmt * TryMutatorCore::create_single_matcher( | 
|---|
| [288eede] | 376 | DeclarationWithType * except_obj, CatchStmt * modded_handler ) { | 
|---|
| [86d5ba7c] | 377 | // { | 
|---|
|  | 378 | //     `modded_handler.decl` | 
|---|
| [cbce272] | 379 | //     if ( `decl.name = (virtual `decl.type`)`except` | 
|---|
| [86d5ba7c] | 380 | //             [&& `modded_handler.cond`] ) { | 
|---|
|  | 381 | //         `modded_handler.body` | 
|---|
|  | 382 | //     } | 
|---|
|  | 383 | // } | 
|---|
|  | 384 |  | 
|---|
| [ba3706f] | 385 | CompoundStmt * block = new CompoundStmt(); | 
|---|
| [cbce272] | 386 |  | 
|---|
|  | 387 | // Local Declaration | 
|---|
|  | 388 | ObjectDecl * local_except = | 
|---|
|  | 389 | dynamic_cast<ObjectDecl *>( modded_handler->get_decl() ); | 
|---|
|  | 390 | assert( local_except ); | 
|---|
| [ba3706f] | 391 | block->push_back( new DeclStmt( local_except ) ); | 
|---|
| [cbce272] | 392 |  | 
|---|
| [86d5ba7c] | 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() ) ); | 
|---|
| [ba912706] | 397 |  | 
|---|
| [86d5ba7c] | 398 | // Add the check on the conditional if it is provided. | 
|---|
| [ba912706] | 399 | if ( modded_handler->get_cond() ) { | 
|---|
| [288eede] | 400 | cond = new LogicalExpr( cond, modded_handler->get_cond() ); | 
|---|
| [ba912706] | 401 | } | 
|---|
| [86d5ba7c] | 402 | // Construct the match condition. | 
|---|
| [ba3706f] | 403 | block->push_back( new IfStmt( | 
|---|
| [288eede] | 404 | cond, modded_handler->get_body(), nullptr ) ); | 
|---|
| [ba912706] | 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 |  | 
|---|
| [046a890] | 413 | FunctionDecl * TryMutatorCore::create_terminate_match( | 
|---|
| [948b0c8] | 414 | CatchList &handlers ) { | 
|---|
| [86d5ba7c] | 415 | // int match(exception * except) { | 
|---|
|  | 416 | //     HANDLER WRAPPERS { return `index`; } | 
|---|
|  | 417 | // } | 
|---|
|  | 418 |  | 
|---|
| [ba3706f] | 419 | CompoundStmt * body = new CompoundStmt(); | 
|---|
| [ba912706] | 420 |  | 
|---|
| [288eede] | 421 | FunctionType * func_type = match_func_t.clone(); | 
|---|
|  | 422 | DeclarationWithType * except_obj = func_type->get_parameters().back(); | 
|---|
|  | 423 |  | 
|---|
| [ba912706] | 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 |  | 
|---|
| [288eede] | 431 | // Body should have been taken by create_terminate_catch. | 
|---|
|  | 432 | assert( nullptr == handler->get_body() ); | 
|---|
|  | 433 |  | 
|---|
|  | 434 | // Create new body. | 
|---|
| [ba3706f] | 435 | handler->set_body( new ReturnStmt( | 
|---|
| [ba912706] | 436 | new ConstantExpr( Constant::from_int( index ) ) ) ); | 
|---|
|  | 437 |  | 
|---|
| [288eede] | 438 | // Create the handler. | 
|---|
|  | 439 | body->push_back( create_single_matcher( except_obj, handler ) ); | 
|---|
|  | 440 | *it = nullptr; | 
|---|
| [ba912706] | 441 | } | 
|---|
|  | 442 |  | 
|---|
| [ba3706f] | 443 | body->push_back( new ReturnStmt( | 
|---|
| [e9145a3] | 444 | new ConstantExpr( Constant::from_int( 0 ) ) ) ); | 
|---|
| [307a732] | 445 |  | 
|---|
| [ba912706] | 446 | return new FunctionDecl("match", Type::StorageClasses(), | 
|---|
| [288eede] | 447 | LinkageSpec::Cforall, func_type, body); | 
|---|
| [ba912706] | 448 | } | 
|---|
|  | 449 |  | 
|---|
| [046a890] | 450 | CompoundStmt * TryMutatorCore::create_terminate_caller( | 
|---|
| [ba912706] | 451 | FunctionDecl * try_wrapper, | 
|---|
|  | 452 | FunctionDecl * terminate_catch, | 
|---|
| [948b0c8] | 453 | FunctionDecl * terminate_match ) { | 
|---|
| [3090127] | 454 | // { __cfaehm_try_terminate(`try`, `catch`, `match`); } | 
|---|
| [ba912706] | 455 |  | 
|---|
| [288eede] | 456 | UntypedExpr * caller = new UntypedExpr( new NameExpr( | 
|---|
| [3090127] | 457 | "__cfaehm_try_terminate" ) ); | 
|---|
| [288eede] | 458 | std::list<Expression *>& args = caller->get_args(); | 
|---|
| [ba912706] | 459 | args.push_back( nameOf( try_wrapper ) ); | 
|---|
|  | 460 | args.push_back( nameOf( terminate_catch ) ); | 
|---|
|  | 461 | args.push_back( nameOf( terminate_match ) ); | 
|---|
|  | 462 |  | 
|---|
| [ba3706f] | 463 | CompoundStmt * callStmt = new CompoundStmt(); | 
|---|
|  | 464 | callStmt->push_back( new ExprStmt( caller ) ); | 
|---|
| [288eede] | 465 | return callStmt; | 
|---|
| [ba912706] | 466 | } | 
|---|
|  | 467 |  | 
|---|
| [046a890] | 468 | FunctionDecl * TryMutatorCore::create_resume_handler( | 
|---|
| [948b0c8] | 469 | CatchList &handlers ) { | 
|---|
| [86d5ba7c] | 470 | // bool handle(exception * except) { | 
|---|
|  | 471 | //     HANDLER WRAPPERS { `hander->body`; return true; } | 
|---|
|  | 472 | // } | 
|---|
| [ba3706f] | 473 | CompoundStmt * body = new CompoundStmt(); | 
|---|
| [288eede] | 474 |  | 
|---|
| [e9145a3] | 475 | FunctionType * func_type = handle_func_t.clone(); | 
|---|
| [288eede] | 476 | DeclarationWithType * except_obj = func_type->get_parameters().back(); | 
|---|
| [ba912706] | 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 ) { | 
|---|
| [ba3706f] | 486 | handling_code = new CompoundStmt(); | 
|---|
| [ba912706] | 487 | handling_code->push_back( handler->get_body() ); | 
|---|
|  | 488 | } | 
|---|
| [ba3706f] | 489 | handling_code->push_back( new ReturnStmt( | 
|---|
| [ad0be81] | 490 | new ConstantExpr( Constant::from_bool( true ) ) ) ); | 
|---|
| [ba912706] | 491 | handler->set_body( handling_code ); | 
|---|
|  | 492 |  | 
|---|
|  | 493 | // Create the handler. | 
|---|
| [288eede] | 494 | body->push_back( create_single_matcher( except_obj, handler ) ); | 
|---|
|  | 495 | *it = nullptr; | 
|---|
| [ba912706] | 496 | } | 
|---|
|  | 497 |  | 
|---|
| [ba3706f] | 498 | body->push_back( new ReturnStmt( | 
|---|
| [e9145a3] | 499 | new ConstantExpr( Constant::from_bool( false ) ) ) ); | 
|---|
| [ad0be81] | 500 |  | 
|---|
| [ba912706] | 501 | return new FunctionDecl("handle", Type::StorageClasses(), | 
|---|
| [288eede] | 502 | LinkageSpec::Cforall, func_type, body); | 
|---|
| [ba912706] | 503 | } | 
|---|
|  | 504 |  | 
|---|
| [046a890] | 505 | CompoundStmt * TryMutatorCore::create_resume_wrapper( | 
|---|
| [ba912706] | 506 | Statement * wraps, | 
|---|
|  | 507 | FunctionDecl * resume_handler ) { | 
|---|
| [ba3706f] | 508 | CompoundStmt * body = new CompoundStmt(); | 
|---|
| [ba912706] | 509 |  | 
|---|
| [288eede] | 510 | // struct __try_resume_node __resume_node | 
|---|
| [3090127] | 511 | //      __attribute__((cleanup( __cfaehm_try_resume_cleanup ))); | 
|---|
| [288eede] | 512 | // ** unwinding of the stack here could cause problems ** | 
|---|
|  | 513 | // ** however I don't think that can happen currently ** | 
|---|
| [3090127] | 514 | // __cfaehm_try_resume_setup( &__resume_node, resume_handler ); | 
|---|
| [ba912706] | 515 |  | 
|---|
|  | 516 | std::list< Attribute * > attributes; | 
|---|
|  | 517 | { | 
|---|
|  | 518 | std::list< Expression * > attr_params; | 
|---|
| [288eede] | 519 | attr_params.push_back( new NameExpr( | 
|---|
| [3090127] | 520 | "__cfaehm_try_resume_cleanup" ) ); | 
|---|
| [288eede] | 521 | attributes.push_back( new Attribute( "cleanup", attr_params ) ); | 
|---|
| [ba912706] | 522 | } | 
|---|
|  | 523 |  | 
|---|
| [288eede] | 524 | ObjectDecl * obj = new ObjectDecl( | 
|---|
|  | 525 | "__resume_node", | 
|---|
| [ba912706] | 526 | Type::StorageClasses(), | 
|---|
|  | 527 | LinkageSpec::Cforall, | 
|---|
|  | 528 | nullptr, | 
|---|
| [288eede] | 529 | new StructInstType( | 
|---|
|  | 530 | Type::Qualifiers(), | 
|---|
|  | 531 | node_decl | 
|---|
|  | 532 | ), | 
|---|
|  | 533 | nullptr, | 
|---|
| [ba912706] | 534 | attributes | 
|---|
| [288eede] | 535 | ); | 
|---|
|  | 536 | appendDeclStmt( body, obj ); | 
|---|
|  | 537 |  | 
|---|
|  | 538 | UntypedExpr *setup = new UntypedExpr( new NameExpr( | 
|---|
| [3090127] | 539 | "__cfaehm_try_resume_setup" ) ); | 
|---|
| [307a732] | 540 | setup->get_args().push_back( new AddressExpr( nameOf( obj ) ) ); | 
|---|
| [288eede] | 541 | setup->get_args().push_back( nameOf( resume_handler ) ); | 
|---|
|  | 542 |  | 
|---|
| [ba3706f] | 543 | body->push_back( new ExprStmt( setup ) ); | 
|---|
| [288eede] | 544 |  | 
|---|
| [ba912706] | 545 | body->push_back( wraps ); | 
|---|
|  | 546 | return body; | 
|---|
|  | 547 | } | 
|---|
|  | 548 |  | 
|---|
| [046a890] | 549 | FunctionDecl * TryMutatorCore::create_finally_wrapper( | 
|---|
| [948b0c8] | 550 | TryStmt * tryStmt ) { | 
|---|
| [66ba544] | 551 | // void finally() { `finally->block` } | 
|---|
| [288eede] | 552 | FinallyStmt * finally = tryStmt->get_finally(); | 
|---|
|  | 553 | CompoundStmt * body = finally->get_block(); | 
|---|
|  | 554 | finally->set_block( nullptr ); | 
|---|
|  | 555 | delete finally; | 
|---|
| [ba912706] | 556 | tryStmt->set_finally( nullptr ); | 
|---|
|  | 557 |  | 
|---|
|  | 558 | return new FunctionDecl("finally", Type::StorageClasses(), | 
|---|
| [307a732] | 559 | LinkageSpec::Cforall, finally_func_t.clone(), body); | 
|---|
| [ba912706] | 560 | } | 
|---|
|  | 561 |  | 
|---|
| [046a890] | 562 | ObjectDecl * TryMutatorCore::create_finally_hook( | 
|---|
| [948b0c8] | 563 | FunctionDecl * finally_wrapper ) { | 
|---|
| [3090127] | 564 | // struct __cfaehm_cleanup_hook __finally_hook | 
|---|
| [046a890] | 565 | //      __attribute__((cleanup( `finally_wrapper` ))); | 
|---|
| [ba912706] | 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 ) ); | 
|---|
| [288eede] | 572 | attributes.push_back( new Attribute( "cleanup", attr_params ) ); | 
|---|
| [ba912706] | 573 | } | 
|---|
|  | 574 |  | 
|---|
| [288eede] | 575 | return new ObjectDecl( | 
|---|
|  | 576 | "__finally_hook", | 
|---|
| [ba912706] | 577 | Type::StorageClasses(), | 
|---|
|  | 578 | LinkageSpec::Cforall, | 
|---|
|  | 579 | nullptr, | 
|---|
| [288eede] | 580 | new StructInstType( | 
|---|
| [ac10576] | 581 | noQualifiers, | 
|---|
| [288eede] | 582 | hook_decl | 
|---|
|  | 583 | ), | 
|---|
| [ba912706] | 584 | nullptr, | 
|---|
|  | 585 | attributes | 
|---|
|  | 586 | ); | 
|---|
|  | 587 | } | 
|---|
|  | 588 |  | 
|---|
| [7f9968ad] | 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 |  | 
|---|
| [948b0c8] | 599 | // Visiting/Mutating Functions | 
|---|
| [046a890] | 600 | void TryMutatorCore::premutate( StructDecl *structDecl ) { | 
|---|
| [86d5ba7c] | 601 | if ( !structDecl->has_body() ) { | 
|---|
|  | 602 | // Skip children? | 
|---|
|  | 603 | return; | 
|---|
| [3090127] | 604 | } else if ( structDecl->get_name() == "__cfaehm_base_exception_t" ) { | 
|---|
| [cbce272] | 605 | assert( nullptr == except_decl ); | 
|---|
|  | 606 | except_decl = structDecl; | 
|---|
|  | 607 | init_func_types(); | 
|---|
| [3090127] | 608 | } else if ( structDecl->get_name() == "__cfaehm_try_resume_node" ) { | 
|---|
| [86d5ba7c] | 609 | assert( nullptr == node_decl ); | 
|---|
|  | 610 | node_decl = structDecl; | 
|---|
| [3090127] | 611 | } else if ( structDecl->get_name() == "__cfaehm_cleanup_hook" ) { | 
|---|
| [86d5ba7c] | 612 | assert( nullptr == hook_decl ); | 
|---|
|  | 613 | hook_decl = structDecl; | 
|---|
|  | 614 | } | 
|---|
|  | 615 | } | 
|---|
|  | 616 |  | 
|---|
| [046a890] | 617 | Statement * TryMutatorCore::postmutate( TryStmt *tryStmt ) { | 
|---|
| [cbce272] | 618 | assert( except_decl ); | 
|---|
| [288eede] | 619 | assert( node_decl ); | 
|---|
|  | 620 | assert( hook_decl ); | 
|---|
|  | 621 |  | 
|---|
| [ba912706] | 622 | // Generate a prefix for the function names? | 
|---|
|  | 623 |  | 
|---|
| [ba3706f] | 624 | CompoundStmt * block = new CompoundStmt(); | 
|---|
| [288eede] | 625 | CompoundStmt * inner = take_try_block( tryStmt ); | 
|---|
| [ba912706] | 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. | 
|---|
| [948b0c8] | 633 | appendDeclStmt( block, create_finally_hook( finally_block ) ); | 
|---|
| [ba912706] | 634 | } | 
|---|
|  | 635 |  | 
|---|
| [288eede] | 636 | CatchList termination_handlers; | 
|---|
|  | 637 | CatchList resumption_handlers; | 
|---|
|  | 638 | split( tryStmt->get_catchers(), | 
|---|
| [1abc5ab] | 639 | termination_handlers, resumption_handlers ); | 
|---|
| [ba912706] | 640 |  | 
|---|
| [288eede] | 641 | if ( resumption_handlers.size() ) { | 
|---|
| [ba912706] | 642 | // Define the helper function. | 
|---|
|  | 643 | FunctionDecl * resume_handler = | 
|---|
|  | 644 | create_resume_handler( resumption_handlers ); | 
|---|
|  | 645 | appendDeclStmt( block, resume_handler ); | 
|---|
|  | 646 | // Prepare hooks | 
|---|
| [948b0c8] | 647 | inner = create_resume_wrapper( inner, resume_handler ); | 
|---|
| [ba912706] | 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 |  | 
|---|
| [7f9968ad] | 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 |  | 
|---|
| [046a890] | 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; | 
|---|
| [6fca7ea] | 684 | mutateAll( translationUnit, translator ); | 
|---|
| [ba912706] | 685 | } | 
|---|
|  | 686 | } | 
|---|