mirror of
https://github.com/golang/go
synced 2024-11-19 00:54:42 -07:00
go.tools/ssa: dump types in "pkg."-unqualified form where appropriate.
Also: types.Typ[types.UnsafePointer].String() now always prints as "unsafe.Pointer". R=gri CC=golang-dev https://golang.org/cl/21820044
This commit is contained in:
parent
8e64946a3a
commit
2e7d5a8b6b
@ -228,6 +228,9 @@ func writeType(buf *bytes.Buffer, typ Type) {
|
||||
buf.WriteString("<nil>")
|
||||
|
||||
case *Basic:
|
||||
if t.Kind() == UnsafePointer {
|
||||
buf.WriteString("unsafe.")
|
||||
}
|
||||
buf.WriteString(t.name)
|
||||
|
||||
case *Array:
|
||||
|
@ -119,7 +119,7 @@ var importedObjectTests = []struct {
|
||||
kind ast.ObjKind
|
||||
typ string
|
||||
}{
|
||||
{"unsafe.Pointer", ast.Typ, "Pointer"},
|
||||
{"unsafe.Pointer", ast.Typ, "unsafe.Pointer"},
|
||||
{"math.Pi", ast.Con, "untyped float"},
|
||||
{"io.Reader", ast.Typ, "interface{Read(p []byte) (n int, err error)}"},
|
||||
{"io.ReadWriter", ast.Typ, "interface{Read(p []byte) (n int, err error); Write(p []byte) (n int, err error)}"},
|
||||
|
@ -578,8 +578,8 @@ func (b *builder) expr0(fn *Function, e ast.Expr) Value {
|
||||
return emitArith(fn, e.Op, b.expr(fn, e.X), b.expr(fn, e.Y), fn.Pkg.typeOf(e), e.OpPos)
|
||||
|
||||
case token.EQL, token.NEQ, token.GTR, token.LSS, token.LEQ, token.GEQ:
|
||||
// TODO(gri): we shouldn't need DefaultType here.
|
||||
cmp := emitCompare(fn, e.Op, b.expr(fn, e.X), b.expr(fn, e.Y), e.OpPos)
|
||||
// The type of x==y may be UntypedBool.
|
||||
return emitConv(fn, cmp, DefaultType(fn.Pkg.typeOf(e)))
|
||||
default:
|
||||
panic("illegal op in BinaryExpr: " + e.Op.String())
|
||||
@ -2221,7 +2221,7 @@ func (p *Package) Build() {
|
||||
init.emit(&v)
|
||||
}
|
||||
|
||||
b := new(builder)
|
||||
var b builder
|
||||
|
||||
// Initialize package-level vars in correct order.
|
||||
for _, varinit := range p.info.InitOrder {
|
||||
|
21
ssa/func.go
21
ssa/func.go
@ -499,7 +499,7 @@ func (f *Function) fullName(from *Package) string {
|
||||
// writeSignature writes to w the signature sig in declaration syntax.
|
||||
// Derived from types.Signature.String().
|
||||
//
|
||||
func writeSignature(w io.Writer, name string, sig *types.Signature, params []*Parameter) {
|
||||
func writeSignature(w io.Writer, pkg *Package, name string, sig *types.Signature, params []*Parameter) {
|
||||
io.WriteString(w, "func ")
|
||||
if recv := sig.Recv(); recv != nil {
|
||||
io.WriteString(w, "(")
|
||||
@ -507,7 +507,7 @@ func writeSignature(w io.Writer, name string, sig *types.Signature, params []*Pa
|
||||
io.WriteString(w, n)
|
||||
io.WriteString(w, " ")
|
||||
}
|
||||
io.WriteString(w, params[0].Type().String())
|
||||
io.WriteString(w, relType(params[0].Type(), pkg))
|
||||
io.WriteString(w, ") ")
|
||||
params = params[1:]
|
||||
}
|
||||
@ -521,9 +521,9 @@ func writeSignature(w io.Writer, name string, sig *types.Signature, params []*Pa
|
||||
io.WriteString(w, " ")
|
||||
if sig.IsVariadic() && i == len(params)-1 {
|
||||
io.WriteString(w, "...")
|
||||
io.WriteString(w, v.Type().Underlying().(*types.Slice).Elem().String())
|
||||
io.WriteString(w, relType(v.Type().Underlying().(*types.Slice).Elem(), pkg))
|
||||
} else {
|
||||
io.WriteString(w, v.Type().String())
|
||||
io.WriteString(w, relType(v.Type(), pkg))
|
||||
}
|
||||
}
|
||||
io.WriteString(w, ")")
|
||||
@ -531,9 +531,9 @@ func writeSignature(w io.Writer, name string, sig *types.Signature, params []*Pa
|
||||
io.WriteString(w, " ")
|
||||
r := sig.Results()
|
||||
if n == 1 && r.At(0).Name() == "" {
|
||||
io.WriteString(w, r.At(0).Type().String())
|
||||
io.WriteString(w, relType(r.At(0).Type(), pkg))
|
||||
} else {
|
||||
io.WriteString(w, r.String())
|
||||
io.WriteString(w, r.String()) // TODO(adonovan): use relType
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -561,18 +561,18 @@ func (f *Function) DumpTo(w io.Writer) {
|
||||
if f.FreeVars != nil {
|
||||
io.WriteString(w, "# Free variables:\n")
|
||||
for i, fv := range f.FreeVars {
|
||||
fmt.Fprintf(w, "# % 3d:\t%s %s\n", i, fv.Name(), fv.Type())
|
||||
fmt.Fprintf(w, "# % 3d:\t%s %s\n", i, fv.Name(), relType(fv.Type(), f.Pkg))
|
||||
}
|
||||
}
|
||||
|
||||
if len(f.Locals) > 0 {
|
||||
io.WriteString(w, "# Locals:\n")
|
||||
for i, l := range f.Locals {
|
||||
fmt.Fprintf(w, "# % 3d:\t%s %s\n", i, l.Name(), deref(l.Type()))
|
||||
fmt.Fprintf(w, "# % 3d:\t%s %s\n", i, l.Name(), relType(deref(l.Type()), f.Pkg))
|
||||
}
|
||||
}
|
||||
|
||||
writeSignature(w, f.Name(), f.Signature, f.Params)
|
||||
writeSignature(w, f.Pkg, f.Name(), f.Signature, f.Params)
|
||||
io.WriteString(w, ":\n")
|
||||
|
||||
if f.Blocks == nil {
|
||||
@ -603,11 +603,12 @@ func (f *Function) DumpTo(w io.Writer) {
|
||||
n, _ := fmt.Fprintf(w, "%s = ", name)
|
||||
l -= n
|
||||
}
|
||||
// TODO(adonovan): append instructions directly to w.
|
||||
n, _ := io.WriteString(w, instr.String())
|
||||
l -= n
|
||||
// Right-align the type.
|
||||
if t := v.Type(); t != nil {
|
||||
fmt.Fprintf(w, " %*s", l-10, t)
|
||||
fmt.Fprintf(w, " %*s", l-10, relType(t, f.Pkg))
|
||||
}
|
||||
case nil:
|
||||
// Be robust against bad transforms.
|
||||
|
13
ssa/print.go
13
ssa/print.go
@ -48,13 +48,6 @@ func relName(v Value, i Instruction) string {
|
||||
// TODO(gri): provide this functionality in go/types (using a
|
||||
// *types.Package, obviously).
|
||||
//
|
||||
// TODO(adonovan): use this more widely, e.g.
|
||||
// ChangeType, Literal, Convert, MakeInterface;
|
||||
// when displaying receiver, params, locals, captures of a Function;
|
||||
// and in the RHS type column for Value-defining Instructions.
|
||||
//
|
||||
// TODO(adonovan): fix: unsafe.Pointer has no ssa.Package.
|
||||
//
|
||||
func relType(t types.Type, from *Package) string {
|
||||
if from != nil {
|
||||
t2 := t
|
||||
@ -177,7 +170,7 @@ func (v *Call) String() string {
|
||||
}
|
||||
|
||||
func (v *ChangeType) String() string {
|
||||
return fmt.Sprintf("changetype %s <- %s (%s)", v.Type(), v.X.Type(), relName(v.X, v))
|
||||
return fmt.Sprintf("changetype %s <- %s (%s)", relType(v.Type(), v.Parent().Pkg), v.X.Type(), relName(v.X, v))
|
||||
}
|
||||
|
||||
func (v *BinOp) String() string {
|
||||
@ -189,7 +182,7 @@ func (v *UnOp) String() string {
|
||||
}
|
||||
|
||||
func (v *Convert) String() string {
|
||||
return fmt.Sprintf("convert %s <- %s (%s)", v.Type(), v.X.Type(), relName(v.X, v))
|
||||
return fmt.Sprintf("convert %s <- %s (%s)", relType(v.Type(), v.Parent().Pkg), v.X.Type(), relName(v.X, v))
|
||||
}
|
||||
|
||||
func (v *ChangeInterface) String() string {
|
||||
@ -197,7 +190,7 @@ func (v *ChangeInterface) String() string {
|
||||
}
|
||||
|
||||
func (v *MakeInterface) String() string {
|
||||
return fmt.Sprintf("make %s <- %s (%s)", v.Type(), v.X.Type(), relName(v.X, v))
|
||||
return fmt.Sprintf("make %s <- %s (%s)", relType(v.Type(), v.Parent().Pkg), relType(v.X.Type(), v.Parent().Pkg), relName(v.X, v))
|
||||
}
|
||||
|
||||
func (v *MakeClosure) String() string {
|
||||
|
Loading…
Reference in New Issue
Block a user