Something went wrong. Try again.
🌱 A minimal programming language and compiler. git.urbach.dev/cli/q
high-performance programming-language compiler
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798package core
import ( "slices"
"git.urbach.dev/cli/q/src/ssa" "git.urbach.dev/cli/q/src/types")
// deleteResources inserts delete calls for all resources.func (f *Function) deleteResources(filter map[string]ssa.Value) { var ( names []string identifiers = f.Block().Identifiers.After.Raw() )
if len(identifiers) == 0 { return }
for name := range identifiers { if filter != nil { _, inherited := filter[name]
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 { aValue := identifiers[a] bValue := identifiers[b]
aStruct, aIsStruct := aValue.(*ssa.Struct)
if aIsStruct && len(aStruct.Arguments) > 0 { aValue = aStruct.Arguments[0] }
bStruct, bIsStruct := bValue.(*ssa.Struct)
if bIsStruct && len(bStruct.Arguments) > 0 { bValue = bStruct.Arguments[0] }
for _, block := range f.IR.Blocks { aIndex := block.Index(aValue) bIndex := block.Index(bValue)
switch { case aIndex != -1 && bIndex != -1: return bIndex - aIndex case aIndex != -1: return 1 case bIndex != -1: return -1 } }
panic("non-deterministic memory deallocation order") })
for _, name := range names { value := identifiers[name]
if value == nil { continue }
resource, isResource := value.Type().(*types.Resource)
if !isResource { continue }
_, isPointer := resource.Of.(*types.Pointer) _, isStruct := resource.Of.(*types.Struct)
if !isPointer && !isStruct { continue }
_, isParam := value.(*ssa.Parameter)
if isParam { continue }
f.delete(value) }}