commit 2f027c25a33eb5827d05f65f7fd123609c03b2f9
parent 6d44d0d3627ec216de16cbae7fb5a43049f58543
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 13 Dec 2020 13:11:02 -0500
Expand checked data structures for functions
Diffstat:
7 files changed, 72 insertions(+), 43 deletions(-)
diff --git a/include/ast.h b/include/ast.h
@@ -38,12 +38,6 @@ struct ast_enum_type {
struct ast_enum_field *values;
};
-enum variadism {
- VARIADISM_NONE,
- VARIADISM_C,
- VARIADISM_HARE,
-};
-
struct ast_function_parameters {
char *name;
struct ast_type *type;
@@ -51,10 +45,10 @@ struct ast_function_parameters {
};
struct ast_function_type {
- bool noreturn;
- enum variadism variadism;
struct ast_type *result;
struct ast_function_parameters *parameters;
+ enum variadism variadism;
+ unsigned int flags; // enum function_flags (types.h)
};
struct ast_pointer_type {
@@ -128,10 +122,10 @@ struct ast_type_decl {
struct ast_function_decl {
char *symbol;
- uint32_t flags; // enum function_flags (check.h)
struct identifier ident;
struct ast_function_type prototype;
struct ast_expression body;
+ unsigned int flags; // enum func_decl_flags (check.h)
};
enum ast_decl_type {
diff --git a/include/check.h b/include/check.h
@@ -1,13 +1,12 @@
#ifndef HARE_CHECK_H
#define HARE_CHECK_H
#include <stdbool.h>
-#include <stdint.h>
#include "identifier.h"
#include "types.h"
struct expression;
-enum function_flags {
+enum func_decl_flags {
FN_FINI = 1 << 0,
FN_INIT = 1 << 1,
FN_TEST = 1 << 2,
@@ -17,7 +16,7 @@ struct function_decl {
const struct type *type;
struct expression *body;
char *symbol;
- uint32_t flags; // enum function_flags
+ unsigned int flags; // enum function_flags
};
enum declaration_type {
diff --git a/include/types.h b/include/types.h
@@ -40,8 +40,30 @@ enum type_storage {
struct type;
+enum variadism {
+ VARIADISM_NONE,
+ VARIADISM_C,
+ VARIADISM_HARE,
+};
+
+enum function_flags {
+ FN_NORETURN = 1 << 0,
+};
+
+struct type_func_param {
+ const struct type *type;
+ struct type_func_parameter *next;
+};
+
+struct type_func {
+ const struct type *result;
+ enum variadism variadism;
+ struct type_func_param *params;
+ unsigned int flags; // enum function_flags
+};
+
enum pointer_flags {
- POINTER_FLAGS_NULLABLE = 1 << 0,
+ PTR_NULLABLE = 1 << 0,
};
struct type_pointer {
@@ -50,7 +72,7 @@ struct type_pointer {
};
enum type_flags {
- TYPE_FLAGS_CONST = 1 << 0,
+ TYPE_CONST = 1 << 0,
};
struct type {
diff --git a/src/check.c b/src/check.c
@@ -9,13 +9,27 @@ struct context {
};
static void
+scan_function(struct context *ctx, const struct ast_function_decl *decl)
+{
+ const struct ast_type fn_atype = {
+ .storage = TYPE_STORAGE_FUNCTION,
+ .flags = TYPE_CONST,
+ .function = decl->prototype,
+ };
+ const struct type *fntype = type_store_lookup_atype(
+ &ctx->store, &fn_atype);
+ assert(fntype);
+}
+
+static void
scan_declarations(struct context *ctx, const struct ast_decls *decls)
{
while (decls) {
const struct ast_decl *decl = &decls->decl;
switch (decl->decl_type) {
case AST_DECL_FUNC:
- assert(0); // TODO
+ scan_function(ctx, &decl->function);
+ break;
case AST_DECL_TYPE:
assert(0); // TODO
case AST_DECL_GLOBAL:
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->flags |= TYPE_FLAGS_CONST;
+ 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.flags |= POINTER_FLAGS_NULLABLE;
+ type->pointer.flags |= PTR_NULLABLE;
want(par, T_TIMES, NULL);
trace(TR_PARSE, "nullable");
/* fallthrough */
@@ -317,7 +317,7 @@ parse_type(struct parser *par, struct ast_type *type)
case T_LBRACKET:
assert(0); // TODO: Slices/arrays
case T_ATTR_NORETURN:
- type->function.noreturn = true;
+ type->function.flags |= FN_NORETURN;
want(par, T_FN, NULL);
/* fallthrough */
case T_FN:
@@ -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->flags & TYPE_FLAGS_CONST) ? "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.flags |= TYPE_FLAGS_CONST;
+ i->type.flags |= TYPE_CONST;
}
want(par, T_EQUAL, NULL);
parse_simple_expression(par, &i->init);
@@ -518,7 +518,7 @@ parse_fn_decl(struct parser *par, struct ast_function_decl *decl)
decl->flags |= FN_TEST;
break;
case T_ATTR_NORETURN:
- decl->prototype.noreturn = true;
+ decl->prototype.flags |= FN_NORETURN;
break;
default:
more = false;
@@ -540,7 +540,7 @@ parse_fn_decl(struct parser *par, struct ast_function_decl *decl)
trace(TR_PARSE, "%s%s%s%s%sfn %s [prototype] = [expr]",
decl->flags & FN_FINI ? "@fini " : "",
decl->flags & FN_INIT ? "@init " : "",
- decl->prototype.noreturn ? "@noreturn " : "",
+ decl->prototype.flags & FN_NORETURN ? "@noreturn " : "",
decl->flags & FN_TEST ? "@test " : "",
decl->symbol ? symbol : "", buf);
trleave(TR_PARSE, NULL);
diff --git a/src/type_store.c b/src/type_store.c
@@ -86,7 +86,7 @@ type_hash(struct type_store *store, const struct type *type)
static const struct type *
builtin_for_atype(const struct ast_type *atype)
{
- bool is_const = (atype->flags & TYPE_FLAGS_CONST) != 0;
+ bool is_const = (atype->flags & TYPE_CONST) != 0;
switch (atype->storage) {
case TYPE_STORAGE_BOOL:
return is_const ? &builtin_type_bool : &builtin_type_const_bool;
@@ -126,7 +126,7 @@ builtin_for_atype(const struct ast_type *atype)
return is_const ? &builtin_type_void : &builtin_type_const_void;
case TYPE_STORAGE_POINTER:
if (atype->pointer.referent->storage == TYPE_STORAGE_CHAR
- && atype->pointer.referent->flags == TYPE_FLAGS_CONST) {
+ && atype->pointer.referent->flags == TYPE_CONST) {
return &builtin_type_const_ptr_char;
}
return NULL;
diff --git a/src/types.c b/src/types.c
@@ -157,109 +157,109 @@ builtin_type_void = {
},
builtin_type_const_bool = {
.storage = TYPE_STORAGE_BOOL,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 4, // XXX: ARCH
.align = 4,
},
builtin_type_const_char = {
.storage = TYPE_STORAGE_CHAR,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 1,
.align = 1,
},
builtin_type_const_f32 = {
.storage = TYPE_STORAGE_F32,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 4,
.align = 4,
},
builtin_type_const_f64 = {
.storage = TYPE_STORAGE_F64,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 8,
.align = 8,
},
builtin_type_const_i8 = {
.storage = TYPE_STORAGE_I8,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 1,
.align = 1,
},
builtin_type_const_i16 = {
.storage = TYPE_STORAGE_I16,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 2,
.align = 2,
},
builtin_type_const_i32 = {
.storage = TYPE_STORAGE_I32,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 4,
.align = 4,
},
builtin_type_const_i64 = {
.storage = TYPE_STORAGE_I64,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 8,
.align = 8,
},
builtin_type_const_int = {
.storage = TYPE_STORAGE_INT,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 4, // XXX: ARCH
.align = 4,
},
builtin_type_const_u8 = {
.storage = TYPE_STORAGE_U8,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 1,
.align = 1,
},
builtin_type_const_u16 = {
.storage = TYPE_STORAGE_U16,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 2,
.align = 2,
},
builtin_type_const_u32 = {
.storage = TYPE_STORAGE_U32,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 4,
.align = 4,
},
builtin_type_const_u64 = {
.storage = TYPE_STORAGE_U64,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 8,
.align = 8,
},
builtin_type_const_uint = {
.storage = TYPE_STORAGE_UINT,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 4,
.align = 4,
},
builtin_type_const_uintptr = {
.storage = TYPE_STORAGE_UINTPTR,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 8, // XXX: ARCH
.align = 8,
},
builtin_type_const_rune = {
.storage = TYPE_STORAGE_RUNE,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 4,
.align = 4,
},
builtin_type_const_size = {
.storage = TYPE_STORAGE_SIZE,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 8, // XXX: ARCH
.align = 8,
},
builtin_type_const_void = {
.storage = TYPE_STORAGE_VOID,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 0,
.align = 0,
};
@@ -267,7 +267,7 @@ builtin_type_const_void = {
// Selected aggregate type singletons
const struct type builtin_type_const_ptr_char = {
.storage = TYPE_STORAGE_POINTER,
- .flags = TYPE_FLAGS_CONST,
+ .flags = TYPE_CONST,
.size = 8, // XXX: ARCH
.align = 8,
.pointer = {