stage1 人工重构ast以及type部分 修复vm的UB行为

This commit is contained in:
zzy
2026-08-14 18:10:58 +08:00
parent b0e6b406ac
commit c06d7247bb
7 changed files with 142 additions and 306 deletions

View File

@@ -99,9 +99,10 @@ static int spl_type_size(spl_type_t t) {
#define PUSH(v) \ #define PUSH(v) \
do { \ do { \
spl_val_t _pv = (spl_val_t)(v); \
if (vm->sp >= vm->config.max_stack_depth) \ if (vm->sp >= vm->config.max_stack_depth) \
VM_ERROR("stack overflow"); \ VM_ERROR("stack overflow"); \
vm->stacks.data[(vm->sp)++] = (spl_val_t)(v); \ vm->stacks.data[(vm->sp)++] = _pv; \
} while (0) } while (0)
#define POP() vm->stacks.data[--(vm->sp)] #define POP() vm->stacks.data[--(vm->sp)]

View File

@@ -1,18 +1,7 @@
// WRITE BY AI
/* spl_ast.c SPL AST 模块LL(1) 递归下降解析 + valid/dump/drop
*
* 设计:节点 kind 是唯一身份(扁平化)。无 `SPL_AST_EXPR`/`SPL_AST_BLOCK_ITEM`
* 等外壳类别--每个表达式/语句/类型节点直接用具体 kind。
* 子节点以 spl_ast_node_ref 引用ref 0 = 空union 数据由 kind 决定解释。
*/
#include "spl_ast.h" #include "spl_ast.h"
#include "spl_dbg.h" #include "spl_dbg.h"
#include "spl_dumptree.h" #include "spl_dumptree.h"
#include "spl_tok.h" #include "spl_tok.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *sdupn(const char *s, usize n) { static char *sdupn(const char *s, usize n) {
char *r = malloc(n + 1); char *r = malloc(n + 1);
@@ -38,8 +27,8 @@ typedef struct {
spl_ast_t *ast; spl_ast_t *ast;
usize pos; usize pos;
int failed; int failed;
int stop_as; /* 0 时后缀表达式不KW_ASfor 头部序列专用 */ int stop_as; /* 0 时后缀表达式不KW_AS for 专用 */
int stop_agg; /* 0 IDENT '{' 不作结构体字面量if/while/match 头部专用 */ int stop_agg; /* 0 IDENT '{' 不作结构体字面量 if/while/match 专用 */
} parser_t; } parser_t;
static inline int is_trivia(spl_tok_type_t t) { return t == TOK_ENDLINE || t == TOK_LINE_COMMENT; } static inline int is_trivia(spl_tok_type_t t) { return t == TOK_ENDLINE || t == TOK_LINE_COMMENT; }
@@ -71,7 +60,7 @@ static spl_ast_node_ref_t new_node(parser_t *p, spl_ast_node_kind_t kind, const
n.kind = kind; n.kind = kind;
if (tok) { if (tok) {
n.dbg.dbg_name = spl_ast_kind_name(kind); n.dbg.dbg_name = spl_ast_kind_name(kind);
n.dbg.fpath = tok->fname; n.dbg.fname = tok->fname;
n.dbg.line = (int)tok->line; n.dbg.line = (int)tok->line;
n.dbg.col = (int)tok->col; n.dbg.col = (int)tok->col;
} }
@@ -1852,7 +1841,7 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
for (usize i = 0; i < vec_size(*stack); i++) for (usize i = 0; i < vec_size(*stack); i++)
printf("%s", vec_at(*stack, i) ? " " : "| "); printf("%s", vec_at(*stack, i) ? " " : "| ");
printf("%s%s #%zu", last ? "`-" : "|-", spl_ast_kind_name(node->kind), node_ref); printf("%s%s #%zu", last ? "`-" : "|-", spl_ast_kind_name(node->kind), node_ref);
if (node->dbg.fpath) if (node->dbg.fname)
printf(" (%d:%d)", node->dbg.line, node->dbg.col); printf(" (%d:%d)", node->dbg.line, node->dbg.col);
printf("\n"); printf("\n");

View File

@@ -1,28 +0,0 @@
/* spl_builtin.c — 内置函数集中注册表实现 */
#include "spl_builtin.h"
#include "../stage0/include/utils.h"
#include <string.h>
static const spl_builtin_t k_builtins[] = {
{"sizeof", SPL_BUILTIN_SIZE_OF, 1, 1, 0},
{"bitsizeof", SPL_BUILTIN_BITSIZE_OF, 1, 1, 0},
{"alignof", SPL_BUILTIN_ALIGN_OF, 1, 1, 0},
{"offsetof", SPL_BUILTIN_OFFSET_OF, 2, 2, 0},
{"field_count", SPL_BUILTIN_FIELD_COUNT, 1, 1, 0},
{"dbg", SPL_BUILTIN_DBG, 0, -1, 1},
{"assert", SPL_BUILTIN_ASSERT, 1, 1, 1},
{"import", SPL_BUILTIN_IMPORT, 1, 1, 1},
};
const spl_builtin_t *spl_builtin_lookup(const char *name) {
if (!name)
return NULL;
for (usize i = 0; i < sizeof(k_builtins) / sizeof(k_builtins[0]); i++) {
if (strcmp(k_builtins[i].name, name) == 0)
return &k_builtins[i];
}
return NULL;
}
int spl_builtin_count(void) { return (int)(sizeof(k_builtins) / sizeof(k_builtins[0])); }

View File

@@ -1,43 +0,0 @@
/* spl_builtin.h — 内置函数集中注册表
*
* @builtin 名称/类别/参数/返回类型的单一事实来源。
* sema类型推导与 ast2irIR 发射)各自按 kind 分发实现。
*/
#ifndef __SPL_BUILTIN_H__
#define __SPL_BUILTIN_H__
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
SPL_BUILTIN_SIZE_OF, /* @sizeof(T) -> usize */
SPL_BUILTIN_BITSIZE_OF, /* @bitsizeof(T) -> usize */
SPL_BUILTIN_ALIGN_OF, /* @alignof(T) -> usize */
SPL_BUILTIN_OFFSET_OF, /* @offsetof(T, field) -> usize */
SPL_BUILTIN_FIELD_COUNT, /* @field_count(T) -> usize */
SPL_BUILTIN_DBG, /* @dbg(args...) -> void */
SPL_BUILTIN_ASSERT, /* @assert(cond) -> void */
SPL_BUILTIN_IMPORT, /* @import(path) -> 模块 (预留) */
SPL_BUILTIN_COUNT
} spl_builtin_kind_t;
typedef struct {
const char *name;
spl_builtin_kind_t kind;
int min_args; /* 参数个数下限 */
int max_args; /* 参数个数上限,-1 = 不限 */
int ret_is_void; /* 1 = void 返回 */
} spl_builtin_t;
/* 按名称查找内置;未注册返回 NULL */
const spl_builtin_t *spl_builtin_lookup(const char *name);
/* 内置总数 */
int spl_builtin_count(void);
#ifdef __cplusplus
}
#endif
#endif /* __SPL_BUILTIN_H__ */

View File

@@ -4,10 +4,12 @@
#include "spl_tok.h" #include "spl_tok.h"
typedef struct { typedef struct {
const char *dbg_name; const char *fname;
const char *fpath;
int line; int line;
int col; int col;
const char *dbg_name;
// TODO
} spl_dbg_node_t; } spl_dbg_node_t;
#define SPL_FATAL(tok, fmt, ...) \ #define SPL_FATAL(tok, fmt, ...) \

View File

@@ -1,160 +1,87 @@
// WRITE BY AI
/* spl_type.c SPL 类型系统type/def 两张完全独立的 arena 表 + 构造器 + dump
* type_table 按 spl_type_id_t 存匿名类型def_table 按 spl_def_id_t 存命名实体。
* 两表 id 空间互不相干0 均保留为 error。 */
#include "spl_type.h" #include "spl_type.h"
#include <stdio.h> static usize spl_type_hash(spl_type_node_t n) { return 0; }
#include <string.h> static int spl_type_eq(spl_type_node_t n1, spl_type_node_t n2) { return 1; }
static spl_type_id_t type_push(spl_type_t *type) { spl_type_id_t spl_type_node_push(spl_type_t *type, spl_type_node_t type_node) {
spl_type_node_t n; spl_type_id_t ret = 0;
memset(&n, 0, sizeof n); int ok = map_get(type->type_map, type_node, &ret);
n.kind = SPL_TYPE_VOID; if (ok) {
vec_push(type->type_table, n); return ret;
return type->type_table.size - 1; }
map_put(type->type_map, type_node, vec_size(type->type_table));
vec_push(type->type_table, type_node);
return vec_size(type->type_table) - 1;
} }
/* 内置整型去重:同 bits/signed 复用同一 id类型唯一化 */ spl_type_id_t spl_type_def_alloc(spl_type_t *type) {
spl_type_id_t spl_type_int(spl_type_t *type, usize bits, int is_signed) { vec_push(type->def_table, (spl_def_node_t){0});
for (usize i = 1; i < type->type_table.size; i++) { return vec_size(type->def_table) - 1;
spl_type_node_t *n = &type->type_table.data[i];
if (n->kind == SPL_TYPE_INT && n->int_type.bits == bits &&
n->int_type.is_signed == is_signed)
return i;
}
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_INT;
type->type_table.data[id].int_type.bits = bits;
type->type_table.data[id].int_type.is_signed = is_signed;
return id;
}
/* 内置浮点去重 */
spl_type_id_t spl_type_float(spl_type_t *type, usize bits) {
for (usize i = 1; i < type->type_table.size; i++) {
spl_type_node_t *n = &type->type_table.data[i];
if (n->kind == SPL_TYPE_FLOAT && n->float_type.bits == bits)
return i;
}
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_FLOAT;
type->type_table.data[id].float_type.bits = bits;
return id;
}
spl_type_id_t spl_type_void(spl_type_t *type) {
for (usize i = 1; i < type->type_table.size; i++)
if (type->type_table.data[i].kind == SPL_TYPE_VOID)
return i;
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_VOID;
return id;
}
spl_type_id_t spl_type_bool(spl_type_t *type) {
for (usize i = 1; i < type->type_table.size; i++)
if (type->type_table.data[i].kind == SPL_TYPE_BOOL)
return i;
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_BOOL;
return id;
}
spl_type_id_t spl_type_ptr(spl_type_t *type, spl_type_id_t val) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_PTR;
type->type_table.data[id].ptr_pointee = val;
return id;
}
spl_type_id_t spl_type_slice(spl_type_t *type, spl_type_id_t val) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_SLICE;
type->type_table.data[id].slice_element = val;
return id;
}
spl_type_id_t spl_type_range(spl_type_t *type, spl_type_id_t val) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_RANGE;
type->type_table.data[id].range_element = val;
return id;
}
spl_type_id_t spl_type_array(spl_type_t *type, spl_type_id_t val, usize len) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_ARRAY;
type->type_table.data[id].array_type.element = val;
type->type_table.data[id].array_type.len = len;
return id;
}
/* 绑定节点/别名引用type 节点指向被引用的类型 id
* (供未来"底层同型但类型系统不认"的 newtype 使用,暂不参与注册) */
spl_type_id_t spl_type_tid(spl_type_t *type, spl_type_id_t val) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_ID;
type->type_table.data[id].type_id = val;
return id;
}
/* 聚合类型:默认 STRUCT字段类型列表 */
spl_type_id_t spl_type_agg(spl_type_t *type, spl_type_id_vec_t fields) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_STRUCT;
type->type_table.data[id].agg_field_types = fields;
return id;
}
spl_type_id_t spl_type_enum(spl_type_t *type, spl_type_id_vec_t variants, spl_type_id_t tag) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_ENUM;
type->type_table.data[id].enum_type.variants = variants;
type->type_table.data[id].enum_type.tag_type = tag;
return id;
}
spl_type_id_t spl_type_fn(spl_type_t *type, spl_type_id_vec_t params, spl_type_id_t ret) {
spl_type_id_t id = type_push(type);
type->type_table.data[id].kind = SPL_TYPE_FN;
type->type_table.data[id].fn_type.params = params;
type->type_table.data[id].fn_type.ret = ret;
return id;
} }
void spl_type_init(spl_type_t *type) { void spl_type_init(spl_type_t *type) {
vec_init(type->type_table); vec_init(type->type_table);
vec_push(type->type_table, (spl_type_node_t){0});
map_init(type->type_map, spl_type_hash, spl_type_eq);
vec_init(type->def_table); vec_init(type->def_table);
/* 两表 id 0 均保留为 error */ vec_push(type->def_table, (spl_def_node_t){0});
type_push(type);
spl_type_def_alloc(type); spl_type_node_push(type, (spl_type_node_t){.kind = SPL_TYPE_VOID});
/* 关键字/内置类型(去重后 i8..u64 等各占一个 iddef 由 sema 登记) */ spl_type_node_push(type, (spl_type_node_t){.kind = SPL_TYPE_BOOL});
spl_type_void(type); spl_type_node_push(type, (spl_type_node_t){
spl_type_bool(type); .kind = SPL_TYPE_INT,
spl_type_int(type, 8, 1); .int_type.bits = 8,
spl_type_int(type, 8, 0); .int_type.is_signed = false,
spl_type_int(type, 16, 1); });
spl_type_int(type, 16, 0); spl_type_node_push(type, (spl_type_node_t){
spl_type_int(type, 32, 1); .kind = SPL_TYPE_INT,
spl_type_int(type, 32, 0); .int_type.bits = 16,
spl_type_int(type, 64, 1); .int_type.is_signed = false,
spl_type_int(type, 64, 0); });
spl_type_float(type, 32); spl_type_node_push(type, (spl_type_node_t){
spl_type_float(type, 64); .kind = SPL_TYPE_INT,
.int_type.bits = 32,
.int_type.is_signed = false,
});
spl_type_node_push(type, (spl_type_node_t){
.kind = SPL_TYPE_INT,
.int_type.bits = 64,
.int_type.is_signed = false,
});
spl_type_node_push(type, (spl_type_node_t){
.kind = SPL_TYPE_INT,
.int_type.bits = 8,
.int_type.is_signed = true,
});
spl_type_node_push(type, (spl_type_node_t){
.kind = SPL_TYPE_INT,
.int_type.bits = 16,
.int_type.is_signed = true,
});
spl_type_node_push(type, (spl_type_node_t){
.kind = SPL_TYPE_INT,
.int_type.bits = 32,
.int_type.is_signed = true,
});
spl_type_node_push(type, (spl_type_node_t){
.kind = SPL_TYPE_INT,
.int_type.bits = 64,
.int_type.is_signed = true,
});
spl_type_node_push(type, (spl_type_node_t){.kind = SPL_TYPE_FLOAT, .float_type.bits = 32});
spl_type_node_push(type, (spl_type_node_t){.kind = SPL_TYPE_FLOAT, .float_type.bits = 64});
} }
void spl_type_drop(spl_type_t *type) { void spl_type_drop(spl_type_t *type) {
/* type 节点持有的内嵌 vec */ vec_for(type->type_table, i) {
for (usize i = 0; i < type->type_table.size; i++) { spl_type_node_t *n = &vec_at(type->type_table, i);
spl_type_node_t *n = &type->type_table.data[i];
switch (n->kind) { switch (n->kind) {
case SPL_TYPE_STRUCT: case SPL_TYPE_STRUCT:
case SPL_TYPE_UNION: case SPL_TYPE_UNION:
vec_free(n->agg_field_types); vec_free(n->agg_field_types);
break; break;
case SPL_TYPE_ENUM: case SPL_TYPE_ENUM:
vec_free(n->enum_type.variants); vec_free(n->adt_type.variants);
break; break;
case SPL_TYPE_FN: case SPL_TYPE_FN:
vec_free(n->fn_type.params); vec_free(n->fn_type.params);
@@ -163,9 +90,9 @@ void spl_type_drop(spl_type_t *type) {
break; break;
} }
} }
/* def 节点持有的内嵌 vec */
for (usize i = 0; i < type->def_table.size; i++) { vec_for(type->def_table, i) {
spl_def_node_t *d = &type->def_table.data[i]; spl_def_node_t *d = &vec_at(type->def_table, i);
switch (d->kind) { switch (d->kind) {
case SPL_DEF_AGG: case SPL_DEF_AGG:
vec_free(d->agg_def); vec_free(d->agg_def);
@@ -177,52 +104,22 @@ void spl_type_drop(spl_type_t *type) {
break; break;
} }
} }
map_free(type->type_map);
vec_free(type->type_table); vec_free(type->type_table);
vec_free(type->def_table); vec_free(type->def_table);
} }
spl_type_id_t spl_type_alloc(spl_type_t *type) { return type_push(type); }
spl_type_node_t *spl_type_node(spl_type_t *type, spl_type_id_t id) { spl_type_node_t *spl_type_node(spl_type_t *type, spl_type_id_t id) {
if (!id || id >= type->type_table.size) if (!id || id >= vec_size(type->type_table))
return NULL; return NULL;
return &type->type_table.data[id]; return &vec_at(type->type_table, id);
}
spl_def_id_t spl_type_def_alloc(spl_type_t *type) {
spl_def_node_t d;
memset(&d, 0, sizeof d);
d.kind = SPL_DEF_NONE;
vec_push(type->def_table, d);
return type->def_table.size - 1;
} }
spl_def_node_t *spl_type_def(spl_type_t *type, spl_def_id_t id) { spl_def_node_t *spl_type_def(spl_type_t *type, spl_def_id_t id) {
if (!id || id >= type->def_table.size) if (!id || id >= vec_size(type->def_table))
return NULL; return NULL;
return &type->def_table.data[id]; return &vec_at(type->def_table, id);
}
static const char *def_kind_name(spl_def_node_t *d) {
switch (d->kind) {
case SPL_DEF_NONE:
return "none";
case SPL_DEF_BUILTIN:
return "builtin";
case SPL_DEF_VAR:
return "var";
case SPL_DEF_MEMBER:
return "member";
case SPL_DEF_FN_PARAMS:
return "fn_params";
case SPL_DEF_AGG:
return "agg";
case SPL_DEF_DISTINCT:
return "distinct";
case SPL_DEF_ALIAS:
return "alias";
}
return "?";
} }
void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id) { void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id) {
@@ -232,6 +129,9 @@ void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id) {
return; return;
} }
switch (n->kind) { switch (n->kind) {
case SPL_TYPE_ERROR:
printf("error");
break;
case SPL_TYPE_VOID: case SPL_TYPE_VOID:
printf("void"); printf("void");
break; break;
@@ -268,7 +168,7 @@ void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id) {
printf("union{%zu fields}", n->agg_field_types.size); printf("union{%zu fields}", n->agg_field_types.size);
break; break;
case SPL_TYPE_ENUM: case SPL_TYPE_ENUM:
printf("enum{%zu variants}", n->enum_type.variants.size); printf("enum{%zu variants}", n->adt_type.variants.size);
break; break;
case SPL_TYPE_FN: { case SPL_TYPE_FN: {
printf("fn("); printf("fn(");
@@ -288,38 +188,66 @@ void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id) {
} }
void spl_type_def_dump(spl_type_t *type, spl_def_id_t id) { void spl_type_def_dump(spl_type_t *type, spl_def_id_t id) {
spl_def_node_t *d = spl_type_def(type, id); spl_def_node_t *node = spl_type_def(type, id);
if (!d) { if (!node) {
printf("(err)"); printf("(err)");
return; return;
} }
printf("kind=%s type_id=%zu", def_kind_name(d), d->type_id);
switch (d->kind) { const char *def_kind_name = "null";
case SPL_DEF_VAR: switch (node->kind) {
printf(" var=%s", d->var_def.name ? d->var_def.name : "?"); case SPL_DEF_ERROR:
def_kind_name = "error";
break;
case SPL_DEF_SCALAR:
def_kind_name = "scalar";
break; break;
case SPL_DEF_MEMBER: case SPL_DEF_MEMBER:
printf(" member=%s", d->var_def.name ? d->var_def.name : "?"); def_kind_name = "member";
break;
case SPL_DEF_VAR:
def_kind_name = "var";
break;
case SPL_DEF_FN_PARAMS:
def_kind_name = "params";
break;
case SPL_DEF_AGG:
def_kind_name = "agg";
break;
case SPL_DEF_DISTINCT:
def_kind_name = "newtype";
break;
case SPL_DEF_ALIAS:
def_kind_name = "sametypes";
break;
}
printf("kind=%s type_id=%zu", def_kind_name, node->type_id);
switch (node->kind) {
case SPL_DEF_VAR:
printf(" var=%s", node->var_def.name ? node->var_def.name : "?");
break;
case SPL_DEF_MEMBER:
printf(" member=%s", node->var_def.name ? node->var_def.name : "?");
break; break;
case SPL_DEF_ALIAS: case SPL_DEF_ALIAS:
case SPL_DEF_DISTINCT: case SPL_DEF_DISTINCT:
printf(" type=%s", d->type_def.name ? d->type_def.name : "?"); printf(" type=%s", node->type_def.name ? node->type_def.name : "?");
break; break;
case SPL_DEF_AGG: case SPL_DEF_AGG:
printf(" agg{"); printf(" agg{");
for (usize i = 0; i < d->agg_def.size; i++) { for (usize i = 0; i < node->agg_def.size; i++) {
if (i) if (i)
printf(","); printf(",");
printf("%s", d->agg_def.data[i].name ? d->agg_def.data[i].name : "?"); printf("%s", node->agg_def.data[i].name ? node->agg_def.data[i].name : "?");
} }
printf("}"); printf("}");
break; break;
case SPL_DEF_FN_PARAMS: case SPL_DEF_FN_PARAMS:
printf(" fn("); printf(" fn(");
for (usize i = 0; i < d->fn_params_def.size; i++) { for (usize i = 0; i < node->fn_params_def.size; i++) {
if (i) if (i)
printf(","); printf(",");
printf("%s", d->fn_params_def.data[i].name ? d->fn_params_def.data[i].name : "?"); printf("%s", node->fn_params_def.data[i].name ? node->fn_params_def.data[i].name : "?");
} }
printf(")"); printf(")");
break; break;

View File

@@ -10,13 +10,14 @@ typedef VEC(spl_def_id_t) spl_def_id_vec_t;
typedef struct { typedef struct {
enum { enum {
SPL_TYPE_ERROR,
SPL_TYPE_VOID, SPL_TYPE_VOID,
SPL_TYPE_BOOL, SPL_TYPE_BOOL,
SPL_TYPE_INT, SPL_TYPE_INT,
SPL_TYPE_FLOAT, SPL_TYPE_FLOAT,
SPL_TYPE_PTR, SPL_TYPE_PTR,
SPL_TYPE_SLICE, SPL_TYPE_SLICE, // 未来拥有泛型后删除
SPL_TYPE_RANGE, SPL_TYPE_RANGE, // 未来拥有泛型后删除
SPL_TYPE_ARRAY, SPL_TYPE_ARRAY,
SPL_TYPE_STRUCT, SPL_TYPE_STRUCT,
SPL_TYPE_UNION, SPL_TYPE_UNION,
@@ -24,6 +25,10 @@ typedef struct {
SPL_TYPE_FN, SPL_TYPE_FN,
SPL_TYPE_ID, SPL_TYPE_ID,
} kind; } kind;
struct {
enum { AUTO, EXTERN_C, PACKED } mode;
usize fixed_align_bits;
} layout;
union { union {
struct { struct {
usize bits; usize bits;
@@ -43,7 +48,7 @@ typedef struct {
struct { struct {
spl_type_id_vec_t variants; spl_type_id_vec_t variants;
spl_type_id_t tag_type; spl_type_id_t tag_type;
} enum_type; // ADT } adt_type; // ADT
struct { struct {
spl_type_id_vec_t params; spl_type_id_vec_t params;
spl_type_id_t ret; spl_type_id_t ret;
@@ -63,8 +68,8 @@ typedef VEC(spl_var_def_t) spl_var_def_vec_t;
typedef struct { typedef struct {
enum { enum {
SPL_DEF_NONE, SPL_DEF_ERROR,
SPL_DEF_BUILTIN, SPL_DEF_SCALAR,
SPL_DEF_MEMBER, SPL_DEF_MEMBER,
SPL_DEF_VAR, SPL_DEF_VAR,
SPL_DEF_FN_PARAMS, SPL_DEF_FN_PARAMS,
@@ -79,20 +84,14 @@ typedef struct {
spl_var_def_vec_t fn_params_def; spl_var_def_vec_t fn_params_def;
spl_var_def_t type_def; spl_var_def_t type_def;
}; };
int source_loc;
enum {
SPL_FLAG_NONE,
} flag;
} spl_def_node_t; } spl_def_node_t;
typedef VEC(spl_def_node_t) spl_def_node_vec_t; typedef VEC(spl_def_node_t) spl_def_node_vec_t;
/* typedef MAP(spl_type_node_t, usize) spl_type_node_map_t;
SPL 设计是严格区分类型做到类型和名称无关即
type (类型名) = (匿名类型)
好处是递归使用可以直接操作类型名的映射的提前分配的匿名类型的id
*/
typedef struct { typedef struct {
spl_type_node_vec_t type_table; spl_type_node_vec_t type_table;
// TODO hashconsing
spl_type_node_map_t type_map;
spl_def_node_vec_t def_table; spl_def_node_vec_t def_table;
} spl_type_t; } spl_type_t;
@@ -102,22 +101,10 @@ void spl_type_drop(spl_type_t *type);
void spl_type_def_dump(spl_type_t *type, spl_def_id_t id); void spl_type_def_dump(spl_type_t *type, spl_def_id_t id);
void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id); void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id);
spl_type_id_t spl_type_alloc(spl_type_t *type);
spl_type_node_t *spl_type_node(spl_type_t *type, spl_type_id_t id); spl_type_node_t *spl_type_node(spl_type_t *type, spl_type_id_t id);
spl_def_id_t spl_type_def_alloc(spl_type_t *type);
spl_def_node_t *spl_type_def(spl_type_t *type, spl_def_id_t id); spl_def_node_t *spl_type_def(spl_type_t *type, spl_def_id_t id);
spl_type_id_t spl_type_void(spl_type_t *type); spl_type_id_t spl_type_def_alloc(spl_type_t *type);
spl_type_id_t spl_type_bool(spl_type_t *type); spl_type_id_t spl_type_node_push(spl_type_t *type, spl_type_node_t type_node);
spl_type_id_t spl_type_int(spl_type_t *type, usize bits, int is_signed);
spl_type_id_t spl_type_float(spl_type_t *type, usize bits);
spl_type_id_t spl_type_ptr(spl_type_t *type, spl_type_id_t val);
spl_type_id_t spl_type_slice(spl_type_t *type, spl_type_id_t val);
spl_type_id_t spl_type_range(spl_type_t *type, spl_type_id_t val);
spl_type_id_t spl_type_array(spl_type_t *type, spl_type_id_t val, usize len);
spl_type_id_t spl_type_tid(spl_type_t *type, spl_type_id_t val);
spl_type_id_t spl_type_agg(spl_type_t *type, spl_type_id_vec_t fields);
spl_type_id_t spl_type_enum(spl_type_t *type, spl_type_id_vec_t variants, spl_type_id_t tag);
spl_type_id_t spl_type_fn(spl_type_t *type, spl_type_id_vec_t params, spl_type_id_t ret);
#endif /* __SPL_TYPE_H__ */ #endif /* __SPL_TYPE_H__ */