| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // exception.h -- Internal exception handling definitions. | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Andrew Beach | 
|---|
| 10 | // Created On       : Mon Jun 26 15:11:00 2017 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Thr Apr  8 15:20:00 2021 | 
|---|
| 13 | // Update Count     : 12 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | // This could be considered several headers. All are internal to the exception | 
|---|
| 19 | // system but needed to depending on whether they are C/Cforall code and | 
|---|
| 20 | // whether or not they are part of the builtins. | 
|---|
| 21 |  | 
|---|
| 22 | #ifdef __cforall | 
|---|
| 23 | extern "C" { | 
|---|
| 24 | #endif | 
|---|
| 25 |  | 
|---|
| 26 | // Included in C code or the built-ins. | 
|---|
| 27 | #if !defined(__cforall) || defined(__cforall_builtins__) | 
|---|
| 28 |  | 
|---|
| 29 | struct __cfaehm_base_exception_t; | 
|---|
| 30 | typedef struct __cfaehm_base_exception_t exception_t; | 
|---|
| 31 | struct __cfavir_type_info; | 
|---|
| 32 | struct __cfaehm_base_exception_t_vtable { | 
|---|
| 33 | const struct __cfavir_type_info * __cfavir_typeid; | 
|---|
| 34 | size_t size; | 
|---|
| 35 | void (*copy)(struct __cfaehm_base_exception_t *this, | 
|---|
| 36 | struct __cfaehm_base_exception_t * other); | 
|---|
| 37 | void (*free)(struct __cfaehm_base_exception_t *this); | 
|---|
| 38 | const char * (*msg)(struct __cfaehm_base_exception_t *this); | 
|---|
| 39 | }; | 
|---|
| 40 | struct __cfaehm_base_exception_t { | 
|---|
| 41 | struct __cfaehm_base_exception_t_vtable const * virtual_table; | 
|---|
| 42 | }; | 
|---|
| 43 | extern struct __cfavir_type_info __cfatid_exception_t; | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | void __cfaehm_cancel_stack(exception_t * except) __attribute__((noreturn)); | 
|---|
| 47 |  | 
|---|
| 48 | // Used in throw statement translation. | 
|---|
| 49 | void __cfaehm_throw_terminate(exception_t * except, void (*)(exception_t *)); | 
|---|
| 50 | void __cfaehm_rethrow_terminate() __attribute__((noreturn)); | 
|---|
| 51 | void __cfaehm_throw_resume(exception_t * except, void (*)(exception_t *)); | 
|---|
| 52 |  | 
|---|
| 53 | // Function catches termination exceptions. | 
|---|
| 54 | void __cfaehm_try_terminate( | 
|---|
| 55 | void (*try_block)(), | 
|---|
| 56 | void (*catch_block)(int index, exception_t * except), | 
|---|
| 57 | int (*match_block)(exception_t * except)); | 
|---|
| 58 |  | 
|---|
| 59 | // Clean-up the exception in catch blocks. | 
|---|
| 60 | void __cfaehm_cleanup_terminate(void * except); | 
|---|
| 61 |  | 
|---|
| 62 | // Data structure creates a list of resume handlers. | 
|---|
| 63 | struct __cfaehm_try_resume_node { | 
|---|
| 64 | struct __cfaehm_try_resume_node * next; | 
|---|
| 65 | _Bool (*handler)(exception_t * except); | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | // These act as constructor and destructor for the resume node. | 
|---|
| 69 | void __cfaehm_try_resume_setup( | 
|---|
| 70 | struct __cfaehm_try_resume_node * node, | 
|---|
| 71 | _Bool (*handler)(exception_t * except)); | 
|---|
| 72 | void __cfaehm_try_resume_cleanup( | 
|---|
| 73 | struct __cfaehm_try_resume_node * node); | 
|---|
| 74 |  | 
|---|
| 75 | // Check for a standard way to call fake deconstructors. | 
|---|
| 76 | struct __cfaehm_cleanup_hook {}; | 
|---|
| 77 |  | 
|---|
| 78 | #endif | 
|---|
| 79 |  | 
|---|
| 80 | // Included in C code and the library. | 
|---|
| 81 | #if !defined(__cforall) || !defined(__cforall_builtins__) | 
|---|
| 82 | struct __cfaehm_node { | 
|---|
| 83 | struct _Unwind_Exception unwind_exception; | 
|---|
| 84 | struct __cfaehm_node * next; | 
|---|
| 85 | int handler_index; | 
|---|
| 86 | }; | 
|---|
| 87 |  | 
|---|
| 88 | static inline exception_t * __cfaehm_cancellation_exception( | 
|---|
| 89 | struct _Unwind_Exception * unwind_exception ) { | 
|---|
| 90 | return (exception_t *)(1 + (struct __cfaehm_node *)unwind_exception); | 
|---|
| 91 | } | 
|---|
| 92 | #endif | 
|---|
| 93 |  | 
|---|
| 94 | #ifdef __cforall | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | // Built-ins not visible in C. | 
|---|
| 98 | #if defined(__cforall_builtins__) | 
|---|
| 99 |  | 
|---|
| 100 | // Not all the built-ins can be expressed in C. These can't be | 
|---|
| 101 | // implemented in the .c file either so they all have to be inline. | 
|---|
| 102 |  | 
|---|
| 103 | trait is_exception(exceptT &, virtualT &) { | 
|---|
| 104 | /* The first field must be a pointer to a virtual table. | 
|---|
| 105 | * That virtual table must be a decendent of the base exception virtual table. | 
|---|
| 106 | * The virtual table must point at the prober type-id. | 
|---|
| 107 | * None of these can be enforced in an assertion. | 
|---|
| 108 | */ | 
|---|
| 109 | }; | 
|---|
| 110 |  | 
|---|
| 111 | trait is_termination_exception(exceptT &, virtualT & | is_exception(exceptT, virtualT)) { | 
|---|
| 112 | void defaultTerminationHandler(exceptT &); | 
|---|
| 113 | }; | 
|---|
| 114 |  | 
|---|
| 115 | trait is_resumption_exception(exceptT &, virtualT & | is_exception(exceptT, virtualT)) { | 
|---|
| 116 | void defaultResumptionHandler(exceptT &); | 
|---|
| 117 | }; | 
|---|
| 118 |  | 
|---|
| 119 | forall(exceptT &, virtualT & | is_termination_exception(exceptT, virtualT)) | 
|---|
| 120 | static inline void $throw(exceptT & except) { | 
|---|
| 121 | __cfaehm_throw_terminate( | 
|---|
| 122 | (exception_t *)&except, | 
|---|
| 123 | (void(*)(exception_t *))defaultTerminationHandler | 
|---|
| 124 | ); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | forall(exceptT &, virtualT & | is_resumption_exception(exceptT, virtualT)) | 
|---|
| 128 | static inline void $throwResume(exceptT & except) { | 
|---|
| 129 | __cfaehm_throw_resume( | 
|---|
| 130 | (exception_t *)&except, | 
|---|
| 131 | (void(*)(exception_t *))defaultResumptionHandler | 
|---|
| 132 | ); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | forall(exceptT &, virtualT & | is_exception(exceptT, virtualT)) | 
|---|
| 136 | static inline void cancel_stack(exceptT & except) __attribute__((noreturn)) { | 
|---|
| 137 | __cfaehm_cancel_stack( (exception_t *)&except ); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | forall(exceptT &, virtualT & | is_exception(exceptT, virtualT)) | 
|---|
| 141 | static inline void defaultTerminationHandler(exceptT & except) { | 
|---|
| 142 | return cancel_stack( except ); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | forall(exceptT &, virtualT & | is_exception(exceptT, virtualT)) | 
|---|
| 146 | static inline void defaultResumptionHandler(exceptT & except) { | 
|---|
| 147 | throw except; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | #endif | 
|---|
| 151 |  | 
|---|
| 152 | #endif | 
|---|