harec

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

commit 437a3ca788a8dccc495e79bb88d0449951e969cc
parent 495539655c0eea80c7f26080277d316f4f55610c
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 12 Dec 2020 08:39:56 -0500

Replace bools with type flags

The named flags are more semantically meaningful.

Diffstat:
Minclude/ast.h | 4++--
Minclude/types.h | 13+++++++++++--
Msrc/parse.c | 8++++----
Msrc/type_store.c | 2++
4 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/include/ast.h b/include/ast.h @@ -57,8 +57,8 @@ struct ast_function_type { }; struct ast_pointer_type { - bool nullable; struct ast_type *referent; + unsigned int flags; }; struct ast_tagged_union_type { @@ -74,7 +74,7 @@ struct ast_struct_union_type { struct ast_type { enum type_storage storage; - bool constant; + unsigned int flags; union { struct identifier alias; struct ast_list_type array; diff --git a/include/types.h b/include/types.h @@ -1,6 +1,7 @@ #ifndef HARE_TYPES_H #define HARE_TYPES_H #include <stdbool.h> +#include <stdint.h> #include "identifier.h" enum type_storage { @@ -39,14 +40,22 @@ enum type_storage { struct type; +enum pointer_flags { + POINTER_NULLABLE = 1 << 0, +}; + struct type_pointer { - bool nullable; const struct type *referent; + unsigned int flags; +}; + +enum type_flags { + TYPE_CONST = 1 << 0, }; struct type { enum type_storage storage; - bool constant; + unsigned int flags; size_t size, align; union { struct type_pointer pointer; diff --git a/src/parse.c b/src/parse.c @@ -233,7 +233,7 @@ parse_type(struct parser *par, struct ast_type *type) struct token tok = {0}; switch (lex(par->lex, &tok)) { case T_CONST: - type->constant = true; + type->flags |= TYPE_CONST; break; default: unlex(par->lex, &tok); @@ -300,7 +300,7 @@ parse_type(struct parser *par, struct ast_type *type) case T_ENUM: assert(0); // TODO: Enums case T_NULLABLE: - type->pointer.nullable = true; + type->pointer.flags |= POINTER_NULLABLE; want(par, T_TIMES, NULL); trace(TR_PARSE, "nullable"); /* fallthrough */ @@ -330,7 +330,7 @@ parse_type(struct parser *par, struct ast_type *type) parse_identifier(par, &type->alias); break; } - trleave(TR_PARSE, "%s%s", type->constant ? "const " : "", + trleave(TR_PARSE, "%s%s", (type->flags & TYPE_CONST) ? "const " : "", type_storage_unparse(type->storage)); } @@ -425,7 +425,7 @@ parse_global_decl(struct parser *par, enum lexical_token mode, want(par, T_COLON, NULL); parse_type(par, &i->type); if (mode == T_CONST) { - i->type.constant = true; + i->type.flags |= TYPE_CONST; } want(par, T_EQUAL, NULL); parse_simple_expression(par, &i->init); diff --git a/src/type_store.c b/src/type_store.c @@ -7,6 +7,7 @@ atype_hash(struct type_store *store, const struct ast_type *type) { unsigned long hash = DJB2_INIT; hash = djb2(hash, type->storage); + hash = djb2(hash, type->flags); switch (type->storage) { case TYPE_STORAGE_BOOL: case TYPE_STORAGE_CHAR: @@ -46,6 +47,7 @@ type_hash(struct type_store *store, const struct type *type) { unsigned long hash = DJB2_INIT; hash = djb2(hash, type->storage); + hash = djb2(hash, type->flags); switch (type->storage) { case TYPE_STORAGE_BOOL: case TYPE_STORAGE_CHAR: