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