2019-11-07 20:05:39 +01:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-02-26 19:45:19 +01:00
|
|
|
package ir
|
2019-11-07 20:05:39 +01:00
|
|
|
|
|
|
|
// This file implements the String() methods for all Value and
|
|
|
|
// Instruction types.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/types"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/types/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// relName returns the name of v relative to i.
|
|
|
|
// In most cases, this is identical to v.Name(), but references to
|
|
|
|
// Functions (including methods) and Globals use RelString and
|
|
|
|
// all types are displayed with relType, so that only cross-package
|
|
|
|
// references are package-qualified.
|
|
|
|
//
|
|
|
|
func relName(v Value, i Instruction) string {
|
2020-02-26 19:45:19 +01:00
|
|
|
if v == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2019-11-07 20:05:39 +01:00
|
|
|
var from *types.Package
|
|
|
|
if i != nil {
|
|
|
|
from = i.Parent().pkg()
|
|
|
|
}
|
|
|
|
switch v := v.(type) {
|
|
|
|
case Member: // *Function or *Global
|
|
|
|
return v.RelString(from)
|
|
|
|
}
|
|
|
|
return v.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func relType(t types.Type, from *types.Package) string {
|
|
|
|
return types.TypeString(t, types.RelativeTo(from))
|
|
|
|
}
|
|
|
|
|
|
|
|
func relString(m Member, from *types.Package) string {
|
|
|
|
// NB: not all globals have an Object (e.g. init$guard),
|
|
|
|
// so use Package().Object not Object.Package().
|
|
|
|
if pkg := m.Package().Pkg; pkg != nil && pkg != from {
|
|
|
|
return fmt.Sprintf("%s.%s", pkg.Path(), m.Name())
|
|
|
|
}
|
|
|
|
return m.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value.String()
|
|
|
|
//
|
|
|
|
// This method is provided only for debugging.
|
|
|
|
// It never appears in disassembly, which uses Value.Name().
|
|
|
|
|
|
|
|
func (v *Parameter) String() string {
|
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("Parameter <%s> {%s}", relType(v.Type(), from), v.name)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FreeVar) String() string {
|
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("FreeVar <%s> %s", relType(v.Type(), from), v.Name())
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Builtin) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("Builtin %s", v.Name())
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instruction.String()
|
|
|
|
|
|
|
|
func (v *Alloc) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
storage := "Stack"
|
2019-11-07 20:05:39 +01:00
|
|
|
if v.Heap {
|
2020-02-26 19:45:19 +01:00
|
|
|
storage = "Heap"
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("%sAlloc <%s>", storage, relType(v.Type(), from))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Sigma) String() string {
|
2019-11-07 20:05:39 +01:00
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
s := fmt.Sprintf("Sigma <%s> [b%d] %s", relType(v.Type(), from), v.From.Index, v.X.Name())
|
|
|
|
return s
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Phi) String() string {
|
|
|
|
var b bytes.Buffer
|
2020-02-26 19:45:19 +01:00
|
|
|
fmt.Fprintf(&b, "Phi <%s>", v.Type())
|
2019-11-07 20:05:39 +01:00
|
|
|
for i, edge := range v.Edges {
|
2020-02-26 19:45:19 +01:00
|
|
|
b.WriteString(" ")
|
2019-11-07 20:05:39 +01:00
|
|
|
// Be robust against malformed CFG.
|
|
|
|
if v.block == nil {
|
|
|
|
b.WriteString("??")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
block := -1
|
|
|
|
if i < len(v.block.Preds) {
|
|
|
|
block = v.block.Preds[i].Index
|
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
fmt.Fprintf(&b, "%d:", block)
|
2019-11-07 20:05:39 +01:00
|
|
|
edgeVal := "<nil>" // be robust
|
|
|
|
if edge != nil {
|
|
|
|
edgeVal = relName(edge, v)
|
|
|
|
}
|
|
|
|
b.WriteString(edgeVal)
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func printCall(v *CallCommon, prefix string, instr Instruction) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
if !v.IsInvoke() {
|
2020-02-26 19:45:19 +01:00
|
|
|
if value, ok := instr.(Value); ok {
|
|
|
|
fmt.Fprintf(&b, "%s <%s> %s", prefix, relType(value.Type(), instr.Parent().pkg()), relName(v.Value, instr))
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(&b, "%s %s", prefix, relName(v.Value, instr))
|
|
|
|
}
|
2019-11-07 20:05:39 +01:00
|
|
|
} else {
|
2020-02-26 19:45:19 +01:00
|
|
|
if value, ok := instr.(Value); ok {
|
|
|
|
fmt.Fprintf(&b, "%sInvoke <%s> %s.%s", prefix, relType(value.Type(), instr.Parent().pkg()), relName(v.Value, instr), v.Method.Name())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(&b, "%sInvoke %s.%s", prefix, relName(v.Value, instr), v.Method.Name())
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
for _, arg := range v.Args {
|
|
|
|
b.WriteString(" ")
|
|
|
|
b.WriteString(relName(arg, instr))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CallCommon) String() string {
|
|
|
|
return printCall(c, "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Call) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return printCall(&v.Call, "Call", v)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *BinOp) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("BinOp <%s> {%s} %s %s", relType(v.Type(), v.Parent().pkg()), v.Op.String(), relName(v.X, v), relName(v.Y, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UnOp) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("UnOp <%s> {%s} %s", relType(v.Type(), v.Parent().pkg()), v.Op.String(), relName(v.X, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Load) String() string {
|
|
|
|
return fmt.Sprintf("Load <%s> %s", relType(v.Type(), v.Parent().pkg()), relName(v.X, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func printConv(prefix string, v, x Value) string {
|
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("%s <%s> %s",
|
2019-11-07 20:05:39 +01:00
|
|
|
prefix,
|
|
|
|
relType(v.Type(), from),
|
|
|
|
relName(x, v.(Instruction)))
|
|
|
|
}
|
|
|
|
|
2020-02-26 19:45:19 +01:00
|
|
|
func (v *ChangeType) String() string { return printConv("ChangeType", v, v.X) }
|
|
|
|
func (v *Convert) String() string { return printConv("Convert", v, v.X) }
|
|
|
|
func (v *ChangeInterface) String() string { return printConv("ChangeInterface", v, v.X) }
|
|
|
|
func (v *MakeInterface) String() string { return printConv("MakeInterface", v, v.X) }
|
2019-11-07 20:05:39 +01:00
|
|
|
|
|
|
|
func (v *MakeClosure) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
2019-11-07 20:05:39 +01:00
|
|
|
var b bytes.Buffer
|
2020-02-26 19:45:19 +01:00
|
|
|
fmt.Fprintf(&b, "MakeClosure <%s> %s", relType(v.Type(), from), relName(v.Fn, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
if v.Bindings != nil {
|
2020-02-26 19:45:19 +01:00
|
|
|
for _, c := range v.Bindings {
|
|
|
|
b.WriteString(" ")
|
2019-11-07 20:05:39 +01:00
|
|
|
b.WriteString(relName(c, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeSlice) String() string {
|
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("MakeSlice <%s> %s %s",
|
2019-11-07 20:05:39 +01:00
|
|
|
relType(v.Type(), from),
|
|
|
|
relName(v.Len, v),
|
|
|
|
relName(v.Cap, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Slice) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("Slice <%s> %s %s %s %s",
|
|
|
|
relType(v.Type(), from), relName(v.X, v), relName(v.Low, v), relName(v.High, v), relName(v.Max, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeMap) String() string {
|
|
|
|
res := ""
|
|
|
|
if v.Reserve != nil {
|
|
|
|
res = relName(v.Reserve, v)
|
|
|
|
}
|
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("MakeMap <%s> %s", relType(v.Type(), from), res)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeChan) String() string {
|
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("MakeChan <%s> %s", relType(v.Type(), from), relName(v.Size, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FieldAddr) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
2019-11-07 20:05:39 +01:00
|
|
|
st := deref(v.X.Type()).Underlying().(*types.Struct)
|
|
|
|
// Be robust against a bad index.
|
|
|
|
name := "?"
|
|
|
|
if 0 <= v.Field && v.Field < st.NumFields() {
|
|
|
|
name = st.Field(v.Field).Name()
|
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("FieldAddr <%s> [%d] (%s) %s", relType(v.Type(), from), v.Field, name, relName(v.X, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Field) String() string {
|
|
|
|
st := v.X.Type().Underlying().(*types.Struct)
|
|
|
|
// Be robust against a bad index.
|
|
|
|
name := "?"
|
|
|
|
if 0 <= v.Field && v.Field < st.NumFields() {
|
|
|
|
name = st.Field(v.Field).Name()
|
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("Field <%s> [%d] (%s) %s", relType(v.Type(), from), v.Field, name, relName(v.X, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *IndexAddr) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("IndexAddr <%s> %s %s", relType(v.Type(), from), relName(v.X, v), relName(v.Index, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Index) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("Index <%s> %s %s", relType(v.Type(), from), relName(v.X, v), relName(v.Index, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 19:45:19 +01:00
|
|
|
func (v *MapLookup) String() string {
|
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("MapLookup <%s> %s %s", relType(v.Type(), from), relName(v.X, v), relName(v.Index, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *StringLookup) String() string {
|
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("StringLookup <%s> %s %s", relType(v.Type(), from), relName(v.X, v), relName(v.Index, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Range) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("Range <%s> %s", relType(v.Type(), from), relName(v.X, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Next) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
return fmt.Sprintf("Next <%s> %s", relType(v.Type(), from), relName(v.Iter, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TypeAssert) String() string {
|
|
|
|
from := v.Parent().pkg()
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("TypeAssert <%s> %s", relType(v.Type(), from), relName(v.X, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Extract) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
from := v.Parent().pkg()
|
|
|
|
name := v.Tuple.Type().(*types.Tuple).At(v.Index).Name()
|
|
|
|
return fmt.Sprintf("Extract <%s> [%d] (%s) %s", relType(v.Type(), from), v.Index, name, relName(v.Tuple, v))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Jump) String() string {
|
|
|
|
// Be robust against malformed CFG.
|
|
|
|
block := -1
|
|
|
|
if s.block != nil && len(s.block.Succs) == 1 {
|
|
|
|
block = s.block.Succs[0].Index
|
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
str := fmt.Sprintf("Jump → b%d", block)
|
|
|
|
if s.Comment != "" {
|
|
|
|
str = fmt.Sprintf("%s # %s", str, s.Comment)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Unreachable) String() string {
|
|
|
|
// Be robust against malformed CFG.
|
|
|
|
block := -1
|
|
|
|
if s.block != nil && len(s.block.Succs) == 1 {
|
|
|
|
block = s.block.Succs[0].Index
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Unreachable → b%d", block)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *If) String() string {
|
|
|
|
// Be robust against malformed CFG.
|
|
|
|
tblock, fblock := -1, -1
|
|
|
|
if s.block != nil && len(s.block.Succs) == 2 {
|
|
|
|
tblock = s.block.Succs[0].Index
|
|
|
|
fblock = s.block.Succs[1].Index
|
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("If %s → b%d b%d", relName(s.Cond, s), tblock, fblock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstantSwitch) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
fmt.Fprintf(&b, "ConstantSwitch %s", relName(s.Tag, s))
|
|
|
|
for _, cond := range s.Conds {
|
|
|
|
fmt.Fprintf(&b, " %s", relName(cond, s))
|
|
|
|
}
|
|
|
|
fmt.Fprint(&b, " →")
|
|
|
|
for _, succ := range s.block.Succs {
|
|
|
|
fmt.Fprintf(&b, " b%d", succ.Index)
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TypeSwitch) String() string {
|
|
|
|
from := s.Parent().pkg()
|
|
|
|
var b bytes.Buffer
|
|
|
|
fmt.Fprintf(&b, "TypeSwitch <%s> %s", relType(s.typ, from), relName(s.Tag, s))
|
|
|
|
for _, cond := range s.Conds {
|
|
|
|
fmt.Fprintf(&b, " %q", relType(cond, s.block.parent.pkg()))
|
|
|
|
}
|
|
|
|
return b.String()
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Go) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return printCall(&s.Call, "Go", s)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Panic) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
// Be robust against malformed CFG.
|
|
|
|
block := -1
|
|
|
|
if s.block != nil && len(s.block.Succs) == 1 {
|
|
|
|
block = s.block.Succs[0].Index
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Panic %s → b%d", relName(s.X, s), block)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Return) String() string {
|
|
|
|
var b bytes.Buffer
|
2020-02-26 19:45:19 +01:00
|
|
|
b.WriteString("Return")
|
|
|
|
for _, r := range s.Results {
|
|
|
|
b.WriteString(" ")
|
2019-11-07 20:05:39 +01:00
|
|
|
b.WriteString(relName(r, s))
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*RunDefers) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return "RunDefers"
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Send) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("Send %s %s", relName(s.Chan, s), relName(s.X, s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (recv *Recv) String() string {
|
|
|
|
from := recv.Parent().pkg()
|
|
|
|
return fmt.Sprintf("Recv <%s> %s", relType(recv.Type(), from), relName(recv.Chan, recv))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Defer) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return printCall(&s.Call, "Defer", s)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Select) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
for i, st := range s.States {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
if st.Dir == types.RecvOnly {
|
|
|
|
b.WriteString("<-")
|
|
|
|
b.WriteString(relName(st.Chan, s))
|
|
|
|
} else {
|
|
|
|
b.WriteString(relName(st.Chan, s))
|
|
|
|
b.WriteString("<-")
|
|
|
|
b.WriteString(relName(st.Send, s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
non := ""
|
|
|
|
if !s.Blocking {
|
2020-02-26 19:45:19 +01:00
|
|
|
non = "Non"
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
2020-02-26 19:45:19 +01:00
|
|
|
from := s.Parent().pkg()
|
|
|
|
return fmt.Sprintf("Select%sBlocking <%s> [%s]", non, relType(s.Type(), from), b.String())
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("Store {%s} %s %s",
|
|
|
|
s.Val.Type(), relName(s.Addr, s), relName(s.Val, s))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BlankStore) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("BlankStore %s", relName(s.Val, s))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MapUpdate) String() string {
|
2020-02-26 19:45:19 +01:00
|
|
|
return fmt.Sprintf("MapUpdate %s %s %s", relName(s.Map, s), relName(s.Key, s), relName(s.Value, s))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DebugRef) String() string {
|
|
|
|
p := s.Parent().Prog.Fset.Position(s.Pos())
|
|
|
|
var descr interface{}
|
|
|
|
if s.object != nil {
|
|
|
|
descr = s.object // e.g. "var x int"
|
|
|
|
} else {
|
|
|
|
descr = reflect.TypeOf(s.Expr) // e.g. "*ast.CallExpr"
|
|
|
|
}
|
|
|
|
var addr string
|
|
|
|
if s.IsAddr {
|
|
|
|
addr = "address of "
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("; %s%s @ %d:%d is %s", addr, descr, p.Line, p.Column, s.X.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) String() string {
|
|
|
|
return "package " + p.Pkg.Path()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.WriterTo = (*Package)(nil) // *Package implements io.Writer
|
|
|
|
|
|
|
|
func (p *Package) WriteTo(w io.Writer) (int64, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
WritePackage(&buf, p)
|
|
|
|
n, err := w.Write(buf.Bytes())
|
|
|
|
return int64(n), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WritePackage writes to buf a human-readable summary of p.
|
|
|
|
func WritePackage(buf *bytes.Buffer, p *Package) {
|
|
|
|
fmt.Fprintf(buf, "%s:\n", p)
|
|
|
|
|
|
|
|
var names []string
|
|
|
|
maxname := 0
|
|
|
|
for name := range p.Members {
|
|
|
|
if l := len(name); l > maxname {
|
|
|
|
maxname = l
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
from := p.Pkg
|
|
|
|
sort.Strings(names)
|
|
|
|
for _, name := range names {
|
|
|
|
switch mem := p.Members[name].(type) {
|
|
|
|
case *NamedConst:
|
|
|
|
fmt.Fprintf(buf, " const %-*s %s = %s\n",
|
|
|
|
maxname, name, mem.Name(), mem.Value.RelString(from))
|
|
|
|
|
|
|
|
case *Function:
|
|
|
|
fmt.Fprintf(buf, " func %-*s %s\n",
|
|
|
|
maxname, name, relType(mem.Type(), from))
|
|
|
|
|
|
|
|
case *Type:
|
|
|
|
fmt.Fprintf(buf, " type %-*s %s\n",
|
|
|
|
maxname, name, relType(mem.Type().Underlying(), from))
|
|
|
|
for _, meth := range typeutil.IntuitiveMethodSet(mem.Type(), &p.Prog.MethodSets) {
|
|
|
|
fmt.Fprintf(buf, " %s\n", types.SelectionString(meth, types.RelativeTo(from)))
|
|
|
|
}
|
|
|
|
|
|
|
|
case *Global:
|
|
|
|
fmt.Fprintf(buf, " var %-*s %s\n",
|
|
|
|
maxname, name, relType(mem.Type().(*types.Pointer).Elem(), from))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(buf, "\n")
|
|
|
|
}
|