From cd61993a08917887401141f8ac03d1b1c80c8806 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 17 Jul 2014 15:10:29 -0400 Subject: [PATCH] go.tools/go/types: move {Type,Object}Of methods from *loader.PackageInfo to *types.Info LGTM=gri R=gri CC=golang-codereviews https://golang.org/cl/113750044 --- go/loader/loader.go | 18 ++++++++++++++ go/loader/pkginfo.go | 59 -------------------------------------------- go/types/api.go | 28 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 59 deletions(-) delete mode 100644 go/loader/pkginfo.go diff --git a/go/loader/loader.go b/go/loader/loader.go index 2a15359a39..e611e7eddb 100644 --- a/go/loader/loader.go +++ b/go/loader/loader.go @@ -258,6 +258,24 @@ type Program struct { AllPackages map[*types.Package]*PackageInfo } +// PackageInfo holds the ASTs and facts derived by the type-checker +// for a single package. +// +// Not mutated once exposed via the API. +// +type PackageInfo struct { + Pkg *types.Package + Importable bool // true if 'import "Pkg.Path()"' would resolve to this + TransitivelyErrorFree bool // true if Pkg and all its dependencies are free of errors + Files []*ast.File // syntax trees for the package's files + Errors []error // non-nil if the package had errors + types.Info // type-checker deductions. + + checker *types.Checker // transient type-checker state +} + +func (info *PackageInfo) String() string { return info.Pkg.Path() } + func (conf *Config) fset() *token.FileSet { if conf.Fset == nil { conf.Fset = token.NewFileSet() diff --git a/go/loader/pkginfo.go b/go/loader/pkginfo.go deleted file mode 100644 index 564d7188a8..0000000000 --- a/go/loader/pkginfo.go +++ /dev/null @@ -1,59 +0,0 @@ -// 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 loader - -import ( - "fmt" - "go/ast" - - "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 exposed via the API. -// -type PackageInfo struct { - Pkg *types.Package - Importable bool // true if 'import "Pkg.Path()"' would resolve to this - TransitivelyErrorFree bool // true if Pkg and all its dependencies are free of errors - Files []*ast.File // abstract syntax for the package's files - Errors []error // non-nil if the package had errors - types.Info // type-checker deductions. - - checker *types.Checker // transient type-checker state -} - -func (info *PackageInfo) String() string { - return fmt.Sprintf("PackageInfo(%s)", info.Pkg.Path()) -} - -// TODO(gri): move the methods below to types.Info. - -// TypeOf returns the type of expression e, or nil if not found. -func (info *PackageInfo) TypeOf(e ast.Expr) types.Type { - if t, ok := info.Types[e]; ok { - return t.Type - } - // Idents appear only in Defs/Uses, not Types. - if id, ok := e.(*ast.Ident); ok { - return info.ObjectOf(id).Type() - } - return nil -} - -// ObjectOf returns the type-checker object denoted by the specified -// id, or nil if not found. -// -// If id is an anonymous struct field, ObjectOf returns the field -// (*types.Var) it uses, not the type (*types.TypeName) it defines. -// -func (info *PackageInfo) ObjectOf(id *ast.Ident) types.Object { - if obj, ok := info.Defs[id]; ok { - return obj - } - return info.Uses[id] -} diff --git a/go/types/api.go b/go/types/api.go index cec106a4ca..01d8928192 100644 --- a/go/types/api.go +++ b/go/types/api.go @@ -206,6 +206,34 @@ type Info struct { InitOrder []*Initializer } +// TypeOf returns the type of expression e, or nil if not found. +// Precondition: the Types, Uses and Defs maps are populated. +// +func (info *Info) TypeOf(e ast.Expr) Type { + if t, ok := info.Types[e]; ok { + return t.Type + } + if id, ok := e.(*ast.Ident); ok { + return info.ObjectOf(id).Type() + } + return nil +} + +// ObjectOf returns the object denoted by the specified id, +// or nil if not found. +// +// If id is an anonymous struct field, ObjectOf returns the field (*Var) +// it uses, not the type (*TypeName) it defines. +// +// Precondition: the Uses and Defs maps are populated. +// +func (info *Info) ObjectOf(id *ast.Ident) Object { + if obj, ok := info.Defs[id]; ok { + return obj + } + return info.Uses[id] +} + // TypeAndValue reports the type and value (for constants) // of the corresponding expression. type TypeAndValue struct {