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 ¶
- Variables
- func FindGcExportData(r *bufio.Reader) (err error)
- func FindPkg(path, srcDir string) (filename, id string)
- 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 ¶
This section is empty.
Variables ¶
var ( Universe *Scope Unsafe *Package )
var Default = Context{
IntSize: 8,
PtrSize: 8,
}
Default is the default context for type checking.
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 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.
Types ¶
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 { Name string Type Type Val interface{} // contains filtered or unexported fields }
A Const represents a declared constant.
type Context ¶
type Context struct { IntSize int64 // size in bytes of int and uint values PtrSize int64 // size in bytes of pointers // If Error is not nil, it is called with each error found // during type checking. Most error messages have accurate // position information; those error strings are formatted // filename:line:column: message. Error func(err error) // If Ident is not 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 is not 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 is not nil, it is used instead of GcImport. Import Importer }
A Context specifies the supporting context for type checking.
func (*Context) Check ¶
Check resolves and typechecks a set of package files within the given context. The package files' ASTs are augmented by assigning types to ast.Objects. If there are no errors, Check returns the package, otherwise it returns the first error. If the context's Error handler is nil, Check terminates as soon as the first error is encountered.
CAUTION: At the moment, the returned *ast.Package only contains the package
name and scope - the other fields are not set up. The returned *Package contains the name and imports (but no scope yet). Once we have the scope moved from *ast.Scope to *Scope, only *Package will be returned.
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 { 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 { 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 Name string // unqualified type name for anonymous fields }
A QualifiedName is a name qualified with the package that declared the name.
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.
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 Type ¶
type Type interface {
// contains filtered or unexported methods
}
All types implement the Type interface.
type TypeName ¶
type TypeName struct { Name string Type Type // *NamedType or *Basic // contains filtered or unexported fields }
A TypeName represents a declared type.