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

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 7b2c8c3c was 8f910430, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Updated the virtual module to prefix the names with the new convention and change the parent_vtable into type_info.

  • Property mode set to 100644
File size: 8.3 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 : Thr Apr  8 15:16:00 2021
13// Update Count     : 4
14//
15
16// Everything below this line should be considered a patch while the exception
17// objects themselves are designed and  created and should be removed in time.
18// -----------------------------------------------------------------------------------------------
19
20// EHM_EXCEPTION(exception_name)(fields...);
21// Create an exception (a virtual structure that inherits from exception_t)
22// with the given name and fields.
23#define EHM_EXCEPTION(exception_name) \
24        _EHM_TYPE_ID_STRUCT(exception_name, ); \
25        _EHM_TYPE_ID_VALUE(exception_name, ); \
26        _EHM_VIRTUAL_TABLE_STRUCT(exception_name, , ); \
27        _EHM_EXCEPTION_STRUCT(exception_name, , )
28
29// EHM_EXTERN_VTABLE(exception_name, table_name);
30// Forward declare a virtual table called table_name for exception_name type.
31#define EHM_EXTERN_VTABLE(exception_name, table_name) \
32        _EHM_EXTERN_VTABLE(exception_name, , table_name)
33
34// EHM_VIRTUAL_TABLE(exception_name, table_name);
35// Define a virtual table called table_name for exception_name type.
36#define EHM_VIRTUAL_TABLE(exception_name, table_name) \
37        _EHM_DEFINE_COPY(exception_name, ) \
38        _EHM_DEFINE_MSG(exception_name, ) \
39        _EHM_VIRTUAL_TABLE(exception_name, , table_name)
40
41// EHM_FORALL_EXCEPTION(exception_name, (assertions), (parameters))(fields...);
42// As EHM_EXCEPTION but for polymorphic types instead of monomorphic ones.
43// The assertions list should include all polymorphic parameters and
44// assertions inside a parentisized list. Parameters should include all the
45// polymorphic parameter names inside a parentisized list (same order).
46#define EHM_FORALL_EXCEPTION(exception_name, assertions, parameters) \
47        _EHM_TYPE_ID_STRUCT(exception_name, forall assertions); \
48        _EHM_VIRTUAL_TABLE_STRUCT(exception_name, forall assertions, parameters); \
49        _EHM_EXCEPTION_STRUCT(exception_name, forall assertions, parameters)
50
51// EHM_FORALL_EXTERN_VTABLE(exception_name, (arguments), table_name);
52// As EHM_EXTERN_VTABLE but for polymorphic types instead of monomorphic ones.
53// Arguments should be the parentisized list of polymorphic arguments.
54#define EHM_FORALL_EXTERN_VTABLE(exception_name, arguments, table_name) \
55        _EHM_EXTERN_VTABLE(exception_name, arguments, table_name)
56
57// EHM_FORALL_VIRTUAL_TABLE(exception_name, (arguments), table_name);
58// As EHM_VIRTUAL_TABLE but for polymorphic types instead of monomorphic ones.
59// Arguments should be the parentisized list of polymorphic arguments.
60#define EHM_FORALL_VIRTUAL_TABLE(exception_name, arguments, table_name) \
61        _EHM_TYPE_ID_VALUE(exception_name, arguments); \
62        _EHM_DEFINE_COPY(exception_name, arguments) \
63        _EHM_DEFINE_MSG(exception_name, arguments) \
64        _EHM_VIRTUAL_TABLE(exception_name, arguments, table_name)
65
66// EHM_DEFAULT_VTABLE(exception_name, (arguments))
67// Create a declaration for a (possibly polymorphic) default vtable.
68#define EHM_DEFAULT_VTABLE(exception_name, arguments) \
69        _EHM_VTABLE_TYPE(exception_name) arguments & const _default_vtable
70
71// IS_EXCEPTION(exception_name [, (...parameters)])
72// IS_RESUMPTION_EXCEPTION(exception_name [, (parameters...)])
73// IS_TERMINATION_EXCEPTION(exception_name [, (parameters...)])
74// Create an assertion that exception_name, possibly with the qualifing parameters, is the given
75// kind of exception with the standard vtable with the same parameters if applicable.
76#define IS_EXCEPTION(...) _IS_EXCEPTION(is_exception, __VA_ARGS__, , ~)
77#define IS_RESUMPTION_EXCEPTION(...) _IS_EXCEPTION(is_resumption_exception, __VA_ARGS__, , ~)
78#define IS_TERMINATION_EXCEPTION(...) _IS_EXCEPTION(is_termination_exception, __VA_ARGS__, , ~)
79
80// Macros starting with a leading underscore are internal.
81
82// Create an exception type definition. must be tailing, can be polymorphic.
83#define _EHM_EXCEPTION_STRUCT(exception_name, forall_clause, parameters) \
84        forall_clause struct exception_name { \
85                _EHM_VTABLE_TYPE(exception_name) parameters const * virtual_table; \
86                _CLOSE
87
88// Create a (possibly polymorphic) virtual table forward declaration.
89#define _EHM_EXTERN_VTABLE(exception_name, arguments, table_name) \
90        extern const _EHM_VTABLE_TYPE(exception_name) arguments table_name
91
92// Create a (possibly polymorphic) virtual table definition.
93#define _EHM_VIRTUAL_TABLE(exception_type, arguments, table_name) \
94        const _EHM_VTABLE_TYPE(exception_type) arguments table_name @= { \
95                .__cfavir_typeid : &_EHM_TYPE_ID_NAME(exception_type), \
96                .size : sizeof(struct exception_type arguments), \
97                .copy : copy, \
98                .^?{} : ^?{}, \
99                .msg : msg, \
100        }
101
102// Create a (possibly polymorphic) copy function from an assignment operator.
103#define _EHM_DEFINE_FORALL_COPY(exception_name, forall_clause, parameters) \
104        forall_clause void copy(exception_name parameters * this, \
105                        exception_name parameters * that) { \
106                *this = *that; \
107        }
108
109#define _EHM_DEFINE_COPY(exception_name, arguments) \
110        void copy(exception_name arguments * this, exception_name arguments * that) { \
111                *this = *that; \
112        }
113
114// Create a (possibly polymorphic) msg function
115#define _EHM_DEFINE_FORALL_MSG(exception_name, forall_clause, parameters) \
116        forall_clause const char * msg(exception_name parameters * this) { \
117                return #exception_name #parameters; \
118        }
119
120#define _EHM_DEFINE_MSG(exception_name, arguments) \
121        const char * msg(exception_name arguments * this) { \
122                return #exception_name #arguments; \
123        }
124
125// Produces the C compatable name of the virtual table type for a virtual type.
126#define _EHM_VTABLE_TYPE(type_name) struct _GLUE2(type_name,_vtable)
127
128// Create the vtable type for exception name.
129#define _EHM_VIRTUAL_TABLE_STRUCT(exception_name, forall_clause, parameters) \
130        forall_clause struct exception_name; \
131        forall_clause _EHM_VTABLE_TYPE(exception_name) { \
132                _EHM_TYPE_ID_TYPE(exception_name) parameters const * __cfavir_typeid; \
133                size_t size; \
134                void (*copy)(exception_name parameters * this, exception_name parameters * other); \
135                void (*^?{})(exception_name parameters & this); \
136                const char * (*msg)(exception_name parameters * this); \
137        }
138
139// Define the function required to satify the trait for exceptions.
140#define _EHM_TRAIT_FUNCTION(exception_name, forall_clause, parameters) \
141        forall_clause inline void mark_exception( \
142                exception_name parameters const &, \
143                _EHM_VTABLE_TYPE(exception_name) parameters const &) {} \
144
145#define __EHM_TRAIT_FUNCTION(exception_name, forall_clause, parameters) \
146        forall_clause inline _EHM_VTABLE_TYPE(exception_name) parameters const & \
147                        get_exception_vtable(exception_name parameters const & this) { \
148                /* This comes before the structure definition, but we know the offset. */ \
149                /* return (_EHM_VTABLE_TYPE(exception_name) parameters const &)this; */ \
150                assert(false); \
151        }
152
153// Generates a new type-id structure. This is used to mangle the name of the
154// type-id instance so it also includes polymorphic information. Must be the
155// direct decendent of exception_t.
156// The second field is used to recover type information about the exception.
157#define _EHM_TYPE_ID_STRUCT(exception_name, forall_clause) \
158        forall_clause _EHM_TYPE_ID_TYPE(exception_name) { \
159                __cfavir_type_info const * parent; \
160        }
161
162// Generate a new type-id value.
163#define _EHM_TYPE_ID_VALUE(exception_name, arguments) \
164        __attribute__((cfa_linkonce)) \
165        _EHM_TYPE_ID_TYPE(exception_name) arguments const \
166                        _EHM_TYPE_ID_NAME(exception_name) = { \
167                &__cfatid_exception_t, \
168        }
169
170// _EHM_TYPE_ID_STRUCT and _EHM_TYPE_ID_VALUE are the two that would need to
171// be updated to extend the hierarchy if we are still using macros when that
172// is added.
173
174// Produce the C compatable name of the type-id type for an exception type.
175#define _EHM_TYPE_ID_TYPE(exception_name) \
176        struct _GLUE2(__cfatid_struct_, exception_name)
177
178// Produce the name of the instance of the type-id for an exception type.
179#define _EHM_TYPE_ID_NAME(exception_name) _GLUE2(__cfatid_,exception_name)
180
181#define _IS_EXCEPTION(kind, exception_name, parameters, ...) \
182        kind(exception_name parameters, _EHM_VTABLE_TYPE(exception_name) parameters)
183
184// Internal helper macros:
185#define _CLOSE(...) __VA_ARGS__ }
186#define _GLUE2(left, right) left##right
Note: See TracBrowser for help on using the repository browser.