Skip to content

Commit

Permalink
cleaning up some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
grantnelson-wf committed Sep 10, 2024
1 parent 4d7c131 commit 3230218
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
25 changes: 18 additions & 7 deletions compiler/internal/dce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,28 @@ then both names will be used to make the unexported method alive.
Since the unexported method is only visible in the package in which it is
defined, the package path is included in the name.

| Declaration | exported | unexported | non-generic | generic instance | primary name | secondary name |
|:------------|:--------:|:----------:|:-----------:|:----------------:|:-------------|:---------------|
| Declaration | exported | unexported | non-generic | generic | primary name | secondary name |
|:------------|:--------:|:----------:|:-----------:|:-------:|:-------------|:---------------|
| variables | x | x | x | - | `<package path>.<var name>` | - |
| functions | x | x | x | | `<package path>.<func name>` | - |
| functions | x | x | | x | `<package path>.<func name>[<instance type list>]` | - |
| functions | x | x | | x | `<package path>.<func name>[<type arguments>]` | - |
| named type | x | x | x | | `<package>.<type name>` | - |
| named type | x | x | | x | `<package>.<type name>[<instance type list>]` | - |
| named type | x | x | | x | `<package>.<type name>[<type arguments>]` | - |
| method | x | | x | | `<package>.<receiver name>` | - |
| method | x | | | x | `<package>.<receiver name>[<instance type list>]` | - |
| method | | x | x | | `<package>.<receiver name>` | `<package path>.<method name>(<parameter type list><variadic indicator>)(<result type list>)` |
| method | | x | | x | `<package>.<receiver name>[<instance type list>]` | `<package path>.<method name>(<parameter type list><variadic indicator>)(<result type list>)` |
| method | x | | | x | `<package>.<receiver name>[<type arguments>]` | - |
| method | | x | x | | `<package>.<receiver name>` | `<package path>.<method name>(<parameter types>)(<result types>)` |
| method | | x | | x | `<package>.<receiver name>[<type arguments>]` | `<package path>.<method name>(<parameter types>)(<result types>)` |

Notes:

- The parameter types include the veridic indicator, e.g. `sum(...int)int`.
- The result types only need parenthesis if there are more than one.
- The type arguments are either the instance types or the parameter types
since the instance type could be a match for the parameter type on the
generic, e.g. `type Foo[T any] struct{}; type Bar[B any] { f Foo[B] }`.
Most compiles will not have any instances that aren't concrete types,
but compiling packages may result in instances carrying generic types.
- These names can get long.

### Dependencies

Expand Down
9 changes: 7 additions & 2 deletions compiler/internal/dce/dce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func Test_Info_SetNameAndDep(t *testing.T) {
}`),
want: Info{
objectFilter: `jim.Aughra`,
methodFilter: `jim.frank(...jim.Aughra)(int)`,
methodFilter: `jim.frank(...jim.Aughra)int`,
},
},
{
Expand All @@ -218,7 +218,7 @@ func Test_Info_SetNameAndDep(t *testing.T) {
}`),
want: Info{
objectFilter: `jim.Aughra[~float64]`,
methodFilter: `jim.frank(*jim.Aughra[float64])(bool)`,
methodFilter: `jim.frank(*jim.Aughra[float64])bool`,
},
},
{
Expand All @@ -241,6 +241,11 @@ func Test_Info_SetNameAndDep(t *testing.T) {
// - Embedded instance in type (e.g. `type X[T any] struct{v T}; type Y[T any] struct{X[T]}; var v = Y[int]{X: X[int]{v: 42}}.X.v`)
// - Embedded interface in struct providing the type with an exposed method.
// - Type params that are self referencing (e.g. `func Keys[K comparable, V any, M ~map[K]V](m M) { ** }`)
// - Recursively converting type parameters. `type Foo[T ~Bar[map[T]string]] struct {}`
// - Check if recursive type parameters can occur and handle them correctly.
// - Types referencing other types, `type Foo[T comparable, S ~[]T, M ~map[T]S] struct {}`
// - Type instances with unions and tilde.
// - Type instancing with a type parameter that matches the constraint, `type Foo[F int|string] struct {}; func Bar[B int|string]()Foo[B]{ return Foo[B]{} }`
}

t.Run(`SetName`, func(t *testing.T) {
Expand Down
14 changes: 12 additions & 2 deletions compiler/internal/dce/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/gopherjs/gopherjs/compiler/typesutil"
)

// getFilters determines the DCE filters for the given object.
// This will return an object filter and optionally return a method filter.
func getFilters(o types.Object) (objectFilter, methodFilter string) {
if typesutil.IsMethod(o) {
recv := typesutil.RecvType(o.Type().(*types.Signature)).Obj()
Expand All @@ -20,9 +22,13 @@ func getFilters(o types.Object) (objectFilter, methodFilter string) {
return
}

// getObjectFilter returns the object filter that functions as the primary
// name when determining if a declaration is alive or not.
// See [naming design](./README.md#naming) for more information.
func getObjectFilter(o types.Object) string {
filter := o.Pkg().Path() + `.` + o.Name()

// Add additional type information for generics and instances.
if named, ok := o.Type().(*types.Named); ok {
if typeArgs := named.TypeArgs(); typeArgs != nil {
filter += getInstanceTypeFilterPart(typeArgs)
Expand All @@ -43,7 +49,6 @@ func getInstanceTypeFilterPart(instTypes *types.TypeList) string {
}

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())
Expand All @@ -55,7 +60,12 @@ 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 {
switch sig.Results().Len() {
case 0:
break
case 1:
filter += getTypeFilterPart(sig.Results().At(0).Type())
default:
filter += `(` + getTupleFilterPart(sig.Results(), false) + `)`
}
return filter
Expand Down

0 comments on commit 3230218

Please sign in to comment.