|
|
|
|
@@ -49,9 +49,8 @@ string_scope_name(Allocator *a, Ast_Scope *scope) {
|
|
|
|
|
|
|
|
|
|
CORE_Static void
|
|
|
|
|
gen_scope_name(Ast_Scope *scope) {
|
|
|
|
|
Arena *scratch = pctx->scratch;
|
|
|
|
|
Scratch_Scope _scope(scratch);
|
|
|
|
|
String string = string_scope_name(scratch, scope);
|
|
|
|
|
Scratch_Scope scratch(pctx->scratch);
|
|
|
|
|
String string = string_scope_name(scratch.arena, scope);
|
|
|
|
|
gen("%.*s", (int)string.len, string.str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -99,20 +98,21 @@ get_ctype_name_for_type(Ast_Type *type) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CORE_Static String
|
|
|
|
|
string_simple_decl_prefix(Allocator *a, Ast_Type *ast) {
|
|
|
|
|
string_simple_decl_prefix(Ast_Type *ast) {
|
|
|
|
|
Allocator *a = pctx->stage_arena;
|
|
|
|
|
switch (ast->kind) {
|
|
|
|
|
case TYPE_POINTER: {
|
|
|
|
|
String string = string_simple_decl_prefix(a, ast->base);
|
|
|
|
|
String string = string_simple_decl_prefix(ast->base);
|
|
|
|
|
string = string_fmt(a, "%Q*", string);
|
|
|
|
|
return string;
|
|
|
|
|
} break;
|
|
|
|
|
case TYPE_LAMBDA: return {}; break;
|
|
|
|
|
case TYPE_ENUM:
|
|
|
|
|
case TYPE_ARRAY: {
|
|
|
|
|
return string_simple_decl_prefix(a, ast->base);
|
|
|
|
|
return string_simple_decl_prefix(ast->base);
|
|
|
|
|
} break;
|
|
|
|
|
case TYPE_SLICE: {
|
|
|
|
|
String string = string_simple_decl_prefix(a, ast->base);
|
|
|
|
|
String string = string_simple_decl_prefix(ast->base);
|
|
|
|
|
string = string_fmt(a, "Slice%llu ", ast->type_id);
|
|
|
|
|
return string;
|
|
|
|
|
} break;
|
|
|
|
|
@@ -136,13 +136,14 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CORE_Static String
|
|
|
|
|
string_simple_decl_postfix(Allocator *a, Ast_Type *ast) {
|
|
|
|
|
string_simple_decl_postfix(Ast_Type *ast) {
|
|
|
|
|
Allocator *a = pctx->stage_arena;
|
|
|
|
|
switch (ast->kind) {
|
|
|
|
|
case TYPE_POINTER:
|
|
|
|
|
return string_simple_decl_postfix(a, ast->base);
|
|
|
|
|
return string_simple_decl_postfix(ast->base);
|
|
|
|
|
break;
|
|
|
|
|
case TYPE_ARRAY: {
|
|
|
|
|
String result = string_simple_decl_postfix(a, ast->arr.base);
|
|
|
|
|
String result = string_simple_decl_postfix(ast->arr.base);
|
|
|
|
|
String string = string_fmt(a, "[%d]%Q", ast->arr.size, result);
|
|
|
|
|
return string;
|
|
|
|
|
} break;
|
|
|
|
|
@@ -158,12 +159,13 @@ string_simple_decl_postfix(Allocator *a, Ast_Type *ast) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CORE_Static String
|
|
|
|
|
string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}) {
|
|
|
|
|
string_simple_decl(Ast_Type *ast, Intern_String name = {}) {
|
|
|
|
|
Allocator *a = pctx->stage_arena;
|
|
|
|
|
if (ast->kind == TYPE_LAMBDA) {
|
|
|
|
|
String prefix = string_simple_decl_prefix(a, ast->func.ret);
|
|
|
|
|
String prefix = string_simple_decl_prefix(ast->func.ret);
|
|
|
|
|
String string = string_fmt(a, "%Q(*%Q)(", prefix, name);
|
|
|
|
|
For(ast->func.args) {
|
|
|
|
|
String prefix_arg = string_simple_decl_prefix(a, it);
|
|
|
|
|
String prefix_arg = string_simple_decl_prefix(it);
|
|
|
|
|
string = string_fmt(a, "%Q%Q", string, prefix_arg);
|
|
|
|
|
if (&it != ast->func.args.end() - 1)
|
|
|
|
|
string = string_fmt(a, "%Q, ", string);
|
|
|
|
|
@@ -172,11 +174,11 @@ string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}) {
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
String string = string_simple_decl_prefix(a, ast);
|
|
|
|
|
String string = string_simple_decl_prefix(ast);
|
|
|
|
|
if (name.len) {
|
|
|
|
|
string = string_fmt(a, "%Q%Q", string, name);
|
|
|
|
|
}
|
|
|
|
|
String postfix = string_simple_decl_postfix(a, ast);
|
|
|
|
|
String postfix = string_simple_decl_postfix(ast);
|
|
|
|
|
string = string_fmt(a, "%Q%Q", string, postfix);
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
@@ -184,11 +186,8 @@ string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}) {
|
|
|
|
|
|
|
|
|
|
CORE_Static void
|
|
|
|
|
gen_simple_decl(Ast_Type *ast, Intern_String name = {}) {
|
|
|
|
|
Arena *scratch = pctx->scratch;
|
|
|
|
|
Scratch_Scope _scope(scratch);
|
|
|
|
|
|
|
|
|
|
String string = string_simple_decl(scratch, ast, name);
|
|
|
|
|
gen("%.*s", (int)string.len, string.str);
|
|
|
|
|
String string = string_simple_decl(ast, name);
|
|
|
|
|
gen("%Q", string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CORE_Static String
|
|
|
|
|
@@ -226,11 +225,9 @@ gen_value(Token *pos, Value a) {
|
|
|
|
|
|
|
|
|
|
switch (type->kind) {
|
|
|
|
|
CASE_INT : {
|
|
|
|
|
Arena *scratch = pctx->scratch;
|
|
|
|
|
Scratch_Scope _scope(scratch);
|
|
|
|
|
|
|
|
|
|
Scratch_Scope scratch(pctx->scratch);
|
|
|
|
|
String postfix = get_type_postfix(type);
|
|
|
|
|
const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10);
|
|
|
|
|
const char *string = bigint_to_error_string(scratch.arena, &a.big_int_val, 10);
|
|
|
|
|
gen("%s%Q", string, postfix);
|
|
|
|
|
} break;
|
|
|
|
|
case TYPE_POINTER: {
|
|
|
|
|
@@ -569,8 +566,7 @@ gen_ast(Ast *ast) {
|
|
|
|
|
|
|
|
|
|
CASE(RETURN, Return) {
|
|
|
|
|
if (is_tuple(node->resolved_type)) {
|
|
|
|
|
Arena *scratch = pctx->scratch;
|
|
|
|
|
Scratch_Scope _scope(scratch);
|
|
|
|
|
Scratch_Scope scratch(pctx->scratch);
|
|
|
|
|
|
|
|
|
|
Intern_String tuple_name = get_unique_name(node);
|
|
|
|
|
gen_simple_decl(node->resolved_type, tuple_name);
|
|
|
|
|
@@ -808,8 +804,7 @@ gen_ast(Ast *ast) {
|
|
|
|
|
For(node->vars)
|
|
|
|
|
gen_ast(it);
|
|
|
|
|
|
|
|
|
|
Arena *scratch = pctx->scratch;
|
|
|
|
|
Scratch_Scope _scope(scratch);
|
|
|
|
|
Scratch_Scope scratch(pctx->scratch);
|
|
|
|
|
|
|
|
|
|
Intern_String var_name = get_unique_name(node);
|
|
|
|
|
gen_simple_decl(node->resolved_type, var_name);
|
|
|
|
|
@@ -905,8 +900,7 @@ compile_to_c_code() {
|
|
|
|
|
|
|
|
|
|
// Generate slice and tuple types
|
|
|
|
|
For_Named(pctx->all_types, type) {
|
|
|
|
|
Arena *scratch = pctx->scratch;
|
|
|
|
|
Scratch_Scope _scope(scratch);
|
|
|
|
|
Scratch_Scope scratch(pctx->scratch);
|
|
|
|
|
|
|
|
|
|
if (type->kind == TYPE_SLICE) {
|
|
|
|
|
genln("typedef struct Slice%llu{", type->type_id);
|
|
|
|
|
@@ -925,7 +919,7 @@ compile_to_c_code() {
|
|
|
|
|
For(type->agg.members) {
|
|
|
|
|
genln("");
|
|
|
|
|
// @todo remove intern from gen
|
|
|
|
|
Intern_String name = pctx->intern(string_fmt(scratch, "m%llu", type->agg.members.get_index(&it)));
|
|
|
|
|
Intern_String name = pctx->intern(string_fmt(scratch.arena, "m%llu", type->agg.members.get_index(&it)));
|
|
|
|
|
gen_simple_decl(it.type, name);
|
|
|
|
|
gen(";");
|
|
|
|
|
}
|
|
|
|
|
|