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 from
752 if (lhs
->points_to() != NULL
753 && rhs
->points_to() != NULL
754 && !lhs
->points_to()->in_heap()
755 && rhs
->points_to()->in_heap()
756 && !rhs
->is_unsafe_pointer_type())
759 reason
->assign(_("conversion from normal type to notinheap 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 std::string name
= gogo
->type_descriptor_name(this, nt
);
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
= gogo
->type_descriptor_name(this, 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 true if this type descriptor is defined in a different
1335 // package. If this returns true it sets *PACKAGE to the package.
1338 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1339 const Package
** package
)
1343 if (nt
->named_object()->package() != NULL
)
1345 // This is a named type defined in a different package. The
1346 // type descriptor should be defined in that package.
1347 *package
= nt
->named_object()->package();
1353 if (this->points_to() != NULL
1354 && this->points_to()->named_type() != NULL
1355 && this->points_to()->named_type()->named_object()->package() != NULL
)
1357 // This is an unnamed pointer to a named type defined in a
1358 // different package. The descriptor should be defined in
1360 *package
= this->points_to()->named_type()->named_object()->package();
1367 // Return a composite literal for a type descriptor.
1370 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1372 return type
->do_type_descriptor(gogo
, NULL
);
1375 // Return a composite literal for a type descriptor with a name.
1378 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1380 go_assert(name
!= NULL
&& type
->named_type() != name
);
1381 return type
->do_type_descriptor(gogo
, name
);
1384 // Make a builtin struct type from a list of fields. The fields are
1385 // pairs of a name and a type.
1388 Type::make_builtin_struct_type(int nfields
, ...)
1391 va_start(ap
, nfields
);
1393 Location bloc
= Linemap::predeclared_location();
1394 Struct_field_list
* sfl
= new Struct_field_list();
1395 for (int i
= 0; i
< nfields
; i
++)
1397 const char* field_name
= va_arg(ap
, const char *);
1398 Type
* type
= va_arg(ap
, Type
*);
1399 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1404 Struct_type
* ret
= Type::make_struct_type(sfl
, bloc
);
1405 ret
->set_is_struct_incomparable();
1409 // A list of builtin named types.
1411 std::vector
<Named_type
*> Type::named_builtin_types
;
1413 // Make a builtin named type.
1416 Type::make_builtin_named_type(const char* name
, Type
* type
)
1418 Location bloc
= Linemap::predeclared_location();
1419 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1420 Named_type
* ret
= no
->type_value();
1421 Type::named_builtin_types
.push_back(ret
);
1425 // Convert the named builtin types.
1428 Type::convert_builtin_named_types(Gogo
* gogo
)
1430 for (std::vector
<Named_type
*>::const_iterator p
=
1431 Type::named_builtin_types
.begin();
1432 p
!= Type::named_builtin_types
.end();
1435 bool r
= (*p
)->verify();
1437 (*p
)->convert(gogo
);
1441 // Return the type of a type descriptor. We should really tie this to
1442 // runtime.Type rather than copying it. This must match the struct "_type"
1443 // declared in libgo/go/runtime/type.go.
1446 Type::make_type_descriptor_type()
1451 Location bloc
= Linemap::predeclared_location();
1453 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1454 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
1455 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1456 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1457 Type
* string_type
= Type::lookup_string_type();
1458 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1460 // This is an unnamed version of unsafe.Pointer. Perhaps we
1461 // should use the named version instead, although that would
1462 // require us to create the unsafe package if it has not been
1463 // imported. It probably doesn't matter.
1464 Type
* void_type
= Type::make_void_type();
1465 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1467 Typed_identifier_list
*params
= new Typed_identifier_list();
1468 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
1469 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1471 Typed_identifier_list
* results
= new Typed_identifier_list();
1472 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1474 Type
* hash_fntype
= Type::make_function_type(NULL
, params
, results
,
1477 params
= new Typed_identifier_list();
1478 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
1479 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
1481 results
= new Typed_identifier_list();
1482 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1484 Type
* equal_fntype
= Type::make_function_type(NULL
, params
, results
,
1487 // Forward declaration for the type descriptor type.
1488 Named_object
* named_type_descriptor_type
=
1489 Named_object::make_type_declaration("_type", NULL
, bloc
);
1490 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1491 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1493 // The type of a method on a concrete type.
1494 Struct_type
* method_type
=
1495 Type::make_builtin_struct_type(5,
1496 "name", pointer_string_type
,
1497 "pkgPath", pointer_string_type
,
1498 "mtyp", pointer_type_descriptor_type
,
1499 "typ", pointer_type_descriptor_type
,
1500 "tfn", unsafe_pointer_type
);
1501 Named_type
* named_method_type
=
1502 Type::make_builtin_named_type("method", method_type
);
1504 // Information for types with a name or methods.
1505 Type
* slice_named_method_type
=
1506 Type::make_array_type(named_method_type
, NULL
);
1507 Struct_type
* uncommon_type
=
1508 Type::make_builtin_struct_type(3,
1509 "name", pointer_string_type
,
1510 "pkgPath", pointer_string_type
,
1511 "methods", slice_named_method_type
);
1512 Named_type
* named_uncommon_type
=
1513 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1515 Type
* pointer_uncommon_type
=
1516 Type::make_pointer_type(named_uncommon_type
);
1518 // The type descriptor type.
1520 Struct_type
* type_descriptor_type
=
1521 Type::make_builtin_struct_type(12,
1522 "size", uintptr_type
,
1523 "ptrdata", uintptr_type
,
1524 "hash", uint32_type
,
1526 "align", uint8_type
,
1527 "fieldAlign", uint8_type
,
1528 "hashfn", hash_fntype
,
1529 "equalfn", equal_fntype
,
1530 "gcdata", pointer_uint8_type
,
1531 "string", pointer_string_type
,
1532 "", pointer_uncommon_type
,
1534 pointer_type_descriptor_type
);
1536 Named_type
* named
= Type::make_builtin_named_type("_type",
1537 type_descriptor_type
);
1539 named_type_descriptor_type
->set_type_value(named
);
1547 // Make the type of a pointer to a type descriptor as represented in
1551 Type::make_type_descriptor_ptr_type()
1555 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1559 // Return the alignment required by the memequalN function. N is a
1560 // type size: 16, 32, 64, or 128. The memequalN functions are defined
1561 // in libgo/go/runtime/alg.go.
1564 Type::memequal_align(Gogo
* gogo
, int size
)
1579 // The code uses [2]int64, which must have the same alignment as
1587 Type
* t
= Type::lookup_integer_type(tn
);
1590 if (!t
->backend_type_align(gogo
, &ret
))
1595 // Return whether this type needs specially built type functions.
1596 // This returns true for types that are comparable and either can not
1597 // use an identity comparison, or are a non-standard size.
1600 Type::needs_specific_type_functions(Gogo
* gogo
)
1602 Named_type
* nt
= this->named_type();
1603 if (nt
!= NULL
&& nt
->is_alias())
1605 if (!this->is_comparable())
1607 if (!this->compare_is_identity(gogo
))
1610 // We create a few predeclared types for type descriptors; they are
1611 // really just for the backend and don't need hash or equality
1613 if (nt
!= NULL
&& Linemap::is_predeclared_location(nt
->location()))
1616 int64_t size
, align
;
1617 if (!this->backend_type_size(gogo
, &size
)
1618 || !this->backend_type_align(gogo
, &align
))
1620 go_assert(saw_errors());
1623 // This switch matches the one in Type::type_functions.
1629 return align
< Type::memequal_align(gogo
, 16);
1631 return align
< Type::memequal_align(gogo
, 32);
1633 return align
< Type::memequal_align(gogo
, 64);
1635 return align
< Type::memequal_align(gogo
, 128);
1641 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1642 // hash code for this type and which compare whether two values of
1643 // this type are equal. If NAME is not NULL it is the name of this
1644 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1645 // functions, for convenience; they may be NULL.
1648 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1649 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1650 Named_object
** equal_fn
)
1652 // If this loop leaves NAME as NULL, then the type does not have a
1654 while (name
!= NULL
&& name
->is_alias())
1655 name
= name
->real_type()->named_type();
1657 if (!this->is_comparable())
1664 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1666 Location bloc
= Linemap::predeclared_location();
1668 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1669 Type
* void_type
= Type::make_void_type();
1670 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1672 if (hash_fntype
== NULL
)
1674 Typed_identifier_list
* params
= new Typed_identifier_list();
1675 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1677 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1679 Typed_identifier_list
* results
= new Typed_identifier_list();
1680 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1682 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1684 if (equal_fntype
== NULL
)
1686 Typed_identifier_list
* params
= new Typed_identifier_list();
1687 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1689 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1692 Typed_identifier_list
* results
= new Typed_identifier_list();
1693 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1696 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1700 const char* hash_fnname
;
1701 const char* equal_fnname
;
1702 if (this->compare_is_identity(gogo
))
1704 int64_t size
, align
;
1705 if (!this->backend_type_size(gogo
, &size
)
1706 || !this->backend_type_align(gogo
, &align
))
1708 go_assert(saw_errors());
1711 bool build_functions
= false;
1712 // This switch matches the one in Type::needs_specific_type_functions.
1713 // The alignment tests are because of the memequal functions,
1714 // which assume that the values are aligned as required for an
1715 // integer of that size.
1719 hash_fnname
= "runtime.memhash0";
1720 equal_fnname
= "runtime.memequal0";
1723 hash_fnname
= "runtime.memhash8";
1724 equal_fnname
= "runtime.memequal8";
1727 if (align
< Type::memequal_align(gogo
, 16))
1728 build_functions
= true;
1731 hash_fnname
= "runtime.memhash16";
1732 equal_fnname
= "runtime.memequal16";
1736 if (align
< Type::memequal_align(gogo
, 32))
1737 build_functions
= true;
1740 hash_fnname
= "runtime.memhash32";
1741 equal_fnname
= "runtime.memequal32";
1745 if (align
< Type::memequal_align(gogo
, 64))
1746 build_functions
= true;
1749 hash_fnname
= "runtime.memhash64";
1750 equal_fnname
= "runtime.memequal64";
1754 if (align
< Type::memequal_align(gogo
, 128))
1755 build_functions
= true;
1758 hash_fnname
= "runtime.memhash128";
1759 equal_fnname
= "runtime.memequal128";
1763 build_functions
= true;
1766 if (build_functions
)
1768 // We don't have a built-in function for a type of this size
1769 // and alignment. Build a function to use that calls the
1770 // generic hash/equality functions for identity, passing the size.
1771 this->specific_type_functions(gogo
, name
, size
, hash_fntype
,
1772 equal_fntype
, hash_fn
, equal_fn
);
1778 switch (this->base()->classification())
1780 case Type::TYPE_ERROR
:
1781 case Type::TYPE_VOID
:
1782 case Type::TYPE_NIL
:
1783 case Type::TYPE_FUNCTION
:
1784 case Type::TYPE_MAP
:
1785 // For these types is_comparable should have returned false.
1788 case Type::TYPE_BOOLEAN
:
1789 case Type::TYPE_INTEGER
:
1790 case Type::TYPE_POINTER
:
1791 case Type::TYPE_CHANNEL
:
1792 // For these types compare_is_identity should have returned true.
1795 case Type::TYPE_FLOAT
:
1796 switch (this->float_type()->bits())
1799 hash_fnname
= "runtime.f32hash";
1800 equal_fnname
= "runtime.f32equal";
1803 hash_fnname
= "runtime.f64hash";
1804 equal_fnname
= "runtime.f64equal";
1811 case Type::TYPE_COMPLEX
:
1812 switch (this->complex_type()->bits())
1815 hash_fnname
= "runtime.c64hash";
1816 equal_fnname
= "runtime.c64equal";
1819 hash_fnname
= "runtime.c128hash";
1820 equal_fnname
= "runtime.c128equal";
1827 case Type::TYPE_STRING
:
1828 hash_fnname
= "runtime.strhash";
1829 equal_fnname
= "runtime.strequal";
1832 case Type::TYPE_STRUCT
:
1834 // This is a struct which can not be compared using a
1835 // simple identity function. We need to build a function
1837 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1838 equal_fntype
, hash_fn
, equal_fn
);
1842 case Type::TYPE_ARRAY
:
1843 if (this->is_slice_type())
1845 // Type::is_compatible_for_comparison should have
1851 // This is an array which can not be compared using a
1852 // simple identity function. We need to build a
1853 // function for comparison.
1854 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1855 equal_fntype
, hash_fn
, equal_fn
);
1860 case Type::TYPE_INTERFACE
:
1861 if (this->interface_type()->is_empty())
1863 hash_fnname
= "runtime.nilinterhash";
1864 equal_fnname
= "runtime.nilinterequal";
1868 hash_fnname
= "runtime.interhash";
1869 equal_fnname
= "runtime.interequal";
1873 case Type::TYPE_NAMED
:
1874 case Type::TYPE_FORWARD
:
1883 Location bloc
= Linemap::predeclared_location();
1884 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1886 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1887 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1888 equal_fntype
, bloc
);
1889 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1892 // A hash table mapping types to the specific hash functions.
1894 Type::Type_functions
Type::type_functions_table
;
1896 // Handle a type function which is specific to a type: if SIZE == -1,
1897 // this is a struct or array that can not use an identity comparison.
1898 // Otherwise, it is a type that uses an identity comparison but is not
1899 // one of the standard supported sizes.
1902 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1903 Function_type
* hash_fntype
,
1904 Function_type
* equal_fntype
,
1905 Named_object
** hash_fn
,
1906 Named_object
** equal_fn
)
1908 Hash_equal_fn
fnull(NULL
, NULL
);
1909 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1910 std::pair
<Type_functions::iterator
, bool> ins
=
1911 Type::type_functions_table
.insert(val
);
1914 // We already have functions for this type
1915 *hash_fn
= ins
.first
->second
.first
;
1916 *equal_fn
= ins
.first
->second
.second
;
1920 std::string hash_name
;
1921 std::string equal_name
;
1922 gogo
->specific_type_function_names(this, name
, &hash_name
, &equal_name
);
1924 Location bloc
= Linemap::predeclared_location();
1926 const Package
* package
= NULL
;
1927 bool is_defined_elsewhere
=
1928 this->type_descriptor_defined_elsewhere(name
, &package
);
1929 if (is_defined_elsewhere
)
1931 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
1933 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
1934 equal_fntype
, bloc
);
1938 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
1939 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
1943 ins
.first
->second
.first
= *hash_fn
;
1944 ins
.first
->second
.second
= *equal_fn
;
1946 if (!is_defined_elsewhere
)
1948 if (gogo
->in_global_scope())
1949 this->write_specific_type_functions(gogo
, name
, size
, hash_name
,
1950 hash_fntype
, equal_name
,
1953 gogo
->queue_specific_type_function(this, name
, size
, hash_name
,
1954 hash_fntype
, equal_name
,
1959 // Write the hash and equality functions for a type which needs to be
1960 // written specially.
1963 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1964 const std::string
& hash_name
,
1965 Function_type
* hash_fntype
,
1966 const std::string
& equal_name
,
1967 Function_type
* equal_fntype
)
1969 Location bloc
= Linemap::predeclared_location();
1971 if (gogo
->specific_type_functions_are_written())
1973 go_assert(saw_errors());
1977 go_assert(this->is_comparable());
1979 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
1981 hash_fn
->func_value()->set_is_type_specific_function();
1982 gogo
->start_block(bloc
);
1985 this->write_identity_hash(gogo
, size
);
1986 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
1987 this->write_named_hash(gogo
, name
, hash_fntype
, equal_fntype
);
1988 else if (this->struct_type() != NULL
)
1989 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
1991 else if (this->array_type() != NULL
)
1992 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
1997 Block
* b
= gogo
->finish_block(bloc
);
1998 gogo
->add_block(b
, bloc
);
1999 gogo
->lower_block(hash_fn
, b
);
2000 gogo
->finish_function(bloc
);
2002 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
2004 equal_fn
->func_value()->set_is_type_specific_function();
2005 gogo
->start_block(bloc
);
2008 this->write_identity_equal(gogo
, size
);
2009 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2010 this->write_named_equal(gogo
, name
);
2011 else if (this->struct_type() != NULL
)
2012 this->struct_type()->write_equal_function(gogo
, name
);
2013 else if (this->array_type() != NULL
)
2014 this->array_type()->write_equal_function(gogo
, name
);
2018 b
= gogo
->finish_block(bloc
);
2019 gogo
->add_block(b
, bloc
);
2020 gogo
->lower_block(equal_fn
, b
);
2021 gogo
->finish_function(bloc
);
2023 // Build the function descriptors for the type descriptor to refer to.
2024 hash_fn
->func_value()->descriptor(gogo
, hash_fn
);
2025 equal_fn
->func_value()->descriptor(gogo
, equal_fn
);
2028 // Write a hash function for a type that can use an identity hash but
2029 // is not one of the standard supported sizes. For example, this
2030 // would be used for the type [3]byte. This builds a return statement
2031 // that returns a call to the memhash function, passing the key and
2032 // seed from the function arguments (already constructed before this
2033 // is called), and the constant size.
2036 Type::write_identity_hash(Gogo
* gogo
, int64_t size
)
2038 Location bloc
= Linemap::predeclared_location();
2040 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2041 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2043 Typed_identifier_list
* params
= new Typed_identifier_list();
2044 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
2045 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
2046 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2048 Typed_identifier_list
* results
= new Typed_identifier_list();
2049 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
2051 Function_type
* memhash_fntype
= Type::make_function_type(NULL
, params
,
2054 Named_object
* memhash
=
2055 Named_object::make_function_declaration("runtime.memhash", NULL
,
2056 memhash_fntype
, bloc
);
2057 memhash
->func_declaration_value()->set_asm_name("runtime.memhash");
2059 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2060 go_assert(key_arg
!= NULL
);
2061 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2062 go_assert(seed_arg
!= NULL
);
2064 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2065 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2066 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2068 Expression_list
* args
= new Expression_list();
2069 args
->push_back(key_ref
);
2070 args
->push_back(seed_ref
);
2071 args
->push_back(size_arg
);
2072 Expression
* func
= Expression::make_func_reference(memhash
, NULL
, bloc
);
2073 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2075 Expression_list
* vals
= new Expression_list();
2076 vals
->push_back(call
);
2077 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2078 gogo
->add_statement(s
);
2081 // Write an equality function for a type that can use an identity
2082 // equality comparison but is not one of the standard supported sizes.
2083 // For example, this would be used for the type [3]byte. This builds
2084 // a return statement that returns a call to the memequal function,
2085 // passing the two keys from the function arguments (already
2086 // constructed before this is called), and the constant size.
2089 Type::write_identity_equal(Gogo
* gogo
, int64_t size
)
2091 Location bloc
= Linemap::predeclared_location();
2093 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2094 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2096 Typed_identifier_list
* params
= new Typed_identifier_list();
2097 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
2098 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
2099 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2101 Typed_identifier_list
* results
= new Typed_identifier_list();
2102 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
2104 Function_type
* memequal_fntype
= Type::make_function_type(NULL
, params
,
2107 Named_object
* memequal
=
2108 Named_object::make_function_declaration("runtime.memequal", NULL
,
2109 memequal_fntype
, bloc
);
2110 memequal
->func_declaration_value()->set_asm_name("runtime.memequal");
2112 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2113 go_assert(key1_arg
!= NULL
);
2114 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2115 go_assert(key2_arg
!= NULL
);
2117 Expression
* key1_ref
= Expression::make_var_reference(key1_arg
, bloc
);
2118 Expression
* key2_ref
= Expression::make_var_reference(key2_arg
, bloc
);
2119 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2121 Expression_list
* args
= new Expression_list();
2122 args
->push_back(key1_ref
);
2123 args
->push_back(key2_ref
);
2124 args
->push_back(size_arg
);
2125 Expression
* func
= Expression::make_func_reference(memequal
, NULL
, bloc
);
2126 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2128 Expression_list
* vals
= new Expression_list();
2129 vals
->push_back(call
);
2130 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2131 gogo
->add_statement(s
);
2134 // Write a hash function that simply calls the hash function for a
2135 // named type. This is used when one named type is defined as
2136 // another. This ensures that this case works when the other named
2137 // type is defined in another package and relies on calling hash
2138 // functions defined only in that package.
2141 Type::write_named_hash(Gogo
* gogo
, Named_type
* name
,
2142 Function_type
* hash_fntype
, Function_type
* equal_fntype
)
2144 Location bloc
= Linemap::predeclared_location();
2146 Named_type
* base_type
= name
->real_type()->named_type();
2147 while (base_type
->is_alias())
2149 base_type
= base_type
->real_type()->named_type();
2150 go_assert(base_type
!= NULL
);
2152 go_assert(base_type
!= NULL
);
2154 // The pointer to the type we are going to hash. This is an
2156 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2157 go_assert(key_arg
!= NULL
);
2159 // The seed argument to the hash function.
2160 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2161 go_assert(seed_arg
!= NULL
);
2163 Named_object
* hash_fn
;
2164 Named_object
* equal_fn
;
2165 name
->real_type()->type_functions(gogo
, base_type
, hash_fntype
, equal_fntype
,
2166 &hash_fn
, &equal_fn
);
2168 // Call the hash function for the base type.
2169 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2170 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2171 Expression_list
* args
= new Expression_list();
2172 args
->push_back(key_ref
);
2173 args
->push_back(seed_ref
);
2174 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
2175 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2177 // Return the hash of the base type.
2178 Expression_list
* vals
= new Expression_list();
2179 vals
->push_back(call
);
2180 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2181 gogo
->add_statement(s
);
2184 // Write an equality function that simply calls the equality function
2185 // for a named type. This is used when one named type is defined as
2186 // another. This ensures that this case works when the other named
2187 // type is defined in another package and relies on calling equality
2188 // functions defined only in that package.
2191 Type::write_named_equal(Gogo
* gogo
, Named_type
* name
)
2193 Location bloc
= Linemap::predeclared_location();
2195 // The pointers to the types we are going to compare. These have
2196 // type unsafe.Pointer.
2197 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2198 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2199 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
2201 Named_type
* base_type
= name
->real_type()->named_type();
2202 go_assert(base_type
!= NULL
);
2204 // Build temporaries with the base type.
2205 Type
* pt
= Type::make_pointer_type(base_type
);
2207 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
2208 ref
= Expression::make_cast(pt
, ref
, bloc
);
2209 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
2210 gogo
->add_statement(p1
);
2212 ref
= Expression::make_var_reference(key2_arg
, bloc
);
2213 ref
= Expression::make_cast(pt
, ref
, bloc
);
2214 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
2215 gogo
->add_statement(p2
);
2217 // Compare the values for equality.
2218 Expression
* t1
= Expression::make_temporary_reference(p1
, bloc
);
2219 t1
= Expression::make_unary(OPERATOR_MULT
, t1
, bloc
);
2221 Expression
* t2
= Expression::make_temporary_reference(p2
, bloc
);
2222 t2
= Expression::make_unary(OPERATOR_MULT
, t2
, bloc
);
2224 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, t1
, t2
, bloc
);
2226 // Return the equality comparison.
2227 Expression_list
* vals
= new Expression_list();
2228 vals
->push_back(cond
);
2229 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2230 gogo
->add_statement(s
);
2233 // Return a composite literal for the type descriptor for a plain type
2234 // of kind RUNTIME_TYPE_KIND named NAME.
2237 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
2238 Named_type
* name
, const Methods
* methods
,
2239 bool only_value_methods
)
2241 Location bloc
= Linemap::predeclared_location();
2243 Type
* td_type
= Type::make_type_descriptor_type();
2244 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
2246 Expression_list
* vals
= new Expression_list();
2249 if (!this->has_pointer())
2250 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
2251 if (this->points_to() != NULL
)
2252 runtime_type_kind
|= RUNTIME_TYPE_KIND_DIRECT_IFACE
;
2255 if (this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2256 runtime_type_kind
|= RUNTIME_TYPE_KIND_GC_PROG
;
2258 Struct_field_list::const_iterator p
= fields
->begin();
2259 go_assert(p
->is_field_name("size"));
2260 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
2261 vals
->push_back(Expression::make_type_info(this, type_info
));
2264 go_assert(p
->is_field_name("ptrdata"));
2265 type_info
= Expression::TYPE_INFO_DESCRIPTOR_PTRDATA
;
2266 vals
->push_back(Expression::make_type_info(this, type_info
));
2269 go_assert(p
->is_field_name("hash"));
2272 h
= name
->hash_for_method(gogo
);
2274 h
= this->hash_for_method(gogo
);
2275 vals
->push_back(Expression::make_integer_ul(h
, p
->type(), bloc
));
2278 go_assert(p
->is_field_name("kind"));
2279 vals
->push_back(Expression::make_integer_ul(runtime_type_kind
, p
->type(),
2283 go_assert(p
->is_field_name("align"));
2284 type_info
= Expression::TYPE_INFO_ALIGNMENT
;
2285 vals
->push_back(Expression::make_type_info(this, type_info
));
2288 go_assert(p
->is_field_name("fieldAlign"));
2289 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
2290 vals
->push_back(Expression::make_type_info(this, type_info
));
2293 go_assert(p
->is_field_name("hashfn"));
2294 Function_type
* hash_fntype
= p
->type()->function_type();
2297 go_assert(p
->is_field_name("equalfn"));
2298 Function_type
* equal_fntype
= p
->type()->function_type();
2300 Named_object
* hash_fn
;
2301 Named_object
* equal_fn
;
2302 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
2304 if (hash_fn
== NULL
)
2305 vals
->push_back(Expression::make_cast(hash_fntype
,
2306 Expression::make_nil(bloc
),
2309 vals
->push_back(Expression::make_func_reference(hash_fn
, NULL
, bloc
));
2310 if (equal_fn
== NULL
)
2311 vals
->push_back(Expression::make_cast(equal_fntype
,
2312 Expression::make_nil(bloc
),
2315 vals
->push_back(Expression::make_func_reference(equal_fn
, NULL
, bloc
));
2318 go_assert(p
->is_field_name("gcdata"));
2319 vals
->push_back(Expression::make_gc_symbol(this));
2322 go_assert(p
->is_field_name("string"));
2323 Expression
* s
= Expression::make_string((name
!= NULL
2324 ? name
->reflection(gogo
)
2325 : this->reflection(gogo
)),
2327 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2330 go_assert(p
->is_field_name("uncommonType"));
2331 if (name
== NULL
&& methods
== NULL
)
2332 vals
->push_back(Expression::make_nil(bloc
));
2335 if (methods
== NULL
)
2336 methods
= name
->methods();
2337 vals
->push_back(this->uncommon_type_constructor(gogo
,
2340 only_value_methods
));
2344 go_assert(p
->is_field_name("ptrToThis"));
2345 if (name
== NULL
&& methods
== NULL
)
2346 vals
->push_back(Expression::make_nil(bloc
));
2351 pt
= Type::make_pointer_type(name
);
2353 pt
= Type::make_pointer_type(this);
2354 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
2358 go_assert(p
== fields
->end());
2360 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
2363 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2364 // length used by the gc toolchain, and also appears in
2365 // libgo/go/reflect/type.go.
2367 static const int64_t max_ptrmask_bytes
= 2048;
2369 // Return a pointer to the Garbage Collection information for this type.
2372 Type::gc_symbol_pointer(Gogo
* gogo
)
2374 Type
* t
= this->forwarded();
2375 while (t
->named_type() != NULL
&& t
->named_type()->is_alias())
2376 t
= t
->named_type()->real_type()->forwarded();
2378 if (!t
->has_pointer())
2379 return gogo
->backend()->nil_pointer_expression();
2381 if (t
->gc_symbol_var_
== NULL
)
2383 t
->make_gc_symbol_var(gogo
);
2384 go_assert(t
->gc_symbol_var_
!= NULL
);
2386 Location bloc
= Linemap::predeclared_location();
2387 Bexpression
* var_expr
=
2388 gogo
->backend()->var_expression(t
->gc_symbol_var_
, VE_rvalue
, bloc
);
2389 Bexpression
* addr_expr
=
2390 gogo
->backend()->address_expression(var_expr
, bloc
);
2392 Type
* uint8_type
= Type::lookup_integer_type("uint8");
2393 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
2394 Btype
* ubtype
= pointer_uint8_type
->get_backend(gogo
);
2395 return gogo
->backend()->convert_expression(ubtype
, addr_expr
, bloc
);
2398 // A mapping from unnamed types to GC symbol variables.
2400 Type::GC_symbol_vars
Type::gc_symbol_vars
;
2402 // Build the GC symbol for this type.
2405 Type::make_gc_symbol_var(Gogo
* gogo
)
2407 go_assert(this->gc_symbol_var_
== NULL
);
2409 Named_type
* nt
= this->named_type();
2411 // We can have multiple instances of unnamed types and similar to type
2412 // descriptors, we only want to the emit the GC data once, so we use a
2414 Bvariable
** phash
= NULL
;
2417 Bvariable
* bvnull
= NULL
;
2418 std::pair
<GC_symbol_vars::iterator
, bool> ins
=
2419 Type::gc_symbol_vars
.insert(std::make_pair(this, bvnull
));
2422 // We've already built a gc symbol for this type.
2423 this->gc_symbol_var_
= ins
.first
->second
;
2426 phash
= &ins
.first
->second
;
2431 if (!this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2433 this->gc_symbol_var_
= this->gc_ptrmask_var(gogo
, ptrsize
, ptrdata
);
2435 *phash
= this->gc_symbol_var_
;
2439 std::string sym_name
= gogo
->gc_symbol_name(this);
2441 // Build the contents of the gc symbol.
2442 Expression
* sym_init
= this->gcprog_constructor(gogo
, ptrsize
, ptrdata
);
2443 Btype
* sym_btype
= sym_init
->type()->get_backend(gogo
);
2445 // If the type descriptor for this type is defined somewhere else, so is the
2447 const Package
* dummy
;
2448 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
2450 std::string
asm_name(go_selectively_encode_id(sym_name
));
2451 this->gc_symbol_var_
=
2452 gogo
->backend()->implicit_variable_reference(sym_name
, asm_name
,
2455 *phash
= this->gc_symbol_var_
;
2459 // See if this gc symbol can appear in multiple packages.
2460 bool is_common
= false;
2463 // We create the symbol for a builtin type whenever we need
2465 is_common
= nt
->is_builtin();
2469 // This is an unnamed type. The descriptor could be defined in
2470 // any package where it is needed, and the linker will pick one
2471 // descriptor to keep.
2475 // Since we are building the GC symbol in this package, we must create the
2476 // variable before converting the initializer to its backend representation
2477 // because the initializer may refer to the GC symbol for this type.
2478 std::string
asm_name(go_selectively_encode_id(sym_name
));
2479 this->gc_symbol_var_
=
2480 gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2481 sym_btype
, false, true, is_common
, 0);
2483 *phash
= this->gc_symbol_var_
;
2485 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2486 context
.set_is_const();
2487 Bexpression
* sym_binit
= sym_init
->get_backend(&context
);
2488 gogo
->backend()->implicit_variable_set_init(this->gc_symbol_var_
, sym_name
,
2489 sym_btype
, false, true, is_common
,
2493 // Return whether this type needs a GC program, and set *PTRDATA to
2494 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2498 Type::needs_gcprog(Gogo
* gogo
, int64_t* ptrsize
, int64_t* ptrdata
)
2500 Type
* voidptr
= Type::make_pointer_type(Type::make_void_type());
2501 if (!voidptr
->backend_type_size(gogo
, ptrsize
))
2504 if (!this->backend_type_ptrdata(gogo
, ptrdata
))
2506 go_assert(saw_errors());
2510 return *ptrdata
/ *ptrsize
> max_ptrmask_bytes
;
2513 // A simple class used to build a GC ptrmask for a type.
2518 Ptrmask(size_t count
)
2519 : bits_((count
+ 7) / 8, 0)
2523 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2529 constructor(Gogo
* gogo
) const;
2534 { this->bits_
.at(index
/ 8) |= 1 << (index
% 8); }
2537 std::vector
<unsigned char> bits_
;
2540 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2541 // counts in bytes. PTRSIZE is the size of a pointer on the target
2545 Ptrmask::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2547 switch (type
->base()->classification())
2550 case Type::TYPE_NIL
:
2551 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2552 case Type::TYPE_NAMED
:
2553 case Type::TYPE_FORWARD
:
2556 case Type::TYPE_ERROR
:
2557 case Type::TYPE_VOID
:
2558 case Type::TYPE_BOOLEAN
:
2559 case Type::TYPE_INTEGER
:
2560 case Type::TYPE_FLOAT
:
2561 case Type::TYPE_COMPLEX
:
2562 case Type::TYPE_SINK
:
2565 case Type::TYPE_FUNCTION
:
2566 case Type::TYPE_POINTER
:
2567 case Type::TYPE_MAP
:
2568 case Type::TYPE_CHANNEL
:
2569 // These types are all a single pointer.
2570 go_assert((offset
% ptrsize
) == 0);
2571 this->set(offset
/ ptrsize
);
2574 case Type::TYPE_STRING
:
2575 // A string starts with a single pointer.
2576 go_assert((offset
% ptrsize
) == 0);
2577 this->set(offset
/ ptrsize
);
2580 case Type::TYPE_INTERFACE
:
2581 // An interface is two pointers.
2582 go_assert((offset
% ptrsize
) == 0);
2583 this->set(offset
/ ptrsize
);
2584 this->set((offset
/ ptrsize
) + 1);
2587 case Type::TYPE_STRUCT
:
2589 if (!type
->has_pointer())
2592 const Struct_field_list
* fields
= type
->struct_type()->fields();
2593 int64_t soffset
= 0;
2594 for (Struct_field_list::const_iterator pf
= fields
->begin();
2595 pf
!= fields
->end();
2598 int64_t field_align
;
2599 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2601 go_assert(saw_errors());
2604 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2606 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2609 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2611 go_assert(saw_errors());
2614 soffset
+= field_size
;
2619 case Type::TYPE_ARRAY
:
2620 if (type
->is_slice_type())
2622 // A slice starts with a single pointer.
2623 go_assert((offset
% ptrsize
) == 0);
2624 this->set(offset
/ ptrsize
);
2629 if (!type
->has_pointer())
2633 if (!type
->array_type()->int_length(&len
))
2635 go_assert(saw_errors());
2639 Type
* element_type
= type
->array_type()->element_type();
2641 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2643 go_assert(saw_errors());
2647 int64_t eoffset
= 0;
2648 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2649 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2655 // Return a symbol name for this ptrmask. This is used to coalesce
2656 // identical ptrmasks, which are common. The symbol name must use
2657 // only characters that are valid in symbols. It's nice if it's
2658 // short. We convert it to a base64 string.
2661 Ptrmask::symname() const
2663 const char chars
[65] =
2664 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.";
2665 go_assert(chars
[64] == '\0');
2669 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2670 p
!= this->bits_
.end();
2673 b
|= *p
<< remaining
;
2675 while (remaining
>= 6)
2677 ret
+= chars
[b
& 0x3f];
2682 while (remaining
> 0)
2684 ret
+= chars
[b
& 0x3f];
2691 // Return a constructor for this ptrmask. This will be used to
2692 // initialize the runtime ptrmask value.
2695 Ptrmask::constructor(Gogo
* gogo
) const
2697 Location bloc
= Linemap::predeclared_location();
2698 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
2699 Expression
* len
= Expression::make_integer_ul(this->bits_
.size(), NULL
,
2701 Array_type
* at
= Type::make_array_type(byte_type
, len
);
2702 Expression_list
* vals
= new Expression_list();
2703 vals
->reserve(this->bits_
.size());
2704 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2705 p
!= this->bits_
.end();
2707 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
2708 return Expression::make_array_composite_literal(at
, vals
, bloc
);
2711 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2712 Type::GC_gcbits_vars
Type::gc_gcbits_vars
;
2714 // Return a ptrmask variable for a type. For a type descriptor this
2715 // is only used for variables that are small enough to not need a
2716 // gcprog, but for a global variable this is used for a variable of
2717 // any size. PTRDATA is the number of bytes of the type that contain
2718 // pointer data. PTRSIZE is the size of a pointer on the target
2722 Type::gc_ptrmask_var(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
2724 Ptrmask
ptrmask(ptrdata
/ ptrsize
);
2725 if (ptrdata
>= ptrsize
)
2726 ptrmask
.set_from(gogo
, this, ptrsize
, 0);
2729 // This can happen in error cases. Just build an empty gcbits.
2730 go_assert(saw_errors());
2733 std::string sym_name
= gogo
->ptrmask_symbol_name(ptrmask
.symname());
2734 Bvariable
* bvnull
= NULL
;
2735 std::pair
<GC_gcbits_vars::iterator
, bool> ins
=
2736 Type::gc_gcbits_vars
.insert(std::make_pair(sym_name
, bvnull
));
2739 // We've already built a GC symbol for this set of gcbits.
2740 return ins
.first
->second
;
2743 Expression
* val
= ptrmask
.constructor(gogo
);
2744 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2745 context
.set_is_const();
2746 Bexpression
* bval
= val
->get_backend(&context
);
2748 std::string
asm_name(go_selectively_encode_id(sym_name
));
2749 Btype
*btype
= val
->type()->get_backend(gogo
);
2750 Bvariable
* ret
= gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2753 gogo
->backend()->implicit_variable_set_init(ret
, sym_name
, btype
, false,
2755 ins
.first
->second
= ret
;
2759 // A GCProg is used to build a program for the garbage collector.
2760 // This is used for types with a lot of pointer data, to reduce the
2761 // size of the data in the compiled program. The program is expanded
2762 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2768 : bytes_(), index_(0), nb_(0)
2771 // The number of bits described so far.
2774 { return this->index_
; }
2777 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2783 constructor(Gogo
* gogo
) const;
2790 should_repeat(int64_t, int64_t);
2793 repeat(int64_t, int64_t);
2796 zero_until(int64_t);
2807 // Add a byte to the program.
2809 byte(unsigned char x
)
2810 { this->bytes_
.push_back(x
); }
2812 // The maximum number of bytes of literal bits.
2813 static const int max_literal
= 127;
2816 std::vector
<unsigned char> bytes_
;
2817 // The index of the last bit described.
2819 // The current set of literal bits.
2820 unsigned char b_
[max_literal
];
2821 // The current number of literal bits.
2825 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2826 // counts in bytes. PTRSIZE is the size of a pointer on the target
2830 GCProg::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2832 switch (type
->base()->classification())
2835 case Type::TYPE_NIL
:
2836 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2837 case Type::TYPE_NAMED
:
2838 case Type::TYPE_FORWARD
:
2841 case Type::TYPE_ERROR
:
2842 case Type::TYPE_VOID
:
2843 case Type::TYPE_BOOLEAN
:
2844 case Type::TYPE_INTEGER
:
2845 case Type::TYPE_FLOAT
:
2846 case Type::TYPE_COMPLEX
:
2847 case Type::TYPE_SINK
:
2850 case Type::TYPE_FUNCTION
:
2851 case Type::TYPE_POINTER
:
2852 case Type::TYPE_MAP
:
2853 case Type::TYPE_CHANNEL
:
2854 // These types are all a single pointer.
2855 go_assert((offset
% ptrsize
) == 0);
2856 this->ptr(offset
/ ptrsize
);
2859 case Type::TYPE_STRING
:
2860 // A string starts with a single pointer.
2861 go_assert((offset
% ptrsize
) == 0);
2862 this->ptr(offset
/ ptrsize
);
2865 case Type::TYPE_INTERFACE
:
2866 // An interface is two pointers.
2867 go_assert((offset
% ptrsize
) == 0);
2868 this->ptr(offset
/ ptrsize
);
2869 this->ptr((offset
/ ptrsize
) + 1);
2872 case Type::TYPE_STRUCT
:
2874 if (!type
->has_pointer())
2877 const Struct_field_list
* fields
= type
->struct_type()->fields();
2878 int64_t soffset
= 0;
2879 for (Struct_field_list::const_iterator pf
= fields
->begin();
2880 pf
!= fields
->end();
2883 int64_t field_align
;
2884 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2886 go_assert(saw_errors());
2889 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2891 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2894 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2896 go_assert(saw_errors());
2899 soffset
+= field_size
;
2904 case Type::TYPE_ARRAY
:
2905 if (type
->is_slice_type())
2907 // A slice starts with a single pointer.
2908 go_assert((offset
% ptrsize
) == 0);
2909 this->ptr(offset
/ ptrsize
);
2914 if (!type
->has_pointer())
2918 if (!type
->array_type()->int_length(&len
))
2920 go_assert(saw_errors());
2924 Type
* element_type
= type
->array_type()->element_type();
2926 // Flatten array of array to a big array by multiplying counts.
2927 while (element_type
->array_type() != NULL
2928 && !element_type
->is_slice_type())
2931 if (!element_type
->array_type()->int_length(&ele_len
))
2933 go_assert(saw_errors());
2938 element_type
= element_type
->array_type()->element_type();
2942 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2944 go_assert(saw_errors());
2948 go_assert(len
> 0 && ele_size
> 0);
2950 if (!this->should_repeat(ele_size
/ ptrsize
, len
))
2952 // Cheaper to just emit the bits.
2953 int64_t eoffset
= 0;
2954 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2955 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2959 go_assert((offset
% ptrsize
) == 0);
2960 go_assert((ele_size
% ptrsize
) == 0);
2961 this->set_from(gogo
, element_type
, ptrsize
, offset
);
2962 this->zero_until((offset
+ ele_size
) / ptrsize
);
2963 this->repeat(ele_size
/ ptrsize
, len
- 1);
2971 // Emit a 1 into the bit stream of a GC program at the given bit index.
2974 GCProg::ptr(int64_t index
)
2976 go_assert(index
>= this->index_
);
2977 this->zero_until(index
);
2981 // Return whether it is worthwhile to use a repeat to describe c
2982 // elements of n bits each, compared to just emitting c copies of the
2983 // n-bit description.
2986 GCProg::should_repeat(int64_t n
, int64_t c
)
2988 // Repeat if there is more than 1 item and if the total data doesn't
2989 // fit into four bytes.
2990 return c
> 1 && c
* n
> 4 * 8;
2993 // Emit an instruction to repeat the description of the last n words c
2994 // times (including the initial description, so c + 1 times in total).
2997 GCProg::repeat(int64_t n
, int64_t c
)
2999 if (n
== 0 || c
== 0)
3003 this->byte(0x80 | static_cast<unsigned char>(n
& 0x7f));
3010 this->index_
+= n
* c
;
3013 // Add zeros to the bit stream up to the given index.
3016 GCProg::zero_until(int64_t index
)
3018 go_assert(index
>= this->index_
);
3019 int64_t skip
= index
- this->index_
;
3024 for (int64_t i
= 0; i
< skip
; ++i
)
3030 this->repeat(1, skip
- 1);
3033 // Add a single literal bit to the program.
3036 GCProg::lit(unsigned char x
)
3038 if (this->nb_
== GCProg::max_literal
)
3040 this->b_
[this->nb_
] = x
;
3045 // Emit the varint encoding of x.
3048 GCProg::varint(int64_t x
)
3053 this->byte(0x80 | static_cast<unsigned char>(x
& 0x7f));
3056 this->byte(static_cast<unsigned char>(x
& 0x7f));
3059 // Flush any pending literal bits.
3066 this->byte(static_cast<unsigned char>(this->nb_
));
3067 unsigned char bits
= 0;
3068 for (int i
= 0; i
< this->nb_
; ++i
)
3070 bits
|= this->b_
[i
] << (i
% 8);
3071 if ((i
+ 1) % 8 == 0)
3077 if (this->nb_
% 8 != 0)
3082 // Mark the end of a GC program.
3091 // Return an Expression for the bytes in a GC program.
3094 GCProg::constructor(Gogo
* gogo
) const
3096 Location bloc
= Linemap::predeclared_location();
3098 // The first four bytes are the length of the program in target byte
3099 // order. Build a struct whose first type is uint32 to make this
3102 Type
* uint32_type
= Type::lookup_integer_type("uint32");
3104 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
3105 Expression
* len
= Expression::make_integer_ul(this->bytes_
.size(), NULL
,
3107 Array_type
* at
= Type::make_array_type(byte_type
, len
);
3109 Struct_type
* st
= Type::make_builtin_struct_type(2, "len", uint32_type
,
3112 Expression_list
* vals
= new Expression_list();
3113 vals
->reserve(this->bytes_
.size());
3114 for (std::vector
<unsigned char>::const_iterator p
= this->bytes_
.begin();
3115 p
!= this->bytes_
.end();
3117 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
3118 Expression
* bytes
= Expression::make_array_composite_literal(at
, vals
, bloc
);
3120 vals
= new Expression_list();
3121 vals
->push_back(Expression::make_integer_ul(this->bytes_
.size(), uint32_type
,
3123 vals
->push_back(bytes
);
3125 return Expression::make_struct_composite_literal(st
, vals
, bloc
);
3128 // Return a composite literal for the garbage collection program for
3129 // this type. This is only used for types that are too large to use a
3133 Type::gcprog_constructor(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
3135 Location bloc
= Linemap::predeclared_location();
3138 prog
.set_from(gogo
, this, ptrsize
, 0);
3139 int64_t offset
= prog
.bit_index() * ptrsize
;
3143 if (!this->backend_type_size(gogo
, &type_size
))
3145 go_assert(saw_errors());
3146 return Expression::make_error(bloc
);
3149 go_assert(offset
>= ptrdata
&& offset
<= type_size
);
3151 return prog
.constructor(gogo
);
3154 // Return a composite literal for the uncommon type information for
3155 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3156 // struct. If name is not NULL, it is the name of the type. If
3157 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3158 // is true if only value methods should be included. At least one of
3159 // NAME and METHODS must not be NULL.
3162 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
3163 Named_type
* name
, const Methods
* methods
,
3164 bool only_value_methods
) const
3166 Location bloc
= Linemap::predeclared_location();
3168 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
3170 Expression_list
* vals
= new Expression_list();
3173 Struct_field_list::const_iterator p
= fields
->begin();
3174 go_assert(p
->is_field_name("name"));
3177 go_assert(p
->is_field_name("pkgPath"));
3181 vals
->push_back(Expression::make_nil(bloc
));
3182 vals
->push_back(Expression::make_nil(bloc
));
3186 Named_object
* no
= name
->named_object();
3187 std::string n
= Gogo::unpack_hidden_name(no
->name());
3188 Expression
* s
= Expression::make_string(n
, bloc
);
3189 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3191 if (name
->is_builtin())
3192 vals
->push_back(Expression::make_nil(bloc
));
3195 const Package
* package
= no
->package();
3196 const std::string
& pkgpath(package
== NULL
3198 : package
->pkgpath());
3199 s
= Expression::make_string(pkgpath
, bloc
);
3200 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3205 go_assert(p
->is_field_name("methods"));
3206 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
3207 only_value_methods
));
3210 go_assert(p
== fields
->end());
3212 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
3214 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
3217 // Sort methods by name.
3223 operator()(const std::pair
<std::string
, const Method
*>& m1
,
3224 const std::pair
<std::string
, const Method
*>& m2
) const
3226 return (Gogo::unpack_hidden_name(m1
.first
)
3227 < Gogo::unpack_hidden_name(m2
.first
));
3231 // Return a composite literal for the type method table for this type.
3232 // METHODS_TYPE is the type of the table, and is a slice type.
3233 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3234 // then only value methods are used.
3237 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
3238 const Methods
* methods
,
3239 bool only_value_methods
) const
3241 Location bloc
= Linemap::predeclared_location();
3243 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
3244 if (methods
!= NULL
)
3246 smethods
.reserve(methods
->count());
3247 for (Methods::const_iterator p
= methods
->begin();
3248 p
!= methods
->end();
3251 if (p
->second
->is_ambiguous())
3253 if (only_value_methods
&& !p
->second
->is_value_method())
3256 // This is where we implement the magic //go:nointerface
3257 // comment. If we saw that comment, we don't add this
3258 // method to the type descriptor.
3259 if (p
->second
->nointerface())
3262 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
3266 if (smethods
.empty())
3267 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
3269 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
3271 Type
* method_type
= methods_type
->array_type()->element_type();
3273 Expression_list
* vals
= new Expression_list();
3274 vals
->reserve(smethods
.size());
3275 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
3277 p
!= smethods
.end();
3279 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
3280 p
->second
, only_value_methods
));
3282 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
3285 // Return a composite literal for a single method. METHOD_TYPE is the
3286 // type of the entry. METHOD_NAME is the name of the method and M is
3287 // the method information.
3290 Type::method_constructor(Gogo
*, Type
* method_type
,
3291 const std::string
& method_name
,
3293 bool only_value_methods
) const
3295 Location bloc
= Linemap::predeclared_location();
3297 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
3299 Expression_list
* vals
= new Expression_list();
3302 Struct_field_list::const_iterator p
= fields
->begin();
3303 go_assert(p
->is_field_name("name"));
3304 const std::string n
= Gogo::unpack_hidden_name(method_name
);
3305 Expression
* s
= Expression::make_string(n
, bloc
);
3306 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3309 go_assert(p
->is_field_name("pkgPath"));
3310 if (!Gogo::is_hidden_name(method_name
))
3311 vals
->push_back(Expression::make_nil(bloc
));
3314 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
3316 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3319 Named_object
* no
= (m
->needs_stub_method()
3321 : m
->named_object());
3323 Function_type
* mtype
;
3324 if (no
->is_function())
3325 mtype
= no
->func_value()->type();
3327 mtype
= no
->func_declaration_value()->type();
3328 go_assert(mtype
->is_method());
3329 Type
* nonmethod_type
= mtype
->copy_without_receiver();
3332 go_assert(p
->is_field_name("mtyp"));
3333 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3336 go_assert(p
->is_field_name("typ"));
3337 bool want_pointer_receiver
= !only_value_methods
&& m
->is_value_method();
3338 nonmethod_type
= mtype
->copy_with_receiver_as_param(want_pointer_receiver
);
3339 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3342 go_assert(p
->is_field_name("tfn"));
3343 vals
->push_back(Expression::make_func_code_reference(no
, bloc
));
3346 go_assert(p
== fields
->end());
3348 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
3351 // Return a composite literal for the type descriptor of a plain type.
3352 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3353 // NULL, it is the name to use as well as the list of methods.
3356 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
3359 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
3363 // Return the type reflection string for this type.
3366 Type::reflection(Gogo
* gogo
) const
3370 // The do_reflection virtual function should set RET to the
3371 // reflection string.
3372 this->do_reflection(gogo
, &ret
);
3377 // Return whether the backend size of the type is known.
3380 Type::is_backend_type_size_known(Gogo
* gogo
)
3382 switch (this->classification_
)
3396 case TYPE_INTERFACE
:
3401 const Struct_field_list
* fields
= this->struct_type()->fields();
3402 for (Struct_field_list::const_iterator pf
= fields
->begin();
3403 pf
!= fields
->end();
3405 if (!pf
->type()->is_backend_type_size_known(gogo
))
3412 const Array_type
* at
= this->array_type();
3413 if (at
->length() == NULL
)
3417 Numeric_constant nc
;
3418 if (!at
->length()->numeric_constant_value(&nc
))
3421 if (!nc
.to_int(&ival
))
3424 return at
->element_type()->is_backend_type_size_known(gogo
);
3429 this->named_type()->convert(gogo
);
3430 return this->named_type()->is_named_backend_type_size_known();
3434 Forward_declaration_type
* fdt
= this->forward_declaration_type();
3435 return fdt
->real_type()->is_backend_type_size_known(gogo
);
3439 case TYPE_CALL_MULTIPLE_RESULT
:
3447 // If the size of the type can be determined, set *PSIZE to the size
3448 // in bytes and return true. Otherwise, return false. This queries
3452 Type::backend_type_size(Gogo
* gogo
, int64_t *psize
)
3454 if (!this->is_backend_type_size_known(gogo
))
3456 if (this->is_error_type())
3458 Btype
* bt
= this->get_backend_placeholder(gogo
);
3459 *psize
= gogo
->backend()->type_size(bt
);
3462 if (this->named_type() != NULL
)
3463 go_error_at(this->named_type()->location(),
3464 "type %s larger than address space",
3465 Gogo::message_name(this->named_type()->name()).c_str());
3467 go_error_at(Linemap::unknown_location(),
3468 "type %s larger than address space",
3469 this->reflection(gogo
).c_str());
3471 // Make this an error type to avoid knock-on errors.
3472 this->classification_
= TYPE_ERROR
;
3478 // If the alignment of the type can be determined, set *PALIGN to
3479 // the alignment in bytes and return true. Otherwise, return false.
3482 Type::backend_type_align(Gogo
* gogo
, int64_t *palign
)
3484 if (!this->is_backend_type_size_known(gogo
))
3486 Btype
* bt
= this->get_backend_placeholder(gogo
);
3487 *palign
= gogo
->backend()->type_alignment(bt
);
3491 // Like backend_type_align, but return the alignment when used as a
3495 Type::backend_type_field_align(Gogo
* gogo
, int64_t *palign
)
3497 if (!this->is_backend_type_size_known(gogo
))
3499 Btype
* bt
= this->get_backend_placeholder(gogo
);
3500 *palign
= gogo
->backend()->type_field_alignment(bt
);
3504 // Get the ptrdata value for a type. This is the size of the prefix
3505 // of the type that contains all pointers. Store the ptrdata in
3506 // *PPTRDATA and return whether we found it.
3509 Type::backend_type_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3513 if (!this->has_pointer())
3516 if (!this->is_backend_type_size_known(gogo
))
3519 switch (this->classification_
)
3528 // These types are nothing but a pointer.
3529 return this->backend_type_size(gogo
, pptrdata
);
3531 case TYPE_INTERFACE
:
3532 // An interface is a struct of two pointers.
3533 return this->backend_type_size(gogo
, pptrdata
);
3537 // A string is a struct whose first field is a pointer, and
3538 // whose second field is not.
3539 Type
* uint8_type
= Type::lookup_integer_type("uint8");
3540 Type
* ptr
= Type::make_pointer_type(uint8_type
);
3541 return ptr
->backend_type_size(gogo
, pptrdata
);
3546 return this->base()->backend_type_ptrdata(gogo
, pptrdata
);
3550 const Struct_field_list
* fields
= this->struct_type()->fields();
3552 const Struct_field
*ptr
= NULL
;
3553 int64_t ptr_offset
= 0;
3554 for (Struct_field_list::const_iterator pf
= fields
->begin();
3555 pf
!= fields
->end();
3558 int64_t field_align
;
3559 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
3561 offset
= (offset
+ (field_align
- 1)) &~ (field_align
- 1);
3563 if (pf
->type()->has_pointer())
3566 ptr_offset
= offset
;
3570 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
3572 offset
+= field_size
;
3577 int64_t ptr_ptrdata
;
3578 if (!ptr
->type()->backend_type_ptrdata(gogo
, &ptr_ptrdata
))
3580 *pptrdata
= ptr_offset
+ ptr_ptrdata
;
3586 if (this->is_slice_type())
3588 // A slice is a struct whose first field is a pointer, and
3589 // whose remaining fields are not.
3590 Type
* element_type
= this->array_type()->element_type();
3591 Type
* ptr
= Type::make_pointer_type(element_type
);
3592 return ptr
->backend_type_size(gogo
, pptrdata
);
3596 Numeric_constant nc
;
3597 if (!this->array_type()->length()->numeric_constant_value(&nc
))
3600 if (!nc
.to_memory_size(&len
))
3603 Type
* element_type
= this->array_type()->element_type();
3605 int64_t ele_ptrdata
;
3606 if (!element_type
->backend_type_size(gogo
, &ele_size
)
3607 || !element_type
->backend_type_ptrdata(gogo
, &ele_ptrdata
))
3609 go_assert(ele_size
> 0 && ele_ptrdata
> 0);
3611 *pptrdata
= (len
- 1) * ele_size
+ ele_ptrdata
;
3623 case TYPE_CALL_MULTIPLE_RESULT
:
3628 // Get the ptrdata value to store in a type descriptor. This is
3629 // normally the same as backend_type_ptrdata, but for a type that is
3630 // large enough to use a gcprog we may need to store a different value
3631 // if it ends with an array. If the gcprog uses a repeat descriptor
3632 // for the array, and if the array element ends with non-pointer data,
3633 // then the gcprog will produce a value that describes the complete
3634 // array where the backend ptrdata will omit the non-pointer elements
3635 // of the final array element. This is a subtle difference but the
3636 // run time code checks it to verify that it has expanded a gcprog as
3640 Type::descriptor_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3642 int64_t backend_ptrdata
;
3643 if (!this->backend_type_ptrdata(gogo
, &backend_ptrdata
))
3647 if (!this->needs_gcprog(gogo
, &ptrsize
, &backend_ptrdata
))
3649 *pptrdata
= backend_ptrdata
;
3654 prog
.set_from(gogo
, this, ptrsize
, 0);
3655 int64_t offset
= prog
.bit_index() * ptrsize
;
3657 go_assert(offset
>= backend_ptrdata
);
3662 // Default function to export a type.
3665 Type::do_export(Export
*) const
3673 Type::import_type(Import
* imp
)
3675 if (imp
->match_c_string("("))
3676 return Function_type::do_import(imp
);
3677 else if (imp
->match_c_string("*"))
3678 return Pointer_type::do_import(imp
);
3679 else if (imp
->match_c_string("struct "))
3680 return Struct_type::do_import(imp
);
3681 else if (imp
->match_c_string("["))
3682 return Array_type::do_import(imp
);
3683 else if (imp
->match_c_string("map "))
3684 return Map_type::do_import(imp
);
3685 else if (imp
->match_c_string("chan "))
3686 return Channel_type::do_import(imp
);
3687 else if (imp
->match_c_string("interface"))
3688 return Interface_type::do_import(imp
);
3691 go_error_at(imp
->location(), "import error: expected type");
3692 return Type::make_error_type();
3696 // Class Error_type.
3698 // Return the backend representation of an Error type.
3701 Error_type::do_get_backend(Gogo
* gogo
)
3703 return gogo
->backend()->error_type();
3706 // Return an expression for the type descriptor for an error type.
3710 Error_type::do_type_descriptor(Gogo
*, Named_type
*)
3712 return Expression::make_error(Linemap::predeclared_location());
3715 // We should not be asked for the reflection string for an error type.
3718 Error_type::do_reflection(Gogo
*, std::string
*) const
3720 go_assert(saw_errors());
3724 Type::make_error_type()
3726 static Error_type singleton_error_type
;
3727 return &singleton_error_type
;
3732 // Get the backend representation of a void type.
3735 Void_type::do_get_backend(Gogo
* gogo
)
3737 return gogo
->backend()->void_type();
3741 Type::make_void_type()
3743 static Void_type singleton_void_type
;
3744 return &singleton_void_type
;
3747 // Class Boolean_type.
3749 // Return the backend representation of the boolean type.
3752 Boolean_type::do_get_backend(Gogo
* gogo
)
3754 return gogo
->backend()->bool_type();
3757 // Make the type descriptor.
3760 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3763 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
3766 Named_object
* no
= gogo
->lookup_global("bool");
3767 go_assert(no
!= NULL
);
3768 return Type::type_descriptor(gogo
, no
->type_value());
3773 Type::make_boolean_type()
3775 static Boolean_type boolean_type
;
3776 return &boolean_type
;
3779 // The named type "bool".
3781 static Named_type
* named_bool_type
;
3783 // Get the named type "bool".
3786 Type::lookup_bool_type()
3788 return named_bool_type
;
3791 // Make the named type "bool".
3794 Type::make_named_bool_type()
3796 Type
* bool_type
= Type::make_boolean_type();
3797 Named_object
* named_object
=
3798 Named_object::make_type("bool", NULL
, bool_type
,
3799 Linemap::predeclared_location());
3800 Named_type
* named_type
= named_object
->type_value();
3801 named_bool_type
= named_type
;
3805 // Class Integer_type.
3807 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
3809 // Create a new integer type. Non-abstract integer types always have
3813 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
3814 int bits
, int runtime_type_kind
)
3816 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
3818 std::string
sname(name
);
3819 Named_object
* named_object
=
3820 Named_object::make_type(sname
, NULL
, integer_type
,
3821 Linemap::predeclared_location());
3822 Named_type
* named_type
= named_object
->type_value();
3823 std::pair
<Named_integer_types::iterator
, bool> ins
=
3824 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
3825 go_assert(ins
.second
);
3829 // Look up an existing integer type.
3832 Integer_type::lookup_integer_type(const char* name
)
3834 Named_integer_types::const_iterator p
=
3835 Integer_type::named_integer_types
.find(name
);
3836 go_assert(p
!= Integer_type::named_integer_types
.end());
3840 // Create a new abstract integer type.
3843 Integer_type::create_abstract_integer_type()
3845 static Integer_type
* abstract_type
;
3846 if (abstract_type
== NULL
)
3848 Type
* int_type
= Type::lookup_integer_type("int");
3849 abstract_type
= new Integer_type(true, false,
3850 int_type
->integer_type()->bits(),
3851 RUNTIME_TYPE_KIND_INT
);
3853 return abstract_type
;
3856 // Create a new abstract character type.
3859 Integer_type::create_abstract_character_type()
3861 static Integer_type
* abstract_type
;
3862 if (abstract_type
== NULL
)
3864 abstract_type
= new Integer_type(true, false, 32,
3865 RUNTIME_TYPE_KIND_INT32
);
3866 abstract_type
->set_is_rune();
3868 return abstract_type
;
3871 // Integer type compatibility.
3874 Integer_type::is_identical(const Integer_type
* t
) const
3876 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
3878 return this->is_abstract_
== t
->is_abstract_
;
3884 Integer_type::do_hash_for_method(Gogo
*) const
3886 return ((this->bits_
<< 4)
3887 + ((this->is_unsigned_
? 1 : 0) << 8)
3888 + ((this->is_abstract_
? 1 : 0) << 9));
3891 // Convert an Integer_type to the backend representation.
3894 Integer_type::do_get_backend(Gogo
* gogo
)
3896 if (this->is_abstract_
)
3898 go_assert(saw_errors());
3899 return gogo
->backend()->error_type();
3901 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
3904 // The type descriptor for an integer type. Integer types are always
3908 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3910 go_assert(name
!= NULL
|| saw_errors());
3911 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
3914 // We should not be asked for the reflection string of a basic type.
3917 Integer_type::do_reflection(Gogo
*, std::string
*) const
3919 go_assert(saw_errors());
3922 // Make an integer type.
3925 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
3926 int runtime_type_kind
)
3928 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
3932 // Make an abstract integer type.
3935 Type::make_abstract_integer_type()
3937 return Integer_type::create_abstract_integer_type();
3940 // Make an abstract character type.
3943 Type::make_abstract_character_type()
3945 return Integer_type::create_abstract_character_type();
3948 // Look up an integer type.
3951 Type::lookup_integer_type(const char* name
)
3953 return Integer_type::lookup_integer_type(name
);
3956 // Class Float_type.
3958 Float_type::Named_float_types
Float_type::named_float_types
;
3960 // Create a new float type. Non-abstract float types always have
3964 Float_type::create_float_type(const char* name
, int bits
,
3965 int runtime_type_kind
)
3967 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
3968 std::string
sname(name
);
3969 Named_object
* named_object
=
3970 Named_object::make_type(sname
, NULL
, float_type
,
3971 Linemap::predeclared_location());
3972 Named_type
* named_type
= named_object
->type_value();
3973 std::pair
<Named_float_types::iterator
, bool> ins
=
3974 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
3975 go_assert(ins
.second
);
3979 // Look up an existing float type.
3982 Float_type::lookup_float_type(const char* name
)
3984 Named_float_types::const_iterator p
=
3985 Float_type::named_float_types
.find(name
);
3986 go_assert(p
!= Float_type::named_float_types
.end());
3990 // Create a new abstract float type.
3993 Float_type::create_abstract_float_type()
3995 static Float_type
* abstract_type
;
3996 if (abstract_type
== NULL
)
3997 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
3998 return abstract_type
;
4001 // Whether this type is identical with T.
4004 Float_type::is_identical(const Float_type
* t
) const
4006 if (this->bits_
!= t
->bits_
)
4008 return this->is_abstract_
== t
->is_abstract_
;
4014 Float_type::do_hash_for_method(Gogo
*) const
4016 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4019 // Convert to the backend representation.
4022 Float_type::do_get_backend(Gogo
* gogo
)
4024 return gogo
->backend()->float_type(this->bits_
);
4027 // The type descriptor for a float type. Float types are always named.
4030 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4032 go_assert(name
!= NULL
|| saw_errors());
4033 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4036 // We should not be asked for the reflection string of a basic type.
4039 Float_type::do_reflection(Gogo
*, std::string
*) const
4041 go_assert(saw_errors());
4044 // Make a floating point type.
4047 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
4049 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
4052 // Make an abstract float type.
4055 Type::make_abstract_float_type()
4057 return Float_type::create_abstract_float_type();
4060 // Look up a float type.
4063 Type::lookup_float_type(const char* name
)
4065 return Float_type::lookup_float_type(name
);
4068 // Class Complex_type.
4070 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
4072 // Create a new complex type. Non-abstract complex types always have
4076 Complex_type::create_complex_type(const char* name
, int bits
,
4077 int runtime_type_kind
)
4079 Complex_type
* complex_type
= new Complex_type(false, bits
,
4081 std::string
sname(name
);
4082 Named_object
* named_object
=
4083 Named_object::make_type(sname
, NULL
, complex_type
,
4084 Linemap::predeclared_location());
4085 Named_type
* named_type
= named_object
->type_value();
4086 std::pair
<Named_complex_types::iterator
, bool> ins
=
4087 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
4089 go_assert(ins
.second
);
4093 // Look up an existing complex type.
4096 Complex_type::lookup_complex_type(const char* name
)
4098 Named_complex_types::const_iterator p
=
4099 Complex_type::named_complex_types
.find(name
);
4100 go_assert(p
!= Complex_type::named_complex_types
.end());
4104 // Create a new abstract complex type.
4107 Complex_type::create_abstract_complex_type()
4109 static Complex_type
* abstract_type
;
4110 if (abstract_type
== NULL
)
4111 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
4112 return abstract_type
;
4115 // Whether this type is identical with T.
4118 Complex_type::is_identical(const Complex_type
*t
) const
4120 if (this->bits_
!= t
->bits_
)
4122 return this->is_abstract_
== t
->is_abstract_
;
4128 Complex_type::do_hash_for_method(Gogo
*) const
4130 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4133 // Convert to the backend representation.
4136 Complex_type::do_get_backend(Gogo
* gogo
)
4138 return gogo
->backend()->complex_type(this->bits_
);
4141 // The type descriptor for a complex type. Complex types are always
4145 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4147 go_assert(name
!= NULL
|| saw_errors());
4148 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4151 // We should not be asked for the reflection string of a basic type.
4154 Complex_type::do_reflection(Gogo
*, std::string
*) const
4156 go_assert(saw_errors());
4159 // Make a complex type.
4162 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
4164 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
4167 // Make an abstract complex type.
4170 Type::make_abstract_complex_type()
4172 return Complex_type::create_abstract_complex_type();
4175 // Look up a complex type.
4178 Type::lookup_complex_type(const char* name
)
4180 return Complex_type::lookup_complex_type(name
);
4183 // Class String_type.
4185 // Convert String_type to the backend representation. A string is a
4186 // struct with two fields: a pointer to the characters and a length.
4189 String_type::do_get_backend(Gogo
* gogo
)
4191 static Btype
* backend_string_type
;
4192 if (backend_string_type
== NULL
)
4194 std::vector
<Backend::Btyped_identifier
> fields(2);
4196 Type
* b
= gogo
->lookup_global("byte")->type_value();
4197 Type
* pb
= Type::make_pointer_type(b
);
4199 // We aren't going to get back to this field to finish the
4200 // backend representation, so force it to be finished now.
4201 if (!gogo
->named_types_are_converted())
4203 Btype
* bt
= pb
->get_backend_placeholder(gogo
);
4204 pb
->finish_backend(gogo
, bt
);
4207 fields
[0].name
= "__data";
4208 fields
[0].btype
= pb
->get_backend(gogo
);
4209 fields
[0].location
= Linemap::predeclared_location();
4211 Type
* int_type
= Type::lookup_integer_type("int");
4212 fields
[1].name
= "__length";
4213 fields
[1].btype
= int_type
->get_backend(gogo
);
4214 fields
[1].location
= fields
[0].location
;
4216 backend_string_type
= gogo
->backend()->struct_type(fields
);
4218 return backend_string_type
;
4221 // The type descriptor for the string type.
4224 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4227 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
4230 Named_object
* no
= gogo
->lookup_global("string");
4231 go_assert(no
!= NULL
);
4232 return Type::type_descriptor(gogo
, no
->type_value());
4236 // We should not be asked for the reflection string of a basic type.
4239 String_type::do_reflection(Gogo
*, std::string
* ret
) const
4241 ret
->append("string");
4244 // Make a string type.
4247 Type::make_string_type()
4249 static String_type string_type
;
4250 return &string_type
;
4253 // The named type "string".
4255 static Named_type
* named_string_type
;
4257 // Get the named type "string".
4260 Type::lookup_string_type()
4262 return named_string_type
;
4265 // Make the named type string.
4268 Type::make_named_string_type()
4270 Type
* string_type
= Type::make_string_type();
4271 Named_object
* named_object
=
4272 Named_object::make_type("string", NULL
, string_type
,
4273 Linemap::predeclared_location());
4274 Named_type
* named_type
= named_object
->type_value();
4275 named_string_type
= named_type
;
4279 // The sink type. This is the type of the blank identifier _. Any
4280 // type may be assigned to it.
4282 class Sink_type
: public Type
4291 do_compare_is_identity(Gogo
*)
4295 do_get_backend(Gogo
*)
4296 { go_unreachable(); }
4299 do_type_descriptor(Gogo
*, Named_type
*)
4300 { go_unreachable(); }
4303 do_reflection(Gogo
*, std::string
*) const
4304 { go_unreachable(); }
4307 do_mangled_name(Gogo
*, std::string
*) const
4308 { go_unreachable(); }
4311 // Make the sink type.
4314 Type::make_sink_type()
4316 static Sink_type sink_type
;
4320 // Class Function_type.
4325 Function_type::do_traverse(Traverse
* traverse
)
4327 if (this->receiver_
!= NULL
4328 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
4329 return TRAVERSE_EXIT
;
4330 if (this->parameters_
!= NULL
4331 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
4332 return TRAVERSE_EXIT
;
4333 if (this->results_
!= NULL
4334 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
4335 return TRAVERSE_EXIT
;
4336 return TRAVERSE_CONTINUE
;
4339 // Returns whether T is a valid redeclaration of this type. If this
4340 // returns false, and REASON is not NULL, *REASON may be set to a
4341 // brief explanation of why it returned false.
4344 Function_type::is_valid_redeclaration(const Function_type
* t
,
4345 std::string
* reason
) const
4347 if (!this->is_identical(t
, false, COMPARE_TAGS
, true, reason
))
4350 // A redeclaration of a function is required to use the same names
4351 // for the receiver and parameters.
4352 if (this->receiver() != NULL
4353 && this->receiver()->name() != t
->receiver()->name())
4356 *reason
= "receiver name changed";
4360 const Typed_identifier_list
* parms1
= this->parameters();
4361 const Typed_identifier_list
* parms2
= t
->parameters();
4364 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4365 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4366 p2
!= parms2
->end();
4369 if (p1
->name() != p2
->name())
4372 *reason
= "parameter name changed";
4376 // This is called at parse time, so we may have unknown
4378 Type
* t1
= p1
->type()->forwarded();
4379 Type
* t2
= p2
->type()->forwarded();
4381 && t1
->forward_declaration_type() != NULL
4382 && (t2
->forward_declaration_type() == NULL
4383 || (t1
->forward_declaration_type()->named_object()
4384 != t2
->forward_declaration_type()->named_object())))
4389 const Typed_identifier_list
* results1
= this->results();
4390 const Typed_identifier_list
* results2
= t
->results();
4391 if (results1
!= NULL
)
4393 Typed_identifier_list::const_iterator res1
= results1
->begin();
4394 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4395 res2
!= results2
->end();
4398 if (res1
->name() != res2
->name())
4401 *reason
= "result name changed";
4405 // This is called at parse time, so we may have unknown
4407 Type
* t1
= res1
->type()->forwarded();
4408 Type
* t2
= res2
->type()->forwarded();
4410 && t1
->forward_declaration_type() != NULL
4411 && (t2
->forward_declaration_type() == NULL
4412 || (t1
->forward_declaration_type()->named_object()
4413 != t2
->forward_declaration_type()->named_object())))
4421 // Check whether T is the same as this type.
4424 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
4425 Cmp_tags cmp_tags
, bool errors_are_identical
,
4426 std::string
* reason
) const
4428 if (!ignore_receiver
)
4430 const Typed_identifier
* r1
= this->receiver();
4431 const Typed_identifier
* r2
= t
->receiver();
4432 if ((r1
!= NULL
) != (r2
!= NULL
))
4435 *reason
= _("different receiver types");
4440 if (!Type::are_identical_cmp_tags(r1
->type(), r2
->type(), cmp_tags
,
4441 errors_are_identical
, reason
))
4443 if (reason
!= NULL
&& !reason
->empty())
4444 *reason
= "receiver: " + *reason
;
4450 const Typed_identifier_list
* parms1
= this->parameters();
4451 const Typed_identifier_list
* parms2
= t
->parameters();
4452 if ((parms1
!= NULL
) != (parms2
!= NULL
))
4455 *reason
= _("different number of parameters");
4460 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4461 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4462 p2
!= parms2
->end();
4465 if (p1
== parms1
->end())
4468 *reason
= _("different number of parameters");
4472 if (!Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
4473 errors_are_identical
, NULL
))
4476 *reason
= _("different parameter types");
4480 if (p1
!= parms1
->end())
4483 *reason
= _("different number of parameters");
4488 if (this->is_varargs() != t
->is_varargs())
4491 *reason
= _("different varargs");
4495 const Typed_identifier_list
* results1
= this->results();
4496 const Typed_identifier_list
* results2
= t
->results();
4497 if ((results1
!= NULL
) != (results2
!= NULL
))
4500 *reason
= _("different number of results");
4503 if (results1
!= NULL
)
4505 Typed_identifier_list::const_iterator res1
= results1
->begin();
4506 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4507 res2
!= results2
->end();
4510 if (res1
== results1
->end())
4513 *reason
= _("different number of results");
4517 if (!Type::are_identical_cmp_tags(res1
->type(), res2
->type(),
4518 cmp_tags
, errors_are_identical
,
4522 *reason
= _("different result types");
4526 if (res1
!= results1
->end())
4529 *reason
= _("different number of results");
4540 Function_type::do_hash_for_method(Gogo
* gogo
) const
4542 unsigned int ret
= 0;
4543 // We ignore the receiver type for hash codes, because we need to
4544 // get the same hash code for a method in an interface and a method
4545 // declared for a type. The former will not have a receiver.
4546 if (this->parameters_
!= NULL
)
4549 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
4550 p
!= this->parameters_
->end();
4552 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4554 if (this->results_
!= NULL
)
4557 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
4558 p
!= this->results_
->end();
4560 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4562 if (this->is_varargs_
)
4568 // Hash result parameters.
4571 Function_type::Results_hash::operator()(const Typed_identifier_list
* t
) const
4573 unsigned int hash
= 0;
4574 for (Typed_identifier_list::const_iterator p
= t
->begin();
4579 hash
= Type::hash_string(p
->name(), hash
);
4580 hash
+= p
->type()->hash_for_method(NULL
);
4585 // Compare result parameters so that can map identical result
4586 // parameters to a single struct type.
4589 Function_type::Results_equal::operator()(const Typed_identifier_list
* a
,
4590 const Typed_identifier_list
* b
) const
4592 if (a
->size() != b
->size())
4594 Typed_identifier_list::const_iterator pa
= a
->begin();
4595 for (Typed_identifier_list::const_iterator pb
= b
->begin();
4599 if (pa
->name() != pb
->name()
4600 || !Type::are_identical(pa
->type(), pb
->type(), true, NULL
))
4606 // Hash from results to a backend struct type.
4608 Function_type::Results_structs
Function_type::results_structs
;
4610 // Get the backend representation for a function type.
4613 Function_type::get_backend_fntype(Gogo
* gogo
)
4615 if (this->fnbtype_
== NULL
)
4617 Backend::Btyped_identifier breceiver
;
4618 if (this->receiver_
!= NULL
)
4620 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
4622 // We always pass the address of the receiver parameter, in
4623 // order to make interface calls work with unknown types.
4624 Type
* rtype
= this->receiver_
->type();
4625 if (rtype
->points_to() == NULL
)
4626 rtype
= Type::make_pointer_type(rtype
);
4627 breceiver
.btype
= rtype
->get_backend(gogo
);
4628 breceiver
.location
= this->receiver_
->location();
4631 std::vector
<Backend::Btyped_identifier
> bparameters
;
4632 if (this->parameters_
!= NULL
)
4634 bparameters
.resize(this->parameters_
->size());
4636 for (Typed_identifier_list::const_iterator p
=
4637 this->parameters_
->begin(); p
!= this->parameters_
->end();
4640 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4641 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
4642 bparameters
[i
].location
= p
->location();
4644 go_assert(i
== bparameters
.size());
4647 std::vector
<Backend::Btyped_identifier
> bresults
;
4648 Btype
* bresult_struct
= NULL
;
4649 if (this->results_
!= NULL
)
4651 bresults
.resize(this->results_
->size());
4653 for (Typed_identifier_list::const_iterator p
=
4654 this->results_
->begin();
4655 p
!= this->results_
->end();
4658 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4659 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
4660 bresults
[i
].location
= p
->location();
4662 go_assert(i
== bresults
.size());
4664 if (this->results_
->size() > 1)
4666 // Use the same results struct for all functions that
4667 // return the same set of results. This is useful to
4668 // unify calls to interface methods with other calls.
4669 std::pair
<Typed_identifier_list
*, Btype
*> val
;
4670 val
.first
= this->results_
;
4672 std::pair
<Results_structs::iterator
, bool> ins
=
4673 Function_type::results_structs
.insert(val
);
4676 // Build a new struct type.
4677 Struct_field_list
* sfl
= new Struct_field_list
;
4678 for (Typed_identifier_list::const_iterator p
=
4679 this->results_
->begin();
4680 p
!= this->results_
->end();
4683 Typed_identifier tid
= *p
;
4684 if (tid
.name().empty())
4685 tid
= Typed_identifier("UNNAMED", tid
.type(),
4687 sfl
->push_back(Struct_field(tid
));
4689 Struct_type
* st
= Type::make_struct_type(sfl
,
4691 st
->set_is_struct_incomparable();
4692 ins
.first
->second
= st
->get_backend(gogo
);
4694 bresult_struct
= ins
.first
->second
;
4698 this->fnbtype_
= gogo
->backend()->function_type(breceiver
, bparameters
,
4699 bresults
, bresult_struct
,
4704 return this->fnbtype_
;
4707 // Get the backend representation for a Go function type.
4710 Function_type::do_get_backend(Gogo
* gogo
)
4712 // When we do anything with a function value other than call it, it
4713 // is represented as a pointer to a struct whose first field is the
4714 // actual function. So that is what we return as the type of a Go
4717 Location loc
= this->location();
4718 Btype
* struct_type
=
4719 gogo
->backend()->placeholder_struct_type("__go_descriptor", loc
);
4720 Btype
* ptr_struct_type
= gogo
->backend()->pointer_type(struct_type
);
4722 std::vector
<Backend::Btyped_identifier
> fields(1);
4723 fields
[0].name
= "code";
4724 fields
[0].btype
= this->get_backend_fntype(gogo
);
4725 fields
[0].location
= loc
;
4726 if (!gogo
->backend()->set_placeholder_struct_type(struct_type
, fields
))
4727 return gogo
->backend()->error_type();
4728 return ptr_struct_type
;
4731 // The type of a function type descriptor.
4734 Function_type::make_function_type_descriptor_type()
4739 Type
* tdt
= Type::make_type_descriptor_type();
4740 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4742 Type
* bool_type
= Type::lookup_bool_type();
4744 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
4746 Struct_type
* s
= Type::make_builtin_struct_type(4,
4748 "dotdotdot", bool_type
,
4752 ret
= Type::make_builtin_named_type("FuncType", s
);
4758 // The type descriptor for a function type.
4761 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4763 Location bloc
= Linemap::predeclared_location();
4765 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
4767 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
4769 Expression_list
* vals
= new Expression_list();
4772 Struct_field_list::const_iterator p
= fields
->begin();
4773 go_assert(p
->is_field_name("_type"));
4774 vals
->push_back(this->type_descriptor_constructor(gogo
,
4775 RUNTIME_TYPE_KIND_FUNC
,
4779 go_assert(p
->is_field_name("dotdotdot"));
4780 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
4783 go_assert(p
->is_field_name("in"));
4784 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
4785 this->parameters()));
4788 go_assert(p
->is_field_name("out"));
4789 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
4793 go_assert(p
== fields
->end());
4795 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
4798 // Return a composite literal for the parameters or results of a type
4802 Function_type::type_descriptor_params(Type
* params_type
,
4803 const Typed_identifier
* receiver
,
4804 const Typed_identifier_list
* params
)
4806 Location bloc
= Linemap::predeclared_location();
4808 if (receiver
== NULL
&& params
== NULL
)
4809 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
4811 Expression_list
* vals
= new Expression_list();
4812 vals
->reserve((params
== NULL
? 0 : params
->size())
4813 + (receiver
!= NULL
? 1 : 0));
4815 if (receiver
!= NULL
)
4816 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
4820 for (Typed_identifier_list::const_iterator p
= params
->begin();
4823 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
4826 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
4829 // The reflection string.
4832 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4834 // FIXME: Turn this off until we straighten out the type of the
4835 // struct field used in a go statement which calls a method.
4836 // go_assert(this->receiver_ == NULL);
4838 ret
->append("func");
4840 if (this->receiver_
!= NULL
)
4842 ret
->push_back('(');
4843 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
4844 ret
->push_back(')');
4847 ret
->push_back('(');
4848 const Typed_identifier_list
* params
= this->parameters();
4851 bool is_varargs
= this->is_varargs_
;
4852 for (Typed_identifier_list::const_iterator p
= params
->begin();
4856 if (p
!= params
->begin())
4858 if (!is_varargs
|| p
+ 1 != params
->end())
4859 this->append_reflection(p
->type(), gogo
, ret
);
4863 this->append_reflection(p
->type()->array_type()->element_type(),
4868 ret
->push_back(')');
4870 const Typed_identifier_list
* results
= this->results();
4871 if (results
!= NULL
&& !results
->empty())
4873 if (results
->size() == 1)
4874 ret
->push_back(' ');
4877 for (Typed_identifier_list::const_iterator p
= results
->begin();
4878 p
!= results
->end();
4881 if (p
!= results
->begin())
4883 this->append_reflection(p
->type(), gogo
, ret
);
4885 if (results
->size() > 1)
4886 ret
->push_back(')');
4890 // Export a function type.
4893 Function_type::do_export(Export
* exp
) const
4895 // We don't write out the receiver. The only function types which
4896 // should have a receiver are the ones associated with explicitly
4897 // defined methods. For those the receiver type is written out by
4898 // Function::export_func.
4900 exp
->write_c_string("(");
4902 if (this->parameters_
!= NULL
)
4904 bool is_varargs
= this->is_varargs_
;
4905 for (Typed_identifier_list::const_iterator p
=
4906 this->parameters_
->begin();
4907 p
!= this->parameters_
->end();
4913 exp
->write_c_string(", ");
4914 exp
->write_name(p
->name());
4915 exp
->write_c_string(" ");
4916 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
4917 exp
->write_type(p
->type());
4920 exp
->write_c_string("...");
4921 exp
->write_type(p
->type()->array_type()->element_type());
4925 exp
->write_c_string(")");
4927 const Typed_identifier_list
* results
= this->results_
;
4928 if (results
!= NULL
)
4930 exp
->write_c_string(" ");
4931 if (results
->size() == 1 && results
->begin()->name().empty())
4932 exp
->write_type(results
->begin()->type());
4936 exp
->write_c_string("(");
4937 for (Typed_identifier_list::const_iterator p
= results
->begin();
4938 p
!= results
->end();
4944 exp
->write_c_string(", ");
4945 exp
->write_name(p
->name());
4946 exp
->write_c_string(" ");
4947 exp
->write_type(p
->type());
4949 exp
->write_c_string(")");
4954 // Import a function type.
4957 Function_type::do_import(Import
* imp
)
4959 imp
->require_c_string("(");
4960 Typed_identifier_list
* parameters
;
4961 bool is_varargs
= false;
4962 if (imp
->peek_char() == ')')
4966 parameters
= new Typed_identifier_list();
4969 std::string name
= imp
->read_name();
4970 imp
->require_c_string(" ");
4972 if (imp
->match_c_string("..."))
4978 Type
* ptype
= imp
->read_type();
4980 ptype
= Type::make_array_type(ptype
, NULL
);
4981 parameters
->push_back(Typed_identifier(name
, ptype
,
4983 if (imp
->peek_char() != ',')
4985 go_assert(!is_varargs
);
4986 imp
->require_c_string(", ");
4989 imp
->require_c_string(")");
4991 Typed_identifier_list
* results
;
4992 if (imp
->peek_char() != ' ')
4997 results
= new Typed_identifier_list
;
4998 if (imp
->peek_char() != '(')
5000 Type
* rtype
= imp
->read_type();
5001 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
5008 std::string name
= imp
->read_name();
5009 imp
->require_c_string(" ");
5010 Type
* rtype
= imp
->read_type();
5011 results
->push_back(Typed_identifier(name
, rtype
,
5013 if (imp
->peek_char() != ',')
5015 imp
->require_c_string(", ");
5017 imp
->require_c_string(")");
5021 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
5024 ret
->set_is_varargs();
5028 // Make a copy of a function type without a receiver.
5031 Function_type::copy_without_receiver() const
5033 go_assert(this->is_method());
5034 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
5037 if (this->is_varargs())
5038 ret
->set_is_varargs();
5039 if (this->is_builtin())
5040 ret
->set_is_builtin();
5044 // Make a copy of a function type with a receiver.
5047 Function_type::copy_with_receiver(Type
* receiver_type
) const
5049 go_assert(!this->is_method());
5050 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
5052 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
5055 if (this->is_varargs_
)
5056 ret
->set_is_varargs();
5060 // Make a copy of a function type with the receiver as the first
5064 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver
) const
5066 go_assert(this->is_method());
5067 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5068 Type
* rtype
= this->receiver_
->type();
5069 if (want_pointer_receiver
)
5070 rtype
= Type::make_pointer_type(rtype
);
5071 Typed_identifier
receiver(this->receiver_
->name(), rtype
,
5072 this->receiver_
->location());
5073 new_params
->push_back(receiver
);
5074 const Typed_identifier_list
* orig_params
= this->parameters_
;
5075 if (orig_params
!= NULL
&& !orig_params
->empty())
5077 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5078 p
!= orig_params
->end();
5080 new_params
->push_back(*p
);
5082 return Type::make_function_type(NULL
, new_params
, this->results_
,
5086 // Make a copy of a function type ignoring any receiver and adding a
5087 // closure parameter.
5090 Function_type::copy_with_names() const
5092 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5093 const Typed_identifier_list
* orig_params
= this->parameters_
;
5094 if (orig_params
!= NULL
&& !orig_params
->empty())
5098 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5099 p
!= orig_params
->end();
5102 snprintf(buf
, sizeof buf
, "pt.%u", count
);
5104 new_params
->push_back(Typed_identifier(buf
, p
->type(),
5109 const Typed_identifier_list
* orig_results
= this->results_
;
5110 Typed_identifier_list
* new_results
;
5111 if (orig_results
== NULL
|| orig_results
->empty())
5115 new_results
= new Typed_identifier_list();
5116 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
5117 p
!= orig_results
->end();
5119 new_results
->push_back(Typed_identifier("", p
->type(),
5123 return Type::make_function_type(NULL
, new_params
, new_results
,
5127 // Make a function type.
5130 Type::make_function_type(Typed_identifier
* receiver
,
5131 Typed_identifier_list
* parameters
,
5132 Typed_identifier_list
* results
,
5135 return new Function_type(receiver
, parameters
, results
, location
);
5138 // Make a backend function type.
5140 Backend_function_type
*
5141 Type::make_backend_function_type(Typed_identifier
* receiver
,
5142 Typed_identifier_list
* parameters
,
5143 Typed_identifier_list
* results
,
5146 return new Backend_function_type(receiver
, parameters
, results
, location
);
5149 // Class Pointer_type.
5154 Pointer_type::do_traverse(Traverse
* traverse
)
5156 return Type::traverse(this->to_type_
, traverse
);
5162 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
5164 return this->to_type_
->hash_for_method(gogo
) << 4;
5167 // Get the backend representation for a pointer type.
5170 Pointer_type::do_get_backend(Gogo
* gogo
)
5172 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
5173 return gogo
->backend()->pointer_type(to_btype
);
5176 // The type of a pointer type descriptor.
5179 Pointer_type::make_pointer_type_descriptor_type()
5184 Type
* tdt
= Type::make_type_descriptor_type();
5185 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5187 Struct_type
* s
= Type::make_builtin_struct_type(2,
5191 ret
= Type::make_builtin_named_type("PtrType", s
);
5197 // The type descriptor for a pointer type.
5200 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5202 if (this->is_unsafe_pointer_type())
5204 go_assert(name
!= NULL
);
5205 return this->plain_type_descriptor(gogo
,
5206 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
5211 Location bloc
= Linemap::predeclared_location();
5213 const Methods
* methods
;
5214 Type
* deref
= this->points_to();
5215 if (deref
->named_type() != NULL
)
5216 methods
= deref
->named_type()->methods();
5217 else if (deref
->struct_type() != NULL
)
5218 methods
= deref
->struct_type()->methods();
5222 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
5224 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
5226 Expression_list
* vals
= new Expression_list();
5229 Struct_field_list::const_iterator p
= fields
->begin();
5230 go_assert(p
->is_field_name("_type"));
5231 vals
->push_back(this->type_descriptor_constructor(gogo
,
5232 RUNTIME_TYPE_KIND_PTR
,
5233 name
, methods
, false));
5236 go_assert(p
->is_field_name("elem"));
5237 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
5239 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
5243 // Reflection string.
5246 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5248 ret
->push_back('*');
5249 this->append_reflection(this->to_type_
, gogo
, ret
);
5255 Pointer_type::do_export(Export
* exp
) const
5257 exp
->write_c_string("*");
5258 if (this->is_unsafe_pointer_type())
5259 exp
->write_c_string("any");
5261 exp
->write_type(this->to_type_
);
5267 Pointer_type::do_import(Import
* imp
)
5269 imp
->require_c_string("*");
5270 if (imp
->match_c_string("any"))
5273 return Type::make_pointer_type(Type::make_void_type());
5275 Type
* to
= imp
->read_type();
5276 return Type::make_pointer_type(to
);
5279 // Cache of pointer types. Key is "to" type, value is pointer type
5280 // that points to key.
5282 Type::Pointer_type_table
Type::pointer_types
;
5284 // A list of placeholder pointer types. We keep this so we can ensure
5285 // they are finalized.
5287 std::vector
<Pointer_type
*> Type::placeholder_pointers
;
5289 // Make a pointer type.
5292 Type::make_pointer_type(Type
* to_type
)
5294 Pointer_type_table::const_iterator p
= pointer_types
.find(to_type
);
5295 if (p
!= pointer_types
.end())
5297 Pointer_type
* ret
= new Pointer_type(to_type
);
5298 pointer_types
[to_type
] = ret
;
5302 // This helper is invoked immediately after named types have been
5303 // converted, to clean up any unresolved pointer types remaining in
5304 // the pointer type cache.
5306 // The motivation for this routine: occasionally the compiler creates
5307 // some specific pointer type as part of a lowering operation (ex:
5308 // pointer-to-void), then Type::backend_type_size() is invoked on the
5309 // type (which creates a Btype placeholder for it), that placeholder
5310 // passed somewhere along the line to the back end, but since there is
5311 // no reference to the type in user code, there is never a call to
5312 // Type::finish_backend for the type (hence the Btype remains as an
5313 // unresolved placeholder). Calling this routine will clean up such
5317 Type::finish_pointer_types(Gogo
* gogo
)
5319 // We don't use begin() and end() because it is possible to add new
5320 // placeholder pointer types as we finalized existing ones.
5321 for (size_t i
= 0; i
< Type::placeholder_pointers
.size(); i
++)
5323 Pointer_type
* pt
= Type::placeholder_pointers
[i
];
5324 Type_btypes::iterator tbti
= Type::type_btypes
.find(pt
);
5325 if (tbti
!= Type::type_btypes
.end() && tbti
->second
.is_placeholder
)
5327 pt
->finish_backend(gogo
, tbti
->second
.btype
);
5328 tbti
->second
.is_placeholder
= false;
5335 // Get the backend representation of a nil type. FIXME: Is this ever
5339 Nil_type::do_get_backend(Gogo
* gogo
)
5341 return gogo
->backend()->pointer_type(gogo
->backend()->void_type());
5344 // Make the nil type.
5347 Type::make_nil_type()
5349 static Nil_type singleton_nil_type
;
5350 return &singleton_nil_type
;
5353 // The type of a function call which returns multiple values. This is
5354 // really a struct, but we don't want to confuse a function call which
5355 // returns a struct with a function call which returns multiple
5358 class Call_multiple_result_type
: public Type
5361 Call_multiple_result_type(Call_expression
* call
)
5362 : Type(TYPE_CALL_MULTIPLE_RESULT
),
5368 do_has_pointer() const
5372 do_compare_is_identity(Gogo
*)
5376 do_get_backend(Gogo
* gogo
)
5378 go_assert(saw_errors());
5379 return gogo
->backend()->error_type();
5383 do_type_descriptor(Gogo
*, Named_type
*)
5385 go_assert(saw_errors());
5386 return Expression::make_error(Linemap::unknown_location());
5390 do_reflection(Gogo
*, std::string
*) const
5391 { go_assert(saw_errors()); }
5394 do_mangled_name(Gogo
*, std::string
*) const
5395 { go_assert(saw_errors()); }
5398 // The expression being called.
5399 Call_expression
* call_
;
5402 // Make a call result type.
5405 Type::make_call_multiple_result_type(Call_expression
* call
)
5407 return new Call_multiple_result_type(call
);
5410 // Class Struct_field.
5412 // Get the name of a field.
5415 Struct_field::field_name() const
5417 const std::string
& name(this->typed_identifier_
.name());
5422 // This is called during parsing, before anything is lowered, so
5423 // we have to be pretty careful to avoid dereferencing an
5424 // unknown type name.
5425 Type
* t
= this->typed_identifier_
.type();
5427 if (t
->classification() == Type::TYPE_POINTER
)
5430 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
5431 dt
= ptype
->points_to();
5433 if (dt
->forward_declaration_type() != NULL
)
5434 return dt
->forward_declaration_type()->name();
5435 else if (dt
->named_type() != NULL
)
5437 // Note that this can be an alias name.
5438 return dt
->named_type()->name();
5440 else if (t
->is_error_type() || dt
->is_error_type())
5442 static const std::string error_string
= "*error*";
5443 return error_string
;
5447 // Avoid crashing in the erroneous case where T is named but
5450 if (t
->forward_declaration_type() != NULL
)
5451 return t
->forward_declaration_type()->name();
5452 else if (t
->named_type() != NULL
)
5453 return t
->named_type()->name();
5460 // Return whether this field is named NAME.
5463 Struct_field::is_field_name(const std::string
& name
) const
5465 const std::string
& me(this->typed_identifier_
.name());
5470 Type
* t
= this->typed_identifier_
.type();
5471 if (t
->points_to() != NULL
)
5473 Named_type
* nt
= t
->named_type();
5474 if (nt
!= NULL
&& nt
->name() == name
)
5477 // This is a horrible hack caused by the fact that we don't pack
5478 // the names of builtin types. FIXME.
5479 if (!this->is_imported_
5482 && nt
->name() == Gogo::unpack_hidden_name(name
))
5489 // Return whether this field is an unexported field named NAME.
5492 Struct_field::is_unexported_field_name(Gogo
* gogo
,
5493 const std::string
& name
) const
5495 const std::string
& field_name(this->field_name());
5496 if (Gogo::is_hidden_name(field_name
)
5497 && name
== Gogo::unpack_hidden_name(field_name
)
5498 && gogo
->pack_hidden_name(name
, false) != field_name
)
5501 // Check for the name of a builtin type. This is like the test in
5502 // is_field_name, only there we return false if this->is_imported_,
5503 // and here we return true.
5504 if (this->is_imported_
&& this->is_anonymous())
5506 Type
* t
= this->typed_identifier_
.type();
5507 if (t
->points_to() != NULL
)
5509 Named_type
* nt
= t
->named_type();
5512 && nt
->name() == Gogo::unpack_hidden_name(name
))
5519 // Return whether this field is an embedded built-in type.
5522 Struct_field::is_embedded_builtin(Gogo
* gogo
) const
5524 const std::string
& name(this->field_name());
5525 // We know that a field is an embedded type if it is anonymous.
5526 // We can decide if it is a built-in type by checking to see if it is
5527 // registered globally under the field's name.
5528 // This allows us to distinguish between embedded built-in types and
5529 // embedded types that are aliases to built-in types.
5530 return (this->is_anonymous()
5531 && !Gogo::is_hidden_name(name
)
5532 && gogo
->lookup_global(name
.c_str()) != NULL
);
5535 // Class Struct_type.
5537 // A hash table used to find identical unnamed structs so that they
5538 // share method tables.
5540 Struct_type::Identical_structs
Struct_type::identical_structs
;
5542 // A hash table used to merge method sets for identical unnamed
5545 Struct_type::Struct_method_tables
Struct_type::struct_method_tables
;
5550 Struct_type::do_traverse(Traverse
* traverse
)
5552 Struct_field_list
* fields
= this->fields_
;
5555 for (Struct_field_list::iterator p
= fields
->begin();
5559 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
5560 return TRAVERSE_EXIT
;
5563 return TRAVERSE_CONTINUE
;
5566 // Verify that the struct type is complete and valid.
5569 Struct_type::do_verify()
5571 Struct_field_list
* fields
= this->fields_
;
5574 for (Struct_field_list::iterator p
= fields
->begin();
5578 Type
* t
= p
->type();
5579 if (p
->is_anonymous())
5581 if ((t
->named_type() != NULL
&& t
->points_to() != NULL
)
5582 || (t
->named_type() == NULL
&& t
->points_to() != NULL
5583 && t
->points_to()->points_to() != NULL
))
5585 go_error_at(p
->location(), "embedded type may not be a pointer");
5586 p
->set_type(Type::make_error_type());
5588 else if (t
->points_to() != NULL
5589 && t
->points_to()->interface_type() != NULL
)
5591 go_error_at(p
->location(),
5592 "embedded type may not be pointer to interface");
5593 p
->set_type(Type::make_error_type());
5600 // Whether this contains a pointer.
5603 Struct_type::do_has_pointer() const
5605 const Struct_field_list
* fields
= this->fields();
5608 for (Struct_field_list::const_iterator p
= fields
->begin();
5612 if (p
->type()->has_pointer())
5618 // Whether this type is identical to T.
5621 Struct_type::is_identical(const Struct_type
* t
, Cmp_tags cmp_tags
,
5622 bool errors_are_identical
) const
5624 if (this->is_struct_incomparable_
!= t
->is_struct_incomparable_
)
5626 const Struct_field_list
* fields1
= this->fields();
5627 const Struct_field_list
* fields2
= t
->fields();
5628 if (fields1
== NULL
|| fields2
== NULL
)
5629 return fields1
== fields2
;
5630 Struct_field_list::const_iterator pf2
= fields2
->begin();
5631 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
5632 pf1
!= fields1
->end();
5635 if (pf2
== fields2
->end())
5637 if (pf1
->field_name() != pf2
->field_name())
5639 if (pf1
->is_anonymous() != pf2
->is_anonymous()
5640 || !Type::are_identical_cmp_tags(pf1
->type(), pf2
->type(), cmp_tags
,
5641 errors_are_identical
, NULL
))
5643 if (cmp_tags
== COMPARE_TAGS
)
5645 if (!pf1
->has_tag())
5652 if (!pf2
->has_tag())
5654 if (pf1
->tag() != pf2
->tag())
5659 if (pf2
!= fields2
->end())
5664 // Whether comparisons of this struct type are simple identity
5668 Struct_type::do_compare_is_identity(Gogo
* gogo
)
5670 const Struct_field_list
* fields
= this->fields_
;
5674 for (Struct_field_list::const_iterator pf
= fields
->begin();
5675 pf
!= fields
->end();
5678 if (Gogo::is_sink_name(pf
->field_name()))
5681 if (!pf
->type()->compare_is_identity(gogo
))
5684 int64_t field_align
;
5685 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
5687 if ((offset
& (field_align
- 1)) != 0)
5689 // This struct has padding. We don't guarantee that that
5690 // padding is zero-initialized for a stack variable, so we
5691 // can't use memcmp to compare struct values.
5696 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
5698 offset
+= field_size
;
5701 int64_t struct_size
;
5702 if (!this->backend_type_size(gogo
, &struct_size
))
5704 if (offset
!= struct_size
)
5706 // Trailing padding may not be zero when on the stack.
5713 // Return whether this struct type is reflexive--whether a value of
5714 // this type is always equal to itself.
5717 Struct_type::do_is_reflexive()
5719 const Struct_field_list
* fields
= this->fields_
;
5722 for (Struct_field_list::const_iterator pf
= fields
->begin();
5723 pf
!= fields
->end();
5726 if (!pf
->type()->is_reflexive())
5732 // Return whether this struct type needs a key update when used as a
5736 Struct_type::do_needs_key_update()
5738 const Struct_field_list
* fields
= this->fields_
;
5741 for (Struct_field_list::const_iterator pf
= fields
->begin();
5742 pf
!= fields
->end();
5745 if (pf
->type()->needs_key_update())
5751 // Return whether this struct type is permitted to be in the heap.
5754 Struct_type::do_in_heap()
5756 const Struct_field_list
* fields
= this->fields_
;
5759 for (Struct_field_list::const_iterator pf
= fields
->begin();
5760 pf
!= fields
->end();
5763 if (!pf
->type()->in_heap())
5769 // Build identity and hash functions for this struct.
5774 Struct_type::do_hash_for_method(Gogo
* gogo
) const
5776 unsigned int ret
= 0;
5777 if (this->fields() != NULL
)
5779 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
5780 pf
!= this->fields()->end();
5782 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
5785 if (this->is_struct_incomparable_
)
5790 // Find the local field NAME.
5793 Struct_type::find_local_field(const std::string
& name
,
5794 unsigned int *pindex
) const
5796 const Struct_field_list
* fields
= this->fields_
;
5800 for (Struct_field_list::const_iterator pf
= fields
->begin();
5801 pf
!= fields
->end();
5804 if (pf
->is_field_name(name
))
5814 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
5816 Field_reference_expression
*
5817 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
5818 Location location
) const
5821 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
5825 // Return an expression for a field, along with the depth at which it
5828 Field_reference_expression
*
5829 Struct_type::field_reference_depth(Expression
* struct_expr
,
5830 const std::string
& name
,
5832 Saw_named_type
* saw
,
5833 unsigned int* depth
) const
5835 const Struct_field_list
* fields
= this->fields_
;
5839 // Look for a field with this name.
5841 for (Struct_field_list::const_iterator pf
= fields
->begin();
5842 pf
!= fields
->end();
5845 if (pf
->is_field_name(name
))
5848 return Expression::make_field_reference(struct_expr
, i
, location
);
5852 // Look for an anonymous field which contains a field with this
5854 unsigned int found_depth
= 0;
5855 Field_reference_expression
* ret
= NULL
;
5857 for (Struct_field_list::const_iterator pf
= fields
->begin();
5858 pf
!= fields
->end();
5861 if (!pf
->is_anonymous())
5864 Struct_type
* st
= pf
->type()->deref()->struct_type();
5868 Saw_named_type
* hold_saw
= saw
;
5869 Saw_named_type saw_here
;
5870 Named_type
* nt
= pf
->type()->named_type();
5872 nt
= pf
->type()->deref()->named_type();
5876 for (q
= saw
; q
!= NULL
; q
= q
->next
)
5880 // If this is an error, it will be reported
5887 saw_here
.next
= saw
;
5892 // Look for a reference using a NULL struct expression. If we
5893 // find one, fill in the struct expression with a reference to
5895 unsigned int subdepth
;
5896 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
5906 if (ret
== NULL
|| subdepth
< found_depth
)
5911 found_depth
= subdepth
;
5912 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
5914 if (pf
->type()->points_to() != NULL
)
5915 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
5916 while (sub
->expr() != NULL
)
5918 sub
= sub
->expr()->deref()->field_reference_expression();
5919 go_assert(sub
!= NULL
);
5921 sub
->set_struct_expression(here
);
5922 sub
->set_implicit(true);
5924 else if (subdepth
> found_depth
)
5928 // We do not handle ambiguity here--it should be handled by
5929 // Type::bind_field_or_method.
5937 *depth
= found_depth
+ 1;
5942 // Return the total number of fields, including embedded fields.
5945 Struct_type::total_field_count() const
5947 if (this->fields_
== NULL
)
5949 unsigned int ret
= 0;
5950 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
5951 pf
!= this->fields_
->end();
5954 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
5957 ret
+= pf
->type()->struct_type()->total_field_count();
5962 // Return whether NAME is an unexported field, for better error reporting.
5965 Struct_type::is_unexported_local_field(Gogo
* gogo
,
5966 const std::string
& name
) const
5968 const Struct_field_list
* fields
= this->fields_
;
5971 for (Struct_field_list::const_iterator pf
= fields
->begin();
5972 pf
!= fields
->end();
5974 if (pf
->is_unexported_field_name(gogo
, name
))
5980 // Finalize the methods of an unnamed struct.
5983 Struct_type::finalize_methods(Gogo
* gogo
)
5985 if (this->all_methods_
!= NULL
)
5988 // It is possible to have multiple identical structs that have
5989 // methods. We want them to share method tables. Otherwise we will
5990 // emit identical methods more than once, which is bad since they
5991 // will even have the same names.
5992 std::pair
<Identical_structs::iterator
, bool> ins
=
5993 Struct_type::identical_structs
.insert(std::make_pair(this, this));
5996 // An identical struct was already entered into the hash table.
5997 // Note that finalize_methods is, fortunately, not recursive.
5998 this->all_methods_
= ins
.first
->second
->all_methods_
;
6002 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6005 // Return the method NAME, or NULL if there isn't one or if it is
6006 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6010 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6012 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6015 // Return a pointer to the interface method table for this type for
6016 // the interface INTERFACE. IS_POINTER is true if this is for a
6020 Struct_type::interface_method_table(Interface_type
* interface
,
6023 std::pair
<Struct_type
*, Struct_type::Struct_method_table_pair
*>
6025 std::pair
<Struct_type::Struct_method_tables::iterator
, bool> ins
=
6026 Struct_type::struct_method_tables
.insert(val
);
6028 Struct_method_table_pair
* smtp
;
6030 smtp
= ins
.first
->second
;
6033 smtp
= new Struct_method_table_pair();
6035 smtp
->second
= NULL
;
6036 ins
.first
->second
= smtp
;
6039 return Type::interface_method_table(this, interface
, is_pointer
,
6040 &smtp
->first
, &smtp
->second
);
6043 // Convert struct fields to the backend representation. This is not
6044 // declared in types.h so that types.h doesn't have to #include
6048 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
6049 bool use_placeholder
,
6050 std::vector
<Backend::Btyped_identifier
>* bfields
)
6052 bfields
->resize(fields
->size());
6054 for (Struct_field_list::const_iterator p
= fields
->begin();
6058 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
6059 (*bfields
)[i
].btype
= (use_placeholder
6060 ? p
->type()->get_backend_placeholder(gogo
)
6061 : p
->type()->get_backend(gogo
));
6062 (*bfields
)[i
].location
= p
->location();
6064 go_assert(i
== fields
->size());
6067 // Get the backend representation for a struct type.
6070 Struct_type::do_get_backend(Gogo
* gogo
)
6072 std::vector
<Backend::Btyped_identifier
> bfields
;
6073 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
6074 return gogo
->backend()->struct_type(bfields
);
6077 // Finish the backend representation of the fields of a struct.
6080 Struct_type::finish_backend_fields(Gogo
* gogo
)
6082 const Struct_field_list
* fields
= this->fields_
;
6085 for (Struct_field_list::const_iterator p
= fields
->begin();
6088 p
->type()->get_backend(gogo
);
6092 // The type of a struct type descriptor.
6095 Struct_type::make_struct_type_descriptor_type()
6100 Type
* tdt
= Type::make_type_descriptor_type();
6101 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6103 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6104 Type
* string_type
= Type::lookup_string_type();
6105 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6108 Type::make_builtin_struct_type(5,
6109 "name", pointer_string_type
,
6110 "pkgPath", pointer_string_type
,
6112 "tag", pointer_string_type
,
6113 "offsetAnon", uintptr_type
);
6114 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
6116 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
6118 Struct_type
* s
= Type::make_builtin_struct_type(2,
6120 "fields", slice_type
);
6122 ret
= Type::make_builtin_named_type("StructType", s
);
6128 // Build a type descriptor for a struct type.
6131 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6133 Location bloc
= Linemap::predeclared_location();
6135 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
6137 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
6139 Expression_list
* vals
= new Expression_list();
6142 const Methods
* methods
= this->methods();
6143 // A named struct should not have methods--the methods should attach
6144 // to the named type.
6145 go_assert(methods
== NULL
|| name
== NULL
);
6147 Struct_field_list::const_iterator ps
= fields
->begin();
6148 go_assert(ps
->is_field_name("_type"));
6149 vals
->push_back(this->type_descriptor_constructor(gogo
,
6150 RUNTIME_TYPE_KIND_STRUCT
,
6151 name
, methods
, true));
6154 go_assert(ps
->is_field_name("fields"));
6156 Expression_list
* elements
= new Expression_list();
6157 elements
->reserve(this->fields_
->size());
6158 Type
* element_type
= ps
->type()->array_type()->element_type();
6159 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
6160 pf
!= this->fields_
->end();
6163 const Struct_field_list
* f
= element_type
->struct_type()->fields();
6165 Expression_list
* fvals
= new Expression_list();
6168 Struct_field_list::const_iterator q
= f
->begin();
6169 go_assert(q
->is_field_name("name"));
6170 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
6171 Expression
* s
= Expression::make_string(n
, bloc
);
6172 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6175 go_assert(q
->is_field_name("pkgPath"));
6176 bool is_embedded_builtin
= pf
->is_embedded_builtin(gogo
);
6177 if (!Gogo::is_hidden_name(pf
->field_name()) && !is_embedded_builtin
)
6178 fvals
->push_back(Expression::make_nil(bloc
));
6182 if (is_embedded_builtin
)
6183 n
= gogo
->package_name();
6185 n
= Gogo::hidden_name_pkgpath(pf
->field_name());
6186 Expression
* s
= Expression::make_string(n
, bloc
);
6187 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6191 go_assert(q
->is_field_name("typ"));
6192 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
6195 go_assert(q
->is_field_name("tag"));
6197 fvals
->push_back(Expression::make_nil(bloc
));
6200 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
6201 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6205 go_assert(q
->is_field_name("offsetAnon"));
6206 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6207 Expression
* o
= Expression::make_struct_field_offset(this, &*pf
);
6208 Expression
* one
= Expression::make_integer_ul(1, uintptr_type
, bloc
);
6209 o
= Expression::make_binary(OPERATOR_LSHIFT
, o
, one
, bloc
);
6210 int av
= pf
->is_anonymous() ? 1 : 0;
6211 Expression
* anon
= Expression::make_integer_ul(av
, uintptr_type
, bloc
);
6212 o
= Expression::make_binary(OPERATOR_OR
, o
, anon
, bloc
);
6213 fvals
->push_back(o
);
6215 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
6217 elements
->push_back(v
);
6220 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
6223 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
6226 // Write the hash function for a struct which can not use the identity
6230 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
6231 Function_type
* hash_fntype
,
6232 Function_type
* equal_fntype
)
6234 Location bloc
= Linemap::predeclared_location();
6236 // The pointer to the struct that we are going to hash. This is an
6237 // argument to the hash function we are implementing here.
6238 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
6239 go_assert(key_arg
!= NULL
);
6240 Type
* key_arg_type
= key_arg
->var_value()->type();
6242 // The seed argument to the hash function.
6243 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
6244 go_assert(seed_arg
!= NULL
);
6246 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6248 // Make a temporary to hold the return value, initialized to the seed.
6249 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
6250 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
6252 gogo
->add_statement(retval
);
6254 // Make a temporary to hold the key as a uintptr.
6255 ref
= Expression::make_var_reference(key_arg
, bloc
);
6256 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
6257 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
6259 gogo
->add_statement(key
);
6261 // Loop over the struct fields.
6262 const Struct_field_list
* fields
= this->fields_
;
6263 for (Struct_field_list::const_iterator pf
= fields
->begin();
6264 pf
!= fields
->end();
6267 if (Gogo::is_sink_name(pf
->field_name()))
6270 // Get a pointer to the value of this field.
6271 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
6272 ref
= Expression::make_temporary_reference(key
, bloc
);
6273 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
6275 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
6277 // Get the hash function to use for the type of this field.
6278 Named_object
* hash_fn
;
6279 Named_object
* equal_fn
;
6280 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
6281 equal_fntype
, &hash_fn
, &equal_fn
);
6283 // Call the hash function for the field, passing retval as the seed.
6284 ref
= Expression::make_temporary_reference(retval
, bloc
);
6285 Expression_list
* args
= new Expression_list();
6286 args
->push_back(subkey
);
6287 args
->push_back(ref
);
6288 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
6289 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
6291 // Set retval to the result.
6292 Temporary_reference_expression
* tref
=
6293 Expression::make_temporary_reference(retval
, bloc
);
6294 tref
->set_is_lvalue();
6295 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
6296 gogo
->add_statement(s
);
6299 // Return retval to the caller of the hash function.
6300 Expression_list
* vals
= new Expression_list();
6301 ref
= Expression::make_temporary_reference(retval
, bloc
);
6302 vals
->push_back(ref
);
6303 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6304 gogo
->add_statement(s
);
6307 // Write the equality function for a struct which can not use the
6308 // identity function.
6311 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
6313 Location bloc
= Linemap::predeclared_location();
6315 // The pointers to the structs we are going to compare.
6316 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
6317 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
6318 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
6320 // Build temporaries with the right types.
6321 Type
* pt
= Type::make_pointer_type(name
!= NULL
6322 ? static_cast<Type
*>(name
)
6323 : static_cast<Type
*>(this));
6325 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
6326 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6327 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
6328 gogo
->add_statement(p1
);
6330 ref
= Expression::make_var_reference(key2_arg
, bloc
);
6331 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6332 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
6333 gogo
->add_statement(p2
);
6335 const Struct_field_list
* fields
= this->fields_
;
6336 unsigned int field_index
= 0;
6337 for (Struct_field_list::const_iterator pf
= fields
->begin();
6338 pf
!= fields
->end();
6339 ++pf
, ++field_index
)
6341 if (Gogo::is_sink_name(pf
->field_name()))
6344 // Compare one field in both P1 and P2.
6345 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
6346 f1
= Expression::make_unary(OPERATOR_MULT
, f1
, bloc
);
6347 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
6349 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
6350 f2
= Expression::make_unary(OPERATOR_MULT
, f2
, bloc
);
6351 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
6353 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
6355 // If the values are not equal, return false.
6356 gogo
->start_block(bloc
);
6357 Expression_list
* vals
= new Expression_list();
6358 vals
->push_back(Expression::make_boolean(false, bloc
));
6359 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6360 gogo
->add_statement(s
);
6361 Block
* then_block
= gogo
->finish_block(bloc
);
6363 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
6364 gogo
->add_statement(s
);
6367 // All the fields are equal, so return true.
6368 Expression_list
* vals
= new Expression_list();
6369 vals
->push_back(Expression::make_boolean(true, bloc
));
6370 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6371 gogo
->add_statement(s
);
6374 // Reflection string.
6377 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6379 ret
->append("struct {");
6381 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
6382 p
!= this->fields_
->end();
6385 if (p
!= this->fields_
->begin())
6386 ret
->push_back(';');
6387 ret
->push_back(' ');
6388 if (p
->is_anonymous())
6389 ret
->push_back('?');
6391 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
6392 ret
->push_back(' ');
6393 if (p
->is_anonymous()
6394 && p
->type()->named_type() != NULL
6395 && p
->type()->named_type()->is_alias())
6396 p
->type()->named_type()->append_reflection_type_name(gogo
, true, ret
);
6398 this->append_reflection(p
->type(), gogo
, ret
);
6402 const std::string
& tag(p
->tag());
6404 for (std::string::const_iterator p
= tag
.begin();
6409 ret
->append("\\x00");
6410 else if (*p
== '\n')
6412 else if (*p
== '\t')
6415 ret
->append("\\\"");
6416 else if (*p
== '\\')
6417 ret
->append("\\\\");
6421 ret
->push_back('"');
6425 if (!this->fields_
->empty())
6426 ret
->push_back(' ');
6428 ret
->push_back('}');
6431 // If the offset of field INDEX in the backend implementation can be
6432 // determined, set *POFFSET to the offset in bytes and return true.
6433 // Otherwise, return false.
6436 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
6439 if (!this->is_backend_type_size_known(gogo
))
6441 Btype
* bt
= this->get_backend_placeholder(gogo
);
6442 *poffset
= gogo
->backend()->type_field_offset(bt
, index
);
6449 Struct_type::do_export(Export
* exp
) const
6451 exp
->write_c_string("struct { ");
6452 const Struct_field_list
* fields
= this->fields_
;
6453 go_assert(fields
!= NULL
);
6454 for (Struct_field_list::const_iterator p
= fields
->begin();
6458 if (p
->is_anonymous())
6459 exp
->write_string("? ");
6462 exp
->write_string(p
->field_name());
6463 exp
->write_c_string(" ");
6465 exp
->write_type(p
->type());
6469 exp
->write_c_string(" ");
6471 Expression::make_string(p
->tag(), Linemap::predeclared_location());
6472 expr
->export_expression(exp
);
6476 exp
->write_c_string("; ");
6478 exp
->write_c_string("}");
6484 Struct_type::do_import(Import
* imp
)
6486 imp
->require_c_string("struct { ");
6487 Struct_field_list
* fields
= new Struct_field_list
;
6488 if (imp
->peek_char() != '}')
6493 if (imp
->match_c_string("? "))
6497 name
= imp
->read_identifier();
6498 imp
->require_c_string(" ");
6500 Type
* ftype
= imp
->read_type();
6502 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
6503 sf
.set_is_imported();
6505 if (imp
->peek_char() == ' ')
6508 Expression
* expr
= Expression::import_expression(imp
);
6509 String_expression
* sexpr
= expr
->string_expression();
6510 go_assert(sexpr
!= NULL
);
6511 sf
.set_tag(sexpr
->val());
6515 imp
->require_c_string("; ");
6516 fields
->push_back(sf
);
6517 if (imp
->peek_char() == '}')
6521 imp
->require_c_string("}");
6523 return Type::make_struct_type(fields
, imp
->location());
6526 // Whether we can write this struct type to a C header file.
6527 // We can't if any of the fields are structs defined in a different package.
6530 Struct_type::can_write_to_c_header(
6531 std::vector
<const Named_object
*>* requires
,
6532 std::vector
<const Named_object
*>* declare
) const
6534 const Struct_field_list
* fields
= this->fields_
;
6535 if (fields
== NULL
|| fields
->empty())
6538 for (Struct_field_list::const_iterator p
= fields
->begin();
6542 if (p
->is_anonymous())
6544 if (!this->can_write_type_to_c_header(p
->type(), requires
, declare
))
6546 if (Gogo::message_name(p
->field_name()) == "_")
6554 // Whether we can write the type T to a C header file.
6557 Struct_type::can_write_type_to_c_header(
6559 std::vector
<const Named_object
*>* requires
,
6560 std::vector
<const Named_object
*>* declare
) const
6563 switch (t
->classification())
6578 case TYPE_INTERFACE
:
6582 // Don't try to handle a pointer to an array.
6583 if (t
->points_to()->array_type() != NULL
6584 && !t
->points_to()->is_slice_type())
6587 if (t
->points_to()->named_type() != NULL
6588 && t
->points_to()->struct_type() != NULL
)
6589 declare
->push_back(t
->points_to()->named_type()->named_object());
6593 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6596 if (t
->is_slice_type())
6598 return this->can_write_type_to_c_header(t
->array_type()->element_type(),
6603 const Named_object
* no
= t
->named_type()->named_object();
6604 if (no
->package() != NULL
)
6606 if (t
->is_unsafe_pointer_type())
6610 if (t
->struct_type() != NULL
)
6612 requires
->push_back(no
);
6613 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6615 return this->can_write_type_to_c_header(t
->base(), requires
, declare
);
6618 case TYPE_CALL_MULTIPLE_RESULT
:
6626 // Write this struct to a C header file.
6629 Struct_type::write_to_c_header(std::ostream
& os
) const
6631 const Struct_field_list
* fields
= this->fields_
;
6632 for (Struct_field_list::const_iterator p
= fields
->begin();
6637 this->write_field_to_c_header(os
, p
->field_name(), p
->type());
6638 os
<< ';' << std::endl
;
6642 // Write the type of a struct field to a C header file.
6645 Struct_type::write_field_to_c_header(std::ostream
& os
, const std::string
& name
,
6646 const Type
*t
) const
6648 bool print_name
= true;
6650 switch (t
->classification())
6662 const Integer_type
* it
= t
->integer_type();
6663 if (it
->is_unsigned())
6665 os
<< "int" << it
->bits() << "_t";
6670 switch (t
->float_type()->bits())
6684 switch (t
->complex_type()->bits())
6687 os
<< "float _Complex";
6690 os
<< "double _Complex";
6707 std::vector
<const Named_object
*> requires
;
6708 std::vector
<const Named_object
*> declare
;
6709 if (!this->can_write_type_to_c_header(t
->points_to(), &requires
,
6714 this->write_field_to_c_header(os
, "", t
->points_to());
6728 case TYPE_INTERFACE
:
6729 if (t
->interface_type()->is_empty())
6736 os
<< "struct {" << std::endl
;
6737 t
->struct_type()->write_to_c_header(os
);
6742 if (t
->is_slice_type())
6746 const Type
*ele
= t
;
6747 std::vector
<const Type
*> array_types
;
6748 while (ele
->array_type() != NULL
&& !ele
->is_slice_type())
6750 array_types
.push_back(ele
);
6751 ele
= ele
->array_type()->element_type();
6753 this->write_field_to_c_header(os
, "", ele
);
6754 os
<< ' ' << Gogo::message_name(name
);
6756 while (!array_types
.empty())
6758 ele
= array_types
.back();
6759 array_types
.pop_back();
6761 Numeric_constant nc
;
6762 if (!ele
->array_type()->length()->numeric_constant_value(&nc
))
6765 if (!nc
.to_int(&val
))
6767 char* s
= mpz_get_str(NULL
, 10, val
);
6778 const Named_object
* no
= t
->named_type()->named_object();
6779 if (t
->struct_type() != NULL
)
6780 os
<< "struct " << no
->message_name();
6781 else if (t
->is_unsafe_pointer_type())
6783 else if (t
== Type::lookup_integer_type("uintptr"))
6787 this->write_field_to_c_header(os
, name
, t
->base());
6795 case TYPE_CALL_MULTIPLE_RESULT
:
6802 if (print_name
&& !name
.empty())
6803 os
<< ' ' << Gogo::message_name(name
);
6806 // Make a struct type.
6809 Type::make_struct_type(Struct_field_list
* fields
,
6812 return new Struct_type(fields
, location
);
6815 // Class Array_type.
6817 // Store the length of an array as an int64_t into *PLEN. Return
6818 // false if the length can not be determined. This will assert if
6819 // called for a slice.
6822 Array_type::int_length(int64_t* plen
)
6824 go_assert(this->length_
!= NULL
);
6825 Numeric_constant nc
;
6826 if (!this->length_
->numeric_constant_value(&nc
))
6828 return nc
.to_memory_size(plen
);
6831 // Whether two array types are identical.
6834 Array_type::is_identical(const Array_type
* t
, Cmp_tags cmp_tags
,
6835 bool errors_are_identical
) const
6837 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
6838 cmp_tags
, errors_are_identical
, NULL
))
6841 if (this->is_array_incomparable_
!= t
->is_array_incomparable_
)
6844 Expression
* l1
= this->length();
6845 Expression
* l2
= t
->length();
6847 // Slices of the same element type are identical.
6848 if (l1
== NULL
&& l2
== NULL
)
6851 // Arrays of the same element type are identical if they have the
6853 if (l1
!= NULL
&& l2
!= NULL
)
6858 // Try to determine the lengths. If we can't, assume the arrays
6859 // are not identical.
6861 Numeric_constant nc1
, nc2
;
6862 if (l1
->numeric_constant_value(&nc1
)
6863 && l2
->numeric_constant_value(&nc2
))
6866 if (nc1
.to_int(&v1
))
6869 if (nc2
.to_int(&v2
))
6871 ret
= mpz_cmp(v1
, v2
) == 0;
6880 // Otherwise the arrays are not identical.
6887 Array_type::do_traverse(Traverse
* traverse
)
6889 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
6890 return TRAVERSE_EXIT
;
6891 if (this->length_
!= NULL
6892 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
6893 return TRAVERSE_EXIT
;
6894 return TRAVERSE_CONTINUE
;
6897 // Check that the length is valid.
6900 Array_type::verify_length()
6902 if (this->length_
== NULL
)
6905 Type_context
context(Type::lookup_integer_type("int"), false);
6906 this->length_
->determine_type(&context
);
6908 if (!this->length_
->is_constant())
6910 go_error_at(this->length_
->location(), "array bound is not constant");
6914 Numeric_constant nc
;
6915 if (!this->length_
->numeric_constant_value(&nc
))
6917 if (this->length_
->type()->integer_type() != NULL
6918 || this->length_
->type()->float_type() != NULL
)
6919 go_error_at(this->length_
->location(), "array bound is not constant");
6921 go_error_at(this->length_
->location(), "array bound is not numeric");
6925 Type
* int_type
= Type::lookup_integer_type("int");
6926 unsigned int tbits
= int_type
->integer_type()->bits();
6928 switch (nc
.to_unsigned_long(&val
))
6930 case Numeric_constant::NC_UL_VALID
:
6931 if (sizeof(val
) >= tbits
/ 8 && val
>> (tbits
- 1) != 0)
6933 go_error_at(this->length_
->location(), "array bound overflows");
6937 case Numeric_constant::NC_UL_NOTINT
:
6938 go_error_at(this->length_
->location(), "array bound truncated to integer");
6940 case Numeric_constant::NC_UL_NEGATIVE
:
6941 go_error_at(this->length_
->location(), "negative array bound");
6943 case Numeric_constant::NC_UL_BIG
:
6946 if (!nc
.to_int(&val
))
6948 unsigned int bits
= mpz_sizeinbase(val
, 2);
6952 go_error_at(this->length_
->location(), "array bound overflows");
6967 Array_type::do_verify()
6969 if (this->element_type()->is_error_type())
6971 if (!this->verify_length())
6972 this->length_
= Expression::make_error(this->length_
->location());
6976 // Whether the type contains pointers. This is always true for a
6977 // slice. For an array it is true if the element type has pointers
6978 // and the length is greater than zero.
6981 Array_type::do_has_pointer() const
6983 if (this->length_
== NULL
)
6985 if (!this->element_type_
->has_pointer())
6988 Numeric_constant nc
;
6989 if (!this->length_
->numeric_constant_value(&nc
))
6991 // Error reported elsewhere.
6996 switch (nc
.to_unsigned_long(&val
))
6998 case Numeric_constant::NC_UL_VALID
:
7000 case Numeric_constant::NC_UL_BIG
:
7003 // Error reported elsewhere.
7008 // Whether we can use memcmp to compare this array.
7011 Array_type::do_compare_is_identity(Gogo
* gogo
)
7013 if (this->length_
== NULL
)
7016 // Check for [...], which indicates that this is not a real type.
7017 if (this->length_
->is_nil_expression())
7020 if (!this->element_type_
->compare_is_identity(gogo
))
7023 // If there is any padding, then we can't use memcmp.
7026 if (!this->element_type_
->backend_type_size(gogo
, &size
)
7027 || !this->element_type_
->backend_type_align(gogo
, &align
))
7029 if ((size
& (align
- 1)) != 0)
7035 // Array type hash code.
7038 Array_type::do_hash_for_method(Gogo
* gogo
) const
7042 // There is no very convenient way to get a hash code for the
7044 ret
= this->element_type_
->hash_for_method(gogo
) + 1;
7045 if (this->is_array_incomparable_
)
7050 // Write the hash function for an array which can not use the identify
7054 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
7055 Function_type
* hash_fntype
,
7056 Function_type
* equal_fntype
)
7058 Location bloc
= Linemap::predeclared_location();
7060 // The pointer to the array that we are going to hash. This is an
7061 // argument to the hash function we are implementing here.
7062 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
7063 go_assert(key_arg
!= NULL
);
7064 Type
* key_arg_type
= key_arg
->var_value()->type();
7066 // The seed argument to the hash function.
7067 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
7068 go_assert(seed_arg
!= NULL
);
7070 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7072 // Make a temporary to hold the return value, initialized to the seed.
7073 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
7074 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
7076 gogo
->add_statement(retval
);
7078 // Make a temporary to hold the key as a uintptr.
7079 ref
= Expression::make_var_reference(key_arg
, bloc
);
7080 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
7081 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
7083 gogo
->add_statement(key
);
7085 // Loop over the array elements.
7087 Type
* int_type
= Type::lookup_integer_type("int");
7088 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7089 gogo
->add_statement(index
);
7091 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7092 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
7093 Type
* pt
= Type::make_pointer_type(name
!= NULL
7094 ? static_cast<Type
*>(name
)
7095 : static_cast<Type
*>(this));
7096 aref
= Expression::make_cast(pt
, aref
, bloc
);
7097 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7102 gogo
->start_block(bloc
);
7104 // Get the hash function for the element type.
7105 Named_object
* hash_fn
;
7106 Named_object
* equal_fn
;
7107 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
7108 hash_fntype
, equal_fntype
, &hash_fn
,
7111 // Get a pointer to this element in the loop.
7112 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
7113 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
7115 // Get the size of each element.
7116 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
7117 Expression::TYPE_INFO_SIZE
);
7119 // Get the hash of this element, passing retval as the seed.
7120 ref
= Expression::make_temporary_reference(retval
, bloc
);
7121 Expression_list
* args
= new Expression_list();
7122 args
->push_back(subkey
);
7123 args
->push_back(ref
);
7124 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
7125 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
7127 // Set retval to the result.
7128 Temporary_reference_expression
* tref
=
7129 Expression::make_temporary_reference(retval
, bloc
);
7130 tref
->set_is_lvalue();
7131 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
7132 gogo
->add_statement(s
);
7134 // Increase the element pointer.
7135 tref
= Expression::make_temporary_reference(key
, bloc
);
7136 tref
->set_is_lvalue();
7137 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
7139 Block
* statements
= gogo
->finish_block(bloc
);
7141 for_range
->add_statements(statements
);
7142 gogo
->add_statement(for_range
);
7144 // Return retval to the caller of the hash function.
7145 Expression_list
* vals
= new Expression_list();
7146 ref
= Expression::make_temporary_reference(retval
, bloc
);
7147 vals
->push_back(ref
);
7148 s
= Statement::make_return_statement(vals
, bloc
);
7149 gogo
->add_statement(s
);
7152 // Write the equality function for an array which can not use the
7153 // identity function.
7156 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
7158 Location bloc
= Linemap::predeclared_location();
7160 // The pointers to the arrays we are going to compare.
7161 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
7162 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
7163 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
7165 // Build temporaries for the keys with the right types.
7166 Type
* pt
= Type::make_pointer_type(name
!= NULL
7167 ? static_cast<Type
*>(name
)
7168 : static_cast<Type
*>(this));
7170 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
7171 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7172 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
7173 gogo
->add_statement(p1
);
7175 ref
= Expression::make_var_reference(key2_arg
, bloc
);
7176 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7177 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
7178 gogo
->add_statement(p2
);
7180 // Loop over the array elements.
7182 Type
* int_type
= Type::lookup_integer_type("int");
7183 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7184 gogo
->add_statement(index
);
7186 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7187 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
7188 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7193 gogo
->start_block(bloc
);
7195 // Compare element in P1 and P2.
7196 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
7197 e1
= Expression::make_unary(OPERATOR_MULT
, e1
, bloc
);
7198 ref
= Expression::make_temporary_reference(index
, bloc
);
7199 e1
= Expression::make_array_index(e1
, ref
, NULL
, NULL
, bloc
);
7201 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
7202 e2
= Expression::make_unary(OPERATOR_MULT
, e2
, bloc
);
7203 ref
= Expression::make_temporary_reference(index
, bloc
);
7204 e2
= Expression::make_array_index(e2
, ref
, NULL
, NULL
, bloc
);
7206 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
7208 // If the elements are not equal, return false.
7209 gogo
->start_block(bloc
);
7210 Expression_list
* vals
= new Expression_list();
7211 vals
->push_back(Expression::make_boolean(false, bloc
));
7212 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
7213 gogo
->add_statement(s
);
7214 Block
* then_block
= gogo
->finish_block(bloc
);
7216 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
7217 gogo
->add_statement(s
);
7219 Block
* statements
= gogo
->finish_block(bloc
);
7221 for_range
->add_statements(statements
);
7222 gogo
->add_statement(for_range
);
7224 // All the elements are equal, so return true.
7225 vals
= new Expression_list();
7226 vals
->push_back(Expression::make_boolean(true, bloc
));
7227 s
= Statement::make_return_statement(vals
, bloc
);
7228 gogo
->add_statement(s
);
7231 // Get the backend representation of the fields of a slice. This is
7232 // not declared in types.h so that types.h doesn't have to #include
7235 // We use int for the count and capacity fields. This matches 6g.
7236 // The language more or less assumes that we can't allocate space of a
7237 // size which does not fit in int.
7240 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
7241 std::vector
<Backend::Btyped_identifier
>* bfields
)
7245 Type
* pet
= Type::make_pointer_type(type
->element_type());
7246 Btype
* pbet
= (use_placeholder
7247 ? pet
->get_backend_placeholder(gogo
)
7248 : pet
->get_backend(gogo
));
7249 Location ploc
= Linemap::predeclared_location();
7251 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
7252 p
->name
= "__values";
7256 Type
* int_type
= Type::lookup_integer_type("int");
7259 p
->name
= "__count";
7260 p
->btype
= int_type
->get_backend(gogo
);
7264 p
->name
= "__capacity";
7265 p
->btype
= int_type
->get_backend(gogo
);
7269 // Get the backend representation for the type of this array. A fixed array is
7270 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7271 // just like an array in C. An open array is a struct with three
7272 // fields: a data pointer, the length, and the capacity.
7275 Array_type::do_get_backend(Gogo
* gogo
)
7277 if (this->length_
== NULL
)
7279 std::vector
<Backend::Btyped_identifier
> bfields
;
7280 get_backend_slice_fields(gogo
, this, false, &bfields
);
7281 return gogo
->backend()->struct_type(bfields
);
7285 Btype
* element
= this->get_backend_element(gogo
, false);
7286 Bexpression
* len
= this->get_backend_length(gogo
);
7287 return gogo
->backend()->array_type(element
, len
);
7291 // Return the backend representation of the element type.
7294 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
7296 if (use_placeholder
)
7297 return this->element_type_
->get_backend_placeholder(gogo
);
7299 return this->element_type_
->get_backend(gogo
);
7302 // Return the backend representation of the length. The length may be
7303 // computed using a function call, so we must only evaluate it once.
7306 Array_type::get_backend_length(Gogo
* gogo
)
7308 go_assert(this->length_
!= NULL
);
7309 if (this->blength_
== NULL
)
7311 if (this->length_
->is_error_expression())
7313 this->blength_
= gogo
->backend()->error_expression();
7314 return this->blength_
;
7316 Numeric_constant nc
;
7318 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
7320 if (mpz_sgn(val
) < 0)
7322 this->blength_
= gogo
->backend()->error_expression();
7323 return this->blength_
;
7325 Type
* t
= nc
.type();
7327 t
= Type::lookup_integer_type("int");
7328 else if (t
->is_abstract())
7329 t
= t
->make_non_abstract_type();
7330 Btype
* btype
= t
->get_backend(gogo
);
7332 gogo
->backend()->integer_constant_expression(btype
, val
);
7337 // Make up a translation context for the array length
7338 // expression. FIXME: This won't work in general.
7339 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7340 this->blength_
= this->length_
->get_backend(&context
);
7342 Btype
* ibtype
= Type::lookup_integer_type("int")->get_backend(gogo
);
7344 gogo
->backend()->convert_expression(ibtype
, this->blength_
,
7345 this->length_
->location());
7348 return this->blength_
;
7351 // Finish backend representation of the array.
7354 Array_type::finish_backend_element(Gogo
* gogo
)
7356 Type
* et
= this->array_type()->element_type();
7357 et
->get_backend(gogo
);
7358 if (this->is_slice_type())
7360 // This relies on the fact that we always use the same
7361 // structure for a pointer to any given type.
7362 Type
* pet
= Type::make_pointer_type(et
);
7363 pet
->get_backend(gogo
);
7367 // Return an expression for a pointer to the values in ARRAY.
7370 Array_type::get_value_pointer(Gogo
*, Expression
* array
, bool is_lvalue
) const
7372 if (this->length() != NULL
)
7375 go_assert(array
->type()->array_type() != NULL
);
7376 Type
* etype
= array
->type()->array_type()->element_type();
7377 array
= Expression::make_unary(OPERATOR_AND
, array
, array
->location());
7378 return Expression::make_cast(Type::make_pointer_type(etype
), array
,
7386 Temporary_reference_expression
* tref
=
7387 array
->temporary_reference_expression();
7388 Var_expression
* ve
= array
->var_expression();
7391 tref
= tref
->copy()->temporary_reference_expression();
7392 tref
->set_is_lvalue();
7395 else if (ve
!= NULL
)
7397 ve
= new Var_expression(ve
->named_object(), ve
->location());
7398 ve
->set_in_lvalue_pos();
7403 return Expression::make_slice_info(array
,
7404 Expression::SLICE_INFO_VALUE_POINTER
,
7408 // Return an expression for the length of the array ARRAY which has this
7412 Array_type::get_length(Gogo
*, Expression
* array
) const
7414 if (this->length_
!= NULL
)
7415 return this->length_
;
7417 // This is a slice. We need to read the length field.
7418 return Expression::make_slice_info(array
, Expression::SLICE_INFO_LENGTH
,
7422 // Return an expression for the capacity of the array ARRAY which has this
7426 Array_type::get_capacity(Gogo
*, Expression
* array
) const
7428 if (this->length_
!= NULL
)
7429 return this->length_
;
7431 // This is a slice. We need to read the capacity field.
7432 return Expression::make_slice_info(array
, Expression::SLICE_INFO_CAPACITY
,
7439 Array_type::do_export(Export
* exp
) const
7441 exp
->write_c_string("[");
7442 if (this->length_
!= NULL
)
7443 this->length_
->export_expression(exp
);
7444 exp
->write_c_string("] ");
7445 exp
->write_type(this->element_type_
);
7451 Array_type::do_import(Import
* imp
)
7453 imp
->require_c_string("[");
7455 if (imp
->peek_char() == ']')
7458 length
= Expression::import_expression(imp
);
7459 imp
->require_c_string("] ");
7460 Type
* element_type
= imp
->read_type();
7461 return Type::make_array_type(element_type
, length
);
7464 // The type of an array type descriptor.
7467 Array_type::make_array_type_descriptor_type()
7472 Type
* tdt
= Type::make_type_descriptor_type();
7473 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7475 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7478 Type::make_builtin_struct_type(4,
7482 "len", uintptr_type
);
7484 ret
= Type::make_builtin_named_type("ArrayType", sf
);
7490 // The type of an slice type descriptor.
7493 Array_type::make_slice_type_descriptor_type()
7498 Type
* tdt
= Type::make_type_descriptor_type();
7499 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7502 Type::make_builtin_struct_type(2,
7506 ret
= Type::make_builtin_named_type("SliceType", sf
);
7512 // Build a type descriptor for an array/slice type.
7515 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7517 if (this->length_
!= NULL
)
7518 return this->array_type_descriptor(gogo
, name
);
7520 return this->slice_type_descriptor(gogo
, name
);
7523 // Build a type descriptor for an array type.
7526 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7528 Location bloc
= Linemap::predeclared_location();
7530 Type
* atdt
= Array_type::make_array_type_descriptor_type();
7532 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
7534 Expression_list
* vals
= new Expression_list();
7537 Struct_field_list::const_iterator p
= fields
->begin();
7538 go_assert(p
->is_field_name("_type"));
7539 vals
->push_back(this->type_descriptor_constructor(gogo
,
7540 RUNTIME_TYPE_KIND_ARRAY
,
7544 go_assert(p
->is_field_name("elem"));
7545 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7548 go_assert(p
->is_field_name("slice"));
7549 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
7550 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
7553 go_assert(p
->is_field_name("len"));
7554 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
7557 go_assert(p
== fields
->end());
7559 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
7562 // Build a type descriptor for a slice type.
7565 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7567 Location bloc
= Linemap::predeclared_location();
7569 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
7571 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
7573 Expression_list
* vals
= new Expression_list();
7576 Struct_field_list::const_iterator p
= fields
->begin();
7577 go_assert(p
->is_field_name("_type"));
7578 vals
->push_back(this->type_descriptor_constructor(gogo
,
7579 RUNTIME_TYPE_KIND_SLICE
,
7583 go_assert(p
->is_field_name("elem"));
7584 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7587 go_assert(p
== fields
->end());
7589 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
7592 // Reflection string.
7595 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7597 ret
->push_back('[');
7598 if (this->length_
!= NULL
)
7600 Numeric_constant nc
;
7601 if (!this->length_
->numeric_constant_value(&nc
))
7603 go_assert(saw_errors());
7607 if (!nc
.to_int(&val
))
7609 go_assert(saw_errors());
7612 char* s
= mpz_get_str(NULL
, 10, val
);
7617 ret
->push_back(']');
7619 this->append_reflection(this->element_type_
, gogo
, ret
);
7622 // Make an array type.
7625 Type::make_array_type(Type
* element_type
, Expression
* length
)
7627 return new Array_type(element_type
, length
);
7632 Named_object
* Map_type::zero_value
;
7633 int64_t Map_type::zero_value_size
;
7634 int64_t Map_type::zero_value_align
;
7636 // If this map requires the "fat" functions, return the pointer to
7637 // pass as the zero value to those functions. Otherwise, in the
7638 // normal case, return NULL. The map requires the "fat" functions if
7639 // the value size is larger than max_zero_size bytes. max_zero_size
7640 // must match maxZero in libgo/go/runtime/hashmap.go.
7643 Map_type::fat_zero_value(Gogo
* gogo
)
7646 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
7648 go_assert(saw_errors());
7651 if (valsize
<= Map_type::max_zero_size
)
7654 if (Map_type::zero_value_size
< valsize
)
7655 Map_type::zero_value_size
= valsize
;
7658 if (!this->val_type_
->backend_type_align(gogo
, &valalign
))
7660 go_assert(saw_errors());
7664 if (Map_type::zero_value_align
< valalign
)
7665 Map_type::zero_value_align
= valalign
;
7667 Location bloc
= Linemap::predeclared_location();
7669 if (Map_type::zero_value
== NULL
)
7671 // The final type will be set in backend_zero_value.
7672 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7673 Expression
* size
= Expression::make_integer_ul(0, NULL
, bloc
);
7674 Array_type
* array_type
= Type::make_array_type(uint8_type
, size
);
7675 array_type
->set_is_array_incomparable();
7676 Variable
* var
= new Variable(array_type
, NULL
, true, false, false, bloc
);
7677 std::string name
= gogo
->map_zero_value_name();
7678 Map_type::zero_value
= Named_object::make_variable(name
, NULL
, var
);
7681 Expression
* z
= Expression::make_var_reference(Map_type::zero_value
, bloc
);
7682 z
= Expression::make_unary(OPERATOR_AND
, z
, bloc
);
7683 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
7684 z
= Expression::make_cast(unsafe_ptr_type
, z
, bloc
);
7688 // Return whether VAR is the map zero value.
7691 Map_type::is_zero_value(Variable
* var
)
7693 return (Map_type::zero_value
!= NULL
7694 && Map_type::zero_value
->var_value() == var
);
7697 // Return the backend representation for the zero value.
7700 Map_type::backend_zero_value(Gogo
* gogo
)
7702 Location bloc
= Linemap::predeclared_location();
7704 go_assert(Map_type::zero_value
!= NULL
);
7706 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7707 Btype
* buint8_type
= uint8_type
->get_backend(gogo
);
7709 Type
* int_type
= Type::lookup_integer_type("int");
7711 Expression
* e
= Expression::make_integer_int64(Map_type::zero_value_size
,
7713 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7714 Bexpression
* blength
= e
->get_backend(&context
);
7716 Btype
* barray_type
= gogo
->backend()->array_type(buint8_type
, blength
);
7718 std::string zname
= Map_type::zero_value
->name();
7719 std::string
asm_name(go_selectively_encode_id(zname
));
7721 gogo
->backend()->implicit_variable(zname
, asm_name
,
7722 barray_type
, false, true, true,
7723 Map_type::zero_value_align
);
7724 gogo
->backend()->implicit_variable_set_init(zvar
, zname
, barray_type
,
7725 false, true, true, NULL
);
7732 Map_type::do_traverse(Traverse
* traverse
)
7734 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
7735 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
7736 return TRAVERSE_EXIT
;
7737 return TRAVERSE_CONTINUE
;
7740 // Check that the map type is OK.
7743 Map_type::do_verify()
7745 // The runtime support uses "map[void]void".
7746 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
7747 go_error_at(this->location_
, "invalid map key type");
7748 if (!this->key_type_
->in_heap())
7749 go_error_at(this->location_
, "go:notinheap map key not allowed");
7750 if (!this->val_type_
->in_heap())
7751 go_error_at(this->location_
, "go:notinheap map value not allowed");
7755 // Whether two map types are identical.
7758 Map_type::is_identical(const Map_type
* t
, Cmp_tags cmp_tags
,
7759 bool errors_are_identical
) const
7761 return (Type::are_identical_cmp_tags(this->key_type(), t
->key_type(),
7762 cmp_tags
, errors_are_identical
, NULL
)
7763 && Type::are_identical_cmp_tags(this->val_type(), t
->val_type(),
7764 cmp_tags
, errors_are_identical
,
7771 Map_type::do_hash_for_method(Gogo
* gogo
) const
7773 return (this->key_type_
->hash_for_method(gogo
)
7774 + this->val_type_
->hash_for_method(gogo
)
7778 // Get the backend representation for a map type. A map type is
7779 // represented as a pointer to a struct. The struct is hmap in
7780 // runtime/hashmap.go.
7783 Map_type::do_get_backend(Gogo
* gogo
)
7785 static Btype
* backend_map_type
;
7786 if (backend_map_type
== NULL
)
7788 std::vector
<Backend::Btyped_identifier
> bfields(9);
7790 Location bloc
= Linemap::predeclared_location();
7792 Type
* int_type
= Type::lookup_integer_type("int");
7793 bfields
[0].name
= "count";
7794 bfields
[0].btype
= int_type
->get_backend(gogo
);
7795 bfields
[0].location
= bloc
;
7797 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7798 bfields
[1].name
= "flags";
7799 bfields
[1].btype
= uint8_type
->get_backend(gogo
);
7800 bfields
[1].location
= bloc
;
7802 bfields
[2].name
= "B";
7803 bfields
[2].btype
= bfields
[1].btype
;
7804 bfields
[2].location
= bloc
;
7806 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7807 bfields
[3].name
= "noverflow";
7808 bfields
[3].btype
= uint16_type
->get_backend(gogo
);
7809 bfields
[3].location
= bloc
;
7811 Type
* uint32_type
= Type::lookup_integer_type("uint32");
7812 bfields
[4].name
= "hash0";
7813 bfields
[4].btype
= uint32_type
->get_backend(gogo
);
7814 bfields
[4].location
= bloc
;
7816 Btype
* bvt
= gogo
->backend()->void_type();
7817 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
7818 bfields
[5].name
= "buckets";
7819 bfields
[5].btype
= bpvt
;
7820 bfields
[5].location
= bloc
;
7822 bfields
[6].name
= "oldbuckets";
7823 bfields
[6].btype
= bpvt
;
7824 bfields
[6].location
= bloc
;
7826 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7827 bfields
[7].name
= "nevacuate";
7828 bfields
[7].btype
= uintptr_type
->get_backend(gogo
);
7829 bfields
[7].location
= bloc
;
7831 bfields
[8].name
= "overflow";
7832 bfields
[8].btype
= bpvt
;
7833 bfields
[8].location
= bloc
;
7835 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
7836 bt
= gogo
->backend()->named_type("runtime.hmap", bt
, bloc
);
7837 backend_map_type
= gogo
->backend()->pointer_type(bt
);
7839 return backend_map_type
;
7842 // The type of a map type descriptor.
7845 Map_type::make_map_type_descriptor_type()
7850 Type
* tdt
= Type::make_type_descriptor_type();
7851 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7852 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7853 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7854 Type
* bool_type
= Type::lookup_bool_type();
7857 Type::make_builtin_struct_type(12,
7863 "keysize", uint8_type
,
7864 "indirectkey", bool_type
,
7865 "valuesize", uint8_type
,
7866 "indirectvalue", bool_type
,
7867 "bucketsize", uint16_type
,
7868 "reflexivekey", bool_type
,
7869 "needkeyupdate", bool_type
);
7871 ret
= Type::make_builtin_named_type("MapType", sf
);
7877 // Build a type descriptor for a map type.
7880 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7882 Location bloc
= Linemap::predeclared_location();
7884 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
7885 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7886 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7889 if (!this->key_type_
->backend_type_size(gogo
, &keysize
))
7891 go_error_at(this->location_
, "error determining map key type size");
7892 return Expression::make_error(this->location_
);
7896 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
7898 go_error_at(this->location_
, "error determining map value type size");
7899 return Expression::make_error(this->location_
);
7903 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptrsize
))
7905 go_assert(saw_errors());
7906 return Expression::make_error(this->location_
);
7909 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
7910 if (bucket_type
== NULL
)
7912 go_assert(saw_errors());
7913 return Expression::make_error(this->location_
);
7917 if (!bucket_type
->backend_type_size(gogo
, &bucketsize
))
7919 go_assert(saw_errors());
7920 return Expression::make_error(this->location_
);
7923 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
7925 Expression_list
* vals
= new Expression_list();
7928 Struct_field_list::const_iterator p
= fields
->begin();
7929 go_assert(p
->is_field_name("_type"));
7930 vals
->push_back(this->type_descriptor_constructor(gogo
,
7931 RUNTIME_TYPE_KIND_MAP
,
7935 go_assert(p
->is_field_name("key"));
7936 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
7939 go_assert(p
->is_field_name("elem"));
7940 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
7943 go_assert(p
->is_field_name("bucket"));
7944 vals
->push_back(Expression::make_type_descriptor(bucket_type
, bloc
));
7947 go_assert(p
->is_field_name("hmap"));
7948 Type
* hmap_type
= this->hmap_type(bucket_type
);
7949 vals
->push_back(Expression::make_type_descriptor(hmap_type
, bloc
));
7952 go_assert(p
->is_field_name("keysize"));
7953 if (keysize
> Map_type::max_key_size
)
7954 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
7956 vals
->push_back(Expression::make_integer_int64(keysize
, uint8_type
, bloc
));
7959 go_assert(p
->is_field_name("indirectkey"));
7960 vals
->push_back(Expression::make_boolean(keysize
> Map_type::max_key_size
,
7964 go_assert(p
->is_field_name("valuesize"));
7965 if (valsize
> Map_type::max_val_size
)
7966 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
7968 vals
->push_back(Expression::make_integer_int64(valsize
, uint8_type
, bloc
));
7971 go_assert(p
->is_field_name("indirectvalue"));
7972 vals
->push_back(Expression::make_boolean(valsize
> Map_type::max_val_size
,
7976 go_assert(p
->is_field_name("bucketsize"));
7977 vals
->push_back(Expression::make_integer_int64(bucketsize
, uint16_type
,
7981 go_assert(p
->is_field_name("reflexivekey"));
7982 vals
->push_back(Expression::make_boolean(this->key_type_
->is_reflexive(),
7986 go_assert(p
->is_field_name("needkeyupdate"));
7987 vals
->push_back(Expression::make_boolean(this->key_type_
->needs_key_update(),
7991 go_assert(p
== fields
->end());
7993 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
7996 // Return the bucket type to use for a map type. This must correspond
7997 // to libgo/go/runtime/hashmap.go.
8000 Map_type::bucket_type(Gogo
* gogo
, int64_t keysize
, int64_t valsize
)
8002 if (this->bucket_type_
!= NULL
)
8003 return this->bucket_type_
;
8005 Type
* key_type
= this->key_type_
;
8006 if (keysize
> Map_type::max_key_size
)
8007 key_type
= Type::make_pointer_type(key_type
);
8009 Type
* val_type
= this->val_type_
;
8010 if (valsize
> Map_type::max_val_size
)
8011 val_type
= Type::make_pointer_type(val_type
);
8013 Expression
* bucket_size
= Expression::make_integer_ul(Map_type::bucket_size
,
8014 NULL
, this->location_
);
8016 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8017 Array_type
* topbits_type
= Type::make_array_type(uint8_type
, bucket_size
);
8018 topbits_type
->set_is_array_incomparable();
8019 Array_type
* keys_type
= Type::make_array_type(key_type
, bucket_size
);
8020 keys_type
->set_is_array_incomparable();
8021 Array_type
* values_type
= Type::make_array_type(val_type
, bucket_size
);
8022 values_type
->set_is_array_incomparable();
8024 // If keys and values have no pointers, the map implementation can
8025 // keep a list of overflow pointers on the side so that buckets can
8026 // be marked as having no pointers. Arrange for the bucket to have
8027 // no pointers by changing the type of the overflow field to uintptr
8028 // in this case. See comment on the hmap.overflow field in
8029 // libgo/go/runtime/hashmap.go.
8030 Type
* overflow_type
;
8031 if (!key_type
->has_pointer() && !val_type
->has_pointer())
8032 overflow_type
= Type::lookup_integer_type("uintptr");
8035 // This should really be a pointer to the bucket type itself,
8036 // but that would require us to construct a Named_type for it to
8037 // give it a way to refer to itself. Since nothing really cares
8038 // (except perhaps for someone using a debugger) just use an
8040 overflow_type
= Type::make_pointer_type(Type::make_void_type());
8043 // Make sure the overflow pointer is the last memory in the struct,
8044 // because the runtime assumes it can use size-ptrSize as the offset
8045 // of the overflow pointer. We double-check that property below
8046 // once the offsets and size are computed.
8048 int64_t topbits_field_size
, topbits_field_align
;
8049 int64_t keys_field_size
, keys_field_align
;
8050 int64_t values_field_size
, values_field_align
;
8051 int64_t overflow_field_size
, overflow_field_align
;
8052 if (!topbits_type
->backend_type_size(gogo
, &topbits_field_size
)
8053 || !topbits_type
->backend_type_field_align(gogo
, &topbits_field_align
)
8054 || !keys_type
->backend_type_size(gogo
, &keys_field_size
)
8055 || !keys_type
->backend_type_field_align(gogo
, &keys_field_align
)
8056 || !values_type
->backend_type_size(gogo
, &values_field_size
)
8057 || !values_type
->backend_type_field_align(gogo
, &values_field_align
)
8058 || !overflow_type
->backend_type_size(gogo
, &overflow_field_size
)
8059 || !overflow_type
->backend_type_field_align(gogo
, &overflow_field_align
))
8061 go_assert(saw_errors());
8066 int64_t max_align
= std::max(std::max(topbits_field_align
, keys_field_align
),
8067 values_field_align
);
8068 if (max_align
<= overflow_field_align
)
8069 ret
= make_builtin_struct_type(4,
8070 "topbits", topbits_type
,
8072 "values", values_type
,
8073 "overflow", overflow_type
);
8076 size_t off
= topbits_field_size
;
8077 off
= ((off
+ keys_field_align
- 1)
8078 &~ static_cast<size_t>(keys_field_align
- 1));
8079 off
+= keys_field_size
;
8080 off
= ((off
+ values_field_align
- 1)
8081 &~ static_cast<size_t>(values_field_align
- 1));
8082 off
+= values_field_size
;
8084 int64_t padded_overflow_field_size
=
8085 ((overflow_field_size
+ max_align
- 1)
8086 &~ static_cast<size_t>(max_align
- 1));
8089 ovoff
= ((ovoff
+ max_align
- 1)
8090 &~ static_cast<size_t>(max_align
- 1));
8091 size_t pad
= (ovoff
- off
8092 + padded_overflow_field_size
- overflow_field_size
);
8094 Expression
* pad_expr
= Expression::make_integer_ul(pad
, NULL
,
8096 Array_type
* pad_type
= Type::make_array_type(uint8_type
, pad_expr
);
8097 pad_type
->set_is_array_incomparable();
8099 ret
= make_builtin_struct_type(5,
8100 "topbits", topbits_type
,
8102 "values", values_type
,
8104 "overflow", overflow_type
);
8107 // Verify that the overflow field is just before the end of the
8110 Btype
* btype
= ret
->get_backend(gogo
);
8111 int64_t offset
= gogo
->backend()->type_field_offset(btype
,
8112 ret
->field_count() - 1);
8114 if (!ret
->backend_type_size(gogo
, &size
))
8116 go_assert(saw_errors());
8121 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptr_size
))
8123 go_assert(saw_errors());
8127 go_assert(offset
+ ptr_size
== size
);
8129 ret
->set_is_struct_incomparable();
8131 this->bucket_type_
= ret
;
8135 // Return the hashmap type for a map type.
8138 Map_type::hmap_type(Type
* bucket_type
)
8140 if (this->hmap_type_
!= NULL
)
8141 return this->hmap_type_
;
8143 Type
* int_type
= Type::lookup_integer_type("int");
8144 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8145 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8146 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8147 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8149 Type
* ptr_bucket_type
= Type::make_pointer_type(bucket_type
);
8151 Struct_type
* ret
= make_builtin_struct_type(8,
8153 "flags", uint8_type
,
8155 "hash0", uint32_type
,
8156 "buckets", ptr_bucket_type
,
8157 "oldbuckets", ptr_bucket_type
,
8158 "nevacuate", uintptr_type
,
8159 "overflow", void_ptr_type
);
8160 ret
->set_is_struct_incomparable();
8161 this->hmap_type_
= ret
;
8165 // Return the iterator type for a map type. This is the type of the
8166 // value used when doing a range over a map.
8169 Map_type::hiter_type(Gogo
* gogo
)
8171 if (this->hiter_type_
!= NULL
)
8172 return this->hiter_type_
;
8174 int64_t keysize
, valsize
;
8175 if (!this->key_type_
->backend_type_size(gogo
, &keysize
)
8176 || !this->val_type_
->backend_type_size(gogo
, &valsize
))
8178 go_assert(saw_errors());
8182 Type
* key_ptr_type
= Type::make_pointer_type(this->key_type_
);
8183 Type
* val_ptr_type
= Type::make_pointer_type(this->val_type_
);
8184 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8185 Type
* uint8_ptr_type
= Type::make_pointer_type(uint8_type
);
8186 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8187 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
8188 Type
* bucket_ptr_type
= Type::make_pointer_type(bucket_type
);
8189 Type
* hmap_type
= this->hmap_type(bucket_type
);
8190 Type
* hmap_ptr_type
= Type::make_pointer_type(hmap_type
);
8191 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8193 Struct_type
* ret
= make_builtin_struct_type(12,
8194 "key", key_ptr_type
,
8195 "val", val_ptr_type
,
8196 "t", uint8_ptr_type
,
8198 "buckets", bucket_ptr_type
,
8199 "bptr", bucket_ptr_type
,
8200 "overflow0", void_ptr_type
,
8201 "overflow1", void_ptr_type
,
8202 "startBucket", uintptr_type
,
8203 "stuff", uintptr_type
,
8204 "bucket", uintptr_type
,
8205 "checkBucket", uintptr_type
);
8206 ret
->set_is_struct_incomparable();
8207 this->hiter_type_
= ret
;
8211 // Reflection string for a map.
8214 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8216 ret
->append("map[");
8217 this->append_reflection(this->key_type_
, gogo
, ret
);
8219 this->append_reflection(this->val_type_
, gogo
, ret
);
8222 // Export a map type.
8225 Map_type::do_export(Export
* exp
) const
8227 exp
->write_c_string("map [");
8228 exp
->write_type(this->key_type_
);
8229 exp
->write_c_string("] ");
8230 exp
->write_type(this->val_type_
);
8233 // Import a map type.
8236 Map_type::do_import(Import
* imp
)
8238 imp
->require_c_string("map [");
8239 Type
* key_type
= imp
->read_type();
8240 imp
->require_c_string("] ");
8241 Type
* val_type
= imp
->read_type();
8242 return Type::make_map_type(key_type
, val_type
, imp
->location());
8248 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
8250 return new Map_type(key_type
, val_type
, location
);
8253 // Class Channel_type.
8258 Channel_type::do_verify()
8260 // We have no location for this error, but this is not something the
8261 // ordinary user will see.
8262 if (!this->element_type_
->in_heap())
8263 go_error_at(Linemap::unknown_location(),
8264 "chan of go:notinheap type not allowed");
8271 Channel_type::do_hash_for_method(Gogo
* gogo
) const
8273 unsigned int ret
= 0;
8274 if (this->may_send_
)
8276 if (this->may_receive_
)
8278 if (this->element_type_
!= NULL
)
8279 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
8283 // Whether this type is the same as T.
8286 Channel_type::is_identical(const Channel_type
* t
, Cmp_tags cmp_tags
,
8287 bool errors_are_identical
) const
8289 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
8290 cmp_tags
, errors_are_identical
, NULL
))
8292 return (this->may_send_
== t
->may_send_
8293 && this->may_receive_
== t
->may_receive_
);
8296 // Return the backend representation for a channel type. A channel is a pointer
8297 // to a __go_channel struct. The __go_channel struct is defined in
8298 // libgo/runtime/channel.h.
8301 Channel_type::do_get_backend(Gogo
* gogo
)
8303 static Btype
* backend_channel_type
;
8304 if (backend_channel_type
== NULL
)
8306 std::vector
<Backend::Btyped_identifier
> bfields
;
8307 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
8308 bt
= gogo
->backend()->named_type("__go_channel", bt
,
8309 Linemap::predeclared_location());
8310 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
8312 return backend_channel_type
;
8315 // Build a type descriptor for a channel type.
8318 Channel_type::make_chan_type_descriptor_type()
8323 Type
* tdt
= Type::make_type_descriptor_type();
8324 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
8326 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8329 Type::make_builtin_struct_type(3,
8332 "dir", uintptr_type
);
8334 ret
= Type::make_builtin_named_type("ChanType", sf
);
8340 // Build a type descriptor for a map type.
8343 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8345 Location bloc
= Linemap::predeclared_location();
8347 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
8349 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
8351 Expression_list
* vals
= new Expression_list();
8354 Struct_field_list::const_iterator p
= fields
->begin();
8355 go_assert(p
->is_field_name("_type"));
8356 vals
->push_back(this->type_descriptor_constructor(gogo
,
8357 RUNTIME_TYPE_KIND_CHAN
,
8361 go_assert(p
->is_field_name("elem"));
8362 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
8365 go_assert(p
->is_field_name("dir"));
8366 // These bits must match the ones in libgo/runtime/go-type.h.
8368 if (this->may_receive_
)
8370 if (this->may_send_
)
8372 vals
->push_back(Expression::make_integer_ul(val
, p
->type(), bloc
));
8375 go_assert(p
== fields
->end());
8377 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
8380 // Reflection string.
8383 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8385 if (!this->may_send_
)
8387 ret
->append("chan");
8388 if (!this->may_receive_
)
8390 ret
->push_back(' ');
8391 this->append_reflection(this->element_type_
, gogo
, ret
);
8397 Channel_type::do_export(Export
* exp
) const
8399 exp
->write_c_string("chan ");
8400 if (this->may_send_
&& !this->may_receive_
)
8401 exp
->write_c_string("-< ");
8402 else if (this->may_receive_
&& !this->may_send_
)
8403 exp
->write_c_string("<- ");
8404 exp
->write_type(this->element_type_
);
8410 Channel_type::do_import(Import
* imp
)
8412 imp
->require_c_string("chan ");
8416 if (imp
->match_c_string("-< "))
8420 may_receive
= false;
8422 else if (imp
->match_c_string("<- "))
8434 Type
* element_type
= imp
->read_type();
8436 return Type::make_channel_type(may_send
, may_receive
, element_type
);
8439 // Return the type to manage a select statement with ncases case
8440 // statements. A value of this type is allocated on the stack. This
8441 // must match the type hselect in libgo/go/runtime/select.go.
8444 Channel_type::select_type(int ncases
)
8446 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
8447 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8449 static Struct_type
* scase_type
;
8450 if (scase_type
== NULL
)
8452 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8453 Type
* uint64_type
= Type::lookup_integer_type("uint64");
8455 Type::make_builtin_struct_type(7,
8456 "elem", unsafe_pointer_type
,
8457 "chan", unsafe_pointer_type
,
8459 "kind", uint16_type
,
8460 "index", uint16_type
,
8461 "receivedp", unsafe_pointer_type
,
8462 "releasetime", uint64_type
);
8463 scase_type
->set_is_struct_incomparable();
8466 Expression
* ncases_expr
=
8467 Expression::make_integer_ul(ncases
, NULL
, Linemap::predeclared_location());
8468 Array_type
* scases
= Type::make_array_type(scase_type
, ncases_expr
);
8469 scases
->set_is_array_incomparable();
8470 Array_type
* order
= Type::make_array_type(uint16_type
, ncases_expr
);
8471 order
->set_is_array_incomparable();
8474 Type::make_builtin_struct_type(7,
8475 "tcase", uint16_type
,
8476 "ncase", uint16_type
,
8477 "pollorder", unsafe_pointer_type
,
8478 "lockorder", unsafe_pointer_type
,
8480 "lockorderarr", order
,
8481 "pollorderarr", order
);
8482 ret
->set_is_struct_incomparable();
8486 // Make a new channel type.
8489 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
8491 return new Channel_type(send
, receive
, element_type
);
8494 // Class Interface_type.
8496 // Return the list of methods.
8498 const Typed_identifier_list
*
8499 Interface_type::methods() const
8501 go_assert(this->methods_are_finalized_
|| saw_errors());
8502 return this->all_methods_
;
8505 // Return the number of methods.
8508 Interface_type::method_count() const
8510 go_assert(this->methods_are_finalized_
|| saw_errors());
8511 return this->all_methods_
== NULL
? 0 : this->all_methods_
->size();
8517 Interface_type::do_traverse(Traverse
* traverse
)
8519 Typed_identifier_list
* methods
= (this->methods_are_finalized_
8520 ? this->all_methods_
8521 : this->parse_methods_
);
8522 if (methods
== NULL
)
8523 return TRAVERSE_CONTINUE
;
8524 return methods
->traverse(traverse
);
8527 // Finalize the methods. This handles interface inheritance.
8530 Interface_type::finalize_methods()
8532 if (this->methods_are_finalized_
)
8534 this->methods_are_finalized_
= true;
8535 if (this->parse_methods_
== NULL
)
8538 this->all_methods_
= new Typed_identifier_list();
8539 this->all_methods_
->reserve(this->parse_methods_
->size());
8540 Typed_identifier_list inherit
;
8541 for (Typed_identifier_list::const_iterator pm
=
8542 this->parse_methods_
->begin();
8543 pm
!= this->parse_methods_
->end();
8546 const Typed_identifier
* p
= &*pm
;
8547 if (p
->name().empty())
8548 inherit
.push_back(*p
);
8549 else if (this->find_method(p
->name()) == NULL
)
8550 this->all_methods_
->push_back(*p
);
8552 go_error_at(p
->location(), "duplicate method %qs",
8553 Gogo::message_name(p
->name()).c_str());
8556 std::vector
<Named_type
*> seen
;
8557 seen
.reserve(inherit
.size());
8558 bool issued_recursive_error
= false;
8559 while (!inherit
.empty())
8561 Type
* t
= inherit
.back().type();
8562 Location tl
= inherit
.back().location();
8565 Interface_type
* it
= t
->interface_type();
8569 go_error_at(tl
, "interface contains embedded non-interface");
8574 if (!issued_recursive_error
)
8576 go_error_at(tl
, "invalid recursive interface");
8577 issued_recursive_error
= true;
8582 Named_type
* nt
= t
->named_type();
8583 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
8585 std::vector
<Named_type
*>::const_iterator q
;
8586 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
8590 go_error_at(tl
, "inherited interface loop");
8594 if (q
!= seen
.end())
8599 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
8600 if (imethods
== NULL
)
8602 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
8603 q
!= imethods
->end();
8606 if (q
->name().empty())
8607 inherit
.push_back(*q
);
8608 else if (this->find_method(q
->name()) == NULL
)
8609 this->all_methods_
->push_back(Typed_identifier(q
->name(),
8612 go_error_at(tl
, "inherited method %qs is ambiguous",
8613 Gogo::message_name(q
->name()).c_str());
8617 if (!this->all_methods_
->empty())
8618 this->all_methods_
->sort_by_name();
8621 delete this->all_methods_
;
8622 this->all_methods_
= NULL
;
8626 // Return the method NAME, or NULL.
8628 const Typed_identifier
*
8629 Interface_type::find_method(const std::string
& name
) const
8631 go_assert(this->methods_are_finalized_
);
8632 if (this->all_methods_
== NULL
)
8634 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8635 p
!= this->all_methods_
->end();
8637 if (p
->name() == name
)
8642 // Return the method index.
8645 Interface_type::method_index(const std::string
& name
) const
8647 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
8649 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8650 p
!= this->all_methods_
->end();
8652 if (p
->name() == name
)
8657 // Return whether NAME is an unexported method, for better error
8661 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
8663 go_assert(this->methods_are_finalized_
);
8664 if (this->all_methods_
== NULL
)
8666 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8667 p
!= this->all_methods_
->end();
8670 const std::string
& method_name(p
->name());
8671 if (Gogo::is_hidden_name(method_name
)
8672 && name
== Gogo::unpack_hidden_name(method_name
)
8673 && gogo
->pack_hidden_name(name
, false) != method_name
)
8679 // Whether this type is identical with T.
8682 Interface_type::is_identical(const Interface_type
* t
, Cmp_tags cmp_tags
,
8683 bool errors_are_identical
) const
8685 // If methods have not been finalized, then we are asking whether
8686 // func redeclarations are the same. This is an error, so for
8687 // simplicity we say they are never the same.
8688 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
8691 // We require the same methods with the same types. The methods
8692 // have already been sorted.
8693 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
8694 return this->all_methods_
== t
->all_methods_
;
8696 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
8699 Assume_identical
* hold_ai
= this->assume_identical_
;
8700 Assume_identical ai
;
8704 this->assume_identical_
= &ai
;
8706 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
8707 Typed_identifier_list::const_iterator p2
;
8708 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
8710 if (p1
== this->all_methods_
->end())
8712 if (p1
->name() != p2
->name()
8713 || !Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
8714 errors_are_identical
, NULL
))
8718 this->assume_identical_
= hold_ai
;
8720 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
8723 // Return true if T1 and T2 are assumed to be identical during a type
8727 Interface_type::assume_identical(const Interface_type
* t1
,
8728 const Interface_type
* t2
) const
8730 for (Assume_identical
* p
= this->assume_identical_
;
8733 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
8738 // Whether we can assign the interface type T to this type. The types
8739 // are known to not be identical. An interface assignment is only
8740 // permitted if T is known to implement all methods in THIS.
8741 // Otherwise a type guard is required.
8744 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
8745 std::string
* reason
) const
8747 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
8748 if (this->all_methods_
== NULL
)
8750 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8751 p
!= this->all_methods_
->end();
8754 const Typed_identifier
* m
= t
->find_method(p
->name());
8760 snprintf(buf
, sizeof buf
,
8761 _("need explicit conversion; missing method %s%s%s"),
8762 go_open_quote(), Gogo::message_name(p
->name()).c_str(),
8764 reason
->assign(buf
);
8769 std::string subreason
;
8770 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
8774 std::string n
= Gogo::message_name(p
->name());
8775 size_t len
= 100 + n
.length() + subreason
.length();
8776 char* buf
= new char[len
];
8777 if (subreason
.empty())
8778 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
8779 go_open_quote(), n
.c_str(), go_close_quote());
8782 _("incompatible type for method %s%s%s (%s)"),
8783 go_open_quote(), n
.c_str(), go_close_quote(),
8785 reason
->assign(buf
);
8798 Interface_type::do_hash_for_method(Gogo
*) const
8800 go_assert(this->methods_are_finalized_
);
8801 unsigned int ret
= 0;
8802 if (this->all_methods_
!= NULL
)
8804 for (Typed_identifier_list::const_iterator p
=
8805 this->all_methods_
->begin();
8806 p
!= this->all_methods_
->end();
8809 ret
= Type::hash_string(p
->name(), ret
);
8810 // We don't use the method type in the hash, to avoid
8811 // infinite recursion if an interface method uses a type
8812 // which is an interface which inherits from the interface
8814 // type T interface { F() interface {T}}
8821 // Return true if T implements the interface. If it does not, and
8822 // REASON is not NULL, set *REASON to a useful error message.
8825 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
8827 go_assert(this->methods_are_finalized_
);
8828 if (this->all_methods_
== NULL
)
8831 bool is_pointer
= false;
8832 const Named_type
* nt
= t
->named_type();
8833 const Struct_type
* st
= t
->struct_type();
8834 // If we start with a named type, we don't dereference it to find
8838 const Type
* pt
= t
->points_to();
8841 // If T is a pointer to a named type, then we need to look at
8842 // the type to which it points.
8844 nt
= pt
->named_type();
8845 st
= pt
->struct_type();
8849 // If we have a named type, get the methods from it rather than from
8854 // Only named and struct types have methods.
8855 if (nt
== NULL
&& st
== NULL
)
8859 if (t
->points_to() != NULL
8860 && t
->points_to()->interface_type() != NULL
)
8861 reason
->assign(_("pointer to interface type has no methods"));
8863 reason
->assign(_("type has no methods"));
8868 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
8872 if (t
->points_to() != NULL
8873 && t
->points_to()->interface_type() != NULL
)
8874 reason
->assign(_("pointer to interface type has no methods"));
8876 reason
->assign(_("type has no methods"));
8881 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8882 p
!= this->all_methods_
->end();
8885 bool is_ambiguous
= false;
8886 Method
* m
= (nt
!= NULL
8887 ? nt
->method_function(p
->name(), &is_ambiguous
)
8888 : st
->method_function(p
->name(), &is_ambiguous
));
8893 std::string n
= Gogo::message_name(p
->name());
8894 size_t len
= n
.length() + 100;
8895 char* buf
= new char[len
];
8897 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
8898 go_open_quote(), n
.c_str(), go_close_quote());
8900 snprintf(buf
, len
, _("missing method %s%s%s"),
8901 go_open_quote(), n
.c_str(), go_close_quote());
8902 reason
->assign(buf
);
8908 Function_type
*p_fn_type
= p
->type()->function_type();
8909 Function_type
* m_fn_type
= m
->type()->function_type();
8910 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
8911 std::string subreason
;
8912 if (!p_fn_type
->is_identical(m_fn_type
, true, COMPARE_TAGS
, true,
8917 std::string n
= Gogo::message_name(p
->name());
8918 size_t len
= 100 + n
.length() + subreason
.length();
8919 char* buf
= new char[len
];
8920 if (subreason
.empty())
8921 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
8922 go_open_quote(), n
.c_str(), go_close_quote());
8925 _("incompatible type for method %s%s%s (%s)"),
8926 go_open_quote(), n
.c_str(), go_close_quote(),
8928 reason
->assign(buf
);
8934 if (!is_pointer
&& !m
->is_value_method())
8938 std::string n
= Gogo::message_name(p
->name());
8939 size_t len
= 100 + n
.length();
8940 char* buf
= new char[len
];
8942 _("method %s%s%s requires a pointer receiver"),
8943 go_open_quote(), n
.c_str(), go_close_quote());
8944 reason
->assign(buf
);
8950 // If the magic //go:nointerface comment was used, the method
8951 // may not be used to implement interfaces.
8952 if (m
->nointerface())
8956 std::string n
= Gogo::message_name(p
->name());
8957 size_t len
= 100 + n
.length();
8958 char* buf
= new char[len
];
8960 _("method %s%s%s is marked go:nointerface"),
8961 go_open_quote(), n
.c_str(), go_close_quote());
8962 reason
->assign(buf
);
8972 // Return the backend representation of the empty interface type. We
8973 // use the same struct for all empty interfaces.
8976 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
8978 static Btype
* empty_interface_type
;
8979 if (empty_interface_type
== NULL
)
8981 std::vector
<Backend::Btyped_identifier
> bfields(2);
8983 Location bloc
= Linemap::predeclared_location();
8985 Type
* pdt
= Type::make_type_descriptor_ptr_type();
8986 bfields
[0].name
= "__type_descriptor";
8987 bfields
[0].btype
= pdt
->get_backend(gogo
);
8988 bfields
[0].location
= bloc
;
8990 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
8991 bfields
[1].name
= "__object";
8992 bfields
[1].btype
= vt
->get_backend(gogo
);
8993 bfields
[1].location
= bloc
;
8995 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
8997 return empty_interface_type
;
9000 // Return a pointer to the backend representation of the method table.
9003 Interface_type::get_backend_methods(Gogo
* gogo
)
9005 if (this->bmethods_
!= NULL
&& !this->bmethods_is_placeholder_
)
9006 return this->bmethods_
;
9008 Location loc
= this->location();
9010 std::vector
<Backend::Btyped_identifier
>
9011 mfields(this->all_methods_
->size() + 1);
9013 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9014 mfields
[0].name
= "__type_descriptor";
9015 mfields
[0].btype
= pdt
->get_backend(gogo
);
9016 mfields
[0].location
= loc
;
9018 std::string last_name
= "";
9020 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9021 p
!= this->all_methods_
->end();
9024 // The type of the method in Go only includes the parameters.
9025 // The actual method also has a receiver, which is always a
9026 // pointer. We need to add that pointer type here in order to
9027 // generate the correct type for the backend.
9028 Function_type
* ft
= p
->type()->function_type();
9029 go_assert(ft
->receiver() == NULL
);
9031 const Typed_identifier_list
* params
= ft
->parameters();
9032 Typed_identifier_list
* mparams
= new Typed_identifier_list();
9034 mparams
->reserve(params
->size() + 1);
9035 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9036 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
9039 for (Typed_identifier_list::const_iterator pp
= params
->begin();
9040 pp
!= params
->end();
9042 mparams
->push_back(*pp
);
9045 Typed_identifier_list
* mresults
= (ft
->results() == NULL
9047 : ft
->results()->copy());
9048 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
9051 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
9052 mfields
[i
].btype
= mft
->get_backend_fntype(gogo
);
9053 mfields
[i
].location
= loc
;
9055 // Sanity check: the names should be sorted.
9056 go_assert(Gogo::unpack_hidden_name(p
->name())
9057 > Gogo::unpack_hidden_name(last_name
));
9058 last_name
= p
->name();
9061 Btype
* st
= gogo
->backend()->struct_type(mfields
);
9062 Btype
* ret
= gogo
->backend()->pointer_type(st
);
9064 if (this->bmethods_
!= NULL
&& this->bmethods_is_placeholder_
)
9065 gogo
->backend()->set_placeholder_pointer_type(this->bmethods_
, ret
);
9066 this->bmethods_
= ret
;
9067 this->bmethods_is_placeholder_
= false;
9071 // Return a placeholder for the pointer to the backend methods table.
9074 Interface_type::get_backend_methods_placeholder(Gogo
* gogo
)
9076 if (this->bmethods_
== NULL
)
9078 Location loc
= this->location();
9079 this->bmethods_
= gogo
->backend()->placeholder_pointer_type("", loc
,
9081 this->bmethods_is_placeholder_
= true;
9083 return this->bmethods_
;
9086 // Return the fields of a non-empty interface type. This is not
9087 // declared in types.h so that types.h doesn't have to #include
9091 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
9092 bool use_placeholder
,
9093 std::vector
<Backend::Btyped_identifier
>* bfields
)
9095 Location loc
= type
->location();
9099 (*bfields
)[0].name
= "__methods";
9100 (*bfields
)[0].btype
= (use_placeholder
9101 ? type
->get_backend_methods_placeholder(gogo
)
9102 : type
->get_backend_methods(gogo
));
9103 (*bfields
)[0].location
= loc
;
9105 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9106 (*bfields
)[1].name
= "__object";
9107 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
9108 (*bfields
)[1].location
= Linemap::predeclared_location();
9111 // Return the backend representation for an interface type. An interface is a
9112 // pointer to a struct. The struct has three fields. The first field is a
9113 // pointer to the type descriptor for the dynamic type of the object.
9114 // The second field is a pointer to a table of methods for the
9115 // interface to be used with the object. The third field is the value
9116 // of the object itself.
9119 Interface_type::do_get_backend(Gogo
* gogo
)
9121 if (this->is_empty())
9122 return Interface_type::get_backend_empty_interface_type(gogo
);
9125 if (this->interface_btype_
!= NULL
)
9126 return this->interface_btype_
;
9127 this->interface_btype_
=
9128 gogo
->backend()->placeholder_struct_type("", this->location_
);
9129 std::vector
<Backend::Btyped_identifier
> bfields
;
9130 get_backend_interface_fields(gogo
, this, false, &bfields
);
9131 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
9133 this->interface_btype_
= gogo
->backend()->error_type();
9134 return this->interface_btype_
;
9138 // Finish the backend representation of the methods.
9141 Interface_type::finish_backend_methods(Gogo
* gogo
)
9143 if (!this->is_empty())
9145 const Typed_identifier_list
* methods
= this->methods();
9146 if (methods
!= NULL
)
9148 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9149 p
!= methods
->end();
9151 p
->type()->get_backend(gogo
);
9154 // Getting the backend methods now will set the placeholder
9156 this->get_backend_methods(gogo
);
9160 // The type of an interface type descriptor.
9163 Interface_type::make_interface_type_descriptor_type()
9168 Type
* tdt
= Type::make_type_descriptor_type();
9169 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
9171 Type
* string_type
= Type::lookup_string_type();
9172 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
9175 Type::make_builtin_struct_type(3,
9176 "name", pointer_string_type
,
9177 "pkgPath", pointer_string_type
,
9180 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
9182 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
9184 Struct_type
* s
= Type::make_builtin_struct_type(2,
9186 "methods", slice_nsm
);
9188 ret
= Type::make_builtin_named_type("InterfaceType", s
);
9194 // Build a type descriptor for an interface type.
9197 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9199 Location bloc
= Linemap::predeclared_location();
9201 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
9203 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
9205 Expression_list
* ivals
= new Expression_list();
9208 Struct_field_list::const_iterator pif
= ifields
->begin();
9209 go_assert(pif
->is_field_name("_type"));
9210 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
9211 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
9215 go_assert(pif
->is_field_name("methods"));
9217 Expression_list
* methods
= new Expression_list();
9218 if (this->all_methods_
!= NULL
)
9220 Type
* elemtype
= pif
->type()->array_type()->element_type();
9222 methods
->reserve(this->all_methods_
->size());
9223 for (Typed_identifier_list::const_iterator pm
=
9224 this->all_methods_
->begin();
9225 pm
!= this->all_methods_
->end();
9228 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
9230 Expression_list
* mvals
= new Expression_list();
9233 Struct_field_list::const_iterator pmf
= mfields
->begin();
9234 go_assert(pmf
->is_field_name("name"));
9235 std::string s
= Gogo::unpack_hidden_name(pm
->name());
9236 Expression
* e
= Expression::make_string(s
, bloc
);
9237 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9240 go_assert(pmf
->is_field_name("pkgPath"));
9241 if (!Gogo::is_hidden_name(pm
->name()))
9242 mvals
->push_back(Expression::make_nil(bloc
));
9245 s
= Gogo::hidden_name_pkgpath(pm
->name());
9246 e
= Expression::make_string(s
, bloc
);
9247 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9251 go_assert(pmf
->is_field_name("typ"));
9252 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
9255 go_assert(pmf
== mfields
->end());
9257 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
9259 methods
->push_back(e
);
9263 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
9267 go_assert(pif
== ifields
->end());
9269 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
9272 // Reflection string.
9275 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9277 ret
->append("interface {");
9278 const Typed_identifier_list
* methods
= this->parse_methods_
;
9279 if (methods
!= NULL
)
9281 ret
->push_back(' ');
9282 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9283 p
!= methods
->end();
9286 if (p
!= methods
->begin())
9288 if (p
->name().empty())
9289 this->append_reflection(p
->type(), gogo
, ret
);
9292 if (!Gogo::is_hidden_name(p
->name()))
9293 ret
->append(p
->name());
9294 else if (gogo
->pkgpath_from_option())
9295 ret
->append(p
->name().substr(1));
9298 // If no -fgo-pkgpath option, backward compatibility
9299 // for how this used to work before -fgo-pkgpath was
9301 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
9302 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
9303 ret
->push_back('.');
9304 ret
->append(Gogo::unpack_hidden_name(p
->name()));
9306 std::string sub
= p
->type()->reflection(gogo
);
9307 go_assert(sub
.compare(0, 4, "func") == 0);
9308 sub
= sub
.substr(4);
9312 ret
->push_back(' ');
9320 Interface_type::do_export(Export
* exp
) const
9322 exp
->write_c_string("interface { ");
9324 const Typed_identifier_list
* methods
= this->parse_methods_
;
9325 if (methods
!= NULL
)
9327 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
9328 pm
!= methods
->end();
9331 if (pm
->name().empty())
9333 exp
->write_c_string("? ");
9334 exp
->write_type(pm
->type());
9338 exp
->write_string(pm
->name());
9339 exp
->write_c_string(" (");
9341 const Function_type
* fntype
= pm
->type()->function_type();
9344 const Typed_identifier_list
* parameters
= fntype
->parameters();
9345 if (parameters
!= NULL
)
9347 bool is_varargs
= fntype
->is_varargs();
9348 for (Typed_identifier_list::const_iterator pp
=
9349 parameters
->begin();
9350 pp
!= parameters
->end();
9356 exp
->write_c_string(", ");
9357 exp
->write_name(pp
->name());
9358 exp
->write_c_string(" ");
9359 if (!is_varargs
|| pp
+ 1 != parameters
->end())
9360 exp
->write_type(pp
->type());
9363 exp
->write_c_string("...");
9364 Type
*pptype
= pp
->type();
9365 exp
->write_type(pptype
->array_type()->element_type());
9370 exp
->write_c_string(")");
9372 const Typed_identifier_list
* results
= fntype
->results();
9373 if (results
!= NULL
)
9375 exp
->write_c_string(" ");
9376 if (results
->size() == 1 && results
->begin()->name().empty())
9377 exp
->write_type(results
->begin()->type());
9381 exp
->write_c_string("(");
9382 for (Typed_identifier_list::const_iterator p
=
9384 p
!= results
->end();
9390 exp
->write_c_string(", ");
9391 exp
->write_name(p
->name());
9392 exp
->write_c_string(" ");
9393 exp
->write_type(p
->type());
9395 exp
->write_c_string(")");
9400 exp
->write_c_string("; ");
9404 exp
->write_c_string("}");
9407 // Import an interface type.
9410 Interface_type::do_import(Import
* imp
)
9412 imp
->require_c_string("interface { ");
9414 Typed_identifier_list
* methods
= new Typed_identifier_list
;
9415 while (imp
->peek_char() != '}')
9417 std::string name
= imp
->read_identifier();
9421 imp
->require_c_string(" ");
9422 Type
* t
= imp
->read_type();
9423 methods
->push_back(Typed_identifier("", t
, imp
->location()));
9424 imp
->require_c_string("; ");
9428 imp
->require_c_string(" (");
9430 Typed_identifier_list
* parameters
;
9431 bool is_varargs
= false;
9432 if (imp
->peek_char() == ')')
9436 parameters
= new Typed_identifier_list
;
9439 std::string name
= imp
->read_name();
9440 imp
->require_c_string(" ");
9442 if (imp
->match_c_string("..."))
9448 Type
* ptype
= imp
->read_type();
9450 ptype
= Type::make_array_type(ptype
, NULL
);
9451 parameters
->push_back(Typed_identifier(name
, ptype
,
9453 if (imp
->peek_char() != ',')
9455 go_assert(!is_varargs
);
9456 imp
->require_c_string(", ");
9459 imp
->require_c_string(")");
9461 Typed_identifier_list
* results
;
9462 if (imp
->peek_char() != ' ')
9466 results
= new Typed_identifier_list
;
9468 if (imp
->peek_char() != '(')
9470 Type
* rtype
= imp
->read_type();
9471 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
9478 std::string name
= imp
->read_name();
9479 imp
->require_c_string(" ");
9480 Type
* rtype
= imp
->read_type();
9481 results
->push_back(Typed_identifier(name
, rtype
,
9483 if (imp
->peek_char() != ',')
9485 imp
->require_c_string(", ");
9487 imp
->require_c_string(")");
9491 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
9495 fntype
->set_is_varargs();
9496 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
9498 imp
->require_c_string("; ");
9501 imp
->require_c_string("}");
9503 if (methods
->empty())
9509 Interface_type
* ret
= Type::make_interface_type(methods
, imp
->location());
9510 ret
->package_
= imp
->package();
9514 // Make an interface type.
9517 Type::make_interface_type(Typed_identifier_list
* methods
,
9520 return new Interface_type(methods
, location
);
9523 // Make an empty interface type.
9526 Type::make_empty_interface_type(Location location
)
9528 Interface_type
* ret
= new Interface_type(NULL
, location
);
9529 ret
->finalize_methods();
9535 // Bind a method to an object.
9538 Method::bind_method(Expression
* expr
, Location location
) const
9540 if (this->stub_
== NULL
)
9542 // When there is no stub object, the binding is determined by
9544 return this->do_bind_method(expr
, location
);
9546 return Expression::make_bound_method(expr
, this, this->stub_
, location
);
9549 // Return the named object associated with a method. This may only be
9550 // called after methods are finalized.
9553 Method::named_object() const
9555 if (this->stub_
!= NULL
)
9557 return this->do_named_object();
9560 // Class Named_method.
9562 // The type of the method.
9565 Named_method::do_type() const
9567 if (this->named_object_
->is_function())
9568 return this->named_object_
->func_value()->type();
9569 else if (this->named_object_
->is_function_declaration())
9570 return this->named_object_
->func_declaration_value()->type();
9575 // Return the location of the method receiver.
9578 Named_method::do_receiver_location() const
9580 return this->do_type()->receiver()->location();
9583 // Bind a method to an object.
9586 Named_method::do_bind_method(Expression
* expr
, Location location
) const
9588 Named_object
* no
= this->named_object_
;
9589 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, this,
9591 // If this is not a local method, and it does not use a stub, then
9592 // the real method expects a different type. We need to cast the
9594 if (this->depth() > 0 && !this->needs_stub_method())
9596 Function_type
* ftype
= this->do_type();
9597 go_assert(ftype
->is_method());
9598 Type
* frtype
= ftype
->receiver()->type();
9599 bme
->set_first_argument_type(frtype
);
9604 // Return whether this method should not participate in interfaces.
9607 Named_method::do_nointerface() const
9609 Named_object
* no
= this->named_object_
;
9610 return no
->is_function() && no
->func_value()->nointerface();
9613 // Class Interface_method.
9615 // Bind a method to an object.
9618 Interface_method::do_bind_method(Expression
* expr
,
9619 Location location
) const
9621 return Expression::make_interface_field_reference(expr
, this->name_
,
9627 // Insert a new method. Return true if it was inserted, false
9631 Methods::insert(const std::string
& name
, Method
* m
)
9633 std::pair
<Method_map::iterator
, bool> ins
=
9634 this->methods_
.insert(std::make_pair(name
, m
));
9639 Method
* old_method
= ins
.first
->second
;
9640 if (m
->depth() < old_method
->depth())
9643 ins
.first
->second
= m
;
9648 if (m
->depth() == old_method
->depth())
9649 old_method
->set_is_ambiguous();
9655 // Return the number of unambiguous methods.
9658 Methods::count() const
9661 for (Method_map::const_iterator p
= this->methods_
.begin();
9662 p
!= this->methods_
.end();
9664 if (!p
->second
->is_ambiguous())
9669 // Class Named_type.
9671 // Return the name of the type.
9674 Named_type::name() const
9676 return this->named_object_
->name();
9679 // Return the name of the type to use in an error message.
9682 Named_type::message_name() const
9684 return this->named_object_
->message_name();
9687 // Return the base type for this type. We have to be careful about
9688 // circular type definitions, which are invalid but may be seen here.
9691 Named_type::named_base()
9696 Type
* ret
= this->type_
->base();
9697 this->seen_
= false;
9702 Named_type::named_base() const
9707 const Type
* ret
= this->type_
->base();
9708 this->seen_
= false;
9712 // Return whether this is an error type. We have to be careful about
9713 // circular type definitions, which are invalid but may be seen here.
9716 Named_type::is_named_error_type() const
9721 bool ret
= this->type_
->is_error_type();
9722 this->seen_
= false;
9726 // Whether this type is comparable. We have to be careful about
9727 // circular type definitions.
9730 Named_type::named_type_is_comparable(std::string
* reason
) const
9735 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
9736 this->type_
, reason
);
9737 this->seen_
= false;
9741 // Add a method to this type.
9744 Named_type::add_method(const std::string
& name
, Function
* function
)
9746 go_assert(!this->is_alias_
);
9747 if (this->local_methods_
== NULL
)
9748 this->local_methods_
= new Bindings(NULL
);
9749 return this->local_methods_
->add_function(name
, NULL
, function
);
9752 // Add a method declaration to this type.
9755 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
9756 Function_type
* type
,
9759 go_assert(!this->is_alias_
);
9760 if (this->local_methods_
== NULL
)
9761 this->local_methods_
= new Bindings(NULL
);
9762 return this->local_methods_
->add_function_declaration(name
, package
, type
,
9766 // Add an existing method to this type.
9769 Named_type::add_existing_method(Named_object
* no
)
9771 go_assert(!this->is_alias_
);
9772 if (this->local_methods_
== NULL
)
9773 this->local_methods_
= new Bindings(NULL
);
9774 this->local_methods_
->add_named_object(no
);
9777 // Look for a local method NAME, and returns its named object, or NULL
9781 Named_type::find_local_method(const std::string
& name
) const
9783 if (this->is_error_
)
9785 if (this->is_alias_
)
9787 Named_type
* nt
= this->type_
->named_type();
9790 if (this->seen_alias_
)
9792 this->seen_alias_
= true;
9793 Named_object
* ret
= nt
->find_local_method(name
);
9794 this->seen_alias_
= false;
9799 if (this->local_methods_
== NULL
)
9801 return this->local_methods_
->lookup(name
);
9804 // Return the list of local methods.
9807 Named_type::local_methods() const
9809 if (this->is_error_
)
9811 if (this->is_alias_
)
9813 Named_type
* nt
= this->type_
->named_type();
9816 if (this->seen_alias_
)
9818 this->seen_alias_
= true;
9819 const Bindings
* ret
= nt
->local_methods();
9820 this->seen_alias_
= false;
9825 return this->local_methods_
;
9828 // Return whether NAME is an unexported field or method, for better
9832 Named_type::is_unexported_local_method(Gogo
* gogo
,
9833 const std::string
& name
) const
9835 if (this->is_error_
)
9837 if (this->is_alias_
)
9839 Named_type
* nt
= this->type_
->named_type();
9842 if (this->seen_alias_
)
9844 this->seen_alias_
= true;
9845 bool ret
= nt
->is_unexported_local_method(gogo
, name
);
9846 this->seen_alias_
= false;
9851 Bindings
* methods
= this->local_methods_
;
9852 if (methods
!= NULL
)
9854 for (Bindings::const_declarations_iterator p
=
9855 methods
->begin_declarations();
9856 p
!= methods
->end_declarations();
9859 if (Gogo::is_hidden_name(p
->first
)
9860 && name
== Gogo::unpack_hidden_name(p
->first
)
9861 && gogo
->pack_hidden_name(name
, false) != p
->first
)
9868 // Build the complete list of methods for this type, which means
9869 // recursively including all methods for anonymous fields. Create all
9873 Named_type::finalize_methods(Gogo
* gogo
)
9875 if (this->is_alias_
)
9877 if (this->all_methods_
!= NULL
)
9880 if (this->local_methods_
!= NULL
9881 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
9883 const Bindings
* lm
= this->local_methods_
;
9884 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
9885 p
!= lm
->end_declarations();
9887 go_error_at(p
->second
->location(),
9888 "invalid pointer or interface receiver type");
9889 delete this->local_methods_
;
9890 this->local_methods_
= NULL
;
9894 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
9897 // Return whether this type has any methods.
9900 Named_type::has_any_methods() const
9902 if (this->is_error_
)
9904 if (this->is_alias_
)
9906 if (this->type_
->named_type() != NULL
)
9908 if (this->seen_alias_
)
9910 this->seen_alias_
= true;
9911 bool ret
= this->type_
->named_type()->has_any_methods();
9912 this->seen_alias_
= false;
9915 if (this->type_
->struct_type() != NULL
)
9916 return this->type_
->struct_type()->has_any_methods();
9919 return this->all_methods_
!= NULL
;
9922 // Return the methods for this type.
9925 Named_type::methods() const
9927 if (this->is_error_
)
9929 if (this->is_alias_
)
9931 if (this->type_
->named_type() != NULL
)
9933 if (this->seen_alias_
)
9935 this->seen_alias_
= true;
9936 const Methods
* ret
= this->type_
->named_type()->methods();
9937 this->seen_alias_
= false;
9940 if (this->type_
->struct_type() != NULL
)
9941 return this->type_
->struct_type()->methods();
9944 return this->all_methods_
;
9947 // Return the method NAME, or NULL if there isn't one or if it is
9948 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
9952 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
9954 if (this->is_error_
)
9956 if (this->is_alias_
)
9958 if (is_ambiguous
!= NULL
)
9959 *is_ambiguous
= false;
9960 if (this->type_
->named_type() != NULL
)
9962 if (this->seen_alias_
)
9964 this->seen_alias_
= true;
9965 Named_type
* nt
= this->type_
->named_type();
9966 Method
* ret
= nt
->method_function(name
, is_ambiguous
);
9967 this->seen_alias_
= false;
9970 if (this->type_
->struct_type() != NULL
)
9971 return this->type_
->struct_type()->method_function(name
, is_ambiguous
);
9974 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
9977 // Return a pointer to the interface method table for this type for
9978 // the interface INTERFACE. IS_POINTER is true if this is for a
9982 Named_type::interface_method_table(Interface_type
* interface
, bool is_pointer
)
9984 if (this->is_error_
)
9985 return Expression::make_error(this->location_
);
9986 if (this->is_alias_
)
9988 if (this->type_
->named_type() != NULL
)
9990 if (this->seen_alias_
)
9991 return Expression::make_error(this->location_
);
9992 this->seen_alias_
= true;
9993 Named_type
* nt
= this->type_
->named_type();
9994 Expression
* ret
= nt
->interface_method_table(interface
, is_pointer
);
9995 this->seen_alias_
= false;
9998 if (this->type_
->struct_type() != NULL
)
9999 return this->type_
->struct_type()->interface_method_table(interface
,
10003 return Type::interface_method_table(this, interface
, is_pointer
,
10004 &this->interface_method_tables_
,
10005 &this->pointer_interface_method_tables_
);
10008 // Look for a use of a complete type within another type. This is
10009 // used to check that we don't try to use a type within itself.
10011 class Find_type_use
: public Traverse
10014 Find_type_use(Named_type
* find_type
)
10015 : Traverse(traverse_types
),
10016 find_type_(find_type
), found_(false)
10019 // Whether we found the type.
10022 { return this->found_
; }
10029 // The type we are looking for.
10030 Named_type
* find_type_
;
10031 // Whether we found the type.
10035 // Check for FIND_TYPE in TYPE.
10038 Find_type_use::type(Type
* type
)
10040 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
10042 this->found_
= true;
10043 return TRAVERSE_EXIT
;
10046 // It's OK if we see a reference to the type in any type which is
10047 // essentially a pointer: a pointer, a slice, a function, a map, or
10049 if (type
->points_to() != NULL
10050 || type
->is_slice_type()
10051 || type
->function_type() != NULL
10052 || type
->map_type() != NULL
10053 || type
->channel_type() != NULL
)
10054 return TRAVERSE_SKIP_COMPONENTS
;
10056 // For an interface, a reference to the type in a method type should
10057 // be ignored, but we have to consider direct inheritance. When
10058 // this is called, there may be cases of direct inheritance
10059 // represented as a method with no name.
10060 if (type
->interface_type() != NULL
)
10062 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
10063 if (methods
!= NULL
)
10065 for (Typed_identifier_list::const_iterator p
= methods
->begin();
10066 p
!= methods
->end();
10069 if (p
->name().empty())
10071 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
10072 return TRAVERSE_EXIT
;
10076 return TRAVERSE_SKIP_COMPONENTS
;
10079 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
10080 // to convert TYPE to the backend representation before we convert
10082 if (type
->named_type() != NULL
)
10084 switch (type
->base()->classification())
10086 case Type::TYPE_ERROR
:
10087 case Type::TYPE_BOOLEAN
:
10088 case Type::TYPE_INTEGER
:
10089 case Type::TYPE_FLOAT
:
10090 case Type::TYPE_COMPLEX
:
10091 case Type::TYPE_STRING
:
10092 case Type::TYPE_NIL
:
10095 case Type::TYPE_ARRAY
:
10096 case Type::TYPE_STRUCT
:
10097 this->find_type_
->add_dependency(type
->named_type());
10100 case Type::TYPE_NAMED
:
10101 case Type::TYPE_FORWARD
:
10102 go_assert(saw_errors());
10105 case Type::TYPE_VOID
:
10106 case Type::TYPE_SINK
:
10107 case Type::TYPE_FUNCTION
:
10108 case Type::TYPE_POINTER
:
10109 case Type::TYPE_CALL_MULTIPLE_RESULT
:
10110 case Type::TYPE_MAP
:
10111 case Type::TYPE_CHANNEL
:
10112 case Type::TYPE_INTERFACE
:
10118 return TRAVERSE_CONTINUE
;
10121 // Look for a circular reference of an alias.
10123 class Find_alias
: public Traverse
10126 Find_alias(Named_type
* find_type
)
10127 : Traverse(traverse_types
),
10128 find_type_(find_type
), found_(false)
10131 // Whether we found the type.
10134 { return this->found_
; }
10141 // The type we are looking for.
10142 Named_type
* find_type_
;
10143 // Whether we found the type.
10148 Find_alias::type(Type
* type
)
10150 Named_type
* nt
= type
->named_type();
10153 if (nt
== this->find_type_
)
10155 this->found_
= true;
10156 return TRAVERSE_EXIT
;
10159 // We started from `type T1 = T2`, where T1 is find_type_ and T2
10160 // is, perhaps indirectly, the parameter TYPE. If TYPE is not
10161 // an alias itself, it's OK if whatever T2 is defined as refers
10163 if (!nt
->is_alias())
10164 return TRAVERSE_SKIP_COMPONENTS
;
10167 return TRAVERSE_CONTINUE
;
10170 // Verify that a named type does not refer to itself.
10173 Named_type::do_verify()
10175 if (this->is_verified_
)
10177 this->is_verified_
= true;
10179 if (this->is_error_
)
10182 if (this->is_alias_
)
10184 Find_alias
find(this);
10185 Type::traverse(this->type_
, &find
);
10188 go_error_at(this->location_
, "invalid recursive alias %qs",
10189 this->message_name().c_str());
10190 this->is_error_
= true;
10195 Find_type_use
find(this);
10196 Type::traverse(this->type_
, &find
);
10199 go_error_at(this->location_
, "invalid recursive type %qs",
10200 this->message_name().c_str());
10201 this->is_error_
= true;
10205 // Check whether any of the local methods overloads an existing
10206 // struct field or interface method. We don't need to check the
10207 // list of methods against itself: that is handled by the Bindings
10209 if (this->local_methods_
!= NULL
)
10211 Struct_type
* st
= this->type_
->struct_type();
10214 for (Bindings::const_declarations_iterator p
=
10215 this->local_methods_
->begin_declarations();
10216 p
!= this->local_methods_
->end_declarations();
10219 const std::string
& name(p
->first
);
10220 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
10222 go_error_at(p
->second
->location(),
10223 "method %qs redeclares struct field name",
10224 Gogo::message_name(name
).c_str());
10233 // Return whether this type is or contains a pointer.
10236 Named_type::do_has_pointer() const
10240 this->seen_
= true;
10241 bool ret
= this->type_
->has_pointer();
10242 this->seen_
= false;
10246 // Return whether comparisons for this type can use the identity
10250 Named_type::do_compare_is_identity(Gogo
* gogo
)
10252 // We don't use this->seen_ here because compare_is_identity may
10253 // call base() later, and that will mess up if seen_ is set here.
10254 if (this->seen_in_compare_is_identity_
)
10256 this->seen_in_compare_is_identity_
= true;
10257 bool ret
= this->type_
->compare_is_identity(gogo
);
10258 this->seen_in_compare_is_identity_
= false;
10262 // Return whether this type is reflexive--whether it is always equal
10266 Named_type::do_is_reflexive()
10268 if (this->seen_in_compare_is_identity_
)
10270 this->seen_in_compare_is_identity_
= true;
10271 bool ret
= this->type_
->is_reflexive();
10272 this->seen_in_compare_is_identity_
= false;
10276 // Return whether this type needs a key update when used as a map key.
10279 Named_type::do_needs_key_update()
10281 if (this->seen_in_compare_is_identity_
)
10283 this->seen_in_compare_is_identity_
= true;
10284 bool ret
= this->type_
->needs_key_update();
10285 this->seen_in_compare_is_identity_
= false;
10289 // Return a hash code. This is used for method lookup. We simply
10290 // hash on the name itself.
10293 Named_type::do_hash_for_method(Gogo
* gogo
) const
10295 if (this->is_error_
)
10298 // Aliases are handled in Type::hash_for_method.
10299 go_assert(!this->is_alias_
);
10301 const std::string
& name(this->named_object()->name());
10302 unsigned int ret
= Type::hash_string(name
, 0);
10304 // GOGO will be NULL here when called from Type_hash_identical.
10305 // That is OK because that is only used for internal hash tables
10306 // where we are going to be comparing named types for equality. In
10307 // other cases, which are cases where the runtime is going to
10308 // compare hash codes to see if the types are the same, we need to
10309 // include the pkgpath in the hash.
10310 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
10312 const Package
* package
= this->named_object()->package();
10313 if (package
== NULL
)
10314 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
10316 ret
= Type::hash_string(package
->pkgpath(), ret
);
10322 // Convert a named type to the backend representation. In order to
10323 // get dependencies right, we fill in a dummy structure for this type,
10324 // then convert all the dependencies, then complete this type. When
10325 // this function is complete, the size of the type is known.
10328 Named_type::convert(Gogo
* gogo
)
10330 if (this->is_error_
|| this->is_converted_
)
10333 this->create_placeholder(gogo
);
10335 // If we are called to turn unsafe.Sizeof into a constant, we may
10336 // not have verified the type yet. We have to make sure it is
10337 // verified, since that sets the list of dependencies.
10340 // Convert all the dependencies. If they refer indirectly back to
10341 // this type, they will pick up the intermediate representation we just
10343 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
10344 p
!= this->dependencies_
.end();
10346 (*p
)->convert(gogo
);
10348 // Complete this type.
10349 Btype
* bt
= this->named_btype_
;
10350 Type
* base
= this->type_
->base();
10351 switch (base
->classification())
10366 case TYPE_FUNCTION
:
10368 // The size of these types is already correct. We don't worry
10369 // about filling them in until later, when we also track
10370 // circular references.
10375 std::vector
<Backend::Btyped_identifier
> bfields
;
10376 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
10378 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10379 bt
= gogo
->backend()->error_type();
10384 // Slice types were completed in create_placeholder.
10385 if (!base
->is_slice_type())
10387 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
10388 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
10389 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
10390 bt
= gogo
->backend()->error_type();
10394 case TYPE_INTERFACE
:
10395 // Interface types were completed in create_placeholder.
10403 case TYPE_CALL_MULTIPLE_RESULT
:
10409 this->named_btype_
= bt
;
10410 this->is_converted_
= true;
10411 this->is_placeholder_
= false;
10414 // Create the placeholder for a named type. This is the first step in
10415 // converting to the backend representation.
10418 Named_type::create_placeholder(Gogo
* gogo
)
10420 if (this->is_error_
)
10421 this->named_btype_
= gogo
->backend()->error_type();
10423 if (this->named_btype_
!= NULL
)
10426 // Create the structure for this type. Note that because we call
10427 // base() here, we don't attempt to represent a named type defined
10428 // as another named type. Instead both named types will point to
10429 // different base representations.
10430 Type
* base
= this->type_
->base();
10432 bool set_name
= true;
10433 switch (base
->classification())
10436 this->is_error_
= true;
10437 this->named_btype_
= gogo
->backend()->error_type();
10447 // These are simple basic types, we can just create them
10449 bt
= Type::get_named_base_btype(gogo
, base
);
10454 // All maps and channels have the same backend representation.
10455 bt
= Type::get_named_base_btype(gogo
, base
);
10458 case TYPE_FUNCTION
:
10461 bool for_function
= base
->classification() == TYPE_FUNCTION
;
10462 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
10470 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10472 this->is_placeholder_
= true;
10477 if (base
->is_slice_type())
10478 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10482 bt
= gogo
->backend()->placeholder_array_type(this->name(),
10484 this->is_placeholder_
= true;
10489 case TYPE_INTERFACE
:
10490 if (base
->interface_type()->is_empty())
10491 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
10494 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10502 case TYPE_CALL_MULTIPLE_RESULT
:
10509 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
10511 this->named_btype_
= bt
;
10513 if (base
->is_slice_type())
10515 // We do not record slices as dependencies of other types,
10516 // because we can fill them in completely here with the final
10518 std::vector
<Backend::Btyped_identifier
> bfields
;
10519 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
10520 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10521 this->named_btype_
= gogo
->backend()->error_type();
10523 else if (base
->interface_type() != NULL
10524 && !base
->interface_type()->is_empty())
10526 // We do not record interfaces as dependencies of other types,
10527 // because we can fill them in completely here with the final
10529 std::vector
<Backend::Btyped_identifier
> bfields
;
10530 get_backend_interface_fields(gogo
, base
->interface_type(), true,
10532 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10533 this->named_btype_
= gogo
->backend()->error_type();
10537 // Get the backend representation for a named type.
10540 Named_type::do_get_backend(Gogo
* gogo
)
10542 if (this->is_error_
)
10543 return gogo
->backend()->error_type();
10545 Btype
* bt
= this->named_btype_
;
10547 if (!gogo
->named_types_are_converted())
10549 // We have not completed converting named types. NAMED_BTYPE_
10550 // is a placeholder and we shouldn't do anything further.
10554 // We don't build dependencies for types whose sizes do not
10555 // change or are not relevant, so we may see them here while
10556 // converting types.
10557 this->create_placeholder(gogo
);
10558 bt
= this->named_btype_
;
10559 go_assert(bt
!= NULL
);
10563 // We are not converting types. This should only be called if the
10564 // type has already been converted.
10565 if (!this->is_converted_
)
10567 go_assert(saw_errors());
10568 return gogo
->backend()->error_type();
10571 go_assert(bt
!= NULL
);
10573 // Complete the backend representation.
10574 Type
* base
= this->type_
->base();
10576 switch (base
->classification())
10579 return gogo
->backend()->error_type();
10593 if (!this->seen_in_get_backend_
)
10595 this->seen_in_get_backend_
= true;
10596 base
->struct_type()->finish_backend_fields(gogo
);
10597 this->seen_in_get_backend_
= false;
10602 if (!this->seen_in_get_backend_
)
10604 this->seen_in_get_backend_
= true;
10605 base
->array_type()->finish_backend_element(gogo
);
10606 this->seen_in_get_backend_
= false;
10610 case TYPE_INTERFACE
:
10611 if (!this->seen_in_get_backend_
)
10613 this->seen_in_get_backend_
= true;
10614 base
->interface_type()->finish_backend_methods(gogo
);
10615 this->seen_in_get_backend_
= false;
10619 case TYPE_FUNCTION
:
10620 // Don't build a circular data structure. GENERIC can't handle
10622 if (this->seen_in_get_backend_
)
10624 this->is_circular_
= true;
10625 return gogo
->backend()->circular_pointer_type(bt
, true);
10627 this->seen_in_get_backend_
= true;
10628 bt1
= Type::get_named_base_btype(gogo
, base
);
10629 this->seen_in_get_backend_
= false;
10630 if (this->is_circular_
)
10631 bt1
= gogo
->backend()->circular_pointer_type(bt
, true);
10632 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10633 bt
= gogo
->backend()->error_type();
10637 // Don't build a circular data structure. GENERIC can't handle
10639 if (this->seen_in_get_backend_
)
10641 this->is_circular_
= true;
10642 return gogo
->backend()->circular_pointer_type(bt
, false);
10644 this->seen_in_get_backend_
= true;
10645 bt1
= Type::get_named_base_btype(gogo
, base
);
10646 this->seen_in_get_backend_
= false;
10647 if (this->is_circular_
)
10648 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
10649 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10650 bt
= gogo
->backend()->error_type();
10655 case TYPE_CALL_MULTIPLE_RESULT
:
10664 // Build a type descriptor for a named type.
10667 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
10669 if (this->is_error_
)
10670 return Expression::make_error(this->location_
);
10671 if (name
== NULL
&& this->is_alias_
)
10673 if (this->seen_alias_
)
10674 return Expression::make_error(this->location_
);
10675 this->seen_alias_
= true;
10676 Expression
* ret
= this->type_
->type_descriptor(gogo
, NULL
);
10677 this->seen_alias_
= false;
10681 // If NAME is not NULL, then we don't really want the type
10682 // descriptor for this type; we want the descriptor for the
10683 // underlying type, giving it the name NAME.
10684 return this->named_type_descriptor(gogo
, this->type_
,
10685 name
== NULL
? this : name
);
10688 // Add to the reflection string. This is used mostly for the name of
10689 // the type used in a type descriptor, not for actual reflection
10693 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
10695 this->append_reflection_type_name(gogo
, false, ret
);
10698 // Add to the reflection string. For an alias we normally use the
10699 // real name, but if USE_ALIAS is true we use the alias name itself.
10702 Named_type::append_reflection_type_name(Gogo
* gogo
, bool use_alias
,
10703 std::string
* ret
) const
10705 if (this->is_error_
)
10707 if (this->is_alias_
&& !use_alias
)
10709 if (this->seen_alias_
)
10711 this->seen_alias_
= true;
10712 this->append_reflection(this->type_
, gogo
, ret
);
10713 this->seen_alias_
= false;
10716 if (!this->is_builtin())
10718 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
10719 // make a unique reflection string, so that the type
10720 // canonicalization in the reflect package will work. In order
10721 // to be compatible with the gc compiler, we put tabs into the
10722 // package path, so that the reflect methods can discard it.
10723 const Package
* package
= this->named_object_
->package();
10724 ret
->push_back('\t');
10725 ret
->append(package
!= NULL
10726 ? package
->pkgpath_symbol()
10727 : gogo
->pkgpath_symbol());
10728 ret
->push_back('\t');
10729 ret
->append(package
!= NULL
10730 ? package
->package_name()
10731 : gogo
->package_name());
10732 ret
->push_back('.');
10734 if (this->in_function_
!= NULL
)
10736 ret
->push_back('\t');
10737 const Typed_identifier
* rcvr
=
10738 this->in_function_
->func_value()->type()->receiver();
10741 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
10742 ret
->append(Gogo::unpack_hidden_name(rcvr_type
->name()));
10743 ret
->push_back('.');
10745 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
10746 ret
->push_back('$');
10747 if (this->in_function_index_
> 0)
10750 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
10752 ret
->push_back('$');
10754 ret
->push_back('\t');
10756 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
10759 // Export the type. This is called to export a global type.
10762 Named_type::export_named_type(Export
* exp
, const std::string
&) const
10764 // We don't need to write the name of the type here, because it will
10765 // be written by Export::write_type anyhow.
10766 exp
->write_c_string("type ");
10767 exp
->write_type(this);
10768 exp
->write_c_string(";\n");
10771 // Import a named type.
10774 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
10776 imp
->require_c_string("type ");
10777 Type
*type
= imp
->read_type();
10778 *ptype
= type
->named_type();
10779 go_assert(*ptype
!= NULL
);
10780 imp
->require_c_string(";\n");
10783 // Export the type when it is referenced by another type. In this
10784 // case Export::export_type will already have issued the name.
10787 Named_type::do_export(Export
* exp
) const
10789 exp
->write_type(this->type_
);
10791 // To save space, we only export the methods directly attached to
10793 Bindings
* methods
= this->local_methods_
;
10794 if (methods
== NULL
)
10797 exp
->write_c_string("\n");
10798 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
10799 p
!= methods
->end_definitions();
10802 exp
->write_c_string(" ");
10803 (*p
)->export_named_object(exp
);
10806 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
10807 p
!= methods
->end_declarations();
10810 if (p
->second
->is_function_declaration())
10812 exp
->write_c_string(" ");
10813 p
->second
->export_named_object(exp
);
10818 // Make a named type.
10821 Type::make_named_type(Named_object
* named_object
, Type
* type
,
10824 return new Named_type(named_object
, type
, location
);
10827 // Finalize the methods for TYPE. It will be a named type or a struct
10828 // type. This sets *ALL_METHODS to the list of methods, and builds
10829 // all required stubs.
10832 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
10833 Methods
** all_methods
)
10835 *all_methods
= new Methods();
10836 std::vector
<const Named_type
*> seen
;
10837 Type::add_methods_for_type(type
, NULL
, 0, false, false, &seen
, *all_methods
);
10838 if ((*all_methods
)->empty())
10840 delete *all_methods
;
10841 *all_methods
= NULL
;
10843 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
10846 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
10847 // build up the struct field indexes as we go. DEPTH is the depth of
10848 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
10849 // adding these methods for an anonymous field with pointer type.
10850 // NEEDS_STUB_METHOD is true if we need to use a stub method which
10851 // calls the real method. TYPES_SEEN is used to avoid infinite
10855 Type::add_methods_for_type(const Type
* type
,
10856 const Method::Field_indexes
* field_indexes
,
10857 unsigned int depth
,
10858 bool is_embedded_pointer
,
10859 bool needs_stub_method
,
10860 std::vector
<const Named_type
*>* seen
,
10863 // Pointer types may not have methods.
10864 if (type
->points_to() != NULL
)
10867 const Named_type
* nt
= type
->named_type();
10870 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
10878 seen
->push_back(nt
);
10880 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
10881 is_embedded_pointer
, needs_stub_method
,
10885 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
10886 is_embedded_pointer
, needs_stub_method
,
10889 // If we are called with depth > 0, then we are looking at an
10890 // anonymous field of a struct. If such a field has interface type,
10891 // then we need to add the interface methods. We don't want to add
10892 // them when depth == 0, because we will already handle them
10893 // following the usual rules for an interface type.
10895 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
10901 // Add the local methods for the named type NT to *METHODS. The
10902 // parameters are as for add_methods_to_type.
10905 Type::add_local_methods_for_type(const Named_type
* nt
,
10906 const Method::Field_indexes
* field_indexes
,
10907 unsigned int depth
,
10908 bool is_embedded_pointer
,
10909 bool needs_stub_method
,
10912 const Bindings
* local_methods
= nt
->local_methods();
10913 if (local_methods
== NULL
)
10916 for (Bindings::const_declarations_iterator p
=
10917 local_methods
->begin_declarations();
10918 p
!= local_methods
->end_declarations();
10921 Named_object
* no
= p
->second
;
10922 bool is_value_method
= (is_embedded_pointer
10923 || !Type::method_expects_pointer(no
));
10924 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
10925 (needs_stub_method
|| depth
> 0));
10926 if (!methods
->insert(no
->name(), m
))
10931 // Add the embedded methods for TYPE to *METHODS. These are the
10932 // methods attached to anonymous fields. The parameters are as for
10933 // add_methods_to_type.
10936 Type::add_embedded_methods_for_type(const Type
* type
,
10937 const Method::Field_indexes
* field_indexes
,
10938 unsigned int depth
,
10939 bool is_embedded_pointer
,
10940 bool needs_stub_method
,
10941 std::vector
<const Named_type
*>* seen
,
10944 // Look for anonymous fields in TYPE. TYPE has fields if it is a
10946 const Struct_type
* st
= type
->struct_type();
10950 const Struct_field_list
* fields
= st
->fields();
10951 if (fields
== NULL
)
10954 unsigned int i
= 0;
10955 for (Struct_field_list::const_iterator pf
= fields
->begin();
10956 pf
!= fields
->end();
10959 if (!pf
->is_anonymous())
10962 Type
* ftype
= pf
->type();
10963 bool is_pointer
= false;
10964 if (ftype
->points_to() != NULL
)
10966 ftype
= ftype
->points_to();
10969 Named_type
* fnt
= ftype
->named_type();
10972 // This is an error, but it will be diagnosed elsewhere.
10976 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
10977 sub_field_indexes
->next
= field_indexes
;
10978 sub_field_indexes
->field_index
= i
;
10980 Methods tmp_methods
;
10981 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
10982 (is_embedded_pointer
|| is_pointer
),
10988 // Check if there are promoted methods that conflict with field names and
10989 // don't add them to the method map.
10990 for (Methods::const_iterator p
= tmp_methods
.begin();
10991 p
!= tmp_methods
.end();
10994 bool found
= false;
10995 for (Struct_field_list::const_iterator fp
= fields
->begin();
10996 fp
!= fields
->end();
10999 if (fp
->field_name() == p
->first
)
11006 !methods
->insert(p
->first
, p
->second
))
11012 // If TYPE is an interface type, then add its method to *METHODS.
11013 // This is for interface methods attached to an anonymous field. The
11014 // parameters are as for add_methods_for_type.
11017 Type::add_interface_methods_for_type(const Type
* type
,
11018 const Method::Field_indexes
* field_indexes
,
11019 unsigned int depth
,
11022 const Interface_type
* it
= type
->interface_type();
11026 const Typed_identifier_list
* imethods
= it
->methods();
11027 if (imethods
== NULL
)
11030 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
11031 pm
!= imethods
->end();
11034 Function_type
* fntype
= pm
->type()->function_type();
11035 if (fntype
== NULL
)
11037 // This is an error, but it should be reported elsewhere
11038 // when we look at the methods for IT.
11041 go_assert(!fntype
->is_method());
11042 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
11043 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
11044 field_indexes
, depth
);
11045 if (!methods
->insert(pm
->name(), m
))
11050 // Build stub methods for TYPE as needed. METHODS is the set of
11051 // methods for the type. A stub method may be needed when a type
11052 // inherits a method from an anonymous field. When we need the
11053 // address of the method, as in a type descriptor, we need to build a
11054 // little stub which does the required field dereferences and jumps to
11055 // the real method. LOCATION is the location of the type definition.
11058 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
11061 if (methods
== NULL
)
11063 for (Methods::const_iterator p
= methods
->begin();
11064 p
!= methods
->end();
11067 Method
* m
= p
->second
;
11068 if (m
->is_ambiguous() || !m
->needs_stub_method())
11071 const std::string
& name(p
->first
);
11073 // Build a stub method.
11075 const Function_type
* fntype
= m
->type();
11077 static unsigned int counter
;
11079 snprintf(buf
, sizeof buf
, "$this%u", counter
);
11082 Type
* receiver_type
= const_cast<Type
*>(type
);
11083 if (!m
->is_value_method())
11084 receiver_type
= Type::make_pointer_type(receiver_type
);
11085 Location receiver_location
= m
->receiver_location();
11086 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
11087 receiver_location
);
11089 const Typed_identifier_list
* fnparams
= fntype
->parameters();
11090 Typed_identifier_list
* stub_params
;
11091 if (fnparams
== NULL
|| fnparams
->empty())
11092 stub_params
= NULL
;
11095 // We give each stub parameter a unique name.
11096 stub_params
= new Typed_identifier_list();
11097 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
11098 pp
!= fnparams
->end();
11102 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
11103 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
11109 const Typed_identifier_list
* fnresults
= fntype
->results();
11110 Typed_identifier_list
* stub_results
;
11111 if (fnresults
== NULL
|| fnresults
->empty())
11112 stub_results
= NULL
;
11115 // We create the result parameters without any names, since
11116 // we won't refer to them.
11117 stub_results
= new Typed_identifier_list();
11118 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
11119 pr
!= fnresults
->end();
11121 stub_results
->push_back(Typed_identifier("", pr
->type(),
11125 Function_type
* stub_type
= Type::make_function_type(receiver
,
11128 fntype
->location());
11129 if (fntype
->is_varargs())
11130 stub_type
->set_is_varargs();
11132 // We only create the function in the package which creates the
11134 const Package
* package
;
11135 if (type
->named_type() == NULL
)
11138 package
= type
->named_type()->named_object()->package();
11139 std::string stub_name
= gogo
->stub_method_name(name
);
11140 Named_object
* stub
;
11141 if (package
!= NULL
)
11142 stub
= Named_object::make_function_declaration(stub_name
, package
,
11143 stub_type
, location
);
11146 stub
= gogo
->start_function(stub_name
, stub_type
, false,
11147 fntype
->location());
11148 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
11149 fntype
->is_varargs(), location
);
11150 gogo
->finish_function(fntype
->location());
11152 if (type
->named_type() == NULL
&& stub
->is_function())
11153 stub
->func_value()->set_is_unnamed_type_stub_method();
11154 if (m
->nointerface() && stub
->is_function())
11155 stub
->func_value()->set_nointerface();
11158 m
->set_stub_object(stub
);
11162 // Build a stub method which adjusts the receiver as required to call
11163 // METHOD. RECEIVER_NAME is the name we used for the receiver.
11164 // PARAMS is the list of function parameters.
11167 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
11168 const char* receiver_name
,
11169 const Typed_identifier_list
* params
,
11173 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
11174 go_assert(receiver_object
!= NULL
);
11176 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
11177 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
11178 if (expr
->type()->points_to() == NULL
)
11179 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11181 Expression_list
* arguments
;
11182 if (params
== NULL
|| params
->empty())
11186 arguments
= new Expression_list();
11187 for (Typed_identifier_list::const_iterator p
= params
->begin();
11188 p
!= params
->end();
11191 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
11192 go_assert(param
!= NULL
);
11193 Expression
* param_ref
= Expression::make_var_reference(param
,
11195 arguments
->push_back(param_ref
);
11199 Expression
* func
= method
->bind_method(expr
, location
);
11200 go_assert(func
!= NULL
);
11201 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
11204 gogo
->add_statement(Statement::make_return_from_call(call
, location
));
11207 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
11208 // in reverse order.
11211 Type::apply_field_indexes(Expression
* expr
,
11212 const Method::Field_indexes
* field_indexes
,
11215 if (field_indexes
== NULL
)
11217 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
11218 Struct_type
* stype
= expr
->type()->deref()->struct_type();
11219 go_assert(stype
!= NULL
11220 && field_indexes
->field_index
< stype
->field_count());
11221 if (expr
->type()->struct_type() == NULL
)
11223 go_assert(expr
->type()->points_to() != NULL
);
11224 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
11225 go_assert(expr
->type()->struct_type() == stype
);
11227 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
11231 // Return whether NO is a method for which the receiver is a pointer.
11234 Type::method_expects_pointer(const Named_object
* no
)
11236 const Function_type
*fntype
;
11237 if (no
->is_function())
11238 fntype
= no
->func_value()->type();
11239 else if (no
->is_function_declaration())
11240 fntype
= no
->func_declaration_value()->type();
11243 return fntype
->receiver()->type()->points_to() != NULL
;
11246 // Given a set of methods for a type, METHODS, return the method NAME,
11247 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
11248 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
11249 // but is ambiguous (and return NULL).
11252 Type::method_function(const Methods
* methods
, const std::string
& name
,
11253 bool* is_ambiguous
)
11255 if (is_ambiguous
!= NULL
)
11256 *is_ambiguous
= false;
11257 if (methods
== NULL
)
11259 Methods::const_iterator p
= methods
->find(name
);
11260 if (p
== methods
->end())
11262 Method
* m
= p
->second
;
11263 if (m
->is_ambiguous())
11265 if (is_ambiguous
!= NULL
)
11266 *is_ambiguous
= true;
11272 // Return a pointer to the interface method table for TYPE for the
11273 // interface INTERFACE.
11276 Type::interface_method_table(Type
* type
,
11277 Interface_type
*interface
,
11279 Interface_method_tables
** method_tables
,
11280 Interface_method_tables
** pointer_tables
)
11282 go_assert(!interface
->is_empty());
11284 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
11287 *pimt
= new Interface_method_tables(5);
11289 std::pair
<Interface_type
*, Expression
*> val(interface
, NULL
);
11290 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
11292 Location loc
= Linemap::predeclared_location();
11295 // This is a new entry in the hash table.
11296 go_assert(ins
.first
->second
== NULL
);
11297 ins
.first
->second
=
11298 Expression::make_interface_mtable_ref(interface
, type
, is_pointer
, loc
);
11300 return Expression::make_unary(OPERATOR_AND
, ins
.first
->second
, loc
);
11303 // Look for field or method NAME for TYPE. Return an Expression for
11304 // the field or method bound to EXPR. If there is no such field or
11305 // method, give an appropriate error and return an error expression.
11308 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
11309 const std::string
& name
,
11312 if (type
->deref()->is_error_type())
11313 return Expression::make_error(location
);
11315 const Named_type
* nt
= type
->deref()->named_type();
11316 const Struct_type
* st
= type
->deref()->struct_type();
11317 const Interface_type
* it
= type
->interface_type();
11319 // If this is a pointer to a pointer, then it is possible that the
11320 // pointed-to type has methods.
11321 bool dereferenced
= false;
11325 && type
->points_to() != NULL
11326 && type
->points_to()->points_to() != NULL
)
11328 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
11329 type
= type
->points_to();
11330 if (type
->deref()->is_error_type())
11331 return Expression::make_error(location
);
11332 nt
= type
->points_to()->named_type();
11333 st
= type
->points_to()->struct_type();
11334 dereferenced
= true;
11337 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
11338 || expr
->is_addressable());
11339 std::vector
<const Named_type
*> seen
;
11340 bool is_method
= false;
11341 bool found_pointer_method
= false;
11342 std::string ambig1
;
11343 std::string ambig2
;
11344 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
11345 &seen
, NULL
, &is_method
,
11346 &found_pointer_method
, &ambig1
, &ambig2
))
11351 go_assert(st
!= NULL
);
11352 if (type
->struct_type() == NULL
)
11356 go_error_at(location
, "pointer type has no field %qs",
11357 Gogo::message_name(name
).c_str());
11358 return Expression::make_error(location
);
11360 go_assert(type
->points_to() != NULL
);
11361 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
11363 go_assert(expr
->type()->struct_type() == st
);
11365 ret
= st
->field_reference(expr
, name
, location
);
11368 go_error_at(location
, "type has no field %qs",
11369 Gogo::message_name(name
).c_str());
11370 return Expression::make_error(location
);
11373 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11374 ret
= Expression::make_interface_field_reference(expr
, name
,
11380 m
= nt
->method_function(name
, NULL
);
11381 else if (st
!= NULL
)
11382 m
= st
->method_function(name
, NULL
);
11385 go_assert(m
!= NULL
);
11388 go_error_at(location
,
11389 "calling method %qs requires explicit dereference",
11390 Gogo::message_name(name
).c_str());
11391 return Expression::make_error(location
);
11393 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
11394 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11395 ret
= m
->bind_method(expr
, location
);
11397 go_assert(ret
!= NULL
);
11402 if (Gogo::is_erroneous_name(name
))
11404 // An error was already reported.
11406 else if (!ambig1
.empty())
11407 go_error_at(location
, "%qs is ambiguous via %qs and %qs",
11408 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
11410 else if (found_pointer_method
)
11411 go_error_at(location
, "method requires a pointer receiver");
11412 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
11413 go_error_at(location
,
11414 ("reference to field %qs in object which "
11415 "has no fields or methods"),
11416 Gogo::message_name(name
).c_str());
11419 bool is_unexported
;
11420 // The test for 'a' and 'z' is to handle builtin names,
11421 // which are not hidden.
11422 if (!Gogo::is_hidden_name(name
) && (name
[0] < 'a' || name
[0] > 'z'))
11423 is_unexported
= false;
11426 std::string unpacked
= Gogo::unpack_hidden_name(name
);
11428 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
11433 go_error_at(location
, "reference to unexported field or method %qs",
11434 Gogo::message_name(name
).c_str());
11436 go_error_at(location
, "reference to undefined field or method %qs",
11437 Gogo::message_name(name
).c_str());
11439 return Expression::make_error(location
);
11443 // Look in TYPE for a field or method named NAME, return true if one
11444 // is found. This looks through embedded anonymous fields and handles
11445 // ambiguity. If a method is found, sets *IS_METHOD to true;
11446 // otherwise, if a field is found, set it to false. If
11447 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
11448 // whose address can not be taken. SEEN is used to avoid infinite
11449 // recursion on invalid types.
11451 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
11452 // method we couldn't use because it requires a pointer. LEVEL is
11453 // used for recursive calls, and can be NULL for a non-recursive call.
11454 // When this function returns false because it finds that the name is
11455 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
11456 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
11457 // will be unchanged.
11459 // This function just returns whether or not there is a field or
11460 // method, and whether it is a field or method. It doesn't build an
11461 // expression to refer to it. If it is a method, we then look in the
11462 // list of all methods for the type. If it is a field, the search has
11463 // to be done again, looking only for fields, and building up the
11464 // expression as we go.
11467 Type::find_field_or_method(const Type
* type
,
11468 const std::string
& name
,
11469 bool receiver_can_be_pointer
,
11470 std::vector
<const Named_type
*>* seen
,
11473 bool* found_pointer_method
,
11474 std::string
* ambig1
,
11475 std::string
* ambig2
)
11477 // Named types can have locally defined methods.
11478 const Named_type
* nt
= type
->named_type();
11479 if (nt
== NULL
&& type
->points_to() != NULL
)
11480 nt
= type
->points_to()->named_type();
11483 Named_object
* no
= nt
->find_local_method(name
);
11486 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
11492 // Record that we have found a pointer method in order to
11493 // give a better error message if we don't find anything
11495 *found_pointer_method
= true;
11498 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11504 // We've already seen this type when searching for methods.
11510 // Interface types can have methods.
11511 const Interface_type
* it
= type
->interface_type();
11512 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11518 // Struct types can have fields. They can also inherit fields and
11519 // methods from anonymous fields.
11520 const Struct_type
* st
= type
->deref()->struct_type();
11523 const Struct_field_list
* fields
= st
->fields();
11524 if (fields
== NULL
)
11528 seen
->push_back(nt
);
11530 int found_level
= 0;
11531 bool found_is_method
= false;
11532 std::string found_ambig1
;
11533 std::string found_ambig2
;
11534 const Struct_field
* found_parent
= NULL
;
11535 for (Struct_field_list::const_iterator pf
= fields
->begin();
11536 pf
!= fields
->end();
11539 if (pf
->is_field_name(name
))
11541 *is_method
= false;
11547 if (!pf
->is_anonymous())
11550 if (pf
->type()->deref()->is_error_type()
11551 || pf
->type()->deref()->is_undefined())
11554 Named_type
* fnt
= pf
->type()->named_type();
11556 fnt
= pf
->type()->deref()->named_type();
11557 go_assert(fnt
!= NULL
);
11559 // Methods with pointer receivers on embedded field are
11560 // inherited by the pointer to struct, and also by the struct
11561 // type if the field itself is a pointer.
11562 bool can_be_pointer
= (receiver_can_be_pointer
11563 || pf
->type()->points_to() != NULL
);
11564 int sublevel
= level
== NULL
? 1 : *level
+ 1;
11565 bool sub_is_method
;
11566 std::string subambig1
;
11567 std::string subambig2
;
11568 bool subfound
= Type::find_field_or_method(fnt
,
11574 found_pointer_method
,
11579 if (!subambig1
.empty())
11581 // The name was found via this field, but is ambiguous.
11582 // if the ambiguity is lower or at the same level as
11583 // anything else we have already found, then we want to
11584 // pass the ambiguity back to the caller.
11585 if (found_level
== 0 || sublevel
<= found_level
)
11587 found_ambig1
= (Gogo::message_name(pf
->field_name())
11588 + '.' + subambig1
);
11589 found_ambig2
= (Gogo::message_name(pf
->field_name())
11590 + '.' + subambig2
);
11591 found_level
= sublevel
;
11597 // The name was found via this field. Use the level to see
11598 // if we want to use this one, or whether it introduces an
11600 if (found_level
== 0 || sublevel
< found_level
)
11602 found_level
= sublevel
;
11603 found_is_method
= sub_is_method
;
11604 found_ambig1
.clear();
11605 found_ambig2
.clear();
11606 found_parent
= &*pf
;
11608 else if (sublevel
> found_level
)
11610 else if (found_ambig1
.empty())
11612 // We found an ambiguity.
11613 go_assert(found_parent
!= NULL
);
11614 found_ambig1
= Gogo::message_name(found_parent
->field_name());
11615 found_ambig2
= Gogo::message_name(pf
->field_name());
11619 // We found an ambiguity, but we already know of one.
11620 // Just report the earlier one.
11625 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
11626 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
11627 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
11628 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
11633 if (found_level
== 0)
11635 else if (found_is_method
11636 && type
->named_type() != NULL
11637 && type
->points_to() != NULL
)
11639 // If this is a method inherited from a struct field in a named pointer
11640 // type, it is invalid to automatically dereference the pointer to the
11641 // struct to find this method.
11643 *level
= found_level
;
11647 else if (!found_ambig1
.empty())
11649 go_assert(!found_ambig1
.empty());
11650 ambig1
->assign(found_ambig1
);
11651 ambig2
->assign(found_ambig2
);
11653 *level
= found_level
;
11659 *level
= found_level
;
11660 *is_method
= found_is_method
;
11665 // Return whether NAME is an unexported field or method for TYPE.
11668 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
11669 const std::string
& name
,
11670 std::vector
<const Named_type
*>* seen
)
11672 const Named_type
* nt
= type
->named_type();
11674 nt
= type
->deref()->named_type();
11677 if (nt
->is_unexported_local_method(gogo
, name
))
11680 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11686 // We've already seen this type.
11692 const Interface_type
* it
= type
->interface_type();
11693 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
11696 type
= type
->deref();
11698 const Struct_type
* st
= type
->struct_type();
11699 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
11705 const Struct_field_list
* fields
= st
->fields();
11706 if (fields
== NULL
)
11710 seen
->push_back(nt
);
11712 for (Struct_field_list::const_iterator pf
= fields
->begin();
11713 pf
!= fields
->end();
11716 if (pf
->is_anonymous()
11717 && !pf
->type()->deref()->is_error_type()
11718 && !pf
->type()->deref()->is_undefined())
11720 Named_type
* subtype
= pf
->type()->named_type();
11721 if (subtype
== NULL
)
11722 subtype
= pf
->type()->deref()->named_type();
11723 if (subtype
== NULL
)
11725 // This is an error, but it will be diagnosed elsewhere.
11728 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
11743 // Class Forward_declaration.
11745 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
11746 : Type(TYPE_FORWARD
),
11747 named_object_(named_object
->resolve()), warned_(false)
11749 go_assert(this->named_object_
->is_unknown()
11750 || this->named_object_
->is_type_declaration());
11753 // Return the named object.
11756 Forward_declaration_type::named_object()
11758 return this->named_object_
->resolve();
11761 const Named_object
*
11762 Forward_declaration_type::named_object() const
11764 return this->named_object_
->resolve();
11767 // Return the name of the forward declared type.
11770 Forward_declaration_type::name() const
11772 return this->named_object()->name();
11775 // Warn about a use of a type which has been declared but not defined.
11778 Forward_declaration_type::warn() const
11780 Named_object
* no
= this->named_object_
->resolve();
11781 if (no
->is_unknown())
11783 // The name was not defined anywhere.
11784 if (!this->warned_
)
11786 go_error_at(this->named_object_
->location(),
11787 "use of undefined type %qs",
11788 no
->message_name().c_str());
11789 this->warned_
= true;
11792 else if (no
->is_type_declaration())
11794 // The name was seen as a type, but the type was never defined.
11795 if (no
->type_declaration_value()->using_type())
11797 go_error_at(this->named_object_
->location(),
11798 "use of undefined type %qs",
11799 no
->message_name().c_str());
11800 this->warned_
= true;
11805 // The name was defined, but not as a type.
11806 if (!this->warned_
)
11808 go_error_at(this->named_object_
->location(), "expected type");
11809 this->warned_
= true;
11814 // Get the base type of a declaration. This gives an error if the
11815 // type has not yet been defined.
11818 Forward_declaration_type::real_type()
11820 if (this->is_defined())
11822 Named_type
* nt
= this->named_object()->type_value();
11823 if (!nt
->is_valid())
11824 return Type::make_error_type();
11825 return this->named_object()->type_value();
11830 return Type::make_error_type();
11835 Forward_declaration_type::real_type() const
11837 if (this->is_defined())
11839 const Named_type
* nt
= this->named_object()->type_value();
11840 if (!nt
->is_valid())
11841 return Type::make_error_type();
11842 return this->named_object()->type_value();
11847 return Type::make_error_type();
11851 // Return whether the base type is defined.
11854 Forward_declaration_type::is_defined() const
11856 return this->named_object()->is_type();
11859 // Add a method. This is used when methods are defined before the
11863 Forward_declaration_type::add_method(const std::string
& name
,
11864 Function
* function
)
11866 Named_object
* no
= this->named_object();
11867 if (no
->is_unknown())
11868 no
->declare_as_type();
11869 return no
->type_declaration_value()->add_method(name
, function
);
11872 // Add a method declaration. This is used when methods are declared
11873 // before the type.
11876 Forward_declaration_type::add_method_declaration(const std::string
& name
,
11878 Function_type
* type
,
11881 Named_object
* no
= this->named_object();
11882 if (no
->is_unknown())
11883 no
->declare_as_type();
11884 Type_declaration
* td
= no
->type_declaration_value();
11885 return td
->add_method_declaration(name
, package
, type
, location
);
11888 // Add an already created object as a method.
11891 Forward_declaration_type::add_existing_method(Named_object
* nom
)
11893 Named_object
* no
= this->named_object();
11894 if (no
->is_unknown())
11895 no
->declare_as_type();
11896 no
->type_declaration_value()->add_existing_method(nom
);
11902 Forward_declaration_type::do_traverse(Traverse
* traverse
)
11904 if (this->is_defined()
11905 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
11906 return TRAVERSE_EXIT
;
11907 return TRAVERSE_CONTINUE
;
11910 // Verify the type.
11913 Forward_declaration_type::do_verify()
11915 if (!this->is_defined() && !this->is_nil_constant_as_type())
11923 // Get the backend representation for the type.
11926 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
11928 if (this->is_defined())
11929 return Type::get_named_base_btype(gogo
, this->real_type());
11932 return gogo
->backend()->error_type();
11934 // We represent an undefined type as a struct with no fields. That
11935 // should work fine for the backend, since the same case can arise
11937 std::vector
<Backend::Btyped_identifier
> fields
;
11938 Btype
* bt
= gogo
->backend()->struct_type(fields
);
11939 return gogo
->backend()->named_type(this->name(), bt
,
11940 this->named_object()->location());
11943 // Build a type descriptor for a forwarded type.
11946 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
11948 Location ploc
= Linemap::predeclared_location();
11949 if (!this->is_defined())
11950 return Expression::make_error(ploc
);
11953 Type
* t
= this->real_type();
11955 return this->named_type_descriptor(gogo
, t
, name
);
11957 return Expression::make_error(this->named_object_
->location());
11961 // The reflection string.
11964 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
11966 this->append_reflection(this->real_type(), gogo
, ret
);
11969 // Export a forward declaration. This can happen when a defined type
11970 // refers to a type which is only declared (and is presumably defined
11971 // in some other file in the same package).
11974 Forward_declaration_type::do_export(Export
*) const
11976 // If there is a base type, that should be exported instead of this.
11977 go_assert(!this->is_defined());
11979 // We don't output anything.
11982 // Make a forward declaration.
11985 Type::make_forward_declaration(Named_object
* named_object
)
11987 return new Forward_declaration_type(named_object
);
11990 // Class Typed_identifier_list.
11992 // Sort the entries by name.
11994 struct Typed_identifier_list_sort
11998 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
12000 return (Gogo::unpack_hidden_name(t1
.name())
12001 < Gogo::unpack_hidden_name(t2
.name()));
12006 Typed_identifier_list::sort_by_name()
12008 std::sort(this->entries_
.begin(), this->entries_
.end(),
12009 Typed_identifier_list_sort());
12015 Typed_identifier_list::traverse(Traverse
* traverse
)
12017 for (Typed_identifier_list::const_iterator p
= this->begin();
12021 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
12022 return TRAVERSE_EXIT
;
12024 return TRAVERSE_CONTINUE
;
12029 Typed_identifier_list
*
12030 Typed_identifier_list::copy() const
12032 Typed_identifier_list
* ret
= new Typed_identifier_list();
12033 for (Typed_identifier_list::const_iterator p
= this->begin();
12036 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));