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 | |
---|
16 | // WARNING: This is for documentation as it will match ANY type. |
---|
17 | trait is_exception(dtype T) { |
---|
18 | /* The first field must be a pointer to a virtual table. |
---|
19 | * That virtual table must be a decendent of the base exception virtual table. |
---|
20 | */ |
---|
21 | }; |
---|
22 | |
---|
23 | forall(dtype T | is_exception(T)) |
---|
24 | inline void cancel_stack(T & except) __attribute__((noreturn)) { |
---|
25 | __cfaehm_cancel_stack( (exception_t *)&except ); |
---|
26 | } |
---|
27 | |
---|
28 | // Everything below this line should be considered a patch while the exception |
---|
29 | // objects themselves are designed and created and should be removed in time. |
---|
30 | // ----------------------------------------------------------------------------------------------- |
---|
31 | |
---|
32 | // All internals helper macros begin with an underscore. |
---|
33 | #define _CLOSE(...) __VA_ARGS__ } |
---|
34 | #define _GLUE2(left, right) left##right |
---|
35 | #define _GLUE3(left, middle, right) left##middle##right |
---|
36 | #define _EXC_DISPATCH(to, ...) to(__VA_ARGS__,__cfaehm_base_exception_t,) |
---|
37 | |
---|
38 | // FWD_TRIVIAL_EXCEPTION(exception_name); |
---|
39 | // Declare a trivial exception, one that adds no fields or features. |
---|
40 | // This will make the exception visible and may go in a .hfa or .cfa file. |
---|
41 | #define FWD_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__) |
---|
42 | // INST_TRIVIAL_EXCEPTION(exception_name); |
---|
43 | // Create the trival exception. This must be used exactly once and should be used in a .cfa file, |
---|
44 | // as it creates the unique instance of the virtual table. |
---|
45 | #define INST_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__) |
---|
46 | // TRIVIAL_EXCEPTION(exception_name[, parent_name]); |
---|
47 | // Does both of the above, a short hand if the exception is only used in one .cfa file. |
---|
48 | // For legacy reasons this is the only one that official supports having a parent other than the |
---|
49 | // base exception. This feature may be removed or changed. |
---|
50 | #define TRIVIAL_EXCEPTION(...) \ |
---|
51 | _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__); \ |
---|
52 | _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__) |
---|
53 | #define _FWD_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \ |
---|
54 | _VTABLE_DECLARATION(exception_name, parent_name)(); \ |
---|
55 | struct exception_name { \ |
---|
56 | VTABLE_FIELD(exception_name); \ |
---|
57 | }; \ |
---|
58 | void ?{}(exception_name & this); \ |
---|
59 | const char * _GLUE2(exception_name,_msg)(exception_name * this) |
---|
60 | #define _INST_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \ |
---|
61 | void ?{}(exception_name & this) { \ |
---|
62 | VTABLE_INIT(this, exception_name); \ |
---|
63 | } \ |
---|
64 | const char * _GLUE2(exception_name,_msg)(exception_name * this) { \ |
---|
65 | return #exception_name; \ |
---|
66 | } \ |
---|
67 | _VTABLE_INSTANCE(exception_name, parent_name,)(_GLUE2(exception_name,_msg)) |
---|
68 | |
---|
69 | // DATA_EXCEPTION(exception_name)(fields...); |
---|
70 | // Forward declare an exception that adds fields but no features. The added fields go in the |
---|
71 | // second argument list. The virtual table instance must be provided later (see VTABLE_INSTANCE). |
---|
72 | #define DATA_EXCEPTION(...) _EXC_DISPATCH(_DATA_EXCEPTION, __VA_ARGS__) |
---|
73 | #define _DATA_EXCEPTION(exception_name, parent_name, ...) \ |
---|
74 | _VTABLE_DECLARATION(exception_name, parent_name)(); \ |
---|
75 | struct exception_name { VTABLE_FIELD(exception_name); _CLOSE |
---|
76 | |
---|
77 | // VTABLE_DECLARATION(exception_name)([new_features...]); |
---|
78 | // Declare a virtual table type for an exception with exception_name. You may also add features |
---|
79 | // (fields on the virtual table) by including them in the second list. |
---|
80 | #define VTABLE_DECLARATION(...) _EXC_DISPATCH(_VTABLE_DECLARATION, __VA_ARGS__) |
---|
81 | #define _VTABLE_DECLARATION(exception_name, parent_name, ...) \ |
---|
82 | struct 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 _GLUE2(exception_name,_copy)(exception_name * this, exception_name * other) { \ |
---|
100 | *this = *other; \ |
---|
101 | } \ |
---|
102 | VTABLE_TYPE(exception_name) VTABLE_NAME(exception_name) @= { \ |
---|
103 | &VTABLE_NAME(parent_name), sizeof(exception_name), \ |
---|
104 | _GLUE2(exception_name,_copy), ^?{}, \ |
---|
105 | _CLOSE |
---|
106 | |
---|
107 | // VTABLE_TYPE(exception_name) | VTABLE_NAME(exception_name) |
---|
108 | // Get the name of the vtable type or the name of the vtable instance for an exception type. |
---|
109 | #define VTABLE_TYPE(exception_name) struct _GLUE2(exception_name,_vtable) |
---|
110 | #define VTABLE_NAME(exception_name) _GLUE3(_,exception_name,_vtable_instance) |
---|
111 | |
---|
112 | // VTABLE_FIELD(exception_name); |
---|
113 | // The declaration of the virtual table field. Should be the first declaration in a virtual type. |
---|
114 | #define VTABLE_FIELD(exception_name) VTABLE_TYPE(exception_name) const * virtual_table |
---|
115 | |
---|
116 | // VTABLE_INIT(object_reference, exception_name); |
---|
117 | // Sets a virtual table field on an object to the virtual table instance for the type. |
---|
118 | #define VTABLE_INIT(this, exception_name) (this).virtual_table = &VTABLE_NAME(exception_name) |
---|