source: tests/exceptions/except-mac.hfa @ c0b2a631

arm-ehjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since c0b2a631 was c0b2a631, checked in by Andrew Beach <ajbeach@…>, 21 months ago

Added the exceptions test folder.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1// Macros to try and make declaring and using exceptions easier
2// No, these are not part of the language, they replace the virtual system.
3
4// Internal use:
5#define GLUE2(left, right) left##right
6#define GLUE3(left, middle, right) left##middle##right
7
8// The fully (perhaps overly) qualified name of the base exception type:
9#define BASE_EXCEPT __cfaabi_ehm__base_exception_t
10
11// Get the name of the vtable type and vtable instance for an exception type:
12#define TABLE(name) GLUE2(name,_vtable)
13#define INSTANCE(name) GLUE3(_,name,_vtable_instance)
14
15// Throws and the bit of overhead:
16#define THROW(expr) throw ((BASE_EXCEPT *)(expr))
17#define THROW_RESUME(expr) throwResume ((BASE_EXCEPT *)(expr))
18
19
20
21// The following macros are for defining your own new exception types.
22
23// Declare vtable and forward declare the exception type and vtable instance.
24// This should start a new exception declaration.
25// ... argument is the additional vtable fields.
26#define DECLARE_EXCEPT(except_name,parent_name,...) \
27struct except_name; \
28struct TABLE(except_name) { \
29        struct TABLE(parent_name) const * parent; \
30        size_t size; \
31        void (*copy)(except_name *this, except_name * other); \
32        void (*free)(except_name &this); \
33        const char * (*msg)(except_name *this); \
34        __VA_ARGS__ \
35}; \
36extern TABLE(except_name) INSTANCE(except_name);
37
38// The first field of the exception structure should be created with this.
39#define VTABLE_FIELD(except_name) \
40struct TABLE(except_name) const * virtual_table
41
42// In each constructor the vtable must be initialized.
43#define VTABLE_INIT(this_name,except_name) \
44this_name.virtual_table = &INSTANCE(except_name)
45
46// Declare the vtable instance. This should end an exception declaration.
47// ... argument is the remaining vtable field values.
48#define VTABLE_INSTANCE(except_name,parent_name,copy,free,msg,...) \
49TABLE(except_name) INSTANCE(except_name) @= { \
50        &INSTANCE(parent_name), sizeof(except_name), \
51        copy, free, msg, ## __VA_ARGS__ \
52};
53
54// Same, but used declarators for arguments.
55#define VTABLE_INSTANCE_KEY(except_name,parent_name,copy,free,msg,...) \
56TABLE(except_name) INSTANCE(except_name) @= { \
57        .parent : &INSTANCE(parent_name), .size : sizeof(except_name), \
58        .copy : copy, .free : free, .msg : msg, ## __VA_ARGS__ \
59};
60
61
62
63// Declare a trivial exception, one that adds no features:
64#define TRIVIAL_EXCEPTION(name) \
65DECLARE_EXCEPT(name,BASE_EXCEPT,) \
66struct name { \
67        VTABLE_FIELD(name); \
68}; \
69const char * GLUE2(name,_msg)(name * this) { \
70    return #name; \
71} \
72void GLUE2(name,_copy)(name * this, name * other) { \
73    this->virtual_table = other->virtual_table; \
74} \
75void ?{}(name & this) { \
76        VTABLE_INIT(this,name); \
77} \
78VTABLE_INSTANCE(name,BASE_EXCEPT,GLUE2(name,_copy),^?{},GLUE2(name,_msg),)
Note: See TracBrowser for help on using the repository browser.