1 // types.cc -- Go frontend types.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
13 #include "go-diagnostics.h"
14 #include "go-encode-id.h"
16 #include "expressions.h"
17 #include "statements.h"
23 // Forward declarations so that we don't have to make types.h #include
27 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
29 std::vector
<Backend::Btyped_identifier
>* bfields
);
32 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
33 std::vector
<Backend::Btyped_identifier
>* bfields
);
36 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
38 std::vector
<Backend::Btyped_identifier
>* bfields
);
42 Type::Type(Type_classification classification
)
43 : classification_(classification
), btype_(NULL
), type_descriptor_var_(NULL
),
52 // Get the base type for a type--skip names and forward declarations.
57 switch (this->classification_
)
60 return this->named_type()->named_base();
62 return this->forward_declaration_type()->real_type()->base();
71 switch (this->classification_
)
74 return this->named_type()->named_base();
76 return this->forward_declaration_type()->real_type()->base();
82 // Skip defined forward declarations.
88 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
89 while (ftype
!= NULL
&& ftype
->is_defined())
91 t
= ftype
->real_type();
92 ftype
= t
->forward_declaration_type();
98 Type::forwarded() const
100 const Type
* t
= this;
101 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
102 while (ftype
!= NULL
&& ftype
->is_defined())
104 t
= ftype
->real_type();
105 ftype
= t
->forward_declaration_type();
110 // Skip alias definitions.
115 Type
* t
= this->forwarded();
116 Named_type
* nt
= t
->named_type();
117 while (nt
!= NULL
&& nt
->is_alias())
119 t
= nt
->real_type()->forwarded();
120 nt
= t
->named_type();
126 Type::unalias() const
128 const Type
* t
= this->forwarded();
129 const Named_type
* nt
= t
->named_type();
130 while (nt
!= NULL
&& nt
->is_alias())
132 t
= nt
->real_type()->forwarded();
133 nt
= t
->named_type();
138 // If this is a named type, return it. Otherwise, return NULL.
143 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
147 Type::named_type() const
149 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
152 // Return true if this type is not defined.
155 Type::is_undefined() const
157 return this->forwarded()->forward_declaration_type() != NULL
;
160 // Return true if this is a basic type: a type which is not composed
161 // of other types, and is not void.
164 Type::is_basic_type() const
166 switch (this->classification_
)
189 return this->base()->is_basic_type();
196 // Return true if this is an abstract type.
199 Type::is_abstract() const
201 switch (this->classification())
204 return this->integer_type()->is_abstract();
206 return this->float_type()->is_abstract();
208 return this->complex_type()->is_abstract();
210 return this->is_abstract_string_type();
212 return this->is_abstract_boolean_type();
218 // Return a non-abstract version of an abstract type.
221 Type::make_non_abstract_type()
223 go_assert(this->is_abstract());
224 switch (this->classification())
227 if (this->integer_type()->is_rune())
228 return Type::lookup_integer_type("int32");
230 return Type::lookup_integer_type("int");
232 return Type::lookup_float_type("float64");
234 return Type::lookup_complex_type("complex128");
236 return Type::lookup_string_type();
238 return Type::lookup_bool_type();
244 // Return true if this is an error type. Don't give an error if we
245 // try to dereference an undefined forwarding type, as this is called
246 // in the parser when the type may legitimately be undefined.
249 Type::is_error_type() const
251 const Type
* t
= this->forwarded();
252 // Note that we return false for an undefined forward type.
253 switch (t
->classification_
)
258 return t
->named_type()->is_named_error_type();
264 // If this is a pointer type, return the type to which it points.
265 // Otherwise, return NULL.
268 Type::points_to() const
270 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
272 return ptype
== NULL
? NULL
: ptype
->points_to();
275 // Return whether this is a slice type.
278 Type::is_slice_type() const
280 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
283 // Return whether this is the predeclared constant nil being used as a
287 Type::is_nil_constant_as_type() const
289 const Type
* t
= this->forwarded();
290 if (t
->forward_declaration_type() != NULL
)
292 const Named_object
* no
= t
->forward_declaration_type()->named_object();
293 if (no
->is_unknown())
294 no
= no
->unknown_value()->real_named_object();
297 && no
->const_value()->expr()->is_nil_expression())
306 Type::traverse(Type
* type
, Traverse
* traverse
)
308 go_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
309 || (traverse
->traverse_mask()
310 & Traverse::traverse_expressions
) != 0);
311 if (traverse
->remember_type(type
))
313 // We have already traversed this type.
314 return TRAVERSE_CONTINUE
;
316 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
318 int t
= traverse
->type(type
);
319 if (t
== TRAVERSE_EXIT
)
320 return TRAVERSE_EXIT
;
321 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
322 return TRAVERSE_CONTINUE
;
324 // An array type has an expression which we need to traverse if
325 // traverse_expressions is set.
326 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
327 return TRAVERSE_EXIT
;
328 return TRAVERSE_CONTINUE
;
331 // Default implementation for do_traverse for child class.
334 Type::do_traverse(Traverse
*)
336 return TRAVERSE_CONTINUE
;
339 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
340 // then return true for all erroneous types; this is used to avoid
341 // cascading errors. If REASON is not NULL, optionally set *REASON to
342 // the reason the types are not identical.
345 Type::are_identical(const Type
* t1
, const Type
* t2
, bool errors_are_identical
,
348 return Type::are_identical_cmp_tags(t1
, t2
, COMPARE_TAGS
,
349 errors_are_identical
, reason
);
352 // Like are_identical, but with a CMP_TAGS parameter.
355 Type::are_identical_cmp_tags(const Type
* t1
, const Type
* t2
, Cmp_tags cmp_tags
,
356 bool errors_are_identical
, std::string
* reason
)
358 if (t1
== NULL
|| t2
== NULL
)
360 // Something is wrong.
361 return errors_are_identical
? true : t1
== t2
;
364 // Skip defined forward declarations. Ignore aliases.
371 // An undefined forward declaration is an error.
372 if (t1
->forward_declaration_type() != NULL
373 || t2
->forward_declaration_type() != NULL
)
374 return errors_are_identical
;
376 // Avoid cascading errors with error types.
377 if (t1
->is_error_type() || t2
->is_error_type())
379 if (errors_are_identical
)
381 return t1
->is_error_type() && t2
->is_error_type();
384 // Get a good reason for the sink type. Note that the sink type on
385 // the left hand side of an assignment is handled in are_assignable.
386 if (t1
->is_sink_type() || t2
->is_sink_type())
389 *reason
= "invalid use of _";
393 // A named type is only identical to itself.
394 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
397 // Check type shapes.
398 if (t1
->classification() != t2
->classification())
401 switch (t1
->classification())
407 // These types are always identical.
411 return t1
->integer_type()->is_identical(t2
->integer_type());
414 return t1
->float_type()->is_identical(t2
->float_type());
417 return t1
->complex_type()->is_identical(t2
->complex_type());
420 return t1
->function_type()->is_identical(t2
->function_type(),
423 errors_are_identical
,
427 return Type::are_identical_cmp_tags(t1
->points_to(), t2
->points_to(),
428 cmp_tags
, errors_are_identical
,
432 return t1
->struct_type()->is_identical(t2
->struct_type(), cmp_tags
,
433 errors_are_identical
);
436 return t1
->array_type()->is_identical(t2
->array_type(), cmp_tags
,
437 errors_are_identical
);
440 return t1
->map_type()->is_identical(t2
->map_type(), cmp_tags
,
441 errors_are_identical
);
444 return t1
->channel_type()->is_identical(t2
->channel_type(), cmp_tags
,
445 errors_are_identical
);
448 return t1
->interface_type()->is_identical(t2
->interface_type(), cmp_tags
,
449 errors_are_identical
);
451 case TYPE_CALL_MULTIPLE_RESULT
:
453 *reason
= "invalid use of multiple-value function call";
461 // Return true if it's OK to have a binary operation with types LHS
462 // and RHS. This is not used for shifts or comparisons.
465 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
467 if (Type::are_identical(lhs
, rhs
, true, NULL
))
470 // A constant of abstract bool type may be mixed with any bool type.
471 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
472 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
475 // A constant of abstract string type may be mixed with any string
477 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
478 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
484 // A constant of abstract integer, float, or complex type may be
485 // mixed with an integer, float, or complex type.
486 if ((rhs
->is_abstract()
487 && (rhs
->integer_type() != NULL
488 || rhs
->float_type() != NULL
489 || rhs
->complex_type() != NULL
)
490 && (lhs
->integer_type() != NULL
491 || lhs
->float_type() != NULL
492 || lhs
->complex_type() != NULL
))
493 || (lhs
->is_abstract()
494 && (lhs
->integer_type() != NULL
495 || lhs
->float_type() != NULL
496 || lhs
->complex_type() != NULL
)
497 && (rhs
->integer_type() != NULL
498 || rhs
->float_type() != NULL
499 || rhs
->complex_type() != NULL
)))
502 // The nil type may be compared to a pointer, an interface type, a
503 // slice type, a channel type, a map type, or a function type.
504 if (lhs
->is_nil_type()
505 && (rhs
->points_to() != NULL
506 || rhs
->interface_type() != NULL
507 || rhs
->is_slice_type()
508 || rhs
->map_type() != NULL
509 || rhs
->channel_type() != NULL
510 || rhs
->function_type() != NULL
))
512 if (rhs
->is_nil_type()
513 && (lhs
->points_to() != NULL
514 || lhs
->interface_type() != NULL
515 || lhs
->is_slice_type()
516 || lhs
->map_type() != NULL
517 || lhs
->channel_type() != NULL
518 || lhs
->function_type() != NULL
))
524 // Return true if a value with type T1 may be compared with a value of
525 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
528 Type::are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
529 const Type
*t2
, std::string
*reason
)
532 && !Type::are_assignable(t1
, t2
, NULL
)
533 && !Type::are_assignable(t2
, t1
, NULL
))
536 *reason
= "incompatible types in binary expression";
542 if (t1
->integer_type() == NULL
543 && t1
->float_type() == NULL
544 && !t1
->is_string_type())
547 *reason
= _("invalid comparison of non-ordered type");
551 else if (t1
->is_slice_type()
552 || t1
->map_type() != NULL
553 || t1
->function_type() != NULL
554 || t2
->is_slice_type()
555 || t2
->map_type() != NULL
556 || t2
->function_type() != NULL
)
558 if (!t1
->is_nil_type() && !t2
->is_nil_type())
562 if (t1
->is_slice_type() || t2
->is_slice_type())
563 *reason
= _("slice can only be compared to nil");
564 else if (t1
->map_type() != NULL
|| t2
->map_type() != NULL
)
565 *reason
= _("map can only be compared to nil");
567 *reason
= _("func can only be compared to nil");
569 // Match 6g error messages.
570 if (t1
->interface_type() != NULL
|| t2
->interface_type() != NULL
)
573 snprintf(buf
, sizeof buf
, _("invalid operation (%s)"),
583 if (!t1
->is_boolean_type()
584 && t1
->integer_type() == NULL
585 && t1
->float_type() == NULL
586 && t1
->complex_type() == NULL
587 && !t1
->is_string_type()
588 && t1
->points_to() == NULL
589 && t1
->channel_type() == NULL
590 && t1
->interface_type() == NULL
591 && t1
->struct_type() == NULL
592 && t1
->array_type() == NULL
593 && !t1
->is_nil_type())
596 *reason
= _("invalid comparison of non-comparable type");
600 if (t1
->named_type() != NULL
)
601 return t1
->named_type()->named_type_is_comparable(reason
);
602 else if (t2
->named_type() != NULL
)
603 return t2
->named_type()->named_type_is_comparable(reason
);
604 else if (t1
->struct_type() != NULL
)
606 if (t1
->struct_type()->is_struct_incomparable())
609 *reason
= _("invalid comparison of generated struct");
612 const Struct_field_list
* fields
= t1
->struct_type()->fields();
613 for (Struct_field_list::const_iterator p
= fields
->begin();
617 if (!p
->type()->is_comparable())
620 *reason
= _("invalid comparison of non-comparable struct");
625 else if (t1
->array_type() != NULL
)
627 if (t1
->array_type()->is_array_incomparable())
630 *reason
= _("invalid comparison of generated array");
633 if (t1
->array_type()->length()->is_nil_expression()
634 || !t1
->array_type()->element_type()->is_comparable())
637 *reason
= _("invalid comparison of non-comparable array");
646 // Return true if a value with type RHS may be assigned to a variable
647 // with type LHS. If REASON is not NULL, set *REASON to the reason
648 // the types are not assignable.
651 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
653 // Do some checks first. Make sure the types are defined.
654 if (rhs
!= NULL
&& !rhs
->is_undefined())
656 if (rhs
->is_void_type())
659 *reason
= "non-value used as value";
662 if (rhs
->is_call_multiple_result_type())
665 reason
->assign(_("multiple-value function call in "
666 "single-value context"));
671 // Any value may be assigned to the blank identifier.
673 && !lhs
->is_undefined()
674 && lhs
->is_sink_type())
677 // Identical types are assignable.
678 if (Type::are_identical(lhs
, rhs
, true, reason
))
681 // The types are assignable if they have identical underlying types
682 // and either LHS or RHS is not a named type.
683 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
684 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
685 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
688 // The types are assignable if LHS is an interface type and RHS
689 // implements the required methods.
690 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
691 if (lhs_interface_type
!= NULL
)
693 if (lhs_interface_type
->implements_interface(rhs
, reason
))
695 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
696 if (rhs_interface_type
!= NULL
697 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
702 // The type are assignable if RHS is a bidirectional channel type,
703 // LHS is a channel type, they have identical element types, and
704 // either LHS or RHS is not a named type.
705 if (lhs
->channel_type() != NULL
706 && rhs
->channel_type() != NULL
707 && rhs
->channel_type()->may_send()
708 && rhs
->channel_type()->may_receive()
709 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
710 && Type::are_identical(lhs
->channel_type()->element_type(),
711 rhs
->channel_type()->element_type(),
716 // The nil type may be assigned to a pointer, function, slice, map,
717 // channel, or interface type.
718 if (rhs
->is_nil_type()
719 && (lhs
->points_to() != NULL
720 || lhs
->function_type() != NULL
721 || lhs
->is_slice_type()
722 || lhs
->map_type() != NULL
723 || lhs
->channel_type() != NULL
724 || lhs
->interface_type() != NULL
))
727 // An untyped numeric constant may be assigned to a numeric type if
728 // it is representable in that type.
729 if ((rhs
->is_abstract()
730 && (rhs
->integer_type() != NULL
731 || rhs
->float_type() != NULL
732 || rhs
->complex_type() != NULL
))
733 && (lhs
->integer_type() != NULL
734 || lhs
->float_type() != NULL
735 || lhs
->complex_type() != NULL
))
738 // Give some better error messages.
739 if (reason
!= NULL
&& reason
->empty())
741 if (rhs
->interface_type() != NULL
)
742 reason
->assign(_("need explicit conversion"));
743 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
745 size_t len
= (lhs
->named_type()->name().length()
746 + rhs
->named_type()->name().length()
748 char* buf
= new char[len
];
749 snprintf(buf
, len
, _("cannot use type %s as type %s"),
750 rhs
->named_type()->message_name().c_str(),
751 lhs
->named_type()->message_name().c_str());
760 // Return true if a value with type RHS may be converted to type LHS.
761 // If REASON is not NULL, set *REASON to the reason the types are not
765 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
767 // The types are convertible if they are assignable.
768 if (Type::are_assignable(lhs
, rhs
, reason
))
771 // A pointer to a regular type may not be converted to a pointer to
772 // a type that may not live in the heap, except when converting from
774 if (lhs
->points_to() != NULL
775 && rhs
->points_to() != NULL
776 && !lhs
->points_to()->in_heap()
777 && rhs
->points_to()->in_heap()
778 && !rhs
->is_unsafe_pointer_type())
781 reason
->assign(_("conversion from normal type to notinheap type"));
785 // The types are convertible if they have identical underlying
786 // types, ignoring struct field tags.
787 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
788 && Type::are_identical_cmp_tags(lhs
->base(), rhs
->base(), IGNORE_TAGS
,
792 // The types are convertible if they are both unnamed pointer types
793 // and their pointer base types have identical underlying types,
794 // ignoring struct field tags.
795 if (lhs
->named_type() == NULL
796 && rhs
->named_type() == NULL
797 && lhs
->points_to() != NULL
798 && rhs
->points_to() != NULL
799 && (lhs
->points_to()->named_type() != NULL
800 || rhs
->points_to()->named_type() != NULL
)
801 && Type::are_identical_cmp_tags(lhs
->points_to()->base(),
802 rhs
->points_to()->base(),
808 // Integer and floating point types are convertible to each other.
809 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
810 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
813 // Complex types are convertible to each other.
814 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
817 // An integer, or []byte, or []rune, may be converted to a string.
818 if (lhs
->is_string_type())
820 if (rhs
->integer_type() != NULL
)
822 if (rhs
->is_slice_type())
824 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
825 if (e
->integer_type() != NULL
826 && (e
->integer_type()->is_byte()
827 || e
->integer_type()->is_rune()))
832 // A string may be converted to []byte or []rune.
833 if (rhs
->is_string_type() && lhs
->is_slice_type())
835 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
836 if (e
->integer_type() != NULL
837 && (e
->integer_type()->is_byte() || e
->integer_type()->is_rune()))
841 // An unsafe.Pointer type may be converted to any pointer type or to
842 // a type whose underlying type is uintptr, and vice-versa.
843 if (lhs
->is_unsafe_pointer_type()
844 && (rhs
->points_to() != NULL
845 || (rhs
->integer_type() != NULL
846 && rhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
848 if (rhs
->is_unsafe_pointer_type()
849 && (lhs
->points_to() != NULL
850 || (lhs
->integer_type() != NULL
851 && lhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
854 // Give a better error message.
858 *reason
= "invalid type conversion";
861 std::string s
= "invalid type conversion (";
871 // Return a hash code for the type to be used for method lookup.
874 Type::hash_for_method(Gogo
* gogo
) const
876 if (this->named_type() != NULL
&& this->named_type()->is_alias())
877 return this->named_type()->real_type()->hash_for_method(gogo
);
878 unsigned int ret
= 0;
879 if (this->classification_
!= TYPE_FORWARD
)
880 ret
+= this->classification_
;
881 return ret
+ this->do_hash_for_method(gogo
);
884 // Default implementation of do_hash_for_method. This is appropriate
885 // for types with no subfields.
888 Type::do_hash_for_method(Gogo
*) const
893 // Return a hash code for a string, given a starting hash.
896 Type::hash_string(const std::string
& s
, unsigned int h
)
898 const char* p
= s
.data();
899 size_t len
= s
.length();
900 for (; len
> 0; --len
)
908 // A hash table mapping unnamed types to the backend representation of
911 Type::Type_btypes
Type::type_btypes
;
913 // Return the backend representation for this type.
916 Type::get_backend(Gogo
* gogo
)
918 if (this->btype_
!= NULL
)
921 if (this->forward_declaration_type() != NULL
922 || this->named_type() != NULL
)
923 return this->get_btype_without_hash(gogo
);
925 if (this->is_error_type())
926 return gogo
->backend()->error_type();
928 // To avoid confusing the backend, translate all identical Go types
929 // to the same backend representation. We use a hash table to do
930 // that. There is no need to use the hash table for named types, as
931 // named types are only identical to themselves.
933 std::pair
<Type
*, Type_btype_entry
> val
;
935 val
.second
.btype
= NULL
;
936 val
.second
.is_placeholder
= false;
937 std::pair
<Type_btypes::iterator
, bool> ins
=
938 Type::type_btypes
.insert(val
);
939 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
941 // Note that GOGO can be NULL here, but only when the GCC
942 // middle-end is asking for a frontend type. That will only
943 // happen for simple types, which should never require
945 if (!ins
.first
->second
.is_placeholder
)
946 this->btype_
= ins
.first
->second
.btype
;
947 else if (gogo
->named_types_are_converted())
949 this->finish_backend(gogo
, ins
.first
->second
.btype
);
950 ins
.first
->second
.is_placeholder
= false;
953 return ins
.first
->second
.btype
;
956 Btype
* bt
= this->get_btype_without_hash(gogo
);
958 if (ins
.first
->second
.btype
== NULL
)
960 ins
.first
->second
.btype
= bt
;
961 ins
.first
->second
.is_placeholder
= false;
965 // We have already created a backend representation for this
966 // type. This can happen when an unnamed type is defined using
967 // a named type which in turns uses an identical unnamed type.
968 // Use the representation we created earlier and ignore the one we just
970 if (this->btype_
== bt
)
971 this->btype_
= ins
.first
->second
.btype
;
972 bt
= ins
.first
->second
.btype
;
978 // Return the backend representation for a type without looking in the
979 // hash table for identical types. This is used for named types,
980 // since a named type is never identical to any other type.
983 Type::get_btype_without_hash(Gogo
* gogo
)
985 if (this->btype_
== NULL
)
987 Btype
* bt
= this->do_get_backend(gogo
);
989 // For a recursive function or pointer type, we will temporarily
990 // return a circular pointer type during the recursion. We
991 // don't want to record that for a forwarding type, as it may
993 if (this->forward_declaration_type() != NULL
994 && gogo
->backend()->is_circular_pointer_type(bt
))
997 if (gogo
== NULL
|| !gogo
->named_types_are_converted())
1002 return this->btype_
;
1005 // Get the backend representation of a type without forcing the
1006 // creation of the backend representation of all supporting types.
1007 // This will return a backend type that has the correct size but may
1008 // be incomplete. E.g., a pointer will just be a placeholder pointer,
1009 // and will not contain the final representation of the type to which
1010 // it points. This is used while converting all named types to the
1011 // backend representation, to avoid problems with indirect references
1012 // to types which are not yet complete. When this is called, the
1013 // sizes of all direct references (e.g., a struct field) should be
1014 // known, but the sizes of indirect references (e.g., the type to
1015 // which a pointer points) may not.
1018 Type::get_backend_placeholder(Gogo
* gogo
)
1020 if (gogo
->named_types_are_converted())
1021 return this->get_backend(gogo
);
1022 if (this->btype_
!= NULL
)
1023 return this->btype_
;
1026 switch (this->classification_
)
1036 // These are simple types that can just be created directly.
1037 return this->get_backend(gogo
);
1041 // All maps and channels have the same backend representation.
1042 return this->get_backend(gogo
);
1046 // Named types keep track of their own dependencies and manage
1047 // their own placeholders.
1048 return this->get_backend(gogo
);
1050 case TYPE_INTERFACE
:
1051 if (this->interface_type()->is_empty())
1052 return Interface_type::get_backend_empty_interface_type(gogo
);
1059 std::pair
<Type
*, Type_btype_entry
> val
;
1061 val
.second
.btype
= NULL
;
1062 val
.second
.is_placeholder
= false;
1063 std::pair
<Type_btypes::iterator
, bool> ins
=
1064 Type::type_btypes
.insert(val
);
1065 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
1066 return ins
.first
->second
.btype
;
1068 switch (this->classification_
)
1072 // A Go function type is a pointer to a struct type.
1073 Location loc
= this->function_type()->location();
1074 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1080 Location loc
= Linemap::unknown_location();
1081 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1082 Pointer_type
* pt
= this->convert
<Pointer_type
, TYPE_POINTER
>();
1083 Type::placeholder_pointers
.push_back(pt
);
1088 // We don't have to make the struct itself be a placeholder. We
1089 // are promised that we know the sizes of the struct fields.
1090 // But we may have to use a placeholder for any particular
1093 std::vector
<Backend::Btyped_identifier
> bfields
;
1094 get_backend_struct_fields(gogo
, this->struct_type()->fields(),
1096 bt
= gogo
->backend()->struct_type(bfields
);
1101 if (this->is_slice_type())
1103 std::vector
<Backend::Btyped_identifier
> bfields
;
1104 get_backend_slice_fields(gogo
, this->array_type(), true, &bfields
);
1105 bt
= gogo
->backend()->struct_type(bfields
);
1109 Btype
* element
= this->array_type()->get_backend_element(gogo
, true);
1110 Bexpression
* len
= this->array_type()->get_backend_length(gogo
);
1111 bt
= gogo
->backend()->array_type(element
, len
);
1115 case TYPE_INTERFACE
:
1117 go_assert(!this->interface_type()->is_empty());
1118 std::vector
<Backend::Btyped_identifier
> bfields
;
1119 get_backend_interface_fields(gogo
, this->interface_type(), true,
1121 bt
= gogo
->backend()->struct_type(bfields
);
1126 case TYPE_CALL_MULTIPLE_RESULT
:
1127 /* Note that various classifications were handled in the earlier
1133 if (ins
.first
->second
.btype
== NULL
)
1135 ins
.first
->second
.btype
= bt
;
1136 ins
.first
->second
.is_placeholder
= true;
1140 // A placeholder for this type got created along the way. Use
1141 // that one and ignore the one we just built.
1142 bt
= ins
.first
->second
.btype
;
1148 // Complete the backend representation. This is called for a type
1149 // using a placeholder type.
1152 Type::finish_backend(Gogo
* gogo
, Btype
*placeholder
)
1154 switch (this->classification_
)
1168 Btype
* bt
= this->do_get_backend(gogo
);
1169 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1170 go_assert(saw_errors());
1176 Btype
* bt
= this->do_get_backend(gogo
);
1177 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1178 go_assert(saw_errors());
1183 // The struct type itself is done, but we have to make sure that
1184 // all the field types are converted.
1185 this->struct_type()->finish_backend_fields(gogo
);
1189 // The array type itself is done, but make sure the element type
1191 this->array_type()->finish_backend_element(gogo
);
1198 case TYPE_INTERFACE
:
1199 // The interface type itself is done, but make sure the method
1200 // types are converted.
1201 this->interface_type()->finish_backend_methods(gogo
);
1209 case TYPE_CALL_MULTIPLE_RESULT
:
1214 this->btype_
= placeholder
;
1217 // Return a pointer to the type descriptor for this type.
1220 Type::type_descriptor_pointer(Gogo
* gogo
, Location location
)
1222 Type
* t
= this->unalias();
1223 if (t
->type_descriptor_var_
== NULL
)
1225 t
->make_type_descriptor_var(gogo
);
1226 go_assert(t
->type_descriptor_var_
!= NULL
);
1228 Bexpression
* var_expr
=
1229 gogo
->backend()->var_expression(t
->type_descriptor_var_
, location
);
1230 Bexpression
* var_addr
=
1231 gogo
->backend()->address_expression(var_expr
, location
);
1232 Type
* td_type
= Type::make_type_descriptor_type();
1233 Btype
* td_btype
= td_type
->get_backend(gogo
);
1234 Btype
* ptd_btype
= gogo
->backend()->pointer_type(td_btype
);
1235 return gogo
->backend()->convert_expression(ptd_btype
, var_addr
, location
);
1238 // A mapping from unnamed types to type descriptor variables.
1240 Type::Type_descriptor_vars
Type::type_descriptor_vars
;
1242 // Build the type descriptor for this type.
1245 Type::make_type_descriptor_var(Gogo
* gogo
)
1247 go_assert(this->type_descriptor_var_
== NULL
);
1249 Named_type
* nt
= this->named_type();
1251 // We can have multiple instances of unnamed types, but we only want
1252 // to emit the type descriptor once. We use a hash table. This is
1253 // not necessary for named types, as they are unique, and we store
1254 // the type descriptor in the type itself.
1255 Bvariable
** phash
= NULL
;
1258 Bvariable
* bvnull
= NULL
;
1259 std::pair
<Type_descriptor_vars::iterator
, bool> ins
=
1260 Type::type_descriptor_vars
.insert(std::make_pair(this, bvnull
));
1263 // We've already built a type descriptor for this type.
1264 this->type_descriptor_var_
= ins
.first
->second
;
1267 phash
= &ins
.first
->second
;
1270 // The type descriptor symbol for the unsafe.Pointer type is defined in
1271 // libgo/go-unsafe-pointer.c, so we just return a reference to that
1272 // symbol if necessary.
1273 if (this->is_unsafe_pointer_type())
1275 Location bloc
= Linemap::predeclared_location();
1277 Type
* td_type
= Type::make_type_descriptor_type();
1278 Btype
* td_btype
= td_type
->get_backend(gogo
);
1279 std::string name
= gogo
->type_descriptor_name(this, nt
);
1280 std::string
asm_name(go_selectively_encode_id(name
));
1281 this->type_descriptor_var_
=
1282 gogo
->backend()->immutable_struct_reference(name
, asm_name
,
1287 *phash
= this->type_descriptor_var_
;
1291 std::string var_name
= gogo
->type_descriptor_name(this, nt
);
1293 // Build the contents of the type descriptor.
1294 Expression
* initializer
= this->do_type_descriptor(gogo
, NULL
);
1296 Btype
* initializer_btype
= initializer
->type()->get_backend(gogo
);
1298 Location loc
= nt
== NULL
? Linemap::predeclared_location() : nt
->location();
1300 const Package
* dummy
;
1301 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
1303 std::string
asm_name(go_selectively_encode_id(var_name
));
1304 this->type_descriptor_var_
=
1305 gogo
->backend()->immutable_struct_reference(var_name
, asm_name
,
1309 *phash
= this->type_descriptor_var_
;
1313 // See if this type descriptor can appear in multiple packages.
1314 bool is_common
= false;
1317 // We create the descriptor for a builtin type whenever we need
1319 is_common
= nt
->is_builtin();
1323 // This is an unnamed type. The descriptor could be defined in
1324 // any package where it is needed, and the linker will pick one
1325 // descriptor to keep.
1329 // We are going to build the type descriptor in this package. We
1330 // must create the variable before we convert the initializer to the
1331 // backend representation, because the initializer may refer to the
1332 // type descriptor of this type. By setting type_descriptor_var_ we
1333 // ensure that type_descriptor_pointer will work if called while
1334 // converting INITIALIZER.
1336 std::string
asm_name(go_selectively_encode_id(var_name
));
1337 this->type_descriptor_var_
=
1338 gogo
->backend()->immutable_struct(var_name
, asm_name
, false, is_common
,
1339 initializer_btype
, loc
);
1341 *phash
= this->type_descriptor_var_
;
1343 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
1344 context
.set_is_const();
1345 Bexpression
* binitializer
= initializer
->get_backend(&context
);
1347 gogo
->backend()->immutable_struct_set_init(this->type_descriptor_var_
,
1348 var_name
, false, is_common
,
1349 initializer_btype
, loc
,
1353 // Return true if this type descriptor is defined in a different
1354 // package. If this returns true it sets *PACKAGE to the package.
1357 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1358 const Package
** package
)
1362 if (nt
->named_object()->package() != NULL
)
1364 // This is a named type defined in a different package. The
1365 // type descriptor should be defined in that package.
1366 *package
= nt
->named_object()->package();
1372 if (this->points_to() != NULL
1373 && this->points_to()->named_type() != NULL
1374 && this->points_to()->named_type()->named_object()->package() != NULL
)
1376 // This is an unnamed pointer to a named type defined in a
1377 // different package. The descriptor should be defined in
1379 *package
= this->points_to()->named_type()->named_object()->package();
1386 // Return a composite literal for a type descriptor.
1389 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1391 return type
->do_type_descriptor(gogo
, NULL
);
1394 // Return a composite literal for a type descriptor with a name.
1397 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1399 go_assert(name
!= NULL
&& type
->named_type() != name
);
1400 return type
->do_type_descriptor(gogo
, name
);
1403 // Make a builtin struct type from a list of fields. The fields are
1404 // pairs of a name and a type.
1407 Type::make_builtin_struct_type(int nfields
, ...)
1410 va_start(ap
, nfields
);
1412 Location bloc
= Linemap::predeclared_location();
1413 Struct_field_list
* sfl
= new Struct_field_list();
1414 for (int i
= 0; i
< nfields
; i
++)
1416 const char* field_name
= va_arg(ap
, const char *);
1417 Type
* type
= va_arg(ap
, Type
*);
1418 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1423 Struct_type
* ret
= Type::make_struct_type(sfl
, bloc
);
1424 ret
->set_is_struct_incomparable();
1428 // A list of builtin named types.
1430 std::vector
<Named_type
*> Type::named_builtin_types
;
1432 // Make a builtin named type.
1435 Type::make_builtin_named_type(const char* name
, Type
* type
)
1437 Location bloc
= Linemap::predeclared_location();
1438 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1439 Named_type
* ret
= no
->type_value();
1440 Type::named_builtin_types
.push_back(ret
);
1444 // Convert the named builtin types.
1447 Type::convert_builtin_named_types(Gogo
* gogo
)
1449 for (std::vector
<Named_type
*>::const_iterator p
=
1450 Type::named_builtin_types
.begin();
1451 p
!= Type::named_builtin_types
.end();
1454 bool r
= (*p
)->verify();
1456 (*p
)->convert(gogo
);
1460 // Return the type of a type descriptor. We should really tie this to
1461 // runtime.Type rather than copying it. This must match the struct "_type"
1462 // declared in libgo/go/runtime/type.go.
1465 Type::make_type_descriptor_type()
1470 Location bloc
= Linemap::predeclared_location();
1472 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1473 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
1474 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1475 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1476 Type
* string_type
= Type::lookup_string_type();
1477 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1479 // This is an unnamed version of unsafe.Pointer. Perhaps we
1480 // should use the named version instead, although that would
1481 // require us to create the unsafe package if it has not been
1482 // imported. It probably doesn't matter.
1483 Type
* void_type
= Type::make_void_type();
1484 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1486 Typed_identifier_list
*params
= new Typed_identifier_list();
1487 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
1488 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1490 Typed_identifier_list
* results
= new Typed_identifier_list();
1491 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1493 Type
* hash_fntype
= Type::make_function_type(NULL
, params
, results
,
1496 params
= new Typed_identifier_list();
1497 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
1498 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
1500 results
= new Typed_identifier_list();
1501 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1503 Type
* equal_fntype
= Type::make_function_type(NULL
, params
, results
,
1506 // Forward declaration for the type descriptor type.
1507 Named_object
* named_type_descriptor_type
=
1508 Named_object::make_type_declaration("_type", NULL
, bloc
);
1509 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1510 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1512 // The type of a method on a concrete type.
1513 Struct_type
* method_type
=
1514 Type::make_builtin_struct_type(5,
1515 "name", pointer_string_type
,
1516 "pkgPath", pointer_string_type
,
1517 "mtyp", pointer_type_descriptor_type
,
1518 "typ", pointer_type_descriptor_type
,
1519 "tfn", unsafe_pointer_type
);
1520 Named_type
* named_method_type
=
1521 Type::make_builtin_named_type("method", method_type
);
1523 // Information for types with a name or methods.
1524 Type
* slice_named_method_type
=
1525 Type::make_array_type(named_method_type
, NULL
);
1526 Struct_type
* uncommon_type
=
1527 Type::make_builtin_struct_type(3,
1528 "name", pointer_string_type
,
1529 "pkgPath", pointer_string_type
,
1530 "methods", slice_named_method_type
);
1531 Named_type
* named_uncommon_type
=
1532 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1534 Type
* pointer_uncommon_type
=
1535 Type::make_pointer_type(named_uncommon_type
);
1537 // The type descriptor type.
1539 Struct_type
* type_descriptor_type
=
1540 Type::make_builtin_struct_type(12,
1541 "size", uintptr_type
,
1542 "ptrdata", uintptr_type
,
1543 "hash", uint32_type
,
1545 "align", uint8_type
,
1546 "fieldAlign", uint8_type
,
1547 "hashfn", hash_fntype
,
1548 "equalfn", equal_fntype
,
1549 "gcdata", pointer_uint8_type
,
1550 "string", pointer_string_type
,
1551 "", pointer_uncommon_type
,
1553 pointer_type_descriptor_type
);
1555 Named_type
* named
= Type::make_builtin_named_type("_type",
1556 type_descriptor_type
);
1558 named_type_descriptor_type
->set_type_value(named
);
1566 // Make the type of a pointer to a type descriptor as represented in
1570 Type::make_type_descriptor_ptr_type()
1574 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1578 // Return the alignment required by the memequalN function. N is a
1579 // type size: 16, 32, 64, or 128. The memequalN functions are defined
1580 // in libgo/go/runtime/alg.go.
1583 Type::memequal_align(Gogo
* gogo
, int size
)
1598 // The code uses [2]int64, which must have the same alignment as
1606 Type
* t
= Type::lookup_integer_type(tn
);
1609 if (!t
->backend_type_align(gogo
, &ret
))
1614 // Return whether this type needs specially built type functions.
1615 // This returns true for types that are comparable and either can not
1616 // use an identity comparison, or are a non-standard size.
1619 Type::needs_specific_type_functions(Gogo
* gogo
)
1621 Named_type
* nt
= this->named_type();
1622 if (nt
!= NULL
&& nt
->is_alias())
1624 if (!this->is_comparable())
1626 if (!this->compare_is_identity(gogo
))
1629 // We create a few predeclared types for type descriptors; they are
1630 // really just for the backend and don't need hash or equality
1632 if (nt
!= NULL
&& Linemap::is_predeclared_location(nt
->location()))
1635 int64_t size
, align
;
1636 if (!this->backend_type_size(gogo
, &size
)
1637 || !this->backend_type_align(gogo
, &align
))
1639 go_assert(saw_errors());
1642 // This switch matches the one in Type::type_functions.
1648 return align
< Type::memequal_align(gogo
, 16);
1650 return align
< Type::memequal_align(gogo
, 32);
1652 return align
< Type::memequal_align(gogo
, 64);
1654 return align
< Type::memequal_align(gogo
, 128);
1660 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1661 // hash code for this type and which compare whether two values of
1662 // this type are equal. If NAME is not NULL it is the name of this
1663 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1664 // functions, for convenience; they may be NULL.
1667 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1668 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1669 Named_object
** equal_fn
)
1671 // If the unaliased type is not a named type, then the type does not
1672 // have a name after all.
1674 name
= name
->unalias()->named_type();
1676 if (!this->is_comparable())
1683 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1685 Location bloc
= Linemap::predeclared_location();
1687 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1688 Type
* void_type
= Type::make_void_type();
1689 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1691 if (hash_fntype
== NULL
)
1693 Typed_identifier_list
* params
= new Typed_identifier_list();
1694 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1696 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
1698 Typed_identifier_list
* results
= new Typed_identifier_list();
1699 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1701 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1703 if (equal_fntype
== NULL
)
1705 Typed_identifier_list
* params
= new Typed_identifier_list();
1706 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1708 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1711 Typed_identifier_list
* results
= new Typed_identifier_list();
1712 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1715 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1719 const char* hash_fnname
;
1720 const char* equal_fnname
;
1721 if (this->compare_is_identity(gogo
))
1723 int64_t size
, align
;
1724 if (!this->backend_type_size(gogo
, &size
)
1725 || !this->backend_type_align(gogo
, &align
))
1727 go_assert(saw_errors());
1730 bool build_functions
= false;
1731 // This switch matches the one in Type::needs_specific_type_functions.
1732 // The alignment tests are because of the memequal functions,
1733 // which assume that the values are aligned as required for an
1734 // integer of that size.
1738 hash_fnname
= "runtime.memhash0";
1739 equal_fnname
= "runtime.memequal0";
1742 hash_fnname
= "runtime.memhash8";
1743 equal_fnname
= "runtime.memequal8";
1746 if (align
< Type::memequal_align(gogo
, 16))
1747 build_functions
= true;
1750 hash_fnname
= "runtime.memhash16";
1751 equal_fnname
= "runtime.memequal16";
1755 if (align
< Type::memequal_align(gogo
, 32))
1756 build_functions
= true;
1759 hash_fnname
= "runtime.memhash32";
1760 equal_fnname
= "runtime.memequal32";
1764 if (align
< Type::memequal_align(gogo
, 64))
1765 build_functions
= true;
1768 hash_fnname
= "runtime.memhash64";
1769 equal_fnname
= "runtime.memequal64";
1773 if (align
< Type::memequal_align(gogo
, 128))
1774 build_functions
= true;
1777 hash_fnname
= "runtime.memhash128";
1778 equal_fnname
= "runtime.memequal128";
1782 build_functions
= true;
1785 if (build_functions
)
1787 // We don't have a built-in function for a type of this size
1788 // and alignment. Build a function to use that calls the
1789 // generic hash/equality functions for identity, passing the size.
1790 this->specific_type_functions(gogo
, name
, size
, hash_fntype
,
1791 equal_fntype
, hash_fn
, equal_fn
);
1797 switch (this->base()->classification())
1799 case Type::TYPE_ERROR
:
1800 case Type::TYPE_VOID
:
1801 case Type::TYPE_NIL
:
1802 case Type::TYPE_FUNCTION
:
1803 case Type::TYPE_MAP
:
1804 // For these types is_comparable should have returned false.
1807 case Type::TYPE_BOOLEAN
:
1808 case Type::TYPE_INTEGER
:
1809 case Type::TYPE_POINTER
:
1810 case Type::TYPE_CHANNEL
:
1811 // For these types compare_is_identity should have returned true.
1814 case Type::TYPE_FLOAT
:
1815 switch (this->float_type()->bits())
1818 hash_fnname
= "runtime.f32hash";
1819 equal_fnname
= "runtime.f32equal";
1822 hash_fnname
= "runtime.f64hash";
1823 equal_fnname
= "runtime.f64equal";
1830 case Type::TYPE_COMPLEX
:
1831 switch (this->complex_type()->bits())
1834 hash_fnname
= "runtime.c64hash";
1835 equal_fnname
= "runtime.c64equal";
1838 hash_fnname
= "runtime.c128hash";
1839 equal_fnname
= "runtime.c128equal";
1846 case Type::TYPE_STRING
:
1847 hash_fnname
= "runtime.strhash";
1848 equal_fnname
= "runtime.strequal";
1851 case Type::TYPE_STRUCT
:
1853 // This is a struct which can not be compared using a
1854 // simple identity function. We need to build a function
1856 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1857 equal_fntype
, hash_fn
, equal_fn
);
1861 case Type::TYPE_ARRAY
:
1862 if (this->is_slice_type())
1864 // Type::is_compatible_for_comparison should have
1870 // This is an array which can not be compared using a
1871 // simple identity function. We need to build a
1872 // function for comparison.
1873 this->specific_type_functions(gogo
, name
, -1, hash_fntype
,
1874 equal_fntype
, hash_fn
, equal_fn
);
1879 case Type::TYPE_INTERFACE
:
1880 if (this->interface_type()->is_empty())
1882 hash_fnname
= "runtime.nilinterhash";
1883 equal_fnname
= "runtime.nilinterequal";
1887 hash_fnname
= "runtime.interhash";
1888 equal_fnname
= "runtime.interequal";
1892 case Type::TYPE_NAMED
:
1893 case Type::TYPE_FORWARD
:
1902 Location bloc
= Linemap::predeclared_location();
1903 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1905 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1906 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1907 equal_fntype
, bloc
);
1908 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1911 // A hash table mapping types to the specific hash functions.
1913 Type::Type_functions
Type::type_functions_table
;
1915 // Handle a type function which is specific to a type: if SIZE == -1,
1916 // this is a struct or array that can not use an identity comparison.
1917 // Otherwise, it is a type that uses an identity comparison but is not
1918 // one of the standard supported sizes.
1921 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1922 Function_type
* hash_fntype
,
1923 Function_type
* equal_fntype
,
1924 Named_object
** hash_fn
,
1925 Named_object
** equal_fn
)
1927 Hash_equal_fn
fnull(NULL
, NULL
);
1928 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1929 std::pair
<Type_functions::iterator
, bool> ins
=
1930 Type::type_functions_table
.insert(val
);
1933 // We already have functions for this type
1934 *hash_fn
= ins
.first
->second
.first
;
1935 *equal_fn
= ins
.first
->second
.second
;
1939 std::string hash_name
;
1940 std::string equal_name
;
1941 gogo
->specific_type_function_names(this, name
, &hash_name
, &equal_name
);
1943 Location bloc
= Linemap::predeclared_location();
1945 const Package
* package
= NULL
;
1946 bool is_defined_elsewhere
=
1947 this->type_descriptor_defined_elsewhere(name
, &package
);
1948 if (is_defined_elsewhere
)
1950 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
1952 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
1953 equal_fntype
, bloc
);
1957 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
1958 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
1962 ins
.first
->second
.first
= *hash_fn
;
1963 ins
.first
->second
.second
= *equal_fn
;
1965 if (!is_defined_elsewhere
)
1967 if (gogo
->in_global_scope())
1968 this->write_specific_type_functions(gogo
, name
, size
, hash_name
,
1969 hash_fntype
, equal_name
,
1972 gogo
->queue_specific_type_function(this, name
, size
, hash_name
,
1973 hash_fntype
, equal_name
,
1978 // Write the hash and equality functions for a type which needs to be
1979 // written specially.
1982 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
, int64_t size
,
1983 const std::string
& hash_name
,
1984 Function_type
* hash_fntype
,
1985 const std::string
& equal_name
,
1986 Function_type
* equal_fntype
)
1988 Location bloc
= Linemap::predeclared_location();
1990 if (gogo
->specific_type_functions_are_written())
1992 go_assert(saw_errors());
1996 go_assert(this->is_comparable());
1998 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
2000 hash_fn
->func_value()->set_is_type_specific_function();
2001 gogo
->start_block(bloc
);
2004 this->write_identity_hash(gogo
, size
);
2005 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2006 this->write_named_hash(gogo
, name
, hash_fntype
, equal_fntype
);
2007 else if (this->struct_type() != NULL
)
2008 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
2010 else if (this->array_type() != NULL
)
2011 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
2016 Block
* b
= gogo
->finish_block(bloc
);
2017 gogo
->add_block(b
, bloc
);
2018 gogo
->lower_block(hash_fn
, b
);
2019 gogo
->finish_function(bloc
);
2021 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
2023 equal_fn
->func_value()->set_is_type_specific_function();
2024 gogo
->start_block(bloc
);
2027 this->write_identity_equal(gogo
, size
);
2028 else if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
2029 this->write_named_equal(gogo
, name
);
2030 else if (this->struct_type() != NULL
)
2031 this->struct_type()->write_equal_function(gogo
, name
);
2032 else if (this->array_type() != NULL
)
2033 this->array_type()->write_equal_function(gogo
, name
);
2037 b
= gogo
->finish_block(bloc
);
2038 gogo
->add_block(b
, bloc
);
2039 gogo
->lower_block(equal_fn
, b
);
2040 gogo
->finish_function(bloc
);
2042 // Build the function descriptors for the type descriptor to refer to.
2043 hash_fn
->func_value()->descriptor(gogo
, hash_fn
);
2044 equal_fn
->func_value()->descriptor(gogo
, equal_fn
);
2047 // Write a hash function for a type that can use an identity hash but
2048 // is not one of the standard supported sizes. For example, this
2049 // would be used for the type [3]byte. This builds a return statement
2050 // that returns a call to the memhash function, passing the key and
2051 // seed from the function arguments (already constructed before this
2052 // is called), and the constant size.
2055 Type::write_identity_hash(Gogo
* gogo
, int64_t size
)
2057 Location bloc
= Linemap::predeclared_location();
2059 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2060 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2062 Typed_identifier_list
* params
= new Typed_identifier_list();
2063 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
2064 params
->push_back(Typed_identifier("seed", uintptr_type
, bloc
));
2065 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2067 Typed_identifier_list
* results
= new Typed_identifier_list();
2068 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
2070 Function_type
* memhash_fntype
= Type::make_function_type(NULL
, params
,
2073 Named_object
* memhash
=
2074 Named_object::make_function_declaration("runtime.memhash", NULL
,
2075 memhash_fntype
, bloc
);
2076 memhash
->func_declaration_value()->set_asm_name("runtime.memhash");
2078 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2079 go_assert(key_arg
!= NULL
);
2080 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2081 go_assert(seed_arg
!= NULL
);
2083 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2084 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2085 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2087 Expression_list
* args
= new Expression_list();
2088 args
->push_back(key_ref
);
2089 args
->push_back(seed_ref
);
2090 args
->push_back(size_arg
);
2091 Expression
* func
= Expression::make_func_reference(memhash
, NULL
, bloc
);
2092 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2094 Expression_list
* vals
= new Expression_list();
2095 vals
->push_back(call
);
2096 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2097 gogo
->add_statement(s
);
2100 // Write an equality function for a type that can use an identity
2101 // equality comparison but is not one of the standard supported sizes.
2102 // For example, this would be used for the type [3]byte. This builds
2103 // a return statement that returns a call to the memequal function,
2104 // passing the two keys from the function arguments (already
2105 // constructed before this is called), and the constant size.
2108 Type::write_identity_equal(Gogo
* gogo
, int64_t size
)
2110 Location bloc
= Linemap::predeclared_location();
2112 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
2113 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
2115 Typed_identifier_list
* params
= new Typed_identifier_list();
2116 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
2117 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
2118 params
->push_back(Typed_identifier("size", uintptr_type
, bloc
));
2120 Typed_identifier_list
* results
= new Typed_identifier_list();
2121 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
2123 Function_type
* memequal_fntype
= Type::make_function_type(NULL
, params
,
2126 Named_object
* memequal
=
2127 Named_object::make_function_declaration("runtime.memequal", NULL
,
2128 memequal_fntype
, bloc
);
2129 memequal
->func_declaration_value()->set_asm_name("runtime.memequal");
2131 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2132 go_assert(key1_arg
!= NULL
);
2133 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2134 go_assert(key2_arg
!= NULL
);
2136 Expression
* key1_ref
= Expression::make_var_reference(key1_arg
, bloc
);
2137 Expression
* key2_ref
= Expression::make_var_reference(key2_arg
, bloc
);
2138 Expression
* size_arg
= Expression::make_integer_int64(size
, uintptr_type
,
2140 Expression_list
* args
= new Expression_list();
2141 args
->push_back(key1_ref
);
2142 args
->push_back(key2_ref
);
2143 args
->push_back(size_arg
);
2144 Expression
* func
= Expression::make_func_reference(memequal
, NULL
, bloc
);
2145 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2147 Expression_list
* vals
= new Expression_list();
2148 vals
->push_back(call
);
2149 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2150 gogo
->add_statement(s
);
2153 // Write a hash function that simply calls the hash function for a
2154 // named type. This is used when one named type is defined as
2155 // another. This ensures that this case works when the other named
2156 // type is defined in another package and relies on calling hash
2157 // functions defined only in that package.
2160 Type::write_named_hash(Gogo
* gogo
, Named_type
* name
,
2161 Function_type
* hash_fntype
, Function_type
* equal_fntype
)
2163 Location bloc
= Linemap::predeclared_location();
2165 Named_type
* base_type
= name
->real_type()->named_type();
2166 while (base_type
->is_alias())
2168 base_type
= base_type
->real_type()->named_type();
2169 go_assert(base_type
!= NULL
);
2171 go_assert(base_type
!= NULL
);
2173 // The pointer to the type we are going to hash. This is an
2175 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
2176 go_assert(key_arg
!= NULL
);
2178 // The seed argument to the hash function.
2179 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
2180 go_assert(seed_arg
!= NULL
);
2182 Named_object
* hash_fn
;
2183 Named_object
* equal_fn
;
2184 name
->real_type()->type_functions(gogo
, base_type
, hash_fntype
, equal_fntype
,
2185 &hash_fn
, &equal_fn
);
2187 // Call the hash function for the base type.
2188 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
2189 Expression
* seed_ref
= Expression::make_var_reference(seed_arg
, bloc
);
2190 Expression_list
* args
= new Expression_list();
2191 args
->push_back(key_ref
);
2192 args
->push_back(seed_ref
);
2193 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
2194 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
2196 // Return the hash of the base type.
2197 Expression_list
* vals
= new Expression_list();
2198 vals
->push_back(call
);
2199 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2200 gogo
->add_statement(s
);
2203 // Write an equality function that simply calls the equality function
2204 // for a named type. This is used when one named type is defined as
2205 // another. This ensures that this case works when the other named
2206 // type is defined in another package and relies on calling equality
2207 // functions defined only in that package.
2210 Type::write_named_equal(Gogo
* gogo
, Named_type
* name
)
2212 Location bloc
= Linemap::predeclared_location();
2214 // The pointers to the types we are going to compare. These have
2215 // type unsafe.Pointer.
2216 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
2217 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
2218 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
2220 Named_type
* base_type
= name
->real_type()->named_type();
2221 go_assert(base_type
!= NULL
);
2223 // Build temporaries with the base type.
2224 Type
* pt
= Type::make_pointer_type(base_type
);
2226 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
2227 ref
= Expression::make_cast(pt
, ref
, bloc
);
2228 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
2229 gogo
->add_statement(p1
);
2231 ref
= Expression::make_var_reference(key2_arg
, bloc
);
2232 ref
= Expression::make_cast(pt
, ref
, bloc
);
2233 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
2234 gogo
->add_statement(p2
);
2236 // Compare the values for equality.
2237 Expression
* t1
= Expression::make_temporary_reference(p1
, bloc
);
2238 t1
= Expression::make_dereference(t1
, Expression::NIL_CHECK_NOT_NEEDED
, bloc
);
2240 Expression
* t2
= Expression::make_temporary_reference(p2
, bloc
);
2241 t2
= Expression::make_dereference(t2
, Expression::NIL_CHECK_NOT_NEEDED
, bloc
);
2243 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, t1
, t2
, bloc
);
2245 // Return the equality comparison.
2246 Expression_list
* vals
= new Expression_list();
2247 vals
->push_back(cond
);
2248 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
2249 gogo
->add_statement(s
);
2252 // Return a composite literal for the type descriptor for a plain type
2253 // of kind RUNTIME_TYPE_KIND named NAME.
2256 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
2257 Named_type
* name
, const Methods
* methods
,
2258 bool only_value_methods
)
2260 Location bloc
= Linemap::predeclared_location();
2262 Type
* td_type
= Type::make_type_descriptor_type();
2263 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
2265 Expression_list
* vals
= new Expression_list();
2268 if (!this->has_pointer())
2269 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
2270 if (this->points_to() != NULL
)
2271 runtime_type_kind
|= RUNTIME_TYPE_KIND_DIRECT_IFACE
;
2274 if (this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2275 runtime_type_kind
|= RUNTIME_TYPE_KIND_GC_PROG
;
2277 Struct_field_list::const_iterator p
= fields
->begin();
2278 go_assert(p
->is_field_name("size"));
2279 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
2280 vals
->push_back(Expression::make_type_info(this, type_info
));
2283 go_assert(p
->is_field_name("ptrdata"));
2284 type_info
= Expression::TYPE_INFO_DESCRIPTOR_PTRDATA
;
2285 vals
->push_back(Expression::make_type_info(this, type_info
));
2288 go_assert(p
->is_field_name("hash"));
2291 h
= name
->hash_for_method(gogo
);
2293 h
= this->hash_for_method(gogo
);
2294 vals
->push_back(Expression::make_integer_ul(h
, p
->type(), bloc
));
2297 go_assert(p
->is_field_name("kind"));
2298 vals
->push_back(Expression::make_integer_ul(runtime_type_kind
, p
->type(),
2302 go_assert(p
->is_field_name("align"));
2303 type_info
= Expression::TYPE_INFO_ALIGNMENT
;
2304 vals
->push_back(Expression::make_type_info(this, type_info
));
2307 go_assert(p
->is_field_name("fieldAlign"));
2308 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
2309 vals
->push_back(Expression::make_type_info(this, type_info
));
2312 go_assert(p
->is_field_name("hashfn"));
2313 Function_type
* hash_fntype
= p
->type()->function_type();
2316 go_assert(p
->is_field_name("equalfn"));
2317 Function_type
* equal_fntype
= p
->type()->function_type();
2319 Named_object
* hash_fn
;
2320 Named_object
* equal_fn
;
2321 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
2323 if (hash_fn
== NULL
)
2324 vals
->push_back(Expression::make_cast(hash_fntype
,
2325 Expression::make_nil(bloc
),
2328 vals
->push_back(Expression::make_func_reference(hash_fn
, NULL
, bloc
));
2329 if (equal_fn
== NULL
)
2330 vals
->push_back(Expression::make_cast(equal_fntype
,
2331 Expression::make_nil(bloc
),
2334 vals
->push_back(Expression::make_func_reference(equal_fn
, NULL
, bloc
));
2337 go_assert(p
->is_field_name("gcdata"));
2338 vals
->push_back(Expression::make_gc_symbol(this));
2341 go_assert(p
->is_field_name("string"));
2342 Expression
* s
= Expression::make_string((name
!= NULL
2343 ? name
->reflection(gogo
)
2344 : this->reflection(gogo
)),
2346 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2349 go_assert(p
->is_field_name("uncommonType"));
2350 if (name
== NULL
&& methods
== NULL
)
2351 vals
->push_back(Expression::make_nil(bloc
));
2354 if (methods
== NULL
)
2355 methods
= name
->methods();
2356 vals
->push_back(this->uncommon_type_constructor(gogo
,
2359 only_value_methods
));
2363 go_assert(p
->is_field_name("ptrToThis"));
2364 if (name
== NULL
&& methods
== NULL
)
2365 vals
->push_back(Expression::make_nil(bloc
));
2370 pt
= Type::make_pointer_type(name
);
2372 pt
= Type::make_pointer_type(this);
2373 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
2377 go_assert(p
== fields
->end());
2379 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
2382 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2383 // length used by the gc toolchain, and also appears in
2384 // libgo/go/reflect/type.go.
2386 static const int64_t max_ptrmask_bytes
= 2048;
2388 // Return a pointer to the Garbage Collection information for this type.
2391 Type::gc_symbol_pointer(Gogo
* gogo
)
2393 Type
* t
= this->unalias();
2395 if (!t
->has_pointer())
2396 return gogo
->backend()->nil_pointer_expression();
2398 if (t
->gc_symbol_var_
== NULL
)
2400 t
->make_gc_symbol_var(gogo
);
2401 go_assert(t
->gc_symbol_var_
!= NULL
);
2403 Location bloc
= Linemap::predeclared_location();
2404 Bexpression
* var_expr
=
2405 gogo
->backend()->var_expression(t
->gc_symbol_var_
, bloc
);
2406 Bexpression
* addr_expr
=
2407 gogo
->backend()->address_expression(var_expr
, bloc
);
2409 Type
* uint8_type
= Type::lookup_integer_type("uint8");
2410 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
2411 Btype
* ubtype
= pointer_uint8_type
->get_backend(gogo
);
2412 return gogo
->backend()->convert_expression(ubtype
, addr_expr
, bloc
);
2415 // A mapping from unnamed types to GC symbol variables.
2417 Type::GC_symbol_vars
Type::gc_symbol_vars
;
2419 // Build the GC symbol for this type.
2422 Type::make_gc_symbol_var(Gogo
* gogo
)
2424 go_assert(this->gc_symbol_var_
== NULL
);
2426 Named_type
* nt
= this->named_type();
2428 // We can have multiple instances of unnamed types and similar to type
2429 // descriptors, we only want to the emit the GC data once, so we use a
2431 Bvariable
** phash
= NULL
;
2434 Bvariable
* bvnull
= NULL
;
2435 std::pair
<GC_symbol_vars::iterator
, bool> ins
=
2436 Type::gc_symbol_vars
.insert(std::make_pair(this, bvnull
));
2439 // We've already built a gc symbol for this type.
2440 this->gc_symbol_var_
= ins
.first
->second
;
2443 phash
= &ins
.first
->second
;
2448 if (!this->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
2450 this->gc_symbol_var_
= this->gc_ptrmask_var(gogo
, ptrsize
, ptrdata
);
2452 *phash
= this->gc_symbol_var_
;
2456 std::string sym_name
= gogo
->gc_symbol_name(this);
2458 // Build the contents of the gc symbol.
2459 Expression
* sym_init
= this->gcprog_constructor(gogo
, ptrsize
, ptrdata
);
2460 Btype
* sym_btype
= sym_init
->type()->get_backend(gogo
);
2462 // If the type descriptor for this type is defined somewhere else, so is the
2464 const Package
* dummy
;
2465 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
2467 std::string
asm_name(go_selectively_encode_id(sym_name
));
2468 this->gc_symbol_var_
=
2469 gogo
->backend()->implicit_variable_reference(sym_name
, asm_name
,
2472 *phash
= this->gc_symbol_var_
;
2476 // See if this gc symbol can appear in multiple packages.
2477 bool is_common
= false;
2480 // We create the symbol for a builtin type whenever we need
2482 is_common
= nt
->is_builtin();
2486 // This is an unnamed type. The descriptor could be defined in
2487 // any package where it is needed, and the linker will pick one
2488 // descriptor to keep.
2492 // Since we are building the GC symbol in this package, we must create the
2493 // variable before converting the initializer to its backend representation
2494 // because the initializer may refer to the GC symbol for this type.
2495 std::string
asm_name(go_selectively_encode_id(sym_name
));
2496 this->gc_symbol_var_
=
2497 gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2498 sym_btype
, false, true, is_common
, 0);
2500 *phash
= this->gc_symbol_var_
;
2502 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2503 context
.set_is_const();
2504 Bexpression
* sym_binit
= sym_init
->get_backend(&context
);
2505 gogo
->backend()->implicit_variable_set_init(this->gc_symbol_var_
, sym_name
,
2506 sym_btype
, false, true, is_common
,
2510 // Return whether this type needs a GC program, and set *PTRDATA to
2511 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2515 Type::needs_gcprog(Gogo
* gogo
, int64_t* ptrsize
, int64_t* ptrdata
)
2517 Type
* voidptr
= Type::make_pointer_type(Type::make_void_type());
2518 if (!voidptr
->backend_type_size(gogo
, ptrsize
))
2521 if (!this->backend_type_ptrdata(gogo
, ptrdata
))
2523 go_assert(saw_errors());
2527 return *ptrdata
/ *ptrsize
> max_ptrmask_bytes
;
2530 // A simple class used to build a GC ptrmask for a type.
2535 Ptrmask(size_t count
)
2536 : bits_((count
+ 7) / 8, 0)
2540 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2546 constructor(Gogo
* gogo
) const;
2551 { this->bits_
.at(index
/ 8) |= 1 << (index
% 8); }
2554 std::vector
<unsigned char> bits_
;
2557 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2558 // counts in bytes. PTRSIZE is the size of a pointer on the target
2562 Ptrmask::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2564 switch (type
->base()->classification())
2567 case Type::TYPE_NIL
:
2568 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2569 case Type::TYPE_NAMED
:
2570 case Type::TYPE_FORWARD
:
2573 case Type::TYPE_ERROR
:
2574 case Type::TYPE_VOID
:
2575 case Type::TYPE_BOOLEAN
:
2576 case Type::TYPE_INTEGER
:
2577 case Type::TYPE_FLOAT
:
2578 case Type::TYPE_COMPLEX
:
2579 case Type::TYPE_SINK
:
2582 case Type::TYPE_FUNCTION
:
2583 case Type::TYPE_POINTER
:
2584 case Type::TYPE_MAP
:
2585 case Type::TYPE_CHANNEL
:
2586 // These types are all a single pointer.
2587 go_assert((offset
% ptrsize
) == 0);
2588 this->set(offset
/ ptrsize
);
2591 case Type::TYPE_STRING
:
2592 // A string starts with a single pointer.
2593 go_assert((offset
% ptrsize
) == 0);
2594 this->set(offset
/ ptrsize
);
2597 case Type::TYPE_INTERFACE
:
2598 // An interface is two pointers.
2599 go_assert((offset
% ptrsize
) == 0);
2600 this->set(offset
/ ptrsize
);
2601 this->set((offset
/ ptrsize
) + 1);
2604 case Type::TYPE_STRUCT
:
2606 if (!type
->has_pointer())
2609 const Struct_field_list
* fields
= type
->struct_type()->fields();
2610 int64_t soffset
= 0;
2611 for (Struct_field_list::const_iterator pf
= fields
->begin();
2612 pf
!= fields
->end();
2615 int64_t field_align
;
2616 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2618 go_assert(saw_errors());
2621 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2623 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2626 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2628 go_assert(saw_errors());
2631 soffset
+= field_size
;
2636 case Type::TYPE_ARRAY
:
2637 if (type
->is_slice_type())
2639 // A slice starts with a single pointer.
2640 go_assert((offset
% ptrsize
) == 0);
2641 this->set(offset
/ ptrsize
);
2646 if (!type
->has_pointer())
2650 if (!type
->array_type()->int_length(&len
))
2652 go_assert(saw_errors());
2656 Type
* element_type
= type
->array_type()->element_type();
2658 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2660 go_assert(saw_errors());
2664 int64_t eoffset
= 0;
2665 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2666 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2672 // Return a symbol name for this ptrmask. This is used to coalesce
2673 // identical ptrmasks, which are common. The symbol name must use
2674 // only characters that are valid in symbols. It's nice if it's
2675 // short. We convert it to a string that uses only 32 characters,
2676 // avoiding digits and u and U.
2679 Ptrmask::symname() const
2681 const char chars
[33] = "abcdefghijklmnopqrstvwxyzABCDEFG";
2682 go_assert(chars
[32] == '\0');
2686 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2687 p
!= this->bits_
.end();
2690 b
|= *p
<< remaining
;
2692 while (remaining
>= 5)
2694 ret
+= chars
[b
& 0x1f];
2699 while (remaining
> 0)
2701 ret
+= chars
[b
& 0x1f];
2708 // Return a constructor for this ptrmask. This will be used to
2709 // initialize the runtime ptrmask value.
2712 Ptrmask::constructor(Gogo
* gogo
) const
2714 Location bloc
= Linemap::predeclared_location();
2715 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
2716 Expression
* len
= Expression::make_integer_ul(this->bits_
.size(), NULL
,
2718 Array_type
* at
= Type::make_array_type(byte_type
, len
);
2719 Expression_list
* vals
= new Expression_list();
2720 vals
->reserve(this->bits_
.size());
2721 for (std::vector
<unsigned char>::const_iterator p
= this->bits_
.begin();
2722 p
!= this->bits_
.end();
2724 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
2725 return Expression::make_array_composite_literal(at
, vals
, bloc
);
2728 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2729 Type::GC_gcbits_vars
Type::gc_gcbits_vars
;
2731 // Return a ptrmask variable for a type. For a type descriptor this
2732 // is only used for variables that are small enough to not need a
2733 // gcprog, but for a global variable this is used for a variable of
2734 // any size. PTRDATA is the number of bytes of the type that contain
2735 // pointer data. PTRSIZE is the size of a pointer on the target
2739 Type::gc_ptrmask_var(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
2741 Ptrmask
ptrmask(ptrdata
/ ptrsize
);
2742 if (ptrdata
>= ptrsize
)
2743 ptrmask
.set_from(gogo
, this, ptrsize
, 0);
2746 // This can happen in error cases. Just build an empty gcbits.
2747 go_assert(saw_errors());
2750 std::string sym_name
= gogo
->ptrmask_symbol_name(ptrmask
.symname());
2751 Bvariable
* bvnull
= NULL
;
2752 std::pair
<GC_gcbits_vars::iterator
, bool> ins
=
2753 Type::gc_gcbits_vars
.insert(std::make_pair(sym_name
, bvnull
));
2756 // We've already built a GC symbol for this set of gcbits.
2757 return ins
.first
->second
;
2760 Expression
* val
= ptrmask
.constructor(gogo
);
2761 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2762 context
.set_is_const();
2763 Bexpression
* bval
= val
->get_backend(&context
);
2765 std::string
asm_name(go_selectively_encode_id(sym_name
));
2766 Btype
*btype
= val
->type()->get_backend(gogo
);
2767 Bvariable
* ret
= gogo
->backend()->implicit_variable(sym_name
, asm_name
,
2770 gogo
->backend()->implicit_variable_set_init(ret
, sym_name
, btype
, false,
2772 ins
.first
->second
= ret
;
2776 // A GCProg is used to build a program for the garbage collector.
2777 // This is used for types with a lot of pointer data, to reduce the
2778 // size of the data in the compiled program. The program is expanded
2779 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2785 : bytes_(), index_(0), nb_(0)
2788 // The number of bits described so far.
2791 { return this->index_
; }
2794 set_from(Gogo
*, Type
*, int64_t ptrsize
, int64_t offset
);
2800 constructor(Gogo
* gogo
) const;
2807 should_repeat(int64_t, int64_t);
2810 repeat(int64_t, int64_t);
2813 zero_until(int64_t);
2824 // Add a byte to the program.
2826 byte(unsigned char x
)
2827 { this->bytes_
.push_back(x
); }
2829 // The maximum number of bytes of literal bits.
2830 static const int max_literal
= 127;
2833 std::vector
<unsigned char> bytes_
;
2834 // The index of the last bit described.
2836 // The current set of literal bits.
2837 unsigned char b_
[max_literal
];
2838 // The current number of literal bits.
2842 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2843 // counts in bytes. PTRSIZE is the size of a pointer on the target
2847 GCProg::set_from(Gogo
* gogo
, Type
* type
, int64_t ptrsize
, int64_t offset
)
2849 switch (type
->base()->classification())
2852 case Type::TYPE_NIL
:
2853 case Type::TYPE_CALL_MULTIPLE_RESULT
:
2854 case Type::TYPE_NAMED
:
2855 case Type::TYPE_FORWARD
:
2858 case Type::TYPE_ERROR
:
2859 case Type::TYPE_VOID
:
2860 case Type::TYPE_BOOLEAN
:
2861 case Type::TYPE_INTEGER
:
2862 case Type::TYPE_FLOAT
:
2863 case Type::TYPE_COMPLEX
:
2864 case Type::TYPE_SINK
:
2867 case Type::TYPE_FUNCTION
:
2868 case Type::TYPE_POINTER
:
2869 case Type::TYPE_MAP
:
2870 case Type::TYPE_CHANNEL
:
2871 // These types are all a single pointer.
2872 go_assert((offset
% ptrsize
) == 0);
2873 this->ptr(offset
/ ptrsize
);
2876 case Type::TYPE_STRING
:
2877 // A string starts with a single pointer.
2878 go_assert((offset
% ptrsize
) == 0);
2879 this->ptr(offset
/ ptrsize
);
2882 case Type::TYPE_INTERFACE
:
2883 // An interface is two pointers.
2884 go_assert((offset
% ptrsize
) == 0);
2885 this->ptr(offset
/ ptrsize
);
2886 this->ptr((offset
/ ptrsize
) + 1);
2889 case Type::TYPE_STRUCT
:
2891 if (!type
->has_pointer())
2894 const Struct_field_list
* fields
= type
->struct_type()->fields();
2895 int64_t soffset
= 0;
2896 for (Struct_field_list::const_iterator pf
= fields
->begin();
2897 pf
!= fields
->end();
2900 int64_t field_align
;
2901 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
2903 go_assert(saw_errors());
2906 soffset
= (soffset
+ (field_align
- 1)) &~ (field_align
- 1);
2908 this->set_from(gogo
, pf
->type(), ptrsize
, offset
+ soffset
);
2911 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
2913 go_assert(saw_errors());
2916 soffset
+= field_size
;
2921 case Type::TYPE_ARRAY
:
2922 if (type
->is_slice_type())
2924 // A slice starts with a single pointer.
2925 go_assert((offset
% ptrsize
) == 0);
2926 this->ptr(offset
/ ptrsize
);
2931 if (!type
->has_pointer())
2935 if (!type
->array_type()->int_length(&len
))
2937 go_assert(saw_errors());
2941 Type
* element_type
= type
->array_type()->element_type();
2943 // Flatten array of array to a big array by multiplying counts.
2944 while (element_type
->array_type() != NULL
2945 && !element_type
->is_slice_type())
2948 if (!element_type
->array_type()->int_length(&ele_len
))
2950 go_assert(saw_errors());
2955 element_type
= element_type
->array_type()->element_type();
2959 if (!element_type
->backend_type_size(gogo
, &ele_size
))
2961 go_assert(saw_errors());
2965 go_assert(len
> 0 && ele_size
> 0);
2967 if (!this->should_repeat(ele_size
/ ptrsize
, len
))
2969 // Cheaper to just emit the bits.
2970 int64_t eoffset
= 0;
2971 for (int64_t i
= 0; i
< len
; i
++, eoffset
+= ele_size
)
2972 this->set_from(gogo
, element_type
, ptrsize
, offset
+ eoffset
);
2976 go_assert((offset
% ptrsize
) == 0);
2977 go_assert((ele_size
% ptrsize
) == 0);
2978 this->set_from(gogo
, element_type
, ptrsize
, offset
);
2979 this->zero_until((offset
+ ele_size
) / ptrsize
);
2980 this->repeat(ele_size
/ ptrsize
, len
- 1);
2988 // Emit a 1 into the bit stream of a GC program at the given bit index.
2991 GCProg::ptr(int64_t index
)
2993 go_assert(index
>= this->index_
);
2994 this->zero_until(index
);
2998 // Return whether it is worthwhile to use a repeat to describe c
2999 // elements of n bits each, compared to just emitting c copies of the
3000 // n-bit description.
3003 GCProg::should_repeat(int64_t n
, int64_t c
)
3005 // Repeat if there is more than 1 item and if the total data doesn't
3006 // fit into four bytes.
3007 return c
> 1 && c
* n
> 4 * 8;
3010 // Emit an instruction to repeat the description of the last n words c
3011 // times (including the initial description, so c + 1 times in total).
3014 GCProg::repeat(int64_t n
, int64_t c
)
3016 if (n
== 0 || c
== 0)
3020 this->byte(0x80 | static_cast<unsigned char>(n
& 0x7f));
3027 this->index_
+= n
* c
;
3030 // Add zeros to the bit stream up to the given index.
3033 GCProg::zero_until(int64_t index
)
3035 go_assert(index
>= this->index_
);
3036 int64_t skip
= index
- this->index_
;
3041 for (int64_t i
= 0; i
< skip
; ++i
)
3047 this->repeat(1, skip
- 1);
3050 // Add a single literal bit to the program.
3053 GCProg::lit(unsigned char x
)
3055 if (this->nb_
== GCProg::max_literal
)
3057 this->b_
[this->nb_
] = x
;
3062 // Emit the varint encoding of x.
3065 GCProg::varint(int64_t x
)
3070 this->byte(0x80 | static_cast<unsigned char>(x
& 0x7f));
3073 this->byte(static_cast<unsigned char>(x
& 0x7f));
3076 // Flush any pending literal bits.
3083 this->byte(static_cast<unsigned char>(this->nb_
));
3084 unsigned char bits
= 0;
3085 for (int i
= 0; i
< this->nb_
; ++i
)
3087 bits
|= this->b_
[i
] << (i
% 8);
3088 if ((i
+ 1) % 8 == 0)
3094 if (this->nb_
% 8 != 0)
3099 // Mark the end of a GC program.
3108 // Return an Expression for the bytes in a GC program.
3111 GCProg::constructor(Gogo
* gogo
) const
3113 Location bloc
= Linemap::predeclared_location();
3115 // The first four bytes are the length of the program in target byte
3116 // order. Build a struct whose first type is uint32 to make this
3119 Type
* uint32_type
= Type::lookup_integer_type("uint32");
3121 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
3122 Expression
* len
= Expression::make_integer_ul(this->bytes_
.size(), NULL
,
3124 Array_type
* at
= Type::make_array_type(byte_type
, len
);
3126 Struct_type
* st
= Type::make_builtin_struct_type(2, "len", uint32_type
,
3129 Expression_list
* vals
= new Expression_list();
3130 vals
->reserve(this->bytes_
.size());
3131 for (std::vector
<unsigned char>::const_iterator p
= this->bytes_
.begin();
3132 p
!= this->bytes_
.end();
3134 vals
->push_back(Expression::make_integer_ul(*p
, byte_type
, bloc
));
3135 Expression
* bytes
= Expression::make_array_composite_literal(at
, vals
, bloc
);
3137 vals
= new Expression_list();
3138 vals
->push_back(Expression::make_integer_ul(this->bytes_
.size(), uint32_type
,
3140 vals
->push_back(bytes
);
3142 return Expression::make_struct_composite_literal(st
, vals
, bloc
);
3145 // Return a composite literal for the garbage collection program for
3146 // this type. This is only used for types that are too large to use a
3150 Type::gcprog_constructor(Gogo
* gogo
, int64_t ptrsize
, int64_t ptrdata
)
3152 Location bloc
= Linemap::predeclared_location();
3155 prog
.set_from(gogo
, this, ptrsize
, 0);
3156 int64_t offset
= prog
.bit_index() * ptrsize
;
3160 if (!this->backend_type_size(gogo
, &type_size
))
3162 go_assert(saw_errors());
3163 return Expression::make_error(bloc
);
3166 go_assert(offset
>= ptrdata
&& offset
<= type_size
);
3168 return prog
.constructor(gogo
);
3171 // Return a composite literal for the uncommon type information for
3172 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3173 // struct. If name is not NULL, it is the name of the type. If
3174 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3175 // is true if only value methods should be included. At least one of
3176 // NAME and METHODS must not be NULL.
3179 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
3180 Named_type
* name
, const Methods
* methods
,
3181 bool only_value_methods
) const
3183 Location bloc
= Linemap::predeclared_location();
3185 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
3187 Expression_list
* vals
= new Expression_list();
3190 Struct_field_list::const_iterator p
= fields
->begin();
3191 go_assert(p
->is_field_name("name"));
3194 go_assert(p
->is_field_name("pkgPath"));
3198 vals
->push_back(Expression::make_nil(bloc
));
3199 vals
->push_back(Expression::make_nil(bloc
));
3203 Named_object
* no
= name
->named_object();
3204 std::string n
= Gogo::unpack_hidden_name(no
->name());
3205 Expression
* s
= Expression::make_string(n
, bloc
);
3206 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3208 if (name
->is_builtin())
3209 vals
->push_back(Expression::make_nil(bloc
));
3212 const Package
* package
= no
->package();
3213 const std::string
& pkgpath(package
== NULL
3215 : package
->pkgpath());
3216 s
= Expression::make_string(pkgpath
, bloc
);
3217 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3222 go_assert(p
->is_field_name("methods"));
3223 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
3224 only_value_methods
));
3227 go_assert(p
== fields
->end());
3229 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
3231 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
3234 // Sort methods by name.
3240 operator()(const std::pair
<std::string
, const Method
*>& m1
,
3241 const std::pair
<std::string
, const Method
*>& m2
) const
3243 return (Gogo::unpack_hidden_name(m1
.first
)
3244 < Gogo::unpack_hidden_name(m2
.first
));
3248 // Return a composite literal for the type method table for this type.
3249 // METHODS_TYPE is the type of the table, and is a slice type.
3250 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3251 // then only value methods are used.
3254 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
3255 const Methods
* methods
,
3256 bool only_value_methods
) const
3258 Location bloc
= Linemap::predeclared_location();
3260 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
3261 if (methods
!= NULL
)
3263 smethods
.reserve(methods
->count());
3264 for (Methods::const_iterator p
= methods
->begin();
3265 p
!= methods
->end();
3268 if (p
->second
->is_ambiguous())
3270 if (only_value_methods
&& !p
->second
->is_value_method())
3273 // This is where we implement the magic //go:nointerface
3274 // comment. If we saw that comment, we don't add this
3275 // method to the type descriptor.
3276 if (p
->second
->nointerface())
3279 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
3283 if (smethods
.empty())
3284 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
3286 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
3288 Type
* method_type
= methods_type
->array_type()->element_type();
3290 Expression_list
* vals
= new Expression_list();
3291 vals
->reserve(smethods
.size());
3292 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
3294 p
!= smethods
.end();
3296 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
3297 p
->second
, only_value_methods
));
3299 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
3302 // Return a composite literal for a single method. METHOD_TYPE is the
3303 // type of the entry. METHOD_NAME is the name of the method and M is
3304 // the method information.
3307 Type::method_constructor(Gogo
*, Type
* method_type
,
3308 const std::string
& method_name
,
3310 bool only_value_methods
) const
3312 Location bloc
= Linemap::predeclared_location();
3314 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
3316 Expression_list
* vals
= new Expression_list();
3319 Struct_field_list::const_iterator p
= fields
->begin();
3320 go_assert(p
->is_field_name("name"));
3321 const std::string n
= Gogo::unpack_hidden_name(method_name
);
3322 Expression
* s
= Expression::make_string(n
, bloc
);
3323 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3326 go_assert(p
->is_field_name("pkgPath"));
3327 if (!Gogo::is_hidden_name(method_name
))
3328 vals
->push_back(Expression::make_nil(bloc
));
3331 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
3333 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3336 Named_object
* no
= (m
->needs_stub_method()
3338 : m
->named_object());
3340 Function_type
* mtype
;
3341 if (no
->is_function())
3342 mtype
= no
->func_value()->type();
3344 mtype
= no
->func_declaration_value()->type();
3345 go_assert(mtype
->is_method());
3346 Type
* nonmethod_type
= mtype
->copy_without_receiver();
3349 go_assert(p
->is_field_name("mtyp"));
3350 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3353 go_assert(p
->is_field_name("typ"));
3354 bool want_pointer_receiver
= !only_value_methods
&& m
->is_value_method();
3355 nonmethod_type
= mtype
->copy_with_receiver_as_param(want_pointer_receiver
);
3356 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
3359 go_assert(p
->is_field_name("tfn"));
3360 vals
->push_back(Expression::make_func_code_reference(no
, bloc
));
3363 go_assert(p
== fields
->end());
3365 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
3368 // Return a composite literal for the type descriptor of a plain type.
3369 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3370 // NULL, it is the name to use as well as the list of methods.
3373 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
3376 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
3380 // Return the type reflection string for this type.
3383 Type::reflection(Gogo
* gogo
) const
3387 // The do_reflection virtual function should set RET to the
3388 // reflection string.
3389 this->do_reflection(gogo
, &ret
);
3394 // Return whether the backend size of the type is known.
3397 Type::is_backend_type_size_known(Gogo
* gogo
)
3399 switch (this->classification_
)
3413 case TYPE_INTERFACE
:
3418 const Struct_field_list
* fields
= this->struct_type()->fields();
3419 for (Struct_field_list::const_iterator pf
= fields
->begin();
3420 pf
!= fields
->end();
3422 if (!pf
->type()->is_backend_type_size_known(gogo
))
3429 const Array_type
* at
= this->array_type();
3430 if (at
->length() == NULL
)
3434 Numeric_constant nc
;
3435 if (!at
->length()->numeric_constant_value(&nc
))
3438 if (!nc
.to_int(&ival
))
3441 return at
->element_type()->is_backend_type_size_known(gogo
);
3446 this->named_type()->convert(gogo
);
3447 return this->named_type()->is_named_backend_type_size_known();
3451 Forward_declaration_type
* fdt
= this->forward_declaration_type();
3452 return fdt
->real_type()->is_backend_type_size_known(gogo
);
3456 case TYPE_CALL_MULTIPLE_RESULT
:
3464 // If the size of the type can be determined, set *PSIZE to the size
3465 // in bytes and return true. Otherwise, return false. This queries
3469 Type::backend_type_size(Gogo
* gogo
, int64_t *psize
)
3471 if (!this->is_backend_type_size_known(gogo
))
3473 if (this->is_error_type())
3475 Btype
* bt
= this->get_backend_placeholder(gogo
);
3476 *psize
= gogo
->backend()->type_size(bt
);
3479 if (this->named_type() != NULL
)
3480 go_error_at(this->named_type()->location(),
3481 "type %s larger than address space",
3482 Gogo::message_name(this->named_type()->name()).c_str());
3484 go_error_at(Linemap::unknown_location(),
3485 "type %s larger than address space",
3486 this->reflection(gogo
).c_str());
3488 // Make this an error type to avoid knock-on errors.
3489 this->classification_
= TYPE_ERROR
;
3495 // If the alignment of the type can be determined, set *PALIGN to
3496 // the alignment in bytes and return true. Otherwise, return false.
3499 Type::backend_type_align(Gogo
* gogo
, int64_t *palign
)
3501 if (!this->is_backend_type_size_known(gogo
))
3503 Btype
* bt
= this->get_backend_placeholder(gogo
);
3504 *palign
= gogo
->backend()->type_alignment(bt
);
3508 // Like backend_type_align, but return the alignment when used as a
3512 Type::backend_type_field_align(Gogo
* gogo
, int64_t *palign
)
3514 if (!this->is_backend_type_size_known(gogo
))
3516 Btype
* bt
= this->get_backend_placeholder(gogo
);
3517 *palign
= gogo
->backend()->type_field_alignment(bt
);
3521 // Get the ptrdata value for a type. This is the size of the prefix
3522 // of the type that contains all pointers. Store the ptrdata in
3523 // *PPTRDATA and return whether we found it.
3526 Type::backend_type_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3530 if (!this->has_pointer())
3533 if (!this->is_backend_type_size_known(gogo
))
3536 switch (this->classification_
)
3545 // These types are nothing but a pointer.
3546 return this->backend_type_size(gogo
, pptrdata
);
3548 case TYPE_INTERFACE
:
3549 // An interface is a struct of two pointers.
3550 return this->backend_type_size(gogo
, pptrdata
);
3554 // A string is a struct whose first field is a pointer, and
3555 // whose second field is not.
3556 Type
* uint8_type
= Type::lookup_integer_type("uint8");
3557 Type
* ptr
= Type::make_pointer_type(uint8_type
);
3558 return ptr
->backend_type_size(gogo
, pptrdata
);
3563 return this->base()->backend_type_ptrdata(gogo
, pptrdata
);
3567 const Struct_field_list
* fields
= this->struct_type()->fields();
3569 const Struct_field
*ptr
= NULL
;
3570 int64_t ptr_offset
= 0;
3571 for (Struct_field_list::const_iterator pf
= fields
->begin();
3572 pf
!= fields
->end();
3575 int64_t field_align
;
3576 if (!pf
->type()->backend_type_field_align(gogo
, &field_align
))
3578 offset
= (offset
+ (field_align
- 1)) &~ (field_align
- 1);
3580 if (pf
->type()->has_pointer())
3583 ptr_offset
= offset
;
3587 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
3589 offset
+= field_size
;
3594 int64_t ptr_ptrdata
;
3595 if (!ptr
->type()->backend_type_ptrdata(gogo
, &ptr_ptrdata
))
3597 *pptrdata
= ptr_offset
+ ptr_ptrdata
;
3603 if (this->is_slice_type())
3605 // A slice is a struct whose first field is a pointer, and
3606 // whose remaining fields are not.
3607 Type
* element_type
= this->array_type()->element_type();
3608 Type
* ptr
= Type::make_pointer_type(element_type
);
3609 return ptr
->backend_type_size(gogo
, pptrdata
);
3613 Numeric_constant nc
;
3614 if (!this->array_type()->length()->numeric_constant_value(&nc
))
3617 if (!nc
.to_memory_size(&len
))
3620 Type
* element_type
= this->array_type()->element_type();
3622 int64_t ele_ptrdata
;
3623 if (!element_type
->backend_type_size(gogo
, &ele_size
)
3624 || !element_type
->backend_type_ptrdata(gogo
, &ele_ptrdata
))
3626 go_assert(ele_size
> 0 && ele_ptrdata
> 0);
3628 *pptrdata
= (len
- 1) * ele_size
+ ele_ptrdata
;
3640 case TYPE_CALL_MULTIPLE_RESULT
:
3645 // Get the ptrdata value to store in a type descriptor. This is
3646 // normally the same as backend_type_ptrdata, but for a type that is
3647 // large enough to use a gcprog we may need to store a different value
3648 // if it ends with an array. If the gcprog uses a repeat descriptor
3649 // for the array, and if the array element ends with non-pointer data,
3650 // then the gcprog will produce a value that describes the complete
3651 // array where the backend ptrdata will omit the non-pointer elements
3652 // of the final array element. This is a subtle difference but the
3653 // run time code checks it to verify that it has expanded a gcprog as
3657 Type::descriptor_ptrdata(Gogo
* gogo
, int64_t* pptrdata
)
3659 int64_t backend_ptrdata
;
3660 if (!this->backend_type_ptrdata(gogo
, &backend_ptrdata
))
3664 if (!this->needs_gcprog(gogo
, &ptrsize
, &backend_ptrdata
))
3666 *pptrdata
= backend_ptrdata
;
3671 prog
.set_from(gogo
, this, ptrsize
, 0);
3672 int64_t offset
= prog
.bit_index() * ptrsize
;
3674 go_assert(offset
>= backend_ptrdata
);
3679 // Default function to export a type.
3682 Type::do_export(Export
*) const
3690 Type::import_type(Import
* imp
)
3692 if (imp
->match_c_string("("))
3693 return Function_type::do_import(imp
);
3694 else if (imp
->match_c_string("*"))
3695 return Pointer_type::do_import(imp
);
3696 else if (imp
->match_c_string("struct "))
3697 return Struct_type::do_import(imp
);
3698 else if (imp
->match_c_string("["))
3699 return Array_type::do_import(imp
);
3700 else if (imp
->match_c_string("map "))
3701 return Map_type::do_import(imp
);
3702 else if (imp
->match_c_string("chan "))
3703 return Channel_type::do_import(imp
);
3704 else if (imp
->match_c_string("interface"))
3705 return Interface_type::do_import(imp
);
3708 go_error_at(imp
->location(), "import error: expected type");
3709 return Type::make_error_type();
3713 // Class Error_type.
3715 // Return the backend representation of an Error type.
3718 Error_type::do_get_backend(Gogo
* gogo
)
3720 return gogo
->backend()->error_type();
3723 // Return an expression for the type descriptor for an error type.
3727 Error_type::do_type_descriptor(Gogo
*, Named_type
*)
3729 return Expression::make_error(Linemap::predeclared_location());
3732 // We should not be asked for the reflection string for an error type.
3735 Error_type::do_reflection(Gogo
*, std::string
*) const
3737 go_assert(saw_errors());
3741 Type::make_error_type()
3743 static Error_type singleton_error_type
;
3744 return &singleton_error_type
;
3749 // Get the backend representation of a void type.
3752 Void_type::do_get_backend(Gogo
* gogo
)
3754 return gogo
->backend()->void_type();
3758 Type::make_void_type()
3760 static Void_type singleton_void_type
;
3761 return &singleton_void_type
;
3764 // Class Boolean_type.
3766 // Return the backend representation of the boolean type.
3769 Boolean_type::do_get_backend(Gogo
* gogo
)
3771 return gogo
->backend()->bool_type();
3774 // Make the type descriptor.
3777 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3780 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
3783 Named_object
* no
= gogo
->lookup_global("bool");
3784 go_assert(no
!= NULL
);
3785 return Type::type_descriptor(gogo
, no
->type_value());
3790 Type::make_boolean_type()
3792 static Boolean_type boolean_type
;
3793 return &boolean_type
;
3796 // The named type "bool".
3798 static Named_type
* named_bool_type
;
3800 // Get the named type "bool".
3803 Type::lookup_bool_type()
3805 return named_bool_type
;
3808 // Make the named type "bool".
3811 Type::make_named_bool_type()
3813 Type
* bool_type
= Type::make_boolean_type();
3814 Named_object
* named_object
=
3815 Named_object::make_type("bool", NULL
, bool_type
,
3816 Linemap::predeclared_location());
3817 Named_type
* named_type
= named_object
->type_value();
3818 named_bool_type
= named_type
;
3822 // Class Integer_type.
3824 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
3826 // Create a new integer type. Non-abstract integer types always have
3830 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
3831 int bits
, int runtime_type_kind
)
3833 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
3835 std::string
sname(name
);
3836 Named_object
* named_object
=
3837 Named_object::make_type(sname
, NULL
, integer_type
,
3838 Linemap::predeclared_location());
3839 Named_type
* named_type
= named_object
->type_value();
3840 std::pair
<Named_integer_types::iterator
, bool> ins
=
3841 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
3842 go_assert(ins
.second
);
3846 // Look up an existing integer type.
3849 Integer_type::lookup_integer_type(const char* name
)
3851 Named_integer_types::const_iterator p
=
3852 Integer_type::named_integer_types
.find(name
);
3853 go_assert(p
!= Integer_type::named_integer_types
.end());
3857 // Create a new abstract integer type.
3860 Integer_type::create_abstract_integer_type()
3862 static Integer_type
* abstract_type
;
3863 if (abstract_type
== NULL
)
3865 Type
* int_type
= Type::lookup_integer_type("int");
3866 abstract_type
= new Integer_type(true, false,
3867 int_type
->integer_type()->bits(),
3868 RUNTIME_TYPE_KIND_INT
);
3870 return abstract_type
;
3873 // Create a new abstract character type.
3876 Integer_type::create_abstract_character_type()
3878 static Integer_type
* abstract_type
;
3879 if (abstract_type
== NULL
)
3881 abstract_type
= new Integer_type(true, false, 32,
3882 RUNTIME_TYPE_KIND_INT32
);
3883 abstract_type
->set_is_rune();
3885 return abstract_type
;
3888 // Integer type compatibility.
3891 Integer_type::is_identical(const Integer_type
* t
) const
3893 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
3895 return this->is_abstract_
== t
->is_abstract_
;
3901 Integer_type::do_hash_for_method(Gogo
*) const
3903 return ((this->bits_
<< 4)
3904 + ((this->is_unsigned_
? 1 : 0) << 8)
3905 + ((this->is_abstract_
? 1 : 0) << 9));
3908 // Convert an Integer_type to the backend representation.
3911 Integer_type::do_get_backend(Gogo
* gogo
)
3913 if (this->is_abstract_
)
3915 go_assert(saw_errors());
3916 return gogo
->backend()->error_type();
3918 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
3921 // The type descriptor for an integer type. Integer types are always
3925 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3927 go_assert(name
!= NULL
|| saw_errors());
3928 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
3931 // We should not be asked for the reflection string of a basic type.
3934 Integer_type::do_reflection(Gogo
*, std::string
*) const
3936 go_assert(saw_errors());
3939 // Make an integer type.
3942 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
3943 int runtime_type_kind
)
3945 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
3949 // Make an abstract integer type.
3952 Type::make_abstract_integer_type()
3954 return Integer_type::create_abstract_integer_type();
3957 // Make an abstract character type.
3960 Type::make_abstract_character_type()
3962 return Integer_type::create_abstract_character_type();
3965 // Look up an integer type.
3968 Type::lookup_integer_type(const char* name
)
3970 return Integer_type::lookup_integer_type(name
);
3973 // Class Float_type.
3975 Float_type::Named_float_types
Float_type::named_float_types
;
3977 // Create a new float type. Non-abstract float types always have
3981 Float_type::create_float_type(const char* name
, int bits
,
3982 int runtime_type_kind
)
3984 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
3985 std::string
sname(name
);
3986 Named_object
* named_object
=
3987 Named_object::make_type(sname
, NULL
, float_type
,
3988 Linemap::predeclared_location());
3989 Named_type
* named_type
= named_object
->type_value();
3990 std::pair
<Named_float_types::iterator
, bool> ins
=
3991 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
3992 go_assert(ins
.second
);
3996 // Look up an existing float type.
3999 Float_type::lookup_float_type(const char* name
)
4001 Named_float_types::const_iterator p
=
4002 Float_type::named_float_types
.find(name
);
4003 go_assert(p
!= Float_type::named_float_types
.end());
4007 // Create a new abstract float type.
4010 Float_type::create_abstract_float_type()
4012 static Float_type
* abstract_type
;
4013 if (abstract_type
== NULL
)
4014 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
4015 return abstract_type
;
4018 // Whether this type is identical with T.
4021 Float_type::is_identical(const Float_type
* t
) const
4023 if (this->bits_
!= t
->bits_
)
4025 return this->is_abstract_
== t
->is_abstract_
;
4031 Float_type::do_hash_for_method(Gogo
*) const
4033 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4036 // Convert to the backend representation.
4039 Float_type::do_get_backend(Gogo
* gogo
)
4041 return gogo
->backend()->float_type(this->bits_
);
4044 // The type descriptor for a float type. Float types are always named.
4047 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4049 go_assert(name
!= NULL
|| saw_errors());
4050 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4053 // We should not be asked for the reflection string of a basic type.
4056 Float_type::do_reflection(Gogo
*, std::string
*) const
4058 go_assert(saw_errors());
4061 // Make a floating point type.
4064 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
4066 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
4069 // Make an abstract float type.
4072 Type::make_abstract_float_type()
4074 return Float_type::create_abstract_float_type();
4077 // Look up a float type.
4080 Type::lookup_float_type(const char* name
)
4082 return Float_type::lookup_float_type(name
);
4085 // Class Complex_type.
4087 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
4089 // Create a new complex type. Non-abstract complex types always have
4093 Complex_type::create_complex_type(const char* name
, int bits
,
4094 int runtime_type_kind
)
4096 Complex_type
* complex_type
= new Complex_type(false, bits
,
4098 std::string
sname(name
);
4099 Named_object
* named_object
=
4100 Named_object::make_type(sname
, NULL
, complex_type
,
4101 Linemap::predeclared_location());
4102 Named_type
* named_type
= named_object
->type_value();
4103 std::pair
<Named_complex_types::iterator
, bool> ins
=
4104 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
4106 go_assert(ins
.second
);
4110 // Look up an existing complex type.
4113 Complex_type::lookup_complex_type(const char* name
)
4115 Named_complex_types::const_iterator p
=
4116 Complex_type::named_complex_types
.find(name
);
4117 go_assert(p
!= Complex_type::named_complex_types
.end());
4121 // Create a new abstract complex type.
4124 Complex_type::create_abstract_complex_type()
4126 static Complex_type
* abstract_type
;
4127 if (abstract_type
== NULL
)
4128 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
4129 return abstract_type
;
4132 // Whether this type is identical with T.
4135 Complex_type::is_identical(const Complex_type
*t
) const
4137 if (this->bits_
!= t
->bits_
)
4139 return this->is_abstract_
== t
->is_abstract_
;
4145 Complex_type::do_hash_for_method(Gogo
*) const
4147 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
4150 // Convert to the backend representation.
4153 Complex_type::do_get_backend(Gogo
* gogo
)
4155 return gogo
->backend()->complex_type(this->bits_
);
4158 // The type descriptor for a complex type. Complex types are always
4162 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4164 go_assert(name
!= NULL
|| saw_errors());
4165 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
4168 // We should not be asked for the reflection string of a basic type.
4171 Complex_type::do_reflection(Gogo
*, std::string
*) const
4173 go_assert(saw_errors());
4176 // Make a complex type.
4179 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
4181 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
4184 // Make an abstract complex type.
4187 Type::make_abstract_complex_type()
4189 return Complex_type::create_abstract_complex_type();
4192 // Look up a complex type.
4195 Type::lookup_complex_type(const char* name
)
4197 return Complex_type::lookup_complex_type(name
);
4200 // Class String_type.
4202 // Convert String_type to the backend representation. A string is a
4203 // struct with two fields: a pointer to the characters and a length.
4206 String_type::do_get_backend(Gogo
* gogo
)
4208 static Btype
* backend_string_type
;
4209 if (backend_string_type
== NULL
)
4211 std::vector
<Backend::Btyped_identifier
> fields(2);
4213 Type
* b
= gogo
->lookup_global("byte")->type_value();
4214 Type
* pb
= Type::make_pointer_type(b
);
4216 // We aren't going to get back to this field to finish the
4217 // backend representation, so force it to be finished now.
4218 if (!gogo
->named_types_are_converted())
4220 Btype
* bt
= pb
->get_backend_placeholder(gogo
);
4221 pb
->finish_backend(gogo
, bt
);
4224 fields
[0].name
= "__data";
4225 fields
[0].btype
= pb
->get_backend(gogo
);
4226 fields
[0].location
= Linemap::predeclared_location();
4228 Type
* int_type
= Type::lookup_integer_type("int");
4229 fields
[1].name
= "__length";
4230 fields
[1].btype
= int_type
->get_backend(gogo
);
4231 fields
[1].location
= fields
[0].location
;
4233 backend_string_type
= gogo
->backend()->struct_type(fields
);
4235 return backend_string_type
;
4238 // The type descriptor for the string type.
4241 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4244 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
4247 Named_object
* no
= gogo
->lookup_global("string");
4248 go_assert(no
!= NULL
);
4249 return Type::type_descriptor(gogo
, no
->type_value());
4253 // We should not be asked for the reflection string of a basic type.
4256 String_type::do_reflection(Gogo
*, std::string
* ret
) const
4258 ret
->append("string");
4261 // Make a string type.
4264 Type::make_string_type()
4266 static String_type string_type
;
4267 return &string_type
;
4270 // The named type "string".
4272 static Named_type
* named_string_type
;
4274 // Get the named type "string".
4277 Type::lookup_string_type()
4279 return named_string_type
;
4282 // Make the named type string.
4285 Type::make_named_string_type()
4287 Type
* string_type
= Type::make_string_type();
4288 Named_object
* named_object
=
4289 Named_object::make_type("string", NULL
, string_type
,
4290 Linemap::predeclared_location());
4291 Named_type
* named_type
= named_object
->type_value();
4292 named_string_type
= named_type
;
4296 // The sink type. This is the type of the blank identifier _. Any
4297 // type may be assigned to it.
4299 class Sink_type
: public Type
4308 do_compare_is_identity(Gogo
*)
4312 do_get_backend(Gogo
*)
4313 { go_unreachable(); }
4316 do_type_descriptor(Gogo
*, Named_type
*)
4317 { go_unreachable(); }
4320 do_reflection(Gogo
*, std::string
*) const
4321 { go_unreachable(); }
4324 do_mangled_name(Gogo
*, std::string
*) const
4325 { go_unreachable(); }
4328 // Make the sink type.
4331 Type::make_sink_type()
4333 static Sink_type sink_type
;
4337 // Class Function_type.
4342 Function_type::do_traverse(Traverse
* traverse
)
4344 if (this->receiver_
!= NULL
4345 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
4346 return TRAVERSE_EXIT
;
4347 if (this->parameters_
!= NULL
4348 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
4349 return TRAVERSE_EXIT
;
4350 if (this->results_
!= NULL
4351 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
4352 return TRAVERSE_EXIT
;
4353 return TRAVERSE_CONTINUE
;
4356 // Returns whether T is a valid redeclaration of this type. If this
4357 // returns false, and REASON is not NULL, *REASON may be set to a
4358 // brief explanation of why it returned false.
4361 Function_type::is_valid_redeclaration(const Function_type
* t
,
4362 std::string
* reason
) const
4364 if (!this->is_identical(t
, false, COMPARE_TAGS
, true, reason
))
4367 // A redeclaration of a function is required to use the same names
4368 // for the receiver and parameters.
4369 if (this->receiver() != NULL
4370 && this->receiver()->name() != t
->receiver()->name())
4373 *reason
= "receiver name changed";
4377 const Typed_identifier_list
* parms1
= this->parameters();
4378 const Typed_identifier_list
* parms2
= t
->parameters();
4381 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4382 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4383 p2
!= parms2
->end();
4386 if (p1
->name() != p2
->name())
4389 *reason
= "parameter name changed";
4393 // This is called at parse time, so we may have unknown
4395 Type
* t1
= p1
->type()->forwarded();
4396 Type
* t2
= p2
->type()->forwarded();
4398 && t1
->forward_declaration_type() != NULL
4399 && (t2
->forward_declaration_type() == NULL
4400 || (t1
->forward_declaration_type()->named_object()
4401 != t2
->forward_declaration_type()->named_object())))
4406 const Typed_identifier_list
* results1
= this->results();
4407 const Typed_identifier_list
* results2
= t
->results();
4408 if (results1
!= NULL
)
4410 Typed_identifier_list::const_iterator res1
= results1
->begin();
4411 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4412 res2
!= results2
->end();
4415 if (res1
->name() != res2
->name())
4418 *reason
= "result name changed";
4422 // This is called at parse time, so we may have unknown
4424 Type
* t1
= res1
->type()->forwarded();
4425 Type
* t2
= res2
->type()->forwarded();
4427 && t1
->forward_declaration_type() != NULL
4428 && (t2
->forward_declaration_type() == NULL
4429 || (t1
->forward_declaration_type()->named_object()
4430 != t2
->forward_declaration_type()->named_object())))
4438 // Check whether T is the same as this type.
4441 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
4442 Cmp_tags cmp_tags
, bool errors_are_identical
,
4443 std::string
* reason
) const
4445 if (!ignore_receiver
)
4447 const Typed_identifier
* r1
= this->receiver();
4448 const Typed_identifier
* r2
= t
->receiver();
4449 if ((r1
!= NULL
) != (r2
!= NULL
))
4452 *reason
= _("different receiver types");
4457 if (!Type::are_identical_cmp_tags(r1
->type(), r2
->type(), cmp_tags
,
4458 errors_are_identical
, reason
))
4460 if (reason
!= NULL
&& !reason
->empty())
4461 *reason
= "receiver: " + *reason
;
4467 const Typed_identifier_list
* parms1
= this->parameters();
4468 if (parms1
!= NULL
&& parms1
->empty())
4470 const Typed_identifier_list
* parms2
= t
->parameters();
4471 if (parms2
!= NULL
&& parms2
->empty())
4473 if ((parms1
!= NULL
) != (parms2
!= NULL
))
4476 *reason
= _("different number of parameters");
4481 Typed_identifier_list::const_iterator p1
= parms1
->begin();
4482 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
4483 p2
!= parms2
->end();
4486 if (p1
== parms1
->end())
4489 *reason
= _("different number of parameters");
4493 if (!Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
4494 errors_are_identical
, NULL
))
4497 *reason
= _("different parameter types");
4501 if (p1
!= parms1
->end())
4504 *reason
= _("different number of parameters");
4509 if (this->is_varargs() != t
->is_varargs())
4512 *reason
= _("different varargs");
4516 const Typed_identifier_list
* results1
= this->results();
4517 if (results1
!= NULL
&& results1
->empty())
4519 const Typed_identifier_list
* results2
= t
->results();
4520 if (results2
!= NULL
&& results2
->empty())
4522 if ((results1
!= NULL
) != (results2
!= NULL
))
4525 *reason
= _("different number of results");
4528 if (results1
!= NULL
)
4530 Typed_identifier_list::const_iterator res1
= results1
->begin();
4531 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
4532 res2
!= results2
->end();
4535 if (res1
== results1
->end())
4538 *reason
= _("different number of results");
4542 if (!Type::are_identical_cmp_tags(res1
->type(), res2
->type(),
4543 cmp_tags
, errors_are_identical
,
4547 *reason
= _("different result types");
4551 if (res1
!= results1
->end())
4554 *reason
= _("different number of results");
4565 Function_type::do_hash_for_method(Gogo
* gogo
) const
4567 unsigned int ret
= 0;
4568 // We ignore the receiver type for hash codes, because we need to
4569 // get the same hash code for a method in an interface and a method
4570 // declared for a type. The former will not have a receiver.
4571 if (this->parameters_
!= NULL
)
4574 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
4575 p
!= this->parameters_
->end();
4577 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4579 if (this->results_
!= NULL
)
4582 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
4583 p
!= this->results_
->end();
4585 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
4587 if (this->is_varargs_
)
4593 // Hash result parameters.
4596 Function_type::Results_hash::operator()(const Typed_identifier_list
* t
) const
4598 unsigned int hash
= 0;
4599 for (Typed_identifier_list::const_iterator p
= t
->begin();
4604 hash
= Type::hash_string(p
->name(), hash
);
4605 hash
+= p
->type()->hash_for_method(NULL
);
4610 // Compare result parameters so that can map identical result
4611 // parameters to a single struct type.
4614 Function_type::Results_equal::operator()(const Typed_identifier_list
* a
,
4615 const Typed_identifier_list
* b
) const
4617 if (a
->size() != b
->size())
4619 Typed_identifier_list::const_iterator pa
= a
->begin();
4620 for (Typed_identifier_list::const_iterator pb
= b
->begin();
4624 if (pa
->name() != pb
->name()
4625 || !Type::are_identical(pa
->type(), pb
->type(), true, NULL
))
4631 // Hash from results to a backend struct type.
4633 Function_type::Results_structs
Function_type::results_structs
;
4635 // Get the backend representation for a function type.
4638 Function_type::get_backend_fntype(Gogo
* gogo
)
4640 if (this->fnbtype_
== NULL
)
4642 Backend::Btyped_identifier breceiver
;
4643 if (this->receiver_
!= NULL
)
4645 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
4647 // We always pass the address of the receiver parameter, in
4648 // order to make interface calls work with unknown types.
4649 Type
* rtype
= this->receiver_
->type();
4650 if (rtype
->points_to() == NULL
)
4651 rtype
= Type::make_pointer_type(rtype
);
4652 breceiver
.btype
= rtype
->get_backend(gogo
);
4653 breceiver
.location
= this->receiver_
->location();
4656 std::vector
<Backend::Btyped_identifier
> bparameters
;
4657 if (this->parameters_
!= NULL
)
4659 bparameters
.resize(this->parameters_
->size());
4661 for (Typed_identifier_list::const_iterator p
=
4662 this->parameters_
->begin(); p
!= this->parameters_
->end();
4665 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4666 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
4667 bparameters
[i
].location
= p
->location();
4669 go_assert(i
== bparameters
.size());
4672 std::vector
<Backend::Btyped_identifier
> bresults
;
4673 Btype
* bresult_struct
= NULL
;
4674 if (this->results_
!= NULL
)
4676 bresults
.resize(this->results_
->size());
4678 for (Typed_identifier_list::const_iterator p
=
4679 this->results_
->begin();
4680 p
!= this->results_
->end();
4683 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
4684 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
4685 bresults
[i
].location
= p
->location();
4687 go_assert(i
== bresults
.size());
4689 if (this->results_
->size() > 1)
4691 // Use the same results struct for all functions that
4692 // return the same set of results. This is useful to
4693 // unify calls to interface methods with other calls.
4694 std::pair
<Typed_identifier_list
*, Btype
*> val
;
4695 val
.first
= this->results_
;
4697 std::pair
<Results_structs::iterator
, bool> ins
=
4698 Function_type::results_structs
.insert(val
);
4701 // Build a new struct type.
4702 Struct_field_list
* sfl
= new Struct_field_list
;
4703 for (Typed_identifier_list::const_iterator p
=
4704 this->results_
->begin();
4705 p
!= this->results_
->end();
4708 Typed_identifier tid
= *p
;
4709 if (tid
.name().empty())
4710 tid
= Typed_identifier("UNNAMED", tid
.type(),
4712 sfl
->push_back(Struct_field(tid
));
4714 Struct_type
* st
= Type::make_struct_type(sfl
,
4716 st
->set_is_struct_incomparable();
4717 ins
.first
->second
= st
->get_backend(gogo
);
4719 bresult_struct
= ins
.first
->second
;
4723 this->fnbtype_
= gogo
->backend()->function_type(breceiver
, bparameters
,
4724 bresults
, bresult_struct
,
4729 return this->fnbtype_
;
4732 // Get the backend representation for a Go function type.
4735 Function_type::do_get_backend(Gogo
* gogo
)
4737 // When we do anything with a function value other than call it, it
4738 // is represented as a pointer to a struct whose first field is the
4739 // actual function. So that is what we return as the type of a Go
4742 Location loc
= this->location();
4743 Btype
* struct_type
=
4744 gogo
->backend()->placeholder_struct_type("__go_descriptor", loc
);
4745 Btype
* ptr_struct_type
= gogo
->backend()->pointer_type(struct_type
);
4747 std::vector
<Backend::Btyped_identifier
> fields(1);
4748 fields
[0].name
= "code";
4749 fields
[0].btype
= this->get_backend_fntype(gogo
);
4750 fields
[0].location
= loc
;
4751 if (!gogo
->backend()->set_placeholder_struct_type(struct_type
, fields
))
4752 return gogo
->backend()->error_type();
4753 return ptr_struct_type
;
4756 // The type of a function type descriptor.
4759 Function_type::make_function_type_descriptor_type()
4764 Type
* tdt
= Type::make_type_descriptor_type();
4765 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4767 Type
* bool_type
= Type::lookup_bool_type();
4769 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
4771 Struct_type
* s
= Type::make_builtin_struct_type(4,
4773 "dotdotdot", bool_type
,
4777 ret
= Type::make_builtin_named_type("FuncType", s
);
4783 // The type descriptor for a function type.
4786 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4788 Location bloc
= Linemap::predeclared_location();
4790 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
4792 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
4794 Expression_list
* vals
= new Expression_list();
4797 Struct_field_list::const_iterator p
= fields
->begin();
4798 go_assert(p
->is_field_name("_type"));
4799 vals
->push_back(this->type_descriptor_constructor(gogo
,
4800 RUNTIME_TYPE_KIND_FUNC
,
4804 go_assert(p
->is_field_name("dotdotdot"));
4805 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
4808 go_assert(p
->is_field_name("in"));
4809 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
4810 this->parameters()));
4813 go_assert(p
->is_field_name("out"));
4814 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
4818 go_assert(p
== fields
->end());
4820 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
4823 // Return a composite literal for the parameters or results of a type
4827 Function_type::type_descriptor_params(Type
* params_type
,
4828 const Typed_identifier
* receiver
,
4829 const Typed_identifier_list
* params
)
4831 Location bloc
= Linemap::predeclared_location();
4833 if (receiver
== NULL
&& params
== NULL
)
4834 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
4836 Expression_list
* vals
= new Expression_list();
4837 vals
->reserve((params
== NULL
? 0 : params
->size())
4838 + (receiver
!= NULL
? 1 : 0));
4840 if (receiver
!= NULL
)
4841 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
4845 for (Typed_identifier_list::const_iterator p
= params
->begin();
4848 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
4851 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
4854 // The reflection string.
4857 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4859 // FIXME: Turn this off until we straighten out the type of the
4860 // struct field used in a go statement which calls a method.
4861 // go_assert(this->receiver_ == NULL);
4863 ret
->append("func");
4865 if (this->receiver_
!= NULL
)
4867 ret
->push_back('(');
4868 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
4869 ret
->push_back(')');
4872 ret
->push_back('(');
4873 const Typed_identifier_list
* params
= this->parameters();
4876 bool is_varargs
= this->is_varargs_
;
4877 for (Typed_identifier_list::const_iterator p
= params
->begin();
4881 if (p
!= params
->begin())
4883 if (!is_varargs
|| p
+ 1 != params
->end())
4884 this->append_reflection(p
->type(), gogo
, ret
);
4888 this->append_reflection(p
->type()->array_type()->element_type(),
4893 ret
->push_back(')');
4895 const Typed_identifier_list
* results
= this->results();
4896 if (results
!= NULL
&& !results
->empty())
4898 if (results
->size() == 1)
4899 ret
->push_back(' ');
4902 for (Typed_identifier_list::const_iterator p
= results
->begin();
4903 p
!= results
->end();
4906 if (p
!= results
->begin())
4908 this->append_reflection(p
->type(), gogo
, ret
);
4910 if (results
->size() > 1)
4911 ret
->push_back(')');
4915 // Export a function type.
4918 Function_type::do_export(Export
* exp
) const
4920 // We don't write out the receiver. The only function types which
4921 // should have a receiver are the ones associated with explicitly
4922 // defined methods. For those the receiver type is written out by
4923 // Function::export_func.
4925 exp
->write_c_string("(");
4927 if (this->parameters_
!= NULL
)
4929 bool is_varargs
= this->is_varargs_
;
4930 for (Typed_identifier_list::const_iterator p
=
4931 this->parameters_
->begin();
4932 p
!= this->parameters_
->end();
4938 exp
->write_c_string(", ");
4939 exp
->write_name(p
->name());
4940 exp
->write_c_string(" ");
4941 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
4942 exp
->write_type(p
->type());
4945 exp
->write_c_string("...");
4946 exp
->write_type(p
->type()->array_type()->element_type());
4950 exp
->write_c_string(")");
4952 const Typed_identifier_list
* results
= this->results_
;
4953 if (results
!= NULL
)
4955 exp
->write_c_string(" ");
4956 if (results
->size() == 1 && results
->begin()->name().empty())
4957 exp
->write_type(results
->begin()->type());
4961 exp
->write_c_string("(");
4962 for (Typed_identifier_list::const_iterator p
= results
->begin();
4963 p
!= results
->end();
4969 exp
->write_c_string(", ");
4970 exp
->write_name(p
->name());
4971 exp
->write_c_string(" ");
4972 exp
->write_type(p
->type());
4974 exp
->write_c_string(")");
4979 // Import a function type.
4982 Function_type::do_import(Import
* imp
)
4984 imp
->require_c_string("(");
4985 Typed_identifier_list
* parameters
;
4986 bool is_varargs
= false;
4987 if (imp
->peek_char() == ')')
4991 parameters
= new Typed_identifier_list();
4994 std::string name
= imp
->read_name();
4995 imp
->require_c_string(" ");
4997 if (imp
->match_c_string("..."))
5003 Type
* ptype
= imp
->read_type();
5005 ptype
= Type::make_array_type(ptype
, NULL
);
5006 parameters
->push_back(Typed_identifier(name
, ptype
,
5008 if (imp
->peek_char() != ',')
5010 go_assert(!is_varargs
);
5011 imp
->require_c_string(", ");
5014 imp
->require_c_string(")");
5016 Typed_identifier_list
* results
;
5017 if (imp
->peek_char() != ' ')
5022 results
= new Typed_identifier_list
;
5023 if (imp
->peek_char() != '(')
5025 Type
* rtype
= imp
->read_type();
5026 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
5033 std::string name
= imp
->read_name();
5034 imp
->require_c_string(" ");
5035 Type
* rtype
= imp
->read_type();
5036 results
->push_back(Typed_identifier(name
, rtype
,
5038 if (imp
->peek_char() != ',')
5040 imp
->require_c_string(", ");
5042 imp
->require_c_string(")");
5046 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
5049 ret
->set_is_varargs();
5053 // Make a copy of a function type without a receiver.
5056 Function_type::copy_without_receiver() const
5058 go_assert(this->is_method());
5059 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
5062 if (this->is_varargs())
5063 ret
->set_is_varargs();
5064 if (this->is_builtin())
5065 ret
->set_is_builtin();
5069 // Make a copy of a function type with a receiver.
5072 Function_type::copy_with_receiver(Type
* receiver_type
) const
5074 go_assert(!this->is_method());
5075 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
5077 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
5080 if (this->is_varargs_
)
5081 ret
->set_is_varargs();
5085 // Make a copy of a function type with the receiver as the first
5089 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver
) const
5091 go_assert(this->is_method());
5092 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5093 Type
* rtype
= this->receiver_
->type();
5094 if (want_pointer_receiver
)
5095 rtype
= Type::make_pointer_type(rtype
);
5096 Typed_identifier
receiver(this->receiver_
->name(), rtype
,
5097 this->receiver_
->location());
5098 new_params
->push_back(receiver
);
5099 const Typed_identifier_list
* orig_params
= this->parameters_
;
5100 if (orig_params
!= NULL
&& !orig_params
->empty())
5102 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5103 p
!= orig_params
->end();
5105 new_params
->push_back(*p
);
5107 return Type::make_function_type(NULL
, new_params
, this->results_
,
5111 // Make a copy of a function type ignoring any receiver and adding a
5112 // closure parameter.
5115 Function_type::copy_with_names() const
5117 Typed_identifier_list
* new_params
= new Typed_identifier_list();
5118 const Typed_identifier_list
* orig_params
= this->parameters_
;
5119 if (orig_params
!= NULL
&& !orig_params
->empty())
5123 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
5124 p
!= orig_params
->end();
5127 snprintf(buf
, sizeof buf
, "pt.%u", count
);
5129 new_params
->push_back(Typed_identifier(buf
, p
->type(),
5134 const Typed_identifier_list
* orig_results
= this->results_
;
5135 Typed_identifier_list
* new_results
;
5136 if (orig_results
== NULL
|| orig_results
->empty())
5140 new_results
= new Typed_identifier_list();
5141 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
5142 p
!= orig_results
->end();
5144 new_results
->push_back(Typed_identifier("", p
->type(),
5148 return Type::make_function_type(NULL
, new_params
, new_results
,
5152 // Make a function type.
5155 Type::make_function_type(Typed_identifier
* receiver
,
5156 Typed_identifier_list
* parameters
,
5157 Typed_identifier_list
* results
,
5160 return new Function_type(receiver
, parameters
, results
, location
);
5163 // Make a backend function type.
5165 Backend_function_type
*
5166 Type::make_backend_function_type(Typed_identifier
* receiver
,
5167 Typed_identifier_list
* parameters
,
5168 Typed_identifier_list
* results
,
5171 return new Backend_function_type(receiver
, parameters
, results
, location
);
5174 // Class Pointer_type.
5179 Pointer_type::do_traverse(Traverse
* traverse
)
5181 return Type::traverse(this->to_type_
, traverse
);
5187 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
5189 return this->to_type_
->hash_for_method(gogo
) << 4;
5192 // Get the backend representation for a pointer type.
5195 Pointer_type::do_get_backend(Gogo
* gogo
)
5197 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
5198 return gogo
->backend()->pointer_type(to_btype
);
5201 // The type of a pointer type descriptor.
5204 Pointer_type::make_pointer_type_descriptor_type()
5209 Type
* tdt
= Type::make_type_descriptor_type();
5210 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5212 Struct_type
* s
= Type::make_builtin_struct_type(2,
5216 ret
= Type::make_builtin_named_type("PtrType", s
);
5222 // The type descriptor for a pointer type.
5225 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5227 if (this->is_unsafe_pointer_type())
5229 go_assert(name
!= NULL
);
5230 return this->plain_type_descriptor(gogo
,
5231 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
5236 Location bloc
= Linemap::predeclared_location();
5238 const Methods
* methods
;
5239 Type
* deref
= this->points_to();
5240 if (deref
->named_type() != NULL
)
5241 methods
= deref
->named_type()->methods();
5242 else if (deref
->struct_type() != NULL
)
5243 methods
= deref
->struct_type()->methods();
5247 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
5249 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
5251 Expression_list
* vals
= new Expression_list();
5254 Struct_field_list::const_iterator p
= fields
->begin();
5255 go_assert(p
->is_field_name("_type"));
5256 vals
->push_back(this->type_descriptor_constructor(gogo
,
5257 RUNTIME_TYPE_KIND_PTR
,
5258 name
, methods
, false));
5261 go_assert(p
->is_field_name("elem"));
5262 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
5264 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
5268 // Reflection string.
5271 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5273 ret
->push_back('*');
5274 this->append_reflection(this->to_type_
, gogo
, ret
);
5280 Pointer_type::do_export(Export
* exp
) const
5282 exp
->write_c_string("*");
5283 if (this->is_unsafe_pointer_type())
5284 exp
->write_c_string("any");
5286 exp
->write_type(this->to_type_
);
5292 Pointer_type::do_import(Import
* imp
)
5294 imp
->require_c_string("*");
5295 if (imp
->match_c_string("any"))
5298 return Type::make_pointer_type(Type::make_void_type());
5300 Type
* to
= imp
->read_type();
5301 return Type::make_pointer_type(to
);
5304 // Cache of pointer types. Key is "to" type, value is pointer type
5305 // that points to key.
5307 Type::Pointer_type_table
Type::pointer_types
;
5309 // A list of placeholder pointer types. We keep this so we can ensure
5310 // they are finalized.
5312 std::vector
<Pointer_type
*> Type::placeholder_pointers
;
5314 // Make a pointer type.
5317 Type::make_pointer_type(Type
* to_type
)
5319 Pointer_type_table::const_iterator p
= pointer_types
.find(to_type
);
5320 if (p
!= pointer_types
.end())
5322 Pointer_type
* ret
= new Pointer_type(to_type
);
5323 pointer_types
[to_type
] = ret
;
5327 // This helper is invoked immediately after named types have been
5328 // converted, to clean up any unresolved pointer types remaining in
5329 // the pointer type cache.
5331 // The motivation for this routine: occasionally the compiler creates
5332 // some specific pointer type as part of a lowering operation (ex:
5333 // pointer-to-void), then Type::backend_type_size() is invoked on the
5334 // type (which creates a Btype placeholder for it), that placeholder
5335 // passed somewhere along the line to the back end, but since there is
5336 // no reference to the type in user code, there is never a call to
5337 // Type::finish_backend for the type (hence the Btype remains as an
5338 // unresolved placeholder). Calling this routine will clean up such
5342 Type::finish_pointer_types(Gogo
* gogo
)
5344 // We don't use begin() and end() because it is possible to add new
5345 // placeholder pointer types as we finalized existing ones.
5346 for (size_t i
= 0; i
< Type::placeholder_pointers
.size(); i
++)
5348 Pointer_type
* pt
= Type::placeholder_pointers
[i
];
5349 Type_btypes::iterator tbti
= Type::type_btypes
.find(pt
);
5350 if (tbti
!= Type::type_btypes
.end() && tbti
->second
.is_placeholder
)
5352 pt
->finish_backend(gogo
, tbti
->second
.btype
);
5353 tbti
->second
.is_placeholder
= false;
5360 // Get the backend representation of a nil type. FIXME: Is this ever
5364 Nil_type::do_get_backend(Gogo
* gogo
)
5366 return gogo
->backend()->pointer_type(gogo
->backend()->void_type());
5369 // Make the nil type.
5372 Type::make_nil_type()
5374 static Nil_type singleton_nil_type
;
5375 return &singleton_nil_type
;
5378 // The type of a function call which returns multiple values. This is
5379 // really a struct, but we don't want to confuse a function call which
5380 // returns a struct with a function call which returns multiple
5383 class Call_multiple_result_type
: public Type
5386 Call_multiple_result_type(Call_expression
* call
)
5387 : Type(TYPE_CALL_MULTIPLE_RESULT
),
5393 do_has_pointer() const
5397 do_compare_is_identity(Gogo
*)
5401 do_get_backend(Gogo
* gogo
)
5403 go_assert(saw_errors());
5404 return gogo
->backend()->error_type();
5408 do_type_descriptor(Gogo
*, Named_type
*)
5410 go_assert(saw_errors());
5411 return Expression::make_error(Linemap::unknown_location());
5415 do_reflection(Gogo
*, std::string
*) const
5416 { go_assert(saw_errors()); }
5419 do_mangled_name(Gogo
*, std::string
*) const
5420 { go_assert(saw_errors()); }
5423 // The expression being called.
5424 Call_expression
* call_
;
5427 // Make a call result type.
5430 Type::make_call_multiple_result_type(Call_expression
* call
)
5432 return new Call_multiple_result_type(call
);
5435 // Class Struct_field.
5437 // Get the name of a field.
5440 Struct_field::field_name() const
5442 const std::string
& name(this->typed_identifier_
.name());
5447 // This is called during parsing, before anything is lowered, so
5448 // we have to be pretty careful to avoid dereferencing an
5449 // unknown type name.
5450 Type
* t
= this->typed_identifier_
.type();
5452 if (t
->classification() == Type::TYPE_POINTER
)
5455 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
5456 dt
= ptype
->points_to();
5458 if (dt
->forward_declaration_type() != NULL
)
5459 return dt
->forward_declaration_type()->name();
5460 else if (dt
->named_type() != NULL
)
5462 // Note that this can be an alias name.
5463 return dt
->named_type()->name();
5465 else if (t
->is_error_type() || dt
->is_error_type())
5467 static const std::string error_string
= "*error*";
5468 return error_string
;
5472 // Avoid crashing in the erroneous case where T is named but
5475 if (t
->forward_declaration_type() != NULL
)
5476 return t
->forward_declaration_type()->name();
5477 else if (t
->named_type() != NULL
)
5478 return t
->named_type()->name();
5485 // Return whether this field is named NAME.
5488 Struct_field::is_field_name(const std::string
& name
) const
5490 const std::string
& me(this->typed_identifier_
.name());
5495 Type
* t
= this->typed_identifier_
.type();
5496 if (t
->points_to() != NULL
)
5498 Named_type
* nt
= t
->named_type();
5499 if (nt
!= NULL
&& nt
->name() == name
)
5502 // This is a horrible hack caused by the fact that we don't pack
5503 // the names of builtin types. FIXME.
5504 if (!this->is_imported_
5507 && nt
->name() == Gogo::unpack_hidden_name(name
))
5514 // Return whether this field is an unexported field named NAME.
5517 Struct_field::is_unexported_field_name(Gogo
* gogo
,
5518 const std::string
& name
) const
5520 const std::string
& field_name(this->field_name());
5521 if (Gogo::is_hidden_name(field_name
)
5522 && name
== Gogo::unpack_hidden_name(field_name
)
5523 && gogo
->pack_hidden_name(name
, false) != field_name
)
5526 // Check for the name of a builtin type. This is like the test in
5527 // is_field_name, only there we return false if this->is_imported_,
5528 // and here we return true.
5529 if (this->is_imported_
&& this->is_anonymous())
5531 Type
* t
= this->typed_identifier_
.type();
5532 if (t
->points_to() != NULL
)
5534 Named_type
* nt
= t
->named_type();
5537 && nt
->name() == Gogo::unpack_hidden_name(name
))
5544 // Return whether this field is an embedded built-in type.
5547 Struct_field::is_embedded_builtin(Gogo
* gogo
) const
5549 const std::string
& name(this->field_name());
5550 // We know that a field is an embedded type if it is anonymous.
5551 // We can decide if it is a built-in type by checking to see if it is
5552 // registered globally under the field's name.
5553 // This allows us to distinguish between embedded built-in types and
5554 // embedded types that are aliases to built-in types.
5555 return (this->is_anonymous()
5556 && !Gogo::is_hidden_name(name
)
5557 && gogo
->lookup_global(name
.c_str()) != NULL
);
5560 // Class Struct_type.
5562 // A hash table used to find identical unnamed structs so that they
5563 // share method tables.
5565 Struct_type::Identical_structs
Struct_type::identical_structs
;
5567 // A hash table used to merge method sets for identical unnamed
5570 Struct_type::Struct_method_tables
Struct_type::struct_method_tables
;
5575 Struct_type::do_traverse(Traverse
* traverse
)
5577 Struct_field_list
* fields
= this->fields_
;
5580 for (Struct_field_list::iterator p
= fields
->begin();
5584 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
5585 return TRAVERSE_EXIT
;
5588 return TRAVERSE_CONTINUE
;
5591 // Verify that the struct type is complete and valid.
5594 Struct_type::do_verify()
5596 Struct_field_list
* fields
= this->fields_
;
5599 for (Struct_field_list::iterator p
= fields
->begin();
5603 Type
* t
= p
->type();
5604 if (p
->is_anonymous())
5606 if ((t
->named_type() != NULL
&& t
->points_to() != NULL
)
5607 || (t
->named_type() == NULL
&& t
->points_to() != NULL
5608 && t
->points_to()->points_to() != NULL
))
5610 go_error_at(p
->location(), "embedded type may not be a pointer");
5611 p
->set_type(Type::make_error_type());
5613 else if (t
->points_to() != NULL
5614 && t
->points_to()->interface_type() != NULL
)
5616 go_error_at(p
->location(),
5617 "embedded type may not be pointer to interface");
5618 p
->set_type(Type::make_error_type());
5625 // Whether this contains a pointer.
5628 Struct_type::do_has_pointer() const
5630 const Struct_field_list
* fields
= this->fields();
5633 for (Struct_field_list::const_iterator p
= fields
->begin();
5637 if (p
->type()->has_pointer())
5643 // Whether this type is identical to T.
5646 Struct_type::is_identical(const Struct_type
* t
, Cmp_tags cmp_tags
,
5647 bool errors_are_identical
) const
5649 if (this->is_struct_incomparable_
!= t
->is_struct_incomparable_
)
5651 const Struct_field_list
* fields1
= this->fields();
5652 const Struct_field_list
* fields2
= t
->fields();
5653 if (fields1
== NULL
|| fields2
== NULL
)
5654 return fields1
== fields2
;
5655 Struct_field_list::const_iterator pf2
= fields2
->begin();
5656 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
5657 pf1
!= fields1
->end();
5660 if (pf2
== fields2
->end())
5662 if (pf1
->field_name() != pf2
->field_name())
5664 if (pf1
->is_anonymous() != pf2
->is_anonymous()
5665 || !Type::are_identical_cmp_tags(pf1
->type(), pf2
->type(), cmp_tags
,
5666 errors_are_identical
, NULL
))
5668 if (cmp_tags
== COMPARE_TAGS
)
5670 if (!pf1
->has_tag())
5677 if (!pf2
->has_tag())
5679 if (pf1
->tag() != pf2
->tag())
5684 if (pf2
!= fields2
->end())
5689 // Whether comparisons of this struct type are simple identity
5693 Struct_type::do_compare_is_identity(Gogo
* gogo
)
5695 const Struct_field_list
* fields
= this->fields_
;
5699 for (Struct_field_list::const_iterator pf
= fields
->begin();
5700 pf
!= fields
->end();
5703 if (Gogo::is_sink_name(pf
->field_name()))
5706 if (!pf
->type()->compare_is_identity(gogo
))
5709 int64_t field_align
;
5710 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
5712 if ((offset
& (field_align
- 1)) != 0)
5714 // This struct has padding. We don't guarantee that that
5715 // padding is zero-initialized for a stack variable, so we
5716 // can't use memcmp to compare struct values.
5721 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
5723 offset
+= field_size
;
5726 int64_t struct_size
;
5727 if (!this->backend_type_size(gogo
, &struct_size
))
5729 if (offset
!= struct_size
)
5731 // Trailing padding may not be zero when on the stack.
5738 // Return whether this struct type is reflexive--whether a value of
5739 // this type is always equal to itself.
5742 Struct_type::do_is_reflexive()
5744 const Struct_field_list
* fields
= this->fields_
;
5747 for (Struct_field_list::const_iterator pf
= fields
->begin();
5748 pf
!= fields
->end();
5751 if (!pf
->type()->is_reflexive())
5757 // Return whether this struct type needs a key update when used as a
5761 Struct_type::do_needs_key_update()
5763 const Struct_field_list
* fields
= this->fields_
;
5766 for (Struct_field_list::const_iterator pf
= fields
->begin();
5767 pf
!= fields
->end();
5770 if (pf
->type()->needs_key_update())
5776 // Return whether this struct type is permitted to be in the heap.
5779 Struct_type::do_in_heap()
5781 const Struct_field_list
* fields
= this->fields_
;
5784 for (Struct_field_list::const_iterator pf
= fields
->begin();
5785 pf
!= fields
->end();
5788 if (!pf
->type()->in_heap())
5794 // Build identity and hash functions for this struct.
5799 Struct_type::do_hash_for_method(Gogo
* gogo
) const
5801 unsigned int ret
= 0;
5802 if (this->fields() != NULL
)
5804 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
5805 pf
!= this->fields()->end();
5807 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
5810 if (this->is_struct_incomparable_
)
5815 // Find the local field NAME.
5818 Struct_type::find_local_field(const std::string
& name
,
5819 unsigned int *pindex
) const
5821 const Struct_field_list
* fields
= this->fields_
;
5825 for (Struct_field_list::const_iterator pf
= fields
->begin();
5826 pf
!= fields
->end();
5829 if (pf
->is_field_name(name
))
5839 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
5841 Field_reference_expression
*
5842 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
5843 Location location
) const
5846 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
5850 // Return an expression for a field, along with the depth at which it
5853 Field_reference_expression
*
5854 Struct_type::field_reference_depth(Expression
* struct_expr
,
5855 const std::string
& name
,
5857 Saw_named_type
* saw
,
5858 unsigned int* depth
) const
5860 const Struct_field_list
* fields
= this->fields_
;
5864 // Look for a field with this name.
5866 for (Struct_field_list::const_iterator pf
= fields
->begin();
5867 pf
!= fields
->end();
5870 if (pf
->is_field_name(name
))
5873 return Expression::make_field_reference(struct_expr
, i
, location
);
5877 // Look for an anonymous field which contains a field with this
5879 unsigned int found_depth
= 0;
5880 Field_reference_expression
* ret
= NULL
;
5882 for (Struct_field_list::const_iterator pf
= fields
->begin();
5883 pf
!= fields
->end();
5886 if (!pf
->is_anonymous())
5889 Struct_type
* st
= pf
->type()->deref()->struct_type();
5893 Saw_named_type
* hold_saw
= saw
;
5894 Saw_named_type saw_here
;
5895 Named_type
* nt
= pf
->type()->named_type();
5897 nt
= pf
->type()->deref()->named_type();
5901 for (q
= saw
; q
!= NULL
; q
= q
->next
)
5905 // If this is an error, it will be reported
5912 saw_here
.next
= saw
;
5917 // Look for a reference using a NULL struct expression. If we
5918 // find one, fill in the struct expression with a reference to
5920 unsigned int subdepth
;
5921 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
5931 if (ret
== NULL
|| subdepth
< found_depth
)
5936 found_depth
= subdepth
;
5937 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
5939 if (pf
->type()->points_to() != NULL
)
5940 here
= Expression::make_dereference(here
,
5941 Expression::NIL_CHECK_DEFAULT
,
5943 while (sub
->expr() != NULL
)
5945 sub
= sub
->expr()->deref()->field_reference_expression();
5946 go_assert(sub
!= NULL
);
5948 sub
->set_struct_expression(here
);
5949 sub
->set_implicit(true);
5951 else if (subdepth
> found_depth
)
5955 // We do not handle ambiguity here--it should be handled by
5956 // Type::bind_field_or_method.
5964 *depth
= found_depth
+ 1;
5969 // Return the total number of fields, including embedded fields.
5972 Struct_type::total_field_count() const
5974 if (this->fields_
== NULL
)
5976 unsigned int ret
= 0;
5977 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
5978 pf
!= this->fields_
->end();
5981 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
5984 ret
+= pf
->type()->struct_type()->total_field_count();
5989 // Return whether NAME is an unexported field, for better error reporting.
5992 Struct_type::is_unexported_local_field(Gogo
* gogo
,
5993 const std::string
& name
) const
5995 const Struct_field_list
* fields
= this->fields_
;
5998 for (Struct_field_list::const_iterator pf
= fields
->begin();
5999 pf
!= fields
->end();
6001 if (pf
->is_unexported_field_name(gogo
, name
))
6007 // Finalize the methods of an unnamed struct.
6010 Struct_type::finalize_methods(Gogo
* gogo
)
6012 if (this->all_methods_
!= NULL
)
6015 // It is possible to have multiple identical structs that have
6016 // methods. We want them to share method tables. Otherwise we will
6017 // emit identical methods more than once, which is bad since they
6018 // will even have the same names.
6019 std::pair
<Identical_structs::iterator
, bool> ins
=
6020 Struct_type::identical_structs
.insert(std::make_pair(this, this));
6023 // An identical struct was already entered into the hash table.
6024 // Note that finalize_methods is, fortunately, not recursive.
6025 this->all_methods_
= ins
.first
->second
->all_methods_
;
6029 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6032 // Return the method NAME, or NULL if there isn't one or if it is
6033 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6037 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6039 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6042 // Return a pointer to the interface method table for this type for
6043 // the interface INTERFACE. IS_POINTER is true if this is for a
6047 Struct_type::interface_method_table(Interface_type
* interface
,
6050 std::pair
<Struct_type
*, Struct_type::Struct_method_table_pair
*>
6052 std::pair
<Struct_type::Struct_method_tables::iterator
, bool> ins
=
6053 Struct_type::struct_method_tables
.insert(val
);
6055 Struct_method_table_pair
* smtp
;
6057 smtp
= ins
.first
->second
;
6060 smtp
= new Struct_method_table_pair();
6062 smtp
->second
= NULL
;
6063 ins
.first
->second
= smtp
;
6066 return Type::interface_method_table(this, interface
, is_pointer
,
6067 &smtp
->first
, &smtp
->second
);
6070 // Convert struct fields to the backend representation. This is not
6071 // declared in types.h so that types.h doesn't have to #include
6075 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
6076 bool use_placeholder
,
6077 std::vector
<Backend::Btyped_identifier
>* bfields
)
6079 bfields
->resize(fields
->size());
6081 for (Struct_field_list::const_iterator p
= fields
->begin();
6085 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
6086 (*bfields
)[i
].btype
= (use_placeholder
6087 ? p
->type()->get_backend_placeholder(gogo
)
6088 : p
->type()->get_backend(gogo
));
6089 (*bfields
)[i
].location
= p
->location();
6091 go_assert(i
== fields
->size());
6094 // Get the backend representation for a struct type.
6097 Struct_type::do_get_backend(Gogo
* gogo
)
6099 std::vector
<Backend::Btyped_identifier
> bfields
;
6100 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
6101 return gogo
->backend()->struct_type(bfields
);
6104 // Finish the backend representation of the fields of a struct.
6107 Struct_type::finish_backend_fields(Gogo
* gogo
)
6109 const Struct_field_list
* fields
= this->fields_
;
6112 for (Struct_field_list::const_iterator p
= fields
->begin();
6115 p
->type()->get_backend(gogo
);
6119 // The type of a struct type descriptor.
6122 Struct_type::make_struct_type_descriptor_type()
6127 Type
* tdt
= Type::make_type_descriptor_type();
6128 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6130 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6131 Type
* string_type
= Type::lookup_string_type();
6132 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6135 Type::make_builtin_struct_type(5,
6136 "name", pointer_string_type
,
6137 "pkgPath", pointer_string_type
,
6139 "tag", pointer_string_type
,
6140 "offsetAnon", uintptr_type
);
6141 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
6143 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
6145 Struct_type
* s
= Type::make_builtin_struct_type(2,
6147 "fields", slice_type
);
6149 ret
= Type::make_builtin_named_type("StructType", s
);
6155 // Build a type descriptor for a struct type.
6158 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6160 Location bloc
= Linemap::predeclared_location();
6162 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
6164 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
6166 Expression_list
* vals
= new Expression_list();
6169 const Methods
* methods
= this->methods();
6170 // A named struct should not have methods--the methods should attach
6171 // to the named type.
6172 go_assert(methods
== NULL
|| name
== NULL
);
6174 Struct_field_list::const_iterator ps
= fields
->begin();
6175 go_assert(ps
->is_field_name("_type"));
6176 vals
->push_back(this->type_descriptor_constructor(gogo
,
6177 RUNTIME_TYPE_KIND_STRUCT
,
6178 name
, methods
, true));
6181 go_assert(ps
->is_field_name("fields"));
6183 Expression_list
* elements
= new Expression_list();
6184 elements
->reserve(this->fields_
->size());
6185 Type
* element_type
= ps
->type()->array_type()->element_type();
6186 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
6187 pf
!= this->fields_
->end();
6190 const Struct_field_list
* f
= element_type
->struct_type()->fields();
6192 Expression_list
* fvals
= new Expression_list();
6195 Struct_field_list::const_iterator q
= f
->begin();
6196 go_assert(q
->is_field_name("name"));
6197 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
6198 Expression
* s
= Expression::make_string(n
, bloc
);
6199 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6202 go_assert(q
->is_field_name("pkgPath"));
6203 bool is_embedded_builtin
= pf
->is_embedded_builtin(gogo
);
6204 if (!Gogo::is_hidden_name(pf
->field_name()) && !is_embedded_builtin
)
6205 fvals
->push_back(Expression::make_nil(bloc
));
6209 if (is_embedded_builtin
)
6210 n
= gogo
->package_name();
6212 n
= Gogo::hidden_name_pkgpath(pf
->field_name());
6213 Expression
* s
= Expression::make_string(n
, bloc
);
6214 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6218 go_assert(q
->is_field_name("typ"));
6219 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
6222 go_assert(q
->is_field_name("tag"));
6224 fvals
->push_back(Expression::make_nil(bloc
));
6227 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
6228 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
6232 go_assert(q
->is_field_name("offsetAnon"));
6233 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6234 Expression
* o
= Expression::make_struct_field_offset(this, &*pf
);
6235 Expression
* one
= Expression::make_integer_ul(1, uintptr_type
, bloc
);
6236 o
= Expression::make_binary(OPERATOR_LSHIFT
, o
, one
, bloc
);
6237 int av
= pf
->is_anonymous() ? 1 : 0;
6238 Expression
* anon
= Expression::make_integer_ul(av
, uintptr_type
, bloc
);
6239 o
= Expression::make_binary(OPERATOR_OR
, o
, anon
, bloc
);
6240 fvals
->push_back(o
);
6242 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
6244 elements
->push_back(v
);
6247 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
6250 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
6253 // Write the hash function for a struct which can not use the identity
6257 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
6258 Function_type
* hash_fntype
,
6259 Function_type
* equal_fntype
)
6261 Location bloc
= Linemap::predeclared_location();
6263 // The pointer to the struct that we are going to hash. This is an
6264 // argument to the hash function we are implementing here.
6265 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
6266 go_assert(key_arg
!= NULL
);
6267 Type
* key_arg_type
= key_arg
->var_value()->type();
6269 // The seed argument to the hash function.
6270 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
6271 go_assert(seed_arg
!= NULL
);
6273 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6275 // Make a temporary to hold the return value, initialized to the seed.
6276 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
6277 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
6279 gogo
->add_statement(retval
);
6281 // Make a temporary to hold the key as a uintptr.
6282 ref
= Expression::make_var_reference(key_arg
, bloc
);
6283 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
6284 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
6286 gogo
->add_statement(key
);
6288 // Loop over the struct fields.
6289 const Struct_field_list
* fields
= this->fields_
;
6290 for (Struct_field_list::const_iterator pf
= fields
->begin();
6291 pf
!= fields
->end();
6294 if (Gogo::is_sink_name(pf
->field_name()))
6297 // Get a pointer to the value of this field.
6298 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
6299 ref
= Expression::make_temporary_reference(key
, bloc
);
6300 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
6302 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
6304 // Get the hash function to use for the type of this field.
6305 Named_object
* hash_fn
;
6306 Named_object
* equal_fn
;
6307 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
6308 equal_fntype
, &hash_fn
, &equal_fn
);
6310 // Call the hash function for the field, passing retval as the seed.
6311 ref
= Expression::make_temporary_reference(retval
, bloc
);
6312 Expression_list
* args
= new Expression_list();
6313 args
->push_back(subkey
);
6314 args
->push_back(ref
);
6315 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
6316 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
6318 // Set retval to the result.
6319 Temporary_reference_expression
* tref
=
6320 Expression::make_temporary_reference(retval
, bloc
);
6321 tref
->set_is_lvalue();
6322 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
6323 gogo
->add_statement(s
);
6326 // Return retval to the caller of the hash function.
6327 Expression_list
* vals
= new Expression_list();
6328 ref
= Expression::make_temporary_reference(retval
, bloc
);
6329 vals
->push_back(ref
);
6330 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6331 gogo
->add_statement(s
);
6334 // Write the equality function for a struct which can not use the
6335 // identity function.
6338 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
6340 Location bloc
= Linemap::predeclared_location();
6342 // The pointers to the structs we are going to compare.
6343 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
6344 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
6345 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
6347 // Build temporaries with the right types.
6348 Type
* pt
= Type::make_pointer_type(name
!= NULL
6349 ? static_cast<Type
*>(name
)
6350 : static_cast<Type
*>(this));
6352 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
6353 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6354 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
6355 gogo
->add_statement(p1
);
6357 ref
= Expression::make_var_reference(key2_arg
, bloc
);
6358 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6359 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
6360 gogo
->add_statement(p2
);
6362 const Struct_field_list
* fields
= this->fields_
;
6363 unsigned int field_index
= 0;
6364 for (Struct_field_list::const_iterator pf
= fields
->begin();
6365 pf
!= fields
->end();
6366 ++pf
, ++field_index
)
6368 if (Gogo::is_sink_name(pf
->field_name()))
6371 // Compare one field in both P1 and P2.
6372 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
6373 f1
= Expression::make_dereference(f1
, Expression::NIL_CHECK_DEFAULT
,
6375 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
6377 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
6378 f2
= Expression::make_dereference(f2
, Expression::NIL_CHECK_DEFAULT
,
6380 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
6382 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
6384 // If the values are not equal, return false.
6385 gogo
->start_block(bloc
);
6386 Expression_list
* vals
= new Expression_list();
6387 vals
->push_back(Expression::make_boolean(false, bloc
));
6388 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6389 gogo
->add_statement(s
);
6390 Block
* then_block
= gogo
->finish_block(bloc
);
6392 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
6393 gogo
->add_statement(s
);
6396 // All the fields are equal, so return true.
6397 Expression_list
* vals
= new Expression_list();
6398 vals
->push_back(Expression::make_boolean(true, bloc
));
6399 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6400 gogo
->add_statement(s
);
6403 // Reflection string.
6406 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6408 ret
->append("struct {");
6410 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
6411 p
!= this->fields_
->end();
6414 if (p
!= this->fields_
->begin())
6415 ret
->push_back(';');
6416 ret
->push_back(' ');
6417 if (p
->is_anonymous())
6418 ret
->push_back('?');
6420 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
6421 ret
->push_back(' ');
6422 if (p
->is_anonymous()
6423 && p
->type()->named_type() != NULL
6424 && p
->type()->named_type()->is_alias())
6425 p
->type()->named_type()->append_reflection_type_name(gogo
, true, ret
);
6427 this->append_reflection(p
->type(), gogo
, ret
);
6431 const std::string
& tag(p
->tag());
6433 for (std::string::const_iterator p
= tag
.begin();
6438 ret
->append("\\x00");
6439 else if (*p
== '\n')
6441 else if (*p
== '\t')
6444 ret
->append("\\\"");
6445 else if (*p
== '\\')
6446 ret
->append("\\\\");
6450 ret
->push_back('"');
6454 if (!this->fields_
->empty())
6455 ret
->push_back(' ');
6457 ret
->push_back('}');
6460 // If the offset of field INDEX in the backend implementation can be
6461 // determined, set *POFFSET to the offset in bytes and return true.
6462 // Otherwise, return false.
6465 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
6468 if (!this->is_backend_type_size_known(gogo
))
6470 Btype
* bt
= this->get_backend_placeholder(gogo
);
6471 *poffset
= gogo
->backend()->type_field_offset(bt
, index
);
6478 Struct_type::do_export(Export
* exp
) const
6480 exp
->write_c_string("struct { ");
6481 const Struct_field_list
* fields
= this->fields_
;
6482 go_assert(fields
!= NULL
);
6483 for (Struct_field_list::const_iterator p
= fields
->begin();
6487 if (p
->is_anonymous())
6488 exp
->write_string("? ");
6491 exp
->write_string(p
->field_name());
6492 exp
->write_c_string(" ");
6494 exp
->write_type(p
->type());
6498 exp
->write_c_string(" ");
6500 Expression::make_string(p
->tag(), Linemap::predeclared_location());
6501 expr
->export_expression(exp
);
6505 exp
->write_c_string("; ");
6507 exp
->write_c_string("}");
6513 Struct_type::do_import(Import
* imp
)
6515 imp
->require_c_string("struct { ");
6516 Struct_field_list
* fields
= new Struct_field_list
;
6517 if (imp
->peek_char() != '}')
6522 if (imp
->match_c_string("? "))
6526 name
= imp
->read_identifier();
6527 imp
->require_c_string(" ");
6529 Type
* ftype
= imp
->read_type();
6531 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
6532 sf
.set_is_imported();
6534 if (imp
->peek_char() == ' ')
6537 Expression
* expr
= Expression::import_expression(imp
);
6538 String_expression
* sexpr
= expr
->string_expression();
6539 go_assert(sexpr
!= NULL
);
6540 sf
.set_tag(sexpr
->val());
6544 imp
->require_c_string("; ");
6545 fields
->push_back(sf
);
6546 if (imp
->peek_char() == '}')
6550 imp
->require_c_string("}");
6552 return Type::make_struct_type(fields
, imp
->location());
6555 // Whether we can write this struct type to a C header file.
6556 // We can't if any of the fields are structs defined in a different package.
6559 Struct_type::can_write_to_c_header(
6560 std::vector
<const Named_object
*>* requires
,
6561 std::vector
<const Named_object
*>* declare
) const
6563 const Struct_field_list
* fields
= this->fields_
;
6564 if (fields
== NULL
|| fields
->empty())
6567 for (Struct_field_list::const_iterator p
= fields
->begin();
6571 if (p
->is_anonymous())
6573 if (!this->can_write_type_to_c_header(p
->type(), requires
, declare
))
6575 if (Gogo::message_name(p
->field_name()) == "_")
6583 // Whether we can write the type T to a C header file.
6586 Struct_type::can_write_type_to_c_header(
6588 std::vector
<const Named_object
*>* requires
,
6589 std::vector
<const Named_object
*>* declare
) const
6592 switch (t
->classification())
6607 case TYPE_INTERFACE
:
6611 // Don't try to handle a pointer to an array.
6612 if (t
->points_to()->array_type() != NULL
6613 && !t
->points_to()->is_slice_type())
6616 if (t
->points_to()->named_type() != NULL
6617 && t
->points_to()->struct_type() != NULL
)
6618 declare
->push_back(t
->points_to()->named_type()->named_object());
6622 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6625 if (t
->is_slice_type())
6627 return this->can_write_type_to_c_header(t
->array_type()->element_type(),
6632 const Named_object
* no
= t
->named_type()->named_object();
6633 if (no
->package() != NULL
)
6635 if (t
->is_unsafe_pointer_type())
6639 if (t
->struct_type() != NULL
)
6641 requires
->push_back(no
);
6642 return t
->struct_type()->can_write_to_c_header(requires
, declare
);
6644 return this->can_write_type_to_c_header(t
->base(), requires
, declare
);
6647 case TYPE_CALL_MULTIPLE_RESULT
:
6655 // Write this struct to a C header file.
6658 Struct_type::write_to_c_header(std::ostream
& os
) const
6660 const Struct_field_list
* fields
= this->fields_
;
6661 for (Struct_field_list::const_iterator p
= fields
->begin();
6666 this->write_field_to_c_header(os
, p
->field_name(), p
->type());
6667 os
<< ';' << std::endl
;
6671 // Write the type of a struct field to a C header file.
6674 Struct_type::write_field_to_c_header(std::ostream
& os
, const std::string
& name
,
6675 const Type
*t
) const
6677 bool print_name
= true;
6679 switch (t
->classification())
6691 const Integer_type
* it
= t
->integer_type();
6692 if (it
->is_unsigned())
6694 os
<< "int" << it
->bits() << "_t";
6699 switch (t
->float_type()->bits())
6713 switch (t
->complex_type()->bits())
6716 os
<< "float _Complex";
6719 os
<< "double _Complex";
6736 std::vector
<const Named_object
*> requires
;
6737 std::vector
<const Named_object
*> declare
;
6738 if (!this->can_write_type_to_c_header(t
->points_to(), &requires
,
6743 this->write_field_to_c_header(os
, "", t
->points_to());
6757 case TYPE_INTERFACE
:
6758 if (t
->interface_type()->is_empty())
6765 os
<< "struct {" << std::endl
;
6766 t
->struct_type()->write_to_c_header(os
);
6771 if (t
->is_slice_type())
6775 const Type
*ele
= t
;
6776 std::vector
<const Type
*> array_types
;
6777 while (ele
->array_type() != NULL
&& !ele
->is_slice_type())
6779 array_types
.push_back(ele
);
6780 ele
= ele
->array_type()->element_type();
6782 this->write_field_to_c_header(os
, "", ele
);
6783 os
<< ' ' << Gogo::message_name(name
);
6785 while (!array_types
.empty())
6787 ele
= array_types
.back();
6788 array_types
.pop_back();
6790 Numeric_constant nc
;
6791 if (!ele
->array_type()->length()->numeric_constant_value(&nc
))
6794 if (!nc
.to_int(&val
))
6796 char* s
= mpz_get_str(NULL
, 10, val
);
6807 const Named_object
* no
= t
->named_type()->named_object();
6808 if (t
->struct_type() != NULL
)
6809 os
<< "struct " << no
->message_name();
6810 else if (t
->is_unsafe_pointer_type())
6812 else if (t
== Type::lookup_integer_type("uintptr"))
6816 this->write_field_to_c_header(os
, name
, t
->base());
6824 case TYPE_CALL_MULTIPLE_RESULT
:
6831 if (print_name
&& !name
.empty())
6832 os
<< ' ' << Gogo::message_name(name
);
6835 // Make a struct type.
6838 Type::make_struct_type(Struct_field_list
* fields
,
6841 return new Struct_type(fields
, location
);
6844 // Class Array_type.
6846 // Store the length of an array as an int64_t into *PLEN. Return
6847 // false if the length can not be determined. This will assert if
6848 // called for a slice.
6851 Array_type::int_length(int64_t* plen
)
6853 go_assert(this->length_
!= NULL
);
6854 Numeric_constant nc
;
6855 if (!this->length_
->numeric_constant_value(&nc
))
6857 return nc
.to_memory_size(plen
);
6860 // Whether two array types are identical.
6863 Array_type::is_identical(const Array_type
* t
, Cmp_tags cmp_tags
,
6864 bool errors_are_identical
) const
6866 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
6867 cmp_tags
, errors_are_identical
, NULL
))
6870 if (this->is_array_incomparable_
!= t
->is_array_incomparable_
)
6873 Expression
* l1
= this->length();
6874 Expression
* l2
= t
->length();
6876 // Slices of the same element type are identical.
6877 if (l1
== NULL
&& l2
== NULL
)
6880 // Arrays of the same element type are identical if they have the
6882 if (l1
!= NULL
&& l2
!= NULL
)
6887 // Try to determine the lengths. If we can't, assume the arrays
6888 // are not identical.
6890 Numeric_constant nc1
, nc2
;
6891 if (l1
->numeric_constant_value(&nc1
)
6892 && l2
->numeric_constant_value(&nc2
))
6895 if (nc1
.to_int(&v1
))
6898 if (nc2
.to_int(&v2
))
6900 ret
= mpz_cmp(v1
, v2
) == 0;
6909 // Otherwise the arrays are not identical.
6916 Array_type::do_traverse(Traverse
* traverse
)
6918 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
6919 return TRAVERSE_EXIT
;
6920 if (this->length_
!= NULL
6921 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
6922 return TRAVERSE_EXIT
;
6923 return TRAVERSE_CONTINUE
;
6926 // Check that the length is valid.
6929 Array_type::verify_length()
6931 if (this->length_
== NULL
)
6934 Type_context
context(Type::lookup_integer_type("int"), false);
6935 this->length_
->determine_type(&context
);
6937 if (!this->length_
->is_constant())
6939 go_error_at(this->length_
->location(), "array bound is not constant");
6943 Numeric_constant nc
;
6944 if (!this->length_
->numeric_constant_value(&nc
))
6946 if (this->length_
->type()->integer_type() != NULL
6947 || this->length_
->type()->float_type() != NULL
)
6948 go_error_at(this->length_
->location(), "array bound is not constant");
6950 go_error_at(this->length_
->location(), "array bound is not numeric");
6954 Type
* int_type
= Type::lookup_integer_type("int");
6955 unsigned int tbits
= int_type
->integer_type()->bits();
6957 switch (nc
.to_unsigned_long(&val
))
6959 case Numeric_constant::NC_UL_VALID
:
6960 if (sizeof(val
) >= tbits
/ 8 && val
>> (tbits
- 1) != 0)
6962 go_error_at(this->length_
->location(), "array bound overflows");
6966 case Numeric_constant::NC_UL_NOTINT
:
6967 go_error_at(this->length_
->location(), "array bound truncated to integer");
6969 case Numeric_constant::NC_UL_NEGATIVE
:
6970 go_error_at(this->length_
->location(), "negative array bound");
6972 case Numeric_constant::NC_UL_BIG
:
6975 if (!nc
.to_int(&val
))
6977 unsigned int bits
= mpz_sizeinbase(val
, 2);
6981 go_error_at(this->length_
->location(), "array bound overflows");
6996 Array_type::do_verify()
6998 if (this->element_type()->is_error_type())
7000 if (!this->verify_length())
7001 this->length_
= Expression::make_error(this->length_
->location());
7005 // Whether the type contains pointers. This is always true for a
7006 // slice. For an array it is true if the element type has pointers
7007 // and the length is greater than zero.
7010 Array_type::do_has_pointer() const
7012 if (this->length_
== NULL
)
7014 if (!this->element_type_
->has_pointer())
7017 Numeric_constant nc
;
7018 if (!this->length_
->numeric_constant_value(&nc
))
7020 // Error reported elsewhere.
7025 switch (nc
.to_unsigned_long(&val
))
7027 case Numeric_constant::NC_UL_VALID
:
7029 case Numeric_constant::NC_UL_BIG
:
7032 // Error reported elsewhere.
7037 // Whether we can use memcmp to compare this array.
7040 Array_type::do_compare_is_identity(Gogo
* gogo
)
7042 if (this->length_
== NULL
)
7045 // Check for [...], which indicates that this is not a real type.
7046 if (this->length_
->is_nil_expression())
7049 if (!this->element_type_
->compare_is_identity(gogo
))
7052 // If there is any padding, then we can't use memcmp.
7055 if (!this->element_type_
->backend_type_size(gogo
, &size
)
7056 || !this->element_type_
->backend_type_align(gogo
, &align
))
7058 if ((size
& (align
- 1)) != 0)
7064 // Array type hash code.
7067 Array_type::do_hash_for_method(Gogo
* gogo
) const
7071 // There is no very convenient way to get a hash code for the
7073 ret
= this->element_type_
->hash_for_method(gogo
) + 1;
7074 if (this->is_array_incomparable_
)
7079 // Write the hash function for an array which can not use the identify
7083 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
7084 Function_type
* hash_fntype
,
7085 Function_type
* equal_fntype
)
7087 Location bloc
= Linemap::predeclared_location();
7089 // The pointer to the array that we are going to hash. This is an
7090 // argument to the hash function we are implementing here.
7091 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
7092 go_assert(key_arg
!= NULL
);
7093 Type
* key_arg_type
= key_arg
->var_value()->type();
7095 // The seed argument to the hash function.
7096 Named_object
* seed_arg
= gogo
->lookup("seed", NULL
);
7097 go_assert(seed_arg
!= NULL
);
7099 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7101 // Make a temporary to hold the return value, initialized to the seed.
7102 Expression
* ref
= Expression::make_var_reference(seed_arg
, bloc
);
7103 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, ref
,
7105 gogo
->add_statement(retval
);
7107 // Make a temporary to hold the key as a uintptr.
7108 ref
= Expression::make_var_reference(key_arg
, bloc
);
7109 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
7110 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
7112 gogo
->add_statement(key
);
7114 // Loop over the array elements.
7116 Type
* int_type
= Type::lookup_integer_type("int");
7117 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7118 gogo
->add_statement(index
);
7120 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7121 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
7122 Type
* pt
= Type::make_pointer_type(name
!= NULL
7123 ? static_cast<Type
*>(name
)
7124 : static_cast<Type
*>(this));
7125 aref
= Expression::make_cast(pt
, aref
, bloc
);
7126 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7131 gogo
->start_block(bloc
);
7133 // Get the hash function for the element type.
7134 Named_object
* hash_fn
;
7135 Named_object
* equal_fn
;
7136 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
7137 hash_fntype
, equal_fntype
, &hash_fn
,
7140 // Get a pointer to this element in the loop.
7141 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
7142 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
7144 // Get the size of each element.
7145 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
7146 Expression::TYPE_INFO_SIZE
);
7148 // Get the hash of this element, passing retval as the seed.
7149 ref
= Expression::make_temporary_reference(retval
, bloc
);
7150 Expression_list
* args
= new Expression_list();
7151 args
->push_back(subkey
);
7152 args
->push_back(ref
);
7153 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
7154 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
7156 // Set retval to the result.
7157 Temporary_reference_expression
* tref
=
7158 Expression::make_temporary_reference(retval
, bloc
);
7159 tref
->set_is_lvalue();
7160 Statement
* s
= Statement::make_assignment(tref
, call
, bloc
);
7161 gogo
->add_statement(s
);
7163 // Increase the element pointer.
7164 tref
= Expression::make_temporary_reference(key
, bloc
);
7165 tref
->set_is_lvalue();
7166 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
7168 Block
* statements
= gogo
->finish_block(bloc
);
7170 for_range
->add_statements(statements
);
7171 gogo
->add_statement(for_range
);
7173 // Return retval to the caller of the hash function.
7174 Expression_list
* vals
= new Expression_list();
7175 ref
= Expression::make_temporary_reference(retval
, bloc
);
7176 vals
->push_back(ref
);
7177 s
= Statement::make_return_statement(vals
, bloc
);
7178 gogo
->add_statement(s
);
7181 // Write the equality function for an array which can not use the
7182 // identity function.
7185 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
7187 Location bloc
= Linemap::predeclared_location();
7189 // The pointers to the arrays we are going to compare.
7190 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
7191 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
7192 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
7194 // Build temporaries for the keys with the right types.
7195 Type
* pt
= Type::make_pointer_type(name
!= NULL
7196 ? static_cast<Type
*>(name
)
7197 : static_cast<Type
*>(this));
7199 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
7200 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7201 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
7202 gogo
->add_statement(p1
);
7204 ref
= Expression::make_var_reference(key2_arg
, bloc
);
7205 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
7206 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
7207 gogo
->add_statement(p2
);
7209 // Loop over the array elements.
7211 Type
* int_type
= Type::lookup_integer_type("int");
7212 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
7213 gogo
->add_statement(index
);
7215 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
7216 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
7217 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
7222 gogo
->start_block(bloc
);
7224 // Compare element in P1 and P2.
7225 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
7226 e1
= Expression::make_dereference(e1
, Expression::NIL_CHECK_DEFAULT
, bloc
);
7227 ref
= Expression::make_temporary_reference(index
, bloc
);
7228 e1
= Expression::make_array_index(e1
, ref
, NULL
, NULL
, bloc
);
7230 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
7231 e2
= Expression::make_dereference(e2
, Expression::NIL_CHECK_DEFAULT
, bloc
);
7232 ref
= Expression::make_temporary_reference(index
, bloc
);
7233 e2
= Expression::make_array_index(e2
, ref
, NULL
, NULL
, bloc
);
7235 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
7237 // If the elements are not equal, return false.
7238 gogo
->start_block(bloc
);
7239 Expression_list
* vals
= new Expression_list();
7240 vals
->push_back(Expression::make_boolean(false, bloc
));
7241 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
7242 gogo
->add_statement(s
);
7243 Block
* then_block
= gogo
->finish_block(bloc
);
7245 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
7246 gogo
->add_statement(s
);
7248 Block
* statements
= gogo
->finish_block(bloc
);
7250 for_range
->add_statements(statements
);
7251 gogo
->add_statement(for_range
);
7253 // All the elements are equal, so return true.
7254 vals
= new Expression_list();
7255 vals
->push_back(Expression::make_boolean(true, bloc
));
7256 s
= Statement::make_return_statement(vals
, bloc
);
7257 gogo
->add_statement(s
);
7260 // Get the backend representation of the fields of a slice. This is
7261 // not declared in types.h so that types.h doesn't have to #include
7264 // We use int for the count and capacity fields. This matches 6g.
7265 // The language more or less assumes that we can't allocate space of a
7266 // size which does not fit in int.
7269 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
7270 std::vector
<Backend::Btyped_identifier
>* bfields
)
7274 Type
* pet
= Type::make_pointer_type(type
->element_type());
7275 Btype
* pbet
= (use_placeholder
7276 ? pet
->get_backend_placeholder(gogo
)
7277 : pet
->get_backend(gogo
));
7278 Location ploc
= Linemap::predeclared_location();
7280 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
7281 p
->name
= "__values";
7285 Type
* int_type
= Type::lookup_integer_type("int");
7288 p
->name
= "__count";
7289 p
->btype
= int_type
->get_backend(gogo
);
7293 p
->name
= "__capacity";
7294 p
->btype
= int_type
->get_backend(gogo
);
7298 // Get the backend representation for the type of this array. A fixed array is
7299 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7300 // just like an array in C. An open array is a struct with three
7301 // fields: a data pointer, the length, and the capacity.
7304 Array_type::do_get_backend(Gogo
* gogo
)
7306 if (this->length_
== NULL
)
7308 std::vector
<Backend::Btyped_identifier
> bfields
;
7309 get_backend_slice_fields(gogo
, this, false, &bfields
);
7310 return gogo
->backend()->struct_type(bfields
);
7314 Btype
* element
= this->get_backend_element(gogo
, false);
7315 Bexpression
* len
= this->get_backend_length(gogo
);
7316 return gogo
->backend()->array_type(element
, len
);
7320 // Return the backend representation of the element type.
7323 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
7325 if (use_placeholder
)
7326 return this->element_type_
->get_backend_placeholder(gogo
);
7328 return this->element_type_
->get_backend(gogo
);
7331 // Return the backend representation of the length. The length may be
7332 // computed using a function call, so we must only evaluate it once.
7335 Array_type::get_backend_length(Gogo
* gogo
)
7337 go_assert(this->length_
!= NULL
);
7338 if (this->blength_
== NULL
)
7340 if (this->length_
->is_error_expression())
7342 this->blength_
= gogo
->backend()->error_expression();
7343 return this->blength_
;
7345 Numeric_constant nc
;
7347 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
7349 if (mpz_sgn(val
) < 0)
7351 this->blength_
= gogo
->backend()->error_expression();
7352 return this->blength_
;
7354 Type
* t
= nc
.type();
7356 t
= Type::lookup_integer_type("int");
7357 else if (t
->is_abstract())
7358 t
= t
->make_non_abstract_type();
7359 Btype
* btype
= t
->get_backend(gogo
);
7361 gogo
->backend()->integer_constant_expression(btype
, val
);
7366 // Make up a translation context for the array length
7367 // expression. FIXME: This won't work in general.
7368 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7369 this->blength_
= this->length_
->get_backend(&context
);
7371 Btype
* ibtype
= Type::lookup_integer_type("int")->get_backend(gogo
);
7373 gogo
->backend()->convert_expression(ibtype
, this->blength_
,
7374 this->length_
->location());
7377 return this->blength_
;
7380 // Finish backend representation of the array.
7383 Array_type::finish_backend_element(Gogo
* gogo
)
7385 Type
* et
= this->array_type()->element_type();
7386 et
->get_backend(gogo
);
7387 if (this->is_slice_type())
7389 // This relies on the fact that we always use the same
7390 // structure for a pointer to any given type.
7391 Type
* pet
= Type::make_pointer_type(et
);
7392 pet
->get_backend(gogo
);
7396 // Return an expression for a pointer to the values in ARRAY.
7399 Array_type::get_value_pointer(Gogo
*, Expression
* array
, bool is_lvalue
) const
7401 if (this->length() != NULL
)
7404 go_assert(array
->type()->array_type() != NULL
);
7405 Type
* etype
= array
->type()->array_type()->element_type();
7406 array
= Expression::make_unary(OPERATOR_AND
, array
, array
->location());
7407 return Expression::make_cast(Type::make_pointer_type(etype
), array
,
7415 Temporary_reference_expression
* tref
=
7416 array
->temporary_reference_expression();
7417 Var_expression
* ve
= array
->var_expression();
7420 tref
= tref
->copy()->temporary_reference_expression();
7421 tref
->set_is_lvalue();
7424 else if (ve
!= NULL
)
7426 ve
= new Var_expression(ve
->named_object(), ve
->location());
7431 return Expression::make_slice_info(array
,
7432 Expression::SLICE_INFO_VALUE_POINTER
,
7436 // Return an expression for the length of the array ARRAY which has this
7440 Array_type::get_length(Gogo
*, Expression
* array
) const
7442 if (this->length_
!= NULL
)
7443 return this->length_
;
7445 // This is a slice. We need to read the length field.
7446 return Expression::make_slice_info(array
, Expression::SLICE_INFO_LENGTH
,
7450 // Return an expression for the capacity of the array ARRAY which has this
7454 Array_type::get_capacity(Gogo
*, Expression
* array
) const
7456 if (this->length_
!= NULL
)
7457 return this->length_
;
7459 // This is a slice. We need to read the capacity field.
7460 return Expression::make_slice_info(array
, Expression::SLICE_INFO_CAPACITY
,
7467 Array_type::do_export(Export
* exp
) const
7469 exp
->write_c_string("[");
7470 if (this->length_
!= NULL
)
7471 this->length_
->export_expression(exp
);
7472 exp
->write_c_string("] ");
7473 exp
->write_type(this->element_type_
);
7479 Array_type::do_import(Import
* imp
)
7481 imp
->require_c_string("[");
7483 if (imp
->peek_char() == ']')
7486 length
= Expression::import_expression(imp
);
7487 imp
->require_c_string("] ");
7488 Type
* element_type
= imp
->read_type();
7489 return Type::make_array_type(element_type
, length
);
7492 // The type of an array type descriptor.
7495 Array_type::make_array_type_descriptor_type()
7500 Type
* tdt
= Type::make_type_descriptor_type();
7501 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7503 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7506 Type::make_builtin_struct_type(4,
7510 "len", uintptr_type
);
7512 ret
= Type::make_builtin_named_type("ArrayType", sf
);
7518 // The type of an slice type descriptor.
7521 Array_type::make_slice_type_descriptor_type()
7526 Type
* tdt
= Type::make_type_descriptor_type();
7527 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7530 Type::make_builtin_struct_type(2,
7534 ret
= Type::make_builtin_named_type("SliceType", sf
);
7540 // Build a type descriptor for an array/slice type.
7543 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7545 if (this->length_
!= NULL
)
7546 return this->array_type_descriptor(gogo
, name
);
7548 return this->slice_type_descriptor(gogo
, name
);
7551 // Build a type descriptor for an array type.
7554 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7556 Location bloc
= Linemap::predeclared_location();
7558 Type
* atdt
= Array_type::make_array_type_descriptor_type();
7560 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
7562 Expression_list
* vals
= new Expression_list();
7565 Struct_field_list::const_iterator p
= fields
->begin();
7566 go_assert(p
->is_field_name("_type"));
7567 vals
->push_back(this->type_descriptor_constructor(gogo
,
7568 RUNTIME_TYPE_KIND_ARRAY
,
7572 go_assert(p
->is_field_name("elem"));
7573 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7576 go_assert(p
->is_field_name("slice"));
7577 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
7578 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
7581 go_assert(p
->is_field_name("len"));
7582 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
7585 go_assert(p
== fields
->end());
7587 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
7590 // Build a type descriptor for a slice type.
7593 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7595 Location bloc
= Linemap::predeclared_location();
7597 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
7599 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
7601 Expression_list
* vals
= new Expression_list();
7604 Struct_field_list::const_iterator p
= fields
->begin();
7605 go_assert(p
->is_field_name("_type"));
7606 vals
->push_back(this->type_descriptor_constructor(gogo
,
7607 RUNTIME_TYPE_KIND_SLICE
,
7611 go_assert(p
->is_field_name("elem"));
7612 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7615 go_assert(p
== fields
->end());
7617 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
7620 // Reflection string.
7623 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7625 ret
->push_back('[');
7626 if (this->length_
!= NULL
)
7628 Numeric_constant nc
;
7629 if (!this->length_
->numeric_constant_value(&nc
))
7631 go_assert(saw_errors());
7635 if (!nc
.to_int(&val
))
7637 go_assert(saw_errors());
7640 char* s
= mpz_get_str(NULL
, 10, val
);
7645 ret
->push_back(']');
7647 this->append_reflection(this->element_type_
, gogo
, ret
);
7650 // Make an array type.
7653 Type::make_array_type(Type
* element_type
, Expression
* length
)
7655 return new Array_type(element_type
, length
);
7660 Named_object
* Map_type::zero_value
;
7661 int64_t Map_type::zero_value_size
;
7662 int64_t Map_type::zero_value_align
;
7664 // If this map requires the "fat" functions, return the pointer to
7665 // pass as the zero value to those functions. Otherwise, in the
7666 // normal case, return NULL. The map requires the "fat" functions if
7667 // the value size is larger than max_zero_size bytes. max_zero_size
7668 // must match maxZero in libgo/go/runtime/hashmap.go.
7671 Map_type::fat_zero_value(Gogo
* gogo
)
7674 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
7676 go_assert(saw_errors());
7679 if (valsize
<= Map_type::max_zero_size
)
7682 if (Map_type::zero_value_size
< valsize
)
7683 Map_type::zero_value_size
= valsize
;
7686 if (!this->val_type_
->backend_type_align(gogo
, &valalign
))
7688 go_assert(saw_errors());
7692 if (Map_type::zero_value_align
< valalign
)
7693 Map_type::zero_value_align
= valalign
;
7695 Location bloc
= Linemap::predeclared_location();
7697 if (Map_type::zero_value
== NULL
)
7699 // The final type will be set in backend_zero_value.
7700 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7701 Expression
* size
= Expression::make_integer_ul(0, NULL
, bloc
);
7702 Array_type
* array_type
= Type::make_array_type(uint8_type
, size
);
7703 array_type
->set_is_array_incomparable();
7704 Variable
* var
= new Variable(array_type
, NULL
, true, false, false, bloc
);
7705 std::string name
= gogo
->map_zero_value_name();
7706 Map_type::zero_value
= Named_object::make_variable(name
, NULL
, var
);
7709 Expression
* z
= Expression::make_var_reference(Map_type::zero_value
, bloc
);
7710 z
= Expression::make_unary(OPERATOR_AND
, z
, bloc
);
7711 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
7712 z
= Expression::make_cast(unsafe_ptr_type
, z
, bloc
);
7716 // Return whether VAR is the map zero value.
7719 Map_type::is_zero_value(Variable
* var
)
7721 return (Map_type::zero_value
!= NULL
7722 && Map_type::zero_value
->var_value() == var
);
7725 // Return the backend representation for the zero value.
7728 Map_type::backend_zero_value(Gogo
* gogo
)
7730 Location bloc
= Linemap::predeclared_location();
7732 go_assert(Map_type::zero_value
!= NULL
);
7734 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7735 Btype
* buint8_type
= uint8_type
->get_backend(gogo
);
7737 Type
* int_type
= Type::lookup_integer_type("int");
7739 Expression
* e
= Expression::make_integer_int64(Map_type::zero_value_size
,
7741 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
7742 Bexpression
* blength
= e
->get_backend(&context
);
7744 Btype
* barray_type
= gogo
->backend()->array_type(buint8_type
, blength
);
7746 std::string zname
= Map_type::zero_value
->name();
7747 std::string
asm_name(go_selectively_encode_id(zname
));
7749 gogo
->backend()->implicit_variable(zname
, asm_name
,
7750 barray_type
, false, false, true,
7751 Map_type::zero_value_align
);
7752 gogo
->backend()->implicit_variable_set_init(zvar
, zname
, barray_type
,
7753 false, false, true, NULL
);
7760 Map_type::do_traverse(Traverse
* traverse
)
7762 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
7763 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
7764 return TRAVERSE_EXIT
;
7765 return TRAVERSE_CONTINUE
;
7768 // Check that the map type is OK.
7771 Map_type::do_verify()
7773 // The runtime support uses "map[void]void".
7774 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
7775 go_error_at(this->location_
, "invalid map key type");
7776 if (!this->key_type_
->in_heap())
7777 go_error_at(this->location_
, "go:notinheap map key not allowed");
7778 if (!this->val_type_
->in_heap())
7779 go_error_at(this->location_
, "go:notinheap map value not allowed");
7783 // Whether two map types are identical.
7786 Map_type::is_identical(const Map_type
* t
, Cmp_tags cmp_tags
,
7787 bool errors_are_identical
) const
7789 return (Type::are_identical_cmp_tags(this->key_type(), t
->key_type(),
7790 cmp_tags
, errors_are_identical
, NULL
)
7791 && Type::are_identical_cmp_tags(this->val_type(), t
->val_type(),
7792 cmp_tags
, errors_are_identical
,
7799 Map_type::do_hash_for_method(Gogo
* gogo
) const
7801 return (this->key_type_
->hash_for_method(gogo
)
7802 + this->val_type_
->hash_for_method(gogo
)
7806 // Get the backend representation for a map type. A map type is
7807 // represented as a pointer to a struct. The struct is hmap in
7808 // runtime/hashmap.go.
7811 Map_type::do_get_backend(Gogo
* gogo
)
7813 static Btype
* backend_map_type
;
7814 if (backend_map_type
== NULL
)
7816 std::vector
<Backend::Btyped_identifier
> bfields(9);
7818 Location bloc
= Linemap::predeclared_location();
7820 Type
* int_type
= Type::lookup_integer_type("int");
7821 bfields
[0].name
= "count";
7822 bfields
[0].btype
= int_type
->get_backend(gogo
);
7823 bfields
[0].location
= bloc
;
7825 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7826 bfields
[1].name
= "flags";
7827 bfields
[1].btype
= uint8_type
->get_backend(gogo
);
7828 bfields
[1].location
= bloc
;
7830 bfields
[2].name
= "B";
7831 bfields
[2].btype
= bfields
[1].btype
;
7832 bfields
[2].location
= bloc
;
7834 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7835 bfields
[3].name
= "noverflow";
7836 bfields
[3].btype
= uint16_type
->get_backend(gogo
);
7837 bfields
[3].location
= bloc
;
7839 Type
* uint32_type
= Type::lookup_integer_type("uint32");
7840 bfields
[4].name
= "hash0";
7841 bfields
[4].btype
= uint32_type
->get_backend(gogo
);
7842 bfields
[4].location
= bloc
;
7844 Btype
* bvt
= gogo
->backend()->void_type();
7845 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
7846 bfields
[5].name
= "buckets";
7847 bfields
[5].btype
= bpvt
;
7848 bfields
[5].location
= bloc
;
7850 bfields
[6].name
= "oldbuckets";
7851 bfields
[6].btype
= bpvt
;
7852 bfields
[6].location
= bloc
;
7854 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7855 bfields
[7].name
= "nevacuate";
7856 bfields
[7].btype
= uintptr_type
->get_backend(gogo
);
7857 bfields
[7].location
= bloc
;
7859 bfields
[8].name
= "extra";
7860 bfields
[8].btype
= bpvt
;
7861 bfields
[8].location
= bloc
;
7863 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
7864 bt
= gogo
->backend()->named_type("runtime.hmap", bt
, bloc
);
7865 backend_map_type
= gogo
->backend()->pointer_type(bt
);
7867 return backend_map_type
;
7870 // The type of a map type descriptor.
7873 Map_type::make_map_type_descriptor_type()
7878 Type
* tdt
= Type::make_type_descriptor_type();
7879 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7880 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7881 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7882 Type
* bool_type
= Type::lookup_bool_type();
7885 Type::make_builtin_struct_type(12,
7891 "keysize", uint8_type
,
7892 "indirectkey", bool_type
,
7893 "valuesize", uint8_type
,
7894 "indirectvalue", bool_type
,
7895 "bucketsize", uint16_type
,
7896 "reflexivekey", bool_type
,
7897 "needkeyupdate", bool_type
);
7899 ret
= Type::make_builtin_named_type("MapType", sf
);
7905 // Build a type descriptor for a map type.
7908 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7910 Location bloc
= Linemap::predeclared_location();
7912 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
7913 Type
* uint8_type
= Type::lookup_integer_type("uint8");
7914 Type
* uint16_type
= Type::lookup_integer_type("uint16");
7917 if (!this->key_type_
->backend_type_size(gogo
, &keysize
))
7919 go_error_at(this->location_
, "error determining map key type size");
7920 return Expression::make_error(this->location_
);
7924 if (!this->val_type_
->backend_type_size(gogo
, &valsize
))
7926 go_error_at(this->location_
, "error determining map value type size");
7927 return Expression::make_error(this->location_
);
7931 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptrsize
))
7933 go_assert(saw_errors());
7934 return Expression::make_error(this->location_
);
7937 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
7938 if (bucket_type
== NULL
)
7940 go_assert(saw_errors());
7941 return Expression::make_error(this->location_
);
7945 if (!bucket_type
->backend_type_size(gogo
, &bucketsize
))
7947 go_assert(saw_errors());
7948 return Expression::make_error(this->location_
);
7951 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
7953 Expression_list
* vals
= new Expression_list();
7956 Struct_field_list::const_iterator p
= fields
->begin();
7957 go_assert(p
->is_field_name("_type"));
7958 vals
->push_back(this->type_descriptor_constructor(gogo
,
7959 RUNTIME_TYPE_KIND_MAP
,
7963 go_assert(p
->is_field_name("key"));
7964 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
7967 go_assert(p
->is_field_name("elem"));
7968 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
7971 go_assert(p
->is_field_name("bucket"));
7972 vals
->push_back(Expression::make_type_descriptor(bucket_type
, bloc
));
7975 go_assert(p
->is_field_name("hmap"));
7976 Type
* hmap_type
= this->hmap_type(bucket_type
);
7977 vals
->push_back(Expression::make_type_descriptor(hmap_type
, bloc
));
7980 go_assert(p
->is_field_name("keysize"));
7981 if (keysize
> Map_type::max_key_size
)
7982 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
7984 vals
->push_back(Expression::make_integer_int64(keysize
, uint8_type
, bloc
));
7987 go_assert(p
->is_field_name("indirectkey"));
7988 vals
->push_back(Expression::make_boolean(keysize
> Map_type::max_key_size
,
7992 go_assert(p
->is_field_name("valuesize"));
7993 if (valsize
> Map_type::max_val_size
)
7994 vals
->push_back(Expression::make_integer_int64(ptrsize
, uint8_type
, bloc
));
7996 vals
->push_back(Expression::make_integer_int64(valsize
, uint8_type
, bloc
));
7999 go_assert(p
->is_field_name("indirectvalue"));
8000 vals
->push_back(Expression::make_boolean(valsize
> Map_type::max_val_size
,
8004 go_assert(p
->is_field_name("bucketsize"));
8005 vals
->push_back(Expression::make_integer_int64(bucketsize
, uint16_type
,
8009 go_assert(p
->is_field_name("reflexivekey"));
8010 vals
->push_back(Expression::make_boolean(this->key_type_
->is_reflexive(),
8014 go_assert(p
->is_field_name("needkeyupdate"));
8015 vals
->push_back(Expression::make_boolean(this->key_type_
->needs_key_update(),
8019 go_assert(p
== fields
->end());
8021 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
8024 // Return the bucket type to use for a map type. This must correspond
8025 // to libgo/go/runtime/hashmap.go.
8028 Map_type::bucket_type(Gogo
* gogo
, int64_t keysize
, int64_t valsize
)
8030 if (this->bucket_type_
!= NULL
)
8031 return this->bucket_type_
;
8033 Type
* key_type
= this->key_type_
;
8034 if (keysize
> Map_type::max_key_size
)
8035 key_type
= Type::make_pointer_type(key_type
);
8037 Type
* val_type
= this->val_type_
;
8038 if (valsize
> Map_type::max_val_size
)
8039 val_type
= Type::make_pointer_type(val_type
);
8041 Expression
* bucket_size
= Expression::make_integer_ul(Map_type::bucket_size
,
8042 NULL
, this->location_
);
8044 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8045 Array_type
* topbits_type
= Type::make_array_type(uint8_type
, bucket_size
);
8046 topbits_type
->set_is_array_incomparable();
8047 Array_type
* keys_type
= Type::make_array_type(key_type
, bucket_size
);
8048 keys_type
->set_is_array_incomparable();
8049 Array_type
* values_type
= Type::make_array_type(val_type
, bucket_size
);
8050 values_type
->set_is_array_incomparable();
8052 // If keys and values have no pointers, the map implementation can
8053 // keep a list of overflow pointers on the side so that buckets can
8054 // be marked as having no pointers. Arrange for the bucket to have
8055 // no pointers by changing the type of the overflow field to uintptr
8056 // in this case. See comment on the hmap.overflow field in
8057 // libgo/go/runtime/hashmap.go.
8058 Type
* overflow_type
;
8059 if (!key_type
->has_pointer() && !val_type
->has_pointer())
8060 overflow_type
= Type::lookup_integer_type("uintptr");
8063 // This should really be a pointer to the bucket type itself,
8064 // but that would require us to construct a Named_type for it to
8065 // give it a way to refer to itself. Since nothing really cares
8066 // (except perhaps for someone using a debugger) just use an
8068 overflow_type
= Type::make_pointer_type(Type::make_void_type());
8071 // Make sure the overflow pointer is the last memory in the struct,
8072 // because the runtime assumes it can use size-ptrSize as the offset
8073 // of the overflow pointer. We double-check that property below
8074 // once the offsets and size are computed.
8076 int64_t topbits_field_size
, topbits_field_align
;
8077 int64_t keys_field_size
, keys_field_align
;
8078 int64_t values_field_size
, values_field_align
;
8079 int64_t overflow_field_size
, overflow_field_align
;
8080 if (!topbits_type
->backend_type_size(gogo
, &topbits_field_size
)
8081 || !topbits_type
->backend_type_field_align(gogo
, &topbits_field_align
)
8082 || !keys_type
->backend_type_size(gogo
, &keys_field_size
)
8083 || !keys_type
->backend_type_field_align(gogo
, &keys_field_align
)
8084 || !values_type
->backend_type_size(gogo
, &values_field_size
)
8085 || !values_type
->backend_type_field_align(gogo
, &values_field_align
)
8086 || !overflow_type
->backend_type_size(gogo
, &overflow_field_size
)
8087 || !overflow_type
->backend_type_field_align(gogo
, &overflow_field_align
))
8089 go_assert(saw_errors());
8094 int64_t max_align
= std::max(std::max(topbits_field_align
, keys_field_align
),
8095 values_field_align
);
8096 if (max_align
<= overflow_field_align
)
8097 ret
= make_builtin_struct_type(4,
8098 "topbits", topbits_type
,
8100 "values", values_type
,
8101 "overflow", overflow_type
);
8104 size_t off
= topbits_field_size
;
8105 off
= ((off
+ keys_field_align
- 1)
8106 &~ static_cast<size_t>(keys_field_align
- 1));
8107 off
+= keys_field_size
;
8108 off
= ((off
+ values_field_align
- 1)
8109 &~ static_cast<size_t>(values_field_align
- 1));
8110 off
+= values_field_size
;
8112 int64_t padded_overflow_field_size
=
8113 ((overflow_field_size
+ max_align
- 1)
8114 &~ static_cast<size_t>(max_align
- 1));
8117 ovoff
= ((ovoff
+ max_align
- 1)
8118 &~ static_cast<size_t>(max_align
- 1));
8119 size_t pad
= (ovoff
- off
8120 + padded_overflow_field_size
- overflow_field_size
);
8122 Expression
* pad_expr
= Expression::make_integer_ul(pad
, NULL
,
8124 Array_type
* pad_type
= Type::make_array_type(uint8_type
, pad_expr
);
8125 pad_type
->set_is_array_incomparable();
8127 ret
= make_builtin_struct_type(5,
8128 "topbits", topbits_type
,
8130 "values", values_type
,
8132 "overflow", overflow_type
);
8135 // Verify that the overflow field is just before the end of the
8138 Btype
* btype
= ret
->get_backend(gogo
);
8139 int64_t offset
= gogo
->backend()->type_field_offset(btype
,
8140 ret
->field_count() - 1);
8142 if (!ret
->backend_type_size(gogo
, &size
))
8144 go_assert(saw_errors());
8149 if (!Type::make_pointer_type(uint8_type
)->backend_type_size(gogo
, &ptr_size
))
8151 go_assert(saw_errors());
8155 go_assert(offset
+ ptr_size
== size
);
8157 ret
->set_is_struct_incomparable();
8159 this->bucket_type_
= ret
;
8163 // Return the hashmap type for a map type.
8166 Map_type::hmap_type(Type
* bucket_type
)
8168 if (this->hmap_type_
!= NULL
)
8169 return this->hmap_type_
;
8171 Type
* int_type
= Type::lookup_integer_type("int");
8172 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8173 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8174 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8175 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8176 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8178 Type
* ptr_bucket_type
= Type::make_pointer_type(bucket_type
);
8180 Struct_type
* ret
= make_builtin_struct_type(9,
8182 "flags", uint8_type
,
8184 "noverflow", uint16_type
,
8185 "hash0", uint32_type
,
8186 "buckets", ptr_bucket_type
,
8187 "oldbuckets", ptr_bucket_type
,
8188 "nevacuate", uintptr_type
,
8189 "extra", void_ptr_type
);
8190 ret
->set_is_struct_incomparable();
8191 this->hmap_type_
= ret
;
8195 // Return the iterator type for a map type. This is the type of the
8196 // value used when doing a range over a map.
8199 Map_type::hiter_type(Gogo
* gogo
)
8201 if (this->hiter_type_
!= NULL
)
8202 return this->hiter_type_
;
8204 int64_t keysize
, valsize
;
8205 if (!this->key_type_
->backend_type_size(gogo
, &keysize
)
8206 || !this->val_type_
->backend_type_size(gogo
, &valsize
))
8208 go_assert(saw_errors());
8212 Type
* key_ptr_type
= Type::make_pointer_type(this->key_type_
);
8213 Type
* val_ptr_type
= Type::make_pointer_type(this->val_type_
);
8214 Type
* uint8_type
= Type::lookup_integer_type("uint8");
8215 Type
* uint8_ptr_type
= Type::make_pointer_type(uint8_type
);
8216 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8217 Type
* bucket_type
= this->bucket_type(gogo
, keysize
, valsize
);
8218 Type
* bucket_ptr_type
= Type::make_pointer_type(bucket_type
);
8219 Type
* hmap_type
= this->hmap_type(bucket_type
);
8220 Type
* hmap_ptr_type
= Type::make_pointer_type(hmap_type
);
8221 Type
* void_ptr_type
= Type::make_pointer_type(Type::make_void_type());
8222 Type
* bool_type
= Type::lookup_bool_type();
8224 Struct_type
* ret
= make_builtin_struct_type(15,
8225 "key", key_ptr_type
,
8226 "val", val_ptr_type
,
8227 "t", uint8_ptr_type
,
8229 "buckets", bucket_ptr_type
,
8230 "bptr", bucket_ptr_type
,
8231 "overflow", void_ptr_type
,
8232 "oldoverflow", void_ptr_type
,
8233 "startBucket", uintptr_type
,
8234 "offset", uint8_type
,
8235 "wrapped", bool_type
,
8238 "bucket", uintptr_type
,
8239 "checkBucket", uintptr_type
);
8240 ret
->set_is_struct_incomparable();
8241 this->hiter_type_
= ret
;
8245 // Reflection string for a map.
8248 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8250 ret
->append("map[");
8251 this->append_reflection(this->key_type_
, gogo
, ret
);
8253 this->append_reflection(this->val_type_
, gogo
, ret
);
8256 // Export a map type.
8259 Map_type::do_export(Export
* exp
) const
8261 exp
->write_c_string("map [");
8262 exp
->write_type(this->key_type_
);
8263 exp
->write_c_string("] ");
8264 exp
->write_type(this->val_type_
);
8267 // Import a map type.
8270 Map_type::do_import(Import
* imp
)
8272 imp
->require_c_string("map [");
8273 Type
* key_type
= imp
->read_type();
8274 imp
->require_c_string("] ");
8275 Type
* val_type
= imp
->read_type();
8276 return Type::make_map_type(key_type
, val_type
, imp
->location());
8282 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
8284 return new Map_type(key_type
, val_type
, location
);
8287 // Class Channel_type.
8292 Channel_type::do_verify()
8294 // We have no location for this error, but this is not something the
8295 // ordinary user will see.
8296 if (!this->element_type_
->in_heap())
8297 go_error_at(Linemap::unknown_location(),
8298 "chan of go:notinheap type not allowed");
8305 Channel_type::do_hash_for_method(Gogo
* gogo
) const
8307 unsigned int ret
= 0;
8308 if (this->may_send_
)
8310 if (this->may_receive_
)
8312 if (this->element_type_
!= NULL
)
8313 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
8317 // Whether this type is the same as T.
8320 Channel_type::is_identical(const Channel_type
* t
, Cmp_tags cmp_tags
,
8321 bool errors_are_identical
) const
8323 if (!Type::are_identical_cmp_tags(this->element_type(), t
->element_type(),
8324 cmp_tags
, errors_are_identical
, NULL
))
8326 return (this->may_send_
== t
->may_send_
8327 && this->may_receive_
== t
->may_receive_
);
8330 // Return the backend representation for a channel type. A channel is a pointer
8331 // to a __go_channel struct. The __go_channel struct is defined in
8332 // libgo/runtime/channel.h.
8335 Channel_type::do_get_backend(Gogo
* gogo
)
8337 static Btype
* backend_channel_type
;
8338 if (backend_channel_type
== NULL
)
8340 std::vector
<Backend::Btyped_identifier
> bfields
;
8341 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
8342 bt
= gogo
->backend()->named_type("__go_channel", bt
,
8343 Linemap::predeclared_location());
8344 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
8346 return backend_channel_type
;
8349 // Build a type descriptor for a channel type.
8352 Channel_type::make_chan_type_descriptor_type()
8357 Type
* tdt
= Type::make_type_descriptor_type();
8358 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
8360 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8363 Type::make_builtin_struct_type(3,
8366 "dir", uintptr_type
);
8368 ret
= Type::make_builtin_named_type("ChanType", sf
);
8374 // Build a type descriptor for a map type.
8377 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8379 Location bloc
= Linemap::predeclared_location();
8381 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
8383 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
8385 Expression_list
* vals
= new Expression_list();
8388 Struct_field_list::const_iterator p
= fields
->begin();
8389 go_assert(p
->is_field_name("_type"));
8390 vals
->push_back(this->type_descriptor_constructor(gogo
,
8391 RUNTIME_TYPE_KIND_CHAN
,
8395 go_assert(p
->is_field_name("elem"));
8396 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
8399 go_assert(p
->is_field_name("dir"));
8400 // These bits must match the ones in libgo/runtime/go-type.h.
8402 if (this->may_receive_
)
8404 if (this->may_send_
)
8406 vals
->push_back(Expression::make_integer_ul(val
, p
->type(), bloc
));
8409 go_assert(p
== fields
->end());
8411 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
8414 // Reflection string.
8417 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8419 if (!this->may_send_
)
8421 ret
->append("chan");
8422 if (!this->may_receive_
)
8424 ret
->push_back(' ');
8425 this->append_reflection(this->element_type_
, gogo
, ret
);
8431 Channel_type::do_export(Export
* exp
) const
8433 exp
->write_c_string("chan ");
8434 if (this->may_send_
&& !this->may_receive_
)
8435 exp
->write_c_string("-< ");
8436 else if (this->may_receive_
&& !this->may_send_
)
8437 exp
->write_c_string("<- ");
8438 exp
->write_type(this->element_type_
);
8444 Channel_type::do_import(Import
* imp
)
8446 imp
->require_c_string("chan ");
8450 if (imp
->match_c_string("-< "))
8454 may_receive
= false;
8456 else if (imp
->match_c_string("<- "))
8468 Type
* element_type
= imp
->read_type();
8470 return Type::make_channel_type(may_send
, may_receive
, element_type
);
8473 // Return the type to manage a select statement with ncases case
8474 // statements. A value of this type is allocated on the stack. This
8475 // must match the type hselect in libgo/go/runtime/select.go.
8478 Channel_type::select_type(int ncases
)
8480 Type
* unsafe_pointer_type
= Type::make_pointer_type(Type::make_void_type());
8481 Type
* uint16_type
= Type::lookup_integer_type("uint16");
8483 static Struct_type
* scase_type
;
8484 if (scase_type
== NULL
)
8486 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8487 Type
* uint64_type
= Type::lookup_integer_type("uint64");
8489 Type::make_builtin_struct_type(7,
8490 "elem", unsafe_pointer_type
,
8491 "chan", unsafe_pointer_type
,
8493 "kind", uint16_type
,
8494 "index", uint16_type
,
8495 "receivedp", unsafe_pointer_type
,
8496 "releasetime", uint64_type
);
8497 scase_type
->set_is_struct_incomparable();
8500 Expression
* ncases_expr
=
8501 Expression::make_integer_ul(ncases
, NULL
, Linemap::predeclared_location());
8502 Array_type
* scases
= Type::make_array_type(scase_type
, ncases_expr
);
8503 scases
->set_is_array_incomparable();
8504 Array_type
* order
= Type::make_array_type(uint16_type
, ncases_expr
);
8505 order
->set_is_array_incomparable();
8508 Type::make_builtin_struct_type(7,
8509 "tcase", uint16_type
,
8510 "ncase", uint16_type
,
8511 "pollorder", unsafe_pointer_type
,
8512 "lockorder", unsafe_pointer_type
,
8514 "lockorderarr", order
,
8515 "pollorderarr", order
);
8516 ret
->set_is_struct_incomparable();
8520 // Make a new channel type.
8523 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
8525 return new Channel_type(send
, receive
, element_type
);
8528 // Class Interface_type.
8530 // Return the list of methods.
8532 const Typed_identifier_list
*
8533 Interface_type::methods() const
8535 go_assert(this->methods_are_finalized_
|| saw_errors());
8536 return this->all_methods_
;
8539 // Return the number of methods.
8542 Interface_type::method_count() const
8544 go_assert(this->methods_are_finalized_
|| saw_errors());
8545 return this->all_methods_
== NULL
? 0 : this->all_methods_
->size();
8551 Interface_type::do_traverse(Traverse
* traverse
)
8553 Typed_identifier_list
* methods
= (this->methods_are_finalized_
8554 ? this->all_methods_
8555 : this->parse_methods_
);
8556 if (methods
== NULL
)
8557 return TRAVERSE_CONTINUE
;
8558 return methods
->traverse(traverse
);
8561 // Finalize the methods. This handles interface inheritance.
8564 Interface_type::finalize_methods()
8566 if (this->methods_are_finalized_
)
8568 this->methods_are_finalized_
= true;
8569 if (this->parse_methods_
== NULL
)
8572 this->all_methods_
= new Typed_identifier_list();
8573 this->all_methods_
->reserve(this->parse_methods_
->size());
8574 Typed_identifier_list inherit
;
8575 for (Typed_identifier_list::const_iterator pm
=
8576 this->parse_methods_
->begin();
8577 pm
!= this->parse_methods_
->end();
8580 const Typed_identifier
* p
= &*pm
;
8581 if (p
->name().empty())
8582 inherit
.push_back(*p
);
8583 else if (this->find_method(p
->name()) == NULL
)
8584 this->all_methods_
->push_back(*p
);
8586 go_error_at(p
->location(), "duplicate method %qs",
8587 Gogo::message_name(p
->name()).c_str());
8590 std::vector
<Named_type
*> seen
;
8591 seen
.reserve(inherit
.size());
8592 bool issued_recursive_error
= false;
8593 while (!inherit
.empty())
8595 Type
* t
= inherit
.back().type();
8596 Location tl
= inherit
.back().location();
8599 Interface_type
* it
= t
->interface_type();
8603 go_error_at(tl
, "interface contains embedded non-interface");
8608 if (!issued_recursive_error
)
8610 go_error_at(tl
, "invalid recursive interface");
8611 issued_recursive_error
= true;
8616 Named_type
* nt
= t
->named_type();
8617 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
8619 std::vector
<Named_type
*>::const_iterator q
;
8620 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
8624 go_error_at(tl
, "inherited interface loop");
8628 if (q
!= seen
.end())
8633 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
8634 if (imethods
== NULL
)
8636 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
8637 q
!= imethods
->end();
8640 if (q
->name().empty())
8641 inherit
.push_back(*q
);
8642 else if (this->find_method(q
->name()) == NULL
)
8643 this->all_methods_
->push_back(Typed_identifier(q
->name(),
8646 go_error_at(tl
, "inherited method %qs is ambiguous",
8647 Gogo::message_name(q
->name()).c_str());
8651 if (!this->all_methods_
->empty())
8652 this->all_methods_
->sort_by_name();
8655 delete this->all_methods_
;
8656 this->all_methods_
= NULL
;
8660 // Return the method NAME, or NULL.
8662 const Typed_identifier
*
8663 Interface_type::find_method(const std::string
& name
) const
8665 go_assert(this->methods_are_finalized_
);
8666 if (this->all_methods_
== NULL
)
8668 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8669 p
!= this->all_methods_
->end();
8671 if (p
->name() == name
)
8676 // Return the method index.
8679 Interface_type::method_index(const std::string
& name
) const
8681 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
8683 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8684 p
!= this->all_methods_
->end();
8686 if (p
->name() == name
)
8691 // Return whether NAME is an unexported method, for better error
8695 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
8697 go_assert(this->methods_are_finalized_
);
8698 if (this->all_methods_
== NULL
)
8700 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8701 p
!= this->all_methods_
->end();
8704 const std::string
& method_name(p
->name());
8705 if (Gogo::is_hidden_name(method_name
)
8706 && name
== Gogo::unpack_hidden_name(method_name
)
8707 && gogo
->pack_hidden_name(name
, false) != method_name
)
8713 // Whether this type is identical with T.
8716 Interface_type::is_identical(const Interface_type
* t
, Cmp_tags cmp_tags
,
8717 bool errors_are_identical
) const
8719 // If methods have not been finalized, then we are asking whether
8720 // func redeclarations are the same. This is an error, so for
8721 // simplicity we say they are never the same.
8722 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
8725 // We require the same methods with the same types. The methods
8726 // have already been sorted.
8727 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
8728 return this->all_methods_
== t
->all_methods_
;
8730 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
8733 Assume_identical
* hold_ai
= this->assume_identical_
;
8734 Assume_identical ai
;
8738 this->assume_identical_
= &ai
;
8740 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
8741 Typed_identifier_list::const_iterator p2
;
8742 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
8744 if (p1
== this->all_methods_
->end())
8746 if (p1
->name() != p2
->name()
8747 || !Type::are_identical_cmp_tags(p1
->type(), p2
->type(), cmp_tags
,
8748 errors_are_identical
, NULL
))
8752 this->assume_identical_
= hold_ai
;
8754 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
8757 // Return true if T1 and T2 are assumed to be identical during a type
8761 Interface_type::assume_identical(const Interface_type
* t1
,
8762 const Interface_type
* t2
) const
8764 for (Assume_identical
* p
= this->assume_identical_
;
8767 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
8772 // Whether we can assign the interface type T to this type. The types
8773 // are known to not be identical. An interface assignment is only
8774 // permitted if T is known to implement all methods in THIS.
8775 // Otherwise a type guard is required.
8778 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
8779 std::string
* reason
) const
8781 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
8782 if (this->all_methods_
== NULL
)
8784 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8785 p
!= this->all_methods_
->end();
8788 const Typed_identifier
* m
= t
->find_method(p
->name());
8794 snprintf(buf
, sizeof buf
,
8795 _("need explicit conversion; missing method %s%s%s"),
8796 go_open_quote(), Gogo::message_name(p
->name()).c_str(),
8798 reason
->assign(buf
);
8803 std::string subreason
;
8804 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
8808 std::string n
= Gogo::message_name(p
->name());
8809 size_t len
= 100 + n
.length() + subreason
.length();
8810 char* buf
= new char[len
];
8811 if (subreason
.empty())
8812 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
8813 go_open_quote(), n
.c_str(), go_close_quote());
8816 _("incompatible type for method %s%s%s (%s)"),
8817 go_open_quote(), n
.c_str(), go_close_quote(),
8819 reason
->assign(buf
);
8832 Interface_type::do_hash_for_method(Gogo
*) const
8834 go_assert(this->methods_are_finalized_
);
8835 unsigned int ret
= 0;
8836 if (this->all_methods_
!= NULL
)
8838 for (Typed_identifier_list::const_iterator p
=
8839 this->all_methods_
->begin();
8840 p
!= this->all_methods_
->end();
8843 ret
= Type::hash_string(p
->name(), ret
);
8844 // We don't use the method type in the hash, to avoid
8845 // infinite recursion if an interface method uses a type
8846 // which is an interface which inherits from the interface
8848 // type T interface { F() interface {T}}
8855 // Return true if T implements the interface. If it does not, and
8856 // REASON is not NULL, set *REASON to a useful error message.
8859 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
8861 go_assert(this->methods_are_finalized_
);
8862 if (this->all_methods_
== NULL
)
8865 bool is_pointer
= false;
8866 const Named_type
* nt
= t
->named_type();
8867 const Struct_type
* st
= t
->struct_type();
8868 // If we start with a named type, we don't dereference it to find
8872 const Type
* pt
= t
->points_to();
8875 // If T is a pointer to a named type, then we need to look at
8876 // the type to which it points.
8878 nt
= pt
->named_type();
8879 st
= pt
->struct_type();
8883 // If we have a named type, get the methods from it rather than from
8888 // Only named and struct types have methods.
8889 if (nt
== NULL
&& st
== NULL
)
8893 if (t
->points_to() != NULL
8894 && t
->points_to()->interface_type() != NULL
)
8895 reason
->assign(_("pointer to interface type has no methods"));
8897 reason
->assign(_("type has no methods"));
8902 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
8906 if (t
->points_to() != NULL
8907 && t
->points_to()->interface_type() != NULL
)
8908 reason
->assign(_("pointer to interface type has no methods"));
8910 reason
->assign(_("type has no methods"));
8915 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
8916 p
!= this->all_methods_
->end();
8919 bool is_ambiguous
= false;
8920 Method
* m
= (nt
!= NULL
8921 ? nt
->method_function(p
->name(), &is_ambiguous
)
8922 : st
->method_function(p
->name(), &is_ambiguous
));
8927 std::string n
= Gogo::message_name(p
->name());
8928 size_t len
= n
.length() + 100;
8929 char* buf
= new char[len
];
8931 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
8932 go_open_quote(), n
.c_str(), go_close_quote());
8934 snprintf(buf
, len
, _("missing method %s%s%s"),
8935 go_open_quote(), n
.c_str(), go_close_quote());
8936 reason
->assign(buf
);
8942 Function_type
*p_fn_type
= p
->type()->function_type();
8943 Function_type
* m_fn_type
= m
->type()->function_type();
8944 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
8945 std::string subreason
;
8946 if (!p_fn_type
->is_identical(m_fn_type
, true, COMPARE_TAGS
, true,
8951 std::string n
= Gogo::message_name(p
->name());
8952 size_t len
= 100 + n
.length() + subreason
.length();
8953 char* buf
= new char[len
];
8954 if (subreason
.empty())
8955 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
8956 go_open_quote(), n
.c_str(), go_close_quote());
8959 _("incompatible type for method %s%s%s (%s)"),
8960 go_open_quote(), n
.c_str(), go_close_quote(),
8962 reason
->assign(buf
);
8968 if (!is_pointer
&& !m
->is_value_method())
8972 std::string n
= Gogo::message_name(p
->name());
8973 size_t len
= 100 + n
.length();
8974 char* buf
= new char[len
];
8976 _("method %s%s%s requires a pointer receiver"),
8977 go_open_quote(), n
.c_str(), go_close_quote());
8978 reason
->assign(buf
);
8984 // If the magic //go:nointerface comment was used, the method
8985 // may not be used to implement interfaces.
8986 if (m
->nointerface())
8990 std::string n
= Gogo::message_name(p
->name());
8991 size_t len
= 100 + n
.length();
8992 char* buf
= new char[len
];
8994 _("method %s%s%s is marked go:nointerface"),
8995 go_open_quote(), n
.c_str(), go_close_quote());
8996 reason
->assign(buf
);
9006 // Return the backend representation of the empty interface type. We
9007 // use the same struct for all empty interfaces.
9010 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
9012 static Btype
* empty_interface_type
;
9013 if (empty_interface_type
== NULL
)
9015 std::vector
<Backend::Btyped_identifier
> bfields(2);
9017 Location bloc
= Linemap::predeclared_location();
9019 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9020 bfields
[0].name
= "__type_descriptor";
9021 bfields
[0].btype
= pdt
->get_backend(gogo
);
9022 bfields
[0].location
= bloc
;
9024 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9025 bfields
[1].name
= "__object";
9026 bfields
[1].btype
= vt
->get_backend(gogo
);
9027 bfields
[1].location
= bloc
;
9029 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
9031 return empty_interface_type
;
9034 // Return a pointer to the backend representation of the method table.
9037 Interface_type::get_backend_methods(Gogo
* gogo
)
9039 if (this->bmethods_
!= NULL
&& !this->bmethods_is_placeholder_
)
9040 return this->bmethods_
;
9042 Location loc
= this->location();
9044 std::vector
<Backend::Btyped_identifier
>
9045 mfields(this->all_methods_
->size() + 1);
9047 Type
* pdt
= Type::make_type_descriptor_ptr_type();
9048 mfields
[0].name
= "__type_descriptor";
9049 mfields
[0].btype
= pdt
->get_backend(gogo
);
9050 mfields
[0].location
= loc
;
9052 std::string last_name
= "";
9054 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
9055 p
!= this->all_methods_
->end();
9058 // The type of the method in Go only includes the parameters.
9059 // The actual method also has a receiver, which is always a
9060 // pointer. We need to add that pointer type here in order to
9061 // generate the correct type for the backend.
9062 Function_type
* ft
= p
->type()->function_type();
9063 go_assert(ft
->receiver() == NULL
);
9065 const Typed_identifier_list
* params
= ft
->parameters();
9066 Typed_identifier_list
* mparams
= new Typed_identifier_list();
9068 mparams
->reserve(params
->size() + 1);
9069 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9070 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
9073 for (Typed_identifier_list::const_iterator pp
= params
->begin();
9074 pp
!= params
->end();
9076 mparams
->push_back(*pp
);
9079 Typed_identifier_list
* mresults
= (ft
->results() == NULL
9081 : ft
->results()->copy());
9082 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
9085 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
9086 mfields
[i
].btype
= mft
->get_backend_fntype(gogo
);
9087 mfields
[i
].location
= loc
;
9089 // Sanity check: the names should be sorted.
9090 go_assert(Gogo::unpack_hidden_name(p
->name())
9091 > Gogo::unpack_hidden_name(last_name
));
9092 last_name
= p
->name();
9095 Btype
* st
= gogo
->backend()->struct_type(mfields
);
9096 Btype
* ret
= gogo
->backend()->pointer_type(st
);
9098 if (this->bmethods_
!= NULL
&& this->bmethods_is_placeholder_
)
9099 gogo
->backend()->set_placeholder_pointer_type(this->bmethods_
, ret
);
9100 this->bmethods_
= ret
;
9101 this->bmethods_is_placeholder_
= false;
9105 // Return a placeholder for the pointer to the backend methods table.
9108 Interface_type::get_backend_methods_placeholder(Gogo
* gogo
)
9110 if (this->bmethods_
== NULL
)
9112 Location loc
= this->location();
9113 this->bmethods_
= gogo
->backend()->placeholder_pointer_type("", loc
,
9115 this->bmethods_is_placeholder_
= true;
9117 return this->bmethods_
;
9120 // Return the fields of a non-empty interface type. This is not
9121 // declared in types.h so that types.h doesn't have to #include
9125 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
9126 bool use_placeholder
,
9127 std::vector
<Backend::Btyped_identifier
>* bfields
)
9129 Location loc
= type
->location();
9133 (*bfields
)[0].name
= "__methods";
9134 (*bfields
)[0].btype
= (use_placeholder
9135 ? type
->get_backend_methods_placeholder(gogo
)
9136 : type
->get_backend_methods(gogo
));
9137 (*bfields
)[0].location
= loc
;
9139 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
9140 (*bfields
)[1].name
= "__object";
9141 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
9142 (*bfields
)[1].location
= Linemap::predeclared_location();
9145 // Return the backend representation for an interface type. An interface is a
9146 // pointer to a struct. The struct has three fields. The first field is a
9147 // pointer to the type descriptor for the dynamic type of the object.
9148 // The second field is a pointer to a table of methods for the
9149 // interface to be used with the object. The third field is the value
9150 // of the object itself.
9153 Interface_type::do_get_backend(Gogo
* gogo
)
9155 if (this->is_empty())
9156 return Interface_type::get_backend_empty_interface_type(gogo
);
9159 if (this->interface_btype_
!= NULL
)
9160 return this->interface_btype_
;
9161 this->interface_btype_
=
9162 gogo
->backend()->placeholder_struct_type("", this->location_
);
9163 std::vector
<Backend::Btyped_identifier
> bfields
;
9164 get_backend_interface_fields(gogo
, this, false, &bfields
);
9165 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
9167 this->interface_btype_
= gogo
->backend()->error_type();
9168 return this->interface_btype_
;
9172 // Finish the backend representation of the methods.
9175 Interface_type::finish_backend_methods(Gogo
* gogo
)
9177 if (!this->is_empty())
9179 const Typed_identifier_list
* methods
= this->methods();
9180 if (methods
!= NULL
)
9182 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9183 p
!= methods
->end();
9185 p
->type()->get_backend(gogo
);
9188 // Getting the backend methods now will set the placeholder
9190 this->get_backend_methods(gogo
);
9194 // The type of an interface type descriptor.
9197 Interface_type::make_interface_type_descriptor_type()
9202 Type
* tdt
= Type::make_type_descriptor_type();
9203 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
9205 Type
* string_type
= Type::lookup_string_type();
9206 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
9209 Type::make_builtin_struct_type(3,
9210 "name", pointer_string_type
,
9211 "pkgPath", pointer_string_type
,
9214 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
9216 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
9218 Struct_type
* s
= Type::make_builtin_struct_type(2,
9220 "methods", slice_nsm
);
9222 ret
= Type::make_builtin_named_type("InterfaceType", s
);
9228 // Build a type descriptor for an interface type.
9231 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9233 Location bloc
= Linemap::predeclared_location();
9235 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
9237 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
9239 Expression_list
* ivals
= new Expression_list();
9242 Struct_field_list::const_iterator pif
= ifields
->begin();
9243 go_assert(pif
->is_field_name("_type"));
9244 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
9245 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
9249 go_assert(pif
->is_field_name("methods"));
9251 Expression_list
* methods
= new Expression_list();
9252 if (this->all_methods_
!= NULL
)
9254 Type
* elemtype
= pif
->type()->array_type()->element_type();
9256 methods
->reserve(this->all_methods_
->size());
9257 for (Typed_identifier_list::const_iterator pm
=
9258 this->all_methods_
->begin();
9259 pm
!= this->all_methods_
->end();
9262 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
9264 Expression_list
* mvals
= new Expression_list();
9267 Struct_field_list::const_iterator pmf
= mfields
->begin();
9268 go_assert(pmf
->is_field_name("name"));
9269 std::string s
= Gogo::unpack_hidden_name(pm
->name());
9270 Expression
* e
= Expression::make_string(s
, bloc
);
9271 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9274 go_assert(pmf
->is_field_name("pkgPath"));
9275 if (!Gogo::is_hidden_name(pm
->name()))
9276 mvals
->push_back(Expression::make_nil(bloc
));
9279 s
= Gogo::hidden_name_pkgpath(pm
->name());
9280 e
= Expression::make_string(s
, bloc
);
9281 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
9285 go_assert(pmf
->is_field_name("typ"));
9286 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
9289 go_assert(pmf
== mfields
->end());
9291 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
9293 methods
->push_back(e
);
9297 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
9301 go_assert(pif
== ifields
->end());
9303 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
9306 // Reflection string.
9309 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9311 ret
->append("interface {");
9312 const Typed_identifier_list
* methods
= this->parse_methods_
;
9313 if (methods
!= NULL
)
9315 ret
->push_back(' ');
9316 for (Typed_identifier_list::const_iterator p
= methods
->begin();
9317 p
!= methods
->end();
9320 if (p
!= methods
->begin())
9322 if (p
->name().empty())
9323 this->append_reflection(p
->type(), gogo
, ret
);
9326 if (!Gogo::is_hidden_name(p
->name()))
9327 ret
->append(p
->name());
9328 else if (gogo
->pkgpath_from_option())
9329 ret
->append(p
->name().substr(1));
9332 // If no -fgo-pkgpath option, backward compatibility
9333 // for how this used to work before -fgo-pkgpath was
9335 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
9336 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
9337 ret
->push_back('.');
9338 ret
->append(Gogo::unpack_hidden_name(p
->name()));
9340 std::string sub
= p
->type()->reflection(gogo
);
9341 go_assert(sub
.compare(0, 4, "func") == 0);
9342 sub
= sub
.substr(4);
9346 ret
->push_back(' ');
9354 Interface_type::do_export(Export
* exp
) const
9356 exp
->write_c_string("interface { ");
9358 const Typed_identifier_list
* methods
= this->parse_methods_
;
9359 if (methods
!= NULL
)
9361 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
9362 pm
!= methods
->end();
9365 if (pm
->name().empty())
9367 exp
->write_c_string("? ");
9368 exp
->write_type(pm
->type());
9372 exp
->write_string(pm
->name());
9373 exp
->write_c_string(" (");
9375 const Function_type
* fntype
= pm
->type()->function_type();
9378 const Typed_identifier_list
* parameters
= fntype
->parameters();
9379 if (parameters
!= NULL
)
9381 bool is_varargs
= fntype
->is_varargs();
9382 for (Typed_identifier_list::const_iterator pp
=
9383 parameters
->begin();
9384 pp
!= parameters
->end();
9390 exp
->write_c_string(", ");
9391 exp
->write_name(pp
->name());
9392 exp
->write_c_string(" ");
9393 if (!is_varargs
|| pp
+ 1 != parameters
->end())
9394 exp
->write_type(pp
->type());
9397 exp
->write_c_string("...");
9398 Type
*pptype
= pp
->type();
9399 exp
->write_type(pptype
->array_type()->element_type());
9404 exp
->write_c_string(")");
9406 const Typed_identifier_list
* results
= fntype
->results();
9407 if (results
!= NULL
)
9409 exp
->write_c_string(" ");
9410 if (results
->size() == 1 && results
->begin()->name().empty())
9411 exp
->write_type(results
->begin()->type());
9415 exp
->write_c_string("(");
9416 for (Typed_identifier_list::const_iterator p
=
9418 p
!= results
->end();
9424 exp
->write_c_string(", ");
9425 exp
->write_name(p
->name());
9426 exp
->write_c_string(" ");
9427 exp
->write_type(p
->type());
9429 exp
->write_c_string(")");
9434 exp
->write_c_string("; ");
9438 exp
->write_c_string("}");
9441 // Import an interface type.
9444 Interface_type::do_import(Import
* imp
)
9446 imp
->require_c_string("interface { ");
9448 Typed_identifier_list
* methods
= new Typed_identifier_list
;
9449 while (imp
->peek_char() != '}')
9451 std::string name
= imp
->read_identifier();
9455 imp
->require_c_string(" ");
9456 Type
* t
= imp
->read_type();
9457 methods
->push_back(Typed_identifier("", t
, imp
->location()));
9458 imp
->require_c_string("; ");
9462 imp
->require_c_string(" (");
9464 Typed_identifier_list
* parameters
;
9465 bool is_varargs
= false;
9466 if (imp
->peek_char() == ')')
9470 parameters
= new Typed_identifier_list
;
9473 std::string name
= imp
->read_name();
9474 imp
->require_c_string(" ");
9476 if (imp
->match_c_string("..."))
9482 Type
* ptype
= imp
->read_type();
9484 ptype
= Type::make_array_type(ptype
, NULL
);
9485 parameters
->push_back(Typed_identifier(name
, ptype
,
9487 if (imp
->peek_char() != ',')
9489 go_assert(!is_varargs
);
9490 imp
->require_c_string(", ");
9493 imp
->require_c_string(")");
9495 Typed_identifier_list
* results
;
9496 if (imp
->peek_char() != ' ')
9500 results
= new Typed_identifier_list
;
9502 if (imp
->peek_char() != '(')
9504 Type
* rtype
= imp
->read_type();
9505 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
9512 std::string name
= imp
->read_name();
9513 imp
->require_c_string(" ");
9514 Type
* rtype
= imp
->read_type();
9515 results
->push_back(Typed_identifier(name
, rtype
,
9517 if (imp
->peek_char() != ',')
9519 imp
->require_c_string(", ");
9521 imp
->require_c_string(")");
9525 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
9529 fntype
->set_is_varargs();
9530 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
9532 imp
->require_c_string("; ");
9535 imp
->require_c_string("}");
9537 if (methods
->empty())
9543 Interface_type
* ret
= Type::make_interface_type(methods
, imp
->location());
9544 ret
->package_
= imp
->package();
9548 // Make an interface type.
9551 Type::make_interface_type(Typed_identifier_list
* methods
,
9554 return new Interface_type(methods
, location
);
9557 // Make an empty interface type.
9560 Type::make_empty_interface_type(Location location
)
9562 Interface_type
* ret
= new Interface_type(NULL
, location
);
9563 ret
->finalize_methods();
9569 // Bind a method to an object.
9572 Method::bind_method(Expression
* expr
, Location location
) const
9574 if (this->stub_
== NULL
)
9576 // When there is no stub object, the binding is determined by
9578 return this->do_bind_method(expr
, location
);
9580 return Expression::make_bound_method(expr
, this, this->stub_
, location
);
9583 // Return the named object associated with a method. This may only be
9584 // called after methods are finalized.
9587 Method::named_object() const
9589 if (this->stub_
!= NULL
)
9591 return this->do_named_object();
9594 // Class Named_method.
9596 // The type of the method.
9599 Named_method::do_type() const
9601 if (this->named_object_
->is_function())
9602 return this->named_object_
->func_value()->type();
9603 else if (this->named_object_
->is_function_declaration())
9604 return this->named_object_
->func_declaration_value()->type();
9609 // Return the location of the method receiver.
9612 Named_method::do_receiver_location() const
9614 return this->do_type()->receiver()->location();
9617 // Bind a method to an object.
9620 Named_method::do_bind_method(Expression
* expr
, Location location
) const
9622 Named_object
* no
= this->named_object_
;
9623 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, this,
9625 // If this is not a local method, and it does not use a stub, then
9626 // the real method expects a different type. We need to cast the
9628 if (this->depth() > 0 && !this->needs_stub_method())
9630 Function_type
* ftype
= this->do_type();
9631 go_assert(ftype
->is_method());
9632 Type
* frtype
= ftype
->receiver()->type();
9633 bme
->set_first_argument_type(frtype
);
9638 // Return whether this method should not participate in interfaces.
9641 Named_method::do_nointerface() const
9643 Named_object
* no
= this->named_object_
;
9644 return no
->is_function() && no
->func_value()->nointerface();
9647 // Class Interface_method.
9649 // Bind a method to an object.
9652 Interface_method::do_bind_method(Expression
* expr
,
9653 Location location
) const
9655 return Expression::make_interface_field_reference(expr
, this->name_
,
9661 // Insert a new method. Return true if it was inserted, false
9665 Methods::insert(const std::string
& name
, Method
* m
)
9667 std::pair
<Method_map::iterator
, bool> ins
=
9668 this->methods_
.insert(std::make_pair(name
, m
));
9673 Method
* old_method
= ins
.first
->second
;
9674 if (m
->depth() < old_method
->depth())
9677 ins
.first
->second
= m
;
9682 if (m
->depth() == old_method
->depth())
9683 old_method
->set_is_ambiguous();
9689 // Return the number of unambiguous methods.
9692 Methods::count() const
9695 for (Method_map::const_iterator p
= this->methods_
.begin();
9696 p
!= this->methods_
.end();
9698 if (!p
->second
->is_ambiguous())
9703 // Class Named_type.
9705 // Return the name of the type.
9708 Named_type::name() const
9710 return this->named_object_
->name();
9713 // Return the name of the type to use in an error message.
9716 Named_type::message_name() const
9718 return this->named_object_
->message_name();
9721 // Return the base type for this type. We have to be careful about
9722 // circular type definitions, which are invalid but may be seen here.
9725 Named_type::named_base()
9730 Type
* ret
= this->type_
->base();
9731 this->seen_
= false;
9736 Named_type::named_base() const
9741 const Type
* ret
= this->type_
->base();
9742 this->seen_
= false;
9746 // Return whether this is an error type. We have to be careful about
9747 // circular type definitions, which are invalid but may be seen here.
9750 Named_type::is_named_error_type() const
9755 bool ret
= this->type_
->is_error_type();
9756 this->seen_
= false;
9760 // Whether this type is comparable. We have to be careful about
9761 // circular type definitions.
9764 Named_type::named_type_is_comparable(std::string
* reason
) const
9769 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
9770 this->type_
, reason
);
9771 this->seen_
= false;
9775 // Add a method to this type.
9778 Named_type::add_method(const std::string
& name
, Function
* function
)
9780 go_assert(!this->is_alias_
);
9781 if (this->local_methods_
== NULL
)
9782 this->local_methods_
= new Bindings(NULL
);
9783 return this->local_methods_
->add_function(name
, NULL
, function
);
9786 // Add a method declaration to this type.
9789 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
9790 Function_type
* type
,
9793 go_assert(!this->is_alias_
);
9794 if (this->local_methods_
== NULL
)
9795 this->local_methods_
= new Bindings(NULL
);
9796 return this->local_methods_
->add_function_declaration(name
, package
, type
,
9800 // Add an existing method to this type.
9803 Named_type::add_existing_method(Named_object
* no
)
9805 go_assert(!this->is_alias_
);
9806 if (this->local_methods_
== NULL
)
9807 this->local_methods_
= new Bindings(NULL
);
9808 this->local_methods_
->add_named_object(no
);
9811 // Look for a local method NAME, and returns its named object, or NULL
9815 Named_type::find_local_method(const std::string
& name
) const
9817 if (this->is_error_
)
9819 if (this->is_alias_
)
9821 Named_type
* nt
= this->type_
->named_type();
9824 if (this->seen_alias_
)
9826 this->seen_alias_
= true;
9827 Named_object
* ret
= nt
->find_local_method(name
);
9828 this->seen_alias_
= false;
9833 if (this->local_methods_
== NULL
)
9835 return this->local_methods_
->lookup(name
);
9838 // Return the list of local methods.
9841 Named_type::local_methods() const
9843 if (this->is_error_
)
9845 if (this->is_alias_
)
9847 Named_type
* nt
= this->type_
->named_type();
9850 if (this->seen_alias_
)
9852 this->seen_alias_
= true;
9853 const Bindings
* ret
= nt
->local_methods();
9854 this->seen_alias_
= false;
9859 return this->local_methods_
;
9862 // Return whether NAME is an unexported field or method, for better
9866 Named_type::is_unexported_local_method(Gogo
* gogo
,
9867 const std::string
& name
) const
9869 if (this->is_error_
)
9871 if (this->is_alias_
)
9873 Named_type
* nt
= this->type_
->named_type();
9876 if (this->seen_alias_
)
9878 this->seen_alias_
= true;
9879 bool ret
= nt
->is_unexported_local_method(gogo
, name
);
9880 this->seen_alias_
= false;
9885 Bindings
* methods
= this->local_methods_
;
9886 if (methods
!= NULL
)
9888 for (Bindings::const_declarations_iterator p
=
9889 methods
->begin_declarations();
9890 p
!= methods
->end_declarations();
9893 if (Gogo::is_hidden_name(p
->first
)
9894 && name
== Gogo::unpack_hidden_name(p
->first
)
9895 && gogo
->pack_hidden_name(name
, false) != p
->first
)
9902 // Build the complete list of methods for this type, which means
9903 // recursively including all methods for anonymous fields. Create all
9907 Named_type::finalize_methods(Gogo
* gogo
)
9909 if (this->is_alias_
)
9911 if (this->all_methods_
!= NULL
)
9914 if (this->local_methods_
!= NULL
9915 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
9917 const Bindings
* lm
= this->local_methods_
;
9918 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
9919 p
!= lm
->end_declarations();
9921 go_error_at(p
->second
->location(),
9922 "invalid pointer or interface receiver type");
9923 delete this->local_methods_
;
9924 this->local_methods_
= NULL
;
9928 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
9931 // Return whether this type has any methods.
9934 Named_type::has_any_methods() const
9936 if (this->is_error_
)
9938 if (this->is_alias_
)
9940 if (this->type_
->named_type() != NULL
)
9942 if (this->seen_alias_
)
9944 this->seen_alias_
= true;
9945 bool ret
= this->type_
->named_type()->has_any_methods();
9946 this->seen_alias_
= false;
9949 if (this->type_
->struct_type() != NULL
)
9950 return this->type_
->struct_type()->has_any_methods();
9953 return this->all_methods_
!= NULL
;
9956 // Return the methods for this type.
9959 Named_type::methods() const
9961 if (this->is_error_
)
9963 if (this->is_alias_
)
9965 if (this->type_
->named_type() != NULL
)
9967 if (this->seen_alias_
)
9969 this->seen_alias_
= true;
9970 const Methods
* ret
= this->type_
->named_type()->methods();
9971 this->seen_alias_
= false;
9974 if (this->type_
->struct_type() != NULL
)
9975 return this->type_
->struct_type()->methods();
9978 return this->all_methods_
;
9981 // Return the method NAME, or NULL if there isn't one or if it is
9982 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
9986 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
9988 if (this->is_error_
)
9990 if (this->is_alias_
)
9992 if (is_ambiguous
!= NULL
)
9993 *is_ambiguous
= false;
9994 if (this->type_
->named_type() != NULL
)
9996 if (this->seen_alias_
)
9998 this->seen_alias_
= true;
9999 Named_type
* nt
= this->type_
->named_type();
10000 Method
* ret
= nt
->method_function(name
, is_ambiguous
);
10001 this->seen_alias_
= false;
10004 if (this->type_
->struct_type() != NULL
)
10005 return this->type_
->struct_type()->method_function(name
, is_ambiguous
);
10008 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
10011 // Return a pointer to the interface method table for this type for
10012 // the interface INTERFACE. IS_POINTER is true if this is for a
10013 // pointer to THIS.
10016 Named_type::interface_method_table(Interface_type
* interface
, bool is_pointer
)
10018 if (this->is_error_
)
10019 return Expression::make_error(this->location_
);
10020 if (this->is_alias_
)
10022 if (this->type_
->named_type() != NULL
)
10024 if (this->seen_alias_
)
10025 return Expression::make_error(this->location_
);
10026 this->seen_alias_
= true;
10027 Named_type
* nt
= this->type_
->named_type();
10028 Expression
* ret
= nt
->interface_method_table(interface
, is_pointer
);
10029 this->seen_alias_
= false;
10032 if (this->type_
->struct_type() != NULL
)
10033 return this->type_
->struct_type()->interface_method_table(interface
,
10037 return Type::interface_method_table(this, interface
, is_pointer
,
10038 &this->interface_method_tables_
,
10039 &this->pointer_interface_method_tables_
);
10042 // Look for a use of a complete type within another type. This is
10043 // used to check that we don't try to use a type within itself.
10045 class Find_type_use
: public Traverse
10048 Find_type_use(Named_type
* find_type
)
10049 : Traverse(traverse_types
),
10050 find_type_(find_type
), found_(false)
10053 // Whether we found the type.
10056 { return this->found_
; }
10063 // The type we are looking for.
10064 Named_type
* find_type_
;
10065 // Whether we found the type.
10069 // Check for FIND_TYPE in TYPE.
10072 Find_type_use::type(Type
* type
)
10074 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
10076 this->found_
= true;
10077 return TRAVERSE_EXIT
;
10080 // It's OK if we see a reference to the type in any type which is
10081 // essentially a pointer: a pointer, a slice, a function, a map, or
10083 if (type
->points_to() != NULL
10084 || type
->is_slice_type()
10085 || type
->function_type() != NULL
10086 || type
->map_type() != NULL
10087 || type
->channel_type() != NULL
)
10088 return TRAVERSE_SKIP_COMPONENTS
;
10090 // For an interface, a reference to the type in a method type should
10091 // be ignored, but we have to consider direct inheritance. When
10092 // this is called, there may be cases of direct inheritance
10093 // represented as a method with no name.
10094 if (type
->interface_type() != NULL
)
10096 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
10097 if (methods
!= NULL
)
10099 for (Typed_identifier_list::const_iterator p
= methods
->begin();
10100 p
!= methods
->end();
10103 if (p
->name().empty())
10105 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
10106 return TRAVERSE_EXIT
;
10110 return TRAVERSE_SKIP_COMPONENTS
;
10113 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
10114 // to convert TYPE to the backend representation before we convert
10116 if (type
->named_type() != NULL
)
10118 switch (type
->base()->classification())
10120 case Type::TYPE_ERROR
:
10121 case Type::TYPE_BOOLEAN
:
10122 case Type::TYPE_INTEGER
:
10123 case Type::TYPE_FLOAT
:
10124 case Type::TYPE_COMPLEX
:
10125 case Type::TYPE_STRING
:
10126 case Type::TYPE_NIL
:
10129 case Type::TYPE_ARRAY
:
10130 case Type::TYPE_STRUCT
:
10131 this->find_type_
->add_dependency(type
->named_type());
10134 case Type::TYPE_NAMED
:
10135 case Type::TYPE_FORWARD
:
10136 go_assert(saw_errors());
10139 case Type::TYPE_VOID
:
10140 case Type::TYPE_SINK
:
10141 case Type::TYPE_FUNCTION
:
10142 case Type::TYPE_POINTER
:
10143 case Type::TYPE_CALL_MULTIPLE_RESULT
:
10144 case Type::TYPE_MAP
:
10145 case Type::TYPE_CHANNEL
:
10146 case Type::TYPE_INTERFACE
:
10152 return TRAVERSE_CONTINUE
;
10155 // Look for a circular reference of an alias.
10157 class Find_alias
: public Traverse
10160 Find_alias(Named_type
* find_type
)
10161 : Traverse(traverse_types
),
10162 find_type_(find_type
), found_(false)
10165 // Whether we found the type.
10168 { return this->found_
; }
10175 // The type we are looking for.
10176 Named_type
* find_type_
;
10177 // Whether we found the type.
10182 Find_alias::type(Type
* type
)
10184 Named_type
* nt
= type
->named_type();
10187 if (nt
== this->find_type_
)
10189 this->found_
= true;
10190 return TRAVERSE_EXIT
;
10193 // We started from `type T1 = T2`, where T1 is find_type_ and T2
10194 // is, perhaps indirectly, the parameter TYPE. If TYPE is not
10195 // an alias itself, it's OK if whatever T2 is defined as refers
10197 if (!nt
->is_alias())
10198 return TRAVERSE_SKIP_COMPONENTS
;
10201 return TRAVERSE_CONTINUE
;
10204 // Verify that a named type does not refer to itself.
10207 Named_type::do_verify()
10209 if (this->is_verified_
)
10211 this->is_verified_
= true;
10213 if (this->is_error_
)
10216 if (this->is_alias_
)
10218 Find_alias
find(this);
10219 Type::traverse(this->type_
, &find
);
10222 go_error_at(this->location_
, "invalid recursive alias %qs",
10223 this->message_name().c_str());
10224 this->is_error_
= true;
10229 Find_type_use
find(this);
10230 Type::traverse(this->type_
, &find
);
10233 go_error_at(this->location_
, "invalid recursive type %qs",
10234 this->message_name().c_str());
10235 this->is_error_
= true;
10239 // Check whether any of the local methods overloads an existing
10240 // struct field or interface method. We don't need to check the
10241 // list of methods against itself: that is handled by the Bindings
10243 if (this->local_methods_
!= NULL
)
10245 Struct_type
* st
= this->type_
->struct_type();
10248 for (Bindings::const_declarations_iterator p
=
10249 this->local_methods_
->begin_declarations();
10250 p
!= this->local_methods_
->end_declarations();
10253 const std::string
& name(p
->first
);
10254 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
10256 go_error_at(p
->second
->location(),
10257 "method %qs redeclares struct field name",
10258 Gogo::message_name(name
).c_str());
10267 // Return whether this type is or contains a pointer.
10270 Named_type::do_has_pointer() const
10274 this->seen_
= true;
10275 bool ret
= this->type_
->has_pointer();
10276 this->seen_
= false;
10280 // Return whether comparisons for this type can use the identity
10284 Named_type::do_compare_is_identity(Gogo
* gogo
)
10286 // We don't use this->seen_ here because compare_is_identity may
10287 // call base() later, and that will mess up if seen_ is set here.
10288 if (this->seen_in_compare_is_identity_
)
10290 this->seen_in_compare_is_identity_
= true;
10291 bool ret
= this->type_
->compare_is_identity(gogo
);
10292 this->seen_in_compare_is_identity_
= false;
10296 // Return whether this type is reflexive--whether it is always equal
10300 Named_type::do_is_reflexive()
10302 if (this->seen_in_compare_is_identity_
)
10304 this->seen_in_compare_is_identity_
= true;
10305 bool ret
= this->type_
->is_reflexive();
10306 this->seen_in_compare_is_identity_
= false;
10310 // Return whether this type needs a key update when used as a map key.
10313 Named_type::do_needs_key_update()
10315 if (this->seen_in_compare_is_identity_
)
10317 this->seen_in_compare_is_identity_
= true;
10318 bool ret
= this->type_
->needs_key_update();
10319 this->seen_in_compare_is_identity_
= false;
10323 // Return a hash code. This is used for method lookup. We simply
10324 // hash on the name itself.
10327 Named_type::do_hash_for_method(Gogo
* gogo
) const
10329 if (this->is_error_
)
10332 // Aliases are handled in Type::hash_for_method.
10333 go_assert(!this->is_alias_
);
10335 const std::string
& name(this->named_object()->name());
10336 unsigned int ret
= Type::hash_string(name
, 0);
10338 // GOGO will be NULL here when called from Type_hash_identical.
10339 // That is OK because that is only used for internal hash tables
10340 // where we are going to be comparing named types for equality. In
10341 // other cases, which are cases where the runtime is going to
10342 // compare hash codes to see if the types are the same, we need to
10343 // include the pkgpath in the hash.
10344 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
10346 const Package
* package
= this->named_object()->package();
10347 if (package
== NULL
)
10348 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
10350 ret
= Type::hash_string(package
->pkgpath(), ret
);
10356 // Convert a named type to the backend representation. In order to
10357 // get dependencies right, we fill in a dummy structure for this type,
10358 // then convert all the dependencies, then complete this type. When
10359 // this function is complete, the size of the type is known.
10362 Named_type::convert(Gogo
* gogo
)
10364 if (this->is_error_
|| this->is_converted_
)
10367 this->create_placeholder(gogo
);
10369 // If we are called to turn unsafe.Sizeof into a constant, we may
10370 // not have verified the type yet. We have to make sure it is
10371 // verified, since that sets the list of dependencies.
10374 // Convert all the dependencies. If they refer indirectly back to
10375 // this type, they will pick up the intermediate representation we just
10377 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
10378 p
!= this->dependencies_
.end();
10380 (*p
)->convert(gogo
);
10382 // Complete this type.
10383 Btype
* bt
= this->named_btype_
;
10384 Type
* base
= this->type_
->base();
10385 switch (base
->classification())
10400 case TYPE_FUNCTION
:
10402 // The size of these types is already correct. We don't worry
10403 // about filling them in until later, when we also track
10404 // circular references.
10409 std::vector
<Backend::Btyped_identifier
> bfields
;
10410 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
10412 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10413 bt
= gogo
->backend()->error_type();
10418 // Slice types were completed in create_placeholder.
10419 if (!base
->is_slice_type())
10421 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
10422 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
10423 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
10424 bt
= gogo
->backend()->error_type();
10428 case TYPE_INTERFACE
:
10429 // Interface types were completed in create_placeholder.
10437 case TYPE_CALL_MULTIPLE_RESULT
:
10443 this->named_btype_
= bt
;
10444 this->is_converted_
= true;
10445 this->is_placeholder_
= false;
10448 // Create the placeholder for a named type. This is the first step in
10449 // converting to the backend representation.
10452 Named_type::create_placeholder(Gogo
* gogo
)
10454 if (this->is_error_
)
10455 this->named_btype_
= gogo
->backend()->error_type();
10457 if (this->named_btype_
!= NULL
)
10460 // Create the structure for this type. Note that because we call
10461 // base() here, we don't attempt to represent a named type defined
10462 // as another named type. Instead both named types will point to
10463 // different base representations.
10464 Type
* base
= this->type_
->base();
10466 bool set_name
= true;
10467 switch (base
->classification())
10470 this->is_error_
= true;
10471 this->named_btype_
= gogo
->backend()->error_type();
10481 // These are simple basic types, we can just create them
10483 bt
= Type::get_named_base_btype(gogo
, base
);
10488 // All maps and channels have the same backend representation.
10489 bt
= Type::get_named_base_btype(gogo
, base
);
10492 case TYPE_FUNCTION
:
10495 bool for_function
= base
->classification() == TYPE_FUNCTION
;
10496 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
10504 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10506 this->is_placeholder_
= true;
10511 if (base
->is_slice_type())
10512 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10516 bt
= gogo
->backend()->placeholder_array_type(this->name(),
10518 this->is_placeholder_
= true;
10523 case TYPE_INTERFACE
:
10524 if (base
->interface_type()->is_empty())
10525 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
10528 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
10536 case TYPE_CALL_MULTIPLE_RESULT
:
10543 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
10545 this->named_btype_
= bt
;
10547 if (base
->is_slice_type())
10549 // We do not record slices as dependencies of other types,
10550 // because we can fill them in completely here with the final
10552 std::vector
<Backend::Btyped_identifier
> bfields
;
10553 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
10554 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10555 this->named_btype_
= gogo
->backend()->error_type();
10557 else if (base
->interface_type() != NULL
10558 && !base
->interface_type()->is_empty())
10560 // We do not record interfaces as dependencies of other types,
10561 // because we can fill them in completely here with the final
10563 std::vector
<Backend::Btyped_identifier
> bfields
;
10564 get_backend_interface_fields(gogo
, base
->interface_type(), true,
10566 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
10567 this->named_btype_
= gogo
->backend()->error_type();
10571 // Get the backend representation for a named type.
10574 Named_type::do_get_backend(Gogo
* gogo
)
10576 if (this->is_error_
)
10577 return gogo
->backend()->error_type();
10579 Btype
* bt
= this->named_btype_
;
10581 if (!gogo
->named_types_are_converted())
10583 // We have not completed converting named types. NAMED_BTYPE_
10584 // is a placeholder and we shouldn't do anything further.
10588 // We don't build dependencies for types whose sizes do not
10589 // change or are not relevant, so we may see them here while
10590 // converting types.
10591 this->create_placeholder(gogo
);
10592 bt
= this->named_btype_
;
10593 go_assert(bt
!= NULL
);
10597 // We are not converting types. This should only be called if the
10598 // type has already been converted.
10599 if (!this->is_converted_
)
10601 go_assert(saw_errors());
10602 return gogo
->backend()->error_type();
10605 go_assert(bt
!= NULL
);
10607 // Complete the backend representation.
10608 Type
* base
= this->type_
->base();
10610 switch (base
->classification())
10613 return gogo
->backend()->error_type();
10627 if (!this->seen_in_get_backend_
)
10629 this->seen_in_get_backend_
= true;
10630 base
->struct_type()->finish_backend_fields(gogo
);
10631 this->seen_in_get_backend_
= false;
10636 if (!this->seen_in_get_backend_
)
10638 this->seen_in_get_backend_
= true;
10639 base
->array_type()->finish_backend_element(gogo
);
10640 this->seen_in_get_backend_
= false;
10644 case TYPE_INTERFACE
:
10645 if (!this->seen_in_get_backend_
)
10647 this->seen_in_get_backend_
= true;
10648 base
->interface_type()->finish_backend_methods(gogo
);
10649 this->seen_in_get_backend_
= false;
10653 case TYPE_FUNCTION
:
10654 // Don't build a circular data structure. GENERIC can't handle
10656 if (this->seen_in_get_backend_
)
10658 this->is_circular_
= true;
10659 return gogo
->backend()->circular_pointer_type(bt
, true);
10661 this->seen_in_get_backend_
= true;
10662 bt1
= Type::get_named_base_btype(gogo
, base
);
10663 this->seen_in_get_backend_
= false;
10664 if (this->is_circular_
)
10665 bt1
= gogo
->backend()->circular_pointer_type(bt
, true);
10666 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10667 bt
= gogo
->backend()->error_type();
10671 // Don't build a circular data structure. GENERIC can't handle
10673 if (this->seen_in_get_backend_
)
10675 this->is_circular_
= true;
10676 return gogo
->backend()->circular_pointer_type(bt
, false);
10678 this->seen_in_get_backend_
= true;
10679 bt1
= Type::get_named_base_btype(gogo
, base
);
10680 this->seen_in_get_backend_
= false;
10681 if (this->is_circular_
)
10682 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
10683 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
10684 bt
= gogo
->backend()->error_type();
10689 case TYPE_CALL_MULTIPLE_RESULT
:
10698 // Build a type descriptor for a named type.
10701 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
10703 if (this->is_error_
)
10704 return Expression::make_error(this->location_
);
10705 if (name
== NULL
&& this->is_alias_
)
10707 if (this->seen_alias_
)
10708 return Expression::make_error(this->location_
);
10709 this->seen_alias_
= true;
10710 Expression
* ret
= this->type_
->type_descriptor(gogo
, NULL
);
10711 this->seen_alias_
= false;
10715 // If NAME is not NULL, then we don't really want the type
10716 // descriptor for this type; we want the descriptor for the
10717 // underlying type, giving it the name NAME.
10718 return this->named_type_descriptor(gogo
, this->type_
,
10719 name
== NULL
? this : name
);
10722 // Add to the reflection string. This is used mostly for the name of
10723 // the type used in a type descriptor, not for actual reflection
10727 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
10729 this->append_reflection_type_name(gogo
, false, ret
);
10732 // Add to the reflection string. For an alias we normally use the
10733 // real name, but if USE_ALIAS is true we use the alias name itself.
10736 Named_type::append_reflection_type_name(Gogo
* gogo
, bool use_alias
,
10737 std::string
* ret
) const
10739 if (this->is_error_
)
10741 if (this->is_alias_
&& !use_alias
)
10743 if (this->seen_alias_
)
10745 this->seen_alias_
= true;
10746 this->append_reflection(this->type_
, gogo
, ret
);
10747 this->seen_alias_
= false;
10750 if (!this->is_builtin())
10752 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
10753 // make a unique reflection string, so that the type
10754 // canonicalization in the reflect package will work. In order
10755 // to be compatible with the gc compiler, we put tabs into the
10756 // package path, so that the reflect methods can discard it.
10757 const Package
* package
= this->named_object_
->package();
10758 ret
->push_back('\t');
10759 ret
->append(package
!= NULL
10760 ? package
->pkgpath_symbol()
10761 : gogo
->pkgpath_symbol());
10762 ret
->push_back('\t');
10763 ret
->append(package
!= NULL
10764 ? package
->package_name()
10765 : gogo
->package_name());
10766 ret
->push_back('.');
10768 if (this->in_function_
!= NULL
)
10770 ret
->push_back('\t');
10771 const Typed_identifier
* rcvr
=
10772 this->in_function_
->func_value()->type()->receiver();
10775 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
10776 ret
->append(Gogo::unpack_hidden_name(rcvr_type
->name()));
10777 ret
->push_back('.');
10779 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
10780 ret
->push_back('$');
10781 if (this->in_function_index_
> 0)
10784 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
10786 ret
->push_back('$');
10788 ret
->push_back('\t');
10790 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
10793 // Export the type. This is called to export a global type.
10796 Named_type::export_named_type(Export
* exp
, const std::string
&) const
10798 // We don't need to write the name of the type here, because it will
10799 // be written by Export::write_type anyhow.
10800 exp
->write_c_string("type ");
10801 exp
->write_type(this);
10802 exp
->write_c_string(";\n");
10805 // Import a named type.
10808 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
10810 imp
->require_c_string("type ");
10811 Type
*type
= imp
->read_type();
10812 *ptype
= type
->named_type();
10813 go_assert(*ptype
!= NULL
);
10814 imp
->require_c_string(";\n");
10817 // Export the type when it is referenced by another type. In this
10818 // case Export::export_type will already have issued the name.
10821 Named_type::do_export(Export
* exp
) const
10823 exp
->write_type(this->type_
);
10825 // To save space, we only export the methods directly attached to
10827 Bindings
* methods
= this->local_methods_
;
10828 if (methods
== NULL
)
10831 exp
->write_c_string("\n");
10832 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
10833 p
!= methods
->end_definitions();
10836 exp
->write_c_string(" ");
10837 (*p
)->export_named_object(exp
);
10840 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
10841 p
!= methods
->end_declarations();
10844 if (p
->second
->is_function_declaration())
10846 exp
->write_c_string(" ");
10847 p
->second
->export_named_object(exp
);
10852 // Make a named type.
10855 Type::make_named_type(Named_object
* named_object
, Type
* type
,
10858 return new Named_type(named_object
, type
, location
);
10861 // Finalize the methods for TYPE. It will be a named type or a struct
10862 // type. This sets *ALL_METHODS to the list of methods, and builds
10863 // all required stubs.
10866 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
10867 Methods
** all_methods
)
10869 *all_methods
= new Methods();
10870 std::vector
<const Named_type
*> seen
;
10871 Type::add_methods_for_type(type
, NULL
, 0, false, false, &seen
, *all_methods
);
10872 if ((*all_methods
)->empty())
10874 delete *all_methods
;
10875 *all_methods
= NULL
;
10877 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
10880 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
10881 // build up the struct field indexes as we go. DEPTH is the depth of
10882 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
10883 // adding these methods for an anonymous field with pointer type.
10884 // NEEDS_STUB_METHOD is true if we need to use a stub method which
10885 // calls the real method. TYPES_SEEN is used to avoid infinite
10889 Type::add_methods_for_type(const Type
* type
,
10890 const Method::Field_indexes
* field_indexes
,
10891 unsigned int depth
,
10892 bool is_embedded_pointer
,
10893 bool needs_stub_method
,
10894 std::vector
<const Named_type
*>* seen
,
10897 // Pointer types may not have methods.
10898 if (type
->points_to() != NULL
)
10901 const Named_type
* nt
= type
->named_type();
10904 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
10912 seen
->push_back(nt
);
10914 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
10915 is_embedded_pointer
, needs_stub_method
,
10919 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
10920 is_embedded_pointer
, needs_stub_method
,
10923 // If we are called with depth > 0, then we are looking at an
10924 // anonymous field of a struct. If such a field has interface type,
10925 // then we need to add the interface methods. We don't want to add
10926 // them when depth == 0, because we will already handle them
10927 // following the usual rules for an interface type.
10929 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
10935 // Add the local methods for the named type NT to *METHODS. The
10936 // parameters are as for add_methods_to_type.
10939 Type::add_local_methods_for_type(const Named_type
* nt
,
10940 const Method::Field_indexes
* field_indexes
,
10941 unsigned int depth
,
10942 bool is_embedded_pointer
,
10943 bool needs_stub_method
,
10946 const Bindings
* local_methods
= nt
->local_methods();
10947 if (local_methods
== NULL
)
10950 for (Bindings::const_declarations_iterator p
=
10951 local_methods
->begin_declarations();
10952 p
!= local_methods
->end_declarations();
10955 Named_object
* no
= p
->second
;
10956 bool is_value_method
= (is_embedded_pointer
10957 || !Type::method_expects_pointer(no
));
10958 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
10959 (needs_stub_method
|| depth
> 0));
10960 if (!methods
->insert(no
->name(), m
))
10965 // Add the embedded methods for TYPE to *METHODS. These are the
10966 // methods attached to anonymous fields. The parameters are as for
10967 // add_methods_to_type.
10970 Type::add_embedded_methods_for_type(const Type
* type
,
10971 const Method::Field_indexes
* field_indexes
,
10972 unsigned int depth
,
10973 bool is_embedded_pointer
,
10974 bool needs_stub_method
,
10975 std::vector
<const Named_type
*>* seen
,
10978 // Look for anonymous fields in TYPE. TYPE has fields if it is a
10980 const Struct_type
* st
= type
->struct_type();
10984 const Struct_field_list
* fields
= st
->fields();
10985 if (fields
== NULL
)
10988 unsigned int i
= 0;
10989 for (Struct_field_list::const_iterator pf
= fields
->begin();
10990 pf
!= fields
->end();
10993 if (!pf
->is_anonymous())
10996 Type
* ftype
= pf
->type();
10997 bool is_pointer
= false;
10998 if (ftype
->points_to() != NULL
)
11000 ftype
= ftype
->points_to();
11003 Named_type
* fnt
= ftype
->named_type();
11006 // This is an error, but it will be diagnosed elsewhere.
11010 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
11011 sub_field_indexes
->next
= field_indexes
;
11012 sub_field_indexes
->field_index
= i
;
11014 Methods tmp_methods
;
11015 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
11016 (is_embedded_pointer
|| is_pointer
),
11022 // Check if there are promoted methods that conflict with field names and
11023 // don't add them to the method map.
11024 for (Methods::const_iterator p
= tmp_methods
.begin();
11025 p
!= tmp_methods
.end();
11028 bool found
= false;
11029 for (Struct_field_list::const_iterator fp
= fields
->begin();
11030 fp
!= fields
->end();
11033 if (fp
->field_name() == p
->first
)
11040 !methods
->insert(p
->first
, p
->second
))
11046 // If TYPE is an interface type, then add its method to *METHODS.
11047 // This is for interface methods attached to an anonymous field. The
11048 // parameters are as for add_methods_for_type.
11051 Type::add_interface_methods_for_type(const Type
* type
,
11052 const Method::Field_indexes
* field_indexes
,
11053 unsigned int depth
,
11056 const Interface_type
* it
= type
->interface_type();
11060 const Typed_identifier_list
* imethods
= it
->methods();
11061 if (imethods
== NULL
)
11064 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
11065 pm
!= imethods
->end();
11068 Function_type
* fntype
= pm
->type()->function_type();
11069 if (fntype
== NULL
)
11071 // This is an error, but it should be reported elsewhere
11072 // when we look at the methods for IT.
11075 go_assert(!fntype
->is_method());
11076 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
11077 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
11078 field_indexes
, depth
);
11079 if (!methods
->insert(pm
->name(), m
))
11084 // Build stub methods for TYPE as needed. METHODS is the set of
11085 // methods for the type. A stub method may be needed when a type
11086 // inherits a method from an anonymous field. When we need the
11087 // address of the method, as in a type descriptor, we need to build a
11088 // little stub which does the required field dereferences and jumps to
11089 // the real method. LOCATION is the location of the type definition.
11092 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
11095 if (methods
== NULL
)
11097 for (Methods::const_iterator p
= methods
->begin();
11098 p
!= methods
->end();
11101 Method
* m
= p
->second
;
11102 if (m
->is_ambiguous() || !m
->needs_stub_method())
11105 const std::string
& name(p
->first
);
11107 // Build a stub method.
11109 const Function_type
* fntype
= m
->type();
11111 static unsigned int counter
;
11113 snprintf(buf
, sizeof buf
, "$this%u", counter
);
11116 Type
* receiver_type
= const_cast<Type
*>(type
);
11117 if (!m
->is_value_method())
11118 receiver_type
= Type::make_pointer_type(receiver_type
);
11119 Location receiver_location
= m
->receiver_location();
11120 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
11121 receiver_location
);
11123 const Typed_identifier_list
* fnparams
= fntype
->parameters();
11124 Typed_identifier_list
* stub_params
;
11125 if (fnparams
== NULL
|| fnparams
->empty())
11126 stub_params
= NULL
;
11129 // We give each stub parameter a unique name.
11130 stub_params
= new Typed_identifier_list();
11131 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
11132 pp
!= fnparams
->end();
11136 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
11137 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
11143 const Typed_identifier_list
* fnresults
= fntype
->results();
11144 Typed_identifier_list
* stub_results
;
11145 if (fnresults
== NULL
|| fnresults
->empty())
11146 stub_results
= NULL
;
11149 // We create the result parameters without any names, since
11150 // we won't refer to them.
11151 stub_results
= new Typed_identifier_list();
11152 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
11153 pr
!= fnresults
->end();
11155 stub_results
->push_back(Typed_identifier("", pr
->type(),
11159 Function_type
* stub_type
= Type::make_function_type(receiver
,
11162 fntype
->location());
11163 if (fntype
->is_varargs())
11164 stub_type
->set_is_varargs();
11166 // We only create the function in the package which creates the
11168 const Package
* package
;
11169 if (type
->named_type() == NULL
)
11172 package
= type
->named_type()->named_object()->package();
11173 std::string stub_name
= gogo
->stub_method_name(package
, name
);
11174 Named_object
* stub
;
11175 if (package
!= NULL
)
11176 stub
= Named_object::make_function_declaration(stub_name
, package
,
11177 stub_type
, location
);
11180 stub
= gogo
->start_function(stub_name
, stub_type
, false,
11181 fntype
->location());
11182 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
11183 fntype
->is_varargs(), location
);
11184 gogo
->finish_function(fntype
->location());
11186 if (type
->named_type() == NULL
&& stub
->is_function())
11187 stub
->func_value()->set_is_unnamed_type_stub_method();
11188 if (m
->nointerface() && stub
->is_function())
11189 stub
->func_value()->set_nointerface();
11192 m
->set_stub_object(stub
);
11196 // Build a stub method which adjusts the receiver as required to call
11197 // METHOD. RECEIVER_NAME is the name we used for the receiver.
11198 // PARAMS is the list of function parameters.
11201 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
11202 const char* receiver_name
,
11203 const Typed_identifier_list
* params
,
11207 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
11208 go_assert(receiver_object
!= NULL
);
11210 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
11211 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
11212 if (expr
->type()->points_to() == NULL
)
11213 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11215 Expression_list
* arguments
;
11216 if (params
== NULL
|| params
->empty())
11220 arguments
= new Expression_list();
11221 for (Typed_identifier_list::const_iterator p
= params
->begin();
11222 p
!= params
->end();
11225 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
11226 go_assert(param
!= NULL
);
11227 Expression
* param_ref
= Expression::make_var_reference(param
,
11229 arguments
->push_back(param_ref
);
11233 Expression
* func
= method
->bind_method(expr
, location
);
11234 go_assert(func
!= NULL
);
11235 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
11238 gogo
->add_statement(Statement::make_return_from_call(call
, location
));
11241 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
11242 // in reverse order.
11245 Type::apply_field_indexes(Expression
* expr
,
11246 const Method::Field_indexes
* field_indexes
,
11249 if (field_indexes
== NULL
)
11251 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
11252 Struct_type
* stype
= expr
->type()->deref()->struct_type();
11253 go_assert(stype
!= NULL
11254 && field_indexes
->field_index
< stype
->field_count());
11255 if (expr
->type()->struct_type() == NULL
)
11257 go_assert(expr
->type()->points_to() != NULL
);
11258 expr
= Expression::make_dereference(expr
, Expression::NIL_CHECK_DEFAULT
,
11260 go_assert(expr
->type()->struct_type() == stype
);
11262 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
11266 // Return whether NO is a method for which the receiver is a pointer.
11269 Type::method_expects_pointer(const Named_object
* no
)
11271 const Function_type
*fntype
;
11272 if (no
->is_function())
11273 fntype
= no
->func_value()->type();
11274 else if (no
->is_function_declaration())
11275 fntype
= no
->func_declaration_value()->type();
11278 return fntype
->receiver()->type()->points_to() != NULL
;
11281 // Given a set of methods for a type, METHODS, return the method NAME,
11282 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
11283 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
11284 // but is ambiguous (and return NULL).
11287 Type::method_function(const Methods
* methods
, const std::string
& name
,
11288 bool* is_ambiguous
)
11290 if (is_ambiguous
!= NULL
)
11291 *is_ambiguous
= false;
11292 if (methods
== NULL
)
11294 Methods::const_iterator p
= methods
->find(name
);
11295 if (p
== methods
->end())
11297 Method
* m
= p
->second
;
11298 if (m
->is_ambiguous())
11300 if (is_ambiguous
!= NULL
)
11301 *is_ambiguous
= true;
11307 // Return a pointer to the interface method table for TYPE for the
11308 // interface INTERFACE.
11311 Type::interface_method_table(Type
* type
,
11312 Interface_type
*interface
,
11314 Interface_method_tables
** method_tables
,
11315 Interface_method_tables
** pointer_tables
)
11317 go_assert(!interface
->is_empty());
11319 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
11322 *pimt
= new Interface_method_tables(5);
11324 std::pair
<Interface_type
*, Expression
*> val(interface
, NULL
);
11325 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
11327 Location loc
= Linemap::predeclared_location();
11330 // This is a new entry in the hash table.
11331 go_assert(ins
.first
->second
== NULL
);
11332 ins
.first
->second
=
11333 Expression::make_interface_mtable_ref(interface
, type
, is_pointer
, loc
);
11335 return Expression::make_unary(OPERATOR_AND
, ins
.first
->second
, loc
);
11338 // Look for field or method NAME for TYPE. Return an Expression for
11339 // the field or method bound to EXPR. If there is no such field or
11340 // method, give an appropriate error and return an error expression.
11343 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
11344 const std::string
& name
,
11347 if (type
->deref()->is_error_type())
11348 return Expression::make_error(location
);
11350 const Named_type
* nt
= type
->deref()->named_type();
11351 const Struct_type
* st
= type
->deref()->struct_type();
11352 const Interface_type
* it
= type
->interface_type();
11354 // If this is a pointer to a pointer, then it is possible that the
11355 // pointed-to type has methods.
11356 bool dereferenced
= false;
11360 && type
->points_to() != NULL
11361 && type
->points_to()->points_to() != NULL
)
11363 expr
= Expression::make_dereference(expr
, Expression::NIL_CHECK_DEFAULT
,
11365 type
= type
->points_to();
11366 if (type
->deref()->is_error_type())
11367 return Expression::make_error(location
);
11368 nt
= type
->points_to()->named_type();
11369 st
= type
->points_to()->struct_type();
11370 dereferenced
= true;
11373 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
11374 || expr
->is_addressable());
11375 std::vector
<const Named_type
*> seen
;
11376 bool is_method
= false;
11377 bool found_pointer_method
= false;
11378 std::string ambig1
;
11379 std::string ambig2
;
11380 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
11381 &seen
, NULL
, &is_method
,
11382 &found_pointer_method
, &ambig1
, &ambig2
))
11387 go_assert(st
!= NULL
);
11388 if (type
->struct_type() == NULL
)
11392 go_error_at(location
, "pointer type has no field %qs",
11393 Gogo::message_name(name
).c_str());
11394 return Expression::make_error(location
);
11396 go_assert(type
->points_to() != NULL
);
11397 expr
= Expression::make_dereference(expr
,
11398 Expression::NIL_CHECK_DEFAULT
,
11400 go_assert(expr
->type()->struct_type() == st
);
11402 ret
= st
->field_reference(expr
, name
, location
);
11405 go_error_at(location
, "type has no field %qs",
11406 Gogo::message_name(name
).c_str());
11407 return Expression::make_error(location
);
11410 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11411 ret
= Expression::make_interface_field_reference(expr
, name
,
11417 m
= nt
->method_function(name
, NULL
);
11418 else if (st
!= NULL
)
11419 m
= st
->method_function(name
, NULL
);
11422 go_assert(m
!= NULL
);
11425 go_error_at(location
,
11426 "calling method %qs requires explicit dereference",
11427 Gogo::message_name(name
).c_str());
11428 return Expression::make_error(location
);
11430 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
11431 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
11432 ret
= m
->bind_method(expr
, location
);
11434 go_assert(ret
!= NULL
);
11439 if (Gogo::is_erroneous_name(name
))
11441 // An error was already reported.
11443 else if (!ambig1
.empty())
11444 go_error_at(location
, "%qs is ambiguous via %qs and %qs",
11445 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
11447 else if (found_pointer_method
)
11448 go_error_at(location
, "method requires a pointer receiver");
11449 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
11450 go_error_at(location
,
11451 ("reference to field %qs in object which "
11452 "has no fields or methods"),
11453 Gogo::message_name(name
).c_str());
11456 bool is_unexported
;
11457 // The test for 'a' and 'z' is to handle builtin names,
11458 // which are not hidden.
11459 if (!Gogo::is_hidden_name(name
) && (name
[0] < 'a' || name
[0] > 'z'))
11460 is_unexported
= false;
11463 std::string unpacked
= Gogo::unpack_hidden_name(name
);
11465 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
11470 go_error_at(location
, "reference to unexported field or method %qs",
11471 Gogo::message_name(name
).c_str());
11473 go_error_at(location
, "reference to undefined field or method %qs",
11474 Gogo::message_name(name
).c_str());
11476 return Expression::make_error(location
);
11480 // Look in TYPE for a field or method named NAME, return true if one
11481 // is found. This looks through embedded anonymous fields and handles
11482 // ambiguity. If a method is found, sets *IS_METHOD to true;
11483 // otherwise, if a field is found, set it to false. If
11484 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
11485 // whose address can not be taken. SEEN is used to avoid infinite
11486 // recursion on invalid types.
11488 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
11489 // method we couldn't use because it requires a pointer. LEVEL is
11490 // used for recursive calls, and can be NULL for a non-recursive call.
11491 // When this function returns false because it finds that the name is
11492 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
11493 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
11494 // will be unchanged.
11496 // This function just returns whether or not there is a field or
11497 // method, and whether it is a field or method. It doesn't build an
11498 // expression to refer to it. If it is a method, we then look in the
11499 // list of all methods for the type. If it is a field, the search has
11500 // to be done again, looking only for fields, and building up the
11501 // expression as we go.
11504 Type::find_field_or_method(const Type
* type
,
11505 const std::string
& name
,
11506 bool receiver_can_be_pointer
,
11507 std::vector
<const Named_type
*>* seen
,
11510 bool* found_pointer_method
,
11511 std::string
* ambig1
,
11512 std::string
* ambig2
)
11514 // Named types can have locally defined methods.
11515 const Named_type
* nt
= type
->unalias()->named_type();
11516 if (nt
== NULL
&& type
->points_to() != NULL
)
11517 nt
= type
->points_to()->unalias()->named_type();
11520 Named_object
* no
= nt
->find_local_method(name
);
11523 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
11529 // Record that we have found a pointer method in order to
11530 // give a better error message if we don't find anything
11532 *found_pointer_method
= true;
11535 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11541 // We've already seen this type when searching for methods.
11547 // Interface types can have methods.
11548 const Interface_type
* it
= type
->interface_type();
11549 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
11555 // Struct types can have fields. They can also inherit fields and
11556 // methods from anonymous fields.
11557 const Struct_type
* st
= type
->deref()->struct_type();
11560 const Struct_field_list
* fields
= st
->fields();
11561 if (fields
== NULL
)
11565 seen
->push_back(nt
);
11567 int found_level
= 0;
11568 bool found_is_method
= false;
11569 std::string found_ambig1
;
11570 std::string found_ambig2
;
11571 const Struct_field
* found_parent
= NULL
;
11572 for (Struct_field_list::const_iterator pf
= fields
->begin();
11573 pf
!= fields
->end();
11576 if (pf
->is_field_name(name
))
11578 *is_method
= false;
11584 if (!pf
->is_anonymous())
11587 if (pf
->type()->deref()->is_error_type()
11588 || pf
->type()->deref()->is_undefined())
11591 Named_type
* fnt
= pf
->type()->named_type();
11593 fnt
= pf
->type()->deref()->named_type();
11594 go_assert(fnt
!= NULL
);
11596 // Methods with pointer receivers on embedded field are
11597 // inherited by the pointer to struct, and also by the struct
11598 // type if the field itself is a pointer.
11599 bool can_be_pointer
= (receiver_can_be_pointer
11600 || pf
->type()->points_to() != NULL
);
11601 int sublevel
= level
== NULL
? 1 : *level
+ 1;
11602 bool sub_is_method
;
11603 std::string subambig1
;
11604 std::string subambig2
;
11605 bool subfound
= Type::find_field_or_method(fnt
,
11611 found_pointer_method
,
11616 if (!subambig1
.empty())
11618 // The name was found via this field, but is ambiguous.
11619 // if the ambiguity is lower or at the same level as
11620 // anything else we have already found, then we want to
11621 // pass the ambiguity back to the caller.
11622 if (found_level
== 0 || sublevel
<= found_level
)
11624 found_ambig1
= (Gogo::message_name(pf
->field_name())
11625 + '.' + subambig1
);
11626 found_ambig2
= (Gogo::message_name(pf
->field_name())
11627 + '.' + subambig2
);
11628 found_level
= sublevel
;
11634 // The name was found via this field. Use the level to see
11635 // if we want to use this one, or whether it introduces an
11637 if (found_level
== 0 || sublevel
< found_level
)
11639 found_level
= sublevel
;
11640 found_is_method
= sub_is_method
;
11641 found_ambig1
.clear();
11642 found_ambig2
.clear();
11643 found_parent
= &*pf
;
11645 else if (sublevel
> found_level
)
11647 else if (found_ambig1
.empty())
11649 // We found an ambiguity.
11650 go_assert(found_parent
!= NULL
);
11651 found_ambig1
= Gogo::message_name(found_parent
->field_name());
11652 found_ambig2
= Gogo::message_name(pf
->field_name());
11656 // We found an ambiguity, but we already know of one.
11657 // Just report the earlier one.
11662 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
11663 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
11664 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
11665 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
11670 if (found_level
== 0)
11672 else if (found_is_method
11673 && type
->named_type() != NULL
11674 && type
->points_to() != NULL
)
11676 // If this is a method inherited from a struct field in a named pointer
11677 // type, it is invalid to automatically dereference the pointer to the
11678 // struct to find this method.
11680 *level
= found_level
;
11684 else if (!found_ambig1
.empty())
11686 go_assert(!found_ambig1
.empty());
11687 ambig1
->assign(found_ambig1
);
11688 ambig2
->assign(found_ambig2
);
11690 *level
= found_level
;
11696 *level
= found_level
;
11697 *is_method
= found_is_method
;
11702 // Return whether NAME is an unexported field or method for TYPE.
11705 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
11706 const std::string
& name
,
11707 std::vector
<const Named_type
*>* seen
)
11709 const Named_type
* nt
= type
->named_type();
11711 nt
= type
->deref()->named_type();
11714 if (nt
->is_unexported_local_method(gogo
, name
))
11717 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
11723 // We've already seen this type.
11729 const Interface_type
* it
= type
->interface_type();
11730 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
11733 type
= type
->deref();
11735 const Struct_type
* st
= type
->struct_type();
11736 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
11742 const Struct_field_list
* fields
= st
->fields();
11743 if (fields
== NULL
)
11747 seen
->push_back(nt
);
11749 for (Struct_field_list::const_iterator pf
= fields
->begin();
11750 pf
!= fields
->end();
11753 if (pf
->is_anonymous()
11754 && !pf
->type()->deref()->is_error_type()
11755 && !pf
->type()->deref()->is_undefined())
11757 Named_type
* subtype
= pf
->type()->named_type();
11758 if (subtype
== NULL
)
11759 subtype
= pf
->type()->deref()->named_type();
11760 if (subtype
== NULL
)
11762 // This is an error, but it will be diagnosed elsewhere.
11765 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
11780 // Class Forward_declaration.
11782 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
11783 : Type(TYPE_FORWARD
),
11784 named_object_(named_object
->resolve()), warned_(false)
11786 go_assert(this->named_object_
->is_unknown()
11787 || this->named_object_
->is_type_declaration());
11790 // Return the named object.
11793 Forward_declaration_type::named_object()
11795 return this->named_object_
->resolve();
11798 const Named_object
*
11799 Forward_declaration_type::named_object() const
11801 return this->named_object_
->resolve();
11804 // Return the name of the forward declared type.
11807 Forward_declaration_type::name() const
11809 return this->named_object()->name();
11812 // Warn about a use of a type which has been declared but not defined.
11815 Forward_declaration_type::warn() const
11817 Named_object
* no
= this->named_object_
->resolve();
11818 if (no
->is_unknown())
11820 // The name was not defined anywhere.
11821 if (!this->warned_
)
11823 go_error_at(this->named_object_
->location(),
11824 "use of undefined type %qs",
11825 no
->message_name().c_str());
11826 this->warned_
= true;
11829 else if (no
->is_type_declaration())
11831 // The name was seen as a type, but the type was never defined.
11832 if (no
->type_declaration_value()->using_type())
11834 go_error_at(this->named_object_
->location(),
11835 "use of undefined type %qs",
11836 no
->message_name().c_str());
11837 this->warned_
= true;
11842 // The name was defined, but not as a type.
11843 if (!this->warned_
)
11845 go_error_at(this->named_object_
->location(), "expected type");
11846 this->warned_
= true;
11851 // Get the base type of a declaration. This gives an error if the
11852 // type has not yet been defined.
11855 Forward_declaration_type::real_type()
11857 if (this->is_defined())
11859 Named_type
* nt
= this->named_object()->type_value();
11860 if (!nt
->is_valid())
11861 return Type::make_error_type();
11862 return this->named_object()->type_value();
11867 return Type::make_error_type();
11872 Forward_declaration_type::real_type() const
11874 if (this->is_defined())
11876 const Named_type
* nt
= this->named_object()->type_value();
11877 if (!nt
->is_valid())
11878 return Type::make_error_type();
11879 return this->named_object()->type_value();
11884 return Type::make_error_type();
11888 // Return whether the base type is defined.
11891 Forward_declaration_type::is_defined() const
11893 return this->named_object()->is_type();
11896 // Add a method. This is used when methods are defined before the
11900 Forward_declaration_type::add_method(const std::string
& name
,
11901 Function
* function
)
11903 Named_object
* no
= this->named_object();
11904 if (no
->is_unknown())
11905 no
->declare_as_type();
11906 return no
->type_declaration_value()->add_method(name
, function
);
11909 // Add a method declaration. This is used when methods are declared
11910 // before the type.
11913 Forward_declaration_type::add_method_declaration(const std::string
& name
,
11915 Function_type
* type
,
11918 Named_object
* no
= this->named_object();
11919 if (no
->is_unknown())
11920 no
->declare_as_type();
11921 Type_declaration
* td
= no
->type_declaration_value();
11922 return td
->add_method_declaration(name
, package
, type
, location
);
11925 // Add an already created object as a method.
11928 Forward_declaration_type::add_existing_method(Named_object
* nom
)
11930 Named_object
* no
= this->named_object();
11931 if (no
->is_unknown())
11932 no
->declare_as_type();
11933 no
->type_declaration_value()->add_existing_method(nom
);
11939 Forward_declaration_type::do_traverse(Traverse
* traverse
)
11941 if (this->is_defined()
11942 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
11943 return TRAVERSE_EXIT
;
11944 return TRAVERSE_CONTINUE
;
11947 // Verify the type.
11950 Forward_declaration_type::do_verify()
11952 if (!this->is_defined() && !this->is_nil_constant_as_type())
11960 // Get the backend representation for the type.
11963 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
11965 if (this->is_defined())
11966 return Type::get_named_base_btype(gogo
, this->real_type());
11969 return gogo
->backend()->error_type();
11971 // We represent an undefined type as a struct with no fields. That
11972 // should work fine for the backend, since the same case can arise
11974 std::vector
<Backend::Btyped_identifier
> fields
;
11975 Btype
* bt
= gogo
->backend()->struct_type(fields
);
11976 return gogo
->backend()->named_type(this->name(), bt
,
11977 this->named_object()->location());
11980 // Build a type descriptor for a forwarded type.
11983 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
11985 Location ploc
= Linemap::predeclared_location();
11986 if (!this->is_defined())
11987 return Expression::make_error(ploc
);
11990 Type
* t
= this->real_type();
11992 return this->named_type_descriptor(gogo
, t
, name
);
11994 return Expression::make_error(this->named_object_
->location());
11998 // The reflection string.
12001 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
12003 this->append_reflection(this->real_type(), gogo
, ret
);
12006 // Export a forward declaration. This can happen when a defined type
12007 // refers to a type which is only declared (and is presumably defined
12008 // in some other file in the same package).
12011 Forward_declaration_type::do_export(Export
*) const
12013 // If there is a base type, that should be exported instead of this.
12014 go_assert(!this->is_defined());
12016 // We don't output anything.
12019 // Make a forward declaration.
12022 Type::make_forward_declaration(Named_object
* named_object
)
12024 return new Forward_declaration_type(named_object
);
12027 // Class Typed_identifier_list.
12029 // Sort the entries by name.
12031 struct Typed_identifier_list_sort
12035 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
12037 return (Gogo::unpack_hidden_name(t1
.name())
12038 < Gogo::unpack_hidden_name(t2
.name()));
12043 Typed_identifier_list::sort_by_name()
12045 std::sort(this->entries_
.begin(), this->entries_
.end(),
12046 Typed_identifier_list_sort());
12052 Typed_identifier_list::traverse(Traverse
* traverse
)
12054 for (Typed_identifier_list::const_iterator p
= this->begin();
12058 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
12059 return TRAVERSE_EXIT
;
12061 return TRAVERSE_CONTINUE
;
12066 Typed_identifier_list
*
12067 Typed_identifier_list::copy() const
12069 Typed_identifier_list
* ret
= new Typed_identifier_list();
12070 for (Typed_identifier_list::const_iterator p
= this->begin();
12073 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));