Skip to content

Commit

Permalink
go/ssa: split pkg() into different cases for *Package and *types.Package
Browse files Browse the repository at this point in the history
Splits (*Function).pkg() into different cases for returning a *Package and the *types.Package.

Updates golang/go#48525

Change-Id: I1c59679c5acd898cf2009a2e3a0ea96b62f82a06
Reviewed-on: https://go-review.googlesource.com/c/tools/+/391334
Reviewed-by: Zvonimir Pavlinovic <[email protected]>
Run-TryBot: Tim King <[email protected]>
gopls-CI: kokoro <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Trust: Tim King <[email protected]>
  • Loading branch information
timothy-king committed Mar 11, 2022
1 parent ee31f70 commit 198cae3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
27 changes: 22 additions & 5 deletions go/ssa/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (f *Function) RelString(from *types.Package) string {

// Package-level function?
// Prefix with package name for cross-package references only.
if p := f.pkg(); p != nil && p != from {
if p := f.relPkg(); p != nil && p != from {
return fmt.Sprintf("%s.%s", p.Path(), f.name)
}

Expand Down Expand Up @@ -442,9 +442,26 @@ func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *ty
types.WriteSignature(buf, sig, types.RelativeTo(from))
}

func (f *Function) pkg() *types.Package {
if f.Pkg != nil {
return f.Pkg.Pkg
// declaredPackage returns the package fn is declared in or nil if the
// function is not declared in a package.
func (fn *Function) declaredPackage() *Package {
switch {
case fn.Pkg != nil:
return fn.Pkg // non-generic function
// generics:
// case fn.Origin != nil:
// return fn.Origin.pkg // instance of a named generic function
case fn.parent != nil:
return fn.parent.declaredPackage() // instance of an anonymous [generic] function
default:
return nil // function is not declared in a package, e.g. a wrapper.
}
}

// relPkg returns types.Package fn is printed in relationship to.
func (fn *Function) relPkg() *types.Package {
if p := fn.declaredPackage(); p != nil {
return p.Pkg
}
return nil
}
Expand Down Expand Up @@ -479,7 +496,7 @@ func WriteFunction(buf *bytes.Buffer, f *Function) {
fmt.Fprintf(buf, "# Recover: %s\n", f.Recover)
}

from := f.pkg()
from := f.relPkg()

if f.FreeVars != nil {
buf.WriteString("# Free variables:\n")
Expand Down
18 changes: 9 additions & 9 deletions go/ssa/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func relName(v Value, i Instruction) string {
var from *types.Package
if i != nil {
from = i.Parent().pkg()
from = i.Parent().relPkg()
}
switch v := v.(type) {
case Member: // *Function or *Global
Expand Down Expand Up @@ -66,12 +66,12 @@ func relString(m Member, from *types.Package) string {
// It never appears in disassembly, which uses Value.Name().

func (v *Parameter) String() string {
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("parameter %s : %s", v.Name(), relType(v.Type(), from))
}

func (v *FreeVar) String() string {
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("freevar %s : %s", v.Name(), relType(v.Type(), from))
}

Expand All @@ -86,7 +86,7 @@ func (v *Alloc) String() string {
if v.Heap {
op = "new"
}
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("%s %s (%s)", op, relType(deref(v.Type()), from), v.Comment)
}

Expand Down Expand Up @@ -160,7 +160,7 @@ func (v *UnOp) String() string {
}

func printConv(prefix string, v, x Value) string {
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("%s %s <- %s (%s)",
prefix,
relType(v.Type(), from),
Expand Down Expand Up @@ -191,7 +191,7 @@ func (v *MakeClosure) String() string {
}

func (v *MakeSlice) String() string {
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("make %s %s %s",
relType(v.Type(), from),
relName(v.Len, v),
Expand Down Expand Up @@ -223,12 +223,12 @@ func (v *MakeMap) String() string {
if v.Reserve != nil {
res = relName(v.Reserve, v)
}
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("make %s %s", relType(v.Type(), from), res)
}

func (v *MakeChan) String() string {
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("make %s %s", relType(v.Type(), from), relName(v.Size, v))
}

Expand Down Expand Up @@ -273,7 +273,7 @@ func (v *Next) String() string {
}

func (v *TypeAssert) String() string {
from := v.Parent().pkg()
from := v.Parent().relPkg()
return fmt.Sprintf("typeassert%s %s.(%s)", commaOk(v.CommaOk), relName(v.X, v), relType(v.AssertedType, from))
}

Expand Down
4 changes: 2 additions & 2 deletions go/ssa/sanity.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ func (s *sanity) checkFunction(fn *Function) bool {
s.errorf("nil Prog")
}

_ = fn.String() // must not crash
_ = fn.RelString(fn.pkg()) // must not crash
_ = fn.String() // must not crash
_ = fn.RelString(fn.relPkg()) // must not crash

// All functions have a package, except delegates (which are
// shared across packages, or duplicated as weak symbols in a
Expand Down

0 comments on commit 198cae3

Please sign in to comment.