Foreign variables, exporting some of the info to the c program, compile errors though

This commit is contained in:
Krzosa Karol
2022-06-18 16:01:05 +02:00
parent 0ed4f62557
commit e63dc0322a
7 changed files with 47 additions and 11 deletions

View File

@@ -208,7 +208,9 @@ enum {
function void
gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names){
if(is_flag_set(decl->flags, AST_FOREIGN)) gen("extern ");
gen_simple_decl(decl->type, decl->name, decl->parent_scope, scope_names);
if(is_flag_set(decl->flags, AST_FOREIGN)) return;
if(emit_value == DONT_EMIT_VALUE){
return;
@@ -645,6 +647,7 @@ insert_builtin_into_scope(Ast_Scope *p, String name, Ast_Type *type){
decl->parent_scope = p;
decl->state = DECL_RESOLVED;
insert_into_scope(p, decl);
pctx->all_types.add(type);
}
function void
@@ -821,6 +824,27 @@ typedef struct String{
}
}
#if 1
gen("Type_Info type_infos[] = {");
global_indent++;
For(pctx->all_types){
genln("{.kind = %d, .size = %d, .align = %d, .is_unsigned = %s, .type_id = %d, ",
(S32)it->kind, (S32)it->size, (S32)it->align, it->is_unsigned ? "true" : "false", it->type_id);
switch(it->kind){
case TYPE_POINTER: {
gen(".base = %d", it->base->type_id);
} break;
case TYPE_ARRAY: {
gen(".base = %d, ", it->base->type_id);
gen(".array_size = %d", it->arr.size);
}break;
// invalid_default_case;
}
gen("},");
}
global_indent--;
gen("};");
#endif
For(pctx->ordered_decls){
genln("");

View File

@@ -181,7 +181,8 @@ struct Parse_Ctx:Lexer{
Allocator *heap;
Arena stage_arena;
U64 type_ids;
Array<Ast_Type *> all_types;
S32 type_ids;
U64 unique_ids; // @Debug
Map type_map;
@@ -243,7 +244,7 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator)
ctx->ordered_decls = {ctx->heap};
ctx->type_map = {ctx->heap};
ctx->modules = {ctx->heap};
// ctx->files = {ctx->heap};
ctx->all_types = {ctx->heap};
bigint_allocator = ctx->perm;
arena_init(&ctx->stage_arena, "Compiler stage arena"_s);

View File

@@ -781,6 +781,9 @@ parse_decl(B32 is_global){
else if(token_match(TK_Identifier, TK_Colon)){
Ast_Expr *typespec = parse_expr();
Ast_Expr *expr = parse_assign_expr();
if(token_match_pound(intern_foreign))
set_flag(flags, AST_FOREIGN);
result = ast_var(tname, typespec, tname->intern_val, expr);
}

View File

@@ -14,10 +14,10 @@ Windows_Bitmap :: struct
hdc: HDC
dib: HBITMAP
Type_ID :: U64
Type_ID :: S32
Type_Info_Kind :: enum
TYPE_NONE
TYPE_S64 // FIRST_NUMERIC
TYPE_S64 :: 7 // FIRST_NUMERIC
TYPE_S32
TYPE_S16
TYPE_S8
@@ -64,7 +64,8 @@ Type_Info :: struct
struct_member_count: S32
fields: *Type_Info_Struct_Field
type_infos: []*Type_Info
type_infos_len: U32 #foreign
type_infos : *Type_Info #foreign
Bitmap :: struct

View File

@@ -1159,6 +1159,11 @@ resolve_decl(Ast_Decl *ast){
make_sure_value_is_compatible_with_type(node->pos, &op, type, EXPR_CAN_BE_NULL|TYPE_CAN_BE_NULL);
node->value = op.value;
if(is_flag_set(node->flags, AST_FOREIGN)){
if(node->expr) compiler_error(node->pos, "#foreign variable shouldn't have value");
}
BREAK();
}

View File

@@ -111,6 +111,7 @@ type_new(Allocator *allocator, Ast_Type_Kind kind, SizeU size, SizeU align){
result->size = size;
result->align = align;
result->type_id = pctx->type_ids++;
pctx->all_types.add(result);
return result;
}
@@ -118,6 +119,7 @@ function Ast_Type *
type_copy(Allocator *a, Ast_Type *type){
Ast_Type *result = exp_alloc_type(a, Ast_Type);
memory_copy(result, type, sizeof(Ast_Type));
pctx->all_types.add(result);
return result;
}

12
types.h
View File

@@ -9,7 +9,7 @@ enum Ast_Type_Kind{
TYPE_UNTYPED_INT,
TYPE_UNTYPED_FLOAT, // LAST_TYPED_NUMERIC
TYPE_UNTYPED_STRING,
TYPE_S64, // FIRST_NUMERIC
TYPE_S64 = 7, // FIRST_NUMERIC
TYPE_S32,
TYPE_S16,
TYPE_S8 ,
@@ -69,7 +69,7 @@ struct Ast;
struct Ast_Type;
struct Ast_Resolved_Member{
Intern_String name;
U64 offset;
S32 offset;
B32 visited;
INLINE_VALUE_FIELDS;
};
@@ -77,17 +77,17 @@ struct Ast_Resolved_Member{
#define ARRAY_SIZE_SLICE (-1)
struct Ast_Type{
Ast_Type_Kind kind;
SizeU size;
SizeU align;
S32 size;
S32 align;
S32 is_unsigned;
U64 type_id;
S32 type_id;
Ast *ast;
union{
Ast_Type *base;
struct{
Ast_Type *base;
S64 size;
S32 size;
// @note: if you have array with size "[32]"
// you still want to pass that array into
// a function that expects an array of size "[]"