harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 3d4352f9e543fac3c654ec284e1b6f9b0f614505
parent 9441c45d53ee5c8f13618915bef651127c123e2b
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 31 Jan 2021 17:07:44 -0500

typedefs: implement enums

Diffstat:
Minclude/types.h | 1+
Msrc/typedef.c | 19++++++++++++++++++-
Msrc/types.c | 8++++----
3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/include/types.h b/include/types.h @@ -150,6 +150,7 @@ const struct type *tagged_select_subtype( bool tagged_subset_compat(const struct type *to, const struct type *from); const char *type_storage_unparse(enum type_storage storage); +bool type_storage_is_signed(enum type_storage storage); bool type_is_signed(const struct type *type); bool type_is_integer(const struct type *type); bool type_is_numeric(const struct type *type); diff --git a/src/typedef.c b/src/typedef.c @@ -153,7 +153,6 @@ emit_type(const struct type *type, FILE *out) switch (type->storage) { case TYPE_STORAGE_BOOL: case TYPE_STORAGE_CHAR: - case TYPE_STORAGE_ENUM: case TYPE_STORAGE_F32: case TYPE_STORAGE_F64: case TYPE_STORAGE_I16: @@ -234,6 +233,24 @@ emit_type(const struct type *type, FILE *out) fprintf(out, ") "); emit_type(type->func.result, out); break; + case TYPE_STORAGE_ENUM: + fprintf(out, "enum %s { ", type_storage_unparse(type->_enum.storage)); + for (const struct type_enum_value *ev = type->_enum.values; + ev; ev = ev->next) { + fprintf(out, "%s = ", ev->name); + if (type_storage_is_signed(type->_enum.storage)) { + fprintf(out, "%zu%s", ev->ival, + storage_to_suffix(type->_enum.storage)); + } else { + fprintf(out, "%zd%s", ev->uval, + storage_to_suffix(type->_enum.storage)); + } + if (ev->next) { + fprintf(out, ", "); + } + } + fprintf(out, "}"); + break; } } diff --git a/src/types.c b/src/types.c @@ -199,8 +199,8 @@ type_is_float(const struct type *type) return type->storage == TYPE_STORAGE_F32 || type->storage == TYPE_STORAGE_F64; } -static bool -storage_is_signed(enum type_storage storage) +bool +type_storage_is_signed(enum type_storage storage) { switch (storage) { case TYPE_STORAGE_VOID: @@ -242,9 +242,9 @@ bool type_is_signed(const struct type *type) { if (type->storage == TYPE_STORAGE_ENUM) { - return storage_is_signed(type->_enum.storage); + return type_storage_is_signed(type->_enum.storage); } - return storage_is_signed(type_dealias(type)->storage); + return type_storage_is_signed(type_dealias(type)->storage); } uint32_t