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
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
"code.google.com/p/go.tools/go/types"
|
2013-09-24 13:08:14 -06:00
|
|
|
"code.google.com/p/go.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
|
|
|
|
// selected type.
|
2013-08-27 15:58:26 -06:00
|
|
|
//
|
go.tools/oracle: refactor Oracle API to allow repeated queries on same scope.
The existing standalone Query function builds an importer, ssa.Program, oracle,
and query position, executes the query and returns the result.
For clients (such as Frederik Zipp's web-based github.com/fzipp/pythia tool)
that wish to load the program once and make several queries, we now expose
these as separate operations too. Here's a client, in pseudocode:
o := oracle.New(...)
for ... {
qpos := o.ParseQueryPos(...)
res := o.Query(mode, qpos)
print result
}
NB: this is a slight deoptimisation in the one-shot case since we have to
build the entire SSA program with debug info, not just the query package,
since we now don't know the query package at that time.
The 'exact' param to ParseQueryPos needs more thought since its
ideal value is a function of the query mode. This will do for now.
Details:
- expose Oracle type, New() func and Query() method.
- expose QueryPos type and ParseQueryPos func.
- improved package doc comment.
- un-exposed the "needs" bits.
- added test.
R=crawshaw
CC=frederik.zipp, golang-dev
https://golang.org/cl/13810043
2013-09-23 13:02:18 -06:00
|
|
|
func implements(o *Oracle, qpos *QueryPos) (queryResult, error) {
|
2013-12-13 16:00:55 -07:00
|
|
|
// Find the selected type.
|
|
|
|
// TODO(adonovan): fix: make it work on qualified Idents too.
|
|
|
|
path, action := findInterestingNode(qpos.info, qpos.path)
|
|
|
|
if action != actionType {
|
|
|
|
return nil, fmt.Errorf("no type here")
|
|
|
|
}
|
|
|
|
T := qpos.info.TypeOf(path[0].(ast.Expr))
|
|
|
|
if T == nil {
|
|
|
|
return nil, fmt.Errorf("no type here")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
for _, info := range o.typeInfo {
|
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
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
return &implementsResult{T, pos, to, from, fromPtr}, nil
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type implementsResult struct {
|
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
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *implementsResult) display(printf printfFunc) {
|
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
|
2013-12-13 16:00:55 -07:00
|
|
|
printf(r.pos, "empty interface type %s", r.t)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(r.pos, "interface type %s", r.t)
|
|
|
|
// Show concrete types first; use two passes.
|
|
|
|
for _, sub := range r.to {
|
|
|
|
if !isInterface(sub) {
|
|
|
|
printf(deref(sub).(*types.Named).Obj(), "\tis implemented by %s type %s",
|
|
|
|
typeKind(sub), sub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, sub := range r.to {
|
|
|
|
if isInterface(sub) {
|
|
|
|
printf(deref(sub).(*types.Named).Obj(), "\tis implemented by %s type %s", typeKind(sub), sub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, super := range r.from {
|
|
|
|
printf(super.(*types.Named).Obj(), "\timplements %s", super)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if r.from != nil {
|
|
|
|
printf(r.pos, "%s type %s", typeKind(r.t), r.t)
|
|
|
|
for _, super := range r.from {
|
|
|
|
printf(super.(*types.Named).Obj(), "\timplements %s", super)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.fromPtr != nil {
|
|
|
|
printf(r.pos, "pointer type *%s", r.t)
|
|
|
|
for _, psuper := range r.fromPtr {
|
|
|
|
printf(psuper.(*types.Named).Obj(), "\timplements %s", psuper)
|
|
|
|
}
|
|
|
|
} else if r.from == nil {
|
|
|
|
printf(r.pos, "%s type %s implements only interface{}", typeKind(r.t), 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{
|
|
|
|
T: makeImplementsType(r.t, fset),
|
|
|
|
AssignableTo: makeImplementsTypes(r.to, fset),
|
|
|
|
AssignableFrom: makeImplementsTypes(r.from, fset),
|
|
|
|
AssignableFromPtr: makeImplementsTypes(r.fromPtr, fset),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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."))
|
|
|
|
}
|
|
|
|
|
|
|
|
func isInterface(T types.Type) bool {
|
|
|
|
_, isI := T.Underlying().(*types.Interface)
|
|
|
|
return isI
|
|
|
|
}
|
|
|
|
|
|
|
|
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] }
|