Commit 4720f49e authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: relocate a bunch of Type-related code

Some cleaned up documentation, but no code changes.

Change-Id: I145398bb6d118c626ab3873ef75dbb64ebc286e9
Reviewed-on: https://go-review.googlesource.com/20404
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent bf44c4c8
......@@ -1250,13 +1250,6 @@ func untype(ctype Ctype) *Type {
return nil
}
var (
idealint = typ(TIDEAL)
idealrune = typ(TIDEAL)
idealfloat = typ(TIDEAL)
idealcomplex = typ(TIDEAL)
)
var predecl []*Type // initialized lazily
func predeclared() []*Type {
......
......@@ -85,69 +85,6 @@ type Sym struct {
Fsym *Sym // funcsym
}
type Type struct {
Etype EType
Nointerface bool
Noalg bool
Chan uint8
Trecur uint8 // to detect loops
Printed bool
Embedded uint8 // TFIELD embedded type
Funarg bool // on TSTRUCT and TFIELD
Copyany bool
Local bool // created in this file
Deferwidth bool
Broke bool // broken type definition.
Isddd bool // TFIELD is ... argument
Align uint8
Haspointers uint8 // 0 unknown, 1 no, 2 yes
Nod *Node // canonical OTYPE node
Orig *Type // original type (type literal or predefined type)
Lineno int32
// TFUNC
Thistuple int
Outtuple int
Intuple int
Outnamed bool
Method *Type
Xmethod *Type
Sym *Sym
Vargen int32 // unique name for OTYPE/ONAME
Nname *Node
Argwid int64
// most nodes
Type *Type // actual type for TFIELD, element type for TARRAY, TCHAN, TMAP, TPTRxx
Width int64 // offset in TFIELD, width in all others
// TFIELD
Down *Type // next struct field, also key type in TMAP
Outer *Type // outer struct
Note *string // literal string annotation
// TARRAY
Bound int64 // negative is slice
// TMAP
Bucket *Type // internal type representing a hash bucket
Hmap *Type // internal type representing a Hmap (map header object)
Hiter *Type // internal type representing hash iterator state
Map *Type // link from the above 3 internal types back to the map type.
Maplineno int32 // first use of TFORW as map key
Embedlineno int32 // first use of TFORW as embedded type
// for TFORW, where to copy the eventual value to
Copyto []*Node
Lastfn *Node // for usefield
}
type Label struct {
Sym *Sym
Def *Node
......@@ -189,66 +126,6 @@ const (
var dclstack *Sym
type Iter struct {
Done int
Tfunc *Type
T *Type
}
type EType uint8
const (
Txxx = iota
TINT8
TUINT8
TINT16
TUINT16
TINT32
TUINT32
TINT64
TUINT64
TINT
TUINT
TUINTPTR
TCOMPLEX64
TCOMPLEX128
TFLOAT32
TFLOAT64
TBOOL
TPTR32
TPTR64
TFUNC
TARRAY
T_old_DARRAY // Doesn't seem to be used in existing code. Used now for Isddd export (see bexport.go). TODO(gri) rename.
TSTRUCT
TCHAN
TMAP
TINTER
TFORW
TFIELD
TANY
TSTRING
TUNSAFEPTR
// pseudo-types for literals
TIDEAL
TNIL
TBLANK
// pseudo-type for frame layout
TFUNCARGS
TCHANARGS
TINTERMETH
NTYPE
)
// Ctype describes the constant kind of an "ideal" (untyped) constant.
type Ctype int8
......@@ -437,18 +314,6 @@ var localimport string
var asmhdr string
var Types [NTYPE]*Type
var idealstring *Type
var idealbool *Type
var bytetype *Type
var runetype *Type
var errortype *Type
var Simtype [NTYPE]EType
var (
......
......@@ -2135,13 +2135,7 @@ func lexinit() {
s2.Def.Etype = EType(s.op)
}
// logically, the type of a string literal.
// types[TSTRING] is the named type string
// (the type of x in var x string or var x = "hello").
// this is the ideal form
// (the type of x in const x = "hello").
idealstring = typ(TSTRING)
idealbool = typ(TBOOL)
s := Pkglookup("true", builtinpkg)
......
......@@ -1462,128 +1462,6 @@ func badtype(op Op, tl *Type, tr *Type) {
Yyerror("illegal types for operand: %v%s", Oconv(op, 0), s)
}
// iterator to walk a structure declaration
func Structfirst(s *Iter, nn **Type) *Type {
var t *Type
n := *nn
if n == nil {
goto bad
}
switch n.Etype {
default:
goto bad
case TSTRUCT, TINTER, TFUNC:
break
}
t = n.Type
if t == nil {
return nil
}
if t.Etype != TFIELD {
Fatalf("structfirst: not field %v", t)
}
s.T = t
return t
bad:
Fatalf("structfirst: not struct %v", n)
return nil
}
func structnext(s *Iter) *Type {
n := s.T
t := n.Down
if t == nil {
return nil
}
if t.Etype != TFIELD {
Fatalf("structnext: not struct %v", n)
return nil
}
s.T = t
return t
}
// iterator to this and inargs in a function
func funcfirst(s *Iter, t *Type) *Type {
var fp *Type
if t == nil {
goto bad
}
if t.Etype != TFUNC {
goto bad
}
s.Tfunc = t
s.Done = 0
fp = Structfirst(s, getthis(t))
if fp == nil {
s.Done = 1
fp = Structfirst(s, getinarg(t))
}
return fp
bad:
Fatalf("funcfirst: not func %v", t)
return nil
}
func funcnext(s *Iter) *Type {
fp := structnext(s)
if fp == nil && s.Done == 0 {
s.Done = 1
fp = Structfirst(s, getinarg(s.Tfunc))
}
return fp
}
func getthis(t *Type) **Type {
if t.Etype != TFUNC {
Fatalf("getthis: not a func %v", t)
}
return &t.Type
}
func Getoutarg(t *Type) **Type {
if t.Etype != TFUNC {
Fatalf("getoutarg: not a func %v", t)
}
return &t.Type.Down
}
func getinarg(t *Type) **Type {
if t.Etype != TFUNC {
Fatalf("getinarg: not a func %v", t)
}
return &t.Type.Down.Down
}
func getthisx(t *Type) *Type {
return *getthis(t)
}
func getoutargx(t *Type) *Type {
return *Getoutarg(t)
}
func getinargx(t *Type) *Type {
return *getinarg(t)
}
// Brcom returns !(op).
// For example, Brcom(==) is !=.
func Brcom(op Op) Op {
......
......@@ -14,6 +14,286 @@ import (
"fmt"
)
// EType describes a kind of type.
type EType uint8
const (
Txxx = iota
TINT8
TUINT8
TINT16
TUINT16
TINT32
TUINT32
TINT64
TUINT64
TINT
TUINT
TUINTPTR
TCOMPLEX64
TCOMPLEX128
TFLOAT32
TFLOAT64
TBOOL
TPTR32
TPTR64
TFUNC
TARRAY
T_old_DARRAY // Doesn't seem to be used in existing code. Used now for Isddd export (see bexport.go). TODO(gri) rename.
TSTRUCT
TCHAN
TMAP
TINTER
TFORW
TFIELD
TANY
TSTRING
TUNSAFEPTR
// pseudo-types for literals
TIDEAL
TNIL
TBLANK
// pseudo-type for frame layout
TFUNCARGS
TCHANARGS
TINTERMETH
NTYPE
)
// Types stores pointers to predeclared named types.
//
// It also stores pointers to several special types:
// - Types[TANY] is the placeholder "any" type recognized by substArgTypes.
// - Types[TBLANK] represents the blank variable's type.
// - Types[TIDEAL] represents untyped numeric constants.
// - Types[TNIL] represents the predeclared "nil" value's type.
// - Types[TUNSAFEPTR] is package unsafe's Pointer type.
var Types [NTYPE]*Type
var (
// Predeclared alias types. Kept separate for better error messages.
bytetype *Type
runetype *Type
// Predeclared error interface type.
errortype *Type
// Types to represent untyped string and boolean constants.
idealstring *Type
idealbool *Type
// Types to represent untyped numeric constants.
// Note: Currently these are only used within the binary export
// data format. The rest of the compiler only uses Types[TIDEAL].
idealint = typ(TIDEAL)
idealrune = typ(TIDEAL)
idealfloat = typ(TIDEAL)
idealcomplex = typ(TIDEAL)
)
// A Type represents a Go type.
type Type struct {
Etype EType
Nointerface bool
Noalg bool
Chan uint8
Trecur uint8 // to detect loops
Printed bool
Embedded uint8 // TFIELD embedded type
Funarg bool // on TSTRUCT and TFIELD
Copyany bool
Local bool // created in this file
Deferwidth bool
Broke bool // broken type definition.
Isddd bool // TFIELD is ... argument
Align uint8
Haspointers uint8 // 0 unknown, 1 no, 2 yes
Nod *Node // canonical OTYPE node
Orig *Type // original type (type literal or predefined type)
Lineno int32
// TFUNC
Thistuple int
Outtuple int
Intuple int
Outnamed bool
Method *Type
Xmethod *Type
Sym *Sym
Vargen int32 // unique name for OTYPE/ONAME
Nname *Node
Argwid int64
// most nodes
Type *Type // actual type for TFIELD, element type for TARRAY, TCHAN, TMAP, TPTRxx
Width int64 // offset in TFIELD, width in all others
// TFIELD
Down *Type // next struct field, also key type in TMAP
Outer *Type // outer struct
Note *string // literal string annotation
// TARRAY
Bound int64 // negative is slice
// TMAP
Bucket *Type // internal type representing a hash bucket
Hmap *Type // internal type representing a Hmap (map header object)
Hiter *Type // internal type representing hash iterator state
Map *Type // link from the above 3 internal types back to the map type.
Maplineno int32 // first use of TFORW as map key
Embedlineno int32 // first use of TFORW as embedded type
// for TFORW, where to copy the eventual value to
Copyto []*Node
Lastfn *Node // for usefield
}
// Iter provides an abstraction for iterating across struct fields
// and function parameters.
type Iter struct {
Done int
Tfunc *Type
T *Type
}
// iterator to walk a structure declaration
func Structfirst(s *Iter, nn **Type) *Type {
var t *Type
n := *nn
if n == nil {
goto bad
}
switch n.Etype {
default:
goto bad
case TSTRUCT, TINTER, TFUNC:
break
}
t = n.Type
if t == nil {
return nil
}
if t.Etype != TFIELD {
Fatalf("structfirst: not field %v", t)
}
s.T = t
return t
bad:
Fatalf("structfirst: not struct %v", n)
return nil
}
func structnext(s *Iter) *Type {
n := s.T
t := n.Down
if t == nil {
return nil
}
if t.Etype != TFIELD {
Fatalf("structnext: not struct %v", n)
return nil
}
s.T = t
return t
}
// iterator to this and inargs in a function
func funcfirst(s *Iter, t *Type) *Type {
var fp *Type
if t == nil {
goto bad
}
if t.Etype != TFUNC {
goto bad
}
s.Tfunc = t
s.Done = 0
fp = Structfirst(s, getthis(t))
if fp == nil {
s.Done = 1
fp = Structfirst(s, getinarg(t))
}
return fp
bad:
Fatalf("funcfirst: not func %v", t)
return nil
}
func funcnext(s *Iter) *Type {
fp := structnext(s)
if fp == nil && s.Done == 0 {
s.Done = 1
fp = Structfirst(s, getinarg(s.Tfunc))
}
return fp
}
func getthis(t *Type) **Type {
if t.Etype != TFUNC {
Fatalf("getthis: not a func %v", t)
}
return &t.Type
}
func Getoutarg(t *Type) **Type {
if t.Etype != TFUNC {
Fatalf("getoutarg: not a func %v", t)
}
return &t.Type.Down
}
func getinarg(t *Type) **Type {
if t.Etype != TFUNC {
Fatalf("getinarg: not a func %v", t)
}
return &t.Type.Down.Down
}
func getthisx(t *Type) *Type {
return *getthis(t)
}
func getoutargx(t *Type) *Type {
return *Getoutarg(t)
}
func getinargx(t *Type) *Type {
return *getinarg(t)
}
func (t *Type) Size() int64 {
dowidth(t)
return t.Width
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment