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
, Struct_type
* type
, bool use_placeholder
,
28 std::vector
<Backend::Btyped_identifier
>* bfields
);
31 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
32 std::vector
<Backend::Btyped_identifier
>* bfields
);
35 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
37 std::vector
<Backend::Btyped_identifier
>* bfields
);
41 Type::Type(Type_classification classification
)
42 : classification_(classification
), btype_(NULL
), type_descriptor_var_(NULL
),
51 // Get the base type for a type--skip names and forward declarations.
56 switch (this->classification_
)
59 return this->named_type()->named_base();
61 return this->forward_declaration_type()->real_type()->base();
70 switch (this->classification_
)
73 return this->named_type()->named_base();
75 return this->forward_declaration_type()->real_type()->base();
81 // Skip defined forward declarations.
87 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
88 while (ftype
!= NULL
&& ftype
->is_defined())
90 t
= ftype
->real_type();
91 ftype
= t
->forward_declaration_type();
97 Type::forwarded() const
100 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
101 while (ftype
!= NULL
&& ftype
->is_defined())
103 t
= ftype
->real_type();
104 ftype
= t
->forward_declaration_type();
109 // Skip alias definitions.
114 Type
* t
= this->forwarded();
115 Named_type
* nt
= t
->named_type();
116 while (nt
!= NULL
&& nt
->is_alias())
118 t
= nt
->real_type()->forwarded();
119 nt
= t
->named_type();
125 Type::unalias() const
127 const Type
* t
= this->forwarded();
128 const Named_type
* nt
= t
->named_type();
129 while (nt
!= NULL
&& nt
->is_alias())
131 t
= nt
->real_type()->forwarded();
132 nt
= t
->named_type();
137 // If this is a named type, return it. Otherwise, return NULL.
142 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
146 Type::named_type() const
148 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
151 // Return true if this type is not defined.
154 Type::is_undefined() const
156 return this->forwarded()->forward_declaration_type() != NULL
;
159 // Return true if this is a basic type: a type which is not composed
160 // of other types, and is not void.
163 Type::is_basic_type() const
165 switch (this->classification_
)
188 return this->base()->is_basic_type();
195 // Return true if this is an abstract type.
198 Type::is_abstract() const
200 switch (this->classification())
203 return this->integer_type()->is_abstract();
205 return this->float_type()->is_abstract();
207 return this->complex_type()->is_abstract();
209 return this->is_abstract_string_type();
211 return this->is_abstract_boolean_type();
217 // Return a non-abstract version of an abstract type.
220 Type::make_non_abstract_type()
222 go_assert(this->is_abstract());
223 switch (this->classification())
226 if (this->integer_type()->is_rune())
227 return Type::lookup_integer_type("int32");
229 return Type::lookup_integer_type("int");
231 return Type::lookup_float_type("float64");
233 return Type::lookup_complex_type("complex128");
235 return Type::lookup_string_type();
237 return Type::lookup_bool_type();
243 // Return true if this is an error type. Don't give an error if we
244 // try to dereference an undefined forwarding type, as this is called
245 // in the parser when the type may legitimately be undefined.
248 Type::is_error_type() const
250 const Type
* t
= this->forwarded();
251 // Note that we return false for an undefined forward type.
252 switch (t
->classification_
)
257 return t
->named_type()->is_named_error_type();
263 // If this is a pointer type, return the type to which it points.
264 // Otherwise, return NULL.
267 Type::points_to() const
269 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
271 return ptype
== NULL
? NULL
: ptype
->points_to();
274 // Return whether this is a slice type.
277 Type::is_slice_type() const
279 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
282 // Return whether this is the predeclared constant nil being used as a
286 Type::is_nil_constant_as_type() const
288 const Type
* t
= this->forwarded();
289 if (t
->forward_declaration_type() != NULL
)
291 const Named_object
* no
= t
->forward_declaration_type()->named_object();
292 if (no
->is_unknown())
293 no
= no
->unknown_value()->real_named_object();
296 && no
->const_value()->expr()->is_nil_expression())
305 Type::traverse(Type
* type
, Traverse
* traverse
)
307 go_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
308 || (traverse
->traverse_mask()
309 & Traverse::traverse_expressions
) != 0);
310 if (traverse
->remember_type(type
))
312 // We have already traversed this type.
313 return TRAVERSE_CONTINUE
;
315 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
317 int t
= traverse
->type(type
);
318 if (t
== TRAVERSE_EXIT
)
319 return TRAVERSE_EXIT
;
320 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
321 return TRAVERSE_CONTINUE
;
323 // An array type has an expression which we need to traverse if
324 // traverse_expressions is set.
325 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
326 return TRAVERSE_EXIT
;
327 return TRAVERSE_CONTINUE
;
330 // Default implementation for do_traverse for child class.
333 Type::do_traverse(Traverse
*)
335 return TRAVERSE_CONTINUE
;
338 // Return whether two types are identical. If REASON is not NULL,
339 // optionally set *REASON to the reason the types are not identical.
342 Type::are_identical(const Type
* t1
, const Type
* t2
, int flags
,
345 if (t1
== NULL
|| t2
== NULL
)
347 // Something is wrong.
348 return (flags
& COMPARE_ERRORS
) == 0 ? true : t1
== t2
;
351 // Skip defined forward declarations.
352 t1
= t1
->forwarded();
353 t2
= t2
->forwarded();
355 if ((flags
& COMPARE_ALIASES
) == 0)
365 // An undefined forward declaration is an error.
366 if (t1
->forward_declaration_type() != NULL
367 || t2
->forward_declaration_type() != NULL
)
368 return (flags
& COMPARE_ERRORS
) == 0;
370 // Avoid cascading errors with error types.
371 if (t1
->is_error_type() || t2
->is_error_type())
373 if ((flags
& COMPARE_ERRORS
) == 0)
375 return t1
->is_error_type() && t2
->is_error_type();
378 // Get a good reason for the sink type. Note that the sink type on
379 // the left hand side of an assignment is handled in are_assignable.
380 if (t1
->is_sink_type() || t2
->is_sink_type())
383 *reason
= "invalid use of _";
387 // A named type is only identical to itself.
388 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
391 // Check type shapes.
392 if (t1
->classification() != t2
->classification())
395 switch (t1
->classification())
401 // These types are always identical.
405 return t1
->integer_type()->is_identical(t2
->integer_type());
408 return t1
->float_type()->is_identical(t2
->float_type());
411 return t1
->complex_type()->is_identical(t2
->complex_type());
414 return t1
->function_type()->is_identical(t2
->function_type(),
415 false, flags
, reason
);
418 return Type::are_identical(t1
->points_to(), t2
->points_to(), flags
,
422 return t1
->struct_type()->is_identical(t2
->struct_type(), flags
);
425 return t1
->array_type()->is_identical(t2
->array_type(), flags
);
428 return t1
->map_type()->is_identical(t2
->map_type(), flags
);
431 return t1
->channel_type()->is_identical(t2
->channel_type(), flags
);
434 return t1
->interface_type()->is_identical(t2
->interface_type(), flags
);
436 case TYPE_CALL_MULTIPLE_RESULT
:
438 *reason
= "invalid use of multiple-value function call";
446 // Return true if it's OK to have a binary operation with types LHS
447 // and RHS. This is not used for shifts or comparisons.
450 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
452 if (Type::are_identical(lhs
, rhs
, Type::COMPARE_TAGS
, NULL
))
455 // A constant of abstract bool type may be mixed with any bool type.
456 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
457 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
460 // A constant of abstract string type may be mixed with any string
462 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
463 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
469 // A constant of abstract integer, float, or complex type may be
470 // mixed with an integer, float, or complex type.
471 if ((rhs
->is_abstract()
472 && (rhs
->integer_type() != NULL
473 || rhs
->float_type() != NULL
474 || rhs
->complex_type() != NULL
)
475 && (lhs
->integer_type() != NULL
476 || lhs
->float_type() != NULL
477 || lhs
->complex_type() != NULL
))
478 || (lhs
->is_abstract()
479 && (lhs
->integer_type() != NULL
480 || lhs
->float_type() != NULL
481 || lhs
->complex_type() != NULL
)
482 && (rhs
->integer_type() != NULL
483 || rhs
->float_type() != NULL
484 || rhs
->complex_type() != NULL
)))
487 // The nil type may be compared to a pointer, an interface type, a
488 // slice type, a channel type, a map type, or a function type.
489 if (lhs
->is_nil_type()
490 && (rhs
->points_to() != NULL
491 || rhs
->interface_type() != NULL
492 || rhs
->is_slice_type()
493 || rhs
->map_type() != NULL
494 || rhs
->channel_type() != NULL
495 || rhs
->function_type() != NULL
))
497 if (rhs
->is_nil_type()
498 && (lhs
->points_to() != NULL
499 || lhs
->interface_type() != NULL
500 || lhs
->is_slice_type()
501 || lhs
->map_type() != NULL
502 || lhs
->channel_type() != NULL
503 || lhs
->function_type() != NULL
))
509 // Return true if a value with type T1 may be compared with a value of
510 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
513 Type::are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
514 const Type
*t2
, std::string
*reason
)
517 && !Type::are_assignable(t1
, t2
, NULL
)
518 && !Type::are_assignable(t2
, t1
, NULL
))
521 *reason
= "incompatible types in binary expression";
527 if (t1
->integer_type() == NULL
528 && t1
->float_type() == NULL
529 && !t1
->is_string_type())
532 *reason
= _("invalid comparison of non-ordered type");
536 else if (t1
->is_slice_type()
537 || t1
->map_type() != NULL
538 || t1
->function_type() != NULL
539 || t2
->is_slice_type()
540 || t2
->map_type() != NULL
541 || t2
->function_type() != NULL
)
543 if (!t1
->is_nil_type() && !t2
->is_nil_type())
547 if (t1
->is_slice_type() || t2
->is_slice_type())
548 *reason
= _("slice can only be compared to nil");
549 else if (t1
->map_type() != NULL
|| t2
->map_type() != NULL
)
550 *reason
= _("map can only be compared to nil");
552 *reason
= _("func can only be compared to nil");
554 // Match 6g error messages.
555 if (t1
->interface_type() != NULL
|| t2
->interface_type() != NULL
)
558 snprintf(buf
, sizeof buf
, _("invalid operation (%s)"),
568 if (!t1
->is_boolean_type()
569 && t1
->integer_type() == NULL
570 && t1
->float_type() == NULL
571 && t1
->complex_type() == NULL
572 && !t1
->is_string_type()
573 && t1
->points_to() == NULL
574 && t1
->channel_type() == NULL
575 && t1
->interface_type() == NULL
576 && t1
->struct_type() == NULL
577 && t1
->array_type() == NULL
578 && !t1
->is_nil_type())
581 *reason
= _("invalid comparison of non-comparable type");
585 if (t1
->unalias()->named_type() != NULL
)
586 return t1
->unalias()->named_type()->named_type_is_comparable(reason
);
587 else if (t2
->unalias()->named_type() != NULL
)
588 return t2
->unalias()->named_type()->named_type_is_comparable(reason
);
589 else if (t1
->struct_type() != NULL
)
591 if (t1
->struct_type()->is_struct_incomparable())
594 *reason
= _("invalid comparison of generated struct");
597 const Struct_field_list
* fields
= t1
->struct_type()->fields();
598 for (Struct_field_list::const_iterator p
= fields
->begin();
602 if (!p
->type()->is_comparable())
605 *reason
= _("invalid comparison of non-comparable struct");
610 else if (t1
->array_type() != NULL
)
612 if (t1
->array_type()->is_array_incomparable())
615 *reason
= _("invalid comparison of generated array");
618 if (t1
->array_type()->length()->is_nil_expression()
619 || !t1
->array_type()->element_type()->is_comparable())
622 *reason
= _("invalid comparison of non-comparable array");
631 // Return true if a value with type RHS may be assigned to a variable
632 // with type LHS. If REASON is not NULL, set *REASON to the reason
633 // the types are not assignable.
636 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
638 // Do some checks first. Make sure the types are defined.
639 if (rhs
!= NULL
&& !rhs
->is_undefined())
641 if (rhs
->is_void_type())
644 *reason
= "non-value used as value";
647 if (rhs
->is_call_multiple_result_type())
650 reason
->assign(_("multiple-value function call in "
651 "single-value context"));
656 // Any value may be assigned to the blank identifier.
658 && !lhs
->is_undefined()
659 && lhs
->is_sink_type())
662 // Identical types are assignable.
663 if (Type::are_identical(lhs
, rhs
, Type::COMPARE_TAGS
, reason
))
666 // Ignore aliases, except for error messages.
667 const Type
* lhs_orig
= lhs
;
668 const Type
* rhs_orig
= rhs
;
669 lhs
= lhs
->unalias();
670 rhs
= rhs
->unalias();
672 // The types are assignable if they have identical underlying types
673 // and either LHS or RHS is not a named type.
674 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
675 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
676 && Type::are_identical(lhs
->base(), rhs
->base(), Type::COMPARE_TAGS
,
680 // The types are assignable if LHS is an interface type and RHS
681 // implements the required methods.
682 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
683 if (lhs_interface_type
!= NULL
)
685 if (lhs_interface_type
->implements_interface(rhs
, reason
))
687 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
688 if (rhs_interface_type
!= NULL
689 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
694 // The type are assignable if RHS is a bidirectional channel type,
695 // LHS is a channel type, they have identical element types, and
696 // either LHS or RHS is not a named type.
697 if (lhs
->channel_type() != NULL
698 && rhs
->channel_type() != NULL
699 && rhs
->channel_type()->may_send()
700 && rhs
->channel_type()->may_receive()
701 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
702 && Type::are_identical(lhs
->channel_type()->element_type(),
703 rhs
->channel_type()->element_type(),
708 // The nil type may be assigned to a pointer, function, slice, map,
709 // channel, or interface type.
710 if (rhs
->is_nil_type()
711 && (lhs
->points_to() != NULL
712 || lhs
->function_type() != NULL
713 || lhs
->is_slice_type()
714 || lhs
->map_type() != NULL
715 || lhs
->channel_type() != NULL
716 || lhs
->interface_type() != NULL
))
719 // An untyped numeric constant may be assigned to a numeric type if
720 // it is representable in that type.
721 if ((rhs
->is_abstract()
722 && (rhs
->integer_type() != NULL
723 || rhs
->float_type() != NULL
724 || rhs
->complex_type() != NULL
))
725 && (lhs
->integer_type() != NULL
726 || lhs
->float_type() != NULL
727 || lhs
->complex_type() != NULL
))
730 // Give some better error messages.
731 if (reason
!= NULL
&& reason
->empty())
733 if (rhs
->interface_type() != NULL
)
734 reason
->assign(_("need explicit conversion"));
735 else if (lhs_orig
->named_type() != NULL
736 && rhs_orig
->named_type() != NULL
)
738 size_t len
= (lhs_orig
->named_type()->name().length()
739 + rhs_orig
->named_type()->name().length()
741 char* buf
= new char[len
];
742 snprintf(buf
, len
, _("cannot use type %s as type %s"),
743 rhs_orig
->named_type()->message_name().c_str(),
744 lhs_orig
->named_type()->message_name().c_str());
753 // Return true if a value with type RHS may be converted to type LHS.
754 // If REASON is not NULL, set *REASON to the reason the types are not
758 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
760 // The types are convertible if they are assignable.
761 if (Type::are_assignable(lhs
, rhs
, reason
))
765 lhs
= lhs
->unalias();
766 rhs
= rhs
->unalias();
768 // A pointer to a regular type may not be converted to a pointer to
769 // a type that may not live in the heap, except when converting from
771 if (lhs
->points_to() != NULL
772 && rhs
->points_to() != NULL
773 && !lhs
->points_to()->in_heap()
774 && rhs
->points_to()->in_heap()
775 && !rhs
->is_unsafe_pointer_type())
778 reason
->assign(_("conversion from normal type to notinheap type"));
782 // The types are convertible if they have identical underlying
783 // types, ignoring struct field tags.
784 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
785 && Type::are_identical(lhs
->base(), rhs
->base(), 0, reason
))
788 // The types are convertible if they are both unnamed pointer types
789 // and their pointer base types have identical underlying types,
790 // ignoring struct field tags.
791 if (lhs
->named_type() == NULL
792 && rhs
->named_type() == NULL
793 && lhs
->points_to() != NULL
794 && rhs
->points_to() != NULL
795 && (lhs
->points_to()->named_type() != NULL
796 || rhs
->points_to()->named_type() != NULL
)
797 && Type::are_identical(lhs
->points_to()->base(),
798 rhs
->points_to()->base(),
802 // Integer and floating point types are convertible to each other.
803 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
804 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
807 // Complex types are convertible to each other.
808 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
811 // An integer, or []byte, or []rune, may be converted to a string.
812 if (lhs
->is_string_type())
814 if (rhs
->integer_type() != NULL
)
816 if (rhs
->is_slice_type())
818 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
819 if (e
->integer_type() != NULL
820 && (e
->integer_type()->is_byte()
821 || e
->integer_type()->is_rune()))
826 // A string may be converted to []byte or []rune.
827 if (rhs
->is_string_type() && lhs
->is_slice_type())
829 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
830 if (e
->integer_type() != NULL
831 && (e
->integer_type()->is_byte() || e
->integer_type()->is_rune()))
835 // An unsafe.Pointer type may be converted to any pointer type or to
836 // a type whose underlying type is uintptr, and vice-versa.
837 if (lhs
->is_unsafe_pointer_type()
838 && (rhs
->points_to() != NULL
839 || (rhs
->integer_type() != NULL
840 && rhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
842 if (rhs
->is_unsafe_pointer_type()
843 && (lhs
->points_to() != NULL
844 || (lhs
->integer_type() != NULL
845 && lhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
848 // Give a better error message.
852 *reason
= "invalid type conversion";
855 std::string s
= "invalid type conversion (";
865 // Copy expressions if it may change the size.
867 // The only type that has an expression is an array type. The only
868 // types whose size can be changed by the size of an array type are an
869 // array type itself, or a struct type with an array field.
871 Type::copy_expressions()
873 // This is run during parsing, so types may not be valid yet.
874 // We only have to worry about array type literals.
875 switch (this->classification_
)
882 Array_type
* at
= this->array_type();
883 if (at
->length() == NULL
)
885 Expression
* len
= at
->length()->copy();
886 if (at
->length() == len
)
888 return Type::make_array_type(at
->element_type(), len
);
893 Struct_type
* st
= this->struct_type();
894 const Struct_field_list
* sfl
= st
->fields();
897 bool changed
= false;
898 Struct_field_list
*nsfl
= new Struct_field_list();
899 for (Struct_field_list::const_iterator pf
= sfl
->begin();
903 Type
* ft
= pf
->type()->copy_expressions();
904 Struct_field
nf(Typed_identifier((pf
->is_anonymous()
910 nf
.set_tag(pf
->tag());
912 if (ft
!= pf
->type())
920 return Type::make_struct_type(nsfl
, st
->location());
927 // Return a hash code for the type to be used for method lookup.
930 Type::hash_for_method(Gogo
* gogo
, int flags
) const
932 const Type
* t
= this->forwarded();
933 if (t
->named_type() != NULL
&& t
->named_type()->is_alias())
936 t
->named_type()->real_type()->hash_for_method(gogo
, flags
);
937 if ((flags
& Type::COMPARE_ALIASES
) != 0)
941 unsigned int ret
= t
->classification_
;
942 return ret
+ t
->do_hash_for_method(gogo
, flags
);
945 // Default implementation of do_hash_for_method. This is appropriate
946 // for types with no subfields.
949 Type::do_hash_for_method(Gogo
*, int) const
954 // A hash table mapping unnamed types to the backend representation of
957 Type::Type_btypes
Type::type_btypes
;
959 // Return the backend representation for this type.
962 Type::get_backend(Gogo
* gogo
)
964 if (this->btype_
!= NULL
)
967 if (this->named_type() != NULL
&& this->named_type()->is_alias()) {
968 Btype
* bt
= this->unalias()->get_backend(gogo
);
969 if (gogo
!= NULL
&& gogo
->named_types_are_converted())
974 if (this->forward_declaration_type() != NULL
975 || this->named_type() != NULL
)
976 return this->get_btype_without_hash(gogo
);
978 if (this->is_error_type())
979 return gogo
->backend()->error_type();
981 // To avoid confusing the backend, translate all identical Go types
982 // to the same backend representation. We use a hash table to do
983 // that. There is no need to use the hash table for named types, as
984 // named types are only identical to themselves.
986 std::pair
<Type
*, Type_btype_entry
> val
;
988 val
.second
.btype
= NULL
;
989 val
.second
.is_placeholder
= false;
990 std::pair
<Type_btypes::iterator
, bool> ins
=
991 Type::type_btypes
.insert(val
);
992 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
994 // Note that GOGO can be NULL here, but only when the GCC
995 // middle-end is asking for a frontend type. That will only
996 // happen for simple types, which should never require
998 if (!ins
.first
->second
.is_placeholder
)
999 this->btype_
= ins
.first
->second
.btype
;
1000 else if (gogo
->named_types_are_converted())
1002 this->finish_backend(gogo
, ins
.first
->second
.btype
);
1003 ins
.first
->second
.is_placeholder
= false;
1006 // We set the has_padding field of a Struct_type when we convert
1007 // to the backend type, so if we have multiple Struct_type's
1008 // mapping to the same backend type we need to copy the
1009 // has_padding field. FIXME: This is awkward. We shouldn't
1010 // really change the type when setting the backend type, but
1011 // there isn't any other good time to add the padding field.
1012 if (ins
.first
->first
->struct_type() != NULL
1013 && ins
.first
->first
->struct_type()->has_padding())
1014 this->struct_type()->set_has_padding();
1016 return ins
.first
->second
.btype
;
1019 Btype
* bt
= this->get_btype_without_hash(gogo
);
1021 if (ins
.first
->second
.btype
== NULL
)
1023 ins
.first
->second
.btype
= bt
;
1024 ins
.first
->second
.is_placeholder
= false;
1028 // We have already created a backend representation for this
1029 // type. This can happen when an unnamed type is defined using
1030 // a named type which in turns uses an identical unnamed type.
1031 // Use the representation we created earlier and ignore the one we just
1033 if (this->btype_
== bt
)
1034 this->btype_
= ins
.first
->second
.btype
;
1035 bt
= ins
.first
->second
.btype
;
1041 // Return the backend representation for a type without looking in the
1042 // hash table for identical types. This is used for named types,
1043 // since a named type is never identical to any other type.
1046 Type::get_btype_without_hash(Gogo
* gogo
)
1048 if (this->btype_
== NULL
)
1050 Btype
* bt
= this->do_get_backend(gogo
);
1052 // For a recursive function or pointer type, we will temporarily
1053 // return a circular pointer type during the recursion. We
1054 // don't want to record that for a forwarding type, as it may
1055 // confuse us later.
1056 if (this->forward_declaration_type() != NULL
1057 && gogo
->backend()->is_circular_pointer_type(bt
))
1060 if (gogo
== NULL
|| !gogo
->named_types_are_converted())
1065 return this->btype_
;
1068 // Get the backend representation of a type without forcing the
1069 // creation of the backend representation of all supporting types.
1070 // This will return a backend type that has the correct size but may
1071 // be incomplete. E.g., a pointer will just be a placeholder pointer,
1072 // and will not contain the final representation of the type to which
1073 // it points. This is used while converting all named types to the
1074 // backend representation, to avoid problems with indirect references
1075 // to types which are not yet complete. When this is called, the
1076 // sizes of all direct references (e.g., a struct field) should be
1077 // known, but the sizes of indirect references (e.g., the type to
1078 // which a pointer points) may not.
1081 Type::get_backend_placeholder(Gogo
* gogo
)
1083 if (gogo
->named_types_are_converted())
1084 return this->get_backend(gogo
);
1085 if (this->btype_
!= NULL
)
1086 return this->btype_
;
1089 switch (this->classification_
)
1099 // These are simple types that can just be created directly.
1100 return this->get_backend(gogo
);
1104 // All maps and channels have the same backend representation.
1105 return this->get_backend(gogo
);
1109 // Named types keep track of their own dependencies and manage
1110 // their own placeholders.
1111 if (this->named_type() != NULL
&& this->named_type()->is_alias())
1112 return this->unalias()->get_backend_placeholder(gogo
);
1113 return this->get_backend(gogo
);
1115 case TYPE_INTERFACE
:
1116 if (this->interface_type()->is_empty())
1117 return Interface_type::get_backend_empty_interface_type(gogo
);
1124 std::pair
<Type
*, Type_btype_entry
> val
;
1126 val
.second
.btype
= NULL
;
1127 val
.second
.is_placeholder
= false;
1128 std::pair
<Type_btypes::iterator
, bool> ins
=
1129 Type::type_btypes
.insert(val
);
1130 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
1131 return ins
.first
->second
.btype
;
1133 switch (this->classification_
)
1137 // A Go function type is a pointer to a struct type.
1138 Location loc
= this->function_type()->location();
1139 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1145 Location loc
= Linemap::unknown_location();
1146 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1147 Pointer_type
* pt
= this->convert
<Pointer_type
, TYPE_POINTER
>();
1148 Type::placeholder_pointers
.push_back(pt
);
1153 // We don't have to make the struct itself be a placeholder. We
1154 // are promised that we know the sizes of the struct fields.
1155 // But we may have to use a placeholder for any particular
1158 std::vector
<Backend::Btyped_identifier
> bfields
;
1159 get_backend_struct_fields(gogo
, this->struct_type(), true, &bfields
);
1160 bt
= gogo
->backend()->struct_type(bfields
);
1165 if (this->is_slice_type())
1167 std::vector
<Backend::Btyped_identifier
> bfields
;
1168 get_backend_slice_fields(gogo
, this->array_type(), true, &bfields
);
1169 bt
= gogo
->backend()->struct_type(bfields
);
1173 Btype
* element
= this->array_type()->get_backend_element(gogo
, true);
1174 Bexpression
* len
= this->array_type()->get_backend_length(gogo
);
1175 bt
= gogo
->backend()->array_type(element
, len
);
1179 case TYPE_INTERFACE
:
1181 go_assert(!this->interface_type()->is_empty());
1182 std::vector
<Backend::Btyped_identifier
> bfields
;
1183 get_backend_interface_fields(gogo
, this->interface_type(), true,
1185 bt
= gogo
->backend()->struct_type(bfields
);
1190 case TYPE_CALL_MULTIPLE_RESULT
:
1191 /* Note that various classifications were handled in the earlier
1197 if (ins
.first
->second
.btype
== NULL
)
1199 ins
.first
->second
.btype
= bt
;
1200 ins
.first
->second
.is_placeholder
= true;
1204 // A placeholder for this type got created along the way. Use
1205 // that one and ignore the one we just built.
1206 bt
= ins
.first
->second
.btype
;
1212 // Complete the backend representation. This is called for a type
1213 // using a placeholder type.
1216 Type::finish_backend(Gogo
* gogo
, Btype
*placeholder
)
1218 switch (this->classification_
)
1232 Btype
* bt
= this->do_get_backend(gogo
);
1233 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1234 go_assert(saw_errors());
1240 Btype
* bt
= this->do_get_backend(gogo
);
1241 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1242 go_assert(saw_errors());
1247 // The struct type itself is done, but we have to make sure that
1248 // all the field types are converted.
1249 this->struct_type()->finish_backend_fields(gogo
);
1253 // The array type itself is done, but make sure the element type
1255 this->array_type()->finish_backend_element(gogo
);
1262 case TYPE_INTERFACE
:
1263 // The interface type itself is done, but make sure the method
1264 // types are converted.
1265 this->interface_type()->finish_backend_methods(gogo
);
1273 case TYPE_CALL_MULTIPLE_RESULT
:
1278 this->btype_
= placeholder
;
1281 // Return a pointer to the type descriptor for this type.
1284 Type::type_descriptor_pointer(Gogo
* gogo
, Location location
)
1286 Type
* t
= this->unalias();
1287 if (t
->type_descriptor_var_
== NULL
)
1289 t
->make_type_descriptor_var(gogo
);
1290 go_assert(t
->type_descriptor_var_
!= NULL
);
1292 Bexpression
* var_expr
=
1293 gogo
->backend()->var_expression(t
->type_descriptor_var_
, location
);
1294 Bexpression
* var_addr
=
1295 gogo
->backend()->address_expression(var_expr
, location
);
1296 Type
* td_type
= Type::make_type_descriptor_type();
1297 Btype
* td_btype
= td_type
->get_backend(gogo
);
1298 Btype
* ptd_btype
= gogo
->backend()->pointer_type(td_btype
);
1299 return gogo
->backend()->convert_expression(ptd_btype
, var_addr
, location
);
1302 // A mapping from unnamed types to type descriptor variables.
1304 Type::Type_descriptor_vars
Type::type_descriptor_vars
;
1306 // Build the type descriptor for this type.
1309 Type::make_type_descriptor_var(Gogo
* gogo
)
1311 go_assert(this->type_descriptor_var_
== NULL
);
1313 Named_type
* nt
= this->named_type();
1315 // We can have multiple instances of unnamed types, but we only want
1316 // to emit the type descriptor once. We use a hash table. This is
1317 // not necessary for named types, as they are unique, and we store
1318 // the type descriptor in the type itself.
1319 Bvariable
** phash
= NULL
;
1322 Bvariable
* bvnull
= NULL
;
1323 std::pair
<Type_descriptor_vars::iterator
, bool> ins
=
1324 Type::type_descriptor_vars
.insert(std::make_pair(this, bvnull
));
1327 // We've already built a type descriptor for this type.
1328 this->type_descriptor_var_
= ins
.first
->second
;
1331 phash
= &ins
.first
->second
;
1334 // The type descriptor symbol for the unsafe.Pointer type is defined in
1335 // libgo/go-unsafe-pointer.c, so we just return a reference to that
1336 // symbol if necessary.
1337 if (this->is_unsafe_pointer_type())
1339 Location bloc
= Linemap::predeclared_location();
1341 Type
* td_type
= Type::make_type_descriptor_type();
1342 Btype
* td_btype
= td_type
->get_backend(gogo
);
1343 std::string name
= gogo
->type_descriptor_name(this, nt
);
1344 std::string
asm_name(go_selectively_encode_id(name
));
1345 this->type_descriptor_var_
=
1346 gogo
->backend()->immutable_struct_reference(name
, asm_name
,
1351 *phash
= this->type_descriptor_var_
;
1355 std::string var_name
= gogo
->type_descriptor_name(this, nt
);
1357 // Build the contents of the type descriptor.
1358 Expression
* initializer
= this->do_type_descriptor(gogo
, NULL
);
1360 Btype
* initializer_btype
= initializer
->type()->get_backend(gogo
);
1362 Location loc
= nt
== NULL
? Linemap::predeclared_location() : nt
->location();
1364 const Package
* dummy
;
1365 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
1367 std::string
asm_name(go_selectively_encode_id(var_name
));
1368 this->type_descriptor_var_
=
1369 gogo
->backend()->immutable_struct_reference(var_name
, asm_name
,
1373 *phash
= this->type_descriptor_var_
;
1377 // See if this type descriptor can appear in multiple packages.
1378 bool is_common
= false;
1381 // We create the descriptor for a builtin type whenever we need
1383 is_common
= nt
->is_builtin();
1387 // This is an unnamed type. The descriptor could be defined in
1388 // any package where it is needed, and the linker will pick one
1389 // descriptor to keep.
1393 // We are going to build the type descriptor in this package. We
1394 // must create the variable before we convert the initializer to the
1395 // backend representation, because the initializer may refer to the
1396 // type descriptor of this type. By setting type_descriptor_var_ we
1397 // ensure that type_descriptor_pointer will work if called while
1398 // converting INITIALIZER.
1400 std::string
asm_name(go_selectively_encode_id(var_name
));
1401 this->type_descriptor_var_
=
1402 gogo
->backend()->immutable_struct(var_name
, asm_name
, false, is_common
,
1403 initializer_btype
, loc
);
1405 *phash
= this->type_descriptor_var_
;
1407 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
1408 context
.set_is_const();
1409 Bexpression
* binitializer
= initializer
->get_backend(&context
);
1411 gogo
->backend()->immutable_struct_set_init(this->type_descriptor_var_
,
1412 var_name
, false, is_common
,
1413 initializer_btype
, loc
,
1417 // Return true if this type descriptor is defined in a different
1418 // package. If this returns true it sets *PACKAGE to the package.
1421 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1422 const Package
** package
)
1426 if (nt
->named_object()->package() != NULL
)
1428 // This is a named type defined in a different package. The
1429 // type descriptor should be defined in that package.
1430 *package
= nt
->named_object()->package();
1436 if (this->points_to() != NULL
1437 && this->points_to()->named_type() != NULL
1438 && this->points_to()->named_type()->named_object()->package() != NULL
)
1440 // This is an unnamed pointer to a named type defined in a
1441 // different package. The descriptor should be defined in
1443 *package
= this->points_to()->named_type()->named_object()->package();
1450 // Return a composite literal for a type descriptor.
1453 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1455 return type
->do_type_descriptor(gogo
, NULL
);
1458 // Return a composite literal for a type descriptor with a name.
1461 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1463 go_assert(name
!= NULL
&& type
->named_type() != name
);
1464 return type
->do_type_descriptor(gogo
, name
);
1467 // Make a builtin struct type from a list of fields. The fields are
1468 // pairs of a name and a type.
1471 Type::make_builtin_struct_type(int nfields
, ...)
1474 va_start(ap
, nfields
);
1476 Location bloc
= Linemap::predeclared_location();
1477 Struct_field_list
* sfl
= new Struct_field_list();
1478 for (int i
= 0; i
< nfields
; i
++)
1480 const char* field_name
= va_arg(ap
, const char *);
1481 Type
* type
= va_arg(ap
, Type
*);
1482 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1487 Struct_type
* ret
= Type::make_struct_type(sfl
, bloc
);
1488 ret
->set_is_struct_incomparable();
1492 // A list of builtin named types.
1494 std::vector
<Named_type
*> Type::named_builtin_types
;
1496 // Make a builtin named type.
1499 Type::make_builtin_named_type(const char* name
, Type
* type
)
1501 Location bloc
= Linemap::predeclared_location();
1502 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1503 Named_type
* ret
= no
->type_value();
1504 Type::named_builtin_types
.push_back(ret
);
1508 // Convert the named builtin types.
1511 Type::convert_builtin_named_types(Gogo
* gogo
)
1513 for (std::vector
<Named_type
*>::const_iterator p
=
1514 Type::named_builtin_types
.begin();
1515 p
!= Type::named_builtin_types
.end();
1518 bool r
= (*p
)->verify();
1520 (*p
)->convert(gogo
);
1524 // Return the type of a type descriptor. We should really tie this to
1525 // runtime.Type rather than copying it. This must match the struct "_type"
1526 // declared in libgo/go/runtime/type.go.
1529 Type::make_type_descriptor_type()
1534 Location bloc
= Linemap::predeclared_location();
1536 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1537 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
1538 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1539 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1540 Type
* string_type
= Type::lookup_string_type();
1541 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1543 // This is an unnamed version of unsafe.Pointer. Perhaps we
1544 // should use the named version instead, although that would
1545 // require us to create the unsafe package if it has not been
1546 // imported. It probably doesn't matter.
1547 Type
* void_type
= Type::make_void_type();
1548 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1550 Typed_identifier_list
*params
= new Typed_identifier_list();
1551 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
1552 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1554 Typed_identifier_list
* results
= new Typed_identifier_list();
1555 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1557 Type
* hash_fntype
= Type::make_function_type(NULL
, params
, results
,
1560 params
= new Typed_identifier_list();
1561 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
1562 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
1564 results
= new Typed_identifier_list();
1565 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1567 Type
* equal_fntype
= Type::make_function_type(NULL
, params
, results
,
1570 // Forward declaration for the type descriptor type.
1571 Named_object
* named_type_descriptor_type
=
1572 Named_object::make_type_declaration("_type", NULL
, bloc
);
1573 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1574 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1576 // The type of a method on a concrete type.
1577 Struct_type
* method_type
=
1578 Type::make_builtin_struct_type(5,
1579 "name", pointer_string_type
,
1580 "pkgPath", pointer_string_type
,
1581 "mtyp", pointer_type_descriptor_type
,
1582 "typ", pointer_type_descriptor_type
,
1583 "tfn", unsafe_pointer_type
);
1584 Named_type
* named_method_type
=
1585 Type::make_builtin_named_type("method", method_type
);
1587 // Information for types with a name or methods.
1588 Type
* slice_named_method_type
=
1589 Type::make_array_type(named_method_type
, NULL
);
1590 Struct_type
* uncommon_type
=
1591 Type::make_builtin_struct_type(3,
1592 "name", pointer_string_type
,
1593 "pkgPath", pointer_string_type
,
1594 "methods", slice_named_method_type
);
1595 Named_type
* named_uncommon_type
=
1596 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1598 Type
* pointer_uncommon_type
=
1599 Type::make_pointer_type(named_uncommon_type
);
1601 // The type descriptor type.
1603 Struct_type
* type_descriptor_type
=
1604 Type::make_builtin_struct_type(12,
1605 "size", uintptr_type
,
1606 "ptrdata", uintptr_type
,
1607 "hash", uint32_type
,
1609 "align", uint8_type
,
1610 "fieldAlign", uint8_type
,
1611 "hashfn", hash_fntype
,
1612 "equalfn", equal_fntype
,
1613 "gcdata", pointer_uint8_type
,
1614 "string", pointer_string_type
,
1615 "", pointer_uncommon_type
,
1617 pointer_type_descriptor_type
);
1619 Named_type
* named
= Type::make_builtin_named_type("_type",
1620 type_descriptor_type
);
1622 named_type_descriptor_type
->set_type_value(named
);
1630 // Make the type of a pointer to a type descriptor as represented in
1634 Type::make_type_descriptor_ptr_type()
1638 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1642 // Return the alignment required by the memequalN function. N is a
1643 // type size: 16, 32, 64, or 128. The memequalN functions are defined
1644 // in libgo/go/runtime/alg.go.
1647 Type::memequal_align(Gogo
* gogo
, int size
)
1662 // The code uses [2]int64, which must have the same alignment as
1670 Type
* t
= Type::lookup_integer_type(tn
);
1673 if (!t
->backend_type_align(gogo
, &ret
))
1678 // Return whether this type needs specially built type functions.
1679 // This returns true for types that are comparable and either can not
1680 // use an identity comparison, or are a non-standard size.
1683 Type::needs_specific_type_functions(Gogo
* gogo
)
1685 Named_type
* nt
= this->named_type();
1686 if (nt
!= NULL
&& nt
->is_alias())
1688 if (!this->is_comparable())
1690 if (!this->compare_is_identity(gogo
))
1693 // We create a few predeclared types for type descriptors; they are
1694 // really just for the backend and don't need hash or equality
1696 if (nt
!= NULL
&& Linemap::is_predeclared_location(nt
->location()))
1699 int64_t size
, align
;
1700 if (!this->backend_type_size(gogo
, &size
)
1701 || !this->backend_type_align(gogo
, &align
))
1703 go_assert(saw_errors());
1706 // This switch matches the one in Type::type_functions.
1712 return align
< Type::memequal_align(gogo
, 16);
1714 return align
< Type::memequal_align(gogo
, 32);
1716 return align
< Type::memequal_align(gogo
, 64);
1718 return align
< Type::memequal_align(gogo
, 128);
1724 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1725 // hash code for this type and which compare whether two values of
1726 // this type are equal. If NAME is not NULL it is the name of this
1727 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1728 // functions, for convenience; they may be NULL.
1731 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1732 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1733 Named_object
** equal_fn
)
1735 // If the unaliased type is not a named type, then the type does not
1736 // have a name after all.
1738 name
= name
->unalias()->named_type();
1740 if (!this->is_comparable())
1747 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1749 Location bloc
= Linemap::predeclared_location();
1751 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1752 Type
* void_type
= Type::make_void_type();
1753 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1755 if (hash_fntype
== NULL
)
1757 Typed_identifier_list
* params
= new Typed_identifier_list();
1758 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1760 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1762 Typed_identifier_list
* results
= new Typed_identifier_list();
1763 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1765 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1767 if (equal_fntype
== NULL
)
1769 Typed_identifier_list
* params
= new Typed_identifier_list();
1770 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1772 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1775 Typed_identifier_list
* results
= new Typed_identifier_list();
1776 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1779 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1783 const char* hash_fnname
;
1784 const char* equal_fnname
;
1785 if (this->compare_is_identity(gogo
))
1787 int64_t size
, align
;
1788 if (!this->backend_type_size(gogo
, &size
)
1789 || !this->backend_type_align(gogo
, &align
))
1791 go_assert(saw_errors());
1794 bool build_functions
= false;
1795 // This switch matches the one in Type::needs_specific_type_functions.
1796 // The alignment tests are because of the memequal functions,
1797 // which assume that the values are aligned as required for an
1798 // integer of that size.
1802 hash_fnname
= "runtime.memhash0";
1803 equal_fnname
= "runtime.memequal0";
1806 hash_fnname
= "runtime.memhash8";
1807 equal_fnname
= "runtime.memequal8";
1810 if (align
< Type::memequal_align(gogo
, 16))
1811 build_functions
= true;
1814 hash_fnname
= "runtime.memhash16";
1815 equal_fnname
= "runtime.memequal16";
1819 if (align
< Type::memequal_align(gogo
, 32))
1820 build_functions
= true;
1823 hash_fnname
= "runtime.memhash32";
1824 equal_fnname
= "runtime.memequal32";
1828 if (align
< Type::memequal_align(gogo
, 64))
1829 build_functions
= true;
1832 hash_fnname
= "runtime.memhash64";
1833 equal_fnname
= "runtime.memequal64";
1837 if (align
< Type::memequal_align(gogo
, 128))
1838 build_functions
= true;
1841 hash_fnname
= "runtime.memhash128";
1842 equal_fnname
= "runtime.memequal128";
1846 build_functions
= true;
1849 if (build_functions
)
1851 // We don't have a built-in function for a type of this size
1852 // and alignment. Build a function to use that calls the
1853 // generic hash/equality functions for identity, passing the size.
1854 this->specific_type_functions(gogo
, name
, size
, hash_fntype
,
1855 equal_fntype
, hash_fn
, equal_fn
);
1861 switch (this->base()->classification())
1863 case Type::TYPE_ERROR
:
1864 case Type::TYPE_VOID
:
1865 case Type::TYPE_NIL
:
1866 case Type::TYPE_FUNCTION
:
1867 case Type::TYPE_MAP
:
1868 // For these types is_comparable should have returned false.
1871 case Type::TYPE_BOOLEAN
:
1872 case Type::TYPE_INTEGER
:
1873 case Type::TYPE_POINTER
:
1874 case Type::TYPE_CHANNEL
:
1875 // For these types compare_is_identity should have returned true.
1878 case Type::TYPE_FLOAT
:
1879 switch (this->float_type()->bits())
1882 hash_fnname
= "runtime.f32hash";
1883 equal_fnname
= "runtime.f32equal";
1886 hash_fnname
= "runtime.f64hash";
1887 equal_fnname
= "runtime.f64equal";
1894 case Type::TYPE_COMPLEX
:
1895 switch (this->complex_type()->bits())
1898 hash_fnname
= "runtime.c64hash";
1899 equal_fnname
= "runtime.c64equal";
1902 hash_fnname
= "runtime.c128hash";
1903 equal_fnname
= "runtime.c128equal";
1910 case Type::TYPE_STRING
:
1911 hash_fnname
= "runtime.strhash";
1912 equal_fnname
= "runtime.strequal";
1915 case Type::TYPE_STRUCT
:
1917 // This is a struct which can not be compared using a
1918 // simple identity function. We need to build a function
1920 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1921 equal_fntype
, hash_fn
, equal_fn
);
1925 case Type::TYPE_ARRAY
:
1926 if (this->is_slice_type())
1928 // Type::is_compatible_for_comparison should have
1934 // This is an array which can not be compared using a
1935 // simple identity function. We need to build a
1936 // function for comparison.
1937 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1938 equal_fntype
, hash_fn
, equal_fn
);
1943 case Type::TYPE_INTERFACE
:
1944 if (this->interface_type()->is_empty())
1946 hash_fnname
= "runtime.nilinterhash";
1947 equal_fnname
= "runtime.nilinterequal";
1951 hash_fnname
= "runtime.interhash";
1952 equal_fnname
= "runtime.interequal";
1956 case Type::TYPE_NAMED
:
1957 case Type::TYPE_FORWARD
:
1966 Location bloc
= Linemap::predeclared_location();
1967 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1969 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1970 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1971 equal_fntype
, bloc
);
1972 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1975 // A hash table mapping types to the specific hash functions.
1977 Type::Type_functions
Type::type_functions_table
;
1979 // Handle a type function which is specific to a type: if SIZE == -1,
1980 // this is a struct or array that can not use an identity comparison.
1981 // Otherwise, it is a type that uses an identity comparison but is not
1982 // one of the standard supported sizes.
1985 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1986 Function_type
* hash_fntype
,
1987 Function_type
* equal_fntype
,
1988 Named_object
** hash_fn
,
1989 Named_object
** equal_fn
)
1991 Hash_equal_fn
fnull(NULL
, NULL
);
1992 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1993 std::pair
<Type_functions::iterator
, bool> ins
=
1994 Type::type_functions_table
.insert(val
);
1997 // We already have functions for this type
1998 *hash_fn
= ins
.first
->second
.first
;
1999 *equal_fn
= ins
.first
->second
.second
;
2003 std::string hash_name
;
2004 std::string equal_name
;
2005 gogo
->specific_type_function_names(this, name
, &hash_name
, &equal_name
);
2007 Location bloc
= Linemap::predeclared_location();
2009 const Package
* package
= NULL
;
2010 bool is_defined_elsewhere
=
2011 this->type_descriptor_defined_elsewhere(name
, &package
);
2012 if (is_defined_elsewhere
)
2014 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
2016 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
2017 equal_fntype
, bloc
);
2021 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
2022 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
2026 ins
.first
->second
.first
= *hash_fn
;
2027 ins
.first
->second
.second
= *equal_fn
;
2029 if (!is_defined_elsewhere
)
2031 if (gogo
->in_global_scope())
2032 this->write_specific_type_functions(gogo
, name
, size
, hash_name
,
2033 hash_fntype
, equal_name
,
2036 gogo
->queue_specific_type_function(this, name
, size
, hash_name
,
2037 hash_fntype
, equal_name
,
2042 // Write the hash and equality functions for a type which needs to be
2043 // written specially.
2046 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
2047 const std::string
& hash_name
,
2048 Function_type
* hash_fntype
,
2049 const std::string
& equal_name
,
2050 Function_type
* equal_fntype
)
2052 Location bloc
= Linemap::predeclared_location();
2054 if (gogo
->specific_type_functions_are_written())
2056 go_assert(saw_errors());
2060 go_assert(this->is_comparable());
2062 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
2064 hash_fn
->func_value()->set_is_type_specific_function();
2065 gogo
->start_block(bloc
);
2068 this->write_identity_hash(gogo
, size
);
2069 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2070 this->write_named_hash(gogo
, name
, hash_fntype
, equal_fntype
);
2071 else if (this->struct_type() != NULL
)
2072 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
2074 else if (this->array_type() != NULL
)
2075 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
2080 Block
* b
= gogo
->finish_block(bloc
);
2081 gogo
->add_block(b
, bloc
);
2082 gogo
->lower_block(hash_fn
, b
);
2083 gogo
->finish_function(bloc
);
2085 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
2087 equal_fn
->func_value()->set_is_type_specific_function();
2088 gogo
->start_block(bloc
);
2091 this->write_identity_equal(gogo
, size
);
2092 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2093 this->write_named_equal(gogo
, name
);
2094 else if (this->struct_type() != NULL
)
2095 this->struct_type()->write_equal_function(gogo
, name
);
2096 else if (this->array_type() != NULL
)
2097 this->array_type()->write_equal_function(gogo
, name
);
2101 b
= gogo
->finish_block(bloc
);
2102 gogo
->add_block(b
, bloc
);
2103 gogo
->lower_block(equal_fn
, b
);
2104 gogo
->finish_function(bloc
);
2106 // Build the function descriptors for the type descriptor to refer to.
2107 hash_fn
->func_value()->descriptor(gogo
, hash_fn
);
2108 equal_fn
->func_value()->descriptor(gogo
, equal_fn
);
2111 // Write a hash function for a type that can use an identity hash but
2112 // is not one of the standard supported sizes. For example, this
2113 // would be used for the type [3]byte. This builds a return statement
2114 // that returns a call to the memhash function, passing the key and
2115 // seed from the function arguments (already constructed before this
2116 // is called), and the constant size.
2119 Type::write_identity_hash(Gogo
* gogo
, int64_t size
)
2121 Location bloc
= Linemap::predeclared_location();
2123 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2124 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2126 Typed_identifier_list
* params
= new Typed_identifier_list();
2127 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
2128 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
2129 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2131 Typed_identifier_list
* results
= new Typed_identifier_list();
2132 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
2134 Function_type
* memhash_fntype
= Type::make_function_type(NULL
, params
,
2137 Named_object
* memhash
=
2138 Named_object::make_function_declaration("runtime.memhash", NULL
,
2139 memhash_fntype
, bloc
);
2140 memhash
->func_declaration_value()->set_asm_name("runtime.memhash");
2142 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2143 go_assert(key_arg
!= NULL
);
2144 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2145 go_assert(seed_arg
!= NULL
);
2147 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2148 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2149 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2151 Expression_list
* args
= new Expression_list();
2152 args
->push_back(key_ref
);
2153 args
->push_back(seed_ref
);
2154 args
->push_back(size_arg
);
2155 Expression
* func
= Expression::make_func_reference(memhash
, NULL
, bloc
);
2156 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2158 Expression_list
* vals
= new Expression_list();
2159 vals
->push_back(call
);
2160 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2161 gogo
->add_statement(s
);
2164 // Write an equality function for a type that can use an identity
2165 // equality comparison but is not one of the standard supported sizes.
2166 // For example, this would be used for the type [3]byte. This builds
2167 // a return statement that returns a call to the memequal function,
2168 // passing the two keys from the function arguments (already
2169 // constructed before this is called), and the constant size.
2172 Type::write_identity_equal(Gogo
* gogo
, int64_t size
)
2174 Location bloc
= Linemap::predeclared_location();
2176 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2177 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2179 Typed_identifier_list
* params
= new Typed_identifier_list();
2180 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
2181 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
2182 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2184 Typed_identifier_list
* results
= new Typed_identifier_list();
2185 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
2187 Function_type
* memequal_fntype
= Type::make_function_type(NULL
, params
,
2190 Named_object
* memequal
=
2191 Named_object::make_function_declaration("runtime.memequal", NULL
,
2192 memequal_fntype
, bloc
);
2193 memequal
->func_declaration_value()->set_asm_name("runtime.memequal");
2195 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2196 go_assert(key1_arg
!= NULL
);
2197 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2198 go_assert(key2_arg
!= NULL
);
2200 Expression
* key1_ref
= Expression::make_var_reference(key1_arg
, bloc
);
2201 Expression
* key2_ref
= Expression::make_var_reference(key2_arg
, bloc
);
2202 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2204 Expression_list
* args
= new Expression_list();
2205 args
->push_back(key1_ref
);
2206 args
->push_back(key2_ref
);
2207 args
->push_back(size_arg
);
2208 Expression
* func
= Expression::make_func_reference(memequal
, NULL
, bloc
);
2209 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2211 Expression_list
* vals
= new Expression_list();
2212 vals
->push_back(call
);
2213 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2214 gogo
->add_statement(s
);
2217 // Write a hash function that simply calls the hash function for a
2218 // named type. This is used when one named type is defined as
2219 // another. This ensures that this case works when the other named
2220 // type is defined in another package and relies on calling hash
2221 // functions defined only in that package.
2224 Type::write_named_hash(Gogo
* gogo
, Named_type
* name
,
2225 Function_type
* hash_fntype
, Function_type
* equal_fntype
)
2227 Location bloc
= Linemap::predeclared_location();
2229 Named_type
* base_type
= name
->real_type()->named_type();
2230 while (base_type
->is_alias())
2232 base_type
= base_type
->real_type()->named_type();
2233 go_assert(base_type
!= NULL
);
2235 go_assert(base_type
!= NULL
);
2237 // The pointer to the type we are going to hash. This is an
2239 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2240 go_assert(key_arg
!= NULL
);
2242 // The seed argument to the hash function.
2243 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2244 go_assert(seed_arg
!= NULL
);
2246 Named_object
* hash_fn
;
2247 Named_object
* equal_fn
;
2248 name
->real_type()->type_functions(gogo
, base_type
, hash_fntype
, equal_fntype
,
2249 &hash_fn
, &equal_fn
);
2251 // Call the hash function for the base type.
2252 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2253 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2254 Expression_list
* args
= new Expression_list();
2255 args
->push_back(key_ref
);
2256 args
->push_back(seed_ref
);
2257 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
2258 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2260 // Return the hash of the base type.
2261 Expression_list
* vals
= new Expression_list();
2262 vals
->push_back(call
);
2263 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2264 gogo
->add_statement(s
);
2267 // Write an equality function that simply calls the equality function
2268 // for a named type. This is used when one named type is defined as
2269 // another. This ensures that this case works when the other named
2270 // type is defined in another package and relies on calling equality
2271 // functions defined only in that package.
2274 Type::write_named_equal(Gogo
* gogo
, Named_type
* name
)
2276 Location bloc
= Linemap::predeclared_location();
2278 // The pointers to the types we are going to compare. These have
2279 // type unsafe.Pointer.
2280 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2281 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2282 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
2284 Named_type
* base_type
= name
->real_type()->named_type();
2285 go_assert(base_type
!= NULL
);
2287 // Build temporaries with the base type.
2288 Type
* pt
= Type::make_pointer_type(base_type
);
2290 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
2291 ref
= Expression::make_cast(pt
, ref
, bloc
);
2292 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
2293 gogo
->add_statement(p1
);
2295 ref
= Expression::make_var_reference(key2_arg
, bloc
);
2296 ref
= Expression::make_cast(pt
, ref
, bloc
);
2297 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
2298 gogo
->add_statement(p2
);
2300 // Compare the values for equality.
2301 Expression
* t1
= Expression::make_temporary_reference(p1
, bloc
);
2302 t1
= Expression::make_dereference(t1
, Expression::NIL_CHECK_NOT_NEEDED
, bloc
);
2304 Expression
* t2
= Expression::make_temporary_reference(p2
, bloc
);
2305 t2
= Expression::make_dereference(t2
, Expression::NIL_CHECK_NOT_NEEDED
, bloc
);
2307 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, t1
, t2
, bloc
);
2309 // Return the equality comparison.
2310 Expression_list
* vals
= new Expression_list();
2311 vals
->push_back(cond
);
2312 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2313 gogo
->add_statement(s
);
2316 // Return whether this type is stored directly in an interface's
2319 // Since finalize_methods runs before type checking, we may see a
2320 // malformed type like 'type T struct { x T }'. Use a visited map
2321 // to avoid infinite recursion.
2324 Type::is_direct_iface_type() const
2326 Unordered_set(const Type
*) visited
;
2327 return this->is_direct_iface_type_helper(&visited
);
2331 Type::is_direct_iface_type_helper(Unordered_set(const Type
*)* visited
) const
2333 if (this->points_to() != NULL
2334 || this->channel_type() != NULL
2335 || this->function_type() != NULL
2336 || this->map_type() != NULL
)
2339 std::pair
<Unordered_set(const Type
*)::iterator
, bool> ins
2340 = visited
->insert(this);
2342 // malformed circular type
2345 const Struct_type
* st
= this->struct_type();
2347 return (st
->field_count() == 1
2348 && st
->field(0)->type()->is_direct_iface_type_helper(visited
));
2349 const Array_type
* at
= this->array_type();
2350 if (at
!= NULL
&& !at
->is_slice_type())
2353 return (at
->int_length(&len
) && len
== 1
2354 && at
->element_type()->is_direct_iface_type_helper(visited
));
2359 // Return a composite literal for the type descriptor for a plain type
2360 // of kind RUNTIME_TYPE_KIND named NAME.
2363 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
2364 Named_type
* name
, const Methods
* methods
,
2365 bool only_value_methods
)
2367 Location bloc
= Linemap::predeclared_location();
2369 Type
* td_type
= Type::make_type_descriptor_type();
2370 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
2372 Expression_list
* vals
= new Expression_list();
2375 if (!this->has_pointer())
2376 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
2377 if (this->is_direct_iface_type())
2378 runtime_type_kind
|= RUNTIME_TYPE_KIND_DIRECT_IFACE
;
2381 if (this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2382 runtime_type_kind
|= RUNTIME_TYPE_KIND_GC_PROG
;
2384 Struct_field_list::const_iterator p
= fields
->begin();
2385 go_assert(p
->is_field_name("size"));
2386 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
2387 vals
->push_back(Expression::make_type_info(this, type_info
));
2390 go_assert(p
->is_field_name("ptrdata"));
2391 type_info
= Expression::TYPE_INFO_DESCRIPTOR_PTRDATA
;
2392 vals
->push_back(Expression::make_type_info(this, type_info
));
2395 go_assert(p
->is_field_name("hash"));
2398 h
= name
->hash_for_method(gogo
, Type::COMPARE_TAGS
);
2400 h
= this->hash_for_method(gogo
, Type::COMPARE_TAGS
);
2401 vals
->push_back(Expression::make_integer_ul(h
, p
->type(), bloc
));
2404 go_assert(p
->is_field_name("kind"));
2405 vals
->push_back(Expression::make_integer_ul(runtime_type_kind
, p
->type(),
2409 go_assert(p
->is_field_name("align"));
2410 type_info
= Expression::TYPE_INFO_ALIGNMENT
;
2411 vals
->push_back(Expression::make_type_info(this, type_info
));
2414 go_assert(p
->is_field_name("fieldAlign"));
2415 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
2416 vals
->push_back(Expression::make_type_info(this, type_info
));
2419 go_assert(p
->is_field_name("hashfn"));
2420 Function_type
* hash_fntype
= p
->type()->function_type();
2423 go_assert(p
->is_field_name("equalfn"));
2424 Function_type
* equal_fntype
= p
->type()->function_type();
2426 Named_object
* hash_fn
;
2427 Named_object
* equal_fn
;
2428 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
2430 if (hash_fn
== NULL
)
2431 vals
->push_back(Expression::make_cast(hash_fntype
,
2432 Expression::make_nil(bloc
),
2435 vals
->push_back(Expression::make_func_reference(hash_fn
, NULL
, bloc
));
2436 if (equal_fn
== NULL
)
2437 vals
->push_back(Expression::make_cast(equal_fntype
,
2438 Expression::make_nil(bloc
),
2441 vals
->push_back(Expression::make_func_reference(equal_fn
, NULL
, bloc
));
2444 go_assert(p
->is_field_name("gcdata"));
2445 vals
->push_back(Expression::make_gc_symbol(this));
2448 go_assert(p
->is_field_name("string"));
2449 Expression
* s
= Expression::make_string((name
!= NULL
2450 ? name
->reflection(gogo
)
2451 : this->reflection(gogo
)),
2453 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2456 go_assert(p
->is_field_name("uncommonType"));
2457 if (name
== NULL
&& methods
== NULL
)
2458 vals
->push_back(Expression::make_nil(bloc
));
2461 if (methods
== NULL
)
2462 methods
= name
->methods();
2463 vals
->push_back(this->uncommon_type_constructor(gogo
,
2466 only_value_methods
));
2470 go_assert(p
->is_field_name("ptrToThis"));
2471 if (name
== NULL
&& methods
== NULL
)
2472 vals
->push_back(Expression::make_nil(bloc
));
2477 pt
= Type::make_pointer_type(name
);
2479 pt
= Type::make_pointer_type(this);
2480 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
2484 go_assert(p
== fields
->end());
2486 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
2489 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2490 // length used by the gc toolchain, and also appears in
2491 // libgo/go/reflect/type.go.
2493 static const int64_t max_ptrmask_bytes
= 2048;
2495 // Return a pointer to the Garbage Collection information for this type.
2498 Type::gc_symbol_pointer(Gogo
* gogo
)
2500 Type
* t
= this->unalias();
2502 if (!t
->has_pointer())
2503 return gogo
->backend()->nil_pointer_expression();
2505 if (t
->gc_symbol_var_
== NULL
)
2507 t
->make_gc_symbol_var(gogo
);
2508 go_assert(t
->gc_symbol_var_
!= NULL
);
2510 Location bloc
= Linemap::predeclared_location();
2511 Bexpression
* var_expr
=
2512 gogo
->backend()->var_expression(t
->gc_symbol_var_
, bloc
);
2513 Bexpression
* addr_expr
=
2514 gogo
->backend()->address_expression(var_expr
, bloc
);
2516 Type
* uint8_type
= Type::lookup_integer_type("uint8");
2517 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
2518 Btype
* ubtype
= pointer_uint8_type
->get_backend(gogo
);
2519 return gogo
->backend()->convert_expression(ubtype
, addr_expr
, bloc
);
2522 // A mapping from unnamed types to GC symbol variables.
2524 Type::GC_symbol_vars
Type::gc_symbol_vars
;
2526 // Build the GC symbol for this type.
2529 Type::make_gc_symbol_var(Gogo
* gogo
)
2531 go_assert(this->gc_symbol_var_
== NULL
);
2533 Named_type
* nt
= this->named_type();
2535 // We can have multiple instances of unnamed types and similar to type
2536 // descriptors, we only want to the emit the GC data once, so we use a
2538 Bvariable
** phash
= NULL
;
2541 Bvariable
* bvnull
= NULL
;
2542 std::pair
<GC_symbol_vars::iterator
, bool> ins
=
2543 Type::gc_symbol_vars
.insert(std::make_pair(this, bvnull
));
2546 // We've already built a gc symbol for this type.
2547 this->gc_symbol_var_
= ins
.first
->second
;
2550 phash
= &ins
.first
->second
;
2555 if (!this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2557 this->gc_symbol_var_
= this->gc_ptrmask_var(gogo
, ptrsize
, ptrdata
);
2559 *phash
= this->gc_symbol_var_
;
2563 std::string sym_name
= gogo
->gc_symbol_name(this);
2565 // Build the contents of the gc symbol.
2566 Expression
* sym_init
= this->gcprog_constructor(gogo
, ptrsize
, ptrdata
);
2567 Btype
* sym_btype
= sym_init
->type()->get_backend(gogo
);
2569 // If the type descriptor for this type is defined somewhere else, so is the
2571 const Package
* dummy
;
2572 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
2574 std::string
asm_name(go_selectively_encode_id(sym_name
));
2575 this->gc_symbol_var_
=
2576 gogo
->backend()->implicit_variable_reference(sym_name
, asm_name
,
2579 *phash
= this->gc_symbol_var_
;
2583 // See if this gc symbol can appear in multiple packages.
2584 bool is_common
= false;
2587 // We create the symbol for a builtin type whenever we need
2589 is_common
= nt
->is_builtin();
2593 // This is an unnamed type. The descriptor could be defined in
2594 // any package where it is needed, and the linker will pick one
2595 // descriptor to keep.
2599 // Since we are building the GC symbol in this package, we must create the
2600 // variable before converting the initializer to its backend representation
2601 // because the initializer may refer to the GC symbol for this type.
2602 std::string
asm_name(go_selectively_encode_id(sym_name
));
2603 this->gc_symbol_var_
=
2604 gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2605 sym_btype
, false, true, is_common
, 0);
2607 *phash
= this->gc_symbol_var_
;
2609 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2610 context
.set_is_const();
2611 Bexpression
* sym_binit
= sym_init
->get_backend(&context
);
2612 gogo
->backend()->implicit_variable_set_init(this->gc_symbol_var_
, sym_name
,
2613 sym_btype
, false, true, is_common
,
2617 // Return whether this type needs a GC program, and set *PTRDATA to
2618 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2622 Type::needs_gcprog(Gogo
* gogo
, int64_t* ptrsize
, int64_t* ptrdata
)
2624 Type
* voidptr
= Type::make_pointer_type(Type::make_void_type());
2625 if (!voidptr
->backend_type_size(gogo
, ptrsize
))
2628 if (!this->backend_type_ptrdata(gogo
, ptrdata
))
2630 go_assert(saw_errors());
2634 return *ptrdata
/ *ptrsize
> max_ptrmask_bytes
;
2637 // A simple class used to build a GC ptrmask for a type.
2642 Ptrmask(size_t count
)
2643 : bits_((count
+ 7) / 8, 0)
2647 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2653 constructor(Gogo
* gogo
) const;
2658 { this->bits_
.at(index
/ 8) |= 1 << (index
% 8); }
2661 std::vector
<unsigned char> bits_
;
2664 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2665 // counts in bytes. PTRSIZE is the size of a pointer on the target
2669 Ptrmask::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2671 switch (type
->base()->classification())
2674 case Type::TYPE_NIL
:
2675 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2676 case Type::TYPE_NAMED
:
2677 case Type::TYPE_FORWARD
:
2680 case Type::TYPE_ERROR
:
2681 case Type::TYPE_VOID
:
2682 case Type::TYPE_BOOLEAN
:
2683 case Type::TYPE_INTEGER
:
2684 case Type::TYPE_FLOAT
:
2685 case Type::TYPE_COMPLEX
:
2686 case Type::TYPE_SINK
:
2689 case Type::TYPE_FUNCTION
:
2690 case Type::TYPE_POINTER
:
2691 case Type::TYPE_MAP
:
2692 case Type::TYPE_CHANNEL
:
2693 // These types are all a single pointer.
2694 go_assert((offset
% ptrsize
) == 0);
2695 this->set(offset
/ ptrsize
);
2698 case Type::TYPE_STRING
:
2699 // A string starts with a single pointer.
2700 go_assert((offset
% ptrsize
) == 0);
2701 this->set(offset
/ ptrsize
);
2704 case Type::TYPE_INTERFACE
:
2705 // An interface is two pointers.
2706 go_assert((offset
% ptrsize
) == 0);
2707 this->set(offset
/ ptrsize
);
2708 this->set((offset
/ ptrsize
) + 1);
2711 case Type::TYPE_STRUCT
:
2713 if (!type
->has_pointer())
2716 const Struct_field_list
* fields
= type
->struct_type()->fields();
2717 int64_t soffset
= 0;
2718 for (Struct_field_list::const_iterator pf
= fields
->begin();
2719 pf
!= fields
->end();
2722 int64_t field_align
;
2723 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2725 go_assert(saw_errors());
2728 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2730 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2733 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2735 go_assert(saw_errors());
2738 soffset
+= field_size
;
2743 case Type::TYPE_ARRAY
:
2744 if (type
->is_slice_type())
2746 // A slice starts with a single pointer.
2747 go_assert((offset
% ptrsize
) == 0);
2748 this->set(offset
/ ptrsize
);
2753 if (!type
->has_pointer())
2757 if (!type
->array_type()->int_length(&len
))
2759 go_assert(saw_errors());
2763 Type
* element_type
= type
->array_type()->element_type();
2765 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2767 go_assert(saw_errors());
2771 int64_t eoffset
= 0;
2772 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2773 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2779 // Return a symbol name for this ptrmask. This is used to coalesce
2780 // identical ptrmasks, which are common. The symbol name must use
2781 // only characters that are valid in symbols. It's nice if it's
2782 // short. We convert it to a string that uses only 32 characters,
2783 // avoiding digits and u and U.
2786 Ptrmask::symname() const
2788 const char chars
[33] = "abcdefghijklmnopqrstvwxyzABCDEFG";
2789 go_assert(chars
[32] == '\0');
2793 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2794 p
!= this->bits_
.end();
2797 b
|= *p
<< remaining
;
2799 while (remaining
>= 5)
2801 ret
+= chars
[b
& 0x1f];
2806 while (remaining
> 0)
2808 ret
+= chars
[b
& 0x1f];
2815 // Return a constructor for this ptrmask. This will be used to
2816 // initialize the runtime ptrmask value.
2819 Ptrmask::constructor(Gogo
* gogo
) const
2821 Location bloc
= Linemap::predeclared_location();
2822 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
2823 Expression
* len
= Expression::make_integer_ul(this->bits_
.size(), NULL
,
2825 Array_type
* at
= Type::make_array_type(byte_type
, len
);
2826 Expression_list
* vals
= new Expression_list();
2827 vals
->reserve(this->bits_
.size());
2828 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2829 p
!= this->bits_
.end();
2831 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
2832 return Expression::make_array_composite_literal(at
, vals
, bloc
);
2835 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2836 Type::GC_gcbits_vars
Type::gc_gcbits_vars
;
2838 // Return a ptrmask variable for a type. For a type descriptor this
2839 // is only used for variables that are small enough to not need a
2840 // gcprog, but for a global variable this is used for a variable of
2841 // any size. PTRDATA is the number of bytes of the type that contain
2842 // pointer data. PTRSIZE is the size of a pointer on the target
2846 Type::gc_ptrmask_var(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
2848 Ptrmask
ptrmask(ptrdata
/ ptrsize
);
2849 if (ptrdata
>= ptrsize
)
2850 ptrmask
.set_from(gogo
, this, ptrsize
, 0);
2853 // This can happen in error cases. Just build an empty gcbits.
2854 go_assert(saw_errors());
2857 std::string sym_name
= gogo
->ptrmask_symbol_name(ptrmask
.symname());
2858 Bvariable
* bvnull
= NULL
;
2859 std::pair
<GC_gcbits_vars::iterator
, bool> ins
=
2860 Type::gc_gcbits_vars
.insert(std::make_pair(sym_name
, bvnull
));
2863 // We've already built a GC symbol for this set of gcbits.
2864 return ins
.first
->second
;
2867 Expression
* val
= ptrmask
.constructor(gogo
);
2868 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2869 context
.set_is_const();
2870 Bexpression
* bval
= val
->get_backend(&context
);
2872 std::string
asm_name(go_selectively_encode_id(sym_name
));
2873 Btype
*btype
= val
->type()->get_backend(gogo
);
2874 Bvariable
* ret
= gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2877 gogo
->backend()->implicit_variable_set_init(ret
, sym_name
, btype
, false,
2879 ins
.first
->second
= ret
;
2883 // A GCProg is used to build a program for the garbage collector.
2884 // This is used for types with a lot of pointer data, to reduce the
2885 // size of the data in the compiled program. The program is expanded
2886 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2892 : bytes_(), index_(0), nb_(0)
2895 // The number of bits described so far.
2898 { return this->index_
; }
2901 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2907 constructor(Gogo
* gogo
) const;
2914 should_repeat(int64_t, int64_t);
2917 repeat(int64_t, int64_t);
2920 zero_until(int64_t);
2931 // Add a byte to the program.
2933 byte(unsigned char x
)
2934 { this->bytes_
.push_back(x
); }
2936 // The maximum number of bytes of literal bits.
2937 static const int max_literal
= 127;
2940 std::vector
<unsigned char> bytes_
;
2941 // The index of the last bit described.
2943 // The current set of literal bits.
2944 unsigned char b_
[max_literal
];
2945 // The current number of literal bits.
2949 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2950 // counts in bytes. PTRSIZE is the size of a pointer on the target
2954 GCProg::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2956 switch (type
->base()->classification())
2959 case Type::TYPE_NIL
:
2960 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2961 case Type::TYPE_NAMED
:
2962 case Type::TYPE_FORWARD
:
2965 case Type::TYPE_ERROR
:
2966 case Type::TYPE_VOID
:
2967 case Type::TYPE_BOOLEAN
:
2968 case Type::TYPE_INTEGER
:
2969 case Type::TYPE_FLOAT
:
2970 case Type::TYPE_COMPLEX
:
2971 case Type::TYPE_SINK
:
2974 case Type::TYPE_FUNCTION
:
2975 case Type::TYPE_POINTER
:
2976 case Type::TYPE_MAP
:
2977 case Type::TYPE_CHANNEL
:
2978 // These types are all a single pointer.
2979 go_assert((offset
% ptrsize
) == 0);
2980 this->ptr(offset
/ ptrsize
);
2983 case Type::TYPE_STRING
:
2984 // A string starts with a single pointer.
2985 go_assert((offset
% ptrsize
) == 0);
2986 this->ptr(offset
/ ptrsize
);
2989 case Type::TYPE_INTERFACE
:
2990 // An interface is two pointers.
2991 go_assert((offset
% ptrsize
) == 0);
2992 this->ptr(offset
/ ptrsize
);
2993 this->ptr((offset
/ ptrsize
) + 1);
2996 case Type::TYPE_STRUCT
:
2998 if (!type
->has_pointer())
3001 const Struct_field_list
* fields
= type
->struct_type()->fields();
3002 int64_t soffset
= 0;
3003 for (Struct_field_list::const_iterator pf
= fields
->begin();
3004 pf
!= fields
->end();
3007 int64_t field_align
;
3008 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
3010 go_assert(saw_errors());
3013 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
3015 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
3018 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
3020 go_assert(saw_errors());
3023 soffset
+= field_size
;
3028 case Type::TYPE_ARRAY
:
3029 if (type
->is_slice_type())
3031 // A slice starts with a single pointer.
3032 go_assert((offset
% ptrsize
) == 0);
3033 this->ptr(offset
/ ptrsize
);
3038 if (!type
->has_pointer())
3042 if (!type
->array_type()->int_length(&len
))
3044 go_assert(saw_errors());
3048 Type
* element_type
= type
->array_type()->element_type();
3050 // Flatten array of array to a big array by multiplying counts.
3051 while (element_type
->array_type() != NULL
3052 && !element_type
->is_slice_type())
3055 if (!element_type
->array_type()->int_length(&ele_len
))
3057 go_assert(saw_errors());
3062 element_type
= element_type
->array_type()->element_type();
3066 if (!element_type
->backend_type_size(gogo
, &ele_size
))
3068 go_assert(saw_errors());
3072 go_assert(len
> 0 && ele_size
> 0);
3074 if (!this->should_repeat(ele_size
/ ptrsize
, len
))
3076 // Cheaper to just emit the bits.
3077 int64_t eoffset
= 0;
3078 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
3079 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
3083 go_assert((offset
% ptrsize
) == 0);
3084 go_assert((ele_size
% ptrsize
) == 0);
3085 this->set_from(gogo
, element_type
, ptrsize
, offset
);
3086 this->zero_until((offset
+ ele_size
) / ptrsize
);
3087 this->repeat(ele_size
/ ptrsize
, len
- 1);
3095 // Emit a 1 into the bit stream of a GC program at the given bit index.
3098 GCProg::ptr(int64_t index
)
3100 go_assert(index
>= this->index_
);
3101 this->zero_until(index
);
3105 // Return whether it is worthwhile to use a repeat to describe c
3106 // elements of n bits each, compared to just emitting c copies of the
3107 // n-bit description.
3110 GCProg::should_repeat(int64_t n
, int64_t c
)
3112 // Repeat if there is more than 1 item and if the total data doesn't
3113 // fit into four bytes.
3114 return c
> 1 && c
* n
> 4 * 8;
3117 // Emit an instruction to repeat the description of the last n words c
3118 // times (including the initial description, so c + 1 times in total).
3121 GCProg::repeat(int64_t n
, int64_t c
)
3123 if (n
== 0 || c
== 0)
3127 this->byte(0x80 | static_cast<unsigned char>(n
& 0x7f));
3134 this->index_
+= n
* c
;
3137 // Add zeros to the bit stream up to the given index.
3140 GCProg::zero_until(int64_t index
)
3142 go_assert(index
>= this->index_
);
3143 int64_t skip
= index
- this->index_
;
3148 for (int64_t i
= 0; i
< skip
; ++i
)
3154 this->repeat(1, skip
- 1);
3157 // Add a single literal bit to the program.
3160 GCProg::lit(unsigned char x
)
3162 if (this->nb_
== GCProg::max_literal
)
3164 this->b_
[this->nb_
] = x
;
3169 // Emit the varint encoding of x.
3172 GCProg::varint(int64_t x
)
3177 this->byte(0x80 | static_cast<unsigned char>(x
& 0x7f));
3180 this->byte(static_cast<unsigned char>(x
& 0x7f));
3183 // Flush any pending literal bits.
3190 this->byte(static_cast<unsigned char>(this->nb_
));
3191 unsigned char bits
= 0;
3192 for (int i
= 0; i
< this->nb_
; ++i
)
3194 bits
|= this->b_
[i
] << (i
% 8);
3195 if ((i
+ 1) % 8 == 0)
3201 if (this->nb_
% 8 != 0)
3206 // Mark the end of a GC program.
3215 // Return an Expression for the bytes in a GC program.
3218 GCProg::constructor(Gogo
* gogo
) const
3220 Location bloc
= Linemap::predeclared_location();
3222 // The first four bytes are the length of the program in target byte
3223 // order. Build a struct whose first type is uint32 to make this
3226 Type
* uint32_type
= Type::lookup_integer_type("uint32");
3228 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
3229 Expression
* len
= Expression::make_integer_ul(this->bytes_
.size(), NULL
,
3231 Array_type
* at
= Type::make_array_type(byte_type
, len
);
3233 Struct_type
* st
= Type::make_builtin_struct_type(2, "len", uint32_type
,
3236 Expression_list
* vals
= new Expression_list();
3237 vals
->reserve(this->bytes_
.size());
3238 for (std::vector
<unsigned char>::const_iterator p
= this->bytes_
.begin();
3239 p
!= this->bytes_
.end();
3241 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
3242 Expression
* bytes
= Expression::make_array_composite_literal(at
, vals
, bloc
);
3244 vals
= new Expression_list();
3245 vals
->push_back(Expression::make_integer_ul(this->bytes_
.size(), uint32_type
,
3247 vals
->push_back(bytes
);
3249 return Expression::make_struct_composite_literal(st
, vals
, bloc
);
3252 // Return a composite literal for the garbage collection program for
3253 // this type. This is only used for types that are too large to use a
3257 Type::gcprog_constructor(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
3259 Location bloc
= Linemap::predeclared_location();
3262 prog
.set_from(gogo
, this, ptrsize
, 0);
3263 int64_t offset
= prog
.bit_index() * ptrsize
;
3267 if (!this->backend_type_size(gogo
, &type_size
))
3269 go_assert(saw_errors());
3270 return Expression::make_error(bloc
);
3273 go_assert(offset
>= ptrdata
&& offset
<= type_size
);
3275 return prog
.constructor(gogo
);
3278 // Return a composite literal for the uncommon type information for
3279 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3280 // struct. If name is not NULL, it is the name of the type. If
3281 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3282 // is true if only value methods should be included. At least one of
3283 // NAME and METHODS must not be NULL.
3286 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
3287 Named_type
* name
, const Methods
* methods
,
3288 bool only_value_methods
) const
3290 Location bloc
= Linemap::predeclared_location();
3292 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
3294 Expression_list
* vals
= new Expression_list();
3297 Struct_field_list::const_iterator p
= fields
->begin();
3298 go_assert(p
->is_field_name("name"));
3301 go_assert(p
->is_field_name("pkgPath"));
3305 vals
->push_back(Expression::make_nil(bloc
));
3306 vals
->push_back(Expression::make_nil(bloc
));
3310 Named_object
* no
= name
->named_object();
3311 std::string n
= Gogo::unpack_hidden_name(no
->name());
3312 Expression
* s
= Expression::make_string(n
, bloc
);
3313 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3315 if (name
->is_builtin())
3316 vals
->push_back(Expression::make_nil(bloc
));
3319 const Package
* package
= no
->package();
3320 const std::string
& pkgpath(package
== NULL
3322 : package
->pkgpath());
3323 s
= Expression::make_string(pkgpath
, bloc
);
3324 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3329 go_assert(p
->is_field_name("methods"));
3330 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
3331 only_value_methods
));
3334 go_assert(p
== fields
->end());
3336 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
3338 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
3341 // Sort methods by name.
3347 operator()(const std::pair
<std::string
, const Method
*>& m1
,
3348 const std::pair
<std::string
, const Method
*>& m2
) const
3350 return (Gogo::unpack_hidden_name(m1
.first
)
3351 < Gogo::unpack_hidden_name(m2
.first
));
3355 // Return a composite literal for the type method table for this type.
3356 // METHODS_TYPE is the type of the table, and is a slice type.
3357 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3358 // then only value methods are used.
3361 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
3362 const Methods
* methods
,
3363 bool only_value_methods
) const
3365 Location bloc
= Linemap::predeclared_location();
3367 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
3368 if (methods
!= NULL
)
3370 smethods
.reserve(methods
->count());
3371 for (Methods::const_iterator p
= methods
->begin();
3372 p
!= methods
->end();
3375 if (p
->second
->is_ambiguous())
3377 if (only_value_methods
&& !p
->second
->is_value_method())
3380 // This is where we implement the magic //go:nointerface
3381 // comment. If we saw that comment, we don't add this
3382 // method to the type descriptor.
3383 if (p
->second
->nointerface())
3386 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
3390 if (smethods
.empty())
3391 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
3393 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
3395 Type
* method_type
= methods_type
->array_type()->element_type();
3397 Expression_list
* vals
= new Expression_list();
3398 vals
->reserve(smethods
.size());
3399 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
3401 p
!= smethods
.end();
3403 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
3404 p
->second
, only_value_methods
));
3406 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
3409 // Return a composite literal for a single method. METHOD_TYPE is the
3410 // type of the entry. METHOD_NAME is the name of the method and M is
3411 // the method information.
3414 Type::method_constructor(Gogo
*, Type
* method_type
,
3415 const std::string
& method_name
,
3417 bool only_value_methods
) const
3419 Location bloc
= Linemap::predeclared_location();
3421 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
3423 Expression_list
* vals
= new Expression_list();
3426 Struct_field_list::const_iterator p
= fields
->begin();
3427 go_assert(p
->is_field_name("name"));
3428 const std::string n
= Gogo::unpack_hidden_name(method_name
);
3429 Expression
* s
= Expression::make_string(n
, bloc
);
3430 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3433 go_assert(p
->is_field_name("pkgPath"));
3434 if (!Gogo::is_hidden_name(method_name
))
3435 vals
->push_back(Expression::make_nil(bloc
));
3438 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
3440 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3443 bool use_direct_iface_stub
=
3444 this->points_to() != NULL
3445 && this->points_to()->is_direct_iface_type()
3446 && m
->is_value_method();
3447 Named_object
* no
= (use_direct_iface_stub
3448 ? m
->iface_stub_object()
3449 : (m
->needs_stub_method()
3451 : m
->named_object()));
3453 Function_type
* mtype
;
3454 if (no
->is_function())
3455 mtype
= no
->func_value()->type();
3457 mtype
= no
->func_declaration_value()->type();
3458 go_assert(mtype
->is_method());
3459 Type
* nonmethod_type
= mtype
->copy_without_receiver();
3462 go_assert(p
->is_field_name("mtyp"));
3463 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3466 go_assert(p
->is_field_name("typ"));
3467 bool want_pointer_receiver
= (!only_value_methods
&& m
->is_value_method()
3468 && !use_direct_iface_stub
);
3469 nonmethod_type
= mtype
->copy_with_receiver_as_param(want_pointer_receiver
);
3470 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3473 go_assert(p
->is_field_name("tfn"));
3474 vals
->push_back(Expression::make_func_code_reference(no
, bloc
));
3477 go_assert(p
== fields
->end());
3479 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
3482 // Return a composite literal for the type descriptor of a plain type.
3483 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3484 // NULL, it is the name to use as well as the list of methods.
3487 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
3490 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
3494 // Return the type reflection string for this type.
3497 Type::reflection(Gogo
* gogo
) const
3501 // The do_reflection virtual function should set RET to the
3502 // reflection string.
3503 this->do_reflection(gogo
, &ret
);
3508 // Return whether the backend size of the type is known.
3511 Type::is_backend_type_size_known(Gogo
* gogo
)
3513 switch (this->classification_
)
3527 case TYPE_INTERFACE
:
3532 const Struct_field_list
* fields
= this->struct_type()->fields();
3533 for (Struct_field_list::const_iterator pf
= fields
->begin();
3534 pf
!= fields
->end();
3536 if (!pf
->type()->is_backend_type_size_known(gogo
))
3543 const Array_type
* at
= this->array_type();
3544 if (at
->length() == NULL
)
3548 Numeric_constant nc
;
3549 if (!at
->length()->numeric_constant_value(&nc
))
3552 if (!nc
.to_int(&ival
))
3555 return at
->element_type()->is_backend_type_size_known(gogo
);
3560 this->named_type()->convert(gogo
);
3561 return this->named_type()->is_named_backend_type_size_known();
3565 Forward_declaration_type
* fdt
= this->forward_declaration_type();
3566 return fdt
->real_type()->is_backend_type_size_known(gogo
);
3570 case TYPE_CALL_MULTIPLE_RESULT
:
3578 // If the size of the type can be determined, set *PSIZE to the size
3579 // in bytes and return true. Otherwise, return false. This queries
3583 Type::backend_type_size(Gogo
* gogo
, int64_t *psize
)
3585 if (!this->is_backend_type_size_known(gogo
))
3587 if (this->is_error_type())
3589 Btype
* bt
= this->get_backend_placeholder(gogo
);
3590 *psize
= gogo
->backend()->type_size(bt
);
3593 if (this->named_type() != NULL
)
3594 go_error_at(this->named_type()->location(),
3595 "type %s larger than address space",
3596 Gogo::message_name(this->named_type()->name()).c_str());
3598 go_error_at(Linemap::unknown_location(),
3599 "type %s larger than address space",
3600 this->reflection(gogo
).c_str());
3602 // Make this an error type to avoid knock-on errors.
3603 this->classification_
= TYPE_ERROR
;
3609 // If the alignment of the type can be determined, set *PALIGN to
3610 // the alignment in bytes and return true. Otherwise, return false.
3613 Type::backend_type_align(Gogo
* gogo
, int64_t *palign
)
3615 if (!this->is_backend_type_size_known(gogo
))
3617 Btype
* bt
= this->get_backend_placeholder(gogo
);
3618 *palign
= gogo
->backend()->type_alignment(bt
);
3622 // Like backend_type_align, but return the alignment when used as a
3626 Type::backend_type_field_align(Gogo
* gogo
, int64_t *palign
)
3628 if (!this->is_backend_type_size_known(gogo
))
3630 Btype
* bt
= this->get_backend_placeholder(gogo
);
3631 *palign
= gogo
->backend()->type_field_alignment(bt
);
3635 // Get the ptrdata value for a type. This is the size of the prefix
3636 // of the type that contains all pointers. Store the ptrdata in
3637 // *PPTRDATA and return whether we found it.
3640 Type::backend_type_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3644 if (!this->has_pointer())
3647 if (!this->is_backend_type_size_known(gogo
))
3650 switch (this->classification_
)
3659 // These types are nothing but a pointer.
3660 return this->backend_type_size(gogo
, pptrdata
);
3662 case TYPE_INTERFACE
:
3663 // An interface is a struct of two pointers.
3664 return this->backend_type_size(gogo
, pptrdata
);
3668 // A string is a struct whose first field is a pointer, and
3669 // whose second field is not.
3670 Type
* uint8_type
= Type::lookup_integer_type("uint8");
3671 Type
* ptr
= Type::make_pointer_type(uint8_type
);
3672 return ptr
->backend_type_size(gogo
, pptrdata
);
3677 return this->base()->backend_type_ptrdata(gogo
, pptrdata
);
3681 const Struct_field_list
* fields
= this->struct_type()->fields();
3683 const Struct_field
*ptr
= NULL
;
3684 int64_t ptr_offset
= 0;
3685 for (Struct_field_list::const_iterator pf
= fields
->begin();
3686 pf
!= fields
->end();
3689 int64_t field_align
;
3690 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
3692 offset
= (offset
+ (field_align
- 1)) &~ (field_align
- 1);
3694 if (pf
->type()->has_pointer())
3697 ptr_offset
= offset
;
3701 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
3703 offset
+= field_size
;
3708 int64_t ptr_ptrdata
;
3709 if (!ptr
->type()->backend_type_ptrdata(gogo
, &ptr_ptrdata
))
3711 *pptrdata
= ptr_offset
+ ptr_ptrdata
;
3717 if (this->is_slice_type())
3719 // A slice is a struct whose first field is a pointer, and
3720 // whose remaining fields are not.
3721 Type
* element_type
= this->array_type()->element_type();
3722 Type
* ptr
= Type::make_pointer_type(element_type
);
3723 return ptr
->backend_type_size(gogo
, pptrdata
);
3727 Numeric_constant nc
;
3728 if (!this->array_type()->length()->numeric_constant_value(&nc
))
3731 if (!nc
.to_memory_size(&len
))
3734 Type
* element_type
= this->array_type()->element_type();
3736 int64_t ele_ptrdata
;
3737 if (!element_type
->backend_type_size(gogo
, &ele_size
)
3738 || !element_type
->backend_type_ptrdata(gogo
, &ele_ptrdata
))
3740 go_assert(ele_size
> 0 && ele_ptrdata
> 0);
3742 *pptrdata
= (len
- 1) * ele_size
+ ele_ptrdata
;
3754 case TYPE_CALL_MULTIPLE_RESULT
:
3759 // Get the ptrdata value to store in a type descriptor. This is
3760 // normally the same as backend_type_ptrdata, but for a type that is
3761 // large enough to use a gcprog we may need to store a different value
3762 // if it ends with an array. If the gcprog uses a repeat descriptor
3763 // for the array, and if the array element ends with non-pointer data,
3764 // then the gcprog will produce a value that describes the complete
3765 // array where the backend ptrdata will omit the non-pointer elements
3766 // of the final array element. This is a subtle difference but the
3767 // run time code checks it to verify that it has expanded a gcprog as
3771 Type::descriptor_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3773 int64_t backend_ptrdata
;
3774 if (!this->backend_type_ptrdata(gogo
, &backend_ptrdata
))
3778 if (!this->needs_gcprog(gogo
, &ptrsize
, &backend_ptrdata
))
3780 *pptrdata
= backend_ptrdata
;
3785 prog
.set_from(gogo
, this, ptrsize
, 0);
3786 int64_t offset
= prog
.bit_index() * ptrsize
;
3788 go_assert(offset
>= backend_ptrdata
);
3793 // Default function to export a type.
3796 Type::do_export(Export
*) const
3804 Type::import_type(Import
* imp
)
3806 if (imp
->match_c_string("("))
3807 return Function_type::do_import(imp
);
3808 else if (imp
->match_c_string("*"))
3809 return Pointer_type::do_import(imp
);
3810 else if (imp
->match_c_string("struct "))
3811 return Struct_type::do_import(imp
);
3812 else if (imp
->match_c_string("["))
3813 return Array_type::do_import(imp
);
3814 else if (imp
->match_c_string("map "))
3815 return Map_type::do_import(imp
);
3816 else if (imp
->match_c_string("chan "))
3817 return Channel_type::do_import(imp
);
3818 else if (imp
->match_c_string("interface"))
3819 return Interface_type::do_import(imp
);
3822 go_error_at(imp
->location(), "import error: expected type");
3823 return Type::make_error_type();
3827 // Class Error_type.
3829 // Return the backend representation of an Error type.
3832 Error_type::do_get_backend(Gogo
* gogo
)
3834 return gogo
->backend()->error_type();
3837 // Return an expression for the type descriptor for an error type.
3841 Error_type::do_type_descriptor(Gogo
*, Named_type
*)
3843 return Expression::make_error(Linemap::predeclared_location());
3846 // We should not be asked for the reflection string for an error type.
3849 Error_type::do_reflection(Gogo
*, std::string
*) const
3851 go_assert(saw_errors());
3855 Type::make_error_type()
3857 static Error_type singleton_error_type
;
3858 return &singleton_error_type
;
3863 // Get the backend representation of a void type.
3866 Void_type::do_get_backend(Gogo
* gogo
)
3868 return gogo
->backend()->void_type();
3872 Type::make_void_type()
3874 static Void_type singleton_void_type
;
3875 return &singleton_void_type
;
3878 // Class Boolean_type.
3880 // Return the backend representation of the boolean type.
3883 Boolean_type::do_get_backend(Gogo
* gogo
)
3885 return gogo
->backend()->bool_type();
3888 // Make the type descriptor.
3891 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3894 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
3897 Named_object
* no
= gogo
->lookup_global("bool");
3898 go_assert(no
!= NULL
);
3899 return Type::type_descriptor(gogo
, no
->type_value());
3904 Type::make_boolean_type()
3906 static Boolean_type boolean_type
;
3907 return &boolean_type
;
3910 // The named type "bool".
3912 static Named_type
* named_bool_type
;
3914 // Get the named type "bool".
3917 Type::lookup_bool_type()
3919 return named_bool_type
;
3922 // Make the named type "bool".
3925 Type::make_named_bool_type()
3927 Type
* bool_type
= Type::make_boolean_type();
3928 Named_object
* named_object
=
3929 Named_object::make_type("bool", NULL
, bool_type
,
3930 Linemap::predeclared_location());
3931 Named_type
* named_type
= named_object
->type_value();
3932 named_bool_type
= named_type
;
3936 // Class Integer_type.
3938 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
3940 // Create a new integer type. Non-abstract integer types always have
3944 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
3945 int bits
, int runtime_type_kind
)
3947 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
3949 std::string
sname(name
);
3950 Named_object
* named_object
=
3951 Named_object::make_type(sname
, NULL
, integer_type
,
3952 Linemap::predeclared_location());
3953 Named_type
* named_type
= named_object
->type_value();
3954 std::pair
<Named_integer_types::iterator
, bool> ins
=
3955 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
3956 go_assert(ins
.second
);
3960 // Look up an existing integer type.
3963 Integer_type::lookup_integer_type(const char* name
)
3965 Named_integer_types::const_iterator p
=
3966 Integer_type::named_integer_types
.find(name
);
3967 go_assert(p
!= Integer_type::named_integer_types
.end());
3971 // Create a new abstract integer type.
3974 Integer_type::create_abstract_integer_type()
3976 static Integer_type
* abstract_type
;
3977 if (abstract_type
== NULL
)
3979 Type
* int_type
= Type::lookup_integer_type("int");
3980 abstract_type
= new Integer_type(true, false,
3981 int_type
->integer_type()->bits(),
3982 RUNTIME_TYPE_KIND_INT
);
3984 return abstract_type
;
3987 // Create a new abstract character type.
3990 Integer_type::create_abstract_character_type()
3992 static Integer_type
* abstract_type
;
3993 if (abstract_type
== NULL
)
3995 abstract_type
= new Integer_type(true, false, 32,
3996 RUNTIME_TYPE_KIND_INT32
);
3997 abstract_type
->set_is_rune();
3999 return abstract_type
;
4002 // Integer type compatibility.
4005 Integer_type::is_identical(const Integer_type
* t
) const
4007 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
4009 return this->is_abstract_
== t
->is_abstract_
;
4015 Integer_type::do_hash_for_method(Gogo
*, int) const
4017 return ((this->bits_
<< 4)
4018 + ((this->is_unsigned_
? 1 : 0) << 8)
4019 + ((this->is_abstract_
? 1 : 0) << 9));
4022 // Convert an Integer_type to the backend representation.
4025 Integer_type::do_get_backend(Gogo
* gogo
)
4027 if (this->is_abstract_
)
4029 go_assert(saw_errors());
4030 return gogo
->backend()->error_type();
4032 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
4035 // The type descriptor for an integer type. Integer types are always
4039 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4041 go_assert(name
!= NULL
|| saw_errors());
4042 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4045 // We should not be asked for the reflection string of a basic type.
4048 Integer_type::do_reflection(Gogo
*, std::string
*) const
4050 go_assert(saw_errors());
4053 // Make an integer type.
4056 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
4057 int runtime_type_kind
)
4059 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
4063 // Make an abstract integer type.
4066 Type::make_abstract_integer_type()
4068 return Integer_type::create_abstract_integer_type();
4071 // Make an abstract character type.
4074 Type::make_abstract_character_type()
4076 return Integer_type::create_abstract_character_type();
4079 // Look up an integer type.
4082 Type::lookup_integer_type(const char* name
)
4084 return Integer_type::lookup_integer_type(name
);
4087 // Class Float_type.
4089 Float_type::Named_float_types
Float_type::named_float_types
;
4091 // Create a new float type. Non-abstract float types always have
4095 Float_type::create_float_type(const char* name
, int bits
,
4096 int runtime_type_kind
)
4098 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
4099 std::string
sname(name
);
4100 Named_object
* named_object
=
4101 Named_object::make_type(sname
, NULL
, float_type
,
4102 Linemap::predeclared_location());
4103 Named_type
* named_type
= named_object
->type_value();
4104 std::pair
<Named_float_types::iterator
, bool> ins
=
4105 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
4106 go_assert(ins
.second
);
4110 // Look up an existing float type.
4113 Float_type::lookup_float_type(const char* name
)
4115 Named_float_types::const_iterator p
=
4116 Float_type::named_float_types
.find(name
);
4117 go_assert(p
!= Float_type::named_float_types
.end());
4121 // Create a new abstract float type.
4124 Float_type::create_abstract_float_type()
4126 static Float_type
* abstract_type
;
4127 if (abstract_type
== NULL
)
4128 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
4129 return abstract_type
;
4132 // Whether this type is identical with T.
4135 Float_type::is_identical(const Float_type
* t
) const
4137 if (this->bits_
!= t
->bits_
)
4139 return this->is_abstract_
== t
->is_abstract_
;
4145 Float_type::do_hash_for_method(Gogo
*, int) const
4147 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4150 // Convert to the backend representation.
4153 Float_type::do_get_backend(Gogo
* gogo
)
4155 return gogo
->backend()->float_type(this->bits_
);
4158 // The type descriptor for a float type. Float types are always named.
4161 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4163 go_assert(name
!= NULL
|| saw_errors());
4164 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4167 // We should not be asked for the reflection string of a basic type.
4170 Float_type::do_reflection(Gogo
*, std::string
*) const
4172 go_assert(saw_errors());
4175 // Make a floating point type.
4178 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
4180 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
4183 // Make an abstract float type.
4186 Type::make_abstract_float_type()
4188 return Float_type::create_abstract_float_type();
4191 // Look up a float type.
4194 Type::lookup_float_type(const char* name
)
4196 return Float_type::lookup_float_type(name
);
4199 // Class Complex_type.
4201 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
4203 // Create a new complex type. Non-abstract complex types always have
4207 Complex_type::create_complex_type(const char* name
, int bits
,
4208 int runtime_type_kind
)
4210 Complex_type
* complex_type
= new Complex_type(false, bits
,
4212 std::string
sname(name
);
4213 Named_object
* named_object
=
4214 Named_object::make_type(sname
, NULL
, complex_type
,
4215 Linemap::predeclared_location());
4216 Named_type
* named_type
= named_object
->type_value();
4217 std::pair
<Named_complex_types::iterator
, bool> ins
=
4218 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
4220 go_assert(ins
.second
);
4224 // Look up an existing complex type.
4227 Complex_type::lookup_complex_type(const char* name
)
4229 Named_complex_types::const_iterator p
=
4230 Complex_type::named_complex_types
.find(name
);
4231 go_assert(p
!= Complex_type::named_complex_types
.end());
4235 // Create a new abstract complex type.
4238 Complex_type::create_abstract_complex_type()
4240 static Complex_type
* abstract_type
;
4241 if (abstract_type
== NULL
)
4242 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
4243 return abstract_type
;
4246 // Whether this type is identical with T.
4249 Complex_type::is_identical(const Complex_type
*t
) const
4251 if (this->bits_
!= t
->bits_
)
4253 return this->is_abstract_
== t
->is_abstract_
;
4259 Complex_type::do_hash_for_method(Gogo
*, int) const
4261 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4264 // Convert to the backend representation.
4267 Complex_type::do_get_backend(Gogo
* gogo
)
4269 return gogo
->backend()->complex_type(this->bits_
);
4272 // The type descriptor for a complex type. Complex types are always
4276 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4278 go_assert(name
!= NULL
|| saw_errors());
4279 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4282 // We should not be asked for the reflection string of a basic type.
4285 Complex_type::do_reflection(Gogo
*, std::string
*) const
4287 go_assert(saw_errors());
4290 // Make a complex type.
4293 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
4295 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
4298 // Make an abstract complex type.
4301 Type::make_abstract_complex_type()
4303 return Complex_type::create_abstract_complex_type();
4306 // Look up a complex type.
4309 Type::lookup_complex_type(const char* name
)
4311 return Complex_type::lookup_complex_type(name
);
4314 // Class String_type.
4316 // Convert String_type to the backend representation. A string is a
4317 // struct with two fields: a pointer to the characters and a length.
4320 String_type::do_get_backend(Gogo
* gogo
)
4322 static Btype
* backend_string_type
;
4323 if (backend_string_type
== NULL
)
4325 std::vector
<Backend::Btyped_identifier
> fields(2);
4327 Type
* b
= gogo
->lookup_global("byte")->type_value();
4328 Type
* pb
= Type::make_pointer_type(b
);
4330 // We aren't going to get back to this field to finish the
4331 // backend representation, so force it to be finished now.
4332 if (!gogo
->named_types_are_converted())
4334 Btype
* bt
= pb
->get_backend_placeholder(gogo
);
4335 pb
->finish_backend(gogo
, bt
);
4338 fields
[0].name
= "__data";
4339 fields
[0].btype
= pb
->get_backend(gogo
);
4340 fields
[0].location
= Linemap::predeclared_location();
4342 Type
* int_type
= Type::lookup_integer_type("int");
4343 fields
[1].name
= "__length";
4344 fields
[1].btype
= int_type
->get_backend(gogo
);
4345 fields
[1].location
= fields
[0].location
;
4347 backend_string_type
= gogo
->backend()->struct_type(fields
);
4349 return backend_string_type
;
4352 // The type descriptor for the string type.
4355 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4358 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
4361 Named_object
* no
= gogo
->lookup_global("string");
4362 go_assert(no
!= NULL
);
4363 return Type::type_descriptor(gogo
, no
->type_value());
4367 // We should not be asked for the reflection string of a basic type.
4370 String_type::do_reflection(Gogo
*, std::string
* ret
) const
4372 ret
->append("string");
4375 // Make a string type.
4378 Type::make_string_type()
4380 static String_type string_type
;
4381 return &string_type
;
4384 // The named type "string".
4386 static Named_type
* named_string_type
;
4388 // Get the named type "string".
4391 Type::lookup_string_type()
4393 return named_string_type
;
4396 // Make the named type string.
4399 Type::make_named_string_type()
4401 Type
* string_type
= Type::make_string_type();
4402 Named_object
* named_object
=
4403 Named_object::make_type("string", NULL
, string_type
,
4404 Linemap::predeclared_location());
4405 Named_type
* named_type
= named_object
->type_value();
4406 named_string_type
= named_type
;
4410 // The sink type. This is the type of the blank identifier _. Any
4411 // type may be assigned to it.
4413 class Sink_type
: public Type
4422 do_compare_is_identity(Gogo
*)
4426 do_get_backend(Gogo
*)
4427 { go_unreachable(); }
4430 do_type_descriptor(Gogo
*, Named_type
*)
4431 { go_unreachable(); }
4434 do_reflection(Gogo
*, std::string
*) const
4435 { go_unreachable(); }
4438 do_mangled_name(Gogo
*, std::string
*) const
4439 { go_unreachable(); }
4442 // Make the sink type.
4445 Type::make_sink_type()
4447 static Sink_type sink_type
;
4451 // Class Function_type.
4456 Function_type::do_traverse(Traverse
* traverse
)
4458 if (this->receiver_
!= NULL
4459 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
4460 return TRAVERSE_EXIT
;
4461 if (this->parameters_
!= NULL
4462 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
4463 return TRAVERSE_EXIT
;
4464 if (this->results_
!= NULL
4465 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
4466 return TRAVERSE_EXIT
;
4467 return TRAVERSE_CONTINUE
;
4470 // Returns whether T is a valid redeclaration of this type. If this
4471 // returns false, and REASON is not NULL, *REASON may be set to a
4472 // brief explanation of why it returned false.
4475 Function_type::is_valid_redeclaration(const Function_type
* t
,
4476 std::string
* reason
) const
4478 if (!this->is_identical(t
, false, COMPARE_TAGS
, reason
))
4481 // A redeclaration of a function is required to use the same names
4482 // for the receiver and parameters.
4483 if (this->receiver() != NULL
4484 && this->receiver()->name() != t
->receiver()->name())
4487 *reason
= "receiver name changed";
4491 const Typed_identifier_list
* parms1
= this->parameters();
4492 const Typed_identifier_list
* parms2
= t
->parameters();
4495 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4496 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4497 p2
!= parms2
->end();
4500 if (p1
->name() != p2
->name())
4503 *reason
= "parameter name changed";
4507 // This is called at parse time, so we may have unknown
4509 Type
* t1
= p1
->type()->forwarded();
4510 Type
* t2
= p2
->type()->forwarded();
4512 && t1
->forward_declaration_type() != NULL
4513 && (t2
->forward_declaration_type() == NULL
4514 || (t1
->forward_declaration_type()->named_object()
4515 != t2
->forward_declaration_type()->named_object())))
4520 const Typed_identifier_list
* results1
= this->results();
4521 const Typed_identifier_list
* results2
= t
->results();
4522 if (results1
!= NULL
)
4524 Typed_identifier_list::const_iterator res1
= results1
->begin();
4525 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4526 res2
!= results2
->end();
4529 if (res1
->name() != res2
->name())
4532 *reason
= "result name changed";
4536 // This is called at parse time, so we may have unknown
4538 Type
* t1
= res1
->type()->forwarded();
4539 Type
* t2
= res2
->type()->forwarded();
4541 && t1
->forward_declaration_type() != NULL
4542 && (t2
->forward_declaration_type() == NULL
4543 || (t1
->forward_declaration_type()->named_object()
4544 != t2
->forward_declaration_type()->named_object())))
4552 // Check whether T is the same as this type.
4555 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
4556 int flags
, std::string
* reason
) const
4558 if (this->is_backend_function_type() != t
->is_backend_function_type())
4561 if (!ignore_receiver
)
4563 const Typed_identifier
* r1
= this->receiver();
4564 const Typed_identifier
* r2
= t
->receiver();
4565 if ((r1
!= NULL
) != (r2
!= NULL
))
4568 *reason
= _("different receiver types");
4573 if (!Type::are_identical(r1
->type(), r2
->type(), flags
, reason
))
4575 if (reason
!= NULL
&& !reason
->empty())
4576 *reason
= "receiver: " + *reason
;
4582 const Typed_identifier_list
* parms1
= this->parameters();
4583 if (parms1
!= NULL
&& parms1
->empty())
4585 const Typed_identifier_list
* parms2
= t
->parameters();
4586 if (parms2
!= NULL
&& parms2
->empty())
4588 if ((parms1
!= NULL
) != (parms2
!= NULL
))
4591 *reason
= _("different number of parameters");
4596 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4597 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4598 p2
!= parms2
->end();
4601 if (p1
== parms1
->end())
4604 *reason
= _("different number of parameters");
4608 if (!Type::are_identical(p1
->type(), p2
->type(), flags
, NULL
))
4611 *reason
= _("different parameter types");
4615 if (p1
!= parms1
->end())
4618 *reason
= _("different number of parameters");
4623 if (this->is_varargs() != t
->is_varargs())
4626 *reason
= _("different varargs");
4630 const Typed_identifier_list
* results1
= this->results();
4631 if (results1
!= NULL
&& results1
->empty())
4633 const Typed_identifier_list
* results2
= t
->results();
4634 if (results2
!= NULL
&& results2
->empty())
4636 if ((results1
!= NULL
) != (results2
!= NULL
))
4639 *reason
= _("different number of results");
4642 if (results1
!= NULL
)
4644 Typed_identifier_list::const_iterator res1
= results1
->begin();
4645 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4646 res2
!= results2
->end();
4649 if (res1
== results1
->end())
4652 *reason
= _("different number of results");
4656 if (!Type::are_identical(res1
->type(), res2
->type(), flags
, NULL
))
4659 *reason
= _("different result types");
4663 if (res1
!= results1
->end())
4666 *reason
= _("different number of results");
4677 Function_type::do_hash_for_method(Gogo
* gogo
, int flags
) const
4679 unsigned int ret
= 0;
4680 // We ignore the receiver type for hash codes, because we need to
4681 // get the same hash code for a method in an interface and a method
4682 // declared for a type. The former will not have a receiver.
4683 if (this->parameters_
!= NULL
)
4686 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
4687 p
!= this->parameters_
->end();
4689 ret
+= p
->type()->hash_for_method(gogo
, flags
) << shift
;
4691 if (this->results_
!= NULL
)
4694 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
4695 p
!= this->results_
->end();
4697 ret
+= p
->type()->hash_for_method(gogo
, flags
) << shift
;
4699 if (this->is_varargs_
)
4705 // Hash result parameters.
4708 Function_type::Results_hash::operator()(const Typed_identifier_list
* t
) const
4710 unsigned int hash
= 0;
4711 for (Typed_identifier_list::const_iterator p
= t
->begin();
4716 hash
= Gogo::hash_string(p
->name(), hash
);
4717 hash
+= p
->type()->hash_for_method(NULL
, Type::COMPARE_TAGS
);
4722 // Compare result parameters so that can map identical result
4723 // parameters to a single struct type.
4726 Function_type::Results_equal::operator()(const Typed_identifier_list
* a
,
4727 const Typed_identifier_list
* b
) const
4729 if (a
->size() != b
->size())
4731 Typed_identifier_list::const_iterator pa
= a
->begin();
4732 for (Typed_identifier_list::const_iterator pb
= b
->begin();
4736 if (pa
->name() != pb
->name()
4737 || !Type::are_identical(pa
->type(), pb
->type(), Type::COMPARE_TAGS
,
4744 // Hash from results to a backend struct type.
4746 Function_type::Results_structs
Function_type::results_structs
;
4748 // Get the backend representation for a function type.
4751 Function_type::get_backend_fntype(Gogo
* gogo
)
4753 if (this->fnbtype_
== NULL
)
4755 Backend::Btyped_identifier breceiver
;
4756 if (this->receiver_
!= NULL
)
4758 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
4760 // We always pass the address of the receiver parameter, in
4761 // order to make interface calls work with unknown types,
4762 // except for direct interface types where the interface call
4763 // actually passes the underlying pointer of the value.
4764 Type
* rtype
= this->receiver_
->type();
4765 if (rtype
->points_to() == NULL
)
4767 if (rtype
->is_direct_iface_type())
4768 rtype
= Type::make_pointer_type(Type::make_void_type());
4770 rtype
= Type::make_pointer_type(rtype
);
4772 breceiver
.btype
= rtype
->get_backend(gogo
);
4773 breceiver
.location
= this->receiver_
->location();
4776 std::vector
<Backend::Btyped_identifier
> bparameters
;
4777 if (this->parameters_
!= NULL
)
4779 bparameters
.resize(this->parameters_
->size());
4781 for (Typed_identifier_list::const_iterator p
=
4782 this->parameters_
->begin(); p
!= this->parameters_
->end();
4785 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4786 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
4787 bparameters
[i
].location
= p
->location();
4789 go_assert(i
== bparameters
.size());
4792 std::vector
<Backend::Btyped_identifier
> bresults
;
4793 Btype
* bresult_struct
= NULL
;
4794 if (this->results_
!= NULL
)
4796 bresults
.resize(this->results_
->size());
4798 for (Typed_identifier_list::const_iterator p
=
4799 this->results_
->begin();
4800 p
!= this->results_
->end();
4803 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4804 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
4805 bresults
[i
].location
= p
->location();
4807 go_assert(i
== bresults
.size());
4809 if (this->results_
->size() > 1)
4811 // Use the same results struct for all functions that
4812 // return the same set of results. This is useful to
4813 // unify calls to interface methods with other calls.
4814 std::pair
<Typed_identifier_list
*, Btype
*> val
;
4815 val
.first
= this->results_
;
4817 std::pair
<Results_structs::iterator
, bool> ins
=
4818 Function_type::results_structs
.insert(val
);
4821 // Build a new struct type.
4822 Struct_field_list
* sfl
= new Struct_field_list
;
4823 for (Typed_identifier_list::const_iterator p
=
4824 this->results_
->begin();
4825 p
!= this->results_
->end();
4828 Typed_identifier tid
= *p
;
4829 if (tid
.name().empty())
4830 tid
= Typed_identifier("UNNAMED", tid
.type(),
4832 sfl
->push_back(Struct_field(tid
));
4834 Struct_type
* st
= Type::make_struct_type(sfl
,
4836 st
->set_is_struct_incomparable();
4837 ins
.first
->second
= st
->get_backend(gogo
);
4839 bresult_struct
= ins
.first
->second
;
4843 this->fnbtype_
= gogo
->backend()->function_type(breceiver
, bparameters
,
4844 bresults
, bresult_struct
,
4849 return this->fnbtype_
;
4852 // Get the backend representation for a Go function type.
4855 Function_type::do_get_backend(Gogo
* gogo
)
4857 // When we do anything with a function value other than call it, it
4858 // is represented as a pointer to a struct whose first field is the
4859 // actual function. So that is what we return as the type of a Go
4862 Location loc
= this->location();
4863 Btype
* struct_type
=
4864 gogo
->backend()->placeholder_struct_type("__go_descriptor", loc
);
4865 Btype
* ptr_struct_type
= gogo
->backend()->pointer_type(struct_type
);
4867 std::vector
<Backend::Btyped_identifier
> fields(1);
4868 fields
[0].name
= "code";
4869 fields
[0].btype
= this->get_backend_fntype(gogo
);
4870 fields
[0].location
= loc
;
4871 if (!gogo
->backend()->set_placeholder_struct_type(struct_type
, fields
))
4872 return gogo
->backend()->error_type();
4873 return ptr_struct_type
;
4876 // The type of a function type descriptor.
4879 Function_type::make_function_type_descriptor_type()
4884 Type
* tdt
= Type::make_type_descriptor_type();
4885 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4887 Type
* bool_type
= Type::lookup_bool_type();
4889 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
4891 Struct_type
* s
= Type::make_builtin_struct_type(4,
4893 "dotdotdot", bool_type
,
4897 ret
= Type::make_builtin_named_type("FuncType", s
);
4903 // The type descriptor for a function type.
4906 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4908 Location bloc
= Linemap::predeclared_location();
4910 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
4912 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
4914 Expression_list
* vals
= new Expression_list();
4917 Struct_field_list::const_iterator p
= fields
->begin();
4918 go_assert(p
->is_field_name("_type"));
4919 vals
->push_back(this->type_descriptor_constructor(gogo
,
4920 RUNTIME_TYPE_KIND_FUNC
,
4924 go_assert(p
->is_field_name("dotdotdot"));
4925 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
4928 go_assert(p
->is_field_name("in"));
4929 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
4930 this->parameters()));
4933 go_assert(p
->is_field_name("out"));
4934 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
4938 go_assert(p
== fields
->end());
4940 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
4943 // Return a composite literal for the parameters or results of a type
4947 Function_type::type_descriptor_params(Type
* params_type
,
4948 const Typed_identifier
* receiver
,
4949 const Typed_identifier_list
* params
)
4951 Location bloc
= Linemap::predeclared_location();
4953 if (receiver
== NULL
&& params
== NULL
)
4954 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
4956 Expression_list
* vals
= new Expression_list();
4957 vals
->reserve((params
== NULL
? 0 : params
->size())
4958 + (receiver
!= NULL
? 1 : 0));
4960 if (receiver
!= NULL
)
4961 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
4965 for (Typed_identifier_list::const_iterator p
= params
->begin();
4968 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
4971 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
4974 // The reflection string.
4977 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4979 // FIXME: Turn this off until we straighten out the type of the
4980 // struct field used in a go statement which calls a method.
4981 // go_assert(this->receiver_ == NULL);
4983 ret
->append("func");
4985 if (this->receiver_
!= NULL
)
4987 ret
->push_back('(');
4988 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
4989 ret
->push_back(')');
4992 ret
->push_back('(');
4993 const Typed_identifier_list
* params
= this->parameters();
4996 bool is_varargs
= this->is_varargs_
;
4997 for (Typed_identifier_list::const_iterator p
= params
->begin();
5001 if (p
!= params
->begin())
5003 if (!is_varargs
|| p
+ 1 != params
->end())
5004 this->append_reflection(p
->type(), gogo
, ret
);
5008 this->append_reflection(p
->type()->array_type()->element_type(),
5013 ret
->push_back(')');
5015 const Typed_identifier_list
* results
= this->results();
5016 if (results
!= NULL
&& !results
->empty())
5018 if (results
->size() == 1)
5019 ret
->push_back(' ');
5022 for (Typed_identifier_list::const_iterator p
= results
->begin();
5023 p
!= results
->end();
5026 if (p
!= results
->begin())
5028 this->append_reflection(p
->type(), gogo
, ret
);
5030 if (results
->size() > 1)
5031 ret
->push_back(')');
5035 // Export a function type.
5038 Function_type::do_export(Export
* exp
) const
5040 // We don't write out the receiver. The only function types which
5041 // should have a receiver are the ones associated with explicitly
5042 // defined methods. For those the receiver type is written out by
5043 // Function::export_func.
5045 exp
->write_c_string("(");
5047 if (this->parameters_
!= NULL
)
5049 bool is_varargs
= this->is_varargs_
;
5050 for (Typed_identifier_list::const_iterator p
=
5051 this->parameters_
->begin();
5052 p
!= this->parameters_
->end();
5058 exp
->write_c_string(", ");
5059 exp
->write_name(p
->name());
5060 exp
->write_c_string(" ");
5061 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
5062 exp
->write_type(p
->type());
5065 exp
->write_c_string("...");
5066 exp
->write_type(p
->type()->array_type()->element_type());
5070 exp
->write_c_string(")");
5072 const Typed_identifier_list
* results
= this->results_
;
5073 if (results
!= NULL
)
5075 exp
->write_c_string(" ");
5076 if (results
->size() == 1 && results
->begin()->name().empty())
5077 exp
->write_type(results
->begin()->type());
5081 exp
->write_c_string("(");
5082 for (Typed_identifier_list::const_iterator p
= results
->begin();
5083 p
!= results
->end();
5089 exp
->write_c_string(", ");
5090 exp
->write_name(p
->name());
5091 exp
->write_c_string(" ");
5092 exp
->write_type(p
->type());
5094 exp
->write_c_string(")");
5099 // Import a function type.
5102 Function_type::do_import(Import
* imp
)
5104 imp
->require_c_string("(");
5105 Typed_identifier_list
* parameters
;
5106 bool is_varargs
= false;
5107 if (imp
->peek_char() == ')')
5111 parameters
= new Typed_identifier_list();
5114 std::string name
= imp
->read_name();
5115 imp
->require_c_string(" ");
5117 if (imp
->match_c_string("..."))
5123 Type
* ptype
= imp
->read_type();
5125 ptype
= Type::make_array_type(ptype
, NULL
);
5126 parameters
->push_back(Typed_identifier(name
, ptype
,
5128 if (imp
->peek_char() != ',')
5130 go_assert(!is_varargs
);
5131 imp
->require_c_string(", ");
5134 imp
->require_c_string(")");
5136 Typed_identifier_list
* results
;
5137 if (imp
->peek_char() != ' ')
5142 results
= new Typed_identifier_list
;
5143 if (imp
->peek_char() != '(')
5145 Type
* rtype
= imp
->read_type();
5146 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
5153 std::string name
= imp
->read_name();
5154 imp
->require_c_string(" ");
5155 Type
* rtype
= imp
->read_type();
5156 results
->push_back(Typed_identifier(name
, rtype
,
5158 if (imp
->peek_char() != ',')
5160 imp
->require_c_string(", ");
5162 imp
->require_c_string(")");
5166 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
5169 ret
->set_is_varargs();
5173 // Make a copy of a function type without a receiver.
5176 Function_type::copy_without_receiver() const
5178 go_assert(this->is_method());
5179 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
5182 if (this->is_varargs())
5183 ret
->set_is_varargs();
5184 if (this->is_builtin())
5185 ret
->set_is_builtin();
5189 // Make a copy of a function type with a receiver.
5192 Function_type::copy_with_receiver(Type
* receiver_type
) const
5194 go_assert(!this->is_method());
5195 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
5197 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
5200 if (this->is_varargs_
)
5201 ret
->set_is_varargs();
5205 // Make a copy of a function type with the receiver as the first
5209 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver
) const
5211 go_assert(this->is_method());
5212 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5213 Type
* rtype
= this->receiver_
->type();
5214 if (want_pointer_receiver
)
5215 rtype
= Type::make_pointer_type(rtype
);
5216 Typed_identifier
receiver(this->receiver_
->name(), rtype
,
5217 this->receiver_
->location());
5218 new_params
->push_back(receiver
);
5219 const Typed_identifier_list
* orig_params
= this->parameters_
;
5220 if (orig_params
!= NULL
&& !orig_params
->empty())
5222 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5223 p
!= orig_params
->end();
5225 new_params
->push_back(*p
);
5227 return Type::make_function_type(NULL
, new_params
, this->results_
,
5231 // Make a copy of a function type ignoring any receiver and adding a
5232 // closure parameter.
5235 Function_type::copy_with_names() const
5237 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5238 const Typed_identifier_list
* orig_params
= this->parameters_
;
5239 if (orig_params
!= NULL
&& !orig_params
->empty())
5243 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5244 p
!= orig_params
->end();
5247 snprintf(buf
, sizeof buf
, "pt.%u", count
);
5249 new_params
->push_back(Typed_identifier(buf
, p
->type(),
5254 const Typed_identifier_list
* orig_results
= this->results_
;
5255 Typed_identifier_list
* new_results
;
5256 if (orig_results
== NULL
|| orig_results
->empty())
5260 new_results
= new Typed_identifier_list();
5261 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
5262 p
!= orig_results
->end();
5264 new_results
->push_back(Typed_identifier("", p
->type(),
5268 return Type::make_function_type(NULL
, new_params
, new_results
,
5272 // Make a function type.
5275 Type::make_function_type(Typed_identifier
* receiver
,
5276 Typed_identifier_list
* parameters
,
5277 Typed_identifier_list
* results
,
5280 return new Function_type(receiver
, parameters
, results
, location
);
5283 // Make a backend function type.
5285 Backend_function_type
*
5286 Type::make_backend_function_type(Typed_identifier
* receiver
,
5287 Typed_identifier_list
* parameters
,
5288 Typed_identifier_list
* results
,
5291 return new Backend_function_type(receiver
, parameters
, results
, location
);
5294 // Class Pointer_type.
5299 Pointer_type::do_traverse(Traverse
* traverse
)
5301 return Type::traverse(this->to_type_
, traverse
);
5307 Pointer_type::do_hash_for_method(Gogo
* gogo
, int flags
) const
5309 return this->to_type_
->hash_for_method(gogo
, flags
) << 4;
5312 // Get the backend representation for a pointer type.
5315 Pointer_type::do_get_backend(Gogo
* gogo
)
5317 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
5318 return gogo
->backend()->pointer_type(to_btype
);
5321 // The type of a pointer type descriptor.
5324 Pointer_type::make_pointer_type_descriptor_type()
5329 Type
* tdt
= Type::make_type_descriptor_type();
5330 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5332 Struct_type
* s
= Type::make_builtin_struct_type(2,
5336 ret
= Type::make_builtin_named_type("PtrType", s
);
5342 // The type descriptor for a pointer type.
5345 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5347 if (this->is_unsafe_pointer_type())
5349 go_assert(name
!= NULL
);
5350 return this->plain_type_descriptor(gogo
,
5351 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
5356 Location bloc
= Linemap::predeclared_location();
5358 const Methods
* methods
;
5359 Type
* deref
= this->points_to();
5360 if (deref
->named_type() != NULL
)
5361 methods
= deref
->named_type()->methods();
5362 else if (deref
->struct_type() != NULL
)
5363 methods
= deref
->struct_type()->methods();
5367 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
5369 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
5371 Expression_list
* vals
= new Expression_list();
5374 Struct_field_list::const_iterator p
= fields
->begin();
5375 go_assert(p
->is_field_name("_type"));
5376 vals
->push_back(this->type_descriptor_constructor(gogo
,
5377 RUNTIME_TYPE_KIND_PTR
,
5378 name
, methods
, false));
5381 go_assert(p
->is_field_name("elem"));
5382 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
5384 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
5388 // Reflection string.
5391 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5393 ret
->push_back('*');
5394 this->append_reflection(this->to_type_
, gogo
, ret
);
5400 Pointer_type::do_export(Export
* exp
) const
5402 exp
->write_c_string("*");
5403 if (this->is_unsafe_pointer_type())
5404 exp
->write_c_string("any");
5406 exp
->write_type(this->to_type_
);
5412 Pointer_type::do_import(Import
* imp
)
5414 imp
->require_c_string("*");
5415 if (imp
->match_c_string("any"))
5418 return Type::make_pointer_type(Type::make_void_type());
5420 Type
* to
= imp
->read_type();
5421 return Type::make_pointer_type(to
);
5424 // Cache of pointer types. Key is "to" type, value is pointer type
5425 // that points to key.
5427 Type::Pointer_type_table
Type::pointer_types
;
5429 // A list of placeholder pointer types. We keep this so we can ensure
5430 // they are finalized.
5432 std::vector
<Pointer_type
*> Type::placeholder_pointers
;
5434 // Make a pointer type.
5437 Type::make_pointer_type(Type
* to_type
)
5439 Pointer_type_table::const_iterator p
= pointer_types
.find(to_type
);
5440 if (p
!= pointer_types
.end())
5442 Pointer_type
* ret
= new Pointer_type(to_type
);
5443 pointer_types
[to_type
] = ret
;
5447 // This helper is invoked immediately after named types have been
5448 // converted, to clean up any unresolved pointer types remaining in
5449 // the pointer type cache.
5451 // The motivation for this routine: occasionally the compiler creates
5452 // some specific pointer type as part of a lowering operation (ex:
5453 // pointer-to-void), then Type::backend_type_size() is invoked on the
5454 // type (which creates a Btype placeholder for it), that placeholder
5455 // passed somewhere along the line to the back end, but since there is
5456 // no reference to the type in user code, there is never a call to
5457 // Type::finish_backend for the type (hence the Btype remains as an
5458 // unresolved placeholder). Calling this routine will clean up such
5462 Type::finish_pointer_types(Gogo
* gogo
)
5464 // We don't use begin() and end() because it is possible to add new
5465 // placeholder pointer types as we finalized existing ones.
5466 for (size_t i
= 0; i
< Type::placeholder_pointers
.size(); i
++)
5468 Pointer_type
* pt
= Type::placeholder_pointers
[i
];
5469 Type_btypes::iterator tbti
= Type::type_btypes
.find(pt
);
5470 if (tbti
!= Type::type_btypes
.end() && tbti
->second
.is_placeholder
)
5472 pt
->finish_backend(gogo
, tbti
->second
.btype
);
5473 tbti
->second
.is_placeholder
= false;
5480 // Get the backend representation of a nil type. FIXME: Is this ever
5484 Nil_type::do_get_backend(Gogo
* gogo
)
5486 return gogo
->backend()->pointer_type(gogo
->backend()->void_type());
5489 // Make the nil type.
5492 Type::make_nil_type()
5494 static Nil_type singleton_nil_type
;
5495 return &singleton_nil_type
;
5498 // The type of a function call which returns multiple values. This is
5499 // really a struct, but we don't want to confuse a function call which
5500 // returns a struct with a function call which returns multiple
5503 class Call_multiple_result_type
: public Type
5506 Call_multiple_result_type(Call_expression
* call
)
5507 : Type(TYPE_CALL_MULTIPLE_RESULT
),
5513 do_has_pointer() const
5517 do_compare_is_identity(Gogo
*)
5521 do_get_backend(Gogo
* gogo
)
5523 go_assert(saw_errors());
5524 return gogo
->backend()->error_type();
5528 do_type_descriptor(Gogo
*, Named_type
*)
5530 go_assert(saw_errors());
5531 return Expression::make_error(Linemap::unknown_location());
5535 do_reflection(Gogo
*, std::string
*) const
5536 { go_assert(saw_errors()); }
5539 do_mangled_name(Gogo
*, std::string
*) const
5540 { go_assert(saw_errors()); }
5543 // The expression being called.
5544 Call_expression
* call_
;
5547 // Make a call result type.
5550 Type::make_call_multiple_result_type(Call_expression
* call
)
5552 return new Call_multiple_result_type(call
);
5555 // Class Struct_field.
5557 // Get the name of a field.
5560 Struct_field::field_name() const
5562 const std::string
& name(this->typed_identifier_
.name());
5567 // This is called during parsing, before anything is lowered, so
5568 // we have to be pretty careful to avoid dereferencing an
5569 // unknown type name.
5570 Type
* t
= this->typed_identifier_
.type();
5572 if (t
->classification() == Type::TYPE_POINTER
)
5575 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
5576 dt
= ptype
->points_to();
5578 if (dt
->forward_declaration_type() != NULL
)
5579 return dt
->forward_declaration_type()->name();
5580 else if (dt
->named_type() != NULL
)
5582 // Note that this can be an alias name.
5583 return dt
->named_type()->name();
5585 else if (t
->is_error_type() || dt
->is_error_type())
5587 static const std::string error_string
= "*error*";
5588 return error_string
;
5592 // Avoid crashing in the erroneous case where T is named but
5595 if (t
->forward_declaration_type() != NULL
)
5596 return t
->forward_declaration_type()->name();
5597 else if (t
->named_type() != NULL
)
5598 return t
->named_type()->name();
5605 // Return whether this field is named NAME.
5608 Struct_field::is_field_name(const std::string
& name
) const
5610 const std::string
& me(this->typed_identifier_
.name());
5615 Type
* t
= this->typed_identifier_
.type();
5616 if (t
->points_to() != NULL
)
5618 Named_type
* nt
= t
->named_type();
5619 if (nt
!= NULL
&& nt
->name() == name
)
5622 // This is a horrible hack caused by the fact that we don't pack
5623 // the names of builtin types. FIXME.
5624 if (!this->is_imported_
5627 && nt
->name() == Gogo::unpack_hidden_name(name
))
5634 // Return whether this field is an unexported field named NAME.
5637 Struct_field::is_unexported_field_name(Gogo
* gogo
,
5638 const std::string
& name
) const
5640 const std::string
& field_name(this->field_name());
5641 if (Gogo::is_hidden_name(field_name
)
5642 && name
== Gogo::unpack_hidden_name(field_name
)
5643 && gogo
->pack_hidden_name(name
, false) != field_name
)
5646 // Check for the name of a builtin type. This is like the test in
5647 // is_field_name, only there we return false if this->is_imported_,
5648 // and here we return true.
5649 if (this->is_imported_
&& this->is_anonymous())
5651 Type
* t
= this->typed_identifier_
.type();
5652 if (t
->points_to() != NULL
)
5654 Named_type
* nt
= t
->named_type();
5657 && nt
->name() == Gogo::unpack_hidden_name(name
))
5664 // Return whether this field is an embedded built-in type.
5667 Struct_field::is_embedded_builtin(Gogo
* gogo
) const
5669 const std::string
& name(this->field_name());
5670 // We know that a field is an embedded type if it is anonymous.
5671 // We can decide if it is a built-in type by checking to see if it is
5672 // registered globally under the field's name.
5673 // This allows us to distinguish between embedded built-in types and
5674 // embedded types that are aliases to built-in types.
5675 return (this->is_anonymous()
5676 && !Gogo::is_hidden_name(name
)
5677 && gogo
->lookup_global(name
.c_str()) != NULL
);
5680 // Class Struct_type.
5682 // A hash table used to find identical unnamed structs so that they
5683 // share method tables.
5685 Struct_type::Identical_structs
Struct_type::identical_structs
;
5687 // A hash table used to merge method sets for identical unnamed
5690 Struct_type::Struct_method_tables
Struct_type::struct_method_tables
;
5695 Struct_type::do_traverse(Traverse
* traverse
)
5697 Struct_field_list
* fields
= this->fields_
;
5700 for (Struct_field_list::iterator p
= fields
->begin();
5704 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
5705 return TRAVERSE_EXIT
;
5708 return TRAVERSE_CONTINUE
;
5711 // Verify that the struct type is complete and valid.
5714 Struct_type::do_verify()
5716 Struct_field_list
* fields
= this->fields_
;
5719 for (Struct_field_list::iterator p
= fields
->begin();
5723 Type
* t
= p
->type();
5724 if (p
->is_anonymous())
5726 if ((t
->named_type() != NULL
&& t
->points_to() != NULL
)
5727 || (t
->named_type() == NULL
&& t
->points_to() != NULL
5728 && t
->points_to()->points_to() != NULL
))
5730 go_error_at(p
->location(), "embedded type may not be a pointer");
5731 p
->set_type(Type::make_error_type());
5733 else if (t
->points_to() != NULL
5734 && t
->points_to()->interface_type() != NULL
)
5736 go_error_at(p
->location(),
5737 "embedded type may not be pointer to interface");
5738 p
->set_type(Type::make_error_type());
5745 // Whether this contains a pointer.
5748 Struct_type::do_has_pointer() const
5750 const Struct_field_list
* fields
= this->fields();
5753 for (Struct_field_list::const_iterator p
= fields
->begin();
5757 if (p
->type()->has_pointer())
5763 // Whether this type is identical to T.
5766 Struct_type::is_identical(const Struct_type
* t
, int flags
) const
5768 if (this->is_struct_incomparable_
!= t
->is_struct_incomparable_
)
5770 const Struct_field_list
* fields1
= this->fields();
5771 const Struct_field_list
* fields2
= t
->fields();
5772 if (fields1
== NULL
|| fields2
== NULL
)
5773 return fields1
== fields2
;
5774 Struct_field_list::const_iterator pf2
= fields2
->begin();
5775 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
5776 pf1
!= fields1
->end();
5779 if (pf2
== fields2
->end())
5781 if (pf1
->field_name() != pf2
->field_name())
5783 if (pf1
->is_anonymous() != pf2
->is_anonymous()
5784 || !Type::are_identical(pf1
->type(), pf2
->type(), flags
, NULL
))
5786 if ((flags
& Type::COMPARE_TAGS
) != 0)
5788 if (!pf1
->has_tag())
5795 if (!pf2
->has_tag())
5797 if (pf1
->tag() != pf2
->tag())
5802 if (pf2
!= fields2
->end())
5807 // Whether comparisons of this struct type are simple identity
5811 Struct_type::do_compare_is_identity(Gogo
* gogo
)
5813 const Struct_field_list
* fields
= this->fields_
;
5817 for (Struct_field_list::const_iterator pf
= fields
->begin();
5818 pf
!= fields
->end();
5821 if (Gogo::is_sink_name(pf
->field_name()))
5824 if (!pf
->type()->compare_is_identity(gogo
))
5827 int64_t field_align
;
5828 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
5830 if ((offset
& (field_align
- 1)) != 0)
5832 // This struct has padding. We don't guarantee that that
5833 // padding is zero-initialized for a stack variable, so we
5834 // can't use memcmp to compare struct values.
5839 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
5841 offset
+= field_size
;
5844 int64_t struct_size
;
5845 if (!this->backend_type_size(gogo
, &struct_size
))
5847 if (offset
!= struct_size
)
5849 // Trailing padding may not be zero when on the stack.
5856 // Return whether this struct type is reflexive--whether a value of
5857 // this type is always equal to itself.
5860 Struct_type::do_is_reflexive()
5862 const Struct_field_list
* fields
= this->fields_
;
5865 for (Struct_field_list::const_iterator pf
= fields
->begin();
5866 pf
!= fields
->end();
5869 if (!pf
->type()->is_reflexive())
5875 // Return whether this struct type needs a key update when used as a
5879 Struct_type::do_needs_key_update()
5881 const Struct_field_list
* fields
= this->fields_
;
5884 for (Struct_field_list::const_iterator pf
= fields
->begin();
5885 pf
!= fields
->end();
5888 if (pf
->type()->needs_key_update())
5894 // Return whether computing the hash value of an instance of this
5895 // struct type might panic.
5898 Struct_type::do_hash_might_panic()
5900 const Struct_field_list
* fields
= this->fields_
;
5903 for (Struct_field_list::const_iterator pf
= fields
->begin();
5904 pf
!= fields
->end();
5907 if (pf
->type()->hash_might_panic())
5913 // Return whether this struct type is permitted to be in the heap.
5916 Struct_type::do_in_heap()
5918 const Struct_field_list
* fields
= this->fields_
;
5921 for (Struct_field_list::const_iterator pf
= fields
->begin();
5922 pf
!= fields
->end();
5925 if (!pf
->type()->in_heap())
5931 // Build identity and hash functions for this struct.
5936 Struct_type::do_hash_for_method(Gogo
* gogo
, int flags
) const
5938 unsigned int ret
= 0;
5939 if (this->fields() != NULL
)
5941 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
5942 pf
!= this->fields()->end();
5944 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
, flags
);
5947 if (this->is_struct_incomparable_
)
5952 // Find the local field NAME.
5955 Struct_type::find_local_field(const std::string
& name
,
5956 unsigned int *pindex
) const
5958 const Struct_field_list
* fields
= this->fields_
;
5962 for (Struct_field_list::const_iterator pf
= fields
->begin();
5963 pf
!= fields
->end();
5966 if (pf
->is_field_name(name
))
5976 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
5978 Field_reference_expression
*
5979 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
5980 Location location
) const
5983 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
5987 // Return an expression for a field, along with the depth at which it
5990 Field_reference_expression
*
5991 Struct_type::field_reference_depth(Expression
* struct_expr
,
5992 const std::string
& name
,
5994 Saw_named_type
* saw
,
5995 unsigned int* depth
) const
5997 const Struct_field_list
* fields
= this->fields_
;
6001 // Look for a field with this name.
6003 for (Struct_field_list::const_iterator pf
= fields
->begin();
6004 pf
!= fields
->end();
6007 if (pf
->is_field_name(name
))
6010 return Expression::make_field_reference(struct_expr
, i
, location
);
6014 // Look for an anonymous field which contains a field with this
6016 unsigned int found_depth
= 0;
6017 Field_reference_expression
* ret
= NULL
;
6019 for (Struct_field_list::const_iterator pf
= fields
->begin();
6020 pf
!= fields
->end();
6023 if (!pf
->is_anonymous())
6026 Struct_type
* st
= pf
->type()->deref()->struct_type();
6030 Saw_named_type
* hold_saw
= saw
;
6031 Saw_named_type saw_here
;
6032 Named_type
* nt
= pf
->type()->named_type();
6034 nt
= pf
->type()->deref()->named_type();
6038 for (q
= saw
; q
!= NULL
; q
= q
->next
)
6042 // If this is an error, it will be reported
6049 saw_here
.next
= saw
;
6054 // Look for a reference using a NULL struct expression. If we
6055 // find one, fill in the struct expression with a reference to
6057 unsigned int subdepth
;
6058 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
6068 if (ret
== NULL
|| subdepth
< found_depth
)
6073 found_depth
= subdepth
;
6074 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
6076 if (pf
->type()->points_to() != NULL
)
6077 here
= Expression::make_dereference(here
,
6078 Expression::NIL_CHECK_DEFAULT
,
6080 while (sub
->expr() != NULL
)
6082 sub
= sub
->expr()->deref()->field_reference_expression();
6083 go_assert(sub
!= NULL
);
6085 sub
->set_struct_expression(here
);
6086 sub
->set_implicit(true);
6088 else if (subdepth
> found_depth
)
6092 // We do not handle ambiguity here--it should be handled by
6093 // Type::bind_field_or_method.
6101 *depth
= found_depth
+ 1;
6106 // Return the total number of fields, including embedded fields.
6109 Struct_type::total_field_count() const
6111 if (this->fields_
== NULL
)
6113 unsigned int ret
= 0;
6114 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
6115 pf
!= this->fields_
->end();
6118 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
6121 ret
+= pf
->type()->struct_type()->total_field_count();
6126 // Return whether NAME is an unexported field, for better error reporting.
6129 Struct_type::is_unexported_local_field(Gogo
* gogo
,
6130 const std::string
& name
) const
6132 const Struct_field_list
* fields
= this->fields_
;
6135 for (Struct_field_list::const_iterator pf
= fields
->begin();
6136 pf
!= fields
->end();
6138 if (pf
->is_unexported_field_name(gogo
, name
))
6144 // Finalize the methods of an unnamed struct.
6147 Struct_type::finalize_methods(Gogo
* gogo
)
6149 if (this->all_methods_
!= NULL
)
6152 // It is possible to have multiple identical structs that have
6153 // methods. We want them to share method tables. Otherwise we will
6154 // emit identical methods more than once, which is bad since they
6155 // will even have the same names.
6156 std::pair
<Identical_structs::iterator
, bool> ins
=
6157 Struct_type::identical_structs
.insert(std::make_pair(this, this));
6160 // An identical struct was already entered into the hash table.
6161 // Note that finalize_methods is, fortunately, not recursive.
6162 this->all_methods_
= ins
.first
->second
->all_methods_
;
6166 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6169 // Return the method NAME, or NULL if there isn't one or if it is
6170 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6174 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6176 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6179 // Return a pointer to the interface method table for this type for
6180 // the interface INTERFACE. IS_POINTER is true if this is for a
6184 Struct_type::interface_method_table(Interface_type
* interface
,
6187 std::pair
<Struct_type
*, Struct_type::Struct_method_table_pair
*>
6189 std::pair
<Struct_type::Struct_method_tables::iterator
, bool> ins
=
6190 Struct_type::struct_method_tables
.insert(val
);
6192 Struct_method_table_pair
* smtp
;
6194 smtp
= ins
.first
->second
;
6197 smtp
= new Struct_method_table_pair();
6199 smtp
->second
= NULL
;
6200 ins
.first
->second
= smtp
;
6203 return Type::interface_method_table(this, interface
, is_pointer
,
6204 &smtp
->first
, &smtp
->second
);
6207 // Convert struct fields to the backend representation. This is not
6208 // declared in types.h so that types.h doesn't have to #include
6212 get_backend_struct_fields(Gogo
* gogo
, Struct_type
* type
, bool use_placeholder
,
6213 std::vector
<Backend::Btyped_identifier
>* bfields
)
6215 const Struct_field_list
* fields
= type
->fields();
6216 bfields
->resize(fields
->size());
6218 int64_t lastsize
= 0;
6219 bool saw_nonzero
= false;
6220 for (Struct_field_list::const_iterator p
= fields
->begin();
6224 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
6225 (*bfields
)[i
].btype
= (use_placeholder
6226 ? p
->type()->get_backend_placeholder(gogo
)
6227 : p
->type()->get_backend(gogo
));
6228 (*bfields
)[i
].location
= p
->location();
6229 lastsize
= gogo
->backend()->type_size((*bfields
)[i
].btype
);
6233 go_assert(i
== fields
->size());
6234 if (saw_nonzero
&& lastsize
== 0)
6236 // For nonzero-sized structs which end in a zero-sized thing, we add
6237 // an extra byte of padding to the type. This padding ensures that
6238 // taking the address of the zero-sized thing can't manufacture a
6239 // pointer to the next object in the heap. See issue 9401.
6240 size_t n
= fields
->size();
6241 bfields
->resize(n
+ 1);
6242 (*bfields
)[n
].name
= "_";
6243 (*bfields
)[n
].btype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
6244 (*bfields
)[n
].location
= (*bfields
)[n
-1].location
;
6245 type
->set_has_padding();
6249 // Get the backend representation for a struct type.
6252 Struct_type::do_get_backend(Gogo
* gogo
)
6254 std::vector
<Backend::Btyped_identifier
> bfields
;
6255 get_backend_struct_fields(gogo
, this, false, &bfields
);
6256 return gogo
->backend()->struct_type(bfields
);
6259 // Finish the backend representation of the fields of a struct.
6262 Struct_type::finish_backend_fields(Gogo
* gogo
)
6264 const Struct_field_list
* fields
= this->fields_
;
6267 for (Struct_field_list::const_iterator p
= fields
->begin();
6270 p
->type()->get_backend(gogo
);
6274 // The type of a struct type descriptor.
6277 Struct_type::make_struct_type_descriptor_type()
6282 Type
* tdt
= Type::make_type_descriptor_type();
6283 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6285 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6286 Type
* string_type
= Type::lookup_string_type();
6287 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6290 Type::make_builtin_struct_type(5,
6291 "name", pointer_string_type
,
6292 "pkgPath", pointer_string_type
,
6294 "tag", pointer_string_type
,
6295 "offsetAnon", uintptr_type
);
6296 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
6298 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
6300 Struct_type
* s
= Type::make_builtin_struct_type(2,
6302 "fields", slice_type
);
6304 ret
= Type::make_builtin_named_type("StructType", s
);
6310 // Build a type descriptor for a struct type.
6313 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6315 Location bloc
= Linemap::predeclared_location();
6317 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
6319 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
6321 Expression_list
* vals
= new Expression_list();
6324 const Methods
* methods
= this->methods();
6325 // A named struct should not have methods--the methods should attach
6326 // to the named type.
6327 go_assert(methods
== NULL
|| name
== NULL
);
6329 Struct_field_list::const_iterator ps
= fields
->begin();
6330 go_assert(ps
->is_field_name("_type"));
6331 vals
->push_back(this->type_descriptor_constructor(gogo
,
6332 RUNTIME_TYPE_KIND_STRUCT
,
6333 name
, methods
, true));
6336 go_assert(ps
->is_field_name("fields"));
6338 Expression_list
* elements
= new Expression_list();
6339 elements
->reserve(this->fields_
->size());
6340 Type
* element_type
= ps
->type()->array_type()->element_type();
6341 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
6342 pf
!= this->fields_
->end();
6345 const Struct_field_list
* f
= element_type
->struct_type()->fields();
6347 Expression_list
* fvals
= new Expression_list();
6350 Struct_field_list::const_iterator q
= f
->begin();
6351 go_assert(q
->is_field_name("name"));
6352 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
6353 Expression
* s
= Expression::make_string(n
, bloc
);
6354 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6357 go_assert(q
->is_field_name("pkgPath"));
6358 bool is_embedded_builtin
= pf
->is_embedded_builtin(gogo
);
6359 if (!Gogo::is_hidden_name(pf
->field_name()) && !is_embedded_builtin
)
6360 fvals
->push_back(Expression::make_nil(bloc
));
6364 if (is_embedded_builtin
)
6365 n
= gogo
->package_name();
6367 n
= Gogo::hidden_name_pkgpath(pf
->field_name());
6368 Expression
* s
= Expression::make_string(n
, bloc
);
6369 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6373 go_assert(q
->is_field_name("typ"));
6374 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
6377 go_assert(q
->is_field_name("tag"));
6379 fvals
->push_back(Expression::make_nil(bloc
));
6382 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
6383 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6387 go_assert(q
->is_field_name("offsetAnon"));
6388 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6389 Expression
* o
= Expression::make_struct_field_offset(this, &*pf
);
6390 Expression
* one
= Expression::make_integer_ul(1, uintptr_type
, bloc
);
6391 o
= Expression::make_binary(OPERATOR_LSHIFT
, o
, one
, bloc
);
6392 int av
= pf
->is_anonymous() ? 1 : 0;
6393 Expression
* anon
= Expression::make_integer_ul(av
, uintptr_type
, bloc
);
6394 o
= Expression::make_binary(OPERATOR_OR
, o
, anon
, bloc
);
6395 fvals
->push_back(o
);
6397 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
6399 elements
->push_back(v
);
6402 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
6405 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
6408 // Write the hash function for a struct which can not use the identity
6412 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
6413 Function_type
* hash_fntype
,
6414 Function_type
* equal_fntype
)
6416 Location bloc
= Linemap::predeclared_location();
6418 // The pointer to the struct that we are going to hash. This is an
6419 // argument to the hash function we are implementing here.
6420 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
6421 go_assert(key_arg
!= NULL
);
6422 Type
* key_arg_type
= key_arg
->var_value()->type();
6424 // The seed argument to the hash function.
6425 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
6426 go_assert(seed_arg
!= NULL
);
6428 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6430 // Make a temporary to hold the return value, initialized to the seed.
6431 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
6432 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
6434 gogo
->add_statement(retval
);
6436 // Make a temporary to hold the key as a uintptr.
6437 ref
= Expression::make_var_reference(key_arg
, bloc
);
6438 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
6439 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
6441 gogo
->add_statement(key
);
6443 // Loop over the struct fields.
6444 const Struct_field_list
* fields
= this->fields_
;
6445 for (Struct_field_list::const_iterator pf
= fields
->begin();
6446 pf
!= fields
->end();
6449 if (Gogo::is_sink_name(pf
->field_name()))
6452 // Get a pointer to the value of this field.
6453 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
6454 ref
= Expression::make_temporary_reference(key
, bloc
);
6455 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
6457 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
6459 // Get the hash function to use for the type of this field.
6460 Named_object
* hash_fn
;
6461 Named_object
* equal_fn
;
6462 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
6463 equal_fntype
, &hash_fn
, &equal_fn
);
6465 // Call the hash function for the field, passing retval as the seed.
6466 ref
= Expression::make_temporary_reference(retval
, bloc
);
6467 Expression_list
* args
= new Expression_list();
6468 args
->push_back(subkey
);
6469 args
->push_back(ref
);
6470 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
6471 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
6473 // Set retval to the result.
6474 Temporary_reference_expression
* tref
=
6475 Expression::make_temporary_reference(retval
, bloc
);
6476 tref
->set_is_lvalue();
6477 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
6478 gogo
->add_statement(s
);
6481 // Return retval to the caller of the hash function.
6482 Expression_list
* vals
= new Expression_list();
6483 ref
= Expression::make_temporary_reference(retval
, bloc
);
6484 vals
->push_back(ref
);
6485 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6486 gogo
->add_statement(s
);
6489 // Write the equality function for a struct which can not use the
6490 // identity function.
6493 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
6495 Location bloc
= Linemap::predeclared_location();
6497 // The pointers to the structs we are going to compare.
6498 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
6499 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
6500 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
6502 // Build temporaries with the right types.
6503 Type
* pt
= Type::make_pointer_type(name
!= NULL
6504 ? static_cast<Type
*>(name
)
6505 : static_cast<Type
*>(this));
6507 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
6508 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6509 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
6510 gogo
->add_statement(p1
);
6512 ref
= Expression::make_var_reference(key2_arg
, bloc
);
6513 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6514 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
6515 gogo
->add_statement(p2
);
6517 const Struct_field_list
* fields
= this->fields_
;
6518 unsigned int field_index
= 0;
6519 for (Struct_field_list::const_iterator pf
= fields
->begin();
6520 pf
!= fields
->end();
6521 ++pf
, ++field_index
)
6523 if (Gogo::is_sink_name(pf
->field_name()))
6526 // Compare one field in both P1 and P2.
6527 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
6528 f1
= Expression::make_dereference(f1
, Expression::NIL_CHECK_DEFAULT
,
6530 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
6532 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
6533 f2
= Expression::make_dereference(f2
, Expression::NIL_CHECK_DEFAULT
,
6535 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
6537 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
6539 // If the values are not equal, return false.
6540 gogo
->start_block(bloc
);
6541 Expression_list
* vals
= new Expression_list();
6542 vals
->push_back(Expression::make_boolean(false, bloc
));
6543 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6544 gogo
->add_statement(s
);
6545 Block
* then_block
= gogo
->finish_block(bloc
);
6547 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
6548 gogo
->add_statement(s
);
6551 // All the fields are equal, so return true.
6552 Expression_list
* vals
= new Expression_list();
6553 vals
->push_back(Expression::make_boolean(true, bloc
));
6554 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6555 gogo
->add_statement(s
);
6558 // Reflection string.
6561 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6563 ret
->append("struct {");
6565 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
6566 p
!= this->fields_
->end();
6569 if (p
!= this->fields_
->begin())
6570 ret
->push_back(';');
6571 ret
->push_back(' ');
6572 if (!p
->is_anonymous())
6574 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
6575 ret
->push_back(' ');
6577 if (p
->is_anonymous()
6578 && p
->type()->named_type() != NULL
6579 && p
->type()->named_type()->is_alias())
6580 p
->type()->named_type()->append_reflection_type_name(gogo
, true, ret
);
6582 this->append_reflection(p
->type(), gogo
, ret
);
6586 const std::string
& tag(p
->tag());
6588 for (std::string::const_iterator p
= tag
.begin();
6593 ret
->append("\\x00");
6594 else if (*p
== '\n')
6596 else if (*p
== '\t')
6599 ret
->append("\\\"");
6600 else if (*p
== '\\')
6601 ret
->append("\\\\");
6605 ret
->push_back('"');
6609 if (!this->fields_
->empty())
6610 ret
->push_back(' ');
6612 ret
->push_back('}');
6615 // If the offset of field INDEX in the backend implementation can be
6616 // determined, set *POFFSET to the offset in bytes and return true.
6617 // Otherwise, return false.
6620 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
6623 if (!this->is_backend_type_size_known(gogo
))
6625 Btype
* bt
= this->get_backend_placeholder(gogo
);
6626 *poffset
= gogo
->backend()->type_field_offset(bt
, index
);
6633 Struct_type::do_export(Export
* exp
) const
6635 exp
->write_c_string("struct { ");
6636 const Struct_field_list
* fields
= this->fields_
;
6637 go_assert(fields
!= NULL
);
6638 for (Struct_field_list::const_iterator p
= fields
->begin();
6642 if (p
->is_anonymous())
6643 exp
->write_string("? ");
6646 exp
->write_string(p
->field_name());
6647 exp
->write_c_string(" ");
6649 exp
->write_type(p
->type());
6653 exp
->write_c_string(" ");
6655 Expression::make_string(p
->tag(), Linemap::predeclared_location());
6657 Export_function_body
efb(exp
, 0);
6658 expr
->export_expression(&efb
);
6659 exp
->write_string(efb
.body());
6664 exp
->write_c_string("; ");
6666 exp
->write_c_string("}");
6672 Struct_type::do_import(Import
* imp
)
6674 imp
->require_c_string("struct { ");
6675 Struct_field_list
* fields
= new Struct_field_list
;
6676 if (imp
->peek_char() != '}')
6681 if (imp
->match_c_string("? "))
6685 name
= imp
->read_identifier();
6686 imp
->require_c_string(" ");
6688 Type
* ftype
= imp
->read_type();
6690 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
6691 sf
.set_is_imported();
6693 if (imp
->peek_char() == ' ')
6696 Expression
* expr
= Expression::import_expression(imp
,
6698 String_expression
* sexpr
= expr
->string_expression();
6699 go_assert(sexpr
!= NULL
);
6700 sf
.set_tag(sexpr
->val());
6704 imp
->require_c_string("; ");
6705 fields
->push_back(sf
);
6706 if (imp
->peek_char() == '}')
6710 imp
->require_c_string("}");
6712 return Type::make_struct_type(fields
, imp
->location());
6715 // Whether we can write this struct type to a C header file.
6716 // We can't if any of the fields are structs defined in a different package.
6719 Struct_type::can_write_to_c_header(
6720 std::vector
<const Named_object
*>* requires
,
6721 std::vector
<const Named_object
*>* declare
) const
6723 const Struct_field_list
* fields
= this->fields_
;
6724 if (fields
== NULL
|| fields
->empty())
6727 for (Struct_field_list::const_iterator p
= fields
->begin();
6731 if (p
->is_anonymous())
6733 if (!this->can_write_type_to_c_header(p
->type(), requires
, declare
))
6735 if (Gogo::message_name(p
->field_name()) == "_")
6743 // Whether we can write the type T to a C header file.
6746 Struct_type::can_write_type_to_c_header(
6748 std::vector
<const Named_object
*>* requires
,
6749 std::vector
<const Named_object
*>* declare
) const
6752 switch (t
->classification())
6767 case TYPE_INTERFACE
:
6771 // Don't try to handle a pointer to an array.
6772 if (t
->points_to()->array_type() != NULL
6773 && !t
->points_to()->is_slice_type())
6776 if (t
->points_to()->named_type() != NULL
6777 && t
->points_to()->struct_type() != NULL
)
6778 declare
->push_back(t
->points_to()->named_type()->named_object());
6782 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6785 if (t
->is_slice_type())
6787 return this->can_write_type_to_c_header(t
->array_type()->element_type(),
6792 const Named_object
* no
= t
->named_type()->named_object();
6793 if (no
->package() != NULL
)
6795 if (t
->is_unsafe_pointer_type())
6799 if (t
->struct_type() != NULL
)
6801 requires
->push_back(no
);
6802 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6804 return this->can_write_type_to_c_header(t
->base(), requires
, declare
);
6807 case TYPE_CALL_MULTIPLE_RESULT
:
6815 // Write this struct to a C header file.
6818 Struct_type::write_to_c_header(std::ostream
& os
) const
6820 const Struct_field_list
* fields
= this->fields_
;
6821 for (Struct_field_list::const_iterator p
= fields
->begin();
6826 this->write_field_to_c_header(os
, p
->field_name(), p
->type());
6827 os
<< ';' << std::endl
;
6831 // Write the type of a struct field to a C header file.
6834 Struct_type::write_field_to_c_header(std::ostream
& os
, const std::string
& name
,
6835 const Type
*t
) const
6837 bool print_name
= true;
6839 switch (t
->classification())
6851 const Integer_type
* it
= t
->integer_type();
6852 if (it
->is_unsigned())
6854 os
<< "int" << it
->bits() << "_t";
6859 switch (t
->float_type()->bits())
6873 switch (t
->complex_type()->bits())
6876 os
<< "float _Complex";
6879 os
<< "double _Complex";
6896 std::vector
<const Named_object
*> requires
;
6897 std::vector
<const Named_object
*> declare
;
6898 if (!this->can_write_type_to_c_header(t
->points_to(), &requires
,
6903 this->write_field_to_c_header(os
, "", t
->points_to());
6917 case TYPE_INTERFACE
:
6918 if (t
->interface_type()->is_empty())
6925 os
<< "struct {" << std::endl
;
6926 t
->struct_type()->write_to_c_header(os
);
6931 if (t
->is_slice_type())
6935 const Type
*ele
= t
;
6936 std::vector
<const Type
*> array_types
;
6937 while (ele
->array_type() != NULL
&& !ele
->is_slice_type())
6939 array_types
.push_back(ele
);
6940 ele
= ele
->array_type()->element_type();
6942 this->write_field_to_c_header(os
, "", ele
);
6943 os
<< ' ' << Gogo::message_name(name
);
6945 while (!array_types
.empty())
6947 ele
= array_types
.back();
6948 array_types
.pop_back();
6950 Numeric_constant nc
;
6951 if (!ele
->array_type()->length()->numeric_constant_value(&nc
))
6954 if (!nc
.to_int(&val
))
6956 char* s
= mpz_get_str(NULL
, 10, val
);
6967 const Named_object
* no
= t
->named_type()->named_object();
6968 if (t
->struct_type() != NULL
)
6969 os
<< "struct " << no
->message_name();
6970 else if (t
->is_unsafe_pointer_type())
6972 else if (t
== Type::lookup_integer_type("uintptr"))
6976 this->write_field_to_c_header(os
, name
, t
->base());
6984 case TYPE_CALL_MULTIPLE_RESULT
:
6991 if (print_name
&& !name
.empty())
6992 os
<< ' ' << Gogo::message_name(name
);
6995 // Make a struct type.
6998 Type::make_struct_type(Struct_field_list
* fields
,
7001 return new Struct_type(fields
, location
);
7004 // Class Array_type.
7006 // Store the length of an array as an int64_t into *PLEN. Return
7007 // false if the length can not be determined. This will assert if
7008 // called for a slice.
7011 Array_type::int_length(int64_t* plen
) const
7013 go_assert(this->length_
!= NULL
);
7014 Numeric_constant nc
;
7015 if (!this->length_
->numeric_constant_value(&nc
))
7017 return nc
.to_memory_size(plen
);
7020 // Whether two array types are identical.
7023 Array_type::is_identical(const Array_type
* t
, int flags
) const
7025 if (!Type::are_identical(this->element_type(), t
->element_type(),
7029 if (this->is_array_incomparable_
!= t
->is_array_incomparable_
)
7032 Expression
* l1
= this->length();
7033 Expression
* l2
= t
->length();
7035 // Slices of the same element type are identical.
7036 if (l1
== NULL
&& l2
== NULL
)
7039 // Arrays of the same element type are identical if they have the
7041 if (l1
!= NULL
&& l2
!= NULL
)
7046 // Try to determine the lengths. If we can't, assume the arrays
7047 // are not identical.
7049 Numeric_constant nc1
, nc2
;
7050 if (l1
->numeric_constant_value(&nc1
)
7051 && l2
->numeric_constant_value(&nc2
))
7054 if (nc1
.to_int(&v1
))
7057 if (nc2
.to_int(&v2
))
7059 ret
= mpz_cmp(v1
, v2
) == 0;
7068 // Otherwise the arrays are not identical.
7075 Array_type::do_traverse(Traverse
* traverse
)
7077 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
7078 return TRAVERSE_EXIT
;
7079 if (this->length_
!= NULL
7080 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
7081 return TRAVERSE_EXIT
;
7082 return TRAVERSE_CONTINUE
;
7085 // Check that the length is valid.
7088 Array_type::verify_length()
7090 if (this->length_
== NULL
)
7093 Type_context
context(Type::lookup_integer_type("int"), false);
7094 this->length_
->determine_type(&context
);
7096 if (!this->length_
->is_constant())
7098 go_error_at(this->length_
->location(), "array bound is not constant");
7102 // For array types, the length expression can be an untyped constant
7103 // representable as an int, but we don't allow explicitly non-integer
7104 // values such as "float64(10)". See issues #13485 and #13486.
7105 if (this->length_
->type()->integer_type() == NULL
7106 && !this->length_
->type()->is_error_type())
7108 go_error_at(this->length_
->location(), "invalid array bound");
7112 Numeric_constant nc
;
7113 if (!this->length_
->numeric_constant_value(&nc
))
7115 if (this->length_
->type()->integer_type() != NULL
7116 || this->length_
->type()->float_type() != NULL
)
7117 go_error_at(this->length_
->location(), "array bound is not constant");
7119 go_error_at(this->length_
->location(), "array bound is not numeric");
7123 Type
* int_type
= Type::lookup_integer_type("int");
7124 unsigned int tbits
= int_type
->integer_type()->bits();
7126 switch (nc
.to_unsigned_long(&val
))
7128 case Numeric_constant::NC_UL_VALID
:
7129 if (sizeof(val
) >= tbits
/ 8 && val
>> (tbits
- 1) != 0)
7131 go_error_at(this->length_
->location(), "array bound overflows");
7135 case Numeric_constant::NC_UL_NOTINT
:
7136 go_error_at(this->length_
->location(), "array bound truncated to integer");
7138 case Numeric_constant::NC_UL_NEGATIVE
:
7139 go_error_at(this->length_
->location(), "negative array bound");
7141 case Numeric_constant::NC_UL_BIG
:
7144 if (!nc
.to_int(&val
))
7146 unsigned int bits
= mpz_sizeinbase(val
, 2);
7150 go_error_at(this->length_
->location(), "array bound overflows");
7165 Array_type::do_verify()
7167 if (this->element_type()->is_error_type())
7169 if (!this->verify_length())
7170 this->length_
= Expression::make_error(this->length_
->location());
7174 // Whether the type contains pointers. This is always true for a
7175 // slice. For an array it is true if the element type has pointers
7176 // and the length is greater than zero.
7179 Array_type::do_has_pointer() const
7181 if (this->length_
== NULL
)
7183 if (!this->element_type_
->has_pointer())
7186 Numeric_constant nc
;
7187 if (!this->length_
->numeric_constant_value(&nc
))
7189 // Error reported elsewhere.
7194 switch (nc
.to_unsigned_long(&val
))
7196 case Numeric_constant::NC_UL_VALID
:
7198 case Numeric_constant::NC_UL_BIG
:
7201 // Error reported elsewhere.
7206 // Whether we can use memcmp to compare this array.
7209 Array_type::do_compare_is_identity(Gogo
* gogo
)
7211 if (this->length_
== NULL
)
7214 // Check for [...], which indicates that this is not a real type.
7215 if (this->length_
->is_nil_expression())
7218 if (!this->element_type_
->compare_is_identity(gogo
))
7221 // If there is any padding, then we can't use memcmp.
7224 if (!this->element_type_
->backend_type_size(gogo
, &size
)
7225 || !this->element_type_
->backend_type_align(gogo
, &align
))
7227 if ((size
& (align
- 1)) != 0)
7233 // Array type hash code.
7236 Array_type::do_hash_for_method(Gogo
* gogo
, int flags
) const
7240 // There is no very convenient way to get a hash code for the
7242 ret
= this->element_type_
->hash_for_method(gogo
, flags
) + 1;
7243 if (this->is_array_incomparable_
)
7248 // Write the hash function for an array which can not use the identify
7252 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
7253 Function_type
* hash_fntype
,
7254 Function_type
* equal_fntype
)
7256 Location bloc
= Linemap::predeclared_location();
7258 // The pointer to the array that we are going to hash. This is an
7259 // argument to the hash function we are implementing here.
7260 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
7261 go_assert(key_arg
!= NULL
);
7262 Type
* key_arg_type
= key_arg
->var_value()->type();
7264 // The seed argument to the hash function.
7265 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
7266 go_assert(seed_arg
!= NULL
);
7268 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7270 // Make a temporary to hold the return value, initialized to the seed.
7271 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
7272 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
7274 gogo
->add_statement(retval
);
7276 // Make a temporary to hold the key as a uintptr.
7277 ref
= Expression::make_var_reference(key_arg
, bloc
);
7278 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
7279 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
7281 gogo
->add_statement(key
);
7283 // Loop over the array elements.
7285 Type
* int_type
= Type::lookup_integer_type("int");
7286 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7287 gogo
->add_statement(index
);
7289 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7290 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
7291 Type
* pt
= Type::make_pointer_type(name
!= NULL
7292 ? static_cast<Type
*>(name
)
7293 : static_cast<Type
*>(this));
7294 aref
= Expression::make_cast(pt
, aref
, bloc
);
7295 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7300 gogo
->start_block(bloc
);
7302 // Get the hash function for the element type.
7303 Named_object
* hash_fn
;
7304 Named_object
* equal_fn
;
7305 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
7306 hash_fntype
, equal_fntype
, &hash_fn
,
7309 // Get a pointer to this element in the loop.
7310 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
7311 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
7313 // Get the size of each element.
7314 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
7315 Expression::TYPE_INFO_SIZE
);
7317 // Get the hash of this element, passing retval as the seed.
7318 ref
= Expression::make_temporary_reference(retval
, bloc
);
7319 Expression_list
* args
= new Expression_list();
7320 args
->push_back(subkey
);
7321 args
->push_back(ref
);
7322 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
7323 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
7325 // Set retval to the result.
7326 Temporary_reference_expression
* tref
=
7327 Expression::make_temporary_reference(retval
, bloc
);
7328 tref
->set_is_lvalue();
7329 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
7330 gogo
->add_statement(s
);
7332 // Increase the element pointer.
7333 tref
= Expression::make_temporary_reference(key
, bloc
);
7334 tref
->set_is_lvalue();
7335 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
7337 Block
* statements
= gogo
->finish_block(bloc
);
7339 for_range
->add_statements(statements
);
7340 gogo
->add_statement(for_range
);
7342 // Return retval to the caller of the hash function.
7343 Expression_list
* vals
= new Expression_list();
7344 ref
= Expression::make_temporary_reference(retval
, bloc
);
7345 vals
->push_back(ref
);
7346 s
= Statement::make_return_statement(vals
, bloc
);
7347 gogo
->add_statement(s
);
7350 // Write the equality function for an array which can not use the
7351 // identity function.
7354 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
7356 Location bloc
= Linemap::predeclared_location();
7358 // The pointers to the arrays we are going to compare.
7359 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
7360 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
7361 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
7363 // Build temporaries for the keys with the right types.
7364 Type
* pt
= Type::make_pointer_type(name
!= NULL
7365 ? static_cast<Type
*>(name
)
7366 : static_cast<Type
*>(this));
7368 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
7369 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7370 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
7371 gogo
->add_statement(p1
);
7373 ref
= Expression::make_var_reference(key2_arg
, bloc
);
7374 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7375 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
7376 gogo
->add_statement(p2
);
7378 // Loop over the array elements.
7380 Type
* int_type
= Type::lookup_integer_type("int");
7381 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7382 gogo
->add_statement(index
);
7384 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7385 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
7386 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7391 gogo
->start_block(bloc
);
7393 // Compare element in P1 and P2.
7394 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
7395 e1
= Expression::make_dereference(e1
, Expression::NIL_CHECK_DEFAULT
, bloc
);
7396 ref
= Expression::make_temporary_reference(index
, bloc
);
7397 e1
= Expression::make_array_index(e1
, ref
, NULL
, NULL
, bloc
);
7399 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
7400 e2
= Expression::make_dereference(e2
, Expression::NIL_CHECK_DEFAULT
, bloc
);
7401 ref
= Expression::make_temporary_reference(index
, bloc
);
7402 e2
= Expression::make_array_index(e2
, ref
, NULL
, NULL
, bloc
);
7404 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
7406 // If the elements are not equal, return false.
7407 gogo
->start_block(bloc
);
7408 Expression_list
* vals
= new Expression_list();
7409 vals
->push_back(Expression::make_boolean(false, bloc
));
7410 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
7411 gogo
->add_statement(s
);
7412 Block
* then_block
= gogo
->finish_block(bloc
);
7414 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
7415 gogo
->add_statement(s
);
7417 Block
* statements
= gogo
->finish_block(bloc
);
7419 for_range
->add_statements(statements
);
7420 gogo
->add_statement(for_range
);
7422 // All the elements are equal, so return true.
7423 vals
= new Expression_list();
7424 vals
->push_back(Expression::make_boolean(true, bloc
));
7425 s
= Statement::make_return_statement(vals
, bloc
);
7426 gogo
->add_statement(s
);
7429 // Get the backend representation of the fields of a slice. This is
7430 // not declared in types.h so that types.h doesn't have to #include
7433 // We use int for the count and capacity fields. This matches 6g.
7434 // The language more or less assumes that we can't allocate space of a
7435 // size which does not fit in int.
7438 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
7439 std::vector
<Backend::Btyped_identifier
>* bfields
)
7443 Type
* pet
= Type::make_pointer_type(type
->element_type());
7444 Btype
* pbet
= (use_placeholder
7445 ? pet
->get_backend_placeholder(gogo
)
7446 : pet
->get_backend(gogo
));
7447 Location ploc
= Linemap::predeclared_location();
7449 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
7450 p
->name
= "__values";
7454 Type
* int_type
= Type::lookup_integer_type("int");
7457 p
->name
= "__count";
7458 p
->btype
= int_type
->get_backend(gogo
);
7462 p
->name
= "__capacity";
7463 p
->btype
= int_type
->get_backend(gogo
);
7467 // Get the backend representation for the type of this array. A fixed array is
7468 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7469 // just like an array in C. An open array is a struct with three
7470 // fields: a data pointer, the length, and the capacity.
7473 Array_type::do_get_backend(Gogo
* gogo
)
7475 if (this->length_
== NULL
)
7477 std::vector
<Backend::Btyped_identifier
> bfields
;
7478 get_backend_slice_fields(gogo
, this, false, &bfields
);
7479 return gogo
->backend()->struct_type(bfields
);
7483 Btype
* element
= this->get_backend_element(gogo
, false);
7484 Bexpression
* len
= this->get_backend_length(gogo
);
7485 return gogo
->backend()->array_type(element
, len
);
7489 // Return the backend representation of the element type.
7492 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
7494 if (use_placeholder
)
7495 return this->element_type_
->get_backend_placeholder(gogo
);
7497 return this->element_type_
->get_backend(gogo
);
7500 // Return the backend representation of the length. The length may be
7501 // computed using a function call, so we must only evaluate it once.
7504 Array_type::get_backend_length(Gogo
* gogo
)
7506 go_assert(this->length_
!= NULL
);
7507 if (this->blength_
== NULL
)
7509 if (this->length_
->is_error_expression())
7511 this->blength_
= gogo
->backend()->error_expression();
7512 return this->blength_
;
7514 Numeric_constant nc
;
7516 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
7518 if (mpz_sgn(val
) < 0)
7520 this->blength_
= gogo
->backend()->error_expression();
7521 return this->blength_
;
7523 Type
* t
= nc
.type();
7525 t
= Type::lookup_integer_type("int");
7526 else if (t
->is_abstract())
7527 t
= t
->make_non_abstract_type();
7528 Btype
* btype
= t
->get_backend(gogo
);
7530 gogo
->backend()->integer_constant_expression(btype
, val
);
7535 // Make up a translation context for the array length
7536 // expression. FIXME: This won't work in general.
7537 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7538 this->blength_
= this->length_
->get_backend(&context
);
7540 Btype
* ibtype
= Type::lookup_integer_type("int")->get_backend(gogo
);
7542 gogo
->backend()->convert_expression(ibtype
, this->blength_
,
7543 this->length_
->location());
7546 return this->blength_
;
7549 // Finish backend representation of the array.
7552 Array_type::finish_backend_element(Gogo
* gogo
)
7554 Type
* et
= this->array_type()->element_type();
7555 et
->get_backend(gogo
);
7556 if (this->is_slice_type())
7558 // This relies on the fact that we always use the same
7559 // structure for a pointer to any given type.
7560 Type
* pet
= Type::make_pointer_type(et
);
7561 pet
->get_backend(gogo
);
7565 // Return an expression for a pointer to the values in ARRAY.
7568 Array_type::get_value_pointer(Gogo
*, Expression
* array
, bool is_lvalue
) const
7570 if (this->length() != NULL
)
7573 go_assert(array
->type()->array_type() != NULL
);
7574 Type
* etype
= array
->type()->array_type()->element_type();
7575 array
= Expression::make_unary(OPERATOR_AND
, array
, array
->location());
7576 return Expression::make_cast(Type::make_pointer_type(etype
), array
,
7584 Temporary_reference_expression
* tref
=
7585 array
->temporary_reference_expression();
7586 Var_expression
* ve
= array
->var_expression();
7589 tref
= tref
->copy()->temporary_reference_expression();
7590 tref
->set_is_lvalue();
7593 else if (ve
!= NULL
)
7595 ve
= new Var_expression(ve
->named_object(), ve
->location());
7600 return Expression::make_slice_info(array
,
7601 Expression::SLICE_INFO_VALUE_POINTER
,
7605 // Return an expression for the length of the array ARRAY which has this
7609 Array_type::get_length(Gogo
*, Expression
* array
) const
7611 if (this->length_
!= NULL
)
7612 return this->length_
;
7614 // This is a slice. We need to read the length field.
7615 return Expression::make_slice_info(array
, Expression::SLICE_INFO_LENGTH
,
7619 // Return an expression for the capacity of the array ARRAY which has this
7623 Array_type::get_capacity(Gogo
*, Expression
* array
) const
7625 if (this->length_
!= NULL
)
7626 return this->length_
;
7628 // This is a slice. We need to read the capacity field.
7629 return Expression::make_slice_info(array
, Expression::SLICE_INFO_CAPACITY
,
7636 Array_type::do_export(Export
* exp
) const
7638 exp
->write_c_string("[");
7639 if (this->length_
!= NULL
)
7641 Numeric_constant nc
;
7643 if (!this->length_
->numeric_constant_value(&nc
) || !nc
.to_int(&val
))
7645 go_assert(saw_errors());
7648 char* s
= mpz_get_str(NULL
, 10, val
);
7649 exp
->write_string(s
);
7650 exp
->write_string(" ");
7653 exp
->write_c_string("] ");
7654 exp
->write_type(this->element_type_
);
7660 Array_type::do_import(Import
* imp
)
7662 imp
->require_c_string("[");
7664 if (imp
->peek_char() == ']')
7667 length
= Expression::import_expression(imp
, imp
->location());
7668 imp
->require_c_string("] ");
7669 Type
* element_type
= imp
->read_type();
7670 return Type::make_array_type(element_type
, length
);
7673 // The type of an array type descriptor.
7676 Array_type::make_array_type_descriptor_type()
7681 Type
* tdt
= Type::make_type_descriptor_type();
7682 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7684 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7687 Type::make_builtin_struct_type(4,
7691 "len", uintptr_type
);
7693 ret
= Type::make_builtin_named_type("ArrayType", sf
);
7699 // The type of an slice type descriptor.
7702 Array_type::make_slice_type_descriptor_type()
7707 Type
* tdt
= Type::make_type_descriptor_type();
7708 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7711 Type::make_builtin_struct_type(2,
7715 ret
= Type::make_builtin_named_type("SliceType", sf
);
7721 // Build a type descriptor for an array/slice type.
7724 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7726 if (this->length_
!= NULL
)
7727 return this->array_type_descriptor(gogo
, name
);
7729 return this->slice_type_descriptor(gogo
, name
);
7732 // Build a type descriptor for an array type.
7735 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7737 Location bloc
= Linemap::predeclared_location();
7739 Type
* atdt
= Array_type::make_array_type_descriptor_type();
7741 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
7743 Expression_list
* vals
= new Expression_list();
7746 Struct_field_list::const_iterator p
= fields
->begin();
7747 go_assert(p
->is_field_name("_type"));
7748 vals
->push_back(this->type_descriptor_constructor(gogo
,
7749 RUNTIME_TYPE_KIND_ARRAY
,
7753 go_assert(p
->is_field_name("elem"));
7754 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7757 go_assert(p
->is_field_name("slice"));
7758 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
7759 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
7762 go_assert(p
->is_field_name("len"));
7763 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
7766 go_assert(p
== fields
->end());
7768 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
7771 // Build a type descriptor for a slice type.
7774 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7776 Location bloc
= Linemap::predeclared_location();
7778 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
7780 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
7782 Expression_list
* vals
= new Expression_list();
7785 Struct_field_list::const_iterator p
= fields
->begin();
7786 go_assert(p
->is_field_name("_type"));
7787 vals
->push_back(this->type_descriptor_constructor(gogo
,
7788 RUNTIME_TYPE_KIND_SLICE
,
7792 go_assert(p
->is_field_name("elem"));
7793 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7796 go_assert(p
== fields
->end());
7798 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
7801 // Reflection string.
7804 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7806 ret
->push_back('[');
7807 if (this->length_
!= NULL
)
7809 Numeric_constant nc
;
7810 if (!this->length_
->numeric_constant_value(&nc
))
7812 go_assert(saw_errors());
7816 if (!nc
.to_int(&val
))
7818 go_assert(saw_errors());
7821 char* s
= mpz_get_str(NULL
, 10, val
);
7826 ret
->push_back(']');
7828 this->append_reflection(this->element_type_
, gogo
, ret
);
7831 // Make an array type.
7834 Type::make_array_type(Type
* element_type
, Expression
* length
)
7836 return new Array_type(element_type
, length
);
7841 Named_object
* Map_type::zero_value
;
7842 int64_t Map_type::zero_value_size
;
7843 int64_t Map_type::zero_value_align
;
7845 // If this map requires the "fat" functions, return the pointer to
7846 // pass as the zero value to those functions. Otherwise, in the
7847 // normal case, return NULL. The map requires the "fat" functions if
7848 // the value size is larger than max_zero_size bytes. max_zero_size
7849 // must match maxZero in libgo/go/runtime/hashmap.go.
7852 Map_type::fat_zero_value(Gogo
* gogo
)
7855 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
7857 go_assert(saw_errors());
7860 if (valsize
<= Map_type::max_zero_size
)
7863 if (Map_type::zero_value_size
< valsize
)
7864 Map_type::zero_value_size
= valsize
;
7867 if (!this->val_type_
->backend_type_align(gogo
, &valalign
))
7869 go_assert(saw_errors());
7873 if (Map_type::zero_value_align
< valalign
)
7874 Map_type::zero_value_align
= valalign
;
7876 Location bloc
= Linemap::predeclared_location();
7878 if (Map_type::zero_value
== NULL
)
7880 // The final type will be set in backend_zero_value.
7881 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7882 Expression
* size
= Expression::make_integer_ul(0, NULL
, bloc
);
7883 Array_type
* array_type
= Type::make_array_type(uint8_type
, size
);
7884 array_type
->set_is_array_incomparable();
7885 Variable
* var
= new Variable(array_type
, NULL
, true, false, false, bloc
);
7886 std::string name
= gogo
->map_zero_value_name();
7887 Map_type::zero_value
= Named_object::make_variable(name
, NULL
, var
);
7890 Expression
* z
= Expression::make_var_reference(Map_type::zero_value
, bloc
);
7891 z
= Expression::make_unary(OPERATOR_AND
, z
, bloc
);
7892 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
7893 z
= Expression::make_cast(unsafe_ptr_type
, z
, bloc
);
7897 // Return whether VAR is the map zero value.
7900 Map_type::is_zero_value(Variable
* var
)
7902 return (Map_type::zero_value
!= NULL
7903 && Map_type::zero_value
->var_value() == var
);
7906 // Return the backend representation for the zero value.
7909 Map_type::backend_zero_value(Gogo
* gogo
)
7911 Location bloc
= Linemap::predeclared_location();
7913 go_assert(Map_type::zero_value
!= NULL
);
7915 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7916 Btype
* buint8_type
= uint8_type
->get_backend(gogo
);
7918 Type
* int_type
= Type::lookup_integer_type("int");
7920 Expression
* e
= Expression::make_integer_int64(Map_type::zero_value_size
,
7922 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7923 Bexpression
* blength
= e
->get_backend(&context
);
7925 Btype
* barray_type
= gogo
->backend()->array_type(buint8_type
, blength
);
7927 std::string zname
= Map_type::zero_value
->name();
7928 std::string
asm_name(go_selectively_encode_id(zname
));
7930 gogo
->backend()->implicit_variable(zname
, asm_name
,
7931 barray_type
, false, false, true,
7932 Map_type::zero_value_align
);
7933 gogo
->backend()->implicit_variable_set_init(zvar
, zname
, barray_type
,
7934 false, false, true, NULL
);
7941 Map_type::do_traverse(Traverse
* traverse
)
7943 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
7944 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
7945 return TRAVERSE_EXIT
;
7946 return TRAVERSE_CONTINUE
;
7949 // Check that the map type is OK.
7952 Map_type::do_verify()
7954 // The runtime support uses "map[void]void".
7955 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
7956 go_error_at(this->location_
, "invalid map key type");
7957 if (!this->key_type_
->in_heap())
7958 go_error_at(this->location_
, "go:notinheap map key not allowed");
7959 if (!this->val_type_
->in_heap())
7960 go_error_at(this->location_
, "go:notinheap map value not allowed");
7964 // Whether two map types are identical.
7967 Map_type::is_identical(const Map_type
* t
, int flags
) const
7969 return (Type::are_identical(this->key_type(), t
->key_type(), flags
, NULL
)
7970 && Type::are_identical(this->val_type(), t
->val_type(), flags
,
7977 Map_type::do_hash_for_method(Gogo
* gogo
, int flags
) const
7979 return (this->key_type_
->hash_for_method(gogo
, flags
)
7980 + this->val_type_
->hash_for_method(gogo
, flags
)
7984 // Get the backend representation for a map type. A map type is
7985 // represented as a pointer to a struct. The struct is hmap in
7986 // runtime/hashmap.go.
7989 Map_type::do_get_backend(Gogo
* gogo
)
7991 static Btype
* backend_map_type
;
7992 if (backend_map_type
== NULL
)
7994 std::vector
<Backend::Btyped_identifier
> bfields(9);
7996 Location bloc
= Linemap::predeclared_location();
7998 Type
* int_type
= Type::lookup_integer_type("int");
7999 bfields
[0].name
= "count";
8000 bfields
[0].btype
= int_type
->get_backend(gogo
);
8001 bfields
[0].location
= bloc
;
8003 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8004 bfields
[1].name
= "flags";
8005 bfields
[1].btype
= uint8_type
->get_backend(gogo
);
8006 bfields
[1].location
= bloc
;
8008 bfields
[2].name
= "B";
8009 bfields
[2].btype
= bfields
[1].btype
;
8010 bfields
[2].location
= bloc
;
8012 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8013 bfields
[3].name
= "noverflow";
8014 bfields
[3].btype
= uint16_type
->get_backend(gogo
);
8015 bfields
[3].location
= bloc
;
8017 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8018 bfields
[4].name
= "hash0";
8019 bfields
[4].btype
= uint32_type
->get_backend(gogo
);
8020 bfields
[4].location
= bloc
;
8022 Btype
* bvt
= gogo
->backend()->void_type();
8023 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
8024 bfields
[5].name
= "buckets";
8025 bfields
[5].btype
= bpvt
;
8026 bfields
[5].location
= bloc
;
8028 bfields
[6].name
= "oldbuckets";
8029 bfields
[6].btype
= bpvt
;
8030 bfields
[6].location
= bloc
;
8032 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8033 bfields
[7].name
= "nevacuate";
8034 bfields
[7].btype
= uintptr_type
->get_backend(gogo
);
8035 bfields
[7].location
= bloc
;
8037 bfields
[8].name
= "extra";
8038 bfields
[8].btype
= bpvt
;
8039 bfields
[8].location
= bloc
;
8041 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
8042 bt
= gogo
->backend()->named_type("runtime.hmap", bt
, bloc
);
8043 backend_map_type
= gogo
->backend()->pointer_type(bt
);
8045 return backend_map_type
;
8048 // The type of a map type descriptor.
8051 Map_type::make_map_type_descriptor_type()
8056 Type
* tdt
= Type::make_type_descriptor_type();
8057 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
8058 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8059 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8060 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8063 Type::make_builtin_struct_type(8,
8068 "keysize", uint8_type
,
8069 "valuesize", uint8_type
,
8070 "bucketsize", uint16_type
,
8071 "flags", uint32_type
);
8073 ret
= Type::make_builtin_named_type("MapType", sf
);
8079 // Build a type descriptor for a map type.
8082 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8084 Location bloc
= Linemap::predeclared_location();
8086 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
8087 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8088 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8089 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8092 if (!this->key_type_
->backend_type_size(gogo
, &keysize
))
8094 go_error_at(this->location_
, "error determining map key type size");
8095 return Expression::make_error(this->location_
);
8099 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
8101 go_error_at(this->location_
, "error determining map value type size");
8102 return Expression::make_error(this->location_
);
8106 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptrsize
))
8108 go_assert(saw_errors());
8109 return Expression::make_error(this->location_
);
8112 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
8113 if (bucket_type
== NULL
)
8115 go_assert(saw_errors());
8116 return Expression::make_error(this->location_
);
8120 if (!bucket_type
->backend_type_size(gogo
, &bucketsize
))
8122 go_assert(saw_errors());
8123 return Expression::make_error(this->location_
);
8126 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
8128 Expression_list
* vals
= new Expression_list();
8131 Struct_field_list::const_iterator p
= fields
->begin();
8132 go_assert(p
->is_field_name("_type"));
8133 vals
->push_back(this->type_descriptor_constructor(gogo
,
8134 RUNTIME_TYPE_KIND_MAP
,
8138 go_assert(p
->is_field_name("key"));
8139 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
8142 go_assert(p
->is_field_name("elem"));
8143 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
8146 go_assert(p
->is_field_name("bucket"));
8147 vals
->push_back(Expression::make_type_descriptor(bucket_type
, bloc
));
8150 go_assert(p
->is_field_name("keysize"));
8151 if (keysize
> Map_type::max_key_size
)
8152 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
8154 vals
->push_back(Expression::make_integer_int64(keysize
, uint8_type
, bloc
));
8157 go_assert(p
->is_field_name("valuesize"));
8158 if (valsize
> Map_type::max_val_size
)
8159 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
8161 vals
->push_back(Expression::make_integer_int64(valsize
, uint8_type
, bloc
));
8164 go_assert(p
->is_field_name("bucketsize"));
8165 vals
->push_back(Expression::make_integer_int64(bucketsize
, uint16_type
,
8169 go_assert(p
->is_field_name("flags"));
8170 // As with the other fields, the flag bits must match the reflect
8171 // and runtime packages.
8172 unsigned long flags
= 0;
8173 if (keysize
> Map_type::max_key_size
)
8175 if (valsize
> Map_type::max_val_size
)
8177 if (this->key_type_
->is_reflexive())
8179 if (this->key_type_
->needs_key_update())
8181 if (this->key_type_
->hash_might_panic())
8183 vals
->push_back(Expression::make_integer_ul(flags
, uint32_type
, bloc
));
8186 go_assert(p
== fields
->end());
8188 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
8191 // Return the bucket type to use for a map type. This must correspond
8192 // to libgo/go/runtime/hashmap.go.
8195 Map_type::bucket_type(Gogo
* gogo
, int64_t keysize
, int64_t valsize
)
8197 if (this->bucket_type_
!= NULL
)
8198 return this->bucket_type_
;
8200 Type
* key_type
= this->key_type_
;
8201 if (keysize
> Map_type::max_key_size
)
8202 key_type
= Type::make_pointer_type(key_type
);
8204 Type
* val_type
= this->val_type_
;
8205 if (valsize
> Map_type::max_val_size
)
8206 val_type
= Type::make_pointer_type(val_type
);
8208 Expression
* bucket_size
= Expression::make_integer_ul(Map_type::bucket_size
,
8209 NULL
, this->location_
);
8211 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8212 Array_type
* topbits_type
= Type::make_array_type(uint8_type
, bucket_size
);
8213 topbits_type
->set_is_array_incomparable();
8214 Array_type
* keys_type
= Type::make_array_type(key_type
, bucket_size
);
8215 keys_type
->set_is_array_incomparable();
8216 Array_type
* values_type
= Type::make_array_type(val_type
, bucket_size
);
8217 values_type
->set_is_array_incomparable();
8219 // If keys and values have no pointers, the map implementation can
8220 // keep a list of overflow pointers on the side so that buckets can
8221 // be marked as having no pointers. Arrange for the bucket to have
8222 // no pointers by changing the type of the overflow field to uintptr
8223 // in this case. See comment on the hmap.overflow field in
8224 // libgo/go/runtime/hashmap.go.
8225 Type
* overflow_type
;
8226 if (!key_type
->has_pointer() && !val_type
->has_pointer())
8227 overflow_type
= Type::lookup_integer_type("uintptr");
8230 // This should really be a pointer to the bucket type itself,
8231 // but that would require us to construct a Named_type for it to
8232 // give it a way to refer to itself. Since nothing really cares
8233 // (except perhaps for someone using a debugger) just use an
8235 overflow_type
= Type::make_pointer_type(Type::make_void_type());
8238 // Make sure the overflow pointer is the last memory in the struct,
8239 // because the runtime assumes it can use size-ptrSize as the offset
8240 // of the overflow pointer. We double-check that property below
8241 // once the offsets and size are computed.
8243 int64_t topbits_field_size
, topbits_field_align
;
8244 int64_t keys_field_size
, keys_field_align
;
8245 int64_t values_field_size
, values_field_align
;
8246 int64_t overflow_field_size
, overflow_field_align
;
8247 if (!topbits_type
->backend_type_size(gogo
, &topbits_field_size
)
8248 || !topbits_type
->backend_type_field_align(gogo
, &topbits_field_align
)
8249 || !keys_type
->backend_type_size(gogo
, &keys_field_size
)
8250 || !keys_type
->backend_type_field_align(gogo
, &keys_field_align
)
8251 || !values_type
->backend_type_size(gogo
, &values_field_size
)
8252 || !values_type
->backend_type_field_align(gogo
, &values_field_align
)
8253 || !overflow_type
->backend_type_size(gogo
, &overflow_field_size
)
8254 || !overflow_type
->backend_type_field_align(gogo
, &overflow_field_align
))
8256 go_assert(saw_errors());
8261 int64_t max_align
= std::max(std::max(topbits_field_align
, keys_field_align
),
8262 values_field_align
);
8263 if (max_align
<= overflow_field_align
)
8264 ret
= make_builtin_struct_type(4,
8265 "topbits", topbits_type
,
8267 "values", values_type
,
8268 "overflow", overflow_type
);
8271 size_t off
= topbits_field_size
;
8272 off
= ((off
+ keys_field_align
- 1)
8273 &~ static_cast<size_t>(keys_field_align
- 1));
8274 off
+= keys_field_size
;
8275 off
= ((off
+ values_field_align
- 1)
8276 &~ static_cast<size_t>(values_field_align
- 1));
8277 off
+= values_field_size
;
8279 int64_t padded_overflow_field_size
=
8280 ((overflow_field_size
+ max_align
- 1)
8281 &~ static_cast<size_t>(max_align
- 1));
8284 ovoff
= ((ovoff
+ max_align
- 1)
8285 &~ static_cast<size_t>(max_align
- 1));
8286 size_t pad
= (ovoff
- off
8287 + padded_overflow_field_size
- overflow_field_size
);
8289 Expression
* pad_expr
= Expression::make_integer_ul(pad
, NULL
,
8291 Array_type
* pad_type
= Type::make_array_type(uint8_type
, pad_expr
);
8292 pad_type
->set_is_array_incomparable();
8294 ret
= make_builtin_struct_type(5,
8295 "topbits", topbits_type
,
8297 "values", values_type
,
8299 "overflow", overflow_type
);
8302 // Verify that the overflow field is just before the end of the
8305 Btype
* btype
= ret
->get_backend(gogo
);
8306 int64_t offset
= gogo
->backend()->type_field_offset(btype
,
8307 ret
->field_count() - 1);
8309 if (!ret
->backend_type_size(gogo
, &size
))
8311 go_assert(saw_errors());
8316 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptr_size
))
8318 go_assert(saw_errors());
8322 go_assert(offset
+ ptr_size
== size
);
8324 ret
->set_is_struct_incomparable();
8326 this->bucket_type_
= ret
;
8330 // Return the hashmap type for a map type.
8333 Map_type::hmap_type(Type
* bucket_type
)
8335 if (this->hmap_type_
!= NULL
)
8336 return this->hmap_type_
;
8338 Type
* int_type
= Type::lookup_integer_type("int");
8339 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8340 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8341 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8342 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8343 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8345 Type
* ptr_bucket_type
= Type::make_pointer_type(bucket_type
);
8347 Struct_type
* ret
= make_builtin_struct_type(9,
8349 "flags", uint8_type
,
8351 "noverflow", uint16_type
,
8352 "hash0", uint32_type
,
8353 "buckets", ptr_bucket_type
,
8354 "oldbuckets", ptr_bucket_type
,
8355 "nevacuate", uintptr_type
,
8356 "extra", void_ptr_type
);
8357 ret
->set_is_struct_incomparable();
8358 this->hmap_type_
= ret
;
8362 // Return the iterator type for a map type. This is the type of the
8363 // value used when doing a range over a map.
8366 Map_type::hiter_type(Gogo
* gogo
)
8368 if (this->hiter_type_
!= NULL
)
8369 return this->hiter_type_
;
8371 int64_t keysize
, valsize
;
8372 if (!this->key_type_
->backend_type_size(gogo
, &keysize
)
8373 || !this->val_type_
->backend_type_size(gogo
, &valsize
))
8375 go_assert(saw_errors());
8379 Type
* key_ptr_type
= Type::make_pointer_type(this->key_type_
);
8380 Type
* val_ptr_type
= Type::make_pointer_type(this->val_type_
);
8381 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8382 Type
* uint8_ptr_type
= Type::make_pointer_type(uint8_type
);
8383 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8384 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
8385 Type
* bucket_ptr_type
= Type::make_pointer_type(bucket_type
);
8386 Type
* hmap_type
= this->hmap_type(bucket_type
);
8387 Type
* hmap_ptr_type
= Type::make_pointer_type(hmap_type
);
8388 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8389 Type
* bool_type
= Type::lookup_bool_type();
8391 Struct_type
* ret
= make_builtin_struct_type(15,
8392 "key", key_ptr_type
,
8393 "val", val_ptr_type
,
8394 "t", uint8_ptr_type
,
8396 "buckets", bucket_ptr_type
,
8397 "bptr", bucket_ptr_type
,
8398 "overflow", void_ptr_type
,
8399 "oldoverflow", void_ptr_type
,
8400 "startBucket", uintptr_type
,
8401 "offset", uint8_type
,
8402 "wrapped", bool_type
,
8405 "bucket", uintptr_type
,
8406 "checkBucket", uintptr_type
);
8407 ret
->set_is_struct_incomparable();
8408 this->hiter_type_
= ret
;
8412 // Reflection string for a map.
8415 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8417 ret
->append("map[");
8418 this->append_reflection(this->key_type_
, gogo
, ret
);
8420 this->append_reflection(this->val_type_
, gogo
, ret
);
8423 // Export a map type.
8426 Map_type::do_export(Export
* exp
) const
8428 exp
->write_c_string("map [");
8429 exp
->write_type(this->key_type_
);
8430 exp
->write_c_string("] ");
8431 exp
->write_type(this->val_type_
);
8434 // Import a map type.
8437 Map_type::do_import(Import
* imp
)
8439 imp
->require_c_string("map [");
8440 Type
* key_type
= imp
->read_type();
8441 imp
->require_c_string("] ");
8442 Type
* val_type
= imp
->read_type();
8443 return Type::make_map_type(key_type
, val_type
, imp
->location());
8449 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
8451 return new Map_type(key_type
, val_type
, location
);
8454 // Class Channel_type.
8459 Channel_type::do_verify()
8461 // We have no location for this error, but this is not something the
8462 // ordinary user will see.
8463 if (!this->element_type_
->in_heap())
8464 go_error_at(Linemap::unknown_location(),
8465 "chan of go:notinheap type not allowed");
8472 Channel_type::do_hash_for_method(Gogo
* gogo
, int flags
) const
8474 unsigned int ret
= 0;
8475 if (this->may_send_
)
8477 if (this->may_receive_
)
8479 if (this->element_type_
!= NULL
)
8480 ret
+= this->element_type_
->hash_for_method(gogo
, flags
) << 2;
8484 // Whether this type is the same as T.
8487 Channel_type::is_identical(const Channel_type
* t
, int flags
) const
8489 if (!Type::are_identical(this->element_type(), t
->element_type(), flags
,
8492 return (this->may_send_
== t
->may_send_
8493 && this->may_receive_
== t
->may_receive_
);
8496 // Return the backend representation for a channel type. A channel is a pointer
8497 // to a __go_channel struct. The __go_channel struct is defined in
8498 // libgo/runtime/channel.h.
8501 Channel_type::do_get_backend(Gogo
* gogo
)
8503 static Btype
* backend_channel_type
;
8504 if (backend_channel_type
== NULL
)
8506 std::vector
<Backend::Btyped_identifier
> bfields
;
8507 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
8508 bt
= gogo
->backend()->named_type("__go_channel", bt
,
8509 Linemap::predeclared_location());
8510 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
8512 return backend_channel_type
;
8515 // Build a type descriptor for a channel type.
8518 Channel_type::make_chan_type_descriptor_type()
8523 Type
* tdt
= Type::make_type_descriptor_type();
8524 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
8526 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8529 Type::make_builtin_struct_type(3,
8532 "dir", uintptr_type
);
8534 ret
= Type::make_builtin_named_type("ChanType", sf
);
8540 // Build a type descriptor for a map type.
8543 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8545 Location bloc
= Linemap::predeclared_location();
8547 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
8549 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
8551 Expression_list
* vals
= new Expression_list();
8554 Struct_field_list::const_iterator p
= fields
->begin();
8555 go_assert(p
->is_field_name("_type"));
8556 vals
->push_back(this->type_descriptor_constructor(gogo
,
8557 RUNTIME_TYPE_KIND_CHAN
,
8561 go_assert(p
->is_field_name("elem"));
8562 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
8565 go_assert(p
->is_field_name("dir"));
8566 // These bits must match the ones in libgo/runtime/go-type.h.
8568 if (this->may_receive_
)
8570 if (this->may_send_
)
8572 vals
->push_back(Expression::make_integer_ul(val
, p
->type(), bloc
));
8575 go_assert(p
== fields
->end());
8577 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
8580 // Reflection string.
8583 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8585 if (!this->may_send_
)
8587 ret
->append("chan");
8588 if (!this->may_receive_
)
8590 ret
->push_back(' ');
8591 this->append_reflection(this->element_type_
, gogo
, ret
);
8597 Channel_type::do_export(Export
* exp
) const
8599 exp
->write_c_string("chan ");
8600 if (this->may_send_
&& !this->may_receive_
)
8601 exp
->write_c_string("-< ");
8602 else if (this->may_receive_
&& !this->may_send_
)
8603 exp
->write_c_string("<- ");
8604 exp
->write_type(this->element_type_
);
8610 Channel_type::do_import(Import
* imp
)
8612 imp
->require_c_string("chan ");
8616 if (imp
->match_c_string("-< "))
8620 may_receive
= false;
8622 else if (imp
->match_c_string("<- "))
8634 Type
* element_type
= imp
->read_type();
8636 return Type::make_channel_type(may_send
, may_receive
, element_type
);
8639 // Return the type that the runtime package uses for one case of a
8640 // select statement. An array of values of this type is allocated on
8641 // the stack. This must match scase in libgo/go/runtime/select.go.
8644 Channel_type::select_case_type()
8646 static Struct_type
* scase_type
;
8647 if (scase_type
== NULL
)
8649 Type
* unsafe_pointer_type
=
8650 Type::make_pointer_type(Type::make_void_type());
8651 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8652 Type
* int64_type
= Type::lookup_integer_type("int64");
8654 Type::make_builtin_struct_type(4,
8655 "c", unsafe_pointer_type
,
8656 "elem", unsafe_pointer_type
,
8657 "kind", uint16_type
,
8658 "releasetime", int64_type
);
8659 scase_type
->set_is_struct_incomparable();
8664 // Make a new channel type.
8667 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
8669 return new Channel_type(send
, receive
, element_type
);
8672 // Class Interface_type.
8674 // Return the list of methods.
8676 const Typed_identifier_list
*
8677 Interface_type::methods() const
8679 go_assert(this->methods_are_finalized_
|| saw_errors());
8680 return this->all_methods_
;
8683 // Return the number of methods.
8686 Interface_type::method_count() const
8688 go_assert(this->methods_are_finalized_
|| saw_errors());
8689 return this->all_methods_
== NULL
? 0 : this->all_methods_
->size();
8695 Interface_type::do_traverse(Traverse
* traverse
)
8697 Typed_identifier_list
* methods
= (this->methods_are_finalized_
8698 ? this->all_methods_
8699 : this->parse_methods_
);
8700 if (methods
== NULL
)
8701 return TRAVERSE_CONTINUE
;
8702 return methods
->traverse(traverse
);
8705 // Finalize the methods. This handles interface inheritance.
8708 Interface_type::finalize_methods()
8710 if (this->methods_are_finalized_
)
8712 this->methods_are_finalized_
= true;
8713 if (this->parse_methods_
== NULL
)
8716 this->all_methods_
= new Typed_identifier_list();
8717 this->all_methods_
->reserve(this->parse_methods_
->size());
8718 Typed_identifier_list inherit
;
8719 for (Typed_identifier_list::const_iterator pm
=
8720 this->parse_methods_
->begin();
8721 pm
!= this->parse_methods_
->end();
8724 const Typed_identifier
* p
= &*pm
;
8725 if (p
->name().empty())
8726 inherit
.push_back(*p
);
8727 else if (this->find_method(p
->name()) == NULL
)
8728 this->all_methods_
->push_back(*p
);
8730 go_error_at(p
->location(), "duplicate method %qs",
8731 Gogo::message_name(p
->name()).c_str());
8734 std::vector
<Named_type
*> seen
;
8735 seen
.reserve(inherit
.size());
8736 bool issued_recursive_error
= false;
8737 while (!inherit
.empty())
8739 Type
* t
= inherit
.back().type();
8740 Location tl
= inherit
.back().location();
8743 Interface_type
* it
= t
->interface_type();
8747 go_error_at(tl
, "interface contains embedded non-interface");
8752 if (!issued_recursive_error
)
8754 go_error_at(tl
, "invalid recursive interface");
8755 issued_recursive_error
= true;
8760 Named_type
* nt
= t
->named_type();
8761 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
8763 std::vector
<Named_type
*>::const_iterator q
;
8764 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
8768 go_error_at(tl
, "inherited interface loop");
8772 if (q
!= seen
.end())
8777 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
8778 if (imethods
== NULL
)
8780 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
8781 q
!= imethods
->end();
8784 if (q
->name().empty())
8785 inherit
.push_back(*q
);
8786 else if (this->find_method(q
->name()) == NULL
)
8787 this->all_methods_
->push_back(Typed_identifier(q
->name(),
8790 go_error_at(tl
, "inherited method %qs is ambiguous",
8791 Gogo::message_name(q
->name()).c_str());
8795 if (!this->all_methods_
->empty())
8796 this->all_methods_
->sort_by_name();
8799 delete this->all_methods_
;
8800 this->all_methods_
= NULL
;
8804 // Return the method NAME, or NULL.
8806 const Typed_identifier
*
8807 Interface_type::find_method(const std::string
& name
) const
8809 go_assert(this->methods_are_finalized_
);
8810 if (this->all_methods_
== NULL
)
8812 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8813 p
!= this->all_methods_
->end();
8815 if (p
->name() == name
)
8820 // Return the method index.
8823 Interface_type::method_index(const std::string
& name
) const
8825 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
8827 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8828 p
!= this->all_methods_
->end();
8830 if (p
->name() == name
)
8835 // Return whether NAME is an unexported method, for better error
8839 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
8841 go_assert(this->methods_are_finalized_
);
8842 if (this->all_methods_
== NULL
)
8844 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8845 p
!= this->all_methods_
->end();
8848 const std::string
& method_name(p
->name());
8849 if (Gogo::is_hidden_name(method_name
)
8850 && name
== Gogo::unpack_hidden_name(method_name
)
8851 && gogo
->pack_hidden_name(name
, false) != method_name
)
8857 // Whether this type is identical with T.
8860 Interface_type::is_identical(const Interface_type
* t
, int flags
) const
8862 // If methods have not been finalized, then we are asking whether
8863 // func redeclarations are the same. This is an error, so for
8864 // simplicity we say they are never the same.
8865 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
8868 // Consult a flag to see whether we need to compare based on
8869 // parse methods or all methods.
8870 Typed_identifier_list
* methods
= (((flags
& COMPARE_EMBEDDED_INTERFACES
) != 0)
8871 ? this->parse_methods_
8872 : this->all_methods_
);
8873 Typed_identifier_list
* tmethods
= (((flags
& COMPARE_EMBEDDED_INTERFACES
) != 0)
8877 // We require the same methods with the same types. The methods
8878 // have already been sorted.
8879 if (methods
== NULL
|| tmethods
== NULL
)
8880 return methods
== tmethods
;
8882 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
8885 Assume_identical
* hold_ai
= this->assume_identical_
;
8886 Assume_identical ai
;
8890 this->assume_identical_
= &ai
;
8892 Typed_identifier_list::const_iterator p1
= methods
->begin();
8893 Typed_identifier_list::const_iterator p2
;
8894 for (p2
= tmethods
->begin(); p2
!= tmethods
->end(); ++p1
, ++p2
)
8896 if (p1
== methods
->end())
8898 if (p1
->name() != p2
->name()
8899 || !Type::are_identical(p1
->type(), p2
->type(), flags
, NULL
))
8903 this->assume_identical_
= hold_ai
;
8905 return p1
== methods
->end() && p2
== tmethods
->end();
8908 // Return true if T1 and T2 are assumed to be identical during a type
8912 Interface_type::assume_identical(const Interface_type
* t1
,
8913 const Interface_type
* t2
) const
8915 for (Assume_identical
* p
= this->assume_identical_
;
8918 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
8923 // Whether we can assign the interface type T to this type. The types
8924 // are known to not be identical. An interface assignment is only
8925 // permitted if T is known to implement all methods in THIS.
8926 // Otherwise a type guard is required.
8929 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
8930 std::string
* reason
) const
8932 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
8933 if (this->all_methods_
== NULL
)
8935 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8936 p
!= this->all_methods_
->end();
8939 const Typed_identifier
* m
= t
->find_method(p
->name());
8945 snprintf(buf
, sizeof buf
,
8946 _("need explicit conversion; missing method %s%s%s"),
8947 go_open_quote(), Gogo::message_name(p
->name()).c_str(),
8949 reason
->assign(buf
);
8954 std::string subreason
;
8955 if (!Type::are_identical(p
->type(), m
->type(), Type::COMPARE_TAGS
,
8960 std::string n
= Gogo::message_name(p
->name());
8961 size_t len
= 100 + n
.length() + subreason
.length();
8962 char* buf
= new char[len
];
8963 if (subreason
.empty())
8964 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
8965 go_open_quote(), n
.c_str(), go_close_quote());
8968 _("incompatible type for method %s%s%s (%s)"),
8969 go_open_quote(), n
.c_str(), go_close_quote(),
8971 reason
->assign(buf
);
8984 Interface_type::do_hash_for_method(Gogo
*, int) const
8986 go_assert(this->methods_are_finalized_
);
8987 unsigned int ret
= 0;
8988 if (this->all_methods_
!= NULL
)
8990 for (Typed_identifier_list::const_iterator p
=
8991 this->all_methods_
->begin();
8992 p
!= this->all_methods_
->end();
8995 ret
= Gogo::hash_string(p
->name(), ret
);
8996 // We don't use the method type in the hash, to avoid
8997 // infinite recursion if an interface method uses a type
8998 // which is an interface which inherits from the interface
9000 // type T interface { F() interface {T}}
9007 // Return true if T implements the interface. If it does not, and
9008 // REASON is not NULL, set *REASON to a useful error message.
9011 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
9013 go_assert(this->methods_are_finalized_
);
9014 if (this->all_methods_
== NULL
)
9017 bool is_pointer
= false;
9018 const Named_type
* nt
= t
->named_type();
9019 const Struct_type
* st
= t
->struct_type();
9020 // If we start with a named type, we don't dereference it to find
9024 const Type
* pt
= t
->points_to();
9027 // If T is a pointer to a named type, then we need to look at
9028 // the type to which it points.
9030 nt
= pt
->named_type();
9031 st
= pt
->struct_type();
9035 // If we have a named type, get the methods from it rather than from
9040 // Only named and struct types have methods.
9041 if (nt
== NULL
&& st
== NULL
)
9045 if (t
->points_to() != NULL
9046 && t
->points_to()->interface_type() != NULL
)
9047 reason
->assign(_("pointer to interface type has no methods"));
9049 reason
->assign(_("type has no methods"));
9054 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
9058 if (t
->points_to() != NULL
9059 && t
->points_to()->interface_type() != NULL
)
9060 reason
->assign(_("pointer to interface type has no methods"));
9062 reason
->assign(_("type has no methods"));
9067 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9068 p
!= this->all_methods_
->end();
9071 bool is_ambiguous
= false;
9072 Method
* m
= (nt
!= NULL
9073 ? nt
->method_function(p
->name(), &is_ambiguous
)
9074 : st
->method_function(p
->name(), &is_ambiguous
));
9079 std::string n
= Gogo::message_name(p
->name());
9080 size_t len
= n
.length() + 100;
9081 char* buf
= new char[len
];
9083 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
9084 go_open_quote(), n
.c_str(), go_close_quote());
9086 snprintf(buf
, len
, _("missing method %s%s%s"),
9087 go_open_quote(), n
.c_str(), go_close_quote());
9088 reason
->assign(buf
);
9094 Function_type
*p_fn_type
= p
->type()->function_type();
9095 Function_type
* m_fn_type
= m
->type()->function_type();
9096 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
9097 std::string subreason
;
9098 if (!p_fn_type
->is_identical(m_fn_type
, true, Type::COMPARE_TAGS
,
9103 std::string n
= Gogo::message_name(p
->name());
9104 size_t len
= 100 + n
.length() + subreason
.length();
9105 char* buf
= new char[len
];
9106 if (subreason
.empty())
9107 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
9108 go_open_quote(), n
.c_str(), go_close_quote());
9111 _("incompatible type for method %s%s%s (%s)"),
9112 go_open_quote(), n
.c_str(), go_close_quote(),
9114 reason
->assign(buf
);
9120 if (!is_pointer
&& !m
->is_value_method())
9124 std::string n
= Gogo::message_name(p
->name());
9125 size_t len
= 100 + n
.length();
9126 char* buf
= new char[len
];
9128 _("method %s%s%s requires a pointer receiver"),
9129 go_open_quote(), n
.c_str(), go_close_quote());
9130 reason
->assign(buf
);
9136 // If the magic //go:nointerface comment was used, the method
9137 // may not be used to implement interfaces.
9138 if (m
->nointerface())
9142 std::string n
= Gogo::message_name(p
->name());
9143 size_t len
= 100 + n
.length();
9144 char* buf
= new char[len
];
9146 _("method %s%s%s is marked go:nointerface"),
9147 go_open_quote(), n
.c_str(), go_close_quote());
9148 reason
->assign(buf
);
9158 // Return the backend representation of the empty interface type. We
9159 // use the same struct for all empty interfaces.
9162 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
9164 static Btype
* empty_interface_type
;
9165 if (empty_interface_type
== NULL
)
9167 std::vector
<Backend::Btyped_identifier
> bfields(2);
9169 Location bloc
= Linemap::predeclared_location();
9171 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9172 bfields
[0].name
= "__type_descriptor";
9173 bfields
[0].btype
= pdt
->get_backend(gogo
);
9174 bfields
[0].location
= bloc
;
9176 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9177 bfields
[1].name
= "__object";
9178 bfields
[1].btype
= vt
->get_backend(gogo
);
9179 bfields
[1].location
= bloc
;
9181 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
9183 return empty_interface_type
;
9186 Interface_type::Bmethods_map
Interface_type::bmethods_map
;
9188 // Return a pointer to the backend representation of the method table.
9191 Interface_type::get_backend_methods(Gogo
* gogo
)
9193 if (this->bmethods_
!= NULL
&& !this->bmethods_is_placeholder_
)
9194 return this->bmethods_
;
9196 std::pair
<Interface_type
*, Bmethods_map_entry
> val
;
9198 val
.second
.btype
= NULL
;
9199 val
.second
.is_placeholder
= false;
9200 std::pair
<Bmethods_map::iterator
, bool> ins
=
9201 Interface_type::bmethods_map
.insert(val
);
9203 && ins
.first
->second
.btype
!= NULL
9204 && !ins
.first
->second
.is_placeholder
)
9206 this->bmethods_
= ins
.first
->second
.btype
;
9207 this->bmethods_is_placeholder_
= false;
9208 return this->bmethods_
;
9211 Location loc
= this->location();
9213 std::vector
<Backend::Btyped_identifier
>
9214 mfields(this->all_methods_
->size() + 1);
9216 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9217 mfields
[0].name
= "__type_descriptor";
9218 mfields
[0].btype
= pdt
->get_backend(gogo
);
9219 mfields
[0].location
= loc
;
9221 std::string last_name
= "";
9223 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9224 p
!= this->all_methods_
->end();
9227 // The type of the method in Go only includes the parameters.
9228 // The actual method also has a receiver, which is always a
9229 // pointer. We need to add that pointer type here in order to
9230 // generate the correct type for the backend.
9231 Function_type
* ft
= p
->type()->function_type();
9232 go_assert(ft
->receiver() == NULL
);
9234 const Typed_identifier_list
* params
= ft
->parameters();
9235 Typed_identifier_list
* mparams
= new Typed_identifier_list();
9237 mparams
->reserve(params
->size() + 1);
9238 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9239 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
9242 for (Typed_identifier_list::const_iterator pp
= params
->begin();
9243 pp
!= params
->end();
9245 mparams
->push_back(*pp
);
9248 Typed_identifier_list
* mresults
= (ft
->results() == NULL
9250 : ft
->results()->copy());
9251 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
9254 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
9255 mfields
[i
].btype
= mft
->get_backend_fntype(gogo
);
9256 mfields
[i
].location
= loc
;
9258 // Sanity check: the names should be sorted.
9259 go_assert(Gogo::unpack_hidden_name(p
->name())
9260 > Gogo::unpack_hidden_name(last_name
));
9261 last_name
= p
->name();
9264 Btype
* st
= gogo
->backend()->struct_type(mfields
);
9265 Btype
* ret
= gogo
->backend()->pointer_type(st
);
9267 if (ins
.first
->second
.btype
!= NULL
9268 && ins
.first
->second
.is_placeholder
)
9269 gogo
->backend()->set_placeholder_pointer_type(ins
.first
->second
.btype
,
9271 this->bmethods_
= ret
;
9272 ins
.first
->second
.btype
= ret
;
9273 this->bmethods_is_placeholder_
= false;
9274 ins
.first
->second
.is_placeholder
= false;
9278 // Return a placeholder for the pointer to the backend methods table.
9281 Interface_type::get_backend_methods_placeholder(Gogo
* gogo
)
9283 if (this->bmethods_
== NULL
)
9285 std::pair
<Interface_type
*, Bmethods_map_entry
> val
;
9287 val
.second
.btype
= NULL
;
9288 val
.second
.is_placeholder
= false;
9289 std::pair
<Bmethods_map::iterator
, bool> ins
=
9290 Interface_type::bmethods_map
.insert(val
);
9291 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
9293 this->bmethods_
= ins
.first
->second
.btype
;
9294 this->bmethods_is_placeholder_
= ins
.first
->second
.is_placeholder
;
9295 return this->bmethods_
;
9298 Location loc
= this->location();
9299 Btype
* bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
9300 this->bmethods_
= bt
;
9301 ins
.first
->second
.btype
= bt
;
9302 this->bmethods_is_placeholder_
= true;
9303 ins
.first
->second
.is_placeholder
= true;
9305 return this->bmethods_
;
9308 // Return the fields of a non-empty interface type. This is not
9309 // declared in types.h so that types.h doesn't have to #include
9313 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
9314 bool use_placeholder
,
9315 std::vector
<Backend::Btyped_identifier
>* bfields
)
9317 Location loc
= type
->location();
9321 (*bfields
)[0].name
= "__methods";
9322 (*bfields
)[0].btype
= (use_placeholder
9323 ? type
->get_backend_methods_placeholder(gogo
)
9324 : type
->get_backend_methods(gogo
));
9325 (*bfields
)[0].location
= loc
;
9327 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9328 (*bfields
)[1].name
= "__object";
9329 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
9330 (*bfields
)[1].location
= Linemap::predeclared_location();
9333 // Return the backend representation for an interface type. An interface is a
9334 // pointer to a struct. The struct has three fields. The first field is a
9335 // pointer to the type descriptor for the dynamic type of the object.
9336 // The second field is a pointer to a table of methods for the
9337 // interface to be used with the object. The third field is the value
9338 // of the object itself.
9341 Interface_type::do_get_backend(Gogo
* gogo
)
9343 if (this->is_empty())
9344 return Interface_type::get_backend_empty_interface_type(gogo
);
9347 if (this->interface_btype_
!= NULL
)
9348 return this->interface_btype_
;
9349 this->interface_btype_
=
9350 gogo
->backend()->placeholder_struct_type("", this->location_
);
9351 std::vector
<Backend::Btyped_identifier
> bfields
;
9352 get_backend_interface_fields(gogo
, this, false, &bfields
);
9353 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
9355 this->interface_btype_
= gogo
->backend()->error_type();
9356 return this->interface_btype_
;
9360 // Finish the backend representation of the methods.
9363 Interface_type::finish_backend_methods(Gogo
* gogo
)
9365 if (!this->is_empty())
9367 const Typed_identifier_list
* methods
= this->methods();
9368 if (methods
!= NULL
)
9370 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9371 p
!= methods
->end();
9373 p
->type()->get_backend(gogo
);
9376 // Getting the backend methods now will set the placeholder
9378 this->get_backend_methods(gogo
);
9382 // The type of an interface type descriptor.
9385 Interface_type::make_interface_type_descriptor_type()
9390 Type
* tdt
= Type::make_type_descriptor_type();
9391 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
9393 Type
* string_type
= Type::lookup_string_type();
9394 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
9397 Type::make_builtin_struct_type(3,
9398 "name", pointer_string_type
,
9399 "pkgPath", pointer_string_type
,
9402 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
9404 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
9406 Struct_type
* s
= Type::make_builtin_struct_type(2,
9408 "methods", slice_nsm
);
9410 ret
= Type::make_builtin_named_type("InterfaceType", s
);
9416 // Build a type descriptor for an interface type.
9419 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9421 Location bloc
= Linemap::predeclared_location();
9423 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
9425 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
9427 Expression_list
* ivals
= new Expression_list();
9430 Struct_field_list::const_iterator pif
= ifields
->begin();
9431 go_assert(pif
->is_field_name("_type"));
9432 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
9433 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
9437 go_assert(pif
->is_field_name("methods"));
9439 Expression_list
* methods
= new Expression_list();
9440 if (this->all_methods_
!= NULL
)
9442 Type
* elemtype
= pif
->type()->array_type()->element_type();
9444 methods
->reserve(this->all_methods_
->size());
9445 for (Typed_identifier_list::const_iterator pm
=
9446 this->all_methods_
->begin();
9447 pm
!= this->all_methods_
->end();
9450 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
9452 Expression_list
* mvals
= new Expression_list();
9455 Struct_field_list::const_iterator pmf
= mfields
->begin();
9456 go_assert(pmf
->is_field_name("name"));
9457 std::string s
= Gogo::unpack_hidden_name(pm
->name());
9458 Expression
* e
= Expression::make_string(s
, bloc
);
9459 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9462 go_assert(pmf
->is_field_name("pkgPath"));
9463 if (!Gogo::is_hidden_name(pm
->name()))
9464 mvals
->push_back(Expression::make_nil(bloc
));
9467 s
= Gogo::hidden_name_pkgpath(pm
->name());
9468 e
= Expression::make_string(s
, bloc
);
9469 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9473 go_assert(pmf
->is_field_name("typ"));
9474 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
9477 go_assert(pmf
== mfields
->end());
9479 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
9481 methods
->push_back(e
);
9485 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
9489 go_assert(pif
== ifields
->end());
9491 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
9494 // Reflection string.
9497 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9499 ret
->append("interface {");
9500 const Typed_identifier_list
* methods
= this->parse_methods_
;
9501 if (methods
!= NULL
)
9503 ret
->push_back(' ');
9504 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9505 p
!= methods
->end();
9508 if (p
!= methods
->begin())
9510 if (p
->name().empty())
9511 this->append_reflection(p
->type(), gogo
, ret
);
9514 if (!Gogo::is_hidden_name(p
->name()))
9515 ret
->append(p
->name());
9516 else if (gogo
->pkgpath_from_option())
9517 ret
->append(p
->name().substr(1));
9520 // If no -fgo-pkgpath option, backward compatibility
9521 // for how this used to work before -fgo-pkgpath was
9523 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
9524 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
9525 ret
->push_back('.');
9526 ret
->append(Gogo::unpack_hidden_name(p
->name()));
9528 std::string sub
= p
->type()->reflection(gogo
);
9529 go_assert(sub
.compare(0, 4, "func") == 0);
9530 sub
= sub
.substr(4);
9534 ret
->push_back(' ');
9542 Interface_type::do_export(Export
* exp
) const
9544 exp
->write_c_string("interface { ");
9546 const Typed_identifier_list
* methods
= this->parse_methods_
;
9547 if (methods
!= NULL
)
9549 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
9550 pm
!= methods
->end();
9553 if (pm
->name().empty())
9555 exp
->write_c_string("? ");
9556 exp
->write_type(pm
->type());
9560 exp
->write_string(pm
->name());
9561 exp
->write_c_string(" (");
9563 const Function_type
* fntype
= pm
->type()->function_type();
9566 const Typed_identifier_list
* parameters
= fntype
->parameters();
9567 if (parameters
!= NULL
)
9569 bool is_varargs
= fntype
->is_varargs();
9570 for (Typed_identifier_list::const_iterator pp
=
9571 parameters
->begin();
9572 pp
!= parameters
->end();
9578 exp
->write_c_string(", ");
9579 exp
->write_name(pp
->name());
9580 exp
->write_c_string(" ");
9581 if (!is_varargs
|| pp
+ 1 != parameters
->end())
9582 exp
->write_type(pp
->type());
9585 exp
->write_c_string("...");
9586 Type
*pptype
= pp
->type();
9587 exp
->write_type(pptype
->array_type()->element_type());
9592 exp
->write_c_string(")");
9594 const Typed_identifier_list
* results
= fntype
->results();
9595 if (results
!= NULL
)
9597 exp
->write_c_string(" ");
9598 if (results
->size() == 1 && results
->begin()->name().empty())
9599 exp
->write_type(results
->begin()->type());
9603 exp
->write_c_string("(");
9604 for (Typed_identifier_list::const_iterator p
=
9606 p
!= results
->end();
9612 exp
->write_c_string(", ");
9613 exp
->write_name(p
->name());
9614 exp
->write_c_string(" ");
9615 exp
->write_type(p
->type());
9617 exp
->write_c_string(")");
9622 exp
->write_c_string("; ");
9626 exp
->write_c_string("}");
9629 // Import an interface type.
9632 Interface_type::do_import(Import
* imp
)
9634 imp
->require_c_string("interface { ");
9636 Typed_identifier_list
* methods
= new Typed_identifier_list
;
9637 while (imp
->peek_char() != '}')
9639 std::string name
= imp
->read_identifier();
9643 imp
->require_c_string(" ");
9644 Type
* t
= imp
->read_type();
9645 methods
->push_back(Typed_identifier("", t
, imp
->location()));
9646 imp
->require_c_string("; ");
9650 imp
->require_c_string(" (");
9652 Typed_identifier_list
* parameters
;
9653 bool is_varargs
= false;
9654 if (imp
->peek_char() == ')')
9658 parameters
= new Typed_identifier_list
;
9661 std::string name
= imp
->read_name();
9662 imp
->require_c_string(" ");
9664 if (imp
->match_c_string("..."))
9670 Type
* ptype
= imp
->read_type();
9672 ptype
= Type::make_array_type(ptype
, NULL
);
9673 parameters
->push_back(Typed_identifier(name
, ptype
,
9675 if (imp
->peek_char() != ',')
9677 go_assert(!is_varargs
);
9678 imp
->require_c_string(", ");
9681 imp
->require_c_string(")");
9683 Typed_identifier_list
* results
;
9684 if (imp
->peek_char() != ' ')
9688 results
= new Typed_identifier_list
;
9690 if (imp
->peek_char() != '(')
9692 Type
* rtype
= imp
->read_type();
9693 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
9700 std::string name
= imp
->read_name();
9701 imp
->require_c_string(" ");
9702 Type
* rtype
= imp
->read_type();
9703 results
->push_back(Typed_identifier(name
, rtype
,
9705 if (imp
->peek_char() != ',')
9707 imp
->require_c_string(", ");
9709 imp
->require_c_string(")");
9713 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
9717 fntype
->set_is_varargs();
9718 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
9720 imp
->require_c_string("; ");
9723 imp
->require_c_string("}");
9725 if (methods
->empty())
9731 Interface_type
* ret
= Type::make_interface_type(methods
, imp
->location());
9732 ret
->package_
= imp
->package();
9736 // Make an interface type.
9739 Type::make_interface_type(Typed_identifier_list
* methods
,
9742 return new Interface_type(methods
, location
);
9745 // Make an empty interface type.
9748 Type::make_empty_interface_type(Location location
)
9750 Interface_type
* ret
= new Interface_type(NULL
, location
);
9751 ret
->finalize_methods();
9757 // Bind a method to an object.
9760 Method::bind_method(Expression
* expr
, Location location
) const
9762 if (this->stub_
== NULL
)
9764 // When there is no stub object, the binding is determined by
9766 return this->do_bind_method(expr
, location
);
9768 return Expression::make_bound_method(expr
, this, this->stub_
, location
);
9771 // Return the named object associated with a method. This may only be
9772 // called after methods are finalized.
9775 Method::named_object() const
9777 if (this->stub_
!= NULL
)
9779 return this->do_named_object();
9782 // Class Named_method.
9784 // The type of the method.
9787 Named_method::do_type() const
9789 if (this->named_object_
->is_function())
9790 return this->named_object_
->func_value()->type();
9791 else if (this->named_object_
->is_function_declaration())
9792 return this->named_object_
->func_declaration_value()->type();
9797 // Return the location of the method receiver.
9800 Named_method::do_receiver_location() const
9802 return this->do_type()->receiver()->location();
9805 // Bind a method to an object.
9808 Named_method::do_bind_method(Expression
* expr
, Location location
) const
9810 Named_object
* no
= this->named_object_
;
9811 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, this,
9813 // If this is not a local method, and it does not use a stub, then
9814 // the real method expects a different type. We need to cast the
9816 if (this->depth() > 0 && !this->needs_stub_method())
9818 Function_type
* ftype
= this->do_type();
9819 go_assert(ftype
->is_method());
9820 Type
* frtype
= ftype
->receiver()->type();
9821 bme
->set_first_argument_type(frtype
);
9826 // Return whether this method should not participate in interfaces.
9829 Named_method::do_nointerface() const
9831 Named_object
* no
= this->named_object_
;
9832 if (no
->is_function())
9833 return no
->func_value()->nointerface();
9834 else if (no
->is_function_declaration())
9835 return no
->func_declaration_value()->nointerface();
9840 // Class Interface_method.
9842 // Bind a method to an object.
9845 Interface_method::do_bind_method(Expression
* expr
,
9846 Location location
) const
9848 return Expression::make_interface_field_reference(expr
, this->name_
,
9854 // Insert a new method. Return true if it was inserted, false
9858 Methods::insert(const std::string
& name
, Method
* m
)
9860 std::pair
<Method_map::iterator
, bool> ins
=
9861 this->methods_
.insert(std::make_pair(name
, m
));
9866 Method
* old_method
= ins
.first
->second
;
9867 if (m
->depth() < old_method
->depth())
9870 ins
.first
->second
= m
;
9875 if (m
->depth() == old_method
->depth())
9876 old_method
->set_is_ambiguous();
9882 // Return the number of unambiguous methods.
9885 Methods::count() const
9888 for (Method_map::const_iterator p
= this->methods_
.begin();
9889 p
!= this->methods_
.end();
9891 if (!p
->second
->is_ambiguous())
9896 // Class Named_type.
9898 // Return the name of the type.
9901 Named_type::name() const
9903 return this->named_object_
->name();
9906 // Return the name of the type to use in an error message.
9909 Named_type::message_name() const
9911 return this->named_object_
->message_name();
9914 // Return the base type for this type. We have to be careful about
9915 // circular type definitions, which are invalid but may be seen here.
9918 Named_type::named_base()
9923 Type
* ret
= this->type_
->base();
9924 this->seen_
= false;
9929 Named_type::named_base() const
9934 const Type
* ret
= this->type_
->base();
9935 this->seen_
= false;
9939 // Return whether this is an error type. We have to be careful about
9940 // circular type definitions, which are invalid but may be seen here.
9943 Named_type::is_named_error_type() const
9948 bool ret
= this->type_
->is_error_type();
9949 this->seen_
= false;
9953 // Whether this type is comparable. We have to be careful about
9954 // circular type definitions.
9957 Named_type::named_type_is_comparable(std::string
* reason
) const
9962 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
9963 this->type_
, reason
);
9964 this->seen_
= false;
9968 // Add a method to this type.
9971 Named_type::add_method(const std::string
& name
, Function
* function
)
9973 go_assert(!this->is_alias_
);
9974 if (this->local_methods_
== NULL
)
9975 this->local_methods_
= new Bindings(NULL
);
9976 return this->local_methods_
->add_function(name
,
9977 this->named_object_
->package(),
9981 // Add a method declaration to this type.
9984 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
9985 Function_type
* type
,
9988 go_assert(!this->is_alias_
);
9989 if (this->local_methods_
== NULL
)
9990 this->local_methods_
= new Bindings(NULL
);
9991 return this->local_methods_
->add_function_declaration(name
, package
, type
,
9995 // Add an existing method to this type.
9998 Named_type::add_existing_method(Named_object
* no
)
10000 go_assert(!this->is_alias_
);
10001 if (this->local_methods_
== NULL
)
10002 this->local_methods_
= new Bindings(NULL
);
10003 this->local_methods_
->add_named_object(no
);
10006 // Look for a local method NAME, and returns its named object, or NULL
10010 Named_type::find_local_method(const std::string
& name
) const
10012 if (this->is_error_
)
10014 if (this->is_alias_
)
10016 Named_type
* nt
= this->type_
->named_type();
10019 if (this->seen_alias_
)
10021 this->seen_alias_
= true;
10022 Named_object
* ret
= nt
->find_local_method(name
);
10023 this->seen_alias_
= false;
10028 if (this->local_methods_
== NULL
)
10030 return this->local_methods_
->lookup(name
);
10033 // Return the list of local methods.
10036 Named_type::local_methods() const
10038 if (this->is_error_
)
10040 if (this->is_alias_
)
10042 Named_type
* nt
= this->type_
->named_type();
10045 if (this->seen_alias_
)
10047 this->seen_alias_
= true;
10048 const Bindings
* ret
= nt
->local_methods();
10049 this->seen_alias_
= false;
10054 return this->local_methods_
;
10057 // Return whether NAME is an unexported field or method, for better
10058 // error reporting.
10061 Named_type::is_unexported_local_method(Gogo
* gogo
,
10062 const std::string
& name
) const
10064 if (this->is_error_
)
10066 if (this->is_alias_
)
10068 Named_type
* nt
= this->type_
->named_type();
10071 if (this->seen_alias_
)
10073 this->seen_alias_
= true;
10074 bool ret
= nt
->is_unexported_local_method(gogo
, name
);
10075 this->seen_alias_
= false;
10080 Bindings
* methods
= this->local_methods_
;
10081 if (methods
!= NULL
)
10083 for (Bindings::const_declarations_iterator p
=
10084 methods
->begin_declarations();
10085 p
!= methods
->end_declarations();
10088 if (Gogo::is_hidden_name(p
->first
)
10089 && name
== Gogo::unpack_hidden_name(p
->first
)
10090 && gogo
->pack_hidden_name(name
, false) != p
->first
)
10097 // Build the complete list of methods for this type, which means
10098 // recursively including all methods for anonymous fields. Create all
10102 Named_type::finalize_methods(Gogo
* gogo
)
10104 if (this->is_alias_
)
10106 if (this->all_methods_
!= NULL
)
10109 if (this->local_methods_
!= NULL
10110 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
10112 const Bindings
* lm
= this->local_methods_
;
10113 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
10114 p
!= lm
->end_declarations();
10116 go_error_at(p
->second
->location(),
10117 "invalid pointer or interface receiver type");
10118 delete this->local_methods_
;
10119 this->local_methods_
= NULL
;
10123 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
10126 // Return whether this type has any methods.
10129 Named_type::has_any_methods() const
10131 if (this->is_error_
)
10133 if (this->is_alias_
)
10135 if (this->type_
->named_type() != NULL
)
10137 if (this->seen_alias_
)
10139 this->seen_alias_
= true;
10140 bool ret
= this->type_
->named_type()->has_any_methods();
10141 this->seen_alias_
= false;
10144 if (this->type_
->struct_type() != NULL
)
10145 return this->type_
->struct_type()->has_any_methods();
10148 return this->all_methods_
!= NULL
;
10151 // Return the methods for this type.
10154 Named_type::methods() const
10156 if (this->is_error_
)
10158 if (this->is_alias_
)
10160 if (this->type_
->named_type() != NULL
)
10162 if (this->seen_alias_
)
10164 this->seen_alias_
= true;
10165 const Methods
* ret
= this->type_
->named_type()->methods();
10166 this->seen_alias_
= false;
10169 if (this->type_
->struct_type() != NULL
)
10170 return this->type_
->struct_type()->methods();
10173 return this->all_methods_
;
10176 // Return the method NAME, or NULL if there isn't one or if it is
10177 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
10181 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
10183 if (this->is_error_
)
10185 if (this->is_alias_
)
10187 if (is_ambiguous
!= NULL
)
10188 *is_ambiguous
= false;
10189 if (this->type_
->named_type() != NULL
)
10191 if (this->seen_alias_
)
10193 this->seen_alias_
= true;
10194 Named_type
* nt
= this->type_
->named_type();
10195 Method
* ret
= nt
->method_function(name
, is_ambiguous
);
10196 this->seen_alias_
= false;
10199 if (this->type_
->struct_type() != NULL
)
10200 return this->type_
->struct_type()->method_function(name
, is_ambiguous
);
10203 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
10206 // Return a pointer to the interface method table for this type for
10207 // the interface INTERFACE. IS_POINTER is true if this is for a
10208 // pointer to THIS.
10211 Named_type::interface_method_table(Interface_type
* interface
, bool is_pointer
)
10213 if (this->is_error_
)
10214 return Expression::make_error(this->location_
);
10215 if (this->is_alias_
)
10217 if (this->type_
->named_type() != NULL
)
10219 if (this->seen_alias_
)
10220 return Expression::make_error(this->location_
);
10221 this->seen_alias_
= true;
10222 Named_type
* nt
= this->type_
->named_type();
10223 Expression
* ret
= nt
->interface_method_table(interface
, is_pointer
);
10224 this->seen_alias_
= false;
10227 if (this->type_
->struct_type() != NULL
)
10228 return this->type_
->struct_type()->interface_method_table(interface
,
10232 return Type::interface_method_table(this, interface
, is_pointer
,
10233 &this->interface_method_tables_
,
10234 &this->pointer_interface_method_tables_
);
10237 // Look for a use of a complete type within another type. This is
10238 // used to check that we don't try to use a type within itself.
10240 class Find_type_use
: public Traverse
10243 Find_type_use(Named_type
* find_type
)
10244 : Traverse(traverse_types
),
10245 find_type_(find_type
), found_(false)
10248 // Whether we found the type.
10251 { return this->found_
; }
10258 // The type we are looking for.
10259 Named_type
* find_type_
;
10260 // Whether we found the type.
10264 // Check for FIND_TYPE in TYPE.
10267 Find_type_use::type(Type
* type
)
10269 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
10271 this->found_
= true;
10272 return TRAVERSE_EXIT
;
10275 // It's OK if we see a reference to the type in any type which is
10276 // essentially a pointer: a pointer, a slice, a function, a map, or
10278 if (type
->points_to() != NULL
10279 || type
->is_slice_type()
10280 || type
->function_type() != NULL
10281 || type
->map_type() != NULL
10282 || type
->channel_type() != NULL
)
10283 return TRAVERSE_SKIP_COMPONENTS
;
10285 // For an interface, a reference to the type in a method type should
10286 // be ignored, but we have to consider direct inheritance. When
10287 // this is called, there may be cases of direct inheritance
10288 // represented as a method with no name.
10289 if (type
->interface_type() != NULL
)
10291 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
10292 if (methods
!= NULL
)
10294 for (Typed_identifier_list::const_iterator p
= methods
->begin();
10295 p
!= methods
->end();
10298 if (p
->name().empty())
10300 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
10301 return TRAVERSE_EXIT
;
10305 return TRAVERSE_SKIP_COMPONENTS
;
10308 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
10309 // to convert TYPE to the backend representation before we convert
10311 if (type
->named_type() != NULL
)
10313 switch (type
->base()->classification())
10315 case Type::TYPE_ERROR
:
10316 case Type::TYPE_BOOLEAN
:
10317 case Type::TYPE_INTEGER
:
10318 case Type::TYPE_FLOAT
:
10319 case Type::TYPE_COMPLEX
:
10320 case Type::TYPE_STRING
:
10321 case Type::TYPE_NIL
:
10324 case Type::TYPE_ARRAY
:
10325 case Type::TYPE_STRUCT
:
10326 this->find_type_
->add_dependency(type
->named_type());
10329 case Type::TYPE_NAMED
:
10330 if (type
->named_type() == type
->base()->named_type())
10332 this->found_
= true;
10333 return TRAVERSE_EXIT
;
10336 go_assert(saw_errors());
10339 case Type::TYPE_FORWARD
:
10340 go_assert(saw_errors());
10343 case Type::TYPE_VOID
:
10344 case Type::TYPE_SINK
:
10345 case Type::TYPE_FUNCTION
:
10346 case Type::TYPE_POINTER
:
10347 case Type::TYPE_CALL_MULTIPLE_RESULT
:
10348 case Type::TYPE_MAP
:
10349 case Type::TYPE_CHANNEL
:
10350 case Type::TYPE_INTERFACE
:
10356 return TRAVERSE_CONTINUE
;
10359 // Look for a circular reference of an alias.
10361 class Find_alias
: public Traverse
10364 Find_alias(Named_type
* find_type
)
10365 : Traverse(traverse_types
),
10366 find_type_(find_type
), found_(false)
10369 // Whether we found the type.
10372 { return this->found_
; }
10379 // The type we are looking for.
10380 Named_type
* find_type_
;
10381 // Whether we found the type.
10386 Find_alias::type(Type
* type
)
10388 Named_type
* nt
= type
->named_type();
10391 if (nt
== this->find_type_
)
10393 this->found_
= true;
10394 return TRAVERSE_EXIT
;
10397 // We started from `type T1 = T2`, where T1 is find_type_ and T2
10398 // is, perhaps indirectly, the parameter TYPE. If TYPE is not
10399 // an alias itself, it's OK if whatever T2 is defined as refers
10401 if (!nt
->is_alias())
10402 return TRAVERSE_SKIP_COMPONENTS
;
10405 // Check if there are recursive inherited interface aliases.
10406 Interface_type
* ift
= type
->interface_type();
10409 const Typed_identifier_list
* methods
= ift
->local_methods();
10410 if (methods
== NULL
)
10411 return TRAVERSE_CONTINUE
;
10412 for (Typed_identifier_list::const_iterator p
= methods
->begin();
10413 p
!= methods
->end();
10415 if (p
->name().empty() && p
->type()->named_type() == this->find_type_
)
10417 this->found_
= true;
10418 return TRAVERSE_EXIT
;
10422 return TRAVERSE_CONTINUE
;
10425 // Verify that a named type does not refer to itself.
10428 Named_type::do_verify()
10430 if (this->is_verified_
)
10432 this->is_verified_
= true;
10434 if (this->is_error_
)
10437 if (this->is_alias_
)
10439 Find_alias
find(this);
10440 Type::traverse(this->type_
, &find
);
10443 go_error_at(this->location_
, "invalid recursive alias %qs",
10444 this->message_name().c_str());
10445 this->is_error_
= true;
10450 Find_type_use
find(this);
10451 Type::traverse(this->type_
, &find
);
10454 go_error_at(this->location_
, "invalid recursive type %qs",
10455 this->message_name().c_str());
10456 this->is_error_
= true;
10460 // Check whether any of the local methods overloads an existing
10461 // struct field or interface method. We don't need to check the
10462 // list of methods against itself: that is handled by the Bindings
10464 if (this->local_methods_
!= NULL
)
10466 Struct_type
* st
= this->type_
->struct_type();
10469 for (Bindings::const_declarations_iterator p
=
10470 this->local_methods_
->begin_declarations();
10471 p
!= this->local_methods_
->end_declarations();
10474 const std::string
& name(p
->first
);
10475 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
10477 go_error_at(p
->second
->location(),
10478 "method %qs redeclares struct field name",
10479 Gogo::message_name(name
).c_str());
10488 // Return whether this type is or contains a pointer.
10491 Named_type::do_has_pointer() const
10495 this->seen_
= true;
10496 bool ret
= this->type_
->has_pointer();
10497 this->seen_
= false;
10501 // Return whether comparisons for this type can use the identity
10505 Named_type::do_compare_is_identity(Gogo
* gogo
)
10507 // We don't use this->seen_ here because compare_is_identity may
10508 // call base() later, and that will mess up if seen_ is set here.
10509 if (this->seen_in_compare_is_identity_
)
10511 this->seen_in_compare_is_identity_
= true;
10512 bool ret
= this->type_
->compare_is_identity(gogo
);
10513 this->seen_in_compare_is_identity_
= false;
10517 // Return whether this type is reflexive--whether it is always equal
10521 Named_type::do_is_reflexive()
10523 if (this->seen_in_compare_is_identity_
)
10525 this->seen_in_compare_is_identity_
= true;
10526 bool ret
= this->type_
->is_reflexive();
10527 this->seen_in_compare_is_identity_
= false;
10531 // Return whether this type needs a key update when used as a map key.
10534 Named_type::do_needs_key_update()
10536 if (this->seen_in_compare_is_identity_
)
10538 this->seen_in_compare_is_identity_
= true;
10539 bool ret
= this->type_
->needs_key_update();
10540 this->seen_in_compare_is_identity_
= false;
10544 // Return a hash code. This is used for method lookup. We simply
10545 // hash on the name itself.
10548 Named_type::do_hash_for_method(Gogo
* gogo
, int) const
10550 if (this->is_error_
)
10553 // Aliases are handled in Type::hash_for_method.
10554 go_assert(!this->is_alias_
);
10556 const std::string
& name(this->named_object()->name());
10557 unsigned int ret
= Gogo::hash_string(name
, 0);
10559 // GOGO will be NULL here when called from Type_hash_identical.
10560 // That is OK because that is only used for internal hash tables
10561 // where we are going to be comparing named types for equality. In
10562 // other cases, which are cases where the runtime is going to
10563 // compare hash codes to see if the types are the same, we need to
10564 // include the pkgpath in the hash.
10565 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
10567 const Package
* package
= this->named_object()->package();
10568 if (package
== NULL
)
10569 ret
= Gogo::hash_string(gogo
->pkgpath(), ret
);
10571 ret
= Gogo::hash_string(package
->pkgpath(), ret
);
10577 // Convert a named type to the backend representation. In order to
10578 // get dependencies right, we fill in a dummy structure for this type,
10579 // then convert all the dependencies, then complete this type. When
10580 // this function is complete, the size of the type is known.
10583 Named_type::convert(Gogo
* gogo
)
10585 if (this->is_error_
|| this->is_converted_
)
10588 this->create_placeholder(gogo
);
10590 // If we are called to turn unsafe.Sizeof into a constant, we may
10591 // not have verified the type yet. We have to make sure it is
10592 // verified, since that sets the list of dependencies.
10595 // Convert all the dependencies. If they refer indirectly back to
10596 // this type, they will pick up the intermediate representation we just
10598 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
10599 p
!= this->dependencies_
.end();
10601 (*p
)->convert(gogo
);
10603 // Complete this type.
10604 Btype
* bt
= this->named_btype_
;
10605 Type
* base
= this->type_
->base();
10606 switch (base
->classification())
10621 case TYPE_FUNCTION
:
10623 // The size of these types is already correct. We don't worry
10624 // about filling them in until later, when we also track
10625 // circular references.
10630 std::vector
<Backend::Btyped_identifier
> bfields
;
10631 get_backend_struct_fields(gogo
, base
->struct_type(), true, &bfields
);
10632 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10633 bt
= gogo
->backend()->error_type();
10638 // Slice types were completed in create_placeholder.
10639 if (!base
->is_slice_type())
10641 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
10642 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
10643 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
10644 bt
= gogo
->backend()->error_type();
10648 case TYPE_INTERFACE
:
10649 // Interface types were completed in create_placeholder.
10657 case TYPE_CALL_MULTIPLE_RESULT
:
10663 this->named_btype_
= bt
;
10664 this->is_converted_
= true;
10665 this->is_placeholder_
= false;
10668 // Create the placeholder for a named type. This is the first step in
10669 // converting to the backend representation.
10672 Named_type::create_placeholder(Gogo
* gogo
)
10674 if (this->is_error_
)
10675 this->named_btype_
= gogo
->backend()->error_type();
10677 if (this->named_btype_
!= NULL
)
10680 // Create the structure for this type. Note that because we call
10681 // base() here, we don't attempt to represent a named type defined
10682 // as another named type. Instead both named types will point to
10683 // different base representations.
10684 Type
* base
= this->type_
->base();
10686 bool set_name
= true;
10687 switch (base
->classification())
10690 this->is_error_
= true;
10691 this->named_btype_
= gogo
->backend()->error_type();
10701 // These are simple basic types, we can just create them
10703 bt
= Type::get_named_base_btype(gogo
, base
);
10708 // All maps and channels have the same backend representation.
10709 bt
= Type::get_named_base_btype(gogo
, base
);
10712 case TYPE_FUNCTION
:
10715 bool for_function
= base
->classification() == TYPE_FUNCTION
;
10716 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
10724 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10726 this->is_placeholder_
= true;
10731 if (base
->is_slice_type())
10732 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10736 bt
= gogo
->backend()->placeholder_array_type(this->name(),
10738 this->is_placeholder_
= true;
10743 case TYPE_INTERFACE
:
10744 if (base
->interface_type()->is_empty())
10745 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
10748 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10756 case TYPE_CALL_MULTIPLE_RESULT
:
10763 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
10765 this->named_btype_
= bt
;
10767 if (base
->is_slice_type())
10769 // We do not record slices as dependencies of other types,
10770 // because we can fill them in completely here with the final
10772 std::vector
<Backend::Btyped_identifier
> bfields
;
10773 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
10774 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10775 this->named_btype_
= gogo
->backend()->error_type();
10777 else if (base
->interface_type() != NULL
10778 && !base
->interface_type()->is_empty())
10780 // We do not record interfaces as dependencies of other types,
10781 // because we can fill them in completely here with the final
10783 std::vector
<Backend::Btyped_identifier
> bfields
;
10784 get_backend_interface_fields(gogo
, base
->interface_type(), true,
10786 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10787 this->named_btype_
= gogo
->backend()->error_type();
10791 // Get the backend representation for a named type.
10794 Named_type::do_get_backend(Gogo
* gogo
)
10796 if (this->is_error_
)
10797 return gogo
->backend()->error_type();
10799 Btype
* bt
= this->named_btype_
;
10801 if (!gogo
->named_types_are_converted())
10803 // We have not completed converting named types. NAMED_BTYPE_
10804 // is a placeholder and we shouldn't do anything further.
10808 // We don't build dependencies for types whose sizes do not
10809 // change or are not relevant, so we may see them here while
10810 // converting types.
10811 this->create_placeholder(gogo
);
10812 bt
= this->named_btype_
;
10813 go_assert(bt
!= NULL
);
10817 // We are not converting types. This should only be called if the
10818 // type has already been converted.
10819 if (!this->is_converted_
)
10821 go_assert(saw_errors());
10822 return gogo
->backend()->error_type();
10825 go_assert(bt
!= NULL
);
10827 // Complete the backend representation.
10828 Type
* base
= this->type_
->base();
10830 switch (base
->classification())
10833 return gogo
->backend()->error_type();
10847 if (!this->seen_in_get_backend_
)
10849 this->seen_in_get_backend_
= true;
10850 base
->struct_type()->finish_backend_fields(gogo
);
10851 this->seen_in_get_backend_
= false;
10856 if (!this->seen_in_get_backend_
)
10858 this->seen_in_get_backend_
= true;
10859 base
->array_type()->finish_backend_element(gogo
);
10860 this->seen_in_get_backend_
= false;
10864 case TYPE_INTERFACE
:
10865 if (!this->seen_in_get_backend_
)
10867 this->seen_in_get_backend_
= true;
10868 base
->interface_type()->finish_backend_methods(gogo
);
10869 this->seen_in_get_backend_
= false;
10873 case TYPE_FUNCTION
:
10874 // Don't build a circular data structure. GENERIC can't handle
10876 if (this->seen_in_get_backend_
)
10877 return gogo
->backend()->circular_pointer_type(bt
, true);
10878 this->seen_in_get_backend_
= true;
10879 bt1
= Type::get_named_base_btype(gogo
, base
);
10880 this->seen_in_get_backend_
= false;
10881 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10882 bt
= gogo
->backend()->error_type();
10886 // Don't build a circular data structure. GENERIC can't handle
10888 if (this->seen_in_get_backend_
)
10889 return gogo
->backend()->circular_pointer_type(bt
, false);
10890 this->seen_in_get_backend_
= true;
10891 bt1
= Type::get_named_base_btype(gogo
, base
);
10892 this->seen_in_get_backend_
= false;
10893 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10894 bt
= gogo
->backend()->error_type();
10899 case TYPE_CALL_MULTIPLE_RESULT
:
10908 // Build a type descriptor for a named type.
10911 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
10913 if (this->is_error_
)
10914 return Expression::make_error(this->location_
);
10915 if (name
== NULL
&& this->is_alias_
)
10917 if (this->seen_alias_
)
10918 return Expression::make_error(this->location_
);
10919 this->seen_alias_
= true;
10920 Expression
* ret
= this->type_
->type_descriptor(gogo
, NULL
);
10921 this->seen_alias_
= false;
10925 // If NAME is not NULL, then we don't really want the type
10926 // descriptor for this type; we want the descriptor for the
10927 // underlying type, giving it the name NAME.
10928 return this->named_type_descriptor(gogo
, this->type_
,
10929 name
== NULL
? this : name
);
10932 // Add to the reflection string. This is used mostly for the name of
10933 // the type used in a type descriptor, not for actual reflection
10937 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
10939 this->append_reflection_type_name(gogo
, false, ret
);
10942 // Add to the reflection string. For an alias we normally use the
10943 // real name, but if USE_ALIAS is true we use the alias name itself.
10946 Named_type::append_reflection_type_name(Gogo
* gogo
, bool use_alias
,
10947 std::string
* ret
) const
10949 if (this->is_error_
)
10951 if (this->is_alias_
&& !use_alias
)
10953 if (this->seen_alias_
)
10955 this->seen_alias_
= true;
10956 this->append_reflection(this->type_
, gogo
, ret
);
10957 this->seen_alias_
= false;
10960 if (!this->is_builtin())
10962 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
10963 // make a unique reflection string, so that the type
10964 // canonicalization in the reflect package will work. In order
10965 // to be compatible with the gc compiler, we put tabs into the
10966 // package path, so that the reflect methods can discard it.
10967 const Package
* package
= this->named_object_
->package();
10968 ret
->push_back('\t');
10969 ret
->append(package
!= NULL
10970 ? package
->pkgpath_symbol()
10971 : gogo
->pkgpath_symbol());
10972 ret
->push_back('\t');
10973 ret
->append(package
!= NULL
10974 ? package
->package_name()
10975 : gogo
->package_name());
10976 ret
->push_back('.');
10978 if (this->in_function_
!= NULL
)
10980 ret
->push_back('\t');
10981 const Typed_identifier
* rcvr
=
10982 this->in_function_
->func_value()->type()->receiver();
10985 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
10986 ret
->append(Gogo::unpack_hidden_name(rcvr_type
->name()));
10987 ret
->push_back('.');
10989 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
10990 ret
->push_back('$');
10991 if (this->in_function_index_
> 0)
10994 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
10996 ret
->push_back('$');
10998 ret
->push_back('\t');
11000 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
11003 // Import a named type. This is only used for export format versions
11004 // before version 3.
11007 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
11009 imp
->require_c_string("type ");
11010 Type
*type
= imp
->read_type();
11011 *ptype
= type
->named_type();
11012 go_assert(*ptype
!= NULL
);
11013 imp
->require_semicolon_if_old_version();
11014 imp
->require_c_string("\n");
11017 // Export the type when it is referenced by another type. In this
11018 // case Export::export_type will already have issued the name. The
11019 // output always ends with a newline, since that is convenient if
11020 // there are methods.
11023 Named_type::do_export(Export
* exp
) const
11025 exp
->write_type(this->type_
);
11026 exp
->write_c_string("\n");
11028 // To save space, we only export the methods directly attached to
11030 Bindings
* methods
= this->local_methods_
;
11031 if (methods
== NULL
)
11034 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
11035 p
!= methods
->end_definitions();
11038 exp
->write_c_string(" ");
11039 (*p
)->export_named_object(exp
);
11042 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
11043 p
!= methods
->end_declarations();
11046 if (p
->second
->is_function_declaration())
11048 exp
->write_c_string(" ");
11049 p
->second
->export_named_object(exp
);
11054 // Make a named type.
11057 Type::make_named_type(Named_object
* named_object
, Type
* type
,
11060 return new Named_type(named_object
, type
, location
);
11063 // Finalize the methods for TYPE. It will be a named type or a struct
11064 // type. This sets *ALL_METHODS to the list of methods, and builds
11065 // all required stubs.
11068 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
11069 Methods
** all_methods
)
11071 *all_methods
= new Methods();
11072 std::vector
<const Named_type
*> seen
;
11073 Type::add_methods_for_type(type
, NULL
, 0, false, false, &seen
, *all_methods
);
11074 if ((*all_methods
)->empty())
11076 delete *all_methods
;
11077 *all_methods
= NULL
;
11079 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
11080 if (type
->is_direct_iface_type())
11081 Type::build_direct_iface_stub_methods(gogo
, type
, *all_methods
, location
);
11084 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
11085 // build up the struct field indexes as we go. DEPTH is the depth of
11086 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
11087 // adding these methods for an anonymous field with pointer type.
11088 // NEEDS_STUB_METHOD is true if we need to use a stub method which
11089 // calls the real method. TYPES_SEEN is used to avoid infinite
11093 Type::add_methods_for_type(const Type
* type
,
11094 const Method::Field_indexes
* field_indexes
,
11095 unsigned int depth
,
11096 bool is_embedded_pointer
,
11097 bool needs_stub_method
,
11098 std::vector
<const Named_type
*>* seen
,
11101 // Pointer types may not have methods.
11102 if (type
->points_to() != NULL
)
11105 const Named_type
* nt
= type
->named_type();
11108 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11116 seen
->push_back(nt
);
11118 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
11119 is_embedded_pointer
, needs_stub_method
,
11123 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
11124 is_embedded_pointer
, needs_stub_method
,
11127 // If we are called with depth > 0, then we are looking at an
11128 // anonymous field of a struct. If such a field has interface type,
11129 // then we need to add the interface methods. We don't want to add
11130 // them when depth == 0, because we will already handle them
11131 // following the usual rules for an interface type.
11133 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
11139 // Add the local methods for the named type NT to *METHODS. The
11140 // parameters are as for add_methods_to_type.
11143 Type::add_local_methods_for_type(const Named_type
* nt
,
11144 const Method::Field_indexes
* field_indexes
,
11145 unsigned int depth
,
11146 bool is_embedded_pointer
,
11147 bool needs_stub_method
,
11150 const Bindings
* local_methods
= nt
->local_methods();
11151 if (local_methods
== NULL
)
11154 for (Bindings::const_declarations_iterator p
=
11155 local_methods
->begin_declarations();
11156 p
!= local_methods
->end_declarations();
11159 Named_object
* no
= p
->second
;
11160 bool is_value_method
= (is_embedded_pointer
11161 || !Type::method_expects_pointer(no
));
11162 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
11163 (needs_stub_method
|| depth
> 0));
11164 if (!methods
->insert(no
->name(), m
))
11169 // Add the embedded methods for TYPE to *METHODS. These are the
11170 // methods attached to anonymous fields. The parameters are as for
11171 // add_methods_to_type.
11174 Type::add_embedded_methods_for_type(const Type
* type
,
11175 const Method::Field_indexes
* field_indexes
,
11176 unsigned int depth
,
11177 bool is_embedded_pointer
,
11178 bool needs_stub_method
,
11179 std::vector
<const Named_type
*>* seen
,
11182 // Look for anonymous fields in TYPE. TYPE has fields if it is a
11184 const Struct_type
* st
= type
->struct_type();
11188 const Struct_field_list
* fields
= st
->fields();
11189 if (fields
== NULL
)
11192 unsigned int i
= 0;
11193 for (Struct_field_list::const_iterator pf
= fields
->begin();
11194 pf
!= fields
->end();
11197 if (!pf
->is_anonymous())
11200 Type
* ftype
= pf
->type();
11201 bool is_pointer
= false;
11202 if (ftype
->points_to() != NULL
)
11204 ftype
= ftype
->points_to();
11207 Named_type
* fnt
= ftype
->named_type();
11210 // This is an error, but it will be diagnosed elsewhere.
11214 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
11215 sub_field_indexes
->next
= field_indexes
;
11216 sub_field_indexes
->field_index
= i
;
11218 Methods tmp_methods
;
11219 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
11220 (is_embedded_pointer
|| is_pointer
),
11226 // Check if there are promoted methods that conflict with field names and
11227 // don't add them to the method map.
11228 for (Methods::const_iterator p
= tmp_methods
.begin();
11229 p
!= tmp_methods
.end();
11232 bool found
= false;
11233 for (Struct_field_list::const_iterator fp
= fields
->begin();
11234 fp
!= fields
->end();
11237 if (fp
->field_name() == p
->first
)
11244 !methods
->insert(p
->first
, p
->second
))
11250 // If TYPE is an interface type, then add its method to *METHODS.
11251 // This is for interface methods attached to an anonymous field. The
11252 // parameters are as for add_methods_for_type.
11255 Type::add_interface_methods_for_type(const Type
* type
,
11256 const Method::Field_indexes
* field_indexes
,
11257 unsigned int depth
,
11260 const Interface_type
* it
= type
->interface_type();
11264 const Typed_identifier_list
* imethods
= it
->methods();
11265 if (imethods
== NULL
)
11268 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
11269 pm
!= imethods
->end();
11272 Function_type
* fntype
= pm
->type()->function_type();
11273 if (fntype
== NULL
)
11275 // This is an error, but it should be reported elsewhere
11276 // when we look at the methods for IT.
11279 go_assert(!fntype
->is_method());
11280 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
11281 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
11282 field_indexes
, depth
);
11283 if (!methods
->insert(pm
->name(), m
))
11288 // Build stub methods for TYPE as needed. METHODS is the set of
11289 // methods for the type. A stub method may be needed when a type
11290 // inherits a method from an anonymous field. When we need the
11291 // address of the method, as in a type descriptor, we need to build a
11292 // little stub which does the required field dereferences and jumps to
11293 // the real method. LOCATION is the location of the type definition.
11296 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
11299 if (methods
== NULL
)
11301 for (Methods::const_iterator p
= methods
->begin();
11302 p
!= methods
->end();
11305 Method
* m
= p
->second
;
11306 if (m
->is_ambiguous() || !m
->needs_stub_method())
11309 const std::string
& name(p
->first
);
11311 // Build a stub method.
11313 const Function_type
* fntype
= m
->type();
11315 static unsigned int counter
;
11317 snprintf(buf
, sizeof buf
, "$this%u", counter
);
11320 Type
* receiver_type
= const_cast<Type
*>(type
);
11321 if (!m
->is_value_method())
11322 receiver_type
= Type::make_pointer_type(receiver_type
);
11323 Location receiver_location
= m
->receiver_location();
11324 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
11325 receiver_location
);
11327 const Typed_identifier_list
* fnparams
= fntype
->parameters();
11328 Typed_identifier_list
* stub_params
;
11329 if (fnparams
== NULL
|| fnparams
->empty())
11330 stub_params
= NULL
;
11333 // We give each stub parameter a unique name.
11334 stub_params
= new Typed_identifier_list();
11335 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
11336 pp
!= fnparams
->end();
11340 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
11341 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
11347 const Typed_identifier_list
* fnresults
= fntype
->results();
11348 Typed_identifier_list
* stub_results
;
11349 if (fnresults
== NULL
|| fnresults
->empty())
11350 stub_results
= NULL
;
11353 // We create the result parameters without any names, since
11354 // we won't refer to them.
11355 stub_results
= new Typed_identifier_list();
11356 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
11357 pr
!= fnresults
->end();
11359 stub_results
->push_back(Typed_identifier("", pr
->type(),
11363 Function_type
* stub_type
= Type::make_function_type(receiver
,
11366 fntype
->location());
11367 if (fntype
->is_varargs())
11368 stub_type
->set_is_varargs();
11370 // We only create the function in the package which creates the
11372 const Package
* package
;
11373 if (type
->named_type() == NULL
)
11376 package
= type
->named_type()->named_object()->package();
11377 std::string stub_name
= gogo
->stub_method_name(package
, name
);
11378 Named_object
* stub
;
11379 if (package
!= NULL
)
11380 stub
= Named_object::make_function_declaration(stub_name
, package
,
11381 stub_type
, location
);
11384 stub
= gogo
->start_function(stub_name
, stub_type
, false,
11385 fntype
->location());
11386 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
11387 fntype
->is_varargs(), location
);
11388 gogo
->finish_function(fntype
->location());
11390 if (type
->named_type() == NULL
&& stub
->is_function())
11391 stub
->func_value()->set_is_unnamed_type_stub_method();
11392 if (m
->nointerface() && stub
->is_function())
11393 stub
->func_value()->set_nointerface();
11396 m
->set_stub_object(stub
);
11400 // Build a stub method which adjusts the receiver as required to call
11401 // METHOD. RECEIVER_NAME is the name we used for the receiver.
11402 // PARAMS is the list of function parameters.
11405 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
11406 const char* receiver_name
,
11407 const Typed_identifier_list
* params
,
11411 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
11412 go_assert(receiver_object
!= NULL
);
11414 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
11415 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
11416 if (expr
->type()->points_to() == NULL
)
11417 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11419 Expression_list
* arguments
;
11420 if (params
== NULL
|| params
->empty())
11424 arguments
= new Expression_list();
11425 for (Typed_identifier_list::const_iterator p
= params
->begin();
11426 p
!= params
->end();
11429 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
11430 go_assert(param
!= NULL
);
11431 Expression
* param_ref
= Expression::make_var_reference(param
,
11433 arguments
->push_back(param_ref
);
11437 Expression
* func
= method
->bind_method(expr
, location
);
11438 go_assert(func
!= NULL
);
11439 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
11442 gogo
->add_statement(Statement::make_return_from_call(call
, location
));
11445 // Build direct interface stub methods for TYPE as needed. METHODS
11446 // is the set of methods for the type. LOCATION is the location of
11447 // the type definition.
11449 // This is for an interface holding a pointer to the type and invoking
11450 // a value method. The interface data is the pointer, and is passed
11451 // to the stub, which dereferences it and passes to the actual method.
11454 Type::build_direct_iface_stub_methods(Gogo
* gogo
, const Type
* type
,
11455 Methods
* methods
, Location loc
)
11457 if (methods
== NULL
)
11460 for (Methods::const_iterator p
= methods
->begin();
11461 p
!= methods
->end();
11464 Method
* m
= p
->second
;
11465 if (!m
->is_value_method())
11468 Type
* receiver_type
= const_cast<Type
*>(type
);
11469 receiver_type
= Type::make_pointer_type(receiver_type
);
11470 const std::string
& name(p
->first
);
11471 Function_type
* fntype
= m
->type();
11473 static unsigned int counter
;
11475 snprintf(buf
, sizeof buf
, "$ptr%u", counter
);
11477 Typed_identifier
* receiver
=
11478 new Typed_identifier(buf
, receiver_type
, m
->receiver_location());
11480 const Typed_identifier_list
* params
= fntype
->parameters();
11481 Typed_identifier_list
* stub_params
;
11482 if (params
== NULL
|| params
->empty())
11483 stub_params
= NULL
;
11486 // We give each stub parameter a unique name.
11487 stub_params
= new Typed_identifier_list();
11488 for (Typed_identifier_list::const_iterator pp
= params
->begin();
11489 pp
!= params
->end();
11493 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
11494 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
11500 const Typed_identifier_list
* fnresults
= fntype
->results();
11501 Typed_identifier_list
* stub_results
;
11502 if (fnresults
== NULL
|| fnresults
->empty())
11503 stub_results
= NULL
;
11506 // We create the result parameters without any names, since
11507 // we won't refer to them.
11508 stub_results
= new Typed_identifier_list();
11509 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
11510 pr
!= fnresults
->end();
11512 stub_results
->push_back(Typed_identifier("", pr
->type(),
11516 Function_type
* stub_type
= Type::make_function_type(receiver
,
11519 fntype
->location());
11520 if (fntype
->is_varargs())
11521 stub_type
->set_is_varargs();
11523 // We only create the function in the package which creates the
11525 const Package
* package
;
11526 if (type
->named_type() == NULL
)
11529 package
= type
->named_type()->named_object()->package();
11531 std::string stub_name
= gogo
->stub_method_name(package
, name
) + "2";
11532 Named_object
* stub
;
11533 if (package
!= NULL
)
11534 stub
= Named_object::make_function_declaration(stub_name
, package
,
11538 stub
= gogo
->start_function(stub_name
, stub_type
, false,
11539 fntype
->location());
11540 Type::build_one_iface_stub_method(gogo
, m
, buf
, stub_params
,
11541 fntype
->is_varargs(), loc
);
11542 gogo
->finish_function(fntype
->location());
11544 if (type
->named_type() == NULL
&& stub
->is_function())
11545 stub
->func_value()->set_is_unnamed_type_stub_method();
11546 if (m
->nointerface() && stub
->is_function())
11547 stub
->func_value()->set_nointerface();
11550 m
->set_iface_stub_object(stub
);
11554 // Build a stub method for METHOD of direct interface type T.
11555 // RECEIVER_NAME is the name we used for the receiver.
11556 // PARAMS is the list of function parameters.
11558 // The stub looks like
11560 // func ($ptr *T, PARAMS) {
11561 // (*$ptr).METHOD(PARAMS)
11565 Type::build_one_iface_stub_method(Gogo
* gogo
, Method
* method
,
11566 const char* receiver_name
,
11567 const Typed_identifier_list
* params
,
11568 bool is_varargs
, Location loc
)
11570 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
11571 go_assert(receiver_object
!= NULL
);
11573 Expression
* expr
= Expression::make_var_reference(receiver_object
, loc
);
11574 expr
= Expression::make_dereference(expr
,
11575 Expression::NIL_CHECK_DEFAULT
,
11578 Expression_list
* arguments
;
11579 if (params
== NULL
|| params
->empty())
11583 arguments
= new Expression_list();
11584 for (Typed_identifier_list::const_iterator p
= params
->begin();
11585 p
!= params
->end();
11588 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
11589 go_assert(param
!= NULL
);
11590 Expression
* param_ref
= Expression::make_var_reference(param
,
11592 arguments
->push_back(param_ref
);
11596 Expression
* func
= method
->bind_method(expr
, loc
);
11597 go_assert(func
!= NULL
);
11598 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
11601 gogo
->add_statement(Statement::make_return_from_call(call
, loc
));
11604 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
11605 // in reverse order.
11608 Type::apply_field_indexes(Expression
* expr
,
11609 const Method::Field_indexes
* field_indexes
,
11612 if (field_indexes
== NULL
)
11614 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
11615 Struct_type
* stype
= expr
->type()->deref()->struct_type();
11616 go_assert(stype
!= NULL
11617 && field_indexes
->field_index
< stype
->field_count());
11618 if (expr
->type()->struct_type() == NULL
)
11620 go_assert(expr
->type()->points_to() != NULL
);
11621 expr
= Expression::make_dereference(expr
, Expression::NIL_CHECK_DEFAULT
,
11623 go_assert(expr
->type()->struct_type() == stype
);
11625 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
11629 // Return whether NO is a method for which the receiver is a pointer.
11632 Type::method_expects_pointer(const Named_object
* no
)
11634 const Function_type
*fntype
;
11635 if (no
->is_function())
11636 fntype
= no
->func_value()->type();
11637 else if (no
->is_function_declaration())
11638 fntype
= no
->func_declaration_value()->type();
11641 return fntype
->receiver()->type()->points_to() != NULL
;
11644 // Given a set of methods for a type, METHODS, return the method NAME,
11645 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
11646 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
11647 // but is ambiguous (and return NULL).
11650 Type::method_function(const Methods
* methods
, const std::string
& name
,
11651 bool* is_ambiguous
)
11653 if (is_ambiguous
!= NULL
)
11654 *is_ambiguous
= false;
11655 if (methods
== NULL
)
11657 Methods::const_iterator p
= methods
->find(name
);
11658 if (p
== methods
->end())
11660 Method
* m
= p
->second
;
11661 if (m
->is_ambiguous())
11663 if (is_ambiguous
!= NULL
)
11664 *is_ambiguous
= true;
11670 // Return a pointer to the interface method table for TYPE for the
11671 // interface INTERFACE.
11674 Type::interface_method_table(Type
* type
,
11675 Interface_type
*interface
,
11677 Interface_method_tables
** method_tables
,
11678 Interface_method_tables
** pointer_tables
)
11680 go_assert(!interface
->is_empty());
11682 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
11685 *pimt
= new Interface_method_tables(5);
11687 std::pair
<Interface_type
*, Expression
*> val(interface
, NULL
);
11688 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
11690 Location loc
= Linemap::predeclared_location();
11693 // This is a new entry in the hash table.
11694 go_assert(ins
.first
->second
== NULL
);
11695 ins
.first
->second
=
11696 Expression::make_interface_mtable_ref(interface
, type
, is_pointer
, loc
);
11698 return Expression::make_unary(OPERATOR_AND
, ins
.first
->second
, loc
);
11701 // Look for field or method NAME for TYPE. Return an Expression for
11702 // the field or method bound to EXPR. If there is no such field or
11703 // method, give an appropriate error and return an error expression.
11706 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
11707 const std::string
& name
,
11710 if (type
->deref()->is_error_type())
11711 return Expression::make_error(location
);
11713 const Named_type
* nt
= type
->deref()->named_type();
11714 const Struct_type
* st
= type
->deref()->struct_type();
11715 const Interface_type
* it
= type
->interface_type();
11717 // If this is a pointer to a pointer, then it is possible that the
11718 // pointed-to type has methods.
11719 bool dereferenced
= false;
11723 && type
->points_to() != NULL
11724 && type
->points_to()->points_to() != NULL
)
11726 expr
= Expression::make_dereference(expr
, Expression::NIL_CHECK_DEFAULT
,
11728 type
= type
->points_to();
11729 if (type
->deref()->is_error_type())
11730 return Expression::make_error(location
);
11731 nt
= type
->points_to()->named_type();
11732 st
= type
->points_to()->struct_type();
11733 dereferenced
= true;
11736 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
11737 || expr
->is_addressable());
11738 std::vector
<const Named_type
*> seen
;
11739 bool is_method
= false;
11740 bool found_pointer_method
= false;
11741 std::string ambig1
;
11742 std::string ambig2
;
11743 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
11744 &seen
, NULL
, &is_method
,
11745 &found_pointer_method
, &ambig1
, &ambig2
))
11750 go_assert(st
!= NULL
);
11751 if (type
->struct_type() == NULL
)
11755 go_error_at(location
, "pointer type has no field %qs",
11756 Gogo::message_name(name
).c_str());
11757 return Expression::make_error(location
);
11759 go_assert(type
->points_to() != NULL
);
11760 expr
= Expression::make_dereference(expr
,
11761 Expression::NIL_CHECK_DEFAULT
,
11763 go_assert(expr
->type()->struct_type() == st
);
11765 ret
= st
->field_reference(expr
, name
, location
);
11768 go_error_at(location
, "type has no field %qs",
11769 Gogo::message_name(name
).c_str());
11770 return Expression::make_error(location
);
11773 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11774 ret
= Expression::make_interface_field_reference(expr
, name
,
11780 m
= nt
->method_function(name
, NULL
);
11781 else if (st
!= NULL
)
11782 m
= st
->method_function(name
, NULL
);
11785 go_assert(m
!= NULL
);
11788 go_error_at(location
,
11789 "calling method %qs requires explicit dereference",
11790 Gogo::message_name(name
).c_str());
11791 return Expression::make_error(location
);
11793 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
11794 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11795 ret
= m
->bind_method(expr
, location
);
11797 go_assert(ret
!= NULL
);
11802 if (Gogo::is_erroneous_name(name
))
11804 // An error was already reported.
11806 else if (!ambig1
.empty())
11807 go_error_at(location
, "%qs is ambiguous via %qs and %qs",
11808 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
11810 else if (found_pointer_method
)
11811 go_error_at(location
, "method requires a pointer receiver");
11812 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
11813 go_error_at(location
,
11814 ("reference to field %qs in object which "
11815 "has no fields or methods"),
11816 Gogo::message_name(name
).c_str());
11819 bool is_unexported
;
11820 // The test for 'a' and 'z' is to handle builtin names,
11821 // which are not hidden.
11822 if (!Gogo::is_hidden_name(name
) && (name
[0] < 'a' || name
[0] > 'z'))
11823 is_unexported
= false;
11826 std::string unpacked
= Gogo::unpack_hidden_name(name
);
11828 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
11833 go_error_at(location
, "reference to unexported field or method %qs",
11834 Gogo::message_name(name
).c_str());
11836 go_error_at(location
, "reference to undefined field or method %qs",
11837 Gogo::message_name(name
).c_str());
11839 return Expression::make_error(location
);
11843 // Look in TYPE for a field or method named NAME, return true if one
11844 // is found. This looks through embedded anonymous fields and handles
11845 // ambiguity. If a method is found, sets *IS_METHOD to true;
11846 // otherwise, if a field is found, set it to false. If
11847 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
11848 // whose address can not be taken. SEEN is used to avoid infinite
11849 // recursion on invalid types.
11851 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
11852 // method we couldn't use because it requires a pointer. LEVEL is
11853 // used for recursive calls, and can be NULL for a non-recursive call.
11854 // When this function returns false because it finds that the name is
11855 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
11856 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
11857 // will be unchanged.
11859 // This function just returns whether or not there is a field or
11860 // method, and whether it is a field or method. It doesn't build an
11861 // expression to refer to it. If it is a method, we then look in the
11862 // list of all methods for the type. If it is a field, the search has
11863 // to be done again, looking only for fields, and building up the
11864 // expression as we go.
11867 Type::find_field_or_method(const Type
* type
,
11868 const std::string
& name
,
11869 bool receiver_can_be_pointer
,
11870 std::vector
<const Named_type
*>* seen
,
11873 bool* found_pointer_method
,
11874 std::string
* ambig1
,
11875 std::string
* ambig2
)
11877 // Named types can have locally defined methods.
11878 const Named_type
* nt
= type
->unalias()->named_type();
11879 if (nt
== NULL
&& type
->points_to() != NULL
)
11880 nt
= type
->points_to()->unalias()->named_type();
11883 Named_object
* no
= nt
->find_local_method(name
);
11886 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
11892 // Record that we have found a pointer method in order to
11893 // give a better error message if we don't find anything
11895 *found_pointer_method
= true;
11898 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11904 // We've already seen this type when searching for methods.
11910 // Interface types can have methods.
11911 const Interface_type
* it
= type
->interface_type();
11912 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11918 // Struct types can have fields. They can also inherit fields and
11919 // methods from anonymous fields.
11920 const Struct_type
* st
= type
->deref()->struct_type();
11923 const Struct_field_list
* fields
= st
->fields();
11924 if (fields
== NULL
)
11928 seen
->push_back(nt
);
11930 int found_level
= 0;
11931 bool found_is_method
= false;
11932 std::string found_ambig1
;
11933 std::string found_ambig2
;
11934 const Struct_field
* found_parent
= NULL
;
11935 for (Struct_field_list::const_iterator pf
= fields
->begin();
11936 pf
!= fields
->end();
11939 if (pf
->is_field_name(name
))
11941 *is_method
= false;
11947 if (!pf
->is_anonymous())
11950 if (pf
->type()->deref()->is_error_type()
11951 || pf
->type()->deref()->is_undefined())
11954 Named_type
* fnt
= pf
->type()->named_type();
11956 fnt
= pf
->type()->deref()->named_type();
11957 go_assert(fnt
!= NULL
);
11959 // Methods with pointer receivers on embedded field are
11960 // inherited by the pointer to struct, and also by the struct
11961 // type if the field itself is a pointer.
11962 bool can_be_pointer
= (receiver_can_be_pointer
11963 || pf
->type()->points_to() != NULL
);
11964 int sublevel
= level
== NULL
? 1 : *level
+ 1;
11965 bool sub_is_method
;
11966 std::string subambig1
;
11967 std::string subambig2
;
11968 bool subfound
= Type::find_field_or_method(fnt
,
11974 found_pointer_method
,
11979 if (!subambig1
.empty())
11981 // The name was found via this field, but is ambiguous.
11982 // if the ambiguity is lower or at the same level as
11983 // anything else we have already found, then we want to
11984 // pass the ambiguity back to the caller.
11985 if (found_level
== 0 || sublevel
<= found_level
)
11987 found_ambig1
= (Gogo::message_name(pf
->field_name())
11988 + '.' + subambig1
);
11989 found_ambig2
= (Gogo::message_name(pf
->field_name())
11990 + '.' + subambig2
);
11991 found_level
= sublevel
;
11997 // The name was found via this field. Use the level to see
11998 // if we want to use this one, or whether it introduces an
12000 if (found_level
== 0 || sublevel
< found_level
)
12002 found_level
= sublevel
;
12003 found_is_method
= sub_is_method
;
12004 found_ambig1
.clear();
12005 found_ambig2
.clear();
12006 found_parent
= &*pf
;
12008 else if (sublevel
> found_level
)
12010 else if (found_ambig1
.empty())
12012 // We found an ambiguity.
12013 go_assert(found_parent
!= NULL
);
12014 found_ambig1
= Gogo::message_name(found_parent
->field_name());
12015 found_ambig2
= Gogo::message_name(pf
->field_name());
12019 // We found an ambiguity, but we already know of one.
12020 // Just report the earlier one.
12025 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
12026 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
12027 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
12028 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
12033 if (found_level
== 0)
12035 else if (found_is_method
12036 && type
->named_type() != NULL
12037 && type
->points_to() != NULL
)
12039 // If this is a method inherited from a struct field in a named pointer
12040 // type, it is invalid to automatically dereference the pointer to the
12041 // struct to find this method.
12043 *level
= found_level
;
12047 else if (!found_ambig1
.empty())
12049 go_assert(!found_ambig1
.empty());
12050 ambig1
->assign(found_ambig1
);
12051 ambig2
->assign(found_ambig2
);
12053 *level
= found_level
;
12059 *level
= found_level
;
12060 *is_method
= found_is_method
;
12065 // Return whether NAME is an unexported field or method for TYPE.
12068 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
12069 const std::string
& name
,
12070 std::vector
<const Named_type
*>* seen
)
12072 const Named_type
* nt
= type
->named_type();
12074 nt
= type
->deref()->named_type();
12077 if (nt
->is_unexported_local_method(gogo
, name
))
12080 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
12086 // We've already seen this type.
12092 const Interface_type
* it
= type
->interface_type();
12093 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
12096 type
= type
->deref();
12098 const Struct_type
* st
= type
->struct_type();
12099 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
12105 const Struct_field_list
* fields
= st
->fields();
12106 if (fields
== NULL
)
12110 seen
->push_back(nt
);
12112 for (Struct_field_list::const_iterator pf
= fields
->begin();
12113 pf
!= fields
->end();
12116 if (pf
->is_anonymous()
12117 && !pf
->type()->deref()->is_error_type()
12118 && !pf
->type()->deref()->is_undefined())
12120 Named_type
* subtype
= pf
->type()->named_type();
12121 if (subtype
== NULL
)
12122 subtype
= pf
->type()->deref()->named_type();
12123 if (subtype
== NULL
)
12125 // This is an error, but it will be diagnosed elsewhere.
12128 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
12143 // Class Forward_declaration.
12145 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
12146 : Type(TYPE_FORWARD
),
12147 named_object_(named_object
->resolve()), warned_(false)
12149 go_assert(this->named_object_
->is_unknown()
12150 || this->named_object_
->is_type_declaration());
12153 // Return the named object.
12156 Forward_declaration_type::named_object()
12158 return this->named_object_
->resolve();
12161 const Named_object
*
12162 Forward_declaration_type::named_object() const
12164 return this->named_object_
->resolve();
12167 // Return the name of the forward declared type.
12170 Forward_declaration_type::name() const
12172 return this->named_object()->name();
12175 // Warn about a use of a type which has been declared but not defined.
12178 Forward_declaration_type::warn() const
12180 Named_object
* no
= this->named_object_
->resolve();
12181 if (no
->is_unknown())
12183 // The name was not defined anywhere.
12184 if (!this->warned_
)
12186 go_error_at(this->named_object_
->location(),
12187 "use of undefined type %qs",
12188 no
->message_name().c_str());
12189 this->warned_
= true;
12192 else if (no
->is_type_declaration())
12194 // The name was seen as a type, but the type was never defined.
12195 if (no
->type_declaration_value()->using_type())
12197 go_error_at(this->named_object_
->location(),
12198 "use of undefined type %qs",
12199 no
->message_name().c_str());
12200 this->warned_
= true;
12205 // The name was defined, but not as a type.
12206 if (!this->warned_
)
12208 go_error_at(this->named_object_
->location(), "expected type");
12209 this->warned_
= true;
12214 // Get the base type of a declaration. This gives an error if the
12215 // type has not yet been defined.
12218 Forward_declaration_type::real_type()
12220 if (this->is_defined())
12222 Named_type
* nt
= this->named_object()->type_value();
12223 if (!nt
->is_valid())
12224 return Type::make_error_type();
12225 return this->named_object()->type_value();
12230 return Type::make_error_type();
12235 Forward_declaration_type::real_type() const
12237 if (this->is_defined())
12239 const Named_type
* nt
= this->named_object()->type_value();
12240 if (!nt
->is_valid())
12241 return Type::make_error_type();
12242 return this->named_object()->type_value();
12247 return Type::make_error_type();
12251 // Return whether the base type is defined.
12254 Forward_declaration_type::is_defined() const
12256 return this->named_object()->is_type();
12259 // Add a method. This is used when methods are defined before the
12263 Forward_declaration_type::add_method(const std::string
& name
,
12264 Function
* function
)
12266 Named_object
* no
= this->named_object();
12267 if (no
->is_unknown())
12268 no
->declare_as_type();
12269 return no
->type_declaration_value()->add_method(name
, function
);
12272 // Add a method declaration. This is used when methods are declared
12273 // before the type.
12276 Forward_declaration_type::add_method_declaration(const std::string
& name
,
12278 Function_type
* type
,
12281 Named_object
* no
= this->named_object();
12282 if (no
->is_unknown())
12283 no
->declare_as_type();
12284 Type_declaration
* td
= no
->type_declaration_value();
12285 return td
->add_method_declaration(name
, package
, type
, location
);
12288 // Add an already created object as a method.
12291 Forward_declaration_type::add_existing_method(Named_object
* nom
)
12293 Named_object
* no
= this->named_object();
12294 if (no
->is_unknown())
12295 no
->declare_as_type();
12296 no
->type_declaration_value()->add_existing_method(nom
);
12302 Forward_declaration_type::do_traverse(Traverse
* traverse
)
12304 if (this->is_defined()
12305 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
12306 return TRAVERSE_EXIT
;
12307 return TRAVERSE_CONTINUE
;
12310 // Verify the type.
12313 Forward_declaration_type::do_verify()
12315 if (!this->is_defined() && !this->is_nil_constant_as_type())
12323 // Get the backend representation for the type.
12326 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
12328 if (this->is_defined())
12329 return Type::get_named_base_btype(gogo
, this->real_type());
12332 return gogo
->backend()->error_type();
12334 // We represent an undefined type as a struct with no fields. That
12335 // should work fine for the backend, since the same case can arise
12337 std::vector
<Backend::Btyped_identifier
> fields
;
12338 Btype
* bt
= gogo
->backend()->struct_type(fields
);
12339 return gogo
->backend()->named_type(this->name(), bt
,
12340 this->named_object()->location());
12343 // Build a type descriptor for a forwarded type.
12346 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
12348 Location ploc
= Linemap::predeclared_location();
12349 if (!this->is_defined())
12350 return Expression::make_error(ploc
);
12353 Type
* t
= this->real_type();
12355 return this->named_type_descriptor(gogo
, t
, name
);
12357 return Expression::make_error(this->named_object_
->location());
12361 // The reflection string.
12364 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
12366 this->append_reflection(this->real_type(), gogo
, ret
);
12369 // Export a forward declaration. This can happen when a defined type
12370 // refers to a type which is only declared (and is presumably defined
12371 // in some other file in the same package).
12374 Forward_declaration_type::do_export(Export
*) const
12376 // If there is a base type, that should be exported instead of this.
12377 go_assert(!this->is_defined());
12379 // We don't output anything.
12382 // Make a forward declaration.
12385 Type::make_forward_declaration(Named_object
* named_object
)
12387 return new Forward_declaration_type(named_object
);
12390 // Class Typed_identifier_list.
12392 // Sort the entries by name.
12394 struct Typed_identifier_list_sort
12398 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
12400 return (Gogo::unpack_hidden_name(t1
.name())
12401 < Gogo::unpack_hidden_name(t2
.name()));
12406 Typed_identifier_list::sort_by_name()
12408 std::sort(this->entries_
.begin(), this->entries_
.end(),
12409 Typed_identifier_list_sort());
12415 Typed_identifier_list::traverse(Traverse
* traverse
) const
12417 for (Typed_identifier_list::const_iterator p
= this->begin();
12421 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
12422 return TRAVERSE_EXIT
;
12424 return TRAVERSE_CONTINUE
;
12429 Typed_identifier_list
*
12430 Typed_identifier_list::copy() const
12432 Typed_identifier_list
* ret
= new Typed_identifier_list();
12433 for (Typed_identifier_list::const_iterator p
= this->begin();
12436 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));