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_
, location
);
1210 Bexpression
* var_addr
=
1211 gogo
->backend()->address_expression(var_expr
, location
);
1212 Type
* td_type
= Type::make_type_descriptor_type();
1213 Btype
* td_btype
= td_type
->get_backend(gogo
);
1214 Btype
* ptd_btype
= gogo
->backend()->pointer_type(td_btype
);
1215 return gogo
->backend()->convert_expression(ptd_btype
, var_addr
, location
);
1218 // A mapping from unnamed types to type descriptor variables.
1220 Type::Type_descriptor_vars
Type::type_descriptor_vars
;
1222 // Build the type descriptor for this type.
1225 Type::make_type_descriptor_var(Gogo
* gogo
)
1227 go_assert(this->type_descriptor_var_
== NULL
);
1229 Named_type
* nt
= this->named_type();
1231 // We can have multiple instances of unnamed types, but we only want
1232 // to emit the type descriptor once. We use a hash table. This is
1233 // not necessary for named types, as they are unique, and we store
1234 // the type descriptor in the type itself.
1235 Bvariable
** phash
= NULL
;
1238 Bvariable
* bvnull
= NULL
;
1239 std::pair
<Type_descriptor_vars::iterator
, bool> ins
=
1240 Type::type_descriptor_vars
.insert(std::make_pair(this, bvnull
));
1243 // We've already built a type descriptor for this type.
1244 this->type_descriptor_var_
= ins
.first
->second
;
1247 phash
= &ins
.first
->second
;
1250 // The type descriptor symbol for the unsafe.Pointer type is defined in
1251 // libgo/go-unsafe-pointer.c, so we just return a reference to that
1252 // symbol if necessary.
1253 if (this->is_unsafe_pointer_type())
1255 Location bloc
= Linemap::predeclared_location();
1257 Type
* td_type
= Type::make_type_descriptor_type();
1258 Btype
* td_btype
= td_type
->get_backend(gogo
);
1259 std::string name
= gogo
->type_descriptor_name(this, nt
);
1260 std::string
asm_name(go_selectively_encode_id(name
));
1261 this->type_descriptor_var_
=
1262 gogo
->backend()->immutable_struct_reference(name
, asm_name
,
1267 *phash
= this->type_descriptor_var_
;
1271 std::string var_name
= gogo
->type_descriptor_name(this, nt
);
1273 // Build the contents of the type descriptor.
1274 Expression
* initializer
= this->do_type_descriptor(gogo
, NULL
);
1276 Btype
* initializer_btype
= initializer
->type()->get_backend(gogo
);
1278 Location loc
= nt
== NULL
? Linemap::predeclared_location() : nt
->location();
1280 const Package
* dummy
;
1281 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
1283 std::string
asm_name(go_selectively_encode_id(var_name
));
1284 this->type_descriptor_var_
=
1285 gogo
->backend()->immutable_struct_reference(var_name
, asm_name
,
1289 *phash
= this->type_descriptor_var_
;
1293 // See if this type descriptor can appear in multiple packages.
1294 bool is_common
= false;
1297 // We create the descriptor for a builtin type whenever we need
1299 is_common
= nt
->is_builtin();
1303 // This is an unnamed type. The descriptor could be defined in
1304 // any package where it is needed, and the linker will pick one
1305 // descriptor to keep.
1309 // We are going to build the type descriptor in this package. We
1310 // must create the variable before we convert the initializer to the
1311 // backend representation, because the initializer may refer to the
1312 // type descriptor of this type. By setting type_descriptor_var_ we
1313 // ensure that type_descriptor_pointer will work if called while
1314 // converting INITIALIZER.
1316 std::string
asm_name(go_selectively_encode_id(var_name
));
1317 this->type_descriptor_var_
=
1318 gogo
->backend()->immutable_struct(var_name
, asm_name
, false, is_common
,
1319 initializer_btype
, loc
);
1321 *phash
= this->type_descriptor_var_
;
1323 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
1324 context
.set_is_const();
1325 Bexpression
* binitializer
= initializer
->get_backend(&context
);
1327 gogo
->backend()->immutable_struct_set_init(this->type_descriptor_var_
,
1328 var_name
, false, is_common
,
1329 initializer_btype
, loc
,
1333 // Return true if this type descriptor is defined in a different
1334 // package. If this returns true it sets *PACKAGE to the package.
1337 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1338 const Package
** package
)
1342 if (nt
->named_object()->package() != NULL
)
1344 // This is a named type defined in a different package. The
1345 // type descriptor should be defined in that package.
1346 *package
= nt
->named_object()->package();
1352 if (this->points_to() != NULL
1353 && this->points_to()->named_type() != NULL
1354 && this->points_to()->named_type()->named_object()->package() != NULL
)
1356 // This is an unnamed pointer to a named type defined in a
1357 // different package. The descriptor should be defined in
1359 *package
= this->points_to()->named_type()->named_object()->package();
1366 // Return a composite literal for a type descriptor.
1369 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1371 return type
->do_type_descriptor(gogo
, NULL
);
1374 // Return a composite literal for a type descriptor with a name.
1377 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1379 go_assert(name
!= NULL
&& type
->named_type() != name
);
1380 return type
->do_type_descriptor(gogo
, name
);
1383 // Make a builtin struct type from a list of fields. The fields are
1384 // pairs of a name and a type.
1387 Type::make_builtin_struct_type(int nfields
, ...)
1390 va_start(ap
, nfields
);
1392 Location bloc
= Linemap::predeclared_location();
1393 Struct_field_list
* sfl
= new Struct_field_list();
1394 for (int i
= 0; i
< nfields
; i
++)
1396 const char* field_name
= va_arg(ap
, const char *);
1397 Type
* type
= va_arg(ap
, Type
*);
1398 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1403 Struct_type
* ret
= Type::make_struct_type(sfl
, bloc
);
1404 ret
->set_is_struct_incomparable();
1408 // A list of builtin named types.
1410 std::vector
<Named_type
*> Type::named_builtin_types
;
1412 // Make a builtin named type.
1415 Type::make_builtin_named_type(const char* name
, Type
* type
)
1417 Location bloc
= Linemap::predeclared_location();
1418 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1419 Named_type
* ret
= no
->type_value();
1420 Type::named_builtin_types
.push_back(ret
);
1424 // Convert the named builtin types.
1427 Type::convert_builtin_named_types(Gogo
* gogo
)
1429 for (std::vector
<Named_type
*>::const_iterator p
=
1430 Type::named_builtin_types
.begin();
1431 p
!= Type::named_builtin_types
.end();
1434 bool r
= (*p
)->verify();
1436 (*p
)->convert(gogo
);
1440 // Return the type of a type descriptor. We should really tie this to
1441 // runtime.Type rather than copying it. This must match the struct "_type"
1442 // declared in libgo/go/runtime/type.go.
1445 Type::make_type_descriptor_type()
1450 Location bloc
= Linemap::predeclared_location();
1452 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1453 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
1454 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1455 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1456 Type
* string_type
= Type::lookup_string_type();
1457 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1459 // This is an unnamed version of unsafe.Pointer. Perhaps we
1460 // should use the named version instead, although that would
1461 // require us to create the unsafe package if it has not been
1462 // imported. It probably doesn't matter.
1463 Type
* void_type
= Type::make_void_type();
1464 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1466 Typed_identifier_list
*params
= new Typed_identifier_list();
1467 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
1468 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1470 Typed_identifier_list
* results
= new Typed_identifier_list();
1471 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1473 Type
* hash_fntype
= Type::make_function_type(NULL
, params
, results
,
1476 params
= new Typed_identifier_list();
1477 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
1478 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
1480 results
= new Typed_identifier_list();
1481 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1483 Type
* equal_fntype
= Type::make_function_type(NULL
, params
, results
,
1486 // Forward declaration for the type descriptor type.
1487 Named_object
* named_type_descriptor_type
=
1488 Named_object::make_type_declaration("_type", NULL
, bloc
);
1489 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1490 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1492 // The type of a method on a concrete type.
1493 Struct_type
* method_type
=
1494 Type::make_builtin_struct_type(5,
1495 "name", pointer_string_type
,
1496 "pkgPath", pointer_string_type
,
1497 "mtyp", pointer_type_descriptor_type
,
1498 "typ", pointer_type_descriptor_type
,
1499 "tfn", unsafe_pointer_type
);
1500 Named_type
* named_method_type
=
1501 Type::make_builtin_named_type("method", method_type
);
1503 // Information for types with a name or methods.
1504 Type
* slice_named_method_type
=
1505 Type::make_array_type(named_method_type
, NULL
);
1506 Struct_type
* uncommon_type
=
1507 Type::make_builtin_struct_type(3,
1508 "name", pointer_string_type
,
1509 "pkgPath", pointer_string_type
,
1510 "methods", slice_named_method_type
);
1511 Named_type
* named_uncommon_type
=
1512 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1514 Type
* pointer_uncommon_type
=
1515 Type::make_pointer_type(named_uncommon_type
);
1517 // The type descriptor type.
1519 Struct_type
* type_descriptor_type
=
1520 Type::make_builtin_struct_type(12,
1521 "size", uintptr_type
,
1522 "ptrdata", uintptr_type
,
1523 "hash", uint32_type
,
1525 "align", uint8_type
,
1526 "fieldAlign", uint8_type
,
1527 "hashfn", hash_fntype
,
1528 "equalfn", equal_fntype
,
1529 "gcdata", pointer_uint8_type
,
1530 "string", pointer_string_type
,
1531 "", pointer_uncommon_type
,
1533 pointer_type_descriptor_type
);
1535 Named_type
* named
= Type::make_builtin_named_type("_type",
1536 type_descriptor_type
);
1538 named_type_descriptor_type
->set_type_value(named
);
1546 // Make the type of a pointer to a type descriptor as represented in
1550 Type::make_type_descriptor_ptr_type()
1554 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1558 // Return the alignment required by the memequalN function. N is a
1559 // type size: 16, 32, 64, or 128. The memequalN functions are defined
1560 // in libgo/go/runtime/alg.go.
1563 Type::memequal_align(Gogo
* gogo
, int size
)
1578 // The code uses [2]int64, which must have the same alignment as
1586 Type
* t
= Type::lookup_integer_type(tn
);
1589 if (!t
->backend_type_align(gogo
, &ret
))
1594 // Return whether this type needs specially built type functions.
1595 // This returns true for types that are comparable and either can not
1596 // use an identity comparison, or are a non-standard size.
1599 Type::needs_specific_type_functions(Gogo
* gogo
)
1601 Named_type
* nt
= this->named_type();
1602 if (nt
!= NULL
&& nt
->is_alias())
1604 if (!this->is_comparable())
1606 if (!this->compare_is_identity(gogo
))
1609 // We create a few predeclared types for type descriptors; they are
1610 // really just for the backend and don't need hash or equality
1612 if (nt
!= NULL
&& Linemap::is_predeclared_location(nt
->location()))
1615 int64_t size
, align
;
1616 if (!this->backend_type_size(gogo
, &size
)
1617 || !this->backend_type_align(gogo
, &align
))
1619 go_assert(saw_errors());
1622 // This switch matches the one in Type::type_functions.
1628 return align
< Type::memequal_align(gogo
, 16);
1630 return align
< Type::memequal_align(gogo
, 32);
1632 return align
< Type::memequal_align(gogo
, 64);
1634 return align
< Type::memequal_align(gogo
, 128);
1640 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1641 // hash code for this type and which compare whether two values of
1642 // this type are equal. If NAME is not NULL it is the name of this
1643 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1644 // functions, for convenience; they may be NULL.
1647 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1648 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1649 Named_object
** equal_fn
)
1651 // If this loop leaves NAME as NULL, then the type does not have a
1653 while (name
!= NULL
&& name
->is_alias())
1654 name
= name
->real_type()->named_type();
1656 if (!this->is_comparable())
1663 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1665 Location bloc
= Linemap::predeclared_location();
1667 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1668 Type
* void_type
= Type::make_void_type();
1669 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1671 if (hash_fntype
== NULL
)
1673 Typed_identifier_list
* params
= new Typed_identifier_list();
1674 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1676 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1678 Typed_identifier_list
* results
= new Typed_identifier_list();
1679 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1681 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1683 if (equal_fntype
== NULL
)
1685 Typed_identifier_list
* params
= new Typed_identifier_list();
1686 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1688 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1691 Typed_identifier_list
* results
= new Typed_identifier_list();
1692 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1695 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1699 const char* hash_fnname
;
1700 const char* equal_fnname
;
1701 if (this->compare_is_identity(gogo
))
1703 int64_t size
, align
;
1704 if (!this->backend_type_size(gogo
, &size
)
1705 || !this->backend_type_align(gogo
, &align
))
1707 go_assert(saw_errors());
1710 bool build_functions
= false;
1711 // This switch matches the one in Type::needs_specific_type_functions.
1712 // The alignment tests are because of the memequal functions,
1713 // which assume that the values are aligned as required for an
1714 // integer of that size.
1718 hash_fnname
= "runtime.memhash0";
1719 equal_fnname
= "runtime.memequal0";
1722 hash_fnname
= "runtime.memhash8";
1723 equal_fnname
= "runtime.memequal8";
1726 if (align
< Type::memequal_align(gogo
, 16))
1727 build_functions
= true;
1730 hash_fnname
= "runtime.memhash16";
1731 equal_fnname
= "runtime.memequal16";
1735 if (align
< Type::memequal_align(gogo
, 32))
1736 build_functions
= true;
1739 hash_fnname
= "runtime.memhash32";
1740 equal_fnname
= "runtime.memequal32";
1744 if (align
< Type::memequal_align(gogo
, 64))
1745 build_functions
= true;
1748 hash_fnname
= "runtime.memhash64";
1749 equal_fnname
= "runtime.memequal64";
1753 if (align
< Type::memequal_align(gogo
, 128))
1754 build_functions
= true;
1757 hash_fnname
= "runtime.memhash128";
1758 equal_fnname
= "runtime.memequal128";
1762 build_functions
= true;
1765 if (build_functions
)
1767 // We don't have a built-in function for a type of this size
1768 // and alignment. Build a function to use that calls the
1769 // generic hash/equality functions for identity, passing the size.
1770 this->specific_type_functions(gogo
, name
, size
, hash_fntype
,
1771 equal_fntype
, hash_fn
, equal_fn
);
1777 switch (this->base()->classification())
1779 case Type::TYPE_ERROR
:
1780 case Type::TYPE_VOID
:
1781 case Type::TYPE_NIL
:
1782 case Type::TYPE_FUNCTION
:
1783 case Type::TYPE_MAP
:
1784 // For these types is_comparable should have returned false.
1787 case Type::TYPE_BOOLEAN
:
1788 case Type::TYPE_INTEGER
:
1789 case Type::TYPE_POINTER
:
1790 case Type::TYPE_CHANNEL
:
1791 // For these types compare_is_identity should have returned true.
1794 case Type::TYPE_FLOAT
:
1795 switch (this->float_type()->bits())
1798 hash_fnname
= "runtime.f32hash";
1799 equal_fnname
= "runtime.f32equal";
1802 hash_fnname
= "runtime.f64hash";
1803 equal_fnname
= "runtime.f64equal";
1810 case Type::TYPE_COMPLEX
:
1811 switch (this->complex_type()->bits())
1814 hash_fnname
= "runtime.c64hash";
1815 equal_fnname
= "runtime.c64equal";
1818 hash_fnname
= "runtime.c128hash";
1819 equal_fnname
= "runtime.c128equal";
1826 case Type::TYPE_STRING
:
1827 hash_fnname
= "runtime.strhash";
1828 equal_fnname
= "runtime.strequal";
1831 case Type::TYPE_STRUCT
:
1833 // This is a struct which can not be compared using a
1834 // simple identity function. We need to build a function
1836 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1837 equal_fntype
, hash_fn
, equal_fn
);
1841 case Type::TYPE_ARRAY
:
1842 if (this->is_slice_type())
1844 // Type::is_compatible_for_comparison should have
1850 // This is an array which can not be compared using a
1851 // simple identity function. We need to build a
1852 // function for comparison.
1853 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1854 equal_fntype
, hash_fn
, equal_fn
);
1859 case Type::TYPE_INTERFACE
:
1860 if (this->interface_type()->is_empty())
1862 hash_fnname
= "runtime.nilinterhash";
1863 equal_fnname
= "runtime.nilinterequal";
1867 hash_fnname
= "runtime.interhash";
1868 equal_fnname
= "runtime.interequal";
1872 case Type::TYPE_NAMED
:
1873 case Type::TYPE_FORWARD
:
1882 Location bloc
= Linemap::predeclared_location();
1883 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1885 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1886 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1887 equal_fntype
, bloc
);
1888 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1891 // A hash table mapping types to the specific hash functions.
1893 Type::Type_functions
Type::type_functions_table
;
1895 // Handle a type function which is specific to a type: if SIZE == -1,
1896 // this is a struct or array that can not use an identity comparison.
1897 // Otherwise, it is a type that uses an identity comparison but is not
1898 // one of the standard supported sizes.
1901 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1902 Function_type
* hash_fntype
,
1903 Function_type
* equal_fntype
,
1904 Named_object
** hash_fn
,
1905 Named_object
** equal_fn
)
1907 Hash_equal_fn
fnull(NULL
, NULL
);
1908 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1909 std::pair
<Type_functions::iterator
, bool> ins
=
1910 Type::type_functions_table
.insert(val
);
1913 // We already have functions for this type
1914 *hash_fn
= ins
.first
->second
.first
;
1915 *equal_fn
= ins
.first
->second
.second
;
1919 std::string hash_name
;
1920 std::string equal_name
;
1921 gogo
->specific_type_function_names(this, name
, &hash_name
, &equal_name
);
1923 Location bloc
= Linemap::predeclared_location();
1925 const Package
* package
= NULL
;
1926 bool is_defined_elsewhere
=
1927 this->type_descriptor_defined_elsewhere(name
, &package
);
1928 if (is_defined_elsewhere
)
1930 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
1932 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
1933 equal_fntype
, bloc
);
1937 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
1938 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
1942 ins
.first
->second
.first
= *hash_fn
;
1943 ins
.first
->second
.second
= *equal_fn
;
1945 if (!is_defined_elsewhere
)
1947 if (gogo
->in_global_scope())
1948 this->write_specific_type_functions(gogo
, name
, size
, hash_name
,
1949 hash_fntype
, equal_name
,
1952 gogo
->queue_specific_type_function(this, name
, size
, hash_name
,
1953 hash_fntype
, equal_name
,
1958 // Write the hash and equality functions for a type which needs to be
1959 // written specially.
1962 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1963 const std::string
& hash_name
,
1964 Function_type
* hash_fntype
,
1965 const std::string
& equal_name
,
1966 Function_type
* equal_fntype
)
1968 Location bloc
= Linemap::predeclared_location();
1970 if (gogo
->specific_type_functions_are_written())
1972 go_assert(saw_errors());
1976 go_assert(this->is_comparable());
1978 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
1980 hash_fn
->func_value()->set_is_type_specific_function();
1981 gogo
->start_block(bloc
);
1984 this->write_identity_hash(gogo
, size
);
1985 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
1986 this->write_named_hash(gogo
, name
, hash_fntype
, equal_fntype
);
1987 else if (this->struct_type() != NULL
)
1988 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
1990 else if (this->array_type() != NULL
)
1991 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
1996 Block
* b
= gogo
->finish_block(bloc
);
1997 gogo
->add_block(b
, bloc
);
1998 gogo
->lower_block(hash_fn
, b
);
1999 gogo
->finish_function(bloc
);
2001 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
2003 equal_fn
->func_value()->set_is_type_specific_function();
2004 gogo
->start_block(bloc
);
2007 this->write_identity_equal(gogo
, size
);
2008 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2009 this->write_named_equal(gogo
, name
);
2010 else if (this->struct_type() != NULL
)
2011 this->struct_type()->write_equal_function(gogo
, name
);
2012 else if (this->array_type() != NULL
)
2013 this->array_type()->write_equal_function(gogo
, name
);
2017 b
= gogo
->finish_block(bloc
);
2018 gogo
->add_block(b
, bloc
);
2019 gogo
->lower_block(equal_fn
, b
);
2020 gogo
->finish_function(bloc
);
2022 // Build the function descriptors for the type descriptor to refer to.
2023 hash_fn
->func_value()->descriptor(gogo
, hash_fn
);
2024 equal_fn
->func_value()->descriptor(gogo
, equal_fn
);
2027 // Write a hash function for a type that can use an identity hash but
2028 // is not one of the standard supported sizes. For example, this
2029 // would be used for the type [3]byte. This builds a return statement
2030 // that returns a call to the memhash function, passing the key and
2031 // seed from the function arguments (already constructed before this
2032 // is called), and the constant size.
2035 Type::write_identity_hash(Gogo
* gogo
, int64_t size
)
2037 Location bloc
= Linemap::predeclared_location();
2039 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2040 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2042 Typed_identifier_list
* params
= new Typed_identifier_list();
2043 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
2044 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
2045 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2047 Typed_identifier_list
* results
= new Typed_identifier_list();
2048 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
2050 Function_type
* memhash_fntype
= Type::make_function_type(NULL
, params
,
2053 Named_object
* memhash
=
2054 Named_object::make_function_declaration("runtime.memhash", NULL
,
2055 memhash_fntype
, bloc
);
2056 memhash
->func_declaration_value()->set_asm_name("runtime.memhash");
2058 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2059 go_assert(key_arg
!= NULL
);
2060 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2061 go_assert(seed_arg
!= NULL
);
2063 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2064 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2065 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2067 Expression_list
* args
= new Expression_list();
2068 args
->push_back(key_ref
);
2069 args
->push_back(seed_ref
);
2070 args
->push_back(size_arg
);
2071 Expression
* func
= Expression::make_func_reference(memhash
, NULL
, bloc
);
2072 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2074 Expression_list
* vals
= new Expression_list();
2075 vals
->push_back(call
);
2076 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2077 gogo
->add_statement(s
);
2080 // Write an equality function for a type that can use an identity
2081 // equality comparison but is not one of the standard supported sizes.
2082 // For example, this would be used for the type [3]byte. This builds
2083 // a return statement that returns a call to the memequal function,
2084 // passing the two keys from the function arguments (already
2085 // constructed before this is called), and the constant size.
2088 Type::write_identity_equal(Gogo
* gogo
, int64_t size
)
2090 Location bloc
= Linemap::predeclared_location();
2092 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2093 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2095 Typed_identifier_list
* params
= new Typed_identifier_list();
2096 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
2097 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
2098 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2100 Typed_identifier_list
* results
= new Typed_identifier_list();
2101 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
2103 Function_type
* memequal_fntype
= Type::make_function_type(NULL
, params
,
2106 Named_object
* memequal
=
2107 Named_object::make_function_declaration("runtime.memequal", NULL
,
2108 memequal_fntype
, bloc
);
2109 memequal
->func_declaration_value()->set_asm_name("runtime.memequal");
2111 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2112 go_assert(key1_arg
!= NULL
);
2113 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2114 go_assert(key2_arg
!= NULL
);
2116 Expression
* key1_ref
= Expression::make_var_reference(key1_arg
, bloc
);
2117 Expression
* key2_ref
= Expression::make_var_reference(key2_arg
, bloc
);
2118 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2120 Expression_list
* args
= new Expression_list();
2121 args
->push_back(key1_ref
);
2122 args
->push_back(key2_ref
);
2123 args
->push_back(size_arg
);
2124 Expression
* func
= Expression::make_func_reference(memequal
, NULL
, bloc
);
2125 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2127 Expression_list
* vals
= new Expression_list();
2128 vals
->push_back(call
);
2129 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2130 gogo
->add_statement(s
);
2133 // Write a hash function that simply calls the hash function for a
2134 // named type. This is used when one named type is defined as
2135 // another. This ensures that this case works when the other named
2136 // type is defined in another package and relies on calling hash
2137 // functions defined only in that package.
2140 Type::write_named_hash(Gogo
* gogo
, Named_type
* name
,
2141 Function_type
* hash_fntype
, Function_type
* equal_fntype
)
2143 Location bloc
= Linemap::predeclared_location();
2145 Named_type
* base_type
= name
->real_type()->named_type();
2146 while (base_type
->is_alias())
2148 base_type
= base_type
->real_type()->named_type();
2149 go_assert(base_type
!= NULL
);
2151 go_assert(base_type
!= NULL
);
2153 // The pointer to the type we are going to hash. This is an
2155 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2156 go_assert(key_arg
!= NULL
);
2158 // The seed argument to the hash function.
2159 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2160 go_assert(seed_arg
!= NULL
);
2162 Named_object
* hash_fn
;
2163 Named_object
* equal_fn
;
2164 name
->real_type()->type_functions(gogo
, base_type
, hash_fntype
, equal_fntype
,
2165 &hash_fn
, &equal_fn
);
2167 // Call the hash function for the base type.
2168 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2169 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2170 Expression_list
* args
= new Expression_list();
2171 args
->push_back(key_ref
);
2172 args
->push_back(seed_ref
);
2173 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
2174 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2176 // Return the hash of the base type.
2177 Expression_list
* vals
= new Expression_list();
2178 vals
->push_back(call
);
2179 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2180 gogo
->add_statement(s
);
2183 // Write an equality function that simply calls the equality function
2184 // for a named type. This is used when one named type is defined as
2185 // another. This ensures that this case works when the other named
2186 // type is defined in another package and relies on calling equality
2187 // functions defined only in that package.
2190 Type::write_named_equal(Gogo
* gogo
, Named_type
* name
)
2192 Location bloc
= Linemap::predeclared_location();
2194 // The pointers to the types we are going to compare. These have
2195 // type unsafe.Pointer.
2196 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2197 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2198 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
2200 Named_type
* base_type
= name
->real_type()->named_type();
2201 go_assert(base_type
!= NULL
);
2203 // Build temporaries with the base type.
2204 Type
* pt
= Type::make_pointer_type(base_type
);
2206 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
2207 ref
= Expression::make_cast(pt
, ref
, bloc
);
2208 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
2209 gogo
->add_statement(p1
);
2211 ref
= Expression::make_var_reference(key2_arg
, bloc
);
2212 ref
= Expression::make_cast(pt
, ref
, bloc
);
2213 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
2214 gogo
->add_statement(p2
);
2216 // Compare the values for equality.
2217 Expression
* t1
= Expression::make_temporary_reference(p1
, bloc
);
2218 t1
= Expression::make_unary(OPERATOR_MULT
, t1
, bloc
);
2220 Expression
* t2
= Expression::make_temporary_reference(p2
, bloc
);
2221 t2
= Expression::make_unary(OPERATOR_MULT
, t2
, bloc
);
2223 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, t1
, t2
, bloc
);
2225 // Return the equality comparison.
2226 Expression_list
* vals
= new Expression_list();
2227 vals
->push_back(cond
);
2228 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2229 gogo
->add_statement(s
);
2232 // Return a composite literal for the type descriptor for a plain type
2233 // of kind RUNTIME_TYPE_KIND named NAME.
2236 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
2237 Named_type
* name
, const Methods
* methods
,
2238 bool only_value_methods
)
2240 Location bloc
= Linemap::predeclared_location();
2242 Type
* td_type
= Type::make_type_descriptor_type();
2243 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
2245 Expression_list
* vals
= new Expression_list();
2248 if (!this->has_pointer())
2249 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
2250 if (this->points_to() != NULL
)
2251 runtime_type_kind
|= RUNTIME_TYPE_KIND_DIRECT_IFACE
;
2254 if (this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2255 runtime_type_kind
|= RUNTIME_TYPE_KIND_GC_PROG
;
2257 Struct_field_list::const_iterator p
= fields
->begin();
2258 go_assert(p
->is_field_name("size"));
2259 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
2260 vals
->push_back(Expression::make_type_info(this, type_info
));
2263 go_assert(p
->is_field_name("ptrdata"));
2264 type_info
= Expression::TYPE_INFO_DESCRIPTOR_PTRDATA
;
2265 vals
->push_back(Expression::make_type_info(this, type_info
));
2268 go_assert(p
->is_field_name("hash"));
2271 h
= name
->hash_for_method(gogo
);
2273 h
= this->hash_for_method(gogo
);
2274 vals
->push_back(Expression::make_integer_ul(h
, p
->type(), bloc
));
2277 go_assert(p
->is_field_name("kind"));
2278 vals
->push_back(Expression::make_integer_ul(runtime_type_kind
, p
->type(),
2282 go_assert(p
->is_field_name("align"));
2283 type_info
= Expression::TYPE_INFO_ALIGNMENT
;
2284 vals
->push_back(Expression::make_type_info(this, type_info
));
2287 go_assert(p
->is_field_name("fieldAlign"));
2288 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
2289 vals
->push_back(Expression::make_type_info(this, type_info
));
2292 go_assert(p
->is_field_name("hashfn"));
2293 Function_type
* hash_fntype
= p
->type()->function_type();
2296 go_assert(p
->is_field_name("equalfn"));
2297 Function_type
* equal_fntype
= p
->type()->function_type();
2299 Named_object
* hash_fn
;
2300 Named_object
* equal_fn
;
2301 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
2303 if (hash_fn
== NULL
)
2304 vals
->push_back(Expression::make_cast(hash_fntype
,
2305 Expression::make_nil(bloc
),
2308 vals
->push_back(Expression::make_func_reference(hash_fn
, NULL
, bloc
));
2309 if (equal_fn
== NULL
)
2310 vals
->push_back(Expression::make_cast(equal_fntype
,
2311 Expression::make_nil(bloc
),
2314 vals
->push_back(Expression::make_func_reference(equal_fn
, NULL
, bloc
));
2317 go_assert(p
->is_field_name("gcdata"));
2318 vals
->push_back(Expression::make_gc_symbol(this));
2321 go_assert(p
->is_field_name("string"));
2322 Expression
* s
= Expression::make_string((name
!= NULL
2323 ? name
->reflection(gogo
)
2324 : this->reflection(gogo
)),
2326 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2329 go_assert(p
->is_field_name("uncommonType"));
2330 if (name
== NULL
&& methods
== NULL
)
2331 vals
->push_back(Expression::make_nil(bloc
));
2334 if (methods
== NULL
)
2335 methods
= name
->methods();
2336 vals
->push_back(this->uncommon_type_constructor(gogo
,
2339 only_value_methods
));
2343 go_assert(p
->is_field_name("ptrToThis"));
2344 if (name
== NULL
&& methods
== NULL
)
2345 vals
->push_back(Expression::make_nil(bloc
));
2350 pt
= Type::make_pointer_type(name
);
2352 pt
= Type::make_pointer_type(this);
2353 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
2357 go_assert(p
== fields
->end());
2359 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
2362 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2363 // length used by the gc toolchain, and also appears in
2364 // libgo/go/reflect/type.go.
2366 static const int64_t max_ptrmask_bytes
= 2048;
2368 // Return a pointer to the Garbage Collection information for this type.
2371 Type::gc_symbol_pointer(Gogo
* gogo
)
2373 Type
* t
= this->forwarded();
2374 while (t
->named_type() != NULL
&& t
->named_type()->is_alias())
2375 t
= t
->named_type()->real_type()->forwarded();
2377 if (!t
->has_pointer())
2378 return gogo
->backend()->nil_pointer_expression();
2380 if (t
->gc_symbol_var_
== NULL
)
2382 t
->make_gc_symbol_var(gogo
);
2383 go_assert(t
->gc_symbol_var_
!= NULL
);
2385 Location bloc
= Linemap::predeclared_location();
2386 Bexpression
* var_expr
=
2387 gogo
->backend()->var_expression(t
->gc_symbol_var_
, bloc
);
2388 Bexpression
* addr_expr
=
2389 gogo
->backend()->address_expression(var_expr
, bloc
);
2391 Type
* uint8_type
= Type::lookup_integer_type("uint8");
2392 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
2393 Btype
* ubtype
= pointer_uint8_type
->get_backend(gogo
);
2394 return gogo
->backend()->convert_expression(ubtype
, addr_expr
, bloc
);
2397 // A mapping from unnamed types to GC symbol variables.
2399 Type::GC_symbol_vars
Type::gc_symbol_vars
;
2401 // Build the GC symbol for this type.
2404 Type::make_gc_symbol_var(Gogo
* gogo
)
2406 go_assert(this->gc_symbol_var_
== NULL
);
2408 Named_type
* nt
= this->named_type();
2410 // We can have multiple instances of unnamed types and similar to type
2411 // descriptors, we only want to the emit the GC data once, so we use a
2413 Bvariable
** phash
= NULL
;
2416 Bvariable
* bvnull
= NULL
;
2417 std::pair
<GC_symbol_vars::iterator
, bool> ins
=
2418 Type::gc_symbol_vars
.insert(std::make_pair(this, bvnull
));
2421 // We've already built a gc symbol for this type.
2422 this->gc_symbol_var_
= ins
.first
->second
;
2425 phash
= &ins
.first
->second
;
2430 if (!this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2432 this->gc_symbol_var_
= this->gc_ptrmask_var(gogo
, ptrsize
, ptrdata
);
2434 *phash
= this->gc_symbol_var_
;
2438 std::string sym_name
= gogo
->gc_symbol_name(this);
2440 // Build the contents of the gc symbol.
2441 Expression
* sym_init
= this->gcprog_constructor(gogo
, ptrsize
, ptrdata
);
2442 Btype
* sym_btype
= sym_init
->type()->get_backend(gogo
);
2444 // If the type descriptor for this type is defined somewhere else, so is the
2446 const Package
* dummy
;
2447 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
2449 std::string
asm_name(go_selectively_encode_id(sym_name
));
2450 this->gc_symbol_var_
=
2451 gogo
->backend()->implicit_variable_reference(sym_name
, asm_name
,
2454 *phash
= this->gc_symbol_var_
;
2458 // See if this gc symbol can appear in multiple packages.
2459 bool is_common
= false;
2462 // We create the symbol for a builtin type whenever we need
2464 is_common
= nt
->is_builtin();
2468 // This is an unnamed type. The descriptor could be defined in
2469 // any package where it is needed, and the linker will pick one
2470 // descriptor to keep.
2474 // Since we are building the GC symbol in this package, we must create the
2475 // variable before converting the initializer to its backend representation
2476 // because the initializer may refer to the GC symbol for this type.
2477 std::string
asm_name(go_selectively_encode_id(sym_name
));
2478 this->gc_symbol_var_
=
2479 gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2480 sym_btype
, false, true, is_common
, 0);
2482 *phash
= this->gc_symbol_var_
;
2484 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2485 context
.set_is_const();
2486 Bexpression
* sym_binit
= sym_init
->get_backend(&context
);
2487 gogo
->backend()->implicit_variable_set_init(this->gc_symbol_var_
, sym_name
,
2488 sym_btype
, false, true, is_common
,
2492 // Return whether this type needs a GC program, and set *PTRDATA to
2493 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2497 Type::needs_gcprog(Gogo
* gogo
, int64_t* ptrsize
, int64_t* ptrdata
)
2499 Type
* voidptr
= Type::make_pointer_type(Type::make_void_type());
2500 if (!voidptr
->backend_type_size(gogo
, ptrsize
))
2503 if (!this->backend_type_ptrdata(gogo
, ptrdata
))
2505 go_assert(saw_errors());
2509 return *ptrdata
/ *ptrsize
> max_ptrmask_bytes
;
2512 // A simple class used to build a GC ptrmask for a type.
2517 Ptrmask(size_t count
)
2518 : bits_((count
+ 7) / 8, 0)
2522 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2528 constructor(Gogo
* gogo
) const;
2533 { this->bits_
.at(index
/ 8) |= 1 << (index
% 8); }
2536 std::vector
<unsigned char> bits_
;
2539 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2540 // counts in bytes. PTRSIZE is the size of a pointer on the target
2544 Ptrmask::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2546 switch (type
->base()->classification())
2549 case Type::TYPE_NIL
:
2550 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2551 case Type::TYPE_NAMED
:
2552 case Type::TYPE_FORWARD
:
2555 case Type::TYPE_ERROR
:
2556 case Type::TYPE_VOID
:
2557 case Type::TYPE_BOOLEAN
:
2558 case Type::TYPE_INTEGER
:
2559 case Type::TYPE_FLOAT
:
2560 case Type::TYPE_COMPLEX
:
2561 case Type::TYPE_SINK
:
2564 case Type::TYPE_FUNCTION
:
2565 case Type::TYPE_POINTER
:
2566 case Type::TYPE_MAP
:
2567 case Type::TYPE_CHANNEL
:
2568 // These types are all a single pointer.
2569 go_assert((offset
% ptrsize
) == 0);
2570 this->set(offset
/ ptrsize
);
2573 case Type::TYPE_STRING
:
2574 // A string starts with a single pointer.
2575 go_assert((offset
% ptrsize
) == 0);
2576 this->set(offset
/ ptrsize
);
2579 case Type::TYPE_INTERFACE
:
2580 // An interface is two pointers.
2581 go_assert((offset
% ptrsize
) == 0);
2582 this->set(offset
/ ptrsize
);
2583 this->set((offset
/ ptrsize
) + 1);
2586 case Type::TYPE_STRUCT
:
2588 if (!type
->has_pointer())
2591 const Struct_field_list
* fields
= type
->struct_type()->fields();
2592 int64_t soffset
= 0;
2593 for (Struct_field_list::const_iterator pf
= fields
->begin();
2594 pf
!= fields
->end();
2597 int64_t field_align
;
2598 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2600 go_assert(saw_errors());
2603 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2605 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2608 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2610 go_assert(saw_errors());
2613 soffset
+= field_size
;
2618 case Type::TYPE_ARRAY
:
2619 if (type
->is_slice_type())
2621 // A slice starts with a single pointer.
2622 go_assert((offset
% ptrsize
) == 0);
2623 this->set(offset
/ ptrsize
);
2628 if (!type
->has_pointer())
2632 if (!type
->array_type()->int_length(&len
))
2634 go_assert(saw_errors());
2638 Type
* element_type
= type
->array_type()->element_type();
2640 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2642 go_assert(saw_errors());
2646 int64_t eoffset
= 0;
2647 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2648 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2654 // Return a symbol name for this ptrmask. This is used to coalesce
2655 // identical ptrmasks, which are common. The symbol name must use
2656 // only characters that are valid in symbols. It's nice if it's
2657 // short. We convert it to a base64 string.
2660 Ptrmask::symname() const
2662 const char chars
[65] =
2663 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.";
2664 go_assert(chars
[64] == '\0');
2668 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2669 p
!= this->bits_
.end();
2672 b
|= *p
<< remaining
;
2674 while (remaining
>= 6)
2676 ret
+= chars
[b
& 0x3f];
2681 while (remaining
> 0)
2683 ret
+= chars
[b
& 0x3f];
2690 // Return a constructor for this ptrmask. This will be used to
2691 // initialize the runtime ptrmask value.
2694 Ptrmask::constructor(Gogo
* gogo
) const
2696 Location bloc
= Linemap::predeclared_location();
2697 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
2698 Expression
* len
= Expression::make_integer_ul(this->bits_
.size(), NULL
,
2700 Array_type
* at
= Type::make_array_type(byte_type
, len
);
2701 Expression_list
* vals
= new Expression_list();
2702 vals
->reserve(this->bits_
.size());
2703 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2704 p
!= this->bits_
.end();
2706 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
2707 return Expression::make_array_composite_literal(at
, vals
, bloc
);
2710 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2711 Type::GC_gcbits_vars
Type::gc_gcbits_vars
;
2713 // Return a ptrmask variable for a type. For a type descriptor this
2714 // is only used for variables that are small enough to not need a
2715 // gcprog, but for a global variable this is used for a variable of
2716 // any size. PTRDATA is the number of bytes of the type that contain
2717 // pointer data. PTRSIZE is the size of a pointer on the target
2721 Type::gc_ptrmask_var(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
2723 Ptrmask
ptrmask(ptrdata
/ ptrsize
);
2724 if (ptrdata
>= ptrsize
)
2725 ptrmask
.set_from(gogo
, this, ptrsize
, 0);
2728 // This can happen in error cases. Just build an empty gcbits.
2729 go_assert(saw_errors());
2732 std::string sym_name
= gogo
->ptrmask_symbol_name(ptrmask
.symname());
2733 Bvariable
* bvnull
= NULL
;
2734 std::pair
<GC_gcbits_vars::iterator
, bool> ins
=
2735 Type::gc_gcbits_vars
.insert(std::make_pair(sym_name
, bvnull
));
2738 // We've already built a GC symbol for this set of gcbits.
2739 return ins
.first
->second
;
2742 Expression
* val
= ptrmask
.constructor(gogo
);
2743 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2744 context
.set_is_const();
2745 Bexpression
* bval
= val
->get_backend(&context
);
2747 std::string
asm_name(go_selectively_encode_id(sym_name
));
2748 Btype
*btype
= val
->type()->get_backend(gogo
);
2749 Bvariable
* ret
= gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2752 gogo
->backend()->implicit_variable_set_init(ret
, sym_name
, btype
, false,
2754 ins
.first
->second
= ret
;
2758 // A GCProg is used to build a program for the garbage collector.
2759 // This is used for types with a lot of pointer data, to reduce the
2760 // size of the data in the compiled program. The program is expanded
2761 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2767 : bytes_(), index_(0), nb_(0)
2770 // The number of bits described so far.
2773 { return this->index_
; }
2776 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2782 constructor(Gogo
* gogo
) const;
2789 should_repeat(int64_t, int64_t);
2792 repeat(int64_t, int64_t);
2795 zero_until(int64_t);
2806 // Add a byte to the program.
2808 byte(unsigned char x
)
2809 { this->bytes_
.push_back(x
); }
2811 // The maximum number of bytes of literal bits.
2812 static const int max_literal
= 127;
2815 std::vector
<unsigned char> bytes_
;
2816 // The index of the last bit described.
2818 // The current set of literal bits.
2819 unsigned char b_
[max_literal
];
2820 // The current number of literal bits.
2824 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2825 // counts in bytes. PTRSIZE is the size of a pointer on the target
2829 GCProg::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2831 switch (type
->base()->classification())
2834 case Type::TYPE_NIL
:
2835 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2836 case Type::TYPE_NAMED
:
2837 case Type::TYPE_FORWARD
:
2840 case Type::TYPE_ERROR
:
2841 case Type::TYPE_VOID
:
2842 case Type::TYPE_BOOLEAN
:
2843 case Type::TYPE_INTEGER
:
2844 case Type::TYPE_FLOAT
:
2845 case Type::TYPE_COMPLEX
:
2846 case Type::TYPE_SINK
:
2849 case Type::TYPE_FUNCTION
:
2850 case Type::TYPE_POINTER
:
2851 case Type::TYPE_MAP
:
2852 case Type::TYPE_CHANNEL
:
2853 // These types are all a single pointer.
2854 go_assert((offset
% ptrsize
) == 0);
2855 this->ptr(offset
/ ptrsize
);
2858 case Type::TYPE_STRING
:
2859 // A string starts with a single pointer.
2860 go_assert((offset
% ptrsize
) == 0);
2861 this->ptr(offset
/ ptrsize
);
2864 case Type::TYPE_INTERFACE
:
2865 // An interface is two pointers.
2866 go_assert((offset
% ptrsize
) == 0);
2867 this->ptr(offset
/ ptrsize
);
2868 this->ptr((offset
/ ptrsize
) + 1);
2871 case Type::TYPE_STRUCT
:
2873 if (!type
->has_pointer())
2876 const Struct_field_list
* fields
= type
->struct_type()->fields();
2877 int64_t soffset
= 0;
2878 for (Struct_field_list::const_iterator pf
= fields
->begin();
2879 pf
!= fields
->end();
2882 int64_t field_align
;
2883 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2885 go_assert(saw_errors());
2888 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2890 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2893 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2895 go_assert(saw_errors());
2898 soffset
+= field_size
;
2903 case Type::TYPE_ARRAY
:
2904 if (type
->is_slice_type())
2906 // A slice starts with a single pointer.
2907 go_assert((offset
% ptrsize
) == 0);
2908 this->ptr(offset
/ ptrsize
);
2913 if (!type
->has_pointer())
2917 if (!type
->array_type()->int_length(&len
))
2919 go_assert(saw_errors());
2923 Type
* element_type
= type
->array_type()->element_type();
2925 // Flatten array of array to a big array by multiplying counts.
2926 while (element_type
->array_type() != NULL
2927 && !element_type
->is_slice_type())
2930 if (!element_type
->array_type()->int_length(&ele_len
))
2932 go_assert(saw_errors());
2937 element_type
= element_type
->array_type()->element_type();
2941 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2943 go_assert(saw_errors());
2947 go_assert(len
> 0 && ele_size
> 0);
2949 if (!this->should_repeat(ele_size
/ ptrsize
, len
))
2951 // Cheaper to just emit the bits.
2952 int64_t eoffset
= 0;
2953 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2954 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2958 go_assert((offset
% ptrsize
) == 0);
2959 go_assert((ele_size
% ptrsize
) == 0);
2960 this->set_from(gogo
, element_type
, ptrsize
, offset
);
2961 this->zero_until((offset
+ ele_size
) / ptrsize
);
2962 this->repeat(ele_size
/ ptrsize
, len
- 1);
2970 // Emit a 1 into the bit stream of a GC program at the given bit index.
2973 GCProg::ptr(int64_t index
)
2975 go_assert(index
>= this->index_
);
2976 this->zero_until(index
);
2980 // Return whether it is worthwhile to use a repeat to describe c
2981 // elements of n bits each, compared to just emitting c copies of the
2982 // n-bit description.
2985 GCProg::should_repeat(int64_t n
, int64_t c
)
2987 // Repeat if there is more than 1 item and if the total data doesn't
2988 // fit into four bytes.
2989 return c
> 1 && c
* n
> 4 * 8;
2992 // Emit an instruction to repeat the description of the last n words c
2993 // times (including the initial description, so c + 1 times in total).
2996 GCProg::repeat(int64_t n
, int64_t c
)
2998 if (n
== 0 || c
== 0)
3002 this->byte(0x80 | static_cast<unsigned char>(n
& 0x7f));
3009 this->index_
+= n
* c
;
3012 // Add zeros to the bit stream up to the given index.
3015 GCProg::zero_until(int64_t index
)
3017 go_assert(index
>= this->index_
);
3018 int64_t skip
= index
- this->index_
;
3023 for (int64_t i
= 0; i
< skip
; ++i
)
3029 this->repeat(1, skip
- 1);
3032 // Add a single literal bit to the program.
3035 GCProg::lit(unsigned char x
)
3037 if (this->nb_
== GCProg::max_literal
)
3039 this->b_
[this->nb_
] = x
;
3044 // Emit the varint encoding of x.
3047 GCProg::varint(int64_t x
)
3052 this->byte(0x80 | static_cast<unsigned char>(x
& 0x7f));
3055 this->byte(static_cast<unsigned char>(x
& 0x7f));
3058 // Flush any pending literal bits.
3065 this->byte(static_cast<unsigned char>(this->nb_
));
3066 unsigned char bits
= 0;
3067 for (int i
= 0; i
< this->nb_
; ++i
)
3069 bits
|= this->b_
[i
] << (i
% 8);
3070 if ((i
+ 1) % 8 == 0)
3076 if (this->nb_
% 8 != 0)
3081 // Mark the end of a GC program.
3090 // Return an Expression for the bytes in a GC program.
3093 GCProg::constructor(Gogo
* gogo
) const
3095 Location bloc
= Linemap::predeclared_location();
3097 // The first four bytes are the length of the program in target byte
3098 // order. Build a struct whose first type is uint32 to make this
3101 Type
* uint32_type
= Type::lookup_integer_type("uint32");
3103 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
3104 Expression
* len
= Expression::make_integer_ul(this->bytes_
.size(), NULL
,
3106 Array_type
* at
= Type::make_array_type(byte_type
, len
);
3108 Struct_type
* st
= Type::make_builtin_struct_type(2, "len", uint32_type
,
3111 Expression_list
* vals
= new Expression_list();
3112 vals
->reserve(this->bytes_
.size());
3113 for (std::vector
<unsigned char>::const_iterator p
= this->bytes_
.begin();
3114 p
!= this->bytes_
.end();
3116 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
3117 Expression
* bytes
= Expression::make_array_composite_literal(at
, vals
, bloc
);
3119 vals
= new Expression_list();
3120 vals
->push_back(Expression::make_integer_ul(this->bytes_
.size(), uint32_type
,
3122 vals
->push_back(bytes
);
3124 return Expression::make_struct_composite_literal(st
, vals
, bloc
);
3127 // Return a composite literal for the garbage collection program for
3128 // this type. This is only used for types that are too large to use a
3132 Type::gcprog_constructor(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
3134 Location bloc
= Linemap::predeclared_location();
3137 prog
.set_from(gogo
, this, ptrsize
, 0);
3138 int64_t offset
= prog
.bit_index() * ptrsize
;
3142 if (!this->backend_type_size(gogo
, &type_size
))
3144 go_assert(saw_errors());
3145 return Expression::make_error(bloc
);
3148 go_assert(offset
>= ptrdata
&& offset
<= type_size
);
3150 return prog
.constructor(gogo
);
3153 // Return a composite literal for the uncommon type information for
3154 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3155 // struct. If name is not NULL, it is the name of the type. If
3156 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3157 // is true if only value methods should be included. At least one of
3158 // NAME and METHODS must not be NULL.
3161 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
3162 Named_type
* name
, const Methods
* methods
,
3163 bool only_value_methods
) const
3165 Location bloc
= Linemap::predeclared_location();
3167 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
3169 Expression_list
* vals
= new Expression_list();
3172 Struct_field_list::const_iterator p
= fields
->begin();
3173 go_assert(p
->is_field_name("name"));
3176 go_assert(p
->is_field_name("pkgPath"));
3180 vals
->push_back(Expression::make_nil(bloc
));
3181 vals
->push_back(Expression::make_nil(bloc
));
3185 Named_object
* no
= name
->named_object();
3186 std::string n
= Gogo::unpack_hidden_name(no
->name());
3187 Expression
* s
= Expression::make_string(n
, bloc
);
3188 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3190 if (name
->is_builtin())
3191 vals
->push_back(Expression::make_nil(bloc
));
3194 const Package
* package
= no
->package();
3195 const std::string
& pkgpath(package
== NULL
3197 : package
->pkgpath());
3198 s
= Expression::make_string(pkgpath
, bloc
);
3199 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3204 go_assert(p
->is_field_name("methods"));
3205 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
3206 only_value_methods
));
3209 go_assert(p
== fields
->end());
3211 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
3213 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
3216 // Sort methods by name.
3222 operator()(const std::pair
<std::string
, const Method
*>& m1
,
3223 const std::pair
<std::string
, const Method
*>& m2
) const
3225 return (Gogo::unpack_hidden_name(m1
.first
)
3226 < Gogo::unpack_hidden_name(m2
.first
));
3230 // Return a composite literal for the type method table for this type.
3231 // METHODS_TYPE is the type of the table, and is a slice type.
3232 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3233 // then only value methods are used.
3236 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
3237 const Methods
* methods
,
3238 bool only_value_methods
) const
3240 Location bloc
= Linemap::predeclared_location();
3242 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
3243 if (methods
!= NULL
)
3245 smethods
.reserve(methods
->count());
3246 for (Methods::const_iterator p
= methods
->begin();
3247 p
!= methods
->end();
3250 if (p
->second
->is_ambiguous())
3252 if (only_value_methods
&& !p
->second
->is_value_method())
3255 // This is where we implement the magic //go:nointerface
3256 // comment. If we saw that comment, we don't add this
3257 // method to the type descriptor.
3258 if (p
->second
->nointerface())
3261 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
3265 if (smethods
.empty())
3266 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
3268 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
3270 Type
* method_type
= methods_type
->array_type()->element_type();
3272 Expression_list
* vals
= new Expression_list();
3273 vals
->reserve(smethods
.size());
3274 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
3276 p
!= smethods
.end();
3278 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
3279 p
->second
, only_value_methods
));
3281 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
3284 // Return a composite literal for a single method. METHOD_TYPE is the
3285 // type of the entry. METHOD_NAME is the name of the method and M is
3286 // the method information.
3289 Type::method_constructor(Gogo
*, Type
* method_type
,
3290 const std::string
& method_name
,
3292 bool only_value_methods
) const
3294 Location bloc
= Linemap::predeclared_location();
3296 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
3298 Expression_list
* vals
= new Expression_list();
3301 Struct_field_list::const_iterator p
= fields
->begin();
3302 go_assert(p
->is_field_name("name"));
3303 const std::string n
= Gogo::unpack_hidden_name(method_name
);
3304 Expression
* s
= Expression::make_string(n
, bloc
);
3305 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3308 go_assert(p
->is_field_name("pkgPath"));
3309 if (!Gogo::is_hidden_name(method_name
))
3310 vals
->push_back(Expression::make_nil(bloc
));
3313 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
3315 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3318 Named_object
* no
= (m
->needs_stub_method()
3320 : m
->named_object());
3322 Function_type
* mtype
;
3323 if (no
->is_function())
3324 mtype
= no
->func_value()->type();
3326 mtype
= no
->func_declaration_value()->type();
3327 go_assert(mtype
->is_method());
3328 Type
* nonmethod_type
= mtype
->copy_without_receiver();
3331 go_assert(p
->is_field_name("mtyp"));
3332 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3335 go_assert(p
->is_field_name("typ"));
3336 bool want_pointer_receiver
= !only_value_methods
&& m
->is_value_method();
3337 nonmethod_type
= mtype
->copy_with_receiver_as_param(want_pointer_receiver
);
3338 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3341 go_assert(p
->is_field_name("tfn"));
3342 vals
->push_back(Expression::make_func_code_reference(no
, bloc
));
3345 go_assert(p
== fields
->end());
3347 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
3350 // Return a composite literal for the type descriptor of a plain type.
3351 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3352 // NULL, it is the name to use as well as the list of methods.
3355 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
3358 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
3362 // Return the type reflection string for this type.
3365 Type::reflection(Gogo
* gogo
) const
3369 // The do_reflection virtual function should set RET to the
3370 // reflection string.
3371 this->do_reflection(gogo
, &ret
);
3376 // Return whether the backend size of the type is known.
3379 Type::is_backend_type_size_known(Gogo
* gogo
)
3381 switch (this->classification_
)
3395 case TYPE_INTERFACE
:
3400 const Struct_field_list
* fields
= this->struct_type()->fields();
3401 for (Struct_field_list::const_iterator pf
= fields
->begin();
3402 pf
!= fields
->end();
3404 if (!pf
->type()->is_backend_type_size_known(gogo
))
3411 const Array_type
* at
= this->array_type();
3412 if (at
->length() == NULL
)
3416 Numeric_constant nc
;
3417 if (!at
->length()->numeric_constant_value(&nc
))
3420 if (!nc
.to_int(&ival
))
3423 return at
->element_type()->is_backend_type_size_known(gogo
);
3428 this->named_type()->convert(gogo
);
3429 return this->named_type()->is_named_backend_type_size_known();
3433 Forward_declaration_type
* fdt
= this->forward_declaration_type();
3434 return fdt
->real_type()->is_backend_type_size_known(gogo
);
3438 case TYPE_CALL_MULTIPLE_RESULT
:
3446 // If the size of the type can be determined, set *PSIZE to the size
3447 // in bytes and return true. Otherwise, return false. This queries
3451 Type::backend_type_size(Gogo
* gogo
, int64_t *psize
)
3453 if (!this->is_backend_type_size_known(gogo
))
3455 if (this->is_error_type())
3457 Btype
* bt
= this->get_backend_placeholder(gogo
);
3458 *psize
= gogo
->backend()->type_size(bt
);
3461 if (this->named_type() != NULL
)
3462 go_error_at(this->named_type()->location(),
3463 "type %s larger than address space",
3464 Gogo::message_name(this->named_type()->name()).c_str());
3466 go_error_at(Linemap::unknown_location(),
3467 "type %s larger than address space",
3468 this->reflection(gogo
).c_str());
3470 // Make this an error type to avoid knock-on errors.
3471 this->classification_
= TYPE_ERROR
;
3477 // If the alignment of the type can be determined, set *PALIGN to
3478 // the alignment in bytes and return true. Otherwise, return false.
3481 Type::backend_type_align(Gogo
* gogo
, int64_t *palign
)
3483 if (!this->is_backend_type_size_known(gogo
))
3485 Btype
* bt
= this->get_backend_placeholder(gogo
);
3486 *palign
= gogo
->backend()->type_alignment(bt
);
3490 // Like backend_type_align, but return the alignment when used as a
3494 Type::backend_type_field_align(Gogo
* gogo
, int64_t *palign
)
3496 if (!this->is_backend_type_size_known(gogo
))
3498 Btype
* bt
= this->get_backend_placeholder(gogo
);
3499 *palign
= gogo
->backend()->type_field_alignment(bt
);
3503 // Get the ptrdata value for a type. This is the size of the prefix
3504 // of the type that contains all pointers. Store the ptrdata in
3505 // *PPTRDATA and return whether we found it.
3508 Type::backend_type_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3512 if (!this->has_pointer())
3515 if (!this->is_backend_type_size_known(gogo
))
3518 switch (this->classification_
)
3527 // These types are nothing but a pointer.
3528 return this->backend_type_size(gogo
, pptrdata
);
3530 case TYPE_INTERFACE
:
3531 // An interface is a struct of two pointers.
3532 return this->backend_type_size(gogo
, pptrdata
);
3536 // A string is a struct whose first field is a pointer, and
3537 // whose second field is not.
3538 Type
* uint8_type
= Type::lookup_integer_type("uint8");
3539 Type
* ptr
= Type::make_pointer_type(uint8_type
);
3540 return ptr
->backend_type_size(gogo
, pptrdata
);
3545 return this->base()->backend_type_ptrdata(gogo
, pptrdata
);
3549 const Struct_field_list
* fields
= this->struct_type()->fields();
3551 const Struct_field
*ptr
= NULL
;
3552 int64_t ptr_offset
= 0;
3553 for (Struct_field_list::const_iterator pf
= fields
->begin();
3554 pf
!= fields
->end();
3557 int64_t field_align
;
3558 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
3560 offset
= (offset
+ (field_align
- 1)) &~ (field_align
- 1);
3562 if (pf
->type()->has_pointer())
3565 ptr_offset
= offset
;
3569 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
3571 offset
+= field_size
;
3576 int64_t ptr_ptrdata
;
3577 if (!ptr
->type()->backend_type_ptrdata(gogo
, &ptr_ptrdata
))
3579 *pptrdata
= ptr_offset
+ ptr_ptrdata
;
3585 if (this->is_slice_type())
3587 // A slice is a struct whose first field is a pointer, and
3588 // whose remaining fields are not.
3589 Type
* element_type
= this->array_type()->element_type();
3590 Type
* ptr
= Type::make_pointer_type(element_type
);
3591 return ptr
->backend_type_size(gogo
, pptrdata
);
3595 Numeric_constant nc
;
3596 if (!this->array_type()->length()->numeric_constant_value(&nc
))
3599 if (!nc
.to_memory_size(&len
))
3602 Type
* element_type
= this->array_type()->element_type();
3604 int64_t ele_ptrdata
;
3605 if (!element_type
->backend_type_size(gogo
, &ele_size
)
3606 || !element_type
->backend_type_ptrdata(gogo
, &ele_ptrdata
))
3608 go_assert(ele_size
> 0 && ele_ptrdata
> 0);
3610 *pptrdata
= (len
- 1) * ele_size
+ ele_ptrdata
;
3622 case TYPE_CALL_MULTIPLE_RESULT
:
3627 // Get the ptrdata value to store in a type descriptor. This is
3628 // normally the same as backend_type_ptrdata, but for a type that is
3629 // large enough to use a gcprog we may need to store a different value
3630 // if it ends with an array. If the gcprog uses a repeat descriptor
3631 // for the array, and if the array element ends with non-pointer data,
3632 // then the gcprog will produce a value that describes the complete
3633 // array where the backend ptrdata will omit the non-pointer elements
3634 // of the final array element. This is a subtle difference but the
3635 // run time code checks it to verify that it has expanded a gcprog as
3639 Type::descriptor_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3641 int64_t backend_ptrdata
;
3642 if (!this->backend_type_ptrdata(gogo
, &backend_ptrdata
))
3646 if (!this->needs_gcprog(gogo
, &ptrsize
, &backend_ptrdata
))
3648 *pptrdata
= backend_ptrdata
;
3653 prog
.set_from(gogo
, this, ptrsize
, 0);
3654 int64_t offset
= prog
.bit_index() * ptrsize
;
3656 go_assert(offset
>= backend_ptrdata
);
3661 // Default function to export a type.
3664 Type::do_export(Export
*) const
3672 Type::import_type(Import
* imp
)
3674 if (imp
->match_c_string("("))
3675 return Function_type::do_import(imp
);
3676 else if (imp
->match_c_string("*"))
3677 return Pointer_type::do_import(imp
);
3678 else if (imp
->match_c_string("struct "))
3679 return Struct_type::do_import(imp
);
3680 else if (imp
->match_c_string("["))
3681 return Array_type::do_import(imp
);
3682 else if (imp
->match_c_string("map "))
3683 return Map_type::do_import(imp
);
3684 else if (imp
->match_c_string("chan "))
3685 return Channel_type::do_import(imp
);
3686 else if (imp
->match_c_string("interface"))
3687 return Interface_type::do_import(imp
);
3690 go_error_at(imp
->location(), "import error: expected type");
3691 return Type::make_error_type();
3695 // Class Error_type.
3697 // Return the backend representation of an Error type.
3700 Error_type::do_get_backend(Gogo
* gogo
)
3702 return gogo
->backend()->error_type();
3705 // Return an expression for the type descriptor for an error type.
3709 Error_type::do_type_descriptor(Gogo
*, Named_type
*)
3711 return Expression::make_error(Linemap::predeclared_location());
3714 // We should not be asked for the reflection string for an error type.
3717 Error_type::do_reflection(Gogo
*, std::string
*) const
3719 go_assert(saw_errors());
3723 Type::make_error_type()
3725 static Error_type singleton_error_type
;
3726 return &singleton_error_type
;
3731 // Get the backend representation of a void type.
3734 Void_type::do_get_backend(Gogo
* gogo
)
3736 return gogo
->backend()->void_type();
3740 Type::make_void_type()
3742 static Void_type singleton_void_type
;
3743 return &singleton_void_type
;
3746 // Class Boolean_type.
3748 // Return the backend representation of the boolean type.
3751 Boolean_type::do_get_backend(Gogo
* gogo
)
3753 return gogo
->backend()->bool_type();
3756 // Make the type descriptor.
3759 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3762 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
3765 Named_object
* no
= gogo
->lookup_global("bool");
3766 go_assert(no
!= NULL
);
3767 return Type::type_descriptor(gogo
, no
->type_value());
3772 Type::make_boolean_type()
3774 static Boolean_type boolean_type
;
3775 return &boolean_type
;
3778 // The named type "bool".
3780 static Named_type
* named_bool_type
;
3782 // Get the named type "bool".
3785 Type::lookup_bool_type()
3787 return named_bool_type
;
3790 // Make the named type "bool".
3793 Type::make_named_bool_type()
3795 Type
* bool_type
= Type::make_boolean_type();
3796 Named_object
* named_object
=
3797 Named_object::make_type("bool", NULL
, bool_type
,
3798 Linemap::predeclared_location());
3799 Named_type
* named_type
= named_object
->type_value();
3800 named_bool_type
= named_type
;
3804 // Class Integer_type.
3806 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
3808 // Create a new integer type. Non-abstract integer types always have
3812 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
3813 int bits
, int runtime_type_kind
)
3815 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
3817 std::string
sname(name
);
3818 Named_object
* named_object
=
3819 Named_object::make_type(sname
, NULL
, integer_type
,
3820 Linemap::predeclared_location());
3821 Named_type
* named_type
= named_object
->type_value();
3822 std::pair
<Named_integer_types::iterator
, bool> ins
=
3823 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
3824 go_assert(ins
.second
);
3828 // Look up an existing integer type.
3831 Integer_type::lookup_integer_type(const char* name
)
3833 Named_integer_types::const_iterator p
=
3834 Integer_type::named_integer_types
.find(name
);
3835 go_assert(p
!= Integer_type::named_integer_types
.end());
3839 // Create a new abstract integer type.
3842 Integer_type::create_abstract_integer_type()
3844 static Integer_type
* abstract_type
;
3845 if (abstract_type
== NULL
)
3847 Type
* int_type
= Type::lookup_integer_type("int");
3848 abstract_type
= new Integer_type(true, false,
3849 int_type
->integer_type()->bits(),
3850 RUNTIME_TYPE_KIND_INT
);
3852 return abstract_type
;
3855 // Create a new abstract character type.
3858 Integer_type::create_abstract_character_type()
3860 static Integer_type
* abstract_type
;
3861 if (abstract_type
== NULL
)
3863 abstract_type
= new Integer_type(true, false, 32,
3864 RUNTIME_TYPE_KIND_INT32
);
3865 abstract_type
->set_is_rune();
3867 return abstract_type
;
3870 // Integer type compatibility.
3873 Integer_type::is_identical(const Integer_type
* t
) const
3875 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
3877 return this->is_abstract_
== t
->is_abstract_
;
3883 Integer_type::do_hash_for_method(Gogo
*) const
3885 return ((this->bits_
<< 4)
3886 + ((this->is_unsigned_
? 1 : 0) << 8)
3887 + ((this->is_abstract_
? 1 : 0) << 9));
3890 // Convert an Integer_type to the backend representation.
3893 Integer_type::do_get_backend(Gogo
* gogo
)
3895 if (this->is_abstract_
)
3897 go_assert(saw_errors());
3898 return gogo
->backend()->error_type();
3900 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
3903 // The type descriptor for an integer type. Integer types are always
3907 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3909 go_assert(name
!= NULL
|| saw_errors());
3910 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
3913 // We should not be asked for the reflection string of a basic type.
3916 Integer_type::do_reflection(Gogo
*, std::string
*) const
3918 go_assert(saw_errors());
3921 // Make an integer type.
3924 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
3925 int runtime_type_kind
)
3927 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
3931 // Make an abstract integer type.
3934 Type::make_abstract_integer_type()
3936 return Integer_type::create_abstract_integer_type();
3939 // Make an abstract character type.
3942 Type::make_abstract_character_type()
3944 return Integer_type::create_abstract_character_type();
3947 // Look up an integer type.
3950 Type::lookup_integer_type(const char* name
)
3952 return Integer_type::lookup_integer_type(name
);
3955 // Class Float_type.
3957 Float_type::Named_float_types
Float_type::named_float_types
;
3959 // Create a new float type. Non-abstract float types always have
3963 Float_type::create_float_type(const char* name
, int bits
,
3964 int runtime_type_kind
)
3966 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
3967 std::string
sname(name
);
3968 Named_object
* named_object
=
3969 Named_object::make_type(sname
, NULL
, float_type
,
3970 Linemap::predeclared_location());
3971 Named_type
* named_type
= named_object
->type_value();
3972 std::pair
<Named_float_types::iterator
, bool> ins
=
3973 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
3974 go_assert(ins
.second
);
3978 // Look up an existing float type.
3981 Float_type::lookup_float_type(const char* name
)
3983 Named_float_types::const_iterator p
=
3984 Float_type::named_float_types
.find(name
);
3985 go_assert(p
!= Float_type::named_float_types
.end());
3989 // Create a new abstract float type.
3992 Float_type::create_abstract_float_type()
3994 static Float_type
* abstract_type
;
3995 if (abstract_type
== NULL
)
3996 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
3997 return abstract_type
;
4000 // Whether this type is identical with T.
4003 Float_type::is_identical(const Float_type
* t
) const
4005 if (this->bits_
!= t
->bits_
)
4007 return this->is_abstract_
== t
->is_abstract_
;
4013 Float_type::do_hash_for_method(Gogo
*) const
4015 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4018 // Convert to the backend representation.
4021 Float_type::do_get_backend(Gogo
* gogo
)
4023 return gogo
->backend()->float_type(this->bits_
);
4026 // The type descriptor for a float type. Float types are always named.
4029 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4031 go_assert(name
!= NULL
|| saw_errors());
4032 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4035 // We should not be asked for the reflection string of a basic type.
4038 Float_type::do_reflection(Gogo
*, std::string
*) const
4040 go_assert(saw_errors());
4043 // Make a floating point type.
4046 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
4048 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
4051 // Make an abstract float type.
4054 Type::make_abstract_float_type()
4056 return Float_type::create_abstract_float_type();
4059 // Look up a float type.
4062 Type::lookup_float_type(const char* name
)
4064 return Float_type::lookup_float_type(name
);
4067 // Class Complex_type.
4069 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
4071 // Create a new complex type. Non-abstract complex types always have
4075 Complex_type::create_complex_type(const char* name
, int bits
,
4076 int runtime_type_kind
)
4078 Complex_type
* complex_type
= new Complex_type(false, bits
,
4080 std::string
sname(name
);
4081 Named_object
* named_object
=
4082 Named_object::make_type(sname
, NULL
, complex_type
,
4083 Linemap::predeclared_location());
4084 Named_type
* named_type
= named_object
->type_value();
4085 std::pair
<Named_complex_types::iterator
, bool> ins
=
4086 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
4088 go_assert(ins
.second
);
4092 // Look up an existing complex type.
4095 Complex_type::lookup_complex_type(const char* name
)
4097 Named_complex_types::const_iterator p
=
4098 Complex_type::named_complex_types
.find(name
);
4099 go_assert(p
!= Complex_type::named_complex_types
.end());
4103 // Create a new abstract complex type.
4106 Complex_type::create_abstract_complex_type()
4108 static Complex_type
* abstract_type
;
4109 if (abstract_type
== NULL
)
4110 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
4111 return abstract_type
;
4114 // Whether this type is identical with T.
4117 Complex_type::is_identical(const Complex_type
*t
) const
4119 if (this->bits_
!= t
->bits_
)
4121 return this->is_abstract_
== t
->is_abstract_
;
4127 Complex_type::do_hash_for_method(Gogo
*) const
4129 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4132 // Convert to the backend representation.
4135 Complex_type::do_get_backend(Gogo
* gogo
)
4137 return gogo
->backend()->complex_type(this->bits_
);
4140 // The type descriptor for a complex type. Complex types are always
4144 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4146 go_assert(name
!= NULL
|| saw_errors());
4147 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4150 // We should not be asked for the reflection string of a basic type.
4153 Complex_type::do_reflection(Gogo
*, std::string
*) const
4155 go_assert(saw_errors());
4158 // Make a complex type.
4161 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
4163 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
4166 // Make an abstract complex type.
4169 Type::make_abstract_complex_type()
4171 return Complex_type::create_abstract_complex_type();
4174 // Look up a complex type.
4177 Type::lookup_complex_type(const char* name
)
4179 return Complex_type::lookup_complex_type(name
);
4182 // Class String_type.
4184 // Convert String_type to the backend representation. A string is a
4185 // struct with two fields: a pointer to the characters and a length.
4188 String_type::do_get_backend(Gogo
* gogo
)
4190 static Btype
* backend_string_type
;
4191 if (backend_string_type
== NULL
)
4193 std::vector
<Backend::Btyped_identifier
> fields(2);
4195 Type
* b
= gogo
->lookup_global("byte")->type_value();
4196 Type
* pb
= Type::make_pointer_type(b
);
4198 // We aren't going to get back to this field to finish the
4199 // backend representation, so force it to be finished now.
4200 if (!gogo
->named_types_are_converted())
4202 Btype
* bt
= pb
->get_backend_placeholder(gogo
);
4203 pb
->finish_backend(gogo
, bt
);
4206 fields
[0].name
= "__data";
4207 fields
[0].btype
= pb
->get_backend(gogo
);
4208 fields
[0].location
= Linemap::predeclared_location();
4210 Type
* int_type
= Type::lookup_integer_type("int");
4211 fields
[1].name
= "__length";
4212 fields
[1].btype
= int_type
->get_backend(gogo
);
4213 fields
[1].location
= fields
[0].location
;
4215 backend_string_type
= gogo
->backend()->struct_type(fields
);
4217 return backend_string_type
;
4220 // The type descriptor for the string type.
4223 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4226 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
4229 Named_object
* no
= gogo
->lookup_global("string");
4230 go_assert(no
!= NULL
);
4231 return Type::type_descriptor(gogo
, no
->type_value());
4235 // We should not be asked for the reflection string of a basic type.
4238 String_type::do_reflection(Gogo
*, std::string
* ret
) const
4240 ret
->append("string");
4243 // Make a string type.
4246 Type::make_string_type()
4248 static String_type string_type
;
4249 return &string_type
;
4252 // The named type "string".
4254 static Named_type
* named_string_type
;
4256 // Get the named type "string".
4259 Type::lookup_string_type()
4261 return named_string_type
;
4264 // Make the named type string.
4267 Type::make_named_string_type()
4269 Type
* string_type
= Type::make_string_type();
4270 Named_object
* named_object
=
4271 Named_object::make_type("string", NULL
, string_type
,
4272 Linemap::predeclared_location());
4273 Named_type
* named_type
= named_object
->type_value();
4274 named_string_type
= named_type
;
4278 // The sink type. This is the type of the blank identifier _. Any
4279 // type may be assigned to it.
4281 class Sink_type
: public Type
4290 do_compare_is_identity(Gogo
*)
4294 do_get_backend(Gogo
*)
4295 { go_unreachable(); }
4298 do_type_descriptor(Gogo
*, Named_type
*)
4299 { go_unreachable(); }
4302 do_reflection(Gogo
*, std::string
*) const
4303 { go_unreachable(); }
4306 do_mangled_name(Gogo
*, std::string
*) const
4307 { go_unreachable(); }
4310 // Make the sink type.
4313 Type::make_sink_type()
4315 static Sink_type sink_type
;
4319 // Class Function_type.
4324 Function_type::do_traverse(Traverse
* traverse
)
4326 if (this->receiver_
!= NULL
4327 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
4328 return TRAVERSE_EXIT
;
4329 if (this->parameters_
!= NULL
4330 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
4331 return TRAVERSE_EXIT
;
4332 if (this->results_
!= NULL
4333 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
4334 return TRAVERSE_EXIT
;
4335 return TRAVERSE_CONTINUE
;
4338 // Returns whether T is a valid redeclaration of this type. If this
4339 // returns false, and REASON is not NULL, *REASON may be set to a
4340 // brief explanation of why it returned false.
4343 Function_type::is_valid_redeclaration(const Function_type
* t
,
4344 std::string
* reason
) const
4346 if (!this->is_identical(t
, false, COMPARE_TAGS
, true, reason
))
4349 // A redeclaration of a function is required to use the same names
4350 // for the receiver and parameters.
4351 if (this->receiver() != NULL
4352 && this->receiver()->name() != t
->receiver()->name())
4355 *reason
= "receiver name changed";
4359 const Typed_identifier_list
* parms1
= this->parameters();
4360 const Typed_identifier_list
* parms2
= t
->parameters();
4363 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4364 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4365 p2
!= parms2
->end();
4368 if (p1
->name() != p2
->name())
4371 *reason
= "parameter name changed";
4375 // This is called at parse time, so we may have unknown
4377 Type
* t1
= p1
->type()->forwarded();
4378 Type
* t2
= p2
->type()->forwarded();
4380 && t1
->forward_declaration_type() != NULL
4381 && (t2
->forward_declaration_type() == NULL
4382 || (t1
->forward_declaration_type()->named_object()
4383 != t2
->forward_declaration_type()->named_object())))
4388 const Typed_identifier_list
* results1
= this->results();
4389 const Typed_identifier_list
* results2
= t
->results();
4390 if (results1
!= NULL
)
4392 Typed_identifier_list::const_iterator res1
= results1
->begin();
4393 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4394 res2
!= results2
->end();
4397 if (res1
->name() != res2
->name())
4400 *reason
= "result name changed";
4404 // This is called at parse time, so we may have unknown
4406 Type
* t1
= res1
->type()->forwarded();
4407 Type
* t2
= res2
->type()->forwarded();
4409 && t1
->forward_declaration_type() != NULL
4410 && (t2
->forward_declaration_type() == NULL
4411 || (t1
->forward_declaration_type()->named_object()
4412 != t2
->forward_declaration_type()->named_object())))
4420 // Check whether T is the same as this type.
4423 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
4424 Cmp_tags cmp_tags
, bool errors_are_identical
,
4425 std::string
* reason
) const
4427 if (!ignore_receiver
)
4429 const Typed_identifier
* r1
= this->receiver();
4430 const Typed_identifier
* r2
= t
->receiver();
4431 if ((r1
!= NULL
) != (r2
!= NULL
))
4434 *reason
= _("different receiver types");
4439 if (!Type::are_identical_cmp_tags(r1
->type(), r2
->type(), cmp_tags
,
4440 errors_are_identical
, reason
))
4442 if (reason
!= NULL
&& !reason
->empty())
4443 *reason
= "receiver: " + *reason
;
4449 const Typed_identifier_list
* parms1
= this->parameters();
4450 const Typed_identifier_list
* parms2
= t
->parameters();
4451 if ((parms1
!= NULL
) != (parms2
!= NULL
))
4454 *reason
= _("different number of parameters");
4459 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4460 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4461 p2
!= parms2
->end();
4464 if (p1
== parms1
->end())
4467 *reason
= _("different number of parameters");
4471 if (!Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
4472 errors_are_identical
, NULL
))
4475 *reason
= _("different parameter types");
4479 if (p1
!= parms1
->end())
4482 *reason
= _("different number of parameters");
4487 if (this->is_varargs() != t
->is_varargs())
4490 *reason
= _("different varargs");
4494 const Typed_identifier_list
* results1
= this->results();
4495 const Typed_identifier_list
* results2
= t
->results();
4496 if ((results1
!= NULL
) != (results2
!= NULL
))
4499 *reason
= _("different number of results");
4502 if (results1
!= NULL
)
4504 Typed_identifier_list::const_iterator res1
= results1
->begin();
4505 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4506 res2
!= results2
->end();
4509 if (res1
== results1
->end())
4512 *reason
= _("different number of results");
4516 if (!Type::are_identical_cmp_tags(res1
->type(), res2
->type(),
4517 cmp_tags
, errors_are_identical
,
4521 *reason
= _("different result types");
4525 if (res1
!= results1
->end())
4528 *reason
= _("different number of results");
4539 Function_type::do_hash_for_method(Gogo
* gogo
) const
4541 unsigned int ret
= 0;
4542 // We ignore the receiver type for hash codes, because we need to
4543 // get the same hash code for a method in an interface and a method
4544 // declared for a type. The former will not have a receiver.
4545 if (this->parameters_
!= NULL
)
4548 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
4549 p
!= this->parameters_
->end();
4551 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4553 if (this->results_
!= NULL
)
4556 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
4557 p
!= this->results_
->end();
4559 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4561 if (this->is_varargs_
)
4567 // Hash result parameters.
4570 Function_type::Results_hash::operator()(const Typed_identifier_list
* t
) const
4572 unsigned int hash
= 0;
4573 for (Typed_identifier_list::const_iterator p
= t
->begin();
4578 hash
= Type::hash_string(p
->name(), hash
);
4579 hash
+= p
->type()->hash_for_method(NULL
);
4584 // Compare result parameters so that can map identical result
4585 // parameters to a single struct type.
4588 Function_type::Results_equal::operator()(const Typed_identifier_list
* a
,
4589 const Typed_identifier_list
* b
) const
4591 if (a
->size() != b
->size())
4593 Typed_identifier_list::const_iterator pa
= a
->begin();
4594 for (Typed_identifier_list::const_iterator pb
= b
->begin();
4598 if (pa
->name() != pb
->name()
4599 || !Type::are_identical(pa
->type(), pb
->type(), true, NULL
))
4605 // Hash from results to a backend struct type.
4607 Function_type::Results_structs
Function_type::results_structs
;
4609 // Get the backend representation for a function type.
4612 Function_type::get_backend_fntype(Gogo
* gogo
)
4614 if (this->fnbtype_
== NULL
)
4616 Backend::Btyped_identifier breceiver
;
4617 if (this->receiver_
!= NULL
)
4619 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
4621 // We always pass the address of the receiver parameter, in
4622 // order to make interface calls work with unknown types.
4623 Type
* rtype
= this->receiver_
->type();
4624 if (rtype
->points_to() == NULL
)
4625 rtype
= Type::make_pointer_type(rtype
);
4626 breceiver
.btype
= rtype
->get_backend(gogo
);
4627 breceiver
.location
= this->receiver_
->location();
4630 std::vector
<Backend::Btyped_identifier
> bparameters
;
4631 if (this->parameters_
!= NULL
)
4633 bparameters
.resize(this->parameters_
->size());
4635 for (Typed_identifier_list::const_iterator p
=
4636 this->parameters_
->begin(); p
!= this->parameters_
->end();
4639 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4640 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
4641 bparameters
[i
].location
= p
->location();
4643 go_assert(i
== bparameters
.size());
4646 std::vector
<Backend::Btyped_identifier
> bresults
;
4647 Btype
* bresult_struct
= NULL
;
4648 if (this->results_
!= NULL
)
4650 bresults
.resize(this->results_
->size());
4652 for (Typed_identifier_list::const_iterator p
=
4653 this->results_
->begin();
4654 p
!= this->results_
->end();
4657 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4658 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
4659 bresults
[i
].location
= p
->location();
4661 go_assert(i
== bresults
.size());
4663 if (this->results_
->size() > 1)
4665 // Use the same results struct for all functions that
4666 // return the same set of results. This is useful to
4667 // unify calls to interface methods with other calls.
4668 std::pair
<Typed_identifier_list
*, Btype
*> val
;
4669 val
.first
= this->results_
;
4671 std::pair
<Results_structs::iterator
, bool> ins
=
4672 Function_type::results_structs
.insert(val
);
4675 // Build a new struct type.
4676 Struct_field_list
* sfl
= new Struct_field_list
;
4677 for (Typed_identifier_list::const_iterator p
=
4678 this->results_
->begin();
4679 p
!= this->results_
->end();
4682 Typed_identifier tid
= *p
;
4683 if (tid
.name().empty())
4684 tid
= Typed_identifier("UNNAMED", tid
.type(),
4686 sfl
->push_back(Struct_field(tid
));
4688 Struct_type
* st
= Type::make_struct_type(sfl
,
4690 st
->set_is_struct_incomparable();
4691 ins
.first
->second
= st
->get_backend(gogo
);
4693 bresult_struct
= ins
.first
->second
;
4697 this->fnbtype_
= gogo
->backend()->function_type(breceiver
, bparameters
,
4698 bresults
, bresult_struct
,
4703 return this->fnbtype_
;
4706 // Get the backend representation for a Go function type.
4709 Function_type::do_get_backend(Gogo
* gogo
)
4711 // When we do anything with a function value other than call it, it
4712 // is represented as a pointer to a struct whose first field is the
4713 // actual function. So that is what we return as the type of a Go
4716 Location loc
= this->location();
4717 Btype
* struct_type
=
4718 gogo
->backend()->placeholder_struct_type("__go_descriptor", loc
);
4719 Btype
* ptr_struct_type
= gogo
->backend()->pointer_type(struct_type
);
4721 std::vector
<Backend::Btyped_identifier
> fields(1);
4722 fields
[0].name
= "code";
4723 fields
[0].btype
= this->get_backend_fntype(gogo
);
4724 fields
[0].location
= loc
;
4725 if (!gogo
->backend()->set_placeholder_struct_type(struct_type
, fields
))
4726 return gogo
->backend()->error_type();
4727 return ptr_struct_type
;
4730 // The type of a function type descriptor.
4733 Function_type::make_function_type_descriptor_type()
4738 Type
* tdt
= Type::make_type_descriptor_type();
4739 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4741 Type
* bool_type
= Type::lookup_bool_type();
4743 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
4745 Struct_type
* s
= Type::make_builtin_struct_type(4,
4747 "dotdotdot", bool_type
,
4751 ret
= Type::make_builtin_named_type("FuncType", s
);
4757 // The type descriptor for a function type.
4760 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4762 Location bloc
= Linemap::predeclared_location();
4764 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
4766 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
4768 Expression_list
* vals
= new Expression_list();
4771 Struct_field_list::const_iterator p
= fields
->begin();
4772 go_assert(p
->is_field_name("_type"));
4773 vals
->push_back(this->type_descriptor_constructor(gogo
,
4774 RUNTIME_TYPE_KIND_FUNC
,
4778 go_assert(p
->is_field_name("dotdotdot"));
4779 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
4782 go_assert(p
->is_field_name("in"));
4783 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
4784 this->parameters()));
4787 go_assert(p
->is_field_name("out"));
4788 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
4792 go_assert(p
== fields
->end());
4794 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
4797 // Return a composite literal for the parameters or results of a type
4801 Function_type::type_descriptor_params(Type
* params_type
,
4802 const Typed_identifier
* receiver
,
4803 const Typed_identifier_list
* params
)
4805 Location bloc
= Linemap::predeclared_location();
4807 if (receiver
== NULL
&& params
== NULL
)
4808 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
4810 Expression_list
* vals
= new Expression_list();
4811 vals
->reserve((params
== NULL
? 0 : params
->size())
4812 + (receiver
!= NULL
? 1 : 0));
4814 if (receiver
!= NULL
)
4815 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
4819 for (Typed_identifier_list::const_iterator p
= params
->begin();
4822 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
4825 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
4828 // The reflection string.
4831 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4833 // FIXME: Turn this off until we straighten out the type of the
4834 // struct field used in a go statement which calls a method.
4835 // go_assert(this->receiver_ == NULL);
4837 ret
->append("func");
4839 if (this->receiver_
!= NULL
)
4841 ret
->push_back('(');
4842 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
4843 ret
->push_back(')');
4846 ret
->push_back('(');
4847 const Typed_identifier_list
* params
= this->parameters();
4850 bool is_varargs
= this->is_varargs_
;
4851 for (Typed_identifier_list::const_iterator p
= params
->begin();
4855 if (p
!= params
->begin())
4857 if (!is_varargs
|| p
+ 1 != params
->end())
4858 this->append_reflection(p
->type(), gogo
, ret
);
4862 this->append_reflection(p
->type()->array_type()->element_type(),
4867 ret
->push_back(')');
4869 const Typed_identifier_list
* results
= this->results();
4870 if (results
!= NULL
&& !results
->empty())
4872 if (results
->size() == 1)
4873 ret
->push_back(' ');
4876 for (Typed_identifier_list::const_iterator p
= results
->begin();
4877 p
!= results
->end();
4880 if (p
!= results
->begin())
4882 this->append_reflection(p
->type(), gogo
, ret
);
4884 if (results
->size() > 1)
4885 ret
->push_back(')');
4889 // Export a function type.
4892 Function_type::do_export(Export
* exp
) const
4894 // We don't write out the receiver. The only function types which
4895 // should have a receiver are the ones associated with explicitly
4896 // defined methods. For those the receiver type is written out by
4897 // Function::export_func.
4899 exp
->write_c_string("(");
4901 if (this->parameters_
!= NULL
)
4903 bool is_varargs
= this->is_varargs_
;
4904 for (Typed_identifier_list::const_iterator p
=
4905 this->parameters_
->begin();
4906 p
!= this->parameters_
->end();
4912 exp
->write_c_string(", ");
4913 exp
->write_name(p
->name());
4914 exp
->write_c_string(" ");
4915 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
4916 exp
->write_type(p
->type());
4919 exp
->write_c_string("...");
4920 exp
->write_type(p
->type()->array_type()->element_type());
4924 exp
->write_c_string(")");
4926 const Typed_identifier_list
* results
= this->results_
;
4927 if (results
!= NULL
)
4929 exp
->write_c_string(" ");
4930 if (results
->size() == 1 && results
->begin()->name().empty())
4931 exp
->write_type(results
->begin()->type());
4935 exp
->write_c_string("(");
4936 for (Typed_identifier_list::const_iterator p
= results
->begin();
4937 p
!= results
->end();
4943 exp
->write_c_string(", ");
4944 exp
->write_name(p
->name());
4945 exp
->write_c_string(" ");
4946 exp
->write_type(p
->type());
4948 exp
->write_c_string(")");
4953 // Import a function type.
4956 Function_type::do_import(Import
* imp
)
4958 imp
->require_c_string("(");
4959 Typed_identifier_list
* parameters
;
4960 bool is_varargs
= false;
4961 if (imp
->peek_char() == ')')
4965 parameters
= new Typed_identifier_list();
4968 std::string name
= imp
->read_name();
4969 imp
->require_c_string(" ");
4971 if (imp
->match_c_string("..."))
4977 Type
* ptype
= imp
->read_type();
4979 ptype
= Type::make_array_type(ptype
, NULL
);
4980 parameters
->push_back(Typed_identifier(name
, ptype
,
4982 if (imp
->peek_char() != ',')
4984 go_assert(!is_varargs
);
4985 imp
->require_c_string(", ");
4988 imp
->require_c_string(")");
4990 Typed_identifier_list
* results
;
4991 if (imp
->peek_char() != ' ')
4996 results
= new Typed_identifier_list
;
4997 if (imp
->peek_char() != '(')
4999 Type
* rtype
= imp
->read_type();
5000 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
5007 std::string name
= imp
->read_name();
5008 imp
->require_c_string(" ");
5009 Type
* rtype
= imp
->read_type();
5010 results
->push_back(Typed_identifier(name
, rtype
,
5012 if (imp
->peek_char() != ',')
5014 imp
->require_c_string(", ");
5016 imp
->require_c_string(")");
5020 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
5023 ret
->set_is_varargs();
5027 // Make a copy of a function type without a receiver.
5030 Function_type::copy_without_receiver() const
5032 go_assert(this->is_method());
5033 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
5036 if (this->is_varargs())
5037 ret
->set_is_varargs();
5038 if (this->is_builtin())
5039 ret
->set_is_builtin();
5043 // Make a copy of a function type with a receiver.
5046 Function_type::copy_with_receiver(Type
* receiver_type
) const
5048 go_assert(!this->is_method());
5049 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
5051 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
5054 if (this->is_varargs_
)
5055 ret
->set_is_varargs();
5059 // Make a copy of a function type with the receiver as the first
5063 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver
) const
5065 go_assert(this->is_method());
5066 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5067 Type
* rtype
= this->receiver_
->type();
5068 if (want_pointer_receiver
)
5069 rtype
= Type::make_pointer_type(rtype
);
5070 Typed_identifier
receiver(this->receiver_
->name(), rtype
,
5071 this->receiver_
->location());
5072 new_params
->push_back(receiver
);
5073 const Typed_identifier_list
* orig_params
= this->parameters_
;
5074 if (orig_params
!= NULL
&& !orig_params
->empty())
5076 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5077 p
!= orig_params
->end();
5079 new_params
->push_back(*p
);
5081 return Type::make_function_type(NULL
, new_params
, this->results_
,
5085 // Make a copy of a function type ignoring any receiver and adding a
5086 // closure parameter.
5089 Function_type::copy_with_names() const
5091 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5092 const Typed_identifier_list
* orig_params
= this->parameters_
;
5093 if (orig_params
!= NULL
&& !orig_params
->empty())
5097 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5098 p
!= orig_params
->end();
5101 snprintf(buf
, sizeof buf
, "pt.%u", count
);
5103 new_params
->push_back(Typed_identifier(buf
, p
->type(),
5108 const Typed_identifier_list
* orig_results
= this->results_
;
5109 Typed_identifier_list
* new_results
;
5110 if (orig_results
== NULL
|| orig_results
->empty())
5114 new_results
= new Typed_identifier_list();
5115 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
5116 p
!= orig_results
->end();
5118 new_results
->push_back(Typed_identifier("", p
->type(),
5122 return Type::make_function_type(NULL
, new_params
, new_results
,
5126 // Make a function type.
5129 Type::make_function_type(Typed_identifier
* receiver
,
5130 Typed_identifier_list
* parameters
,
5131 Typed_identifier_list
* results
,
5134 return new Function_type(receiver
, parameters
, results
, location
);
5137 // Make a backend function type.
5139 Backend_function_type
*
5140 Type::make_backend_function_type(Typed_identifier
* receiver
,
5141 Typed_identifier_list
* parameters
,
5142 Typed_identifier_list
* results
,
5145 return new Backend_function_type(receiver
, parameters
, results
, location
);
5148 // Class Pointer_type.
5153 Pointer_type::do_traverse(Traverse
* traverse
)
5155 return Type::traverse(this->to_type_
, traverse
);
5161 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
5163 return this->to_type_
->hash_for_method(gogo
) << 4;
5166 // Get the backend representation for a pointer type.
5169 Pointer_type::do_get_backend(Gogo
* gogo
)
5171 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
5172 return gogo
->backend()->pointer_type(to_btype
);
5175 // The type of a pointer type descriptor.
5178 Pointer_type::make_pointer_type_descriptor_type()
5183 Type
* tdt
= Type::make_type_descriptor_type();
5184 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5186 Struct_type
* s
= Type::make_builtin_struct_type(2,
5190 ret
= Type::make_builtin_named_type("PtrType", s
);
5196 // The type descriptor for a pointer type.
5199 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5201 if (this->is_unsafe_pointer_type())
5203 go_assert(name
!= NULL
);
5204 return this->plain_type_descriptor(gogo
,
5205 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
5210 Location bloc
= Linemap::predeclared_location();
5212 const Methods
* methods
;
5213 Type
* deref
= this->points_to();
5214 if (deref
->named_type() != NULL
)
5215 methods
= deref
->named_type()->methods();
5216 else if (deref
->struct_type() != NULL
)
5217 methods
= deref
->struct_type()->methods();
5221 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
5223 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
5225 Expression_list
* vals
= new Expression_list();
5228 Struct_field_list::const_iterator p
= fields
->begin();
5229 go_assert(p
->is_field_name("_type"));
5230 vals
->push_back(this->type_descriptor_constructor(gogo
,
5231 RUNTIME_TYPE_KIND_PTR
,
5232 name
, methods
, false));
5235 go_assert(p
->is_field_name("elem"));
5236 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
5238 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
5242 // Reflection string.
5245 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5247 ret
->push_back('*');
5248 this->append_reflection(this->to_type_
, gogo
, ret
);
5254 Pointer_type::do_export(Export
* exp
) const
5256 exp
->write_c_string("*");
5257 if (this->is_unsafe_pointer_type())
5258 exp
->write_c_string("any");
5260 exp
->write_type(this->to_type_
);
5266 Pointer_type::do_import(Import
* imp
)
5268 imp
->require_c_string("*");
5269 if (imp
->match_c_string("any"))
5272 return Type::make_pointer_type(Type::make_void_type());
5274 Type
* to
= imp
->read_type();
5275 return Type::make_pointer_type(to
);
5278 // Cache of pointer types. Key is "to" type, value is pointer type
5279 // that points to key.
5281 Type::Pointer_type_table
Type::pointer_types
;
5283 // A list of placeholder pointer types. We keep this so we can ensure
5284 // they are finalized.
5286 std::vector
<Pointer_type
*> Type::placeholder_pointers
;
5288 // Make a pointer type.
5291 Type::make_pointer_type(Type
* to_type
)
5293 Pointer_type_table::const_iterator p
= pointer_types
.find(to_type
);
5294 if (p
!= pointer_types
.end())
5296 Pointer_type
* ret
= new Pointer_type(to_type
);
5297 pointer_types
[to_type
] = ret
;
5301 // This helper is invoked immediately after named types have been
5302 // converted, to clean up any unresolved pointer types remaining in
5303 // the pointer type cache.
5305 // The motivation for this routine: occasionally the compiler creates
5306 // some specific pointer type as part of a lowering operation (ex:
5307 // pointer-to-void), then Type::backend_type_size() is invoked on the
5308 // type (which creates a Btype placeholder for it), that placeholder
5309 // passed somewhere along the line to the back end, but since there is
5310 // no reference to the type in user code, there is never a call to
5311 // Type::finish_backend for the type (hence the Btype remains as an
5312 // unresolved placeholder). Calling this routine will clean up such
5316 Type::finish_pointer_types(Gogo
* gogo
)
5318 // We don't use begin() and end() because it is possible to add new
5319 // placeholder pointer types as we finalized existing ones.
5320 for (size_t i
= 0; i
< Type::placeholder_pointers
.size(); i
++)
5322 Pointer_type
* pt
= Type::placeholder_pointers
[i
];
5323 Type_btypes::iterator tbti
= Type::type_btypes
.find(pt
);
5324 if (tbti
!= Type::type_btypes
.end() && tbti
->second
.is_placeholder
)
5326 pt
->finish_backend(gogo
, tbti
->second
.btype
);
5327 tbti
->second
.is_placeholder
= false;
5334 // Get the backend representation of a nil type. FIXME: Is this ever
5338 Nil_type::do_get_backend(Gogo
* gogo
)
5340 return gogo
->backend()->pointer_type(gogo
->backend()->void_type());
5343 // Make the nil type.
5346 Type::make_nil_type()
5348 static Nil_type singleton_nil_type
;
5349 return &singleton_nil_type
;
5352 // The type of a function call which returns multiple values. This is
5353 // really a struct, but we don't want to confuse a function call which
5354 // returns a struct with a function call which returns multiple
5357 class Call_multiple_result_type
: public Type
5360 Call_multiple_result_type(Call_expression
* call
)
5361 : Type(TYPE_CALL_MULTIPLE_RESULT
),
5367 do_has_pointer() const
5371 do_compare_is_identity(Gogo
*)
5375 do_get_backend(Gogo
* gogo
)
5377 go_assert(saw_errors());
5378 return gogo
->backend()->error_type();
5382 do_type_descriptor(Gogo
*, Named_type
*)
5384 go_assert(saw_errors());
5385 return Expression::make_error(Linemap::unknown_location());
5389 do_reflection(Gogo
*, std::string
*) const
5390 { go_assert(saw_errors()); }
5393 do_mangled_name(Gogo
*, std::string
*) const
5394 { go_assert(saw_errors()); }
5397 // The expression being called.
5398 Call_expression
* call_
;
5401 // Make a call result type.
5404 Type::make_call_multiple_result_type(Call_expression
* call
)
5406 return new Call_multiple_result_type(call
);
5409 // Class Struct_field.
5411 // Get the name of a field.
5414 Struct_field::field_name() const
5416 const std::string
& name(this->typed_identifier_
.name());
5421 // This is called during parsing, before anything is lowered, so
5422 // we have to be pretty careful to avoid dereferencing an
5423 // unknown type name.
5424 Type
* t
= this->typed_identifier_
.type();
5426 if (t
->classification() == Type::TYPE_POINTER
)
5429 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
5430 dt
= ptype
->points_to();
5432 if (dt
->forward_declaration_type() != NULL
)
5433 return dt
->forward_declaration_type()->name();
5434 else if (dt
->named_type() != NULL
)
5436 // Note that this can be an alias name.
5437 return dt
->named_type()->name();
5439 else if (t
->is_error_type() || dt
->is_error_type())
5441 static const std::string error_string
= "*error*";
5442 return error_string
;
5446 // Avoid crashing in the erroneous case where T is named but
5449 if (t
->forward_declaration_type() != NULL
)
5450 return t
->forward_declaration_type()->name();
5451 else if (t
->named_type() != NULL
)
5452 return t
->named_type()->name();
5459 // Return whether this field is named NAME.
5462 Struct_field::is_field_name(const std::string
& name
) const
5464 const std::string
& me(this->typed_identifier_
.name());
5469 Type
* t
= this->typed_identifier_
.type();
5470 if (t
->points_to() != NULL
)
5472 Named_type
* nt
= t
->named_type();
5473 if (nt
!= NULL
&& nt
->name() == name
)
5476 // This is a horrible hack caused by the fact that we don't pack
5477 // the names of builtin types. FIXME.
5478 if (!this->is_imported_
5481 && nt
->name() == Gogo::unpack_hidden_name(name
))
5488 // Return whether this field is an unexported field named NAME.
5491 Struct_field::is_unexported_field_name(Gogo
* gogo
,
5492 const std::string
& name
) const
5494 const std::string
& field_name(this->field_name());
5495 if (Gogo::is_hidden_name(field_name
)
5496 && name
== Gogo::unpack_hidden_name(field_name
)
5497 && gogo
->pack_hidden_name(name
, false) != field_name
)
5500 // Check for the name of a builtin type. This is like the test in
5501 // is_field_name, only there we return false if this->is_imported_,
5502 // and here we return true.
5503 if (this->is_imported_
&& this->is_anonymous())
5505 Type
* t
= this->typed_identifier_
.type();
5506 if (t
->points_to() != NULL
)
5508 Named_type
* nt
= t
->named_type();
5511 && nt
->name() == Gogo::unpack_hidden_name(name
))
5518 // Return whether this field is an embedded built-in type.
5521 Struct_field::is_embedded_builtin(Gogo
* gogo
) const
5523 const std::string
& name(this->field_name());
5524 // We know that a field is an embedded type if it is anonymous.
5525 // We can decide if it is a built-in type by checking to see if it is
5526 // registered globally under the field's name.
5527 // This allows us to distinguish between embedded built-in types and
5528 // embedded types that are aliases to built-in types.
5529 return (this->is_anonymous()
5530 && !Gogo::is_hidden_name(name
)
5531 && gogo
->lookup_global(name
.c_str()) != NULL
);
5534 // Class Struct_type.
5536 // A hash table used to find identical unnamed structs so that they
5537 // share method tables.
5539 Struct_type::Identical_structs
Struct_type::identical_structs
;
5541 // A hash table used to merge method sets for identical unnamed
5544 Struct_type::Struct_method_tables
Struct_type::struct_method_tables
;
5549 Struct_type::do_traverse(Traverse
* traverse
)
5551 Struct_field_list
* fields
= this->fields_
;
5554 for (Struct_field_list::iterator p
= fields
->begin();
5558 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
5559 return TRAVERSE_EXIT
;
5562 return TRAVERSE_CONTINUE
;
5565 // Verify that the struct type is complete and valid.
5568 Struct_type::do_verify()
5570 Struct_field_list
* fields
= this->fields_
;
5573 for (Struct_field_list::iterator p
= fields
->begin();
5577 Type
* t
= p
->type();
5578 if (p
->is_anonymous())
5580 if ((t
->named_type() != NULL
&& t
->points_to() != NULL
)
5581 || (t
->named_type() == NULL
&& t
->points_to() != NULL
5582 && t
->points_to()->points_to() != NULL
))
5584 go_error_at(p
->location(), "embedded type may not be a pointer");
5585 p
->set_type(Type::make_error_type());
5587 else if (t
->points_to() != NULL
5588 && t
->points_to()->interface_type() != NULL
)
5590 go_error_at(p
->location(),
5591 "embedded type may not be pointer to interface");
5592 p
->set_type(Type::make_error_type());
5599 // Whether this contains a pointer.
5602 Struct_type::do_has_pointer() const
5604 const Struct_field_list
* fields
= this->fields();
5607 for (Struct_field_list::const_iterator p
= fields
->begin();
5611 if (p
->type()->has_pointer())
5617 // Whether this type is identical to T.
5620 Struct_type::is_identical(const Struct_type
* t
, Cmp_tags cmp_tags
,
5621 bool errors_are_identical
) const
5623 if (this->is_struct_incomparable_
!= t
->is_struct_incomparable_
)
5625 const Struct_field_list
* fields1
= this->fields();
5626 const Struct_field_list
* fields2
= t
->fields();
5627 if (fields1
== NULL
|| fields2
== NULL
)
5628 return fields1
== fields2
;
5629 Struct_field_list::const_iterator pf2
= fields2
->begin();
5630 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
5631 pf1
!= fields1
->end();
5634 if (pf2
== fields2
->end())
5636 if (pf1
->field_name() != pf2
->field_name())
5638 if (pf1
->is_anonymous() != pf2
->is_anonymous()
5639 || !Type::are_identical_cmp_tags(pf1
->type(), pf2
->type(), cmp_tags
,
5640 errors_are_identical
, NULL
))
5642 if (cmp_tags
== COMPARE_TAGS
)
5644 if (!pf1
->has_tag())
5651 if (!pf2
->has_tag())
5653 if (pf1
->tag() != pf2
->tag())
5658 if (pf2
!= fields2
->end())
5663 // Whether comparisons of this struct type are simple identity
5667 Struct_type::do_compare_is_identity(Gogo
* gogo
)
5669 const Struct_field_list
* fields
= this->fields_
;
5673 for (Struct_field_list::const_iterator pf
= fields
->begin();
5674 pf
!= fields
->end();
5677 if (Gogo::is_sink_name(pf
->field_name()))
5680 if (!pf
->type()->compare_is_identity(gogo
))
5683 int64_t field_align
;
5684 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
5686 if ((offset
& (field_align
- 1)) != 0)
5688 // This struct has padding. We don't guarantee that that
5689 // padding is zero-initialized for a stack variable, so we
5690 // can't use memcmp to compare struct values.
5695 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
5697 offset
+= field_size
;
5700 int64_t struct_size
;
5701 if (!this->backend_type_size(gogo
, &struct_size
))
5703 if (offset
!= struct_size
)
5705 // Trailing padding may not be zero when on the stack.
5712 // Return whether this struct type is reflexive--whether a value of
5713 // this type is always equal to itself.
5716 Struct_type::do_is_reflexive()
5718 const Struct_field_list
* fields
= this->fields_
;
5721 for (Struct_field_list::const_iterator pf
= fields
->begin();
5722 pf
!= fields
->end();
5725 if (!pf
->type()->is_reflexive())
5731 // Return whether this struct type needs a key update when used as a
5735 Struct_type::do_needs_key_update()
5737 const Struct_field_list
* fields
= this->fields_
;
5740 for (Struct_field_list::const_iterator pf
= fields
->begin();
5741 pf
!= fields
->end();
5744 if (pf
->type()->needs_key_update())
5750 // Return whether this struct type is permitted to be in the heap.
5753 Struct_type::do_in_heap()
5755 const Struct_field_list
* fields
= this->fields_
;
5758 for (Struct_field_list::const_iterator pf
= fields
->begin();
5759 pf
!= fields
->end();
5762 if (!pf
->type()->in_heap())
5768 // Build identity and hash functions for this struct.
5773 Struct_type::do_hash_for_method(Gogo
* gogo
) const
5775 unsigned int ret
= 0;
5776 if (this->fields() != NULL
)
5778 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
5779 pf
!= this->fields()->end();
5781 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
5784 if (this->is_struct_incomparable_
)
5789 // Find the local field NAME.
5792 Struct_type::find_local_field(const std::string
& name
,
5793 unsigned int *pindex
) const
5795 const Struct_field_list
* fields
= this->fields_
;
5799 for (Struct_field_list::const_iterator pf
= fields
->begin();
5800 pf
!= fields
->end();
5803 if (pf
->is_field_name(name
))
5813 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
5815 Field_reference_expression
*
5816 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
5817 Location location
) const
5820 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
5824 // Return an expression for a field, along with the depth at which it
5827 Field_reference_expression
*
5828 Struct_type::field_reference_depth(Expression
* struct_expr
,
5829 const std::string
& name
,
5831 Saw_named_type
* saw
,
5832 unsigned int* depth
) const
5834 const Struct_field_list
* fields
= this->fields_
;
5838 // Look for a field with this name.
5840 for (Struct_field_list::const_iterator pf
= fields
->begin();
5841 pf
!= fields
->end();
5844 if (pf
->is_field_name(name
))
5847 return Expression::make_field_reference(struct_expr
, i
, location
);
5851 // Look for an anonymous field which contains a field with this
5853 unsigned int found_depth
= 0;
5854 Field_reference_expression
* ret
= NULL
;
5856 for (Struct_field_list::const_iterator pf
= fields
->begin();
5857 pf
!= fields
->end();
5860 if (!pf
->is_anonymous())
5863 Struct_type
* st
= pf
->type()->deref()->struct_type();
5867 Saw_named_type
* hold_saw
= saw
;
5868 Saw_named_type saw_here
;
5869 Named_type
* nt
= pf
->type()->named_type();
5871 nt
= pf
->type()->deref()->named_type();
5875 for (q
= saw
; q
!= NULL
; q
= q
->next
)
5879 // If this is an error, it will be reported
5886 saw_here
.next
= saw
;
5891 // Look for a reference using a NULL struct expression. If we
5892 // find one, fill in the struct expression with a reference to
5894 unsigned int subdepth
;
5895 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
5905 if (ret
== NULL
|| subdepth
< found_depth
)
5910 found_depth
= subdepth
;
5911 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
5913 if (pf
->type()->points_to() != NULL
)
5914 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
5915 while (sub
->expr() != NULL
)
5917 sub
= sub
->expr()->deref()->field_reference_expression();
5918 go_assert(sub
!= NULL
);
5920 sub
->set_struct_expression(here
);
5921 sub
->set_implicit(true);
5923 else if (subdepth
> found_depth
)
5927 // We do not handle ambiguity here--it should be handled by
5928 // Type::bind_field_or_method.
5936 *depth
= found_depth
+ 1;
5941 // Return the total number of fields, including embedded fields.
5944 Struct_type::total_field_count() const
5946 if (this->fields_
== NULL
)
5948 unsigned int ret
= 0;
5949 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
5950 pf
!= this->fields_
->end();
5953 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
5956 ret
+= pf
->type()->struct_type()->total_field_count();
5961 // Return whether NAME is an unexported field, for better error reporting.
5964 Struct_type::is_unexported_local_field(Gogo
* gogo
,
5965 const std::string
& name
) const
5967 const Struct_field_list
* fields
= this->fields_
;
5970 for (Struct_field_list::const_iterator pf
= fields
->begin();
5971 pf
!= fields
->end();
5973 if (pf
->is_unexported_field_name(gogo
, name
))
5979 // Finalize the methods of an unnamed struct.
5982 Struct_type::finalize_methods(Gogo
* gogo
)
5984 if (this->all_methods_
!= NULL
)
5987 // It is possible to have multiple identical structs that have
5988 // methods. We want them to share method tables. Otherwise we will
5989 // emit identical methods more than once, which is bad since they
5990 // will even have the same names.
5991 std::pair
<Identical_structs::iterator
, bool> ins
=
5992 Struct_type::identical_structs
.insert(std::make_pair(this, this));
5995 // An identical struct was already entered into the hash table.
5996 // Note that finalize_methods is, fortunately, not recursive.
5997 this->all_methods_
= ins
.first
->second
->all_methods_
;
6001 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6004 // Return the method NAME, or NULL if there isn't one or if it is
6005 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6009 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6011 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6014 // Return a pointer to the interface method table for this type for
6015 // the interface INTERFACE. IS_POINTER is true if this is for a
6019 Struct_type::interface_method_table(Interface_type
* interface
,
6022 std::pair
<Struct_type
*, Struct_type::Struct_method_table_pair
*>
6024 std::pair
<Struct_type::Struct_method_tables::iterator
, bool> ins
=
6025 Struct_type::struct_method_tables
.insert(val
);
6027 Struct_method_table_pair
* smtp
;
6029 smtp
= ins
.first
->second
;
6032 smtp
= new Struct_method_table_pair();
6034 smtp
->second
= NULL
;
6035 ins
.first
->second
= smtp
;
6038 return Type::interface_method_table(this, interface
, is_pointer
,
6039 &smtp
->first
, &smtp
->second
);
6042 // Convert struct fields to the backend representation. This is not
6043 // declared in types.h so that types.h doesn't have to #include
6047 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
6048 bool use_placeholder
,
6049 std::vector
<Backend::Btyped_identifier
>* bfields
)
6051 bfields
->resize(fields
->size());
6053 for (Struct_field_list::const_iterator p
= fields
->begin();
6057 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
6058 (*bfields
)[i
].btype
= (use_placeholder
6059 ? p
->type()->get_backend_placeholder(gogo
)
6060 : p
->type()->get_backend(gogo
));
6061 (*bfields
)[i
].location
= p
->location();
6063 go_assert(i
== fields
->size());
6066 // Get the backend representation for a struct type.
6069 Struct_type::do_get_backend(Gogo
* gogo
)
6071 std::vector
<Backend::Btyped_identifier
> bfields
;
6072 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
6073 return gogo
->backend()->struct_type(bfields
);
6076 // Finish the backend representation of the fields of a struct.
6079 Struct_type::finish_backend_fields(Gogo
* gogo
)
6081 const Struct_field_list
* fields
= this->fields_
;
6084 for (Struct_field_list::const_iterator p
= fields
->begin();
6087 p
->type()->get_backend(gogo
);
6091 // The type of a struct type descriptor.
6094 Struct_type::make_struct_type_descriptor_type()
6099 Type
* tdt
= Type::make_type_descriptor_type();
6100 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6102 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6103 Type
* string_type
= Type::lookup_string_type();
6104 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6107 Type::make_builtin_struct_type(5,
6108 "name", pointer_string_type
,
6109 "pkgPath", pointer_string_type
,
6111 "tag", pointer_string_type
,
6112 "offsetAnon", uintptr_type
);
6113 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
6115 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
6117 Struct_type
* s
= Type::make_builtin_struct_type(2,
6119 "fields", slice_type
);
6121 ret
= Type::make_builtin_named_type("StructType", s
);
6127 // Build a type descriptor for a struct type.
6130 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6132 Location bloc
= Linemap::predeclared_location();
6134 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
6136 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
6138 Expression_list
* vals
= new Expression_list();
6141 const Methods
* methods
= this->methods();
6142 // A named struct should not have methods--the methods should attach
6143 // to the named type.
6144 go_assert(methods
== NULL
|| name
== NULL
);
6146 Struct_field_list::const_iterator ps
= fields
->begin();
6147 go_assert(ps
->is_field_name("_type"));
6148 vals
->push_back(this->type_descriptor_constructor(gogo
,
6149 RUNTIME_TYPE_KIND_STRUCT
,
6150 name
, methods
, true));
6153 go_assert(ps
->is_field_name("fields"));
6155 Expression_list
* elements
= new Expression_list();
6156 elements
->reserve(this->fields_
->size());
6157 Type
* element_type
= ps
->type()->array_type()->element_type();
6158 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
6159 pf
!= this->fields_
->end();
6162 const Struct_field_list
* f
= element_type
->struct_type()->fields();
6164 Expression_list
* fvals
= new Expression_list();
6167 Struct_field_list::const_iterator q
= f
->begin();
6168 go_assert(q
->is_field_name("name"));
6169 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
6170 Expression
* s
= Expression::make_string(n
, bloc
);
6171 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6174 go_assert(q
->is_field_name("pkgPath"));
6175 bool is_embedded_builtin
= pf
->is_embedded_builtin(gogo
);
6176 if (!Gogo::is_hidden_name(pf
->field_name()) && !is_embedded_builtin
)
6177 fvals
->push_back(Expression::make_nil(bloc
));
6181 if (is_embedded_builtin
)
6182 n
= gogo
->package_name();
6184 n
= Gogo::hidden_name_pkgpath(pf
->field_name());
6185 Expression
* s
= Expression::make_string(n
, bloc
);
6186 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6190 go_assert(q
->is_field_name("typ"));
6191 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
6194 go_assert(q
->is_field_name("tag"));
6196 fvals
->push_back(Expression::make_nil(bloc
));
6199 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
6200 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6204 go_assert(q
->is_field_name("offsetAnon"));
6205 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6206 Expression
* o
= Expression::make_struct_field_offset(this, &*pf
);
6207 Expression
* one
= Expression::make_integer_ul(1, uintptr_type
, bloc
);
6208 o
= Expression::make_binary(OPERATOR_LSHIFT
, o
, one
, bloc
);
6209 int av
= pf
->is_anonymous() ? 1 : 0;
6210 Expression
* anon
= Expression::make_integer_ul(av
, uintptr_type
, bloc
);
6211 o
= Expression::make_binary(OPERATOR_OR
, o
, anon
, bloc
);
6212 fvals
->push_back(o
);
6214 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
6216 elements
->push_back(v
);
6219 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
6222 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
6225 // Write the hash function for a struct which can not use the identity
6229 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
6230 Function_type
* hash_fntype
,
6231 Function_type
* equal_fntype
)
6233 Location bloc
= Linemap::predeclared_location();
6235 // The pointer to the struct that we are going to hash. This is an
6236 // argument to the hash function we are implementing here.
6237 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
6238 go_assert(key_arg
!= NULL
);
6239 Type
* key_arg_type
= key_arg
->var_value()->type();
6241 // The seed argument to the hash function.
6242 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
6243 go_assert(seed_arg
!= NULL
);
6245 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6247 // Make a temporary to hold the return value, initialized to the seed.
6248 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
6249 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
6251 gogo
->add_statement(retval
);
6253 // Make a temporary to hold the key as a uintptr.
6254 ref
= Expression::make_var_reference(key_arg
, bloc
);
6255 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
6256 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
6258 gogo
->add_statement(key
);
6260 // Loop over the struct fields.
6261 const Struct_field_list
* fields
= this->fields_
;
6262 for (Struct_field_list::const_iterator pf
= fields
->begin();
6263 pf
!= fields
->end();
6266 if (Gogo::is_sink_name(pf
->field_name()))
6269 // Get a pointer to the value of this field.
6270 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
6271 ref
= Expression::make_temporary_reference(key
, bloc
);
6272 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
6274 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
6276 // Get the hash function to use for the type of this field.
6277 Named_object
* hash_fn
;
6278 Named_object
* equal_fn
;
6279 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
6280 equal_fntype
, &hash_fn
, &equal_fn
);
6282 // Call the hash function for the field, passing retval as the seed.
6283 ref
= Expression::make_temporary_reference(retval
, bloc
);
6284 Expression_list
* args
= new Expression_list();
6285 args
->push_back(subkey
);
6286 args
->push_back(ref
);
6287 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
6288 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
6290 // Set retval to the result.
6291 Temporary_reference_expression
* tref
=
6292 Expression::make_temporary_reference(retval
, bloc
);
6293 tref
->set_is_lvalue();
6294 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
6295 gogo
->add_statement(s
);
6298 // Return retval to the caller of the hash function.
6299 Expression_list
* vals
= new Expression_list();
6300 ref
= Expression::make_temporary_reference(retval
, bloc
);
6301 vals
->push_back(ref
);
6302 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6303 gogo
->add_statement(s
);
6306 // Write the equality function for a struct which can not use the
6307 // identity function.
6310 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
6312 Location bloc
= Linemap::predeclared_location();
6314 // The pointers to the structs we are going to compare.
6315 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
6316 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
6317 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
6319 // Build temporaries with the right types.
6320 Type
* pt
= Type::make_pointer_type(name
!= NULL
6321 ? static_cast<Type
*>(name
)
6322 : static_cast<Type
*>(this));
6324 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
6325 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6326 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
6327 gogo
->add_statement(p1
);
6329 ref
= Expression::make_var_reference(key2_arg
, bloc
);
6330 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6331 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
6332 gogo
->add_statement(p2
);
6334 const Struct_field_list
* fields
= this->fields_
;
6335 unsigned int field_index
= 0;
6336 for (Struct_field_list::const_iterator pf
= fields
->begin();
6337 pf
!= fields
->end();
6338 ++pf
, ++field_index
)
6340 if (Gogo::is_sink_name(pf
->field_name()))
6343 // Compare one field in both P1 and P2.
6344 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
6345 f1
= Expression::make_unary(OPERATOR_MULT
, f1
, bloc
);
6346 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
6348 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
6349 f2
= Expression::make_unary(OPERATOR_MULT
, f2
, bloc
);
6350 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
6352 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
6354 // If the values are not equal, return false.
6355 gogo
->start_block(bloc
);
6356 Expression_list
* vals
= new Expression_list();
6357 vals
->push_back(Expression::make_boolean(false, bloc
));
6358 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6359 gogo
->add_statement(s
);
6360 Block
* then_block
= gogo
->finish_block(bloc
);
6362 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
6363 gogo
->add_statement(s
);
6366 // All the fields are equal, so return true.
6367 Expression_list
* vals
= new Expression_list();
6368 vals
->push_back(Expression::make_boolean(true, bloc
));
6369 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6370 gogo
->add_statement(s
);
6373 // Reflection string.
6376 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6378 ret
->append("struct {");
6380 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
6381 p
!= this->fields_
->end();
6384 if (p
!= this->fields_
->begin())
6385 ret
->push_back(';');
6386 ret
->push_back(' ');
6387 if (p
->is_anonymous())
6388 ret
->push_back('?');
6390 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
6391 ret
->push_back(' ');
6392 if (p
->is_anonymous()
6393 && p
->type()->named_type() != NULL
6394 && p
->type()->named_type()->is_alias())
6395 p
->type()->named_type()->append_reflection_type_name(gogo
, true, ret
);
6397 this->append_reflection(p
->type(), gogo
, ret
);
6401 const std::string
& tag(p
->tag());
6403 for (std::string::const_iterator p
= tag
.begin();
6408 ret
->append("\\x00");
6409 else if (*p
== '\n')
6411 else if (*p
== '\t')
6414 ret
->append("\\\"");
6415 else if (*p
== '\\')
6416 ret
->append("\\\\");
6420 ret
->push_back('"');
6424 if (!this->fields_
->empty())
6425 ret
->push_back(' ');
6427 ret
->push_back('}');
6430 // If the offset of field INDEX in the backend implementation can be
6431 // determined, set *POFFSET to the offset in bytes and return true.
6432 // Otherwise, return false.
6435 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
6438 if (!this->is_backend_type_size_known(gogo
))
6440 Btype
* bt
= this->get_backend_placeholder(gogo
);
6441 *poffset
= gogo
->backend()->type_field_offset(bt
, index
);
6448 Struct_type::do_export(Export
* exp
) const
6450 exp
->write_c_string("struct { ");
6451 const Struct_field_list
* fields
= this->fields_
;
6452 go_assert(fields
!= NULL
);
6453 for (Struct_field_list::const_iterator p
= fields
->begin();
6457 if (p
->is_anonymous())
6458 exp
->write_string("? ");
6461 exp
->write_string(p
->field_name());
6462 exp
->write_c_string(" ");
6464 exp
->write_type(p
->type());
6468 exp
->write_c_string(" ");
6470 Expression::make_string(p
->tag(), Linemap::predeclared_location());
6471 expr
->export_expression(exp
);
6475 exp
->write_c_string("; ");
6477 exp
->write_c_string("}");
6483 Struct_type::do_import(Import
* imp
)
6485 imp
->require_c_string("struct { ");
6486 Struct_field_list
* fields
= new Struct_field_list
;
6487 if (imp
->peek_char() != '}')
6492 if (imp
->match_c_string("? "))
6496 name
= imp
->read_identifier();
6497 imp
->require_c_string(" ");
6499 Type
* ftype
= imp
->read_type();
6501 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
6502 sf
.set_is_imported();
6504 if (imp
->peek_char() == ' ')
6507 Expression
* expr
= Expression::import_expression(imp
);
6508 String_expression
* sexpr
= expr
->string_expression();
6509 go_assert(sexpr
!= NULL
);
6510 sf
.set_tag(sexpr
->val());
6514 imp
->require_c_string("; ");
6515 fields
->push_back(sf
);
6516 if (imp
->peek_char() == '}')
6520 imp
->require_c_string("}");
6522 return Type::make_struct_type(fields
, imp
->location());
6525 // Whether we can write this struct type to a C header file.
6526 // We can't if any of the fields are structs defined in a different package.
6529 Struct_type::can_write_to_c_header(
6530 std::vector
<const Named_object
*>* requires
,
6531 std::vector
<const Named_object
*>* declare
) const
6533 const Struct_field_list
* fields
= this->fields_
;
6534 if (fields
== NULL
|| fields
->empty())
6537 for (Struct_field_list::const_iterator p
= fields
->begin();
6541 if (p
->is_anonymous())
6543 if (!this->can_write_type_to_c_header(p
->type(), requires
, declare
))
6545 if (Gogo::message_name(p
->field_name()) == "_")
6553 // Whether we can write the type T to a C header file.
6556 Struct_type::can_write_type_to_c_header(
6558 std::vector
<const Named_object
*>* requires
,
6559 std::vector
<const Named_object
*>* declare
) const
6562 switch (t
->classification())
6577 case TYPE_INTERFACE
:
6581 // Don't try to handle a pointer to an array.
6582 if (t
->points_to()->array_type() != NULL
6583 && !t
->points_to()->is_slice_type())
6586 if (t
->points_to()->named_type() != NULL
6587 && t
->points_to()->struct_type() != NULL
)
6588 declare
->push_back(t
->points_to()->named_type()->named_object());
6592 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6595 if (t
->is_slice_type())
6597 return this->can_write_type_to_c_header(t
->array_type()->element_type(),
6602 const Named_object
* no
= t
->named_type()->named_object();
6603 if (no
->package() != NULL
)
6605 if (t
->is_unsafe_pointer_type())
6609 if (t
->struct_type() != NULL
)
6611 requires
->push_back(no
);
6612 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6614 return this->can_write_type_to_c_header(t
->base(), requires
, declare
);
6617 case TYPE_CALL_MULTIPLE_RESULT
:
6625 // Write this struct to a C header file.
6628 Struct_type::write_to_c_header(std::ostream
& os
) const
6630 const Struct_field_list
* fields
= this->fields_
;
6631 for (Struct_field_list::const_iterator p
= fields
->begin();
6636 this->write_field_to_c_header(os
, p
->field_name(), p
->type());
6637 os
<< ';' << std::endl
;
6641 // Write the type of a struct field to a C header file.
6644 Struct_type::write_field_to_c_header(std::ostream
& os
, const std::string
& name
,
6645 const Type
*t
) const
6647 bool print_name
= true;
6649 switch (t
->classification())
6661 const Integer_type
* it
= t
->integer_type();
6662 if (it
->is_unsigned())
6664 os
<< "int" << it
->bits() << "_t";
6669 switch (t
->float_type()->bits())
6683 switch (t
->complex_type()->bits())
6686 os
<< "float _Complex";
6689 os
<< "double _Complex";
6706 std::vector
<const Named_object
*> requires
;
6707 std::vector
<const Named_object
*> declare
;
6708 if (!this->can_write_type_to_c_header(t
->points_to(), &requires
,
6713 this->write_field_to_c_header(os
, "", t
->points_to());
6727 case TYPE_INTERFACE
:
6728 if (t
->interface_type()->is_empty())
6735 os
<< "struct {" << std::endl
;
6736 t
->struct_type()->write_to_c_header(os
);
6741 if (t
->is_slice_type())
6745 const Type
*ele
= t
;
6746 std::vector
<const Type
*> array_types
;
6747 while (ele
->array_type() != NULL
&& !ele
->is_slice_type())
6749 array_types
.push_back(ele
);
6750 ele
= ele
->array_type()->element_type();
6752 this->write_field_to_c_header(os
, "", ele
);
6753 os
<< ' ' << Gogo::message_name(name
);
6755 while (!array_types
.empty())
6757 ele
= array_types
.back();
6758 array_types
.pop_back();
6760 Numeric_constant nc
;
6761 if (!ele
->array_type()->length()->numeric_constant_value(&nc
))
6764 if (!nc
.to_int(&val
))
6766 char* s
= mpz_get_str(NULL
, 10, val
);
6777 const Named_object
* no
= t
->named_type()->named_object();
6778 if (t
->struct_type() != NULL
)
6779 os
<< "struct " << no
->message_name();
6780 else if (t
->is_unsafe_pointer_type())
6782 else if (t
== Type::lookup_integer_type("uintptr"))
6786 this->write_field_to_c_header(os
, name
, t
->base());
6794 case TYPE_CALL_MULTIPLE_RESULT
:
6801 if (print_name
&& !name
.empty())
6802 os
<< ' ' << Gogo::message_name(name
);
6805 // Make a struct type.
6808 Type::make_struct_type(Struct_field_list
* fields
,
6811 return new Struct_type(fields
, location
);
6814 // Class Array_type.
6816 // Store the length of an array as an int64_t into *PLEN. Return
6817 // false if the length can not be determined. This will assert if
6818 // called for a slice.
6821 Array_type::int_length(int64_t* plen
)
6823 go_assert(this->length_
!= NULL
);
6824 Numeric_constant nc
;
6825 if (!this->length_
->numeric_constant_value(&nc
))
6827 return nc
.to_memory_size(plen
);
6830 // Whether two array types are identical.
6833 Array_type::is_identical(const Array_type
* t
, Cmp_tags cmp_tags
,
6834 bool errors_are_identical
) const
6836 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
6837 cmp_tags
, errors_are_identical
, NULL
))
6840 if (this->is_array_incomparable_
!= t
->is_array_incomparable_
)
6843 Expression
* l1
= this->length();
6844 Expression
* l2
= t
->length();
6846 // Slices of the same element type are identical.
6847 if (l1
== NULL
&& l2
== NULL
)
6850 // Arrays of the same element type are identical if they have the
6852 if (l1
!= NULL
&& l2
!= NULL
)
6857 // Try to determine the lengths. If we can't, assume the arrays
6858 // are not identical.
6860 Numeric_constant nc1
, nc2
;
6861 if (l1
->numeric_constant_value(&nc1
)
6862 && l2
->numeric_constant_value(&nc2
))
6865 if (nc1
.to_int(&v1
))
6868 if (nc2
.to_int(&v2
))
6870 ret
= mpz_cmp(v1
, v2
) == 0;
6879 // Otherwise the arrays are not identical.
6886 Array_type::do_traverse(Traverse
* traverse
)
6888 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
6889 return TRAVERSE_EXIT
;
6890 if (this->length_
!= NULL
6891 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
6892 return TRAVERSE_EXIT
;
6893 return TRAVERSE_CONTINUE
;
6896 // Check that the length is valid.
6899 Array_type::verify_length()
6901 if (this->length_
== NULL
)
6904 Type_context
context(Type::lookup_integer_type("int"), false);
6905 this->length_
->determine_type(&context
);
6907 if (!this->length_
->is_constant())
6909 go_error_at(this->length_
->location(), "array bound is not constant");
6913 Numeric_constant nc
;
6914 if (!this->length_
->numeric_constant_value(&nc
))
6916 if (this->length_
->type()->integer_type() != NULL
6917 || this->length_
->type()->float_type() != NULL
)
6918 go_error_at(this->length_
->location(), "array bound is not constant");
6920 go_error_at(this->length_
->location(), "array bound is not numeric");
6924 Type
* int_type
= Type::lookup_integer_type("int");
6925 unsigned int tbits
= int_type
->integer_type()->bits();
6927 switch (nc
.to_unsigned_long(&val
))
6929 case Numeric_constant::NC_UL_VALID
:
6930 if (sizeof(val
) >= tbits
/ 8 && val
>> (tbits
- 1) != 0)
6932 go_error_at(this->length_
->location(), "array bound overflows");
6936 case Numeric_constant::NC_UL_NOTINT
:
6937 go_error_at(this->length_
->location(), "array bound truncated to integer");
6939 case Numeric_constant::NC_UL_NEGATIVE
:
6940 go_error_at(this->length_
->location(), "negative array bound");
6942 case Numeric_constant::NC_UL_BIG
:
6945 if (!nc
.to_int(&val
))
6947 unsigned int bits
= mpz_sizeinbase(val
, 2);
6951 go_error_at(this->length_
->location(), "array bound overflows");
6966 Array_type::do_verify()
6968 if (this->element_type()->is_error_type())
6970 if (!this->verify_length())
6971 this->length_
= Expression::make_error(this->length_
->location());
6975 // Whether the type contains pointers. This is always true for a
6976 // slice. For an array it is true if the element type has pointers
6977 // and the length is greater than zero.
6980 Array_type::do_has_pointer() const
6982 if (this->length_
== NULL
)
6984 if (!this->element_type_
->has_pointer())
6987 Numeric_constant nc
;
6988 if (!this->length_
->numeric_constant_value(&nc
))
6990 // Error reported elsewhere.
6995 switch (nc
.to_unsigned_long(&val
))
6997 case Numeric_constant::NC_UL_VALID
:
6999 case Numeric_constant::NC_UL_BIG
:
7002 // Error reported elsewhere.
7007 // Whether we can use memcmp to compare this array.
7010 Array_type::do_compare_is_identity(Gogo
* gogo
)
7012 if (this->length_
== NULL
)
7015 // Check for [...], which indicates that this is not a real type.
7016 if (this->length_
->is_nil_expression())
7019 if (!this->element_type_
->compare_is_identity(gogo
))
7022 // If there is any padding, then we can't use memcmp.
7025 if (!this->element_type_
->backend_type_size(gogo
, &size
)
7026 || !this->element_type_
->backend_type_align(gogo
, &align
))
7028 if ((size
& (align
- 1)) != 0)
7034 // Array type hash code.
7037 Array_type::do_hash_for_method(Gogo
* gogo
) const
7041 // There is no very convenient way to get a hash code for the
7043 ret
= this->element_type_
->hash_for_method(gogo
) + 1;
7044 if (this->is_array_incomparable_
)
7049 // Write the hash function for an array which can not use the identify
7053 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
7054 Function_type
* hash_fntype
,
7055 Function_type
* equal_fntype
)
7057 Location bloc
= Linemap::predeclared_location();
7059 // The pointer to the array that we are going to hash. This is an
7060 // argument to the hash function we are implementing here.
7061 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
7062 go_assert(key_arg
!= NULL
);
7063 Type
* key_arg_type
= key_arg
->var_value()->type();
7065 // The seed argument to the hash function.
7066 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
7067 go_assert(seed_arg
!= NULL
);
7069 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7071 // Make a temporary to hold the return value, initialized to the seed.
7072 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
7073 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
7075 gogo
->add_statement(retval
);
7077 // Make a temporary to hold the key as a uintptr.
7078 ref
= Expression::make_var_reference(key_arg
, bloc
);
7079 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
7080 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
7082 gogo
->add_statement(key
);
7084 // Loop over the array elements.
7086 Type
* int_type
= Type::lookup_integer_type("int");
7087 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7088 gogo
->add_statement(index
);
7090 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7091 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
7092 Type
* pt
= Type::make_pointer_type(name
!= NULL
7093 ? static_cast<Type
*>(name
)
7094 : static_cast<Type
*>(this));
7095 aref
= Expression::make_cast(pt
, aref
, bloc
);
7096 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7101 gogo
->start_block(bloc
);
7103 // Get the hash function for the element type.
7104 Named_object
* hash_fn
;
7105 Named_object
* equal_fn
;
7106 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
7107 hash_fntype
, equal_fntype
, &hash_fn
,
7110 // Get a pointer to this element in the loop.
7111 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
7112 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
7114 // Get the size of each element.
7115 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
7116 Expression::TYPE_INFO_SIZE
);
7118 // Get the hash of this element, passing retval as the seed.
7119 ref
= Expression::make_temporary_reference(retval
, bloc
);
7120 Expression_list
* args
= new Expression_list();
7121 args
->push_back(subkey
);
7122 args
->push_back(ref
);
7123 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
7124 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
7126 // Set retval to the result.
7127 Temporary_reference_expression
* tref
=
7128 Expression::make_temporary_reference(retval
, bloc
);
7129 tref
->set_is_lvalue();
7130 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
7131 gogo
->add_statement(s
);
7133 // Increase the element pointer.
7134 tref
= Expression::make_temporary_reference(key
, bloc
);
7135 tref
->set_is_lvalue();
7136 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
7138 Block
* statements
= gogo
->finish_block(bloc
);
7140 for_range
->add_statements(statements
);
7141 gogo
->add_statement(for_range
);
7143 // Return retval to the caller of the hash function.
7144 Expression_list
* vals
= new Expression_list();
7145 ref
= Expression::make_temporary_reference(retval
, bloc
);
7146 vals
->push_back(ref
);
7147 s
= Statement::make_return_statement(vals
, bloc
);
7148 gogo
->add_statement(s
);
7151 // Write the equality function for an array which can not use the
7152 // identity function.
7155 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
7157 Location bloc
= Linemap::predeclared_location();
7159 // The pointers to the arrays we are going to compare.
7160 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
7161 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
7162 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
7164 // Build temporaries for the keys with the right types.
7165 Type
* pt
= Type::make_pointer_type(name
!= NULL
7166 ? static_cast<Type
*>(name
)
7167 : static_cast<Type
*>(this));
7169 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
7170 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7171 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
7172 gogo
->add_statement(p1
);
7174 ref
= Expression::make_var_reference(key2_arg
, bloc
);
7175 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7176 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
7177 gogo
->add_statement(p2
);
7179 // Loop over the array elements.
7181 Type
* int_type
= Type::lookup_integer_type("int");
7182 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7183 gogo
->add_statement(index
);
7185 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7186 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
7187 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7192 gogo
->start_block(bloc
);
7194 // Compare element in P1 and P2.
7195 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
7196 e1
= Expression::make_unary(OPERATOR_MULT
, e1
, bloc
);
7197 ref
= Expression::make_temporary_reference(index
, bloc
);
7198 e1
= Expression::make_array_index(e1
, ref
, NULL
, NULL
, bloc
);
7200 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
7201 e2
= Expression::make_unary(OPERATOR_MULT
, e2
, bloc
);
7202 ref
= Expression::make_temporary_reference(index
, bloc
);
7203 e2
= Expression::make_array_index(e2
, ref
, NULL
, NULL
, bloc
);
7205 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
7207 // If the elements are not equal, return false.
7208 gogo
->start_block(bloc
);
7209 Expression_list
* vals
= new Expression_list();
7210 vals
->push_back(Expression::make_boolean(false, bloc
));
7211 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
7212 gogo
->add_statement(s
);
7213 Block
* then_block
= gogo
->finish_block(bloc
);
7215 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
7216 gogo
->add_statement(s
);
7218 Block
* statements
= gogo
->finish_block(bloc
);
7220 for_range
->add_statements(statements
);
7221 gogo
->add_statement(for_range
);
7223 // All the elements are equal, so return true.
7224 vals
= new Expression_list();
7225 vals
->push_back(Expression::make_boolean(true, bloc
));
7226 s
= Statement::make_return_statement(vals
, bloc
);
7227 gogo
->add_statement(s
);
7230 // Get the backend representation of the fields of a slice. This is
7231 // not declared in types.h so that types.h doesn't have to #include
7234 // We use int for the count and capacity fields. This matches 6g.
7235 // The language more or less assumes that we can't allocate space of a
7236 // size which does not fit in int.
7239 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
7240 std::vector
<Backend::Btyped_identifier
>* bfields
)
7244 Type
* pet
= Type::make_pointer_type(type
->element_type());
7245 Btype
* pbet
= (use_placeholder
7246 ? pet
->get_backend_placeholder(gogo
)
7247 : pet
->get_backend(gogo
));
7248 Location ploc
= Linemap::predeclared_location();
7250 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
7251 p
->name
= "__values";
7255 Type
* int_type
= Type::lookup_integer_type("int");
7258 p
->name
= "__count";
7259 p
->btype
= int_type
->get_backend(gogo
);
7263 p
->name
= "__capacity";
7264 p
->btype
= int_type
->get_backend(gogo
);
7268 // Get the backend representation for the type of this array. A fixed array is
7269 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7270 // just like an array in C. An open array is a struct with three
7271 // fields: a data pointer, the length, and the capacity.
7274 Array_type::do_get_backend(Gogo
* gogo
)
7276 if (this->length_
== NULL
)
7278 std::vector
<Backend::Btyped_identifier
> bfields
;
7279 get_backend_slice_fields(gogo
, this, false, &bfields
);
7280 return gogo
->backend()->struct_type(bfields
);
7284 Btype
* element
= this->get_backend_element(gogo
, false);
7285 Bexpression
* len
= this->get_backend_length(gogo
);
7286 return gogo
->backend()->array_type(element
, len
);
7290 // Return the backend representation of the element type.
7293 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
7295 if (use_placeholder
)
7296 return this->element_type_
->get_backend_placeholder(gogo
);
7298 return this->element_type_
->get_backend(gogo
);
7301 // Return the backend representation of the length. The length may be
7302 // computed using a function call, so we must only evaluate it once.
7305 Array_type::get_backend_length(Gogo
* gogo
)
7307 go_assert(this->length_
!= NULL
);
7308 if (this->blength_
== NULL
)
7310 if (this->length_
->is_error_expression())
7312 this->blength_
= gogo
->backend()->error_expression();
7313 return this->blength_
;
7315 Numeric_constant nc
;
7317 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
7319 if (mpz_sgn(val
) < 0)
7321 this->blength_
= gogo
->backend()->error_expression();
7322 return this->blength_
;
7324 Type
* t
= nc
.type();
7326 t
= Type::lookup_integer_type("int");
7327 else if (t
->is_abstract())
7328 t
= t
->make_non_abstract_type();
7329 Btype
* btype
= t
->get_backend(gogo
);
7331 gogo
->backend()->integer_constant_expression(btype
, val
);
7336 // Make up a translation context for the array length
7337 // expression. FIXME: This won't work in general.
7338 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7339 this->blength_
= this->length_
->get_backend(&context
);
7341 Btype
* ibtype
= Type::lookup_integer_type("int")->get_backend(gogo
);
7343 gogo
->backend()->convert_expression(ibtype
, this->blength_
,
7344 this->length_
->location());
7347 return this->blength_
;
7350 // Finish backend representation of the array.
7353 Array_type::finish_backend_element(Gogo
* gogo
)
7355 Type
* et
= this->array_type()->element_type();
7356 et
->get_backend(gogo
);
7357 if (this->is_slice_type())
7359 // This relies on the fact that we always use the same
7360 // structure for a pointer to any given type.
7361 Type
* pet
= Type::make_pointer_type(et
);
7362 pet
->get_backend(gogo
);
7366 // Return an expression for a pointer to the values in ARRAY.
7369 Array_type::get_value_pointer(Gogo
*, Expression
* array
, bool is_lvalue
) const
7371 if (this->length() != NULL
)
7374 go_assert(array
->type()->array_type() != NULL
);
7375 Type
* etype
= array
->type()->array_type()->element_type();
7376 array
= Expression::make_unary(OPERATOR_AND
, array
, array
->location());
7377 return Expression::make_cast(Type::make_pointer_type(etype
), array
,
7385 Temporary_reference_expression
* tref
=
7386 array
->temporary_reference_expression();
7387 Var_expression
* ve
= array
->var_expression();
7390 tref
= tref
->copy()->temporary_reference_expression();
7391 tref
->set_is_lvalue();
7394 else if (ve
!= NULL
)
7396 ve
= new Var_expression(ve
->named_object(), ve
->location());
7401 return Expression::make_slice_info(array
,
7402 Expression::SLICE_INFO_VALUE_POINTER
,
7406 // Return an expression for the length of the array ARRAY which has this
7410 Array_type::get_length(Gogo
*, Expression
* array
) const
7412 if (this->length_
!= NULL
)
7413 return this->length_
;
7415 // This is a slice. We need to read the length field.
7416 return Expression::make_slice_info(array
, Expression::SLICE_INFO_LENGTH
,
7420 // Return an expression for the capacity of the array ARRAY which has this
7424 Array_type::get_capacity(Gogo
*, Expression
* array
) const
7426 if (this->length_
!= NULL
)
7427 return this->length_
;
7429 // This is a slice. We need to read the capacity field.
7430 return Expression::make_slice_info(array
, Expression::SLICE_INFO_CAPACITY
,
7437 Array_type::do_export(Export
* exp
) const
7439 exp
->write_c_string("[");
7440 if (this->length_
!= NULL
)
7441 this->length_
->export_expression(exp
);
7442 exp
->write_c_string("] ");
7443 exp
->write_type(this->element_type_
);
7449 Array_type::do_import(Import
* imp
)
7451 imp
->require_c_string("[");
7453 if (imp
->peek_char() == ']')
7456 length
= Expression::import_expression(imp
);
7457 imp
->require_c_string("] ");
7458 Type
* element_type
= imp
->read_type();
7459 return Type::make_array_type(element_type
, length
);
7462 // The type of an array type descriptor.
7465 Array_type::make_array_type_descriptor_type()
7470 Type
* tdt
= Type::make_type_descriptor_type();
7471 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7473 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7476 Type::make_builtin_struct_type(4,
7480 "len", uintptr_type
);
7482 ret
= Type::make_builtin_named_type("ArrayType", sf
);
7488 // The type of an slice type descriptor.
7491 Array_type::make_slice_type_descriptor_type()
7496 Type
* tdt
= Type::make_type_descriptor_type();
7497 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7500 Type::make_builtin_struct_type(2,
7504 ret
= Type::make_builtin_named_type("SliceType", sf
);
7510 // Build a type descriptor for an array/slice type.
7513 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7515 if (this->length_
!= NULL
)
7516 return this->array_type_descriptor(gogo
, name
);
7518 return this->slice_type_descriptor(gogo
, name
);
7521 // Build a type descriptor for an array type.
7524 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7526 Location bloc
= Linemap::predeclared_location();
7528 Type
* atdt
= Array_type::make_array_type_descriptor_type();
7530 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
7532 Expression_list
* vals
= new Expression_list();
7535 Struct_field_list::const_iterator p
= fields
->begin();
7536 go_assert(p
->is_field_name("_type"));
7537 vals
->push_back(this->type_descriptor_constructor(gogo
,
7538 RUNTIME_TYPE_KIND_ARRAY
,
7542 go_assert(p
->is_field_name("elem"));
7543 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7546 go_assert(p
->is_field_name("slice"));
7547 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
7548 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
7551 go_assert(p
->is_field_name("len"));
7552 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
7555 go_assert(p
== fields
->end());
7557 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
7560 // Build a type descriptor for a slice type.
7563 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7565 Location bloc
= Linemap::predeclared_location();
7567 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
7569 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
7571 Expression_list
* vals
= new Expression_list();
7574 Struct_field_list::const_iterator p
= fields
->begin();
7575 go_assert(p
->is_field_name("_type"));
7576 vals
->push_back(this->type_descriptor_constructor(gogo
,
7577 RUNTIME_TYPE_KIND_SLICE
,
7581 go_assert(p
->is_field_name("elem"));
7582 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7585 go_assert(p
== fields
->end());
7587 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
7590 // Reflection string.
7593 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7595 ret
->push_back('[');
7596 if (this->length_
!= NULL
)
7598 Numeric_constant nc
;
7599 if (!this->length_
->numeric_constant_value(&nc
))
7601 go_assert(saw_errors());
7605 if (!nc
.to_int(&val
))
7607 go_assert(saw_errors());
7610 char* s
= mpz_get_str(NULL
, 10, val
);
7615 ret
->push_back(']');
7617 this->append_reflection(this->element_type_
, gogo
, ret
);
7620 // Make an array type.
7623 Type::make_array_type(Type
* element_type
, Expression
* length
)
7625 return new Array_type(element_type
, length
);
7630 Named_object
* Map_type::zero_value
;
7631 int64_t Map_type::zero_value_size
;
7632 int64_t Map_type::zero_value_align
;
7634 // If this map requires the "fat" functions, return the pointer to
7635 // pass as the zero value to those functions. Otherwise, in the
7636 // normal case, return NULL. The map requires the "fat" functions if
7637 // the value size is larger than max_zero_size bytes. max_zero_size
7638 // must match maxZero in libgo/go/runtime/hashmap.go.
7641 Map_type::fat_zero_value(Gogo
* gogo
)
7644 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
7646 go_assert(saw_errors());
7649 if (valsize
<= Map_type::max_zero_size
)
7652 if (Map_type::zero_value_size
< valsize
)
7653 Map_type::zero_value_size
= valsize
;
7656 if (!this->val_type_
->backend_type_align(gogo
, &valalign
))
7658 go_assert(saw_errors());
7662 if (Map_type::zero_value_align
< valalign
)
7663 Map_type::zero_value_align
= valalign
;
7665 Location bloc
= Linemap::predeclared_location();
7667 if (Map_type::zero_value
== NULL
)
7669 // The final type will be set in backend_zero_value.
7670 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7671 Expression
* size
= Expression::make_integer_ul(0, NULL
, bloc
);
7672 Array_type
* array_type
= Type::make_array_type(uint8_type
, size
);
7673 array_type
->set_is_array_incomparable();
7674 Variable
* var
= new Variable(array_type
, NULL
, true, false, false, bloc
);
7675 std::string name
= gogo
->map_zero_value_name();
7676 Map_type::zero_value
= Named_object::make_variable(name
, NULL
, var
);
7679 Expression
* z
= Expression::make_var_reference(Map_type::zero_value
, bloc
);
7680 z
= Expression::make_unary(OPERATOR_AND
, z
, bloc
);
7681 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
7682 z
= Expression::make_cast(unsafe_ptr_type
, z
, bloc
);
7686 // Return whether VAR is the map zero value.
7689 Map_type::is_zero_value(Variable
* var
)
7691 return (Map_type::zero_value
!= NULL
7692 && Map_type::zero_value
->var_value() == var
);
7695 // Return the backend representation for the zero value.
7698 Map_type::backend_zero_value(Gogo
* gogo
)
7700 Location bloc
= Linemap::predeclared_location();
7702 go_assert(Map_type::zero_value
!= NULL
);
7704 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7705 Btype
* buint8_type
= uint8_type
->get_backend(gogo
);
7707 Type
* int_type
= Type::lookup_integer_type("int");
7709 Expression
* e
= Expression::make_integer_int64(Map_type::zero_value_size
,
7711 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7712 Bexpression
* blength
= e
->get_backend(&context
);
7714 Btype
* barray_type
= gogo
->backend()->array_type(buint8_type
, blength
);
7716 std::string zname
= Map_type::zero_value
->name();
7717 std::string
asm_name(go_selectively_encode_id(zname
));
7719 gogo
->backend()->implicit_variable(zname
, asm_name
,
7720 barray_type
, false, true, true,
7721 Map_type::zero_value_align
);
7722 gogo
->backend()->implicit_variable_set_init(zvar
, zname
, barray_type
,
7723 false, true, true, NULL
);
7730 Map_type::do_traverse(Traverse
* traverse
)
7732 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
7733 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
7734 return TRAVERSE_EXIT
;
7735 return TRAVERSE_CONTINUE
;
7738 // Check that the map type is OK.
7741 Map_type::do_verify()
7743 // The runtime support uses "map[void]void".
7744 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
7745 go_error_at(this->location_
, "invalid map key type");
7746 if (!this->key_type_
->in_heap())
7747 go_error_at(this->location_
, "go:notinheap map key not allowed");
7748 if (!this->val_type_
->in_heap())
7749 go_error_at(this->location_
, "go:notinheap map value not allowed");
7753 // Whether two map types are identical.
7756 Map_type::is_identical(const Map_type
* t
, Cmp_tags cmp_tags
,
7757 bool errors_are_identical
) const
7759 return (Type::are_identical_cmp_tags(this->key_type(), t
->key_type(),
7760 cmp_tags
, errors_are_identical
, NULL
)
7761 && Type::are_identical_cmp_tags(this->val_type(), t
->val_type(),
7762 cmp_tags
, errors_are_identical
,
7769 Map_type::do_hash_for_method(Gogo
* gogo
) const
7771 return (this->key_type_
->hash_for_method(gogo
)
7772 + this->val_type_
->hash_for_method(gogo
)
7776 // Get the backend representation for a map type. A map type is
7777 // represented as a pointer to a struct. The struct is hmap in
7778 // runtime/hashmap.go.
7781 Map_type::do_get_backend(Gogo
* gogo
)
7783 static Btype
* backend_map_type
;
7784 if (backend_map_type
== NULL
)
7786 std::vector
<Backend::Btyped_identifier
> bfields(9);
7788 Location bloc
= Linemap::predeclared_location();
7790 Type
* int_type
= Type::lookup_integer_type("int");
7791 bfields
[0].name
= "count";
7792 bfields
[0].btype
= int_type
->get_backend(gogo
);
7793 bfields
[0].location
= bloc
;
7795 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7796 bfields
[1].name
= "flags";
7797 bfields
[1].btype
= uint8_type
->get_backend(gogo
);
7798 bfields
[1].location
= bloc
;
7800 bfields
[2].name
= "B";
7801 bfields
[2].btype
= bfields
[1].btype
;
7802 bfields
[2].location
= bloc
;
7804 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7805 bfields
[3].name
= "noverflow";
7806 bfields
[3].btype
= uint16_type
->get_backend(gogo
);
7807 bfields
[3].location
= bloc
;
7809 Type
* uint32_type
= Type::lookup_integer_type("uint32");
7810 bfields
[4].name
= "hash0";
7811 bfields
[4].btype
= uint32_type
->get_backend(gogo
);
7812 bfields
[4].location
= bloc
;
7814 Btype
* bvt
= gogo
->backend()->void_type();
7815 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
7816 bfields
[5].name
= "buckets";
7817 bfields
[5].btype
= bpvt
;
7818 bfields
[5].location
= bloc
;
7820 bfields
[6].name
= "oldbuckets";
7821 bfields
[6].btype
= bpvt
;
7822 bfields
[6].location
= bloc
;
7824 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7825 bfields
[7].name
= "nevacuate";
7826 bfields
[7].btype
= uintptr_type
->get_backend(gogo
);
7827 bfields
[7].location
= bloc
;
7829 bfields
[8].name
= "overflow";
7830 bfields
[8].btype
= bpvt
;
7831 bfields
[8].location
= bloc
;
7833 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
7834 bt
= gogo
->backend()->named_type("runtime.hmap", bt
, bloc
);
7835 backend_map_type
= gogo
->backend()->pointer_type(bt
);
7837 return backend_map_type
;
7840 // The type of a map type descriptor.
7843 Map_type::make_map_type_descriptor_type()
7848 Type
* tdt
= Type::make_type_descriptor_type();
7849 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7850 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7851 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7852 Type
* bool_type
= Type::lookup_bool_type();
7855 Type::make_builtin_struct_type(12,
7861 "keysize", uint8_type
,
7862 "indirectkey", bool_type
,
7863 "valuesize", uint8_type
,
7864 "indirectvalue", bool_type
,
7865 "bucketsize", uint16_type
,
7866 "reflexivekey", bool_type
,
7867 "needkeyupdate", bool_type
);
7869 ret
= Type::make_builtin_named_type("MapType", sf
);
7875 // Build a type descriptor for a map type.
7878 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7880 Location bloc
= Linemap::predeclared_location();
7882 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
7883 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7884 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7887 if (!this->key_type_
->backend_type_size(gogo
, &keysize
))
7889 go_error_at(this->location_
, "error determining map key type size");
7890 return Expression::make_error(this->location_
);
7894 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
7896 go_error_at(this->location_
, "error determining map value type size");
7897 return Expression::make_error(this->location_
);
7901 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptrsize
))
7903 go_assert(saw_errors());
7904 return Expression::make_error(this->location_
);
7907 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
7908 if (bucket_type
== NULL
)
7910 go_assert(saw_errors());
7911 return Expression::make_error(this->location_
);
7915 if (!bucket_type
->backend_type_size(gogo
, &bucketsize
))
7917 go_assert(saw_errors());
7918 return Expression::make_error(this->location_
);
7921 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
7923 Expression_list
* vals
= new Expression_list();
7926 Struct_field_list::const_iterator p
= fields
->begin();
7927 go_assert(p
->is_field_name("_type"));
7928 vals
->push_back(this->type_descriptor_constructor(gogo
,
7929 RUNTIME_TYPE_KIND_MAP
,
7933 go_assert(p
->is_field_name("key"));
7934 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
7937 go_assert(p
->is_field_name("elem"));
7938 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
7941 go_assert(p
->is_field_name("bucket"));
7942 vals
->push_back(Expression::make_type_descriptor(bucket_type
, bloc
));
7945 go_assert(p
->is_field_name("hmap"));
7946 Type
* hmap_type
= this->hmap_type(bucket_type
);
7947 vals
->push_back(Expression::make_type_descriptor(hmap_type
, bloc
));
7950 go_assert(p
->is_field_name("keysize"));
7951 if (keysize
> Map_type::max_key_size
)
7952 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
7954 vals
->push_back(Expression::make_integer_int64(keysize
, uint8_type
, bloc
));
7957 go_assert(p
->is_field_name("indirectkey"));
7958 vals
->push_back(Expression::make_boolean(keysize
> Map_type::max_key_size
,
7962 go_assert(p
->is_field_name("valuesize"));
7963 if (valsize
> Map_type::max_val_size
)
7964 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
7966 vals
->push_back(Expression::make_integer_int64(valsize
, uint8_type
, bloc
));
7969 go_assert(p
->is_field_name("indirectvalue"));
7970 vals
->push_back(Expression::make_boolean(valsize
> Map_type::max_val_size
,
7974 go_assert(p
->is_field_name("bucketsize"));
7975 vals
->push_back(Expression::make_integer_int64(bucketsize
, uint16_type
,
7979 go_assert(p
->is_field_name("reflexivekey"));
7980 vals
->push_back(Expression::make_boolean(this->key_type_
->is_reflexive(),
7984 go_assert(p
->is_field_name("needkeyupdate"));
7985 vals
->push_back(Expression::make_boolean(this->key_type_
->needs_key_update(),
7989 go_assert(p
== fields
->end());
7991 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
7994 // Return the bucket type to use for a map type. This must correspond
7995 // to libgo/go/runtime/hashmap.go.
7998 Map_type::bucket_type(Gogo
* gogo
, int64_t keysize
, int64_t valsize
)
8000 if (this->bucket_type_
!= NULL
)
8001 return this->bucket_type_
;
8003 Type
* key_type
= this->key_type_
;
8004 if (keysize
> Map_type::max_key_size
)
8005 key_type
= Type::make_pointer_type(key_type
);
8007 Type
* val_type
= this->val_type_
;
8008 if (valsize
> Map_type::max_val_size
)
8009 val_type
= Type::make_pointer_type(val_type
);
8011 Expression
* bucket_size
= Expression::make_integer_ul(Map_type::bucket_size
,
8012 NULL
, this->location_
);
8014 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8015 Array_type
* topbits_type
= Type::make_array_type(uint8_type
, bucket_size
);
8016 topbits_type
->set_is_array_incomparable();
8017 Array_type
* keys_type
= Type::make_array_type(key_type
, bucket_size
);
8018 keys_type
->set_is_array_incomparable();
8019 Array_type
* values_type
= Type::make_array_type(val_type
, bucket_size
);
8020 values_type
->set_is_array_incomparable();
8022 // If keys and values have no pointers, the map implementation can
8023 // keep a list of overflow pointers on the side so that buckets can
8024 // be marked as having no pointers. Arrange for the bucket to have
8025 // no pointers by changing the type of the overflow field to uintptr
8026 // in this case. See comment on the hmap.overflow field in
8027 // libgo/go/runtime/hashmap.go.
8028 Type
* overflow_type
;
8029 if (!key_type
->has_pointer() && !val_type
->has_pointer())
8030 overflow_type
= Type::lookup_integer_type("uintptr");
8033 // This should really be a pointer to the bucket type itself,
8034 // but that would require us to construct a Named_type for it to
8035 // give it a way to refer to itself. Since nothing really cares
8036 // (except perhaps for someone using a debugger) just use an
8038 overflow_type
= Type::make_pointer_type(Type::make_void_type());
8041 // Make sure the overflow pointer is the last memory in the struct,
8042 // because the runtime assumes it can use size-ptrSize as the offset
8043 // of the overflow pointer. We double-check that property below
8044 // once the offsets and size are computed.
8046 int64_t topbits_field_size
, topbits_field_align
;
8047 int64_t keys_field_size
, keys_field_align
;
8048 int64_t values_field_size
, values_field_align
;
8049 int64_t overflow_field_size
, overflow_field_align
;
8050 if (!topbits_type
->backend_type_size(gogo
, &topbits_field_size
)
8051 || !topbits_type
->backend_type_field_align(gogo
, &topbits_field_align
)
8052 || !keys_type
->backend_type_size(gogo
, &keys_field_size
)
8053 || !keys_type
->backend_type_field_align(gogo
, &keys_field_align
)
8054 || !values_type
->backend_type_size(gogo
, &values_field_size
)
8055 || !values_type
->backend_type_field_align(gogo
, &values_field_align
)
8056 || !overflow_type
->backend_type_size(gogo
, &overflow_field_size
)
8057 || !overflow_type
->backend_type_field_align(gogo
, &overflow_field_align
))
8059 go_assert(saw_errors());
8064 int64_t max_align
= std::max(std::max(topbits_field_align
, keys_field_align
),
8065 values_field_align
);
8066 if (max_align
<= overflow_field_align
)
8067 ret
= make_builtin_struct_type(4,
8068 "topbits", topbits_type
,
8070 "values", values_type
,
8071 "overflow", overflow_type
);
8074 size_t off
= topbits_field_size
;
8075 off
= ((off
+ keys_field_align
- 1)
8076 &~ static_cast<size_t>(keys_field_align
- 1));
8077 off
+= keys_field_size
;
8078 off
= ((off
+ values_field_align
- 1)
8079 &~ static_cast<size_t>(values_field_align
- 1));
8080 off
+= values_field_size
;
8082 int64_t padded_overflow_field_size
=
8083 ((overflow_field_size
+ max_align
- 1)
8084 &~ static_cast<size_t>(max_align
- 1));
8087 ovoff
= ((ovoff
+ max_align
- 1)
8088 &~ static_cast<size_t>(max_align
- 1));
8089 size_t pad
= (ovoff
- off
8090 + padded_overflow_field_size
- overflow_field_size
);
8092 Expression
* pad_expr
= Expression::make_integer_ul(pad
, NULL
,
8094 Array_type
* pad_type
= Type::make_array_type(uint8_type
, pad_expr
);
8095 pad_type
->set_is_array_incomparable();
8097 ret
= make_builtin_struct_type(5,
8098 "topbits", topbits_type
,
8100 "values", values_type
,
8102 "overflow", overflow_type
);
8105 // Verify that the overflow field is just before the end of the
8108 Btype
* btype
= ret
->get_backend(gogo
);
8109 int64_t offset
= gogo
->backend()->type_field_offset(btype
,
8110 ret
->field_count() - 1);
8112 if (!ret
->backend_type_size(gogo
, &size
))
8114 go_assert(saw_errors());
8119 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptr_size
))
8121 go_assert(saw_errors());
8125 go_assert(offset
+ ptr_size
== size
);
8127 ret
->set_is_struct_incomparable();
8129 this->bucket_type_
= ret
;
8133 // Return the hashmap type for a map type.
8136 Map_type::hmap_type(Type
* bucket_type
)
8138 if (this->hmap_type_
!= NULL
)
8139 return this->hmap_type_
;
8141 Type
* int_type
= Type::lookup_integer_type("int");
8142 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8143 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8144 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8145 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8147 Type
* ptr_bucket_type
= Type::make_pointer_type(bucket_type
);
8149 Struct_type
* ret
= make_builtin_struct_type(8,
8151 "flags", uint8_type
,
8153 "hash0", uint32_type
,
8154 "buckets", ptr_bucket_type
,
8155 "oldbuckets", ptr_bucket_type
,
8156 "nevacuate", uintptr_type
,
8157 "overflow", void_ptr_type
);
8158 ret
->set_is_struct_incomparable();
8159 this->hmap_type_
= ret
;
8163 // Return the iterator type for a map type. This is the type of the
8164 // value used when doing a range over a map.
8167 Map_type::hiter_type(Gogo
* gogo
)
8169 if (this->hiter_type_
!= NULL
)
8170 return this->hiter_type_
;
8172 int64_t keysize
, valsize
;
8173 if (!this->key_type_
->backend_type_size(gogo
, &keysize
)
8174 || !this->val_type_
->backend_type_size(gogo
, &valsize
))
8176 go_assert(saw_errors());
8180 Type
* key_ptr_type
= Type::make_pointer_type(this->key_type_
);
8181 Type
* val_ptr_type
= Type::make_pointer_type(this->val_type_
);
8182 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8183 Type
* uint8_ptr_type
= Type::make_pointer_type(uint8_type
);
8184 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8185 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
8186 Type
* bucket_ptr_type
= Type::make_pointer_type(bucket_type
);
8187 Type
* hmap_type
= this->hmap_type(bucket_type
);
8188 Type
* hmap_ptr_type
= Type::make_pointer_type(hmap_type
);
8189 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8191 Struct_type
* ret
= make_builtin_struct_type(12,
8192 "key", key_ptr_type
,
8193 "val", val_ptr_type
,
8194 "t", uint8_ptr_type
,
8196 "buckets", bucket_ptr_type
,
8197 "bptr", bucket_ptr_type
,
8198 "overflow0", void_ptr_type
,
8199 "overflow1", void_ptr_type
,
8200 "startBucket", uintptr_type
,
8201 "stuff", uintptr_type
,
8202 "bucket", uintptr_type
,
8203 "checkBucket", uintptr_type
);
8204 ret
->set_is_struct_incomparable();
8205 this->hiter_type_
= ret
;
8209 // Reflection string for a map.
8212 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8214 ret
->append("map[");
8215 this->append_reflection(this->key_type_
, gogo
, ret
);
8217 this->append_reflection(this->val_type_
, gogo
, ret
);
8220 // Export a map type.
8223 Map_type::do_export(Export
* exp
) const
8225 exp
->write_c_string("map [");
8226 exp
->write_type(this->key_type_
);
8227 exp
->write_c_string("] ");
8228 exp
->write_type(this->val_type_
);
8231 // Import a map type.
8234 Map_type::do_import(Import
* imp
)
8236 imp
->require_c_string("map [");
8237 Type
* key_type
= imp
->read_type();
8238 imp
->require_c_string("] ");
8239 Type
* val_type
= imp
->read_type();
8240 return Type::make_map_type(key_type
, val_type
, imp
->location());
8246 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
8248 return new Map_type(key_type
, val_type
, location
);
8251 // Class Channel_type.
8256 Channel_type::do_verify()
8258 // We have no location for this error, but this is not something the
8259 // ordinary user will see.
8260 if (!this->element_type_
->in_heap())
8261 go_error_at(Linemap::unknown_location(),
8262 "chan of go:notinheap type not allowed");
8269 Channel_type::do_hash_for_method(Gogo
* gogo
) const
8271 unsigned int ret
= 0;
8272 if (this->may_send_
)
8274 if (this->may_receive_
)
8276 if (this->element_type_
!= NULL
)
8277 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
8281 // Whether this type is the same as T.
8284 Channel_type::is_identical(const Channel_type
* t
, Cmp_tags cmp_tags
,
8285 bool errors_are_identical
) const
8287 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
8288 cmp_tags
, errors_are_identical
, NULL
))
8290 return (this->may_send_
== t
->may_send_
8291 && this->may_receive_
== t
->may_receive_
);
8294 // Return the backend representation for a channel type. A channel is a pointer
8295 // to a __go_channel struct. The __go_channel struct is defined in
8296 // libgo/runtime/channel.h.
8299 Channel_type::do_get_backend(Gogo
* gogo
)
8301 static Btype
* backend_channel_type
;
8302 if (backend_channel_type
== NULL
)
8304 std::vector
<Backend::Btyped_identifier
> bfields
;
8305 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
8306 bt
= gogo
->backend()->named_type("__go_channel", bt
,
8307 Linemap::predeclared_location());
8308 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
8310 return backend_channel_type
;
8313 // Build a type descriptor for a channel type.
8316 Channel_type::make_chan_type_descriptor_type()
8321 Type
* tdt
= Type::make_type_descriptor_type();
8322 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
8324 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8327 Type::make_builtin_struct_type(3,
8330 "dir", uintptr_type
);
8332 ret
= Type::make_builtin_named_type("ChanType", sf
);
8338 // Build a type descriptor for a map type.
8341 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8343 Location bloc
= Linemap::predeclared_location();
8345 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
8347 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
8349 Expression_list
* vals
= new Expression_list();
8352 Struct_field_list::const_iterator p
= fields
->begin();
8353 go_assert(p
->is_field_name("_type"));
8354 vals
->push_back(this->type_descriptor_constructor(gogo
,
8355 RUNTIME_TYPE_KIND_CHAN
,
8359 go_assert(p
->is_field_name("elem"));
8360 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
8363 go_assert(p
->is_field_name("dir"));
8364 // These bits must match the ones in libgo/runtime/go-type.h.
8366 if (this->may_receive_
)
8368 if (this->may_send_
)
8370 vals
->push_back(Expression::make_integer_ul(val
, p
->type(), bloc
));
8373 go_assert(p
== fields
->end());
8375 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
8378 // Reflection string.
8381 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8383 if (!this->may_send_
)
8385 ret
->append("chan");
8386 if (!this->may_receive_
)
8388 ret
->push_back(' ');
8389 this->append_reflection(this->element_type_
, gogo
, ret
);
8395 Channel_type::do_export(Export
* exp
) const
8397 exp
->write_c_string("chan ");
8398 if (this->may_send_
&& !this->may_receive_
)
8399 exp
->write_c_string("-< ");
8400 else if (this->may_receive_
&& !this->may_send_
)
8401 exp
->write_c_string("<- ");
8402 exp
->write_type(this->element_type_
);
8408 Channel_type::do_import(Import
* imp
)
8410 imp
->require_c_string("chan ");
8414 if (imp
->match_c_string("-< "))
8418 may_receive
= false;
8420 else if (imp
->match_c_string("<- "))
8432 Type
* element_type
= imp
->read_type();
8434 return Type::make_channel_type(may_send
, may_receive
, element_type
);
8437 // Return the type to manage a select statement with ncases case
8438 // statements. A value of this type is allocated on the stack. This
8439 // must match the type hselect in libgo/go/runtime/select.go.
8442 Channel_type::select_type(int ncases
)
8444 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
8445 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8447 static Struct_type
* scase_type
;
8448 if (scase_type
== NULL
)
8450 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8451 Type
* uint64_type
= Type::lookup_integer_type("uint64");
8453 Type::make_builtin_struct_type(7,
8454 "elem", unsafe_pointer_type
,
8455 "chan", unsafe_pointer_type
,
8457 "kind", uint16_type
,
8458 "index", uint16_type
,
8459 "receivedp", unsafe_pointer_type
,
8460 "releasetime", uint64_type
);
8461 scase_type
->set_is_struct_incomparable();
8464 Expression
* ncases_expr
=
8465 Expression::make_integer_ul(ncases
, NULL
, Linemap::predeclared_location());
8466 Array_type
* scases
= Type::make_array_type(scase_type
, ncases_expr
);
8467 scases
->set_is_array_incomparable();
8468 Array_type
* order
= Type::make_array_type(uint16_type
, ncases_expr
);
8469 order
->set_is_array_incomparable();
8472 Type::make_builtin_struct_type(7,
8473 "tcase", uint16_type
,
8474 "ncase", uint16_type
,
8475 "pollorder", unsafe_pointer_type
,
8476 "lockorder", unsafe_pointer_type
,
8478 "lockorderarr", order
,
8479 "pollorderarr", order
);
8480 ret
->set_is_struct_incomparable();
8484 // Make a new channel type.
8487 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
8489 return new Channel_type(send
, receive
, element_type
);
8492 // Class Interface_type.
8494 // Return the list of methods.
8496 const Typed_identifier_list
*
8497 Interface_type::methods() const
8499 go_assert(this->methods_are_finalized_
|| saw_errors());
8500 return this->all_methods_
;
8503 // Return the number of methods.
8506 Interface_type::method_count() const
8508 go_assert(this->methods_are_finalized_
|| saw_errors());
8509 return this->all_methods_
== NULL
? 0 : this->all_methods_
->size();
8515 Interface_type::do_traverse(Traverse
* traverse
)
8517 Typed_identifier_list
* methods
= (this->methods_are_finalized_
8518 ? this->all_methods_
8519 : this->parse_methods_
);
8520 if (methods
== NULL
)
8521 return TRAVERSE_CONTINUE
;
8522 return methods
->traverse(traverse
);
8525 // Finalize the methods. This handles interface inheritance.
8528 Interface_type::finalize_methods()
8530 if (this->methods_are_finalized_
)
8532 this->methods_are_finalized_
= true;
8533 if (this->parse_methods_
== NULL
)
8536 this->all_methods_
= new Typed_identifier_list();
8537 this->all_methods_
->reserve(this->parse_methods_
->size());
8538 Typed_identifier_list inherit
;
8539 for (Typed_identifier_list::const_iterator pm
=
8540 this->parse_methods_
->begin();
8541 pm
!= this->parse_methods_
->end();
8544 const Typed_identifier
* p
= &*pm
;
8545 if (p
->name().empty())
8546 inherit
.push_back(*p
);
8547 else if (this->find_method(p
->name()) == NULL
)
8548 this->all_methods_
->push_back(*p
);
8550 go_error_at(p
->location(), "duplicate method %qs",
8551 Gogo::message_name(p
->name()).c_str());
8554 std::vector
<Named_type
*> seen
;
8555 seen
.reserve(inherit
.size());
8556 bool issued_recursive_error
= false;
8557 while (!inherit
.empty())
8559 Type
* t
= inherit
.back().type();
8560 Location tl
= inherit
.back().location();
8563 Interface_type
* it
= t
->interface_type();
8567 go_error_at(tl
, "interface contains embedded non-interface");
8572 if (!issued_recursive_error
)
8574 go_error_at(tl
, "invalid recursive interface");
8575 issued_recursive_error
= true;
8580 Named_type
* nt
= t
->named_type();
8581 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
8583 std::vector
<Named_type
*>::const_iterator q
;
8584 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
8588 go_error_at(tl
, "inherited interface loop");
8592 if (q
!= seen
.end())
8597 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
8598 if (imethods
== NULL
)
8600 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
8601 q
!= imethods
->end();
8604 if (q
->name().empty())
8605 inherit
.push_back(*q
);
8606 else if (this->find_method(q
->name()) == NULL
)
8607 this->all_methods_
->push_back(Typed_identifier(q
->name(),
8610 go_error_at(tl
, "inherited method %qs is ambiguous",
8611 Gogo::message_name(q
->name()).c_str());
8615 if (!this->all_methods_
->empty())
8616 this->all_methods_
->sort_by_name();
8619 delete this->all_methods_
;
8620 this->all_methods_
= NULL
;
8624 // Return the method NAME, or NULL.
8626 const Typed_identifier
*
8627 Interface_type::find_method(const std::string
& name
) const
8629 go_assert(this->methods_are_finalized_
);
8630 if (this->all_methods_
== NULL
)
8632 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8633 p
!= this->all_methods_
->end();
8635 if (p
->name() == name
)
8640 // Return the method index.
8643 Interface_type::method_index(const std::string
& name
) const
8645 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
8647 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8648 p
!= this->all_methods_
->end();
8650 if (p
->name() == name
)
8655 // Return whether NAME is an unexported method, for better error
8659 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
8661 go_assert(this->methods_are_finalized_
);
8662 if (this->all_methods_
== NULL
)
8664 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8665 p
!= this->all_methods_
->end();
8668 const std::string
& method_name(p
->name());
8669 if (Gogo::is_hidden_name(method_name
)
8670 && name
== Gogo::unpack_hidden_name(method_name
)
8671 && gogo
->pack_hidden_name(name
, false) != method_name
)
8677 // Whether this type is identical with T.
8680 Interface_type::is_identical(const Interface_type
* t
, Cmp_tags cmp_tags
,
8681 bool errors_are_identical
) const
8683 // If methods have not been finalized, then we are asking whether
8684 // func redeclarations are the same. This is an error, so for
8685 // simplicity we say they are never the same.
8686 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
8689 // We require the same methods with the same types. The methods
8690 // have already been sorted.
8691 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
8692 return this->all_methods_
== t
->all_methods_
;
8694 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
8697 Assume_identical
* hold_ai
= this->assume_identical_
;
8698 Assume_identical ai
;
8702 this->assume_identical_
= &ai
;
8704 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
8705 Typed_identifier_list::const_iterator p2
;
8706 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
8708 if (p1
== this->all_methods_
->end())
8710 if (p1
->name() != p2
->name()
8711 || !Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
8712 errors_are_identical
, NULL
))
8716 this->assume_identical_
= hold_ai
;
8718 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
8721 // Return true if T1 and T2 are assumed to be identical during a type
8725 Interface_type::assume_identical(const Interface_type
* t1
,
8726 const Interface_type
* t2
) const
8728 for (Assume_identical
* p
= this->assume_identical_
;
8731 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
8736 // Whether we can assign the interface type T to this type. The types
8737 // are known to not be identical. An interface assignment is only
8738 // permitted if T is known to implement all methods in THIS.
8739 // Otherwise a type guard is required.
8742 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
8743 std::string
* reason
) const
8745 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
8746 if (this->all_methods_
== NULL
)
8748 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8749 p
!= this->all_methods_
->end();
8752 const Typed_identifier
* m
= t
->find_method(p
->name());
8758 snprintf(buf
, sizeof buf
,
8759 _("need explicit conversion; missing method %s%s%s"),
8760 go_open_quote(), Gogo::message_name(p
->name()).c_str(),
8762 reason
->assign(buf
);
8767 std::string subreason
;
8768 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
8772 std::string n
= Gogo::message_name(p
->name());
8773 size_t len
= 100 + n
.length() + subreason
.length();
8774 char* buf
= new char[len
];
8775 if (subreason
.empty())
8776 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
8777 go_open_quote(), n
.c_str(), go_close_quote());
8780 _("incompatible type for method %s%s%s (%s)"),
8781 go_open_quote(), n
.c_str(), go_close_quote(),
8783 reason
->assign(buf
);
8796 Interface_type::do_hash_for_method(Gogo
*) const
8798 go_assert(this->methods_are_finalized_
);
8799 unsigned int ret
= 0;
8800 if (this->all_methods_
!= NULL
)
8802 for (Typed_identifier_list::const_iterator p
=
8803 this->all_methods_
->begin();
8804 p
!= this->all_methods_
->end();
8807 ret
= Type::hash_string(p
->name(), ret
);
8808 // We don't use the method type in the hash, to avoid
8809 // infinite recursion if an interface method uses a type
8810 // which is an interface which inherits from the interface
8812 // type T interface { F() interface {T}}
8819 // Return true if T implements the interface. If it does not, and
8820 // REASON is not NULL, set *REASON to a useful error message.
8823 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
8825 go_assert(this->methods_are_finalized_
);
8826 if (this->all_methods_
== NULL
)
8829 bool is_pointer
= false;
8830 const Named_type
* nt
= t
->named_type();
8831 const Struct_type
* st
= t
->struct_type();
8832 // If we start with a named type, we don't dereference it to find
8836 const Type
* pt
= t
->points_to();
8839 // If T is a pointer to a named type, then we need to look at
8840 // the type to which it points.
8842 nt
= pt
->named_type();
8843 st
= pt
->struct_type();
8847 // If we have a named type, get the methods from it rather than from
8852 // Only named and struct types have methods.
8853 if (nt
== NULL
&& st
== NULL
)
8857 if (t
->points_to() != NULL
8858 && t
->points_to()->interface_type() != NULL
)
8859 reason
->assign(_("pointer to interface type has no methods"));
8861 reason
->assign(_("type has no methods"));
8866 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
8870 if (t
->points_to() != NULL
8871 && t
->points_to()->interface_type() != NULL
)
8872 reason
->assign(_("pointer to interface type has no methods"));
8874 reason
->assign(_("type has no methods"));
8879 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8880 p
!= this->all_methods_
->end();
8883 bool is_ambiguous
= false;
8884 Method
* m
= (nt
!= NULL
8885 ? nt
->method_function(p
->name(), &is_ambiguous
)
8886 : st
->method_function(p
->name(), &is_ambiguous
));
8891 std::string n
= Gogo::message_name(p
->name());
8892 size_t len
= n
.length() + 100;
8893 char* buf
= new char[len
];
8895 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
8896 go_open_quote(), n
.c_str(), go_close_quote());
8898 snprintf(buf
, len
, _("missing method %s%s%s"),
8899 go_open_quote(), n
.c_str(), go_close_quote());
8900 reason
->assign(buf
);
8906 Function_type
*p_fn_type
= p
->type()->function_type();
8907 Function_type
* m_fn_type
= m
->type()->function_type();
8908 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
8909 std::string subreason
;
8910 if (!p_fn_type
->is_identical(m_fn_type
, true, COMPARE_TAGS
, true,
8915 std::string n
= Gogo::message_name(p
->name());
8916 size_t len
= 100 + n
.length() + subreason
.length();
8917 char* buf
= new char[len
];
8918 if (subreason
.empty())
8919 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
8920 go_open_quote(), n
.c_str(), go_close_quote());
8923 _("incompatible type for method %s%s%s (%s)"),
8924 go_open_quote(), n
.c_str(), go_close_quote(),
8926 reason
->assign(buf
);
8932 if (!is_pointer
&& !m
->is_value_method())
8936 std::string n
= Gogo::message_name(p
->name());
8937 size_t len
= 100 + n
.length();
8938 char* buf
= new char[len
];
8940 _("method %s%s%s requires a pointer receiver"),
8941 go_open_quote(), n
.c_str(), go_close_quote());
8942 reason
->assign(buf
);
8948 // If the magic //go:nointerface comment was used, the method
8949 // may not be used to implement interfaces.
8950 if (m
->nointerface())
8954 std::string n
= Gogo::message_name(p
->name());
8955 size_t len
= 100 + n
.length();
8956 char* buf
= new char[len
];
8958 _("method %s%s%s is marked go:nointerface"),
8959 go_open_quote(), n
.c_str(), go_close_quote());
8960 reason
->assign(buf
);
8970 // Return the backend representation of the empty interface type. We
8971 // use the same struct for all empty interfaces.
8974 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
8976 static Btype
* empty_interface_type
;
8977 if (empty_interface_type
== NULL
)
8979 std::vector
<Backend::Btyped_identifier
> bfields(2);
8981 Location bloc
= Linemap::predeclared_location();
8983 Type
* pdt
= Type::make_type_descriptor_ptr_type();
8984 bfields
[0].name
= "__type_descriptor";
8985 bfields
[0].btype
= pdt
->get_backend(gogo
);
8986 bfields
[0].location
= bloc
;
8988 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
8989 bfields
[1].name
= "__object";
8990 bfields
[1].btype
= vt
->get_backend(gogo
);
8991 bfields
[1].location
= bloc
;
8993 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
8995 return empty_interface_type
;
8998 // Return a pointer to the backend representation of the method table.
9001 Interface_type::get_backend_methods(Gogo
* gogo
)
9003 if (this->bmethods_
!= NULL
&& !this->bmethods_is_placeholder_
)
9004 return this->bmethods_
;
9006 Location loc
= this->location();
9008 std::vector
<Backend::Btyped_identifier
>
9009 mfields(this->all_methods_
->size() + 1);
9011 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9012 mfields
[0].name
= "__type_descriptor";
9013 mfields
[0].btype
= pdt
->get_backend(gogo
);
9014 mfields
[0].location
= loc
;
9016 std::string last_name
= "";
9018 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9019 p
!= this->all_methods_
->end();
9022 // The type of the method in Go only includes the parameters.
9023 // The actual method also has a receiver, which is always a
9024 // pointer. We need to add that pointer type here in order to
9025 // generate the correct type for the backend.
9026 Function_type
* ft
= p
->type()->function_type();
9027 go_assert(ft
->receiver() == NULL
);
9029 const Typed_identifier_list
* params
= ft
->parameters();
9030 Typed_identifier_list
* mparams
= new Typed_identifier_list();
9032 mparams
->reserve(params
->size() + 1);
9033 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9034 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
9037 for (Typed_identifier_list::const_iterator pp
= params
->begin();
9038 pp
!= params
->end();
9040 mparams
->push_back(*pp
);
9043 Typed_identifier_list
* mresults
= (ft
->results() == NULL
9045 : ft
->results()->copy());
9046 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
9049 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
9050 mfields
[i
].btype
= mft
->get_backend_fntype(gogo
);
9051 mfields
[i
].location
= loc
;
9053 // Sanity check: the names should be sorted.
9054 go_assert(Gogo::unpack_hidden_name(p
->name())
9055 > Gogo::unpack_hidden_name(last_name
));
9056 last_name
= p
->name();
9059 Btype
* st
= gogo
->backend()->struct_type(mfields
);
9060 Btype
* ret
= gogo
->backend()->pointer_type(st
);
9062 if (this->bmethods_
!= NULL
&& this->bmethods_is_placeholder_
)
9063 gogo
->backend()->set_placeholder_pointer_type(this->bmethods_
, ret
);
9064 this->bmethods_
= ret
;
9065 this->bmethods_is_placeholder_
= false;
9069 // Return a placeholder for the pointer to the backend methods table.
9072 Interface_type::get_backend_methods_placeholder(Gogo
* gogo
)
9074 if (this->bmethods_
== NULL
)
9076 Location loc
= this->location();
9077 this->bmethods_
= gogo
->backend()->placeholder_pointer_type("", loc
,
9079 this->bmethods_is_placeholder_
= true;
9081 return this->bmethods_
;
9084 // Return the fields of a non-empty interface type. This is not
9085 // declared in types.h so that types.h doesn't have to #include
9089 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
9090 bool use_placeholder
,
9091 std::vector
<Backend::Btyped_identifier
>* bfields
)
9093 Location loc
= type
->location();
9097 (*bfields
)[0].name
= "__methods";
9098 (*bfields
)[0].btype
= (use_placeholder
9099 ? type
->get_backend_methods_placeholder(gogo
)
9100 : type
->get_backend_methods(gogo
));
9101 (*bfields
)[0].location
= loc
;
9103 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9104 (*bfields
)[1].name
= "__object";
9105 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
9106 (*bfields
)[1].location
= Linemap::predeclared_location();
9109 // Return the backend representation for an interface type. An interface is a
9110 // pointer to a struct. The struct has three fields. The first field is a
9111 // pointer to the type descriptor for the dynamic type of the object.
9112 // The second field is a pointer to a table of methods for the
9113 // interface to be used with the object. The third field is the value
9114 // of the object itself.
9117 Interface_type::do_get_backend(Gogo
* gogo
)
9119 if (this->is_empty())
9120 return Interface_type::get_backend_empty_interface_type(gogo
);
9123 if (this->interface_btype_
!= NULL
)
9124 return this->interface_btype_
;
9125 this->interface_btype_
=
9126 gogo
->backend()->placeholder_struct_type("", this->location_
);
9127 std::vector
<Backend::Btyped_identifier
> bfields
;
9128 get_backend_interface_fields(gogo
, this, false, &bfields
);
9129 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
9131 this->interface_btype_
= gogo
->backend()->error_type();
9132 return this->interface_btype_
;
9136 // Finish the backend representation of the methods.
9139 Interface_type::finish_backend_methods(Gogo
* gogo
)
9141 if (!this->is_empty())
9143 const Typed_identifier_list
* methods
= this->methods();
9144 if (methods
!= NULL
)
9146 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9147 p
!= methods
->end();
9149 p
->type()->get_backend(gogo
);
9152 // Getting the backend methods now will set the placeholder
9154 this->get_backend_methods(gogo
);
9158 // The type of an interface type descriptor.
9161 Interface_type::make_interface_type_descriptor_type()
9166 Type
* tdt
= Type::make_type_descriptor_type();
9167 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
9169 Type
* string_type
= Type::lookup_string_type();
9170 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
9173 Type::make_builtin_struct_type(3,
9174 "name", pointer_string_type
,
9175 "pkgPath", pointer_string_type
,
9178 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
9180 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
9182 Struct_type
* s
= Type::make_builtin_struct_type(2,
9184 "methods", slice_nsm
);
9186 ret
= Type::make_builtin_named_type("InterfaceType", s
);
9192 // Build a type descriptor for an interface type.
9195 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9197 Location bloc
= Linemap::predeclared_location();
9199 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
9201 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
9203 Expression_list
* ivals
= new Expression_list();
9206 Struct_field_list::const_iterator pif
= ifields
->begin();
9207 go_assert(pif
->is_field_name("_type"));
9208 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
9209 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
9213 go_assert(pif
->is_field_name("methods"));
9215 Expression_list
* methods
= new Expression_list();
9216 if (this->all_methods_
!= NULL
)
9218 Type
* elemtype
= pif
->type()->array_type()->element_type();
9220 methods
->reserve(this->all_methods_
->size());
9221 for (Typed_identifier_list::const_iterator pm
=
9222 this->all_methods_
->begin();
9223 pm
!= this->all_methods_
->end();
9226 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
9228 Expression_list
* mvals
= new Expression_list();
9231 Struct_field_list::const_iterator pmf
= mfields
->begin();
9232 go_assert(pmf
->is_field_name("name"));
9233 std::string s
= Gogo::unpack_hidden_name(pm
->name());
9234 Expression
* e
= Expression::make_string(s
, bloc
);
9235 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9238 go_assert(pmf
->is_field_name("pkgPath"));
9239 if (!Gogo::is_hidden_name(pm
->name()))
9240 mvals
->push_back(Expression::make_nil(bloc
));
9243 s
= Gogo::hidden_name_pkgpath(pm
->name());
9244 e
= Expression::make_string(s
, bloc
);
9245 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9249 go_assert(pmf
->is_field_name("typ"));
9250 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
9253 go_assert(pmf
== mfields
->end());
9255 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
9257 methods
->push_back(e
);
9261 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
9265 go_assert(pif
== ifields
->end());
9267 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
9270 // Reflection string.
9273 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9275 ret
->append("interface {");
9276 const Typed_identifier_list
* methods
= this->parse_methods_
;
9277 if (methods
!= NULL
)
9279 ret
->push_back(' ');
9280 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9281 p
!= methods
->end();
9284 if (p
!= methods
->begin())
9286 if (p
->name().empty())
9287 this->append_reflection(p
->type(), gogo
, ret
);
9290 if (!Gogo::is_hidden_name(p
->name()))
9291 ret
->append(p
->name());
9292 else if (gogo
->pkgpath_from_option())
9293 ret
->append(p
->name().substr(1));
9296 // If no -fgo-pkgpath option, backward compatibility
9297 // for how this used to work before -fgo-pkgpath was
9299 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
9300 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
9301 ret
->push_back('.');
9302 ret
->append(Gogo::unpack_hidden_name(p
->name()));
9304 std::string sub
= p
->type()->reflection(gogo
);
9305 go_assert(sub
.compare(0, 4, "func") == 0);
9306 sub
= sub
.substr(4);
9310 ret
->push_back(' ');
9318 Interface_type::do_export(Export
* exp
) const
9320 exp
->write_c_string("interface { ");
9322 const Typed_identifier_list
* methods
= this->parse_methods_
;
9323 if (methods
!= NULL
)
9325 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
9326 pm
!= methods
->end();
9329 if (pm
->name().empty())
9331 exp
->write_c_string("? ");
9332 exp
->write_type(pm
->type());
9336 exp
->write_string(pm
->name());
9337 exp
->write_c_string(" (");
9339 const Function_type
* fntype
= pm
->type()->function_type();
9342 const Typed_identifier_list
* parameters
= fntype
->parameters();
9343 if (parameters
!= NULL
)
9345 bool is_varargs
= fntype
->is_varargs();
9346 for (Typed_identifier_list::const_iterator pp
=
9347 parameters
->begin();
9348 pp
!= parameters
->end();
9354 exp
->write_c_string(", ");
9355 exp
->write_name(pp
->name());
9356 exp
->write_c_string(" ");
9357 if (!is_varargs
|| pp
+ 1 != parameters
->end())
9358 exp
->write_type(pp
->type());
9361 exp
->write_c_string("...");
9362 Type
*pptype
= pp
->type();
9363 exp
->write_type(pptype
->array_type()->element_type());
9368 exp
->write_c_string(")");
9370 const Typed_identifier_list
* results
= fntype
->results();
9371 if (results
!= NULL
)
9373 exp
->write_c_string(" ");
9374 if (results
->size() == 1 && results
->begin()->name().empty())
9375 exp
->write_type(results
->begin()->type());
9379 exp
->write_c_string("(");
9380 for (Typed_identifier_list::const_iterator p
=
9382 p
!= results
->end();
9388 exp
->write_c_string(", ");
9389 exp
->write_name(p
->name());
9390 exp
->write_c_string(" ");
9391 exp
->write_type(p
->type());
9393 exp
->write_c_string(")");
9398 exp
->write_c_string("; ");
9402 exp
->write_c_string("}");
9405 // Import an interface type.
9408 Interface_type::do_import(Import
* imp
)
9410 imp
->require_c_string("interface { ");
9412 Typed_identifier_list
* methods
= new Typed_identifier_list
;
9413 while (imp
->peek_char() != '}')
9415 std::string name
= imp
->read_identifier();
9419 imp
->require_c_string(" ");
9420 Type
* t
= imp
->read_type();
9421 methods
->push_back(Typed_identifier("", t
, imp
->location()));
9422 imp
->require_c_string("; ");
9426 imp
->require_c_string(" (");
9428 Typed_identifier_list
* parameters
;
9429 bool is_varargs
= false;
9430 if (imp
->peek_char() == ')')
9434 parameters
= new Typed_identifier_list
;
9437 std::string name
= imp
->read_name();
9438 imp
->require_c_string(" ");
9440 if (imp
->match_c_string("..."))
9446 Type
* ptype
= imp
->read_type();
9448 ptype
= Type::make_array_type(ptype
, NULL
);
9449 parameters
->push_back(Typed_identifier(name
, ptype
,
9451 if (imp
->peek_char() != ',')
9453 go_assert(!is_varargs
);
9454 imp
->require_c_string(", ");
9457 imp
->require_c_string(")");
9459 Typed_identifier_list
* results
;
9460 if (imp
->peek_char() != ' ')
9464 results
= new Typed_identifier_list
;
9466 if (imp
->peek_char() != '(')
9468 Type
* rtype
= imp
->read_type();
9469 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
9476 std::string name
= imp
->read_name();
9477 imp
->require_c_string(" ");
9478 Type
* rtype
= imp
->read_type();
9479 results
->push_back(Typed_identifier(name
, rtype
,
9481 if (imp
->peek_char() != ',')
9483 imp
->require_c_string(", ");
9485 imp
->require_c_string(")");
9489 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
9493 fntype
->set_is_varargs();
9494 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
9496 imp
->require_c_string("; ");
9499 imp
->require_c_string("}");
9501 if (methods
->empty())
9507 Interface_type
* ret
= Type::make_interface_type(methods
, imp
->location());
9508 ret
->package_
= imp
->package();
9512 // Make an interface type.
9515 Type::make_interface_type(Typed_identifier_list
* methods
,
9518 return new Interface_type(methods
, location
);
9521 // Make an empty interface type.
9524 Type::make_empty_interface_type(Location location
)
9526 Interface_type
* ret
= new Interface_type(NULL
, location
);
9527 ret
->finalize_methods();
9533 // Bind a method to an object.
9536 Method::bind_method(Expression
* expr
, Location location
) const
9538 if (this->stub_
== NULL
)
9540 // When there is no stub object, the binding is determined by
9542 return this->do_bind_method(expr
, location
);
9544 return Expression::make_bound_method(expr
, this, this->stub_
, location
);
9547 // Return the named object associated with a method. This may only be
9548 // called after methods are finalized.
9551 Method::named_object() const
9553 if (this->stub_
!= NULL
)
9555 return this->do_named_object();
9558 // Class Named_method.
9560 // The type of the method.
9563 Named_method::do_type() const
9565 if (this->named_object_
->is_function())
9566 return this->named_object_
->func_value()->type();
9567 else if (this->named_object_
->is_function_declaration())
9568 return this->named_object_
->func_declaration_value()->type();
9573 // Return the location of the method receiver.
9576 Named_method::do_receiver_location() const
9578 return this->do_type()->receiver()->location();
9581 // Bind a method to an object.
9584 Named_method::do_bind_method(Expression
* expr
, Location location
) const
9586 Named_object
* no
= this->named_object_
;
9587 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, this,
9589 // If this is not a local method, and it does not use a stub, then
9590 // the real method expects a different type. We need to cast the
9592 if (this->depth() > 0 && !this->needs_stub_method())
9594 Function_type
* ftype
= this->do_type();
9595 go_assert(ftype
->is_method());
9596 Type
* frtype
= ftype
->receiver()->type();
9597 bme
->set_first_argument_type(frtype
);
9602 // Return whether this method should not participate in interfaces.
9605 Named_method::do_nointerface() const
9607 Named_object
* no
= this->named_object_
;
9608 return no
->is_function() && no
->func_value()->nointerface();
9611 // Class Interface_method.
9613 // Bind a method to an object.
9616 Interface_method::do_bind_method(Expression
* expr
,
9617 Location location
) const
9619 return Expression::make_interface_field_reference(expr
, this->name_
,
9625 // Insert a new method. Return true if it was inserted, false
9629 Methods::insert(const std::string
& name
, Method
* m
)
9631 std::pair
<Method_map::iterator
, bool> ins
=
9632 this->methods_
.insert(std::make_pair(name
, m
));
9637 Method
* old_method
= ins
.first
->second
;
9638 if (m
->depth() < old_method
->depth())
9641 ins
.first
->second
= m
;
9646 if (m
->depth() == old_method
->depth())
9647 old_method
->set_is_ambiguous();
9653 // Return the number of unambiguous methods.
9656 Methods::count() const
9659 for (Method_map::const_iterator p
= this->methods_
.begin();
9660 p
!= this->methods_
.end();
9662 if (!p
->second
->is_ambiguous())
9667 // Class Named_type.
9669 // Return the name of the type.
9672 Named_type::name() const
9674 return this->named_object_
->name();
9677 // Return the name of the type to use in an error message.
9680 Named_type::message_name() const
9682 return this->named_object_
->message_name();
9685 // Return the base type for this type. We have to be careful about
9686 // circular type definitions, which are invalid but may be seen here.
9689 Named_type::named_base()
9694 Type
* ret
= this->type_
->base();
9695 this->seen_
= false;
9700 Named_type::named_base() const
9705 const Type
* ret
= this->type_
->base();
9706 this->seen_
= false;
9710 // Return whether this is an error type. We have to be careful about
9711 // circular type definitions, which are invalid but may be seen here.
9714 Named_type::is_named_error_type() const
9719 bool ret
= this->type_
->is_error_type();
9720 this->seen_
= false;
9724 // Whether this type is comparable. We have to be careful about
9725 // circular type definitions.
9728 Named_type::named_type_is_comparable(std::string
* reason
) const
9733 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
9734 this->type_
, reason
);
9735 this->seen_
= false;
9739 // Add a method to this type.
9742 Named_type::add_method(const std::string
& name
, Function
* function
)
9744 go_assert(!this->is_alias_
);
9745 if (this->local_methods_
== NULL
)
9746 this->local_methods_
= new Bindings(NULL
);
9747 return this->local_methods_
->add_function(name
, NULL
, function
);
9750 // Add a method declaration to this type.
9753 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
9754 Function_type
* type
,
9757 go_assert(!this->is_alias_
);
9758 if (this->local_methods_
== NULL
)
9759 this->local_methods_
= new Bindings(NULL
);
9760 return this->local_methods_
->add_function_declaration(name
, package
, type
,
9764 // Add an existing method to this type.
9767 Named_type::add_existing_method(Named_object
* no
)
9769 go_assert(!this->is_alias_
);
9770 if (this->local_methods_
== NULL
)
9771 this->local_methods_
= new Bindings(NULL
);
9772 this->local_methods_
->add_named_object(no
);
9775 // Look for a local method NAME, and returns its named object, or NULL
9779 Named_type::find_local_method(const std::string
& name
) const
9781 if (this->is_error_
)
9783 if (this->is_alias_
)
9785 Named_type
* nt
= this->type_
->named_type();
9788 if (this->seen_alias_
)
9790 this->seen_alias_
= true;
9791 Named_object
* ret
= nt
->find_local_method(name
);
9792 this->seen_alias_
= false;
9797 if (this->local_methods_
== NULL
)
9799 return this->local_methods_
->lookup(name
);
9802 // Return the list of local methods.
9805 Named_type::local_methods() const
9807 if (this->is_error_
)
9809 if (this->is_alias_
)
9811 Named_type
* nt
= this->type_
->named_type();
9814 if (this->seen_alias_
)
9816 this->seen_alias_
= true;
9817 const Bindings
* ret
= nt
->local_methods();
9818 this->seen_alias_
= false;
9823 return this->local_methods_
;
9826 // Return whether NAME is an unexported field or method, for better
9830 Named_type::is_unexported_local_method(Gogo
* gogo
,
9831 const std::string
& name
) const
9833 if (this->is_error_
)
9835 if (this->is_alias_
)
9837 Named_type
* nt
= this->type_
->named_type();
9840 if (this->seen_alias_
)
9842 this->seen_alias_
= true;
9843 bool ret
= nt
->is_unexported_local_method(gogo
, name
);
9844 this->seen_alias_
= false;
9849 Bindings
* methods
= this->local_methods_
;
9850 if (methods
!= NULL
)
9852 for (Bindings::const_declarations_iterator p
=
9853 methods
->begin_declarations();
9854 p
!= methods
->end_declarations();
9857 if (Gogo::is_hidden_name(p
->first
)
9858 && name
== Gogo::unpack_hidden_name(p
->first
)
9859 && gogo
->pack_hidden_name(name
, false) != p
->first
)
9866 // Build the complete list of methods for this type, which means
9867 // recursively including all methods for anonymous fields. Create all
9871 Named_type::finalize_methods(Gogo
* gogo
)
9873 if (this->is_alias_
)
9875 if (this->all_methods_
!= NULL
)
9878 if (this->local_methods_
!= NULL
9879 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
9881 const Bindings
* lm
= this->local_methods_
;
9882 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
9883 p
!= lm
->end_declarations();
9885 go_error_at(p
->second
->location(),
9886 "invalid pointer or interface receiver type");
9887 delete this->local_methods_
;
9888 this->local_methods_
= NULL
;
9892 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
9895 // Return whether this type has any methods.
9898 Named_type::has_any_methods() const
9900 if (this->is_error_
)
9902 if (this->is_alias_
)
9904 if (this->type_
->named_type() != NULL
)
9906 if (this->seen_alias_
)
9908 this->seen_alias_
= true;
9909 bool ret
= this->type_
->named_type()->has_any_methods();
9910 this->seen_alias_
= false;
9913 if (this->type_
->struct_type() != NULL
)
9914 return this->type_
->struct_type()->has_any_methods();
9917 return this->all_methods_
!= NULL
;
9920 // Return the methods for this type.
9923 Named_type::methods() const
9925 if (this->is_error_
)
9927 if (this->is_alias_
)
9929 if (this->type_
->named_type() != NULL
)
9931 if (this->seen_alias_
)
9933 this->seen_alias_
= true;
9934 const Methods
* ret
= this->type_
->named_type()->methods();
9935 this->seen_alias_
= false;
9938 if (this->type_
->struct_type() != NULL
)
9939 return this->type_
->struct_type()->methods();
9942 return this->all_methods_
;
9945 // Return the method NAME, or NULL if there isn't one or if it is
9946 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
9950 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
9952 if (this->is_error_
)
9954 if (this->is_alias_
)
9956 if (is_ambiguous
!= NULL
)
9957 *is_ambiguous
= false;
9958 if (this->type_
->named_type() != NULL
)
9960 if (this->seen_alias_
)
9962 this->seen_alias_
= true;
9963 Named_type
* nt
= this->type_
->named_type();
9964 Method
* ret
= nt
->method_function(name
, is_ambiguous
);
9965 this->seen_alias_
= false;
9968 if (this->type_
->struct_type() != NULL
)
9969 return this->type_
->struct_type()->method_function(name
, is_ambiguous
);
9972 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
9975 // Return a pointer to the interface method table for this type for
9976 // the interface INTERFACE. IS_POINTER is true if this is for a
9980 Named_type::interface_method_table(Interface_type
* interface
, bool is_pointer
)
9982 if (this->is_error_
)
9983 return Expression::make_error(this->location_
);
9984 if (this->is_alias_
)
9986 if (this->type_
->named_type() != NULL
)
9988 if (this->seen_alias_
)
9989 return Expression::make_error(this->location_
);
9990 this->seen_alias_
= true;
9991 Named_type
* nt
= this->type_
->named_type();
9992 Expression
* ret
= nt
->interface_method_table(interface
, is_pointer
);
9993 this->seen_alias_
= false;
9996 if (this->type_
->struct_type() != NULL
)
9997 return this->type_
->struct_type()->interface_method_table(interface
,
10001 return Type::interface_method_table(this, interface
, is_pointer
,
10002 &this->interface_method_tables_
,
10003 &this->pointer_interface_method_tables_
);
10006 // Look for a use of a complete type within another type. This is
10007 // used to check that we don't try to use a type within itself.
10009 class Find_type_use
: public Traverse
10012 Find_type_use(Named_type
* find_type
)
10013 : Traverse(traverse_types
),
10014 find_type_(find_type
), found_(false)
10017 // Whether we found the type.
10020 { return this->found_
; }
10027 // The type we are looking for.
10028 Named_type
* find_type_
;
10029 // Whether we found the type.
10033 // Check for FIND_TYPE in TYPE.
10036 Find_type_use::type(Type
* type
)
10038 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
10040 this->found_
= true;
10041 return TRAVERSE_EXIT
;
10044 // It's OK if we see a reference to the type in any type which is
10045 // essentially a pointer: a pointer, a slice, a function, a map, or
10047 if (type
->points_to() != NULL
10048 || type
->is_slice_type()
10049 || type
->function_type() != NULL
10050 || type
->map_type() != NULL
10051 || type
->channel_type() != NULL
)
10052 return TRAVERSE_SKIP_COMPONENTS
;
10054 // For an interface, a reference to the type in a method type should
10055 // be ignored, but we have to consider direct inheritance. When
10056 // this is called, there may be cases of direct inheritance
10057 // represented as a method with no name.
10058 if (type
->interface_type() != NULL
)
10060 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
10061 if (methods
!= NULL
)
10063 for (Typed_identifier_list::const_iterator p
= methods
->begin();
10064 p
!= methods
->end();
10067 if (p
->name().empty())
10069 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
10070 return TRAVERSE_EXIT
;
10074 return TRAVERSE_SKIP_COMPONENTS
;
10077 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
10078 // to convert TYPE to the backend representation before we convert
10080 if (type
->named_type() != NULL
)
10082 switch (type
->base()->classification())
10084 case Type::TYPE_ERROR
:
10085 case Type::TYPE_BOOLEAN
:
10086 case Type::TYPE_INTEGER
:
10087 case Type::TYPE_FLOAT
:
10088 case Type::TYPE_COMPLEX
:
10089 case Type::TYPE_STRING
:
10090 case Type::TYPE_NIL
:
10093 case Type::TYPE_ARRAY
:
10094 case Type::TYPE_STRUCT
:
10095 this->find_type_
->add_dependency(type
->named_type());
10098 case Type::TYPE_NAMED
:
10099 case Type::TYPE_FORWARD
:
10100 go_assert(saw_errors());
10103 case Type::TYPE_VOID
:
10104 case Type::TYPE_SINK
:
10105 case Type::TYPE_FUNCTION
:
10106 case Type::TYPE_POINTER
:
10107 case Type::TYPE_CALL_MULTIPLE_RESULT
:
10108 case Type::TYPE_MAP
:
10109 case Type::TYPE_CHANNEL
:
10110 case Type::TYPE_INTERFACE
:
10116 return TRAVERSE_CONTINUE
;
10119 // Look for a circular reference of an alias.
10121 class Find_alias
: public Traverse
10124 Find_alias(Named_type
* find_type
)
10125 : Traverse(traverse_types
),
10126 find_type_(find_type
), found_(false)
10129 // Whether we found the type.
10132 { return this->found_
; }
10139 // The type we are looking for.
10140 Named_type
* find_type_
;
10141 // Whether we found the type.
10146 Find_alias::type(Type
* type
)
10148 Named_type
* nt
= type
->named_type();
10151 if (nt
== this->find_type_
)
10153 this->found_
= true;
10154 return TRAVERSE_EXIT
;
10157 // We started from `type T1 = T2`, where T1 is find_type_ and T2
10158 // is, perhaps indirectly, the parameter TYPE. If TYPE is not
10159 // an alias itself, it's OK if whatever T2 is defined as refers
10161 if (!nt
->is_alias())
10162 return TRAVERSE_SKIP_COMPONENTS
;
10165 return TRAVERSE_CONTINUE
;
10168 // Verify that a named type does not refer to itself.
10171 Named_type::do_verify()
10173 if (this->is_verified_
)
10175 this->is_verified_
= true;
10177 if (this->is_error_
)
10180 if (this->is_alias_
)
10182 Find_alias
find(this);
10183 Type::traverse(this->type_
, &find
);
10186 go_error_at(this->location_
, "invalid recursive alias %qs",
10187 this->message_name().c_str());
10188 this->is_error_
= true;
10193 Find_type_use
find(this);
10194 Type::traverse(this->type_
, &find
);
10197 go_error_at(this->location_
, "invalid recursive type %qs",
10198 this->message_name().c_str());
10199 this->is_error_
= true;
10203 // Check whether any of the local methods overloads an existing
10204 // struct field or interface method. We don't need to check the
10205 // list of methods against itself: that is handled by the Bindings
10207 if (this->local_methods_
!= NULL
)
10209 Struct_type
* st
= this->type_
->struct_type();
10212 for (Bindings::const_declarations_iterator p
=
10213 this->local_methods_
->begin_declarations();
10214 p
!= this->local_methods_
->end_declarations();
10217 const std::string
& name(p
->first
);
10218 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
10220 go_error_at(p
->second
->location(),
10221 "method %qs redeclares struct field name",
10222 Gogo::message_name(name
).c_str());
10231 // Return whether this type is or contains a pointer.
10234 Named_type::do_has_pointer() const
10238 this->seen_
= true;
10239 bool ret
= this->type_
->has_pointer();
10240 this->seen_
= false;
10244 // Return whether comparisons for this type can use the identity
10248 Named_type::do_compare_is_identity(Gogo
* gogo
)
10250 // We don't use this->seen_ here because compare_is_identity may
10251 // call base() later, and that will mess up if seen_ is set here.
10252 if (this->seen_in_compare_is_identity_
)
10254 this->seen_in_compare_is_identity_
= true;
10255 bool ret
= this->type_
->compare_is_identity(gogo
);
10256 this->seen_in_compare_is_identity_
= false;
10260 // Return whether this type is reflexive--whether it is always equal
10264 Named_type::do_is_reflexive()
10266 if (this->seen_in_compare_is_identity_
)
10268 this->seen_in_compare_is_identity_
= true;
10269 bool ret
= this->type_
->is_reflexive();
10270 this->seen_in_compare_is_identity_
= false;
10274 // Return whether this type needs a key update when used as a map key.
10277 Named_type::do_needs_key_update()
10279 if (this->seen_in_compare_is_identity_
)
10281 this->seen_in_compare_is_identity_
= true;
10282 bool ret
= this->type_
->needs_key_update();
10283 this->seen_in_compare_is_identity_
= false;
10287 // Return a hash code. This is used for method lookup. We simply
10288 // hash on the name itself.
10291 Named_type::do_hash_for_method(Gogo
* gogo
) const
10293 if (this->is_error_
)
10296 // Aliases are handled in Type::hash_for_method.
10297 go_assert(!this->is_alias_
);
10299 const std::string
& name(this->named_object()->name());
10300 unsigned int ret
= Type::hash_string(name
, 0);
10302 // GOGO will be NULL here when called from Type_hash_identical.
10303 // That is OK because that is only used for internal hash tables
10304 // where we are going to be comparing named types for equality. In
10305 // other cases, which are cases where the runtime is going to
10306 // compare hash codes to see if the types are the same, we need to
10307 // include the pkgpath in the hash.
10308 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
10310 const Package
* package
= this->named_object()->package();
10311 if (package
== NULL
)
10312 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
10314 ret
= Type::hash_string(package
->pkgpath(), ret
);
10320 // Convert a named type to the backend representation. In order to
10321 // get dependencies right, we fill in a dummy structure for this type,
10322 // then convert all the dependencies, then complete this type. When
10323 // this function is complete, the size of the type is known.
10326 Named_type::convert(Gogo
* gogo
)
10328 if (this->is_error_
|| this->is_converted_
)
10331 this->create_placeholder(gogo
);
10333 // If we are called to turn unsafe.Sizeof into a constant, we may
10334 // not have verified the type yet. We have to make sure it is
10335 // verified, since that sets the list of dependencies.
10338 // Convert all the dependencies. If they refer indirectly back to
10339 // this type, they will pick up the intermediate representation we just
10341 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
10342 p
!= this->dependencies_
.end();
10344 (*p
)->convert(gogo
);
10346 // Complete this type.
10347 Btype
* bt
= this->named_btype_
;
10348 Type
* base
= this->type_
->base();
10349 switch (base
->classification())
10364 case TYPE_FUNCTION
:
10366 // The size of these types is already correct. We don't worry
10367 // about filling them in until later, when we also track
10368 // circular references.
10373 std::vector
<Backend::Btyped_identifier
> bfields
;
10374 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
10376 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10377 bt
= gogo
->backend()->error_type();
10382 // Slice types were completed in create_placeholder.
10383 if (!base
->is_slice_type())
10385 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
10386 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
10387 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
10388 bt
= gogo
->backend()->error_type();
10392 case TYPE_INTERFACE
:
10393 // Interface types were completed in create_placeholder.
10401 case TYPE_CALL_MULTIPLE_RESULT
:
10407 this->named_btype_
= bt
;
10408 this->is_converted_
= true;
10409 this->is_placeholder_
= false;
10412 // Create the placeholder for a named type. This is the first step in
10413 // converting to the backend representation.
10416 Named_type::create_placeholder(Gogo
* gogo
)
10418 if (this->is_error_
)
10419 this->named_btype_
= gogo
->backend()->error_type();
10421 if (this->named_btype_
!= NULL
)
10424 // Create the structure for this type. Note that because we call
10425 // base() here, we don't attempt to represent a named type defined
10426 // as another named type. Instead both named types will point to
10427 // different base representations.
10428 Type
* base
= this->type_
->base();
10430 bool set_name
= true;
10431 switch (base
->classification())
10434 this->is_error_
= true;
10435 this->named_btype_
= gogo
->backend()->error_type();
10445 // These are simple basic types, we can just create them
10447 bt
= Type::get_named_base_btype(gogo
, base
);
10452 // All maps and channels have the same backend representation.
10453 bt
= Type::get_named_base_btype(gogo
, base
);
10456 case TYPE_FUNCTION
:
10459 bool for_function
= base
->classification() == TYPE_FUNCTION
;
10460 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
10468 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10470 this->is_placeholder_
= true;
10475 if (base
->is_slice_type())
10476 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10480 bt
= gogo
->backend()->placeholder_array_type(this->name(),
10482 this->is_placeholder_
= true;
10487 case TYPE_INTERFACE
:
10488 if (base
->interface_type()->is_empty())
10489 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
10492 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10500 case TYPE_CALL_MULTIPLE_RESULT
:
10507 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
10509 this->named_btype_
= bt
;
10511 if (base
->is_slice_type())
10513 // We do not record slices as dependencies of other types,
10514 // because we can fill them in completely here with the final
10516 std::vector
<Backend::Btyped_identifier
> bfields
;
10517 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
10518 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10519 this->named_btype_
= gogo
->backend()->error_type();
10521 else if (base
->interface_type() != NULL
10522 && !base
->interface_type()->is_empty())
10524 // We do not record interfaces as dependencies of other types,
10525 // because we can fill them in completely here with the final
10527 std::vector
<Backend::Btyped_identifier
> bfields
;
10528 get_backend_interface_fields(gogo
, base
->interface_type(), true,
10530 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10531 this->named_btype_
= gogo
->backend()->error_type();
10535 // Get the backend representation for a named type.
10538 Named_type::do_get_backend(Gogo
* gogo
)
10540 if (this->is_error_
)
10541 return gogo
->backend()->error_type();
10543 Btype
* bt
= this->named_btype_
;
10545 if (!gogo
->named_types_are_converted())
10547 // We have not completed converting named types. NAMED_BTYPE_
10548 // is a placeholder and we shouldn't do anything further.
10552 // We don't build dependencies for types whose sizes do not
10553 // change or are not relevant, so we may see them here while
10554 // converting types.
10555 this->create_placeholder(gogo
);
10556 bt
= this->named_btype_
;
10557 go_assert(bt
!= NULL
);
10561 // We are not converting types. This should only be called if the
10562 // type has already been converted.
10563 if (!this->is_converted_
)
10565 go_assert(saw_errors());
10566 return gogo
->backend()->error_type();
10569 go_assert(bt
!= NULL
);
10571 // Complete the backend representation.
10572 Type
* base
= this->type_
->base();
10574 switch (base
->classification())
10577 return gogo
->backend()->error_type();
10591 if (!this->seen_in_get_backend_
)
10593 this->seen_in_get_backend_
= true;
10594 base
->struct_type()->finish_backend_fields(gogo
);
10595 this->seen_in_get_backend_
= false;
10600 if (!this->seen_in_get_backend_
)
10602 this->seen_in_get_backend_
= true;
10603 base
->array_type()->finish_backend_element(gogo
);
10604 this->seen_in_get_backend_
= false;
10608 case TYPE_INTERFACE
:
10609 if (!this->seen_in_get_backend_
)
10611 this->seen_in_get_backend_
= true;
10612 base
->interface_type()->finish_backend_methods(gogo
);
10613 this->seen_in_get_backend_
= false;
10617 case TYPE_FUNCTION
:
10618 // Don't build a circular data structure. GENERIC can't handle
10620 if (this->seen_in_get_backend_
)
10622 this->is_circular_
= true;
10623 return gogo
->backend()->circular_pointer_type(bt
, true);
10625 this->seen_in_get_backend_
= true;
10626 bt1
= Type::get_named_base_btype(gogo
, base
);
10627 this->seen_in_get_backend_
= false;
10628 if (this->is_circular_
)
10629 bt1
= gogo
->backend()->circular_pointer_type(bt
, true);
10630 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10631 bt
= gogo
->backend()->error_type();
10635 // Don't build a circular data structure. GENERIC can't handle
10637 if (this->seen_in_get_backend_
)
10639 this->is_circular_
= true;
10640 return gogo
->backend()->circular_pointer_type(bt
, false);
10642 this->seen_in_get_backend_
= true;
10643 bt1
= Type::get_named_base_btype(gogo
, base
);
10644 this->seen_in_get_backend_
= false;
10645 if (this->is_circular_
)
10646 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
10647 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10648 bt
= gogo
->backend()->error_type();
10653 case TYPE_CALL_MULTIPLE_RESULT
:
10662 // Build a type descriptor for a named type.
10665 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
10667 if (this->is_error_
)
10668 return Expression::make_error(this->location_
);
10669 if (name
== NULL
&& this->is_alias_
)
10671 if (this->seen_alias_
)
10672 return Expression::make_error(this->location_
);
10673 this->seen_alias_
= true;
10674 Expression
* ret
= this->type_
->type_descriptor(gogo
, NULL
);
10675 this->seen_alias_
= false;
10679 // If NAME is not NULL, then we don't really want the type
10680 // descriptor for this type; we want the descriptor for the
10681 // underlying type, giving it the name NAME.
10682 return this->named_type_descriptor(gogo
, this->type_
,
10683 name
== NULL
? this : name
);
10686 // Add to the reflection string. This is used mostly for the name of
10687 // the type used in a type descriptor, not for actual reflection
10691 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
10693 this->append_reflection_type_name(gogo
, false, ret
);
10696 // Add to the reflection string. For an alias we normally use the
10697 // real name, but if USE_ALIAS is true we use the alias name itself.
10700 Named_type::append_reflection_type_name(Gogo
* gogo
, bool use_alias
,
10701 std::string
* ret
) const
10703 if (this->is_error_
)
10705 if (this->is_alias_
&& !use_alias
)
10707 if (this->seen_alias_
)
10709 this->seen_alias_
= true;
10710 this->append_reflection(this->type_
, gogo
, ret
);
10711 this->seen_alias_
= false;
10714 if (!this->is_builtin())
10716 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
10717 // make a unique reflection string, so that the type
10718 // canonicalization in the reflect package will work. In order
10719 // to be compatible with the gc compiler, we put tabs into the
10720 // package path, so that the reflect methods can discard it.
10721 const Package
* package
= this->named_object_
->package();
10722 ret
->push_back('\t');
10723 ret
->append(package
!= NULL
10724 ? package
->pkgpath_symbol()
10725 : gogo
->pkgpath_symbol());
10726 ret
->push_back('\t');
10727 ret
->append(package
!= NULL
10728 ? package
->package_name()
10729 : gogo
->package_name());
10730 ret
->push_back('.');
10732 if (this->in_function_
!= NULL
)
10734 ret
->push_back('\t');
10735 const Typed_identifier
* rcvr
=
10736 this->in_function_
->func_value()->type()->receiver();
10739 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
10740 ret
->append(Gogo::unpack_hidden_name(rcvr_type
->name()));
10741 ret
->push_back('.');
10743 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
10744 ret
->push_back('$');
10745 if (this->in_function_index_
> 0)
10748 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
10750 ret
->push_back('$');
10752 ret
->push_back('\t');
10754 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
10757 // Export the type. This is called to export a global type.
10760 Named_type::export_named_type(Export
* exp
, const std::string
&) const
10762 // We don't need to write the name of the type here, because it will
10763 // be written by Export::write_type anyhow.
10764 exp
->write_c_string("type ");
10765 exp
->write_type(this);
10766 exp
->write_c_string(";\n");
10769 // Import a named type.
10772 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
10774 imp
->require_c_string("type ");
10775 Type
*type
= imp
->read_type();
10776 *ptype
= type
->named_type();
10777 go_assert(*ptype
!= NULL
);
10778 imp
->require_c_string(";\n");
10781 // Export the type when it is referenced by another type. In this
10782 // case Export::export_type will already have issued the name.
10785 Named_type::do_export(Export
* exp
) const
10787 exp
->write_type(this->type_
);
10789 // To save space, we only export the methods directly attached to
10791 Bindings
* methods
= this->local_methods_
;
10792 if (methods
== NULL
)
10795 exp
->write_c_string("\n");
10796 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
10797 p
!= methods
->end_definitions();
10800 exp
->write_c_string(" ");
10801 (*p
)->export_named_object(exp
);
10804 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
10805 p
!= methods
->end_declarations();
10808 if (p
->second
->is_function_declaration())
10810 exp
->write_c_string(" ");
10811 p
->second
->export_named_object(exp
);
10816 // Make a named type.
10819 Type::make_named_type(Named_object
* named_object
, Type
* type
,
10822 return new Named_type(named_object
, type
, location
);
10825 // Finalize the methods for TYPE. It will be a named type or a struct
10826 // type. This sets *ALL_METHODS to the list of methods, and builds
10827 // all required stubs.
10830 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
10831 Methods
** all_methods
)
10833 *all_methods
= new Methods();
10834 std::vector
<const Named_type
*> seen
;
10835 Type::add_methods_for_type(type
, NULL
, 0, false, false, &seen
, *all_methods
);
10836 if ((*all_methods
)->empty())
10838 delete *all_methods
;
10839 *all_methods
= NULL
;
10841 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
10844 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
10845 // build up the struct field indexes as we go. DEPTH is the depth of
10846 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
10847 // adding these methods for an anonymous field with pointer type.
10848 // NEEDS_STUB_METHOD is true if we need to use a stub method which
10849 // calls the real method. TYPES_SEEN is used to avoid infinite
10853 Type::add_methods_for_type(const Type
* type
,
10854 const Method::Field_indexes
* field_indexes
,
10855 unsigned int depth
,
10856 bool is_embedded_pointer
,
10857 bool needs_stub_method
,
10858 std::vector
<const Named_type
*>* seen
,
10861 // Pointer types may not have methods.
10862 if (type
->points_to() != NULL
)
10865 const Named_type
* nt
= type
->named_type();
10868 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
10876 seen
->push_back(nt
);
10878 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
10879 is_embedded_pointer
, needs_stub_method
,
10883 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
10884 is_embedded_pointer
, needs_stub_method
,
10887 // If we are called with depth > 0, then we are looking at an
10888 // anonymous field of a struct. If such a field has interface type,
10889 // then we need to add the interface methods. We don't want to add
10890 // them when depth == 0, because we will already handle them
10891 // following the usual rules for an interface type.
10893 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
10899 // Add the local methods for the named type NT to *METHODS. The
10900 // parameters are as for add_methods_to_type.
10903 Type::add_local_methods_for_type(const Named_type
* nt
,
10904 const Method::Field_indexes
* field_indexes
,
10905 unsigned int depth
,
10906 bool is_embedded_pointer
,
10907 bool needs_stub_method
,
10910 const Bindings
* local_methods
= nt
->local_methods();
10911 if (local_methods
== NULL
)
10914 for (Bindings::const_declarations_iterator p
=
10915 local_methods
->begin_declarations();
10916 p
!= local_methods
->end_declarations();
10919 Named_object
* no
= p
->second
;
10920 bool is_value_method
= (is_embedded_pointer
10921 || !Type::method_expects_pointer(no
));
10922 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
10923 (needs_stub_method
|| depth
> 0));
10924 if (!methods
->insert(no
->name(), m
))
10929 // Add the embedded methods for TYPE to *METHODS. These are the
10930 // methods attached to anonymous fields. The parameters are as for
10931 // add_methods_to_type.
10934 Type::add_embedded_methods_for_type(const Type
* type
,
10935 const Method::Field_indexes
* field_indexes
,
10936 unsigned int depth
,
10937 bool is_embedded_pointer
,
10938 bool needs_stub_method
,
10939 std::vector
<const Named_type
*>* seen
,
10942 // Look for anonymous fields in TYPE. TYPE has fields if it is a
10944 const Struct_type
* st
= type
->struct_type();
10948 const Struct_field_list
* fields
= st
->fields();
10949 if (fields
== NULL
)
10952 unsigned int i
= 0;
10953 for (Struct_field_list::const_iterator pf
= fields
->begin();
10954 pf
!= fields
->end();
10957 if (!pf
->is_anonymous())
10960 Type
* ftype
= pf
->type();
10961 bool is_pointer
= false;
10962 if (ftype
->points_to() != NULL
)
10964 ftype
= ftype
->points_to();
10967 Named_type
* fnt
= ftype
->named_type();
10970 // This is an error, but it will be diagnosed elsewhere.
10974 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
10975 sub_field_indexes
->next
= field_indexes
;
10976 sub_field_indexes
->field_index
= i
;
10978 Methods tmp_methods
;
10979 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
10980 (is_embedded_pointer
|| is_pointer
),
10986 // Check if there are promoted methods that conflict with field names and
10987 // don't add them to the method map.
10988 for (Methods::const_iterator p
= tmp_methods
.begin();
10989 p
!= tmp_methods
.end();
10992 bool found
= false;
10993 for (Struct_field_list::const_iterator fp
= fields
->begin();
10994 fp
!= fields
->end();
10997 if (fp
->field_name() == p
->first
)
11004 !methods
->insert(p
->first
, p
->second
))
11010 // If TYPE is an interface type, then add its method to *METHODS.
11011 // This is for interface methods attached to an anonymous field. The
11012 // parameters are as for add_methods_for_type.
11015 Type::add_interface_methods_for_type(const Type
* type
,
11016 const Method::Field_indexes
* field_indexes
,
11017 unsigned int depth
,
11020 const Interface_type
* it
= type
->interface_type();
11024 const Typed_identifier_list
* imethods
= it
->methods();
11025 if (imethods
== NULL
)
11028 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
11029 pm
!= imethods
->end();
11032 Function_type
* fntype
= pm
->type()->function_type();
11033 if (fntype
== NULL
)
11035 // This is an error, but it should be reported elsewhere
11036 // when we look at the methods for IT.
11039 go_assert(!fntype
->is_method());
11040 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
11041 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
11042 field_indexes
, depth
);
11043 if (!methods
->insert(pm
->name(), m
))
11048 // Build stub methods for TYPE as needed. METHODS is the set of
11049 // methods for the type. A stub method may be needed when a type
11050 // inherits a method from an anonymous field. When we need the
11051 // address of the method, as in a type descriptor, we need to build a
11052 // little stub which does the required field dereferences and jumps to
11053 // the real method. LOCATION is the location of the type definition.
11056 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
11059 if (methods
== NULL
)
11061 for (Methods::const_iterator p
= methods
->begin();
11062 p
!= methods
->end();
11065 Method
* m
= p
->second
;
11066 if (m
->is_ambiguous() || !m
->needs_stub_method())
11069 const std::string
& name(p
->first
);
11071 // Build a stub method.
11073 const Function_type
* fntype
= m
->type();
11075 static unsigned int counter
;
11077 snprintf(buf
, sizeof buf
, "$this%u", counter
);
11080 Type
* receiver_type
= const_cast<Type
*>(type
);
11081 if (!m
->is_value_method())
11082 receiver_type
= Type::make_pointer_type(receiver_type
);
11083 Location receiver_location
= m
->receiver_location();
11084 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
11085 receiver_location
);
11087 const Typed_identifier_list
* fnparams
= fntype
->parameters();
11088 Typed_identifier_list
* stub_params
;
11089 if (fnparams
== NULL
|| fnparams
->empty())
11090 stub_params
= NULL
;
11093 // We give each stub parameter a unique name.
11094 stub_params
= new Typed_identifier_list();
11095 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
11096 pp
!= fnparams
->end();
11100 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
11101 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
11107 const Typed_identifier_list
* fnresults
= fntype
->results();
11108 Typed_identifier_list
* stub_results
;
11109 if (fnresults
== NULL
|| fnresults
->empty())
11110 stub_results
= NULL
;
11113 // We create the result parameters without any names, since
11114 // we won't refer to them.
11115 stub_results
= new Typed_identifier_list();
11116 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
11117 pr
!= fnresults
->end();
11119 stub_results
->push_back(Typed_identifier("", pr
->type(),
11123 Function_type
* stub_type
= Type::make_function_type(receiver
,
11126 fntype
->location());
11127 if (fntype
->is_varargs())
11128 stub_type
->set_is_varargs();
11130 // We only create the function in the package which creates the
11132 const Package
* package
;
11133 if (type
->named_type() == NULL
)
11136 package
= type
->named_type()->named_object()->package();
11137 std::string stub_name
= gogo
->stub_method_name(name
);
11138 Named_object
* stub
;
11139 if (package
!= NULL
)
11140 stub
= Named_object::make_function_declaration(stub_name
, package
,
11141 stub_type
, location
);
11144 stub
= gogo
->start_function(stub_name
, stub_type
, false,
11145 fntype
->location());
11146 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
11147 fntype
->is_varargs(), location
);
11148 gogo
->finish_function(fntype
->location());
11150 if (type
->named_type() == NULL
&& stub
->is_function())
11151 stub
->func_value()->set_is_unnamed_type_stub_method();
11152 if (m
->nointerface() && stub
->is_function())
11153 stub
->func_value()->set_nointerface();
11156 m
->set_stub_object(stub
);
11160 // Build a stub method which adjusts the receiver as required to call
11161 // METHOD. RECEIVER_NAME is the name we used for the receiver.
11162 // PARAMS is the list of function parameters.
11165 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
11166 const char* receiver_name
,
11167 const Typed_identifier_list
* params
,
11171 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
11172 go_assert(receiver_object
!= NULL
);
11174 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
11175 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
11176 if (expr
->type()->points_to() == NULL
)
11177 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11179 Expression_list
* arguments
;
11180 if (params
== NULL
|| params
->empty())
11184 arguments
= new Expression_list();
11185 for (Typed_identifier_list::const_iterator p
= params
->begin();
11186 p
!= params
->end();
11189 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
11190 go_assert(param
!= NULL
);
11191 Expression
* param_ref
= Expression::make_var_reference(param
,
11193 arguments
->push_back(param_ref
);
11197 Expression
* func
= method
->bind_method(expr
, location
);
11198 go_assert(func
!= NULL
);
11199 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
11202 gogo
->add_statement(Statement::make_return_from_call(call
, location
));
11205 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
11206 // in reverse order.
11209 Type::apply_field_indexes(Expression
* expr
,
11210 const Method::Field_indexes
* field_indexes
,
11213 if (field_indexes
== NULL
)
11215 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
11216 Struct_type
* stype
= expr
->type()->deref()->struct_type();
11217 go_assert(stype
!= NULL
11218 && field_indexes
->field_index
< stype
->field_count());
11219 if (expr
->type()->struct_type() == NULL
)
11221 go_assert(expr
->type()->points_to() != NULL
);
11222 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
11223 go_assert(expr
->type()->struct_type() == stype
);
11225 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
11229 // Return whether NO is a method for which the receiver is a pointer.
11232 Type::method_expects_pointer(const Named_object
* no
)
11234 const Function_type
*fntype
;
11235 if (no
->is_function())
11236 fntype
= no
->func_value()->type();
11237 else if (no
->is_function_declaration())
11238 fntype
= no
->func_declaration_value()->type();
11241 return fntype
->receiver()->type()->points_to() != NULL
;
11244 // Given a set of methods for a type, METHODS, return the method NAME,
11245 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
11246 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
11247 // but is ambiguous (and return NULL).
11250 Type::method_function(const Methods
* methods
, const std::string
& name
,
11251 bool* is_ambiguous
)
11253 if (is_ambiguous
!= NULL
)
11254 *is_ambiguous
= false;
11255 if (methods
== NULL
)
11257 Methods::const_iterator p
= methods
->find(name
);
11258 if (p
== methods
->end())
11260 Method
* m
= p
->second
;
11261 if (m
->is_ambiguous())
11263 if (is_ambiguous
!= NULL
)
11264 *is_ambiguous
= true;
11270 // Return a pointer to the interface method table for TYPE for the
11271 // interface INTERFACE.
11274 Type::interface_method_table(Type
* type
,
11275 Interface_type
*interface
,
11277 Interface_method_tables
** method_tables
,
11278 Interface_method_tables
** pointer_tables
)
11280 go_assert(!interface
->is_empty());
11282 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
11285 *pimt
= new Interface_method_tables(5);
11287 std::pair
<Interface_type
*, Expression
*> val(interface
, NULL
);
11288 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
11290 Location loc
= Linemap::predeclared_location();
11293 // This is a new entry in the hash table.
11294 go_assert(ins
.first
->second
== NULL
);
11295 ins
.first
->second
=
11296 Expression::make_interface_mtable_ref(interface
, type
, is_pointer
, loc
);
11298 return Expression::make_unary(OPERATOR_AND
, ins
.first
->second
, loc
);
11301 // Look for field or method NAME for TYPE. Return an Expression for
11302 // the field or method bound to EXPR. If there is no such field or
11303 // method, give an appropriate error and return an error expression.
11306 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
11307 const std::string
& name
,
11310 if (type
->deref()->is_error_type())
11311 return Expression::make_error(location
);
11313 const Named_type
* nt
= type
->deref()->named_type();
11314 const Struct_type
* st
= type
->deref()->struct_type();
11315 const Interface_type
* it
= type
->interface_type();
11317 // If this is a pointer to a pointer, then it is possible that the
11318 // pointed-to type has methods.
11319 bool dereferenced
= false;
11323 && type
->points_to() != NULL
11324 && type
->points_to()->points_to() != NULL
)
11326 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
11327 type
= type
->points_to();
11328 if (type
->deref()->is_error_type())
11329 return Expression::make_error(location
);
11330 nt
= type
->points_to()->named_type();
11331 st
= type
->points_to()->struct_type();
11332 dereferenced
= true;
11335 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
11336 || expr
->is_addressable());
11337 std::vector
<const Named_type
*> seen
;
11338 bool is_method
= false;
11339 bool found_pointer_method
= false;
11340 std::string ambig1
;
11341 std::string ambig2
;
11342 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
11343 &seen
, NULL
, &is_method
,
11344 &found_pointer_method
, &ambig1
, &ambig2
))
11349 go_assert(st
!= NULL
);
11350 if (type
->struct_type() == NULL
)
11354 go_error_at(location
, "pointer type has no field %qs",
11355 Gogo::message_name(name
).c_str());
11356 return Expression::make_error(location
);
11358 go_assert(type
->points_to() != NULL
);
11359 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
11361 go_assert(expr
->type()->struct_type() == st
);
11363 ret
= st
->field_reference(expr
, name
, location
);
11366 go_error_at(location
, "type has no field %qs",
11367 Gogo::message_name(name
).c_str());
11368 return Expression::make_error(location
);
11371 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11372 ret
= Expression::make_interface_field_reference(expr
, name
,
11378 m
= nt
->method_function(name
, NULL
);
11379 else if (st
!= NULL
)
11380 m
= st
->method_function(name
, NULL
);
11383 go_assert(m
!= NULL
);
11386 go_error_at(location
,
11387 "calling method %qs requires explicit dereference",
11388 Gogo::message_name(name
).c_str());
11389 return Expression::make_error(location
);
11391 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
11392 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11393 ret
= m
->bind_method(expr
, location
);
11395 go_assert(ret
!= NULL
);
11400 if (Gogo::is_erroneous_name(name
))
11402 // An error was already reported.
11404 else if (!ambig1
.empty())
11405 go_error_at(location
, "%qs is ambiguous via %qs and %qs",
11406 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
11408 else if (found_pointer_method
)
11409 go_error_at(location
, "method requires a pointer receiver");
11410 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
11411 go_error_at(location
,
11412 ("reference to field %qs in object which "
11413 "has no fields or methods"),
11414 Gogo::message_name(name
).c_str());
11417 bool is_unexported
;
11418 // The test for 'a' and 'z' is to handle builtin names,
11419 // which are not hidden.
11420 if (!Gogo::is_hidden_name(name
) && (name
[0] < 'a' || name
[0] > 'z'))
11421 is_unexported
= false;
11424 std::string unpacked
= Gogo::unpack_hidden_name(name
);
11426 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
11431 go_error_at(location
, "reference to unexported field or method %qs",
11432 Gogo::message_name(name
).c_str());
11434 go_error_at(location
, "reference to undefined field or method %qs",
11435 Gogo::message_name(name
).c_str());
11437 return Expression::make_error(location
);
11441 // Look in TYPE for a field or method named NAME, return true if one
11442 // is found. This looks through embedded anonymous fields and handles
11443 // ambiguity. If a method is found, sets *IS_METHOD to true;
11444 // otherwise, if a field is found, set it to false. If
11445 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
11446 // whose address can not be taken. SEEN is used to avoid infinite
11447 // recursion on invalid types.
11449 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
11450 // method we couldn't use because it requires a pointer. LEVEL is
11451 // used for recursive calls, and can be NULL for a non-recursive call.
11452 // When this function returns false because it finds that the name is
11453 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
11454 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
11455 // will be unchanged.
11457 // This function just returns whether or not there is a field or
11458 // method, and whether it is a field or method. It doesn't build an
11459 // expression to refer to it. If it is a method, we then look in the
11460 // list of all methods for the type. If it is a field, the search has
11461 // to be done again, looking only for fields, and building up the
11462 // expression as we go.
11465 Type::find_field_or_method(const Type
* type
,
11466 const std::string
& name
,
11467 bool receiver_can_be_pointer
,
11468 std::vector
<const Named_type
*>* seen
,
11471 bool* found_pointer_method
,
11472 std::string
* ambig1
,
11473 std::string
* ambig2
)
11475 // Named types can have locally defined methods.
11476 const Named_type
* nt
= type
->named_type();
11477 if (nt
== NULL
&& type
->points_to() != NULL
)
11478 nt
= type
->points_to()->named_type();
11481 Named_object
* no
= nt
->find_local_method(name
);
11484 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
11490 // Record that we have found a pointer method in order to
11491 // give a better error message if we don't find anything
11493 *found_pointer_method
= true;
11496 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11502 // We've already seen this type when searching for methods.
11508 // Interface types can have methods.
11509 const Interface_type
* it
= type
->interface_type();
11510 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11516 // Struct types can have fields. They can also inherit fields and
11517 // methods from anonymous fields.
11518 const Struct_type
* st
= type
->deref()->struct_type();
11521 const Struct_field_list
* fields
= st
->fields();
11522 if (fields
== NULL
)
11526 seen
->push_back(nt
);
11528 int found_level
= 0;
11529 bool found_is_method
= false;
11530 std::string found_ambig1
;
11531 std::string found_ambig2
;
11532 const Struct_field
* found_parent
= NULL
;
11533 for (Struct_field_list::const_iterator pf
= fields
->begin();
11534 pf
!= fields
->end();
11537 if (pf
->is_field_name(name
))
11539 *is_method
= false;
11545 if (!pf
->is_anonymous())
11548 if (pf
->type()->deref()->is_error_type()
11549 || pf
->type()->deref()->is_undefined())
11552 Named_type
* fnt
= pf
->type()->named_type();
11554 fnt
= pf
->type()->deref()->named_type();
11555 go_assert(fnt
!= NULL
);
11557 // Methods with pointer receivers on embedded field are
11558 // inherited by the pointer to struct, and also by the struct
11559 // type if the field itself is a pointer.
11560 bool can_be_pointer
= (receiver_can_be_pointer
11561 || pf
->type()->points_to() != NULL
);
11562 int sublevel
= level
== NULL
? 1 : *level
+ 1;
11563 bool sub_is_method
;
11564 std::string subambig1
;
11565 std::string subambig2
;
11566 bool subfound
= Type::find_field_or_method(fnt
,
11572 found_pointer_method
,
11577 if (!subambig1
.empty())
11579 // The name was found via this field, but is ambiguous.
11580 // if the ambiguity is lower or at the same level as
11581 // anything else we have already found, then we want to
11582 // pass the ambiguity back to the caller.
11583 if (found_level
== 0 || sublevel
<= found_level
)
11585 found_ambig1
= (Gogo::message_name(pf
->field_name())
11586 + '.' + subambig1
);
11587 found_ambig2
= (Gogo::message_name(pf
->field_name())
11588 + '.' + subambig2
);
11589 found_level
= sublevel
;
11595 // The name was found via this field. Use the level to see
11596 // if we want to use this one, or whether it introduces an
11598 if (found_level
== 0 || sublevel
< found_level
)
11600 found_level
= sublevel
;
11601 found_is_method
= sub_is_method
;
11602 found_ambig1
.clear();
11603 found_ambig2
.clear();
11604 found_parent
= &*pf
;
11606 else if (sublevel
> found_level
)
11608 else if (found_ambig1
.empty())
11610 // We found an ambiguity.
11611 go_assert(found_parent
!= NULL
);
11612 found_ambig1
= Gogo::message_name(found_parent
->field_name());
11613 found_ambig2
= Gogo::message_name(pf
->field_name());
11617 // We found an ambiguity, but we already know of one.
11618 // Just report the earlier one.
11623 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
11624 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
11625 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
11626 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
11631 if (found_level
== 0)
11633 else if (found_is_method
11634 && type
->named_type() != NULL
11635 && type
->points_to() != NULL
)
11637 // If this is a method inherited from a struct field in a named pointer
11638 // type, it is invalid to automatically dereference the pointer to the
11639 // struct to find this method.
11641 *level
= found_level
;
11645 else if (!found_ambig1
.empty())
11647 go_assert(!found_ambig1
.empty());
11648 ambig1
->assign(found_ambig1
);
11649 ambig2
->assign(found_ambig2
);
11651 *level
= found_level
;
11657 *level
= found_level
;
11658 *is_method
= found_is_method
;
11663 // Return whether NAME is an unexported field or method for TYPE.
11666 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
11667 const std::string
& name
,
11668 std::vector
<const Named_type
*>* seen
)
11670 const Named_type
* nt
= type
->named_type();
11672 nt
= type
->deref()->named_type();
11675 if (nt
->is_unexported_local_method(gogo
, name
))
11678 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11684 // We've already seen this type.
11690 const Interface_type
* it
= type
->interface_type();
11691 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
11694 type
= type
->deref();
11696 const Struct_type
* st
= type
->struct_type();
11697 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
11703 const Struct_field_list
* fields
= st
->fields();
11704 if (fields
== NULL
)
11708 seen
->push_back(nt
);
11710 for (Struct_field_list::const_iterator pf
= fields
->begin();
11711 pf
!= fields
->end();
11714 if (pf
->is_anonymous()
11715 && !pf
->type()->deref()->is_error_type()
11716 && !pf
->type()->deref()->is_undefined())
11718 Named_type
* subtype
= pf
->type()->named_type();
11719 if (subtype
== NULL
)
11720 subtype
= pf
->type()->deref()->named_type();
11721 if (subtype
== NULL
)
11723 // This is an error, but it will be diagnosed elsewhere.
11726 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
11741 // Class Forward_declaration.
11743 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
11744 : Type(TYPE_FORWARD
),
11745 named_object_(named_object
->resolve()), warned_(false)
11747 go_assert(this->named_object_
->is_unknown()
11748 || this->named_object_
->is_type_declaration());
11751 // Return the named object.
11754 Forward_declaration_type::named_object()
11756 return this->named_object_
->resolve();
11759 const Named_object
*
11760 Forward_declaration_type::named_object() const
11762 return this->named_object_
->resolve();
11765 // Return the name of the forward declared type.
11768 Forward_declaration_type::name() const
11770 return this->named_object()->name();
11773 // Warn about a use of a type which has been declared but not defined.
11776 Forward_declaration_type::warn() const
11778 Named_object
* no
= this->named_object_
->resolve();
11779 if (no
->is_unknown())
11781 // The name was not defined anywhere.
11782 if (!this->warned_
)
11784 go_error_at(this->named_object_
->location(),
11785 "use of undefined type %qs",
11786 no
->message_name().c_str());
11787 this->warned_
= true;
11790 else if (no
->is_type_declaration())
11792 // The name was seen as a type, but the type was never defined.
11793 if (no
->type_declaration_value()->using_type())
11795 go_error_at(this->named_object_
->location(),
11796 "use of undefined type %qs",
11797 no
->message_name().c_str());
11798 this->warned_
= true;
11803 // The name was defined, but not as a type.
11804 if (!this->warned_
)
11806 go_error_at(this->named_object_
->location(), "expected type");
11807 this->warned_
= true;
11812 // Get the base type of a declaration. This gives an error if the
11813 // type has not yet been defined.
11816 Forward_declaration_type::real_type()
11818 if (this->is_defined())
11820 Named_type
* nt
= this->named_object()->type_value();
11821 if (!nt
->is_valid())
11822 return Type::make_error_type();
11823 return this->named_object()->type_value();
11828 return Type::make_error_type();
11833 Forward_declaration_type::real_type() const
11835 if (this->is_defined())
11837 const Named_type
* nt
= this->named_object()->type_value();
11838 if (!nt
->is_valid())
11839 return Type::make_error_type();
11840 return this->named_object()->type_value();
11845 return Type::make_error_type();
11849 // Return whether the base type is defined.
11852 Forward_declaration_type::is_defined() const
11854 return this->named_object()->is_type();
11857 // Add a method. This is used when methods are defined before the
11861 Forward_declaration_type::add_method(const std::string
& name
,
11862 Function
* function
)
11864 Named_object
* no
= this->named_object();
11865 if (no
->is_unknown())
11866 no
->declare_as_type();
11867 return no
->type_declaration_value()->add_method(name
, function
);
11870 // Add a method declaration. This is used when methods are declared
11871 // before the type.
11874 Forward_declaration_type::add_method_declaration(const std::string
& name
,
11876 Function_type
* type
,
11879 Named_object
* no
= this->named_object();
11880 if (no
->is_unknown())
11881 no
->declare_as_type();
11882 Type_declaration
* td
= no
->type_declaration_value();
11883 return td
->add_method_declaration(name
, package
, type
, location
);
11886 // Add an already created object as a method.
11889 Forward_declaration_type::add_existing_method(Named_object
* nom
)
11891 Named_object
* no
= this->named_object();
11892 if (no
->is_unknown())
11893 no
->declare_as_type();
11894 no
->type_declaration_value()->add_existing_method(nom
);
11900 Forward_declaration_type::do_traverse(Traverse
* traverse
)
11902 if (this->is_defined()
11903 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
11904 return TRAVERSE_EXIT
;
11905 return TRAVERSE_CONTINUE
;
11908 // Verify the type.
11911 Forward_declaration_type::do_verify()
11913 if (!this->is_defined() && !this->is_nil_constant_as_type())
11921 // Get the backend representation for the type.
11924 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
11926 if (this->is_defined())
11927 return Type::get_named_base_btype(gogo
, this->real_type());
11930 return gogo
->backend()->error_type();
11932 // We represent an undefined type as a struct with no fields. That
11933 // should work fine for the backend, since the same case can arise
11935 std::vector
<Backend::Btyped_identifier
> fields
;
11936 Btype
* bt
= gogo
->backend()->struct_type(fields
);
11937 return gogo
->backend()->named_type(this->name(), bt
,
11938 this->named_object()->location());
11941 // Build a type descriptor for a forwarded type.
11944 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
11946 Location ploc
= Linemap::predeclared_location();
11947 if (!this->is_defined())
11948 return Expression::make_error(ploc
);
11951 Type
* t
= this->real_type();
11953 return this->named_type_descriptor(gogo
, t
, name
);
11955 return Expression::make_error(this->named_object_
->location());
11959 // The reflection string.
11962 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
11964 this->append_reflection(this->real_type(), gogo
, ret
);
11967 // Export a forward declaration. This can happen when a defined type
11968 // refers to a type which is only declared (and is presumably defined
11969 // in some other file in the same package).
11972 Forward_declaration_type::do_export(Export
*) const
11974 // If there is a base type, that should be exported instead of this.
11975 go_assert(!this->is_defined());
11977 // We don't output anything.
11980 // Make a forward declaration.
11983 Type::make_forward_declaration(Named_object
* named_object
)
11985 return new Forward_declaration_type(named_object
);
11988 // Class Typed_identifier_list.
11990 // Sort the entries by name.
11992 struct Typed_identifier_list_sort
11996 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
11998 return (Gogo::unpack_hidden_name(t1
.name())
11999 < Gogo::unpack_hidden_name(t2
.name()));
12004 Typed_identifier_list::sort_by_name()
12006 std::sort(this->entries_
.begin(), this->entries_
.end(),
12007 Typed_identifier_list_sort());
12013 Typed_identifier_list::traverse(Traverse
* traverse
)
12015 for (Typed_identifier_list::const_iterator p
= this->begin();
12019 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
12020 return TRAVERSE_EXIT
;
12022 return TRAVERSE_CONTINUE
;
12027 Typed_identifier_list
*
12028 Typed_identifier_list::copy() const
12030 Typed_identifier_list
* ret
= new Typed_identifier_list();
12031 for (Typed_identifier_list::const_iterator p
= this->begin();
12034 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));