types

package
v0.0.0-...-94861f8 Latest Latest
Warning

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

Go to latest
Published: May 3, 2017 License: BSD-3-Clause, Unlicense Imports: 7 Imported by: 0

Documentation

Overview

Package types provide a type-checker for the SubC language.

Index

Constants

This section is empty.

Variables

View Source
var Typ = [...]*Basic{
	Invalid: {Invalid, 0, "invalid type"},

	Bool:    {Bool, IsBoolean, "_Bool"},
	Complex: {Complex, IsComplex, "_Complex"},
	Char:    {Char, IsInteger, "char"},
	Short:   {Short, IsInteger, "short"},
	Int:     {Int, IsInteger, "int"},
	Long:    {Int, IsInteger, "long"},
	Float:   {Float, IsFloat, "float"},
	Double:  {Double, IsFloat, "double"},
	Void:    {Void, IsVoid, "void"},

	UntypedString: {UntypedString, IsString | IsUntyped, "untyped string"},
}

Typ is an array containing all the basic types.

Functions

func ExprString

func ExprString(x ast.Expr) string

ExprString returns a pretty printed version of an expression.

func Identical

func Identical(x, y Type) bool

Identical returns if two types are identical.

func ObjectString

func ObjectString(obj Object) string

ObjectString returns a pretty-printed version of an object.

func SelectionString

func SelectionString(s *Selection) string

SelectionString returns a pretty printed output of a selection.

func TypeString

func TypeString(typ Type) string

TypeString returns a pretty printed string of a type.

func WriteExpr

func WriteExpr(buf *bytes.Buffer, x ast.Expr)

WriteExpr writes a pretty printed version of an expression.

func WriteSignature

func WriteSignature(buf *bytes.Buffer, sig *Signature)

WriteSignature writes the representation of the signature sig to buf,

func WriteType

func WriteType(buf *bytes.Buffer, typ Type)

WriteType returns a pretty printed string of a type.

Types

type Array

type Array struct {
	// contains filtered or unexported fields
}

Array represents an array.

func NewArray

func NewArray(elem Type, len int64) *Array

NewArray creates an array.

func (*Array) Elem

func (t *Array) Elem() Type

func (*Array) Len

func (t *Array) Len() int64

func (*Array) String

func (t *Array) String() string

func (*Array) Underlying

func (t *Array) Underlying() Type

type Basic

type Basic struct {
	// contains filtered or unexported fields
}

Basic represents a native type like char or int.

func (*Basic) String

func (t *Basic) String() string

func (*Basic) Underlying

func (t *Basic) Underlying() Type

type BasicInfo

type BasicInfo int

Basic Types.

const (
	IsBoolean BasicInfo = 1 << iota // boolean type
	IsInteger                       // integer type
	IsFloat                         // floating point type
	IsComplex                       // complex type
	IsString                        // a string
	IsUntyped                       // untyped, mainly for literals
	IsVoid                          // void type

	IsNumeric = IsInteger // an integer or pointer
)

Basic type information.

type BasicType

type BasicType int

Basic Types.

const (
	Invalid BasicType = iota

	Char
	Short
	Int
	Long
	Float
	Double
	Bool
	Complex
	Void

	UntypedString
)

Various basic types.

type Config

type Config struct {
	MaxErrors int   // the maximum number of errors before bailing out
	Sizes     Sizes // used to determine the size, offset of and alignment of types.
}

Config is used to control the behavior of the type checker while it is running.

type Const

type Const struct {
	// contains filtered or unexported fields
}

Const represents a constant value.

func NewConst

func NewConst(pos scanner.Position, name string, typ Type, val constant.Value) *Const

NewConst creates a new constant declaration.

func (*Const) Name

func (obj *Const) Name() string

func (*Const) Parent

func (obj *Const) Parent() *Scope

func (*Const) Pos

func (obj *Const) Pos() scanner.Position

func (*Const) String

func (obj *Const) String() string

func (*Const) Type

func (obj *Const) Type() Type

type Enum

type Enum struct {
	// contains filtered or unexported fields
}

Enum represent enums.

func NewEnum

func NewEnum(values []*Const) *Enum

NewEnum creates a new enum.

func (*Enum) String

func (t *Enum) String() string

func (*Enum) Underlying

func (t *Enum) Underlying() Type

type Func

type Func struct {
	// contains filtered or unexported fields
}

Func represents a function.

func NewFunc

func NewFunc(pos scanner.Position, storage Storage, name string, sig *Signature) *Func

NewFunc creates a new function declaration.

func (*Func) Name

func (obj *Func) Name() string

func (*Func) Parent

func (obj *Func) Parent() *Scope

func (*Func) Pos

func (obj *Func) Pos() scanner.Position

func (*Func) Storage

func (obj *Func) Storage() Storage

func (*Func) String

func (obj *Func) String() string

func (*Func) Type

func (obj *Func) Type() Type

type Fwrd

type Fwrd struct {
	// contains filtered or unexported fields
}

Fwrd represents a forward declaration.

func NewFwrd

func NewFwrd(name string, objs ...Object) *Fwrd

NewFwrd creates a new forward declaration.

func (*Fwrd) Name

func (obj *Fwrd) Name() string

func (*Fwrd) Parent

func (obj *Fwrd) Parent() *Scope

func (*Fwrd) Pos

func (obj *Fwrd) Pos() scanner.Position

func (*Fwrd) String

func (obj *Fwrd) String() string

func (*Fwrd) Type

func (obj *Fwrd) Type() Type

type Info

type Info struct {
	// stores all the type and values of the expression, values are stored if
	// the expression is constant
	Types map[ast.Expr]TypeAndValue

	// used to store the declaration of objects based on their identifier
	Defs map[*ast.Ident]Object

	// stores identifier that are used, it can be used to lookup to its definitions.
	Uses map[*ast.Ident]Object

	// selections from struct and unions
	Selections map[*ast.SelectorExpr]*Selection

	// scope information
	Scopes map[ast.Node]*Scope
}

Info contains the type checked information after the type checker is ran.

func Check

func Check(conf Config, prog *ast.Prog) (*Info, error)

Check type checks an AST tree and return an type information structure of map structures indexed by the AST tree nodes.

type Label

type Label struct {
	// contains filtered or unexported fields
}

Label represents a label.

func NewLabel

func NewLabel(pos scanner.Position, name string) *Label

NewLabel creates a new label declaration.

func (*Label) Name

func (obj *Label) Name() string

func (*Label) Parent

func (obj *Label) Parent() *Scope

func (*Label) Pos

func (obj *Label) Pos() scanner.Position

func (*Label) String

func (obj *Label) String() string

func (*Label) Type

func (obj *Label) Type() Type

type Named

type Named struct {
	// contains filtered or unexported fields
}

Named represents a name that has been defined by a record. An example is struct file fd, fd will be the Named in this case.

func NewNamed

func NewNamed(obj *TypeName, underlying Type) *Named

NewNamed creates a named.

func (*Named) Obj

func (t *Named) Obj() *TypeName

func (*Named) String

func (t *Named) String() string

func (*Named) Underlying

func (t *Named) Underlying() Type

type Namespace

type Namespace int

Namespace represents a namespace for the language

const (
	Ord Namespace = iota // local variables, global variables, enums, etc
	Tag                  // struct and function declarations
	Lab                  // labels
	Fwd                  // forward declarations
)

Possible namespaces for an declaration to be in.

func (Namespace) String

func (ns Namespace) String() string

type Object

type Object interface {
	Parent() *Scope        // returns the parent scope of the object is in
	Pos() scanner.Position // the position of the object in the source code
	Name() string          // the name of the object
	Type() Type            // the type of the object
	String() string        // the string of the object
	// contains filtered or unexported methods
}

Object represents a type checked object.

type Pointer

type Pointer struct {
	// contains filtered or unexported fields
}

Pointer represents a pointer.

func NewPointer

func NewPointer(base Type, decay *Array) *Pointer

NewPointer creates a new pointer. The decay represents if this was an array that decayed to a pointer when passing in a function or using it in operations.

func (*Pointer) Decay

func (t *Pointer) Decay() *Array

func (*Pointer) Elem

func (t *Pointer) Elem() Type

func (*Pointer) String

func (t *Pointer) String() string

func (*Pointer) Underlying

func (t *Pointer) Underlying() Type

type Record

type Record struct {
	// contains filtered or unexported fields
}

Record represents struct and union types.

func NewRecord

func NewRecord(union bool, fields []*Var) *Record

NewRecord creates a new record.

func (*Record) Field

func (t *Record) Field(i int) *Var

func (*Record) IsUnion

func (t *Record) IsUnion() bool

func (*Record) NumFields

func (t *Record) NumFields() int

func (*Record) String

func (t *Record) String() string

func (*Record) Underlying

func (t *Record) Underlying() Type

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope represent a scope.

func NewScope

func NewScope(parent *Scope, span scan.Span) *Scope

NewScope returns a new scope with a span boundary specified by span.

func (*Scope) Insert

func (s *Scope) Insert(ns Namespace, obj Object) Object

Insert inserts into a namespace the object, if it already exists, it returns the existing object without overwriting it.

func (*Scope) Lookup

func (s *Scope) Lookup(ns Namespace, name string) Object

Lookup looks up a name in a namespace.

func (*Scope) LookupParent

func (s *Scope) LookupParent(ns Namespace, name string, pos scanner.Position) (*Scope, Object)

LookupParent will recursively look up to the top level for the object name.

func (*Scope) Parent

func (s *Scope) Parent() *Scope

Parent returns the parent scope.

type Selection

type Selection struct {
	// contains filtered or unexported fields
}

Selection represents a selection expression. Examples include a.x or a->x.

func (*Selection) Indirect

func (s *Selection) Indirect() bool

func (*Selection) IsUnion

func (s *Selection) IsUnion() bool

func (*Selection) Obj

func (s *Selection) Obj() Object

func (*Selection) Offset

func (s *Selection) Offset() int64

func (*Selection) String

func (s *Selection) String() string

func (*Selection) Type

func (s *Selection) Type() Type

type Signature

type Signature struct {
	// contains filtered or unexported fields
}

Signature represents a signature.

func NewSignature

func NewSignature(params *Tuple, result *Var, variadic bool) *Signature

NewSignature creates a new signature.

func (*Signature) Params

func (t *Signature) Params() *Tuple

func (*Signature) Result

func (t *Signature) Result() *Var

func (*Signature) String

func (t *Signature) String() string

func (*Signature) Underlying

func (t *Signature) Underlying() Type

func (*Signature) Variadic

func (t *Signature) Variadic() bool

type Sizes

type Sizes interface {
	// Alignof returns the alignment of a variable of type T.
	Alignof(T Type) int64

	// Offsetsof returns the offsets of the given struct fields, in bytes.
	Offsetsof(fields []*Var) []int64

	// Sizeof returns the size of a variable of type T.
	Sizeof(T Type) int64
}

Sizes defines the sizing functions.

type StdSizes

type StdSizes struct {
	WordSize int64 // word size in bytes - must be >= 2
	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 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 alignments are aligned against WordSize.
  • 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.

func (*StdSizes) Alignof

func (s *StdSizes) Alignof(T Type) int64

func (*StdSizes) Offsetsof

func (s *StdSizes) Offsetsof(fields []*Var) []int64

Offsetof returns the offset of set of fields. All of the fields are aligned to the nearest word size boundary.

func (*StdSizes) Sizeof

func (s *StdSizes) Sizeof(T Type) int64

Sizeof returns the size of a type.

type Storage

type Storage uint

Storage represents a storage location.

const (
	Auto         Storage = 1 << iota // local declaration storage.
	Public                           // global storage (file scope)
	Extern                           // external storage, for using them before linking resolution.
	GlobalStatic                     // file scope static variables
	LocalStatic                      // function scope static variables
)

Storage locations.

func (Storage) String

func (s Storage) String() string

type Tuple

type Tuple struct {
	// contains filtered or unexported fields
}

Tuple represents function arguments.

func NewTuple

func NewTuple(vars ...*Var) *Tuple

NewTuple creates a tuple.

func (*Tuple) Len

func (t *Tuple) Len() int

func (*Tuple) String

func (t *Tuple) String() string

func (*Tuple) Underlying

func (t *Tuple) Underlying() Type

type Type

type Type interface {
	Underlying() Type
	String() string
}

Type represents a type.

type TypeAndValue

type TypeAndValue struct {
	Type  Type
	Value constant.Value
	// contains filtered or unexported fields
}

TypeAndValue represents a type and a value.

func (TypeAndValue) Addressable

func (tv TypeAndValue) Addressable() bool

func (TypeAndValue) Assignable

func (tv TypeAndValue) Assignable() bool

func (TypeAndValue) IsType

func (tv TypeAndValue) IsType() bool

IsType returns if the this mode is a type expression.

func (TypeAndValue) IsValue

func (tv TypeAndValue) IsValue() bool

func (TypeAndValue) IsVoid

func (tv TypeAndValue) IsVoid() bool

IsVoid returns if the type is a void value.

type TypeName

type TypeName struct {
	// contains filtered or unexported fields
}

TypeName represents a type name object. An example would be struct file fd; fd in this case would be the type name.

func NewTypeName

func NewTypeName(pos scanner.Position, name string, typ Type) *TypeName

NewTypeName creates a type name object.

func (*TypeName) Name

func (obj *TypeName) Name() string

func (*TypeName) Parent

func (obj *TypeName) Parent() *Scope

func (*TypeName) Pos

func (obj *TypeName) Pos() scanner.Position

func (*TypeName) String

func (obj *TypeName) String() string

func (*TypeName) Type

func (obj *TypeName) Type() Type

type Var

type Var struct {
	// contains filtered or unexported fields
}

Var represents a variable.

func NewField

func NewField(pos scanner.Position, name string, typ Type) *Var

NewField creates a new field inside a record declaration object.

func NewVar

func NewVar(pos scanner.Position, storage Storage, name string, typ Type, val constant.Value) *Var

NewVar creates a new variable declaration.

func (*Var) Name

func (obj *Var) Name() string

func (*Var) Parent

func (obj *Var) Parent() *Scope

func (*Var) Pos

func (obj *Var) Pos() scanner.Position

func (*Var) Storage

func (obj *Var) Storage() Storage

func (*Var) String

func (obj *Var) String() string

func (*Var) Type

func (obj *Var) Type() Type

func (*Var) Value

func (obj *Var) Value() constant.Value

Jump to

Keyboard shortcuts

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