2013-08-27 16:49:13 -06: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.
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
package oracle
|
|
|
|
|
|
|
|
import (
|
2013-12-13 16:00:55 -07:00
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2013-09-03 13:29:02 -06:00
|
|
|
"go/token"
|
2013-12-13 16:00:55 -07:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2013-09-03 13:29:02 -06:00
|
|
|
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
"golang.org/x/tools/go/loader"
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/go/types"
|
|
|
|
"golang.org/x/tools/oracle/serial"
|
2013-08-27 15:58:26 -06:00
|
|
|
)
|
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
// Implements displays the "implements" relation as it pertains to the
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
// selected type within a single package.
|
|
|
|
// If the selection is a method, 'implements' displays
|
2015-02-24 16:02:49 -07:00
|
|
|
// the corresponding methods of the types that would have been reported
|
|
|
|
// by an implements query on the receiver type.
|
2013-08-27 15:58:26 -06:00
|
|
|
//
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
func implements(q *Query) error {
|
|
|
|
lconf := loader.Config{Build: q.Build}
|
|
|
|
allowErrors(&lconf)
|
|
|
|
|
|
|
|
if err := importQueryPackage(q.Pos, &lconf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load/parse/type-check the program.
|
|
|
|
lprog, err := lconf.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
q.Fset = lprog.Fset
|
|
|
|
|
|
|
|
qpos, err := parseQueryPos(lprog, q.Pos, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
// Find the selected type.
|
|
|
|
path, action := findInterestingNode(qpos.info, qpos.path)
|
2015-02-24 16:02:49 -07:00
|
|
|
|
|
|
|
var method *types.Func
|
|
|
|
var T types.Type // selected type (receiver if method != nil)
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case actionExpr:
|
|
|
|
// method?
|
|
|
|
if id, ok := path[0].(*ast.Ident); ok {
|
|
|
|
if obj, ok := qpos.info.ObjectOf(id).(*types.Func); ok {
|
|
|
|
recv := obj.Type().(*types.Signature).Recv()
|
|
|
|
if recv == nil {
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
return fmt.Errorf("this function is not a method")
|
2015-02-24 16:02:49 -07:00
|
|
|
}
|
|
|
|
method = obj
|
|
|
|
T = recv.Type()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case actionType:
|
|
|
|
T = qpos.info.TypeOf(path[0].(ast.Expr))
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
if T == nil {
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
return fmt.Errorf("no type or method here")
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find all named types, even local types (which can have
|
|
|
|
// methods via promotion) and the built-in "error".
|
|
|
|
//
|
|
|
|
// TODO(adonovan): include all packages in PTA scope too?
|
|
|
|
// i.e. don't reduceScope?
|
|
|
|
//
|
|
|
|
var allNamed []types.Type
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
for _, info := range lprog.AllPackages {
|
2014-02-27 11:21:59 -07:00
|
|
|
for _, obj := range info.Defs {
|
|
|
|
if obj, ok := obj.(*types.TypeName); ok {
|
2013-12-13 16:00:55 -07:00
|
|
|
allNamed = append(allNamed, obj.Type())
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
allNamed = append(allNamed, types.Universe.Lookup("error").Type())
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2014-02-11 14:49:27 -07:00
|
|
|
var msets types.MethodSetCache
|
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
// Test each named type.
|
|
|
|
var to, from, fromPtr []types.Type
|
|
|
|
for _, U := range allNamed {
|
|
|
|
if isInterface(T) {
|
2014-02-11 14:49:27 -07:00
|
|
|
if msets.MethodSet(T).Len() == 0 {
|
2013-12-13 16:00:55 -07:00
|
|
|
continue // empty interface
|
|
|
|
}
|
|
|
|
if isInterface(U) {
|
2014-02-11 14:49:27 -07:00
|
|
|
if msets.MethodSet(U).Len() == 0 {
|
2013-12-13 16:00:55 -07:00
|
|
|
continue // empty interface
|
|
|
|
}
|
|
|
|
|
|
|
|
// T interface, U interface
|
2014-01-28 11:57:56 -07:00
|
|
|
if !types.Identical(T, U) {
|
|
|
|
if types.AssignableTo(U, T) {
|
2013-12-13 16:00:55 -07:00
|
|
|
to = append(to, U)
|
|
|
|
}
|
2014-01-28 11:57:56 -07:00
|
|
|
if types.AssignableTo(T, U) {
|
2013-12-13 16:00:55 -07:00
|
|
|
from = append(from, U)
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 15:58:26 -06:00
|
|
|
} else {
|
2013-12-13 16:00:55 -07:00
|
|
|
// T interface, U concrete
|
2014-01-28 11:57:56 -07:00
|
|
|
if types.AssignableTo(U, T) {
|
2013-12-13 16:00:55 -07:00
|
|
|
to = append(to, U)
|
2014-01-28 11:57:56 -07:00
|
|
|
} else if pU := types.NewPointer(U); types.AssignableTo(pU, T) {
|
2013-12-13 16:00:55 -07:00
|
|
|
to = append(to, pU)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if isInterface(U) {
|
2014-02-11 14:49:27 -07:00
|
|
|
if msets.MethodSet(U).Len() == 0 {
|
2013-12-13 16:00:55 -07:00
|
|
|
continue // empty interface
|
|
|
|
}
|
|
|
|
|
|
|
|
// T concrete, U interface
|
2014-01-28 11:57:56 -07:00
|
|
|
if types.AssignableTo(T, U) {
|
2013-12-13 16:00:55 -07:00
|
|
|
from = append(from, U)
|
2014-01-28 11:57:56 -07:00
|
|
|
} else if pT := types.NewPointer(T); types.AssignableTo(pT, U) {
|
2013-12-13 16:00:55 -07:00
|
|
|
fromPtr = append(fromPtr, U)
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
var pos interface{} = qpos
|
|
|
|
if nt, ok := deref(T).(*types.Named); ok {
|
|
|
|
pos = nt.Obj()
|
|
|
|
}
|
|
|
|
|
2014-03-11 16:24:39 -06:00
|
|
|
// Sort types (arbitrarily) to ensure test determinism.
|
2013-12-13 16:00:55 -07:00
|
|
|
sort.Sort(typesByString(to))
|
|
|
|
sort.Sort(typesByString(from))
|
|
|
|
sort.Sort(typesByString(fromPtr))
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2015-02-24 16:02:49 -07:00
|
|
|
var toMethod, fromMethod, fromPtrMethod []*types.Selection // contain nils
|
|
|
|
if method != nil {
|
|
|
|
for _, t := range to {
|
|
|
|
toMethod = append(toMethod,
|
|
|
|
types.NewMethodSet(t).Lookup(method.Pkg(), method.Name()))
|
|
|
|
}
|
|
|
|
for _, t := range from {
|
|
|
|
fromMethod = append(fromMethod,
|
|
|
|
types.NewMethodSet(t).Lookup(method.Pkg(), method.Name()))
|
|
|
|
}
|
|
|
|
for _, t := range fromPtr {
|
|
|
|
fromPtrMethod = append(fromPtrMethod,
|
|
|
|
types.NewMethodSet(t).Lookup(method.Pkg(), method.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
q.result = &implementsResult{
|
|
|
|
qpos, T, pos, to, from, fromPtr, method, toMethod, fromMethod, fromPtrMethod,
|
|
|
|
}
|
|
|
|
return nil
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type implementsResult struct {
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
qpos *queryPos
|
2015-02-24 16:02:49 -07:00
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
t types.Type // queried type (not necessarily named)
|
|
|
|
pos interface{} // pos of t (*types.Name or *QueryPos)
|
|
|
|
to []types.Type // named or ptr-to-named types assignable to interface T
|
|
|
|
from []types.Type // named interfaces assignable from T
|
|
|
|
fromPtr []types.Type // named interfaces assignable only from *T
|
2015-02-24 16:02:49 -07:00
|
|
|
|
|
|
|
// if a method was queried:
|
|
|
|
method *types.Func // queried method
|
|
|
|
toMethod []*types.Selection // method of type to[i], if any
|
|
|
|
fromMethod []*types.Selection // method of type from[i], if any
|
|
|
|
fromPtrMethod []*types.Selection // method of type fromPtrMethod[i], if any
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *implementsResult) display(printf printfFunc) {
|
2015-02-24 16:02:49 -07:00
|
|
|
relation := "is implemented by"
|
|
|
|
|
|
|
|
meth := func(sel *types.Selection) {
|
|
|
|
if sel != nil {
|
|
|
|
printf(sel.Obj(), "\t%s method (%s).%s",
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
relation, r.qpos.typeString(sel.Recv()), sel.Obj().Name())
|
2015-02-24 16:02:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
if isInterface(r.t) {
|
2014-02-11 14:49:27 -07:00
|
|
|
if types.NewMethodSet(r.t).Len() == 0 { // TODO(adonovan): cache mset
|
2015-03-31 08:39:18 -06:00
|
|
|
printf(r.pos, "empty interface type %s", r.qpos.typeString(r.t))
|
2013-12-13 16:00:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-24 16:02:49 -07:00
|
|
|
if r.method == nil {
|
2015-03-31 08:39:18 -06:00
|
|
|
printf(r.pos, "interface type %s", r.qpos.typeString(r.t))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
printf(r.method, "abstract method %s", r.qpos.objectString(r.method))
|
2015-02-24 16:02:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show concrete types (or methods) first; use two passes.
|
|
|
|
for i, sub := range r.to {
|
2013-12-13 16:00:55 -07:00
|
|
|
if !isInterface(sub) {
|
2015-02-24 16:02:49 -07:00
|
|
|
if r.method == nil {
|
|
|
|
printf(deref(sub).(*types.Named).Obj(), "\t%s %s type %s",
|
2015-03-31 08:39:18 -06:00
|
|
|
relation, typeKind(sub), r.qpos.typeString(sub))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
|
|
|
meth(r.toMethod[i])
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
}
|
2015-02-24 16:02:49 -07:00
|
|
|
for i, sub := range r.to {
|
2013-12-13 16:00:55 -07:00
|
|
|
if isInterface(sub) {
|
2015-02-24 16:02:49 -07:00
|
|
|
if r.method == nil {
|
|
|
|
printf(sub.(*types.Named).Obj(), "\t%s %s type %s",
|
2015-03-31 08:39:18 -06:00
|
|
|
relation, typeKind(sub), r.qpos.typeString(sub))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
|
|
|
meth(r.toMethod[i])
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 16:02:49 -07:00
|
|
|
relation = "implements"
|
|
|
|
for i, super := range r.from {
|
|
|
|
if r.method == nil {
|
2015-03-31 08:39:18 -06:00
|
|
|
printf(super.(*types.Named).Obj(), "\t%s %s",
|
|
|
|
relation, r.qpos.typeString(super))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
|
|
|
meth(r.fromMethod[i])
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
} else {
|
2015-02-24 16:02:49 -07:00
|
|
|
relation = "implements"
|
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
if r.from != nil {
|
2015-02-24 16:02:49 -07:00
|
|
|
if r.method == nil {
|
2015-03-31 08:39:18 -06:00
|
|
|
printf(r.pos, "%s type %s",
|
|
|
|
typeKind(r.t), r.qpos.typeString(r.t))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
|
|
|
printf(r.method, "concrete method %s",
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
r.qpos.objectString(r.method))
|
2015-02-24 16:02:49 -07:00
|
|
|
}
|
|
|
|
for i, super := range r.from {
|
|
|
|
if r.method == nil {
|
|
|
|
printf(super.(*types.Named).Obj(), "\t%s %s",
|
2015-03-31 08:39:18 -06:00
|
|
|
relation, r.qpos.typeString(super))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
|
|
|
meth(r.fromMethod[i])
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.fromPtr != nil {
|
2015-02-24 16:02:49 -07:00
|
|
|
if r.method == nil {
|
2015-03-31 08:39:18 -06:00
|
|
|
printf(r.pos, "pointer type *%s", r.qpos.typeString(r.t))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
|
|
|
// TODO(adonovan): de-dup (C).f and (*C).f implementing (I).f.
|
|
|
|
printf(r.method, "concrete method %s",
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
r.qpos.objectString(r.method))
|
2015-02-24 16:02:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, psuper := range r.fromPtr {
|
|
|
|
if r.method == nil {
|
|
|
|
printf(psuper.(*types.Named).Obj(), "\t%s %s",
|
2015-03-31 08:39:18 -06:00
|
|
|
relation, r.qpos.typeString(psuper))
|
2015-02-24 16:02:49 -07:00
|
|
|
} else {
|
|
|
|
meth(r.fromPtrMethod[i])
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
} else if r.from == nil {
|
2015-03-31 08:39:18 -06:00
|
|
|
printf(r.pos, "%s type %s implements only interface{}",
|
|
|
|
typeKind(r.t), r.qpos.typeString(r.t))
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *implementsResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
2013-12-13 16:00:55 -07:00
|
|
|
res.Implements = &serial.Implements{
|
2015-02-24 16:02:49 -07:00
|
|
|
T: makeImplementsType(r.t, fset),
|
|
|
|
AssignableTo: makeImplementsTypes(r.to, fset),
|
|
|
|
AssignableFrom: makeImplementsTypes(r.from, fset),
|
|
|
|
AssignableFromPtr: makeImplementsTypes(r.fromPtr, fset),
|
|
|
|
AssignableToMethod: methodsToSerial(r.qpos.info.Pkg, r.toMethod, fset),
|
|
|
|
AssignableFromMethod: methodsToSerial(r.qpos.info.Pkg, r.fromMethod, fset),
|
|
|
|
AssignableFromPtrMethod: methodsToSerial(r.qpos.info.Pkg, r.fromPtrMethod, fset),
|
|
|
|
}
|
|
|
|
if r.method != nil {
|
|
|
|
res.Implements.Method = &serial.DescribeMethod{
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
Name: r.qpos.objectString(r.method),
|
2015-02-24 16:02:49 -07:00
|
|
|
Pos: fset.Position(r.method.Pos()).String(),
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeImplementsTypes(tt []types.Type, fset *token.FileSet) []serial.ImplementsType {
|
|
|
|
var r []serial.ImplementsType
|
|
|
|
for _, t := range tt {
|
|
|
|
r = append(r, makeImplementsType(t, fset))
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeImplementsType(T types.Type, fset *token.FileSet) serial.ImplementsType {
|
|
|
|
var pos token.Pos
|
|
|
|
if nt, ok := deref(T).(*types.Named); ok { // implementsResult.t may be non-named
|
|
|
|
pos = nt.Obj().Pos()
|
|
|
|
}
|
|
|
|
return serial.ImplementsType{
|
|
|
|
Name: T.String(),
|
|
|
|
Pos: fset.Position(pos).String(),
|
|
|
|
Kind: typeKind(T),
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
|
|
|
|
// typeKind returns a string describing the underlying kind of type,
|
|
|
|
// e.g. "slice", "array", "struct".
|
|
|
|
func typeKind(T types.Type) string {
|
|
|
|
s := reflect.TypeOf(T.Underlying()).String()
|
|
|
|
return strings.ToLower(strings.TrimPrefix(s, "*types."))
|
|
|
|
}
|
|
|
|
|
2014-12-29 13:47:06 -07:00
|
|
|
func isInterface(T types.Type) bool { return types.IsInterface(T) }
|
2013-12-13 16:00:55 -07:00
|
|
|
|
|
|
|
type typesByString []types.Type
|
|
|
|
|
|
|
|
func (p typesByString) Len() int { return len(p) }
|
|
|
|
func (p typesByString) Less(i, j int) bool { return p[i].String() < p[j].String() }
|
|
|
|
func (p typesByString) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|