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 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 { func (s *StdSizes) Sizeof(T Type) int64 {
switch t := T.Underlying().(type) { switch t := T.Underlying().(type) {
case *Basic: case *Basic:
if z := t.size; z > 0 { assert(isTyped(T))
return z 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 return s.WordSize * 2
} }
case *Array: case *Array:

View File

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

View File

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