types

package
v19.10.22+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2019 License: MIT Imports: 14 Imported by: 0

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

View Source
const DefaultMaxAlign = 8

DefaultMaxAlign is the default maximum alignment, in bytes, used by DefaultAlignof.

View Source
const DefaultPtrSize = 8

DefaultPtrSize is the default size of ints, uint, and pointers, in bytes, used by DefaultSizeof.

Variables

View Source
var (
	Universe *Scope
	Unsafe   *Package
)
View Source
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

func DefaultAlignof(typ Type) int64

DefaultAlignof implements the default alignment computation for unsafe.Alignof. It is used if Context.Alignof == nil.

func DefaultOffsetsof

func DefaultOffsetsof(fields []*Field) []int64

DefaultOffsetsof implements the default field offset computation for unsafe.Offsetof. It is used if Context.Offsetsof == nil.

func DefaultSizeof

func DefaultSizeof(typ Type) int64

DefaultSizeof implements the default size computation for unsafe.Sizeof. It is used if Context.Sizeof == nil.

func FindGcExportData

func FindGcExportData(r *bufio.Reader) (err error)

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

func FindPkg(path, srcDir string) (filename, id string)

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

func IsIdentical(x, y Type) bool

IsIdentical returns true if x and y are identical.

Types

type Array

type Array struct {
	Len int64
	Elt Type
}

An Array represents an array type [Len]Elt.

func (*Array) String

func (t *Array) String() string

type Basic

type Basic struct {
	Kind BasicKind
	Info BasicInfo

	Name string
	// contains filtered or unexported fields
}

A Basic represents a basic type.

func (*Basic) String

func (t *Basic) String() string

type BasicInfo

type BasicInfo int

BasicInfo is a set of flags describing properties of a basic type.

const (
	IsBoolean BasicInfo = 1 << iota
	IsInteger
	IsUnsigned
	IsFloat
	IsComplex
	IsString
	IsUntyped

	IsOrdered   = IsInteger | IsFloat | IsString
	IsNumeric   = IsInteger | IsFloat | IsComplex
	IsConstType = IsBoolean | IsNumeric | IsString
)

Properties of basic types.

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 Chan

type Chan struct {
	Dir ast.ChanDir
	Elt Type
}

A Chan represents a channel type chan Elt, <-chan Elt, or chan<-Elt.

func (*Chan) String

func (t *Chan) String() string

type Complex

type Complex struct {
	Re, Im *big.Rat
}

Representation of complex numbers.

func (Complex) String

func (c Complex) String() string

type Const

type Const struct {
	Pkg  *Package
	Name string
	Type Type
	Val  interface{}
	// contains filtered or unexported fields
}

A Const represents a declared constant.

func (*Const) GetName

func (obj *Const) GetName() string

func (*Const) GetPkg

func (obj *Const) GetPkg() *Package

func (*Const) GetPos

func (obj *Const) GetPos() token.Pos

func (*Const) GetType

func (obj *Const) GetType() Type

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

func (ctxt *Context) Check(fset *token.FileSet, files []*ast.File) (*Package, error)

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.

func (*Func) GetName

func (obj *Func) GetName() string

func (*Func) GetPkg

func (obj *Func) GetPkg() *Package

func (*Func) GetPos

func (obj *Func) GetPos() token.Pos

func (*Func) GetType

func (obj *Func) GetType() Type

type Importer

type Importer func(imports map[string]*Package, path string) (pkg *Package, err error)

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{...}.

func (*Interface) String

func (t *Interface) String() string

type Map

type Map struct {
	Key, Elt Type
}

A Map represents a map type map[Key]Elt.

func (*Map) String

func (t *Map) String() string

type Method

type Method struct {
	QualifiedName
	Type *Signature
}

A Method represents a method.

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.

func (*NamedType) String

func (t *NamedType) String() string

type NilType

type NilType struct{}

Representation of nil.

func (NilType) String

func (NilType) String() string

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 Check

func Check(fset *token.FileSet, files []*ast.File) (*Package, error)

Check is shorthand for ctxt.Check where ctxt is a default (empty) context.

func GcImport

func GcImport(imports map[string]*Package, path string) (pkg *Package, err error)

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).

func (*Package) GetName

func (obj *Package) GetName() string

func (*Package) GetPkg

func (obj *Package) GetPkg() *Package

func (*Package) GetPos

func (obj *Package) GetPos() token.Pos

func (*Package) GetType

func (obj *Package) GetType() Type

type Pointer

type Pointer struct {
	Base Type
}

A Pointer represents a pointer type *Base.

func (*Pointer) String

func (t *Pointer) String() string

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.

func (*Result) String

func (t *Result) String() string

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

func (s *Scope) Insert(obj Object) Object

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.

func (*Scope) Lookup

func (s *Scope) Lookup(name string) Object

Lookup returns the object with the given name if it is found in scope s, otherwise it returns nil. Outer scopes are ignored.

func (*Scope) String

func (s *Scope) String() string

Debugging support

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(...) (...).

func (*Signature) String

func (t *Signature) String() string

type Slice

type Slice struct {
	Elt Type
}

A Slice represents a slice type []Elt.

func (*Slice) String

func (t *Slice) String() string

type Struct

type Struct struct {
	Fields []*Field
	// contains filtered or unexported fields
}

A Struct represents a struct type struct{...}.

func (*Struct) String

func (t *Struct) String() string

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.

func (*TypeName) GetName

func (obj *TypeName) GetName() string

func (*TypeName) GetPkg

func (obj *TypeName) GetPkg() *Package

func (*TypeName) GetPos

func (obj *TypeName) GetPos() token.Pos

func (*TypeName) GetType

func (obj *TypeName) GetType() Type

type Var

type Var struct {
	Pkg  *Package // nil for parameters
	Name string
	Type Type
	// contains filtered or unexported fields
}

A Variable represents a declared variable (including function parameters and results).

func (*Var) GetName

func (obj *Var) GetName() string

func (*Var) GetPkg

func (obj *Var) GetPkg() *Package

func (*Var) GetPos

func (obj *Var) GetPos() token.Pos

func (*Var) GetType

func (obj *Var) GetType() Type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL