From 0b4b877c725b0f72fb47ca587439bb31fcc86866 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 16 Jan 2014 14:47:26 -0800 Subject: [PATCH] go.tools/go/types: remove size from Basic More localized computation of sizes. R=adonovan CC=golang-codereviews https://golang.org/cl/53460043 --- go/types/sizes.go | 26 +++++++++++++++++--- go/types/types.go | 1 - go/types/universe.go | 56 ++++++++++++++++++++++---------------------- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/go/types/sizes.go b/go/types/sizes.go index 2535e4e9..fe24345d 100644 --- a/go/types/sizes.go +++ b/go/types/sizes.go @@ -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: diff --git a/go/types/types.go b/go/types/types.go index 5733f49f..d4bf89e5 100644 --- a/go/types/types.go +++ b/go/types/types.go @@ -83,7 +83,6 @@ const ( type Basic struct { kind BasicKind info BasicInfo - size int64 // use DefaultSizeof to get size name string } diff --git a/go/types/universe.go b/go/types/universe.go index 52f1678c..49a9c428 100644 --- a/go/types/universe.go +++ b/go/types/universe.go @@ -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() {