source: libcfa/src/exception.h @ 1c01c58

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 1c01c58 was 1c01c58, checked in by Andrew Beach <ajbeach@…>, 17 months ago

Rather large commit to get coroutine cancellation working.

This includes what you would expect, like new code in exceptions and a new
test, but it also includes a bunch of other things.

New coroutine state, currently just marks that the stack was cancelled. New
helpers for checking code structure and generating vtables. Changes to the
coroutine interface so resume may throw exceptions on cancellation, plus the
exception type that is thrown. Changes to the coroutine keyword generation to
generate exception code for each type of coroutine.

  • Property mode set to 100644
File size: 3.7 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 -- Builtins for exception handling.
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 : Tue May 19 14:17:00 2020
13// Update Count     : 10
14//
15
16#pragma once
17
18
19#ifdef __cforall
20extern "C" {
21#endif
22
23struct __cfaehm_base_exception_t;
24typedef struct __cfaehm_base_exception_t exception_t;
25struct __cfaehm_base_exception_t_vtable {
26        const struct __cfaehm_base_exception_t_vtable * parent;
27        size_t size;
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);
32};
33struct __cfaehm_base_exception_t {
34        struct __cfaehm_base_exception_t_vtable const * virtual_table;
35};
36extern struct __cfaehm_base_exception_t_vtable
37        ___cfaehm_base_exception_t_vtable_instance;
38
39
40void __cfaehm_cancel_stack(exception_t * except) __attribute__((noreturn));
41
42// Used in throw statement translation.
43void __cfaehm_throw_terminate(exception_t * except, void (*)(exception_t *));
44void __cfaehm_rethrow_terminate() __attribute__((noreturn));
45void __cfaehm_throw_resume(exception_t * except, void (*)(exception_t *));
46
47// Function catches termination exceptions.
48void __cfaehm_try_terminate(
49    void (*try_block)(),
50    void (*catch_block)(int index, exception_t * except),
51    int (*match_block)(exception_t * except));
52
53// Clean-up the exception in catch blocks.
54void __cfaehm_cleanup_terminate(void * except);
55
56// Data structure creates a list of resume handlers.
57struct __cfaehm_try_resume_node {
58    struct __cfaehm_try_resume_node * next;
59    _Bool (*handler)(exception_t * except);
60};
61
62// These act as constructor and destructor for the resume node.
63void __cfaehm_try_resume_setup(
64    struct __cfaehm_try_resume_node * node,
65    _Bool (*handler)(exception_t * except));
66void __cfaehm_try_resume_cleanup(
67    struct __cfaehm_try_resume_node * node);
68
69// Check for a standard way to call fake deconstructors.
70struct __cfaehm_cleanup_hook {};
71
72#ifdef __cforall
73}
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
78trait is_exception(dtype exceptT) {
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(exceptT *);
83        // This is never used and should be a no-op.
84};
85
86trait is_termination_exception(dtype exceptT | is_exception(exceptT)) {
87        void defaultTerminationHandler(exceptT &);
88};
89
90trait is_resumption_exception(dtype exceptT | is_exception(exceptT)) {
91        void defaultResumptionHandler(exceptT &);
92};
93
94forall(dtype exceptT | is_termination_exception(exceptT))
95static inline void $throw(exceptT & except) {
96        __cfaehm_throw_terminate(
97                (exception_t *)&except,
98                (void(*)(exception_t *))defaultTerminationHandler
99        );
100}
101
102forall(dtype exceptT | is_resumption_exception(exceptT))
103static inline void $throwResume(exceptT & except) {
104        __cfaehm_throw_resume(
105                (exception_t *)&except,
106                (void(*)(exception_t *))defaultResumptionHandler
107        );
108}
109
110forall(dtype exceptT | is_exception(exceptT))
111static inline void cancel_stack(exceptT & except) __attribute__((noreturn)) {
112        __cfaehm_cancel_stack( (exception_t *)&except );
113}
114
115forall(dtype exceptT | is_exception(exceptT))
116static inline void defaultTerminationHandler(exceptT & except) {
117        return cancel_stack( except );
118}
119
120forall(dtype exceptT | is_exception(exceptT))
121static inline void defaultResumptionHandler(exceptT & except) {
122        throw except;
123}
124
125#endif
Note: See TracBrowser for help on using the repository browser.