From 95f39f42ebf41964b8340d99de3b1354a36b0bb4 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 31 May 2021 21:06:31 +0200 Subject: [PATCH] Fix nasty bug where the node copies did not get a proper id. --- resources/editor_plugins/nano/c3.nanorc | 2 +- src/compiler/compiler_internal.h | 2 +- src/compiler/copying.c | 11 +- src/compiler/parse_global.c | 2 +- src/compiler/sema_casts.c | 2 +- src/compiler/sema_expr.c | 2 +- src/compiler/sema_name_resolution.c | 2 +- src/compiler/sema_passes.c | 1 + src/compiler/sema_stmts.c | 8 +- src/utils/lib.h | 1 - src/utils/malloc.h | 3 +- .../test_suite/bitstruct/bitstruct_general.c3 | 9 +- test/test_suite/define/common.c3 | 118 ++---------------- test/test_suite/generic/generic_copy.c3t | 12 ++ test/test_suite/globals/incr_array.c3 | 75 ----------- test/test_suite/globals/incr_enum.c3 | 17 --- 16 files changed, 48 insertions(+), 219 deletions(-) create mode 100644 test/test_suite/generic/generic_copy.c3t delete mode 100644 test/test_suite/globals/incr_array.c3 delete mode 100644 test/test_suite/globals/incr_enum.c3 diff --git a/resources/editor_plugins/nano/c3.nanorc b/resources/editor_plugins/nano/c3.nanorc index 34659f9da..6e3f362c0 100644 --- a/resources/editor_plugins/nano/c3.nanorc +++ b/resources/editor_plugins/nano/c3.nanorc @@ -21,7 +21,7 @@ color brightred "@\<[A-Za-z_]+\>" color green "\<(virtual|void|bool|quad|double|float|long|ulong|int|uint|short|ushort|ichar|char|isize|usize|iptr|uptr|iptrdiff|uptrdiff|half)\>" # Keywords -color yellow "\<(alias|as|asm|assert|attribute|break|case|catch|const|continue|default|defer|define|do|else|enum|extern|error|false|for|foreach|func|generic|if|import|interface|macro|module|nextcase|null|private|return|static|struct|switch|template|true|try|typeid|typeof|union|while|var|volatile)\>" +color yellow "\<(alias|as|asm|assert|attribute|break|case|catch|const|continue|default|defer|define|do|else|enum|extern|error|false|for|foreach|func|generic|if|import|interface|macro|module|nextcase|null|private|return|static|struct|switch|true|try|typeid|typeof|union|while|var|volatile)\>" # Strings color brightblack "\"[^"]*\"" diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 192484d68..4cfee9da7 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -227,7 +227,7 @@ struct Type_ struct TypeInfo_ { - ResolveStatus resolve_status : 2; + ResolveStatus resolve_status : 3; bool virtual_type : 1; Type *type; TypeInfoKind kind; diff --git a/src/compiler/copying.c b/src/compiler/copying.c index fc2aa4c10..a74fcf02f 100644 --- a/src/compiler/copying.c +++ b/src/compiler/copying.c @@ -66,7 +66,7 @@ static DesignatorElement **macro_copy_designator_list(DesignatorElement **list) Expr *copy_expr(Expr *source_expr) { if (!source_expr) return NULL; - Expr *expr = COPY(source_expr); + Expr *expr = expr_copy(source_expr); switch (source_expr->expr_kind) { case EXPR_ENUM_CONSTANT: @@ -194,7 +194,7 @@ Expr *copy_expr(Expr *source_expr) Ast *copy_ast(Ast *source) { if (!source) return NULL; - Ast *ast = COPY(source); + Ast *ast = ast_copy(source); switch (source->ast_kind) { case AST_DOCS: @@ -388,7 +388,7 @@ Decl *decl_copy_local_from_macro(Decl *to_copy) { if (!to_copy) return NULL; assert(to_copy->decl_kind == DECL_VAR); - Decl *copy = COPY(to_copy); + Decl *copy = decl_copy(to_copy); MACRO_COPY_TYPE(copy->var.type_info); MACRO_COPY_EXPR(copy->var.init_expr); return copy; @@ -397,8 +397,7 @@ Decl *decl_copy_local_from_macro(Decl *to_copy) TypeInfo *copy_type_info(TypeInfo *source) { if (!source) return NULL; - TypeInfo *copy = malloc_arena(sizeof(TypeInfo)); - memcpy(copy, source, sizeof(TypeInfo)); + TypeInfo *copy = type_info_copy(source); switch (source->kind) { case TYPE_INFO_POISON: @@ -464,7 +463,7 @@ static Attr **copy_attributes(Attr** attr_list) Decl *copy_decl(Decl *decl) { if (!decl) return NULL; - Decl *copy = COPY(decl); + Decl *copy = decl_copy(decl); MACRO_COPY_AST(copy->docs); copy->attributes = copy_attributes(copy->attributes); switch (decl->decl_kind) diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 502840fbe..508ec1143 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -1618,7 +1618,7 @@ static inline Decl *parse_define_ident(Context *context, Visibility visibility) TokenType alias_type = context->tok.type; if (alias_type != TOKEN_IDENT && alias_type != TOKEN_CONST_IDENT) { - SEMA_TOKEN_ERROR(context->tok, "An regular identifier was expected here."); + SEMA_TOKEN_ERROR(context->tok, "An identifier was expected here."); return poisoned_decl; } diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index abd6a2aad..102c4e3f3 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -12,7 +12,7 @@ static inline bool insert_cast(Expr *expr, CastKind kind, Type *type) { assert(expr->resolve_status == RESOLVE_DONE); assert(expr->type); - Expr *inner = COPY(expr); + Expr *inner = expr_copy(expr); expr->expr_kind = EXPR_CAST; expr->cast_expr.kind = kind; expr->cast_expr.expr = inner; diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index e7342872b..e4dd31972 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -4296,7 +4296,7 @@ static inline bool sema_expr_analyse_ct_incdec(Context *context, Expr *expr, Exp return false; } - Expr *end_value = COPY(start_value); + Expr *end_value = expr_copy(start_value); // Make the change. BigInt change; diff --git a/src/compiler/sema_name_resolution.c b/src/compiler/sema_name_resolution.c index ce191908a..73b3da118 100644 --- a/src/compiler/sema_name_resolution.c +++ b/src/compiler/sema_name_resolution.c @@ -382,7 +382,7 @@ bool sema_add_local(Context *context, Decl *decl) bool sema_unwrap_var(Context *context, Decl *decl) { - Decl *alias = COPY(decl); + Decl *alias = decl_copy(decl); alias->var.kind = VARDECL_ALIAS; alias->var.alias = decl; alias->var.failable = false; diff --git a/src/compiler/sema_passes.c b/src/compiler/sema_passes.c index 096234ee2..d5ea8f86a 100644 --- a/src/compiler/sema_passes.c +++ b/src/compiler/sema_passes.c @@ -200,6 +200,7 @@ void sema_analysis_pass_decls(Module *module) { Context *context = module->contexts[index]; context->current_scope = &context->scopes[0]; + assert(context->current_scope->defer_last < 10000000); context->current_scope->scope_id = 0; context->last_local = &context->locals[0]; VECEACH(context->enums, i) diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 03bb9ae69..ddecebb6d 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -26,6 +26,7 @@ void context_push_scope_with_flags(Context *context, ScopeFlags flags) context->current_scope->local_decl_start = context->last_local; context->current_scope->in_defer = previous_defer; context->current_scope->defer_last = parent_defer; + assert(parent_defer < 1000000); if (flags & (SCOPE_DEFER | SCOPE_EXPR_BLOCK)) { @@ -58,6 +59,7 @@ void context_push_scope_with_label(Context *context, Decl *label) static inline void context_pop_defers_to(Context *context, DeferList *list) { list->end = context_start_defer(context); + assert(context->current_scope->defer_last < 10000000); list->start = context->current_scope->defer_last; context->current_scope->defer_last = list->end; } @@ -105,7 +107,7 @@ static void context_pop_defers_and_replace_ast(Context *context, Ast *ast) return; } assert(ast->ast_kind != AST_COMPOUND_STMT); - Ast *replacement = COPY(ast); + Ast *replacement = ast_copy(ast); ast->ast_kind = AST_SCOPED_STMT; ast->scoped_stmt.stmt = replacement; ast->scoped_stmt.defers = defers; @@ -1689,7 +1691,7 @@ EXIT: { if (unwrapped && !statement->flow.has_break && statement->flow.no_exit) { - Decl *decl = COPY(unwrapped); + Decl *decl = decl_copy(unwrapped); decl->var.kind = VARDECL_ALIAS; decl->var.alias = unwrapped; decl->var.failable = false; @@ -1727,7 +1729,7 @@ static bool sema_analyse_try_stmt(Context *context, Ast *stmt) if (expr->expr_kind == EXPR_IDENTIFIER) { Decl *var = expr->identifier_expr.decl; - Decl *decl = COPY(var); + Decl *decl = decl_copy(var); decl->var.kind = VARDECL_ALIAS; decl->var.alias = var; decl->var.failable = false; diff --git a/src/utils/lib.h b/src/utils/lib.h index 3d1522a0d..015c9695d 100644 --- a/src/utils/lib.h +++ b/src/utils/lib.h @@ -23,7 +23,6 @@ void run_arena_allocator_tests(void); #define MALLOC(mem) malloc_arena(mem) #define CALLOCS(type) ({ type *__x = malloc_arena(sizeof(type)); memset(__x, 0, sizeof(type)); __x; }) -#define COPY(value) ({ typeof(value) __x = malloc_arena(sizeof(*value)); memcpy(__x, value, sizeof(*value)); __x; }) static inline bool is_power_of_two(uint64_t x) { diff --git a/src/utils/malloc.h b/src/utils/malloc.h index fb95d5a51..aee11cacd 100644 --- a/src/utils/malloc.h +++ b/src/utils/malloc.h @@ -16,4 +16,5 @@ static inline type *name##_calloc(void) { \ memset(ptr, 0, sizeof(type)); \ return ptr; } \ static inline type *name##ptr(type ## Id id) { return ((type *)name##_arena.ptr) + id; } \ -static inline type##Id name##id(type *ptr) { return (unsigned) { ptr - ((type *)name##_arena.ptr) }; } +static inline type##Id name##id(type *ptr) { return (unsigned) { ptr - ((type *)name##_arena.ptr) }; } \ +static inline type *name##_copy(type *ptr) { type *x = name##_alloc(); memcpy(x, ptr, sizeof(type)); return x; } diff --git a/test/test_suite/bitstruct/bitstruct_general.c3 b/test/test_suite/bitstruct/bitstruct_general.c3 index 932c01611..eedaa5c7b 100644 --- a/test/test_suite/bitstruct/bitstruct_general.c3 +++ b/test/test_suite/bitstruct/bitstruct_general.c3 @@ -20,12 +20,11 @@ bitstruct BitField2 : char struct Packet { - bitstruct bitfield : int + bitstruct : int { - int a : 3; - int b : 3; - int c : 2; - int pad : 24; + int a : 0..2; + int b : 3..5; + int c : 5..6; } int packet_id; } diff --git a/test/test_suite/define/common.c3 b/test/test_suite/define/common.c3 index 5147e7197..1259e110a 100644 --- a/test/test_suite/define/common.c3 +++ b/test/test_suite/define/common.c3 @@ -1,109 +1,17 @@ -// #skip - - - -struct FooInt = Foo(int); -func void someFunctionIntBool = someFunction(int, bool); -// const int A_CONST_INT = A_CONST(int); DOESN'T WORK -// void* standard_foo = __stdin; DOESN'T WORK - - -// Define as -define __stdin as standard_foo; -define Foo(int) as FooInt; -define void someFunction(int, bool) as someFunctionIntBool; -define A_CONST(int) as A_CONST_INT; - - -// aliasof -void* standard_foo aliasof __stdin; -func void someFunctionIntBool aliasof someFunction(int, bool); -struct FooInt aliasof Foo(int); -A_CONST_INT aliasof A_CONST(int); - +module foo; // define = define standard_foo = __stdin; -define someFunctionIntBool = someFunction(int, bool); -define FooInt = Foo(int); -define A_CONST_INT = A_CONST(int); +define someFunctionIntBool = someFunction; +define FooInt = Foo; +define A_CONST_INT = A_CONST; -// :: -void* standard_foo :: __stdin; -func void someFunctionIntBool :: someFunction(int, bool); -struct FooInt :: Foo(int); -const int A_CONST_INT :: A_CONST(int); - - - -struct IntArray :: VarArray(int); -func IntArray* newIntArray :: var_array::newArray(int) -const int MAX_INT_CAPACITY ::MAX_INT_CAPACITY(int); - -func void foo() -{ - IntArray* array = newIntArray(); - array.append(10); -} - - -struct IntArray :: VarArray(int); -func IntArray* newIntArray :: var_array::newArray(int) -const int MAX_INT_CAPACITY ::MAX_INT_CAPACITY(int); - -func void foo() -{ - IntArray* array = newIntArray(); - array.append(10); -} - - - -struct IntArray :: VarArray(int); -func IntArray* newIntArray(int capacity) :: var_array::newArray(int) -const int MAX_INT_CAPACITY :: var_array::MAX_CAPACITY(int); - -func void foo(int max_elements) -{ - assert(max_elements <= MAX_INT_CAPACITY) - IntArray* array = newIntArray(max_elements); - array.append(10); -} - - - -define IntArray = VarArray(int); -define newIntArray = var_array::newArray; -define MAX_INT_CAPACITY = var_array::MAX_INT_CAPACITY(int); -define FOOBAR = 123; - -func void foo(int max_elements) -{ - assert(max_elements <= MAX_INT_CAPACITY) - IntArray* array = newIntArray(max_elements); - array.append(10); -} - - -void* standard_foo aliasof __stdin; -func void someFunctionIntBool aliasof someFunction(int, bool); -struct FooInt aliasof Foo(int); -A_CONST_INT aliasof A_CONST(int); - -func void foo(int max_elements) -{ - assert(max_elements <= MAX_INT_CAPACITY) - IntArray* array = newIntArray(max_elements); - array.append(10); -} - -struct IntArray = VarArray(int); -func newIntArray = var_array::newArray; -const MAX_INT_CAPACITY = var_array::MAX_INT_CAPACITY(int); - -func void foo(int max_elements) -{ - assert(max_elements <= MAX_INT_CAPACITY) - IntArray* array = newIntArray(max_elements); - array.append(10); -} +define standard_foo = ofke; // #error: Expected '=' +define func foo = fef; // #error: An identifier was expected here. +define feokfe = func void(int); // #error: Expected a function or variable name here +define AOFKE = ofek; // #error: Expected a constant name here +define okfoe = OFKEOK; // #error: Expected a function or variable name here +define Helo = helo; // #error: A type name was expected here +define Helo = OFKE; // #error: A type name was expected here +define helo = Helo; // #error: Expected a function or variable name here +define HELO = Helo; // #error: Expected a constant name here diff --git a/test/test_suite/generic/generic_copy.c3t b/test/test_suite/generic/generic_copy.c3t new file mode 100644 index 000000000..7bbaae001 --- /dev/null +++ b/test/test_suite/generic/generic_copy.c3t @@ -0,0 +1,12 @@ +module foo; + +func void abc() +{ + int i; + defer { i++; } +} + +module tester; +import foo; + +define abc_my = abc; \ No newline at end of file diff --git a/test/test_suite/globals/incr_array.c3 b/test/test_suite/globals/incr_array.c3 deleted file mode 100644 index 0a606649d..000000000 --- a/test/test_suite/globals/incr_array.c3 +++ /dev/null @@ -1,75 +0,0 @@ -// #skip - -int[+] a; - -a += 10; - -public func int main() -{ - a += 20; // @error{cannot add values to incremental array in function scope} - return 0; -} -/* -const int[+] A; -A += 10; -A += 20; -A += 30; -*/ - -int[+] b; - -func void test1() -{ - int[+] d; // @error{incremental arrays not allowed in function scope} -} - -int[+] c; - -func void test2() -{ - b += 10; // @error{cannot add values to incremental array in function scope} -} - -int[+] x = {}; // @error{incremental array cannot have initializer} - - -int g; -char[2] h = { 1, 2 }; - -g += 10; // @error{'a' is not an incremental array} - -xyz += 20; // @error{module test has no symbol b} - -h += 30; // @error{'d' is not an incremental array} - -test2 += 20; // @error{'main' is not an incremental array} - -int[+] i; - -i += xyz; // @error{use of undeclared identifier c} - -a += test2; // @error{invalid type conversion from 'i32 ()' to 'i32'} - -int[+] k; - -k += 1; -k += 2; -k += 3; - -public func void test3() -{ - int c = a; // @error{invalid type conversion from 'i32[3]' to 'i32'} -} - -struct Point -{ - int x; - int y; -} - -Point[+] points; - -points += { 10, 11 }; -points += { 20, main }; // @error{invalid type conversion from 'i32 ()' to 'i32'} -points += { 30, 31 }; - diff --git a/test/test_suite/globals/incr_enum.c3 b/test/test_suite/globals/incr_enum.c3 deleted file mode 100644 index 1cf637ffc..000000000 --- a/test/test_suite/globals/incr_enum.c3 +++ /dev/null @@ -1,17 +0,0 @@ -// #skip - -type State enum u8 { - A, - B, -} - -State += 10; // @error{expected identifier after incremental enum} - - -type State enum u8 { - A, - B, -} - -State += C; -State += D;