Better support for Type types, squashing bugs due to pointer confusion etc.

This commit is contained in:
Krzosa Karol
2022-06-19 10:18:57 +02:00
parent 94b820a071
commit ade2638255
6 changed files with 56 additions and 17 deletions

View File

@@ -113,7 +113,8 @@ struct Ast_Var_Unpack: Ast_Expr{
struct Ast_Unary: Ast_Expr{ struct Ast_Unary: Ast_Expr{
Token_Kind op; Token_Kind op;
Ast_Expr *expr; Ast_Expr *expr;
U64 padding[2]; // For folding constants into atoms Ast_Type *resolved_type_val;
U64 padding[1]; // For folding constants into atoms
}; };
struct Ast_Index: Ast_Expr{ struct Ast_Index: Ast_Expr{

View File

@@ -284,6 +284,11 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
BREAK(); BREAK();
} }
CASE(ARRAY, Array){
gen("%d", node->resolved_type->type_id);
BREAK();
}
CASE(INDEX, Index){ CASE(INDEX, Index){
gen("("); gen("(");
gen_expr(node->expr); gen_expr(node->expr);
@@ -332,7 +337,19 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
CASE(UNARY, Unary){ CASE(UNARY, Unary){
gen("("); gen("(");
if(node->op != TK_PostIncrement && node->op != TK_PostDecrement) gen("%s", name(node->op)); if(node->op != TK_PostIncrement && node->op != TK_PostDecrement){
if(node->op == TK_Pointer){
// Normal types are usually generated with gen_simple_decl
// if they are part of the expression they are a value
// which means we need to shortcircuit here with proper type_id
Ast_Type *base = get_type_base(node->resolved_type);
if(base == type_type){
gen("%d)", node->resolved_type_val->type_id);
return true;
}
}
gen("%s", name(node->op));
}
gen_expr(node->expr); gen_expr(node->expr);
if(node->op == TK_PostIncrement || node->op == TK_PostDecrement) gen("%s", name(node->op)); if(node->op == TK_PostIncrement || node->op == TK_PostDecrement) gen("%s", name(node->op));
gen(")"); gen(")");

View File

@@ -167,16 +167,15 @@ parse_expr_call(Ast_Expr *left, Token_Kind close_kind){
Token *token = token_get(); Token *token = token_get();
Ast_Atom *name = 0; Ast_Atom *name = 0;
Ast_Expr *index = 0; Ast_Expr *index = 0;
if(token_match(TK_OpenBracket)){
index = parse_expr(0);
token_expect(TK_CloseBracket);
token_expect(TK_Assign);
}
Ast_Expr *item = parse_expr(); Ast_Expr *item = parse_expr();
if(token_match(TK_Assign)){ if(token_match(TK_Assign)){
assert(is_flag_set(item->flags, AST_ATOM)); assert(is_flag_set(item->flags, AST_ATOM));
if(item->kind != AST_IDENT){
index = item;
} else{
name = (Ast_Atom *)item; name = (Ast_Atom *)item;
}
item = parse_expr(); item = parse_expr();
} }

View File

@@ -42,11 +42,22 @@ create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap
return result return result
print :: (type: Type) print :: (type: Type)
switch type type_info := get_type_info(type)
int, S64, S32, S16, S8 if !type_info
OutputDebugStringA("int") return
default
OutputDebugStringA("unknown_type") switch type_info.kind
S64, S32, S16, S8, int
OutputDebugStringA("Integer")
U64, U32, U16, U8
OutputDebugStringA("Unsigned")
Type_Info_Kind.POINTER
OutputDebugStringA("*")
print(type_info.base_type)
Type_Info_Kind.SLICE
OutputDebugStringA("[]")
print(type_info.base_type)
default;; OutputDebugStringA("Unknown")
app_is_running := true app_is_running := true
window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
@@ -67,6 +78,7 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR
good_scheduling = true good_scheduling = true
print([]**S64)
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 == int)

View File

@@ -829,7 +829,8 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){
} }
Ast_Type *type = decl->type; Ast_Type *type = decl->type;
if(type == type_type && (is_enum(decl->type_val) || is_struct(decl->type_val))) type = decl->type_val; if(type == type_type && decl->type_val && (is_enum(decl->type_val) || is_struct(decl->type_val)))
type = decl->type_val;
if(current) current->resolved_type = type; if(current) current->resolved_type = type;
if(is_pointer(type)) type = type->base; if(is_pointer(type)) type = type->base;
@@ -973,8 +974,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
return operand_lvalue(node->resolved_type); return operand_lvalue(node->resolved_type);
} }
else if(value.type->kind == TYPE_TYPE){ else if(value.type->kind == TYPE_TYPE){
node->resolved_type = type_pointer(value.type_val); node->resolved_type = type_type;
return operand_type(node->resolved_type); node->resolved_type_val = type_pointer(value.type_val);
return operand_type(node->resolved_type_val);
} }
else{ compiler_error(node->pos, "Dereferencing expression %Q that is not a [Pointer] or [Type]", typestring(value.type)); return {}; } else{ compiler_error(node->pos, "Dereferencing expression %Q that is not a [Pointer] or [Type]", typestring(value.type)); return {}; }
} }
@@ -1139,6 +1141,14 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
invalid_return; invalid_return;
} }
function Ast_Type *
get_type_base(Ast_Type *type){
switch(type->kind){
case TYPE_POINTER: case TYPE_SLICE: case TYPE_ARRAY: return get_type_base(type->base);
default: return type;
}
}
function void function void
resolve_decl(Ast_Decl *ast){ resolve_decl(Ast_Decl *ast){
if(ast->state == DECL_RESOLVED) if(ast->state == DECL_RESOLVED)