From ea4852079fd4cfee06e3fd803aee088b5af1be78 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 17 Jul 2026 13:03:05 +0200 Subject: [PATCH] Implemented deallocation of new identifiers in scopes --- examples/fnl/fnl.q | 1 - lib/fs/readFile.q | 1 - lib/wayland/connect.q | 1 - src/core/compileIf.go | 6 ++++++ src/core/compileReturn.go | 4 ++-- src/core/compileReturnTuple.go | 2 +- src/core/compileSwitch.go | 4 ++++ src/core/deleteResources.go | 23 ++++++++++++++++++----- src/core/optimize.go | 2 +- 9 files changed, 32 insertions(+), 12 deletions(-) diff --git a/examples/fnl/fnl.q b/examples/fnl/fnl.q index e49a08d..758b7ee 100644 --- a/examples/fnl/fnl.q +++ b/examples/fnl/fnl.q @@ -93,7 +93,6 @@ processFile(path string, mode int) { io.writeLine("final newline [added]") newSource := addNewline(source) fs.writeFile(path, newSource) - delete(newSource) } else { io.writeLine("no final newline") } diff --git a/lib/fs/readFile.q b/lib/fs/readFile.q index c588140..8b9270c 100644 --- a/lib/fs/readFile.q +++ b/lib/fs/readFile.q @@ -29,7 +29,6 @@ readFile(path string) -> (!string, error) { n, err := io.readFrom(fd, buffer[pos..]) if err != 0 { - delete(buffer) close(fd) return "", err } diff --git a/lib/wayland/connect.q b/lib/wayland/connect.q index 296355c..ff884af 100644 --- a/lib/wayland/connect.q +++ b/lib/wayland/connect.q @@ -8,7 +8,6 @@ connect() -> (!uint, error) { } socket, err := unix.connect(path) - delete(path) if err != 0 { return 0, err diff --git a/src/core/compileIf.go b/src/core/compileIf.go index 95490a9..0a4bcff 100644 --- a/src/core/compileIf.go +++ b/src/core/compileIf.go @@ -22,12 +22,14 @@ func (f *Function) compileIf(branch *ast.If) error { // Append the if.then block f.AddBlock(thenBlock) + before := f.Block().Identifiers.Before err = f.compileAST(branch.Body) if err != nil { return err } + f.deleteResources(before) f.jump(exitBlock) } else { elseLabel := f.CreateLabel("if.else", f.Count.Branch) @@ -38,6 +40,8 @@ func (f *Function) compileIf(branch *ast.If) error { return err } + before := f.Block().Identifiers.After + // Append the if.then block f.AddBlock(thenBlock) err = f.compileAST(branch.Body) @@ -46,6 +50,7 @@ func (f *Function) compileIf(branch *ast.If) error { return err } + f.deleteResources(before) f.jump(exitBlock) // Append the if.else block @@ -56,6 +61,7 @@ func (f *Function) compileIf(branch *ast.If) error { return err } + f.deleteResources(before) f.jump(exitBlock) } diff --git a/src/core/compileReturn.go b/src/core/compileReturn.go index 0e2b228..9490554 100644 --- a/src/core/compileReturn.go +++ b/src/core/compileReturn.go @@ -25,7 +25,7 @@ func (f *Function) compileReturn(node *ast.Return) error { } if len(node.Values) == 0 { - f.deleteResources() + f.deleteResources(nil) f.Append(&ssa.Return{}) return nil } @@ -36,7 +36,7 @@ func (f *Function) compileReturn(node *ast.Return) error { return err } - f.deleteResources() + f.deleteResources(nil) f.Append(&ssa.Return{Arguments: args}) return nil } \ No newline at end of file diff --git a/src/core/compileReturnTuple.go b/src/core/compileReturnTuple.go index 5a4a514..486ee20 100644 --- a/src/core/compileReturnTuple.go +++ b/src/core/compileReturnTuple.go @@ -32,7 +32,7 @@ func (f *Function) compileReturnTuple(node *ast.Return) error { return errors.NewAt(&ReturnCountMismatch{Count: len(args), ExpectedCount: len(f.Output)}, f.File, node.Values[0].Source().Start()) } - f.deleteResources() + f.deleteResources(nil) f.Append(&ssa.Return{Arguments: args}) return nil } \ No newline at end of file diff --git a/src/core/compileSwitch.go b/src/core/compileSwitch.go index e1043b5..304550a 100644 --- a/src/core/compileSwitch.go +++ b/src/core/compileSwitch.go @@ -13,12 +13,14 @@ func (f *Function) compileSwitch(s *ast.Switch) error { for i, branch := range s.Cases { if branch.Condition == nil { + before := f.Block().Identifiers.Before err := f.compileAST(branch.Body) if err != nil { return err } + f.deleteResources(before) f.jump(exitBlock) f.AddBlock(exitBlock) break @@ -43,12 +45,14 @@ func (f *Function) compileSwitch(s *ast.Switch) error { } f.AddBlock(thenBlock) + before := f.Block().Identifiers.Before err = f.compileAST(branch.Body) if err != nil { return err } + f.deleteResources(before) f.jump(exitBlock) f.AddBlock(elseBlock) } diff --git a/src/core/deleteResources.go b/src/core/deleteResources.go index 4004d7e..d27b9d0 100644 --- a/src/core/deleteResources.go +++ b/src/core/deleteResources.go @@ -8,17 +8,30 @@ import ( ) // deleteResources inserts delete calls for all resources. -func (f *Function) deleteResources() { - identifiers := f.Block().Identifiers.After +func (f *Function) deleteResources(filter map[string]ssa.Value) { + var ( + names []string + identifiers = f.Block().Identifiers.After + ) if len(identifiers) == 0 { return } - names := make([]string, 0, len(identifiers)) + for name := range identifiers { + if filter != nil { + _, inherited := filter[name] - for k := range identifiers { - names = append(names, k) + if inherited { + continue + } + } + + if names == nil { + names = make([]string, 0, len(identifiers)) + } + + names = append(names, name) } slices.SortFunc(names, func(a string, b string) int { diff --git a/src/core/optimize.go b/src/core/optimize.go index 1c53575..6cde9f2 100644 --- a/src/core/optimize.go +++ b/src/core/optimize.go @@ -22,7 +22,7 @@ func (f *Function) optimize() error { return errors.New(&ReturnCountMismatch{Count: 0, ExpectedCount: len(f.Output)}, f.File, f.Output[0].Source) } - f.deleteResources() + f.deleteResources(nil) f.Block().Append(&ssa.Return{}) } -- 2.51.2