From 7392b453a39f411dd7a459cf687dcd7dd44d6565 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 9 Apr 2020 13:49:51 +0200 Subject: [PATCH] Work on unions and anonymous structs/unions. --- src/compiler/ast.c | 4 ++++ src/compiler/compiler_internal.h | 13 +----------- src/compiler/llvm_codegen_expr.c | 4 ---- src/compiler/sema_expr.c | 35 ++++++++++++++++++++------------ 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/compiler/ast.c b/src/compiler/ast.c index 46c466f84..02d1d38d1 100644 --- a/src/compiler/ast.c +++ b/src/compiler/ast.c @@ -110,6 +110,10 @@ Decl *decl_new_var(Token name, TypeInfo *type, VarDeclKind kind, Visibility visi return decl; } +/** + * Recursively find a node in a declaration. + * @return NULL if it wasn't found, otherwise the member. + */ Decl *struct_find_name(Decl *decl, const char* name) { Decl** compare_members = decl->strukt.members; diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index a1ff2938a..b676f03d5 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -521,17 +521,6 @@ typedef struct Expr** initializer_expr; } ExprInitializer; -typedef struct _DesignatedInitPath -{ - Decl *decl; - struct _DesignatedInitPath *sub_path; -} DesignatedInitPath; - -typedef struct -{ - DesignatedInitPath path; - Expr *value; -} ExprDesignatedInit; struct _Expr { @@ -540,7 +529,7 @@ struct _Expr SourceRange span; Type *type; union { - ExprDesignatedInit designated_init_expr; + Expr *group_expr; ExprCast cast_expr; ExprConst const_expr; ExprStructValue struct_value_expr; diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 6bad86e85..b050172ec 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -180,7 +180,6 @@ LLVMValueRef gencontext_emit_address(GenContext *context, Expr *expr) case EXPR_EXPRESSION_LIST: case EXPR_CAST: case EXPR_MACRO_EXPR: - case EXPR_DESIGNATED_INIT: UNREACHABLE } UNREACHABLE @@ -968,9 +967,6 @@ LLVMValueRef gencontext_emit_expr(GenContext *context, Expr *expr) { case EXPR_POISONED: UNREACHABLE - case EXPR_DESIGNATED_INIT: - // This is handled inside of initializer setup - UNREACHABLE case EXPR_EXPR_BLOCK: return gencontext_emit_expr_block(context, expr); case EXPR_SCOPED_EXPR: diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 8894fca68..7b829b01b 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -281,11 +281,10 @@ static inline bool sema_expr_analyse_call(Context *context, Type *to, Expr *expr } } - -static inline bool sema_expr_analyse_subscript(Context *context, Type *to, Expr *expr) +static inline bool sema_expr_analyse_subscript_after_parent_resolution(Context *context, Expr *expr) { - if (!sema_analyse_expr(context, NULL, expr->subscript_expr.expr)) return false; - + assert(expr->expr_kind == EXPR_SUBSCRIPT); + assert(expr->subscript_expr.expr->resolve_status == RESOLVE_DONE); Type *type = expr->subscript_expr.expr->type->canonical; Type *inner_type; switch (type->type_kind) @@ -307,7 +306,6 @@ static inline bool sema_expr_analyse_subscript(Context *context, Type *to, Expr return false; } - if (!sema_analyse_expr(context, type_isize, expr->subscript_expr.index)) return false; // Unless we already have type_usize, cast to type_isize; @@ -319,6 +317,13 @@ static inline bool sema_expr_analyse_subscript(Context *context, Type *to, Expr return true; } +static inline bool sema_expr_analyse_subscript(Context *context, Type *to, Expr *expr) +{ + if (!sema_analyse_expr(context, NULL, expr->subscript_expr.expr)) return false; + + return sema_expr_analyse_subscript_after_parent_resolution(context, expr); +} + static inline bool sema_expr_analyse_method_function(Context *context, Expr *expr, Decl *decl, bool is_pointer) { const char *name = expr->access_expr.sub_element.string; @@ -362,11 +367,12 @@ static inline bool sema_expr_analyse_group(Context *context, Type *to, Expr *exp } -static inline bool sema_expr_analyse_access(Context *context, Type *to, Expr *expr) +static inline bool sema_expr_analyse_access_after_parent_resolution(Context *context, Expr *expr) { - if (!sema_analyse_expr(context, NULL, expr->access_expr.parent)) return false; - Type *parent_type = expr->access_expr.parent->type; + assert(expr->expr_kind == EXPR_ACCESS); + assert(expr->access_expr.parent->resolve_status == RESOLVE_DONE); + Type *parent_type = expr->access_expr.parent->type; Type *type = parent_type->canonical; bool is_pointer = type->type_kind == TYPE_POINTER; if (is_pointer) @@ -410,6 +416,13 @@ static inline bool sema_expr_analyse_access(Context *context, Type *to, Expr *ex return true; } +static inline bool sema_expr_analyse_access(Context *context, Expr *expr) +{ + if (!sema_analyse_expr(context, NULL, expr->access_expr.parent)) return false; + return sema_expr_analyse_access_after_parent_resolution(context, expr); +} + + static inline bool sema_expr_analyse_type_access(Context *context, Type *to, Expr *expr) { TypeInfo *type_info = expr->type_access.type; @@ -1992,9 +2005,6 @@ static Expr *expr_copy_from_macro(Context *context, Expr *macro, Expr *source_ex Expr *expr = expr_shallow_copy(source_expr); switch (source_expr->expr_kind) { - case EXPR_DESIGNATED_INIT: - // This type of expression is only created after analysis. - UNREACHABLE case EXPR_EXPR_BLOCK: ast_copy_list_from_macro(context, macro, &expr->expr_block.stmts); return expr; @@ -2449,7 +2459,6 @@ static inline bool sema_analyse_expr_dispatch(Context *context, Type *to, Expr * case EXPR_POISONED: return false; case EXPR_SCOPED_EXPR: - case EXPR_DESIGNATED_INIT: UNREACHABLE case EXPR_EXPR_BLOCK: return sema_expr_analyse_expr_block(context, to, expr); @@ -2482,7 +2491,7 @@ static inline bool sema_analyse_expr_dispatch(Context *context, Type *to, Expr * case EXPR_GROUP: return sema_expr_analyse_group(context, to, expr); case EXPR_ACCESS: - return sema_expr_analyse_access(context, to, expr); + return sema_expr_analyse_access(context, expr); case EXPR_INITIALIZER_LIST: return sema_expr_analyse_initializer_list(context, to, expr); case EXPR_CAST: