diff options
| author | kr.angelov <kr.angelov@gmail.com> | 2014-10-09 13:44:26 +0000 |
|---|---|---|
| committer | kr.angelov <kr.angelov@gmail.com> | 2014-10-09 13:44:26 +0000 |
| commit | 86e9acc7a7b714307e08ab25117ca03cecb00936 (patch) | |
| tree | 38dd7904147567c4e97e1f62eca64739d3a516eb /src/runtime/c/gu | |
| parent | 8e3ed825a8dde13894a44169ddc09cc2c2be60b2 (diff) | |
throw away the long obsolete runtime type information in the C runtime
Diffstat (limited to 'src/runtime/c/gu')
| -rw-r--r-- | src/runtime/c/gu/bits.c | 2 | ||||
| -rw-r--r-- | src/runtime/c/gu/bits.h | 3 | ||||
| -rw-r--r-- | src/runtime/c/gu/exn.c | 38 | ||||
| -rw-r--r-- | src/runtime/c/gu/exn.h | 29 | ||||
| -rw-r--r-- | src/runtime/c/gu/in.c | 4 | ||||
| -rw-r--r-- | src/runtime/c/gu/in.h | 4 | ||||
| -rw-r--r-- | src/runtime/c/gu/map.c | 17 | ||||
| -rw-r--r-- | src/runtime/c/gu/map.h | 41 | ||||
| -rw-r--r-- | src/runtime/c/gu/out.c | 2 | ||||
| -rw-r--r-- | src/runtime/c/gu/seq.c | 5 | ||||
| -rw-r--r-- | src/runtime/c/gu/str.c | 2 | ||||
| -rw-r--r-- | src/runtime/c/gu/str.h | 4 | ||||
| -rw-r--r-- | src/runtime/c/gu/string.c | 5 | ||||
| -rw-r--r-- | src/runtime/c/gu/string.h | 22 | ||||
| -rw-r--r-- | src/runtime/c/gu/type.c | 228 | ||||
| -rw-r--r-- | src/runtime/c/gu/type.h | 474 | ||||
| -rw-r--r-- | src/runtime/c/gu/ucs.c | 2 | ||||
| -rw-r--r-- | src/runtime/c/gu/ucs.h | 2 | ||||
| -rw-r--r-- | src/runtime/c/gu/utf8.c | 12 | ||||
| -rw-r--r-- | src/runtime/c/gu/variant.c | 3 | ||||
| -rw-r--r-- | src/runtime/c/gu/variant.h | 60 |
21 files changed, 30 insertions, 929 deletions
diff --git a/src/runtime/c/gu/bits.c b/src/runtime/c/gu/bits.c index 398b3fdd5..6197983cc 100644 --- a/src/runtime/c/gu/bits.c +++ b/src/runtime/c/gu/bits.c @@ -24,8 +24,6 @@ unsigned gu_ceil2e(unsigned u) return u; } -GU_DEFINE_TYPE(GuIntDecodeExn, abstract, _); - double gu_decode_double(uint64_t u) { diff --git a/src/runtime/c/gu/bits.h b/src/runtime/c/gu/bits.h index 5a55ff233..c7f94c17c 100644 --- a/src/runtime/c/gu/bits.h +++ b/src/runtime/c/gu/bits.h @@ -103,9 +103,6 @@ gu_tagged(void* ptr, size_t tag) { } #include <gu/exn.h> -#include <gu/type.h> - -extern GU_DECLARE_TYPE(GuIntDecodeExn, abstract); #define GU_DECODE_2C_(u_, t_, umax_, posmax_, tmin_, err_) \ (((u_) <= (posmax_)) \ diff --git a/src/runtime/c/gu/exn.c b/src/runtime/c/gu/exn.c index 003a4357f..1b8b4f4a5 100644 --- a/src/runtime/c/gu/exn.c +++ b/src/runtime/c/gu/exn.c @@ -3,12 +3,10 @@ GuExn* -gu_new_exn(GuExn* parent, GuKind* catch, GuPool* pool) +gu_new_exn(GuPool* pool) { GuExn* exn = gu_new(GuExn, pool); exn->state = GU_EXN_OK; - exn->parent = parent; - exn->catch = catch; exn->caught = NULL; exn->data.pool = pool; exn->data.data = NULL; @@ -20,6 +18,12 @@ gu_exn_is_raised(GuExn* err) { return err && (err->state == GU_EXN_RAISED); } +bool +gu_exn_caught_(GuExn* err, const char* type) +{ + return (err->caught && strcmp(err->caught, type) == 0); +} + void gu_exn_block(GuExn* err) { @@ -37,24 +41,11 @@ gu_exn_unblock(GuExn* err) } GuExnData* -gu_exn_raise_debug_(GuExn* base, GuType* type, - const char* filename, const char* func, int lineno) +gu_exn_raise_debug_(GuExn* err, const char* type, + const char* filename, const char* func, int lineno) { gu_require(type); - // TODO: log the error, once there's a system for dumping - // error objects. - - GuExn* err = base; - - while (err && !(err->catch && gu_type_has_kind(type, err->catch))) { - err->state = GU_EXN_RAISED; - err = err->parent; - } - if (!err) { - gu_abort_(GU_ASSERT_ASSERTION, filename, func, lineno, - "Unexpected error raised"); - } GuExnState old_state = err->state; err->state = GU_EXN_RAISED; if (old_state == GU_EXN_OK) { @@ -63,23 +54,18 @@ gu_exn_raise_debug_(GuExn* base, GuType* type, return &err->data; } } + // Exceptian had already been raised, possibly blocked, or no // exception value is required. return NULL; } GuExnData* -gu_exn_raise_(GuExn* base, GuType* type) +gu_exn_raise_(GuExn* base, const char* type) { return gu_exn_raise_debug_(base, type, NULL, NULL, -1); } -GuType* -gu_exn_caught(GuExn* err) -{ - return err->caught; -} - void gu_raise_errno(GuExn* err) { @@ -90,5 +76,3 @@ gu_raise_errno(GuExn* err) err_data->data = gu_errno; } } - -GU_DEFINE_TYPE(GuErrno, signed, _); diff --git a/src/runtime/c/gu/exn.h b/src/runtime/c/gu/exn.h index faf413a96..14f77dbcf 100644 --- a/src/runtime/c/gu/exn.h +++ b/src/runtime/c/gu/exn.h @@ -2,7 +2,6 @@ #define GU_EXN_H_ #include <gu/mem.h> -#include <gu/type.h> /** @file * @@ -47,9 +46,7 @@ struct GuExnData struct GuExn { /// @privatesection GuExnState state; - GuExn* parent; - GuKind* catch; - GuType* caught; + const char* caught; GuExnData data; }; @@ -59,10 +56,8 @@ struct GuExn { /// Allocate a new local exception frame. -#define gu_exn(parent_, catch_, pool_) &(GuExn){ \ +#define gu_exn(pool_) &(GuExn){ \ .state = GU_EXN_OK, \ - .parent = parent_, \ - .catch = gu_kind(catch_), \ .caught = NULL, \ .data = {.pool = pool_, .data = NULL} \ } @@ -70,8 +65,7 @@ struct GuExn { /// Allocate a new exception frame. GuExn* -gu_new_exn(GuExn* parent, GuKind* catch_kind, GuPool* pool); - +gu_new_exn(GuPool* pool); bool @@ -83,10 +77,11 @@ gu_exn_clear(GuExn* err) { err->state = GU_EXN_OK; } - - -GuType* -gu_exn_caught(GuExn* err); +#define gu_exn_caught(err, type) \ + (err->caught && strcmp(err->caught, #type) == 0) + +bool +gu_exn_caught_(GuExn* err, const char* type); static inline const void* gu_exn_caught_data(GuExn* err) @@ -104,11 +99,11 @@ gu_exn_unblock(GuExn* err); //@private GuExnData* -gu_exn_raise_(GuExn* err, GuType* type); +gu_exn_raise_(GuExn* err, const char* type); //@private GuExnData* -gu_exn_raise_debug_(GuExn* err, GuType* type, +gu_exn_raise_debug_(GuExn* err, const char* type, const char* filename, const char* func, int lineno); #ifdef NDEBUG @@ -122,7 +117,7 @@ gu_exn_raise_debug_(GuExn* err, GuType* type, /// Raise an exception. #define gu_raise(exn, T) \ - gu_exn_raise(exn, gu_type(T)) + gu_exn_raise(exn, #T) /**< * @param exn The current exception frame. * @@ -168,8 +163,6 @@ gu_ok(GuExn* exn) { typedef int GuErrno; -extern GU_DECLARE_TYPE(GuErrno, signed); - void gu_raise_errno(GuExn* err); diff --git a/src/runtime/c/gu/in.c b/src/runtime/c/gu/in.c index ddac81102..3f28d9ba1 100644 --- a/src/runtime/c/gu/in.c +++ b/src/runtime/c/gu/in.c @@ -2,8 +2,6 @@ #include <gu/bits.h> #include <math.h> -GU_DEFINE_TYPE(GuEOF, abstract, _); - static bool gu_in_is_buffering(GuIn* in) @@ -258,7 +256,7 @@ gu_in_fini(GuFinalizer* fin) { GuIn* in = gu_container(fin, GuIn, fini); GuPool* pool = gu_local_pool(); - GuExn* err = gu_exn(NULL, type, pool); + GuExn* err = gu_exn(pool); gu_in_end_buffering(in, err); gu_pool_free(pool); } diff --git a/src/runtime/c/gu/in.h b/src/runtime/c/gu/in.h index 6ed07a886..91c40a42a 100644 --- a/src/runtime/c/gu/in.h +++ b/src/runtime/c/gu/in.h @@ -132,8 +132,4 @@ GuIn* gu_data_in(const uint8_t* buf, size_t size, GuPool* pool); -extern GU_DECLARE_TYPE(GuEOF, abstract); - -#include <gu/type.h> - #endif // GU_IN_H_ diff --git a/src/runtime/c/gu/map.c b/src/runtime/c/gu/map.c index f01b0943a..d6b8a0c72 100644 --- a/src/runtime/c/gu/map.c +++ b/src/runtime/c/gu/map.c @@ -1,6 +1,5 @@ #include <gu/defs.h> #include <gu/mem.h> -#include <gu/type.h> #include <gu/map.h> #include <gu/assert.h> #include <gu/prime.h> @@ -432,19 +431,3 @@ gu_make_map(size_t key_size, GuHasher* hasher, return map; } -GuMap* -gu_map_type_make(GuMapType* mtype, GuPool* pool) -{ - size_t key_size = 0; - if (mtype->hasher && mtype->hasher != gu_addr_hasher) { - key_size = gu_type_size(mtype->key_type); - } - size_t value_size = gu_type_size(mtype->value_type); - return gu_make_map(key_size, mtype->hasher, - value_size, mtype->default_value, pool); -} - -GU_DEFINE_KIND(GuMap, abstract); -// GU_DEFINE_KIND(GuIntMap, GuMap); - - diff --git a/src/runtime/c/gu/map.h b/src/runtime/c/gu/map.h index 33edc2a00..813a99372 100644 --- a/src/runtime/c/gu/map.h +++ b/src/runtime/c/gu/map.h @@ -81,45 +81,4 @@ typedef GuMap GuIntMap; #define gu_new_int_map(VAL_T, DEFAULT, POOL) \ gu_new_map(int, gu_int_hasher, VAL_T, DEFAULT, POOL) - -#if defined(GU_TYPE_H_) && !defined(GU_MAP_H_TYPE_) -#define GU_MAP_H_TYPE_ - -extern GU_DECLARE_KIND(GuMap); - -typedef const struct GuMapType GuMapType, GuType_GuMap; - -struct GuMapType { - GuType_abstract abstract_base; - GuHasher* hasher; - GuType* key_type; - GuType* value_type; - const void* default_value; -}; - -GuMap* -gu_map_type_make(GuMapType* mtype, GuPool* pool); - -#define gu_map_type_new(MAP_T, POOL) \ - gu_map_type_make(gu_type_cast(gu_type(MAP_T), GuMap), (POOL)) - -#define GU_TYPE_INIT_GuMap(k_, t_, kt_, h_, vt_, dv_) \ - { \ - .abstract_base = GU_TYPE_INIT_abstract(k_, t_, _), \ - .hasher = h_, \ - .key_type = kt_, \ - .value_type = vt_, \ - .default_value = dv_ \ - } - -#define gu_type__GuIntMap gu_type__GuMap - -typedef GuType_GuMap GuType_GuIntMap; - -#define GU_TYPE_INIT_GuIntMap(KIND, MAP_T, VAL_T, DEFAULT) \ - GU_TYPE_INIT_GuMap(KIND, MAP_T, gu_type(int), gu_int_hasher, \ - VAL_T, DEFAULT) - -#endif - #endif // GU_MAP_H_ diff --git a/src/runtime/c/gu/out.c b/src/runtime/c/gu/out.c index 439a3de72..6bde9cc58 100644 --- a/src/runtime/c/gu/out.c +++ b/src/runtime/c/gu/out.c @@ -59,7 +59,7 @@ gu_out_fini(GuFinalizer* self) GuOut* out = gu_container(self, GuOut, fini); if (gu_out_is_buffering(out)) { GuPool* pool = gu_local_pool(); - GuExn* err = gu_new_exn(NULL, gu_kind(type), pool); + GuExn* err = gu_new_exn(pool); gu_out_end_buf(out, err); gu_pool_free(pool); } diff --git a/src/runtime/c/gu/seq.c b/src/runtime/c/gu/seq.c index 48a56fc24..83d7cfd9f 100644 --- a/src/runtime/c/gu/seq.c +++ b/src/runtime/c/gu/seq.c @@ -465,8 +465,3 @@ gu_buf_out(GuBuf* buf, GuPool* pool) bout->buf = buf; return gu_new_out(&bout->stream, pool); } - -#include <gu/type.h> - -GU_DEFINE_KIND(GuSeq, GuOpaque); -GU_DEFINE_KIND(GuBuf, abstract); diff --git a/src/runtime/c/gu/str.c b/src/runtime/c/gu/str.c index 073781b23..1453f78e4 100644 --- a/src/runtime/c/gu/str.c +++ b/src/runtime/c/gu/str.c @@ -59,8 +59,6 @@ GuHasher gu_str_hasher[1] = { } }; -GU_DEFINE_TYPE(GuStr, repr, _); - char* gu_vasprintf(const char* fmt, va_list args, GuPool* pool) { diff --git a/src/runtime/c/gu/str.h b/src/runtime/c/gu/str.h index d40f57b08..fcb7acfae 100644 --- a/src/runtime/c/gu/str.h +++ b/src/runtime/c/gu/str.h @@ -18,10 +18,6 @@ gu_str_eq(GuStr s1, GuStr s2); extern GuHasher gu_str_hasher[1]; -#include <gu/type.h> - -extern GU_DECLARE_TYPE(GuStr, repr); - char* gu_vasprintf(const char* fmt, va_list args, GuPool* pool); char* gu_asprintf(GuPool* pool, const char* fmt, ...); diff --git a/src/runtime/c/gu/string.c b/src/runtime/c/gu/string.c index 969ac709a..3a61a33c2 100644 --- a/src/runtime/c/gu/string.c +++ b/src/runtime/c/gu/string.c @@ -1,4 +1,3 @@ -#include <gu/type.h> #include <gu/out.h> #include <gu/seq.h> #include <gu/map.h> @@ -230,7 +229,3 @@ GuHasher gu_string_hasher[1] = { .hash = gu_string_hasher_hash } }; - - -GU_DEFINE_KIND(GuString, pointer); -GU_DEFINE_KIND(GuStringMap, GuMap); diff --git a/src/runtime/c/gu/string.h b/src/runtime/c/gu/string.h index 46244b96c..111050606 100644 --- a/src/runtime/c/gu/string.h +++ b/src/runtime/c/gu/string.h @@ -66,28 +66,6 @@ gu_string_hash(GuHash h, GuString s); extern GuHasher gu_string_hasher[1]; #endif -#ifdef GU_TYPE_H_ -# ifndef GU_STRING_H_TYPE_ -# define GU_STRING_H_TYPE_ - -extern GU_DECLARE_KIND(GuString); -# endif - -# if defined(GU_MAP_H_TYPE_) && !defined(GU_STRING_H_MAP_TYPE_) -# define GU_STRING_H_MAP_TYPE_ - -extern GU_DECLARE_KIND(GuStringMap); -typedef GuType_GuMap GuType_GuStringMap; - -#define GU_TYPE_INIT_GuStringMap(KIND, MAP_T, VAL_T, DEFAULT) \ - GU_TYPE_INIT_GuMap(KIND, MAP_T, \ - gu_type(GuString), gu_string_hasher, \ - VAL_T, DEFAULT) - -# endif -#endif - - #if defined(GU_SEQ_H_) && !defined(GU_STRING_H_SEQ_) #define GU_STRING_H_SEQ_ diff --git a/src/runtime/c/gu/type.c b/src/runtime/c/gu/type.c deleted file mode 100644 index 26260a614..000000000 --- a/src/runtime/c/gu/type.c +++ /dev/null @@ -1,228 +0,0 @@ - -#include <gu/type.h> -#include <gu/assert.h> -#include <gu/map.h> - -GuKind GU_TYPE_IDENT(type)[1] = {{ .super = NULL }}; - -GU_DEFINE_KIND(alias, type); -GU_DEFINE_KIND(typedef, alias); -GU_DEFINE_KIND(referenced, alias); - -GU_DEFINE_KIND(repr, type); -GU_DEFINE_KIND(GuOpaque, repr); - -GU_DEFINE_KIND(abstract, type); - -GU_DEFINE_KIND(struct, repr); - -GU_DEFINE_KIND(pointer, repr); -GU_DEFINE_KIND(reference, pointer); -GU_DEFINE_KIND(shared, pointer); - -GU_DEFINE_KIND(primitive, repr); - -// sizeof(void) is illegal, so do this manually -GuPrimType GU_TYPE_IDENT(void)[1] = {{ - .repr_base = { - .type_base = { - .kind_base = { - .super = gu_kind(primitive), - }, - }, - .size = 0, - .align = 1, - }, - .name = "void", -}}; - -GU_DEFINE_KIND(integer, primitive); -GU_DEFINE_TYPE(char, integer, _); - -GU_DEFINE_KIND(signed, integer); -GU_DEFINE_TYPE(int, signed, _); -GU_DEFINE_TYPE(int8_t, signed, _); -GU_DEFINE_TYPE(int16_t, signed, _); -GU_DEFINE_TYPE(int32_t, signed, _); -GU_DEFINE_TYPE(int64_t, signed, _); -GU_DEFINE_TYPE(intptr_t, signed, _); -GU_DEFINE_TYPE(intmax_t, signed, _); - -GU_DEFINE_KIND(unsigned, integer); -GU_DEFINE_TYPE(uint8_t, unsigned, _); -GU_DEFINE_TYPE(uint16_t, unsigned, _); -GU_DEFINE_TYPE(uint32_t, unsigned, _); -GU_DEFINE_TYPE(uint64_t, unsigned, _); -GU_DEFINE_TYPE(uintmax_t, unsigned, _); -GU_DEFINE_TYPE(size_t, unsigned, _); - -GU_DEFINE_TYPE(GuLength, unsigned, _); - -GU_DEFINE_KIND(GuFloating, primitive); -GU_DEFINE_TYPE(float, GuFloating, _); -GU_DEFINE_TYPE(double, GuFloating, _); -GU_DEFINE_TYPE(GuLongDouble, GuFloating, _); - - -GU_DEFINE_KIND(enum, repr); - -bool gu_type_has_kind(GuType* type, GuKind* kind) -{ - GuKind* k = (GuKind*)type; - while (k != NULL) { - if (k == kind) { - return true; - } - k = k->super; - } - return false; -} - - -struct GuTypeMap { - GuMap* map; -}; - -static void -gu_type_map_init(GuTypeMap* tmap, GuTypeTable* table) -{ - for (int i = 0; i < table->parents.len; i++) { - gu_type_map_init(tmap, table->parents.elems[i]); - } - for (int i = 0; i < table->entries.len; i++) { - GuTypeTableEntry* e = &table->entries.elems[i]; - gu_map_put(tmap->map, e->kind, void*, e->val); - } -} - -GuTypeMap* -gu_new_type_map(GuTypeTable* table, GuPool* pool) -{ - GuTypeMap* tmap = gu_new(GuTypeMap, pool); - tmap->map = gu_new_map(GuKind, NULL, void*, &gu_null, pool); - gu_type_map_init(tmap, table); - return tmap; -} - -bool -gu_struct_has_flex(GuStructRepr* srepr) -{ - for (int i = 0; i < srepr->members.len; i++) { - if (srepr->members.elems[i].is_flex) { - return true; - } - } - return false; -} - -void* -gu_type_map_get(GuTypeMap* tmap, GuType* type) -{ - GuKind* kind = (GuKind*)type; - while (kind != NULL) { - void* val = gu_map_get(tmap->map, kind, void*); - if (val != NULL) { - return val; - } - kind = kind->super; - } - return NULL; -} - -const void* -gu_type_dyn_cast(GuType* type, GuKind* kind) -{ - if (gu_type_has_kind(type, kind)) { - return type; - } - return NULL; -} - - -const void* -gu_type_check_cast(GuType* type, GuKind* kind) -{ - gu_assert(gu_type_has_kind(type, kind)); - return type; -} - -GuTypeRepr* -gu_type_repr(GuType* type) -{ - GuTypeAlias* alias; - while ((alias = gu_type_try_cast(type, alias))) { - type = alias->type; - } - return gu_type_try_cast(type, repr); -} - -size_t -gu_type_size(GuType* type) -{ - GuTypeRepr* repr = gu_type_repr(type); - return repr ? repr->size : 0; -} - -GuEnumConstant* -gu_enum_value(GuEnumType* etype, const void* enump) -{ - size_t esize = etype->repr_base.size; -#define CHECK_ENUM_TYPE(t_) do { \ - if (esize == sizeof(t_)) { \ - t_ c = *(const t_*)enump; \ - for (int i = 0; i < etype->constants.len; i++) { \ - GuEnumConstant* cp = &etype->constants.elems[i]; \ - t_ d = *(const t_*)cp->enum_value; \ - if (c == d) { \ - return cp; \ - } \ - } \ - return NULL; \ - } \ - } while (false) - - CHECK_ENUM_TYPE(int); - CHECK_ENUM_TYPE(char); - CHECK_ENUM_TYPE(short); - CHECK_ENUM_TYPE(long); - CHECK_ENUM_TYPE(long long); - - return NULL; -} - -void* -gu_type_malloc(GuType* type, GuPool* pool) -{ - GuTypeRepr* repr = gu_type_repr(type); - gu_assert(repr); - return gu_malloc_aligned(pool, repr->size, repr->align); -} - -#if 0 - -typedef const struct GuPtrConvFns GuPtrConvFns; - -struct GuPtrConvFns { - void* (*get)(const void* pp); - void (*set)(void** pp, void* p); -}; - -#define GU_TYPE_PTR_DEFINE_GETSET(name_, t_) \ - static void* gu_type_##name_##_ptr_get(const void* pp) { \ - return *(t_* const*) pp; \ - } \ - \ - static void gu_type_##name_##_ptr_set(void* pp, void* p) { \ - *(t_**) pp = p; \ - } \ - static GuPtrConvFns gu_ptr_conv_##name_ = { \ - .get = gu_type_##name_##_ptr_get, \ - .set = gu_type_##name_##_ptr_set \ - } - -GU_TYPE_PTR_DEFINE_GETSET(void, void); -GU_TYPE_PTR_DEFINE_GETSET(struct, GuStruct); -GU_TYPE_PTR_DEFINE_GETSET(int, int); - - -#endif diff --git a/src/runtime/c/gu/type.h b/src/runtime/c/gu/type.h deleted file mode 100644 index b7d0e6dc9..000000000 --- a/src/runtime/c/gu/type.h +++ /dev/null @@ -1,474 +0,0 @@ - -#ifndef GU_TYPE_H_ -#define GU_TYPE_H_ - -#include <gu/defs.h> - -// -// kind -// - -typedef const struct GuKind GuKind; - -struct GuKind { - GuKind* super; -}; - -// Use GU_PASTE here so k_ can be preprocessor-expanded -#define GU_TYPE_IDENT(k_) GU_PASTE(gu_type__,k_) - -#define gu_kind(k_) ((GuKind*)GU_TYPE_IDENT(k_)) - -#define GU_DECLARE_KIND(k_) \ - GuKind GU_TYPE_IDENT(k_)[1] - -extern GU_DECLARE_KIND(kind); - -#define GU_DEFINE_KIND(k_, super_k_) \ - GuKind GU_TYPE_IDENT(k_)[1] = {{ .super = gu_kind(super_k_) }} - -// -// type -// - -typedef const struct GuType GuType; - -struct GuType { - GuKind kind_base; -}; - -typedef GuType GuType_type; - -extern GU_DECLARE_KIND(type); - -#define GU_TYPE_INIT_type(k_, t_, _) { .kind_base = { .super = gu_kind(k_) } } - -#define gu_type(t_) ((GuType*)gu_kind(t_)) - - -#define GU_KIND_TYPE(k_) GU_PASTE(GuType_,k_) - -// This cannot be used indirectly, since we don't want to pp-expand k_. -// We must inline the body into other macros. -#define GU_TYPE_INIT(k_, ...) \ - GU_TYPE_INIT_##k_(k_, __VA_ARGS__) - -//#define GU_TYPE_LIT(k_, ...) -// ((GuType*)(GuType_##k_[]){GU_TYPE_INIT(k_, __VA_ARGS__)}) -#define GU_TYPE_LIT(k_, ...) \ - ((GuType*)&(GU_KIND_TYPE(k_)) GU_TYPE_INIT_##k_(k_, __VA_ARGS__)) - -#define GU_DECLARE_TYPE(t_, k_) \ - GU_KIND_TYPE(k_) GU_TYPE_IDENT(t_)[1] - -//#define GU_DEFINE_TYPE(t_, k_, ...) -// GuType_##k_ GU_TYPE_IDENT(t_) = GU_TYPE_INIT(k_, t_, __VA_ARGS__) -#define GU_DEFINE_TYPE(t_, k_, ...) \ - GU_KIND_TYPE(k_) GU_TYPE_IDENT(t_)[1] = \ - { GU_TYPE_INIT_##k_(k_, t_, __VA_ARGS__) } - -#define GU_DEFINE_TYPE_ALIAS(t1_, t2_) \ - static GuType* const GU_TYPE_IDENT(t1_) = gu_type(t2_) - - -// -// abstract -// - -typedef GuType GuType_abstract; - -#define GU_TYPE_INIT_abstract(k_, t_, _) \ - GU_TYPE_INIT_type(k_, t_, _) - -extern GU_DECLARE_KIND(abstract); - - -// -// repr -// - -typedef struct GuTypeRepr GuTypeRepr, GuType_repr; - -struct GuTypeRepr { - GuType type_base; - uint16_t size; - uint16_t align; -}; - -#define GU_TYPE_INIT_repr(k_, t_, _) { \ - .type_base = GU_TYPE_INIT_type(k_, t_, _), \ - .size = sizeof(t_), \ - .align = gu_alignof(t_) \ - } - -extern GU_DECLARE_KIND(repr); - - - -// -// GuOpaque -// - -typedef GuType_repr GuType_GuOpaque; - -#define GU_TYPE_INIT_GuOpaque GU_TYPE_INIT_repr - -extern GU_DECLARE_KIND(GuOpaque); - -// -// pointer -// - -typedef const struct GuPointerType GuPointerType, GuType_pointer; - -struct GuPointerType { - GuType_repr repr_base; - GuType* pointed_type; -}; - -#define GU_TYPE_INIT_pointer(k_, t_, pointed_) \ - { \ - .repr_base = GU_TYPE_INIT_repr(k_, t_, _), \ - .pointed_type = pointed_ \ -} - - -extern GU_DECLARE_KIND(pointer); - -#define gu_ptr_type(t_) \ - GU_TYPE_LIT(pointer, t_*, gu_type(t_)) - - - - - - - -// -// alias -// - - -typedef const struct GuTypeAlias GuTypeAlias, GuType_alias; - -struct GuTypeAlias { - GuType type_base; - GuType* type; -}; - -#define GU_TYPE_INIT_alias(k_, t_, type_) { \ - .type_base = GU_TYPE_INIT_type(k_, t_, _), \ - .type = type_ \ -} - -extern GU_DECLARE_KIND(alias); - -// -// typedef -// - -typedef const struct GuTypeDef GuTypeDef, GuType_typedef; - -struct GuTypeDef { - GuType_alias alias_base; - const char* name; -}; - -#define GU_TYPE_INIT_typedef(k_, t_, type_) { \ - .alias_base = GU_TYPE_INIT_alias(k_, t_, type_), \ - .name = #t_, \ -} - -extern GU_DECLARE_KIND(typedef); - -#define GU_DEFINE_TYPEDEF_X(t_, dk_, k_, ...) \ - GU_DEFINE_TYPE(t_, dk_, GU_TYPE_LIT(k_, t_, __VA_ARGS__)) - -#define GU_DEFINE_TYPEDEF(t_, ...) \ - GU_DEFINE_TYPEDEF_X(t_, typedef, __VA_ARGS__) - - - -// -// referenced -// - -extern GU_DECLARE_KIND(referenced); - -typedef GuType_alias GuType_referenced; - -#define GU_TYPE_INIT_referenced GU_TYPE_INIT_alias - - - -#include <gu/mem.h> - -// -// struct -// - -typedef const struct GuStructRepr GuStructRepr, GuType_struct; - -typedef const struct GuMember GuMember; - -struct GuMember { - ptrdiff_t offset; - const char* name; - GuType* type; - bool is_flex; -}; - -struct GuStructRepr { - GuType_repr repr_base; - const char* name; - struct { - int len; - GuMember* elems; - } members; -}; - -extern GU_DECLARE_KIND(struct); - -#define GU_MEMBER_AUX_(struct_, member_, type_, is_flex_) \ - { \ - .offset = offsetof(struct_, member_), \ - .name = #member_, \ - .type = type_, \ - .is_flex = is_flex_, \ - } - -#define GU_MEMBER_V(struct_, member_, type_) \ - GU_MEMBER_AUX_(struct_, member_, type_, false) - -#define GU_MEMBER(s_, m_, t_) \ - GU_MEMBER_V(s_, m_, gu_type(t_)) - -#define GU_MEMBER_P(s_, m_, t_) \ - GU_MEMBER_V(s_, m_, gu_ptr_type(t_)) - -#define GU_MEMBER_S(s_, m_, t_) \ - GU_MEMBER_V(s_, m_, gu_shared_ptr_type(t_)) - -#define GU_FLEX_MEMBER_V(struct_, member_, type_) \ - GU_MEMBER_AUX_(struct_, member_, type_, true) - -#define GU_FLEX_MEMBER(s_, m_, t_) \ - GU_FLEX_MEMBER_V(s_, m_, gu_type(t_)) - -#define GU_FLEX_MEMBER_P(s_, m_, t_) \ - GU_FLEX_MEMBER_V(s_, m_, gu_ptr_type(t_)) - - -#define GU_TYPE_INIT_struct(k_, t_, ...) { \ - .repr_base = GU_TYPE_INIT_repr(k_, t_, _), \ - .name = #t_, \ - .members = { \ - .len = GU_ARRAY_LEN(GuMember,GU_ID({__VA_ARGS__})), \ - .elems = ((GuMember[]){__VA_ARGS__}) \ - } \ -} - -bool -gu_struct_has_flex(GuStructRepr* srepr); - - -// -// reference -// - -typedef GuType_pointer GuType_reference; - -#define GU_TYPE_INIT_reference GU_TYPE_INIT_pointer - -extern GU_DECLARE_KIND(reference); - - -// -// shared -// - -typedef GuType_pointer GuType_shared; - -#define GU_TYPE_INIT_shared GU_TYPE_INIT_pointer - -extern GU_DECLARE_KIND(shared); - -#define gu_shared_ptr_type(t_) \ - GU_TYPE_LIT(shared, t_*, gu_type(t_)) - -// -// primitives -// - -typedef const struct GuPrimType GuPrimType, GuType_primitive; - -struct GuPrimType { - GuType_repr repr_base; - const char* name; -}; - -#define GU_TYPE_INIT_primitive(k_, t_, _) { \ - .repr_base = GU_TYPE_INIT_repr(k_, t_, _), \ - .name = #t_ \ -} - -extern GU_DECLARE_KIND(primitive); -extern GU_DECLARE_TYPE(void, primitive); - -#define GU_TYPE_INIT_integer GU_TYPE_INIT_primitive -typedef GuType_primitive GuType_integer; -extern GU_DECLARE_KIND(integer); -extern GU_DECLARE_TYPE(char, integer); - -#define GU_TYPE_INIT_signed GU_TYPE_INIT_integer -typedef GuType_integer GuType_signed; -extern GU_DECLARE_KIND(signed); -extern GU_DECLARE_TYPE(int, signed); -extern GU_DECLARE_TYPE(int8_t, signed); -extern GU_DECLARE_TYPE(int16_t, signed); -extern GU_DECLARE_TYPE(int32_t, signed); -extern GU_DECLARE_TYPE(int64_t, signed); -extern GU_DECLARE_TYPE(intptr_t, signed); -extern GU_DECLARE_TYPE(intmax_t, signed); - -#define GU_TYPE_INIT_unsigned GU_TYPE_INIT_integer -typedef GuType_integer GuType_unsigned; -extern GU_DECLARE_KIND(unsigned); -extern GU_DECLARE_TYPE(uint8_t, unsigned); -extern GU_DECLARE_TYPE(uint16_t, unsigned); -extern GU_DECLARE_TYPE(uint32_t, unsigned); -extern GU_DECLARE_TYPE(uint64_t, unsigned); -extern GU_DECLARE_TYPE(uintmax_t, unsigned); -extern GU_DECLARE_TYPE(size_t, unsigned); - -typedef size_t GuLength; -extern GU_DECLARE_TYPE(GuLength, unsigned); // TODO: get rid - - -#define GU_TYPE_INIT_GuFloating GU_TYPE_INIT_primitive -typedef GuType_primitive GuType_GuFloating; -extern GU_DECLARE_KIND(GuFloating); -extern GU_DECLARE_TYPE(float, GuFloating); -extern GU_DECLARE_TYPE(double, GuFloating); -typedef long double GuLongDouble; -extern GU_DECLARE_TYPE(GuLongDouble, GuFloating); - - - -// -// enum -// - -extern GU_DECLARE_KIND(enum); - -typedef const struct GuEnumConstant GuEnumConstant; - -struct GuEnumConstant { - const char* name; - int64_t value; - const void* enum_value; -}; - -typedef const struct GuEnumType GuEnumType, GuType_enum; - -struct GuEnumType { - GuType_repr repr_base; - struct { - int len; - GuEnumConstant* elems; - } constants; -}; - -#define GU_ENUM_C(t_, x) { \ - .name = #x, \ - .value = x, \ - .enum_value = (const t_[1]){ x } \ - } - -#define GU_TYPE_INIT_enum(k_, t_, ...) { \ - .repr_base = GU_TYPE_INIT_repr(k_, t_, _), \ - .constants = { \ - .len = GU_ARRAY_LEN(GuEnumConstant,GU_ID({__VA_ARGS__})), \ - .elems = ((GuEnumConstant[]){__VA_ARGS__}) \ - } \ -} - -GuEnumConstant* -gu_enum_value(GuEnumType* etype, const void* enump); - - - - -bool gu_type_has_kind(const GuType* type, const GuKind* kind); - - - - -typedef const struct GuTypeTableEntry GuTypeTableEntry; - -struct GuTypeTableEntry { - GuKind* kind; - void* val; -}; - -typedef const struct GuTypeTable GuTypeTable; - -struct GuTypeTable { - struct { - int len; - GuTypeTable** elems; - } parents; - struct { - int len; - GuTypeTableEntry* elems; - } entries; -}; - -#define GU_TYPETABLE(parents_, ...) { \ - .parents = parents_, \ - .entries = { \ - .len = GU_ARRAY_LEN(GuTypeTableEntry,GU_ID({__VA_ARGS__})), \ - .elems = ((GuTypeTableEntry[]){__VA_ARGS__}) \ - } \ - } - -typedef struct GuTypeMap GuTypeMap; - -GuTypeMap* -gu_new_type_map(GuTypeTable* table, GuPool* pool); - -void* -gu_type_map_get(GuTypeMap* tmap, GuType* type); - -size_t -gu_type_size(GuType* type); - -GuTypeRepr* -gu_type_repr(GuType* type); - -const void* -gu_type_check_cast(GuType* t, GuKind* k); - -const void* -gu_type_dyn_cast(GuType* t, GuKind* k); - -#define gu_type_try_cast(type_, k_) \ - ((GU_KIND_TYPE(k_)*)gu_type_dyn_cast(type_, gu_kind(k_))) - -#ifndef NDEBUG -#define gu_type_cast(type_, k_) \ - ((GU_KIND_TYPE(k_)*)gu_type_check_cast(type_, gu_kind(k_))) -#else -#define gu_type_cast(type_, k_) \ - ((GU_KIND_TYPE(k_)*)(type_)) -#endif - -void* gu_type_malloc(GuType* type, GuPool* pool); - -#if 0 -void* gu_type_ptr_get(GuType* type, const void* pp); -void gu_type_ptr_set(GuType* type, void* pp, void* p); -#endif - - -#endif // GU_TYPE_H_ diff --git a/src/runtime/c/gu/ucs.c b/src/runtime/c/gu/ucs.c index 2b1e63cf0..05ea163ae 100644 --- a/src/runtime/c/gu/ucs.c +++ b/src/runtime/c/gu/ucs.c @@ -1,8 +1,6 @@ #include <gu/ucs.h> #include <gu/assert.h> -GU_DEFINE_TYPE(GuUCSExn, abstract, _); - bool gu_char_is_valid(char c) { diff --git a/src/runtime/c/gu/ucs.h b/src/runtime/c/gu/ucs.h index 5764e60c8..b09e0ff3f 100644 --- a/src/runtime/c/gu/ucs.h +++ b/src/runtime/c/gu/ucs.h @@ -36,6 +36,4 @@ gu_str_to_ucs(const char* cbuf, size_t len, GuUCS* ubuf, GuExn* err); size_t gu_ucs_to_str(const GuUCS* ubuf, size_t len, char* cbuf, GuExn* err); -extern GU_DECLARE_TYPE(GuUCSExn, abstract); - #endif // GU_ISO10646_H_ diff --git a/src/runtime/c/gu/utf8.c b/src/runtime/c/gu/utf8.c index 70db65f05..8f22e5823 100644 --- a/src/runtime/c/gu/utf8.c +++ b/src/runtime/c/gu/utf8.c @@ -49,9 +49,9 @@ gu_in_utf8_(GuIn* in, GuExn* err) uint8_t buf[3]; // If reading the extra bytes causes EOF, it is an encoding // error, not a legitimate end of character stream. - GuExn* tmp_err = gu_exn(err, GuEOF, NULL); - gu_in_bytes(in, buf, len, tmp_err); - if (tmp_err->caught) { + gu_in_bytes(in, buf, len, err); + if (gu_exn_caught(err, GuEOF)) { + gu_exn_clear(err); goto fail; } if (!gu_ok(err)) { @@ -151,9 +151,9 @@ gu_in_utf8_buf(uint8_t** buf, GuIn* in, GuExn* err) } // If reading the extra bytes causes EOF, it is an encoding // error, not a legitimate end of character stream. - GuExn* tmp_err = gu_exn(err, GuEOF, NULL); - gu_in_bytes(in, p, len, tmp_err); - if (tmp_err->caught) { + gu_in_bytes(in, p, len, err); + if (gu_exn_caught(err, GuEOF)) { + gu_exn_clear(err); goto fail; } if (!gu_ok(err)) { diff --git a/src/runtime/c/gu/variant.c b/src/runtime/c/gu/variant.c index e4c5a342e..dd4149429 100644 --- a/src/runtime/c/gu/variant.c +++ b/src/runtime/c/gu/variant.c @@ -109,6 +109,3 @@ gu_variant_intval(GuVariant variant) } const GuVariant gu_null_variant = { (GuWord) NULL }; - -GU_DEFINE_KIND(GuVariant, repr); -GU_DEFINE_KIND(GuVariantAsPtr, repr); diff --git a/src/runtime/c/gu/variant.h b/src/runtime/c/gu/variant.h index bc9f45315..a4c24b4af 100644 --- a/src/runtime/c/gu/variant.h +++ b/src/runtime/c/gu/variant.h @@ -27,7 +27,6 @@ #include <gu/defs.h> #include <gu/mem.h> -#include <gu/type.h> /** @name Variants * @{ @@ -105,63 +104,4 @@ gu_variant_is_null(GuVariant v) { return ((void*)v == NULL); } - -// variant - -typedef const struct GuConstructor GuConstructor; - -struct GuConstructor { - int c_tag; - const char* c_name; - const GuType* type; -}; - -#define GU_CONSTRUCTOR_V(ctag, c_type) { \ - .c_tag = ctag, \ - .c_name = #ctag, \ - .type = c_type \ -} - -#define GU_CONSTRUCTOR(ctag, t_) \ - GU_CONSTRUCTOR_V(ctag, gu_type(t_)) - -#define GU_CONSTRUCTOR_P(ctag, t_) \ - GU_CONSTRUCTOR_V(ctag, gu_ptr_type(t_)) - -#define GU_CONSTRUCTOR_S(ctag, t_, ...) \ - GU_CONSTRUCTOR_V(ctag, GU_TYPE_LIT(struct, t_, __VA_ARGS__)) - -#define GU_CONSTRUCTOR_S1(ctag, t_, mem1_, type1_) \ - GU_CONSTRUCTOR_S(ctag, t_, \ - GU_MEMBER(t_, mem1_, type1_)) - -#define GU_CONSTRUCTOR_S2(ctag, t_, mem1_, type1_, mem2_, type2_) \ - GU_CONSTRUCTOR_S(ctag, t_, \ - GU_MEMBER(t_, mem1_, type1_), \ - GU_MEMBER(t_, mem2_, type2_)) - - - -typedef struct { - int len; - GuConstructor* elems; -} GuConstructors; - -typedef const struct GuVariantType GuVariantType, GuType_GuVariant; - -struct GuVariantType { - GuType_repr repr_base; - GuConstructors ctors; -}; - -#define GU_TYPE_INIT_GuVariant(k_, t_, ...) { \ - .repr_base = GU_TYPE_INIT_repr(k_, GuVariant, _), \ - .ctors = { \ - .len = GU_ARRAY_LEN(GuConstructor,GU_ID({__VA_ARGS__})), \ - .elems = ((GuConstructor[]){__VA_ARGS__}) \ - } \ -} - -extern GU_DECLARE_KIND(GuVariant); - #endif // GU_VARIANT_H_ |
