[5ee153d] | 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 | // ExceptTranslateNew.cpp -- Conversion of exception control flow structures. |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Mon Nov 8 11:53:00 2021 |
---|
[33b7d49] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Fri Mar 11 17:51:00 2022 |
---|
| 13 | // Update Count : 2 |
---|
[5ee153d] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "ExceptTranslate.h" |
---|
| 17 | |
---|
| 18 | #include "AST/Expr.hpp" |
---|
| 19 | #include "AST/Pass.hpp" |
---|
| 20 | #include "AST/Stmt.hpp" |
---|
| 21 | #include "AST/TranslationUnit.hpp" |
---|
[5f3ba11] | 22 | #include "AST/DeclReplacer.hpp" |
---|
[5ee153d] | 23 | |
---|
| 24 | namespace ControlStruct { |
---|
| 25 | |
---|
| 26 | namespace { |
---|
| 27 | |
---|
[400b8be] | 28 | typedef std::list<ast::CatchClause*> CatchList; |
---|
[5f3ba11] | 29 | |
---|
| 30 | void appendDeclStmt( ast::CompoundStmt * block, ast::DeclWithType * item ) { |
---|
| 31 | block->push_back(new ast::DeclStmt(block->location, item)); |
---|
| 32 | } |
---|
| 33 | |
---|
[c36814a] | 34 | class TranslateThrowsCore final : public ast::WithGuards { |
---|
[5ee153d] | 35 | const ast::ObjectDecl * terminateHandlerExcept; |
---|
| 36 | enum Context { NoHandler, TerHandler, ResHandler } currentContext; |
---|
| 37 | |
---|
| 38 | const ast::Stmt * createEitherThrow( |
---|
| 39 | const ast::ThrowStmt * throwStmt, const char * funcName ); |
---|
| 40 | const ast::Stmt * createTerminateRethrow( const ast::ThrowStmt * ); |
---|
| 41 | |
---|
| 42 | public: |
---|
| 43 | TranslateThrowsCore() : |
---|
| 44 | terminateHandlerExcept( nullptr ), currentContext( NoHandler ) |
---|
| 45 | {} |
---|
| 46 | |
---|
[400b8be] | 47 | void previsit( const ast::CatchClause * stmt ); |
---|
[5ee153d] | 48 | const ast::Stmt * postvisit( const ast::ThrowStmt * stmt ); |
---|
| 49 | }; |
---|
| 50 | |
---|
| 51 | const ast::Stmt * TranslateThrowsCore::createEitherThrow( |
---|
| 52 | const ast::ThrowStmt * throwStmt, const char * funcName ) { |
---|
| 53 | // `throwFunc`( `throwStmt->name` ); |
---|
| 54 | ast::UntypedExpr * call = new ast::UntypedExpr( throwStmt->location, |
---|
| 55 | new ast::NameExpr( throwStmt->location, funcName ) |
---|
| 56 | ); |
---|
| 57 | call->args.push_back( throwStmt->expr ); |
---|
| 58 | return new ast::ExprStmt( throwStmt->location, call ); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | ast::VariableExpr * varOf( const ast::DeclWithType * decl ) { |
---|
| 62 | return new ast::VariableExpr( decl->location, decl ); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | const ast::Stmt * TranslateThrowsCore::createTerminateRethrow( |
---|
| 66 | const ast::ThrowStmt * stmt ) { |
---|
| 67 | // { `terminate_handler_except` = 0p; __rethrow_terminate(); } |
---|
| 68 | assert( nullptr == stmt->expr ); |
---|
| 69 | assert( terminateHandlerExcept ); |
---|
| 70 | |
---|
| 71 | ast::CompoundStmt * result = new ast::CompoundStmt( |
---|
| 72 | stmt->location, {}, std::vector<ast::Label>( stmt->labels ) ); |
---|
| 73 | result->push_back( new ast::ExprStmt( stmt->location, |
---|
| 74 | ast::UntypedExpr::createAssign( |
---|
| 75 | stmt->location, |
---|
| 76 | varOf( terminateHandlerExcept ), |
---|
| 77 | ast::ConstantExpr::null( |
---|
| 78 | stmt->location, |
---|
| 79 | terminateHandlerExcept->type |
---|
| 80 | ) |
---|
| 81 | ) |
---|
| 82 | ) ); |
---|
| 83 | result->push_back( new ast::ExprStmt( stmt->location, new ast::UntypedExpr( |
---|
| 84 | stmt->location, |
---|
| 85 | new ast::NameExpr( stmt->location, "__cfaehm_rethrow_terminate" ) |
---|
| 86 | ) ) ); |
---|
| 87 | return result; |
---|
| 88 | } |
---|
| 89 | |
---|
[400b8be] | 90 | void TranslateThrowsCore::previsit( const ast::CatchClause * stmt ) { |
---|
[5ee153d] | 91 | // Validate the statement's form. |
---|
| 92 | const ast::ObjectDecl * decl = stmt->decl.as<ast::ObjectDecl>(); |
---|
| 93 | // Also checking the type would be nice. |
---|
| 94 | if ( !decl || !decl->type.as<ast::PointerType>() ) { |
---|
| 95 | std::string kind = (ast::Terminate == stmt->kind) ? "catch" : "catchResume"; |
---|
| 96 | SemanticError( stmt->location, kind + " must have pointer to an exception type" ); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | // Track the handler context. |
---|
| 100 | if ( ast::Terminate == stmt->kind ) { |
---|
| 101 | GuardValue( currentContext ) = TerHandler; |
---|
| 102 | GuardValue( terminateHandlerExcept ) = decl; |
---|
| 103 | } else { |
---|
| 104 | GuardValue( currentContext ) = ResHandler; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | const ast::Stmt * TranslateThrowsCore::postvisit( |
---|
| 109 | const ast::ThrowStmt * stmt ) { |
---|
| 110 | // Ignoring ThrowStmt::target for now. |
---|
| 111 | // Handle Termination (Raise, Reraise, Error): |
---|
| 112 | if ( ast::Terminate == stmt->kind ) { |
---|
| 113 | if ( stmt->expr ) { |
---|
| 114 | return createEitherThrow( stmt, "$throw" ); |
---|
| 115 | } else if ( TerHandler == currentContext ) { |
---|
| 116 | return createTerminateRethrow( stmt ); |
---|
| 117 | } else { |
---|
| 118 | abort( "Invalid throw in %s at %i\n", |
---|
| 119 | stmt->location.filename.c_str(), |
---|
| 120 | stmt->location.first_line); |
---|
| 121 | } |
---|
| 122 | // Handle Resumption (Raise, Reraise, Error): |
---|
| 123 | } else { |
---|
| 124 | if ( stmt->expr ) { |
---|
| 125 | return createEitherThrow( stmt, "$throwResume" ); |
---|
| 126 | } else if ( ResHandler == currentContext ) { |
---|
| 127 | // This has to be handled later. |
---|
| 128 | return stmt; |
---|
| 129 | } else { |
---|
| 130 | abort( "Invalid throwResume in %s at %i\n", |
---|
| 131 | stmt->location.filename.c_str(), |
---|
| 132 | stmt->location.first_line); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
[5f3ba11] | 137 | |
---|
[c36814a] | 138 | class TryMutatorCore final { |
---|
[5f3ba11] | 139 | // The built in types used in translation. |
---|
| 140 | const ast::StructDecl * except_decl; |
---|
| 141 | const ast::StructDecl * node_decl; |
---|
| 142 | const ast::StructDecl * hook_decl; |
---|
| 143 | |
---|
| 144 | // The many helper functions for code/syntree generation. |
---|
| 145 | ast::CompoundStmt * take_try_block( ast::TryStmt * tryStmt ); |
---|
| 146 | ast::FunctionDecl * create_try_wrapper( const ast::CompoundStmt * body ); |
---|
| 147 | ast::FunctionDecl * create_terminate_catch( CatchList &handlers ); |
---|
| 148 | ast::CompoundStmt * create_single_matcher( |
---|
[400b8be] | 149 | const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler ); |
---|
[5f3ba11] | 150 | ast::FunctionDecl * create_terminate_match( CatchList &handlers ); |
---|
| 151 | ast::CompoundStmt * create_terminate_caller( CodeLocation loc, ast::FunctionDecl * try_wrapper, |
---|
| 152 | ast::FunctionDecl * terminate_catch, ast::FunctionDecl * terminate_match ); |
---|
| 153 | ast::FunctionDecl * create_resume_handler( CatchList &handlers ); |
---|
| 154 | ast::CompoundStmt * create_resume_wrapper( |
---|
| 155 | const ast::Stmt * wraps, const ast::FunctionDecl * resume_handler ); |
---|
| 156 | ast::FunctionDecl * create_finally_wrapper( ast::TryStmt * tryStmt ); |
---|
| 157 | ast::ObjectDecl * create_finally_hook( ast::FunctionDecl * finally_wrapper ); |
---|
| 158 | ast::Stmt * create_resume_rethrow( const ast::ThrowStmt * throwStmt ); |
---|
| 159 | |
---|
[33b7d49] | 160 | // Types used in translation, first group are internal. |
---|
| 161 | ast::ObjectDecl * make_index_object( CodeLocation const & ) const; |
---|
| 162 | ast::ObjectDecl * make_exception_object( CodeLocation const & ) const; |
---|
| 163 | ast::ObjectDecl * make_bool_object( CodeLocation const & ) const; |
---|
| 164 | ast::ObjectDecl * make_voidptr_object( CodeLocation const & ) const; |
---|
| 165 | ast::ObjectDecl * make_unused_index_object( CodeLocation const & ) const; |
---|
[5f3ba11] | 166 | // void (*function)(); |
---|
[33b7d49] | 167 | ast::FunctionDecl * make_try_function( CodeLocation const & ) const; |
---|
[5f3ba11] | 168 | // void (*function)(int, exception); |
---|
[33b7d49] | 169 | ast::FunctionDecl * make_catch_function( CodeLocation const & ) const; |
---|
[5f3ba11] | 170 | // int (*function)(exception); |
---|
[33b7d49] | 171 | ast::FunctionDecl * make_match_function( CodeLocation const & ) const; |
---|
[5f3ba11] | 172 | // bool (*function)(exception); |
---|
[33b7d49] | 173 | ast::FunctionDecl * make_handle_function( CodeLocation const & ) const; |
---|
[5f3ba11] | 174 | // void (*function)(__attribute__((unused)) void *); |
---|
[33b7d49] | 175 | ast::FunctionDecl * make_finally_function( CodeLocation const & ) const; |
---|
[5f3ba11] | 176 | |
---|
| 177 | public: |
---|
| 178 | TryMutatorCore() : |
---|
| 179 | except_decl( nullptr ), node_decl( nullptr ), hook_decl( nullptr ) |
---|
| 180 | {} |
---|
| 181 | |
---|
| 182 | void previsit( const ast::StructDecl *structDecl ); |
---|
| 183 | ast::Stmt * postvisit( const ast::TryStmt *tryStmt ); |
---|
| 184 | ast::Stmt * postvisit( const ast::ThrowStmt *throwStmt ); |
---|
| 185 | }; |
---|
| 186 | |
---|
[33b7d49] | 187 | ast::ObjectDecl * TryMutatorCore::make_index_object( |
---|
| 188 | CodeLocation const & location ) const { |
---|
| 189 | return new ast::ObjectDecl( |
---|
| 190 | location, |
---|
[5f3ba11] | 191 | "__handler_index", |
---|
[e8616b6] | 192 | new ast::BasicType( ast::BasicType::SignedInt ) |
---|
[5f3ba11] | 193 | ); |
---|
[33b7d49] | 194 | } |
---|
| 195 | |
---|
| 196 | ast::ObjectDecl * TryMutatorCore::make_exception_object( |
---|
| 197 | CodeLocation const & location ) const { |
---|
| 198 | assert( except_decl ); |
---|
| 199 | return new ast::ObjectDecl( |
---|
| 200 | location, |
---|
[5f3ba11] | 201 | "__exception_inst", |
---|
[e8616b6] | 202 | new ast::PointerType( new ast::StructInstType( except_decl ) ) |
---|
[5f3ba11] | 203 | ); |
---|
[33b7d49] | 204 | } |
---|
| 205 | |
---|
| 206 | ast::ObjectDecl * TryMutatorCore::make_bool_object( |
---|
| 207 | CodeLocation const & location ) const { |
---|
| 208 | return new ast::ObjectDecl( |
---|
| 209 | location, |
---|
[5f3ba11] | 210 | "__ret_bool", |
---|
| 211 | new ast::BasicType( ast::BasicType::Bool ), |
---|
| 212 | nullptr, //init |
---|
| 213 | ast::Storage::Classes{}, |
---|
| 214 | ast::Linkage::Cforall, |
---|
| 215 | nullptr, //width |
---|
| 216 | std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) } |
---|
| 217 | ); |
---|
[33b7d49] | 218 | } |
---|
| 219 | |
---|
| 220 | ast::ObjectDecl * TryMutatorCore::make_voidptr_object( |
---|
| 221 | CodeLocation const & location ) const { |
---|
| 222 | return new ast::ObjectDecl( |
---|
| 223 | location, |
---|
[5f3ba11] | 224 | "__hook", |
---|
| 225 | new ast::PointerType( |
---|
| 226 | new ast::VoidType() |
---|
| 227 | ), |
---|
| 228 | nullptr, //init |
---|
| 229 | ast::Storage::Classes{}, |
---|
| 230 | ast::Linkage::Cforall, |
---|
| 231 | nullptr, //width |
---|
| 232 | std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) } |
---|
| 233 | ); |
---|
[33b7d49] | 234 | } |
---|
[5f3ba11] | 235 | |
---|
[33b7d49] | 236 | ast::ObjectDecl * TryMutatorCore::make_unused_index_object( |
---|
| 237 | CodeLocation const & location ) const { |
---|
| 238 | return new ast::ObjectDecl( |
---|
| 239 | location, |
---|
[5f3ba11] | 240 | "__handler_index", |
---|
| 241 | new ast::BasicType(ast::BasicType::SignedInt), |
---|
| 242 | nullptr, |
---|
| 243 | ast::Storage::Classes{}, |
---|
| 244 | ast::Linkage::Cforall, |
---|
| 245 | nullptr, //width |
---|
| 246 | std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) } |
---|
| 247 | ); |
---|
[33b7d49] | 248 | } |
---|
[5f3ba11] | 249 | |
---|
[33b7d49] | 250 | ast::FunctionDecl * TryMutatorCore::make_try_function( |
---|
| 251 | CodeLocation const & location ) const { |
---|
| 252 | return new ast::FunctionDecl( |
---|
| 253 | location, |
---|
[5f3ba11] | 254 | "try", |
---|
| 255 | {}, //forall |
---|
| 256 | {}, //no param |
---|
| 257 | {}, //no return |
---|
| 258 | nullptr, |
---|
| 259 | ast::Storage::Classes{}, |
---|
| 260 | ast::Linkage::Cforall |
---|
| 261 | ); |
---|
[33b7d49] | 262 | } |
---|
[5f3ba11] | 263 | |
---|
[33b7d49] | 264 | ast::FunctionDecl * TryMutatorCore::make_catch_function( |
---|
| 265 | CodeLocation const & location ) const { |
---|
| 266 | return new ast::FunctionDecl( |
---|
| 267 | location, |
---|
[5f3ba11] | 268 | "catch", |
---|
| 269 | {}, //forall |
---|
[33b7d49] | 270 | { make_index_object( location ), make_exception_object( location ) }, |
---|
[5f3ba11] | 271 | {}, //return void |
---|
| 272 | nullptr, |
---|
| 273 | ast::Storage::Classes{}, |
---|
| 274 | ast::Linkage::Cforall |
---|
| 275 | ); |
---|
[33b7d49] | 276 | } |
---|
[5f3ba11] | 277 | |
---|
[33b7d49] | 278 | ast::FunctionDecl * TryMutatorCore::make_match_function( |
---|
| 279 | CodeLocation const & location ) const { |
---|
| 280 | return new ast::FunctionDecl( |
---|
| 281 | location, |
---|
[5f3ba11] | 282 | "match", |
---|
| 283 | {}, //forall |
---|
[33b7d49] | 284 | { make_exception_object( location ) }, |
---|
| 285 | { make_unused_index_object( location ) }, |
---|
[5f3ba11] | 286 | nullptr, |
---|
| 287 | ast::Storage::Classes{}, |
---|
| 288 | ast::Linkage::Cforall |
---|
| 289 | ); |
---|
[33b7d49] | 290 | } |
---|
[5f3ba11] | 291 | |
---|
[33b7d49] | 292 | ast::FunctionDecl * TryMutatorCore::make_handle_function( |
---|
| 293 | CodeLocation const & location ) const { |
---|
| 294 | return new ast::FunctionDecl( |
---|
| 295 | location, |
---|
[5f3ba11] | 296 | "handle", |
---|
| 297 | {}, //forall |
---|
[33b7d49] | 298 | { make_exception_object( location ) }, |
---|
| 299 | { make_bool_object( location ) }, |
---|
[5f3ba11] | 300 | nullptr, |
---|
| 301 | ast::Storage::Classes{}, |
---|
| 302 | ast::Linkage::Cforall |
---|
| 303 | ); |
---|
[33b7d49] | 304 | } |
---|
[5f3ba11] | 305 | |
---|
[33b7d49] | 306 | ast::FunctionDecl * TryMutatorCore::make_finally_function( |
---|
| 307 | CodeLocation const & location ) const { |
---|
| 308 | return new ast::FunctionDecl( |
---|
| 309 | location, |
---|
[5f3ba11] | 310 | "finally", |
---|
| 311 | {}, //forall |
---|
[33b7d49] | 312 | { make_voidptr_object( location ) }, |
---|
[5f3ba11] | 313 | {}, //return void |
---|
| 314 | nullptr, |
---|
| 315 | ast::Storage::Classes{}, |
---|
[ce04120] | 316 | ast::Linkage::Cforall, |
---|
| 317 | {}, |
---|
| 318 | { ast::Function::Inline } |
---|
[5f3ba11] | 319 | ); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | // TryStmt Mutation Helpers |
---|
| 323 | |
---|
| 324 | ast::FunctionDecl * TryMutatorCore::create_try_wrapper( |
---|
| 325 | const ast::CompoundStmt *body ) { |
---|
| 326 | |
---|
[33b7d49] | 327 | ast::FunctionDecl * ret = make_try_function( body->location ); |
---|
[5f3ba11] | 328 | ret->stmts = body; |
---|
| 329 | return ret; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | ast::FunctionDecl * TryMutatorCore::create_terminate_catch( |
---|
| 333 | CatchList &handlers ) { |
---|
[400b8be] | 334 | std::vector<ast::ptr<ast::CaseClause>> handler_wrappers; |
---|
[5f3ba11] | 335 | |
---|
| 336 | assert (!handlers.empty()); |
---|
| 337 | const CodeLocation loc = handlers.front()->location; |
---|
| 338 | |
---|
[33b7d49] | 339 | ast::FunctionDecl * func_t = make_catch_function( loc ); |
---|
[5f3ba11] | 340 | const ast::DeclWithType * index_obj = func_t->params.front(); |
---|
| 341 | const ast::DeclWithType * except_obj = func_t->params.back(); |
---|
| 342 | |
---|
| 343 | // Index 1..{number of handlers} |
---|
| 344 | int index = 0; |
---|
| 345 | CatchList::iterator it = handlers.begin(); |
---|
| 346 | for ( ; it != handlers.end() ; ++it ) { |
---|
| 347 | ++index; |
---|
[400b8be] | 348 | ast::CatchClause * handler = *it; |
---|
[5f3ba11] | 349 | const CodeLocation loc = handler->location; |
---|
| 350 | |
---|
| 351 | // case `index`: |
---|
| 352 | // { |
---|
| 353 | // `handler.decl` = { (virtual `decl.type`)`except` }; |
---|
| 354 | // `handler.body`; |
---|
| 355 | // } |
---|
| 356 | // return; |
---|
| 357 | ast::CompoundStmt * block = new ast::CompoundStmt(loc); |
---|
| 358 | |
---|
| 359 | // Just copy the exception value. (Post Validation) |
---|
| 360 | const ast::ObjectDecl * handler_decl = |
---|
| 361 | handler->decl.strict_as<ast::ObjectDecl>(); |
---|
| 362 | ast::ObjectDecl * local_except = ast::deepCopy(handler_decl); |
---|
| 363 | ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc, |
---|
| 364 | new ast::VariableExpr( loc, except_obj ), |
---|
| 365 | local_except->get_type() |
---|
| 366 | ); |
---|
| 367 | vcex->location = handler->location; |
---|
| 368 | local_except->init = new ast::ListInit(loc, { new ast::SingleInit( loc, vcex ) }); |
---|
| 369 | block->push_back( new ast::DeclStmt( loc, local_except ) ); |
---|
| 370 | |
---|
| 371 | // Add the cleanup attribute. |
---|
| 372 | local_except->attributes.push_back( new ast::Attribute( |
---|
| 373 | "cleanup", |
---|
| 374 | { new ast::NameExpr( loc, "__cfaehm_cleanup_terminate" ) } |
---|
| 375 | ) ); |
---|
| 376 | |
---|
| 377 | ast::DeclReplacer::DeclMap mapping; |
---|
| 378 | mapping[handler_decl] = local_except; |
---|
| 379 | const ast::Stmt * mutBody = strict_dynamic_cast<const ast::Stmt *>( |
---|
| 380 | ast::DeclReplacer::replace(handler->body, mapping)); |
---|
| 381 | |
---|
| 382 | |
---|
| 383 | block->push_back( mutBody ); |
---|
| 384 | // handler->body = nullptr; |
---|
| 385 | |
---|
[400b8be] | 386 | handler_wrappers.push_back( new ast::CaseClause(loc, |
---|
[5f3ba11] | 387 | ast::ConstantExpr::from_int(loc, index) , |
---|
| 388 | { block, new ast::ReturnStmt( loc, nullptr ) } |
---|
| 389 | )); |
---|
| 390 | } |
---|
| 391 | // TODO: Some sort of meaningful error on default perhaps? |
---|
| 392 | |
---|
[33b7d49] | 393 | ast::SwitchStmt * handler_lookup = new ast::SwitchStmt( loc, |
---|
[5f3ba11] | 394 | new ast::VariableExpr( loc, index_obj ), |
---|
| 395 | std::move(handler_wrappers) |
---|
| 396 | ); |
---|
[33b7d49] | 397 | ast::CompoundStmt * body = new ast::CompoundStmt( loc, {handler_lookup} ); |
---|
[5f3ba11] | 398 | |
---|
| 399 | func_t->stmts = body; |
---|
| 400 | return func_t; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | // Create a single check from a moddified handler. |
---|
| 404 | // except_obj is referenced, modded_handler will be freed. |
---|
| 405 | ast::CompoundStmt * TryMutatorCore::create_single_matcher( |
---|
[400b8be] | 406 | const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler ) { |
---|
[5f3ba11] | 407 | // { |
---|
| 408 | // `modded_handler.decl` |
---|
| 409 | // if ( `decl.name = (virtual `decl.type`)`except` |
---|
| 410 | // [&& `modded_handler.cond`] ) { |
---|
| 411 | // `modded_handler.body` |
---|
| 412 | // } |
---|
| 413 | // } |
---|
| 414 | |
---|
| 415 | const CodeLocation loc = modded_handler->location; |
---|
| 416 | ast::CompoundStmt * block = new ast::CompoundStmt(loc); |
---|
| 417 | |
---|
| 418 | // Local Declaration |
---|
| 419 | const ast::ObjectDecl * local_except = |
---|
| 420 | modded_handler->decl.strict_as<ast::ObjectDecl>(); |
---|
| 421 | block->push_back( new ast::DeclStmt( loc, local_except ) ); |
---|
| 422 | |
---|
| 423 | // Check for type match. |
---|
[33b7d49] | 424 | ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc, |
---|
[5f3ba11] | 425 | new ast::VariableExpr(loc, except_obj ), |
---|
| 426 | local_except->get_type() |
---|
| 427 | ); |
---|
| 428 | ast::Expr * cond = ast::UntypedExpr::createAssign(loc, |
---|
| 429 | new ast::VariableExpr(loc, local_except ), vcex ); |
---|
| 430 | |
---|
| 431 | // Add the check on the conditional if it is provided. |
---|
| 432 | if ( modded_handler->cond ) { |
---|
| 433 | cond = new ast::LogicalExpr( loc, cond, modded_handler->cond, ast::LogicalFlag::AndExpr ); |
---|
| 434 | } |
---|
| 435 | // Construct the match condition. |
---|
[33b7d49] | 436 | block->push_back( new ast::IfStmt(loc, |
---|
[5f3ba11] | 437 | cond, modded_handler->body, nullptr ) ); |
---|
| 438 | |
---|
| 439 | return block; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | ast::FunctionDecl * TryMutatorCore::create_terminate_match( |
---|
| 443 | CatchList &handlers ) { |
---|
| 444 | // int match(exception * except) { |
---|
| 445 | // HANDLER WRAPPERS { return `index`; } |
---|
| 446 | // } |
---|
| 447 | |
---|
| 448 | assert (!handlers.empty()); |
---|
| 449 | const CodeLocation loc = handlers.front()->location; |
---|
| 450 | |
---|
| 451 | ast::CompoundStmt * body = new ast::CompoundStmt(loc); |
---|
| 452 | |
---|
[33b7d49] | 453 | ast::FunctionDecl * func_t = make_match_function( loc ); |
---|
[5f3ba11] | 454 | const ast::DeclWithType * except_obj = func_t->params.back(); |
---|
| 455 | |
---|
| 456 | // Index 1..{number of handlers} |
---|
| 457 | int index = 0; |
---|
| 458 | CatchList::iterator it; |
---|
| 459 | for ( it = handlers.begin() ; it != handlers.end() ; ++it ) { |
---|
| 460 | ++index; |
---|
[400b8be] | 461 | ast::CatchClause * handler = *it; |
---|
[5f3ba11] | 462 | |
---|
| 463 | // Body should have been taken by create_terminate_catch. |
---|
| 464 | // xxx - just ignore it? |
---|
| 465 | // assert( nullptr == handler->get_body() ); |
---|
| 466 | |
---|
| 467 | // Create new body. |
---|
| 468 | handler->body = new ast::ReturnStmt( handler->location, |
---|
| 469 | ast::ConstantExpr::from_int( handler->location, index ) ); |
---|
| 470 | |
---|
| 471 | // Create the handler. |
---|
| 472 | body->push_back( create_single_matcher( except_obj, handler ) ); |
---|
| 473 | *it = nullptr; |
---|
| 474 | } |
---|
| 475 | |
---|
[33b7d49] | 476 | body->push_back( new ast::ReturnStmt(loc, |
---|
[5f3ba11] | 477 | ast::ConstantExpr::from_int( loc, 0 ) )); |
---|
| 478 | |
---|
| 479 | func_t->stmts = body; |
---|
| 480 | |
---|
| 481 | return func_t; |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | ast::CompoundStmt * TryMutatorCore::create_terminate_caller( |
---|
| 485 | CodeLocation loc, |
---|
| 486 | ast::FunctionDecl * try_wrapper, |
---|
| 487 | ast::FunctionDecl * terminate_catch, |
---|
| 488 | ast::FunctionDecl * terminate_match ) { |
---|
| 489 | // { __cfaehm_try_terminate(`try`, `catch`, `match`); } |
---|
| 490 | |
---|
| 491 | ast::UntypedExpr * caller = new ast::UntypedExpr(loc, new ast::NameExpr(loc, |
---|
| 492 | "__cfaehm_try_terminate" ) ); |
---|
| 493 | caller->args.push_back( new ast::VariableExpr(loc, try_wrapper ) ); |
---|
| 494 | caller->args.push_back( new ast::VariableExpr(loc, terminate_catch ) ); |
---|
| 495 | caller->args.push_back( new ast::VariableExpr(loc, terminate_match ) ); |
---|
| 496 | |
---|
| 497 | ast::CompoundStmt * callStmt = new ast::CompoundStmt(loc); |
---|
| 498 | callStmt->push_back( new ast::ExprStmt( loc, caller ) ); |
---|
| 499 | return callStmt; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | ast::FunctionDecl * TryMutatorCore::create_resume_handler( |
---|
| 503 | CatchList &handlers ) { |
---|
| 504 | // bool handle(exception * except) { |
---|
| 505 | // HANDLER WRAPPERS { `hander->body`; return true; } |
---|
| 506 | // } |
---|
| 507 | assert (!handlers.empty()); |
---|
| 508 | const CodeLocation loc = handlers.front()->location; |
---|
| 509 | ast::CompoundStmt * body = new ast::CompoundStmt(loc); |
---|
| 510 | |
---|
[33b7d49] | 511 | ast::FunctionDecl * func_t = make_handle_function( loc ); |
---|
[5f3ba11] | 512 | const ast::DeclWithType * except_obj = func_t->params.back(); |
---|
| 513 | |
---|
| 514 | CatchList::iterator it; |
---|
| 515 | for ( it = handlers.begin() ; it != handlers.end() ; ++it ) { |
---|
[400b8be] | 516 | ast::CatchClause * handler = *it; |
---|
[5f3ba11] | 517 | const CodeLocation loc = handler->location; |
---|
| 518 | // Modifiy body. |
---|
| 519 | ast::CompoundStmt * handling_code; |
---|
| 520 | if (handler->body.as<ast::CompoundStmt>()) { |
---|
[33b7d49] | 521 | handling_code = strict_dynamic_cast<ast::CompoundStmt*>( |
---|
| 522 | handler->body.get_and_mutate() ); |
---|
[5f3ba11] | 523 | } else { |
---|
| 524 | handling_code = new ast::CompoundStmt(loc); |
---|
| 525 | handling_code->push_back( handler->body ); |
---|
| 526 | } |
---|
| 527 | handling_code->push_back( new ast::ReturnStmt(loc, |
---|
| 528 | ast::ConstantExpr::from_bool(loc, true ) ) ); |
---|
| 529 | handler->body = handling_code; |
---|
| 530 | |
---|
| 531 | // Create the handler. |
---|
| 532 | body->push_back( create_single_matcher( except_obj, handler ) ); |
---|
| 533 | *it = nullptr; |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | body->push_back( new ast::ReturnStmt(loc, |
---|
| 537 | ast::ConstantExpr::from_bool(loc, false ) ) ); |
---|
| 538 | func_t->stmts = body; |
---|
| 539 | |
---|
| 540 | return func_t; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | ast::CompoundStmt * TryMutatorCore::create_resume_wrapper( |
---|
| 544 | const ast::Stmt * wraps, |
---|
| 545 | const ast::FunctionDecl * resume_handler ) { |
---|
| 546 | const CodeLocation loc = wraps->location; |
---|
| 547 | ast::CompoundStmt * body = new ast::CompoundStmt(loc); |
---|
| 548 | |
---|
| 549 | // struct __try_resume_node __resume_node |
---|
| 550 | // __attribute__((cleanup( __cfaehm_try_resume_cleanup ))); |
---|
| 551 | // ** unwinding of the stack here could cause problems ** |
---|
| 552 | // ** however I don't think that can happen currently ** |
---|
| 553 | // __cfaehm_try_resume_setup( &__resume_node, resume_handler ); |
---|
| 554 | |
---|
| 555 | ast::ObjectDecl * obj = new ast::ObjectDecl( |
---|
| 556 | loc, |
---|
| 557 | "__resume_node", |
---|
| 558 | new ast::StructInstType( |
---|
| 559 | node_decl |
---|
| 560 | ), |
---|
| 561 | nullptr, |
---|
| 562 | ast::Storage::Classes{}, |
---|
| 563 | ast::Linkage::Cforall, |
---|
| 564 | nullptr, |
---|
| 565 | {new ast::Attribute("cleanup", {new ast::NameExpr(loc, "__cfaehm_try_resume_cleanup")})} |
---|
| 566 | ); |
---|
| 567 | appendDeclStmt( body, obj ); |
---|
| 568 | |
---|
| 569 | ast::UntypedExpr *setup = new ast::UntypedExpr(loc, new ast::NameExpr(loc, |
---|
| 570 | "__cfaehm_try_resume_setup" ) ); |
---|
| 571 | setup->args.push_back( new ast::AddressExpr( loc, new ast::VariableExpr(loc, obj ) ) ); |
---|
| 572 | setup->args.push_back( new ast::VariableExpr( loc, resume_handler ) ); |
---|
| 573 | |
---|
| 574 | body->push_back( new ast::ExprStmt(loc, setup ) ); |
---|
| 575 | |
---|
| 576 | body->push_back( wraps ); |
---|
| 577 | return body; |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | ast::FunctionDecl * TryMutatorCore::create_finally_wrapper( |
---|
| 581 | ast::TryStmt * tryStmt ) { |
---|
| 582 | // void finally() { `finally->block` } |
---|
[400b8be] | 583 | const ast::FinallyClause * finally = tryStmt->finally; |
---|
[5f3ba11] | 584 | const ast::CompoundStmt * body = finally->body; |
---|
| 585 | |
---|
[33b7d49] | 586 | ast::FunctionDecl * func_t = make_finally_function( tryStmt->location ); |
---|
[5f3ba11] | 587 | func_t->stmts = body; |
---|
| 588 | |
---|
| 589 | tryStmt->finally = nullptr; |
---|
| 590 | |
---|
| 591 | return func_t; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | ast::ObjectDecl * TryMutatorCore::create_finally_hook( |
---|
| 595 | ast::FunctionDecl * finally_wrapper ) { |
---|
| 596 | // struct __cfaehm_cleanup_hook __finally_hook |
---|
| 597 | // __attribute__((cleanup( `finally_wrapper` ))); |
---|
| 598 | |
---|
| 599 | const CodeLocation loc = finally_wrapper->location; |
---|
| 600 | return new ast::ObjectDecl( |
---|
| 601 | loc, |
---|
| 602 | "__finally_hook", |
---|
| 603 | new ast::StructInstType( |
---|
| 604 | hook_decl |
---|
| 605 | ), |
---|
| 606 | nullptr, |
---|
| 607 | ast::Storage::Classes{}, |
---|
| 608 | ast::Linkage::Cforall, |
---|
| 609 | nullptr, |
---|
| 610 | {new ast::Attribute("cleanup", {new ast::VariableExpr{loc, finally_wrapper}})} |
---|
| 611 | ); |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | ast::Stmt * TryMutatorCore::create_resume_rethrow( const ast::ThrowStmt *throwStmt ) { |
---|
| 615 | // return false; |
---|
| 616 | const CodeLocation loc = throwStmt->location; |
---|
[33b7d49] | 617 | ast::Stmt * result = new ast::ReturnStmt(loc, |
---|
[5f3ba11] | 618 | ast::ConstantExpr::from_bool( loc, false ) |
---|
| 619 | ); |
---|
| 620 | result->labels = throwStmt->labels; |
---|
| 621 | return result; |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | // Visiting/Mutating Functions |
---|
| 625 | void TryMutatorCore::previsit( const ast::StructDecl *structDecl ) { |
---|
| 626 | if ( !structDecl->body ) { |
---|
| 627 | // Skip children? |
---|
| 628 | return; |
---|
| 629 | } else if ( structDecl->name == "__cfaehm_base_exception_t" ) { |
---|
| 630 | assert( nullptr == except_decl ); |
---|
| 631 | except_decl = structDecl; |
---|
| 632 | } else if ( structDecl->name == "__cfaehm_try_resume_node" ) { |
---|
| 633 | assert( nullptr == node_decl ); |
---|
| 634 | node_decl = structDecl; |
---|
| 635 | } else if ( structDecl->name == "__cfaehm_cleanup_hook" ) { |
---|
| 636 | assert( nullptr == hook_decl ); |
---|
| 637 | hook_decl = structDecl; |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | ast::Stmt * TryMutatorCore::postvisit( const ast::TryStmt *tryStmt ) { |
---|
| 642 | assert( except_decl ); |
---|
| 643 | assert( node_decl ); |
---|
| 644 | assert( hook_decl ); |
---|
| 645 | |
---|
| 646 | const CodeLocation loc = tryStmt->location; |
---|
| 647 | ast::TryStmt * mutStmt = mutate(tryStmt); |
---|
| 648 | // Generate a prefix for the function names? |
---|
| 649 | |
---|
| 650 | ast::CompoundStmt * block = new ast::CompoundStmt( loc ); |
---|
| 651 | // ast::CompoundStmt * inner = take_try_block( mutStmt ); |
---|
| 652 | // this is never mutated so let node deletion do its job? |
---|
| 653 | const ast::CompoundStmt * inner = mutStmt->body; |
---|
| 654 | |
---|
| 655 | if ( mutStmt->finally ) { |
---|
| 656 | // Define the helper function. |
---|
| 657 | ast::FunctionDecl * finally_block = |
---|
| 658 | create_finally_wrapper( mutStmt ); |
---|
| 659 | appendDeclStmt( block, finally_block ); |
---|
| 660 | // Create and add the finally cleanup hook. |
---|
| 661 | appendDeclStmt( block, create_finally_hook( finally_block ) ); |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | CatchList termination_handlers; |
---|
| 665 | CatchList resumption_handlers; |
---|
| 666 | |
---|
| 667 | for (auto & handler: mutStmt->handlers) { |
---|
| 668 | // xxx - should always be unique? mutate as safe const-cast |
---|
| 669 | assert(handler->unique()); |
---|
| 670 | if (handler->kind == ast::ExceptionKind::Resume) { |
---|
| 671 | resumption_handlers.push_back(handler.get_and_mutate()); |
---|
| 672 | } |
---|
| 673 | else { |
---|
| 674 | termination_handlers.push_back(handler.get_and_mutate()); |
---|
| 675 | } |
---|
| 676 | } |
---|
| 677 | |
---|
| 678 | if ( resumption_handlers.size() ) { |
---|
| 679 | // Define the helper function. |
---|
| 680 | ast::FunctionDecl * resume_handler = |
---|
| 681 | create_resume_handler( resumption_handlers ); |
---|
| 682 | appendDeclStmt( block, resume_handler ); |
---|
| 683 | // Prepare hooks |
---|
| 684 | inner = create_resume_wrapper( inner, resume_handler ); |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | if ( termination_handlers.size() ) { |
---|
| 688 | // Define the three helper functions. |
---|
| 689 | ast::FunctionDecl * try_wrapper = create_try_wrapper( inner ); |
---|
| 690 | appendDeclStmt( block, try_wrapper ); |
---|
| 691 | ast::FunctionDecl * terminate_catch = |
---|
| 692 | create_terminate_catch( termination_handlers ); |
---|
| 693 | appendDeclStmt( block, terminate_catch ); |
---|
| 694 | ast::FunctionDecl * terminate_match = |
---|
| 695 | create_terminate_match( termination_handlers ); |
---|
| 696 | appendDeclStmt( block, terminate_match ); |
---|
| 697 | // Build the call to the try wrapper. |
---|
| 698 | inner = create_terminate_caller(inner->location, |
---|
| 699 | try_wrapper, terminate_catch, terminate_match ); |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | // Embed the try block. |
---|
| 703 | block->push_back( inner ); |
---|
| 704 | |
---|
| 705 | return block; |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | ast::Stmt * TryMutatorCore::postvisit( const ast::ThrowStmt *throwStmt ) { |
---|
| 709 | // Only valid `throwResume;` statements should remain. (2/3 checks) |
---|
| 710 | assert( ast::ExceptionKind::Resume == throwStmt->kind && ! throwStmt->expr ); |
---|
| 711 | return create_resume_rethrow( throwStmt ); |
---|
| 712 | } |
---|
| 713 | |
---|
[5ee153d] | 714 | } // namespace |
---|
| 715 | |
---|
| 716 | void translateThrows( ast::TranslationUnit & transUnit ) { |
---|
| 717 | ast::Pass<TranslateThrowsCore>::run( transUnit ); |
---|
| 718 | } |
---|
| 719 | |
---|
[5f3ba11] | 720 | void translateTries( ast::TranslationUnit & transUnit ) { |
---|
| 721 | ast::Pass<TryMutatorCore>::run(transUnit); |
---|
| 722 | } |
---|
| 723 | |
---|
[5ee153d] | 724 | } // namespace ControlStruct |
---|
| 725 | |
---|
| 726 | // Local Variables: // |
---|
| 727 | // tab-width: 4 // |
---|
| 728 | // mode: c++ // |
---|
| 729 | // compile-command: "make install" // |
---|
| 730 | // End: // |
---|