1 // Copyright 2015 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 // This file provides methods that let us export a Type as an ../ssa:Type. 6 // We don't export this package's Type directly because it would lead 7 // to an import cycle with this package and ../ssa. 8 // TODO: move Type to its own package, then we don't need to dance around import cycles. 9 10 package gc 11 12 import ( 13 "cmd/compile/internal/ssa" 14 "fmt" 15 ) 16 17 // EType describes a kind of type. 18 type EType uint8 19 20 const ( 21 Txxx = iota 22 23 TINT8 24 TUINT8 25 TINT16 26 TUINT16 27 TINT32 28 TUINT32 29 TINT64 30 TUINT64 31 TINT 32 TUINT 33 TUINTPTR 34 35 TCOMPLEX64 36 TCOMPLEX128 37 38 TFLOAT32 39 TFLOAT64 40 41 TBOOL 42 43 TPTR32 44 TPTR64 45 46 TFUNC 47 TSLICE 48 TARRAY 49 TSTRUCT 50 TCHAN 51 TMAP 52 TINTER 53 TFORW 54 TANY 55 TSTRING 56 TUNSAFEPTR 57 58 // pseudo-types for literals 59 TIDEAL 60 TNIL 61 TBLANK 62 63 // pseudo-types for frame layout 64 TFUNCARGS 65 TCHANARGS 66 TINTERMETH 67 68 // pseudo-types for import/export 69 TDDDFIELD // wrapper: contained type is a ... field 70 71 NTYPE 72 ) 73 74 // ChanDir is whether a channel can send, receive, or both. 75 type ChanDir uint8 76 77 func (c ChanDir) CanRecv() bool { return c&Crecv != 0 } 78 func (c ChanDir) CanSend() bool { return c&Csend != 0 } 79 80 const ( 81 // types of channel 82 // must match ../../../../reflect/type.go:/ChanDir 83 Crecv ChanDir = 1 << 0 84 Csend ChanDir = 1 << 1 85 Cboth ChanDir = Crecv | Csend 86 ) 87 88 // Types stores pointers to predeclared named types. 89 // 90 // It also stores pointers to several special types: 91 // - Types[TANY] is the placeholder "any" type recognized by substArgTypes. 92 // - Types[TBLANK] represents the blank variable's type. 93 // - Types[TIDEAL] represents untyped numeric constants. 94 // - Types[TNIL] represents the predeclared "nil" value's type. 95 // - Types[TUNSAFEPTR] is package unsafe's Pointer type. 96 var Types [NTYPE]*Type 97 98 var ( 99 // Predeclared alias types. Kept separate for better error messages. 100 bytetype *Type 101 runetype *Type 102 103 // Predeclared error interface type. 104 errortype *Type 105 106 // Types to represent untyped string and boolean constants. 107 idealstring *Type 108 idealbool *Type 109 110 // Types to represent untyped numeric constants. 111 // Note: Currently these are only used within the binary export 112 // data format. The rest of the compiler only uses Types[TIDEAL]. 113 idealint = typ(TIDEAL) 114 idealrune = typ(TIDEAL) 115 idealfloat = typ(TIDEAL) 116 idealcomplex = typ(TIDEAL) 117 ) 118 119 // A Type represents a Go type. 120 type Type struct { 121 // Extra contains extra etype-specific fields. 122 // As an optimization, those etype-specific structs which contain exactly 123 // one pointer-shaped field are stored as values rather than pointers when possible. 124 // 125 // TMAP: *MapType 126 // TFORW: *ForwardType 127 // TFUNC: *FuncType 128 // TINTERMETHOD: InterMethType 129 // TSTRUCT: *StructType 130 // TINTER: *InterType 131 // TDDDFIELD: DDDFieldType 132 // TFUNCARGS: FuncArgsType 133 // TCHANARGS: ChanArgsType 134 // TCHAN: *ChanType 135 // TPTR32, TPTR64: PtrType 136 // TARRAY: *ArrayType 137 // TSLICE: SliceType 138 Extra interface{} 139 140 // Width is the width of this Type in bytes. 141 Width int64 142 143 methods Fields 144 allMethods Fields 145 146 nod *Node // canonical OTYPE node 147 Orig *Type // original type (type literal or predefined type) 148 149 sliceOf *Type 150 ptrTo *Type 151 152 Sym *Sym // symbol containing name, for named types 153 Vargen int32 // unique name for OTYPE/ONAME 154 Lineno int32 // line at which this type was declared, implicitly or explicitly 155 156 Etype EType // kind of type 157 Noalg bool // suppress hash and eq algorithm generation 158 Trecur uint8 // to detect loops 159 Local bool // created in this file 160 Deferwidth bool 161 Broke bool // broken type definition. 162 Align uint8 // the required alignment of this type, in bytes 163 NotInHeap bool // type cannot be heap allocated 164 } 165 166 // MapType contains Type fields specific to maps. 167 type MapType struct { 168 Key *Type // Key type 169 Val *Type // Val (elem) type 170 171 Bucket *Type // internal struct type representing a hash bucket 172 Hmap *Type // internal struct type representing the Hmap (map header object) 173 Hiter *Type // internal struct type representing hash iterator state 174 } 175 176 // MapType returns t's extra map-specific fields. 177 func (t *Type) MapType() *MapType { 178 t.wantEtype(TMAP) 179 return t.Extra.(*MapType) 180 } 181 182 // ForwardType contains Type fields specific to forward types. 183 type ForwardType struct { 184 Copyto []*Node // where to copy the eventual value to 185 Embedlineno int32 // first use of this type as an embedded type 186 } 187 188 // ForwardType returns t's extra forward-type-specific fields. 189 func (t *Type) ForwardType() *ForwardType { 190 t.wantEtype(TFORW) 191 return t.Extra.(*ForwardType) 192 } 193 194 // FuncType contains Type fields specific to func types. 195 type FuncType struct { 196 Receiver *Type // function receiver 197 Results *Type // function results 198 Params *Type // function params 199 200 Nname *Node 201 202 // Argwid is the total width of the function receiver, params, and results. 203 // It gets calculated via a temporary TFUNCARGS type. 204 // Note that TFUNC's Width is Widthptr. 205 Argwid int64 206 207 Outnamed bool 208 } 209 210 // FuncType returns t's extra func-specific fields. 211 func (t *Type) FuncType() *FuncType { 212 t.wantEtype(TFUNC) 213 return t.Extra.(*FuncType) 214 } 215 216 // InterMethType contains Type fields specific to interface method pseudo-types. 217 type InterMethType struct { 218 Nname *Node 219 } 220 221 // StructType contains Type fields specific to struct types. 222 type StructType struct { 223 fields Fields 224 225 // Maps have three associated internal structs (see struct MapType). 226 // Map links such structs back to their map type. 227 Map *Type 228 229 Funarg Funarg // type of function arguments for arg struct 230 Haspointers uint8 // 0 unknown, 1 no, 2 yes 231 } 232 233 // Fnstruct records the kind of function argument 234 type Funarg uint8 235 236 const ( 237 FunargNone Funarg = iota 238 FunargRcvr // receiver 239 FunargParams // input parameters 240 FunargResults // output results 241 ) 242 243 // StructType returns t's extra struct-specific fields. 244 func (t *Type) StructType() *StructType { 245 t.wantEtype(TSTRUCT) 246 return t.Extra.(*StructType) 247 } 248 249 // InterType contains Type fields specific to interface types. 250 type InterType struct { 251 fields Fields 252 } 253 254 // PtrType contains Type fields specific to pointer types. 255 type PtrType struct { 256 Elem *Type // element type 257 } 258 259 // DDDFieldType contains Type fields specific to TDDDFIELD types. 260 type DDDFieldType struct { 261 T *Type // reference to a slice type for ... args 262 } 263 264 // ChanArgsType contains Type fields specific to TCHANARGS types. 265 type ChanArgsType struct { 266 T *Type // reference to a chan type whose elements need a width check 267 } 268 269 // // FuncArgsType contains Type fields specific to TFUNCARGS types. 270 type FuncArgsType struct { 271 T *Type // reference to a func type whose elements need a width check 272 } 273 274 // ChanType contains Type fields specific to channel types. 275 type ChanType struct { 276 Elem *Type // element type 277 Dir ChanDir // channel direction 278 } 279 280 // ChanType returns t's extra channel-specific fields. 281 func (t *Type) ChanType() *ChanType { 282 t.wantEtype(TCHAN) 283 return t.Extra.(*ChanType) 284 } 285 286 // ArrayType contains Type fields specific to array types. 287 type ArrayType struct { 288 Elem *Type // element type 289 Bound int64 // number of elements; <0 if unknown yet 290 Haspointers uint8 // 0 unknown, 1 no, 2 yes 291 } 292 293 // SliceType contains Type fields specific to slice types. 294 type SliceType struct { 295 Elem *Type // element type 296 } 297 298 // A Field represents a field in a struct or a method in an interface or 299 // associated with a named type. 300 type Field struct { 301 Nointerface bool 302 Embedded uint8 // embedded field 303 Funarg Funarg 304 Broke bool // broken field definition 305 Isddd bool // field is ... argument 306 307 Sym *Sym 308 Nname *Node 309 310 Type *Type // field type 311 312 // Offset in bytes of this field or method within its enclosing struct 313 // or interface Type. 314 Offset int64 315 316 Note string // literal string annotation 317 } 318 319 // End returns the offset of the first byte immediately after this field. 320 func (f *Field) End() int64 { 321 return f.Offset + f.Type.Width 322 } 323 324 // Fields is a pointer to a slice of *Field. 325 // This saves space in Types that do not have fields or methods 326 // compared to a simple slice of *Field. 327 type Fields struct { 328 s *[]*Field 329 } 330 331 // Len returns the number of entries in f. 332 func (f *Fields) Len() int { 333 if f.s == nil { 334 return 0 335 } 336 return len(*f.s) 337 } 338 339 // Slice returns the entries in f as a slice. 340 // Changes to the slice entries will be reflected in f. 341 func (f *Fields) Slice() []*Field { 342 if f.s == nil { 343 return nil 344 } 345 return *f.s 346 } 347 348 // Index returns the i'th element of Fields. 349 // It panics if f does not have at least i+1 elements. 350 func (f *Fields) Index(i int) *Field { 351 return (*f.s)[i] 352 } 353 354 // Set sets f to a slice. 355 // This takes ownership of the slice. 356 func (f *Fields) Set(s []*Field) { 357 if len(s) == 0 { 358 f.s = nil 359 } else { 360 // Copy s and take address of t rather than s to avoid 361 // allocation in the case where len(s) == 0. 362 t := s 363 f.s = &t 364 } 365 } 366 367 // Append appends entries to f. 368 func (f *Fields) Append(s ...*Field) { 369 if f.s == nil { 370 f.s = new([]*Field) 371 } 372 *f.s = append(*f.s, s...) 373 } 374 375 // typ returns a new Type of the specified kind. 376 func typ(et EType) *Type { 377 t := &Type{ 378 Etype: et, 379 Width: BADWIDTH, 380 Lineno: lineno, 381 } 382 t.Orig = t 383 // TODO(josharian): lazily initialize some of these? 384 switch t.Etype { 385 case TMAP: 386 t.Extra = new(MapType) 387 case TFORW: 388 t.Extra = new(ForwardType) 389 case TFUNC: 390 t.Extra = new(FuncType) 391 case TINTERMETH: 392 t.Extra = InterMethType{} 393 case TSTRUCT: 394 t.Extra = new(StructType) 395 case TINTER: 396 t.Extra = new(InterType) 397 case TPTR32, TPTR64: 398 t.Extra = PtrType{} 399 case TCHANARGS: 400 t.Extra = ChanArgsType{} 401 case TFUNCARGS: 402 t.Extra = FuncArgsType{} 403 case TDDDFIELD: 404 t.Extra = DDDFieldType{} 405 case TCHAN: 406 t.Extra = new(ChanType) 407 } 408 return t 409 } 410 411 // typArray returns a new fixed-length array Type. 412 func typArray(elem *Type, bound int64) *Type { 413 if bound < 0 { 414 Fatalf("typArray: invalid bound %v", bound) 415 } 416 t := typ(TARRAY) 417 t.Extra = &ArrayType{Elem: elem, Bound: bound} 418 t.NotInHeap = elem.NotInHeap 419 return t 420 } 421 422 // typSlice returns the slice Type with element type elem. 423 func typSlice(elem *Type) *Type { 424 if t := elem.sliceOf; t != nil { 425 if t.Elem() != elem { 426 Fatalf("elem mismatch") 427 } 428 return t 429 } 430 431 t := typ(TSLICE) 432 t.Extra = SliceType{Elem: elem} 433 elem.sliceOf = t 434 return t 435 } 436 437 // typDDDArray returns a new [...]T array Type. 438 func typDDDArray(elem *Type) *Type { 439 t := typ(TARRAY) 440 t.Extra = &ArrayType{Elem: elem, Bound: -1} 441 t.NotInHeap = elem.NotInHeap 442 return t 443 } 444 445 // typChan returns a new chan Type with direction dir. 446 func typChan(elem *Type, dir ChanDir) *Type { 447 t := typ(TCHAN) 448 ct := t.ChanType() 449 ct.Elem = elem 450 ct.Dir = dir 451 return t 452 } 453 454 // typMap returns a new map Type with key type k and element (aka value) type v. 455 func typMap(k, v *Type) *Type { 456 t := typ(TMAP) 457 mt := t.MapType() 458 mt.Key = k 459 mt.Val = v 460 return t 461 } 462 463 // typPtr returns the pointer type pointing to t. 464 func typPtr(elem *Type) *Type { 465 if t := elem.ptrTo; t != nil { 466 if t.Elem() != elem { 467 Fatalf("elem mismatch") 468 } 469 return t 470 } 471 472 t := typ(Tptr) 473 t.Extra = PtrType{Elem: elem} 474 t.Width = int64(Widthptr) 475 t.Align = uint8(Widthptr) 476 elem.ptrTo = t 477 return t 478 } 479 480 // typDDDField returns a new TDDDFIELD type for slice type s. 481 func typDDDField(s *Type) *Type { 482 t := typ(TDDDFIELD) 483 t.Extra = DDDFieldType{T: s} 484 return t 485 } 486 487 // typChanArgs returns a new TCHANARGS type for channel type c. 488 func typChanArgs(c *Type) *Type { 489 t := typ(TCHANARGS) 490 t.Extra = ChanArgsType{T: c} 491 return t 492 } 493 494 // typFuncArgs returns a new TFUNCARGS type for func type f. 495 func typFuncArgs(f *Type) *Type { 496 t := typ(TFUNCARGS) 497 t.Extra = FuncArgsType{T: f} 498 return t 499 } 500 501 func newField() *Field { 502 return &Field{ 503 Offset: BADWIDTH, 504 } 505 } 506 507 // substArgTypes substitutes the given list of types for 508 // successive occurrences of the "any" placeholder in the 509 // type syntax expression n.Type. 510 // The result of substArgTypes MUST be assigned back to old, e.g. 511 // n.Left = substArgTypes(n.Left, t1, t2) 512 func substArgTypes(old *Node, types ...*Type) *Node { 513 n := *old // make shallow copy 514 515 for _, t := range types { 516 dowidth(t) 517 } 518 n.Type = substAny(n.Type, &types) 519 if len(types) > 0 { 520 Fatalf("substArgTypes: too many argument types") 521 } 522 return &n 523 } 524 525 // substAny walks t, replacing instances of "any" with successive 526 // elements removed from types. It returns the substituted type. 527 func substAny(t *Type, types *[]*Type) *Type { 528 if t == nil { 529 return nil 530 } 531 532 switch t.Etype { 533 default: 534 // Leave the type unchanged. 535 536 case TANY: 537 if len(*types) == 0 { 538 Fatalf("substArgTypes: not enough argument types") 539 } 540 t = (*types)[0] 541 *types = (*types)[1:] 542 543 case TPTR32, TPTR64: 544 elem := substAny(t.Elem(), types) 545 if elem != t.Elem() { 546 t = t.Copy() 547 t.Extra = PtrType{Elem: elem} 548 } 549 550 case TARRAY: 551 elem := substAny(t.Elem(), types) 552 if elem != t.Elem() { 553 t = t.Copy() 554 t.Extra.(*ArrayType).Elem = elem 555 } 556 557 case TSLICE: 558 elem := substAny(t.Elem(), types) 559 if elem != t.Elem() { 560 t = t.Copy() 561 t.Extra = SliceType{Elem: elem} 562 } 563 564 case TCHAN: 565 elem := substAny(t.Elem(), types) 566 if elem != t.Elem() { 567 t = t.Copy() 568 t.Extra.(*ChanType).Elem = elem 569 } 570 571 case TMAP: 572 key := substAny(t.Key(), types) 573 val := substAny(t.Val(), types) 574 if key != t.Key() || val != t.Val() { 575 t = t.Copy() 576 t.Extra.(*MapType).Key = key 577 t.Extra.(*MapType).Val = val 578 } 579 580 case TFUNC: 581 recvs := substAny(t.Recvs(), types) 582 params := substAny(t.Params(), types) 583 results := substAny(t.Results(), types) 584 if recvs != t.Recvs() || params != t.Params() || results != t.Results() { 585 t = t.Copy() 586 t.FuncType().Receiver = recvs 587 t.FuncType().Results = results 588 t.FuncType().Params = params 589 } 590 591 case TSTRUCT: 592 fields := t.FieldSlice() 593 var nfs []*Field 594 for i, f := range fields { 595 nft := substAny(f.Type, types) 596 if nft == f.Type { 597 continue 598 } 599 if nfs == nil { 600 nfs = append([]*Field(nil), fields...) 601 } 602 nfs[i] = f.Copy() 603 nfs[i].Type = nft 604 } 605 if nfs != nil { 606 t = t.Copy() 607 t.SetFields(nfs) 608 } 609 } 610 611 return t 612 } 613 614 // Copy returns a shallow copy of the Type. 615 func (t *Type) Copy() *Type { 616 if t == nil { 617 return nil 618 } 619 nt := *t 620 // copy any *T Extra fields, to avoid aliasing 621 switch t.Etype { 622 case TMAP: 623 x := *t.Extra.(*MapType) 624 nt.Extra = &x 625 case TFORW: 626 x := *t.Extra.(*ForwardType) 627 nt.Extra = &x 628 case TFUNC: 629 x := *t.Extra.(*FuncType) 630 nt.Extra = &x 631 case TSTRUCT: 632 x := *t.Extra.(*StructType) 633 nt.Extra = &x 634 case TINTER: 635 x := *t.Extra.(*InterType) 636 nt.Extra = &x 637 case TCHAN: 638 x := *t.Extra.(*ChanType) 639 nt.Extra = &x 640 case TARRAY: 641 x := *t.Extra.(*ArrayType) 642 nt.Extra = &x 643 } 644 // TODO(mdempsky): Find out why this is necessary and explain. 645 if t.Orig == t { 646 nt.Orig = &nt 647 } 648 return &nt 649 } 650 651 func (f *Field) Copy() *Field { 652 nf := *f 653 return &nf 654 } 655 656 // Iter provides an abstraction for iterating across struct fields and 657 // interface methods. 658 type Iter struct { 659 s []*Field 660 } 661 662 // iterFields returns the first field or method in struct or interface type t 663 // and an Iter value to continue iterating across the rest. 664 func iterFields(t *Type) (*Field, Iter) { 665 return t.Fields().Iter() 666 } 667 668 // Iter returns the first field in fs and an Iter value to continue iterating 669 // across its successor fields. 670 // Deprecated: New code should use Slice instead. 671 func (fs *Fields) Iter() (*Field, Iter) { 672 i := Iter{s: fs.Slice()} 673 f := i.Next() 674 return f, i 675 } 676 677 // Next returns the next field or method, if any. 678 func (i *Iter) Next() *Field { 679 if len(i.s) == 0 { 680 return nil 681 } 682 f := i.s[0] 683 i.s = i.s[1:] 684 return f 685 } 686 687 func (t *Type) wantEtype(et EType) { 688 if t.Etype != et { 689 Fatalf("want %v, but have %v", et, t) 690 } 691 } 692 693 func (t *Type) Recvs() *Type { return t.FuncType().Receiver } 694 func (t *Type) Params() *Type { return t.FuncType().Params } 695 func (t *Type) Results() *Type { return t.FuncType().Results } 696 697 // Recv returns the receiver of function type t, if any. 698 func (t *Type) Recv() *Field { 699 s := t.Recvs() 700 if s.NumFields() == 0 { 701 return nil 702 } 703 return s.Field(0) 704 } 705 706 // recvsParamsResults stores the accessor functions for a function Type's 707 // receiver, parameters, and result parameters, in that order. 708 // It can be used to iterate over all of a function's parameter lists. 709 var recvsParamsResults = [3]func(*Type) *Type{ 710 (*Type).Recvs, (*Type).Params, (*Type).Results, 711 } 712 713 // paramsResults is like recvsParamsResults, but omits receiver parameters. 714 var paramsResults = [2]func(*Type) *Type{ 715 (*Type).Params, (*Type).Results, 716 } 717 718 // Key returns the key type of map type t. 719 func (t *Type) Key() *Type { 720 t.wantEtype(TMAP) 721 return t.Extra.(*MapType).Key 722 } 723 724 // Val returns the value type of map type t. 725 func (t *Type) Val() *Type { 726 t.wantEtype(TMAP) 727 return t.Extra.(*MapType).Val 728 } 729 730 // Elem returns the type of elements of t. 731 // Usable with pointers, channels, arrays, and slices. 732 func (t *Type) Elem() *Type { 733 switch t.Etype { 734 case TPTR32, TPTR64: 735 return t.Extra.(PtrType).Elem 736 case TARRAY: 737 return t.Extra.(*ArrayType).Elem 738 case TSLICE: 739 return t.Extra.(SliceType).Elem 740 case TCHAN: 741 return t.Extra.(*ChanType).Elem 742 } 743 Fatalf("Type.Elem %s", t.Etype) 744 return nil 745 } 746 747 // DDDField returns the slice ... type for TDDDFIELD type t. 748 func (t *Type) DDDField() *Type { 749 t.wantEtype(TDDDFIELD) 750 return t.Extra.(DDDFieldType).T 751 } 752 753 // ChanArgs returns the channel type for TCHANARGS type t. 754 func (t *Type) ChanArgs() *Type { 755 t.wantEtype(TCHANARGS) 756 return t.Extra.(ChanArgsType).T 757 } 758 759 // FuncArgs returns the channel type for TFUNCARGS type t. 760 func (t *Type) FuncArgs() *Type { 761 t.wantEtype(TFUNCARGS) 762 return t.Extra.(FuncArgsType).T 763 } 764 765 // Nname returns the associated function's nname. 766 func (t *Type) Nname() *Node { 767 switch t.Etype { 768 case TFUNC: 769 return t.Extra.(*FuncType).Nname 770 case TINTERMETH: 771 return t.Extra.(InterMethType).Nname 772 } 773 Fatalf("Type.Nname %v %v", t.Etype, t) 774 return nil 775 } 776 777 // Nname sets the associated function's nname. 778 func (t *Type) SetNname(n *Node) { 779 switch t.Etype { 780 case TFUNC: 781 t.Extra.(*FuncType).Nname = n 782 case TINTERMETH: 783 t.Extra = InterMethType{Nname: n} 784 default: 785 Fatalf("Type.SetNname %v %v", t.Etype, t) 786 } 787 } 788 789 // IsFuncArgStruct reports whether t is a struct representing function parameters. 790 func (t *Type) IsFuncArgStruct() bool { 791 return t.Etype == TSTRUCT && t.Extra.(*StructType).Funarg != FunargNone 792 } 793 794 func (t *Type) Methods() *Fields { 795 // TODO(mdempsky): Validate t? 796 return &t.methods 797 } 798 799 func (t *Type) AllMethods() *Fields { 800 // TODO(mdempsky): Validate t? 801 return &t.allMethods 802 } 803 804 func (t *Type) Fields() *Fields { 805 switch t.Etype { 806 case TSTRUCT: 807 return &t.Extra.(*StructType).fields 808 case TINTER: 809 return &t.Extra.(*InterType).fields 810 } 811 Fatalf("Fields: type %v does not have fields", t) 812 return nil 813 } 814 815 // Field returns the i'th field/method of struct/interface type t. 816 func (t *Type) Field(i int) *Field { 817 return t.Fields().Slice()[i] 818 } 819 820 // FieldSlice returns a slice of containing all fields/methods of 821 // struct/interface type t. 822 func (t *Type) FieldSlice() []*Field { 823 return t.Fields().Slice() 824 } 825 826 // SetFields sets struct/interface type t's fields/methods to fields. 827 func (t *Type) SetFields(fields []*Field) { 828 for _, f := range fields { 829 // If type T contains a field F with a go:notinheap 830 // type, then T must also be go:notinheap. Otherwise, 831 // you could heap allocate T and then get a pointer F, 832 // which would be a heap pointer to a go:notinheap 833 // type. 834 if f.Type != nil && f.Type.NotInHeap { 835 t.NotInHeap = true 836 break 837 } 838 } 839 t.Fields().Set(fields) 840 } 841 842 func (t *Type) isDDDArray() bool { 843 if t.Etype != TARRAY { 844 return false 845 } 846 return t.Extra.(*ArrayType).Bound < 0 847 } 848 849 // ArgWidth returns the total aligned argument size for a function. 850 // It includes the receiver, parameters, and results. 851 func (t *Type) ArgWidth() int64 { 852 t.wantEtype(TFUNC) 853 return t.Extra.(*FuncType).Argwid 854 } 855 856 func (t *Type) Size() int64 { 857 dowidth(t) 858 return t.Width 859 } 860 861 func (t *Type) Alignment() int64 { 862 dowidth(t) 863 return int64(t.Align) 864 } 865 866 func (t *Type) SimpleString() string { 867 return t.Etype.String() 868 } 869 870 // Compare compares types for purposes of the SSA back 871 // end, returning an ssa.Cmp (one of CMPlt, CMPeq, CMPgt). 872 // The answers are correct for an optimizer 873 // or code generator, but not necessarily typechecking. 874 // The order chosen is arbitrary, only consistency and division 875 // into equivalence classes (Types that compare CMPeq) matters. 876 func (t *Type) Compare(u ssa.Type) ssa.Cmp { 877 x, ok := u.(*Type) 878 // ssa.CompilerType is smaller than gc.Type 879 // bare pointer equality is easy. 880 if !ok { 881 return ssa.CMPgt 882 } 883 if x == t { 884 return ssa.CMPeq 885 } 886 return t.cmp(x) 887 } 888 889 func cmpForNe(x bool) ssa.Cmp { 890 if x { 891 return ssa.CMPlt 892 } 893 return ssa.CMPgt 894 } 895 896 func (r *Sym) cmpsym(s *Sym) ssa.Cmp { 897 if r == s { 898 return ssa.CMPeq 899 } 900 if r == nil { 901 return ssa.CMPlt 902 } 903 if s == nil { 904 return ssa.CMPgt 905 } 906 // Fast sort, not pretty sort 907 if len(r.Name) != len(s.Name) { 908 return cmpForNe(len(r.Name) < len(s.Name)) 909 } 910 if r.Pkg != s.Pkg { 911 if len(r.Pkg.Prefix) != len(s.Pkg.Prefix) { 912 return cmpForNe(len(r.Pkg.Prefix) < len(s.Pkg.Prefix)) 913 } 914 if r.Pkg.Prefix != s.Pkg.Prefix { 915 return cmpForNe(r.Pkg.Prefix < s.Pkg.Prefix) 916 } 917 } 918 if r.Name != s.Name { 919 return cmpForNe(r.Name < s.Name) 920 } 921 return ssa.CMPeq 922 } 923 924 // cmp compares two *Types t and x, returning ssa.CMPlt, 925 // ssa.CMPeq, ssa.CMPgt as t<x, t==x, t>x, for an arbitrary 926 // and optimizer-centric notion of comparison. 927 func (t *Type) cmp(x *Type) ssa.Cmp { 928 // This follows the structure of eqtype in subr.go 929 // with two exceptions. 930 // 1. Symbols are compared more carefully because a <,=,> result is desired. 931 // 2. Maps are treated specially to avoid endless recursion -- maps 932 // contain an internal data type not expressible in Go source code. 933 if t == x { 934 return ssa.CMPeq 935 } 936 if t == nil { 937 return ssa.CMPlt 938 } 939 if x == nil { 940 return ssa.CMPgt 941 } 942 943 if t.Etype != x.Etype { 944 return cmpForNe(t.Etype < x.Etype) 945 } 946 947 if t.Sym != nil || x.Sym != nil { 948 // Special case: we keep byte and uint8 separate 949 // for error messages. Treat them as equal. 950 switch t.Etype { 951 case TUINT8: 952 if (t == Types[TUINT8] || t == bytetype) && (x == Types[TUINT8] || x == bytetype) { 953 return ssa.CMPeq 954 } 955 956 case TINT32: 957 if (t == Types[runetype.Etype] || t == runetype) && (x == Types[runetype.Etype] || x == runetype) { 958 return ssa.CMPeq 959 } 960 } 961 } 962 963 if c := t.Sym.cmpsym(x.Sym); c != ssa.CMPeq { 964 return c 965 } 966 967 if x.Sym != nil { 968 // Syms non-nil, if vargens match then equal. 969 if t.Vargen != x.Vargen { 970 return cmpForNe(t.Vargen < x.Vargen) 971 } 972 return ssa.CMPeq 973 } 974 // both syms nil, look at structure below. 975 976 switch t.Etype { 977 case TBOOL, TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128, TUNSAFEPTR, TUINTPTR, 978 TINT8, TINT16, TINT32, TINT64, TINT, TUINT8, TUINT16, TUINT32, TUINT64, TUINT: 979 return ssa.CMPeq 980 } 981 982 switch t.Etype { 983 case TMAP: 984 if c := t.Key().cmp(x.Key()); c != ssa.CMPeq { 985 return c 986 } 987 return t.Val().cmp(x.Val()) 988 989 case TPTR32, TPTR64, TSLICE: 990 // No special cases for these, they are handled 991 // by the general code after the switch. 992 993 case TSTRUCT: 994 if t.StructType().Map == nil { 995 if x.StructType().Map != nil { 996 return ssa.CMPlt // nil < non-nil 997 } 998 // to the fallthrough 999 } else if x.StructType().Map == nil { 1000 return ssa.CMPgt // nil > non-nil 1001 } else if t.StructType().Map.MapType().Bucket == t { 1002 // Both have non-nil Map 1003 // Special case for Maps which include a recursive type where the recursion is not broken with a named type 1004 if x.StructType().Map.MapType().Bucket != x { 1005 return ssa.CMPlt // bucket maps are least 1006 } 1007 return t.StructType().Map.cmp(x.StructType().Map) 1008 } else if x.StructType().Map.MapType().Bucket == x { 1009 return ssa.CMPgt // bucket maps are least 1010 } // If t != t.Map.Bucket, fall through to general case 1011 1012 fallthrough 1013 case TINTER: 1014 t1, ti := iterFields(t) 1015 x1, xi := iterFields(x) 1016 for ; t1 != nil && x1 != nil; t1, x1 = ti.Next(), xi.Next() { 1017 if t1.Embedded != x1.Embedded { 1018 return cmpForNe(t1.Embedded < x1.Embedded) 1019 } 1020 if t1.Note != x1.Note { 1021 return cmpForNe(t1.Note < x1.Note) 1022 } 1023 if c := t1.Sym.cmpsym(x1.Sym); c != ssa.CMPeq { 1024 return c 1025 } 1026 if c := t1.Type.cmp(x1.Type); c != ssa.CMPeq { 1027 return c 1028 } 1029 } 1030 if t1 != x1 { 1031 return cmpForNe(t1 == nil) 1032 } 1033 return ssa.CMPeq 1034 1035 case TFUNC: 1036 for _, f := range recvsParamsResults { 1037 // Loop over fields in structs, ignoring argument names. 1038 ta, ia := iterFields(f(t)) 1039 tb, ib := iterFields(f(x)) 1040 for ; ta != nil && tb != nil; ta, tb = ia.Next(), ib.Next() { 1041 if ta.Isddd != tb.Isddd { 1042 return cmpForNe(!ta.Isddd) 1043 } 1044 if c := ta.Type.cmp(tb.Type); c != ssa.CMPeq { 1045 return c 1046 } 1047 } 1048 if ta != tb { 1049 return cmpForNe(ta == nil) 1050 } 1051 } 1052 return ssa.CMPeq 1053 1054 case TARRAY: 1055 if t.NumElem() != x.NumElem() { 1056 return cmpForNe(t.NumElem() < x.NumElem()) 1057 } 1058 1059 case TCHAN: 1060 if t.ChanDir() != x.ChanDir() { 1061 return cmpForNe(t.ChanDir() < x.ChanDir()) 1062 } 1063 1064 default: 1065 e := fmt.Sprintf("Do not know how to compare %v with %v", t, x) 1066 panic(e) 1067 } 1068 1069 // Common element type comparison for TARRAY, TCHAN, TPTR32, TPTR64, and TSLICE. 1070 return t.Elem().cmp(x.Elem()) 1071 } 1072 1073 // IsKind reports whether t is a Type of the specified kind. 1074 func (t *Type) IsKind(et EType) bool { 1075 return t != nil && t.Etype == et 1076 } 1077 1078 func (t *Type) IsBoolean() bool { 1079 return t.Etype == TBOOL 1080 } 1081 1082 var unsignedEType = [...]EType{ 1083 TINT8: TUINT8, 1084 TUINT8: TUINT8, 1085 TINT16: TUINT16, 1086 TUINT16: TUINT16, 1087 TINT32: TUINT32, 1088 TUINT32: TUINT32, 1089 TINT64: TUINT64, 1090 TUINT64: TUINT64, 1091 TINT: TUINT, 1092 TUINT: TUINT, 1093 TUINTPTR: TUINTPTR, 1094 } 1095 1096 // toUnsigned returns the unsigned equivalent of integer type t. 1097 func (t *Type) toUnsigned() *Type { 1098 if !t.IsInteger() { 1099 Fatalf("unsignedType(%v)", t) 1100 } 1101 return Types[unsignedEType[t.Etype]] 1102 } 1103 1104 func (t *Type) IsInteger() bool { 1105 switch t.Etype { 1106 case TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32, TINT64, TUINT64, TINT, TUINT, TUINTPTR: 1107 return true 1108 } 1109 return false 1110 } 1111 1112 func (t *Type) IsSigned() bool { 1113 switch t.Etype { 1114 case TINT8, TINT16, TINT32, TINT64, TINT: 1115 return true 1116 } 1117 return false 1118 } 1119 1120 func (t *Type) IsFloat() bool { 1121 return t.Etype == TFLOAT32 || t.Etype == TFLOAT64 1122 } 1123 1124 func (t *Type) IsComplex() bool { 1125 return t.Etype == TCOMPLEX64 || t.Etype == TCOMPLEX128 1126 } 1127 1128 // IsPtr reports whether t is a regular Go pointer type. 1129 // This does not include unsafe.Pointer. 1130 func (t *Type) IsPtr() bool { 1131 return t.Etype == TPTR32 || t.Etype == TPTR64 1132 } 1133 1134 // IsUnsafePtr reports whether t is an unsafe pointer. 1135 func (t *Type) IsUnsafePtr() bool { 1136 return t.Etype == TUNSAFEPTR 1137 } 1138 1139 // IsPtrShaped reports whether t is represented by a single machine pointer. 1140 // In addition to regular Go pointer types, this includes map, channel, and 1141 // function types and unsafe.Pointer. It does not include array or struct types 1142 // that consist of a single pointer shaped type. 1143 // TODO(mdempsky): Should it? See golang.org/issue/15028. 1144 func (t *Type) IsPtrShaped() bool { 1145 return t.Etype == TPTR32 || t.Etype == TPTR64 || t.Etype == TUNSAFEPTR || 1146 t.Etype == TMAP || t.Etype == TCHAN || t.Etype == TFUNC 1147 } 1148 1149 func (t *Type) IsString() bool { 1150 return t.Etype == TSTRING 1151 } 1152 1153 func (t *Type) IsMap() bool { 1154 return t.Etype == TMAP 1155 } 1156 1157 func (t *Type) IsChan() bool { 1158 return t.Etype == TCHAN 1159 } 1160 1161 func (t *Type) IsSlice() bool { 1162 return t.Etype == TSLICE 1163 } 1164 1165 func (t *Type) IsArray() bool { 1166 return t.Etype == TARRAY 1167 } 1168 1169 func (t *Type) IsStruct() bool { 1170 return t.Etype == TSTRUCT 1171 } 1172 1173 func (t *Type) IsInterface() bool { 1174 return t.Etype == TINTER 1175 } 1176 1177 // IsEmptyInterface reports whether t is an empty interface type. 1178 func (t *Type) IsEmptyInterface() bool { 1179 return t.IsInterface() && t.NumFields() == 0 1180 } 1181 1182 func (t *Type) ElemType() ssa.Type { 1183 // TODO(josharian): If Type ever moves to a shared 1184 // internal package, remove this silly wrapper. 1185 return t.Elem() 1186 } 1187 func (t *Type) PtrTo() ssa.Type { 1188 return ptrto(t) 1189 } 1190 1191 func (t *Type) NumFields() int { 1192 return t.Fields().Len() 1193 } 1194 func (t *Type) FieldType(i int) ssa.Type { 1195 return t.Field(i).Type 1196 } 1197 func (t *Type) FieldOff(i int) int64 { 1198 return t.Field(i).Offset 1199 } 1200 func (t *Type) FieldName(i int) string { 1201 return t.Field(i).Sym.Name 1202 } 1203 1204 func (t *Type) NumElem() int64 { 1205 t.wantEtype(TARRAY) 1206 at := t.Extra.(*ArrayType) 1207 if at.Bound < 0 { 1208 Fatalf("NumElem array %v does not have bound yet", t) 1209 } 1210 return at.Bound 1211 } 1212 1213 // SetNumElem sets the number of elements in an array type. 1214 // The only allowed use is on array types created with typDDDArray. 1215 // For other uses, create a new array with typArray instead. 1216 func (t *Type) SetNumElem(n int64) { 1217 t.wantEtype(TARRAY) 1218 at := t.Extra.(*ArrayType) 1219 if at.Bound >= 0 { 1220 Fatalf("SetNumElem array %v already has bound %d", t, at.Bound) 1221 } 1222 at.Bound = n 1223 } 1224 1225 // ChanDir returns the direction of a channel type t. 1226 // The direction will be one of Crecv, Csend, or Cboth. 1227 func (t *Type) ChanDir() ChanDir { 1228 t.wantEtype(TCHAN) 1229 return t.Extra.(*ChanType).Dir 1230 } 1231 1232 func (t *Type) IsMemory() bool { return false } 1233 func (t *Type) IsFlags() bool { return false } 1234 func (t *Type) IsVoid() bool { return false } 1235 func (t *Type) IsTuple() bool { return false } 1236 1237 // IsUntyped reports whether t is an untyped type. 1238 func (t *Type) IsUntyped() bool { 1239 if t == nil { 1240 return false 1241 } 1242 if t == idealstring || t == idealbool { 1243 return true 1244 } 1245 switch t.Etype { 1246 case TNIL, TIDEAL: 1247 return true 1248 } 1249 return false 1250 } 1251