source: libcfa/src/exception.h @ b08ab18

Last change on this file since b08ab18 was b7898ac, checked in by Andrew Beach <ajbeach@…>, 6 months ago

Another attempt at fixing execptions. It is very close to the last attempt but the offsets have been updated and checked.

  • Property mode set to 100644
File size: 4.9 KB
Line 
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 : Peter A. Buhr
12// Last Modified On : Thu Feb  2 11:20:19 2023
13// Update Count     : 13
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
23extern "C" {
24#endif
25
26// Included in C code or the built-ins.
27#if !defined(__cforall) || defined(__cforall_builtins__)
28
29struct __cfaehm_base_exception_t;
30typedef struct __cfaehm_base_exception_t exception_t;
31struct __cfavir_type_info;
32struct __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};
40struct __cfaehm_base_exception_t {
41        struct __cfaehm_base_exception_t_vtable const * virtual_table;
42};
43extern struct __cfavir_type_info __cfatid_exception_t;
44
45
46struct __cfaehm_base_exception_t * __cfaehm_get_current_termination(void);
47
48
49void __cfaehm_cancel_stack(exception_t * except) __attribute__((noreturn));
50
51// Used in throw statement translation.
52void __cfaehm_throw_terminate(exception_t * except, void (*)(exception_t *));
53void __cfaehm_rethrow_terminate() __attribute__((noreturn));
54void __cfaehm_throw_resume(exception_t * except, void (*)(exception_t *));
55
56// Used in non-local ehm (see coroutine.cfa)
57void __cfaehm_allocate_exception( exception_t * except );
58void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *));
59
60
61// Function catches termination exceptions.
62int __cfaehm_try_terminate(
63        void (*try_block)(),
64        int (*match_block)(exception_t * except));
65
66// Clean-up the exception in catch blocks.
67void __cfaehm_cleanup_terminate(void * except);
68
69// Data structure creates a list of resume handlers.
70struct __cfaehm_try_resume_node {
71        struct __cfaehm_try_resume_node * next;
72        _Bool (*handler)(exception_t * except);
73};
74
75// These act as constructor and destructor for the resume node.
76void __cfaehm_try_resume_setup(
77        struct __cfaehm_try_resume_node * node,
78        _Bool (*handler)(exception_t * except));
79void __cfaehm_try_resume_cleanup(
80        struct __cfaehm_try_resume_node * node);
81
82// Check for a standard way to call fake deconstructors.
83struct __cfaehm_cleanup_hook {};
84
85#endif
86
87// Included in C code and the library.
88#if !defined(__cforall) || !defined(__cforall_builtins__)
89struct __cfaehm_node {
90        struct _Unwind_Exception unwind_exception;
91        struct __cfaehm_node * next;
92        int handler_index;
93};
94
95static inline exception_t * __cfaehm_cancellation_exception(
96                struct _Unwind_Exception * unwind_exception ) {
97        return (exception_t *)(1 + (struct __cfaehm_node *)unwind_exception);
98}
99#endif
100
101#ifdef __cforall
102}
103
104// Built-ins not visible in C.
105#if defined(__cforall_builtins__)
106
107// Not all the built-ins can be expressed in C. These can't be
108// implemented in the .c file either so they all have to be inline.
109
110forall( exceptT &, virtualT & )
111trait is_exception {
112        /* The first field must be a pointer to a virtual table.
113         * That virtual table must be a decendent of the base exception virtual table.
114         * The virtual table must point at the prober type-id.
115         * None of these can be enforced in an assertion.
116         */
117};
118
119forall( exceptT &, virtualT & | is_exception(exceptT, virtualT) )
120trait is_termination_exception {
121        void defaultTerminationHandler(exceptT &);
122};
123
124forall( exceptT &, virtualT & | is_exception(exceptT, virtualT) )
125trait is_resumption_exception {
126        void defaultResumptionHandler(exceptT &);
127};
128
129forall(exceptT &, virtualT & | is_termination_exception(exceptT, virtualT))
130static inline void $throw(exceptT & except) {
131        __cfaehm_throw_terminate(
132                (exception_t *)&except,
133                (void(*)(exception_t *))defaultTerminationHandler
134        );
135}
136
137forall(exceptT &, virtualT & | is_resumption_exception(exceptT, virtualT))
138static inline void $throwResume(exceptT & except) {
139        __cfaehm_throw_resume(
140                (exception_t *)&except,
141                (void(*)(exception_t *))defaultResumptionHandler
142        );
143}
144
145forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
146static inline void cancel_stack(exceptT & except) __attribute__((noreturn)) {
147        __cfaehm_cancel_stack( (exception_t *)&except );
148}
149
150forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
151static inline void defaultTerminationHandler(exceptT & except) {
152        return cancel_stack( except );
153}
154
155forall(exceptT &, virtualT & | is_termination_exception(exceptT, virtualT))
156static inline void defaultResumptionHandler(exceptT & except) {
157        throw except;
158}
159
160#endif
161
162#endif
Note: See TracBrowser for help on using the repository browser.