Working on type operations and type infos

This commit is contained in:
Krzosa Karol
2022-06-19 00:05:22 +02:00
parent 9d2ce3560b
commit fc0d4345ee
4 changed files with 37 additions and 22 deletions

View File

@@ -837,11 +837,11 @@ typedef struct String{
gen_ast(it); gen_ast(it);
} }
genln("S32 type_infos_len = %d;", pctx->all_types.len); genln("S64 type_infos_len = %d;", pctx->all_types.len);
genln("Type_Info *type_infos = (Type_Info[]){"); genln("Type_Info *type_infos = (Type_Info[]){");
global_indent++; global_indent++;
For(pctx->all_types){ For(pctx->all_types){
genln("{/*%Q*/.kind = %d, .size = %d, .align = %d, .is_unsigned = %s, .type_id = %d, ", typestring(it), genln("{/*%Q*/.kind = %d, .size = %d, .align = %d, .is_unsigned = %s, .type = %d, ", typestring(it),
(S32)it->kind, (S32)it->size, (S32)it->align, it->is_unsigned ? "true" : "false", it->type_id); (S32)it->kind, (S32)it->size, (S32)it->align, it->is_unsigned ? "true" : "false", it->type_id);
switch(it->kind){ switch(it->kind){
case TYPE_POINTER: case TYPE_POINTER:
@@ -857,7 +857,7 @@ typedef struct String{
gen(".lambda_argument_count = %d, ", it->func.args.len); gen(".lambda_argument_count = %d, ", it->func.args.len);
gen(".lambda_arguments = (Type_Info[%d]){", it->func.args.len); gen(".lambda_arguments = (Type_Info[%d]){", it->func.args.len);
For_Named(it->func.args, arg){ For_Named(it->func.args, arg){
gen("{.type_id = %d}, ", arg->type_id); gen("{.type = %d}, ", arg->type_id);
} }
gen("}"); gen("}");
} break; } break;
@@ -865,7 +865,7 @@ typedef struct String{
gen(".struct_member_count = %d, ", it->agg.members.len); gen(".struct_member_count = %d, ", it->agg.members.len);
gen(".struct_members = (Type_Info_Struct_Member[]){"); gen(".struct_members = (Type_Info_Struct_Member[]){");
For_Named(it->agg.members, m){ For_Named(it->agg.members, m){
gen("{.name = LIT(\"%Q\"), .type_id = %d, .offset = %d}, ", m.name, m.type->type_id, m.offset); gen("{.name = LIT(\"%Q\"), .type = %d, .offset = %d}, ", m.name, m.type->type_id, m.offset);
} }
gen("}"); gen("}");

View File

@@ -14,7 +14,6 @@ Any :: struct
*/ */
Type_ID :: S32
Type_Info_Kind :: enum Type_Info_Kind :: enum
NONE NONE
S64 :: 7 // FIRST_NUMERIC S64 :: 7 // FIRST_NUMERIC
@@ -45,29 +44,29 @@ Type_Info_Kind :: enum
Type_Info_Struct_Member :: struct Type_Info_Struct_Member :: struct
name: String name: String
type_id: Type_ID type: Type
offset: S32 offset: S64
Type_Info :: struct Type_Info :: struct
kind: Type_Info_Kind kind: Type_Info_Kind
size: S32 size: S64
align: S32 align: S64
is_unsigned: Bool is_unsigned: Bool
type_id: Type_ID type: Type
base_type: Type_ID base_type: Type
array_size: S32 array_size: S64
struct_member_count: S32 struct_member_count: S64
struct_members: *Type_Info_Struct_Member struct_members: *Type_Info_Struct_Member
lambda_argument_count: S32 lambda_argument_count: S64
lambda_arguments: *Type_Info lambda_arguments: *Type_Info
lambda_return: Type_ID lambda_return: Type
type_infos_len: S32 #foreign type_infos_len: S64 #foreign
type_infos : *Type_Info #foreign type_infos : *Type_Info #foreign
get_type_info :: (type: Type): *Type_Info get_type_info :: (type: Type): *Type_Info
id := type->S32 id := type->S64
if id >= type_infos_len if id >= type_infos_len
return 0 return 0
return type_infos + id return type_infos + id

View File

@@ -62,6 +62,9 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
char_info := get_type_info(char) char_info := get_type_info(char)
assert(char_info.kind == Type_Info_Kind.CHAR) assert(char_info.kind == Type_Info_Kind.CHAR)
#assert(int == int)
#assert(int != char)
#assert(*char == *char)
arena: Arena arena: Arena
window_name := string_to_string16(&arena, "Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ") window_name := string_to_string16(&arena, "Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ")

View File

@@ -138,6 +138,8 @@ function Value
compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){ compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
if(is_enum(a.type) && (a.type == b.type)) if(is_enum(a.type) && (a.type == b.type))
goto skip_eval; goto skip_eval;
if(a.type->kind == TYPE_TYPE && b.type->kind == TYPE_TYPE)
goto skip_eval;
if(!(is_numeric(a.type) && is_numeric(b.type))) if(!(is_numeric(a.type) && is_numeric(b.type)))
compiler_error(pos, "Constant application of binary %s on values of type %Q and %Q is not allowed", name(op), typestring(a.type), typestring(b.type)); compiler_error(pos, "Constant application of binary %s on values of type %Q and %Q is not allowed", name(op), typestring(a.type), typestring(b.type));
@@ -146,6 +148,13 @@ compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
skip_eval: B32 result = 0; skip_eval: B32 result = 0;
if(is_const){ if(is_const){
switch(a.type->kind){ switch(a.type->kind){
case TYPE_TYPE:{
switch(op){
case TK_Equals: result = a.type_val->type_id == b.type_val->type_id; break;
case TK_NotEquals: result = a.type_val->type_id != b.type_val->type_id; break;
default: goto failure;
}
}break;
CASE_INT:{ CASE_INT:{
CmpRes cmp = bigint_cmp(&a.big_int_val, &b.big_int_val); CmpRes cmp = bigint_cmp(&a.big_int_val, &b.big_int_val);
switch(op){ switch(op){
@@ -155,14 +164,14 @@ compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
case TK_LesserThen: result = cmp == CMP_LT; break; case TK_LesserThen: result = cmp == CMP_LT; break;
case TK_Equals: result = cmp == CMP_EQ; break; case TK_Equals: result = cmp == CMP_EQ; break;
case TK_NotEquals: result = cmp != CMP_EQ; break; case TK_NotEquals: result = cmp != CMP_EQ; break;
invalid_default_case; default: goto failure;
} }
}break; }break;
CASE_BOOL:{ CASE_BOOL:{
switch(op){ switch(op){
case TK_Equals: result = a.bool_val == b.bool_val; break; case TK_Equals: result = a.bool_val == b.bool_val; break;
case TK_NotEquals: result = a.bool_val != b.bool_val; break; case TK_NotEquals: result = a.bool_val != b.bool_val; break;
invalid_default_case; default: goto failure;
} }
}break; }break;
CASE_FLOAT:{ CASE_FLOAT:{
@@ -173,13 +182,17 @@ compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
case TK_LesserThen: result = a.f64_val < b.f64_val; break; case TK_LesserThen: result = a.f64_val < b.f64_val; break;
case TK_Equals: result = a.f64_val == b.f64_val; break; case TK_Equals: result = a.f64_val == b.f64_val; break;
case TK_NotEquals: result = a.f64_val != b.f64_val; break; case TK_NotEquals: result = a.f64_val != b.f64_val; break;
invalid_default_case; default: goto failure;
} }
}break; }break;
CASE_STRING:{ CASE_STRING:{
invalid_codepath; goto failure;
}break; }break;
invalid_default_case;
default: {
failure: compiler_error(pos, "Constant application of binary %s on values of type %Q and %Q is not allowed", name(op), typestring(a.type), typestring(b.type));
}
} }
} }