source: libcfa/src/exception.hfa @ 7c38d53

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

Added mark_exception to reduce the chance something is accidentally treated as an exception.

  • Property mode set to 100644
File size: 5.6 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.hfa -- User facing tools for working with exceptions.
8//
9// Author           : Andrew Beach
10// Created On       : Thu Apr  7 10:25:00 2020
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Apr 13 15:42:00 2020
13// Update Count     : 1
14//
15
16trait is_exception(dtype T) {
17        // The trait system can't describe the actual constrants.
18        // Unused, should always be a no-op.
19        void mark_exception(T *);
20};
21
22forall(dtype T | is_exception(T))
23inline void cancel_stack(T & except) __attribute__((noreturn)) {
24        __cfaehm_cancel_stack( (exception_t *)&except );
25}
26
27// Everything below this line should be considered a patch while the exception
28// objects themselves are designed and  created and should be removed in time.
29// -----------------------------------------------------------------------------------------------
30
31// All internals helper macros begin with an underscore.
32#define _CLOSE(...) __VA_ARGS__ }
33#define _GLUE2(left, right) left##right
34#define _GLUE3(left, middle, right) left##middle##right
35#define _EXC_DISPATCH(to, ...) to(__VA_ARGS__,__cfaehm_base_exception_t,)
36
37// FWD_TRIVIAL_EXCEPTION(exception_name);
38// Declare a trivial exception, one that adds no fields or features.
39// This will make the exception visible and may go in a .hfa or .cfa file.
40#define FWD_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__)
41// INST_TRIVIAL_EXCEPTION(exception_name);
42// Create the trival exception. This must be used exactly once and should be used in a .cfa file,
43// as it creates the unique instance of the virtual table.
44#define INST_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__)
45// TRIVIAL_EXCEPTION(exception_name[, parent_name]);
46// Does both of the above, a short hand if the exception is only used in one .cfa file.
47// For legacy reasons this is the only one that official supports having a parent other than the
48// base exception. This feature may be removed or changed.
49#define TRIVIAL_EXCEPTION(...) \
50        _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__); \
51        _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__)
52#define _FWD_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \
53        _VTABLE_DECLARATION(exception_name, parent_name)(); \
54        struct exception_name { \
55                VTABLE_FIELD(exception_name); \
56        }; \
57        void ?{}(exception_name & this); \
58        const char * _GLUE2(exception_name,_msg)(exception_name * this)
59#define _INST_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \
60        void ?{}(exception_name & this) { \
61                VTABLE_INIT(this, exception_name); \
62        } \
63        const char * _GLUE2(exception_name,_msg)(exception_name * this) { \
64                return #exception_name; \
65        } \
66        _VTABLE_INSTANCE(exception_name, parent_name,)(_GLUE2(exception_name,_msg))
67
68// DATA_EXCEPTION(exception_name)(fields...);
69// Forward declare an exception that adds fields but no features. The added fields go in the
70// second argument list. The virtual table instance must be provided later (see VTABLE_INSTANCE).
71#define DATA_EXCEPTION(...) _EXC_DISPATCH(_DATA_EXCEPTION, __VA_ARGS__)
72#define _DATA_EXCEPTION(exception_name, parent_name, ...) \
73        _VTABLE_DECLARATION(exception_name, parent_name)(); \
74        struct exception_name { VTABLE_FIELD(exception_name); _CLOSE
75
76// VTABLE_DECLARATION(exception_name)([new_features...]);
77// Declare a virtual table type for an exception with exception_name. You may also add features
78// (fields on the virtual table) by including them in the second list.
79#define VTABLE_DECLARATION(...) _EXC_DISPATCH(_VTABLE_DECLARATION, __VA_ARGS__)
80#define _VTABLE_DECLARATION(exception_name, parent_name, ...) \
81        struct exception_name; \
82        void mark_exception(exception_name *); \
83        VTABLE_TYPE(exception_name); \
84        extern VTABLE_TYPE(exception_name) VTABLE_NAME(exception_name); \
85        VTABLE_TYPE(exception_name) { \
86                VTABLE_TYPE(parent_name) const * parent; \
87                size_t size; \
88                void (*copy)(exception_name * this, exception_name * other); \
89                void (*free)(exception_name & this); \
90                const char * (*msg)(exception_name * this); \
91                _CLOSE
92
93// VTABLE_INSTANCE(exception_name)(msg [, others...]);
94// Create the instance of the virtual table. There must be exactly one instance of a virtual table
95// for each exception type. This fills in most of the fields of the virtual table (uses ?=? and
96// ^?{}) but you must provide the message function and any other fields added in the declaration.
97#define VTABLE_INSTANCE(...) _EXC_DISPATCH(_VTABLE_INSTANCE, __VA_ARGS__)
98#define _VTABLE_INSTANCE(exception_name, parent_name, ...) \
99        void mark_exception(exception_name *) {} \
100        void _GLUE2(exception_name,_copy)(exception_name * this, exception_name * other) { \
101                *this = *other; \
102        } \
103        VTABLE_TYPE(exception_name) VTABLE_NAME(exception_name) @= { \
104                &VTABLE_NAME(parent_name), sizeof(exception_name), \
105                _GLUE2(exception_name,_copy), ^?{}, \
106                _CLOSE
107
108// VTABLE_TYPE(exception_name) | VTABLE_NAME(exception_name)
109// Get the name of the vtable type or the name of the vtable instance for an exception type.
110#define VTABLE_TYPE(exception_name) struct _GLUE2(exception_name,_vtable)
111#define VTABLE_NAME(exception_name) _GLUE3(_,exception_name,_vtable_instance)
112
113// VTABLE_FIELD(exception_name);
114// The declaration of the virtual table field. Should be the first declaration in a virtual type.
115#define VTABLE_FIELD(exception_name) VTABLE_TYPE(exception_name) const * virtual_table
116
117// VTABLE_INIT(object_reference, exception_name);
118// Sets a virtual table field on an object to the virtual table instance for the type.
119#define VTABLE_INIT(this, exception_name) (this).virtual_table = &VTABLE_NAME(exception_name)
Note: See TracBrowser for help on using the repository browser.