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