| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package types |
| 6 |
|
| 7 |
import ( |
| 8 |
"cmd/compile/internal/base" |
| 9 |
"cmd/internal/objabi" |
| 10 |
"cmd/internal/src" |
| 11 |
"fmt" |
| 12 |
"go/constant" |
| 13 |
"internal/buildcfg" |
| 14 |
"internal/types/errors" |
| 15 |
"sync" |
| 16 |
) |
| 17 |
|
| 18 |
// Object represents an ir.Node, but without needing to import cmd/compile/internal/ir, |
| 19 |
// which would cause an import cycle. The uses in other packages must type assert |
| 20 |
// values of type Object to ir.Node or a more specific type. |
| 21 |
type Object interface { |
| 22 |
Pos() src.XPos |
| 23 |
Sym() *Sym |
| 24 |
Type() *Type |
| 25 |
} |
| 26 |
|
| 27 |
//go:generate stringer -type Kind -trimprefix T type.go |
| 28 |
|
| 29 |
// Kind describes a kind of type. |
| 30 |
type Kind uint8 |
| 31 |
|
| 32 |
const ( |
| 33 |
Txxx Kind = iota |
| 34 |
|
| 35 |
TINT8 |
| 36 |
TUINT8 |
| 37 |
TINT16 |
| 38 |
TUINT16 |
| 39 |
TINT32 |
| 40 |
TUINT32 |
| 41 |
TINT64 |
| 42 |
TUINT64 |
| 43 |
TINT |
| 44 |
TUINT |
| 45 |
TUINTPTR |
| 46 |
|
| 47 |
TCOMPLEX64 |
| 48 |
TCOMPLEX128 |
| 49 |
|
| 50 |
TFLOAT32 |
| 51 |
TFLOAT64 |
| 52 |
|
| 53 |
TBOOL |
| 54 |
|
| 55 |
TPTR |
| 56 |
TFUNC |
| 57 |
TSLICE |
| 58 |
TARRAY |
| 59 |
TSTRUCT |
| 60 |
TCHAN |
| 61 |
TMAP |
| 62 |
TINTER |
| 63 |
TFORW |
| 64 |
TANY |
| 65 |
TSTRING |
| 66 |
TUNSAFEPTR |
| 67 |
|
| 68 |
// pseudo-types for literals |
| 69 |
TIDEAL // untyped numeric constants |
| 70 |
TNIL |
| 71 |
TBLANK |
| 72 |
|
| 73 |
// pseudo-types used temporarily only during frame layout (CalcSize()) |
| 74 |
TFUNCARGS |
| 75 |
TCHANARGS |
| 76 |
|
| 77 |
// SSA backend types |
| 78 |
TSSA // internal types used by SSA backend (flags, memory, etc.) |
| 79 |
TTUPLE // a pair of types, used by SSA backend |
| 80 |
TRESULTS // multiple types; the result of calling a function or method, with a memory at the end. |
| 81 |
|
| 82 |
NTYPE |
| 83 |
) |
| 84 |
|
| 85 |
// ChanDir is whether a channel can send, receive, or both. |
| 86 |
type ChanDir uint8 |
| 87 |
|
| 88 |
func (c ChanDir) CanRecv() bool { return c&Crecv != 0 } |
| 89 |
func (c ChanDir) CanSend() bool { return c&Csend != 0 } |
| 90 |
|
| 91 |
const ( |
| 92 |
// types of channel |
| 93 |
// must match ../../../../reflect/type.go:/ChanDir |
| 94 |
Crecv ChanDir = 1 << 0 |
| 95 |
Csend ChanDir = 1 << 1 |
| 96 |
Cboth ChanDir = Crecv | Csend |
| 97 |
) |
| 98 |
|
| 99 |
// Types stores pointers to predeclared named types. |
| 100 |
// |
| 101 |
// It also stores pointers to several special types: |
| 102 |
// - Types[TANY] is the placeholder "any" type recognized by SubstArgTypes. |
| 103 |
// - Types[TBLANK] represents the blank variable's type. |
| 104 |
// - Types[TINTER] is the canonical "interface{}" type. |
| 105 |
// - Types[TNIL] represents the predeclared "nil" value's type. |
| 106 |
// - Types[TUNSAFEPTR] is package unsafe's Pointer type. |
| 107 |
var Types [NTYPE]*Type |
| 108 |
|
| 109 |
var ( |
| 110 |
// Predeclared alias types. These are actually created as distinct |
| 111 |
// defined types for better error messages, but are then specially |
| 112 |
// treated as identical to their respective underlying types. |
| 113 |
AnyType *Type |
| 114 |
ByteType *Type |
| 115 |
RuneType *Type |
| 116 |
|
| 117 |
// Predeclared error interface type. |
| 118 |
ErrorType *Type |
| 119 |
// Predeclared comparable interface type. |
| 120 |
ComparableType *Type |
| 121 |
|
| 122 |
// Types to represent untyped string and boolean constants. |
| 123 |
UntypedString = newType(TSTRING) |
| 124 |
UntypedBool = newType(TBOOL) |
| 125 |
|
| 126 |
// Types to represent untyped numeric constants. |
| 127 |
UntypedInt = newType(TIDEAL) |
| 128 |
UntypedRune = newType(TIDEAL) |
| 129 |
UntypedFloat = newType(TIDEAL) |
| 130 |
UntypedComplex = newType(TIDEAL) |
| 131 |
) |
| 132 |
|
| 133 |
// UntypedTypes maps from a constant.Kind to its untyped Type |
| 134 |
// representation. |
| 135 |
var UntypedTypes = [...]*Type{ |
| 136 |
constant.Bool: UntypedBool, |
| 137 |
constant.String: UntypedString, |
| 138 |
constant.Int: UntypedInt, |
| 139 |
constant.Float: UntypedFloat, |
| 140 |
constant.Complex: UntypedComplex, |
| 141 |
} |
| 142 |
|
| 143 |
// DefaultKinds maps from a constant.Kind to its default Kind. |
| 144 |
var DefaultKinds = [...]Kind{ |
| 145 |
constant.Bool: TBOOL, |
| 146 |
constant.String: TSTRING, |
| 147 |
constant.Int: TINT, |
| 148 |
constant.Float: TFLOAT64, |
| 149 |
constant.Complex: TCOMPLEX128, |
| 150 |
} |
| 151 |
|
| 152 |
// A Type represents a Go type. |
| 153 |
// |
| 154 |
// There may be multiple unnamed types with identical structure. However, there must |
| 155 |
// be a unique Type object for each unique named (defined) type. After noding, a |
| 156 |
// package-level type can be looked up by building its unique symbol sym (sym = |
| 157 |
// package.Lookup(name)) and checking sym.Def. If sym.Def is non-nil, the type |
| 158 |
// already exists at package scope and is available at sym.Def.(*ir.Name).Type(). |
| 159 |
// Local types (which may have the same name as a package-level type) are |
| 160 |
// distinguished by their vargen, which is embedded in their symbol name. |
| 161 |
type Type struct { |
| 162 |
// extra contains extra etype-specific fields. |
| 163 |
// As an optimization, those etype-specific structs which contain exactly |
| 164 |
// one pointer-shaped field are stored as values rather than pointers when possible. |
| 165 |
// |
| 166 |
// TMAP: *Map |
| 167 |
// TFORW: *Forward |
| 168 |
// TFUNC: *Func |
| 169 |
// TSTRUCT: *Struct |
| 170 |
// TINTER: *Interface |
| 171 |
// TFUNCARGS: FuncArgs |
| 172 |
// TCHANARGS: ChanArgs |
| 173 |
// TCHAN: *Chan |
| 174 |
// TPTR: Ptr |
| 175 |
// TARRAY: *Array |
| 176 |
// TSLICE: Slice |
| 177 |
// TSSA: string |
| 178 |
extra interface{} |
| 179 |
|
| 180 |
// width is the width of this Type in bytes. |
| 181 |
width int64 // valid if Align > 0 |
| 182 |
|
| 183 |
// list of base methods (excluding embedding) |
| 184 |
methods fields |
| 185 |
// list of all methods (including embedding) |
| 186 |
allMethods fields |
| 187 |
|
| 188 |
// canonical OTYPE node for a named type (should be an ir.Name node with same sym) |
| 189 |
obj Object |
| 190 |
// the underlying type (type literal or predeclared type) for a defined type |
| 191 |
underlying *Type |
| 192 |
|
| 193 |
// Cache of composite types, with this type being the element type. |
| 194 |
cache struct { |
| 195 |
ptr *Type // *T, or nil |
| 196 |
slice *Type // []T, or nil |
| 197 |
} |
| 198 |
|
| 199 |
kind Kind // kind of type |
| 200 |
align uint8 // the required alignment of this type, in bytes (0 means Width and Align have not yet been computed) |
| 201 |
|
| 202 |
intRegs, floatRegs uint8 // registers needed for ABIInternal |
| 203 |
|
| 204 |
flags bitset8 |
| 205 |
alg AlgKind // valid if Align > 0 |
| 206 |
|
| 207 |
// size of prefix of object that contains all pointers. valid if Align > 0. |
| 208 |
// Note that for pointers, this is always PtrSize even if the element type |
| 209 |
// is NotInHeap. See size.go:PtrDataSize for details. |
| 210 |
ptrBytes int64 |
| 211 |
} |
| 212 |
|
| 213 |
// Registers returns the number of integer and floating-point |
| 214 |
// registers required to represent a parameter of this type under the |
| 215 |
// ABIInternal calling conventions. |
| 216 |
// |
| 217 |
// If t must be passed by memory, Registers returns (math.MaxUint8, |
| 218 |
// math.MaxUint8). |
| 219 |
func (t *Type) Registers() (uint8, uint8) { |
| 220 |
CalcSize(t) |
| 221 |
return t.intRegs, t.floatRegs |
| 222 |
} |
| 223 |
|
| 224 |
func (*Type) CanBeAnSSAAux() {} |
| 225 |
|
| 226 |
const ( |
| 227 |
typeNotInHeap = 1 << iota // type cannot be heap allocated |
| 228 |
typeNoalg // suppress hash and eq algorithm generation |
| 229 |
typeDeferwidth // width computation has been deferred and type is on deferredTypeStack |
| 230 |
typeRecur |
| 231 |
typeIsShape // represents a set of closely related types, for generics |
| 232 |
typeHasShape // there is a shape somewhere in the type |
| 233 |
// typeIsFullyInstantiated reports whether a type is fully instantiated generic type; i.e. |
| 234 |
// an instantiated generic type where all type arguments are non-generic or fully instantiated generic types. |
| 235 |
typeIsFullyInstantiated |
| 236 |
) |
| 237 |
|
| 238 |
func (t *Type) NotInHeap() bool { return t.flags&typeNotInHeap != 0 } |
| 239 |
func (t *Type) Noalg() bool { return t.flags&typeNoalg != 0 } |
| 240 |
func (t *Type) Deferwidth() bool { return t.flags&typeDeferwidth != 0 } |
| 241 |
func (t *Type) Recur() bool { return t.flags&typeRecur != 0 } |
| 242 |
func (t *Type) IsShape() bool { return t.flags&typeIsShape != 0 } |
| 243 |
func (t *Type) HasShape() bool { return t.flags&typeHasShape != 0 } |
| 244 |
func (t *Type) IsFullyInstantiated() bool { return t.flags&typeIsFullyInstantiated != 0 } |
| 245 |
|
| 246 |
func (t *Type) SetNotInHeap(b bool) { t.flags.set(typeNotInHeap, b) } |
| 247 |
func (t *Type) SetNoalg(b bool) { t.flags.set(typeNoalg, b) } |
| 248 |
func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) } |
| 249 |
func (t *Type) SetRecur(b bool) { t.flags.set(typeRecur, b) } |
| 250 |
func (t *Type) SetIsFullyInstantiated(b bool) { t.flags.set(typeIsFullyInstantiated, b) } |
| 251 |
|
| 252 |
// Should always do SetHasShape(true) when doing SetIsShape(true). |
| 253 |
func (t *Type) SetIsShape(b bool) { t.flags.set(typeIsShape, b) } |
| 254 |
func (t *Type) SetHasShape(b bool) { t.flags.set(typeHasShape, b) } |
| 255 |
|
| 256 |
// Kind returns the kind of type t. |
| 257 |
func (t *Type) Kind() Kind { return t.kind } |
| 258 |
|
| 259 |
// Sym returns the name of type t. |
| 260 |
func (t *Type) Sym() *Sym { |
| 261 |
if t.obj != nil { |
| 262 |
return t.obj.Sym() |
| 263 |
} |
| 264 |
return nil |
| 265 |
} |
| 266 |
|
| 267 |
// Underlying returns the underlying type of type t. |
| 268 |
func (t *Type) Underlying() *Type { return t.underlying } |
| 269 |
|
| 270 |
// Pos returns a position associated with t, if any. |
| 271 |
// This should only be used for diagnostics. |
| 272 |
func (t *Type) Pos() src.XPos { |
| 273 |
if t.obj != nil { |
| 274 |
return t.obj.Pos() |
| 275 |
} |
| 276 |
return src.NoXPos |
| 277 |
} |
| 278 |
|
| 279 |
// Map contains Type fields specific to maps. |
| 280 |
type Map struct { |
| 281 |
Key *Type // Key type |
| 282 |
Elem *Type // Val (elem) type |
| 283 |
|
| 284 |
// Note: It would be cleaner to completely split Map into OldMap and |
| 285 |
// SwissMap, but 99% of the types map code doesn't care about the |
| 286 |
// implementation at all, so it is tons of churn to split the type. |
| 287 |
// Only code that looks at the bucket field can care about the |
| 288 |
// implementation. |
| 289 |
|
| 290 |
// GOEXPERIMENT=noswissmap fields |
| 291 |
OldBucket *Type // internal struct type representing a hash bucket |
| 292 |
|
| 293 |
// GOEXPERIMENT=swissmap fields |
| 294 |
SwissGroup *Type // internal struct type representing a slot group |
| 295 |
} |
| 296 |
|
| 297 |
// MapType returns t's extra map-specific fields. |
| 298 |
func (t *Type) MapType() *Map { |
| 299 |
t.wantEtype(TMAP) |
| 300 |
return t.extra.(*Map) |
| 301 |
} |
| 302 |
|
| 303 |
// Forward contains Type fields specific to forward types. |
| 304 |
type Forward struct { |
| 305 |
Copyto []*Type // where to copy the eventual value to |
| 306 |
Embedlineno src.XPos // first use of this type as an embedded type |
| 307 |
} |
| 308 |
|
| 309 |
// forwardType returns t's extra forward-type-specific fields. |
| 310 |
func (t *Type) forwardType() *Forward { |
| 311 |
t.wantEtype(TFORW) |
| 312 |
return t.extra.(*Forward) |
| 313 |
} |
| 314 |
|
| 315 |
// Func contains Type fields specific to func types. |
| 316 |
type Func struct { |
| 317 |
allParams []*Field // slice of all parameters, in receiver/params/results order |
| 318 |
|
| 319 |
startParams int // index of the start of the (regular) parameters section |
| 320 |
startResults int // index of the start of the results section |
| 321 |
|
| 322 |
resultsTuple *Type // struct-like type representing multi-value results |
| 323 |
|
| 324 |
// Argwid is the total width of the function receiver, params, and results. |
| 325 |
// It gets calculated via a temporary TFUNCARGS type. |
| 326 |
// Note that TFUNC's Width is Widthptr. |
| 327 |
Argwid int64 |
| 328 |
} |
| 329 |
|
| 330 |
func (ft *Func) recvs() []*Field { return ft.allParams[:ft.startParams] } |
| 331 |
func (ft *Func) params() []*Field { return ft.allParams[ft.startParams:ft.startResults] } |
| 332 |
func (ft *Func) results() []*Field { return ft.allParams[ft.startResults:] } |
| 333 |
func (ft *Func) recvParams() []*Field { return ft.allParams[:ft.startResults] } |
| 334 |
func (ft *Func) paramsResults() []*Field { return ft.allParams[ft.startParams:] } |
| 335 |
|
| 336 |
// funcType returns t's extra func-specific fields. |
| 337 |
func (t *Type) funcType() *Func { |
| 338 |
t.wantEtype(TFUNC) |
| 339 |
return t.extra.(*Func) |
| 340 |
} |
| 341 |
|
| 342 |
// StructType contains Type fields specific to struct types. |
| 343 |
type Struct struct { |
| 344 |
fields fields |
| 345 |
|
| 346 |
// Maps have three associated internal structs (see struct MapType). |
| 347 |
// Map links such structs back to their map type. |
| 348 |
Map *Type |
| 349 |
|
| 350 |
ParamTuple bool // whether this struct is actually a tuple of signature parameters |
| 351 |
} |
| 352 |
|
| 353 |
// StructType returns t's extra struct-specific fields. |
| 354 |
func (t *Type) StructType() *Struct { |
| 355 |
t.wantEtype(TSTRUCT) |
| 356 |
return t.extra.(*Struct) |
| 357 |
} |
| 358 |
|
| 359 |
// Interface contains Type fields specific to interface types. |
| 360 |
type Interface struct { |
| 361 |
} |
| 362 |
|
| 363 |
// Ptr contains Type fields specific to pointer types. |
| 364 |
type Ptr struct { |
| 365 |
Elem *Type // element type |
| 366 |
} |
| 367 |
|
| 368 |
// ChanArgs contains Type fields specific to TCHANARGS types. |
| 369 |
type ChanArgs struct { |
| 370 |
T *Type // reference to a chan type whose elements need a width check |
| 371 |
} |
| 372 |
|
| 373 |
// FuncArgs contains Type fields specific to TFUNCARGS types. |
| 374 |
type FuncArgs struct { |
| 375 |
T *Type // reference to a func type whose elements need a width check |
| 376 |
} |
| 377 |
|
| 378 |
// Chan contains Type fields specific to channel types. |
| 379 |
type Chan struct { |
| 380 |
Elem *Type // element type |
| 381 |
Dir ChanDir // channel direction |
| 382 |
} |
| 383 |
|
| 384 |
// chanType returns t's extra channel-specific fields. |
| 385 |
func (t *Type) chanType() *Chan { |
| 386 |
t.wantEtype(TCHAN) |
| 387 |
return t.extra.(*Chan) |
| 388 |
} |
| 389 |
|
| 390 |
type Tuple struct { |
| 391 |
first *Type |
| 392 |
second *Type |
| 393 |
// Any tuple with a memory type must put that memory type second. |
| 394 |
} |
| 395 |
|
| 396 |
// Results are the output from calls that will be late-expanded. |
| 397 |
type Results struct { |
| 398 |
Types []*Type // Last element is memory output from call. |
| 399 |
} |
| 400 |
|
| 401 |
// Array contains Type fields specific to array types. |
| 402 |
type Array struct { |
| 403 |
Elem *Type // element type |
| 404 |
Bound int64 // number of elements; <0 if unknown yet |
| 405 |
} |
| 406 |
|
| 407 |
// Slice contains Type fields specific to slice types. |
| 408 |
type Slice struct { |
| 409 |
Elem *Type // element type |
| 410 |
} |
| 411 |
|
| 412 |
// A Field is a (Sym, Type) pairing along with some other information, and, |
| 413 |
// depending on the context, is used to represent: |
| 414 |
// - a field in a struct |
| 415 |
// - a method in an interface or associated with a named type |
| 416 |
// - a function parameter |
| 417 |
type Field struct { |
| 418 |
flags bitset8 |
| 419 |
|
| 420 |
Embedded uint8 // embedded field |
| 421 |
|
| 422 |
Pos src.XPos |
| 423 |
|
| 424 |
// Name of field/method/parameter. Can be nil for interface fields embedded |
| 425 |
// in interfaces and unnamed parameters. |
| 426 |
Sym *Sym |
| 427 |
Type *Type // field type |
| 428 |
Note string // literal string annotation |
| 429 |
|
| 430 |
// For fields that represent function parameters, Nname points to the |
| 431 |
// associated ONAME Node. For fields that represent methods, Nname points to |
| 432 |
// the function name node. |
| 433 |
Nname Object |
| 434 |
|
| 435 |
// Offset in bytes of this field or method within its enclosing struct |
| 436 |
// or interface Type. For parameters, this is BADWIDTH. |
| 437 |
Offset int64 |
| 438 |
} |
| 439 |
|
| 440 |
const ( |
| 441 |
fieldIsDDD = 1 << iota // field is ... argument |
| 442 |
fieldNointerface |
| 443 |
) |
| 444 |
|
| 445 |
func (f *Field) IsDDD() bool { return f.flags&fieldIsDDD != 0 } |
| 446 |
func (f *Field) Nointerface() bool { return f.flags&fieldNointerface != 0 } |
| 447 |
|
| 448 |
func (f *Field) SetIsDDD(b bool) { f.flags.set(fieldIsDDD, b) } |
| 449 |
func (f *Field) SetNointerface(b bool) { f.flags.set(fieldNointerface, b) } |
| 450 |
|
| 451 |
// End returns the offset of the first byte immediately after this field. |
| 452 |
func (f *Field) End() int64 { |
| 453 |
return f.Offset + f.Type.width |
| 454 |
} |
| 455 |
|
| 456 |
// IsMethod reports whether f represents a method rather than a struct field. |
| 457 |
func (f *Field) IsMethod() bool { |
| 458 |
return f.Type.kind == TFUNC && f.Type.Recv() != nil |
| 459 |
} |
| 460 |
|
| 461 |
// CompareFields compares two Field values by name. |
| 462 |
func CompareFields(a, b *Field) int { |
| 463 |
return CompareSyms(a.Sym, b.Sym) |
| 464 |
} |
| 465 |
|
| 466 |
// fields is a pointer to a slice of *Field. |
| 467 |
// This saves space in Types that do not have fields or methods |
| 468 |
// compared to a simple slice of *Field. |
| 469 |
type fields struct { |
| 470 |
s *[]*Field |
| 471 |
} |
| 472 |
|
| 473 |
// Slice returns the entries in f as a slice. |
| 474 |
// Changes to the slice entries will be reflected in f. |
| 475 |
func (f *fields) Slice() []*Field { |
| 476 |
if f.s == nil { |
| 477 |
return nil |
| 478 |
} |
| 479 |
return *f.s |
| 480 |
} |
| 481 |
|
| 482 |
// Set sets f to a slice. |
| 483 |
// This takes ownership of the slice. |
| 484 |
func (f *fields) Set(s []*Field) { |
| 485 |
if len(s) == 0 { |
| 486 |
f.s = nil |
| 487 |
} else { |
| 488 |
// Copy s and take address of t rather than s to avoid |
| 489 |
// allocation in the case where len(s) == 0. |
| 490 |
t := s |
| 491 |
f.s = &t |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
// newType returns a new Type of the specified kind. |
| 496 |
func newType(et Kind) *Type { |
| 497 |
t := &Type{ |
| 498 |
kind: et, |
| 499 |
width: BADWIDTH, |
| 500 |
} |
| 501 |
t.underlying = t |
| 502 |
// TODO(josharian): lazily initialize some of these? |
| 503 |
switch t.kind { |
| 504 |
case TMAP: |
| 505 |
t.extra = new(Map) |
| 506 |
case TFORW: |
| 507 |
t.extra = new(Forward) |
| 508 |
case TFUNC: |
| 509 |
t.extra = new(Func) |
| 510 |
case TSTRUCT: |
| 511 |
t.extra = new(Struct) |
| 512 |
case TINTER: |
| 513 |
t.extra = new(Interface) |
| 514 |
case TPTR: |
| 515 |
t.extra = Ptr{} |
| 516 |
case TCHANARGS: |
| 517 |
t.extra = ChanArgs{} |
| 518 |
case TFUNCARGS: |
| 519 |
t.extra = FuncArgs{} |
| 520 |
case TCHAN: |
| 521 |
t.extra = new(Chan) |
| 522 |
case TTUPLE: |
| 523 |
t.extra = new(Tuple) |
| 524 |
case TRESULTS: |
| 525 |
t.extra = new(Results) |
| 526 |
} |
| 527 |
return t |
| 528 |
} |
| 529 |
|
| 530 |
// NewArray returns a new fixed-length array Type. |
| 531 |
func NewArray(elem *Type, bound int64) *Type { |
| 532 |
if bound < 0 { |
| 533 |
base.Fatalf("NewArray: invalid bound %v", bound) |
| 534 |
} |
| 535 |
t := newType(TARRAY) |
| 536 |
t.extra = &Array{Elem: elem, Bound: bound} |
| 537 |
if elem.HasShape() { |
| 538 |
t.SetHasShape(true) |
| 539 |
} |
| 540 |
if elem.NotInHeap() { |
| 541 |
t.SetNotInHeap(true) |
| 542 |
} |
| 543 |
return t |
| 544 |
} |
| 545 |
|
| 546 |
// NewSlice returns the slice Type with element type elem. |
| 547 |
func NewSlice(elem *Type) *Type { |
| 548 |
if t := elem.cache.slice; t != nil { |
| 549 |
if t.Elem() != elem { |
| 550 |
base.Fatalf("elem mismatch") |
| 551 |
} |
| 552 |
if elem.HasShape() != t.HasShape() { |
| 553 |
base.Fatalf("Incorrect HasShape flag for cached slice type") |
| 554 |
} |
| 555 |
return t |
| 556 |
} |
| 557 |
|
| 558 |
t := newType(TSLICE) |
| 559 |
t.extra = Slice{Elem: elem} |
| 560 |
elem.cache.slice = t |
| 561 |
if elem.HasShape() { |
| 562 |
t.SetHasShape(true) |
| 563 |
} |
| 564 |
return t |
| 565 |
} |
| 566 |
|
| 567 |
// NewChan returns a new chan Type with direction dir. |
| 568 |
func NewChan(elem *Type, dir ChanDir) *Type { |
| 569 |
t := newType(TCHAN) |
| 570 |
ct := t.chanType() |
| 571 |
ct.Elem = elem |
| 572 |
ct.Dir = dir |
| 573 |
if elem.HasShape() { |
| 574 |
t.SetHasShape(true) |
| 575 |
} |
| 576 |
return t |
| 577 |
} |
| 578 |
|
| 579 |
func NewTuple(t1, t2 *Type) *Type { |
| 580 |
t := newType(TTUPLE) |
| 581 |
t.extra.(*Tuple).first = t1 |
| 582 |
t.extra.(*Tuple).second = t2 |
| 583 |
if t1.HasShape() || t2.HasShape() { |
| 584 |
t.SetHasShape(true) |
| 585 |
} |
| 586 |
return t |
| 587 |
} |
| 588 |
|
| 589 |
func newResults(types []*Type) *Type { |
| 590 |
t := newType(TRESULTS) |
| 591 |
t.extra.(*Results).Types = types |
| 592 |
return t |
| 593 |
} |
| 594 |
|
| 595 |
func NewResults(types []*Type) *Type { |
| 596 |
if len(types) == 1 && types[0] == TypeMem { |
| 597 |
return TypeResultMem |
| 598 |
} |
| 599 |
return newResults(types) |
| 600 |
} |
| 601 |
|
| 602 |
func newSSA(name string) *Type { |
| 603 |
t := newType(TSSA) |
| 604 |
t.extra = name |
| 605 |
return t |
| 606 |
} |
| 607 |
|
| 608 |
// NewMap returns a new map Type with key type k and element (aka value) type v. |
| 609 |
func NewMap(k, v *Type) *Type { |
| 610 |
t := newType(TMAP) |
| 611 |
mt := t.MapType() |
| 612 |
mt.Key = k |
| 613 |
mt.Elem = v |
| 614 |
if k.HasShape() || v.HasShape() { |
| 615 |
t.SetHasShape(true) |
| 616 |
} |
| 617 |
return t |
| 618 |
} |
| 619 |
|
| 620 |
// NewPtrCacheEnabled controls whether *T Types are cached in T. |
| 621 |
// Caching is disabled just before starting the backend. |
| 622 |
// This allows the backend to run concurrently. |
| 623 |
var NewPtrCacheEnabled = true |
| 624 |
|
| 625 |
// NewPtr returns the pointer type pointing to t. |
| 626 |
func NewPtr(elem *Type) *Type { |
| 627 |
if elem == nil { |
| 628 |
base.Fatalf("NewPtr: pointer to elem Type is nil") |
| 629 |
} |
| 630 |
|
| 631 |
if t := elem.cache.ptr; t != nil { |
| 632 |
if t.Elem() != elem { |
| 633 |
base.Fatalf("NewPtr: elem mismatch") |
| 634 |
} |
| 635 |
if elem.HasShape() != t.HasShape() { |
| 636 |
base.Fatalf("Incorrect HasShape flag for cached pointer type") |
| 637 |
} |
| 638 |
return t |
| 639 |
} |
| 640 |
|
| 641 |
t := newType(TPTR) |
| 642 |
t.extra = Ptr{Elem: elem} |
| 643 |
t.width = int64(PtrSize) |
| 644 |
t.align = uint8(PtrSize) |
| 645 |
t.intRegs = 1 |
| 646 |
if NewPtrCacheEnabled { |
| 647 |
elem.cache.ptr = t |
| 648 |
} |
| 649 |
if elem.HasShape() { |
| 650 |
t.SetHasShape(true) |
| 651 |
} |
| 652 |
t.alg = AMEM |
| 653 |
if elem.Noalg() { |
| 654 |
t.SetNoalg(true) |
| 655 |
t.alg = ANOALG |
| 656 |
} |
| 657 |
// Note: we can't check elem.NotInHeap here because it might |
| 658 |
// not be set yet. See size.go:PtrDataSize. |
| 659 |
t.ptrBytes = int64(PtrSize) |
| 660 |
return t |
| 661 |
} |
| 662 |
|
| 663 |
// NewChanArgs returns a new TCHANARGS type for channel type c. |
| 664 |
func NewChanArgs(c *Type) *Type { |
| 665 |
t := newType(TCHANARGS) |
| 666 |
t.extra = ChanArgs{T: c} |
| 667 |
return t |
| 668 |
} |
| 669 |
|
| 670 |
// NewFuncArgs returns a new TFUNCARGS type for func type f. |
| 671 |
func NewFuncArgs(f *Type) *Type { |
| 672 |
t := newType(TFUNCARGS) |
| 673 |
t.extra = FuncArgs{T: f} |
| 674 |
return t |
| 675 |
} |
| 676 |
|
| 677 |
func NewField(pos src.XPos, sym *Sym, typ *Type) *Field { |
| 678 |
f := &Field{ |
| 679 |
Pos: pos, |
| 680 |
Sym: sym, |
| 681 |
Type: typ, |
| 682 |
Offset: BADWIDTH, |
| 683 |
} |
| 684 |
if typ == nil { |
| 685 |
base.Fatalf("typ is nil") |
| 686 |
} |
| 687 |
return f |
| 688 |
} |
| 689 |
|
| 690 |
// SubstAny walks t, replacing instances of "any" with successive |
| 691 |
// elements removed from types. It returns the substituted type. |
| 692 |
func SubstAny(t *Type, types *[]*Type) *Type { |
| 693 |
if t == nil { |
| 694 |
return nil |
| 695 |
} |
| 696 |
|
| 697 |
switch t.kind { |
| 698 |
default: |
| 699 |
// Leave the type unchanged. |
| 700 |
|
| 701 |
case TANY: |
| 702 |
if len(*types) == 0 { |
| 703 |
base.Fatalf("SubstArgTypes: not enough argument types") |
| 704 |
} |
| 705 |
t = (*types)[0] |
| 706 |
*types = (*types)[1:] |
| 707 |
|
| 708 |
case TPTR: |
| 709 |
elem := SubstAny(t.Elem(), types) |
| 710 |
if elem != t.Elem() { |
| 711 |
t = t.copy() |
| 712 |
t.extra = Ptr{Elem: elem} |
| 713 |
} |
| 714 |
|
| 715 |
case TARRAY: |
| 716 |
elem := SubstAny(t.Elem(), types) |
| 717 |
if elem != t.Elem() { |
| 718 |
t = t.copy() |
| 719 |
t.extra.(*Array).Elem = elem |
| 720 |
} |
| 721 |
|
| 722 |
case TSLICE: |
| 723 |
elem := SubstAny(t.Elem(), types) |
| 724 |
if elem != t.Elem() { |
| 725 |
t = t.copy() |
| 726 |
t.extra = Slice{Elem: elem} |
| 727 |
} |
| 728 |
|
| 729 |
case TCHAN: |
| 730 |
elem := SubstAny(t.Elem(), types) |
| 731 |
if elem != t.Elem() { |
| 732 |
t = t.copy() |
| 733 |
t.extra.(*Chan).Elem = elem |
| 734 |
} |
| 735 |
|
| 736 |
case TMAP: |
| 737 |
key := SubstAny(t.Key(), types) |
| 738 |
elem := SubstAny(t.Elem(), types) |
| 739 |
if key != t.Key() || elem != t.Elem() { |
| 740 |
t = t.copy() |
| 741 |
t.extra.(*Map).Key = key |
| 742 |
t.extra.(*Map).Elem = elem |
| 743 |
} |
| 744 |
|
| 745 |
case TFUNC: |
| 746 |
ft := t.funcType() |
| 747 |
allParams := substFields(ft.allParams, types) |
| 748 |
|
| 749 |
t = t.copy() |
| 750 |
ft = t.funcType() |
| 751 |
ft.allParams = allParams |
| 752 |
|
| 753 |
rt := ft.resultsTuple |
| 754 |
rt = rt.copy() |
| 755 |
ft.resultsTuple = rt |
| 756 |
rt.setFields(t.Results()) |
| 757 |
|
| 758 |
case TSTRUCT: |
| 759 |
// Make a copy of all fields, including ones whose type does not change. |
| 760 |
// This prevents aliasing across functions, which can lead to later |
| 761 |
// fields getting their Offset incorrectly overwritten. |
| 762 |
nfs := substFields(t.Fields(), types) |
| 763 |
t = t.copy() |
| 764 |
t.setFields(nfs) |
| 765 |
} |
| 766 |
|
| 767 |
return t |
| 768 |
} |
| 769 |
|
| 770 |
func substFields(fields []*Field, types *[]*Type) []*Field { |
| 771 |
nfs := make([]*Field, len(fields)) |
| 772 |
for i, f := range fields { |
| 773 |
nft := SubstAny(f.Type, types) |
| 774 |
nfs[i] = f.Copy() |
| 775 |
nfs[i].Type = nft |
| 776 |
} |
| 777 |
return nfs |
| 778 |
} |
| 779 |
|
| 780 |
// copy returns a shallow copy of the Type. |
| 781 |
func (t *Type) copy() *Type { |
| 782 |
if t == nil { |
| 783 |
return nil |
| 784 |
} |
| 785 |
nt := *t |
| 786 |
// copy any *T Extra fields, to avoid aliasing |
| 787 |
switch t.kind { |
| 788 |
case TMAP: |
| 789 |
x := *t.extra.(*Map) |
| 790 |
nt.extra = &x |
| 791 |
case TFORW: |
| 792 |
x := *t.extra.(*Forward) |
| 793 |
nt.extra = &x |
| 794 |
case TFUNC: |
| 795 |
x := *t.extra.(*Func) |
| 796 |
nt.extra = &x |
| 797 |
case TSTRUCT: |
| 798 |
x := *t.extra.(*Struct) |
| 799 |
nt.extra = &x |
| 800 |
case TINTER: |
| 801 |
x := *t.extra.(*Interface) |
| 802 |
nt.extra = &x |
| 803 |
case TCHAN: |
| 804 |
x := *t.extra.(*Chan) |
| 805 |
nt.extra = &x |
| 806 |
case TARRAY: |
| 807 |
x := *t.extra.(*Array) |
| 808 |
nt.extra = &x |
| 809 |
case TTUPLE, TSSA, TRESULTS: |
| 810 |
base.Fatalf("ssa types cannot be copied") |
| 811 |
} |
| 812 |
// TODO(mdempsky): Find out why this is necessary and explain. |
| 813 |
if t.underlying == t { |
| 814 |
nt.underlying = &nt |
| 815 |
} |
| 816 |
return &nt |
| 817 |
} |
| 818 |
|
| 819 |
func (f *Field) Copy() *Field { |
| 820 |
nf := *f |
| 821 |
return &nf |
| 822 |
} |
| 823 |
|
| 824 |
func (t *Type) wantEtype(et Kind) { |
| 825 |
if t.kind != et { |
| 826 |
base.Fatalf("want %v, but have %v", et, t) |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
// ResultsTuple returns the result type of signature type t as a tuple. |
| 831 |
// This can be used as the type of multi-valued call expressions. |
| 832 |
func (t *Type) ResultsTuple() *Type { return t.funcType().resultsTuple } |
| 833 |
|
| 834 |
// Recvs returns a slice of receiver parameters of signature type t. |
| 835 |
// The returned slice always has length 0 or 1. |
| 836 |
func (t *Type) Recvs() []*Field { return t.funcType().recvs() } |
| 837 |
|
| 838 |
// Params returns a slice of regular parameters of signature type t. |
| 839 |
func (t *Type) Params() []*Field { return t.funcType().params() } |
| 840 |
|
| 841 |
// Results returns a slice of result parameters of signature type t. |
| 842 |
func (t *Type) Results() []*Field { return t.funcType().results() } |
| 843 |
|
| 844 |
// RecvParamsResults returns a slice containing all of the |
| 845 |
// signature's parameters in receiver (if any), (normal) parameters, |
| 846 |
// and then results. |
| 847 |
func (t *Type) RecvParamsResults() []*Field { return t.funcType().allParams } |
| 848 |
|
| 849 |
// RecvParams returns a slice containing the signature's receiver (if |
| 850 |
// any) followed by its (normal) parameters. |
| 851 |
func (t *Type) RecvParams() []*Field { return t.funcType().recvParams() } |
| 852 |
|
| 853 |
// ParamsResults returns a slice containing the signature's (normal) |
| 854 |
// parameters followed by its results. |
| 855 |
func (t *Type) ParamsResults() []*Field { return t.funcType().paramsResults() } |
| 856 |
|
| 857 |
func (t *Type) NumRecvs() int { return len(t.Recvs()) } |
| 858 |
func (t *Type) NumParams() int { return len(t.Params()) } |
| 859 |
func (t *Type) NumResults() int { return len(t.Results()) } |
| 860 |
|
| 861 |
// IsVariadic reports whether function type t is variadic. |
| 862 |
func (t *Type) IsVariadic() bool { |
| 863 |
n := t.NumParams() |
| 864 |
return n > 0 && t.Param(n-1).IsDDD() |
| 865 |
} |
| 866 |
|
| 867 |
// Recv returns the receiver of function type t, if any. |
| 868 |
func (t *Type) Recv() *Field { |
| 869 |
if s := t.Recvs(); len(s) == 1 { |
| 870 |
return s[0] |
| 871 |
} |
| 872 |
return nil |
| 873 |
} |
| 874 |
|
| 875 |
// Param returns the i'th parameter of signature type t. |
| 876 |
func (t *Type) Param(i int) *Field { return t.Params()[i] } |
| 877 |
|
| 878 |
// Result returns the i'th result of signature type t. |
| 879 |
func (t *Type) Result(i int) *Field { return t.Results()[i] } |
| 880 |
|
| 881 |
// Key returns the key type of map type t. |
| 882 |
func (t *Type) Key() *Type { |
| 883 |
t.wantEtype(TMAP) |
| 884 |
return t.extra.(*Map).Key |
| 885 |
} |
| 886 |
|
| 887 |
// Elem returns the type of elements of t. |
| 888 |
// Usable with pointers, channels, arrays, slices, and maps. |
| 889 |
func (t *Type) Elem() *Type { |
| 890 |
switch t.kind { |
| 891 |
case TPTR: |
| 892 |
return t.extra.(Ptr).Elem |
| 893 |
case TARRAY: |
| 894 |
return t.extra.(*Array).Elem |
| 895 |
case TSLICE: |
| 896 |
return t.extra.(Slice).Elem |
| 897 |
case TCHAN: |
| 898 |
return t.extra.(*Chan).Elem |
| 899 |
case TMAP: |
| 900 |
return t.extra.(*Map).Elem |
| 901 |
} |
| 902 |
base.Fatalf("Type.Elem %s", t.kind) |
| 903 |
return nil |
| 904 |
} |
| 905 |
|
| 906 |
// ChanArgs returns the channel type for TCHANARGS type t. |
| 907 |
func (t *Type) ChanArgs() *Type { |
| 908 |
t.wantEtype(TCHANARGS) |
| 909 |
return t.extra.(ChanArgs).T |
| 910 |
} |
| 911 |
|
| 912 |
// FuncArgs returns the func type for TFUNCARGS type t. |
| 913 |
func (t *Type) FuncArgs() *Type { |
| 914 |
t.wantEtype(TFUNCARGS) |
| 915 |
return t.extra.(FuncArgs).T |
| 916 |
} |
| 917 |
|
| 918 |
// IsFuncArgStruct reports whether t is a struct representing function parameters or results. |
| 919 |
func (t *Type) IsFuncArgStruct() bool { |
| 920 |
return t.kind == TSTRUCT && t.extra.(*Struct).ParamTuple |
| 921 |
} |
| 922 |
|
| 923 |
// Methods returns a pointer to the base methods (excluding embedding) for type t. |
| 924 |
// These can either be concrete methods (for non-interface types) or interface |
| 925 |
// methods (for interface types). |
| 926 |
func (t *Type) Methods() []*Field { |
| 927 |
return t.methods.Slice() |
| 928 |
} |
| 929 |
|
| 930 |
// AllMethods returns a pointer to all the methods (including embedding) for type t. |
| 931 |
// For an interface type, this is the set of methods that are typically iterated |
| 932 |
// over. For non-interface types, AllMethods() only returns a valid result after |
| 933 |
// CalcMethods() has been called at least once. |
| 934 |
func (t *Type) AllMethods() []*Field { |
| 935 |
if t.kind == TINTER { |
| 936 |
// Calculate the full method set of an interface type on the fly |
| 937 |
// now, if not done yet. |
| 938 |
CalcSize(t) |
| 939 |
} |
| 940 |
return t.allMethods.Slice() |
| 941 |
} |
| 942 |
|
| 943 |
// SetMethods sets the direct method set for type t (i.e., *not* |
| 944 |
// including promoted methods from embedded types). |
| 945 |
func (t *Type) SetMethods(fs []*Field) { |
| 946 |
t.methods.Set(fs) |
| 947 |
} |
| 948 |
|
| 949 |
// SetAllMethods sets the set of all methods for type t (i.e., |
| 950 |
// including promoted methods from embedded types). |
| 951 |
func (t *Type) SetAllMethods(fs []*Field) { |
| 952 |
t.allMethods.Set(fs) |
| 953 |
} |
| 954 |
|
| 955 |
// fields returns the fields of struct type t. |
| 956 |
func (t *Type) fields() *fields { |
| 957 |
t.wantEtype(TSTRUCT) |
| 958 |
return &t.extra.(*Struct).fields |
| 959 |
} |
| 960 |
|
| 961 |
// Field returns the i'th field of struct type t. |
| 962 |
func (t *Type) Field(i int) *Field { return t.Fields()[i] } |
| 963 |
|
| 964 |
// Fields returns a slice of containing all fields of |
| 965 |
// a struct type t. |
| 966 |
func (t *Type) Fields() []*Field { return t.fields().Slice() } |
| 967 |
|
| 968 |
// setFields sets struct type t's fields to fields. |
| 969 |
func (t *Type) setFields(fields []*Field) { |
| 970 |
// If we've calculated the width of t before, |
| 971 |
// then some other type such as a function signature |
| 972 |
// might now have the wrong type. |
| 973 |
// Rather than try to track and invalidate those, |
| 974 |
// enforce that SetFields cannot be called once |
| 975 |
// t's width has been calculated. |
| 976 |
if t.widthCalculated() { |
| 977 |
base.Fatalf("SetFields of %v: width previously calculated", t) |
| 978 |
} |
| 979 |
t.wantEtype(TSTRUCT) |
| 980 |
t.fields().Set(fields) |
| 981 |
} |
| 982 |
|
| 983 |
// SetInterface sets the base methods of an interface type t. |
| 984 |
func (t *Type) SetInterface(methods []*Field) { |
| 985 |
t.wantEtype(TINTER) |
| 986 |
t.methods.Set(methods) |
| 987 |
} |
| 988 |
|
| 989 |
// ArgWidth returns the total aligned argument size for a function. |
| 990 |
// It includes the receiver, parameters, and results. |
| 991 |
func (t *Type) ArgWidth() int64 { |
| 992 |
t.wantEtype(TFUNC) |
| 993 |
return t.extra.(*Func).Argwid |
| 994 |
} |
| 995 |
|
| 996 |
func (t *Type) Size() int64 { |
| 997 |
if t.kind == TSSA { |
| 998 |
if t == TypeInt128 { |
| 999 |
return 16 |
| 1000 |
} |
| 1001 |
return 0 |
| 1002 |
} |
| 1003 |
CalcSize(t) |
| 1004 |
return t.width |
| 1005 |
} |
| 1006 |
|
| 1007 |
func (t *Type) Alignment() int64 { |
| 1008 |
CalcSize(t) |
| 1009 |
return int64(t.align) |
| 1010 |
} |
| 1011 |
|
| 1012 |
func (t *Type) SimpleString() string { |
| 1013 |
return t.kind.String() |
| 1014 |
} |
| 1015 |
|
| 1016 |
// Cmp is a comparison between values a and b. |
| 1017 |
// |
| 1018 |
// -1 if a < b |
| 1019 |
// 0 if a == b |
| 1020 |
// 1 if a > b |
| 1021 |
type Cmp int8 |
| 1022 |
|
| 1023 |
const ( |
| 1024 |
CMPlt = Cmp(-1) |
| 1025 |
CMPeq = Cmp(0) |
| 1026 |
CMPgt = Cmp(1) |
| 1027 |
) |
| 1028 |
|
| 1029 |
// Compare compares types for purposes of the SSA back |
| 1030 |
// end, returning a Cmp (one of CMPlt, CMPeq, CMPgt). |
| 1031 |
// The answers are correct for an optimizer |
| 1032 |
// or code generator, but not necessarily typechecking. |
| 1033 |
// The order chosen is arbitrary, only consistency and division |
| 1034 |
// into equivalence classes (Types that compare CMPeq) matters. |
| 1035 |
func (t *Type) Compare(x *Type) Cmp { |
| 1036 |
if x == t { |
| 1037 |
return CMPeq |
| 1038 |
} |
| 1039 |
return t.cmp(x) |
| 1040 |
} |
| 1041 |
|
| 1042 |
func cmpForNe(x bool) Cmp { |
| 1043 |
if x { |
| 1044 |
return CMPlt |
| 1045 |
} |
| 1046 |
return CMPgt |
| 1047 |
} |
| 1048 |
|
| 1049 |
func (r *Sym) cmpsym(s *Sym) Cmp { |
| 1050 |
if r == s { |
| 1051 |
return CMPeq |
| 1052 |
} |
| 1053 |
if r == nil { |
| 1054 |
return CMPlt |
| 1055 |
} |
| 1056 |
if s == nil { |
| 1057 |
return CMPgt |
| 1058 |
} |
| 1059 |
// Fast sort, not pretty sort |
| 1060 |
if len(r.Name) != len(s.Name) { |
| 1061 |
return cmpForNe(len(r.Name) < len(s.Name)) |
| 1062 |
} |
| 1063 |
if r.Pkg != s.Pkg { |
| 1064 |
if len(r.Pkg.Prefix) != len(s.Pkg.Prefix) { |
| 1065 |
return cmpForNe(len(r.Pkg.Prefix) < len(s.Pkg.Prefix)) |
| 1066 |
} |
| 1067 |
if r.Pkg.Prefix != s.Pkg.Prefix { |
| 1068 |
return cmpForNe(r.Pkg.Prefix < s.Pkg.Prefix) |
| 1069 |
} |
| 1070 |
} |
| 1071 |
if r.Name != s.Name { |
| 1072 |
return cmpForNe(r.Name < s.Name) |
| 1073 |
} |
| 1074 |
return CMPeq |
| 1075 |
} |
| 1076 |
|
| 1077 |
// cmp compares two *Types t and x, returning CMPlt, |
| 1078 |
// CMPeq, CMPgt as t<x, t==x, t>x, for an arbitrary |
| 1079 |
// and optimizer-centric notion of comparison. |
| 1080 |
// TODO(josharian): make this safe for recursive interface types |
| 1081 |
// and use in signatlist sorting. See issue 19869. |
| 1082 |
func (t *Type) cmp(x *Type) Cmp { |
| 1083 |
// This follows the structure of function identical in identity.go |
| 1084 |
// with two exceptions. |
| 1085 |
// 1. Symbols are compared more carefully because a <,=,> result is desired. |
| 1086 |
// 2. Maps are treated specially to avoid endless recursion -- maps |
| 1087 |
// contain an internal data type not expressible in Go source code. |
| 1088 |
if t == x { |
| 1089 |
return CMPeq |
| 1090 |
} |
| 1091 |
if t == nil { |
| 1092 |
return CMPlt |
| 1093 |
} |
| 1094 |
if x == nil { |
| 1095 |
return CMPgt |
| 1096 |
} |
| 1097 |
|
| 1098 |
if t.kind != x.kind { |
| 1099 |
return cmpForNe(t.kind < x.kind) |
| 1100 |
} |
| 1101 |
|
| 1102 |
if t.obj != nil || x.obj != nil { |
| 1103 |
// Special case: we keep byte and uint8 separate |
| 1104 |
// for error messages. Treat them as equal. |
| 1105 |
switch t.kind { |
| 1106 |
case TUINT8: |
| 1107 |
if (t == Types[TUINT8] || t == ByteType) && (x == Types[TUINT8] || x == ByteType) { |
| 1108 |
return CMPeq |
| 1109 |
} |
| 1110 |
|
| 1111 |
case TINT32: |
| 1112 |
if (t == Types[RuneType.kind] || t == RuneType) && (x == Types[RuneType.kind] || x == RuneType) { |
| 1113 |
return CMPeq |
| 1114 |
} |
| 1115 |
|
| 1116 |
case TINTER: |
| 1117 |
// Make sure named any type matches any empty interface. |
| 1118 |
if t == AnyType && x.IsEmptyInterface() || x == AnyType && t.IsEmptyInterface() { |
| 1119 |
return CMPeq |
| 1120 |
} |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
if c := t.Sym().cmpsym(x.Sym()); c != CMPeq { |
| 1125 |
return c |
| 1126 |
} |
| 1127 |
|
| 1128 |
if x.obj != nil { |
| 1129 |
return CMPeq |
| 1130 |
} |
| 1131 |
// both syms nil, look at structure below. |
| 1132 |
|
| 1133 |
switch t.kind { |
| 1134 |
case TBOOL, TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128, TUNSAFEPTR, TUINTPTR, |
| 1135 |
TINT8, TINT16, TINT32, TINT64, TINT, TUINT8, TUINT16, TUINT32, TUINT64, TUINT: |
| 1136 |
return CMPeq |
| 1137 |
|
| 1138 |
case TSSA: |
| 1139 |
tname := t.extra.(string) |
| 1140 |
xname := x.extra.(string) |
| 1141 |
// desire fast sorting, not pretty sorting. |
| 1142 |
if len(tname) == len(xname) { |
| 1143 |
if tname == xname { |
| 1144 |
return CMPeq |
| 1145 |
} |
| 1146 |
if tname < xname { |
| 1147 |
return CMPlt |
| 1148 |
} |
| 1149 |
return CMPgt |
| 1150 |
} |
| 1151 |
if len(tname) > len(xname) { |
| 1152 |
return CMPgt |
| 1153 |
} |
| 1154 |
return CMPlt |
| 1155 |
|
| 1156 |
case TTUPLE: |
| 1157 |
xtup := x.extra.(*Tuple) |
| 1158 |
ttup := t.extra.(*Tuple) |
| 1159 |
if c := ttup.first.Compare(xtup.first); c != CMPeq { |
| 1160 |
return c |
| 1161 |
} |
| 1162 |
return ttup.second.Compare(xtup.second) |
| 1163 |
|
| 1164 |
case TRESULTS: |
| 1165 |
xResults := x.extra.(*Results) |
| 1166 |
tResults := t.extra.(*Results) |
| 1167 |
xl, tl := len(xResults.Types), len(tResults.Types) |
| 1168 |
if tl != xl { |
| 1169 |
if tl < xl { |
| 1170 |
return CMPlt |
| 1171 |
} |
| 1172 |
return CMPgt |
| 1173 |
} |
| 1174 |
for i := 0; i < tl; i++ { |
| 1175 |
if c := tResults.Types[i].Compare(xResults.Types[i]); c != CMPeq { |
| 1176 |
return c |
| 1177 |
} |
| 1178 |
} |
| 1179 |
return CMPeq |
| 1180 |
|
| 1181 |
case TMAP: |
| 1182 |
if c := t.Key().cmp(x.Key()); c != CMPeq { |
| 1183 |
return c |
| 1184 |
} |
| 1185 |
return t.Elem().cmp(x.Elem()) |
| 1186 |
|
| 1187 |
case TPTR, TSLICE: |
| 1188 |
// No special cases for these, they are handled |
| 1189 |
// by the general code after the switch. |
| 1190 |
|
| 1191 |
case TSTRUCT: |
| 1192 |
if buildcfg.Experiment.SwissMap { |
| 1193 |
// Is this a map group type? |
| 1194 |
if t.StructType().Map == nil { |
| 1195 |
if x.StructType().Map != nil { |
| 1196 |
return CMPlt // nil < non-nil |
| 1197 |
} |
| 1198 |
// to the fallthrough |
| 1199 |
} else if x.StructType().Map == nil { |
| 1200 |
return CMPgt // nil > non-nil |
| 1201 |
} |
| 1202 |
// Both have non-nil Map, fallthrough to the general |
| 1203 |
// case. Note that the map type does not directly refer |
| 1204 |
// to the group type (it uses unsafe.Pointer). If it |
| 1205 |
// did, this would need special handling to avoid |
| 1206 |
// infinite recursion. |
| 1207 |
} else { |
| 1208 |
// Is this a map bucket type? |
| 1209 |
if t.StructType().Map == nil { |
| 1210 |
if x.StructType().Map != nil { |
| 1211 |
return CMPlt // nil < non-nil |
| 1212 |
} |
| 1213 |
// to the fallthrough |
| 1214 |
} else if x.StructType().Map == nil { |
| 1215 |
return CMPgt // nil > non-nil |
| 1216 |
} |
| 1217 |
// Both have non-nil Map, fallthrough to the general |
| 1218 |
// case. Note that the map type does not directly refer |
| 1219 |
// to the bucket type (it uses unsafe.Pointer). If it |
| 1220 |
// did, this would need special handling to avoid |
| 1221 |
// infinite recursion. |
| 1222 |
} |
| 1223 |
|
| 1224 |
tfs := t.Fields() |
| 1225 |
xfs := x.Fields() |
| 1226 |
for i := 0; i < len(tfs) && i < len(xfs); i++ { |
| 1227 |
t1, x1 := tfs[i], xfs[i] |
| 1228 |
if t1.Embedded != x1.Embedded { |
| 1229 |
return cmpForNe(t1.Embedded < x1.Embedded) |
| 1230 |
} |
| 1231 |
if t1.Note != x1.Note { |
| 1232 |
return cmpForNe(t1.Note < x1.Note) |
| 1233 |
} |
| 1234 |
if c := t1.Sym.cmpsym(x1.Sym); c != CMPeq { |
| 1235 |
return c |
| 1236 |
} |
| 1237 |
if c := t1.Type.cmp(x1.Type); c != CMPeq { |
| 1238 |
return c |
| 1239 |
} |
| 1240 |
} |
| 1241 |
if len(tfs) != len(xfs) { |
| 1242 |
return cmpForNe(len(tfs) < len(xfs)) |
| 1243 |
} |
| 1244 |
return CMPeq |
| 1245 |
|
| 1246 |
case TINTER: |
| 1247 |
tfs := t.AllMethods() |
| 1248 |
xfs := x.AllMethods() |
| 1249 |
for i := 0; i < len(tfs) && i < len(xfs); i++ { |
| 1250 |
t1, x1 := tfs[i], xfs[i] |
| 1251 |
if c := t1.Sym.cmpsym(x1.Sym); c != CMPeq { |
| 1252 |
return c |
| 1253 |
} |
| 1254 |
if c := t1.Type.cmp(x1.Type); c != CMPeq { |
| 1255 |
return c |
| 1256 |
} |
| 1257 |
} |
| 1258 |
if len(tfs) != len(xfs) { |
| 1259 |
return cmpForNe(len(tfs) < len(xfs)) |
| 1260 |
} |
| 1261 |
return CMPeq |
| 1262 |
|
| 1263 |
case TFUNC: |
| 1264 |
if tn, xn := t.NumRecvs(), x.NumRecvs(); tn != xn { |
| 1265 |
return cmpForNe(tn < xn) |
| 1266 |
} |
| 1267 |
if tn, xn := t.NumParams(), x.NumParams(); tn != xn { |
| 1268 |
return cmpForNe(tn < xn) |
| 1269 |
} |
| 1270 |
if tn, xn := t.NumResults(), x.NumResults(); tn != xn { |
| 1271 |
return cmpForNe(tn < xn) |
| 1272 |
} |
| 1273 |
if tv, xv := t.IsVariadic(), x.IsVariadic(); tv != xv { |
| 1274 |
return cmpForNe(!tv) |
| 1275 |
} |
| 1276 |
|
| 1277 |
tfs := t.RecvParamsResults() |
| 1278 |
xfs := x.RecvParamsResults() |
| 1279 |
for i, tf := range tfs { |
| 1280 |
if c := tf.Type.cmp(xfs[i].Type); c != CMPeq { |
| 1281 |
return c |
| 1282 |
} |
| 1283 |
} |
| 1284 |
return CMPeq |
| 1285 |
|
| 1286 |
case TARRAY: |
| 1287 |
if t.NumElem() != x.NumElem() { |
| 1288 |
return cmpForNe(t.NumElem() < x.NumElem()) |
| 1289 |
} |
| 1290 |
|
| 1291 |
case TCHAN: |
| 1292 |
if t.ChanDir() != x.ChanDir() { |
| 1293 |
return cmpForNe(t.ChanDir() < x.ChanDir()) |
| 1294 |
} |
| 1295 |
|
| 1296 |
default: |
| 1297 |
e := fmt.Sprintf("Do not know how to compare %v with %v", t, x) |
| 1298 |
panic(e) |
| 1299 |
} |
| 1300 |
|
| 1301 |
// Common element type comparison for TARRAY, TCHAN, TPTR, and TSLICE. |
| 1302 |
return t.Elem().cmp(x.Elem()) |
| 1303 |
} |
| 1304 |
|
| 1305 |
// IsKind reports whether t is a Type of the specified kind. |
| 1306 |
func (t *Type) IsKind(et Kind) bool { |
| 1307 |
return t != nil && t.kind == et |
| 1308 |
} |
| 1309 |
|
| 1310 |
func (t *Type) IsBoolean() bool { |
| 1311 |
return t.kind == TBOOL |
| 1312 |
} |
| 1313 |
|
| 1314 |
var unsignedEType = [...]Kind{ |
| 1315 |
TINT8: TUINT8, |
| 1316 |
TUINT8: TUINT8, |
| 1317 |
TINT16: TUINT16, |
| 1318 |
TUINT16: TUINT16, |
| 1319 |
TINT32: TUINT32, |
| 1320 |
TUINT32: TUINT32, |
| 1321 |
TINT64: TUINT64, |
| 1322 |
TUINT64: TUINT64, |
| 1323 |
TINT: TUINT, |
| 1324 |
TUINT: TUINT, |
| 1325 |
TUINTPTR: TUINTPTR, |
| 1326 |
} |
| 1327 |
|
| 1328 |
// ToUnsigned returns the unsigned equivalent of integer type t. |
| 1329 |
func (t *Type) ToUnsigned() *Type { |
| 1330 |
if !t.IsInteger() { |
| 1331 |
base.Fatalf("unsignedType(%v)", t) |
| 1332 |
} |
| 1333 |
return Types[unsignedEType[t.kind]] |
| 1334 |
} |
| 1335 |
|
| 1336 |
func (t *Type) IsInteger() bool { |
| 1337 |
switch t.kind { |
| 1338 |
case TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32, TINT64, TUINT64, TINT, TUINT, TUINTPTR: |
| 1339 |
return true |
| 1340 |
} |
| 1341 |
return t == UntypedInt || t == UntypedRune |
| 1342 |
} |
| 1343 |
|
| 1344 |
func (t *Type) IsSigned() bool { |
| 1345 |
switch t.kind { |
| 1346 |
case TINT8, TINT16, TINT32, TINT64, TINT: |
| 1347 |
return true |
| 1348 |
} |
| 1349 |
return false |
| 1350 |
} |
| 1351 |
|
| 1352 |
func (t *Type) IsUnsigned() bool { |
| 1353 |
switch t.kind { |
| 1354 |
case TUINT8, TUINT16, TUINT32, TUINT64, TUINT, TUINTPTR: |
| 1355 |
return true |
| 1356 |
} |
| 1357 |
return false |
| 1358 |
} |
| 1359 |
|
| 1360 |
func (t *Type) IsFloat() bool { |
| 1361 |
return t.kind == TFLOAT32 || t.kind == TFLOAT64 || t == UntypedFloat |
| 1362 |
} |
| 1363 |
|
| 1364 |
func (t *Type) IsComplex() bool { |
| 1365 |
return t.kind == TCOMPLEX64 || t.kind == TCOMPLEX128 || t == UntypedComplex |
| 1366 |
} |
| 1367 |
|
| 1368 |
// IsPtr reports whether t is a regular Go pointer type. |
| 1369 |
// This does not include unsafe.Pointer. |
| 1370 |
func (t *Type) IsPtr() bool { |
| 1371 |
return t.kind == TPTR |
| 1372 |
} |
| 1373 |
|
| 1374 |
// IsPtrElem reports whether t is the element of a pointer (to t). |
| 1375 |
func (t *Type) IsPtrElem() bool { |
| 1376 |
return t.cache.ptr != nil |
| 1377 |
} |
| 1378 |
|
| 1379 |
// IsUnsafePtr reports whether t is an unsafe pointer. |
| 1380 |
func (t *Type) IsUnsafePtr() bool { |
| 1381 |
return t.kind == TUNSAFEPTR |
| 1382 |
} |
| 1383 |
|
| 1384 |
// IsUintptr reports whether t is a uintptr. |
| 1385 |
func (t *Type) IsUintptr() bool { |
| 1386 |
return t.kind == TUINTPTR |
| 1387 |
} |
| 1388 |
|
| 1389 |
// IsPtrShaped reports whether t is represented by a single machine pointer. |
| 1390 |
// In addition to regular Go pointer types, this includes map, channel, and |
| 1391 |
// function types and unsafe.Pointer. It does not include array or struct types |
| 1392 |
// that consist of a single pointer shaped type. |
| 1393 |
// TODO(mdempsky): Should it? See golang.org/issue/15028. |
| 1394 |
func (t *Type) IsPtrShaped() bool { |
| 1395 |
return t.kind == TPTR || t.kind == TUNSAFEPTR || |
| 1396 |
t.kind == TMAP || t.kind == TCHAN || t.kind == TFUNC |
| 1397 |
} |
| 1398 |
|
| 1399 |
// HasNil reports whether the set of values determined by t includes nil. |
| 1400 |
func (t *Type) HasNil() bool { |
| 1401 |
switch t.kind { |
| 1402 |
case TCHAN, TFUNC, TINTER, TMAP, TNIL, TPTR, TSLICE, TUNSAFEPTR: |
| 1403 |
return true |
| 1404 |
} |
| 1405 |
return false |
| 1406 |
} |
| 1407 |
|
| 1408 |
func (t *Type) IsString() bool { |
| 1409 |
return t.kind == TSTRING |
| 1410 |
} |
| 1411 |
|
| 1412 |
func (t *Type) IsMap() bool { |
| 1413 |
return t.kind == TMAP |
| 1414 |
} |
| 1415 |
|
| 1416 |
func (t *Type) IsChan() bool { |
| 1417 |
return t.kind == TCHAN |
| 1418 |
} |
| 1419 |
|
| 1420 |
func (t *Type) IsSlice() bool { |
| 1421 |
return t.kind == TSLICE |
| 1422 |
} |
| 1423 |
|
| 1424 |
func (t *Type) IsArray() bool { |
| 1425 |
return t.kind == TARRAY |
| 1426 |
} |
| 1427 |
|
| 1428 |
func (t *Type) IsStruct() bool { |
| 1429 |
return t.kind == TSTRUCT |
| 1430 |
} |
| 1431 |
|
| 1432 |
func (t *Type) IsInterface() bool { |
| 1433 |
return t.kind == TINTER |
| 1434 |
} |
| 1435 |
|
| 1436 |
// IsEmptyInterface reports whether t is an empty interface type. |
| 1437 |
func (t *Type) IsEmptyInterface() bool { |
| 1438 |
return t.IsInterface() && len(t.AllMethods()) == 0 |
| 1439 |
} |
| 1440 |
|
| 1441 |
// IsScalar reports whether 't' is a scalar Go type, e.g. |
| 1442 |
// bool/int/float/complex. Note that struct and array types consisting |
| 1443 |
// of a single scalar element are not considered scalar, likewise |
| 1444 |
// pointer types are also not considered scalar. |
| 1445 |
func (t *Type) IsScalar() bool { |
| 1446 |
switch t.kind { |
| 1447 |
case TBOOL, TINT8, TUINT8, TINT16, TUINT16, TINT32, |
| 1448 |
TUINT32, TINT64, TUINT64, TINT, TUINT, |
| 1449 |
TUINTPTR, TCOMPLEX64, TCOMPLEX128, TFLOAT32, TFLOAT64: |
| 1450 |
return true |
| 1451 |
} |
| 1452 |
return false |
| 1453 |
} |
| 1454 |
|
| 1455 |
func (t *Type) PtrTo() *Type { |
| 1456 |
return NewPtr(t) |
| 1457 |
} |
| 1458 |
|
| 1459 |
func (t *Type) NumFields() int { |
| 1460 |
if t.kind == TRESULTS { |
| 1461 |
return len(t.extra.(*Results).Types) |
| 1462 |
} |
| 1463 |
return len(t.Fields()) |
| 1464 |
} |
| 1465 |
func (t *Type) FieldType(i int) *Type { |
| 1466 |
if t.kind == TTUPLE { |
| 1467 |
switch i { |
| 1468 |
case 0: |
| 1469 |
return t.extra.(*Tuple).first |
| 1470 |
case 1: |
| 1471 |
return t.extra.(*Tuple).second |
| 1472 |
default: |
| 1473 |
panic("bad tuple index") |
| 1474 |
} |
| 1475 |
} |
| 1476 |
if t.kind == TRESULTS { |
| 1477 |
return t.extra.(*Results).Types[i] |
| 1478 |
} |
| 1479 |
return t.Field(i).Type |
| 1480 |
} |
| 1481 |
func (t *Type) FieldOff(i int) int64 { |
| 1482 |
return t.Field(i).Offset |
| 1483 |
} |
| 1484 |
func (t *Type) FieldName(i int) string { |
| 1485 |
return t.Field(i).Sym.Name |
| 1486 |
} |
| 1487 |
|
| 1488 |
// OffsetOf reports the offset of the field of a struct. |
| 1489 |
// The field is looked up by name. |
| 1490 |
func (t *Type) OffsetOf(name string) int64 { |
| 1491 |
if t.kind != TSTRUCT { |
| 1492 |
base.Fatalf("can't call OffsetOf on non-struct %v", t) |
| 1493 |
} |
| 1494 |
for _, f := range t.Fields() { |
| 1495 |
if f.Sym.Name == name { |
| 1496 |
return f.Offset |
| 1497 |
} |
| 1498 |
} |
| 1499 |
base.Fatalf("couldn't find field %s in %v", name, t) |
| 1500 |
return -1 |
| 1501 |
} |
| 1502 |
|
| 1503 |
func (t *Type) NumElem() int64 { |
| 1504 |
t.wantEtype(TARRAY) |
| 1505 |
return t.extra.(*Array).Bound |
| 1506 |
} |
| 1507 |
|
| 1508 |
type componentsIncludeBlankFields bool |
| 1509 |
|
| 1510 |
const ( |
| 1511 |
IgnoreBlankFields componentsIncludeBlankFields = false |
| 1512 |
CountBlankFields componentsIncludeBlankFields = true |
| 1513 |
) |
| 1514 |
|
| 1515 |
// NumComponents returns the number of primitive elements that compose t. |
| 1516 |
// Struct and array types are flattened for the purpose of counting. |
| 1517 |
// All other types (including string, slice, and interface types) count as one element. |
| 1518 |
// If countBlank is IgnoreBlankFields, then blank struct fields |
| 1519 |
// (and their comprised elements) are excluded from the count. |
| 1520 |
// struct { x, y [3]int } has six components; [10]struct{ x, y string } has twenty. |
| 1521 |
func (t *Type) NumComponents(countBlank componentsIncludeBlankFields) int64 { |
| 1522 |
switch t.kind { |
| 1523 |
case TSTRUCT: |
| 1524 |
if t.IsFuncArgStruct() { |
| 1525 |
base.Fatalf("NumComponents func arg struct") |
| 1526 |
} |
| 1527 |
var n int64 |
| 1528 |
for _, f := range t.Fields() { |
| 1529 |
if countBlank == IgnoreBlankFields && f.Sym.IsBlank() { |
| 1530 |
continue |
| 1531 |
} |
| 1532 |
n += f.Type.NumComponents(countBlank) |
| 1533 |
} |
| 1534 |
return n |
| 1535 |
case TARRAY: |
| 1536 |
return t.NumElem() * t.Elem().NumComponents(countBlank) |
| 1537 |
} |
| 1538 |
return 1 |
| 1539 |
} |
| 1540 |
|
| 1541 |
// SoleComponent returns the only primitive component in t, |
| 1542 |
// if there is exactly one. Otherwise, it returns nil. |
| 1543 |
// Components are counted as in NumComponents, including blank fields. |
| 1544 |
// Keep in sync with cmd/compile/internal/walk/convert.go:soleComponent. |
| 1545 |
func (t *Type) SoleComponent() *Type { |
| 1546 |
switch t.kind { |
| 1547 |
case TSTRUCT: |
| 1548 |
if t.IsFuncArgStruct() { |
| 1549 |
base.Fatalf("SoleComponent func arg struct") |
| 1550 |
} |
| 1551 |
if t.NumFields() != 1 { |
| 1552 |
return nil |
| 1553 |
} |
| 1554 |
return t.Field(0).Type.SoleComponent() |
| 1555 |
case TARRAY: |
| 1556 |
if t.NumElem() != 1 { |
| 1557 |
return nil |
| 1558 |
} |
| 1559 |
return t.Elem().SoleComponent() |
| 1560 |
} |
| 1561 |
return t |
| 1562 |
} |
| 1563 |
|
| 1564 |
// ChanDir returns the direction of a channel type t. |
| 1565 |
// The direction will be one of Crecv, Csend, or Cboth. |
| 1566 |
func (t *Type) ChanDir() ChanDir { |
| 1567 |
t.wantEtype(TCHAN) |
| 1568 |
return t.extra.(*Chan).Dir |
| 1569 |
} |
| 1570 |
|
| 1571 |
func (t *Type) IsMemory() bool { |
| 1572 |
if t == TypeMem || t.kind == TTUPLE && t.extra.(*Tuple).second == TypeMem { |
| 1573 |
return true |
| 1574 |
} |
| 1575 |
if t.kind == TRESULTS { |
| 1576 |
if types := t.extra.(*Results).Types; len(types) > 0 && types[len(types)-1] == TypeMem { |
| 1577 |
return true |
| 1578 |
} |
| 1579 |
} |
| 1580 |
return false |
| 1581 |
} |
| 1582 |
func (t *Type) IsFlags() bool { return t == TypeFlags } |
| 1583 |
func (t *Type) IsVoid() bool { return t == TypeVoid } |
| 1584 |
func (t *Type) IsTuple() bool { return t.kind == TTUPLE } |
| 1585 |
func (t *Type) IsResults() bool { return t.kind == TRESULTS } |
| 1586 |
|
| 1587 |
// IsUntyped reports whether t is an untyped type. |
| 1588 |
func (t *Type) IsUntyped() bool { |
| 1589 |
if t == nil { |
| 1590 |
return false |
| 1591 |
} |
| 1592 |
if t == UntypedString || t == UntypedBool { |
| 1593 |
return true |
| 1594 |
} |
| 1595 |
switch t.kind { |
| 1596 |
case TNIL, TIDEAL: |
| 1597 |
return true |
| 1598 |
} |
| 1599 |
return false |
| 1600 |
} |
| 1601 |
|
| 1602 |
// HasPointers reports whether t contains a heap pointer. |
| 1603 |
// Note that this function ignores pointers to not-in-heap types. |
| 1604 |
func (t *Type) HasPointers() bool { |
| 1605 |
return PtrDataSize(t) > 0 |
| 1606 |
} |
| 1607 |
|
| 1608 |
var recvType *Type |
| 1609 |
|
| 1610 |
// FakeRecvType returns the singleton type used for interface method receivers. |
| 1611 |
func FakeRecvType() *Type { |
| 1612 |
if recvType == nil { |
| 1613 |
recvType = NewPtr(newType(TSTRUCT)) |
| 1614 |
} |
| 1615 |
return recvType |
| 1616 |
} |
| 1617 |
|
| 1618 |
func FakeRecv() *Field { |
| 1619 |
return NewField(base.AutogeneratedPos, nil, FakeRecvType()) |
| 1620 |
} |
| 1621 |
|
| 1622 |
var ( |
| 1623 |
// TSSA types. HasPointers assumes these are pointer-free. |
| 1624 |
TypeInvalid = newSSA("invalid") |
| 1625 |
TypeMem = newSSA("mem") |
| 1626 |
TypeFlags = newSSA("flags") |
| 1627 |
TypeVoid = newSSA("void") |
| 1628 |
TypeInt128 = newSSA("int128") |
| 1629 |
TypeResultMem = newResults([]*Type{TypeMem}) |
| 1630 |
) |
| 1631 |
|
| 1632 |
func init() { |
| 1633 |
TypeInt128.width = 16 |
| 1634 |
TypeInt128.align = 8 |
| 1635 |
} |
| 1636 |
|
| 1637 |
// NewNamed returns a new named type for the given type name. obj should be an |
| 1638 |
// ir.Name. The new type is incomplete (marked as TFORW kind), and the underlying |
| 1639 |
// type should be set later via SetUnderlying(). References to the type are |
| 1640 |
// maintained until the type is filled in, so those references can be updated when |
| 1641 |
// the type is complete. |
| 1642 |
func NewNamed(obj Object) *Type { |
| 1643 |
t := newType(TFORW) |
| 1644 |
t.obj = obj |
| 1645 |
sym := obj.Sym() |
| 1646 |
if sym.Pkg == ShapePkg { |
| 1647 |
t.SetIsShape(true) |
| 1648 |
t.SetHasShape(true) |
| 1649 |
} |
| 1650 |
if sym.Pkg.Path == "internal/runtime/sys" && sym.Name == "nih" { |
| 1651 |
// Recognize the special not-in-heap type. Any type including |
| 1652 |
// this type will also be not-in-heap. |
| 1653 |
// This logic is duplicated in go/types and |
| 1654 |
// cmd/compile/internal/types2. |
| 1655 |
t.SetNotInHeap(true) |
| 1656 |
} |
| 1657 |
return t |
| 1658 |
} |
| 1659 |
|
| 1660 |
// Obj returns the canonical type name node for a named type t, nil for an unnamed type. |
| 1661 |
func (t *Type) Obj() Object { |
| 1662 |
return t.obj |
| 1663 |
} |
| 1664 |
|
| 1665 |
// SetUnderlying sets the underlying type of an incomplete type (i.e. type whose kind |
| 1666 |
// is currently TFORW). SetUnderlying automatically updates any types that were waiting |
| 1667 |
// for this type to be completed. |
| 1668 |
func (t *Type) SetUnderlying(underlying *Type) { |
| 1669 |
if underlying.kind == TFORW { |
| 1670 |
// This type isn't computed yet; when it is, update n. |
| 1671 |
underlying.forwardType().Copyto = append(underlying.forwardType().Copyto, t) |
| 1672 |
return |
| 1673 |
} |
| 1674 |
|
| 1675 |
ft := t.forwardType() |
| 1676 |
|
| 1677 |
// TODO(mdempsky): Fix Type rekinding. |
| 1678 |
t.kind = underlying.kind |
| 1679 |
t.extra = underlying.extra |
| 1680 |
t.width = underlying.width |
| 1681 |
t.align = underlying.align |
| 1682 |
t.alg = underlying.alg |
| 1683 |
t.ptrBytes = underlying.ptrBytes |
| 1684 |
t.intRegs = underlying.intRegs |
| 1685 |
t.floatRegs = underlying.floatRegs |
| 1686 |
t.underlying = underlying.underlying |
| 1687 |
|
| 1688 |
if underlying.NotInHeap() { |
| 1689 |
t.SetNotInHeap(true) |
| 1690 |
} |
| 1691 |
if underlying.HasShape() { |
| 1692 |
t.SetHasShape(true) |
| 1693 |
} |
| 1694 |
|
| 1695 |
// spec: "The declared type does not inherit any methods bound |
| 1696 |
// to the existing type, but the method set of an interface |
| 1697 |
// type [...] remains unchanged." |
| 1698 |
if t.IsInterface() { |
| 1699 |
t.methods = underlying.methods |
| 1700 |
t.allMethods = underlying.allMethods |
| 1701 |
} |
| 1702 |
|
| 1703 |
// Update types waiting on this type. |
| 1704 |
for _, w := range ft.Copyto { |
| 1705 |
w.SetUnderlying(t) |
| 1706 |
} |
| 1707 |
|
| 1708 |
// Double-check use of type as embedded type. |
| 1709 |
if ft.Embedlineno.IsKnown() { |
| 1710 |
if t.IsPtr() || t.IsUnsafePtr() { |
| 1711 |
base.ErrorfAt(ft.Embedlineno, errors.InvalidPtrEmbed, "embedded type cannot be a pointer") |
| 1712 |
} |
| 1713 |
} |
| 1714 |
} |
| 1715 |
|
| 1716 |
func fieldsHasShape(fields []*Field) bool { |
| 1717 |
for _, f := range fields { |
| 1718 |
if f.Type != nil && f.Type.HasShape() { |
| 1719 |
return true |
| 1720 |
} |
| 1721 |
} |
| 1722 |
return false |
| 1723 |
} |
| 1724 |
|
| 1725 |
// newBasic returns a new basic type of the given kind. |
| 1726 |
func newBasic(kind Kind, obj Object) *Type { |
| 1727 |
t := newType(kind) |
| 1728 |
t.obj = obj |
| 1729 |
return t |
| 1730 |
} |
| 1731 |
|
| 1732 |
// NewInterface returns a new interface for the given methods and |
| 1733 |
// embedded types. Embedded types are specified as fields with no Sym. |
| 1734 |
func NewInterface(methods []*Field) *Type { |
| 1735 |
t := newType(TINTER) |
| 1736 |
t.SetInterface(methods) |
| 1737 |
for _, f := range methods { |
| 1738 |
// f.Type could be nil for a broken interface declaration |
| 1739 |
if f.Type != nil && f.Type.HasShape() { |
| 1740 |
t.SetHasShape(true) |
| 1741 |
break |
| 1742 |
} |
| 1743 |
} |
| 1744 |
return t |
| 1745 |
} |
| 1746 |
|
| 1747 |
// NewSignature returns a new function type for the given receiver, |
| 1748 |
// parameters, and results, any of which may be nil. |
| 1749 |
func NewSignature(recv *Field, params, results []*Field) *Type { |
| 1750 |
startParams := 0 |
| 1751 |
if recv != nil { |
| 1752 |
startParams = 1 |
| 1753 |
} |
| 1754 |
startResults := startParams + len(params) |
| 1755 |
|
| 1756 |
allParams := make([]*Field, startResults+len(results)) |
| 1757 |
if recv != nil { |
| 1758 |
allParams[0] = recv |
| 1759 |
} |
| 1760 |
copy(allParams[startParams:], params) |
| 1761 |
copy(allParams[startResults:], results) |
| 1762 |
|
| 1763 |
t := newType(TFUNC) |
| 1764 |
ft := t.funcType() |
| 1765 |
|
| 1766 |
funargs := func(fields []*Field) *Type { |
| 1767 |
s := NewStruct(fields) |
| 1768 |
s.StructType().ParamTuple = true |
| 1769 |
return s |
| 1770 |
} |
| 1771 |
|
| 1772 |
ft.allParams = allParams |
| 1773 |
ft.startParams = startParams |
| 1774 |
ft.startResults = startResults |
| 1775 |
|
| 1776 |
ft.resultsTuple = funargs(allParams[startResults:]) |
| 1777 |
|
| 1778 |
if fieldsHasShape(allParams) { |
| 1779 |
t.SetHasShape(true) |
| 1780 |
} |
| 1781 |
|
| 1782 |
return t |
| 1783 |
} |
| 1784 |
|
| 1785 |
// NewStruct returns a new struct with the given fields. |
| 1786 |
func NewStruct(fields []*Field) *Type { |
| 1787 |
t := newType(TSTRUCT) |
| 1788 |
t.setFields(fields) |
| 1789 |
if fieldsHasShape(fields) { |
| 1790 |
t.SetHasShape(true) |
| 1791 |
} |
| 1792 |
for _, f := range fields { |
| 1793 |
if f.Type.NotInHeap() { |
| 1794 |
t.SetNotInHeap(true) |
| 1795 |
break |
| 1796 |
} |
| 1797 |
} |
| 1798 |
|
| 1799 |
return t |
| 1800 |
} |
| 1801 |
|
| 1802 |
var ( |
| 1803 |
IsInt [NTYPE]bool |
| 1804 |
IsFloat [NTYPE]bool |
| 1805 |
IsComplex [NTYPE]bool |
| 1806 |
IsSimple [NTYPE]bool |
| 1807 |
) |
| 1808 |
|
| 1809 |
var IsOrdered [NTYPE]bool |
| 1810 |
|
| 1811 |
// IsReflexive reports whether t has a reflexive equality operator. |
| 1812 |
// That is, if x==x for all x of type t. |
| 1813 |
func IsReflexive(t *Type) bool { |
| 1814 |
switch t.Kind() { |
| 1815 |
case TBOOL, |
| 1816 |
TINT, |
| 1817 |
TUINT, |
| 1818 |
TINT8, |
| 1819 |
TUINT8, |
| 1820 |
TINT16, |
| 1821 |
TUINT16, |
| 1822 |
TINT32, |
| 1823 |
TUINT32, |
| 1824 |
TINT64, |
| 1825 |
TUINT64, |
| 1826 |
TUINTPTR, |
| 1827 |
TPTR, |
| 1828 |
TUNSAFEPTR, |
| 1829 |
TSTRING, |
| 1830 |
TCHAN: |
| 1831 |
return true |
| 1832 |
|
| 1833 |
case TFLOAT32, |
| 1834 |
TFLOAT64, |
| 1835 |
TCOMPLEX64, |
| 1836 |
TCOMPLEX128, |
| 1837 |
TINTER: |
| 1838 |
return false |
| 1839 |
|
| 1840 |
case TARRAY: |
| 1841 |
return IsReflexive(t.Elem()) |
| 1842 |
|
| 1843 |
case TSTRUCT: |
| 1844 |
for _, t1 := range t.Fields() { |
| 1845 |
if !IsReflexive(t1.Type) { |
| 1846 |
return false |
| 1847 |
} |
| 1848 |
} |
| 1849 |
return true |
| 1850 |
|
| 1851 |
default: |
| 1852 |
base.Fatalf("bad type for map key: %v", t) |
| 1853 |
return false |
| 1854 |
} |
| 1855 |
} |
| 1856 |
|
| 1857 |
// Can this type be stored directly in an interface word? |
| 1858 |
// Yes, if the representation is a single pointer. |
| 1859 |
func IsDirectIface(t *Type) bool { |
| 1860 |
switch t.Kind() { |
| 1861 |
case TPTR: |
| 1862 |
// Pointers to notinheap types must be stored indirectly. See issue 42076. |
| 1863 |
return !t.Elem().NotInHeap() |
| 1864 |
case TCHAN, |
| 1865 |
TMAP, |
| 1866 |
TFUNC, |
| 1867 |
TUNSAFEPTR: |
| 1868 |
return true |
| 1869 |
|
| 1870 |
case TARRAY: |
| 1871 |
// Array of 1 direct iface type can be direct. |
| 1872 |
return t.NumElem() == 1 && IsDirectIface(t.Elem()) |
| 1873 |
|
| 1874 |
case TSTRUCT: |
| 1875 |
// Struct with 1 field of direct iface type can be direct. |
| 1876 |
return t.NumFields() == 1 && IsDirectIface(t.Field(0).Type) |
| 1877 |
} |
| 1878 |
|
| 1879 |
return false |
| 1880 |
} |
| 1881 |
|
| 1882 |
// IsInterfaceMethod reports whether (field) m is |
| 1883 |
// an interface method. Such methods have the |
| 1884 |
// special receiver type types.FakeRecvType(). |
| 1885 |
func IsInterfaceMethod(f *Type) bool { |
| 1886 |
return f.Recv().Type == FakeRecvType() |
| 1887 |
} |
| 1888 |
|
| 1889 |
// IsMethodApplicable reports whether method m can be called on a |
| 1890 |
// value of type t. This is necessary because we compute a single |
| 1891 |
// method set for both T and *T, but some *T methods are not |
| 1892 |
// applicable to T receivers. |
| 1893 |
func IsMethodApplicable(t *Type, m *Field) bool { |
| 1894 |
return t.IsPtr() || !m.Type.Recv().Type.IsPtr() || IsInterfaceMethod(m.Type) || m.Embedded == 2 |
| 1895 |
} |
| 1896 |
|
| 1897 |
// RuntimeSymName returns the name of s if it's in package "runtime"; otherwise |
| 1898 |
// it returns "". |
| 1899 |
func RuntimeSymName(s *Sym) string { |
| 1900 |
if s.Pkg.Path == "runtime" { |
| 1901 |
return s.Name |
| 1902 |
} |
| 1903 |
return "" |
| 1904 |
} |
| 1905 |
|
| 1906 |
// ReflectSymName returns the name of s if it's in package "reflect"; otherwise |
| 1907 |
// it returns "". |
| 1908 |
func ReflectSymName(s *Sym) string { |
| 1909 |
if s.Pkg.Path == "reflect" { |
| 1910 |
return s.Name |
| 1911 |
} |
| 1912 |
return "" |
| 1913 |
} |
| 1914 |
|
| 1915 |
// IsNoInstrumentPkg reports whether p is a package that |
| 1916 |
// should not be instrumented. |
| 1917 |
func IsNoInstrumentPkg(p *Pkg) bool { |
| 1918 |
return objabi.LookupPkgSpecial(p.Path).NoInstrument |
| 1919 |
} |
| 1920 |
|
| 1921 |
// IsNoRacePkg reports whether p is a package that |
| 1922 |
// should not be race instrumented. |
| 1923 |
func IsNoRacePkg(p *Pkg) bool { |
| 1924 |
return objabi.LookupPkgSpecial(p.Path).NoRaceFunc |
| 1925 |
} |
| 1926 |
|
| 1927 |
// IsRuntimePkg reports whether p is a runtime package. |
| 1928 |
func IsRuntimePkg(p *Pkg) bool { |
| 1929 |
return objabi.LookupPkgSpecial(p.Path).Runtime |
| 1930 |
} |
| 1931 |
|
| 1932 |
// ReceiverBaseType returns the underlying type, if any, |
| 1933 |
// that owns methods with receiver parameter t. |
| 1934 |
// The result is either a named type or an anonymous struct. |
| 1935 |
func ReceiverBaseType(t *Type) *Type { |
| 1936 |
if t == nil { |
| 1937 |
return nil |
| 1938 |
} |
| 1939 |
|
| 1940 |
// Strip away pointer if it's there. |
| 1941 |
if t.IsPtr() { |
| 1942 |
if t.Sym() != nil { |
| 1943 |
return nil |
| 1944 |
} |
| 1945 |
t = t.Elem() |
| 1946 |
if t == nil { |
| 1947 |
return nil |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
// Must be a named type or anonymous struct. |
| 1952 |
if t.Sym() == nil && !t.IsStruct() { |
| 1953 |
return nil |
| 1954 |
} |
| 1955 |
|
| 1956 |
// Check types. |
| 1957 |
if IsSimple[t.Kind()] { |
| 1958 |
return t |
| 1959 |
} |
| 1960 |
switch t.Kind() { |
| 1961 |
case TARRAY, TCHAN, TFUNC, TMAP, TSLICE, TSTRING, TSTRUCT: |
| 1962 |
return t |
| 1963 |
} |
| 1964 |
return nil |
| 1965 |
} |
| 1966 |
|
| 1967 |
func FloatForComplex(t *Type) *Type { |
| 1968 |
switch t.Kind() { |
| 1969 |
case TCOMPLEX64: |
| 1970 |
return Types[TFLOAT32] |
| 1971 |
case TCOMPLEX128: |
| 1972 |
return Types[TFLOAT64] |
| 1973 |
} |
| 1974 |
base.Fatalf("unexpected type: %v", t) |
| 1975 |
return nil |
| 1976 |
} |
| 1977 |
|
| 1978 |
func ComplexForFloat(t *Type) *Type { |
| 1979 |
switch t.Kind() { |
| 1980 |
case TFLOAT32: |
| 1981 |
return Types[TCOMPLEX64] |
| 1982 |
case TFLOAT64: |
| 1983 |
return Types[TCOMPLEX128] |
| 1984 |
} |
| 1985 |
base.Fatalf("unexpected type: %v", t) |
| 1986 |
return nil |
| 1987 |
} |
| 1988 |
|
| 1989 |
func TypeSym(t *Type) *Sym { |
| 1990 |
return TypeSymLookup(TypeSymName(t)) |
| 1991 |
} |
| 1992 |
|
| 1993 |
func TypeSymLookup(name string) *Sym { |
| 1994 |
typepkgmu.Lock() |
| 1995 |
s := typepkg.Lookup(name) |
| 1996 |
typepkgmu.Unlock() |
| 1997 |
return s |
| 1998 |
} |
| 1999 |
|
| 2000 |
func TypeSymName(t *Type) string { |
| 2001 |
name := t.LinkString() |
| 2002 |
// Use a separate symbol name for Noalg types for #17752. |
| 2003 |
if TypeHasNoAlg(t) { |
| 2004 |
name = "noalg." + name |
| 2005 |
} |
| 2006 |
return name |
| 2007 |
} |
| 2008 |
|
| 2009 |
// Fake package for runtime type info (headers) |
| 2010 |
// Don't access directly, use typeLookup below. |
| 2011 |
var ( |
| 2012 |
typepkgmu sync.Mutex // protects typepkg lookups |
| 2013 |
typepkg = NewPkg("type", "type") |
| 2014 |
) |
| 2015 |
|
| 2016 |
var SimType [NTYPE]Kind |
| 2017 |
|
| 2018 |
// Fake package for shape types (see typecheck.Shapify()). |
| 2019 |
var ShapePkg = NewPkg("go.shape", "go.shape") |