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