types

package
v0.0.0-...-1d03baa Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2013 License: BSD-3-Clause 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

This section is empty.

Variables

View Source
var (
	Universe *Scope
	Unsafe   *Package
)
View Source
var Default = Context{

	IntSize: 8,
	PtrSize: 8,
}

Default is the default context for type checking.

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

Types

type Array

type Array struct {
	Len int64
	Elt Type
}

An Array represents an array type [Len]Elt.

type Basic

type Basic struct {
	Kind BasicKind
	Info BasicInfo
	Size int64
	Name string
}

A Basic represents a basic type.

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.

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 {
	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) GetPos

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

func (*Const) GetType

func (obj *Const) GetType() Type

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

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

func (*Func) GetName

func (obj *Func) GetName() string

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

type Map

type Map struct {
	Key, Elt Type
}

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

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.

type NilType

type NilType struct{}

Representation of nil.

func (NilType) String

func (NilType) String() string

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 Check

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

Check is shorthand for Default.Check.

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

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.

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.

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 Slice

type Slice struct {
	Elt Type
}

A Slice represents a slice type []Elt.

type Struct

type Struct struct {
	Fields []*Field
}

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

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.

func (*TypeName) GetName

func (obj *TypeName) GetName() string

func (*TypeName) GetPos

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

func (*TypeName) GetType

func (obj *TypeName) GetType() Type

type Var

type Var struct {
	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) 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