source: libcfa/src/exception.h @ 2554f24

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

Try terminate now does not call the catch function, now they have the same caller. This involved updating some platform dependent code which should be correct in all cases.

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