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 (!p->type()->is_comparable())
594 *reason = _("invalid comparison of non-comparable struct");
599 else if (t1->array_type() != NULL)
601 if (t1->array_type()->length()->is_nil_expression()
602 || !t1->array_type()->element_type()->is_comparable())
605 *reason = _("invalid comparison of non-comparable array");
614 // Return true if a value with type RHS may be assigned to a variable
615 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
616 // hidden fields are modified. If REASON is not NULL, set *REASON to
617 // the reason the types are not assignable.
620 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
621 bool check_hidden_fields,
624 // Do some checks first. Make sure the types are defined.
625 if (rhs != NULL && !rhs->is_undefined())
627 if (rhs->is_void_type())
630 *reason = "non-value used as value";
633 if (rhs->is_call_multiple_result_type())
636 reason->assign(_("multiple value function call in "
637 "single value context"));
642 if (lhs != NULL && !lhs->is_undefined())
644 // Any value may be assigned to the blank identifier.
645 if (lhs->is_sink_type())
648 // All fields of a struct must be exported, or the assignment
649 // must be in the same package.
650 if (check_hidden_fields && rhs != NULL && !rhs->is_undefined())
652 if (lhs->has_hidden_fields(NULL, reason)
653 || rhs->has_hidden_fields(NULL, reason))
658 // Identical types are assignable.
659 if (Type::are_identical(lhs, rhs, true, reason))
662 // The types are assignable if they have identical underlying types
663 // and either LHS or RHS is not a named type.
664 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
665 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
666 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
669 // The types are assignable if LHS is an interface type and RHS
670 // implements the required methods.
671 const Interface_type* lhs_interface_type = lhs->interface_type();
672 if (lhs_interface_type != NULL)
674 if (lhs_interface_type->implements_interface(rhs, reason))
676 const Interface_type* rhs_interface_type = rhs->interface_type();
677 if (rhs_interface_type != NULL
678 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
683 // The type are assignable if RHS is a bidirectional channel type,
684 // LHS is a channel type, they have identical element types, and
685 // either LHS or RHS is not a named type.
686 if (lhs->channel_type() != NULL
687 && rhs->channel_type() != NULL
688 && rhs->channel_type()->may_send()
689 && rhs->channel_type()->may_receive()
690 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
691 && Type::are_identical(lhs->channel_type()->element_type(),
692 rhs->channel_type()->element_type(),
697 // The nil type may be assigned to a pointer, function, slice, map,
698 // channel, or interface type.
699 if (rhs->is_nil_type()
700 && (lhs->points_to() != NULL
701 || lhs->function_type() != NULL
702 || lhs->is_slice_type()
703 || lhs->map_type() != NULL
704 || lhs->channel_type() != NULL
705 || lhs->interface_type() != NULL))
708 // An untyped numeric constant may be assigned to a numeric type if
709 // it is representable in that type.
710 if ((rhs->is_abstract()
711 && (rhs->integer_type() != NULL
712 || rhs->float_type() != NULL
713 || rhs->complex_type() != NULL))
714 && (lhs->integer_type() != NULL
715 || lhs->float_type() != NULL
716 || lhs->complex_type() != NULL))
719 // Give some better error messages.
720 if (reason != NULL && reason->empty())
722 if (rhs->interface_type() != NULL)
723 reason->assign(_("need explicit conversion"));
724 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
726 size_t len = (lhs->named_type()->name().length()
727 + rhs->named_type()->name().length()
729 char* buf = new char[len];
730 snprintf(buf, len, _("cannot use type %s as type %s"),
731 rhs->named_type()->message_name().c_str(),
732 lhs->named_type()->message_name().c_str());
741 // Return true if a value with type RHS may be assigned to a variable
742 // with type LHS. If REASON is not NULL, set *REASON to the reason
743 // the types are not assignable.
746 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
748 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
751 // Like are_assignable but don't check for hidden fields.
754 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
757 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
760 // Return true if a value with type RHS may be converted to type LHS.
761 // If REASON is not NULL, set *REASON to the reason the types are not
765 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
767 // The types are convertible if they are assignable.
768 if (Type::are_assignable(lhs, rhs, reason))
771 // The types are convertible if they have identical underlying
773 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
774 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
777 // The types are convertible if they are both unnamed pointer types
778 // and their pointer base types have identical underlying types.
779 if (lhs->named_type() == NULL
780 && rhs->named_type() == NULL
781 && lhs->points_to() != NULL
782 && rhs->points_to() != NULL
783 && (lhs->points_to()->named_type() != NULL
784 || rhs->points_to()->named_type() != NULL)
785 && Type::are_identical(lhs->points_to()->base(),
786 rhs->points_to()->base(),
791 // Integer and floating point types are convertible to each other.
792 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
793 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
796 // Complex types are convertible to each other.
797 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
800 // An integer, or []byte, or []rune, may be converted to a string.
801 if (lhs->is_string_type())
803 if (rhs->integer_type() != NULL)
805 if (rhs->is_slice_type())
807 const Type* e = rhs->array_type()->element_type()->forwarded();
808 if (e->integer_type() != NULL
809 && (e->integer_type()->is_byte()
810 || e->integer_type()->is_rune()))
815 // A string may be converted to []byte or []rune.
816 if (rhs->is_string_type() && lhs->is_slice_type())
818 const Type* e = lhs->array_type()->element_type()->forwarded();
819 if (e->integer_type() != NULL
820 && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
824 // An unsafe.Pointer type may be converted to any pointer type or to
825 // uintptr, and vice-versa.
826 if (lhs->is_unsafe_pointer_type()
827 && (rhs->points_to() != NULL
828 || (rhs->integer_type() != NULL
829 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
831 if (rhs->is_unsafe_pointer_type()
832 && (lhs->points_to() != NULL
833 || (lhs->integer_type() != NULL
834 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
837 // Give a better error message.
841 *reason = "invalid type conversion";
844 std::string s = "invalid type conversion (";
854 // Return whether this type has any hidden fields. This is only a
855 // possibility for a few types.
858 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
860 switch (this->forwarded()->classification_)
863 return this->named_type()->named_type_has_hidden_fields(reason);
865 return this->struct_type()->struct_has_hidden_fields(within, reason);
867 return this->array_type()->array_has_hidden_fields(within, reason);
873 // Return a hash code for the type to be used for method lookup.
876 Type::hash_for_method(Gogo* gogo) const
878 unsigned int ret = 0;
879 if (this->classification_ != TYPE_FORWARD)
880 ret += this->classification_;
881 return ret + this->do_hash_for_method(gogo);
884 // Default implementation of do_hash_for_method. This is appropriate
885 // for types with no subfields.
888 Type::do_hash_for_method(Gogo*) const
893 // Return a hash code for a string, given a starting hash.
896 Type::hash_string(const std::string& s, unsigned int h)
898 const char* p = s.data();
899 size_t len = s.length();
900 for (; len > 0; --len)
908 // A hash table mapping unnamed types to the backend representation of
911 Type::Type_btypes Type::type_btypes;
913 // Return a tree representing this type.
916 Type::get_backend(Gogo* gogo)
918 if (this->btype_ != NULL)
920 if (this->btype_is_placeholder_ && gogo->named_types_are_converted())
921 this->finish_backend(gogo);
925 if (this->forward_declaration_type() != NULL
926 || this->named_type() != NULL)
927 return this->get_btype_without_hash(gogo);
929 if (this->is_error_type())
930 return gogo->backend()->error_type();
932 // To avoid confusing the backend, translate all identical Go types
933 // to the same backend representation. We use a hash table to do
934 // that. There is no need to use the hash table for named types, as
935 // named types are only identical to themselves.
937 std::pair<Type*, Btype*> val(this, NULL);
938 std::pair<Type_btypes::iterator, bool> ins =
939 Type::type_btypes.insert(val);
940 if (!ins.second && ins.first->second != NULL)
942 if (gogo != NULL && gogo->named_types_are_converted())
943 this->btype_ = ins.first->second;
944 return ins.first->second;
947 Btype* bt = this->get_btype_without_hash(gogo);
949 if (ins.first->second == NULL)
950 ins.first->second = bt;
953 // We have already created a backend representation for this
954 // type. This can happen when an unnamed type is defined using
955 // a named type which in turns uses an identical unnamed type.
956 // Use the tree we created earlier and ignore the one we just
958 bt = ins.first->second;
959 if (gogo == NULL || !gogo->named_types_are_converted())
967 // Return the backend representation for a type without looking in the
968 // hash table for identical types. This is used for named types,
969 // since a named type is never identical to any other type.
972 Type::get_btype_without_hash(Gogo* gogo)
974 if (this->btype_ == NULL)
976 Btype* bt = this->do_get_backend(gogo);
978 // For a recursive function or pointer type, we will temporarily
979 // return a circular pointer type during the recursion. We
980 // don't want to record that for a forwarding type, as it may
982 if (this->forward_declaration_type() != NULL
983 && gogo->backend()->is_circular_pointer_type(bt))
986 if (gogo == NULL || !gogo->named_types_are_converted())
994 // Get the backend representation of a type without forcing the
995 // creation of the backend representation of all supporting types.
996 // This will return a backend type that has the correct size but may
997 // be incomplete. E.g., a pointer will just be a placeholder pointer,
998 // and will not contain the final representation of the type to which
999 // it points. This is used while converting all named types to the
1000 // backend representation, to avoid problems with indirect references
1001 // to types which are not yet complete. When this is called, the
1002 // sizes of all direct references (e.g., a struct field) should be
1003 // known, but the sizes of indirect references (e.g., the type to
1004 // which a pointer points) may not.
1007 Type::get_backend_placeholder(Gogo* gogo)
1009 if (gogo->named_types_are_converted())
1010 return this->get_backend(gogo);
1011 if (this->btype_ != NULL)
1012 return this->btype_;
1015 switch (this->classification_)
1025 // These are simple types that can just be created directly.
1026 return this->get_backend(gogo);
1030 Location loc = this->function_type()->location();
1031 bt = gogo->backend()->placeholder_pointer_type("", loc, true);
1037 Location loc = Linemap::unknown_location();
1038 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1043 // We don't have to make the struct itself be a placeholder. We
1044 // are promised that we know the sizes of the struct fields.
1045 // But we may have to use a placeholder for any particular
1048 std::vector<Backend::Btyped_identifier> bfields;
1049 get_backend_struct_fields(gogo, this->struct_type()->fields(),
1051 bt = gogo->backend()->struct_type(bfields);
1056 if (this->is_slice_type())
1058 std::vector<Backend::Btyped_identifier> bfields;
1059 get_backend_slice_fields(gogo, this->array_type(), true, &bfields);
1060 bt = gogo->backend()->struct_type(bfields);
1064 Btype* element = this->array_type()->get_backend_element(gogo, true);
1065 Bexpression* len = this->array_type()->get_backend_length(gogo);
1066 bt = gogo->backend()->array_type(element, len);
1072 // All maps and channels have the same backend representation.
1073 return this->get_backend(gogo);
1075 case TYPE_INTERFACE:
1076 if (this->interface_type()->is_empty())
1077 return Interface_type::get_backend_empty_interface_type(gogo);
1080 std::vector<Backend::Btyped_identifier> bfields;
1081 get_backend_interface_fields(gogo, this->interface_type(), true,
1083 bt = gogo->backend()->struct_type(bfields);
1089 // Named types keep track of their own dependencies and manage
1090 // their own placeholders.
1091 return this->get_backend(gogo);
1094 case TYPE_CALL_MULTIPLE_RESULT:
1100 this->btype_is_placeholder_ = true;
1104 // Complete the backend representation. This is called for a type
1105 // using a placeholder type.
1108 Type::finish_backend(Gogo* gogo)
1110 go_assert(this->btype_ != NULL);
1111 if (!this->btype_is_placeholder_)
1114 switch (this->classification_)
1128 Btype* bt = this->do_get_backend(gogo);
1129 if (!gogo->backend()->set_placeholder_function_type(this->btype_, bt))
1130 go_assert(saw_errors());
1136 Btype* bt = this->do_get_backend(gogo);
1137 if (!gogo->backend()->set_placeholder_pointer_type(this->btype_, bt))
1138 go_assert(saw_errors());
1143 // The struct type itself is done, but we have to make sure that
1144 // all the field types are converted.
1145 this->struct_type()->finish_backend_fields(gogo);
1149 // The array type itself is done, but make sure the element type
1151 this->array_type()->finish_backend_element(gogo);
1158 case TYPE_INTERFACE:
1159 // The interface type itself is done, but make sure the method
1160 // types are converted.
1161 this->interface_type()->finish_backend_methods(gogo);
1169 case TYPE_CALL_MULTIPLE_RESULT:
1174 this->btype_is_placeholder_ = false;
1177 // Return a pointer to the type descriptor for this type.
1180 Type::type_descriptor_pointer(Gogo* gogo, Location location)
1182 Type* t = this->forwarded();
1183 if (t->named_type() != NULL && t->named_type()->is_alias())
1184 t = t->named_type()->real_type();
1185 if (t->type_descriptor_var_ == NULL)
1187 t->make_type_descriptor_var(gogo);
1188 go_assert(t->type_descriptor_var_ != NULL);
1190 tree var_tree = var_to_tree(t->type_descriptor_var_);
1191 if (var_tree == error_mark_node)
1192 return error_mark_node;
1193 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
1196 // A mapping from unnamed types to type descriptor variables.
1198 Type::Type_descriptor_vars Type::type_descriptor_vars;
1200 // Build the type descriptor for this type.
1203 Type::make_type_descriptor_var(Gogo* gogo)
1205 go_assert(this->type_descriptor_var_ == NULL);
1207 Named_type* nt = this->named_type();
1209 // We can have multiple instances of unnamed types, but we only want
1210 // to emit the type descriptor once. We use a hash table. This is
1211 // not necessary for named types, as they are unique, and we store
1212 // the type descriptor in the type itself.
1213 Bvariable** phash = NULL;
1216 Bvariable* bvnull = NULL;
1217 std::pair<Type_descriptor_vars::iterator, bool> ins =
1218 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1221 // We've already build a type descriptor for this type.
1222 this->type_descriptor_var_ = ins.first->second;
1225 phash = &ins.first->second;
1228 std::string var_name = this->type_descriptor_var_name(gogo, nt);
1230 // Build the contents of the type descriptor.
1231 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1233 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1235 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1237 const Package* dummy;
1238 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1240 this->type_descriptor_var_ =
1241 gogo->backend()->immutable_struct_reference(var_name,
1245 *phash = this->type_descriptor_var_;
1249 // See if this type descriptor can appear in multiple packages.
1250 bool is_common = false;
1253 // We create the descriptor for a builtin type whenever we need
1255 is_common = nt->is_builtin();
1259 // This is an unnamed type. The descriptor could be defined in
1260 // any package where it is needed, and the linker will pick one
1261 // descriptor to keep.
1265 // We are going to build the type descriptor in this package. We
1266 // must create the variable before we convert the initializer to the
1267 // backend representation, because the initializer may refer to the
1268 // type descriptor of this type. By setting type_descriptor_var_ we
1269 // ensure that type_descriptor_pointer will work if called while
1270 // converting INITIALIZER.
1272 this->type_descriptor_var_ =
1273 gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
1276 *phash = this->type_descriptor_var_;
1278 Translate_context context(gogo, NULL, NULL, NULL);
1279 context.set_is_const();
1280 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
1282 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1283 var_name, is_common,
1284 initializer_btype, loc,
1288 // Return the name of the type descriptor variable. If NT is not
1289 // NULL, use it to get the name. Otherwise this is an unnamed type.
1292 Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
1295 return "__go_td_" + this->mangled_name(gogo);
1297 Named_object* no = nt->named_object();
1298 const Named_object* in_function = nt->in_function();
1299 std::string ret = "__go_tdn_";
1300 if (nt->is_builtin())
1301 go_assert(in_function == NULL);
1304 const std::string& unique_prefix(no->package() == NULL
1305 ? gogo->unique_prefix()
1306 : no->package()->unique_prefix());
1307 const std::string& package_name(no->package() == NULL
1308 ? gogo->package_name()
1309 : no->package()->name());
1310 ret.append(unique_prefix);
1312 ret.append(package_name);
1314 if (in_function != NULL)
1316 ret.append(Gogo::unpack_hidden_name(in_function->name()));
1320 ret.append(no->name());
1324 // Return true if this type descriptor is defined in a different
1325 // package. If this returns true it sets *PACKAGE to the package.
1328 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1329 const Package** package)
1333 if (nt->named_object()->package() != NULL)
1335 // This is a named type defined in a different package. The
1336 // type descriptor should be defined in that package.
1337 *package = nt->named_object()->package();
1343 if (this->points_to() != NULL
1344 && this->points_to()->named_type() != NULL
1345 && this->points_to()->named_type()->named_object()->package() != NULL)
1347 // This is an unnamed pointer to a named type defined in a
1348 // different package. The descriptor should be defined in
1350 *package = this->points_to()->named_type()->named_object()->package();
1357 // Return a composite literal for a type descriptor.
1360 Type::type_descriptor(Gogo* gogo, Type* type)
1362 return type->do_type_descriptor(gogo, NULL);
1365 // Return a composite literal for a type descriptor with a name.
1368 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1370 go_assert(name != NULL && type->named_type() != name);
1371 return type->do_type_descriptor(gogo, name);
1374 // Make a builtin struct type from a list of fields. The fields are
1375 // pairs of a name and a type.
1378 Type::make_builtin_struct_type(int nfields, ...)
1381 va_start(ap, nfields);
1383 Location bloc = Linemap::predeclared_location();
1384 Struct_field_list* sfl = new Struct_field_list();
1385 for (int i = 0; i < nfields; i++)
1387 const char* field_name = va_arg(ap, const char *);
1388 Type* type = va_arg(ap, Type*);
1389 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1394 return Type::make_struct_type(sfl, bloc);
1397 // A list of builtin named types.
1399 std::vector<Named_type*> Type::named_builtin_types;
1401 // Make a builtin named type.
1404 Type::make_builtin_named_type(const char* name, Type* type)
1406 Location bloc = Linemap::predeclared_location();
1407 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1408 Named_type* ret = no->type_value();
1409 Type::named_builtin_types.push_back(ret);
1413 // Convert the named builtin types.
1416 Type::convert_builtin_named_types(Gogo* gogo)
1418 for (std::vector<Named_type*>::const_iterator p =
1419 Type::named_builtin_types.begin();
1420 p != Type::named_builtin_types.end();
1423 bool r = (*p)->verify();
1425 (*p)->convert(gogo);
1429 // Return the type of a type descriptor. We should really tie this to
1430 // runtime.Type rather than copying it. This must match commonType in
1431 // libgo/go/runtime/type.go.
1434 Type::make_type_descriptor_type()
1439 Location bloc = Linemap::predeclared_location();
1441 Type* uint8_type = Type::lookup_integer_type("uint8");
1442 Type* uint32_type = Type::lookup_integer_type("uint32");
1443 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1444 Type* string_type = Type::lookup_string_type();
1445 Type* pointer_string_type = Type::make_pointer_type(string_type);
1447 // This is an unnamed version of unsafe.Pointer. Perhaps we
1448 // should use the named version instead, although that would
1449 // require us to create the unsafe package if it has not been
1450 // imported. It probably doesn't matter.
1451 Type* void_type = Type::make_void_type();
1452 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1454 // Forward declaration for the type descriptor type.
1455 Named_object* named_type_descriptor_type =
1456 Named_object::make_type_declaration("commonType", NULL, bloc);
1457 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1458 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1460 // The type of a method on a concrete type.
1461 Struct_type* method_type =
1462 Type::make_builtin_struct_type(5,
1463 "name", pointer_string_type,
1464 "pkgPath", pointer_string_type,
1465 "mtyp", pointer_type_descriptor_type,
1466 "typ", pointer_type_descriptor_type,
1467 "tfn", unsafe_pointer_type);
1468 Named_type* named_method_type =
1469 Type::make_builtin_named_type("method", method_type);
1471 // Information for types with a name or methods.
1472 Type* slice_named_method_type =
1473 Type::make_array_type(named_method_type, NULL);
1474 Struct_type* uncommon_type =
1475 Type::make_builtin_struct_type(3,
1476 "name", pointer_string_type,
1477 "pkgPath", pointer_string_type,
1478 "methods", slice_named_method_type);
1479 Named_type* named_uncommon_type =
1480 Type::make_builtin_named_type("uncommonType", uncommon_type);
1482 Type* pointer_uncommon_type =
1483 Type::make_pointer_type(named_uncommon_type);
1485 // The type descriptor type.
1487 Typed_identifier_list* params = new Typed_identifier_list();
1488 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1489 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1491 Typed_identifier_list* results = new Typed_identifier_list();
1492 results->push_back(Typed_identifier("", uintptr_type, bloc));
1494 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1496 params = new Typed_identifier_list();
1497 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1498 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1499 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1501 results = new Typed_identifier_list();
1502 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1504 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1507 Struct_type* type_descriptor_type =
1508 Type::make_builtin_struct_type(10,
1510 "align", uint8_type,
1511 "fieldAlign", uint8_type,
1512 "size", uintptr_type,
1513 "hash", uint32_type,
1514 "hashfn", hashfn_type,
1515 "equalfn", equalfn_type,
1516 "string", pointer_string_type,
1517 "", pointer_uncommon_type,
1519 pointer_type_descriptor_type);
1521 Named_type* named = Type::make_builtin_named_type("commonType",
1522 type_descriptor_type);
1524 named_type_descriptor_type->set_type_value(named);
1532 // Make the type of a pointer to a type descriptor as represented in
1536 Type::make_type_descriptor_ptr_type()
1540 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1544 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1545 // hash code for this type and which compare whether two values of
1546 // this type are equal. If NAME is not NULL it is the name of this
1547 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1548 // functions, for convenience; they may be NULL.
1551 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1552 Function_type* equal_fntype, Named_object** hash_fn,
1553 Named_object** equal_fn)
1555 if (hash_fntype == NULL || equal_fntype == NULL)
1557 Location bloc = Linemap::predeclared_location();
1559 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1560 Type* void_type = Type::make_void_type();
1561 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1563 if (hash_fntype == NULL)
1565 Typed_identifier_list* params = new Typed_identifier_list();
1566 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1568 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1570 Typed_identifier_list* results = new Typed_identifier_list();
1571 results->push_back(Typed_identifier("", uintptr_type, bloc));
1573 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1575 if (equal_fntype == NULL)
1577 Typed_identifier_list* params = new Typed_identifier_list();
1578 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1580 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1582 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1584 Typed_identifier_list* results = new Typed_identifier_list();
1585 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1588 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1592 const char* hash_fnname;
1593 const char* equal_fnname;
1594 if (this->compare_is_identity(gogo))
1596 hash_fnname = "__go_type_hash_identity";
1597 equal_fnname = "__go_type_equal_identity";
1599 else if (!this->is_comparable())
1601 hash_fnname = "__go_type_hash_error";
1602 equal_fnname = "__go_type_equal_error";
1606 switch (this->base()->classification())
1608 case Type::TYPE_ERROR:
1609 case Type::TYPE_VOID:
1610 case Type::TYPE_NIL:
1611 case Type::TYPE_FUNCTION:
1612 case Type::TYPE_MAP:
1613 // For these types is_comparable should have returned false.
1616 case Type::TYPE_BOOLEAN:
1617 case Type::TYPE_INTEGER:
1618 case Type::TYPE_POINTER:
1619 case Type::TYPE_CHANNEL:
1620 // For these types compare_is_identity should have returned true.
1623 case Type::TYPE_FLOAT:
1624 hash_fnname = "__go_type_hash_float";
1625 equal_fnname = "__go_type_equal_float";
1628 case Type::TYPE_COMPLEX:
1629 hash_fnname = "__go_type_hash_complex";
1630 equal_fnname = "__go_type_equal_complex";
1633 case Type::TYPE_STRING:
1634 hash_fnname = "__go_type_hash_string";
1635 equal_fnname = "__go_type_equal_string";
1638 case Type::TYPE_STRUCT:
1640 // This is a struct which can not be compared using a
1641 // simple identity function. We need to build a function
1643 this->specific_type_functions(gogo, name, hash_fntype,
1644 equal_fntype, hash_fn, equal_fn);
1648 case Type::TYPE_ARRAY:
1649 if (this->is_slice_type())
1651 // Type::is_compatible_for_comparison should have
1657 // This is an array which can not be compared using a
1658 // simple identity function. We need to build a
1659 // function for comparison.
1660 this->specific_type_functions(gogo, name, hash_fntype,
1661 equal_fntype, hash_fn, equal_fn);
1666 case Type::TYPE_INTERFACE:
1667 if (this->interface_type()->is_empty())
1669 hash_fnname = "__go_type_hash_empty_interface";
1670 equal_fnname = "__go_type_equal_empty_interface";
1674 hash_fnname = "__go_type_hash_interface";
1675 equal_fnname = "__go_type_equal_interface";
1679 case Type::TYPE_NAMED:
1680 case Type::TYPE_FORWARD:
1689 Location bloc = Linemap::predeclared_location();
1690 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1692 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1693 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1694 equal_fntype, bloc);
1695 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1698 // A hash table mapping types to the specific hash functions.
1700 Type::Type_functions Type::type_functions_table;
1702 // Handle a type function which is specific to a type: a struct or
1703 // array which can not use an identity comparison.
1706 Type::specific_type_functions(Gogo* gogo, Named_type* name,
1707 Function_type* hash_fntype,
1708 Function_type* equal_fntype,
1709 Named_object** hash_fn,
1710 Named_object** equal_fn)
1712 Hash_equal_fn fnull(NULL, NULL);
1713 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1714 std::pair<Type_functions::iterator, bool> ins =
1715 Type::type_functions_table.insert(val);
1718 // We already have functions for this type
1719 *hash_fn = ins.first->second.first;
1720 *equal_fn = ins.first->second.second;
1724 std::string base_name;
1727 // Mangled names can have '.' if they happen to refer to named
1728 // types in some way. That's fine if this is simply a named
1729 // type, but otherwise it will confuse the code that builds
1730 // function identifiers. Remove '.' when necessary.
1731 base_name = this->mangled_name(gogo);
1733 while ((i = base_name.find('.')) != std::string::npos)
1735 base_name = gogo->pack_hidden_name(base_name, false);
1739 // This name is already hidden or not as appropriate.
1740 base_name = name->name();
1741 const Named_object* in_function = name->in_function();
1742 if (in_function != NULL)
1743 base_name += '$' + in_function->name();
1745 std::string hash_name = base_name + "$hash";
1746 std::string equal_name = base_name + "$equal";
1748 Location bloc = Linemap::predeclared_location();
1750 const Package* package = NULL;
1751 bool is_defined_elsewhere =
1752 this->type_descriptor_defined_elsewhere(name, &package);
1753 if (is_defined_elsewhere)
1755 *hash_fn = Named_object::make_function_declaration(hash_name, package,
1757 *equal_fn = Named_object::make_function_declaration(equal_name, package,
1758 equal_fntype, bloc);
1762 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1763 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1767 ins.first->second.first = *hash_fn;
1768 ins.first->second.second = *equal_fn;
1770 if (!is_defined_elsewhere)
1772 if (gogo->in_global_scope())
1773 this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1774 equal_name, equal_fntype);
1776 gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1777 equal_name, equal_fntype);
1781 // Write the hash and equality functions for a type which needs to be
1782 // written specially.
1785 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1786 const std::string& hash_name,
1787 Function_type* hash_fntype,
1788 const std::string& equal_name,
1789 Function_type* equal_fntype)
1791 Location bloc = Linemap::predeclared_location();
1793 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1795 gogo->start_block(bloc);
1797 if (this->struct_type() != NULL)
1798 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1800 else if (this->array_type() != NULL)
1801 this->array_type()->write_hash_function(gogo, name, hash_fntype,
1806 Block* b = gogo->finish_block(bloc);
1807 gogo->add_block(b, bloc);
1808 gogo->lower_block(hash_fn, b);
1809 gogo->finish_function(bloc);
1811 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1813 gogo->start_block(bloc);
1815 if (this->struct_type() != NULL)
1816 this->struct_type()->write_equal_function(gogo, name);
1817 else if (this->array_type() != NULL)
1818 this->array_type()->write_equal_function(gogo, name);
1822 b = gogo->finish_block(bloc);
1823 gogo->add_block(b, bloc);
1824 gogo->lower_block(equal_fn, b);
1825 gogo->finish_function(bloc);
1828 // Return a composite literal for the type descriptor for a plain type
1829 // of kind RUNTIME_TYPE_KIND named NAME.
1832 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1833 Named_type* name, const Methods* methods,
1834 bool only_value_methods)
1836 Location bloc = Linemap::predeclared_location();
1838 Type* td_type = Type::make_type_descriptor_type();
1839 const Struct_field_list* fields = td_type->struct_type()->fields();
1841 Expression_list* vals = new Expression_list();
1844 if (!this->has_pointer())
1845 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1846 Struct_field_list::const_iterator p = fields->begin();
1847 go_assert(p->is_field_name("Kind"));
1849 mpz_init_set_ui(iv, runtime_type_kind);
1850 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1853 go_assert(p->is_field_name("align"));
1854 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1855 vals->push_back(Expression::make_type_info(this, type_info));
1858 go_assert(p->is_field_name("fieldAlign"));
1859 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1860 vals->push_back(Expression::make_type_info(this, type_info));
1863 go_assert(p->is_field_name("size"));
1864 type_info = Expression::TYPE_INFO_SIZE;
1865 vals->push_back(Expression::make_type_info(this, type_info));
1868 go_assert(p->is_field_name("hash"));
1871 h = name->hash_for_method(gogo);
1873 h = this->hash_for_method(gogo);
1875 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1878 go_assert(p->is_field_name("hashfn"));
1879 Function_type* hash_fntype = p->type()->function_type();
1882 go_assert(p->is_field_name("equalfn"));
1883 Function_type* equal_fntype = p->type()->function_type();
1885 Named_object* hash_fn;
1886 Named_object* equal_fn;
1887 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1889 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1890 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1893 go_assert(p->is_field_name("string"));
1894 Expression* s = Expression::make_string((name != NULL
1895 ? name->reflection(gogo)
1896 : this->reflection(gogo)),
1898 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1901 go_assert(p->is_field_name("uncommonType"));
1902 if (name == NULL && methods == NULL)
1903 vals->push_back(Expression::make_nil(bloc));
1906 if (methods == NULL)
1907 methods = name->methods();
1908 vals->push_back(this->uncommon_type_constructor(gogo,
1911 only_value_methods));
1915 go_assert(p->is_field_name("ptrToThis"));
1917 vals->push_back(Expression::make_nil(bloc));
1920 Type* pt = Type::make_pointer_type(name);
1921 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1925 go_assert(p == fields->end());
1929 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1932 // Return a composite literal for the uncommon type information for
1933 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1934 // struct. If name is not NULL, it is the name of the type. If
1935 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1936 // is true if only value methods should be included. At least one of
1937 // NAME and METHODS must not be NULL.
1940 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1941 Named_type* name, const Methods* methods,
1942 bool only_value_methods) const
1944 Location bloc = Linemap::predeclared_location();
1946 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1948 Expression_list* vals = new Expression_list();
1951 Struct_field_list::const_iterator p = fields->begin();
1952 go_assert(p->is_field_name("name"));
1955 go_assert(p->is_field_name("pkgPath"));
1959 vals->push_back(Expression::make_nil(bloc));
1960 vals->push_back(Expression::make_nil(bloc));
1964 Named_object* no = name->named_object();
1965 std::string n = Gogo::unpack_hidden_name(no->name());
1966 Expression* s = Expression::make_string(n, bloc);
1967 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1969 if (name->is_builtin())
1970 vals->push_back(Expression::make_nil(bloc));
1973 const Package* package = no->package();
1974 const std::string& unique_prefix(package == NULL
1975 ? gogo->unique_prefix()
1976 : package->unique_prefix());
1977 const std::string& package_name(package == NULL
1978 ? gogo->package_name()
1980 n.assign(unique_prefix);
1982 n.append(package_name);
1983 if (name->in_function() != NULL)
1986 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1988 s = Expression::make_string(n, bloc);
1989 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1994 go_assert(p->is_field_name("methods"));
1995 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1996 only_value_methods));
1999 go_assert(p == fields->end());
2001 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
2003 return Expression::make_unary(OPERATOR_AND, r, bloc);
2006 // Sort methods by name.
2012 operator()(const std::pair<std::string, const Method*>& m1,
2013 const std::pair<std::string, const Method*>& m2) const
2014 { return m1.first < m2.first; }
2017 // Return a composite literal for the type method table for this type.
2018 // METHODS_TYPE is the type of the table, and is a slice type.
2019 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
2020 // then only value methods are used.
2023 Type::methods_constructor(Gogo* gogo, Type* methods_type,
2024 const Methods* methods,
2025 bool only_value_methods) const
2027 Location bloc = Linemap::predeclared_location();
2029 std::vector<std::pair<std::string, const Method*> > smethods;
2030 if (methods != NULL)
2032 smethods.reserve(methods->count());
2033 for (Methods::const_iterator p = methods->begin();
2034 p != methods->end();
2037 if (p->second->is_ambiguous())
2039 if (only_value_methods && !p->second->is_value_method())
2041 smethods.push_back(std::make_pair(p->first, p->second));
2045 if (smethods.empty())
2046 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
2048 std::sort(smethods.begin(), smethods.end(), Sort_methods());
2050 Type* method_type = methods_type->array_type()->element_type();
2052 Expression_list* vals = new Expression_list();
2053 vals->reserve(smethods.size());
2054 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
2056 p != smethods.end();
2058 vals->push_back(this->method_constructor(gogo, method_type, p->first,
2059 p->second, only_value_methods));
2061 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
2064 // Return a composite literal for a single method. METHOD_TYPE is the
2065 // type of the entry. METHOD_NAME is the name of the method and M is
2066 // the method information.
2069 Type::method_constructor(Gogo*, Type* method_type,
2070 const std::string& method_name,
2072 bool only_value_methods) const
2074 Location bloc = Linemap::predeclared_location();
2076 const Struct_field_list* fields = method_type->struct_type()->fields();
2078 Expression_list* vals = new Expression_list();
2081 Struct_field_list::const_iterator p = fields->begin();
2082 go_assert(p->is_field_name("name"));
2083 const std::string n = Gogo::unpack_hidden_name(method_name);
2084 Expression* s = Expression::make_string(n, bloc);
2085 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2088 go_assert(p->is_field_name("pkgPath"));
2089 if (!Gogo::is_hidden_name(method_name))
2090 vals->push_back(Expression::make_nil(bloc));
2093 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
2094 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2097 Named_object* no = (m->needs_stub_method()
2099 : m->named_object());
2101 Function_type* mtype;
2102 if (no->is_function())
2103 mtype = no->func_value()->type();
2105 mtype = no->func_declaration_value()->type();
2106 go_assert(mtype->is_method());
2107 Type* nonmethod_type = mtype->copy_without_receiver();
2110 go_assert(p->is_field_name("mtyp"));
2111 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
2114 go_assert(p->is_field_name("typ"));
2115 if (!only_value_methods && m->is_value_method())
2117 // This is a value method on a pointer type. Change the type of
2118 // the method to use a pointer receiver. The implementation
2119 // always uses a pointer receiver anyhow.
2120 Type* rtype = mtype->receiver()->type();
2121 Type* prtype = Type::make_pointer_type(rtype);
2122 Typed_identifier* receiver =
2123 new Typed_identifier(mtype->receiver()->name(), prtype,
2124 mtype->receiver()->location());
2125 mtype = Type::make_function_type(receiver,
2126 (mtype->parameters() == NULL
2128 : mtype->parameters()->copy()),
2129 (mtype->results() == NULL
2131 : mtype->results()->copy()),
2134 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
2137 go_assert(p->is_field_name("tfn"));
2138 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
2141 go_assert(p == fields->end());
2143 return Expression::make_struct_composite_literal(method_type, vals, bloc);
2146 // Return a composite literal for the type descriptor of a plain type.
2147 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
2148 // NULL, it is the name to use as well as the list of methods.
2151 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
2154 return this->type_descriptor_constructor(gogo, runtime_type_kind,
2158 // Return the type reflection string for this type.
2161 Type::reflection(Gogo* gogo) const
2165 // The do_reflection virtual function should set RET to the
2166 // reflection string.
2167 this->do_reflection(gogo, &ret);
2172 // Return a mangled name for the type.
2175 Type::mangled_name(Gogo* gogo) const
2179 // The do_mangled_name virtual function should set RET to the
2180 // mangled name. For a composite type it should append a code for
2181 // the composition and then call do_mangled_name on the components.
2182 this->do_mangled_name(gogo, &ret);
2187 // Return whether the backend size of the type is known.
2190 Type::is_backend_type_size_known(Gogo* gogo)
2192 switch (this->classification_)
2206 case TYPE_INTERFACE:
2211 const Struct_field_list* fields = this->struct_type()->fields();
2212 for (Struct_field_list::const_iterator pf = fields->begin();
2213 pf != fields->end();
2215 if (!pf->type()->is_backend_type_size_known(gogo))
2222 const Array_type* at = this->array_type();
2223 if (at->length() == NULL)
2230 bool length_known = at->length()->integer_constant_value(true,
2236 return at->element_type()->is_backend_type_size_known(gogo);
2241 // Begin converting this type to the backend representation.
2242 // This will create a placeholder if necessary.
2243 this->get_backend(gogo);
2244 return this->named_type()->is_named_backend_type_size_known();
2248 Forward_declaration_type* fdt = this->forward_declaration_type();
2249 return fdt->real_type()->is_backend_type_size_known(gogo);
2253 case TYPE_CALL_MULTIPLE_RESULT:
2261 // If the size of the type can be determined, set *PSIZE to the size
2262 // in bytes and return true. Otherwise, return false. This queries
2266 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2268 if (!this->is_backend_type_size_known(gogo))
2270 Btype* bt = this->get_backend_placeholder(gogo);
2271 size_t size = gogo->backend()->type_size(bt);
2272 *psize = static_cast<unsigned int>(size);
2278 // If the alignment of the type can be determined, set *PALIGN to
2279 // the alignment in bytes and return true. Otherwise, return false.
2282 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2284 if (!this->is_backend_type_size_known(gogo))
2286 Btype* bt = this->get_backend_placeholder(gogo);
2287 size_t align = gogo->backend()->type_alignment(bt);
2288 *palign = static_cast<unsigned int>(align);
2289 if (*palign != align)
2294 // Like backend_type_align, but return the alignment when used as a
2298 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2300 if (!this->is_backend_type_size_known(gogo))
2302 Btype* bt = this->get_backend_placeholder(gogo);
2303 size_t a = gogo->backend()->type_field_alignment(bt);
2304 *palign = static_cast<unsigned int>(a);
2310 // Default function to export a type.
2313 Type::do_export(Export*) const
2321 Type::import_type(Import* imp)
2323 if (imp->match_c_string("("))
2324 return Function_type::do_import(imp);
2325 else if (imp->match_c_string("*"))
2326 return Pointer_type::do_import(imp);
2327 else if (imp->match_c_string("struct "))
2328 return Struct_type::do_import(imp);
2329 else if (imp->match_c_string("["))
2330 return Array_type::do_import(imp);
2331 else if (imp->match_c_string("map "))
2332 return Map_type::do_import(imp);
2333 else if (imp->match_c_string("chan "))
2334 return Channel_type::do_import(imp);
2335 else if (imp->match_c_string("interface"))
2336 return Interface_type::do_import(imp);
2339 error_at(imp->location(), "import error: expected type");
2340 return Type::make_error_type();
2344 // A type used to indicate a parsing error. This exists to simplify
2345 // later error detection.
2347 class Error_type : public Type
2356 do_compare_is_identity(Gogo*) const
2360 do_get_backend(Gogo* gogo)
2361 { return gogo->backend()->error_type(); }
2364 do_type_descriptor(Gogo*, Named_type*)
2365 { return Expression::make_error(Linemap::predeclared_location()); }
2368 do_reflection(Gogo*, std::string*) const
2369 { go_assert(saw_errors()); }
2372 do_mangled_name(Gogo*, std::string* ret) const
2373 { ret->push_back('E'); }
2377 Type::make_error_type()
2379 static Error_type singleton_error_type;
2380 return &singleton_error_type;
2385 class Void_type : public Type
2394 do_compare_is_identity(Gogo*) const
2398 do_get_backend(Gogo* gogo)
2399 { return gogo->backend()->void_type(); }
2402 do_type_descriptor(Gogo*, Named_type*)
2403 { go_unreachable(); }
2406 do_reflection(Gogo*, std::string*) const
2410 do_mangled_name(Gogo*, std::string* ret) const
2411 { ret->push_back('v'); }
2415 Type::make_void_type()
2417 static Void_type singleton_void_type;
2418 return &singleton_void_type;
2421 // The boolean type.
2423 class Boolean_type : public Type
2427 : Type(TYPE_BOOLEAN)
2432 do_compare_is_identity(Gogo*) const
2436 do_get_backend(Gogo* gogo)
2437 { return gogo->backend()->bool_type(); }
2440 do_type_descriptor(Gogo*, Named_type* name);
2442 // We should not be asked for the reflection string of a basic type.
2444 do_reflection(Gogo*, std::string* ret) const
2445 { ret->append("bool"); }
2448 do_mangled_name(Gogo*, std::string* ret) const
2449 { ret->push_back('b'); }
2452 // Make the type descriptor.
2455 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2458 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2461 Named_object* no = gogo->lookup_global("bool");
2462 go_assert(no != NULL);
2463 return Type::type_descriptor(gogo, no->type_value());
2468 Type::make_boolean_type()
2470 static Boolean_type boolean_type;
2471 return &boolean_type;
2474 // The named type "bool".
2476 static Named_type* named_bool_type;
2478 // Get the named type "bool".
2481 Type::lookup_bool_type()
2483 return named_bool_type;
2486 // Make the named type "bool".
2489 Type::make_named_bool_type()
2491 Type* bool_type = Type::make_boolean_type();
2492 Named_object* named_object =
2493 Named_object::make_type("bool", NULL, bool_type,
2494 Linemap::predeclared_location());
2495 Named_type* named_type = named_object->type_value();
2496 named_bool_type = named_type;
2500 // Class Integer_type.
2502 Integer_type::Named_integer_types Integer_type::named_integer_types;
2504 // Create a new integer type. Non-abstract integer types always have
2508 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2509 int bits, int runtime_type_kind)
2511 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2513 std::string sname(name);
2514 Named_object* named_object =
2515 Named_object::make_type(sname, NULL, integer_type,
2516 Linemap::predeclared_location());
2517 Named_type* named_type = named_object->type_value();
2518 std::pair<Named_integer_types::iterator, bool> ins =
2519 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2520 go_assert(ins.second);
2524 // Look up an existing integer type.
2527 Integer_type::lookup_integer_type(const char* name)
2529 Named_integer_types::const_iterator p =
2530 Integer_type::named_integer_types.find(name);
2531 go_assert(p != Integer_type::named_integer_types.end());
2535 // Create a new abstract integer type.
2538 Integer_type::create_abstract_integer_type()
2540 static Integer_type* abstract_type;
2541 if (abstract_type == NULL)
2542 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2543 RUNTIME_TYPE_KIND_INT);
2544 return abstract_type;
2547 // Create a new abstract character type.
2550 Integer_type::create_abstract_character_type()
2552 static Integer_type* abstract_type;
2553 if (abstract_type == NULL)
2555 abstract_type = new Integer_type(true, false, 32,
2556 RUNTIME_TYPE_KIND_INT32);
2557 abstract_type->set_is_rune();
2559 return abstract_type;
2562 // Integer type compatibility.
2565 Integer_type::is_identical(const Integer_type* t) const
2567 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2569 return this->is_abstract_ == t->is_abstract_;
2575 Integer_type::do_hash_for_method(Gogo*) const
2577 return ((this->bits_ << 4)
2578 + ((this->is_unsigned_ ? 1 : 0) << 8)
2579 + ((this->is_abstract_ ? 1 : 0) << 9));
2582 // Convert an Integer_type to the backend representation.
2585 Integer_type::do_get_backend(Gogo* gogo)
2587 if (this->is_abstract_)
2589 go_assert(saw_errors());
2590 return gogo->backend()->error_type();
2592 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2595 // The type descriptor for an integer type. Integer types are always
2599 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2601 go_assert(name != NULL || saw_errors());
2602 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2605 // We should not be asked for the reflection string of a basic type.
2608 Integer_type::do_reflection(Gogo*, std::string*) const
2610 go_assert(saw_errors());
2616 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2619 snprintf(buf, sizeof buf, "i%s%s%de",
2620 this->is_abstract_ ? "a" : "",
2621 this->is_unsigned_ ? "u" : "",
2626 // Make an integer type.
2629 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2630 int runtime_type_kind)
2632 return Integer_type::create_integer_type(name, is_unsigned, bits,
2636 // Make an abstract integer type.
2639 Type::make_abstract_integer_type()
2641 return Integer_type::create_abstract_integer_type();
2644 // Make an abstract character type.
2647 Type::make_abstract_character_type()
2649 return Integer_type::create_abstract_character_type();
2652 // Look up an integer type.
2655 Type::lookup_integer_type(const char* name)
2657 return Integer_type::lookup_integer_type(name);
2660 // Class Float_type.
2662 Float_type::Named_float_types Float_type::named_float_types;
2664 // Create a new float type. Non-abstract float types always have
2668 Float_type::create_float_type(const char* name, int bits,
2669 int runtime_type_kind)
2671 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2672 std::string sname(name);
2673 Named_object* named_object =
2674 Named_object::make_type(sname, NULL, float_type,
2675 Linemap::predeclared_location());
2676 Named_type* named_type = named_object->type_value();
2677 std::pair<Named_float_types::iterator, bool> ins =
2678 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2679 go_assert(ins.second);
2683 // Look up an existing float type.
2686 Float_type::lookup_float_type(const char* name)
2688 Named_float_types::const_iterator p =
2689 Float_type::named_float_types.find(name);
2690 go_assert(p != Float_type::named_float_types.end());
2694 // Create a new abstract float type.
2697 Float_type::create_abstract_float_type()
2699 static Float_type* abstract_type;
2700 if (abstract_type == NULL)
2701 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2702 return abstract_type;
2705 // Whether this type is identical with T.
2708 Float_type::is_identical(const Float_type* t) const
2710 if (this->bits_ != t->bits_)
2712 return this->is_abstract_ == t->is_abstract_;
2718 Float_type::do_hash_for_method(Gogo*) const
2720 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2723 // Convert to the backend representation.
2726 Float_type::do_get_backend(Gogo* gogo)
2728 return gogo->backend()->float_type(this->bits_);
2731 // The type descriptor for a float type. Float types are always named.
2734 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2736 go_assert(name != NULL || saw_errors());
2737 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2740 // We should not be asked for the reflection string of a basic type.
2743 Float_type::do_reflection(Gogo*, std::string*) const
2745 go_assert(saw_errors());
2751 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2754 snprintf(buf, sizeof buf, "f%s%de",
2755 this->is_abstract_ ? "a" : "",
2760 // Make a floating point type.
2763 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2765 return Float_type::create_float_type(name, bits, runtime_type_kind);
2768 // Make an abstract float type.
2771 Type::make_abstract_float_type()
2773 return Float_type::create_abstract_float_type();
2776 // Look up a float type.
2779 Type::lookup_float_type(const char* name)
2781 return Float_type::lookup_float_type(name);
2784 // Class Complex_type.
2786 Complex_type::Named_complex_types Complex_type::named_complex_types;
2788 // Create a new complex type. Non-abstract complex types always have
2792 Complex_type::create_complex_type(const char* name, int bits,
2793 int runtime_type_kind)
2795 Complex_type* complex_type = new Complex_type(false, bits,
2797 std::string sname(name);
2798 Named_object* named_object =
2799 Named_object::make_type(sname, NULL, complex_type,
2800 Linemap::predeclared_location());
2801 Named_type* named_type = named_object->type_value();
2802 std::pair<Named_complex_types::iterator, bool> ins =
2803 Complex_type::named_complex_types.insert(std::make_pair(sname,
2805 go_assert(ins.second);
2809 // Look up an existing complex type.
2812 Complex_type::lookup_complex_type(const char* name)
2814 Named_complex_types::const_iterator p =
2815 Complex_type::named_complex_types.find(name);
2816 go_assert(p != Complex_type::named_complex_types.end());
2820 // Create a new abstract complex type.
2823 Complex_type::create_abstract_complex_type()
2825 static Complex_type* abstract_type;
2826 if (abstract_type == NULL)
2827 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2828 return abstract_type;
2831 // Whether this type is identical with T.
2834 Complex_type::is_identical(const Complex_type *t) const
2836 if (this->bits_ != t->bits_)
2838 return this->is_abstract_ == t->is_abstract_;
2844 Complex_type::do_hash_for_method(Gogo*) const
2846 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2849 // Convert to the backend representation.
2852 Complex_type::do_get_backend(Gogo* gogo)
2854 return gogo->backend()->complex_type(this->bits_);
2857 // The type descriptor for a complex type. Complex types are always
2861 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2863 go_assert(name != NULL || saw_errors());
2864 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2867 // We should not be asked for the reflection string of a basic type.
2870 Complex_type::do_reflection(Gogo*, std::string*) const
2872 go_assert(saw_errors());
2878 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2881 snprintf(buf, sizeof buf, "c%s%de",
2882 this->is_abstract_ ? "a" : "",
2887 // Make a complex type.
2890 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2892 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2895 // Make an abstract complex type.
2898 Type::make_abstract_complex_type()
2900 return Complex_type::create_abstract_complex_type();
2903 // Look up a complex type.
2906 Type::lookup_complex_type(const char* name)
2908 return Complex_type::lookup_complex_type(name);
2911 // Class String_type.
2913 // Convert String_type to the backend representation. A string is a
2914 // struct with two fields: a pointer to the characters and a length.
2917 String_type::do_get_backend(Gogo* gogo)
2919 static Btype* backend_string_type;
2920 if (backend_string_type == NULL)
2922 std::vector<Backend::Btyped_identifier> fields(2);
2924 Type* b = gogo->lookup_global("byte")->type_value();
2925 Type* pb = Type::make_pointer_type(b);
2927 // We aren't going to get back to this field to finish the
2928 // backend representation, so force it to be finished now.
2929 if (!gogo->named_types_are_converted())
2931 pb->get_backend_placeholder(gogo);
2932 pb->finish_backend(gogo);
2935 fields[0].name = "__data";
2936 fields[0].btype = pb->get_backend(gogo);
2937 fields[0].location = Linemap::predeclared_location();
2939 Type* int_type = Type::lookup_integer_type("int");
2940 fields[1].name = "__length";
2941 fields[1].btype = int_type->get_backend(gogo);
2942 fields[1].location = fields[0].location;
2944 backend_string_type = gogo->backend()->struct_type(fields);
2946 return backend_string_type;
2949 // Return a tree for the length of STRING.
2952 String_type::length_tree(Gogo*, tree string)
2954 tree string_type = TREE_TYPE(string);
2955 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2956 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2957 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2959 return fold_build3(COMPONENT_REF, integer_type_node, string,
2960 length_field, NULL_TREE);
2963 // Return a tree for a pointer to the bytes of STRING.
2966 String_type::bytes_tree(Gogo*, tree string)
2968 tree string_type = TREE_TYPE(string);
2969 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2970 tree bytes_field = TYPE_FIELDS(string_type);
2971 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2973 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2974 bytes_field, NULL_TREE);
2977 // The type descriptor for the string type.
2980 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2983 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2986 Named_object* no = gogo->lookup_global("string");
2987 go_assert(no != NULL);
2988 return Type::type_descriptor(gogo, no->type_value());
2992 // We should not be asked for the reflection string of a basic type.
2995 String_type::do_reflection(Gogo*, std::string* ret) const
2997 ret->append("string");
3000 // Mangled name of a string type.
3003 String_type::do_mangled_name(Gogo*, std::string* ret) const
3005 ret->push_back('z');
3008 // Make a string type.
3011 Type::make_string_type()
3013 static String_type string_type;
3014 return &string_type;
3017 // The named type "string".
3019 static Named_type* named_string_type;
3021 // Get the named type "string".
3024 Type::lookup_string_type()
3026 return named_string_type;
3029 // Make the named type string.
3032 Type::make_named_string_type()
3034 Type* string_type = Type::make_string_type();
3035 Named_object* named_object =
3036 Named_object::make_type("string", NULL, string_type,
3037 Linemap::predeclared_location());
3038 Named_type* named_type = named_object->type_value();
3039 named_string_type = named_type;
3043 // The sink type. This is the type of the blank identifier _. Any
3044 // type may be assigned to it.
3046 class Sink_type : public Type
3055 do_compare_is_identity(Gogo*) const
3059 do_get_backend(Gogo*)
3060 { go_unreachable(); }
3063 do_type_descriptor(Gogo*, Named_type*)
3064 { go_unreachable(); }
3067 do_reflection(Gogo*, std::string*) const
3068 { go_unreachable(); }
3071 do_mangled_name(Gogo*, std::string*) const
3072 { go_unreachable(); }
3075 // Make the sink type.
3078 Type::make_sink_type()
3080 static Sink_type sink_type;
3084 // Class Function_type.
3089 Function_type::do_traverse(Traverse* traverse)
3091 if (this->receiver_ != NULL
3092 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
3093 return TRAVERSE_EXIT;
3094 if (this->parameters_ != NULL
3095 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
3096 return TRAVERSE_EXIT;
3097 if (this->results_ != NULL
3098 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
3099 return TRAVERSE_EXIT;
3100 return TRAVERSE_CONTINUE;
3103 // Returns whether T is a valid redeclaration of this type. If this
3104 // returns false, and REASON is not NULL, *REASON may be set to a
3105 // brief explanation of why it returned false.
3108 Function_type::is_valid_redeclaration(const Function_type* t,
3109 std::string* reason) const
3111 if (!this->is_identical(t, false, true, reason))
3114 // A redeclaration of a function is required to use the same names
3115 // for the receiver and parameters.
3116 if (this->receiver() != NULL
3117 && this->receiver()->name() != t->receiver()->name())
3120 *reason = "receiver name changed";
3124 const Typed_identifier_list* parms1 = this->parameters();
3125 const Typed_identifier_list* parms2 = t->parameters();
3128 Typed_identifier_list::const_iterator p1 = parms1->begin();
3129 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3130 p2 != parms2->end();
3133 if (p1->name() != p2->name())
3136 *reason = "parameter name changed";
3140 // This is called at parse time, so we may have unknown
3142 Type* t1 = p1->type()->forwarded();
3143 Type* t2 = p2->type()->forwarded();
3145 && t1->forward_declaration_type() != NULL
3146 && (t2->forward_declaration_type() == NULL
3147 || (t1->forward_declaration_type()->named_object()
3148 != t2->forward_declaration_type()->named_object())))
3153 const Typed_identifier_list* results1 = this->results();
3154 const Typed_identifier_list* results2 = t->results();
3155 if (results1 != NULL)
3157 Typed_identifier_list::const_iterator res1 = results1->begin();
3158 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3159 res2 != results2->end();
3162 if (res1->name() != res2->name())
3165 *reason = "result name changed";
3169 // This is called at parse time, so we may have unknown
3171 Type* t1 = res1->type()->forwarded();
3172 Type* t2 = res2->type()->forwarded();
3174 && t1->forward_declaration_type() != NULL
3175 && (t2->forward_declaration_type() == NULL
3176 || (t1->forward_declaration_type()->named_object()
3177 != t2->forward_declaration_type()->named_object())))
3185 // Check whether T is the same as this type.
3188 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
3189 bool errors_are_identical,
3190 std::string* reason) const
3192 if (!ignore_receiver)
3194 const Typed_identifier* r1 = this->receiver();
3195 const Typed_identifier* r2 = t->receiver();
3196 if ((r1 != NULL) != (r2 != NULL))
3199 *reason = _("different receiver types");
3204 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
3207 if (reason != NULL && !reason->empty())
3208 *reason = "receiver: " + *reason;
3214 const Typed_identifier_list* parms1 = this->parameters();
3215 const Typed_identifier_list* parms2 = t->parameters();
3216 if ((parms1 != NULL) != (parms2 != NULL))
3219 *reason = _("different number of parameters");
3224 Typed_identifier_list::const_iterator p1 = parms1->begin();
3225 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3226 p2 != parms2->end();
3229 if (p1 == parms1->end())
3232 *reason = _("different number of parameters");
3236 if (!Type::are_identical(p1->type(), p2->type(),
3237 errors_are_identical, NULL))
3240 *reason = _("different parameter types");
3244 if (p1 != parms1->end())
3247 *reason = _("different number of parameters");
3252 if (this->is_varargs() != t->is_varargs())
3255 *reason = _("different varargs");
3259 const Typed_identifier_list* results1 = this->results();
3260 const Typed_identifier_list* results2 = t->results();
3261 if ((results1 != NULL) != (results2 != NULL))
3264 *reason = _("different number of results");
3267 if (results1 != NULL)
3269 Typed_identifier_list::const_iterator res1 = results1->begin();
3270 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3271 res2 != results2->end();
3274 if (res1 == results1->end())
3277 *reason = _("different number of results");
3281 if (!Type::are_identical(res1->type(), res2->type(),
3282 errors_are_identical, NULL))
3285 *reason = _("different result types");
3289 if (res1 != results1->end())
3292 *reason = _("different number of results");
3303 Function_type::do_hash_for_method(Gogo* gogo) const
3305 unsigned int ret = 0;
3306 // We ignore the receiver type for hash codes, because we need to
3307 // get the same hash code for a method in an interface and a method
3308 // declared for a type. The former will not have a receiver.
3309 if (this->parameters_ != NULL)
3312 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3313 p != this->parameters_->end();
3315 ret += p->type()->hash_for_method(gogo) << shift;
3317 if (this->results_ != NULL)
3320 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3321 p != this->results_->end();
3323 ret += p->type()->hash_for_method(gogo) << shift;
3325 if (this->is_varargs_)
3331 // Get the backend representation for a function type.
3334 Function_type::do_get_backend(Gogo* gogo)
3336 Backend::Btyped_identifier breceiver;
3337 if (this->receiver_ != NULL)
3339 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3341 // We always pass the address of the receiver parameter, in
3342 // order to make interface calls work with unknown types.
3343 Type* rtype = this->receiver_->type();
3344 if (rtype->points_to() == NULL)
3345 rtype = Type::make_pointer_type(rtype);
3346 breceiver.btype = rtype->get_backend(gogo);
3347 breceiver.location = this->receiver_->location();
3350 std::vector<Backend::Btyped_identifier> bparameters;
3351 if (this->parameters_ != NULL)
3353 bparameters.resize(this->parameters_->size());
3355 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3356 p != this->parameters_->end();
3359 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3360 bparameters[i].btype = p->type()->get_backend(gogo);
3361 bparameters[i].location = p->location();
3363 go_assert(i == bparameters.size());
3366 std::vector<Backend::Btyped_identifier> bresults;
3367 if (this->results_ != NULL)
3369 bresults.resize(this->results_->size());
3371 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3372 p != this->results_->end();
3375 bresults[i].name = Gogo::unpack_hidden_name(p->name());
3376 bresults[i].btype = p->type()->get_backend(gogo);
3377 bresults[i].location = p->location();
3379 go_assert(i == bresults.size());
3382 return gogo->backend()->function_type(breceiver, bparameters, bresults,
3386 // The type of a function type descriptor.
3389 Function_type::make_function_type_descriptor_type()
3394 Type* tdt = Type::make_type_descriptor_type();
3395 Type* ptdt = Type::make_type_descriptor_ptr_type();
3397 Type* bool_type = Type::lookup_bool_type();
3399 Type* slice_type = Type::make_array_type(ptdt, NULL);
3401 Struct_type* s = Type::make_builtin_struct_type(4,
3403 "dotdotdot", bool_type,
3407 ret = Type::make_builtin_named_type("FuncType", s);
3413 // The type descriptor for a function type.
3416 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3418 Location bloc = Linemap::predeclared_location();
3420 Type* ftdt = Function_type::make_function_type_descriptor_type();
3422 const Struct_field_list* fields = ftdt->struct_type()->fields();
3424 Expression_list* vals = new Expression_list();
3427 Struct_field_list::const_iterator p = fields->begin();
3428 go_assert(p->is_field_name("commonType"));
3429 vals->push_back(this->type_descriptor_constructor(gogo,
3430 RUNTIME_TYPE_KIND_FUNC,
3434 go_assert(p->is_field_name("dotdotdot"));
3435 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3438 go_assert(p->is_field_name("in"));
3439 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3440 this->parameters()));
3443 go_assert(p->is_field_name("out"));
3444 vals->push_back(this->type_descriptor_params(p->type(), NULL,
3448 go_assert(p == fields->end());
3450 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3453 // Return a composite literal for the parameters or results of a type
3457 Function_type::type_descriptor_params(Type* params_type,
3458 const Typed_identifier* receiver,
3459 const Typed_identifier_list* params)
3461 Location bloc = Linemap::predeclared_location();
3463 if (receiver == NULL && params == NULL)
3464 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3466 Expression_list* vals = new Expression_list();
3467 vals->reserve((params == NULL ? 0 : params->size())
3468 + (receiver != NULL ? 1 : 0));
3470 if (receiver != NULL)
3471 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3475 for (Typed_identifier_list::const_iterator p = params->begin();
3478 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3481 return Expression::make_slice_composite_literal(params_type, vals, bloc);
3484 // The reflection string.
3487 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3489 // FIXME: Turn this off until we straighten out the type of the
3490 // struct field used in a go statement which calls a method.
3491 // go_assert(this->receiver_ == NULL);
3493 ret->append("func");
3495 if (this->receiver_ != NULL)
3497 ret->push_back('(');
3498 this->append_reflection(this->receiver_->type(), gogo, ret);
3499 ret->push_back(')');
3502 ret->push_back('(');
3503 const Typed_identifier_list* params = this->parameters();
3506 bool is_varargs = this->is_varargs_;
3507 for (Typed_identifier_list::const_iterator p = params->begin();
3511 if (p != params->begin())
3513 if (!is_varargs || p + 1 != params->end())
3514 this->append_reflection(p->type(), gogo, ret);
3518 this->append_reflection(p->type()->array_type()->element_type(),
3523 ret->push_back(')');