source: libcfa/src/exception.h @ fd54fef

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since fd54fef was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[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//
[c960331]7// exception.h -- Internal exception handling definitions.
[fa4805f]8//
9// Author           : Andrew Beach
10// Created On       : Mon Jun 26 15:11:00 2017
[3090127]11// Last Modified By : Andrew Beach
[c960331]12// Last Modified On : Tue Oct 27 14:45:00 2020
13// Update Count     : 11
[fa4805f]14//
15
[6b0b624]16#pragma once
[fa4805f]17
[c960331]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.
[fa4805f]21
[0cf5b79]22#ifdef __cforall
[0fc9756b]23extern "C" {
[fa4805f]24#endif
25
[c960331]26// Included in C code or the built-ins.
27#if !defined(__cforall) || defined(__cforall_builtins__)
28
[3090127]29struct __cfaehm_base_exception_t;
30typedef struct __cfaehm_base_exception_t exception_t;
31struct __cfaehm_base_exception_t_vtable {
32        const struct __cfaehm_base_exception_t_vtable * parent;
[86d5ba7c]33        size_t size;
[3090127]34        void (*copy)(struct __cfaehm_base_exception_t *this,
35                     struct __cfaehm_base_exception_t * other);
36        void (*free)(struct __cfaehm_base_exception_t *this);
37        const char * (*msg)(struct __cfaehm_base_exception_t *this);
[86d5ba7c]38};
[3090127]39struct __cfaehm_base_exception_t {
40        struct __cfaehm_base_exception_t_vtable const * virtual_table;
[86d5ba7c]41};
[3090127]42extern struct __cfaehm_base_exception_t_vtable
43        ___cfaehm_base_exception_t_vtable_instance;
[86d5ba7c]44
45
[979df46]46void __cfaehm_cancel_stack(exception_t * except) __attribute__((noreturn));
47
[fa4805f]48// Used in throw statement translation.
[046a890]49void __cfaehm_throw_terminate(exception_t * except, void (*)(exception_t *));
[3090127]50void __cfaehm_rethrow_terminate() __attribute__((noreturn));
[046a890]51void __cfaehm_throw_resume(exception_t * except, void (*)(exception_t *));
[fa4805f]52
53// Function catches termination exceptions.
[3090127]54void __cfaehm_try_terminate(
[c960331]55        void (*try_block)(),
56        void (*catch_block)(int index, exception_t * except),
57        int (*match_block)(exception_t * except));
[fa4805f]58
[86d5ba7c]59// Clean-up the exception in catch blocks.
[3090127]60void __cfaehm_cleanup_terminate(void * except);
[86d5ba7c]61
[fa4805f]62// Data structure creates a list of resume handlers.
[3090127]63struct __cfaehm_try_resume_node {
[c960331]64        struct __cfaehm_try_resume_node * next;
65        _Bool (*handler)(exception_t * except);
[fa4805f]66};
67
[86d5ba7c]68// These act as constructor and destructor for the resume node.
[3090127]69void __cfaehm_try_resume_setup(
[c960331]70        struct __cfaehm_try_resume_node * node,
71        _Bool (*handler)(exception_t * except));
[3090127]72void __cfaehm_try_resume_cleanup(
[c960331]73        struct __cfaehm_try_resume_node * node);
[fa4805f]74
75// Check for a standard way to call fake deconstructors.
[3090127]76struct __cfaehm_cleanup_hook {};
[fa4805f]77
[c960331]78#endif
79
80// Included in C code and the library.
81#if !defined(__cforall) || !defined(__cforall_builtins__)
82struct __cfaehm_node {
83        struct _Unwind_Exception unwind_exception;
84        struct __cfaehm_node * next;
85        int handler_index;
86};
87
88static 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
[0cf5b79]94#ifdef __cforall
[fa4805f]95}
[046a890]96
[c960331]97// Built-ins not visible in C.
98#if defined(__cforall_builtins__)
99
[046a890]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
[fd54fef]103trait is_exception(exceptT &, virtualT &) {
[046a890]104        /* The first field must be a pointer to a virtual table.
[69c5c00]105         * That virtual table must be a decendent of the base exception virtual table.
[046a890]106         */
[69c5c00]107        virtualT const & get_exception_vtable(exceptT *);
108        // Always returns the virtual table for this type (associated types hack).
[046a890]109};
110
[fd54fef]111trait is_termination_exception(exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
[1c01c58]112        void defaultTerminationHandler(exceptT &);
[046a890]113};
114
[fd54fef]115trait is_resumption_exception(exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
[1c01c58]116        void defaultResumptionHandler(exceptT &);
[046a890]117};
118
[fd54fef]119forall(exceptT &, virtualT & | is_termination_exception(exceptT, virtualT))
[1c01c58]120static inline void $throw(exceptT & except) {
[046a890]121        __cfaehm_throw_terminate(
122                (exception_t *)&except,
123                (void(*)(exception_t *))defaultTerminationHandler
124        );
125}
126
[fd54fef]127forall(exceptT &, virtualT & | is_resumption_exception(exceptT, virtualT))
[1c01c58]128static inline void $throwResume(exceptT & except) {
[046a890]129        __cfaehm_throw_resume(
130                (exception_t *)&except,
131                (void(*)(exception_t *))defaultResumptionHandler
132        );
133}
134
[fd54fef]135forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
[1c01c58]136static inline void cancel_stack(exceptT & except) __attribute__((noreturn)) {
[046a890]137        __cfaehm_cancel_stack( (exception_t *)&except );
138}
139
[fd54fef]140forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
[1c01c58]141static inline void defaultTerminationHandler(exceptT & except) {
[046a890]142        return cancel_stack( except );
143}
144
[fd54fef]145forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
[1c01c58]146static inline void defaultResumptionHandler(exceptT & except) {
[046a890]147        throw except;
148}
149
[fa4805f]150#endif
[c960331]151
152#endif
Note: See TracBrowser for help on using the repository browser.