go.tools/go/types: remove size from Basic

More localized computation of sizes.

R=adonovan
CC=golang-codereviews
https://golang.org/cl/53460043
This commit is contained in:
Robert Griesemer 2014-01-16 14:47:26 -08:00
parent 9b91992c8c
commit 0b4b877c72
3 changed files with 51 additions and 32 deletions

View File

@ -82,13 +82,33 @@ func (s *StdSizes) Offsetsof(fields []*Var) []int64 {
return offsets
}
var basicSizes = [...]byte{
Bool: 1,
Int8: 1,
Int16: 2,
Int32: 4,
Int64: 8,
Uint8: 1,
Uint16: 2,
Uint32: 4,
Uint64: 8,
Float32: 4,
Float64: 8,
Complex64: 8,
Complex128: 16,
}
func (s *StdSizes) Sizeof(T Type) int64 {
switch t := T.Underlying().(type) {
case *Basic:
if z := t.size; z > 0 {
return z
assert(isTyped(T))
k := t.kind
if int(k) < len(basicSizes) {
if s := basicSizes[k]; s > 0 {
return int64(s)
}
}
if t.kind == String {
if k == String {
return s.WordSize * 2
}
case *Array:

View File

@ -83,7 +83,6 @@ const (
type Basic struct {
kind BasicKind
info BasicInfo
size int64 // use DefaultSizeof to get size
name string
}

View File

@ -22,39 +22,39 @@ var (
)
var Typ = [...]*Basic{
Invalid: {Invalid, 0, 0, "invalid type"},
Invalid: {Invalid, 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"},
Bool: {Bool, IsBoolean, "bool"},
Int: {Int, IsInteger, "int"},
Int8: {Int8, IsInteger, "int8"},
Int16: {Int16, IsInteger, "int16"},
Int32: {Int32, IsInteger, "int32"},
Int64: {Int64, IsInteger, "int64"},
Uint: {Uint, IsInteger | IsUnsigned, "uint"},
Uint8: {Uint8, IsInteger | IsUnsigned, "uint8"},
Uint16: {Uint16, IsInteger | IsUnsigned, "uint16"},
Uint32: {Uint32, IsInteger | IsUnsigned, "uint32"},
Uint64: {Uint64, IsInteger | IsUnsigned, "uint64"},
Uintptr: {Uintptr, IsInteger | IsUnsigned, "uintptr"},
Float32: {Float32, IsFloat, "float32"},
Float64: {Float64, IsFloat, "float64"},
Complex64: {Complex64, IsComplex, "complex64"},
Complex128: {Complex128, IsComplex, "complex128"},
String: {String, IsString, "string"},
UnsafePointer: {UnsafePointer, 0, "Pointer"},
UntypedBool: {UntypedBool, IsBoolean | IsUntyped, 0, "untyped bool"},
UntypedInt: {UntypedInt, IsInteger | IsUntyped, 0, "untyped int"},
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"},
UntypedBool: {UntypedBool, IsBoolean | IsUntyped, "untyped bool"},
UntypedInt: {UntypedInt, IsInteger | IsUntyped, "untyped int"},
UntypedRune: {UntypedRune, IsInteger | IsUntyped, "untyped rune"},
UntypedFloat: {UntypedFloat, IsFloat | IsUntyped, "untyped float"},
UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"},
UntypedString: {UntypedString, IsString | IsUntyped, "untyped string"},
UntypedNil: {UntypedNil, IsUntyped, "untyped nil"},
}
var aliases = [...]*Basic{
{Byte, IsInteger | IsUnsigned, 1, "byte"},
{Rune, IsInteger, 4, "rune"},
{Byte, IsInteger | IsUnsigned, "byte"},
{Rune, IsInteger, "rune"},
}
func defPredeclaredTypes() {