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