forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
290a344
commit ceebb8a
Showing
4 changed files
with
92 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters