1 // types.h -- Go frontend types. -*- C++ -*-
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.
10 #include "go-linemap.h"
15 class Typed_identifier;
16 class Typed_identifier_list;
23 class Struct_field_list;
31 class Forward_declaration_type;
34 class Type_hash_identical;
37 class Expression_list;
38 class Call_expression;
39 class Field_reference_expression;
40 class Bound_method_expression;
44 class Translate_context;
51 // Type codes used in type descriptors. These must match the values
52 // in libgo/runtime/go-type.h. They also match the values in the gc
53 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
54 // although this is not required.
56 static const int RUNTIME_TYPE_KIND_BOOL = 1;
57 static const int RUNTIME_TYPE_KIND_INT = 2;
58 static const int RUNTIME_TYPE_KIND_INT8 = 3;
59 static const int RUNTIME_TYPE_KIND_INT16 = 4;
60 static const int RUNTIME_TYPE_KIND_INT32 = 5;
61 static const int RUNTIME_TYPE_KIND_INT64 = 6;
62 static const int RUNTIME_TYPE_KIND_UINT = 7;
63 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
64 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
65 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
66 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
67 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
68 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
69 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
70 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
71 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
72 static const int RUNTIME_TYPE_KIND_ARRAY = 17;
73 static const int RUNTIME_TYPE_KIND_CHAN = 18;
74 static const int RUNTIME_TYPE_KIND_FUNC = 19;
75 static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
76 static const int RUNTIME_TYPE_KIND_MAP = 21;
77 static const int RUNTIME_TYPE_KIND_PTR = 22;
78 static const int RUNTIME_TYPE_KIND_SLICE = 23;
79 static const int RUNTIME_TYPE_KIND_STRING = 24;
80 static const int RUNTIME_TYPE_KIND_STRUCT = 25;
81 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
83 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
85 // To build the complete list of methods for a named type we need to
86 // gather all methods from anonymous fields. Those methods may
87 // require an arbitrary set of indirections and field offsets. There
88 // is also the possibility of ambiguous methods, which we could ignore
89 // except that we want to give a better error message for that case.
90 // This is a base class. There are two types of methods: named
91 // methods, and methods which are inherited from an anonymous field of
97 // For methods in anonymous types we need to know the sequence of
98 // field references used to extract the pointer to pass to the
99 // method. Since each method for a particular anonymous field will
100 // have the sequence of field indexes, and since the indexes can be
101 // shared going down the chain, we use a manually managed linked
102 // list. The first entry in the list is the field index for the
103 // last field, the one passed to the method.
107 const Field_indexes* next;
108 unsigned int field_index;
114 // Get the list of field indexes.
116 field_indexes() const
117 { return this->field_indexes_; }
122 { return this->depth_; }
124 // Return whether this is a value method--a method which does not
125 // require a pointer expression.
127 is_value_method() const
128 { return this->is_value_method_; }
130 // Return whether we need a stub method--this is true if we can't
131 // just pass the main object to the method.
133 needs_stub_method() const
134 { return this->needs_stub_method_; }
136 // Return whether this is an ambiguous method name.
139 { return this->is_ambiguous_; }
141 // Note that this method is ambiguous.
144 { this->is_ambiguous_ = true; }
146 // Return the type of the method.
149 { return this->do_type(); }
151 // Return the location of the method receiver.
153 receiver_location() const
154 { return this->do_receiver_location(); }
156 // Return an expression which binds this method to EXPR. This is
157 // something which can be used with a function call.
159 bind_method(Expression* expr, Location location) const;
161 // Return the named object for this method. This may only be called
162 // after methods are finalized.
164 named_object() const;
166 // Get the stub object.
170 go_assert(this->stub_ != NULL);
174 // Set the stub object.
176 set_stub_object(Named_object* no)
178 go_assert(this->stub_ == NULL);
183 // These objects are only built by the child classes.
184 Method(const Field_indexes* field_indexes, unsigned int depth,
185 bool is_value_method, bool needs_stub_method)
186 : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
187 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
191 // The named object for this method.
192 virtual Named_object*
193 do_named_object() const = 0;
195 // The type of the method.
196 virtual Function_type*
199 // Return the location of the method receiver.
201 do_receiver_location() const = 0;
203 // Bind a method to an object.
205 do_bind_method(Expression* expr, Location location) const = 0;
208 // The sequence of field indexes used for this method. If this is
209 // NULL, then the method is defined for the current type.
210 const Field_indexes* field_indexes_;
211 // The depth at which this method was found.
213 // If a stub method is required, this is its object. This is only
214 // set after stub methods are built in finalize_methods.
216 // Whether this is a value method--a method that does not require a
218 bool is_value_method_;
219 // Whether a stub method is required.
220 bool needs_stub_method_;
221 // Whether this method is ambiguous.
225 // A named method. This is what you get with a method declaration,
226 // either directly on the type, or inherited from some anonymous
229 class Named_method : public Method
232 Named_method(Named_object* named_object, const Field_indexes* field_indexes,
233 unsigned int depth, bool is_value_method,
234 bool needs_stub_method)
235 : Method(field_indexes, depth, is_value_method, needs_stub_method),
236 named_object_(named_object)
240 // Get the Named_object for the method.
242 do_named_object() const
243 { return this->named_object_; }
245 // The type of the method.
249 // Return the location of the method receiver.
251 do_receiver_location() const;
253 // Bind a method to an object.
255 do_bind_method(Expression* expr, Location location) const;
258 // The method itself. For a method which needs a stub, this starts
259 // out as the underlying method, and is later replaced with the stub
261 Named_object* named_object_;
264 // An interface method. This is used when an interface appears as an
265 // anonymous field in a named struct.
267 class Interface_method : public Method
270 Interface_method(const std::string& name, Location location,
271 Function_type* fntype, const Field_indexes* field_indexes,
273 : Method(field_indexes, depth, true, true),
274 name_(name), location_(location), fntype_(fntype)
278 // Get the Named_object for the method. This should never be
279 // called, as we always create a stub.
281 do_named_object() const
282 { go_unreachable(); }
284 // The type of the method.
287 { return this->fntype_; }
289 // Return the location of the method receiver.
291 do_receiver_location() const
292 { return this->location_; }
294 // Bind a method to an object.
296 do_bind_method(Expression* expr, Location location) const;
299 // The name of the interface method to call.
301 // The location of the definition of the interface method.
303 // The type of the interface method.
304 Function_type* fntype_;
307 // A mapping from method name to Method. This is a wrapper around a
313 typedef Unordered_map(std::string, Method*) Method_map;
316 typedef Method_map::const_iterator const_iterator;
322 // Insert a new method. Returns true if it was inserted, false if
323 // it was overidden or ambiguous.
325 insert(const std::string& name, Method* m);
327 // The number of (unambiguous) methods.
334 { return this->methods_.begin(); }
338 { return this->methods_.end(); }
342 find(const std::string& name) const
343 { return this->methods_.find(name); }
349 // The base class for all types.
354 // The types of types.
355 enum Type_classification
368 TYPE_CALL_MULTIPLE_RESULT,
388 // Get the unnamed bool type.
392 // Get the named type "bool".
396 // Make the named type "bool".
398 make_named_bool_type();
400 // Make an abstract integer type.
402 make_abstract_integer_type();
404 // Make an abstract type for a character constant.
406 make_abstract_character_type();
408 // Make a named integer type with a specified size.
409 // RUNTIME_TYPE_KIND is the code to use in reflection information,
410 // to distinguish int and int32.
412 make_integer_type(const char* name, bool is_unsigned, int bits,
413 int runtime_type_kind);
415 // Look up a named integer type.
417 lookup_integer_type(const char* name);
419 // Make an abstract floating point type.
421 make_abstract_float_type();
423 // Make a named floating point type with a specific size.
424 // RUNTIME_TYPE_KIND is the code to use in reflection information,
425 // to distinguish float and float32.
427 make_float_type(const char* name, int bits, int runtime_type_kind);
429 // Look up a named float type.
431 lookup_float_type(const char* name);
433 // Make an abstract complex type.
435 make_abstract_complex_type();
437 // Make a named complex type with a specific size.
438 // RUNTIME_TYPE_KIND is the code to use in reflection information,
439 // to distinguish complex and complex64.
441 make_complex_type(const char* name, int bits, int runtime_type_kind);
443 // Look up a named complex type.
445 lookup_complex_type(const char* name);
447 // Get the unnamed string type.
451 // Get the named type "string".
453 lookup_string_type();
455 // Make the named type "string".
457 make_named_string_type();
462 static Function_type*
463 make_function_type(Typed_identifier* receiver,
464 Typed_identifier_list* parameters,
465 Typed_identifier_list* results,
469 make_pointer_type(Type*);
475 make_call_multiple_result_type(Call_expression*);
478 make_struct_type(Struct_field_list* fields, Location);
481 make_array_type(Type* element_type, Expression* length);
484 make_map_type(Type* key_type, Type* value_type, Location);
487 make_channel_type(bool send, bool receive, Type*);
489 static Interface_type*
490 make_interface_type(Typed_identifier_list* methods, Location);
492 static Interface_type*
493 make_empty_interface_type(Location);
496 make_type_descriptor_type();
499 make_type_descriptor_ptr_type();
502 make_named_type(Named_object*, Type*, Location);
505 make_forward_declaration(Named_object*);
509 traverse(Type*, Traverse*);
511 // Verify the type. This is called after parsing, and verifies that
512 // types are complete and meet the language requirements. This
513 // returns false if the type is invalid.
516 { return this->do_verify(); }
518 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
519 // returns that an erroneous type is identical to any other type;
520 // this is used to avoid cascading errors. If this returns false,
521 // and REASON is not NULL, it may set *REASON.
523 are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
524 std::string* reason);
526 // Return true if two types are compatible for use in a binary
527 // operation, other than a shift, comparison, or channel send. This
528 // is an equivalence relation.
530 are_compatible_for_binop(const Type* t1, const Type* t2);
532 // Return true if two types are compatible for use with the
533 // comparison operator. IS_EQUALITY_OP is true if this is an
534 // equality comparison, false if it is an ordered comparison. This
535 // is an equivalence relation. If this returns false, and REASON is
536 // not NULL, it sets *REASON.
538 are_compatible_for_comparison(bool is_equality_op, const Type *t1,
539 const Type *t2, std::string* reason);
541 // Return true if a type is comparable with itself. This is true of
542 // most types, but false for, e.g., function types.
544 is_comparable() const
545 { return Type::are_compatible_for_comparison(true, this, this, NULL); }
547 // Return true if a value with type RHS is assignable to a variable
548 // with type LHS. This is not an equivalence relation. If this
549 // returns false, and REASON is not NULL, it sets *REASON.
551 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
553 // Return true if a value with type RHS is assignable to a variable
554 // with type LHS, ignoring any assignment of hidden fields
555 // (unexported fields of a type imported from another package).
556 // This is like the are_assignable method.
558 are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
559 std::string* reason);
561 // Return true if a value with type RHS may be converted to type
562 // LHS. If this returns false, and REASON is not NULL, it sets
565 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
567 // Whether this type has any hidden fields which are not visible in
568 // the current compilation, such as a field whose name begins with a
569 // lower case letter in a struct imported from a different package.
570 // WITHIN is not NULL if we are looking at fields in a named type.
572 has_hidden_fields(const Named_type* within, std::string* reason) const;
574 // Return true if values of this type can be compared using an
575 // identity function which gets nothing but a pointer to the value
578 compare_is_identity(Gogo* gogo) const
579 { return this->do_compare_is_identity(gogo); }
581 // Return a hash code for this type for the method hash table.
582 // Types which are equivalent according to are_identical will have
583 // the same hash code.
585 hash_for_method(Gogo*) const;
587 // Return the type classification.
589 classification() const
590 { return this->classification_; }
592 // Return the base type for this type. This looks through forward
593 // declarations and names. Using this with a forward declaration
594 // which has not been defined will return an error type.
601 // Return the type skipping defined forward declarations. If this
602 // type is a forward declaration which has not been defined, it will
603 // return the Forward_declaration_type. This differs from base() in
604 // that it will return a Named_type, and for a
605 // Forward_declaration_type which is not defined it will return that
606 // type rather than an error type.
613 // Return true if this is a basic type: a type which is not composed
614 // of other types, and is not void.
616 is_basic_type() const;
618 // Return true if this is an abstract type--an integer, floating
619 // point, or complex type whose size has not been determined.
623 // Return a non-abstract version of an abstract type.
625 make_non_abstract_type();
627 // Return true if this type is or contains a pointer. This
628 // determines whether the garbage collector needs to look at a value
632 { return this->do_has_pointer(); }
634 // Return true if this is the error type. This returns false for a
635 // type which is not defined, as it is called by the parser before
636 // all types are defined.
638 is_error_type() const;
640 // Return true if this is the error type or if the type is
641 // undefined. If the type is undefined, this will give an error.
642 // This should only be called after parsing is complete.
645 { return this->base()->is_error_type(); }
647 // Return true if this is a void type.
650 { return this->classification_ == TYPE_VOID; }
652 // If this is an integer type, return the Integer_type. Otherwise,
653 // return NULL. This is a controlled dynamic_cast.
656 { return this->convert<Integer_type, TYPE_INTEGER>(); }
660 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
662 // If this is a floating point type, return the Float_type.
663 // Otherwise, return NULL. This is a controlled dynamic_cast.
666 { return this->convert<Float_type, TYPE_FLOAT>(); }
670 { return this->convert<const Float_type, TYPE_FLOAT>(); }
672 // If this is a complex type, return the Complex_type. Otherwise,
676 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
680 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
682 // Return true if this is a boolean type.
684 is_boolean_type() const
685 { return this->base()->classification_ == TYPE_BOOLEAN; }
687 // Return true if this is an abstract boolean type.
689 is_abstract_boolean_type() const
690 { return this->classification_ == TYPE_BOOLEAN; }
692 // Return true if this is a string type.
694 is_string_type() const
695 { return this->base()->classification_ == TYPE_STRING; }
697 // Return true if this is an abstract string type.
699 is_abstract_string_type() const
700 { return this->classification_ == TYPE_STRING; }
702 // Return true if this is the sink type. This is the type of the
703 // blank identifier _.
706 { return this->base()->classification_ == TYPE_SINK; }
708 // If this is a function type, return it. Otherwise, return NULL.
711 { return this->convert<Function_type, TYPE_FUNCTION>(); }
714 function_type() const
715 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
717 // If this is a pointer type, return the type to which it points.
718 // Otherwise, return NULL.
722 // If this is a pointer type, return the type to which it points.
723 // Otherwise, return the type itself.
727 Type* pt = this->points_to();
728 return pt != NULL ? pt : this;
734 const Type* pt = this->points_to();
735 return pt != NULL ? pt : this;
738 // Return true if this is the nil type. We don't use base() here,
739 // because this can be called during parse, and there is no way to
740 // name the nil type anyhow.
743 { return this->classification_ == TYPE_NIL; }
745 // Return true if this is the predeclared constant nil being used as
746 // a type. This is what the parser produces for type switches which
749 is_nil_constant_as_type() const;
751 // Return true if this is the return type of a function which
752 // returns multiple values.
754 is_call_multiple_result_type() const
755 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
757 // If this is a struct type, return it. Otherwise, return NULL.
760 { return this->convert<Struct_type, TYPE_STRUCT>(); }
764 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
766 // If this is an array type, return it. Otherwise, return NULL.
769 { return this->convert<Array_type, TYPE_ARRAY>(); }
773 { return this->convert<const Array_type, TYPE_ARRAY>(); }
775 // Return whether if this is a slice type.
777 is_slice_type() const;
779 // If this is a map type, return it. Otherwise, return NULL.
782 { return this->convert<Map_type, TYPE_MAP>(); }
786 { return this->convert<const Map_type, TYPE_MAP>(); }
788 // If this is a channel type, return it. Otherwise, return NULL.
791 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
795 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
797 // If this is an interface type, return it. Otherwise, return NULL.
800 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
802 const Interface_type*
803 interface_type() const
804 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
806 // If this is a named type, return it. Otherwise, return NULL.
813 // If this is a forward declaration, return it. Otherwise, return
815 Forward_declaration_type*
816 forward_declaration_type()
817 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
819 const Forward_declaration_type*
820 forward_declaration_type() const
822 return this->convert_no_base<const Forward_declaration_type,
826 // Return true if this type is not yet defined.
828 is_undefined() const;
830 // Return true if this is the unsafe.pointer type. We currently
831 // represent that as pointer-to-void.
833 is_unsafe_pointer_type() const
834 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
836 // Look for field or method NAME for TYPE. Return an expression for
837 // it, bound to EXPR.
839 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
840 const std::string& name, Location);
842 // Return true if NAME is an unexported field or method of TYPE.
844 is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
845 std::vector<const Named_type*>*);
847 // Convert the builtin named types.
849 convert_builtin_named_types(Gogo*);
851 // Return the backend representation of this type.
855 // Return a placeholder for the backend representation of the type.
856 // This will return a type of the correct size, but for which some
857 // of the fields may still need to be completed.
859 get_backend_placeholder(Gogo*);
861 // Finish the backend representation of a placeholder.
863 finish_backend(Gogo*);
865 // Build a type descriptor entry for this type. Return a pointer to
866 // it. The location is the location which causes us to need the
869 type_descriptor_pointer(Gogo* gogo, Location);
871 // Return the type reflection string for this type.
873 reflection(Gogo*) const;
875 // Return a mangled name for the type. This is a name which can be
876 // used in assembler code. Identical types should have the same
879 mangled_name(Gogo*) const;
881 // If the size of the type can be determined, set *PSIZE to the size
882 // in bytes and return true. Otherwise, return false. This queries
885 backend_type_size(Gogo*, unsigned int* psize);
887 // If the alignment of the type can be determined, set *PALIGN to
888 // the alignment in bytes and return true. Otherwise, return false.
890 backend_type_align(Gogo*, unsigned int* palign);
892 // If the alignment of a struct field of this type can be
893 // determined, set *PALIGN to the alignment in bytes and return
894 // true. Otherwise, return false.
896 backend_type_field_align(Gogo*, unsigned int* palign);
898 // Whether the backend size is known.
900 is_backend_type_size_known(Gogo*);
902 // Get the hash and equality functions for a type.
904 type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
905 Function_type* equal_fntype, Named_object** hash_fn,
906 Named_object** equal_fn);
908 // Write the hash and equality type functions.
910 write_specific_type_functions(Gogo*, Named_type*,
911 const std::string& hash_name,
912 Function_type* hash_fntype,
913 const std::string& equal_name,
914 Function_type* equal_fntype);
918 export_type(Export* exp) const
919 { this->do_export(exp); }
923 import_type(Import*);
926 Type(Type_classification);
928 // Functions implemented by the child class.
930 // Traverse the subtypes.
932 do_traverse(Traverse*);
940 do_has_pointer() const
944 do_compare_is_identity(Gogo*) const = 0;
947 do_hash_for_method(Gogo*) const;
950 do_get_backend(Gogo*) = 0;
953 do_type_descriptor(Gogo*, Named_type* name) = 0;
956 do_reflection(Gogo*, std::string*) const = 0;
959 do_mangled_name(Gogo*, std::string*) const = 0;
962 do_export(Export*) const;
964 // Return whether a method expects a pointer as the receiver.
966 method_expects_pointer(const Named_object*);
968 // Finalize the methods for a type.
970 finalize_methods(Gogo*, const Type*, Location, Methods**);
972 // Return a method from a set of methods.
974 method_function(const Methods*, const std::string& name,
977 // Return a composite literal for the type descriptor entry for a
980 type_descriptor(Gogo*, Type*);
982 // Return a composite literal for the type descriptor entry for
983 // TYPE, using NAME as the name of the type.
985 named_type_descriptor(Gogo*, Type* type, Named_type* name);
987 // Return a composite literal for a plain type descriptor for this
988 // type with the given kind and name.
990 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
992 // Build a composite literal for the basic type descriptor.
994 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
995 const Methods*, bool only_value_methods);
997 // Make a builtin struct type from a list of fields.
999 make_builtin_struct_type(int nfields, ...);
1001 // Make a builtin named type.
1003 make_builtin_named_type(const char* name, Type* type);
1005 // For the benefit of child class reflection string generation.
1007 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
1008 { type->do_reflection(gogo, ret); }
1010 // For the benefit of child class mangling.
1012 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
1013 { type->do_mangled_name(gogo, ret); }
1015 // Incorporate a string into a hash code.
1017 hash_string(const std::string&, unsigned int);
1019 // Return the backend representation for the underlying type of a
1022 get_named_base_btype(Gogo* gogo, Type* base_type)
1023 { return base_type->get_btype_without_hash(gogo); }
1026 // Convert to the desired type classification, or return NULL. This
1027 // is a controlled dynamic_cast.
1028 template<typename Type_class, Type_classification type_classification>
1032 Type* base = this->base();
1033 return (base->classification_ == type_classification
1034 ? static_cast<Type_class*>(base)
1038 template<typename Type_class, Type_classification type_classification>
1042 const Type* base = this->base();
1043 return (base->classification_ == type_classification
1044 ? static_cast<Type_class*>(base)
1048 template<typename Type_class, Type_classification type_classification>
1052 return (this->classification_ == type_classification
1053 ? static_cast<Type_class*>(this)
1057 template<typename Type_class, Type_classification type_classification>
1059 convert_no_base() const
1061 return (this->classification_ == type_classification
1062 ? static_cast<Type_class*>(this)
1066 // Support for are_assignable and are_assignable_hidden_ok.
1068 are_assignable_check_hidden(const Type* lhs, const Type* rhs,
1069 bool check_hidden_fields, std::string* reason);
1071 // Map unnamed types to type descriptor decls.
1072 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1073 Type_identical) Type_descriptor_vars;
1075 static Type_descriptor_vars type_descriptor_vars;
1077 // Build the type descriptor variable for this type.
1079 make_type_descriptor_var(Gogo*);
1081 // Return the name of the type descriptor variable. If NAME is not
1082 // NULL, it is the name to use.
1084 type_descriptor_var_name(Gogo*, Named_type* name);
1086 // Return true if the type descriptor for this type should be
1087 // defined in some other package. If NAME is not NULL, it is the
1088 // name of this type. If this returns true it sets *PACKAGE to the
1089 // package where the type descriptor is defined.
1091 type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
1093 // Build the hash and equality type functions for a type which needs
1094 // specific functions.
1096 specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
1097 Function_type* equal_fntype, Named_object** hash_fn,
1098 Named_object** equal_fn);
1100 // Build a composite literal for the uncommon type information.
1102 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1103 Named_type*, const Methods*,
1104 bool only_value_methods) const;
1106 // Build a composite literal for the methods.
1108 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1109 bool only_value_methods) const;
1111 // Build a composite literal for one method.
1113 method_constructor(Gogo*, Type* method_type, const std::string& name,
1114 const Method*, bool only_value_methods) const;
1117 build_receive_return_type(tree type);
1119 // A hash table we use to avoid infinite recursion.
1120 typedef Unordered_set_hash(const Named_type*, Type_hash_identical,
1121 Type_identical) Types_seen;
1123 // Add all methods for TYPE to the list of methods for THIS.
1125 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1126 unsigned int depth, bool, bool, Types_seen*,
1130 add_local_methods_for_type(const Named_type* type,
1131 const Method::Field_indexes*,
1132 unsigned int depth, bool, bool, Methods**);
1135 add_embedded_methods_for_type(const Type* type,
1136 const Method::Field_indexes*,
1137 unsigned int depth, bool, bool, Types_seen*,
1141 add_interface_methods_for_type(const Type* type,
1142 const Method::Field_indexes*,
1143 unsigned int depth, Methods**);
1145 // Build stub methods for a type.
1147 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1151 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1152 const Typed_identifier_list*, bool is_varargs,
1156 apply_field_indexes(Expression*, const Method::Field_indexes*,
1159 // Look for a field or method named NAME in TYPE.
1161 find_field_or_method(const Type* type, const std::string& name,
1162 bool receiver_can_be_pointer,
1163 std::vector<const Named_type*>*, int* level,
1164 bool* is_method, bool* found_pointer_method,
1165 std::string* ambig1, std::string* ambig2);
1167 // Get the backend representation for a type without looking in the
1168 // hash table for identical types.
1170 get_btype_without_hash(Gogo*);
1172 // A mapping from Type to Btype*, used to ensure that the backend
1173 // representation of identical types is identical.
1174 typedef Unordered_map_hash(const Type*, Btype*, Type_hash_identical,
1175 Type_identical) Type_btypes;
1177 static Type_btypes type_btypes;
1179 // A list of builtin named types.
1180 static std::vector<Named_type*> named_builtin_types;
1182 // A map from types which need specific type functions to the type
1183 // functions themselves.
1184 typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
1185 typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
1186 Type_identical) Type_functions;
1188 static Type_functions type_functions_table;
1190 // The type classification.
1191 Type_classification classification_;
1192 // Whether btype_ is a placeholder type used while named types are
1194 bool btype_is_placeholder_;
1195 // The backend representation of the type, once it has been
1198 // The type descriptor for this type. This starts out as NULL and
1199 // is filled in as needed.
1200 Bvariable* type_descriptor_var_;
1203 // Type hash table operations.
1205 class Type_hash_identical
1209 operator()(const Type* type) const
1210 { return type->hash_for_method(NULL); }
1213 class Type_identical
1217 operator()(const Type* t1, const Type* t2) const
1218 { return Type::are_identical(t1, t2, false, NULL); }
1221 // An identifier with a type.
1223 class Typed_identifier
1226 Typed_identifier(const std::string& name, Type* type,
1228 : name_(name), type_(type), location_(location)
1234 { return this->name_; }
1239 { return this->type_; }
1241 // Return the location where the name was seen. This is not always
1245 { return this->location_; }
1247 // Set the type--sometimes we see the identifier before the type.
1249 set_type(Type* type)
1251 go_assert(this->type_ == NULL || type->is_error_type());
1260 // The location where the name was seen.
1264 // A list of Typed_identifiers.
1266 class Typed_identifier_list
1269 Typed_identifier_list()
1273 // Whether the list is empty.
1276 { return this->entries_.empty(); }
1278 // Return the number of entries in the list.
1281 { return this->entries_.size(); }
1283 // Add an entry to the end of the list.
1285 push_back(const Typed_identifier& td)
1286 { this->entries_.push_back(td); }
1288 // Remove an entry from the end of the list.
1291 { this->entries_.pop_back(); }
1293 // Set the type of entry I to TYPE.
1295 set_type(size_t i, Type* type)
1297 go_assert(i < this->entries_.size());
1298 this->entries_[i].set_type(type);
1301 // Sort the entries by name.
1307 traverse(Traverse*);
1309 // Return the first and last elements.
1312 { return this->entries_.front(); }
1314 const Typed_identifier&
1316 { return this->entries_.front(); }
1320 { return this->entries_.back(); }
1322 const Typed_identifier&
1324 { return this->entries_.back(); }
1326 const Typed_identifier&
1328 { return this->entries_.at(i); }
1331 set(size_t i, const Typed_identifier& t)
1332 { this->entries_.at(i) = t; }
1337 go_assert(c <= this->entries_.size());
1338 this->entries_.resize(c, Typed_identifier("", NULL,
1339 Linemap::unknown_location()));
1344 { this->entries_.reserve(c); }
1348 typedef std::vector<Typed_identifier>::iterator iterator;
1349 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1353 { return this->entries_.begin(); }
1357 { return this->entries_.begin(); }
1361 { return this->entries_.end(); }
1365 { return this->entries_.end(); }
1367 // Return a copy of this list. This returns an independent copy of
1368 // the vector, but does not copy the types.
1369 Typed_identifier_list*
1373 std::vector<Typed_identifier> entries_;
1376 // The type of an integer.
1378 class Integer_type : public Type
1381 // Create a new integer type.
1383 create_integer_type(const char* name, bool is_unsigned, int bits,
1384 int runtime_type_kind);
1386 // Look up an existing integer type.
1388 lookup_integer_type(const char* name);
1390 // Create an abstract integer type.
1391 static Integer_type*
1392 create_abstract_integer_type();
1394 // Create an abstract character type.
1395 static Integer_type*
1396 create_abstract_character_type();
1398 // Whether this is an abstract integer type.
1401 { return this->is_abstract_; }
1403 // Whether this is an unsigned type.
1406 { return this->is_unsigned_; }
1408 // The number of bits.
1411 { return this->bits_; }
1413 // Whether this type is the same as T.
1415 is_identical(const Integer_type* t) const;
1417 // Whether this is the type "byte" or another name for "byte".
1420 { return this->is_byte_; }
1422 // Mark this as the "byte" type.
1425 { this->is_byte_ = true; }
1427 // Whether this is the type "rune" or another name for "rune".
1430 { return this->is_rune_; }
1432 // Mark this as the "rune" type.
1435 { this->is_rune_ = true; }
1439 do_compare_is_identity(Gogo*) const
1443 do_hash_for_method(Gogo*) const;
1446 do_get_backend(Gogo*);
1449 do_type_descriptor(Gogo*, Named_type*);
1452 do_reflection(Gogo*, std::string*) const;
1455 do_mangled_name(Gogo*, std::string*) const;
1458 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1459 int runtime_type_kind)
1460 : Type(TYPE_INTEGER),
1461 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1462 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1465 // Map names of integer types to the types themselves.
1466 typedef std::map<std::string, Named_type*> Named_integer_types;
1467 static Named_integer_types named_integer_types;
1469 // True if this is an abstract type.
1471 // True if this is an unsigned type.
1473 // True if this is the byte type.
1475 // True if this is the rune type.
1477 // The number of bits.
1479 // The runtime type code used in the type descriptor for this type.
1480 int runtime_type_kind_;
1483 // The type of a floating point number.
1485 class Float_type : public Type
1488 // Create a new float type.
1490 create_float_type(const char* name, int bits, int runtime_type_kind);
1492 // Look up an existing float type.
1494 lookup_float_type(const char* name);
1496 // Create an abstract float type.
1498 create_abstract_float_type();
1500 // Whether this is an abstract float type.
1503 { return this->is_abstract_; }
1505 // The number of bits.
1508 { return this->bits_; }
1510 // Whether this type is the same as T.
1512 is_identical(const Float_type* t) const;
1516 do_compare_is_identity(Gogo*) const
1520 do_hash_for_method(Gogo*) const;
1523 do_get_backend(Gogo*);
1526 do_type_descriptor(Gogo*, Named_type*);
1529 do_reflection(Gogo*, std::string*) const;
1532 do_mangled_name(Gogo*, std::string*) const;
1535 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1537 is_abstract_(is_abstract), bits_(bits),
1538 runtime_type_kind_(runtime_type_kind)
1541 // Map names of float types to the types themselves.
1542 typedef std::map<std::string, Named_type*> Named_float_types;
1543 static Named_float_types named_float_types;
1545 // True if this is an abstract type.
1547 // The number of bits in the floating point value.
1549 // The runtime type code used in the type descriptor for this type.
1550 int runtime_type_kind_;
1553 // The type of a complex number.
1555 class Complex_type : public Type
1558 // Create a new complex type.
1560 create_complex_type(const char* name, int bits, int runtime_type_kind);
1562 // Look up an existing complex type.
1564 lookup_complex_type(const char* name);
1566 // Create an abstract complex type.
1567 static Complex_type*
1568 create_abstract_complex_type();
1570 // Whether this is an abstract complex type.
1573 { return this->is_abstract_; }
1575 // The number of bits: 64 or 128.
1577 { return this->bits_; }
1579 // Whether this type is the same as T.
1581 is_identical(const Complex_type* t) const;
1585 do_compare_is_identity(Gogo*) const
1589 do_hash_for_method(Gogo*) const;
1592 do_get_backend(Gogo*);
1595 do_type_descriptor(Gogo*, Named_type*);
1598 do_reflection(Gogo*, std::string*) const;
1601 do_mangled_name(Gogo*, std::string*) const;
1604 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1605 : Type(TYPE_COMPLEX),
1606 is_abstract_(is_abstract), bits_(bits),
1607 runtime_type_kind_(runtime_type_kind)
1610 // Map names of complex types to the types themselves.
1611 typedef std::map<std::string, Named_type*> Named_complex_types;
1612 static Named_complex_types named_complex_types;
1614 // True if this is an abstract type.
1616 // The number of bits in the complex value--64 or 128.
1618 // The runtime type code used in the type descriptor for this type.
1619 int runtime_type_kind_;
1622 // The type of a string.
1624 class String_type : public Type
1631 // Return a tree for the length of STRING.
1633 length_tree(Gogo*, tree string);
1635 // Return a tree which points to the bytes of STRING.
1637 bytes_tree(Gogo*, tree string);
1641 do_has_pointer() const
1645 do_compare_is_identity(Gogo*) const
1649 do_get_backend(Gogo*);
1652 do_type_descriptor(Gogo*, Named_type*);
1655 do_reflection(Gogo*, std::string*) const;
1658 do_mangled_name(Gogo*, std::string* ret) const;
1661 // The named string type.
1662 static Named_type* string_type_;
1665 // The type of a function.
1667 class Function_type : public Type
1670 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1671 Typed_identifier_list* results, Location location)
1672 : Type(TYPE_FUNCTION),
1673 receiver_(receiver), parameters_(parameters), results_(results),
1674 location_(location), is_varargs_(false), is_builtin_(false)
1677 // Get the receiver.
1678 const Typed_identifier*
1680 { return this->receiver_; }
1682 // Get the return names and types.
1683 const Typed_identifier_list*
1685 { return this->results_; }
1687 // Get the parameter names and types.
1688 const Typed_identifier_list*
1690 { return this->parameters_; }
1692 // Whether this is a varargs function.
1695 { return this->is_varargs_; }
1697 // Whether this is a builtin function.
1700 { return this->is_builtin_; }
1702 // The location where this type was defined.
1705 { return this->location_; }
1707 // Return whether this is a method type.
1710 { return this->receiver_ != NULL; }
1712 // Whether T is a valid redeclaration of this type. This is called
1713 // when a function is declared more than once.
1715 is_valid_redeclaration(const Function_type* t, std::string*) const;
1717 // Whether this type is the same as T.
1719 is_identical(const Function_type* t, bool ignore_receiver,
1720 bool errors_are_identical, std::string*) const;
1722 // Record that this is a varargs function.
1725 { this->is_varargs_ = true; }
1727 // Record that this is a builtin function.
1730 { this->is_builtin_ = true; }
1732 // Import a function type.
1733 static Function_type*
1736 // Return a copy of this type without a receiver. This is only
1737 // valid for a method type.
1739 copy_without_receiver() const;
1741 // Return a copy of this type with a receiver. This is used when an
1742 // interface method is attached to a named or struct type.
1744 copy_with_receiver(Type*) const;
1747 make_function_type_descriptor_type();
1751 do_traverse(Traverse*);
1753 // A trampoline function has a pointer which matters for GC.
1755 do_has_pointer() const
1759 do_compare_is_identity(Gogo*) const
1763 do_hash_for_method(Gogo*) const;
1766 do_get_backend(Gogo*);
1769 do_type_descriptor(Gogo*, Named_type*);
1772 do_reflection(Gogo*, std::string*) const;
1775 do_mangled_name(Gogo*, std::string*) const;
1778 do_export(Export*) const;
1782 type_descriptor_params(Type*, const Typed_identifier*,
1783 const Typed_identifier_list*);
1785 // The receiver name and type. This will be NULL for a normal
1786 // function, non-NULL for a method.
1787 Typed_identifier* receiver_;
1788 // The parameter names and types.
1789 Typed_identifier_list* parameters_;
1790 // The result names and types. This will be NULL if no result was
1792 Typed_identifier_list* results_;
1793 // The location where this type was defined. This exists solely to
1794 // give a location for the fields of the struct if this function
1795 // returns multiple values.
1797 // Whether this function takes a variable number of arguments.
1799 // Whether this is a special builtin function which can not simply
1800 // be called. This is used for len, cap, etc.
1804 // The type of a pointer.
1806 class Pointer_type : public Type
1809 Pointer_type(Type* to_type)
1810 : Type(TYPE_POINTER),
1816 { return this->to_type_; }
1818 // Import a pointer type.
1819 static Pointer_type*
1823 make_pointer_type_descriptor_type();
1827 do_traverse(Traverse*);
1830 do_has_pointer() const
1834 do_compare_is_identity(Gogo*) const
1838 do_hash_for_method(Gogo*) const;
1841 do_get_backend(Gogo*);
1844 do_type_descriptor(Gogo*, Named_type*);
1847 do_reflection(Gogo*, std::string*) const;
1850 do_mangled_name(Gogo*, std::string*) const;
1853 do_export(Export*) const;
1856 // The type to which this type points.
1860 // The type of a field in a struct.
1865 explicit Struct_field(const Typed_identifier& typed_identifier)
1866 : typed_identifier_(typed_identifier), tag_(NULL)
1873 // Return whether this struct field is named NAME.
1875 is_field_name(const std::string& name) const;
1880 { return this->typed_identifier_.type(); }
1882 // The field location.
1885 { return this->typed_identifier_.location(); }
1887 // Whether the field has a tag.
1890 { return this->tag_ != NULL; }
1896 go_assert(this->tag_ != NULL);
1900 // Whether this is an anonymous field.
1902 is_anonymous() const
1903 { return this->typed_identifier_.name().empty(); }
1905 // Set the tag. FIXME: This is never freed.
1907 set_tag(const std::string& tag)
1908 { this->tag_ = new std::string(tag); }
1910 // Set the type. This is only used in error cases.
1912 set_type(Type* type)
1913 { this->typed_identifier_.set_type(type); }
1916 // The field name, type, and location.
1917 Typed_identifier typed_identifier_;
1918 // The field tag. This is NULL if the field has no tag.
1922 // A list of struct fields.
1924 class Struct_field_list
1931 // Whether the list is empty.
1934 { return this->entries_.empty(); }
1936 // Return the number of entries.
1939 { return this->entries_.size(); }
1941 // Add an entry to the end of the list.
1943 push_back(const Struct_field& sf)
1944 { this->entries_.push_back(sf); }
1946 // Index into the list.
1949 { return this->entries_.at(i); }
1951 // Last entry in list.
1954 { return this->entries_.back(); }
1958 typedef std::vector<Struct_field>::iterator iterator;
1959 typedef std::vector<Struct_field>::const_iterator const_iterator;
1963 { return this->entries_.begin(); }
1967 { return this->entries_.begin(); }
1971 { return this->entries_.end(); }
1975 { return this->entries_.end(); }
1978 std::vector<Struct_field> entries_;
1981 // The type of a struct.
1983 class Struct_type : public Type
1986 Struct_type(Struct_field_list* fields, Location location)
1987 : Type(TYPE_STRUCT),
1988 fields_(fields), location_(location), all_methods_(NULL)
1991 // Return the field NAME. This only looks at local fields, not at
1992 // embedded types. If the field is found, and PINDEX is not NULL,
1993 // this sets *PINDEX to the field index. If the field is not found,
1994 // this returns NULL.
1996 find_local_field(const std::string& name, unsigned int *pindex) const;
1998 // Return the field number INDEX.
2000 field(unsigned int index) const
2001 { return &this->fields_->at(index); }
2003 // Get the struct fields.
2004 const Struct_field_list*
2006 { return this->fields_; }
2008 // Return the number of fields.
2011 { return this->fields_->size(); }
2013 // Push a new field onto the end of the struct. This is used when
2014 // building a closure variable.
2016 push_field(const Struct_field& sf)
2017 { this->fields_->push_back(sf); }
2019 // Return an expression referring to field NAME in STRUCT_EXPR, or
2020 // NULL if there is no field with that name.
2021 Field_reference_expression*
2022 field_reference(Expression* struct_expr, const std::string& name,
2025 // Return the total number of fields, including embedded fields.
2026 // This is the number of values that can appear in a conversion to
2029 total_field_count() const;
2031 // Whether this type is identical with T.
2033 is_identical(const Struct_type* t, bool errors_are_identical) const;
2035 // Whether this struct type has any hidden fields. This returns
2036 // true if any fields have hidden names, or if any non-pointer
2037 // anonymous fields have types with hidden fields.
2039 struct_has_hidden_fields(const Named_type* within, std::string*) const;
2041 // Return whether NAME is a local field which is not exported. This
2042 // is only used for better error reporting.
2044 is_unexported_local_field(Gogo*, const std::string& name) const;
2046 // If this is an unnamed struct, build the complete list of methods,
2047 // including those from anonymous fields, and build methods stubs if
2050 finalize_methods(Gogo*);
2052 // Return whether this type has any methods. This should only be
2053 // called after the finalize_methods pass.
2055 has_any_methods() const
2056 { return this->all_methods_ != NULL; }
2058 // Return the methods for tihs type. This should only be called
2059 // after the finalize_methods pass.
2062 { return this->all_methods_; }
2064 // Return the method to use for NAME. This returns NULL if there is
2065 // no such method or if the method is ambiguous. When it returns
2066 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2068 method_function(const std::string& name, bool* is_ambiguous) const;
2070 // Traverse just the field types of a struct type.
2072 traverse_field_types(Traverse* traverse)
2073 { return this->do_traverse(traverse); }
2075 // If the offset of field INDEX in the backend implementation can be
2076 // determined, set *POFFSET to the offset in bytes and return true.
2077 // Otherwise, return false.
2079 backend_field_offset(Gogo*, unsigned int index, unsigned int* poffset);
2081 // Finish the backend representation of all the fields.
2083 finish_backend_fields(Gogo*);
2085 // Import a struct type.
2090 make_struct_type_descriptor_type();
2092 // Write the hash function for this type.
2094 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2096 // Write the equality function for this type.
2098 write_equal_function(Gogo*, Named_type*);
2102 do_traverse(Traverse*);
2108 do_has_pointer() const;
2111 do_compare_is_identity(Gogo*) const;
2114 do_hash_for_method(Gogo*) const;
2117 do_get_backend(Gogo*);
2120 do_type_descriptor(Gogo*, Named_type*);
2123 do_reflection(Gogo*, std::string*) const;
2126 do_mangled_name(Gogo*, std::string*) const;
2129 do_export(Export*) const;
2132 // Used to avoid infinite loops in field_reference_depth.
2133 struct Saw_named_type
2135 Saw_named_type* next;
2139 Field_reference_expression*
2140 field_reference_depth(Expression* struct_expr, const std::string& name,
2141 Location, Saw_named_type*,
2142 unsigned int* depth) const;
2144 // The fields of the struct.
2145 Struct_field_list* fields_;
2146 // The place where the struct was declared.
2148 // If this struct is unnamed, a list of methods.
2149 Methods* all_methods_;
2152 // The type of an array.
2154 class Array_type : public Type
2157 Array_type(Type* element_type, Expression* length)
2159 element_type_(element_type), length_(length), length_tree_(NULL)
2162 // Return the element type.
2164 element_type() const
2165 { return this->element_type_; }
2167 // Return the length. This will return NULL for an open array.
2170 { return this->length_; }
2172 // Whether this type is identical with T.
2174 is_identical(const Array_type* t, bool errors_are_identical) const;
2176 // Whether this type has any hidden fields.
2178 array_has_hidden_fields(const Named_type* within, std::string* reason) const
2179 { return this->element_type_->has_hidden_fields(within, reason); }
2181 // Return a tree for the pointer to the values in an array.
2183 value_pointer_tree(Gogo*, tree array) const;
2185 // Return a tree for the length of an array with this type.
2187 length_tree(Gogo*, tree array);
2189 // Return a tree for the capacity of an array with this type.
2191 capacity_tree(Gogo*, tree array);
2193 // Import an array type.
2197 // Return the backend representation of the element type.
2199 get_backend_element(Gogo*, bool use_placeholder);
2201 // Return the backend representation of the length.
2203 get_backend_length(Gogo*);
2205 // Finish the backend representation of the element type.
2207 finish_backend_element(Gogo*);
2210 make_array_type_descriptor_type();
2213 make_slice_type_descriptor_type();
2215 // Write the hash function for this type.
2217 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2219 // Write the equality function for this type.
2221 write_equal_function(Gogo*, Named_type*);
2225 do_traverse(Traverse* traverse);
2231 do_has_pointer() const
2233 return this->length_ == NULL || this->element_type_->has_pointer();
2237 do_compare_is_identity(Gogo*) const;
2240 do_hash_for_method(Gogo*) const;
2243 do_get_backend(Gogo*);
2246 do_type_descriptor(Gogo*, Named_type*);
2249 do_reflection(Gogo*, std::string*) const;
2252 do_mangled_name(Gogo*, std::string*) const;
2255 do_export(Export*) const;
2262 get_length_tree(Gogo*);
2265 array_type_descriptor(Gogo*, Named_type*);
2268 slice_type_descriptor(Gogo*, Named_type*);
2270 // The type of elements of the array.
2271 Type* element_type_;
2272 // The number of elements. This may be NULL.
2273 Expression* length_;
2274 // The length as a tree. We only want to compute this once.
2278 // The type of a map.
2280 class Map_type : public Type
2283 Map_type(Type* key_type, Type* val_type, Location location)
2285 key_type_(key_type), val_type_(val_type), location_(location)
2288 // Return the key type.
2291 { return this->key_type_; }
2293 // Return the value type.
2296 { return this->val_type_; }
2298 // Whether this type is identical with T.
2300 is_identical(const Map_type* t, bool errors_are_identical) const;
2302 // Import a map type.
2307 make_map_type_descriptor_type();
2310 make_map_descriptor_type();
2312 // Build a map descriptor for this type. Return a pointer to it.
2313 // The location is the location which causes us to need the
2316 map_descriptor_pointer(Gogo* gogo, Location);
2320 do_traverse(Traverse*);
2326 do_has_pointer() const
2330 do_compare_is_identity(Gogo*) const
2334 do_hash_for_method(Gogo*) const;
2337 do_get_backend(Gogo*);
2340 do_type_descriptor(Gogo*, Named_type*);
2343 do_reflection(Gogo*, std::string*) const;
2346 do_mangled_name(Gogo*, std::string*) const;
2349 do_export(Export*) const;
2352 // Mapping from map types to map descriptors.
2353 typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
2354 Type_identical) Map_descriptors;
2355 static Map_descriptors map_descriptors;
2358 map_descriptor(Gogo*);
2364 // Where the type was defined.
2368 // The type of a channel.
2370 class Channel_type : public Type
2373 Channel_type(bool may_send, bool may_receive, Type* element_type)
2374 : Type(TYPE_CHANNEL),
2375 may_send_(may_send), may_receive_(may_receive),
2376 element_type_(element_type)
2377 { go_assert(may_send || may_receive); }
2379 // Whether this channel can send data.
2382 { return this->may_send_; }
2384 // Whether this channel can receive data.
2387 { return this->may_receive_; }
2389 // The type of the values that may be sent on this channel. This is
2390 // NULL if any type may be sent.
2392 element_type() const
2393 { return this->element_type_; }
2395 // Whether this type is identical with T.
2397 is_identical(const Channel_type* t, bool errors_are_identical) const;
2399 // Import a channel type.
2400 static Channel_type*
2404 make_chan_type_descriptor_type();
2408 do_traverse(Traverse* traverse)
2409 { return Type::traverse(this->element_type_, traverse); }
2412 do_has_pointer() const
2416 do_compare_is_identity(Gogo*) const
2420 do_hash_for_method(Gogo*) const;
2423 do_get_backend(Gogo*);
2426 do_type_descriptor(Gogo*, Named_type*);
2429 do_reflection(Gogo*, std::string*) const;
2432 do_mangled_name(Gogo*, std::string*) const;
2435 do_export(Export*) const;
2438 // Whether this channel can send data.
2440 // Whether this channel can receive data.
2442 // The types of elements which may be sent on this channel. If this
2443 // is NULL, it means that any type may be sent.
2444 Type* element_type_;
2447 // An interface type.
2449 class Interface_type : public Type
2452 Interface_type(Typed_identifier_list* methods, Location location)
2453 : Type(TYPE_INTERFACE),
2454 parse_methods_(methods), all_methods_(NULL), location_(location),
2455 interface_btype_(NULL), assume_identical_(NULL),
2456 methods_are_finalized_(false), seen_(false)
2457 { go_assert(methods == NULL || !methods->empty()); }
2459 // The location where the interface type was defined.
2462 { return this->location_; }
2464 // Return whether this is an empty interface.
2468 go_assert(this->methods_are_finalized_);
2469 return this->all_methods_ == NULL;
2472 // Return the list of methods. This will return NULL for an empty
2474 const Typed_identifier_list*
2477 go_assert(this->methods_are_finalized_);
2478 return this->all_methods_;
2481 // Return the number of methods.
2483 method_count() const
2485 go_assert(this->methods_are_finalized_);
2486 return this->all_methods_ == NULL ? 0 : this->all_methods_->size();
2489 // Return the method NAME, or NULL.
2490 const Typed_identifier*
2491 find_method(const std::string& name) const;
2493 // Return the zero-based index of method NAME.
2495 method_index(const std::string& name) const;
2497 // Finalize the methods. This sets all_methods_. This handles
2498 // interface inheritance.
2502 // Return true if T implements this interface. If this returns
2503 // false, and REASON is not NULL, it sets *REASON to the reason that
2506 implements_interface(const Type* t, std::string* reason) const;
2508 // Whether this type is identical with T. REASON is as in
2509 // implements_interface.
2511 is_identical(const Interface_type* t, bool errors_are_identical) const;
2513 // Whether we can assign T to this type. is_identical is known to
2516 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2518 // Return whether NAME is a method which is not exported. This is
2519 // only used for better error reporting.
2521 is_unexported_method(Gogo*, const std::string& name) const;
2523 // Import an interface type.
2524 static Interface_type*
2527 // Make a struct for an empty interface type.
2529 get_backend_empty_interface_type(Gogo*);
2531 // Finish the backend representation of the method types.
2533 finish_backend_methods(Gogo*);
2536 make_interface_type_descriptor_type();
2540 do_traverse(Traverse*);
2543 do_has_pointer() const
2547 do_compare_is_identity(Gogo*) const
2551 do_hash_for_method(Gogo*) const;
2554 do_get_backend(Gogo*);
2557 do_type_descriptor(Gogo*, Named_type*);
2560 do_reflection(Gogo*, std::string*) const;
2563 do_mangled_name(Gogo*, std::string*) const;
2566 do_export(Export*) const;
2569 // This type guards against infinite recursion when comparing
2570 // interface types. We keep a list of interface types assumed to be
2571 // identical during comparison. We just keep the list on the stack.
2572 // This permits us to compare cases like
2573 // type I1 interface { F() interface{I1} }
2574 // type I2 interface { F() interface{I2} }
2575 struct Assume_identical
2577 Assume_identical* next;
2578 const Interface_type* t1;
2579 const Interface_type* t2;
2583 assume_identical(const Interface_type*, const Interface_type*) const;
2585 // The list of methods associated with the interface from the
2586 // parser. This will be NULL for the empty interface. This may
2587 // include unnamed interface types.
2588 Typed_identifier_list* parse_methods_;
2589 // The list of all methods associated with the interface. This
2590 // expands any interface types listed in methods_. It is set by
2591 // finalize_methods. This will be NULL for the empty interface.
2592 Typed_identifier_list* all_methods_;
2593 // The location where the interface was defined.
2595 // The backend representation of this type during backend conversion.
2596 Btype* interface_btype_;
2597 // A list of interface types assumed to be identical during
2598 // interface comparison.
2599 mutable Assume_identical* assume_identical_;
2600 // Whether the methods have been finalized.
2601 bool methods_are_finalized_;
2602 // Used to avoid endless recursion in do_mangled_name.
2606 // The value we keep for a named type. This lets us get the right
2607 // name when we convert to trees. Note that we don't actually keep
2608 // the name here; the name is in the Named_object which points to
2609 // this. This object exists to hold a unique tree which represents
2612 class Named_type : public Type
2615 Named_type(Named_object* named_object, Type* type, Location location)
2617 named_object_(named_object), in_function_(NULL), type_(type),
2618 local_methods_(NULL), all_methods_(NULL),
2619 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2620 location_(location), named_btype_(NULL), dependencies_(),
2621 is_visible_(true), is_error_(false), is_placeholder_(false),
2622 is_converted_(false), is_circular_(false), seen_(false),
2623 seen_in_compare_is_identity_(false), seen_in_get_backend_(false)
2626 // Return the associated Named_object. This holds the actual name.
2629 { return this->named_object_; }
2632 named_object() const
2633 { return this->named_object_; }
2635 // Set the Named_object. This is used when we see a type
2636 // declaration followed by a type.
2638 set_named_object(Named_object* no)
2639 { this->named_object_ = no; }
2641 // Return the function in which this type is defined. This will
2642 // return NULL for a type defined in global scope.
2645 { return this->in_function_; }
2647 // Set the function in which this type is defined.
2649 set_in_function(Named_object* f)
2650 { this->in_function_ = f; }
2652 // Return the name of the type.
2656 // Return the name of the type for an error message. The difference
2657 // is that if the type is defined in a different package, this will
2658 // return PACKAGE.NAME.
2660 message_name() const;
2662 // Return the underlying type.
2665 { return this->type_; }
2669 { return this->type_; }
2671 // Return the location.
2674 { return this->location_; }
2676 // Whether this type is visible. This only matters when parsing.
2679 { return this->is_visible_; }
2681 // Mark this type as visible.
2684 { this->is_visible_ = true; }
2686 // Mark this type as invisible.
2689 { this->is_visible_ = false; }
2691 // Whether this is a builtin type.
2694 { return Linemap::is_predeclared_location(this->location_); }
2696 // Whether this is an alias. There are currently two aliases: byte
2701 // Whether this is a circular type: a pointer or function type that
2702 // refers to itself, which is not possible in C.
2705 { return this->is_circular_; }
2707 // Return the base type for this type.
2714 // Return whether this is an error type.
2716 is_named_error_type() const;
2718 // Return whether this type is comparable. If REASON is not NULL,
2719 // set *REASON when returning false.
2721 named_type_is_comparable(std::string* reason) const;
2723 // Add a method to this type.
2725 add_method(const std::string& name, Function*);
2727 // Add a method declaration to this type.
2729 add_method_declaration(const std::string& name, Package* package,
2730 Function_type* type, Location location);
2732 // Add an existing method--one defined before the type itself was
2733 // defined--to a type.
2735 add_existing_method(Named_object*);
2737 // Look up a local method.
2739 find_local_method(const std::string& name) const;
2741 // Return the list of local methods.
2743 local_methods() const
2744 { return this->local_methods_; }
2746 // Build the complete list of methods, including those from
2747 // anonymous fields, and build method stubs if needed.
2749 finalize_methods(Gogo*);
2751 // Return whether this type has any methods. This should only be
2752 // called after the finalize_methods pass.
2754 has_any_methods() const
2755 { return this->all_methods_ != NULL; }
2757 // Return the methods for this type. This should only be called
2758 // after the finalized_methods pass.
2761 { return this->all_methods_; }
2763 // Return the method to use for NAME. This returns NULL if there is
2764 // no such method or if the method is ambiguous. When it returns
2765 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2767 method_function(const std::string& name, bool *is_ambiguous) const;
2769 // Return whether NAME is a known field or method which is not
2770 // exported. This is only used for better error reporting.
2772 is_unexported_local_method(Gogo*, const std::string& name) const;
2774 // Return a pointer to the interface method table for this type for
2775 // the interface INTERFACE. If IS_POINTER is true, set the type
2776 // descriptor to a pointer to this type, otherwise set it to this
2779 interface_method_table(Gogo*, const Interface_type* interface,
2782 // Whether this type has any hidden fields.
2784 named_type_has_hidden_fields(std::string* reason) const;
2786 // Note that a type must be converted to the backend representation
2787 // before we convert this type.
2789 add_dependency(Named_type* nt)
2790 { this->dependencies_.push_back(nt); }
2792 // Return true if the size and alignment of the backend
2793 // representation of this type is known. This is always true after
2794 // types have been converted, but may be false beforehand.
2796 is_named_backend_type_size_known() const
2797 { return this->named_btype_ != NULL && !this->is_placeholder_; }
2801 export_named_type(Export*, const std::string& name) const;
2803 // Import a named type.
2805 import_named_type(Import*, Named_type**);
2807 // Initial conversion to backend representation.
2813 do_traverse(Traverse* traverse)
2814 { return Type::traverse(this->type_, traverse); }
2820 do_has_pointer() const;
2823 do_compare_is_identity(Gogo*) const;
2826 do_hash_for_method(Gogo*) const;
2829 do_get_backend(Gogo*);
2832 do_type_descriptor(Gogo*, Named_type*);
2835 do_reflection(Gogo*, std::string*) const;
2838 do_mangled_name(Gogo*, std::string* ret) const;
2841 do_export(Export*) const;
2844 // Create the placeholder during conversion.
2846 create_placeholder(Gogo*);
2848 // A mapping from interfaces to the associated interface method
2849 // tables for this type. This maps to a decl.
2850 typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
2851 Type_identical) Interface_method_tables;
2853 // A pointer back to the Named_object for this type.
2854 Named_object* named_object_;
2855 // If this type is defined in a function, a pointer back to the
2856 // function in which it is defined.
2857 Named_object* in_function_;
2860 // The list of methods defined for this type. Any named type can
2862 Bindings* local_methods_;
2863 // The full list of methods for this type, including methods
2864 // declared for anonymous fields.
2865 Methods* all_methods_;
2866 // A mapping from interfaces to the associated interface method
2867 // tables for this type.
2868 Interface_method_tables* interface_method_tables_;
2869 // A mapping from interfaces to the associated interface method
2870 // tables for pointers to this type.
2871 Interface_method_tables* pointer_interface_method_tables_;
2872 // The location where this type was defined.
2874 // The backend representation of this type during backend
2875 // conversion. This is used to avoid endless recursion when a named
2876 // type refers to itself.
2877 Btype* named_btype_;
2878 // A list of types which must be converted to the backend
2879 // representation before this type can be converted. This is for
2881 // type S1 { p *S2 }
2883 // where we can't convert S2 to the backend representation unless we
2884 // have converted S1.
2885 std::vector<Named_type*> dependencies_;
2886 // Whether this type is visible. This is false if this type was
2887 // created because it was referenced by an imported object, but the
2888 // type itself was not exported. This will always be true for types
2889 // created in the current package.
2891 // Whether this type is erroneous.
2893 // Whether the current value of named_btype_ is a placeholder for
2894 // which the final size of the type is not known.
2895 bool is_placeholder_;
2896 // Whether this type has been converted to the backend
2897 // representation. Implies that is_placeholder_ is false.
2899 // Whether this is a pointer or function type which refers to the
2902 // In a recursive operation such as has_hidden_fields, this flag is
2903 // used to prevent infinite recursion when a type refers to itself.
2904 // This is mutable because it is always reset to false when the
2907 // Like seen_, but used only by do_compare_is_identity.
2908 mutable bool seen_in_compare_is_identity_;
2909 // Like seen_, but used only by do_get_backend.
2910 bool seen_in_get_backend_;
2913 // A forward declaration. This handles a type which has been declared
2916 class Forward_declaration_type : public Type
2919 Forward_declaration_type(Named_object* named_object);
2921 // The named object associated with this type declaration. This
2922 // will be resolved.
2927 named_object() const;
2929 // Return the name of the type.
2933 // Return the type to which this points. Give an error if the type
2934 // has not yet been defined.
2941 // Whether the base type has been defined.
2945 // Add a method to this type.
2947 add_method(const std::string& name, Function*);
2949 // Add a method declaration to this type.
2951 add_method_declaration(const std::string& name, Package*, Function_type*,
2956 do_traverse(Traverse* traverse);
2959 do_has_pointer() const
2960 { return this->real_type()->has_pointer(); }
2963 do_compare_is_identity(Gogo* gogo) const
2964 { return this->real_type()->compare_is_identity(gogo); }
2967 do_hash_for_method(Gogo* gogo) const
2968 { return this->real_type()->hash_for_method(gogo); }
2971 do_get_backend(Gogo* gogo);
2974 do_type_descriptor(Gogo*, Named_type*);
2977 do_reflection(Gogo*, std::string*) const;
2980 do_mangled_name(Gogo*, std::string* ret) const;
2983 do_export(Export*) const;
2986 // Issue a warning about a use of an undefined type.
2990 // The type declaration.
2991 Named_object* named_object_;
2992 // Whether we have issued a warning about this type.
2993 mutable bool warned_;
2996 // The Type_context struct describes what we expect for the type of an
3001 // The exact type we expect, if known. This may be NULL.
3003 // Whether an abstract type is permitted.
3004 bool may_be_abstract;
3008 : type(NULL), may_be_abstract(false)
3011 Type_context(Type* a_type, bool a_may_be_abstract)
3012 : type(a_type), may_be_abstract(a_may_be_abstract)
3016 #endif // !defined(GO_TYPES_H)