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.
13 #include "go-diagnostics.h"
14 #include "go-encode-id.h"
16 #include "expressions.h"
17 #include "statements.h"
23 // Forward declarations so that we don't have to make types.h #include
27 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
29 std::vector
<Backend::Btyped_identifier
>* bfields
);
32 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
33 std::vector
<Backend::Btyped_identifier
>* bfields
);
36 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
38 std::vector
<Backend::Btyped_identifier
>* bfields
);
42 Type::Type(Type_classification classification
)
43 : classification_(classification
), btype_(NULL
), type_descriptor_var_(NULL
),
52 // Get the base type for a type--skip names and forward declarations.
57 switch (this->classification_
)
60 return this->named_type()->named_base();
62 return this->forward_declaration_type()->real_type()->base();
71 switch (this->classification_
)
74 return this->named_type()->named_base();
76 return this->forward_declaration_type()->real_type()->base();
82 // Skip defined forward declarations.
88 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
89 while (ftype
!= NULL
&& ftype
->is_defined())
91 t
= ftype
->real_type();
92 ftype
= t
->forward_declaration_type();
98 Type::forwarded() const
100 const Type
* t
= this;
101 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
102 while (ftype
!= NULL
&& ftype
->is_defined())
104 t
= ftype
->real_type();
105 ftype
= t
->forward_declaration_type();
110 // If this is a named type, return it. Otherwise, return NULL.
115 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
119 Type::named_type() const
121 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
124 // Return true if this type is not defined.
127 Type::is_undefined() const
129 return this->forwarded()->forward_declaration_type() != NULL
;
132 // Return true if this is a basic type: a type which is not composed
133 // of other types, and is not void.
136 Type::is_basic_type() const
138 switch (this->classification_
)
161 return this->base()->is_basic_type();
168 // Return true if this is an abstract type.
171 Type::is_abstract() const
173 switch (this->classification())
176 return this->integer_type()->is_abstract();
178 return this->float_type()->is_abstract();
180 return this->complex_type()->is_abstract();
182 return this->is_abstract_string_type();
184 return this->is_abstract_boolean_type();
190 // Return a non-abstract version of an abstract type.
193 Type::make_non_abstract_type()
195 go_assert(this->is_abstract());
196 switch (this->classification())
199 if (this->integer_type()->is_rune())
200 return Type::lookup_integer_type("int32");
202 return Type::lookup_integer_type("int");
204 return Type::lookup_float_type("float64");
206 return Type::lookup_complex_type("complex128");
208 return Type::lookup_string_type();
210 return Type::lookup_bool_type();
216 // Return true if this is an error type. Don't give an error if we
217 // try to dereference an undefined forwarding type, as this is called
218 // in the parser when the type may legitimately be undefined.
221 Type::is_error_type() const
223 const Type
* t
= this->forwarded();
224 // Note that we return false for an undefined forward type.
225 switch (t
->classification_
)
230 return t
->named_type()->is_named_error_type();
236 // If this is a pointer type, return the type to which it points.
237 // Otherwise, return NULL.
240 Type::points_to() const
242 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
244 return ptype
== NULL
? NULL
: ptype
->points_to();
247 // Return whether this is a slice type.
250 Type::is_slice_type() const
252 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
255 // Return whether this is the predeclared constant nil being used as a
259 Type::is_nil_constant_as_type() const
261 const Type
* t
= this->forwarded();
262 if (t
->forward_declaration_type() != NULL
)
264 const Named_object
* no
= t
->forward_declaration_type()->named_object();
265 if (no
->is_unknown())
266 no
= no
->unknown_value()->real_named_object();
269 && no
->const_value()->expr()->is_nil_expression())
278 Type::traverse(Type
* type
, Traverse
* traverse
)
280 go_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
281 || (traverse
->traverse_mask()
282 & Traverse::traverse_expressions
) != 0);
283 if (traverse
->remember_type(type
))
285 // We have already traversed this type.
286 return TRAVERSE_CONTINUE
;
288 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
290 int t
= traverse
->type(type
);
291 if (t
== TRAVERSE_EXIT
)
292 return TRAVERSE_EXIT
;
293 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
294 return TRAVERSE_CONTINUE
;
296 // An array type has an expression which we need to traverse if
297 // traverse_expressions is set.
298 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
299 return TRAVERSE_EXIT
;
300 return TRAVERSE_CONTINUE
;
303 // Default implementation for do_traverse for child class.
306 Type::do_traverse(Traverse
*)
308 return TRAVERSE_CONTINUE
;
311 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
312 // then return true for all erroneous types; this is used to avoid
313 // cascading errors. If REASON is not NULL, optionally set *REASON to
314 // the reason the types are not identical.
317 Type::are_identical(const Type
* t1
, const Type
* t2
, bool errors_are_identical
,
320 return Type::are_identical_cmp_tags(t1
, t2
, COMPARE_TAGS
,
321 errors_are_identical
, reason
);
324 // Like are_identical, but with a CMP_TAGS parameter.
327 Type::are_identical_cmp_tags(const Type
* t1
, const Type
* t2
, Cmp_tags cmp_tags
,
328 bool errors_are_identical
, std::string
* reason
)
330 if (t1
== NULL
|| t2
== NULL
)
332 // Something is wrong.
333 return errors_are_identical
? true : t1
== t2
;
336 // Skip defined forward declarations.
337 t1
= t1
->forwarded();
338 t2
= t2
->forwarded();
340 // Ignore aliases for purposes of type identity.
341 while (t1
->named_type() != NULL
&& t1
->named_type()->is_alias())
342 t1
= t1
->named_type()->real_type()->forwarded();
343 while (t2
->named_type() != NULL
&& t2
->named_type()->is_alias())
344 t2
= t2
->named_type()->real_type()->forwarded();
349 // An undefined forward declaration is an error.
350 if (t1
->forward_declaration_type() != NULL
351 || t2
->forward_declaration_type() != NULL
)
352 return errors_are_identical
;
354 // Avoid cascading errors with error types.
355 if (t1
->is_error_type() || t2
->is_error_type())
357 if (errors_are_identical
)
359 return t1
->is_error_type() && t2
->is_error_type();
362 // Get a good reason for the sink type. Note that the sink type on
363 // the left hand side of an assignment is handled in are_assignable.
364 if (t1
->is_sink_type() || t2
->is_sink_type())
367 *reason
= "invalid use of _";
371 // A named type is only identical to itself.
372 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
375 // Check type shapes.
376 if (t1
->classification() != t2
->classification())
379 switch (t1
->classification())
385 // These types are always identical.
389 return t1
->integer_type()->is_identical(t2
->integer_type());
392 return t1
->float_type()->is_identical(t2
->float_type());
395 return t1
->complex_type()->is_identical(t2
->complex_type());
398 return t1
->function_type()->is_identical(t2
->function_type(),
401 errors_are_identical
,
405 return Type::are_identical_cmp_tags(t1
->points_to(), t2
->points_to(),
406 cmp_tags
, errors_are_identical
,
410 return t1
->struct_type()->is_identical(t2
->struct_type(), cmp_tags
,
411 errors_are_identical
);
414 return t1
->array_type()->is_identical(t2
->array_type(), cmp_tags
,
415 errors_are_identical
);
418 return t1
->map_type()->is_identical(t2
->map_type(), cmp_tags
,
419 errors_are_identical
);
422 return t1
->channel_type()->is_identical(t2
->channel_type(), cmp_tags
,
423 errors_are_identical
);
426 return t1
->interface_type()->is_identical(t2
->interface_type(), cmp_tags
,
427 errors_are_identical
);
429 case TYPE_CALL_MULTIPLE_RESULT
:
431 *reason
= "invalid use of multiple-value function call";
439 // Return true if it's OK to have a binary operation with types LHS
440 // and RHS. This is not used for shifts or comparisons.
443 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
445 if (Type::are_identical(lhs
, rhs
, true, NULL
))
448 // A constant of abstract bool type may be mixed with any bool type.
449 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
450 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
453 // A constant of abstract string type may be mixed with any string
455 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
456 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
462 // A constant of abstract integer, float, or complex type may be
463 // mixed with an integer, float, or complex type.
464 if ((rhs
->is_abstract()
465 && (rhs
->integer_type() != NULL
466 || rhs
->float_type() != NULL
467 || rhs
->complex_type() != NULL
)
468 && (lhs
->integer_type() != NULL
469 || lhs
->float_type() != NULL
470 || lhs
->complex_type() != NULL
))
471 || (lhs
->is_abstract()
472 && (lhs
->integer_type() != NULL
473 || lhs
->float_type() != NULL
474 || lhs
->complex_type() != NULL
)
475 && (rhs
->integer_type() != NULL
476 || rhs
->float_type() != NULL
477 || rhs
->complex_type() != NULL
)))
480 // The nil type may be compared to a pointer, an interface type, a
481 // slice type, a channel type, a map type, or a function type.
482 if (lhs
->is_nil_type()
483 && (rhs
->points_to() != NULL
484 || rhs
->interface_type() != NULL
485 || rhs
->is_slice_type()
486 || rhs
->map_type() != NULL
487 || rhs
->channel_type() != NULL
488 || rhs
->function_type() != NULL
))
490 if (rhs
->is_nil_type()
491 && (lhs
->points_to() != NULL
492 || lhs
->interface_type() != NULL
493 || lhs
->is_slice_type()
494 || lhs
->map_type() != NULL
495 || lhs
->channel_type() != NULL
496 || lhs
->function_type() != NULL
))
502 // Return true if a value with type T1 may be compared with a value of
503 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
506 Type::are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
507 const Type
*t2
, std::string
*reason
)
510 && !Type::are_assignable(t1
, t2
, NULL
)
511 && !Type::are_assignable(t2
, t1
, NULL
))
514 *reason
= "incompatible types in binary expression";
520 if (t1
->integer_type() == NULL
521 && t1
->float_type() == NULL
522 && !t1
->is_string_type())
525 *reason
= _("invalid comparison of non-ordered type");
529 else if (t1
->is_slice_type()
530 || t1
->map_type() != NULL
531 || t1
->function_type() != NULL
532 || t2
->is_slice_type()
533 || t2
->map_type() != NULL
534 || t2
->function_type() != NULL
)
536 if (!t1
->is_nil_type() && !t2
->is_nil_type())
540 if (t1
->is_slice_type() || t2
->is_slice_type())
541 *reason
= _("slice can only be compared to nil");
542 else if (t1
->map_type() != NULL
|| t2
->map_type() != NULL
)
543 *reason
= _("map can only be compared to nil");
545 *reason
= _("func can only be compared to nil");
547 // Match 6g error messages.
548 if (t1
->interface_type() != NULL
|| t2
->interface_type() != NULL
)
551 snprintf(buf
, sizeof buf
, _("invalid operation (%s)"),
561 if (!t1
->is_boolean_type()
562 && t1
->integer_type() == NULL
563 && t1
->float_type() == NULL
564 && t1
->complex_type() == NULL
565 && !t1
->is_string_type()
566 && t1
->points_to() == NULL
567 && t1
->channel_type() == NULL
568 && t1
->interface_type() == NULL
569 && t1
->struct_type() == NULL
570 && t1
->array_type() == NULL
571 && !t1
->is_nil_type())
574 *reason
= _("invalid comparison of non-comparable type");
578 if (t1
->named_type() != NULL
)
579 return t1
->named_type()->named_type_is_comparable(reason
);
580 else if (t2
->named_type() != NULL
)
581 return t2
->named_type()->named_type_is_comparable(reason
);
582 else if (t1
->struct_type() != NULL
)
584 if (t1
->struct_type()->is_struct_incomparable())
587 *reason
= _("invalid comparison of generated struct");
590 const Struct_field_list
* fields
= t1
->struct_type()->fields();
591 for (Struct_field_list::const_iterator p
= fields
->begin();
595 if (!p
->type()->is_comparable())
598 *reason
= _("invalid comparison of non-comparable struct");
603 else if (t1
->array_type() != NULL
)
605 if (t1
->array_type()->is_array_incomparable())
608 *reason
= _("invalid comparison of generated array");
611 if (t1
->array_type()->length()->is_nil_expression()
612 || !t1
->array_type()->element_type()->is_comparable())
615 *reason
= _("invalid comparison of non-comparable array");
624 // Return true if a value with type RHS may be assigned to a variable
625 // with type LHS. If REASON is not NULL, set *REASON to the reason
626 // the types are not assignable.
629 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
631 // Do some checks first. Make sure the types are defined.
632 if (rhs
!= NULL
&& !rhs
->is_undefined())
634 if (rhs
->is_void_type())
637 *reason
= "non-value used as value";
640 if (rhs
->is_call_multiple_result_type())
643 reason
->assign(_("multiple-value function call in "
644 "single-value context"));
649 // Any value may be assigned to the blank identifier.
651 && !lhs
->is_undefined()
652 && lhs
->is_sink_type())
655 // Identical types are assignable.
656 if (Type::are_identical(lhs
, rhs
, true, reason
))
659 // The types are assignable if they have identical underlying types
660 // and either LHS or RHS is not a named type.
661 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
662 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
663 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
666 // The types are assignable if LHS is an interface type and RHS
667 // implements the required methods.
668 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
669 if (lhs_interface_type
!= NULL
)
671 if (lhs_interface_type
->implements_interface(rhs
, reason
))
673 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
674 if (rhs_interface_type
!= NULL
675 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
680 // The type are assignable if RHS is a bidirectional channel type,
681 // LHS is a channel type, they have identical element types, and
682 // either LHS or RHS is not a named type.
683 if (lhs
->channel_type() != NULL
684 && rhs
->channel_type() != NULL
685 && rhs
->channel_type()->may_send()
686 && rhs
->channel_type()->may_receive()
687 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
688 && Type::are_identical(lhs
->channel_type()->element_type(),
689 rhs
->channel_type()->element_type(),
694 // The nil type may be assigned to a pointer, function, slice, map,
695 // channel, or interface type.
696 if (rhs
->is_nil_type()
697 && (lhs
->points_to() != NULL
698 || lhs
->function_type() != NULL
699 || lhs
->is_slice_type()
700 || lhs
->map_type() != NULL
701 || lhs
->channel_type() != NULL
702 || lhs
->interface_type() != NULL
))
705 // An untyped numeric constant may be assigned to a numeric type if
706 // it is representable in that type.
707 if ((rhs
->is_abstract()
708 && (rhs
->integer_type() != NULL
709 || rhs
->float_type() != NULL
710 || rhs
->complex_type() != NULL
))
711 && (lhs
->integer_type() != NULL
712 || lhs
->float_type() != NULL
713 || lhs
->complex_type() != NULL
))
716 // Give some better error messages.
717 if (reason
!= NULL
&& reason
->empty())
719 if (rhs
->interface_type() != NULL
)
720 reason
->assign(_("need explicit conversion"));
721 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
723 size_t len
= (lhs
->named_type()->name().length()
724 + rhs
->named_type()->name().length()
726 char* buf
= new char[len
];
727 snprintf(buf
, len
, _("cannot use type %s as type %s"),
728 rhs
->named_type()->message_name().c_str(),
729 lhs
->named_type()->message_name().c_str());
738 // Return true if a value with type RHS may be converted to type LHS.
739 // If REASON is not NULL, set *REASON to the reason the types are not
743 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
745 // The types are convertible if they are assignable.
746 if (Type::are_assignable(lhs
, rhs
, reason
))
749 // A pointer to a regular type may not be converted to a pointer to
750 // a type that may not live in the heap, except when converting to
752 if (lhs
->points_to() != NULL
753 && rhs
->points_to() != NULL
754 && !rhs
->points_to()->in_heap()
755 && lhs
->points_to()->in_heap()
756 && !lhs
->is_unsafe_pointer_type())
759 reason
->assign(_("conversion from notinheap type to normal type"));
763 // The types are convertible if they have identical underlying
764 // types, ignoring struct field tags.
765 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
766 && Type::are_identical_cmp_tags(lhs
->base(), rhs
->base(), IGNORE_TAGS
,
770 // The types are convertible if they are both unnamed pointer types
771 // and their pointer base types have identical underlying types,
772 // ignoring struct field tags.
773 if (lhs
->named_type() == NULL
774 && rhs
->named_type() == NULL
775 && lhs
->points_to() != NULL
776 && rhs
->points_to() != NULL
777 && (lhs
->points_to()->named_type() != NULL
778 || rhs
->points_to()->named_type() != NULL
)
779 && Type::are_identical_cmp_tags(lhs
->points_to()->base(),
780 rhs
->points_to()->base(),
786 // Integer and floating point types are convertible to each other.
787 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
788 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
791 // Complex types are convertible to each other.
792 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
795 // An integer, or []byte, or []rune, may be converted to a string.
796 if (lhs
->is_string_type())
798 if (rhs
->integer_type() != NULL
)
800 if (rhs
->is_slice_type())
802 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
803 if (e
->integer_type() != NULL
804 && (e
->integer_type()->is_byte()
805 || e
->integer_type()->is_rune()))
810 // A string may be converted to []byte or []rune.
811 if (rhs
->is_string_type() && lhs
->is_slice_type())
813 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
814 if (e
->integer_type() != NULL
815 && (e
->integer_type()->is_byte() || e
->integer_type()->is_rune()))
819 // An unsafe.Pointer type may be converted to any pointer type or to
820 // a type whose underlying type is uintptr, and vice-versa.
821 if (lhs
->is_unsafe_pointer_type()
822 && (rhs
->points_to() != NULL
823 || (rhs
->integer_type() != NULL
824 && rhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
826 if (rhs
->is_unsafe_pointer_type()
827 && (lhs
->points_to() != NULL
828 || (lhs
->integer_type() != NULL
829 && lhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
832 // Give a better error message.
836 *reason
= "invalid type conversion";
839 std::string s
= "invalid type conversion (";
849 // Return a hash code for the type to be used for method lookup.
852 Type::hash_for_method(Gogo
* gogo
) const
854 if (this->named_type() != NULL
&& this->named_type()->is_alias())
855 return this->named_type()->real_type()->hash_for_method(gogo
);
856 unsigned int ret
= 0;
857 if (this->classification_
!= TYPE_FORWARD
)
858 ret
+= this->classification_
;
859 return ret
+ this->do_hash_for_method(gogo
);
862 // Default implementation of do_hash_for_method. This is appropriate
863 // for types with no subfields.
866 Type::do_hash_for_method(Gogo
*) const
871 // Return a hash code for a string, given a starting hash.
874 Type::hash_string(const std::string
& s
, unsigned int h
)
876 const char* p
= s
.data();
877 size_t len
= s
.length();
878 for (; len
> 0; --len
)
886 // A hash table mapping unnamed types to the backend representation of
889 Type::Type_btypes
Type::type_btypes
;
891 // Return the backend representation for this type.
894 Type::get_backend(Gogo
* gogo
)
896 if (this->btype_
!= NULL
)
899 if (this->forward_declaration_type() != NULL
900 || this->named_type() != NULL
)
901 return this->get_btype_without_hash(gogo
);
903 if (this->is_error_type())
904 return gogo
->backend()->error_type();
906 // To avoid confusing the backend, translate all identical Go types
907 // to the same backend representation. We use a hash table to do
908 // that. There is no need to use the hash table for named types, as
909 // named types are only identical to themselves.
911 std::pair
<Type
*, Type_btype_entry
> val
;
913 val
.second
.btype
= NULL
;
914 val
.second
.is_placeholder
= false;
915 std::pair
<Type_btypes::iterator
, bool> ins
=
916 Type::type_btypes
.insert(val
);
917 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
919 // Note that GOGO can be NULL here, but only when the GCC
920 // middle-end is asking for a frontend type. That will only
921 // happen for simple types, which should never require
923 if (!ins
.first
->second
.is_placeholder
)
924 this->btype_
= ins
.first
->second
.btype
;
925 else if (gogo
->named_types_are_converted())
927 this->finish_backend(gogo
, ins
.first
->second
.btype
);
928 ins
.first
->second
.is_placeholder
= false;
931 return ins
.first
->second
.btype
;
934 Btype
* bt
= this->get_btype_without_hash(gogo
);
936 if (ins
.first
->second
.btype
== NULL
)
938 ins
.first
->second
.btype
= bt
;
939 ins
.first
->second
.is_placeholder
= false;
943 // We have already created a backend representation for this
944 // type. This can happen when an unnamed type is defined using
945 // a named type which in turns uses an identical unnamed type.
946 // Use the representation we created earlier and ignore the one we just
948 if (this->btype_
== bt
)
949 this->btype_
= ins
.first
->second
.btype
;
950 bt
= ins
.first
->second
.btype
;
956 // Return the backend representation for a type without looking in the
957 // hash table for identical types. This is used for named types,
958 // since a named type is never identical to any other type.
961 Type::get_btype_without_hash(Gogo
* gogo
)
963 if (this->btype_
== NULL
)
965 Btype
* bt
= this->do_get_backend(gogo
);
967 // For a recursive function or pointer type, we will temporarily
968 // return a circular pointer type during the recursion. We
969 // don't want to record that for a forwarding type, as it may
971 if (this->forward_declaration_type() != NULL
972 && gogo
->backend()->is_circular_pointer_type(bt
))
975 if (gogo
== NULL
|| !gogo
->named_types_are_converted())
983 // Get the backend representation of a type without forcing the
984 // creation of the backend representation of all supporting types.
985 // This will return a backend type that has the correct size but may
986 // be incomplete. E.g., a pointer will just be a placeholder pointer,
987 // and will not contain the final representation of the type to which
988 // it points. This is used while converting all named types to the
989 // backend representation, to avoid problems with indirect references
990 // to types which are not yet complete. When this is called, the
991 // sizes of all direct references (e.g., a struct field) should be
992 // known, but the sizes of indirect references (e.g., the type to
993 // which a pointer points) may not.
996 Type::get_backend_placeholder(Gogo
* gogo
)
998 if (gogo
->named_types_are_converted())
999 return this->get_backend(gogo
);
1000 if (this->btype_
!= NULL
)
1001 return this->btype_
;
1004 switch (this->classification_
)
1014 // These are simple types that can just be created directly.
1015 return this->get_backend(gogo
);
1019 // All maps and channels have the same backend representation.
1020 return this->get_backend(gogo
);
1024 // Named types keep track of their own dependencies and manage
1025 // their own placeholders.
1026 return this->get_backend(gogo
);
1028 case TYPE_INTERFACE
:
1029 if (this->interface_type()->is_empty())
1030 return Interface_type::get_backend_empty_interface_type(gogo
);
1037 std::pair
<Type
*, Type_btype_entry
> val
;
1039 val
.second
.btype
= NULL
;
1040 val
.second
.is_placeholder
= false;
1041 std::pair
<Type_btypes::iterator
, bool> ins
=
1042 Type::type_btypes
.insert(val
);
1043 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
1044 return ins
.first
->second
.btype
;
1046 switch (this->classification_
)
1050 // A Go function type is a pointer to a struct type.
1051 Location loc
= this->function_type()->location();
1052 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1058 Location loc
= Linemap::unknown_location();
1059 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1060 Pointer_type
* pt
= this->convert
<Pointer_type
, TYPE_POINTER
>();
1061 Type::placeholder_pointers
.push_back(pt
);
1066 // We don't have to make the struct itself be a placeholder. We
1067 // are promised that we know the sizes of the struct fields.
1068 // But we may have to use a placeholder for any particular
1071 std::vector
<Backend::Btyped_identifier
> bfields
;
1072 get_backend_struct_fields(gogo
, this->struct_type()->fields(),
1074 bt
= gogo
->backend()->struct_type(bfields
);
1079 if (this->is_slice_type())
1081 std::vector
<Backend::Btyped_identifier
> bfields
;
1082 get_backend_slice_fields(gogo
, this->array_type(), true, &bfields
);
1083 bt
= gogo
->backend()->struct_type(bfields
);
1087 Btype
* element
= this->array_type()->get_backend_element(gogo
, true);
1088 Bexpression
* len
= this->array_type()->get_backend_length(gogo
);
1089 bt
= gogo
->backend()->array_type(element
, len
);
1093 case TYPE_INTERFACE
:
1095 go_assert(!this->interface_type()->is_empty());
1096 std::vector
<Backend::Btyped_identifier
> bfields
;
1097 get_backend_interface_fields(gogo
, this->interface_type(), true,
1099 bt
= gogo
->backend()->struct_type(bfields
);
1104 case TYPE_CALL_MULTIPLE_RESULT
:
1105 /* Note that various classifications were handled in the earlier
1111 if (ins
.first
->second
.btype
== NULL
)
1113 ins
.first
->second
.btype
= bt
;
1114 ins
.first
->second
.is_placeholder
= true;
1118 // A placeholder for this type got created along the way. Use
1119 // that one and ignore the one we just built.
1120 bt
= ins
.first
->second
.btype
;
1126 // Complete the backend representation. This is called for a type
1127 // using a placeholder type.
1130 Type::finish_backend(Gogo
* gogo
, Btype
*placeholder
)
1132 switch (this->classification_
)
1146 Btype
* bt
= this->do_get_backend(gogo
);
1147 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1148 go_assert(saw_errors());
1154 Btype
* bt
= this->do_get_backend(gogo
);
1155 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1156 go_assert(saw_errors());
1161 // The struct type itself is done, but we have to make sure that
1162 // all the field types are converted.
1163 this->struct_type()->finish_backend_fields(gogo
);
1167 // The array type itself is done, but make sure the element type
1169 this->array_type()->finish_backend_element(gogo
);
1176 case TYPE_INTERFACE
:
1177 // The interface type itself is done, but make sure the method
1178 // types are converted.
1179 this->interface_type()->finish_backend_methods(gogo
);
1187 case TYPE_CALL_MULTIPLE_RESULT
:
1192 this->btype_
= placeholder
;
1195 // Return a pointer to the type descriptor for this type.
1198 Type::type_descriptor_pointer(Gogo
* gogo
, Location location
)
1200 Type
* t
= this->forwarded();
1201 while (t
->named_type() != NULL
&& t
->named_type()->is_alias())
1202 t
= t
->named_type()->real_type()->forwarded();
1203 if (t
->type_descriptor_var_
== NULL
)
1205 t
->make_type_descriptor_var(gogo
);
1206 go_assert(t
->type_descriptor_var_
!= NULL
);
1208 Bexpression
* var_expr
=
1209 gogo
->backend()->var_expression(t
->type_descriptor_var_
,
1210 VE_rvalue
, location
);
1211 Bexpression
* var_addr
=
1212 gogo
->backend()->address_expression(var_expr
, location
);
1213 Type
* td_type
= Type::make_type_descriptor_type();
1214 Btype
* td_btype
= td_type
->get_backend(gogo
);
1215 Btype
* ptd_btype
= gogo
->backend()->pointer_type(td_btype
);
1216 return gogo
->backend()->convert_expression(ptd_btype
, var_addr
, location
);
1219 // A mapping from unnamed types to type descriptor variables.
1221 Type::Type_descriptor_vars
Type::type_descriptor_vars
;
1223 // Build the type descriptor for this type.
1226 Type::make_type_descriptor_var(Gogo
* gogo
)
1228 go_assert(this->type_descriptor_var_
== NULL
);
1230 Named_type
* nt
= this->named_type();
1232 // We can have multiple instances of unnamed types, but we only want
1233 // to emit the type descriptor once. We use a hash table. This is
1234 // not necessary for named types, as they are unique, and we store
1235 // the type descriptor in the type itself.
1236 Bvariable
** phash
= NULL
;
1239 Bvariable
* bvnull
= NULL
;
1240 std::pair
<Type_descriptor_vars::iterator
, bool> ins
=
1241 Type::type_descriptor_vars
.insert(std::make_pair(this, bvnull
));
1244 // We've already built a type descriptor for this type.
1245 this->type_descriptor_var_
= ins
.first
->second
;
1248 phash
= &ins
.first
->second
;
1251 // The type descriptor symbol for the unsafe.Pointer type is defined in
1252 // libgo/go-unsafe-pointer.c, so we just return a reference to that
1253 // symbol if necessary.
1254 if (this->is_unsafe_pointer_type())
1256 Location bloc
= Linemap::predeclared_location();
1258 Type
* td_type
= Type::make_type_descriptor_type();
1259 Btype
* td_btype
= td_type
->get_backend(gogo
);
1260 const char *name
= "__go_tdn_unsafe.Pointer";
1261 std::string
asm_name(go_selectively_encode_id(name
));
1262 this->type_descriptor_var_
=
1263 gogo
->backend()->immutable_struct_reference(name
, asm_name
,
1268 *phash
= this->type_descriptor_var_
;
1272 std::string var_name
= this->type_descriptor_var_name(gogo
, nt
);
1274 // Build the contents of the type descriptor.
1275 Expression
* initializer
= this->do_type_descriptor(gogo
, NULL
);
1277 Btype
* initializer_btype
= initializer
->type()->get_backend(gogo
);
1279 Location loc
= nt
== NULL
? Linemap::predeclared_location() : nt
->location();
1281 const Package
* dummy
;
1282 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
1284 std::string
asm_name(go_selectively_encode_id(var_name
));
1285 this->type_descriptor_var_
=
1286 gogo
->backend()->immutable_struct_reference(var_name
, asm_name
,
1290 *phash
= this->type_descriptor_var_
;
1294 // See if this type descriptor can appear in multiple packages.
1295 bool is_common
= false;
1298 // We create the descriptor for a builtin type whenever we need
1300 is_common
= nt
->is_builtin();
1304 // This is an unnamed type. The descriptor could be defined in
1305 // any package where it is needed, and the linker will pick one
1306 // descriptor to keep.
1310 // We are going to build the type descriptor in this package. We
1311 // must create the variable before we convert the initializer to the
1312 // backend representation, because the initializer may refer to the
1313 // type descriptor of this type. By setting type_descriptor_var_ we
1314 // ensure that type_descriptor_pointer will work if called while
1315 // converting INITIALIZER.
1317 std::string
asm_name(go_selectively_encode_id(var_name
));
1318 this->type_descriptor_var_
=
1319 gogo
->backend()->immutable_struct(var_name
, asm_name
, false, is_common
,
1320 initializer_btype
, loc
);
1322 *phash
= this->type_descriptor_var_
;
1324 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
1325 context
.set_is_const();
1326 Bexpression
* binitializer
= initializer
->get_backend(&context
);
1328 gogo
->backend()->immutable_struct_set_init(this->type_descriptor_var_
,
1329 var_name
, false, is_common
,
1330 initializer_btype
, loc
,
1334 // Return the name of the type descriptor variable. If NT is not
1335 // NULL, use it to get the name. Otherwise this is an unnamed type.
1338 Type::type_descriptor_var_name(Gogo
* gogo
, Named_type
* nt
)
1341 return "__go_td_" + this->mangled_name(gogo
);
1343 Named_object
* no
= nt
->named_object();
1345 const Named_object
* in_function
= nt
->in_function(&index
);
1346 std::string ret
= "__go_tdn_";
1347 if (nt
->is_builtin())
1348 go_assert(in_function
== NULL
);
1351 const std::string
& pkgpath(no
->package() == NULL
1352 ? gogo
->pkgpath_symbol()
1353 : no
->package()->pkgpath_symbol());
1354 ret
.append(pkgpath
);
1356 if (in_function
!= NULL
)
1358 const Typed_identifier
* rcvr
=
1359 in_function
->func_value()->type()->receiver();
1362 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
1363 ret
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
1366 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
1371 snprintf(buf
, sizeof buf
, "%u", index
);
1378 std::string
mname(Gogo::mangle_possibly_hidden_name(no
->name()));
1384 // Return true if this type descriptor is defined in a different
1385 // package. If this returns true it sets *PACKAGE to the package.
1388 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1389 const Package
** package
)
1393 if (nt
->named_object()->package() != NULL
)
1395 // This is a named type defined in a different package. The
1396 // type descriptor should be defined in that package.
1397 *package
= nt
->named_object()->package();
1403 if (this->points_to() != NULL
1404 && this->points_to()->named_type() != NULL
1405 && this->points_to()->named_type()->named_object()->package() != NULL
)
1407 // This is an unnamed pointer to a named type defined in a
1408 // different package. The descriptor should be defined in
1410 *package
= this->points_to()->named_type()->named_object()->package();
1417 // Return a composite literal for a type descriptor.
1420 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1422 return type
->do_type_descriptor(gogo
, NULL
);
1425 // Return a composite literal for a type descriptor with a name.
1428 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1430 go_assert(name
!= NULL
&& type
->named_type() != name
);
1431 return type
->do_type_descriptor(gogo
, name
);
1434 // Make a builtin struct type from a list of fields. The fields are
1435 // pairs of a name and a type.
1438 Type::make_builtin_struct_type(int nfields
, ...)
1441 va_start(ap
, nfields
);
1443 Location bloc
= Linemap::predeclared_location();
1444 Struct_field_list
* sfl
= new Struct_field_list();
1445 for (int i
= 0; i
< nfields
; i
++)
1447 const char* field_name
= va_arg(ap
, const char *);
1448 Type
* type
= va_arg(ap
, Type
*);
1449 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1454 Struct_type
* ret
= Type::make_struct_type(sfl
, bloc
);
1455 ret
->set_is_struct_incomparable();
1459 // A list of builtin named types.
1461 std::vector
<Named_type
*> Type::named_builtin_types
;
1463 // Make a builtin named type.
1466 Type::make_builtin_named_type(const char* name
, Type
* type
)
1468 Location bloc
= Linemap::predeclared_location();
1469 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1470 Named_type
* ret
= no
->type_value();
1471 Type::named_builtin_types
.push_back(ret
);
1475 // Convert the named builtin types.
1478 Type::convert_builtin_named_types(Gogo
* gogo
)
1480 for (std::vector
<Named_type
*>::const_iterator p
=
1481 Type::named_builtin_types
.begin();
1482 p
!= Type::named_builtin_types
.end();
1485 bool r
= (*p
)->verify();
1487 (*p
)->convert(gogo
);
1491 // Return the type of a type descriptor. We should really tie this to
1492 // runtime.Type rather than copying it. This must match the struct "_type"
1493 // declared in libgo/go/runtime/type.go.
1496 Type::make_type_descriptor_type()
1501 Location bloc
= Linemap::predeclared_location();
1503 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1504 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
1505 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1506 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1507 Type
* string_type
= Type::lookup_string_type();
1508 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1510 // This is an unnamed version of unsafe.Pointer. Perhaps we
1511 // should use the named version instead, although that would
1512 // require us to create the unsafe package if it has not been
1513 // imported. It probably doesn't matter.
1514 Type
* void_type
= Type::make_void_type();
1515 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1517 Typed_identifier_list
*params
= new Typed_identifier_list();
1518 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
1519 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1521 Typed_identifier_list
* results
= new Typed_identifier_list();
1522 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1524 Type
* hash_fntype
= Type::make_function_type(NULL
, params
, results
,
1527 params
= new Typed_identifier_list();
1528 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
1529 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
1531 results
= new Typed_identifier_list();
1532 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1534 Type
* equal_fntype
= Type::make_function_type(NULL
, params
, results
,
1537 // Forward declaration for the type descriptor type.
1538 Named_object
* named_type_descriptor_type
=
1539 Named_object::make_type_declaration("_type", NULL
, bloc
);
1540 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1541 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1543 // The type of a method on a concrete type.
1544 Struct_type
* method_type
=
1545 Type::make_builtin_struct_type(5,
1546 "name", pointer_string_type
,
1547 "pkgPath", pointer_string_type
,
1548 "mtyp", pointer_type_descriptor_type
,
1549 "typ", pointer_type_descriptor_type
,
1550 "tfn", unsafe_pointer_type
);
1551 Named_type
* named_method_type
=
1552 Type::make_builtin_named_type("method", method_type
);
1554 // Information for types with a name or methods.
1555 Type
* slice_named_method_type
=
1556 Type::make_array_type(named_method_type
, NULL
);
1557 Struct_type
* uncommon_type
=
1558 Type::make_builtin_struct_type(3,
1559 "name", pointer_string_type
,
1560 "pkgPath", pointer_string_type
,
1561 "methods", slice_named_method_type
);
1562 Named_type
* named_uncommon_type
=
1563 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1565 Type
* pointer_uncommon_type
=
1566 Type::make_pointer_type(named_uncommon_type
);
1568 // The type descriptor type.
1570 Struct_type
* type_descriptor_type
=
1571 Type::make_builtin_struct_type(12,
1572 "size", uintptr_type
,
1573 "ptrdata", uintptr_type
,
1574 "hash", uint32_type
,
1576 "align", uint8_type
,
1577 "fieldAlign", uint8_type
,
1578 "hashfn", hash_fntype
,
1579 "equalfn", equal_fntype
,
1580 "gcdata", pointer_uint8_type
,
1581 "string", pointer_string_type
,
1582 "", pointer_uncommon_type
,
1584 pointer_type_descriptor_type
);
1586 Named_type
* named
= Type::make_builtin_named_type("_type",
1587 type_descriptor_type
);
1589 named_type_descriptor_type
->set_type_value(named
);
1597 // Make the type of a pointer to a type descriptor as represented in
1601 Type::make_type_descriptor_ptr_type()
1605 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1609 // Return the alignment required by the memequalN function. N is a
1610 // type size: 16, 32, 64, or 128. The memequalN functions are defined
1611 // in libgo/go/runtime/alg.go.
1614 Type::memequal_align(Gogo
* gogo
, int size
)
1629 // The code uses [2]int64, which must have the same alignment as
1637 Type
* t
= Type::lookup_integer_type(tn
);
1640 if (!t
->backend_type_align(gogo
, &ret
))
1645 // Return whether this type needs specially built type functions.
1646 // This returns true for types that are comparable and either can not
1647 // use an identity comparison, or are a non-standard size.
1650 Type::needs_specific_type_functions(Gogo
* gogo
)
1652 Named_type
* nt
= this->named_type();
1653 if (nt
!= NULL
&& nt
->is_alias())
1655 if (!this->is_comparable())
1657 if (!this->compare_is_identity(gogo
))
1660 // We create a few predeclared types for type descriptors; they are
1661 // really just for the backend and don't need hash or equality
1663 if (nt
!= NULL
&& Linemap::is_predeclared_location(nt
->location()))
1666 int64_t size
, align
;
1667 if (!this->backend_type_size(gogo
, &size
)
1668 || !this->backend_type_align(gogo
, &align
))
1670 go_assert(saw_errors());
1673 // This switch matches the one in Type::type_functions.
1679 return align
< Type::memequal_align(gogo
, 16);
1681 return align
< Type::memequal_align(gogo
, 32);
1683 return align
< Type::memequal_align(gogo
, 64);
1685 return align
< Type::memequal_align(gogo
, 128);
1691 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1692 // hash code for this type and which compare whether two values of
1693 // this type are equal. If NAME is not NULL it is the name of this
1694 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1695 // functions, for convenience; they may be NULL.
1698 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1699 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1700 Named_object
** equal_fn
)
1702 // If this loop leaves NAME as NULL, then the type does not have a
1704 while (name
!= NULL
&& name
->is_alias())
1705 name
= name
->real_type()->named_type();
1707 if (!this->is_comparable())
1714 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1716 Location bloc
= Linemap::predeclared_location();
1718 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1719 Type
* void_type
= Type::make_void_type();
1720 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1722 if (hash_fntype
== NULL
)
1724 Typed_identifier_list
* params
= new Typed_identifier_list();
1725 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1727 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1729 Typed_identifier_list
* results
= new Typed_identifier_list();
1730 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1732 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1734 if (equal_fntype
== NULL
)
1736 Typed_identifier_list
* params
= new Typed_identifier_list();
1737 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1739 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1742 Typed_identifier_list
* results
= new Typed_identifier_list();
1743 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1746 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1750 const char* hash_fnname
;
1751 const char* equal_fnname
;
1752 if (this->compare_is_identity(gogo
))
1754 int64_t size
, align
;
1755 if (!this->backend_type_size(gogo
, &size
)
1756 || !this->backend_type_align(gogo
, &align
))
1758 go_assert(saw_errors());
1761 bool build_functions
= false;
1762 // This switch matches the one in Type::needs_specific_type_functions.
1763 // The alignment tests are because of the memequal functions,
1764 // which assume that the values are aligned as required for an
1765 // integer of that size.
1769 hash_fnname
= "runtime.memhash0";
1770 equal_fnname
= "runtime.memequal0";
1773 hash_fnname
= "runtime.memhash8";
1774 equal_fnname
= "runtime.memequal8";
1777 if (align
< Type::memequal_align(gogo
, 16))
1778 build_functions
= true;
1781 hash_fnname
= "runtime.memhash16";
1782 equal_fnname
= "runtime.memequal16";
1786 if (align
< Type::memequal_align(gogo
, 32))
1787 build_functions
= true;
1790 hash_fnname
= "runtime.memhash32";
1791 equal_fnname
= "runtime.memequal32";
1795 if (align
< Type::memequal_align(gogo
, 64))
1796 build_functions
= true;
1799 hash_fnname
= "runtime.memhash64";
1800 equal_fnname
= "runtime.memequal64";
1804 if (align
< Type::memequal_align(gogo
, 128))
1805 build_functions
= true;
1808 hash_fnname
= "runtime.memhash128";
1809 equal_fnname
= "runtime.memequal128";
1813 build_functions
= true;
1816 if (build_functions
)
1818 // We don't have a built-in function for a type of this size
1819 // and alignment. Build a function to use that calls the
1820 // generic hash/equality functions for identity, passing the size.
1821 this->specific_type_functions(gogo
, name
, size
, hash_fntype
,
1822 equal_fntype
, hash_fn
, equal_fn
);
1828 switch (this->base()->classification())
1830 case Type::TYPE_ERROR
:
1831 case Type::TYPE_VOID
:
1832 case Type::TYPE_NIL
:
1833 case Type::TYPE_FUNCTION
:
1834 case Type::TYPE_MAP
:
1835 // For these types is_comparable should have returned false.
1838 case Type::TYPE_BOOLEAN
:
1839 case Type::TYPE_INTEGER
:
1840 case Type::TYPE_POINTER
:
1841 case Type::TYPE_CHANNEL
:
1842 // For these types compare_is_identity should have returned true.
1845 case Type::TYPE_FLOAT
:
1846 switch (this->float_type()->bits())
1849 hash_fnname
= "runtime.f32hash";
1850 equal_fnname
= "runtime.f32equal";
1853 hash_fnname
= "runtime.f64hash";
1854 equal_fnname
= "runtime.f64equal";
1861 case Type::TYPE_COMPLEX
:
1862 switch (this->complex_type()->bits())
1865 hash_fnname
= "runtime.c64hash";
1866 equal_fnname
= "runtime.c64equal";
1869 hash_fnname
= "runtime.c128hash";
1870 equal_fnname
= "runtime.c128equal";
1877 case Type::TYPE_STRING
:
1878 hash_fnname
= "runtime.strhash";
1879 equal_fnname
= "runtime.strequal";
1882 case Type::TYPE_STRUCT
:
1884 // This is a struct which can not be compared using a
1885 // simple identity function. We need to build a function
1887 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1888 equal_fntype
, hash_fn
, equal_fn
);
1892 case Type::TYPE_ARRAY
:
1893 if (this->is_slice_type())
1895 // Type::is_compatible_for_comparison should have
1901 // This is an array which can not be compared using a
1902 // simple identity function. We need to build a
1903 // function for comparison.
1904 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1905 equal_fntype
, hash_fn
, equal_fn
);
1910 case Type::TYPE_INTERFACE
:
1911 if (this->interface_type()->is_empty())
1913 hash_fnname
= "runtime.nilinterhash";
1914 equal_fnname
= "runtime.nilinterequal";
1918 hash_fnname
= "runtime.interhash";
1919 equal_fnname
= "runtime.interequal";
1923 case Type::TYPE_NAMED
:
1924 case Type::TYPE_FORWARD
:
1933 Location bloc
= Linemap::predeclared_location();
1934 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1936 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1937 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1938 equal_fntype
, bloc
);
1939 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1942 // A hash table mapping types to the specific hash functions.
1944 Type::Type_functions
Type::type_functions_table
;
1946 // Handle a type function which is specific to a type: if SIZE == -1,
1947 // this is a struct or array that can not use an identity comparison.
1948 // Otherwise, it is a type that uses an identity comparison but is not
1949 // one of the standard supported sizes.
1952 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1953 Function_type
* hash_fntype
,
1954 Function_type
* equal_fntype
,
1955 Named_object
** hash_fn
,
1956 Named_object
** equal_fn
)
1958 Hash_equal_fn
fnull(NULL
, NULL
);
1959 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1960 std::pair
<Type_functions::iterator
, bool> ins
=
1961 Type::type_functions_table
.insert(val
);
1964 // We already have functions for this type
1965 *hash_fn
= ins
.first
->second
.first
;
1966 *equal_fn
= ins
.first
->second
.second
;
1970 std::string base_name
;
1973 // Mangled names can have '.' if they happen to refer to named
1974 // types in some way. That's fine if this is simply a named
1975 // type, but otherwise it will confuse the code that builds
1976 // function identifiers. Remove '.' when necessary.
1977 base_name
= this->mangled_name(gogo
);
1979 while ((i
= base_name
.find('.')) != std::string::npos
)
1981 base_name
= gogo
->pack_hidden_name(base_name
, false);
1985 // This name is already hidden or not as appropriate.
1986 base_name
= name
->name();
1988 const Named_object
* in_function
= name
->in_function(&index
);
1989 if (in_function
!= NULL
)
1991 base_name
.append(1, '$');
1992 const Typed_identifier
* rcvr
=
1993 in_function
->func_value()->type()->receiver();
1996 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
1997 base_name
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
1998 base_name
.append(1, '$');
2000 base_name
.append(Gogo::unpack_hidden_name(in_function
->name()));
2004 snprintf(buf
, sizeof buf
, "%u", index
);
2010 std::string hash_name
= base_name
+ "$hash";
2011 std::string equal_name
= base_name
+ "$equal";
2013 Location bloc
= Linemap::predeclared_location();
2015 const Package
* package
= NULL
;
2016 bool is_defined_elsewhere
=
2017 this->type_descriptor_defined_elsewhere(name
, &package
);
2018 if (is_defined_elsewhere
)
2020 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
2022 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
2023 equal_fntype
, bloc
);
2027 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
2028 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
2032 ins
.first
->second
.first
= *hash_fn
;
2033 ins
.first
->second
.second
= *equal_fn
;
2035 if (!is_defined_elsewhere
)
2037 if (gogo
->in_global_scope())
2038 this->write_specific_type_functions(gogo
, name
, size
, hash_name
,
2039 hash_fntype
, equal_name
,
2042 gogo
->queue_specific_type_function(this, name
, size
, hash_name
,
2043 hash_fntype
, equal_name
,
2048 // Write the hash and equality functions for a type which needs to be
2049 // written specially.
2052 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
2053 const std::string
& hash_name
,
2054 Function_type
* hash_fntype
,
2055 const std::string
& equal_name
,
2056 Function_type
* equal_fntype
)
2058 Location bloc
= Linemap::predeclared_location();
2060 if (gogo
->specific_type_functions_are_written())
2062 go_assert(saw_errors());
2066 go_assert(this->is_comparable());
2068 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
2070 hash_fn
->func_value()->set_is_type_specific_function();
2071 gogo
->start_block(bloc
);
2074 this->write_identity_hash(gogo
, size
);
2075 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2076 this->write_named_hash(gogo
, name
, hash_fntype
, equal_fntype
);
2077 else if (this->struct_type() != NULL
)
2078 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
2080 else if (this->array_type() != NULL
)
2081 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
2086 Block
* b
= gogo
->finish_block(bloc
);
2087 gogo
->add_block(b
, bloc
);
2088 gogo
->lower_block(hash_fn
, b
);
2089 gogo
->finish_function(bloc
);
2091 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
2093 equal_fn
->func_value()->set_is_type_specific_function();
2094 gogo
->start_block(bloc
);
2097 this->write_identity_equal(gogo
, size
);
2098 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2099 this->write_named_equal(gogo
, name
);
2100 else if (this->struct_type() != NULL
)
2101 this->struct_type()->write_equal_function(gogo
, name
);
2102 else if (this->array_type() != NULL
)
2103 this->array_type()->write_equal_function(gogo
, name
);
2107 b
= gogo
->finish_block(bloc
);
2108 gogo
->add_block(b
, bloc
);
2109 gogo
->lower_block(equal_fn
, b
);
2110 gogo
->finish_function(bloc
);
2112 // Build the function descriptors for the type descriptor to refer to.
2113 hash_fn
->func_value()->descriptor(gogo
, hash_fn
);
2114 equal_fn
->func_value()->descriptor(gogo
, equal_fn
);
2117 // Write a hash function for a type that can use an identity hash but
2118 // is not one of the standard supported sizes. For example, this
2119 // would be used for the type [3]byte. This builds a return statement
2120 // that returns a call to the memhash function, passing the key and
2121 // seed from the function arguments (already constructed before this
2122 // is called), and the constant size.
2125 Type::write_identity_hash(Gogo
* gogo
, int64_t size
)
2127 Location bloc
= Linemap::predeclared_location();
2129 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2130 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2132 Typed_identifier_list
* params
= new Typed_identifier_list();
2133 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
2134 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
2135 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2137 Typed_identifier_list
* results
= new Typed_identifier_list();
2138 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
2140 Function_type
* memhash_fntype
= Type::make_function_type(NULL
, params
,
2143 Named_object
* memhash
=
2144 Named_object::make_function_declaration("runtime.memhash", NULL
,
2145 memhash_fntype
, bloc
);
2146 memhash
->func_declaration_value()->set_asm_name("runtime.memhash");
2148 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2149 go_assert(key_arg
!= NULL
);
2150 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2151 go_assert(seed_arg
!= NULL
);
2153 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2154 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2155 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2157 Expression_list
* args
= new Expression_list();
2158 args
->push_back(key_ref
);
2159 args
->push_back(seed_ref
);
2160 args
->push_back(size_arg
);
2161 Expression
* func
= Expression::make_func_reference(memhash
, NULL
, bloc
);
2162 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2164 Expression_list
* vals
= new Expression_list();
2165 vals
->push_back(call
);
2166 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2167 gogo
->add_statement(s
);
2170 // Write an equality function for a type that can use an identity
2171 // equality comparison but is not one of the standard supported sizes.
2172 // For example, this would be used for the type [3]byte. This builds
2173 // a return statement that returns a call to the memequal function,
2174 // passing the two keys from the function arguments (already
2175 // constructed before this is called), and the constant size.
2178 Type::write_identity_equal(Gogo
* gogo
, int64_t size
)
2180 Location bloc
= Linemap::predeclared_location();
2182 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2183 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2185 Typed_identifier_list
* params
= new Typed_identifier_list();
2186 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
2187 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
2188 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2190 Typed_identifier_list
* results
= new Typed_identifier_list();
2191 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
2193 Function_type
* memequal_fntype
= Type::make_function_type(NULL
, params
,
2196 Named_object
* memequal
=
2197 Named_object::make_function_declaration("runtime.memequal", NULL
,
2198 memequal_fntype
, bloc
);
2199 memequal
->func_declaration_value()->set_asm_name("runtime.memequal");
2201 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2202 go_assert(key1_arg
!= NULL
);
2203 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2204 go_assert(key2_arg
!= NULL
);
2206 Expression
* key1_ref
= Expression::make_var_reference(key1_arg
, bloc
);
2207 Expression
* key2_ref
= Expression::make_var_reference(key2_arg
, bloc
);
2208 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2210 Expression_list
* args
= new Expression_list();
2211 args
->push_back(key1_ref
);
2212 args
->push_back(key2_ref
);
2213 args
->push_back(size_arg
);
2214 Expression
* func
= Expression::make_func_reference(memequal
, NULL
, bloc
);
2215 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2217 Expression_list
* vals
= new Expression_list();
2218 vals
->push_back(call
);
2219 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2220 gogo
->add_statement(s
);
2223 // Write a hash function that simply calls the hash function for a
2224 // named type. This is used when one named type is defined as
2225 // another. This ensures that this case works when the other named
2226 // type is defined in another package and relies on calling hash
2227 // functions defined only in that package.
2230 Type::write_named_hash(Gogo
* gogo
, Named_type
* name
,
2231 Function_type
* hash_fntype
, Function_type
* equal_fntype
)
2233 Location bloc
= Linemap::predeclared_location();
2235 Named_type
* base_type
= name
->real_type()->named_type();
2236 while (base_type
->is_alias())
2238 base_type
= base_type
->real_type()->named_type();
2239 go_assert(base_type
!= NULL
);
2241 go_assert(base_type
!= NULL
);
2243 // The pointer to the type we are going to hash. This is an
2245 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2246 go_assert(key_arg
!= NULL
);
2248 // The seed argument to the hash function.
2249 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2250 go_assert(seed_arg
!= NULL
);
2252 Named_object
* hash_fn
;
2253 Named_object
* equal_fn
;
2254 name
->real_type()->type_functions(gogo
, base_type
, hash_fntype
, equal_fntype
,
2255 &hash_fn
, &equal_fn
);
2257 // Call the hash function for the base type.
2258 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2259 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2260 Expression_list
* args
= new Expression_list();
2261 args
->push_back(key_ref
);
2262 args
->push_back(seed_ref
);
2263 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
2264 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2266 // Return the hash of the base type.
2267 Expression_list
* vals
= new Expression_list();
2268 vals
->push_back(call
);
2269 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2270 gogo
->add_statement(s
);
2273 // Write an equality function that simply calls the equality function
2274 // for a named type. This is used when one named type is defined as
2275 // another. This ensures that this case works when the other named
2276 // type is defined in another package and relies on calling equality
2277 // functions defined only in that package.
2280 Type::write_named_equal(Gogo
* gogo
, Named_type
* name
)
2282 Location bloc
= Linemap::predeclared_location();
2284 // The pointers to the types we are going to compare. These have
2285 // type unsafe.Pointer.
2286 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2287 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2288 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
2290 Named_type
* base_type
= name
->real_type()->named_type();
2291 go_assert(base_type
!= NULL
);
2293 // Build temporaries with the base type.
2294 Type
* pt
= Type::make_pointer_type(base_type
);
2296 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
2297 ref
= Expression::make_cast(pt
, ref
, bloc
);
2298 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
2299 gogo
->add_statement(p1
);
2301 ref
= Expression::make_var_reference(key2_arg
, bloc
);
2302 ref
= Expression::make_cast(pt
, ref
, bloc
);
2303 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
2304 gogo
->add_statement(p2
);
2306 // Compare the values for equality.
2307 Expression
* t1
= Expression::make_temporary_reference(p1
, bloc
);
2308 t1
= Expression::make_unary(OPERATOR_MULT
, t1
, bloc
);
2310 Expression
* t2
= Expression::make_temporary_reference(p2
, bloc
);
2311 t2
= Expression::make_unary(OPERATOR_MULT
, t2
, bloc
);
2313 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, t1
, t2
, bloc
);
2315 // Return the equality comparison.
2316 Expression_list
* vals
= new Expression_list();
2317 vals
->push_back(cond
);
2318 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2319 gogo
->add_statement(s
);
2322 // Return a composite literal for the type descriptor for a plain type
2323 // of kind RUNTIME_TYPE_KIND named NAME.
2326 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
2327 Named_type
* name
, const Methods
* methods
,
2328 bool only_value_methods
)
2330 Location bloc
= Linemap::predeclared_location();
2332 Type
* td_type
= Type::make_type_descriptor_type();
2333 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
2335 Expression_list
* vals
= new Expression_list();
2338 if (!this->has_pointer())
2339 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
2340 if (this->points_to() != NULL
)
2341 runtime_type_kind
|= RUNTIME_TYPE_KIND_DIRECT_IFACE
;
2344 if (this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2345 runtime_type_kind
|= RUNTIME_TYPE_KIND_GC_PROG
;
2347 Struct_field_list::const_iterator p
= fields
->begin();
2348 go_assert(p
->is_field_name("size"));
2349 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
2350 vals
->push_back(Expression::make_type_info(this, type_info
));
2353 go_assert(p
->is_field_name("ptrdata"));
2354 type_info
= Expression::TYPE_INFO_DESCRIPTOR_PTRDATA
;
2355 vals
->push_back(Expression::make_type_info(this, type_info
));
2358 go_assert(p
->is_field_name("hash"));
2361 h
= name
->hash_for_method(gogo
);
2363 h
= this->hash_for_method(gogo
);
2364 vals
->push_back(Expression::make_integer_ul(h
, p
->type(), bloc
));
2367 go_assert(p
->is_field_name("kind"));
2368 vals
->push_back(Expression::make_integer_ul(runtime_type_kind
, p
->type(),
2372 go_assert(p
->is_field_name("align"));
2373 type_info
= Expression::TYPE_INFO_ALIGNMENT
;
2374 vals
->push_back(Expression::make_type_info(this, type_info
));
2377 go_assert(p
->is_field_name("fieldAlign"));
2378 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
2379 vals
->push_back(Expression::make_type_info(this, type_info
));
2382 go_assert(p
->is_field_name("hashfn"));
2383 Function_type
* hash_fntype
= p
->type()->function_type();
2386 go_assert(p
->is_field_name("equalfn"));
2387 Function_type
* equal_fntype
= p
->type()->function_type();
2389 Named_object
* hash_fn
;
2390 Named_object
* equal_fn
;
2391 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
2393 if (hash_fn
== NULL
)
2394 vals
->push_back(Expression::make_cast(hash_fntype
,
2395 Expression::make_nil(bloc
),
2398 vals
->push_back(Expression::make_func_reference(hash_fn
, NULL
, bloc
));
2399 if (equal_fn
== NULL
)
2400 vals
->push_back(Expression::make_cast(equal_fntype
,
2401 Expression::make_nil(bloc
),
2404 vals
->push_back(Expression::make_func_reference(equal_fn
, NULL
, bloc
));
2407 go_assert(p
->is_field_name("gcdata"));
2408 vals
->push_back(Expression::make_gc_symbol(this));
2411 go_assert(p
->is_field_name("string"));
2412 Expression
* s
= Expression::make_string((name
!= NULL
2413 ? name
->reflection(gogo
)
2414 : this->reflection(gogo
)),
2416 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2419 go_assert(p
->is_field_name("uncommonType"));
2420 if (name
== NULL
&& methods
== NULL
)
2421 vals
->push_back(Expression::make_nil(bloc
));
2424 if (methods
== NULL
)
2425 methods
= name
->methods();
2426 vals
->push_back(this->uncommon_type_constructor(gogo
,
2429 only_value_methods
));
2433 go_assert(p
->is_field_name("ptrToThis"));
2434 if (name
== NULL
&& methods
== NULL
)
2435 vals
->push_back(Expression::make_nil(bloc
));
2440 pt
= Type::make_pointer_type(name
);
2442 pt
= Type::make_pointer_type(this);
2443 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
2447 go_assert(p
== fields
->end());
2449 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
2452 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2453 // length used by the gc toolchain, and also appears in
2454 // libgo/go/reflect/type.go.
2456 static const int64_t max_ptrmask_bytes
= 2048;
2458 // Return a pointer to the Garbage Collection information for this type.
2461 Type::gc_symbol_pointer(Gogo
* gogo
)
2463 Type
* t
= this->forwarded();
2464 while (t
->named_type() != NULL
&& t
->named_type()->is_alias())
2465 t
= t
->named_type()->real_type()->forwarded();
2467 if (!t
->has_pointer())
2468 return gogo
->backend()->nil_pointer_expression();
2470 if (t
->gc_symbol_var_
== NULL
)
2472 t
->make_gc_symbol_var(gogo
);
2473 go_assert(t
->gc_symbol_var_
!= NULL
);
2475 Location bloc
= Linemap::predeclared_location();
2476 Bexpression
* var_expr
=
2477 gogo
->backend()->var_expression(t
->gc_symbol_var_
, VE_rvalue
, bloc
);
2478 Bexpression
* addr_expr
=
2479 gogo
->backend()->address_expression(var_expr
, bloc
);
2481 Type
* uint8_type
= Type::lookup_integer_type("uint8");
2482 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
2483 Btype
* ubtype
= pointer_uint8_type
->get_backend(gogo
);
2484 return gogo
->backend()->convert_expression(ubtype
, addr_expr
, bloc
);
2487 // A mapping from unnamed types to GC symbol variables.
2489 Type::GC_symbol_vars
Type::gc_symbol_vars
;
2491 // Build the GC symbol for this type.
2494 Type::make_gc_symbol_var(Gogo
* gogo
)
2496 go_assert(this->gc_symbol_var_
== NULL
);
2498 Named_type
* nt
= this->named_type();
2500 // We can have multiple instances of unnamed types and similar to type
2501 // descriptors, we only want to the emit the GC data once, so we use a
2503 Bvariable
** phash
= NULL
;
2506 Bvariable
* bvnull
= NULL
;
2507 std::pair
<GC_symbol_vars::iterator
, bool> ins
=
2508 Type::gc_symbol_vars
.insert(std::make_pair(this, bvnull
));
2511 // We've already built a gc symbol for this type.
2512 this->gc_symbol_var_
= ins
.first
->second
;
2515 phash
= &ins
.first
->second
;
2520 if (!this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2522 this->gc_symbol_var_
= this->gc_ptrmask_var(gogo
, ptrsize
, ptrdata
);
2524 *phash
= this->gc_symbol_var_
;
2528 std::string sym_name
= this->type_descriptor_var_name(gogo
, nt
) + "$gc";
2530 // Build the contents of the gc symbol.
2531 Expression
* sym_init
= this->gcprog_constructor(gogo
, ptrsize
, ptrdata
);
2532 Btype
* sym_btype
= sym_init
->type()->get_backend(gogo
);
2534 // If the type descriptor for this type is defined somewhere else, so is the
2536 const Package
* dummy
;
2537 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
2539 std::string
asm_name(go_selectively_encode_id(sym_name
));
2540 this->gc_symbol_var_
=
2541 gogo
->backend()->implicit_variable_reference(sym_name
, asm_name
,
2544 *phash
= this->gc_symbol_var_
;
2548 // See if this gc symbol can appear in multiple packages.
2549 bool is_common
= false;
2552 // We create the symbol for a builtin type whenever we need
2554 is_common
= nt
->is_builtin();
2558 // This is an unnamed type. The descriptor could be defined in
2559 // any package where it is needed, and the linker will pick one
2560 // descriptor to keep.
2564 // Since we are building the GC symbol in this package, we must create the
2565 // variable before converting the initializer to its backend representation
2566 // because the initializer may refer to the GC symbol for this type.
2567 std::string
asm_name(go_selectively_encode_id(sym_name
));
2568 this->gc_symbol_var_
=
2569 gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2570 sym_btype
, false, true, is_common
, 0);
2572 *phash
= this->gc_symbol_var_
;
2574 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2575 context
.set_is_const();
2576 Bexpression
* sym_binit
= sym_init
->get_backend(&context
);
2577 gogo
->backend()->implicit_variable_set_init(this->gc_symbol_var_
, sym_name
,
2578 sym_btype
, false, true, is_common
,
2582 // Return whether this type needs a GC program, and set *PTRDATA to
2583 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2587 Type::needs_gcprog(Gogo
* gogo
, int64_t* ptrsize
, int64_t* ptrdata
)
2589 Type
* voidptr
= Type::make_pointer_type(Type::make_void_type());
2590 if (!voidptr
->backend_type_size(gogo
, ptrsize
))
2593 if (!this->backend_type_ptrdata(gogo
, ptrdata
))
2595 go_assert(saw_errors());
2599 return *ptrdata
/ *ptrsize
> max_ptrmask_bytes
;
2602 // A simple class used to build a GC ptrmask for a type.
2607 Ptrmask(size_t count
)
2608 : bits_((count
+ 7) / 8, 0)
2612 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2618 constructor(Gogo
* gogo
) const;
2623 { this->bits_
.at(index
/ 8) |= 1 << (index
% 8); }
2626 std::vector
<unsigned char> bits_
;
2629 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2630 // counts in bytes. PTRSIZE is the size of a pointer on the target
2634 Ptrmask::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2636 switch (type
->base()->classification())
2639 case Type::TYPE_NIL
:
2640 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2641 case Type::TYPE_NAMED
:
2642 case Type::TYPE_FORWARD
:
2645 case Type::TYPE_ERROR
:
2646 case Type::TYPE_VOID
:
2647 case Type::TYPE_BOOLEAN
:
2648 case Type::TYPE_INTEGER
:
2649 case Type::TYPE_FLOAT
:
2650 case Type::TYPE_COMPLEX
:
2651 case Type::TYPE_SINK
:
2654 case Type::TYPE_FUNCTION
:
2655 case Type::TYPE_POINTER
:
2656 case Type::TYPE_MAP
:
2657 case Type::TYPE_CHANNEL
:
2658 // These types are all a single pointer.
2659 go_assert((offset
% ptrsize
) == 0);
2660 this->set(offset
/ ptrsize
);
2663 case Type::TYPE_STRING
:
2664 // A string starts with a single pointer.
2665 go_assert((offset
% ptrsize
) == 0);
2666 this->set(offset
/ ptrsize
);
2669 case Type::TYPE_INTERFACE
:
2670 // An interface is two pointers.
2671 go_assert((offset
% ptrsize
) == 0);
2672 this->set(offset
/ ptrsize
);
2673 this->set((offset
/ ptrsize
) + 1);
2676 case Type::TYPE_STRUCT
:
2678 if (!type
->has_pointer())
2681 const Struct_field_list
* fields
= type
->struct_type()->fields();
2682 int64_t soffset
= 0;
2683 for (Struct_field_list::const_iterator pf
= fields
->begin();
2684 pf
!= fields
->end();
2687 int64_t field_align
;
2688 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2690 go_assert(saw_errors());
2693 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2695 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2698 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2700 go_assert(saw_errors());
2703 soffset
+= field_size
;
2708 case Type::TYPE_ARRAY
:
2709 if (type
->is_slice_type())
2711 // A slice starts with a single pointer.
2712 go_assert((offset
% ptrsize
) == 0);
2713 this->set(offset
/ ptrsize
);
2718 if (!type
->has_pointer())
2722 if (!type
->array_type()->int_length(&len
))
2724 go_assert(saw_errors());
2728 Type
* element_type
= type
->array_type()->element_type();
2730 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2732 go_assert(saw_errors());
2736 int64_t eoffset
= 0;
2737 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2738 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2744 // Return a symbol name for this ptrmask. This is used to coalesce
2745 // identical ptrmasks, which are common. The symbol name must use
2746 // only characters that are valid in symbols. It's nice if it's
2747 // short. We convert it to a base64 string.
2750 Ptrmask::symname() const
2752 const char chars
[65] =
2753 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.";
2754 go_assert(chars
[64] == '\0');
2758 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2759 p
!= this->bits_
.end();
2762 b
|= *p
<< remaining
;
2764 while (remaining
>= 6)
2766 ret
+= chars
[b
& 0x3f];
2771 while (remaining
> 0)
2773 ret
+= chars
[b
& 0x3f];
2780 // Return a constructor for this ptrmask. This will be used to
2781 // initialize the runtime ptrmask value.
2784 Ptrmask::constructor(Gogo
* gogo
) const
2786 Location bloc
= Linemap::predeclared_location();
2787 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
2788 Expression
* len
= Expression::make_integer_ul(this->bits_
.size(), NULL
,
2790 Array_type
* at
= Type::make_array_type(byte_type
, len
);
2791 Expression_list
* vals
= new Expression_list();
2792 vals
->reserve(this->bits_
.size());
2793 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2794 p
!= this->bits_
.end();
2796 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
2797 return Expression::make_array_composite_literal(at
, vals
, bloc
);
2800 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2801 Type::GC_gcbits_vars
Type::gc_gcbits_vars
;
2803 // Return a ptrmask variable for a type. For a type descriptor this
2804 // is only used for variables that are small enough to not need a
2805 // gcprog, but for a global variable this is used for a variable of
2806 // any size. PTRDATA is the number of bytes of the type that contain
2807 // pointer data. PTRSIZE is the size of a pointer on the target
2811 Type::gc_ptrmask_var(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
2813 Ptrmask
ptrmask(ptrdata
/ ptrsize
);
2814 if (ptrdata
>= ptrsize
)
2815 ptrmask
.set_from(gogo
, this, ptrsize
, 0);
2818 // This can happen in error cases. Just build an empty gcbits.
2819 go_assert(saw_errors());
2821 std::string sym_name
= "runtime.gcbits." + ptrmask
.symname();
2822 Bvariable
* bvnull
= NULL
;
2823 std::pair
<GC_gcbits_vars::iterator
, bool> ins
=
2824 Type::gc_gcbits_vars
.insert(std::make_pair(sym_name
, bvnull
));
2827 // We've already built a GC symbol for this set of gcbits.
2828 return ins
.first
->second
;
2831 Expression
* val
= ptrmask
.constructor(gogo
);
2832 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2833 context
.set_is_const();
2834 Bexpression
* bval
= val
->get_backend(&context
);
2836 std::string
asm_name(go_selectively_encode_id(sym_name
));
2837 Btype
*btype
= val
->type()->get_backend(gogo
);
2838 Bvariable
* ret
= gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2841 gogo
->backend()->implicit_variable_set_init(ret
, sym_name
, btype
, false,
2843 ins
.first
->second
= ret
;
2847 // A GCProg is used to build a program for the garbage collector.
2848 // This is used for types with a lot of pointer data, to reduce the
2849 // size of the data in the compiled program. The program is expanded
2850 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2856 : bytes_(), index_(0), nb_(0)
2859 // The number of bits described so far.
2862 { return this->index_
; }
2865 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2871 constructor(Gogo
* gogo
) const;
2878 should_repeat(int64_t, int64_t);
2881 repeat(int64_t, int64_t);
2884 zero_until(int64_t);
2895 // Add a byte to the program.
2897 byte(unsigned char x
)
2898 { this->bytes_
.push_back(x
); }
2900 // The maximum number of bytes of literal bits.
2901 static const int max_literal
= 127;
2904 std::vector
<unsigned char> bytes_
;
2905 // The index of the last bit described.
2907 // The current set of literal bits.
2908 unsigned char b_
[max_literal
];
2909 // The current number of literal bits.
2913 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2914 // counts in bytes. PTRSIZE is the size of a pointer on the target
2918 GCProg::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2920 switch (type
->base()->classification())
2923 case Type::TYPE_NIL
:
2924 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2925 case Type::TYPE_NAMED
:
2926 case Type::TYPE_FORWARD
:
2929 case Type::TYPE_ERROR
:
2930 case Type::TYPE_VOID
:
2931 case Type::TYPE_BOOLEAN
:
2932 case Type::TYPE_INTEGER
:
2933 case Type::TYPE_FLOAT
:
2934 case Type::TYPE_COMPLEX
:
2935 case Type::TYPE_SINK
:
2938 case Type::TYPE_FUNCTION
:
2939 case Type::TYPE_POINTER
:
2940 case Type::TYPE_MAP
:
2941 case Type::TYPE_CHANNEL
:
2942 // These types are all a single pointer.
2943 go_assert((offset
% ptrsize
) == 0);
2944 this->ptr(offset
/ ptrsize
);
2947 case Type::TYPE_STRING
:
2948 // A string starts with a single pointer.
2949 go_assert((offset
% ptrsize
) == 0);
2950 this->ptr(offset
/ ptrsize
);
2953 case Type::TYPE_INTERFACE
:
2954 // An interface is two pointers.
2955 go_assert((offset
% ptrsize
) == 0);
2956 this->ptr(offset
/ ptrsize
);
2957 this->ptr((offset
/ ptrsize
) + 1);
2960 case Type::TYPE_STRUCT
:
2962 if (!type
->has_pointer())
2965 const Struct_field_list
* fields
= type
->struct_type()->fields();
2966 int64_t soffset
= 0;
2967 for (Struct_field_list::const_iterator pf
= fields
->begin();
2968 pf
!= fields
->end();
2971 int64_t field_align
;
2972 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2974 go_assert(saw_errors());
2977 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2979 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2982 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2984 go_assert(saw_errors());
2987 soffset
+= field_size
;
2992 case Type::TYPE_ARRAY
:
2993 if (type
->is_slice_type())
2995 // A slice starts with a single pointer.
2996 go_assert((offset
% ptrsize
) == 0);
2997 this->ptr(offset
/ ptrsize
);
3002 if (!type
->has_pointer())
3006 if (!type
->array_type()->int_length(&len
))
3008 go_assert(saw_errors());
3012 Type
* element_type
= type
->array_type()->element_type();
3014 // Flatten array of array to a big array by multiplying counts.
3015 while (element_type
->array_type() != NULL
3016 && !element_type
->is_slice_type())
3019 if (!element_type
->array_type()->int_length(&ele_len
))
3021 go_assert(saw_errors());
3026 element_type
= element_type
->array_type()->element_type();
3030 if (!element_type
->backend_type_size(gogo
, &ele_size
))
3032 go_assert(saw_errors());
3036 go_assert(len
> 0 && ele_size
> 0);
3038 if (!this->should_repeat(ele_size
/ ptrsize
, len
))
3040 // Cheaper to just emit the bits.
3041 int64_t eoffset
= 0;
3042 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
3043 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
3047 go_assert((offset
% ptrsize
) == 0);
3048 go_assert((ele_size
% ptrsize
) == 0);
3049 this->set_from(gogo
, element_type
, ptrsize
, offset
);
3050 this->zero_until((offset
+ ele_size
) / ptrsize
);
3051 this->repeat(ele_size
/ ptrsize
, len
- 1);
3059 // Emit a 1 into the bit stream of a GC program at the given bit index.
3062 GCProg::ptr(int64_t index
)
3064 go_assert(index
>= this->index_
);
3065 this->zero_until(index
);
3069 // Return whether it is worthwhile to use a repeat to describe c
3070 // elements of n bits each, compared to just emitting c copies of the
3071 // n-bit description.
3074 GCProg::should_repeat(int64_t n
, int64_t c
)
3076 // Repeat if there is more than 1 item and if the total data doesn't
3077 // fit into four bytes.
3078 return c
> 1 && c
* n
> 4 * 8;
3081 // Emit an instruction to repeat the description of the last n words c
3082 // times (including the initial description, so c + 1 times in total).
3085 GCProg::repeat(int64_t n
, int64_t c
)
3087 if (n
== 0 || c
== 0)
3091 this->byte(0x80 | static_cast<unsigned char>(n
& 0x7f));
3098 this->index_
+= n
* c
;
3101 // Add zeros to the bit stream up to the given index.
3104 GCProg::zero_until(int64_t index
)
3106 go_assert(index
>= this->index_
);
3107 int64_t skip
= index
- this->index_
;
3112 for (int64_t i
= 0; i
< skip
; ++i
)
3118 this->repeat(1, skip
- 1);
3121 // Add a single literal bit to the program.
3124 GCProg::lit(unsigned char x
)
3126 if (this->nb_
== GCProg::max_literal
)
3128 this->b_
[this->nb_
] = x
;
3133 // Emit the varint encoding of x.
3136 GCProg::varint(int64_t x
)
3141 this->byte(0x80 | static_cast<unsigned char>(x
& 0x7f));
3144 this->byte(static_cast<unsigned char>(x
& 0x7f));
3147 // Flush any pending literal bits.
3154 this->byte(static_cast<unsigned char>(this->nb_
));
3155 unsigned char bits
= 0;
3156 for (int i
= 0; i
< this->nb_
; ++i
)
3158 bits
|= this->b_
[i
] << (i
% 8);
3159 if ((i
+ 1) % 8 == 0)
3165 if (this->nb_
% 8 != 0)
3170 // Mark the end of a GC program.
3179 // Return an Expression for the bytes in a GC program.
3182 GCProg::constructor(Gogo
* gogo
) const
3184 Location bloc
= Linemap::predeclared_location();
3186 // The first four bytes are the length of the program in target byte
3187 // order. Build a struct whose first type is uint32 to make this
3190 Type
* uint32_type
= Type::lookup_integer_type("uint32");
3192 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
3193 Expression
* len
= Expression::make_integer_ul(this->bytes_
.size(), NULL
,
3195 Array_type
* at
= Type::make_array_type(byte_type
, len
);
3197 Struct_type
* st
= Type::make_builtin_struct_type(2, "len", uint32_type
,
3200 Expression_list
* vals
= new Expression_list();
3201 vals
->reserve(this->bytes_
.size());
3202 for (std::vector
<unsigned char>::const_iterator p
= this->bytes_
.begin();
3203 p
!= this->bytes_
.end();
3205 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
3206 Expression
* bytes
= Expression::make_array_composite_literal(at
, vals
, bloc
);
3208 vals
= new Expression_list();
3209 vals
->push_back(Expression::make_integer_ul(this->bytes_
.size(), uint32_type
,
3211 vals
->push_back(bytes
);
3213 return Expression::make_struct_composite_literal(st
, vals
, bloc
);
3216 // Return a composite literal for the garbage collection program for
3217 // this type. This is only used for types that are too large to use a
3221 Type::gcprog_constructor(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
3223 Location bloc
= Linemap::predeclared_location();
3226 prog
.set_from(gogo
, this, ptrsize
, 0);
3227 int64_t offset
= prog
.bit_index() * ptrsize
;
3231 if (!this->backend_type_size(gogo
, &type_size
))
3233 go_assert(saw_errors());
3234 return Expression::make_error(bloc
);
3237 go_assert(offset
>= ptrdata
&& offset
<= type_size
);
3239 return prog
.constructor(gogo
);
3242 // Return a composite literal for the uncommon type information for
3243 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3244 // struct. If name is not NULL, it is the name of the type. If
3245 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3246 // is true if only value methods should be included. At least one of
3247 // NAME and METHODS must not be NULL.
3250 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
3251 Named_type
* name
, const Methods
* methods
,
3252 bool only_value_methods
) const
3254 Location bloc
= Linemap::predeclared_location();
3256 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
3258 Expression_list
* vals
= new Expression_list();
3261 Struct_field_list::const_iterator p
= fields
->begin();
3262 go_assert(p
->is_field_name("name"));
3265 go_assert(p
->is_field_name("pkgPath"));
3269 vals
->push_back(Expression::make_nil(bloc
));
3270 vals
->push_back(Expression::make_nil(bloc
));
3274 Named_object
* no
= name
->named_object();
3275 std::string n
= Gogo::unpack_hidden_name(no
->name());
3276 Expression
* s
= Expression::make_string(n
, bloc
);
3277 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3279 if (name
->is_builtin())
3280 vals
->push_back(Expression::make_nil(bloc
));
3283 const Package
* package
= no
->package();
3284 const std::string
& pkgpath(package
== NULL
3286 : package
->pkgpath());
3287 s
= Expression::make_string(pkgpath
, bloc
);
3288 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3293 go_assert(p
->is_field_name("methods"));
3294 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
3295 only_value_methods
));
3298 go_assert(p
== fields
->end());
3300 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
3302 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
3305 // Sort methods by name.
3311 operator()(const std::pair
<std::string
, const Method
*>& m1
,
3312 const std::pair
<std::string
, const Method
*>& m2
) const
3314 return (Gogo::unpack_hidden_name(m1
.first
)
3315 < Gogo::unpack_hidden_name(m2
.first
));
3319 // Return a composite literal for the type method table for this type.
3320 // METHODS_TYPE is the type of the table, and is a slice type.
3321 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3322 // then only value methods are used.
3325 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
3326 const Methods
* methods
,
3327 bool only_value_methods
) const
3329 Location bloc
= Linemap::predeclared_location();
3331 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
3332 if (methods
!= NULL
)
3334 smethods
.reserve(methods
->count());
3335 for (Methods::const_iterator p
= methods
->begin();
3336 p
!= methods
->end();
3339 if (p
->second
->is_ambiguous())
3341 if (only_value_methods
&& !p
->second
->is_value_method())
3344 // This is where we implement the magic //go:nointerface
3345 // comment. If we saw that comment, we don't add this
3346 // method to the type descriptor.
3347 if (p
->second
->nointerface())
3350 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
3354 if (smethods
.empty())
3355 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
3357 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
3359 Type
* method_type
= methods_type
->array_type()->element_type();
3361 Expression_list
* vals
= new Expression_list();
3362 vals
->reserve(smethods
.size());
3363 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
3365 p
!= smethods
.end();
3367 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
3368 p
->second
, only_value_methods
));
3370 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
3373 // Return a composite literal for a single method. METHOD_TYPE is the
3374 // type of the entry. METHOD_NAME is the name of the method and M is
3375 // the method information.
3378 Type::method_constructor(Gogo
*, Type
* method_type
,
3379 const std::string
& method_name
,
3381 bool only_value_methods
) const
3383 Location bloc
= Linemap::predeclared_location();
3385 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
3387 Expression_list
* vals
= new Expression_list();
3390 Struct_field_list::const_iterator p
= fields
->begin();
3391 go_assert(p
->is_field_name("name"));
3392 const std::string n
= Gogo::unpack_hidden_name(method_name
);
3393 Expression
* s
= Expression::make_string(n
, bloc
);
3394 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3397 go_assert(p
->is_field_name("pkgPath"));
3398 if (!Gogo::is_hidden_name(method_name
))
3399 vals
->push_back(Expression::make_nil(bloc
));
3402 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
3404 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3407 Named_object
* no
= (m
->needs_stub_method()
3409 : m
->named_object());
3411 Function_type
* mtype
;
3412 if (no
->is_function())
3413 mtype
= no
->func_value()->type();
3415 mtype
= no
->func_declaration_value()->type();
3416 go_assert(mtype
->is_method());
3417 Type
* nonmethod_type
= mtype
->copy_without_receiver();
3420 go_assert(p
->is_field_name("mtyp"));
3421 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3424 go_assert(p
->is_field_name("typ"));
3425 bool want_pointer_receiver
= !only_value_methods
&& m
->is_value_method();
3426 nonmethod_type
= mtype
->copy_with_receiver_as_param(want_pointer_receiver
);
3427 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3430 go_assert(p
->is_field_name("tfn"));
3431 vals
->push_back(Expression::make_func_code_reference(no
, bloc
));
3434 go_assert(p
== fields
->end());
3436 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
3439 // Return a composite literal for the type descriptor of a plain type.
3440 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3441 // NULL, it is the name to use as well as the list of methods.
3444 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
3447 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
3451 // Return the type reflection string for this type.
3454 Type::reflection(Gogo
* gogo
) const
3458 // The do_reflection virtual function should set RET to the
3459 // reflection string.
3460 this->do_reflection(gogo
, &ret
);
3465 // Return a mangled name for the type.
3468 Type::mangled_name(Gogo
* gogo
) const
3472 // The do_mangled_name virtual function should set RET to the
3473 // mangled name. For a composite type it should append a code for
3474 // the composition and then call do_mangled_name on the components.
3475 this->do_mangled_name(gogo
, &ret
);
3480 // Return whether the backend size of the type is known.
3483 Type::is_backend_type_size_known(Gogo
* gogo
)
3485 switch (this->classification_
)
3499 case TYPE_INTERFACE
:
3504 const Struct_field_list
* fields
= this->struct_type()->fields();
3505 for (Struct_field_list::const_iterator pf
= fields
->begin();
3506 pf
!= fields
->end();
3508 if (!pf
->type()->is_backend_type_size_known(gogo
))
3515 const Array_type
* at
= this->array_type();
3516 if (at
->length() == NULL
)
3520 Numeric_constant nc
;
3521 if (!at
->length()->numeric_constant_value(&nc
))
3524 if (!nc
.to_int(&ival
))
3527 return at
->element_type()->is_backend_type_size_known(gogo
);
3532 this->named_type()->convert(gogo
);
3533 return this->named_type()->is_named_backend_type_size_known();
3537 Forward_declaration_type
* fdt
= this->forward_declaration_type();
3538 return fdt
->real_type()->is_backend_type_size_known(gogo
);
3542 case TYPE_CALL_MULTIPLE_RESULT
:
3550 // If the size of the type can be determined, set *PSIZE to the size
3551 // in bytes and return true. Otherwise, return false. This queries
3555 Type::backend_type_size(Gogo
* gogo
, int64_t *psize
)
3557 if (!this->is_backend_type_size_known(gogo
))
3559 if (this->is_error_type())
3561 Btype
* bt
= this->get_backend_placeholder(gogo
);
3562 *psize
= gogo
->backend()->type_size(bt
);
3565 if (this->named_type() != NULL
)
3566 go_error_at(this->named_type()->location(),
3567 "type %s larger than address space",
3568 Gogo::message_name(this->named_type()->name()).c_str());
3570 go_error_at(Linemap::unknown_location(),
3571 "type %s larger than address space",
3572 this->reflection(gogo
).c_str());
3574 // Make this an error type to avoid knock-on errors.
3575 this->classification_
= TYPE_ERROR
;
3581 // If the alignment of the type can be determined, set *PALIGN to
3582 // the alignment in bytes and return true. Otherwise, return false.
3585 Type::backend_type_align(Gogo
* gogo
, int64_t *palign
)
3587 if (!this->is_backend_type_size_known(gogo
))
3589 Btype
* bt
= this->get_backend_placeholder(gogo
);
3590 *palign
= gogo
->backend()->type_alignment(bt
);
3594 // Like backend_type_align, but return the alignment when used as a
3598 Type::backend_type_field_align(Gogo
* gogo
, int64_t *palign
)
3600 if (!this->is_backend_type_size_known(gogo
))
3602 Btype
* bt
= this->get_backend_placeholder(gogo
);
3603 *palign
= gogo
->backend()->type_field_alignment(bt
);
3607 // Get the ptrdata value for a type. This is the size of the prefix
3608 // of the type that contains all pointers. Store the ptrdata in
3609 // *PPTRDATA and return whether we found it.
3612 Type::backend_type_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3616 if (!this->has_pointer())
3619 if (!this->is_backend_type_size_known(gogo
))
3622 switch (this->classification_
)
3631 // These types are nothing but a pointer.
3632 return this->backend_type_size(gogo
, pptrdata
);
3634 case TYPE_INTERFACE
:
3635 // An interface is a struct of two pointers.
3636 return this->backend_type_size(gogo
, pptrdata
);
3640 // A string is a struct whose first field is a pointer, and
3641 // whose second field is not.
3642 Type
* uint8_type
= Type::lookup_integer_type("uint8");
3643 Type
* ptr
= Type::make_pointer_type(uint8_type
);
3644 return ptr
->backend_type_size(gogo
, pptrdata
);
3649 return this->base()->backend_type_ptrdata(gogo
, pptrdata
);
3653 const Struct_field_list
* fields
= this->struct_type()->fields();
3655 const Struct_field
*ptr
= NULL
;
3656 int64_t ptr_offset
= 0;
3657 for (Struct_field_list::const_iterator pf
= fields
->begin();
3658 pf
!= fields
->end();
3661 int64_t field_align
;
3662 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
3664 offset
= (offset
+ (field_align
- 1)) &~ (field_align
- 1);
3666 if (pf
->type()->has_pointer())
3669 ptr_offset
= offset
;
3673 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
3675 offset
+= field_size
;
3680 int64_t ptr_ptrdata
;
3681 if (!ptr
->type()->backend_type_ptrdata(gogo
, &ptr_ptrdata
))
3683 *pptrdata
= ptr_offset
+ ptr_ptrdata
;
3689 if (this->is_slice_type())
3691 // A slice is a struct whose first field is a pointer, and
3692 // whose remaining fields are not.
3693 Type
* element_type
= this->array_type()->element_type();
3694 Type
* ptr
= Type::make_pointer_type(element_type
);
3695 return ptr
->backend_type_size(gogo
, pptrdata
);
3699 Numeric_constant nc
;
3700 if (!this->array_type()->length()->numeric_constant_value(&nc
))
3703 if (!nc
.to_memory_size(&len
))
3706 Type
* element_type
= this->array_type()->element_type();
3708 int64_t ele_ptrdata
;
3709 if (!element_type
->backend_type_size(gogo
, &ele_size
)
3710 || !element_type
->backend_type_ptrdata(gogo
, &ele_ptrdata
))
3712 go_assert(ele_size
> 0 && ele_ptrdata
> 0);
3714 *pptrdata
= (len
- 1) * ele_size
+ ele_ptrdata
;
3726 case TYPE_CALL_MULTIPLE_RESULT
:
3731 // Get the ptrdata value to store in a type descriptor. This is
3732 // normally the same as backend_type_ptrdata, but for a type that is
3733 // large enough to use a gcprog we may need to store a different value
3734 // if it ends with an array. If the gcprog uses a repeat descriptor
3735 // for the array, and if the array element ends with non-pointer data,
3736 // then the gcprog will produce a value that describes the complete
3737 // array where the backend ptrdata will omit the non-pointer elements
3738 // of the final array element. This is a subtle difference but the
3739 // run time code checks it to verify that it has expanded a gcprog as
3743 Type::descriptor_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3745 int64_t backend_ptrdata
;
3746 if (!this->backend_type_ptrdata(gogo
, &backend_ptrdata
))
3750 if (!this->needs_gcprog(gogo
, &ptrsize
, &backend_ptrdata
))
3752 *pptrdata
= backend_ptrdata
;
3757 prog
.set_from(gogo
, this, ptrsize
, 0);
3758 int64_t offset
= prog
.bit_index() * ptrsize
;
3760 go_assert(offset
>= backend_ptrdata
);
3765 // Default function to export a type.
3768 Type::do_export(Export
*) const
3776 Type::import_type(Import
* imp
)
3778 if (imp
->match_c_string("("))
3779 return Function_type::do_import(imp
);
3780 else if (imp
->match_c_string("*"))
3781 return Pointer_type::do_import(imp
);
3782 else if (imp
->match_c_string("struct "))
3783 return Struct_type::do_import(imp
);
3784 else if (imp
->match_c_string("["))
3785 return Array_type::do_import(imp
);
3786 else if (imp
->match_c_string("map "))
3787 return Map_type::do_import(imp
);
3788 else if (imp
->match_c_string("chan "))
3789 return Channel_type::do_import(imp
);
3790 else if (imp
->match_c_string("interface"))
3791 return Interface_type::do_import(imp
);
3794 go_error_at(imp
->location(), "import error: expected type");
3795 return Type::make_error_type();
3799 // A type used to indicate a parsing error. This exists to simplify
3800 // later error detection.
3802 class Error_type
: public Type
3811 do_compare_is_identity(Gogo
*)
3815 do_get_backend(Gogo
* gogo
)
3816 { return gogo
->backend()->error_type(); }
3819 do_type_descriptor(Gogo
*, Named_type
*)
3820 { return Expression::make_error(Linemap::predeclared_location()); }
3823 do_reflection(Gogo
*, std::string
*) const
3824 { go_assert(saw_errors()); }
3827 do_mangled_name(Gogo
*, std::string
* ret
) const
3828 { ret
->push_back('E'); }
3832 Type::make_error_type()
3834 static Error_type singleton_error_type
;
3835 return &singleton_error_type
;
3840 class Void_type
: public Type
3849 do_compare_is_identity(Gogo
*)
3853 do_get_backend(Gogo
* gogo
)
3854 { return gogo
->backend()->void_type(); }
3857 do_type_descriptor(Gogo
*, Named_type
*)
3858 { go_unreachable(); }
3861 do_reflection(Gogo
*, std::string
*) const
3865 do_mangled_name(Gogo
*, std::string
* ret
) const
3866 { ret
->push_back('v'); }
3870 Type::make_void_type()
3872 static Void_type singleton_void_type
;
3873 return &singleton_void_type
;
3876 // The boolean type.
3878 class Boolean_type
: public Type
3882 : Type(TYPE_BOOLEAN
)
3887 do_compare_is_identity(Gogo
*)
3891 do_get_backend(Gogo
* gogo
)
3892 { return gogo
->backend()->bool_type(); }
3895 do_type_descriptor(Gogo
*, Named_type
* name
);
3897 // We should not be asked for the reflection string of a basic type.
3899 do_reflection(Gogo
*, std::string
* ret
) const
3900 { ret
->append("bool"); }
3903 do_mangled_name(Gogo
*, std::string
* ret
) const
3904 { ret
->push_back('b'); }
3907 // Make the type descriptor.
3910 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3913 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
3916 Named_object
* no
= gogo
->lookup_global("bool");
3917 go_assert(no
!= NULL
);
3918 return Type::type_descriptor(gogo
, no
->type_value());
3923 Type::make_boolean_type()
3925 static Boolean_type boolean_type
;
3926 return &boolean_type
;
3929 // The named type "bool".
3931 static Named_type
* named_bool_type
;
3933 // Get the named type "bool".
3936 Type::lookup_bool_type()
3938 return named_bool_type
;
3941 // Make the named type "bool".
3944 Type::make_named_bool_type()
3946 Type
* bool_type
= Type::make_boolean_type();
3947 Named_object
* named_object
=
3948 Named_object::make_type("bool", NULL
, bool_type
,
3949 Linemap::predeclared_location());
3950 Named_type
* named_type
= named_object
->type_value();
3951 named_bool_type
= named_type
;
3955 // Class Integer_type.
3957 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
3959 // Create a new integer type. Non-abstract integer types always have
3963 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
3964 int bits
, int runtime_type_kind
)
3966 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
3968 std::string
sname(name
);
3969 Named_object
* named_object
=
3970 Named_object::make_type(sname
, NULL
, integer_type
,
3971 Linemap::predeclared_location());
3972 Named_type
* named_type
= named_object
->type_value();
3973 std::pair
<Named_integer_types::iterator
, bool> ins
=
3974 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
3975 go_assert(ins
.second
);
3979 // Look up an existing integer type.
3982 Integer_type::lookup_integer_type(const char* name
)
3984 Named_integer_types::const_iterator p
=
3985 Integer_type::named_integer_types
.find(name
);
3986 go_assert(p
!= Integer_type::named_integer_types
.end());
3990 // Create a new abstract integer type.
3993 Integer_type::create_abstract_integer_type()
3995 static Integer_type
* abstract_type
;
3996 if (abstract_type
== NULL
)
3998 Type
* int_type
= Type::lookup_integer_type("int");
3999 abstract_type
= new Integer_type(true, false,
4000 int_type
->integer_type()->bits(),
4001 RUNTIME_TYPE_KIND_INT
);
4003 return abstract_type
;
4006 // Create a new abstract character type.
4009 Integer_type::create_abstract_character_type()
4011 static Integer_type
* abstract_type
;
4012 if (abstract_type
== NULL
)
4014 abstract_type
= new Integer_type(true, false, 32,
4015 RUNTIME_TYPE_KIND_INT32
);
4016 abstract_type
->set_is_rune();
4018 return abstract_type
;
4021 // Integer type compatibility.
4024 Integer_type::is_identical(const Integer_type
* t
) const
4026 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
4028 return this->is_abstract_
== t
->is_abstract_
;
4034 Integer_type::do_hash_for_method(Gogo
*) const
4036 return ((this->bits_
<< 4)
4037 + ((this->is_unsigned_
? 1 : 0) << 8)
4038 + ((this->is_abstract_
? 1 : 0) << 9));
4041 // Convert an Integer_type to the backend representation.
4044 Integer_type::do_get_backend(Gogo
* gogo
)
4046 if (this->is_abstract_
)
4048 go_assert(saw_errors());
4049 return gogo
->backend()->error_type();
4051 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
4054 // The type descriptor for an integer type. Integer types are always
4058 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4060 go_assert(name
!= NULL
|| saw_errors());
4061 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4064 // We should not be asked for the reflection string of a basic type.
4067 Integer_type::do_reflection(Gogo
*, std::string
*) const
4069 go_assert(saw_errors());
4075 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
4078 snprintf(buf
, sizeof buf
, "i%s%s%de",
4079 this->is_abstract_
? "a" : "",
4080 this->is_unsigned_
? "u" : "",
4085 // Make an integer type.
4088 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
4089 int runtime_type_kind
)
4091 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
4095 // Make an abstract integer type.
4098 Type::make_abstract_integer_type()
4100 return Integer_type::create_abstract_integer_type();
4103 // Make an abstract character type.
4106 Type::make_abstract_character_type()
4108 return Integer_type::create_abstract_character_type();
4111 // Look up an integer type.
4114 Type::lookup_integer_type(const char* name
)
4116 return Integer_type::lookup_integer_type(name
);
4119 // Class Float_type.
4121 Float_type::Named_float_types
Float_type::named_float_types
;
4123 // Create a new float type. Non-abstract float types always have
4127 Float_type::create_float_type(const char* name
, int bits
,
4128 int runtime_type_kind
)
4130 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
4131 std::string
sname(name
);
4132 Named_object
* named_object
=
4133 Named_object::make_type(sname
, NULL
, float_type
,
4134 Linemap::predeclared_location());
4135 Named_type
* named_type
= named_object
->type_value();
4136 std::pair
<Named_float_types::iterator
, bool> ins
=
4137 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
4138 go_assert(ins
.second
);
4142 // Look up an existing float type.
4145 Float_type::lookup_float_type(const char* name
)
4147 Named_float_types::const_iterator p
=
4148 Float_type::named_float_types
.find(name
);
4149 go_assert(p
!= Float_type::named_float_types
.end());
4153 // Create a new abstract float type.
4156 Float_type::create_abstract_float_type()
4158 static Float_type
* abstract_type
;
4159 if (abstract_type
== NULL
)
4160 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
4161 return abstract_type
;
4164 // Whether this type is identical with T.
4167 Float_type::is_identical(const Float_type
* t
) const
4169 if (this->bits_
!= t
->bits_
)
4171 return this->is_abstract_
== t
->is_abstract_
;
4177 Float_type::do_hash_for_method(Gogo
*) const
4179 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4182 // Convert to the backend representation.
4185 Float_type::do_get_backend(Gogo
* gogo
)
4187 return gogo
->backend()->float_type(this->bits_
);
4190 // The type descriptor for a float type. Float types are always named.
4193 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4195 go_assert(name
!= NULL
|| saw_errors());
4196 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4199 // We should not be asked for the reflection string of a basic type.
4202 Float_type::do_reflection(Gogo
*, std::string
*) const
4204 go_assert(saw_errors());
4210 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
4213 snprintf(buf
, sizeof buf
, "f%s%de",
4214 this->is_abstract_
? "a" : "",
4219 // Make a floating point type.
4222 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
4224 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
4227 // Make an abstract float type.
4230 Type::make_abstract_float_type()
4232 return Float_type::create_abstract_float_type();
4235 // Look up a float type.
4238 Type::lookup_float_type(const char* name
)
4240 return Float_type::lookup_float_type(name
);
4243 // Class Complex_type.
4245 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
4247 // Create a new complex type. Non-abstract complex types always have
4251 Complex_type::create_complex_type(const char* name
, int bits
,
4252 int runtime_type_kind
)
4254 Complex_type
* complex_type
= new Complex_type(false, bits
,
4256 std::string
sname(name
);
4257 Named_object
* named_object
=
4258 Named_object::make_type(sname
, NULL
, complex_type
,
4259 Linemap::predeclared_location());
4260 Named_type
* named_type
= named_object
->type_value();
4261 std::pair
<Named_complex_types::iterator
, bool> ins
=
4262 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
4264 go_assert(ins
.second
);
4268 // Look up an existing complex type.
4271 Complex_type::lookup_complex_type(const char* name
)
4273 Named_complex_types::const_iterator p
=
4274 Complex_type::named_complex_types
.find(name
);
4275 go_assert(p
!= Complex_type::named_complex_types
.end());
4279 // Create a new abstract complex type.
4282 Complex_type::create_abstract_complex_type()
4284 static Complex_type
* abstract_type
;
4285 if (abstract_type
== NULL
)
4286 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
4287 return abstract_type
;
4290 // Whether this type is identical with T.
4293 Complex_type::is_identical(const Complex_type
*t
) const
4295 if (this->bits_
!= t
->bits_
)
4297 return this->is_abstract_
== t
->is_abstract_
;
4303 Complex_type::do_hash_for_method(Gogo
*) const
4305 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4308 // Convert to the backend representation.
4311 Complex_type::do_get_backend(Gogo
* gogo
)
4313 return gogo
->backend()->complex_type(this->bits_
);
4316 // The type descriptor for a complex type. Complex types are always
4320 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4322 go_assert(name
!= NULL
|| saw_errors());
4323 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4326 // We should not be asked for the reflection string of a basic type.
4329 Complex_type::do_reflection(Gogo
*, std::string
*) const
4331 go_assert(saw_errors());
4337 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
4340 snprintf(buf
, sizeof buf
, "c%s%de",
4341 this->is_abstract_
? "a" : "",
4346 // Make a complex type.
4349 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
4351 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
4354 // Make an abstract complex type.
4357 Type::make_abstract_complex_type()
4359 return Complex_type::create_abstract_complex_type();
4362 // Look up a complex type.
4365 Type::lookup_complex_type(const char* name
)
4367 return Complex_type::lookup_complex_type(name
);
4370 // Class String_type.
4372 // Convert String_type to the backend representation. A string is a
4373 // struct with two fields: a pointer to the characters and a length.
4376 String_type::do_get_backend(Gogo
* gogo
)
4378 static Btype
* backend_string_type
;
4379 if (backend_string_type
== NULL
)
4381 std::vector
<Backend::Btyped_identifier
> fields(2);
4383 Type
* b
= gogo
->lookup_global("byte")->type_value();
4384 Type
* pb
= Type::make_pointer_type(b
);
4386 // We aren't going to get back to this field to finish the
4387 // backend representation, so force it to be finished now.
4388 if (!gogo
->named_types_are_converted())
4390 Btype
* bt
= pb
->get_backend_placeholder(gogo
);
4391 pb
->finish_backend(gogo
, bt
);
4394 fields
[0].name
= "__data";
4395 fields
[0].btype
= pb
->get_backend(gogo
);
4396 fields
[0].location
= Linemap::predeclared_location();
4398 Type
* int_type
= Type::lookup_integer_type("int");
4399 fields
[1].name
= "__length";
4400 fields
[1].btype
= int_type
->get_backend(gogo
);
4401 fields
[1].location
= fields
[0].location
;
4403 backend_string_type
= gogo
->backend()->struct_type(fields
);
4405 return backend_string_type
;
4408 // The type descriptor for the string type.
4411 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4414 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
4417 Named_object
* no
= gogo
->lookup_global("string");
4418 go_assert(no
!= NULL
);
4419 return Type::type_descriptor(gogo
, no
->type_value());
4423 // We should not be asked for the reflection string of a basic type.
4426 String_type::do_reflection(Gogo
*, std::string
* ret
) const
4428 ret
->append("string");
4431 // Mangled name of a string type.
4434 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
4436 ret
->push_back('z');
4439 // Make a string type.
4442 Type::make_string_type()
4444 static String_type string_type
;
4445 return &string_type
;
4448 // The named type "string".
4450 static Named_type
* named_string_type
;
4452 // Get the named type "string".
4455 Type::lookup_string_type()
4457 return named_string_type
;
4460 // Make the named type string.
4463 Type::make_named_string_type()
4465 Type
* string_type
= Type::make_string_type();
4466 Named_object
* named_object
=
4467 Named_object::make_type("string", NULL
, string_type
,
4468 Linemap::predeclared_location());
4469 Named_type
* named_type
= named_object
->type_value();
4470 named_string_type
= named_type
;
4474 // The sink type. This is the type of the blank identifier _. Any
4475 // type may be assigned to it.
4477 class Sink_type
: public Type
4486 do_compare_is_identity(Gogo
*)
4490 do_get_backend(Gogo
*)
4491 { go_unreachable(); }
4494 do_type_descriptor(Gogo
*, Named_type
*)
4495 { go_unreachable(); }
4498 do_reflection(Gogo
*, std::string
*) const
4499 { go_unreachable(); }
4502 do_mangled_name(Gogo
*, std::string
*) const
4503 { go_unreachable(); }
4506 // Make the sink type.
4509 Type::make_sink_type()
4511 static Sink_type sink_type
;
4515 // Class Function_type.
4520 Function_type::do_traverse(Traverse
* traverse
)
4522 if (this->receiver_
!= NULL
4523 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
4524 return TRAVERSE_EXIT
;
4525 if (this->parameters_
!= NULL
4526 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
4527 return TRAVERSE_EXIT
;
4528 if (this->results_
!= NULL
4529 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
4530 return TRAVERSE_EXIT
;
4531 return TRAVERSE_CONTINUE
;
4534 // Returns whether T is a valid redeclaration of this type. If this
4535 // returns false, and REASON is not NULL, *REASON may be set to a
4536 // brief explanation of why it returned false.
4539 Function_type::is_valid_redeclaration(const Function_type
* t
,
4540 std::string
* reason
) const
4542 if (!this->is_identical(t
, false, COMPARE_TAGS
, true, reason
))
4545 // A redeclaration of a function is required to use the same names
4546 // for the receiver and parameters.
4547 if (this->receiver() != NULL
4548 && this->receiver()->name() != t
->receiver()->name())
4551 *reason
= "receiver name changed";
4555 const Typed_identifier_list
* parms1
= this->parameters();
4556 const Typed_identifier_list
* parms2
= t
->parameters();
4559 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4560 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4561 p2
!= parms2
->end();
4564 if (p1
->name() != p2
->name())
4567 *reason
= "parameter name changed";
4571 // This is called at parse time, so we may have unknown
4573 Type
* t1
= p1
->type()->forwarded();
4574 Type
* t2
= p2
->type()->forwarded();
4576 && t1
->forward_declaration_type() != NULL
4577 && (t2
->forward_declaration_type() == NULL
4578 || (t1
->forward_declaration_type()->named_object()
4579 != t2
->forward_declaration_type()->named_object())))
4584 const Typed_identifier_list
* results1
= this->results();
4585 const Typed_identifier_list
* results2
= t
->results();
4586 if (results1
!= NULL
)
4588 Typed_identifier_list::const_iterator res1
= results1
->begin();
4589 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4590 res2
!= results2
->end();
4593 if (res1
->name() != res2
->name())
4596 *reason
= "result name changed";
4600 // This is called at parse time, so we may have unknown
4602 Type
* t1
= res1
->type()->forwarded();
4603 Type
* t2
= res2
->type()->forwarded();
4605 && t1
->forward_declaration_type() != NULL
4606 && (t2
->forward_declaration_type() == NULL
4607 || (t1
->forward_declaration_type()->named_object()
4608 != t2
->forward_declaration_type()->named_object())))
4616 // Check whether T is the same as this type.
4619 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
4620 Cmp_tags cmp_tags
, bool errors_are_identical
,
4621 std::string
* reason
) const
4623 if (!ignore_receiver
)
4625 const Typed_identifier
* r1
= this->receiver();
4626 const Typed_identifier
* r2
= t
->receiver();
4627 if ((r1
!= NULL
) != (r2
!= NULL
))
4630 *reason
= _("different receiver types");
4635 if (!Type::are_identical_cmp_tags(r1
->type(), r2
->type(), cmp_tags
,
4636 errors_are_identical
, reason
))
4638 if (reason
!= NULL
&& !reason
->empty())
4639 *reason
= "receiver: " + *reason
;
4645 const Typed_identifier_list
* parms1
= this->parameters();
4646 const Typed_identifier_list
* parms2
= t
->parameters();
4647 if ((parms1
!= NULL
) != (parms2
!= NULL
))
4650 *reason
= _("different number of parameters");
4655 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4656 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4657 p2
!= parms2
->end();
4660 if (p1
== parms1
->end())
4663 *reason
= _("different number of parameters");
4667 if (!Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
4668 errors_are_identical
, NULL
))
4671 *reason
= _("different parameter types");
4675 if (p1
!= parms1
->end())
4678 *reason
= _("different number of parameters");
4683 if (this->is_varargs() != t
->is_varargs())
4686 *reason
= _("different varargs");
4690 const Typed_identifier_list
* results1
= this->results();
4691 const Typed_identifier_list
* results2
= t
->results();
4692 if ((results1
!= NULL
) != (results2
!= NULL
))
4695 *reason
= _("different number of results");
4698 if (results1
!= NULL
)
4700 Typed_identifier_list::const_iterator res1
= results1
->begin();
4701 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4702 res2
!= results2
->end();
4705 if (res1
== results1
->end())
4708 *reason
= _("different number of results");
4712 if (!Type::are_identical_cmp_tags(res1
->type(), res2
->type(),
4713 cmp_tags
, errors_are_identical
,
4717 *reason
= _("different result types");
4721 if (res1
!= results1
->end())
4724 *reason
= _("different number of results");
4735 Function_type::do_hash_for_method(Gogo
* gogo
) const
4737 unsigned int ret
= 0;
4738 // We ignore the receiver type for hash codes, because we need to
4739 // get the same hash code for a method in an interface and a method
4740 // declared for a type. The former will not have a receiver.
4741 if (this->parameters_
!= NULL
)
4744 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
4745 p
!= this->parameters_
->end();
4747 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4749 if (this->results_
!= NULL
)
4752 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
4753 p
!= this->results_
->end();
4755 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4757 if (this->is_varargs_
)
4763 // Hash result parameters.
4766 Function_type::Results_hash::operator()(const Typed_identifier_list
* t
) const
4768 unsigned int hash
= 0;
4769 for (Typed_identifier_list::const_iterator p
= t
->begin();
4774 hash
= Type::hash_string(p
->name(), hash
);
4775 hash
+= p
->type()->hash_for_method(NULL
);
4780 // Compare result parameters so that can map identical result
4781 // parameters to a single struct type.
4784 Function_type::Results_equal::operator()(const Typed_identifier_list
* a
,
4785 const Typed_identifier_list
* b
) const
4787 if (a
->size() != b
->size())
4789 Typed_identifier_list::const_iterator pa
= a
->begin();
4790 for (Typed_identifier_list::const_iterator pb
= b
->begin();
4794 if (pa
->name() != pb
->name()
4795 || !Type::are_identical(pa
->type(), pb
->type(), true, NULL
))
4801 // Hash from results to a backend struct type.
4803 Function_type::Results_structs
Function_type::results_structs
;
4805 // Get the backend representation for a function type.
4808 Function_type::get_backend_fntype(Gogo
* gogo
)
4810 if (this->fnbtype_
== NULL
)
4812 Backend::Btyped_identifier breceiver
;
4813 if (this->receiver_
!= NULL
)
4815 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
4817 // We always pass the address of the receiver parameter, in
4818 // order to make interface calls work with unknown types.
4819 Type
* rtype
= this->receiver_
->type();
4820 if (rtype
->points_to() == NULL
)
4821 rtype
= Type::make_pointer_type(rtype
);
4822 breceiver
.btype
= rtype
->get_backend(gogo
);
4823 breceiver
.location
= this->receiver_
->location();
4826 std::vector
<Backend::Btyped_identifier
> bparameters
;
4827 if (this->parameters_
!= NULL
)
4829 bparameters
.resize(this->parameters_
->size());
4831 for (Typed_identifier_list::const_iterator p
=
4832 this->parameters_
->begin(); p
!= this->parameters_
->end();
4835 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4836 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
4837 bparameters
[i
].location
= p
->location();
4839 go_assert(i
== bparameters
.size());
4842 std::vector
<Backend::Btyped_identifier
> bresults
;
4843 Btype
* bresult_struct
= NULL
;
4844 if (this->results_
!= NULL
)
4846 bresults
.resize(this->results_
->size());
4848 for (Typed_identifier_list::const_iterator p
=
4849 this->results_
->begin();
4850 p
!= this->results_
->end();
4853 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4854 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
4855 bresults
[i
].location
= p
->location();
4857 go_assert(i
== bresults
.size());
4859 if (this->results_
->size() > 1)
4861 // Use the same results struct for all functions that
4862 // return the same set of results. This is useful to
4863 // unify calls to interface methods with other calls.
4864 std::pair
<Typed_identifier_list
*, Btype
*> val
;
4865 val
.first
= this->results_
;
4867 std::pair
<Results_structs::iterator
, bool> ins
=
4868 Function_type::results_structs
.insert(val
);
4871 // Build a new struct type.
4872 Struct_field_list
* sfl
= new Struct_field_list
;
4873 for (Typed_identifier_list::const_iterator p
=
4874 this->results_
->begin();
4875 p
!= this->results_
->end();
4878 Typed_identifier tid
= *p
;
4879 if (tid
.name().empty())
4880 tid
= Typed_identifier("UNNAMED", tid
.type(),
4882 sfl
->push_back(Struct_field(tid
));
4884 Struct_type
* st
= Type::make_struct_type(sfl
,
4886 st
->set_is_struct_incomparable();
4887 ins
.first
->second
= st
->get_backend(gogo
);
4889 bresult_struct
= ins
.first
->second
;
4893 this->fnbtype_
= gogo
->backend()->function_type(breceiver
, bparameters
,
4894 bresults
, bresult_struct
,
4899 return this->fnbtype_
;
4902 // Get the backend representation for a Go function type.
4905 Function_type::do_get_backend(Gogo
* gogo
)
4907 // When we do anything with a function value other than call it, it
4908 // is represented as a pointer to a struct whose first field is the
4909 // actual function. So that is what we return as the type of a Go
4912 Location loc
= this->location();
4913 Btype
* struct_type
=
4914 gogo
->backend()->placeholder_struct_type("__go_descriptor", loc
);
4915 Btype
* ptr_struct_type
= gogo
->backend()->pointer_type(struct_type
);
4917 std::vector
<Backend::Btyped_identifier
> fields(1);
4918 fields
[0].name
= "code";
4919 fields
[0].btype
= this->get_backend_fntype(gogo
);
4920 fields
[0].location
= loc
;
4921 if (!gogo
->backend()->set_placeholder_struct_type(struct_type
, fields
))
4922 return gogo
->backend()->error_type();
4923 return ptr_struct_type
;
4926 // The type of a function type descriptor.
4929 Function_type::make_function_type_descriptor_type()
4934 Type
* tdt
= Type::make_type_descriptor_type();
4935 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4937 Type
* bool_type
= Type::lookup_bool_type();
4939 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
4941 Struct_type
* s
= Type::make_builtin_struct_type(4,
4943 "dotdotdot", bool_type
,
4947 ret
= Type::make_builtin_named_type("FuncType", s
);
4953 // The type descriptor for a function type.
4956 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4958 Location bloc
= Linemap::predeclared_location();
4960 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
4962 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
4964 Expression_list
* vals
= new Expression_list();
4967 Struct_field_list::const_iterator p
= fields
->begin();
4968 go_assert(p
->is_field_name("_type"));
4969 vals
->push_back(this->type_descriptor_constructor(gogo
,
4970 RUNTIME_TYPE_KIND_FUNC
,
4974 go_assert(p
->is_field_name("dotdotdot"));
4975 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
4978 go_assert(p
->is_field_name("in"));
4979 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
4980 this->parameters()));
4983 go_assert(p
->is_field_name("out"));
4984 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
4988 go_assert(p
== fields
->end());
4990 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
4993 // Return a composite literal for the parameters or results of a type
4997 Function_type::type_descriptor_params(Type
* params_type
,
4998 const Typed_identifier
* receiver
,
4999 const Typed_identifier_list
* params
)
5001 Location bloc
= Linemap::predeclared_location();
5003 if (receiver
== NULL
&& params
== NULL
)
5004 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
5006 Expression_list
* vals
= new Expression_list();
5007 vals
->reserve((params
== NULL
? 0 : params
->size())
5008 + (receiver
!= NULL
? 1 : 0));
5010 if (receiver
!= NULL
)
5011 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
5015 for (Typed_identifier_list::const_iterator p
= params
->begin();
5018 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
5021 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
5024 // The reflection string.
5027 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5029 // FIXME: Turn this off until we straighten out the type of the
5030 // struct field used in a go statement which calls a method.
5031 // go_assert(this->receiver_ == NULL);
5033 ret
->append("func");
5035 if (this->receiver_
!= NULL
)
5037 ret
->push_back('(');
5038 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
5039 ret
->push_back(')');
5042 ret
->push_back('(');
5043 const Typed_identifier_list
* params
= this->parameters();
5046 bool is_varargs
= this->is_varargs_
;
5047 for (Typed_identifier_list::const_iterator p
= params
->begin();
5051 if (p
!= params
->begin())
5053 if (!is_varargs
|| p
+ 1 != params
->end())
5054 this->append_reflection(p
->type(), gogo
, ret
);
5058 this->append_reflection(p
->type()->array_type()->element_type(),
5063 ret
->push_back(')');
5065 const Typed_identifier_list
* results
= this->results();
5066 if (results
!= NULL
&& !results
->empty())
5068 if (results
->size() == 1)
5069 ret
->push_back(' ');
5072 for (Typed_identifier_list::const_iterator p
= results
->begin();
5073 p
!= results
->end();
5076 if (p
!= results
->begin())
5078 this->append_reflection(p
->type(), gogo
, ret
);
5080 if (results
->size() > 1)
5081 ret
->push_back(')');
5088 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5090 ret
->push_back('F');
5092 if (this->receiver_
!= NULL
)
5094 ret
->push_back('m');
5095 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
5098 const Typed_identifier_list
* params
= this->parameters();
5101 ret
->push_back('p');
5102 for (Typed_identifier_list::const_iterator p
= params
->begin();
5105 this->append_mangled_name(p
->type(), gogo
, ret
);
5106 if (this->is_varargs_
)
5107 ret
->push_back('V');
5108 ret
->push_back('e');
5111 const Typed_identifier_list
* results
= this->results();
5112 if (results
!= NULL
)
5114 ret
->push_back('r');
5115 for (Typed_identifier_list::const_iterator p
= results
->begin();
5116 p
!= results
->end();
5118 this->append_mangled_name(p
->type(), gogo
, ret
);
5119 ret
->push_back('e');
5122 ret
->push_back('e');
5125 // Export a function type.
5128 Function_type::do_export(Export
* exp
) const
5130 // We don't write out the receiver. The only function types which
5131 // should have a receiver are the ones associated with explicitly
5132 // defined methods. For those the receiver type is written out by
5133 // Function::export_func.
5135 exp
->write_c_string("(");
5137 if (this->parameters_
!= NULL
)
5139 bool is_varargs
= this->is_varargs_
;
5140 for (Typed_identifier_list::const_iterator p
=
5141 this->parameters_
->begin();
5142 p
!= this->parameters_
->end();
5148 exp
->write_c_string(", ");
5149 exp
->write_name(p
->name());
5150 exp
->write_c_string(" ");
5151 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
5152 exp
->write_type(p
->type());
5155 exp
->write_c_string("...");
5156 exp
->write_type(p
->type()->array_type()->element_type());
5160 exp
->write_c_string(")");
5162 const Typed_identifier_list
* results
= this->results_
;
5163 if (results
!= NULL
)
5165 exp
->write_c_string(" ");
5166 if (results
->size() == 1 && results
->begin()->name().empty())
5167 exp
->write_type(results
->begin()->type());
5171 exp
->write_c_string("(");
5172 for (Typed_identifier_list::const_iterator p
= results
->begin();
5173 p
!= results
->end();
5179 exp
->write_c_string(", ");
5180 exp
->write_name(p
->name());
5181 exp
->write_c_string(" ");
5182 exp
->write_type(p
->type());
5184 exp
->write_c_string(")");
5189 // Import a function type.
5192 Function_type::do_import(Import
* imp
)
5194 imp
->require_c_string("(");
5195 Typed_identifier_list
* parameters
;
5196 bool is_varargs
= false;
5197 if (imp
->peek_char() == ')')
5201 parameters
= new Typed_identifier_list();
5204 std::string name
= imp
->read_name();
5205 imp
->require_c_string(" ");
5207 if (imp
->match_c_string("..."))
5213 Type
* ptype
= imp
->read_type();
5215 ptype
= Type::make_array_type(ptype
, NULL
);
5216 parameters
->push_back(Typed_identifier(name
, ptype
,
5218 if (imp
->peek_char() != ',')
5220 go_assert(!is_varargs
);
5221 imp
->require_c_string(", ");
5224 imp
->require_c_string(")");
5226 Typed_identifier_list
* results
;
5227 if (imp
->peek_char() != ' ')
5232 results
= new Typed_identifier_list
;
5233 if (imp
->peek_char() != '(')
5235 Type
* rtype
= imp
->read_type();
5236 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
5243 std::string name
= imp
->read_name();
5244 imp
->require_c_string(" ");
5245 Type
* rtype
= imp
->read_type();
5246 results
->push_back(Typed_identifier(name
, rtype
,
5248 if (imp
->peek_char() != ',')
5250 imp
->require_c_string(", ");
5252 imp
->require_c_string(")");
5256 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
5259 ret
->set_is_varargs();
5263 // Make a copy of a function type without a receiver.
5266 Function_type::copy_without_receiver() const
5268 go_assert(this->is_method());
5269 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
5272 if (this->is_varargs())
5273 ret
->set_is_varargs();
5274 if (this->is_builtin())
5275 ret
->set_is_builtin();
5279 // Make a copy of a function type with a receiver.
5282 Function_type::copy_with_receiver(Type
* receiver_type
) const
5284 go_assert(!this->is_method());
5285 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
5287 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
5290 if (this->is_varargs_
)
5291 ret
->set_is_varargs();
5295 // Make a copy of a function type with the receiver as the first
5299 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver
) const
5301 go_assert(this->is_method());
5302 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5303 Type
* rtype
= this->receiver_
->type();
5304 if (want_pointer_receiver
)
5305 rtype
= Type::make_pointer_type(rtype
);
5306 Typed_identifier
receiver(this->receiver_
->name(), rtype
,
5307 this->receiver_
->location());
5308 new_params
->push_back(receiver
);
5309 const Typed_identifier_list
* orig_params
= this->parameters_
;
5310 if (orig_params
!= NULL
&& !orig_params
->empty())
5312 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5313 p
!= orig_params
->end();
5315 new_params
->push_back(*p
);
5317 return Type::make_function_type(NULL
, new_params
, this->results_
,
5321 // Make a copy of a function type ignoring any receiver and adding a
5322 // closure parameter.
5325 Function_type::copy_with_names() const
5327 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5328 const Typed_identifier_list
* orig_params
= this->parameters_
;
5329 if (orig_params
!= NULL
&& !orig_params
->empty())
5333 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5334 p
!= orig_params
->end();
5337 snprintf(buf
, sizeof buf
, "pt.%u", count
);
5339 new_params
->push_back(Typed_identifier(buf
, p
->type(),
5344 const Typed_identifier_list
* orig_results
= this->results_
;
5345 Typed_identifier_list
* new_results
;
5346 if (orig_results
== NULL
|| orig_results
->empty())
5350 new_results
= new Typed_identifier_list();
5351 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
5352 p
!= orig_results
->end();
5354 new_results
->push_back(Typed_identifier("", p
->type(),
5358 return Type::make_function_type(NULL
, new_params
, new_results
,
5362 // Make a function type.
5365 Type::make_function_type(Typed_identifier
* receiver
,
5366 Typed_identifier_list
* parameters
,
5367 Typed_identifier_list
* results
,
5370 return new Function_type(receiver
, parameters
, results
, location
);
5373 // Make a backend function type.
5375 Backend_function_type
*
5376 Type::make_backend_function_type(Typed_identifier
* receiver
,
5377 Typed_identifier_list
* parameters
,
5378 Typed_identifier_list
* results
,
5381 return new Backend_function_type(receiver
, parameters
, results
, location
);
5384 // Class Pointer_type.
5389 Pointer_type::do_traverse(Traverse
* traverse
)
5391 return Type::traverse(this->to_type_
, traverse
);
5397 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
5399 return this->to_type_
->hash_for_method(gogo
) << 4;
5402 // Get the backend representation for a pointer type.
5405 Pointer_type::do_get_backend(Gogo
* gogo
)
5407 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
5408 return gogo
->backend()->pointer_type(to_btype
);
5411 // The type of a pointer type descriptor.
5414 Pointer_type::make_pointer_type_descriptor_type()
5419 Type
* tdt
= Type::make_type_descriptor_type();
5420 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5422 Struct_type
* s
= Type::make_builtin_struct_type(2,
5426 ret
= Type::make_builtin_named_type("PtrType", s
);
5432 // The type descriptor for a pointer type.
5435 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5437 if (this->is_unsafe_pointer_type())
5439 go_assert(name
!= NULL
);
5440 return this->plain_type_descriptor(gogo
,
5441 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
5446 Location bloc
= Linemap::predeclared_location();
5448 const Methods
* methods
;
5449 Type
* deref
= this->points_to();
5450 if (deref
->named_type() != NULL
)
5451 methods
= deref
->named_type()->methods();
5452 else if (deref
->struct_type() != NULL
)
5453 methods
= deref
->struct_type()->methods();
5457 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
5459 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
5461 Expression_list
* vals
= new Expression_list();
5464 Struct_field_list::const_iterator p
= fields
->begin();
5465 go_assert(p
->is_field_name("_type"));
5466 vals
->push_back(this->type_descriptor_constructor(gogo
,
5467 RUNTIME_TYPE_KIND_PTR
,
5468 name
, methods
, false));
5471 go_assert(p
->is_field_name("elem"));
5472 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
5474 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
5478 // Reflection string.
5481 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5483 ret
->push_back('*');
5484 this->append_reflection(this->to_type_
, gogo
, ret
);
5488 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5490 ret
->push_back('p');
5491 this->append_mangled_name(this->to_type_
, gogo
, ret
);
5497 Pointer_type::do_export(Export
* exp
) const
5499 exp
->write_c_string("*");
5500 if (this->is_unsafe_pointer_type())
5501 exp
->write_c_string("any");
5503 exp
->write_type(this->to_type_
);
5509 Pointer_type::do_import(Import
* imp
)
5511 imp
->require_c_string("*");
5512 if (imp
->match_c_string("any"))
5515 return Type::make_pointer_type(Type::make_void_type());
5517 Type
* to
= imp
->read_type();
5518 return Type::make_pointer_type(to
);
5521 // Cache of pointer types. Key is "to" type, value is pointer type
5522 // that points to key.
5524 Type::Pointer_type_table
Type::pointer_types
;
5526 // A list of placeholder pointer types. We keep this so we can ensure
5527 // they are finalized.
5529 std::vector
<Pointer_type
*> Type::placeholder_pointers
;
5531 // Make a pointer type.
5534 Type::make_pointer_type(Type
* to_type
)
5536 Pointer_type_table::const_iterator p
= pointer_types
.find(to_type
);
5537 if (p
!= pointer_types
.end())
5539 Pointer_type
* ret
= new Pointer_type(to_type
);
5540 pointer_types
[to_type
] = ret
;
5544 // This helper is invoked immediately after named types have been
5545 // converted, to clean up any unresolved pointer types remaining in
5546 // the pointer type cache.
5548 // The motivation for this routine: occasionally the compiler creates
5549 // some specific pointer type as part of a lowering operation (ex:
5550 // pointer-to-void), then Type::backend_type_size() is invoked on the
5551 // type (which creates a Btype placeholder for it), that placeholder
5552 // passed somewhere along the line to the back end, but since there is
5553 // no reference to the type in user code, there is never a call to
5554 // Type::finish_backend for the type (hence the Btype remains as an
5555 // unresolved placeholder). Calling this routine will clean up such
5559 Type::finish_pointer_types(Gogo
* gogo
)
5561 // We don't use begin() and end() because it is possible to add new
5562 // placeholder pointer types as we finalized existing ones.
5563 for (size_t i
= 0; i
< Type::placeholder_pointers
.size(); i
++)
5565 Pointer_type
* pt
= Type::placeholder_pointers
[i
];
5566 Type_btypes::iterator tbti
= Type::type_btypes
.find(pt
);
5567 if (tbti
!= Type::type_btypes
.end() && tbti
->second
.is_placeholder
)
5569 pt
->finish_backend(gogo
, tbti
->second
.btype
);
5570 tbti
->second
.is_placeholder
= false;
5575 // The nil type. We use a special type for nil because it is not the
5576 // same as any other type. In C term nil has type void*, but there is
5577 // no such type in Go.
5579 class Nil_type
: public Type
5588 do_compare_is_identity(Gogo
*)
5592 do_get_backend(Gogo
* gogo
)
5593 { return gogo
->backend()->pointer_type(gogo
->backend()->void_type()); }
5596 do_type_descriptor(Gogo
*, Named_type
*)
5597 { go_unreachable(); }
5600 do_reflection(Gogo
*, std::string
*) const
5601 { go_unreachable(); }
5604 do_mangled_name(Gogo
*, std::string
* ret
) const
5605 { ret
->push_back('n'); }
5608 // Make the nil type.
5611 Type::make_nil_type()
5613 static Nil_type singleton_nil_type
;
5614 return &singleton_nil_type
;
5617 // The type of a function call which returns multiple values. This is
5618 // really a struct, but we don't want to confuse a function call which
5619 // returns a struct with a function call which returns multiple
5622 class Call_multiple_result_type
: public Type
5625 Call_multiple_result_type(Call_expression
* call
)
5626 : Type(TYPE_CALL_MULTIPLE_RESULT
),
5632 do_has_pointer() const
5636 do_compare_is_identity(Gogo
*)
5640 do_get_backend(Gogo
* gogo
)
5642 go_assert(saw_errors());
5643 return gogo
->backend()->error_type();
5647 do_type_descriptor(Gogo
*, Named_type
*)
5649 go_assert(saw_errors());
5650 return Expression::make_error(Linemap::unknown_location());
5654 do_reflection(Gogo
*, std::string
*) const
5655 { go_assert(saw_errors()); }
5658 do_mangled_name(Gogo
*, std::string
*) const
5659 { go_assert(saw_errors()); }
5662 // The expression being called.
5663 Call_expression
* call_
;
5666 // Make a call result type.
5669 Type::make_call_multiple_result_type(Call_expression
* call
)
5671 return new Call_multiple_result_type(call
);
5674 // Class Struct_field.
5676 // Get the name of a field.
5679 Struct_field::field_name() const
5681 const std::string
& name(this->typed_identifier_
.name());
5686 // This is called during parsing, before anything is lowered, so
5687 // we have to be pretty careful to avoid dereferencing an
5688 // unknown type name.
5689 Type
* t
= this->typed_identifier_
.type();
5691 if (t
->classification() == Type::TYPE_POINTER
)
5694 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
5695 dt
= ptype
->points_to();
5697 if (dt
->forward_declaration_type() != NULL
)
5698 return dt
->forward_declaration_type()->name();
5699 else if (dt
->named_type() != NULL
)
5701 // Note that this can be an alias name.
5702 return dt
->named_type()->name();
5704 else if (t
->is_error_type() || dt
->is_error_type())
5706 static const std::string error_string
= "*error*";
5707 return error_string
;
5711 // Avoid crashing in the erroneous case where T is named but
5714 if (t
->forward_declaration_type() != NULL
)
5715 return t
->forward_declaration_type()->name();
5716 else if (t
->named_type() != NULL
)
5717 return t
->named_type()->name();
5724 // Return whether this field is named NAME.
5727 Struct_field::is_field_name(const std::string
& name
) const
5729 const std::string
& me(this->typed_identifier_
.name());
5734 Type
* t
= this->typed_identifier_
.type();
5735 if (t
->points_to() != NULL
)
5737 Named_type
* nt
= t
->named_type();
5738 if (nt
!= NULL
&& nt
->name() == name
)
5741 // This is a horrible hack caused by the fact that we don't pack
5742 // the names of builtin types. FIXME.
5743 if (!this->is_imported_
5746 && nt
->name() == Gogo::unpack_hidden_name(name
))
5753 // Return whether this field is an unexported field named NAME.
5756 Struct_field::is_unexported_field_name(Gogo
* gogo
,
5757 const std::string
& name
) const
5759 const std::string
& field_name(this->field_name());
5760 if (Gogo::is_hidden_name(field_name
)
5761 && name
== Gogo::unpack_hidden_name(field_name
)
5762 && gogo
->pack_hidden_name(name
, false) != field_name
)
5765 // Check for the name of a builtin type. This is like the test in
5766 // is_field_name, only there we return false if this->is_imported_,
5767 // and here we return true.
5768 if (this->is_imported_
&& this->is_anonymous())
5770 Type
* t
= this->typed_identifier_
.type();
5771 if (t
->points_to() != NULL
)
5773 Named_type
* nt
= t
->named_type();
5776 && nt
->name() == Gogo::unpack_hidden_name(name
))
5783 // Return whether this field is an embedded built-in type.
5786 Struct_field::is_embedded_builtin(Gogo
* gogo
) const
5788 const std::string
& name(this->field_name());
5789 // We know that a field is an embedded type if it is anonymous.
5790 // We can decide if it is a built-in type by checking to see if it is
5791 // registered globally under the field's name.
5792 // This allows us to distinguish between embedded built-in types and
5793 // embedded types that are aliases to built-in types.
5794 return (this->is_anonymous()
5795 && !Gogo::is_hidden_name(name
)
5796 && gogo
->lookup_global(name
.c_str()) != NULL
);
5799 // Class Struct_type.
5801 // A hash table used to find identical unnamed structs so that they
5802 // share method tables.
5804 Struct_type::Identical_structs
Struct_type::identical_structs
;
5806 // A hash table used to merge method sets for identical unnamed
5809 Struct_type::Struct_method_tables
Struct_type::struct_method_tables
;
5814 Struct_type::do_traverse(Traverse
* traverse
)
5816 Struct_field_list
* fields
= this->fields_
;
5819 for (Struct_field_list::iterator p
= fields
->begin();
5823 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
5824 return TRAVERSE_EXIT
;
5827 return TRAVERSE_CONTINUE
;
5830 // Verify that the struct type is complete and valid.
5833 Struct_type::do_verify()
5835 Struct_field_list
* fields
= this->fields_
;
5838 for (Struct_field_list::iterator p
= fields
->begin();
5842 Type
* t
= p
->type();
5843 if (p
->is_anonymous())
5845 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
5847 go_error_at(p
->location(), "embedded type may not be a pointer");
5848 p
->set_type(Type::make_error_type());
5850 else if (t
->points_to() != NULL
5851 && t
->points_to()->interface_type() != NULL
)
5853 go_error_at(p
->location(),
5854 "embedded type may not be pointer to interface");
5855 p
->set_type(Type::make_error_type());
5862 // Whether this contains a pointer.
5865 Struct_type::do_has_pointer() const
5867 const Struct_field_list
* fields
= this->fields();
5870 for (Struct_field_list::const_iterator p
= fields
->begin();
5874 if (p
->type()->has_pointer())
5880 // Whether this type is identical to T.
5883 Struct_type::is_identical(const Struct_type
* t
, Cmp_tags cmp_tags
,
5884 bool errors_are_identical
) const
5886 if (this->is_struct_incomparable_
!= t
->is_struct_incomparable_
)
5888 const Struct_field_list
* fields1
= this->fields();
5889 const Struct_field_list
* fields2
= t
->fields();
5890 if (fields1
== NULL
|| fields2
== NULL
)
5891 return fields1
== fields2
;
5892 Struct_field_list::const_iterator pf2
= fields2
->begin();
5893 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
5894 pf1
!= fields1
->end();
5897 if (pf2
== fields2
->end())
5899 if (pf1
->field_name() != pf2
->field_name())
5901 if (pf1
->is_anonymous() != pf2
->is_anonymous()
5902 || !Type::are_identical_cmp_tags(pf1
->type(), pf2
->type(), cmp_tags
,
5903 errors_are_identical
, NULL
))
5905 if (cmp_tags
== COMPARE_TAGS
)
5907 if (!pf1
->has_tag())
5914 if (!pf2
->has_tag())
5916 if (pf1
->tag() != pf2
->tag())
5921 if (pf2
!= fields2
->end())
5926 // Whether comparisons of this struct type are simple identity
5930 Struct_type::do_compare_is_identity(Gogo
* gogo
)
5932 const Struct_field_list
* fields
= this->fields_
;
5936 for (Struct_field_list::const_iterator pf
= fields
->begin();
5937 pf
!= fields
->end();
5940 if (Gogo::is_sink_name(pf
->field_name()))
5943 if (!pf
->type()->compare_is_identity(gogo
))
5946 int64_t field_align
;
5947 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
5949 if ((offset
& (field_align
- 1)) != 0)
5951 // This struct has padding. We don't guarantee that that
5952 // padding is zero-initialized for a stack variable, so we
5953 // can't use memcmp to compare struct values.
5958 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
5960 offset
+= field_size
;
5963 int64_t struct_size
;
5964 if (!this->backend_type_size(gogo
, &struct_size
))
5966 if (offset
!= struct_size
)
5968 // Trailing padding may not be zero when on the stack.
5975 // Return whether this struct type is reflexive--whether a value of
5976 // this type is always equal to itself.
5979 Struct_type::do_is_reflexive()
5981 const Struct_field_list
* fields
= this->fields_
;
5984 for (Struct_field_list::const_iterator pf
= fields
->begin();
5985 pf
!= fields
->end();
5988 if (!pf
->type()->is_reflexive())
5994 // Return whether this struct type needs a key update when used as a
5998 Struct_type::do_needs_key_update()
6000 const Struct_field_list
* fields
= this->fields_
;
6003 for (Struct_field_list::const_iterator pf
= fields
->begin();
6004 pf
!= fields
->end();
6007 if (pf
->type()->needs_key_update())
6013 // Return whether this struct type is permitted to be in the heap.
6016 Struct_type::do_in_heap()
6018 const Struct_field_list
* fields
= this->fields_
;
6021 for (Struct_field_list::const_iterator pf
= fields
->begin();
6022 pf
!= fields
->end();
6025 if (!pf
->type()->in_heap())
6031 // Build identity and hash functions for this struct.
6036 Struct_type::do_hash_for_method(Gogo
* gogo
) const
6038 unsigned int ret
= 0;
6039 if (this->fields() != NULL
)
6041 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
6042 pf
!= this->fields()->end();
6044 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
6047 if (this->is_struct_incomparable_
)
6052 // Find the local field NAME.
6055 Struct_type::find_local_field(const std::string
& name
,
6056 unsigned int *pindex
) const
6058 const Struct_field_list
* fields
= this->fields_
;
6062 for (Struct_field_list::const_iterator pf
= fields
->begin();
6063 pf
!= fields
->end();
6066 if (pf
->is_field_name(name
))
6076 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
6078 Field_reference_expression
*
6079 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
6080 Location location
) const
6083 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
6087 // Return an expression for a field, along with the depth at which it
6090 Field_reference_expression
*
6091 Struct_type::field_reference_depth(Expression
* struct_expr
,
6092 const std::string
& name
,
6094 Saw_named_type
* saw
,
6095 unsigned int* depth
) const
6097 const Struct_field_list
* fields
= this->fields_
;
6101 // Look for a field with this name.
6103 for (Struct_field_list::const_iterator pf
= fields
->begin();
6104 pf
!= fields
->end();
6107 if (pf
->is_field_name(name
))
6110 return Expression::make_field_reference(struct_expr
, i
, location
);
6114 // Look for an anonymous field which contains a field with this
6116 unsigned int found_depth
= 0;
6117 Field_reference_expression
* ret
= NULL
;
6119 for (Struct_field_list::const_iterator pf
= fields
->begin();
6120 pf
!= fields
->end();
6123 if (!pf
->is_anonymous())
6126 Struct_type
* st
= pf
->type()->deref()->struct_type();
6130 Saw_named_type
* hold_saw
= saw
;
6131 Saw_named_type saw_here
;
6132 Named_type
* nt
= pf
->type()->named_type();
6134 nt
= pf
->type()->deref()->named_type();
6138 for (q
= saw
; q
!= NULL
; q
= q
->next
)
6142 // If this is an error, it will be reported
6149 saw_here
.next
= saw
;
6154 // Look for a reference using a NULL struct expression. If we
6155 // find one, fill in the struct expression with a reference to
6157 unsigned int subdepth
;
6158 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
6168 if (ret
== NULL
|| subdepth
< found_depth
)
6173 found_depth
= subdepth
;
6174 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
6176 if (pf
->type()->points_to() != NULL
)
6177 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
6178 while (sub
->expr() != NULL
)
6180 sub
= sub
->expr()->deref()->field_reference_expression();
6181 go_assert(sub
!= NULL
);
6183 sub
->set_struct_expression(here
);
6184 sub
->set_implicit(true);
6186 else if (subdepth
> found_depth
)
6190 // We do not handle ambiguity here--it should be handled by
6191 // Type::bind_field_or_method.
6199 *depth
= found_depth
+ 1;
6204 // Return the total number of fields, including embedded fields.
6207 Struct_type::total_field_count() const
6209 if (this->fields_
== NULL
)
6211 unsigned int ret
= 0;
6212 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
6213 pf
!= this->fields_
->end();
6216 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
6219 ret
+= pf
->type()->struct_type()->total_field_count();
6224 // Return whether NAME is an unexported field, for better error reporting.
6227 Struct_type::is_unexported_local_field(Gogo
* gogo
,
6228 const std::string
& name
) const
6230 const Struct_field_list
* fields
= this->fields_
;
6233 for (Struct_field_list::const_iterator pf
= fields
->begin();
6234 pf
!= fields
->end();
6236 if (pf
->is_unexported_field_name(gogo
, name
))
6242 // Finalize the methods of an unnamed struct.
6245 Struct_type::finalize_methods(Gogo
* gogo
)
6247 if (this->all_methods_
!= NULL
)
6250 // It is possible to have multiple identical structs that have
6251 // methods. We want them to share method tables. Otherwise we will
6252 // emit identical methods more than once, which is bad since they
6253 // will even have the same names.
6254 std::pair
<Identical_structs::iterator
, bool> ins
=
6255 Struct_type::identical_structs
.insert(std::make_pair(this, this));
6258 // An identical struct was already entered into the hash table.
6259 // Note that finalize_methods is, fortunately, not recursive.
6260 this->all_methods_
= ins
.first
->second
->all_methods_
;
6264 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6267 // Return the method NAME, or NULL if there isn't one or if it is
6268 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6272 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6274 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6277 // Return a pointer to the interface method table for this type for
6278 // the interface INTERFACE. IS_POINTER is true if this is for a
6282 Struct_type::interface_method_table(Interface_type
* interface
,
6285 std::pair
<Struct_type
*, Struct_type::Struct_method_table_pair
*>
6287 std::pair
<Struct_type::Struct_method_tables::iterator
, bool> ins
=
6288 Struct_type::struct_method_tables
.insert(val
);
6290 Struct_method_table_pair
* smtp
;
6292 smtp
= ins
.first
->second
;
6295 smtp
= new Struct_method_table_pair();
6297 smtp
->second
= NULL
;
6298 ins
.first
->second
= smtp
;
6301 return Type::interface_method_table(this, interface
, is_pointer
,
6302 &smtp
->first
, &smtp
->second
);
6305 // Convert struct fields to the backend representation. This is not
6306 // declared in types.h so that types.h doesn't have to #include
6310 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
6311 bool use_placeholder
,
6312 std::vector
<Backend::Btyped_identifier
>* bfields
)
6314 bfields
->resize(fields
->size());
6316 for (Struct_field_list::const_iterator p
= fields
->begin();
6320 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
6321 (*bfields
)[i
].btype
= (use_placeholder
6322 ? p
->type()->get_backend_placeholder(gogo
)
6323 : p
->type()->get_backend(gogo
));
6324 (*bfields
)[i
].location
= p
->location();
6326 go_assert(i
== fields
->size());
6329 // Get the backend representation for a struct type.
6332 Struct_type::do_get_backend(Gogo
* gogo
)
6334 std::vector
<Backend::Btyped_identifier
> bfields
;
6335 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
6336 return gogo
->backend()->struct_type(bfields
);
6339 // Finish the backend representation of the fields of a struct.
6342 Struct_type::finish_backend_fields(Gogo
* gogo
)
6344 const Struct_field_list
* fields
= this->fields_
;
6347 for (Struct_field_list::const_iterator p
= fields
->begin();
6350 p
->type()->get_backend(gogo
);
6354 // The type of a struct type descriptor.
6357 Struct_type::make_struct_type_descriptor_type()
6362 Type
* tdt
= Type::make_type_descriptor_type();
6363 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6365 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6366 Type
* string_type
= Type::lookup_string_type();
6367 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6370 Type::make_builtin_struct_type(5,
6371 "name", pointer_string_type
,
6372 "pkgPath", pointer_string_type
,
6374 "tag", pointer_string_type
,
6375 "offset", uintptr_type
);
6376 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
6378 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
6380 Struct_type
* s
= Type::make_builtin_struct_type(2,
6382 "fields", slice_type
);
6384 ret
= Type::make_builtin_named_type("StructType", s
);
6390 // Build a type descriptor for a struct type.
6393 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6395 Location bloc
= Linemap::predeclared_location();
6397 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
6399 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
6401 Expression_list
* vals
= new Expression_list();
6404 const Methods
* methods
= this->methods();
6405 // A named struct should not have methods--the methods should attach
6406 // to the named type.
6407 go_assert(methods
== NULL
|| name
== NULL
);
6409 Struct_field_list::const_iterator ps
= fields
->begin();
6410 go_assert(ps
->is_field_name("_type"));
6411 vals
->push_back(this->type_descriptor_constructor(gogo
,
6412 RUNTIME_TYPE_KIND_STRUCT
,
6413 name
, methods
, true));
6416 go_assert(ps
->is_field_name("fields"));
6418 Expression_list
* elements
= new Expression_list();
6419 elements
->reserve(this->fields_
->size());
6420 Type
* element_type
= ps
->type()->array_type()->element_type();
6421 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
6422 pf
!= this->fields_
->end();
6425 const Struct_field_list
* f
= element_type
->struct_type()->fields();
6427 Expression_list
* fvals
= new Expression_list();
6430 Struct_field_list::const_iterator q
= f
->begin();
6431 go_assert(q
->is_field_name("name"));
6432 if (pf
->is_anonymous())
6433 fvals
->push_back(Expression::make_nil(bloc
));
6436 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
6437 Expression
* s
= Expression::make_string(n
, bloc
);
6438 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6442 go_assert(q
->is_field_name("pkgPath"));
6443 bool is_embedded_builtin
= pf
->is_embedded_builtin(gogo
);
6444 if (!Gogo::is_hidden_name(pf
->field_name()) && !is_embedded_builtin
)
6445 fvals
->push_back(Expression::make_nil(bloc
));
6449 if (is_embedded_builtin
)
6450 n
= gogo
->package_name();
6452 n
= Gogo::hidden_name_pkgpath(pf
->field_name());
6453 Expression
* s
= Expression::make_string(n
, bloc
);
6454 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6458 go_assert(q
->is_field_name("typ"));
6459 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
6462 go_assert(q
->is_field_name("tag"));
6464 fvals
->push_back(Expression::make_nil(bloc
));
6467 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
6468 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6472 go_assert(q
->is_field_name("offset"));
6473 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
6475 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
6477 elements
->push_back(v
);
6480 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
6483 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
6486 // Write the hash function for a struct which can not use the identity
6490 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
6491 Function_type
* hash_fntype
,
6492 Function_type
* equal_fntype
)
6494 Location bloc
= Linemap::predeclared_location();
6496 // The pointer to the struct that we are going to hash. This is an
6497 // argument to the hash function we are implementing here.
6498 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
6499 go_assert(key_arg
!= NULL
);
6500 Type
* key_arg_type
= key_arg
->var_value()->type();
6502 // The seed argument to the hash function.
6503 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
6504 go_assert(seed_arg
!= NULL
);
6506 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6508 // Make a temporary to hold the return value, initialized to the seed.
6509 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
6510 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
6512 gogo
->add_statement(retval
);
6514 // Make a temporary to hold the key as a uintptr.
6515 ref
= Expression::make_var_reference(key_arg
, bloc
);
6516 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
6517 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
6519 gogo
->add_statement(key
);
6521 // Loop over the struct fields.
6522 const Struct_field_list
* fields
= this->fields_
;
6523 for (Struct_field_list::const_iterator pf
= fields
->begin();
6524 pf
!= fields
->end();
6527 if (Gogo::is_sink_name(pf
->field_name()))
6530 // Get a pointer to the value of this field.
6531 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
6532 ref
= Expression::make_temporary_reference(key
, bloc
);
6533 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
6535 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
6537 // Get the hash function to use for the type of this field.
6538 Named_object
* hash_fn
;
6539 Named_object
* equal_fn
;
6540 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
6541 equal_fntype
, &hash_fn
, &equal_fn
);
6543 // Call the hash function for the field, passing retval as the seed.
6544 ref
= Expression::make_temporary_reference(retval
, bloc
);
6545 Expression_list
* args
= new Expression_list();
6546 args
->push_back(subkey
);
6547 args
->push_back(ref
);
6548 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
6549 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
6551 // Set retval to the result.
6552 Temporary_reference_expression
* tref
=
6553 Expression::make_temporary_reference(retval
, bloc
);
6554 tref
->set_is_lvalue();
6555 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
6556 gogo
->add_statement(s
);
6559 // Return retval to the caller of the hash function.
6560 Expression_list
* vals
= new Expression_list();
6561 ref
= Expression::make_temporary_reference(retval
, bloc
);
6562 vals
->push_back(ref
);
6563 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6564 gogo
->add_statement(s
);
6567 // Write the equality function for a struct which can not use the
6568 // identity function.
6571 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
6573 Location bloc
= Linemap::predeclared_location();
6575 // The pointers to the structs we are going to compare.
6576 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
6577 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
6578 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
6580 // Build temporaries with the right types.
6581 Type
* pt
= Type::make_pointer_type(name
!= NULL
6582 ? static_cast<Type
*>(name
)
6583 : static_cast<Type
*>(this));
6585 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
6586 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6587 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
6588 gogo
->add_statement(p1
);
6590 ref
= Expression::make_var_reference(key2_arg
, bloc
);
6591 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6592 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
6593 gogo
->add_statement(p2
);
6595 const Struct_field_list
* fields
= this->fields_
;
6596 unsigned int field_index
= 0;
6597 for (Struct_field_list::const_iterator pf
= fields
->begin();
6598 pf
!= fields
->end();
6599 ++pf
, ++field_index
)
6601 if (Gogo::is_sink_name(pf
->field_name()))
6604 // Compare one field in both P1 and P2.
6605 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
6606 f1
= Expression::make_unary(OPERATOR_MULT
, f1
, bloc
);
6607 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
6609 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
6610 f2
= Expression::make_unary(OPERATOR_MULT
, f2
, bloc
);
6611 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
6613 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
6615 // If the values are not equal, return false.
6616 gogo
->start_block(bloc
);
6617 Expression_list
* vals
= new Expression_list();
6618 vals
->push_back(Expression::make_boolean(false, bloc
));
6619 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6620 gogo
->add_statement(s
);
6621 Block
* then_block
= gogo
->finish_block(bloc
);
6623 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
6624 gogo
->add_statement(s
);
6627 // All the fields are equal, so return true.
6628 Expression_list
* vals
= new Expression_list();
6629 vals
->push_back(Expression::make_boolean(true, bloc
));
6630 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6631 gogo
->add_statement(s
);
6634 // Reflection string.
6637 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6639 ret
->append("struct {");
6641 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
6642 p
!= this->fields_
->end();
6645 if (p
!= this->fields_
->begin())
6646 ret
->push_back(';');
6647 ret
->push_back(' ');
6648 if (p
->is_anonymous())
6649 ret
->push_back('?');
6651 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
6652 ret
->push_back(' ');
6653 if (p
->is_anonymous()
6654 && p
->type()->named_type() != NULL
6655 && p
->type()->named_type()->is_alias())
6656 p
->type()->named_type()->append_reflection_type_name(gogo
, true, ret
);
6658 this->append_reflection(p
->type(), gogo
, ret
);
6662 const std::string
& tag(p
->tag());
6664 for (std::string::const_iterator p
= tag
.begin();
6669 ret
->append("\\x00");
6670 else if (*p
== '\n')
6672 else if (*p
== '\t')
6675 ret
->append("\\\"");
6676 else if (*p
== '\\')
6677 ret
->append("\\\\");
6681 ret
->push_back('"');
6685 if (!this->fields_
->empty())
6686 ret
->push_back(' ');
6688 ret
->push_back('}');
6694 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6696 ret
->push_back('S');
6698 const Struct_field_list
* fields
= this->fields_
;
6701 for (Struct_field_list::const_iterator p
= fields
->begin();
6705 if (p
->is_anonymous())
6710 std::string
n(Gogo::mangle_possibly_hidden_name(p
->field_name()));
6712 snprintf(buf
, sizeof buf
, "%u_",
6713 static_cast<unsigned int>(n
.length()));
6718 // For an anonymous field with an alias type, the field name
6719 // is the alias name.
6720 if (p
->is_anonymous()
6721 && p
->type()->named_type() != NULL
6722 && p
->type()->named_type()->is_alias())
6723 p
->type()->named_type()->append_mangled_type_name(gogo
, true, ret
);
6725 this->append_mangled_name(p
->type(), gogo
, ret
);
6728 const std::string
& tag(p
->tag());
6730 for (std::string::const_iterator p
= tag
.begin();
6734 if (ISALNUM(*p
) || *p
== '_')
6739 snprintf(buf
, sizeof buf
, ".%x.",
6740 static_cast<unsigned int>(*p
));
6745 snprintf(buf
, sizeof buf
, "T%u_",
6746 static_cast<unsigned int>(out
.length()));
6753 if (this->is_struct_incomparable_
)
6754 ret
->push_back('x');
6756 ret
->push_back('e');
6759 // If the offset of field INDEX in the backend implementation can be
6760 // determined, set *POFFSET to the offset in bytes and return true.
6761 // Otherwise, return false.
6764 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
6767 if (!this->is_backend_type_size_known(gogo
))
6769 Btype
* bt
= this->get_backend_placeholder(gogo
);
6770 *poffset
= gogo
->backend()->type_field_offset(bt
, index
);
6777 Struct_type::do_export(Export
* exp
) const
6779 exp
->write_c_string("struct { ");
6780 const Struct_field_list
* fields
= this->fields_
;
6781 go_assert(fields
!= NULL
);
6782 for (Struct_field_list::const_iterator p
= fields
->begin();
6786 if (p
->is_anonymous())
6787 exp
->write_string("? ");
6790 exp
->write_string(p
->field_name());
6791 exp
->write_c_string(" ");
6793 exp
->write_type(p
->type());
6797 exp
->write_c_string(" ");
6799 Expression::make_string(p
->tag(), Linemap::predeclared_location());
6800 expr
->export_expression(exp
);
6804 exp
->write_c_string("; ");
6806 exp
->write_c_string("}");
6812 Struct_type::do_import(Import
* imp
)
6814 imp
->require_c_string("struct { ");
6815 Struct_field_list
* fields
= new Struct_field_list
;
6816 if (imp
->peek_char() != '}')
6821 if (imp
->match_c_string("? "))
6825 name
= imp
->read_identifier();
6826 imp
->require_c_string(" ");
6828 Type
* ftype
= imp
->read_type();
6830 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
6831 sf
.set_is_imported();
6833 if (imp
->peek_char() == ' ')
6836 Expression
* expr
= Expression::import_expression(imp
);
6837 String_expression
* sexpr
= expr
->string_expression();
6838 go_assert(sexpr
!= NULL
);
6839 sf
.set_tag(sexpr
->val());
6843 imp
->require_c_string("; ");
6844 fields
->push_back(sf
);
6845 if (imp
->peek_char() == '}')
6849 imp
->require_c_string("}");
6851 return Type::make_struct_type(fields
, imp
->location());
6854 // Whether we can write this struct type to a C header file.
6855 // We can't if any of the fields are structs defined in a different package.
6858 Struct_type::can_write_to_c_header(
6859 std::vector
<const Named_object
*>* requires
,
6860 std::vector
<const Named_object
*>* declare
) const
6862 const Struct_field_list
* fields
= this->fields_
;
6863 if (fields
== NULL
|| fields
->empty())
6866 for (Struct_field_list::const_iterator p
= fields
->begin();
6870 if (p
->is_anonymous())
6872 if (!this->can_write_type_to_c_header(p
->type(), requires
, declare
))
6874 if (Gogo::message_name(p
->field_name()) == "_")
6882 // Whether we can write the type T to a C header file.
6885 Struct_type::can_write_type_to_c_header(
6887 std::vector
<const Named_object
*>* requires
,
6888 std::vector
<const Named_object
*>* declare
) const
6891 switch (t
->classification())
6906 case TYPE_INTERFACE
:
6910 // Don't try to handle a pointer to an array.
6911 if (t
->points_to()->array_type() != NULL
6912 && !t
->points_to()->is_slice_type())
6915 if (t
->points_to()->named_type() != NULL
6916 && t
->points_to()->struct_type() != NULL
)
6917 declare
->push_back(t
->points_to()->named_type()->named_object());
6921 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6924 if (t
->is_slice_type())
6926 return this->can_write_type_to_c_header(t
->array_type()->element_type(),
6931 const Named_object
* no
= t
->named_type()->named_object();
6932 if (no
->package() != NULL
)
6934 if (t
->is_unsafe_pointer_type())
6938 if (t
->struct_type() != NULL
)
6940 requires
->push_back(no
);
6941 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6943 return this->can_write_type_to_c_header(t
->base(), requires
, declare
);
6946 case TYPE_CALL_MULTIPLE_RESULT
:
6954 // Write this struct to a C header file.
6957 Struct_type::write_to_c_header(std::ostream
& os
) const
6959 const Struct_field_list
* fields
= this->fields_
;
6960 for (Struct_field_list::const_iterator p
= fields
->begin();
6965 this->write_field_to_c_header(os
, p
->field_name(), p
->type());
6966 os
<< ';' << std::endl
;
6970 // Write the type of a struct field to a C header file.
6973 Struct_type::write_field_to_c_header(std::ostream
& os
, const std::string
& name
,
6974 const Type
*t
) const
6976 bool print_name
= true;
6978 switch (t
->classification())
6990 const Integer_type
* it
= t
->integer_type();
6991 if (it
->is_unsigned())
6993 os
<< "int" << it
->bits() << "_t";
6998 switch (t
->float_type()->bits())
7012 switch (t
->complex_type()->bits())
7015 os
<< "float _Complex";
7018 os
<< "double _Complex";
7035 std::vector
<const Named_object
*> requires
;
7036 std::vector
<const Named_object
*> declare
;
7037 if (!this->can_write_type_to_c_header(t
->points_to(), &requires
,
7042 this->write_field_to_c_header(os
, "", t
->points_to());
7056 case TYPE_INTERFACE
:
7057 if (t
->interface_type()->is_empty())
7064 os
<< "struct {" << std::endl
;
7065 t
->struct_type()->write_to_c_header(os
);
7070 if (t
->is_slice_type())
7074 const Type
*ele
= t
;
7075 std::vector
<const Type
*> array_types
;
7076 while (ele
->array_type() != NULL
&& !ele
->is_slice_type())
7078 array_types
.push_back(ele
);
7079 ele
= ele
->array_type()->element_type();
7081 this->write_field_to_c_header(os
, "", ele
);
7082 os
<< ' ' << Gogo::message_name(name
);
7084 while (!array_types
.empty())
7086 ele
= array_types
.back();
7087 array_types
.pop_back();
7089 Numeric_constant nc
;
7090 if (!ele
->array_type()->length()->numeric_constant_value(&nc
))
7093 if (!nc
.to_int(&val
))
7095 char* s
= mpz_get_str(NULL
, 10, val
);
7106 const Named_object
* no
= t
->named_type()->named_object();
7107 if (t
->struct_type() != NULL
)
7108 os
<< "struct " << no
->message_name();
7109 else if (t
->is_unsafe_pointer_type())
7111 else if (t
== Type::lookup_integer_type("uintptr"))
7115 this->write_field_to_c_header(os
, name
, t
->base());
7123 case TYPE_CALL_MULTIPLE_RESULT
:
7130 if (print_name
&& !name
.empty())
7131 os
<< ' ' << Gogo::message_name(name
);
7134 // Make a struct type.
7137 Type::make_struct_type(Struct_field_list
* fields
,
7140 return new Struct_type(fields
, location
);
7143 // Class Array_type.
7145 // Store the length of an array as an int64_t into *PLEN. Return
7146 // false if the length can not be determined. This will assert if
7147 // called for a slice.
7150 Array_type::int_length(int64_t* plen
)
7152 go_assert(this->length_
!= NULL
);
7153 Numeric_constant nc
;
7154 if (!this->length_
->numeric_constant_value(&nc
))
7156 return nc
.to_memory_size(plen
);
7159 // Whether two array types are identical.
7162 Array_type::is_identical(const Array_type
* t
, Cmp_tags cmp_tags
,
7163 bool errors_are_identical
) const
7165 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
7166 cmp_tags
, errors_are_identical
, NULL
))
7169 if (this->is_array_incomparable_
!= t
->is_array_incomparable_
)
7172 Expression
* l1
= this->length();
7173 Expression
* l2
= t
->length();
7175 // Slices of the same element type are identical.
7176 if (l1
== NULL
&& l2
== NULL
)
7179 // Arrays of the same element type are identical if they have the
7181 if (l1
!= NULL
&& l2
!= NULL
)
7186 // Try to determine the lengths. If we can't, assume the arrays
7187 // are not identical.
7189 Numeric_constant nc1
, nc2
;
7190 if (l1
->numeric_constant_value(&nc1
)
7191 && l2
->numeric_constant_value(&nc2
))
7194 if (nc1
.to_int(&v1
))
7197 if (nc2
.to_int(&v2
))
7199 ret
= mpz_cmp(v1
, v2
) == 0;
7208 // Otherwise the arrays are not identical.
7215 Array_type::do_traverse(Traverse
* traverse
)
7217 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
7218 return TRAVERSE_EXIT
;
7219 if (this->length_
!= NULL
7220 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
7221 return TRAVERSE_EXIT
;
7222 return TRAVERSE_CONTINUE
;
7225 // Check that the length is valid.
7228 Array_type::verify_length()
7230 if (this->length_
== NULL
)
7233 Type_context
context(Type::lookup_integer_type("int"), false);
7234 this->length_
->determine_type(&context
);
7236 if (!this->length_
->is_constant())
7238 go_error_at(this->length_
->location(), "array bound is not constant");
7242 Numeric_constant nc
;
7243 if (!this->length_
->numeric_constant_value(&nc
))
7245 if (this->length_
->type()->integer_type() != NULL
7246 || this->length_
->type()->float_type() != NULL
)
7247 go_error_at(this->length_
->location(), "array bound is not constant");
7249 go_error_at(this->length_
->location(), "array bound is not numeric");
7253 Type
* int_type
= Type::lookup_integer_type("int");
7254 unsigned int tbits
= int_type
->integer_type()->bits();
7256 switch (nc
.to_unsigned_long(&val
))
7258 case Numeric_constant::NC_UL_VALID
:
7259 if (sizeof(val
) >= tbits
/ 8 && val
>> (tbits
- 1) != 0)
7261 go_error_at(this->length_
->location(), "array bound overflows");
7265 case Numeric_constant::NC_UL_NOTINT
:
7266 go_error_at(this->length_
->location(), "array bound truncated to integer");
7268 case Numeric_constant::NC_UL_NEGATIVE
:
7269 go_error_at(this->length_
->location(), "negative array bound");
7271 case Numeric_constant::NC_UL_BIG
:
7274 if (!nc
.to_int(&val
))
7276 unsigned int bits
= mpz_sizeinbase(val
, 2);
7280 go_error_at(this->length_
->location(), "array bound overflows");
7295 Array_type::do_verify()
7297 if (this->element_type()->is_error_type())
7299 if (!this->verify_length())
7300 this->length_
= Expression::make_error(this->length_
->location());
7304 // Whether the type contains pointers. This is always true for a
7305 // slice. For an array it is true if the element type has pointers
7306 // and the length is greater than zero.
7309 Array_type::do_has_pointer() const
7311 if (this->length_
== NULL
)
7313 if (!this->element_type_
->has_pointer())
7316 Numeric_constant nc
;
7317 if (!this->length_
->numeric_constant_value(&nc
))
7319 // Error reported elsewhere.
7324 switch (nc
.to_unsigned_long(&val
))
7326 case Numeric_constant::NC_UL_VALID
:
7328 case Numeric_constant::NC_UL_BIG
:
7331 // Error reported elsewhere.
7336 // Whether we can use memcmp to compare this array.
7339 Array_type::do_compare_is_identity(Gogo
* gogo
)
7341 if (this->length_
== NULL
)
7344 // Check for [...], which indicates that this is not a real type.
7345 if (this->length_
->is_nil_expression())
7348 if (!this->element_type_
->compare_is_identity(gogo
))
7351 // If there is any padding, then we can't use memcmp.
7354 if (!this->element_type_
->backend_type_size(gogo
, &size
)
7355 || !this->element_type_
->backend_type_align(gogo
, &align
))
7357 if ((size
& (align
- 1)) != 0)
7363 // Array type hash code.
7366 Array_type::do_hash_for_method(Gogo
* gogo
) const
7370 // There is no very convenient way to get a hash code for the
7372 ret
= this->element_type_
->hash_for_method(gogo
) + 1;
7373 if (this->is_array_incomparable_
)
7378 // Write the hash function for an array which can not use the identify
7382 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
7383 Function_type
* hash_fntype
,
7384 Function_type
* equal_fntype
)
7386 Location bloc
= Linemap::predeclared_location();
7388 // The pointer to the array that we are going to hash. This is an
7389 // argument to the hash function we are implementing here.
7390 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
7391 go_assert(key_arg
!= NULL
);
7392 Type
* key_arg_type
= key_arg
->var_value()->type();
7394 // The seed argument to the hash function.
7395 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
7396 go_assert(seed_arg
!= NULL
);
7398 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7400 // Make a temporary to hold the return value, initialized to the seed.
7401 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
7402 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
7404 gogo
->add_statement(retval
);
7406 // Make a temporary to hold the key as a uintptr.
7407 ref
= Expression::make_var_reference(key_arg
, bloc
);
7408 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
7409 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
7411 gogo
->add_statement(key
);
7413 // Loop over the array elements.
7415 Type
* int_type
= Type::lookup_integer_type("int");
7416 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7417 gogo
->add_statement(index
);
7419 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7420 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
7421 Type
* pt
= Type::make_pointer_type(name
!= NULL
7422 ? static_cast<Type
*>(name
)
7423 : static_cast<Type
*>(this));
7424 aref
= Expression::make_cast(pt
, aref
, bloc
);
7425 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7430 gogo
->start_block(bloc
);
7432 // Get the hash function for the element type.
7433 Named_object
* hash_fn
;
7434 Named_object
* equal_fn
;
7435 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
7436 hash_fntype
, equal_fntype
, &hash_fn
,
7439 // Get a pointer to this element in the loop.
7440 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
7441 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
7443 // Get the size of each element.
7444 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
7445 Expression::TYPE_INFO_SIZE
);
7447 // Get the hash of this element, passing retval as the seed.
7448 ref
= Expression::make_temporary_reference(retval
, bloc
);
7449 Expression_list
* args
= new Expression_list();
7450 args
->push_back(subkey
);
7451 args
->push_back(ref
);
7452 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
7453 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
7455 // Set retval to the result.
7456 Temporary_reference_expression
* tref
=
7457 Expression::make_temporary_reference(retval
, bloc
);
7458 tref
->set_is_lvalue();
7459 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
7460 gogo
->add_statement(s
);
7462 // Increase the element pointer.
7463 tref
= Expression::make_temporary_reference(key
, bloc
);
7464 tref
->set_is_lvalue();
7465 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
7467 Block
* statements
= gogo
->finish_block(bloc
);
7469 for_range
->add_statements(statements
);
7470 gogo
->add_statement(for_range
);
7472 // Return retval to the caller of the hash function.
7473 Expression_list
* vals
= new Expression_list();
7474 ref
= Expression::make_temporary_reference(retval
, bloc
);
7475 vals
->push_back(ref
);
7476 s
= Statement::make_return_statement(vals
, bloc
);
7477 gogo
->add_statement(s
);
7480 // Write the equality function for an array which can not use the
7481 // identity function.
7484 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
7486 Location bloc
= Linemap::predeclared_location();
7488 // The pointers to the arrays we are going to compare.
7489 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
7490 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
7491 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
7493 // Build temporaries for the keys with the right types.
7494 Type
* pt
= Type::make_pointer_type(name
!= NULL
7495 ? static_cast<Type
*>(name
)
7496 : static_cast<Type
*>(this));
7498 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
7499 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7500 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
7501 gogo
->add_statement(p1
);
7503 ref
= Expression::make_var_reference(key2_arg
, bloc
);
7504 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7505 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
7506 gogo
->add_statement(p2
);
7508 // Loop over the array elements.
7510 Type
* int_type
= Type::lookup_integer_type("int");
7511 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7512 gogo
->add_statement(index
);
7514 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7515 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
7516 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7521 gogo
->start_block(bloc
);
7523 // Compare element in P1 and P2.
7524 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
7525 e1
= Expression::make_unary(OPERATOR_MULT
, e1
, bloc
);
7526 ref
= Expression::make_temporary_reference(index
, bloc
);
7527 e1
= Expression::make_array_index(e1
, ref
, NULL
, NULL
, bloc
);
7529 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
7530 e2
= Expression::make_unary(OPERATOR_MULT
, e2
, bloc
);
7531 ref
= Expression::make_temporary_reference(index
, bloc
);
7532 e2
= Expression::make_array_index(e2
, ref
, NULL
, NULL
, bloc
);
7534 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
7536 // If the elements are not equal, return false.
7537 gogo
->start_block(bloc
);
7538 Expression_list
* vals
= new Expression_list();
7539 vals
->push_back(Expression::make_boolean(false, bloc
));
7540 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
7541 gogo
->add_statement(s
);
7542 Block
* then_block
= gogo
->finish_block(bloc
);
7544 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
7545 gogo
->add_statement(s
);
7547 Block
* statements
= gogo
->finish_block(bloc
);
7549 for_range
->add_statements(statements
);
7550 gogo
->add_statement(for_range
);
7552 // All the elements are equal, so return true.
7553 vals
= new Expression_list();
7554 vals
->push_back(Expression::make_boolean(true, bloc
));
7555 s
= Statement::make_return_statement(vals
, bloc
);
7556 gogo
->add_statement(s
);
7559 // Get the backend representation of the fields of a slice. This is
7560 // not declared in types.h so that types.h doesn't have to #include
7563 // We use int for the count and capacity fields. This matches 6g.
7564 // The language more or less assumes that we can't allocate space of a
7565 // size which does not fit in int.
7568 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
7569 std::vector
<Backend::Btyped_identifier
>* bfields
)
7573 Type
* pet
= Type::make_pointer_type(type
->element_type());
7574 Btype
* pbet
= (use_placeholder
7575 ? pet
->get_backend_placeholder(gogo
)
7576 : pet
->get_backend(gogo
));
7577 Location ploc
= Linemap::predeclared_location();
7579 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
7580 p
->name
= "__values";
7584 Type
* int_type
= Type::lookup_integer_type("int");
7587 p
->name
= "__count";
7588 p
->btype
= int_type
->get_backend(gogo
);
7592 p
->name
= "__capacity";
7593 p
->btype
= int_type
->get_backend(gogo
);
7597 // Get the backend representation for the type of this array. A fixed array is
7598 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7599 // just like an array in C. An open array is a struct with three
7600 // fields: a data pointer, the length, and the capacity.
7603 Array_type::do_get_backend(Gogo
* gogo
)
7605 if (this->length_
== NULL
)
7607 std::vector
<Backend::Btyped_identifier
> bfields
;
7608 get_backend_slice_fields(gogo
, this, false, &bfields
);
7609 return gogo
->backend()->struct_type(bfields
);
7613 Btype
* element
= this->get_backend_element(gogo
, false);
7614 Bexpression
* len
= this->get_backend_length(gogo
);
7615 return gogo
->backend()->array_type(element
, len
);
7619 // Return the backend representation of the element type.
7622 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
7624 if (use_placeholder
)
7625 return this->element_type_
->get_backend_placeholder(gogo
);
7627 return this->element_type_
->get_backend(gogo
);
7630 // Return the backend representation of the length. The length may be
7631 // computed using a function call, so we must only evaluate it once.
7634 Array_type::get_backend_length(Gogo
* gogo
)
7636 go_assert(this->length_
!= NULL
);
7637 if (this->blength_
== NULL
)
7639 Numeric_constant nc
;
7641 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
7643 if (mpz_sgn(val
) < 0)
7645 this->blength_
= gogo
->backend()->error_expression();
7646 return this->blength_
;
7648 Type
* t
= nc
.type();
7650 t
= Type::lookup_integer_type("int");
7651 else if (t
->is_abstract())
7652 t
= t
->make_non_abstract_type();
7653 Btype
* btype
= t
->get_backend(gogo
);
7655 gogo
->backend()->integer_constant_expression(btype
, val
);
7660 // Make up a translation context for the array length
7661 // expression. FIXME: This won't work in general.
7662 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7663 this->blength_
= this->length_
->get_backend(&context
);
7665 Btype
* ibtype
= Type::lookup_integer_type("int")->get_backend(gogo
);
7667 gogo
->backend()->convert_expression(ibtype
, this->blength_
,
7668 this->length_
->location());
7671 return this->blength_
;
7674 // Finish backend representation of the array.
7677 Array_type::finish_backend_element(Gogo
* gogo
)
7679 Type
* et
= this->array_type()->element_type();
7680 et
->get_backend(gogo
);
7681 if (this->is_slice_type())
7683 // This relies on the fact that we always use the same
7684 // structure for a pointer to any given type.
7685 Type
* pet
= Type::make_pointer_type(et
);
7686 pet
->get_backend(gogo
);
7690 // Return an expression for a pointer to the values in ARRAY.
7693 Array_type::get_value_pointer(Gogo
*, Expression
* array
, bool is_lvalue
) const
7695 if (this->length() != NULL
)
7698 go_assert(array
->type()->array_type() != NULL
);
7699 Type
* etype
= array
->type()->array_type()->element_type();
7700 array
= Expression::make_unary(OPERATOR_AND
, array
, array
->location());
7701 return Expression::make_cast(Type::make_pointer_type(etype
), array
,
7709 Temporary_reference_expression
* tref
=
7710 array
->temporary_reference_expression();
7711 Var_expression
* ve
= array
->var_expression();
7714 tref
= tref
->copy()->temporary_reference_expression();
7715 tref
->set_is_lvalue();
7718 else if (ve
!= NULL
)
7720 ve
= new Var_expression(ve
->named_object(), ve
->location());
7721 ve
->set_in_lvalue_pos();
7726 return Expression::make_slice_info(array
,
7727 Expression::SLICE_INFO_VALUE_POINTER
,
7731 // Return an expression for the length of the array ARRAY which has this
7735 Array_type::get_length(Gogo
*, Expression
* array
) const
7737 if (this->length_
!= NULL
)
7738 return this->length_
;
7740 // This is a slice. We need to read the length field.
7741 return Expression::make_slice_info(array
, Expression::SLICE_INFO_LENGTH
,
7745 // Return an expression for the capacity of the array ARRAY which has this
7749 Array_type::get_capacity(Gogo
*, Expression
* array
) const
7751 if (this->length_
!= NULL
)
7752 return this->length_
;
7754 // This is a slice. We need to read the capacity field.
7755 return Expression::make_slice_info(array
, Expression::SLICE_INFO_CAPACITY
,
7762 Array_type::do_export(Export
* exp
) const
7764 exp
->write_c_string("[");
7765 if (this->length_
!= NULL
)
7766 this->length_
->export_expression(exp
);
7767 exp
->write_c_string("] ");
7768 exp
->write_type(this->element_type_
);
7774 Array_type::do_import(Import
* imp
)
7776 imp
->require_c_string("[");
7778 if (imp
->peek_char() == ']')
7781 length
= Expression::import_expression(imp
);
7782 imp
->require_c_string("] ");
7783 Type
* element_type
= imp
->read_type();
7784 return Type::make_array_type(element_type
, length
);
7787 // The type of an array type descriptor.
7790 Array_type::make_array_type_descriptor_type()
7795 Type
* tdt
= Type::make_type_descriptor_type();
7796 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7798 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7801 Type::make_builtin_struct_type(4,
7805 "len", uintptr_type
);
7807 ret
= Type::make_builtin_named_type("ArrayType", sf
);
7813 // The type of an slice type descriptor.
7816 Array_type::make_slice_type_descriptor_type()
7821 Type
* tdt
= Type::make_type_descriptor_type();
7822 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7825 Type::make_builtin_struct_type(2,
7829 ret
= Type::make_builtin_named_type("SliceType", sf
);
7835 // Build a type descriptor for an array/slice type.
7838 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7840 if (this->length_
!= NULL
)
7841 return this->array_type_descriptor(gogo
, name
);
7843 return this->slice_type_descriptor(gogo
, name
);
7846 // Build a type descriptor for an array type.
7849 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7851 Location bloc
= Linemap::predeclared_location();
7853 Type
* atdt
= Array_type::make_array_type_descriptor_type();
7855 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
7857 Expression_list
* vals
= new Expression_list();
7860 Struct_field_list::const_iterator p
= fields
->begin();
7861 go_assert(p
->is_field_name("_type"));
7862 vals
->push_back(this->type_descriptor_constructor(gogo
,
7863 RUNTIME_TYPE_KIND_ARRAY
,
7867 go_assert(p
->is_field_name("elem"));
7868 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7871 go_assert(p
->is_field_name("slice"));
7872 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
7873 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
7876 go_assert(p
->is_field_name("len"));
7877 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
7880 go_assert(p
== fields
->end());
7882 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
7885 // Build a type descriptor for a slice type.
7888 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7890 Location bloc
= Linemap::predeclared_location();
7892 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
7894 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
7896 Expression_list
* vals
= new Expression_list();
7899 Struct_field_list::const_iterator p
= fields
->begin();
7900 go_assert(p
->is_field_name("_type"));
7901 vals
->push_back(this->type_descriptor_constructor(gogo
,
7902 RUNTIME_TYPE_KIND_SLICE
,
7906 go_assert(p
->is_field_name("elem"));
7907 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7910 go_assert(p
== fields
->end());
7912 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
7915 // Reflection string.
7918 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7920 ret
->push_back('[');
7921 if (this->length_
!= NULL
)
7923 Numeric_constant nc
;
7924 if (!this->length_
->numeric_constant_value(&nc
))
7926 go_assert(saw_errors());
7930 if (!nc
.to_int(&val
))
7932 go_assert(saw_errors());
7935 char* s
= mpz_get_str(NULL
, 10, val
);
7940 ret
->push_back(']');
7942 this->append_reflection(this->element_type_
, gogo
, ret
);
7948 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7950 ret
->push_back('A');
7951 this->append_mangled_name(this->element_type_
, gogo
, ret
);
7952 if (this->length_
!= NULL
)
7954 Numeric_constant nc
;
7955 if (!this->length_
->numeric_constant_value(&nc
))
7957 go_assert(saw_errors());
7961 if (!nc
.to_int(&val
))
7963 go_assert(saw_errors());
7966 char *s
= mpz_get_str(NULL
, 10, val
);
7970 if (this->is_array_incomparable_
)
7971 ret
->push_back('x');
7973 ret
->push_back('e');
7976 // Make an array type.
7979 Type::make_array_type(Type
* element_type
, Expression
* length
)
7981 return new Array_type(element_type
, length
);
7986 Named_object
* Map_type::zero_value
;
7987 int64_t Map_type::zero_value_size
;
7988 int64_t Map_type::zero_value_align
;
7990 // If this map requires the "fat" functions, return the pointer to
7991 // pass as the zero value to those functions. Otherwise, in the
7992 // normal case, return NULL. The map requires the "fat" functions if
7993 // the value size is larger than max_zero_size bytes. max_zero_size
7994 // must match maxZero in libgo/go/runtime/hashmap.go.
7997 Map_type::fat_zero_value(Gogo
* gogo
)
8000 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
8002 go_assert(saw_errors());
8005 if (valsize
<= Map_type::max_zero_size
)
8008 if (Map_type::zero_value_size
< valsize
)
8009 Map_type::zero_value_size
= valsize
;
8012 if (!this->val_type_
->backend_type_align(gogo
, &valalign
))
8014 go_assert(saw_errors());
8018 if (Map_type::zero_value_align
< valalign
)
8019 Map_type::zero_value_align
= valalign
;
8021 Location bloc
= Linemap::predeclared_location();
8023 if (Map_type::zero_value
== NULL
)
8025 // The final type will be set in backend_zero_value.
8026 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8027 Expression
* size
= Expression::make_integer_ul(0, NULL
, bloc
);
8028 Array_type
* array_type
= Type::make_array_type(uint8_type
, size
);
8029 array_type
->set_is_array_incomparable();
8030 Variable
* var
= new Variable(array_type
, NULL
, true, false, false, bloc
);
8031 Map_type::zero_value
= Named_object::make_variable("go$zerovalue", NULL
,
8035 Expression
* z
= Expression::make_var_reference(Map_type::zero_value
, bloc
);
8036 z
= Expression::make_unary(OPERATOR_AND
, z
, bloc
);
8037 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8038 z
= Expression::make_cast(unsafe_ptr_type
, z
, bloc
);
8042 // Return whether VAR is the map zero value.
8045 Map_type::is_zero_value(Variable
* var
)
8047 return (Map_type::zero_value
!= NULL
8048 && Map_type::zero_value
->var_value() == var
);
8051 // Return the backend representation for the zero value.
8054 Map_type::backend_zero_value(Gogo
* gogo
)
8056 Location bloc
= Linemap::predeclared_location();
8058 go_assert(Map_type::zero_value
!= NULL
);
8060 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8061 Btype
* buint8_type
= uint8_type
->get_backend(gogo
);
8063 Type
* int_type
= Type::lookup_integer_type("int");
8065 Expression
* e
= Expression::make_integer_int64(Map_type::zero_value_size
,
8067 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
8068 Bexpression
* blength
= e
->get_backend(&context
);
8070 Btype
* barray_type
= gogo
->backend()->array_type(buint8_type
, blength
);
8072 std::string zname
= Map_type::zero_value
->name();
8073 std::string
asm_name(go_selectively_encode_id(zname
));
8075 gogo
->backend()->implicit_variable(zname
, asm_name
,
8076 barray_type
, false, true, true,
8077 Map_type::zero_value_align
);
8078 gogo
->backend()->implicit_variable_set_init(zvar
, zname
, barray_type
,
8079 false, true, true, NULL
);
8086 Map_type::do_traverse(Traverse
* traverse
)
8088 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
8089 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
8090 return TRAVERSE_EXIT
;
8091 return TRAVERSE_CONTINUE
;
8094 // Check that the map type is OK.
8097 Map_type::do_verify()
8099 // The runtime support uses "map[void]void".
8100 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
8101 go_error_at(this->location_
, "invalid map key type");
8102 if (!this->key_type_
->in_heap())
8103 go_error_at(this->location_
, "go:notinheap map key not allowed");
8104 if (!this->val_type_
->in_heap())
8105 go_error_at(this->location_
, "go:notinheap map value not allowed");
8109 // Whether two map types are identical.
8112 Map_type::is_identical(const Map_type
* t
, Cmp_tags cmp_tags
,
8113 bool errors_are_identical
) const
8115 return (Type::are_identical_cmp_tags(this->key_type(), t
->key_type(),
8116 cmp_tags
, errors_are_identical
, NULL
)
8117 && Type::are_identical_cmp_tags(this->val_type(), t
->val_type(),
8118 cmp_tags
, errors_are_identical
,
8125 Map_type::do_hash_for_method(Gogo
* gogo
) const
8127 return (this->key_type_
->hash_for_method(gogo
)
8128 + this->val_type_
->hash_for_method(gogo
)
8132 // Get the backend representation for a map type. A map type is
8133 // represented as a pointer to a struct. The struct is hmap in
8134 // runtime/hashmap.go.
8137 Map_type::do_get_backend(Gogo
* gogo
)
8139 static Btype
* backend_map_type
;
8140 if (backend_map_type
== NULL
)
8142 std::vector
<Backend::Btyped_identifier
> bfields(9);
8144 Location bloc
= Linemap::predeclared_location();
8146 Type
* int_type
= Type::lookup_integer_type("int");
8147 bfields
[0].name
= "count";
8148 bfields
[0].btype
= int_type
->get_backend(gogo
);
8149 bfields
[0].location
= bloc
;
8151 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8152 bfields
[1].name
= "flags";
8153 bfields
[1].btype
= uint8_type
->get_backend(gogo
);
8154 bfields
[1].location
= bloc
;
8156 bfields
[2].name
= "B";
8157 bfields
[2].btype
= bfields
[1].btype
;
8158 bfields
[2].location
= bloc
;
8160 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8161 bfields
[3].name
= "noverflow";
8162 bfields
[3].btype
= uint16_type
->get_backend(gogo
);
8163 bfields
[3].location
= bloc
;
8165 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8166 bfields
[4].name
= "hash0";
8167 bfields
[4].btype
= uint32_type
->get_backend(gogo
);
8168 bfields
[4].location
= bloc
;
8170 Btype
* bvt
= gogo
->backend()->void_type();
8171 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
8172 bfields
[5].name
= "buckets";
8173 bfields
[5].btype
= bpvt
;
8174 bfields
[5].location
= bloc
;
8176 bfields
[6].name
= "oldbuckets";
8177 bfields
[6].btype
= bpvt
;
8178 bfields
[6].location
= bloc
;
8180 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8181 bfields
[7].name
= "nevacuate";
8182 bfields
[7].btype
= uintptr_type
->get_backend(gogo
);
8183 bfields
[7].location
= bloc
;
8185 bfields
[8].name
= "overflow";
8186 bfields
[8].btype
= bpvt
;
8187 bfields
[8].location
= bloc
;
8189 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
8190 bt
= gogo
->backend()->named_type("runtime.hmap", bt
, bloc
);
8191 backend_map_type
= gogo
->backend()->pointer_type(bt
);
8193 return backend_map_type
;
8196 // The type of a map type descriptor.
8199 Map_type::make_map_type_descriptor_type()
8204 Type
* tdt
= Type::make_type_descriptor_type();
8205 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
8206 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8207 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8208 Type
* bool_type
= Type::lookup_bool_type();
8211 Type::make_builtin_struct_type(12,
8217 "keysize", uint8_type
,
8218 "indirectkey", bool_type
,
8219 "valuesize", uint8_type
,
8220 "indirectvalue", bool_type
,
8221 "bucketsize", uint16_type
,
8222 "reflexivekey", bool_type
,
8223 "needkeyupdate", bool_type
);
8225 ret
= Type::make_builtin_named_type("MapType", sf
);
8231 // Build a type descriptor for a map type.
8234 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8236 Location bloc
= Linemap::predeclared_location();
8238 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
8239 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8240 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8243 if (!this->key_type_
->backend_type_size(gogo
, &keysize
))
8245 go_error_at(this->location_
, "error determining map key type size");
8246 return Expression::make_error(this->location_
);
8250 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
8252 go_error_at(this->location_
, "error determining map value type size");
8253 return Expression::make_error(this->location_
);
8257 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptrsize
))
8259 go_assert(saw_errors());
8260 return Expression::make_error(this->location_
);
8263 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
8264 if (bucket_type
== NULL
)
8266 go_assert(saw_errors());
8267 return Expression::make_error(this->location_
);
8271 if (!bucket_type
->backend_type_size(gogo
, &bucketsize
))
8273 go_assert(saw_errors());
8274 return Expression::make_error(this->location_
);
8277 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
8279 Expression_list
* vals
= new Expression_list();
8282 Struct_field_list::const_iterator p
= fields
->begin();
8283 go_assert(p
->is_field_name("_type"));
8284 vals
->push_back(this->type_descriptor_constructor(gogo
,
8285 RUNTIME_TYPE_KIND_MAP
,
8289 go_assert(p
->is_field_name("key"));
8290 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
8293 go_assert(p
->is_field_name("elem"));
8294 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
8297 go_assert(p
->is_field_name("bucket"));
8298 vals
->push_back(Expression::make_type_descriptor(bucket_type
, bloc
));
8301 go_assert(p
->is_field_name("hmap"));
8302 Type
* hmap_type
= this->hmap_type(bucket_type
);
8303 vals
->push_back(Expression::make_type_descriptor(hmap_type
, bloc
));
8306 go_assert(p
->is_field_name("keysize"));
8307 if (keysize
> Map_type::max_key_size
)
8308 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
8310 vals
->push_back(Expression::make_integer_int64(keysize
, uint8_type
, bloc
));
8313 go_assert(p
->is_field_name("indirectkey"));
8314 vals
->push_back(Expression::make_boolean(keysize
> Map_type::max_key_size
,
8318 go_assert(p
->is_field_name("valuesize"));
8319 if (valsize
> Map_type::max_val_size
)
8320 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
8322 vals
->push_back(Expression::make_integer_int64(valsize
, uint8_type
, bloc
));
8325 go_assert(p
->is_field_name("indirectvalue"));
8326 vals
->push_back(Expression::make_boolean(valsize
> Map_type::max_val_size
,
8330 go_assert(p
->is_field_name("bucketsize"));
8331 vals
->push_back(Expression::make_integer_int64(bucketsize
, uint16_type
,
8335 go_assert(p
->is_field_name("reflexivekey"));
8336 vals
->push_back(Expression::make_boolean(this->key_type_
->is_reflexive(),
8340 go_assert(p
->is_field_name("needkeyupdate"));
8341 vals
->push_back(Expression::make_boolean(this->key_type_
->needs_key_update(),
8345 go_assert(p
== fields
->end());
8347 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
8350 // Return the bucket type to use for a map type. This must correspond
8351 // to libgo/go/runtime/hashmap.go.
8354 Map_type::bucket_type(Gogo
* gogo
, int64_t keysize
, int64_t valsize
)
8356 if (this->bucket_type_
!= NULL
)
8357 return this->bucket_type_
;
8359 Type
* key_type
= this->key_type_
;
8360 if (keysize
> Map_type::max_key_size
)
8361 key_type
= Type::make_pointer_type(key_type
);
8363 Type
* val_type
= this->val_type_
;
8364 if (valsize
> Map_type::max_val_size
)
8365 val_type
= Type::make_pointer_type(val_type
);
8367 Expression
* bucket_size
= Expression::make_integer_ul(Map_type::bucket_size
,
8368 NULL
, this->location_
);
8370 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8371 Array_type
* topbits_type
= Type::make_array_type(uint8_type
, bucket_size
);
8372 topbits_type
->set_is_array_incomparable();
8373 Array_type
* keys_type
= Type::make_array_type(key_type
, bucket_size
);
8374 keys_type
->set_is_array_incomparable();
8375 Array_type
* values_type
= Type::make_array_type(val_type
, bucket_size
);
8376 values_type
->set_is_array_incomparable();
8378 // If keys and values have no pointers, the map implementation can
8379 // keep a list of overflow pointers on the side so that buckets can
8380 // be marked as having no pointers. Arrange for the bucket to have
8381 // no pointers by changing the type of the overflow field to uintptr
8382 // in this case. See comment on the hmap.overflow field in
8383 // libgo/go/runtime/hashmap.go.
8384 Type
* overflow_type
;
8385 if (!key_type
->has_pointer() && !val_type
->has_pointer())
8386 overflow_type
= Type::lookup_integer_type("uintptr");
8389 // This should really be a pointer to the bucket type itself,
8390 // but that would require us to construct a Named_type for it to
8391 // give it a way to refer to itself. Since nothing really cares
8392 // (except perhaps for someone using a debugger) just use an
8394 overflow_type
= Type::make_pointer_type(Type::make_void_type());
8397 // Make sure the overflow pointer is the last memory in the struct,
8398 // because the runtime assumes it can use size-ptrSize as the offset
8399 // of the overflow pointer. We double-check that property below
8400 // once the offsets and size are computed.
8402 int64_t topbits_field_size
, topbits_field_align
;
8403 int64_t keys_field_size
, keys_field_align
;
8404 int64_t values_field_size
, values_field_align
;
8405 int64_t overflow_field_size
, overflow_field_align
;
8406 if (!topbits_type
->backend_type_size(gogo
, &topbits_field_size
)
8407 || !topbits_type
->backend_type_field_align(gogo
, &topbits_field_align
)
8408 || !keys_type
->backend_type_size(gogo
, &keys_field_size
)
8409 || !keys_type
->backend_type_field_align(gogo
, &keys_field_align
)
8410 || !values_type
->backend_type_size(gogo
, &values_field_size
)
8411 || !values_type
->backend_type_field_align(gogo
, &values_field_align
)
8412 || !overflow_type
->backend_type_size(gogo
, &overflow_field_size
)
8413 || !overflow_type
->backend_type_field_align(gogo
, &overflow_field_align
))
8415 go_assert(saw_errors());
8420 int64_t max_align
= std::max(std::max(topbits_field_align
, keys_field_align
),
8421 values_field_align
);
8422 if (max_align
<= overflow_field_align
)
8423 ret
= make_builtin_struct_type(4,
8424 "topbits", topbits_type
,
8426 "values", values_type
,
8427 "overflow", overflow_type
);
8430 size_t off
= topbits_field_size
;
8431 off
= ((off
+ keys_field_align
- 1)
8432 &~ static_cast<size_t>(keys_field_align
- 1));
8433 off
+= keys_field_size
;
8434 off
= ((off
+ values_field_align
- 1)
8435 &~ static_cast<size_t>(values_field_align
- 1));
8436 off
+= values_field_size
;
8438 int64_t padded_overflow_field_size
=
8439 ((overflow_field_size
+ max_align
- 1)
8440 &~ static_cast<size_t>(max_align
- 1));
8443 ovoff
= ((ovoff
+ max_align
- 1)
8444 &~ static_cast<size_t>(max_align
- 1));
8445 size_t pad
= (ovoff
- off
8446 + padded_overflow_field_size
- overflow_field_size
);
8448 Expression
* pad_expr
= Expression::make_integer_ul(pad
, NULL
,
8450 Array_type
* pad_type
= Type::make_array_type(uint8_type
, pad_expr
);
8451 pad_type
->set_is_array_incomparable();
8453 ret
= make_builtin_struct_type(5,
8454 "topbits", topbits_type
,
8456 "values", values_type
,
8458 "overflow", overflow_type
);
8461 // Verify that the overflow field is just before the end of the
8464 Btype
* btype
= ret
->get_backend(gogo
);
8465 int64_t offset
= gogo
->backend()->type_field_offset(btype
,
8466 ret
->field_count() - 1);
8468 if (!ret
->backend_type_size(gogo
, &size
))
8470 go_assert(saw_errors());
8475 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptr_size
))
8477 go_assert(saw_errors());
8481 go_assert(offset
+ ptr_size
== size
);
8483 ret
->set_is_struct_incomparable();
8485 this->bucket_type_
= ret
;
8489 // Return the hashmap type for a map type.
8492 Map_type::hmap_type(Type
* bucket_type
)
8494 if (this->hmap_type_
!= NULL
)
8495 return this->hmap_type_
;
8497 Type
* int_type
= Type::lookup_integer_type("int");
8498 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8499 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8500 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8501 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8503 Type
* ptr_bucket_type
= Type::make_pointer_type(bucket_type
);
8505 Struct_type
* ret
= make_builtin_struct_type(8,
8507 "flags", uint8_type
,
8509 "hash0", uint32_type
,
8510 "buckets", ptr_bucket_type
,
8511 "oldbuckets", ptr_bucket_type
,
8512 "nevacuate", uintptr_type
,
8513 "overflow", void_ptr_type
);
8514 ret
->set_is_struct_incomparable();
8515 this->hmap_type_
= ret
;
8519 // Return the iterator type for a map type. This is the type of the
8520 // value used when doing a range over a map.
8523 Map_type::hiter_type(Gogo
* gogo
)
8525 if (this->hiter_type_
!= NULL
)
8526 return this->hiter_type_
;
8528 int64_t keysize
, valsize
;
8529 if (!this->key_type_
->backend_type_size(gogo
, &keysize
)
8530 || !this->val_type_
->backend_type_size(gogo
, &valsize
))
8532 go_assert(saw_errors());
8536 Type
* key_ptr_type
= Type::make_pointer_type(this->key_type_
);
8537 Type
* val_ptr_type
= Type::make_pointer_type(this->val_type_
);
8538 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8539 Type
* uint8_ptr_type
= Type::make_pointer_type(uint8_type
);
8540 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8541 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
8542 Type
* bucket_ptr_type
= Type::make_pointer_type(bucket_type
);
8543 Type
* hmap_type
= this->hmap_type(bucket_type
);
8544 Type
* hmap_ptr_type
= Type::make_pointer_type(hmap_type
);
8545 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8547 Struct_type
* ret
= make_builtin_struct_type(12,
8548 "key", key_ptr_type
,
8549 "val", val_ptr_type
,
8550 "t", uint8_ptr_type
,
8552 "buckets", bucket_ptr_type
,
8553 "bptr", bucket_ptr_type
,
8554 "overflow0", void_ptr_type
,
8555 "overflow1", void_ptr_type
,
8556 "startBucket", uintptr_type
,
8557 "stuff", uintptr_type
,
8558 "bucket", uintptr_type
,
8559 "checkBucket", uintptr_type
);
8560 ret
->set_is_struct_incomparable();
8561 this->hiter_type_
= ret
;
8565 // Reflection string for a map.
8568 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8570 ret
->append("map[");
8571 this->append_reflection(this->key_type_
, gogo
, ret
);
8573 this->append_reflection(this->val_type_
, gogo
, ret
);
8576 // Mangled name for a map.
8579 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8581 ret
->push_back('M');
8582 this->append_mangled_name(this->key_type_
, gogo
, ret
);
8584 this->append_mangled_name(this->val_type_
, gogo
, ret
);
8587 // Export a map type.
8590 Map_type::do_export(Export
* exp
) const
8592 exp
->write_c_string("map [");
8593 exp
->write_type(this->key_type_
);
8594 exp
->write_c_string("] ");
8595 exp
->write_type(this->val_type_
);
8598 // Import a map type.
8601 Map_type::do_import(Import
* imp
)
8603 imp
->require_c_string("map [");
8604 Type
* key_type
= imp
->read_type();
8605 imp
->require_c_string("] ");
8606 Type
* val_type
= imp
->read_type();
8607 return Type::make_map_type(key_type
, val_type
, imp
->location());
8613 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
8615 return new Map_type(key_type
, val_type
, location
);
8618 // Class Channel_type.
8623 Channel_type::do_verify()
8625 // We have no location for this error, but this is not something the
8626 // ordinary user will see.
8627 if (!this->element_type_
->in_heap())
8628 go_error_at(Linemap::unknown_location(),
8629 "chan of go:notinheap type not allowed");
8636 Channel_type::do_hash_for_method(Gogo
* gogo
) const
8638 unsigned int ret
= 0;
8639 if (this->may_send_
)
8641 if (this->may_receive_
)
8643 if (this->element_type_
!= NULL
)
8644 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
8648 // Whether this type is the same as T.
8651 Channel_type::is_identical(const Channel_type
* t
, Cmp_tags cmp_tags
,
8652 bool errors_are_identical
) const
8654 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
8655 cmp_tags
, errors_are_identical
, NULL
))
8657 return (this->may_send_
== t
->may_send_
8658 && this->may_receive_
== t
->may_receive_
);
8661 // Return the backend representation for a channel type. A channel is a pointer
8662 // to a __go_channel struct. The __go_channel struct is defined in
8663 // libgo/runtime/channel.h.
8666 Channel_type::do_get_backend(Gogo
* gogo
)
8668 static Btype
* backend_channel_type
;
8669 if (backend_channel_type
== NULL
)
8671 std::vector
<Backend::Btyped_identifier
> bfields
;
8672 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
8673 bt
= gogo
->backend()->named_type("__go_channel", bt
,
8674 Linemap::predeclared_location());
8675 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
8677 return backend_channel_type
;
8680 // Build a type descriptor for a channel type.
8683 Channel_type::make_chan_type_descriptor_type()
8688 Type
* tdt
= Type::make_type_descriptor_type();
8689 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
8691 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8694 Type::make_builtin_struct_type(3,
8697 "dir", uintptr_type
);
8699 ret
= Type::make_builtin_named_type("ChanType", sf
);
8705 // Build a type descriptor for a map type.
8708 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8710 Location bloc
= Linemap::predeclared_location();
8712 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
8714 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
8716 Expression_list
* vals
= new Expression_list();
8719 Struct_field_list::const_iterator p
= fields
->begin();
8720 go_assert(p
->is_field_name("_type"));
8721 vals
->push_back(this->type_descriptor_constructor(gogo
,
8722 RUNTIME_TYPE_KIND_CHAN
,
8726 go_assert(p
->is_field_name("elem"));
8727 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
8730 go_assert(p
->is_field_name("dir"));
8731 // These bits must match the ones in libgo/runtime/go-type.h.
8733 if (this->may_receive_
)
8735 if (this->may_send_
)
8737 vals
->push_back(Expression::make_integer_ul(val
, p
->type(), bloc
));
8740 go_assert(p
== fields
->end());
8742 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
8745 // Reflection string.
8748 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8750 if (!this->may_send_
)
8752 ret
->append("chan");
8753 if (!this->may_receive_
)
8755 ret
->push_back(' ');
8756 this->append_reflection(this->element_type_
, gogo
, ret
);
8762 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8764 ret
->push_back('C');
8765 this->append_mangled_name(this->element_type_
, gogo
, ret
);
8766 if (this->may_send_
)
8767 ret
->push_back('s');
8768 if (this->may_receive_
)
8769 ret
->push_back('r');
8770 ret
->push_back('e');
8776 Channel_type::do_export(Export
* exp
) const
8778 exp
->write_c_string("chan ");
8779 if (this->may_send_
&& !this->may_receive_
)
8780 exp
->write_c_string("-< ");
8781 else if (this->may_receive_
&& !this->may_send_
)
8782 exp
->write_c_string("<- ");
8783 exp
->write_type(this->element_type_
);
8789 Channel_type::do_import(Import
* imp
)
8791 imp
->require_c_string("chan ");
8795 if (imp
->match_c_string("-< "))
8799 may_receive
= false;
8801 else if (imp
->match_c_string("<- "))
8813 Type
* element_type
= imp
->read_type();
8815 return Type::make_channel_type(may_send
, may_receive
, element_type
);
8818 // Return the type to manage a select statement with ncases case
8819 // statements. A value of this type is allocated on the stack. This
8820 // must match the type hselect in libgo/go/runtime/select.go.
8823 Channel_type::select_type(int ncases
)
8825 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
8826 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8828 static Struct_type
* scase_type
;
8829 if (scase_type
== NULL
)
8831 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8832 Type
* uint64_type
= Type::lookup_integer_type("uint64");
8834 Type::make_builtin_struct_type(7,
8835 "elem", unsafe_pointer_type
,
8836 "chan", unsafe_pointer_type
,
8838 "kind", uint16_type
,
8839 "index", uint16_type
,
8840 "receivedp", unsafe_pointer_type
,
8841 "releasetime", uint64_type
);
8842 scase_type
->set_is_struct_incomparable();
8845 Expression
* ncases_expr
=
8846 Expression::make_integer_ul(ncases
, NULL
, Linemap::predeclared_location());
8847 Array_type
* scases
= Type::make_array_type(scase_type
, ncases_expr
);
8848 scases
->set_is_array_incomparable();
8849 Array_type
* order
= Type::make_array_type(uint16_type
, ncases_expr
);
8850 order
->set_is_array_incomparable();
8853 Type::make_builtin_struct_type(7,
8854 "tcase", uint16_type
,
8855 "ncase", uint16_type
,
8856 "pollorder", unsafe_pointer_type
,
8857 "lockorder", unsafe_pointer_type
,
8859 "lockorderarr", order
,
8860 "pollorderarr", order
);
8861 ret
->set_is_struct_incomparable();
8865 // Make a new channel type.
8868 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
8870 return new Channel_type(send
, receive
, element_type
);
8873 // Class Interface_type.
8875 // Return the list of methods.
8877 const Typed_identifier_list
*
8878 Interface_type::methods() const
8880 go_assert(this->methods_are_finalized_
|| saw_errors());
8881 return this->all_methods_
;
8884 // Return the number of methods.
8887 Interface_type::method_count() const
8889 go_assert(this->methods_are_finalized_
|| saw_errors());
8890 return this->all_methods_
== NULL
? 0 : this->all_methods_
->size();
8896 Interface_type::do_traverse(Traverse
* traverse
)
8898 Typed_identifier_list
* methods
= (this->methods_are_finalized_
8899 ? this->all_methods_
8900 : this->parse_methods_
);
8901 if (methods
== NULL
)
8902 return TRAVERSE_CONTINUE
;
8903 return methods
->traverse(traverse
);
8906 // Finalize the methods. This handles interface inheritance.
8909 Interface_type::finalize_methods()
8911 if (this->methods_are_finalized_
)
8913 this->methods_are_finalized_
= true;
8914 if (this->parse_methods_
== NULL
)
8917 this->all_methods_
= new Typed_identifier_list();
8918 this->all_methods_
->reserve(this->parse_methods_
->size());
8919 Typed_identifier_list inherit
;
8920 for (Typed_identifier_list::const_iterator pm
=
8921 this->parse_methods_
->begin();
8922 pm
!= this->parse_methods_
->end();
8925 const Typed_identifier
* p
= &*pm
;
8926 if (p
->name().empty())
8927 inherit
.push_back(*p
);
8928 else if (this->find_method(p
->name()) == NULL
)
8929 this->all_methods_
->push_back(*p
);
8931 go_error_at(p
->location(), "duplicate method %qs",
8932 Gogo::message_name(p
->name()).c_str());
8935 std::vector
<Named_type
*> seen
;
8936 seen
.reserve(inherit
.size());
8937 bool issued_recursive_error
= false;
8938 while (!inherit
.empty())
8940 Type
* t
= inherit
.back().type();
8941 Location tl
= inherit
.back().location();
8944 Interface_type
* it
= t
->interface_type();
8948 go_error_at(tl
, "interface contains embedded non-interface");
8953 if (!issued_recursive_error
)
8955 go_error_at(tl
, "invalid recursive interface");
8956 issued_recursive_error
= true;
8961 Named_type
* nt
= t
->named_type();
8962 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
8964 std::vector
<Named_type
*>::const_iterator q
;
8965 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
8969 go_error_at(tl
, "inherited interface loop");
8973 if (q
!= seen
.end())
8978 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
8979 if (imethods
== NULL
)
8981 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
8982 q
!= imethods
->end();
8985 if (q
->name().empty())
8986 inherit
.push_back(*q
);
8987 else if (this->find_method(q
->name()) == NULL
)
8988 this->all_methods_
->push_back(Typed_identifier(q
->name(),
8991 go_error_at(tl
, "inherited method %qs is ambiguous",
8992 Gogo::message_name(q
->name()).c_str());
8996 if (!this->all_methods_
->empty())
8997 this->all_methods_
->sort_by_name();
9000 delete this->all_methods_
;
9001 this->all_methods_
= NULL
;
9005 // Return the method NAME, or NULL.
9007 const Typed_identifier
*
9008 Interface_type::find_method(const std::string
& name
) const
9010 go_assert(this->methods_are_finalized_
);
9011 if (this->all_methods_
== NULL
)
9013 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9014 p
!= this->all_methods_
->end();
9016 if (p
->name() == name
)
9021 // Return the method index.
9024 Interface_type::method_index(const std::string
& name
) const
9026 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
9028 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9029 p
!= this->all_methods_
->end();
9031 if (p
->name() == name
)
9036 // Return whether NAME is an unexported method, for better error
9040 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
9042 go_assert(this->methods_are_finalized_
);
9043 if (this->all_methods_
== NULL
)
9045 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9046 p
!= this->all_methods_
->end();
9049 const std::string
& method_name(p
->name());
9050 if (Gogo::is_hidden_name(method_name
)
9051 && name
== Gogo::unpack_hidden_name(method_name
)
9052 && gogo
->pack_hidden_name(name
, false) != method_name
)
9058 // Whether this type is identical with T.
9061 Interface_type::is_identical(const Interface_type
* t
, Cmp_tags cmp_tags
,
9062 bool errors_are_identical
) const
9064 // If methods have not been finalized, then we are asking whether
9065 // func redeclarations are the same. This is an error, so for
9066 // simplicity we say they are never the same.
9067 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
9070 // We require the same methods with the same types. The methods
9071 // have already been sorted.
9072 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
9073 return this->all_methods_
== t
->all_methods_
;
9075 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
9078 Assume_identical
* hold_ai
= this->assume_identical_
;
9079 Assume_identical ai
;
9083 this->assume_identical_
= &ai
;
9085 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
9086 Typed_identifier_list::const_iterator p2
;
9087 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
9089 if (p1
== this->all_methods_
->end())
9091 if (p1
->name() != p2
->name()
9092 || !Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
9093 errors_are_identical
, NULL
))
9097 this->assume_identical_
= hold_ai
;
9099 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
9102 // Return true if T1 and T2 are assumed to be identical during a type
9106 Interface_type::assume_identical(const Interface_type
* t1
,
9107 const Interface_type
* t2
) const
9109 for (Assume_identical
* p
= this->assume_identical_
;
9112 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
9117 // Whether we can assign the interface type T to this type. The types
9118 // are known to not be identical. An interface assignment is only
9119 // permitted if T is known to implement all methods in THIS.
9120 // Otherwise a type guard is required.
9123 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
9124 std::string
* reason
) const
9126 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
9127 if (this->all_methods_
== NULL
)
9129 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9130 p
!= this->all_methods_
->end();
9133 const Typed_identifier
* m
= t
->find_method(p
->name());
9139 snprintf(buf
, sizeof buf
,
9140 _("need explicit conversion; missing method %s%s%s"),
9141 go_open_quote(), Gogo::message_name(p
->name()).c_str(),
9143 reason
->assign(buf
);
9148 std::string subreason
;
9149 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
9153 std::string n
= Gogo::message_name(p
->name());
9154 size_t len
= 100 + n
.length() + subreason
.length();
9155 char* buf
= new char[len
];
9156 if (subreason
.empty())
9157 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
9158 go_open_quote(), n
.c_str(), go_close_quote());
9161 _("incompatible type for method %s%s%s (%s)"),
9162 go_open_quote(), n
.c_str(), go_close_quote(),
9164 reason
->assign(buf
);
9177 Interface_type::do_hash_for_method(Gogo
*) const
9179 go_assert(this->methods_are_finalized_
);
9180 unsigned int ret
= 0;
9181 if (this->all_methods_
!= NULL
)
9183 for (Typed_identifier_list::const_iterator p
=
9184 this->all_methods_
->begin();
9185 p
!= this->all_methods_
->end();
9188 ret
= Type::hash_string(p
->name(), ret
);
9189 // We don't use the method type in the hash, to avoid
9190 // infinite recursion if an interface method uses a type
9191 // which is an interface which inherits from the interface
9193 // type T interface { F() interface {T}}
9200 // Return true if T implements the interface. If it does not, and
9201 // REASON is not NULL, set *REASON to a useful error message.
9204 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
9206 go_assert(this->methods_are_finalized_
);
9207 if (this->all_methods_
== NULL
)
9210 bool is_pointer
= false;
9211 const Named_type
* nt
= t
->named_type();
9212 const Struct_type
* st
= t
->struct_type();
9213 // If we start with a named type, we don't dereference it to find
9217 const Type
* pt
= t
->points_to();
9220 // If T is a pointer to a named type, then we need to look at
9221 // the type to which it points.
9223 nt
= pt
->named_type();
9224 st
= pt
->struct_type();
9228 // If we have a named type, get the methods from it rather than from
9233 // Only named and struct types have methods.
9234 if (nt
== NULL
&& st
== NULL
)
9238 if (t
->points_to() != NULL
9239 && t
->points_to()->interface_type() != NULL
)
9240 reason
->assign(_("pointer to interface type has no methods"));
9242 reason
->assign(_("type has no methods"));
9247 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
9251 if (t
->points_to() != NULL
9252 && t
->points_to()->interface_type() != NULL
)
9253 reason
->assign(_("pointer to interface type has no methods"));
9255 reason
->assign(_("type has no methods"));
9260 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9261 p
!= this->all_methods_
->end();
9264 bool is_ambiguous
= false;
9265 Method
* m
= (nt
!= NULL
9266 ? nt
->method_function(p
->name(), &is_ambiguous
)
9267 : st
->method_function(p
->name(), &is_ambiguous
));
9272 std::string n
= Gogo::message_name(p
->name());
9273 size_t len
= n
.length() + 100;
9274 char* buf
= new char[len
];
9276 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
9277 go_open_quote(), n
.c_str(), go_close_quote());
9279 snprintf(buf
, len
, _("missing method %s%s%s"),
9280 go_open_quote(), n
.c_str(), go_close_quote());
9281 reason
->assign(buf
);
9287 Function_type
*p_fn_type
= p
->type()->function_type();
9288 Function_type
* m_fn_type
= m
->type()->function_type();
9289 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
9290 std::string subreason
;
9291 if (!p_fn_type
->is_identical(m_fn_type
, true, COMPARE_TAGS
, true,
9296 std::string n
= Gogo::message_name(p
->name());
9297 size_t len
= 100 + n
.length() + subreason
.length();
9298 char* buf
= new char[len
];
9299 if (subreason
.empty())
9300 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
9301 go_open_quote(), n
.c_str(), go_close_quote());
9304 _("incompatible type for method %s%s%s (%s)"),
9305 go_open_quote(), n
.c_str(), go_close_quote(),
9307 reason
->assign(buf
);
9313 if (!is_pointer
&& !m
->is_value_method())
9317 std::string n
= Gogo::message_name(p
->name());
9318 size_t len
= 100 + n
.length();
9319 char* buf
= new char[len
];
9321 _("method %s%s%s requires a pointer receiver"),
9322 go_open_quote(), n
.c_str(), go_close_quote());
9323 reason
->assign(buf
);
9329 // If the magic //go:nointerface comment was used, the method
9330 // may not be used to implement interfaces.
9331 if (m
->nointerface())
9335 std::string n
= Gogo::message_name(p
->name());
9336 size_t len
= 100 + n
.length();
9337 char* buf
= new char[len
];
9339 _("method %s%s%s is marked go:nointerface"),
9340 go_open_quote(), n
.c_str(), go_close_quote());
9341 reason
->assign(buf
);
9351 // Return the backend representation of the empty interface type. We
9352 // use the same struct for all empty interfaces.
9355 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
9357 static Btype
* empty_interface_type
;
9358 if (empty_interface_type
== NULL
)
9360 std::vector
<Backend::Btyped_identifier
> bfields(2);
9362 Location bloc
= Linemap::predeclared_location();
9364 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9365 bfields
[0].name
= "__type_descriptor";
9366 bfields
[0].btype
= pdt
->get_backend(gogo
);
9367 bfields
[0].location
= bloc
;
9369 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9370 bfields
[1].name
= "__object";
9371 bfields
[1].btype
= vt
->get_backend(gogo
);
9372 bfields
[1].location
= bloc
;
9374 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
9376 return empty_interface_type
;
9379 // Return a pointer to the backend representation of the method table.
9382 Interface_type::get_backend_methods(Gogo
* gogo
)
9384 if (this->bmethods_
!= NULL
&& !this->bmethods_is_placeholder_
)
9385 return this->bmethods_
;
9387 Location loc
= this->location();
9389 std::vector
<Backend::Btyped_identifier
>
9390 mfields(this->all_methods_
->size() + 1);
9392 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9393 mfields
[0].name
= "__type_descriptor";
9394 mfields
[0].btype
= pdt
->get_backend(gogo
);
9395 mfields
[0].location
= loc
;
9397 std::string last_name
= "";
9399 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9400 p
!= this->all_methods_
->end();
9403 // The type of the method in Go only includes the parameters.
9404 // The actual method also has a receiver, which is always a
9405 // pointer. We need to add that pointer type here in order to
9406 // generate the correct type for the backend.
9407 Function_type
* ft
= p
->type()->function_type();
9408 go_assert(ft
->receiver() == NULL
);
9410 const Typed_identifier_list
* params
= ft
->parameters();
9411 Typed_identifier_list
* mparams
= new Typed_identifier_list();
9413 mparams
->reserve(params
->size() + 1);
9414 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9415 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
9418 for (Typed_identifier_list::const_iterator pp
= params
->begin();
9419 pp
!= params
->end();
9421 mparams
->push_back(*pp
);
9424 Typed_identifier_list
* mresults
= (ft
->results() == NULL
9426 : ft
->results()->copy());
9427 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
9430 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
9431 mfields
[i
].btype
= mft
->get_backend_fntype(gogo
);
9432 mfields
[i
].location
= loc
;
9434 // Sanity check: the names should be sorted.
9435 go_assert(Gogo::unpack_hidden_name(p
->name())
9436 > Gogo::unpack_hidden_name(last_name
));
9437 last_name
= p
->name();
9440 Btype
* st
= gogo
->backend()->struct_type(mfields
);
9441 Btype
* ret
= gogo
->backend()->pointer_type(st
);
9443 if (this->bmethods_
!= NULL
&& this->bmethods_is_placeholder_
)
9444 gogo
->backend()->set_placeholder_pointer_type(this->bmethods_
, ret
);
9445 this->bmethods_
= ret
;
9446 this->bmethods_is_placeholder_
= false;
9450 // Return a placeholder for the pointer to the backend methods table.
9453 Interface_type::get_backend_methods_placeholder(Gogo
* gogo
)
9455 if (this->bmethods_
== NULL
)
9457 Location loc
= this->location();
9458 this->bmethods_
= gogo
->backend()->placeholder_pointer_type("", loc
,
9460 this->bmethods_is_placeholder_
= true;
9462 return this->bmethods_
;
9465 // Return the fields of a non-empty interface type. This is not
9466 // declared in types.h so that types.h doesn't have to #include
9470 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
9471 bool use_placeholder
,
9472 std::vector
<Backend::Btyped_identifier
>* bfields
)
9474 Location loc
= type
->location();
9478 (*bfields
)[0].name
= "__methods";
9479 (*bfields
)[0].btype
= (use_placeholder
9480 ? type
->get_backend_methods_placeholder(gogo
)
9481 : type
->get_backend_methods(gogo
));
9482 (*bfields
)[0].location
= loc
;
9484 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9485 (*bfields
)[1].name
= "__object";
9486 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
9487 (*bfields
)[1].location
= Linemap::predeclared_location();
9490 // Return the backend representation for an interface type. An interface is a
9491 // pointer to a struct. The struct has three fields. The first field is a
9492 // pointer to the type descriptor for the dynamic type of the object.
9493 // The second field is a pointer to a table of methods for the
9494 // interface to be used with the object. The third field is the value
9495 // of the object itself.
9498 Interface_type::do_get_backend(Gogo
* gogo
)
9500 if (this->is_empty())
9501 return Interface_type::get_backend_empty_interface_type(gogo
);
9504 if (this->interface_btype_
!= NULL
)
9505 return this->interface_btype_
;
9506 this->interface_btype_
=
9507 gogo
->backend()->placeholder_struct_type("", this->location_
);
9508 std::vector
<Backend::Btyped_identifier
> bfields
;
9509 get_backend_interface_fields(gogo
, this, false, &bfields
);
9510 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
9512 this->interface_btype_
= gogo
->backend()->error_type();
9513 return this->interface_btype_
;
9517 // Finish the backend representation of the methods.
9520 Interface_type::finish_backend_methods(Gogo
* gogo
)
9522 if (!this->is_empty())
9524 const Typed_identifier_list
* methods
= this->methods();
9525 if (methods
!= NULL
)
9527 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9528 p
!= methods
->end();
9530 p
->type()->get_backend(gogo
);
9533 // Getting the backend methods now will set the placeholder
9535 this->get_backend_methods(gogo
);
9539 // The type of an interface type descriptor.
9542 Interface_type::make_interface_type_descriptor_type()
9547 Type
* tdt
= Type::make_type_descriptor_type();
9548 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
9550 Type
* string_type
= Type::lookup_string_type();
9551 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
9554 Type::make_builtin_struct_type(3,
9555 "name", pointer_string_type
,
9556 "pkgPath", pointer_string_type
,
9559 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
9561 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
9563 Struct_type
* s
= Type::make_builtin_struct_type(2,
9565 "methods", slice_nsm
);
9567 ret
= Type::make_builtin_named_type("InterfaceType", s
);
9573 // Build a type descriptor for an interface type.
9576 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9578 Location bloc
= Linemap::predeclared_location();
9580 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
9582 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
9584 Expression_list
* ivals
= new Expression_list();
9587 Struct_field_list::const_iterator pif
= ifields
->begin();
9588 go_assert(pif
->is_field_name("_type"));
9589 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
9590 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
9594 go_assert(pif
->is_field_name("methods"));
9596 Expression_list
* methods
= new Expression_list();
9597 if (this->all_methods_
!= NULL
)
9599 Type
* elemtype
= pif
->type()->array_type()->element_type();
9601 methods
->reserve(this->all_methods_
->size());
9602 for (Typed_identifier_list::const_iterator pm
=
9603 this->all_methods_
->begin();
9604 pm
!= this->all_methods_
->end();
9607 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
9609 Expression_list
* mvals
= new Expression_list();
9612 Struct_field_list::const_iterator pmf
= mfields
->begin();
9613 go_assert(pmf
->is_field_name("name"));
9614 std::string s
= Gogo::unpack_hidden_name(pm
->name());
9615 Expression
* e
= Expression::make_string(s
, bloc
);
9616 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9619 go_assert(pmf
->is_field_name("pkgPath"));
9620 if (!Gogo::is_hidden_name(pm
->name()))
9621 mvals
->push_back(Expression::make_nil(bloc
));
9624 s
= Gogo::hidden_name_pkgpath(pm
->name());
9625 e
= Expression::make_string(s
, bloc
);
9626 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9630 go_assert(pmf
->is_field_name("typ"));
9631 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
9634 go_assert(pmf
== mfields
->end());
9636 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
9638 methods
->push_back(e
);
9642 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
9646 go_assert(pif
== ifields
->end());
9648 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
9651 // Reflection string.
9654 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9656 ret
->append("interface {");
9657 const Typed_identifier_list
* methods
= this->parse_methods_
;
9658 if (methods
!= NULL
)
9660 ret
->push_back(' ');
9661 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9662 p
!= methods
->end();
9665 if (p
!= methods
->begin())
9667 if (p
->name().empty())
9668 this->append_reflection(p
->type(), gogo
, ret
);
9671 if (!Gogo::is_hidden_name(p
->name()))
9672 ret
->append(p
->name());
9673 else if (gogo
->pkgpath_from_option())
9674 ret
->append(p
->name().substr(1));
9677 // If no -fgo-pkgpath option, backward compatibility
9678 // for how this used to work before -fgo-pkgpath was
9680 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
9681 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
9682 ret
->push_back('.');
9683 ret
->append(Gogo::unpack_hidden_name(p
->name()));
9685 std::string sub
= p
->type()->reflection(gogo
);
9686 go_assert(sub
.compare(0, 4, "func") == 0);
9687 sub
= sub
.substr(4);
9691 ret
->push_back(' ');
9699 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
9701 go_assert(this->methods_are_finalized_
);
9703 ret
->push_back('I');
9705 const Typed_identifier_list
* methods
= this->all_methods_
;
9706 if (methods
!= NULL
&& !this->seen_
)
9709 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9710 p
!= methods
->end();
9713 if (!p
->name().empty())
9715 std::string
n(Gogo::mangle_possibly_hidden_name(p
->name()));
9717 snprintf(buf
, sizeof buf
, "%u_",
9718 static_cast<unsigned int>(n
.length()));
9722 this->append_mangled_name(p
->type(), gogo
, ret
);
9724 this->seen_
= false;
9727 ret
->push_back('e');
9733 Interface_type::do_export(Export
* exp
) const
9735 exp
->write_c_string("interface { ");
9737 const Typed_identifier_list
* methods
= this->parse_methods_
;
9738 if (methods
!= NULL
)
9740 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
9741 pm
!= methods
->end();
9744 if (pm
->name().empty())
9746 exp
->write_c_string("? ");
9747 exp
->write_type(pm
->type());
9751 exp
->write_string(pm
->name());
9752 exp
->write_c_string(" (");
9754 const Function_type
* fntype
= pm
->type()->function_type();
9757 const Typed_identifier_list
* parameters
= fntype
->parameters();
9758 if (parameters
!= NULL
)
9760 bool is_varargs
= fntype
->is_varargs();
9761 for (Typed_identifier_list::const_iterator pp
=
9762 parameters
->begin();
9763 pp
!= parameters
->end();
9769 exp
->write_c_string(", ");
9770 exp
->write_name(pp
->name());
9771 exp
->write_c_string(" ");
9772 if (!is_varargs
|| pp
+ 1 != parameters
->end())
9773 exp
->write_type(pp
->type());
9776 exp
->write_c_string("...");
9777 Type
*pptype
= pp
->type();
9778 exp
->write_type(pptype
->array_type()->element_type());
9783 exp
->write_c_string(")");
9785 const Typed_identifier_list
* results
= fntype
->results();
9786 if (results
!= NULL
)
9788 exp
->write_c_string(" ");
9789 if (results
->size() == 1 && results
->begin()->name().empty())
9790 exp
->write_type(results
->begin()->type());
9794 exp
->write_c_string("(");
9795 for (Typed_identifier_list::const_iterator p
=
9797 p
!= results
->end();
9803 exp
->write_c_string(", ");
9804 exp
->write_name(p
->name());
9805 exp
->write_c_string(" ");
9806 exp
->write_type(p
->type());
9808 exp
->write_c_string(")");
9813 exp
->write_c_string("; ");
9817 exp
->write_c_string("}");
9820 // Import an interface type.
9823 Interface_type::do_import(Import
* imp
)
9825 imp
->require_c_string("interface { ");
9827 Typed_identifier_list
* methods
= new Typed_identifier_list
;
9828 while (imp
->peek_char() != '}')
9830 std::string name
= imp
->read_identifier();
9834 imp
->require_c_string(" ");
9835 Type
* t
= imp
->read_type();
9836 methods
->push_back(Typed_identifier("", t
, imp
->location()));
9837 imp
->require_c_string("; ");
9841 imp
->require_c_string(" (");
9843 Typed_identifier_list
* parameters
;
9844 bool is_varargs
= false;
9845 if (imp
->peek_char() == ')')
9849 parameters
= new Typed_identifier_list
;
9852 std::string name
= imp
->read_name();
9853 imp
->require_c_string(" ");
9855 if (imp
->match_c_string("..."))
9861 Type
* ptype
= imp
->read_type();
9863 ptype
= Type::make_array_type(ptype
, NULL
);
9864 parameters
->push_back(Typed_identifier(name
, ptype
,
9866 if (imp
->peek_char() != ',')
9868 go_assert(!is_varargs
);
9869 imp
->require_c_string(", ");
9872 imp
->require_c_string(")");
9874 Typed_identifier_list
* results
;
9875 if (imp
->peek_char() != ' ')
9879 results
= new Typed_identifier_list
;
9881 if (imp
->peek_char() != '(')
9883 Type
* rtype
= imp
->read_type();
9884 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
9891 std::string name
= imp
->read_name();
9892 imp
->require_c_string(" ");
9893 Type
* rtype
= imp
->read_type();
9894 results
->push_back(Typed_identifier(name
, rtype
,
9896 if (imp
->peek_char() != ',')
9898 imp
->require_c_string(", ");
9900 imp
->require_c_string(")");
9904 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
9908 fntype
->set_is_varargs();
9909 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
9911 imp
->require_c_string("; ");
9914 imp
->require_c_string("}");
9916 if (methods
->empty())
9922 Interface_type
* ret
= Type::make_interface_type(methods
, imp
->location());
9923 ret
->package_
= imp
->package();
9927 // Make an interface type.
9930 Type::make_interface_type(Typed_identifier_list
* methods
,
9933 return new Interface_type(methods
, location
);
9936 // Make an empty interface type.
9939 Type::make_empty_interface_type(Location location
)
9941 Interface_type
* ret
= new Interface_type(NULL
, location
);
9942 ret
->finalize_methods();
9948 // Bind a method to an object.
9951 Method::bind_method(Expression
* expr
, Location location
) const
9953 if (this->stub_
== NULL
)
9955 // When there is no stub object, the binding is determined by
9957 return this->do_bind_method(expr
, location
);
9959 return Expression::make_bound_method(expr
, this, this->stub_
, location
);
9962 // Return the named object associated with a method. This may only be
9963 // called after methods are finalized.
9966 Method::named_object() const
9968 if (this->stub_
!= NULL
)
9970 return this->do_named_object();
9973 // Class Named_method.
9975 // The type of the method.
9978 Named_method::do_type() const
9980 if (this->named_object_
->is_function())
9981 return this->named_object_
->func_value()->type();
9982 else if (this->named_object_
->is_function_declaration())
9983 return this->named_object_
->func_declaration_value()->type();
9988 // Return the location of the method receiver.
9991 Named_method::do_receiver_location() const
9993 return this->do_type()->receiver()->location();
9996 // Bind a method to an object.
9999 Named_method::do_bind_method(Expression
* expr
, Location location
) const
10001 Named_object
* no
= this->named_object_
;
10002 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, this,
10004 // If this is not a local method, and it does not use a stub, then
10005 // the real method expects a different type. We need to cast the
10007 if (this->depth() > 0 && !this->needs_stub_method())
10009 Function_type
* ftype
= this->do_type();
10010 go_assert(ftype
->is_method());
10011 Type
* frtype
= ftype
->receiver()->type();
10012 bme
->set_first_argument_type(frtype
);
10017 // Return whether this method should not participate in interfaces.
10020 Named_method::do_nointerface() const
10022 Named_object
* no
= this->named_object_
;
10023 return no
->is_function() && no
->func_value()->nointerface();
10026 // Class Interface_method.
10028 // Bind a method to an object.
10031 Interface_method::do_bind_method(Expression
* expr
,
10032 Location location
) const
10034 return Expression::make_interface_field_reference(expr
, this->name_
,
10040 // Insert a new method. Return true if it was inserted, false
10044 Methods::insert(const std::string
& name
, Method
* m
)
10046 std::pair
<Method_map::iterator
, bool> ins
=
10047 this->methods_
.insert(std::make_pair(name
, m
));
10052 Method
* old_method
= ins
.first
->second
;
10053 if (m
->depth() < old_method
->depth())
10056 ins
.first
->second
= m
;
10061 if (m
->depth() == old_method
->depth())
10062 old_method
->set_is_ambiguous();
10068 // Return the number of unambiguous methods.
10071 Methods::count() const
10074 for (Method_map::const_iterator p
= this->methods_
.begin();
10075 p
!= this->methods_
.end();
10077 if (!p
->second
->is_ambiguous())
10082 // Class Named_type.
10084 // Return the name of the type.
10087 Named_type::name() const
10089 return this->named_object_
->name();
10092 // Return the name of the type to use in an error message.
10095 Named_type::message_name() const
10097 return this->named_object_
->message_name();
10100 // Return the base type for this type. We have to be careful about
10101 // circular type definitions, which are invalid but may be seen here.
10104 Named_type::named_base()
10108 this->seen_
= true;
10109 Type
* ret
= this->type_
->base();
10110 this->seen_
= false;
10115 Named_type::named_base() const
10119 this->seen_
= true;
10120 const Type
* ret
= this->type_
->base();
10121 this->seen_
= false;
10125 // Return whether this is an error type. We have to be careful about
10126 // circular type definitions, which are invalid but may be seen here.
10129 Named_type::is_named_error_type() const
10133 this->seen_
= true;
10134 bool ret
= this->type_
->is_error_type();
10135 this->seen_
= false;
10139 // Whether this type is comparable. We have to be careful about
10140 // circular type definitions.
10143 Named_type::named_type_is_comparable(std::string
* reason
) const
10147 this->seen_
= true;
10148 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
10149 this->type_
, reason
);
10150 this->seen_
= false;
10154 // Add a method to this type.
10157 Named_type::add_method(const std::string
& name
, Function
* function
)
10159 go_assert(!this->is_alias_
);
10160 if (this->local_methods_
== NULL
)
10161 this->local_methods_
= new Bindings(NULL
);
10162 return this->local_methods_
->add_function(name
, NULL
, function
);
10165 // Add a method declaration to this type.
10168 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
10169 Function_type
* type
,
10172 go_assert(!this->is_alias_
);
10173 if (this->local_methods_
== NULL
)
10174 this->local_methods_
= new Bindings(NULL
);
10175 return this->local_methods_
->add_function_declaration(name
, package
, type
,
10179 // Add an existing method to this type.
10182 Named_type::add_existing_method(Named_object
* no
)
10184 go_assert(!this->is_alias_
);
10185 if (this->local_methods_
== NULL
)
10186 this->local_methods_
= new Bindings(NULL
);
10187 this->local_methods_
->add_named_object(no
);
10190 // Look for a local method NAME, and returns its named object, or NULL
10194 Named_type::find_local_method(const std::string
& name
) const
10196 if (this->is_error_
)
10198 if (this->is_alias_
)
10200 Named_type
* nt
= this->type_
->named_type();
10203 if (this->seen_alias_
)
10205 this->seen_alias_
= true;
10206 Named_object
* ret
= nt
->find_local_method(name
);
10207 this->seen_alias_
= false;
10212 if (this->local_methods_
== NULL
)
10214 return this->local_methods_
->lookup(name
);
10217 // Return the list of local methods.
10220 Named_type::local_methods() const
10222 if (this->is_error_
)
10224 if (this->is_alias_
)
10226 Named_type
* nt
= this->type_
->named_type();
10229 if (this->seen_alias_
)
10231 this->seen_alias_
= true;
10232 const Bindings
* ret
= nt
->local_methods();
10233 this->seen_alias_
= false;
10238 return this->local_methods_
;
10241 // Return whether NAME is an unexported field or method, for better
10242 // error reporting.
10245 Named_type::is_unexported_local_method(Gogo
* gogo
,
10246 const std::string
& name
) const
10248 if (this->is_error_
)
10250 if (this->is_alias_
)
10252 Named_type
* nt
= this->type_
->named_type();
10255 if (this->seen_alias_
)
10257 this->seen_alias_
= true;
10258 bool ret
= nt
->is_unexported_local_method(gogo
, name
);
10259 this->seen_alias_
= false;
10264 Bindings
* methods
= this->local_methods_
;
10265 if (methods
!= NULL
)
10267 for (Bindings::const_declarations_iterator p
=
10268 methods
->begin_declarations();
10269 p
!= methods
->end_declarations();
10272 if (Gogo::is_hidden_name(p
->first
)
10273 && name
== Gogo::unpack_hidden_name(p
->first
)
10274 && gogo
->pack_hidden_name(name
, false) != p
->first
)
10281 // Build the complete list of methods for this type, which means
10282 // recursively including all methods for anonymous fields. Create all
10286 Named_type::finalize_methods(Gogo
* gogo
)
10288 if (this->is_alias_
)
10290 if (this->all_methods_
!= NULL
)
10293 if (this->local_methods_
!= NULL
10294 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
10296 const Bindings
* lm
= this->local_methods_
;
10297 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
10298 p
!= lm
->end_declarations();
10300 go_error_at(p
->second
->location(),
10301 "invalid pointer or interface receiver type");
10302 delete this->local_methods_
;
10303 this->local_methods_
= NULL
;
10307 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
10310 // Return whether this type has any methods.
10313 Named_type::has_any_methods() const
10315 if (this->is_error_
)
10317 if (this->is_alias_
)
10319 if (this->type_
->named_type() != NULL
)
10321 if (this->seen_alias_
)
10323 this->seen_alias_
= true;
10324 bool ret
= this->type_
->named_type()->has_any_methods();
10325 this->seen_alias_
= false;
10328 if (this->type_
->struct_type() != NULL
)
10329 return this->type_
->struct_type()->has_any_methods();
10332 return this->all_methods_
!= NULL
;
10335 // Return the methods for this type.
10338 Named_type::methods() const
10340 if (this->is_error_
)
10342 if (this->is_alias_
)
10344 if (this->type_
->named_type() != NULL
)
10346 if (this->seen_alias_
)
10348 this->seen_alias_
= true;
10349 const Methods
* ret
= this->type_
->named_type()->methods();
10350 this->seen_alias_
= false;
10353 if (this->type_
->struct_type() != NULL
)
10354 return this->type_
->struct_type()->methods();
10357 return this->all_methods_
;
10360 // Return the method NAME, or NULL if there isn't one or if it is
10361 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
10365 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
10367 if (this->is_error_
)
10369 if (this->is_alias_
)
10371 if (is_ambiguous
!= NULL
)
10372 *is_ambiguous
= false;
10373 if (this->type_
->named_type() != NULL
)
10375 if (this->seen_alias_
)
10377 this->seen_alias_
= true;
10378 Named_type
* nt
= this->type_
->named_type();
10379 Method
* ret
= nt
->method_function(name
, is_ambiguous
);
10380 this->seen_alias_
= false;
10383 if (this->type_
->struct_type() != NULL
)
10384 return this->type_
->struct_type()->method_function(name
, is_ambiguous
);
10387 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
10390 // Return a pointer to the interface method table for this type for
10391 // the interface INTERFACE. IS_POINTER is true if this is for a
10392 // pointer to THIS.
10395 Named_type::interface_method_table(Interface_type
* interface
, bool is_pointer
)
10397 if (this->is_error_
)
10398 return Expression::make_error(this->location_
);
10399 if (this->is_alias_
)
10401 if (this->type_
->named_type() != NULL
)
10403 if (this->seen_alias_
)
10404 return Expression::make_error(this->location_
);
10405 this->seen_alias_
= true;
10406 Named_type
* nt
= this->type_
->named_type();
10407 Expression
* ret
= nt
->interface_method_table(interface
, is_pointer
);
10408 this->seen_alias_
= false;
10411 if (this->type_
->struct_type() != NULL
)
10412 return this->type_
->struct_type()->interface_method_table(interface
,
10416 return Type::interface_method_table(this, interface
, is_pointer
,
10417 &this->interface_method_tables_
,
10418 &this->pointer_interface_method_tables_
);
10421 // Look for a use of a complete type within another type. This is
10422 // used to check that we don't try to use a type within itself.
10424 class Find_type_use
: public Traverse
10427 Find_type_use(Named_type
* find_type
)
10428 : Traverse(traverse_types
),
10429 find_type_(find_type
), found_(false)
10432 // Whether we found the type.
10435 { return this->found_
; }
10442 // The type we are looking for.
10443 Named_type
* find_type_
;
10444 // Whether we found the type.
10448 // Check for FIND_TYPE in TYPE.
10451 Find_type_use::type(Type
* type
)
10453 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
10455 this->found_
= true;
10456 return TRAVERSE_EXIT
;
10459 // It's OK if we see a reference to the type in any type which is
10460 // essentially a pointer: a pointer, a slice, a function, a map, or
10462 if (type
->points_to() != NULL
10463 || type
->is_slice_type()
10464 || type
->function_type() != NULL
10465 || type
->map_type() != NULL
10466 || type
->channel_type() != NULL
)
10467 return TRAVERSE_SKIP_COMPONENTS
;
10469 // For an interface, a reference to the type in a method type should
10470 // be ignored, but we have to consider direct inheritance. When
10471 // this is called, there may be cases of direct inheritance
10472 // represented as a method with no name.
10473 if (type
->interface_type() != NULL
)
10475 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
10476 if (methods
!= NULL
)
10478 for (Typed_identifier_list::const_iterator p
= methods
->begin();
10479 p
!= methods
->end();
10482 if (p
->name().empty())
10484 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
10485 return TRAVERSE_EXIT
;
10489 return TRAVERSE_SKIP_COMPONENTS
;
10492 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
10493 // to convert TYPE to the backend representation before we convert
10495 if (type
->named_type() != NULL
)
10497 switch (type
->base()->classification())
10499 case Type::TYPE_ERROR
:
10500 case Type::TYPE_BOOLEAN
:
10501 case Type::TYPE_INTEGER
:
10502 case Type::TYPE_FLOAT
:
10503 case Type::TYPE_COMPLEX
:
10504 case Type::TYPE_STRING
:
10505 case Type::TYPE_NIL
:
10508 case Type::TYPE_ARRAY
:
10509 case Type::TYPE_STRUCT
:
10510 this->find_type_
->add_dependency(type
->named_type());
10513 case Type::TYPE_NAMED
:
10514 case Type::TYPE_FORWARD
:
10515 go_assert(saw_errors());
10518 case Type::TYPE_VOID
:
10519 case Type::TYPE_SINK
:
10520 case Type::TYPE_FUNCTION
:
10521 case Type::TYPE_POINTER
:
10522 case Type::TYPE_CALL_MULTIPLE_RESULT
:
10523 case Type::TYPE_MAP
:
10524 case Type::TYPE_CHANNEL
:
10525 case Type::TYPE_INTERFACE
:
10531 return TRAVERSE_CONTINUE
;
10534 // Look for a circular reference of an alias.
10536 class Find_alias
: public Traverse
10539 Find_alias(Named_type
* find_type
)
10540 : Traverse(traverse_types
),
10541 find_type_(find_type
), found_(false)
10544 // Whether we found the type.
10547 { return this->found_
; }
10554 // The type we are looking for.
10555 Named_type
* find_type_
;
10556 // Whether we found the type.
10561 Find_alias::type(Type
* type
)
10563 Named_type
* nt
= type
->named_type();
10566 if (nt
== this->find_type_
)
10568 this->found_
= true;
10569 return TRAVERSE_EXIT
;
10572 // We started from `type T1 = T2`, where T1 is find_type_ and T2
10573 // is, perhaps indirectly, the parameter TYPE. If TYPE is not
10574 // an alias itself, it's OK if whatever T2 is defined as refers
10576 if (!nt
->is_alias())
10577 return TRAVERSE_SKIP_COMPONENTS
;
10580 return TRAVERSE_CONTINUE
;
10583 // Verify that a named type does not refer to itself.
10586 Named_type::do_verify()
10588 if (this->is_verified_
)
10590 this->is_verified_
= true;
10592 if (this->is_error_
)
10595 if (this->is_alias_
)
10597 Find_alias
find(this);
10598 Type::traverse(this->type_
, &find
);
10601 go_error_at(this->location_
, "invalid recursive alias %qs",
10602 this->message_name().c_str());
10603 this->is_error_
= true;
10608 Find_type_use
find(this);
10609 Type::traverse(this->type_
, &find
);
10612 go_error_at(this->location_
, "invalid recursive type %qs",
10613 this->message_name().c_str());
10614 this->is_error_
= true;
10618 // Check whether any of the local methods overloads an existing
10619 // struct field or interface method. We don't need to check the
10620 // list of methods against itself: that is handled by the Bindings
10622 if (this->local_methods_
!= NULL
)
10624 Struct_type
* st
= this->type_
->struct_type();
10627 for (Bindings::const_declarations_iterator p
=
10628 this->local_methods_
->begin_declarations();
10629 p
!= this->local_methods_
->end_declarations();
10632 const std::string
& name(p
->first
);
10633 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
10635 go_error_at(p
->second
->location(),
10636 "method %qs redeclares struct field name",
10637 Gogo::message_name(name
).c_str());
10646 // Return whether this type is or contains a pointer.
10649 Named_type::do_has_pointer() const
10653 this->seen_
= true;
10654 bool ret
= this->type_
->has_pointer();
10655 this->seen_
= false;
10659 // Return whether comparisons for this type can use the identity
10663 Named_type::do_compare_is_identity(Gogo
* gogo
)
10665 // We don't use this->seen_ here because compare_is_identity may
10666 // call base() later, and that will mess up if seen_ is set here.
10667 if (this->seen_in_compare_is_identity_
)
10669 this->seen_in_compare_is_identity_
= true;
10670 bool ret
= this->type_
->compare_is_identity(gogo
);
10671 this->seen_in_compare_is_identity_
= false;
10675 // Return whether this type is reflexive--whether it is always equal
10679 Named_type::do_is_reflexive()
10681 if (this->seen_in_compare_is_identity_
)
10683 this->seen_in_compare_is_identity_
= true;
10684 bool ret
= this->type_
->is_reflexive();
10685 this->seen_in_compare_is_identity_
= false;
10689 // Return whether this type needs a key update when used as a map key.
10692 Named_type::do_needs_key_update()
10694 if (this->seen_in_compare_is_identity_
)
10696 this->seen_in_compare_is_identity_
= true;
10697 bool ret
= this->type_
->needs_key_update();
10698 this->seen_in_compare_is_identity_
= false;
10702 // Return a hash code. This is used for method lookup. We simply
10703 // hash on the name itself.
10706 Named_type::do_hash_for_method(Gogo
* gogo
) const
10708 if (this->is_error_
)
10711 // Aliases are handled in Type::hash_for_method.
10712 go_assert(!this->is_alias_
);
10714 const std::string
& name(this->named_object()->name());
10715 unsigned int ret
= Type::hash_string(name
, 0);
10717 // GOGO will be NULL here when called from Type_hash_identical.
10718 // That is OK because that is only used for internal hash tables
10719 // where we are going to be comparing named types for equality. In
10720 // other cases, which are cases where the runtime is going to
10721 // compare hash codes to see if the types are the same, we need to
10722 // include the pkgpath in the hash.
10723 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
10725 const Package
* package
= this->named_object()->package();
10726 if (package
== NULL
)
10727 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
10729 ret
= Type::hash_string(package
->pkgpath(), ret
);
10735 // Convert a named type to the backend representation. In order to
10736 // get dependencies right, we fill in a dummy structure for this type,
10737 // then convert all the dependencies, then complete this type. When
10738 // this function is complete, the size of the type is known.
10741 Named_type::convert(Gogo
* gogo
)
10743 if (this->is_error_
|| this->is_converted_
)
10746 this->create_placeholder(gogo
);
10748 // If we are called to turn unsafe.Sizeof into a constant, we may
10749 // not have verified the type yet. We have to make sure it is
10750 // verified, since that sets the list of dependencies.
10753 // Convert all the dependencies. If they refer indirectly back to
10754 // this type, they will pick up the intermediate representation we just
10756 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
10757 p
!= this->dependencies_
.end();
10759 (*p
)->convert(gogo
);
10761 // Complete this type.
10762 Btype
* bt
= this->named_btype_
;
10763 Type
* base
= this->type_
->base();
10764 switch (base
->classification())
10779 case TYPE_FUNCTION
:
10781 // The size of these types is already correct. We don't worry
10782 // about filling them in until later, when we also track
10783 // circular references.
10788 std::vector
<Backend::Btyped_identifier
> bfields
;
10789 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
10791 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10792 bt
= gogo
->backend()->error_type();
10797 // Slice types were completed in create_placeholder.
10798 if (!base
->is_slice_type())
10800 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
10801 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
10802 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
10803 bt
= gogo
->backend()->error_type();
10807 case TYPE_INTERFACE
:
10808 // Interface types were completed in create_placeholder.
10816 case TYPE_CALL_MULTIPLE_RESULT
:
10822 this->named_btype_
= bt
;
10823 this->is_converted_
= true;
10824 this->is_placeholder_
= false;
10827 // Create the placeholder for a named type. This is the first step in
10828 // converting to the backend representation.
10831 Named_type::create_placeholder(Gogo
* gogo
)
10833 if (this->is_error_
)
10834 this->named_btype_
= gogo
->backend()->error_type();
10836 if (this->named_btype_
!= NULL
)
10839 // Create the structure for this type. Note that because we call
10840 // base() here, we don't attempt to represent a named type defined
10841 // as another named type. Instead both named types will point to
10842 // different base representations.
10843 Type
* base
= this->type_
->base();
10845 bool set_name
= true;
10846 switch (base
->classification())
10849 this->is_error_
= true;
10850 this->named_btype_
= gogo
->backend()->error_type();
10860 // These are simple basic types, we can just create them
10862 bt
= Type::get_named_base_btype(gogo
, base
);
10867 // All maps and channels have the same backend representation.
10868 bt
= Type::get_named_base_btype(gogo
, base
);
10871 case TYPE_FUNCTION
:
10874 bool for_function
= base
->classification() == TYPE_FUNCTION
;
10875 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
10883 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10885 this->is_placeholder_
= true;
10890 if (base
->is_slice_type())
10891 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10895 bt
= gogo
->backend()->placeholder_array_type(this->name(),
10897 this->is_placeholder_
= true;
10902 case TYPE_INTERFACE
:
10903 if (base
->interface_type()->is_empty())
10904 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
10907 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10915 case TYPE_CALL_MULTIPLE_RESULT
:
10922 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
10924 this->named_btype_
= bt
;
10926 if (base
->is_slice_type())
10928 // We do not record slices as dependencies of other types,
10929 // because we can fill them in completely here with the final
10931 std::vector
<Backend::Btyped_identifier
> bfields
;
10932 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
10933 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10934 this->named_btype_
= gogo
->backend()->error_type();
10936 else if (base
->interface_type() != NULL
10937 && !base
->interface_type()->is_empty())
10939 // We do not record interfaces as dependencies of other types,
10940 // because we can fill them in completely here with the final
10942 std::vector
<Backend::Btyped_identifier
> bfields
;
10943 get_backend_interface_fields(gogo
, base
->interface_type(), true,
10945 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10946 this->named_btype_
= gogo
->backend()->error_type();
10950 // Get the backend representation for a named type.
10953 Named_type::do_get_backend(Gogo
* gogo
)
10955 if (this->is_error_
)
10956 return gogo
->backend()->error_type();
10958 Btype
* bt
= this->named_btype_
;
10960 if (!gogo
->named_types_are_converted())
10962 // We have not completed converting named types. NAMED_BTYPE_
10963 // is a placeholder and we shouldn't do anything further.
10967 // We don't build dependencies for types whose sizes do not
10968 // change or are not relevant, so we may see them here while
10969 // converting types.
10970 this->create_placeholder(gogo
);
10971 bt
= this->named_btype_
;
10972 go_assert(bt
!= NULL
);
10976 // We are not converting types. This should only be called if the
10977 // type has already been converted.
10978 if (!this->is_converted_
)
10980 go_assert(saw_errors());
10981 return gogo
->backend()->error_type();
10984 go_assert(bt
!= NULL
);
10986 // Complete the backend representation.
10987 Type
* base
= this->type_
->base();
10989 switch (base
->classification())
10992 return gogo
->backend()->error_type();
11006 if (!this->seen_in_get_backend_
)
11008 this->seen_in_get_backend_
= true;
11009 base
->struct_type()->finish_backend_fields(gogo
);
11010 this->seen_in_get_backend_
= false;
11015 if (!this->seen_in_get_backend_
)
11017 this->seen_in_get_backend_
= true;
11018 base
->array_type()->finish_backend_element(gogo
);
11019 this->seen_in_get_backend_
= false;
11023 case TYPE_INTERFACE
:
11024 if (!this->seen_in_get_backend_
)
11026 this->seen_in_get_backend_
= true;
11027 base
->interface_type()->finish_backend_methods(gogo
);
11028 this->seen_in_get_backend_
= false;
11032 case TYPE_FUNCTION
:
11033 // Don't build a circular data structure. GENERIC can't handle
11035 if (this->seen_in_get_backend_
)
11037 this->is_circular_
= true;
11038 return gogo
->backend()->circular_pointer_type(bt
, true);
11040 this->seen_in_get_backend_
= true;
11041 bt1
= Type::get_named_base_btype(gogo
, base
);
11042 this->seen_in_get_backend_
= false;
11043 if (this->is_circular_
)
11044 bt1
= gogo
->backend()->circular_pointer_type(bt
, true);
11045 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
11046 bt
= gogo
->backend()->error_type();
11050 // Don't build a circular data structure. GENERIC can't handle
11052 if (this->seen_in_get_backend_
)
11054 this->is_circular_
= true;
11055 return gogo
->backend()->circular_pointer_type(bt
, false);
11057 this->seen_in_get_backend_
= true;
11058 bt1
= Type::get_named_base_btype(gogo
, base
);
11059 this->seen_in_get_backend_
= false;
11060 if (this->is_circular_
)
11061 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
11062 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
11063 bt
= gogo
->backend()->error_type();
11068 case TYPE_CALL_MULTIPLE_RESULT
:
11077 // Build a type descriptor for a named type.
11080 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
11082 if (this->is_error_
)
11083 return Expression::make_error(this->location_
);
11084 if (name
== NULL
&& this->is_alias_
)
11086 if (this->seen_alias_
)
11087 return Expression::make_error(this->location_
);
11088 this->seen_alias_
= true;
11089 Expression
* ret
= this->type_
->type_descriptor(gogo
, NULL
);
11090 this->seen_alias_
= false;
11094 // If NAME is not NULL, then we don't really want the type
11095 // descriptor for this type; we want the descriptor for the
11096 // underlying type, giving it the name NAME.
11097 return this->named_type_descriptor(gogo
, this->type_
,
11098 name
== NULL
? this : name
);
11101 // Add to the reflection string. This is used mostly for the name of
11102 // the type used in a type descriptor, not for actual reflection
11106 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
11108 this->append_reflection_type_name(gogo
, false, ret
);
11111 // Add to the reflection string. For an alias we normally use the
11112 // real name, but if USE_ALIAS is true we use the alias name itself.
11115 Named_type::append_reflection_type_name(Gogo
* gogo
, bool use_alias
,
11116 std::string
* ret
) const
11118 if (this->is_error_
)
11120 if (this->is_alias_
&& !use_alias
)
11122 if (this->seen_alias_
)
11124 this->seen_alias_
= true;
11125 this->append_reflection(this->type_
, gogo
, ret
);
11126 this->seen_alias_
= false;
11129 if (!this->is_builtin())
11131 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
11132 // make a unique reflection string, so that the type
11133 // canonicalization in the reflect package will work. In order
11134 // to be compatible with the gc compiler, we put tabs into the
11135 // package path, so that the reflect methods can discard it.
11136 const Package
* package
= this->named_object_
->package();
11137 ret
->push_back('\t');
11138 ret
->append(package
!= NULL
11139 ? package
->pkgpath_symbol()
11140 : gogo
->pkgpath_symbol());
11141 ret
->push_back('\t');
11142 ret
->append(package
!= NULL
11143 ? package
->package_name()
11144 : gogo
->package_name());
11145 ret
->push_back('.');
11147 if (this->in_function_
!= NULL
)
11149 ret
->push_back('\t');
11150 const Typed_identifier
* rcvr
=
11151 this->in_function_
->func_value()->type()->receiver();
11154 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
11155 ret
->append(Gogo::unpack_hidden_name(rcvr_type
->name()));
11156 ret
->push_back('.');
11158 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
11159 ret
->push_back('$');
11160 if (this->in_function_index_
> 0)
11163 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
11165 ret
->push_back('$');
11167 ret
->push_back('\t');
11169 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
11172 // Get the mangled name.
11175 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
11177 this->append_mangled_type_name(gogo
, false, ret
);
11180 // Get the mangled name. For an alias we normally get the real name,
11181 // but if USE_ALIAS is true we use the alias name itself.
11184 Named_type::append_mangled_type_name(Gogo
* gogo
, bool use_alias
,
11185 std::string
* ret
) const
11187 if (this->is_error_
)
11189 if (this->is_alias_
&& !use_alias
)
11191 if (this->seen_alias_
)
11193 this->seen_alias_
= true;
11194 this->append_mangled_name(this->type_
, gogo
, ret
);
11195 this->seen_alias_
= false;
11198 Named_object
* no
= this->named_object_
;
11200 if (this->is_builtin())
11201 go_assert(this->in_function_
== NULL
);
11204 const std::string
& pkgpath(no
->package() == NULL
11205 ? gogo
->pkgpath_symbol()
11206 : no
->package()->pkgpath_symbol());
11208 name
.append(1, '.');
11209 if (this->in_function_
!= NULL
)
11211 const Typed_identifier
* rcvr
=
11212 this->in_function_
->func_value()->type()->receiver();
11215 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
11216 name
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
11217 name
.append(1, '.');
11219 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
11220 name
.append(1, '$');
11221 if (this->in_function_index_
> 0)
11224 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
11226 name
.append(1, '$');
11230 name
.append(Gogo::unpack_hidden_name(no
->name()));
11232 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
11237 // Export the type. This is called to export a global type.
11240 Named_type::export_named_type(Export
* exp
, const std::string
&) const
11242 // We don't need to write the name of the type here, because it will
11243 // be written by Export::write_type anyhow.
11244 exp
->write_c_string("type ");
11245 exp
->write_type(this);
11246 exp
->write_c_string(";\n");
11249 // Import a named type.
11252 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
11254 imp
->require_c_string("type ");
11255 Type
*type
= imp
->read_type();
11256 *ptype
= type
->named_type();
11257 go_assert(*ptype
!= NULL
);
11258 imp
->require_c_string(";\n");
11261 // Export the type when it is referenced by another type. In this
11262 // case Export::export_type will already have issued the name.
11265 Named_type::do_export(Export
* exp
) const
11267 exp
->write_type(this->type_
);
11269 // To save space, we only export the methods directly attached to
11271 Bindings
* methods
= this->local_methods_
;
11272 if (methods
== NULL
)
11275 exp
->write_c_string("\n");
11276 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
11277 p
!= methods
->end_definitions();
11280 exp
->write_c_string(" ");
11281 (*p
)->export_named_object(exp
);
11284 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
11285 p
!= methods
->end_declarations();
11288 if (p
->second
->is_function_declaration())
11290 exp
->write_c_string(" ");
11291 p
->second
->export_named_object(exp
);
11296 // Make a named type.
11299 Type::make_named_type(Named_object
* named_object
, Type
* type
,
11302 return new Named_type(named_object
, type
, location
);
11305 // Finalize the methods for TYPE. It will be a named type or a struct
11306 // type. This sets *ALL_METHODS to the list of methods, and builds
11307 // all required stubs.
11310 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
11311 Methods
** all_methods
)
11313 *all_methods
= new Methods();
11314 std::vector
<const Named_type
*> seen
;
11315 Type::add_methods_for_type(type
, NULL
, 0, false, false, &seen
, *all_methods
);
11316 if ((*all_methods
)->empty())
11318 delete *all_methods
;
11319 *all_methods
= NULL
;
11321 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
11324 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
11325 // build up the struct field indexes as we go. DEPTH is the depth of
11326 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
11327 // adding these methods for an anonymous field with pointer type.
11328 // NEEDS_STUB_METHOD is true if we need to use a stub method which
11329 // calls the real method. TYPES_SEEN is used to avoid infinite
11333 Type::add_methods_for_type(const Type
* type
,
11334 const Method::Field_indexes
* field_indexes
,
11335 unsigned int depth
,
11336 bool is_embedded_pointer
,
11337 bool needs_stub_method
,
11338 std::vector
<const Named_type
*>* seen
,
11341 // Pointer types may not have methods.
11342 if (type
->points_to() != NULL
)
11345 const Named_type
* nt
= type
->named_type();
11348 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11356 seen
->push_back(nt
);
11358 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
11359 is_embedded_pointer
, needs_stub_method
,
11363 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
11364 is_embedded_pointer
, needs_stub_method
,
11367 // If we are called with depth > 0, then we are looking at an
11368 // anonymous field of a struct. If such a field has interface type,
11369 // then we need to add the interface methods. We don't want to add
11370 // them when depth == 0, because we will already handle them
11371 // following the usual rules for an interface type.
11373 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
11379 // Add the local methods for the named type NT to *METHODS. The
11380 // parameters are as for add_methods_to_type.
11383 Type::add_local_methods_for_type(const Named_type
* nt
,
11384 const Method::Field_indexes
* field_indexes
,
11385 unsigned int depth
,
11386 bool is_embedded_pointer
,
11387 bool needs_stub_method
,
11390 const Bindings
* local_methods
= nt
->local_methods();
11391 if (local_methods
== NULL
)
11394 for (Bindings::const_declarations_iterator p
=
11395 local_methods
->begin_declarations();
11396 p
!= local_methods
->end_declarations();
11399 Named_object
* no
= p
->second
;
11400 bool is_value_method
= (is_embedded_pointer
11401 || !Type::method_expects_pointer(no
));
11402 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
11403 (needs_stub_method
|| depth
> 0));
11404 if (!methods
->insert(no
->name(), m
))
11409 // Add the embedded methods for TYPE to *METHODS. These are the
11410 // methods attached to anonymous fields. The parameters are as for
11411 // add_methods_to_type.
11414 Type::add_embedded_methods_for_type(const Type
* type
,
11415 const Method::Field_indexes
* field_indexes
,
11416 unsigned int depth
,
11417 bool is_embedded_pointer
,
11418 bool needs_stub_method
,
11419 std::vector
<const Named_type
*>* seen
,
11422 // Look for anonymous fields in TYPE. TYPE has fields if it is a
11424 const Struct_type
* st
= type
->struct_type();
11428 const Struct_field_list
* fields
= st
->fields();
11429 if (fields
== NULL
)
11432 unsigned int i
= 0;
11433 for (Struct_field_list::const_iterator pf
= fields
->begin();
11434 pf
!= fields
->end();
11437 if (!pf
->is_anonymous())
11440 Type
* ftype
= pf
->type();
11441 bool is_pointer
= false;
11442 if (ftype
->points_to() != NULL
)
11444 ftype
= ftype
->points_to();
11447 Named_type
* fnt
= ftype
->named_type();
11450 // This is an error, but it will be diagnosed elsewhere.
11454 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
11455 sub_field_indexes
->next
= field_indexes
;
11456 sub_field_indexes
->field_index
= i
;
11458 Methods tmp_methods
;
11459 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
11460 (is_embedded_pointer
|| is_pointer
),
11466 // Check if there are promoted methods that conflict with field names and
11467 // don't add them to the method map.
11468 for (Methods::const_iterator p
= tmp_methods
.begin();
11469 p
!= tmp_methods
.end();
11472 bool found
= false;
11473 for (Struct_field_list::const_iterator fp
= fields
->begin();
11474 fp
!= fields
->end();
11477 if (fp
->field_name() == p
->first
)
11484 !methods
->insert(p
->first
, p
->second
))
11490 // If TYPE is an interface type, then add its method to *METHODS.
11491 // This is for interface methods attached to an anonymous field. The
11492 // parameters are as for add_methods_for_type.
11495 Type::add_interface_methods_for_type(const Type
* type
,
11496 const Method::Field_indexes
* field_indexes
,
11497 unsigned int depth
,
11500 const Interface_type
* it
= type
->interface_type();
11504 const Typed_identifier_list
* imethods
= it
->methods();
11505 if (imethods
== NULL
)
11508 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
11509 pm
!= imethods
->end();
11512 Function_type
* fntype
= pm
->type()->function_type();
11513 if (fntype
== NULL
)
11515 // This is an error, but it should be reported elsewhere
11516 // when we look at the methods for IT.
11519 go_assert(!fntype
->is_method());
11520 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
11521 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
11522 field_indexes
, depth
);
11523 if (!methods
->insert(pm
->name(), m
))
11528 // Build stub methods for TYPE as needed. METHODS is the set of
11529 // methods for the type. A stub method may be needed when a type
11530 // inherits a method from an anonymous field. When we need the
11531 // address of the method, as in a type descriptor, we need to build a
11532 // little stub which does the required field dereferences and jumps to
11533 // the real method. LOCATION is the location of the type definition.
11536 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
11539 if (methods
== NULL
)
11541 for (Methods::const_iterator p
= methods
->begin();
11542 p
!= methods
->end();
11545 Method
* m
= p
->second
;
11546 if (m
->is_ambiguous() || !m
->needs_stub_method())
11549 const std::string
& name(p
->first
);
11551 // Build a stub method.
11553 const Function_type
* fntype
= m
->type();
11555 static unsigned int counter
;
11557 snprintf(buf
, sizeof buf
, "$this%u", counter
);
11560 Type
* receiver_type
= const_cast<Type
*>(type
);
11561 if (!m
->is_value_method())
11562 receiver_type
= Type::make_pointer_type(receiver_type
);
11563 Location receiver_location
= m
->receiver_location();
11564 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
11565 receiver_location
);
11567 const Typed_identifier_list
* fnparams
= fntype
->parameters();
11568 Typed_identifier_list
* stub_params
;
11569 if (fnparams
== NULL
|| fnparams
->empty())
11570 stub_params
= NULL
;
11573 // We give each stub parameter a unique name.
11574 stub_params
= new Typed_identifier_list();
11575 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
11576 pp
!= fnparams
->end();
11580 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
11581 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
11587 const Typed_identifier_list
* fnresults
= fntype
->results();
11588 Typed_identifier_list
* stub_results
;
11589 if (fnresults
== NULL
|| fnresults
->empty())
11590 stub_results
= NULL
;
11593 // We create the result parameters without any names, since
11594 // we won't refer to them.
11595 stub_results
= new Typed_identifier_list();
11596 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
11597 pr
!= fnresults
->end();
11599 stub_results
->push_back(Typed_identifier("", pr
->type(),
11603 Function_type
* stub_type
= Type::make_function_type(receiver
,
11606 fntype
->location());
11607 if (fntype
->is_varargs())
11608 stub_type
->set_is_varargs();
11610 // We only create the function in the package which creates the
11612 const Package
* package
;
11613 if (type
->named_type() == NULL
)
11616 package
= type
->named_type()->named_object()->package();
11617 std::string stub_name
= name
+ "$stub";
11618 Named_object
* stub
;
11619 if (package
!= NULL
)
11620 stub
= Named_object::make_function_declaration(stub_name
, package
,
11621 stub_type
, location
);
11624 stub
= gogo
->start_function(stub_name
, stub_type
, false,
11625 fntype
->location());
11626 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
11627 fntype
->is_varargs(), location
);
11628 gogo
->finish_function(fntype
->location());
11630 if (type
->named_type() == NULL
&& stub
->is_function())
11631 stub
->func_value()->set_is_unnamed_type_stub_method();
11632 if (m
->nointerface() && stub
->is_function())
11633 stub
->func_value()->set_nointerface();
11636 m
->set_stub_object(stub
);
11640 // Build a stub method which adjusts the receiver as required to call
11641 // METHOD. RECEIVER_NAME is the name we used for the receiver.
11642 // PARAMS is the list of function parameters.
11645 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
11646 const char* receiver_name
,
11647 const Typed_identifier_list
* params
,
11651 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
11652 go_assert(receiver_object
!= NULL
);
11654 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
11655 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
11656 if (expr
->type()->points_to() == NULL
)
11657 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11659 Expression_list
* arguments
;
11660 if (params
== NULL
|| params
->empty())
11664 arguments
= new Expression_list();
11665 for (Typed_identifier_list::const_iterator p
= params
->begin();
11666 p
!= params
->end();
11669 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
11670 go_assert(param
!= NULL
);
11671 Expression
* param_ref
= Expression::make_var_reference(param
,
11673 arguments
->push_back(param_ref
);
11677 Expression
* func
= method
->bind_method(expr
, location
);
11678 go_assert(func
!= NULL
);
11679 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
11682 gogo
->add_statement(Statement::make_return_from_call(call
, location
));
11685 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
11686 // in reverse order.
11689 Type::apply_field_indexes(Expression
* expr
,
11690 const Method::Field_indexes
* field_indexes
,
11693 if (field_indexes
== NULL
)
11695 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
11696 Struct_type
* stype
= expr
->type()->deref()->struct_type();
11697 go_assert(stype
!= NULL
11698 && field_indexes
->field_index
< stype
->field_count());
11699 if (expr
->type()->struct_type() == NULL
)
11701 go_assert(expr
->type()->points_to() != NULL
);
11702 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
11703 go_assert(expr
->type()->struct_type() == stype
);
11705 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
11709 // Return whether NO is a method for which the receiver is a pointer.
11712 Type::method_expects_pointer(const Named_object
* no
)
11714 const Function_type
*fntype
;
11715 if (no
->is_function())
11716 fntype
= no
->func_value()->type();
11717 else if (no
->is_function_declaration())
11718 fntype
= no
->func_declaration_value()->type();
11721 return fntype
->receiver()->type()->points_to() != NULL
;
11724 // Given a set of methods for a type, METHODS, return the method NAME,
11725 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
11726 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
11727 // but is ambiguous (and return NULL).
11730 Type::method_function(const Methods
* methods
, const std::string
& name
,
11731 bool* is_ambiguous
)
11733 if (is_ambiguous
!= NULL
)
11734 *is_ambiguous
= false;
11735 if (methods
== NULL
)
11737 Methods::const_iterator p
= methods
->find(name
);
11738 if (p
== methods
->end())
11740 Method
* m
= p
->second
;
11741 if (m
->is_ambiguous())
11743 if (is_ambiguous
!= NULL
)
11744 *is_ambiguous
= true;
11750 // Return a pointer to the interface method table for TYPE for the
11751 // interface INTERFACE.
11754 Type::interface_method_table(Type
* type
,
11755 Interface_type
*interface
,
11757 Interface_method_tables
** method_tables
,
11758 Interface_method_tables
** pointer_tables
)
11760 go_assert(!interface
->is_empty());
11762 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
11765 *pimt
= new Interface_method_tables(5);
11767 std::pair
<Interface_type
*, Expression
*> val(interface
, NULL
);
11768 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
11770 Location loc
= Linemap::predeclared_location();
11773 // This is a new entry in the hash table.
11774 go_assert(ins
.first
->second
== NULL
);
11775 ins
.first
->second
=
11776 Expression::make_interface_mtable_ref(interface
, type
, is_pointer
, loc
);
11778 return Expression::make_unary(OPERATOR_AND
, ins
.first
->second
, loc
);
11781 // Look for field or method NAME for TYPE. Return an Expression for
11782 // the field or method bound to EXPR. If there is no such field or
11783 // method, give an appropriate error and return an error expression.
11786 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
11787 const std::string
& name
,
11790 if (type
->deref()->is_error_type())
11791 return Expression::make_error(location
);
11793 const Named_type
* nt
= type
->deref()->named_type();
11794 const Struct_type
* st
= type
->deref()->struct_type();
11795 const Interface_type
* it
= type
->interface_type();
11797 // If this is a pointer to a pointer, then it is possible that the
11798 // pointed-to type has methods.
11799 bool dereferenced
= false;
11803 && type
->points_to() != NULL
11804 && type
->points_to()->points_to() != NULL
)
11806 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
11807 type
= type
->points_to();
11808 if (type
->deref()->is_error_type())
11809 return Expression::make_error(location
);
11810 nt
= type
->points_to()->named_type();
11811 st
= type
->points_to()->struct_type();
11812 dereferenced
= true;
11815 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
11816 || expr
->is_addressable());
11817 std::vector
<const Named_type
*> seen
;
11818 bool is_method
= false;
11819 bool found_pointer_method
= false;
11820 std::string ambig1
;
11821 std::string ambig2
;
11822 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
11823 &seen
, NULL
, &is_method
,
11824 &found_pointer_method
, &ambig1
, &ambig2
))
11829 go_assert(st
!= NULL
);
11830 if (type
->struct_type() == NULL
)
11832 go_assert(type
->points_to() != NULL
);
11833 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
11835 go_assert(expr
->type()->struct_type() == st
);
11837 ret
= st
->field_reference(expr
, name
, location
);
11839 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11840 ret
= Expression::make_interface_field_reference(expr
, name
,
11846 m
= nt
->method_function(name
, NULL
);
11847 else if (st
!= NULL
)
11848 m
= st
->method_function(name
, NULL
);
11851 go_assert(m
!= NULL
);
11854 go_error_at(location
,
11855 "calling method %qs requires explicit dereference",
11856 Gogo::message_name(name
).c_str());
11857 return Expression::make_error(location
);
11859 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
11860 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11861 ret
= m
->bind_method(expr
, location
);
11863 go_assert(ret
!= NULL
);
11868 if (Gogo::is_erroneous_name(name
))
11870 // An error was already reported.
11872 else if (!ambig1
.empty())
11873 go_error_at(location
, "%qs is ambiguous via %qs and %qs",
11874 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
11876 else if (found_pointer_method
)
11877 go_error_at(location
, "method requires a pointer receiver");
11878 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
11879 go_error_at(location
,
11880 ("reference to field %qs in object which "
11881 "has no fields or methods"),
11882 Gogo::message_name(name
).c_str());
11885 bool is_unexported
;
11886 // The test for 'a' and 'z' is to handle builtin names,
11887 // which are not hidden.
11888 if (!Gogo::is_hidden_name(name
) && (name
[0] < 'a' || name
[0] > 'z'))
11889 is_unexported
= false;
11892 std::string unpacked
= Gogo::unpack_hidden_name(name
);
11894 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
11899 go_error_at(location
, "reference to unexported field or method %qs",
11900 Gogo::message_name(name
).c_str());
11902 go_error_at(location
, "reference to undefined field or method %qs",
11903 Gogo::message_name(name
).c_str());
11905 return Expression::make_error(location
);
11909 // Look in TYPE for a field or method named NAME, return true if one
11910 // is found. This looks through embedded anonymous fields and handles
11911 // ambiguity. If a method is found, sets *IS_METHOD to true;
11912 // otherwise, if a field is found, set it to false. If
11913 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
11914 // whose address can not be taken. SEEN is used to avoid infinite
11915 // recursion on invalid types.
11917 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
11918 // method we couldn't use because it requires a pointer. LEVEL is
11919 // used for recursive calls, and can be NULL for a non-recursive call.
11920 // When this function returns false because it finds that the name is
11921 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
11922 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
11923 // will be unchanged.
11925 // This function just returns whether or not there is a field or
11926 // method, and whether it is a field or method. It doesn't build an
11927 // expression to refer to it. If it is a method, we then look in the
11928 // list of all methods for the type. If it is a field, the search has
11929 // to be done again, looking only for fields, and building up the
11930 // expression as we go.
11933 Type::find_field_or_method(const Type
* type
,
11934 const std::string
& name
,
11935 bool receiver_can_be_pointer
,
11936 std::vector
<const Named_type
*>* seen
,
11939 bool* found_pointer_method
,
11940 std::string
* ambig1
,
11941 std::string
* ambig2
)
11943 // Named types can have locally defined methods.
11944 const Named_type
* nt
= type
->named_type();
11945 if (nt
== NULL
&& type
->points_to() != NULL
)
11946 nt
= type
->points_to()->named_type();
11949 Named_object
* no
= nt
->find_local_method(name
);
11952 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
11958 // Record that we have found a pointer method in order to
11959 // give a better error message if we don't find anything
11961 *found_pointer_method
= true;
11964 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11970 // We've already seen this type when searching for methods.
11976 // Interface types can have methods.
11977 const Interface_type
* it
= type
->interface_type();
11978 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11984 // Struct types can have fields. They can also inherit fields and
11985 // methods from anonymous fields.
11986 const Struct_type
* st
= type
->deref()->struct_type();
11989 const Struct_field_list
* fields
= st
->fields();
11990 if (fields
== NULL
)
11994 seen
->push_back(nt
);
11996 int found_level
= 0;
11997 bool found_is_method
= false;
11998 std::string found_ambig1
;
11999 std::string found_ambig2
;
12000 const Struct_field
* found_parent
= NULL
;
12001 for (Struct_field_list::const_iterator pf
= fields
->begin();
12002 pf
!= fields
->end();
12005 if (pf
->is_field_name(name
))
12007 *is_method
= false;
12013 if (!pf
->is_anonymous())
12016 if (pf
->type()->deref()->is_error_type()
12017 || pf
->type()->deref()->is_undefined())
12020 Named_type
* fnt
= pf
->type()->named_type();
12022 fnt
= pf
->type()->deref()->named_type();
12023 go_assert(fnt
!= NULL
);
12025 // Methods with pointer receivers on embedded field are
12026 // inherited by the pointer to struct, and also by the struct
12027 // type if the field itself is a pointer.
12028 bool can_be_pointer
= (receiver_can_be_pointer
12029 || pf
->type()->points_to() != NULL
);
12030 int sublevel
= level
== NULL
? 1 : *level
+ 1;
12031 bool sub_is_method
;
12032 std::string subambig1
;
12033 std::string subambig2
;
12034 bool subfound
= Type::find_field_or_method(fnt
,
12040 found_pointer_method
,
12045 if (!subambig1
.empty())
12047 // The name was found via this field, but is ambiguous.
12048 // if the ambiguity is lower or at the same level as
12049 // anything else we have already found, then we want to
12050 // pass the ambiguity back to the caller.
12051 if (found_level
== 0 || sublevel
<= found_level
)
12053 found_ambig1
= (Gogo::message_name(pf
->field_name())
12054 + '.' + subambig1
);
12055 found_ambig2
= (Gogo::message_name(pf
->field_name())
12056 + '.' + subambig2
);
12057 found_level
= sublevel
;
12063 // The name was found via this field. Use the level to see
12064 // if we want to use this one, or whether it introduces an
12066 if (found_level
== 0 || sublevel
< found_level
)
12068 found_level
= sublevel
;
12069 found_is_method
= sub_is_method
;
12070 found_ambig1
.clear();
12071 found_ambig2
.clear();
12072 found_parent
= &*pf
;
12074 else if (sublevel
> found_level
)
12076 else if (found_ambig1
.empty())
12078 // We found an ambiguity.
12079 go_assert(found_parent
!= NULL
);
12080 found_ambig1
= Gogo::message_name(found_parent
->field_name());
12081 found_ambig2
= Gogo::message_name(pf
->field_name());
12085 // We found an ambiguity, but we already know of one.
12086 // Just report the earlier one.
12091 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
12092 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
12093 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
12094 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
12099 if (found_level
== 0)
12101 else if (found_is_method
12102 && type
->named_type() != NULL
12103 && type
->points_to() != NULL
)
12105 // If this is a method inherited from a struct field in a named pointer
12106 // type, it is invalid to automatically dereference the pointer to the
12107 // struct to find this method.
12109 *level
= found_level
;
12113 else if (!found_ambig1
.empty())
12115 go_assert(!found_ambig1
.empty());
12116 ambig1
->assign(found_ambig1
);
12117 ambig2
->assign(found_ambig2
);
12119 *level
= found_level
;
12125 *level
= found_level
;
12126 *is_method
= found_is_method
;
12131 // Return whether NAME is an unexported field or method for TYPE.
12134 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
12135 const std::string
& name
,
12136 std::vector
<const Named_type
*>* seen
)
12138 const Named_type
* nt
= type
->named_type();
12140 nt
= type
->deref()->named_type();
12143 if (nt
->is_unexported_local_method(gogo
, name
))
12146 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
12152 // We've already seen this type.
12158 const Interface_type
* it
= type
->interface_type();
12159 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
12162 type
= type
->deref();
12164 const Struct_type
* st
= type
->struct_type();
12165 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
12171 const Struct_field_list
* fields
= st
->fields();
12172 if (fields
== NULL
)
12176 seen
->push_back(nt
);
12178 for (Struct_field_list::const_iterator pf
= fields
->begin();
12179 pf
!= fields
->end();
12182 if (pf
->is_anonymous()
12183 && !pf
->type()->deref()->is_error_type()
12184 && !pf
->type()->deref()->is_undefined())
12186 Named_type
* subtype
= pf
->type()->named_type();
12187 if (subtype
== NULL
)
12188 subtype
= pf
->type()->deref()->named_type();
12189 if (subtype
== NULL
)
12191 // This is an error, but it will be diagnosed elsewhere.
12194 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
12209 // Class Forward_declaration.
12211 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
12212 : Type(TYPE_FORWARD
),
12213 named_object_(named_object
->resolve()), warned_(false)
12215 go_assert(this->named_object_
->is_unknown()
12216 || this->named_object_
->is_type_declaration());
12219 // Return the named object.
12222 Forward_declaration_type::named_object()
12224 return this->named_object_
->resolve();
12227 const Named_object
*
12228 Forward_declaration_type::named_object() const
12230 return this->named_object_
->resolve();
12233 // Return the name of the forward declared type.
12236 Forward_declaration_type::name() const
12238 return this->named_object()->name();
12241 // Warn about a use of a type which has been declared but not defined.
12244 Forward_declaration_type::warn() const
12246 Named_object
* no
= this->named_object_
->resolve();
12247 if (no
->is_unknown())
12249 // The name was not defined anywhere.
12250 if (!this->warned_
)
12252 go_error_at(this->named_object_
->location(),
12253 "use of undefined type %qs",
12254 no
->message_name().c_str());
12255 this->warned_
= true;
12258 else if (no
->is_type_declaration())
12260 // The name was seen as a type, but the type was never defined.
12261 if (no
->type_declaration_value()->using_type())
12263 go_error_at(this->named_object_
->location(),
12264 "use of undefined type %qs",
12265 no
->message_name().c_str());
12266 this->warned_
= true;
12271 // The name was defined, but not as a type.
12272 if (!this->warned_
)
12274 go_error_at(this->named_object_
->location(), "expected type");
12275 this->warned_
= true;
12280 // Get the base type of a declaration. This gives an error if the
12281 // type has not yet been defined.
12284 Forward_declaration_type::real_type()
12286 if (this->is_defined())
12288 Named_type
* nt
= this->named_object()->type_value();
12289 if (!nt
->is_valid())
12290 return Type::make_error_type();
12291 return this->named_object()->type_value();
12296 return Type::make_error_type();
12301 Forward_declaration_type::real_type() const
12303 if (this->is_defined())
12305 const Named_type
* nt
= this->named_object()->type_value();
12306 if (!nt
->is_valid())
12307 return Type::make_error_type();
12308 return this->named_object()->type_value();
12313 return Type::make_error_type();
12317 // Return whether the base type is defined.
12320 Forward_declaration_type::is_defined() const
12322 return this->named_object()->is_type();
12325 // Add a method. This is used when methods are defined before the
12329 Forward_declaration_type::add_method(const std::string
& name
,
12330 Function
* function
)
12332 Named_object
* no
= this->named_object();
12333 if (no
->is_unknown())
12334 no
->declare_as_type();
12335 return no
->type_declaration_value()->add_method(name
, function
);
12338 // Add a method declaration. This is used when methods are declared
12339 // before the type.
12342 Forward_declaration_type::add_method_declaration(const std::string
& name
,
12344 Function_type
* type
,
12347 Named_object
* no
= this->named_object();
12348 if (no
->is_unknown())
12349 no
->declare_as_type();
12350 Type_declaration
* td
= no
->type_declaration_value();
12351 return td
->add_method_declaration(name
, package
, type
, location
);
12354 // Add an already created object as a method.
12357 Forward_declaration_type::add_existing_method(Named_object
* nom
)
12359 Named_object
* no
= this->named_object();
12360 if (no
->is_unknown())
12361 no
->declare_as_type();
12362 no
->type_declaration_value()->add_existing_method(nom
);
12368 Forward_declaration_type::do_traverse(Traverse
* traverse
)
12370 if (this->is_defined()
12371 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
12372 return TRAVERSE_EXIT
;
12373 return TRAVERSE_CONTINUE
;
12376 // Verify the type.
12379 Forward_declaration_type::do_verify()
12381 if (!this->is_defined() && !this->is_nil_constant_as_type())
12389 // Get the backend representation for the type.
12392 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
12394 if (this->is_defined())
12395 return Type::get_named_base_btype(gogo
, this->real_type());
12398 return gogo
->backend()->error_type();
12400 // We represent an undefined type as a struct with no fields. That
12401 // should work fine for the backend, since the same case can arise
12403 std::vector
<Backend::Btyped_identifier
> fields
;
12404 Btype
* bt
= gogo
->backend()->struct_type(fields
);
12405 return gogo
->backend()->named_type(this->name(), bt
,
12406 this->named_object()->location());
12409 // Build a type descriptor for a forwarded type.
12412 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
12414 Location ploc
= Linemap::predeclared_location();
12415 if (!this->is_defined())
12416 return Expression::make_error(ploc
);
12419 Type
* t
= this->real_type();
12421 return this->named_type_descriptor(gogo
, t
, name
);
12423 return Expression::make_error(this->named_object_
->location());
12427 // The reflection string.
12430 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
12432 this->append_reflection(this->real_type(), gogo
, ret
);
12435 // The mangled name.
12438 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
12440 if (this->is_defined())
12441 this->append_mangled_name(this->real_type(), gogo
, ret
);
12444 const Named_object
* no
= this->named_object();
12446 if (no
->package() == NULL
)
12447 name
= gogo
->pkgpath_symbol();
12449 name
= no
->package()->pkgpath_symbol();
12451 name
+= Gogo::unpack_hidden_name(no
->name());
12453 snprintf(buf
, sizeof buf
, "N%u_",
12454 static_cast<unsigned int>(name
.length()));
12460 // Export a forward declaration. This can happen when a defined type
12461 // refers to a type which is only declared (and is presumably defined
12462 // in some other file in the same package).
12465 Forward_declaration_type::do_export(Export
*) const
12467 // If there is a base type, that should be exported instead of this.
12468 go_assert(!this->is_defined());
12470 // We don't output anything.
12473 // Make a forward declaration.
12476 Type::make_forward_declaration(Named_object
* named_object
)
12478 return new Forward_declaration_type(named_object
);
12481 // Class Typed_identifier_list.
12483 // Sort the entries by name.
12485 struct Typed_identifier_list_sort
12489 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
12491 return (Gogo::unpack_hidden_name(t1
.name())
12492 < Gogo::unpack_hidden_name(t2
.name()));
12497 Typed_identifier_list::sort_by_name()
12499 std::sort(this->entries_
.begin(), this->entries_
.end(),
12500 Typed_identifier_list_sort());
12506 Typed_identifier_list::traverse(Traverse
* traverse
)
12508 for (Typed_identifier_list::const_iterator p
= this->begin();
12512 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
12513 return TRAVERSE_EXIT
;
12515 return TRAVERSE_CONTINUE
;
12520 Typed_identifier_list
*
12521 Typed_identifier_list::copy() const
12523 Typed_identifier_list
* ret
= new Typed_identifier_list();
12524 for (Typed_identifier_list::const_iterator p
= this->begin();
12527 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));