Big Refactor to type less :P

This commit is contained in:
gingerBill
2016-08-01 13:11:50 +01:00
parent 88e05ad2b2
commit e5665a190d
10 changed files with 1398 additions and 1285 deletions

View File

@@ -1,6 +1,4 @@
define void @main() {
entry:
%0 = alloca i64, align 8 ; a
store i64 zeroinitializer, i64* %0
ret void
}

View File

@@ -1,3 +1,2 @@
main :: proc() {
a : int;
}

View File

@@ -19,8 +19,7 @@ struct Operand {
AddressingMode mode;
Type *type;
ExactValue value;
AstNode *expression;
AstNode *expr;
BuiltinProcedureId builtin_id;
};
@@ -30,7 +29,7 @@ struct TypeAndValue {
ExactValue value;
};
struct DeclarationInfo {
struct DeclInfo {
Scope *scope;
Entity **entities;
@@ -45,26 +44,26 @@ struct DeclarationInfo {
};
void init_declaration_info(DeclarationInfo *d, Scope *scope) {
void init_declaration_info(DeclInfo *d, Scope *scope) {
d->scope = scope;
map_init(&d->deps, gb_heap_allocator());
}
DeclarationInfo *make_declaration_info(gbAllocator a, Scope *scope) {
DeclarationInfo *d = gb_alloc_item(a, DeclarationInfo);
DeclInfo *make_declaration_info(gbAllocator a, Scope *scope) {
DeclInfo *d = gb_alloc_item(a, DeclInfo);
init_declaration_info(d, scope);
return d;
}
void destroy_declaration_info(DeclarationInfo *d) {
void destroy_declaration_info(DeclInfo *d) {
map_destroy(&d->deps);
}
b32 has_init(DeclarationInfo *d) {
b32 has_init(DeclInfo *d) {
if (d->init_expr != NULL)
return true;
if (d->proc_decl != NULL) {
if (d->proc_decl->procedure_declaration.body != NULL)
if (d->proc_decl->proc_decl.body != NULL)
return true;
}
@@ -91,7 +90,7 @@ ExpressionInfo make_expression_info(b32 is_lhs, AddressingMode mode, Type *type,
struct ProcedureInfo {
AstFile *file;
Token token;
DeclarationInfo *decl;
DeclInfo *decl;
Type * type; // Type_Procedure
AstNode * body; // AstNode_BlockStatement
};
@@ -151,7 +150,7 @@ gb_global BuiltinProcedure builtin_procedures[BuiltinProcedure_Count] = {
struct CheckerContext {
Scope *scope;
DeclarationInfo *decl;
DeclInfo *decl;
};
struct CheckerInfo {
@@ -160,7 +159,7 @@ struct CheckerInfo {
Map<Entity *> uses; // Key: AstNode * | Identifier -> Entity
Map<Scope *> scopes; // Key: AstNode * | Node -> Scope
Map<ExpressionInfo> untyped; // Key: AstNode * | Expression -> ExpressionInfo
Map<DeclarationInfo *> entities; // Key: Entity *
Map<DeclInfo *> entities; // Key: Entity *
};
struct Checker {
@@ -259,7 +258,7 @@ Entity *scope_insert_entity(Scope *s, Entity *entity) {
return NULL;
}
void add_dependency(DeclarationInfo *d, Entity *e) {
void add_dependency(DeclInfo *d, Entity *e) {
map_set(&d->deps, hash_pointer(e), cast(b32)true);
}
@@ -391,8 +390,8 @@ TypeAndValue *type_and_value_of_expression(CheckerInfo *i, AstNode *expression)
}
Entity *entity_of_identifier(CheckerInfo *i, AstNode *identifier) {
GB_ASSERT(identifier->kind == AstNode_Identifier);
Entity *entity_of_ident(CheckerInfo *i, AstNode *identifier) {
GB_ASSERT(identifier->kind == AstNode_Ident);
Entity **found = map_get(&i->definitions, hash_pointer(identifier));
if (found)
return *found;
@@ -403,12 +402,12 @@ Entity *entity_of_identifier(CheckerInfo *i, AstNode *identifier) {
return NULL;
}
Type *type_of_expression(CheckerInfo *i, AstNode *expression) {
Type *type_of_expr(CheckerInfo *i, AstNode *expression) {
TypeAndValue *found = type_and_value_of_expression(i, expression);
if (found)
return found->type;
if (expression->kind == AstNode_Identifier) {
Entity *entity = entity_of_identifier(i, expression);
if (expression->kind == AstNode_Ident) {
Entity *entity = entity_of_ident(i, expression);
if (entity)
return entity->type;
}
@@ -441,7 +440,7 @@ void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode
void add_entity_definition(CheckerInfo *i, AstNode *identifier, Entity *entity) {
GB_ASSERT(identifier != NULL);
GB_ASSERT(identifier->kind == AstNode_Identifier);
GB_ASSERT(identifier->kind == AstNode_Ident);
u64 key = hash_pointer(identifier);
map_set(&i->definitions, key, entity);
}
@@ -460,14 +459,14 @@ void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
void add_entity_use(CheckerInfo *i, AstNode *identifier, Entity *entity) {
GB_ASSERT(identifier != NULL);
GB_ASSERT(identifier->kind == AstNode_Identifier);
GB_ASSERT(identifier->kind == AstNode_Ident);
u64 key = hash_pointer(identifier);
map_set(&i->uses, key, entity);
}
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclarationInfo *d) {
GB_ASSERT(are_strings_equal(identifier->identifier.token.string, e->token.string));
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclInfo *d) {
GB_ASSERT(are_strings_equal(identifier->ident.token.string, e->token.string));
add_entity(c, c->global_scope, identifier, e);
map_set(&c->info.entities, hash_pointer(e), d);
@@ -475,7 +474,7 @@ void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclarationInfo
}
void check_procedure_later(Checker *c, AstFile *file, Token token, DeclarationInfo *decl, Type *type, AstNode *body) {
void check_procedure_later(Checker *c, AstFile *file, Token token, DeclInfo *decl, Type *type, AstNode *body) {
ProcedureInfo info = {};
info.file = file;
info.token = token;
@@ -533,27 +532,27 @@ void check_parsed_files(Checker *c) {
gb_for_array(i, c->parser->files) {
AstFile *f = &c->parser->files[i];
add_curr_ast_file(c, f);
for (AstNode *decl = f->declarations; decl != NULL; decl = decl->next) {
if (!is_ast_node_declaration(decl))
for (AstNode *decl = f->decls; decl != NULL; decl = decl->next) {
if (!is_ast_node_decl(decl))
continue;
switch (decl->kind) {
case AstNode_BadDeclaration:
case AstNode_BadDecl:
break;
case AstNode_VariableDeclaration: {
auto *vd = &decl->variable_declaration;
case AstNode_VarDecl: {
auto *vd = &decl->var_decl;
switch (vd->kind) {
case Declaration_Immutable: {
for (AstNode *name = vd->name_list, *value = vd->value_list;
name != NULL && value != NULL;
name = name->next, value = value->next) {
GB_ASSERT(name->kind == AstNode_Identifier);
GB_ASSERT(name->kind == AstNode_Ident);
ExactValue v = {ExactValue_Invalid};
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->identifier.token, NULL, v);
DeclarationInfo *di = make_declaration_info(c->allocator, c->global_scope);
di->type_expr = vd->type_expression;
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->ident.token, NULL, v);
DeclInfo *di = make_declaration_info(c->allocator, c->global_scope);
di->type_expr = vd->type;
di->init_expr = value;
add_file_entity(c, name, e, di);
}
@@ -561,7 +560,7 @@ void check_parsed_files(Checker *c) {
isize lhs_count = vd->name_count;
isize rhs_count = vd->value_count;
if (rhs_count == 0 && vd->type_expression == NULL) {
if (rhs_count == 0 && vd->type == NULL) {
error(&c->error_collector, ast_node_token(decl), "Missing type or initial expression");
} else if (lhs_count < rhs_count) {
error(&c->error_collector, ast_node_token(decl), "Extra initial expression");
@@ -572,25 +571,25 @@ void check_parsed_files(Checker *c) {
isize entity_count = vd->name_count;
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
DeclarationInfo *di = NULL;
DeclInfo *di = NULL;
if (vd->value_count == 1) {
di = make_declaration_info(gb_heap_allocator(), c->global_scope);
di->entities = entities;
di->entity_count = entity_count;
di->type_expr = vd->type_expression;
di->type_expr = vd->type;
di->init_expr = vd->value_list;
}
AstNode *value = vd->value_list;
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
Entity *e = make_entity_variable(c->allocator, c->global_scope, name->identifier.token, NULL);
Entity *e = make_entity_variable(c->allocator, c->global_scope, name->ident.token, NULL);
entities[entity_index++] = e;
DeclarationInfo *d = di;
DeclInfo *d = di;
if (d == NULL) {
AstNode *init_expr = value;
d = make_declaration_info(gb_heap_allocator(), c->global_scope);
d->type_expr = vd->type_expression;
d->type_expr = vd->type;
d->init_expr = init_expr;
}
@@ -605,35 +604,35 @@ void check_parsed_files(Checker *c) {
} break;
case AstNode_TypeDeclaration: {
AstNode *identifier = decl->type_declaration.name;
Entity *e = make_entity_type_name(c->allocator, c->global_scope, identifier->identifier.token, NULL);
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
d->type_expr = decl->type_declaration.type_expression;
case AstNode_TypeDecl: {
AstNode *identifier = decl->type_decl.name;
Entity *e = make_entity_type_name(c->allocator, c->global_scope, identifier->ident.token, NULL);
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
d->type_expr = decl->type_decl.type;
add_file_entity(c, identifier, e, d);
} break;
case AstNode_AliasDeclaration: {
AstNode *identifier = decl->alias_declaration.name;
Entity *e = make_entity_alias_name(c->allocator, c->global_scope, identifier->identifier.token, NULL);
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
d->type_expr = decl->alias_declaration.type_expression;
case AstNode_AliasDecl: {
AstNode *identifier = decl->alias_decl.name;
Entity *e = make_entity_alias_name(c->allocator, c->global_scope, identifier->ident.token, NULL);
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
d->type_expr = decl->alias_decl.type;
add_file_entity(c, identifier, e, d);
} break;
case AstNode_ProcedureDeclaration: {
AstNode *identifier = decl->procedure_declaration.name;
Token token = identifier->identifier.token;
case AstNode_ProcDecl: {
AstNode *identifier = decl->proc_decl.name;
Token token = identifier->ident.token;
Entity *e = make_entity_procedure(c->allocator, c->global_scope, token, NULL);
add_entity(c, c->global_scope, identifier, e);
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
d->proc_decl = decl;
map_set(&c->info.entities, hash_pointer(e), d);
e->order = gb_array_count(c->info.entities.entries);
} break;
case AstNode_ImportDeclaration:
case AstNode_ImportDecl:
// NOTE(bill): ignore
break;
@@ -648,8 +647,8 @@ void check_parsed_files(Checker *c) {
gb_for_array(i, c->info.entities.entries) {
auto *entry = &c->info.entities.entries[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key;
DeclarationInfo *d = entry->value;
check_entity_declaration(c, e, d, NULL);
DeclInfo *d = entry->value;
check_entity_decl(c, e, d, NULL);
}
@@ -657,7 +656,7 @@ void check_parsed_files(Checker *c) {
gb_for_array(i, c->procedures) {
ProcedureInfo *pi = &c->procedures[i];
add_curr_ast_file(c, pi->file);
check_procedure_body(c, pi->token, pi->decl, pi->type, pi->body);
check_proc_body(c, pi->token, pi->decl, pi->type, pi->body);
}
@@ -668,7 +667,7 @@ void check_parsed_files(Checker *c) {
AstNode *expr = cast(AstNode *)cast(uintptr)key;
ExpressionInfo *info = &entry->value;
if (is_type_typed(info->type)) {
GB_PANIC("%s (type %s) is typed!", expression_to_string(expr), info->type);
GB_PANIC("%s (type %s) is typed!", expr_to_string(expr), info->type);
}
add_type_and_value(&c->info, expr, info->mode, info->type, info->value);
}

File diff suppressed because it is too large Load Diff

View File

@@ -6,12 +6,12 @@ enum StatementFlag : u32 {
// Statement_FallthroughAllowed = GB_BIT(2), // TODO(bill): fallthrough
};
void check_statement(Checker *c, AstNode *node, u32 flags);
void check_stmt(Checker *c, AstNode *node, u32 flags);
void check_statement_list(Checker *c, AstNode *node, u32 flags) {
void check_stmt_list(Checker *c, AstNode *node, u32 flags) {
for (; node != NULL; node = node->next) {
if (node->kind != AstNode_EmptyStatement) {
check_statement(c, node, flags);
if (node->kind != AstNode_EmptyStmt) {
check_stmt(c, node, flags);
}
}
}
@@ -27,7 +27,7 @@ b32 check_is_terminating_list(Checker *c, AstNode *list) {
// Iterate backwards
for (AstNode *n = list; n != NULL; n = n->prev) {
if (n->kind != AstNode_EmptyStatement)
if (n->kind != AstNode_EmptyStmt)
return check_is_terminating(c, n);
}
@@ -39,26 +39,26 @@ b32 check_is_terminating_list(Checker *c, AstNode *list) {
// TODO(bill): Warn/err against code after `return` that it won't be executed
b32 check_is_terminating(Checker *c, AstNode *node) {
switch (node->kind) {
case AstNode_ReturnStatement:
case AstNode_ReturnStmt:
return true;
case AstNode_BlockStatement:
return check_is_terminating_list(c, node->block_statement.list);
case AstNode_BlockStmt:
return check_is_terminating_list(c, node->block_stmt.list);
case AstNode_ExpressionStatement:
return check_is_terminating(c, node->expression_statement.expression);
case AstNode_ExprStmt:
return check_is_terminating(c, node->expr_stmt.expr);
case AstNode_IfStatement:
if (node->if_statement.else_statement != NULL) {
if (check_is_terminating(c, node->if_statement.body) &&
check_is_terminating(c, node->if_statement.else_statement)) {
case AstNode_IfStmt:
if (node->if_stmt.else_stmt != NULL) {
if (check_is_terminating(c, node->if_stmt.body) &&
check_is_terminating(c, node->if_stmt.else_stmt)) {
return true;
}
}
break;
case AstNode_ForStatement:
if (node->for_statement.cond == NULL) {
case AstNode_ForStmt:
if (node->for_stmt.cond == NULL) {
return true;
}
break;
@@ -141,14 +141,14 @@ void check_assignment(Checker *c, Operand *operand, Type *type, String context_n
if (!check_is_assignable_to(c, operand, type)) {
gbString type_string = type_to_string(type);
gbString op_type_string = type_to_string(operand->type);
gbString expr_str = expression_to_string(operand->expression);
gbString expr_str = expr_to_string(operand->expr);
defer (gb_string_free(type_string));
defer (gb_string_free(op_type_string));
defer (gb_string_free(expr_str));
// TODO(bill): is this a good enough error message?
error(&c->error_collector, ast_node_token(operand->expression),
error(&c->error_collector, ast_node_token(operand->expr),
"Cannot assign value `%s` of type `%s` to `%s` in %.*s",
expr_str,
op_type_string,
@@ -167,11 +167,11 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
return NULL;
}
AstNode *node = unparen_expression(lhs);
AstNode *node = unparen_expr(lhs);
// NOTE(bill): Ignore assignments to `_`
if (node->kind == AstNode_Identifier &&
are_strings_equal(node->identifier.token.string, make_string("_"))) {
if (node->kind == AstNode_Ident &&
are_strings_equal(node->ident.token.string, make_string("_"))) {
add_entity_definition(&c->info, node, NULL);
check_assignment(c, op_a, NULL, make_string("assignment to `_` identifier"));
if (op_a->mode == Addressing_Invalid)
@@ -181,8 +181,8 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
Entity *e = NULL;
b32 used = false;
if (node->kind == AstNode_Identifier) {
e = scope_lookup_entity(c->context.scope, node->identifier.token.string);
if (node->kind == AstNode_Ident) {
e = scope_lookup_entity(c->context.scope, node->ident.token.string);
if (e != NULL && e->kind == Entity_Variable) {
used = e->variable.used; // TODO(bill): Make backup just in case
}
@@ -190,7 +190,7 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
Operand op_b = {Addressing_Invalid};
check_expression(c, &op_b, lhs);
check_expr(c, &op_b, lhs);
if (e) e->variable.used = used;
if (op_b.mode == Addressing_Invalid ||
@@ -204,15 +204,15 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
case Addressing_Invalid:
return NULL;
default: {
if (op_b.expression->kind == AstNode_SelectorExpression) {
if (op_b.expr->kind == AstNode_SelectorExpr) {
// NOTE(bill): Extra error checks
Operand op_c = {Addressing_Invalid};
check_expression(c, &op_c, op_b.expression->selector_expression.operand);
check_expr(c, &op_c, op_b.expr->selector_expr.expr);
}
gbString str = expression_to_string(op_b.expression);
gbString str = expr_to_string(op_b.expr);
defer (gb_string_free(str));
error(&c->error_collector, ast_node_token(op_b.expression), "Cannot assign to `%s`", str);
error(&c->error_collector, ast_node_token(op_b.expr), "Cannot assign to `%s`", str);
} break;
}
@@ -264,7 +264,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNode *in
i < lhs_count && i < init_count && rhs != NULL;
i++, rhs = rhs->next) {
Operand operand = {};
check_multi_expression(c, &operand, rhs);
check_multi_expr(c, &operand, rhs);
if (operand.type->kind != Type_Tuple) {
check_init_variable(c, lhs[i], &operand, context_name);
} else {
@@ -297,8 +297,8 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
if (operand->mode != Addressing_Constant) {
// TODO(bill): better error
error(&c->error_collector, ast_node_token(operand->expression),
"`%.*s` is not a constant", LIT(ast_node_token(operand->expression).string));
error(&c->error_collector, ast_node_token(operand->expr),
"`%.*s` is not a constant", LIT(ast_node_token(operand->expr).string));
if (e->type == NULL)
e->type = &basic_types[Basic_Invalid];
return;
@@ -319,7 +319,7 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
}
void check_constant_declaration(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr) {
void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr) {
GB_ASSERT(e->type == NULL);
if (e->variable.visited) {
@@ -343,11 +343,11 @@ void check_constant_declaration(Checker *c, Entity *e, AstNode *type_expr, AstNo
Operand operand = {Addressing_Invalid};
if (init_expr)
check_expression(c, &operand, init_expr);
check_expr(c, &operand, init_expr);
check_init_constant(c, e, &operand);
}
void check_type_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *named_type) {
void check_type_decl(Checker *c, Entity *e, AstNode *type_expr, Type *named_type) {
GB_ASSERT(e->type == NULL);
Type *named = make_type_named(c->allocator, e->token.string, NULL, e);
named->named.type_name = e;
@@ -359,7 +359,7 @@ void check_type_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *nam
set_base_type(named, get_base_type(get_base_type(named)));
}
void check_alias_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *alias_type) {
void check_alias_decl(Checker *c, Entity *e, AstNode *type_expr, Type *alias_type) {
GB_ASSERT(e->type == NULL);
Type *named = make_type_alias(c->allocator, e->token.string, NULL, e);
named->alias.alias_name = e;
@@ -371,18 +371,18 @@ void check_alias_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *al
set_base_type(named, get_base_type(get_base_type(named)));
}
void check_procedure_body(Checker *c, Token token, DeclarationInfo *decl, Type *type, AstNode *body) {
GB_ASSERT(body->kind == AstNode_BlockStatement);
void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body) {
GB_ASSERT(body->kind == AstNode_BlockStmt);
CheckerContext old_context = c->context;
c->context.scope = decl->scope;
c->context.decl = decl;
push_procedure(c, type);
check_statement_list(c, body->block_statement.list, 0);
check_stmt_list(c, body->block_stmt.list, 0);
if (type->procedure.result_count > 0) {
if (!check_is_terminating(c, body)) {
error(&c->error_collector, body->block_statement.close, "Missing return statement at the end of the procedure");
error(&c->error_collector, body->block_stmt.close, "Missing return statement at the end of the procedure");
}
}
pop_procedure(c);
@@ -390,12 +390,12 @@ void check_procedure_body(Checker *c, Token token, DeclarationInfo *decl, Type *
c->context = old_context;
}
void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32 check_body_later) {
void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
GB_ASSERT(e->type == NULL);
Type *proc_type = make_type_procedure(c->allocator, e->parent, NULL, 0, NULL, 0);
e->type = proc_type;
auto *pd = &d->proc_decl->procedure_declaration;
auto *pd = &d->proc_decl->proc_decl;
#if 1
Scope *original_curr_scope = c->context.scope;
@@ -407,9 +407,9 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
b32 is_inline = false;
b32 is_no_inline = false;
for (AstNode *tag = pd->tag_list; tag != NULL; tag = tag->next) {
GB_ASSERT(tag->kind == AstNode_TagExpression);
GB_ASSERT(tag->kind == AstNode_TagExpr);
String tag_name = tag->tag_expression.name.string;
String tag_name = tag->tag_expr.name.string;
if (are_strings_equal(tag_name, make_string("foreign"))) {
is_foreign = true;
} else if (are_strings_equal(tag_name, make_string("inline"))) {
@@ -435,11 +435,11 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
d->scope = c->context.scope;
GB_ASSERT(pd->body->kind == AstNode_BlockStatement);
GB_ASSERT(pd->body->kind == AstNode_BlockStmt);
if (check_body_later) {
check_procedure_later(c, c->curr_ast_file, e->token, d, proc_type, pd->body);
} else {
check_procedure_body(c, e->token, d, proc_type, pd->body);
check_proc_body(c, e->token, d, proc_type, pd->body);
}
}
@@ -450,7 +450,7 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
}
void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize entity_count, AstNode *type_expr, AstNode *init_expr) {
void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count, AstNode *type_expr, AstNode *init_expr) {
GB_ASSERT(e->type == NULL);
GB_ASSERT(e->kind == Entity_Variable);
@@ -472,7 +472,7 @@ void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize
if (entities == NULL || entity_count == 1) {
GB_ASSERT(entities == NULL || entities[0] == e);
Operand operand = {};
check_expression(c, &operand, init_expr);
check_expr(c, &operand, init_expr);
check_init_variable(c, e, &operand, make_string("variable declaration"));
}
@@ -486,26 +486,26 @@ void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize
void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *d, Type *named_type) {
void check_entity_decl(Checker *c, Entity *e, DeclInfo *d, Type *named_type) {
if (e->type != NULL)
return;
switch (e->kind) {
case Entity_Constant:
c->context.decl = d;
check_constant_declaration(c, e, d->type_expr, d->init_expr);
check_const_decl(c, e, d->type_expr, d->init_expr);
break;
case Entity_Variable:
c->context.decl = d;
check_variable_declaration(c, e, d->entities, d->entity_count, d->type_expr, d->init_expr);
check_var_decl(c, e, d->entities, d->entity_count, d->type_expr, d->init_expr);
break;
case Entity_TypeName:
check_type_declaration(c, e, d->type_expr, named_type);
check_type_decl(c, e, d->type_expr, named_type);
break;
case Entity_AliasName:
check_alias_declaration(c, e, d->type_expr, named_type);
check_alias_decl(c, e, d->type_expr, named_type);
break;
case Entity_Procedure:
check_procedure_declaration(c, e, d, true);
check_proc_decl(c, e, d, true);
break;
}
@@ -514,15 +514,15 @@ void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *d, Type *n
void check_statement(Checker *c, AstNode *node, u32 flags) {
void check_stmt(Checker *c, AstNode *node, u32 flags) {
switch (node->kind) {
case AstNode_EmptyStatement: break;
case AstNode_BadStatement: break;
case AstNode_BadDeclaration: break;
case AstNode_EmptyStmt: break;
case AstNode_BadStmt: break;
case AstNode_BadDecl: break;
case AstNode_ExpressionStatement: {
case AstNode_ExprStmt: {
Operand operand = {Addressing_Invalid};
ExpressionKind kind = check_expression_base(c, &operand, node->expression_statement.expression);
ExpressionKind kind = check_expr_base(c, &operand, node->expr_stmt.expr);
switch (operand.mode) {
case Addressing_Type:
error(&c->error_collector, ast_node_token(node), "Is not an expression");
@@ -535,15 +535,15 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
}
} break;
case AstNode_TagStatement:
case AstNode_TagStmt:
// TODO(bill): Tag Statements
error(&c->error_collector, ast_node_token(node), "Tag statements are not supported yet");
check_statement(c, node->tag_statement.statement, flags);
check_stmt(c, node->tag_stmt.stmt, flags);
break;
case AstNode_IncDecStatement: {
case AstNode_IncDecStmt: {
Token op = {};
auto *s = &node->inc_dec_statement;
auto *s = &node->inc_dec_stmt;
op = s->op;
switch (s->op.kind) {
case Token_Increment:
@@ -560,7 +560,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
}
Operand operand = {Addressing_Invalid};
check_expression(c, &operand, s->expression);
check_expr(c, &operand, s->expr);
if (operand.mode == Addressing_Invalid)
return;
if (!is_type_numeric(operand.type)) {
@@ -568,36 +568,36 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
return;
}
AstNode basic_lit = {AstNode_BasicLiteral};
basic_lit.basic_literal = s->op;
basic_lit.basic_literal.kind = Token_Integer;
basic_lit.basic_literal.string = make_string("1");
AstNode basic_lit = {AstNode_BasicLit};
basic_lit.basic_lit = s->op;
basic_lit.basic_lit.kind = Token_Integer;
basic_lit.basic_lit.string = make_string("1");
AstNode be = {AstNode_BinaryExpression};
be.binary_expression.op = op;
be.binary_expression.left = s->expression;;
be.binary_expression.right = &basic_lit;
check_binary_expression(c, &operand, &be);
AstNode be = {AstNode_BinaryExpr};
be.binary_expr.op = op;
be.binary_expr.left = s->expr;;
be.binary_expr.right = &basic_lit;
check_binary_expr(c, &operand, &be);
} break;
case AstNode_AssignStatement:
switch (node->assign_statement.op.kind) {
case AstNode_AssignStmt:
switch (node->assign_stmt.op.kind) {
case Token_Eq: {
// a, b, c = 1, 2, 3; // Multisided
if (node->assign_statement.lhs_count == 0) {
error(&c->error_collector, node->assign_statement.op, "Missing lhs in assignment statement");
if (node->assign_stmt.lhs_count == 0) {
error(&c->error_collector, node->assign_stmt.op, "Missing lhs in assignment statement");
return;
}
Operand operand = {};
AstNode *lhs = node->assign_statement.lhs_list;
AstNode *rhs = node->assign_statement.rhs_list;
AstNode *lhs = node->assign_stmt.lhs_list;
AstNode *rhs = node->assign_stmt.rhs_list;
isize i = 0;
for (;
lhs != NULL && rhs != NULL;
lhs = lhs->next, rhs = rhs->next) {
check_multi_expression(c, &operand, rhs);
check_multi_expr(c, &operand, rhs);
if (operand.type->kind != Type_Tuple) {
check_assignment_variable(c, &operand, lhs);
i++;
@@ -615,7 +615,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
}
}
if (i < node->assign_statement.lhs_count && i < node->assign_statement.rhs_count) {
if (i < node->assign_stmt.lhs_count && i < node->assign_stmt.rhs_count) {
if (lhs == NULL)
error(&c->error_collector, ast_node_token(lhs), "Too few values on the right hand side of the declaration");
} else if (rhs != NULL) {
@@ -625,9 +625,9 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
default: {
// a += 1; // Single-sided
Token op = node->assign_statement.op;
if (node->assign_statement.lhs_count != 1 ||
node->assign_statement.rhs_count != 1) {
Token op = node->assign_stmt.op;
if (node->assign_stmt.lhs_count != 1 ||
node->assign_stmt.rhs_count != 1) {
error(&c->error_collector, op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
return;
}
@@ -637,61 +637,61 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
}
// TODO(bill): Check if valid assignment operator
Operand operand = {Addressing_Invalid};
AstNode be = {AstNode_BinaryExpression};
be.binary_expression.op = op;
AstNode be = {AstNode_BinaryExpr};
be.binary_expr.op = op;
// NOTE(bill): Only use the first one will be used
be.binary_expression.left = node->assign_statement.lhs_list;
be.binary_expression.right = node->assign_statement.rhs_list;
be.binary_expr.left = node->assign_stmt.lhs_list;
be.binary_expr.right = node->assign_stmt.rhs_list;
check_binary_expression(c, &operand, &be);
check_binary_expr(c, &operand, &be);
if (operand.mode == Addressing_Invalid)
return;
// NOTE(bill): Only use the first one will be used
check_assignment_variable(c, &operand, node->assign_statement.lhs_list);
check_assignment_variable(c, &operand, node->assign_stmt.lhs_list);
} break;
}
break;
case AstNode_BlockStatement:
case AstNode_BlockStmt:
check_open_scope(c, node);
check_statement_list(c, node->block_statement.list, flags);
check_stmt_list(c, node->block_stmt.list, flags);
check_close_scope(c);
break;
case AstNode_IfStatement: {
case AstNode_IfStmt: {
check_open_scope(c, node);
defer (check_close_scope(c));
auto *is = &node->if_statement;
auto *is = &node->if_stmt;
if (is->init != NULL)
check_statement(c, is->init, 0);
check_stmt(c, is->init, 0);
Operand operand = {Addressing_Invalid};
check_expression(c, &operand, node->if_statement.cond);
check_expr(c, &operand, node->if_stmt.cond);
if (operand.mode != Addressing_Invalid &&
!is_type_boolean(operand.type)) {
error(&c->error_collector, ast_node_token(node->if_statement.cond),
error(&c->error_collector, ast_node_token(node->if_stmt.cond),
"Non-boolean condition in `if` statement");
}
check_statement(c, node->if_statement.body, flags);
check_stmt(c, node->if_stmt.body, flags);
if (node->if_statement.else_statement) {
switch (node->if_statement.else_statement->kind) {
case AstNode_IfStatement:
case AstNode_BlockStatement:
check_statement(c, node->if_statement.else_statement, flags);
if (node->if_stmt.else_stmt) {
switch (node->if_stmt.else_stmt->kind) {
case AstNode_IfStmt:
case AstNode_BlockStmt:
check_stmt(c, node->if_stmt.else_stmt, flags);
break;
default:
error(&c->error_collector, ast_node_token(node->if_statement.else_statement),
error(&c->error_collector, ast_node_token(node->if_stmt.else_stmt),
"Invalid `else` statement in `if` statement");
break;
}
}
} break;
case AstNode_ReturnStatement: {
auto *rs = &node->return_statement;
case AstNode_ReturnStmt: {
auto *rs = &node->return_stmt;
GB_ASSERT(gb_array_count(c->procedure_stack) > 0);
if (c->in_defer) {
@@ -712,45 +712,45 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
} else if (result_count > 0) {
auto *tuple = &proc_type->procedure.results->tuple;
check_init_variables(c, tuple->variables, tuple->variable_count,
rs->results, rs->result_count, make_string("return statement"));
rs->result_list, rs->result_count, make_string("return statement"));
}
} break;
case AstNode_ForStatement: {
case AstNode_ForStmt: {
flags |= Statement_BreakAllowed | Statement_ContinueAllowed;
check_open_scope(c, node);
defer (check_close_scope(c));
if (node->for_statement.init != NULL)
check_statement(c, node->for_statement.init, 0);
if (node->for_statement.cond) {
if (node->for_stmt.init != NULL)
check_stmt(c, node->for_stmt.init, 0);
if (node->for_stmt.cond) {
Operand operand = {Addressing_Invalid};
check_expression(c, &operand, node->for_statement.cond);
check_expr(c, &operand, node->for_stmt.cond);
if (operand.mode != Addressing_Invalid &&
!is_type_boolean(operand.type)) {
error(&c->error_collector, ast_node_token(node->for_statement.cond),
error(&c->error_collector, ast_node_token(node->for_stmt.cond),
"Non-boolean condition in `for` statement");
}
}
if (node->for_statement.end != NULL)
check_statement(c, node->for_statement.end, 0);
check_statement(c, node->for_statement.body, flags);
if (node->for_stmt.end != NULL)
check_stmt(c, node->for_stmt.end, 0);
check_stmt(c, node->for_stmt.body, flags);
} break;
case AstNode_DeferStatement: {
auto *ds = &node->defer_statement;
if (is_ast_node_declaration(ds->statement)) {
case AstNode_DeferStmt: {
auto *ds = &node->defer_stmt;
if (is_ast_node_decl(ds->stmt)) {
error(&c->error_collector, ds->token, "You cannot defer a declaration");
} else {
b32 out_in_defer = c->in_defer;
c->in_defer = true;
check_statement(c, ds->statement, 0);
check_stmt(c, ds->stmt, 0);
c->in_defer = out_in_defer;
}
} break;
case AstNode_BranchStatement: {
Token token = node->branch_statement.token;
case AstNode_BranchStmt: {
Token token = node->branch_stmt.token;
switch (token.kind) {
case Token_break:
if ((flags & Statement_BreakAllowed) == 0)
@@ -768,8 +768,8 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
// Declarations
case AstNode_VariableDeclaration: {
auto *vd = &node->variable_declaration;
case AstNode_VarDecl: {
auto *vd = &node->var_decl;
isize entity_count = vd->name_count;
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
@@ -780,8 +780,8 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
Entity *entity = NULL;
Token token = name->identifier.token;
if (name->kind == AstNode_Identifier) {
Token token = name->ident.token;
if (name->kind == AstNode_Ident) {
String str = token.string;
Entity *found = NULL;
// NOTE(bill): Ignore assignments to `_`
@@ -807,8 +807,8 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
}
Type *init_type = NULL;
if (vd->type_expression) {
init_type = check_type(c, vd->type_expression, NULL);
if (vd->type) {
init_type = check_type(c, vd->type, NULL);
if (init_type == NULL)
init_type = &basic_types[Basic_Invalid];
}
@@ -839,18 +839,18 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
for (AstNode *name = vd->name_list, *value = vd->value_list;
name != NULL && value != NULL;
name = name->next, value = value->next) {
GB_ASSERT(name->kind == AstNode_Identifier);
GB_ASSERT(name->kind == AstNode_Ident);
ExactValue v = {ExactValue_Invalid};
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->identifier.token, NULL, v);
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->ident.token, NULL, v);
entities[entity_index++] = e;
check_constant_declaration(c, e, vd->type_expression, value);
check_const_decl(c, e, vd->type, value);
}
isize lhs_count = vd->name_count;
isize rhs_count = vd->value_count;
// TODO(bill): Better error messages or is this good enough?
if (rhs_count == 0 && vd->type_expression == NULL) {
if (rhs_count == 0 && vd->type == NULL) {
error(&c->error_collector, ast_node_token(node), "Missing type or initial expression");
} else if (lhs_count < rhs_count) {
error(&c->error_collector, ast_node_token(node), "Extra initial expression");
@@ -868,32 +868,32 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
}
} break;
case AstNode_ProcedureDeclaration: {
auto *pd = &node->procedure_declaration;
Entity *e = make_entity_procedure(c->allocator, c->context.scope, pd->name->identifier.token, NULL);
case AstNode_ProcDecl: {
auto *pd = &node->proc_decl;
Entity *e = make_entity_procedure(c->allocator, c->context.scope, pd->name->ident.token, NULL);
add_entity(c, c->context.scope, pd->name, e);
DeclarationInfo decl = {};
DeclInfo decl = {};
init_declaration_info(&decl, e->parent);
decl.proc_decl = node;
check_procedure_declaration(c, e, &decl, false);
check_proc_decl(c, e, &decl, false);
destroy_declaration_info(&decl);
} break;
case AstNode_TypeDeclaration: {
auto *td = &node->type_declaration;
case AstNode_TypeDecl: {
auto *td = &node->type_decl;
AstNode *name = td->name;
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name->identifier.token, NULL);
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name->ident.token, NULL);
add_entity(c, c->context.scope, name, e);
check_type_declaration(c, e, td->type_expression, NULL);
check_type_decl(c, e, td->type, NULL);
} break;
case AstNode_AliasDeclaration: {
auto *ad = &node->alias_declaration;
case AstNode_AliasDecl: {
auto *ad = &node->alias_decl;
AstNode *name = ad->name;
Entity *e = make_entity_alias_name(c->allocator, c->context.scope, name->identifier.token, NULL);
Entity *e = make_entity_alias_name(c->allocator, c->context.scope, name->ident.token, NULL);
add_entity(c, c->context.scope, name, e);
check_alias_declaration(c, e, ad->type_expression, NULL);
check_alias_decl(c, e, ad->type, NULL);
} break;
}
}

View File

@@ -39,7 +39,7 @@ void ssa_gen_code(ssaGen *s) {
gb_for_array(i, info->entities.entries) {
auto *entry = &info->entities.entries[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key;
DeclarationInfo *decl = entry->value;
DeclInfo *decl = entry->value;
String name = e->token.string;

View File

@@ -228,10 +228,9 @@ void ssa_print_value(gbFile *f, ssaModule *m, ssaValue *value, Type *type_hint)
void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
GB_ASSERT(value->kind == ssaValue_Instruction);
ssaInstruction *instr = &value->instruction;
ssa_fprintf(f, "\t");
ssaInstruction *instr = &value->instruction;
switch (instr->kind) {
case ssaInstruction_Local: {
Type *type = instr->local.entity->type;
@@ -261,7 +260,7 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
} break;
case ssaInstruction_Load: {
Type *type = ssa_value_type(instr->load.address);
Type *type = instr->load.type;
ssa_fprintf(f, "%%%d = load ", value->id);
ssa_print_type(f, m->sizes, type);
ssa_fprintf(f, ", ");
@@ -271,6 +270,32 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
ssa_fprintf(f, "\n");
} break;
case ssaInstruction_GetElementPtr: {
Type *rt = instr->get_element_ptr.result_type;
Type *et = instr->get_element_ptr.element_type;
Type *t_int = &basic_types[Basic_int];
ssa_fprintf(f, "%%%d = getelementptr ", value->id);
if (instr->get_element_ptr.inbounds)
ssa_fprintf(f, "inbounds ");
ssa_print_type(f, m->sizes, et);
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, et);
ssa_fprintf(f, "* ");
ssa_print_value(f, m, instr->get_element_ptr.address, et);
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, t_int);
ssa_fprintf(f, " ");
ssa_print_value(f, m, instr->get_element_ptr.indices[0], t_int);
if (instr->get_element_ptr.index_count == 2) {
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, t_int);
ssa_fprintf(f, " ");
ssa_print_value(f, m, instr->get_element_ptr.indices[1], t_int);
}
ssa_fprintf(f, "\n");
} break;
case ssaInstruction_BinaryOp: {
auto *bo = &value->instruction.binary_op;
@@ -366,7 +391,7 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
} break;
case ssaValue_Global: {
ssaGlobal *g = &v->global;
auto *g = &v->global;
ssa_print_encoded_global(f, g->entity->token.string);
ssa_fprintf(f, " = global ");
ssa_print_type(f, m->sizes, get_base_type(g->entity->type));

View File

@@ -31,7 +31,7 @@ struct ssaProcedure {
String name;
Entity *entity;
Type *type;
DeclarationInfo *decl;
DeclInfo *decl;
AstNode *type_expr;
AstNode *body;
@@ -42,36 +42,26 @@ struct ssaProcedure {
struct ssaTypeName {
Entity *entity;
Type *type;
};
struct ssaGlobal {
b32 generated;
Entity *entity;
Type *type;
ssaValue *value;
};
struct ssaConstant {
Type *type;
ExactValue value;
};
#define SSA_INSTRUCTION_KINDS \
SSA_INSTRUCTION_KIND(Invalid), \
SSA_INSTRUCTION_KIND(Local), \
SSA_INSTRUCTION_KIND(Store), \
SSA_INSTRUCTION_KIND(Load), \
SSA_INSTRUCTION_KIND(GetElementPtr), \
SSA_INSTRUCTION_KIND(Convert), \
SSA_INSTRUCTION_KIND(BinaryOp), \
SSA_INSTRUCTION_KIND(Count),
enum ssaInstructionKind {
ssaInstruction_Invalid,
#define SSA_INSTRUCTION_KIND(x) GB_JOIN2(ssaInstruction_, x)
SSA_INSTRUCTION_KINDS
#undef SSA_INSTRUCTION_KIND
};
ssaInstruction_Local,
ssaInstruction_Store,
ssaInstruction_Load,
ssaInstruction_GetElementPtr,
ssaInstruction_Convert,
ssaInstruction_BinaryOp,
ssaInstruction_Count,
String const ssa_instruction_strings[] = {
#define SSA_INSTRUCTION_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1}
SSA_INSTRUCTION_KINDS
#undef SSA_INSTRUCTION_KIND
};
struct ssaInstruction {
@@ -91,18 +81,18 @@ struct ssaInstruction {
ssaValue *value;
} store;
struct {
Type *type;
ssaValue *address;
} load;
struct {
ssaValue *address;
Type *result_type;
Type *element_type;
isize index_count;
isize indices[2];
Type * result_type;
Type * element_type;
ssaValue *indices[2];
isize index_count;
b32 inbounds;
} get_element_ptr;
struct {
Type *type;
Token op;
@@ -131,9 +121,20 @@ struct ssaValue {
i32 id;
union {
ssaConstant constant;
ssaTypeName type_name;
ssaGlobal global;
struct {
Type * type;
ExactValue value;
} constant;
struct {
Entity *entity;
Type * type;
} type_name;
struct {
b32 generated;
Entity * entity;
Type * type;
ssaValue *value;
} global;
ssaProcedure procedure;
ssaBlock block;
ssaInstruction instruction;
@@ -188,7 +189,9 @@ Type *ssa_instruction_type(ssaInstruction *instr) {
case ssaInstruction_Store:
return ssa_value_type(instr->store.address);
case ssaInstruction_Load:
return ssa_value_type(instr->load.address);
return instr->load.type;
case ssaInstruction_GetElementPtr:
return instr->get_element_ptr.result_type;
case ssaInstruction_BinaryOp:
return instr->binary_op.type;
}
@@ -204,7 +207,10 @@ void ssa_instruction_set_type(ssaInstruction *instr, Type *type) {
ssa_value_set_type(instr->store.value, type);
break;
case ssaInstruction_Load:
// NOTE(bill): Do nothing
instr->load.type = type;
break;
case ssaInstruction_GetElementPtr:
instr->get_element_ptr.result_type = type;
break;
case ssaInstruction_BinaryOp:
instr->binary_op.type = type;
@@ -251,8 +257,8 @@ void ssa_value_set_type(ssaValue *value, Type *type) {
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr);
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv);
ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr);
ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv);
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr);
ssaValue *ssa_emit_conversion(ssaProcedure *proc, ssaValue *value, Type *a_type);
@@ -318,16 +324,24 @@ ssaValue *ssa_make_instruction_load(ssaProcedure *p, ssaValue *address) {
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Load);
ssaInstruction *i = &v->instruction;
i->load.address = address;
i->load.type = ssa_value_type(address);
if (p->curr_block) {
gb_array_append(p->curr_block->values, v);
}
return v;
}
ssaValue *ssa_make_instruction_get_element_ptr(ssaProcedure *p, ssaValue *address) {
ssaValue *ssa_make_instruction_get_element_ptr(ssaProcedure *p, ssaValue *address,
ssaValue *index0, ssaValue *index1, isize index_count,
b32 inbounds) {
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_GetElementPtr);
ssaInstruction *i = &v->instruction;
i->get_element_ptr.address = address;
i->get_element_ptr.indices[0] = index0;
i->get_element_ptr.indices[1] = index1;
i->get_element_ptr.index_count = index_count;
i->get_element_ptr.element_type = ssa_value_type(address);
i->get_element_ptr.inbounds = inbounds;
if (p->curr_block) {
gb_array_append(p->curr_block->values, v);
}
@@ -354,7 +368,7 @@ ssaValue *ssa_make_value_constant(gbAllocator a, Type *type, ExactValue value) {
return v;
}
ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclarationInfo *decl, ssaModule *m) {
ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclInfo *decl, ssaModule *m) {
ssaValue *v = ssa_alloc_value(a, ssaValue_Procedure);
v->procedure.module = m;
v->procedure.entity = e;
@@ -446,8 +460,8 @@ void ssa_end_procedure_body(ssaProcedure *proc) {
b32 ssa_is_blank_identifier(AstNode *i) {
GB_ASSERT(i->kind == AstNode_Identifier);
return are_strings_equal(i->identifier.token.string, make_string("_"));
GB_ASSERT(i->kind == AstNode_Ident);
return are_strings_equal(i->ident.token.string, make_string("_"));
}
@@ -485,8 +499,6 @@ ssaValue *ssa_emit_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
ssaValue *ssa_emit_load(ssaProcedure *p, ssaValue *address) {
ssaValue *v = ssa_make_instruction_load(p, address);
Type *t = ssa_value_type(address);
ssa_value_set_type(v, type_deref(t));
ssa_emit(p, v);
return v;
}
@@ -590,12 +602,12 @@ ssaValue *ssa_emit_compare(ssaProcedure *proc, Token op, ssaValue *left, ssaValu
return ssa_emit(proc, v);
}
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
switch (expr->kind) {
case AstNode_Identifier: {
case AstNode_Ident: {
Entity *e = *map_get(&proc->module->info->uses, hash_pointer(expr));
if (e->kind == Entity_Builtin) {
// TODO(bill): Entity_Builtin
GB_PANIC("TODO(bill): Entity_Builtin");
return NULL;
}
@@ -605,32 +617,34 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
}
} break;
case AstNode_ParenExpression:
return ssa_build_single_expression(proc, unparen_expression(expr), tv);
case AstNode_ParenExpr:
return ssa_build_single_expr(proc, unparen_expr(expr), tv);
case AstNode_DereferenceExpression: {
ssaLvalue addr = ssa_build_address(proc, expr->dereference_expression.operand);
return ssa_lvalue_load(addr, proc);
case AstNode_DerefExpr: {
ssaLvalue addr = ssa_build_address(proc, expr);
ssaValue *load = ssa_lvalue_load(addr, proc);
ssa_value_set_type(load, type_deref(ssa_value_type(load)));
return load;
} break;
case AstNode_UnaryExpression: {
auto *ue = &expr->unary_expression;
case AstNode_UnaryExpr: {
auto *ue = &expr->unary_expr;
switch (ue->op.kind) {
case Token_Pointer:
return ssa_lvalue_address(ssa_build_address(proc, ue->operand), proc);
return ssa_lvalue_address(ssa_build_address(proc, ue->expr), proc);
case Token_Add:
return ssa_build_expression(proc, ue->operand);
return ssa_build_expr(proc, ue->expr);
case Token_Sub: {
// NOTE(bill): -x == 0 - x
// NOTE(bill): -`x` == 0 - `x`
ExactValue zero = make_exact_value_integer(0);
ssaValue *left = ssa_make_value_constant(proc->module->allocator, tv->type, zero);
ssaValue *right = ssa_build_expression(proc, ue->operand);
ssaValue *right = ssa_build_expr(proc, ue->expr);
return ssa_emit_arith(proc, ue->op, left, right, tv->type);
} break;
case Token_Xor: { // Bitwise not
// NOTE(bill): "not" x == x "xor" -1
// NOTE(bill): "not" `x` == `x` "xor" `-1`
ExactValue neg_one = make_exact_value_integer(-1);
ssaValue *left = ssa_build_expression(proc, ue->operand);
ssaValue *left = ssa_build_expr(proc, ue->expr);
ssaValue *right = ssa_make_value_constant(proc->module->allocator, tv->type, neg_one);
return ssa_emit_arith(proc, ue->op, left, right, tv->type);
} break;
@@ -641,8 +655,8 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
}
} break;
case AstNode_BinaryExpression: {
auto *be = &expr->binary_expression;
case AstNode_BinaryExpr: {
auto *be = &expr->binary_expr;
switch (be->op.kind) {
case Token_Add:
case Token_Sub:
@@ -653,17 +667,17 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
case Token_Or:
case Token_Xor:
return ssa_emit_arith(proc, be->op,
ssa_build_expression(proc, be->left),
ssa_build_expression(proc, be->right),
ssa_build_expr(proc, be->left),
ssa_build_expr(proc, be->right),
tv->type);
case Token_AndNot: {
AstNode ue = {AstNode_UnaryExpression};
ue.unary_expression.op = be->op;
ue.unary_expression.op.kind = Token_Xor;
ue.unary_expression.operand = be->right;
ssaValue *left = ssa_build_expression(proc, be->left);
ssaValue *right = ssa_build_expression(proc, &ue);
AstNode ue = {AstNode_UnaryExpr};
ue.unary_expr.op = be->op;
ue.unary_expr.op.kind = Token_Xor;
ue.unary_expr.expr = be->right;
ssaValue *left = ssa_build_expr(proc, be->left);
ssaValue *right = ssa_build_expr(proc, &ue);
Token op = be->op;
op.kind = Token_And;
return ssa_emit_arith(proc, op, left, right, tv->type);
@@ -676,8 +690,8 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
case Token_Gt:
case Token_GtEq: {
ssaValue *cmp = ssa_emit_compare(proc, be->op,
ssa_build_expression(proc, be->left),
ssa_build_expression(proc, be->right));
ssa_build_expr(proc, be->left),
ssa_build_expr(proc, be->right));
return ssa_emit_conversion(proc, cmp, default_type(tv->type));
} break;
@@ -685,18 +699,43 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
GB_PANIC("Invalid binary expression");
}
} break;
case AstNode_ProcedureLiteral:
case AstNode_ProcLit:
break;
case AstNode_CastExpression:
case AstNode_CastExpr:
break;
case AstNode_CallExpression:
case AstNode_CallExpr:
break;
case AstNode_SliceExpression:
case AstNode_SliceExpr:
break;
case AstNode_IndexExpression: {
case AstNode_IndexExpr: {
auto *ie = &expr->index_expr;
Type *t = type_of_expr(proc->module->info, ie->expr);
t = get_base_type(t);
switch (t->kind) {
case Type_Basic: {
// TODO(bill): Strings AstNode_IndexExpression
} break;
case Type_Array: {
Type *t_int = &basic_types[Basic_int];
ssaValue *e = ssa_lvalue_address(ssa_build_address(proc, ie->expr), proc);
ssaValue *i0 = ssa_make_value_constant(proc->module->allocator, t_int, make_exact_value_integer(0));
ssaValue *i1 = ssa_emit_conversion(proc, ssa_build_expr(proc, ie->index), t_int);
ssaValue *gep = ssa_make_instruction_get_element_ptr(proc, e,
i0, i1, 2,
true);
ssa_value_set_type(gep, t->array.element);
return ssa_emit_load(proc, ssa_emit(proc, gep));
} break;
case Type_Slice:
break;
case Type_Pointer:
break;
}
} break;
case AstNode_SelectorExpression:
case AstNode_SelectorExpr:
break;
}
@@ -705,8 +744,8 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
}
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
expr = unparen_expression(expr);
ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) {
expr = unparen_expr(expr);
TypeAndValue *tv = map_get(&proc->module->info->types, hash_pointer(expr));
if (tv) {
@@ -723,7 +762,7 @@ ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
gb_printf("!Addressable!\n");
// TODO(bill): Addressing_Variable
} else {
value = ssa_build_single_expression(proc, expr, tv);
value = ssa_build_single_expr(proc, expr, tv);
}
return value;
@@ -734,9 +773,9 @@ ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr) {
switch (expr->kind) {
case AstNode_Identifier: {
case AstNode_Ident: {
if (!ssa_is_blank_identifier(expr)) {
Entity *e = entity_of_identifier(proc->module->info, expr);
Entity *e = entity_of_ident(proc->module->info, expr);
ssaLvalue val = {ssaLvalue_Address};
val.address.expr = expr;
@@ -748,22 +787,65 @@ ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr) {
}
} break;
case AstNode_ParenExpression:
return ssa_build_address(proc, unparen_expression(expr));
case AstNode_ParenExpr:
return ssa_build_address(proc, unparen_expr(expr));
/*
ssaLvalue addr = ssa_build_address(proc, expr->dereference_expr.operand);
ssaValue *load = ssa_lvalue_load(addr, proc);
ssaValue *deref = ssa_emit_load(proc, load);
ssa_value_set_type(deref, type_deref(ssa_value_type(deref)));
return deref;
*/
#if 1
case AstNode_DerefExpr: {
AstNode *operand = expr->deref_expr.expr;
ssaLvalue addr = ssa_build_address(proc, operand);
ssaValue *value = ssa_lvalue_load(addr, proc);
case AstNode_DereferenceExpression: {
ssaLvalue val = {ssaLvalue_Address};
AstNode *operand = expr->dereference_expression.operand;
val.address.value = ssa_build_expression(proc, operand);
val.address.value = value;
val.address.expr = expr;
return val;
} break;
#endif
case AstNode_SelectorExpression:
case AstNode_SelectorExpr:
break;
case AstNode_IndexExpression:
break;
case AstNode_IndexExpr: {
ssaValue *v = NULL;
Type *element_type = NULL;
auto *ie = &expr->index_expr;
Type *t = type_of_expr(proc->module->info, expr->index_expr.expr);
t = get_base_type(t);
switch (t->kind) {
case Type_Array: {
Type *t_int = &basic_types[Basic_int];
ssaValue *e = ssa_lvalue_address(ssa_build_address(proc, ie->expr), proc);
ssaValue *i0 = ssa_make_value_constant(proc->module->allocator, t_int, make_exact_value_integer(0));
ssaValue *i1 = ssa_emit_conversion(proc, ssa_build_expr(proc, ie->index), t_int);
ssaValue *gep = ssa_make_instruction_get_element_ptr(proc, e,
i0, i1, 2,
true);
element_type = t->array.element;
v = gep;
} break;
case Type_Pointer:
GB_PANIC("ssa_build_address AstNode_IndexExpression Type_Slice");
break;
case Type_Slice:
GB_PANIC("ssa_build_address AstNode_IndexExpression Type_Slice");
break;
}
ssa_value_set_type(v, element_type);
ssaLvalue val = {ssaLvalue_Address};
val.address.value = ssa_emit(proc, v);
val.address.expr = expr;
return val;
} break;
// TODO(bill): Others address
}
@@ -782,19 +864,19 @@ void ssa_build_assign_op(ssaProcedure *proc, ssaLvalue lhs, ssaValue *value, Tok
}
void ssa_build_statement(ssaProcedure *proc, AstNode *s);
void ssa_build_stmt(ssaProcedure *proc, AstNode *s);
void ssa_build_statement_list(ssaProcedure *proc, AstNode *list) {
void ssa_build_stmt_list(ssaProcedure *proc, AstNode *list) {
for (AstNode *stmt = list ; stmt != NULL; stmt = stmt->next)
ssa_build_statement(proc, stmt);
ssa_build_stmt(proc, stmt);
}
void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
void ssa_build_stmt(ssaProcedure *proc, AstNode *s) {
switch (s->kind) {
case AstNode_EmptyStatement:
case AstNode_EmptyStmt:
break;
case AstNode_VariableDeclaration: {
auto *vd = &s->variable_declaration;
case AstNode_VarDecl: {
auto *vd = &s->var_decl;
if (vd->kind == Declaration_Mutable) {
if (vd->name_count == vd->value_count) { // 1:1 assigment
gbArray(ssaLvalue) lvals;
@@ -815,7 +897,7 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
}
for (AstNode *value = vd->value_list; value != NULL; value = value->next) {
ssaValue *init = ssa_build_expression(proc, value);
ssaValue *init = ssa_build_expr(proc, value);
gb_array_append(inits, init);
}
@@ -837,22 +919,22 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
}
} break;
case AstNode_IncDecStatement: {
Token op = s->inc_dec_statement.op;
case AstNode_IncDecStmt: {
Token op = s->inc_dec_stmt.op;
if (op.kind == Token_Increment) {
op.kind = Token_Add;
} else if (op.kind == Token_Decrement) {
op.kind = Token_Sub;
}
ssaLvalue lval = ssa_build_address(proc, s->inc_dec_statement.expression);
ssaLvalue lval = ssa_build_address(proc, s->inc_dec_stmt.expr);
ssaValue *one = ssa_make_value_constant(proc->module->allocator, ssa_lvalue_type(lval),
make_exact_value_integer(1));
ssa_build_assign_op(proc, lval, one, op);
} break;
case AstNode_AssignStatement: {
auto *assign = &s->assign_statement;
case AstNode_AssignStmt: {
auto *assign = &s->assign_stmt;
switch (assign->op.kind) {
case Token_Eq: {
gbArray(ssaLvalue) lvals;
@@ -873,7 +955,7 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
if (assign->lhs_count == 1) {
AstNode *lhs = assign->lhs_list;
AstNode *rhs = assign->rhs_list;
ssaValue *init = ssa_build_expression(proc, rhs);
ssaValue *init = ssa_build_expr(proc, rhs);
ssa_lvalue_store(lvals[0], proc, init);
} else {
gbArray(ssaValue *) inits;
@@ -881,7 +963,7 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
defer (gb_array_free(inits));
for (AstNode *rhs = assign->rhs_list; rhs != NULL; rhs = rhs->next) {
ssaValue *init = ssa_build_expression(proc, rhs);
ssaValue *init = ssa_build_expr(proc, rhs);
gb_array_append(inits, init);
}
@@ -903,33 +985,33 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
kind += Token_Add - Token_AddEq; // Convert += to +
op.kind = cast(TokenKind)kind;
ssaLvalue lhs = ssa_build_address(proc, assign->lhs_list);
ssaValue *value = ssa_build_expression(proc, assign->rhs_list);
ssaValue *value = ssa_build_expr(proc, assign->rhs_list);
ssa_build_assign_op(proc, lhs, value, op);
} break;
}
} break;
case AstNode_ExpressionStatement:
ssa_build_expression(proc, s->expression_statement.expression);
case AstNode_ExprStmt:
ssa_build_expr(proc, s->expr_stmt.expr);
break;
case AstNode_BlockStatement:
ssa_build_statement_list(proc, s->block_statement.list);
case AstNode_BlockStmt:
ssa_build_stmt_list(proc, s->block_stmt.list);
break;
case AstNode_IfStatement:
case AstNode_IfStmt:
GB_PANIC("AstNode_IfStatement");
break;
case AstNode_ReturnStatement:
case AstNode_ReturnStmt:
GB_PANIC("AstNode_ReturnStatement");
break;
case AstNode_ForStatement:
case AstNode_ForStmt:
GB_PANIC("AstNode_ForStatement");
break;
case AstNode_DeferStatement:
case AstNode_DeferStmt:
GB_PANIC("AstNode_DeferStatement");
break;
case AstNode_BranchStatement:
case AstNode_BranchStmt:
GB_PANIC("AstNode_BranchStatement");
break;
}
@@ -945,9 +1027,9 @@ void ssa_build_procedure(ssaValue *value) {
AstNode *proc_decl = proc->decl->proc_decl;
switch (proc_decl->kind) {
case AstNode_ProcedureDeclaration:
proc->type_expr = proc_decl->procedure_declaration.type;
proc->body = proc_decl->procedure_declaration.body;
case AstNode_ProcDecl:
proc->type_expr = proc_decl->proc_decl.type;
proc->body = proc_decl->proc_decl.body;
break;
default:
return;
@@ -960,7 +1042,7 @@ void ssa_build_procedure(ssaValue *value) {
ssa_begin_procedure_body(proc);
ssa_build_statement(proc, proc->body);
ssa_build_stmt(proc, proc->body);
ssa_end_procedure_body(proc);
}

File diff suppressed because it is too large Load Diff

View File

@@ -10,190 +10,190 @@ void print_ast(AstNode *node, isize indent) {
return;
switch (node->kind) {
case AstNode_BasicLiteral:
case AstNode_BasicLit:
print_indent(indent);
print_token(node->basic_literal);
print_token(node->basic_lit);
break;
case AstNode_Identifier:
case AstNode_Ident:
print_indent(indent);
print_token(node->identifier.token);
print_token(node->ident.token);
break;
case AstNode_ProcedureLiteral:
case AstNode_ProcLit:
print_indent(indent);
gb_printf("(proc lit)\n");
print_ast(node->procedure_literal.type, indent+1);
print_ast(node->procedure_literal.body, indent+1);
print_ast(node->proc_lit.type, indent+1);
print_ast(node->proc_lit.body, indent+1);
break;
case AstNode_CompoundLiteral:
case AstNode_CompoundLit:
print_indent(indent);
gb_printf("(compound lit)\n");
print_ast(node->compound_literal.type_expression, indent+1);
print_ast(node->compound_literal.element_list, indent+1);
print_ast(node->compound_lit.type, indent+1);
print_ast(node->compound_lit.elem_list, indent+1);
break;
case AstNode_TagExpression:
case AstNode_TagExpr:
print_indent(indent);
gb_printf("(tag)\n");
print_indent(indent+1);
print_token(node->tag_expression.name);
print_ast(node->tag_expression.expression, indent+1);
print_token(node->tag_expr.name);
print_ast(node->tag_expr.expr, indent+1);
break;
case AstNode_UnaryExpression:
case AstNode_UnaryExpr:
print_indent(indent);
print_token(node->unary_expression.op);
print_ast(node->unary_expression.operand, indent+1);
print_token(node->unary_expr.op);
print_ast(node->unary_expr.expr, indent+1);
break;
case AstNode_BinaryExpression:
case AstNode_BinaryExpr:
print_indent(indent);
print_token(node->binary_expression.op);
print_ast(node->binary_expression.left, indent+1);
print_ast(node->binary_expression.right, indent+1);
print_token(node->binary_expr.op);
print_ast(node->binary_expr.left, indent+1);
print_ast(node->binary_expr.right, indent+1);
break;
case AstNode_CallExpression:
case AstNode_CallExpr:
print_indent(indent);
gb_printf("(call)\n");
print_ast(node->call_expression.proc, indent+1);
print_ast(node->call_expression.arg_list, indent+1);
print_ast(node->call_expr.proc, indent+1);
print_ast(node->call_expr.arg_list, indent+1);
break;
case AstNode_SelectorExpression:
case AstNode_SelectorExpr:
print_indent(indent);
gb_printf(".\n");
print_ast(node->selector_expression.operand, indent+1);
print_ast(node->selector_expression.selector, indent+1);
print_ast(node->selector_expr.expr, indent+1);
print_ast(node->selector_expr.selector, indent+1);
break;
case AstNode_IndexExpression:
case AstNode_IndexExpr:
print_indent(indent);
gb_printf("([])\n");
print_ast(node->index_expression.expression, indent+1);
print_ast(node->index_expression.value, indent+1);
print_ast(node->index_expr.expr, indent+1);
print_ast(node->index_expr.index, indent+1);
break;
case AstNode_CastExpression:
case AstNode_CastExpr:
print_indent(indent);
gb_printf("(cast)\n");
print_ast(node->cast_expression.type_expression, indent+1);
print_ast(node->cast_expression.operand, indent+1);
print_ast(node->cast_expr.type, indent+1);
print_ast(node->cast_expr.expr, indent+1);
break;
case AstNode_DereferenceExpression:
case AstNode_DerefExpr:
print_indent(indent);
gb_printf("(dereference)\n");
print_ast(node->dereference_expression.operand, indent+1);
gb_printf("(deref)\n");
print_ast(node->deref_expr.expr, indent+1);
break;
case AstNode_ExpressionStatement:
print_ast(node->expression_statement.expression, indent);
case AstNode_ExprStmt:
print_ast(node->expr_stmt.expr, indent);
break;
case AstNode_IncDecStatement:
case AstNode_IncDecStmt:
print_indent(indent);
print_token(node->inc_dec_statement.op);
print_ast(node->inc_dec_statement.expression, indent+1);
print_token(node->inc_dec_stmt.op);
print_ast(node->inc_dec_stmt.expr, indent+1);
break;
case AstNode_AssignStatement:
case AstNode_AssignStmt:
print_indent(indent);
print_token(node->assign_statement.op);
print_ast(node->assign_statement.lhs_list, indent+1);
print_ast(node->assign_statement.rhs_list, indent+1);
print_token(node->assign_stmt.op);
print_ast(node->assign_stmt.lhs_list, indent+1);
print_ast(node->assign_stmt.rhs_list, indent+1);
break;
case AstNode_BlockStatement:
case AstNode_BlockStmt:
print_indent(indent);
gb_printf("(block)\n");
print_ast(node->block_statement.list, indent+1);
print_ast(node->block_stmt.list, indent+1);
break;
case AstNode_IfStatement:
case AstNode_IfStmt:
print_indent(indent);
gb_printf("(if)\n");
print_ast(node->if_statement.cond, indent+1);
print_ast(node->if_statement.body, indent+1);
if (node->if_statement.else_statement) {
print_ast(node->if_stmt.cond, indent+1);
print_ast(node->if_stmt.body, indent+1);
if (node->if_stmt.else_stmt) {
print_indent(indent);
gb_printf("(else)\n");
print_ast(node->if_statement.else_statement, indent+1);
print_ast(node->if_stmt.else_stmt, indent+1);
}
break;
case AstNode_ReturnStatement:
case AstNode_ReturnStmt:
print_indent(indent);
gb_printf("(return)\n");
print_ast(node->return_statement.results, indent+1);
print_ast(node->return_stmt.result_list, indent+1);
break;
case AstNode_ForStatement:
case AstNode_ForStmt:
print_indent(indent);
gb_printf("(for)\n");
print_ast(node->for_statement.init, indent+1);
print_ast(node->for_statement.cond, indent+1);
print_ast(node->for_statement.end, indent+1);
print_ast(node->for_statement.body, indent+1);
print_ast(node->for_stmt.init, indent+1);
print_ast(node->for_stmt.cond, indent+1);
print_ast(node->for_stmt.end, indent+1);
print_ast(node->for_stmt.body, indent+1);
break;
case AstNode_DeferStatement:
case AstNode_DeferStmt:
print_indent(indent);
gb_printf("(defer)\n");
print_ast(node->defer_statement.statement, indent+1);
print_ast(node->defer_stmt.stmt, indent+1);
break;
case AstNode_VariableDeclaration:
case AstNode_VarDecl:
print_indent(indent);
if (node->variable_declaration.kind == Declaration_Mutable)
if (node->var_decl.kind == Declaration_Mutable)
gb_printf("(decl:var,mutable)\n");
else if (node->variable_declaration.kind == Declaration_Immutable)
else if (node->var_decl.kind == Declaration_Immutable)
gb_printf("(decl:var,immutable)\n");
print_ast(node->variable_declaration.name_list, indent+1);
print_ast(node->variable_declaration.type_expression, indent+1);
print_ast(node->variable_declaration.value_list, indent+1);
print_ast(node->var_decl.name_list, indent+1);
print_ast(node->var_decl.type, indent+1);
print_ast(node->var_decl.value_list, indent+1);
break;
case AstNode_ProcedureDeclaration:
case AstNode_ProcDecl:
print_indent(indent);
if (node->procedure_declaration.kind == Declaration_Mutable)
if (node->proc_decl.kind == Declaration_Mutable)
gb_printf("(decl:proc,mutable)\n");
else if (node->procedure_declaration.kind == Declaration_Immutable)
else if (node->proc_decl.kind == Declaration_Immutable)
gb_printf("(decl:proc,immutable)\n");
print_ast(node->procedure_declaration.type, indent+1);
print_ast(node->procedure_declaration.body, indent+1);
print_ast(node->procedure_declaration.tag_list, indent+1);
print_ast(node->proc_decl.type, indent+1);
print_ast(node->proc_decl.body, indent+1);
print_ast(node->proc_decl.tag_list, indent+1);
break;
case AstNode_TypeDeclaration:
case AstNode_TypeDecl:
print_indent(indent);
gb_printf("(type)\n");
print_ast(node->type_declaration.name, indent+1);
print_ast(node->type_declaration.type_expression, indent+1);
print_ast(node->type_decl.name, indent+1);
print_ast(node->type_decl.type, indent+1);
break;
case AstNode_AliasDeclaration:
case AstNode_AliasDecl:
print_indent(indent);
gb_printf("(alias)\n");
print_ast(node->alias_declaration.name, indent+1);
print_ast(node->alias_declaration.type_expression, indent+1);
print_ast(node->alias_decl.name, indent+1);
print_ast(node->alias_decl.type, indent+1);
break;
case AstNode_ProcedureType:
case AstNode_ProcType:
print_indent(indent);
gb_printf("(type:proc)(%td -> %td)\n", node->procedure_type.param_count, node->procedure_type.result_count);
print_ast(node->procedure_type.param_list, indent+1);
if (node->procedure_type.result_list) {
gb_printf("(type:proc)(%td -> %td)\n", node->proc_type.param_count, node->proc_type.result_count);
print_ast(node->proc_type.param_list, indent+1);
if (node->proc_type.result_list) {
print_indent(indent+1);
gb_printf("->\n");
print_ast(node->procedure_type.result_list, indent+1);
print_ast(node->proc_type.result_list, indent+1);
}
break;
case AstNode_Field:
print_ast(node->field.name_list, indent);
print_ast(node->field.type_expression, indent);
print_ast(node->field.type, indent);
break;
case AstNode_PointerType:
print_indent(indent);
print_token(node->pointer_type.token);
print_ast(node->pointer_type.type_expression, indent+1);
print_ast(node->pointer_type.type, indent+1);
break;
case AstNode_ArrayType:
print_indent(indent);
gb_printf("[]\n");
print_ast(node->array_type.count, indent+1);
print_ast(node->array_type.element, indent+1);
print_ast(node->array_type.elem, indent+1);
break;
case AstNode_StructType:
print_indent(indent);