github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/go/types.h (about) 1 // types.h -- Go frontend types. -*- C++ -*- 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 #ifndef GO_TYPES_H 8 #define GO_TYPES_H 9 10 #include <ostream> 11 12 #include "go-linemap.h" 13 #include "escape.h" 14 15 class Gogo; 16 class Package; 17 class Variable; 18 class Traverse; 19 class Typed_identifier; 20 class Typed_identifier_list; 21 class Integer_type; 22 class Float_type; 23 class Complex_type; 24 class String_type; 25 class Function_type; 26 class Backend_function_type; 27 class Struct_field; 28 class Struct_field_list; 29 class Struct_type; 30 class Pointer_type; 31 class Array_type; 32 class Map_type; 33 class Channel_type; 34 class Interface_type; 35 class Named_type; 36 class Forward_declaration_type; 37 class Method; 38 class Methods; 39 class Type_hash_identical; 40 class Type_identical; 41 class Expression; 42 class Expression_list; 43 class Call_expression; 44 class Field_reference_expression; 45 class Bound_method_expression; 46 class Bindings; 47 class Named_object; 48 class Function; 49 class Translate_context; 50 class Export; 51 class Import; 52 class Btype; 53 class Bexpression; 54 class Bvariable; 55 56 // Type codes used in type descriptors. These must match the values 57 // in libgo/runtime/go-type.h. They also match the values in the gc 58 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go, 59 // although this is not required. 60 61 static const int RUNTIME_TYPE_KIND_BOOL = 1; 62 static const int RUNTIME_TYPE_KIND_INT = 2; 63 static const int RUNTIME_TYPE_KIND_INT8 = 3; 64 static const int RUNTIME_TYPE_KIND_INT16 = 4; 65 static const int RUNTIME_TYPE_KIND_INT32 = 5; 66 static const int RUNTIME_TYPE_KIND_INT64 = 6; 67 static const int RUNTIME_TYPE_KIND_UINT = 7; 68 static const int RUNTIME_TYPE_KIND_UINT8 = 8; 69 static const int RUNTIME_TYPE_KIND_UINT16 = 9; 70 static const int RUNTIME_TYPE_KIND_UINT32 = 10; 71 static const int RUNTIME_TYPE_KIND_UINT64 = 11; 72 static const int RUNTIME_TYPE_KIND_UINTPTR = 12; 73 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13; 74 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14; 75 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15; 76 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16; 77 static const int RUNTIME_TYPE_KIND_ARRAY = 17; 78 static const int RUNTIME_TYPE_KIND_CHAN = 18; 79 static const int RUNTIME_TYPE_KIND_FUNC = 19; 80 static const int RUNTIME_TYPE_KIND_INTERFACE = 20; 81 static const int RUNTIME_TYPE_KIND_MAP = 21; 82 static const int RUNTIME_TYPE_KIND_PTR = 22; 83 static const int RUNTIME_TYPE_KIND_SLICE = 23; 84 static const int RUNTIME_TYPE_KIND_STRING = 24; 85 static const int RUNTIME_TYPE_KIND_STRUCT = 25; 86 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26; 87 88 static const int RUNTIME_TYPE_KIND_DIRECT_IFACE = (1 << 5); 89 static const int RUNTIME_TYPE_KIND_GC_PROG = (1 << 6); 90 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7); 91 92 // To build the complete list of methods for a named type we need to 93 // gather all methods from anonymous fields. Those methods may 94 // require an arbitrary set of indirections and field offsets. There 95 // is also the possibility of ambiguous methods, which we could ignore 96 // except that we want to give a better error message for that case. 97 // This is a base class. There are two types of methods: named 98 // methods, and methods which are inherited from an anonymous field of 99 // interface type. 100 101 class Method 102 { 103 public: 104 // For methods in anonymous types we need to know the sequence of 105 // field references used to extract the pointer to pass to the 106 // method. Since each method for a particular anonymous field will 107 // have the sequence of field indexes, and since the indexes can be 108 // shared going down the chain, we use a manually managed linked 109 // list. The first entry in the list is the field index for the 110 // last field, the one passed to the method. 111 112 struct Field_indexes 113 { 114 const Field_indexes* next; 115 unsigned int field_index; 116 }; 117 118 virtual ~Method() 119 { } 120 121 // Get the list of field indexes. 122 const Field_indexes* 123 field_indexes() const 124 { return this->field_indexes_; } 125 126 // Get the depth. 127 unsigned int 128 depth() const 129 { return this->depth_; } 130 131 // Return whether this is a value method--a method which does not 132 // require a pointer expression. 133 bool 134 is_value_method() const 135 { return this->is_value_method_; } 136 137 // Return whether we need a stub method--this is true if we can't 138 // just pass the main object to the method. 139 bool 140 needs_stub_method() const 141 { return this->needs_stub_method_; } 142 143 // Return whether this is an ambiguous method name. 144 bool 145 is_ambiguous() const 146 { return this->is_ambiguous_; } 147 148 // Note that this method is ambiguous. 149 void 150 set_is_ambiguous() 151 { this->is_ambiguous_ = true; } 152 153 // Return the type of the method. 154 Function_type* 155 type() const 156 { return this->do_type(); } 157 158 // Return the location of the method receiver. 159 Location 160 receiver_location() const 161 { return this->do_receiver_location(); } 162 163 // Return an expression which binds this method to EXPR. This is 164 // something which can be used with a function call. 165 Expression* 166 bind_method(Expression* expr, Location location) const; 167 168 // Return the named object for this method. This may only be called 169 // after methods are finalized. 170 Named_object* 171 named_object() const; 172 173 // Get the stub object. 174 Named_object* 175 stub_object() const 176 { 177 go_assert(this->stub_ != NULL); 178 return this->stub_; 179 } 180 181 // Set the stub object. 182 void 183 set_stub_object(Named_object* no) 184 { 185 go_assert(this->stub_ == NULL); 186 this->stub_ = no; 187 } 188 189 // Get the direct interface method stub object. 190 Named_object* 191 iface_stub_object() const 192 { 193 go_assert(this->iface_stub_ != NULL); 194 return this->iface_stub_; 195 } 196 197 // Set the direct interface method stub object. 198 void 199 set_iface_stub_object(Named_object* no) 200 { 201 go_assert(this->iface_stub_ == NULL); 202 this->iface_stub_ = no; 203 } 204 205 // Return true if this method should not participate in any 206 // interfaces. 207 bool 208 nointerface() const 209 { return this->do_nointerface(); } 210 211 protected: 212 // These objects are only built by the child classes. 213 Method(const Field_indexes* field_indexes, unsigned int depth, 214 bool is_value_method, bool needs_stub_method) 215 : field_indexes_(field_indexes), depth_(depth), stub_(NULL), iface_stub_(NULL), 216 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method), 217 is_ambiguous_(false) 218 { } 219 220 // The named object for this method. 221 virtual Named_object* 222 do_named_object() const = 0; 223 224 // The type of the method. 225 virtual Function_type* 226 do_type() const = 0; 227 228 // Return the location of the method receiver. 229 virtual Location 230 do_receiver_location() const = 0; 231 232 // Bind a method to an object. 233 virtual Expression* 234 do_bind_method(Expression* expr, Location location) const = 0; 235 236 // Return whether this method should not participate in interfaces. 237 virtual bool 238 do_nointerface() const = 0; 239 240 private: 241 // The sequence of field indexes used for this method. If this is 242 // NULL, then the method is defined for the current type. 243 const Field_indexes* field_indexes_; 244 // The depth at which this method was found. 245 unsigned int depth_; 246 // If a stub method is required, this is its object. This is only 247 // set after stub methods are built in finalize_methods. 248 Named_object* stub_; 249 // Stub object for direct interface type. This is only set after 250 // stub methods are built in finalize_methods. 251 Named_object* iface_stub_; 252 // Whether this is a value method--a method that does not require a 253 // pointer. 254 bool is_value_method_; 255 // Whether a stub method is required. 256 bool needs_stub_method_; 257 // Whether this method is ambiguous. 258 bool is_ambiguous_; 259 }; 260 261 // A named method. This is what you get with a method declaration, 262 // either directly on the type, or inherited from some anonymous 263 // embedded field. 264 265 class Named_method : public Method 266 { 267 public: 268 Named_method(Named_object* named_object, const Field_indexes* field_indexes, 269 unsigned int depth, bool is_value_method, 270 bool needs_stub_method) 271 : Method(field_indexes, depth, is_value_method, needs_stub_method), 272 named_object_(named_object) 273 { } 274 275 protected: 276 // Get the Named_object for the method. 277 Named_object* 278 do_named_object() const 279 { return this->named_object_; } 280 281 // The type of the method. 282 Function_type* 283 do_type() const; 284 285 // Return the location of the method receiver. 286 Location 287 do_receiver_location() const; 288 289 // Bind a method to an object. 290 Expression* 291 do_bind_method(Expression* expr, Location location) const; 292 293 // Return whether this method should not participate in interfaces. 294 bool 295 do_nointerface() const; 296 297 private: 298 // The method itself. For a method which needs a stub, this starts 299 // out as the underlying method, and is later replaced with the stub 300 // method. 301 Named_object* named_object_; 302 }; 303 304 // An interface method. This is used when an interface appears as an 305 // anonymous field in a named struct. 306 307 class Interface_method : public Method 308 { 309 public: 310 Interface_method(const std::string& name, Location location, 311 Function_type* fntype, const Field_indexes* field_indexes, 312 unsigned int depth) 313 : Method(field_indexes, depth, true, true), 314 name_(name), location_(location), fntype_(fntype) 315 { } 316 317 protected: 318 // Get the Named_object for the method. This should never be 319 // called, as we always create a stub. 320 Named_object* 321 do_named_object() const 322 { go_unreachable(); } 323 324 // The type of the method. 325 Function_type* 326 do_type() const 327 { return this->fntype_; } 328 329 // Return the location of the method receiver. 330 Location 331 do_receiver_location() const 332 { return this->location_; } 333 334 // Bind a method to an object. 335 Expression* 336 do_bind_method(Expression* expr, Location location) const; 337 338 // Return whether this method should not participate in interfaces. 339 bool 340 do_nointerface() const 341 { return false; } 342 343 private: 344 // The name of the interface method to call. 345 std::string name_; 346 // The location of the definition of the interface method. 347 Location location_; 348 // The type of the interface method. 349 Function_type* fntype_; 350 }; 351 352 // A mapping from method name to Method. This is a wrapper around a 353 // hash table. 354 355 class Methods 356 { 357 private: 358 typedef Unordered_map(std::string, Method*) Method_map; 359 360 public: 361 typedef Method_map::const_iterator const_iterator; 362 363 Methods() 364 : methods_() 365 { } 366 367 // Insert a new method. Returns true if it was inserted, false if 368 // it was overidden or ambiguous. 369 bool 370 insert(const std::string& name, Method* m); 371 372 // The number of (unambiguous) methods. 373 size_t 374 count() const; 375 376 // Iterate. 377 const_iterator 378 begin() const 379 { return this->methods_.begin(); } 380 381 const_iterator 382 end() const 383 { return this->methods_.end(); } 384 385 // Lookup. 386 const_iterator 387 find(const std::string& name) const 388 { return this->methods_.find(name); } 389 390 bool 391 empty() const 392 { return this->methods_.empty(); } 393 394 private: 395 Method_map methods_; 396 }; 397 398 // The base class for all types. 399 400 class Type 401 { 402 public: 403 // The types of types. 404 enum Type_classification 405 { 406 TYPE_ERROR, 407 TYPE_VOID, 408 TYPE_BOOLEAN, 409 TYPE_INTEGER, 410 TYPE_FLOAT, 411 TYPE_COMPLEX, 412 TYPE_STRING, 413 TYPE_SINK, 414 TYPE_FUNCTION, 415 TYPE_POINTER, 416 TYPE_NIL, 417 TYPE_CALL_MULTIPLE_RESULT, 418 TYPE_STRUCT, 419 TYPE_ARRAY, 420 TYPE_MAP, 421 TYPE_CHANNEL, 422 TYPE_INTERFACE, 423 TYPE_NAMED, 424 TYPE_FORWARD 425 }; 426 427 virtual ~Type(); 428 429 // Creators. 430 431 static Type* 432 make_error_type(); 433 434 static Type* 435 make_void_type(); 436 437 // Get the unnamed bool type. 438 static Type* 439 make_boolean_type(); 440 441 // Get the named type "bool". 442 static Named_type* 443 lookup_bool_type(); 444 445 // Make the named type "bool". 446 static Named_type* 447 make_named_bool_type(); 448 449 // Make an abstract integer type. 450 static Integer_type* 451 make_abstract_integer_type(); 452 453 // Make an abstract type for a character constant. 454 static Integer_type* 455 make_abstract_character_type(); 456 457 // Make a named integer type with a specified size. 458 // RUNTIME_TYPE_KIND is the code to use in reflection information, 459 // to distinguish int and int32. 460 static Named_type* 461 make_integer_type(const char* name, bool is_unsigned, int bits, 462 int runtime_type_kind); 463 464 // Look up a named integer type. 465 static Named_type* 466 lookup_integer_type(const char* name); 467 468 // Make an abstract floating point type. 469 static Float_type* 470 make_abstract_float_type(); 471 472 // Make a named floating point type with a specific size. 473 // RUNTIME_TYPE_KIND is the code to use in reflection information, 474 // to distinguish float and float32. 475 static Named_type* 476 make_float_type(const char* name, int bits, int runtime_type_kind); 477 478 // Look up a named float type. 479 static Named_type* 480 lookup_float_type(const char* name); 481 482 // Make an abstract complex type. 483 static Complex_type* 484 make_abstract_complex_type(); 485 486 // Make a named complex type with a specific size. 487 // RUNTIME_TYPE_KIND is the code to use in reflection information, 488 // to distinguish complex and complex64. 489 static Named_type* 490 make_complex_type(const char* name, int bits, int runtime_type_kind); 491 492 // Look up a named complex type. 493 static Named_type* 494 lookup_complex_type(const char* name); 495 496 // Get the unnamed string type. 497 static Type* 498 make_string_type(); 499 500 // Get the named type "string". 501 static Named_type* 502 lookup_string_type(); 503 504 // Make the named type "string". 505 static Named_type* 506 make_named_string_type(); 507 508 static Type* 509 make_sink_type(); 510 511 static Function_type* 512 make_function_type(Typed_identifier* receiver, 513 Typed_identifier_list* parameters, 514 Typed_identifier_list* results, 515 Location); 516 517 static Backend_function_type* 518 make_backend_function_type(Typed_identifier* receiver, 519 Typed_identifier_list* parameters, 520 Typed_identifier_list* results, 521 Location); 522 523 static Pointer_type* 524 make_pointer_type(Type*); 525 526 static void 527 finish_pointer_types(Gogo* gogo); 528 529 static Type* 530 make_nil_type(); 531 532 static Type* 533 make_call_multiple_result_type(Call_expression*); 534 535 static Struct_type* 536 make_struct_type(Struct_field_list* fields, Location); 537 538 static Array_type* 539 make_array_type(Type* element_type, Expression* length); 540 541 static Map_type* 542 make_map_type(Type* key_type, Type* value_type, Location); 543 544 static Channel_type* 545 make_channel_type(bool send, bool receive, Type*); 546 547 static Interface_type* 548 make_interface_type(Typed_identifier_list* methods, Location); 549 550 static Interface_type* 551 make_empty_interface_type(Location); 552 553 static Type* 554 make_type_descriptor_type(); 555 556 static Type* 557 make_type_descriptor_ptr_type(); 558 559 static Named_type* 560 make_named_type(Named_object*, Type*, Location); 561 562 static Type* 563 make_forward_declaration(Named_object*); 564 565 // Make a builtin struct type from a list of fields. 566 static Struct_type* 567 make_builtin_struct_type(int nfields, ...); 568 569 // Make a builtin named type. 570 static Named_type* 571 make_builtin_named_type(const char* name, Type* type); 572 573 // Traverse a type. 574 static int 575 traverse(Type*, Traverse*); 576 577 // Verify the type. This is called after parsing, and verifies that 578 // types are complete and meet the language requirements. This 579 // returns false if the type is invalid and we should not continue 580 // traversing it. 581 bool 582 verify() 583 { return this->do_verify(); } 584 585 // Bit flags to pass to are_identical and friends. 586 587 // Treat error types as their own distinct type. Sometimes we 588 // ignore error types--treat them as identical to every other 589 // type--to avoid cascading errors. 590 static const int COMPARE_ERRORS = 1; 591 592 // Compare struct field tags when comparing structs. We ignore 593 // struct field tags for purposes of type conversion. 594 static const int COMPARE_TAGS = 2; 595 596 // Compare aliases: treat an alias to T as distinct from T. 597 static const int COMPARE_ALIASES = 4; 598 599 // When comparing interface types compare the interface embedding heirarchy, 600 // if any, rather than only comparing method sets. Useful primarily when 601 // exporting types. 602 static const int COMPARE_EMBEDDED_INTERFACES = 8; 603 604 // Return true if two types are identical. If this returns false, 605 // and REASON is not NULL, it may set *REASON. 606 static bool 607 are_identical(const Type* lhs, const Type* rhs, int flags, 608 std::string* reason); 609 610 // Return true if two types are compatible for use in a binary 611 // operation, other than a shift, comparison, or channel send. This 612 // is an equivalence relation. 613 static bool 614 are_compatible_for_binop(const Type* t1, const Type* t2); 615 616 // Return true if two types are compatible for use with the 617 // comparison operator. IS_EQUALITY_OP is true if this is an 618 // equality comparison, false if it is an ordered comparison. This 619 // is an equivalence relation. If this returns false, and REASON is 620 // not NULL, it sets *REASON. 621 static bool 622 are_compatible_for_comparison(bool is_equality_op, const Type *t1, 623 const Type *t2, std::string* reason); 624 625 // Return true if a type is comparable with itself. This is true of 626 // most types, but false for, e.g., function types. 627 bool 628 is_comparable() const 629 { return Type::are_compatible_for_comparison(true, this, this, NULL); } 630 631 // Return true if a value with type RHS is assignable to a variable 632 // with type LHS. This is not an equivalence relation. If this 633 // returns false, and REASON is not NULL, it sets *REASON. 634 static bool 635 are_assignable(const Type* lhs, const Type* rhs, std::string* reason); 636 637 // Return true if a value with type RHS may be converted to type 638 // LHS. If this returns false, and REASON is not NULL, it sets 639 // *REASON. 640 static bool 641 are_convertible(const Type* lhs, const Type* rhs, std::string* reason); 642 643 // Return true if values of this type can be compared using an 644 // identity function which gets nothing but a pointer to the value 645 // and a size. 646 bool 647 compare_is_identity(Gogo* gogo) 648 { return this->do_compare_is_identity(gogo); } 649 650 // Return whether values of this type are reflexive: if a comparison 651 // of a value with itself always returns true. 652 bool 653 is_reflexive() 654 { return this->do_is_reflexive(); } 655 656 // Return whether values of this, when used as a key in map, 657 // requires the key to be updated when an assignment is made. 658 bool 659 needs_key_update() 660 { return this->do_needs_key_update(); } 661 662 // Return whether the hash function of this type might panic. This 663 // is only called for types used as a key in a map type. 664 bool 665 hash_might_panic() 666 { return this->do_hash_might_panic(); } 667 668 // Whether the type is permitted in the heap. 669 bool 670 in_heap() 671 { return this->do_in_heap(); } 672 673 // Return a hash code for this type for the method hash table. 674 // Types which are equivalent according to are_identical will have 675 // the same hash code. 676 unsigned int 677 hash_for_method(Gogo*, int) const; 678 679 // Return the type classification. 680 Type_classification 681 classification() const 682 { return this->classification_; } 683 684 // Return the base type for this type. This looks through forward 685 // declarations and names. Using this with a forward declaration 686 // which has not been defined will return an error type. 687 Type* 688 base(); 689 690 const Type* 691 base() const; 692 693 // Return the type skipping defined forward declarations. If this 694 // type is a forward declaration which has not been defined, it will 695 // return the Forward_declaration_type. This differs from base() in 696 // that it will return a Named_type, and for a 697 // Forward_declaration_type which is not defined it will return that 698 // type rather than an error type. 699 Type* 700 forwarded(); 701 702 const Type* 703 forwarded() const; 704 705 // Return the type skipping any alias definitions and any defined 706 // forward declarations. This is like forwarded, but also 707 // recursively expands alias definitions to the aliased type. 708 Type* 709 unalias(); 710 711 const Type* 712 unalias() const; 713 714 // Return true if this is a basic type: a type which is not composed 715 // of other types, and is not void. 716 bool 717 is_basic_type() const; 718 719 // Return true if this is an abstract type--an integer, floating 720 // point, or complex type whose size has not been determined. 721 bool 722 is_abstract() const; 723 724 // Return a non-abstract version of an abstract type. 725 Type* 726 make_non_abstract_type(); 727 728 // Return true if this type is or contains a pointer. This 729 // determines whether the garbage collector needs to look at a value 730 // of this type. 731 bool 732 has_pointer() const 733 { return this->do_has_pointer(); } 734 735 // Return true if this is the error type. This returns false for a 736 // type which is not defined, as it is called by the parser before 737 // all types are defined. 738 bool 739 is_error_type() const; 740 741 // Return true if this is the error type or if the type is 742 // undefined. If the type is undefined, this will give an error. 743 // This should only be called after parsing is complete. 744 bool 745 is_error() const 746 { return this->base()->is_error_type(); } 747 748 // Return true if this is a void type. 749 bool 750 is_void_type() const 751 { return this->classification_ == TYPE_VOID; } 752 753 // If this is an integer type, return the Integer_type. Otherwise, 754 // return NULL. This is a controlled dynamic_cast. 755 Integer_type* 756 integer_type() 757 { return this->convert<Integer_type, TYPE_INTEGER>(); } 758 759 const Integer_type* 760 integer_type() const 761 { return this->convert<const Integer_type, TYPE_INTEGER>(); } 762 763 // If this is a floating point type, return the Float_type. 764 // Otherwise, return NULL. This is a controlled dynamic_cast. 765 Float_type* 766 float_type() 767 { return this->convert<Float_type, TYPE_FLOAT>(); } 768 769 const Float_type* 770 float_type() const 771 { return this->convert<const Float_type, TYPE_FLOAT>(); } 772 773 // If this is a complex type, return the Complex_type. Otherwise, 774 // return NULL. 775 Complex_type* 776 complex_type() 777 { return this->convert<Complex_type, TYPE_COMPLEX>(); } 778 779 const Complex_type* 780 complex_type() const 781 { return this->convert<const Complex_type, TYPE_COMPLEX>(); } 782 783 // Return whether this is a numeric type. 784 bool 785 is_numeric_type() const 786 { 787 Type_classification tc = this->base()->classification_; 788 return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX; 789 } 790 791 // Return true if this is a boolean type. 792 bool 793 is_boolean_type() const 794 { return this->base()->classification_ == TYPE_BOOLEAN; } 795 796 // Return true if this is an abstract boolean type. 797 bool 798 is_abstract_boolean_type() const 799 { return this->classification_ == TYPE_BOOLEAN; } 800 801 // Return true if this is a string type. 802 bool 803 is_string_type() const 804 { return this->base()->classification_ == TYPE_STRING; } 805 806 // Return true if this is an abstract string type. 807 bool 808 is_abstract_string_type() const 809 { return this->classification_ == TYPE_STRING; } 810 811 // Return true if this is the sink type. This is the type of the 812 // blank identifier _. 813 bool 814 is_sink_type() const 815 { return this->base()->classification_ == TYPE_SINK; } 816 817 // If this is a function type, return it. Otherwise, return NULL. 818 Function_type* 819 function_type() 820 { return this->convert<Function_type, TYPE_FUNCTION>(); } 821 822 const Function_type* 823 function_type() const 824 { return this->convert<const Function_type, TYPE_FUNCTION>(); } 825 826 // If this is a pointer type, return the type to which it points. 827 // Otherwise, return NULL. 828 Type* 829 points_to() const; 830 831 // If this is a pointer type, return the type to which it points. 832 // Otherwise, return the type itself. 833 Type* 834 deref() 835 { 836 Type* pt = this->points_to(); 837 return pt != NULL ? pt : this; 838 } 839 840 const Type* 841 deref() const 842 { 843 const Type* pt = this->points_to(); 844 return pt != NULL ? pt : this; 845 } 846 847 // Return true if this is the nil type. We don't use base() here, 848 // because this can be called during parse, and there is no way to 849 // name the nil type anyhow. 850 bool 851 is_nil_type() const 852 { return this->classification_ == TYPE_NIL; } 853 854 // Return true if this is the predeclared constant nil being used as 855 // a type. This is what the parser produces for type switches which 856 // use "case nil". 857 bool 858 is_nil_constant_as_type() const; 859 860 // Return true if this is the return type of a function which 861 // returns multiple values. 862 bool 863 is_call_multiple_result_type() const 864 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; } 865 866 // If this is a struct type, return it. Otherwise, return NULL. 867 Struct_type* 868 struct_type() 869 { return this->convert<Struct_type, TYPE_STRUCT>(); } 870 871 const Struct_type* 872 struct_type() const 873 { return this->convert<const Struct_type, TYPE_STRUCT>(); } 874 875 // If this is an array type, return it. Otherwise, return NULL. 876 Array_type* 877 array_type() 878 { return this->convert<Array_type, TYPE_ARRAY>(); } 879 880 const Array_type* 881 array_type() const 882 { return this->convert<const Array_type, TYPE_ARRAY>(); } 883 884 // Return whether if this is a slice type. 885 bool 886 is_slice_type() const; 887 888 // If this is a map type, return it. Otherwise, return NULL. 889 Map_type* 890 map_type() 891 { return this->convert<Map_type, TYPE_MAP>(); } 892 893 const Map_type* 894 map_type() const 895 { return this->convert<const Map_type, TYPE_MAP>(); } 896 897 // If this is a channel type, return it. Otherwise, return NULL. 898 Channel_type* 899 channel_type() 900 { return this->convert<Channel_type, TYPE_CHANNEL>(); } 901 902 const Channel_type* 903 channel_type() const 904 { return this->convert<const Channel_type, TYPE_CHANNEL>(); } 905 906 // If this is an interface type, return it. Otherwise, return NULL. 907 Interface_type* 908 interface_type() 909 { return this->convert<Interface_type, TYPE_INTERFACE>(); } 910 911 const Interface_type* 912 interface_type() const 913 { return this->convert<const Interface_type, TYPE_INTERFACE>(); } 914 915 // If this is a named type, return it. Otherwise, return NULL. 916 Named_type* 917 named_type(); 918 919 const Named_type* 920 named_type() const; 921 922 // If this is a forward declaration, return it. Otherwise, return 923 // NULL. 924 Forward_declaration_type* 925 forward_declaration_type() 926 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); } 927 928 const Forward_declaration_type* 929 forward_declaration_type() const 930 { 931 return this->convert_no_base<const Forward_declaration_type, 932 TYPE_FORWARD>(); 933 } 934 935 // Return true if this type is not yet defined. 936 bool 937 is_undefined() const; 938 939 // Return true if this is the unsafe.pointer type. We currently 940 // represent that as pointer-to-void. 941 bool 942 is_unsafe_pointer_type() const 943 { return this->points_to() != NULL && this->points_to()->is_void_type(); } 944 945 // Return whether this type is stored directly in an interface's 946 // data word. 947 bool 948 is_direct_iface_type() const; 949 950 // Return a version of this type with any expressions copied, but 951 // only if copying the expressions will affect the size of the type. 952 // If there are no such expressions in the type (expressions can 953 // only occur in array types), just return the same type. If any 954 // expressions can not affect the size of the type, just return the 955 // same type. 956 Type* 957 copy_expressions(); 958 959 // Look for field or method NAME for TYPE. Return an expression for 960 // it, bound to EXPR. 961 static Expression* 962 bind_field_or_method(Gogo*, const Type* type, Expression* expr, 963 const std::string& name, Location); 964 965 // Return true if NAME is an unexported field or method of TYPE. 966 static bool 967 is_unexported_field_or_method(Gogo*, const Type*, const std::string&, 968 std::vector<const Named_type*>*); 969 970 // Convert the builtin named types. 971 static void 972 convert_builtin_named_types(Gogo*); 973 974 // Return the backend representation of this type. 975 Btype* 976 get_backend(Gogo*); 977 978 // Return a placeholder for the backend representation of the type. 979 // This will return a type of the correct size, but for which some 980 // of the fields may still need to be completed. 981 Btype* 982 get_backend_placeholder(Gogo*); 983 984 // Finish the backend representation of a placeholder. 985 void 986 finish_backend(Gogo*, Btype*); 987 988 // Build a type descriptor entry for this type. Return a pointer to 989 // it. The location is the location which causes us to need the 990 // entry. 991 Bexpression* 992 type_descriptor_pointer(Gogo* gogo, Location); 993 994 // Build the Garbage Collection symbol for this type. Return a pointer to it. 995 Bexpression* 996 gc_symbol_pointer(Gogo* gogo); 997 998 // Return whether this type needs a garbage collection program. 999 // Sets *PTRSIZE and *PTRDATA. 1000 bool 1001 needs_gcprog(Gogo*, int64_t* ptrsize, int64_t* ptrdata); 1002 1003 // Return a ptrmask variable for this type. 1004 Bvariable* 1005 gc_ptrmask_var(Gogo*, int64_t ptrsize, int64_t ptrdata); 1006 1007 // Return the type reflection string for this type. 1008 std::string 1009 reflection(Gogo*) const; 1010 1011 // Return a mangled name for the type. This is a name which can be 1012 // used in assembler code. Identical types should have the same 1013 // manged name. 1014 std::string 1015 mangled_name(Gogo*) const; 1016 1017 // If the size of the type can be determined, set *PSIZE to the size 1018 // in bytes and return true. Otherwise, return false. This queries 1019 // the backend. 1020 bool 1021 backend_type_size(Gogo*, int64_t* psize); 1022 1023 // If the alignment of the type can be determined, set *PALIGN to 1024 // the alignment in bytes and return true. Otherwise, return false. 1025 bool 1026 backend_type_align(Gogo*, int64_t* palign); 1027 1028 // If the alignment of a struct field of this type can be 1029 // determined, set *PALIGN to the alignment in bytes and return 1030 // true. Otherwise, return false. 1031 bool 1032 backend_type_field_align(Gogo*, int64_t* palign); 1033 1034 // Determine the ptrdata size for the backend version of this type: 1035 // the length of the prefix of the type that can contain a pointer 1036 // value. If it can be determined, set *PPTRDATA to the value in 1037 // bytes and return true. Otherwise, return false. 1038 bool 1039 backend_type_ptrdata(Gogo*, int64_t* pptrdata); 1040 1041 // Determine the ptrdata size that we are going to set in the type 1042 // descriptor. This is normally the same as backend_type_ptrdata, 1043 // but differs if we use a gcprog for an array. The arguments and 1044 // results are as for backend_type_ptrdata. 1045 bool 1046 descriptor_ptrdata(Gogo*, int64_t* pptrdata); 1047 1048 // Whether the backend size is known. 1049 bool 1050 is_backend_type_size_known(Gogo*); 1051 1052 // Return whether the type needs specially built type functions. 1053 bool 1054 needs_specific_type_functions(Gogo*); 1055 1056 // Get the equality function for a type. Returns NULL if the type 1057 // is not comparable. 1058 Named_object* 1059 equal_function(Gogo*, Named_type* name, Function_type* equal_fntype); 1060 1061 // Get the hash function for a type. Returns NULL if the type is 1062 // not comparable. 1063 Named_object* 1064 hash_function(Gogo*, Function_type* hash_fntype); 1065 1066 // Write the equal function for a type. 1067 void 1068 write_equal_function(Gogo*, Named_type*, int64_t size, 1069 const std::string& equal_name, 1070 Function_type* equal_fntype); 1071 1072 // Write the hash function for a type. 1073 void 1074 write_hash_function(Gogo*, int64_t size, const std::string& hash_name, 1075 Function_type* hash_fntype); 1076 1077 // Return the alignment required by the memequalN function. 1078 static int64_t memequal_align(Gogo*, int size); 1079 1080 // Export the type. 1081 void 1082 export_type(Export* exp) const 1083 { this->do_export(exp); } 1084 1085 // Import a type. 1086 static Type* 1087 import_type(Import*); 1088 1089 protected: 1090 Type(Type_classification); 1091 1092 // Functions implemented by the child class. 1093 1094 // Traverse the subtypes. 1095 virtual int 1096 do_traverse(Traverse*); 1097 1098 // Verify the type. 1099 virtual bool 1100 do_verify() 1101 { return true; } 1102 1103 virtual bool 1104 do_has_pointer() const 1105 { return false; } 1106 1107 virtual bool 1108 do_compare_is_identity(Gogo*) = 0; 1109 1110 virtual bool 1111 do_is_reflexive() 1112 { return true; } 1113 1114 virtual bool 1115 do_needs_key_update() 1116 { return false; } 1117 1118 virtual bool 1119 do_hash_might_panic() 1120 { return false; } 1121 1122 virtual bool 1123 do_in_heap() 1124 { return true; } 1125 1126 virtual unsigned int 1127 do_hash_for_method(Gogo*, int) const; 1128 1129 virtual Btype* 1130 do_get_backend(Gogo*) = 0; 1131 1132 virtual Expression* 1133 do_type_descriptor(Gogo*, Named_type* name) = 0; 1134 1135 virtual void 1136 do_reflection(Gogo*, std::string*) const = 0; 1137 1138 virtual void 1139 do_mangled_name(Gogo*, std::string*) const = 0; 1140 1141 virtual void 1142 do_export(Export*) const; 1143 1144 // Return whether a method expects a pointer as the receiver. 1145 static bool 1146 method_expects_pointer(const Named_object*); 1147 1148 // Finalize the methods for a type. 1149 static void 1150 finalize_methods(Gogo*, const Type*, Location, Methods**); 1151 1152 // Return a method from a set of methods. 1153 static Method* 1154 method_function(const Methods*, const std::string& name, 1155 bool* is_ambiguous); 1156 1157 // A mapping from interfaces to the associated interface method 1158 // tables for this type. This maps to a decl. 1159 typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical, 1160 Type_identical) Interface_method_tables; 1161 1162 // Return a pointer to the interface method table for TYPE for the 1163 // interface INTERFACE. 1164 static Expression* 1165 interface_method_table(Type* type, 1166 Interface_type *interface, bool is_pointer, 1167 Interface_method_tables** method_tables, 1168 Interface_method_tables** pointer_tables); 1169 1170 // Return a composite literal for the type descriptor entry for a 1171 // type. 1172 static Expression* 1173 type_descriptor(Gogo*, Type*); 1174 1175 // Return a composite literal for the type descriptor entry for 1176 // TYPE, using NAME as the name of the type. 1177 static Expression* 1178 named_type_descriptor(Gogo*, Type* type, Named_type* name); 1179 1180 // Return a composite literal for a plain type descriptor for this 1181 // type with the given kind and name. 1182 Expression* 1183 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name); 1184 1185 // Build a composite literal for the basic type descriptor. 1186 Expression* 1187 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*, 1188 const Methods*, bool only_value_methods); 1189 1190 // For the benefit of child class reflection string generation. 1191 void 1192 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const 1193 { type->do_reflection(gogo, ret); } 1194 1195 // For the benefit of child class mangling. 1196 void 1197 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const 1198 { type->do_mangled_name(gogo, ret); } 1199 1200 // Return the backend representation for the underlying type of a 1201 // named type. 1202 static Btype* 1203 get_named_base_btype(Gogo* gogo, Type* base_type) 1204 { return base_type->get_btype_without_hash(gogo); } 1205 1206 private: 1207 // Convert to the desired type classification, or return NULL. This 1208 // is a controlled dynamic_cast. 1209 template<typename Type_class, Type_classification type_classification> 1210 Type_class* 1211 convert() 1212 { 1213 Type* base = this->base(); 1214 return (base->classification_ == type_classification 1215 ? static_cast<Type_class*>(base) 1216 : NULL); 1217 } 1218 1219 template<typename Type_class, Type_classification type_classification> 1220 const Type_class* 1221 convert() const 1222 { 1223 const Type* base = this->base(); 1224 return (base->classification_ == type_classification 1225 ? static_cast<Type_class*>(base) 1226 : NULL); 1227 } 1228 1229 template<typename Type_class, Type_classification type_classification> 1230 Type_class* 1231 convert_no_base() 1232 { 1233 return (this->classification_ == type_classification 1234 ? static_cast<Type_class*>(this) 1235 : NULL); 1236 } 1237 1238 template<typename Type_class, Type_classification type_classification> 1239 const Type_class* 1240 convert_no_base() const 1241 { 1242 return (this->classification_ == type_classification 1243 ? static_cast<Type_class*>(this) 1244 : NULL); 1245 } 1246 1247 // Map unnamed types to type descriptor decls. 1248 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical, 1249 Type_identical) Type_descriptor_vars; 1250 1251 static Type_descriptor_vars type_descriptor_vars; 1252 1253 // Build the type descriptor variable for this type. 1254 void 1255 make_type_descriptor_var(Gogo*); 1256 1257 // Map unnamed types to type descriptor decls. 1258 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical, 1259 Type_identical) GC_symbol_vars; 1260 1261 static GC_symbol_vars gc_symbol_vars; 1262 1263 // Map ptrmask symbol names to the ptrmask variable. 1264 typedef Unordered_map(std::string, Bvariable*) GC_gcbits_vars; 1265 1266 static GC_gcbits_vars gc_gcbits_vars; 1267 1268 // Build the GC symbol for this type. 1269 void 1270 make_gc_symbol_var(Gogo*); 1271 1272 // Return true if the type descriptor for this type should be 1273 // defined in some other package. If NAME is not NULL, it is the 1274 // name of this type. If this returns true it sets *PACKAGE to the 1275 // package where the type descriptor is defined. 1276 bool 1277 type_descriptor_defined_elsewhere(Named_type* name, const Package** package); 1278 1279 // Make a composite literal for the garbage collection program for 1280 // this type. 1281 Expression* 1282 gcprog_constructor(Gogo*, int64_t ptrsize, int64_t ptrdata); 1283 1284 // Build the hash function for a type that needs specific functions. 1285 Named_object* 1286 build_hash_function(Gogo*, int64_t size, Function_type* hash_fntype); 1287 1288 // Build the equal function for a type that needs specific functions. 1289 Named_object* 1290 build_equal_function(Gogo*, Named_type*, int64_t size, 1291 Function_type* equal_fntype); 1292 1293 void 1294 write_identity_hash(Gogo*, int64_t size); 1295 1296 void 1297 write_identity_equal(Gogo*, int64_t size); 1298 1299 void 1300 write_named_equal(Gogo*, Named_type*); 1301 1302 // Build a composite literal for the uncommon type information. 1303 Expression* 1304 uncommon_type_constructor(Gogo*, Type* uncommon_type, 1305 Named_type*, const Methods*, 1306 bool only_value_methods) const; 1307 1308 // Build a composite literal for the methods. 1309 Expression* 1310 methods_constructor(Gogo*, Type* methods_type, const Methods*, 1311 bool only_value_methods) const; 1312 1313 // Build a composite literal for one method. 1314 Expression* 1315 method_constructor(Gogo*, Type* method_type, const std::string& name, 1316 const Method*, bool only_value_methods) const; 1317 1318 // Add all methods for TYPE to the list of methods for THIS. 1319 static void 1320 add_methods_for_type(const Type* type, const Method::Field_indexes*, 1321 unsigned int depth, bool, bool, 1322 std::vector<const Named_type*>*, 1323 Methods*); 1324 1325 static void 1326 add_local_methods_for_type(const Named_type* type, 1327 const Method::Field_indexes*, 1328 unsigned int depth, bool, bool, Methods*); 1329 1330 static void 1331 add_embedded_methods_for_type(const Type* type, 1332 const Method::Field_indexes*, 1333 unsigned int depth, bool, bool, 1334 std::vector<const Named_type*>*, 1335 Methods*); 1336 1337 static void 1338 add_interface_methods_for_type(const Type* type, 1339 const Method::Field_indexes*, 1340 unsigned int depth, Methods*); 1341 1342 // Build stub methods for a type. 1343 static void 1344 build_stub_methods(Gogo*, const Type* type, const Methods* methods, 1345 Location); 1346 1347 static void 1348 build_one_stub_method(Gogo*, Method*, const char* receiver_name, 1349 const Typed_identifier_list*, bool is_varargs, 1350 Location); 1351 1352 // Build direct interface stub methods for a type. 1353 static void 1354 build_direct_iface_stub_methods(Gogo*, const Type*, Methods*, Location); 1355 1356 static void 1357 build_one_iface_stub_method(Gogo*, Method*, const char*, 1358 const Typed_identifier_list*, 1359 bool, Location); 1360 1361 static Expression* 1362 apply_field_indexes(Expression*, const Method::Field_indexes*, 1363 Location); 1364 1365 // Look for a field or method named NAME in TYPE. 1366 static bool 1367 find_field_or_method(const Type* type, const std::string& name, 1368 bool receiver_can_be_pointer, 1369 std::vector<const Named_type*>*, int* level, 1370 bool* is_method, bool* found_pointer_method, 1371 std::string* ambig1, std::string* ambig2); 1372 1373 // Helper function for is_direct_iface_type, to prevent infinite 1374 // recursion. 1375 bool 1376 is_direct_iface_type_helper(Unordered_set(const Type*)*) const; 1377 1378 // Get the backend representation for a type without looking in the 1379 // hash table for identical types. 1380 Btype* 1381 get_btype_without_hash(Gogo*); 1382 1383 // A backend type that may be a placeholder. 1384 struct Type_btype_entry 1385 { 1386 Btype *btype; 1387 bool is_placeholder; 1388 }; 1389 1390 // A mapping from Type to Btype*, used to ensure that the backend 1391 // representation of identical types is identical. This is only 1392 // used for unnamed types. 1393 typedef Unordered_map_hash(const Type*, Type_btype_entry, 1394 Type_hash_identical, Type_identical) Type_btypes; 1395 1396 static Type_btypes type_btypes; 1397 1398 // A list of builtin named types. 1399 static std::vector<Named_type*> named_builtin_types; 1400 1401 // A map from types that need a specific hash or equality function 1402 // to the hash or equality function. 1403 typedef Unordered_map_hash(const Type*, Named_object*, Type_hash_identical, 1404 Type_identical) Type_function; 1405 1406 static Type_function type_hash_functions_table; 1407 static Type_function type_equal_functions_table; 1408 1409 // Cache for reusing existing pointer types; maps from pointed-to-type 1410 // to pointer type. 1411 typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table; 1412 1413 static Pointer_type_table pointer_types; 1414 1415 // List of placeholder pointer types. 1416 static std::vector<Type*> placeholder_pointers; 1417 1418 // The type classification. 1419 Type_classification classification_; 1420 // The backend representation of the type, once it has been 1421 // determined. 1422 Btype* btype_; 1423 // The type descriptor for this type. This starts out as NULL and 1424 // is filled in as needed. 1425 Bvariable* type_descriptor_var_; 1426 // The GC symbol for this type. This starts out as NULL and 1427 // is filled in as needed. 1428 Bvariable* gc_symbol_var_; 1429 // Whether this type can appear in the heap. 1430 bool in_heap_; 1431 }; 1432 1433 // Type hash table operations, treating aliases as identical to the 1434 // types that they alias. 1435 1436 class Type_hash_identical 1437 { 1438 public: 1439 unsigned int 1440 operator()(const Type* type) const 1441 { 1442 return type->hash_for_method(NULL, 1443 Type::COMPARE_ERRORS | Type::COMPARE_TAGS); 1444 } 1445 }; 1446 1447 class Type_identical 1448 { 1449 public: 1450 bool 1451 operator()(const Type* t1, const Type* t2) const 1452 { 1453 return Type::are_identical(t1, t2, 1454 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 1455 NULL); 1456 } 1457 }; 1458 1459 // An identifier with a type. 1460 1461 class Typed_identifier 1462 { 1463 public: 1464 Typed_identifier(const std::string& name, Type* type, 1465 Location location) 1466 : name_(name), type_(type), location_(location), note_(NULL) 1467 { } 1468 1469 // Get the name. 1470 const std::string& 1471 name() const 1472 { return this->name_; } 1473 1474 // Get the type. 1475 Type* 1476 type() const 1477 { return this->type_; } 1478 1479 // Return the location where the name was seen. This is not always 1480 // meaningful. 1481 Location 1482 location() const 1483 { return this->location_; } 1484 1485 // Set the type--sometimes we see the identifier before the type. 1486 void 1487 set_type(Type* type) 1488 { 1489 go_assert(this->type_ == NULL || type->is_error_type()); 1490 this->type_ = type; 1491 } 1492 1493 // Get the escape note. 1494 std::string* 1495 note() const 1496 { return this->note_; } 1497 1498 // Set the escape note. 1499 void 1500 set_note(const std::string& note) 1501 { 1502 if (this->note_ != NULL) 1503 go_assert(*this->note_ == note); 1504 else 1505 this->note_ = new std::string(note); 1506 } 1507 1508 private: 1509 // Identifier name. 1510 std::string name_; 1511 // Type. 1512 Type* type_; 1513 // The location where the name was seen. 1514 Location location_; 1515 // Escape note for this typed identifier. Used when importing and exporting 1516 // functions. 1517 std::string* note_; 1518 }; 1519 1520 // A list of Typed_identifiers. 1521 1522 class Typed_identifier_list 1523 { 1524 public: 1525 Typed_identifier_list() 1526 : entries_() 1527 { } 1528 1529 // Whether the list is empty. 1530 bool 1531 empty() const 1532 { return this->entries_.empty(); } 1533 1534 // Return the number of entries in the list. 1535 size_t 1536 size() const 1537 { return this->entries_.size(); } 1538 1539 // Add an entry to the end of the list. 1540 void 1541 push_back(const Typed_identifier& td) 1542 { this->entries_.push_back(td); } 1543 1544 // Remove an entry from the end of the list. 1545 void 1546 pop_back() 1547 { this->entries_.pop_back(); } 1548 1549 // Set the type of entry I to TYPE. 1550 void 1551 set_type(size_t i, Type* type) 1552 { 1553 go_assert(i < this->entries_.size()); 1554 this->entries_[i].set_type(type); 1555 } 1556 1557 // Sort the entries by name. 1558 void 1559 sort_by_name(); 1560 1561 // Traverse types. 1562 int 1563 traverse(Traverse*) const; 1564 1565 // Return the first and last elements. 1566 Typed_identifier& 1567 front() 1568 { return this->entries_.front(); } 1569 1570 const Typed_identifier& 1571 front() const 1572 { return this->entries_.front(); } 1573 1574 Typed_identifier& 1575 back() 1576 { return this->entries_.back(); } 1577 1578 const Typed_identifier& 1579 back() const 1580 { return this->entries_.back(); } 1581 1582 Typed_identifier& 1583 at(size_t i) 1584 { return this->entries_.at(i); } 1585 1586 const Typed_identifier& 1587 at(size_t i) const 1588 { return this->entries_.at(i); } 1589 1590 void 1591 set(size_t i, const Typed_identifier& t) 1592 { this->entries_.at(i) = t; } 1593 1594 void 1595 resize(size_t c) 1596 { 1597 go_assert(c <= this->entries_.size()); 1598 this->entries_.resize(c, Typed_identifier("", NULL, 1599 Linemap::unknown_location())); 1600 } 1601 1602 void 1603 reserve(size_t c) 1604 { this->entries_.reserve(c); } 1605 1606 // Iterators. 1607 1608 typedef std::vector<Typed_identifier>::iterator iterator; 1609 typedef std::vector<Typed_identifier>::const_iterator const_iterator; 1610 1611 iterator 1612 begin() 1613 { return this->entries_.begin(); } 1614 1615 const_iterator 1616 begin() const 1617 { return this->entries_.begin(); } 1618 1619 iterator 1620 end() 1621 { return this->entries_.end(); } 1622 1623 const_iterator 1624 end() const 1625 { return this->entries_.end(); } 1626 1627 // Return a copy of this list. This returns an independent copy of 1628 // the vector, but does not copy the types. 1629 Typed_identifier_list* 1630 copy() const; 1631 1632 private: 1633 std::vector<Typed_identifier> entries_; 1634 }; 1635 1636 // A type used to indicate a parsing error. This exists to simplify 1637 // later error detection. 1638 1639 class Error_type : public Type 1640 { 1641 public: 1642 Error_type() 1643 : Type(TYPE_ERROR) 1644 { } 1645 1646 protected: 1647 bool 1648 do_compare_is_identity(Gogo*) 1649 { return false; } 1650 1651 Btype* 1652 do_get_backend(Gogo* gogo); 1653 1654 Expression* 1655 do_type_descriptor(Gogo*, Named_type*); 1656 1657 void 1658 do_reflection(Gogo*, std::string*) const; 1659 1660 void 1661 do_mangled_name(Gogo*, std::string* ret) const; 1662 }; 1663 1664 // The void type. 1665 1666 class Void_type : public Type 1667 { 1668 public: 1669 Void_type() 1670 : Type(TYPE_VOID) 1671 { } 1672 1673 protected: 1674 bool 1675 do_compare_is_identity(Gogo*) 1676 { return false; } 1677 1678 Btype* 1679 do_get_backend(Gogo* gogo); 1680 1681 Expression* 1682 do_type_descriptor(Gogo*, Named_type*) 1683 { go_unreachable(); } 1684 1685 void 1686 do_reflection(Gogo*, std::string*) const 1687 { } 1688 1689 void 1690 do_mangled_name(Gogo*, std::string* ret) const; 1691 }; 1692 1693 // The boolean type. 1694 1695 class Boolean_type : public Type 1696 { 1697 public: 1698 Boolean_type() 1699 : Type(TYPE_BOOLEAN) 1700 { } 1701 1702 protected: 1703 bool 1704 do_compare_is_identity(Gogo*) 1705 { return true; } 1706 1707 Btype* 1708 do_get_backend(Gogo* gogo); 1709 1710 Expression* 1711 do_type_descriptor(Gogo*, Named_type* name); 1712 1713 // We should not be asked for the reflection string of a basic type. 1714 void 1715 do_reflection(Gogo*, std::string* ret) const 1716 { ret->append("bool"); } 1717 1718 void 1719 do_mangled_name(Gogo*, std::string* ret) const; 1720 }; 1721 1722 // The type of an integer. 1723 1724 class Integer_type : public Type 1725 { 1726 public: 1727 // Create a new integer type. 1728 static Named_type* 1729 create_integer_type(const char* name, bool is_unsigned, int bits, 1730 int runtime_type_kind); 1731 1732 // Look up an existing integer type. 1733 static Named_type* 1734 lookup_integer_type(const char* name); 1735 1736 // Create an abstract integer type. 1737 static Integer_type* 1738 create_abstract_integer_type(); 1739 1740 // Create an abstract character type. 1741 static Integer_type* 1742 create_abstract_character_type(); 1743 1744 // Whether this is an abstract integer type. 1745 bool 1746 is_abstract() const 1747 { return this->is_abstract_; } 1748 1749 // Whether this is an unsigned type. 1750 bool 1751 is_unsigned() const 1752 { return this->is_unsigned_; } 1753 1754 // The number of bits. 1755 int 1756 bits() const 1757 { return this->bits_; } 1758 1759 // Whether this type is the same as T. 1760 bool 1761 is_identical(const Integer_type* t) const; 1762 1763 // Whether this is the type "byte" or another name for "byte". 1764 bool 1765 is_byte() const 1766 { return this->is_byte_; } 1767 1768 // Mark this as the "byte" type. 1769 void 1770 set_is_byte() 1771 { this->is_byte_ = true; } 1772 1773 // Whether this is the type "rune" or another name for "rune". 1774 bool 1775 is_rune() const 1776 { return this->is_rune_; } 1777 1778 // Mark this as the "rune" type. 1779 void 1780 set_is_rune() 1781 { this->is_rune_ = true; } 1782 1783 protected: 1784 bool 1785 do_compare_is_identity(Gogo*) 1786 { return true; } 1787 1788 unsigned int 1789 do_hash_for_method(Gogo*, int) const; 1790 1791 Btype* 1792 do_get_backend(Gogo*); 1793 1794 Expression* 1795 do_type_descriptor(Gogo*, Named_type*); 1796 1797 void 1798 do_reflection(Gogo*, std::string*) const; 1799 1800 void 1801 do_mangled_name(Gogo*, std::string*) const; 1802 1803 private: 1804 Integer_type(bool is_abstract, bool is_unsigned, int bits, 1805 int runtime_type_kind) 1806 : Type(TYPE_INTEGER), 1807 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false), 1808 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind) 1809 { } 1810 1811 // Map names of integer types to the types themselves. 1812 typedef std::map<std::string, Named_type*> Named_integer_types; 1813 static Named_integer_types named_integer_types; 1814 1815 // True if this is an abstract type. 1816 bool is_abstract_; 1817 // True if this is an unsigned type. 1818 bool is_unsigned_; 1819 // True if this is the byte type. 1820 bool is_byte_; 1821 // True if this is the rune type. 1822 bool is_rune_; 1823 // The number of bits. 1824 int bits_; 1825 // The runtime type code used in the type descriptor for this type. 1826 int runtime_type_kind_; 1827 }; 1828 1829 // The type of a floating point number. 1830 1831 class Float_type : public Type 1832 { 1833 public: 1834 // Create a new float type. 1835 static Named_type* 1836 create_float_type(const char* name, int bits, int runtime_type_kind); 1837 1838 // Look up an existing float type. 1839 static Named_type* 1840 lookup_float_type(const char* name); 1841 1842 // Create an abstract float type. 1843 static Float_type* 1844 create_abstract_float_type(); 1845 1846 // Whether this is an abstract float type. 1847 bool 1848 is_abstract() const 1849 { return this->is_abstract_; } 1850 1851 // The number of bits. 1852 int 1853 bits() const 1854 { return this->bits_; } 1855 1856 // Whether this type is the same as T. 1857 bool 1858 is_identical(const Float_type* t) const; 1859 1860 protected: 1861 bool 1862 do_compare_is_identity(Gogo*) 1863 { return false; } 1864 1865 bool 1866 do_is_reflexive() 1867 { return false; } 1868 1869 // Distinction between +0 and -0 requires a key update. 1870 bool 1871 do_needs_key_update() 1872 { return true; } 1873 1874 unsigned int 1875 do_hash_for_method(Gogo*, int) const; 1876 1877 Btype* 1878 do_get_backend(Gogo*); 1879 1880 Expression* 1881 do_type_descriptor(Gogo*, Named_type*); 1882 1883 void 1884 do_reflection(Gogo*, std::string*) const; 1885 1886 void 1887 do_mangled_name(Gogo*, std::string*) const; 1888 1889 private: 1890 Float_type(bool is_abstract, int bits, int runtime_type_kind) 1891 : Type(TYPE_FLOAT), 1892 is_abstract_(is_abstract), bits_(bits), 1893 runtime_type_kind_(runtime_type_kind) 1894 { } 1895 1896 // Map names of float types to the types themselves. 1897 typedef std::map<std::string, Named_type*> Named_float_types; 1898 static Named_float_types named_float_types; 1899 1900 // True if this is an abstract type. 1901 bool is_abstract_; 1902 // The number of bits in the floating point value. 1903 int bits_; 1904 // The runtime type code used in the type descriptor for this type. 1905 int runtime_type_kind_; 1906 }; 1907 1908 // The type of a complex number. 1909 1910 class Complex_type : public Type 1911 { 1912 public: 1913 // Create a new complex type. 1914 static Named_type* 1915 create_complex_type(const char* name, int bits, int runtime_type_kind); 1916 1917 // Look up an existing complex type. 1918 static Named_type* 1919 lookup_complex_type(const char* name); 1920 1921 // Create an abstract complex type. 1922 static Complex_type* 1923 create_abstract_complex_type(); 1924 1925 // Whether this is an abstract complex type. 1926 bool 1927 is_abstract() const 1928 { return this->is_abstract_; } 1929 1930 // The number of bits: 64 or 128. 1931 int bits() const 1932 { return this->bits_; } 1933 1934 // Whether this type is the same as T. 1935 bool 1936 is_identical(const Complex_type* t) const; 1937 1938 protected: 1939 bool 1940 do_compare_is_identity(Gogo*) 1941 { return false; } 1942 1943 bool 1944 do_is_reflexive() 1945 { return false; } 1946 1947 // Distinction between +0 and -0 requires a key update. 1948 bool 1949 do_needs_key_update() 1950 { return true; } 1951 1952 unsigned int 1953 do_hash_for_method(Gogo*, int) const; 1954 1955 Btype* 1956 do_get_backend(Gogo*); 1957 1958 Expression* 1959 do_type_descriptor(Gogo*, Named_type*); 1960 1961 void 1962 do_reflection(Gogo*, std::string*) const; 1963 1964 void 1965 do_mangled_name(Gogo*, std::string*) const; 1966 1967 private: 1968 Complex_type(bool is_abstract, int bits, int runtime_type_kind) 1969 : Type(TYPE_COMPLEX), 1970 is_abstract_(is_abstract), bits_(bits), 1971 runtime_type_kind_(runtime_type_kind) 1972 { } 1973 1974 // Map names of complex types to the types themselves. 1975 typedef std::map<std::string, Named_type*> Named_complex_types; 1976 static Named_complex_types named_complex_types; 1977 1978 // True if this is an abstract type. 1979 bool is_abstract_; 1980 // The number of bits in the complex value--64 or 128. 1981 int bits_; 1982 // The runtime type code used in the type descriptor for this type. 1983 int runtime_type_kind_; 1984 }; 1985 1986 // The type of a string. 1987 1988 class String_type : public Type 1989 { 1990 public: 1991 String_type() 1992 : Type(TYPE_STRING) 1993 { } 1994 1995 protected: 1996 bool 1997 do_has_pointer() const 1998 { return true; } 1999 2000 bool 2001 do_compare_is_identity(Gogo*) 2002 { return false; } 2003 2004 // New string might have a smaller backing store. 2005 bool 2006 do_needs_key_update() 2007 { return true; } 2008 2009 Btype* 2010 do_get_backend(Gogo*); 2011 2012 Expression* 2013 do_type_descriptor(Gogo*, Named_type*); 2014 2015 void 2016 do_reflection(Gogo*, std::string*) const; 2017 2018 void 2019 do_mangled_name(Gogo*, std::string* ret) const; 2020 2021 private: 2022 // The named string type. 2023 static Named_type* string_type_; 2024 }; 2025 2026 // The type of a function. 2027 2028 class Function_type : public Type 2029 { 2030 public: 2031 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters, 2032 Typed_identifier_list* results, Location location) 2033 : Type(TYPE_FUNCTION), 2034 receiver_(receiver), parameters_(parameters), results_(results), 2035 location_(location), is_varargs_(false), is_builtin_(false), 2036 fnbtype_(NULL), is_tagged_(false) 2037 { } 2038 2039 // Get the receiver. 2040 const Typed_identifier* 2041 receiver() const 2042 { return this->receiver_; } 2043 2044 // Add an escape note for the receiver. 2045 void 2046 add_receiver_note(int encoding) 2047 { this->receiver_->set_note(Escape_note::make_tag(encoding)); } 2048 2049 // Get the return names and types. 2050 const Typed_identifier_list* 2051 results() const 2052 { return this->results_; } 2053 2054 // Get the parameter names and types. 2055 const Typed_identifier_list* 2056 parameters() const 2057 { return this->parameters_; } 2058 2059 // Add an escape note for the ith parameter. 2060 void 2061 add_parameter_note(int index, int encoding) 2062 { this->parameters_->at(index).set_note(Escape_note::make_tag(encoding)); } 2063 2064 // Whether this function has been tagged during escape analysis. 2065 bool 2066 is_tagged() const 2067 { return this->is_tagged_; } 2068 2069 // Mark this function as tagged after analyzing its escape. 2070 void 2071 set_is_tagged() 2072 { this->is_tagged_ = true; } 2073 2074 // Whether this is a varargs function. 2075 bool 2076 is_varargs() const 2077 { return this->is_varargs_; } 2078 2079 // Whether this is a builtin function. 2080 bool 2081 is_builtin() const 2082 { return this->is_builtin_; } 2083 2084 // The location where this type was defined. 2085 Location 2086 location() const 2087 { return this->location_; } 2088 2089 // Return whether this is a method type. 2090 bool 2091 is_method() const 2092 { return this->receiver_ != NULL; } 2093 2094 // Whether T is a valid redeclaration of this type. This is called 2095 // when a function is declared more than once. 2096 bool 2097 is_valid_redeclaration(const Function_type* t, std::string*) const; 2098 2099 // Whether this type is the same as T. 2100 bool 2101 is_identical(const Function_type* t, bool ignore_receiver, int flags, 2102 std::string*) const; 2103 2104 // Record that this is a varargs function. 2105 void 2106 set_is_varargs() 2107 { this->is_varargs_ = true; } 2108 2109 // Record that this is a builtin function. 2110 void 2111 set_is_builtin() 2112 { this->is_builtin_ = true; } 2113 2114 // Import a function type. 2115 static Function_type* 2116 do_import(Import*); 2117 2118 // Return a copy of this type without a receiver. This is only 2119 // valid for a method type. 2120 Function_type* 2121 copy_without_receiver() const; 2122 2123 // Return a copy of this type with a receiver. This is used when an 2124 // interface method is attached to a named or struct type. 2125 Function_type* 2126 copy_with_receiver(Type*) const; 2127 2128 // Return a copy of this type with the receiver treated as the first 2129 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is 2130 // forced to be a pointer. 2131 Function_type* 2132 copy_with_receiver_as_param(bool want_pointer_receiver) const; 2133 2134 // Return a copy of this type ignoring any receiver and using dummy 2135 // names for all parameters. This is used for thunks for method 2136 // values. 2137 Function_type* 2138 copy_with_names() const; 2139 2140 static Type* 2141 make_function_type_descriptor_type(); 2142 2143 // Return the backend representation of this function type. This is used 2144 // as the real type of a backend function declaration or defintion. 2145 Btype* 2146 get_backend_fntype(Gogo*); 2147 2148 // Return whether this is a Backend_function_type. 2149 virtual bool 2150 is_backend_function_type() const 2151 { return false; } 2152 2153 protected: 2154 int 2155 do_traverse(Traverse*); 2156 2157 // A function descriptor may be allocated on the heap. 2158 bool 2159 do_has_pointer() const 2160 { return true; } 2161 2162 bool 2163 do_compare_is_identity(Gogo*) 2164 { return false; } 2165 2166 unsigned int 2167 do_hash_for_method(Gogo*, int) const; 2168 2169 Btype* 2170 do_get_backend(Gogo*); 2171 2172 Expression* 2173 do_type_descriptor(Gogo*, Named_type*); 2174 2175 void 2176 do_reflection(Gogo*, std::string*) const; 2177 2178 void 2179 do_mangled_name(Gogo*, std::string*) const; 2180 2181 void 2182 do_export(Export*) const; 2183 2184 private: 2185 Expression* 2186 type_descriptor_params(Type*, const Typed_identifier*, 2187 const Typed_identifier_list*); 2188 2189 // A mapping from a list of result types to a backend struct type. 2190 class Results_hash 2191 { 2192 public: 2193 unsigned int 2194 operator()(const Typed_identifier_list*) const; 2195 }; 2196 2197 class Results_equal 2198 { 2199 public: 2200 bool 2201 operator()(const Typed_identifier_list*, 2202 const Typed_identifier_list*) const; 2203 }; 2204 2205 typedef Unordered_map_hash(Typed_identifier_list*, Btype*, 2206 Results_hash, Results_equal) Results_structs; 2207 2208 static Results_structs results_structs; 2209 2210 // The receiver name and type. This will be NULL for a normal 2211 // function, non-NULL for a method. 2212 Typed_identifier* receiver_; 2213 // The parameter names and types. 2214 Typed_identifier_list* parameters_; 2215 // The result names and types. This will be NULL if no result was 2216 // specified. 2217 Typed_identifier_list* results_; 2218 // The location where this type was defined. This exists solely to 2219 // give a location for the fields of the struct if this function 2220 // returns multiple values. 2221 Location location_; 2222 // Whether this function takes a variable number of arguments. 2223 bool is_varargs_; 2224 // Whether this is a special builtin function which can not simply 2225 // be called. This is used for len, cap, etc. 2226 bool is_builtin_; 2227 // The backend representation of this type for backend function 2228 // declarations and definitions. 2229 Btype* fnbtype_; 2230 // Whether this function has been analyzed by escape analysis. If this is 2231 // TRUE, this function type's parameters contain a summary of the analysis. 2232 bool is_tagged_; 2233 }; 2234 2235 // The type of a function's backend representation. 2236 2237 class Backend_function_type : public Function_type 2238 { 2239 public: 2240 Backend_function_type(Typed_identifier* receiver, 2241 Typed_identifier_list* parameters, 2242 Typed_identifier_list* results, Location location) 2243 : Function_type(receiver, parameters, results, location) 2244 { } 2245 2246 // Return whether this is a Backend_function_type. This overrides 2247 // Function_type::is_backend_function_type. 2248 bool 2249 is_backend_function_type() const 2250 { return true; } 2251 2252 protected: 2253 Btype* 2254 do_get_backend(Gogo* gogo) 2255 { return this->get_backend_fntype(gogo); } 2256 }; 2257 2258 // The type of a pointer. 2259 2260 class Pointer_type : public Type 2261 { 2262 public: 2263 Pointer_type(Type* to_type) 2264 : Type(TYPE_POINTER), 2265 to_type_(to_type) 2266 {} 2267 2268 Type* 2269 points_to() const 2270 { return this->to_type_; } 2271 2272 // Import a pointer type. 2273 static Pointer_type* 2274 do_import(Import*); 2275 2276 static Type* 2277 make_pointer_type_descriptor_type(); 2278 2279 protected: 2280 int 2281 do_traverse(Traverse*); 2282 2283 bool 2284 do_verify() 2285 { return this->to_type_->verify(); } 2286 2287 bool 2288 do_has_pointer() const 2289 { return true; } 2290 2291 bool 2292 do_compare_is_identity(Gogo*) 2293 { return true; } 2294 2295 unsigned int 2296 do_hash_for_method(Gogo*, int) const; 2297 2298 Btype* 2299 do_get_backend(Gogo*); 2300 2301 Expression* 2302 do_type_descriptor(Gogo*, Named_type*); 2303 2304 void 2305 do_reflection(Gogo*, std::string*) const; 2306 2307 void 2308 do_mangled_name(Gogo*, std::string*) const; 2309 2310 void 2311 do_export(Export*) const; 2312 2313 private: 2314 // The type to which this type points. 2315 Type* to_type_; 2316 }; 2317 2318 // The nil type. We use a special type for nil because it is not the 2319 // same as any other type. In C term nil has type void*, but there is 2320 // no such type in Go. 2321 2322 class Nil_type : public Type 2323 { 2324 public: 2325 Nil_type() 2326 : Type(TYPE_NIL) 2327 { } 2328 2329 protected: 2330 bool 2331 do_compare_is_identity(Gogo*) 2332 { return false; } 2333 2334 Btype* 2335 do_get_backend(Gogo* gogo); 2336 2337 Expression* 2338 do_type_descriptor(Gogo*, Named_type*) 2339 { go_unreachable(); } 2340 2341 void 2342 do_reflection(Gogo*, std::string*) const 2343 { go_unreachable(); } 2344 2345 void 2346 do_mangled_name(Gogo*, std::string* ret) const; 2347 }; 2348 2349 // The type of a field in a struct. 2350 2351 class Struct_field 2352 { 2353 public: 2354 explicit Struct_field(const Typed_identifier& typed_identifier) 2355 : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false) 2356 { } 2357 2358 // The field name. 2359 const std::string& 2360 field_name() const; 2361 2362 // Return whether this struct field is named NAME. 2363 bool 2364 is_field_name(const std::string& name) const; 2365 2366 // Return whether this struct field is an unexported field named NAME. 2367 bool 2368 is_unexported_field_name(Gogo*, const std::string& name) const; 2369 2370 // Return whether this struct field is an embedded built-in type. 2371 bool 2372 is_embedded_builtin(Gogo*) const; 2373 2374 // The field type. 2375 Type* 2376 type() const 2377 { return this->typed_identifier_.type(); } 2378 2379 // The field location. 2380 Location 2381 location() const 2382 { return this->typed_identifier_.location(); } 2383 2384 // Whether the field has a tag. 2385 bool 2386 has_tag() const 2387 { return this->tag_ != NULL; } 2388 2389 // The tag. 2390 const std::string& 2391 tag() const 2392 { 2393 go_assert(this->tag_ != NULL); 2394 return *this->tag_; 2395 } 2396 2397 // Whether this is an anonymous field. 2398 bool 2399 is_anonymous() const 2400 { return this->typed_identifier_.name().empty(); } 2401 2402 // Set the tag. FIXME: This is never freed. 2403 void 2404 set_tag(const std::string& tag) 2405 { this->tag_ = new std::string(tag); } 2406 2407 // Record that this field is defined in an imported struct. 2408 void 2409 set_is_imported() 2410 { this->is_imported_ = true; } 2411 2412 // Set the type. This is only used in error cases. 2413 void 2414 set_type(Type* type) 2415 { this->typed_identifier_.set_type(type); } 2416 2417 private: 2418 // The field name, type, and location. 2419 Typed_identifier typed_identifier_; 2420 // The field tag. This is NULL if the field has no tag. 2421 std::string* tag_; 2422 // Whether this field is defined in an imported struct. 2423 bool is_imported_; 2424 }; 2425 2426 // A list of struct fields. 2427 2428 class Struct_field_list 2429 { 2430 public: 2431 Struct_field_list() 2432 : entries_() 2433 { } 2434 2435 // Whether the list is empty. 2436 bool 2437 empty() const 2438 { return this->entries_.empty(); } 2439 2440 // Return the number of entries. 2441 size_t 2442 size() const 2443 { return this->entries_.size(); } 2444 2445 // Add an entry to the end of the list. 2446 void 2447 push_back(const Struct_field& sf) 2448 { this->entries_.push_back(sf); } 2449 2450 // Index into the list. 2451 const Struct_field& 2452 at(size_t i) const 2453 { return this->entries_.at(i); } 2454 2455 // Last entry in list. 2456 Struct_field& 2457 back() 2458 { return this->entries_.back(); } 2459 2460 // Iterators. 2461 2462 typedef std::vector<Struct_field>::iterator iterator; 2463 typedef std::vector<Struct_field>::const_iterator const_iterator; 2464 2465 iterator 2466 begin() 2467 { return this->entries_.begin(); } 2468 2469 const_iterator 2470 begin() const 2471 { return this->entries_.begin(); } 2472 2473 iterator 2474 end() 2475 { return this->entries_.end(); } 2476 2477 const_iterator 2478 end() const 2479 { return this->entries_.end(); } 2480 2481 private: 2482 std::vector<Struct_field> entries_; 2483 }; 2484 2485 // The type of a struct. 2486 2487 class Struct_type : public Type 2488 { 2489 public: 2490 Struct_type(Struct_field_list* fields, Location location) 2491 : Type(TYPE_STRUCT), 2492 fields_(fields), location_(location), all_methods_(NULL), 2493 is_struct_incomparable_(false), has_padding_(false) 2494 { } 2495 2496 // Return the field NAME. This only looks at local fields, not at 2497 // embedded types. If the field is found, and PINDEX is not NULL, 2498 // this sets *PINDEX to the field index. If the field is not found, 2499 // this returns NULL. 2500 const Struct_field* 2501 find_local_field(const std::string& name, unsigned int *pindex) const; 2502 2503 // Return the field number INDEX. 2504 const Struct_field* 2505 field(unsigned int index) const 2506 { return &this->fields_->at(index); } 2507 2508 // Get the struct fields. 2509 const Struct_field_list* 2510 fields() const 2511 { return this->fields_; } 2512 2513 // Return the number of fields. 2514 size_t 2515 field_count() const 2516 { return this->fields_->size(); } 2517 2518 // Location of struct definition. 2519 Location 2520 location() const 2521 { return this->location_; } 2522 2523 // Push a new field onto the end of the struct. This is used when 2524 // building a closure variable. 2525 void 2526 push_field(const Struct_field& sf) 2527 { this->fields_->push_back(sf); } 2528 2529 // Return an expression referring to field NAME in STRUCT_EXPR, or 2530 // NULL if there is no field with that name. 2531 Field_reference_expression* 2532 field_reference(Expression* struct_expr, const std::string& name, 2533 Location) const; 2534 2535 // Return the total number of fields, including embedded fields. 2536 // This is the number of values that can appear in a conversion to 2537 // this type. 2538 unsigned int 2539 total_field_count() const; 2540 2541 // Whether this type is identical with T. 2542 bool 2543 is_identical(const Struct_type* t, int) const; 2544 2545 // Return whether NAME is a local field which is not exported. This 2546 // is only used for better error reporting. 2547 bool 2548 is_unexported_local_field(Gogo*, const std::string& name) const; 2549 2550 // If this is an unnamed struct, build the complete list of methods, 2551 // including those from anonymous fields, and build methods stubs if 2552 // needed. 2553 void 2554 finalize_methods(Gogo*); 2555 2556 // Return whether this type has any methods. This should only be 2557 // called after the finalize_methods pass. 2558 bool 2559 has_any_methods() const 2560 { return this->all_methods_ != NULL; } 2561 2562 // Return the methods for this type. This should only be called 2563 // after the finalize_methods pass. 2564 const Methods* 2565 methods() const 2566 { return this->all_methods_; } 2567 2568 // Return the method to use for NAME. This returns NULL if there is 2569 // no such method or if the method is ambiguous. When it returns 2570 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous. 2571 Method* 2572 method_function(const std::string& name, bool* is_ambiguous) const; 2573 2574 // Return a pointer to the interface method table for this type for 2575 // the interface INTERFACE. If IS_POINTER is true, set the type 2576 // descriptor to a pointer to this type, otherwise set it to this 2577 // type. 2578 Expression* 2579 interface_method_table(Interface_type* interface, bool is_pointer); 2580 2581 // Traverse just the field types of a struct type. 2582 int 2583 traverse_field_types(Traverse* traverse) 2584 { return this->do_traverse(traverse); } 2585 2586 // If the offset of field INDEX in the backend implementation can be 2587 // determined, set *POFFSET to the offset in bytes and return true. 2588 // Otherwise, return false. 2589 bool 2590 backend_field_offset(Gogo*, unsigned int index, int64_t* poffset); 2591 2592 // Finish the backend representation of all the fields. 2593 void 2594 finish_backend_fields(Gogo*); 2595 2596 // Import a struct type. 2597 static Struct_type* 2598 do_import(Import*); 2599 2600 static Type* 2601 make_struct_type_descriptor_type(); 2602 2603 // Return whether this is a generated struct that is not comparable. 2604 bool 2605 is_struct_incomparable() const 2606 { return this->is_struct_incomparable_; } 2607 2608 // Record that this is a generated struct that is not comparable. 2609 void 2610 set_is_struct_incomparable() 2611 { this->is_struct_incomparable_ = true; } 2612 2613 // Return whether this struct's backend type has padding, due to 2614 // trailing zero-sized field. 2615 bool 2616 has_padding() const 2617 { return this->has_padding_; } 2618 2619 // Record that this struct's backend type has padding. 2620 void 2621 set_has_padding() 2622 { this->has_padding_ = true; } 2623 2624 // Write the hash function for this type. 2625 void 2626 write_hash_function(Gogo*, Function_type*); 2627 2628 // Write the equality function for this type. 2629 void 2630 write_equal_function(Gogo*, Named_type*); 2631 2632 // Whether we can write this type to a C header file, to implement 2633 // -fgo-c-header. 2634 bool 2635 can_write_to_c_header(std::vector<const Named_object*>*, 2636 std::vector<const Named_object*>*) const; 2637 2638 // Write this type to a C header file, to implement -fgo-c-header. 2639 void 2640 write_to_c_header(std::ostream&) const; 2641 2642 protected: 2643 int 2644 do_traverse(Traverse*); 2645 2646 bool 2647 do_verify(); 2648 2649 bool 2650 do_has_pointer() const; 2651 2652 bool 2653 do_compare_is_identity(Gogo*); 2654 2655 bool 2656 do_is_reflexive(); 2657 2658 bool 2659 do_needs_key_update(); 2660 2661 bool 2662 do_hash_might_panic(); 2663 2664 bool 2665 do_in_heap(); 2666 2667 unsigned int 2668 do_hash_for_method(Gogo*, int) const; 2669 2670 Btype* 2671 do_get_backend(Gogo*); 2672 2673 Expression* 2674 do_type_descriptor(Gogo*, Named_type*); 2675 2676 void 2677 do_reflection(Gogo*, std::string*) const; 2678 2679 void 2680 do_mangled_name(Gogo*, std::string*) const; 2681 2682 void 2683 do_export(Export*) const; 2684 2685 private: 2686 bool 2687 can_write_type_to_c_header(const Type*, 2688 std::vector<const Named_object*>*, 2689 std::vector<const Named_object*>*) const; 2690 2691 void 2692 write_field_to_c_header(std::ostream&, const std::string&, const Type*) const; 2693 2694 // Used to merge method sets of identical unnamed structs. 2695 typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical, 2696 Type_identical) Identical_structs; 2697 2698 static Identical_structs identical_structs; 2699 2700 // Used to manage method tables for identical unnamed structs. 2701 typedef std::pair<Interface_method_tables*, Interface_method_tables*> 2702 Struct_method_table_pair; 2703 2704 typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*, 2705 Type_hash_identical, Type_identical) 2706 Struct_method_tables; 2707 2708 static Struct_method_tables struct_method_tables; 2709 2710 // Used to avoid infinite loops in field_reference_depth. 2711 struct Saw_named_type 2712 { 2713 Saw_named_type* next; 2714 Named_type* nt; 2715 }; 2716 2717 Field_reference_expression* 2718 field_reference_depth(Expression* struct_expr, const std::string& name, 2719 Location, Saw_named_type*, 2720 unsigned int* depth) const; 2721 2722 // The fields of the struct. 2723 Struct_field_list* fields_; 2724 // The place where the struct was declared. 2725 Location location_; 2726 // If this struct is unnamed, a list of methods. 2727 Methods* all_methods_; 2728 // True if this is a generated struct that is not considered to be 2729 // comparable. 2730 bool is_struct_incomparable_; 2731 // True if this struct's backend type has padding, due to trailing 2732 // zero-sized field. 2733 bool has_padding_; 2734 }; 2735 2736 // The type of an array. 2737 2738 class Array_type : public Type 2739 { 2740 public: 2741 Array_type(Type* element_type, Expression* length) 2742 : Type(TYPE_ARRAY), 2743 element_type_(element_type), length_(length), blength_(NULL), 2744 issued_length_error_(false), is_array_incomparable_(false) 2745 { } 2746 2747 // Return the element type. 2748 Type* 2749 element_type() const 2750 { return this->element_type_; } 2751 2752 // Return the length. This will return NULL for a slice. 2753 Expression* 2754 length() const 2755 { return this->length_; } 2756 2757 // Store the length as an int64_t into *PLEN. Return false if the 2758 // length can not be determined. This will assert if called for a 2759 // slice. 2760 bool 2761 int_length(int64_t* plen) const; 2762 2763 // Whether this type is identical with T. 2764 bool 2765 is_identical(const Array_type* t, int) const; 2766 2767 // Return an expression for the pointer to the values in an array. 2768 Expression* 2769 get_value_pointer(Gogo*, Expression* array, bool is_lvalue) const; 2770 2771 // Return an expression for the length of an array with this type. 2772 Expression* 2773 get_length(Gogo*, Expression* array) const; 2774 2775 // Return an expression for the capacity of an array with this type. 2776 Expression* 2777 get_capacity(Gogo*, Expression* array) const; 2778 2779 // Import an array type. 2780 static Array_type* 2781 do_import(Import*); 2782 2783 // Return the backend representation of the element type. 2784 Btype* 2785 get_backend_element(Gogo*, bool use_placeholder); 2786 2787 // Return the backend representation of the length. 2788 Bexpression* 2789 get_backend_length(Gogo*); 2790 2791 // Finish the backend representation of the element type. 2792 void 2793 finish_backend_element(Gogo*); 2794 2795 static Type* 2796 make_array_type_descriptor_type(); 2797 2798 static Type* 2799 make_slice_type_descriptor_type(); 2800 2801 // Return whether this is a generated array that is not comparable. 2802 bool 2803 is_array_incomparable() const 2804 { return this->is_array_incomparable_; } 2805 2806 // Record that this is a generated array that is not comparable. 2807 void 2808 set_is_array_incomparable() 2809 { this->is_array_incomparable_ = true; } 2810 2811 // Write the hash function for this type. 2812 void 2813 write_hash_function(Gogo*, Function_type*); 2814 2815 // Write the equality function for this type. 2816 void 2817 write_equal_function(Gogo*, Named_type*); 2818 2819 protected: 2820 int 2821 do_traverse(Traverse* traverse); 2822 2823 bool 2824 do_verify(); 2825 2826 bool 2827 do_has_pointer() const; 2828 2829 bool 2830 do_compare_is_identity(Gogo*); 2831 2832 bool 2833 do_is_reflexive() 2834 { 2835 return this->length_ != NULL && this->element_type_->is_reflexive(); 2836 } 2837 2838 bool 2839 do_needs_key_update() 2840 { return this->element_type_->needs_key_update(); } 2841 2842 bool 2843 do_hash_might_panic() 2844 { return this->length_ != NULL && this->element_type_->hash_might_panic(); } 2845 2846 bool 2847 do_in_heap() 2848 { return this->length_ == NULL || this->element_type_->in_heap(); } 2849 2850 unsigned int 2851 do_hash_for_method(Gogo*, int) const; 2852 2853 Btype* 2854 do_get_backend(Gogo*); 2855 2856 Expression* 2857 do_type_descriptor(Gogo*, Named_type*); 2858 2859 void 2860 do_reflection(Gogo*, std::string*) const; 2861 2862 void 2863 do_mangled_name(Gogo*, std::string*) const; 2864 2865 void 2866 do_export(Export*) const; 2867 2868 private: 2869 bool 2870 verify_length(); 2871 2872 Expression* 2873 array_type_descriptor(Gogo*, Named_type*); 2874 2875 Expression* 2876 slice_type_descriptor(Gogo*, Named_type*); 2877 2878 // The type of elements of the array. 2879 Type* element_type_; 2880 // The number of elements. This may be NULL. 2881 Expression* length_; 2882 // The backend representation of the length. 2883 // We only want to compute this once. 2884 Bexpression* blength_; 2885 // Whether or not an invalid length error has been issued for this type, 2886 // to avoid knock-on errors. 2887 mutable bool issued_length_error_; 2888 // True if this is a generated array that is not considered to be 2889 // comparable. 2890 bool is_array_incomparable_; 2891 }; 2892 2893 // The type of a map. 2894 2895 class Map_type : public Type 2896 { 2897 public: 2898 Map_type(Type* key_type, Type* val_type, Location location) 2899 : Type(TYPE_MAP), 2900 key_type_(key_type), val_type_(val_type), hmap_type_(NULL), 2901 bucket_type_(NULL), hiter_type_(NULL), location_(location) 2902 { } 2903 2904 // Return the key type. 2905 Type* 2906 key_type() const 2907 { return this->key_type_; } 2908 2909 // Return the value type. 2910 Type* 2911 val_type() const 2912 { return this->val_type_; } 2913 2914 // Return the type used for an iteration over this map. 2915 Type* 2916 hiter_type(Gogo*); 2917 2918 // If this map requires the "fat" functions, returns the pointer to 2919 // pass as the zero value to those functions. Otherwise, in the 2920 // normal case, returns NULL. 2921 Expression* 2922 fat_zero_value(Gogo*); 2923 2924 // Map algorithm to use for this map type. We may use specialized 2925 // fast map routines for certain key types. 2926 enum Map_alg 2927 { 2928 // 32-bit key. 2929 MAP_ALG_FAST32, 2930 // 32-bit pointer key. 2931 MAP_ALG_FAST32PTR, 2932 // 64-bit key. 2933 MAP_ALG_FAST64, 2934 // 64-bit pointer key. 2935 MAP_ALG_FAST64PTR, 2936 // String key. 2937 MAP_ALG_FASTSTR, 2938 // Anything else. 2939 MAP_ALG_SLOW, 2940 }; 2941 2942 Map_alg 2943 algorithm(Gogo*); 2944 2945 // Return whether VAR is the map zero value. 2946 static bool 2947 is_zero_value(Variable* var); 2948 2949 // Return the backend representation of the map zero value. 2950 static Bvariable* 2951 backend_zero_value(Gogo*); 2952 2953 // Whether this type is identical with T. 2954 bool 2955 is_identical(const Map_type* t, int) const; 2956 2957 // Import a map type. 2958 static Map_type* 2959 do_import(Import*); 2960 2961 static Type* 2962 make_map_type_descriptor_type(); 2963 2964 // This must be in sync with libgo/go/runtime/map.go. 2965 static const int bucket_size = 8; 2966 2967 protected: 2968 int 2969 do_traverse(Traverse*); 2970 2971 bool 2972 do_verify(); 2973 2974 bool 2975 do_has_pointer() const 2976 { return true; } 2977 2978 bool 2979 do_compare_is_identity(Gogo*) 2980 { return false; } 2981 2982 bool 2983 do_is_reflexive() 2984 { 2985 return this->key_type_->is_reflexive() && this->val_type_->is_reflexive(); 2986 } 2987 2988 unsigned int 2989 do_hash_for_method(Gogo*, int) const; 2990 2991 Btype* 2992 do_get_backend(Gogo*); 2993 2994 Expression* 2995 do_type_descriptor(Gogo*, Named_type*); 2996 2997 void 2998 do_reflection(Gogo*, std::string*) const; 2999 3000 void 3001 do_mangled_name(Gogo*, std::string*) const; 3002 3003 void 3004 do_export(Export*) const; 3005 3006 private: 3007 // These must be in sync with libgo/go/runtime/map.go. 3008 static const int max_key_size = 128; 3009 static const int max_val_size = 128; 3010 static const int max_zero_size = 1024; 3011 3012 // Maps with value types larger than max_zero_size require passing a 3013 // zero value pointer to the map functions. 3014 3015 // The zero value variable. 3016 static Named_object* zero_value; 3017 3018 // The current size of the zero value. 3019 static int64_t zero_value_size; 3020 3021 // The current alignment of the zero value. 3022 static int64_t zero_value_align; 3023 3024 Type* 3025 bucket_type(Gogo*, int64_t, int64_t); 3026 3027 Type* 3028 hmap_type(Type*); 3029 3030 // The key type. 3031 Type* key_type_; 3032 // The value type. 3033 Type* val_type_; 3034 // The hashmap type. At run time a map is represented as a pointer 3035 // to this type. 3036 Type* hmap_type_; 3037 // The bucket type, the type used to hold keys and values at run time. 3038 Type* bucket_type_; 3039 // The iterator type. 3040 Type* hiter_type_; 3041 // Where the type was defined. 3042 Location location_; 3043 }; 3044 3045 // The type of a channel. 3046 3047 class Channel_type : public Type 3048 { 3049 public: 3050 Channel_type(bool may_send, bool may_receive, Type* element_type) 3051 : Type(TYPE_CHANNEL), 3052 may_send_(may_send), may_receive_(may_receive), 3053 element_type_(element_type) 3054 { go_assert(may_send || may_receive); } 3055 3056 // Whether this channel can send data. 3057 bool 3058 may_send() const 3059 { return this->may_send_; } 3060 3061 // Whether this channel can receive data. 3062 bool 3063 may_receive() const 3064 { return this->may_receive_; } 3065 3066 // The type of the values that may be sent on this channel. This is 3067 // NULL if any type may be sent. 3068 Type* 3069 element_type() const 3070 { return this->element_type_; } 3071 3072 // Whether this type is identical with T. 3073 bool 3074 is_identical(const Channel_type* t, int) const; 3075 3076 // Import a channel type. 3077 static Channel_type* 3078 do_import(Import*); 3079 3080 static Type* 3081 make_chan_type_descriptor_type(); 3082 3083 static Type* 3084 select_case_type(); 3085 3086 protected: 3087 int 3088 do_traverse(Traverse* traverse) 3089 { return Type::traverse(this->element_type_, traverse); } 3090 3091 bool 3092 do_verify(); 3093 3094 bool 3095 do_has_pointer() const 3096 { return true; } 3097 3098 bool 3099 do_compare_is_identity(Gogo*) 3100 { return true; } 3101 3102 unsigned int 3103 do_hash_for_method(Gogo*, int) const; 3104 3105 Btype* 3106 do_get_backend(Gogo*); 3107 3108 Expression* 3109 do_type_descriptor(Gogo*, Named_type*); 3110 3111 void 3112 do_reflection(Gogo*, std::string*) const; 3113 3114 void 3115 do_mangled_name(Gogo*, std::string*) const; 3116 3117 void 3118 do_export(Export*) const; 3119 3120 private: 3121 // Whether this channel can send data. 3122 bool may_send_; 3123 // Whether this channel can receive data. 3124 bool may_receive_; 3125 // The types of elements which may be sent on this channel. If this 3126 // is NULL, it means that any type may be sent. 3127 Type* element_type_; 3128 }; 3129 3130 // An interface type. 3131 3132 class Interface_type : public Type 3133 { 3134 public: 3135 Interface_type(Typed_identifier_list* methods, Location location) 3136 : Type(TYPE_INTERFACE), 3137 parse_methods_(methods), all_methods_(NULL), location_(location), 3138 package_(NULL), interface_btype_(NULL), bmethods_(NULL), 3139 assume_identical_(NULL), methods_are_finalized_(false), 3140 bmethods_is_placeholder_(false), seen_(false) 3141 { go_assert(methods == NULL || !methods->empty()); } 3142 3143 // The location where the interface type was defined. 3144 Location 3145 location() const 3146 { return this->location_; } 3147 3148 // The package where the interface type was defined. Returns NULL 3149 // for the package currently being compiled. 3150 Package* 3151 package() const 3152 { return this->package_; } 3153 3154 // Return whether this is an empty interface. 3155 bool 3156 is_empty() const 3157 { 3158 go_assert(this->methods_are_finalized_); 3159 return this->all_methods_ == NULL; 3160 } 3161 3162 // Return the list of locally defined methods. This will return NULL 3163 // for an empty interface. Embedded interfaces will appear in this 3164 // list as an entry with no name. 3165 const Typed_identifier_list* 3166 local_methods() const 3167 { return this->parse_methods_; } 3168 3169 // Return the list of all methods. This will return NULL for an 3170 // empty interface. 3171 const Typed_identifier_list* 3172 methods() const; 3173 3174 // Return the number of methods. 3175 size_t 3176 method_count() const; 3177 3178 // Return the method NAME, or NULL. 3179 const Typed_identifier* 3180 find_method(const std::string& name) const; 3181 3182 // Return the zero-based index of method NAME. 3183 size_t 3184 method_index(const std::string& name) const; 3185 3186 // Finalize the methods. This sets all_methods_. This handles 3187 // interface inheritance. 3188 void 3189 finalize_methods(); 3190 3191 // Return true if T implements this interface. If this returns 3192 // false, and REASON is not NULL, it sets *REASON to the reason that 3193 // it fails. 3194 bool 3195 implements_interface(const Type* t, std::string* reason) const; 3196 3197 // Whether this type is identical with T. REASON is as in 3198 // implements_interface. 3199 bool 3200 is_identical(const Interface_type* t, int) const; 3201 3202 // Whether we can assign T to this type. is_identical is known to 3203 // be false. 3204 bool 3205 is_compatible_for_assign(const Interface_type*, std::string* reason) const; 3206 3207 // Return whether NAME is a method which is not exported. This is 3208 // only used for better error reporting. 3209 bool 3210 is_unexported_method(Gogo*, const std::string& name) const; 3211 3212 // Import an interface type. 3213 static Interface_type* 3214 do_import(Import*); 3215 3216 // Make a struct for an empty interface type. 3217 static Btype* 3218 get_backend_empty_interface_type(Gogo*); 3219 3220 // Get a pointer to the backend representation of the method table. 3221 Btype* 3222 get_backend_methods(Gogo*); 3223 3224 // Return a placeholder for the backend representation of the 3225 // pointer to the method table. 3226 Btype* 3227 get_backend_methods_placeholder(Gogo*); 3228 3229 // Finish the backend representation of the method types. 3230 void 3231 finish_backend_methods(Gogo*); 3232 3233 static Type* 3234 make_interface_type_descriptor_type(); 3235 3236 // Return whether methods are finalized for this interface. 3237 bool 3238 methods_are_finalized() const 3239 { return this->methods_are_finalized_; } 3240 3241 // Sort embedded interfaces by name. Needed when we are preparing 3242 // to emit types into the export data. 3243 void 3244 sort_embedded() 3245 { 3246 if (parse_methods_ != NULL) 3247 parse_methods_->sort_by_name(); 3248 } 3249 3250 protected: 3251 int 3252 do_traverse(Traverse*); 3253 3254 bool 3255 do_has_pointer() const 3256 { return true; } 3257 3258 bool 3259 do_compare_is_identity(Gogo*) 3260 { return false; } 3261 3262 // Not reflexive if it contains a float. 3263 bool 3264 do_is_reflexive() 3265 { return false; } 3266 3267 // Distinction between +0 and -0 requires a key update if it 3268 // contains a float. 3269 bool 3270 do_needs_key_update() 3271 { return true; } 3272 3273 // Hashing an unhashable type stored in an interface might panic. 3274 bool 3275 do_hash_might_panic() 3276 { return true; } 3277 3278 unsigned int 3279 do_hash_for_method(Gogo*, int) const; 3280 3281 Btype* 3282 do_get_backend(Gogo*); 3283 3284 Expression* 3285 do_type_descriptor(Gogo*, Named_type*); 3286 3287 void 3288 do_reflection(Gogo*, std::string*) const; 3289 3290 void 3291 do_mangled_name(Gogo*, std::string*) const; 3292 3293 void 3294 do_export(Export*) const; 3295 3296 private: 3297 // This type guards against infinite recursion when comparing 3298 // interface types. We keep a list of interface types assumed to be 3299 // identical during comparison. We just keep the list on the stack. 3300 // This permits us to compare cases like 3301 // type I1 interface { F() interface{I1} } 3302 // type I2 interface { F() interface{I2} } 3303 struct Assume_identical 3304 { 3305 Assume_identical* next; 3306 const Interface_type* t1; 3307 const Interface_type* t2; 3308 }; 3309 3310 bool 3311 assume_identical(const Interface_type*, const Interface_type*) const; 3312 3313 struct Bmethods_map_entry 3314 { 3315 Btype *btype; 3316 bool is_placeholder; 3317 }; 3318 3319 // A mapping from Interface_type to the backend type of its bmethods_, 3320 // used to ensure that the backend representation of identical types 3321 // is identical. 3322 typedef Unordered_map_hash(const Interface_type*, Bmethods_map_entry, 3323 Type_hash_identical, Type_identical) Bmethods_map; 3324 3325 static Bmethods_map bmethods_map; 3326 3327 // The list of methods associated with the interface from the 3328 // parser. This will be NULL for the empty interface. This may 3329 // include unnamed interface types. 3330 Typed_identifier_list* parse_methods_; 3331 // The list of all methods associated with the interface. This 3332 // expands any interface types listed in methods_. It is set by 3333 // finalize_methods. This will be NULL for the empty interface. 3334 Typed_identifier_list* all_methods_; 3335 // The location where the interface was defined. 3336 Location location_; 3337 // The package where the interface was defined. This is NULL for 3338 // the package being compiled. 3339 Package* package_; 3340 // The backend representation of this type during backend conversion. 3341 Btype* interface_btype_; 3342 // The backend representation of the pointer to the method table. 3343 Btype* bmethods_; 3344 // A list of interface types assumed to be identical during 3345 // interface comparison. 3346 mutable Assume_identical* assume_identical_; 3347 // Whether the methods have been finalized. 3348 bool methods_are_finalized_; 3349 // Whether the bmethods_ field is a placeholder. 3350 bool bmethods_is_placeholder_; 3351 // Used to avoid endless recursion in do_mangled_name. 3352 mutable bool seen_; 3353 }; 3354 3355 // The value we keep for a named type. This lets us get the right 3356 // name when we convert to backend. Note that we don't actually keep 3357 // the name here; the name is in the Named_object which points to 3358 // this. This object exists to hold a unique backend representation for 3359 // the type. 3360 3361 class Named_type : public Type 3362 { 3363 public: 3364 Named_type(Named_object* named_object, Type* type, Location location) 3365 : Type(TYPE_NAMED), 3366 named_object_(named_object), in_function_(NULL), in_function_index_(0), 3367 type_(type), local_methods_(NULL), all_methods_(NULL), 3368 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL), 3369 location_(location), named_btype_(NULL), dependencies_(), 3370 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true), 3371 is_placeholder_(false), is_converted_(false), is_verified_(false), 3372 seen_(false), seen_in_compare_is_identity_(false), 3373 seen_in_get_backend_(false), seen_alias_(false) 3374 { } 3375 3376 // Return the associated Named_object. This holds the actual name. 3377 Named_object* 3378 named_object() 3379 { return this->named_object_; } 3380 3381 const Named_object* 3382 named_object() const 3383 { return this->named_object_; } 3384 3385 // Set the Named_object. This is used when we see a type 3386 // declaration followed by a type. 3387 void 3388 set_named_object(Named_object* no) 3389 { this->named_object_ = no; } 3390 3391 // Whether this is an alias (type T1 = T2) rather than an ordinary 3392 // named type (type T1 T2). 3393 bool 3394 is_alias() const 3395 { return this->is_alias_; } 3396 3397 // Record that this type is an alias. 3398 void 3399 set_is_alias() 3400 { this->is_alias_ = true; } 3401 3402 // Mark this type as not permitted in the heap. 3403 void 3404 set_not_in_heap() 3405 { this->in_heap_ = false; } 3406 3407 // Return the function in which this type is defined. This will 3408 // return NULL for a type defined in global scope. 3409 const Named_object* 3410 in_function(unsigned int *pindex) const 3411 { 3412 *pindex = this->in_function_index_; 3413 return this->in_function_; 3414 } 3415 3416 // Set the function in which this type is defined. 3417 void 3418 set_in_function(Named_object* f, unsigned int index) 3419 { 3420 this->in_function_ = f; 3421 this->in_function_index_ = index; 3422 } 3423 3424 // Return the name of the type. 3425 const std::string& 3426 name() const; 3427 3428 // Return the name of the type for an error message. The difference 3429 // is that if the type is defined in a different package, this will 3430 // return PACKAGE.NAME. 3431 std::string 3432 message_name() const; 3433 3434 // Return the underlying type. 3435 Type* 3436 real_type() 3437 { return this->type_; } 3438 3439 const Type* 3440 real_type() const 3441 { return this->type_; } 3442 3443 // Return the location. 3444 Location 3445 location() const 3446 { return this->location_; } 3447 3448 // Whether this type is visible. This only matters when parsing. 3449 bool 3450 is_visible() const 3451 { return this->is_visible_; } 3452 3453 // Mark this type as visible. 3454 void 3455 set_is_visible() 3456 { this->is_visible_ = true; } 3457 3458 // Mark this type as invisible. 3459 void 3460 clear_is_visible() 3461 { this->is_visible_ = false; } 3462 3463 // Whether this is a builtin type. 3464 bool 3465 is_builtin() const 3466 { return Linemap::is_predeclared_location(this->location_); } 3467 3468 // Whether this named type is valid. A recursive named type is invalid. 3469 bool 3470 is_valid() const 3471 { return !this->is_error_; } 3472 3473 // Return the base type for this type. 3474 Type* 3475 named_base(); 3476 3477 const Type* 3478 named_base() const; 3479 3480 // Return whether this is an error type. 3481 bool 3482 is_named_error_type() const; 3483 3484 // Return whether this type is comparable. If REASON is not NULL, 3485 // set *REASON when returning false. 3486 bool 3487 named_type_is_comparable(std::string* reason) const; 3488 3489 // Add a method to this type. 3490 Named_object* 3491 add_method(const std::string& name, Function*); 3492 3493 // Add a method declaration to this type. 3494 Named_object* 3495 add_method_declaration(const std::string& name, Package* package, 3496 Function_type* type, Location location); 3497 3498 // Add an existing method--one defined before the type itself was 3499 // defined--to a type. 3500 void 3501 add_existing_method(Named_object*); 3502 3503 // Look up a local method. 3504 Named_object* 3505 find_local_method(const std::string& name) const; 3506 3507 // Return the list of local methods. 3508 const Bindings* 3509 local_methods() const; 3510 3511 // Build the complete list of methods, including those from 3512 // anonymous fields, and build method stubs if needed. 3513 void 3514 finalize_methods(Gogo*); 3515 3516 // Return whether this type has any methods. This should only be 3517 // called after the finalize_methods pass. 3518 bool 3519 has_any_methods() const; 3520 3521 // Return the methods for this type. This should only be called 3522 // after the finalized_methods pass. 3523 const Methods* 3524 methods() const; 3525 3526 // Return the method to use for NAME. This returns NULL if there is 3527 // no such method or if the method is ambiguous. When it returns 3528 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous. 3529 Method* 3530 method_function(const std::string& name, bool *is_ambiguous) const; 3531 3532 // Return whether NAME is a known field or method which is not 3533 // exported. This is only used for better error reporting. 3534 bool 3535 is_unexported_local_method(Gogo*, const std::string& name) const; 3536 3537 // Return a pointer to the interface method table for this type for 3538 // the interface INTERFACE. If IS_POINTER is true, set the type 3539 // descriptor to a pointer to this type, otherwise set it to this 3540 // type. 3541 Expression* 3542 interface_method_table(Interface_type* interface, bool is_pointer); 3543 3544 // Note that a type must be converted to the backend representation 3545 // before we convert this type. 3546 void 3547 add_dependency(Named_type* nt) 3548 { this->dependencies_.push_back(nt); } 3549 3550 // Return true if the size and alignment of the backend 3551 // representation of this type is known. This is always true after 3552 // types have been converted, but may be false beforehand. 3553 bool 3554 is_named_backend_type_size_known() const 3555 { return this->named_btype_ != NULL && !this->is_placeholder_; } 3556 3557 // Add to the reflection string as for Type::append_reflection, but 3558 // if USE_ALIAS use the alias name rather than the alias target. 3559 void 3560 append_reflection_type_name(Gogo*, bool use_alias, std::string*) const; 3561 3562 // Append the mangled type name as for Type::append_mangled_name, 3563 // but if USE_ALIAS use the alias name rather than the alias target. 3564 void 3565 append_mangled_type_name(Gogo*, bool use_alias, std::string*) const; 3566 3567 // Import a named type. 3568 static void 3569 import_named_type(Import*, Named_type**); 3570 3571 // Initial conversion to backend representation. 3572 void 3573 convert(Gogo*); 3574 3575 protected: 3576 int 3577 do_traverse(Traverse* traverse) 3578 { return Type::traverse(this->type_, traverse); } 3579 3580 bool 3581 do_verify(); 3582 3583 bool 3584 do_has_pointer() const; 3585 3586 bool 3587 do_compare_is_identity(Gogo*); 3588 3589 bool 3590 do_is_reflexive(); 3591 3592 bool 3593 do_needs_key_update(); 3594 3595 bool 3596 do_in_heap() 3597 { return this->in_heap_ && this->type_->in_heap(); } 3598 3599 unsigned int 3600 do_hash_for_method(Gogo*, int) const; 3601 3602 Btype* 3603 do_get_backend(Gogo*); 3604 3605 Expression* 3606 do_type_descriptor(Gogo*, Named_type*); 3607 3608 void 3609 do_reflection(Gogo*, std::string*) const; 3610 3611 void 3612 do_mangled_name(Gogo*, std::string* ret) const; 3613 3614 void 3615 do_export(Export*) const; 3616 3617 private: 3618 // Create the placeholder during conversion. 3619 void 3620 create_placeholder(Gogo*); 3621 3622 // A pointer back to the Named_object for this type. 3623 Named_object* named_object_; 3624 // If this type is defined in a function, a pointer back to the 3625 // function in which it is defined. 3626 Named_object* in_function_; 3627 // The index of this type in IN_FUNCTION_. 3628 unsigned int in_function_index_; 3629 // The actual type. 3630 Type* type_; 3631 // The list of methods defined for this type. Any named type can 3632 // have methods. 3633 Bindings* local_methods_; 3634 // The full list of methods for this type, including methods 3635 // declared for anonymous fields. 3636 Methods* all_methods_; 3637 // A mapping from interfaces to the associated interface method 3638 // tables for this type. 3639 Interface_method_tables* interface_method_tables_; 3640 // A mapping from interfaces to the associated interface method 3641 // tables for pointers to this type. 3642 Interface_method_tables* pointer_interface_method_tables_; 3643 // The location where this type was defined. 3644 Location location_; 3645 // The backend representation of this type during backend 3646 // conversion. This is used to avoid endless recursion when a named 3647 // type refers to itself. 3648 Btype* named_btype_; 3649 // A list of types which must be converted to the backend 3650 // representation before this type can be converted. This is for 3651 // cases like 3652 // type S1 { p *S2 } 3653 // type S2 { s S1 } 3654 // where we can't convert S2 to the backend representation unless we 3655 // have converted S1. 3656 std::vector<Named_type*> dependencies_; 3657 // Whether this is an alias type. 3658 bool is_alias_; 3659 // Whether this type is visible. This is false if this type was 3660 // created because it was referenced by an imported object, but the 3661 // type itself was not exported. This will always be true for types 3662 // created in the current package. 3663 bool is_visible_; 3664 // Whether this type is erroneous. 3665 bool is_error_; 3666 // Whether this type is permitted in the heap. This is true by 3667 // default, false if there is a magic //go:notinheap comment. 3668 bool in_heap_; 3669 // Whether the current value of named_btype_ is a placeholder for 3670 // which the final size of the type is not known. 3671 bool is_placeholder_; 3672 // Whether this type has been converted to the backend 3673 // representation. Implies that is_placeholder_ is false. 3674 bool is_converted_; 3675 // Whether this type has been verified. 3676 bool is_verified_; 3677 // In a recursive operation such as has_pointer, this flag is used 3678 // to prevent infinite recursion when a type refers to itself. This 3679 // is mutable because it is always reset to false when the function 3680 // exits. 3681 mutable bool seen_; 3682 // Like seen_, but used only by do_compare_is_identity. 3683 bool seen_in_compare_is_identity_; 3684 // Like seen_, but used only by do_get_backend. 3685 bool seen_in_get_backend_; 3686 // Like seen_, but used when resolving aliases. 3687 mutable bool seen_alias_; 3688 }; 3689 3690 // A forward declaration. This handles a type which has been declared 3691 // but not defined. 3692 3693 class Forward_declaration_type : public Type 3694 { 3695 public: 3696 Forward_declaration_type(Named_object* named_object); 3697 3698 // The named object associated with this type declaration. This 3699 // will be resolved. 3700 Named_object* 3701 named_object(); 3702 3703 const Named_object* 3704 named_object() const; 3705 3706 // Return the name of the type. 3707 const std::string& 3708 name() const; 3709 3710 // Return the type to which this points. Give an error if the type 3711 // has not yet been defined. 3712 Type* 3713 real_type(); 3714 3715 const Type* 3716 real_type() const; 3717 3718 // Whether the base type has been defined. 3719 bool 3720 is_defined() const; 3721 3722 // Add a method to this type. 3723 Named_object* 3724 add_method(const std::string& name, Function*); 3725 3726 // Add a method declaration to this type. 3727 Named_object* 3728 add_method_declaration(const std::string& name, Package*, Function_type*, 3729 Location); 3730 3731 // Add an already created object as a method to this type. 3732 void 3733 add_existing_method(Named_object*); 3734 3735 protected: 3736 int 3737 do_traverse(Traverse* traverse); 3738 3739 bool 3740 do_verify(); 3741 3742 bool 3743 do_has_pointer() const 3744 { return this->real_type()->has_pointer(); } 3745 3746 bool 3747 do_compare_is_identity(Gogo* gogo) 3748 { return this->real_type()->compare_is_identity(gogo); } 3749 3750 bool 3751 do_is_reflexive() 3752 { return this->real_type()->is_reflexive(); } 3753 3754 bool 3755 do_needs_key_update() 3756 { return this->real_type()->needs_key_update(); } 3757 3758 bool 3759 do_in_heap() 3760 { return this->real_type()->in_heap(); } 3761 3762 unsigned int 3763 do_hash_for_method(Gogo* gogo, int flags) const 3764 { return this->real_type()->hash_for_method(gogo, flags); } 3765 3766 Btype* 3767 do_get_backend(Gogo* gogo); 3768 3769 Expression* 3770 do_type_descriptor(Gogo*, Named_type*); 3771 3772 void 3773 do_reflection(Gogo*, std::string*) const; 3774 3775 void 3776 do_mangled_name(Gogo*, std::string* ret) const; 3777 3778 void 3779 do_export(Export*) const; 3780 3781 private: 3782 // Issue a warning about a use of an undefined type. 3783 void 3784 warn() const; 3785 3786 // The type declaration. 3787 Named_object* named_object_; 3788 // Whether we have issued a warning about this type. 3789 mutable bool warned_; 3790 }; 3791 3792 // The Type_context struct describes what we expect for the type of an 3793 // expression. 3794 3795 struct Type_context 3796 { 3797 // The exact type we expect, if known. This may be NULL. 3798 Type* type; 3799 // Whether an abstract type is permitted. 3800 bool may_be_abstract; 3801 3802 // Constructors. 3803 Type_context() 3804 : type(NULL), may_be_abstract(false) 3805 { } 3806 3807 Type_context(Type* a_type, bool a_may_be_abstract) 3808 : type(a_type), may_be_abstract(a_may_be_abstract) 3809 { } 3810 }; 3811 3812 #endif // !defined(GO_TYPES_H)