Skip to content

Commit

Permalink
cleaning up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
grantnelson-wf committed Sep 10, 2024
1 parent 290a344 commit ceebb8a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 87 deletions.
5 changes: 3 additions & 2 deletions compiler/internal/dce/dce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func Test_Collector_Collecting(t *testing.T) {
depCount(t, decl1, 2)
depCount(t, decl2, 3)

// The second collection overwrites the first collection.
// The second collection adds to existing dependencies.
c.CollectDCEDeps(decl2, func() {
c.DeclareDCEDep(obj4)
c.DeclareDCEDep(obj5)
})
depCount(t, decl1, 2)
depCount(t, decl2, 1)
depCount(t, decl2, 4)
}

func Test_Info_SetNameAndDep(t *testing.T) {
Expand Down
81 changes: 81 additions & 0 deletions compiler/internal/dce/filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package dce

import (
"go/types"
"strings"

"github.com/gopherjs/gopherjs/compiler/typesutil"
)

func getFilters(o types.Object) (objectFilter, methodFilter string) {
if typesutil.IsMethod(o) {
recv := typesutil.RecvType(o.Type().(*types.Signature)).Obj()
objectFilter = getObjectFilter(recv)
if !o.Exported() {
methodFilter = getMethodFilter(o)
}
} else {
objectFilter = getObjectFilter(o)
}
return
}

func getObjectFilter(o types.Object) string {
filter := o.Pkg().Path() + `.` + o.Name()

if named, ok := o.Type().(*types.Named); ok {
if typeArgs := named.TypeArgs(); typeArgs != nil {
filter += getInstanceTypeFilterPart(typeArgs)
} else if typeParams := named.TypeParams(); typeParams != nil {
filter += getParameterTypeFilterPart(typeParams)
}
}

return filter
}

func getInstanceTypeFilterPart(instTypes *types.TypeList) string {
parts := make([]string, instTypes.Len())
for i := 0; i < instTypes.Len(); i++ {
parts[i] = getTypeFilterPart(instTypes.At(i))
}
return `[` + strings.Join(parts, `,`) + `]`
}

func getParameterTypeFilterPart(typeParams *types.TypeParamList) string {
// TODO: Handle recursively converting type parameters. `type Foo[T ~Bar[map[T]string]] struct {}`
parts := make([]string, typeParams.Len())
for i := 0; i < typeParams.Len(); i++ {
parts[i] = getTypeFilterPart(typeParams.At(i).Constraint())
}
return `[` + strings.Join(parts, `,`) + `]`
}

func getMethodFilter(o types.Object) string {
sig := o.Type().(*types.Signature)
filter := o.Pkg().Path() + `.` + o.Name() +
`(` + getTupleFilterPart(sig.Params(), sig.Variadic()) + `)`
if sig.Results().Len() > 0 {
filter += `(` + getTupleFilterPart(sig.Results(), false) + `)`
}
return filter
}

func getTupleFilterPart(t *types.Tuple, variadic bool) string {
parts := make([]string, t.Len())
for i := range parts {
if i == t.Len()-1 && variadic {
parts[i] = `...` + getTypeFilterPart(t.At(i).Type().(*types.Slice).Elem())
} else {
parts[i] = getTypeFilterPart(t.At(i).Type())
}
}
return strings.Join(parts, `,`)
}

func getTypeFilterPart(t types.Type) string {
if o, ok := t.(types.Object); ok {
return getObjectFilter(o)
}
return t.String()
}
75 changes: 0 additions & 75 deletions compiler/internal/dce/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"go/types"
"sort"
"strings"

"github.com/gopherjs/gopherjs/compiler/typesutil"
)

// Info contains information used by the dead-code elimination (DCE) logic to
Expand Down Expand Up @@ -127,76 +125,3 @@ func (id *Info) getDeps() []string {
sort.Strings(deps)
return deps
}

func getFilters(o types.Object) (objectFilter, methodFilter string) {
if typesutil.IsMethod(o) {
recv := typesutil.RecvType(o.Type().(*types.Signature)).Obj()
objectFilter = getObjectFilter(recv)
if !o.Exported() {
methodFilter = getMethodFilter(o)
}
} else {
objectFilter = getObjectFilter(o)
}
return
}

func getObjectFilter(o types.Object) string {
filter := o.Pkg().Path() + `.` + o.Name()

if named, ok := o.Type().(*types.Named); ok {
if typeArgs := named.TypeArgs(); typeArgs != nil {
filter += getInstanceTypeFilterPart(typeArgs)
} else if typeParams := named.TypeParams(); typeParams != nil {
filter += getParameterTypeFilterPart(typeParams)
}
}

return filter
}

func getInstanceTypeFilterPart(instTypes *types.TypeList) string {
parts := make([]string, instTypes.Len())
for i := 0; i < instTypes.Len(); i++ {
parts[i] = getTypeFilterPart(instTypes.At(i))
}
return `[` + strings.Join(parts, `,`) + `]`
}

func getParameterTypeFilterPart(typeParams *types.TypeParamList) string {
// TODO: Handle recursively converting type parameters. `type Foo[T ~Bar[map[T]string]] struct {}`
parts := make([]string, typeParams.Len())
for i := 0; i < typeParams.Len(); i++ {
parts[i] = getTypeFilterPart(typeParams.At(i).Constraint())
}
return `[` + strings.Join(parts, `,`) + `]`
}

func getMethodFilter(o types.Object) string {
sig := o.Type().(*types.Signature)
filter := o.Pkg().Path() + `.` + o.Name() +
`(` + getTupleFilterPart(sig.Params(), sig.Variadic()) + `)`
if sig.Results().Len() > 0 {
filter += `(` + getTupleFilterPart(sig.Results(), false) + `)`
}
return filter
}

func getTupleFilterPart(t *types.Tuple, variadic bool) string {
parts := make([]string, t.Len())
for i := range parts {
if i == t.Len()-1 && variadic {
parts[i] = `...` + getTypeFilterPart(t.At(i).Type().(*types.Slice).Elem())
} else {
parts[i] = getTypeFilterPart(t.At(i).Type())
}
}
return strings.Join(parts, `,`)
}

func getTypeFilterPart(t types.Type) string {
if o, ok := t.(types.Object); ok {
return getObjectFilter(o)
}
return t.String()
}
18 changes: 8 additions & 10 deletions compiler/internal/dce/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,16 @@ func (s *Selector[D]) Include(decl D, implementsLink bool) {
s.pendingDecls = append(s.pendingDecls, decl)
}

info := &declInfo[D]{
decl: decl,
objectFilter: dce.objectFilter,
methodFilter: dce.methodFilter,
info := &declInfo[D]{decl: decl}

if dce.objectFilter != `` {
info.objectFilter = dce.objectFilter
s.byFilter[info.objectFilter] = append(s.byFilter[info.objectFilter], info)
}
s.addFilterInfo(info.objectFilter, info)
s.addFilterInfo(info.methodFilter, info)
}

func (s *Selector[D]) addFilterInfo(filter string, info *declInfo[D]) {
if len(filter) > 0 {
s.byFilter[filter] = append(s.byFilter[filter], info)
if dce.methodFilter != `` {
info.methodFilter = dce.methodFilter
s.byFilter[info.methodFilter] = append(s.byFilter[info.methodFilter], info)
}
}

Expand Down

0 comments on commit ceebb8a

Please sign in to comment.