Skip to content

Commit

Permalink
Delete compiler/free.jou and move its contents elsewhere (#588)
Browse files Browse the repository at this point in the history
* free_tokens

* free_constant

* free_signature

* free_export_symbol

* free_file_types
  • Loading branch information
Akuli authored Jan 10, 2025
1 parent da700b1 commit ce97e07
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 58 deletions.
7 changes: 3 additions & 4 deletions compiler/cf_graph.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 0 additions & 43 deletions compiler/free.jou

This file was deleted.

6 changes: 3 additions & 3 deletions compiler/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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++:
Expand Down Expand Up @@ -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

Expand Down
17 changes: 11 additions & 6 deletions compiler/structs.jou
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# TODO: delete this file, merge into others

import "stdlib/str.jou"
import "stdlib/mem.jou"

import "./types.jou"

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions compiler/token.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 21 additions & 0 deletions compiler/typecheck/common.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions compiler/types.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ce97e07

Please sign in to comment.