1
0
mirror of https://github.com/golang/go synced 2024-11-19 22:24:49 -07:00

go/types: add missing documentation to Object factory functions

Fixes #22516.

Change-Id: Ib6648cb224e7e85e894263ef79ea81a5850e9a19
Reviewed-on: https://go-review.googlesource.com/75595
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
griesemer 2017-11-02 17:41:55 -07:00 committed by Robert Griesemer
parent a9e2479a44
commit d593f85ebd
2 changed files with 33 additions and 6 deletions

View File

@ -12,8 +12,6 @@ import (
"go/token" "go/token"
) )
// TODO(gri) Document factory, accessor methods, and fields. General clean-up.
// An Object describes a named language entity such as a package, // An Object describes a named language entity such as a package,
// constant, type, variable, function (incl. methods), or label. // constant, type, variable, function (incl. methods), or label.
// All objects implement the Object interface. // All objects implement the Object interface.
@ -21,7 +19,7 @@ import (
type Object interface { type Object interface {
Parent() *Scope // scope in which this object is declared; nil for methods and struct fields Parent() *Scope // scope in which this object is declared; nil for methods and struct fields
Pos() token.Pos // position of object identifier in declaration Pos() token.Pos // position of object identifier in declaration
Pkg() *Package // nil for objects in the Universe scope and labels Pkg() *Package // package to which this object belongs; nil for labels and objects in the Universe scope
Name() string // package local object name Name() string // package local object name
Type() Type // object type Type() Type // object type
Exported() bool // reports whether the name starts with a capital letter Exported() bool // reports whether the name starts with a capital letter
@ -121,12 +119,15 @@ func (obj *object) sameId(pkg *Package, name string) bool {
} }
// A PkgName represents an imported Go package. // A PkgName represents an imported Go package.
// PkgNames don't have a type.
type PkgName struct { type PkgName struct {
object object
imported *Package imported *Package
used bool // set if the package was used used bool // set if the package was used
} }
// NewPkgName returns a new PkgName object representing an imported package.
// The remaining arguments set the attributes found with all Objects.
func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName { func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName {
return &PkgName{object{nil, pos, pkg, name, Typ[Invalid], 0, token.NoPos}, imported, false} return &PkgName{object{nil, pos, pkg, name, Typ[Invalid], 0, token.NoPos}, imported, false}
} }
@ -142,6 +143,8 @@ type Const struct {
visited bool // for initialization cycle detection visited bool // for initialization cycle detection
} }
// NewConst returns a new constant with value val.
// The remaining arguments set the attributes found with all Objects.
func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const { func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const {
return &Const{object{nil, pos, pkg, name, typ, 0, token.NoPos}, val, false} return &Const{object{nil, pos, pkg, name, typ, 0, token.NoPos}, val, false}
} }
@ -154,6 +157,13 @@ type TypeName struct {
object object
} }
// NewTypeName returns a new type name denoting the given typ.
// The remaining arguments set the attributes found with all Objects.
//
// The typ argument may be a defined (Named) type or an alias type.
// It may also be nil such that the returned TypeName can be used as
// argument for NewNamed, which will set the TypeName's type as a side-
// effect.
func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName { func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName {
return &TypeName{object{nil, pos, pkg, name, typ, 0, token.NoPos}} return &TypeName{object{nil, pos, pkg, name, typ, 0, token.NoPos}}
} }
@ -191,20 +201,30 @@ type Var struct {
used bool // set if the variable was used used bool // set if the variable was used
} }
// NewVar returns a new variable.
// The arguments set the attributes found with all Objects.
func NewVar(pos token.Pos, pkg *Package, name string, typ Type) *Var { func NewVar(pos token.Pos, pkg *Package, name string, typ Type) *Var {
return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}} return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}}
} }
// NewParam returns a new variable representing a function parameter.
func NewParam(pos token.Pos, pkg *Package, name string, typ Type) *Var { func NewParam(pos token.Pos, pkg *Package, name string, typ Type) *Var {
return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, used: true} // parameters are always 'used' return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, used: true} // parameters are always 'used'
} }
// NewField returns a new variable representing a struct field.
// For anonymous (embedded) fields, the name is the unqualified
// type name under which the field is accessible.
func NewField(pos token.Pos, pkg *Package, name string, typ Type, anonymous bool) *Var { func NewField(pos token.Pos, pkg *Package, name string, typ Type, anonymous bool) *Var {
return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, anonymous: anonymous, isField: true} return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, anonymous: anonymous, isField: true}
} }
// Anonymous reports whether the variable is an anonymous field.
func (obj *Var) Anonymous() bool { return obj.anonymous } func (obj *Var) Anonymous() bool { return obj.anonymous }
// IsField reports whether the variable is a struct field.
func (obj *Var) IsField() bool { return obj.isField } func (obj *Var) IsField() bool { return obj.isField }
func (*Var) isDependency() {} // a variable may be a dependency of an initialization expression func (*Var) isDependency() {} // a variable may be a dependency of an initialization expression
// A Func represents a declared function, concrete method, or abstract // A Func represents a declared function, concrete method, or abstract
@ -214,6 +234,8 @@ type Func struct {
object object
} }
// NewFunc returns a new function with the given signature, representing
// the function's type.
func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func { func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func {
// don't store a nil signature // don't store a nil signature
var typ Type var typ Type
@ -231,15 +253,19 @@ func (obj *Func) FullName() string {
return buf.String() return buf.String()
} }
// Scope returns the scope of the function's body block.
func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope } func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope }
func (*Func) isDependency() {} // a function may be a dependency of an initialization expression func (*Func) isDependency() {} // a function may be a dependency of an initialization expression
// A Label represents a declared label. // A Label represents a declared label.
// Labels don't have a type.
type Label struct { type Label struct {
object object
used bool // set if the label was used used bool // set if the label was used
} }
// NewLabel returns a new label.
func NewLabel(pos token.Pos, pkg *Package, name string) *Label { func NewLabel(pos token.Pos, pkg *Package, name string) *Label {
return &Label{object{pos: pos, pkg: pkg, name: name, typ: Typ[Invalid]}, false} return &Label{object{pos: pos, pkg: pkg, name: name, typ: Typ[Invalid]}, false}
} }

View File

@ -383,6 +383,7 @@ type Named struct {
} }
// NewNamed returns a new named type for the given type name, underlying type, and associated methods. // NewNamed returns a new named type for the given type name, underlying type, and associated methods.
// If the given type name obj doesn't have a type yet, its type is set to the returned named type.
// The underlying type must not be a *Named. // The underlying type must not be a *Named.
func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named { func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
if _, ok := underlying.(*Named); ok { if _, ok := underlying.(*Named); ok {