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-09-03 13:29:02 -06:00
|
|
|
"go/token"
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
"code.google.com/p/go.tools/go/types"
|
2013-09-03 13:29:02 -06:00
|
|
|
"code.google.com/p/go.tools/oracle/json"
|
2013-08-27 15:58:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Implements displays the 'implements" relation among all
|
|
|
|
// package-level named types in the package containing the query
|
|
|
|
// position.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): more features:
|
|
|
|
// - should we include pairs of types belonging to
|
2013-09-03 13:29:02 -06:00
|
|
|
// different packages in the 'implements' relation?
|
2013-08-27 15:58:26 -06:00
|
|
|
// - should we restrict the query to the type declaration identified
|
|
|
|
// by the query position, if any, and use all types in the package
|
|
|
|
// otherwise?
|
|
|
|
// - should we show types that are local to functions?
|
|
|
|
// They can only have methods via promotion.
|
|
|
|
// - abbreviate the set of concrete types implementing the empty
|
|
|
|
// interface.
|
|
|
|
// - should we scan the instruction stream for MakeInterface
|
|
|
|
// instructions and report which concrete->interface conversions
|
|
|
|
// actually occur, with examples? (NB: this is not a conservative
|
|
|
|
// answer due to ChangeInterface, i.e. subtyping among interfaces.)
|
|
|
|
//
|
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) {
|
|
|
|
pkg := qpos.info.Pkg
|
2013-08-27 15:58:26 -06:00
|
|
|
|
|
|
|
// Compute set of named interface/concrete types at package level.
|
|
|
|
var interfaces, concretes []*types.Named
|
2013-09-10 12:11:42 -06:00
|
|
|
scope := pkg.Scope()
|
|
|
|
for _, name := range scope.Names() {
|
|
|
|
mem := scope.Lookup(name)
|
|
|
|
if t, ok := mem.(*types.TypeName); ok {
|
2013-08-27 15:58:26 -06:00
|
|
|
nt := t.Type().(*types.Named)
|
|
|
|
if _, ok := nt.Underlying().(*types.Interface); ok {
|
|
|
|
interfaces = append(interfaces, nt)
|
|
|
|
} else {
|
|
|
|
concretes = append(concretes, nt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each interface, show the concrete types that implement it.
|
|
|
|
var facts []implementsFact
|
|
|
|
for _, iface := range interfaces {
|
|
|
|
fact := implementsFact{iface: iface}
|
|
|
|
for _, conc := range concretes {
|
|
|
|
if types.IsAssignableTo(conc, iface) {
|
|
|
|
fact.conc = conc
|
|
|
|
} else if ptr := types.NewPointer(conc); types.IsAssignableTo(ptr, iface) {
|
|
|
|
fact.conc = ptr
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
facts = append(facts, fact)
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
// TODO(adonovan): sort facts to ensure test nondeterminism.
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
return &implementsResult{o.prog.Fset, facts}, nil
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type implementsFact struct {
|
|
|
|
iface *types.Named
|
|
|
|
conc types.Type // Named or Pointer(Named)
|
|
|
|
}
|
|
|
|
|
|
|
|
type implementsResult struct {
|
2013-09-03 13:29:02 -06:00
|
|
|
fset *token.FileSet
|
2013-08-27 15:58:26 -06:00
|
|
|
facts []implementsFact // facts are grouped by interface
|
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *implementsResult) display(printf printfFunc) {
|
2013-08-27 15:58:26 -06:00
|
|
|
var prevIface *types.Named
|
|
|
|
for _, fact := range r.facts {
|
|
|
|
if fact.iface != prevIface {
|
2013-09-03 13:29:02 -06:00
|
|
|
printf(fact.iface.Obj(), "\tInterface %s:", fact.iface)
|
2013-08-27 15:58:26 -06:00
|
|
|
prevIface = fact.iface
|
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
printf(deref(fact.conc).(*types.Named).Obj(), "\t\t%s", fact.conc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *implementsResult) toJSON(res *json.Result, fset *token.FileSet) {
|
|
|
|
var facts []*json.Implements
|
|
|
|
for _, fact := range r.facts {
|
|
|
|
facts = append(facts, &json.Implements{
|
|
|
|
I: fact.iface.String(),
|
|
|
|
IPos: fset.Position(fact.iface.Obj().Pos()).String(),
|
|
|
|
C: fact.conc.String(),
|
|
|
|
CPos: fset.Position(deref(fact.conc).(*types.Named).Obj().Pos()).String(),
|
|
|
|
})
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
res.Implements = facts
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|