Started work on cleaning up dumping the AST. Lots still to do but... Split try expr further and fixed emit of statement.

This commit is contained in:
Christoffer Lerno
2020-05-06 22:23:53 +02:00
parent f62a094f75
commit 89ce6064e1
14 changed files with 340 additions and 257 deletions

View File

@@ -304,6 +304,9 @@ static void parse_option()
void parse_arguments(int argc, const char *argv[]) void parse_arguments(int argc, const char *argv[])
{ {
arg_count = argc;
args = argv;
if (argc < 2) if (argc < 2)
{ {
usage(); usage();
@@ -333,8 +336,6 @@ void parse_arguments(int argc, const char *argv[])
build_options.severity[i] = DIAG_ERROR; build_options.severity[i] = DIAG_ERROR;
} }
arg_count = argc;
args = argv;
for (arg_index = 1; arg_index < arg_count; arg_index++) for (arg_index = 1; arg_index < arg_count; arg_index++)
{ {
current_arg = args[arg_index]; current_arg = args[arg_index];

View File

@@ -5,6 +5,20 @@
#include "compiler_internal.h" #include "compiler_internal.h"
static void fprint_asts_recursive(FILE *file, Ast **asts, int indent); static void fprint_asts_recursive(FILE *file, Ast **asts, int indent);
static void fprint_decl_list(FILE *file, Decl **decls, int indent);
static void fprint_ast_recursive(FILE *file, Ast *ast, int indent);
#define DUMP(text) do { fprintf_indented(file, indent, text); fprintf(file, "\n"); } while(0)
#define DUMPF(text, ...) do { fprintf_indented(file, indent, text, __VA_ARGS__); fprintf(file, "\n"); } while(0)
#define DUMPI(text) do { fprintf_indented(file, indent + 1, text); fprintf(file, "\n"); } while(0)
#define DUMPFI(text, ...) do { fprintf_indented(file, indent + 1, text, __VA_ARGS__); fprintf(file, "\n"); } while(0)
#define DUMPE() fprint_endparen(file, indent);
#define DUMPEND() fprint_endparen(file, indent); return
#define DUMPEXPR(_expr) fprint_expr_recursive(file, _expr, indent + 1)
#define DUMPAST(_ast) fprint_ast_recursive(file, _ast, indent + 1)
#define DUMPTI(_type_info) fprint_type_info_recursive(file, _type_info, indent + 1)
#define DUMPTYPE(_type) fprint_type_recursive(file, _type, indent + 1)
#define DUMPDECLS(_decls) fprint_decl_list(file, _decls, indent + 1)
Decl *decl_new(DeclKind decl_kind, Token name, Visibility visibility) Decl *decl_new(DeclKind decl_kind, Token name, Visibility visibility)
{ {
@@ -341,10 +355,8 @@ void fprint_type_recursive(FILE *file, Type *type, int indent)
case TYPE_POISONED: case TYPE_POISONED:
fprintf_indented(file, indent, "(type poison)\n"); fprintf_indented(file, indent, "(type poison)\n");
return; return;
case TYPE_META_TYPE: case TYPE_TYPEID:
fprintf_indented(file, indent, "(meta-type"); DUMP("(typeid)");
fprint_type_recursive(file, type->child, indent + 1);
fprint_endparen(file, indent);
return; return;
case TYPE_FUNC: case TYPE_FUNC:
fprintf_indented(file, indent, "(type-func %s)\n", type->func.signature->mangled_signature); fprintf_indented(file, indent, "(type-func %s)\n", type->func.signature->mangled_signature);
@@ -362,14 +374,9 @@ void fprint_type_recursive(FILE *file, Type *type, int indent)
fprintf_indented(file, indent, "(error %s::%s)\n", type->decl->module->name, type->decl->name); fprintf_indented(file, indent, "(error %s::%s)\n", type->decl->module->name, type->decl->name);
return; return;
case TYPE_TYPEDEF: case TYPE_TYPEDEF:
if (type->canonical != type) DUMPF("(user-defined %s", type->name);
{ DUMPTYPE(type->canonical);
fprintf_indented(file, indent, "(user-defined %s::%s\n", type->decl->module->name, type->decl->name); DUMPEND();
fprint_type_recursive(file, type->canonical, indent + 1);
fprint_endparen(file, indent);
break;
}
break;
case TYPE_POINTER: case TYPE_POINTER:
fprintf_indented(file, indent, "(pointer\n"); fprintf_indented(file, indent, "(pointer\n");
fprint_type_recursive(file, type->pointer, indent + 1); fprint_type_recursive(file, type->pointer, indent + 1);
@@ -411,10 +418,11 @@ void fprint_type_recursive(FILE *file, Type *type, int indent)
fprintf_indented(file, indent, "(comp time float)\n"); fprintf_indented(file, indent, "(comp time float)\n");
break; break;
case TYPE_STRING: case TYPE_STRING:
fprintf_indented(file, indent, "(string)\n"); DUMP("(string)");
break; return;
case TYPE_ERROR_UNION: case TYPE_ERROR_UNION:
TODO DUMP("(error-union)");
return;
} }
} }
@@ -433,57 +441,58 @@ const char *resolve_status_to_string(ResolveStatus status)
} }
} }
void fprint_type_info_recursive(FILE *file, TypeInfo *type_info, int indent) void fprint_type_info_recursive(FILE *file, TypeInfo *type_info, int indent)
{ {
if (!type_info) if (!type_info)
{ {
fprintf_indented(file, indent, "(type_info missing)\n"); DUMP("(type_info missing)");
return; return;
} }
fprintf_indented(file, indent, "(type_info\n"); DUMP("(type_info");
fprintf_indented(file, indent + 1, "(resolve_status %s)\n", resolve_status_to_string(type_info->resolve_status)); DUMPFI("(resolve_status %s)", resolve_status_to_string(type_info->resolve_status));
if (type_info->resolve_status == RESOLVE_DONE) if (type_info->resolve_status == RESOLVE_DONE)
{ {
fprint_type_recursive(file, type_info->type, indent + 1); DUMPTYPE(type_info->type);
fprint_endparen(file, indent); DUMPEND();
return;
} }
indent++;
switch (type_info->kind) switch (type_info->kind)
{ {
case TYPE_INFO_POISON: case TYPE_INFO_POISON:
fprintf_indented(file, indent + 1, "(POISON)\n"); DUMP("(POISON)");
break; break;
case TYPE_INFO_IDENTIFIER: case TYPE_INFO_IDENTIFIER:
if (type_info->unresolved.path) if (type_info->unresolved.path)
{ {
fprintf_indented(file, indent + 1, "(unresolved %s::%s)\n", type_info->unresolved.path->module, type_info->unresolved.name_loc.string); DUMPF("(unresolved %s::%s)\n", type_info->unresolved.path->module, type_info->unresolved.name_loc.string);
return; break;;
} }
fprintf_indented(file, indent + 1, "(unresolved %s)\n", type_info->unresolved.name_loc.string); DUMPF("(unresolved %s)", type_info->unresolved.name_loc.string);
break; break;
case TYPE_INFO_ARRAY: case TYPE_INFO_ARRAY:
fprintf_indented(file, indent + 1, "(unresolved-array\n"); DUMP("(unresolved-array");
fprint_type_info_recursive(file, type_info->array.base, indent + 2); DUMPTI(type_info->array.base);
if (type_info->array.len) fprint_expr_recursive(file, type_info->array.len, indent + 1); DUMPEXPR(type_info->array.len);
fprint_endparen(file, indent + 1); DUMPE();
break; break;
case TYPE_INFO_POINTER: case TYPE_INFO_POINTER:
fprintf_indented(file, indent + 1, "(pointer\n"); DUMP("pointer");
fprint_type_info_recursive(file, type_info->pointer, indent + 2); DUMPTI(type_info->pointer);
fprint_endparen(file, indent + 1); DUMPE();
break; break;
case TYPE_INFO_INC_ARRAY: case TYPE_INFO_INC_ARRAY:
fprintf_indented(file, indent + 1, "(incarray\n"); DUMP("(incarray");
fprint_type_info_recursive(file, type_info->array.base, indent + 2); DUMPTI(type_info->array.base);
fprint_endparen(file, indent + 1); DUMPE();
break;
case TYPE_INFO_EXPRESSION: case TYPE_INFO_EXPRESSION:
fprintf_indented(file, indent + 1, "(typexpr\n"); DUMP("(typexpr");
fprint_expr_recursive(file, type_info->unresolved_type_expr, indent + 2); DUMPEXPR(type_info->unresolved_type_expr);
fprint_endparen(file, indent + 1); DUMPE();
break; break;
} }
fprint_endparen(file, indent); indent--;
DUMPEND();
} }
void fprint_expr_common(FILE *file, Expr *expr, int indent) void fprint_expr_common(FILE *file, Expr *expr, int indent)
@@ -491,10 +500,10 @@ void fprint_expr_common(FILE *file, Expr *expr, int indent)
switch (expr->resolve_status) switch (expr->resolve_status)
{ {
case RESOLVE_NOT_DONE: case RESOLVE_NOT_DONE:
fprintf_indented(file, indent, "(unresolved)\n"); DUMP("(unresolved)");
break; break;
case RESOLVE_RUNNING: case RESOLVE_RUNNING:
fprintf_indented(file, indent, "(resolving)\n"); DUMP("(resolving)");
break; break;
case RESOLVE_DONE: case RESOLVE_DONE:
fprint_type_recursive(file, expr->type, indent); fprint_type_recursive(file, expr->type, indent);
@@ -502,98 +511,98 @@ void fprint_expr_common(FILE *file, Expr *expr, int indent)
} }
} }
#define DUMPEXPC(_expr) fprint_expr_common(file, _expr, indent + 1)
void fprint_expr_recursive(FILE *file, Expr *expr, int indent) void fprint_expr_recursive(FILE *file, Expr *expr, int indent)
{ {
if (!expr) return;
switch (expr->expr_kind) switch (expr->expr_kind)
{ {
case EXPR_IDENTIFIER: case EXPR_IDENTIFIER:
fprintf_indented(file, indent, "(ident %s\n", expr->identifier_expr.identifier); DUMPF("(ident %s", expr->identifier_expr.identifier);
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
break; DUMPEND();
case EXPR_EXPR_BLOCK: case EXPR_EXPR_BLOCK:
if (!expr->expr_block.stmts) if (!expr->expr_block.stmts)
{ {
fprintf(file, "(expr_block)\n"); DUMP("(expr_block)");
return; return;
} }
fprintf(file, "(expr_block\n"); DUMP("(expr_block");
{ fprint_asts_recursive(file, expr->expr_block.stmts, indent + 1);
fprint_asts_recursive(file, expr->expr_block.stmts, indent + 1); DUMPEND();
}
break;
case EXPR_CONST: case EXPR_CONST:
fprintf_indented(file, indent, "(const "); fprintf_indented(file, indent, "(const ");
expr_const_fprint(file, &expr->const_expr); expr_const_fprint(file, &expr->const_expr);
fprintf(file, "\n"); fprintf(file, "\n");
fprint_expr_common(file, expr, indent + 1); fprint_expr_common(file, expr, indent + 1);
break; DUMPEND();
case EXPR_BINARY: case EXPR_BINARY:
fprintf_indented(file, indent, "(binary %s\n", token_type_to_string(binaryop_to_token(expr->binary_expr.operator))); DUMPF("(binary %s", token_type_to_string(binaryop_to_token(expr->binary_expr.operator)));
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_expr_recursive(file, expr->binary_expr.left, indent + 1); fprint_expr_recursive(file, expr->binary_expr.left, indent + 1);
fprint_expr_recursive(file, expr->binary_expr.right, indent + 1); fprint_expr_recursive(file, expr->binary_expr.right, indent + 1);
break; DUMPEND();
case EXPR_UNARY: case EXPR_UNARY:
fprintf_indented(file, indent, "(unary %s\n", token_type_to_string(unaryop_to_token(expr->unary_expr.operator))); DUMPF("(unary %s", token_type_to_string(unaryop_to_token(expr->unary_expr.operator)));
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_expr_recursive(file, expr->unary_expr.expr, indent + 1); DUMPEXPR(expr->unary_expr.expr);
break; DUMPEND();
case EXPR_POST_UNARY: case EXPR_POST_UNARY:
fprintf_indented(file, indent, "(postunary %s\n", token_type_to_string(postunaryop_to_token(expr->post_expr.operator))); DUMPF("(postunary %s", token_type_to_string(postunaryop_to_token(expr->post_expr.operator)));
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_expr_recursive(file, expr->post_expr.expr, indent + 1); DUMPEXPR(expr->post_expr.expr);
break; DUMPEND();
case EXPR_TYPE_ACCESS: case EXPR_TYPE_ACCESS:
fprintf_indented(file, indent, "(typeaccess .%s\n", expr->type_access.name.string); DUMPF("(typeaccess .%s", expr->type_access.name.string);
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_type_info_recursive(file, expr->type_access.type, indent + 1); DUMPTI(expr->type_access.type);
break; DUMPEND();
case EXPR_ACCESS: case EXPR_ACCESS:
fprintf_indented(file, indent, "(access .%s\n", expr->access_expr.sub_element.string); DUMPF("(access .%s", expr->access_expr.sub_element.string);
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_expr_recursive(file, expr->access_expr.parent, indent + 1); DUMPEXPR(expr->access_expr.parent);
break; DUMPEND();
case EXPR_TYPEID: case EXPR_TYPEID:
fprintf_indented(file, indent, "(typeid \n"); DUMP("(typeid");
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_type_info_recursive(file, expr->typeid_expr, indent + 1); DUMPTI(expr->typeid_expr);
break; DUMPEND();
case EXPR_GROUP: case EXPR_GROUP:
fprintf_indented(file, indent, "(group\n"); DUMP("(group");
fprint_expr_recursive(file, expr->group_expr, indent + 1); DUMPEXPR(expr->group_expr);
break; DUMPEND();
case EXPR_CALL: case EXPR_CALL:
fprintf_indented(file, indent, "(call\n"); DUMP("(call");
fprint_expr_common(file, expr, indent + 1); fprint_expr_common(file, expr, indent + 1);
fprint_expr_recursive(file, expr->call_expr.function, indent + 1); fprint_expr_recursive(file, expr->call_expr.function, indent + 1);
VECEACH(expr->call_expr.arguments, i)
{ {
VECEACH(expr->call_expr.arguments, i) fprint_expr_recursive(file, expr->call_expr.arguments[i], indent + 1);
{
fprint_expr_recursive(file, expr->call_expr.arguments[i], indent + 1);
}
} }
break; DUMPEND();
case EXPR_TERNARY: case EXPR_TERNARY:
if (!expr->ternary_expr.then_expr) if (!expr->ternary_expr.then_expr)
{ {
fprintf_indented(file, indent, "(elvis\n"); DUMP("(elvis");
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_expr_recursive(file, expr->ternary_expr.cond, indent + 1); DUMPEXPR(expr->ternary_expr.cond);
} }
else else
{ {
fprintf_indented(file, indent, "(cond\n"); DUMP("(cond");
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_expr_recursive(file, expr->ternary_expr.cond, indent + 1); DUMPEXPR(expr->ternary_expr.cond);
fprint_expr_recursive(file, expr->ternary_expr.then_expr, indent + 1); DUMPEXPR(expr->ternary_expr.then_expr);
} }
fprint_expr_recursive(file, expr->ternary_expr.else_expr, indent + 1); DUMPEXPR(expr->ternary_expr.else_expr);
break; DUMPEND();
case EXPR_INITIALIZER_LIST: case EXPR_INITIALIZER_LIST:
fprintf_indented(file, indent, "(initializerlist "); fprintf_indented(file, indent, "(initializerlist ");
switch (expr->expr_initializer.init_type) switch (expr->expr_initializer.init_type)
{ {
case INITIALIZER_UNKNOWN: case INITIALIZER_UNKNOWN:
fprintf(file, "not-analyzed\n"); fprintf(file, "not-analyzed\n");
break; break;
case INITIALIZER_ZERO: case INITIALIZER_ZERO:
@@ -606,59 +615,95 @@ void fprint_expr_recursive(FILE *file, Expr *expr, int indent)
fprintf(file, "designated\n"); fprintf(file, "designated\n");
break; break;
} }
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
{ {
VECEACH(expr->expr_initializer.initializer_expr, i) VECEACH(expr->expr_initializer.initializer_expr, i)
{ {
fprint_expr_recursive(file, expr->expr_initializer.initializer_expr[i], indent + 1); fprint_expr_recursive(file, expr->expr_initializer.initializer_expr[i], indent + 1);
} }
} }
break; DUMPEND();
case EXPR_SUBSCRIPT: case EXPR_SUBSCRIPT:
fprintf_indented(file, indent, "(subscript\n"); DUMP("(subscript");
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_expr_recursive(file, expr->subscript_expr.expr, indent + 1); DUMPEXPR(expr->subscript_expr.expr);
fprint_expr_recursive(file, expr->subscript_expr.index, indent + 1); DUMPEXPC(expr->subscript_expr.index);
break; DUMPEND();
case EXPR_TRY: case EXPR_TRY:
if (!expr->try_expr.else_expr) switch (expr->try_expr.type)
{ {
fprintf_indented(file, indent, "(try\n"); case TRY_EXPR_ELSE_JUMP:
fprint_expr_common(file, expr, indent + 1); DUMP("(try-else-jump");
fprint_expr_recursive(file, expr->try_expr.expr, indent + 1); DUMPEXPC(expr);
DUMPEXPR(expr->try_expr.expr);
DUMPAST(expr->try_expr.else_stmt);
DUMPEND();
case TRY_EXPR_ELSE_EXPR:
DUMP("(try-else");
DUMPEXPC(expr);
DUMPEXPR(expr->try_expr.expr);
DUMPEXPR(expr->try_expr.else_expr);
DUMPEND();
case TRY_STMT:
DUMP("(try-stmt");
DUMPAST(expr->try_expr.stmt);
DUMPEND();
case TRY_EXPR:
DUMP("(try-expr");
DUMPEXPR(expr->try_expr.expr);
DUMPEND();
} }
else UNREACHABLE
{
fprintf_indented(file, indent, "(try-else\n");
fprint_expr_common(file, expr, indent + 1);
fprint_expr_recursive(file, expr->try_expr.expr, indent + 1);
fprint_expr_recursive(file, expr->try_expr.else_expr, indent + 1);
}
break;
case EXPR_CAST: case EXPR_CAST:
fprintf_indented(file, indent, "cast\n"); DUMP("(cast\n");
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_type_info_recursive(file, expr->cast_expr.type_info, indent + 1); DUMPTI(expr->cast_expr.type_info);
fprint_expr_recursive(file, expr->cast_expr.expr, indent + 1); DUMPEXPR(expr->cast_expr.expr);
break; DUMPEND();
case EXPR_EXPRESSION_LIST: case EXPR_EXPRESSION_LIST:
fprintf_indented(file, indent, "(expression-list\n"); DUMP("(expression-list");
fprint_expr_common(file, expr, indent + 1); DUMPEXPC(expr);
fprint_type_info_recursive(file, expr->struct_value_expr.type, indent + 1); DUMPTI(expr->struct_value_expr.type);
VECEACH(expr->expression_list, i) VECEACH(expr->expression_list, i)
{ {
fprint_expr_recursive(file, expr->expression_list[i], indent + 1); fprint_expr_recursive(file, expr->expression_list[i], indent + 1);
} }
break; DUMPEND();
default: case EXPR_POISONED:
fprintf_indented(file, indent, "(TODOEXPR)\n"); DUMP("(POISONED)");
return; return;
case EXPR_TYPEOF:
DUMP("(typeof");
DUMPEXPR(expr->typeof_expr);
DUMPEND();
case EXPR_SCOPED_EXPR:
DUMP("(scopedexpr");
DUMPEXPR(expr->expr_scope.expr);
// TODO defers.
DUMPEND();
case EXPR_MACRO_EXPR:
DUMP("(macro-expr");
DUMPEXPR(expr->macro_expr);
DUMPEND();
case EXPR_RANGE:
DUMP("(range");
DUMPEXPR(expr->range_expr.left);
DUMPEXPR(expr->range_expr.right);
DUMPEND();
case EXPR_DESIGNATED_INITIALIZER:
DUMP("(designated-initializer");
// TODO path
DUMPEXPR(expr->designated_init_expr.value);
DUMPEND();
case EXPR_COMPOUND_LITERAL:
DUMP("(compound-literal");
DUMPTI(expr->expr_compound_literal.type_info);
DUMPEXPR(expr->expr_compound_literal.initializer);
DUMPEND();
} }
fprint_endparen(file, indent); UNREACHABLE
} }
static void fprint_decl_list(FILE *file, Decl **decls, int indent);
static void fprint_ast_recursive(FILE *file, Ast *ast, int indent);
void fprint_func_signature(FILE *file, FunctionSignature *signature, int indent) void fprint_func_signature(FILE *file, FunctionSignature *signature, int indent)
{ {
@@ -687,14 +732,26 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent)
switch (decl->decl_kind) switch (decl->decl_kind)
{ {
case DECL_VAR: case DECL_VAR:
fprintf_indented(file, indent, "(var-%s %s\n", decl_var_to_string(decl->var.kind), decl->name ?: ""); DUMPF("(var-%s %s", decl_var_to_string(decl->var.kind), decl->name ?: "");
fprint_type_info_recursive(file, decl->var.type_info, indent + 1); DUMPTI(decl->var.type_info);
if (decl->var.init_expr) switch (decl->var.kind)
{ {
fprint_expr_recursive(file, decl->var.init_expr, indent + 1); case VARDECL_CONST:
DUMPEXPR(decl->var.init_expr);
break;
case VARDECL_GLOBAL:
DUMPEXPR(decl->var.init_expr);
break;
case VARDECL_LOCAL:
DUMPEXPR(decl->var.init_expr);
break;
case VARDECL_PARAM:
DUMPEXPR(decl->var.init_expr);
break;
case VARDECL_MEMBER:
break;
} }
fprint_endparen(file, indent); DUMPEND();
break;
case DECL_MACRO: case DECL_MACRO:
fprintf_indented(file, indent, "(macro %s\n", decl->name); fprintf_indented(file, indent, "(macro %s\n", decl->name);
fprint_type_info_recursive(file, decl->macro_decl.rtype, indent + 1); fprint_type_info_recursive(file, decl->macro_decl.rtype, indent + 1);
@@ -720,15 +777,13 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent)
fprint_endparen(file, indent); fprint_endparen(file, indent);
break; break;
case DECL_STRUCT: case DECL_STRUCT:
fprintf_indented(file, indent, "(struct %s\n", decl->name); DUMPF("(struct %s", decl->name);
fprint_decl_list(file, decl->strukt.members, indent + 1); DUMPDECLS(decl->strukt.members);
fprint_endparen(file, indent); DUMPEND();
break;
case DECL_UNION: case DECL_UNION:
fprintf_indented(file, indent, "(union %s\n", decl->name); DUMPF("(union %s", decl->name);
fprint_decl_list(file, decl->strukt.members, indent + 1); DUMPDECLS(decl->strukt.members);
fprint_endparen(file, indent); DUMPEND();
break;
case DECL_ENUM: case DECL_ENUM:
fprintf_indented(file, indent, "(enum %s\n", decl->name); fprintf_indented(file, indent, "(enum %s\n", decl->name);
fprint_type_info_recursive(file, decl->enums.type_info, indent + 1); fprint_type_info_recursive(file, decl->enums.type_info, indent + 1);
@@ -823,11 +878,46 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent)
return; return;
case DECL_IMPORT: case DECL_IMPORT:
fprintf_indented(file, indent, "(import %s", decl->name); fprintf_indented(file, indent, "(import %s", decl->name);
TODO
fprint_endparen(file, indent); fprint_endparen(file, indent);
break; break;
case DECL_ATTRIBUTE: case DECL_ATTRIBUTE:
TODO fprintf_indented(file, indent, "(attribute %s", decl->name);
if (decl->attr.domains & ATTR_FUNC)
{
fprintf_indented(file, indent + 1, "(func)");
}
if (decl->attr.domains & ATTR_VAR)
{
fprintf_indented(file, indent + 1, "(var)");
}
if (decl->attr.domains & ATTR_ENUM)
{
fprintf_indented(file, indent + 1, "(enum)");
}
if (decl->attr.domains & ATTR_STRUCT)
{
fprintf_indented(file, indent + 1, "(struct)");
}
if (decl->attr.domains & ATTR_UNION)
{
fprintf_indented(file, indent + 1, "(union)");
}
if (decl->attr.domains & ATTR_CONST)
{
fprintf_indented(file, indent + 1, "(const)");
}
if (decl->attr.domains & ATTR_ERROR)
{
fprintf_indented(file, indent + 1, "(error)");
}
if (decl->attr.domains & ATTR_TYPEDEF)
{
fprintf_indented(file, indent + 1, "(typedef");
}
// TODO attribute
fprint_endparen(file, indent);
break;
case DECL_THROWS: case DECL_THROWS:
fprintf_indented(file, indent, "(throws"); fprintf_indented(file, indent, "(throws");
fprint_type_info_recursive(file, decl->throws, indent + 1); fprint_type_info_recursive(file, decl->throws, indent + 1);
@@ -854,128 +944,111 @@ static void fprint_asts_recursive(FILE *file, Ast **asts, int indent)
static void fprint_ast_recursive(FILE *file, Ast *ast, int indent) static void fprint_ast_recursive(FILE *file, Ast *ast, int indent)
{ {
fprint_indent(file, indent); if (!ast) return;
switch (ast->ast_kind) switch (ast->ast_kind)
{ {
case AST_COMPOUND_STMT: case AST_COMPOUND_STMT:
if (!ast->compound_stmt.stmts) if (!ast->compound_stmt.stmts)
{ {
fprintf(file, "(compound)\n"); DUMP("(compound)");
return; return;
} }
fprintf(file, "(compound\n"); DUMP("(compound\n");
{ fprint_asts_recursive(file, ast->compound_stmt.stmts, indent + 1);
fprint_asts_recursive(file, ast->compound_stmt.stmts, indent + 1); DUMPEND();
}
break;
case AST_DECL_EXPR_LIST: case AST_DECL_EXPR_LIST:
fprintf(file, "(declexprlist\n"); DUMP("(declexprlist");
{ fprint_asts_recursive(file, ast->decl_expr_stmt, indent + 1);
fprint_asts_recursive(file, ast->decl_expr_stmt, indent + 1); DUMPEND();
}
break;
case AST_DECLARE_STMT: case AST_DECLARE_STMT:
fprintf(file, "(declare\n"); DUMP("(declare");
fprint_decl_recursive(file, ast->declare_stmt, indent + 1); fprint_decl_recursive(file, ast->declare_stmt, indent + 1);
break; DUMPEND();
case AST_EXPR_STMT: case AST_EXPR_STMT:
fprintf(file, "(exprstmt\n"); DUMP("expr");
fprint_expr_recursive(file, ast->expr_stmt, indent + 1); DUMPEXPR(ast->expr_stmt);
break; DUMPEND();
return;
case AST_WHILE_STMT: case AST_WHILE_STMT:
fprintf(file, "(while\n"); DUMP("(while");
fprint_ast_recursive(file, ast->while_stmt.cond, indent + 1); DUMPAST(ast->while_stmt.cond);
fprint_ast_recursive(file, ast->while_stmt.body, indent + 1); DUMPAST(ast->while_stmt.body);
break; DUMPEND();
case AST_SCOPED_STMT: case AST_SCOPED_STMT:
fprintf(file, "(scoped\n"); DUMP("(scoped");
fprint_ast_recursive(file, ast->scoped_stmt.stmt, indent + 1); DUMPAST(ast->scoped_stmt.stmt);
break; DUMPEND();
case AST_CT_FOR_STMT: case AST_CT_FOR_STMT:
if (ast->ct_for_stmt.index.string) if (ast->ct_for_stmt.index.string)
{ {
fprintf(file, "($for %s, %s\n", ast->ct_for_stmt.index.string, ast->ct_for_stmt.value.string); DUMPF("($for %s, %s\n", ast->ct_for_stmt.index.string, ast->ct_for_stmt.value.string);
} }
else else
{ {
fprintf(file, "($for %s\n", ast->ct_for_stmt.value.string); DUMPF("($for %s\n", ast->ct_for_stmt.value.string);
} }
fprint_expr_recursive(file, ast->ct_for_stmt.expr, indent + 1); DUMPEXPR(ast->ct_for_stmt.expr);
fprint_ast_recursive(file, ast->ct_for_stmt.body, indent + 1); DUMPAST(ast->ct_for_stmt.body);
break; DUMPEND();
case AST_DO_STMT: case AST_DO_STMT:
fprintf(file, "(do\n"); DUMP("(do");
fprint_ast_recursive(file, ast->do_stmt.body, indent + 1); DUMPAST(ast->do_stmt.body);
fprint_expr_recursive(file, ast->do_stmt.expr, indent + 1); DUMPEXPR(ast->do_stmt.expr);
break; DUMPEND();
case AST_RETURN_STMT: case AST_RETURN_STMT:
if (ast->return_stmt.expr) if (ast->return_stmt.expr)
{ {
fprintf(file, "(return\n"); DUMP("(return");
fprint_expr_recursive(file, ast->expr_stmt, indent + 1); DUMPEXPR(ast->expr_stmt);
break; DUMPEND();
}
else
{
fprintf(file, "(return)\n");
return;
} }
DUMP("(return)");
return;
case AST_BREAK_STMT: case AST_BREAK_STMT:
fprintf(file, "(break)\n"); DUMP("(break)");
return; return;
case AST_NEXT_STMT: case AST_NEXT_STMT:
fprintf(file, "(next)\n"); DUMP("(next)");
return; return;
case AST_CONTINUE_STMT: case AST_CONTINUE_STMT:
fprintf(file, "(continue)\n"); DUMP("(continue)");
return; return;
case AST_DEFAULT_STMT: case AST_DEFAULT_STMT:
fprintf(file, "(default)\n"); DUMP("(default)");
return; return;
case AST_FOR_STMT: case AST_FOR_STMT:
fprintf(file, "(for\n"); DUMP("(for");
if (ast->for_stmt.init) DUMPAST(ast->for_stmt.init);
{ DUMPEXPR(ast->for_stmt.cond);
fprint_ast_recursive(file, ast->for_stmt.init, indent + 1);
}
if (ast->for_stmt.cond)
{
fprint_expr_recursive(file, ast->for_stmt.cond, indent + 1);
}
if (ast->for_stmt.incr) if (ast->for_stmt.incr)
{ {
fprint_expr_recursive(file, ast->for_stmt.incr, indent + 1); DUMPEXPR(ast->for_stmt.incr);
} }
else else
{ {
fprint_indent(file, indent + 1); DUMPI("(noincr)");
fprintf(file, "(noincr)\n");
} }
fprint_ast_recursive(file, ast->for_stmt.body, indent + 1); DUMPAST(ast->for_stmt.body);
break; DUMPEND();
case AST_IF_STMT: case AST_IF_STMT:
fprintf(file, "(if\n"); DUMP("(if");
fprint_ast_recursive(file, ast->if_stmt.cond, indent + 1); DUMPAST(ast->if_stmt.cond);
fprint_ast_recursive(file, ast->if_stmt.then_body, indent + 1); DUMPAST(ast->if_stmt.then_body);
if (ast->if_stmt.else_body) DUMPAST(ast->if_stmt.else_body);
{ DUMPEND();
fprint_ast_recursive(file, ast->if_stmt.else_body, indent + 1);
}
break;
case AST_SWITCH_STMT: case AST_SWITCH_STMT:
fprintf(file, "(switchstmt\n"); DUMP("(switch");
fprint_ast_recursive(file, ast->switch_stmt.cond, indent + 1); fprint_ast_recursive(file, ast->switch_stmt.cond, indent + 1);
fprint_asts_recursive(file, ast->switch_stmt.cases, indent + 1); fprint_asts_recursive(file, ast->switch_stmt.cases, indent + 1);
break; DUMPEND();
case AST_CASE_STMT: case AST_CASE_STMT:
fprintf(file, "(case\n"); DUMP("(case");
fprint_expr_recursive(file, ast->case_stmt.expr, indent + 1); DUMPEXPR(ast->case_stmt.expr);
break; DUMPEND();
case AST_DEFER_STMT: case AST_DEFER_STMT:
fprintf(file, "(defer\n"); DUMP("(defer");
fprint_ast_recursive(file, ast->defer_stmt.body, indent + 1); DUMPAST(ast->defer_stmt.body);
break; DUMPEND();
case AST_GENERIC_CASE_STMT: case AST_GENERIC_CASE_STMT:
fprintf(file, "(generic-case\n"); fprintf(file, "(generic-case\n");
fprint_indent(file, indent + 1); fprint_indent(file, indent + 1);
@@ -990,11 +1063,11 @@ static void fprint_ast_recursive(FILE *file, Ast *ast, int indent)
fprint_ast_recursive(file, ast->generic_case_stmt.body, indent + 1); fprint_ast_recursive(file, ast->generic_case_stmt.body, indent + 1);
break; break;
case AST_GENERIC_DEFAULT_STMT: case AST_GENERIC_DEFAULT_STMT:
fprintf(file, "(generic-default\n"); DUMP("(generic-default");
fprint_ast_recursive(file, ast->generic_default_stmt, indent + 1); DUMPAST(ast->generic_default_stmt);
break; DUMPEND();
case AST_POISONED: case AST_POISONED:
fprintf(file, "(ast-poisoned)\n"); DUMP("(ast-poisoned)");
return; return;
case AST_ASM_STMT: case AST_ASM_STMT:
TODO TODO
@@ -1003,8 +1076,10 @@ static void fprint_ast_recursive(FILE *file, Ast *ast, int indent)
TODO TODO
break; break;
case AST_CATCH_STMT: case AST_CATCH_STMT:
TODO DUMP("(catch");
break; fprint_decl_recursive(file, ast->catch_stmt.error_param, indent + 1);
DUMPAST(ast->catch_stmt.body);
DUMPEND();
case AST_CT_IF_STMT: case AST_CT_IF_STMT:
TODO TODO
break; break;
@@ -1024,23 +1099,24 @@ static void fprint_ast_recursive(FILE *file, Ast *ast, int indent)
TODO TODO
break; break;
case AST_GOTO_STMT: case AST_GOTO_STMT:
fprintf(file, "(goto %s)\n", ast->goto_stmt.label_name); DUMPF("(goto %s)", ast->goto_stmt.label_name);
return; return;
case AST_LABEL: case AST_LABEL:
fprintf(file, "(label %s)\n", ast->label_stmt.name); DUMPF("(label %s)", ast->label_stmt.name);
return; return;
case AST_NOP_STMT: case AST_NOP_STMT:
TODO DUMP("(nop)");
break; return;
case AST_THROW_STMT: case AST_THROW_STMT:
fprintf(file, "(throw\n"); DUMP("(throw");
fprint_expr_recursive(file, ast->throw_stmt.throw_value, indent + 1); DUMPEXPR(ast->throw_stmt.throw_value);
break; DUMPEND();
case AST_VOLATILE_STMT: case AST_VOLATILE_STMT:
TODO DUMP("(volatile");
break; DUMPAST(ast->volatile_stmt);
DUMPEND();
} }
fprint_endparen(file, indent); UNREACHABLE
} }
void fprint_ast(FILE *file, Ast *ast) void fprint_ast(FILE *file, Ast *ast)
{ {

View File

@@ -754,7 +754,7 @@ CastKind cast_to_bool_kind(Type *type)
case TYPE_ARRAY: case TYPE_ARRAY:
case TYPE_VARARRAY: case TYPE_VARARRAY:
case TYPE_SUBARRAY: case TYPE_SUBARRAY:
case TYPE_META_TYPE: case TYPE_TYPEID:
// Improve consider vararray / subarray conversion to boolean. // Improve consider vararray / subarray conversion to boolean.
return CAST_ERROR; return CAST_ERROR;
case TYPE_BOOL: case TYPE_BOOL:
@@ -789,7 +789,7 @@ bool cast(Expr *expr, Type *to_type, CastType cast_type)
{ {
case TYPE_POISONED: case TYPE_POISONED:
case TYPE_VOID: case TYPE_VOID:
case TYPE_META_TYPE: case TYPE_TYPEID:
break; break;
case TYPE_BOOL: case TYPE_BOOL:
// Bool may convert into integers and floats but only explicitly. // Bool may convert into integers and floats but only explicitly.

View File

@@ -197,8 +197,6 @@ struct _Type
TypeFunc func; TypeFunc func;
// Type* // Type*
Type *pointer; Type *pointer;
// Type.type
Type *child;
}; };
}; };
@@ -431,6 +429,7 @@ typedef enum
{ {
TRY_EXPR_ELSE_EXPR, TRY_EXPR_ELSE_EXPR,
TRY_EXPR_ELSE_JUMP, TRY_EXPR_ELSE_JUMP,
TRY_EXPR,
TRY_STMT, TRY_STMT,
} TryType; } TryType;

View File

@@ -503,8 +503,8 @@ typedef enum
TYPE_ARRAY, TYPE_ARRAY,
TYPE_VARARRAY, TYPE_VARARRAY,
TYPE_SUBARRAY, TYPE_SUBARRAY,
TYPE_META_TYPE, TYPE_TYPEID,
TYPE_LAST = TYPE_META_TYPE TYPE_LAST = TYPE_TYPEID
} TypeKind; } TypeKind;
#define ALL_INTS TYPE_I8: case TYPE_I16: case TYPE_I32: case TYPE_I64: \ #define ALL_INTS TYPE_I8: case TYPE_I16: case TYPE_I32: case TYPE_I64: \

View File

@@ -108,7 +108,7 @@ LLVMMetadataRef gencontext_get_debug_type(GenContext *context, Type *type)
case TYPE_POISONED: case TYPE_POISONED:
case TYPE_IXX: case TYPE_IXX:
case TYPE_FXX: case TYPE_FXX:
case TYPE_META_TYPE: case TYPE_TYPEID:
UNREACHABLE UNREACHABLE
case TYPE_BOOL: case TYPE_BOOL:
return gencontext_simple_debug_type(context, type, DW_ATE_boolean); return gencontext_simple_debug_type(context, type, DW_ATE_boolean);

View File

@@ -811,6 +811,11 @@ LLVMValueRef gencontext_emit_typeid(GenContext *context, Expr *expr)
LLVMValueRef gencontext_emit_try_expr(GenContext *context, Expr *expr) LLVMValueRef gencontext_emit_try_expr(GenContext *context, Expr *expr)
{ {
if (expr->try_expr.type == TRY_STMT)
{
gencontext_emit_stmt(context, expr->try_expr.stmt);
return NULL;
}
if (expr->try_expr.type == TRY_EXPR_ELSE_EXPR) if (expr->try_expr.type == TRY_EXPR_ELSE_EXPR)
{ {
LLVMBasicBlockRef else_block = gencontext_get_try_target(context, expr); LLVMBasicBlockRef else_block = gencontext_get_try_target(context, expr);

View File

@@ -10,7 +10,7 @@ static inline LLVMTypeRef gencontext_create_basic_llvm_type(GenContext *context,
{ {
switch (type->type_kind) switch (type->type_kind)
{ {
case TYPE_META_TYPE: case TYPE_TYPEID:
return LLVMIntTypeInContext(context->context, type->builtin.bitsize); return LLVMIntTypeInContext(context->context, type->builtin.bitsize);
case TYPE_BOOL: case TYPE_BOOL:
return LLVMInt1TypeInContext(context->context); return LLVMInt1TypeInContext(context->context);

View File

@@ -169,7 +169,7 @@ LLVMTypeRef llvm_get_type(LLVMContextRef context, Type *type)
switch (type->type_kind) switch (type->type_kind)
{ {
case TYPE_POISONED: case TYPE_POISONED:
case TYPE_META_TYPE: case TYPE_TYPEID:
return type->backend_type = LLVMIntTypeInContext(context, type->builtin.bitsize); return type->backend_type = LLVMIntTypeInContext(context, type->builtin.bitsize);
case TYPE_ERROR: case TYPE_ERROR:
return type->backend_type = llvm_get_type(context, type_error_base); return type->backend_type = llvm_get_type(context, type_error_base);

View File

@@ -67,6 +67,9 @@ void expr_const_fprint(FILE *__restrict file, ExprConst *expr)
case TYPE_ERROR: case TYPE_ERROR:
fprintf(file, "%s", expr->error_constant->name); fprintf(file, "%s", expr->error_constant->name);
break; break;
case TYPE_STRING:
fprintf(file, "%.*s", expr->string.len, expr->string.chars);
break;
default: default:
UNREACHABLE UNREACHABLE
} }

View File

@@ -400,7 +400,7 @@ static Expr *parse_try_expr(Context *context, Expr *left)
Expr *try_expr = EXPR_NEW_TOKEN(EXPR_TRY, context->tok); Expr *try_expr = EXPR_NEW_TOKEN(EXPR_TRY, context->tok);
advance_and_verify(context, TOKEN_TRY); advance_and_verify(context, TOKEN_TRY);
try_expr->try_expr.expr = TRY_EXPR_OR(parse_precedence(context, PREC_TRY + 1), poisoned_expr); try_expr->try_expr.expr = TRY_EXPR_OR(parse_precedence(context, PREC_TRY + 1), poisoned_expr);
try_expr->try_expr.type = TRY_STMT; try_expr->try_expr.type = TRY_EXPR;
if (try_consume(context, TOKEN_ELSE)) if (try_consume(context, TOKEN_ELSE))
{ {
switch (context->tok.type) switch (context->tok.type)

View File

@@ -1863,7 +1863,7 @@ static bool sema_expr_analyse_comp(Context *context, Expr *expr, Expr *left, Exp
case TYPE_ARRAY: case TYPE_ARRAY:
case TYPE_VARARRAY: case TYPE_VARARRAY:
case TYPE_SUBARRAY: case TYPE_SUBARRAY:
case TYPE_META_TYPE: case TYPE_TYPEID:
// Only != and == allowed. // Only != and == allowed.
goto ERR; goto ERR;
case ALL_INTS: case ALL_INTS:
@@ -2099,7 +2099,7 @@ static bool sema_expr_analyse_not(Context *context, Type *to, Expr *expr, Expr *
case TYPE_STRING: case TYPE_STRING:
case TYPE_ENUM: case TYPE_ENUM:
case TYPE_ERROR: case TYPE_ERROR:
case TYPE_META_TYPE: case TYPE_TYPEID:
SEMA_ERROR(expr, "Cannot use 'not' on %s", type_to_error_string(inner->type)); SEMA_ERROR(expr, "Cannot use 'not' on %s", type_to_error_string(inner->type));
return false; return false;
} }
@@ -2259,7 +2259,7 @@ static inline bool sema_expr_analyse_try(Context *context, Type *to, Expr *expr)
break; break;
} }
} }
if (expr->try_expr.type != TRY_STMT) if (expr->try_expr.type != TRY_STMT && expr->try_expr.type != TRY_EXPR)
{ {
CatchInfo info = { .kind = CATCH_TRY_ELSE, .try_else = expr, .defer = context->current_scope->defers.start }; CatchInfo info = { .kind = CATCH_TRY_ELSE, .try_else = expr, .defer = context->current_scope->defers.start };
// Absorb all errors. // Absorb all errors.

View File

@@ -741,7 +741,7 @@ static bool sema_analyse_switch_stmt(Context *context, Ast *statement)
assert(switch_type->type_kind != TYPE_IXX); assert(switch_type->type_kind != TYPE_IXX);
case TYPE_BOOL: case TYPE_BOOL:
case TYPE_ERROR: case TYPE_ERROR:
case TYPE_META_TYPE: case TYPE_TYPEID:
case TYPE_ENUM: case TYPE_ENUM:
case TYPE_STRING: case TYPE_STRING:
break; break;

View File

@@ -102,9 +102,8 @@ const char *type_to_error_string(Type *type)
case TYPE_UNION: case TYPE_UNION:
case TYPE_ERROR: case TYPE_ERROR:
return type->name; return type->name;
case TYPE_META_TYPE: case TYPE_TYPEID:
asprintf(&buffer, "type %s", type_to_error_string(type->child)); return "typeid";
return buffer;
case TYPE_POINTER: case TYPE_POINTER:
asprintf(&buffer, "%s*", type_to_error_string(type->pointer)); asprintf(&buffer, "%s*", type_to_error_string(type->pointer));
return buffer; return buffer;
@@ -208,7 +207,7 @@ size_t type_size(Type *canonical)
case TYPE_VOID: case TYPE_VOID:
return 1; return 1;
case TYPE_BOOL: case TYPE_BOOL:
case TYPE_META_TYPE: case TYPE_TYPEID:
case ALL_INTS: case ALL_INTS:
case ALL_FLOATS: case ALL_FLOATS:
return canonical->builtin.bytesize; return canonical->builtin.bytesize;
@@ -242,7 +241,7 @@ unsigned int type_abi_alignment(Type *canonical)
case TYPE_STRUCT: case TYPE_STRUCT:
case TYPE_UNION: case TYPE_UNION:
return canonical->decl->strukt.abi_alignment; return canonical->decl->strukt.abi_alignment;
case TYPE_META_TYPE: case TYPE_TYPEID:
case TYPE_BOOL: case TYPE_BOOL:
case ALL_INTS: case ALL_INTS:
case ALL_FLOATS: case ALL_FLOATS:
@@ -440,7 +439,7 @@ type_create(#_name, &_shortname, _type, _bits, target->align_ ## _align, target-
#undef DEF_TYPE #undef DEF_TYPE
type_create("typeid", &t_typeid, TYPE_META_TYPE, target->width_pointer, target->align_pref_pointer, target->align_pointer); type_create("typeid", &t_typeid, TYPE_TYPEID, target->width_pointer, target->align_pref_pointer, target->align_pointer);
type_create("void*", &t_voidstar, TYPE_POINTER, target->width_pointer, target->align_pref_pointer, target->align_pointer); type_create("void*", &t_voidstar, TYPE_POINTER, target->width_pointer, target->align_pref_pointer, target->align_pointer);
create_type_cache(type_void); create_type_cache(type_void);
type_void->type_cache[0] = &t_voidstar; type_void->type_cache[0] = &t_voidstar;
@@ -678,7 +677,7 @@ Type *type_find_max_type(Type *type, Type *other)
case TYPE_FUNC: case TYPE_FUNC:
case TYPE_UNION: case TYPE_UNION:
case TYPE_ERROR_UNION: case TYPE_ERROR_UNION:
case TYPE_META_TYPE: case TYPE_TYPEID:
return NULL; return NULL;
case TYPE_STRUCT: case TYPE_STRUCT:
TODO TODO