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