go/tools/cmd/godex: don't print unexported methods

and print exported methods of unexported types

LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/80920046
This commit is contained in:
Robert Griesemer 2014-03-27 10:49:59 -07:00
parent 33f2a1701b
commit f293460f67

View File

@ -55,33 +55,41 @@ func (p *printer) printPackage(pkg *types.Package, filter func(types.Object) boo
var ( var (
consts []*types.Const consts []*types.Const
typez []*types.TypeName // types without methods typez []*types.TypeName // types without methods
typem []*types.TypeName // types with methods typem []*types.Named // types with methods
vars []*types.Var vars []*types.Var
funcs []*types.Func funcs []*types.Func
builtins []*types.Builtin builtins []*types.Builtin
) )
scope := pkg.Scope() scope := pkg.Scope()
for _, name := range scope.Names() { for _, name := range scope.Names() {
obj := scope.Lookup(name) if obj := scope.Lookup(name); filter(obj) {
if !filter(obj) { switch obj := obj.(type) {
continue case *types.Const:
} consts = append(consts, obj)
switch obj := obj.(type) { case *types.TypeName:
case *types.Const: // group into types with methods and types without
consts = append(consts, obj) // (for now this is only considering explicitly declared - not "inherited" methods)
case *types.TypeName: if named, _ := obj.Type().(*types.Named); named != nil && named.NumMethods() > 0 {
if named, _ := obj.Type().(*types.Named); named != nil && named.NumMethods() > 0 { typem = append(typem, named)
typem = append(typem, obj) } else {
} else { typez = append(typez, obj)
typez = append(typez, obj) }
case *types.Var:
vars = append(vars, obj)
case *types.Func:
funcs = append(funcs, obj)
case *types.Builtin:
// for unsafe.Sizeof, etc.
builtins = append(builtins, obj)
}
} else {
// type is filtered out but may contain visible methods
if obj, _ := obj.(*types.TypeName); obj != nil {
// see case *types.TypeName above
if named, _ := obj.Type().(*types.Named); named != nil && named.NumMethods() > 0 {
typem = append(typem, named)
}
} }
case *types.Var:
vars = append(vars, obj)
case *types.Func:
funcs = append(funcs, obj)
case *types.Builtin:
// for unsafe.Sizeof, etc.
builtins = append(builtins, obj)
} }
} }
@ -121,16 +129,24 @@ func (p *printer) printPackage(pkg *types.Package, filter func(types.Object) boo
p.print(")\n\n") p.print(")\n\n")
} }
for _, obj := range typem { for _, typ := range typem {
p.printf("type %s ", obj.Name()) hasEntries := false
typ := obj.Type().(*types.Named) if obj := typ.Obj(); filter(obj) {
p.writeType(p.pkg, typ.Underlying()) p.printf("type %s ", obj.Name())
p.print("\n") p.writeType(p.pkg, typ.Underlying())
p.print("\n")
hasEntries = true
}
for i, n := 0, typ.NumMethods(); i < n; i++ { for i, n := 0, typ.NumMethods(); i < n; i++ {
p.printFunc(typ.Method(i)) if obj := typ.Method(i); filter(obj) {
p.printFunc(obj)
p.print("\n")
hasEntries = true
}
}
if hasEntries {
p.print("\n") p.print("\n")
} }
p.print("\n")
} }
for _, obj := range funcs { for _, obj := range funcs {