Documentation ¶
Overview ¶
Package types declares the data structures for representing Go types and implements typechecking of package files.
WARNING: THE TYPES API IS SUBJECT TO CHANGE.
Index ¶
- Constants
- Variables
- func DefaultAlignof(typ Type) int64
- func DefaultOffsetsof(fields []*Field) []int64
- func DefaultSizeof(typ Type) int64
- func FindGcExportData(r *bufio.Reader) (err error)
- func FindPkg(path, srcDir string) (filename, id string)
- func IsIdentical(x, y Type) bool
- type Array
- type Basic
- type BasicInfo
- type BasicKind
- type Chan
- type Complex
- type Const
- type Context
- type Field
- type Func
- type Importer
- type Interface
- type Map
- type Method
- type NamedType
- type NilType
- type Object
- type Package
- type Pointer
- type QualifiedName
- type Result
- type Scope
- type Signature
- type Slice
- type Struct
- type Type
- type TypeName
- type Var
Constants ¶
const DefaultMaxAlign = 8
DefaultMaxAlign is the default maximum alignment, in bytes, used by DefaultAlignof.
const DefaultPtrSize = 8
DefaultPtrSize is the default size of ints, uint, and pointers, in bytes, used by DefaultSizeof.
Variables ¶
var ( Universe *Scope Unsafe *Package )
var Typ = [...]*Basic{ Invalid: {Invalid, 0, 0, "invalid type"}, Bool: {Bool, IsBoolean, 1, "bool"}, Int: {Int, IsInteger, 0, "int"}, Int8: {Int8, IsInteger, 1, "int8"}, Int16: {Int16, IsInteger, 2, "int16"}, Int32: {Int32, IsInteger, 4, "int32"}, Int64: {Int64, IsInteger, 8, "int64"}, Uint: {Uint, IsInteger | IsUnsigned, 0, "uint"}, Uint8: {Uint8, IsInteger | IsUnsigned, 1, "uint8"}, Uint16: {Uint16, IsInteger | IsUnsigned, 2, "uint16"}, Uint32: {Uint32, IsInteger | IsUnsigned, 4, "uint32"}, Uint64: {Uint64, IsInteger | IsUnsigned, 8, "uint64"}, Uintptr: {Uintptr, IsInteger | IsUnsigned, 0, "uintptr"}, Float32: {Float32, IsFloat, 4, "float32"}, Float64: {Float64, IsFloat, 8, "float64"}, Complex64: {Complex64, IsComplex, 8, "complex64"}, Complex128: {Complex128, IsComplex, 16, "complex128"}, String: {String, IsString, 0, "string"}, UnsafePointer: {UnsafePointer, 0, 0, "Pointer"}, UntypedBool: {UntypedBool, IsBoolean | IsUntyped, 0, "untyped boolean"}, UntypedInt: {UntypedInt, IsInteger | IsUntyped, 0, "untyped integer"}, UntypedRune: {UntypedRune, IsInteger | IsUntyped, 0, "untyped rune"}, UntypedFloat: {UntypedFloat, IsFloat | IsUntyped, 0, "untyped float"}, UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, 0, "untyped complex"}, UntypedString: {UntypedString, IsString | IsUntyped, 0, "untyped string"}, UntypedNil: {UntypedNil, IsUntyped, 0, "untyped nil"}, }
Predeclared types, indexed by BasicKind.
Functions ¶
func DefaultAlignof ¶
DefaultAlignof implements the default alignment computation for unsafe.Alignof. It is used if Context.Alignof == nil.
func DefaultOffsetsof ¶
DefaultOffsetsof implements the default field offset computation for unsafe.Offsetof. It is used if Context.Offsetsof == nil.
func DefaultSizeof ¶
DefaultSizeof implements the default size computation for unsafe.Sizeof. It is used if Context.Sizeof == nil.
func FindGcExportData ¶
FindGcExportData positions the reader r at the beginning of the export data section of an underlying GC-created object/archive file by reading from it. The reader must be positioned at the start of the file before calling this function.
func FindPkg ¶
FindPkg returns the filename and unique package id for an import path based on package information provided by build.Import (using the build.Default build.Context). If no file was found, an empty filename is returned.
func IsIdentical ¶
IsIdentical returns true if x and y are identical.
Types ¶
type Basic ¶
type Basic struct { Kind BasicKind Info BasicInfo Name string // contains filtered or unexported fields }
A Basic represents a basic type.
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 Const ¶
type Const struct { Pkg *Package Name string Type Type Val interface{} // contains filtered or unexported fields }
A Const represents a declared constant.
type Context ¶
type Context struct { // If Error != nil, it is called with each error found // during type checking. The error strings of errors with // detailed position information are formatted as follows: // filename:line:column: message Error func(err error) // If Ident != nil, it is called for each identifier id // denoting an Object in the files provided to Check, and // obj is the denoted object. // Ident is not called for fields and methods in struct or // interface types or composite literals, or for blank (_) // or dot (.) identifiers in dot-imports. // TODO(gri) Consider making Fields and Methods ordinary // Objects - than we could lift this restriction. Ident func(id *ast.Ident, obj Object) // If Expr != nil, it is called for each expression x that is // type-checked: typ is the expression type, and val is the value // if x is constant, val is nil otherwise. // // Constants are represented as follows: // // bool -> bool // numeric -> int64, *big.Int, *big.Rat, Complex // string -> string // nil -> NilType // // Constant values are normalized, that is, they are represented // using the "smallest" possible type that can represent the value. // For instance, 1.0 is represented as an int64 because it can be // represented accurately as an int64. Expr func(x ast.Expr, typ Type, val interface{}) // If Import != nil, it is called for each imported package. // Otherwise, GcImporter is called. Import Importer // If Alignof != nil, it is called to determine the alignment // of the given type. Otherwise DefaultAlignmentof is called. // Alignof must implement the alignment guarantees required by // the spec. Alignof func(Type) int64 // If Offsetsof != nil, it is called to determine the offsets // of the given struct fields, in bytes. Otherwise DefaultOffsetsof // is called. Offsetsof must implement the offset guarantees // required by the spec. Offsetsof func(fields []*Field) []int64 // If Sizeof != nil, it is called to determine the size of the // given type. Otherwise, DefaultSizeof is called. Sizeof must // implement the size guarantees required by the spec. Sizeof func(Type) int64 }
A Context specifies the supporting context for type checking. An empty Context is a ready-to-use default context.
func (*Context) Check ¶
Check resolves and typechecks a set of package files within the given context. It returns the package and the first error encountered, if any. If the context's Error handler is nil, Check terminates as soon as the first error is encountered; otherwise it continues until the entire package is checked. If there are errors, the package may be only partially type-checked, and the resulting package may be incomplete (missing objects, imports, etc.).
type Field ¶
type Field struct { QualifiedName Type Type Tag string IsAnonymous bool }
A Field represents a field of a struct.
type Func ¶
type Func struct { Pkg *Package Name string Type Type // *Signature or *Builtin // contains filtered or unexported fields }
A Func represents a declared function.
type Importer ¶
An Importer resolves import paths to Package objects. The imports map records the packages already imported, indexed by package id (canonical import path). An Importer must determine the canonical import path and check the map to see if it is already present in the imports map. If so, the Importer can return the map entry. Otherwise, the Importer should load the package data for the given path into a new *Package, record pkg in the imports map, and then return pkg.
type Interface ¶
type Interface struct {
Methods []*Method // TODO(gri) consider keeping them in sorted order
}
An Interface represents an interface type interface{...}.
type NamedType ¶
type NamedType struct { Obj *TypeName // corresponding declared object Underlying Type // nil if not fully declared yet; never a *NamedType Methods []*Method // TODO(gri) consider keeping them in sorted order }
A NamedType represents a named type as declared in a type declaration.
type Object ¶
type Object interface { GetPkg() *Package GetName() string GetType() Type GetPos() token.Pos // 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.
type Package ¶
type Package struct { Name string Path string // import path, "" for current (non-imported) package Scope *Scope // package-level scope Imports map[string]*Package // map of import paths to imported packages Complete bool // if set, this package was imported completely // contains filtered or unexported fields }
A Package represents the contents (objects) of a Go package.
func GcImport ¶
GcImport imports a gc-generated package given its import path, adds the corresponding package object to the imports map, and returns the object. Local import paths are interpreted relative to the current working directory. The imports map must contains all packages already imported. GcImport satisfies the ast.Importer signature.
func GcImportData ¶
func GcImportData(imports map[string]*Package, filename, id string, data *bufio.Reader) (pkg *Package, err error)
GcImportData imports a package by reading the gc-generated export data, adds the corresponding package object to the imports map indexed by id, and returns the object.
The imports map must contains all packages already imported. The data reader position must be the beginning of the export data section. The filename is only used in error messages.
If imports[id] contains the completely imported package, that package can be used directly, and there is no need to call this function (but there is also no harm but for extra time used).
type QualifiedName ¶
type QualifiedName struct { Pkg *Package // nil only for predeclared error.Error (exported) Name string // unqualified type name for anonymous fields }
A QualifiedName is a name qualified with the package that declared the name. Note: Pkg may be a fake package (no name, no scope) because the GC compiler's
export information doesn't provide full information in some cases.
TODO(gri): Should change Pkg to PkgPath since it's the only thing we care about.
func (QualifiedName) IsSame ¶
func (p QualifiedName) IsSame(q QualifiedName) bool
IsSame reports whether p and q are the same.
type Result ¶
type Result struct {
Values []*Var // Signature.Results of the function called
}
A Result represents a (multi-value) function call result.
type Scope ¶
type Scope struct { Outer *Scope Entries []Object // scope entries in insertion order // contains filtered or unexported fields }
A Scope maintains the set of named language entities declared in the scope and a link to the immediately surrounding (outer) scope.
func (*Scope) Insert ¶
Insert attempts to insert an object obj into scope s. If s already contains an object with the same name, Insert leaves s unchanged and returns that object. Otherwise it inserts obj and returns nil.
type Signature ¶
type Signature struct { Recv *Var // nil if not a method Params []*Var // (incoming) parameters from left to right; or nil Results []*Var // (outgoing) results from left to right; or nil IsVariadic bool // true if the last parameter's type is of the form ...T }
A Signature represents a user-defined function type func(...) (...).
type Struct ¶
type Struct struct { Fields []*Field // contains filtered or unexported fields }
A Struct represents a struct type struct{...}.
type Type ¶
type Type interface { String() string // contains filtered or unexported methods }
All types implement the Type interface.
type TypeName ¶
type TypeName struct { Pkg *Package Name string Type Type // *NamedType or *Basic // contains filtered or unexported fields }
A TypeName represents a declared type.