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 | #define EHM_TYPE_ID(exception_name) _EHM_TYPE_ID_TYPE(exception_name) |
---|
67 | |
---|
68 | #define EHM_MATCH_ALL __cfa__parent_vtable |
---|
69 | |
---|
70 | // IS_EXCEPTION(exception_name [, (...parameters)]) |
---|
71 | // IS_RESUMPTION_EXCEPTION(exception_name [, (parameters...)]) |
---|
72 | // IS_TERMINATION_EXCEPTION(exception_name [, (parameters...)]) |
---|
73 | // Create an assertion that exception_name, possibly with the qualifing parameters, is the given |
---|
74 | // kind of exception with the standard vtable with the same parameters if applicable. |
---|
75 | #define IS_EXCEPTION(...) _IS_EXCEPTION(is_exception, __VA_ARGS__, , ~) |
---|
76 | #define IS_RESUMPTION_EXCEPTION(...) _IS_EXCEPTION(is_resumption_exception, __VA_ARGS__, , ~) |
---|
77 | #define IS_TERMINATION_EXCEPTION(...) _IS_EXCEPTION(is_termination_exception, __VA_ARGS__, , ~) |
---|
78 | |
---|
79 | // Macros starting with a leading underscore are internal. |
---|
80 | |
---|
81 | // Create an exception type definition. must be tailing, can be polymorphic. |
---|
82 | #define _EHM_EXCEPTION_STRUCT(exception_name, forall_clause, parameters) \ |
---|
83 | forall_clause struct exception_name { \ |
---|
84 | _EHM_VTABLE_TYPE(exception_name) parameters const * virtual_table; \ |
---|
85 | _CLOSE |
---|
86 | |
---|
87 | // Create a (possibly polymorphic) virtual table forward declaration. |
---|
88 | #define _EHM_EXTERN_VTABLE(exception_name, arguments, table_name) \ |
---|
89 | extern const _EHM_VTABLE_TYPE(exception_name) arguments table_name |
---|
90 | |
---|
91 | // Create a (possibly polymorphic) virtual table definition. |
---|
92 | #define _EHM_VIRTUAL_TABLE(exception_type, arguments, table_name) \ |
---|
93 | const _EHM_VTABLE_TYPE(exception_type) arguments table_name @= { \ |
---|
94 | .__cfavir_typeid : &_EHM_TYPE_ID_NAME(exception_type), \ |
---|
95 | .size : sizeof(struct exception_type arguments), \ |
---|
96 | .copy : copy, \ |
---|
97 | .^?{} : ^?{}, \ |
---|
98 | .msg : msg, \ |
---|
99 | } |
---|
100 | |
---|
101 | // Create a (possibly polymorphic) copy function from an assignment operator. |
---|
102 | #define _EHM_DEFINE_FORALL_COPY(exception_name, forall_clause, parameters) \ |
---|
103 | forall_clause void copy(exception_name parameters * this, \ |
---|
104 | exception_name parameters * that) { \ |
---|
105 | *this = *that; \ |
---|
106 | } |
---|
107 | |
---|
108 | #define _EHM_DEFINE_COPY(exception_name, arguments) \ |
---|
109 | void copy(exception_name arguments * this, exception_name arguments * that) { \ |
---|
110 | *this = *that; \ |
---|
111 | } |
---|
112 | |
---|
113 | // Create a (possibly polymorphic) msg function |
---|
114 | #define _EHM_DEFINE_FORALL_MSG(exception_name, forall_clause, parameters) \ |
---|
115 | forall_clause const char * msg(exception_name parameters * this) { \ |
---|
116 | return #exception_name #parameters; \ |
---|
117 | } |
---|
118 | |
---|
119 | #define _EHM_DEFINE_MSG(exception_name, arguments) \ |
---|
120 | const char * msg(exception_name arguments * this) { \ |
---|
121 | return #exception_name #arguments; \ |
---|
122 | } |
---|
123 | |
---|
124 | // Produces the C compatable name of the virtual table type for a virtual type. |
---|
125 | #define _EHM_VTABLE_TYPE(type_name) struct _GLUE2(type_name,_vtable) |
---|
126 | |
---|
127 | // Create the vtable type for exception name. |
---|
128 | #define _EHM_VIRTUAL_TABLE_STRUCT(exception_name, forall_clause, parameters) \ |
---|
129 | forall_clause struct exception_name; \ |
---|
130 | forall_clause _EHM_VTABLE_TYPE(exception_name) { \ |
---|
131 | _EHM_TYPE_ID_TYPE(exception_name) parameters const * __cfavir_typeid; \ |
---|
132 | size_t size; \ |
---|
133 | void (*copy)(exception_name parameters * this, exception_name parameters * other); \ |
---|
134 | void (*^?{})(exception_name parameters & this); \ |
---|
135 | const char * (*msg)(exception_name parameters * this); \ |
---|
136 | } |
---|
137 | |
---|
138 | // Define the function required to satify the trait for exceptions. |
---|
139 | #define _EHM_TRAIT_FUNCTION(exception_name, forall_clause, parameters) \ |
---|
140 | forall_clause inline void mark_exception( \ |
---|
141 | exception_name parameters const &, \ |
---|
142 | _EHM_VTABLE_TYPE(exception_name) parameters const &) {} \ |
---|
143 | |
---|
144 | #define _EHM_TRAIT_FUNCTION2(exception_name, forall_clause, parameters) \ |
---|
145 | forall_clause _EHM_VTABLE_TYPE(exception_name) parameters const & \ |
---|
146 | get_exception_vtable(exception_name parameters const & this) |
---|
147 | |
---|
148 | #define __EHM_TRAIT_FUNCTION(exception_name, forall_clause, parameters) \ |
---|
149 | forall_clause inline _EHM_VTABLE_TYPE(exception_name) parameters const & \ |
---|
150 | get_exception_vtable(exception_name parameters const & this) { \ |
---|
151 | /* This comes before the structure definition, but we know the offset. */ \ |
---|
152 | /* return (_EHM_VTABLE_TYPE(exception_name) parameters const &)this; */ \ |
---|
153 | assert(false); \ |
---|
154 | } |
---|
155 | |
---|
156 | // Generates a new type-id structure. This is used to mangle the name of the |
---|
157 | // type-id instance so it also includes polymorphic information. Must be the |
---|
158 | // direct decendent of exception_t. |
---|
159 | // The second field is used to recover type information about the exception. |
---|
160 | #define _EHM_TYPE_ID_STRUCT(exception_name, forall_clause) \ |
---|
161 | forall_clause _EHM_TYPE_ID_TYPE(exception_name) { \ |
---|
162 | __cfa__parent_vtable const * parent; \ |
---|
163 | } |
---|
164 | |
---|
165 | // Generate a new type-id value. |
---|
166 | #define _EHM_TYPE_ID_VALUE(exception_name, arguments) \ |
---|
167 | __attribute__(( section(".gnu.linkonce." "__cfatid_" #exception_name) )) \ |
---|
168 | _EHM_TYPE_ID_TYPE(exception_name) arguments const \ |
---|
169 | _EHM_TYPE_ID_NAME(exception_name) = { \ |
---|
170 | &__cfatid_exception_t, \ |
---|
171 | } |
---|
172 | |
---|
173 | // _EHM_TYPE_ID_STRUCT and _EHM_TYPE_ID_VALUE are the two that would need to |
---|
174 | // be updated to extend the hierarchy if we are still using macros when that |
---|
175 | // is added. |
---|
176 | |
---|
177 | // Produce the C compatable name of the type-id type for an exception type. |
---|
178 | #define _EHM_TYPE_ID_TYPE(exception_name) \ |
---|
179 | struct _GLUE2(__cfatid_struct_, exception_name) |
---|
180 | |
---|
181 | // Produce the name of the instance of the type-id for an exception type. |
---|
182 | #define _EHM_TYPE_ID_NAME(exception_name) _GLUE2(__cfatid_,exception_name) |
---|
183 | |
---|
184 | #define _IS_EXCEPTION(kind, exception_name, parameters, ...) \ |
---|
185 | kind(exception_name parameters, _EHM_VTABLE_TYPE(exception_name) parameters) |
---|
186 | |
---|
187 | // Internal helper macros: |
---|
188 | #define _CLOSE(...) __VA_ARGS__ } |
---|
189 | #define _GLUE2(left, right) left##right |
---|