Add resolved_type to Ast_Expr and remove from inheriting

This commit is contained in:
Krzosa Karol
2022-06-14 12:49:45 +02:00
parent 699ca4b18a
commit 17e342c4da
3 changed files with 36 additions and 33 deletions

17
ast.cpp
View File

@@ -66,6 +66,11 @@ struct Ast{
struct Ast_Type; struct Ast_Type;
struct Ast_Expr:Ast{ struct Ast_Expr:Ast{
union{
Ast_Type *resolved_type;
Ast_Type *index_original_type;
Ast_Type *cast_after_type;
};
}; };
#define VALUE_FIELDS \ #define VALUE_FIELDS \
@@ -79,7 +84,6 @@ union{ \
}; };
#define INLINE_VALUE_FIELDS union{Value value; struct{VALUE_FIELDS};} #define INLINE_VALUE_FIELDS union{Value value; struct{VALUE_FIELDS};}
struct Value{VALUE_FIELDS}; struct Value{VALUE_FIELDS};
// BigInt big_int_val;
struct Ast_Atom: Ast_Expr{ struct Ast_Atom: Ast_Expr{
Ast_Decl *resolved_decl; Ast_Decl *resolved_decl;
@@ -98,20 +102,17 @@ struct Ast_Call: Ast_Expr{
Ast_Expr *typespec; Ast_Expr *typespec;
}; };
Array<Ast_Call_Item *> exprs; Array<Ast_Call_Item *> exprs;
Ast_Type *type;
}; };
struct Ast_Unary: Ast_Expr{ struct Ast_Unary: Ast_Expr{
Token_Kind op; Token_Kind op;
Ast_Expr *expr; Ast_Expr *expr;
Ast_Type *type;
U64 padding[2]; // For folding constants into atoms U64 padding[2]; // For folding constants into atoms
}; };
struct Ast_Index: Ast_Expr{ struct Ast_Index: Ast_Expr{
Ast_Expr *expr; Ast_Expr *expr;
Ast_Expr *index; Ast_Expr *index;
Ast_Type *original_type;
}; };
struct Ast_Binary: Ast_Expr{ struct Ast_Binary: Ast_Expr{
@@ -119,10 +120,14 @@ struct Ast_Binary: Ast_Expr{
Ast_Expr *left; Ast_Expr *left;
Ast_Expr *right; Ast_Expr *right;
Ast_Type *type; // Ast_Type *type; // For casts after_type
Ast_Type *before_type; Ast_Type *before_type;
}; };
struct Ast_Len: Ast_Expr{
Ast_Expr *expr;
};
// Problem: We are parsing out of order, in the middle of parsing a function // Problem: We are parsing out of order, in the middle of parsing a function
// we can jump down a different function, we cant therfore use global map. // we can jump down a different function, we cant therfore use global map.
// Each scope needs to have it's checked locals list. To lookup syms we need to // Each scope needs to have it's checked locals list. To lookup syms we need to
@@ -156,13 +161,11 @@ struct Ast_Lambda : Ast_Expr {
Array<Ast_Decl *> args; Array<Ast_Decl *> args;
Ast_Expr *ret; Ast_Expr *ret;
Ast_Scope *scope; Ast_Scope *scope;
Ast_Type *type;
}; };
struct Ast_Array: Ast_Expr{ struct Ast_Array: Ast_Expr{
Ast_Expr *base; Ast_Expr *base;
Ast_Expr *expr; Ast_Expr *expr;
Ast_Type *type;
}; };
/* /*

View File

@@ -207,7 +207,7 @@ gen_lambda(Intern_String name, Ast_Lambda *lambda, B32 generate_block = true){
if(name == pctx->intern("main"_s)){ if(name == pctx->intern("main"_s)){
is_foreign = true; is_foreign = true;
} }
gen_simple_decl(lambda->type->func.ret, name, lambda->parent_scope, !is_foreign); gen_simple_decl(lambda->resolved_type->func.ret, name, lambda->parent_scope, !is_foreign);
gen("("); gen("(");
For(lambda->args){ For(lambda->args){
gen_var(it, DONT_EMIT_VALUE, true); gen_var(it, DONT_EMIT_VALUE, true);
@@ -271,7 +271,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
CASE(BINARY, Binary){ CASE(BINARY, Binary){
if(node->op == TK_Dot){ if(node->op == TK_Dot){
if(gen_expr(node->left)){ if(gen_expr(node->left)){
if(node->type && node->type->kind == TYPE_POINTER) gen("->"); if(node->resolved_type && node->resolved_type->kind == TYPE_POINTER) gen("->");
else gen("."); else gen(".");
} }
gen_expr(node->right); gen_expr(node->right);
@@ -280,7 +280,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
else if(node->op == TK_Arrow){ else if(node->op == TK_Arrow){
gen("("); gen("(");
gen("("); gen("(");
gen_simple_decl(node->type); gen_simple_decl(node->resolved_type);
gen(")"); gen(")");
gen_expr(node->left); gen_expr(node->left);
gen(")"); gen(")");
@@ -335,7 +335,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
CASE(COMPOUND, Call){ CASE(COMPOUND, Call){
gen("("); gen("(");
gen_simple_decl(node->type); gen_simple_decl(node->resolved_type);
gen(")"); gen(")");
gen("{"); gen("{");
For(node->exprs){ For(node->exprs){

View File

@@ -557,7 +557,7 @@ 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);
node->type = expr.type; node->resolved_type = expr.type;
return expr; return expr;
} }
@@ -655,7 +655,7 @@ 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)) type = decl->type_val; if(type == type_type && is_enum(decl->type_val)) type = decl->type_val;
if(current) current->type = type; if(current) current->resolved_type = type;
if(is_pointer(type)) type = type->base; if(is_pointer(type)) type = type->base;
type_complete(type); type_complete(type);
@@ -706,9 +706,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
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 != type_type) compiler_error(node->pos, "Prefix array operator is only allowed on types");
if(node->expr){ if(node->expr){
node->type = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val)); node->resolved_type = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val));
} else{ } else{
node->type = type_slice(type.type_val, node); node->resolved_type = type_slice(type.type_val, node);
{ // @c_backend { // @c_backend
Ast_Scope *scope = ast_decl_scope(0, pctx->heap, (Ast_File *)node->parent_scope->file); Ast_Scope *scope = ast_decl_scope(0, pctx->heap, (Ast_File *)node->parent_scope->file);
@@ -722,23 +722,23 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
Ast_Decl *struct_decl = ast_struct(0, scope); Ast_Decl *struct_decl = ast_struct(0, scope);
String name = gen_string_simple_decl(pctx->perm, node->type); String name = gen_string_simple_decl(pctx->perm, node->resolved_type);
struct_decl->name = pctx->intern(name); struct_decl->name = pctx->intern(name);
struct_decl->type = type_type; struct_decl->type = type_type;
struct_decl->type_val = node->type; struct_decl->type_val = node->resolved_type;
pctx->ordered_decls.add(struct_decl); pctx->ordered_decls.add(struct_decl);
} }
} }
return operand_type(node->type); return operand_type(node->resolved_type);
BREAK(); BREAK();
} }
CASE(LAMBDA_EXPR, Lambda){ CASE(LAMBDA_EXPR, Lambda){
node->type = resolve_lambda_type(node); node->resolved_type = resolve_lambda_type(node);
Operand result = operand_type(node->type); Operand result = operand_type(node->resolved_type);
try_resolving_lambda_scope(&result, node, node->type); try_resolving_lambda_scope(&result, node, node->resolved_type);
return result; return result;
BREAK(); BREAK();
} }
@@ -753,7 +753,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
compiler_error(node->pos, "Indexing variable that is not an [Array] or [Pointer], it's of type %s instead", docname(left.type)); compiler_error(node->pos, "Indexing variable that is not an [Array] or [Pointer], it's of type %s instead", docname(left.type));
} }
node->original_type = left.type; node->index_original_type = left.type;
return operand_lvalue(left.type->arr.base); return operand_lvalue(left.type->arr.base);
BREAK(); BREAK();
} }
@@ -767,7 +767,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
right.value = convert_untyped_to_typed(node->pos, right.value, left.type); right.value = convert_untyped_to_typed(node->pos, right.value, left.type);
if(left.type != right.type) compiler_error(node->pos, "Can't assign value when left is %s and right is %s", docname(left.type), docname(right.type)); if(left.type != right.type) compiler_error(node->pos, "Can't assign value when left is %s and right is %s", docname(left.type), docname(right.type));
node->type = right.type; node->resolved_type = right.type;
return {}; return {};
} }
else if(node->op == TK_Arrow){ else if(node->op == TK_Arrow){
@@ -805,18 +805,18 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
Operand value = resolve_expr(node->expr, AST_CANT_BE_NULL); Operand value = resolve_expr(node->expr, AST_CANT_BE_NULL);
if(node->op == TK_Pointer){ if(node->op == TK_Pointer){
if(value.type->kind == TYPE_POINTER){ if(value.type->kind == TYPE_POINTER){
node->type = value.type->base; node->resolved_type = value.type->base;
return operand_lvalue(node->type); return operand_lvalue(node->resolved_type);
} }
else if(value.type->kind == TYPE_TYPE){ else if(value.type->kind == TYPE_TYPE){
node->type = type_pointer(value.type_val); node->resolved_type = type_pointer(value.type_val);
return operand_type(node->type); return operand_type(node->resolved_type);
} }
else{ compiler_error(node->pos, "Dereferencing expression %s that is not a [Pointer] or [Type]", docname(value.type)); return {}; } else{ compiler_error(node->pos, "Dereferencing expression %s that is not a [Pointer] or [Type]", docname(value.type)); return {}; }
} }
else if(node->op == TK_Dereference){ else if(node->op == TK_Dereference){
node->type = type_pointer(value.type); node->resolved_type = type_pointer(value.type);
return operand_lvalue(node->type); return operand_lvalue(node->resolved_type);
} }
else{ else{
eval_unary(node->pos, node->op, &value); eval_unary(node->pos, node->op, &value);
@@ -845,7 +845,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
compiler_error(node->pos, "Couldn't infer type of compound expression"); compiler_error(node->pos, "Couldn't infer type of compound expression");
type_complete(type); type_complete(type);
node->type = type; node->resolved_type = type;
if(is_array(type)){ if(is_array(type)){
resolve_compound_array(node, type); resolve_compound_array(node, type);
} }
@@ -965,12 +965,12 @@ resolve_decl(Ast_Decl *ast){
switch(ast->kind){ switch(ast->kind){
CASE(LAMBDA, Decl){ CASE(LAMBDA, Decl){
Ast_Lambda *lambda = node->lambda; Ast_Lambda *lambda = node->lambda;
lambda->type = resolve_lambda_type(lambda); lambda->resolved_type = resolve_lambda_type(lambda);
Operand result = operand_type(lambda->type); Operand result = operand_type(lambda->resolved_type);
// @note: top level lambda needs to get marked as resolved // @note: top level lambda needs to get marked as resolved
// so that the cyclic dependency wont trigger // so that the cyclic dependency wont trigger
node->type = lambda->type; node->type = lambda->resolved_type;
node->state = DECL_RESOLVED; node->state = DECL_RESOLVED;
// @todo: We also need to make sure there is a return value when ret type is not void // @todo: We also need to make sure there is a return value when ret type is not void