mirror of
https://github.com/golang/go
synced 2024-11-19 02:44:44 -07:00
f119874203
Command set: - what: an extremely fast query that parses a single file and returns the AST stack, package name and the set of query modes that apply to the current selection. Intended for GUI tools that need to grey out UI elements. - definition: shows the definition of an identifier. - pointsto: the PTA features of 'describe' have been split out into their own command. - describe: with PTA stripped out, the cost is now bounded by type checking. Performance: - The importer.Config.TypeCheckFuncBodies predicate supports setting the 'IgnoreFuncBodies' typechecker flag on a per-package basis. This means we can load dependencies from source more quickly if we only need exported types. (We avoid gcimport data because it may be absent or stale.) This also means we can run type-based queries on packages that aren't part of the pointer analysis scope. (Yay.) - Modes that require only type analysis of the query package run a "what" query first, and restrict their analysis scope to just that package and its dependencies (sans func bodies), making them much faster. - We call newOracle not oracle.New in Query, so that the 'needs' bitset isn't ignored (oops!). This makes the non-PTA queries faster. Also: - removed vestigial timers junk. - pos.go: existing position utilties split out into own file. Added parsePosFlag utility. - numerous cosmetic tweaks. + very basic tests. To do in follow-ups: - sophisticated editor integration of "what". - better tests. - refactoring of control flow as described in comment. - changes to "implements", "describe" commands. - update design doc + user manual. R=crawshaw, dominik.honnef CC=golang-dev, gri https://golang.org/cl/40630043
110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
// 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.
|
|
|
|
package importer
|
|
|
|
import (
|
|
"fmt"
|
|
"go/ast"
|
|
|
|
"code.google.com/p/go.tools/go/exact"
|
|
"code.google.com/p/go.tools/go/types"
|
|
)
|
|
|
|
// PackageInfo holds the ASTs and facts derived by the type-checker
|
|
// for a single package.
|
|
//
|
|
// Not mutated once constructed.
|
|
//
|
|
type PackageInfo struct {
|
|
Pkg *types.Package
|
|
Importable bool // true if 'import "Pkg.Path()"' would resolve to this
|
|
Err error // non-nil if the package had static errors
|
|
Files []*ast.File // abstract syntax for the package's files
|
|
types.Info // type-checker deductions.
|
|
}
|
|
|
|
func (info *PackageInfo) String() string {
|
|
return fmt.Sprintf("PackageInfo(%s)", info.Pkg.Path())
|
|
}
|
|
|
|
// TypeOf returns the type of expression e.
|
|
// Precondition: e belongs to the package's ASTs.
|
|
//
|
|
func (info *PackageInfo) TypeOf(e ast.Expr) types.Type {
|
|
if t, ok := info.Types[e]; ok {
|
|
return t
|
|
}
|
|
// Defining ast.Idents (id := expr) get only Ident callbacks
|
|
// but not Expr callbacks.
|
|
if id, ok := e.(*ast.Ident); ok {
|
|
return info.ObjectOf(id).Type()
|
|
}
|
|
panic("no type for expression")
|
|
}
|
|
|
|
// ValueOf returns the value of expression e if it is a constant, nil
|
|
// otherwise.
|
|
// Precondition: e belongs to the package's ASTs.
|
|
//
|
|
func (info *PackageInfo) ValueOf(e ast.Expr) exact.Value {
|
|
return info.Values[e]
|
|
}
|
|
|
|
// ObjectOf returns the typechecker object denoted by the specified id.
|
|
// Precondition: id belongs to the package's ASTs.
|
|
//
|
|
func (info *PackageInfo) ObjectOf(id *ast.Ident) types.Object {
|
|
return info.Objects[id]
|
|
}
|
|
|
|
// IsType returns true iff expression e denotes a type.
|
|
// Precondition: e belongs to the package's ASTs.
|
|
//
|
|
// TODO(gri): move this into go/types.
|
|
//
|
|
func (info *PackageInfo) IsType(e ast.Expr) bool {
|
|
switch e := e.(type) {
|
|
case *ast.SelectorExpr: // pkg.Type
|
|
if sel := info.Selections[e]; sel.Kind() == types.PackageObj {
|
|
_, isType := sel.Obj().(*types.TypeName)
|
|
return isType
|
|
}
|
|
case *ast.StarExpr: // *T
|
|
return info.IsType(e.X)
|
|
case *ast.Ident:
|
|
_, isType := info.ObjectOf(e).(*types.TypeName)
|
|
return isType
|
|
case *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.ChanType:
|
|
return true
|
|
case *ast.ParenExpr:
|
|
return info.IsType(e.X)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// TypeCaseVar returns the implicit variable created by a single-type
|
|
// case clause in a type switch, or nil if not found.
|
|
//
|
|
func (info *PackageInfo) TypeCaseVar(cc *ast.CaseClause) *types.Var {
|
|
if v := info.Implicits[cc]; v != nil {
|
|
return v.(*types.Var)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ImportSpecPkg returns the PkgName for a given ImportSpec, possibly
|
|
// an implicit one for a dot-import or an import-without-rename.
|
|
// It returns nil if not found.
|
|
//
|
|
func (info *PackageInfo) ImportSpecPkg(spec *ast.ImportSpec) *types.PkgName {
|
|
if spec.Name != nil {
|
|
return info.ObjectOf(spec.Name).(*types.PkgName)
|
|
}
|
|
if p := info.Implicits[spec]; p != nil {
|
|
return p.(*types.PkgName)
|
|
}
|
|
return nil
|
|
}
|