[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
|
---|
[288eede] | 12 | // Last Modified On : Thr Jun 29 15:18:00 2017
|
---|
| 13 | // Update Count : 1
|
---|
[ba912706] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "ExceptTranslate.h"
|
---|
| 17 | #include "Common/PassVisitor.h"
|
---|
[288eede] | 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"
|
---|
[ba912706] | 23 |
|
---|
| 24 | namespace ControlFlow {
|
---|
| 25 |
|
---|
| 26 | // This (large) section could probably be moved out of the class
|
---|
| 27 | // and be static helpers instead.
|
---|
| 28 |
|
---|
| 29 | // Type(Qualifiers &, false, std::list<Attribute *> &)
|
---|
| 30 |
|
---|
| 31 | // void (*function)()
|
---|
| 32 | static FunctionType void_func_t(Type::Qualifiers(), false);
|
---|
| 33 | // void (*function)(int, exception);
|
---|
| 34 | static FunctionType catch_func_t(Type::Qualifiers(), false);
|
---|
| 35 | // int (*function)(exception);
|
---|
| 36 | static FunctionType match_func_t(Type::Qualifiers(), false);
|
---|
| 37 | // bool (*function)(exception);
|
---|
| 38 | static FunctionType handle_func_t(Type::Qualifiers(), false);
|
---|
| 39 |
|
---|
| 40 | static void init_func_types() {
|
---|
[288eede] | 41 | static bool init_complete = false;
|
---|
[ba912706] | 42 | if (init_complete) {
|
---|
| 43 | return;
|
---|
| 44 | }
|
---|
| 45 | ObjectDecl index_obj(
|
---|
[288eede] | 46 | "__handler_index",
|
---|
[ba912706] | 47 | Type::StorageClasses(),
|
---|
| 48 | LinkageSpec::Cforall,
|
---|
| 49 | /*bitfieldWidth*/ NULL,
|
---|
| 50 | new BasicType(emptyQualifiers, BasicType::UnsignedInt),
|
---|
| 51 | /*init*/ NULL
|
---|
| 52 | );
|
---|
| 53 | ObjectDecl exception_obj(
|
---|
[288eede] | 54 | "__exception_inst",
|
---|
[ba912706] | 55 | Type::StorageClasses(),
|
---|
| 56 | LinkageSpec::Cforall,
|
---|
| 57 | /*bitfieldWidth*/ NULL,
|
---|
| 58 | new BasicType(emptyQualifiers, BasicType::UnsignedInt),
|
---|
| 59 | /*init*/ NULL
|
---|
| 60 | );
|
---|
| 61 | ObjectDecl bool_obj(
|
---|
[288eede] | 62 | "__ret_bool",
|
---|
[ba912706] | 63 | Type::StorageClasses(),
|
---|
| 64 | LinkageSpec::Cforall,
|
---|
| 65 | /*bitfieldWidth*/ NULL,
|
---|
| 66 | new BasicType(emptyQualifiers, BasicType::Bool),
|
---|
| 67 | /*init*/ NULL
|
---|
| 68 | );
|
---|
| 69 |
|
---|
| 70 | catch_func_t.get_parameters().push_back(index_obj.clone());
|
---|
| 71 | catch_func_t.get_parameters().push_back(exception_obj.clone());
|
---|
| 72 | match_func_t.get_returnVals().push_back(index_obj.clone());
|
---|
| 73 | match_func_t.get_parameters().push_back(exception_obj.clone());
|
---|
| 74 | handle_func_t.get_returnVals().push_back(bool_obj.clone());
|
---|
| 75 | handle_func_t.get_parameters().push_back(exception_obj.clone());
|
---|
| 76 |
|
---|
| 77 | init_complete = true;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | // Buricratic Helpers (Not having to do with the paritular operation.)
|
---|
| 81 |
|
---|
| 82 | typedef std::list<CatchStmt*> CatchList;
|
---|
| 83 |
|
---|
| 84 | void split( CatchList& allHandlers, CatchList& terHandlers,
|
---|
[1abc5ab] | 85 | CatchList& resHandlers ) {
|
---|
[ba912706] | 86 | while ( !allHandlers.empty() ) {
|
---|
[288eede] | 87 | CatchStmt * stmt = allHandlers.front();
|
---|
[ba912706] | 88 | allHandlers.pop_front();
|
---|
[288eede] | 89 | if (CatchStmt::Terminate == stmt->get_kind()) {
|
---|
[ba912706] | 90 | terHandlers.push_back(stmt);
|
---|
| 91 | } else {
|
---|
| 92 | resHandlers.push_back(stmt);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | template<typename T>
|
---|
| 98 | void free_all( std::list<T *> &list ) {
|
---|
[288eede] | 99 | typename std::list<T *>::iterator it;
|
---|
[ba912706] | 100 | for ( it = list.begin() ; it != list.end() ; ++it ) {
|
---|
| 101 | delete *it;
|
---|
| 102 | }
|
---|
| 103 | list.clear();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void appendDeclStmt( CompoundStmt * block, Declaration * item ) {
|
---|
[288eede] | 107 | block->push_back(new DeclStmt(noLabels, item));
|
---|
[ba912706] | 108 | }
|
---|
| 109 |
|
---|
[288eede] | 110 | Expression * nameOf( DeclarationWithType * decl ) {
|
---|
| 111 | return new VariableExpr( decl );
|
---|
[ba912706] | 112 | }
|
---|
| 113 |
|
---|
| 114 | // ThrowStmt Mutation Helpers
|
---|
| 115 |
|
---|
| 116 | Statement * create_terminate_throw( ThrowStmt *throwStmt ) {
|
---|
| 117 | // __throw_terminate( EXPR );
|
---|
[288eede] | 118 | UntypedExpr * call = new UntypedExpr( new NameExpr(
|
---|
| 119 | "__cfaehm__throw_termination" ) );
|
---|
| 120 | call->get_args().push_back( throwStmt->get_expr() );
|
---|
[ba912706] | 121 | Statement * result = new ExprStmt( throwStmt->get_labels(), call );
|
---|
| 122 | throwStmt->set_expr( nullptr );
|
---|
| 123 | delete throwStmt;
|
---|
| 124 | return result;
|
---|
| 125 | }
|
---|
| 126 | Statement * create_terminate_rethrow( ThrowStmt *throwStmt ) {
|
---|
| 127 | // __rethrow_terminate();
|
---|
[288eede] | 128 | assert( nullptr == throwStmt->get_expr() );
|
---|
[ba912706] | 129 | Statement * result = new ExprStmt(
|
---|
| 130 | throwStmt->get_labels(),
|
---|
[288eede] | 131 | new UntypedExpr( new NameExpr( "__cfaehm__rethrow_termination" ) )
|
---|
[ba912706] | 132 | );
|
---|
| 133 | delete throwStmt;
|
---|
| 134 | return result;
|
---|
| 135 | }
|
---|
| 136 | Statement * create_resume_throw( ThrowStmt *throwStmt ) {
|
---|
| 137 | // __throw_resume( EXPR );
|
---|
[288eede] | 138 | UntypedExpr * call = new UntypedExpr( new NameExpr(
|
---|
| 139 | "__cfaehm__throw_resumption" ) );
|
---|
| 140 | call->get_args().push_back( throwStmt->get_expr() );
|
---|
[ba912706] | 141 | Statement * result = new ExprStmt( throwStmt->get_labels(), call );
|
---|
| 142 | throwStmt->set_expr( nullptr );
|
---|
| 143 | delete throwStmt;
|
---|
| 144 | return result;
|
---|
| 145 | }
|
---|
| 146 | Statement * create_resume_rethrow( ThrowStmt *throwStmt ) {
|
---|
| 147 | // return false;
|
---|
| 148 | Statement * result = new ReturnStmt(
|
---|
| 149 | throwStmt->get_labels(),
|
---|
[288eede] | 150 | new ConstantExpr( Constant::from_bool( false ) )
|
---|
[ba912706] | 151 | );
|
---|
| 152 | delete throwStmt;
|
---|
| 153 | return result;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | // TryStmt Mutation Helpers
|
---|
| 157 |
|
---|
| 158 | CompoundStmt * take_try_block( TryStmt *tryStmt ) {
|
---|
| 159 | CompoundStmt * block = tryStmt->get_block();
|
---|
| 160 | tryStmt->set_block( nullptr );
|
---|
| 161 | return block;
|
---|
| 162 | }
|
---|
[288eede] | 163 | FunctionDecl * create_try_wrapper( CompoundStmt *body ) {
|
---|
[ba912706] | 164 |
|
---|
[288eede] | 165 | return new FunctionDecl( "try", Type::StorageClasses(),
|
---|
| 166 | LinkageSpec::Cforall, void_func_t.clone(), body );
|
---|
[ba912706] | 167 | }
|
---|
| 168 |
|
---|
| 169 | FunctionDecl * create_terminate_catch( CatchList &handlers ) {
|
---|
| 170 | std::list<CaseStmt *> handler_wrappers;
|
---|
| 171 |
|
---|
[288eede] | 172 | FunctionType *func_type = catch_func_t.clone();
|
---|
| 173 | DeclarationWithType * index_obj = func_type->get_parameters().front();
|
---|
| 174 | // DeclarationWithType * except_obj = func_type->get_parameters().back();
|
---|
| 175 |
|
---|
[ba912706] | 176 | // Index 1..{number of handlers}
|
---|
| 177 | int index = 0;
|
---|
| 178 | CatchList::iterator it = handlers.begin();
|
---|
| 179 | for ( ; it != handlers.end() ; ++it ) {
|
---|
| 180 | ++index;
|
---|
| 181 | CatchStmt * handler = *it;
|
---|
| 182 |
|
---|
[288eede] | 183 | // INTEGERconstant Version
|
---|
| 184 | // case `index`:
|
---|
| 185 | // {
|
---|
| 186 | // `handler.body`
|
---|
| 187 | // }
|
---|
| 188 | // return;
|
---|
| 189 | std::list<Statement *> caseBody;
|
---|
| 190 | caseBody.push_back( handler->get_body() );
|
---|
| 191 | handler->set_body( nullptr );
|
---|
| 192 | caseBody.push_back( new ReturnStmt( noLabels, nullptr ) );
|
---|
| 193 |
|
---|
| 194 | handler_wrappers.push_back( new CaseStmt(
|
---|
[ba912706] | 195 | noLabels,
|
---|
| 196 | new ConstantExpr( Constant::from_int( index ) ),
|
---|
[288eede] | 197 | caseBody
|
---|
| 198 | ) );
|
---|
[ba912706] | 199 | }
|
---|
| 200 | // TODO: Some sort of meaningful error on default perhaps?
|
---|
| 201 |
|
---|
[288eede] | 202 | std::list<Statement*> stmt_handlers;
|
---|
| 203 | while ( !handler_wrappers.empty() ) {
|
---|
| 204 | stmt_handlers.push_back( handler_wrappers.front() );
|
---|
| 205 | handler_wrappers.pop_front();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[ba912706] | 208 | SwitchStmt * handler_lookup = new SwitchStmt(
|
---|
| 209 | noLabels,
|
---|
[288eede] | 210 | nameOf( index_obj ),
|
---|
| 211 | stmt_handlers
|
---|
[ba912706] | 212 | );
|
---|
| 213 | CompoundStmt * body = new CompoundStmt( noLabels );
|
---|
| 214 | body->push_back( handler_lookup );
|
---|
| 215 |
|
---|
| 216 | return new FunctionDecl("catch", Type::StorageClasses(),
|
---|
[288eede] | 217 | LinkageSpec::Cforall, func_type, body);
|
---|
[ba912706] | 218 | }
|
---|
| 219 |
|
---|
| 220 | // Create a single check from a moddified handler.
|
---|
[288eede] | 221 | // except_obj is referenced, modded_handler will be freed.
|
---|
| 222 | CompoundStmt *create_single_matcher(
|
---|
| 223 | DeclarationWithType * except_obj, CatchStmt * modded_handler ) {
|
---|
| 224 | CompoundStmt * block = new CompoundStmt( noLabels );
|
---|
| 225 |
|
---|
| 226 | // INTEGERconstant Version
|
---|
| 227 | assert( nullptr == modded_handler->get_decl() );
|
---|
| 228 | ConstantExpr * number =
|
---|
| 229 | dynamic_cast<ConstantExpr*>( modded_handler->get_cond() );
|
---|
| 230 | assert( number );
|
---|
| 231 | modded_handler->set_cond( nullptr );
|
---|
[ba912706] | 232 |
|
---|
[288eede] | 233 | Expression * cond;
|
---|
| 234 | {
|
---|
| 235 | std::list<Expression *> args;
|
---|
| 236 | args.push_back( number );
|
---|
| 237 | args.push_back( nameOf( except_obj ) );
|
---|
| 238 | cond = new UntypedExpr( new NameExpr( "?==?" /*???*/), args );
|
---|
| 239 | }
|
---|
[ba912706] | 240 |
|
---|
| 241 | if ( modded_handler->get_cond() ) {
|
---|
[288eede] | 242 | cond = new LogicalExpr( cond, modded_handler->get_cond() );
|
---|
[ba912706] | 243 | }
|
---|
| 244 | block->push_back( new IfStmt( noLabels,
|
---|
[288eede] | 245 | cond, modded_handler->get_body(), nullptr ) );
|
---|
[ba912706] | 246 |
|
---|
| 247 | modded_handler->set_decl( nullptr );
|
---|
| 248 | modded_handler->set_cond( nullptr );
|
---|
| 249 | modded_handler->set_body( nullptr );
|
---|
| 250 | delete modded_handler;
|
---|
| 251 | return block;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | FunctionDecl * create_terminate_match( CatchList &handlers ) {
|
---|
| 255 | CompoundStmt * body = new CompoundStmt( noLabels );
|
---|
| 256 |
|
---|
[288eede] | 257 | FunctionType * func_type = match_func_t.clone();
|
---|
| 258 | DeclarationWithType * except_obj = func_type->get_parameters().back();
|
---|
| 259 |
|
---|
[ba912706] | 260 | // Index 1..{number of handlers}
|
---|
| 261 | int index = 0;
|
---|
| 262 | CatchList::iterator it;
|
---|
| 263 | for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
|
---|
| 264 | ++index;
|
---|
| 265 | CatchStmt * handler = *it;
|
---|
| 266 |
|
---|
[288eede] | 267 | // Body should have been taken by create_terminate_catch.
|
---|
| 268 | assert( nullptr == handler->get_body() );
|
---|
| 269 |
|
---|
| 270 | // Create new body.
|
---|
[ba912706] | 271 | handler->set_body( new ReturnStmt( noLabels,
|
---|
| 272 | new ConstantExpr( Constant::from_int( index ) ) ) );
|
---|
| 273 |
|
---|
[288eede] | 274 | // Create the handler.
|
---|
| 275 | body->push_back( create_single_matcher( except_obj, handler ) );
|
---|
| 276 | *it = nullptr;
|
---|
[ba912706] | 277 | }
|
---|
| 278 |
|
---|
| 279 | return new FunctionDecl("match", Type::StorageClasses(),
|
---|
[288eede] | 280 | LinkageSpec::Cforall, func_type, body);
|
---|
[ba912706] | 281 | }
|
---|
| 282 |
|
---|
[288eede] | 283 | CompoundStmt * create_terminate_caller(
|
---|
[ba912706] | 284 | FunctionDecl * try_wrapper,
|
---|
| 285 | FunctionDecl * terminate_catch,
|
---|
| 286 | FunctionDecl * terminate_match) {
|
---|
| 287 |
|
---|
[288eede] | 288 | UntypedExpr * caller = new UntypedExpr( new NameExpr(
|
---|
| 289 | "__cfaehm__try_terminate" ) );
|
---|
| 290 | std::list<Expression *>& args = caller->get_args();
|
---|
[ba912706] | 291 | args.push_back( nameOf( try_wrapper ) );
|
---|
| 292 | args.push_back( nameOf( terminate_catch ) );
|
---|
| 293 | args.push_back( nameOf( terminate_match ) );
|
---|
| 294 |
|
---|
[288eede] | 295 | CompoundStmt * callStmt = new CompoundStmt( noLabels );
|
---|
| 296 | callStmt->push_back( new ExprStmt( noLabels, caller ) );
|
---|
| 297 | return callStmt;
|
---|
[ba912706] | 298 | }
|
---|
| 299 |
|
---|
| 300 | FunctionDecl * create_resume_handler( CatchList &handlers ) {
|
---|
[288eede] | 301 | CompoundStmt * body = new CompoundStmt( noLabels );
|
---|
| 302 |
|
---|
| 303 | FunctionType * func_type = match_func_t.clone();
|
---|
| 304 | DeclarationWithType * except_obj = func_type->get_parameters().back();
|
---|
[ba912706] | 305 |
|
---|
| 306 | CatchList::iterator it;
|
---|
| 307 | for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
|
---|
| 308 | CatchStmt * handler = *it;
|
---|
| 309 |
|
---|
| 310 | // Modifiy body.
|
---|
| 311 | CompoundStmt * handling_code =
|
---|
| 312 | dynamic_cast<CompoundStmt*>( handler->get_body() );
|
---|
| 313 | if ( ! handling_code ) {
|
---|
| 314 | handling_code = new CompoundStmt( noLabels );
|
---|
| 315 | handling_code->push_back( handler->get_body() );
|
---|
| 316 | }
|
---|
[288eede] | 317 | handling_code->push_back( new ReturnStmt( noLabels,
|
---|
[ba912706] | 318 | new ConstantExpr( Constant::from_bool( false ) ) ) );
|
---|
| 319 | handler->set_body( handling_code );
|
---|
| 320 |
|
---|
| 321 | // Create the handler.
|
---|
[288eede] | 322 | body->push_back( create_single_matcher( except_obj, handler ) );
|
---|
| 323 | *it = nullptr;
|
---|
[ba912706] | 324 | }
|
---|
| 325 |
|
---|
| 326 | return new FunctionDecl("handle", Type::StorageClasses(),
|
---|
[288eede] | 327 | LinkageSpec::Cforall, func_type, body);
|
---|
[ba912706] | 328 | }
|
---|
| 329 |
|
---|
[288eede] | 330 | CompoundStmt * create_resume_wrapper(
|
---|
| 331 | StructDecl * node_decl,
|
---|
[ba912706] | 332 | Statement * wraps,
|
---|
| 333 | FunctionDecl * resume_handler ) {
|
---|
| 334 | CompoundStmt * body = new CompoundStmt( noLabels );
|
---|
| 335 |
|
---|
[288eede] | 336 | // struct __try_resume_node __resume_node
|
---|
| 337 | // __attribute__((cleanup( __cfaehm__try_resume_cleanup )));
|
---|
| 338 | // ** unwinding of the stack here could cause problems **
|
---|
| 339 | // ** however I don't think that can happen currently **
|
---|
| 340 | // __cfaehm__try_resume_setup( &__resume_node, resume_handler );
|
---|
[ba912706] | 341 |
|
---|
| 342 | std::list< Attribute * > attributes;
|
---|
| 343 | {
|
---|
| 344 | std::list< Expression * > attr_params;
|
---|
[288eede] | 345 | attr_params.push_back( new NameExpr(
|
---|
| 346 | "__cfaehm__try_resume_cleanup" ) );
|
---|
| 347 | attributes.push_back( new Attribute( "cleanup", attr_params ) );
|
---|
[ba912706] | 348 | }
|
---|
| 349 |
|
---|
[288eede] | 350 | ObjectDecl * obj = new ObjectDecl(
|
---|
| 351 | "__resume_node",
|
---|
[ba912706] | 352 | Type::StorageClasses(),
|
---|
| 353 | LinkageSpec::Cforall,
|
---|
| 354 | nullptr,
|
---|
[288eede] | 355 | new StructInstType(
|
---|
| 356 | Type::Qualifiers(),
|
---|
| 357 | node_decl
|
---|
| 358 | ),
|
---|
| 359 | nullptr,
|
---|
[ba912706] | 360 | attributes
|
---|
[288eede] | 361 | );
|
---|
| 362 | appendDeclStmt( body, obj );
|
---|
| 363 |
|
---|
| 364 | UntypedExpr *setup = new UntypedExpr( new NameExpr(
|
---|
| 365 | "__cfaehm__try_resume_setup" ) );
|
---|
| 366 | setup->get_args().push_back( nameOf( obj ) );
|
---|
| 367 | setup->get_args().push_back( nameOf( resume_handler ) );
|
---|
| 368 |
|
---|
| 369 | body->push_back( new ExprStmt( noLabels, setup ) );
|
---|
| 370 |
|
---|
[ba912706] | 371 | body->push_back( wraps );
|
---|
| 372 | return body;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | FunctionDecl * create_finally_wrapper( TryStmt * tryStmt ) {
|
---|
[288eede] | 376 | FinallyStmt * finally = tryStmt->get_finally();
|
---|
| 377 | CompoundStmt * body = finally->get_block();
|
---|
| 378 | finally->set_block( nullptr );
|
---|
| 379 | delete finally;
|
---|
[ba912706] | 380 | tryStmt->set_finally( nullptr );
|
---|
| 381 |
|
---|
| 382 | return new FunctionDecl("finally", Type::StorageClasses(),
|
---|
[288eede] | 383 | LinkageSpec::Cforall, void_func_t.clone(), body);
|
---|
[ba912706] | 384 | }
|
---|
| 385 |
|
---|
[288eede] | 386 | ObjectDecl * create_finally_hook(
|
---|
| 387 | StructDecl * hook_decl, FunctionDecl * finally_wrapper ) {
|
---|
| 388 | // struct __cfaehm__cleanup_hook __finally_hook
|
---|
| 389 | // __attribute__((cleanup( finally_wrapper )));
|
---|
[ba912706] | 390 |
|
---|
| 391 | // Make Cleanup Attribute.
|
---|
| 392 | std::list< Attribute * > attributes;
|
---|
| 393 | {
|
---|
| 394 | std::list< Expression * > attr_params;
|
---|
| 395 | attr_params.push_back( nameOf( finally_wrapper ) );
|
---|
[288eede] | 396 | attributes.push_back( new Attribute( "cleanup", attr_params ) );
|
---|
[ba912706] | 397 | }
|
---|
| 398 |
|
---|
[288eede] | 399 | return new ObjectDecl(
|
---|
| 400 | "__finally_hook",
|
---|
[ba912706] | 401 | Type::StorageClasses(),
|
---|
| 402 | LinkageSpec::Cforall,
|
---|
| 403 | nullptr,
|
---|
[288eede] | 404 | new StructInstType(
|
---|
| 405 | emptyQualifiers,
|
---|
| 406 | hook_decl
|
---|
| 407 | ),
|
---|
[ba912706] | 408 | nullptr,
|
---|
| 409 | attributes
|
---|
| 410 | );
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 |
|
---|
[1abc5ab] | 414 | class ExceptionMutatorCore : public WithGuards {
|
---|
[ba912706] | 415 | enum Context { NoHandler, TerHandler, ResHandler };
|
---|
| 416 |
|
---|
| 417 | // Also need to handle goto, break & continue.
|
---|
| 418 | // They need to be cut off in a ResHandler, until we enter another
|
---|
| 419 | // loop, switch or the goto stays within the function.
|
---|
| 420 |
|
---|
[288eede] | 421 | Context cur_context;
|
---|
[ba912706] | 422 |
|
---|
| 423 | // We might not need this, but a unique base for each try block's
|
---|
| 424 | // generated functions might be nice.
|
---|
| 425 | //std::string curFunctionName;
|
---|
| 426 | //unsigned int try_count = 0;
|
---|
| 427 |
|
---|
[288eede] | 428 | StructDecl *node_decl;
|
---|
| 429 | StructDecl *hook_decl;
|
---|
[ba912706] | 430 |
|
---|
| 431 | public:
|
---|
| 432 | ExceptionMutatorCore() :
|
---|
[288eede] | 433 | cur_context(NoHandler),
|
---|
| 434 | node_decl(nullptr), hook_decl(nullptr)
|
---|
[ba912706] | 435 | {}
|
---|
| 436 |
|
---|
[288eede] | 437 | void premutate( CatchStmt *catchStmt );
|
---|
| 438 | void premutate( StructDecl *structDecl );
|
---|
[ba912706] | 439 | Statement * postmutate( ThrowStmt *throwStmt );
|
---|
| 440 | Statement * postmutate( TryStmt *tryStmt );
|
---|
| 441 | };
|
---|
| 442 |
|
---|
| 443 | Statement * ExceptionMutatorCore::postmutate( ThrowStmt *throwStmt ) {
|
---|
| 444 | // Ignoring throwStmt->get_target() for now.
|
---|
| 445 | if ( ThrowStmt::Terminate == throwStmt->get_kind() ) {
|
---|
| 446 | if ( throwStmt->get_expr() ) {
|
---|
| 447 | return create_terminate_throw( throwStmt );
|
---|
[288eede] | 448 | } else if ( TerHandler == cur_context ) {
|
---|
[ba912706] | 449 | return create_terminate_rethrow( throwStmt );
|
---|
| 450 | } else {
|
---|
| 451 | assertf(false, "Invalid throw in %s at %i\n",
|
---|
[288eede] | 452 | throwStmt->location.filename.c_str(),
|
---|
[ba912706] | 453 | throwStmt->location.linenumber);
|
---|
| 454 | return nullptr;
|
---|
| 455 | }
|
---|
| 456 | } else {
|
---|
| 457 | if ( throwStmt->get_expr() ) {
|
---|
| 458 | return create_resume_throw( throwStmt );
|
---|
[288eede] | 459 | } else if ( ResHandler == cur_context ) {
|
---|
[ba912706] | 460 | return create_resume_rethrow( throwStmt );
|
---|
| 461 | } else {
|
---|
| 462 | assertf(false, "Invalid throwResume in %s at %i\n",
|
---|
[288eede] | 463 | throwStmt->location.filename.c_str(),
|
---|
[ba912706] | 464 | throwStmt->location.linenumber);
|
---|
| 465 | return nullptr;
|
---|
| 466 | }
|
---|
| 467 | }
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | Statement * ExceptionMutatorCore::postmutate( TryStmt *tryStmt ) {
|
---|
[288eede] | 471 | assert( node_decl );
|
---|
| 472 | assert( hook_decl );
|
---|
| 473 |
|
---|
[ba912706] | 474 | // Generate a prefix for the function names?
|
---|
| 475 |
|
---|
[288eede] | 476 | CompoundStmt * block = new CompoundStmt( noLabels );
|
---|
| 477 | CompoundStmt * inner = take_try_block( tryStmt );
|
---|
[ba912706] | 478 |
|
---|
| 479 | if ( tryStmt->get_finally() ) {
|
---|
| 480 | // Define the helper function.
|
---|
| 481 | FunctionDecl * finally_block =
|
---|
| 482 | create_finally_wrapper( tryStmt );
|
---|
| 483 | appendDeclStmt( block, finally_block );
|
---|
| 484 | // Create and add the finally cleanup hook.
|
---|
[288eede] | 485 | appendDeclStmt( block,
|
---|
| 486 | create_finally_hook( hook_decl, finally_block ) );
|
---|
[ba912706] | 487 | }
|
---|
| 488 |
|
---|
[288eede] | 489 | CatchList termination_handlers;
|
---|
| 490 | CatchList resumption_handlers;
|
---|
| 491 | split( tryStmt->get_catchers(),
|
---|
[1abc5ab] | 492 | termination_handlers, resumption_handlers );
|
---|
[ba912706] | 493 |
|
---|
[288eede] | 494 | if ( resumption_handlers.size() ) {
|
---|
[ba912706] | 495 | // Define the helper function.
|
---|
| 496 | FunctionDecl * resume_handler =
|
---|
| 497 | create_resume_handler( resumption_handlers );
|
---|
| 498 | appendDeclStmt( block, resume_handler );
|
---|
| 499 | // Prepare hooks
|
---|
[288eede] | 500 | inner = create_resume_wrapper( node_decl, inner, resume_handler );
|
---|
[ba912706] | 501 | }
|
---|
| 502 |
|
---|
| 503 | if ( termination_handlers.size() ) {
|
---|
| 504 | // Define the three helper functions.
|
---|
| 505 | FunctionDecl * try_wrapper = create_try_wrapper( inner );
|
---|
| 506 | appendDeclStmt( block, try_wrapper );
|
---|
| 507 | FunctionDecl * terminate_catch =
|
---|
| 508 | create_terminate_catch( termination_handlers );
|
---|
| 509 | appendDeclStmt( block, terminate_catch );
|
---|
| 510 | FunctionDecl * terminate_match =
|
---|
| 511 | create_terminate_match( termination_handlers );
|
---|
| 512 | appendDeclStmt( block, terminate_match );
|
---|
| 513 | // Build the call to the try wrapper.
|
---|
| 514 | inner = create_terminate_caller(
|
---|
| 515 | try_wrapper, terminate_catch, terminate_match );
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | // Embed the try block.
|
---|
| 519 | block->push_back( inner );
|
---|
| 520 |
|
---|
[288eede] | 521 | //free_all( termination_handlers );
|
---|
| 522 | //free_all( resumption_handlers );
|
---|
[ba912706] | 523 |
|
---|
| 524 | return block;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | void ExceptionMutatorCore::premutate( CatchStmt *catchStmt ) {
|
---|
[288eede] | 528 | GuardValue( cur_context );
|
---|
| 529 | if ( CatchStmt::Terminate == catchStmt->get_kind() ) {
|
---|
| 530 | cur_context = TerHandler;
|
---|
[ba912706] | 531 | } else {
|
---|
[288eede] | 532 | cur_context = ResHandler;
|
---|
[ba912706] | 533 | }
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[288eede] | 536 | void ExceptionMutatorCore::premutate( StructDecl *structDecl ) {
|
---|
| 537 | if ( !structDecl->has_body() ) {
|
---|
| 538 | // Skip children?
|
---|
| 539 | return;
|
---|
| 540 | } else if ( structDecl->get_name() == "__cfaehm__try_resume_node" ) {
|
---|
| 541 | assert( nullptr == node_decl );
|
---|
| 542 | node_decl = structDecl;
|
---|
| 543 | } else if ( structDecl->get_name() == "__cfaehm__cleanup_hook" ) {
|
---|
| 544 | assert( nullptr == hook_decl );
|
---|
| 545 | hook_decl = structDecl;
|
---|
| 546 | }
|
---|
| 547 | // Later we might get the exception type as well.
|
---|
| 548 | }
|
---|
| 549 |
|
---|
[1abc5ab] | 550 | void translateEHM( std::list< Declaration *> & translationUnit ) {
|
---|
[288eede] | 551 | init_func_types();
|
---|
| 552 |
|
---|
[ba912706] | 553 | PassVisitor<ExceptionMutatorCore> translator;
|
---|
| 554 | for ( Declaration * decl : translationUnit ) {
|
---|
[288eede] | 555 | decl->acceptMutator( translator );
|
---|
[ba912706] | 556 | }
|
---|
| 557 | }
|
---|
| 558 | }
|
---|