commit 038254a9e3dd307487e631ccf7f45dffb4a774da
parent 1728db34f8b103f976a47ddd9849e5b71215e8ab
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 14 Jan 2021 14:20:20 -0500
Store type hash in type struct
Diffstat:
4 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/include/types.h b/include/types.h
@@ -108,6 +108,7 @@ enum type_flags {
struct type {
enum type_storage storage;
+ uint64_t id;
unsigned int flags;
size_t size, align;
union {
@@ -132,8 +133,10 @@ bool type_is_float(const struct type *type);
uint64_t type_hash(const struct type *type);
+void builtin_types_init();
+
// Built-in type singletons
-extern const struct type
+extern struct type
// Primitive
builtin_type_bool,
builtin_type_char,
diff --git a/src/check.c b/src/check.c
@@ -1299,6 +1299,7 @@ scan_declarations(struct context *ctx, const struct ast_decls *decls)
void
check(struct context *ctx, const struct ast_unit *aunit, struct unit *unit)
{
+ builtin_types_init();
ctx->store.check_context = ctx;
ctx->ns = unit->ns;
diff --git a/src/type_store.c b/src/type_store.c
@@ -558,10 +558,11 @@ static const struct type *type_store_lookup_type(
static void
type_init_from_type(struct type_store *store,
- struct type *new, const struct type *old)
+ struct type *new, const struct type *old, uint64_t hash)
{
new->storage = old->storage;
new->flags = old->flags;
+ new->id = hash;
switch (old->storage) {
case TYPE_STORAGE_BOOL:
@@ -683,7 +684,7 @@ type_store_lookup_type(struct type_store *store, const struct type *type)
bucket = *next = xcalloc(1, sizeof(struct type_bucket));
// XXX: can we replace this with memcpy?
- type_init_from_type(store, &bucket->type, type);
+ type_init_from_type(store, &bucket->type, type, hash);
return &bucket->type;
}
diff --git a/src/types.c b/src/types.c
@@ -311,8 +311,35 @@ type_hash(const struct type *type)
return hash;
}
+void
+builtin_types_init()
+{
+ struct type *builtins[] = {
+ &builtin_type_bool, &builtin_type_char, &builtin_type_f32,
+ &builtin_type_f64, &builtin_type_i8, &builtin_type_i16,
+ &builtin_type_i32, &builtin_type_i64, &builtin_type_int,
+ &builtin_type_u8, &builtin_type_u16, &builtin_type_u32,
+ &builtin_type_u64, &builtin_type_uint, &builtin_type_uintptr,
+ &builtin_type_null, &builtin_type_rune, &builtin_type_size,
+ &builtin_type_void, &builtin_type_const_bool,
+ &builtin_type_const_char, &builtin_type_const_f32,
+ &builtin_type_const_f64, &builtin_type_const_i8,
+ &builtin_type_const_i16, &builtin_type_const_i32,
+ &builtin_type_const_i64, &builtin_type_const_int,
+ &builtin_type_const_u8, &builtin_type_const_u16,
+ &builtin_type_const_u32, &builtin_type_const_u64,
+ &builtin_type_const_uint, &builtin_type_const_uintptr,
+ &builtin_type_const_rune, &builtin_type_const_size,
+ &builtin_type_const_void, &builtin_type_const_ptr_char,
+ &builtin_type_str, &builtin_type_const_str,
+ };
+ for (size_t i = 0; i < sizeof(builtins) / sizeof(builtins[0]); ++i) {
+ builtins[i]->id = type_hash(builtins[i]);
+ }
+}
+
// Built-in type singletons
-const struct type builtin_type_bool = {
+struct type builtin_type_bool = {
.storage = TYPE_STORAGE_BOOL,
.size = 4, // XXX: ARCH
.align = 4,
@@ -517,7 +544,7 @@ builtin_type_const_void = {
};
// Others
-const struct type builtin_type_const_ptr_char = {
+struct type builtin_type_const_ptr_char = {
.storage = TYPE_STORAGE_POINTER,
.flags = TYPE_CONST,
.size = 8, // XXX: ARCH