Documentation ¶
Overview ¶
Package types declares the data types and implements the algorithms for type-checking of Go packages. Use Check and Config.Check to invoke the type-checker.
Type-checking consists of several interdependent phases:
Name resolution maps each identifier (ast.Ident) in the program to the language object (Object) it denotes. Use Info.{Defs,Uses,Implicits} for the results of name resolution.
Constant folding computes the exact constant value (exact.Value) for every expression (ast.Expr) that is a compile-time constant. Use Info.Types[expr].Value for the results of constant folding.
Type inference computes the type (Type) of every expression (ast.Expr) and checks for compliance with the language specification. Use Info.Types[expr].Type for the results of type inference.
Index ¶
- Variables
- func AssertableTo(V *Interface, T Type) bool
- func AssignableTo(V, T Type) bool
- func Comparable(T Type) bool
- func ConvertibleTo(V, T Type) bool
- func DefPredeclaredTestFuncs()
- func ExprString(x ast.Expr) string
- func Id(pkg *Package, name string) string
- func Identical(x, y Type) bool
- func Implements(V Type, T *Interface) bool
- func IsInterface(typ Type) bool
- func ObjectString(this *Package, obj Object) string
- func SelectionString(this *Package, s *Selection) string
- func TypeString(this *Package, typ Type) string
- func WriteExpr(buf *bytes.Buffer, x ast.Expr)
- func WriteSignature(buf *bytes.Buffer, this *Package, sig *Signature)
- func WriteType(buf *bytes.Buffer, this *Package, typ Type)
- type Array
- type Basic
- type BasicInfo
- type BasicKind
- type Builtin
- type Chan
- type ChanDir
- type Checker
- type Config
- type Const
- type Error
- type Func
- func (obj *Func) Exported() bool
- func (obj *Func) FullName() string
- func (obj *Func) Id() string
- func (obj *Func) Name() string
- func (obj *Func) Parent() *Scope
- func (obj *Func) Pkg() *Package
- func (obj *Func) Pos() token.Pos
- func (obj *Func) Scope() *Scope
- func (obj *Func) String() string
- func (obj *Func) Type() Type
- type Importer
- type Info
- type Initializer
- type Interface
- func (t *Interface) Complete() *Interface
- func (t *Interface) Embedded(i int) *Named
- func (t *Interface) Empty() bool
- func (t *Interface) ExplicitMethod(i int) *Func
- func (t *Interface) Method(i int) *Func
- func (t *Interface) NumEmbeddeds() int
- func (t *Interface) NumExplicitMethods() int
- func (t *Interface) NumMethods() int
- func (t *Interface) String() string
- func (t *Interface) Underlying() Type
- type Label
- type Map
- type MethodSet
- type MethodSetCache
- type Named
- type Nil
- type Object
- type Package
- type PkgName
- func (obj *PkgName) Exported() bool
- func (obj *PkgName) Id() string
- func (obj *PkgName) Imported() *Package
- func (obj *PkgName) Name() string
- func (obj *PkgName) Parent() *Scope
- func (obj *PkgName) Pkg() *Package
- func (obj *PkgName) Pos() token.Pos
- func (obj *PkgName) String() string
- func (obj *PkgName) Type() Type
- type Pointer
- type Scope
- func (s *Scope) Child(i int) *Scope
- func (s *Scope) Insert(obj Object) Object
- func (s *Scope) Len() int
- func (s *Scope) Lookup(name string) Object
- func (s *Scope) LookupParent(name string) (*Scope, Object)
- func (s *Scope) Names() []string
- func (s *Scope) NumChildren() int
- func (s *Scope) Parent() *Scope
- func (s *Scope) String() string
- func (s *Scope) WriteTo(w io.Writer, n int, recurse bool)
- type Selection
- type SelectionKind
- type Signature
- type Sizes
- type Slice
- type StdSizes
- type Struct
- type Tuple
- type Type
- type TypeAndValue
- func (tv TypeAndValue) Addressable() bool
- func (tv TypeAndValue) Assignable() bool
- func (tv TypeAndValue) HasOk() bool
- func (tv TypeAndValue) IsBuiltin() bool
- func (tv TypeAndValue) IsNil() bool
- func (tv TypeAndValue) IsType() bool
- func (tv TypeAndValue) IsValue() bool
- func (tv TypeAndValue) IsVoid() bool
- type TypeName
- type Var
- func (obj *Var) Anonymous() bool
- func (obj *Var) Exported() bool
- func (obj *Var) Id() string
- func (obj *Var) IsField() bool
- func (obj *Var) Name() string
- func (obj *Var) Parent() *Scope
- func (obj *Var) Pkg() *Package
- func (obj *Var) Pos() token.Pos
- func (obj *Var) String() string
- func (obj *Var) Type() Type
Constants ¶
This section is empty.
Variables ¶
var ( Universe *Scope Unsafe *Package UniverseByte *Basic // uint8 alias, but has name "byte" UniverseRune *Basic // int32 alias, but has name "rune" )
var GcCompatibilityMode bool
If GcCompatibilityMode is set, printing of types is modified to match the representation of some types in the gc compiler:
- byte and rune lose their alias name and simply stand for uint8 and int32 respectively
- embedded interfaces get flattened (the embedding info is lost, and certain recursive interface types cannot be printed anymore)
This makes it easier to compare packages computed with the type- checker vs packages imported from gc export data.
Caution: This flag affects all uses of WriteType, globally. It is only provided for testing in conjunction with gc-generated data. It may be removed at any time.
var Typ = [...]*Basic{ Invalid: {Invalid, 0, "invalid type"}, Bool: {Bool, IsBoolean, "bool"}, Int: {Int, IsInteger, "int"}, Int8: {Int8, IsInteger, "int8"}, Int16: {Int16, IsInteger, "int16"}, Int32: {Int32, IsInteger, "int32"}, Int64: {Int64, IsInteger, "int64"}, Uint: {Uint, IsInteger | IsUnsigned, "uint"}, Uint8: {Uint8, IsInteger | IsUnsigned, "uint8"}, Uint16: {Uint16, IsInteger | IsUnsigned, "uint16"}, Uint32: {Uint32, IsInteger | IsUnsigned, "uint32"}, Uint64: {Uint64, IsInteger | IsUnsigned, "uint64"}, Uintptr: {Uintptr, IsInteger | IsUnsigned, "uintptr"}, Float32: {Float32, IsFloat, "float32"}, Float64: {Float64, IsFloat, "float64"}, Complex64: {Complex64, IsComplex, "complex64"}, Complex128: {Complex128, IsComplex, "complex128"}, String: {String, IsString, "string"}, UnsafePointer: {UnsafePointer, 0, "Pointer"}, UntypedBool: {UntypedBool, IsBoolean | IsUntyped, "untyped bool"}, UntypedInt: {UntypedInt, IsInteger | IsUntyped, "untyped int"}, UntypedRune: {UntypedRune, IsInteger | IsUntyped, "untyped rune"}, UntypedFloat: {UntypedFloat, IsFloat | IsUntyped, "untyped float"}, UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"}, UntypedString: {UntypedString, IsString | IsUntyped, "untyped string"}, UntypedNil: {UntypedNil, IsUntyped, "untyped nil"}, }
Functions ¶
func AssertableTo ¶
AssertableTo reports whether a value of type V can be asserted to have type T.
func AssignableTo ¶
AssignableTo reports whether a value of type V is assignable to a variable of type T.
func Comparable ¶
Comparable reports whether values of type T are comparable.
func ConvertibleTo ¶
ConvertibleTo reports whether a value of type V is convertible to a value of type T.
func DefPredeclaredTestFuncs ¶
func DefPredeclaredTestFuncs()
DefPredeclaredTestFuncs defines the assert and trace built-ins. These built-ins are intended for debugging and testing of this package only.
func ExprString ¶
ExprString returns the (possibly simplified) string representation for x.
func Id ¶
Id returns name if it is exported, otherwise it returns the name qualified with the package path.
func Implements ¶
Implements reports whether type V implements interface T.
func IsInterface ¶
IsInterface reports whether typ is an interface type.
func ObjectString ¶
ObjectString returns the string form of obj. Object and type names are printed package-qualified only if they do not belong to this package.
func SelectionString ¶
SelectionString returns the string form of s. Type names are printed package-qualified only if they do not belong to this package.
Examples:
"field (T) f int" "method (T) f(X) Y" "method expr (T) f(X) Y"
func TypeString ¶
TypeString returns the string representation of typ. Named types are printed package-qualified if they do not belong to this package.
func WriteSignature ¶
WriteSignature writes the representation of the signature sig to buf, without a leading "func" keyword. Named types are printed package-qualified if they do not belong to this package.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
An Array represents an array type.
func (*Array) Underlying ¶
type Basic ¶
type Basic struct {
// contains filtered or unexported fields
}
A Basic represents a basic type.
func (*Basic) Underlying ¶
type BasicInfo ¶
type BasicInfo int
BasicInfo is a set of flags describing properties of a basic type.
type BasicKind ¶
type BasicKind int
BasicKind describes the kind of basic type.
const ( Invalid BasicKind = iota // type is invalid // predeclared types Bool Int Int8 Int16 Int32 Int64 Uint Uint8 Uint16 Uint32 Uint64 Uintptr Float32 Float64 Complex64 Complex128 String UnsafePointer // types for untyped values UntypedBool UntypedInt UntypedRune UntypedFloat UntypedComplex UntypedString UntypedNil // aliases Byte = Uint8 Rune = Int32 )
type Builtin ¶
type Builtin struct {
// contains filtered or unexported fields
}
A Builtin represents a built-in function. Builtins don't have a valid type.
type Chan ¶
type Chan struct {
// contains filtered or unexported fields
}
A Chan represents a channel type.
func (*Chan) Underlying ¶
type Checker ¶
type Checker struct { *Info // contains filtered or unexported fields }
A Checker maintains the state of the type checker. It must be created with NewChecker.
func NewChecker ¶
NewChecker returns a new Checker instance for a given package. Package files may be added incrementally via checker.Files.
type Config ¶
type Config struct { // If IgnoreFuncBodies is set, function bodies are not // type-checked. IgnoreFuncBodies bool // If FakeImportC is set, `import "C"` (for packages requiring Cgo) // declares an empty "C" package and errors are omitted for qualified // identifiers referring to package C (which won't find an object). // This feature is intended for the standard library cmd/api tool. // // Caution: Effects may be unpredictable due to follow-up errors. // Do not use casually! FakeImportC bool // Packages is used to look up (and thus canonicalize) packages by // package path. If Packages is nil, it is set to a new empty map. // During type-checking, imported packages are added to the map. Packages map[string]*Package // If Error != nil, it is called with each error found // during type checking; err has dynamic type Error. // Secondary errors (for instance, to enumerate all types // involved in an invalid recursive type declaration) have // error strings that start with a '\t' character. // If Error == nil, type-checking stops with the first // error found. Error func(err error) // If Import != nil, it is called for each imported package. // Otherwise, DefaultImport is called. Import Importer // If Sizes != nil, it provides the sizing functions for package unsafe. // Otherwise &StdSizes{WordSize: 8, MaxAlign: 8} is used instead. Sizes Sizes // If DisableUnusedImportCheck is set, packages are not checked // for unused imports. DisableUnusedImportCheck bool }
A Config specifies the configuration for type checking. The zero value for Config is a ready-to-use default configuration.
func (*Config) Check ¶
func (conf *Config) Check(path string, fset *token.FileSet, files []*ast.File, info *Info) (*Package, error)
Check type-checks a package and returns the resulting package object, the first error if any, and if info != nil, additional type information. The package is marked as complete if no errors occurred, otherwise it is incomplete. See Config.Error for controlling behavior in the presence of errors.
The package is specified by a list of *ast.Files and corresponding file set, and the package path the package is identified with. The clean path must not be empty or dot (".").
type Const ¶
type Const struct {
// contains filtered or unexported fields
}
A Const represents a declared constant.
type Error ¶
type Error struct { Fset *token.FileSet // file set for interpretation of Pos Pos token.Pos // error position Msg string // error message Soft bool // if set, error is "soft" }
An Error describes a type-checking error; it implements the error interface. A "soft" error is an error that still permits a valid interpretation of a package (such as "unused variable"); "hard" errors may lead to unpredictable behavior if ignored.
type Func ¶
type Func struct {
// contains filtered or unexported fields
}
A Func represents a declared function, concrete method, or abstract (interface) method. Its Type() is always a *Signature. An abstract method may belong to many interfaces due to embedding.
func MissingMethod ¶
MissingMethod returns (nil, false) if V implements T, otherwise it returns a missing method required by T and whether it is missing or just has the wrong type.
For non-interface types V, or if static is set, V implements T if all methods of T are present in V. Otherwise (V is an interface and static is not set), MissingMethod only checks that methods of T which are also present in V have matching types (e.g., for a type assertion x.(T) where x is of interface type V).
type Importer ¶
An importer resolves import paths to Packages. The imports map records packages already known, indexed by package path. The type-checker will invoke Import with Config.Packages. An importer must determine the canonical package path and check imports to see if it is already present in the map. If so, the Importer can return the map entry. Otherwise, the importer must load the package data for the given path into a new *Package, record it in imports map, and return the package. TODO(gri) Need to be clearer about requirements of completeness.
var DefaultImport Importer
DefaultImport is the default importer invoked if Config.Import == nil. The declaration:
import _ "golang.org/x/tools/go/gcimporter"
in a client of go/types will initialize DefaultImport to gcimporter.Import.
type Info ¶
type Info struct { // Types maps expressions to their types, and for constant // expressions, their values. Invalid expressions are omitted. // // For (possibly parenthesized) identifiers denoting built-in // functions, the recorded signatures are call-site specific: // if the call result is not a constant, the recorded type is // an argument-specific signature. Otherwise, the recorded type // is invalid. // // Identifiers on the lhs of declarations (i.e., the identifiers // which are being declared) are collected in the Defs map. // Identifiers denoting packages are collected in the Uses maps. Types map[ast.Expr]TypeAndValue // Defs maps identifiers to the objects they define (including // package names, dots "." of dot-imports, and blank "_" identifiers). // For identifiers that do not denote objects (e.g., the package name // in package clauses, or symbolic variables t in t := x.(type) of // type switch headers), the corresponding objects are nil. // // For an anonymous field, Defs returns the field *Var it defines. // // Invariant: Defs[id] == nil || Defs[id].Pos() == id.Pos() Defs map[*ast.Ident]Object // Uses maps identifiers to the objects they denote. // // For an anonymous field, Uses returns the *TypeName it denotes. // // Invariant: Uses[id].Pos() != id.Pos() Uses map[*ast.Ident]Object // Implicits maps nodes to their implicitly declared objects, if any. // The following node and object types may appear: // // node declared object // // *ast.ImportSpec *PkgName for dot-imports and imports without renames // *ast.CaseClause type-specific *Var for each type switch case clause (incl. default) // *ast.Field anonymous struct field or parameter *Var // Implicits map[ast.Node]Object // Selections maps selector expressions (excluding qualified identifiers) // to their corresponding selections. Selections map[*ast.SelectorExpr]*Selection // Scopes maps ast.Nodes to the scopes they define. Package scopes are not // associated with a specific node but with all files belonging to a package. // Thus, the package scope can be found in the type-checked Package object. // Scopes nest, with the Universe scope being the outermost scope, enclosing // the package scope, which contains (one or more) files scopes, which enclose // function scopes which in turn enclose statement and function literal scopes. // Note that even though package-level functions are declared in the package // scope, the function scopes are embedded in the file scope of the file // containing the function declaration. // // The following node types may appear in Scopes: // // *ast.File // *ast.FuncType // *ast.BlockStmt // *ast.IfStmt // *ast.SwitchStmt // *ast.TypeSwitchStmt // *ast.CaseClause // *ast.CommClause // *ast.ForStmt // *ast.RangeStmt // Scopes map[ast.Node]*Scope // InitOrder is the list of package-level initializers in the order in which // they must be executed. Initializers referring to variables related by an // initialization dependency appear in topological order, the others appear // in source order. Variables without an initialization expression do not // appear in this list. InitOrder []*Initializer }
Info holds result type information for a type-checked package. Only the information for which a map is provided is collected. If the package has type errors, the collected information may be incomplete.
type Initializer ¶
An Initializer describes a package-level variable, or a list of variables in case of a multi-valued initialization expression, and the corresponding initialization expression.
func (*Initializer) String ¶
func (init *Initializer) String() string
type Interface ¶
type Interface struct {
// contains filtered or unexported fields
}
An Interface represents an interface type.
func NewInterface ¶
NewInterface returns a new interface for the given methods and embedded types.
func (*Interface) Complete ¶
Complete computes the interface's method set. It must be called by users of NewInterface after the interface's embedded types are fully defined and before using the interface type in any way other than to form other types. Complete returns the receiver.
func (*Interface) Embedded ¶
Embedded returns the i'th embedded type of interface t for 0 <= i < t.NumEmbeddeds(). The types are ordered by the corresponding TypeName's unique Id.
func (*Interface) ExplicitMethod ¶
ExplicitMethod returns the i'th explicitly declared method of interface t for 0 <= i < t.NumExplicitMethods(). The methods are ordered by their unique Id.
func (*Interface) Method ¶
Method returns the i'th method of interface t for 0 <= i < t.NumMethods(). The methods are ordered by their unique Id.
func (*Interface) NumEmbeddeds ¶
NumEmbeddeds returns the number of embedded types in interface t.
func (*Interface) NumExplicitMethods ¶
NumExplicitMethods returns the number of explicitly declared methods of interface t.
func (*Interface) NumMethods ¶
NumMethods returns the total number of methods of interface t.
func (*Interface) Underlying ¶
type Label ¶
type Label struct {
// contains filtered or unexported fields
}
A Label represents a declared label.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
A Map represents a map type.
func (*Map) Underlying ¶
type MethodSet ¶
type MethodSet struct {
// contains filtered or unexported fields
}
A MethodSet is an ordered set of concrete or abstract (interface) methods; a method is a MethodVal selection, and they are ordered by ascending m.Obj().Id(). The zero value for a MethodSet is a ready-to-use empty method set.
func NewMethodSet ¶
NewMethodSet returns the method set for the given type T. It always returns a non-nil method set, even if it is empty.
A MethodSetCache handles repeat queries more efficiently.
type MethodSetCache ¶
type MethodSetCache struct {
// contains filtered or unexported fields
}
A MethodSetCache records the method set of each type T for which MethodSet(T) is called so that repeat queries are fast. The zero value is a ready-to-use cache instance.
func (*MethodSetCache) MethodSet ¶
func (cache *MethodSetCache) MethodSet(T Type) *MethodSet
MethodSet returns the method set of type T. It is thread-safe.
If cache is nil, this function is equivalent to NewMethodSet(T). Utility functions can thus expose an optional *MethodSetCache parameter to clients that care about performance.
type Named ¶
type Named struct {
// contains filtered or unexported fields
}
A Named represents a named type.
func NewNamed ¶
NewNamed returns a new named type for the given type name, underlying type, and associated methods. The underlying type must not be a *Named.
func (*Named) AddMethod ¶
AddMethod adds method m unless it is already in the method list. TODO(gri) find a better solution instead of providing this function
func (*Named) NumMethods ¶
NumMethods returns the number of explicit methods whose receiver is named type t.
func (*Named) SetUnderlying ¶
SetUnderlying sets the underlying type and marks t as complete. TODO(gri) determine if there's a better solution rather than providing this function
func (*Named) Underlying ¶
type Nil ¶
type Nil struct {
// contains filtered or unexported fields
}
Nil represents the predeclared value nil.
type Object ¶
type Object interface { Parent() *Scope // scope in which this object is declared Pos() token.Pos // position of object identifier in declaration Pkg() *Package // nil for objects in the Universe scope and labels Name() string // package local object name Type() Type // object type Exported() bool // reports whether the name starts with a capital letter Id() string // object id (see Id below) // String returns a human-readable string of the object. String() string // contains filtered or unexported methods }
An Object describes a named language entity such as a package, constant, type, variable, function (incl. methods), or label. All objects implement the Object interface.
func LookupFieldOrMethod ¶
func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool)
LookupFieldOrMethod looks up a field or method with given package and name in T and returns the corresponding *Var or *Func, an index sequence, and a bool indicating if there were any pointer indirections on the path to the field or method. If addressable is set, T is the type of an addressable variable (only matters for method lookups).
The last index entry is the field or method index in the (possibly embedded) type where the entry was found, either:
- the list of declared methods of a named type; or
- the list of all methods (method set) of an interface type; or
- the list of fields of a struct type.
The earlier index entries are the indices of the anonymous struct fields traversed to get to the found entry, starting at depth 0.
If no entry is found, a nil object is returned. In this case, the returned index and indirect values have the following meaning:
If index != nil, the index sequence points to an ambiguous entry (the same name appeared more than once at the same embedding level).
If indirect is set, a method with a pointer receiver type was found but there was no pointer on the path from the actual receiver type to the method's formal receiver base type, nor was the receiver addressable.
type Package ¶
type Package struct {
// contains filtered or unexported fields
}
A Package describes a Go package.
func Check ¶
Check type-checks a package and returns the resulting complete package object, or a nil package and the first error. The package is specified by a list of *ast.Files and corresponding file set, and the import path the package is identified with. The clean path must not be empty or dot (".").
For more control over type-checking and results, use Config.Check.
func NewPackage ¶
NewPackage returns a new Package for the given package path and name; the name must not be the blank identifier. The package is not complete and contains no explicit imports.
func (*Package) Complete ¶
A package is complete if its scope contains (at least) all exported objects; otherwise it is incomplete.
func (*Package) Imports ¶
Imports returns the list of packages explicitly imported by pkg; the list is in source order. Package unsafe is excluded.
func (*Package) MarkComplete ¶
func (pkg *Package) MarkComplete()
MarkComplete marks a package as complete.
func (*Package) Scope ¶
Scope returns the (complete or incomplete) package scope holding the objects declared at package level (TypeNames, Consts, Vars, and Funcs).
func (*Package) SetImports ¶
SetImports sets the list of explicitly imported packages to list. It is the caller's responsibility to make sure list elements are unique.
type PkgName ¶
type PkgName struct {
// contains filtered or unexported fields
}
A PkgName represents an imported Go package.
func NewPkgName ¶
type Pointer ¶
type Pointer struct {
// contains filtered or unexported fields
}
A Pointer represents a pointer type.
func NewPointer ¶
NewPointer returns a new pointer type for the given element (base) type.
func (*Pointer) Underlying ¶
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
A Scope maintains a set of objects and links to its containing (parent) and contained (children) scopes. Objects may be inserted and looked up by name. The zero value for Scope is a ready-to-use empty scope.
func NewScope ¶
NewScope returns a new, empty scope contained in the given parent scope, if any. The comment is for debugging only.
func (*Scope) Insert ¶
Insert attempts to insert an object obj into scope s. If s already contains an alternative object alt with the same name, Insert leaves s unchanged and returns alt. Otherwise it inserts obj, sets the object's parent scope if not already set, and returns nil.
func (*Scope) Lookup ¶
Lookup returns the object in scope s with the given name if such an object exists; otherwise the result is nil.
func (*Scope) LookupParent ¶
LookupParent follows the parent chain of scopes starting with s until it finds a scope where Lookup(name) returns a non-nil object, and then returns that scope and object. If no such scope exists, the result is (nil, nil).
Note that obj.Parent() may be different from the returned scope if the object was inserted into the scope and already had a parent at that time (see Insert, below). This can only happen for dot-imported objects whose scope is the scope of the package that exported them.
func (*Scope) NumChildren ¶
NumChildren() returns the number of scopes nested in s.
type Selection ¶
type Selection struct {
// contains filtered or unexported fields
}
A Selection describes a selector expression x.f. For the declarations:
type T struct{ x int; E } type E struct{} func (e E) m() {} var p *T
the following relations exist:
Selector Kind Recv Obj Type Index Indirect p.x FieldVal T x int {0} true p.m MethodVal *T m func (e *T) m() {1, 0} true T.m MethodExpr T m func m(_ T) {1, 0} false
func (*Selection) Index ¶
Index describes the path from x to f in x.f. The last index entry is the field or method index of the type declaring f; either:
- the list of declared methods of a named type; or
- the list of methods of an interface type; or
- the list of fields of a struct type.
The earlier index entries are the indices of the embedded fields implicitly traversed to get from (the type of) x to f, starting at embedding depth 0.
func (*Selection) Indirect ¶
Indirect reports whether any pointer indirection was required to get from x to f in x.f.
type SelectionKind ¶
type SelectionKind int
SelectionKind describes the kind of a selector expression x.f (excluding qualified identifiers).
const ( FieldVal SelectionKind = iota // x.f is a struct field selector MethodVal // x.f is a method selector MethodExpr // x.f is a method expression )
type Signature ¶
type Signature struct {
// contains filtered or unexported fields
}
A Signature represents a (non-builtin) function or method type.
func NewSignature ¶
NewSignature returns a new function type for the given receiver, parameters, and results, either of which may be nil. If variadic is set, the function is variadic, it must have at least one parameter, and the last parameter must be of unnamed slice type.
func (*Signature) Recv ¶
Recv returns the receiver of signature s (if a method), or nil if a function.
For an abstract method, Recv returns the enclosing interface either as a *Named or an *Interface. Due to embedding, an interface may contain methods whose receiver type is a different interface.
func (*Signature) Underlying ¶
type Sizes ¶
type Sizes interface { // Alignof returns the alignment of a variable of type T. // Alignof must implement the alignment guarantees required by the spec. Alignof(T Type) int64 // Offsetsof returns the offsets of the given struct fields, in bytes. // Offsetsof must implement the offset guarantees required by the spec. Offsetsof(fields []*Var) []int64 // Sizeof returns the size of a variable of type T. // Sizeof must implement the size guarantees required by the spec. Sizeof(T Type) int64 }
Sizes defines the sizing functions for package unsafe.
type Slice ¶
type Slice struct {
// contains filtered or unexported fields
}
A Slice represents a slice type.
func (*Slice) Underlying ¶
type StdSizes ¶
type StdSizes struct { WordSize int64 // word size in bytes - must be >= 4 (32bits) MaxAlign int64 // maximum alignment in bytes - must be >= 1 }
StdSizes is a convenience type for creating commonly used Sizes. It makes the following simplifying assumptions:
- The size of explicitly sized basic types (int16, etc.) is the specified size.
- The size of strings and interfaces is 2*WordSize.
- The size of slices is 3*WordSize.
- The size of an array of n elements corresponds to the size of a struct of n consecutive fields of the array's element type.
- The size of a struct is the offset of the last field plus that field's size. As with all element types, if the struct is used in an array its size must first be aligned to a multiple of the struct's alignment.
- All other types have size WordSize.
- Arrays and structs are aligned per spec definition; all other types are naturally aligned with a maximum alignment MaxAlign.
*StdSizes implements Sizes.
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
A Struct represents a struct type.
func NewStruct ¶
NewStruct returns a new struct with the given fields and corresponding field tags. If a field with index i has a tag, tags[i] must be that tag, but len(tags) may be only as long as required to hold the tag with the largest index i. Consequently, if no field has a tag, tags may be nil.
func (*Struct) NumFields ¶
NumFields returns the number of fields in the struct (including blank and anonymous fields).
func (*Struct) Underlying ¶
type Tuple ¶
type Tuple struct {
// contains filtered or unexported fields
}
A Tuple represents an ordered list of variables; a nil *Tuple is a valid (empty) tuple. Tuples are used as components of signatures and to represent the type of multiple assignments; they are not first class types of Go.
func (*Tuple) Underlying ¶
type Type ¶
type Type interface { // Underlying returns the underlying type of a type. Underlying() Type // String returns a string representation of a type. String() string }
A Type represents a type of Go. All types implement the Type interface.
func New ¶
New is a convenience function to create a new type from a given expression or type literal string evaluated in Universe scope. New(str) is shorthand for Eval(str, nil, nil), but only returns the type result, and panics in case of an error. Position info for objects in the result type is undefined.
type TypeAndValue ¶
TypeAndValue reports the type and value (for constants) of the corresponding expression.
func Eval ¶
func Eval(str string, pkg *Package, scope *Scope) (TypeAndValue, error)
Eval returns the type and, if constant, the value for the expression or type literal string str evaluated in scope. If the expression contains function literals, the function bodies are ignored (though they must be syntactically correct).
If pkg == nil, the Universe scope is used and the provided scope is ignored. Otherwise, the scope must belong to the package (either the package scope, or nested within the package scope).
An error is returned if the scope is incorrect, the string has syntax errors, or if it cannot be evaluated in the scope. Position info for objects in the result type is undefined.
Note: Eval should not be used instead of running Check to compute types and values, but in addition to Check. Eval will re-evaluate its argument each time, and it also does not know about the context in which an expression is used (e.g., an assignment). Thus, top- level untyped constants will return an untyped type rather then the respective context-specific type.
func EvalNode ¶
func EvalNode(fset *token.FileSet, node ast.Expr, pkg *Package, scope *Scope) (tv TypeAndValue, err error)
EvalNode is like Eval but instead of string it accepts an expression node and respective file set.
An error is returned if the scope is incorrect if the node cannot be evaluated in the scope.
func (TypeAndValue) Addressable ¶
func (tv TypeAndValue) Addressable() bool
Addressable reports whether the corresponding expression is addressable (http://golang.org/ref/spec#Address_operators).
func (TypeAndValue) Assignable ¶
func (tv TypeAndValue) Assignable() bool
Assignable reports whether the corresponding expression is assignable to (provided a value of the right type).
func (TypeAndValue) HasOk ¶
func (tv TypeAndValue) HasOk() bool
HasOk reports whether the corresponding expression may be used on the lhs of a comma-ok assignment.
func (TypeAndValue) IsBuiltin ¶
func (tv TypeAndValue) IsBuiltin() bool
IsBuiltin reports whether the corresponding expression denotes a (possibly parenthesized) built-in function.
func (TypeAndValue) IsNil ¶
func (tv TypeAndValue) IsNil() bool
IsNil reports whether the corresponding expression denotes the predeclared value nil.
func (TypeAndValue) IsType ¶
func (tv TypeAndValue) IsType() bool
IsType reports whether the corresponding expression specifies a type.
func (TypeAndValue) IsValue ¶
func (tv TypeAndValue) IsValue() bool
IsValue reports whether the corresponding expression is a value. Builtins are not considered values. Constant values have a non- nil Value.
func (TypeAndValue) IsVoid ¶
func (tv TypeAndValue) IsVoid() bool
IsVoid reports whether the corresponding expression is a function call without results.
type TypeName ¶
type TypeName struct {
// contains filtered or unexported fields
}
A TypeName represents a declared type.
Source Files ¶
- api.go
- assignments.go
- builtins.go
- call.go
- check.go
- conversions.go
- decl.go
- errors.go
- eval.go
- expr.go
- exprstring.go
- go12.go
- initorder.go
- labels.go
- lookup.go
- methodset.go
- methodsetcache.go
- object.go
- objset.go
- operand.go
- ordering.go
- package.go
- predicates.go
- resolver.go
- return.go
- scope.go
- selection.go
- sizes.go
- stmt.go
- type.go
- typestring.go
- typexpr.go
- universe.go