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.
3529 // go_assert(this->receiver_ == NULL);
3531 ret->append("func");
3533 if (this->receiver_ != NULL)
3535 ret->push_back('(');
3536 this->append_reflection(this->receiver_->type(), gogo, ret);
3537 ret->push_back(')');
3540 ret->push_back('(');
3541 const Typed_identifier_list* params = this->parameters();
3544 bool is_varargs = this->is_varargs_;
3545 for (Typed_identifier_list::const_iterator p = params->begin();
3549 if (p != params->begin())
3551 if (!is_varargs || p + 1 != params->end())
3552 this->append_reflection(p->type(), gogo, ret);
3556 this->append_reflection(p->type()->array_type()->element_type(),
3561 ret->push_back(')');
3563 const Typed_identifier_list* results = this->results();
3564 if (results != NULL && !results->empty())
3566 if (results->size() == 1)
3567 ret->push_back(' ');
3570 for (Typed_identifier_list::const_iterator p = results->begin();
3571 p != results->end();
3574 if (p != results->begin())
3576 this->append_reflection(p->type(), gogo, ret);
3578 if (results->size() > 1)
3579 ret->push_back(')');
3586 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3588 ret->push_back('F');
3590 if (this->receiver_ != NULL)
3592 ret->push_back('m');
3593 this->append_mangled_name(this->receiver_->type(), gogo, ret);
3596 const Typed_identifier_list* params = this->parameters();
3599 ret->push_back('p');
3600 for (Typed_identifier_list::const_iterator p = params->begin();
3603 this->append_mangled_name(p->type(), gogo, ret);
3604 if (this->is_varargs_)
3605 ret->push_back('V');
3606 ret->push_back('e');
3609 const Typed_identifier_list* results = this->results();
3610 if (results != NULL)
3612 ret->push_back('r');
3613 for (Typed_identifier_list::const_iterator p = results->begin();
3614 p != results->end();
3616 this->append_mangled_name(p->type(), gogo, ret);
3617 ret->push_back('e');
3620 ret->push_back('e');
3623 // Export a function type.
3626 Function_type::do_export(Export* exp) const
3628 // We don't write out the receiver. The only function types which
3629 // should have a receiver are the ones associated with explicitly
3630 // defined methods. For those the receiver type is written out by
3631 // Function::export_func.
3633 exp->write_c_string("(");
3635 if (this->parameters_ != NULL)
3637 bool is_varargs = this->is_varargs_;
3638 for (Typed_identifier_list::const_iterator p =
3639 this->parameters_->begin();
3640 p != this->parameters_->end();
3646 exp->write_c_string(", ");
3647 exp->write_name(p->name());
3648 exp->write_c_string(" ");
3649 if (!is_varargs || p + 1 != this->parameters_->end())
3650 exp->write_type(p->type());
3653 exp->write_c_string("...");
3654 exp->write_type(p->type()->array_type()->element_type());
3658 exp->write_c_string(")");
3660 const Typed_identifier_list* results = this->results_;
3661 if (results != NULL)
3663 exp->write_c_string(" ");
3664 if (results->size() == 1 && results->begin()->name().empty())
3665 exp->write_type(results->begin()->type());
3669 exp->write_c_string("(");
3670 for (Typed_identifier_list::const_iterator p = results->begin();
3671 p != results->end();
3677 exp->write_c_string(", ");
3678 exp->write_name(p->name());
3679 exp->write_c_string(" ");
3680 exp->write_type(p->type());
3682 exp->write_c_string(")");
3687 // Import a function type.
3690 Function_type::do_import(Import* imp)
3692 imp->require_c_string("(");
3693 Typed_identifier_list* parameters;
3694 bool is_varargs = false;
3695 if (imp->peek_char() == ')')
3699 parameters = new Typed_identifier_list();
3702 std::string name = imp->read_name();
3703 imp->require_c_string(" ");
3705 if (imp->match_c_string("..."))
3711 Type* ptype = imp->read_type();
3713 ptype = Type::make_array_type(ptype, NULL);
3714 parameters->push_back(Typed_identifier(name, ptype,
3716 if (imp->peek_char() != ',')
3718 go_assert(!is_varargs);
3719 imp->require_c_string(", ");
3722 imp->require_c_string(")");
3724 Typed_identifier_list* results;
3725 if (imp->peek_char() != ' ')
3730 results = new Typed_identifier_list;
3731 if (imp->peek_char() != '(')
3733 Type* rtype = imp->read_type();
3734 results->push_back(Typed_identifier("", rtype, imp->location()));
3741 std::string name = imp->read_name();
3742 imp->require_c_string(" ");
3743 Type* rtype = imp->read_type();
3744 results->push_back(Typed_identifier(name, rtype,
3746 if (imp->peek_char() != ',')
3748 imp->require_c_string(", ");
3750 imp->require_c_string(")");
3754 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3757 ret->set_is_varargs();
3761 // Make a copy of a function type without a receiver.
3764 Function_type::copy_without_receiver() const
3766 go_assert(this->is_method());
3767 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3770 if (this->is_varargs())
3771 ret->set_is_varargs();
3772 if (this->is_builtin())
3773 ret->set_is_builtin();
3777 // Make a copy of a function type with a receiver.
3780 Function_type::copy_with_receiver(Type* receiver_type) const
3782 go_assert(!this->is_method());
3783 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3785 Function_type* ret = Type::make_function_type(receiver, this->parameters_,
3788 if (this->is_varargs_)
3789 ret->set_is_varargs();
3793 // Make a function type.
3796 Type::make_function_type(Typed_identifier* receiver,
3797 Typed_identifier_list* parameters,
3798 Typed_identifier_list* results,
3801 return new Function_type(receiver, parameters, results, location);
3804 // Class Pointer_type.
3809 Pointer_type::do_traverse(Traverse* traverse)
3811 return Type::traverse(this->to_type_, traverse);
3817 Pointer_type::do_hash_for_method(Gogo* gogo) const
3819 return this->to_type_->hash_for_method(gogo) << 4;
3822 // Get the backend representation for a pointer type.
3825 Pointer_type::do_get_backend(Gogo* gogo)
3827 Btype* to_btype = this->to_type_->get_backend(gogo);
3828 return gogo->backend()->pointer_type(to_btype);
3831 // The type of a pointer type descriptor.
3834 Pointer_type::make_pointer_type_descriptor_type()
3839 Type* tdt = Type::make_type_descriptor_type();
3840 Type* ptdt = Type::make_type_descriptor_ptr_type();
3842 Struct_type* s = Type::make_builtin_struct_type(2,
3846 ret = Type::make_builtin_named_type("PtrType", s);
3852 // The type descriptor for a pointer type.
3855 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3857 if (this->is_unsafe_pointer_type())
3859 go_assert(name != NULL);
3860 return this->plain_type_descriptor(gogo,
3861 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3866 Location bloc = Linemap::predeclared_location();
3868 const Methods* methods;
3869 Type* deref = this->points_to();
3870 if (deref->named_type() != NULL)
3871 methods = deref->named_type()->methods();
3872 else if (deref->struct_type() != NULL)
3873 methods = deref->struct_type()->methods();
3877 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3879 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3881 Expression_list* vals = new Expression_list();
3884 Struct_field_list::const_iterator p = fields->begin();
3885 go_assert(p->is_field_name("commonType"));
3886 vals->push_back(this->type_descriptor_constructor(gogo,
3887 RUNTIME_TYPE_KIND_PTR,
3888 name, methods, false));
3891 go_assert(p->is_field_name("elem"));
3892 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3894 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3898 // Reflection string.
3901 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3903 ret->push_back('*');
3904 this->append_reflection(this->to_type_, gogo, ret);
3910 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3912 ret->push_back('p');
3913 this->append_mangled_name(this->to_type_, gogo, ret);
3919 Pointer_type::do_export(Export* exp) const
3921 exp->write_c_string("*");
3922 if (this->is_unsafe_pointer_type())
3923 exp->write_c_string("any");
3925 exp->write_type(this->to_type_);
3931 Pointer_type::do_import(Import* imp)
3933 imp->require_c_string("*");
3934 if (imp->match_c_string("any"))
3937 return Type::make_pointer_type(Type::make_void_type());
3939 Type* to = imp->read_type();
3940 return Type::make_pointer_type(to);
3943 // Make a pointer type.
3946 Type::make_pointer_type(Type* to_type)
3948 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3949 static Hashtable pointer_types;
3950 Hashtable::const_iterator p = pointer_types.find(to_type);
3951 if (p != pointer_types.end())
3953 Pointer_type* ret = new Pointer_type(to_type);
3954 pointer_types[to_type] = ret;
3958 // The nil type. We use a special type for nil because it is not the
3959 // same as any other type. In C term nil has type void*, but there is
3960 // no such type in Go.
3962 class Nil_type : public Type
3971 do_compare_is_identity(Gogo*) const
3975 do_get_backend(Gogo* gogo)
3976 { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3979 do_type_descriptor(Gogo*, Named_type*)
3980 { go_unreachable(); }
3983 do_reflection(Gogo*, std::string*) const
3984 { go_unreachable(); }
3987 do_mangled_name(Gogo*, std::string* ret) const
3988 { ret->push_back('n'); }
3991 // Make the nil type.
3994 Type::make_nil_type()
3996 static Nil_type singleton_nil_type;
3997 return &singleton_nil_type;
4000 // The type of a function call which returns multiple values. This is
4001 // really a struct, but we don't want to confuse a function call which
4002 // returns a struct with a function call which returns multiple
4005 class Call_multiple_result_type : public Type
4008 Call_multiple_result_type(Call_expression* call)
4009 : Type(TYPE_CALL_MULTIPLE_RESULT),
4015 do_has_pointer() const
4017 go_assert(saw_errors());
4022 do_compare_is_identity(Gogo*) const
4026 do_get_backend(Gogo* gogo)
4028 go_assert(saw_errors());
4029 return gogo->backend()->error_type();
4033 do_type_descriptor(Gogo*, Named_type*)
4035 go_assert(saw_errors());
4036 return Expression::make_error(Linemap::unknown_location());
4040 do_reflection(Gogo*, std::string*) const
4041 { go_assert(saw_errors()); }
4044 do_mangled_name(Gogo*, std::string*) const
4045 { go_assert(saw_errors()); }
4048 // The expression being called.
4049 Call_expression* call_;
4052 // Make a call result type.
4055 Type::make_call_multiple_result_type(Call_expression* call)
4057 return new Call_multiple_result_type(call);
4060 // Class Struct_field.
4062 // Get the name of a field.
4065 Struct_field::field_name() const
4067 const std::string& name(this->typed_identifier_.name());
4072 // This is called during parsing, before anything is lowered, so
4073 // we have to be pretty careful to avoid dereferencing an
4074 // unknown type name.
4075 Type* t = this->typed_identifier_.type();
4077 if (t->classification() == Type::TYPE_POINTER)
4080 Pointer_type* ptype = static_cast<Pointer_type*>(t);
4081 dt = ptype->points_to();
4083 if (dt->forward_declaration_type() != NULL)
4084 return dt->forward_declaration_type()->name();
4085 else if (dt->named_type() != NULL)
4086 return dt->named_type()->name();
4087 else if (t->is_error_type() || dt->is_error_type())
4089 static const std::string error_string = "*error*";
4090 return error_string;
4094 // Avoid crashing in the erroneous case where T is named but
4097 if (t->forward_declaration_type() != NULL)
4098 return t->forward_declaration_type()->name();
4099 else if (t->named_type() != NULL)
4100 return t->named_type()->name();
4107 // Return whether this field is named NAME.
4110 Struct_field::is_field_name(const std::string& name) const
4112 const std::string& me(this->typed_identifier_.name());
4117 Type* t = this->typed_identifier_.type();
4118 if (t->points_to() != NULL)
4120 Named_type* nt = t->named_type();
4121 if (nt != NULL && nt->name() == name)
4124 // This is a horrible hack caused by the fact that we don't pack
4125 // the names of builtin types. FIXME.
4128 && nt->name() == Gogo::unpack_hidden_name(name))
4135 // Class Struct_type.
4140 Struct_type::do_traverse(Traverse* traverse)
4142 Struct_field_list* fields = this->fields_;
4145 for (Struct_field_list::iterator p = fields->begin();
4149 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
4150 return TRAVERSE_EXIT;
4153 return TRAVERSE_CONTINUE;
4156 // Verify that the struct type is complete and valid.
4159 Struct_type::do_verify()
4161 Struct_field_list* fields = this->fields_;
4164 for (Struct_field_list::iterator p = fields->begin();
4168 Type* t = p->type();
4169 if (t->is_undefined())
4171 error_at(p->location(), "struct field type is incomplete");
4172 p->set_type(Type::make_error_type());
4174 else if (p->is_anonymous())
4176 if (t->named_type() != NULL && t->points_to() != NULL)
4178 error_at(p->location(), "embedded type may not be a pointer");
4179 p->set_type(Type::make_error_type());
4181 else if (t->points_to() != NULL
4182 && t->points_to()->interface_type() != NULL)
4184 error_at(p->location(),
4185 "embedded type may not be pointer to interface");
4186 p->set_type(Type::make_error_type());
4193 // Whether this contains a pointer.
4196 Struct_type::do_has_pointer() const
4198 const Struct_field_list* fields = this->fields();
4201 for (Struct_field_list::const_iterator p = fields->begin();
4205 if (p->type()->has_pointer())
4211 // Whether this type is identical to T.
4214 Struct_type::is_identical(const Struct_type* t,
4215 bool errors_are_identical) const
4217 const Struct_field_list* fields1 = this->fields();
4218 const Struct_field_list* fields2 = t->fields();
4219 if (fields1 == NULL || fields2 == NULL)
4220 return fields1 == fields2;
4221 Struct_field_list::const_iterator pf2 = fields2->begin();
4222 for (Struct_field_list::const_iterator pf1 = fields1->begin();
4223 pf1 != fields1->end();
4226 if (pf2 == fields2->end())
4228 if (pf1->field_name() != pf2->field_name())
4230 if (pf1->is_anonymous() != pf2->is_anonymous()
4231 || !Type::are_identical(pf1->type(), pf2->type(),
4232 errors_are_identical, NULL))
4234 if (!pf1->has_tag())
4241 if (!pf2->has_tag())
4243 if (pf1->tag() != pf2->tag())
4247 if (pf2 != fields2->end())
4252 // Whether this struct type has any hidden fields.
4255 Struct_type::struct_has_hidden_fields(const Named_type* within,
4256 std::string* reason) const
4258 const Struct_field_list* fields = this->fields();
4261 const Package* within_package = (within == NULL
4263 : within->named_object()->package());
4264 for (Struct_field_list::const_iterator pf = fields->begin();
4265 pf != fields->end();
4268 if (within_package != NULL
4269 && !pf->is_anonymous()
4270 && Gogo::is_hidden_name(pf->field_name()))
4274 std::string within_name = within->named_object()->message_name();
4275 std::string name = Gogo::message_name(pf->field_name());
4276 size_t bufsize = 200 + within_name.length() + name.length();
4277 char* buf = new char[bufsize];
4278 snprintf(buf, bufsize,
4279 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4280 open_quote, within_name.c_str(), close_quote,
4281 open_quote, name.c_str(), close_quote);
4282 reason->assign(buf);
4288 if (pf->type()->has_hidden_fields(within, reason))
4295 // Whether comparisons of this struct type are simple identity
4299 Struct_type::do_compare_is_identity(Gogo* gogo) const
4301 const Struct_field_list* fields = this->fields_;
4304 unsigned int offset = 0;
4305 for (Struct_field_list::const_iterator pf = fields->begin();
4306 pf != fields->end();
4309 if (Gogo::is_sink_name(pf->field_name()))
4312 if (!pf->type()->compare_is_identity(gogo))
4315 unsigned int field_align;
4316 if (!pf->type()->backend_type_align(gogo, &field_align))
4318 if ((offset & (field_align - 1)) != 0)
4320 // This struct has padding. We don't guarantee that that
4321 // padding is zero-initialized for a stack variable, so we
4322 // can't use memcmp to compare struct values.
4326 unsigned int field_size;
4327 if (!pf->type()->backend_type_size(gogo, &field_size))
4329 offset += field_size;
4334 // Build identity and hash functions for this struct.
4339 Struct_type::do_hash_for_method(Gogo* gogo) const
4341 unsigned int ret = 0;
4342 if (this->fields() != NULL)
4344 for (Struct_field_list::const_iterator pf = this->fields()->begin();
4345 pf != this->fields()->end();
4347 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4352 // Find the local field NAME.
4355 Struct_type::find_local_field(const std::string& name,
4356 unsigned int *pindex) const
4358 const Struct_field_list* fields = this->fields_;
4362 for (Struct_field_list::const_iterator pf = fields->begin();
4363 pf != fields->end();
4366 if (pf->is_field_name(name))
4376 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4378 Field_reference_expression*
4379 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4380 Location location) const
4383 return this->field_reference_depth(struct_expr, name, location, NULL,
4387 // Return an expression for a field, along with the depth at which it
4390 Field_reference_expression*
4391 Struct_type::field_reference_depth(Expression* struct_expr,
4392 const std::string& name,
4394 Saw_named_type* saw,
4395 unsigned int* depth) const
4397 const Struct_field_list* fields = this->fields_;
4401 // Look for a field with this name.
4403 for (Struct_field_list::const_iterator pf = fields->begin();
4404 pf != fields->end();
4407 if (pf->is_field_name(name))
4410 return Expression::make_field_reference(struct_expr, i, location);
4414 // Look for an anonymous field which contains a field with this
4416 unsigned int found_depth = 0;
4417 Field_reference_expression* ret = NULL;
4419 for (Struct_field_list::const_iterator pf = fields->begin();
4420 pf != fields->end();
4423 if (!pf->is_anonymous())
4426 Struct_type* st = pf->type()->deref()->struct_type();
4430 Saw_named_type* hold_saw = saw;
4431 Saw_named_type saw_here;
4432 Named_type* nt = pf->type()->named_type();
4434 nt = pf->type()->deref()->named_type();
4438 for (q = saw; q != NULL; q = q->next)
4442 // If this is an error, it will be reported
4449 saw_here.next = saw;
4454 // Look for a reference using a NULL struct expression. If we
4455 // find one, fill in the struct expression with a reference to
4457 unsigned int subdepth;
4458 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
4468 if (ret == NULL || subdepth < found_depth)
4473 found_depth = subdepth;
4474 Expression* here = Expression::make_field_reference(struct_expr, i,
4476 if (pf->type()->points_to() != NULL)
4477 here = Expression::make_unary(OPERATOR_MULT, here, location);
4478 while (sub->expr() != NULL)
4480 sub = sub->expr()->deref()->field_reference_expression();
4481 go_assert(sub != NULL);
4483 sub->set_struct_expression(here);
4485 else if (subdepth > found_depth)
4489 // We do not handle ambiguity here--it should be handled by
4490 // Type::bind_field_or_method.
4498 *depth = found_depth + 1;
4503 // Return the total number of fields, including embedded fields.
4506 Struct_type::total_field_count() const
4508 if (this->fields_ == NULL)
4510 unsigned int ret = 0;
4511 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4512 pf != this->fields_->end();
4515 if (!pf->is_anonymous() || pf->type()->struct_type() == NULL)
4518 ret += pf->type()->struct_type()->total_field_count();
4523 // Return whether NAME is an unexported field, for better error reporting.
4526 Struct_type::is_unexported_local_field(Gogo* gogo,
4527 const std::string& name) const
4529 const Struct_field_list* fields = this->fields_;
4532 for (Struct_field_list::const_iterator pf = fields->begin();
4533 pf != fields->end();
4536 const std::string& field_name(pf->field_name());
4537 if (Gogo::is_hidden_name(field_name)
4538 && name == Gogo::unpack_hidden_name(field_name)
4539 && gogo->pack_hidden_name(name, false) != field_name)
4546 // Finalize the methods of an unnamed struct.
4549 Struct_type::finalize_methods(Gogo* gogo)
4551 if (this->all_methods_ != NULL)
4553 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
4556 // Return the method NAME, or NULL if there isn't one or if it is
4557 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
4561 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
4563 return Type::method_function(this->all_methods_, name, is_ambiguous);
4566 // Convert struct fields to the backend representation. This is not
4567 // declared in types.h so that types.h doesn't have to #include
4571 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
4572 bool use_placeholder,
4573 std::vector<Backend::Btyped_identifier>* bfields)
4575 bfields->resize(fields->size());
4577 for (Struct_field_list::const_iterator p = fields->begin();
4581 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
4582 (*bfields)[i].btype = (use_placeholder
4583 ? p->type()->get_backend_placeholder(gogo)
4584 : p->type()->get_backend(gogo));
4585 (*bfields)[i].location = p->location();
4587 go_assert(i == fields->size());
4590 // Get the tree for a struct type.
4593 Struct_type::do_get_backend(Gogo* gogo)
4595 std::vector<Backend::Btyped_identifier> bfields;
4596 get_backend_struct_fields(gogo, this->fields_, false, &bfields);
4597 return gogo->backend()->struct_type(bfields);
4600 // Finish the backend representation of the fields of a struct.
4603 Struct_type::finish_backend_fields(Gogo* gogo)
4605 const Struct_field_list* fields = this->fields_;
4608 for (Struct_field_list::const_iterator p = fields->begin();
4611 p->type()->get_backend(gogo);
4615 // The type of a struct type descriptor.
4618 Struct_type::make_struct_type_descriptor_type()
4623 Type* tdt = Type::make_type_descriptor_type();
4624 Type* ptdt = Type::make_type_descriptor_ptr_type();
4626 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4627 Type* string_type = Type::lookup_string_type();
4628 Type* pointer_string_type = Type::make_pointer_type(string_type);
4631 Type::make_builtin_struct_type(5,
4632 "name", pointer_string_type,
4633 "pkgPath", pointer_string_type,
4635 "tag", pointer_string_type,
4636 "offset", uintptr_type);
4637 Type* nsf = Type::make_builtin_named_type("structField", sf);
4639 Type* slice_type = Type::make_array_type(nsf, NULL);
4641 Struct_type* s = Type::make_builtin_struct_type(2,
4643 "fields", slice_type);
4645 ret = Type::make_builtin_named_type("StructType", s);
4651 // Build a type descriptor for a struct type.
4654 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4656 Location bloc = Linemap::predeclared_location();
4658 Type* stdt = Struct_type::make_struct_type_descriptor_type();
4660 const Struct_field_list* fields = stdt->struct_type()->fields();
4662 Expression_list* vals = new Expression_list();
4665 const Methods* methods = this->methods();
4666 // A named struct should not have methods--the methods should attach
4667 // to the named type.
4668 go_assert(methods == NULL || name == NULL);
4670 Struct_field_list::const_iterator ps = fields->begin();
4671 go_assert(ps->is_field_name("commonType"));
4672 vals->push_back(this->type_descriptor_constructor(gogo,
4673 RUNTIME_TYPE_KIND_STRUCT,
4674 name, methods, true));
4677 go_assert(ps->is_field_name("fields"));
4679 Expression_list* elements = new Expression_list();
4680 elements->reserve(this->fields_->size());
4681 Type* element_type = ps->type()->array_type()->element_type();
4682 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4683 pf != this->fields_->end();
4686 const Struct_field_list* f = element_type->struct_type()->fields();
4688 Expression_list* fvals = new Expression_list();
4691 Struct_field_list::const_iterator q = f->begin();
4692 go_assert(q->is_field_name("name"));
4693 if (pf->is_anonymous())
4694 fvals->push_back(Expression::make_nil(bloc));
4697 std::string n = Gogo::unpack_hidden_name(pf->field_name());
4698 Expression* s = Expression::make_string(n, bloc);
4699 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4703 go_assert(q->is_field_name("pkgPath"));
4704 if (!Gogo::is_hidden_name(pf->field_name()))
4705 fvals->push_back(Expression::make_nil(bloc));
4708 std::string n = Gogo::hidden_name_pkgpath(pf->field_name());
4709 Expression* s = Expression::make_string(n, bloc);
4710 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4714 go_assert(q->is_field_name("typ"));
4715 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
4718 go_assert(q->is_field_name("tag"));
4720 fvals->push_back(Expression::make_nil(bloc));
4723 Expression* s = Expression::make_string(pf->tag(), bloc);
4724 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4728 go_assert(q->is_field_name("offset"));
4729 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
4731 Expression* v = Expression::make_struct_composite_literal(element_type,
4733 elements->push_back(v);
4736 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
4739 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4742 // Write the hash function for a struct which can not use the identity
4746 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
4747 Function_type* hash_fntype,
4748 Function_type* equal_fntype)
4750 Location bloc = Linemap::predeclared_location();
4752 // The pointer to the struct that we are going to hash. This is an
4753 // argument to the hash function we are implementing here.
4754 Named_object* key_arg = gogo->lookup("key", NULL);
4755 go_assert(key_arg != NULL);
4756 Type* key_arg_type = key_arg->var_value()->type();
4758 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4762 mpz_init_set_ui(ival, 0);
4763 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
4766 // Make a temporary to hold the return value, initialized to 0.
4767 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
4769 gogo->add_statement(retval);
4771 // Make a temporary to hold the key as a uintptr.
4772 Expression* ref = Expression::make_var_reference(key_arg, bloc);
4773 ref = Expression::make_cast(uintptr_type, ref, bloc);
4774 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
4776 gogo->add_statement(key);
4778 // Loop over the struct fields.
4780 const Struct_field_list* fields = this->fields_;
4781 for (Struct_field_list::const_iterator pf = fields->begin();
4782 pf != fields->end();
4785 if (Gogo::is_sink_name(pf->field_name()))
4792 // Multiply retval by 33.
4793 mpz_init_set_ui(ival, 33);
4794 Expression* i33 = Expression::make_integer(&ival, uintptr_type,
4798 ref = Expression::make_temporary_reference(retval, bloc);
4799 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ,
4801 gogo->add_statement(s);
4804 // Get a pointer to the value of this field.
4805 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
4806 ref = Expression::make_temporary_reference(key, bloc);
4807 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
4809 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
4811 // Get the size of this field.
4812 Expression* size = Expression::make_type_info(pf->type(),
4813 Expression::TYPE_INFO_SIZE);
4815 // Get the hash function to use for the type of this field.
4816 Named_object* hash_fn;
4817 Named_object* equal_fn;
4818 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
4819 equal_fntype, &hash_fn, &equal_fn);
4821 // Call the hash function for the field.
4822 Expression_list* args = new Expression_list();
4823 args->push_back(subkey);
4824 args->push_back(size);
4825 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
4826 Expression* call = Expression::make_call(func, args, false, bloc);
4828 // Add the field's hash value to retval.
4829 Temporary_reference_expression* tref =
4830 Expression::make_temporary_reference(retval, bloc);
4831 tref->set_is_lvalue();
4832 Statement* s = Statement::make_assignment_operation(OPERATOR_PLUSEQ,
4834 gogo->add_statement(s);
4837 // Return retval to the caller of the hash function.
4838 Expression_list* vals = new Expression_list();
4839 ref = Expression::make_temporary_reference(retval, bloc);
4840 vals->push_back(ref);
4841 Statement* s = Statement::make_return_statement(vals, bloc);
4842 gogo->add_statement(s);
4845 // Write the equality function for a struct which can not use the
4846 // identity function.
4849 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
4851 Location bloc = Linemap::predeclared_location();
4853 // The pointers to the structs we are going to compare.
4854 Named_object* key1_arg = gogo->lookup("key1", NULL);
4855 Named_object* key2_arg = gogo->lookup("key2", NULL);
4856 go_assert(key1_arg != NULL && key2_arg != NULL);
4858 // Build temporaries with the right types.
4859 Type* pt = Type::make_pointer_type(name != NULL
4860 ? static_cast<Type*>(name)
4861 : static_cast<Type*>(this));
4863 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
4864 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4865 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
4866 gogo->add_statement(p1);
4868 ref = Expression::make_var_reference(key2_arg, bloc);
4869 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4870 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
4871 gogo->add_statement(p2);
4873 const Struct_field_list* fields = this->fields_;
4874 unsigned int field_index = 0;
4875 for (Struct_field_list::const_iterator pf = fields->begin();
4876 pf != fields->end();
4877 ++pf, ++field_index)
4879 if (Gogo::is_sink_name(pf->field_name()))
4882 // Compare one field in both P1 and P2.
4883 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
4884 f1 = Expression::make_unary(OPERATOR_MULT, f1, bloc);
4885 f1 = Expression::make_field_reference(f1, field_index, bloc);
4887 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
4888 f2 = Expression::make_unary(OPERATOR_MULT, f2, bloc);
4889 f2 = Expression::make_field_reference(f2, field_index, bloc);
4891 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
4893 // If the values are not equal, return false.
4894 gogo->start_block(bloc);
4895 Expression_list* vals = new Expression_list();
4896 vals->push_back(Expression::make_boolean(false, bloc));
4897 Statement* s = Statement::make_return_statement(vals, bloc);
4898 gogo->add_statement(s);
4899 Block* then_block = gogo->finish_block(bloc);
4901 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
4902 gogo->add_statement(s);
4905 // All the fields are equal, so return true.
4906 Expression_list* vals = new Expression_list();
4907 vals->push_back(Expression::make_boolean(true, bloc));
4908 Statement* s = Statement::make_return_statement(vals, bloc);
4909 gogo->add_statement(s);
4912 // Reflection string.
4915 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
4917 ret->append("struct { ");
4919 for (Struct_field_list::const_iterator p = this->fields_->begin();
4920 p != this->fields_->end();
4923 if (p != this->fields_->begin())
4925 if (p->is_anonymous())
4926 ret->push_back('?');
4928 ret->append(Gogo::unpack_hidden_name(p->field_name()));
4929 ret->push_back(' ');
4930 this->append_reflection(p->type(), gogo, ret);
4934 const std::string& tag(p->tag());
4936 for (std::string::const_iterator p = tag.begin();
4941 ret->append("\\x00");
4942 else if (*p == '\n')
4944 else if (*p == '\t')
4947 ret->append("\\\"");
4948 else if (*p == '\\')
4949 ret->append("\\\\");
4953 ret->push_back('"');
4963 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4965 ret->push_back('S');
4967 const Struct_field_list* fields = this->fields_;
4970 for (Struct_field_list::const_iterator p = fields->begin();
4974 if (p->is_anonymous())
4978 std::string n = Gogo::unpack_hidden_name(p->field_name());
4980 snprintf(buf, sizeof buf, "%u_",
4981 static_cast<unsigned int>(n.length()));
4985 this->append_mangled_name(p->type(), gogo, ret);
4988 const std::string& tag(p->tag());
4990 for (std::string::const_iterator p = tag.begin();
4994 if (ISALNUM(*p) || *p == '_')
4999 snprintf(buf, sizeof buf, ".%x.",
5000 static_cast<unsigned int>(*p));
5005 snprintf(buf, sizeof buf, "T%u_",
5006 static_cast<unsigned int>(out.length()));
5013 ret->push_back('e');
5016 // If the offset of field INDEX in the backend implementation can be
5017 // determined, set *POFFSET to the offset in bytes and return true.
5018 // Otherwise, return false.
5021 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
5022 unsigned int* poffset)
5024 if (!this->is_backend_type_size_known(gogo))
5026 Btype* bt = this->get_backend_placeholder(gogo);
5027 size_t offset = gogo->backend()->type_field_offset(bt, index);
5028 *poffset = static_cast<unsigned int>(offset);
5029 if (*poffset != offset)
5037 Struct_type::do_export(Export* exp) const
5039 exp->write_c_string("struct { ");
5040 const Struct_field_list* fields = this->fields_;
5041 go_assert(fields != NULL);
5042 for (Struct_field_list::const_iterator p = fields->begin();
5046 if (p->is_anonymous())
5047 exp->write_string("? ");
5050 exp->write_string(p->field_name());
5051 exp->write_c_string(" ");
5053 exp->write_type(p->type());
5057 exp->write_c_string(" ");
5059 Expression::make_string(p->tag(), Linemap::predeclared_location());
5060 expr->export_expression(exp);
5064 exp->write_c_string("; ");
5066 exp->write_c_string("}");
5072 Struct_type::do_import(Import* imp)
5074 imp->require_c_string("struct { ");
5075 Struct_field_list* fields = new Struct_field_list;
5076 if (imp->peek_char() != '}')
5081 if (imp->match_c_string("? "))
5085 name = imp->read_identifier();
5086 imp->require_c_string(" ");
5088 Type* ftype = imp->read_type();
5090 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
5092 if (imp->peek_char() == ' ')
5095 Expression* expr = Expression::import_expression(imp);
5096 String_expression* sexpr = expr->string_expression();
5097 go_assert(sexpr != NULL);
5098 sf.set_tag(sexpr->val());
5102 imp->require_c_string("; ");
5103 fields->push_back(sf);
5104 if (imp->peek_char() == '}')
5108 imp->require_c_string("}");
5110 return Type::make_struct_type(fields, imp->location());
5113 // Make a struct type.
5116 Type::make_struct_type(Struct_field_list* fields,
5119 return new Struct_type(fields, location);
5122 // Class Array_type.
5124 // Whether two array types are identical.
5127 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
5129 if (!Type::are_identical(this->element_type(), t->element_type(),
5130 errors_are_identical, NULL))
5133 Expression* l1 = this->length();
5134 Expression* l2 = t->length();
5136 // Slices of the same element type are identical.
5137 if (l1 == NULL && l2 == NULL)
5140 // Arrays of the same element type are identical if they have the
5142 if (l1 != NULL && l2 != NULL)
5147 // Try to determine the lengths. If we can't, assume the arrays
5148 // are not identical.
5150 Numeric_constant nc1, nc2;
5151 if (l1->numeric_constant_value(&nc1)
5152 && l2->numeric_constant_value(&nc2))
5155 if (nc1.to_int(&v1))
5158 if (nc2.to_int(&v2))
5160 ret = mpz_cmp(v1, v2) == 0;
5169 // Otherwise the arrays are not identical.
5176 Array_type::do_traverse(Traverse* traverse)
5178 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
5179 return TRAVERSE_EXIT;
5180 if (this->length_ != NULL
5181 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
5182 return TRAVERSE_EXIT;
5183 return TRAVERSE_CONTINUE;
5186 // Check that the length is valid.
5189 Array_type::verify_length()
5191 if (this->length_ == NULL)
5194 Type_context context(Type::lookup_integer_type("int"), false);
5195 this->length_->determine_type(&context);
5197 if (!this->length_->is_constant())
5199 error_at(this->length_->location(), "array bound is not constant");
5203 Numeric_constant nc;
5204 if (!this->length_->numeric_constant_value(&nc))
5206 if (this->length_->type()->integer_type() != NULL
5207 || this->length_->type()->float_type() != NULL)
5208 error_at(this->length_->location(), "array bound is not constant");
5210 error_at(this->length_->location(), "array bound is not numeric");
5215 switch (nc.to_unsigned_long(&val))
5217 case Numeric_constant::NC_UL_VALID:
5219 case Numeric_constant::NC_UL_NOTINT:
5220 error_at(this->length_->location(), "array bound truncated to integer");
5222 case Numeric_constant::NC_UL_NEGATIVE:
5223 error_at(this->length_->location(), "negative array bound");
5225 case Numeric_constant::NC_UL_BIG:
5226 error_at(this->length_->location(), "array bound overflows");
5232 Type* int_type = Type::lookup_integer_type("int");
5233 unsigned int tbits = int_type->integer_type()->bits();
5234 if (sizeof(val) <= tbits * 8
5235 && val >> (tbits - 1) != 0)
5237 error_at(this->length_->location(), "array bound overflows");
5247 Array_type::do_verify()
5249 if (!this->verify_length())
5250 this->length_ = Expression::make_error(this->length_->location());
5254 // Whether we can use memcmp to compare this array.
5257 Array_type::do_compare_is_identity(Gogo* gogo) const
5259 if (this->length_ == NULL)
5262 // Check for [...], which indicates that this is not a real type.
5263 if (this->length_->is_nil_expression())
5266 if (!this->element_type_->compare_is_identity(gogo))
5269 // If there is any padding, then we can't use memcmp.
5272 if (!this->element_type_->backend_type_size(gogo, &size)
5273 || !this->element_type_->backend_type_align(gogo, &align))
5275 if ((size & (align - 1)) != 0)
5281 // Array type hash code.
5284 Array_type::do_hash_for_method(Gogo* gogo) const
5286 // There is no very convenient way to get a hash code for the
5288 return this->element_type_->hash_for_method(gogo) + 1;
5291 // Write the hash function for an array which can not use the identify
5295 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
5296 Function_type* hash_fntype,
5297 Function_type* equal_fntype)
5299 Location bloc = Linemap::predeclared_location();
5301 // The pointer to the array that we are going to hash. This is an
5302 // argument to the hash function we are implementing here.
5303 Named_object* key_arg = gogo->lookup("key", NULL);
5304 go_assert(key_arg != NULL);
5305 Type* key_arg_type = key_arg->var_value()->type();
5307 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5311 mpz_init_set_ui(ival, 0);
5312 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
5315 // Make a temporary to hold the return value, initialized to 0.
5316 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
5318 gogo->add_statement(retval);
5320 // Make a temporary to hold the key as a uintptr.
5321 Expression* ref = Expression::make_var_reference(key_arg, bloc);
5322 ref = Expression::make_cast(uintptr_type, ref, bloc);
5323 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
5325 gogo->add_statement(key);
5327 // Loop over the array elements.
5329 Type* int_type = Type::lookup_integer_type("int");
5330 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5331 gogo->add_statement(index);
5333 Expression* iref = Expression::make_temporary_reference(index, bloc);
5334 Expression* aref = Expression::make_var_reference(key_arg, bloc);
5335 Type* pt = Type::make_pointer_type(name != NULL
5336 ? static_cast<Type*>(name)
5337 : static_cast<Type*>(this));
5338 aref = Expression::make_cast(pt, aref, bloc);
5339 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5344 gogo->start_block(bloc);
5346 // Multiply retval by 33.
5347 mpz_init_set_ui(ival, 33);
5348 Expression* i33 = Expression::make_integer(&ival, uintptr_type, bloc);
5351 ref = Expression::make_temporary_reference(retval, bloc);
5352 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ, ref,
5354 gogo->add_statement(s);
5356 // Get the hash function for the element type.
5357 Named_object* hash_fn;
5358 Named_object* equal_fn;
5359 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
5360 hash_fntype, equal_fntype, &hash_fn,
5363 // Get a pointer to this element in the loop.
5364 Expression* subkey = Expression::make_temporary_reference(key, bloc);
5365 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
5367 // Get the size of each element.
5368 Expression* ele_size = Expression::make_type_info(this->element_type_,
5369 Expression::TYPE_INFO_SIZE);
5371 // Get the hash of this element.
5372 Expression_list* args = new Expression_list();
5373 args->push_back(subkey);
5374 args->push_back(ele_size);
5375 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
5376 Expression* call = Expression::make_call(func, args, false, bloc);
5378 // Add the element's hash value to retval.
5379 Temporary_reference_expression* tref =
5380 Expression::make_temporary_reference(retval, bloc);
5381 tref->set_is_lvalue();
5382 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, call, bloc);
5383 gogo->add_statement(s);
5385 // Increase the element pointer.
5386 tref = Expression::make_temporary_reference(key, bloc);
5387 tref->set_is_lvalue();
5388 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
5391 Block* statements = gogo->finish_block(bloc);
5393 for_range->add_statements(statements);
5394 gogo->add_statement(for_range);
5396 // Return retval to the caller of the hash function.
5397 Expression_list* vals = new Expression_list();
5398 ref = Expression::make_temporary_reference(retval, bloc);
5399 vals->push_back(ref);
5400 s = Statement::make_return_statement(vals, bloc);
5401 gogo->add_statement(s);
5404 // Write the equality function for an array which can not use the
5405 // identity function.
5408 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
5410 Location bloc = Linemap::predeclared_location();
5412 // The pointers to the arrays we are going to compare.
5413 Named_object* key1_arg = gogo->lookup("key1", NULL);
5414 Named_object* key2_arg = gogo->lookup("key2", NULL);
5415 go_assert(key1_arg != NULL && key2_arg != NULL);
5417 // Build temporaries for the keys with the right types.
5418 Type* pt = Type::make_pointer_type(name != NULL
5419 ? static_cast<Type*>(name)
5420 : static_cast<Type*>(this));
5422 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
5423 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5424 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
5425 gogo->add_statement(p1);
5427 ref = Expression::make_var_reference(key2_arg, bloc);
5428 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5429 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
5430 gogo->add_statement(p2);
5432 // Loop over the array elements.
5434 Type* int_type = Type::lookup_integer_type("int");
5435 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5436 gogo->add_statement(index);
5438 Expression* iref = Expression::make_temporary_reference(index, bloc);
5439 Expression* aref = Expression::make_temporary_reference(p1, bloc);
5440 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5445 gogo->start_block(bloc);
5447 // Compare element in P1 and P2.
5448 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
5449 e1 = Expression::make_unary(OPERATOR_MULT, e1, bloc);
5450 ref = Expression::make_temporary_reference(index, bloc);
5451 e1 = Expression::make_array_index(e1, ref, NULL, bloc);
5453 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
5454 e2 = Expression::make_unary(OPERATOR_MULT, e2, bloc);
5455 ref = Expression::make_temporary_reference(index, bloc);
5456 e2 = Expression::make_array_index(e2, ref, NULL, bloc);
5458 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
5460 // If the elements are not equal, return false.
5461 gogo->start_block(bloc);
5462 Expression_list* vals = new Expression_list();
5463 vals->push_back(Expression::make_boolean(false, bloc));
5464 Statement* s = Statement::make_return_statement(vals, bloc);
5465 gogo->add_statement(s);
5466 Block* then_block = gogo->finish_block(bloc);
5468 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
5469 gogo->add_statement(s);
5471 Block* statements = gogo->finish_block(bloc);
5473 for_range->add_statements(statements);
5474 gogo->add_statement(for_range);
5476 // All the elements are equal, so return true.
5477 vals = new Expression_list();
5478 vals->push_back(Expression::make_boolean(true, bloc));
5479 s = Statement::make_return_statement(vals, bloc);
5480 gogo->add_statement(s);
5483 // Get a tree for the length of a fixed array. The length may be
5484 // computed using a function call, so we must only evaluate it once.
5487 Array_type::get_length_tree(Gogo* gogo)
5489 go_assert(this->length_ != NULL);
5490 if (this->length_tree_ == NULL_TREE)
5492 Numeric_constant nc;
5494 if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val))
5496 if (mpz_sgn(val) < 0)
5498 this->length_tree_ = error_mark_node;
5499 return this->length_tree_;
5501 Type* t = nc.type();
5503 t = Type::lookup_integer_type("int");
5504 else if (t->is_abstract())
5505 t = t->make_non_abstract_type();
5506 tree tt = type_to_tree(t->get_backend(gogo));
5507 this->length_tree_ = Expression::integer_constant_tree(val, tt);
5512 // Make up a translation context for the array length
5513 // expression. FIXME: This won't work in general.
5514 Translate_context context(gogo, NULL, NULL, NULL);
5515 tree len = this->length_->get_tree(&context);
5516 if (len != error_mark_node)
5518 len = convert_to_integer(integer_type_node, len);
5519 len = save_expr(len);
5521 this->length_tree_ = len;
5524 return this->length_tree_;
5527 // Get the backend representation of the fields of a slice. This is
5528 // not declared in types.h so that types.h doesn't have to #include
5531 // We use int for the count and capacity fields. This matches 6g.
5532 // The language more or less assumes that we can't allocate space of a
5533 // size which does not fit in int.
5536 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
5537 std::vector<Backend::Btyped_identifier>* bfields)
5541 Type* pet = Type::make_pointer_type(type->element_type());
5542 Btype* pbet = (use_placeholder
5543 ? pet->get_backend_placeholder(gogo)
5544 : pet->get_backend(gogo));
5545 Location ploc = Linemap::predeclared_location();
5547 Backend::Btyped_identifier* p = &(*bfields)[0];
5548 p->name = "__values";
5552 Type* int_type = Type::lookup_integer_type("int");
5555 p->name = "__count";
5556 p->btype = int_type->get_backend(gogo);
5560 p->name = "__capacity";
5561 p->btype = int_type->get_backend(gogo);
5565 // Get a tree for the type of this array. A fixed array is simply
5566 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
5567 // just like an array in C. An open array is a struct with three
5568 // fields: a data pointer, the length, and the capacity.
5571 Array_type::do_get_backend(Gogo* gogo)
5573 if (this->length_ == NULL)
5575 std::vector<Backend::Btyped_identifier> bfields;
5576 get_backend_slice_fields(gogo, this, false, &bfields);
5577 return gogo->backend()->struct_type(bfields);
5581 Btype* element = this->get_backend_element(gogo, false);
5582 Bexpression* len = this->get_backend_length(gogo);
5583 return gogo->backend()->array_type(element, len);
5587 // Return the backend representation of the element type.
5590 Array_type::get_backend_element(Gogo* gogo, bool use_placeholder)
5592 if (use_placeholder)
5593 return this->element_type_->get_backend_placeholder(gogo);
5595 return this->element_type_->get_backend(gogo);
5598 // Return the backend representation of the length.
5601 Array_type::get_backend_length(Gogo* gogo)
5603 return tree_to_expr(this->get_length_tree(gogo));
5606 // Finish backend representation of the array.
5609 Array_type::finish_backend_element(Gogo* gogo)
5611 Type* et = this->array_type()->element_type();
5612 et->get_backend(gogo);
5613 if (this->is_slice_type())
5615 // This relies on the fact that we always use the same
5616 // structure for a pointer to any given type.
5617 Type* pet = Type::make_pointer_type(et);
5618 pet->get_backend(gogo);
5622 // Return a tree for a pointer to the values in ARRAY.
5625 Array_type::value_pointer_tree(Gogo*, tree array) const
5628 if (this->length() != NULL)
5631 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
5632 build_fold_addr_expr(array));
5637 tree field = TYPE_FIELDS(TREE_TYPE(array));
5638 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
5640 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
5643 if (TREE_CONSTANT(array))
5644 TREE_CONSTANT(ret) = 1;
5648 // Return a tree for the length of the array ARRAY which has this
5652 Array_type::length_tree(Gogo* gogo, tree array)
5654 if (this->length_ != NULL)
5656 if (TREE_CODE(array) == SAVE_EXPR)
5657 return fold_convert(integer_type_node, this->get_length_tree(gogo));
5659 return omit_one_operand(integer_type_node,
5660 this->get_length_tree(gogo), array);
5663 // This is an open array. We need to read the length field.
5665 tree type = TREE_TYPE(array);
5666 go_assert(TREE_CODE(type) == RECORD_TYPE);
5668 tree field = DECL_CHAIN(TYPE_FIELDS(type));
5669 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
5671 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5672 if (TREE_CONSTANT(array))
5673 TREE_CONSTANT(ret) = 1;
5677 // Return a tree for the capacity of the array ARRAY which has this
5681 Array_type::capacity_tree(Gogo* gogo, tree array)
5683 if (this->length_ != NULL)
5684 return omit_one_operand(integer_type_node, this->get_length_tree(gogo),
5687 // This is an open array. We need to read the capacity field.
5689 tree type = TREE_TYPE(array);
5690 go_assert(TREE_CODE(type) == RECORD_TYPE);
5692 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
5693 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
5695 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5701 Array_type::do_export(Export* exp) const
5703 exp->write_c_string("[");
5704 if (this->length_ != NULL)
5705 this->length_->export_expression(exp);
5706 exp->write_c_string("] ");
5707 exp->write_type(this->element_type_);
5713 Array_type::do_import(Import* imp)
5715 imp->require_c_string("[");
5717 if (imp->peek_char() == ']')
5720 length = Expression::import_expression(imp);
5721 imp->require_c_string("] ");
5722 Type* element_type = imp->read_type();
5723 return Type::make_array_type(element_type, length);
5726 // The type of an array type descriptor.
5729 Array_type::make_array_type_descriptor_type()
5734 Type* tdt = Type::make_type_descriptor_type();
5735 Type* ptdt = Type::make_type_descriptor_ptr_type();
5737 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5740 Type::make_builtin_struct_type(4,
5744 "len", uintptr_type);
5746 ret = Type::make_builtin_named_type("ArrayType", sf);
5752 // The type of an slice type descriptor.
5755 Array_type::make_slice_type_descriptor_type()
5760 Type* tdt = Type::make_type_descriptor_type();
5761 Type* ptdt = Type::make_type_descriptor_ptr_type();
5764 Type::make_builtin_struct_type(2,
5768 ret = Type::make_builtin_named_type("SliceType", sf);
5774 // Build a type descriptor for an array/slice type.
5777 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5779 if (this->length_ != NULL)
5780 return this->array_type_descriptor(gogo, name);
5782 return this->slice_type_descriptor(gogo, name);
5785 // Build a type descriptor for an array type.
5788 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
5790 Location bloc = Linemap::predeclared_location();
5792 Type* atdt = Array_type::make_array_type_descriptor_type();
5794 const Struct_field_list* fields = atdt->struct_type()->fields();
5796 Expression_list* vals = new Expression_list();
5799 Struct_field_list::const_iterator p = fields->begin();
5800 go_assert(p->is_field_name("commonType"));
5801 vals->push_back(this->type_descriptor_constructor(gogo,
5802 RUNTIME_TYPE_KIND_ARRAY,
5806 go_assert(p->is_field_name("elem"));
5807 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5810 go_assert(p->is_field_name("slice"));
5811 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
5812 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
5815 go_assert(p->is_field_name("len"));
5816 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
5819 go_assert(p == fields->end());
5821 return Expression::make_struct_composite_literal(atdt, vals, bloc);
5824 // Build a type descriptor for a slice type.
5827 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
5829 Location bloc = Linemap::predeclared_location();
5831 Type* stdt = Array_type::make_slice_type_descriptor_type();
5833 const Struct_field_list* fields = stdt->struct_type()->fields();
5835 Expression_list* vals = new Expression_list();
5838 Struct_field_list::const_iterator p = fields->begin();
5839 go_assert(p->is_field_name("commonType"));
5840 vals->push_back(this->type_descriptor_constructor(gogo,
5841 RUNTIME_TYPE_KIND_SLICE,
5845 go_assert(p->is_field_name("elem"));
5846 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5849 go_assert(p == fields->end());
5851 return Expression::make_struct_composite_literal(stdt, vals, bloc);
5854 // Reflection string.
5857 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
5859 ret->push_back('[');
5860 if (this->length_ != NULL)
5862 Numeric_constant nc;
5864 if (!this->length_->numeric_constant_value(&nc)
5865 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
5866 error_at(this->length_->location(), "invalid array length");
5870 snprintf(buf, sizeof buf, "%lu", val);
5874 ret->push_back(']');
5876 this->append_reflection(this->element_type_, gogo, ret);
5882 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5884 ret->push_back('A');
5885 this->append_mangled_name(this->element_type_, gogo, ret);
5886 if (this->length_ != NULL)
5888 Numeric_constant nc;
5890 if (!this->length_->numeric_constant_value(&nc)
5891 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
5892 error_at(this->length_->location(), "invalid array length");
5896 snprintf(buf, sizeof buf, "%lu", val);
5900 ret->push_back('e');
5903 // Make an array type.
5906 Type::make_array_type(Type* element_type, Expression* length)
5908 return new Array_type(element_type, length);
5916 Map_type::do_traverse(Traverse* traverse)
5918 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
5919 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
5920 return TRAVERSE_EXIT;
5921 return TRAVERSE_CONTINUE;
5924 // Check that the map type is OK.
5927 Map_type::do_verify()
5929 // The runtime support uses "map[void]void".
5930 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
5931 error_at(this->location_, "invalid map key type");
5935 // Whether two map types are identical.
5938 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
5940 return (Type::are_identical(this->key_type(), t->key_type(),
5941 errors_are_identical, NULL)
5942 && Type::are_identical(this->val_type(), t->val_type(),
5943 errors_are_identical, NULL));
5949 Map_type::do_hash_for_method(Gogo* gogo) const
5951 return (this->key_type_->hash_for_method(gogo)
5952 + this->val_type_->hash_for_method(gogo)
5956 // Get the backend representation for a map type. A map type is
5957 // represented as a pointer to a struct. The struct is __go_map in
5961 Map_type::do_get_backend(Gogo* gogo)
5963 static Btype* backend_map_type;
5964 if (backend_map_type == NULL)
5966 std::vector<Backend::Btyped_identifier> bfields(4);
5968 Location bloc = Linemap::predeclared_location();
5970 Type* pdt = Type::make_type_descriptor_ptr_type();
5971 bfields[0].name = "__descriptor";
5972 bfields[0].btype = pdt->get_backend(gogo);
5973 bfields[0].location = bloc;
5975 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5976 bfields[1].name = "__element_count";
5977 bfields[1].btype = uintptr_type->get_backend(gogo);
5978 bfields[1].location = bloc;
5980 bfields[2].name = "__bucket_count";
5981 bfields[2].btype = bfields[1].btype;
5982 bfields[2].location = bloc;
5984 Btype* bvt = gogo->backend()->void_type();
5985 Btype* bpvt = gogo->backend()->pointer_type(bvt);
5986 Btype* bppvt = gogo->backend()->pointer_type(bpvt);
5987 bfields[3].name = "__buckets";
5988 bfields[3].btype = bppvt;
5989 bfields[3].location = bloc;
5991 Btype *bt = gogo->backend()->struct_type(bfields);
5992 bt = gogo->backend()->named_type("__go_map", bt, bloc);
5993 backend_map_type = gogo->backend()->pointer_type(bt);
5995 return backend_map_type;
5998 // The type of a map type descriptor.
6001 Map_type::make_map_type_descriptor_type()
6006 Type* tdt = Type::make_type_descriptor_type();
6007 Type* ptdt = Type::make_type_descriptor_ptr_type();
6010 Type::make_builtin_struct_type(3,
6015 ret = Type::make_builtin_named_type("MapType", sf);
6021 // Build a type descriptor for a map type.
6024 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6026 Location bloc = Linemap::predeclared_location();
6028 Type* mtdt = Map_type::make_map_type_descriptor_type();
6030 const Struct_field_list* fields = mtdt->struct_type()->fields();
6032 Expression_list* vals = new Expression_list();
6035 Struct_field_list::const_iterator p = fields->begin();
6036 go_assert(p->is_field_name("commonType"));
6037 vals->push_back(this->type_descriptor_constructor(gogo,
6038 RUNTIME_TYPE_KIND_MAP,
6042 go_assert(p->is_field_name("key"));
6043 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
6046 go_assert(p->is_field_name("elem"));
6047 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
6050 go_assert(p == fields->end());
6052 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
6055 // A mapping from map types to map descriptors.
6057 Map_type::Map_descriptors Map_type::map_descriptors;
6059 // Build a map descriptor for this type. Return a pointer to it.
6062 Map_type::map_descriptor_pointer(Gogo* gogo, Location location)
6064 Bvariable* bvar = this->map_descriptor(gogo);
6065 tree var_tree = var_to_tree(bvar);
6066 if (var_tree == error_mark_node)
6067 return error_mark_node;
6068 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
6071 // Build a map descriptor for this type.
6074 Map_type::map_descriptor(Gogo* gogo)
6076 std::pair<Map_type*, Bvariable*> val(this, NULL);
6077 std::pair<Map_type::Map_descriptors::iterator, bool> ins =
6078 Map_type::map_descriptors.insert(val);
6080 return ins.first->second;
6082 Type* key_type = this->key_type_;
6083 Type* val_type = this->val_type_;
6085 // The map entry type is a struct with three fields. Build that
6086 // struct so that we can get the offsets of the key and value within
6087 // a map entry. The first field should technically be a pointer to
6088 // this type itself, but since we only care about field offsets we
6089 // just use pointer to bool.
6090 Type* pbool = Type::make_pointer_type(Type::make_boolean_type());
6091 Struct_type* map_entry_type =
6092 Type::make_builtin_struct_type(3,
6097 Type* map_descriptor_type = Map_type::make_map_descriptor_type();
6099 const Struct_field_list* fields =
6100 map_descriptor_type->struct_type()->fields();
6102 Expression_list* vals = new Expression_list();
6105 Location bloc = Linemap::predeclared_location();
6107 Struct_field_list::const_iterator p = fields->begin();
6109 go_assert(p->is_field_name("__map_descriptor"));
6110 vals->push_back(Expression::make_type_descriptor(this, bloc));
6113 go_assert(p->is_field_name("__entry_size"));
6114 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
6115 vals->push_back(Expression::make_type_info(map_entry_type, type_info));
6117 Struct_field_list::const_iterator pf = map_entry_type->fields()->begin();
6119 go_assert(pf->is_field_name("__key"));
6122 go_assert(p->is_field_name("__key_offset"));
6123 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6126 go_assert(pf->is_field_name("__val"));
6129 go_assert(p->is_field_name("__val_offset"));
6130 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6133 go_assert(p == fields->end());
6135 Expression* initializer =
6136 Expression::make_struct_composite_literal(map_descriptor_type, vals, bloc);
6138 std::string mangled_name = "__go_map_" + this->mangled_name(gogo);
6139 Btype* map_descriptor_btype = map_descriptor_type->get_backend(gogo);
6140 Bvariable* bvar = gogo->backend()->immutable_struct(mangled_name, true,
6141 map_descriptor_btype,
6144 Translate_context context(gogo, NULL, NULL, NULL);
6145 context.set_is_const();
6146 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
6148 gogo->backend()->immutable_struct_set_init(bvar, mangled_name, true,
6149 map_descriptor_btype, bloc,
6152 ins.first->second = bvar;
6156 // Build the type of a map descriptor. This must match the struct
6157 // __go_map_descriptor in libgo/runtime/map.h.
6160 Map_type::make_map_descriptor_type()
6165 Type* ptdt = Type::make_type_descriptor_ptr_type();
6166 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6168 Type::make_builtin_struct_type(4,
6169 "__map_descriptor", ptdt,
6170 "__entry_size", uintptr_type,
6171 "__key_offset", uintptr_type,
6172 "__val_offset", uintptr_type);
6173 ret = Type::make_builtin_named_type("__go_map_descriptor", sf);
6178 // Reflection string for a map.
6181 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
6183 ret->append("map[");
6184 this->append_reflection(this->key_type_, gogo, ret);
6186 this->append_reflection(this->val_type_, gogo, ret);
6189 // Mangled name for a map.
6192 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6194 ret->push_back('M');
6195 this->append_mangled_name(this->key_type_, gogo, ret);
6197 this->append_mangled_name(this->val_type_, gogo, ret);
6200 // Export a map type.
6203 Map_type::do_export(Export* exp) const
6205 exp->write_c_string("map [");
6206 exp->write_type(this->key_type_);
6207 exp->write_c_string("] ");
6208 exp->write_type(this->val_type_);
6211 // Import a map type.
6214 Map_type::do_import(Import* imp)
6216 imp->require_c_string("map [");
6217 Type* key_type = imp->read_type();
6218 imp->require_c_string("] ");
6219 Type* val_type = imp->read_type();
6220 return Type::make_map_type(key_type, val_type, imp->location());
6226 Type::make_map_type(Type* key_type, Type* val_type, Location location)
6228 return new Map_type(key_type, val_type, location);
6231 // Class Channel_type.
6236 Channel_type::do_hash_for_method(Gogo* gogo) const
6238 unsigned int ret = 0;
6239 if (this->may_send_)
6241 if (this->may_receive_)
6243 if (this->element_type_ != NULL)
6244 ret += this->element_type_->hash_for_method(gogo) << 2;
6248 // Whether this type is the same as T.
6251 Channel_type::is_identical(const Channel_type* t,
6252 bool errors_are_identical) const
6254 if (!Type::are_identical(this->element_type(), t->element_type(),
6255 errors_are_identical, NULL))
6257 return (this->may_send_ == t->may_send_
6258 && this->may_receive_ == t->may_receive_);
6261 // Return the tree for a channel type. A channel is a pointer to a
6262 // __go_channel struct. The __go_channel struct is defined in
6263 // libgo/runtime/channel.h.
6266 Channel_type::do_get_backend(Gogo* gogo)
6268 static Btype* backend_channel_type;
6269 if (backend_channel_type == NULL)
6271 std::vector<Backend::Btyped_identifier> bfields;
6272 Btype* bt = gogo->backend()->struct_type(bfields);
6273 bt = gogo->backend()->named_type("__go_channel", bt,
6274 Linemap::predeclared_location());
6275 backend_channel_type = gogo->backend()->pointer_type(bt);
6277 return backend_channel_type;
6280 // Build a type descriptor for a channel type.
6283 Channel_type::make_chan_type_descriptor_type()
6288 Type* tdt = Type::make_type_descriptor_type();
6289 Type* ptdt = Type::make_type_descriptor_ptr_type();
6291 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6294 Type::make_builtin_struct_type(3,
6297 "dir", uintptr_type);
6299 ret = Type::make_builtin_named_type("ChanType", sf);
6305 // Build a type descriptor for a map type.
6308 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6310 Location bloc = Linemap::predeclared_location();
6312 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
6314 const Struct_field_list* fields = ctdt->struct_type()->fields();
6316 Expression_list* vals = new Expression_list();
6319 Struct_field_list::const_iterator p = fields->begin();
6320 go_assert(p->is_field_name("commonType"));
6321 vals->push_back(this->type_descriptor_constructor(gogo,
6322 RUNTIME_TYPE_KIND_CHAN,
6326 go_assert(p->is_field_name("elem"));
6327 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6330 go_assert(p->is_field_name("dir"));
6331 // These bits must match the ones in libgo/runtime/go-type.h.
6333 if (this->may_receive_)
6335 if (this->may_send_)
6338 mpz_init_set_ui(iv, val);
6339 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
6343 go_assert(p == fields->end());
6345 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
6348 // Reflection string.
6351 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
6353 if (!this->may_send_)
6355 ret->append("chan");
6356 if (!this->may_receive_)
6358 ret->push_back(' ');
6359 this->append_reflection(this->element_type_, gogo, ret);
6365 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6367 ret->push_back('C');
6368 this->append_mangled_name(this->element_type_, gogo, ret);
6369 if (this->may_send_)
6370 ret->push_back('s');
6371 if (this->may_receive_)
6372 ret->push_back('r');
6373 ret->push_back('e');
6379 Channel_type::do_export(Export* exp) const
6381 exp->write_c_string("chan ");
6382 if (this->may_send_ && !this->may_receive_)
6383 exp->write_c_string("-< ");
6384 else if (this->may_receive_ && !this->may_send_)
6385 exp->write_c_string("<- ");
6386 exp->write_type(this->element_type_);
6392 Channel_type::do_import(Import* imp)
6394 imp->require_c_string("chan ");
6398 if (imp->match_c_string("-< "))
6402 may_receive = false;
6404 else if (imp->match_c_string("<- "))
6416 Type* element_type = imp->read_type();
6418 return Type::make_channel_type(may_send, may_receive, element_type);
6421 // Make a new channel type.
6424 Type::make_channel_type(bool send, bool receive, Type* element_type)
6426 return new Channel_type(send, receive, element_type);
6429 // Class Interface_type.
6434 Interface_type::do_traverse(Traverse* traverse)
6436 Typed_identifier_list* methods = (this->methods_are_finalized_
6437 ? this->all_methods_
6438 : this->parse_methods_);
6439 if (methods == NULL)
6440 return TRAVERSE_CONTINUE;
6441 return methods->traverse(traverse);
6444 // Finalize the methods. This handles interface inheritance.
6447 Interface_type::finalize_methods()
6449 if (this->methods_are_finalized_)
6451 this->methods_are_finalized_ = true;
6452 if (this->parse_methods_ == NULL)
6455 this->all_methods_ = new Typed_identifier_list();
6456 this->all_methods_->reserve(this->parse_methods_->size());
6457 Typed_identifier_list inherit;
6458 for (Typed_identifier_list::const_iterator pm =
6459 this->parse_methods_->begin();
6460 pm != this->parse_methods_->end();
6463 const Typed_identifier* p = &*pm;
6464 if (p->name().empty())
6465 inherit.push_back(*p);
6466 else if (this->find_method(p->name()) == NULL)
6467 this->all_methods_->push_back(*p);
6469 error_at(p->location(), "duplicate method %qs",
6470 Gogo::message_name(p->name()).c_str());
6473 std::vector<Named_type*> seen;
6474 seen.reserve(inherit.size());
6475 bool issued_recursive_error = false;
6476 while (!inherit.empty())
6478 Type* t = inherit.back().type();
6479 Location tl = inherit.back().location();
6482 Interface_type* it = t->interface_type();
6486 error_at(tl, "interface contains embedded non-interface");
6491 if (!issued_recursive_error)
6493 error_at(tl, "invalid recursive interface");
6494 issued_recursive_error = true;
6499 Named_type* nt = t->named_type();
6500 if (nt != NULL && it->parse_methods_ != NULL)
6502 std::vector<Named_type*>::const_iterator q;
6503 for (q = seen.begin(); q != seen.end(); ++q)
6507 error_at(tl, "inherited interface loop");
6511 if (q != seen.end())
6516 const Typed_identifier_list* imethods = it->parse_methods_;
6517 if (imethods == NULL)
6519 for (Typed_identifier_list::const_iterator q = imethods->begin();
6520 q != imethods->end();
6523 if (q->name().empty())
6524 inherit.push_back(*q);
6525 else if (this->find_method(q->name()) == NULL)
6526 this->all_methods_->push_back(Typed_identifier(q->name(),
6529 error_at(tl, "inherited method %qs is ambiguous",
6530 Gogo::message_name(q->name()).c_str());
6534 if (!this->all_methods_->empty())
6535 this->all_methods_->sort_by_name();
6538 delete this->all_methods_;
6539 this->all_methods_ = NULL;
6543 // Return the method NAME, or NULL.
6545 const Typed_identifier*
6546 Interface_type::find_method(const std::string& name) const
6548 go_assert(this->methods_are_finalized_);
6549 if (this->all_methods_ == NULL)
6551 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6552 p != this->all_methods_->end();
6554 if (p->name() == name)
6559 // Return the method index.
6562 Interface_type::method_index(const std::string& name) const
6564 go_assert(this->methods_are_finalized_ && this->all_methods_ != NULL);
6566 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6567 p != this->all_methods_->end();
6569 if (p->name() == name)
6574 // Return whether NAME is an unexported method, for better error
6578 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
6580 go_assert(this->methods_are_finalized_);
6581 if (this->all_methods_ == NULL)
6583 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6584 p != this->all_methods_->end();
6587 const std::string& method_name(p->name());
6588 if (Gogo::is_hidden_name(method_name)
6589 && name == Gogo::unpack_hidden_name(method_name)
6590 && gogo->pack_hidden_name(name, false) != method_name)
6596 // Whether this type is identical with T.
6599 Interface_type::is_identical(const Interface_type* t,
6600 bool errors_are_identical) const
6602 // If methods have not been finalized, then we are asking whether
6603 // func redeclarations are the same. This is an error, so for
6604 // simplicity we say they are never the same.
6605 if (!this->methods_are_finalized_ || !t->methods_are_finalized_)
6608 // We require the same methods with the same types. The methods
6609 // have already been sorted.
6610 if (this->all_methods_ == NULL || t->all_methods_ == NULL)
6611 return this->all_methods_ == t->all_methods_;
6613 if (this->assume_identical(this, t) || t->assume_identical(t, this))
6616 Assume_identical* hold_ai = this->assume_identical_;
6617 Assume_identical ai;
6621 this->assume_identical_ = &ai;
6623 Typed_identifier_list::const_iterator p1 = this->all_methods_->begin();
6624 Typed_identifier_list::const_iterator p2;
6625 for (p2 = t->all_methods_->begin(); p2 != t->all_methods_->end(); ++p1, ++p2)
6627 if (p1 == this->all_methods_->end())
6629 if (p1->name() != p2->name()
6630 || !Type::are_identical(p1->type(), p2->type(),
6631 errors_are_identical, NULL))
6635 this->assume_identical_ = hold_ai;
6637 return p1 == this->all_methods_->end() && p2 == t->all_methods_->end();
6640 // Return true if T1 and T2 are assumed to be identical during a type
6644 Interface_type::assume_identical(const Interface_type* t1,
6645 const Interface_type* t2) const
6647 for (Assume_identical* p = this->assume_identical_;
6650 if ((p->t1 == t1 && p->t2 == t2) || (p->t1 == t2 && p->t2 == t1))
6655 // Whether we can assign the interface type T to this type. The types
6656 // are known to not be identical. An interface assignment is only
6657 // permitted if T is known to implement all methods in THIS.
6658 // Otherwise a type guard is required.
6661 Interface_type::is_compatible_for_assign(const Interface_type* t,
6662 std::string* reason) const
6664 go_assert(this->methods_are_finalized_ && t->methods_are_finalized_);
6665 if (this->all_methods_ == NULL)
6667 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6668 p != this->all_methods_->end();
6671 const Typed_identifier* m = t->find_method(p->name());
6677 snprintf(buf, sizeof buf,
6678 _("need explicit conversion; missing method %s%s%s"),
6679 open_quote, Gogo::message_name(p->name()).c_str(),
6681 reason->assign(buf);
6686 std::string subreason;
6687 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
6691 std::string n = Gogo::message_name(p->name());
6692 size_t len = 100 + n.length() + subreason.length();
6693 char* buf = new char[len];
6694 if (subreason.empty())
6695 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6696 open_quote, n.c_str(), close_quote);
6699 _("incompatible type for method %s%s%s (%s)"),
6700 open_quote, n.c_str(), close_quote,
6702 reason->assign(buf);
6715 Interface_type::do_hash_for_method(Gogo*) const
6717 go_assert(this->methods_are_finalized_);
6718 unsigned int ret = 0;
6719 if (this->all_methods_ != NULL)
6721 for (Typed_identifier_list::const_iterator p =
6722 this->all_methods_->begin();
6723 p != this->all_methods_->end();
6726 ret = Type::hash_string(p->name(), ret);
6727 // We don't use the method type in the hash, to avoid
6728 // infinite recursion if an interface method uses a type
6729 // which is an interface which inherits from the interface
6731 // type T interface { F() interface {T}}
6738 // Return true if T implements the interface. If it does not, and
6739 // REASON is not NULL, set *REASON to a useful error message.
6742 Interface_type::implements_interface(const Type* t, std::string* reason) const
6744 go_assert(this->methods_are_finalized_);
6745 if (this->all_methods_ == NULL)
6748 bool is_pointer = false;
6749 const Named_type* nt = t->named_type();
6750 const Struct_type* st = t->struct_type();
6751 // If we start with a named type, we don't dereference it to find
6755 const Type* pt = t->points_to();
6758 // If T is a pointer to a named type, then we need to look at
6759 // the type to which it points.
6761 nt = pt->named_type();
6762 st = pt->struct_type();
6766 // If we have a named type, get the methods from it rather than from
6771 // Only named and struct types have methods.
6772 if (nt == NULL && st == NULL)
6776 if (t->points_to() != NULL
6777 && t->points_to()->interface_type() != NULL)
6778 reason->assign(_("pointer to interface type has no methods"));
6780 reason->assign(_("type has no methods"));
6785 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
6789 if (t->points_to() != NULL
6790 && t->points_to()->interface_type() != NULL)
6791 reason->assign(_("pointer to interface type has no methods"));
6793 reason->assign(_("type has no methods"));
6798 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6799 p != this->all_methods_->end();
6802 bool is_ambiguous = false;
6803 Method* m = (nt != NULL
6804 ? nt->method_function(p->name(), &is_ambiguous)
6805 : st->method_function(p->name(), &is_ambiguous));
6810 std::string n = Gogo::message_name(p->name());
6811 size_t len = n.length() + 100;
6812 char* buf = new char[len];
6814 snprintf(buf, len, _("ambiguous method %s%s%s"),
6815 open_quote, n.c_str(), close_quote);
6817 snprintf(buf, len, _("missing method %s%s%s"),
6818 open_quote, n.c_str(), close_quote);
6819 reason->assign(buf);
6825 Function_type *p_fn_type = p->type()->function_type();
6826 Function_type* m_fn_type = m->type()->function_type();
6827 go_assert(p_fn_type != NULL && m_fn_type != NULL);
6828 std::string subreason;
6829 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
6833 std::string n = Gogo::message_name(p->name());
6834 size_t len = 100 + n.length() + subreason.length();
6835 char* buf = new char[len];
6836 if (subreason.empty())
6837 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6838 open_quote, n.c_str(), close_quote);
6841 _("incompatible type for method %s%s%s (%s)"),
6842 open_quote, n.c_str(), close_quote,
6844 reason->assign(buf);
6850 if (!is_pointer && !m->is_value_method())
6854 std::string n = Gogo::message_name(p->name());
6855 size_t len = 100 + n.length();
6856 char* buf = new char[len];
6858 _("method %s%s%s requires a pointer receiver"),
6859 open_quote, n.c_str(), close_quote);
6860 reason->assign(buf);
6870 // Return the backend representation of the empty interface type. We
6871 // use the same struct for all empty interfaces.
6874 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
6876 static Btype* empty_interface_type;
6877 if (empty_interface_type == NULL)
6879 std::vector<Backend::Btyped_identifier> bfields(2);
6881 Location bloc = Linemap::predeclared_location();
6883 Type* pdt = Type::make_type_descriptor_ptr_type();
6884 bfields[0].name = "__type_descriptor";
6885 bfields[0].btype = pdt->get_backend(gogo);
6886 bfields[0].location = bloc;
6888 Type* vt = Type::make_pointer_type(Type::make_void_type());
6889 bfields[1].name = "__object";
6890 bfields[1].btype = vt->get_backend(gogo);
6891 bfields[1].location = bloc;
6893 empty_interface_type = gogo->backend()->struct_type(bfields);
6895 return empty_interface_type;
6898 // Return the fields of a non-empty interface type. This is not
6899 // declared in types.h so that types.h doesn't have to #include
6903 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
6904 bool use_placeholder,
6905 std::vector<Backend::Btyped_identifier>* bfields)
6907 Location loc = type->location();
6909 std::vector<Backend::Btyped_identifier> mfields(type->methods()->size() + 1);
6911 Type* pdt = Type::make_type_descriptor_ptr_type();
6912 mfields[0].name = "__type_descriptor";
6913 mfields[0].btype = pdt->get_backend(gogo);
6914 mfields[0].location = loc;
6916 std::string last_name = "";
6918 for (Typed_identifier_list::const_iterator p = type->methods()->begin();
6919 p != type->methods()->end();
6922 // The type of the method in Go only includes the parameters.
6923 // The actual method also has a receiver, which is always a
6924 // pointer. We need to add that pointer type here in order to
6925 // generate the correct type for the backend.
6926 Function_type* ft = p->type()->function_type();
6927 go_assert(ft->receiver() == NULL);
6929 const Typed_identifier_list* params = ft->parameters();
6930 Typed_identifier_list* mparams = new Typed_identifier_list();
6932 mparams->reserve(params->size() + 1);
6933 Type* vt = Type::make_pointer_type(Type::make_void_type());
6934 mparams->push_back(Typed_identifier("", vt, ft->location()));
6937 for (Typed_identifier_list::const_iterator pp = params->begin();
6938 pp != params->end();
6940 mparams->push_back(*pp);
6943 Typed_identifier_list* mresults = (ft->results() == NULL
6945 : ft->results()->copy());
6946 Function_type* mft = Type::make_function_type(NULL, mparams, mresults,
6949 mfields[i].name = Gogo::unpack_hidden_name(p->name());
6950 mfields[i].btype = (use_placeholder
6951 ? mft->get_backend_placeholder(gogo)
6952 : mft->get_backend(gogo));
6953 mfields[i].location = loc;
6954 // Sanity check: the names should be sorted.
6955 go_assert(p->name() > last_name);
6956 last_name = p->name();
6959 Btype* methods = gogo->backend()->struct_type(mfields);
6963 (*bfields)[0].name = "__methods";
6964 (*bfields)[0].btype = gogo->backend()->pointer_type(methods);
6965 (*bfields)[0].location = loc;
6967 Type* vt = Type::make_pointer_type(Type::make_void_type());
6968 (*bfields)[1].name = "__object";
6969 (*bfields)[1].btype = vt->get_backend(gogo);
6970 (*bfields)[1].location = Linemap::predeclared_location();
6973 // Return a tree for an interface type. An interface is a pointer to
6974 // a struct. The struct has three fields. The first field is a
6975 // pointer to the type descriptor for the dynamic type of the object.
6976 // The second field is a pointer to a table of methods for the
6977 // interface to be used with the object. The third field is the value
6978 // of the object itself.
6981 Interface_type::do_get_backend(Gogo* gogo)
6983 if (this->is_empty())
6984 return Interface_type::get_backend_empty_interface_type(gogo);
6987 if (this->interface_btype_ != NULL)
6988 return this->interface_btype_;
6989 this->interface_btype_ =
6990 gogo->backend()->placeholder_struct_type("", this->location_);
6991 std::vector<Backend::Btyped_identifier> bfields;
6992 get_backend_interface_fields(gogo, this, false, &bfields);
6993 if (!gogo->backend()->set_placeholder_struct_type(this->interface_btype_,
6995 this->interface_btype_ = gogo->backend()->error_type();
6996 return this->interface_btype_;
7000 // Finish the backend representation of the methods.
7003 Interface_type::finish_backend_methods(Gogo* gogo)
7005 if (!this->interface_type()->is_empty())
7007 const Typed_identifier_list* methods = this->methods();
7008 if (methods != NULL)
7010 for (Typed_identifier_list::const_iterator p = methods->begin();
7011 p != methods->end();
7013 p->type()->get_backend(gogo);
7018 // The type of an interface type descriptor.
7021 Interface_type::make_interface_type_descriptor_type()
7026 Type* tdt = Type::make_type_descriptor_type();
7027 Type* ptdt = Type::make_type_descriptor_ptr_type();
7029 Type* string_type = Type::lookup_string_type();
7030 Type* pointer_string_type = Type::make_pointer_type(string_type);
7033 Type::make_builtin_struct_type(3,
7034 "name", pointer_string_type,
7035 "pkgPath", pointer_string_type,
7038 Type* nsm = Type::make_builtin_named_type("imethod", sm);
7040 Type* slice_nsm = Type::make_array_type(nsm, NULL);
7042 Struct_type* s = Type::make_builtin_struct_type(2,
7044 "methods", slice_nsm);
7046 ret = Type::make_builtin_named_type("InterfaceType", s);
7052 // Build a type descriptor for an interface type.
7055 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7057 Location bloc = Linemap::predeclared_location();
7059 Type* itdt = Interface_type::make_interface_type_descriptor_type();
7061 const Struct_field_list* ifields = itdt->struct_type()->fields();
7063 Expression_list* ivals = new Expression_list();
7066 Struct_field_list::const_iterator pif = ifields->begin();
7067 go_assert(pif->is_field_name("commonType"));
7068 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
7069 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
7073 go_assert(pif->is_field_name("methods"));
7075 Expression_list* methods = new Expression_list();
7076 if (this->all_methods_ != NULL)
7078 Type* elemtype = pif->type()->array_type()->element_type();
7080 methods->reserve(this->all_methods_->size());
7081 for (Typed_identifier_list::const_iterator pm =
7082 this->all_methods_->begin();
7083 pm != this->all_methods_->end();
7086 const Struct_field_list* mfields = elemtype->struct_type()->fields();
7088 Expression_list* mvals = new Expression_list();
7091 Struct_field_list::const_iterator pmf = mfields->begin();
7092 go_assert(pmf->is_field_name("name"));
7093 std::string s = Gogo::unpack_hidden_name(pm->name());
7094 Expression* e = Expression::make_string(s, bloc);
7095 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7098 go_assert(pmf->is_field_name("pkgPath"));
7099 if (!Gogo::is_hidden_name(pm->name()))
7100 mvals->push_back(Expression::make_nil(bloc));
7103 s = Gogo::hidden_name_pkgpath(pm->name());
7104 e = Expression::make_string(s, bloc);
7105 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7109 go_assert(pmf->is_field_name("typ"));
7110 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
7113 go_assert(pmf == mfields->end());
7115 e = Expression::make_struct_composite_literal(elemtype, mvals,
7117 methods->push_back(e);
7121 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
7125 go_assert(pif == ifields->end());
7127 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
7130 // Reflection string.
7133 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
7135 ret->append("interface {");
7136 const Typed_identifier_list* methods = this->parse_methods_;
7137 if (methods != NULL)
7139 ret->push_back(' ');
7140 for (Typed_identifier_list::const_iterator p = methods->begin();
7141 p != methods->end();
7144 if (p != methods->begin())
7146 if (p->name().empty())
7147 this->append_reflection(p->type(), gogo, ret);
7150 if (!Gogo::is_hidden_name(p->name()))
7151 ret->append(p->name());
7152 else if (gogo->pkgpath_from_option())
7153 ret->append(p->name().substr(1));
7156 // If no -fgo-pkgpath option, backward compatibility
7157 // for how this used to work before -fgo-pkgpath was
7159 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
7160 ret->append(pkgpath.substr(pkgpath.find('.') + 1));
7161 ret->push_back('.');
7162 ret->append(Gogo::unpack_hidden_name(p->name()));
7164 std::string sub = p->type()->reflection(gogo);
7165 go_assert(sub.compare(0, 4, "func") == 0);
7166 sub = sub.substr(4);
7170 ret->push_back(' ');
7178 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7180 go_assert(this->methods_are_finalized_);
7182 ret->push_back('I');
7184 const Typed_identifier_list* methods = this->all_methods_;
7185 if (methods != NULL && !this->seen_)
7188 for (Typed_identifier_list::const_iterator p = methods->begin();
7189 p != methods->end();
7192 if (!p->name().empty())
7194 std::string n = Gogo::unpack_hidden_name(p->name());
7196 snprintf(buf, sizeof buf, "%u_",
7197 static_cast<unsigned int>(n.length()));
7201 this->append_mangled_name(p->type(), gogo, ret);
7203 this->seen_ = false;
7206 ret->push_back('e');
7212 Interface_type::do_export(Export* exp) const
7214 exp->write_c_string("interface { ");
7216 const Typed_identifier_list* methods = this->parse_methods_;
7217 if (methods != NULL)
7219 for (Typed_identifier_list::const_iterator pm = methods->begin();
7220 pm != methods->end();
7223 if (pm->name().empty())
7225 exp->write_c_string("? ");
7226 exp->write_type(pm->type());
7230 exp->write_string(pm->name());
7231 exp->write_c_string(" (");
7233 const Function_type* fntype = pm->type()->function_type();
7236 const Typed_identifier_list* parameters = fntype->parameters();
7237 if (parameters != NULL)
7239 bool is_varargs = fntype->is_varargs();
7240 for (Typed_identifier_list::const_iterator pp =
7241 parameters->begin();
7242 pp != parameters->end();
7248 exp->write_c_string(", ");
7249 exp->write_name(pp->name());
7250 exp->write_c_string(" ");
7251 if (!is_varargs || pp + 1 != parameters->end())
7252 exp->write_type(pp->type());
7255 exp->write_c_string("...");
7256 Type *pptype = pp->type();
7257 exp->write_type(pptype->array_type()->element_type());
7262 exp->write_c_string(")");
7264 const Typed_identifier_list* results = fntype->results();
7265 if (results != NULL)
7267 exp->write_c_string(" ");
7268 if (results->size() == 1 && results->begin()->name().empty())
7269 exp->write_type(results->begin()->type());
7273 exp->write_c_string("(");
7274 for (Typed_identifier_list::const_iterator p =
7276 p != results->end();
7282 exp->write_c_string(", ");
7283 exp->write_name(p->name());
7284 exp->write_c_string(" ");
7285 exp->write_type(p->type());
7287 exp->write_c_string(")");
7292 exp->write_c_string("; ");
7296 exp->write_c_string("}");
7299 // Import an interface type.
7302 Interface_type::do_import(Import* imp)
7304 imp->require_c_string("interface { ");
7306 Typed_identifier_list* methods = new Typed_identifier_list;
7307 while (imp->peek_char() != '}')
7309 std::string name = imp->read_identifier();
7313 imp->require_c_string(" ");
7314 Type* t = imp->read_type();
7315 methods->push_back(Typed_identifier("", t, imp->location()));
7316 imp->require_c_string("; ");
7320 imp->require_c_string(" (");
7322 Typed_identifier_list* parameters;
7323 bool is_varargs = false;
7324 if (imp->peek_char() == ')')
7328 parameters = new Typed_identifier_list;
7331 std::string name = imp->read_name();
7332 imp->require_c_string(" ");
7334 if (imp->match_c_string("..."))
7340 Type* ptype = imp->read_type();
7342 ptype = Type::make_array_type(ptype, NULL);
7343 parameters->push_back(Typed_identifier(name, ptype,
7345 if (imp->peek_char() != ',')
7347 go_assert(!is_varargs);
7348 imp->require_c_string(", ");
7351 imp->require_c_string(")");
7353 Typed_identifier_list* results;
7354 if (imp->peek_char() != ' ')
7358 results = new Typed_identifier_list;
7360 if (imp->peek_char() != '(')
7362 Type* rtype = imp->read_type();
7363 results->push_back(Typed_identifier("", rtype, imp->location()));
7370 std::string name = imp->read_name();
7371 imp->require_c_string(" ");
7372 Type* rtype = imp->read_type();
7373 results->push_back(Typed_identifier(name, rtype,
7375 if (imp->peek_char() != ',')
7377 imp->require_c_string(", ");
7379 imp->require_c_string(")");
7383 Function_type* fntype = Type::make_function_type(NULL, parameters,
7387 fntype->set_is_varargs();
7388 methods->push_back(Typed_identifier(name, fntype, imp->location()));
7390 imp->require_c_string("; ");
7393 imp->require_c_string("}");
7395 if (methods->empty())
7401 return Type::make_interface_type(methods, imp->location());
7404 // Make an interface type.
7407 Type::make_interface_type(Typed_identifier_list* methods,
7410 return new Interface_type(methods, location);
7413 // Make an empty interface type.
7416 Type::make_empty_interface_type(Location location)
7418 Interface_type* ret = new Interface_type(NULL, location);
7419 ret->finalize_methods();
7425 // Bind a method to an object.
7428 Method::bind_method(Expression* expr, Location location) const
7430 if (this->stub_ == NULL)
7432 // When there is no stub object, the binding is determined by
7434 return this->do_bind_method(expr, location);
7436 return Expression::make_bound_method(expr, this->stub_, location);
7439 // Return the named object associated with a method. This may only be
7440 // called after methods are finalized.
7443 Method::named_object() const
7445 if (this->stub_ != NULL)
7447 return this->do_named_object();
7450 // Class Named_method.
7452 // The type of the method.
7455 Named_method::do_type() const
7457 if (this->named_object_->is_function())
7458 return this->named_object_->func_value()->type();
7459 else if (this->named_object_->is_function_declaration())
7460 return this->named_object_->func_declaration_value()->type();
7465 // Return the location of the method receiver.
7468 Named_method::do_receiver_location() const
7470 return this->do_type()->receiver()->location();
7473 // Bind a method to an object.
7476 Named_method::do_bind_method(Expression* expr, Location location) const
7478 Named_object* no = this->named_object_;
7479 Bound_method_expression* bme = Expression::make_bound_method(expr, no,
7481 // If this is not a local method, and it does not use a stub, then
7482 // the real method expects a different type. We need to cast the
7484 if (this->depth() > 0 && !this->needs_stub_method())
7486 Function_type* ftype = this->do_type();
7487 go_assert(ftype->is_method());
7488 Type* frtype = ftype->receiver()->type();
7489 bme->set_first_argument_type(frtype);
7494 // Class Interface_method.
7496 // Bind a method to an object.
7499 Interface_method::do_bind_method(Expression* expr,
7500 Location location) const
7502 return Expression::make_interface_field_reference(expr, this->name_,
7508 // Insert a new method. Return true if it was inserted, false
7512 Methods::insert(const std::string& name, Method* m)
7514 std::pair<Method_map::iterator, bool> ins =
7515 this->methods_.insert(std::make_pair(name, m));
7520 Method* old_method = ins.first->second;
7521 if (m->depth() < old_method->depth())
7524 ins.first->second = m;
7529 if (m->depth() == old_method->depth())
7530 old_method->set_is_ambiguous();
7536 // Return the number of unambiguous methods.
7539 Methods::count() const
7542 for (Method_map::const_iterator p = this->methods_.begin();
7543 p != this->methods_.end();
7545 if (!p->second->is_ambiguous())
7550 // Class Named_type.
7552 // Return the name of the type.
7555 Named_type::name() const
7557 return this->named_object_->name();
7560 // Return the name of the type to use in an error message.
7563 Named_type::message_name() const
7565 return this->named_object_->message_name();
7568 // Whether this is an alias. There are currently only two aliases so
7569 // we just recognize them by name.
7572 Named_type::is_alias() const
7574 if (!this->is_builtin())
7576 const std::string& name(this->name());
7577 return name == "byte" || name == "rune";
7580 // Return the base type for this type. We have to be careful about
7581 // circular type definitions, which are invalid but may be seen here.
7584 Named_type::named_base()
7589 Type* ret = this->type_->base();
7590 this->seen_ = false;
7595 Named_type::named_base() const
7600 const Type* ret = this->type_->base();
7601 this->seen_ = false;
7605 // Return whether this is an error type. We have to be careful about
7606 // circular type definitions, which are invalid but may be seen here.
7609 Named_type::is_named_error_type() const
7614 bool ret = this->type_->is_error_type();
7615 this->seen_ = false;
7619 // Whether this type is comparable. We have to be careful about
7620 // circular type definitions.
7623 Named_type::named_type_is_comparable(std::string* reason) const
7628 bool ret = Type::are_compatible_for_comparison(true, this->type_,
7629 this->type_, reason);
7630 this->seen_ = false;
7634 // Add a method to this type.
7637 Named_type::add_method(const std::string& name, Function* function)
7639 if (this->local_methods_ == NULL)
7640 this->local_methods_ = new Bindings(NULL);
7641 return this->local_methods_->add_function(name, NULL, function);
7644 // Add a method declaration to this type.
7647 Named_type::add_method_declaration(const std::string& name, Package* package,
7648 Function_type* type,
7651 if (this->local_methods_ == NULL)
7652 this->local_methods_ = new Bindings(NULL);
7653 return this->local_methods_->add_function_declaration(name, package, type,
7657 // Add an existing method to this type.
7660 Named_type::add_existing_method(Named_object* no)
7662 if (this->local_methods_ == NULL)
7663 this->local_methods_ = new Bindings(NULL);
7664 this->local_methods_->add_named_object(no);
7667 // Look for a local method NAME, and returns its named object, or NULL
7671 Named_type::find_local_method(const std::string& name) const
7673 if (this->local_methods_ == NULL)
7675 return this->local_methods_->lookup(name);
7678 // Return whether NAME is an unexported field or method, for better
7682 Named_type::is_unexported_local_method(Gogo* gogo,
7683 const std::string& name) const
7685 Bindings* methods = this->local_methods_;
7686 if (methods != NULL)
7688 for (Bindings::const_declarations_iterator p =
7689 methods->begin_declarations();
7690 p != methods->end_declarations();
7693 if (Gogo::is_hidden_name(p->first)
7694 && name == Gogo::unpack_hidden_name(p->first)
7695 && gogo->pack_hidden_name(name, false) != p->first)
7702 // Build the complete list of methods for this type, which means
7703 // recursively including all methods for anonymous fields. Create all
7707 Named_type::finalize_methods(Gogo* gogo)
7709 if (this->all_methods_ != NULL)
7712 if (this->local_methods_ != NULL
7713 && (this->points_to() != NULL || this->interface_type() != NULL))
7715 const Bindings* lm = this->local_methods_;
7716 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
7717 p != lm->end_declarations();
7719 error_at(p->second->location(),
7720 "invalid pointer or interface receiver type");
7721 delete this->local_methods_;
7722 this->local_methods_ = NULL;
7726 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
7729 // Return the method NAME, or NULL if there isn't one or if it is
7730 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
7734 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
7736 return Type::method_function(this->all_methods_, name, is_ambiguous);
7739 // Return a pointer to the interface method table for this type for
7740 // the interface INTERFACE. IS_POINTER is true if this is for a
7744 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
7747 go_assert(!interface->is_empty());
7749 Interface_method_tables** pimt = (is_pointer
7750 ? &this->interface_method_tables_
7751 : &this->pointer_interface_method_tables_);
7754 *pimt = new Interface_method_tables(5);
7756 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
7757 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
7761 // This is a new entry in the hash table.
7762 go_assert(ins.first->second == NULL_TREE);
7763 ins.first->second = gogo->interface_method_table_for_type(interface,
7768 tree decl = ins.first->second;
7769 if (decl == error_mark_node)
7770 return error_mark_node;
7771 go_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
7772 return build_fold_addr_expr(decl);
7775 // Return whether a named type has any hidden fields.
7778 Named_type::named_type_has_hidden_fields(std::string* reason) const
7783 bool ret = this->type_->has_hidden_fields(this, reason);
7784 this->seen_ = false;
7788 // Look for a use of a complete type within another type. This is
7789 // used to check that we don't try to use a type within itself.
7791 class Find_type_use : public Traverse
7794 Find_type_use(Named_type* find_type)
7795 : Traverse(traverse_types),
7796 find_type_(find_type), found_(false)
7799 // Whether we found the type.
7802 { return this->found_; }
7809 // The type we are looking for.
7810 Named_type* find_type_;
7811 // Whether we found the type.
7815 // Check for FIND_TYPE in TYPE.
7818 Find_type_use::type(Type* type)
7820 if (type->named_type() != NULL && this->find_type_ == type->named_type())
7822 this->found_ = true;
7823 return TRAVERSE_EXIT;
7826 // It's OK if we see a reference to the type in any type which is
7827 // essentially a pointer: a pointer, a slice, a function, a map, or
7829 if (type->points_to() != NULL
7830 || type->is_slice_type()
7831 || type->function_type() != NULL
7832 || type->map_type() != NULL
7833 || type->channel_type() != NULL)
7834 return TRAVERSE_SKIP_COMPONENTS;
7836 // For an interface, a reference to the type in a method type should
7837 // be ignored, but we have to consider direct inheritance. When
7838 // this is called, there may be cases of direct inheritance
7839 // represented as a method with no name.
7840 if (type->interface_type() != NULL)
7842 const Typed_identifier_list* methods = type->interface_type()->methods();
7843 if (methods != NULL)
7845 for (Typed_identifier_list::const_iterator p = methods->begin();
7846 p != methods->end();
7849 if (p->name().empty())
7851 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
7852 return TRAVERSE_EXIT;
7856 return TRAVERSE_SKIP_COMPONENTS;
7859 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
7860 // to convert TYPE to the backend representation before we convert
7862 if (type->named_type() != NULL)
7864 switch (type->base()->classification())
7866 case Type::TYPE_ERROR:
7867 case Type::TYPE_BOOLEAN:
7868 case Type::TYPE_INTEGER:
7869 case Type::TYPE_FLOAT:
7870 case Type::TYPE_COMPLEX:
7871 case Type::TYPE_STRING:
7872 case Type::TYPE_NIL:
7875 case Type::TYPE_ARRAY:
7876 case Type::TYPE_STRUCT:
7877 this->find_type_->add_dependency(type->named_type());
7880 case Type::TYPE_NAMED:
7881 case Type::TYPE_FORWARD:
7882 go_assert(saw_errors());
7885 case Type::TYPE_VOID:
7886 case Type::TYPE_SINK:
7887 case Type::TYPE_FUNCTION:
7888 case Type::TYPE_POINTER:
7889 case Type::TYPE_CALL_MULTIPLE_RESULT:
7890 case Type::TYPE_MAP:
7891 case Type::TYPE_CHANNEL:
7892 case Type::TYPE_INTERFACE:
7898 return TRAVERSE_CONTINUE;
7901 // Verify that a named type does not refer to itself.
7904 Named_type::do_verify()
7906 if (this->is_verified_)
7908 this->is_verified_ = true;
7910 Find_type_use find(this);
7911 Type::traverse(this->type_, &find);
7914 error_at(this->location_, "invalid recursive type %qs",
7915 this->message_name().c_str());
7916 this->is_error_ = true;
7920 // Check whether any of the local methods overloads an existing
7921 // struct field or interface method. We don't need to check the
7922 // list of methods against itself: that is handled by the Bindings
7924 if (this->local_methods_ != NULL)
7926 Struct_type* st = this->type_->struct_type();
7929 for (Bindings::const_declarations_iterator p =
7930 this->local_methods_->begin_declarations();
7931 p != this->local_methods_->end_declarations();
7934 const std::string& name(p->first);
7935 if (st != NULL && st->find_local_field(name, NULL) != NULL)
7937 error_at(p->second->location(),
7938 "method %qs redeclares struct field name",
7939 Gogo::message_name(name).c_str());
7948 // Return whether this type is or contains a pointer.
7951 Named_type::do_has_pointer() const
7956 bool ret = this->type_->has_pointer();
7957 this->seen_ = false;
7961 // Return whether comparisons for this type can use the identity
7965 Named_type::do_compare_is_identity(Gogo* gogo) const
7967 // We don't use this->seen_ here because compare_is_identity may
7968 // call base() later, and that will mess up if seen_ is set here.
7969 if (this->seen_in_compare_is_identity_)
7971 this->seen_in_compare_is_identity_ = true;
7972 bool ret = this->type_->compare_is_identity(gogo);
7973 this->seen_in_compare_is_identity_ = false;
7977 // Return a hash code. This is used for method lookup. We simply
7978 // hash on the name itself.
7981 Named_type::do_hash_for_method(Gogo* gogo) const
7983 if (this->is_alias())
7984 return this->type_->named_type()->do_hash_for_method(gogo);
7986 const std::string& name(this->named_object()->name());
7987 unsigned int ret = Type::hash_string(name, 0);
7989 // GOGO will be NULL here when called from Type_hash_identical.
7990 // That is OK because that is only used for internal hash tables
7991 // where we are going to be comparing named types for equality. In
7992 // other cases, which are cases where the runtime is going to
7993 // compare hash codes to see if the types are the same, we need to
7994 // include the pkgpath in the hash.
7995 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
7997 const Package* package = this->named_object()->package();
7998 if (package == NULL)
7999 ret = Type::hash_string(gogo->pkgpath(), ret);
8001 ret = Type::hash_string(package->pkgpath(), ret);
8007 // Convert a named type to the backend representation. In order to
8008 // get dependencies right, we fill in a dummy structure for this type,
8009 // then convert all the dependencies, then complete this type. When
8010 // this function is complete, the size of the type is known.
8013 Named_type::convert(Gogo* gogo)
8015 if (this->is_error_ || this->is_converted_)
8018 this->create_placeholder(gogo);
8020 // If we are called to turn unsafe.Sizeof into a constant, we may
8021 // not have verified the type yet. We have to make sure it is
8022 // verified, since that sets the list of dependencies.
8025 // Convert all the dependencies. If they refer indirectly back to
8026 // this type, they will pick up the intermediate tree we just
8028 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
8029 p != this->dependencies_.end();
8031 (*p)->convert(gogo);
8033 // Complete this type.
8034 Btype* bt = this->named_btype_;
8035 Type* base = this->type_->base();
8036 switch (base->classification())
8053 // The size of these types is already correct. We don't worry
8054 // about filling them in until later, when we also track
8055 // circular references.
8060 std::vector<Backend::Btyped_identifier> bfields;
8061 get_backend_struct_fields(gogo, base->struct_type()->fields(),
8063 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8064 bt = gogo->backend()->error_type();
8069 // Slice types were completed in create_placeholder.
8070 if (!base->is_slice_type())
8072 Btype* bet = base->array_type()->get_backend_element(gogo, true);
8073 Bexpression* blen = base->array_type()->get_backend_length(gogo);
8074 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
8075 bt = gogo->backend()->error_type();
8079 case TYPE_INTERFACE:
8080 // Interface types were completed in create_placeholder.
8088 case TYPE_CALL_MULTIPLE_RESULT:
8094 this->named_btype_ = bt;
8095 this->is_converted_ = true;
8096 this->is_placeholder_ = false;
8099 // Create the placeholder for a named type. This is the first step in
8100 // converting to the backend representation.
8103 Named_type::create_placeholder(Gogo* gogo)
8105 if (this->is_error_)
8106 this->named_btype_ = gogo->backend()->error_type();
8108 if (this->named_btype_ != NULL)
8111 // Create the structure for this type. Note that because we call
8112 // base() here, we don't attempt to represent a named type defined
8113 // as another named type. Instead both named types will point to
8114 // different base representations.
8115 Type* base = this->type_->base();
8117 bool set_name = true;
8118 switch (base->classification())
8121 this->is_error_ = true;
8122 this->named_btype_ = gogo->backend()->error_type();
8132 // These are simple basic types, we can just create them
8134 bt = Type::get_named_base_btype(gogo, base);
8139 // All maps and channels have the same backend representation.
8140 bt = Type::get_named_base_btype(gogo, base);
8146 bool for_function = base->classification() == TYPE_FUNCTION;
8147 bt = gogo->backend()->placeholder_pointer_type(this->name(),
8155 bt = gogo->backend()->placeholder_struct_type(this->name(),
8157 this->is_placeholder_ = true;
8162 if (base->is_slice_type())
8163 bt = gogo->backend()->placeholder_struct_type(this->name(),
8167 bt = gogo->backend()->placeholder_array_type(this->name(),
8169 this->is_placeholder_ = true;
8174 case TYPE_INTERFACE:
8175 if (base->interface_type()->is_empty())
8176 bt = Interface_type::get_backend_empty_interface_type(gogo);
8179 bt = gogo->backend()->placeholder_struct_type(this->name(),
8187 case TYPE_CALL_MULTIPLE_RESULT:
8194 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
8196 this->named_btype_ = bt;
8198 if (base->is_slice_type())
8200 // We do not record slices as dependencies of other types,
8201 // because we can fill them in completely here with the final
8203 std::vector<Backend::Btyped_identifier> bfields;
8204 get_backend_slice_fields(gogo, base->array_type(), true, &bfields);
8205 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8206 this->named_btype_ = gogo->backend()->error_type();
8208 else if (base->interface_type() != NULL
8209 && !base->interface_type()->is_empty())
8211 // We do not record interfaces as dependencies of other types,
8212 // because we can fill them in completely here with the final
8214 std::vector<Backend::Btyped_identifier> bfields;
8215 get_backend_interface_fields(gogo, base->interface_type(), true,
8217 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8218 this->named_btype_ = gogo->backend()->error_type();
8222 // Get a tree for a named type.
8225 Named_type::do_get_backend(Gogo* gogo)
8227 if (this->is_error_)
8228 return gogo->backend()->error_type();
8230 Btype* bt = this->named_btype_;
8232 if (!gogo->named_types_are_converted())
8234 // We have not completed converting named types. NAMED_BTYPE_
8235 // is a placeholder and we shouldn't do anything further.
8239 // We don't build dependencies for types whose sizes do not
8240 // change or are not relevant, so we may see them here while
8241 // converting types.
8242 this->create_placeholder(gogo);
8243 bt = this->named_btype_;
8244 go_assert(bt != NULL);
8248 // We are not converting types. This should only be called if the
8249 // type has already been converted.
8250 if (!this->is_converted_)
8252 go_assert(saw_errors());
8253 return gogo->backend()->error_type();
8256 go_assert(bt != NULL);
8258 // Complete the tree.
8259 Type* base = this->type_->base();
8261 switch (base->classification())
8264 return gogo->backend()->error_type();
8278 if (!this->seen_in_get_backend_)
8280 this->seen_in_get_backend_ = true;
8281 base->struct_type()->finish_backend_fields(gogo);
8282 this->seen_in_get_backend_ = false;
8287 if (!this->seen_in_get_backend_)
8289 this->seen_in_get_backend_ = true;
8290 base->array_type()->finish_backend_element(gogo);
8291 this->seen_in_get_backend_ = false;
8295 case TYPE_INTERFACE:
8296 if (!this->seen_in_get_backend_)
8298 this->seen_in_get_backend_ = true;
8299 base->interface_type()->finish_backend_methods(gogo);
8300 this->seen_in_get_backend_ = false;
8305 // Don't build a circular data structure. GENERIC can't handle
8307 if (this->seen_in_get_backend_)
8309 this->is_circular_ = true;
8310 return gogo->backend()->circular_pointer_type(bt, true);
8312 this->seen_in_get_backend_ = true;
8313 bt1 = Type::get_named_base_btype(gogo, base);
8314 this->seen_in_get_backend_ = false;
8315 if (this->is_circular_)
8316 bt1 = gogo->backend()->circular_pointer_type(bt, true);
8317 if (!gogo->backend()->set_placeholder_function_type(bt, bt1))
8318 bt = gogo->backend()->error_type();
8322 // Don't build a circular data structure. GENERIC can't handle
8324 if (this->seen_in_get_backend_)
8326 this->is_circular_ = true;
8327 return gogo->backend()->circular_pointer_type(bt, false);
8329 this->seen_in_get_backend_ = true;
8330 bt1 = Type::get_named_base_btype(gogo, base);
8331 this->seen_in_get_backend_ = false;
8332 if (this->is_circular_)
8333 bt1 = gogo->backend()->circular_pointer_type(bt, false);
8334 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
8335 bt = gogo->backend()->error_type();
8340 case TYPE_CALL_MULTIPLE_RESULT:
8349 // Build a type descriptor for a named type.
8352 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8354 if (name == NULL && this->is_alias())
8355 return this->type_->type_descriptor(gogo, this->type_);
8357 // If NAME is not NULL, then we don't really want the type
8358 // descriptor for this type; we want the descriptor for the
8359 // underlying type, giving it the name NAME.
8360 return this->named_type_descriptor(gogo, this->type_,
8361 name == NULL ? this : name);
8364 // Add to the reflection string. This is used mostly for the name of
8365 // the type used in a type descriptor, not for actual reflection
8369 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
8371 if (this->is_alias())
8373 this->append_reflection(this->type_, gogo, ret);
8376 if (!this->is_builtin())
8378 // We handle -fgo-prefix and -fgo-pkgpath differently here for
8379 // compatibility with how the compiler worked before
8380 // -fgo-pkgpath was introduced. When -fgo-pkgpath is specified,
8381 // we use it to make a unique reflection string, so that the
8382 // type canonicalization in the reflect package will work. In
8383 // order to be compatible with the gc compiler, we put tabs into
8384 // the package path, so that the reflect methods can discard it.
8385 const Package* package = this->named_object_->package();
8386 if (gogo->pkgpath_from_option())
8388 ret->push_back('\t');
8389 ret->append(package != NULL
8390 ? package->pkgpath_symbol()
8391 : gogo->pkgpath_symbol());
8392 ret->push_back('\t');
8394 ret->append(package != NULL
8395 ? package->package_name()
8396 : gogo->package_name());
8397 ret->push_back('.');
8399 if (this->in_function_ != NULL)
8401 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
8402 ret->push_back('$');
8403 if (this->in_function_index_ > 0)
8406 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8408 ret->push_back('$');
8411 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
8414 // Get the mangled name.
8417 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8419 if (this->is_alias())
8421 this->append_mangled_name(this->type_, gogo, ret);
8424 Named_object* no = this->named_object_;
8426 if (this->is_builtin())
8427 go_assert(this->in_function_ == NULL);
8430 const std::string& pkgpath(no->package() == NULL
8431 ? gogo->pkgpath_symbol()
8432 : no->package()->pkgpath_symbol());
8434 name.append(1, '.');
8435 if (this->in_function_ != NULL)
8437 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
8438 name.append(1, '$');
8439 if (this->in_function_index_ > 0)
8442 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8444 name.append(1, '$');
8448 name.append(Gogo::unpack_hidden_name(no->name()));
8450 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
8455 // Export the type. This is called to export a global type.
8458 Named_type::export_named_type(Export* exp, const std::string&) const
8460 // We don't need to write the name of the type here, because it will
8461 // be written by Export::write_type anyhow.
8462 exp->write_c_string("type ");
8463 exp->write_type(this);
8464 exp->write_c_string(";\n");
8467 // Import a named type.
8470 Named_type::import_named_type(Import* imp, Named_type** ptype)
8472 imp->require_c_string("type ");
8473 Type *type = imp->read_type();
8474 *ptype = type->named_type();
8475 go_assert(*ptype != NULL);
8476 imp->require_c_string(";\n");
8479 // Export the type when it is referenced by another type. In this
8480 // case Export::export_type will already have issued the name.
8483 Named_type::do_export(Export* exp) const
8485 exp->write_type(this->type_);
8487 // To save space, we only export the methods directly attached to
8489 Bindings* methods = this->local_methods_;
8490 if (methods == NULL)
8493 exp->write_c_string("\n");
8494 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
8495 p != methods->end_definitions();
8498 exp->write_c_string(" ");
8499 (*p)->export_named_object(exp);
8502 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
8503 p != methods->end_declarations();
8506 if (p->second->is_function_declaration())
8508 exp->write_c_string(" ");
8509 p->second->export_named_object(exp);
8514 // Make a named type.
8517 Type::make_named_type(Named_object* named_object, Type* type,
8520 return new Named_type(named_object, type, location);
8523 // Finalize the methods for TYPE. It will be a named type or a struct
8524 // type. This sets *ALL_METHODS to the list of methods, and builds
8525 // all required stubs.
8528 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
8529 Methods** all_methods)
8531 *all_methods = NULL;
8532 Types_seen types_seen;
8533 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
8535 Type::build_stub_methods(gogo, type, *all_methods, location);
8538 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8539 // build up the struct field indexes as we go. DEPTH is the depth of
8540 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8541 // adding these methods for an anonymous field with pointer type.
8542 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8543 // calls the real method. TYPES_SEEN is used to avoid infinite
8547 Type::add_methods_for_type(const Type* type,
8548 const Method::Field_indexes* field_indexes,
8550 bool is_embedded_pointer,
8551 bool needs_stub_method,
8552 Types_seen* types_seen,
8555 // Pointer types may not have methods.
8556 if (type->points_to() != NULL)
8559 const Named_type* nt = type->named_type();
8562 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
8568 Type::add_local_methods_for_type(nt, field_indexes, depth,
8569 is_embedded_pointer, needs_stub_method,
8572 Type::add_embedded_methods_for_type(type, field_indexes, depth,
8573 is_embedded_pointer, needs_stub_method,
8574 types_seen, methods);
8576 // If we are called with depth > 0, then we are looking at an
8577 // anonymous field of a struct. If such a field has interface type,
8578 // then we need to add the interface methods. We don't want to add
8579 // them when depth == 0, because we will already handle them
8580 // following the usual rules for an interface type.
8582 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
8585 // Add the local methods for the named type NT to *METHODS. The
8586 // parameters are as for add_methods_to_type.
8589 Type::add_local_methods_for_type(const Named_type* nt,
8590 const Method::Field_indexes* field_indexes,
8592 bool is_embedded_pointer,
8593 bool needs_stub_method,
8596 const Bindings* local_methods = nt->local_methods();
8597 if (local_methods == NULL)
8600 if (*methods == NULL)
8601 *methods = new Methods();
8603 for (Bindings::const_declarations_iterator p =
8604 local_methods->begin_declarations();
8605 p != local_methods->end_declarations();
8608 Named_object* no = p->second;
8609 bool is_value_method = (is_embedded_pointer
8610 || !Type::method_expects_pointer(no));
8611 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
8613 || (depth > 0 && is_value_method)));
8614 if (!(*methods)->insert(no->name(), m))
8619 // Add the embedded methods for TYPE to *METHODS. These are the
8620 // methods attached to anonymous fields. The parameters are as for
8621 // add_methods_to_type.
8624 Type::add_embedded_methods_for_type(const Type* type,
8625 const Method::Field_indexes* field_indexes,
8627 bool is_embedded_pointer,
8628 bool needs_stub_method,
8629 Types_seen* types_seen,
8632 // Look for anonymous fields in TYPE. TYPE has fields if it is a
8634 const Struct_type* st = type->struct_type();
8638 const Struct_field_list* fields = st->fields();
8643 for (Struct_field_list::const_iterator pf = fields->begin();
8644 pf != fields->end();
8647 if (!pf->is_anonymous())
8650 Type* ftype = pf->type();
8651 bool is_pointer = false;
8652 if (ftype->points_to() != NULL)
8654 ftype = ftype->points_to();
8657 Named_type* fnt = ftype->named_type();
8660 // This is an error, but it will be diagnosed elsewhere.
8664 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
8665 sub_field_indexes->next = field_indexes;
8666 sub_field_indexes->field_index = i;
8668 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
8669 (is_embedded_pointer || is_pointer),
8678 // If TYPE is an interface type, then add its method to *METHODS.
8679 // This is for interface methods attached to an anonymous field. The
8680 // parameters are as for add_methods_for_type.
8683 Type::add_interface_methods_for_type(const Type* type,
8684 const Method::Field_indexes* field_indexes,
8688 const Interface_type* it = type->interface_type();
8692 const Typed_identifier_list* imethods = it->methods();
8693 if (imethods == NULL)
8696 if (*methods == NULL)
8697 *methods = new Methods();
8699 for (Typed_identifier_list::const_iterator pm = imethods->begin();
8700 pm != imethods->end();
8703 Function_type* fntype = pm->type()->function_type();
8706 // This is an error, but it should be reported elsewhere
8707 // when we look at the methods for IT.
8710 go_assert(!fntype->is_method());
8711 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
8712 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
8713 field_indexes, depth);
8714 if (!(*methods)->insert(pm->name(), m))
8719 // Build stub methods for TYPE as needed. METHODS is the set of
8720 // methods for the type. A stub method may be needed when a type
8721 // inherits a method from an anonymous field. When we need the
8722 // address of the method, as in a type descriptor, we need to build a
8723 // little stub which does the required field dereferences and jumps to
8724 // the real method. LOCATION is the location of the type definition.
8727 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
8730 if (methods == NULL)
8732 for (Methods::const_iterator p = methods->begin();
8733 p != methods->end();
8736 Method* m = p->second;
8737 if (m->is_ambiguous() || !m->needs_stub_method())
8740 const std::string& name(p->first);
8742 // Build a stub method.
8744 const Function_type* fntype = m->type();
8746 static unsigned int counter;
8748 snprintf(buf, sizeof buf, "$this%u", counter);
8751 Type* receiver_type = const_cast<Type*>(type);
8752 if (!m->is_value_method())
8753 receiver_type = Type::make_pointer_type(receiver_type);
8754 Location receiver_location = m->receiver_location();
8755 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
8758 const Typed_identifier_list* fnparams = fntype->parameters();
8759 Typed_identifier_list* stub_params;
8760 if (fnparams == NULL || fnparams->empty())
8764 // We give each stub parameter a unique name.
8765 stub_params = new Typed_identifier_list();
8766 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
8767 pp != fnparams->end();
8771 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
8772 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
8778 const Typed_identifier_list* fnresults = fntype->results();
8779 Typed_identifier_list* stub_results;
8780 if (fnresults == NULL || fnresults->empty())
8781 stub_results = NULL;
8784 // We create the result parameters without any names, since
8785 // we won't refer to them.
8786 stub_results = new Typed_identifier_list();
8787 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
8788 pr != fnresults->end();
8790 stub_results->push_back(Typed_identifier("", pr->type(),
8794 Function_type* stub_type = Type::make_function_type(receiver,
8797 fntype->location());
8798 if (fntype->is_varargs())
8799 stub_type->set_is_varargs();
8801 // We only create the function in the package which creates the
8803 const Package* package;
8804 if (type->named_type() == NULL)
8807 package = type->named_type()->named_object()->package();
8809 if (package != NULL)
8810 stub = Named_object::make_function_declaration(name, package,
8811 stub_type, location);
8814 stub = gogo->start_function(name, stub_type, false,
8815 fntype->location());
8816 Type::build_one_stub_method(gogo, m, buf, stub_params,
8817 fntype->is_varargs(), location);
8818 gogo->finish_function(fntype->location());
8821 m->set_stub_object(stub);
8825 // Build a stub method which adjusts the receiver as required to call
8826 // METHOD. RECEIVER_NAME is the name we used for the receiver.
8827 // PARAMS is the list of function parameters.
8830 Type::build_one_stub_method(Gogo* gogo, Method* method,
8831 const char* receiver_name,
8832 const Typed_identifier_list* params,
8836 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
8837 go_assert(receiver_object != NULL);
8839 Expression* expr = Expression::make_var_reference(receiver_object, location);
8840 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
8841 if (expr->type()->points_to() == NULL)
8842 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8844 Expression_list* arguments;
8845 if (params == NULL || params->empty())
8849 arguments = new Expression_list();
8850 for (Typed_identifier_list::const_iterator p = params->begin();
8854 Named_object* param = gogo->lookup(p->name(), NULL);
8855 go_assert(param != NULL);
8856 Expression* param_ref = Expression::make_var_reference(param,
8858 arguments->push_back(param_ref);
8862 Expression* func = method->bind_method(expr, location);
8863 go_assert(func != NULL);
8864 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
8866 call->set_hidden_fields_are_ok();
8867 size_t count = call->result_count();
8869 gogo->add_statement(Statement::make_statement(call, true));
8872 Expression_list* retvals = new Expression_list();
8874 retvals->push_back(call);
8877 for (size_t i = 0; i < count; ++i)
8878 retvals->push_back(Expression::make_call_result(call, i));
8880 Return_statement* retstat = Statement::make_return_statement(retvals,
8883 // We can return values with hidden fields from a stub. This is
8884 // necessary if the method is itself hidden.
8885 retstat->set_hidden_fields_are_ok();
8887 gogo->add_statement(retstat);
8891 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
8892 // in reverse order.
8895 Type::apply_field_indexes(Expression* expr,
8896 const Method::Field_indexes* field_indexes,
8899 if (field_indexes == NULL)
8901 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
8902 Struct_type* stype = expr->type()->deref()->struct_type();
8903 go_assert(stype != NULL
8904 && field_indexes->field_index < stype->field_count());
8905 if (expr->type()->struct_type() == NULL)
8907 go_assert(expr->type()->points_to() != NULL);
8908 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8909 go_assert(expr->type()->struct_type() == stype);
8911 return Expression::make_field_reference(expr, field_indexes->field_index,
8915 // Return whether NO is a method for which the receiver is a pointer.
8918 Type::method_expects_pointer(const Named_object* no)
8920 const Function_type *fntype;
8921 if (no->is_function())
8922 fntype = no->func_value()->type();
8923 else if (no->is_function_declaration())
8924 fntype = no->func_declaration_value()->type();
8927 return fntype->receiver()->type()->points_to() != NULL;
8930 // Given a set of methods for a type, METHODS, return the method NAME,
8931 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
8932 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
8933 // but is ambiguous (and return NULL).
8936 Type::method_function(const Methods* methods, const std::string& name,
8939 if (is_ambiguous != NULL)
8940 *is_ambiguous = false;
8941 if (methods == NULL)
8943 Methods::const_iterator p = methods->find(name);
8944 if (p == methods->end())
8946 Method* m = p->second;
8947 if (m->is_ambiguous())
8949 if (is_ambiguous != NULL)
8950 *is_ambiguous = true;
8956 // Look for field or method NAME for TYPE. Return an Expression for
8957 // the field or method bound to EXPR. If there is no such field or
8958 // method, give an appropriate error and return an error expression.
8961 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
8962 const std::string& name,
8965 if (type->deref()->is_error_type())
8966 return Expression::make_error(location);
8968 const Named_type* nt = type->deref()->named_type();
8969 const Struct_type* st = type->deref()->struct_type();
8970 const Interface_type* it = type->interface_type();
8972 // If this is a pointer to a pointer, then it is possible that the
8973 // pointed-to type has methods.
8974 bool dereferenced = false;
8978 && type->points_to() != NULL
8979 && type->points_to()->points_to() != NULL)
8981 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8982 type = type->points_to();
8983 if (type->deref()->is_error_type())
8984 return Expression::make_error(location);
8985 nt = type->points_to()->named_type();
8986 st = type->points_to()->struct_type();
8987 dereferenced = true;
8990 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
8991 || expr->is_addressable());
8992 std::vector<const Named_type*> seen;
8993 bool is_method = false;
8994 bool found_pointer_method = false;
8997 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
8998 &seen, NULL, &is_method,
8999 &found_pointer_method, &ambig1, &ambig2))
9004 go_assert(st != NULL);
9005 if (type->struct_type() == NULL)
9007 go_assert(type->points_to() != NULL);
9008 expr = Expression::make_unary(OPERATOR_MULT, expr,
9010 go_assert(expr->type()->struct_type() == st);
9012 ret = st->field_reference(expr, name, location);
9014 else if (it != NULL && it->find_method(name) != NULL)
9015 ret = Expression::make_interface_field_reference(expr, name,
9021 m = nt->method_function(name, NULL);
9022 else if (st != NULL)
9023 m = st->method_function(name, NULL);
9026 go_assert(m != NULL);
9027 if (dereferenced && m->is_value_method())
9030 "calling value method requires explicit dereference");
9031 return Expression::make_error(location);
9033 if (!m->is_value_method() && expr->type()->points_to() == NULL)
9034 expr = Expression::make_unary(OPERATOR_AND, expr, location);
9035 ret = m->bind_method(expr, location);
9037 go_assert(ret != NULL);
9042 if (!ambig1.empty())
9043 error_at(location, "%qs is ambiguous via %qs and %qs",
9044 Gogo::message_name(name).c_str(), ambig1.c_str(),
9046 else if (found_pointer_method)
9047 error_at(location, "method requires a pointer receiver");
9048 else if (nt == NULL && st == NULL && it == NULL)
9050 ("reference to field %qs in object which "
9051 "has no fields or methods"),
9052 Gogo::message_name(name).c_str());
9056 if (!Gogo::is_hidden_name(name))
9057 is_unexported = false;
9060 std::string unpacked = Gogo::unpack_hidden_name(name);
9062 is_unexported = Type::is_unexported_field_or_method(gogo, type,
9067 error_at(location, "reference to unexported field or method %qs",
9068 Gogo::message_name(name).c_str());
9070 error_at(location, "reference to undefined field or method %qs",
9071 Gogo::message_name(name).c_str());
9073 return Expression::make_error(location);
9077 // Look in TYPE for a field or method named NAME, return true if one
9078 // is found. This looks through embedded anonymous fields and handles
9079 // ambiguity. If a method is found, sets *IS_METHOD to true;
9080 // otherwise, if a field is found, set it to false. If
9081 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9082 // whose address can not be taken. SEEN is used to avoid infinite
9083 // recursion on invalid types.
9085 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9086 // method we couldn't use because it requires a pointer. LEVEL is
9087 // used for recursive calls, and can be NULL for a non-recursive call.
9088 // When this function returns false because it finds that the name is
9089 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9090 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9091 // will be unchanged.
9093 // This function just returns whether or not there is a field or
9094 // method, and whether it is a field or method. It doesn't build an
9095 // expression to refer to it. If it is a method, we then look in the
9096 // list of all methods for the type. If it is a field, the search has
9097 // to be done again, looking only for fields, and building up the
9098 // expression as we go.
9101 Type::find_field_or_method(const Type* type,
9102 const std::string& name,
9103 bool receiver_can_be_pointer,
9104 std::vector<const Named_type*>* seen,
9107 bool* found_pointer_method,
9108 std::string* ambig1,
9109 std::string* ambig2)
9111 // Named types can have locally defined methods.
9112 const Named_type* nt = type->named_type();
9113 if (nt == NULL && type->points_to() != NULL)
9114 nt = type->points_to()->named_type();
9117 Named_object* no = nt->find_local_method(name);
9120 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
9126 // Record that we have found a pointer method in order to
9127 // give a better error message if we don't find anything
9129 *found_pointer_method = true;
9132 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9138 // We've already seen this type when searching for methods.
9144 // Interface types can have methods.
9145 const Interface_type* it = type->interface_type();
9146 if (it != NULL && it->find_method(name) != NULL)
9152 // Struct types can have fields. They can also inherit fields and
9153 // methods from anonymous fields.
9154 const Struct_type* st = type->deref()->struct_type();
9157 const Struct_field_list* fields = st->fields();
9162 seen->push_back(nt);
9164 int found_level = 0;
9165 bool found_is_method = false;
9166 std::string found_ambig1;
9167 std::string found_ambig2;
9168 const Struct_field* found_parent = NULL;
9169 for (Struct_field_list::const_iterator pf = fields->begin();
9170 pf != fields->end();
9173 if (pf->is_field_name(name))
9181 if (!pf->is_anonymous())
9184 if (pf->type()->deref()->is_error_type()
9185 || pf->type()->deref()->is_undefined())
9188 Named_type* fnt = pf->type()->named_type();
9190 fnt = pf->type()->deref()->named_type();
9191 go_assert(fnt != NULL);
9193 int sublevel = level == NULL ? 1 : *level + 1;
9195 std::string subambig1;
9196 std::string subambig2;
9197 bool subfound = Type::find_field_or_method(fnt,
9199 receiver_can_be_pointer,
9203 found_pointer_method,
9208 if (!subambig1.empty())
9210 // The name was found via this field, but is ambiguous.
9211 // if the ambiguity is lower or at the same level as
9212 // anything else we have already found, then we want to
9213 // pass the ambiguity back to the caller.
9214 if (found_level == 0 || sublevel <= found_level)
9216 found_ambig1 = (Gogo::message_name(pf->field_name())
9218 found_ambig2 = (Gogo::message_name(pf->field_name())
9220 found_level = sublevel;
9226 // The name was found via this field. Use the level to see
9227 // if we want to use this one, or whether it introduces an
9229 if (found_level == 0 || sublevel < found_level)
9231 found_level = sublevel;
9232 found_is_method = sub_is_method;
9233 found_ambig1.clear();
9234 found_ambig2.clear();
9235 found_parent = &*pf;
9237 else if (sublevel > found_level)
9239 else if (found_ambig1.empty())
9241 // We found an ambiguity.
9242 go_assert(found_parent != NULL);
9243 found_ambig1 = Gogo::message_name(found_parent->field_name());
9244 found_ambig2 = Gogo::message_name(pf->field_name());
9248 // We found an ambiguity, but we already know of one.
9249 // Just report the earlier one.
9254 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
9255 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
9256 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
9257 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
9262 if (found_level == 0)
9264 else if (!found_ambig1.empty())
9266 go_assert(!found_ambig1.empty());
9267 ambig1->assign(found_ambig1);
9268 ambig2->assign(found_ambig2);
9270 *level = found_level;
9276 *level = found_level;
9277 *is_method = found_is_method;
9282 // Return whether NAME is an unexported field or method for TYPE.
9285 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
9286 const std::string& name,
9287 std::vector<const Named_type*>* seen)
9289 const Named_type* nt = type->named_type();
9291 nt = type->deref()->named_type();
9294 if (nt->is_unexported_local_method(gogo, name))
9297 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9303 // We've already seen this type.
9309 const Interface_type* it = type->interface_type();
9310 if (it != NULL && it->is_unexported_method(gogo, name))
9313 type = type->deref();
9315 const Struct_type* st = type->struct_type();
9316 if (st != NULL && st->is_unexported_local_field(gogo, name))
9322 const Struct_field_list* fields = st->fields();
9327 seen->push_back(nt);
9329 for (Struct_field_list::const_iterator pf = fields->begin();
9330 pf != fields->end();
9333 if (pf->is_anonymous()
9334 && !pf->type()->deref()->is_error_type()
9335 && !pf->type()->deref()->is_undefined())
9337 Named_type* subtype = pf->type()->named_type();
9338 if (subtype == NULL)
9339 subtype = pf->type()->deref()->named_type();
9340 if (subtype == NULL)
9342 // This is an error, but it will be diagnosed elsewhere.
9345 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
9360 // Class Forward_declaration.
9362 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
9363 : Type(TYPE_FORWARD),
9364 named_object_(named_object->resolve()), warned_(false)
9366 go_assert(this->named_object_->is_unknown()
9367 || this->named_object_->is_type_declaration());
9370 // Return the named object.
9373 Forward_declaration_type::named_object()
9375 return this->named_object_->resolve();
9379 Forward_declaration_type::named_object() const
9381 return this->named_object_->resolve();
9384 // Return the name of the forward declared type.
9387 Forward_declaration_type::name() const
9389 return this->named_object()->name();
9392 // Warn about a use of a type which has been declared but not defined.
9395 Forward_declaration_type::warn() const
9397 Named_object* no = this->named_object_->resolve();
9398 if (no->is_unknown())
9400 // The name was not defined anywhere.
9403 error_at(this->named_object_->location(),
9404 "use of undefined type %qs",
9405 no->message_name().c_str());
9406 this->warned_ = true;
9409 else if (no->is_type_declaration())
9411 // The name was seen as a type, but the type was never defined.
9412 if (no->type_declaration_value()->using_type())
9414 error_at(this->named_object_->location(),
9415 "use of undefined type %qs",
9416 no->message_name().c_str());
9417 this->warned_ = true;
9422 // The name was defined, but not as a type.
9425 error_at(this->named_object_->location(), "expected type");
9426 this->warned_ = true;
9431 // Get the base type of a declaration. This gives an error if the
9432 // type has not yet been defined.
9435 Forward_declaration_type::real_type()
9437 if (this->is_defined())
9438 return this->named_object()->type_value();
9442 return Type::make_error_type();
9447 Forward_declaration_type::real_type() const
9449 if (this->is_defined())
9450 return this->named_object()->type_value();
9454 return Type::make_error_type();
9458 // Return whether the base type is defined.
9461 Forward_declaration_type::is_defined() const
9463 return this->named_object()->is_type();
9466 // Add a method. This is used when methods are defined before the
9470 Forward_declaration_type::add_method(const std::string& name,
9473 Named_object* no = this->named_object();
9474 if (no->is_unknown())
9475 no->declare_as_type();
9476 return no->type_declaration_value()->add_method(name, function);
9479 // Add a method declaration. This is used when methods are declared
9483 Forward_declaration_type::add_method_declaration(const std::string& name,
9485 Function_type* type,
9488 Named_object* no = this->named_object();
9489 if (no->is_unknown())
9490 no->declare_as_type();
9491 Type_declaration* td = no->type_declaration_value();
9492 return td->add_method_declaration(name, package, type, location);
9498 Forward_declaration_type::do_traverse(Traverse* traverse)
9500 if (this->is_defined()
9501 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
9502 return TRAVERSE_EXIT;
9503 return TRAVERSE_CONTINUE;
9506 // Get the backend representation for the type.
9509 Forward_declaration_type::do_get_backend(Gogo* gogo)
9511 if (this->is_defined())
9512 return Type::get_named_base_btype(gogo, this->real_type());
9515 return gogo->backend()->error_type();
9517 // We represent an undefined type as a struct with no fields. That
9518 // should work fine for the backend, since the same case can arise
9520 std::vector<Backend::Btyped_identifier> fields;
9521 Btype* bt = gogo->backend()->struct_type(fields);
9522 return gogo->backend()->named_type(this->name(), bt,
9523 this->named_object()->location());
9526 // Build a type descriptor for a forwarded type.
9529 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9531 Location ploc = Linemap::predeclared_location();
9532 if (!this->is_defined())
9533 return Expression::make_error(ploc);
9536 Type* t = this->real_type();
9538 return this->named_type_descriptor(gogo, t, name);
9540 return Expression::make_type_descriptor(t, ploc);
9544 // The reflection string.
9547 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
9549 this->append_reflection(this->real_type(), gogo, ret);
9552 // The mangled name.
9555 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
9557 if (this->is_defined())
9558 this->append_mangled_name(this->real_type(), gogo, ret);
9561 const Named_object* no = this->named_object();
9563 if (no->package() == NULL)
9564 name = gogo->pkgpath_symbol();
9566 name = no->package()->pkgpath_symbol();
9568 name += Gogo::unpack_hidden_name(no->name());
9570 snprintf(buf, sizeof buf, "N%u_",
9571 static_cast<unsigned int>(name.length()));
9577 // Export a forward declaration. This can happen when a defined type
9578 // refers to a type which is only declared (and is presumably defined
9579 // in some other file in the same package).
9582 Forward_declaration_type::do_export(Export*) const
9584 // If there is a base type, that should be exported instead of this.
9585 go_assert(!this->is_defined());
9587 // We don't output anything.
9590 // Make a forward declaration.
9593 Type::make_forward_declaration(Named_object* named_object)
9595 return new Forward_declaration_type(named_object);
9598 // Class Typed_identifier_list.
9600 // Sort the entries by name.
9602 struct Typed_identifier_list_sort
9606 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
9607 { return t1.name() < t2.name(); }
9611 Typed_identifier_list::sort_by_name()
9613 std::sort(this->entries_.begin(), this->entries_.end(),
9614 Typed_identifier_list_sort());
9620 Typed_identifier_list::traverse(Traverse* traverse)
9622 for (Typed_identifier_list::const_iterator p = this->begin();
9626 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
9627 return TRAVERSE_EXIT;
9629 return TRAVERSE_CONTINUE;
9634 Typed_identifier_list*
9635 Typed_identifier_list::copy() const
9637 Typed_identifier_list* ret = new Typed_identifier_list();
9638 for (Typed_identifier_list::const_iterator p = this->begin();
9641 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));