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 and we should not continue
517 { return this->do_verify(); }
519 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
520 // returns that an erroneous type is identical to any other type;
521 // this is used to avoid cascading errors. If this returns false,
522 // and REASON is not NULL, it may set *REASON.
524 are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
525 std::string* reason);
527 // Return true if two types are compatible for use in a binary
528 // operation, other than a shift, comparison, or channel send. This
529 // is an equivalence relation.
531 are_compatible_for_binop(const Type* t1, const Type* t2);
533 // Return true if two types are compatible for use with the
534 // comparison operator. IS_EQUALITY_OP is true if this is an
535 // equality comparison, false if it is an ordered comparison. This
536 // is an equivalence relation. If this returns false, and REASON is
537 // not NULL, it sets *REASON.
539 are_compatible_for_comparison(bool is_equality_op, const Type *t1,
540 const Type *t2, std::string* reason);
542 // Return true if a type is comparable with itself. This is true of
543 // most types, but false for, e.g., function types.
545 is_comparable() const
546 { return Type::are_compatible_for_comparison(true, this, this, NULL); }
548 // Return true if a value with type RHS is assignable to a variable
549 // with type LHS. This is not an equivalence relation. If this
550 // returns false, and REASON is not NULL, it sets *REASON.
552 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
554 // Return true if a value with type RHS is assignable to a variable
555 // with type LHS, ignoring any assignment of hidden fields
556 // (unexported fields of a type imported from another package).
557 // This is like the are_assignable method.
559 are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
560 std::string* reason);
562 // Return true if a value with type RHS may be converted to type
563 // LHS. If this returns false, and REASON is not NULL, it sets
566 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
568 // Whether this type has any hidden fields which are not visible in
569 // the current compilation, such as a field whose name begins with a
570 // lower case letter in a struct imported from a different package.
571 // WITHIN is not NULL if we are looking at fields in a named type.
573 has_hidden_fields(const Named_type* within, std::string* reason) const;
575 // Return true if values of this type can be compared using an
576 // identity function which gets nothing but a pointer to the value
579 compare_is_identity(Gogo* gogo) const
580 { return this->do_compare_is_identity(gogo); }
582 // Return a hash code for this type for the method hash table.
583 // Types which are equivalent according to are_identical will have
584 // the same hash code.
586 hash_for_method(Gogo*) const;
588 // Return the type classification.
590 classification() const
591 { return this->classification_; }
593 // Return the base type for this type. This looks through forward
594 // declarations and names. Using this with a forward declaration
595 // which has not been defined will return an error type.
602 // Return the type skipping defined forward declarations. If this
603 // type is a forward declaration which has not been defined, it will
604 // return the Forward_declaration_type. This differs from base() in
605 // that it will return a Named_type, and for a
606 // Forward_declaration_type which is not defined it will return that
607 // type rather than an error type.
614 // Return true if this is a basic type: a type which is not composed
615 // of other types, and is not void.
617 is_basic_type() const;
619 // Return true if this is an abstract type--an integer, floating
620 // point, or complex type whose size has not been determined.
624 // Return a non-abstract version of an abstract type.
626 make_non_abstract_type();
628 // Return true if this type is or contains a pointer. This
629 // determines whether the garbage collector needs to look at a value
633 { return this->do_has_pointer(); }
635 // Return true if this is the error type. This returns false for a
636 // type which is not defined, as it is called by the parser before
637 // all types are defined.
639 is_error_type() const;
641 // Return true if this is the error type or if the type is
642 // undefined. If the type is undefined, this will give an error.
643 // This should only be called after parsing is complete.
646 { return this->base()->is_error_type(); }
648 // Return true if this is a void type.
651 { return this->classification_ == TYPE_VOID; }
653 // If this is an integer type, return the Integer_type. Otherwise,
654 // return NULL. This is a controlled dynamic_cast.
657 { return this->convert<Integer_type, TYPE_INTEGER>(); }
661 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
663 // If this is a floating point type, return the Float_type.
664 // Otherwise, return NULL. This is a controlled dynamic_cast.
667 { return this->convert<Float_type, TYPE_FLOAT>(); }
671 { return this->convert<const Float_type, TYPE_FLOAT>(); }
673 // If this is a complex type, return the Complex_type. Otherwise,
677 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
681 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
683 // Return whether this is a numeric type.
685 is_numeric_type() const
687 Type_classification tc = this->base()->classification_;
688 return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
691 // Return true if this is a boolean type.
693 is_boolean_type() const
694 { return this->base()->classification_ == TYPE_BOOLEAN; }
696 // Return true if this is an abstract boolean type.
698 is_abstract_boolean_type() const
699 { return this->classification_ == TYPE_BOOLEAN; }
701 // Return true if this is a string type.
703 is_string_type() const
704 { return this->base()->classification_ == TYPE_STRING; }
706 // Return true if this is an abstract string type.
708 is_abstract_string_type() const
709 { return this->classification_ == TYPE_STRING; }
711 // Return true if this is the sink type. This is the type of the
712 // blank identifier _.
715 { return this->base()->classification_ == TYPE_SINK; }
717 // If this is a function type, return it. Otherwise, return NULL.
720 { return this->convert<Function_type, TYPE_FUNCTION>(); }
723 function_type() const
724 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
726 // If this is a pointer type, return the type to which it points.
727 // Otherwise, return NULL.
731 // If this is a pointer type, return the type to which it points.
732 // Otherwise, return the type itself.
736 Type* pt = this->points_to();
737 return pt != NULL ? pt : this;
743 const Type* pt = this->points_to();
744 return pt != NULL ? pt : this;
747 // Return true if this is the nil type. We don't use base() here,
748 // because this can be called during parse, and there is no way to
749 // name the nil type anyhow.
752 { return this->classification_ == TYPE_NIL; }
754 // Return true if this is the predeclared constant nil being used as
755 // a type. This is what the parser produces for type switches which
758 is_nil_constant_as_type() const;
760 // Return true if this is the return type of a function which
761 // returns multiple values.
763 is_call_multiple_result_type() const
764 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
766 // If this is a struct type, return it. Otherwise, return NULL.
769 { return this->convert<Struct_type, TYPE_STRUCT>(); }
773 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
775 // If this is an array type, return it. Otherwise, return NULL.
778 { return this->convert<Array_type, TYPE_ARRAY>(); }
782 { return this->convert<const Array_type, TYPE_ARRAY>(); }
784 // Return whether if this is a slice type.
786 is_slice_type() const;
788 // If this is a map type, return it. Otherwise, return NULL.
791 { return this->convert<Map_type, TYPE_MAP>(); }
795 { return this->convert<const Map_type, TYPE_MAP>(); }
797 // If this is a channel type, return it. Otherwise, return NULL.
800 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
804 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
806 // If this is an interface type, return it. Otherwise, return NULL.
809 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
811 const Interface_type*
812 interface_type() const
813 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
815 // If this is a named type, return it. Otherwise, return NULL.
822 // If this is a forward declaration, return it. Otherwise, return
824 Forward_declaration_type*
825 forward_declaration_type()
826 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
828 const Forward_declaration_type*
829 forward_declaration_type() const
831 return this->convert_no_base<const Forward_declaration_type,
835 // Return true if this type is not yet defined.
837 is_undefined() const;
839 // Return true if this is the unsafe.pointer type. We currently
840 // represent that as pointer-to-void.
842 is_unsafe_pointer_type() const
843 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
845 // Look for field or method NAME for TYPE. Return an expression for
846 // it, bound to EXPR.
848 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
849 const std::string& name, Location);
851 // Return true if NAME is an unexported field or method of TYPE.
853 is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
854 std::vector<const Named_type*>*);
856 // Convert the builtin named types.
858 convert_builtin_named_types(Gogo*);
860 // Return the backend representation of this type.
864 // Return a placeholder for the backend representation of the type.
865 // This will return a type of the correct size, but for which some
866 // of the fields may still need to be completed.
868 get_backend_placeholder(Gogo*);
870 // Finish the backend representation of a placeholder.
872 finish_backend(Gogo*);
874 // Build a type descriptor entry for this type. Return a pointer to
875 // it. The location is the location which causes us to need the
878 type_descriptor_pointer(Gogo* gogo, Location);
880 // Return the type reflection string for this type.
882 reflection(Gogo*) const;
884 // Return a mangled name for the type. This is a name which can be
885 // used in assembler code. Identical types should have the same
888 mangled_name(Gogo*) const;
890 // If the size of the type can be determined, set *PSIZE to the size
891 // in bytes and return true. Otherwise, return false. This queries
894 backend_type_size(Gogo*, unsigned int* psize);
896 // If the alignment of the type can be determined, set *PALIGN to
897 // the alignment in bytes and return true. Otherwise, return false.
899 backend_type_align(Gogo*, unsigned int* palign);
901 // If the alignment of a struct field of this type can be
902 // determined, set *PALIGN to the alignment in bytes and return
903 // true. Otherwise, return false.
905 backend_type_field_align(Gogo*, unsigned int* palign);
907 // Whether the backend size is known.
909 is_backend_type_size_known(Gogo*);
911 // Get the hash and equality functions for a type.
913 type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
914 Function_type* equal_fntype, Named_object** hash_fn,
915 Named_object** equal_fn);
917 // Write the hash and equality type functions.
919 write_specific_type_functions(Gogo*, Named_type*,
920 const std::string& hash_name,
921 Function_type* hash_fntype,
922 const std::string& equal_name,
923 Function_type* equal_fntype);
927 export_type(Export* exp) const
928 { this->do_export(exp); }
932 import_type(Import*);
935 Type(Type_classification);
937 // Functions implemented by the child class.
939 // Traverse the subtypes.
941 do_traverse(Traverse*);
949 do_has_pointer() const
953 do_compare_is_identity(Gogo*) const = 0;
956 do_hash_for_method(Gogo*) const;
959 do_get_backend(Gogo*) = 0;
962 do_type_descriptor(Gogo*, Named_type* name) = 0;
965 do_reflection(Gogo*, std::string*) const = 0;
968 do_mangled_name(Gogo*, std::string*) const = 0;
971 do_export(Export*) const;
973 // Return whether a method expects a pointer as the receiver.
975 method_expects_pointer(const Named_object*);
977 // Finalize the methods for a type.
979 finalize_methods(Gogo*, const Type*, Location, Methods**);
981 // Return a method from a set of methods.
983 method_function(const Methods*, const std::string& name,
986 // Return a composite literal for the type descriptor entry for a
989 type_descriptor(Gogo*, Type*);
991 // Return a composite literal for the type descriptor entry for
992 // TYPE, using NAME as the name of the type.
994 named_type_descriptor(Gogo*, Type* type, Named_type* name);
996 // Return a composite literal for a plain type descriptor for this
997 // type with the given kind and name.
999 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
1001 // Build a composite literal for the basic type descriptor.
1003 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
1004 const Methods*, bool only_value_methods);
1006 // Make a builtin struct type from a list of fields.
1008 make_builtin_struct_type(int nfields, ...);
1010 // Make a builtin named type.
1012 make_builtin_named_type(const char* name, Type* type);
1014 // For the benefit of child class reflection string generation.
1016 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
1017 { type->do_reflection(gogo, ret); }
1019 // For the benefit of child class mangling.
1021 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
1022 { type->do_mangled_name(gogo, ret); }
1024 // Incorporate a string into a hash code.
1026 hash_string(const std::string&, unsigned int);
1028 // Return the backend representation for the underlying type of a
1031 get_named_base_btype(Gogo* gogo, Type* base_type)
1032 { return base_type->get_btype_without_hash(gogo); }
1035 // Convert to the desired type classification, or return NULL. This
1036 // is a controlled dynamic_cast.
1037 template<typename Type_class, Type_classification type_classification>
1041 Type* base = this->base();
1042 return (base->classification_ == type_classification
1043 ? static_cast<Type_class*>(base)
1047 template<typename Type_class, Type_classification type_classification>
1051 const Type* base = this->base();
1052 return (base->classification_ == type_classification
1053 ? static_cast<Type_class*>(base)
1057 template<typename Type_class, Type_classification type_classification>
1061 return (this->classification_ == type_classification
1062 ? static_cast<Type_class*>(this)
1066 template<typename Type_class, Type_classification type_classification>
1068 convert_no_base() const
1070 return (this->classification_ == type_classification
1071 ? static_cast<Type_class*>(this)
1075 // Support for are_assignable and are_assignable_hidden_ok.
1077 are_assignable_check_hidden(const Type* lhs, const Type* rhs,
1078 bool check_hidden_fields, std::string* reason);
1080 // Map unnamed types to type descriptor decls.
1081 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1082 Type_identical) Type_descriptor_vars;
1084 static Type_descriptor_vars type_descriptor_vars;
1086 // Build the type descriptor variable for this type.
1088 make_type_descriptor_var(Gogo*);
1090 // Return the name of the type descriptor variable. If NAME is not
1091 // NULL, it is the name to use.
1093 type_descriptor_var_name(Gogo*, Named_type* name);
1095 // Return true if the type descriptor for this type should be
1096 // defined in some other package. If NAME is not NULL, it is the
1097 // name of this type. If this returns true it sets *PACKAGE to the
1098 // package where the type descriptor is defined.
1100 type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
1102 // Build the hash and equality type functions for a type which needs
1103 // specific functions.
1105 specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
1106 Function_type* equal_fntype, Named_object** hash_fn,
1107 Named_object** equal_fn);
1109 // Build a composite literal for the uncommon type information.
1111 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1112 Named_type*, const Methods*,
1113 bool only_value_methods) const;
1115 // Build a composite literal for the methods.
1117 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1118 bool only_value_methods) const;
1120 // Build a composite literal for one method.
1122 method_constructor(Gogo*, Type* method_type, const std::string& name,
1123 const Method*, bool only_value_methods) const;
1126 build_receive_return_type(tree type);
1128 // A hash table we use to avoid infinite recursion.
1129 typedef Unordered_set_hash(const Named_type*, Type_hash_identical,
1130 Type_identical) Types_seen;
1132 // Add all methods for TYPE to the list of methods for THIS.
1134 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1135 unsigned int depth, bool, bool, Types_seen*,
1139 add_local_methods_for_type(const Named_type* type,
1140 const Method::Field_indexes*,
1141 unsigned int depth, bool, bool, Methods**);
1144 add_embedded_methods_for_type(const Type* type,
1145 const Method::Field_indexes*,
1146 unsigned int depth, bool, bool, Types_seen*,
1150 add_interface_methods_for_type(const Type* type,
1151 const Method::Field_indexes*,
1152 unsigned int depth, Methods**);
1154 // Build stub methods for a type.
1156 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1160 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1161 const Typed_identifier_list*, bool is_varargs,
1165 apply_field_indexes(Expression*, const Method::Field_indexes*,
1168 // Look for a field or method named NAME in TYPE.
1170 find_field_or_method(const Type* type, const std::string& name,
1171 bool receiver_can_be_pointer,
1172 std::vector<const Named_type*>*, int* level,
1173 bool* is_method, bool* found_pointer_method,
1174 std::string* ambig1, std::string* ambig2);
1176 // Get the backend representation for a type without looking in the
1177 // hash table for identical types.
1179 get_btype_without_hash(Gogo*);
1181 // A mapping from Type to Btype*, used to ensure that the backend
1182 // representation of identical types is identical.
1183 typedef Unordered_map_hash(const Type*, Btype*, Type_hash_identical,
1184 Type_identical) Type_btypes;
1186 static Type_btypes type_btypes;
1188 // A list of builtin named types.
1189 static std::vector<Named_type*> named_builtin_types;
1191 // A map from types which need specific type functions to the type
1192 // functions themselves.
1193 typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
1194 typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
1195 Type_identical) Type_functions;
1197 static Type_functions type_functions_table;
1199 // The type classification.
1200 Type_classification classification_;
1201 // Whether btype_ is a placeholder type used while named types are
1203 bool btype_is_placeholder_;
1204 // The backend representation of the type, once it has been
1207 // The type descriptor for this type. This starts out as NULL and
1208 // is filled in as needed.
1209 Bvariable* type_descriptor_var_;
1212 // Type hash table operations.
1214 class Type_hash_identical
1218 operator()(const Type* type) const
1219 { return type->hash_for_method(NULL); }
1222 class Type_identical
1226 operator()(const Type* t1, const Type* t2) const
1227 { return Type::are_identical(t1, t2, false, NULL); }
1230 // An identifier with a type.
1232 class Typed_identifier
1235 Typed_identifier(const std::string& name, Type* type,
1237 : name_(name), type_(type), location_(location)
1243 { return this->name_; }
1248 { return this->type_; }
1250 // Return the location where the name was seen. This is not always
1254 { return this->location_; }
1256 // Set the type--sometimes we see the identifier before the type.
1258 set_type(Type* type)
1260 go_assert(this->type_ == NULL || type->is_error_type());
1269 // The location where the name was seen.
1273 // A list of Typed_identifiers.
1275 class Typed_identifier_list
1278 Typed_identifier_list()
1282 // Whether the list is empty.
1285 { return this->entries_.empty(); }
1287 // Return the number of entries in the list.
1290 { return this->entries_.size(); }
1292 // Add an entry to the end of the list.
1294 push_back(const Typed_identifier& td)
1295 { this->entries_.push_back(td); }
1297 // Remove an entry from the end of the list.
1300 { this->entries_.pop_back(); }
1302 // Set the type of entry I to TYPE.
1304 set_type(size_t i, Type* type)
1306 go_assert(i < this->entries_.size());
1307 this->entries_[i].set_type(type);
1310 // Sort the entries by name.
1316 traverse(Traverse*);
1318 // Return the first and last elements.
1321 { return this->entries_.front(); }
1323 const Typed_identifier&
1325 { return this->entries_.front(); }
1329 { return this->entries_.back(); }
1331 const Typed_identifier&
1333 { return this->entries_.back(); }
1335 const Typed_identifier&
1337 { return this->entries_.at(i); }
1340 set(size_t i, const Typed_identifier& t)
1341 { this->entries_.at(i) = t; }
1346 go_assert(c <= this->entries_.size());
1347 this->entries_.resize(c, Typed_identifier("", NULL,
1348 Linemap::unknown_location()));
1353 { this->entries_.reserve(c); }
1357 typedef std::vector<Typed_identifier>::iterator iterator;
1358 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1362 { return this->entries_.begin(); }
1366 { return this->entries_.begin(); }
1370 { return this->entries_.end(); }
1374 { return this->entries_.end(); }
1376 // Return a copy of this list. This returns an independent copy of
1377 // the vector, but does not copy the types.
1378 Typed_identifier_list*
1382 std::vector<Typed_identifier> entries_;
1385 // The type of an integer.
1387 class Integer_type : public Type
1390 // Create a new integer type.
1392 create_integer_type(const char* name, bool is_unsigned, int bits,
1393 int runtime_type_kind);
1395 // Look up an existing integer type.
1397 lookup_integer_type(const char* name);
1399 // Create an abstract integer type.
1400 static Integer_type*
1401 create_abstract_integer_type();
1403 // Create an abstract character type.
1404 static Integer_type*
1405 create_abstract_character_type();
1407 // Whether this is an abstract integer type.
1410 { return this->is_abstract_; }
1412 // Whether this is an unsigned type.
1415 { return this->is_unsigned_; }
1417 // The number of bits.
1420 { return this->bits_; }
1422 // Whether this type is the same as T.
1424 is_identical(const Integer_type* t) const;
1426 // Whether this is the type "byte" or another name for "byte".
1429 { return this->is_byte_; }
1431 // Mark this as the "byte" type.
1434 { this->is_byte_ = true; }
1436 // Whether this is the type "rune" or another name for "rune".
1439 { return this->is_rune_; }
1441 // Mark this as the "rune" type.
1444 { this->is_rune_ = true; }
1448 do_compare_is_identity(Gogo*) const
1452 do_hash_for_method(Gogo*) const;
1455 do_get_backend(Gogo*);
1458 do_type_descriptor(Gogo*, Named_type*);
1461 do_reflection(Gogo*, std::string*) const;
1464 do_mangled_name(Gogo*, std::string*) const;
1467 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1468 int runtime_type_kind)
1469 : Type(TYPE_INTEGER),
1470 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1471 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1474 // Map names of integer types to the types themselves.
1475 typedef std::map<std::string, Named_type*> Named_integer_types;
1476 static Named_integer_types named_integer_types;
1478 // True if this is an abstract type.
1480 // True if this is an unsigned type.
1482 // True if this is the byte type.
1484 // True if this is the rune type.
1486 // The number of bits.
1488 // The runtime type code used in the type descriptor for this type.
1489 int runtime_type_kind_;
1492 // The type of a floating point number.
1494 class Float_type : public Type
1497 // Create a new float type.
1499 create_float_type(const char* name, int bits, int runtime_type_kind);
1501 // Look up an existing float type.
1503 lookup_float_type(const char* name);
1505 // Create an abstract float type.
1507 create_abstract_float_type();
1509 // Whether this is an abstract float type.
1512 { return this->is_abstract_; }
1514 // The number of bits.
1517 { return this->bits_; }
1519 // Whether this type is the same as T.
1521 is_identical(const Float_type* t) const;
1525 do_compare_is_identity(Gogo*) const
1529 do_hash_for_method(Gogo*) const;
1532 do_get_backend(Gogo*);
1535 do_type_descriptor(Gogo*, Named_type*);
1538 do_reflection(Gogo*, std::string*) const;
1541 do_mangled_name(Gogo*, std::string*) const;
1544 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1546 is_abstract_(is_abstract), bits_(bits),
1547 runtime_type_kind_(runtime_type_kind)
1550 // Map names of float types to the types themselves.
1551 typedef std::map<std::string, Named_type*> Named_float_types;
1552 static Named_float_types named_float_types;
1554 // True if this is an abstract type.
1556 // The number of bits in the floating point value.
1558 // The runtime type code used in the type descriptor for this type.
1559 int runtime_type_kind_;
1562 // The type of a complex number.
1564 class Complex_type : public Type
1567 // Create a new complex type.
1569 create_complex_type(const char* name, int bits, int runtime_type_kind);
1571 // Look up an existing complex type.
1573 lookup_complex_type(const char* name);
1575 // Create an abstract complex type.
1576 static Complex_type*
1577 create_abstract_complex_type();
1579 // Whether this is an abstract complex type.
1582 { return this->is_abstract_; }
1584 // The number of bits: 64 or 128.
1586 { return this->bits_; }
1588 // Whether this type is the same as T.
1590 is_identical(const Complex_type* t) const;
1594 do_compare_is_identity(Gogo*) const
1598 do_hash_for_method(Gogo*) const;
1601 do_get_backend(Gogo*);
1604 do_type_descriptor(Gogo*, Named_type*);
1607 do_reflection(Gogo*, std::string*) const;
1610 do_mangled_name(Gogo*, std::string*) const;
1613 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1614 : Type(TYPE_COMPLEX),
1615 is_abstract_(is_abstract), bits_(bits),
1616 runtime_type_kind_(runtime_type_kind)
1619 // Map names of complex types to the types themselves.
1620 typedef std::map<std::string, Named_type*> Named_complex_types;
1621 static Named_complex_types named_complex_types;
1623 // True if this is an abstract type.
1625 // The number of bits in the complex value--64 or 128.
1627 // The runtime type code used in the type descriptor for this type.
1628 int runtime_type_kind_;
1631 // The type of a string.
1633 class String_type : public Type
1640 // Return a tree for the length of STRING.
1642 length_tree(Gogo*, tree string);
1644 // Return a tree which points to the bytes of STRING.
1646 bytes_tree(Gogo*, tree string);
1650 do_has_pointer() const
1654 do_compare_is_identity(Gogo*) const
1658 do_get_backend(Gogo*);
1661 do_type_descriptor(Gogo*, Named_type*);
1664 do_reflection(Gogo*, std::string*) const;
1667 do_mangled_name(Gogo*, std::string* ret) const;
1670 // The named string type.
1671 static Named_type* string_type_;
1674 // The type of a function.
1676 class Function_type : public Type
1679 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1680 Typed_identifier_list* results, Location location)
1681 : Type(TYPE_FUNCTION),
1682 receiver_(receiver), parameters_(parameters), results_(results),
1683 location_(location), is_varargs_(false), is_builtin_(false)
1686 // Get the receiver.
1687 const Typed_identifier*
1689 { return this->receiver_; }
1691 // Get the return names and types.
1692 const Typed_identifier_list*
1694 { return this->results_; }
1696 // Get the parameter names and types.
1697 const Typed_identifier_list*
1699 { return this->parameters_; }
1701 // Whether this is a varargs function.
1704 { return this->is_varargs_; }
1706 // Whether this is a builtin function.
1709 { return this->is_builtin_; }
1711 // The location where this type was defined.
1714 { return this->location_; }
1716 // Return whether this is a method type.
1719 { return this->receiver_ != NULL; }
1721 // Whether T is a valid redeclaration of this type. This is called
1722 // when a function is declared more than once.
1724 is_valid_redeclaration(const Function_type* t, std::string*) const;
1726 // Whether this type is the same as T.
1728 is_identical(const Function_type* t, bool ignore_receiver,
1729 bool errors_are_identical, std::string*) const;
1731 // Record that this is a varargs function.
1734 { this->is_varargs_ = true; }
1736 // Record that this is a builtin function.
1739 { this->is_builtin_ = true; }
1741 // Import a function type.
1742 static Function_type*
1745 // Return a copy of this type without a receiver. This is only
1746 // valid for a method type.
1748 copy_without_receiver() const;
1750 // Return a copy of this type with a receiver. This is used when an
1751 // interface method is attached to a named or struct type.
1753 copy_with_receiver(Type*) const;
1756 make_function_type_descriptor_type();
1760 do_traverse(Traverse*);
1762 // A trampoline function has a pointer which matters for GC.
1764 do_has_pointer() const
1768 do_compare_is_identity(Gogo*) const
1772 do_hash_for_method(Gogo*) const;
1775 do_get_backend(Gogo*);
1778 do_type_descriptor(Gogo*, Named_type*);
1781 do_reflection(Gogo*, std::string*) const;
1784 do_mangled_name(Gogo*, std::string*) const;
1787 do_export(Export*) const;
1791 type_descriptor_params(Type*, const Typed_identifier*,
1792 const Typed_identifier_list*);
1794 // The receiver name and type. This will be NULL for a normal
1795 // function, non-NULL for a method.
1796 Typed_identifier* receiver_;
1797 // The parameter names and types.
1798 Typed_identifier_list* parameters_;
1799 // The result names and types. This will be NULL if no result was
1801 Typed_identifier_list* results_;
1802 // The location where this type was defined. This exists solely to
1803 // give a location for the fields of the struct if this function
1804 // returns multiple values.
1806 // Whether this function takes a variable number of arguments.
1808 // Whether this is a special builtin function which can not simply
1809 // be called. This is used for len, cap, etc.
1813 // The type of a pointer.
1815 class Pointer_type : public Type
1818 Pointer_type(Type* to_type)
1819 : Type(TYPE_POINTER),
1825 { return this->to_type_; }
1827 // Import a pointer type.
1828 static Pointer_type*
1832 make_pointer_type_descriptor_type();
1836 do_traverse(Traverse*);
1839 do_has_pointer() const
1843 do_compare_is_identity(Gogo*) const
1847 do_hash_for_method(Gogo*) const;
1850 do_get_backend(Gogo*);
1853 do_type_descriptor(Gogo*, Named_type*);
1856 do_reflection(Gogo*, std::string*) const;
1859 do_mangled_name(Gogo*, std::string*) const;
1862 do_export(Export*) const;
1865 // The type to which this type points.
1869 // The type of a field in a struct.
1874 explicit Struct_field(const Typed_identifier& typed_identifier)
1875 : typed_identifier_(typed_identifier), tag_(NULL)
1882 // Return whether this struct field is named NAME.
1884 is_field_name(const std::string& name) const;
1889 { return this->typed_identifier_.type(); }
1891 // The field location.
1894 { return this->typed_identifier_.location(); }
1896 // Whether the field has a tag.
1899 { return this->tag_ != NULL; }
1905 go_assert(this->tag_ != NULL);
1909 // Whether this is an anonymous field.
1911 is_anonymous() const
1912 { return this->typed_identifier_.name().empty(); }
1914 // Set the tag. FIXME: This is never freed.
1916 set_tag(const std::string& tag)
1917 { this->tag_ = new std::string(tag); }
1919 // Set the type. This is only used in error cases.
1921 set_type(Type* type)
1922 { this->typed_identifier_.set_type(type); }
1925 // The field name, type, and location.
1926 Typed_identifier typed_identifier_;
1927 // The field tag. This is NULL if the field has no tag.
1931 // A list of struct fields.
1933 class Struct_field_list
1940 // Whether the list is empty.
1943 { return this->entries_.empty(); }
1945 // Return the number of entries.
1948 { return this->entries_.size(); }
1950 // Add an entry to the end of the list.
1952 push_back(const Struct_field& sf)
1953 { this->entries_.push_back(sf); }
1955 // Index into the list.
1958 { return this->entries_.at(i); }
1960 // Last entry in list.
1963 { return this->entries_.back(); }
1967 typedef std::vector<Struct_field>::iterator iterator;
1968 typedef std::vector<Struct_field>::const_iterator const_iterator;
1972 { return this->entries_.begin(); }
1976 { return this->entries_.begin(); }
1980 { return this->entries_.end(); }
1984 { return this->entries_.end(); }
1987 std::vector<Struct_field> entries_;
1990 // The type of a struct.
1992 class Struct_type : public Type
1995 Struct_type(Struct_field_list* fields, Location location)
1996 : Type(TYPE_STRUCT),
1997 fields_(fields), location_(location), all_methods_(NULL)
2000 // Return the field NAME. This only looks at local fields, not at
2001 // embedded types. If the field is found, and PINDEX is not NULL,
2002 // this sets *PINDEX to the field index. If the field is not found,
2003 // this returns NULL.
2005 find_local_field(const std::string& name, unsigned int *pindex) const;
2007 // Return the field number INDEX.
2009 field(unsigned int index) const
2010 { return &this->fields_->at(index); }
2012 // Get the struct fields.
2013 const Struct_field_list*
2015 { return this->fields_; }
2017 // Return the number of fields.
2020 { return this->fields_->size(); }
2022 // Push a new field onto the end of the struct. This is used when
2023 // building a closure variable.
2025 push_field(const Struct_field& sf)
2026 { this->fields_->push_back(sf); }
2028 // Return an expression referring to field NAME in STRUCT_EXPR, or
2029 // NULL if there is no field with that name.
2030 Field_reference_expression*
2031 field_reference(Expression* struct_expr, const std::string& name,
2034 // Return the total number of fields, including embedded fields.
2035 // This is the number of values that can appear in a conversion to
2038 total_field_count() const;
2040 // Whether this type is identical with T.
2042 is_identical(const Struct_type* t, bool errors_are_identical) const;
2044 // Whether this struct type has any hidden fields. This returns
2045 // true if any fields have hidden names, or if any non-pointer
2046 // anonymous fields have types with hidden fields.
2048 struct_has_hidden_fields(const Named_type* within, std::string*) const;
2050 // Return whether NAME is a local field which is not exported. This
2051 // is only used for better error reporting.
2053 is_unexported_local_field(Gogo*, const std::string& name) const;
2055 // If this is an unnamed struct, build the complete list of methods,
2056 // including those from anonymous fields, and build methods stubs if
2059 finalize_methods(Gogo*);
2061 // Return whether this type has any methods. This should only be
2062 // called after the finalize_methods pass.
2064 has_any_methods() const
2065 { return this->all_methods_ != NULL; }
2067 // Return the methods for tihs type. This should only be called
2068 // after the finalize_methods pass.
2071 { return this->all_methods_; }
2073 // Return the method to use for NAME. This returns NULL if there is
2074 // no such method or if the method is ambiguous. When it returns
2075 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2077 method_function(const std::string& name, bool* is_ambiguous) const;
2079 // Traverse just the field types of a struct type.
2081 traverse_field_types(Traverse* traverse)
2082 { return this->do_traverse(traverse); }
2084 // If the offset of field INDEX in the backend implementation can be
2085 // determined, set *POFFSET to the offset in bytes and return true.
2086 // Otherwise, return false.
2088 backend_field_offset(Gogo*, unsigned int index, unsigned int* poffset);
2090 // Finish the backend representation of all the fields.
2092 finish_backend_fields(Gogo*);
2094 // Import a struct type.
2099 make_struct_type_descriptor_type();
2101 // Write the hash function for this type.
2103 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2105 // Write the equality function for this type.
2107 write_equal_function(Gogo*, Named_type*);
2111 do_traverse(Traverse*);
2117 do_has_pointer() const;
2120 do_compare_is_identity(Gogo*) const;
2123 do_hash_for_method(Gogo*) const;
2126 do_get_backend(Gogo*);
2129 do_type_descriptor(Gogo*, Named_type*);
2132 do_reflection(Gogo*, std::string*) const;
2135 do_mangled_name(Gogo*, std::string*) const;
2138 do_export(Export*) const;
2141 // Used to avoid infinite loops in field_reference_depth.
2142 struct Saw_named_type
2144 Saw_named_type* next;
2148 Field_reference_expression*
2149 field_reference_depth(Expression* struct_expr, const std::string& name,
2150 Location, Saw_named_type*,
2151 unsigned int* depth) const;
2153 // The fields of the struct.
2154 Struct_field_list* fields_;
2155 // The place where the struct was declared.
2157 // If this struct is unnamed, a list of methods.
2158 Methods* all_methods_;
2161 // The type of an array.
2163 class Array_type : public Type
2166 Array_type(Type* element_type, Expression* length)
2168 element_type_(element_type), length_(length), length_tree_(NULL)
2171 // Return the element type.
2173 element_type() const
2174 { return this->element_type_; }
2176 // Return the length. This will return NULL for an open array.
2179 { return this->length_; }
2181 // Whether this type is identical with T.
2183 is_identical(const Array_type* t, bool errors_are_identical) const;
2185 // Whether this type has any hidden fields.
2187 array_has_hidden_fields(const Named_type* within, std::string* reason) const
2188 { return this->element_type_->has_hidden_fields(within, reason); }
2190 // Return a tree for the pointer to the values in an array.
2192 value_pointer_tree(Gogo*, tree array) const;
2194 // Return a tree for the length of an array with this type.
2196 length_tree(Gogo*, tree array);
2198 // Return a tree for the capacity of an array with this type.
2200 capacity_tree(Gogo*, tree array);
2202 // Import an array type.
2206 // Return the backend representation of the element type.
2208 get_backend_element(Gogo*, bool use_placeholder);
2210 // Return the backend representation of the length.
2212 get_backend_length(Gogo*);
2214 // Finish the backend representation of the element type.
2216 finish_backend_element(Gogo*);
2219 make_array_type_descriptor_type();
2222 make_slice_type_descriptor_type();
2224 // Write the hash function for this type.
2226 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2228 // Write the equality function for this type.
2230 write_equal_function(Gogo*, Named_type*);
2234 do_traverse(Traverse* traverse);
2240 do_has_pointer() const
2242 return this->length_ == NULL || this->element_type_->has_pointer();
2246 do_compare_is_identity(Gogo*) const;
2249 do_hash_for_method(Gogo*) const;
2252 do_get_backend(Gogo*);
2255 do_type_descriptor(Gogo*, Named_type*);
2258 do_reflection(Gogo*, std::string*) const;
2261 do_mangled_name(Gogo*, std::string*) const;
2264 do_export(Export*) const;
2271 get_length_tree(Gogo*);
2274 array_type_descriptor(Gogo*, Named_type*);
2277 slice_type_descriptor(Gogo*, Named_type*);
2279 // The type of elements of the array.
2280 Type* element_type_;
2281 // The number of elements. This may be NULL.
2282 Expression* length_;
2283 // The length as a tree. We only want to compute this once.
2287 // The type of a map.
2289 class Map_type : public Type
2292 Map_type(Type* key_type, Type* val_type, Location location)
2294 key_type_(key_type), val_type_(val_type), location_(location)
2297 // Return the key type.
2300 { return this->key_type_; }
2302 // Return the value type.
2305 { return this->val_type_; }
2307 // Whether this type is identical with T.
2309 is_identical(const Map_type* t, bool errors_are_identical) const;
2311 // Import a map type.
2316 make_map_type_descriptor_type();
2319 make_map_descriptor_type();
2321 // Build a map descriptor for this type. Return a pointer to it.
2322 // The location is the location which causes us to need the
2325 map_descriptor_pointer(Gogo* gogo, Location);
2329 do_traverse(Traverse*);
2335 do_has_pointer() const
2339 do_compare_is_identity(Gogo*) const
2343 do_hash_for_method(Gogo*) const;
2346 do_get_backend(Gogo*);
2349 do_type_descriptor(Gogo*, Named_type*);
2352 do_reflection(Gogo*, std::string*) const;
2355 do_mangled_name(Gogo*, std::string*) const;
2358 do_export(Export*) const;
2361 // Mapping from map types to map descriptors.
2362 typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
2363 Type_identical) Map_descriptors;
2364 static Map_descriptors map_descriptors;
2367 map_descriptor(Gogo*);
2373 // Where the type was defined.
2377 // The type of a channel.
2379 class Channel_type : public Type
2382 Channel_type(bool may_send, bool may_receive, Type* element_type)
2383 : Type(TYPE_CHANNEL),
2384 may_send_(may_send), may_receive_(may_receive),
2385 element_type_(element_type)
2386 { go_assert(may_send || may_receive); }
2388 // Whether this channel can send data.
2391 { return this->may_send_; }
2393 // Whether this channel can receive data.
2396 { return this->may_receive_; }
2398 // The type of the values that may be sent on this channel. This is
2399 // NULL if any type may be sent.
2401 element_type() const
2402 { return this->element_type_; }
2404 // Whether this type is identical with T.
2406 is_identical(const Channel_type* t, bool errors_are_identical) const;
2408 // Import a channel type.
2409 static Channel_type*
2413 make_chan_type_descriptor_type();
2417 do_traverse(Traverse* traverse)
2418 { return Type::traverse(this->element_type_, traverse); }
2421 do_has_pointer() const
2425 do_compare_is_identity(Gogo*) const
2429 do_hash_for_method(Gogo*) const;
2432 do_get_backend(Gogo*);
2435 do_type_descriptor(Gogo*, Named_type*);
2438 do_reflection(Gogo*, std::string*) const;
2441 do_mangled_name(Gogo*, std::string*) const;
2444 do_export(Export*) const;
2447 // Whether this channel can send data.
2449 // Whether this channel can receive data.
2451 // The types of elements which may be sent on this channel. If this
2452 // is NULL, it means that any type may be sent.
2453 Type* element_type_;
2456 // An interface type.
2458 class Interface_type : public Type
2461 Interface_type(Typed_identifier_list* methods, Location location)
2462 : Type(TYPE_INTERFACE),
2463 parse_methods_(methods), all_methods_(NULL), location_(location),
2464 interface_btype_(NULL), assume_identical_(NULL),
2465 methods_are_finalized_(false), seen_(false)
2466 { go_assert(methods == NULL || !methods->empty()); }
2468 // The location where the interface type was defined.
2471 { return this->location_; }
2473 // Return whether this is an empty interface.
2477 go_assert(this->methods_are_finalized_);
2478 return this->all_methods_ == NULL;
2481 // Return the list of methods. This will return NULL for an empty
2483 const Typed_identifier_list*
2486 go_assert(this->methods_are_finalized_);
2487 return this->all_methods_;
2490 // Return the number of methods.
2492 method_count() const
2494 go_assert(this->methods_are_finalized_);
2495 return this->all_methods_ == NULL ? 0 : this->all_methods_->size();
2498 // Return the method NAME, or NULL.
2499 const Typed_identifier*
2500 find_method(const std::string& name) const;
2502 // Return the zero-based index of method NAME.
2504 method_index(const std::string& name) const;
2506 // Finalize the methods. This sets all_methods_. This handles
2507 // interface inheritance.
2511 // Return true if T implements this interface. If this returns
2512 // false, and REASON is not NULL, it sets *REASON to the reason that
2515 implements_interface(const Type* t, std::string* reason) const;
2517 // Whether this type is identical with T. REASON is as in
2518 // implements_interface.
2520 is_identical(const Interface_type* t, bool errors_are_identical) const;
2522 // Whether we can assign T to this type. is_identical is known to
2525 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2527 // Return whether NAME is a method which is not exported. This is
2528 // only used for better error reporting.
2530 is_unexported_method(Gogo*, const std::string& name) const;
2532 // Import an interface type.
2533 static Interface_type*
2536 // Make a struct for an empty interface type.
2538 get_backend_empty_interface_type(Gogo*);
2540 // Finish the backend representation of the method types.
2542 finish_backend_methods(Gogo*);
2545 make_interface_type_descriptor_type();
2549 do_traverse(Traverse*);
2552 do_has_pointer() const
2556 do_compare_is_identity(Gogo*) const
2560 do_hash_for_method(Gogo*) const;
2563 do_get_backend(Gogo*);
2566 do_type_descriptor(Gogo*, Named_type*);
2569 do_reflection(Gogo*, std::string*) const;
2572 do_mangled_name(Gogo*, std::string*) const;
2575 do_export(Export*) const;
2578 // This type guards against infinite recursion when comparing
2579 // interface types. We keep a list of interface types assumed to be
2580 // identical during comparison. We just keep the list on the stack.
2581 // This permits us to compare cases like
2582 // type I1 interface { F() interface{I1} }
2583 // type I2 interface { F() interface{I2} }
2584 struct Assume_identical
2586 Assume_identical* next;
2587 const Interface_type* t1;
2588 const Interface_type* t2;
2592 assume_identical(const Interface_type*, const Interface_type*) const;
2594 // The list of methods associated with the interface from the
2595 // parser. This will be NULL for the empty interface. This may
2596 // include unnamed interface types.
2597 Typed_identifier_list* parse_methods_;
2598 // The list of all methods associated with the interface. This
2599 // expands any interface types listed in methods_. It is set by
2600 // finalize_methods. This will be NULL for the empty interface.
2601 Typed_identifier_list* all_methods_;
2602 // The location where the interface was defined.
2604 // The backend representation of this type during backend conversion.
2605 Btype* interface_btype_;
2606 // A list of interface types assumed to be identical during
2607 // interface comparison.
2608 mutable Assume_identical* assume_identical_;
2609 // Whether the methods have been finalized.
2610 bool methods_are_finalized_;
2611 // Used to avoid endless recursion in do_mangled_name.
2615 // The value we keep for a named type. This lets us get the right
2616 // name when we convert to trees. Note that we don't actually keep
2617 // the name here; the name is in the Named_object which points to
2618 // this. This object exists to hold a unique tree which represents
2621 class Named_type : public Type
2624 Named_type(Named_object* named_object, Type* type, Location location)
2626 named_object_(named_object), in_function_(NULL), type_(type),
2627 local_methods_(NULL), all_methods_(NULL),
2628 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2629 location_(location), named_btype_(NULL), dependencies_(),
2630 is_visible_(true), is_error_(false), is_placeholder_(false),
2631 is_converted_(false), is_circular_(false), seen_(false),
2632 seen_in_compare_is_identity_(false), seen_in_get_backend_(false)
2635 // Return the associated Named_object. This holds the actual name.
2638 { return this->named_object_; }
2641 named_object() const
2642 { return this->named_object_; }
2644 // Set the Named_object. This is used when we see a type
2645 // declaration followed by a type.
2647 set_named_object(Named_object* no)
2648 { this->named_object_ = no; }
2650 // Return the function in which this type is defined. This will
2651 // return NULL for a type defined in global scope.
2654 { return this->in_function_; }
2656 // Set the function in which this type is defined.
2658 set_in_function(Named_object* f)
2659 { this->in_function_ = f; }
2661 // Return the name of the type.
2665 // Return the name of the type for an error message. The difference
2666 // is that if the type is defined in a different package, this will
2667 // return PACKAGE.NAME.
2669 message_name() const;
2671 // Return the underlying type.
2674 { return this->type_; }
2678 { return this->type_; }
2680 // Return the location.
2683 { return this->location_; }
2685 // Whether this type is visible. This only matters when parsing.
2688 { return this->is_visible_; }
2690 // Mark this type as visible.
2693 { this->is_visible_ = true; }
2695 // Mark this type as invisible.
2698 { this->is_visible_ = false; }
2700 // Whether this is a builtin type.
2703 { return Linemap::is_predeclared_location(this->location_); }
2705 // Whether this is an alias. There are currently two aliases: byte
2710 // Whether this is a circular type: a pointer or function type that
2711 // refers to itself, which is not possible in C.
2714 { return this->is_circular_; }
2716 // Return the base type for this type.
2723 // Return whether this is an error type.
2725 is_named_error_type() const;
2727 // Return whether this type is comparable. If REASON is not NULL,
2728 // set *REASON when returning false.
2730 named_type_is_comparable(std::string* reason) const;
2732 // Add a method to this type.
2734 add_method(const std::string& name, Function*);
2736 // Add a method declaration to this type.
2738 add_method_declaration(const std::string& name, Package* package,
2739 Function_type* type, Location location);
2741 // Add an existing method--one defined before the type itself was
2742 // defined--to a type.
2744 add_existing_method(Named_object*);
2746 // Look up a local method.
2748 find_local_method(const std::string& name) const;
2750 // Return the list of local methods.
2752 local_methods() const
2753 { return this->local_methods_; }
2755 // Build the complete list of methods, including those from
2756 // anonymous fields, and build method stubs if needed.
2758 finalize_methods(Gogo*);
2760 // Return whether this type has any methods. This should only be
2761 // called after the finalize_methods pass.
2763 has_any_methods() const
2764 { return this->all_methods_ != NULL; }
2766 // Return the methods for this type. This should only be called
2767 // after the finalized_methods pass.
2770 { return this->all_methods_; }
2772 // Return the method to use for NAME. This returns NULL if there is
2773 // no such method or if the method is ambiguous. When it returns
2774 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2776 method_function(const std::string& name, bool *is_ambiguous) const;
2778 // Return whether NAME is a known field or method which is not
2779 // exported. This is only used for better error reporting.
2781 is_unexported_local_method(Gogo*, const std::string& name) const;
2783 // Return a pointer to the interface method table for this type for
2784 // the interface INTERFACE. If IS_POINTER is true, set the type
2785 // descriptor to a pointer to this type, otherwise set it to this
2788 interface_method_table(Gogo*, const Interface_type* interface,
2791 // Whether this type has any hidden fields.
2793 named_type_has_hidden_fields(std::string* reason) const;
2795 // Note that a type must be converted to the backend representation
2796 // before we convert this type.
2798 add_dependency(Named_type* nt)
2799 { this->dependencies_.push_back(nt); }
2801 // Return true if the size and alignment of the backend
2802 // representation of this type is known. This is always true after
2803 // types have been converted, but may be false beforehand.
2805 is_named_backend_type_size_known() const
2806 { return this->named_btype_ != NULL && !this->is_placeholder_; }
2810 export_named_type(Export*, const std::string& name) const;
2812 // Import a named type.
2814 import_named_type(Import*, Named_type**);
2816 // Initial conversion to backend representation.
2822 do_traverse(Traverse* traverse)
2823 { return Type::traverse(this->type_, traverse); }
2829 do_has_pointer() const;
2832 do_compare_is_identity(Gogo*) const;
2835 do_hash_for_method(Gogo*) const;
2838 do_get_backend(Gogo*);
2841 do_type_descriptor(Gogo*, Named_type*);
2844 do_reflection(Gogo*, std::string*) const;
2847 do_mangled_name(Gogo*, std::string* ret) const;
2850 do_export(Export*) const;
2853 // Create the placeholder during conversion.
2855 create_placeholder(Gogo*);
2857 // A mapping from interfaces to the associated interface method
2858 // tables for this type. This maps to a decl.
2859 typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
2860 Type_identical) Interface_method_tables;
2862 // A pointer back to the Named_object for this type.
2863 Named_object* named_object_;
2864 // If this type is defined in a function, a pointer back to the
2865 // function in which it is defined.
2866 Named_object* in_function_;
2869 // The list of methods defined for this type. Any named type can
2871 Bindings* local_methods_;
2872 // The full list of methods for this type, including methods
2873 // declared for anonymous fields.
2874 Methods* all_methods_;
2875 // A mapping from interfaces to the associated interface method
2876 // tables for this type.
2877 Interface_method_tables* interface_method_tables_;
2878 // A mapping from interfaces to the associated interface method
2879 // tables for pointers to this type.
2880 Interface_method_tables* pointer_interface_method_tables_;
2881 // The location where this type was defined.
2883 // The backend representation of this type during backend
2884 // conversion. This is used to avoid endless recursion when a named
2885 // type refers to itself.
2886 Btype* named_btype_;
2887 // A list of types which must be converted to the backend
2888 // representation before this type can be converted. This is for
2890 // type S1 { p *S2 }
2892 // where we can't convert S2 to the backend representation unless we
2893 // have converted S1.
2894 std::vector<Named_type*> dependencies_;
2895 // Whether this type is visible. This is false if this type was
2896 // created because it was referenced by an imported object, but the
2897 // type itself was not exported. This will always be true for types
2898 // created in the current package.
2900 // Whether this type is erroneous.
2902 // Whether the current value of named_btype_ is a placeholder for
2903 // which the final size of the type is not known.
2904 bool is_placeholder_;
2905 // Whether this type has been converted to the backend
2906 // representation. Implies that is_placeholder_ is false.
2908 // Whether this is a pointer or function type which refers to the
2911 // In a recursive operation such as has_hidden_fields, this flag is
2912 // used to prevent infinite recursion when a type refers to itself.
2913 // This is mutable because it is always reset to false when the
2916 // Like seen_, but used only by do_compare_is_identity.
2917 mutable bool seen_in_compare_is_identity_;
2918 // Like seen_, but used only by do_get_backend.
2919 bool seen_in_get_backend_;
2922 // A forward declaration. This handles a type which has been declared
2925 class Forward_declaration_type : public Type
2928 Forward_declaration_type(Named_object* named_object);
2930 // The named object associated with this type declaration. This
2931 // will be resolved.
2936 named_object() const;
2938 // Return the name of the type.
2942 // Return the type to which this points. Give an error if the type
2943 // has not yet been defined.
2950 // Whether the base type has been defined.
2954 // Add a method to this type.
2956 add_method(const std::string& name, Function*);
2958 // Add a method declaration to this type.
2960 add_method_declaration(const std::string& name, Package*, Function_type*,
2965 do_traverse(Traverse* traverse);
2968 do_has_pointer() const
2969 { return this->real_type()->has_pointer(); }
2972 do_compare_is_identity(Gogo* gogo) const
2973 { return this->real_type()->compare_is_identity(gogo); }
2976 do_hash_for_method(Gogo* gogo) const
2977 { return this->real_type()->hash_for_method(gogo); }
2980 do_get_backend(Gogo* gogo);
2983 do_type_descriptor(Gogo*, Named_type*);
2986 do_reflection(Gogo*, std::string*) const;
2989 do_mangled_name(Gogo*, std::string* ret) const;
2992 do_export(Export*) const;
2995 // Issue a warning about a use of an undefined type.
2999 // The type declaration.
3000 Named_object* named_object_;
3001 // Whether we have issued a warning about this type.
3002 mutable bool warned_;
3005 // The Type_context struct describes what we expect for the type of an
3010 // The exact type we expect, if known. This may be NULL.
3012 // Whether an abstract type is permitted.
3013 bool may_be_abstract;
3017 : type(NULL), may_be_abstract(false)
3020 Type_context(Type* a_type, bool a_may_be_abstract)
3021 : type(a_type), may_be_abstract(a_may_be_abstract)
3025 #endif // !defined(GO_TYPES_H)