| [fa4805f] | 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 -- Builtins for exception handling. | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Andrew Beach | 
|---|
|  | 10 | // Created On       : Mon Jun 26 15:11:00 2017 | 
|---|
| [3090127] | 11 | // Last Modified By : Andrew Beach | 
|---|
| [046a890] | 12 | // Last Modified On : Tue May 19 14:17:00 2020 | 
|---|
|  | 13 | // Update Count     : 10 | 
|---|
| [fa4805f] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [6b0b624] | 16 | #pragma once | 
|---|
| [fa4805f] | 17 |  | 
|---|
|  | 18 |  | 
|---|
| [0cf5b79] | 19 | #ifdef __cforall | 
|---|
| [0fc9756b] | 20 | extern "C" { | 
|---|
| [fa4805f] | 21 | #endif | 
|---|
|  | 22 |  | 
|---|
| [3090127] | 23 | struct __cfaehm_base_exception_t; | 
|---|
|  | 24 | typedef struct __cfaehm_base_exception_t exception_t; | 
|---|
|  | 25 | struct __cfaehm_base_exception_t_vtable { | 
|---|
|  | 26 | const struct __cfaehm_base_exception_t_vtable * parent; | 
|---|
| [86d5ba7c] | 27 | size_t size; | 
|---|
| [3090127] | 28 | void (*copy)(struct __cfaehm_base_exception_t *this, | 
|---|
|  | 29 | struct __cfaehm_base_exception_t * other); | 
|---|
|  | 30 | void (*free)(struct __cfaehm_base_exception_t *this); | 
|---|
|  | 31 | const char * (*msg)(struct __cfaehm_base_exception_t *this); | 
|---|
| [86d5ba7c] | 32 | }; | 
|---|
| [3090127] | 33 | struct __cfaehm_base_exception_t { | 
|---|
|  | 34 | struct __cfaehm_base_exception_t_vtable const * virtual_table; | 
|---|
| [86d5ba7c] | 35 | }; | 
|---|
| [3090127] | 36 | extern struct __cfaehm_base_exception_t_vtable | 
|---|
|  | 37 | ___cfaehm_base_exception_t_vtable_instance; | 
|---|
| [86d5ba7c] | 38 |  | 
|---|
|  | 39 |  | 
|---|
| [979df46] | 40 | void __cfaehm_cancel_stack(exception_t * except) __attribute__((noreturn)); | 
|---|
|  | 41 |  | 
|---|
| [fa4805f] | 42 | // Used in throw statement translation. | 
|---|
| [046a890] | 43 | void __cfaehm_throw_terminate(exception_t * except, void (*)(exception_t *)); | 
|---|
| [3090127] | 44 | void __cfaehm_rethrow_terminate() __attribute__((noreturn)); | 
|---|
| [046a890] | 45 | void __cfaehm_throw_resume(exception_t * except, void (*)(exception_t *)); | 
|---|
| [fa4805f] | 46 |  | 
|---|
|  | 47 | // Function catches termination exceptions. | 
|---|
| [3090127] | 48 | void __cfaehm_try_terminate( | 
|---|
| [fa4805f] | 49 | void (*try_block)(), | 
|---|
| [0304215a] | 50 | void (*catch_block)(int index, exception_t * except), | 
|---|
|  | 51 | int (*match_block)(exception_t * except)); | 
|---|
| [fa4805f] | 52 |  | 
|---|
| [86d5ba7c] | 53 | // Clean-up the exception in catch blocks. | 
|---|
| [3090127] | 54 | void __cfaehm_cleanup_terminate(void * except); | 
|---|
| [86d5ba7c] | 55 |  | 
|---|
| [fa4805f] | 56 | // Data structure creates a list of resume handlers. | 
|---|
| [3090127] | 57 | struct __cfaehm_try_resume_node { | 
|---|
|  | 58 | struct __cfaehm_try_resume_node * next; | 
|---|
| [0304215a] | 59 | _Bool (*handler)(exception_t * except); | 
|---|
| [fa4805f] | 60 | }; | 
|---|
|  | 61 |  | 
|---|
| [86d5ba7c] | 62 | // These act as constructor and destructor for the resume node. | 
|---|
| [3090127] | 63 | void __cfaehm_try_resume_setup( | 
|---|
|  | 64 | struct __cfaehm_try_resume_node * node, | 
|---|
| [0304215a] | 65 | _Bool (*handler)(exception_t * except)); | 
|---|
| [3090127] | 66 | void __cfaehm_try_resume_cleanup( | 
|---|
|  | 67 | struct __cfaehm_try_resume_node * node); | 
|---|
| [fa4805f] | 68 |  | 
|---|
|  | 69 | // Check for a standard way to call fake deconstructors. | 
|---|
| [3090127] | 70 | struct __cfaehm_cleanup_hook {}; | 
|---|
| [fa4805f] | 71 |  | 
|---|
| [0cf5b79] | 72 | #ifdef __cforall | 
|---|
| [fa4805f] | 73 | } | 
|---|
| [046a890] | 74 |  | 
|---|
|  | 75 | // Not all the built-ins can be expressed in C. These can't be | 
|---|
|  | 76 | // implemented in the .c file either so they all have to be inline. | 
|---|
|  | 77 |  | 
|---|
|  | 78 | trait is_exception(dtype T) { | 
|---|
|  | 79 | /* The first field must be a pointer to a virtual table. | 
|---|
|  | 80 | * That virtual table must be a decendent of the base exception virtual tab$ | 
|---|
|  | 81 | */ | 
|---|
|  | 82 | void mark_exception(T *); | 
|---|
|  | 83 | // This is never used and should be a no-op. | 
|---|
|  | 84 | }; | 
|---|
|  | 85 |  | 
|---|
|  | 86 | trait is_termination_exception(dtype T | is_exception(T)) { | 
|---|
|  | 87 | void defaultTerminationHandler(T &); | 
|---|
|  | 88 | }; | 
|---|
|  | 89 |  | 
|---|
|  | 90 | trait is_resumption_exception(dtype T | is_exception(T)) { | 
|---|
|  | 91 | void defaultResumptionHandler(T &); | 
|---|
|  | 92 | }; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | forall(dtype T | is_termination_exception(T)) | 
|---|
|  | 95 | static inline void $throw(T & except) { | 
|---|
|  | 96 | __cfaehm_throw_terminate( | 
|---|
|  | 97 | (exception_t *)&except, | 
|---|
|  | 98 | (void(*)(exception_t *))defaultTerminationHandler | 
|---|
|  | 99 | ); | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
|  | 102 | forall(dtype T | is_resumption_exception(T)) | 
|---|
|  | 103 | static inline void $throwResume(T & except) { | 
|---|
|  | 104 | __cfaehm_throw_resume( | 
|---|
|  | 105 | (exception_t *)&except, | 
|---|
|  | 106 | (void(*)(exception_t *))defaultResumptionHandler | 
|---|
|  | 107 | ); | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | forall(dtype T | is_exception(T)) | 
|---|
|  | 111 | static inline void cancel_stack(T & except) __attribute__((noreturn)) { | 
|---|
|  | 112 | __cfaehm_cancel_stack( (exception_t *)&except ); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | forall(dtype T | is_exception(T)) | 
|---|
|  | 116 | static inline void defaultTerminationHandler(T & except) { | 
|---|
|  | 117 | return cancel_stack( except ); | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | forall(dtype T | is_exception(T)) | 
|---|
|  | 121 | static inline void defaultResumptionHandler(T & except) { | 
|---|
|  | 122 | throw except; | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
| [fa4805f] | 125 | #endif | 
|---|