1 // types.cc -- Go frontend types.
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.
11 #ifndef ENABLE_BUILD_WITH_CXX
23 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
31 #include "statements.h"
37 // Forward declarations so that we don't have to make types.h #include
41 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
43 std::vector<Backend::Btyped_identifier>* bfields);
46 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
47 std::vector<Backend::Btyped_identifier>* bfields);
50 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
52 std::vector<Backend::Btyped_identifier>* bfields);
56 Type::Type(Type_classification classification)
57 : classification_(classification), btype_is_placeholder_(false),
58 btype_(NULL), type_descriptor_var_(NULL)
66 // Get the base type for a type--skip names and forward declarations.
71 switch (this->classification_)
74 return this->named_type()->named_base();
76 return this->forward_declaration_type()->real_type()->base();
85 switch (this->classification_)
88 return this->named_type()->named_base();
90 return this->forward_declaration_type()->real_type()->base();
96 // Skip defined forward declarations.
102 Forward_declaration_type* ftype = t->forward_declaration_type();
103 while (ftype != NULL && ftype->is_defined())
105 t = ftype->real_type();
106 ftype = t->forward_declaration_type();
112 Type::forwarded() const
114 const Type* t = this;
115 const Forward_declaration_type* ftype = t->forward_declaration_type();
116 while (ftype != NULL && ftype->is_defined())
118 t = ftype->real_type();
119 ftype = t->forward_declaration_type();
124 // If this is a named type, return it. Otherwise, return NULL.
129 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
133 Type::named_type() const
135 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
138 // Return true if this type is not defined.
141 Type::is_undefined() const
143 return this->forwarded()->forward_declaration_type() != NULL;
146 // Return true if this is a basic type: a type which is not composed
147 // of other types, and is not void.
150 Type::is_basic_type() const
152 switch (this->classification_)
175 return this->base()->is_basic_type();
182 // Return true if this is an abstract type.
185 Type::is_abstract() const
187 switch (this->classification())
190 return this->integer_type()->is_abstract();
192 return this->float_type()->is_abstract();
194 return this->complex_type()->is_abstract();
196 return this->is_abstract_string_type();
198 return this->is_abstract_boolean_type();
204 // Return a non-abstract version of an abstract type.
207 Type::make_non_abstract_type()
209 go_assert(this->is_abstract());
210 switch (this->classification())
213 if (this->integer_type()->is_rune())
214 return Type::lookup_integer_type("int32");
216 return Type::lookup_integer_type("int");
218 return Type::lookup_float_type("float64");
220 return Type::lookup_complex_type("complex128");
222 return Type::lookup_string_type();
224 return Type::lookup_bool_type();
230 // Return true if this is an error type. Don't give an error if we
231 // try to dereference an undefined forwarding type, as this is called
232 // in the parser when the type may legitimately be undefined.
235 Type::is_error_type() const
237 const Type* t = this->forwarded();
238 // Note that we return false for an undefined forward type.
239 switch (t->classification_)
244 return t->named_type()->is_named_error_type();
250 // If this is a pointer type, return the type to which it points.
251 // Otherwise, return NULL.
254 Type::points_to() const
256 const Pointer_type* ptype = this->convert<const Pointer_type,
258 return ptype == NULL ? NULL : ptype->points_to();
261 // Return whether this is an open array type.
264 Type::is_slice_type() const
266 return this->array_type() != NULL && this->array_type()->length() == NULL;
269 // Return whether this is the predeclared constant nil being used as a
273 Type::is_nil_constant_as_type() const
275 const Type* t = this->forwarded();
276 if (t->forward_declaration_type() != NULL)
278 const Named_object* no = t->forward_declaration_type()->named_object();
279 if (no->is_unknown())
280 no = no->unknown_value()->real_named_object();
283 && no->const_value()->expr()->is_nil_expression())
292 Type::traverse(Type* type, Traverse* traverse)
294 go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
295 || (traverse->traverse_mask()
296 & Traverse::traverse_expressions) != 0);
297 if (traverse->remember_type(type))
299 // We have already traversed this type.
300 return TRAVERSE_CONTINUE;
302 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
304 int t = traverse->type(type);
305 if (t == TRAVERSE_EXIT)
306 return TRAVERSE_EXIT;
307 else if (t == TRAVERSE_SKIP_COMPONENTS)
308 return TRAVERSE_CONTINUE;
310 // An array type has an expression which we need to traverse if
311 // traverse_expressions is set.
312 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
313 return TRAVERSE_EXIT;
314 return TRAVERSE_CONTINUE;
317 // Default implementation for do_traverse for child class.
320 Type::do_traverse(Traverse*)
322 return TRAVERSE_CONTINUE;
325 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
326 // then return true for all erroneous types; this is used to avoid
327 // cascading errors. If REASON is not NULL, optionally set *REASON to
328 // the reason the types are not identical.
331 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
334 if (t1 == NULL || t2 == NULL)
336 // Something is wrong.
337 return errors_are_identical ? true : t1 == t2;
340 // Skip defined forward declarations.
341 t1 = t1->forwarded();
342 t2 = t2->forwarded();
344 // Ignore aliases for purposes of type identity.
345 if (t1->named_type() != NULL && t1->named_type()->is_alias())
346 t1 = t1->named_type()->real_type();
347 if (t2->named_type() != NULL && t2->named_type()->is_alias())
348 t2 = t2->named_type()->real_type();
353 // An undefined forward declaration is an error.
354 if (t1->forward_declaration_type() != NULL
355 || t2->forward_declaration_type() != NULL)
356 return errors_are_identical;
358 // Avoid cascading errors with error types.
359 if (t1->is_error_type() || t2->is_error_type())
361 if (errors_are_identical)
363 return t1->is_error_type() && t2->is_error_type();
366 // Get a good reason for the sink type. Note that the sink type on
367 // the left hand side of an assignment is handled in are_assignable.
368 if (t1->is_sink_type() || t2->is_sink_type())
371 *reason = "invalid use of _";
375 // A named type is only identical to itself.
376 if (t1->named_type() != NULL || t2->named_type() != NULL)
379 // Check type shapes.
380 if (t1->classification() != t2->classification())
383 switch (t1->classification())
389 // These types are always identical.
393 return t1->integer_type()->is_identical(t2->integer_type());
396 return t1->float_type()->is_identical(t2->float_type());
399 return t1->complex_type()->is_identical(t2->complex_type());
402 return t1->function_type()->is_identical(t2->function_type(),
404 errors_are_identical,
408 return Type::are_identical(t1->points_to(), t2->points_to(),
409 errors_are_identical, reason);
412 return t1->struct_type()->is_identical(t2->struct_type(),
413 errors_are_identical);
416 return t1->array_type()->is_identical(t2->array_type(),
417 errors_are_identical);
420 return t1->map_type()->is_identical(t2->map_type(),
421 errors_are_identical);
424 return t1->channel_type()->is_identical(t2->channel_type(),
425 errors_are_identical);
428 return t1->interface_type()->is_identical(t2->interface_type(),
429 errors_are_identical);
431 case TYPE_CALL_MULTIPLE_RESULT:
433 *reason = "invalid use of multiple value function call";
441 // Return true if it's OK to have a binary operation with types LHS
442 // and RHS. This is not used for shifts or comparisons.
445 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
447 if (Type::are_identical(lhs, rhs, true, NULL))
450 // A constant of abstract bool type may be mixed with any bool type.
451 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
452 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
455 // A constant of abstract string type may be mixed with any string
457 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
458 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
464 // A constant of abstract integer, float, or complex type may be
465 // mixed with an integer, float, or complex type.
466 if ((rhs->is_abstract()
467 && (rhs->integer_type() != NULL
468 || rhs->float_type() != NULL
469 || rhs->complex_type() != NULL)
470 && (lhs->integer_type() != NULL
471 || lhs->float_type() != NULL
472 || lhs->complex_type() != NULL))
473 || (lhs->is_abstract()
474 && (lhs->integer_type() != NULL
475 || lhs->float_type() != NULL
476 || lhs->complex_type() != NULL)
477 && (rhs->integer_type() != NULL
478 || rhs->float_type() != NULL
479 || rhs->complex_type() != NULL)))
482 // The nil type may be compared to a pointer, an interface type, a
483 // slice type, a channel type, a map type, or a function type.
484 if (lhs->is_nil_type()
485 && (rhs->points_to() != NULL
486 || rhs->interface_type() != NULL
487 || rhs->is_slice_type()
488 || rhs->map_type() != NULL
489 || rhs->channel_type() != NULL
490 || rhs->function_type() != NULL))
492 if (rhs->is_nil_type()
493 && (lhs->points_to() != NULL
494 || lhs->interface_type() != NULL
495 || lhs->is_slice_type()
496 || lhs->map_type() != NULL
497 || lhs->channel_type() != NULL
498 || lhs->function_type() != NULL))
504 // Return true if a value with type T1 may be compared with a value of
505 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
508 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
509 const Type *t2, std::string *reason)
512 && !Type::are_assignable(t1, t2, NULL)
513 && !Type::are_assignable(t2, t1, NULL))
516 *reason = "incompatible types in binary expression";
522 if (t1->integer_type() == NULL
523 && t1->float_type() == NULL
524 && !t1->is_string_type())
527 *reason = _("invalid comparison of non-ordered type");
531 else if (t1->is_slice_type()
532 || t1->map_type() != NULL
533 || t1->function_type() != NULL
534 || t2->is_slice_type()
535 || t2->map_type() != NULL
536 || t2->function_type() != NULL)
538 if (!t1->is_nil_type() && !t2->is_nil_type())
542 if (t1->is_slice_type() || t2->is_slice_type())
543 *reason = _("slice can only be compared to nil");
544 else if (t1->map_type() != NULL || t2->map_type() != NULL)
545 *reason = _("map can only be compared to nil");
547 *reason = _("func can only be compared to nil");
549 // Match 6g error messages.
550 if (t1->interface_type() != NULL || t2->interface_type() != NULL)
553 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
563 if (!t1->is_boolean_type()
564 && t1->integer_type() == NULL
565 && t1->float_type() == NULL
566 && t1->complex_type() == NULL
567 && !t1->is_string_type()
568 && t1->points_to() == NULL
569 && t1->channel_type() == NULL
570 && t1->interface_type() == NULL
571 && t1->struct_type() == NULL
572 && t1->array_type() == NULL
573 && !t1->is_nil_type())
576 *reason = _("invalid comparison of non-comparable type");
580 if (t1->named_type() != NULL)
581 return t1->named_type()->named_type_is_comparable(reason);
582 else if (t2->named_type() != NULL)
583 return t2->named_type()->named_type_is_comparable(reason);
584 else if (t1->struct_type() != NULL)
586 const Struct_field_list* fields = t1->struct_type()->fields();
587 for (Struct_field_list::const_iterator p = fields->begin();
591 if (Gogo::is_sink_name(p->field_name()))
594 if (!p->type()->is_comparable())
597 *reason = _("invalid comparison of non-comparable struct");
602 else if (t1->array_type() != NULL)
604 if (t1->array_type()->length()->is_nil_expression()
605 || !t1->array_type()->element_type()->is_comparable())
608 *reason = _("invalid comparison of non-comparable array");
617 // Return true if a value with type RHS may be assigned to a variable
618 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
619 // hidden fields are modified. If REASON is not NULL, set *REASON to
620 // the reason the types are not assignable.
623 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
624 bool check_hidden_fields,
627 // Do some checks first. Make sure the types are defined.
628 if (rhs != NULL && !rhs->is_undefined())
630 if (rhs->is_void_type())
633 *reason = "non-value used as value";
636 if (rhs->is_call_multiple_result_type())
639 reason->assign(_("multiple value function call in "
640 "single value context"));
645 if (lhs != NULL && !lhs->is_undefined())
647 // Any value may be assigned to the blank identifier.
648 if (lhs->is_sink_type())
651 // All fields of a struct must be exported, or the assignment
652 // must be in the same package.
653 if (check_hidden_fields && rhs != NULL && !rhs->is_undefined())
655 if (lhs->has_hidden_fields(NULL, reason)
656 || rhs->has_hidden_fields(NULL, reason))
661 // Identical types are assignable.
662 if (Type::are_identical(lhs, rhs, true, reason))
665 // The types are assignable if they have identical underlying types
666 // and either LHS or RHS is not a named type.
667 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
668 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
669 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
672 // The types are assignable if LHS is an interface type and RHS
673 // implements the required methods.
674 const Interface_type* lhs_interface_type = lhs->interface_type();
675 if (lhs_interface_type != NULL)
677 if (lhs_interface_type->implements_interface(rhs, reason))
679 const Interface_type* rhs_interface_type = rhs->interface_type();
680 if (rhs_interface_type != NULL
681 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
686 // The type are assignable if RHS is a bidirectional channel type,
687 // LHS is a channel type, they have identical element types, and
688 // either LHS or RHS is not a named type.
689 if (lhs->channel_type() != NULL
690 && rhs->channel_type() != NULL
691 && rhs->channel_type()->may_send()
692 && rhs->channel_type()->may_receive()
693 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
694 && Type::are_identical(lhs->channel_type()->element_type(),
695 rhs->channel_type()->element_type(),
700 // The nil type may be assigned to a pointer, function, slice, map,
701 // channel, or interface type.
702 if (rhs->is_nil_type()
703 && (lhs->points_to() != NULL
704 || lhs->function_type() != NULL
705 || lhs->is_slice_type()
706 || lhs->map_type() != NULL
707 || lhs->channel_type() != NULL
708 || lhs->interface_type() != NULL))
711 // An untyped numeric constant may be assigned to a numeric type if
712 // it is representable in that type.
713 if ((rhs->is_abstract()
714 && (rhs->integer_type() != NULL
715 || rhs->float_type() != NULL
716 || rhs->complex_type() != NULL))
717 && (lhs->integer_type() != NULL
718 || lhs->float_type() != NULL
719 || lhs->complex_type() != NULL))
722 // Give some better error messages.
723 if (reason != NULL && reason->empty())
725 if (rhs->interface_type() != NULL)
726 reason->assign(_("need explicit conversion"));
727 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
729 size_t len = (lhs->named_type()->name().length()
730 + rhs->named_type()->name().length()
732 char* buf = new char[len];
733 snprintf(buf, len, _("cannot use type %s as type %s"),
734 rhs->named_type()->message_name().c_str(),
735 lhs->named_type()->message_name().c_str());
744 // Return true if a value with type RHS may be assigned to a variable
745 // with type LHS. If REASON is not NULL, set *REASON to the reason
746 // the types are not assignable.
749 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
751 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
754 // Like are_assignable but don't check for hidden fields.
757 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
760 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
763 // Return true if a value with type RHS may be converted to type LHS.
764 // If REASON is not NULL, set *REASON to the reason the types are not
768 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
770 // The types are convertible if they are assignable.
771 if (Type::are_assignable(lhs, rhs, reason))
774 // The types are convertible if they have identical underlying
776 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
777 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
780 // The types are convertible if they are both unnamed pointer types
781 // and their pointer base types have identical underlying types.
782 if (lhs->named_type() == NULL
783 && rhs->named_type() == NULL
784 && lhs->points_to() != NULL
785 && rhs->points_to() != NULL
786 && (lhs->points_to()->named_type() != NULL
787 || rhs->points_to()->named_type() != NULL)
788 && Type::are_identical(lhs->points_to()->base(),
789 rhs->points_to()->base(),
794 // Integer and floating point types are convertible to each other.
795 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
796 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
799 // Complex types are convertible to each other.
800 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
803 // An integer, or []byte, or []rune, may be converted to a string.
804 if (lhs->is_string_type())
806 if (rhs->integer_type() != NULL)
808 if (rhs->is_slice_type())
810 const Type* e = rhs->array_type()->element_type()->forwarded();
811 if (e->integer_type() != NULL
812 && (e->integer_type()->is_byte()
813 || e->integer_type()->is_rune()))
818 // A string may be converted to []byte or []rune.
819 if (rhs->is_string_type() && lhs->is_slice_type())
821 const Type* e = lhs->array_type()->element_type()->forwarded();
822 if (e->integer_type() != NULL
823 && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
827 // An unsafe.Pointer type may be converted to any pointer type or to
828 // uintptr, and vice-versa.
829 if (lhs->is_unsafe_pointer_type()
830 && (rhs->points_to() != NULL
831 || (rhs->integer_type() != NULL
832 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
834 if (rhs->is_unsafe_pointer_type()
835 && (lhs->points_to() != NULL
836 || (lhs->integer_type() != NULL
837 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
840 // Give a better error message.
844 *reason = "invalid type conversion";
847 std::string s = "invalid type conversion (";
857 // Return whether this type has any hidden fields. This is only a
858 // possibility for a few types.
861 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
863 switch (this->forwarded()->classification_)
866 return this->named_type()->named_type_has_hidden_fields(reason);
868 return this->struct_type()->struct_has_hidden_fields(within, reason);
870 return this->array_type()->array_has_hidden_fields(within, reason);
876 // Return a hash code for the type to be used for method lookup.
879 Type::hash_for_method(Gogo* gogo) const
881 unsigned int ret = 0;
882 if (this->classification_ != TYPE_FORWARD)
883 ret += this->classification_;
884 return ret + this->do_hash_for_method(gogo);
887 // Default implementation of do_hash_for_method. This is appropriate
888 // for types with no subfields.
891 Type::do_hash_for_method(Gogo*) const
896 // Return a hash code for a string, given a starting hash.
899 Type::hash_string(const std::string& s, unsigned int h)
901 const char* p = s.data();
902 size_t len = s.length();
903 for (; len > 0; --len)
911 // A hash table mapping unnamed types to the backend representation of
914 Type::Type_btypes Type::type_btypes;
916 // Return a tree representing this type.
919 Type::get_backend(Gogo* gogo)
921 if (this->btype_ != NULL)
923 if (this->btype_is_placeholder_ && gogo->named_types_are_converted())
924 this->finish_backend(gogo);
928 if (this->forward_declaration_type() != NULL
929 || this->named_type() != NULL)
930 return this->get_btype_without_hash(gogo);
932 if (this->is_error_type())
933 return gogo->backend()->error_type();
935 // To avoid confusing the backend, translate all identical Go types
936 // to the same backend representation. We use a hash table to do
937 // that. There is no need to use the hash table for named types, as
938 // named types are only identical to themselves.
940 std::pair<Type*, Btype*> val(this, NULL);
941 std::pair<Type_btypes::iterator, bool> ins =
942 Type::type_btypes.insert(val);
943 if (!ins.second && ins.first->second != NULL)
945 if (gogo != NULL && gogo->named_types_are_converted())
946 this->btype_ = ins.first->second;
947 return ins.first->second;
950 Btype* bt = this->get_btype_without_hash(gogo);
952 if (ins.first->second == NULL)
953 ins.first->second = bt;
956 // We have already created a backend representation for this
957 // type. This can happen when an unnamed type is defined using
958 // a named type which in turns uses an identical unnamed type.
959 // Use the tree we created earlier and ignore the one we just
961 bt = ins.first->second;
962 if (gogo == NULL || !gogo->named_types_are_converted())
970 // Return the backend representation for a type without looking in the
971 // hash table for identical types. This is used for named types,
972 // since a named type is never identical to any other type.
975 Type::get_btype_without_hash(Gogo* gogo)
977 if (this->btype_ == NULL)
979 Btype* bt = this->do_get_backend(gogo);
981 // For a recursive function or pointer type, we will temporarily
982 // return a circular pointer type during the recursion. We
983 // don't want to record that for a forwarding type, as it may
985 if (this->forward_declaration_type() != NULL
986 && gogo->backend()->is_circular_pointer_type(bt))
989 if (gogo == NULL || !gogo->named_types_are_converted())
997 // Get the backend representation of a type without forcing the
998 // creation of the backend representation of all supporting types.
999 // This will return a backend type that has the correct size but may
1000 // be incomplete. E.g., a pointer will just be a placeholder pointer,
1001 // and will not contain the final representation of the type to which
1002 // it points. This is used while converting all named types to the
1003 // backend representation, to avoid problems with indirect references
1004 // to types which are not yet complete. When this is called, the
1005 // sizes of all direct references (e.g., a struct field) should be
1006 // known, but the sizes of indirect references (e.g., the type to
1007 // which a pointer points) may not.
1010 Type::get_backend_placeholder(Gogo* gogo)
1012 if (gogo->named_types_are_converted())
1013 return this->get_backend(gogo);
1014 if (this->btype_ != NULL)
1015 return this->btype_;
1018 switch (this->classification_)
1028 // These are simple types that can just be created directly.
1029 return this->get_backend(gogo);
1033 Location loc = this->function_type()->location();
1034 bt = gogo->backend()->placeholder_pointer_type("", loc, true);
1040 Location loc = Linemap::unknown_location();
1041 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1046 // We don't have to make the struct itself be a placeholder. We
1047 // are promised that we know the sizes of the struct fields.
1048 // But we may have to use a placeholder for any particular
1051 std::vector<Backend::Btyped_identifier> bfields;
1052 get_backend_struct_fields(gogo, this->struct_type()->fields(),
1054 bt = gogo->backend()->struct_type(bfields);
1059 if (this->is_slice_type())
1061 std::vector<Backend::Btyped_identifier> bfields;
1062 get_backend_slice_fields(gogo, this->array_type(), true, &bfields);
1063 bt = gogo->backend()->struct_type(bfields);
1067 Btype* element = this->array_type()->get_backend_element(gogo, true);
1068 Bexpression* len = this->array_type()->get_backend_length(gogo);
1069 bt = gogo->backend()->array_type(element, len);
1075 // All maps and channels have the same backend representation.
1076 return this->get_backend(gogo);
1078 case TYPE_INTERFACE:
1079 if (this->interface_type()->is_empty())
1080 return Interface_type::get_backend_empty_interface_type(gogo);
1083 std::vector<Backend::Btyped_identifier> bfields;
1084 get_backend_interface_fields(gogo, this->interface_type(), true,
1086 bt = gogo->backend()->struct_type(bfields);
1092 // Named types keep track of their own dependencies and manage
1093 // their own placeholders.
1094 return this->get_backend(gogo);
1097 case TYPE_CALL_MULTIPLE_RESULT:
1103 this->btype_is_placeholder_ = true;
1107 // Complete the backend representation. This is called for a type
1108 // using a placeholder type.
1111 Type::finish_backend(Gogo* gogo)
1113 go_assert(this->btype_ != NULL);
1114 if (!this->btype_is_placeholder_)
1117 switch (this->classification_)
1131 Btype* bt = this->do_get_backend(gogo);
1132 if (!gogo->backend()->set_placeholder_function_type(this->btype_, bt))
1133 go_assert(saw_errors());
1139 Btype* bt = this->do_get_backend(gogo);
1140 if (!gogo->backend()->set_placeholder_pointer_type(this->btype_, bt))
1141 go_assert(saw_errors());
1146 // The struct type itself is done, but we have to make sure that
1147 // all the field types are converted.
1148 this->struct_type()->finish_backend_fields(gogo);
1152 // The array type itself is done, but make sure the element type
1154 this->array_type()->finish_backend_element(gogo);
1161 case TYPE_INTERFACE:
1162 // The interface type itself is done, but make sure the method
1163 // types are converted.
1164 this->interface_type()->finish_backend_methods(gogo);
1172 case TYPE_CALL_MULTIPLE_RESULT:
1177 this->btype_is_placeholder_ = false;
1180 // Return a pointer to the type descriptor for this type.
1183 Type::type_descriptor_pointer(Gogo* gogo, Location location)
1185 Type* t = this->forwarded();
1186 if (t->named_type() != NULL && t->named_type()->is_alias())
1187 t = t->named_type()->real_type();
1188 if (t->type_descriptor_var_ == NULL)
1190 t->make_type_descriptor_var(gogo);
1191 go_assert(t->type_descriptor_var_ != NULL);
1193 tree var_tree = var_to_tree(t->type_descriptor_var_);
1194 if (var_tree == error_mark_node)
1195 return error_mark_node;
1196 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
1199 // A mapping from unnamed types to type descriptor variables.
1201 Type::Type_descriptor_vars Type::type_descriptor_vars;
1203 // Build the type descriptor for this type.
1206 Type::make_type_descriptor_var(Gogo* gogo)
1208 go_assert(this->type_descriptor_var_ == NULL);
1210 Named_type* nt = this->named_type();
1212 // We can have multiple instances of unnamed types, but we only want
1213 // to emit the type descriptor once. We use a hash table. This is
1214 // not necessary for named types, as they are unique, and we store
1215 // the type descriptor in the type itself.
1216 Bvariable** phash = NULL;
1219 Bvariable* bvnull = NULL;
1220 std::pair<Type_descriptor_vars::iterator, bool> ins =
1221 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1224 // We've already build a type descriptor for this type.
1225 this->type_descriptor_var_ = ins.first->second;
1228 phash = &ins.first->second;
1231 std::string var_name = this->type_descriptor_var_name(gogo, nt);
1233 // Build the contents of the type descriptor.
1234 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1236 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1238 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1240 const Package* dummy;
1241 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1243 this->type_descriptor_var_ =
1244 gogo->backend()->immutable_struct_reference(var_name,
1248 *phash = this->type_descriptor_var_;
1252 // See if this type descriptor can appear in multiple packages.
1253 bool is_common = false;
1256 // We create the descriptor for a builtin type whenever we need
1258 is_common = nt->is_builtin();
1262 // This is an unnamed type. The descriptor could be defined in
1263 // any package where it is needed, and the linker will pick one
1264 // descriptor to keep.
1268 // We are going to build the type descriptor in this package. We
1269 // must create the variable before we convert the initializer to the
1270 // backend representation, because the initializer may refer to the
1271 // type descriptor of this type. By setting type_descriptor_var_ we
1272 // ensure that type_descriptor_pointer will work if called while
1273 // converting INITIALIZER.
1275 this->type_descriptor_var_ =
1276 gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
1279 *phash = this->type_descriptor_var_;
1281 Translate_context context(gogo, NULL, NULL, NULL);
1282 context.set_is_const();
1283 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
1285 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1286 var_name, is_common,
1287 initializer_btype, loc,
1291 // Return the name of the type descriptor variable. If NT is not
1292 // NULL, use it to get the name. Otherwise this is an unnamed type.
1295 Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
1298 return "__go_td_" + this->mangled_name(gogo);
1300 Named_object* no = nt->named_object();
1302 const Named_object* in_function = nt->in_function(&index);
1303 std::string ret = "__go_tdn_";
1304 if (nt->is_builtin())
1305 go_assert(in_function == NULL);
1308 const std::string& pkgpath(no->package() == NULL
1309 ? gogo->pkgpath_symbol()
1310 : no->package()->pkgpath_symbol());
1311 ret.append(pkgpath);
1313 if (in_function != NULL)
1315 ret.append(Gogo::unpack_hidden_name(in_function->name()));
1320 snprintf(buf, sizeof buf, "%u", index);
1327 // FIXME: This adds in pkgpath twice for hidden symbols, which is
1329 const std::string& name(no->name());
1330 if (!Gogo::is_hidden_name(name))
1335 ret.append(Gogo::pkgpath_for_symbol(Gogo::hidden_name_pkgpath(name)));
1337 ret.append(Gogo::unpack_hidden_name(name));
1343 // Return true if this type descriptor is defined in a different
1344 // package. If this returns true it sets *PACKAGE to the package.
1347 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1348 const Package** package)
1352 if (nt->named_object()->package() != NULL)
1354 // This is a named type defined in a different package. The
1355 // type descriptor should be defined in that package.
1356 *package = nt->named_object()->package();
1362 if (this->points_to() != NULL
1363 && this->points_to()->named_type() != NULL
1364 && this->points_to()->named_type()->named_object()->package() != NULL)
1366 // This is an unnamed pointer to a named type defined in a
1367 // different package. The descriptor should be defined in
1369 *package = this->points_to()->named_type()->named_object()->package();
1376 // Return a composite literal for a type descriptor.
1379 Type::type_descriptor(Gogo* gogo, Type* type)
1381 return type->do_type_descriptor(gogo, NULL);
1384 // Return a composite literal for a type descriptor with a name.
1387 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1389 go_assert(name != NULL && type->named_type() != name);
1390 return type->do_type_descriptor(gogo, name);
1393 // Make a builtin struct type from a list of fields. The fields are
1394 // pairs of a name and a type.
1397 Type::make_builtin_struct_type(int nfields, ...)
1400 va_start(ap, nfields);
1402 Location bloc = Linemap::predeclared_location();
1403 Struct_field_list* sfl = new Struct_field_list();
1404 for (int i = 0; i < nfields; i++)
1406 const char* field_name = va_arg(ap, const char *);
1407 Type* type = va_arg(ap, Type*);
1408 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1413 return Type::make_struct_type(sfl, bloc);
1416 // A list of builtin named types.
1418 std::vector<Named_type*> Type::named_builtin_types;
1420 // Make a builtin named type.
1423 Type::make_builtin_named_type(const char* name, Type* type)
1425 Location bloc = Linemap::predeclared_location();
1426 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1427 Named_type* ret = no->type_value();
1428 Type::named_builtin_types.push_back(ret);
1432 // Convert the named builtin types.
1435 Type::convert_builtin_named_types(Gogo* gogo)
1437 for (std::vector<Named_type*>::const_iterator p =
1438 Type::named_builtin_types.begin();
1439 p != Type::named_builtin_types.end();
1442 bool r = (*p)->verify();
1444 (*p)->convert(gogo);
1448 // Return the type of a type descriptor. We should really tie this to
1449 // runtime.Type rather than copying it. This must match commonType in
1450 // libgo/go/runtime/type.go.
1453 Type::make_type_descriptor_type()
1458 Location bloc = Linemap::predeclared_location();
1460 Type* uint8_type = Type::lookup_integer_type("uint8");
1461 Type* uint32_type = Type::lookup_integer_type("uint32");
1462 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1463 Type* string_type = Type::lookup_string_type();
1464 Type* pointer_string_type = Type::make_pointer_type(string_type);
1466 // This is an unnamed version of unsafe.Pointer. Perhaps we
1467 // should use the named version instead, although that would
1468 // require us to create the unsafe package if it has not been
1469 // imported. It probably doesn't matter.
1470 Type* void_type = Type::make_void_type();
1471 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1473 // Forward declaration for the type descriptor type.
1474 Named_object* named_type_descriptor_type =
1475 Named_object::make_type_declaration("commonType", NULL, bloc);
1476 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1477 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1479 // The type of a method on a concrete type.
1480 Struct_type* method_type =
1481 Type::make_builtin_struct_type(5,
1482 "name", pointer_string_type,
1483 "pkgPath", pointer_string_type,
1484 "mtyp", pointer_type_descriptor_type,
1485 "typ", pointer_type_descriptor_type,
1486 "tfn", unsafe_pointer_type);
1487 Named_type* named_method_type =
1488 Type::make_builtin_named_type("method", method_type);
1490 // Information for types with a name or methods.
1491 Type* slice_named_method_type =
1492 Type::make_array_type(named_method_type, NULL);
1493 Struct_type* uncommon_type =
1494 Type::make_builtin_struct_type(3,
1495 "name", pointer_string_type,
1496 "pkgPath", pointer_string_type,
1497 "methods", slice_named_method_type);
1498 Named_type* named_uncommon_type =
1499 Type::make_builtin_named_type("uncommonType", uncommon_type);
1501 Type* pointer_uncommon_type =
1502 Type::make_pointer_type(named_uncommon_type);
1504 // The type descriptor type.
1506 Typed_identifier_list* params = new Typed_identifier_list();
1507 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1508 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1510 Typed_identifier_list* results = new Typed_identifier_list();
1511 results->push_back(Typed_identifier("", uintptr_type, bloc));
1513 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1515 params = new Typed_identifier_list();
1516 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1517 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1518 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1520 results = new Typed_identifier_list();
1521 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1523 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1526 Struct_type* type_descriptor_type =
1527 Type::make_builtin_struct_type(10,
1529 "align", uint8_type,
1530 "fieldAlign", uint8_type,
1531 "size", uintptr_type,
1532 "hash", uint32_type,
1533 "hashfn", hashfn_type,
1534 "equalfn", equalfn_type,
1535 "string", pointer_string_type,
1536 "", pointer_uncommon_type,
1538 pointer_type_descriptor_type);
1540 Named_type* named = Type::make_builtin_named_type("commonType",
1541 type_descriptor_type);
1543 named_type_descriptor_type->set_type_value(named);
1551 // Make the type of a pointer to a type descriptor as represented in
1555 Type::make_type_descriptor_ptr_type()
1559 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1563 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1564 // hash code for this type and which compare whether two values of
1565 // this type are equal. If NAME is not NULL it is the name of this
1566 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1567 // functions, for convenience; they may be NULL.
1570 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1571 Function_type* equal_fntype, Named_object** hash_fn,
1572 Named_object** equal_fn)
1574 if (hash_fntype == NULL || equal_fntype == NULL)
1576 Location bloc = Linemap::predeclared_location();
1578 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1579 Type* void_type = Type::make_void_type();
1580 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1582 if (hash_fntype == NULL)
1584 Typed_identifier_list* params = new Typed_identifier_list();
1585 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1587 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1589 Typed_identifier_list* results = new Typed_identifier_list();
1590 results->push_back(Typed_identifier("", uintptr_type, bloc));
1592 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1594 if (equal_fntype == NULL)
1596 Typed_identifier_list* params = new Typed_identifier_list();
1597 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1599 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1601 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1603 Typed_identifier_list* results = new Typed_identifier_list();
1604 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1607 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1611 const char* hash_fnname;
1612 const char* equal_fnname;
1613 if (this->compare_is_identity(gogo))
1615 hash_fnname = "__go_type_hash_identity";
1616 equal_fnname = "__go_type_equal_identity";
1618 else if (!this->is_comparable())
1620 hash_fnname = "__go_type_hash_error";
1621 equal_fnname = "__go_type_equal_error";
1625 switch (this->base()->classification())
1627 case Type::TYPE_ERROR:
1628 case Type::TYPE_VOID:
1629 case Type::TYPE_NIL:
1630 case Type::TYPE_FUNCTION:
1631 case Type::TYPE_MAP:
1632 // For these types is_comparable should have returned false.
1635 case Type::TYPE_BOOLEAN:
1636 case Type::TYPE_INTEGER:
1637 case Type::TYPE_POINTER:
1638 case Type::TYPE_CHANNEL:
1639 // For these types compare_is_identity should have returned true.
1642 case Type::TYPE_FLOAT:
1643 hash_fnname = "__go_type_hash_float";
1644 equal_fnname = "__go_type_equal_float";
1647 case Type::TYPE_COMPLEX:
1648 hash_fnname = "__go_type_hash_complex";
1649 equal_fnname = "__go_type_equal_complex";
1652 case Type::TYPE_STRING:
1653 hash_fnname = "__go_type_hash_string";
1654 equal_fnname = "__go_type_equal_string";
1657 case Type::TYPE_STRUCT:
1659 // This is a struct which can not be compared using a
1660 // simple identity function. We need to build a function
1662 this->specific_type_functions(gogo, name, hash_fntype,
1663 equal_fntype, hash_fn, equal_fn);
1667 case Type::TYPE_ARRAY:
1668 if (this->is_slice_type())
1670 // Type::is_compatible_for_comparison should have
1676 // This is an array which can not be compared using a
1677 // simple identity function. We need to build a
1678 // function for comparison.
1679 this->specific_type_functions(gogo, name, hash_fntype,
1680 equal_fntype, hash_fn, equal_fn);
1685 case Type::TYPE_INTERFACE:
1686 if (this->interface_type()->is_empty())
1688 hash_fnname = "__go_type_hash_empty_interface";
1689 equal_fnname = "__go_type_equal_empty_interface";
1693 hash_fnname = "__go_type_hash_interface";
1694 equal_fnname = "__go_type_equal_interface";
1698 case Type::TYPE_NAMED:
1699 case Type::TYPE_FORWARD:
1708 Location bloc = Linemap::predeclared_location();
1709 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1711 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1712 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1713 equal_fntype, bloc);
1714 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1717 // A hash table mapping types to the specific hash functions.
1719 Type::Type_functions Type::type_functions_table;
1721 // Handle a type function which is specific to a type: a struct or
1722 // array which can not use an identity comparison.
1725 Type::specific_type_functions(Gogo* gogo, Named_type* name,
1726 Function_type* hash_fntype,
1727 Function_type* equal_fntype,
1728 Named_object** hash_fn,
1729 Named_object** equal_fn)
1731 Hash_equal_fn fnull(NULL, NULL);
1732 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1733 std::pair<Type_functions::iterator, bool> ins =
1734 Type::type_functions_table.insert(val);
1737 // We already have functions for this type
1738 *hash_fn = ins.first->second.first;
1739 *equal_fn = ins.first->second.second;
1743 std::string base_name;
1746 // Mangled names can have '.' if they happen to refer to named
1747 // types in some way. That's fine if this is simply a named
1748 // type, but otherwise it will confuse the code that builds
1749 // function identifiers. Remove '.' when necessary.
1750 base_name = this->mangled_name(gogo);
1752 while ((i = base_name.find('.')) != std::string::npos)
1754 base_name = gogo->pack_hidden_name(base_name, false);
1758 // This name is already hidden or not as appropriate.
1759 base_name = name->name();
1761 const Named_object* in_function = name->in_function(&index);
1762 if (in_function != NULL)
1764 base_name += '$' + Gogo::unpack_hidden_name(in_function->name());
1768 snprintf(buf, sizeof buf, "%u", index);
1774 std::string hash_name = base_name + "$hash";
1775 std::string equal_name = base_name + "$equal";
1777 Location bloc = Linemap::predeclared_location();
1779 const Package* package = NULL;
1780 bool is_defined_elsewhere =
1781 this->type_descriptor_defined_elsewhere(name, &package);
1782 if (is_defined_elsewhere)
1784 *hash_fn = Named_object::make_function_declaration(hash_name, package,
1786 *equal_fn = Named_object::make_function_declaration(equal_name, package,
1787 equal_fntype, bloc);
1791 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1792 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1796 ins.first->second.first = *hash_fn;
1797 ins.first->second.second = *equal_fn;
1799 if (!is_defined_elsewhere)
1801 if (gogo->in_global_scope())
1802 this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1803 equal_name, equal_fntype);
1805 gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1806 equal_name, equal_fntype);
1810 // Write the hash and equality functions for a type which needs to be
1811 // written specially.
1814 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1815 const std::string& hash_name,
1816 Function_type* hash_fntype,
1817 const std::string& equal_name,
1818 Function_type* equal_fntype)
1820 Location bloc = Linemap::predeclared_location();
1822 if (gogo->specific_type_functions_are_written())
1824 go_assert(saw_errors());
1828 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1830 gogo->start_block(bloc);
1832 if (this->struct_type() != NULL)
1833 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1835 else if (this->array_type() != NULL)
1836 this->array_type()->write_hash_function(gogo, name, hash_fntype,
1841 Block* b = gogo->finish_block(bloc);
1842 gogo->add_block(b, bloc);
1843 gogo->lower_block(hash_fn, b);
1844 gogo->finish_function(bloc);
1846 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1848 gogo->start_block(bloc);
1850 if (this->struct_type() != NULL)
1851 this->struct_type()->write_equal_function(gogo, name);
1852 else if (this->array_type() != NULL)
1853 this->array_type()->write_equal_function(gogo, name);
1857 b = gogo->finish_block(bloc);
1858 gogo->add_block(b, bloc);
1859 gogo->lower_block(equal_fn, b);
1860 gogo->finish_function(bloc);
1863 // Return a composite literal for the type descriptor for a plain type
1864 // of kind RUNTIME_TYPE_KIND named NAME.
1867 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1868 Named_type* name, const Methods* methods,
1869 bool only_value_methods)
1871 Location bloc = Linemap::predeclared_location();
1873 Type* td_type = Type::make_type_descriptor_type();
1874 const Struct_field_list* fields = td_type->struct_type()->fields();
1876 Expression_list* vals = new Expression_list();
1879 if (!this->has_pointer())
1880 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1881 Struct_field_list::const_iterator p = fields->begin();
1882 go_assert(p->is_field_name("Kind"));
1884 mpz_init_set_ui(iv, runtime_type_kind);
1885 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1888 go_assert(p->is_field_name("align"));
1889 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1890 vals->push_back(Expression::make_type_info(this, type_info));
1893 go_assert(p->is_field_name("fieldAlign"));
1894 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1895 vals->push_back(Expression::make_type_info(this, type_info));
1898 go_assert(p->is_field_name("size"));
1899 type_info = Expression::TYPE_INFO_SIZE;
1900 vals->push_back(Expression::make_type_info(this, type_info));
1903 go_assert(p->is_field_name("hash"));
1906 h = name->hash_for_method(gogo);
1908 h = this->hash_for_method(gogo);
1910 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1913 go_assert(p->is_field_name("hashfn"));
1914 Function_type* hash_fntype = p->type()->function_type();
1917 go_assert(p->is_field_name("equalfn"));
1918 Function_type* equal_fntype = p->type()->function_type();
1920 Named_object* hash_fn;
1921 Named_object* equal_fn;
1922 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1924 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1925 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1928 go_assert(p->is_field_name("string"));
1929 Expression* s = Expression::make_string((name != NULL
1930 ? name->reflection(gogo)
1931 : this->reflection(gogo)),
1933 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1936 go_assert(p->is_field_name("uncommonType"));
1937 if (name == NULL && methods == NULL)
1938 vals->push_back(Expression::make_nil(bloc));
1941 if (methods == NULL)
1942 methods = name->methods();
1943 vals->push_back(this->uncommon_type_constructor(gogo,
1946 only_value_methods));
1950 go_assert(p->is_field_name("ptrToThis"));
1952 vals->push_back(Expression::make_nil(bloc));
1955 Type* pt = Type::make_pointer_type(name);
1956 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1960 go_assert(p == fields->end());
1964 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1967 // Return a composite literal for the uncommon type information for
1968 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1969 // struct. If name is not NULL, it is the name of the type. If
1970 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1971 // is true if only value methods should be included. At least one of
1972 // NAME and METHODS must not be NULL.
1975 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1976 Named_type* name, const Methods* methods,
1977 bool only_value_methods) const
1979 Location bloc = Linemap::predeclared_location();
1981 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1983 Expression_list* vals = new Expression_list();
1986 Struct_field_list::const_iterator p = fields->begin();
1987 go_assert(p->is_field_name("name"));
1990 go_assert(p->is_field_name("pkgPath"));
1994 vals->push_back(Expression::make_nil(bloc));
1995 vals->push_back(Expression::make_nil(bloc));
1999 Named_object* no = name->named_object();
2000 std::string n = Gogo::unpack_hidden_name(no->name());
2001 Expression* s = Expression::make_string(n, bloc);
2002 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2004 if (name->is_builtin())
2005 vals->push_back(Expression::make_nil(bloc));
2008 const Package* package = no->package();
2009 const std::string& pkgpath(package == NULL
2011 : package->pkgpath());
2014 const Named_object* in_function = name->in_function(&index);
2015 if (in_function != NULL)
2018 n.append(Gogo::unpack_hidden_name(in_function->name()));
2022 snprintf(buf, sizeof buf, "%u", index);
2027 s = Expression::make_string(n, bloc);
2028 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2033 go_assert(p->is_field_name("methods"));
2034 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
2035 only_value_methods));
2038 go_assert(p == fields->end());
2040 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
2042 return Expression::make_unary(OPERATOR_AND, r, bloc);
2045 // Sort methods by name.
2051 operator()(const std::pair<std::string, const Method*>& m1,
2052 const std::pair<std::string, const Method*>& m2) const
2053 { return m1.first < m2.first; }
2056 // Return a composite literal for the type method table for this type.
2057 // METHODS_TYPE is the type of the table, and is a slice type.
2058 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
2059 // then only value methods are used.
2062 Type::methods_constructor(Gogo* gogo, Type* methods_type,
2063 const Methods* methods,
2064 bool only_value_methods) const
2066 Location bloc = Linemap::predeclared_location();
2068 std::vector<std::pair<std::string, const Method*> > smethods;
2069 if (methods != NULL)
2071 smethods.reserve(methods->count());
2072 for (Methods::const_iterator p = methods->begin();
2073 p != methods->end();
2076 if (p->second->is_ambiguous())
2078 if (only_value_methods && !p->second->is_value_method())
2080 smethods.push_back(std::make_pair(p->first, p->second));
2084 if (smethods.empty())
2085 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
2087 std::sort(smethods.begin(), smethods.end(), Sort_methods());
2089 Type* method_type = methods_type->array_type()->element_type();
2091 Expression_list* vals = new Expression_list();
2092 vals->reserve(smethods.size());
2093 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
2095 p != smethods.end();
2097 vals->push_back(this->method_constructor(gogo, method_type, p->first,
2098 p->second, only_value_methods));
2100 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
2103 // Return a composite literal for a single method. METHOD_TYPE is the
2104 // type of the entry. METHOD_NAME is the name of the method and M is
2105 // the method information.
2108 Type::method_constructor(Gogo*, Type* method_type,
2109 const std::string& method_name,
2111 bool only_value_methods) const
2113 Location bloc = Linemap::predeclared_location();
2115 const Struct_field_list* fields = method_type->struct_type()->fields();
2117 Expression_list* vals = new Expression_list();
2120 Struct_field_list::const_iterator p = fields->begin();
2121 go_assert(p->is_field_name("name"));
2122 const std::string n = Gogo::unpack_hidden_name(method_name);
2123 Expression* s = Expression::make_string(n, bloc);
2124 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2127 go_assert(p->is_field_name("pkgPath"));
2128 if (!Gogo::is_hidden_name(method_name))
2129 vals->push_back(Expression::make_nil(bloc));
2132 s = Expression::make_string(Gogo::hidden_name_pkgpath(method_name),
2134 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2137 Named_object* no = (m->needs_stub_method()
2139 : m->named_object());
2141 Function_type* mtype;
2142 if (no->is_function())
2143 mtype = no->func_value()->type();
2145 mtype = no->func_declaration_value()->type();
2146 go_assert(mtype->is_method());
2147 Type* nonmethod_type = mtype->copy_without_receiver();
2150 go_assert(p->is_field_name("mtyp"));
2151 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
2154 go_assert(p->is_field_name("typ"));
2155 if (!only_value_methods && m->is_value_method())
2157 // This is a value method on a pointer type. Change the type of
2158 // the method to use a pointer receiver. The implementation
2159 // always uses a pointer receiver anyhow.
2160 Type* rtype = mtype->receiver()->type();
2161 Type* prtype = Type::make_pointer_type(rtype);
2162 Typed_identifier* receiver =
2163 new Typed_identifier(mtype->receiver()->name(), prtype,
2164 mtype->receiver()->location());
2165 mtype = Type::make_function_type(receiver,
2166 (mtype->parameters() == NULL
2168 : mtype->parameters()->copy()),
2169 (mtype->results() == NULL
2171 : mtype->results()->copy()),
2174 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
2177 go_assert(p->is_field_name("tfn"));
2178 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
2181 go_assert(p == fields->end());
2183 return Expression::make_struct_composite_literal(method_type, vals, bloc);
2186 // Return a composite literal for the type descriptor of a plain type.
2187 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
2188 // NULL, it is the name to use as well as the list of methods.
2191 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
2194 return this->type_descriptor_constructor(gogo, runtime_type_kind,
2198 // Return the type reflection string for this type.
2201 Type::reflection(Gogo* gogo) const
2205 // The do_reflection virtual function should set RET to the
2206 // reflection string.
2207 this->do_reflection(gogo, &ret);
2212 // Return a mangled name for the type.
2215 Type::mangled_name(Gogo* gogo) const
2219 // The do_mangled_name virtual function should set RET to the
2220 // mangled name. For a composite type it should append a code for
2221 // the composition and then call do_mangled_name on the components.
2222 this->do_mangled_name(gogo, &ret);
2227 // Return whether the backend size of the type is known.
2230 Type::is_backend_type_size_known(Gogo* gogo)
2232 switch (this->classification_)
2246 case TYPE_INTERFACE:
2251 const Struct_field_list* fields = this->struct_type()->fields();
2252 for (Struct_field_list::const_iterator pf = fields->begin();
2253 pf != fields->end();
2255 if (!pf->type()->is_backend_type_size_known(gogo))
2262 const Array_type* at = this->array_type();
2263 if (at->length() == NULL)
2267 Numeric_constant nc;
2268 if (!at->length()->numeric_constant_value(&nc))
2271 if (!nc.to_int(&ival))
2274 return at->element_type()->is_backend_type_size_known(gogo);
2279 // Begin converting this type to the backend representation.
2280 // This will create a placeholder if necessary.
2281 this->get_backend(gogo);
2282 return this->named_type()->is_named_backend_type_size_known();
2286 Forward_declaration_type* fdt = this->forward_declaration_type();
2287 return fdt->real_type()->is_backend_type_size_known(gogo);
2291 case TYPE_CALL_MULTIPLE_RESULT:
2299 // If the size of the type can be determined, set *PSIZE to the size
2300 // in bytes and return true. Otherwise, return false. This queries
2304 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2306 if (!this->is_backend_type_size_known(gogo))
2308 Btype* bt = this->get_backend_placeholder(gogo);
2309 size_t size = gogo->backend()->type_size(bt);
2310 *psize = static_cast<unsigned int>(size);
2316 // If the alignment of the type can be determined, set *PALIGN to
2317 // the alignment in bytes and return true. Otherwise, return false.
2320 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2322 if (!this->is_backend_type_size_known(gogo))
2324 Btype* bt = this->get_backend_placeholder(gogo);
2325 size_t align = gogo->backend()->type_alignment(bt);
2326 *palign = static_cast<unsigned int>(align);
2327 if (*palign != align)
2332 // Like backend_type_align, but return the alignment when used as a
2336 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2338 if (!this->is_backend_type_size_known(gogo))
2340 Btype* bt = this->get_backend_placeholder(gogo);
2341 size_t a = gogo->backend()->type_field_alignment(bt);
2342 *palign = static_cast<unsigned int>(a);
2348 // Default function to export a type.
2351 Type::do_export(Export*) const
2359 Type::import_type(Import* imp)
2361 if (imp->match_c_string("("))
2362 return Function_type::do_import(imp);
2363 else if (imp->match_c_string("*"))
2364 return Pointer_type::do_import(imp);
2365 else if (imp->match_c_string("struct "))
2366 return Struct_type::do_import(imp);
2367 else if (imp->match_c_string("["))
2368 return Array_type::do_import(imp);
2369 else if (imp->match_c_string("map "))
2370 return Map_type::do_import(imp);
2371 else if (imp->match_c_string("chan "))
2372 return Channel_type::do_import(imp);
2373 else if (imp->match_c_string("interface"))
2374 return Interface_type::do_import(imp);
2377 error_at(imp->location(), "import error: expected type");
2378 return Type::make_error_type();
2382 // A type used to indicate a parsing error. This exists to simplify
2383 // later error detection.
2385 class Error_type : public Type
2394 do_compare_is_identity(Gogo*) const
2398 do_get_backend(Gogo* gogo)
2399 { return gogo->backend()->error_type(); }
2402 do_type_descriptor(Gogo*, Named_type*)
2403 { return Expression::make_error(Linemap::predeclared_location()); }
2406 do_reflection(Gogo*, std::string*) const
2407 { go_assert(saw_errors()); }
2410 do_mangled_name(Gogo*, std::string* ret) const
2411 { ret->push_back('E'); }
2415 Type::make_error_type()
2417 static Error_type singleton_error_type;
2418 return &singleton_error_type;
2423 class Void_type : public Type
2432 do_compare_is_identity(Gogo*) const
2436 do_get_backend(Gogo* gogo)
2437 { return gogo->backend()->void_type(); }
2440 do_type_descriptor(Gogo*, Named_type*)
2441 { go_unreachable(); }
2444 do_reflection(Gogo*, std::string*) const
2448 do_mangled_name(Gogo*, std::string* ret) const
2449 { ret->push_back('v'); }
2453 Type::make_void_type()
2455 static Void_type singleton_void_type;
2456 return &singleton_void_type;
2459 // The boolean type.
2461 class Boolean_type : public Type
2465 : Type(TYPE_BOOLEAN)
2470 do_compare_is_identity(Gogo*) const
2474 do_get_backend(Gogo* gogo)
2475 { return gogo->backend()->bool_type(); }
2478 do_type_descriptor(Gogo*, Named_type* name);
2480 // We should not be asked for the reflection string of a basic type.
2482 do_reflection(Gogo*, std::string* ret) const
2483 { ret->append("bool"); }
2486 do_mangled_name(Gogo*, std::string* ret) const
2487 { ret->push_back('b'); }
2490 // Make the type descriptor.
2493 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2496 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2499 Named_object* no = gogo->lookup_global("bool");
2500 go_assert(no != NULL);
2501 return Type::type_descriptor(gogo, no->type_value());
2506 Type::make_boolean_type()
2508 static Boolean_type boolean_type;
2509 return &boolean_type;
2512 // The named type "bool".
2514 static Named_type* named_bool_type;
2516 // Get the named type "bool".
2519 Type::lookup_bool_type()
2521 return named_bool_type;
2524 // Make the named type "bool".
2527 Type::make_named_bool_type()
2529 Type* bool_type = Type::make_boolean_type();
2530 Named_object* named_object =
2531 Named_object::make_type("bool", NULL, bool_type,
2532 Linemap::predeclared_location());
2533 Named_type* named_type = named_object->type_value();
2534 named_bool_type = named_type;
2538 // Class Integer_type.
2540 Integer_type::Named_integer_types Integer_type::named_integer_types;
2542 // Create a new integer type. Non-abstract integer types always have
2546 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2547 int bits, int runtime_type_kind)
2549 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2551 std::string sname(name);
2552 Named_object* named_object =
2553 Named_object::make_type(sname, NULL, integer_type,
2554 Linemap::predeclared_location());
2555 Named_type* named_type = named_object->type_value();
2556 std::pair<Named_integer_types::iterator, bool> ins =
2557 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2558 go_assert(ins.second);
2562 // Look up an existing integer type.
2565 Integer_type::lookup_integer_type(const char* name)
2567 Named_integer_types::const_iterator p =
2568 Integer_type::named_integer_types.find(name);
2569 go_assert(p != Integer_type::named_integer_types.end());
2573 // Create a new abstract integer type.
2576 Integer_type::create_abstract_integer_type()
2578 static Integer_type* abstract_type;
2579 if (abstract_type == NULL)
2580 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2581 RUNTIME_TYPE_KIND_INT);
2582 return abstract_type;
2585 // Create a new abstract character type.
2588 Integer_type::create_abstract_character_type()
2590 static Integer_type* abstract_type;
2591 if (abstract_type == NULL)
2593 abstract_type = new Integer_type(true, false, 32,
2594 RUNTIME_TYPE_KIND_INT32);
2595 abstract_type->set_is_rune();
2597 return abstract_type;
2600 // Integer type compatibility.
2603 Integer_type::is_identical(const Integer_type* t) const
2605 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2607 return this->is_abstract_ == t->is_abstract_;
2613 Integer_type::do_hash_for_method(Gogo*) const
2615 return ((this->bits_ << 4)
2616 + ((this->is_unsigned_ ? 1 : 0) << 8)
2617 + ((this->is_abstract_ ? 1 : 0) << 9));
2620 // Convert an Integer_type to the backend representation.
2623 Integer_type::do_get_backend(Gogo* gogo)
2625 if (this->is_abstract_)
2627 go_assert(saw_errors());
2628 return gogo->backend()->error_type();
2630 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2633 // The type descriptor for an integer type. Integer types are always
2637 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2639 go_assert(name != NULL || saw_errors());
2640 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2643 // We should not be asked for the reflection string of a basic type.
2646 Integer_type::do_reflection(Gogo*, std::string*) const
2648 go_assert(saw_errors());
2654 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2657 snprintf(buf, sizeof buf, "i%s%s%de",
2658 this->is_abstract_ ? "a" : "",
2659 this->is_unsigned_ ? "u" : "",
2664 // Make an integer type.
2667 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2668 int runtime_type_kind)
2670 return Integer_type::create_integer_type(name, is_unsigned, bits,
2674 // Make an abstract integer type.
2677 Type::make_abstract_integer_type()
2679 return Integer_type::create_abstract_integer_type();
2682 // Make an abstract character type.
2685 Type::make_abstract_character_type()
2687 return Integer_type::create_abstract_character_type();
2690 // Look up an integer type.
2693 Type::lookup_integer_type(const char* name)
2695 return Integer_type::lookup_integer_type(name);
2698 // Class Float_type.
2700 Float_type::Named_float_types Float_type::named_float_types;
2702 // Create a new float type. Non-abstract float types always have
2706 Float_type::create_float_type(const char* name, int bits,
2707 int runtime_type_kind)
2709 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2710 std::string sname(name);
2711 Named_object* named_object =
2712 Named_object::make_type(sname, NULL, float_type,
2713 Linemap::predeclared_location());
2714 Named_type* named_type = named_object->type_value();
2715 std::pair<Named_float_types::iterator, bool> ins =
2716 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2717 go_assert(ins.second);
2721 // Look up an existing float type.
2724 Float_type::lookup_float_type(const char* name)
2726 Named_float_types::const_iterator p =
2727 Float_type::named_float_types.find(name);
2728 go_assert(p != Float_type::named_float_types.end());
2732 // Create a new abstract float type.
2735 Float_type::create_abstract_float_type()
2737 static Float_type* abstract_type;
2738 if (abstract_type == NULL)
2739 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2740 return abstract_type;
2743 // Whether this type is identical with T.
2746 Float_type::is_identical(const Float_type* t) const
2748 if (this->bits_ != t->bits_)
2750 return this->is_abstract_ == t->is_abstract_;
2756 Float_type::do_hash_for_method(Gogo*) const
2758 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2761 // Convert to the backend representation.
2764 Float_type::do_get_backend(Gogo* gogo)
2766 return gogo->backend()->float_type(this->bits_);
2769 // The type descriptor for a float type. Float types are always named.
2772 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2774 go_assert(name != NULL || saw_errors());
2775 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2778 // We should not be asked for the reflection string of a basic type.
2781 Float_type::do_reflection(Gogo*, std::string*) const
2783 go_assert(saw_errors());
2789 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2792 snprintf(buf, sizeof buf, "f%s%de",
2793 this->is_abstract_ ? "a" : "",
2798 // Make a floating point type.
2801 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2803 return Float_type::create_float_type(name, bits, runtime_type_kind);
2806 // Make an abstract float type.
2809 Type::make_abstract_float_type()
2811 return Float_type::create_abstract_float_type();
2814 // Look up a float type.
2817 Type::lookup_float_type(const char* name)
2819 return Float_type::lookup_float_type(name);
2822 // Class Complex_type.
2824 Complex_type::Named_complex_types Complex_type::named_complex_types;
2826 // Create a new complex type. Non-abstract complex types always have
2830 Complex_type::create_complex_type(const char* name, int bits,
2831 int runtime_type_kind)
2833 Complex_type* complex_type = new Complex_type(false, bits,
2835 std::string sname(name);
2836 Named_object* named_object =
2837 Named_object::make_type(sname, NULL, complex_type,
2838 Linemap::predeclared_location());
2839 Named_type* named_type = named_object->type_value();
2840 std::pair<Named_complex_types::iterator, bool> ins =
2841 Complex_type::named_complex_types.insert(std::make_pair(sname,
2843 go_assert(ins.second);
2847 // Look up an existing complex type.
2850 Complex_type::lookup_complex_type(const char* name)
2852 Named_complex_types::const_iterator p =
2853 Complex_type::named_complex_types.find(name);
2854 go_assert(p != Complex_type::named_complex_types.end());
2858 // Create a new abstract complex type.
2861 Complex_type::create_abstract_complex_type()
2863 static Complex_type* abstract_type;
2864 if (abstract_type == NULL)
2865 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2866 return abstract_type;
2869 // Whether this type is identical with T.
2872 Complex_type::is_identical(const Complex_type *t) const
2874 if (this->bits_ != t->bits_)
2876 return this->is_abstract_ == t->is_abstract_;
2882 Complex_type::do_hash_for_method(Gogo*) const
2884 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2887 // Convert to the backend representation.
2890 Complex_type::do_get_backend(Gogo* gogo)
2892 return gogo->backend()->complex_type(this->bits_);
2895 // The type descriptor for a complex type. Complex types are always
2899 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2901 go_assert(name != NULL || saw_errors());
2902 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2905 // We should not be asked for the reflection string of a basic type.
2908 Complex_type::do_reflection(Gogo*, std::string*) const
2910 go_assert(saw_errors());
2916 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2919 snprintf(buf, sizeof buf, "c%s%de",
2920 this->is_abstract_ ? "a" : "",
2925 // Make a complex type.
2928 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2930 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2933 // Make an abstract complex type.
2936 Type::make_abstract_complex_type()
2938 return Complex_type::create_abstract_complex_type();
2941 // Look up a complex type.
2944 Type::lookup_complex_type(const char* name)
2946 return Complex_type::lookup_complex_type(name);
2949 // Class String_type.
2951 // Convert String_type to the backend representation. A string is a
2952 // struct with two fields: a pointer to the characters and a length.
2955 String_type::do_get_backend(Gogo* gogo)
2957 static Btype* backend_string_type;
2958 if (backend_string_type == NULL)
2960 std::vector<Backend::Btyped_identifier> fields(2);
2962 Type* b = gogo->lookup_global("byte")->type_value();
2963 Type* pb = Type::make_pointer_type(b);
2965 // We aren't going to get back to this field to finish the
2966 // backend representation, so force it to be finished now.
2967 if (!gogo->named_types_are_converted())
2969 pb->get_backend_placeholder(gogo);
2970 pb->finish_backend(gogo);
2973 fields[0].name = "__data";
2974 fields[0].btype = pb->get_backend(gogo);
2975 fields[0].location = Linemap::predeclared_location();
2977 Type* int_type = Type::lookup_integer_type("int");
2978 fields[1].name = "__length";
2979 fields[1].btype = int_type->get_backend(gogo);
2980 fields[1].location = fields[0].location;
2982 backend_string_type = gogo->backend()->struct_type(fields);
2984 return backend_string_type;
2987 // Return a tree for the length of STRING.
2990 String_type::length_tree(Gogo*, tree string)
2992 tree string_type = TREE_TYPE(string);
2993 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2994 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2995 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2997 return fold_build3(COMPONENT_REF, integer_type_node, string,
2998 length_field, NULL_TREE);
3001 // Return a tree for a pointer to the bytes of STRING.
3004 String_type::bytes_tree(Gogo*, tree string)
3006 tree string_type = TREE_TYPE(string);
3007 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
3008 tree bytes_field = TYPE_FIELDS(string_type);
3009 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
3011 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
3012 bytes_field, NULL_TREE);
3015 // The type descriptor for the string type.
3018 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3021 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
3024 Named_object* no = gogo->lookup_global("string");
3025 go_assert(no != NULL);
3026 return Type::type_descriptor(gogo, no->type_value());
3030 // We should not be asked for the reflection string of a basic type.
3033 String_type::do_reflection(Gogo*, std::string* ret) const
3035 ret->append("string");
3038 // Mangled name of a string type.
3041 String_type::do_mangled_name(Gogo*, std::string* ret) const
3043 ret->push_back('z');
3046 // Make a string type.
3049 Type::make_string_type()
3051 static String_type string_type;
3052 return &string_type;
3055 // The named type "string".
3057 static Named_type* named_string_type;
3059 // Get the named type "string".
3062 Type::lookup_string_type()
3064 return named_string_type;
3067 // Make the named type string.
3070 Type::make_named_string_type()
3072 Type* string_type = Type::make_string_type();
3073 Named_object* named_object =
3074 Named_object::make_type("string", NULL, string_type,
3075 Linemap::predeclared_location());
3076 Named_type* named_type = named_object->type_value();
3077 named_string_type = named_type;
3081 // The sink type. This is the type of the blank identifier _. Any
3082 // type may be assigned to it.
3084 class Sink_type : public Type
3093 do_compare_is_identity(Gogo*) const
3097 do_get_backend(Gogo*)
3098 { go_unreachable(); }
3101 do_type_descriptor(Gogo*, Named_type*)
3102 { go_unreachable(); }
3105 do_reflection(Gogo*, std::string*) const
3106 { go_unreachable(); }
3109 do_mangled_name(Gogo*, std::string*) const
3110 { go_unreachable(); }
3113 // Make the sink type.
3116 Type::make_sink_type()
3118 static Sink_type sink_type;
3122 // Class Function_type.
3127 Function_type::do_traverse(Traverse* traverse)
3129 if (this->receiver_ != NULL
3130 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
3131 return TRAVERSE_EXIT;
3132 if (this->parameters_ != NULL
3133 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
3134 return TRAVERSE_EXIT;
3135 if (this->results_ != NULL
3136 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
3137 return TRAVERSE_EXIT;
3138 return TRAVERSE_CONTINUE;
3141 // Returns whether T is a valid redeclaration of this type. If this
3142 // returns false, and REASON is not NULL, *REASON may be set to a
3143 // brief explanation of why it returned false.
3146 Function_type::is_valid_redeclaration(const Function_type* t,
3147 std::string* reason) const
3149 if (!this->is_identical(t, false, true, reason))
3152 // A redeclaration of a function is required to use the same names
3153 // for the receiver and parameters.
3154 if (this->receiver() != NULL
3155 && this->receiver()->name() != t->receiver()->name())
3158 *reason = "receiver name changed";
3162 const Typed_identifier_list* parms1 = this->parameters();
3163 const Typed_identifier_list* parms2 = t->parameters();
3166 Typed_identifier_list::const_iterator p1 = parms1->begin();
3167 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3168 p2 != parms2->end();
3171 if (p1->name() != p2->name())
3174 *reason = "parameter name changed";
3178 // This is called at parse time, so we may have unknown
3180 Type* t1 = p1->type()->forwarded();
3181 Type* t2 = p2->type()->forwarded();
3183 && t1->forward_declaration_type() != NULL
3184 && (t2->forward_declaration_type() == NULL
3185 || (t1->forward_declaration_type()->named_object()
3186 != t2->forward_declaration_type()->named_object())))
3191 const Typed_identifier_list* results1 = this->results();
3192 const Typed_identifier_list* results2 = t->results();
3193 if (results1 != NULL)
3195 Typed_identifier_list::const_iterator res1 = results1->begin();
3196 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3197 res2 != results2->end();
3200 if (res1->name() != res2->name())
3203 *reason = "result name changed";
3207 // This is called at parse time, so we may have unknown
3209 Type* t1 = res1->type()->forwarded();
3210 Type* t2 = res2->type()->forwarded();
3212 && t1->forward_declaration_type() != NULL
3213 && (t2->forward_declaration_type() == NULL
3214 || (t1->forward_declaration_type()->named_object()
3215 != t2->forward_declaration_type()->named_object())))
3223 // Check whether T is the same as this type.
3226 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
3227 bool errors_are_identical,
3228 std::string* reason) const
3230 if (!ignore_receiver)
3232 const Typed_identifier* r1 = this->receiver();
3233 const Typed_identifier* r2 = t->receiver();
3234 if ((r1 != NULL) != (r2 != NULL))
3237 *reason = _("different receiver types");
3242 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
3245 if (reason != NULL && !reason->empty())
3246 *reason = "receiver: " + *reason;
3252 const Typed_identifier_list* parms1 = this->parameters();
3253 const Typed_identifier_list* parms2 = t->parameters();
3254 if ((parms1 != NULL) != (parms2 != NULL))
3257 *reason = _("different number of parameters");
3262 Typed_identifier_list::const_iterator p1 = parms1->begin();
3263 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3264 p2 != parms2->end();
3267 if (p1 == parms1->end())
3270 *reason = _("different number of parameters");
3274 if (!Type::are_identical(p1->type(), p2->type(),
3275 errors_are_identical, NULL))
3278 *reason = _("different parameter types");
3282 if (p1 != parms1->end())
3285 *reason = _("different number of parameters");
3290 if (this->is_varargs() != t->is_varargs())
3293 *reason = _("different varargs");
3297 const Typed_identifier_list* results1 = this->results();
3298 const Typed_identifier_list* results2 = t->results();
3299 if ((results1 != NULL) != (results2 != NULL))
3302 *reason = _("different number of results");
3305 if (results1 != NULL)
3307 Typed_identifier_list::const_iterator res1 = results1->begin();
3308 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3309 res2 != results2->end();
3312 if (res1 == results1->end())
3315 *reason = _("different number of results");
3319 if (!Type::are_identical(res1->type(), res2->type(),
3320 errors_are_identical, NULL))
3323 *reason = _("different result types");
3327 if (res1 != results1->end())
3330 *reason = _("different number of results");
3341 Function_type::do_hash_for_method(Gogo* gogo) const
3343 unsigned int ret = 0;
3344 // We ignore the receiver type for hash codes, because we need to
3345 // get the same hash code for a method in an interface and a method
3346 // declared for a type. The former will not have a receiver.
3347 if (this->parameters_ != NULL)
3350 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3351 p != this->parameters_->end();
3353 ret += p->type()->hash_for_method(gogo) << shift;
3355 if (this->results_ != NULL)
3358 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3359 p != this->results_->end();
3361 ret += p->type()->hash_for_method(gogo) << shift;
3363 if (this->is_varargs_)
3369 // Get the backend representation for a function type.
3372 Function_type::do_get_backend(Gogo* gogo)
3374 Backend::Btyped_identifier breceiver;
3375 if (this->receiver_ != NULL)
3377 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3379 // We always pass the address of the receiver parameter, in
3380 // order to make interface calls work with unknown types.
3381 Type* rtype = this->receiver_->type();
3382 if (rtype->points_to() == NULL)
3383 rtype = Type::make_pointer_type(rtype);
3384 breceiver.btype = rtype->get_backend(gogo);
3385 breceiver.location = this->receiver_->location();
3388 std::vector<Backend::Btyped_identifier> bparameters;
3389 if (this->parameters_ != NULL)
3391 bparameters.resize(this->parameters_->size());
3393 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3394 p != this->parameters_->end();
3397 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3398 bparameters[i].btype = p->type()->get_backend(gogo);
3399 bparameters[i].location = p->location();
3401 go_assert(i == bparameters.size());
3404 std::vector<Backend::Btyped_identifier> bresults;
3405 if (this->results_ != NULL)
3407 bresults.resize(this->results_->size());
3409 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3410 p != this->results_->end();
3413 bresults[i].name = Gogo::unpack_hidden_name(p->name());
3414 bresults[i].btype = p->type()->get_backend(gogo);
3415 bresults[i].location = p->location();
3417 go_assert(i == bresults.size());
3420 return gogo->backend()->function_type(breceiver, bparameters, bresults,
3424 // The type of a function type descriptor.
3427 Function_type::make_function_type_descriptor_type()
3432 Type* tdt = Type::make_type_descriptor_type();
3433 Type* ptdt = Type::make_type_descriptor_ptr_type();
3435 Type* bool_type = Type::lookup_bool_type();
3437 Type* slice_type = Type::make_array_type(ptdt, NULL);
3439 Struct_type* s = Type::make_builtin_struct_type(4,
3441 "dotdotdot", bool_type,
3445 ret = Type::make_builtin_named_type("FuncType", s);
3451 // The type descriptor for a function type.
3454 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3456 Location bloc = Linemap::predeclared_location();
3458 Type* ftdt = Function_type::make_function_type_descriptor_type();
3460 const Struct_field_list* fields = ftdt->struct_type()->fields();
3462 Expression_list* vals = new Expression_list();
3465 Struct_field_list::const_iterator p = fields->begin();
3466 go_assert(p->is_field_name("commonType"));
3467 vals->push_back(this->type_descriptor_constructor(gogo,
3468 RUNTIME_TYPE_KIND_FUNC,
3472 go_assert(p->is_field_name("dotdotdot"));
3473 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3476 go_assert(p->is_field_name("in"));
3477 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3478 this->parameters()));
3481 go_assert(p->is_field_name("out"));
3482 vals->push_back(this->type_descriptor_params(p->type(), NULL,
3486 go_assert(p == fields->end());
3488 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3491 // Return a composite literal for the parameters or results of a type
3495 Function_type::type_descriptor_params(Type* params_type,
3496 const Typed_identifier* receiver,
3497 const Typed_identifier_list* params)
3499 Location bloc = Linemap::predeclared_location();
3501 if (receiver == NULL && params == NULL)
3502 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3504 Expression_list* vals = new Expression_list();
3505 vals->reserve((params == NULL ? 0 : params->size())
3506 + (receiver != NULL ? 1 : 0));
3508 if (receiver != NULL)
3509 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3513 for (Typed_identifier_list::const_iterator p = params->begin();
3516 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3519 return Expression::make_slice_composite_literal(params_type, vals, bloc);
3522 // The reflection string.
3525 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3527 // FIXME: Turn this off until we straighten out the type of the
3528 // struct field used in a go statement which calls a method.