Move types to Core_Ctx

This commit is contained in:
Krzosa Karol
2023-01-01 16:27:42 +01:00
parent d441abcddb
commit d120d05eee
8 changed files with 191 additions and 131 deletions

View File

@@ -23,7 +23,7 @@ _ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0){
CORE_Static Ast_Atom * CORE_Static Ast_Atom *
ast_str(Token *pos, Intern_String string){ ast_str(Token *pos, Intern_String string){
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM); AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->type = untyped_string; result->type = pctx->untyped_string;
result->intern_val = string; result->intern_val = string;
return result; return result;
} }
@@ -39,14 +39,14 @@ CORE_Static Ast_Atom *
ast_bool(Token *pos, B32 bool_val){ ast_bool(Token *pos, B32 bool_val){
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM); AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->bool_val = bool_val; result->bool_val = bool_val;
result->type = untyped_bool; result->type = pctx->untyped_bool;
return result; return result;
} }
CORE_Static Ast_Atom * CORE_Static Ast_Atom *
ast_float(Token *pos, F64 value){ ast_float(Token *pos, F64 value){
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM); AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->type = untyped_float; result->type = pctx->untyped_float;
result->f64_val = value; result->f64_val = value;
return result; return result;
} }
@@ -54,7 +54,7 @@ ast_float(Token *pos, F64 value){
CORE_Static Ast_Atom * CORE_Static Ast_Atom *
ast_int(Token *pos, BigInt val){ ast_int(Token *pos, BigInt val){
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM); AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->type = untyped_int; result->type = pctx->untyped_int;
result->big_int_val = bigint_copy(pctx->perm, &val); result->big_int_val = bigint_copy(pctx->perm, &val);
return result; return result;
} }
@@ -256,7 +256,7 @@ ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
CORE_Static Ast_Decl * CORE_Static Ast_Decl *
ast_type(Token *pos, Intern_String name, Ast_Type *type){ ast_type(Token *pos, Intern_String name, Ast_Type *type){
AST_NEW(Decl, TYPE, pos, AST_DECL); AST_NEW(Decl, TYPE, pos, AST_DECL);
result->type = type_type; result->type = pctx->type_type;
result->type_val = type; result->type_val = type;
result->name = name; result->name = name;
return result; return result;
@@ -332,7 +332,7 @@ CORE_Static Value
value_bool(B32 v){ value_bool(B32 v){
Value value; Value value;
value.bool_val = v; value.bool_val = v;
value.type = untyped_bool; value.type = pctx->untyped_bool;
return value; return value;
} }
@@ -340,14 +340,14 @@ CORE_Static Value
value_int(BigInt b){ value_int(BigInt b){
Value value; Value value;
value.big_int_val = b; value.big_int_val = b;
value.type = untyped_int; value.type = pctx->untyped_int;
return value; return value;
} }
CORE_Static Value CORE_Static Value
value_int(S64 s64){ value_int(S64 s64){
Value value; Value value;
value.type = untyped_int; value.type = pctx->untyped_int;
bigint_init_signed(&value.big_int_val, s64); bigint_init_signed(&value.big_int_val, s64);
return value; return value;
} }
@@ -356,7 +356,7 @@ CORE_Static Value
value_float(F64 b){ value_float(F64 b){
Value value; Value value;
value.f64_val = b; value.f64_val = b;
value.type = untyped_float; value.type = pctx->untyped_float;
return value; return value;
} }
@@ -364,7 +364,7 @@ CORE_Static Value
value_float(BigInt a){ value_float(BigInt a){
Value value; Value value;
value.f64_val = bigint_as_float(&a); value.f64_val = bigint_as_float(&a);
value.type = untyped_float; value.type = pctx->untyped_float;
return value; return value;
} }

View File

@@ -213,7 +213,7 @@ gen_value(Token *pos, Value a){
gen("%s%Q", string, postfix); gen("%s%Q", string, postfix);
}break; }break;
case TYPE_POINTER:{ case TYPE_POINTER:{
if(a.type == type_pointer_to_char){ if(a.type == pctx->type_pointer_to_char){
gen("\"%Q\"", a.intern_val); gen("\"%Q\"", a.intern_val);
} }
else{ else{
@@ -377,7 +377,7 @@ gen_expr(Ast_Expr *ast){
CASE(INDEX, Index){ CASE(INDEX, Index){
gen("("); gen("(");
gen_expr(node->expr); gen_expr(node->expr);
if(node->index_original_type == type_string || node->index_original_type == untyped_string){ if(node->index_original_type == pctx->type_string || node->index_original_type == pctx->untyped_string){
gen(".str"); gen(".str");
} }
else if(is_slice(node->index_original_type)){ else if(is_slice(node->index_original_type)){

View File

@@ -90,7 +90,63 @@ pctx->op_info_table[18] = {pctx->intern("~"_s), "NEG"_s, TK_Neg, 0, 1};
pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1}; pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
/*END*/ /*END*/
init_type(); // Init types
pctx->type__void = {TYPE_VOID};
pctx->type__string = {TYPE_STRING, sizeof(String), __alignof(String)};
pctx->type__bool = {TYPE_BOOL, sizeof(bool), __alignof(bool)};
pctx->type__type = {TYPE_TYPE, sizeof(S64), __alignof(S64)};
pctx->type__f32 = {TYPE_F32, sizeof(F32), __alignof(F32)};
pctx->type__f64 = {TYPE_F64, sizeof(F64), __alignof(F64)};
pctx->type__s8 = {TYPE_S8, sizeof(S8), __alignof(S8)};
pctx->type__s16 = {TYPE_S16, sizeof(S16), __alignof(S16)};
pctx->type__s32 = {TYPE_S32, sizeof(S32), __alignof(S32)};
pctx->type__s64 = {TYPE_S64, sizeof(S64), __alignof(S64)};
pctx->type__u8 = {TYPE_U8, sizeof(U8), __alignof(U8), true};
pctx->type__u16 = {TYPE_U16, sizeof(U16), __alignof(U16), true};
pctx->type__u32 = {TYPE_U32, sizeof(U32), __alignof(U32), true};
pctx->type__u64 = {TYPE_U64, sizeof(U64), __alignof(U64), true};
pctx->type__untyped_bool = {TYPE_UNTYPED_BOOL, sizeof(bool), __alignof(bool)};
pctx->type__untyped_int = {TYPE_UNTYPED_INT, sizeof(S64), __alignof(S64)};
pctx->type__untyped_string = {TYPE_UNTYPED_STRING, sizeof(String), __alignof(String)};
pctx->type__untyped_float = {TYPE_UNTYPED_FLOAT, sizeof(double), __alignof(double)};
pctx->type__char = {TYPE_CHAR, sizeof(char), __alignof(char)};
pctx->type__int = {TYPE_INT, sizeof(int), __alignof(int)};
pctx->type_char = &pctx->type__char;
pctx->type_int = &pctx->type__int;
pctx->type_void = &pctx->type__void;
//pctx->type_any; // Needs to be inited at runtime
pctx->type_type = &pctx->type__type;
pctx->type_string = &pctx->type__string;
pctx->type_bool = &pctx->type__bool;
pctx->type_f32 = &pctx->type__f32;
pctx->type_f64 = &pctx->type__f64;
pctx->type_s8 = &pctx->type__s8 ;
pctx->type_s16 = &pctx->type__s16;
pctx->type_s32 = &pctx->type__s32;
pctx->type_s64 = &pctx->type__s64;
pctx->type_u8 = &pctx->type__u8 ;
pctx->type_u16 = &pctx->type__u16;
pctx->type_u32 = &pctx->type__u32;
pctx->type_u64 = &pctx->type__u64;
pctx->untyped_string = &pctx->type__untyped_string;
pctx->untyped_bool = &pctx->type__untyped_bool;
pctx->untyped_int = &pctx->type__untyped_int;
pctx->untyped_float = &pctx->type__untyped_float;
pctx->type_pointer_to_char = type_pointer(pctx->type_char);
pctx->type_pointer_to_void = type_pointer(pctx->type_void);
// Init paths // Init paths
ctx->exe_folder = os_get_exe_dir(ctx->perm); ctx->exe_folder = os_get_exe_dir(ctx->perm);
@@ -227,22 +283,22 @@ compile_file_to_string(Allocator *allocator, String filename) {
{ {
Ast_Module *module = add_module(0, pctx->intern("Language.core"_s)); Ast_Module *module = add_module(0, pctx->intern("Language.core"_s));
{ {
insert_builtin_type_into_scope(module, "S64"_s, type_s64); insert_builtin_type_into_scope(module, "S64"_s, pctx->type_s64);
insert_builtin_type_into_scope(module, "S32"_s, type_s32); insert_builtin_type_into_scope(module, "S32"_s, pctx->type_s32);
insert_builtin_type_into_scope(module, "S16"_s, type_s16); insert_builtin_type_into_scope(module, "S16"_s, pctx->type_s16);
insert_builtin_type_into_scope(module, "S8"_s, type_s8); insert_builtin_type_into_scope(module, "S8"_s, pctx->type_s8);
insert_builtin_type_into_scope(module, "int"_s, type_int); insert_builtin_type_into_scope(module, "int"_s, pctx->type_int);
insert_builtin_type_into_scope(module, "char"_s, type_char); insert_builtin_type_into_scope(module, "char"_s, pctx->type_char);
insert_builtin_type_into_scope(module, "U64"_s, type_u64); insert_builtin_type_into_scope(module, "U64"_s, pctx->type_u64);
insert_builtin_type_into_scope(module, "U32"_s, type_u32); insert_builtin_type_into_scope(module, "U32"_s, pctx->type_u32);
insert_builtin_type_into_scope(module, "U16"_s, type_u16); insert_builtin_type_into_scope(module, "U16"_s, pctx->type_u16);
insert_builtin_type_into_scope(module, "U8"_s, type_u8); insert_builtin_type_into_scope(module, "U8"_s, pctx->type_u8);
insert_builtin_type_into_scope(module, "F64"_s, type_f64); insert_builtin_type_into_scope(module, "F64"_s, pctx->type_f64);
insert_builtin_type_into_scope(module, "F32"_s, type_f32); insert_builtin_type_into_scope(module, "F32"_s, pctx->type_f32);
insert_builtin_type_into_scope(module, "void"_s, type_void); insert_builtin_type_into_scope(module, "void"_s, pctx->type_void);
insert_builtin_type_into_scope(module, "Bool"_s, type_bool); insert_builtin_type_into_scope(module, "Bool"_s, pctx->type_bool);
insert_builtin_type_into_scope(module, "String"_s, type_string); insert_builtin_type_into_scope(module, "String"_s, pctx->type_string);
insert_builtin_type_into_scope(module, "Type"_s, type_type); insert_builtin_type_into_scope(module, "Type"_s, pctx->type_type);
} }
{ {
@@ -251,14 +307,14 @@ compile_file_to_string(Allocator *allocator, String filename) {
decl->state = DECL_RESOLVED; decl->state = DECL_RESOLVED;
Value v1 = {}; Value v1 = {};
v1.type = untyped_string; v1.type = pctx->untyped_string;
v1.intern_val = pctx->intern(OS_NAME); v1.intern_val = pctx->intern(OS_NAME);
Ast_Decl *const_os1 = ast_const(&pctx->null_token, pctx->intern("OSName"_s), v1); Ast_Decl *const_os1 = ast_const(&pctx->null_token, pctx->intern("OSName"_s), v1);
const_os1->state = DECL_RESOLVED; const_os1->state = DECL_RESOLVED;
insert_into_scope(scope, const_os1); insert_into_scope(scope, const_os1);
Value v2 = {}; Value v2 = {};
v1.type = untyped_string; v1.type = pctx->untyped_string;
v1.intern_val = pctx->intern(OS_NAME_LOWER); v1.intern_val = pctx->intern(OS_NAME_LOWER);
Ast_Decl *const_os2 = ast_const(&pctx->null_token, pctx->intern("OSNameLower"_s), v2); Ast_Decl *const_os2 = ast_const(&pctx->null_token, pctx->intern("OSNameLower"_s), v2);
const_os2->state = DECL_RESOLVED; const_os2->state = DECL_RESOLVED;
@@ -276,8 +332,8 @@ compile_file_to_string(Allocator *allocator, String filename) {
// so we mark where it ends // so we mark where it ends
pctx->base_language_ordered_decl_len = length(&pctx->ordered_decls); pctx->base_language_ordered_decl_len = length(&pctx->ordered_decls);
Ast_Decl *any_decl = search_for_single_decl(module, pctx->intern("Any"_s)); Ast_Decl *any_decl = search_for_single_decl(module, pctx->intern("Any"_s));
assert(any_decl->type == type_type); assert(any_decl->type == pctx->type_type);
type_any = any_decl->type_val; pctx->type_any = any_decl->type_val;
} }
Ast_Module *module = add_module(0, pctx->intern(filename), true); Ast_Module *module = add_module(0, pctx->intern(filename), true);

View File

@@ -8,6 +8,7 @@
with 2 allocators, and simplify stuff with 2 allocators, and simplify stuff
@! Cleanup big int allocator @! Cleanup big int allocator
@! Replace iterator interface @! Replace iterator interface
@! Errors and logs should be placed on the compiler context
@! Clean way to free all memory and reset the compiler @! Clean way to free all memory and reset the compiler
@! Bring the Table<> @! Bring the Table<>
@@ -139,6 +140,66 @@ print(f" Ast_Operator_Info op_info_table[{size}];")
*/ */
Ast_Operator_Info op_info_table[20]; Ast_Operator_Info op_info_table[20];
/*END*/ /*END*/
Ast_Type type__void;
Ast_Type type__string;
Ast_Type type__bool;
Ast_Type type__type;
Ast_Type type__f32;
Ast_Type type__f64;
Ast_Type type__s8 ;
Ast_Type type__s16;
Ast_Type type__s32;
Ast_Type type__s64;
Ast_Type type__u8 ;
Ast_Type type__u16;
Ast_Type type__u32;
Ast_Type type__u64;
Ast_Type type__untyped_bool;
Ast_Type type__untyped_int;
Ast_Type type__untyped_string;
Ast_Type type__untyped_float;
Ast_Type type__char;
Ast_Type type__int;
Ast_Type *type_char = &type__char;
Ast_Type *type_int = &type__int;
Ast_Type *type_void = &type__void;
Ast_Type *type_pointer_to_char;
Ast_Type *type_pointer_to_void;
Ast_Type *type_any; // Needs to be inited at runtime
Ast_Type *type_type = &type__type;
Ast_Type *type_string = &type__string;
Ast_Type *type_bool = &type__bool;
Ast_Type *type_f32 = &type__f32;
Ast_Type *type_f64 = &type__f64;
Ast_Type *type_s8 = &type__s8 ;
Ast_Type *type_s16 = &type__s16;
Ast_Type *type_s32 = &type__s32;
Ast_Type *type_s64 = &type__s64;
Ast_Type *type_u8 = &type__u8 ;
Ast_Type *type_u16 = &type__u16;
Ast_Type *type_u32 = &type__u32;
Ast_Type *type_u64 = &type__u64;
Ast_Type *untyped_string = &type__untyped_string;
Ast_Type *untyped_bool = &type__untyped_bool;
Ast_Type *untyped_int = &type__untyped_int;
Ast_Type *untyped_float = &type__untyped_float;
Intern_String intern(String string){ Intern_String intern(String string){
assert(string.len > 0); assert(string.len > 0);
return intern_string(&interns, string); return intern_string(&interns, string);

View File

@@ -3,64 +3,8 @@ thread_local Core_Ctx *pctx;
Allocator *bigint_allocator; Allocator *bigint_allocator;
global S64 bigint_allocation_count; global S64 bigint_allocation_count;
//-----------------------------------------------------------------------------
// Type globals
//-----------------------------------------------------------------------------
const uintptr_t pointer_size = sizeof(uintptr_t); const uintptr_t pointer_size = sizeof(uintptr_t);
const uintptr_t pointer_align = __alignof(uintptr_t); const uintptr_t pointer_align = __alignof(uintptr_t);
global Ast_Type type__void = {TYPE_VOID};
global Ast_Type type__string = {TYPE_STRING, sizeof(String), __alignof(String)};
global Ast_Type type__bool = {TYPE_BOOL, sizeof(bool), __alignof(bool)};
global Ast_Type type__type = {TYPE_TYPE, sizeof(S64), __alignof(S64)};
global Ast_Type type__f32 = {TYPE_F32, sizeof(F32), __alignof(F32)};
global Ast_Type type__f64 = {TYPE_F64, sizeof(F64), __alignof(F64)};
global Ast_Type type__s8 = {TYPE_S8, sizeof(S8), __alignof(S8)};
global Ast_Type type__s16 = {TYPE_S16, sizeof(S16), __alignof(S16)};
global Ast_Type type__s32 = {TYPE_S32, sizeof(S32), __alignof(S32)};
global Ast_Type type__s64 = {TYPE_S64, sizeof(S64), __alignof(S64)};
global Ast_Type type__u8 = {TYPE_U8, sizeof(U8), __alignof(U8), true};
global Ast_Type type__u16 = {TYPE_U16, sizeof(U16), __alignof(U16), true};
global Ast_Type type__u32 = {TYPE_U32, sizeof(U32), __alignof(U32), true};
global Ast_Type type__u64 = {TYPE_U64, sizeof(U64), __alignof(U64), true};
global Ast_Type type__untyped_bool = {TYPE_UNTYPED_BOOL, sizeof(bool), __alignof(bool)};
global Ast_Type type__untyped_int = {TYPE_UNTYPED_INT, sizeof(S64), __alignof(S64)};
global Ast_Type type__untyped_string = {TYPE_UNTYPED_STRING, sizeof(String), __alignof(String)};
global Ast_Type type__untyped_float = {TYPE_UNTYPED_FLOAT, sizeof(double), __alignof(double)};
global Ast_Type type__char = {TYPE_CHAR, sizeof(char), __alignof(char)};
global Ast_Type type__int = {TYPE_INT, sizeof(int), __alignof(int)};
global Ast_Type *type_char = &type__char;
global Ast_Type *type_int = &type__int;
global Ast_Type *type_void = &type__void;
global Ast_Type *type_pointer_to_char; // Needs to be inited at runtime
global Ast_Type *type_pointer_to_void; // Needs to be inited at runtime
global Ast_Type *type_any; // Needs to be inited at runtime
global Ast_Type *type_type = &type__type;
global Ast_Type *type_string = &type__string;
global Ast_Type *type_bool = &type__bool;
global Ast_Type *type_f32 = &type__f32;
global Ast_Type *type_f64 = &type__f64;
global Ast_Type *type_s8 = &type__s8 ;
global Ast_Type *type_s16 = &type__s16;
global Ast_Type *type_s32 = &type__s32;
global Ast_Type *type_s64 = &type__s64;
global Ast_Type *type_u8 = &type__u8 ;
global Ast_Type *type_u16 = &type__u16;
global Ast_Type *type_u32 = &type__u32;
global Ast_Type *type_u64 = &type__u64;
global Ast_Type *untyped_string = &type__untyped_string;
global Ast_Type *untyped_bool = &type__untyped_bool;
global Ast_Type *untyped_int = &type__untyped_int;
global Ast_Type *untyped_float = &type__untyped_float;

View File

@@ -973,7 +973,7 @@ parse_file(Ast_File *file){
set_flag(decl->flags, AST_GLOBAL); set_flag(decl->flags, AST_GLOBAL);
if(decl->kind == AST_STRUCT){ if(decl->kind == AST_STRUCT){
decl->type = type_type; decl->type = pctx->type_type;
decl->type_val = type_incomplete(decl); decl->type_val = type_incomplete(decl);
decl->state = DECL_RESOLVED; decl->state = DECL_RESOLVED;
} }

View File

@@ -23,7 +23,7 @@ operand(Ast_Decl *decl){
static Operand static Operand
operand_type(Ast_Type *type) { operand_type(Ast_Type *type) {
Operand result = {}; Operand result = {};
result.type = type_type; result.type = pctx->type_type;
result.is_const = true; result.is_const = true;
result.is_lvalue = false; result.is_lvalue = false;
result.type_val = type; result.type_val = type;
@@ -33,7 +33,7 @@ operand_type(Ast_Type *type) {
CORE_Static Operand CORE_Static Operand
operand_int(BigInt big_int){ operand_int(BigInt big_int){
Operand result = {}; Operand result = {};
result.type = untyped_int; result.type = pctx->untyped_int;
result.big_int_val = bigint_copy(pctx->perm, &big_int); result.big_int_val = bigint_copy(pctx->perm, &big_int);
result.is_const = true; result.is_const = true;
result.is_lvalue = false; result.is_lvalue = false;
@@ -43,7 +43,7 @@ operand_int(BigInt big_int){
CORE_Static Operand CORE_Static Operand
operand_str(Intern_String intern_val){ operand_str(Intern_String intern_val){
Operand result = {}; Operand result = {};
result.type = type_string; result.type = pctx->type_string;
result.intern_val = intern_val; result.intern_val = intern_val;
result.is_const = true; result.is_const = true;
result.is_lvalue = false; result.is_lvalue = false;
@@ -89,10 +89,10 @@ operand_rvalue(Ast_Type *type){
CORE_Static Ast_Type * CORE_Static Ast_Type *
get_default_type_from_untyped(Ast_Type *type){ get_default_type_from_untyped(Ast_Type *type){
switch(type->kind){ switch(type->kind){
case TYPE_UNTYPED_INT: return type_s64; break; case TYPE_UNTYPED_INT: return pctx->type_s64; break;
case TYPE_UNTYPED_BOOL: return type_bool; break; case TYPE_UNTYPED_BOOL: return pctx->type_bool; break;
case TYPE_UNTYPED_STRING: return type_string; break; case TYPE_UNTYPED_STRING: return pctx->type_string; break;
case TYPE_UNTYPED_FLOAT: return type_f32; break; case TYPE_UNTYPED_FLOAT: return pctx->type_f32; break;
default: invalid_codepath; default: invalid_codepath;
} }
return 0; return 0;
@@ -131,7 +131,7 @@ convert_untyped_to_typed(Token *pos, Value *a, Ast_Type *new_type){
if(is_any(new_type)) if(is_any(new_type))
new_type = get_default_type_from_untyped(a->type); new_type = get_default_type_from_untyped(a->type);
else if(is_int(a->type) && is_int(new_type)) else if(is_int(a->type) && is_int(new_type))
assert(a->type == untyped_int); assert(a->type == pctx->untyped_int);
else if(is_int(a->type) && is_enum(new_type)) else if(is_int(a->type) && is_enum(new_type))
; ;
else if(is_int(a->type) && is_float(new_type)) else if(is_int(a->type) && is_float(new_type))
@@ -327,7 +327,7 @@ eval_unary(Token *pos, Token_Kind op, Operand *operand){
compiler_error(pos, "Unary [%s] cant be applied to value of type %Q", name(op), typestring(type)); compiler_error(pos, "Unary [%s] cant be applied to value of type %Q", name(op), typestring(type));
if(op == TK_Not) if(op == TK_Not)
a->type = untyped_bool; a->type = pctx->untyped_bool;
if(op == TK_Increment || op == TK_Decrement || op == TK_PostIncrement || op == TK_PostDecrement) if(op == TK_Increment || op == TK_Decrement || op == TK_PostIncrement || op == TK_PostDecrement)
if(!operand->is_lvalue) if(!operand->is_lvalue)
@@ -681,7 +681,7 @@ resolve_typespec(Ast_Expr *ast, Resolve_Flag flags){
Operand resolved = resolve_expr(ast, flags | RESOLVE_TYPESPEC, 0, 0); Operand resolved = resolve_expr(ast, flags | RESOLVE_TYPESPEC, 0, 0);
if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE)) if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE))
type_complete(resolved.type_val); type_complete(resolved.type_val);
if(resolved.type != type_type) if(resolved.type != pctx->type_type)
compiler_error(ast->pos, "Expected [Type] got instead %Q", typestring(resolved.type)); compiler_error(ast->pos, "Expected [Type] got instead %Q", typestring(resolved.type));
return resolved.type_val; return resolved.type_val;
} }
@@ -780,7 +780,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
CASE(RUNTIME_ASSERT, Builtin){ CASE(RUNTIME_ASSERT, Builtin){
resolve_and_require_bool("Assert condition is not boolean", node->expr, AST_CAN_BE_NULL); resolve_and_require_bool("Assert condition is not boolean", node->expr, AST_CAN_BE_NULL);
try_propagating_resolved_type_to_untyped_literals(node->expr, type_bool); try_propagating_resolved_type_to_untyped_literals(node->expr, pctx->type_bool);
BREAK(); BREAK();
} }
@@ -818,7 +818,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
compiler_error(node->pos, "Invalid type of for loop condition %Q, required [Bool]", typestring(op.type)); compiler_error(node->pos, "Invalid type of for loop condition %Q, required [Bool]", typestring(op.type));
} }
else{ else{
try_propagating_resolved_type_to_untyped_literals(node->cond, type_bool); try_propagating_resolved_type_to_untyped_literals(node->cond, pctx->type_bool);
} }
} }
@@ -832,7 +832,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
For(node->ifs){ For(node->ifs){
resolve_stmt(it->init, ret); resolve_stmt(it->init, ret);
resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL); resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL);
try_propagating_resolved_type_to_untyped_literals(it->expr, type_bool); try_propagating_resolved_type_to_untyped_literals(it->expr, pctx->type_bool);
For_Named(it->scope->stmts, jt) For_Named(it->scope->stmts, jt)
resolve_stmt(jt, ret); resolve_stmt(jt, ret);
} }
@@ -1042,7 +1042,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){
it->resolved_name = m.name; it->resolved_name = m.name;
it->resolved_index = type->agg.members.get_index(&m); it->resolved_index = type->agg.members.get_index(&m);
// @copy_paste // @copy_paste
if(m.type == type_type) if(m.type == pctx->type_type)
item_type = m.type_val; item_type = m.type_val;
else item_type = m.type; else item_type = m.type;
@@ -1067,7 +1067,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){
m->visited = true; m->visited = true;
// @copy_paste // @copy_paste
if(m->type == type_type && m->type_val){ if(m->type == pctx->type_type && m->type_val){
item_type = m->type_val; item_type = m->type_val;
} }
else item_type = m->type; else item_type = m->type;
@@ -1147,7 +1147,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
CASE(ARRAY, Array){ CASE(ARRAY, Array){
Operand type = resolve_expr(node->base, AST_CANT_BE_NULL, 0, 0); Operand type = resolve_expr(node->base, AST_CANT_BE_NULL, 0, 0);
Operand expr = require_const_int(node->expr, AST_CAN_BE_NULL); Operand expr = require_const_int(node->expr, AST_CAN_BE_NULL);
if(type.type != type_type) compiler_error(node->pos, "Prefix array operator is only allowed on types"); if(type.type != pctx->type_type) compiler_error(node->pos, "Prefix array operator is only allowed on types");
type_complete(type.type_val); type_complete(type.type_val);
if(node->expr){ if(node->expr){
@@ -1184,8 +1184,8 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
node->index_original_type = left.type; node->index_original_type = left.type;
node->resolved_type = left.type->arr.base; node->resolved_type = left.type->arr.base;
if(left.type == type_pointer_to_char){ if(left.type == pctx->type_pointer_to_char){
node->resolved_type = type_char; node->resolved_type = pctx->type_char;
} }
else if(is_string(left.type)) { else if(is_string(left.type)) {
@@ -1199,16 +1199,16 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
left.type = compound_and_const_string_context; left.type = compound_and_const_string_context;
} }
else{ else{
left.type = type_string; left.type = pctx->type_string;
} }
} }
node->resolved_type = type_u8; node->resolved_type = pctx->type_u8;
} }
// @todo: type_architecture? // @todo: type_architecture?
// we only try to convert the index cause array can't be const // we only try to convert the index cause array can't be const
// right now // right now
try_propagating_resolved_type_to_untyped_literals(node->index, type_s64); try_propagating_resolved_type_to_untyped_literals(node->index, pctx->type_s64);
try_propagating_resolved_type_to_untyped_literals(node->expr, left.type); try_propagating_resolved_type_to_untyped_literals(node->expr, left.type);
if(!left.type) if(!left.type)
@@ -1217,7 +1217,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
compiler_error(node->expr->pos, "Internal compiler error: type of array is marked as untyped somehow"); compiler_error(node->expr->pos, "Internal compiler error: type of array is marked as untyped somehow");
if(is_string(left.type)){ if(is_string(left.type)){
return operand_lvalue_set_flag_on_node(type_u8, node); return operand_lvalue_set_flag_on_node(pctx->type_u8, node);
} }
if(!is_array(left.type) && !is_pointer(left.type) && !is_slice(left.type)){ if(!is_array(left.type) && !is_pointer(left.type) && !is_slice(left.type)){
@@ -1259,7 +1259,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
else { else {
// Make sure it's a type but also not a type id // Make sure it's a type but also not a type id
Ast_Type *type = left.type; Ast_Type *type = left.type;
if(type == type_type && left.type_val){ if(type == pctx->type_type && left.type_val){
if(is_enum(left.type_val) || is_struct(left.type_val)){ if(is_enum(left.type_val) || is_struct(left.type_val)){
type = left.type_val; type = left.type_val;
} }
@@ -1413,7 +1413,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
Ast_Type *type = op.type; Ast_Type *type = op.type;
if(type){ if(type){
if(type != type_type) if(type != pctx->type_type)
compiler_error(node->pos, "Expected type [Type] got instead %Q", typestring(type)); compiler_error(node->pos, "Expected type [Type] got instead %Q", typestring(type));
type = op.type_val; type = op.type_val;
} }
@@ -1446,7 +1446,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope); Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope);
node->kind = AST_SIZE_OF; node->kind = AST_SIZE_OF;
Ast_Type *type = name.type == type_type ? name.type_val : name.type; Ast_Type *type = name.type == pctx->type_type ? name.type_val : name.type;
type_complete(type); type_complete(type);
if(type->size == 0){ if(type->size == 0){
@@ -1463,7 +1463,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope); Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope);
node->kind = AST_TYPE_OF; node->kind = AST_TYPE_OF;
Ast_Type *type = name.type == type_type ? name.type_val : name.type; Ast_Type *type = name.type == pctx->type_type ? name.type_val : name.type;
Operand result = operand_type(type); Operand result = operand_type(type);
rewrite_into_const(node, Ast_Builtin, result.value); rewrite_into_const(node, Ast_Builtin, result.value);
@@ -1475,7 +1475,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope); Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope);
node->kind = AST_ALIGN_OF; node->kind = AST_ALIGN_OF;
Ast_Type *type = name.type == type_type ? name.type_val : name.type; Ast_Type *type = name.type == pctx->type_type ? name.type_val : name.type;
type_complete(type); type_complete(type);
if(type->size == 0){ if(type->size == 0){
compiler_error(node->pos, "Internal compiler error: calling SizeOf but the resulting size of type is obviously invalid suggesting that type was not completed properly"); compiler_error(node->pos, "Internal compiler error: calling SizeOf but the resulting size of type is obviously invalid suggesting that type was not completed properly");
@@ -1491,7 +1491,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope); Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope);
node->kind = AST_LENGTH_OF; node->kind = AST_LENGTH_OF;
node->resolved_type = type_s64; node->resolved_type = pctx->type_s64;
if(is_array(name.type)){ if(is_array(name.type)){
Value value = value_int(name.type->arr.size); Value value = value_int(name.type->arr.size);
rewrite_into_const(node, Ast_Builtin, value); rewrite_into_const(node, Ast_Builtin, value);
@@ -1503,7 +1503,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
return operand_const_rvalue(value); return operand_const_rvalue(value);
} }
else if(is_array(name.type) || is_slice(name.type) || is_string(name.type)){ else if(is_array(name.type) || is_slice(name.type) || is_string(name.type)){
return operand_rvalue(type_s64); return operand_rvalue(pctx->type_s64);
} }
else compiler_error(node->pos, "Can't get length of type %Q", typestring(name.type)); else compiler_error(node->pos, "Can't get length of type %Q", typestring(name.type));
} }
@@ -1666,7 +1666,7 @@ resolve_decl(Ast_Decl *ast){
} }
node->value = op.value; node->value = op.value;
if(op.value.type == type_type){ if(op.value.type == pctx->type_type){
node->kind = AST_TYPE; node->kind = AST_TYPE;
if(is_flag_set(node->flags, AST_STRICT)){ if(is_flag_set(node->flags, AST_STRICT)){
node->type_val = type_copy(pctx->perm, node->type_val); node->type_val = type_copy(pctx->perm, node->type_val);
@@ -1705,7 +1705,7 @@ resolve_decl(Ast_Decl *ast){
CASE(ENUM, Decl){ CASE(ENUM, Decl){
Ast_Type *type_of_enum = resolve_typespec(node->typespec, AST_CAN_BE_NULL); Ast_Type *type_of_enum = resolve_typespec(node->typespec, AST_CAN_BE_NULL);
node->type = type_type; node->type = pctx->type_type;
node->type_val = type_enum(node, type_of_enum); node->type_val = type_enum(node, type_of_enum);
S64 value = 1; S64 value = 1;

View File

@@ -27,7 +27,7 @@ get_name_of_type(Ast_Type *type){
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Type constructors and utillities // Type constructors and utillities
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
force_inline B32 is_any(Ast_Type *a){return a == type_any;} force_inline B32 is_any(Ast_Type *a){return a == pctx->type_any;}
force_inline B32 is_struct(Ast_Type *a){return a->kind == TYPE_STRUCT;} force_inline B32 is_struct(Ast_Type *a){return a->kind == TYPE_STRUCT;}
force_inline B32 is_lambda(Ast_Type *a){return a->kind == TYPE_LAMBDA;} force_inline B32 is_lambda(Ast_Type *a){return a->kind == TYPE_LAMBDA;}
force_inline B32 is_array(Ast_Type *a){return a->kind == TYPE_ARRAY;} force_inline B32 is_array(Ast_Type *a){return a->kind == TYPE_ARRAY;}
@@ -36,8 +36,8 @@ force_inline B32 is_tuple(Ast_Type *a){return a->kind == TYPE_TUPLE;}
force_inline B32 is_enum(Ast_Type *a){return a->kind == TYPE_ENUM;} force_inline B32 is_enum(Ast_Type *a){return a->kind == TYPE_ENUM;}
force_inline B32 is_pointer(Ast_Type *a){return a->kind == TYPE_POINTER;} force_inline B32 is_pointer(Ast_Type *a){return a->kind == TYPE_POINTER;}
force_inline B32 is_void(Ast_Type *a){return a->kind == TYPE_VOID;} force_inline B32 is_void(Ast_Type *a){return a->kind == TYPE_VOID;}
force_inline B32 is_void_pointer(Ast_Type *a){return a == type_pointer_to_void;} force_inline B32 is_void_pointer(Ast_Type *a){return a == pctx->type_pointer_to_void;}
force_inline B32 is_string(Ast_Type *a){return a->kind == TYPE_STRING || a->kind == TYPE_UNTYPED_STRING || a == type_pointer_to_char;} force_inline B32 is_string(Ast_Type *a){return a->kind == TYPE_STRING || a->kind == TYPE_UNTYPED_STRING || a == pctx->type_pointer_to_char;}
force_inline B32 is_untyped_int(Ast_Type *a){return a->kind == TYPE_UNTYPED_INT;} force_inline B32 is_untyped_int(Ast_Type *a){return a->kind == TYPE_UNTYPED_INT;}
force_inline B32 is_typed_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8);} force_inline B32 is_typed_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8);}
force_inline B32 is_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT;} force_inline B32 is_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT;}
@@ -113,7 +113,7 @@ type_slice(Ast_Type *base, Ast *ast){
CORE_Static Ast_Type * CORE_Static Ast_Type *
type_try_tupling(Array<Ast_Type *> types, Ast *ast){ type_try_tupling(Array<Ast_Type *> types, Ast *ast){
if(types.len == 0) return type_void; if(types.len == 0) return pctx->type_void;
if(types.len == 1) return types[0]; if(types.len == 1) return types[0];
U64 hash = 13; U64 hash = 13;
@@ -204,7 +204,7 @@ type_lambda(Ast *ast, Array<Ast_Type *> return_vals, Array<Ast_Type *> args){
CORE_Static Ast_Type * CORE_Static Ast_Type *
type_enum(Ast_Decl *ast, Ast_Type *type){ type_enum(Ast_Decl *ast, Ast_Type *type){
if(!type){ if(!type){
type = type_s64; type = pctx->type_s64;
} }
Ast_Type *result = type_new(pctx->perm, TYPE_ENUM, type->size, type->align); Ast_Type *result = type_new(pctx->perm, TYPE_ENUM, type->size, type->align);
@@ -273,8 +273,7 @@ type_complete(Ast_Type *type){
CORE_Static void CORE_Static void
init_type(){ init_type(){
type_pointer_to_char = type_pointer(type_char);
type_pointer_to_void = type_pointer(type_void);
} }
CORE_Static void CORE_Static void