Rewriting constant casts into literals of said type

This commit is contained in:
Krzosa Karol
2022-06-22 14:01:15 +02:00
parent b0872e2303
commit f66b155e40
3 changed files with 45 additions and 17 deletions

View File

@@ -3,7 +3,7 @@ function Ast_Decl *parse_decl(B32 is_global);
function void function void
print_token_context(Token *token){ print_token_context(Token *token){
if(!token) return; if(!token) return;
printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1); printf(" :: tdi:%u %s:%d\n", token->di, token->file.str, (S32)token->line + 1);
// @Note(Krzosa): Print error line // @Note(Krzosa): Print error line
{ {
int i = 0; int i = 0;

View File

@@ -89,17 +89,15 @@ print :: (a: Any)
// print(a[i]) // print(a[i])
app_is_running := true little_untyped_test :: ()
window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT if true == false;; pass
if msg == WM_DESTROY if true;; pass
PostQuitMessage(0) cast_value1 := 32->S64
app_is_running = false cast_value2 := true->Bool
return 0
else;; return DefWindowProcW(hwnd, msg, wparam, lparam)
WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int switch 4
if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR 4;; OutputDebugStringA("4")
good_scheduling = true 3;; OutputDebugStringA("3")
some_type: Type = Vec2 some_type: Type = Vec2
char_info := get_type_info(char) char_info := get_type_info(char)
@@ -113,6 +111,19 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
#assert(int != char) #assert(int != char)
#assert(*char == *char) #assert(*char == *char)
app_is_running := true
window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
if msg == WM_DESTROY
PostQuitMessage(0)
app_is_running = false
return 0
else;; return DefWindowProcW(hwnd, msg, wparam, lparam)
WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int
if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR
good_scheduling = true
arena: Arena arena: Arena
window_name := string_to_string16(&arena, "Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ") window_name := string_to_string16(&arena, "Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ")
w := WNDCLASSW{ w := WNDCLASSW{

View File

@@ -473,6 +473,7 @@ insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){
// //
function void function void
try_propagating_resolved_type_to_untyped_literals(Ast *ast, Ast_Type *type, Ast_Type *additional_not_bool_type = 0){ try_propagating_resolved_type_to_untyped_literals(Ast *ast, Ast_Type *type, Ast_Type *additional_not_bool_type = 0){
if(!type) compiler_error(ast->pos, "Internal compiler error: Type passed to try_propagating_resolved_type_to_untyped_literals is null");
if(!ast) return; if(!ast) return;
if(is_untyped(type)) return; if(is_untyped(type)) return;
@@ -678,10 +679,10 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
BREAK(); BREAK();
} }
// @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL
CASE(IF, If){ CASE(IF, If){
For(node->ifs){ For(node->ifs){
resolve_stmt(it->init, ret); resolve_stmt(it->init, ret);
// @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL
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, type_bool);
For_Named(it->scope->stmts, jt) For_Named(it->scope->stmts, jt)
@@ -691,16 +692,26 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
} }
CASE(SWITCH, Switch){ CASE(SWITCH, Switch){
// @todo better typechecking Operand base = resolve_expr(node->value, AST_CANT_BE_NULL);
resolve_expr(node->value, AST_CANT_BE_NULL); if(!is_int(base.type) && !is_enum(base.type))
compiler_error(node->pos, "You can only switch on integer values(enums included), the type of expression in switch statement is instead %Q", typestring(base.type));
try_converting_untyped_to_default_type(&base.value);
try_propagating_resolved_type_to_untyped_literals(node->value, base.type);
For(node->cases){ For(node->cases){
For_Named(it->labels, label){ For_Named(it->labels, label){
Operand op = resolve_expr(label, AST_CANT_BE_NULL); Operand op = resolve_expr(label, AST_CANT_BE_NULL);
if(!op.is_const) compiler_error(label->pos, "Switch label required to be constant"); if(!op.is_const) compiler_error(label->pos, "Switch label required to be constant");
make_sure_value_is_compatible_with_type(label->pos, &op, base.type, TYPE_AND_EXPR_REQUIRED);
try_propagating_resolved_type_to_untyped_literals(label, base.type);
} }
For_Named(it->scope->stmts, stmt) resolve_stmt(stmt, ret);
For_Named(it->scope->stmts, stmt)
resolve_stmt(stmt, ret);
} }
if(node->default_scope) For_Named(node->default_scope->stmts, stmt) resolve_stmt(stmt, ret); if(node->default_scope)
For_Named(node->default_scope->stmts, stmt)
resolve_stmt(stmt, ret);
BREAK(); BREAK();
} }
@@ -758,6 +769,7 @@ resolve_lambda_type(Ast_Lambda *lambda){
Ast_Type *type = resolve_typespec(it->typespec, AST_CANT_BE_NULL); Ast_Type *type = resolve_typespec(it->typespec, AST_CANT_BE_NULL);
Operand default_value = resolve_expr(it->expr, AST_CAN_BE_NULL, type); Operand default_value = resolve_expr(it->expr, AST_CAN_BE_NULL, type);
make_sure_value_is_compatible_with_type(it->pos, &default_value, type, EXPR_CAN_BE_NULL); make_sure_value_is_compatible_with_type(it->pos, &default_value, type, EXPR_CAN_BE_NULL);
it->type = type; it->type = type;
try_propagating_resolved_type_to_untyped_literals(it->expr, it->type); try_propagating_resolved_type_to_untyped_literals(it->expr, it->type);
} }
@@ -833,9 +845,14 @@ resolve_cast(Ast_Binary *node){
} }
assert(original_type != type ? expr.type == type : 1); assert(original_type != type ? expr.type == type : 1);
if(expr.is_const) check_value_bounds(node->pos, &expr.value); if(expr.is_const) {
check_value_bounds(node->pos, &expr.value);
rewrite_into_const(node, Ast_Binary, expr.value);
}
node->resolved_type = expr.type; node->resolved_type = expr.type;
if(!expr.is_const)
try_propagating_resolved_type_to_untyped_literals(node->left, node->resolved_type);
return expr; return expr;
} }