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.
12 #include "expressions.h"
13 #include "statements.h"
19 // Forward declarations so that we don't have to make types.h #include
23 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
25 std::vector
<Backend::Btyped_identifier
>* bfields
);
28 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
29 std::vector
<Backend::Btyped_identifier
>* bfields
);
32 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
34 std::vector
<Backend::Btyped_identifier
>* bfields
);
38 Type::Type(Type_classification classification
)
39 : classification_(classification
), btype_(NULL
), type_descriptor_var_(NULL
),
48 // Get the base type for a type--skip names and forward declarations.
53 switch (this->classification_
)
56 return this->named_type()->named_base();
58 return this->forward_declaration_type()->real_type()->base();
67 switch (this->classification_
)
70 return this->named_type()->named_base();
72 return this->forward_declaration_type()->real_type()->base();
78 // Skip defined forward declarations.
84 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
85 while (ftype
!= NULL
&& ftype
->is_defined())
87 t
= ftype
->real_type();
88 ftype
= t
->forward_declaration_type();
94 Type::forwarded() const
97 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
98 while (ftype
!= NULL
&& ftype
->is_defined())
100 t
= ftype
->real_type();
101 ftype
= t
->forward_declaration_type();
106 // If this is a named type, return it. Otherwise, return NULL.
111 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
115 Type::named_type() const
117 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
120 // Return true if this type is not defined.
123 Type::is_undefined() const
125 return this->forwarded()->forward_declaration_type() != NULL
;
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
132 Type::is_basic_type() const
134 switch (this->classification_
)
157 return this->base()->is_basic_type();
164 // Return true if this is an abstract type.
167 Type::is_abstract() const
169 switch (this->classification())
172 return this->integer_type()->is_abstract();
174 return this->float_type()->is_abstract();
176 return this->complex_type()->is_abstract();
178 return this->is_abstract_string_type();
180 return this->is_abstract_boolean_type();
186 // Return a non-abstract version of an abstract type.
189 Type::make_non_abstract_type()
191 go_assert(this->is_abstract());
192 switch (this->classification())
195 if (this->integer_type()->is_rune())
196 return Type::lookup_integer_type("int32");
198 return Type::lookup_integer_type("int");
200 return Type::lookup_float_type("float64");
202 return Type::lookup_complex_type("complex128");
204 return Type::lookup_string_type();
206 return Type::lookup_bool_type();
212 // Return true if this is an error type. Don't give an error if we
213 // try to dereference an undefined forwarding type, as this is called
214 // in the parser when the type may legitimately be undefined.
217 Type::is_error_type() const
219 const Type
* t
= this->forwarded();
220 // Note that we return false for an undefined forward type.
221 switch (t
->classification_
)
226 return t
->named_type()->is_named_error_type();
232 // If this is a pointer type, return the type to which it points.
233 // Otherwise, return NULL.
236 Type::points_to() const
238 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
240 return ptype
== NULL
? NULL
: ptype
->points_to();
243 // Return whether this is a slice type.
246 Type::is_slice_type() const
248 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
251 // Return whether this is the predeclared constant nil being used as a
255 Type::is_nil_constant_as_type() const
257 const Type
* t
= this->forwarded();
258 if (t
->forward_declaration_type() != NULL
)
260 const Named_object
* no
= t
->forward_declaration_type()->named_object();
261 if (no
->is_unknown())
262 no
= no
->unknown_value()->real_named_object();
265 && no
->const_value()->expr()->is_nil_expression())
274 Type::traverse(Type
* type
, Traverse
* traverse
)
276 go_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
277 || (traverse
->traverse_mask()
278 & Traverse::traverse_expressions
) != 0);
279 if (traverse
->remember_type(type
))
281 // We have already traversed this type.
282 return TRAVERSE_CONTINUE
;
284 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
286 int t
= traverse
->type(type
);
287 if (t
== TRAVERSE_EXIT
)
288 return TRAVERSE_EXIT
;
289 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
290 return TRAVERSE_CONTINUE
;
292 // An array type has an expression which we need to traverse if
293 // traverse_expressions is set.
294 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
295 return TRAVERSE_EXIT
;
296 return TRAVERSE_CONTINUE
;
299 // Default implementation for do_traverse for child class.
302 Type::do_traverse(Traverse
*)
304 return TRAVERSE_CONTINUE
;
307 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
308 // then return true for all erroneous types; this is used to avoid
309 // cascading errors. If REASON is not NULL, optionally set *REASON to
310 // the reason the types are not identical.
313 Type::are_identical(const Type
* t1
, const Type
* t2
, bool errors_are_identical
,
316 if (t1
== NULL
|| t2
== NULL
)
318 // Something is wrong.
319 return errors_are_identical
? true : t1
== t2
;
322 // Skip defined forward declarations.
323 t1
= t1
->forwarded();
324 t2
= t2
->forwarded();
326 // Ignore aliases for purposes of type identity.
327 if (t1
->named_type() != NULL
&& t1
->named_type()->is_alias())
328 t1
= t1
->named_type()->real_type();
329 if (t2
->named_type() != NULL
&& t2
->named_type()->is_alias())
330 t2
= t2
->named_type()->real_type();
335 // An undefined forward declaration is an error.
336 if (t1
->forward_declaration_type() != NULL
337 || t2
->forward_declaration_type() != NULL
)
338 return errors_are_identical
;
340 // Avoid cascading errors with error types.
341 if (t1
->is_error_type() || t2
->is_error_type())
343 if (errors_are_identical
)
345 return t1
->is_error_type() && t2
->is_error_type();
348 // Get a good reason for the sink type. Note that the sink type on
349 // the left hand side of an assignment is handled in are_assignable.
350 if (t1
->is_sink_type() || t2
->is_sink_type())
353 *reason
= "invalid use of _";
357 // A named type is only identical to itself.
358 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
361 // Check type shapes.
362 if (t1
->classification() != t2
->classification())
365 switch (t1
->classification())
371 // These types are always identical.
375 return t1
->integer_type()->is_identical(t2
->integer_type());
378 return t1
->float_type()->is_identical(t2
->float_type());
381 return t1
->complex_type()->is_identical(t2
->complex_type());
384 return t1
->function_type()->is_identical(t2
->function_type(),
386 errors_are_identical
,
390 return Type::are_identical(t1
->points_to(), t2
->points_to(),
391 errors_are_identical
, reason
);
394 return t1
->struct_type()->is_identical(t2
->struct_type(),
395 errors_are_identical
);
398 return t1
->array_type()->is_identical(t2
->array_type(),
399 errors_are_identical
);
402 return t1
->map_type()->is_identical(t2
->map_type(),
403 errors_are_identical
);
406 return t1
->channel_type()->is_identical(t2
->channel_type(),
407 errors_are_identical
);
410 return t1
->interface_type()->is_identical(t2
->interface_type(),
411 errors_are_identical
);
413 case TYPE_CALL_MULTIPLE_RESULT
:
415 *reason
= "invalid use of multiple-value function call";
423 // Return true if it's OK to have a binary operation with types LHS
424 // and RHS. This is not used for shifts or comparisons.
427 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
429 if (Type::are_identical(lhs
, rhs
, true, NULL
))
432 // A constant of abstract bool type may be mixed with any bool type.
433 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
434 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
437 // A constant of abstract string type may be mixed with any string
439 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
440 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
446 // A constant of abstract integer, float, or complex type may be
447 // mixed with an integer, float, or complex type.
448 if ((rhs
->is_abstract()
449 && (rhs
->integer_type() != NULL
450 || rhs
->float_type() != NULL
451 || rhs
->complex_type() != NULL
)
452 && (lhs
->integer_type() != NULL
453 || lhs
->float_type() != NULL
454 || lhs
->complex_type() != NULL
))
455 || (lhs
->is_abstract()
456 && (lhs
->integer_type() != NULL
457 || lhs
->float_type() != NULL
458 || lhs
->complex_type() != NULL
)
459 && (rhs
->integer_type() != NULL
460 || rhs
->float_type() != NULL
461 || rhs
->complex_type() != NULL
)))
464 // The nil type may be compared to a pointer, an interface type, a
465 // slice type, a channel type, a map type, or a function type.
466 if (lhs
->is_nil_type()
467 && (rhs
->points_to() != NULL
468 || rhs
->interface_type() != NULL
469 || rhs
->is_slice_type()
470 || rhs
->map_type() != NULL
471 || rhs
->channel_type() != NULL
472 || rhs
->function_type() != NULL
))
474 if (rhs
->is_nil_type()
475 && (lhs
->points_to() != NULL
476 || lhs
->interface_type() != NULL
477 || lhs
->is_slice_type()
478 || lhs
->map_type() != NULL
479 || lhs
->channel_type() != NULL
480 || lhs
->function_type() != NULL
))
486 // Return true if a value with type T1 may be compared with a value of
487 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
490 Type::are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
491 const Type
*t2
, std::string
*reason
)
494 && !Type::are_assignable(t1
, t2
, NULL
)
495 && !Type::are_assignable(t2
, t1
, NULL
))
498 *reason
= "incompatible types in binary expression";
504 if (t1
->integer_type() == NULL
505 && t1
->float_type() == NULL
506 && !t1
->is_string_type())
509 *reason
= _("invalid comparison of non-ordered type");
513 else if (t1
->is_slice_type()
514 || t1
->map_type() != NULL
515 || t1
->function_type() != NULL
516 || t2
->is_slice_type()
517 || t2
->map_type() != NULL
518 || t2
->function_type() != NULL
)
520 if (!t1
->is_nil_type() && !t2
->is_nil_type())
524 if (t1
->is_slice_type() || t2
->is_slice_type())
525 *reason
= _("slice can only be compared to nil");
526 else if (t1
->map_type() != NULL
|| t2
->map_type() != NULL
)
527 *reason
= _("map can only be compared to nil");
529 *reason
= _("func can only be compared to nil");
531 // Match 6g error messages.
532 if (t1
->interface_type() != NULL
|| t2
->interface_type() != NULL
)
535 snprintf(buf
, sizeof buf
, _("invalid operation (%s)"),
545 if (!t1
->is_boolean_type()
546 && t1
->integer_type() == NULL
547 && t1
->float_type() == NULL
548 && t1
->complex_type() == NULL
549 && !t1
->is_string_type()
550 && t1
->points_to() == NULL
551 && t1
->channel_type() == NULL
552 && t1
->interface_type() == NULL
553 && t1
->struct_type() == NULL
554 && t1
->array_type() == NULL
555 && !t1
->is_nil_type())
558 *reason
= _("invalid comparison of non-comparable type");
562 if (t1
->named_type() != NULL
)
563 return t1
->named_type()->named_type_is_comparable(reason
);
564 else if (t2
->named_type() != NULL
)
565 return t2
->named_type()->named_type_is_comparable(reason
);
566 else if (t1
->struct_type() != NULL
)
568 const Struct_field_list
* fields
= t1
->struct_type()->fields();
569 for (Struct_field_list::const_iterator p
= fields
->begin();
573 if (!p
->type()->is_comparable())
576 *reason
= _("invalid comparison of non-comparable struct");
581 else if (t1
->array_type() != NULL
)
583 if (t1
->array_type()->length()->is_nil_expression()
584 || !t1
->array_type()->element_type()->is_comparable())
587 *reason
= _("invalid comparison of non-comparable array");
596 // Return true if a value with type RHS may be assigned to a variable
597 // with type LHS. If REASON is not NULL, set *REASON to the reason
598 // the types are not assignable.
601 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
603 // Do some checks first. Make sure the types are defined.
604 if (rhs
!= NULL
&& !rhs
->is_undefined())
606 if (rhs
->is_void_type())
609 *reason
= "non-value used as value";
612 if (rhs
->is_call_multiple_result_type())
615 reason
->assign(_("multiple-value function call in "
616 "single-value context"));
621 // Any value may be assigned to the blank identifier.
623 && !lhs
->is_undefined()
624 && lhs
->is_sink_type())
627 // Identical types are assignable.
628 if (Type::are_identical(lhs
, rhs
, true, reason
))
631 // The types are assignable if they have identical underlying types
632 // and either LHS or RHS is not a named type.
633 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
634 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
635 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
638 // The types are assignable if LHS is an interface type and RHS
639 // implements the required methods.
640 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
641 if (lhs_interface_type
!= NULL
)
643 if (lhs_interface_type
->implements_interface(rhs
, reason
))
645 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
646 if (rhs_interface_type
!= NULL
647 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
652 // The type are assignable if RHS is a bidirectional channel type,
653 // LHS is a channel type, they have identical element types, and
654 // either LHS or RHS is not a named type.
655 if (lhs
->channel_type() != NULL
656 && rhs
->channel_type() != NULL
657 && rhs
->channel_type()->may_send()
658 && rhs
->channel_type()->may_receive()
659 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
660 && Type::are_identical(lhs
->channel_type()->element_type(),
661 rhs
->channel_type()->element_type(),
666 // The nil type may be assigned to a pointer, function, slice, map,
667 // channel, or interface type.
668 if (rhs
->is_nil_type()
669 && (lhs
->points_to() != NULL
670 || lhs
->function_type() != NULL
671 || lhs
->is_slice_type()
672 || lhs
->map_type() != NULL
673 || lhs
->channel_type() != NULL
674 || lhs
->interface_type() != NULL
))
677 // An untyped numeric constant may be assigned to a numeric type if
678 // it is representable in that type.
679 if ((rhs
->is_abstract()
680 && (rhs
->integer_type() != NULL
681 || rhs
->float_type() != NULL
682 || rhs
->complex_type() != NULL
))
683 && (lhs
->integer_type() != NULL
684 || lhs
->float_type() != NULL
685 || lhs
->complex_type() != NULL
))
688 // Give some better error messages.
689 if (reason
!= NULL
&& reason
->empty())
691 if (rhs
->interface_type() != NULL
)
692 reason
->assign(_("need explicit conversion"));
693 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
695 size_t len
= (lhs
->named_type()->name().length()
696 + rhs
->named_type()->name().length()
698 char* buf
= new char[len
];
699 snprintf(buf
, len
, _("cannot use type %s as type %s"),
700 rhs
->named_type()->message_name().c_str(),
701 lhs
->named_type()->message_name().c_str());
710 // Return true if a value with type RHS may be converted to type LHS.
711 // If REASON is not NULL, set *REASON to the reason the types are not
715 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
717 // The types are convertible if they are assignable.
718 if (Type::are_assignable(lhs
, rhs
, reason
))
721 // The types are convertible if they have identical underlying
723 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
724 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
727 // The types are convertible if they are both unnamed pointer types
728 // and their pointer base types have identical underlying types.
729 if (lhs
->named_type() == NULL
730 && rhs
->named_type() == NULL
731 && lhs
->points_to() != NULL
732 && rhs
->points_to() != NULL
733 && (lhs
->points_to()->named_type() != NULL
734 || rhs
->points_to()->named_type() != NULL
)
735 && Type::are_identical(lhs
->points_to()->base(),
736 rhs
->points_to()->base(),
741 // Integer and floating point types are convertible to each other.
742 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
743 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
746 // Complex types are convertible to each other.
747 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
750 // An integer, or []byte, or []rune, may be converted to a string.
751 if (lhs
->is_string_type())
753 if (rhs
->integer_type() != NULL
)
755 if (rhs
->is_slice_type())
757 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
758 if (e
->integer_type() != NULL
759 && (e
->integer_type()->is_byte()
760 || e
->integer_type()->is_rune()))
765 // A string may be converted to []byte or []rune.
766 if (rhs
->is_string_type() && lhs
->is_slice_type())
768 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
769 if (e
->integer_type() != NULL
770 && (e
->integer_type()->is_byte() || e
->integer_type()->is_rune()))
774 // An unsafe.Pointer type may be converted to any pointer type or to
775 // a type whose underlying type is uintptr, and vice-versa.
776 if (lhs
->is_unsafe_pointer_type()
777 && (rhs
->points_to() != NULL
778 || (rhs
->integer_type() != NULL
779 && rhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
781 if (rhs
->is_unsafe_pointer_type()
782 && (lhs
->points_to() != NULL
783 || (lhs
->integer_type() != NULL
784 && lhs
->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
787 // Give a better error message.
791 *reason
= "invalid type conversion";
794 std::string s
= "invalid type conversion (";
804 // Return a hash code for the type to be used for method lookup.
807 Type::hash_for_method(Gogo
* gogo
) const
809 unsigned int ret
= 0;
810 if (this->classification_
!= TYPE_FORWARD
)
811 ret
+= this->classification_
;
812 return ret
+ this->do_hash_for_method(gogo
);
815 // Default implementation of do_hash_for_method. This is appropriate
816 // for types with no subfields.
819 Type::do_hash_for_method(Gogo
*) const
824 // Return a hash code for a string, given a starting hash.
827 Type::hash_string(const std::string
& s
, unsigned int h
)
829 const char* p
= s
.data();
830 size_t len
= s
.length();
831 for (; len
> 0; --len
)
839 // A hash table mapping unnamed types to the backend representation of
842 Type::Type_btypes
Type::type_btypes
;
844 // Return the backend representation for this type.
847 Type::get_backend(Gogo
* gogo
)
849 if (this->btype_
!= NULL
)
852 if (this->forward_declaration_type() != NULL
853 || this->named_type() != NULL
)
854 return this->get_btype_without_hash(gogo
);
856 if (this->is_error_type())
857 return gogo
->backend()->error_type();
859 // To avoid confusing the backend, translate all identical Go types
860 // to the same backend representation. We use a hash table to do
861 // that. There is no need to use the hash table for named types, as
862 // named types are only identical to themselves.
864 std::pair
<Type
*, Type_btype_entry
> val
;
866 val
.second
.btype
= NULL
;
867 val
.second
.is_placeholder
= false;
868 std::pair
<Type_btypes::iterator
, bool> ins
=
869 Type::type_btypes
.insert(val
);
870 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
872 // Note that GOGO can be NULL here, but only when the GCC
873 // middle-end is asking for a frontend type. That will only
874 // happen for simple types, which should never require
876 if (!ins
.first
->second
.is_placeholder
)
877 this->btype_
= ins
.first
->second
.btype
;
878 else if (gogo
->named_types_are_converted())
880 this->finish_backend(gogo
, ins
.first
->second
.btype
);
881 ins
.first
->second
.is_placeholder
= false;
884 return ins
.first
->second
.btype
;
887 Btype
* bt
= this->get_btype_without_hash(gogo
);
889 if (ins
.first
->second
.btype
== NULL
)
891 ins
.first
->second
.btype
= bt
;
892 ins
.first
->second
.is_placeholder
= false;
896 // We have already created a backend representation for this
897 // type. This can happen when an unnamed type is defined using
898 // a named type which in turns uses an identical unnamed type.
899 // Use the representation we created earlier and ignore the one we just
901 if (this->btype_
== bt
)
902 this->btype_
= ins
.first
->second
.btype
;
903 bt
= ins
.first
->second
.btype
;
909 // Return the backend representation for a type without looking in the
910 // hash table for identical types. This is used for named types,
911 // since a named type is never identical to any other type.
914 Type::get_btype_without_hash(Gogo
* gogo
)
916 if (this->btype_
== NULL
)
918 Btype
* bt
= this->do_get_backend(gogo
);
920 // For a recursive function or pointer type, we will temporarily
921 // return a circular pointer type during the recursion. We
922 // don't want to record that for a forwarding type, as it may
924 if (this->forward_declaration_type() != NULL
925 && gogo
->backend()->is_circular_pointer_type(bt
))
928 if (gogo
== NULL
|| !gogo
->named_types_are_converted())
936 // Get the backend representation of a type without forcing the
937 // creation of the backend representation of all supporting types.
938 // This will return a backend type that has the correct size but may
939 // be incomplete. E.g., a pointer will just be a placeholder pointer,
940 // and will not contain the final representation of the type to which
941 // it points. This is used while converting all named types to the
942 // backend representation, to avoid problems with indirect references
943 // to types which are not yet complete. When this is called, the
944 // sizes of all direct references (e.g., a struct field) should be
945 // known, but the sizes of indirect references (e.g., the type to
946 // which a pointer points) may not.
949 Type::get_backend_placeholder(Gogo
* gogo
)
951 if (gogo
->named_types_are_converted())
952 return this->get_backend(gogo
);
953 if (this->btype_
!= NULL
)
957 switch (this->classification_
)
967 // These are simple types that can just be created directly.
968 return this->get_backend(gogo
);
972 // All maps and channels have the same backend representation.
973 return this->get_backend(gogo
);
977 // Named types keep track of their own dependencies and manage
978 // their own placeholders.
979 return this->get_backend(gogo
);
982 if (this->interface_type()->is_empty())
983 return Interface_type::get_backend_empty_interface_type(gogo
);
990 std::pair
<Type
*, Type_btype_entry
> val
;
992 val
.second
.btype
= NULL
;
993 val
.second
.is_placeholder
= false;
994 std::pair
<Type_btypes::iterator
, bool> ins
=
995 Type::type_btypes
.insert(val
);
996 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
997 return ins
.first
->second
.btype
;
999 switch (this->classification_
)
1003 // A Go function type is a pointer to a struct type.
1004 Location loc
= this->function_type()->location();
1005 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1011 Location loc
= Linemap::unknown_location();
1012 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1017 // We don't have to make the struct itself be a placeholder. We
1018 // are promised that we know the sizes of the struct fields.
1019 // But we may have to use a placeholder for any particular
1022 std::vector
<Backend::Btyped_identifier
> bfields
;
1023 get_backend_struct_fields(gogo
, this->struct_type()->fields(),
1025 bt
= gogo
->backend()->struct_type(bfields
);
1030 if (this->is_slice_type())
1032 std::vector
<Backend::Btyped_identifier
> bfields
;
1033 get_backend_slice_fields(gogo
, this->array_type(), true, &bfields
);
1034 bt
= gogo
->backend()->struct_type(bfields
);
1038 Btype
* element
= this->array_type()->get_backend_element(gogo
, true);
1039 Bexpression
* len
= this->array_type()->get_backend_length(gogo
);
1040 bt
= gogo
->backend()->array_type(element
, len
);
1044 case TYPE_INTERFACE
:
1046 go_assert(!this->interface_type()->is_empty());
1047 std::vector
<Backend::Btyped_identifier
> bfields
;
1048 get_backend_interface_fields(gogo
, this->interface_type(), true,
1050 bt
= gogo
->backend()->struct_type(bfields
);
1055 case TYPE_CALL_MULTIPLE_RESULT
:
1056 /* Note that various classifications were handled in the earlier
1062 if (ins
.first
->second
.btype
== NULL
)
1064 ins
.first
->second
.btype
= bt
;
1065 ins
.first
->second
.is_placeholder
= true;
1069 // A placeholder for this type got created along the way. Use
1070 // that one and ignore the one we just built.
1071 bt
= ins
.first
->second
.btype
;
1077 // Complete the backend representation. This is called for a type
1078 // using a placeholder type.
1081 Type::finish_backend(Gogo
* gogo
, Btype
*placeholder
)
1083 switch (this->classification_
)
1097 Btype
* bt
= this->do_get_backend(gogo
);
1098 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1099 go_assert(saw_errors());
1105 Btype
* bt
= this->do_get_backend(gogo
);
1106 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1107 go_assert(saw_errors());
1112 // The struct type itself is done, but we have to make sure that
1113 // all the field types are converted.
1114 this->struct_type()->finish_backend_fields(gogo
);
1118 // The array type itself is done, but make sure the element type
1120 this->array_type()->finish_backend_element(gogo
);
1127 case TYPE_INTERFACE
:
1128 // The interface type itself is done, but make sure the method
1129 // types are converted.
1130 this->interface_type()->finish_backend_methods(gogo
);
1138 case TYPE_CALL_MULTIPLE_RESULT
:
1143 this->btype_
= placeholder
;
1146 // Return a pointer to the type descriptor for this type.
1149 Type::type_descriptor_pointer(Gogo
* gogo
, Location location
)
1151 Type
* t
= this->forwarded();
1152 if (t
->named_type() != NULL
&& t
->named_type()->is_alias())
1153 t
= t
->named_type()->real_type();
1154 if (t
->type_descriptor_var_
== NULL
)
1156 t
->make_type_descriptor_var(gogo
);
1157 go_assert(t
->type_descriptor_var_
!= NULL
);
1159 Bexpression
* var_expr
=
1160 gogo
->backend()->var_expression(t
->type_descriptor_var_
, location
);
1161 return gogo
->backend()->address_expression(var_expr
, location
);
1164 // A mapping from unnamed types to type descriptor variables.
1166 Type::Type_descriptor_vars
Type::type_descriptor_vars
;
1168 // Build the type descriptor for this type.
1171 Type::make_type_descriptor_var(Gogo
* gogo
)
1173 go_assert(this->type_descriptor_var_
== NULL
);
1175 Named_type
* nt
= this->named_type();
1177 // We can have multiple instances of unnamed types, but we only want
1178 // to emit the type descriptor once. We use a hash table. This is
1179 // not necessary for named types, as they are unique, and we store
1180 // the type descriptor in the type itself.
1181 Bvariable
** phash
= NULL
;
1184 Bvariable
* bvnull
= NULL
;
1185 std::pair
<Type_descriptor_vars::iterator
, bool> ins
=
1186 Type::type_descriptor_vars
.insert(std::make_pair(this, bvnull
));
1189 // We've already built a type descriptor for this type.
1190 this->type_descriptor_var_
= ins
.first
->second
;
1193 phash
= &ins
.first
->second
;
1196 // The type descriptor symbol for the unsafe.Pointer type is defined in
1197 // libgo/go-unsafe-pointer.c, so we just return a reference to that
1198 // symbol if necessary.
1199 if (this->is_unsafe_pointer_type())
1201 Location bloc
= Linemap::predeclared_location();
1203 Type
* td_type
= Type::make_type_descriptor_type();
1204 Btype
* td_btype
= td_type
->get_backend(gogo
);
1205 this->type_descriptor_var_
=
1206 gogo
->backend()->immutable_struct_reference("__go_tdn_unsafe.Pointer",
1211 *phash
= this->type_descriptor_var_
;
1215 std::string var_name
= this->type_descriptor_var_name(gogo
, nt
);
1217 // Build the contents of the type descriptor.
1218 Expression
* initializer
= this->do_type_descriptor(gogo
, NULL
);
1220 Btype
* initializer_btype
= initializer
->type()->get_backend(gogo
);
1222 Location loc
= nt
== NULL
? Linemap::predeclared_location() : nt
->location();
1224 const Package
* dummy
;
1225 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
1227 this->type_descriptor_var_
=
1228 gogo
->backend()->immutable_struct_reference(var_name
,
1232 *phash
= this->type_descriptor_var_
;
1236 // See if this type descriptor can appear in multiple packages.
1237 bool is_common
= false;
1240 // We create the descriptor for a builtin type whenever we need
1242 is_common
= nt
->is_builtin();
1246 // This is an unnamed type. The descriptor could be defined in
1247 // any package where it is needed, and the linker will pick one
1248 // descriptor to keep.
1252 // We are going to build the type descriptor in this package. We
1253 // must create the variable before we convert the initializer to the
1254 // backend representation, because the initializer may refer to the
1255 // type descriptor of this type. By setting type_descriptor_var_ we
1256 // ensure that type_descriptor_pointer will work if called while
1257 // converting INITIALIZER.
1259 this->type_descriptor_var_
=
1260 gogo
->backend()->immutable_struct(var_name
, false, is_common
,
1261 initializer_btype
, loc
);
1263 *phash
= this->type_descriptor_var_
;
1265 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
1266 context
.set_is_const();
1267 Bexpression
* binitializer
= initializer
->get_backend(&context
);
1269 gogo
->backend()->immutable_struct_set_init(this->type_descriptor_var_
,
1270 var_name
, false, is_common
,
1271 initializer_btype
, loc
,
1275 // Return the name of the type descriptor variable. If NT is not
1276 // NULL, use it to get the name. Otherwise this is an unnamed type.
1279 Type::type_descriptor_var_name(Gogo
* gogo
, Named_type
* nt
)
1282 return "__go_td_" + this->mangled_name(gogo
);
1284 Named_object
* no
= nt
->named_object();
1286 const Named_object
* in_function
= nt
->in_function(&index
);
1287 std::string ret
= "__go_tdn_";
1288 if (nt
->is_builtin())
1289 go_assert(in_function
== NULL
);
1292 const std::string
& pkgpath(no
->package() == NULL
1293 ? gogo
->pkgpath_symbol()
1294 : no
->package()->pkgpath_symbol());
1295 ret
.append(pkgpath
);
1297 if (in_function
!= NULL
)
1299 const Typed_identifier
* rcvr
=
1300 in_function
->func_value()->type()->receiver();
1303 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
1304 ret
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
1307 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
1312 snprintf(buf
, sizeof buf
, "%u", index
);
1319 // FIXME: This adds in pkgpath twice for hidden symbols, which is
1321 const std::string
& name(no
->name());
1322 if (!Gogo::is_hidden_name(name
))
1327 ret
.append(Gogo::pkgpath_for_symbol(Gogo::hidden_name_pkgpath(name
)));
1329 ret
.append(Gogo::unpack_hidden_name(name
));
1335 // Return true if this type descriptor is defined in a different
1336 // package. If this returns true it sets *PACKAGE to the package.
1339 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1340 const Package
** package
)
1344 if (nt
->named_object()->package() != NULL
)
1346 // This is a named type defined in a different package. The
1347 // type descriptor should be defined in that package.
1348 *package
= nt
->named_object()->package();
1354 if (this->points_to() != NULL
1355 && this->points_to()->named_type() != NULL
1356 && this->points_to()->named_type()->named_object()->package() != NULL
)
1358 // This is an unnamed pointer to a named type defined in a
1359 // different package. The descriptor should be defined in
1361 *package
= this->points_to()->named_type()->named_object()->package();
1368 // Return a composite literal for a type descriptor.
1371 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1373 return type
->do_type_descriptor(gogo
, NULL
);
1376 // Return a composite literal for a type descriptor with a name.
1379 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1381 go_assert(name
!= NULL
&& type
->named_type() != name
);
1382 return type
->do_type_descriptor(gogo
, name
);
1385 // Generate the GC symbol for this TYPE. VALS is the data so far in this
1386 // symbol; extra values will be appended in do_gc_symbol. OFFSET is the
1387 // offset into the symbol where the GC data is located. STACK_SIZE is the
1388 // size of the GC stack when dealing with array types.
1391 Type::gc_symbol(Gogo
* gogo
, Type
* type
, Expression_list
** vals
,
1392 Expression
** offset
, int stack_size
)
1394 type
->do_gc_symbol(gogo
, vals
, offset
, stack_size
);
1397 // Make a builtin struct type from a list of fields. The fields are
1398 // pairs of a name and a type.
1401 Type::make_builtin_struct_type(int nfields
, ...)
1404 va_start(ap
, nfields
);
1406 Location bloc
= Linemap::predeclared_location();
1407 Struct_field_list
* sfl
= new Struct_field_list();
1408 for (int i
= 0; i
< nfields
; i
++)
1410 const char* field_name
= va_arg(ap
, const char *);
1411 Type
* type
= va_arg(ap
, Type
*);
1412 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1417 return Type::make_struct_type(sfl
, bloc
);
1420 // A list of builtin named types.
1422 std::vector
<Named_type
*> Type::named_builtin_types
;
1424 // Make a builtin named type.
1427 Type::make_builtin_named_type(const char* name
, Type
* type
)
1429 Location bloc
= Linemap::predeclared_location();
1430 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1431 Named_type
* ret
= no
->type_value();
1432 Type::named_builtin_types
.push_back(ret
);
1436 // Convert the named builtin types.
1439 Type::convert_builtin_named_types(Gogo
* gogo
)
1441 for (std::vector
<Named_type
*>::const_iterator p
=
1442 Type::named_builtin_types
.begin();
1443 p
!= Type::named_builtin_types
.end();
1446 bool r
= (*p
)->verify();
1448 (*p
)->convert(gogo
);
1452 // Return the type of a type descriptor. We should really tie this to
1453 // runtime.Type rather than copying it. This must match commonType in
1454 // libgo/go/runtime/type.go.
1457 Type::make_type_descriptor_type()
1462 Location bloc
= Linemap::predeclared_location();
1464 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1465 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1466 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1467 Type
* string_type
= Type::lookup_string_type();
1468 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1470 // This is an unnamed version of unsafe.Pointer. Perhaps we
1471 // should use the named version instead, although that would
1472 // require us to create the unsafe package if it has not been
1473 // imported. It probably doesn't matter.
1474 Type
* void_type
= Type::make_void_type();
1475 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1477 Typed_identifier_list
*params
= new Typed_identifier_list();
1478 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
1479 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1481 Typed_identifier_list
* results
= new Typed_identifier_list();
1482 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1484 Type
* hash_fntype
= Type::make_function_type(NULL
, params
, results
,
1487 params
= new Typed_identifier_list();
1488 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
1489 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
1490 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1492 results
= new Typed_identifier_list();
1493 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1495 Type
* equal_fntype
= Type::make_function_type(NULL
, params
, results
,
1498 // Forward declaration for the type descriptor type.
1499 Named_object
* named_type_descriptor_type
=
1500 Named_object::make_type_declaration("commonType", NULL
, bloc
);
1501 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1502 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1504 // The type of a method on a concrete type.
1505 Struct_type
* method_type
=
1506 Type::make_builtin_struct_type(5,
1507 "name", pointer_string_type
,
1508 "pkgPath", pointer_string_type
,
1509 "mtyp", pointer_type_descriptor_type
,
1510 "typ", pointer_type_descriptor_type
,
1511 "tfn", unsafe_pointer_type
);
1512 Named_type
* named_method_type
=
1513 Type::make_builtin_named_type("method", method_type
);
1515 // Information for types with a name or methods.
1516 Type
* slice_named_method_type
=
1517 Type::make_array_type(named_method_type
, NULL
);
1518 Struct_type
* uncommon_type
=
1519 Type::make_builtin_struct_type(3,
1520 "name", pointer_string_type
,
1521 "pkgPath", pointer_string_type
,
1522 "methods", slice_named_method_type
);
1523 Named_type
* named_uncommon_type
=
1524 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1526 Type
* pointer_uncommon_type
=
1527 Type::make_pointer_type(named_uncommon_type
);
1529 // The type descriptor type.
1531 Struct_type
* type_descriptor_type
=
1532 Type::make_builtin_struct_type(11,
1534 "align", uint8_type
,
1535 "fieldAlign", uint8_type
,
1536 "size", uintptr_type
,
1537 "hash", uint32_type
,
1538 "hashfn", hash_fntype
,
1539 "equalfn", equal_fntype
,
1541 "string", pointer_string_type
,
1542 "", pointer_uncommon_type
,
1544 pointer_type_descriptor_type
);
1546 Named_type
* named
= Type::make_builtin_named_type("commonType",
1547 type_descriptor_type
);
1549 named_type_descriptor_type
->set_type_value(named
);
1557 // Make the type of a pointer to a type descriptor as represented in
1561 Type::make_type_descriptor_ptr_type()
1565 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1569 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1570 // hash code for this type and which compare whether two values of
1571 // this type are equal. If NAME is not NULL it is the name of this
1572 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1573 // functions, for convenience; they may be NULL.
1576 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1577 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1578 Named_object
** equal_fn
)
1580 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1582 Location bloc
= Linemap::predeclared_location();
1584 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1585 Type
* void_type
= Type::make_void_type();
1586 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1588 if (hash_fntype
== NULL
)
1590 Typed_identifier_list
* params
= new Typed_identifier_list();
1591 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1593 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1595 Typed_identifier_list
* results
= new Typed_identifier_list();
1596 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1598 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1600 if (equal_fntype
== NULL
)
1602 Typed_identifier_list
* params
= new Typed_identifier_list();
1603 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1605 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1607 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1609 Typed_identifier_list
* results
= new Typed_identifier_list();
1610 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1613 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1617 const char* hash_fnname
;
1618 const char* equal_fnname
;
1619 if (this->compare_is_identity(gogo
))
1621 hash_fnname
= "__go_type_hash_identity";
1622 equal_fnname
= "__go_type_equal_identity";
1624 else if (!this->is_comparable() ||
1625 (this->struct_type() != NULL
1626 && Thunk_statement::is_thunk_struct(this->struct_type())))
1628 hash_fnname
= "__go_type_hash_error";
1629 equal_fnname
= "__go_type_equal_error";
1633 switch (this->base()->classification())
1635 case Type::TYPE_ERROR
:
1636 case Type::TYPE_VOID
:
1637 case Type::TYPE_NIL
:
1638 case Type::TYPE_FUNCTION
:
1639 case Type::TYPE_MAP
:
1640 // For these types is_comparable should have returned false.
1643 case Type::TYPE_BOOLEAN
:
1644 case Type::TYPE_INTEGER
:
1645 case Type::TYPE_POINTER
:
1646 case Type::TYPE_CHANNEL
:
1647 // For these types compare_is_identity should have returned true.
1650 case Type::TYPE_FLOAT
:
1651 hash_fnname
= "__go_type_hash_float";
1652 equal_fnname
= "__go_type_equal_float";
1655 case Type::TYPE_COMPLEX
:
1656 hash_fnname
= "__go_type_hash_complex";
1657 equal_fnname
= "__go_type_equal_complex";
1660 case Type::TYPE_STRING
:
1661 hash_fnname
= "__go_type_hash_string";
1662 equal_fnname
= "__go_type_equal_string";
1665 case Type::TYPE_STRUCT
:
1667 // This is a struct which can not be compared using a
1668 // simple identity function. We need to build a function
1670 this->specific_type_functions(gogo
, name
, hash_fntype
,
1671 equal_fntype
, hash_fn
, equal_fn
);
1675 case Type::TYPE_ARRAY
:
1676 if (this->is_slice_type())
1678 // Type::is_compatible_for_comparison should have
1684 // This is an array which can not be compared using a
1685 // simple identity function. We need to build a
1686 // function for comparison.
1687 this->specific_type_functions(gogo
, name
, hash_fntype
,
1688 equal_fntype
, hash_fn
, equal_fn
);
1693 case Type::TYPE_INTERFACE
:
1694 if (this->interface_type()->is_empty())
1696 hash_fnname
= "__go_type_hash_empty_interface";
1697 equal_fnname
= "__go_type_equal_empty_interface";
1701 hash_fnname
= "__go_type_hash_interface";
1702 equal_fnname
= "__go_type_equal_interface";
1706 case Type::TYPE_NAMED
:
1707 case Type::TYPE_FORWARD
:
1716 Location bloc
= Linemap::predeclared_location();
1717 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1719 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1720 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1721 equal_fntype
, bloc
);
1722 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1725 // A hash table mapping types to the specific hash functions.
1727 Type::Type_functions
Type::type_functions_table
;
1729 // Handle a type function which is specific to a type: a struct or
1730 // array which can not use an identity comparison.
1733 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
,
1734 Function_type
* hash_fntype
,
1735 Function_type
* equal_fntype
,
1736 Named_object
** hash_fn
,
1737 Named_object
** equal_fn
)
1739 Hash_equal_fn
fnull(NULL
, NULL
);
1740 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1741 std::pair
<Type_functions::iterator
, bool> ins
=
1742 Type::type_functions_table
.insert(val
);
1745 // We already have functions for this type
1746 *hash_fn
= ins
.first
->second
.first
;
1747 *equal_fn
= ins
.first
->second
.second
;
1751 std::string base_name
;
1754 // Mangled names can have '.' if they happen to refer to named
1755 // types in some way. That's fine if this is simply a named
1756 // type, but otherwise it will confuse the code that builds
1757 // function identifiers. Remove '.' when necessary.
1758 base_name
= this->mangled_name(gogo
);
1760 while ((i
= base_name
.find('.')) != std::string::npos
)
1762 base_name
= gogo
->pack_hidden_name(base_name
, false);
1766 // This name is already hidden or not as appropriate.
1767 base_name
= name
->name();
1769 const Named_object
* in_function
= name
->in_function(&index
);
1770 if (in_function
!= NULL
)
1772 base_name
.append(1, '$');
1773 const Typed_identifier
* rcvr
=
1774 in_function
->func_value()->type()->receiver();
1777 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
1778 base_name
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
1779 base_name
.append(1, '$');
1781 base_name
.append(Gogo::unpack_hidden_name(in_function
->name()));
1785 snprintf(buf
, sizeof buf
, "%u", index
);
1791 std::string hash_name
= base_name
+ "$hash";
1792 std::string equal_name
= base_name
+ "$equal";
1794 Location bloc
= Linemap::predeclared_location();
1796 const Package
* package
= NULL
;
1797 bool is_defined_elsewhere
=
1798 this->type_descriptor_defined_elsewhere(name
, &package
);
1799 if (is_defined_elsewhere
)
1801 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
1803 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
1804 equal_fntype
, bloc
);
1808 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
1809 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
1813 ins
.first
->second
.first
= *hash_fn
;
1814 ins
.first
->second
.second
= *equal_fn
;
1816 if (!is_defined_elsewhere
)
1818 if (gogo
->in_global_scope())
1819 this->write_specific_type_functions(gogo
, name
, hash_name
, hash_fntype
,
1820 equal_name
, equal_fntype
);
1822 gogo
->queue_specific_type_function(this, name
, hash_name
, hash_fntype
,
1823 equal_name
, equal_fntype
);
1827 // Write the hash and equality functions for a type which needs to be
1828 // written specially.
1831 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
,
1832 const std::string
& hash_name
,
1833 Function_type
* hash_fntype
,
1834 const std::string
& equal_name
,
1835 Function_type
* equal_fntype
)
1837 Location bloc
= Linemap::predeclared_location();
1839 if (gogo
->specific_type_functions_are_written())
1841 go_assert(saw_errors());
1845 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
1847 hash_fn
->func_value()->set_is_type_specific_function();
1848 gogo
->start_block(bloc
);
1850 if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
1851 this->write_named_hash(gogo
, name
, hash_fntype
, equal_fntype
);
1852 else if (this->struct_type() != NULL
)
1853 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
1855 else if (this->array_type() != NULL
)
1856 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
1861 Block
* b
= gogo
->finish_block(bloc
);
1862 gogo
->add_block(b
, bloc
);
1863 gogo
->lower_block(hash_fn
, b
);
1864 gogo
->finish_function(bloc
);
1866 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
1868 equal_fn
->func_value()->set_is_type_specific_function();
1869 gogo
->start_block(bloc
);
1871 if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
1872 this->write_named_equal(gogo
, name
);
1873 else if (this->struct_type() != NULL
)
1874 this->struct_type()->write_equal_function(gogo
, name
);
1875 else if (this->array_type() != NULL
)
1876 this->array_type()->write_equal_function(gogo
, name
);
1880 b
= gogo
->finish_block(bloc
);
1881 gogo
->add_block(b
, bloc
);
1882 gogo
->lower_block(equal_fn
, b
);
1883 gogo
->finish_function(bloc
);
1885 // Build the function descriptors for the type descriptor to refer to.
1886 hash_fn
->func_value()->descriptor(gogo
, hash_fn
);
1887 equal_fn
->func_value()->descriptor(gogo
, equal_fn
);
1890 // Write a hash function that simply calls the hash function for a
1891 // named type. This is used when one named type is defined as
1892 // another. This ensures that this case works when the other named
1893 // type is defined in another package and relies on calling hash
1894 // functions defined only in that package.
1897 Type::write_named_hash(Gogo
* gogo
, Named_type
* name
,
1898 Function_type
* hash_fntype
, Function_type
* equal_fntype
)
1900 Location bloc
= Linemap::predeclared_location();
1902 Named_type
* base_type
= name
->real_type()->named_type();
1903 go_assert(base_type
!= NULL
);
1905 // The pointer to the type we are going to hash. This is an
1907 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
1908 go_assert(key_arg
!= NULL
);
1910 // The size of the type we are going to hash.
1911 Named_object
* keysz_arg
= gogo
->lookup("key_size", NULL
);
1912 go_assert(keysz_arg
!= NULL
);
1914 Named_object
* hash_fn
;
1915 Named_object
* equal_fn
;
1916 name
->real_type()->type_functions(gogo
, base_type
, hash_fntype
, equal_fntype
,
1917 &hash_fn
, &equal_fn
);
1919 // Call the hash function for the base type.
1920 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
1921 Expression
* keysz_ref
= Expression::make_var_reference(keysz_arg
, bloc
);
1922 Expression_list
* args
= new Expression_list();
1923 args
->push_back(key_ref
);
1924 args
->push_back(keysz_ref
);
1925 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
1926 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
1928 // Return the hash of the base type.
1929 Expression_list
* vals
= new Expression_list();
1930 vals
->push_back(call
);
1931 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
1932 gogo
->add_statement(s
);
1935 // Write an equality function that simply calls the equality function
1936 // for a named type. This is used when one named type is defined as
1937 // another. This ensures that this case works when the other named
1938 // type is defined in another package and relies on calling equality
1939 // functions defined only in that package.
1942 Type::write_named_equal(Gogo
* gogo
, Named_type
* name
)
1944 Location bloc
= Linemap::predeclared_location();
1946 // The pointers to the types we are going to compare. These have
1947 // type unsafe.Pointer.
1948 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
1949 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
1950 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
1952 Named_type
* base_type
= name
->real_type()->named_type();
1953 go_assert(base_type
!= NULL
);
1955 // Build temporaries with the base type.
1956 Type
* pt
= Type::make_pointer_type(base_type
);
1958 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
1959 ref
= Expression::make_cast(pt
, ref
, bloc
);
1960 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
1961 gogo
->add_statement(p1
);
1963 ref
= Expression::make_var_reference(key2_arg
, bloc
);
1964 ref
= Expression::make_cast(pt
, ref
, bloc
);
1965 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
1966 gogo
->add_statement(p2
);
1968 // Compare the values for equality.
1969 Expression
* t1
= Expression::make_temporary_reference(p1
, bloc
);
1970 t1
= Expression::make_unary(OPERATOR_MULT
, t1
, bloc
);
1972 Expression
* t2
= Expression::make_temporary_reference(p2
, bloc
);
1973 t2
= Expression::make_unary(OPERATOR_MULT
, t2
, bloc
);
1975 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, t1
, t2
, bloc
);
1977 // Return the equality comparison.
1978 Expression_list
* vals
= new Expression_list();
1979 vals
->push_back(cond
);
1980 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
1981 gogo
->add_statement(s
);
1984 // Return a composite literal for the type descriptor for a plain type
1985 // of kind RUNTIME_TYPE_KIND named NAME.
1988 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1989 Named_type
* name
, const Methods
* methods
,
1990 bool only_value_methods
)
1992 Location bloc
= Linemap::predeclared_location();
1994 Type
* td_type
= Type::make_type_descriptor_type();
1995 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1997 Expression_list
* vals
= new Expression_list();
2000 if (!this->has_pointer())
2001 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
2002 if (this->points_to() != NULL
)
2003 runtime_type_kind
|= RUNTIME_TYPE_KIND_DIRECT_IFACE
;
2004 Struct_field_list::const_iterator p
= fields
->begin();
2005 go_assert(p
->is_field_name("kind"));
2006 vals
->push_back(Expression::make_integer_ul(runtime_type_kind
, p
->type(),
2010 go_assert(p
->is_field_name("align"));
2011 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
2012 vals
->push_back(Expression::make_type_info(this, type_info
));
2015 go_assert(p
->is_field_name("fieldAlign"));
2016 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
2017 vals
->push_back(Expression::make_type_info(this, type_info
));
2020 go_assert(p
->is_field_name("size"));
2021 type_info
= Expression::TYPE_INFO_SIZE
;
2022 vals
->push_back(Expression::make_type_info(this, type_info
));
2025 go_assert(p
->is_field_name("hash"));
2028 h
= name
->hash_for_method(gogo
);
2030 h
= this->hash_for_method(gogo
);
2031 vals
->push_back(Expression::make_integer_ul(h
, p
->type(), bloc
));
2034 go_assert(p
->is_field_name("hashfn"));
2035 Function_type
* hash_fntype
= p
->type()->function_type();
2038 go_assert(p
->is_field_name("equalfn"));
2039 Function_type
* equal_fntype
= p
->type()->function_type();
2041 Named_object
* hash_fn
;
2042 Named_object
* equal_fn
;
2043 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
2045 vals
->push_back(Expression::make_func_reference(hash_fn
, NULL
, bloc
));
2046 vals
->push_back(Expression::make_func_reference(equal_fn
, NULL
, bloc
));
2049 go_assert(p
->is_field_name("gc"));
2050 vals
->push_back(Expression::make_gc_symbol(this));
2053 go_assert(p
->is_field_name("string"));
2054 Expression
* s
= Expression::make_string((name
!= NULL
2055 ? name
->reflection(gogo
)
2056 : this->reflection(gogo
)),
2058 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2061 go_assert(p
->is_field_name("uncommonType"));
2062 if (name
== NULL
&& methods
== NULL
)
2063 vals
->push_back(Expression::make_nil(bloc
));
2066 if (methods
== NULL
)
2067 methods
= name
->methods();
2068 vals
->push_back(this->uncommon_type_constructor(gogo
,
2071 only_value_methods
));
2075 go_assert(p
->is_field_name("ptrToThis"));
2076 if (name
== NULL
&& methods
== NULL
)
2077 vals
->push_back(Expression::make_nil(bloc
));
2082 pt
= Type::make_pointer_type(name
);
2084 pt
= Type::make_pointer_type(this);
2085 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
2089 go_assert(p
== fields
->end());
2091 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
2094 // Return a pointer to the Garbage Collection information for this type.
2097 Type::gc_symbol_pointer(Gogo
* gogo
)
2099 Type
* t
= this->forwarded();
2100 if (t
->named_type() != NULL
&& t
->named_type()->is_alias())
2101 t
= t
->named_type()->real_type();
2102 if (t
->gc_symbol_var_
== NULL
)
2104 t
->make_gc_symbol_var(gogo
);
2105 go_assert(t
->gc_symbol_var_
!= NULL
);
2107 Location bloc
= Linemap::predeclared_location();
2108 Bexpression
* var_expr
=
2109 gogo
->backend()->var_expression(t
->gc_symbol_var_
, bloc
);
2110 return gogo
->backend()->address_expression(var_expr
, bloc
);
2113 // A mapping from unnamed types to GC symbol variables.
2115 Type::GC_symbol_vars
Type::gc_symbol_vars
;
2117 // Build the GC symbol for this type.
2120 Type::make_gc_symbol_var(Gogo
* gogo
)
2122 go_assert(this->gc_symbol_var_
== NULL
);
2124 Named_type
* nt
= this->named_type();
2126 // We can have multiple instances of unnamed types and similar to type
2127 // descriptors, we only want to the emit the GC data once, so we use a
2129 Bvariable
** phash
= NULL
;
2132 Bvariable
* bvnull
= NULL
;
2133 std::pair
<GC_symbol_vars::iterator
, bool> ins
=
2134 Type::gc_symbol_vars
.insert(std::make_pair(this, bvnull
));
2137 // We've already built a gc symbol for this type.
2138 this->gc_symbol_var_
= ins
.first
->second
;
2141 phash
= &ins
.first
->second
;
2144 std::string sym_name
= this->type_descriptor_var_name(gogo
, nt
) + "$gc";
2146 // Build the contents of the gc symbol.
2147 Expression
* sym_init
= this->gc_symbol_constructor(gogo
);
2148 Btype
* sym_btype
= sym_init
->type()->get_backend(gogo
);
2150 // If the type descriptor for this type is defined somewhere else, so is the
2152 const Package
* dummy
;
2153 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
2155 this->gc_symbol_var_
=
2156 gogo
->backend()->implicit_variable_reference(sym_name
, sym_btype
);
2158 *phash
= this->gc_symbol_var_
;
2162 // See if this gc symbol can appear in multiple packages.
2163 bool is_common
= false;
2166 // We create the symbol for a builtin type whenever we need
2168 is_common
= nt
->is_builtin();
2172 // This is an unnamed type. The descriptor could be defined in
2173 // any package where it is needed, and the linker will pick one
2174 // descriptor to keep.
2178 // Since we are building the GC symbol in this package, we must create the
2179 // variable before converting the initializer to its backend representation
2180 // because the initializer may refer to the GC symbol for this type.
2181 this->gc_symbol_var_
=
2182 gogo
->backend()->implicit_variable(sym_name
, sym_btype
, false, true, is_common
, 0);
2184 *phash
= this->gc_symbol_var_
;
2186 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
2187 context
.set_is_const();
2188 Bexpression
* sym_binit
= sym_init
->get_backend(&context
);
2189 gogo
->backend()->implicit_variable_set_init(this->gc_symbol_var_
, sym_name
,
2190 sym_btype
, false, true, is_common
,
2194 // Return an array literal for the Garbage Collection information for this type.
2197 Type::gc_symbol_constructor(Gogo
* gogo
)
2199 Location bloc
= Linemap::predeclared_location();
2201 // The common GC Symbol data starts with the width of the type and ends
2202 // with the GC Opcode GC_END.
2203 // However, for certain types, the GC symbol may include extra information
2204 // before the ending opcode, so we pass the expression list into
2205 // Type::gc_symbol to allow it to add extra information as is necessary.
2206 Expression_list
* vals
= new Expression_list
;
2208 Type
* uintptr_t = Type::lookup_integer_type("uintptr");
2210 vals
->push_back(Expression::make_type_info(this,
2211 Expression::TYPE_INFO_SIZE
));
2213 Expression
* offset
= Expression::make_integer_ul(0, uintptr_t, bloc
);
2215 this->do_gc_symbol(gogo
, &vals
, &offset
, 0);
2217 vals
->push_back(Expression::make_integer_ul(GC_END
, uintptr_t, bloc
));
2219 Expression
* len
= Expression::make_integer_ul(vals
->size() + 1, NULL
,
2221 Array_type
* gc_symbol_type
= Type::make_array_type(uintptr_t, len
);
2222 return Expression::make_array_composite_literal(gc_symbol_type
, vals
, bloc
);
2225 // Advance the OFFSET of the GC symbol by this type's width.
2228 Type::advance_gc_offset(Expression
** offset
)
2230 if (this->is_error_type())
2233 Location bloc
= Linemap::predeclared_location();
2235 Expression::make_type_info(this, Expression::TYPE_INFO_SIZE
);
2236 *offset
= Expression::make_binary(OPERATOR_PLUS
, *offset
, width
, bloc
);
2239 // Return a composite literal for the uncommon type information for
2240 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
2241 // struct. If name is not NULL, it is the name of the type. If
2242 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
2243 // is true if only value methods should be included. At least one of
2244 // NAME and METHODS must not be NULL.
2247 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
2248 Named_type
* name
, const Methods
* methods
,
2249 bool only_value_methods
) const
2251 Location bloc
= Linemap::predeclared_location();
2253 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
2255 Expression_list
* vals
= new Expression_list();
2258 Struct_field_list::const_iterator p
= fields
->begin();
2259 go_assert(p
->is_field_name("name"));
2262 go_assert(p
->is_field_name("pkgPath"));
2266 vals
->push_back(Expression::make_nil(bloc
));
2267 vals
->push_back(Expression::make_nil(bloc
));
2271 Named_object
* no
= name
->named_object();
2272 std::string n
= Gogo::unpack_hidden_name(no
->name());
2273 Expression
* s
= Expression::make_string(n
, bloc
);
2274 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2276 if (name
->is_builtin())
2277 vals
->push_back(Expression::make_nil(bloc
));
2280 const Package
* package
= no
->package();
2281 const std::string
& pkgpath(package
== NULL
2283 : package
->pkgpath());
2284 s
= Expression::make_string(pkgpath
, bloc
);
2285 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2290 go_assert(p
->is_field_name("methods"));
2291 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
2292 only_value_methods
));
2295 go_assert(p
== fields
->end());
2297 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
2299 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
2302 // Sort methods by name.
2308 operator()(const std::pair
<std::string
, const Method
*>& m1
,
2309 const std::pair
<std::string
, const Method
*>& m2
) const
2311 return (Gogo::unpack_hidden_name(m1
.first
)
2312 < Gogo::unpack_hidden_name(m2
.first
));
2316 // Return a composite literal for the type method table for this type.
2317 // METHODS_TYPE is the type of the table, and is a slice type.
2318 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
2319 // then only value methods are used.
2322 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
2323 const Methods
* methods
,
2324 bool only_value_methods
) const
2326 Location bloc
= Linemap::predeclared_location();
2328 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
2329 if (methods
!= NULL
)
2331 smethods
.reserve(methods
->count());
2332 for (Methods::const_iterator p
= methods
->begin();
2333 p
!= methods
->end();
2336 if (p
->second
->is_ambiguous())
2338 if (only_value_methods
&& !p
->second
->is_value_method())
2341 // This is where we implement the magic //go:nointerface
2342 // comment. If we saw that comment, we don't add this
2343 // method to the type descriptor.
2344 if (p
->second
->nointerface())
2347 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
2351 if (smethods
.empty())
2352 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
2354 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
2356 Type
* method_type
= methods_type
->array_type()->element_type();
2358 Expression_list
* vals
= new Expression_list();
2359 vals
->reserve(smethods
.size());
2360 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
2362 p
!= smethods
.end();
2364 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
2365 p
->second
, only_value_methods
));
2367 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
2370 // Return a composite literal for a single method. METHOD_TYPE is the
2371 // type of the entry. METHOD_NAME is the name of the method and M is
2372 // the method information.
2375 Type::method_constructor(Gogo
*, Type
* method_type
,
2376 const std::string
& method_name
,
2378 bool only_value_methods
) const
2380 Location bloc
= Linemap::predeclared_location();
2382 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
2384 Expression_list
* vals
= new Expression_list();
2387 Struct_field_list::const_iterator p
= fields
->begin();
2388 go_assert(p
->is_field_name("name"));
2389 const std::string n
= Gogo::unpack_hidden_name(method_name
);
2390 Expression
* s
= Expression::make_string(n
, bloc
);
2391 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2394 go_assert(p
->is_field_name("pkgPath"));
2395 if (!Gogo::is_hidden_name(method_name
))
2396 vals
->push_back(Expression::make_nil(bloc
));
2399 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
2401 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2404 Named_object
* no
= (m
->needs_stub_method()
2406 : m
->named_object());
2408 Function_type
* mtype
;
2409 if (no
->is_function())
2410 mtype
= no
->func_value()->type();
2412 mtype
= no
->func_declaration_value()->type();
2413 go_assert(mtype
->is_method());
2414 Type
* nonmethod_type
= mtype
->copy_without_receiver();
2417 go_assert(p
->is_field_name("mtyp"));
2418 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
2421 go_assert(p
->is_field_name("typ"));
2422 bool want_pointer_receiver
= !only_value_methods
&& m
->is_value_method();
2423 nonmethod_type
= mtype
->copy_with_receiver_as_param(want_pointer_receiver
);
2424 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
2427 go_assert(p
->is_field_name("tfn"));
2428 vals
->push_back(Expression::make_func_code_reference(no
, bloc
));
2431 go_assert(p
== fields
->end());
2433 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
2436 // Return a composite literal for the type descriptor of a plain type.
2437 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
2438 // NULL, it is the name to use as well as the list of methods.
2441 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
2444 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
2448 // Return the type reflection string for this type.
2451 Type::reflection(Gogo
* gogo
) const
2455 // The do_reflection virtual function should set RET to the
2456 // reflection string.
2457 this->do_reflection(gogo
, &ret
);
2462 // Return a mangled name for the type.
2465 Type::mangled_name(Gogo
* gogo
) const
2469 // The do_mangled_name virtual function should set RET to the
2470 // mangled name. For a composite type it should append a code for
2471 // the composition and then call do_mangled_name on the components.
2472 this->do_mangled_name(gogo
, &ret
);
2477 // Return whether the backend size of the type is known.
2480 Type::is_backend_type_size_known(Gogo
* gogo
)
2482 switch (this->classification_
)
2496 case TYPE_INTERFACE
:
2501 const Struct_field_list
* fields
= this->struct_type()->fields();
2502 for (Struct_field_list::const_iterator pf
= fields
->begin();
2503 pf
!= fields
->end();
2505 if (!pf
->type()->is_backend_type_size_known(gogo
))
2512 const Array_type
* at
= this->array_type();
2513 if (at
->length() == NULL
)
2517 Numeric_constant nc
;
2518 if (!at
->length()->numeric_constant_value(&nc
))
2521 if (!nc
.to_int(&ival
))
2524 return at
->element_type()->is_backend_type_size_known(gogo
);
2529 this->named_type()->convert(gogo
);
2530 return this->named_type()->is_named_backend_type_size_known();
2534 Forward_declaration_type
* fdt
= this->forward_declaration_type();
2535 return fdt
->real_type()->is_backend_type_size_known(gogo
);
2539 case TYPE_CALL_MULTIPLE_RESULT
:
2547 // If the size of the type can be determined, set *PSIZE to the size
2548 // in bytes and return true. Otherwise, return false. This queries
2552 Type::backend_type_size(Gogo
* gogo
, int64_t *psize
)
2554 if (!this->is_backend_type_size_known(gogo
))
2556 if (this->is_error_type())
2558 Btype
* bt
= this->get_backend_placeholder(gogo
);
2559 *psize
= gogo
->backend()->type_size(bt
);
2562 if (this->named_type() != NULL
)
2563 error_at(this->named_type()->location(),
2564 "type %s larger than address space",
2565 Gogo::message_name(this->named_type()->name()).c_str());
2567 error("type %s larger than address space",
2568 this->reflection(gogo
).c_str());
2570 // Make this an error type to avoid knock-on errors.
2571 this->classification_
= TYPE_ERROR
;
2577 // If the alignment of the type can be determined, set *PALIGN to
2578 // the alignment in bytes and return true. Otherwise, return false.
2581 Type::backend_type_align(Gogo
* gogo
, int64_t *palign
)
2583 if (!this->is_backend_type_size_known(gogo
))
2585 Btype
* bt
= this->get_backend_placeholder(gogo
);
2586 *palign
= gogo
->backend()->type_alignment(bt
);
2590 // Like backend_type_align, but return the alignment when used as a
2594 Type::backend_type_field_align(Gogo
* gogo
, int64_t *palign
)
2596 if (!this->is_backend_type_size_known(gogo
))
2598 Btype
* bt
= this->get_backend_placeholder(gogo
);
2599 *palign
= gogo
->backend()->type_field_alignment(bt
);
2603 // Default function to export a type.
2606 Type::do_export(Export
*) const
2614 Type::import_type(Import
* imp
)
2616 if (imp
->match_c_string("("))
2617 return Function_type::do_import(imp
);
2618 else if (imp
->match_c_string("*"))
2619 return Pointer_type::do_import(imp
);
2620 else if (imp
->match_c_string("struct "))
2621 return Struct_type::do_import(imp
);
2622 else if (imp
->match_c_string("["))
2623 return Array_type::do_import(imp
);
2624 else if (imp
->match_c_string("map "))
2625 return Map_type::do_import(imp
);
2626 else if (imp
->match_c_string("chan "))
2627 return Channel_type::do_import(imp
);
2628 else if (imp
->match_c_string("interface"))
2629 return Interface_type::do_import(imp
);
2632 error_at(imp
->location(), "import error: expected type");
2633 return Type::make_error_type();
2637 // A type used to indicate a parsing error. This exists to simplify
2638 // later error detection.
2640 class Error_type
: public Type
2649 do_compare_is_identity(Gogo
*)
2653 do_get_backend(Gogo
* gogo
)
2654 { return gogo
->backend()->error_type(); }
2657 do_type_descriptor(Gogo
*, Named_type
*)
2658 { return Expression::make_error(Linemap::predeclared_location()); }
2661 do_reflection(Gogo
*, std::string
*) const
2662 { go_assert(saw_errors()); }
2665 do_gc_symbol(Gogo
*, Expression_list
**, Expression
**, int)
2666 { go_assert(saw_errors()); }
2669 do_mangled_name(Gogo
*, std::string
* ret
) const
2670 { ret
->push_back('E'); }
2674 Type::make_error_type()
2676 static Error_type singleton_error_type
;
2677 return &singleton_error_type
;
2682 class Void_type
: public Type
2691 do_compare_is_identity(Gogo
*)
2695 do_get_backend(Gogo
* gogo
)
2696 { return gogo
->backend()->void_type(); }
2699 do_type_descriptor(Gogo
*, Named_type
*)
2700 { go_unreachable(); }
2703 do_reflection(Gogo
*, std::string
*) const
2707 do_gc_symbol(Gogo
*, Expression_list
**, Expression
**, int)
2711 do_mangled_name(Gogo
*, std::string
* ret
) const
2712 { ret
->push_back('v'); }
2716 Type::make_void_type()
2718 static Void_type singleton_void_type
;
2719 return &singleton_void_type
;
2722 // The boolean type.
2724 class Boolean_type
: public Type
2728 : Type(TYPE_BOOLEAN
)
2733 do_compare_is_identity(Gogo
*)
2737 do_get_backend(Gogo
* gogo
)
2738 { return gogo
->backend()->bool_type(); }
2741 do_type_descriptor(Gogo
*, Named_type
* name
);
2743 // We should not be asked for the reflection string of a basic type.
2745 do_reflection(Gogo
*, std::string
* ret
) const
2746 { ret
->append("bool"); }
2749 do_gc_symbol(Gogo
*, Expression_list
**, Expression
**, int);
2752 do_mangled_name(Gogo
*, std::string
* ret
) const
2753 { ret
->push_back('b'); }
2756 // Make the type descriptor.
2759 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2762 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
2765 Named_object
* no
= gogo
->lookup_global("bool");
2766 go_assert(no
!= NULL
);
2767 return Type::type_descriptor(gogo
, no
->type_value());
2771 // Update the offset of the GC symbol.
2774 Boolean_type::do_gc_symbol(Gogo
*, Expression_list
**, Expression
** offset
, int)
2775 { this->advance_gc_offset(offset
); }
2778 Type::make_boolean_type()
2780 static Boolean_type boolean_type
;
2781 return &boolean_type
;
2784 // The named type "bool".
2786 static Named_type
* named_bool_type
;
2788 // Get the named type "bool".
2791 Type::lookup_bool_type()
2793 return named_bool_type
;
2796 // Make the named type "bool".
2799 Type::make_named_bool_type()
2801 Type
* bool_type
= Type::make_boolean_type();
2802 Named_object
* named_object
=
2803 Named_object::make_type("bool", NULL
, bool_type
,
2804 Linemap::predeclared_location());
2805 Named_type
* named_type
= named_object
->type_value();
2806 named_bool_type
= named_type
;
2810 // Class Integer_type.
2812 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
2814 // Create a new integer type. Non-abstract integer types always have
2818 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
2819 int bits
, int runtime_type_kind
)
2821 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
2823 std::string
sname(name
);
2824 Named_object
* named_object
=
2825 Named_object::make_type(sname
, NULL
, integer_type
,
2826 Linemap::predeclared_location());
2827 Named_type
* named_type
= named_object
->type_value();
2828 std::pair
<Named_integer_types::iterator
, bool> ins
=
2829 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
2830 go_assert(ins
.second
);
2834 // Look up an existing integer type.
2837 Integer_type::lookup_integer_type(const char* name
)
2839 Named_integer_types::const_iterator p
=
2840 Integer_type::named_integer_types
.find(name
);
2841 go_assert(p
!= Integer_type::named_integer_types
.end());
2845 // Create a new abstract integer type.
2848 Integer_type::create_abstract_integer_type()
2850 static Integer_type
* abstract_type
;
2851 if (abstract_type
== NULL
)
2853 Type
* int_type
= Type::lookup_integer_type("int");
2854 abstract_type
= new Integer_type(true, false,
2855 int_type
->integer_type()->bits(),
2856 RUNTIME_TYPE_KIND_INT
);
2858 return abstract_type
;
2861 // Create a new abstract character type.
2864 Integer_type::create_abstract_character_type()
2866 static Integer_type
* abstract_type
;
2867 if (abstract_type
== NULL
)
2869 abstract_type
= new Integer_type(true, false, 32,
2870 RUNTIME_TYPE_KIND_INT32
);
2871 abstract_type
->set_is_rune();
2873 return abstract_type
;
2876 // Integer type compatibility.
2879 Integer_type::is_identical(const Integer_type
* t
) const
2881 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
2883 return this->is_abstract_
== t
->is_abstract_
;
2889 Integer_type::do_hash_for_method(Gogo
*) const
2891 return ((this->bits_
<< 4)
2892 + ((this->is_unsigned_
? 1 : 0) << 8)
2893 + ((this->is_abstract_
? 1 : 0) << 9));
2896 // Convert an Integer_type to the backend representation.
2899 Integer_type::do_get_backend(Gogo
* gogo
)
2901 if (this->is_abstract_
)
2903 go_assert(saw_errors());
2904 return gogo
->backend()->error_type();
2906 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
2909 // The type descriptor for an integer type. Integer types are always
2913 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2915 go_assert(name
!= NULL
|| saw_errors());
2916 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2919 // We should not be asked for the reflection string of a basic type.
2922 Integer_type::do_reflection(Gogo
*, std::string
*) const
2924 go_assert(saw_errors());
2930 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2933 snprintf(buf
, sizeof buf
, "i%s%s%de",
2934 this->is_abstract_
? "a" : "",
2935 this->is_unsigned_
? "u" : "",
2940 // Make an integer type.
2943 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
2944 int runtime_type_kind
)
2946 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
2950 // Make an abstract integer type.
2953 Type::make_abstract_integer_type()
2955 return Integer_type::create_abstract_integer_type();
2958 // Make an abstract character type.
2961 Type::make_abstract_character_type()
2963 return Integer_type::create_abstract_character_type();
2966 // Look up an integer type.
2969 Type::lookup_integer_type(const char* name
)
2971 return Integer_type::lookup_integer_type(name
);
2974 // Class Float_type.
2976 Float_type::Named_float_types
Float_type::named_float_types
;
2978 // Create a new float type. Non-abstract float types always have
2982 Float_type::create_float_type(const char* name
, int bits
,
2983 int runtime_type_kind
)
2985 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
2986 std::string
sname(name
);
2987 Named_object
* named_object
=
2988 Named_object::make_type(sname
, NULL
, float_type
,
2989 Linemap::predeclared_location());
2990 Named_type
* named_type
= named_object
->type_value();
2991 std::pair
<Named_float_types::iterator
, bool> ins
=
2992 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
2993 go_assert(ins
.second
);
2997 // Look up an existing float type.
3000 Float_type::lookup_float_type(const char* name
)
3002 Named_float_types::const_iterator p
=
3003 Float_type::named_float_types
.find(name
);
3004 go_assert(p
!= Float_type::named_float_types
.end());
3008 // Create a new abstract float type.
3011 Float_type::create_abstract_float_type()
3013 static Float_type
* abstract_type
;
3014 if (abstract_type
== NULL
)
3015 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
3016 return abstract_type
;
3019 // Whether this type is identical with T.
3022 Float_type::is_identical(const Float_type
* t
) const
3024 if (this->bits_
!= t
->bits_
)
3026 return this->is_abstract_
== t
->is_abstract_
;
3032 Float_type::do_hash_for_method(Gogo
*) const
3034 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
3037 // Convert to the backend representation.
3040 Float_type::do_get_backend(Gogo
* gogo
)
3042 return gogo
->backend()->float_type(this->bits_
);
3045 // The type descriptor for a float type. Float types are always named.
3048 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3050 go_assert(name
!= NULL
|| saw_errors());
3051 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
3054 // We should not be asked for the reflection string of a basic type.
3057 Float_type::do_reflection(Gogo
*, std::string
*) const
3059 go_assert(saw_errors());
3065 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
3068 snprintf(buf
, sizeof buf
, "f%s%de",
3069 this->is_abstract_
? "a" : "",
3074 // Make a floating point type.
3077 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
3079 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
3082 // Make an abstract float type.
3085 Type::make_abstract_float_type()
3087 return Float_type::create_abstract_float_type();
3090 // Look up a float type.
3093 Type::lookup_float_type(const char* name
)
3095 return Float_type::lookup_float_type(name
);
3098 // Class Complex_type.
3100 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
3102 // Create a new complex type. Non-abstract complex types always have
3106 Complex_type::create_complex_type(const char* name
, int bits
,
3107 int runtime_type_kind
)
3109 Complex_type
* complex_type
= new Complex_type(false, bits
,
3111 std::string
sname(name
);
3112 Named_object
* named_object
=
3113 Named_object::make_type(sname
, NULL
, complex_type
,
3114 Linemap::predeclared_location());
3115 Named_type
* named_type
= named_object
->type_value();
3116 std::pair
<Named_complex_types::iterator
, bool> ins
=
3117 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
3119 go_assert(ins
.second
);
3123 // Look up an existing complex type.
3126 Complex_type::lookup_complex_type(const char* name
)
3128 Named_complex_types::const_iterator p
=
3129 Complex_type::named_complex_types
.find(name
);
3130 go_assert(p
!= Complex_type::named_complex_types
.end());
3134 // Create a new abstract complex type.
3137 Complex_type::create_abstract_complex_type()
3139 static Complex_type
* abstract_type
;
3140 if (abstract_type
== NULL
)
3141 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
3142 return abstract_type
;
3145 // Whether this type is identical with T.
3148 Complex_type::is_identical(const Complex_type
*t
) const
3150 if (this->bits_
!= t
->bits_
)
3152 return this->is_abstract_
== t
->is_abstract_
;
3158 Complex_type::do_hash_for_method(Gogo
*) const
3160 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
3163 // Convert to the backend representation.
3166 Complex_type::do_get_backend(Gogo
* gogo
)
3168 return gogo
->backend()->complex_type(this->bits_
);
3171 // The type descriptor for a complex type. Complex types are always
3175 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3177 go_assert(name
!= NULL
|| saw_errors());
3178 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
3181 // We should not be asked for the reflection string of a basic type.
3184 Complex_type::do_reflection(Gogo
*, std::string
*) const
3186 go_assert(saw_errors());
3192 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
3195 snprintf(buf
, sizeof buf
, "c%s%de",
3196 this->is_abstract_
? "a" : "",
3201 // Make a complex type.
3204 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
3206 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
3209 // Make an abstract complex type.
3212 Type::make_abstract_complex_type()
3214 return Complex_type::create_abstract_complex_type();
3217 // Look up a complex type.
3220 Type::lookup_complex_type(const char* name
)
3222 return Complex_type::lookup_complex_type(name
);
3225 // Class String_type.
3227 // Convert String_type to the backend representation. A string is a
3228 // struct with two fields: a pointer to the characters and a length.
3231 String_type::do_get_backend(Gogo
* gogo
)
3233 static Btype
* backend_string_type
;
3234 if (backend_string_type
== NULL
)
3236 std::vector
<Backend::Btyped_identifier
> fields(2);
3238 Type
* b
= gogo
->lookup_global("byte")->type_value();
3239 Type
* pb
= Type::make_pointer_type(b
);
3241 // We aren't going to get back to this field to finish the
3242 // backend representation, so force it to be finished now.
3243 if (!gogo
->named_types_are_converted())
3245 Btype
* bt
= pb
->get_backend_placeholder(gogo
);
3246 pb
->finish_backend(gogo
, bt
);
3249 fields
[0].name
= "__data";
3250 fields
[0].btype
= pb
->get_backend(gogo
);
3251 fields
[0].location
= Linemap::predeclared_location();
3253 Type
* int_type
= Type::lookup_integer_type("int");
3254 fields
[1].name
= "__length";
3255 fields
[1].btype
= int_type
->get_backend(gogo
);
3256 fields
[1].location
= fields
[0].location
;
3258 backend_string_type
= gogo
->backend()->struct_type(fields
);
3260 return backend_string_type
;
3263 // The type descriptor for the string type.
3266 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3269 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
3272 Named_object
* no
= gogo
->lookup_global("string");
3273 go_assert(no
!= NULL
);
3274 return Type::type_descriptor(gogo
, no
->type_value());
3278 // We should not be asked for the reflection string of a basic type.
3281 String_type::do_reflection(Gogo
*, std::string
* ret
) const
3283 ret
->append("string");
3286 // Generate GC symbol for strings.
3289 String_type::do_gc_symbol(Gogo
*, Expression_list
** vals
,
3290 Expression
** offset
, int)
3292 Location bloc
= Linemap::predeclared_location();
3293 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3294 (*vals
)->push_back(Expression::make_integer_ul(GC_STRING
, uintptr_type
,
3296 (*vals
)->push_back(*offset
);
3297 this->advance_gc_offset(offset
);
3300 // Mangled name of a string type.
3303 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
3305 ret
->push_back('z');
3308 // Make a string type.
3311 Type::make_string_type()
3313 static String_type string_type
;
3314 return &string_type
;
3317 // The named type "string".
3319 static Named_type
* named_string_type
;
3321 // Get the named type "string".
3324 Type::lookup_string_type()
3326 return named_string_type
;
3329 // Make the named type string.
3332 Type::make_named_string_type()
3334 Type
* string_type
= Type::make_string_type();
3335 Named_object
* named_object
=
3336 Named_object::make_type("string", NULL
, string_type
,
3337 Linemap::predeclared_location());
3338 Named_type
* named_type
= named_object
->type_value();
3339 named_string_type
= named_type
;
3343 // The sink type. This is the type of the blank identifier _. Any
3344 // type may be assigned to it.
3346 class Sink_type
: public Type
3355 do_compare_is_identity(Gogo
*)
3359 do_get_backend(Gogo
*)
3360 { go_unreachable(); }
3363 do_type_descriptor(Gogo
*, Named_type
*)
3364 { go_unreachable(); }
3367 do_reflection(Gogo
*, std::string
*) const
3368 { go_unreachable(); }
3371 do_gc_symbol(Gogo
*, Expression_list
**, Expression
**, int)
3372 { go_unreachable(); }
3375 do_mangled_name(Gogo
*, std::string
*) const
3376 { go_unreachable(); }
3379 // Make the sink type.
3382 Type::make_sink_type()
3384 static Sink_type sink_type
;
3388 // Class Function_type.
3393 Function_type::do_traverse(Traverse
* traverse
)
3395 if (this->receiver_
!= NULL
3396 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
3397 return TRAVERSE_EXIT
;
3398 if (this->parameters_
!= NULL
3399 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
3400 return TRAVERSE_EXIT
;
3401 if (this->results_
!= NULL
3402 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
3403 return TRAVERSE_EXIT
;
3404 return TRAVERSE_CONTINUE
;
3407 // Returns whether T is a valid redeclaration of this type. If this
3408 // returns false, and REASON is not NULL, *REASON may be set to a
3409 // brief explanation of why it returned false.
3412 Function_type::is_valid_redeclaration(const Function_type
* t
,
3413 std::string
* reason
) const
3415 if (!this->is_identical(t
, false, true, reason
))
3418 // A redeclaration of a function is required to use the same names
3419 // for the receiver and parameters.
3420 if (this->receiver() != NULL
3421 && this->receiver()->name() != t
->receiver()->name())
3424 *reason
= "receiver name changed";
3428 const Typed_identifier_list
* parms1
= this->parameters();
3429 const Typed_identifier_list
* parms2
= t
->parameters();
3432 Typed_identifier_list::const_iterator p1
= parms1
->begin();
3433 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
3434 p2
!= parms2
->end();
3437 if (p1
->name() != p2
->name())
3440 *reason
= "parameter name changed";
3444 // This is called at parse time, so we may have unknown
3446 Type
* t1
= p1
->type()->forwarded();
3447 Type
* t2
= p2
->type()->forwarded();
3449 && t1
->forward_declaration_type() != NULL
3450 && (t2
->forward_declaration_type() == NULL
3451 || (t1
->forward_declaration_type()->named_object()
3452 != t2
->forward_declaration_type()->named_object())))
3457 const Typed_identifier_list
* results1
= this->results();
3458 const Typed_identifier_list
* results2
= t
->results();
3459 if (results1
!= NULL
)
3461 Typed_identifier_list::const_iterator res1
= results1
->begin();
3462 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
3463 res2
!= results2
->end();
3466 if (res1
->name() != res2
->name())
3469 *reason
= "result name changed";
3473 // This is called at parse time, so we may have unknown
3475 Type
* t1
= res1
->type()->forwarded();
3476 Type
* t2
= res2
->type()->forwarded();
3478 && t1
->forward_declaration_type() != NULL
3479 && (t2
->forward_declaration_type() == NULL
3480 || (t1
->forward_declaration_type()->named_object()
3481 != t2
->forward_declaration_type()->named_object())))
3489 // Check whether T is the same as this type.
3492 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
3493 bool errors_are_identical
,
3494 std::string
* reason
) const
3496 if (!ignore_receiver
)
3498 const Typed_identifier
* r1
= this->receiver();
3499 const Typed_identifier
* r2
= t
->receiver();
3500 if ((r1
!= NULL
) != (r2
!= NULL
))
3503 *reason
= _("different receiver types");
3508 if (!Type::are_identical(r1
->type(), r2
->type(), errors_are_identical
,
3511 if (reason
!= NULL
&& !reason
->empty())
3512 *reason
= "receiver: " + *reason
;
3518 const Typed_identifier_list
* parms1
= this->parameters();
3519 const Typed_identifier_list
* parms2
= t
->parameters();
3520 if ((parms1
!= NULL
) != (parms2
!= NULL
))
3523 *reason
= _("different number of parameters");
3528 Typed_identifier_list::const_iterator p1
= parms1
->begin();
3529 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
3530 p2
!= parms2
->end();
3533 if (p1
== parms1
->end())
3536 *reason
= _("different number of parameters");
3540 if (!Type::are_identical(p1
->type(), p2
->type(),
3541 errors_are_identical
, NULL
))
3544 *reason
= _("different parameter types");
3548 if (p1
!= parms1
->end())
3551 *reason
= _("different number of parameters");
3556 if (this->is_varargs() != t
->is_varargs())
3559 *reason
= _("different varargs");
3563 const Typed_identifier_list
* results1
= this->results();
3564 const Typed_identifier_list
* results2
= t
->results();
3565 if ((results1
!= NULL
) != (results2
!= NULL
))
3568 *reason
= _("different number of results");
3571 if (results1
!= NULL
)
3573 Typed_identifier_list::const_iterator res1
= results1
->begin();
3574 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
3575 res2
!= results2
->end();
3578 if (res1
== results1
->end())
3581 *reason
= _("different number of results");
3585 if (!Type::are_identical(res1
->type(), res2
->type(),
3586 errors_are_identical
, NULL
))
3589 *reason
= _("different result types");
3593 if (res1
!= results1
->end())
3596 *reason
= _("different number of results");
3607 Function_type::do_hash_for_method(Gogo
* gogo
) const
3609 unsigned int ret
= 0;
3610 // We ignore the receiver type for hash codes, because we need to
3611 // get the same hash code for a method in an interface and a method
3612 // declared for a type. The former will not have a receiver.
3613 if (this->parameters_
!= NULL
)
3616 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
3617 p
!= this->parameters_
->end();
3619 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
3621 if (this->results_
!= NULL
)
3624 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
3625 p
!= this->results_
->end();
3627 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
3629 if (this->is_varargs_
)
3635 // Hash result parameters.
3638 Function_type::Results_hash::operator()(const Typed_identifier_list
* t
) const
3640 unsigned int hash
= 0;
3641 for (Typed_identifier_list::const_iterator p
= t
->begin();
3646 hash
= Type::hash_string(p
->name(), hash
);
3647 hash
+= p
->type()->hash_for_method(NULL
);
3652 // Compare result parameters so that can map identical result
3653 // parameters to a single struct type.
3656 Function_type::Results_equal::operator()(const Typed_identifier_list
* a
,
3657 const Typed_identifier_list
* b
) const
3659 if (a
->size() != b
->size())
3661 Typed_identifier_list::const_iterator pa
= a
->begin();
3662 for (Typed_identifier_list::const_iterator pb
= b
->begin();
3666 if (pa
->name() != pb
->name()
3667 || !Type::are_identical(pa
->type(), pb
->type(), true, NULL
))
3673 // Hash from results to a backend struct type.
3675 Function_type::Results_structs
Function_type::results_structs
;
3677 // Get the backend representation for a function type.
3680 Function_type::get_backend_fntype(Gogo
* gogo
)
3682 if (this->fnbtype_
== NULL
)
3684 Backend::Btyped_identifier breceiver
;
3685 if (this->receiver_
!= NULL
)
3687 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
3689 // We always pass the address of the receiver parameter, in
3690 // order to make interface calls work with unknown types.
3691 Type
* rtype
= this->receiver_
->type();
3692 if (rtype
->points_to() == NULL
)
3693 rtype
= Type::make_pointer_type(rtype
);
3694 breceiver
.btype
= rtype
->get_backend(gogo
);
3695 breceiver
.location
= this->receiver_
->location();
3698 std::vector
<Backend::Btyped_identifier
> bparameters
;
3699 if (this->parameters_
!= NULL
)
3701 bparameters
.resize(this->parameters_
->size());
3703 for (Typed_identifier_list::const_iterator p
=
3704 this->parameters_
->begin(); p
!= this->parameters_
->end();
3707 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
3708 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
3709 bparameters
[i
].location
= p
->location();
3711 go_assert(i
== bparameters
.size());
3714 std::vector
<Backend::Btyped_identifier
> bresults
;
3715 Btype
* bresult_struct
= NULL
;
3716 if (this->results_
!= NULL
)
3718 bresults
.resize(this->results_
->size());
3720 for (Typed_identifier_list::const_iterator p
=
3721 this->results_
->begin();
3722 p
!= this->results_
->end();
3725 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
3726 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
3727 bresults
[i
].location
= p
->location();
3729 go_assert(i
== bresults
.size());
3731 if (this->results_
->size() > 1)
3733 // Use the same results struct for all functions that
3734 // return the same set of results. This is useful to
3735 // unify calls to interface methods with other calls.
3736 std::pair
<Typed_identifier_list
*, Btype
*> val
;
3737 val
.first
= this->results_
;
3739 std::pair
<Results_structs::iterator
, bool> ins
=
3740 Function_type::results_structs
.insert(val
);
3743 // Build a new struct type.
3744 Struct_field_list
* sfl
= new Struct_field_list
;
3745 for (Typed_identifier_list::const_iterator p
=
3746 this->results_
->begin();
3747 p
!= this->results_
->end();
3750 Typed_identifier tid
= *p
;
3751 if (tid
.name().empty())
3752 tid
= Typed_identifier("UNNAMED", tid
.type(),
3754 sfl
->push_back(Struct_field(tid
));
3756 Struct_type
* st
= Type::make_struct_type(sfl
,
3758 ins
.first
->second
= st
->get_backend(gogo
);
3760 bresult_struct
= ins
.first
->second
;
3764 this->fnbtype_
= gogo
->backend()->function_type(breceiver
, bparameters
,
3765 bresults
, bresult_struct
,
3770 return this->fnbtype_
;
3773 // Get the backend representation for a Go function type.
3776 Function_type::do_get_backend(Gogo
* gogo
)
3778 // When we do anything with a function value other than call it, it
3779 // is represented as a pointer to a struct whose first field is the
3780 // actual function. So that is what we return as the type of a Go
3783 Location loc
= this->location();
3784 Btype
* struct_type
=
3785 gogo
->backend()->placeholder_struct_type("__go_descriptor", loc
);
3786 Btype
* ptr_struct_type
= gogo
->backend()->pointer_type(struct_type
);
3788 std::vector
<Backend::Btyped_identifier
> fields(1);
3789 fields
[0].name
= "code";
3790 fields
[0].btype
= this->get_backend_fntype(gogo
);
3791 fields
[0].location
= loc
;
3792 if (!gogo
->backend()->set_placeholder_struct_type(struct_type
, fields
))
3793 return gogo
->backend()->error_type();
3794 return ptr_struct_type
;
3797 // The type of a function type descriptor.
3800 Function_type::make_function_type_descriptor_type()
3805 Type
* tdt
= Type::make_type_descriptor_type();
3806 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3808 Type
* bool_type
= Type::lookup_bool_type();
3810 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
3812 Struct_type
* s
= Type::make_builtin_struct_type(4,
3814 "dotdotdot", bool_type
,
3818 ret
= Type::make_builtin_named_type("FuncType", s
);
3824 // The type descriptor for a function type.
3827 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3829 Location bloc
= Linemap::predeclared_location();
3831 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
3833 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
3835 Expression_list
* vals
= new Expression_list();
3838 Struct_field_list::const_iterator p
= fields
->begin();
3839 go_assert(p
->is_field_name("commonType"));
3840 vals
->push_back(this->type_descriptor_constructor(gogo
,
3841 RUNTIME_TYPE_KIND_FUNC
,
3845 go_assert(p
->is_field_name("dotdotdot"));
3846 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
3849 go_assert(p
->is_field_name("in"));
3850 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
3851 this->parameters()));
3854 go_assert(p
->is_field_name("out"));
3855 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
3859 go_assert(p
== fields
->end());
3861 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
3864 // Return a composite literal for the parameters or results of a type
3868 Function_type::type_descriptor_params(Type
* params_type
,
3869 const Typed_identifier
* receiver
,
3870 const Typed_identifier_list
* params
)
3872 Location bloc
= Linemap::predeclared_location();
3874 if (receiver
== NULL
&& params
== NULL
)
3875 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
3877 Expression_list
* vals
= new Expression_list();
3878 vals
->reserve((params
== NULL
? 0 : params
->size())
3879 + (receiver
!= NULL
? 1 : 0));
3881 if (receiver
!= NULL
)
3882 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
3886 for (Typed_identifier_list::const_iterator p
= params
->begin();
3889 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
3892 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
3895 // The reflection string.
3898 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3900 // FIXME: Turn this off until we straighten out the type of the
3901 // struct field used in a go statement which calls a method.
3902 // go_assert(this->receiver_ == NULL);
3904 ret
->append("func");
3906 if (this->receiver_
!= NULL
)
3908 ret
->push_back('(');
3909 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
3910 ret
->push_back(')');
3913 ret
->push_back('(');
3914 const Typed_identifier_list
* params
= this->parameters();
3917 bool is_varargs
= this->is_varargs_
;
3918 for (Typed_identifier_list::const_iterator p
= params
->begin();
3922 if (p
!= params
->begin())
3924 if (!is_varargs
|| p
+ 1 != params
->end())
3925 this->append_reflection(p
->type(), gogo
, ret
);
3929 this->append_reflection(p
->type()->array_type()->element_type(),
3934 ret
->push_back(')');
3936 const Typed_identifier_list
* results
= this->results();
3937 if (results
!= NULL
&& !results
->empty())
3939 if (results
->size() == 1)
3940 ret
->push_back(' ');
3943 for (Typed_identifier_list::const_iterator p
= results
->begin();
3944 p
!= results
->end();
3947 if (p
!= results
->begin())
3949 this->append_reflection(p
->type(), gogo
, ret
);
3951 if (results
->size() > 1)
3952 ret
->push_back(')');
3956 // Generate GC symbol for a function type.
3959 Function_type::do_gc_symbol(Gogo
*, Expression_list
** vals
,
3960 Expression
** offset
, int)
3962 Location bloc
= Linemap::predeclared_location();
3963 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3965 // We use GC_APTR here because we do not currently have a way to describe the
3966 // the type of the possible function closure. FIXME.
3967 (*vals
)->push_back(Expression::make_integer_ul(GC_APTR
, uintptr_type
, bloc
));
3968 (*vals
)->push_back(*offset
);
3969 this->advance_gc_offset(offset
);
3975 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3977 ret
->push_back('F');
3979 if (this->receiver_
!= NULL
)
3981 ret
->push_back('m');
3982 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
3985 const Typed_identifier_list
* params
= this->parameters();
3988 ret
->push_back('p');
3989 for (Typed_identifier_list::const_iterator p
= params
->begin();
3992 this->append_mangled_name(p
->type(), gogo
, ret
);
3993 if (this->is_varargs_
)
3994 ret
->push_back('V');
3995 ret
->push_back('e');
3998 const Typed_identifier_list
* results
= this->results();
3999 if (results
!= NULL
)
4001 ret
->push_back('r');
4002 for (Typed_identifier_list::const_iterator p
= results
->begin();
4003 p
!= results
->end();
4005 this->append_mangled_name(p
->type(), gogo
, ret
);
4006 ret
->push_back('e');
4009 ret
->push_back('e');
4012 // Export a function type.
4015 Function_type::do_export(Export
* exp
) const
4017 // We don't write out the receiver. The only function types which
4018 // should have a receiver are the ones associated with explicitly
4019 // defined methods. For those the receiver type is written out by
4020 // Function::export_func.
4022 exp
->write_c_string("(");
4024 if (this->parameters_
!= NULL
)
4026 bool is_varargs
= this->is_varargs_
;
4027 for (Typed_identifier_list::const_iterator p
=
4028 this->parameters_
->begin();
4029 p
!= this->parameters_
->end();
4035 exp
->write_c_string(", ");
4036 exp
->write_name(p
->name());
4037 exp
->write_c_string(" ");
4038 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
4039 exp
->write_type(p
->type());
4042 exp
->write_c_string("...");
4043 exp
->write_type(p
->type()->array_type()->element_type());
4047 exp
->write_c_string(")");
4049 const Typed_identifier_list
* results
= this->results_
;
4050 if (results
!= NULL
)
4052 exp
->write_c_string(" ");
4053 if (results
->size() == 1 && results
->begin()->name().empty())
4054 exp
->write_type(results
->begin()->type());
4058 exp
->write_c_string("(");
4059 for (Typed_identifier_list::const_iterator p
= results
->begin();
4060 p
!= results
->end();
4066 exp
->write_c_string(", ");
4067 exp
->write_name(p
->name());
4068 exp
->write_c_string(" ");
4069 exp
->write_type(p
->type());
4071 exp
->write_c_string(")");
4076 // Import a function type.
4079 Function_type::do_import(Import
* imp
)
4081 imp
->require_c_string("(");
4082 Typed_identifier_list
* parameters
;
4083 bool is_varargs
= false;
4084 if (imp
->peek_char() == ')')
4088 parameters
= new Typed_identifier_list();
4091 std::string name
= imp
->read_name();
4092 imp
->require_c_string(" ");
4094 if (imp
->match_c_string("..."))
4100 Type
* ptype
= imp
->read_type();
4102 ptype
= Type::make_array_type(ptype
, NULL
);
4103 parameters
->push_back(Typed_identifier(name
, ptype
,
4105 if (imp
->peek_char() != ',')
4107 go_assert(!is_varargs
);
4108 imp
->require_c_string(", ");
4111 imp
->require_c_string(")");
4113 Typed_identifier_list
* results
;
4114 if (imp
->peek_char() != ' ')
4119 results
= new Typed_identifier_list
;
4120 if (imp
->peek_char() != '(')
4122 Type
* rtype
= imp
->read_type();
4123 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
4130 std::string name
= imp
->read_name();
4131 imp
->require_c_string(" ");
4132 Type
* rtype
= imp
->read_type();
4133 results
->push_back(Typed_identifier(name
, rtype
,
4135 if (imp
->peek_char() != ',')
4137 imp
->require_c_string(", ");
4139 imp
->require_c_string(")");
4143 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
4146 ret
->set_is_varargs();
4150 // Make a copy of a function type without a receiver.
4153 Function_type::copy_without_receiver() const
4155 go_assert(this->is_method());
4156 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
4159 if (this->is_varargs())
4160 ret
->set_is_varargs();
4161 if (this->is_builtin())
4162 ret
->set_is_builtin();
4166 // Make a copy of a function type with a receiver.
4169 Function_type::copy_with_receiver(Type
* receiver_type
) const
4171 go_assert(!this->is_method());
4172 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
4174 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
4177 if (this->is_varargs_
)
4178 ret
->set_is_varargs();
4182 // Make a copy of a function type with the receiver as the first
4186 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver
) const
4188 go_assert(this->is_method());
4189 Typed_identifier_list
* new_params
= new Typed_identifier_list();
4190 Type
* rtype
= this->receiver_
->type();
4191 if (want_pointer_receiver
)
4192 rtype
= Type::make_pointer_type(rtype
);
4193 Typed_identifier
receiver(this->receiver_
->name(), rtype
,
4194 this->receiver_
->location());
4195 new_params
->push_back(receiver
);
4196 const Typed_identifier_list
* orig_params
= this->parameters_
;
4197 if (orig_params
!= NULL
&& !orig_params
->empty())
4199 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
4200 p
!= orig_params
->end();
4202 new_params
->push_back(*p
);
4204 return Type::make_function_type(NULL
, new_params
, this->results_
,
4208 // Make a copy of a function type ignoring any receiver and adding a
4209 // closure parameter.
4212 Function_type::copy_with_names() const
4214 Typed_identifier_list
* new_params
= new Typed_identifier_list();
4215 const Typed_identifier_list
* orig_params
= this->parameters_
;
4216 if (orig_params
!= NULL
&& !orig_params
->empty())
4220 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
4221 p
!= orig_params
->end();
4224 snprintf(buf
, sizeof buf
, "pt.%u", count
);
4226 new_params
->push_back(Typed_identifier(buf
, p
->type(),
4231 const Typed_identifier_list
* orig_results
= this->results_
;
4232 Typed_identifier_list
* new_results
;
4233 if (orig_results
== NULL
|| orig_results
->empty())
4237 new_results
= new Typed_identifier_list();
4238 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
4239 p
!= orig_results
->end();
4241 new_results
->push_back(Typed_identifier("", p
->type(),
4245 return Type::make_function_type(NULL
, new_params
, new_results
,
4249 // Make a function type.
4252 Type::make_function_type(Typed_identifier
* receiver
,
4253 Typed_identifier_list
* parameters
,
4254 Typed_identifier_list
* results
,
4257 return new Function_type(receiver
, parameters
, results
, location
);
4260 // Make a backend function type.
4262 Backend_function_type
*
4263 Type::make_backend_function_type(Typed_identifier
* receiver
,
4264 Typed_identifier_list
* parameters
,
4265 Typed_identifier_list
* results
,
4268 return new Backend_function_type(receiver
, parameters
, results
, location
);
4271 // Class Pointer_type.
4276 Pointer_type::do_traverse(Traverse
* traverse
)
4278 return Type::traverse(this->to_type_
, traverse
);
4284 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
4286 return this->to_type_
->hash_for_method(gogo
) << 4;
4289 // Get the backend representation for a pointer type.
4292 Pointer_type::do_get_backend(Gogo
* gogo
)
4294 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
4295 return gogo
->backend()->pointer_type(to_btype
);
4298 // The type of a pointer type descriptor.
4301 Pointer_type::make_pointer_type_descriptor_type()
4306 Type
* tdt
= Type::make_type_descriptor_type();
4307 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4309 Struct_type
* s
= Type::make_builtin_struct_type(2,
4313 ret
= Type::make_builtin_named_type("PtrType", s
);
4319 // The type descriptor for a pointer type.
4322 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4324 if (this->is_unsafe_pointer_type())
4326 go_assert(name
!= NULL
);
4327 return this->plain_type_descriptor(gogo
,
4328 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
4333 Location bloc
= Linemap::predeclared_location();
4335 const Methods
* methods
;
4336 Type
* deref
= this->points_to();
4337 if (deref
->named_type() != NULL
)
4338 methods
= deref
->named_type()->methods();
4339 else if (deref
->struct_type() != NULL
)
4340 methods
= deref
->struct_type()->methods();
4344 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
4346 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
4348 Expression_list
* vals
= new Expression_list();
4351 Struct_field_list::const_iterator p
= fields
->begin();
4352 go_assert(p
->is_field_name("commonType"));
4353 vals
->push_back(this->type_descriptor_constructor(gogo
,
4354 RUNTIME_TYPE_KIND_PTR
,
4355 name
, methods
, false));
4358 go_assert(p
->is_field_name("elem"));
4359 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
4361 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
4365 // Reflection string.
4368 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4370 ret
->push_back('*');
4371 this->append_reflection(this->to_type_
, gogo
, ret
);
4374 // Generate GC symbol for pointer types.
4377 Pointer_type::do_gc_symbol(Gogo
*, Expression_list
** vals
,
4378 Expression
** offset
, int)
4380 Location loc
= Linemap::predeclared_location();
4381 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4383 unsigned long opval
= this->to_type_
->has_pointer() ? GC_PTR
: GC_APTR
;
4384 (*vals
)->push_back(Expression::make_integer_ul(opval
, uintptr_type
, loc
));
4385 (*vals
)->push_back(*offset
);
4387 if (this->to_type_
->has_pointer())
4388 (*vals
)->push_back(Expression::make_gc_symbol(this->to_type_
));
4389 this->advance_gc_offset(offset
);
4395 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4397 ret
->push_back('p');
4398 this->append_mangled_name(this->to_type_
, gogo
, ret
);
4404 Pointer_type::do_export(Export
* exp
) const
4406 exp
->write_c_string("*");
4407 if (this->is_unsafe_pointer_type())
4408 exp
->write_c_string("any");
4410 exp
->write_type(this->to_type_
);
4416 Pointer_type::do_import(Import
* imp
)
4418 imp
->require_c_string("*");
4419 if (imp
->match_c_string("any"))
4422 return Type::make_pointer_type(Type::make_void_type());
4424 Type
* to
= imp
->read_type();
4425 return Type::make_pointer_type(to
);
4428 // Make a pointer type.
4431 Type::make_pointer_type(Type
* to_type
)
4433 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
4434 static Hashtable pointer_types
;
4435 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
4436 if (p
!= pointer_types
.end())
4438 Pointer_type
* ret
= new Pointer_type(to_type
);
4439 pointer_types
[to_type
] = ret
;
4443 // The nil type. We use a special type for nil because it is not the
4444 // same as any other type. In C term nil has type void*, but there is
4445 // no such type in Go.
4447 class Nil_type
: public Type
4456 do_compare_is_identity(Gogo
*)
4460 do_get_backend(Gogo
* gogo
)
4461 { return gogo
->backend()->pointer_type(gogo
->backend()->void_type()); }
4464 do_type_descriptor(Gogo
*, Named_type
*)
4465 { go_unreachable(); }
4468 do_reflection(Gogo
*, std::string
*) const
4469 { go_unreachable(); }
4472 do_gc_symbol(Gogo
*, Expression_list
**, Expression
**, int)
4473 { go_unreachable(); }
4476 do_mangled_name(Gogo
*, std::string
* ret
) const
4477 { ret
->push_back('n'); }
4480 // Make the nil type.
4483 Type::make_nil_type()
4485 static Nil_type singleton_nil_type
;
4486 return &singleton_nil_type
;
4489 // The type of a function call which returns multiple values. This is
4490 // really a struct, but we don't want to confuse a function call which
4491 // returns a struct with a function call which returns multiple
4494 class Call_multiple_result_type
: public Type
4497 Call_multiple_result_type(Call_expression
* call
)
4498 : Type(TYPE_CALL_MULTIPLE_RESULT
),
4504 do_has_pointer() const
4506 go_assert(saw_errors());
4511 do_compare_is_identity(Gogo
*)
4515 do_get_backend(Gogo
* gogo
)
4517 go_assert(saw_errors());
4518 return gogo
->backend()->error_type();
4522 do_type_descriptor(Gogo
*, Named_type
*)
4524 go_assert(saw_errors());
4525 return Expression::make_error(Linemap::unknown_location());
4529 do_reflection(Gogo
*, std::string
*) const
4530 { go_assert(saw_errors()); }
4533 do_gc_symbol(Gogo
*, Expression_list
**, Expression
**, int)
4534 { go_unreachable(); }
4537 do_mangled_name(Gogo
*, std::string
*) const
4538 { go_assert(saw_errors()); }
4541 // The expression being called.
4542 Call_expression
* call_
;
4545 // Make a call result type.
4548 Type::make_call_multiple_result_type(Call_expression
* call
)
4550 return new Call_multiple_result_type(call
);
4553 // Class Struct_field.
4555 // Get the name of a field.
4558 Struct_field::field_name() const
4560 const std::string
& name(this->typed_identifier_
.name());
4565 // This is called during parsing, before anything is lowered, so
4566 // we have to be pretty careful to avoid dereferencing an
4567 // unknown type name.
4568 Type
* t
= this->typed_identifier_
.type();
4570 if (t
->classification() == Type::TYPE_POINTER
)
4573 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
4574 dt
= ptype
->points_to();
4576 if (dt
->forward_declaration_type() != NULL
)
4577 return dt
->forward_declaration_type()->name();
4578 else if (dt
->named_type() != NULL
)
4579 return dt
->named_type()->name();
4580 else if (t
->is_error_type() || dt
->is_error_type())
4582 static const std::string error_string
= "*error*";
4583 return error_string
;
4587 // Avoid crashing in the erroneous case where T is named but
4590 if (t
->forward_declaration_type() != NULL
)
4591 return t
->forward_declaration_type()->name();
4592 else if (t
->named_type() != NULL
)
4593 return t
->named_type()->name();
4600 // Return whether this field is named NAME.
4603 Struct_field::is_field_name(const std::string
& name
) const
4605 const std::string
& me(this->typed_identifier_
.name());
4610 Type
* t
= this->typed_identifier_
.type();
4611 if (t
->points_to() != NULL
)
4613 Named_type
* nt
= t
->named_type();
4614 if (nt
!= NULL
&& nt
->name() == name
)
4617 // This is a horrible hack caused by the fact that we don't pack
4618 // the names of builtin types. FIXME.
4619 if (!this->is_imported_
4622 && nt
->name() == Gogo::unpack_hidden_name(name
))
4629 // Return whether this field is an unexported field named NAME.
4632 Struct_field::is_unexported_field_name(Gogo
* gogo
,
4633 const std::string
& name
) const
4635 const std::string
& field_name(this->field_name());
4636 if (Gogo::is_hidden_name(field_name
)
4637 && name
== Gogo::unpack_hidden_name(field_name
)
4638 && gogo
->pack_hidden_name(name
, false) != field_name
)
4641 // Check for the name of a builtin type. This is like the test in
4642 // is_field_name, only there we return false if this->is_imported_,
4643 // and here we return true.
4644 if (this->is_imported_
&& this->is_anonymous())
4646 Type
* t
= this->typed_identifier_
.type();
4647 if (t
->points_to() != NULL
)
4649 Named_type
* nt
= t
->named_type();
4652 && nt
->name() == Gogo::unpack_hidden_name(name
))
4659 // Return whether this field is an embedded built-in type.
4662 Struct_field::is_embedded_builtin(Gogo
* gogo
) const
4664 const std::string
& name(this->field_name());
4665 // We know that a field is an embedded type if it is anonymous.
4666 // We can decide if it is a built-in type by checking to see if it is
4667 // registered globally under the field's name.
4668 // This allows us to distinguish between embedded built-in types and
4669 // embedded types that are aliases to built-in types.
4670 return (this->is_anonymous()
4671 && !Gogo::is_hidden_name(name
)
4672 && gogo
->lookup_global(name
.c_str()) != NULL
);
4675 // Class Struct_type.
4677 // A hash table used to find identical unnamed structs so that they
4678 // share method tables.
4680 Struct_type::Identical_structs
Struct_type::identical_structs
;
4682 // A hash table used to merge method sets for identical unnamed
4685 Struct_type::Struct_method_tables
Struct_type::struct_method_tables
;
4690 Struct_type::do_traverse(Traverse
* traverse
)
4692 Struct_field_list
* fields
= this->fields_
;
4695 for (Struct_field_list::iterator p
= fields
->begin();
4699 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
4700 return TRAVERSE_EXIT
;
4703 return TRAVERSE_CONTINUE
;
4706 // Verify that the struct type is complete and valid.
4709 Struct_type::do_verify()
4711 Struct_field_list
* fields
= this->fields_
;
4714 for (Struct_field_list::iterator p
= fields
->begin();
4718 Type
* t
= p
->type();
4719 if (p
->is_anonymous())
4721 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
4723 error_at(p
->location(), "embedded type may not be a pointer");
4724 p
->set_type(Type::make_error_type());
4726 else if (t
->points_to() != NULL
4727 && t
->points_to()->interface_type() != NULL
)
4729 error_at(p
->location(),
4730 "embedded type may not be pointer to interface");
4731 p
->set_type(Type::make_error_type());
4738 // Whether this contains a pointer.
4741 Struct_type::do_has_pointer() const
4743 const Struct_field_list
* fields
= this->fields();
4746 for (Struct_field_list::const_iterator p
= fields
->begin();
4750 if (p
->type()->has_pointer())
4756 // Whether this type is identical to T.
4759 Struct_type::is_identical(const Struct_type
* t
,
4760 bool errors_are_identical
) const
4762 const Struct_field_list
* fields1
= this->fields();
4763 const Struct_field_list
* fields2
= t
->fields();
4764 if (fields1
== NULL
|| fields2
== NULL
)
4765 return fields1
== fields2
;
4766 Struct_field_list::const_iterator pf2
= fields2
->begin();
4767 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
4768 pf1
!= fields1
->end();
4771 if (pf2
== fields2
->end())
4773 if (pf1
->field_name() != pf2
->field_name())
4775 if (pf1
->is_anonymous() != pf2
->is_anonymous()
4776 || !Type::are_identical(pf1
->type(), pf2
->type(),
4777 errors_are_identical
, NULL
))
4779 if (!pf1
->has_tag())
4786 if (!pf2
->has_tag())
4788 if (pf1
->tag() != pf2
->tag())
4792 if (pf2
!= fields2
->end())
4797 // Whether comparisons of this struct type are simple identity
4801 Struct_type::do_compare_is_identity(Gogo
* gogo
)
4803 const Struct_field_list
* fields
= this->fields_
;
4807 for (Struct_field_list::const_iterator pf
= fields
->begin();
4808 pf
!= fields
->end();
4811 if (Gogo::is_sink_name(pf
->field_name()))
4814 if (!pf
->type()->compare_is_identity(gogo
))
4817 int64_t field_align
;
4818 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
4820 if ((offset
& (field_align
- 1)) != 0)
4822 // This struct has padding. We don't guarantee that that
4823 // padding is zero-initialized for a stack variable, so we
4824 // can't use memcmp to compare struct values.
4829 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
4831 offset
+= field_size
;
4834 int64_t struct_size
;
4835 if (!this->backend_type_size(gogo
, &struct_size
))
4837 if (offset
!= struct_size
)
4839 // Trailing padding may not be zero when on the stack.
4846 // Build identity and hash functions for this struct.
4851 Struct_type::do_hash_for_method(Gogo
* gogo
) const
4853 unsigned int ret
= 0;
4854 if (this->fields() != NULL
)
4856 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
4857 pf
!= this->fields()->end();
4859 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
4864 // Find the local field NAME.
4867 Struct_type::find_local_field(const std::string
& name
,
4868 unsigned int *pindex
) const
4870 const Struct_field_list
* fields
= this->fields_
;
4874 for (Struct_field_list::const_iterator pf
= fields
->begin();
4875 pf
!= fields
->end();
4878 if (pf
->is_field_name(name
))
4888 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4890 Field_reference_expression
*
4891 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
4892 Location location
) const
4895 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
4899 // Return an expression for a field, along with the depth at which it
4902 Field_reference_expression
*
4903 Struct_type::field_reference_depth(Expression
* struct_expr
,
4904 const std::string
& name
,
4906 Saw_named_type
* saw
,
4907 unsigned int* depth
) const
4909 const Struct_field_list
* fields
= this->fields_
;
4913 // Look for a field with this name.
4915 for (Struct_field_list::const_iterator pf
= fields
->begin();
4916 pf
!= fields
->end();
4919 if (pf
->is_field_name(name
))
4922 return Expression::make_field_reference(struct_expr
, i
, location
);
4926 // Look for an anonymous field which contains a field with this
4928 unsigned int found_depth
= 0;
4929 Field_reference_expression
* ret
= NULL
;
4931 for (Struct_field_list::const_iterator pf
= fields
->begin();
4932 pf
!= fields
->end();
4935 if (!pf
->is_anonymous())
4938 Struct_type
* st
= pf
->type()->deref()->struct_type();
4942 Saw_named_type
* hold_saw
= saw
;
4943 Saw_named_type saw_here
;
4944 Named_type
* nt
= pf
->type()->named_type();
4946 nt
= pf
->type()->deref()->named_type();
4950 for (q
= saw
; q
!= NULL
; q
= q
->next
)
4954 // If this is an error, it will be reported
4961 saw_here
.next
= saw
;
4966 // Look for a reference using a NULL struct expression. If we
4967 // find one, fill in the struct expression with a reference to
4969 unsigned int subdepth
;
4970 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
4980 if (ret
== NULL
|| subdepth
< found_depth
)
4985 found_depth
= subdepth
;
4986 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
4988 if (pf
->type()->points_to() != NULL
)
4989 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
4990 while (sub
->expr() != NULL
)
4992 sub
= sub
->expr()->deref()->field_reference_expression();
4993 go_assert(sub
!= NULL
);
4995 sub
->set_struct_expression(here
);
4996 sub
->set_implicit(true);
4998 else if (subdepth
> found_depth
)
5002 // We do not handle ambiguity here--it should be handled by
5003 // Type::bind_field_or_method.
5011 *depth
= found_depth
+ 1;
5016 // Return the total number of fields, including embedded fields.
5019 Struct_type::total_field_count() const
5021 if (this->fields_
== NULL
)
5023 unsigned int ret
= 0;
5024 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
5025 pf
!= this->fields_
->end();
5028 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
5031 ret
+= pf
->type()->struct_type()->total_field_count();
5036 // Return whether NAME is an unexported field, for better error reporting.
5039 Struct_type::is_unexported_local_field(Gogo
* gogo
,
5040 const std::string
& name
) const
5042 const Struct_field_list
* fields
= this->fields_
;
5045 for (Struct_field_list::const_iterator pf
= fields
->begin();
5046 pf
!= fields
->end();
5048 if (pf
->is_unexported_field_name(gogo
, name
))
5054 // Finalize the methods of an unnamed struct.
5057 Struct_type::finalize_methods(Gogo
* gogo
)
5059 if (this->all_methods_
!= NULL
)
5062 // It is possible to have multiple identical structs that have
5063 // methods. We want them to share method tables. Otherwise we will
5064 // emit identical methods more than once, which is bad since they
5065 // will even have the same names.
5066 std::pair
<Identical_structs::iterator
, bool> ins
=
5067 Struct_type::identical_structs
.insert(std::make_pair(this, this));
5070 // An identical struct was already entered into the hash table.
5071 // Note that finalize_methods is, fortunately, not recursive.
5072 this->all_methods_
= ins
.first
->second
->all_methods_
;
5076 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
5079 // Return the method NAME, or NULL if there isn't one or if it is
5080 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
5084 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
5086 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
5089 // Return a pointer to the interface method table for this type for
5090 // the interface INTERFACE. IS_POINTER is true if this is for a
5094 Struct_type::interface_method_table(Interface_type
* interface
,
5097 std::pair
<Struct_type
*, Struct_type::Struct_method_table_pair
*>
5099 std::pair
<Struct_type::Struct_method_tables::iterator
, bool> ins
=
5100 Struct_type::struct_method_tables
.insert(val
);
5102 Struct_method_table_pair
* smtp
;
5104 smtp
= ins
.first
->second
;
5107 smtp
= new Struct_method_table_pair();
5109 smtp
->second
= NULL
;
5110 ins
.first
->second
= smtp
;
5113 return Type::interface_method_table(this, interface
, is_pointer
,
5114 &smtp
->first
, &smtp
->second
);
5117 // Convert struct fields to the backend representation. This is not
5118 // declared in types.h so that types.h doesn't have to #include
5122 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
5123 bool use_placeholder
,
5124 std::vector
<Backend::Btyped_identifier
>* bfields
)
5126 bfields
->resize(fields
->size());
5128 for (Struct_field_list::const_iterator p
= fields
->begin();
5132 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
5133 (*bfields
)[i
].btype
= (use_placeholder
5134 ? p
->type()->get_backend_placeholder(gogo
)
5135 : p
->type()->get_backend(gogo
));
5136 (*bfields
)[i
].location
= p
->location();
5138 go_assert(i
== fields
->size());
5141 // Get the backend representation for a struct type.
5144 Struct_type::do_get_backend(Gogo
* gogo
)
5146 std::vector
<Backend::Btyped_identifier
> bfields
;
5147 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
5148 return gogo
->backend()->struct_type(bfields
);
5151 // Finish the backend representation of the fields of a struct.
5154 Struct_type::finish_backend_fields(Gogo
* gogo
)
5156 const Struct_field_list
* fields
= this->fields_
;
5159 for (Struct_field_list::const_iterator p
= fields
->begin();
5162 p
->type()->get_backend(gogo
);
5166 // The type of a struct type descriptor.
5169 Struct_type::make_struct_type_descriptor_type()
5174 Type
* tdt
= Type::make_type_descriptor_type();
5175 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5177 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5178 Type
* string_type
= Type::lookup_string_type();
5179 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
5182 Type::make_builtin_struct_type(5,
5183 "name", pointer_string_type
,
5184 "pkgPath", pointer_string_type
,
5186 "tag", pointer_string_type
,
5187 "offset", uintptr_type
);
5188 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
5190 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
5192 Struct_type
* s
= Type::make_builtin_struct_type(2,
5194 "fields", slice_type
);
5196 ret
= Type::make_builtin_named_type("StructType", s
);
5202 // Build a type descriptor for a struct type.
5205 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5207 Location bloc
= Linemap::predeclared_location();
5209 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
5211 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
5213 Expression_list
* vals
= new Expression_list();
5216 const Methods
* methods
= this->methods();
5217 // A named struct should not have methods--the methods should attach
5218 // to the named type.
5219 go_assert(methods
== NULL
|| name
== NULL
);
5221 Struct_field_list::const_iterator ps
= fields
->begin();
5222 go_assert(ps
->is_field_name("commonType"));
5223 vals
->push_back(this->type_descriptor_constructor(gogo
,
5224 RUNTIME_TYPE_KIND_STRUCT
,
5225 name
, methods
, true));
5228 go_assert(ps
->is_field_name("fields"));
5230 Expression_list
* elements
= new Expression_list();
5231 elements
->reserve(this->fields_
->size());
5232 Type
* element_type
= ps
->type()->array_type()->element_type();
5233 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
5234 pf
!= this->fields_
->end();
5237 const Struct_field_list
* f
= element_type
->struct_type()->fields();
5239 Expression_list
* fvals
= new Expression_list();
5242 Struct_field_list::const_iterator q
= f
->begin();
5243 go_assert(q
->is_field_name("name"));
5244 if (pf
->is_anonymous())
5245 fvals
->push_back(Expression::make_nil(bloc
));
5248 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
5249 Expression
* s
= Expression::make_string(n
, bloc
);
5250 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
5254 go_assert(q
->is_field_name("pkgPath"));
5255 bool is_embedded_builtin
= pf
->is_embedded_builtin(gogo
);
5256 if (!Gogo::is_hidden_name(pf
->field_name()) && !is_embedded_builtin
)
5257 fvals
->push_back(Expression::make_nil(bloc
));
5261 if (is_embedded_builtin
)
5262 n
= gogo
->package_name();
5264 n
= Gogo::hidden_name_pkgpath(pf
->field_name());
5265 Expression
* s
= Expression::make_string(n
, bloc
);
5266 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
5270 go_assert(q
->is_field_name("typ"));
5271 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
5274 go_assert(q
->is_field_name("tag"));
5276 fvals
->push_back(Expression::make_nil(bloc
));
5279 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
5280 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
5284 go_assert(q
->is_field_name("offset"));
5285 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
5287 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
5289 elements
->push_back(v
);
5292 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
5295 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
5298 // Write the hash function for a struct which can not use the identity
5302 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
5303 Function_type
* hash_fntype
,
5304 Function_type
* equal_fntype
)
5306 Location bloc
= Linemap::predeclared_location();
5308 // The pointer to the struct that we are going to hash. This is an
5309 // argument to the hash function we are implementing here.
5310 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
5311 go_assert(key_arg
!= NULL
);
5312 Type
* key_arg_type
= key_arg
->var_value()->type();
5314 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5317 Expression
* zero
= Expression::make_integer_ul(0, uintptr_type
, bloc
);
5319 // Make a temporary to hold the return value, initialized to 0.
5320 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
5322 gogo
->add_statement(retval
);
5324 // Make a temporary to hold the key as a uintptr.
5325 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
5326 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
5327 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
5329 gogo
->add_statement(key
);
5331 // Loop over the struct fields.
5333 const Struct_field_list
* fields
= this->fields_
;
5334 for (Struct_field_list::const_iterator pf
= fields
->begin();
5335 pf
!= fields
->end();
5338 if (Gogo::is_sink_name(pf
->field_name()))
5345 // Multiply retval by 33.
5346 Expression
* i33
= Expression::make_integer_ul(33, uintptr_type
,
5348 ref
= Expression::make_temporary_reference(retval
, bloc
);
5349 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
,
5351 gogo
->add_statement(s
);
5354 // Get a pointer to the value of this field.
5355 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
5356 ref
= Expression::make_temporary_reference(key
, bloc
);
5357 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
5359 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
5361 // Get the size of this field.
5362 Expression
* size
= Expression::make_type_info(pf
->type(),
5363 Expression::TYPE_INFO_SIZE
);
5365 // Get the hash function to use for the type of this field.
5366 Named_object
* hash_fn
;
5367 Named_object
* equal_fn
;
5368 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
5369 equal_fntype
, &hash_fn
, &equal_fn
);
5371 // Call the hash function for the field.
5372 Expression_list
* args
= new Expression_list();
5373 args
->push_back(subkey
);
5374 args
->push_back(size
);
5375 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
5376 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
5378 // Add the field's hash value to retval.
5379 Temporary_reference_expression
* tref
=
5380 Expression::make_temporary_reference(retval
, bloc
);
5381 tref
->set_is_lvalue();
5382 Statement
* s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
,
5384 gogo
->add_statement(s
);
5387 // Return retval to the caller of the hash function.
5388 Expression_list
* vals
= new Expression_list();
5389 ref
= Expression::make_temporary_reference(retval
, bloc
);
5390 vals
->push_back(ref
);
5391 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5392 gogo
->add_statement(s
);
5395 // Write the equality function for a struct which can not use the
5396 // identity function.
5399 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
5401 Location bloc
= Linemap::predeclared_location();
5403 // The pointers to the structs we are going to compare.
5404 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
5405 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
5406 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
5408 // Build temporaries with the right types.
5409 Type
* pt
= Type::make_pointer_type(name
!= NULL
5410 ? static_cast<Type
*>(name
)
5411 : static_cast<Type
*>(this));
5413 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
5414 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5415 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
5416 gogo
->add_statement(p1
);
5418 ref
= Expression::make_var_reference(key2_arg
, bloc
);
5419 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5420 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
5421 gogo
->add_statement(p2
);
5423 const Struct_field_list
* fields
= this->fields_
;
5424 unsigned int field_index
= 0;
5425 for (Struct_field_list::const_iterator pf
= fields
->begin();
5426 pf
!= fields
->end();
5427 ++pf
, ++field_index
)
5429 if (Gogo::is_sink_name(pf
->field_name()))
5432 // Compare one field in both P1 and P2.
5433 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
5434 f1
= Expression::make_unary(OPERATOR_MULT
, f1
, bloc
);
5435 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
5437 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
5438 f2
= Expression::make_unary(OPERATOR_MULT
, f2
, bloc
);
5439 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
5441 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
5443 // If the values are not equal, return false.
5444 gogo
->start_block(bloc
);
5445 Expression_list
* vals
= new Expression_list();
5446 vals
->push_back(Expression::make_boolean(false, bloc
));
5447 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5448 gogo
->add_statement(s
);
5449 Block
* then_block
= gogo
->finish_block(bloc
);
5451 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
5452 gogo
->add_statement(s
);
5455 // All the fields are equal, so return true.
5456 Expression_list
* vals
= new Expression_list();
5457 vals
->push_back(Expression::make_boolean(true, bloc
));
5458 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5459 gogo
->add_statement(s
);
5462 // Reflection string.
5465 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5467 ret
->append("struct {");
5469 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
5470 p
!= this->fields_
->end();
5473 if (p
!= this->fields_
->begin())
5474 ret
->push_back(';');
5475 ret
->push_back(' ');
5476 if (p
->is_anonymous())
5477 ret
->push_back('?');
5479 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
5480 ret
->push_back(' ');
5481 this->append_reflection(p
->type(), gogo
, ret
);
5485 const std::string
& tag(p
->tag());
5487 for (std::string::const_iterator p
= tag
.begin();
5492 ret
->append("\\x00");
5493 else if (*p
== '\n')
5495 else if (*p
== '\t')
5498 ret
->append("\\\"");
5499 else if (*p
== '\\')
5500 ret
->append("\\\\");
5504 ret
->push_back('"');
5508 if (!this->fields_
->empty())
5509 ret
->push_back(' ');
5511 ret
->push_back('}');
5514 // Generate GC symbol for struct types.
5517 Struct_type::do_gc_symbol(Gogo
* gogo
, Expression_list
** vals
,
5518 Expression
** offset
, int stack_size
)
5520 Location bloc
= Linemap::predeclared_location();
5521 const Struct_field_list
* sfl
= this->fields();
5522 for (Struct_field_list::const_iterator p
= sfl
->begin();
5526 Expression
* field_offset
=
5527 Expression::make_struct_field_offset(this, &*p
);
5529 Expression::make_binary(OPERATOR_PLUS
, *offset
, field_offset
, bloc
);
5530 Type::gc_symbol(gogo
, p
->type(), vals
, &o
, stack_size
);
5532 this->advance_gc_offset(offset
);
5538 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5540 ret
->push_back('S');
5542 const Struct_field_list
* fields
= this->fields_
;
5545 for (Struct_field_list::const_iterator p
= fields
->begin();
5549 if (p
->is_anonymous())
5553 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
5555 snprintf(buf
, sizeof buf
, "%u_",
5556 static_cast<unsigned int>(n
.length()));
5560 this->append_mangled_name(p
->type(), gogo
, ret
);
5563 const std::string
& tag(p
->tag());
5565 for (std::string::const_iterator p
= tag
.begin();
5569 if (ISALNUM(*p
) || *p
== '_')
5574 snprintf(buf
, sizeof buf
, ".%x.",
5575 static_cast<unsigned int>(*p
));
5580 snprintf(buf
, sizeof buf
, "T%u_",
5581 static_cast<unsigned int>(out
.length()));
5588 ret
->push_back('e');
5591 // If the offset of field INDEX in the backend implementation can be
5592 // determined, set *POFFSET to the offset in bytes and return true.
5593 // Otherwise, return false.
5596 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
5599 if (!this->is_backend_type_size_known(gogo
))
5601 Btype
* bt
= this->get_backend_placeholder(gogo
);
5602 *poffset
= gogo
->backend()->type_field_offset(bt
, index
);
5609 Struct_type::do_export(Export
* exp
) const
5611 exp
->write_c_string("struct { ");
5612 const Struct_field_list
* fields
= this->fields_
;
5613 go_assert(fields
!= NULL
);
5614 for (Struct_field_list::const_iterator p
= fields
->begin();
5618 if (p
->is_anonymous())
5619 exp
->write_string("? ");
5622 exp
->write_string(p
->field_name());
5623 exp
->write_c_string(" ");
5625 exp
->write_type(p
->type());
5629 exp
->write_c_string(" ");
5631 Expression::make_string(p
->tag(), Linemap::predeclared_location());
5632 expr
->export_expression(exp
);
5636 exp
->write_c_string("; ");
5638 exp
->write_c_string("}");
5644 Struct_type::do_import(Import
* imp
)
5646 imp
->require_c_string("struct { ");
5647 Struct_field_list
* fields
= new Struct_field_list
;
5648 if (imp
->peek_char() != '}')
5653 if (imp
->match_c_string("? "))
5657 name
= imp
->read_identifier();
5658 imp
->require_c_string(" ");
5660 Type
* ftype
= imp
->read_type();
5662 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
5663 sf
.set_is_imported();
5665 if (imp
->peek_char() == ' ')
5668 Expression
* expr
= Expression::import_expression(imp
);
5669 String_expression
* sexpr
= expr
->string_expression();
5670 go_assert(sexpr
!= NULL
);
5671 sf
.set_tag(sexpr
->val());
5675 imp
->require_c_string("; ");
5676 fields
->push_back(sf
);
5677 if (imp
->peek_char() == '}')
5681 imp
->require_c_string("}");
5683 return Type::make_struct_type(fields
, imp
->location());
5686 // Make a struct type.
5689 Type::make_struct_type(Struct_field_list
* fields
,
5692 return new Struct_type(fields
, location
);
5695 // Class Array_type.
5697 // Whether two array types are identical.
5700 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
5702 if (!Type::are_identical(this->element_type(), t
->element_type(),
5703 errors_are_identical
, NULL
))
5706 Expression
* l1
= this->length();
5707 Expression
* l2
= t
->length();
5709 // Slices of the same element type are identical.
5710 if (l1
== NULL
&& l2
== NULL
)
5713 // Arrays of the same element type are identical if they have the
5715 if (l1
!= NULL
&& l2
!= NULL
)
5720 // Try to determine the lengths. If we can't, assume the arrays
5721 // are not identical.
5723 Numeric_constant nc1
, nc2
;
5724 if (l1
->numeric_constant_value(&nc1
)
5725 && l2
->numeric_constant_value(&nc2
))
5728 if (nc1
.to_int(&v1
))
5731 if (nc2
.to_int(&v2
))
5733 ret
= mpz_cmp(v1
, v2
) == 0;
5742 // Otherwise the arrays are not identical.
5749 Array_type::do_traverse(Traverse
* traverse
)
5751 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
5752 return TRAVERSE_EXIT
;
5753 if (this->length_
!= NULL
5754 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
5755 return TRAVERSE_EXIT
;
5756 return TRAVERSE_CONTINUE
;
5759 // Check that the length is valid.
5762 Array_type::verify_length()
5764 if (this->length_
== NULL
)
5767 Type_context
context(Type::lookup_integer_type("int"), false);
5768 this->length_
->determine_type(&context
);
5770 if (!this->length_
->is_constant())
5772 error_at(this->length_
->location(), "array bound is not constant");
5776 Numeric_constant nc
;
5777 if (!this->length_
->numeric_constant_value(&nc
))
5779 if (this->length_
->type()->integer_type() != NULL
5780 || this->length_
->type()->float_type() != NULL
)
5781 error_at(this->length_
->location(), "array bound is not constant");
5783 error_at(this->length_
->location(), "array bound is not numeric");
5787 Type
* int_type
= Type::lookup_integer_type("int");
5788 unsigned int tbits
= int_type
->integer_type()->bits();
5790 switch (nc
.to_unsigned_long(&val
))
5792 case Numeric_constant::NC_UL_VALID
:
5793 if (sizeof(val
) >= tbits
/ 8 && val
>> (tbits
- 1) != 0)
5795 error_at(this->length_
->location(), "array bound overflows");
5799 case Numeric_constant::NC_UL_NOTINT
:
5800 error_at(this->length_
->location(), "array bound truncated to integer");
5802 case Numeric_constant::NC_UL_NEGATIVE
:
5803 error_at(this->length_
->location(), "negative array bound");
5805 case Numeric_constant::NC_UL_BIG
:
5808 if (!nc
.to_int(&val
))
5810 unsigned int bits
= mpz_sizeinbase(val
, 2);
5814 error_at(this->length_
->location(), "array bound overflows");
5829 Array_type::do_verify()
5831 if (this->element_type()->is_error_type())
5833 if (!this->verify_length())
5834 this->length_
= Expression::make_error(this->length_
->location());
5838 // Whether we can use memcmp to compare this array.
5841 Array_type::do_compare_is_identity(Gogo
* gogo
)
5843 if (this->length_
== NULL
)
5846 // Check for [...], which indicates that this is not a real type.
5847 if (this->length_
->is_nil_expression())
5850 if (!this->element_type_
->compare_is_identity(gogo
))
5853 // If there is any padding, then we can't use memcmp.
5856 if (!this->element_type_
->backend_type_size(gogo
, &size
)
5857 || !this->element_type_
->backend_type_align(gogo
, &align
))
5859 if ((size
& (align
- 1)) != 0)
5865 // Array type hash code.
5868 Array_type::do_hash_for_method(Gogo
* gogo
) const
5870 // There is no very convenient way to get a hash code for the
5872 return this->element_type_
->hash_for_method(gogo
) + 1;
5875 // Write the hash function for an array which can not use the identify
5879 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
5880 Function_type
* hash_fntype
,
5881 Function_type
* equal_fntype
)
5883 Location bloc
= Linemap::predeclared_location();
5885 // The pointer to the array that we are going to hash. This is an
5886 // argument to the hash function we are implementing here.
5887 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
5888 go_assert(key_arg
!= NULL
);
5889 Type
* key_arg_type
= key_arg
->var_value()->type();
5891 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5894 Expression
* zero
= Expression::make_integer_ul(0, uintptr_type
, bloc
);
5896 // Make a temporary to hold the return value, initialized to 0.
5897 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
5899 gogo
->add_statement(retval
);
5901 // Make a temporary to hold the key as a uintptr.
5902 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
5903 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
5904 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
5906 gogo
->add_statement(key
);
5908 // Loop over the array elements.
5910 Type
* int_type
= Type::lookup_integer_type("int");
5911 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
5912 gogo
->add_statement(index
);
5914 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
5915 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
5916 Type
* pt
= Type::make_pointer_type(name
!= NULL
5917 ? static_cast<Type
*>(name
)
5918 : static_cast<Type
*>(this));
5919 aref
= Expression::make_cast(pt
, aref
, bloc
);
5920 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
5925 gogo
->start_block(bloc
);
5927 // Multiply retval by 33.
5928 Expression
* i33
= Expression::make_integer_ul(33, uintptr_type
, bloc
);
5930 ref
= Expression::make_temporary_reference(retval
, bloc
);
5931 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
, ref
,
5933 gogo
->add_statement(s
);
5935 // Get the hash function for the element type.
5936 Named_object
* hash_fn
;
5937 Named_object
* equal_fn
;
5938 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
5939 hash_fntype
, equal_fntype
, &hash_fn
,
5942 // Get a pointer to this element in the loop.
5943 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
5944 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
5946 // Get the size of each element.
5947 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
5948 Expression::TYPE_INFO_SIZE
);
5950 // Get the hash of this element.
5951 Expression_list
* args
= new Expression_list();
5952 args
->push_back(subkey
);
5953 args
->push_back(ele_size
);
5954 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
5955 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
5957 // Add the element's hash value to retval.
5958 Temporary_reference_expression
* tref
=
5959 Expression::make_temporary_reference(retval
, bloc
);
5960 tref
->set_is_lvalue();
5961 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, call
, bloc
);
5962 gogo
->add_statement(s
);
5964 // Increase the element pointer.
5965 tref
= Expression::make_temporary_reference(key
, bloc
);
5966 tref
->set_is_lvalue();
5967 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
5969 Block
* statements
= gogo
->finish_block(bloc
);
5971 for_range
->add_statements(statements
);
5972 gogo
->add_statement(for_range
);
5974 // Return retval to the caller of the hash function.
5975 Expression_list
* vals
= new Expression_list();
5976 ref
= Expression::make_temporary_reference(retval
, bloc
);
5977 vals
->push_back(ref
);
5978 s
= Statement::make_return_statement(vals
, bloc
);
5979 gogo
->add_statement(s
);
5982 // Write the equality function for an array which can not use the
5983 // identity function.
5986 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
5988 Location bloc
= Linemap::predeclared_location();
5990 // The pointers to the arrays we are going to compare.
5991 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
5992 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
5993 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
5995 // Build temporaries for the keys with the right types.
5996 Type
* pt
= Type::make_pointer_type(name
!= NULL
5997 ? static_cast<Type
*>(name
)
5998 : static_cast<Type
*>(this));
6000 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
6001 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6002 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
6003 gogo
->add_statement(p1
);
6005 ref
= Expression::make_var_reference(key2_arg
, bloc
);
6006 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
6007 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
6008 gogo
->add_statement(p2
);
6010 // Loop over the array elements.
6012 Type
* int_type
= Type::lookup_integer_type("int");
6013 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
6014 gogo
->add_statement(index
);
6016 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
6017 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
6018 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
6023 gogo
->start_block(bloc
);
6025 // Compare element in P1 and P2.
6026 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
6027 e1
= Expression::make_unary(OPERATOR_MULT
, e1
, bloc
);
6028 ref
= Expression::make_temporary_reference(index
, bloc
);
6029 e1
= Expression::make_array_index(e1
, ref
, NULL
, NULL
, bloc
);
6031 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
6032 e2
= Expression::make_unary(OPERATOR_MULT
, e2
, bloc
);
6033 ref
= Expression::make_temporary_reference(index
, bloc
);
6034 e2
= Expression::make_array_index(e2
, ref
, NULL
, NULL
, bloc
);
6036 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
6038 // If the elements are not equal, return false.
6039 gogo
->start_block(bloc
);
6040 Expression_list
* vals
= new Expression_list();
6041 vals
->push_back(Expression::make_boolean(false, bloc
));
6042 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
6043 gogo
->add_statement(s
);
6044 Block
* then_block
= gogo
->finish_block(bloc
);
6046 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
6047 gogo
->add_statement(s
);
6049 Block
* statements
= gogo
->finish_block(bloc
);
6051 for_range
->add_statements(statements
);
6052 gogo
->add_statement(for_range
);
6054 // All the elements are equal, so return true.
6055 vals
= new Expression_list();
6056 vals
->push_back(Expression::make_boolean(true, bloc
));
6057 s
= Statement::make_return_statement(vals
, bloc
);
6058 gogo
->add_statement(s
);
6061 // Get the backend representation of the fields of a slice. This is
6062 // not declared in types.h so that types.h doesn't have to #include
6065 // We use int for the count and capacity fields. This matches 6g.
6066 // The language more or less assumes that we can't allocate space of a
6067 // size which does not fit in int.
6070 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
6071 std::vector
<Backend::Btyped_identifier
>* bfields
)
6075 Type
* pet
= Type::make_pointer_type(type
->element_type());
6076 Btype
* pbet
= (use_placeholder
6077 ? pet
->get_backend_placeholder(gogo
)
6078 : pet
->get_backend(gogo
));
6079 Location ploc
= Linemap::predeclared_location();
6081 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
6082 p
->name
= "__values";
6086 Type
* int_type
= Type::lookup_integer_type("int");
6089 p
->name
= "__count";
6090 p
->btype
= int_type
->get_backend(gogo
);
6094 p
->name
= "__capacity";
6095 p
->btype
= int_type
->get_backend(gogo
);
6099 // Get the backend representation for the type of this array. A fixed array is
6100 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
6101 // just like an array in C. An open array is a struct with three
6102 // fields: a data pointer, the length, and the capacity.
6105 Array_type::do_get_backend(Gogo
* gogo
)
6107 if (this->length_
== NULL
)
6109 std::vector
<Backend::Btyped_identifier
> bfields
;
6110 get_backend_slice_fields(gogo
, this, false, &bfields
);
6111 return gogo
->backend()->struct_type(bfields
);
6115 Btype
* element
= this->get_backend_element(gogo
, false);
6116 Bexpression
* len
= this->get_backend_length(gogo
);
6117 return gogo
->backend()->array_type(element
, len
);
6121 // Return the backend representation of the element type.
6124 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
6126 if (use_placeholder
)
6127 return this->element_type_
->get_backend_placeholder(gogo
);
6129 return this->element_type_
->get_backend(gogo
);
6132 // Return the backend representation of the length. The length may be
6133 // computed using a function call, so we must only evaluate it once.
6136 Array_type::get_backend_length(Gogo
* gogo
)
6138 go_assert(this->length_
!= NULL
);
6139 if (this->blength_
== NULL
)
6141 Numeric_constant nc
;
6143 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
6145 if (mpz_sgn(val
) < 0)
6147 this->blength_
= gogo
->backend()->error_expression();
6148 return this->blength_
;
6150 Type
* t
= nc
.type();
6152 t
= Type::lookup_integer_type("int");
6153 else if (t
->is_abstract())
6154 t
= t
->make_non_abstract_type();
6155 Btype
* btype
= t
->get_backend(gogo
);
6157 gogo
->backend()->integer_constant_expression(btype
, val
);
6162 // Make up a translation context for the array length
6163 // expression. FIXME: This won't work in general.
6164 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
6165 this->blength_
= this->length_
->get_backend(&context
);
6167 Btype
* ibtype
= Type::lookup_integer_type("int")->get_backend(gogo
);
6169 gogo
->backend()->convert_expression(ibtype
, this->blength_
,
6170 this->length_
->location());
6173 return this->blength_
;
6176 // Finish backend representation of the array.
6179 Array_type::finish_backend_element(Gogo
* gogo
)
6181 Type
* et
= this->array_type()->element_type();
6182 et
->get_backend(gogo
);
6183 if (this->is_slice_type())
6185 // This relies on the fact that we always use the same
6186 // structure for a pointer to any given type.
6187 Type
* pet
= Type::make_pointer_type(et
);
6188 pet
->get_backend(gogo
);
6192 // Return an expression for a pointer to the values in ARRAY.
6195 Array_type::get_value_pointer(Gogo
*, Expression
* array
) const
6197 if (this->length() != NULL
)
6200 go_assert(array
->type()->array_type() != NULL
);
6201 Type
* etype
= array
->type()->array_type()->element_type();
6202 array
= Expression::make_unary(OPERATOR_AND
, array
, array
->location());
6203 return Expression::make_cast(Type::make_pointer_type(etype
), array
,
6208 return Expression::make_slice_info(array
,
6209 Expression::SLICE_INFO_VALUE_POINTER
,
6213 // Return an expression for the length of the array ARRAY which has this
6217 Array_type::get_length(Gogo
*, Expression
* array
) const
6219 if (this->length_
!= NULL
)
6220 return this->length_
;
6222 // This is a slice. We need to read the length field.
6223 return Expression::make_slice_info(array
, Expression::SLICE_INFO_LENGTH
,
6227 // Return an expression for the capacity of the array ARRAY which has this
6231 Array_type::get_capacity(Gogo
*, Expression
* array
) const
6233 if (this->length_
!= NULL
)
6234 return this->length_
;
6236 // This is a slice. We need to read the capacity field.
6237 return Expression::make_slice_info(array
, Expression::SLICE_INFO_CAPACITY
,
6244 Array_type::do_export(Export
* exp
) const
6246 exp
->write_c_string("[");
6247 if (this->length_
!= NULL
)
6248 this->length_
->export_expression(exp
);
6249 exp
->write_c_string("] ");
6250 exp
->write_type(this->element_type_
);
6256 Array_type::do_import(Import
* imp
)
6258 imp
->require_c_string("[");
6260 if (imp
->peek_char() == ']')
6263 length
= Expression::import_expression(imp
);
6264 imp
->require_c_string("] ");
6265 Type
* element_type
= imp
->read_type();
6266 return Type::make_array_type(element_type
, length
);
6269 // The type of an array type descriptor.
6272 Array_type::make_array_type_descriptor_type()
6277 Type
* tdt
= Type::make_type_descriptor_type();
6278 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6280 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6283 Type::make_builtin_struct_type(4,
6287 "len", uintptr_type
);
6289 ret
= Type::make_builtin_named_type("ArrayType", sf
);
6295 // The type of an slice type descriptor.
6298 Array_type::make_slice_type_descriptor_type()
6303 Type
* tdt
= Type::make_type_descriptor_type();
6304 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6307 Type::make_builtin_struct_type(2,
6311 ret
= Type::make_builtin_named_type("SliceType", sf
);
6317 // Build a type descriptor for an array/slice type.
6320 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6322 if (this->length_
!= NULL
)
6323 return this->array_type_descriptor(gogo
, name
);
6325 return this->slice_type_descriptor(gogo
, name
);
6328 // Build a type descriptor for an array type.
6331 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6333 Location bloc
= Linemap::predeclared_location();
6335 Type
* atdt
= Array_type::make_array_type_descriptor_type();
6337 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
6339 Expression_list
* vals
= new Expression_list();
6342 Struct_field_list::const_iterator p
= fields
->begin();
6343 go_assert(p
->is_field_name("commonType"));
6344 vals
->push_back(this->type_descriptor_constructor(gogo
,
6345 RUNTIME_TYPE_KIND_ARRAY
,
6349 go_assert(p
->is_field_name("elem"));
6350 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
6353 go_assert(p
->is_field_name("slice"));
6354 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
6355 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
6358 go_assert(p
->is_field_name("len"));
6359 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
6362 go_assert(p
== fields
->end());
6364 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
6367 // Build a type descriptor for a slice type.
6370 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6372 Location bloc
= Linemap::predeclared_location();
6374 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
6376 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
6378 Expression_list
* vals
= new Expression_list();
6381 Struct_field_list::const_iterator p
= fields
->begin();
6382 go_assert(p
->is_field_name("commonType"));
6383 vals
->push_back(this->type_descriptor_constructor(gogo
,
6384 RUNTIME_TYPE_KIND_SLICE
,
6388 go_assert(p
->is_field_name("elem"));
6389 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
6392 go_assert(p
== fields
->end());
6394 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
6397 // Reflection string.
6400 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6402 ret
->push_back('[');
6403 if (this->length_
!= NULL
)
6405 Numeric_constant nc
;
6406 if (!this->length_
->numeric_constant_value(&nc
))
6408 go_assert(saw_errors());
6412 if (!nc
.to_int(&val
))
6414 go_assert(saw_errors());
6417 char* s
= mpz_get_str(NULL
, 10, val
);
6422 ret
->push_back(']');
6424 this->append_reflection(this->element_type_
, gogo
, ret
);
6427 // GC Symbol construction for array types.
6430 Array_type::do_gc_symbol(Gogo
* gogo
, Expression_list
** vals
,
6431 Expression
** offset
, int stack_size
)
6433 if (this->length_
== NULL
)
6434 this->slice_gc_symbol(gogo
, vals
, offset
, stack_size
);
6436 this->array_gc_symbol(gogo
, vals
, offset
, stack_size
);
6439 // Generate the GC Symbol for a slice.
6442 Array_type::slice_gc_symbol(Gogo
* gogo
, Expression_list
** vals
,
6443 Expression
** offset
, int)
6445 Location bloc
= Linemap::predeclared_location();
6447 // Differentiate between slices with zero-length and non-zero-length values.
6448 Type
* element_type
= this->element_type();
6449 int64_t element_size
;
6450 bool ok
= element_type
->backend_type_size(gogo
, &element_size
);
6452 go_assert(saw_errors());
6456 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6457 unsigned long opval
= element_size
== 0 ? GC_APTR
: GC_SLICE
;
6458 (*vals
)->push_back(Expression::make_integer_ul(opval
, uintptr_type
, bloc
));
6459 (*vals
)->push_back(*offset
);
6461 if (element_size
!= 0 && ok
)
6462 (*vals
)->push_back(Expression::make_gc_symbol(element_type
));
6463 this->advance_gc_offset(offset
);
6466 // Generate the GC symbol for an array.
6469 Array_type::array_gc_symbol(Gogo
* gogo
, Expression_list
** vals
,
6470 Expression
** offset
, int stack_size
)
6472 Location bloc
= Linemap::predeclared_location();
6474 Numeric_constant nc
;
6475 unsigned long bound
;
6476 if (!this->length_
->numeric_constant_value(&nc
)
6477 || nc
.to_unsigned_long(&bound
) == Numeric_constant::NC_UL_NOTINT
)
6479 go_assert(saw_errors());
6483 Btype
* pbtype
= gogo
->backend()->pointer_type(gogo
->backend()->void_type());
6484 int64_t pwidth
= gogo
->backend()->type_size(pbtype
);
6486 bool ok
= this->backend_type_size(gogo
, &iwidth
);
6489 go_assert(saw_errors());
6493 Type
* element_type
= this->element_type();
6494 if (bound
< 1 || !element_type
->has_pointer())
6495 this->advance_gc_offset(offset
);
6496 else if (ok
&& (bound
== 1 || iwidth
<= 4 * pwidth
))
6498 for (unsigned int i
= 0; i
< bound
; ++i
)
6499 Type::gc_symbol(gogo
, element_type
, vals
, offset
, stack_size
);
6503 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6505 if (stack_size
< GC_STACK_CAPACITY
)
6507 (*vals
)->push_back(Expression::make_integer_ul(GC_ARRAY_START
,
6508 uintptr_type
, bloc
));
6509 (*vals
)->push_back(*offset
);
6510 Expression
* uintptr_len
=
6511 Expression::make_cast(uintptr_type
, this->length_
, bloc
);
6512 (*vals
)->push_back(uintptr_len
);
6515 Expression::make_type_info(element_type
,
6516 Expression::TYPE_INFO_SIZE
);
6517 (*vals
)->push_back(width
);
6519 Expression
* offset2
= Expression::make_integer_ul(0, uintptr_type
,
6522 Type::gc_symbol(gogo
, element_type
, vals
, &offset2
, stack_size
+ 1);
6523 (*vals
)->push_back(Expression::make_integer_ul(GC_ARRAY_NEXT
,
6524 uintptr_type
, bloc
));
6528 (*vals
)->push_back(Expression::make_integer_ul(GC_REGION
,
6529 uintptr_type
, bloc
));
6530 (*vals
)->push_back(*offset
);
6533 Expression::make_type_info(this, Expression::TYPE_INFO_SIZE
);
6534 (*vals
)->push_back(width
);
6535 (*vals
)->push_back(Expression::make_gc_symbol(this));
6537 this->advance_gc_offset(offset
);
6544 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6546 ret
->push_back('A');
6547 this->append_mangled_name(this->element_type_
, gogo
, ret
);
6548 if (this->length_
!= NULL
)
6550 Numeric_constant nc
;
6551 if (!this->length_
->numeric_constant_value(&nc
))
6553 go_assert(saw_errors());
6557 if (!nc
.to_int(&val
))
6559 go_assert(saw_errors());
6562 char *s
= mpz_get_str(NULL
, 10, val
);
6567 ret
->push_back('e');
6570 // Make an array type.
6573 Type::make_array_type(Type
* element_type
, Expression
* length
)
6575 return new Array_type(element_type
, length
);
6583 Map_type::do_traverse(Traverse
* traverse
)
6585 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
6586 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
6587 return TRAVERSE_EXIT
;
6588 return TRAVERSE_CONTINUE
;
6591 // Check that the map type is OK.
6594 Map_type::do_verify()
6596 // The runtime support uses "map[void]void".
6597 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
6598 error_at(this->location_
, "invalid map key type");
6602 // Whether two map types are identical.
6605 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
6607 return (Type::are_identical(this->key_type(), t
->key_type(),
6608 errors_are_identical
, NULL
)
6609 && Type::are_identical(this->val_type(), t
->val_type(),
6610 errors_are_identical
, NULL
));
6616 Map_type::do_hash_for_method(Gogo
* gogo
) const
6618 return (this->key_type_
->hash_for_method(gogo
)
6619 + this->val_type_
->hash_for_method(gogo
)
6623 // Get the backend representation for a map type. A map type is
6624 // represented as a pointer to a struct. The struct is __go_map in
6628 Map_type::do_get_backend(Gogo
* gogo
)
6630 static Btype
* backend_map_type
;
6631 if (backend_map_type
== NULL
)
6633 std::vector
<Backend::Btyped_identifier
> bfields(4);
6635 Location bloc
= Linemap::predeclared_location();
6637 Type
* pdt
= Type::make_type_descriptor_ptr_type();
6638 bfields
[0].name
= "__descriptor";
6639 bfields
[0].btype
= pdt
->get_backend(gogo
);
6640 bfields
[0].location
= bloc
;
6642 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6643 bfields
[1].name
= "__element_count";
6644 bfields
[1].btype
= uintptr_type
->get_backend(gogo
);
6645 bfields
[1].location
= bloc
;
6647 bfields
[2].name
= "__bucket_count";
6648 bfields
[2].btype
= bfields
[1].btype
;
6649 bfields
[2].location
= bloc
;
6651 Btype
* bvt
= gogo
->backend()->void_type();
6652 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
6653 Btype
* bppvt
= gogo
->backend()->pointer_type(bpvt
);
6654 bfields
[3].name
= "__buckets";
6655 bfields
[3].btype
= bppvt
;
6656 bfields
[3].location
= bloc
;
6658 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
6659 bt
= gogo
->backend()->named_type("__go_map", bt
, bloc
);
6660 backend_map_type
= gogo
->backend()->pointer_type(bt
);
6662 return backend_map_type
;
6665 // The type of a map type descriptor.
6668 Map_type::make_map_type_descriptor_type()
6673 Type
* tdt
= Type::make_type_descriptor_type();
6674 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6677 Type::make_builtin_struct_type(3,
6682 ret
= Type::make_builtin_named_type("MapType", sf
);
6688 // Build a type descriptor for a map type.
6691 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6693 Location bloc
= Linemap::predeclared_location();
6695 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
6697 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
6699 Expression_list
* vals
= new Expression_list();
6702 Struct_field_list::const_iterator p
= fields
->begin();
6703 go_assert(p
->is_field_name("commonType"));
6704 vals
->push_back(this->type_descriptor_constructor(gogo
,
6705 RUNTIME_TYPE_KIND_MAP
,
6709 go_assert(p
->is_field_name("key"));
6710 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
6713 go_assert(p
->is_field_name("elem"));
6714 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
6717 go_assert(p
== fields
->end());
6719 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
6722 // A mapping from map types to map descriptors.
6724 Map_type::Map_descriptors
Map_type::map_descriptors
;
6726 // Build a map descriptor for this type. Return a pointer to it.
6729 Map_type::map_descriptor_pointer(Gogo
* gogo
, Location location
)
6731 Bvariable
* bvar
= this->map_descriptor(gogo
);
6732 Bexpression
* var_expr
= gogo
->backend()->var_expression(bvar
, location
);
6733 return gogo
->backend()->address_expression(var_expr
, location
);
6736 // Build a map descriptor for this type.
6739 Map_type::map_descriptor(Gogo
* gogo
)
6741 std::pair
<Map_type
*, Bvariable
*> val(this, NULL
);
6742 std::pair
<Map_type::Map_descriptors::iterator
, bool> ins
=
6743 Map_type::map_descriptors
.insert(val
);
6745 return ins
.first
->second
;
6747 Type
* key_type
= this->key_type_
;
6748 Type
* val_type
= this->val_type_
;
6750 // The map entry type is a struct with three fields. Build that
6751 // struct so that we can get the offsets of the key and value within
6752 // a map entry. The first field should technically be a pointer to
6753 // this type itself, but since we only care about field offsets we
6754 // just use pointer to bool.
6755 Type
* pbool
= Type::make_pointer_type(Type::make_boolean_type());
6756 Struct_type
* map_entry_type
=
6757 Type::make_builtin_struct_type(3,
6762 Type
* map_descriptor_type
= Map_type::make_map_descriptor_type();
6764 const Struct_field_list
* fields
=
6765 map_descriptor_type
->struct_type()->fields();
6767 Expression_list
* vals
= new Expression_list();
6770 Location bloc
= Linemap::predeclared_location();
6772 Struct_field_list::const_iterator p
= fields
->begin();
6774 go_assert(p
->is_field_name("__map_descriptor"));
6775 vals
->push_back(Expression::make_type_descriptor(this, bloc
));
6778 go_assert(p
->is_field_name("__entry_size"));
6779 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
6780 vals
->push_back(Expression::make_type_info(map_entry_type
, type_info
));
6782 Struct_field_list::const_iterator pf
= map_entry_type
->fields()->begin();
6784 go_assert(pf
->is_field_name("__key"));
6787 go_assert(p
->is_field_name("__key_offset"));
6788 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6791 go_assert(pf
->is_field_name("__val"));
6794 go_assert(p
->is_field_name("__val_offset"));
6795 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6798 go_assert(p
== fields
->end());
6800 Expression
* initializer
=
6801 Expression::make_struct_composite_literal(map_descriptor_type
, vals
, bloc
);
6803 std::string mangled_name
= "__go_map_" + this->mangled_name(gogo
);
6804 Btype
* map_descriptor_btype
= map_descriptor_type
->get_backend(gogo
);
6805 Bvariable
* bvar
= gogo
->backend()->immutable_struct(mangled_name
, false,
6807 map_descriptor_btype
,
6810 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
6811 context
.set_is_const();
6812 Bexpression
* binitializer
= initializer
->get_backend(&context
);
6814 gogo
->backend()->immutable_struct_set_init(bvar
, mangled_name
, false, true,
6815 map_descriptor_btype
, bloc
,
6818 ins
.first
->second
= bvar
;
6822 // Build the type of a map descriptor. This must match the struct
6823 // __go_map_descriptor in libgo/runtime/map.h.
6826 Map_type::make_map_descriptor_type()
6831 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6832 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6834 Type::make_builtin_struct_type(4,
6835 "__map_descriptor", ptdt
,
6836 "__entry_size", uintptr_type
,
6837 "__key_offset", uintptr_type
,
6838 "__val_offset", uintptr_type
);
6839 ret
= Type::make_builtin_named_type("__go_map_descriptor", sf
);
6844 // Reflection string for a map.
6847 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6849 ret
->append("map[");
6850 this->append_reflection(this->key_type_
, gogo
, ret
);
6852 this->append_reflection(this->val_type_
, gogo
, ret
);
6855 // Generate GC symbol for a map.
6858 Map_type::do_gc_symbol(Gogo
*, Expression_list
** vals
,
6859 Expression
** offset
, int)
6861 // TODO(cmang): Generate GC data for the Map elements.
6862 Location bloc
= Linemap::predeclared_location();
6863 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6865 (*vals
)->push_back(Expression::make_integer_ul(GC_APTR
, uintptr_type
, bloc
));
6866 (*vals
)->push_back(*offset
);
6867 this->advance_gc_offset(offset
);
6870 // Mangled name for a map.
6873 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6875 ret
->push_back('M');
6876 this->append_mangled_name(this->key_type_
, gogo
, ret
);
6878 this->append_mangled_name(this->val_type_
, gogo
, ret
);
6881 // Export a map type.
6884 Map_type::do_export(Export
* exp
) const
6886 exp
->write_c_string("map [");
6887 exp
->write_type(this->key_type_
);
6888 exp
->write_c_string("] ");
6889 exp
->write_type(this->val_type_
);
6892 // Import a map type.
6895 Map_type::do_import(Import
* imp
)
6897 imp
->require_c_string("map [");
6898 Type
* key_type
= imp
->read_type();
6899 imp
->require_c_string("] ");
6900 Type
* val_type
= imp
->read_type();
6901 return Type::make_map_type(key_type
, val_type
, imp
->location());
6907 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
6909 return new Map_type(key_type
, val_type
, location
);
6912 // Class Channel_type.
6917 Channel_type::do_hash_for_method(Gogo
* gogo
) const
6919 unsigned int ret
= 0;
6920 if (this->may_send_
)
6922 if (this->may_receive_
)
6924 if (this->element_type_
!= NULL
)
6925 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
6929 // Whether this type is the same as T.
6932 Channel_type::is_identical(const Channel_type
* t
,
6933 bool errors_are_identical
) const
6935 if (!Type::are_identical(this->element_type(), t
->element_type(),
6936 errors_are_identical
, NULL
))
6938 return (this->may_send_
== t
->may_send_
6939 && this->may_receive_
== t
->may_receive_
);
6942 // Return the backend representation for a channel type. A channel is a pointer
6943 // to a __go_channel struct. The __go_channel struct is defined in
6944 // libgo/runtime/channel.h.
6947 Channel_type::do_get_backend(Gogo
* gogo
)
6949 static Btype
* backend_channel_type
;
6950 if (backend_channel_type
== NULL
)
6952 std::vector
<Backend::Btyped_identifier
> bfields
;
6953 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
6954 bt
= gogo
->backend()->named_type("__go_channel", bt
,
6955 Linemap::predeclared_location());
6956 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
6958 return backend_channel_type
;
6961 // Build a type descriptor for a channel type.
6964 Channel_type::make_chan_type_descriptor_type()
6969 Type
* tdt
= Type::make_type_descriptor_type();
6970 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6972 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6975 Type::make_builtin_struct_type(3,
6978 "dir", uintptr_type
);
6980 ret
= Type::make_builtin_named_type("ChanType", sf
);
6986 // Build a type descriptor for a map type.
6989 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6991 Location bloc
= Linemap::predeclared_location();
6993 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
6995 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
6997 Expression_list
* vals
= new Expression_list();
7000 Struct_field_list::const_iterator p
= fields
->begin();
7001 go_assert(p
->is_field_name("commonType"));
7002 vals
->push_back(this->type_descriptor_constructor(gogo
,
7003 RUNTIME_TYPE_KIND_CHAN
,
7007 go_assert(p
->is_field_name("elem"));
7008 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
7011 go_assert(p
->is_field_name("dir"));
7012 // These bits must match the ones in libgo/runtime/go-type.h.
7014 if (this->may_receive_
)
7016 if (this->may_send_
)
7018 vals
->push_back(Expression::make_integer_ul(val
, p
->type(), bloc
));
7021 go_assert(p
== fields
->end());
7023 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
7026 // Reflection string.
7029 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7031 if (!this->may_send_
)
7033 ret
->append("chan");
7034 if (!this->may_receive_
)
7036 ret
->push_back(' ');
7037 this->append_reflection(this->element_type_
, gogo
, ret
);
7040 // Generate GC symbol for channels.
7043 Channel_type::do_gc_symbol(Gogo
*, Expression_list
** vals
,
7044 Expression
** offset
, int)
7046 Location bloc
= Linemap::predeclared_location();
7047 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7049 (*vals
)->push_back(Expression::make_integer_ul(GC_CHAN_PTR
, uintptr_type
,
7051 (*vals
)->push_back(*offset
);
7053 Type
* unsafeptr_type
= Type::make_pointer_type(Type::make_void_type());
7054 Expression
* type_descriptor
=
7055 Expression::make_type_descriptor(this, bloc
);
7057 Expression::make_unsafe_cast(unsafeptr_type
, type_descriptor
, bloc
);
7058 (*vals
)->push_back(type_descriptor
);
7059 this->advance_gc_offset(offset
);
7065 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7067 ret
->push_back('C');
7068 this->append_mangled_name(this->element_type_
, gogo
, ret
);
7069 if (this->may_send_
)
7070 ret
->push_back('s');
7071 if (this->may_receive_
)
7072 ret
->push_back('r');
7073 ret
->push_back('e');
7079 Channel_type::do_export(Export
* exp
) const
7081 exp
->write_c_string("chan ");
7082 if (this->may_send_
&& !this->may_receive_
)
7083 exp
->write_c_string("-< ");
7084 else if (this->may_receive_
&& !this->may_send_
)
7085 exp
->write_c_string("<- ");
7086 exp
->write_type(this->element_type_
);
7092 Channel_type::do_import(Import
* imp
)
7094 imp
->require_c_string("chan ");
7098 if (imp
->match_c_string("-< "))
7102 may_receive
= false;
7104 else if (imp
->match_c_string("<- "))
7116 Type
* element_type
= imp
->read_type();
7118 return Type::make_channel_type(may_send
, may_receive
, element_type
);
7121 // Make a new channel type.
7124 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
7126 return new Channel_type(send
, receive
, element_type
);
7129 // Class Interface_type.
7131 // Return the list of methods.
7133 const Typed_identifier_list
*
7134 Interface_type::methods() const
7136 go_assert(this->methods_are_finalized_
|| saw_errors());
7137 return this->all_methods_
;
7140 // Return the number of methods.
7143 Interface_type::method_count() const
7145 go_assert(this->methods_are_finalized_
|| saw_errors());
7146 return this->all_methods_
== NULL
? 0 : this->all_methods_
->size();
7152 Interface_type::do_traverse(Traverse
* traverse
)
7154 Typed_identifier_list
* methods
= (this->methods_are_finalized_
7155 ? this->all_methods_
7156 : this->parse_methods_
);
7157 if (methods
== NULL
)
7158 return TRAVERSE_CONTINUE
;
7159 return methods
->traverse(traverse
);
7162 // Finalize the methods. This handles interface inheritance.
7165 Interface_type::finalize_methods()
7167 if (this->methods_are_finalized_
)
7169 this->methods_are_finalized_
= true;
7170 if (this->parse_methods_
== NULL
)
7173 this->all_methods_
= new Typed_identifier_list();
7174 this->all_methods_
->reserve(this->parse_methods_
->size());
7175 Typed_identifier_list inherit
;
7176 for (Typed_identifier_list::const_iterator pm
=
7177 this->parse_methods_
->begin();
7178 pm
!= this->parse_methods_
->end();
7181 const Typed_identifier
* p
= &*pm
;
7182 if (p
->name().empty())
7183 inherit
.push_back(*p
);
7184 else if (this->find_method(p
->name()) == NULL
)
7185 this->all_methods_
->push_back(*p
);
7187 error_at(p
->location(), "duplicate method %qs",
7188 Gogo::message_name(p
->name()).c_str());
7191 std::vector
<Named_type
*> seen
;
7192 seen
.reserve(inherit
.size());
7193 bool issued_recursive_error
= false;
7194 while (!inherit
.empty())
7196 Type
* t
= inherit
.back().type();
7197 Location tl
= inherit
.back().location();
7200 Interface_type
* it
= t
->interface_type();
7204 error_at(tl
, "interface contains embedded non-interface");
7209 if (!issued_recursive_error
)
7211 error_at(tl
, "invalid recursive interface");
7212 issued_recursive_error
= true;
7217 Named_type
* nt
= t
->named_type();
7218 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
7220 std::vector
<Named_type
*>::const_iterator q
;
7221 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
7225 error_at(tl
, "inherited interface loop");
7229 if (q
!= seen
.end())
7234 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
7235 if (imethods
== NULL
)
7237 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
7238 q
!= imethods
->end();
7241 if (q
->name().empty())
7242 inherit
.push_back(*q
);
7243 else if (this->find_method(q
->name()) == NULL
)
7244 this->all_methods_
->push_back(Typed_identifier(q
->name(),
7247 error_at(tl
, "inherited method %qs is ambiguous",
7248 Gogo::message_name(q
->name()).c_str());
7252 if (!this->all_methods_
->empty())
7253 this->all_methods_
->sort_by_name();
7256 delete this->all_methods_
;
7257 this->all_methods_
= NULL
;
7261 // Return the method NAME, or NULL.
7263 const Typed_identifier
*
7264 Interface_type::find_method(const std::string
& name
) const
7266 go_assert(this->methods_are_finalized_
);
7267 if (this->all_methods_
== NULL
)
7269 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7270 p
!= this->all_methods_
->end();
7272 if (p
->name() == name
)
7277 // Return the method index.
7280 Interface_type::method_index(const std::string
& name
) const
7282 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
7284 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7285 p
!= this->all_methods_
->end();
7287 if (p
->name() == name
)
7292 // Return whether NAME is an unexported method, for better error
7296 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
7298 go_assert(this->methods_are_finalized_
);
7299 if (this->all_methods_
== NULL
)
7301 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7302 p
!= this->all_methods_
->end();
7305 const std::string
& method_name(p
->name());
7306 if (Gogo::is_hidden_name(method_name
)
7307 && name
== Gogo::unpack_hidden_name(method_name
)
7308 && gogo
->pack_hidden_name(name
, false) != method_name
)
7314 // Whether this type is identical with T.
7317 Interface_type::is_identical(const Interface_type
* t
,
7318 bool errors_are_identical
) const
7320 // If methods have not been finalized, then we are asking whether
7321 // func redeclarations are the same. This is an error, so for
7322 // simplicity we say they are never the same.
7323 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
7326 // We require the same methods with the same types. The methods
7327 // have already been sorted.
7328 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
7329 return this->all_methods_
== t
->all_methods_
;
7331 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
7334 Assume_identical
* hold_ai
= this->assume_identical_
;
7335 Assume_identical ai
;
7339 this->assume_identical_
= &ai
;
7341 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
7342 Typed_identifier_list::const_iterator p2
;
7343 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
7345 if (p1
== this->all_methods_
->end())
7347 if (p1
->name() != p2
->name()
7348 || !Type::are_identical(p1
->type(), p2
->type(),
7349 errors_are_identical
, NULL
))
7353 this->assume_identical_
= hold_ai
;
7355 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
7358 // Return true if T1 and T2 are assumed to be identical during a type
7362 Interface_type::assume_identical(const Interface_type
* t1
,
7363 const Interface_type
* t2
) const
7365 for (Assume_identical
* p
= this->assume_identical_
;
7368 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
7373 // Whether we can assign the interface type T to this type. The types
7374 // are known to not be identical. An interface assignment is only
7375 // permitted if T is known to implement all methods in THIS.
7376 // Otherwise a type guard is required.
7379 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
7380 std::string
* reason
) const
7382 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
7383 if (this->all_methods_
== NULL
)
7385 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7386 p
!= this->all_methods_
->end();
7389 const Typed_identifier
* m
= t
->find_method(p
->name());
7395 snprintf(buf
, sizeof buf
,
7396 _("need explicit conversion; missing method %s%s%s"),
7397 open_quote
, Gogo::message_name(p
->name()).c_str(),
7399 reason
->assign(buf
);
7404 std::string subreason
;
7405 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
7409 std::string n
= Gogo::message_name(p
->name());
7410 size_t len
= 100 + n
.length() + subreason
.length();
7411 char* buf
= new char[len
];
7412 if (subreason
.empty())
7413 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
7414 open_quote
, n
.c_str(), close_quote
);
7417 _("incompatible type for method %s%s%s (%s)"),
7418 open_quote
, n
.c_str(), close_quote
,
7420 reason
->assign(buf
);
7433 Interface_type::do_hash_for_method(Gogo
*) const
7435 go_assert(this->methods_are_finalized_
);
7436 unsigned int ret
= 0;
7437 if (this->all_methods_
!= NULL
)
7439 for (Typed_identifier_list::const_iterator p
=
7440 this->all_methods_
->begin();
7441 p
!= this->all_methods_
->end();
7444 ret
= Type::hash_string(p
->name(), ret
);
7445 // We don't use the method type in the hash, to avoid
7446 // infinite recursion if an interface method uses a type
7447 // which is an interface which inherits from the interface
7449 // type T interface { F() interface {T}}
7456 // Return true if T implements the interface. If it does not, and
7457 // REASON is not NULL, set *REASON to a useful error message.
7460 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
7462 go_assert(this->methods_are_finalized_
);
7463 if (this->all_methods_
== NULL
)
7466 bool is_pointer
= false;
7467 const Named_type
* nt
= t
->named_type();
7468 const Struct_type
* st
= t
->struct_type();
7469 // If we start with a named type, we don't dereference it to find
7473 const Type
* pt
= t
->points_to();
7476 // If T is a pointer to a named type, then we need to look at
7477 // the type to which it points.
7479 nt
= pt
->named_type();
7480 st
= pt
->struct_type();
7484 // If we have a named type, get the methods from it rather than from
7489 // Only named and struct types have methods.
7490 if (nt
== NULL
&& st
== NULL
)
7494 if (t
->points_to() != NULL
7495 && t
->points_to()->interface_type() != NULL
)
7496 reason
->assign(_("pointer to interface type has no methods"));
7498 reason
->assign(_("type has no methods"));
7503 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
7507 if (t
->points_to() != NULL
7508 && t
->points_to()->interface_type() != NULL
)
7509 reason
->assign(_("pointer to interface type has no methods"));
7511 reason
->assign(_("type has no methods"));
7516 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7517 p
!= this->all_methods_
->end();
7520 bool is_ambiguous
= false;
7521 Method
* m
= (nt
!= NULL
7522 ? nt
->method_function(p
->name(), &is_ambiguous
)
7523 : st
->method_function(p
->name(), &is_ambiguous
));
7528 std::string n
= Gogo::message_name(p
->name());
7529 size_t len
= n
.length() + 100;
7530 char* buf
= new char[len
];
7532 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
7533 open_quote
, n
.c_str(), close_quote
);
7535 snprintf(buf
, len
, _("missing method %s%s%s"),
7536 open_quote
, n
.c_str(), close_quote
);
7537 reason
->assign(buf
);
7543 Function_type
*p_fn_type
= p
->type()->function_type();
7544 Function_type
* m_fn_type
= m
->type()->function_type();
7545 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
7546 std::string subreason
;
7547 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
7551 std::string n
= Gogo::message_name(p
->name());
7552 size_t len
= 100 + n
.length() + subreason
.length();
7553 char* buf
= new char[len
];
7554 if (subreason
.empty())
7555 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
7556 open_quote
, n
.c_str(), close_quote
);
7559 _("incompatible type for method %s%s%s (%s)"),
7560 open_quote
, n
.c_str(), close_quote
,
7562 reason
->assign(buf
);
7568 if (!is_pointer
&& !m
->is_value_method())
7572 std::string n
= Gogo::message_name(p
->name());
7573 size_t len
= 100 + n
.length();
7574 char* buf
= new char[len
];
7576 _("method %s%s%s requires a pointer receiver"),
7577 open_quote
, n
.c_str(), close_quote
);
7578 reason
->assign(buf
);
7584 // If the magic //go:nointerface comment was used, the method
7585 // may not be used to implement interfaces.
7586 if (m
->nointerface())
7590 std::string n
= Gogo::message_name(p
->name());
7591 size_t len
= 100 + n
.length();
7592 char* buf
= new char[len
];
7594 _("method %s%s%s is marked go:nointerface"),
7595 open_quote
, n
.c_str(), close_quote
);
7596 reason
->assign(buf
);
7606 // Return the backend representation of the empty interface type. We
7607 // use the same struct for all empty interfaces.
7610 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
7612 static Btype
* empty_interface_type
;
7613 if (empty_interface_type
== NULL
)
7615 std::vector
<Backend::Btyped_identifier
> bfields(2);
7617 Location bloc
= Linemap::predeclared_location();
7619 Type
* pdt
= Type::make_type_descriptor_ptr_type();
7620 bfields
[0].name
= "__type_descriptor";
7621 bfields
[0].btype
= pdt
->get_backend(gogo
);
7622 bfields
[0].location
= bloc
;
7624 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
7625 bfields
[1].name
= "__object";
7626 bfields
[1].btype
= vt
->get_backend(gogo
);
7627 bfields
[1].location
= bloc
;
7629 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
7631 return empty_interface_type
;
7634 // Return a pointer to the backend representation of the method table.
7637 Interface_type::get_backend_methods(Gogo
* gogo
)
7639 if (this->bmethods_
!= NULL
&& !this->bmethods_is_placeholder_
)
7640 return this->bmethods_
;
7642 Location loc
= this->location();
7644 std::vector
<Backend::Btyped_identifier
>
7645 mfields(this->all_methods_
->size() + 1);
7647 Type
* pdt
= Type::make_type_descriptor_ptr_type();
7648 mfields
[0].name
= "__type_descriptor";
7649 mfields
[0].btype
= pdt
->get_backend(gogo
);
7650 mfields
[0].location
= loc
;
7652 std::string last_name
= "";
7654 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7655 p
!= this->all_methods_
->end();
7658 // The type of the method in Go only includes the parameters.
7659 // The actual method also has a receiver, which is always a
7660 // pointer. We need to add that pointer type here in order to
7661 // generate the correct type for the backend.
7662 Function_type
* ft
= p
->type()->function_type();
7663 go_assert(ft
->receiver() == NULL
);
7665 const Typed_identifier_list
* params
= ft
->parameters();
7666 Typed_identifier_list
* mparams
= new Typed_identifier_list();
7668 mparams
->reserve(params
->size() + 1);
7669 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
7670 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
7673 for (Typed_identifier_list::const_iterator pp
= params
->begin();
7674 pp
!= params
->end();
7676 mparams
->push_back(*pp
);
7679 Typed_identifier_list
* mresults
= (ft
->results() == NULL
7681 : ft
->results()->copy());
7682 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
7685 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
7686 mfields
[i
].btype
= mft
->get_backend_fntype(gogo
);
7687 mfields
[i
].location
= loc
;
7689 // Sanity check: the names should be sorted.
7690 go_assert(Gogo::unpack_hidden_name(p
->name())
7691 > Gogo::unpack_hidden_name(last_name
));
7692 last_name
= p
->name();
7695 Btype
* st
= gogo
->backend()->struct_type(mfields
);
7696 Btype
* ret
= gogo
->backend()->pointer_type(st
);
7698 if (this->bmethods_
!= NULL
&& this->bmethods_is_placeholder_
)
7699 gogo
->backend()->set_placeholder_pointer_type(this->bmethods_
, ret
);
7700 this->bmethods_
= ret
;
7701 this->bmethods_is_placeholder_
= false;
7705 // Return a placeholder for the pointer to the backend methods table.
7708 Interface_type::get_backend_methods_placeholder(Gogo
* gogo
)
7710 if (this->bmethods_
== NULL
)
7712 Location loc
= this->location();
7713 this->bmethods_
= gogo
->backend()->placeholder_pointer_type("", loc
,
7715 this->bmethods_is_placeholder_
= true;
7717 return this->bmethods_
;
7720 // Return the fields of a non-empty interface type. This is not
7721 // declared in types.h so that types.h doesn't have to #include
7725 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
7726 bool use_placeholder
,
7727 std::vector
<Backend::Btyped_identifier
>* bfields
)
7729 Location loc
= type
->location();
7733 (*bfields
)[0].name
= "__methods";
7734 (*bfields
)[0].btype
= (use_placeholder
7735 ? type
->get_backend_methods_placeholder(gogo
)
7736 : type
->get_backend_methods(gogo
));
7737 (*bfields
)[0].location
= loc
;
7739 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
7740 (*bfields
)[1].name
= "__object";
7741 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
7742 (*bfields
)[1].location
= Linemap::predeclared_location();
7745 // Return the backend representation for an interface type. An interface is a
7746 // pointer to a struct. The struct has three fields. The first field is a
7747 // pointer to the type descriptor for the dynamic type of the object.
7748 // The second field is a pointer to a table of methods for the
7749 // interface to be used with the object. The third field is the value
7750 // of the object itself.
7753 Interface_type::do_get_backend(Gogo
* gogo
)
7755 if (this->is_empty())
7756 return Interface_type::get_backend_empty_interface_type(gogo
);
7759 if (this->interface_btype_
!= NULL
)
7760 return this->interface_btype_
;
7761 this->interface_btype_
=
7762 gogo
->backend()->placeholder_struct_type("", this->location_
);
7763 std::vector
<Backend::Btyped_identifier
> bfields
;
7764 get_backend_interface_fields(gogo
, this, false, &bfields
);
7765 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
7767 this->interface_btype_
= gogo
->backend()->error_type();
7768 return this->interface_btype_
;
7772 // Finish the backend representation of the methods.
7775 Interface_type::finish_backend_methods(Gogo
* gogo
)
7777 if (!this->is_empty())
7779 const Typed_identifier_list
* methods
= this->methods();
7780 if (methods
!= NULL
)
7782 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7783 p
!= methods
->end();
7785 p
->type()->get_backend(gogo
);
7788 // Getting the backend methods now will set the placeholder
7790 this->get_backend_methods(gogo
);
7794 // The type of an interface type descriptor.
7797 Interface_type::make_interface_type_descriptor_type()
7802 Type
* tdt
= Type::make_type_descriptor_type();
7803 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7805 Type
* string_type
= Type::lookup_string_type();
7806 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
7809 Type::make_builtin_struct_type(3,
7810 "name", pointer_string_type
,
7811 "pkgPath", pointer_string_type
,
7814 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
7816 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
7818 Struct_type
* s
= Type::make_builtin_struct_type(2,
7820 "methods", slice_nsm
);
7822 ret
= Type::make_builtin_named_type("InterfaceType", s
);
7828 // Build a type descriptor for an interface type.
7831 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7833 Location bloc
= Linemap::predeclared_location();
7835 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
7837 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
7839 Expression_list
* ivals
= new Expression_list();
7842 Struct_field_list::const_iterator pif
= ifields
->begin();
7843 go_assert(pif
->is_field_name("commonType"));
7844 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
7845 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
7849 go_assert(pif
->is_field_name("methods"));
7851 Expression_list
* methods
= new Expression_list();
7852 if (this->all_methods_
!= NULL
)
7854 Type
* elemtype
= pif
->type()->array_type()->element_type();
7856 methods
->reserve(this->all_methods_
->size());
7857 for (Typed_identifier_list::const_iterator pm
=
7858 this->all_methods_
->begin();
7859 pm
!= this->all_methods_
->end();
7862 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
7864 Expression_list
* mvals
= new Expression_list();
7867 Struct_field_list::const_iterator pmf
= mfields
->begin();
7868 go_assert(pmf
->is_field_name("name"));
7869 std::string s
= Gogo::unpack_hidden_name(pm
->name());
7870 Expression
* e
= Expression::make_string(s
, bloc
);
7871 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7874 go_assert(pmf
->is_field_name("pkgPath"));
7875 if (!Gogo::is_hidden_name(pm
->name()))
7876 mvals
->push_back(Expression::make_nil(bloc
));
7879 s
= Gogo::hidden_name_pkgpath(pm
->name());
7880 e
= Expression::make_string(s
, bloc
);
7881 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7885 go_assert(pmf
->is_field_name("typ"));
7886 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
7889 go_assert(pmf
== mfields
->end());
7891 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
7893 methods
->push_back(e
);
7897 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
7901 go_assert(pif
== ifields
->end());
7903 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
7906 // Reflection string.
7909 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7911 ret
->append("interface {");
7912 const Typed_identifier_list
* methods
= this->parse_methods_
;
7913 if (methods
!= NULL
)
7915 ret
->push_back(' ');
7916 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7917 p
!= methods
->end();
7920 if (p
!= methods
->begin())
7922 if (p
->name().empty())
7923 this->append_reflection(p
->type(), gogo
, ret
);
7926 if (!Gogo::is_hidden_name(p
->name()))
7927 ret
->append(p
->name());
7928 else if (gogo
->pkgpath_from_option())
7929 ret
->append(p
->name().substr(1));
7932 // If no -fgo-pkgpath option, backward compatibility
7933 // for how this used to work before -fgo-pkgpath was
7935 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
7936 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
7937 ret
->push_back('.');
7938 ret
->append(Gogo::unpack_hidden_name(p
->name()));
7940 std::string sub
= p
->type()->reflection(gogo
);
7941 go_assert(sub
.compare(0, 4, "func") == 0);
7942 sub
= sub
.substr(4);
7946 ret
->push_back(' ');
7951 // Generate GC symbol for interface types.
7954 Interface_type::do_gc_symbol(Gogo
*, Expression_list
** vals
,
7955 Expression
** offset
, int)
7957 Location bloc
= Linemap::predeclared_location();
7958 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7960 unsigned long opval
= this->is_empty() ? GC_EFACE
: GC_IFACE
;
7961 (*vals
)->push_back(Expression::make_integer_ul(opval
, uintptr_type
, bloc
));
7962 (*vals
)->push_back(*offset
);
7963 this->advance_gc_offset(offset
);
7969 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7971 go_assert(this->methods_are_finalized_
);
7973 ret
->push_back('I');
7975 const Typed_identifier_list
* methods
= this->all_methods_
;
7976 if (methods
!= NULL
&& !this->seen_
)
7979 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7980 p
!= methods
->end();
7983 if (!p
->name().empty())
7986 if (!Gogo::is_hidden_name(p
->name()))
7991 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
7992 n
.append(Gogo::pkgpath_for_symbol(pkgpath
));
7994 n
.append(Gogo::unpack_hidden_name(p
->name()));
7997 snprintf(buf
, sizeof buf
, "%u_",
7998 static_cast<unsigned int>(n
.length()));
8002 this->append_mangled_name(p
->type(), gogo
, ret
);
8004 this->seen_
= false;
8007 ret
->push_back('e');
8013 Interface_type::do_export(Export
* exp
) const
8015 exp
->write_c_string("interface { ");
8017 const Typed_identifier_list
* methods
= this->parse_methods_
;
8018 if (methods
!= NULL
)
8020 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
8021 pm
!= methods
->end();
8024 if (pm
->name().empty())
8026 exp
->write_c_string("? ");
8027 exp
->write_type(pm
->type());
8031 exp
->write_string(pm
->name());
8032 exp
->write_c_string(" (");
8034 const Function_type
* fntype
= pm
->type()->function_type();
8037 const Typed_identifier_list
* parameters
= fntype
->parameters();
8038 if (parameters
!= NULL
)
8040 bool is_varargs
= fntype
->is_varargs();
8041 for (Typed_identifier_list::const_iterator pp
=
8042 parameters
->begin();
8043 pp
!= parameters
->end();
8049 exp
->write_c_string(", ");
8050 exp
->write_name(pp
->name());
8051 exp
->write_c_string(" ");
8052 if (!is_varargs
|| pp
+ 1 != parameters
->end())
8053 exp
->write_type(pp
->type());
8056 exp
->write_c_string("...");
8057 Type
*pptype
= pp
->type();
8058 exp
->write_type(pptype
->array_type()->element_type());
8063 exp
->write_c_string(")");
8065 const Typed_identifier_list
* results
= fntype
->results();
8066 if (results
!= NULL
)
8068 exp
->write_c_string(" ");
8069 if (results
->size() == 1 && results
->begin()->name().empty())
8070 exp
->write_type(results
->begin()->type());
8074 exp
->write_c_string("(");
8075 for (Typed_identifier_list::const_iterator p
=
8077 p
!= results
->end();
8083 exp
->write_c_string(", ");
8084 exp
->write_name(p
->name());
8085 exp
->write_c_string(" ");
8086 exp
->write_type(p
->type());
8088 exp
->write_c_string(")");
8093 exp
->write_c_string("; ");
8097 exp
->write_c_string("}");
8100 // Import an interface type.
8103 Interface_type::do_import(Import
* imp
)
8105 imp
->require_c_string("interface { ");
8107 Typed_identifier_list
* methods
= new Typed_identifier_list
;
8108 while (imp
->peek_char() != '}')
8110 std::string name
= imp
->read_identifier();
8114 imp
->require_c_string(" ");
8115 Type
* t
= imp
->read_type();
8116 methods
->push_back(Typed_identifier("", t
, imp
->location()));
8117 imp
->require_c_string("; ");
8121 imp
->require_c_string(" (");
8123 Typed_identifier_list
* parameters
;
8124 bool is_varargs
= false;
8125 if (imp
->peek_char() == ')')
8129 parameters
= new Typed_identifier_list
;
8132 std::string name
= imp
->read_name();
8133 imp
->require_c_string(" ");
8135 if (imp
->match_c_string("..."))
8141 Type
* ptype
= imp
->read_type();
8143 ptype
= Type::make_array_type(ptype
, NULL
);
8144 parameters
->push_back(Typed_identifier(name
, ptype
,
8146 if (imp
->peek_char() != ',')
8148 go_assert(!is_varargs
);
8149 imp
->require_c_string(", ");
8152 imp
->require_c_string(")");
8154 Typed_identifier_list
* results
;
8155 if (imp
->peek_char() != ' ')
8159 results
= new Typed_identifier_list
;
8161 if (imp
->peek_char() != '(')
8163 Type
* rtype
= imp
->read_type();
8164 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
8171 std::string name
= imp
->read_name();
8172 imp
->require_c_string(" ");
8173 Type
* rtype
= imp
->read_type();
8174 results
->push_back(Typed_identifier(name
, rtype
,
8176 if (imp
->peek_char() != ',')
8178 imp
->require_c_string(", ");
8180 imp
->require_c_string(")");
8184 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
8188 fntype
->set_is_varargs();
8189 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
8191 imp
->require_c_string("; ");
8194 imp
->require_c_string("}");
8196 if (methods
->empty())
8202 return Type::make_interface_type(methods
, imp
->location());
8205 // Make an interface type.
8208 Type::make_interface_type(Typed_identifier_list
* methods
,
8211 return new Interface_type(methods
, location
);
8214 // Make an empty interface type.
8217 Type::make_empty_interface_type(Location location
)
8219 Interface_type
* ret
= new Interface_type(NULL
, location
);
8220 ret
->finalize_methods();
8226 // Bind a method to an object.
8229 Method::bind_method(Expression
* expr
, Location location
) const
8231 if (this->stub_
== NULL
)
8233 // When there is no stub object, the binding is determined by
8235 return this->do_bind_method(expr
, location
);
8237 return Expression::make_bound_method(expr
, this, this->stub_
, location
);
8240 // Return the named object associated with a method. This may only be
8241 // called after methods are finalized.
8244 Method::named_object() const
8246 if (this->stub_
!= NULL
)
8248 return this->do_named_object();
8251 // Class Named_method.
8253 // The type of the method.
8256 Named_method::do_type() const
8258 if (this->named_object_
->is_function())
8259 return this->named_object_
->func_value()->type();
8260 else if (this->named_object_
->is_function_declaration())
8261 return this->named_object_
->func_declaration_value()->type();
8266 // Return the location of the method receiver.
8269 Named_method::do_receiver_location() const
8271 return this->do_type()->receiver()->location();
8274 // Bind a method to an object.
8277 Named_method::do_bind_method(Expression
* expr
, Location location
) const
8279 Named_object
* no
= this->named_object_
;
8280 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, this,
8282 // If this is not a local method, and it does not use a stub, then
8283 // the real method expects a different type. We need to cast the
8285 if (this->depth() > 0 && !this->needs_stub_method())
8287 Function_type
* ftype
= this->do_type();
8288 go_assert(ftype
->is_method());
8289 Type
* frtype
= ftype
->receiver()->type();
8290 bme
->set_first_argument_type(frtype
);
8295 // Return whether this method should not participate in interfaces.
8298 Named_method::do_nointerface() const
8300 Named_object
* no
= this->named_object_
;
8301 return no
->is_function() && no
->func_value()->nointerface();
8304 // Class Interface_method.
8306 // Bind a method to an object.
8309 Interface_method::do_bind_method(Expression
* expr
,
8310 Location location
) const
8312 return Expression::make_interface_field_reference(expr
, this->name_
,
8318 // Insert a new method. Return true if it was inserted, false
8322 Methods::insert(const std::string
& name
, Method
* m
)
8324 std::pair
<Method_map::iterator
, bool> ins
=
8325 this->methods_
.insert(std::make_pair(name
, m
));
8330 Method
* old_method
= ins
.first
->second
;
8331 if (m
->depth() < old_method
->depth())
8334 ins
.first
->second
= m
;
8339 if (m
->depth() == old_method
->depth())
8340 old_method
->set_is_ambiguous();
8346 // Return the number of unambiguous methods.
8349 Methods::count() const
8352 for (Method_map::const_iterator p
= this->methods_
.begin();
8353 p
!= this->methods_
.end();
8355 if (!p
->second
->is_ambiguous())
8360 // Class Named_type.
8362 // Return the name of the type.
8365 Named_type::name() const
8367 return this->named_object_
->name();
8370 // Return the name of the type to use in an error message.
8373 Named_type::message_name() const
8375 return this->named_object_
->message_name();
8378 // Whether this is an alias. There are currently only two aliases so
8379 // we just recognize them by name.
8382 Named_type::is_alias() const
8384 if (!this->is_builtin())
8386 const std::string
& name(this->name());
8387 return name
== "byte" || name
== "rune";
8390 // Return the base type for this type. We have to be careful about
8391 // circular type definitions, which are invalid but may be seen here.
8394 Named_type::named_base()
8399 Type
* ret
= this->type_
->base();
8400 this->seen_
= false;
8405 Named_type::named_base() const
8410 const Type
* ret
= this->type_
->base();
8411 this->seen_
= false;
8415 // Return whether this is an error type. We have to be careful about
8416 // circular type definitions, which are invalid but may be seen here.
8419 Named_type::is_named_error_type() const
8424 bool ret
= this->type_
->is_error_type();
8425 this->seen_
= false;
8429 // Whether this type is comparable. We have to be careful about
8430 // circular type definitions.
8433 Named_type::named_type_is_comparable(std::string
* reason
) const
8438 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
8439 this->type_
, reason
);
8440 this->seen_
= false;
8444 // Add a method to this type.
8447 Named_type::add_method(const std::string
& name
, Function
* function
)
8449 if (this->local_methods_
== NULL
)
8450 this->local_methods_
= new Bindings(NULL
);
8451 return this->local_methods_
->add_function(name
, NULL
, function
);
8454 // Add a method declaration to this type.
8457 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
8458 Function_type
* type
,
8461 if (this->local_methods_
== NULL
)
8462 this->local_methods_
= new Bindings(NULL
);
8463 return this->local_methods_
->add_function_declaration(name
, package
, type
,
8467 // Add an existing method to this type.
8470 Named_type::add_existing_method(Named_object
* no
)
8472 if (this->local_methods_
== NULL
)
8473 this->local_methods_
= new Bindings(NULL
);
8474 this->local_methods_
->add_named_object(no
);
8477 // Look for a local method NAME, and returns its named object, or NULL
8481 Named_type::find_local_method(const std::string
& name
) const
8483 if (this->local_methods_
== NULL
)
8485 return this->local_methods_
->lookup(name
);
8488 // Return whether NAME is an unexported field or method, for better
8492 Named_type::is_unexported_local_method(Gogo
* gogo
,
8493 const std::string
& name
) const
8495 Bindings
* methods
= this->local_methods_
;
8496 if (methods
!= NULL
)
8498 for (Bindings::const_declarations_iterator p
=
8499 methods
->begin_declarations();
8500 p
!= methods
->end_declarations();
8503 if (Gogo::is_hidden_name(p
->first
)
8504 && name
== Gogo::unpack_hidden_name(p
->first
)
8505 && gogo
->pack_hidden_name(name
, false) != p
->first
)
8512 // Build the complete list of methods for this type, which means
8513 // recursively including all methods for anonymous fields. Create all
8517 Named_type::finalize_methods(Gogo
* gogo
)
8519 if (this->all_methods_
!= NULL
)
8522 if (this->local_methods_
!= NULL
8523 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
8525 const Bindings
* lm
= this->local_methods_
;
8526 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
8527 p
!= lm
->end_declarations();
8529 error_at(p
->second
->location(),
8530 "invalid pointer or interface receiver type");
8531 delete this->local_methods_
;
8532 this->local_methods_
= NULL
;
8536 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
8539 // Return the method NAME, or NULL if there isn't one or if it is
8540 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
8544 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
8546 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
8549 // Return a pointer to the interface method table for this type for
8550 // the interface INTERFACE. IS_POINTER is true if this is for a
8554 Named_type::interface_method_table(Interface_type
* interface
, bool is_pointer
)
8556 return Type::interface_method_table(this, interface
, is_pointer
,
8557 &this->interface_method_tables_
,
8558 &this->pointer_interface_method_tables_
);
8561 // Look for a use of a complete type within another type. This is
8562 // used to check that we don't try to use a type within itself.
8564 class Find_type_use
: public Traverse
8567 Find_type_use(Named_type
* find_type
)
8568 : Traverse(traverse_types
),
8569 find_type_(find_type
), found_(false)
8572 // Whether we found the type.
8575 { return this->found_
; }
8582 // The type we are looking for.
8583 Named_type
* find_type_
;
8584 // Whether we found the type.
8588 // Check for FIND_TYPE in TYPE.
8591 Find_type_use::type(Type
* type
)
8593 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
8595 this->found_
= true;
8596 return TRAVERSE_EXIT
;
8599 // It's OK if we see a reference to the type in any type which is
8600 // essentially a pointer: a pointer, a slice, a function, a map, or
8602 if (type
->points_to() != NULL
8603 || type
->is_slice_type()
8604 || type
->function_type() != NULL
8605 || type
->map_type() != NULL
8606 || type
->channel_type() != NULL
)
8607 return TRAVERSE_SKIP_COMPONENTS
;
8609 // For an interface, a reference to the type in a method type should
8610 // be ignored, but we have to consider direct inheritance. When
8611 // this is called, there may be cases of direct inheritance
8612 // represented as a method with no name.
8613 if (type
->interface_type() != NULL
)
8615 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
8616 if (methods
!= NULL
)
8618 for (Typed_identifier_list::const_iterator p
= methods
->begin();
8619 p
!= methods
->end();
8622 if (p
->name().empty())
8624 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
8625 return TRAVERSE_EXIT
;
8629 return TRAVERSE_SKIP_COMPONENTS
;
8632 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
8633 // to convert TYPE to the backend representation before we convert
8635 if (type
->named_type() != NULL
)
8637 switch (type
->base()->classification())
8639 case Type::TYPE_ERROR
:
8640 case Type::TYPE_BOOLEAN
:
8641 case Type::TYPE_INTEGER
:
8642 case Type::TYPE_FLOAT
:
8643 case Type::TYPE_COMPLEX
:
8644 case Type::TYPE_STRING
:
8645 case Type::TYPE_NIL
:
8648 case Type::TYPE_ARRAY
:
8649 case Type::TYPE_STRUCT
:
8650 this->find_type_
->add_dependency(type
->named_type());
8653 case Type::TYPE_NAMED
:
8654 case Type::TYPE_FORWARD
:
8655 go_assert(saw_errors());
8658 case Type::TYPE_VOID
:
8659 case Type::TYPE_SINK
:
8660 case Type::TYPE_FUNCTION
:
8661 case Type::TYPE_POINTER
:
8662 case Type::TYPE_CALL_MULTIPLE_RESULT
:
8663 case Type::TYPE_MAP
:
8664 case Type::TYPE_CHANNEL
:
8665 case Type::TYPE_INTERFACE
:
8671 return TRAVERSE_CONTINUE
;
8674 // Verify that a named type does not refer to itself.
8677 Named_type::do_verify()
8679 if (this->is_verified_
)
8681 this->is_verified_
= true;
8683 Find_type_use
find(this);
8684 Type::traverse(this->type_
, &find
);
8687 error_at(this->location_
, "invalid recursive type %qs",
8688 this->message_name().c_str());
8689 this->is_error_
= true;
8693 // Check whether any of the local methods overloads an existing
8694 // struct field or interface method. We don't need to check the
8695 // list of methods against itself: that is handled by the Bindings
8697 if (this->local_methods_
!= NULL
)
8699 Struct_type
* st
= this->type_
->struct_type();
8702 for (Bindings::const_declarations_iterator p
=
8703 this->local_methods_
->begin_declarations();
8704 p
!= this->local_methods_
->end_declarations();
8707 const std::string
& name(p
->first
);
8708 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
8710 error_at(p
->second
->location(),
8711 "method %qs redeclares struct field name",
8712 Gogo::message_name(name
).c_str());
8721 // Return whether this type is or contains a pointer.
8724 Named_type::do_has_pointer() const
8729 bool ret
= this->type_
->has_pointer();
8730 this->seen_
= false;
8734 // Return whether comparisons for this type can use the identity
8738 Named_type::do_compare_is_identity(Gogo
* gogo
)
8740 // We don't use this->seen_ here because compare_is_identity may
8741 // call base() later, and that will mess up if seen_ is set here.
8742 if (this->seen_in_compare_is_identity_
)
8744 this->seen_in_compare_is_identity_
= true;
8745 bool ret
= this->type_
->compare_is_identity(gogo
);
8746 this->seen_in_compare_is_identity_
= false;
8750 // Return a hash code. This is used for method lookup. We simply
8751 // hash on the name itself.
8754 Named_type::do_hash_for_method(Gogo
* gogo
) const
8756 if (this->is_alias())
8757 return this->type_
->named_type()->do_hash_for_method(gogo
);
8759 const std::string
& name(this->named_object()->name());
8760 unsigned int ret
= Type::hash_string(name
, 0);
8762 // GOGO will be NULL here when called from Type_hash_identical.
8763 // That is OK because that is only used for internal hash tables
8764 // where we are going to be comparing named types for equality. In
8765 // other cases, which are cases where the runtime is going to
8766 // compare hash codes to see if the types are the same, we need to
8767 // include the pkgpath in the hash.
8768 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
8770 const Package
* package
= this->named_object()->package();
8771 if (package
== NULL
)
8772 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
8774 ret
= Type::hash_string(package
->pkgpath(), ret
);
8780 // Convert a named type to the backend representation. In order to
8781 // get dependencies right, we fill in a dummy structure for this type,
8782 // then convert all the dependencies, then complete this type. When
8783 // this function is complete, the size of the type is known.
8786 Named_type::convert(Gogo
* gogo
)
8788 if (this->is_error_
|| this->is_converted_
)
8791 this->create_placeholder(gogo
);
8793 // If we are called to turn unsafe.Sizeof into a constant, we may
8794 // not have verified the type yet. We have to make sure it is
8795 // verified, since that sets the list of dependencies.
8798 // Convert all the dependencies. If they refer indirectly back to
8799 // this type, they will pick up the intermediate representation we just
8801 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
8802 p
!= this->dependencies_
.end();
8804 (*p
)->convert(gogo
);
8806 // Complete this type.
8807 Btype
* bt
= this->named_btype_
;
8808 Type
* base
= this->type_
->base();
8809 switch (base
->classification())
8826 // The size of these types is already correct. We don't worry
8827 // about filling them in until later, when we also track
8828 // circular references.
8833 std::vector
<Backend::Btyped_identifier
> bfields
;
8834 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
8836 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8837 bt
= gogo
->backend()->error_type();
8842 // Slice types were completed in create_placeholder.
8843 if (!base
->is_slice_type())
8845 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
8846 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
8847 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
8848 bt
= gogo
->backend()->error_type();
8852 case TYPE_INTERFACE
:
8853 // Interface types were completed in create_placeholder.
8861 case TYPE_CALL_MULTIPLE_RESULT
:
8867 this->named_btype_
= bt
;
8868 this->is_converted_
= true;
8869 this->is_placeholder_
= false;
8872 // Create the placeholder for a named type. This is the first step in
8873 // converting to the backend representation.
8876 Named_type::create_placeholder(Gogo
* gogo
)
8878 if (this->is_error_
)
8879 this->named_btype_
= gogo
->backend()->error_type();
8881 if (this->named_btype_
!= NULL
)
8884 // Create the structure for this type. Note that because we call
8885 // base() here, we don't attempt to represent a named type defined
8886 // as another named type. Instead both named types will point to
8887 // different base representations.
8888 Type
* base
= this->type_
->base();
8890 bool set_name
= true;
8891 switch (base
->classification())
8894 this->is_error_
= true;
8895 this->named_btype_
= gogo
->backend()->error_type();
8905 // These are simple basic types, we can just create them
8907 bt
= Type::get_named_base_btype(gogo
, base
);
8912 // All maps and channels have the same backend representation.
8913 bt
= Type::get_named_base_btype(gogo
, base
);
8919 bool for_function
= base
->classification() == TYPE_FUNCTION
;
8920 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
8928 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8930 this->is_placeholder_
= true;
8935 if (base
->is_slice_type())
8936 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8940 bt
= gogo
->backend()->placeholder_array_type(this->name(),
8942 this->is_placeholder_
= true;
8947 case TYPE_INTERFACE
:
8948 if (base
->interface_type()->is_empty())
8949 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
8952 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8960 case TYPE_CALL_MULTIPLE_RESULT
:
8967 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
8969 this->named_btype_
= bt
;
8971 if (base
->is_slice_type())
8973 // We do not record slices as dependencies of other types,
8974 // because we can fill them in completely here with the final
8976 std::vector
<Backend::Btyped_identifier
> bfields
;
8977 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
8978 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8979 this->named_btype_
= gogo
->backend()->error_type();
8981 else if (base
->interface_type() != NULL
8982 && !base
->interface_type()->is_empty())
8984 // We do not record interfaces as dependencies of other types,
8985 // because we can fill them in completely here with the final
8987 std::vector
<Backend::Btyped_identifier
> bfields
;
8988 get_backend_interface_fields(gogo
, base
->interface_type(), true,
8990 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8991 this->named_btype_
= gogo
->backend()->error_type();
8995 // Get the backend representation for a named type.
8998 Named_type::do_get_backend(Gogo
* gogo
)
9000 if (this->is_error_
)
9001 return gogo
->backend()->error_type();
9003 Btype
* bt
= this->named_btype_
;
9005 if (!gogo
->named_types_are_converted())
9007 // We have not completed converting named types. NAMED_BTYPE_
9008 // is a placeholder and we shouldn't do anything further.
9012 // We don't build dependencies for types whose sizes do not
9013 // change or are not relevant, so we may see them here while
9014 // converting types.
9015 this->create_placeholder(gogo
);
9016 bt
= this->named_btype_
;
9017 go_assert(bt
!= NULL
);
9021 // We are not converting types. This should only be called if the
9022 // type has already been converted.
9023 if (!this->is_converted_
)
9025 go_assert(saw_errors());
9026 return gogo
->backend()->error_type();
9029 go_assert(bt
!= NULL
);
9031 // Complete the backend representation.
9032 Type
* base
= this->type_
->base();
9034 switch (base
->classification())
9037 return gogo
->backend()->error_type();
9051 if (!this->seen_in_get_backend_
)
9053 this->seen_in_get_backend_
= true;
9054 base
->struct_type()->finish_backend_fields(gogo
);
9055 this->seen_in_get_backend_
= false;
9060 if (!this->seen_in_get_backend_
)
9062 this->seen_in_get_backend_
= true;
9063 base
->array_type()->finish_backend_element(gogo
);
9064 this->seen_in_get_backend_
= false;
9068 case TYPE_INTERFACE
:
9069 if (!this->seen_in_get_backend_
)
9071 this->seen_in_get_backend_
= true;
9072 base
->interface_type()->finish_backend_methods(gogo
);
9073 this->seen_in_get_backend_
= false;
9078 // Don't build a circular data structure. GENERIC can't handle
9080 if (this->seen_in_get_backend_
)
9082 this->is_circular_
= true;
9083 return gogo
->backend()->circular_pointer_type(bt
, false);
9085 this->seen_in_get_backend_
= true;
9086 bt1
= Type::get_named_base_btype(gogo
, base
);
9087 this->seen_in_get_backend_
= false;
9088 if (this->is_circular_
)
9089 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
9090 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
9091 bt
= gogo
->backend()->error_type();
9095 // Don't build a circular data structure. GENERIC can't handle
9097 if (this->seen_in_get_backend_
)
9099 this->is_circular_
= true;
9100 return gogo
->backend()->circular_pointer_type(bt
, false);
9102 this->seen_in_get_backend_
= true;
9103 bt1
= Type::get_named_base_btype(gogo
, base
);
9104 this->seen_in_get_backend_
= false;
9105 if (this->is_circular_
)
9106 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
9107 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
9108 bt
= gogo
->backend()->error_type();
9113 case TYPE_CALL_MULTIPLE_RESULT
:
9122 // Build a type descriptor for a named type.
9125 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9127 if (name
== NULL
&& this->is_alias())
9128 return this->type_
->type_descriptor(gogo
, this->type_
);
9130 // If NAME is not NULL, then we don't really want the type
9131 // descriptor for this type; we want the descriptor for the
9132 // underlying type, giving it the name NAME.
9133 return this->named_type_descriptor(gogo
, this->type_
,
9134 name
== NULL
? this : name
);
9137 // Add to the reflection string. This is used mostly for the name of
9138 // the type used in a type descriptor, not for actual reflection
9142 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9144 if (this->is_alias())
9146 this->append_reflection(this->type_
, gogo
, ret
);
9149 if (!this->is_builtin())
9151 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
9152 // make a unique reflection string, so that the type
9153 // canonicalization in the reflect package will work. In order
9154 // to be compatible with the gc compiler, we put tabs into the
9155 // package path, so that the reflect methods can discard it.
9156 const Package
* package
= this->named_object_
->package();
9157 ret
->push_back('\t');
9158 ret
->append(package
!= NULL
9159 ? package
->pkgpath_symbol()
9160 : gogo
->pkgpath_symbol());
9161 ret
->push_back('\t');
9162 ret
->append(package
!= NULL
9163 ? package
->package_name()
9164 : gogo
->package_name());
9165 ret
->push_back('.');
9167 if (this->in_function_
!= NULL
)
9169 ret
->push_back('\t');
9170 const Typed_identifier
* rcvr
=
9171 this->in_function_
->func_value()->type()->receiver();
9174 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
9175 ret
->append(Gogo::unpack_hidden_name(rcvr_type
->name()));
9176 ret
->push_back('.');
9178 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
9179 ret
->push_back('$');
9180 if (this->in_function_index_
> 0)
9183 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
9185 ret
->push_back('$');
9187 ret
->push_back('\t');
9189 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
9192 // Generate GC symbol for named types.
9195 Named_type::do_gc_symbol(Gogo
* gogo
, Expression_list
** vals
,
9196 Expression
** offset
, int stack
)
9201 Type::gc_symbol(gogo
, this->real_type(), vals
, offset
, stack
);
9202 this->seen_
= false;
9206 // Get the mangled name.
9209 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
9211 if (this->is_alias())
9213 this->append_mangled_name(this->type_
, gogo
, ret
);
9216 Named_object
* no
= this->named_object_
;
9218 if (this->is_builtin())
9219 go_assert(this->in_function_
== NULL
);
9222 const std::string
& pkgpath(no
->package() == NULL
9223 ? gogo
->pkgpath_symbol()
9224 : no
->package()->pkgpath_symbol());
9226 name
.append(1, '.');
9227 if (this->in_function_
!= NULL
)
9229 const Typed_identifier
* rcvr
=
9230 this->in_function_
->func_value()->type()->receiver();
9233 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
9234 name
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
9235 name
.append(1, '.');
9237 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
9238 name
.append(1, '$');
9239 if (this->in_function_index_
> 0)
9242 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
9244 name
.append(1, '$');
9248 name
.append(Gogo::unpack_hidden_name(no
->name()));
9250 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
9255 // Export the type. This is called to export a global type.
9258 Named_type::export_named_type(Export
* exp
, const std::string
&) const
9260 // We don't need to write the name of the type here, because it will
9261 // be written by Export::write_type anyhow.
9262 exp
->write_c_string("type ");
9263 exp
->write_type(this);
9264 exp
->write_c_string(";\n");
9267 // Import a named type.
9270 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
9272 imp
->require_c_string("type ");
9273 Type
*type
= imp
->read_type();
9274 *ptype
= type
->named_type();
9275 go_assert(*ptype
!= NULL
);
9276 imp
->require_c_string(";\n");
9279 // Export the type when it is referenced by another type. In this
9280 // case Export::export_type will already have issued the name.
9283 Named_type::do_export(Export
* exp
) const
9285 exp
->write_type(this->type_
);
9287 // To save space, we only export the methods directly attached to
9289 Bindings
* methods
= this->local_methods_
;
9290 if (methods
== NULL
)
9293 exp
->write_c_string("\n");
9294 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
9295 p
!= methods
->end_definitions();
9298 exp
->write_c_string(" ");
9299 (*p
)->export_named_object(exp
);
9302 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
9303 p
!= methods
->end_declarations();
9306 if (p
->second
->is_function_declaration())
9308 exp
->write_c_string(" ");
9309 p
->second
->export_named_object(exp
);
9314 // Make a named type.
9317 Type::make_named_type(Named_object
* named_object
, Type
* type
,
9320 return new Named_type(named_object
, type
, location
);
9323 // Finalize the methods for TYPE. It will be a named type or a struct
9324 // type. This sets *ALL_METHODS to the list of methods, and builds
9325 // all required stubs.
9328 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
9329 Methods
** all_methods
)
9331 *all_methods
= new Methods();
9332 std::vector
<const Named_type
*> seen
;
9333 Type::add_methods_for_type(type
, NULL
, 0, false, false, &seen
, *all_methods
);
9334 if ((*all_methods
)->empty())
9336 delete *all_methods
;
9337 *all_methods
= NULL
;
9339 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
9342 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
9343 // build up the struct field indexes as we go. DEPTH is the depth of
9344 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
9345 // adding these methods for an anonymous field with pointer type.
9346 // NEEDS_STUB_METHOD is true if we need to use a stub method which
9347 // calls the real method. TYPES_SEEN is used to avoid infinite
9351 Type::add_methods_for_type(const Type
* type
,
9352 const Method::Field_indexes
* field_indexes
,
9354 bool is_embedded_pointer
,
9355 bool needs_stub_method
,
9356 std::vector
<const Named_type
*>* seen
,
9359 // Pointer types may not have methods.
9360 if (type
->points_to() != NULL
)
9363 const Named_type
* nt
= type
->named_type();
9366 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9374 seen
->push_back(nt
);
9376 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
9377 is_embedded_pointer
, needs_stub_method
,
9381 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
9382 is_embedded_pointer
, needs_stub_method
,
9385 // If we are called with depth > 0, then we are looking at an
9386 // anonymous field of a struct. If such a field has interface type,
9387 // then we need to add the interface methods. We don't want to add
9388 // them when depth == 0, because we will already handle them
9389 // following the usual rules for an interface type.
9391 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
9397 // Add the local methods for the named type NT to *METHODS. The
9398 // parameters are as for add_methods_to_type.
9401 Type::add_local_methods_for_type(const Named_type
* nt
,
9402 const Method::Field_indexes
* field_indexes
,
9404 bool is_embedded_pointer
,
9405 bool needs_stub_method
,
9408 const Bindings
* local_methods
= nt
->local_methods();
9409 if (local_methods
== NULL
)
9412 for (Bindings::const_declarations_iterator p
=
9413 local_methods
->begin_declarations();
9414 p
!= local_methods
->end_declarations();
9417 Named_object
* no
= p
->second
;
9418 bool is_value_method
= (is_embedded_pointer
9419 || !Type::method_expects_pointer(no
));
9420 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
9421 (needs_stub_method
|| depth
> 0));
9422 if (!methods
->insert(no
->name(), m
))
9427 // Add the embedded methods for TYPE to *METHODS. These are the
9428 // methods attached to anonymous fields. The parameters are as for
9429 // add_methods_to_type.
9432 Type::add_embedded_methods_for_type(const Type
* type
,
9433 const Method::Field_indexes
* field_indexes
,
9435 bool is_embedded_pointer
,
9436 bool needs_stub_method
,
9437 std::vector
<const Named_type
*>* seen
,
9440 // Look for anonymous fields in TYPE. TYPE has fields if it is a
9442 const Struct_type
* st
= type
->struct_type();
9446 const Struct_field_list
* fields
= st
->fields();
9451 for (Struct_field_list::const_iterator pf
= fields
->begin();
9452 pf
!= fields
->end();
9455 if (!pf
->is_anonymous())
9458 Type
* ftype
= pf
->type();
9459 bool is_pointer
= false;
9460 if (ftype
->points_to() != NULL
)
9462 ftype
= ftype
->points_to();
9465 Named_type
* fnt
= ftype
->named_type();
9468 // This is an error, but it will be diagnosed elsewhere.
9472 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
9473 sub_field_indexes
->next
= field_indexes
;
9474 sub_field_indexes
->field_index
= i
;
9476 Methods tmp_methods
;
9477 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
9478 (is_embedded_pointer
|| is_pointer
),
9484 // Check if there are promoted methods that conflict with field names and
9485 // don't add them to the method map.
9486 for (Methods::const_iterator p
= tmp_methods
.begin();
9487 p
!= tmp_methods
.end();
9491 for (Struct_field_list::const_iterator fp
= fields
->begin();
9492 fp
!= fields
->end();
9495 if (fp
->field_name() == p
->first
)
9502 !methods
->insert(p
->first
, p
->second
))
9508 // If TYPE is an interface type, then add its method to *METHODS.
9509 // This is for interface methods attached to an anonymous field. The
9510 // parameters are as for add_methods_for_type.
9513 Type::add_interface_methods_for_type(const Type
* type
,
9514 const Method::Field_indexes
* field_indexes
,
9518 const Interface_type
* it
= type
->interface_type();
9522 const Typed_identifier_list
* imethods
= it
->methods();
9523 if (imethods
== NULL
)
9526 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
9527 pm
!= imethods
->end();
9530 Function_type
* fntype
= pm
->type()->function_type();
9533 // This is an error, but it should be reported elsewhere
9534 // when we look at the methods for IT.
9537 go_assert(!fntype
->is_method());
9538 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
9539 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
9540 field_indexes
, depth
);
9541 if (!methods
->insert(pm
->name(), m
))
9546 // Build stub methods for TYPE as needed. METHODS is the set of
9547 // methods for the type. A stub method may be needed when a type
9548 // inherits a method from an anonymous field. When we need the
9549 // address of the method, as in a type descriptor, we need to build a
9550 // little stub which does the required field dereferences and jumps to
9551 // the real method. LOCATION is the location of the type definition.
9554 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
9557 if (methods
== NULL
)
9559 for (Methods::const_iterator p
= methods
->begin();
9560 p
!= methods
->end();
9563 Method
* m
= p
->second
;
9564 if (m
->is_ambiguous() || !m
->needs_stub_method())
9567 const std::string
& name(p
->first
);
9569 // Build a stub method.
9571 const Function_type
* fntype
= m
->type();
9573 static unsigned int counter
;
9575 snprintf(buf
, sizeof buf
, "$this%u", counter
);
9578 Type
* receiver_type
= const_cast<Type
*>(type
);
9579 if (!m
->is_value_method())
9580 receiver_type
= Type::make_pointer_type(receiver_type
);
9581 Location receiver_location
= m
->receiver_location();
9582 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
9585 const Typed_identifier_list
* fnparams
= fntype
->parameters();
9586 Typed_identifier_list
* stub_params
;
9587 if (fnparams
== NULL
|| fnparams
->empty())
9591 // We give each stub parameter a unique name.
9592 stub_params
= new Typed_identifier_list();
9593 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
9594 pp
!= fnparams
->end();
9598 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
9599 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
9605 const Typed_identifier_list
* fnresults
= fntype
->results();
9606 Typed_identifier_list
* stub_results
;
9607 if (fnresults
== NULL
|| fnresults
->empty())
9608 stub_results
= NULL
;
9611 // We create the result parameters without any names, since
9612 // we won't refer to them.
9613 stub_results
= new Typed_identifier_list();
9614 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
9615 pr
!= fnresults
->end();
9617 stub_results
->push_back(Typed_identifier("", pr
->type(),
9621 Function_type
* stub_type
= Type::make_function_type(receiver
,
9624 fntype
->location());
9625 if (fntype
->is_varargs())
9626 stub_type
->set_is_varargs();
9628 // We only create the function in the package which creates the
9630 const Package
* package
;
9631 if (type
->named_type() == NULL
)
9634 package
= type
->named_type()->named_object()->package();
9635 std::string stub_name
= name
+ "$stub";
9637 if (package
!= NULL
)
9638 stub
= Named_object::make_function_declaration(stub_name
, package
,
9639 stub_type
, location
);
9642 stub
= gogo
->start_function(stub_name
, stub_type
, false,
9643 fntype
->location());
9644 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
9645 fntype
->is_varargs(), location
);
9646 gogo
->finish_function(fntype
->location());
9648 if (type
->named_type() == NULL
&& stub
->is_function())
9649 stub
->func_value()->set_is_unnamed_type_stub_method();
9650 if (m
->nointerface() && stub
->is_function())
9651 stub
->func_value()->set_nointerface();
9654 m
->set_stub_object(stub
);
9658 // Build a stub method which adjusts the receiver as required to call
9659 // METHOD. RECEIVER_NAME is the name we used for the receiver.
9660 // PARAMS is the list of function parameters.
9663 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
9664 const char* receiver_name
,
9665 const Typed_identifier_list
* params
,
9669 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
9670 go_assert(receiver_object
!= NULL
);
9672 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
9673 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
9674 if (expr
->type()->points_to() == NULL
)
9675 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
9677 Expression_list
* arguments
;
9678 if (params
== NULL
|| params
->empty())
9682 arguments
= new Expression_list();
9683 for (Typed_identifier_list::const_iterator p
= params
->begin();
9687 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
9688 go_assert(param
!= NULL
);
9689 Expression
* param_ref
= Expression::make_var_reference(param
,
9691 arguments
->push_back(param_ref
);
9695 Expression
* func
= method
->bind_method(expr
, location
);
9696 go_assert(func
!= NULL
);
9697 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
9700 gogo
->add_statement(Statement::make_return_from_call(call
, location
));
9703 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
9704 // in reverse order.
9707 Type::apply_field_indexes(Expression
* expr
,
9708 const Method::Field_indexes
* field_indexes
,
9711 if (field_indexes
== NULL
)
9713 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
9714 Struct_type
* stype
= expr
->type()->deref()->struct_type();
9715 go_assert(stype
!= NULL
9716 && field_indexes
->field_index
< stype
->field_count());
9717 if (expr
->type()->struct_type() == NULL
)
9719 go_assert(expr
->type()->points_to() != NULL
);
9720 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
9721 go_assert(expr
->type()->struct_type() == stype
);
9723 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
9727 // Return whether NO is a method for which the receiver is a pointer.
9730 Type::method_expects_pointer(const Named_object
* no
)
9732 const Function_type
*fntype
;
9733 if (no
->is_function())
9734 fntype
= no
->func_value()->type();
9735 else if (no
->is_function_declaration())
9736 fntype
= no
->func_declaration_value()->type();
9739 return fntype
->receiver()->type()->points_to() != NULL
;
9742 // Given a set of methods for a type, METHODS, return the method NAME,
9743 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
9744 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
9745 // but is ambiguous (and return NULL).
9748 Type::method_function(const Methods
* methods
, const std::string
& name
,
9751 if (is_ambiguous
!= NULL
)
9752 *is_ambiguous
= false;
9753 if (methods
== NULL
)
9755 Methods::const_iterator p
= methods
->find(name
);
9756 if (p
== methods
->end())
9758 Method
* m
= p
->second
;
9759 if (m
->is_ambiguous())
9761 if (is_ambiguous
!= NULL
)
9762 *is_ambiguous
= true;
9768 // Return a pointer to the interface method table for TYPE for the
9769 // interface INTERFACE.
9772 Type::interface_method_table(Type
* type
,
9773 Interface_type
*interface
,
9775 Interface_method_tables
** method_tables
,
9776 Interface_method_tables
** pointer_tables
)
9778 go_assert(!interface
->is_empty());
9780 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
9783 *pimt
= new Interface_method_tables(5);
9785 std::pair
<Interface_type
*, Expression
*> val(interface
, NULL
);
9786 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
9788 Location loc
= Linemap::predeclared_location();
9791 // This is a new entry in the hash table.
9792 go_assert(ins
.first
->second
== NULL
);
9794 Expression::make_interface_mtable_ref(interface
, type
, is_pointer
, loc
);
9796 return Expression::make_unary(OPERATOR_AND
, ins
.first
->second
, loc
);
9799 // Look for field or method NAME for TYPE. Return an Expression for
9800 // the field or method bound to EXPR. If there is no such field or
9801 // method, give an appropriate error and return an error expression.
9804 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
9805 const std::string
& name
,
9808 if (type
->deref()->is_error_type())
9809 return Expression::make_error(location
);
9811 const Named_type
* nt
= type
->deref()->named_type();
9812 const Struct_type
* st
= type
->deref()->struct_type();
9813 const Interface_type
* it
= type
->interface_type();
9815 // If this is a pointer to a pointer, then it is possible that the
9816 // pointed-to type has methods.
9817 bool dereferenced
= false;
9821 && type
->points_to() != NULL
9822 && type
->points_to()->points_to() != NULL
)
9824 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
9825 type
= type
->points_to();
9826 if (type
->deref()->is_error_type())
9827 return Expression::make_error(location
);
9828 nt
= type
->points_to()->named_type();
9829 st
= type
->points_to()->struct_type();
9830 dereferenced
= true;
9833 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
9834 || expr
->is_addressable());
9835 std::vector
<const Named_type
*> seen
;
9836 bool is_method
= false;
9837 bool found_pointer_method
= false;
9840 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
9841 &seen
, NULL
, &is_method
,
9842 &found_pointer_method
, &ambig1
, &ambig2
))
9847 go_assert(st
!= NULL
);
9848 if (type
->struct_type() == NULL
)
9850 go_assert(type
->points_to() != NULL
);
9851 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
9853 go_assert(expr
->type()->struct_type() == st
);
9855 ret
= st
->field_reference(expr
, name
, location
);
9857 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
9858 ret
= Expression::make_interface_field_reference(expr
, name
,
9864 m
= nt
->method_function(name
, NULL
);
9865 else if (st
!= NULL
)
9866 m
= st
->method_function(name
, NULL
);
9869 go_assert(m
!= NULL
);
9873 "calling method %qs requires explicit dereference",
9874 Gogo::message_name(name
).c_str());
9875 return Expression::make_error(location
);
9877 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
9878 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
9879 ret
= m
->bind_method(expr
, location
);
9881 go_assert(ret
!= NULL
);
9886 if (Gogo::is_erroneous_name(name
))
9888 // An error was already reported.
9890 else if (!ambig1
.empty())
9891 error_at(location
, "%qs is ambiguous via %qs and %qs",
9892 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
9894 else if (found_pointer_method
)
9895 error_at(location
, "method requires a pointer receiver");
9896 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
9898 ("reference to field %qs in object which "
9899 "has no fields or methods"),
9900 Gogo::message_name(name
).c_str());
9904 // The test for 'a' and 'z' is to handle builtin names,
9905 // which are not hidden.
9906 if (!Gogo::is_hidden_name(name
) && (name
[0] < 'a' || name
[0] > 'z'))
9907 is_unexported
= false;
9910 std::string unpacked
= Gogo::unpack_hidden_name(name
);
9912 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
9917 error_at(location
, "reference to unexported field or method %qs",
9918 Gogo::message_name(name
).c_str());
9920 error_at(location
, "reference to undefined field or method %qs",
9921 Gogo::message_name(name
).c_str());
9923 return Expression::make_error(location
);
9927 // Look in TYPE for a field or method named NAME, return true if one
9928 // is found. This looks through embedded anonymous fields and handles
9929 // ambiguity. If a method is found, sets *IS_METHOD to true;
9930 // otherwise, if a field is found, set it to false. If
9931 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9932 // whose address can not be taken. SEEN is used to avoid infinite
9933 // recursion on invalid types.
9935 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9936 // method we couldn't use because it requires a pointer. LEVEL is
9937 // used for recursive calls, and can be NULL for a non-recursive call.
9938 // When this function returns false because it finds that the name is
9939 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9940 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9941 // will be unchanged.
9943 // This function just returns whether or not there is a field or
9944 // method, and whether it is a field or method. It doesn't build an
9945 // expression to refer to it. If it is a method, we then look in the
9946 // list of all methods for the type. If it is a field, the search has
9947 // to be done again, looking only for fields, and building up the
9948 // expression as we go.
9951 Type::find_field_or_method(const Type
* type
,
9952 const std::string
& name
,
9953 bool receiver_can_be_pointer
,
9954 std::vector
<const Named_type
*>* seen
,
9957 bool* found_pointer_method
,
9958 std::string
* ambig1
,
9959 std::string
* ambig2
)
9961 // Named types can have locally defined methods.
9962 const Named_type
* nt
= type
->named_type();
9963 if (nt
== NULL
&& type
->points_to() != NULL
)
9964 nt
= type
->points_to()->named_type();
9967 Named_object
* no
= nt
->find_local_method(name
);
9970 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
9976 // Record that we have found a pointer method in order to
9977 // give a better error message if we don't find anything
9979 *found_pointer_method
= true;
9982 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9988 // We've already seen this type when searching for methods.
9994 // Interface types can have methods.
9995 const Interface_type
* it
= type
->interface_type();
9996 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
10002 // Struct types can have fields. They can also inherit fields and
10003 // methods from anonymous fields.
10004 const Struct_type
* st
= type
->deref()->struct_type();
10007 const Struct_field_list
* fields
= st
->fields();
10008 if (fields
== NULL
)
10012 seen
->push_back(nt
);
10014 int found_level
= 0;
10015 bool found_is_method
= false;
10016 std::string found_ambig1
;
10017 std::string found_ambig2
;
10018 const Struct_field
* found_parent
= NULL
;
10019 for (Struct_field_list::const_iterator pf
= fields
->begin();
10020 pf
!= fields
->end();
10023 if (pf
->is_field_name(name
))
10025 *is_method
= false;
10031 if (!pf
->is_anonymous())
10034 if (pf
->type()->deref()->is_error_type()
10035 || pf
->type()->deref()->is_undefined())
10038 Named_type
* fnt
= pf
->type()->named_type();
10040 fnt
= pf
->type()->deref()->named_type();
10041 go_assert(fnt
!= NULL
);
10043 // Methods with pointer receivers on embedded field are
10044 // inherited by the pointer to struct, and also by the struct
10045 // type if the field itself is a pointer.
10046 bool can_be_pointer
= (receiver_can_be_pointer
10047 || pf
->type()->points_to() != NULL
);
10048 int sublevel
= level
== NULL
? 1 : *level
+ 1;
10049 bool sub_is_method
;
10050 std::string subambig1
;
10051 std::string subambig2
;
10052 bool subfound
= Type::find_field_or_method(fnt
,
10058 found_pointer_method
,
10063 if (!subambig1
.empty())
10065 // The name was found via this field, but is ambiguous.
10066 // if the ambiguity is lower or at the same level as
10067 // anything else we have already found, then we want to
10068 // pass the ambiguity back to the caller.
10069 if (found_level
== 0 || sublevel
<= found_level
)
10071 found_ambig1
= (Gogo::message_name(pf
->field_name())
10072 + '.' + subambig1
);
10073 found_ambig2
= (Gogo::message_name(pf
->field_name())
10074 + '.' + subambig2
);
10075 found_level
= sublevel
;
10081 // The name was found via this field. Use the level to see
10082 // if we want to use this one, or whether it introduces an
10084 if (found_level
== 0 || sublevel
< found_level
)
10086 found_level
= sublevel
;
10087 found_is_method
= sub_is_method
;
10088 found_ambig1
.clear();
10089 found_ambig2
.clear();
10090 found_parent
= &*pf
;
10092 else if (sublevel
> found_level
)
10094 else if (found_ambig1
.empty())
10096 // We found an ambiguity.
10097 go_assert(found_parent
!= NULL
);
10098 found_ambig1
= Gogo::message_name(found_parent
->field_name());
10099 found_ambig2
= Gogo::message_name(pf
->field_name());
10103 // We found an ambiguity, but we already know of one.
10104 // Just report the earlier one.
10109 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
10110 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
10111 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
10112 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
10117 if (found_level
== 0)
10119 else if (found_is_method
10120 && type
->named_type() != NULL
10121 && type
->points_to() != NULL
)
10123 // If this is a method inherited from a struct field in a named pointer
10124 // type, it is invalid to automatically dereference the pointer to the
10125 // struct to find this method.
10127 *level
= found_level
;
10131 else if (!found_ambig1
.empty())
10133 go_assert(!found_ambig1
.empty());
10134 ambig1
->assign(found_ambig1
);
10135 ambig2
->assign(found_ambig2
);
10137 *level
= found_level
;
10143 *level
= found_level
;
10144 *is_method
= found_is_method
;
10149 // Return whether NAME is an unexported field or method for TYPE.
10152 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
10153 const std::string
& name
,
10154 std::vector
<const Named_type
*>* seen
)
10156 const Named_type
* nt
= type
->named_type();
10158 nt
= type
->deref()->named_type();
10161 if (nt
->is_unexported_local_method(gogo
, name
))
10164 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
10170 // We've already seen this type.
10176 const Interface_type
* it
= type
->interface_type();
10177 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
10180 type
= type
->deref();
10182 const Struct_type
* st
= type
->struct_type();
10183 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
10189 const Struct_field_list
* fields
= st
->fields();
10190 if (fields
== NULL
)
10194 seen
->push_back(nt
);
10196 for (Struct_field_list::const_iterator pf
= fields
->begin();
10197 pf
!= fields
->end();
10200 if (pf
->is_anonymous()
10201 && !pf
->type()->deref()->is_error_type()
10202 && !pf
->type()->deref()->is_undefined())
10204 Named_type
* subtype
= pf
->type()->named_type();
10205 if (subtype
== NULL
)
10206 subtype
= pf
->type()->deref()->named_type();
10207 if (subtype
== NULL
)
10209 // This is an error, but it will be diagnosed elsewhere.
10212 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
10227 // Class Forward_declaration.
10229 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
10230 : Type(TYPE_FORWARD
),
10231 named_object_(named_object
->resolve()), warned_(false)
10233 go_assert(this->named_object_
->is_unknown()
10234 || this->named_object_
->is_type_declaration());
10237 // Return the named object.
10240 Forward_declaration_type::named_object()
10242 return this->named_object_
->resolve();
10245 const Named_object
*
10246 Forward_declaration_type::named_object() const
10248 return this->named_object_
->resolve();
10251 // Return the name of the forward declared type.
10254 Forward_declaration_type::name() const
10256 return this->named_object()->name();
10259 // Warn about a use of a type which has been declared but not defined.
10262 Forward_declaration_type::warn() const
10264 Named_object
* no
= this->named_object_
->resolve();
10265 if (no
->is_unknown())
10267 // The name was not defined anywhere.
10268 if (!this->warned_
)
10270 error_at(this->named_object_
->location(),
10271 "use of undefined type %qs",
10272 no
->message_name().c_str());
10273 this->warned_
= true;
10276 else if (no
->is_type_declaration())
10278 // The name was seen as a type, but the type was never defined.
10279 if (no
->type_declaration_value()->using_type())
10281 error_at(this->named_object_
->location(),
10282 "use of undefined type %qs",
10283 no
->message_name().c_str());
10284 this->warned_
= true;
10289 // The name was defined, but not as a type.
10290 if (!this->warned_
)
10292 error_at(this->named_object_
->location(), "expected type");
10293 this->warned_
= true;
10298 // Get the base type of a declaration. This gives an error if the
10299 // type has not yet been defined.
10302 Forward_declaration_type::real_type()
10304 if (this->is_defined())
10306 Named_type
* nt
= this->named_object()->type_value();
10307 if (!nt
->is_valid())
10308 return Type::make_error_type();
10309 return this->named_object()->type_value();
10314 return Type::make_error_type();
10319 Forward_declaration_type::real_type() const
10321 if (this->is_defined())
10323 const Named_type
* nt
= this->named_object()->type_value();
10324 if (!nt
->is_valid())
10325 return Type::make_error_type();
10326 return this->named_object()->type_value();
10331 return Type::make_error_type();
10335 // Return whether the base type is defined.
10338 Forward_declaration_type::is_defined() const
10340 return this->named_object()->is_type();
10343 // Add a method. This is used when methods are defined before the
10347 Forward_declaration_type::add_method(const std::string
& name
,
10348 Function
* function
)
10350 Named_object
* no
= this->named_object();
10351 if (no
->is_unknown())
10352 no
->declare_as_type();
10353 return no
->type_declaration_value()->add_method(name
, function
);
10356 // Add a method declaration. This is used when methods are declared
10357 // before the type.
10360 Forward_declaration_type::add_method_declaration(const std::string
& name
,
10362 Function_type
* type
,
10365 Named_object
* no
= this->named_object();
10366 if (no
->is_unknown())
10367 no
->declare_as_type();
10368 Type_declaration
* td
= no
->type_declaration_value();
10369 return td
->add_method_declaration(name
, package
, type
, location
);
10375 Forward_declaration_type::do_traverse(Traverse
* traverse
)
10377 if (this->is_defined()
10378 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
10379 return TRAVERSE_EXIT
;
10380 return TRAVERSE_CONTINUE
;
10383 // Verify the type.
10386 Forward_declaration_type::do_verify()
10388 if (!this->is_defined() && !this->is_nil_constant_as_type())
10396 // Get the backend representation for the type.
10399 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
10401 if (this->is_defined())
10402 return Type::get_named_base_btype(gogo
, this->real_type());
10405 return gogo
->backend()->error_type();
10407 // We represent an undefined type as a struct with no fields. That
10408 // should work fine for the backend, since the same case can arise
10410 std::vector
<Backend::Btyped_identifier
> fields
;
10411 Btype
* bt
= gogo
->backend()->struct_type(fields
);
10412 return gogo
->backend()->named_type(this->name(), bt
,
10413 this->named_object()->location());
10416 // Build a type descriptor for a forwarded type.
10419 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
10421 Location ploc
= Linemap::predeclared_location();
10422 if (!this->is_defined())
10423 return Expression::make_error(ploc
);
10426 Type
* t
= this->real_type();
10428 return this->named_type_descriptor(gogo
, t
, name
);
10430 return Expression::make_type_descriptor(t
, ploc
);
10434 // The reflection string.
10437 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
10439 this->append_reflection(this->real_type(), gogo
, ret
);
10442 // The mangled name.
10445 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
10447 if (this->is_defined())
10448 this->append_mangled_name(this->real_type(), gogo
, ret
);
10451 const Named_object
* no
= this->named_object();
10453 if (no
->package() == NULL
)
10454 name
= gogo
->pkgpath_symbol();
10456 name
= no
->package()->pkgpath_symbol();
10458 name
+= Gogo::unpack_hidden_name(no
->name());
10460 snprintf(buf
, sizeof buf
, "N%u_",
10461 static_cast<unsigned int>(name
.length()));
10467 // Export a forward declaration. This can happen when a defined type
10468 // refers to a type which is only declared (and is presumably defined
10469 // in some other file in the same package).
10472 Forward_declaration_type::do_export(Export
*) const
10474 // If there is a base type, that should be exported instead of this.
10475 go_assert(!this->is_defined());
10477 // We don't output anything.
10480 // Make a forward declaration.
10483 Type::make_forward_declaration(Named_object
* named_object
)
10485 return new Forward_declaration_type(named_object
);
10488 // Class Typed_identifier_list.
10490 // Sort the entries by name.
10492 struct Typed_identifier_list_sort
10496 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
10498 return (Gogo::unpack_hidden_name(t1
.name())
10499 < Gogo::unpack_hidden_name(t2
.name()));
10504 Typed_identifier_list::sort_by_name()
10506 std::sort(this->entries_
.begin(), this->entries_
.end(),
10507 Typed_identifier_list_sort());
10513 Typed_identifier_list::traverse(Traverse
* traverse
)
10515 for (Typed_identifier_list::const_iterator p
= this->begin();
10519 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
10520 return TRAVERSE_EXIT
;
10522 return TRAVERSE_CONTINUE
;
10527 Typed_identifier_list
*
10528 Typed_identifier_list::copy() const
10530 Typed_identifier_list
* ret
= new Typed_identifier_list();
10531 for (Typed_identifier_list::const_iterator p
= this->begin();
10534 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));