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
)
47 // Get the base type for a type--skip names and forward declarations.
52 switch (this->classification_
)
55 return this->named_type()->named_base();
57 return this->forward_declaration_type()->real_type()->base();
66 switch (this->classification_
)
69 return this->named_type()->named_base();
71 return this->forward_declaration_type()->real_type()->base();
77 // Skip defined forward declarations.
83 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
84 while (ftype
!= NULL
&& ftype
->is_defined())
86 t
= ftype
->real_type();
87 ftype
= t
->forward_declaration_type();
93 Type::forwarded() const
96 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
97 while (ftype
!= NULL
&& ftype
->is_defined())
99 t
= ftype
->real_type();
100 ftype
= t
->forward_declaration_type();
105 // If this is a named type, return it. Otherwise, return NULL.
110 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
114 Type::named_type() const
116 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
119 // Return true if this type is not defined.
122 Type::is_undefined() const
124 return this->forwarded()->forward_declaration_type() != NULL
;
127 // Return true if this is a basic type: a type which is not composed
128 // of other types, and is not void.
131 Type::is_basic_type() const
133 switch (this->classification_
)
156 return this->base()->is_basic_type();
163 // Return true if this is an abstract type.
166 Type::is_abstract() const
168 switch (this->classification())
171 return this->integer_type()->is_abstract();
173 return this->float_type()->is_abstract();
175 return this->complex_type()->is_abstract();
177 return this->is_abstract_string_type();
179 return this->is_abstract_boolean_type();
185 // Return a non-abstract version of an abstract type.
188 Type::make_non_abstract_type()
190 go_assert(this->is_abstract());
191 switch (this->classification())
194 if (this->integer_type()->is_rune())
195 return Type::lookup_integer_type("int32");
197 return Type::lookup_integer_type("int");
199 return Type::lookup_float_type("float64");
201 return Type::lookup_complex_type("complex128");
203 return Type::lookup_string_type();
205 return Type::lookup_bool_type();
211 // Return true if this is an error type. Don't give an error if we
212 // try to dereference an undefined forwarding type, as this is called
213 // in the parser when the type may legitimately be undefined.
216 Type::is_error_type() const
218 const Type
* t
= this->forwarded();
219 // Note that we return false for an undefined forward type.
220 switch (t
->classification_
)
225 return t
->named_type()->is_named_error_type();
231 // If this is a pointer type, return the type to which it points.
232 // Otherwise, return NULL.
235 Type::points_to() const
237 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
239 return ptype
== NULL
? NULL
: ptype
->points_to();
242 // Return whether this is a slice type.
245 Type::is_slice_type() const
247 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
250 // Return whether this is the predeclared constant nil being used as a
254 Type::is_nil_constant_as_type() const
256 const Type
* t
= this->forwarded();
257 if (t
->forward_declaration_type() != NULL
)
259 const Named_object
* no
= t
->forward_declaration_type()->named_object();
260 if (no
->is_unknown())
261 no
= no
->unknown_value()->real_named_object();
264 && no
->const_value()->expr()->is_nil_expression())
273 Type::traverse(Type
* type
, Traverse
* traverse
)
275 go_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
276 || (traverse
->traverse_mask()
277 & Traverse::traverse_expressions
) != 0);
278 if (traverse
->remember_type(type
))
280 // We have already traversed this type.
281 return TRAVERSE_CONTINUE
;
283 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
285 int t
= traverse
->type(type
);
286 if (t
== TRAVERSE_EXIT
)
287 return TRAVERSE_EXIT
;
288 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
289 return TRAVERSE_CONTINUE
;
291 // An array type has an expression which we need to traverse if
292 // traverse_expressions is set.
293 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
294 return TRAVERSE_EXIT
;
295 return TRAVERSE_CONTINUE
;
298 // Default implementation for do_traverse for child class.
301 Type::do_traverse(Traverse
*)
303 return TRAVERSE_CONTINUE
;
306 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
307 // then return true for all erroneous types; this is used to avoid
308 // cascading errors. If REASON is not NULL, optionally set *REASON to
309 // the reason the types are not identical.
312 Type::are_identical(const Type
* t1
, const Type
* t2
, bool errors_are_identical
,
315 if (t1
== NULL
|| t2
== NULL
)
317 // Something is wrong.
318 return errors_are_identical
? true : t1
== t2
;
321 // Skip defined forward declarations.
322 t1
= t1
->forwarded();
323 t2
= t2
->forwarded();
325 // Ignore aliases for purposes of type identity.
326 if (t1
->named_type() != NULL
&& t1
->named_type()->is_alias())
327 t1
= t1
->named_type()->real_type();
328 if (t2
->named_type() != NULL
&& t2
->named_type()->is_alias())
329 t2
= t2
->named_type()->real_type();
334 // An undefined forward declaration is an error.
335 if (t1
->forward_declaration_type() != NULL
336 || t2
->forward_declaration_type() != NULL
)
337 return errors_are_identical
;
339 // Avoid cascading errors with error types.
340 if (t1
->is_error_type() || t2
->is_error_type())
342 if (errors_are_identical
)
344 return t1
->is_error_type() && t2
->is_error_type();
347 // Get a good reason for the sink type. Note that the sink type on
348 // the left hand side of an assignment is handled in are_assignable.
349 if (t1
->is_sink_type() || t2
->is_sink_type())
352 *reason
= "invalid use of _";
356 // A named type is only identical to itself.
357 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
360 // Check type shapes.
361 if (t1
->classification() != t2
->classification())
364 switch (t1
->classification())
370 // These types are always identical.
374 return t1
->integer_type()->is_identical(t2
->integer_type());
377 return t1
->float_type()->is_identical(t2
->float_type());
380 return t1
->complex_type()->is_identical(t2
->complex_type());
383 return t1
->function_type()->is_identical(t2
->function_type(),
385 errors_are_identical
,
389 return Type::are_identical(t1
->points_to(), t2
->points_to(),
390 errors_are_identical
, reason
);
393 return t1
->struct_type()->is_identical(t2
->struct_type(),
394 errors_are_identical
);
397 return t1
->array_type()->is_identical(t2
->array_type(),
398 errors_are_identical
);
401 return t1
->map_type()->is_identical(t2
->map_type(),
402 errors_are_identical
);
405 return t1
->channel_type()->is_identical(t2
->channel_type(),
406 errors_are_identical
);
409 return t1
->interface_type()->is_identical(t2
->interface_type(),
410 errors_are_identical
);
412 case TYPE_CALL_MULTIPLE_RESULT
:
414 *reason
= "invalid use of multiple-value function call";
422 // Return true if it's OK to have a binary operation with types LHS
423 // and RHS. This is not used for shifts or comparisons.
426 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
428 if (Type::are_identical(lhs
, rhs
, true, NULL
))
431 // A constant of abstract bool type may be mixed with any bool type.
432 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
433 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
436 // A constant of abstract string type may be mixed with any string
438 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
439 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
445 // A constant of abstract integer, float, or complex type may be
446 // mixed with an integer, float, or complex type.
447 if ((rhs
->is_abstract()
448 && (rhs
->integer_type() != NULL
449 || rhs
->float_type() != NULL
450 || rhs
->complex_type() != NULL
)
451 && (lhs
->integer_type() != NULL
452 || lhs
->float_type() != NULL
453 || lhs
->complex_type() != NULL
))
454 || (lhs
->is_abstract()
455 && (lhs
->integer_type() != NULL
456 || lhs
->float_type() != NULL
457 || lhs
->complex_type() != NULL
)
458 && (rhs
->integer_type() != NULL
459 || rhs
->float_type() != NULL
460 || rhs
->complex_type() != NULL
)))
463 // The nil type may be compared to a pointer, an interface type, a
464 // slice type, a channel type, a map type, or a function type.
465 if (lhs
->is_nil_type()
466 && (rhs
->points_to() != NULL
467 || rhs
->interface_type() != NULL
468 || rhs
->is_slice_type()
469 || rhs
->map_type() != NULL
470 || rhs
->channel_type() != NULL
471 || rhs
->function_type() != NULL
))
473 if (rhs
->is_nil_type()
474 && (lhs
->points_to() != NULL
475 || lhs
->interface_type() != NULL
476 || lhs
->is_slice_type()
477 || lhs
->map_type() != NULL
478 || lhs
->channel_type() != NULL
479 || lhs
->function_type() != NULL
))
485 // Return true if a value with type T1 may be compared with a value of
486 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
489 Type::are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
490 const Type
*t2
, std::string
*reason
)
493 && !Type::are_assignable(t1
, t2
, NULL
)
494 && !Type::are_assignable(t2
, t1
, NULL
))
497 *reason
= "incompatible types in binary expression";
503 if (t1
->integer_type() == NULL
504 && t1
->float_type() == NULL
505 && !t1
->is_string_type())
508 *reason
= _("invalid comparison of non-ordered type");
512 else if (t1
->is_slice_type()
513 || t1
->map_type() != NULL
514 || t1
->function_type() != NULL
515 || t2
->is_slice_type()
516 || t2
->map_type() != NULL
517 || t2
->function_type() != NULL
)
519 if (!t1
->is_nil_type() && !t2
->is_nil_type())
523 if (t1
->is_slice_type() || t2
->is_slice_type())
524 *reason
= _("slice can only be compared to nil");
525 else if (t1
->map_type() != NULL
|| t2
->map_type() != NULL
)
526 *reason
= _("map can only be compared to nil");
528 *reason
= _("func can only be compared to nil");
530 // Match 6g error messages.
531 if (t1
->interface_type() != NULL
|| t2
->interface_type() != NULL
)
534 snprintf(buf
, sizeof buf
, _("invalid operation (%s)"),
544 if (!t1
->is_boolean_type()
545 && t1
->integer_type() == NULL
546 && t1
->float_type() == NULL
547 && t1
->complex_type() == NULL
548 && !t1
->is_string_type()
549 && t1
->points_to() == NULL
550 && t1
->channel_type() == NULL
551 && t1
->interface_type() == NULL
552 && t1
->struct_type() == NULL
553 && t1
->array_type() == NULL
554 && !t1
->is_nil_type())
557 *reason
= _("invalid comparison of non-comparable type");
561 if (t1
->named_type() != NULL
)
562 return t1
->named_type()->named_type_is_comparable(reason
);
563 else if (t2
->named_type() != NULL
)
564 return t2
->named_type()->named_type_is_comparable(reason
);
565 else if (t1
->struct_type() != NULL
)
567 const Struct_field_list
* fields
= t1
->struct_type()->fields();
568 for (Struct_field_list::const_iterator p
= fields
->begin();
572 if (!p
->type()->is_comparable())
575 *reason
= _("invalid comparison of non-comparable struct");
580 else if (t1
->array_type() != NULL
)
582 if (t1
->array_type()->length()->is_nil_expression()
583 || !t1
->array_type()->element_type()->is_comparable())
586 *reason
= _("invalid comparison of non-comparable array");
595 // Return true if a value with type RHS may be assigned to a variable
596 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
597 // hidden fields are modified. If REASON is not NULL, set *REASON to
598 // the reason the types are not assignable.
601 Type::are_assignable_check_hidden(const Type
* lhs
, const Type
* rhs
,
602 bool check_hidden_fields
,
605 // Do some checks first. Make sure the types are defined.
606 if (rhs
!= NULL
&& !rhs
->is_undefined())
608 if (rhs
->is_void_type())
611 *reason
= "non-value used as value";
614 if (rhs
->is_call_multiple_result_type())
617 reason
->assign(_("multiple-value function call in "
618 "single-value context"));
623 if (lhs
!= NULL
&& !lhs
->is_undefined())
625 // Any value may be assigned to the blank identifier.
626 if (lhs
->is_sink_type())
629 // All fields of a struct must be exported, or the assignment
630 // must be in the same package.
631 if (check_hidden_fields
&& rhs
!= NULL
&& !rhs
->is_undefined())
633 if (lhs
->has_hidden_fields(NULL
, reason
)
634 || rhs
->has_hidden_fields(NULL
, reason
))
639 // Identical types are assignable.
640 if (Type::are_identical(lhs
, rhs
, true, reason
))
643 // The types are assignable if they have identical underlying types
644 // and either LHS or RHS is not a named type.
645 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
646 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
647 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
650 // The types are assignable if LHS is an interface type and RHS
651 // implements the required methods.
652 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
653 if (lhs_interface_type
!= NULL
)
655 if (lhs_interface_type
->implements_interface(rhs
, reason
))
657 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
658 if (rhs_interface_type
!= NULL
659 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
664 // The type are assignable if RHS is a bidirectional channel type,
665 // LHS is a channel type, they have identical element types, and
666 // either LHS or RHS is not a named type.
667 if (lhs
->channel_type() != NULL
668 && rhs
->channel_type() != NULL
669 && rhs
->channel_type()->may_send()
670 && rhs
->channel_type()->may_receive()
671 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
672 && Type::are_identical(lhs
->channel_type()->element_type(),
673 rhs
->channel_type()->element_type(),
678 // The nil type may be assigned to a pointer, function, slice, map,
679 // channel, or interface type.
680 if (rhs
->is_nil_type()
681 && (lhs
->points_to() != NULL
682 || lhs
->function_type() != NULL
683 || lhs
->is_slice_type()
684 || lhs
->map_type() != NULL
685 || lhs
->channel_type() != NULL
686 || lhs
->interface_type() != NULL
))
689 // An untyped numeric constant may be assigned to a numeric type if
690 // it is representable in that type.
691 if ((rhs
->is_abstract()
692 && (rhs
->integer_type() != NULL
693 || rhs
->float_type() != NULL
694 || rhs
->complex_type() != NULL
))
695 && (lhs
->integer_type() != NULL
696 || lhs
->float_type() != NULL
697 || lhs
->complex_type() != NULL
))
700 // Give some better error messages.
701 if (reason
!= NULL
&& reason
->empty())
703 if (rhs
->interface_type() != NULL
)
704 reason
->assign(_("need explicit conversion"));
705 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
707 size_t len
= (lhs
->named_type()->name().length()
708 + rhs
->named_type()->name().length()
710 char* buf
= new char[len
];
711 snprintf(buf
, len
, _("cannot use type %s as type %s"),
712 rhs
->named_type()->message_name().c_str(),
713 lhs
->named_type()->message_name().c_str());
722 // Return true if a value with type RHS may be assigned to a variable
723 // with type LHS. If REASON is not NULL, set *REASON to the reason
724 // the types are not assignable.
727 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
729 return Type::are_assignable_check_hidden(lhs
, rhs
, false, reason
);
732 // Like are_assignable but don't check for hidden fields.
735 Type::are_assignable_hidden_ok(const Type
* lhs
, const Type
* rhs
,
738 return Type::are_assignable_check_hidden(lhs
, rhs
, false, reason
);
741 // Return true if a value with type RHS may be converted to type LHS.
742 // If REASON is not NULL, set *REASON to the reason the types are not
746 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
748 // The types are convertible if they are assignable.
749 if (Type::are_assignable(lhs
, rhs
, reason
))
752 // The types are convertible if they have identical underlying
754 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
755 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
758 // The types are convertible if they are both unnamed pointer types
759 // and their pointer base types have identical underlying types.
760 if (lhs
->named_type() == NULL
761 && rhs
->named_type() == NULL
762 && lhs
->points_to() != NULL
763 && rhs
->points_to() != NULL
764 && (lhs
->points_to()->named_type() != NULL
765 || rhs
->points_to()->named_type() != NULL
)
766 && Type::are_identical(lhs
->points_to()->base(),
767 rhs
->points_to()->base(),
772 // Integer and floating point types are convertible to each other.
773 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
774 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
777 // Complex types are convertible to each other.
778 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
781 // An integer, or []byte, or []rune, may be converted to a string.
782 if (lhs
->is_string_type())
784 if (rhs
->integer_type() != NULL
)
786 if (rhs
->is_slice_type())
788 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
789 if (e
->integer_type() != NULL
790 && (e
->integer_type()->is_byte()
791 || e
->integer_type()->is_rune()))
796 // A string may be converted to []byte or []rune.
797 if (rhs
->is_string_type() && lhs
->is_slice_type())
799 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
800 if (e
->integer_type() != NULL
801 && (e
->integer_type()->is_byte() || e
->integer_type()->is_rune()))
805 // An unsafe.Pointer type may be converted to any pointer type or to
806 // uintptr, and vice-versa.
807 if (lhs
->is_unsafe_pointer_type()
808 && (rhs
->points_to() != NULL
809 || (rhs
->integer_type() != NULL
810 && rhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
812 if (rhs
->is_unsafe_pointer_type()
813 && (lhs
->points_to() != NULL
814 || (lhs
->integer_type() != NULL
815 && lhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
818 // Give a better error message.
822 *reason
= "invalid type conversion";
825 std::string s
= "invalid type conversion (";
835 // Return whether this type has any hidden fields. This is only a
836 // possibility for a few types.
839 Type::has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
841 switch (this->forwarded()->classification_
)
844 return this->named_type()->named_type_has_hidden_fields(reason
);
846 return this->struct_type()->struct_has_hidden_fields(within
, reason
);
848 return this->array_type()->array_has_hidden_fields(within
, reason
);
854 // Return a hash code for the type to be used for method lookup.
857 Type::hash_for_method(Gogo
* gogo
) const
859 unsigned int ret
= 0;
860 if (this->classification_
!= TYPE_FORWARD
)
861 ret
+= this->classification_
;
862 return ret
+ this->do_hash_for_method(gogo
);
865 // Default implementation of do_hash_for_method. This is appropriate
866 // for types with no subfields.
869 Type::do_hash_for_method(Gogo
*) const
874 // Return a hash code for a string, given a starting hash.
877 Type::hash_string(const std::string
& s
, unsigned int h
)
879 const char* p
= s
.data();
880 size_t len
= s
.length();
881 for (; len
> 0; --len
)
889 // A hash table mapping unnamed types to the backend representation of
892 Type::Type_btypes
Type::type_btypes
;
894 // Return the backend representation for this type.
897 Type::get_backend(Gogo
* gogo
)
899 if (this->btype_
!= NULL
)
902 if (this->forward_declaration_type() != NULL
903 || this->named_type() != NULL
)
904 return this->get_btype_without_hash(gogo
);
906 if (this->is_error_type())
907 return gogo
->backend()->error_type();
909 // To avoid confusing the backend, translate all identical Go types
910 // to the same backend representation. We use a hash table to do
911 // that. There is no need to use the hash table for named types, as
912 // named types are only identical to themselves.
914 std::pair
<Type
*, Type_btype_entry
> val
;
916 val
.second
.btype
= NULL
;
917 val
.second
.is_placeholder
= false;
918 std::pair
<Type_btypes::iterator
, bool> ins
=
919 Type::type_btypes
.insert(val
);
920 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
922 // Note that GOGO can be NULL here, but only when the GCC
923 // middle-end is asking for a frontend type. That will only
924 // happen for simple types, which should never require
926 if (!ins
.first
->second
.is_placeholder
)
927 this->btype_
= ins
.first
->second
.btype
;
928 else if (gogo
->named_types_are_converted())
930 this->finish_backend(gogo
, ins
.first
->second
.btype
);
931 ins
.first
->second
.is_placeholder
= false;
934 return ins
.first
->second
.btype
;
937 Btype
* bt
= this->get_btype_without_hash(gogo
);
939 if (ins
.first
->second
.btype
== NULL
)
941 ins
.first
->second
.btype
= bt
;
942 ins
.first
->second
.is_placeholder
= false;
946 // We have already created a backend representation for this
947 // type. This can happen when an unnamed type is defined using
948 // a named type which in turns uses an identical unnamed type.
949 // Use the representation we created earlier and ignore the one we just
951 if (this->btype_
== bt
)
952 this->btype_
= ins
.first
->second
.btype
;
953 bt
= ins
.first
->second
.btype
;
959 // Return the backend representation for a type without looking in the
960 // hash table for identical types. This is used for named types,
961 // since a named type is never identical to any other type.
964 Type::get_btype_without_hash(Gogo
* gogo
)
966 if (this->btype_
== NULL
)
968 Btype
* bt
= this->do_get_backend(gogo
);
970 // For a recursive function or pointer type, we will temporarily
971 // return a circular pointer type during the recursion. We
972 // don't want to record that for a forwarding type, as it may
974 if (this->forward_declaration_type() != NULL
975 && gogo
->backend()->is_circular_pointer_type(bt
))
978 if (gogo
== NULL
|| !gogo
->named_types_are_converted())
986 // Get the backend representation of a type without forcing the
987 // creation of the backend representation of all supporting types.
988 // This will return a backend type that has the correct size but may
989 // be incomplete. E.g., a pointer will just be a placeholder pointer,
990 // and will not contain the final representation of the type to which
991 // it points. This is used while converting all named types to the
992 // backend representation, to avoid problems with indirect references
993 // to types which are not yet complete. When this is called, the
994 // sizes of all direct references (e.g., a struct field) should be
995 // known, but the sizes of indirect references (e.g., the type to
996 // which a pointer points) may not.
999 Type::get_backend_placeholder(Gogo
* gogo
)
1001 if (gogo
->named_types_are_converted())
1002 return this->get_backend(gogo
);
1003 if (this->btype_
!= NULL
)
1004 return this->btype_
;
1007 switch (this->classification_
)
1017 // These are simple types that can just be created directly.
1018 return this->get_backend(gogo
);
1022 // All maps and channels have the same backend representation.
1023 return this->get_backend(gogo
);
1027 // Named types keep track of their own dependencies and manage
1028 // their own placeholders.
1029 return this->get_backend(gogo
);
1031 case TYPE_INTERFACE
:
1032 if (this->interface_type()->is_empty())
1033 return Interface_type::get_backend_empty_interface_type(gogo
);
1040 std::pair
<Type
*, Type_btype_entry
> val
;
1042 val
.second
.btype
= NULL
;
1043 val
.second
.is_placeholder
= false;
1044 std::pair
<Type_btypes::iterator
, bool> ins
=
1045 Type::type_btypes
.insert(val
);
1046 if (!ins
.second
&& ins
.first
->second
.btype
!= NULL
)
1047 return ins
.first
->second
.btype
;
1049 switch (this->classification_
)
1053 // A Go function type is a pointer to a struct type.
1054 Location loc
= this->function_type()->location();
1055 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1061 Location loc
= Linemap::unknown_location();
1062 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1067 // We don't have to make the struct itself be a placeholder. We
1068 // are promised that we know the sizes of the struct fields.
1069 // But we may have to use a placeholder for any particular
1072 std::vector
<Backend::Btyped_identifier
> bfields
;
1073 get_backend_struct_fields(gogo
, this->struct_type()->fields(),
1075 bt
= gogo
->backend()->struct_type(bfields
);
1080 if (this->is_slice_type())
1082 std::vector
<Backend::Btyped_identifier
> bfields
;
1083 get_backend_slice_fields(gogo
, this->array_type(), true, &bfields
);
1084 bt
= gogo
->backend()->struct_type(bfields
);
1088 Btype
* element
= this->array_type()->get_backend_element(gogo
, true);
1089 Bexpression
* len
= this->array_type()->get_backend_length(gogo
);
1090 bt
= gogo
->backend()->array_type(element
, len
);
1094 case TYPE_INTERFACE
:
1096 go_assert(!this->interface_type()->is_empty());
1097 std::vector
<Backend::Btyped_identifier
> bfields
;
1098 get_backend_interface_fields(gogo
, this->interface_type(), true,
1100 bt
= gogo
->backend()->struct_type(bfields
);
1105 case TYPE_CALL_MULTIPLE_RESULT
:
1106 /* Note that various classifications were handled in the earlier
1112 if (ins
.first
->second
.btype
== NULL
)
1114 ins
.first
->second
.btype
= bt
;
1115 ins
.first
->second
.is_placeholder
= true;
1119 // A placeholder for this type got created along the way. Use
1120 // that one and ignore the one we just built.
1121 bt
= ins
.first
->second
.btype
;
1127 // Complete the backend representation. This is called for a type
1128 // using a placeholder type.
1131 Type::finish_backend(Gogo
* gogo
, Btype
*placeholder
)
1133 switch (this->classification_
)
1147 Btype
* bt
= this->do_get_backend(gogo
);
1148 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1149 go_assert(saw_errors());
1155 Btype
* bt
= this->do_get_backend(gogo
);
1156 if (!gogo
->backend()->set_placeholder_pointer_type(placeholder
, bt
))
1157 go_assert(saw_errors());
1162 // The struct type itself is done, but we have to make sure that
1163 // all the field types are converted.
1164 this->struct_type()->finish_backend_fields(gogo
);
1168 // The array type itself is done, but make sure the element type
1170 this->array_type()->finish_backend_element(gogo
);
1177 case TYPE_INTERFACE
:
1178 // The interface type itself is done, but make sure the method
1179 // types are converted.
1180 this->interface_type()->finish_backend_methods(gogo
);
1188 case TYPE_CALL_MULTIPLE_RESULT
:
1193 this->btype_
= placeholder
;
1196 // Return a pointer to the type descriptor for this type.
1199 Type::type_descriptor_pointer(Gogo
* gogo
, Location location
)
1201 Type
* t
= this->forwarded();
1202 if (t
->named_type() != NULL
&& t
->named_type()->is_alias())
1203 t
= t
->named_type()->real_type();
1204 if (t
->type_descriptor_var_
== NULL
)
1206 t
->make_type_descriptor_var(gogo
);
1207 go_assert(t
->type_descriptor_var_
!= NULL
);
1209 Bexpression
* var_expr
=
1210 gogo
->backend()->var_expression(t
->type_descriptor_var_
, location
);
1211 return gogo
->backend()->address_expression(var_expr
, location
);
1214 // A mapping from unnamed types to type descriptor variables.
1216 Type::Type_descriptor_vars
Type::type_descriptor_vars
;
1218 // Build the type descriptor for this type.
1221 Type::make_type_descriptor_var(Gogo
* gogo
)
1223 go_assert(this->type_descriptor_var_
== NULL
);
1225 Named_type
* nt
= this->named_type();
1227 // We can have multiple instances of unnamed types, but we only want
1228 // to emit the type descriptor once. We use a hash table. This is
1229 // not necessary for named types, as they are unique, and we store
1230 // the type descriptor in the type itself.
1231 Bvariable
** phash
= NULL
;
1234 Bvariable
* bvnull
= NULL
;
1235 std::pair
<Type_descriptor_vars::iterator
, bool> ins
=
1236 Type::type_descriptor_vars
.insert(std::make_pair(this, bvnull
));
1239 // We've already build a type descriptor for this type.
1240 this->type_descriptor_var_
= ins
.first
->second
;
1243 phash
= &ins
.first
->second
;
1246 std::string var_name
= this->type_descriptor_var_name(gogo
, nt
);
1248 // Build the contents of the type descriptor.
1249 Expression
* initializer
= this->do_type_descriptor(gogo
, NULL
);
1251 Btype
* initializer_btype
= initializer
->type()->get_backend(gogo
);
1253 Location loc
= nt
== NULL
? Linemap::predeclared_location() : nt
->location();
1255 const Package
* dummy
;
1256 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
1258 this->type_descriptor_var_
=
1259 gogo
->backend()->immutable_struct_reference(var_name
,
1263 *phash
= this->type_descriptor_var_
;
1267 // See if this type descriptor can appear in multiple packages.
1268 bool is_common
= false;
1271 // We create the descriptor for a builtin type whenever we need
1273 is_common
= nt
->is_builtin();
1277 // This is an unnamed type. The descriptor could be defined in
1278 // any package where it is needed, and the linker will pick one
1279 // descriptor to keep.
1283 // We are going to build the type descriptor in this package. We
1284 // must create the variable before we convert the initializer to the
1285 // backend representation, because the initializer may refer to the
1286 // type descriptor of this type. By setting type_descriptor_var_ we
1287 // ensure that type_descriptor_pointer will work if called while
1288 // converting INITIALIZER.
1290 this->type_descriptor_var_
=
1291 gogo
->backend()->immutable_struct(var_name
, false, is_common
,
1292 initializer_btype
, loc
);
1294 *phash
= this->type_descriptor_var_
;
1296 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
1297 context
.set_is_const();
1298 Bexpression
* binitializer
= initializer
->get_backend(&context
);
1300 gogo
->backend()->immutable_struct_set_init(this->type_descriptor_var_
,
1301 var_name
, false, is_common
,
1302 initializer_btype
, loc
,
1306 // Return the name of the type descriptor variable. If NT is not
1307 // NULL, use it to get the name. Otherwise this is an unnamed type.
1310 Type::type_descriptor_var_name(Gogo
* gogo
, Named_type
* nt
)
1313 return "__go_td_" + this->mangled_name(gogo
);
1315 Named_object
* no
= nt
->named_object();
1317 const Named_object
* in_function
= nt
->in_function(&index
);
1318 std::string ret
= "__go_tdn_";
1319 if (nt
->is_builtin())
1320 go_assert(in_function
== NULL
);
1323 const std::string
& pkgpath(no
->package() == NULL
1324 ? gogo
->pkgpath_symbol()
1325 : no
->package()->pkgpath_symbol());
1326 ret
.append(pkgpath
);
1328 if (in_function
!= NULL
)
1330 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
1335 snprintf(buf
, sizeof buf
, "%u", index
);
1342 // FIXME: This adds in pkgpath twice for hidden symbols, which is
1344 const std::string
& name(no
->name());
1345 if (!Gogo::is_hidden_name(name
))
1350 ret
.append(Gogo::pkgpath_for_symbol(Gogo::hidden_name_pkgpath(name
)));
1352 ret
.append(Gogo::unpack_hidden_name(name
));
1358 // Return true if this type descriptor is defined in a different
1359 // package. If this returns true it sets *PACKAGE to the package.
1362 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1363 const Package
** package
)
1367 if (nt
->named_object()->package() != NULL
)
1369 // This is a named type defined in a different package. The
1370 // type descriptor should be defined in that package.
1371 *package
= nt
->named_object()->package();
1377 if (this->points_to() != NULL
1378 && this->points_to()->named_type() != NULL
1379 && this->points_to()->named_type()->named_object()->package() != NULL
)
1381 // This is an unnamed pointer to a named type defined in a
1382 // different package. The descriptor should be defined in
1384 *package
= this->points_to()->named_type()->named_object()->package();
1391 // Return a composite literal for a type descriptor.
1394 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1396 return type
->do_type_descriptor(gogo
, NULL
);
1399 // Return a composite literal for a type descriptor with a name.
1402 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1404 go_assert(name
!= NULL
&& type
->named_type() != name
);
1405 return type
->do_type_descriptor(gogo
, name
);
1408 // Make a builtin struct type from a list of fields. The fields are
1409 // pairs of a name and a type.
1412 Type::make_builtin_struct_type(int nfields
, ...)
1415 va_start(ap
, nfields
);
1417 Location bloc
= Linemap::predeclared_location();
1418 Struct_field_list
* sfl
= new Struct_field_list();
1419 for (int i
= 0; i
< nfields
; i
++)
1421 const char* field_name
= va_arg(ap
, const char *);
1422 Type
* type
= va_arg(ap
, Type
*);
1423 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1428 return Type::make_struct_type(sfl
, bloc
);
1431 // A list of builtin named types.
1433 std::vector
<Named_type
*> Type::named_builtin_types
;
1435 // Make a builtin named type.
1438 Type::make_builtin_named_type(const char* name
, Type
* type
)
1440 Location bloc
= Linemap::predeclared_location();
1441 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1442 Named_type
* ret
= no
->type_value();
1443 Type::named_builtin_types
.push_back(ret
);
1447 // Convert the named builtin types.
1450 Type::convert_builtin_named_types(Gogo
* gogo
)
1452 for (std::vector
<Named_type
*>::const_iterator p
=
1453 Type::named_builtin_types
.begin();
1454 p
!= Type::named_builtin_types
.end();
1457 bool r
= (*p
)->verify();
1459 (*p
)->convert(gogo
);
1463 // Return the type of a type descriptor. We should really tie this to
1464 // runtime.Type rather than copying it. This must match commonType in
1465 // libgo/go/runtime/type.go.
1468 Type::make_type_descriptor_type()
1473 Location bloc
= Linemap::predeclared_location();
1475 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1476 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1477 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1478 Type
* string_type
= Type::lookup_string_type();
1479 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1481 // This is an unnamed version of unsafe.Pointer. Perhaps we
1482 // should use the named version instead, although that would
1483 // require us to create the unsafe package if it has not been
1484 // imported. It probably doesn't matter.
1485 Type
* void_type
= Type::make_void_type();
1486 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1488 // Forward declaration for the type descriptor type.
1489 Named_object
* named_type_descriptor_type
=
1490 Named_object::make_type_declaration("commonType", NULL
, bloc
);
1491 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1492 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1494 // The type of a method on a concrete type.
1495 Struct_type
* method_type
=
1496 Type::make_builtin_struct_type(5,
1497 "name", pointer_string_type
,
1498 "pkgPath", pointer_string_type
,
1499 "mtyp", pointer_type_descriptor_type
,
1500 "typ", pointer_type_descriptor_type
,
1501 "tfn", unsafe_pointer_type
);
1502 Named_type
* named_method_type
=
1503 Type::make_builtin_named_type("method", method_type
);
1505 // Information for types with a name or methods.
1506 Type
* slice_named_method_type
=
1507 Type::make_array_type(named_method_type
, NULL
);
1508 Struct_type
* uncommon_type
=
1509 Type::make_builtin_struct_type(3,
1510 "name", pointer_string_type
,
1511 "pkgPath", pointer_string_type
,
1512 "methods", slice_named_method_type
);
1513 Named_type
* named_uncommon_type
=
1514 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1516 Type
* pointer_uncommon_type
=
1517 Type::make_pointer_type(named_uncommon_type
);
1519 // The type descriptor type.
1521 Struct_type
* type_descriptor_type
=
1522 Type::make_builtin_struct_type(11,
1524 "align", uint8_type
,
1525 "fieldAlign", uint8_type
,
1526 "size", uintptr_type
,
1527 "hash", uint32_type
,
1528 "hashfn", uintptr_type
,
1529 "equalfn", uintptr_type
,
1530 "string", pointer_string_type
,
1531 "", pointer_uncommon_type
,
1533 pointer_type_descriptor_type
,
1534 "zero", unsafe_pointer_type
);
1536 Named_type
* named
= Type::make_builtin_named_type("commonType",
1537 type_descriptor_type
);
1539 named_type_descriptor_type
->set_type_value(named
);
1547 // Make the type of a pointer to a type descriptor as represented in
1551 Type::make_type_descriptor_ptr_type()
1555 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1559 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1560 // hash code for this type and which compare whether two values of
1561 // this type are equal. If NAME is not NULL it is the name of this
1562 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1563 // functions, for convenience; they may be NULL.
1566 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1567 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1568 Named_object
** equal_fn
)
1570 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1572 Location bloc
= Linemap::predeclared_location();
1574 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1575 Type
* void_type
= Type::make_void_type();
1576 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1578 if (hash_fntype
== NULL
)
1580 Typed_identifier_list
* params
= new Typed_identifier_list();
1581 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1583 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1585 Typed_identifier_list
* results
= new Typed_identifier_list();
1586 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1588 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1590 if (equal_fntype
== NULL
)
1592 Typed_identifier_list
* params
= new Typed_identifier_list();
1593 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1595 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1597 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1599 Typed_identifier_list
* results
= new Typed_identifier_list();
1600 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1603 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1607 const char* hash_fnname
;
1608 const char* equal_fnname
;
1609 if (this->compare_is_identity(gogo
))
1611 hash_fnname
= "__go_type_hash_identity";
1612 equal_fnname
= "__go_type_equal_identity";
1614 else if (!this->is_comparable())
1616 hash_fnname
= "__go_type_hash_error";
1617 equal_fnname
= "__go_type_equal_error";
1621 switch (this->base()->classification())
1623 case Type::TYPE_ERROR
:
1624 case Type::TYPE_VOID
:
1625 case Type::TYPE_NIL
:
1626 case Type::TYPE_FUNCTION
:
1627 case Type::TYPE_MAP
:
1628 // For these types is_comparable should have returned false.
1631 case Type::TYPE_BOOLEAN
:
1632 case Type::TYPE_INTEGER
:
1633 case Type::TYPE_POINTER
:
1634 case Type::TYPE_CHANNEL
:
1635 // For these types compare_is_identity should have returned true.
1638 case Type::TYPE_FLOAT
:
1639 hash_fnname
= "__go_type_hash_float";
1640 equal_fnname
= "__go_type_equal_float";
1643 case Type::TYPE_COMPLEX
:
1644 hash_fnname
= "__go_type_hash_complex";
1645 equal_fnname
= "__go_type_equal_complex";
1648 case Type::TYPE_STRING
:
1649 hash_fnname
= "__go_type_hash_string";
1650 equal_fnname
= "__go_type_equal_string";
1653 case Type::TYPE_STRUCT
:
1655 // This is a struct which can not be compared using a
1656 // simple identity function. We need to build a function
1658 this->specific_type_functions(gogo
, name
, hash_fntype
,
1659 equal_fntype
, hash_fn
, equal_fn
);
1663 case Type::TYPE_ARRAY
:
1664 if (this->is_slice_type())
1666 // Type::is_compatible_for_comparison should have
1672 // This is an array which can not be compared using a
1673 // simple identity function. We need to build a
1674 // function for comparison.
1675 this->specific_type_functions(gogo
, name
, hash_fntype
,
1676 equal_fntype
, hash_fn
, equal_fn
);
1681 case Type::TYPE_INTERFACE
:
1682 if (this->interface_type()->is_empty())
1684 hash_fnname
= "__go_type_hash_empty_interface";
1685 equal_fnname
= "__go_type_equal_empty_interface";
1689 hash_fnname
= "__go_type_hash_interface";
1690 equal_fnname
= "__go_type_equal_interface";
1694 case Type::TYPE_NAMED
:
1695 case Type::TYPE_FORWARD
:
1704 Location bloc
= Linemap::predeclared_location();
1705 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1707 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1708 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1709 equal_fntype
, bloc
);
1710 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1713 // A hash table mapping types to the specific hash functions.
1715 Type::Type_functions
Type::type_functions_table
;
1717 // Handle a type function which is specific to a type: a struct or
1718 // array which can not use an identity comparison.
1721 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
,
1722 Function_type
* hash_fntype
,
1723 Function_type
* equal_fntype
,
1724 Named_object
** hash_fn
,
1725 Named_object
** equal_fn
)
1727 Hash_equal_fn
fnull(NULL
, NULL
);
1728 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1729 std::pair
<Type_functions::iterator
, bool> ins
=
1730 Type::type_functions_table
.insert(val
);
1733 // We already have functions for this type
1734 *hash_fn
= ins
.first
->second
.first
;
1735 *equal_fn
= ins
.first
->second
.second
;
1739 std::string base_name
;
1742 // Mangled names can have '.' if they happen to refer to named
1743 // types in some way. That's fine if this is simply a named
1744 // type, but otherwise it will confuse the code that builds
1745 // function identifiers. Remove '.' when necessary.
1746 base_name
= this->mangled_name(gogo
);
1748 while ((i
= base_name
.find('.')) != std::string::npos
)
1750 base_name
= gogo
->pack_hidden_name(base_name
, false);
1754 // This name is already hidden or not as appropriate.
1755 base_name
= name
->name();
1757 const Named_object
* in_function
= name
->in_function(&index
);
1758 if (in_function
!= NULL
)
1760 base_name
+= '$' + Gogo::unpack_hidden_name(in_function
->name());
1764 snprintf(buf
, sizeof buf
, "%u", index
);
1770 std::string hash_name
= base_name
+ "$hash";
1771 std::string equal_name
= base_name
+ "$equal";
1773 Location bloc
= Linemap::predeclared_location();
1775 const Package
* package
= NULL
;
1776 bool is_defined_elsewhere
=
1777 this->type_descriptor_defined_elsewhere(name
, &package
);
1778 if (is_defined_elsewhere
)
1780 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
1782 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
1783 equal_fntype
, bloc
);
1787 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
1788 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
1792 ins
.first
->second
.first
= *hash_fn
;
1793 ins
.first
->second
.second
= *equal_fn
;
1795 if (!is_defined_elsewhere
)
1797 if (gogo
->in_global_scope())
1798 this->write_specific_type_functions(gogo
, name
, hash_name
, hash_fntype
,
1799 equal_name
, equal_fntype
);
1801 gogo
->queue_specific_type_function(this, name
, hash_name
, hash_fntype
,
1802 equal_name
, equal_fntype
);
1806 // Write the hash and equality functions for a type which needs to be
1807 // written specially.
1810 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
,
1811 const std::string
& hash_name
,
1812 Function_type
* hash_fntype
,
1813 const std::string
& equal_name
,
1814 Function_type
* equal_fntype
)
1816 Location bloc
= Linemap::predeclared_location();
1818 if (gogo
->specific_type_functions_are_written())
1820 go_assert(saw_errors());
1824 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
1826 gogo
->start_block(bloc
);
1828 if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
1829 this->write_named_hash(gogo
, name
, hash_fntype
, equal_fntype
);
1830 else if (this->struct_type() != NULL
)
1831 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
1833 else if (this->array_type() != NULL
)
1834 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
1839 Block
* b
= gogo
->finish_block(bloc
);
1840 gogo
->add_block(b
, bloc
);
1841 gogo
->lower_block(hash_fn
, b
);
1842 gogo
->finish_function(bloc
);
1844 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
1846 gogo
->start_block(bloc
);
1848 if (name
!= NULL
&& name
->real_type()->named_type() != NULL
)
1849 this->write_named_equal(gogo
, name
);
1850 else if (this->struct_type() != NULL
)
1851 this->struct_type()->write_equal_function(gogo
, name
);
1852 else if (this->array_type() != NULL
)
1853 this->array_type()->write_equal_function(gogo
, name
);
1857 b
= gogo
->finish_block(bloc
);
1858 gogo
->add_block(b
, bloc
);
1859 gogo
->lower_block(equal_fn
, b
);
1860 gogo
->finish_function(bloc
);
1863 // Write a hash function that simply calls the hash function for a
1864 // named type. This is used when one named type is defined as
1865 // another. This ensures that this case works when the other named
1866 // type is defined in another package and relies on calling hash
1867 // functions defined only in that package.
1870 Type::write_named_hash(Gogo
* gogo
, Named_type
* name
,
1871 Function_type
* hash_fntype
, Function_type
* equal_fntype
)
1873 Location bloc
= Linemap::predeclared_location();
1875 Named_type
* base_type
= name
->real_type()->named_type();
1876 go_assert(base_type
!= NULL
);
1878 // The pointer to the type we are going to hash. This is an
1880 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
1881 go_assert(key_arg
!= NULL
);
1883 // The size of the type we are going to hash.
1884 Named_object
* keysz_arg
= gogo
->lookup("key_size", NULL
);
1885 go_assert(keysz_arg
!= NULL
);
1887 Named_object
* hash_fn
;
1888 Named_object
* equal_fn
;
1889 name
->real_type()->type_functions(gogo
, base_type
, hash_fntype
, equal_fntype
,
1890 &hash_fn
, &equal_fn
);
1892 // Call the hash function for the base type.
1893 Expression
* key_ref
= Expression::make_var_reference(key_arg
, bloc
);
1894 Expression
* keysz_ref
= Expression::make_var_reference(keysz_arg
, bloc
);
1895 Expression_list
* args
= new Expression_list();
1896 args
->push_back(key_ref
);
1897 args
->push_back(keysz_ref
);
1898 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
1899 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
1901 // Return the hash of the base type.
1902 Expression_list
* vals
= new Expression_list();
1903 vals
->push_back(call
);
1904 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
1905 gogo
->add_statement(s
);
1908 // Write an equality function that simply calls the equality function
1909 // for a named type. This is used when one named type is defined as
1910 // another. This ensures that this case works when the other named
1911 // type is defined in another package and relies on calling equality
1912 // functions defined only in that package.
1915 Type::write_named_equal(Gogo
* gogo
, Named_type
* name
)
1917 Location bloc
= Linemap::predeclared_location();
1919 // The pointers to the types we are going to compare. These have
1920 // type unsafe.Pointer.
1921 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
1922 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
1923 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
1925 Named_type
* base_type
= name
->real_type()->named_type();
1926 go_assert(base_type
!= NULL
);
1928 // Build temporaries with the base type.
1929 Type
* pt
= Type::make_pointer_type(base_type
);
1931 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
1932 ref
= Expression::make_cast(pt
, ref
, bloc
);
1933 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
1934 gogo
->add_statement(p1
);
1936 ref
= Expression::make_var_reference(key2_arg
, bloc
);
1937 ref
= Expression::make_cast(pt
, ref
, bloc
);
1938 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
1939 gogo
->add_statement(p2
);
1941 // Compare the values for equality.
1942 Expression
* t1
= Expression::make_temporary_reference(p1
, bloc
);
1943 t1
= Expression::make_unary(OPERATOR_MULT
, t1
, bloc
);
1945 Expression
* t2
= Expression::make_temporary_reference(p2
, bloc
);
1946 t2
= Expression::make_unary(OPERATOR_MULT
, t2
, bloc
);
1948 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, t1
, t2
, bloc
);
1950 // Return the equality comparison.
1951 Expression_list
* vals
= new Expression_list();
1952 vals
->push_back(cond
);
1953 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
1954 gogo
->add_statement(s
);
1957 // Return a composite literal for the type descriptor for a plain type
1958 // of kind RUNTIME_TYPE_KIND named NAME.
1961 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1962 Named_type
* name
, const Methods
* methods
,
1963 bool only_value_methods
)
1965 Location bloc
= Linemap::predeclared_location();
1967 Type
* td_type
= Type::make_type_descriptor_type();
1968 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1970 Expression_list
* vals
= new Expression_list();
1973 if (!this->has_pointer())
1974 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
1975 Struct_field_list::const_iterator p
= fields
->begin();
1976 go_assert(p
->is_field_name("Kind"));
1978 mpz_init_set_ui(iv
, runtime_type_kind
);
1979 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1982 go_assert(p
->is_field_name("align"));
1983 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1984 vals
->push_back(Expression::make_type_info(this, type_info
));
1987 go_assert(p
->is_field_name("fieldAlign"));
1988 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1989 vals
->push_back(Expression::make_type_info(this, type_info
));
1992 go_assert(p
->is_field_name("size"));
1993 type_info
= Expression::TYPE_INFO_SIZE
;
1994 vals
->push_back(Expression::make_type_info(this, type_info
));
1997 go_assert(p
->is_field_name("hash"));
2000 h
= name
->hash_for_method(gogo
);
2002 h
= this->hash_for_method(gogo
);
2004 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
2007 go_assert(p
->is_field_name("hashfn"));
2008 Function_type
* hash_fntype
= p
->type()->function_type();
2011 go_assert(p
->is_field_name("equalfn"));
2012 Function_type
* equal_fntype
= p
->type()->function_type();
2014 Named_object
* hash_fn
;
2015 Named_object
* equal_fn
;
2016 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
2018 vals
->push_back(Expression::make_func_code_reference(hash_fn
, bloc
));
2019 vals
->push_back(Expression::make_func_code_reference(equal_fn
, bloc
));
2022 go_assert(p
->is_field_name("string"));
2023 Expression
* s
= Expression::make_string((name
!= NULL
2024 ? name
->reflection(gogo
)
2025 : this->reflection(gogo
)),
2027 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2030 go_assert(p
->is_field_name("uncommonType"));
2031 if (name
== NULL
&& methods
== NULL
)
2032 vals
->push_back(Expression::make_nil(bloc
));
2035 if (methods
== NULL
)
2036 methods
= name
->methods();
2037 vals
->push_back(this->uncommon_type_constructor(gogo
,
2040 only_value_methods
));
2044 go_assert(p
->is_field_name("ptrToThis"));
2046 vals
->push_back(Expression::make_nil(bloc
));
2049 Type
* pt
= Type::make_pointer_type(name
);
2050 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
2054 go_assert(p
->is_field_name("zero"));
2055 Expression
* z
= Expression::make_var_reference(gogo
->zero_value(this), bloc
);
2056 z
= Expression::make_unary(OPERATOR_AND
, z
, bloc
);
2057 Type
* void_type
= Type::make_void_type();
2058 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
2059 z
= Expression::make_cast(unsafe_pointer_type
, z
, bloc
);
2063 go_assert(p
== fields
->end());
2067 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
2070 // Return a composite literal for the uncommon type information for
2071 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
2072 // struct. If name is not NULL, it is the name of the type. If
2073 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
2074 // is true if only value methods should be included. At least one of
2075 // NAME and METHODS must not be NULL.
2078 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
2079 Named_type
* name
, const Methods
* methods
,
2080 bool only_value_methods
) const
2082 Location bloc
= Linemap::predeclared_location();
2084 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
2086 Expression_list
* vals
= new Expression_list();
2089 Struct_field_list::const_iterator p
= fields
->begin();
2090 go_assert(p
->is_field_name("name"));
2093 go_assert(p
->is_field_name("pkgPath"));
2097 vals
->push_back(Expression::make_nil(bloc
));
2098 vals
->push_back(Expression::make_nil(bloc
));
2102 Named_object
* no
= name
->named_object();
2103 std::string n
= Gogo::unpack_hidden_name(no
->name());
2104 Expression
* s
= Expression::make_string(n
, bloc
);
2105 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2107 if (name
->is_builtin())
2108 vals
->push_back(Expression::make_nil(bloc
));
2111 const Package
* package
= no
->package();
2112 const std::string
& pkgpath(package
== NULL
2114 : package
->pkgpath());
2117 const Named_object
* in_function
= name
->in_function(&index
);
2118 if (in_function
!= NULL
)
2121 n
.append(Gogo::unpack_hidden_name(in_function
->name()));
2125 snprintf(buf
, sizeof buf
, "%u", index
);
2130 s
= Expression::make_string(n
, bloc
);
2131 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2136 go_assert(p
->is_field_name("methods"));
2137 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
2138 only_value_methods
));
2141 go_assert(p
== fields
->end());
2143 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
2145 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
2148 // Sort methods by name.
2154 operator()(const std::pair
<std::string
, const Method
*>& m1
,
2155 const std::pair
<std::string
, const Method
*>& m2
) const
2156 { return m1
.first
< m2
.first
; }
2159 // Return a composite literal for the type method table for this type.
2160 // METHODS_TYPE is the type of the table, and is a slice type.
2161 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
2162 // then only value methods are used.
2165 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
2166 const Methods
* methods
,
2167 bool only_value_methods
) const
2169 Location bloc
= Linemap::predeclared_location();
2171 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
2172 if (methods
!= NULL
)
2174 smethods
.reserve(methods
->count());
2175 for (Methods::const_iterator p
= methods
->begin();
2176 p
!= methods
->end();
2179 if (p
->second
->is_ambiguous())
2181 if (only_value_methods
&& !p
->second
->is_value_method())
2184 // This is where we implement the magic //go:nointerface
2185 // comment. If we saw that comment, we don't add this
2186 // method to the type descriptor.
2187 if (p
->second
->nointerface())
2190 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
2194 if (smethods
.empty())
2195 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
2197 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
2199 Type
* method_type
= methods_type
->array_type()->element_type();
2201 Expression_list
* vals
= new Expression_list();
2202 vals
->reserve(smethods
.size());
2203 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
2205 p
!= smethods
.end();
2207 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
2208 p
->second
, only_value_methods
));
2210 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
2213 // Return a composite literal for a single method. METHOD_TYPE is the
2214 // type of the entry. METHOD_NAME is the name of the method and M is
2215 // the method information.
2218 Type::method_constructor(Gogo
*, Type
* method_type
,
2219 const std::string
& method_name
,
2221 bool only_value_methods
) const
2223 Location bloc
= Linemap::predeclared_location();
2225 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
2227 Expression_list
* vals
= new Expression_list();
2230 Struct_field_list::const_iterator p
= fields
->begin();
2231 go_assert(p
->is_field_name("name"));
2232 const std::string n
= Gogo::unpack_hidden_name(method_name
);
2233 Expression
* s
= Expression::make_string(n
, bloc
);
2234 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2237 go_assert(p
->is_field_name("pkgPath"));
2238 if (!Gogo::is_hidden_name(method_name
))
2239 vals
->push_back(Expression::make_nil(bloc
));
2242 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
2244 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2247 Named_object
* no
= (m
->needs_stub_method()
2249 : m
->named_object());
2251 Function_type
* mtype
;
2252 if (no
->is_function())
2253 mtype
= no
->func_value()->type();
2255 mtype
= no
->func_declaration_value()->type();
2256 go_assert(mtype
->is_method());
2257 Type
* nonmethod_type
= mtype
->copy_without_receiver();
2260 go_assert(p
->is_field_name("mtyp"));
2261 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
2264 go_assert(p
->is_field_name("typ"));
2265 bool want_pointer_receiver
= !only_value_methods
&& m
->is_value_method();
2266 nonmethod_type
= mtype
->copy_with_receiver_as_param(want_pointer_receiver
);
2267 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
2270 go_assert(p
->is_field_name("tfn"));
2271 vals
->push_back(Expression::make_func_code_reference(no
, bloc
));
2274 go_assert(p
== fields
->end());
2276 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
2279 // Return a composite literal for the type descriptor of a plain type.
2280 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
2281 // NULL, it is the name to use as well as the list of methods.
2284 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
2287 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
2291 // Return the type reflection string for this type.
2294 Type::reflection(Gogo
* gogo
) const
2298 // The do_reflection virtual function should set RET to the
2299 // reflection string.
2300 this->do_reflection(gogo
, &ret
);
2305 // Return a mangled name for the type.
2308 Type::mangled_name(Gogo
* gogo
) const
2312 // The do_mangled_name virtual function should set RET to the
2313 // mangled name. For a composite type it should append a code for
2314 // the composition and then call do_mangled_name on the components.
2315 this->do_mangled_name(gogo
, &ret
);
2320 // Return whether the backend size of the type is known.
2323 Type::is_backend_type_size_known(Gogo
* gogo
)
2325 switch (this->classification_
)
2339 case TYPE_INTERFACE
:
2344 const Struct_field_list
* fields
= this->struct_type()->fields();
2345 for (Struct_field_list::const_iterator pf
= fields
->begin();
2346 pf
!= fields
->end();
2348 if (!pf
->type()->is_backend_type_size_known(gogo
))
2355 const Array_type
* at
= this->array_type();
2356 if (at
->length() == NULL
)
2360 Numeric_constant nc
;
2361 if (!at
->length()->numeric_constant_value(&nc
))
2364 if (!nc
.to_int(&ival
))
2367 return at
->element_type()->is_backend_type_size_known(gogo
);
2372 this->named_type()->convert(gogo
);
2373 return this->named_type()->is_named_backend_type_size_known();
2377 Forward_declaration_type
* fdt
= this->forward_declaration_type();
2378 return fdt
->real_type()->is_backend_type_size_known(gogo
);
2382 case TYPE_CALL_MULTIPLE_RESULT
:
2390 // If the size of the type can be determined, set *PSIZE to the size
2391 // in bytes and return true. Otherwise, return false. This queries
2395 Type::backend_type_size(Gogo
* gogo
, unsigned long *psize
)
2397 if (!this->is_backend_type_size_known(gogo
))
2399 Btype
* bt
= this->get_backend_placeholder(gogo
);
2400 size_t size
= gogo
->backend()->type_size(bt
);
2401 *psize
= static_cast<unsigned long>(size
);
2407 // If the alignment of the type can be determined, set *PALIGN to
2408 // the alignment in bytes and return true. Otherwise, return false.
2411 Type::backend_type_align(Gogo
* gogo
, unsigned long *palign
)
2413 if (!this->is_backend_type_size_known(gogo
))
2415 Btype
* bt
= this->get_backend_placeholder(gogo
);
2416 size_t align
= gogo
->backend()->type_alignment(bt
);
2417 *palign
= static_cast<unsigned long>(align
);
2418 if (*palign
!= align
)
2423 // Like backend_type_align, but return the alignment when used as a
2427 Type::backend_type_field_align(Gogo
* gogo
, unsigned long *palign
)
2429 if (!this->is_backend_type_size_known(gogo
))
2431 Btype
* bt
= this->get_backend_placeholder(gogo
);
2432 size_t a
= gogo
->backend()->type_field_alignment(bt
);
2433 *palign
= static_cast<unsigned long>(a
);
2439 // Default function to export a type.
2442 Type::do_export(Export
*) const
2450 Type::import_type(Import
* imp
)
2452 if (imp
->match_c_string("("))
2453 return Function_type::do_import(imp
);
2454 else if (imp
->match_c_string("*"))
2455 return Pointer_type::do_import(imp
);
2456 else if (imp
->match_c_string("struct "))
2457 return Struct_type::do_import(imp
);
2458 else if (imp
->match_c_string("["))
2459 return Array_type::do_import(imp
);
2460 else if (imp
->match_c_string("map "))
2461 return Map_type::do_import(imp
);
2462 else if (imp
->match_c_string("chan "))
2463 return Channel_type::do_import(imp
);
2464 else if (imp
->match_c_string("interface"))
2465 return Interface_type::do_import(imp
);
2468 error_at(imp
->location(), "import error: expected type");
2469 return Type::make_error_type();
2473 // A type used to indicate a parsing error. This exists to simplify
2474 // later error detection.
2476 class Error_type
: public Type
2485 do_compare_is_identity(Gogo
*)
2489 do_get_backend(Gogo
* gogo
)
2490 { return gogo
->backend()->error_type(); }
2493 do_type_descriptor(Gogo
*, Named_type
*)
2494 { return Expression::make_error(Linemap::predeclared_location()); }
2497 do_reflection(Gogo
*, std::string
*) const
2498 { go_assert(saw_errors()); }
2501 do_mangled_name(Gogo
*, std::string
* ret
) const
2502 { ret
->push_back('E'); }
2506 Type::make_error_type()
2508 static Error_type singleton_error_type
;
2509 return &singleton_error_type
;
2514 class Void_type
: public Type
2523 do_compare_is_identity(Gogo
*)
2527 do_get_backend(Gogo
* gogo
)
2528 { return gogo
->backend()->void_type(); }
2531 do_type_descriptor(Gogo
*, Named_type
*)
2532 { go_unreachable(); }
2535 do_reflection(Gogo
*, std::string
*) const
2539 do_mangled_name(Gogo
*, std::string
* ret
) const
2540 { ret
->push_back('v'); }
2544 Type::make_void_type()
2546 static Void_type singleton_void_type
;
2547 return &singleton_void_type
;
2550 // The boolean type.
2552 class Boolean_type
: public Type
2556 : Type(TYPE_BOOLEAN
)
2561 do_compare_is_identity(Gogo
*)
2565 do_get_backend(Gogo
* gogo
)
2566 { return gogo
->backend()->bool_type(); }
2569 do_type_descriptor(Gogo
*, Named_type
* name
);
2571 // We should not be asked for the reflection string of a basic type.
2573 do_reflection(Gogo
*, std::string
* ret
) const
2574 { ret
->append("bool"); }
2577 do_mangled_name(Gogo
*, std::string
* ret
) const
2578 { ret
->push_back('b'); }
2581 // Make the type descriptor.
2584 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2587 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
2590 Named_object
* no
= gogo
->lookup_global("bool");
2591 go_assert(no
!= NULL
);
2592 return Type::type_descriptor(gogo
, no
->type_value());
2597 Type::make_boolean_type()
2599 static Boolean_type boolean_type
;
2600 return &boolean_type
;
2603 // The named type "bool".
2605 static Named_type
* named_bool_type
;
2607 // Get the named type "bool".
2610 Type::lookup_bool_type()
2612 return named_bool_type
;
2615 // Make the named type "bool".
2618 Type::make_named_bool_type()
2620 Type
* bool_type
= Type::make_boolean_type();
2621 Named_object
* named_object
=
2622 Named_object::make_type("bool", NULL
, bool_type
,
2623 Linemap::predeclared_location());
2624 Named_type
* named_type
= named_object
->type_value();
2625 named_bool_type
= named_type
;
2629 // Class Integer_type.
2631 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
2633 // Create a new integer type. Non-abstract integer types always have
2637 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
2638 int bits
, int runtime_type_kind
)
2640 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
2642 std::string
sname(name
);
2643 Named_object
* named_object
=
2644 Named_object::make_type(sname
, NULL
, integer_type
,
2645 Linemap::predeclared_location());
2646 Named_type
* named_type
= named_object
->type_value();
2647 std::pair
<Named_integer_types::iterator
, bool> ins
=
2648 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
2649 go_assert(ins
.second
);
2653 // Look up an existing integer type.
2656 Integer_type::lookup_integer_type(const char* name
)
2658 Named_integer_types::const_iterator p
=
2659 Integer_type::named_integer_types
.find(name
);
2660 go_assert(p
!= Integer_type::named_integer_types
.end());
2664 // Create a new abstract integer type.
2667 Integer_type::create_abstract_integer_type()
2669 static Integer_type
* abstract_type
;
2670 if (abstract_type
== NULL
)
2672 Type
* int_type
= Type::lookup_integer_type("int");
2673 abstract_type
= new Integer_type(true, false,
2674 int_type
->integer_type()->bits(),
2675 RUNTIME_TYPE_KIND_INT
);
2677 return abstract_type
;
2680 // Create a new abstract character type.
2683 Integer_type::create_abstract_character_type()
2685 static Integer_type
* abstract_type
;
2686 if (abstract_type
== NULL
)
2688 abstract_type
= new Integer_type(true, false, 32,
2689 RUNTIME_TYPE_KIND_INT32
);
2690 abstract_type
->set_is_rune();
2692 return abstract_type
;
2695 // Integer type compatibility.
2698 Integer_type::is_identical(const Integer_type
* t
) const
2700 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
2702 return this->is_abstract_
== t
->is_abstract_
;
2708 Integer_type::do_hash_for_method(Gogo
*) const
2710 return ((this->bits_
<< 4)
2711 + ((this->is_unsigned_
? 1 : 0) << 8)
2712 + ((this->is_abstract_
? 1 : 0) << 9));
2715 // Convert an Integer_type to the backend representation.
2718 Integer_type::do_get_backend(Gogo
* gogo
)
2720 if (this->is_abstract_
)
2722 go_assert(saw_errors());
2723 return gogo
->backend()->error_type();
2725 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
2728 // The type descriptor for an integer type. Integer types are always
2732 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2734 go_assert(name
!= NULL
|| saw_errors());
2735 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2738 // We should not be asked for the reflection string of a basic type.
2741 Integer_type::do_reflection(Gogo
*, std::string
*) const
2743 go_assert(saw_errors());
2749 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2752 snprintf(buf
, sizeof buf
, "i%s%s%de",
2753 this->is_abstract_
? "a" : "",
2754 this->is_unsigned_
? "u" : "",
2759 // Make an integer type.
2762 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
2763 int runtime_type_kind
)
2765 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
2769 // Make an abstract integer type.
2772 Type::make_abstract_integer_type()
2774 return Integer_type::create_abstract_integer_type();
2777 // Make an abstract character type.
2780 Type::make_abstract_character_type()
2782 return Integer_type::create_abstract_character_type();
2785 // Look up an integer type.
2788 Type::lookup_integer_type(const char* name
)
2790 return Integer_type::lookup_integer_type(name
);
2793 // Class Float_type.
2795 Float_type::Named_float_types
Float_type::named_float_types
;
2797 // Create a new float type. Non-abstract float types always have
2801 Float_type::create_float_type(const char* name
, int bits
,
2802 int runtime_type_kind
)
2804 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
2805 std::string
sname(name
);
2806 Named_object
* named_object
=
2807 Named_object::make_type(sname
, NULL
, float_type
,
2808 Linemap::predeclared_location());
2809 Named_type
* named_type
= named_object
->type_value();
2810 std::pair
<Named_float_types::iterator
, bool> ins
=
2811 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
2812 go_assert(ins
.second
);
2816 // Look up an existing float type.
2819 Float_type::lookup_float_type(const char* name
)
2821 Named_float_types::const_iterator p
=
2822 Float_type::named_float_types
.find(name
);
2823 go_assert(p
!= Float_type::named_float_types
.end());
2827 // Create a new abstract float type.
2830 Float_type::create_abstract_float_type()
2832 static Float_type
* abstract_type
;
2833 if (abstract_type
== NULL
)
2834 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
2835 return abstract_type
;
2838 // Whether this type is identical with T.
2841 Float_type::is_identical(const Float_type
* t
) const
2843 if (this->bits_
!= t
->bits_
)
2845 return this->is_abstract_
== t
->is_abstract_
;
2851 Float_type::do_hash_for_method(Gogo
*) const
2853 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2856 // Convert to the backend representation.
2859 Float_type::do_get_backend(Gogo
* gogo
)
2861 return gogo
->backend()->float_type(this->bits_
);
2864 // The type descriptor for a float type. Float types are always named.
2867 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2869 go_assert(name
!= NULL
|| saw_errors());
2870 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2873 // We should not be asked for the reflection string of a basic type.
2876 Float_type::do_reflection(Gogo
*, std::string
*) const
2878 go_assert(saw_errors());
2884 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2887 snprintf(buf
, sizeof buf
, "f%s%de",
2888 this->is_abstract_
? "a" : "",
2893 // Make a floating point type.
2896 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
2898 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
2901 // Make an abstract float type.
2904 Type::make_abstract_float_type()
2906 return Float_type::create_abstract_float_type();
2909 // Look up a float type.
2912 Type::lookup_float_type(const char* name
)
2914 return Float_type::lookup_float_type(name
);
2917 // Class Complex_type.
2919 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
2921 // Create a new complex type. Non-abstract complex types always have
2925 Complex_type::create_complex_type(const char* name
, int bits
,
2926 int runtime_type_kind
)
2928 Complex_type
* complex_type
= new Complex_type(false, bits
,
2930 std::string
sname(name
);
2931 Named_object
* named_object
=
2932 Named_object::make_type(sname
, NULL
, complex_type
,
2933 Linemap::predeclared_location());
2934 Named_type
* named_type
= named_object
->type_value();
2935 std::pair
<Named_complex_types::iterator
, bool> ins
=
2936 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
2938 go_assert(ins
.second
);
2942 // Look up an existing complex type.
2945 Complex_type::lookup_complex_type(const char* name
)
2947 Named_complex_types::const_iterator p
=
2948 Complex_type::named_complex_types
.find(name
);
2949 go_assert(p
!= Complex_type::named_complex_types
.end());
2953 // Create a new abstract complex type.
2956 Complex_type::create_abstract_complex_type()
2958 static Complex_type
* abstract_type
;
2959 if (abstract_type
== NULL
)
2960 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
2961 return abstract_type
;
2964 // Whether this type is identical with T.
2967 Complex_type::is_identical(const Complex_type
*t
) const
2969 if (this->bits_
!= t
->bits_
)
2971 return this->is_abstract_
== t
->is_abstract_
;
2977 Complex_type::do_hash_for_method(Gogo
*) const
2979 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2982 // Convert to the backend representation.
2985 Complex_type::do_get_backend(Gogo
* gogo
)
2987 return gogo
->backend()->complex_type(this->bits_
);
2990 // The type descriptor for a complex type. Complex types are always
2994 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2996 go_assert(name
!= NULL
|| saw_errors());
2997 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
3000 // We should not be asked for the reflection string of a basic type.
3003 Complex_type::do_reflection(Gogo
*, std::string
*) const
3005 go_assert(saw_errors());
3011 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
3014 snprintf(buf
, sizeof buf
, "c%s%de",
3015 this->is_abstract_
? "a" : "",
3020 // Make a complex type.
3023 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
3025 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
3028 // Make an abstract complex type.
3031 Type::make_abstract_complex_type()
3033 return Complex_type::create_abstract_complex_type();
3036 // Look up a complex type.
3039 Type::lookup_complex_type(const char* name
)
3041 return Complex_type::lookup_complex_type(name
);
3044 // Class String_type.
3046 // Convert String_type to the backend representation. A string is a
3047 // struct with two fields: a pointer to the characters and a length.
3050 String_type::do_get_backend(Gogo
* gogo
)
3052 static Btype
* backend_string_type
;
3053 if (backend_string_type
== NULL
)
3055 std::vector
<Backend::Btyped_identifier
> fields(2);
3057 Type
* b
= gogo
->lookup_global("byte")->type_value();
3058 Type
* pb
= Type::make_pointer_type(b
);
3060 // We aren't going to get back to this field to finish the
3061 // backend representation, so force it to be finished now.
3062 if (!gogo
->named_types_are_converted())
3064 Btype
* bt
= pb
->get_backend_placeholder(gogo
);
3065 pb
->finish_backend(gogo
, bt
);
3068 fields
[0].name
= "__data";
3069 fields
[0].btype
= pb
->get_backend(gogo
);
3070 fields
[0].location
= Linemap::predeclared_location();
3072 Type
* int_type
= Type::lookup_integer_type("int");
3073 fields
[1].name
= "__length";
3074 fields
[1].btype
= int_type
->get_backend(gogo
);
3075 fields
[1].location
= fields
[0].location
;
3077 backend_string_type
= gogo
->backend()->struct_type(fields
);
3079 return backend_string_type
;
3082 // The type descriptor for the string type.
3085 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3088 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
3091 Named_object
* no
= gogo
->lookup_global("string");
3092 go_assert(no
!= NULL
);
3093 return Type::type_descriptor(gogo
, no
->type_value());
3097 // We should not be asked for the reflection string of a basic type.
3100 String_type::do_reflection(Gogo
*, std::string
* ret
) const
3102 ret
->append("string");
3105 // Mangled name of a string type.
3108 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
3110 ret
->push_back('z');
3113 // Make a string type.
3116 Type::make_string_type()
3118 static String_type string_type
;
3119 return &string_type
;
3122 // The named type "string".
3124 static Named_type
* named_string_type
;
3126 // Get the named type "string".
3129 Type::lookup_string_type()
3131 return named_string_type
;
3134 // Make the named type string.
3137 Type::make_named_string_type()
3139 Type
* string_type
= Type::make_string_type();
3140 Named_object
* named_object
=
3141 Named_object::make_type("string", NULL
, string_type
,
3142 Linemap::predeclared_location());
3143 Named_type
* named_type
= named_object
->type_value();
3144 named_string_type
= named_type
;
3148 // The sink type. This is the type of the blank identifier _. Any
3149 // type may be assigned to it.
3151 class Sink_type
: public Type
3160 do_compare_is_identity(Gogo
*)
3164 do_get_backend(Gogo
*)
3165 { go_unreachable(); }
3168 do_type_descriptor(Gogo
*, Named_type
*)
3169 { go_unreachable(); }
3172 do_reflection(Gogo
*, std::string
*) const
3173 { go_unreachable(); }
3176 do_mangled_name(Gogo
*, std::string
*) const
3177 { go_unreachable(); }
3180 // Make the sink type.
3183 Type::make_sink_type()
3185 static Sink_type sink_type
;
3189 // Class Function_type.
3194 Function_type::do_traverse(Traverse
* traverse
)
3196 if (this->receiver_
!= NULL
3197 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
3198 return TRAVERSE_EXIT
;
3199 if (this->parameters_
!= NULL
3200 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
3201 return TRAVERSE_EXIT
;
3202 if (this->results_
!= NULL
3203 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
3204 return TRAVERSE_EXIT
;
3205 return TRAVERSE_CONTINUE
;
3208 // Returns whether T is a valid redeclaration of this type. If this
3209 // returns false, and REASON is not NULL, *REASON may be set to a
3210 // brief explanation of why it returned false.
3213 Function_type::is_valid_redeclaration(const Function_type
* t
,
3214 std::string
* reason
) const
3216 if (!this->is_identical(t
, false, true, reason
))
3219 // A redeclaration of a function is required to use the same names
3220 // for the receiver and parameters.
3221 if (this->receiver() != NULL
3222 && this->receiver()->name() != t
->receiver()->name())
3225 *reason
= "receiver name changed";
3229 const Typed_identifier_list
* parms1
= this->parameters();
3230 const Typed_identifier_list
* parms2
= t
->parameters();
3233 Typed_identifier_list::const_iterator p1
= parms1
->begin();
3234 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
3235 p2
!= parms2
->end();
3238 if (p1
->name() != p2
->name())
3241 *reason
= "parameter name changed";
3245 // This is called at parse time, so we may have unknown
3247 Type
* t1
= p1
->type()->forwarded();
3248 Type
* t2
= p2
->type()->forwarded();
3250 && t1
->forward_declaration_type() != NULL
3251 && (t2
->forward_declaration_type() == NULL
3252 || (t1
->forward_declaration_type()->named_object()
3253 != t2
->forward_declaration_type()->named_object())))
3258 const Typed_identifier_list
* results1
= this->results();
3259 const Typed_identifier_list
* results2
= t
->results();
3260 if (results1
!= NULL
)
3262 Typed_identifier_list::const_iterator res1
= results1
->begin();
3263 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
3264 res2
!= results2
->end();
3267 if (res1
->name() != res2
->name())
3270 *reason
= "result name changed";
3274 // This is called at parse time, so we may have unknown
3276 Type
* t1
= res1
->type()->forwarded();
3277 Type
* t2
= res2
->type()->forwarded();
3279 && t1
->forward_declaration_type() != NULL
3280 && (t2
->forward_declaration_type() == NULL
3281 || (t1
->forward_declaration_type()->named_object()
3282 != t2
->forward_declaration_type()->named_object())))
3290 // Check whether T is the same as this type.
3293 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
3294 bool errors_are_identical
,
3295 std::string
* reason
) const
3297 if (!ignore_receiver
)
3299 const Typed_identifier
* r1
= this->receiver();
3300 const Typed_identifier
* r2
= t
->receiver();
3301 if ((r1
!= NULL
) != (r2
!= NULL
))
3304 *reason
= _("different receiver types");
3309 if (!Type::are_identical(r1
->type(), r2
->type(), errors_are_identical
,
3312 if (reason
!= NULL
&& !reason
->empty())
3313 *reason
= "receiver: " + *reason
;
3319 const Typed_identifier_list
* parms1
= this->parameters();
3320 const Typed_identifier_list
* parms2
= t
->parameters();
3321 if ((parms1
!= NULL
) != (parms2
!= NULL
))
3324 *reason
= _("different number of parameters");
3329 Typed_identifier_list::const_iterator p1
= parms1
->begin();
3330 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
3331 p2
!= parms2
->end();
3334 if (p1
== parms1
->end())
3337 *reason
= _("different number of parameters");
3341 if (!Type::are_identical(p1
->type(), p2
->type(),
3342 errors_are_identical
, NULL
))
3345 *reason
= _("different parameter types");
3349 if (p1
!= parms1
->end())
3352 *reason
= _("different number of parameters");
3357 if (this->is_varargs() != t
->is_varargs())
3360 *reason
= _("different varargs");
3364 const Typed_identifier_list
* results1
= this->results();
3365 const Typed_identifier_list
* results2
= t
->results();
3366 if ((results1
!= NULL
) != (results2
!= NULL
))
3369 *reason
= _("different number of results");
3372 if (results1
!= NULL
)
3374 Typed_identifier_list::const_iterator res1
= results1
->begin();
3375 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
3376 res2
!= results2
->end();
3379 if (res1
== results1
->end())
3382 *reason
= _("different number of results");
3386 if (!Type::are_identical(res1
->type(), res2
->type(),
3387 errors_are_identical
, NULL
))
3390 *reason
= _("different result types");
3394 if (res1
!= results1
->end())
3397 *reason
= _("different number of results");
3408 Function_type::do_hash_for_method(Gogo
* gogo
) const
3410 unsigned int ret
= 0;
3411 // We ignore the receiver type for hash codes, because we need to
3412 // get the same hash code for a method in an interface and a method
3413 // declared for a type. The former will not have a receiver.
3414 if (this->parameters_
!= NULL
)
3417 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
3418 p
!= this->parameters_
->end();
3420 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
3422 if (this->results_
!= NULL
)
3425 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
3426 p
!= this->results_
->end();
3428 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
3430 if (this->is_varargs_
)
3436 // Hash result parameters.
3439 Function_type::Results_hash::operator()(const Typed_identifier_list
* t
) const
3441 unsigned int hash
= 0;
3442 for (Typed_identifier_list::const_iterator p
= t
->begin();
3447 hash
= Type::hash_string(p
->name(), hash
);
3448 hash
+= p
->type()->hash_for_method(NULL
);
3453 // Compare result parameters so that can map identical result
3454 // parameters to a single struct type.
3457 Function_type::Results_equal::operator()(const Typed_identifier_list
* a
,
3458 const Typed_identifier_list
* b
) const
3460 if (a
->size() != b
->size())
3462 Typed_identifier_list::const_iterator pa
= a
->begin();
3463 for (Typed_identifier_list::const_iterator pb
= b
->begin();
3467 if (pa
->name() != pb
->name()
3468 || !Type::are_identical(pa
->type(), pb
->type(), true, NULL
))
3474 // Hash from results to a backend struct type.
3476 Function_type::Results_structs
Function_type::results_structs
;
3478 // Get the backend representation for a function type.
3481 Function_type::get_backend_fntype(Gogo
* gogo
)
3483 if (this->fnbtype_
== NULL
)
3485 Backend::Btyped_identifier breceiver
;
3486 if (this->receiver_
!= NULL
)
3488 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
3490 // We always pass the address of the receiver parameter, in
3491 // order to make interface calls work with unknown types.
3492 Type
* rtype
= this->receiver_
->type();
3493 if (rtype
->points_to() == NULL
)
3494 rtype
= Type::make_pointer_type(rtype
);
3495 breceiver
.btype
= rtype
->get_backend(gogo
);
3496 breceiver
.location
= this->receiver_
->location();
3499 std::vector
<Backend::Btyped_identifier
> bparameters
;
3500 if (this->parameters_
!= NULL
)
3502 bparameters
.resize(this->parameters_
->size());
3504 for (Typed_identifier_list::const_iterator p
=
3505 this->parameters_
->begin(); p
!= this->parameters_
->end();
3508 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
3509 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
3510 bparameters
[i
].location
= p
->location();
3512 go_assert(i
== bparameters
.size());
3515 std::vector
<Backend::Btyped_identifier
> bresults
;
3516 Btype
* bresult_struct
= NULL
;
3517 if (this->results_
!= NULL
)
3519 bresults
.resize(this->results_
->size());
3521 for (Typed_identifier_list::const_iterator p
=
3522 this->results_
->begin();
3523 p
!= this->results_
->end();
3526 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
3527 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
3528 bresults
[i
].location
= p
->location();
3530 go_assert(i
== bresults
.size());
3532 if (this->results_
->size() > 1)
3534 // Use the same results struct for all functions that
3535 // return the same set of results. This is useful to
3536 // unify calls to interface methods with other calls.
3537 std::pair
<Typed_identifier_list
*, Btype
*> val
;
3538 val
.first
= this->results_
;
3540 std::pair
<Results_structs::iterator
, bool> ins
=
3541 Function_type::results_structs
.insert(val
);
3544 // Build a new struct type.
3545 Struct_field_list
* sfl
= new Struct_field_list
;
3546 for (Typed_identifier_list::const_iterator p
=
3547 this->results_
->begin();
3548 p
!= this->results_
->end();
3551 Typed_identifier tid
= *p
;
3552 if (tid
.name().empty())
3553 tid
= Typed_identifier("UNNAMED", tid
.type(),
3555 sfl
->push_back(Struct_field(tid
));
3557 Struct_type
* st
= Type::make_struct_type(sfl
,
3559 ins
.first
->second
= st
->get_backend(gogo
);
3561 bresult_struct
= ins
.first
->second
;
3565 this->fnbtype_
= gogo
->backend()->function_type(breceiver
, bparameters
,
3566 bresults
, bresult_struct
,
3571 return this->fnbtype_
;
3574 // Get the backend representation for a Go function type.
3577 Function_type::do_get_backend(Gogo
* gogo
)
3579 // When we do anything with a function value other than call it, it
3580 // is represented as a pointer to a struct whose first field is the
3581 // actual function. So that is what we return as the type of a Go
3584 Location loc
= this->location();
3585 Btype
* struct_type
=
3586 gogo
->backend()->placeholder_struct_type("__go_descriptor", loc
);
3587 Btype
* ptr_struct_type
= gogo
->backend()->pointer_type(struct_type
);
3589 std::vector
<Backend::Btyped_identifier
> fields(1);
3590 fields
[0].name
= "code";
3591 fields
[0].btype
= this->get_backend_fntype(gogo
);
3592 fields
[0].location
= loc
;
3593 if (!gogo
->backend()->set_placeholder_struct_type(struct_type
, fields
))
3594 return gogo
->backend()->error_type();
3595 return ptr_struct_type
;
3598 // The type of a function type descriptor.
3601 Function_type::make_function_type_descriptor_type()
3606 Type
* tdt
= Type::make_type_descriptor_type();
3607 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3609 Type
* bool_type
= Type::lookup_bool_type();
3611 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
3613 Struct_type
* s
= Type::make_builtin_struct_type(4,
3615 "dotdotdot", bool_type
,
3619 ret
= Type::make_builtin_named_type("FuncType", s
);
3625 // The type descriptor for a function type.
3628 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3630 Location bloc
= Linemap::predeclared_location();
3632 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
3634 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
3636 Expression_list
* vals
= new Expression_list();
3639 Struct_field_list::const_iterator p
= fields
->begin();
3640 go_assert(p
->is_field_name("commonType"));
3641 vals
->push_back(this->type_descriptor_constructor(gogo
,
3642 RUNTIME_TYPE_KIND_FUNC
,
3646 go_assert(p
->is_field_name("dotdotdot"));
3647 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
3650 go_assert(p
->is_field_name("in"));
3651 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
3652 this->parameters()));
3655 go_assert(p
->is_field_name("out"));
3656 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
3660 go_assert(p
== fields
->end());
3662 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
3665 // Return a composite literal for the parameters or results of a type
3669 Function_type::type_descriptor_params(Type
* params_type
,
3670 const Typed_identifier
* receiver
,
3671 const Typed_identifier_list
* params
)
3673 Location bloc
= Linemap::predeclared_location();
3675 if (receiver
== NULL
&& params
== NULL
)
3676 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
3678 Expression_list
* vals
= new Expression_list();
3679 vals
->reserve((params
== NULL
? 0 : params
->size())
3680 + (receiver
!= NULL
? 1 : 0));
3682 if (receiver
!= NULL
)
3683 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
3687 for (Typed_identifier_list::const_iterator p
= params
->begin();
3690 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
3693 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
3696 // The reflection string.
3699 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3701 // FIXME: Turn this off until we straighten out the type of the
3702 // struct field used in a go statement which calls a method.
3703 // go_assert(this->receiver_ == NULL);
3705 ret
->append("func");
3707 if (this->receiver_
!= NULL
)
3709 ret
->push_back('(');
3710 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
3711 ret
->push_back(')');
3714 ret
->push_back('(');
3715 const Typed_identifier_list
* params
= this->parameters();
3718 bool is_varargs
= this->is_varargs_
;
3719 for (Typed_identifier_list::const_iterator p
= params
->begin();
3723 if (p
!= params
->begin())
3725 if (!is_varargs
|| p
+ 1 != params
->end())
3726 this->append_reflection(p
->type(), gogo
, ret
);
3730 this->append_reflection(p
->type()->array_type()->element_type(),
3735 ret
->push_back(')');
3737 const Typed_identifier_list
* results
= this->results();
3738 if (results
!= NULL
&& !results
->empty())
3740 if (results
->size() == 1)
3741 ret
->push_back(' ');
3744 for (Typed_identifier_list::const_iterator p
= results
->begin();
3745 p
!= results
->end();
3748 if (p
!= results
->begin())
3750 this->append_reflection(p
->type(), gogo
, ret
);
3752 if (results
->size() > 1)
3753 ret
->push_back(')');
3760 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3762 ret
->push_back('F');
3764 if (this->receiver_
!= NULL
)
3766 ret
->push_back('m');
3767 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
3770 const Typed_identifier_list
* params
= this->parameters();
3773 ret
->push_back('p');
3774 for (Typed_identifier_list::const_iterator p
= params
->begin();
3777 this->append_mangled_name(p
->type(), gogo
, ret
);
3778 if (this->is_varargs_
)
3779 ret
->push_back('V');
3780 ret
->push_back('e');
3783 const Typed_identifier_list
* results
= this->results();
3784 if (results
!= NULL
)
3786 ret
->push_back('r');
3787 for (Typed_identifier_list::const_iterator p
= results
->begin();
3788 p
!= results
->end();
3790 this->append_mangled_name(p
->type(), gogo
, ret
);
3791 ret
->push_back('e');
3794 ret
->push_back('e');
3797 // Export a function type.
3800 Function_type::do_export(Export
* exp
) const
3802 // We don't write out the receiver. The only function types which
3803 // should have a receiver are the ones associated with explicitly
3804 // defined methods. For those the receiver type is written out by
3805 // Function::export_func.
3807 exp
->write_c_string("(");
3809 if (this->parameters_
!= NULL
)
3811 bool is_varargs
= this->is_varargs_
;
3812 for (Typed_identifier_list::const_iterator p
=
3813 this->parameters_
->begin();
3814 p
!= this->parameters_
->end();
3820 exp
->write_c_string(", ");
3821 exp
->write_name(p
->name());
3822 exp
->write_c_string(" ");
3823 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
3824 exp
->write_type(p
->type());
3827 exp
->write_c_string("...");
3828 exp
->write_type(p
->type()->array_type()->element_type());
3832 exp
->write_c_string(")");
3834 const Typed_identifier_list
* results
= this->results_
;
3835 if (results
!= NULL
)
3837 exp
->write_c_string(" ");
3838 if (results
->size() == 1 && results
->begin()->name().empty())
3839 exp
->write_type(results
->begin()->type());
3843 exp
->write_c_string("(");
3844 for (Typed_identifier_list::const_iterator p
= results
->begin();
3845 p
!= results
->end();
3851 exp
->write_c_string(", ");
3852 exp
->write_name(p
->name());
3853 exp
->write_c_string(" ");
3854 exp
->write_type(p
->type());
3856 exp
->write_c_string(")");
3861 // Import a function type.
3864 Function_type::do_import(Import
* imp
)
3866 imp
->require_c_string("(");
3867 Typed_identifier_list
* parameters
;
3868 bool is_varargs
= false;
3869 if (imp
->peek_char() == ')')
3873 parameters
= new Typed_identifier_list();
3876 std::string name
= imp
->read_name();
3877 imp
->require_c_string(" ");
3879 if (imp
->match_c_string("..."))
3885 Type
* ptype
= imp
->read_type();
3887 ptype
= Type::make_array_type(ptype
, NULL
);
3888 parameters
->push_back(Typed_identifier(name
, ptype
,
3890 if (imp
->peek_char() != ',')
3892 go_assert(!is_varargs
);
3893 imp
->require_c_string(", ");
3896 imp
->require_c_string(")");
3898 Typed_identifier_list
* results
;
3899 if (imp
->peek_char() != ' ')
3904 results
= new Typed_identifier_list
;
3905 if (imp
->peek_char() != '(')
3907 Type
* rtype
= imp
->read_type();
3908 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
3915 std::string name
= imp
->read_name();
3916 imp
->require_c_string(" ");
3917 Type
* rtype
= imp
->read_type();
3918 results
->push_back(Typed_identifier(name
, rtype
,
3920 if (imp
->peek_char() != ',')
3922 imp
->require_c_string(", ");
3924 imp
->require_c_string(")");
3928 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
3931 ret
->set_is_varargs();
3935 // Make a copy of a function type without a receiver.
3938 Function_type::copy_without_receiver() const
3940 go_assert(this->is_method());
3941 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
3944 if (this->is_varargs())
3945 ret
->set_is_varargs();
3946 if (this->is_builtin())
3947 ret
->set_is_builtin();
3951 // Make a copy of a function type with a receiver.
3954 Function_type::copy_with_receiver(Type
* receiver_type
) const
3956 go_assert(!this->is_method());
3957 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
3959 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
3962 if (this->is_varargs_
)
3963 ret
->set_is_varargs();
3967 // Make a copy of a function type with the receiver as the first
3971 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver
) const
3973 go_assert(this->is_method());
3974 Typed_identifier_list
* new_params
= new Typed_identifier_list();
3975 Type
* rtype
= this->receiver_
->type();
3976 if (want_pointer_receiver
)
3977 rtype
= Type::make_pointer_type(rtype
);
3978 Typed_identifier
receiver(this->receiver_
->name(), rtype
,
3979 this->receiver_
->location());
3980 new_params
->push_back(receiver
);
3981 const Typed_identifier_list
* orig_params
= this->parameters_
;
3982 if (orig_params
!= NULL
&& !orig_params
->empty())
3984 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
3985 p
!= orig_params
->end();
3987 new_params
->push_back(*p
);
3989 return Type::make_function_type(NULL
, new_params
, this->results_
,
3993 // Make a copy of a function type ignoring any receiver and adding a
3994 // closure parameter.
3997 Function_type::copy_with_names() const
3999 Typed_identifier_list
* new_params
= new Typed_identifier_list();
4000 const Typed_identifier_list
* orig_params
= this->parameters_
;
4001 if (orig_params
!= NULL
&& !orig_params
->empty())
4005 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
4006 p
!= orig_params
->end();
4009 snprintf(buf
, sizeof buf
, "pt.%u", count
);
4011 new_params
->push_back(Typed_identifier(buf
, p
->type(),
4016 const Typed_identifier_list
* orig_results
= this->results_
;
4017 Typed_identifier_list
* new_results
;
4018 if (orig_results
== NULL
|| orig_results
->empty())
4022 new_results
= new Typed_identifier_list();
4023 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
4024 p
!= orig_results
->end();
4026 new_results
->push_back(Typed_identifier("", p
->type(),
4030 return Type::make_function_type(NULL
, new_params
, new_results
,
4034 // Make a function type.
4037 Type::make_function_type(Typed_identifier
* receiver
,
4038 Typed_identifier_list
* parameters
,
4039 Typed_identifier_list
* results
,
4042 return new Function_type(receiver
, parameters
, results
, location
);
4045 // Make a backend function type.
4047 Backend_function_type
*
4048 Type::make_backend_function_type(Typed_identifier
* receiver
,
4049 Typed_identifier_list
* parameters
,
4050 Typed_identifier_list
* results
,
4053 return new Backend_function_type(receiver
, parameters
, results
, location
);
4056 // Class Pointer_type.
4061 Pointer_type::do_traverse(Traverse
* traverse
)
4063 return Type::traverse(this->to_type_
, traverse
);
4069 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
4071 return this->to_type_
->hash_for_method(gogo
) << 4;
4074 // Get the backend representation for a pointer type.
4077 Pointer_type::do_get_backend(Gogo
* gogo
)
4079 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
4080 return gogo
->backend()->pointer_type(to_btype
);
4083 // The type of a pointer type descriptor.
4086 Pointer_type::make_pointer_type_descriptor_type()
4091 Type
* tdt
= Type::make_type_descriptor_type();
4092 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4094 Struct_type
* s
= Type::make_builtin_struct_type(2,
4098 ret
= Type::make_builtin_named_type("PtrType", s
);
4104 // The type descriptor for a pointer type.
4107 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4109 if (this->is_unsafe_pointer_type())
4111 go_assert(name
!= NULL
);
4112 return this->plain_type_descriptor(gogo
,
4113 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
4118 Location bloc
= Linemap::predeclared_location();
4120 const Methods
* methods
;
4121 Type
* deref
= this->points_to();
4122 if (deref
->named_type() != NULL
)
4123 methods
= deref
->named_type()->methods();
4124 else if (deref
->struct_type() != NULL
)
4125 methods
= deref
->struct_type()->methods();
4129 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
4131 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
4133 Expression_list
* vals
= new Expression_list();
4136 Struct_field_list::const_iterator p
= fields
->begin();
4137 go_assert(p
->is_field_name("commonType"));
4138 vals
->push_back(this->type_descriptor_constructor(gogo
,
4139 RUNTIME_TYPE_KIND_PTR
,
4140 name
, methods
, false));
4143 go_assert(p
->is_field_name("elem"));
4144 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
4146 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
4150 // Reflection string.
4153 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4155 ret
->push_back('*');
4156 this->append_reflection(this->to_type_
, gogo
, ret
);
4162 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4164 ret
->push_back('p');
4165 this->append_mangled_name(this->to_type_
, gogo
, ret
);
4171 Pointer_type::do_export(Export
* exp
) const
4173 exp
->write_c_string("*");
4174 if (this->is_unsafe_pointer_type())
4175 exp
->write_c_string("any");
4177 exp
->write_type(this->to_type_
);
4183 Pointer_type::do_import(Import
* imp
)
4185 imp
->require_c_string("*");
4186 if (imp
->match_c_string("any"))
4189 return Type::make_pointer_type(Type::make_void_type());
4191 Type
* to
= imp
->read_type();
4192 return Type::make_pointer_type(to
);
4195 // Make a pointer type.
4198 Type::make_pointer_type(Type
* to_type
)
4200 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
4201 static Hashtable pointer_types
;
4202 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
4203 if (p
!= pointer_types
.end())
4205 Pointer_type
* ret
= new Pointer_type(to_type
);
4206 pointer_types
[to_type
] = ret
;
4210 // The nil type. We use a special type for nil because it is not the
4211 // same as any other type. In C term nil has type void*, but there is
4212 // no such type in Go.
4214 class Nil_type
: public Type
4223 do_compare_is_identity(Gogo
*)
4227 do_get_backend(Gogo
* gogo
)
4228 { return gogo
->backend()->pointer_type(gogo
->backend()->void_type()); }
4231 do_type_descriptor(Gogo
*, Named_type
*)
4232 { go_unreachable(); }
4235 do_reflection(Gogo
*, std::string
*) const
4236 { go_unreachable(); }
4239 do_mangled_name(Gogo
*, std::string
* ret
) const
4240 { ret
->push_back('n'); }
4243 // Make the nil type.
4246 Type::make_nil_type()
4248 static Nil_type singleton_nil_type
;
4249 return &singleton_nil_type
;
4252 // The type of a function call which returns multiple values. This is
4253 // really a struct, but we don't want to confuse a function call which
4254 // returns a struct with a function call which returns multiple
4257 class Call_multiple_result_type
: public Type
4260 Call_multiple_result_type(Call_expression
* call
)
4261 : Type(TYPE_CALL_MULTIPLE_RESULT
),
4267 do_has_pointer() const
4269 go_assert(saw_errors());
4274 do_compare_is_identity(Gogo
*)
4278 do_get_backend(Gogo
* gogo
)
4280 go_assert(saw_errors());
4281 return gogo
->backend()->error_type();
4285 do_type_descriptor(Gogo
*, Named_type
*)
4287 go_assert(saw_errors());
4288 return Expression::make_error(Linemap::unknown_location());
4292 do_reflection(Gogo
*, std::string
*) const
4293 { go_assert(saw_errors()); }
4296 do_mangled_name(Gogo
*, std::string
*) const
4297 { go_assert(saw_errors()); }
4300 // The expression being called.
4301 Call_expression
* call_
;
4304 // Make a call result type.
4307 Type::make_call_multiple_result_type(Call_expression
* call
)
4309 return new Call_multiple_result_type(call
);
4312 // Class Struct_field.
4314 // Get the name of a field.
4317 Struct_field::field_name() const
4319 const std::string
& name(this->typed_identifier_
.name());
4324 // This is called during parsing, before anything is lowered, so
4325 // we have to be pretty careful to avoid dereferencing an
4326 // unknown type name.
4327 Type
* t
= this->typed_identifier_
.type();
4329 if (t
->classification() == Type::TYPE_POINTER
)
4332 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
4333 dt
= ptype
->points_to();
4335 if (dt
->forward_declaration_type() != NULL
)
4336 return dt
->forward_declaration_type()->name();
4337 else if (dt
->named_type() != NULL
)
4338 return dt
->named_type()->name();
4339 else if (t
->is_error_type() || dt
->is_error_type())
4341 static const std::string error_string
= "*error*";
4342 return error_string
;
4346 // Avoid crashing in the erroneous case where T is named but
4349 if (t
->forward_declaration_type() != NULL
)
4350 return t
->forward_declaration_type()->name();
4351 else if (t
->named_type() != NULL
)
4352 return t
->named_type()->name();
4359 // Return whether this field is named NAME.
4362 Struct_field::is_field_name(const std::string
& name
) const
4364 const std::string
& me(this->typed_identifier_
.name());
4369 Type
* t
= this->typed_identifier_
.type();
4370 if (t
->points_to() != NULL
)
4372 Named_type
* nt
= t
->named_type();
4373 if (nt
!= NULL
&& nt
->name() == name
)
4376 // This is a horrible hack caused by the fact that we don't pack
4377 // the names of builtin types. FIXME.
4378 if (!this->is_imported_
4381 && nt
->name() == Gogo::unpack_hidden_name(name
))
4388 // Return whether this field is an unexported field named NAME.
4391 Struct_field::is_unexported_field_name(Gogo
* gogo
,
4392 const std::string
& name
) const
4394 const std::string
& field_name(this->field_name());
4395 if (Gogo::is_hidden_name(field_name
)
4396 && name
== Gogo::unpack_hidden_name(field_name
)
4397 && gogo
->pack_hidden_name(name
, false) != field_name
)
4400 // Check for the name of a builtin type. This is like the test in
4401 // is_field_name, only there we return false if this->is_imported_,
4402 // and here we return true.
4403 if (this->is_imported_
&& this->is_anonymous())
4405 Type
* t
= this->typed_identifier_
.type();
4406 if (t
->points_to() != NULL
)
4408 Named_type
* nt
= t
->named_type();
4411 && nt
->name() == Gogo::unpack_hidden_name(name
))
4418 // Return whether this field is an embedded built-in type.
4421 Struct_field::is_embedded_builtin(Gogo
* gogo
) const
4423 const std::string
& name(this->field_name());
4424 // We know that a field is an embedded type if it is anonymous.
4425 // We can decide if it is a built-in type by checking to see if it is
4426 // registered globally under the field's name.
4427 // This allows us to distinguish between embedded built-in types and
4428 // embedded types that are aliases to built-in types.
4429 return (this->is_anonymous()
4430 && !Gogo::is_hidden_name(name
)
4431 && gogo
->lookup_global(name
.c_str()) != NULL
);
4434 // Class Struct_type.
4436 // A hash table used to find identical unnamed structs so that they
4437 // share method tables.
4439 Struct_type::Identical_structs
Struct_type::identical_structs
;
4441 // A hash table used to merge method sets for identical unnamed
4444 Struct_type::Struct_method_tables
Struct_type::struct_method_tables
;
4449 Struct_type::do_traverse(Traverse
* traverse
)
4451 Struct_field_list
* fields
= this->fields_
;
4454 for (Struct_field_list::iterator p
= fields
->begin();
4458 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
4459 return TRAVERSE_EXIT
;
4462 return TRAVERSE_CONTINUE
;
4465 // Verify that the struct type is complete and valid.
4468 Struct_type::do_verify()
4470 Struct_field_list
* fields
= this->fields_
;
4473 for (Struct_field_list::iterator p
= fields
->begin();
4477 Type
* t
= p
->type();
4478 if (p
->is_anonymous())
4480 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
4482 error_at(p
->location(), "embedded type may not be a pointer");
4483 p
->set_type(Type::make_error_type());
4485 else if (t
->points_to() != NULL
4486 && t
->points_to()->interface_type() != NULL
)
4488 error_at(p
->location(),
4489 "embedded type may not be pointer to interface");
4490 p
->set_type(Type::make_error_type());
4497 // Whether this contains a pointer.
4500 Struct_type::do_has_pointer() const
4502 const Struct_field_list
* fields
= this->fields();
4505 for (Struct_field_list::const_iterator p
= fields
->begin();
4509 if (p
->type()->has_pointer())
4515 // Whether this type is identical to T.
4518 Struct_type::is_identical(const Struct_type
* t
,
4519 bool errors_are_identical
) const
4521 const Struct_field_list
* fields1
= this->fields();
4522 const Struct_field_list
* fields2
= t
->fields();
4523 if (fields1
== NULL
|| fields2
== NULL
)
4524 return fields1
== fields2
;
4525 Struct_field_list::const_iterator pf2
= fields2
->begin();
4526 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
4527 pf1
!= fields1
->end();
4530 if (pf2
== fields2
->end())
4532 if (pf1
->field_name() != pf2
->field_name())
4534 if (pf1
->is_anonymous() != pf2
->is_anonymous()
4535 || !Type::are_identical(pf1
->type(), pf2
->type(),
4536 errors_are_identical
, NULL
))
4538 if (!pf1
->has_tag())
4545 if (!pf2
->has_tag())
4547 if (pf1
->tag() != pf2
->tag())
4551 if (pf2
!= fields2
->end())
4556 // Whether this struct type has any hidden fields.
4559 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
4560 std::string
* reason
) const
4562 const Struct_field_list
* fields
= this->fields();
4565 const Package
* within_package
= (within
== NULL
4567 : within
->named_object()->package());
4568 for (Struct_field_list::const_iterator pf
= fields
->begin();
4569 pf
!= fields
->end();
4572 if (within_package
!= NULL
4573 && !pf
->is_anonymous()
4574 && Gogo::is_hidden_name(pf
->field_name()))
4578 std::string within_name
= within
->named_object()->message_name();
4579 std::string name
= Gogo::message_name(pf
->field_name());
4580 size_t bufsize
= 200 + within_name
.length() + name
.length();
4581 char* buf
= new char[bufsize
];
4582 snprintf(buf
, bufsize
,
4583 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4584 open_quote
, within_name
.c_str(), close_quote
,
4585 open_quote
, name
.c_str(), close_quote
);
4586 reason
->assign(buf
);
4592 if (pf
->type()->has_hidden_fields(within
, reason
))
4599 // Whether comparisons of this struct type are simple identity
4603 Struct_type::do_compare_is_identity(Gogo
* gogo
)
4605 const Struct_field_list
* fields
= this->fields_
;
4608 unsigned long offset
= 0;
4609 for (Struct_field_list::const_iterator pf
= fields
->begin();
4610 pf
!= fields
->end();
4613 if (Gogo::is_sink_name(pf
->field_name()))
4616 if (!pf
->type()->compare_is_identity(gogo
))
4619 unsigned long field_align
;
4620 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
4622 if ((offset
& (field_align
- 1)) != 0)
4624 // This struct has padding. We don't guarantee that that
4625 // padding is zero-initialized for a stack variable, so we
4626 // can't use memcmp to compare struct values.
4630 unsigned long field_size
;
4631 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
4633 offset
+= field_size
;
4636 unsigned long struct_size
;
4637 if (!this->backend_type_size(gogo
, &struct_size
))
4639 if (offset
!= struct_size
)
4641 // Trailing padding may not be zero when on the stack.
4648 // Build identity and hash functions for this struct.
4653 Struct_type::do_hash_for_method(Gogo
* gogo
) const
4655 unsigned int ret
= 0;
4656 if (this->fields() != NULL
)
4658 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
4659 pf
!= this->fields()->end();
4661 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
4666 // Find the local field NAME.
4669 Struct_type::find_local_field(const std::string
& name
,
4670 unsigned int *pindex
) const
4672 const Struct_field_list
* fields
= this->fields_
;
4676 for (Struct_field_list::const_iterator pf
= fields
->begin();
4677 pf
!= fields
->end();
4680 if (pf
->is_field_name(name
))
4690 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4692 Field_reference_expression
*
4693 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
4694 Location location
) const
4697 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
4701 // Return an expression for a field, along with the depth at which it
4704 Field_reference_expression
*
4705 Struct_type::field_reference_depth(Expression
* struct_expr
,
4706 const std::string
& name
,
4708 Saw_named_type
* saw
,
4709 unsigned int* depth
) const
4711 const Struct_field_list
* fields
= this->fields_
;
4715 // Look for a field with this name.
4717 for (Struct_field_list::const_iterator pf
= fields
->begin();
4718 pf
!= fields
->end();
4721 if (pf
->is_field_name(name
))
4724 return Expression::make_field_reference(struct_expr
, i
, location
);
4728 // Look for an anonymous field which contains a field with this
4730 unsigned int found_depth
= 0;
4731 Field_reference_expression
* ret
= NULL
;
4733 for (Struct_field_list::const_iterator pf
= fields
->begin();
4734 pf
!= fields
->end();
4737 if (!pf
->is_anonymous())
4740 Struct_type
* st
= pf
->type()->deref()->struct_type();
4744 Saw_named_type
* hold_saw
= saw
;
4745 Saw_named_type saw_here
;
4746 Named_type
* nt
= pf
->type()->named_type();
4748 nt
= pf
->type()->deref()->named_type();
4752 for (q
= saw
; q
!= NULL
; q
= q
->next
)
4756 // If this is an error, it will be reported
4763 saw_here
.next
= saw
;
4768 // Look for a reference using a NULL struct expression. If we
4769 // find one, fill in the struct expression with a reference to
4771 unsigned int subdepth
;
4772 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
4782 if (ret
== NULL
|| subdepth
< found_depth
)
4787 found_depth
= subdepth
;
4788 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
4790 if (pf
->type()->points_to() != NULL
)
4791 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
4792 while (sub
->expr() != NULL
)
4794 sub
= sub
->expr()->deref()->field_reference_expression();
4795 go_assert(sub
!= NULL
);
4797 sub
->set_struct_expression(here
);
4798 sub
->set_implicit(true);
4800 else if (subdepth
> found_depth
)
4804 // We do not handle ambiguity here--it should be handled by
4805 // Type::bind_field_or_method.
4813 *depth
= found_depth
+ 1;
4818 // Return the total number of fields, including embedded fields.
4821 Struct_type::total_field_count() const
4823 if (this->fields_
== NULL
)
4825 unsigned int ret
= 0;
4826 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
4827 pf
!= this->fields_
->end();
4830 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
4833 ret
+= pf
->type()->struct_type()->total_field_count();
4838 // Return whether NAME is an unexported field, for better error reporting.
4841 Struct_type::is_unexported_local_field(Gogo
* gogo
,
4842 const std::string
& name
) const
4844 const Struct_field_list
* fields
= this->fields_
;
4847 for (Struct_field_list::const_iterator pf
= fields
->begin();
4848 pf
!= fields
->end();
4850 if (pf
->is_unexported_field_name(gogo
, name
))
4856 // Finalize the methods of an unnamed struct.
4859 Struct_type::finalize_methods(Gogo
* gogo
)
4861 if (this->all_methods_
!= NULL
)
4864 // It is possible to have multiple identical structs that have
4865 // methods. We want them to share method tables. Otherwise we will
4866 // emit identical methods more than once, which is bad since they
4867 // will even have the same names.
4868 std::pair
<Identical_structs::iterator
, bool> ins
=
4869 Struct_type::identical_structs
.insert(std::make_pair(this, this));
4872 // An identical struct was already entered into the hash table.
4873 // Note that finalize_methods is, fortunately, not recursive.
4874 this->all_methods_
= ins
.first
->second
->all_methods_
;
4878 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
4881 // Return the method NAME, or NULL if there isn't one or if it is
4882 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
4886 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
4888 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
4891 // Return a pointer to the interface method table for this type for
4892 // the interface INTERFACE. IS_POINTER is true if this is for a
4896 Struct_type::interface_method_table(Interface_type
* interface
,
4899 std::pair
<Struct_type
*, Struct_type::Struct_method_table_pair
*>
4901 std::pair
<Struct_type::Struct_method_tables::iterator
, bool> ins
=
4902 Struct_type::struct_method_tables
.insert(val
);
4904 Struct_method_table_pair
* smtp
;
4906 smtp
= ins
.first
->second
;
4909 smtp
= new Struct_method_table_pair();
4911 smtp
->second
= NULL
;
4912 ins
.first
->second
= smtp
;
4915 return Type::interface_method_table(this, interface
, is_pointer
,
4916 &smtp
->first
, &smtp
->second
);
4919 // Convert struct fields to the backend representation. This is not
4920 // declared in types.h so that types.h doesn't have to #include
4924 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
4925 bool use_placeholder
,
4926 std::vector
<Backend::Btyped_identifier
>* bfields
)
4928 bfields
->resize(fields
->size());
4930 for (Struct_field_list::const_iterator p
= fields
->begin();
4934 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
4935 (*bfields
)[i
].btype
= (use_placeholder
4936 ? p
->type()->get_backend_placeholder(gogo
)
4937 : p
->type()->get_backend(gogo
));
4938 (*bfields
)[i
].location
= p
->location();
4940 go_assert(i
== fields
->size());
4943 // Get the backend representation for a struct type.
4946 Struct_type::do_get_backend(Gogo
* gogo
)
4948 std::vector
<Backend::Btyped_identifier
> bfields
;
4949 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
4950 return gogo
->backend()->struct_type(bfields
);
4953 // Finish the backend representation of the fields of a struct.
4956 Struct_type::finish_backend_fields(Gogo
* gogo
)
4958 const Struct_field_list
* fields
= this->fields_
;
4961 for (Struct_field_list::const_iterator p
= fields
->begin();
4964 p
->type()->get_backend(gogo
);
4968 // The type of a struct type descriptor.
4971 Struct_type::make_struct_type_descriptor_type()
4976 Type
* tdt
= Type::make_type_descriptor_type();
4977 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4979 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4980 Type
* string_type
= Type::lookup_string_type();
4981 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
4984 Type::make_builtin_struct_type(5,
4985 "name", pointer_string_type
,
4986 "pkgPath", pointer_string_type
,
4988 "tag", pointer_string_type
,
4989 "offset", uintptr_type
);
4990 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
4992 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
4994 Struct_type
* s
= Type::make_builtin_struct_type(2,
4996 "fields", slice_type
);
4998 ret
= Type::make_builtin_named_type("StructType", s
);
5004 // Build a type descriptor for a struct type.
5007 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5009 Location bloc
= Linemap::predeclared_location();
5011 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
5013 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
5015 Expression_list
* vals
= new Expression_list();
5018 const Methods
* methods
= this->methods();
5019 // A named struct should not have methods--the methods should attach
5020 // to the named type.
5021 go_assert(methods
== NULL
|| name
== NULL
);
5023 Struct_field_list::const_iterator ps
= fields
->begin();
5024 go_assert(ps
->is_field_name("commonType"));
5025 vals
->push_back(this->type_descriptor_constructor(gogo
,
5026 RUNTIME_TYPE_KIND_STRUCT
,
5027 name
, methods
, true));
5030 go_assert(ps
->is_field_name("fields"));
5032 Expression_list
* elements
= new Expression_list();
5033 elements
->reserve(this->fields_
->size());
5034 Type
* element_type
= ps
->type()->array_type()->element_type();
5035 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
5036 pf
!= this->fields_
->end();
5039 const Struct_field_list
* f
= element_type
->struct_type()->fields();
5041 Expression_list
* fvals
= new Expression_list();
5044 Struct_field_list::const_iterator q
= f
->begin();
5045 go_assert(q
->is_field_name("name"));
5046 if (pf
->is_anonymous())
5047 fvals
->push_back(Expression::make_nil(bloc
));
5050 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
5051 Expression
* s
= Expression::make_string(n
, bloc
);
5052 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
5056 go_assert(q
->is_field_name("pkgPath"));
5057 bool is_embedded_builtin
= pf
->is_embedded_builtin(gogo
);
5058 if (!Gogo::is_hidden_name(pf
->field_name()) && !is_embedded_builtin
)
5059 fvals
->push_back(Expression::make_nil(bloc
));
5063 if (is_embedded_builtin
)
5064 n
= gogo
->package_name();
5066 n
= Gogo::hidden_name_pkgpath(pf
->field_name());
5067 Expression
* s
= Expression::make_string(n
, bloc
);
5068 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
5072 go_assert(q
->is_field_name("typ"));
5073 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
5076 go_assert(q
->is_field_name("tag"));
5078 fvals
->push_back(Expression::make_nil(bloc
));
5081 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
5082 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
5086 go_assert(q
->is_field_name("offset"));
5087 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
5089 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
5091 elements
->push_back(v
);
5094 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
5097 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
5100 // Write the hash function for a struct which can not use the identity
5104 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
5105 Function_type
* hash_fntype
,
5106 Function_type
* equal_fntype
)
5108 Location bloc
= Linemap::predeclared_location();
5110 // The pointer to the struct that we are going to hash. This is an
5111 // argument to the hash function we are implementing here.
5112 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
5113 go_assert(key_arg
!= NULL
);
5114 Type
* key_arg_type
= key_arg
->var_value()->type();
5116 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5120 mpz_init_set_ui(ival
, 0);
5121 Expression
* zero
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
5124 // Make a temporary to hold the return value, initialized to 0.
5125 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
5127 gogo
->add_statement(retval
);
5129 // Make a temporary to hold the key as a uintptr.
5130 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
5131 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
5132 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
5134 gogo
->add_statement(key
);
5136 // Loop over the struct fields.
5138 const Struct_field_list
* fields
= this->fields_
;
5139 for (Struct_field_list::const_iterator pf
= fields
->begin();
5140 pf
!= fields
->end();
5143 if (Gogo::is_sink_name(pf
->field_name()))
5150 // Multiply retval by 33.
5151 mpz_init_set_ui(ival
, 33);
5152 Expression
* i33
= Expression::make_integer(&ival
, uintptr_type
,
5156 ref
= Expression::make_temporary_reference(retval
, bloc
);
5157 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
,
5159 gogo
->add_statement(s
);
5162 // Get a pointer to the value of this field.
5163 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
5164 ref
= Expression::make_temporary_reference(key
, bloc
);
5165 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
5167 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
5169 // Get the size of this field.
5170 Expression
* size
= Expression::make_type_info(pf
->type(),
5171 Expression::TYPE_INFO_SIZE
);
5173 // Get the hash function to use for the type of this field.
5174 Named_object
* hash_fn
;
5175 Named_object
* equal_fn
;
5176 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
5177 equal_fntype
, &hash_fn
, &equal_fn
);
5179 // Call the hash function for the field.
5180 Expression_list
* args
= new Expression_list();
5181 args
->push_back(subkey
);
5182 args
->push_back(size
);
5183 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
5184 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
5186 // Add the field's hash value to retval.
5187 Temporary_reference_expression
* tref
=
5188 Expression::make_temporary_reference(retval
, bloc
);
5189 tref
->set_is_lvalue();
5190 Statement
* s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
,
5192 gogo
->add_statement(s
);
5195 // Return retval to the caller of the hash function.
5196 Expression_list
* vals
= new Expression_list();
5197 ref
= Expression::make_temporary_reference(retval
, bloc
);
5198 vals
->push_back(ref
);
5199 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5200 gogo
->add_statement(s
);
5203 // Write the equality function for a struct which can not use the
5204 // identity function.
5207 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
5209 Location bloc
= Linemap::predeclared_location();
5211 // The pointers to the structs we are going to compare.
5212 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
5213 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
5214 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
5216 // Build temporaries with the right types.
5217 Type
* pt
= Type::make_pointer_type(name
!= NULL
5218 ? static_cast<Type
*>(name
)
5219 : static_cast<Type
*>(this));
5221 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
5222 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5223 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
5224 gogo
->add_statement(p1
);
5226 ref
= Expression::make_var_reference(key2_arg
, bloc
);
5227 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5228 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
5229 gogo
->add_statement(p2
);
5231 const Struct_field_list
* fields
= this->fields_
;
5232 unsigned int field_index
= 0;
5233 for (Struct_field_list::const_iterator pf
= fields
->begin();
5234 pf
!= fields
->end();
5235 ++pf
, ++field_index
)
5237 if (Gogo::is_sink_name(pf
->field_name()))
5240 // Compare one field in both P1 and P2.
5241 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
5242 f1
= Expression::make_unary(OPERATOR_MULT
, f1
, bloc
);
5243 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
5245 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
5246 f2
= Expression::make_unary(OPERATOR_MULT
, f2
, bloc
);
5247 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
5249 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
5251 // If the values are not equal, return false.
5252 gogo
->start_block(bloc
);
5253 Expression_list
* vals
= new Expression_list();
5254 vals
->push_back(Expression::make_boolean(false, bloc
));
5255 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5256 gogo
->add_statement(s
);
5257 Block
* then_block
= gogo
->finish_block(bloc
);
5259 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
5260 gogo
->add_statement(s
);
5263 // All the fields are equal, so return true.
5264 Expression_list
* vals
= new Expression_list();
5265 vals
->push_back(Expression::make_boolean(true, bloc
));
5266 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5267 gogo
->add_statement(s
);
5270 // Reflection string.
5273 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5275 ret
->append("struct {");
5277 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
5278 p
!= this->fields_
->end();
5281 if (p
!= this->fields_
->begin())
5282 ret
->push_back(';');
5283 ret
->push_back(' ');
5284 if (p
->is_anonymous())
5285 ret
->push_back('?');
5287 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
5288 ret
->push_back(' ');
5289 this->append_reflection(p
->type(), gogo
, ret
);
5293 const std::string
& tag(p
->tag());
5295 for (std::string::const_iterator p
= tag
.begin();
5300 ret
->append("\\x00");
5301 else if (*p
== '\n')
5303 else if (*p
== '\t')
5306 ret
->append("\\\"");
5307 else if (*p
== '\\')
5308 ret
->append("\\\\");
5312 ret
->push_back('"');
5316 if (!this->fields_
->empty())
5317 ret
->push_back(' ');
5319 ret
->push_back('}');
5325 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5327 ret
->push_back('S');
5329 const Struct_field_list
* fields
= this->fields_
;
5332 for (Struct_field_list::const_iterator p
= fields
->begin();
5336 if (p
->is_anonymous())
5340 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
5342 snprintf(buf
, sizeof buf
, "%u_",
5343 static_cast<unsigned int>(n
.length()));
5347 this->append_mangled_name(p
->type(), gogo
, ret
);
5350 const std::string
& tag(p
->tag());
5352 for (std::string::const_iterator p
= tag
.begin();
5356 if (ISALNUM(*p
) || *p
== '_')
5361 snprintf(buf
, sizeof buf
, ".%x.",
5362 static_cast<unsigned int>(*p
));
5367 snprintf(buf
, sizeof buf
, "T%u_",
5368 static_cast<unsigned int>(out
.length()));
5375 ret
->push_back('e');
5378 // If the offset of field INDEX in the backend implementation can be
5379 // determined, set *POFFSET to the offset in bytes and return true.
5380 // Otherwise, return false.
5383 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
5384 unsigned int* poffset
)
5386 if (!this->is_backend_type_size_known(gogo
))
5388 Btype
* bt
= this->get_backend_placeholder(gogo
);
5389 size_t offset
= gogo
->backend()->type_field_offset(bt
, index
);
5390 *poffset
= static_cast<unsigned int>(offset
);
5391 if (*poffset
!= offset
)
5399 Struct_type::do_export(Export
* exp
) const
5401 exp
->write_c_string("struct { ");
5402 const Struct_field_list
* fields
= this->fields_
;
5403 go_assert(fields
!= NULL
);
5404 for (Struct_field_list::const_iterator p
= fields
->begin();
5408 if (p
->is_anonymous())
5409 exp
->write_string("? ");
5412 exp
->write_string(p
->field_name());
5413 exp
->write_c_string(" ");
5415 exp
->write_type(p
->type());
5419 exp
->write_c_string(" ");
5421 Expression::make_string(p
->tag(), Linemap::predeclared_location());
5422 expr
->export_expression(exp
);
5426 exp
->write_c_string("; ");
5428 exp
->write_c_string("}");
5434 Struct_type::do_import(Import
* imp
)
5436 imp
->require_c_string("struct { ");
5437 Struct_field_list
* fields
= new Struct_field_list
;
5438 if (imp
->peek_char() != '}')
5443 if (imp
->match_c_string("? "))
5447 name
= imp
->read_identifier();
5448 imp
->require_c_string(" ");
5450 Type
* ftype
= imp
->read_type();
5452 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
5453 sf
.set_is_imported();
5455 if (imp
->peek_char() == ' ')
5458 Expression
* expr
= Expression::import_expression(imp
);
5459 String_expression
* sexpr
= expr
->string_expression();
5460 go_assert(sexpr
!= NULL
);
5461 sf
.set_tag(sexpr
->val());
5465 imp
->require_c_string("; ");
5466 fields
->push_back(sf
);
5467 if (imp
->peek_char() == '}')
5471 imp
->require_c_string("}");
5473 return Type::make_struct_type(fields
, imp
->location());
5476 // Make a struct type.
5479 Type::make_struct_type(Struct_field_list
* fields
,
5482 return new Struct_type(fields
, location
);
5485 // Class Array_type.
5487 // Whether two array types are identical.
5490 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
5492 if (!Type::are_identical(this->element_type(), t
->element_type(),
5493 errors_are_identical
, NULL
))
5496 Expression
* l1
= this->length();
5497 Expression
* l2
= t
->length();
5499 // Slices of the same element type are identical.
5500 if (l1
== NULL
&& l2
== NULL
)
5503 // Arrays of the same element type are identical if they have the
5505 if (l1
!= NULL
&& l2
!= NULL
)
5510 // Try to determine the lengths. If we can't, assume the arrays
5511 // are not identical.
5513 Numeric_constant nc1
, nc2
;
5514 if (l1
->numeric_constant_value(&nc1
)
5515 && l2
->numeric_constant_value(&nc2
))
5518 if (nc1
.to_int(&v1
))
5521 if (nc2
.to_int(&v2
))
5523 ret
= mpz_cmp(v1
, v2
) == 0;
5532 // Otherwise the arrays are not identical.
5539 Array_type::do_traverse(Traverse
* traverse
)
5541 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
5542 return TRAVERSE_EXIT
;
5543 if (this->length_
!= NULL
5544 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
5545 return TRAVERSE_EXIT
;
5546 return TRAVERSE_CONTINUE
;
5549 // Check that the length is valid.
5552 Array_type::verify_length()
5554 if (this->length_
== NULL
)
5557 Type_context
context(Type::lookup_integer_type("int"), false);
5558 this->length_
->determine_type(&context
);
5560 if (!this->length_
->is_constant())
5562 error_at(this->length_
->location(), "array bound is not constant");
5566 Numeric_constant nc
;
5567 if (!this->length_
->numeric_constant_value(&nc
))
5569 if (this->length_
->type()->integer_type() != NULL
5570 || this->length_
->type()->float_type() != NULL
)
5571 error_at(this->length_
->location(), "array bound is not constant");
5573 error_at(this->length_
->location(), "array bound is not numeric");
5578 switch (nc
.to_unsigned_long(&val
))
5580 case Numeric_constant::NC_UL_VALID
:
5582 case Numeric_constant::NC_UL_NOTINT
:
5583 error_at(this->length_
->location(), "array bound truncated to integer");
5585 case Numeric_constant::NC_UL_NEGATIVE
:
5586 error_at(this->length_
->location(), "negative array bound");
5588 case Numeric_constant::NC_UL_BIG
:
5589 error_at(this->length_
->location(), "array bound overflows");
5595 Type
* int_type
= Type::lookup_integer_type("int");
5596 unsigned int tbits
= int_type
->integer_type()->bits();
5597 if (sizeof(val
) <= tbits
* 8
5598 && val
>> (tbits
- 1) != 0)
5600 error_at(this->length_
->location(), "array bound overflows");
5610 Array_type::do_verify()
5612 if (!this->verify_length())
5613 this->length_
= Expression::make_error(this->length_
->location());
5617 // Whether we can use memcmp to compare this array.
5620 Array_type::do_compare_is_identity(Gogo
* gogo
)
5622 if (this->length_
== NULL
)
5625 // Check for [...], which indicates that this is not a real type.
5626 if (this->length_
->is_nil_expression())
5629 if (!this->element_type_
->compare_is_identity(gogo
))
5632 // If there is any padding, then we can't use memcmp.
5634 unsigned long align
;
5635 if (!this->element_type_
->backend_type_size(gogo
, &size
)
5636 || !this->element_type_
->backend_type_align(gogo
, &align
))
5638 if ((size
& (align
- 1)) != 0)
5644 // Array type hash code.
5647 Array_type::do_hash_for_method(Gogo
* gogo
) const
5649 // There is no very convenient way to get a hash code for the
5651 return this->element_type_
->hash_for_method(gogo
) + 1;
5654 // Write the hash function for an array which can not use the identify
5658 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
5659 Function_type
* hash_fntype
,
5660 Function_type
* equal_fntype
)
5662 Location bloc
= Linemap::predeclared_location();
5664 // The pointer to the array that we are going to hash. This is an
5665 // argument to the hash function we are implementing here.
5666 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
5667 go_assert(key_arg
!= NULL
);
5668 Type
* key_arg_type
= key_arg
->var_value()->type();
5670 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5674 mpz_init_set_ui(ival
, 0);
5675 Expression
* zero
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
5678 // Make a temporary to hold the return value, initialized to 0.
5679 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
5681 gogo
->add_statement(retval
);
5683 // Make a temporary to hold the key as a uintptr.
5684 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
5685 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
5686 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
5688 gogo
->add_statement(key
);
5690 // Loop over the array elements.
5692 Type
* int_type
= Type::lookup_integer_type("int");
5693 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
5694 gogo
->add_statement(index
);
5696 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
5697 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
5698 Type
* pt
= Type::make_pointer_type(name
!= NULL
5699 ? static_cast<Type
*>(name
)
5700 : static_cast<Type
*>(this));
5701 aref
= Expression::make_cast(pt
, aref
, bloc
);
5702 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
5707 gogo
->start_block(bloc
);
5709 // Multiply retval by 33.
5710 mpz_init_set_ui(ival
, 33);
5711 Expression
* i33
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
5714 ref
= Expression::make_temporary_reference(retval
, bloc
);
5715 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
, ref
,
5717 gogo
->add_statement(s
);
5719 // Get the hash function for the element type.
5720 Named_object
* hash_fn
;
5721 Named_object
* equal_fn
;
5722 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
5723 hash_fntype
, equal_fntype
, &hash_fn
,
5726 // Get a pointer to this element in the loop.
5727 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
5728 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
5730 // Get the size of each element.
5731 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
5732 Expression::TYPE_INFO_SIZE
);
5734 // Get the hash of this element.
5735 Expression_list
* args
= new Expression_list();
5736 args
->push_back(subkey
);
5737 args
->push_back(ele_size
);
5738 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
5739 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
5741 // Add the element's hash value to retval.
5742 Temporary_reference_expression
* tref
=
5743 Expression::make_temporary_reference(retval
, bloc
);
5744 tref
->set_is_lvalue();
5745 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, call
, bloc
);
5746 gogo
->add_statement(s
);
5748 // Increase the element pointer.
5749 tref
= Expression::make_temporary_reference(key
, bloc
);
5750 tref
->set_is_lvalue();
5751 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
5754 Block
* statements
= gogo
->finish_block(bloc
);
5756 for_range
->add_statements(statements
);
5757 gogo
->add_statement(for_range
);
5759 // Return retval to the caller of the hash function.
5760 Expression_list
* vals
= new Expression_list();
5761 ref
= Expression::make_temporary_reference(retval
, bloc
);
5762 vals
->push_back(ref
);
5763 s
= Statement::make_return_statement(vals
, bloc
);
5764 gogo
->add_statement(s
);
5767 // Write the equality function for an array which can not use the
5768 // identity function.
5771 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
5773 Location bloc
= Linemap::predeclared_location();
5775 // The pointers to the arrays we are going to compare.
5776 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
5777 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
5778 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
5780 // Build temporaries for the keys with the right types.
5781 Type
* pt
= Type::make_pointer_type(name
!= NULL
5782 ? static_cast<Type
*>(name
)
5783 : static_cast<Type
*>(this));
5785 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
5786 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5787 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
5788 gogo
->add_statement(p1
);
5790 ref
= Expression::make_var_reference(key2_arg
, bloc
);
5791 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5792 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
5793 gogo
->add_statement(p2
);
5795 // Loop over the array elements.
5797 Type
* int_type
= Type::lookup_integer_type("int");
5798 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
5799 gogo
->add_statement(index
);
5801 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
5802 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
5803 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
5808 gogo
->start_block(bloc
);
5810 // Compare element in P1 and P2.
5811 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
5812 e1
= Expression::make_unary(OPERATOR_MULT
, e1
, bloc
);
5813 ref
= Expression::make_temporary_reference(index
, bloc
);
5814 e1
= Expression::make_array_index(e1
, ref
, NULL
, NULL
, bloc
);
5816 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
5817 e2
= Expression::make_unary(OPERATOR_MULT
, e2
, bloc
);
5818 ref
= Expression::make_temporary_reference(index
, bloc
);
5819 e2
= Expression::make_array_index(e2
, ref
, NULL
, NULL
, bloc
);
5821 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
5823 // If the elements are not equal, return false.
5824 gogo
->start_block(bloc
);
5825 Expression_list
* vals
= new Expression_list();
5826 vals
->push_back(Expression::make_boolean(false, bloc
));
5827 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5828 gogo
->add_statement(s
);
5829 Block
* then_block
= gogo
->finish_block(bloc
);
5831 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
5832 gogo
->add_statement(s
);
5834 Block
* statements
= gogo
->finish_block(bloc
);
5836 for_range
->add_statements(statements
);
5837 gogo
->add_statement(for_range
);
5839 // All the elements are equal, so return true.
5840 vals
= new Expression_list();
5841 vals
->push_back(Expression::make_boolean(true, bloc
));
5842 s
= Statement::make_return_statement(vals
, bloc
);
5843 gogo
->add_statement(s
);
5846 // Get the backend representation of the fields of a slice. This is
5847 // not declared in types.h so that types.h doesn't have to #include
5850 // We use int for the count and capacity fields. This matches 6g.
5851 // The language more or less assumes that we can't allocate space of a
5852 // size which does not fit in int.
5855 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
5856 std::vector
<Backend::Btyped_identifier
>* bfields
)
5860 Type
* pet
= Type::make_pointer_type(type
->element_type());
5861 Btype
* pbet
= (use_placeholder
5862 ? pet
->get_backend_placeholder(gogo
)
5863 : pet
->get_backend(gogo
));
5864 Location ploc
= Linemap::predeclared_location();
5866 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
5867 p
->name
= "__values";
5871 Type
* int_type
= Type::lookup_integer_type("int");
5874 p
->name
= "__count";
5875 p
->btype
= int_type
->get_backend(gogo
);
5879 p
->name
= "__capacity";
5880 p
->btype
= int_type
->get_backend(gogo
);
5884 // Get the backend representation for the type of this array. A fixed array is
5885 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
5886 // just like an array in C. An open array is a struct with three
5887 // fields: a data pointer, the length, and the capacity.
5890 Array_type::do_get_backend(Gogo
* gogo
)
5892 if (this->length_
== NULL
)
5894 std::vector
<Backend::Btyped_identifier
> bfields
;
5895 get_backend_slice_fields(gogo
, this, false, &bfields
);
5896 return gogo
->backend()->struct_type(bfields
);
5900 Btype
* element
= this->get_backend_element(gogo
, false);
5901 Bexpression
* len
= this->get_backend_length(gogo
);
5902 return gogo
->backend()->array_type(element
, len
);
5906 // Return the backend representation of the element type.
5909 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
5911 if (use_placeholder
)
5912 return this->element_type_
->get_backend_placeholder(gogo
);
5914 return this->element_type_
->get_backend(gogo
);
5917 // Return the backend representation of the length. The length may be
5918 // computed using a function call, so we must only evaluate it once.
5921 Array_type::get_backend_length(Gogo
* gogo
)
5923 go_assert(this->length_
!= NULL
);
5924 if (this->blength_
== NULL
)
5926 Numeric_constant nc
;
5928 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
5930 if (mpz_sgn(val
) < 0)
5932 this->blength_
= gogo
->backend()->error_expression();
5933 return this->blength_
;
5935 Type
* t
= nc
.type();
5937 t
= Type::lookup_integer_type("int");
5938 else if (t
->is_abstract())
5939 t
= t
->make_non_abstract_type();
5940 Btype
* btype
= t
->get_backend(gogo
);
5942 gogo
->backend()->integer_constant_expression(btype
, val
);
5947 // Make up a translation context for the array length
5948 // expression. FIXME: This won't work in general.
5949 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
5950 this->blength_
= this->length_
->get_backend(&context
);
5952 Btype
* ibtype
= Type::lookup_integer_type("int")->get_backend(gogo
);
5954 gogo
->backend()->convert_expression(ibtype
, this->blength_
,
5955 this->length_
->location());
5958 return this->blength_
;
5961 // Finish backend representation of the array.
5964 Array_type::finish_backend_element(Gogo
* gogo
)
5966 Type
* et
= this->array_type()->element_type();
5967 et
->get_backend(gogo
);
5968 if (this->is_slice_type())
5970 // This relies on the fact that we always use the same
5971 // structure for a pointer to any given type.
5972 Type
* pet
= Type::make_pointer_type(et
);
5973 pet
->get_backend(gogo
);
5977 // Return an expression for a pointer to the values in ARRAY.
5980 Array_type::get_value_pointer(Gogo
*, Expression
* array
) const
5982 if (this->length() != NULL
)
5985 go_assert(array
->type()->array_type() != NULL
);
5986 Type
* etype
= array
->type()->array_type()->element_type();
5987 array
= Expression::make_unary(OPERATOR_AND
, array
, array
->location());
5988 return Expression::make_cast(Type::make_pointer_type(etype
), array
,
5993 return Expression::make_slice_info(array
,
5994 Expression::SLICE_INFO_VALUE_POINTER
,
5998 // Return an expression for the length of the array ARRAY which has this
6002 Array_type::get_length(Gogo
*, Expression
* array
) const
6004 if (this->length_
!= NULL
)
6005 return this->length_
;
6007 // This is a slice. We need to read the length field.
6008 return Expression::make_slice_info(array
, Expression::SLICE_INFO_LENGTH
,
6012 // Return an expression for the capacity of the array ARRAY which has this
6016 Array_type::get_capacity(Gogo
*, Expression
* array
) const
6018 if (this->length_
!= NULL
)
6019 return this->length_
;
6021 // This is a slice. We need to read the capacity field.
6022 return Expression::make_slice_info(array
, Expression::SLICE_INFO_CAPACITY
,
6029 Array_type::do_export(Export
* exp
) const
6031 exp
->write_c_string("[");
6032 if (this->length_
!= NULL
)
6033 this->length_
->export_expression(exp
);
6034 exp
->write_c_string("] ");
6035 exp
->write_type(this->element_type_
);
6041 Array_type::do_import(Import
* imp
)
6043 imp
->require_c_string("[");
6045 if (imp
->peek_char() == ']')
6048 length
= Expression::import_expression(imp
);
6049 imp
->require_c_string("] ");
6050 Type
* element_type
= imp
->read_type();
6051 return Type::make_array_type(element_type
, length
);
6054 // The type of an array type descriptor.
6057 Array_type::make_array_type_descriptor_type()
6062 Type
* tdt
= Type::make_type_descriptor_type();
6063 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6065 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6068 Type::make_builtin_struct_type(4,
6072 "len", uintptr_type
);
6074 ret
= Type::make_builtin_named_type("ArrayType", sf
);
6080 // The type of an slice type descriptor.
6083 Array_type::make_slice_type_descriptor_type()
6088 Type
* tdt
= Type::make_type_descriptor_type();
6089 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6092 Type::make_builtin_struct_type(2,
6096 ret
= Type::make_builtin_named_type("SliceType", sf
);
6102 // Build a type descriptor for an array/slice type.
6105 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6107 if (this->length_
!= NULL
)
6108 return this->array_type_descriptor(gogo
, name
);
6110 return this->slice_type_descriptor(gogo
, name
);
6113 // Build a type descriptor for an array type.
6116 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6118 Location bloc
= Linemap::predeclared_location();
6120 Type
* atdt
= Array_type::make_array_type_descriptor_type();
6122 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
6124 Expression_list
* vals
= new Expression_list();
6127 Struct_field_list::const_iterator p
= fields
->begin();
6128 go_assert(p
->is_field_name("commonType"));
6129 vals
->push_back(this->type_descriptor_constructor(gogo
,
6130 RUNTIME_TYPE_KIND_ARRAY
,
6134 go_assert(p
->is_field_name("elem"));
6135 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
6138 go_assert(p
->is_field_name("slice"));
6139 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
6140 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
6143 go_assert(p
->is_field_name("len"));
6144 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
6147 go_assert(p
== fields
->end());
6149 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
6152 // Build a type descriptor for a slice type.
6155 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6157 Location bloc
= Linemap::predeclared_location();
6159 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
6161 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
6163 Expression_list
* vals
= new Expression_list();
6166 Struct_field_list::const_iterator p
= fields
->begin();
6167 go_assert(p
->is_field_name("commonType"));
6168 vals
->push_back(this->type_descriptor_constructor(gogo
,
6169 RUNTIME_TYPE_KIND_SLICE
,
6173 go_assert(p
->is_field_name("elem"));
6174 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
6177 go_assert(p
== fields
->end());
6179 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
6182 // Reflection string.
6185 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6187 ret
->push_back('[');
6188 if (this->length_
!= NULL
)
6190 Numeric_constant nc
;
6192 if (!this->length_
->numeric_constant_value(&nc
)
6193 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
6194 error_at(this->length_
->location(), "invalid array length");
6198 snprintf(buf
, sizeof buf
, "%lu", val
);
6202 ret
->push_back(']');
6204 this->append_reflection(this->element_type_
, gogo
, ret
);
6210 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6212 ret
->push_back('A');
6213 this->append_mangled_name(this->element_type_
, gogo
, ret
);
6214 if (this->length_
!= NULL
)
6216 Numeric_constant nc
;
6218 if (!this->length_
->numeric_constant_value(&nc
)
6219 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
6220 error_at(this->length_
->location(), "invalid array length");
6224 snprintf(buf
, sizeof buf
, "%lu", val
);
6228 ret
->push_back('e');
6231 // Make an array type.
6234 Type::make_array_type(Type
* element_type
, Expression
* length
)
6236 return new Array_type(element_type
, length
);
6244 Map_type::do_traverse(Traverse
* traverse
)
6246 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
6247 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
6248 return TRAVERSE_EXIT
;
6249 return TRAVERSE_CONTINUE
;
6252 // Check that the map type is OK.
6255 Map_type::do_verify()
6257 // The runtime support uses "map[void]void".
6258 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
6259 error_at(this->location_
, "invalid map key type");
6263 // Whether two map types are identical.
6266 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
6268 return (Type::are_identical(this->key_type(), t
->key_type(),
6269 errors_are_identical
, NULL
)
6270 && Type::are_identical(this->val_type(), t
->val_type(),
6271 errors_are_identical
, NULL
));
6277 Map_type::do_hash_for_method(Gogo
* gogo
) const
6279 return (this->key_type_
->hash_for_method(gogo
)
6280 + this->val_type_
->hash_for_method(gogo
)
6284 // Get the backend representation for a map type. A map type is
6285 // represented as a pointer to a struct. The struct is __go_map in
6289 Map_type::do_get_backend(Gogo
* gogo
)
6291 static Btype
* backend_map_type
;
6292 if (backend_map_type
== NULL
)
6294 std::vector
<Backend::Btyped_identifier
> bfields(4);
6296 Location bloc
= Linemap::predeclared_location();
6298 Type
* pdt
= Type::make_type_descriptor_ptr_type();
6299 bfields
[0].name
= "__descriptor";
6300 bfields
[0].btype
= pdt
->get_backend(gogo
);
6301 bfields
[0].location
= bloc
;
6303 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6304 bfields
[1].name
= "__element_count";
6305 bfields
[1].btype
= uintptr_type
->get_backend(gogo
);
6306 bfields
[1].location
= bloc
;
6308 bfields
[2].name
= "__bucket_count";
6309 bfields
[2].btype
= bfields
[1].btype
;
6310 bfields
[2].location
= bloc
;
6312 Btype
* bvt
= gogo
->backend()->void_type();
6313 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
6314 Btype
* bppvt
= gogo
->backend()->pointer_type(bpvt
);
6315 bfields
[3].name
= "__buckets";
6316 bfields
[3].btype
= bppvt
;
6317 bfields
[3].location
= bloc
;
6319 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
6320 bt
= gogo
->backend()->named_type("__go_map", bt
, bloc
);
6321 backend_map_type
= gogo
->backend()->pointer_type(bt
);
6323 return backend_map_type
;
6326 // The type of a map type descriptor.
6329 Map_type::make_map_type_descriptor_type()
6334 Type
* tdt
= Type::make_type_descriptor_type();
6335 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6338 Type::make_builtin_struct_type(3,
6343 ret
= Type::make_builtin_named_type("MapType", sf
);
6349 // Build a type descriptor for a map type.
6352 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6354 Location bloc
= Linemap::predeclared_location();
6356 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
6358 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
6360 Expression_list
* vals
= new Expression_list();
6363 Struct_field_list::const_iterator p
= fields
->begin();
6364 go_assert(p
->is_field_name("commonType"));
6365 vals
->push_back(this->type_descriptor_constructor(gogo
,
6366 RUNTIME_TYPE_KIND_MAP
,
6370 go_assert(p
->is_field_name("key"));
6371 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
6374 go_assert(p
->is_field_name("elem"));
6375 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
6378 go_assert(p
== fields
->end());
6380 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
6383 // A mapping from map types to map descriptors.
6385 Map_type::Map_descriptors
Map_type::map_descriptors
;
6387 // Build a map descriptor for this type. Return a pointer to it.
6390 Map_type::map_descriptor_pointer(Gogo
* gogo
, Location location
)
6392 Bvariable
* bvar
= this->map_descriptor(gogo
);
6393 Bexpression
* var_expr
= gogo
->backend()->var_expression(bvar
, location
);
6394 return gogo
->backend()->address_expression(var_expr
, location
);
6397 // Build a map descriptor for this type.
6400 Map_type::map_descriptor(Gogo
* gogo
)
6402 std::pair
<Map_type
*, Bvariable
*> val(this, NULL
);
6403 std::pair
<Map_type::Map_descriptors::iterator
, bool> ins
=
6404 Map_type::map_descriptors
.insert(val
);
6406 return ins
.first
->second
;
6408 Type
* key_type
= this->key_type_
;
6409 Type
* val_type
= this->val_type_
;
6411 // The map entry type is a struct with three fields. Build that
6412 // struct so that we can get the offsets of the key and value within
6413 // a map entry. The first field should technically be a pointer to
6414 // this type itself, but since we only care about field offsets we
6415 // just use pointer to bool.
6416 Type
* pbool
= Type::make_pointer_type(Type::make_boolean_type());
6417 Struct_type
* map_entry_type
=
6418 Type::make_builtin_struct_type(3,
6423 Type
* map_descriptor_type
= Map_type::make_map_descriptor_type();
6425 const Struct_field_list
* fields
=
6426 map_descriptor_type
->struct_type()->fields();
6428 Expression_list
* vals
= new Expression_list();
6431 Location bloc
= Linemap::predeclared_location();
6433 Struct_field_list::const_iterator p
= fields
->begin();
6435 go_assert(p
->is_field_name("__map_descriptor"));
6436 vals
->push_back(Expression::make_type_descriptor(this, bloc
));
6439 go_assert(p
->is_field_name("__entry_size"));
6440 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
6441 vals
->push_back(Expression::make_type_info(map_entry_type
, type_info
));
6443 Struct_field_list::const_iterator pf
= map_entry_type
->fields()->begin();
6445 go_assert(pf
->is_field_name("__key"));
6448 go_assert(p
->is_field_name("__key_offset"));
6449 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6452 go_assert(pf
->is_field_name("__val"));
6455 go_assert(p
->is_field_name("__val_offset"));
6456 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6459 go_assert(p
== fields
->end());
6461 Expression
* initializer
=
6462 Expression::make_struct_composite_literal(map_descriptor_type
, vals
, bloc
);
6464 std::string mangled_name
= "__go_map_" + this->mangled_name(gogo
);
6465 Btype
* map_descriptor_btype
= map_descriptor_type
->get_backend(gogo
);
6466 Bvariable
* bvar
= gogo
->backend()->immutable_struct(mangled_name
, false,
6468 map_descriptor_btype
,
6471 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
6472 context
.set_is_const();
6473 Bexpression
* binitializer
= initializer
->get_backend(&context
);
6475 gogo
->backend()->immutable_struct_set_init(bvar
, mangled_name
, false, true,
6476 map_descriptor_btype
, bloc
,
6479 ins
.first
->second
= bvar
;
6483 // Build the type of a map descriptor. This must match the struct
6484 // __go_map_descriptor in libgo/runtime/map.h.
6487 Map_type::make_map_descriptor_type()
6492 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6493 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6495 Type::make_builtin_struct_type(4,
6496 "__map_descriptor", ptdt
,
6497 "__entry_size", uintptr_type
,
6498 "__key_offset", uintptr_type
,
6499 "__val_offset", uintptr_type
);
6500 ret
= Type::make_builtin_named_type("__go_map_descriptor", sf
);
6505 // Reflection string for a map.
6508 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6510 ret
->append("map[");
6511 this->append_reflection(this->key_type_
, gogo
, ret
);
6513 this->append_reflection(this->val_type_
, gogo
, ret
);
6516 // Mangled name for a map.
6519 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6521 ret
->push_back('M');
6522 this->append_mangled_name(this->key_type_
, gogo
, ret
);
6524 this->append_mangled_name(this->val_type_
, gogo
, ret
);
6527 // Export a map type.
6530 Map_type::do_export(Export
* exp
) const
6532 exp
->write_c_string("map [");
6533 exp
->write_type(this->key_type_
);
6534 exp
->write_c_string("] ");
6535 exp
->write_type(this->val_type_
);
6538 // Import a map type.
6541 Map_type::do_import(Import
* imp
)
6543 imp
->require_c_string("map [");
6544 Type
* key_type
= imp
->read_type();
6545 imp
->require_c_string("] ");
6546 Type
* val_type
= imp
->read_type();
6547 return Type::make_map_type(key_type
, val_type
, imp
->location());
6553 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
6555 return new Map_type(key_type
, val_type
, location
);
6558 // Class Channel_type.
6563 Channel_type::do_hash_for_method(Gogo
* gogo
) const
6565 unsigned int ret
= 0;
6566 if (this->may_send_
)
6568 if (this->may_receive_
)
6570 if (this->element_type_
!= NULL
)
6571 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
6575 // Whether this type is the same as T.
6578 Channel_type::is_identical(const Channel_type
* t
,
6579 bool errors_are_identical
) const
6581 if (!Type::are_identical(this->element_type(), t
->element_type(),
6582 errors_are_identical
, NULL
))
6584 return (this->may_send_
== t
->may_send_
6585 && this->may_receive_
== t
->may_receive_
);
6588 // Return the backend representation for a channel type. A channel is a pointer
6589 // to a __go_channel struct. The __go_channel struct is defined in
6590 // libgo/runtime/channel.h.
6593 Channel_type::do_get_backend(Gogo
* gogo
)
6595 static Btype
* backend_channel_type
;
6596 if (backend_channel_type
== NULL
)
6598 std::vector
<Backend::Btyped_identifier
> bfields
;
6599 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
6600 bt
= gogo
->backend()->named_type("__go_channel", bt
,
6601 Linemap::predeclared_location());
6602 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
6604 return backend_channel_type
;
6607 // Build a type descriptor for a channel type.
6610 Channel_type::make_chan_type_descriptor_type()
6615 Type
* tdt
= Type::make_type_descriptor_type();
6616 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6618 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6621 Type::make_builtin_struct_type(3,
6624 "dir", uintptr_type
);
6626 ret
= Type::make_builtin_named_type("ChanType", sf
);
6632 // Build a type descriptor for a map type.
6635 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6637 Location bloc
= Linemap::predeclared_location();
6639 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
6641 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
6643 Expression_list
* vals
= new Expression_list();
6646 Struct_field_list::const_iterator p
= fields
->begin();
6647 go_assert(p
->is_field_name("commonType"));
6648 vals
->push_back(this->type_descriptor_constructor(gogo
,
6649 RUNTIME_TYPE_KIND_CHAN
,
6653 go_assert(p
->is_field_name("elem"));
6654 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
6657 go_assert(p
->is_field_name("dir"));
6658 // These bits must match the ones in libgo/runtime/go-type.h.
6660 if (this->may_receive_
)
6662 if (this->may_send_
)
6665 mpz_init_set_ui(iv
, val
);
6666 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
6670 go_assert(p
== fields
->end());
6672 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
6675 // Reflection string.
6678 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6680 if (!this->may_send_
)
6682 ret
->append("chan");
6683 if (!this->may_receive_
)
6685 ret
->push_back(' ');
6686 this->append_reflection(this->element_type_
, gogo
, ret
);
6692 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6694 ret
->push_back('C');
6695 this->append_mangled_name(this->element_type_
, gogo
, ret
);
6696 if (this->may_send_
)
6697 ret
->push_back('s');
6698 if (this->may_receive_
)
6699 ret
->push_back('r');
6700 ret
->push_back('e');
6706 Channel_type::do_export(Export
* exp
) const
6708 exp
->write_c_string("chan ");
6709 if (this->may_send_
&& !this->may_receive_
)
6710 exp
->write_c_string("-< ");
6711 else if (this->may_receive_
&& !this->may_send_
)
6712 exp
->write_c_string("<- ");
6713 exp
->write_type(this->element_type_
);
6719 Channel_type::do_import(Import
* imp
)
6721 imp
->require_c_string("chan ");
6725 if (imp
->match_c_string("-< "))
6729 may_receive
= false;
6731 else if (imp
->match_c_string("<- "))
6743 Type
* element_type
= imp
->read_type();
6745 return Type::make_channel_type(may_send
, may_receive
, element_type
);
6748 // Make a new channel type.
6751 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
6753 return new Channel_type(send
, receive
, element_type
);
6756 // Class Interface_type.
6758 // Return the list of methods.
6760 const Typed_identifier_list
*
6761 Interface_type::methods() const
6763 go_assert(this->methods_are_finalized_
|| saw_errors());
6764 return this->all_methods_
;
6767 // Return the number of methods.
6770 Interface_type::method_count() const
6772 go_assert(this->methods_are_finalized_
|| saw_errors());
6773 return this->all_methods_
== NULL
? 0 : this->all_methods_
->size();
6779 Interface_type::do_traverse(Traverse
* traverse
)
6781 Typed_identifier_list
* methods
= (this->methods_are_finalized_
6782 ? this->all_methods_
6783 : this->parse_methods_
);
6784 if (methods
== NULL
)
6785 return TRAVERSE_CONTINUE
;
6786 return methods
->traverse(traverse
);
6789 // Finalize the methods. This handles interface inheritance.
6792 Interface_type::finalize_methods()
6794 if (this->methods_are_finalized_
)
6796 this->methods_are_finalized_
= true;
6797 if (this->parse_methods_
== NULL
)
6800 this->all_methods_
= new Typed_identifier_list();
6801 this->all_methods_
->reserve(this->parse_methods_
->size());
6802 Typed_identifier_list inherit
;
6803 for (Typed_identifier_list::const_iterator pm
=
6804 this->parse_methods_
->begin();
6805 pm
!= this->parse_methods_
->end();
6808 const Typed_identifier
* p
= &*pm
;
6809 if (p
->name().empty())
6810 inherit
.push_back(*p
);
6811 else if (this->find_method(p
->name()) == NULL
)
6812 this->all_methods_
->push_back(*p
);
6814 error_at(p
->location(), "duplicate method %qs",
6815 Gogo::message_name(p
->name()).c_str());
6818 std::vector
<Named_type
*> seen
;
6819 seen
.reserve(inherit
.size());
6820 bool issued_recursive_error
= false;
6821 while (!inherit
.empty())
6823 Type
* t
= inherit
.back().type();
6824 Location tl
= inherit
.back().location();
6827 Interface_type
* it
= t
->interface_type();
6831 error_at(tl
, "interface contains embedded non-interface");
6836 if (!issued_recursive_error
)
6838 error_at(tl
, "invalid recursive interface");
6839 issued_recursive_error
= true;
6844 Named_type
* nt
= t
->named_type();
6845 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
6847 std::vector
<Named_type
*>::const_iterator q
;
6848 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
6852 error_at(tl
, "inherited interface loop");
6856 if (q
!= seen
.end())
6861 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
6862 if (imethods
== NULL
)
6864 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
6865 q
!= imethods
->end();
6868 if (q
->name().empty())
6869 inherit
.push_back(*q
);
6870 else if (this->find_method(q
->name()) == NULL
)
6871 this->all_methods_
->push_back(Typed_identifier(q
->name(),
6874 error_at(tl
, "inherited method %qs is ambiguous",
6875 Gogo::message_name(q
->name()).c_str());
6879 if (!this->all_methods_
->empty())
6880 this->all_methods_
->sort_by_name();
6883 delete this->all_methods_
;
6884 this->all_methods_
= NULL
;
6888 // Return the method NAME, or NULL.
6890 const Typed_identifier
*
6891 Interface_type::find_method(const std::string
& name
) const
6893 go_assert(this->methods_are_finalized_
);
6894 if (this->all_methods_
== NULL
)
6896 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6897 p
!= this->all_methods_
->end();
6899 if (p
->name() == name
)
6904 // Return the method index.
6907 Interface_type::method_index(const std::string
& name
) const
6909 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
6911 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6912 p
!= this->all_methods_
->end();
6914 if (p
->name() == name
)
6919 // Return whether NAME is an unexported method, for better error
6923 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
6925 go_assert(this->methods_are_finalized_
);
6926 if (this->all_methods_
== NULL
)
6928 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6929 p
!= this->all_methods_
->end();
6932 const std::string
& method_name(p
->name());
6933 if (Gogo::is_hidden_name(method_name
)
6934 && name
== Gogo::unpack_hidden_name(method_name
)
6935 && gogo
->pack_hidden_name(name
, false) != method_name
)
6941 // Whether this type is identical with T.
6944 Interface_type::is_identical(const Interface_type
* t
,
6945 bool errors_are_identical
) const
6947 // If methods have not been finalized, then we are asking whether
6948 // func redeclarations are the same. This is an error, so for
6949 // simplicity we say they are never the same.
6950 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
6953 // We require the same methods with the same types. The methods
6954 // have already been sorted.
6955 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
6956 return this->all_methods_
== t
->all_methods_
;
6958 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
6961 Assume_identical
* hold_ai
= this->assume_identical_
;
6962 Assume_identical ai
;
6966 this->assume_identical_
= &ai
;
6968 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
6969 Typed_identifier_list::const_iterator p2
;
6970 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
6972 if (p1
== this->all_methods_
->end())
6974 if (p1
->name() != p2
->name()
6975 || !Type::are_identical(p1
->type(), p2
->type(),
6976 errors_are_identical
, NULL
))
6980 this->assume_identical_
= hold_ai
;
6982 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
6985 // Return true if T1 and T2 are assumed to be identical during a type
6989 Interface_type::assume_identical(const Interface_type
* t1
,
6990 const Interface_type
* t2
) const
6992 for (Assume_identical
* p
= this->assume_identical_
;
6995 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
7000 // Whether we can assign the interface type T to this type. The types
7001 // are known to not be identical. An interface assignment is only
7002 // permitted if T is known to implement all methods in THIS.
7003 // Otherwise a type guard is required.
7006 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
7007 std::string
* reason
) const
7009 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
7010 if (this->all_methods_
== NULL
)
7012 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7013 p
!= this->all_methods_
->end();
7016 const Typed_identifier
* m
= t
->find_method(p
->name());
7022 snprintf(buf
, sizeof buf
,
7023 _("need explicit conversion; missing method %s%s%s"),
7024 open_quote
, Gogo::message_name(p
->name()).c_str(),
7026 reason
->assign(buf
);
7031 std::string subreason
;
7032 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
7036 std::string n
= Gogo::message_name(p
->name());
7037 size_t len
= 100 + n
.length() + subreason
.length();
7038 char* buf
= new char[len
];
7039 if (subreason
.empty())
7040 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
7041 open_quote
, n
.c_str(), close_quote
);
7044 _("incompatible type for method %s%s%s (%s)"),
7045 open_quote
, n
.c_str(), close_quote
,
7047 reason
->assign(buf
);
7060 Interface_type::do_hash_for_method(Gogo
*) const
7062 go_assert(this->methods_are_finalized_
);
7063 unsigned int ret
= 0;
7064 if (this->all_methods_
!= NULL
)
7066 for (Typed_identifier_list::const_iterator p
=
7067 this->all_methods_
->begin();
7068 p
!= this->all_methods_
->end();
7071 ret
= Type::hash_string(p
->name(), ret
);
7072 // We don't use the method type in the hash, to avoid
7073 // infinite recursion if an interface method uses a type
7074 // which is an interface which inherits from the interface
7076 // type T interface { F() interface {T}}
7083 // Return true if T implements the interface. If it does not, and
7084 // REASON is not NULL, set *REASON to a useful error message.
7087 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
7089 go_assert(this->methods_are_finalized_
);
7090 if (this->all_methods_
== NULL
)
7093 bool is_pointer
= false;
7094 const Named_type
* nt
= t
->named_type();
7095 const Struct_type
* st
= t
->struct_type();
7096 // If we start with a named type, we don't dereference it to find
7100 const Type
* pt
= t
->points_to();
7103 // If T is a pointer to a named type, then we need to look at
7104 // the type to which it points.
7106 nt
= pt
->named_type();
7107 st
= pt
->struct_type();
7111 // If we have a named type, get the methods from it rather than from
7116 // Only named and struct types have methods.
7117 if (nt
== NULL
&& st
== NULL
)
7121 if (t
->points_to() != NULL
7122 && t
->points_to()->interface_type() != NULL
)
7123 reason
->assign(_("pointer to interface type has no methods"));
7125 reason
->assign(_("type has no methods"));
7130 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
7134 if (t
->points_to() != NULL
7135 && t
->points_to()->interface_type() != NULL
)
7136 reason
->assign(_("pointer to interface type has no methods"));
7138 reason
->assign(_("type has no methods"));
7143 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7144 p
!= this->all_methods_
->end();
7147 bool is_ambiguous
= false;
7148 Method
* m
= (nt
!= NULL
7149 ? nt
->method_function(p
->name(), &is_ambiguous
)
7150 : st
->method_function(p
->name(), &is_ambiguous
));
7155 std::string n
= Gogo::message_name(p
->name());
7156 size_t len
= n
.length() + 100;
7157 char* buf
= new char[len
];
7159 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
7160 open_quote
, n
.c_str(), close_quote
);
7162 snprintf(buf
, len
, _("missing method %s%s%s"),
7163 open_quote
, n
.c_str(), close_quote
);
7164 reason
->assign(buf
);
7170 Function_type
*p_fn_type
= p
->type()->function_type();
7171 Function_type
* m_fn_type
= m
->type()->function_type();
7172 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
7173 std::string subreason
;
7174 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
7178 std::string n
= Gogo::message_name(p
->name());
7179 size_t len
= 100 + n
.length() + subreason
.length();
7180 char* buf
= new char[len
];
7181 if (subreason
.empty())
7182 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
7183 open_quote
, n
.c_str(), close_quote
);
7186 _("incompatible type for method %s%s%s (%s)"),
7187 open_quote
, n
.c_str(), close_quote
,
7189 reason
->assign(buf
);
7195 if (!is_pointer
&& !m
->is_value_method())
7199 std::string n
= Gogo::message_name(p
->name());
7200 size_t len
= 100 + n
.length();
7201 char* buf
= new char[len
];
7203 _("method %s%s%s requires a pointer receiver"),
7204 open_quote
, n
.c_str(), close_quote
);
7205 reason
->assign(buf
);
7211 // If the magic //go:nointerface comment was used, the method
7212 // may not be used to implement interfaces.
7213 if (m
->nointerface())
7217 std::string n
= Gogo::message_name(p
->name());
7218 size_t len
= 100 + n
.length();
7219 char* buf
= new char[len
];
7221 _("method %s%s%s is marked go:nointerface"),
7222 open_quote
, n
.c_str(), close_quote
);
7223 reason
->assign(buf
);
7233 // Return the backend representation of the empty interface type. We
7234 // use the same struct for all empty interfaces.
7237 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
7239 static Btype
* empty_interface_type
;
7240 if (empty_interface_type
== NULL
)
7242 std::vector
<Backend::Btyped_identifier
> bfields(2);
7244 Location bloc
= Linemap::predeclared_location();
7246 Type
* pdt
= Type::make_type_descriptor_ptr_type();
7247 bfields
[0].name
= "__type_descriptor";
7248 bfields
[0].btype
= pdt
->get_backend(gogo
);
7249 bfields
[0].location
= bloc
;
7251 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
7252 bfields
[1].name
= "__object";
7253 bfields
[1].btype
= vt
->get_backend(gogo
);
7254 bfields
[1].location
= bloc
;
7256 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
7258 return empty_interface_type
;
7261 // Return a pointer to the backend representation of the method table.
7264 Interface_type::get_backend_methods(Gogo
* gogo
)
7266 if (this->bmethods_
!= NULL
&& !this->bmethods_is_placeholder_
)
7267 return this->bmethods_
;
7269 Location loc
= this->location();
7271 std::vector
<Backend::Btyped_identifier
>
7272 mfields(this->all_methods_
->size() + 1);
7274 Type
* pdt
= Type::make_type_descriptor_ptr_type();
7275 mfields
[0].name
= "__type_descriptor";
7276 mfields
[0].btype
= pdt
->get_backend(gogo
);
7277 mfields
[0].location
= loc
;
7279 std::string last_name
= "";
7281 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
7282 p
!= this->all_methods_
->end();
7285 // The type of the method in Go only includes the parameters.
7286 // The actual method also has a receiver, which is always a
7287 // pointer. We need to add that pointer type here in order to
7288 // generate the correct type for the backend.
7289 Function_type
* ft
= p
->type()->function_type();
7290 go_assert(ft
->receiver() == NULL
);
7292 const Typed_identifier_list
* params
= ft
->parameters();
7293 Typed_identifier_list
* mparams
= new Typed_identifier_list();
7295 mparams
->reserve(params
->size() + 1);
7296 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
7297 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
7300 for (Typed_identifier_list::const_iterator pp
= params
->begin();
7301 pp
!= params
->end();
7303 mparams
->push_back(*pp
);
7306 Typed_identifier_list
* mresults
= (ft
->results() == NULL
7308 : ft
->results()->copy());
7309 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
7312 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
7313 mfields
[i
].btype
= mft
->get_backend_fntype(gogo
);
7314 mfields
[i
].location
= loc
;
7316 // Sanity check: the names should be sorted.
7317 go_assert(p
->name() > last_name
);
7318 last_name
= p
->name();
7321 Btype
* st
= gogo
->backend()->struct_type(mfields
);
7322 Btype
* ret
= gogo
->backend()->pointer_type(st
);
7324 if (this->bmethods_
!= NULL
&& this->bmethods_is_placeholder_
)
7325 gogo
->backend()->set_placeholder_pointer_type(this->bmethods_
, ret
);
7326 this->bmethods_
= ret
;
7327 this->bmethods_is_placeholder_
= false;
7331 // Return a placeholder for the pointer to the backend methods table.
7334 Interface_type::get_backend_methods_placeholder(Gogo
* gogo
)
7336 if (this->bmethods_
== NULL
)
7338 Location loc
= this->location();
7339 this->bmethods_
= gogo
->backend()->placeholder_pointer_type("", loc
,
7341 this->bmethods_is_placeholder_
= true;
7343 return this->bmethods_
;
7346 // Return the fields of a non-empty interface type. This is not
7347 // declared in types.h so that types.h doesn't have to #include
7351 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
7352 bool use_placeholder
,
7353 std::vector
<Backend::Btyped_identifier
>* bfields
)
7355 Location loc
= type
->location();
7359 (*bfields
)[0].name
= "__methods";
7360 (*bfields
)[0].btype
= (use_placeholder
7361 ? type
->get_backend_methods_placeholder(gogo
)
7362 : type
->get_backend_methods(gogo
));
7363 (*bfields
)[0].location
= loc
;
7365 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
7366 (*bfields
)[1].name
= "__object";
7367 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
7368 (*bfields
)[1].location
= Linemap::predeclared_location();
7371 // Return the backend representation for an interface type. An interface is a
7372 // pointer to a struct. The struct has three fields. The first field is a
7373 // pointer to the type descriptor for the dynamic type of the object.
7374 // The second field is a pointer to a table of methods for the
7375 // interface to be used with the object. The third field is the value
7376 // of the object itself.
7379 Interface_type::do_get_backend(Gogo
* gogo
)
7381 if (this->is_empty())
7382 return Interface_type::get_backend_empty_interface_type(gogo
);
7385 if (this->interface_btype_
!= NULL
)
7386 return this->interface_btype_
;
7387 this->interface_btype_
=
7388 gogo
->backend()->placeholder_struct_type("", this->location_
);
7389 std::vector
<Backend::Btyped_identifier
> bfields
;
7390 get_backend_interface_fields(gogo
, this, false, &bfields
);
7391 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
7393 this->interface_btype_
= gogo
->backend()->error_type();
7394 return this->interface_btype_
;
7398 // Finish the backend representation of the methods.
7401 Interface_type::finish_backend_methods(Gogo
* gogo
)
7403 if (!this->is_empty())
7405 const Typed_identifier_list
* methods
= this->methods();
7406 if (methods
!= NULL
)
7408 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7409 p
!= methods
->end();
7411 p
->type()->get_backend(gogo
);
7414 // Getting the backend methods now will set the placeholder
7416 this->get_backend_methods(gogo
);
7420 // The type of an interface type descriptor.
7423 Interface_type::make_interface_type_descriptor_type()
7428 Type
* tdt
= Type::make_type_descriptor_type();
7429 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7431 Type
* string_type
= Type::lookup_string_type();
7432 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
7435 Type::make_builtin_struct_type(3,
7436 "name", pointer_string_type
,
7437 "pkgPath", pointer_string_type
,
7440 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
7442 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
7444 Struct_type
* s
= Type::make_builtin_struct_type(2,
7446 "methods", slice_nsm
);
7448 ret
= Type::make_builtin_named_type("InterfaceType", s
);
7454 // Build a type descriptor for an interface type.
7457 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7459 Location bloc
= Linemap::predeclared_location();
7461 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
7463 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
7465 Expression_list
* ivals
= new Expression_list();
7468 Struct_field_list::const_iterator pif
= ifields
->begin();
7469 go_assert(pif
->is_field_name("commonType"));
7470 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
7471 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
7475 go_assert(pif
->is_field_name("methods"));
7477 Expression_list
* methods
= new Expression_list();
7478 if (this->all_methods_
!= NULL
)
7480 Type
* elemtype
= pif
->type()->array_type()->element_type();
7482 methods
->reserve(this->all_methods_
->size());
7483 for (Typed_identifier_list::const_iterator pm
=
7484 this->all_methods_
->begin();
7485 pm
!= this->all_methods_
->end();
7488 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
7490 Expression_list
* mvals
= new Expression_list();
7493 Struct_field_list::const_iterator pmf
= mfields
->begin();
7494 go_assert(pmf
->is_field_name("name"));
7495 std::string s
= Gogo::unpack_hidden_name(pm
->name());
7496 Expression
* e
= Expression::make_string(s
, bloc
);
7497 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7500 go_assert(pmf
->is_field_name("pkgPath"));
7501 if (!Gogo::is_hidden_name(pm
->name()))
7502 mvals
->push_back(Expression::make_nil(bloc
));
7505 s
= Gogo::hidden_name_pkgpath(pm
->name());
7506 e
= Expression::make_string(s
, bloc
);
7507 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7511 go_assert(pmf
->is_field_name("typ"));
7512 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
7515 go_assert(pmf
== mfields
->end());
7517 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
7519 methods
->push_back(e
);
7523 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
7527 go_assert(pif
== ifields
->end());
7529 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
7532 // Reflection string.
7535 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7537 ret
->append("interface {");
7538 const Typed_identifier_list
* methods
= this->parse_methods_
;
7539 if (methods
!= NULL
)
7541 ret
->push_back(' ');
7542 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7543 p
!= methods
->end();
7546 if (p
!= methods
->begin())
7548 if (p
->name().empty())
7549 this->append_reflection(p
->type(), gogo
, ret
);
7552 if (!Gogo::is_hidden_name(p
->name()))
7553 ret
->append(p
->name());
7554 else if (gogo
->pkgpath_from_option())
7555 ret
->append(p
->name().substr(1));
7558 // If no -fgo-pkgpath option, backward compatibility
7559 // for how this used to work before -fgo-pkgpath was
7561 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
7562 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
7563 ret
->push_back('.');
7564 ret
->append(Gogo::unpack_hidden_name(p
->name()));
7566 std::string sub
= p
->type()->reflection(gogo
);
7567 go_assert(sub
.compare(0, 4, "func") == 0);
7568 sub
= sub
.substr(4);
7572 ret
->push_back(' ');
7580 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7582 go_assert(this->methods_are_finalized_
);
7584 ret
->push_back('I');
7586 const Typed_identifier_list
* methods
= this->all_methods_
;
7587 if (methods
!= NULL
&& !this->seen_
)
7590 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7591 p
!= methods
->end();
7594 if (!p
->name().empty())
7597 if (!Gogo::is_hidden_name(p
->name()))
7602 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
7603 n
.append(Gogo::pkgpath_for_symbol(pkgpath
));
7605 n
.append(Gogo::unpack_hidden_name(p
->name()));
7608 snprintf(buf
, sizeof buf
, "%u_",
7609 static_cast<unsigned int>(n
.length()));
7613 this->append_mangled_name(p
->type(), gogo
, ret
);
7615 this->seen_
= false;
7618 ret
->push_back('e');
7624 Interface_type::do_export(Export
* exp
) const
7626 exp
->write_c_string("interface { ");
7628 const Typed_identifier_list
* methods
= this->parse_methods_
;
7629 if (methods
!= NULL
)
7631 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
7632 pm
!= methods
->end();
7635 if (pm
->name().empty())
7637 exp
->write_c_string("? ");
7638 exp
->write_type(pm
->type());
7642 exp
->write_string(pm
->name());
7643 exp
->write_c_string(" (");
7645 const Function_type
* fntype
= pm
->type()->function_type();
7648 const Typed_identifier_list
* parameters
= fntype
->parameters();
7649 if (parameters
!= NULL
)
7651 bool is_varargs
= fntype
->is_varargs();
7652 for (Typed_identifier_list::const_iterator pp
=
7653 parameters
->begin();
7654 pp
!= parameters
->end();
7660 exp
->write_c_string(", ");
7661 exp
->write_name(pp
->name());
7662 exp
->write_c_string(" ");
7663 if (!is_varargs
|| pp
+ 1 != parameters
->end())
7664 exp
->write_type(pp
->type());
7667 exp
->write_c_string("...");
7668 Type
*pptype
= pp
->type();
7669 exp
->write_type(pptype
->array_type()->element_type());
7674 exp
->write_c_string(")");
7676 const Typed_identifier_list
* results
= fntype
->results();
7677 if (results
!= NULL
)
7679 exp
->write_c_string(" ");
7680 if (results
->size() == 1 && results
->begin()->name().empty())
7681 exp
->write_type(results
->begin()->type());
7685 exp
->write_c_string("(");
7686 for (Typed_identifier_list::const_iterator p
=
7688 p
!= results
->end();
7694 exp
->write_c_string(", ");
7695 exp
->write_name(p
->name());
7696 exp
->write_c_string(" ");
7697 exp
->write_type(p
->type());
7699 exp
->write_c_string(")");
7704 exp
->write_c_string("; ");
7708 exp
->write_c_string("}");
7711 // Import an interface type.
7714 Interface_type::do_import(Import
* imp
)
7716 imp
->require_c_string("interface { ");
7718 Typed_identifier_list
* methods
= new Typed_identifier_list
;
7719 while (imp
->peek_char() != '}')
7721 std::string name
= imp
->read_identifier();
7725 imp
->require_c_string(" ");
7726 Type
* t
= imp
->read_type();
7727 methods
->push_back(Typed_identifier("", t
, imp
->location()));
7728 imp
->require_c_string("; ");
7732 imp
->require_c_string(" (");
7734 Typed_identifier_list
* parameters
;
7735 bool is_varargs
= false;
7736 if (imp
->peek_char() == ')')
7740 parameters
= new Typed_identifier_list
;
7743 std::string name
= imp
->read_name();
7744 imp
->require_c_string(" ");
7746 if (imp
->match_c_string("..."))
7752 Type
* ptype
= imp
->read_type();
7754 ptype
= Type::make_array_type(ptype
, NULL
);
7755 parameters
->push_back(Typed_identifier(name
, ptype
,
7757 if (imp
->peek_char() != ',')
7759 go_assert(!is_varargs
);
7760 imp
->require_c_string(", ");
7763 imp
->require_c_string(")");
7765 Typed_identifier_list
* results
;
7766 if (imp
->peek_char() != ' ')
7770 results
= new Typed_identifier_list
;
7772 if (imp
->peek_char() != '(')
7774 Type
* rtype
= imp
->read_type();
7775 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
7782 std::string name
= imp
->read_name();
7783 imp
->require_c_string(" ");
7784 Type
* rtype
= imp
->read_type();
7785 results
->push_back(Typed_identifier(name
, rtype
,
7787 if (imp
->peek_char() != ',')
7789 imp
->require_c_string(", ");
7791 imp
->require_c_string(")");
7795 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
7799 fntype
->set_is_varargs();
7800 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
7802 imp
->require_c_string("; ");
7805 imp
->require_c_string("}");
7807 if (methods
->empty())
7813 return Type::make_interface_type(methods
, imp
->location());
7816 // Make an interface type.
7819 Type::make_interface_type(Typed_identifier_list
* methods
,
7822 return new Interface_type(methods
, location
);
7825 // Make an empty interface type.
7828 Type::make_empty_interface_type(Location location
)
7830 Interface_type
* ret
= new Interface_type(NULL
, location
);
7831 ret
->finalize_methods();
7837 // Bind a method to an object.
7840 Method::bind_method(Expression
* expr
, Location location
) const
7842 if (this->stub_
== NULL
)
7844 // When there is no stub object, the binding is determined by
7846 return this->do_bind_method(expr
, location
);
7848 return Expression::make_bound_method(expr
, this, this->stub_
, location
);
7851 // Return the named object associated with a method. This may only be
7852 // called after methods are finalized.
7855 Method::named_object() const
7857 if (this->stub_
!= NULL
)
7859 return this->do_named_object();
7862 // Class Named_method.
7864 // The type of the method.
7867 Named_method::do_type() const
7869 if (this->named_object_
->is_function())
7870 return this->named_object_
->func_value()->type();
7871 else if (this->named_object_
->is_function_declaration())
7872 return this->named_object_
->func_declaration_value()->type();
7877 // Return the location of the method receiver.
7880 Named_method::do_receiver_location() const
7882 return this->do_type()->receiver()->location();
7885 // Bind a method to an object.
7888 Named_method::do_bind_method(Expression
* expr
, Location location
) const
7890 Named_object
* no
= this->named_object_
;
7891 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, this,
7893 // If this is not a local method, and it does not use a stub, then
7894 // the real method expects a different type. We need to cast the
7896 if (this->depth() > 0 && !this->needs_stub_method())
7898 Function_type
* ftype
= this->do_type();
7899 go_assert(ftype
->is_method());
7900 Type
* frtype
= ftype
->receiver()->type();
7901 bme
->set_first_argument_type(frtype
);
7906 // Return whether this method should not participate in interfaces.
7909 Named_method::do_nointerface() const
7911 Named_object
* no
= this->named_object_
;
7912 return no
->is_function() && no
->func_value()->nointerface();
7915 // Class Interface_method.
7917 // Bind a method to an object.
7920 Interface_method::do_bind_method(Expression
* expr
,
7921 Location location
) const
7923 return Expression::make_interface_field_reference(expr
, this->name_
,
7929 // Insert a new method. Return true if it was inserted, false
7933 Methods::insert(const std::string
& name
, Method
* m
)
7935 std::pair
<Method_map::iterator
, bool> ins
=
7936 this->methods_
.insert(std::make_pair(name
, m
));
7941 Method
* old_method
= ins
.first
->second
;
7942 if (m
->depth() < old_method
->depth())
7945 ins
.first
->second
= m
;
7950 if (m
->depth() == old_method
->depth())
7951 old_method
->set_is_ambiguous();
7957 // Return the number of unambiguous methods.
7960 Methods::count() const
7963 for (Method_map::const_iterator p
= this->methods_
.begin();
7964 p
!= this->methods_
.end();
7966 if (!p
->second
->is_ambiguous())
7971 // Class Named_type.
7973 // Return the name of the type.
7976 Named_type::name() const
7978 return this->named_object_
->name();
7981 // Return the name of the type to use in an error message.
7984 Named_type::message_name() const
7986 return this->named_object_
->message_name();
7989 // Whether this is an alias. There are currently only two aliases so
7990 // we just recognize them by name.
7993 Named_type::is_alias() const
7995 if (!this->is_builtin())
7997 const std::string
& name(this->name());
7998 return name
== "byte" || name
== "rune";
8001 // Return the base type for this type. We have to be careful about
8002 // circular type definitions, which are invalid but may be seen here.
8005 Named_type::named_base()
8010 Type
* ret
= this->type_
->base();
8011 this->seen_
= false;
8016 Named_type::named_base() const
8021 const Type
* ret
= this->type_
->base();
8022 this->seen_
= false;
8026 // Return whether this is an error type. We have to be careful about
8027 // circular type definitions, which are invalid but may be seen here.
8030 Named_type::is_named_error_type() const
8035 bool ret
= this->type_
->is_error_type();
8036 this->seen_
= false;
8040 // Whether this type is comparable. We have to be careful about
8041 // circular type definitions.
8044 Named_type::named_type_is_comparable(std::string
* reason
) const
8049 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
8050 this->type_
, reason
);
8051 this->seen_
= false;
8055 // Add a method to this type.
8058 Named_type::add_method(const std::string
& name
, Function
* function
)
8060 if (this->local_methods_
== NULL
)
8061 this->local_methods_
= new Bindings(NULL
);
8062 return this->local_methods_
->add_function(name
, NULL
, function
);
8065 // Add a method declaration to this type.
8068 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
8069 Function_type
* type
,
8072 if (this->local_methods_
== NULL
)
8073 this->local_methods_
= new Bindings(NULL
);
8074 return this->local_methods_
->add_function_declaration(name
, package
, type
,
8078 // Add an existing method to this type.
8081 Named_type::add_existing_method(Named_object
* no
)
8083 if (this->local_methods_
== NULL
)
8084 this->local_methods_
= new Bindings(NULL
);
8085 this->local_methods_
->add_named_object(no
);
8088 // Look for a local method NAME, and returns its named object, or NULL
8092 Named_type::find_local_method(const std::string
& name
) const
8094 if (this->local_methods_
== NULL
)
8096 return this->local_methods_
->lookup(name
);
8099 // Return whether NAME is an unexported field or method, for better
8103 Named_type::is_unexported_local_method(Gogo
* gogo
,
8104 const std::string
& name
) const
8106 Bindings
* methods
= this->local_methods_
;
8107 if (methods
!= NULL
)
8109 for (Bindings::const_declarations_iterator p
=
8110 methods
->begin_declarations();
8111 p
!= methods
->end_declarations();
8114 if (Gogo::is_hidden_name(p
->first
)
8115 && name
== Gogo::unpack_hidden_name(p
->first
)
8116 && gogo
->pack_hidden_name(name
, false) != p
->first
)
8123 // Build the complete list of methods for this type, which means
8124 // recursively including all methods for anonymous fields. Create all
8128 Named_type::finalize_methods(Gogo
* gogo
)
8130 if (this->all_methods_
!= NULL
)
8133 if (this->local_methods_
!= NULL
8134 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
8136 const Bindings
* lm
= this->local_methods_
;
8137 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
8138 p
!= lm
->end_declarations();
8140 error_at(p
->second
->location(),
8141 "invalid pointer or interface receiver type");
8142 delete this->local_methods_
;
8143 this->local_methods_
= NULL
;
8147 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
8150 // Return the method NAME, or NULL if there isn't one or if it is
8151 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
8155 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
8157 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
8160 // Return a pointer to the interface method table for this type for
8161 // the interface INTERFACE. IS_POINTER is true if this is for a
8165 Named_type::interface_method_table(Interface_type
* interface
, bool is_pointer
)
8167 return Type::interface_method_table(this, interface
, is_pointer
,
8168 &this->interface_method_tables_
,
8169 &this->pointer_interface_method_tables_
);
8172 // Return whether a named type has any hidden fields.
8175 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
8180 bool ret
= this->type_
->has_hidden_fields(this, reason
);
8181 this->seen_
= false;
8185 // Look for a use of a complete type within another type. This is
8186 // used to check that we don't try to use a type within itself.
8188 class Find_type_use
: public Traverse
8191 Find_type_use(Named_type
* find_type
)
8192 : Traverse(traverse_types
),
8193 find_type_(find_type
), found_(false)
8196 // Whether we found the type.
8199 { return this->found_
; }
8206 // The type we are looking for.
8207 Named_type
* find_type_
;
8208 // Whether we found the type.
8212 // Check for FIND_TYPE in TYPE.
8215 Find_type_use::type(Type
* type
)
8217 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
8219 this->found_
= true;
8220 return TRAVERSE_EXIT
;
8223 // It's OK if we see a reference to the type in any type which is
8224 // essentially a pointer: a pointer, a slice, a function, a map, or
8226 if (type
->points_to() != NULL
8227 || type
->is_slice_type()
8228 || type
->function_type() != NULL
8229 || type
->map_type() != NULL
8230 || type
->channel_type() != NULL
)
8231 return TRAVERSE_SKIP_COMPONENTS
;
8233 // For an interface, a reference to the type in a method type should
8234 // be ignored, but we have to consider direct inheritance. When
8235 // this is called, there may be cases of direct inheritance
8236 // represented as a method with no name.
8237 if (type
->interface_type() != NULL
)
8239 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
8240 if (methods
!= NULL
)
8242 for (Typed_identifier_list::const_iterator p
= methods
->begin();
8243 p
!= methods
->end();
8246 if (p
->name().empty())
8248 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
8249 return TRAVERSE_EXIT
;
8253 return TRAVERSE_SKIP_COMPONENTS
;
8256 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
8257 // to convert TYPE to the backend representation before we convert
8259 if (type
->named_type() != NULL
)
8261 switch (type
->base()->classification())
8263 case Type::TYPE_ERROR
:
8264 case Type::TYPE_BOOLEAN
:
8265 case Type::TYPE_INTEGER
:
8266 case Type::TYPE_FLOAT
:
8267 case Type::TYPE_COMPLEX
:
8268 case Type::TYPE_STRING
:
8269 case Type::TYPE_NIL
:
8272 case Type::TYPE_ARRAY
:
8273 case Type::TYPE_STRUCT
:
8274 this->find_type_
->add_dependency(type
->named_type());
8277 case Type::TYPE_NAMED
:
8278 case Type::TYPE_FORWARD
:
8279 go_assert(saw_errors());
8282 case Type::TYPE_VOID
:
8283 case Type::TYPE_SINK
:
8284 case Type::TYPE_FUNCTION
:
8285 case Type::TYPE_POINTER
:
8286 case Type::TYPE_CALL_MULTIPLE_RESULT
:
8287 case Type::TYPE_MAP
:
8288 case Type::TYPE_CHANNEL
:
8289 case Type::TYPE_INTERFACE
:
8295 return TRAVERSE_CONTINUE
;
8298 // Verify that a named type does not refer to itself.
8301 Named_type::do_verify()
8303 if (this->is_verified_
)
8305 this->is_verified_
= true;
8307 Find_type_use
find(this);
8308 Type::traverse(this->type_
, &find
);
8311 error_at(this->location_
, "invalid recursive type %qs",
8312 this->message_name().c_str());
8313 this->is_error_
= true;
8317 // Check whether any of the local methods overloads an existing
8318 // struct field or interface method. We don't need to check the
8319 // list of methods against itself: that is handled by the Bindings
8321 if (this->local_methods_
!= NULL
)
8323 Struct_type
* st
= this->type_
->struct_type();
8326 for (Bindings::const_declarations_iterator p
=
8327 this->local_methods_
->begin_declarations();
8328 p
!= this->local_methods_
->end_declarations();
8331 const std::string
& name(p
->first
);
8332 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
8334 error_at(p
->second
->location(),
8335 "method %qs redeclares struct field name",
8336 Gogo::message_name(name
).c_str());
8345 // Return whether this type is or contains a pointer.
8348 Named_type::do_has_pointer() const
8353 bool ret
= this->type_
->has_pointer();
8354 this->seen_
= false;
8358 // Return whether comparisons for this type can use the identity
8362 Named_type::do_compare_is_identity(Gogo
* gogo
)
8364 // We don't use this->seen_ here because compare_is_identity may
8365 // call base() later, and that will mess up if seen_ is set here.
8366 if (this->seen_in_compare_is_identity_
)
8368 this->seen_in_compare_is_identity_
= true;
8369 bool ret
= this->type_
->compare_is_identity(gogo
);
8370 this->seen_in_compare_is_identity_
= false;
8374 // Return a hash code. This is used for method lookup. We simply
8375 // hash on the name itself.
8378 Named_type::do_hash_for_method(Gogo
* gogo
) const
8380 if (this->is_alias())
8381 return this->type_
->named_type()->do_hash_for_method(gogo
);
8383 const std::string
& name(this->named_object()->name());
8384 unsigned int ret
= Type::hash_string(name
, 0);
8386 // GOGO will be NULL here when called from Type_hash_identical.
8387 // That is OK because that is only used for internal hash tables
8388 // where we are going to be comparing named types for equality. In
8389 // other cases, which are cases where the runtime is going to
8390 // compare hash codes to see if the types are the same, we need to
8391 // include the pkgpath in the hash.
8392 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
8394 const Package
* package
= this->named_object()->package();
8395 if (package
== NULL
)
8396 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
8398 ret
= Type::hash_string(package
->pkgpath(), ret
);
8404 // Convert a named type to the backend representation. In order to
8405 // get dependencies right, we fill in a dummy structure for this type,
8406 // then convert all the dependencies, then complete this type. When
8407 // this function is complete, the size of the type is known.
8410 Named_type::convert(Gogo
* gogo
)
8412 if (this->is_error_
|| this->is_converted_
)
8415 this->create_placeholder(gogo
);
8417 // If we are called to turn unsafe.Sizeof into a constant, we may
8418 // not have verified the type yet. We have to make sure it is
8419 // verified, since that sets the list of dependencies.
8422 // Convert all the dependencies. If they refer indirectly back to
8423 // this type, they will pick up the intermediate representation we just
8425 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
8426 p
!= this->dependencies_
.end();
8428 (*p
)->convert(gogo
);
8430 // Complete this type.
8431 Btype
* bt
= this->named_btype_
;
8432 Type
* base
= this->type_
->base();
8433 switch (base
->classification())
8450 // The size of these types is already correct. We don't worry
8451 // about filling them in until later, when we also track
8452 // circular references.
8457 std::vector
<Backend::Btyped_identifier
> bfields
;
8458 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
8460 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8461 bt
= gogo
->backend()->error_type();
8466 // Slice types were completed in create_placeholder.
8467 if (!base
->is_slice_type())
8469 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
8470 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
8471 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
8472 bt
= gogo
->backend()->error_type();
8476 case TYPE_INTERFACE
:
8477 // Interface types were completed in create_placeholder.
8485 case TYPE_CALL_MULTIPLE_RESULT
:
8491 this->named_btype_
= bt
;
8492 this->is_converted_
= true;
8493 this->is_placeholder_
= false;
8496 // Create the placeholder for a named type. This is the first step in
8497 // converting to the backend representation.
8500 Named_type::create_placeholder(Gogo
* gogo
)
8502 if (this->is_error_
)
8503 this->named_btype_
= gogo
->backend()->error_type();
8505 if (this->named_btype_
!= NULL
)
8508 // Create the structure for this type. Note that because we call
8509 // base() here, we don't attempt to represent a named type defined
8510 // as another named type. Instead both named types will point to
8511 // different base representations.
8512 Type
* base
= this->type_
->base();
8514 bool set_name
= true;
8515 switch (base
->classification())
8518 this->is_error_
= true;
8519 this->named_btype_
= gogo
->backend()->error_type();
8529 // These are simple basic types, we can just create them
8531 bt
= Type::get_named_base_btype(gogo
, base
);
8536 // All maps and channels have the same backend representation.
8537 bt
= Type::get_named_base_btype(gogo
, base
);
8543 bool for_function
= base
->classification() == TYPE_FUNCTION
;
8544 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
8552 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8554 this->is_placeholder_
= true;
8559 if (base
->is_slice_type())
8560 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8564 bt
= gogo
->backend()->placeholder_array_type(this->name(),
8566 this->is_placeholder_
= true;
8571 case TYPE_INTERFACE
:
8572 if (base
->interface_type()->is_empty())
8573 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
8576 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8584 case TYPE_CALL_MULTIPLE_RESULT
:
8591 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
8593 this->named_btype_
= bt
;
8595 if (base
->is_slice_type())
8597 // We do not record slices as dependencies of other types,
8598 // because we can fill them in completely here with the final
8600 std::vector
<Backend::Btyped_identifier
> bfields
;
8601 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
8602 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8603 this->named_btype_
= gogo
->backend()->error_type();
8605 else if (base
->interface_type() != NULL
8606 && !base
->interface_type()->is_empty())
8608 // We do not record interfaces as dependencies of other types,
8609 // because we can fill them in completely here with the final
8611 std::vector
<Backend::Btyped_identifier
> bfields
;
8612 get_backend_interface_fields(gogo
, base
->interface_type(), true,
8614 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8615 this->named_btype_
= gogo
->backend()->error_type();
8619 // Get the backend representation for a named type.
8622 Named_type::do_get_backend(Gogo
* gogo
)
8624 if (this->is_error_
)
8625 return gogo
->backend()->error_type();
8627 Btype
* bt
= this->named_btype_
;
8629 if (!gogo
->named_types_are_converted())
8631 // We have not completed converting named types. NAMED_BTYPE_
8632 // is a placeholder and we shouldn't do anything further.
8636 // We don't build dependencies for types whose sizes do not
8637 // change or are not relevant, so we may see them here while
8638 // converting types.
8639 this->create_placeholder(gogo
);
8640 bt
= this->named_btype_
;
8641 go_assert(bt
!= NULL
);
8645 // We are not converting types. This should only be called if the
8646 // type has already been converted.
8647 if (!this->is_converted_
)
8649 go_assert(saw_errors());
8650 return gogo
->backend()->error_type();
8653 go_assert(bt
!= NULL
);
8655 // Complete the backend representation.
8656 Type
* base
= this->type_
->base();
8658 switch (base
->classification())
8661 return gogo
->backend()->error_type();
8675 if (!this->seen_in_get_backend_
)
8677 this->seen_in_get_backend_
= true;
8678 base
->struct_type()->finish_backend_fields(gogo
);
8679 this->seen_in_get_backend_
= false;
8684 if (!this->seen_in_get_backend_
)
8686 this->seen_in_get_backend_
= true;
8687 base
->array_type()->finish_backend_element(gogo
);
8688 this->seen_in_get_backend_
= false;
8692 case TYPE_INTERFACE
:
8693 if (!this->seen_in_get_backend_
)
8695 this->seen_in_get_backend_
= true;
8696 base
->interface_type()->finish_backend_methods(gogo
);
8697 this->seen_in_get_backend_
= false;
8702 // Don't build a circular data structure. GENERIC can't handle
8704 if (this->seen_in_get_backend_
)
8706 this->is_circular_
= true;
8707 return gogo
->backend()->circular_pointer_type(bt
, false);
8709 this->seen_in_get_backend_
= true;
8710 bt1
= Type::get_named_base_btype(gogo
, base
);
8711 this->seen_in_get_backend_
= false;
8712 if (this->is_circular_
)
8713 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
8714 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
8715 bt
= gogo
->backend()->error_type();
8719 // Don't build a circular data structure. GENERIC can't handle
8721 if (this->seen_in_get_backend_
)
8723 this->is_circular_
= true;
8724 return gogo
->backend()->circular_pointer_type(bt
, false);
8726 this->seen_in_get_backend_
= true;
8727 bt1
= Type::get_named_base_btype(gogo
, base
);
8728 this->seen_in_get_backend_
= false;
8729 if (this->is_circular_
)
8730 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
8731 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
8732 bt
= gogo
->backend()->error_type();
8737 case TYPE_CALL_MULTIPLE_RESULT
:
8746 // Build a type descriptor for a named type.
8749 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8751 if (name
== NULL
&& this->is_alias())
8752 return this->type_
->type_descriptor(gogo
, this->type_
);
8754 // If NAME is not NULL, then we don't really want the type
8755 // descriptor for this type; we want the descriptor for the
8756 // underlying type, giving it the name NAME.
8757 return this->named_type_descriptor(gogo
, this->type_
,
8758 name
== NULL
? this : name
);
8761 // Add to the reflection string. This is used mostly for the name of
8762 // the type used in a type descriptor, not for actual reflection
8766 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8768 if (this->is_alias())
8770 this->append_reflection(this->type_
, gogo
, ret
);
8773 if (!this->is_builtin())
8775 // We handle -fgo-prefix and -fgo-pkgpath differently here for
8776 // compatibility with how the compiler worked before
8777 // -fgo-pkgpath was introduced. When -fgo-pkgpath is specified,
8778 // we use it to make a unique reflection string, so that the
8779 // type canonicalization in the reflect package will work. In
8780 // order to be compatible with the gc compiler, we put tabs into
8781 // the package path, so that the reflect methods can discard it.
8782 const Package
* package
= this->named_object_
->package();
8783 if (gogo
->pkgpath_from_option())
8785 ret
->push_back('\t');
8786 ret
->append(package
!= NULL
8787 ? package
->pkgpath_symbol()
8788 : gogo
->pkgpath_symbol());
8789 ret
->push_back('\t');
8791 ret
->append(package
!= NULL
8792 ? package
->package_name()
8793 : gogo
->package_name());
8794 ret
->push_back('.');
8796 if (this->in_function_
!= NULL
)
8798 ret
->push_back('\t');
8799 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
8800 ret
->push_back('$');
8801 if (this->in_function_index_
> 0)
8804 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
8806 ret
->push_back('$');
8808 ret
->push_back('\t');
8810 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
8813 // Get the mangled name.
8816 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8818 if (this->is_alias())
8820 this->append_mangled_name(this->type_
, gogo
, ret
);
8823 Named_object
* no
= this->named_object_
;
8825 if (this->is_builtin())
8826 go_assert(this->in_function_
== NULL
);
8829 const std::string
& pkgpath(no
->package() == NULL
8830 ? gogo
->pkgpath_symbol()
8831 : no
->package()->pkgpath_symbol());
8833 name
.append(1, '.');
8834 if (this->in_function_
!= NULL
)
8836 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
8837 name
.append(1, '$');
8838 if (this->in_function_index_
> 0)
8841 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
8843 name
.append(1, '$');
8847 name
.append(Gogo::unpack_hidden_name(no
->name()));
8849 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
8854 // Export the type. This is called to export a global type.
8857 Named_type::export_named_type(Export
* exp
, const std::string
&) const
8859 // We don't need to write the name of the type here, because it will
8860 // be written by Export::write_type anyhow.
8861 exp
->write_c_string("type ");
8862 exp
->write_type(this);
8863 exp
->write_c_string(";\n");
8866 // Import a named type.
8869 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
8871 imp
->require_c_string("type ");
8872 Type
*type
= imp
->read_type();
8873 *ptype
= type
->named_type();
8874 go_assert(*ptype
!= NULL
);
8875 imp
->require_c_string(";\n");
8878 // Export the type when it is referenced by another type. In this
8879 // case Export::export_type will already have issued the name.
8882 Named_type::do_export(Export
* exp
) const
8884 exp
->write_type(this->type_
);
8886 // To save space, we only export the methods directly attached to
8888 Bindings
* methods
= this->local_methods_
;
8889 if (methods
== NULL
)
8892 exp
->write_c_string("\n");
8893 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
8894 p
!= methods
->end_definitions();
8897 exp
->write_c_string(" ");
8898 (*p
)->export_named_object(exp
);
8901 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
8902 p
!= methods
->end_declarations();
8905 if (p
->second
->is_function_declaration())
8907 exp
->write_c_string(" ");
8908 p
->second
->export_named_object(exp
);
8913 // Make a named type.
8916 Type::make_named_type(Named_object
* named_object
, Type
* type
,
8919 return new Named_type(named_object
, type
, location
);
8922 // Finalize the methods for TYPE. It will be a named type or a struct
8923 // type. This sets *ALL_METHODS to the list of methods, and builds
8924 // all required stubs.
8927 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
8928 Methods
** all_methods
)
8930 *all_methods
= NULL
;
8931 std::vector
<const Named_type
*> seen
;
8932 Type::add_methods_for_type(type
, NULL
, 0, false, false, &seen
, all_methods
);
8933 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
8936 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8937 // build up the struct field indexes as we go. DEPTH is the depth of
8938 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8939 // adding these methods for an anonymous field with pointer type.
8940 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8941 // calls the real method. TYPES_SEEN is used to avoid infinite
8945 Type::add_methods_for_type(const Type
* type
,
8946 const Method::Field_indexes
* field_indexes
,
8948 bool is_embedded_pointer
,
8949 bool needs_stub_method
,
8950 std::vector
<const Named_type
*>* seen
,
8953 // Pointer types may not have methods.
8954 if (type
->points_to() != NULL
)
8957 const Named_type
* nt
= type
->named_type();
8960 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
8968 seen
->push_back(nt
);
8970 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
8971 is_embedded_pointer
, needs_stub_method
,
8975 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
8976 is_embedded_pointer
, needs_stub_method
,
8979 // If we are called with depth > 0, then we are looking at an
8980 // anonymous field of a struct. If such a field has interface type,
8981 // then we need to add the interface methods. We don't want to add
8982 // them when depth == 0, because we will already handle them
8983 // following the usual rules for an interface type.
8985 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
8991 // Add the local methods for the named type NT to *METHODS. The
8992 // parameters are as for add_methods_to_type.
8995 Type::add_local_methods_for_type(const Named_type
* nt
,
8996 const Method::Field_indexes
* field_indexes
,
8998 bool is_embedded_pointer
,
8999 bool needs_stub_method
,
9002 const Bindings
* local_methods
= nt
->local_methods();
9003 if (local_methods
== NULL
)
9006 if (*methods
== NULL
)
9007 *methods
= new Methods();
9009 for (Bindings::const_declarations_iterator p
=
9010 local_methods
->begin_declarations();
9011 p
!= local_methods
->end_declarations();
9014 Named_object
* no
= p
->second
;
9015 bool is_value_method
= (is_embedded_pointer
9016 || !Type::method_expects_pointer(no
));
9017 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
9018 (needs_stub_method
|| depth
> 0));
9019 if (!(*methods
)->insert(no
->name(), m
))
9024 // Add the embedded methods for TYPE to *METHODS. These are the
9025 // methods attached to anonymous fields. The parameters are as for
9026 // add_methods_to_type.
9029 Type::add_embedded_methods_for_type(const Type
* type
,
9030 const Method::Field_indexes
* field_indexes
,
9032 bool is_embedded_pointer
,
9033 bool needs_stub_method
,
9034 std::vector
<const Named_type
*>* seen
,
9037 // Look for anonymous fields in TYPE. TYPE has fields if it is a
9039 const Struct_type
* st
= type
->struct_type();
9043 const Struct_field_list
* fields
= st
->fields();
9048 for (Struct_field_list::const_iterator pf
= fields
->begin();
9049 pf
!= fields
->end();
9052 if (!pf
->is_anonymous())
9055 Type
* ftype
= pf
->type();
9056 bool is_pointer
= false;
9057 if (ftype
->points_to() != NULL
)
9059 ftype
= ftype
->points_to();
9062 Named_type
* fnt
= ftype
->named_type();
9065 // This is an error, but it will be diagnosed elsewhere.
9069 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
9070 sub_field_indexes
->next
= field_indexes
;
9071 sub_field_indexes
->field_index
= i
;
9073 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
9074 (is_embedded_pointer
|| is_pointer
),
9083 // If TYPE is an interface type, then add its method to *METHODS.
9084 // This is for interface methods attached to an anonymous field. The
9085 // parameters are as for add_methods_for_type.
9088 Type::add_interface_methods_for_type(const Type
* type
,
9089 const Method::Field_indexes
* field_indexes
,
9093 const Interface_type
* it
= type
->interface_type();
9097 const Typed_identifier_list
* imethods
= it
->methods();
9098 if (imethods
== NULL
)
9101 if (*methods
== NULL
)
9102 *methods
= new Methods();
9104 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
9105 pm
!= imethods
->end();
9108 Function_type
* fntype
= pm
->type()->function_type();
9111 // This is an error, but it should be reported elsewhere
9112 // when we look at the methods for IT.
9115 go_assert(!fntype
->is_method());
9116 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
9117 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
9118 field_indexes
, depth
);
9119 if (!(*methods
)->insert(pm
->name(), m
))
9124 // Build stub methods for TYPE as needed. METHODS is the set of
9125 // methods for the type. A stub method may be needed when a type
9126 // inherits a method from an anonymous field. When we need the
9127 // address of the method, as in a type descriptor, we need to build a
9128 // little stub which does the required field dereferences and jumps to
9129 // the real method. LOCATION is the location of the type definition.
9132 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
9135 if (methods
== NULL
)
9137 for (Methods::const_iterator p
= methods
->begin();
9138 p
!= methods
->end();
9141 Method
* m
= p
->second
;
9142 if (m
->is_ambiguous() || !m
->needs_stub_method())
9145 const std::string
& name(p
->first
);
9147 // Build a stub method.
9149 const Function_type
* fntype
= m
->type();
9151 static unsigned int counter
;
9153 snprintf(buf
, sizeof buf
, "$this%u", counter
);
9156 Type
* receiver_type
= const_cast<Type
*>(type
);
9157 if (!m
->is_value_method())
9158 receiver_type
= Type::make_pointer_type(receiver_type
);
9159 Location receiver_location
= m
->receiver_location();
9160 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
9163 const Typed_identifier_list
* fnparams
= fntype
->parameters();
9164 Typed_identifier_list
* stub_params
;
9165 if (fnparams
== NULL
|| fnparams
->empty())
9169 // We give each stub parameter a unique name.
9170 stub_params
= new Typed_identifier_list();
9171 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
9172 pp
!= fnparams
->end();
9176 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
9177 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
9183 const Typed_identifier_list
* fnresults
= fntype
->results();
9184 Typed_identifier_list
* stub_results
;
9185 if (fnresults
== NULL
|| fnresults
->empty())
9186 stub_results
= NULL
;
9189 // We create the result parameters without any names, since
9190 // we won't refer to them.
9191 stub_results
= new Typed_identifier_list();
9192 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
9193 pr
!= fnresults
->end();
9195 stub_results
->push_back(Typed_identifier("", pr
->type(),
9199 Function_type
* stub_type
= Type::make_function_type(receiver
,
9202 fntype
->location());
9203 if (fntype
->is_varargs())
9204 stub_type
->set_is_varargs();
9206 // We only create the function in the package which creates the
9208 const Package
* package
;
9209 if (type
->named_type() == NULL
)
9212 package
= type
->named_type()->named_object()->package();
9214 if (package
!= NULL
)
9215 stub
= Named_object::make_function_declaration(name
, package
,
9216 stub_type
, location
);
9219 stub
= gogo
->start_function(name
, stub_type
, false,
9220 fntype
->location());
9221 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
9222 fntype
->is_varargs(), location
);
9223 gogo
->finish_function(fntype
->location());
9225 if (type
->named_type() == NULL
&& stub
->is_function())
9226 stub
->func_value()->set_is_unnamed_type_stub_method();
9227 if (m
->nointerface() && stub
->is_function())
9228 stub
->func_value()->set_nointerface();
9231 m
->set_stub_object(stub
);
9235 // Build a stub method which adjusts the receiver as required to call
9236 // METHOD. RECEIVER_NAME is the name we used for the receiver.
9237 // PARAMS is the list of function parameters.
9240 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
9241 const char* receiver_name
,
9242 const Typed_identifier_list
* params
,
9246 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
9247 go_assert(receiver_object
!= NULL
);
9249 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
9250 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
9251 if (expr
->type()->points_to() == NULL
)
9252 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
9254 Expression_list
* arguments
;
9255 if (params
== NULL
|| params
->empty())
9259 arguments
= new Expression_list();
9260 for (Typed_identifier_list::const_iterator p
= params
->begin();
9264 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
9265 go_assert(param
!= NULL
);
9266 Expression
* param_ref
= Expression::make_var_reference(param
,
9268 arguments
->push_back(param_ref
);
9272 Expression
* func
= method
->bind_method(expr
, location
);
9273 go_assert(func
!= NULL
);
9274 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
9276 call
->set_hidden_fields_are_ok();
9278 Statement
* s
= Statement::make_return_from_call(call
, location
);
9279 Return_statement
* retstat
= s
->return_statement();
9280 if (retstat
!= NULL
)
9282 // We can return values with hidden fields from a stub. This is
9283 // necessary if the method is itself hidden.
9284 retstat
->set_hidden_fields_are_ok();
9286 gogo
->add_statement(s
);
9289 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
9290 // in reverse order.
9293 Type::apply_field_indexes(Expression
* expr
,
9294 const Method::Field_indexes
* field_indexes
,
9297 if (field_indexes
== NULL
)
9299 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
9300 Struct_type
* stype
= expr
->type()->deref()->struct_type();
9301 go_assert(stype
!= NULL
9302 && field_indexes
->field_index
< stype
->field_count());
9303 if (expr
->type()->struct_type() == NULL
)
9305 go_assert(expr
->type()->points_to() != NULL
);
9306 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
9307 go_assert(expr
->type()->struct_type() == stype
);
9309 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
9313 // Return whether NO is a method for which the receiver is a pointer.
9316 Type::method_expects_pointer(const Named_object
* no
)
9318 const Function_type
*fntype
;
9319 if (no
->is_function())
9320 fntype
= no
->func_value()->type();
9321 else if (no
->is_function_declaration())
9322 fntype
= no
->func_declaration_value()->type();
9325 return fntype
->receiver()->type()->points_to() != NULL
;
9328 // Given a set of methods for a type, METHODS, return the method NAME,
9329 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
9330 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
9331 // but is ambiguous (and return NULL).
9334 Type::method_function(const Methods
* methods
, const std::string
& name
,
9337 if (is_ambiguous
!= NULL
)
9338 *is_ambiguous
= false;
9339 if (methods
== NULL
)
9341 Methods::const_iterator p
= methods
->find(name
);
9342 if (p
== methods
->end())
9344 Method
* m
= p
->second
;
9345 if (m
->is_ambiguous())
9347 if (is_ambiguous
!= NULL
)
9348 *is_ambiguous
= true;
9354 // Return a pointer to the interface method table for TYPE for the
9355 // interface INTERFACE.
9358 Type::interface_method_table(Type
* type
,
9359 Interface_type
*interface
,
9361 Interface_method_tables
** method_tables
,
9362 Interface_method_tables
** pointer_tables
)
9364 go_assert(!interface
->is_empty());
9366 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
9369 *pimt
= new Interface_method_tables(5);
9371 std::pair
<Interface_type
*, Expression
*> val(interface
, NULL
);
9372 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
9374 Location loc
= Linemap::predeclared_location();
9377 // This is a new entry in the hash table.
9378 go_assert(ins
.first
->second
== NULL
);
9380 Expression::make_interface_mtable_ref(interface
, type
, is_pointer
, loc
);
9382 return Expression::make_unary(OPERATOR_AND
, ins
.first
->second
, loc
);
9385 // Look for field or method NAME for TYPE. Return an Expression for
9386 // the field or method bound to EXPR. If there is no such field or
9387 // method, give an appropriate error and return an error expression.
9390 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
9391 const std::string
& name
,
9394 if (type
->deref()->is_error_type())
9395 return Expression::make_error(location
);
9397 const Named_type
* nt
= type
->deref()->named_type();
9398 const Struct_type
* st
= type
->deref()->struct_type();
9399 const Interface_type
* it
= type
->interface_type();
9401 // If this is a pointer to a pointer, then it is possible that the
9402 // pointed-to type has methods.
9403 bool dereferenced
= false;
9407 && type
->points_to() != NULL
9408 && type
->points_to()->points_to() != NULL
)
9410 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
9411 type
= type
->points_to();
9412 if (type
->deref()->is_error_type())
9413 return Expression::make_error(location
);
9414 nt
= type
->points_to()->named_type();
9415 st
= type
->points_to()->struct_type();
9416 dereferenced
= true;
9419 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
9420 || expr
->is_addressable());
9421 std::vector
<const Named_type
*> seen
;
9422 bool is_method
= false;
9423 bool found_pointer_method
= false;
9426 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
9427 &seen
, NULL
, &is_method
,
9428 &found_pointer_method
, &ambig1
, &ambig2
))
9433 go_assert(st
!= NULL
);
9434 if (type
->struct_type() == NULL
)
9436 go_assert(type
->points_to() != NULL
);
9437 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
9439 go_assert(expr
->type()->struct_type() == st
);
9441 ret
= st
->field_reference(expr
, name
, location
);
9443 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
9444 ret
= Expression::make_interface_field_reference(expr
, name
,
9450 m
= nt
->method_function(name
, NULL
);
9451 else if (st
!= NULL
)
9452 m
= st
->method_function(name
, NULL
);
9455 go_assert(m
!= NULL
);
9456 if (dereferenced
&& m
->is_value_method())
9459 "calling value method requires explicit dereference");
9460 return Expression::make_error(location
);
9462 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
9463 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
9464 ret
= m
->bind_method(expr
, location
);
9466 go_assert(ret
!= NULL
);
9471 if (Gogo::is_erroneous_name(name
))
9473 // An error was already reported.
9475 else if (!ambig1
.empty())
9476 error_at(location
, "%qs is ambiguous via %qs and %qs",
9477 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
9479 else if (found_pointer_method
)
9480 error_at(location
, "method requires a pointer receiver");
9481 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
9483 ("reference to field %qs in object which "
9484 "has no fields or methods"),
9485 Gogo::message_name(name
).c_str());
9489 // The test for 'a' and 'z' is to handle builtin names,
9490 // which are not hidden.
9491 if (!Gogo::is_hidden_name(name
) && (name
[0] < 'a' || name
[0] > 'z'))
9492 is_unexported
= false;
9495 std::string unpacked
= Gogo::unpack_hidden_name(name
);
9497 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
9502 error_at(location
, "reference to unexported field or method %qs",
9503 Gogo::message_name(name
).c_str());
9505 error_at(location
, "reference to undefined field or method %qs",
9506 Gogo::message_name(name
).c_str());
9508 return Expression::make_error(location
);
9512 // Look in TYPE for a field or method named NAME, return true if one
9513 // is found. This looks through embedded anonymous fields and handles
9514 // ambiguity. If a method is found, sets *IS_METHOD to true;
9515 // otherwise, if a field is found, set it to false. If
9516 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9517 // whose address can not be taken. SEEN is used to avoid infinite
9518 // recursion on invalid types.
9520 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9521 // method we couldn't use because it requires a pointer. LEVEL is
9522 // used for recursive calls, and can be NULL for a non-recursive call.
9523 // When this function returns false because it finds that the name is
9524 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9525 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9526 // will be unchanged.
9528 // This function just returns whether or not there is a field or
9529 // method, and whether it is a field or method. It doesn't build an
9530 // expression to refer to it. If it is a method, we then look in the
9531 // list of all methods for the type. If it is a field, the search has
9532 // to be done again, looking only for fields, and building up the
9533 // expression as we go.
9536 Type::find_field_or_method(const Type
* type
,
9537 const std::string
& name
,
9538 bool receiver_can_be_pointer
,
9539 std::vector
<const Named_type
*>* seen
,
9542 bool* found_pointer_method
,
9543 std::string
* ambig1
,
9544 std::string
* ambig2
)
9546 // Named types can have locally defined methods.
9547 const Named_type
* nt
= type
->named_type();
9548 if (nt
== NULL
&& type
->points_to() != NULL
)
9549 nt
= type
->points_to()->named_type();
9552 Named_object
* no
= nt
->find_local_method(name
);
9555 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
9561 // Record that we have found a pointer method in order to
9562 // give a better error message if we don't find anything
9564 *found_pointer_method
= true;
9567 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9573 // We've already seen this type when searching for methods.
9579 // Interface types can have methods.
9580 const Interface_type
* it
= type
->interface_type();
9581 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
9587 // Struct types can have fields. They can also inherit fields and
9588 // methods from anonymous fields.
9589 const Struct_type
* st
= type
->deref()->struct_type();
9592 const Struct_field_list
* fields
= st
->fields();
9597 seen
->push_back(nt
);
9599 int found_level
= 0;
9600 bool found_is_method
= false;
9601 std::string found_ambig1
;
9602 std::string found_ambig2
;
9603 const Struct_field
* found_parent
= NULL
;
9604 for (Struct_field_list::const_iterator pf
= fields
->begin();
9605 pf
!= fields
->end();
9608 if (pf
->is_field_name(name
))
9616 if (!pf
->is_anonymous())
9619 if (pf
->type()->deref()->is_error_type()
9620 || pf
->type()->deref()->is_undefined())
9623 Named_type
* fnt
= pf
->type()->named_type();
9625 fnt
= pf
->type()->deref()->named_type();
9626 go_assert(fnt
!= NULL
);
9628 // Methods with pointer receivers on embedded field are
9629 // inherited by the pointer to struct, and also by the struct
9630 // type if the field itself is a pointer.
9631 bool can_be_pointer
= (receiver_can_be_pointer
9632 || pf
->type()->points_to() != NULL
);
9633 int sublevel
= level
== NULL
? 1 : *level
+ 1;
9635 std::string subambig1
;
9636 std::string subambig2
;
9637 bool subfound
= Type::find_field_or_method(fnt
,
9643 found_pointer_method
,
9648 if (!subambig1
.empty())
9650 // The name was found via this field, but is ambiguous.
9651 // if the ambiguity is lower or at the same level as
9652 // anything else we have already found, then we want to
9653 // pass the ambiguity back to the caller.
9654 if (found_level
== 0 || sublevel
<= found_level
)
9656 found_ambig1
= (Gogo::message_name(pf
->field_name())
9658 found_ambig2
= (Gogo::message_name(pf
->field_name())
9660 found_level
= sublevel
;
9666 // The name was found via this field. Use the level to see
9667 // if we want to use this one, or whether it introduces an
9669 if (found_level
== 0 || sublevel
< found_level
)
9671 found_level
= sublevel
;
9672 found_is_method
= sub_is_method
;
9673 found_ambig1
.clear();
9674 found_ambig2
.clear();
9675 found_parent
= &*pf
;
9677 else if (sublevel
> found_level
)
9679 else if (found_ambig1
.empty())
9681 // We found an ambiguity.
9682 go_assert(found_parent
!= NULL
);
9683 found_ambig1
= Gogo::message_name(found_parent
->field_name());
9684 found_ambig2
= Gogo::message_name(pf
->field_name());
9688 // We found an ambiguity, but we already know of one.
9689 // Just report the earlier one.
9694 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
9695 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
9696 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
9697 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
9702 if (found_level
== 0)
9704 else if (!found_ambig1
.empty())
9706 go_assert(!found_ambig1
.empty());
9707 ambig1
->assign(found_ambig1
);
9708 ambig2
->assign(found_ambig2
);
9710 *level
= found_level
;
9716 *level
= found_level
;
9717 *is_method
= found_is_method
;
9722 // Return whether NAME is an unexported field or method for TYPE.
9725 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
9726 const std::string
& name
,
9727 std::vector
<const Named_type
*>* seen
)
9729 const Named_type
* nt
= type
->named_type();
9731 nt
= type
->deref()->named_type();
9734 if (nt
->is_unexported_local_method(gogo
, name
))
9737 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9743 // We've already seen this type.
9749 const Interface_type
* it
= type
->interface_type();
9750 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
9753 type
= type
->deref();
9755 const Struct_type
* st
= type
->struct_type();
9756 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
9762 const Struct_field_list
* fields
= st
->fields();
9767 seen
->push_back(nt
);
9769 for (Struct_field_list::const_iterator pf
= fields
->begin();
9770 pf
!= fields
->end();
9773 if (pf
->is_anonymous()
9774 && !pf
->type()->deref()->is_error_type()
9775 && !pf
->type()->deref()->is_undefined())
9777 Named_type
* subtype
= pf
->type()->named_type();
9778 if (subtype
== NULL
)
9779 subtype
= pf
->type()->deref()->named_type();
9780 if (subtype
== NULL
)
9782 // This is an error, but it will be diagnosed elsewhere.
9785 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
9800 // Class Forward_declaration.
9802 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
9803 : Type(TYPE_FORWARD
),
9804 named_object_(named_object
->resolve()), warned_(false)
9806 go_assert(this->named_object_
->is_unknown()
9807 || this->named_object_
->is_type_declaration());
9810 // Return the named object.
9813 Forward_declaration_type::named_object()
9815 return this->named_object_
->resolve();
9819 Forward_declaration_type::named_object() const
9821 return this->named_object_
->resolve();
9824 // Return the name of the forward declared type.
9827 Forward_declaration_type::name() const
9829 return this->named_object()->name();
9832 // Warn about a use of a type which has been declared but not defined.
9835 Forward_declaration_type::warn() const
9837 Named_object
* no
= this->named_object_
->resolve();
9838 if (no
->is_unknown())
9840 // The name was not defined anywhere.
9843 error_at(this->named_object_
->location(),
9844 "use of undefined type %qs",
9845 no
->message_name().c_str());
9846 this->warned_
= true;
9849 else if (no
->is_type_declaration())
9851 // The name was seen as a type, but the type was never defined.
9852 if (no
->type_declaration_value()->using_type())
9854 error_at(this->named_object_
->location(),
9855 "use of undefined type %qs",
9856 no
->message_name().c_str());
9857 this->warned_
= true;
9862 // The name was defined, but not as a type.
9865 error_at(this->named_object_
->location(), "expected type");
9866 this->warned_
= true;
9871 // Get the base type of a declaration. This gives an error if the
9872 // type has not yet been defined.
9875 Forward_declaration_type::real_type()
9877 if (this->is_defined())
9878 return this->named_object()->type_value();
9882 return Type::make_error_type();
9887 Forward_declaration_type::real_type() const
9889 if (this->is_defined())
9890 return this->named_object()->type_value();
9894 return Type::make_error_type();
9898 // Return whether the base type is defined.
9901 Forward_declaration_type::is_defined() const
9903 return this->named_object()->is_type();
9906 // Add a method. This is used when methods are defined before the
9910 Forward_declaration_type::add_method(const std::string
& name
,
9913 Named_object
* no
= this->named_object();
9914 if (no
->is_unknown())
9915 no
->declare_as_type();
9916 return no
->type_declaration_value()->add_method(name
, function
);
9919 // Add a method declaration. This is used when methods are declared
9923 Forward_declaration_type::add_method_declaration(const std::string
& name
,
9925 Function_type
* type
,
9928 Named_object
* no
= this->named_object();
9929 if (no
->is_unknown())
9930 no
->declare_as_type();
9931 Type_declaration
* td
= no
->type_declaration_value();
9932 return td
->add_method_declaration(name
, package
, type
, location
);
9938 Forward_declaration_type::do_traverse(Traverse
* traverse
)
9940 if (this->is_defined()
9941 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
9942 return TRAVERSE_EXIT
;
9943 return TRAVERSE_CONTINUE
;
9949 Forward_declaration_type::do_verify()
9951 if (!this->is_defined() && !this->is_nil_constant_as_type())
9959 // Get the backend representation for the type.
9962 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
9964 if (this->is_defined())
9965 return Type::get_named_base_btype(gogo
, this->real_type());
9968 return gogo
->backend()->error_type();
9970 // We represent an undefined type as a struct with no fields. That
9971 // should work fine for the backend, since the same case can arise
9973 std::vector
<Backend::Btyped_identifier
> fields
;
9974 Btype
* bt
= gogo
->backend()->struct_type(fields
);
9975 return gogo
->backend()->named_type(this->name(), bt
,
9976 this->named_object()->location());
9979 // Build a type descriptor for a forwarded type.
9982 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9984 Location ploc
= Linemap::predeclared_location();
9985 if (!this->is_defined())
9986 return Expression::make_error(ploc
);
9989 Type
* t
= this->real_type();
9991 return this->named_type_descriptor(gogo
, t
, name
);
9993 return Expression::make_type_descriptor(t
, ploc
);
9997 // The reflection string.
10000 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
10002 this->append_reflection(this->real_type(), gogo
, ret
);
10005 // The mangled name.
10008 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
10010 if (this->is_defined())
10011 this->append_mangled_name(this->real_type(), gogo
, ret
);
10014 const Named_object
* no
= this->named_object();
10016 if (no
->package() == NULL
)
10017 name
= gogo
->pkgpath_symbol();
10019 name
= no
->package()->pkgpath_symbol();
10021 name
+= Gogo::unpack_hidden_name(no
->name());
10023 snprintf(buf
, sizeof buf
, "N%u_",
10024 static_cast<unsigned int>(name
.length()));
10030 // Export a forward declaration. This can happen when a defined type
10031 // refers to a type which is only declared (and is presumably defined
10032 // in some other file in the same package).
10035 Forward_declaration_type::do_export(Export
*) const
10037 // If there is a base type, that should be exported instead of this.
10038 go_assert(!this->is_defined());
10040 // We don't output anything.
10043 // Make a forward declaration.
10046 Type::make_forward_declaration(Named_object
* named_object
)
10048 return new Forward_declaration_type(named_object
);
10051 // Class Typed_identifier_list.
10053 // Sort the entries by name.
10055 struct Typed_identifier_list_sort
10059 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
10060 { return t1
.name() < t2
.name(); }
10064 Typed_identifier_list::sort_by_name()
10066 std::sort(this->entries_
.begin(), this->entries_
.end(),
10067 Typed_identifier_list_sort());
10073 Typed_identifier_list::traverse(Traverse
* traverse
)
10075 for (Typed_identifier_list::const_iterator p
= this->begin();
10079 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
10080 return TRAVERSE_EXIT
;
10082 return TRAVERSE_CONTINUE
;
10087 Typed_identifier_list
*
10088 Typed_identifier_list::copy() const
10090 Typed_identifier_list
* ret
= new Typed_identifier_list();
10091 for (Typed_identifier_list::const_iterator p
= this->begin();
10094 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));