source: libcfa/src/exception.hfa @ 046a890

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

That should get default operations working for throws. More tests to come.

  • Property mode set to 100644
File size: 5.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 : Tue May 19 14:17:00 2020
13// Update Count     : 2
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// All internals helper macros begin with an underscore.
21#define _CLOSE(...) __VA_ARGS__ }
22#define _GLUE2(left, right) left##right
23#define _GLUE3(left, middle, right) left##middle##right
24#define _EXC_DISPATCH(to, ...) to(__VA_ARGS__,__cfaehm_base_exception_t,)
25
26// FWD_TRIVIAL_EXCEPTION(exception_name);
27// Declare a trivial exception, one that adds no fields or features.
28// This will make the exception visible and may go in a .hfa or .cfa file.
29#define FWD_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__)
30// INST_TRIVIAL_EXCEPTION(exception_name);
31// Create the trival exception. This must be used exactly once and should be used in a .cfa file,
32// as it creates the unique instance of the virtual table.
33#define INST_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__)
34// TRIVIAL_EXCEPTION(exception_name[, parent_name]);
35// Does both of the above, a short hand if the exception is only used in one .cfa file.
36// For legacy reasons this is the only one that official supports having a parent other than the
37// base exception. This feature may be removed or changed.
38#define TRIVIAL_EXCEPTION(...) \
39        _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__); \
40        _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__)
41#define _FWD_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \
42        _VTABLE_DECLARATION(exception_name, parent_name)(); \
43        struct exception_name { \
44                VTABLE_FIELD(exception_name); \
45        }; \
46        void ?{}(exception_name & this); \
47        const char * _GLUE2(exception_name,_msg)(exception_name * this)
48#define _INST_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \
49        void ?{}(exception_name & this) { \
50                VTABLE_INIT(this, exception_name); \
51        } \
52        const char * _GLUE2(exception_name,_msg)(exception_name * this) { \
53                return #exception_name; \
54        } \
55        _VTABLE_INSTANCE(exception_name, parent_name,)(_GLUE2(exception_name,_msg))
56
57// DATA_EXCEPTION(exception_name)(fields...);
58// Forward declare an exception that adds fields but no features. The added fields go in the
59// second argument list. The virtual table instance must be provided later (see VTABLE_INSTANCE).
60#define DATA_EXCEPTION(...) _EXC_DISPATCH(_DATA_EXCEPTION, __VA_ARGS__)
61#define _DATA_EXCEPTION(exception_name, parent_name, ...) \
62        _VTABLE_DECLARATION(exception_name, parent_name)(); \
63        struct exception_name { VTABLE_FIELD(exception_name); _CLOSE
64
65// VTABLE_DECLARATION(exception_name)([new_features...]);
66// Declare a virtual table type for an exception with exception_name. You may also add features
67// (fields on the virtual table) by including them in the second list.
68#define VTABLE_DECLARATION(...) _EXC_DISPATCH(_VTABLE_DECLARATION, __VA_ARGS__)
69#define _VTABLE_DECLARATION(exception_name, parent_name, ...) \
70        struct exception_name; \
71        void mark_exception(exception_name *); \
72        VTABLE_TYPE(exception_name); \
73        extern VTABLE_TYPE(exception_name) VTABLE_NAME(exception_name); \
74        VTABLE_TYPE(exception_name) { \
75                VTABLE_TYPE(parent_name) const * parent; \
76                size_t size; \
77                void (*copy)(exception_name * this, exception_name * other); \
78                void (*free)(exception_name & this); \
79                const char * (*msg)(exception_name * this); \
80                _CLOSE
81
82// VTABLE_INSTANCE(exception_name)(msg [, others...]);
83// Create the instance of the virtual table. There must be exactly one instance of a virtual table
84// for each exception type. This fills in most of the fields of the virtual table (uses ?=? and
85// ^?{}) but you must provide the message function and any other fields added in the declaration.
86#define VTABLE_INSTANCE(...) _EXC_DISPATCH(_VTABLE_INSTANCE, __VA_ARGS__)
87#define _VTABLE_INSTANCE(exception_name, parent_name, ...) \
88        void mark_exception(exception_name *) {} \
89        void _GLUE2(exception_name,_copy)(exception_name * this, exception_name * other) { \
90                *this = *other; \
91        } \
92        VTABLE_TYPE(exception_name) VTABLE_NAME(exception_name) @= { \
93                &VTABLE_NAME(parent_name), sizeof(exception_name), \
94                _GLUE2(exception_name,_copy), ^?{}, \
95                _CLOSE
96
97// VTABLE_TYPE(exception_name) | VTABLE_NAME(exception_name)
98// Get the name of the vtable type or the name of the vtable instance for an exception type.
99#define VTABLE_TYPE(exception_name) struct _GLUE2(exception_name,_vtable)
100#define VTABLE_NAME(exception_name) _GLUE3(_,exception_name,_vtable_instance)
101
102// VTABLE_FIELD(exception_name);
103// The declaration of the virtual table field. Should be the first declaration in a virtual type.
104#define VTABLE_FIELD(exception_name) VTABLE_TYPE(exception_name) const * virtual_table
105
106// VTABLE_INIT(object_reference, exception_name);
107// Sets a virtual table field on an object to the virtual table instance for the type.
108#define VTABLE_INIT(this, exception_name) (this).virtual_table = &VTABLE_NAME(exception_name)
Note: See TracBrowser for help on using the repository browser.