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