From ce97e07653230d37a40f6ec11a1c2e694736ee84 Mon Sep 17 00:00:00 2001 From: Akuli Date: Fri, 10 Jan 2025 20:13:22 +0200 Subject: [PATCH] Delete compiler/free.jou and move its contents elsewhere (#588) * free_tokens * free_constant * free_signature * free_export_symbol * free_file_types --- compiler/cf_graph.jou | 7 +++--- compiler/free.jou | 43 ----------------------------------- compiler/main.jou | 6 ++--- compiler/structs.jou | 17 +++++++++----- compiler/token.jou | 7 ++++++ compiler/typecheck/common.jou | 21 +++++++++++++++++ compiler/types.jou | 7 ++++-- 7 files changed, 50 insertions(+), 58 deletions(-) delete mode 100644 compiler/free.jou diff --git a/compiler/cf_graph.jou b/compiler/cf_graph.jou index fc30b96f..aef50992 100644 --- a/compiler/cf_graph.jou +++ b/compiler/cf_graph.jou @@ -8,7 +8,6 @@ import "./errors_and_warnings.jou" import "./structs.jou" import "./typecheck/common.jou" import "./print.jou" -import "./free.jou" import "./types.jou" @@ -196,11 +195,11 @@ class CfInstruction: def free(self) -> None: if self->kind == CfInstructionKind::Constant: - free_constant(&self->constant) + self->constant.free() if self->kind == CfInstructionKind::StringArray: free(self->strarray.str) if self->kind == CfInstructionKind::Call: - free_signature(&self->signature) + self->signature.free() free(self->operands) def add_operand(self, operand: LocalVariable*) -> None: @@ -291,7 +290,7 @@ class CfGraph: printf("\n") def free(self) -> None: - free_signature(&self->signature) + self->signature.free() for b = self->all_blocks; b < &self->all_blocks[self->n_all_blocks]; b++: (*b)->free() if *b != &self->start_block and *b != &self->end_block: diff --git a/compiler/free.jou b/compiler/free.jou deleted file mode 100644 index bf697508..00000000 --- a/compiler/free.jou +++ /dev/null @@ -1,43 +0,0 @@ -# Boring boilerplate code to free up data structures used in compilation. - -import "stdlib/mem.jou" - -import "./structs.jou" -import "./types.jou" -import "./token.jou" -import "./typecheck/common.jou" - -def free_tokens(tokenlist: Token*) -> None: - for t = tokenlist; t->kind != TokenKind::EndOfFile; t++: - if t->kind == TokenKind::String: - free(t->long_string) - free(tokenlist) - -def free_constant(c: Constant*) -> None: - if c->kind == ConstantKind::String: - free(c->str) - -def free_signature(sig: Signature*) -> None: - free(sig->argnames) - free(sig->argtypes) - -def free_export_symbol(es: ExportSymbol*) -> None: - if es->kind == ExportSymbolKind::Function: - free_signature(&es->funcsignature) - -def free_file_types(ft: FileTypes*) -> None: - for t = ft->owned_types; t < &ft->owned_types[ft->n_owned_types]; t++: - free_type(*t) - for func = ft->functions; func < &ft->functions[ft->nfunctions]; func++: - free_signature(&func->signature) - for fom = ft->fomtypes; fom < &ft->fomtypes[ft->nfomtypes]; fom++: - for et = fom->expr_types; et < &fom->expr_types[fom->n_expr_types]; et++: - free(*et) - free(fom->expr_types) - free(fom->locals) # Don't free individual locals because they're owned by CFG now - free_signature(&fom->signature) - free(ft->globals) - free(ft->types) - free(ft->owned_types) - free(ft->functions) - free(ft->fomtypes) diff --git a/compiler/main.jou b/compiler/main.jou index 3e7a1393..743862e7 100644 --- a/compiler/main.jou +++ b/compiler/main.jou @@ -18,7 +18,7 @@ import "./typecheck/step2_populate_types.jou" import "./typecheck/step3_function_and_method_bodies.jou" import "./target.jou" import "./types.jou" -import "./free.jou" +import "./token.jou" import "./parser.jou" import "./paths.jou" import "./errors_and_warnings.jou" @@ -189,7 +189,7 @@ class FileState: def free(self) -> None: self->ast.free() free(self->path) - free_file_types(&self->types) + self->types.free() def add_imported_symbol(self, es: ExportSymbol*, imp: AstImport*) -> None: for i = 0; i < self->ast.body.nstatements; i++: @@ -390,7 +390,7 @@ class CompileState: for i = 0; i < self->nfiles; i++: for es = pending_exports[i]; es->name[0] != '\0'; es++: - free_export_symbol(es) + es->free() free(pending_exports[i]) pending_exports[i] = NULL diff --git a/compiler/structs.jou b/compiler/structs.jou index 9a36e662..c5272688 100644 --- a/compiler/structs.jou +++ b/compiler/structs.jou @@ -1,6 +1,7 @@ # TODO: delete this file, merge into others import "stdlib/str.jou" +import "stdlib/mem.jou" import "./types.jou" @@ -48,6 +49,16 @@ class Constant: boolean: bool enum_member: EnumMemberConstant + def free(self) -> None: + if self->kind == ConstantKind::String: + free(self->str) + + def copy(self: Constant) -> Constant: + if self.kind == ConstantKind::String: + self.str = strdup(self.str) + assert self.str != NULL + return self + def get_type(self) -> Type*: if self->kind == ConstantKind::EnumMember: return self->enum_member.enumtype @@ -65,12 +76,6 @@ class Constant: return get_integer_type(self->integer.size_in_bits, self->integer.is_signed) assert False - def copy(self: Constant) -> Constant: - if self.kind == ConstantKind::String: - self.str = strdup(self.str) - assert self.str != NULL - return self - def int_constant(type: Type*, value: long) -> Constant: assert type->is_integer_type() diff --git a/compiler/token.jou b/compiler/token.jou index 008b1369..f8aab982 100644 --- a/compiler/token.jou +++ b/compiler/token.jou @@ -137,3 +137,10 @@ class Token: message: byte* = malloc(strlen(what_was_expected_instead) + 500) sprintf(message, "expected %s, got %s", what_was_expected_instead, got) fail(self->location, message) + + +def free_tokens(tokenlist: Token*) -> None: + for t = tokenlist; t->kind != TokenKind::EndOfFile; t++: + if t->kind == TokenKind::String: + free(t->long_string) + free(tokenlist) diff --git a/compiler/typecheck/common.jou b/compiler/typecheck/common.jou index 4663b135..2cda6377 100644 --- a/compiler/typecheck/common.jou +++ b/compiler/typecheck/common.jou @@ -59,6 +59,10 @@ class ExportSymbol: funcsignature: Signature type: Type* # ExportSymbolKind::Type and ExportSymbolKind::GlobalVar + def free(self) -> None: + if self->kind == ExportSymbolKind::Function: + self->funcsignature.free() + # Type information about a function or method defined in the current file. # Not created for anything imported from another file. @@ -114,6 +118,23 @@ class FileTypes: functions: SignatureAndUsedPtr* nfunctions: int + def free(self) -> None: + for t = self->owned_types; t < &self->owned_types[self->n_owned_types]; t++: + free_type(*t) + for func = self->functions; func < &self->functions[self->nfunctions]; func++: + func->signature.free() + for fom = self->fomtypes; fom < &self->fomtypes[self->nfomtypes]; fom++: + for et = fom->expr_types; et < &fom->expr_types[fom->n_expr_types]; et++: + free(*et) + free(fom->expr_types) + free(fom->locals) # Don't free individual locals because they're owned by CFG now + fom->signature.free() + free(self->globals) + free(self->types) + free(self->owned_types) + free(self->functions) + free(self->fomtypes) + def find_type(self, name: byte*) -> Type*: for t = self->types; t < &self->types[self->ntypes]; t++: if strcmp(t->type->name, name) == 0: diff --git a/compiler/types.jou b/compiler/types.jou index 0e1d7735..4c942b07 100644 --- a/compiler/types.jou +++ b/compiler/types.jou @@ -2,7 +2,6 @@ import "stdlib/mem.jou" import "stdlib/str.jou" import "./errors_and_warnings.jou" -import "./free.jou" class ClassField: name: byte[100] @@ -186,7 +185,7 @@ def free_type(t: Type*) -> None: assert t != NULL if t->kind == TypeKind::Class: for m = t->classdata.methods; m < &t->classdata.methods[t->classdata.nmethods]; m++: - free_signature(m) + m->free() free(t->classdata.fields) free(t->classdata.methods) @@ -246,6 +245,10 @@ class Signature: # TODO: rename to return_type_location returntype_location: Location # meaningful even if returntype is NULL + def free(self) -> None: + free(self->argnames) + free(self->argtypes) + def get_self_class(self) -> Type*: if self->nargs > 0 and strcmp(self->argnames[0], "self") == 0: if self->argtypes[0]->kind == TypeKind::Pointer: