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 08:04:55 -07:00
|
|
|
"bytes"
|
2013-08-27 15:58:26 -06:00
|
|
|
"go/ast"
|
2013-12-13 08:04:55 -07:00
|
|
|
"go/printer"
|
2013-09-03 13:29:02 -06:00
|
|
|
"go/token"
|
|
|
|
"sort"
|
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
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// freevars displays the lexical (not package-level) free variables of
|
|
|
|
// the selection.
|
|
|
|
//
|
|
|
|
// It treats A.B.C as a separate variable from A to reveal the parts
|
|
|
|
// of an aggregate type that are actually needed.
|
|
|
|
// This aids refactoring.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): optionally display the free references to
|
|
|
|
// file/package scope objects, and to objects from other packages.
|
|
|
|
// Depending on where the resulting function abstraction will go,
|
|
|
|
// these might be interesting. Perhaps group the results into three
|
|
|
|
// bands.
|
|
|
|
//
|
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 freevars(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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
file := qpos.path[len(qpos.path)-1] // the enclosing file
|
|
|
|
fileScope := qpos.info.Scopes[file]
|
2013-09-12 09:00:22 -06:00
|
|
|
pkgScope := fileScope.Parent()
|
2013-08-27 15:58:26 -06:00
|
|
|
|
|
|
|
// The id and sel functions return non-nil if they denote an
|
|
|
|
// object o or selection o.x.y that is referenced by the
|
|
|
|
// selection but defined neither within the selection nor at
|
|
|
|
// file scope, i.e. it is in the lexical environment.
|
|
|
|
var id func(n *ast.Ident) types.Object
|
|
|
|
var sel func(n *ast.SelectorExpr) types.Object
|
|
|
|
|
|
|
|
sel = func(n *ast.SelectorExpr) types.Object {
|
|
|
|
switch x := unparen(n.X).(type) {
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
return sel(x)
|
|
|
|
case *ast.Ident:
|
|
|
|
return id(x)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id = func(n *ast.Ident) types.Object {
|
2014-07-11 03:50:09 -06:00
|
|
|
obj := qpos.info.Uses[n]
|
2013-08-27 15:58:26 -06:00
|
|
|
if obj == nil {
|
2014-07-11 03:50:09 -06:00
|
|
|
return nil // not a reference
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-13 10:52:57 -06:00
|
|
|
if _, ok := obj.(*types.PkgName); ok {
|
2013-08-27 15:58:26 -06:00
|
|
|
return nil // imported package
|
|
|
|
}
|
|
|
|
if !(file.Pos() <= obj.Pos() && obj.Pos() <= file.End()) {
|
|
|
|
return nil // not defined in this file
|
|
|
|
}
|
2013-09-12 09:00:22 -06:00
|
|
|
scope := obj.Parent()
|
|
|
|
if scope == nil {
|
|
|
|
return nil // e.g. interface method, struct field
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-12 09:00:22 -06:00
|
|
|
if scope == fileScope || scope == pkgScope {
|
|
|
|
return nil // defined at file or package scope
|
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
|
|
|
if qpos.start <= obj.Pos() && obj.Pos() <= qpos.end {
|
2013-08-27 15:58:26 -06:00
|
|
|
return nil // defined within selection => not free
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maps each reference that is free in the selection
|
|
|
|
// to the object it refers to.
|
2013-09-03 13:29:02 -06:00
|
|
|
// The map de-duplicates repeated references.
|
|
|
|
refsMap := make(map[string]freevarsRef)
|
2013-08-27 15:58:26 -06:00
|
|
|
|
|
|
|
// Visit all the identifiers in the selected ASTs.
|
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
|
|
|
ast.Inspect(qpos.path[0], func(n ast.Node) bool {
|
2013-08-27 15:58:26 -06:00
|
|
|
if n == nil {
|
|
|
|
return true // popping DFS stack
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this node contained within the selection?
|
|
|
|
// (freevars permits inexact selections,
|
|
|
|
// like two stmts in a block.)
|
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
|
|
|
if qpos.start <= n.Pos() && n.End() <= qpos.end {
|
2013-09-03 13:29:02 -06:00
|
|
|
var obj types.Object
|
|
|
|
var prune bool
|
2013-08-27 15:58:26 -06:00
|
|
|
switch n := n.(type) {
|
|
|
|
case *ast.Ident:
|
2013-09-03 13:29:02 -06:00
|
|
|
obj = id(n)
|
2013-08-27 15:58:26 -06:00
|
|
|
|
|
|
|
case *ast.SelectorExpr:
|
2013-09-03 13:29:02 -06:00
|
|
|
obj = sel(n)
|
|
|
|
prune = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if obj != nil {
|
|
|
|
var kind string
|
|
|
|
switch obj.(type) {
|
|
|
|
case *types.Var:
|
|
|
|
kind = "var"
|
|
|
|
case *types.Func:
|
|
|
|
kind = "func"
|
|
|
|
case *types.TypeName:
|
|
|
|
kind = "type"
|
|
|
|
case *types.Const:
|
|
|
|
kind = "const"
|
|
|
|
case *types.Label:
|
|
|
|
kind = "label"
|
|
|
|
default:
|
|
|
|
panic(obj)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
typ := qpos.info.TypeOf(n.(ast.Expr))
|
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
|
|
|
ref := freevarsRef{kind, printNode(lprog.Fset, n), typ, obj}
|
2013-09-03 13:29:02 -06:00
|
|
|
refsMap[ref.ref] = ref
|
|
|
|
|
|
|
|
if prune {
|
2013-08-27 15:58:26 -06:00
|
|
|
return false // don't descend
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true // descend
|
|
|
|
})
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
refs := make([]freevarsRef, 0, len(refsMap))
|
|
|
|
for _, ref := range refsMap {
|
|
|
|
refs = append(refs, ref)
|
|
|
|
}
|
|
|
|
sort.Sort(byRef(refs))
|
|
|
|
|
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 = &freevarsResult{
|
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
|
|
|
qpos: qpos,
|
2013-09-03 13:29:02 -06:00
|
|
|
refs: refs,
|
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 nil
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type freevarsResult 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
|
2013-09-03 13:29:02 -06:00
|
|
|
refs []freevarsRef
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type freevarsRef struct {
|
2013-09-03 13:29:02 -06:00
|
|
|
kind string
|
|
|
|
ref string
|
|
|
|
typ types.Type
|
2013-08-27 15:58:26 -06:00
|
|
|
obj types.Object
|
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *freevarsResult) display(printf printfFunc) {
|
2013-08-27 15:58:26 -06:00
|
|
|
if len(r.refs) == 0 {
|
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
|
|
|
printf(r.qpos, "No free identifiers.")
|
2013-09-03 13:29:02 -06:00
|
|
|
} else {
|
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
|
|
|
printf(r.qpos, "Free identifiers:")
|
2013-09-03 13:29:02 -06:00
|
|
|
for _, ref := range r.refs {
|
2013-12-13 08:04:55 -07:00
|
|
|
// Avoid printing "type T T".
|
|
|
|
var typstr string
|
|
|
|
if ref.kind != "type" {
|
|
|
|
typstr = " " + types.TypeString(r.qpos.info.Pkg, ref.typ)
|
|
|
|
}
|
|
|
|
printf(ref.obj, "%s %s%s", ref.kind, ref.ref, typstr)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *freevarsResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
|
|
|
var refs []*serial.FreeVar
|
2013-09-03 13:29:02 -06:00
|
|
|
for _, ref := range r.refs {
|
|
|
|
refs = append(refs,
|
2013-09-24 13:08:14 -06:00
|
|
|
&serial.FreeVar{
|
2013-09-03 13:29:02 -06:00
|
|
|
Pos: fset.Position(ref.obj.Pos()).String(),
|
|
|
|
Kind: ref.kind,
|
|
|
|
Ref: ref.ref,
|
|
|
|
Type: ref.typ.String(),
|
|
|
|
})
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
res.Freevars = refs
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
|
|
|
|
// -------- utils --------
|
|
|
|
|
|
|
|
type byRef []freevarsRef
|
|
|
|
|
|
|
|
func (p byRef) Len() int { return len(p) }
|
|
|
|
func (p byRef) Less(i, j int) bool { return p[i].ref < p[j].ref }
|
|
|
|
func (p byRef) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
2013-12-13 08:04:55 -07:00
|
|
|
|
|
|
|
// printNode returns the pretty-printed syntax of n.
|
|
|
|
func printNode(fset *token.FileSet, n ast.Node) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
printer.Fprint(&buf, fset, n)
|
|
|
|
return buf.String()
|
|
|
|
}
|