From 0a8a63bc15ba88928a7051a799e883a75c92d599 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 26 Jun 2024 11:43:14 +0200 Subject: [PATCH] Fix to headergen. Updated module name store. --- src/compiler/compiler.c | 7 ++++++- src/compiler/compiler_internal.h | 1 + src/compiler/context.c | 4 ++-- src/compiler/headers.c | 6 ++---- src/compiler/module.c | 1 + src/compiler/parse_global.c | 19 ++++++------------- src/compiler/sema_decls.c | 1 + 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 1f02c82a5..0edc6aa2c 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -1154,8 +1154,13 @@ Module *compiler_find_or_create_module(Path *module_name, const char **parameter const char *scratch_buffer_interned(void) { TokenType type = TOKEN_INVALID_TOKEN; + return scratch_buffer_interned_as(&type); +} + +const char *scratch_buffer_interned_as(TokenType* type) +{ return symtab_add(scratch_buffer.str, scratch_buffer.len, - fnv1a(scratch_buffer.str, scratch_buffer.len), &type); + fnv1a(scratch_buffer.str, scratch_buffer.len), type); } File *compile_and_invoke(const char *file, const char *args) diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index e71321293..94b4a15be 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -2408,6 +2408,7 @@ DeclId decltable_get(DeclTable *table, const char *name); void decltable_set(DeclTable *table, Decl *decl); const char *scratch_buffer_interned(void); +const char *scratch_buffer_interned_as(TokenType *type); const char *symtab_preset(const char *data, TokenType type); const char *symtab_add(const char *symbol, uint32_t len, uint32_t fnv1hash, TokenType *type); diff --git a/src/compiler/context.c b/src/compiler/context.c index 75eedcec7..3b52c636c 100644 --- a/src/compiler/context.c +++ b/src/compiler/context.c @@ -98,12 +98,11 @@ bool context_set_module_from_filename(ParseContext *context) bool context_set_module(ParseContext *context, Path *path, const char **generic_parameters) { - // Note that we allow the illegal name for now, to be able to parse further. + if (!str_has_no_uppercase(path->module)) { RETURN_PRINT_ERROR_AT(false, path, "A module name may not have any uppercase characters."); } - return create_module_or_check_name(context->unit, path, generic_parameters); } @@ -263,6 +262,7 @@ ERR: decl_poison(old); } + bool unit_add_import(CompilationUnit *unit, Path *path, bool private_import) { DEBUG_LOG("SEMA: Add import of '%s'.", path->module); diff --git a/src/compiler/headers.c b/src/compiler/headers.c index 567917cb5..e82fbdddd 100644 --- a/src/compiler/headers.c +++ b/src/compiler/headers.c @@ -41,7 +41,6 @@ static void header_print_type(FILE *file, Type *type) return; } assert(!type_is_optional(type)); - type = type_flatten_no_export(type); switch (type->type_kind) { case CT_TYPES: @@ -502,12 +501,11 @@ RETRY: Type *underlying_type = decl->enums.type_info->type->canonical; if (underlying_type == type_cint->canonical) { - PRINTF("typedef enum %s__ %s;\n", decl_get_extname(decl), decl_get_extname(decl)); - PRINTF("enum %s__\n{\n", decl_get_extname(decl)); + PRINTF("typedef enum %s__\n{\n", decl_get_extname(decl)); FOREACH_BEGIN(Decl *enum_member, decl->enums.values) PRINTF("\t %s_%s,\n", decl_get_extname(decl), enum_member->name); FOREACH_END(); - PRINTF("};\n"); + PRINTF("} %s;\n", decl_get_extname(decl)); return; } PRINTF("typedef "); diff --git a/src/compiler/module.c b/src/compiler/module.c index ddc3a1ba6..45f509950 100644 --- a/src/compiler/module.c +++ b/src/compiler/module.c @@ -34,6 +34,7 @@ const char *module_create_object_file_name(Module *module) return scratch_buffer_to_string(); } + Path *path_create_from_string(const char *string, uint32_t len, SourceSpan span) { assert(string); diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 1140c1cb6..cfe2568f2 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -335,36 +335,29 @@ bool parse_path_prefix(ParseContext *c, Path** path_ref) *path_ref = NULL; if (!tok_is(c, TOKEN_IDENT) || peek(c) != TOKEN_SCOPE) return true; - char *scratch_ptr = scratch_buffer.str; - uint32_t offset = 0; - Path *path = CALLOCS(Path); path->span = c->span; - unsigned len = (unsigned)strlen(symstr(c)); - memcpy(scratch_ptr, symstr(c), len); - offset += len; + scratch_buffer_clear(); + scratch_buffer_append(symstr(c)); SourceSpan last_loc = c->span; advance(c); advance(c); while (tok_is(c, TOKEN_IDENT) && peek(c) == TOKEN_SCOPE) { last_loc = c->span; - scratch_ptr[offset++] = ':'; - scratch_ptr[offset++] = ':'; - len = c->data.lex_len; - memcpy(scratch_ptr + offset, symstr(c), len); - offset += len; + scratch_buffer_append("::"); + scratch_buffer_append_len(symstr(c), c->data.lex_len); advance(c); advance(c); } TokenType type = TOKEN_IDENT; path->span = extend_span_with_token(path->span, last_loc); - path->module = symtab_add(scratch_ptr, offset, fnv1a(scratch_ptr, offset), &type); + path->module = scratch_buffer_interned_as(&type); if (type != TOKEN_IDENT) { RETURN_PRINT_ERROR_AT(false, path, "A module name was expected here."); } - path->len = offset; + path->len = scratch_buffer.len; *path_ref = path; return true; } diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index 531e8df38..af54a3f5f 100644 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -992,6 +992,7 @@ static void sema_recursively_import(Type *type) FOREACH_BEGIN(Decl *param, decl->enums.parameters) sema_recursively_import(param->type); FOREACH_END(); + return; case DECL_TYPEDEF: sema_recursively_import(type->canonical); return;