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