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.
21 #include "expressions.h"
22 #include "statements.h"
28 // Forward declarations so that we don't have to make types.h #include
32 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
34 std::vector
<Backend::Btyped_identifier
>* bfields
);
37 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
38 std::vector
<Backend::Btyped_identifier
>* bfields
);
41 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
43 std::vector
<Backend::Btyped_identifier
>* bfields
);
47 Type::Type(Type_classification classification
)
48 : classification_(classification
), btype_is_placeholder_(false),
49 btype_(NULL
), type_descriptor_var_(NULL
)
57 // Get the base type for a type--skip names and forward declarations.
62 switch (this->classification_
)
65 return this->named_type()->named_base();
67 return this->forward_declaration_type()->real_type()->base();
76 switch (this->classification_
)
79 return this->named_type()->named_base();
81 return this->forward_declaration_type()->real_type()->base();
87 // Skip defined forward declarations.
93 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
94 while (ftype
!= NULL
&& ftype
->is_defined())
96 t
= ftype
->real_type();
97 ftype
= t
->forward_declaration_type();
103 Type::forwarded() const
105 const Type
* t
= this;
106 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
107 while (ftype
!= NULL
&& ftype
->is_defined())
109 t
= ftype
->real_type();
110 ftype
= t
->forward_declaration_type();
115 // If this is a named type, return it. Otherwise, return NULL.
120 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
124 Type::named_type() const
126 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
129 // Return true if this type is not defined.
132 Type::is_undefined() const
134 return this->forwarded()->forward_declaration_type() != NULL
;
137 // Return true if this is a basic type: a type which is not composed
138 // of other types, and is not void.
141 Type::is_basic_type() const
143 switch (this->classification_
)
166 return this->base()->is_basic_type();
173 // Return true if this is an abstract type.
176 Type::is_abstract() const
178 switch (this->classification())
181 return this->integer_type()->is_abstract();
183 return this->float_type()->is_abstract();
185 return this->complex_type()->is_abstract();
187 return this->is_abstract_string_type();
189 return this->is_abstract_boolean_type();
195 // Return a non-abstract version of an abstract type.
198 Type::make_non_abstract_type()
200 go_assert(this->is_abstract());
201 switch (this->classification())
204 if (this->integer_type()->is_rune())
205 return Type::lookup_integer_type("int32");
207 return Type::lookup_integer_type("int");
209 return Type::lookup_float_type("float64");
211 return Type::lookup_complex_type("complex128");
213 return Type::lookup_string_type();
215 return Type::lookup_bool_type();
221 // Return true if this is an error type. Don't give an error if we
222 // try to dereference an undefined forwarding type, as this is called
223 // in the parser when the type may legitimately be undefined.
226 Type::is_error_type() const
228 const Type
* t
= this->forwarded();
229 // Note that we return false for an undefined forward type.
230 switch (t
->classification_
)
235 return t
->named_type()->is_named_error_type();
241 // If this is a pointer type, return the type to which it points.
242 // Otherwise, return NULL.
245 Type::points_to() const
247 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
249 return ptype
== NULL
? NULL
: ptype
->points_to();
252 // Return whether this is an open array type.
255 Type::is_slice_type() const
257 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
260 // Return whether this is the predeclared constant nil being used as a
264 Type::is_nil_constant_as_type() const
266 const Type
* t
= this->forwarded();
267 if (t
->forward_declaration_type() != NULL
)
269 const Named_object
* no
= t
->forward_declaration_type()->named_object();
270 if (no
->is_unknown())
271 no
= no
->unknown_value()->real_named_object();
274 && no
->const_value()->expr()->is_nil_expression())
283 Type::traverse(Type
* type
, Traverse
* traverse
)
285 go_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
286 || (traverse
->traverse_mask()
287 & Traverse::traverse_expressions
) != 0);
288 if (traverse
->remember_type(type
))
290 // We have already traversed this type.
291 return TRAVERSE_CONTINUE
;
293 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
295 int t
= traverse
->type(type
);
296 if (t
== TRAVERSE_EXIT
)
297 return TRAVERSE_EXIT
;
298 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
299 return TRAVERSE_CONTINUE
;
301 // An array type has an expression which we need to traverse if
302 // traverse_expressions is set.
303 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
304 return TRAVERSE_EXIT
;
305 return TRAVERSE_CONTINUE
;
308 // Default implementation for do_traverse for child class.
311 Type::do_traverse(Traverse
*)
313 return TRAVERSE_CONTINUE
;
316 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
317 // then return true for all erroneous types; this is used to avoid
318 // cascading errors. If REASON is not NULL, optionally set *REASON to
319 // the reason the types are not identical.
322 Type::are_identical(const Type
* t1
, const Type
* t2
, bool errors_are_identical
,
325 if (t1
== NULL
|| t2
== NULL
)
327 // Something is wrong.
328 return errors_are_identical
? true : t1
== t2
;
331 // Skip defined forward declarations.
332 t1
= t1
->forwarded();
333 t2
= t2
->forwarded();
335 // Ignore aliases for purposes of type identity.
336 if (t1
->named_type() != NULL
&& t1
->named_type()->is_alias())
337 t1
= t1
->named_type()->real_type();
338 if (t2
->named_type() != NULL
&& t2
->named_type()->is_alias())
339 t2
= t2
->named_type()->real_type();
344 // An undefined forward declaration is an error.
345 if (t1
->forward_declaration_type() != NULL
346 || t2
->forward_declaration_type() != NULL
)
347 return errors_are_identical
;
349 // Avoid cascading errors with error types.
350 if (t1
->is_error_type() || t2
->is_error_type())
352 if (errors_are_identical
)
354 return t1
->is_error_type() && t2
->is_error_type();
357 // Get a good reason for the sink type. Note that the sink type on
358 // the left hand side of an assignment is handled in are_assignable.
359 if (t1
->is_sink_type() || t2
->is_sink_type())
362 *reason
= "invalid use of _";
366 // A named type is only identical to itself.
367 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
370 // Check type shapes.
371 if (t1
->classification() != t2
->classification())
374 switch (t1
->classification())
380 // These types are always identical.
384 return t1
->integer_type()->is_identical(t2
->integer_type());
387 return t1
->float_type()->is_identical(t2
->float_type());
390 return t1
->complex_type()->is_identical(t2
->complex_type());
393 return t1
->function_type()->is_identical(t2
->function_type(),
395 errors_are_identical
,
399 return Type::are_identical(t1
->points_to(), t2
->points_to(),
400 errors_are_identical
, reason
);
403 return t1
->struct_type()->is_identical(t2
->struct_type(),
404 errors_are_identical
);
407 return t1
->array_type()->is_identical(t2
->array_type(),
408 errors_are_identical
);
411 return t1
->map_type()->is_identical(t2
->map_type(),
412 errors_are_identical
);
415 return t1
->channel_type()->is_identical(t2
->channel_type(),
416 errors_are_identical
);
419 return t1
->interface_type()->is_identical(t2
->interface_type(),
420 errors_are_identical
);
422 case TYPE_CALL_MULTIPLE_RESULT
:
424 *reason
= "invalid use of multiple-value function call";
432 // Return true if it's OK to have a binary operation with types LHS
433 // and RHS. This is not used for shifts or comparisons.
436 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
438 if (Type::are_identical(lhs
, rhs
, true, NULL
))
441 // A constant of abstract bool type may be mixed with any bool type.
442 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
443 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
446 // A constant of abstract string type may be mixed with any string
448 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
449 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
455 // A constant of abstract integer, float, or complex type may be
456 // mixed with an integer, float, or complex type.
457 if ((rhs
->is_abstract()
458 && (rhs
->integer_type() != NULL
459 || rhs
->float_type() != NULL
460 || rhs
->complex_type() != NULL
)
461 && (lhs
->integer_type() != NULL
462 || lhs
->float_type() != NULL
463 || lhs
->complex_type() != NULL
))
464 || (lhs
->is_abstract()
465 && (lhs
->integer_type() != NULL
466 || lhs
->float_type() != NULL
467 || lhs
->complex_type() != NULL
)
468 && (rhs
->integer_type() != NULL
469 || rhs
->float_type() != NULL
470 || rhs
->complex_type() != NULL
)))
473 // The nil type may be compared to a pointer, an interface type, a
474 // slice type, a channel type, a map type, or a function type.
475 if (lhs
->is_nil_type()
476 && (rhs
->points_to() != NULL
477 || rhs
->interface_type() != NULL
478 || rhs
->is_slice_type()
479 || rhs
->map_type() != NULL
480 || rhs
->channel_type() != NULL
481 || rhs
->function_type() != NULL
))
483 if (rhs
->is_nil_type()
484 && (lhs
->points_to() != NULL
485 || lhs
->interface_type() != NULL
486 || lhs
->is_slice_type()
487 || lhs
->map_type() != NULL
488 || lhs
->channel_type() != NULL
489 || lhs
->function_type() != NULL
))
495 // Return true if a value with type T1 may be compared with a value of
496 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
499 Type::are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
500 const Type
*t2
, std::string
*reason
)
503 && !Type::are_assignable(t1
, t2
, NULL
)
504 && !Type::are_assignable(t2
, t1
, NULL
))
507 *reason
= "incompatible types in binary expression";
513 if (t1
->integer_type() == NULL
514 && t1
->float_type() == NULL
515 && !t1
->is_string_type())
518 *reason
= _("invalid comparison of non-ordered type");
522 else if (t1
->is_slice_type()
523 || t1
->map_type() != NULL
524 || t1
->function_type() != NULL
525 || t2
->is_slice_type()
526 || t2
->map_type() != NULL
527 || t2
->function_type() != NULL
)
529 if (!t1
->is_nil_type() && !t2
->is_nil_type())
533 if (t1
->is_slice_type() || t2
->is_slice_type())
534 *reason
= _("slice can only be compared to nil");
535 else if (t1
->map_type() != NULL
|| t2
->map_type() != NULL
)
536 *reason
= _("map can only be compared to nil");
538 *reason
= _("func can only be compared to nil");
540 // Match 6g error messages.
541 if (t1
->interface_type() != NULL
|| t2
->interface_type() != NULL
)
544 snprintf(buf
, sizeof buf
, _("invalid operation (%s)"),
554 if (!t1
->is_boolean_type()
555 && t1
->integer_type() == NULL
556 && t1
->float_type() == NULL
557 && t1
->complex_type() == NULL
558 && !t1
->is_string_type()
559 && t1
->points_to() == NULL
560 && t1
->channel_type() == NULL
561 && t1
->interface_type() == NULL
562 && t1
->struct_type() == NULL
563 && t1
->array_type() == NULL
564 && !t1
->is_nil_type())
567 *reason
= _("invalid comparison of non-comparable type");
571 if (t1
->named_type() != NULL
)
572 return t1
->named_type()->named_type_is_comparable(reason
);
573 else if (t2
->named_type() != NULL
)
574 return t2
->named_type()->named_type_is_comparable(reason
);
575 else if (t1
->struct_type() != NULL
)
577 const Struct_field_list
* fields
= t1
->struct_type()->fields();
578 for (Struct_field_list::const_iterator p
= fields
->begin();
582 if (Gogo::is_sink_name(p
->field_name()))
585 if (!p
->type()->is_comparable())
588 *reason
= _("invalid comparison of non-comparable struct");
593 else if (t1
->array_type() != NULL
)
595 if (t1
->array_type()->length()->is_nil_expression()
596 || !t1
->array_type()->element_type()->is_comparable())
599 *reason
= _("invalid comparison of non-comparable array");
608 // Return true if a value with type RHS may be assigned to a variable
609 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
610 // hidden fields are modified. If REASON is not NULL, set *REASON to
611 // the reason the types are not assignable.
614 Type::are_assignable_check_hidden(const Type
* lhs
, const Type
* rhs
,
615 bool check_hidden_fields
,
618 // Do some checks first. Make sure the types are defined.
619 if (rhs
!= NULL
&& !rhs
->is_undefined())
621 if (rhs
->is_void_type())
624 *reason
= "non-value used as value";
627 if (rhs
->is_call_multiple_result_type())
630 reason
->assign(_("multiple-value function call in "
631 "single-value context"));
636 if (lhs
!= NULL
&& !lhs
->is_undefined())
638 // Any value may be assigned to the blank identifier.
639 if (lhs
->is_sink_type())
642 // All fields of a struct must be exported, or the assignment
643 // must be in the same package.
644 if (check_hidden_fields
&& rhs
!= NULL
&& !rhs
->is_undefined())
646 if (lhs
->has_hidden_fields(NULL
, reason
)
647 || rhs
->has_hidden_fields(NULL
, reason
))
652 // Identical types are assignable.
653 if (Type::are_identical(lhs
, rhs
, true, reason
))
656 // The types are assignable if they have identical underlying types
657 // and either LHS or RHS is not a named type.
658 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
659 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
660 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
663 // The types are assignable if LHS is an interface type and RHS
664 // implements the required methods.
665 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
666 if (lhs_interface_type
!= NULL
)
668 if (lhs_interface_type
->implements_interface(rhs
, reason
))
670 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
671 if (rhs_interface_type
!= NULL
672 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
677 // The type are assignable if RHS is a bidirectional channel type,
678 // LHS is a channel type, they have identical element types, and
679 // either LHS or RHS is not a named type.
680 if (lhs
->channel_type() != NULL
681 && rhs
->channel_type() != NULL
682 && rhs
->channel_type()->may_send()
683 && rhs
->channel_type()->may_receive()
684 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
685 && Type::are_identical(lhs
->channel_type()->element_type(),
686 rhs
->channel_type()->element_type(),
691 // The nil type may be assigned to a pointer, function, slice, map,
692 // channel, or interface type.
693 if (rhs
->is_nil_type()
694 && (lhs
->points_to() != NULL
695 || lhs
->function_type() != NULL
696 || lhs
->is_slice_type()
697 || lhs
->map_type() != NULL
698 || lhs
->channel_type() != NULL
699 || lhs
->interface_type() != NULL
))
702 // An untyped numeric constant may be assigned to a numeric type if
703 // it is representable in that type.
704 if ((rhs
->is_abstract()
705 && (rhs
->integer_type() != NULL
706 || rhs
->float_type() != NULL
707 || rhs
->complex_type() != NULL
))
708 && (lhs
->integer_type() != NULL
709 || lhs
->float_type() != NULL
710 || lhs
->complex_type() != NULL
))
713 // Give some better error messages.
714 if (reason
!= NULL
&& reason
->empty())
716 if (rhs
->interface_type() != NULL
)
717 reason
->assign(_("need explicit conversion"));
718 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
720 size_t len
= (lhs
->named_type()->name().length()
721 + rhs
->named_type()->name().length()
723 char* buf
= new char[len
];
724 snprintf(buf
, len
, _("cannot use type %s as type %s"),
725 rhs
->named_type()->message_name().c_str(),
726 lhs
->named_type()->message_name().c_str());
735 // Return true if a value with type RHS may be assigned to a variable
736 // with type LHS. If REASON is not NULL, set *REASON to the reason
737 // the types are not assignable.
740 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
742 return Type::are_assignable_check_hidden(lhs
, rhs
, false, reason
);
745 // Like are_assignable but don't check for hidden fields.
748 Type::are_assignable_hidden_ok(const Type
* lhs
, const Type
* rhs
,
751 return Type::are_assignable_check_hidden(lhs
, rhs
, false, reason
);
754 // Return true if a value with type RHS may be converted to type LHS.
755 // If REASON is not NULL, set *REASON to the reason the types are not
759 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
761 // The types are convertible if they are assignable.
762 if (Type::are_assignable(lhs
, rhs
, reason
))
765 // The types are convertible if they have identical underlying
767 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
768 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
771 // The types are convertible if they are both unnamed pointer types
772 // and their pointer base types have identical underlying types.
773 if (lhs
->named_type() == NULL
774 && rhs
->named_type() == NULL
775 && lhs
->points_to() != NULL
776 && rhs
->points_to() != NULL
777 && (lhs
->points_to()->named_type() != NULL
778 || rhs
->points_to()->named_type() != NULL
)
779 && Type::are_identical(lhs
->points_to()->base(),
780 rhs
->points_to()->base(),
785 // Integer and floating point types are convertible to each other.
786 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
787 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
790 // Complex types are convertible to each other.
791 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
794 // An integer, or []byte, or []rune, may be converted to a string.
795 if (lhs
->is_string_type())
797 if (rhs
->integer_type() != NULL
)
799 if (rhs
->is_slice_type())
801 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
802 if (e
->integer_type() != NULL
803 && (e
->integer_type()->is_byte()
804 || e
->integer_type()->is_rune()))
809 // A string may be converted to []byte or []rune.
810 if (rhs
->is_string_type() && lhs
->is_slice_type())
812 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
813 if (e
->integer_type() != NULL
814 && (e
->integer_type()->is_byte() || e
->integer_type()->is_rune()))
818 // An unsafe.Pointer type may be converted to any pointer type or to
819 // uintptr, and vice-versa.
820 if (lhs
->is_unsafe_pointer_type()
821 && (rhs
->points_to() != NULL
822 || (rhs
->integer_type() != NULL
823 && rhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
825 if (rhs
->is_unsafe_pointer_type()
826 && (lhs
->points_to() != NULL
827 || (lhs
->integer_type() != NULL
828 && lhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
831 // Give a better error message.
835 *reason
= "invalid type conversion";
838 std::string s
= "invalid type conversion (";
848 // Return whether this type has any hidden fields. This is only a
849 // possibility for a few types.
852 Type::has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
854 switch (this->forwarded()->classification_
)
857 return this->named_type()->named_type_has_hidden_fields(reason
);
859 return this->struct_type()->struct_has_hidden_fields(within
, reason
);
861 return this->array_type()->array_has_hidden_fields(within
, reason
);
867 // Return a hash code for the type to be used for method lookup.
870 Type::hash_for_method(Gogo
* gogo
) const
872 unsigned int ret
= 0;
873 if (this->classification_
!= TYPE_FORWARD
)
874 ret
+= this->classification_
;
875 return ret
+ this->do_hash_for_method(gogo
);
878 // Default implementation of do_hash_for_method. This is appropriate
879 // for types with no subfields.
882 Type::do_hash_for_method(Gogo
*) const
887 // Return a hash code for a string, given a starting hash.
890 Type::hash_string(const std::string
& s
, unsigned int h
)
892 const char* p
= s
.data();
893 size_t len
= s
.length();
894 for (; len
> 0; --len
)
902 // A hash table mapping unnamed types to the backend representation of
905 Type::Type_btypes
Type::type_btypes
;
907 // Return a tree representing this type.
910 Type::get_backend(Gogo
* gogo
)
912 if (this->btype_
!= NULL
)
914 if (this->btype_is_placeholder_
&& gogo
->named_types_are_converted())
915 this->finish_backend(gogo
);
919 if (this->forward_declaration_type() != NULL
920 || this->named_type() != NULL
)
921 return this->get_btype_without_hash(gogo
);
923 if (this->is_error_type())
924 return gogo
->backend()->error_type();
926 // To avoid confusing the backend, translate all identical Go types
927 // to the same backend representation. We use a hash table to do
928 // that. There is no need to use the hash table for named types, as
929 // named types are only identical to themselves.
931 std::pair
<Type
*, Btype
*> val(this, NULL
);
932 std::pair
<Type_btypes::iterator
, bool> ins
=
933 Type::type_btypes
.insert(val
);
934 if (!ins
.second
&& ins
.first
->second
!= NULL
)
936 if (gogo
!= NULL
&& gogo
->named_types_are_converted())
937 this->btype_
= ins
.first
->second
;
938 return ins
.first
->second
;
941 Btype
* bt
= this->get_btype_without_hash(gogo
);
943 if (ins
.first
->second
== NULL
)
944 ins
.first
->second
= bt
;
947 // We have already created a backend representation for this
948 // type. This can happen when an unnamed type is defined using
949 // a named type which in turns uses an identical unnamed type.
950 // Use the tree we created earlier and ignore the one we just
952 bt
= ins
.first
->second
;
953 if (gogo
== NULL
|| !gogo
->named_types_are_converted())
961 // Return the backend representation for a type without looking in the
962 // hash table for identical types. This is used for named types,
963 // since a named type is never identical to any other type.
966 Type::get_btype_without_hash(Gogo
* gogo
)
968 if (this->btype_
== NULL
)
970 Btype
* bt
= this->do_get_backend(gogo
);
972 // For a recursive function or pointer type, we will temporarily
973 // return a circular pointer type during the recursion. We
974 // don't want to record that for a forwarding type, as it may
976 if (this->forward_declaration_type() != NULL
977 && gogo
->backend()->is_circular_pointer_type(bt
))
980 if (gogo
== NULL
|| !gogo
->named_types_are_converted())
988 // Get the backend representation of a type without forcing the
989 // creation of the backend representation of all supporting types.
990 // This will return a backend type that has the correct size but may
991 // be incomplete. E.g., a pointer will just be a placeholder pointer,
992 // and will not contain the final representation of the type to which
993 // it points. This is used while converting all named types to the
994 // backend representation, to avoid problems with indirect references
995 // to types which are not yet complete. When this is called, the
996 // sizes of all direct references (e.g., a struct field) should be
997 // known, but the sizes of indirect references (e.g., the type to
998 // which a pointer points) may not.
1001 Type::get_backend_placeholder(Gogo
* gogo
)
1003 if (gogo
->named_types_are_converted())
1004 return this->get_backend(gogo
);
1005 if (this->btype_
!= NULL
)
1006 return this->btype_
;
1009 switch (this->classification_
)
1019 // These are simple types that can just be created directly.
1020 return this->get_backend(gogo
);
1024 Location loc
= this->function_type()->location();
1025 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, true);
1031 Location loc
= Linemap::unknown_location();
1032 bt
= gogo
->backend()->placeholder_pointer_type("", loc
, false);
1037 // We don't have to make the struct itself be a placeholder. We
1038 // are promised that we know the sizes of the struct fields.
1039 // But we may have to use a placeholder for any particular
1042 std::vector
<Backend::Btyped_identifier
> bfields
;
1043 get_backend_struct_fields(gogo
, this->struct_type()->fields(),
1045 bt
= gogo
->backend()->struct_type(bfields
);
1050 if (this->is_slice_type())
1052 std::vector
<Backend::Btyped_identifier
> bfields
;
1053 get_backend_slice_fields(gogo
, this->array_type(), true, &bfields
);
1054 bt
= gogo
->backend()->struct_type(bfields
);
1058 Btype
* element
= this->array_type()->get_backend_element(gogo
, true);
1059 Bexpression
* len
= this->array_type()->get_backend_length(gogo
);
1060 bt
= gogo
->backend()->array_type(element
, len
);
1066 // All maps and channels have the same backend representation.
1067 return this->get_backend(gogo
);
1069 case TYPE_INTERFACE
:
1070 if (this->interface_type()->is_empty())
1071 return Interface_type::get_backend_empty_interface_type(gogo
);
1074 std::vector
<Backend::Btyped_identifier
> bfields
;
1075 get_backend_interface_fields(gogo
, this->interface_type(), true,
1077 bt
= gogo
->backend()->struct_type(bfields
);
1083 // Named types keep track of their own dependencies and manage
1084 // their own placeholders.
1085 return this->get_backend(gogo
);
1088 case TYPE_CALL_MULTIPLE_RESULT
:
1094 this->btype_is_placeholder_
= true;
1098 // Complete the backend representation. This is called for a type
1099 // using a placeholder type.
1102 Type::finish_backend(Gogo
* gogo
)
1104 go_assert(this->btype_
!= NULL
);
1105 if (!this->btype_is_placeholder_
)
1108 switch (this->classification_
)
1122 Btype
* bt
= this->do_get_backend(gogo
);
1123 if (!gogo
->backend()->set_placeholder_function_type(this->btype_
, bt
))
1124 go_assert(saw_errors());
1130 Btype
* bt
= this->do_get_backend(gogo
);
1131 if (!gogo
->backend()->set_placeholder_pointer_type(this->btype_
, bt
))
1132 go_assert(saw_errors());
1137 // The struct type itself is done, but we have to make sure that
1138 // all the field types are converted.
1139 this->struct_type()->finish_backend_fields(gogo
);
1143 // The array type itself is done, but make sure the element type
1145 this->array_type()->finish_backend_element(gogo
);
1152 case TYPE_INTERFACE
:
1153 // The interface type itself is done, but make sure the method
1154 // types are converted.
1155 this->interface_type()->finish_backend_methods(gogo
);
1163 case TYPE_CALL_MULTIPLE_RESULT
:
1168 this->btype_is_placeholder_
= false;
1171 // Return a pointer to the type descriptor for this type.
1174 Type::type_descriptor_pointer(Gogo
* gogo
, Location location
)
1176 Type
* t
= this->forwarded();
1177 if (t
->named_type() != NULL
&& t
->named_type()->is_alias())
1178 t
= t
->named_type()->real_type();
1179 if (t
->type_descriptor_var_
== NULL
)
1181 t
->make_type_descriptor_var(gogo
);
1182 go_assert(t
->type_descriptor_var_
!= NULL
);
1184 tree var_tree
= var_to_tree(t
->type_descriptor_var_
);
1185 if (var_tree
== error_mark_node
)
1186 return error_mark_node
;
1187 return build_fold_addr_expr_loc(location
.gcc_location(), var_tree
);
1190 // A mapping from unnamed types to type descriptor variables.
1192 Type::Type_descriptor_vars
Type::type_descriptor_vars
;
1194 // Build the type descriptor for this type.
1197 Type::make_type_descriptor_var(Gogo
* gogo
)
1199 go_assert(this->type_descriptor_var_
== NULL
);
1201 Named_type
* nt
= this->named_type();
1203 // We can have multiple instances of unnamed types, but we only want
1204 // to emit the type descriptor once. We use a hash table. This is
1205 // not necessary for named types, as they are unique, and we store
1206 // the type descriptor in the type itself.
1207 Bvariable
** phash
= NULL
;
1210 Bvariable
* bvnull
= NULL
;
1211 std::pair
<Type_descriptor_vars::iterator
, bool> ins
=
1212 Type::type_descriptor_vars
.insert(std::make_pair(this, bvnull
));
1215 // We've already build a type descriptor for this type.
1216 this->type_descriptor_var_
= ins
.first
->second
;
1219 phash
= &ins
.first
->second
;
1222 std::string var_name
= this->type_descriptor_var_name(gogo
, nt
);
1224 // Build the contents of the type descriptor.
1225 Expression
* initializer
= this->do_type_descriptor(gogo
, NULL
);
1227 Btype
* initializer_btype
= initializer
->type()->get_backend(gogo
);
1229 Location loc
= nt
== NULL
? Linemap::predeclared_location() : nt
->location();
1231 const Package
* dummy
;
1232 if (this->type_descriptor_defined_elsewhere(nt
, &dummy
))
1234 this->type_descriptor_var_
=
1235 gogo
->backend()->immutable_struct_reference(var_name
,
1239 *phash
= this->type_descriptor_var_
;
1243 // See if this type descriptor can appear in multiple packages.
1244 bool is_common
= false;
1247 // We create the descriptor for a builtin type whenever we need
1249 is_common
= nt
->is_builtin();
1253 // This is an unnamed type. The descriptor could be defined in
1254 // any package where it is needed, and the linker will pick one
1255 // descriptor to keep.
1259 // We are going to build the type descriptor in this package. We
1260 // must create the variable before we convert the initializer to the
1261 // backend representation, because the initializer may refer to the
1262 // type descriptor of this type. By setting type_descriptor_var_ we
1263 // ensure that type_descriptor_pointer will work if called while
1264 // converting INITIALIZER.
1266 this->type_descriptor_var_
=
1267 gogo
->backend()->immutable_struct(var_name
, is_common
, initializer_btype
,
1270 *phash
= this->type_descriptor_var_
;
1272 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
1273 context
.set_is_const();
1274 Bexpression
* binitializer
= tree_to_expr(initializer
->get_tree(&context
));
1276 gogo
->backend()->immutable_struct_set_init(this->type_descriptor_var_
,
1277 var_name
, is_common
,
1278 initializer_btype
, loc
,
1282 // Return the name of the type descriptor variable. If NT is not
1283 // NULL, use it to get the name. Otherwise this is an unnamed type.
1286 Type::type_descriptor_var_name(Gogo
* gogo
, Named_type
* nt
)
1289 return "__go_td_" + this->mangled_name(gogo
);
1291 Named_object
* no
= nt
->named_object();
1293 const Named_object
* in_function
= nt
->in_function(&index
);
1294 std::string ret
= "__go_tdn_";
1295 if (nt
->is_builtin())
1296 go_assert(in_function
== NULL
);
1299 const std::string
& pkgpath(no
->package() == NULL
1300 ? gogo
->pkgpath_symbol()
1301 : no
->package()->pkgpath_symbol());
1302 ret
.append(pkgpath
);
1304 if (in_function
!= NULL
)
1306 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
1311 snprintf(buf
, sizeof buf
, "%u", index
);
1318 // FIXME: This adds in pkgpath twice for hidden symbols, which is
1320 const std::string
& name(no
->name());
1321 if (!Gogo::is_hidden_name(name
))
1326 ret
.append(Gogo::pkgpath_for_symbol(Gogo::hidden_name_pkgpath(name
)));
1328 ret
.append(Gogo::unpack_hidden_name(name
));
1334 // Return true if this type descriptor is defined in a different
1335 // package. If this returns true it sets *PACKAGE to the package.
1338 Type::type_descriptor_defined_elsewhere(Named_type
* nt
,
1339 const Package
** package
)
1343 if (nt
->named_object()->package() != NULL
)
1345 // This is a named type defined in a different package. The
1346 // type descriptor should be defined in that package.
1347 *package
= nt
->named_object()->package();
1353 if (this->points_to() != NULL
1354 && this->points_to()->named_type() != NULL
1355 && this->points_to()->named_type()->named_object()->package() != NULL
)
1357 // This is an unnamed pointer to a named type defined in a
1358 // different package. The descriptor should be defined in
1360 *package
= this->points_to()->named_type()->named_object()->package();
1367 // Return a composite literal for a type descriptor.
1370 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
1372 return type
->do_type_descriptor(gogo
, NULL
);
1375 // Return a composite literal for a type descriptor with a name.
1378 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
1380 go_assert(name
!= NULL
&& type
->named_type() != name
);
1381 return type
->do_type_descriptor(gogo
, name
);
1384 // Make a builtin struct type from a list of fields. The fields are
1385 // pairs of a name and a type.
1388 Type::make_builtin_struct_type(int nfields
, ...)
1391 va_start(ap
, nfields
);
1393 Location bloc
= Linemap::predeclared_location();
1394 Struct_field_list
* sfl
= new Struct_field_list();
1395 for (int i
= 0; i
< nfields
; i
++)
1397 const char* field_name
= va_arg(ap
, const char *);
1398 Type
* type
= va_arg(ap
, Type
*);
1399 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
1404 return Type::make_struct_type(sfl
, bloc
);
1407 // A list of builtin named types.
1409 std::vector
<Named_type
*> Type::named_builtin_types
;
1411 // Make a builtin named type.
1414 Type::make_builtin_named_type(const char* name
, Type
* type
)
1416 Location bloc
= Linemap::predeclared_location();
1417 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
1418 Named_type
* ret
= no
->type_value();
1419 Type::named_builtin_types
.push_back(ret
);
1423 // Convert the named builtin types.
1426 Type::convert_builtin_named_types(Gogo
* gogo
)
1428 for (std::vector
<Named_type
*>::const_iterator p
=
1429 Type::named_builtin_types
.begin();
1430 p
!= Type::named_builtin_types
.end();
1433 bool r
= (*p
)->verify();
1435 (*p
)->convert(gogo
);
1439 // Return the type of a type descriptor. We should really tie this to
1440 // runtime.Type rather than copying it. This must match commonType in
1441 // libgo/go/runtime/type.go.
1444 Type::make_type_descriptor_type()
1449 Location bloc
= Linemap::predeclared_location();
1451 Type
* uint8_type
= Type::lookup_integer_type("uint8");
1452 Type
* uint32_type
= Type::lookup_integer_type("uint32");
1453 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1454 Type
* string_type
= Type::lookup_string_type();
1455 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
1457 // This is an unnamed version of unsafe.Pointer. Perhaps we
1458 // should use the named version instead, although that would
1459 // require us to create the unsafe package if it has not been
1460 // imported. It probably doesn't matter.
1461 Type
* void_type
= Type::make_void_type();
1462 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1464 // Forward declaration for the type descriptor type.
1465 Named_object
* named_type_descriptor_type
=
1466 Named_object::make_type_declaration("commonType", NULL
, bloc
);
1467 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1468 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1470 // The type of a method on a concrete type.
1471 Struct_type
* method_type
=
1472 Type::make_builtin_struct_type(5,
1473 "name", pointer_string_type
,
1474 "pkgPath", pointer_string_type
,
1475 "mtyp", pointer_type_descriptor_type
,
1476 "typ", pointer_type_descriptor_type
,
1477 "tfn", unsafe_pointer_type
);
1478 Named_type
* named_method_type
=
1479 Type::make_builtin_named_type("method", method_type
);
1481 // Information for types with a name or methods.
1482 Type
* slice_named_method_type
=
1483 Type::make_array_type(named_method_type
, NULL
);
1484 Struct_type
* uncommon_type
=
1485 Type::make_builtin_struct_type(3,
1486 "name", pointer_string_type
,
1487 "pkgPath", pointer_string_type
,
1488 "methods", slice_named_method_type
);
1489 Named_type
* named_uncommon_type
=
1490 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1492 Type
* pointer_uncommon_type
=
1493 Type::make_pointer_type(named_uncommon_type
);
1495 // The type descriptor type.
1497 Typed_identifier_list
* params
= new Typed_identifier_list();
1498 params
->push_back(Typed_identifier("key", unsafe_pointer_type
, bloc
));
1499 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1501 Typed_identifier_list
* results
= new Typed_identifier_list();
1502 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1504 Type
* hashfn_type
= Type::make_function_type(NULL
, params
, results
, bloc
);
1506 params
= new Typed_identifier_list();
1507 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
, bloc
));
1508 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
, bloc
));
1509 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1511 results
= new Typed_identifier_list();
1512 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1514 Type
* equalfn_type
= Type::make_function_type(NULL
, params
, results
,
1517 Struct_type
* type_descriptor_type
=
1518 Type::make_builtin_struct_type(10,
1520 "align", uint8_type
,
1521 "fieldAlign", uint8_type
,
1522 "size", uintptr_type
,
1523 "hash", uint32_type
,
1524 "hashfn", hashfn_type
,
1525 "equalfn", equalfn_type
,
1526 "string", pointer_string_type
,
1527 "", pointer_uncommon_type
,
1529 pointer_type_descriptor_type
);
1531 Named_type
* named
= Type::make_builtin_named_type("commonType",
1532 type_descriptor_type
);
1534 named_type_descriptor_type
->set_type_value(named
);
1542 // Make the type of a pointer to a type descriptor as represented in
1546 Type::make_type_descriptor_ptr_type()
1550 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1554 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1555 // hash code for this type and which compare whether two values of
1556 // this type are equal. If NAME is not NULL it is the name of this
1557 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1558 // functions, for convenience; they may be NULL.
1561 Type::type_functions(Gogo
* gogo
, Named_type
* name
, Function_type
* hash_fntype
,
1562 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1563 Named_object
** equal_fn
)
1565 if (hash_fntype
== NULL
|| equal_fntype
== NULL
)
1567 Location bloc
= Linemap::predeclared_location();
1569 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1570 Type
* void_type
= Type::make_void_type();
1571 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
1573 if (hash_fntype
== NULL
)
1575 Typed_identifier_list
* params
= new Typed_identifier_list();
1576 params
->push_back(Typed_identifier("key", unsafe_pointer_type
,
1578 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1580 Typed_identifier_list
* results
= new Typed_identifier_list();
1581 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1583 hash_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1585 if (equal_fntype
== NULL
)
1587 Typed_identifier_list
* params
= new Typed_identifier_list();
1588 params
->push_back(Typed_identifier("key1", unsafe_pointer_type
,
1590 params
->push_back(Typed_identifier("key2", unsafe_pointer_type
,
1592 params
->push_back(Typed_identifier("key_size", uintptr_type
, bloc
));
1594 Typed_identifier_list
* results
= new Typed_identifier_list();
1595 results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
1598 equal_fntype
= Type::make_function_type(NULL
, params
, results
, bloc
);
1602 const char* hash_fnname
;
1603 const char* equal_fnname
;
1604 if (this->compare_is_identity(gogo
))
1606 hash_fnname
= "__go_type_hash_identity";
1607 equal_fnname
= "__go_type_equal_identity";
1609 else if (!this->is_comparable())
1611 hash_fnname
= "__go_type_hash_error";
1612 equal_fnname
= "__go_type_equal_error";
1616 switch (this->base()->classification())
1618 case Type::TYPE_ERROR
:
1619 case Type::TYPE_VOID
:
1620 case Type::TYPE_NIL
:
1621 case Type::TYPE_FUNCTION
:
1622 case Type::TYPE_MAP
:
1623 // For these types is_comparable should have returned false.
1626 case Type::TYPE_BOOLEAN
:
1627 case Type::TYPE_INTEGER
:
1628 case Type::TYPE_POINTER
:
1629 case Type::TYPE_CHANNEL
:
1630 // For these types compare_is_identity should have returned true.
1633 case Type::TYPE_FLOAT
:
1634 hash_fnname
= "__go_type_hash_float";
1635 equal_fnname
= "__go_type_equal_float";
1638 case Type::TYPE_COMPLEX
:
1639 hash_fnname
= "__go_type_hash_complex";
1640 equal_fnname
= "__go_type_equal_complex";
1643 case Type::TYPE_STRING
:
1644 hash_fnname
= "__go_type_hash_string";
1645 equal_fnname
= "__go_type_equal_string";
1648 case Type::TYPE_STRUCT
:
1650 // This is a struct which can not be compared using a
1651 // simple identity function. We need to build a function
1653 this->specific_type_functions(gogo
, name
, hash_fntype
,
1654 equal_fntype
, hash_fn
, equal_fn
);
1658 case Type::TYPE_ARRAY
:
1659 if (this->is_slice_type())
1661 // Type::is_compatible_for_comparison should have
1667 // This is an array which can not be compared using a
1668 // simple identity function. We need to build a
1669 // function for comparison.
1670 this->specific_type_functions(gogo
, name
, hash_fntype
,
1671 equal_fntype
, hash_fn
, equal_fn
);
1676 case Type::TYPE_INTERFACE
:
1677 if (this->interface_type()->is_empty())
1679 hash_fnname
= "__go_type_hash_empty_interface";
1680 equal_fnname
= "__go_type_equal_empty_interface";
1684 hash_fnname
= "__go_type_hash_interface";
1685 equal_fnname
= "__go_type_equal_interface";
1689 case Type::TYPE_NAMED
:
1690 case Type::TYPE_FORWARD
:
1699 Location bloc
= Linemap::predeclared_location();
1700 *hash_fn
= Named_object::make_function_declaration(hash_fnname
, NULL
,
1702 (*hash_fn
)->func_declaration_value()->set_asm_name(hash_fnname
);
1703 *equal_fn
= Named_object::make_function_declaration(equal_fnname
, NULL
,
1704 equal_fntype
, bloc
);
1705 (*equal_fn
)->func_declaration_value()->set_asm_name(equal_fnname
);
1708 // A hash table mapping types to the specific hash functions.
1710 Type::Type_functions
Type::type_functions_table
;
1712 // Handle a type function which is specific to a type: a struct or
1713 // array which can not use an identity comparison.
1716 Type::specific_type_functions(Gogo
* gogo
, Named_type
* name
,
1717 Function_type
* hash_fntype
,
1718 Function_type
* equal_fntype
,
1719 Named_object
** hash_fn
,
1720 Named_object
** equal_fn
)
1722 Hash_equal_fn
fnull(NULL
, NULL
);
1723 std::pair
<Type
*, Hash_equal_fn
> val(name
!= NULL
? name
: this, fnull
);
1724 std::pair
<Type_functions::iterator
, bool> ins
=
1725 Type::type_functions_table
.insert(val
);
1728 // We already have functions for this type
1729 *hash_fn
= ins
.first
->second
.first
;
1730 *equal_fn
= ins
.first
->second
.second
;
1734 std::string base_name
;
1737 // Mangled names can have '.' if they happen to refer to named
1738 // types in some way. That's fine if this is simply a named
1739 // type, but otherwise it will confuse the code that builds
1740 // function identifiers. Remove '.' when necessary.
1741 base_name
= this->mangled_name(gogo
);
1743 while ((i
= base_name
.find('.')) != std::string::npos
)
1745 base_name
= gogo
->pack_hidden_name(base_name
, false);
1749 // This name is already hidden or not as appropriate.
1750 base_name
= name
->name();
1752 const Named_object
* in_function
= name
->in_function(&index
);
1753 if (in_function
!= NULL
)
1755 base_name
+= '$' + Gogo::unpack_hidden_name(in_function
->name());
1759 snprintf(buf
, sizeof buf
, "%u", index
);
1765 std::string hash_name
= base_name
+ "$hash";
1766 std::string equal_name
= base_name
+ "$equal";
1768 Location bloc
= Linemap::predeclared_location();
1770 const Package
* package
= NULL
;
1771 bool is_defined_elsewhere
=
1772 this->type_descriptor_defined_elsewhere(name
, &package
);
1773 if (is_defined_elsewhere
)
1775 *hash_fn
= Named_object::make_function_declaration(hash_name
, package
,
1777 *equal_fn
= Named_object::make_function_declaration(equal_name
, package
,
1778 equal_fntype
, bloc
);
1782 *hash_fn
= gogo
->declare_package_function(hash_name
, hash_fntype
, bloc
);
1783 *equal_fn
= gogo
->declare_package_function(equal_name
, equal_fntype
,
1787 ins
.first
->second
.first
= *hash_fn
;
1788 ins
.first
->second
.second
= *equal_fn
;
1790 if (!is_defined_elsewhere
)
1792 if (gogo
->in_global_scope())
1793 this->write_specific_type_functions(gogo
, name
, hash_name
, hash_fntype
,
1794 equal_name
, equal_fntype
);
1796 gogo
->queue_specific_type_function(this, name
, hash_name
, hash_fntype
,
1797 equal_name
, equal_fntype
);
1801 // Write the hash and equality functions for a type which needs to be
1802 // written specially.
1805 Type::write_specific_type_functions(Gogo
* gogo
, Named_type
* name
,
1806 const std::string
& hash_name
,
1807 Function_type
* hash_fntype
,
1808 const std::string
& equal_name
,
1809 Function_type
* equal_fntype
)
1811 Location bloc
= Linemap::predeclared_location();
1813 if (gogo
->specific_type_functions_are_written())
1815 go_assert(saw_errors());
1819 Named_object
* hash_fn
= gogo
->start_function(hash_name
, hash_fntype
, false,
1821 gogo
->start_block(bloc
);
1823 if (this->struct_type() != NULL
)
1824 this->struct_type()->write_hash_function(gogo
, name
, hash_fntype
,
1826 else if (this->array_type() != NULL
)
1827 this->array_type()->write_hash_function(gogo
, name
, hash_fntype
,
1832 Block
* b
= gogo
->finish_block(bloc
);
1833 gogo
->add_block(b
, bloc
);
1834 gogo
->lower_block(hash_fn
, b
);
1835 gogo
->finish_function(bloc
);
1837 Named_object
*equal_fn
= gogo
->start_function(equal_name
, equal_fntype
,
1839 gogo
->start_block(bloc
);
1841 if (this->struct_type() != NULL
)
1842 this->struct_type()->write_equal_function(gogo
, name
);
1843 else if (this->array_type() != NULL
)
1844 this->array_type()->write_equal_function(gogo
, name
);
1848 b
= gogo
->finish_block(bloc
);
1849 gogo
->add_block(b
, bloc
);
1850 gogo
->lower_block(equal_fn
, b
);
1851 gogo
->finish_function(bloc
);
1854 // Return a composite literal for the type descriptor for a plain type
1855 // of kind RUNTIME_TYPE_KIND named NAME.
1858 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1859 Named_type
* name
, const Methods
* methods
,
1860 bool only_value_methods
)
1862 Location bloc
= Linemap::predeclared_location();
1864 Type
* td_type
= Type::make_type_descriptor_type();
1865 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1867 Expression_list
* vals
= new Expression_list();
1870 if (!this->has_pointer())
1871 runtime_type_kind
|= RUNTIME_TYPE_KIND_NO_POINTERS
;
1872 Struct_field_list::const_iterator p
= fields
->begin();
1873 go_assert(p
->is_field_name("Kind"));
1875 mpz_init_set_ui(iv
, runtime_type_kind
);
1876 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1879 go_assert(p
->is_field_name("align"));
1880 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1881 vals
->push_back(Expression::make_type_info(this, type_info
));
1884 go_assert(p
->is_field_name("fieldAlign"));
1885 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1886 vals
->push_back(Expression::make_type_info(this, type_info
));
1889 go_assert(p
->is_field_name("size"));
1890 type_info
= Expression::TYPE_INFO_SIZE
;
1891 vals
->push_back(Expression::make_type_info(this, type_info
));
1894 go_assert(p
->is_field_name("hash"));
1897 h
= name
->hash_for_method(gogo
);
1899 h
= this->hash_for_method(gogo
);
1901 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1904 go_assert(p
->is_field_name("hashfn"));
1905 Function_type
* hash_fntype
= p
->type()->function_type();
1908 go_assert(p
->is_field_name("equalfn"));
1909 Function_type
* equal_fntype
= p
->type()->function_type();
1911 Named_object
* hash_fn
;
1912 Named_object
* equal_fn
;
1913 this->type_functions(gogo
, name
, hash_fntype
, equal_fntype
, &hash_fn
,
1915 vals
->push_back(Expression::make_func_reference(hash_fn
, NULL
, bloc
));
1916 vals
->push_back(Expression::make_func_reference(equal_fn
, NULL
, bloc
));
1919 go_assert(p
->is_field_name("string"));
1920 Expression
* s
= Expression::make_string((name
!= NULL
1921 ? name
->reflection(gogo
)
1922 : this->reflection(gogo
)),
1924 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1927 go_assert(p
->is_field_name("uncommonType"));
1928 if (name
== NULL
&& methods
== NULL
)
1929 vals
->push_back(Expression::make_nil(bloc
));
1932 if (methods
== NULL
)
1933 methods
= name
->methods();
1934 vals
->push_back(this->uncommon_type_constructor(gogo
,
1937 only_value_methods
));
1941 go_assert(p
->is_field_name("ptrToThis"));
1943 vals
->push_back(Expression::make_nil(bloc
));
1946 Type
* pt
= Type::make_pointer_type(name
);
1947 vals
->push_back(Expression::make_type_descriptor(pt
, bloc
));
1951 go_assert(p
== fields
->end());
1955 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
1958 // Return a composite literal for the uncommon type information for
1959 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1960 // struct. If name is not NULL, it is the name of the type. If
1961 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1962 // is true if only value methods should be included. At least one of
1963 // NAME and METHODS must not be NULL.
1966 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
1967 Named_type
* name
, const Methods
* methods
,
1968 bool only_value_methods
) const
1970 Location bloc
= Linemap::predeclared_location();
1972 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
1974 Expression_list
* vals
= new Expression_list();
1977 Struct_field_list::const_iterator p
= fields
->begin();
1978 go_assert(p
->is_field_name("name"));
1981 go_assert(p
->is_field_name("pkgPath"));
1985 vals
->push_back(Expression::make_nil(bloc
));
1986 vals
->push_back(Expression::make_nil(bloc
));
1990 Named_object
* no
= name
->named_object();
1991 std::string n
= Gogo::unpack_hidden_name(no
->name());
1992 Expression
* s
= Expression::make_string(n
, bloc
);
1993 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1995 if (name
->is_builtin())
1996 vals
->push_back(Expression::make_nil(bloc
));
1999 const Package
* package
= no
->package();
2000 const std::string
& pkgpath(package
== NULL
2002 : package
->pkgpath());
2005 const Named_object
* in_function
= name
->in_function(&index
);
2006 if (in_function
!= NULL
)
2009 n
.append(Gogo::unpack_hidden_name(in_function
->name()));
2013 snprintf(buf
, sizeof buf
, "%u", index
);
2018 s
= Expression::make_string(n
, bloc
);
2019 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2024 go_assert(p
->is_field_name("methods"));
2025 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
2026 only_value_methods
));
2029 go_assert(p
== fields
->end());
2031 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
2033 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
2036 // Sort methods by name.
2042 operator()(const std::pair
<std::string
, const Method
*>& m1
,
2043 const std::pair
<std::string
, const Method
*>& m2
) const
2044 { return m1
.first
< m2
.first
; }
2047 // Return a composite literal for the type method table for this type.
2048 // METHODS_TYPE is the type of the table, and is a slice type.
2049 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
2050 // then only value methods are used.
2053 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
2054 const Methods
* methods
,
2055 bool only_value_methods
) const
2057 Location bloc
= Linemap::predeclared_location();
2059 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
2060 if (methods
!= NULL
)
2062 smethods
.reserve(methods
->count());
2063 for (Methods::const_iterator p
= methods
->begin();
2064 p
!= methods
->end();
2067 if (p
->second
->is_ambiguous())
2069 if (only_value_methods
&& !p
->second
->is_value_method())
2071 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
2075 if (smethods
.empty())
2076 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
2078 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
2080 Type
* method_type
= methods_type
->array_type()->element_type();
2082 Expression_list
* vals
= new Expression_list();
2083 vals
->reserve(smethods
.size());
2084 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
2086 p
!= smethods
.end();
2088 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
2089 p
->second
, only_value_methods
));
2091 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
2094 // Return a composite literal for a single method. METHOD_TYPE is the
2095 // type of the entry. METHOD_NAME is the name of the method and M is
2096 // the method information.
2099 Type::method_constructor(Gogo
*, Type
* method_type
,
2100 const std::string
& method_name
,
2102 bool only_value_methods
) const
2104 Location bloc
= Linemap::predeclared_location();
2106 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
2108 Expression_list
* vals
= new Expression_list();
2111 Struct_field_list::const_iterator p
= fields
->begin();
2112 go_assert(p
->is_field_name("name"));
2113 const std::string n
= Gogo::unpack_hidden_name(method_name
);
2114 Expression
* s
= Expression::make_string(n
, bloc
);
2115 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2118 go_assert(p
->is_field_name("pkgPath"));
2119 if (!Gogo::is_hidden_name(method_name
))
2120 vals
->push_back(Expression::make_nil(bloc
));
2123 s
= Expression::make_string(Gogo::hidden_name_pkgpath(method_name
),
2125 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
2128 Named_object
* no
= (m
->needs_stub_method()
2130 : m
->named_object());
2132 Function_type
* mtype
;
2133 if (no
->is_function())
2134 mtype
= no
->func_value()->type();
2136 mtype
= no
->func_declaration_value()->type();
2137 go_assert(mtype
->is_method());
2138 Type
* nonmethod_type
= mtype
->copy_without_receiver();
2141 go_assert(p
->is_field_name("mtyp"));
2142 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
2145 go_assert(p
->is_field_name("typ"));
2146 if (!only_value_methods
&& m
->is_value_method())
2148 // This is a value method on a pointer type. Change the type of
2149 // the method to use a pointer receiver. The implementation
2150 // always uses a pointer receiver anyhow.
2151 Type
* rtype
= mtype
->receiver()->type();
2152 Type
* prtype
= Type::make_pointer_type(rtype
);
2153 Typed_identifier
* receiver
=
2154 new Typed_identifier(mtype
->receiver()->name(), prtype
,
2155 mtype
->receiver()->location());
2156 mtype
= Type::make_function_type(receiver
,
2157 (mtype
->parameters() == NULL
2159 : mtype
->parameters()->copy()),
2160 (mtype
->results() == NULL
2162 : mtype
->results()->copy()),
2165 vals
->push_back(Expression::make_type_descriptor(mtype
, bloc
));
2168 go_assert(p
->is_field_name("tfn"));
2169 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
2172 go_assert(p
== fields
->end());
2174 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
2177 // Return a composite literal for the type descriptor of a plain type.
2178 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
2179 // NULL, it is the name to use as well as the list of methods.
2182 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
2185 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
2189 // Return the type reflection string for this type.
2192 Type::reflection(Gogo
* gogo
) const
2196 // The do_reflection virtual function should set RET to the
2197 // reflection string.
2198 this->do_reflection(gogo
, &ret
);
2203 // Return a mangled name for the type.
2206 Type::mangled_name(Gogo
* gogo
) const
2210 // The do_mangled_name virtual function should set RET to the
2211 // mangled name. For a composite type it should append a code for
2212 // the composition and then call do_mangled_name on the components.
2213 this->do_mangled_name(gogo
, &ret
);
2218 // Return whether the backend size of the type is known.
2221 Type::is_backend_type_size_known(Gogo
* gogo
)
2223 switch (this->classification_
)
2237 case TYPE_INTERFACE
:
2242 const Struct_field_list
* fields
= this->struct_type()->fields();
2243 for (Struct_field_list::const_iterator pf
= fields
->begin();
2244 pf
!= fields
->end();
2246 if (!pf
->type()->is_backend_type_size_known(gogo
))
2253 const Array_type
* at
= this->array_type();
2254 if (at
->length() == NULL
)
2258 Numeric_constant nc
;
2259 if (!at
->length()->numeric_constant_value(&nc
))
2262 if (!nc
.to_int(&ival
))
2265 return at
->element_type()->is_backend_type_size_known(gogo
);
2270 // Begin converting this type to the backend representation.
2271 // This will create a placeholder if necessary.
2272 this->get_backend(gogo
);
2273 return this->named_type()->is_named_backend_type_size_known();
2277 Forward_declaration_type
* fdt
= this->forward_declaration_type();
2278 return fdt
->real_type()->is_backend_type_size_known(gogo
);
2282 case TYPE_CALL_MULTIPLE_RESULT
:
2290 // If the size of the type can be determined, set *PSIZE to the size
2291 // in bytes and return true. Otherwise, return false. This queries
2295 Type::backend_type_size(Gogo
* gogo
, unsigned int *psize
)
2297 if (!this->is_backend_type_size_known(gogo
))
2299 Btype
* bt
= this->get_backend_placeholder(gogo
);
2300 size_t size
= gogo
->backend()->type_size(bt
);
2301 *psize
= static_cast<unsigned int>(size
);
2307 // If the alignment of the type can be determined, set *PALIGN to
2308 // the alignment in bytes and return true. Otherwise, return false.
2311 Type::backend_type_align(Gogo
* gogo
, unsigned int *palign
)
2313 if (!this->is_backend_type_size_known(gogo
))
2315 Btype
* bt
= this->get_backend_placeholder(gogo
);
2316 size_t align
= gogo
->backend()->type_alignment(bt
);
2317 *palign
= static_cast<unsigned int>(align
);
2318 if (*palign
!= align
)
2323 // Like backend_type_align, but return the alignment when used as a
2327 Type::backend_type_field_align(Gogo
* gogo
, unsigned int *palign
)
2329 if (!this->is_backend_type_size_known(gogo
))
2331 Btype
* bt
= this->get_backend_placeholder(gogo
);
2332 size_t a
= gogo
->backend()->type_field_alignment(bt
);
2333 *palign
= static_cast<unsigned int>(a
);
2339 // Default function to export a type.
2342 Type::do_export(Export
*) const
2350 Type::import_type(Import
* imp
)
2352 if (imp
->match_c_string("("))
2353 return Function_type::do_import(imp
);
2354 else if (imp
->match_c_string("*"))
2355 return Pointer_type::do_import(imp
);
2356 else if (imp
->match_c_string("struct "))
2357 return Struct_type::do_import(imp
);
2358 else if (imp
->match_c_string("["))
2359 return Array_type::do_import(imp
);
2360 else if (imp
->match_c_string("map "))
2361 return Map_type::do_import(imp
);
2362 else if (imp
->match_c_string("chan "))
2363 return Channel_type::do_import(imp
);
2364 else if (imp
->match_c_string("interface"))
2365 return Interface_type::do_import(imp
);
2368 error_at(imp
->location(), "import error: expected type");
2369 return Type::make_error_type();
2373 // A type used to indicate a parsing error. This exists to simplify
2374 // later error detection.
2376 class Error_type
: public Type
2385 do_compare_is_identity(Gogo
*) const
2389 do_get_backend(Gogo
* gogo
)
2390 { return gogo
->backend()->error_type(); }
2393 do_type_descriptor(Gogo
*, Named_type
*)
2394 { return Expression::make_error(Linemap::predeclared_location()); }
2397 do_reflection(Gogo
*, std::string
*) const
2398 { go_assert(saw_errors()); }
2401 do_mangled_name(Gogo
*, std::string
* ret
) const
2402 { ret
->push_back('E'); }
2406 Type::make_error_type()
2408 static Error_type singleton_error_type
;
2409 return &singleton_error_type
;
2414 class Void_type
: public Type
2423 do_compare_is_identity(Gogo
*) const
2427 do_get_backend(Gogo
* gogo
)
2428 { return gogo
->backend()->void_type(); }
2431 do_type_descriptor(Gogo
*, Named_type
*)
2432 { go_unreachable(); }
2435 do_reflection(Gogo
*, std::string
*) const
2439 do_mangled_name(Gogo
*, std::string
* ret
) const
2440 { ret
->push_back('v'); }
2444 Type::make_void_type()
2446 static Void_type singleton_void_type
;
2447 return &singleton_void_type
;
2450 // The boolean type.
2452 class Boolean_type
: public Type
2456 : Type(TYPE_BOOLEAN
)
2461 do_compare_is_identity(Gogo
*) const
2465 do_get_backend(Gogo
* gogo
)
2466 { return gogo
->backend()->bool_type(); }
2469 do_type_descriptor(Gogo
*, Named_type
* name
);
2471 // We should not be asked for the reflection string of a basic type.
2473 do_reflection(Gogo
*, std::string
* ret
) const
2474 { ret
->append("bool"); }
2477 do_mangled_name(Gogo
*, std::string
* ret
) const
2478 { ret
->push_back('b'); }
2481 // Make the type descriptor.
2484 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2487 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
2490 Named_object
* no
= gogo
->lookup_global("bool");
2491 go_assert(no
!= NULL
);
2492 return Type::type_descriptor(gogo
, no
->type_value());
2497 Type::make_boolean_type()
2499 static Boolean_type boolean_type
;
2500 return &boolean_type
;
2503 // The named type "bool".
2505 static Named_type
* named_bool_type
;
2507 // Get the named type "bool".
2510 Type::lookup_bool_type()
2512 return named_bool_type
;
2515 // Make the named type "bool".
2518 Type::make_named_bool_type()
2520 Type
* bool_type
= Type::make_boolean_type();
2521 Named_object
* named_object
=
2522 Named_object::make_type("bool", NULL
, bool_type
,
2523 Linemap::predeclared_location());
2524 Named_type
* named_type
= named_object
->type_value();
2525 named_bool_type
= named_type
;
2529 // Class Integer_type.
2531 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
2533 // Create a new integer type. Non-abstract integer types always have
2537 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
2538 int bits
, int runtime_type_kind
)
2540 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
2542 std::string
sname(name
);
2543 Named_object
* named_object
=
2544 Named_object::make_type(sname
, NULL
, integer_type
,
2545 Linemap::predeclared_location());
2546 Named_type
* named_type
= named_object
->type_value();
2547 std::pair
<Named_integer_types::iterator
, bool> ins
=
2548 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
2549 go_assert(ins
.second
);
2553 // Look up an existing integer type.
2556 Integer_type::lookup_integer_type(const char* name
)
2558 Named_integer_types::const_iterator p
=
2559 Integer_type::named_integer_types
.find(name
);
2560 go_assert(p
!= Integer_type::named_integer_types
.end());
2564 // Create a new abstract integer type.
2567 Integer_type::create_abstract_integer_type()
2569 static Integer_type
* abstract_type
;
2570 if (abstract_type
== NULL
)
2571 abstract_type
= new Integer_type(true, false, INT_TYPE_SIZE
,
2572 RUNTIME_TYPE_KIND_INT
);
2573 return abstract_type
;
2576 // Create a new abstract character type.
2579 Integer_type::create_abstract_character_type()
2581 static Integer_type
* abstract_type
;
2582 if (abstract_type
== NULL
)
2584 abstract_type
= new Integer_type(true, false, 32,
2585 RUNTIME_TYPE_KIND_INT32
);
2586 abstract_type
->set_is_rune();
2588 return abstract_type
;
2591 // Integer type compatibility.
2594 Integer_type::is_identical(const Integer_type
* t
) const
2596 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
2598 return this->is_abstract_
== t
->is_abstract_
;
2604 Integer_type::do_hash_for_method(Gogo
*) const
2606 return ((this->bits_
<< 4)
2607 + ((this->is_unsigned_
? 1 : 0) << 8)
2608 + ((this->is_abstract_
? 1 : 0) << 9));
2611 // Convert an Integer_type to the backend representation.
2614 Integer_type::do_get_backend(Gogo
* gogo
)
2616 if (this->is_abstract_
)
2618 go_assert(saw_errors());
2619 return gogo
->backend()->error_type();
2621 return gogo
->backend()->integer_type(this->is_unsigned_
, this->bits_
);
2624 // The type descriptor for an integer type. Integer types are always
2628 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2630 go_assert(name
!= NULL
|| saw_errors());
2631 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2634 // We should not be asked for the reflection string of a basic type.
2637 Integer_type::do_reflection(Gogo
*, std::string
*) const
2639 go_assert(saw_errors());
2645 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2648 snprintf(buf
, sizeof buf
, "i%s%s%de",
2649 this->is_abstract_
? "a" : "",
2650 this->is_unsigned_
? "u" : "",
2655 // Make an integer type.
2658 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
2659 int runtime_type_kind
)
2661 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
2665 // Make an abstract integer type.
2668 Type::make_abstract_integer_type()
2670 return Integer_type::create_abstract_integer_type();
2673 // Make an abstract character type.
2676 Type::make_abstract_character_type()
2678 return Integer_type::create_abstract_character_type();
2681 // Look up an integer type.
2684 Type::lookup_integer_type(const char* name
)
2686 return Integer_type::lookup_integer_type(name
);
2689 // Class Float_type.
2691 Float_type::Named_float_types
Float_type::named_float_types
;
2693 // Create a new float type. Non-abstract float types always have
2697 Float_type::create_float_type(const char* name
, int bits
,
2698 int runtime_type_kind
)
2700 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
2701 std::string
sname(name
);
2702 Named_object
* named_object
=
2703 Named_object::make_type(sname
, NULL
, float_type
,
2704 Linemap::predeclared_location());
2705 Named_type
* named_type
= named_object
->type_value();
2706 std::pair
<Named_float_types::iterator
, bool> ins
=
2707 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
2708 go_assert(ins
.second
);
2712 // Look up an existing float type.
2715 Float_type::lookup_float_type(const char* name
)
2717 Named_float_types::const_iterator p
=
2718 Float_type::named_float_types
.find(name
);
2719 go_assert(p
!= Float_type::named_float_types
.end());
2723 // Create a new abstract float type.
2726 Float_type::create_abstract_float_type()
2728 static Float_type
* abstract_type
;
2729 if (abstract_type
== NULL
)
2730 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
2731 return abstract_type
;
2734 // Whether this type is identical with T.
2737 Float_type::is_identical(const Float_type
* t
) const
2739 if (this->bits_
!= t
->bits_
)
2741 return this->is_abstract_
== t
->is_abstract_
;
2747 Float_type::do_hash_for_method(Gogo
*) const
2749 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2752 // Convert to the backend representation.
2755 Float_type::do_get_backend(Gogo
* gogo
)
2757 return gogo
->backend()->float_type(this->bits_
);
2760 // The type descriptor for a float type. Float types are always named.
2763 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2765 go_assert(name
!= NULL
|| saw_errors());
2766 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2769 // We should not be asked for the reflection string of a basic type.
2772 Float_type::do_reflection(Gogo
*, std::string
*) const
2774 go_assert(saw_errors());
2780 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2783 snprintf(buf
, sizeof buf
, "f%s%de",
2784 this->is_abstract_
? "a" : "",
2789 // Make a floating point type.
2792 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
2794 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
2797 // Make an abstract float type.
2800 Type::make_abstract_float_type()
2802 return Float_type::create_abstract_float_type();
2805 // Look up a float type.
2808 Type::lookup_float_type(const char* name
)
2810 return Float_type::lookup_float_type(name
);
2813 // Class Complex_type.
2815 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
2817 // Create a new complex type. Non-abstract complex types always have
2821 Complex_type::create_complex_type(const char* name
, int bits
,
2822 int runtime_type_kind
)
2824 Complex_type
* complex_type
= new Complex_type(false, bits
,
2826 std::string
sname(name
);
2827 Named_object
* named_object
=
2828 Named_object::make_type(sname
, NULL
, complex_type
,
2829 Linemap::predeclared_location());
2830 Named_type
* named_type
= named_object
->type_value();
2831 std::pair
<Named_complex_types::iterator
, bool> ins
=
2832 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
2834 go_assert(ins
.second
);
2838 // Look up an existing complex type.
2841 Complex_type::lookup_complex_type(const char* name
)
2843 Named_complex_types::const_iterator p
=
2844 Complex_type::named_complex_types
.find(name
);
2845 go_assert(p
!= Complex_type::named_complex_types
.end());
2849 // Create a new abstract complex type.
2852 Complex_type::create_abstract_complex_type()
2854 static Complex_type
* abstract_type
;
2855 if (abstract_type
== NULL
)
2856 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
2857 return abstract_type
;
2860 // Whether this type is identical with T.
2863 Complex_type::is_identical(const Complex_type
*t
) const
2865 if (this->bits_
!= t
->bits_
)
2867 return this->is_abstract_
== t
->is_abstract_
;
2873 Complex_type::do_hash_for_method(Gogo
*) const
2875 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2878 // Convert to the backend representation.
2881 Complex_type::do_get_backend(Gogo
* gogo
)
2883 return gogo
->backend()->complex_type(this->bits_
);
2886 // The type descriptor for a complex type. Complex types are always
2890 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2892 go_assert(name
!= NULL
|| saw_errors());
2893 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2896 // We should not be asked for the reflection string of a basic type.
2899 Complex_type::do_reflection(Gogo
*, std::string
*) const
2901 go_assert(saw_errors());
2907 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2910 snprintf(buf
, sizeof buf
, "c%s%de",
2911 this->is_abstract_
? "a" : "",
2916 // Make a complex type.
2919 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
2921 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
2924 // Make an abstract complex type.
2927 Type::make_abstract_complex_type()
2929 return Complex_type::create_abstract_complex_type();
2932 // Look up a complex type.
2935 Type::lookup_complex_type(const char* name
)
2937 return Complex_type::lookup_complex_type(name
);
2940 // Class String_type.
2942 // Convert String_type to the backend representation. A string is a
2943 // struct with two fields: a pointer to the characters and a length.
2946 String_type::do_get_backend(Gogo
* gogo
)
2948 static Btype
* backend_string_type
;
2949 if (backend_string_type
== NULL
)
2951 std::vector
<Backend::Btyped_identifier
> fields(2);
2953 Type
* b
= gogo
->lookup_global("byte")->type_value();
2954 Type
* pb
= Type::make_pointer_type(b
);
2956 // We aren't going to get back to this field to finish the
2957 // backend representation, so force it to be finished now.
2958 if (!gogo
->named_types_are_converted())
2960 pb
->get_backend_placeholder(gogo
);
2961 pb
->finish_backend(gogo
);
2964 fields
[0].name
= "__data";
2965 fields
[0].btype
= pb
->get_backend(gogo
);
2966 fields
[0].location
= Linemap::predeclared_location();
2968 Type
* int_type
= Type::lookup_integer_type("int");
2969 fields
[1].name
= "__length";
2970 fields
[1].btype
= int_type
->get_backend(gogo
);
2971 fields
[1].location
= fields
[0].location
;
2973 backend_string_type
= gogo
->backend()->struct_type(fields
);
2975 return backend_string_type
;
2978 // Return a tree for the length of STRING.
2981 String_type::length_tree(Gogo
*, tree string
)
2983 tree string_type
= TREE_TYPE(string
);
2984 go_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2985 tree length_field
= DECL_CHAIN(TYPE_FIELDS(string_type
));
2986 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field
)),
2988 return fold_build3(COMPONENT_REF
, integer_type_node
, string
,
2989 length_field
, NULL_TREE
);
2992 // Return a tree for a pointer to the bytes of STRING.
2995 String_type::bytes_tree(Gogo
*, tree string
)
2997 tree string_type
= TREE_TYPE(string
);
2998 go_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2999 tree bytes_field
= TYPE_FIELDS(string_type
);
3000 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field
)),
3002 return fold_build3(COMPONENT_REF
, TREE_TYPE(bytes_field
), string
,
3003 bytes_field
, NULL_TREE
);
3006 // The type descriptor for the string type.
3009 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3012 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
3015 Named_object
* no
= gogo
->lookup_global("string");
3016 go_assert(no
!= NULL
);
3017 return Type::type_descriptor(gogo
, no
->type_value());
3021 // We should not be asked for the reflection string of a basic type.
3024 String_type::do_reflection(Gogo
*, std::string
* ret
) const
3026 ret
->append("string");
3029 // Mangled name of a string type.
3032 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
3034 ret
->push_back('z');
3037 // Make a string type.
3040 Type::make_string_type()
3042 static String_type string_type
;
3043 return &string_type
;
3046 // The named type "string".
3048 static Named_type
* named_string_type
;
3050 // Get the named type "string".
3053 Type::lookup_string_type()
3055 return named_string_type
;
3058 // Make the named type string.
3061 Type::make_named_string_type()
3063 Type
* string_type
= Type::make_string_type();
3064 Named_object
* named_object
=
3065 Named_object::make_type("string", NULL
, string_type
,
3066 Linemap::predeclared_location());
3067 Named_type
* named_type
= named_object
->type_value();
3068 named_string_type
= named_type
;
3072 // The sink type. This is the type of the blank identifier _. Any
3073 // type may be assigned to it.
3075 class Sink_type
: public Type
3084 do_compare_is_identity(Gogo
*) const
3088 do_get_backend(Gogo
*)
3089 { go_unreachable(); }
3092 do_type_descriptor(Gogo
*, Named_type
*)
3093 { go_unreachable(); }
3096 do_reflection(Gogo
*, std::string
*) const
3097 { go_unreachable(); }
3100 do_mangled_name(Gogo
*, std::string
*) const
3101 { go_unreachable(); }
3104 // Make the sink type.
3107 Type::make_sink_type()
3109 static Sink_type sink_type
;
3113 // Class Function_type.
3118 Function_type::do_traverse(Traverse
* traverse
)
3120 if (this->receiver_
!= NULL
3121 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
3122 return TRAVERSE_EXIT
;
3123 if (this->parameters_
!= NULL
3124 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
3125 return TRAVERSE_EXIT
;
3126 if (this->results_
!= NULL
3127 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
3128 return TRAVERSE_EXIT
;
3129 return TRAVERSE_CONTINUE
;
3132 // Returns whether T is a valid redeclaration of this type. If this
3133 // returns false, and REASON is not NULL, *REASON may be set to a
3134 // brief explanation of why it returned false.
3137 Function_type::is_valid_redeclaration(const Function_type
* t
,
3138 std::string
* reason
) const
3140 if (!this->is_identical(t
, false, true, reason
))
3143 // A redeclaration of a function is required to use the same names
3144 // for the receiver and parameters.
3145 if (this->receiver() != NULL
3146 && this->receiver()->name() != t
->receiver()->name())
3149 *reason
= "receiver name changed";
3153 const Typed_identifier_list
* parms1
= this->parameters();
3154 const Typed_identifier_list
* parms2
= t
->parameters();
3157 Typed_identifier_list::const_iterator p1
= parms1
->begin();
3158 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
3159 p2
!= parms2
->end();
3162 if (p1
->name() != p2
->name())
3165 *reason
= "parameter name changed";
3169 // This is called at parse time, so we may have unknown
3171 Type
* t1
= p1
->type()->forwarded();
3172 Type
* t2
= p2
->type()->forwarded();
3174 && t1
->forward_declaration_type() != NULL
3175 && (t2
->forward_declaration_type() == NULL
3176 || (t1
->forward_declaration_type()->named_object()
3177 != t2
->forward_declaration_type()->named_object())))
3182 const Typed_identifier_list
* results1
= this->results();
3183 const Typed_identifier_list
* results2
= t
->results();
3184 if (results1
!= NULL
)
3186 Typed_identifier_list::const_iterator res1
= results1
->begin();
3187 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
3188 res2
!= results2
->end();
3191 if (res1
->name() != res2
->name())
3194 *reason
= "result name changed";
3198 // This is called at parse time, so we may have unknown
3200 Type
* t1
= res1
->type()->forwarded();
3201 Type
* t2
= res2
->type()->forwarded();
3203 && t1
->forward_declaration_type() != NULL
3204 && (t2
->forward_declaration_type() == NULL
3205 || (t1
->forward_declaration_type()->named_object()
3206 != t2
->forward_declaration_type()->named_object())))
3214 // Check whether T is the same as this type.
3217 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
3218 bool errors_are_identical
,
3219 std::string
* reason
) const
3221 if (!ignore_receiver
)
3223 const Typed_identifier
* r1
= this->receiver();
3224 const Typed_identifier
* r2
= t
->receiver();
3225 if ((r1
!= NULL
) != (r2
!= NULL
))
3228 *reason
= _("different receiver types");
3233 if (!Type::are_identical(r1
->type(), r2
->type(), errors_are_identical
,
3236 if (reason
!= NULL
&& !reason
->empty())
3237 *reason
= "receiver: " + *reason
;
3243 const Typed_identifier_list
* parms1
= this->parameters();
3244 const Typed_identifier_list
* parms2
= t
->parameters();
3245 if ((parms1
!= NULL
) != (parms2
!= NULL
))
3248 *reason
= _("different number of parameters");
3253 Typed_identifier_list::const_iterator p1
= parms1
->begin();
3254 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
3255 p2
!= parms2
->end();
3258 if (p1
== parms1
->end())
3261 *reason
= _("different number of parameters");
3265 if (!Type::are_identical(p1
->type(), p2
->type(),
3266 errors_are_identical
, NULL
))
3269 *reason
= _("different parameter types");
3273 if (p1
!= parms1
->end())
3276 *reason
= _("different number of parameters");
3281 if (this->is_varargs() != t
->is_varargs())
3284 *reason
= _("different varargs");
3288 const Typed_identifier_list
* results1
= this->results();
3289 const Typed_identifier_list
* results2
= t
->results();
3290 if ((results1
!= NULL
) != (results2
!= NULL
))
3293 *reason
= _("different number of results");
3296 if (results1
!= NULL
)
3298 Typed_identifier_list::const_iterator res1
= results1
->begin();
3299 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
3300 res2
!= results2
->end();
3303 if (res1
== results1
->end())
3306 *reason
= _("different number of results");
3310 if (!Type::are_identical(res1
->type(), res2
->type(),
3311 errors_are_identical
, NULL
))
3314 *reason
= _("different result types");
3318 if (res1
!= results1
->end())
3321 *reason
= _("different number of results");
3332 Function_type::do_hash_for_method(Gogo
* gogo
) const
3334 unsigned int ret
= 0;
3335 // We ignore the receiver type for hash codes, because we need to
3336 // get the same hash code for a method in an interface and a method
3337 // declared for a type. The former will not have a receiver.
3338 if (this->parameters_
!= NULL
)
3341 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
3342 p
!= this->parameters_
->end();
3344 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
3346 if (this->results_
!= NULL
)
3349 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
3350 p
!= this->results_
->end();
3352 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
3354 if (this->is_varargs_
)
3360 // Get the backend representation for a function type.
3363 Function_type::do_get_backend(Gogo
* gogo
)
3365 Backend::Btyped_identifier breceiver
;
3366 if (this->receiver_
!= NULL
)
3368 breceiver
.name
= Gogo::unpack_hidden_name(this->receiver_
->name());
3370 // We always pass the address of the receiver parameter, in
3371 // order to make interface calls work with unknown types.
3372 Type
* rtype
= this->receiver_
->type();
3373 if (rtype
->points_to() == NULL
)
3374 rtype
= Type::make_pointer_type(rtype
);
3375 breceiver
.btype
= rtype
->get_backend(gogo
);
3376 breceiver
.location
= this->receiver_
->location();
3379 std::vector
<Backend::Btyped_identifier
> bparameters
;
3380 if (this->parameters_
!= NULL
)
3382 bparameters
.resize(this->parameters_
->size());
3384 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
3385 p
!= this->parameters_
->end();
3388 bparameters
[i
].name
= Gogo::unpack_hidden_name(p
->name());
3389 bparameters
[i
].btype
= p
->type()->get_backend(gogo
);
3390 bparameters
[i
].location
= p
->location();
3392 go_assert(i
== bparameters
.size());
3395 std::vector
<Backend::Btyped_identifier
> bresults
;
3396 if (this->results_
!= NULL
)
3398 bresults
.resize(this->results_
->size());
3400 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
3401 p
!= this->results_
->end();
3404 bresults
[i
].name
= Gogo::unpack_hidden_name(p
->name());
3405 bresults
[i
].btype
= p
->type()->get_backend(gogo
);
3406 bresults
[i
].location
= p
->location();
3408 go_assert(i
== bresults
.size());
3411 return gogo
->backend()->function_type(breceiver
, bparameters
, bresults
,
3415 // The type of a function type descriptor.
3418 Function_type::make_function_type_descriptor_type()
3423 Type
* tdt
= Type::make_type_descriptor_type();
3424 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3426 Type
* bool_type
= Type::lookup_bool_type();
3428 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
3430 Struct_type
* s
= Type::make_builtin_struct_type(4,
3432 "dotdotdot", bool_type
,
3436 ret
= Type::make_builtin_named_type("FuncType", s
);
3442 // The type descriptor for a function type.
3445 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3447 Location bloc
= Linemap::predeclared_location();
3449 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
3451 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
3453 Expression_list
* vals
= new Expression_list();
3456 Struct_field_list::const_iterator p
= fields
->begin();
3457 go_assert(p
->is_field_name("commonType"));
3458 vals
->push_back(this->type_descriptor_constructor(gogo
,
3459 RUNTIME_TYPE_KIND_FUNC
,
3463 go_assert(p
->is_field_name("dotdotdot"));
3464 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
3467 go_assert(p
->is_field_name("in"));
3468 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
3469 this->parameters()));
3472 go_assert(p
->is_field_name("out"));
3473 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
3477 go_assert(p
== fields
->end());
3479 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
3482 // Return a composite literal for the parameters or results of a type
3486 Function_type::type_descriptor_params(Type
* params_type
,
3487 const Typed_identifier
* receiver
,
3488 const Typed_identifier_list
* params
)
3490 Location bloc
= Linemap::predeclared_location();
3492 if (receiver
== NULL
&& params
== NULL
)
3493 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
3495 Expression_list
* vals
= new Expression_list();
3496 vals
->reserve((params
== NULL
? 0 : params
->size())
3497 + (receiver
!= NULL
? 1 : 0));
3499 if (receiver
!= NULL
)
3500 vals
->push_back(Expression::make_type_descriptor(receiver
->type(), bloc
));
3504 for (Typed_identifier_list::const_iterator p
= params
->begin();
3507 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
3510 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
3513 // The reflection string.
3516 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3518 // FIXME: Turn this off until we straighten out the type of the
3519 // struct field used in a go statement which calls a method.
3520 // go_assert(this->receiver_ == NULL);
3522 ret
->append("func");
3524 if (this->receiver_
!= NULL
)
3526 ret
->push_back('(');
3527 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
3528 ret
->push_back(')');
3531 ret
->push_back('(');
3532 const Typed_identifier_list
* params
= this->parameters();
3535 bool is_varargs
= this->is_varargs_
;
3536 for (Typed_identifier_list::const_iterator p
= params
->begin();
3540 if (p
!= params
->begin())
3542 if (!is_varargs
|| p
+ 1 != params
->end())
3543 this->append_reflection(p
->type(), gogo
, ret
);
3547 this->append_reflection(p
->type()->array_type()->element_type(),
3552 ret
->push_back(')');
3554 const Typed_identifier_list
* results
= this->results();
3555 if (results
!= NULL
&& !results
->empty())
3557 if (results
->size() == 1)
3558 ret
->push_back(' ');
3561 for (Typed_identifier_list::const_iterator p
= results
->begin();
3562 p
!= results
->end();
3565 if (p
!= results
->begin())
3567 this->append_reflection(p
->type(), gogo
, ret
);
3569 if (results
->size() > 1)
3570 ret
->push_back(')');
3577 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3579 ret
->push_back('F');
3581 if (this->receiver_
!= NULL
)
3583 ret
->push_back('m');
3584 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
3587 const Typed_identifier_list
* params
= this->parameters();
3590 ret
->push_back('p');
3591 for (Typed_identifier_list::const_iterator p
= params
->begin();
3594 this->append_mangled_name(p
->type(), gogo
, ret
);
3595 if (this->is_varargs_
)
3596 ret
->push_back('V');
3597 ret
->push_back('e');
3600 const Typed_identifier_list
* results
= this->results();
3601 if (results
!= NULL
)
3603 ret
->push_back('r');
3604 for (Typed_identifier_list::const_iterator p
= results
->begin();
3605 p
!= results
->end();
3607 this->append_mangled_name(p
->type(), gogo
, ret
);
3608 ret
->push_back('e');
3611 ret
->push_back('e');
3614 // Export a function type.
3617 Function_type::do_export(Export
* exp
) const
3619 // We don't write out the receiver. The only function types which
3620 // should have a receiver are the ones associated with explicitly
3621 // defined methods. For those the receiver type is written out by
3622 // Function::export_func.
3624 exp
->write_c_string("(");
3626 if (this->parameters_
!= NULL
)
3628 bool is_varargs
= this->is_varargs_
;
3629 for (Typed_identifier_list::const_iterator p
=
3630 this->parameters_
->begin();
3631 p
!= this->parameters_
->end();
3637 exp
->write_c_string(", ");
3638 exp
->write_name(p
->name());
3639 exp
->write_c_string(" ");
3640 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
3641 exp
->write_type(p
->type());
3644 exp
->write_c_string("...");
3645 exp
->write_type(p
->type()->array_type()->element_type());
3649 exp
->write_c_string(")");
3651 const Typed_identifier_list
* results
= this->results_
;
3652 if (results
!= NULL
)
3654 exp
->write_c_string(" ");
3655 if (results
->size() == 1 && results
->begin()->name().empty())
3656 exp
->write_type(results
->begin()->type());
3660 exp
->write_c_string("(");
3661 for (Typed_identifier_list::const_iterator p
= results
->begin();
3662 p
!= results
->end();
3668 exp
->write_c_string(", ");
3669 exp
->write_name(p
->name());
3670 exp
->write_c_string(" ");
3671 exp
->write_type(p
->type());
3673 exp
->write_c_string(")");
3678 // Import a function type.
3681 Function_type::do_import(Import
* imp
)
3683 imp
->require_c_string("(");
3684 Typed_identifier_list
* parameters
;
3685 bool is_varargs
= false;
3686 if (imp
->peek_char() == ')')
3690 parameters
= new Typed_identifier_list();
3693 std::string name
= imp
->read_name();
3694 imp
->require_c_string(" ");
3696 if (imp
->match_c_string("..."))
3702 Type
* ptype
= imp
->read_type();
3704 ptype
= Type::make_array_type(ptype
, NULL
);
3705 parameters
->push_back(Typed_identifier(name
, ptype
,
3707 if (imp
->peek_char() != ',')
3709 go_assert(!is_varargs
);
3710 imp
->require_c_string(", ");
3713 imp
->require_c_string(")");
3715 Typed_identifier_list
* results
;
3716 if (imp
->peek_char() != ' ')
3721 results
= new Typed_identifier_list
;
3722 if (imp
->peek_char() != '(')
3724 Type
* rtype
= imp
->read_type();
3725 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
3732 std::string name
= imp
->read_name();
3733 imp
->require_c_string(" ");
3734 Type
* rtype
= imp
->read_type();
3735 results
->push_back(Typed_identifier(name
, rtype
,
3737 if (imp
->peek_char() != ',')
3739 imp
->require_c_string(", ");
3741 imp
->require_c_string(")");
3745 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
3748 ret
->set_is_varargs();
3752 // Make a copy of a function type without a receiver.
3755 Function_type::copy_without_receiver() const
3757 go_assert(this->is_method());
3758 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
3761 if (this->is_varargs())
3762 ret
->set_is_varargs();
3763 if (this->is_builtin())
3764 ret
->set_is_builtin();
3768 // Make a copy of a function type with a receiver.
3771 Function_type::copy_with_receiver(Type
* receiver_type
) const
3773 go_assert(!this->is_method());
3774 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
3776 Function_type
* ret
= Type::make_function_type(receiver
, this->parameters_
,
3779 if (this->is_varargs_
)
3780 ret
->set_is_varargs();
3784 // Make a function type.
3787 Type::make_function_type(Typed_identifier
* receiver
,
3788 Typed_identifier_list
* parameters
,
3789 Typed_identifier_list
* results
,
3792 return new Function_type(receiver
, parameters
, results
, location
);
3795 // Class Pointer_type.
3800 Pointer_type::do_traverse(Traverse
* traverse
)
3802 return Type::traverse(this->to_type_
, traverse
);
3808 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
3810 return this->to_type_
->hash_for_method(gogo
) << 4;
3813 // Get the backend representation for a pointer type.
3816 Pointer_type::do_get_backend(Gogo
* gogo
)
3818 Btype
* to_btype
= this->to_type_
->get_backend(gogo
);
3819 return gogo
->backend()->pointer_type(to_btype
);
3822 // The type of a pointer type descriptor.
3825 Pointer_type::make_pointer_type_descriptor_type()
3830 Type
* tdt
= Type::make_type_descriptor_type();
3831 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3833 Struct_type
* s
= Type::make_builtin_struct_type(2,
3837 ret
= Type::make_builtin_named_type("PtrType", s
);
3843 // The type descriptor for a pointer type.
3846 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3848 if (this->is_unsafe_pointer_type())
3850 go_assert(name
!= NULL
);
3851 return this->plain_type_descriptor(gogo
,
3852 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
3857 Location bloc
= Linemap::predeclared_location();
3859 const Methods
* methods
;
3860 Type
* deref
= this->points_to();
3861 if (deref
->named_type() != NULL
)
3862 methods
= deref
->named_type()->methods();
3863 else if (deref
->struct_type() != NULL
)
3864 methods
= deref
->struct_type()->methods();
3868 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
3870 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
3872 Expression_list
* vals
= new Expression_list();
3875 Struct_field_list::const_iterator p
= fields
->begin();
3876 go_assert(p
->is_field_name("commonType"));
3877 vals
->push_back(this->type_descriptor_constructor(gogo
,
3878 RUNTIME_TYPE_KIND_PTR
,
3879 name
, methods
, false));
3882 go_assert(p
->is_field_name("elem"));
3883 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
3885 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
3889 // Reflection string.
3892 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3894 ret
->push_back('*');
3895 this->append_reflection(this->to_type_
, gogo
, ret
);
3901 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3903 ret
->push_back('p');
3904 this->append_mangled_name(this->to_type_
, gogo
, ret
);
3910 Pointer_type::do_export(Export
* exp
) const
3912 exp
->write_c_string("*");
3913 if (this->is_unsafe_pointer_type())
3914 exp
->write_c_string("any");
3916 exp
->write_type(this->to_type_
);
3922 Pointer_type::do_import(Import
* imp
)
3924 imp
->require_c_string("*");
3925 if (imp
->match_c_string("any"))
3928 return Type::make_pointer_type(Type::make_void_type());
3930 Type
* to
= imp
->read_type();
3931 return Type::make_pointer_type(to
);
3934 // Make a pointer type.
3937 Type::make_pointer_type(Type
* to_type
)
3939 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
3940 static Hashtable pointer_types
;
3941 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
3942 if (p
!= pointer_types
.end())
3944 Pointer_type
* ret
= new Pointer_type(to_type
);
3945 pointer_types
[to_type
] = ret
;
3949 // The nil type. We use a special type for nil because it is not the
3950 // same as any other type. In C term nil has type void*, but there is
3951 // no such type in Go.
3953 class Nil_type
: public Type
3962 do_compare_is_identity(Gogo
*) const
3966 do_get_backend(Gogo
* gogo
)
3967 { return gogo
->backend()->pointer_type(gogo
->backend()->void_type()); }
3970 do_type_descriptor(Gogo
*, Named_type
*)
3971 { go_unreachable(); }
3974 do_reflection(Gogo
*, std::string
*) const
3975 { go_unreachable(); }
3978 do_mangled_name(Gogo
*, std::string
* ret
) const
3979 { ret
->push_back('n'); }
3982 // Make the nil type.
3985 Type::make_nil_type()
3987 static Nil_type singleton_nil_type
;
3988 return &singleton_nil_type
;
3991 // The type of a function call which returns multiple values. This is
3992 // really a struct, but we don't want to confuse a function call which
3993 // returns a struct with a function call which returns multiple
3996 class Call_multiple_result_type
: public Type
3999 Call_multiple_result_type(Call_expression
* call
)
4000 : Type(TYPE_CALL_MULTIPLE_RESULT
),
4006 do_has_pointer() const
4008 go_assert(saw_errors());
4013 do_compare_is_identity(Gogo
*) const
4017 do_get_backend(Gogo
* gogo
)
4019 go_assert(saw_errors());
4020 return gogo
->backend()->error_type();
4024 do_type_descriptor(Gogo
*, Named_type
*)
4026 go_assert(saw_errors());
4027 return Expression::make_error(Linemap::unknown_location());
4031 do_reflection(Gogo
*, std::string
*) const
4032 { go_assert(saw_errors()); }
4035 do_mangled_name(Gogo
*, std::string
*) const
4036 { go_assert(saw_errors()); }
4039 // The expression being called.
4040 Call_expression
* call_
;
4043 // Make a call result type.
4046 Type::make_call_multiple_result_type(Call_expression
* call
)
4048 return new Call_multiple_result_type(call
);
4051 // Class Struct_field.
4053 // Get the name of a field.
4056 Struct_field::field_name() const
4058 const std::string
& name(this->typed_identifier_
.name());
4063 // This is called during parsing, before anything is lowered, so
4064 // we have to be pretty careful to avoid dereferencing an
4065 // unknown type name.
4066 Type
* t
= this->typed_identifier_
.type();
4068 if (t
->classification() == Type::TYPE_POINTER
)
4071 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
4072 dt
= ptype
->points_to();
4074 if (dt
->forward_declaration_type() != NULL
)
4075 return dt
->forward_declaration_type()->name();
4076 else if (dt
->named_type() != NULL
)
4077 return dt
->named_type()->name();
4078 else if (t
->is_error_type() || dt
->is_error_type())
4080 static const std::string error_string
= "*error*";
4081 return error_string
;
4085 // Avoid crashing in the erroneous case where T is named but
4088 if (t
->forward_declaration_type() != NULL
)
4089 return t
->forward_declaration_type()->name();
4090 else if (t
->named_type() != NULL
)
4091 return t
->named_type()->name();
4098 // Return whether this field is named NAME.
4101 Struct_field::is_field_name(const std::string
& name
) const
4103 const std::string
& me(this->typed_identifier_
.name());
4108 Type
* t
= this->typed_identifier_
.type();
4109 if (t
->points_to() != NULL
)
4111 Named_type
* nt
= t
->named_type();
4112 if (nt
!= NULL
&& nt
->name() == name
)
4115 // This is a horrible hack caused by the fact that we don't pack
4116 // the names of builtin types. FIXME.
4119 && nt
->name() == Gogo::unpack_hidden_name(name
))
4126 // Class Struct_type.
4131 Struct_type::do_traverse(Traverse
* traverse
)
4133 Struct_field_list
* fields
= this->fields_
;
4136 for (Struct_field_list::iterator p
= fields
->begin();
4140 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
4141 return TRAVERSE_EXIT
;
4144 return TRAVERSE_CONTINUE
;
4147 // Verify that the struct type is complete and valid.
4150 Struct_type::do_verify()
4152 Struct_field_list
* fields
= this->fields_
;
4155 for (Struct_field_list::iterator p
= fields
->begin();
4159 Type
* t
= p
->type();
4160 if (t
->is_undefined())
4162 error_at(p
->location(), "struct field type is incomplete");
4163 p
->set_type(Type::make_error_type());
4165 else if (p
->is_anonymous())
4167 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
4169 error_at(p
->location(), "embedded type may not be a pointer");
4170 p
->set_type(Type::make_error_type());
4172 else if (t
->points_to() != NULL
4173 && t
->points_to()->interface_type() != NULL
)
4175 error_at(p
->location(),
4176 "embedded type may not be pointer to interface");
4177 p
->set_type(Type::make_error_type());
4184 // Whether this contains a pointer.
4187 Struct_type::do_has_pointer() const
4189 const Struct_field_list
* fields
= this->fields();
4192 for (Struct_field_list::const_iterator p
= fields
->begin();
4196 if (p
->type()->has_pointer())
4202 // Whether this type is identical to T.
4205 Struct_type::is_identical(const Struct_type
* t
,
4206 bool errors_are_identical
) const
4208 const Struct_field_list
* fields1
= this->fields();
4209 const Struct_field_list
* fields2
= t
->fields();
4210 if (fields1
== NULL
|| fields2
== NULL
)
4211 return fields1
== fields2
;
4212 Struct_field_list::const_iterator pf2
= fields2
->begin();
4213 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
4214 pf1
!= fields1
->end();
4217 if (pf2
== fields2
->end())
4219 if (pf1
->field_name() != pf2
->field_name())
4221 if (pf1
->is_anonymous() != pf2
->is_anonymous()
4222 || !Type::are_identical(pf1
->type(), pf2
->type(),
4223 errors_are_identical
, NULL
))
4225 if (!pf1
->has_tag())
4232 if (!pf2
->has_tag())
4234 if (pf1
->tag() != pf2
->tag())
4238 if (pf2
!= fields2
->end())
4243 // Whether this struct type has any hidden fields.
4246 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
4247 std::string
* reason
) const
4249 const Struct_field_list
* fields
= this->fields();
4252 const Package
* within_package
= (within
== NULL
4254 : within
->named_object()->package());
4255 for (Struct_field_list::const_iterator pf
= fields
->begin();
4256 pf
!= fields
->end();
4259 if (within_package
!= NULL
4260 && !pf
->is_anonymous()
4261 && Gogo::is_hidden_name(pf
->field_name()))
4265 std::string within_name
= within
->named_object()->message_name();
4266 std::string name
= Gogo::message_name(pf
->field_name());
4267 size_t bufsize
= 200 + within_name
.length() + name
.length();
4268 char* buf
= new char[bufsize
];
4269 snprintf(buf
, bufsize
,
4270 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4271 open_quote
, within_name
.c_str(), close_quote
,
4272 open_quote
, name
.c_str(), close_quote
);
4273 reason
->assign(buf
);
4279 if (pf
->type()->has_hidden_fields(within
, reason
))
4286 // Whether comparisons of this struct type are simple identity
4290 Struct_type::do_compare_is_identity(Gogo
* gogo
) const
4292 const Struct_field_list
* fields
= this->fields_
;
4295 unsigned int offset
= 0;
4296 for (Struct_field_list::const_iterator pf
= fields
->begin();
4297 pf
!= fields
->end();
4300 if (Gogo::is_sink_name(pf
->field_name()))
4303 if (!pf
->type()->compare_is_identity(gogo
))
4306 unsigned int field_align
;
4307 if (!pf
->type()->backend_type_align(gogo
, &field_align
))
4309 if ((offset
& (field_align
- 1)) != 0)
4311 // This struct has padding. We don't guarantee that that
4312 // padding is zero-initialized for a stack variable, so we
4313 // can't use memcmp to compare struct values.
4317 unsigned int field_size
;
4318 if (!pf
->type()->backend_type_size(gogo
, &field_size
))
4320 offset
+= field_size
;
4325 // Build identity and hash functions for this struct.
4330 Struct_type::do_hash_for_method(Gogo
* gogo
) const
4332 unsigned int ret
= 0;
4333 if (this->fields() != NULL
)
4335 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
4336 pf
!= this->fields()->end();
4338 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
4343 // Find the local field NAME.
4346 Struct_type::find_local_field(const std::string
& name
,
4347 unsigned int *pindex
) const
4349 const Struct_field_list
* fields
= this->fields_
;
4353 for (Struct_field_list::const_iterator pf
= fields
->begin();
4354 pf
!= fields
->end();
4357 if (pf
->is_field_name(name
))
4367 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4369 Field_reference_expression
*
4370 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
4371 Location location
) const
4374 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
4378 // Return an expression for a field, along with the depth at which it
4381 Field_reference_expression
*
4382 Struct_type::field_reference_depth(Expression
* struct_expr
,
4383 const std::string
& name
,
4385 Saw_named_type
* saw
,
4386 unsigned int* depth
) const
4388 const Struct_field_list
* fields
= this->fields_
;
4392 // Look for a field with this name.
4394 for (Struct_field_list::const_iterator pf
= fields
->begin();
4395 pf
!= fields
->end();
4398 if (pf
->is_field_name(name
))
4401 return Expression::make_field_reference(struct_expr
, i
, location
);
4405 // Look for an anonymous field which contains a field with this
4407 unsigned int found_depth
= 0;
4408 Field_reference_expression
* ret
= NULL
;
4410 for (Struct_field_list::const_iterator pf
= fields
->begin();
4411 pf
!= fields
->end();
4414 if (!pf
->is_anonymous())
4417 Struct_type
* st
= pf
->type()->deref()->struct_type();
4421 Saw_named_type
* hold_saw
= saw
;
4422 Saw_named_type saw_here
;
4423 Named_type
* nt
= pf
->type()->named_type();
4425 nt
= pf
->type()->deref()->named_type();
4429 for (q
= saw
; q
!= NULL
; q
= q
->next
)
4433 // If this is an error, it will be reported
4440 saw_here
.next
= saw
;
4445 // Look for a reference using a NULL struct expression. If we
4446 // find one, fill in the struct expression with a reference to
4448 unsigned int subdepth
;
4449 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
4459 if (ret
== NULL
|| subdepth
< found_depth
)
4464 found_depth
= subdepth
;
4465 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
4467 if (pf
->type()->points_to() != NULL
)
4468 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
4469 while (sub
->expr() != NULL
)
4471 sub
= sub
->expr()->deref()->field_reference_expression();
4472 go_assert(sub
!= NULL
);
4474 sub
->set_struct_expression(here
);
4476 else if (subdepth
> found_depth
)
4480 // We do not handle ambiguity here--it should be handled by
4481 // Type::bind_field_or_method.
4489 *depth
= found_depth
+ 1;
4494 // Return the total number of fields, including embedded fields.
4497 Struct_type::total_field_count() const
4499 if (this->fields_
== NULL
)
4501 unsigned int ret
= 0;
4502 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
4503 pf
!= this->fields_
->end();
4506 if (!pf
->is_anonymous() || pf
->type()->struct_type() == NULL
)
4509 ret
+= pf
->type()->struct_type()->total_field_count();
4514 // Return whether NAME is an unexported field, for better error reporting.
4517 Struct_type::is_unexported_local_field(Gogo
* gogo
,
4518 const std::string
& name
) const
4520 const Struct_field_list
* fields
= this->fields_
;
4523 for (Struct_field_list::const_iterator pf
= fields
->begin();
4524 pf
!= fields
->end();
4527 const std::string
& field_name(pf
->field_name());
4528 if (Gogo::is_hidden_name(field_name
)
4529 && name
== Gogo::unpack_hidden_name(field_name
)
4530 && gogo
->pack_hidden_name(name
, false) != field_name
)
4537 // Finalize the methods of an unnamed struct.
4540 Struct_type::finalize_methods(Gogo
* gogo
)
4542 if (this->all_methods_
!= NULL
)
4544 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
4547 // Return the method NAME, or NULL if there isn't one or if it is
4548 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
4552 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
4554 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
4557 // Return a pointer to the interface method table for this type for
4558 // the interface INTERFACE. IS_POINTER is true if this is for a
4562 Struct_type::interface_method_table(Gogo
* gogo
,
4563 const Interface_type
* interface
,
4566 return Type::interface_method_table(gogo
, this, interface
, is_pointer
,
4567 &this->interface_method_tables_
,
4568 &this->pointer_interface_method_tables_
);
4571 // Convert struct fields to the backend representation. This is not
4572 // declared in types.h so that types.h doesn't have to #include
4576 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
4577 bool use_placeholder
,
4578 std::vector
<Backend::Btyped_identifier
>* bfields
)
4580 bfields
->resize(fields
->size());
4582 for (Struct_field_list::const_iterator p
= fields
->begin();
4586 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
4587 (*bfields
)[i
].btype
= (use_placeholder
4588 ? p
->type()->get_backend_placeholder(gogo
)
4589 : p
->type()->get_backend(gogo
));
4590 (*bfields
)[i
].location
= p
->location();
4592 go_assert(i
== fields
->size());
4595 // Get the tree for a struct type.
4598 Struct_type::do_get_backend(Gogo
* gogo
)
4600 std::vector
<Backend::Btyped_identifier
> bfields
;
4601 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
4602 return gogo
->backend()->struct_type(bfields
);
4605 // Finish the backend representation of the fields of a struct.
4608 Struct_type::finish_backend_fields(Gogo
* gogo
)
4610 const Struct_field_list
* fields
= this->fields_
;
4613 for (Struct_field_list::const_iterator p
= fields
->begin();
4616 p
->type()->get_backend(gogo
);
4620 // The type of a struct type descriptor.
4623 Struct_type::make_struct_type_descriptor_type()
4628 Type
* tdt
= Type::make_type_descriptor_type();
4629 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4631 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4632 Type
* string_type
= Type::lookup_string_type();
4633 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
4636 Type::make_builtin_struct_type(5,
4637 "name", pointer_string_type
,
4638 "pkgPath", pointer_string_type
,
4640 "tag", pointer_string_type
,
4641 "offset", uintptr_type
);
4642 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
4644 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
4646 Struct_type
* s
= Type::make_builtin_struct_type(2,
4648 "fields", slice_type
);
4650 ret
= Type::make_builtin_named_type("StructType", s
);
4656 // Build a type descriptor for a struct type.
4659 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4661 Location bloc
= Linemap::predeclared_location();
4663 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
4665 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4667 Expression_list
* vals
= new Expression_list();
4670 const Methods
* methods
= this->methods();
4671 // A named struct should not have methods--the methods should attach
4672 // to the named type.
4673 go_assert(methods
== NULL
|| name
== NULL
);
4675 Struct_field_list::const_iterator ps
= fields
->begin();
4676 go_assert(ps
->is_field_name("commonType"));
4677 vals
->push_back(this->type_descriptor_constructor(gogo
,
4678 RUNTIME_TYPE_KIND_STRUCT
,
4679 name
, methods
, true));
4682 go_assert(ps
->is_field_name("fields"));
4684 Expression_list
* elements
= new Expression_list();
4685 elements
->reserve(this->fields_
->size());
4686 Type
* element_type
= ps
->type()->array_type()->element_type();
4687 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
4688 pf
!= this->fields_
->end();
4691 const Struct_field_list
* f
= element_type
->struct_type()->fields();
4693 Expression_list
* fvals
= new Expression_list();
4696 Struct_field_list::const_iterator q
= f
->begin();
4697 go_assert(q
->is_field_name("name"));
4698 if (pf
->is_anonymous())
4699 fvals
->push_back(Expression::make_nil(bloc
));
4702 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
4703 Expression
* s
= Expression::make_string(n
, bloc
);
4704 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4708 go_assert(q
->is_field_name("pkgPath"));
4709 if (!Gogo::is_hidden_name(pf
->field_name()))
4710 fvals
->push_back(Expression::make_nil(bloc
));
4713 std::string n
= Gogo::hidden_name_pkgpath(pf
->field_name());
4714 Expression
* s
= Expression::make_string(n
, bloc
);
4715 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4719 go_assert(q
->is_field_name("typ"));
4720 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
4723 go_assert(q
->is_field_name("tag"));
4725 fvals
->push_back(Expression::make_nil(bloc
));
4728 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
4729 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4733 go_assert(q
->is_field_name("offset"));
4734 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
4736 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
4738 elements
->push_back(v
);
4741 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
4744 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4747 // Write the hash function for a struct which can not use the identity
4751 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
4752 Function_type
* hash_fntype
,
4753 Function_type
* equal_fntype
)
4755 Location bloc
= Linemap::predeclared_location();
4757 // The pointer to the struct that we are going to hash. This is an
4758 // argument to the hash function we are implementing here.
4759 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
4760 go_assert(key_arg
!= NULL
);
4761 Type
* key_arg_type
= key_arg
->var_value()->type();
4763 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4767 mpz_init_set_ui(ival
, 0);
4768 Expression
* zero
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
4771 // Make a temporary to hold the return value, initialized to 0.
4772 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
4774 gogo
->add_statement(retval
);
4776 // Make a temporary to hold the key as a uintptr.
4777 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
4778 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
4779 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
4781 gogo
->add_statement(key
);
4783 // Loop over the struct fields.
4785 const Struct_field_list
* fields
= this->fields_
;
4786 for (Struct_field_list::const_iterator pf
= fields
->begin();
4787 pf
!= fields
->end();
4790 if (Gogo::is_sink_name(pf
->field_name()))
4797 // Multiply retval by 33.
4798 mpz_init_set_ui(ival
, 33);
4799 Expression
* i33
= Expression::make_integer(&ival
, uintptr_type
,
4803 ref
= Expression::make_temporary_reference(retval
, bloc
);
4804 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
,
4806 gogo
->add_statement(s
);
4809 // Get a pointer to the value of this field.
4810 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
4811 ref
= Expression::make_temporary_reference(key
, bloc
);
4812 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
4814 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
4816 // Get the size of this field.
4817 Expression
* size
= Expression::make_type_info(pf
->type(),
4818 Expression::TYPE_INFO_SIZE
);
4820 // Get the hash function to use for the type of this field.
4821 Named_object
* hash_fn
;
4822 Named_object
* equal_fn
;
4823 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
4824 equal_fntype
, &hash_fn
, &equal_fn
);
4826 // Call the hash function for the field.
4827 Expression_list
* args
= new Expression_list();
4828 args
->push_back(subkey
);
4829 args
->push_back(size
);
4830 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
4831 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
4833 // Add the field's hash value to retval.
4834 Temporary_reference_expression
* tref
=
4835 Expression::make_temporary_reference(retval
, bloc
);
4836 tref
->set_is_lvalue();
4837 Statement
* s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
,
4839 gogo
->add_statement(s
);
4842 // Return retval to the caller of the hash function.
4843 Expression_list
* vals
= new Expression_list();
4844 ref
= Expression::make_temporary_reference(retval
, bloc
);
4845 vals
->push_back(ref
);
4846 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
4847 gogo
->add_statement(s
);
4850 // Write the equality function for a struct which can not use the
4851 // identity function.
4854 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
4856 Location bloc
= Linemap::predeclared_location();
4858 // The pointers to the structs we are going to compare.
4859 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
4860 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
4861 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
4863 // Build temporaries with the right types.
4864 Type
* pt
= Type::make_pointer_type(name
!= NULL
4865 ? static_cast<Type
*>(name
)
4866 : static_cast<Type
*>(this));
4868 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
4869 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
4870 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
4871 gogo
->add_statement(p1
);
4873 ref
= Expression::make_var_reference(key2_arg
, bloc
);
4874 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
4875 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
4876 gogo
->add_statement(p2
);
4878 const Struct_field_list
* fields
= this->fields_
;
4879 unsigned int field_index
= 0;
4880 for (Struct_field_list::const_iterator pf
= fields
->begin();
4881 pf
!= fields
->end();
4882 ++pf
, ++field_index
)
4884 if (Gogo::is_sink_name(pf
->field_name()))
4887 // Compare one field in both P1 and P2.
4888 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
4889 f1
= Expression::make_unary(OPERATOR_MULT
, f1
, bloc
);
4890 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
4892 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
4893 f2
= Expression::make_unary(OPERATOR_MULT
, f2
, bloc
);
4894 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
4896 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
4898 // If the values are not equal, return false.
4899 gogo
->start_block(bloc
);
4900 Expression_list
* vals
= new Expression_list();
4901 vals
->push_back(Expression::make_boolean(false, bloc
));
4902 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
4903 gogo
->add_statement(s
);
4904 Block
* then_block
= gogo
->finish_block(bloc
);
4906 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
4907 gogo
->add_statement(s
);
4910 // All the fields are equal, so return true.
4911 Expression_list
* vals
= new Expression_list();
4912 vals
->push_back(Expression::make_boolean(true, bloc
));
4913 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
4914 gogo
->add_statement(s
);
4917 // Reflection string.
4920 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4922 ret
->append("struct { ");
4924 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
4925 p
!= this->fields_
->end();
4928 if (p
!= this->fields_
->begin())
4930 if (p
->is_anonymous())
4931 ret
->push_back('?');
4933 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
4934 ret
->push_back(' ');
4935 this->append_reflection(p
->type(), gogo
, ret
);
4939 const std::string
& tag(p
->tag());
4941 for (std::string::const_iterator p
= tag
.begin();
4946 ret
->append("\\x00");
4947 else if (*p
== '\n')
4949 else if (*p
== '\t')
4952 ret
->append("\\\"");
4953 else if (*p
== '\\')
4954 ret
->append("\\\\");
4958 ret
->push_back('"');
4968 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4970 ret
->push_back('S');
4972 const Struct_field_list
* fields
= this->fields_
;
4975 for (Struct_field_list::const_iterator p
= fields
->begin();
4979 if (p
->is_anonymous())
4983 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
4985 snprintf(buf
, sizeof buf
, "%u_",
4986 static_cast<unsigned int>(n
.length()));
4990 this->append_mangled_name(p
->type(), gogo
, ret
);
4993 const std::string
& tag(p
->tag());
4995 for (std::string::const_iterator p
= tag
.begin();
4999 if (ISALNUM(*p
) || *p
== '_')
5004 snprintf(buf
, sizeof buf
, ".%x.",
5005 static_cast<unsigned int>(*p
));
5010 snprintf(buf
, sizeof buf
, "T%u_",
5011 static_cast<unsigned int>(out
.length()));
5018 ret
->push_back('e');
5021 // If the offset of field INDEX in the backend implementation can be
5022 // determined, set *POFFSET to the offset in bytes and return true.
5023 // Otherwise, return false.
5026 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
5027 unsigned int* poffset
)
5029 if (!this->is_backend_type_size_known(gogo
))
5031 Btype
* bt
= this->get_backend_placeholder(gogo
);
5032 size_t offset
= gogo
->backend()->type_field_offset(bt
, index
);
5033 *poffset
= static_cast<unsigned int>(offset
);
5034 if (*poffset
!= offset
)
5042 Struct_type::do_export(Export
* exp
) const
5044 exp
->write_c_string("struct { ");
5045 const Struct_field_list
* fields
= this->fields_
;
5046 go_assert(fields
!= NULL
);
5047 for (Struct_field_list::const_iterator p
= fields
->begin();
5051 if (p
->is_anonymous())
5052 exp
->write_string("? ");
5055 exp
->write_string(p
->field_name());
5056 exp
->write_c_string(" ");
5058 exp
->write_type(p
->type());
5062 exp
->write_c_string(" ");
5064 Expression::make_string(p
->tag(), Linemap::predeclared_location());
5065 expr
->export_expression(exp
);
5069 exp
->write_c_string("; ");
5071 exp
->write_c_string("}");
5077 Struct_type::do_import(Import
* imp
)
5079 imp
->require_c_string("struct { ");
5080 Struct_field_list
* fields
= new Struct_field_list
;
5081 if (imp
->peek_char() != '}')
5086 if (imp
->match_c_string("? "))
5090 name
= imp
->read_identifier();
5091 imp
->require_c_string(" ");
5093 Type
* ftype
= imp
->read_type();
5095 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
5097 if (imp
->peek_char() == ' ')
5100 Expression
* expr
= Expression::import_expression(imp
);
5101 String_expression
* sexpr
= expr
->string_expression();
5102 go_assert(sexpr
!= NULL
);
5103 sf
.set_tag(sexpr
->val());
5107 imp
->require_c_string("; ");
5108 fields
->push_back(sf
);
5109 if (imp
->peek_char() == '}')
5113 imp
->require_c_string("}");
5115 return Type::make_struct_type(fields
, imp
->location());
5118 // Make a struct type.
5121 Type::make_struct_type(Struct_field_list
* fields
,
5124 return new Struct_type(fields
, location
);
5127 // Class Array_type.
5129 // Whether two array types are identical.
5132 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
5134 if (!Type::are_identical(this->element_type(), t
->element_type(),
5135 errors_are_identical
, NULL
))
5138 Expression
* l1
= this->length();
5139 Expression
* l2
= t
->length();
5141 // Slices of the same element type are identical.
5142 if (l1
== NULL
&& l2
== NULL
)
5145 // Arrays of the same element type are identical if they have the
5147 if (l1
!= NULL
&& l2
!= NULL
)
5152 // Try to determine the lengths. If we can't, assume the arrays
5153 // are not identical.
5155 Numeric_constant nc1
, nc2
;
5156 if (l1
->numeric_constant_value(&nc1
)
5157 && l2
->numeric_constant_value(&nc2
))
5160 if (nc1
.to_int(&v1
))
5163 if (nc2
.to_int(&v2
))
5165 ret
= mpz_cmp(v1
, v2
) == 0;
5174 // Otherwise the arrays are not identical.
5181 Array_type::do_traverse(Traverse
* traverse
)
5183 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
5184 return TRAVERSE_EXIT
;
5185 if (this->length_
!= NULL
5186 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
5187 return TRAVERSE_EXIT
;
5188 return TRAVERSE_CONTINUE
;
5191 // Check that the length is valid.
5194 Array_type::verify_length()
5196 if (this->length_
== NULL
)
5199 Type_context
context(Type::lookup_integer_type("int"), false);
5200 this->length_
->determine_type(&context
);
5202 if (!this->length_
->is_constant())
5204 error_at(this->length_
->location(), "array bound is not constant");
5208 Numeric_constant nc
;
5209 if (!this->length_
->numeric_constant_value(&nc
))
5211 if (this->length_
->type()->integer_type() != NULL
5212 || this->length_
->type()->float_type() != NULL
)
5213 error_at(this->length_
->location(), "array bound is not constant");
5215 error_at(this->length_
->location(), "array bound is not numeric");
5220 switch (nc
.to_unsigned_long(&val
))
5222 case Numeric_constant::NC_UL_VALID
:
5224 case Numeric_constant::NC_UL_NOTINT
:
5225 error_at(this->length_
->location(), "array bound truncated to integer");
5227 case Numeric_constant::NC_UL_NEGATIVE
:
5228 error_at(this->length_
->location(), "negative array bound");
5230 case Numeric_constant::NC_UL_BIG
:
5231 error_at(this->length_
->location(), "array bound overflows");
5237 Type
* int_type
= Type::lookup_integer_type("int");
5238 unsigned int tbits
= int_type
->integer_type()->bits();
5239 if (sizeof(val
) <= tbits
* 8
5240 && val
>> (tbits
- 1) != 0)
5242 error_at(this->length_
->location(), "array bound overflows");
5252 Array_type::do_verify()
5254 if (!this->verify_length())
5255 this->length_
= Expression::make_error(this->length_
->location());
5259 // Whether we can use memcmp to compare this array.
5262 Array_type::do_compare_is_identity(Gogo
* gogo
) const
5264 if (this->length_
== NULL
)
5267 // Check for [...], which indicates that this is not a real type.
5268 if (this->length_
->is_nil_expression())
5271 if (!this->element_type_
->compare_is_identity(gogo
))
5274 // If there is any padding, then we can't use memcmp.
5277 if (!this->element_type_
->backend_type_size(gogo
, &size
)
5278 || !this->element_type_
->backend_type_align(gogo
, &align
))
5280 if ((size
& (align
- 1)) != 0)
5286 // Array type hash code.
5289 Array_type::do_hash_for_method(Gogo
* gogo
) const
5291 // There is no very convenient way to get a hash code for the
5293 return this->element_type_
->hash_for_method(gogo
) + 1;
5296 // Write the hash function for an array which can not use the identify
5300 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
5301 Function_type
* hash_fntype
,
5302 Function_type
* equal_fntype
)
5304 Location bloc
= Linemap::predeclared_location();
5306 // The pointer to the array that we are going to hash. This is an
5307 // argument to the hash function we are implementing here.
5308 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
5309 go_assert(key_arg
!= NULL
);
5310 Type
* key_arg_type
= key_arg
->var_value()->type();
5312 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5316 mpz_init_set_ui(ival
, 0);
5317 Expression
* zero
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
5320 // Make a temporary to hold the return value, initialized to 0.
5321 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
5323 gogo
->add_statement(retval
);
5325 // Make a temporary to hold the key as a uintptr.
5326 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
5327 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
5328 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
5330 gogo
->add_statement(key
);
5332 // Loop over the array elements.
5334 Type
* int_type
= Type::lookup_integer_type("int");
5335 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
5336 gogo
->add_statement(index
);
5338 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
5339 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
5340 Type
* pt
= Type::make_pointer_type(name
!= NULL
5341 ? static_cast<Type
*>(name
)
5342 : static_cast<Type
*>(this));
5343 aref
= Expression::make_cast(pt
, aref
, bloc
);
5344 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
5349 gogo
->start_block(bloc
);
5351 // Multiply retval by 33.
5352 mpz_init_set_ui(ival
, 33);
5353 Expression
* i33
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
5356 ref
= Expression::make_temporary_reference(retval
, bloc
);
5357 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
, ref
,
5359 gogo
->add_statement(s
);
5361 // Get the hash function for the element type.
5362 Named_object
* hash_fn
;
5363 Named_object
* equal_fn
;
5364 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
5365 hash_fntype
, equal_fntype
, &hash_fn
,
5368 // Get a pointer to this element in the loop.
5369 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
5370 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
5372 // Get the size of each element.
5373 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
5374 Expression::TYPE_INFO_SIZE
);
5376 // Get the hash of this element.
5377 Expression_list
* args
= new Expression_list();
5378 args
->push_back(subkey
);
5379 args
->push_back(ele_size
);
5380 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
5381 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
5383 // Add the element's hash value to retval.
5384 Temporary_reference_expression
* tref
=
5385 Expression::make_temporary_reference(retval
, bloc
);
5386 tref
->set_is_lvalue();
5387 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, call
, bloc
);
5388 gogo
->add_statement(s
);
5390 // Increase the element pointer.
5391 tref
= Expression::make_temporary_reference(key
, bloc
);
5392 tref
->set_is_lvalue();
5393 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
5396 Block
* statements
= gogo
->finish_block(bloc
);
5398 for_range
->add_statements(statements
);
5399 gogo
->add_statement(for_range
);
5401 // Return retval to the caller of the hash function.
5402 Expression_list
* vals
= new Expression_list();
5403 ref
= Expression::make_temporary_reference(retval
, bloc
);
5404 vals
->push_back(ref
);
5405 s
= Statement::make_return_statement(vals
, bloc
);
5406 gogo
->add_statement(s
);
5409 // Write the equality function for an array which can not use the
5410 // identity function.
5413 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
5415 Location bloc
= Linemap::predeclared_location();
5417 // The pointers to the arrays we are going to compare.
5418 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
5419 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
5420 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
5422 // Build temporaries for the keys with the right types.
5423 Type
* pt
= Type::make_pointer_type(name
!= NULL
5424 ? static_cast<Type
*>(name
)
5425 : static_cast<Type
*>(this));
5427 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
5428 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5429 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
5430 gogo
->add_statement(p1
);
5432 ref
= Expression::make_var_reference(key2_arg
, bloc
);
5433 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5434 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
5435 gogo
->add_statement(p2
);
5437 // Loop over the array elements.
5439 Type
* int_type
= Type::lookup_integer_type("int");
5440 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
5441 gogo
->add_statement(index
);
5443 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
5444 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
5445 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
5450 gogo
->start_block(bloc
);
5452 // Compare element in P1 and P2.
5453 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
5454 e1
= Expression::make_unary(OPERATOR_MULT
, e1
, bloc
);
5455 ref
= Expression::make_temporary_reference(index
, bloc
);
5456 e1
= Expression::make_array_index(e1
, ref
, NULL
, bloc
);
5458 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
5459 e2
= Expression::make_unary(OPERATOR_MULT
, e2
, bloc
);
5460 ref
= Expression::make_temporary_reference(index
, bloc
);
5461 e2
= Expression::make_array_index(e2
, ref
, NULL
, bloc
);
5463 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
5465 // If the elements are not equal, return false.
5466 gogo
->start_block(bloc
);
5467 Expression_list
* vals
= new Expression_list();
5468 vals
->push_back(Expression::make_boolean(false, bloc
));
5469 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5470 gogo
->add_statement(s
);
5471 Block
* then_block
= gogo
->finish_block(bloc
);
5473 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
5474 gogo
->add_statement(s
);
5476 Block
* statements
= gogo
->finish_block(bloc
);
5478 for_range
->add_statements(statements
);
5479 gogo
->add_statement(for_range
);
5481 // All the elements are equal, so return true.
5482 vals
= new Expression_list();
5483 vals
->push_back(Expression::make_boolean(true, bloc
));
5484 s
= Statement::make_return_statement(vals
, bloc
);
5485 gogo
->add_statement(s
);
5488 // Get a tree for the length of a fixed array. The length may be
5489 // computed using a function call, so we must only evaluate it once.
5492 Array_type::get_length_tree(Gogo
* gogo
)
5494 go_assert(this->length_
!= NULL
);
5495 if (this->length_tree_
== NULL_TREE
)
5497 Numeric_constant nc
;
5499 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
5501 if (mpz_sgn(val
) < 0)
5503 this->length_tree_
= error_mark_node
;
5504 return this->length_tree_
;
5506 Type
* t
= nc
.type();
5508 t
= Type::lookup_integer_type("int");
5509 else if (t
->is_abstract())
5510 t
= t
->make_non_abstract_type();
5511 tree tt
= type_to_tree(t
->get_backend(gogo
));
5512 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
5517 // Make up a translation context for the array length
5518 // expression. FIXME: This won't work in general.
5519 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
5520 tree len
= this->length_
->get_tree(&context
);
5521 if (len
!= error_mark_node
)
5523 len
= convert_to_integer(integer_type_node
, len
);
5524 len
= save_expr(len
);
5526 this->length_tree_
= len
;
5529 return this->length_tree_
;
5532 // Get the backend representation of the fields of a slice. This is
5533 // not declared in types.h so that types.h doesn't have to #include
5536 // We use int for the count and capacity fields. This matches 6g.
5537 // The language more or less assumes that we can't allocate space of a
5538 // size which does not fit in int.
5541 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
5542 std::vector
<Backend::Btyped_identifier
>* bfields
)
5546 Type
* pet
= Type::make_pointer_type(type
->element_type());
5547 Btype
* pbet
= (use_placeholder
5548 ? pet
->get_backend_placeholder(gogo
)
5549 : pet
->get_backend(gogo
));
5550 Location ploc
= Linemap::predeclared_location();
5552 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
5553 p
->name
= "__values";
5557 Type
* int_type
= Type::lookup_integer_type("int");
5560 p
->name
= "__count";
5561 p
->btype
= int_type
->get_backend(gogo
);
5565 p
->name
= "__capacity";
5566 p
->btype
= int_type
->get_backend(gogo
);
5570 // Get a tree for the type of this array. A fixed array is simply
5571 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
5572 // just like an array in C. An open array is a struct with three
5573 // fields: a data pointer, the length, and the capacity.
5576 Array_type::do_get_backend(Gogo
* gogo
)
5578 if (this->length_
== NULL
)
5580 std::vector
<Backend::Btyped_identifier
> bfields
;
5581 get_backend_slice_fields(gogo
, this, false, &bfields
);
5582 return gogo
->backend()->struct_type(bfields
);
5586 Btype
* element
= this->get_backend_element(gogo
, false);
5587 Bexpression
* len
= this->get_backend_length(gogo
);
5588 return gogo
->backend()->array_type(element
, len
);
5592 // Return the backend representation of the element type.
5595 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
5597 if (use_placeholder
)
5598 return this->element_type_
->get_backend_placeholder(gogo
);
5600 return this->element_type_
->get_backend(gogo
);
5603 // Return the backend representation of the length.
5606 Array_type::get_backend_length(Gogo
* gogo
)
5608 return tree_to_expr(this->get_length_tree(gogo
));
5611 // Finish backend representation of the array.
5614 Array_type::finish_backend_element(Gogo
* gogo
)
5616 Type
* et
= this->array_type()->element_type();
5617 et
->get_backend(gogo
);
5618 if (this->is_slice_type())
5620 // This relies on the fact that we always use the same
5621 // structure for a pointer to any given type.
5622 Type
* pet
= Type::make_pointer_type(et
);
5623 pet
->get_backend(gogo
);
5627 // Return a tree for a pointer to the values in ARRAY.
5630 Array_type::value_pointer_tree(Gogo
*, tree array
) const
5633 if (this->length() != NULL
)
5636 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
5637 build_fold_addr_expr(array
));
5642 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
5643 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
5645 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
5648 if (TREE_CONSTANT(array
))
5649 TREE_CONSTANT(ret
) = 1;
5653 // Return a tree for the length of the array ARRAY which has this
5657 Array_type::length_tree(Gogo
* gogo
, tree array
)
5659 if (this->length_
!= NULL
)
5661 if (TREE_CODE(array
) == SAVE_EXPR
)
5662 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
5664 return omit_one_operand(integer_type_node
,
5665 this->get_length_tree(gogo
), array
);
5668 // This is an open array. We need to read the length field.
5670 tree type
= TREE_TYPE(array
);
5671 go_assert(TREE_CODE(type
) == RECORD_TYPE
);
5673 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
5674 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
5676 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
5677 if (TREE_CONSTANT(array
))
5678 TREE_CONSTANT(ret
) = 1;
5682 // Return a tree for the capacity of the array ARRAY which has this
5686 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
5688 if (this->length_
!= NULL
)
5689 return omit_one_operand(integer_type_node
, this->get_length_tree(gogo
),
5692 // This is an open array. We need to read the capacity field.
5694 tree type
= TREE_TYPE(array
);
5695 go_assert(TREE_CODE(type
) == RECORD_TYPE
);
5697 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
5698 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
5700 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
5706 Array_type::do_export(Export
* exp
) const
5708 exp
->write_c_string("[");
5709 if (this->length_
!= NULL
)
5710 this->length_
->export_expression(exp
);
5711 exp
->write_c_string("] ");
5712 exp
->write_type(this->element_type_
);
5718 Array_type::do_import(Import
* imp
)
5720 imp
->require_c_string("[");
5722 if (imp
->peek_char() == ']')
5725 length
= Expression::import_expression(imp
);
5726 imp
->require_c_string("] ");
5727 Type
* element_type
= imp
->read_type();
5728 return Type::make_array_type(element_type
, length
);
5731 // The type of an array type descriptor.
5734 Array_type::make_array_type_descriptor_type()
5739 Type
* tdt
= Type::make_type_descriptor_type();
5740 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5742 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5745 Type::make_builtin_struct_type(4,
5749 "len", uintptr_type
);
5751 ret
= Type::make_builtin_named_type("ArrayType", sf
);
5757 // The type of an slice type descriptor.
5760 Array_type::make_slice_type_descriptor_type()
5765 Type
* tdt
= Type::make_type_descriptor_type();
5766 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5769 Type::make_builtin_struct_type(2,
5773 ret
= Type::make_builtin_named_type("SliceType", sf
);
5779 // Build a type descriptor for an array/slice type.
5782 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5784 if (this->length_
!= NULL
)
5785 return this->array_type_descriptor(gogo
, name
);
5787 return this->slice_type_descriptor(gogo
, name
);
5790 // Build a type descriptor for an array type.
5793 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5795 Location bloc
= Linemap::predeclared_location();
5797 Type
* atdt
= Array_type::make_array_type_descriptor_type();
5799 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
5801 Expression_list
* vals
= new Expression_list();
5804 Struct_field_list::const_iterator p
= fields
->begin();
5805 go_assert(p
->is_field_name("commonType"));
5806 vals
->push_back(this->type_descriptor_constructor(gogo
,
5807 RUNTIME_TYPE_KIND_ARRAY
,
5811 go_assert(p
->is_field_name("elem"));
5812 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5815 go_assert(p
->is_field_name("slice"));
5816 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
5817 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
5820 go_assert(p
->is_field_name("len"));
5821 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
5824 go_assert(p
== fields
->end());
5826 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
5829 // Build a type descriptor for a slice type.
5832 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5834 Location bloc
= Linemap::predeclared_location();
5836 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
5838 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
5840 Expression_list
* vals
= new Expression_list();
5843 Struct_field_list::const_iterator p
= fields
->begin();
5844 go_assert(p
->is_field_name("commonType"));
5845 vals
->push_back(this->type_descriptor_constructor(gogo
,
5846 RUNTIME_TYPE_KIND_SLICE
,
5850 go_assert(p
->is_field_name("elem"));
5851 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5854 go_assert(p
== fields
->end());
5856 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
5859 // Reflection string.
5862 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5864 ret
->push_back('[');
5865 if (this->length_
!= NULL
)
5867 Numeric_constant nc
;
5869 if (!this->length_
->numeric_constant_value(&nc
)
5870 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
5871 error_at(this->length_
->location(), "invalid array length");
5875 snprintf(buf
, sizeof buf
, "%lu", val
);
5879 ret
->push_back(']');
5881 this->append_reflection(this->element_type_
, gogo
, ret
);
5887 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5889 ret
->push_back('A');
5890 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5891 if (this->length_
!= NULL
)
5893 Numeric_constant nc
;
5895 if (!this->length_
->numeric_constant_value(&nc
)
5896 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
5897 error_at(this->length_
->location(), "invalid array length");
5901 snprintf(buf
, sizeof buf
, "%lu", val
);
5905 ret
->push_back('e');
5908 // Make an array type.
5911 Type::make_array_type(Type
* element_type
, Expression
* length
)
5913 return new Array_type(element_type
, length
);
5921 Map_type::do_traverse(Traverse
* traverse
)
5923 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
5924 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
5925 return TRAVERSE_EXIT
;
5926 return TRAVERSE_CONTINUE
;
5929 // Check that the map type is OK.
5932 Map_type::do_verify()
5934 // The runtime support uses "map[void]void".
5935 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
5936 error_at(this->location_
, "invalid map key type");
5940 // Whether two map types are identical.
5943 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
5945 return (Type::are_identical(this->key_type(), t
->key_type(),
5946 errors_are_identical
, NULL
)
5947 && Type::are_identical(this->val_type(), t
->val_type(),
5948 errors_are_identical
, NULL
));
5954 Map_type::do_hash_for_method(Gogo
* gogo
) const
5956 return (this->key_type_
->hash_for_method(gogo
)
5957 + this->val_type_
->hash_for_method(gogo
)
5961 // Get the backend representation for a map type. A map type is
5962 // represented as a pointer to a struct. The struct is __go_map in
5966 Map_type::do_get_backend(Gogo
* gogo
)
5968 static Btype
* backend_map_type
;
5969 if (backend_map_type
== NULL
)
5971 std::vector
<Backend::Btyped_identifier
> bfields(4);
5973 Location bloc
= Linemap::predeclared_location();
5975 Type
* pdt
= Type::make_type_descriptor_ptr_type();
5976 bfields
[0].name
= "__descriptor";
5977 bfields
[0].btype
= pdt
->get_backend(gogo
);
5978 bfields
[0].location
= bloc
;
5980 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5981 bfields
[1].name
= "__element_count";
5982 bfields
[1].btype
= uintptr_type
->get_backend(gogo
);
5983 bfields
[1].location
= bloc
;
5985 bfields
[2].name
= "__bucket_count";
5986 bfields
[2].btype
= bfields
[1].btype
;
5987 bfields
[2].location
= bloc
;
5989 Btype
* bvt
= gogo
->backend()->void_type();
5990 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
5991 Btype
* bppvt
= gogo
->backend()->pointer_type(bpvt
);
5992 bfields
[3].name
= "__buckets";
5993 bfields
[3].btype
= bppvt
;
5994 bfields
[3].location
= bloc
;
5996 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
5997 bt
= gogo
->backend()->named_type("__go_map", bt
, bloc
);
5998 backend_map_type
= gogo
->backend()->pointer_type(bt
);
6000 return backend_map_type
;
6003 // The type of a map type descriptor.
6006 Map_type::make_map_type_descriptor_type()
6011 Type
* tdt
= Type::make_type_descriptor_type();
6012 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6015 Type::make_builtin_struct_type(3,
6020 ret
= Type::make_builtin_named_type("MapType", sf
);
6026 // Build a type descriptor for a map type.
6029 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6031 Location bloc
= Linemap::predeclared_location();
6033 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
6035 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
6037 Expression_list
* vals
= new Expression_list();
6040 Struct_field_list::const_iterator p
= fields
->begin();
6041 go_assert(p
->is_field_name("commonType"));
6042 vals
->push_back(this->type_descriptor_constructor(gogo
,
6043 RUNTIME_TYPE_KIND_MAP
,
6047 go_assert(p
->is_field_name("key"));
6048 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
6051 go_assert(p
->is_field_name("elem"));
6052 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
6055 go_assert(p
== fields
->end());
6057 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
6060 // A mapping from map types to map descriptors.
6062 Map_type::Map_descriptors
Map_type::map_descriptors
;
6064 // Build a map descriptor for this type. Return a pointer to it.
6067 Map_type::map_descriptor_pointer(Gogo
* gogo
, Location location
)
6069 Bvariable
* bvar
= this->map_descriptor(gogo
);
6070 tree var_tree
= var_to_tree(bvar
);
6071 if (var_tree
== error_mark_node
)
6072 return error_mark_node
;
6073 return build_fold_addr_expr_loc(location
.gcc_location(), var_tree
);
6076 // Build a map descriptor for this type.
6079 Map_type::map_descriptor(Gogo
* gogo
)
6081 std::pair
<Map_type
*, Bvariable
*> val(this, NULL
);
6082 std::pair
<Map_type::Map_descriptors::iterator
, bool> ins
=
6083 Map_type::map_descriptors
.insert(val
);
6085 return ins
.first
->second
;
6087 Type
* key_type
= this->key_type_
;
6088 Type
* val_type
= this->val_type_
;
6090 // The map entry type is a struct with three fields. Build that
6091 // struct so that we can get the offsets of the key and value within
6092 // a map entry. The first field should technically be a pointer to
6093 // this type itself, but since we only care about field offsets we
6094 // just use pointer to bool.
6095 Type
* pbool
= Type::make_pointer_type(Type::make_boolean_type());
6096 Struct_type
* map_entry_type
=
6097 Type::make_builtin_struct_type(3,
6102 Type
* map_descriptor_type
= Map_type::make_map_descriptor_type();
6104 const Struct_field_list
* fields
=
6105 map_descriptor_type
->struct_type()->fields();
6107 Expression_list
* vals
= new Expression_list();
6110 Location bloc
= Linemap::predeclared_location();
6112 Struct_field_list::const_iterator p
= fields
->begin();
6114 go_assert(p
->is_field_name("__map_descriptor"));
6115 vals
->push_back(Expression::make_type_descriptor(this, bloc
));
6118 go_assert(p
->is_field_name("__entry_size"));
6119 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
6120 vals
->push_back(Expression::make_type_info(map_entry_type
, type_info
));
6122 Struct_field_list::const_iterator pf
= map_entry_type
->fields()->begin();
6124 go_assert(pf
->is_field_name("__key"));
6127 go_assert(p
->is_field_name("__key_offset"));
6128 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6131 go_assert(pf
->is_field_name("__val"));
6134 go_assert(p
->is_field_name("__val_offset"));
6135 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6138 go_assert(p
== fields
->end());
6140 Expression
* initializer
=
6141 Expression::make_struct_composite_literal(map_descriptor_type
, vals
, bloc
);
6143 std::string mangled_name
= "__go_map_" + this->mangled_name(gogo
);
6144 Btype
* map_descriptor_btype
= map_descriptor_type
->get_backend(gogo
);
6145 Bvariable
* bvar
= gogo
->backend()->immutable_struct(mangled_name
, true,
6146 map_descriptor_btype
,
6149 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
6150 context
.set_is_const();
6151 Bexpression
* binitializer
= tree_to_expr(initializer
->get_tree(&context
));
6153 gogo
->backend()->immutable_struct_set_init(bvar
, mangled_name
, true,
6154 map_descriptor_btype
, bloc
,
6157 ins
.first
->second
= bvar
;
6161 // Build the type of a map descriptor. This must match the struct
6162 // __go_map_descriptor in libgo/runtime/map.h.
6165 Map_type::make_map_descriptor_type()
6170 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6171 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6173 Type::make_builtin_struct_type(4,
6174 "__map_descriptor", ptdt
,
6175 "__entry_size", uintptr_type
,
6176 "__key_offset", uintptr_type
,
6177 "__val_offset", uintptr_type
);
6178 ret
= Type::make_builtin_named_type("__go_map_descriptor", sf
);
6183 // Reflection string for a map.
6186 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6188 ret
->append("map[");
6189 this->append_reflection(this->key_type_
, gogo
, ret
);
6191 this->append_reflection(this->val_type_
, gogo
, ret
);
6194 // Mangled name for a map.
6197 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6199 ret
->push_back('M');
6200 this->append_mangled_name(this->key_type_
, gogo
, ret
);
6202 this->append_mangled_name(this->val_type_
, gogo
, ret
);
6205 // Export a map type.
6208 Map_type::do_export(Export
* exp
) const
6210 exp
->write_c_string("map [");
6211 exp
->write_type(this->key_type_
);
6212 exp
->write_c_string("] ");
6213 exp
->write_type(this->val_type_
);
6216 // Import a map type.
6219 Map_type::do_import(Import
* imp
)
6221 imp
->require_c_string("map [");
6222 Type
* key_type
= imp
->read_type();
6223 imp
->require_c_string("] ");
6224 Type
* val_type
= imp
->read_type();
6225 return Type::make_map_type(key_type
, val_type
, imp
->location());
6231 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
6233 return new Map_type(key_type
, val_type
, location
);
6236 // Class Channel_type.
6241 Channel_type::do_hash_for_method(Gogo
* gogo
) const
6243 unsigned int ret
= 0;
6244 if (this->may_send_
)
6246 if (this->may_receive_
)
6248 if (this->element_type_
!= NULL
)
6249 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
6253 // Whether this type is the same as T.
6256 Channel_type::is_identical(const Channel_type
* t
,
6257 bool errors_are_identical
) const
6259 if (!Type::are_identical(this->element_type(), t
->element_type(),
6260 errors_are_identical
, NULL
))
6262 return (this->may_send_
== t
->may_send_
6263 && this->may_receive_
== t
->may_receive_
);
6266 // Return the tree for a channel type. A channel is a pointer to a
6267 // __go_channel struct. The __go_channel struct is defined in
6268 // libgo/runtime/channel.h.
6271 Channel_type::do_get_backend(Gogo
* gogo
)
6273 static Btype
* backend_channel_type
;
6274 if (backend_channel_type
== NULL
)
6276 std::vector
<Backend::Btyped_identifier
> bfields
;
6277 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
6278 bt
= gogo
->backend()->named_type("__go_channel", bt
,
6279 Linemap::predeclared_location());
6280 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
6282 return backend_channel_type
;
6285 // Build a type descriptor for a channel type.
6288 Channel_type::make_chan_type_descriptor_type()
6293 Type
* tdt
= Type::make_type_descriptor_type();
6294 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6296 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6299 Type::make_builtin_struct_type(3,
6302 "dir", uintptr_type
);
6304 ret
= Type::make_builtin_named_type("ChanType", sf
);
6310 // Build a type descriptor for a map type.
6313 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6315 Location bloc
= Linemap::predeclared_location();
6317 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
6319 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
6321 Expression_list
* vals
= new Expression_list();
6324 Struct_field_list::const_iterator p
= fields
->begin();
6325 go_assert(p
->is_field_name("commonType"));
6326 vals
->push_back(this->type_descriptor_constructor(gogo
,
6327 RUNTIME_TYPE_KIND_CHAN
,
6331 go_assert(p
->is_field_name("elem"));
6332 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
6335 go_assert(p
->is_field_name("dir"));
6336 // These bits must match the ones in libgo/runtime/go-type.h.
6338 if (this->may_receive_
)
6340 if (this->may_send_
)
6343 mpz_init_set_ui(iv
, val
);
6344 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
6348 go_assert(p
== fields
->end());
6350 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
6353 // Reflection string.
6356 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6358 if (!this->may_send_
)
6360 ret
->append("chan");
6361 if (!this->may_receive_
)
6363 ret
->push_back(' ');
6364 this->append_reflection(this->element_type_
, gogo
, ret
);
6370 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6372 ret
->push_back('C');
6373 this->append_mangled_name(this->element_type_
, gogo
, ret
);
6374 if (this->may_send_
)
6375 ret
->push_back('s');
6376 if (this->may_receive_
)
6377 ret
->push_back('r');
6378 ret
->push_back('e');
6384 Channel_type::do_export(Export
* exp
) const
6386 exp
->write_c_string("chan ");
6387 if (this->may_send_
&& !this->may_receive_
)
6388 exp
->write_c_string("-< ");
6389 else if (this->may_receive_
&& !this->may_send_
)
6390 exp
->write_c_string("<- ");
6391 exp
->write_type(this->element_type_
);
6397 Channel_type::do_import(Import
* imp
)
6399 imp
->require_c_string("chan ");
6403 if (imp
->match_c_string("-< "))
6407 may_receive
= false;
6409 else if (imp
->match_c_string("<- "))
6421 Type
* element_type
= imp
->read_type();
6423 return Type::make_channel_type(may_send
, may_receive
, element_type
);
6426 // Make a new channel type.
6429 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
6431 return new Channel_type(send
, receive
, element_type
);
6434 // Class Interface_type.
6439 Interface_type::do_traverse(Traverse
* traverse
)
6441 Typed_identifier_list
* methods
= (this->methods_are_finalized_
6442 ? this->all_methods_
6443 : this->parse_methods_
);
6444 if (methods
== NULL
)
6445 return TRAVERSE_CONTINUE
;
6446 return methods
->traverse(traverse
);
6449 // Finalize the methods. This handles interface inheritance.
6452 Interface_type::finalize_methods()
6454 if (this->methods_are_finalized_
)
6456 this->methods_are_finalized_
= true;
6457 if (this->parse_methods_
== NULL
)
6460 this->all_methods_
= new Typed_identifier_list();
6461 this->all_methods_
->reserve(this->parse_methods_
->size());
6462 Typed_identifier_list inherit
;
6463 for (Typed_identifier_list::const_iterator pm
=
6464 this->parse_methods_
->begin();
6465 pm
!= this->parse_methods_
->end();
6468 const Typed_identifier
* p
= &*pm
;
6469 if (p
->name().empty())
6470 inherit
.push_back(*p
);
6471 else if (this->find_method(p
->name()) == NULL
)
6472 this->all_methods_
->push_back(*p
);
6474 error_at(p
->location(), "duplicate method %qs",
6475 Gogo::message_name(p
->name()).c_str());
6478 std::vector
<Named_type
*> seen
;
6479 seen
.reserve(inherit
.size());
6480 bool issued_recursive_error
= false;
6481 while (!inherit
.empty())
6483 Type
* t
= inherit
.back().type();
6484 Location tl
= inherit
.back().location();
6487 Interface_type
* it
= t
->interface_type();
6491 error_at(tl
, "interface contains embedded non-interface");
6496 if (!issued_recursive_error
)
6498 error_at(tl
, "invalid recursive interface");
6499 issued_recursive_error
= true;
6504 Named_type
* nt
= t
->named_type();
6505 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
6507 std::vector
<Named_type
*>::const_iterator q
;
6508 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
6512 error_at(tl
, "inherited interface loop");
6516 if (q
!= seen
.end())
6521 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
6522 if (imethods
== NULL
)
6524 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
6525 q
!= imethods
->end();
6528 if (q
->name().empty())
6529 inherit
.push_back(*q
);
6530 else if (this->find_method(q
->name()) == NULL
)
6531 this->all_methods_
->push_back(Typed_identifier(q
->name(),
6534 error_at(tl
, "inherited method %qs is ambiguous",
6535 Gogo::message_name(q
->name()).c_str());
6539 if (!this->all_methods_
->empty())
6540 this->all_methods_
->sort_by_name();
6543 delete this->all_methods_
;
6544 this->all_methods_
= NULL
;
6548 // Return the method NAME, or NULL.
6550 const Typed_identifier
*
6551 Interface_type::find_method(const std::string
& name
) const
6553 go_assert(this->methods_are_finalized_
);
6554 if (this->all_methods_
== NULL
)
6556 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6557 p
!= this->all_methods_
->end();
6559 if (p
->name() == name
)
6564 // Return the method index.
6567 Interface_type::method_index(const std::string
& name
) const
6569 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
6571 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6572 p
!= this->all_methods_
->end();
6574 if (p
->name() == name
)
6579 // Return whether NAME is an unexported method, for better error
6583 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
6585 go_assert(this->methods_are_finalized_
);
6586 if (this->all_methods_
== NULL
)
6588 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6589 p
!= this->all_methods_
->end();
6592 const std::string
& method_name(p
->name());
6593 if (Gogo::is_hidden_name(method_name
)
6594 && name
== Gogo::unpack_hidden_name(method_name
)
6595 && gogo
->pack_hidden_name(name
, false) != method_name
)
6601 // Whether this type is identical with T.
6604 Interface_type::is_identical(const Interface_type
* t
,
6605 bool errors_are_identical
) const
6607 // If methods have not been finalized, then we are asking whether
6608 // func redeclarations are the same. This is an error, so for
6609 // simplicity we say they are never the same.
6610 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
6613 // We require the same methods with the same types. The methods
6614 // have already been sorted.
6615 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
6616 return this->all_methods_
== t
->all_methods_
;
6618 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
6621 Assume_identical
* hold_ai
= this->assume_identical_
;
6622 Assume_identical ai
;
6626 this->assume_identical_
= &ai
;
6628 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
6629 Typed_identifier_list::const_iterator p2
;
6630 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
6632 if (p1
== this->all_methods_
->end())
6634 if (p1
->name() != p2
->name()
6635 || !Type::are_identical(p1
->type(), p2
->type(),
6636 errors_are_identical
, NULL
))
6640 this->assume_identical_
= hold_ai
;
6642 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
6645 // Return true if T1 and T2 are assumed to be identical during a type
6649 Interface_type::assume_identical(const Interface_type
* t1
,
6650 const Interface_type
* t2
) const
6652 for (Assume_identical
* p
= this->assume_identical_
;
6655 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
6660 // Whether we can assign the interface type T to this type. The types
6661 // are known to not be identical. An interface assignment is only
6662 // permitted if T is known to implement all methods in THIS.
6663 // Otherwise a type guard is required.
6666 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
6667 std::string
* reason
) const
6669 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
6670 if (this->all_methods_
== NULL
)
6672 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6673 p
!= this->all_methods_
->end();
6676 const Typed_identifier
* m
= t
->find_method(p
->name());
6682 snprintf(buf
, sizeof buf
,
6683 _("need explicit conversion; missing method %s%s%s"),
6684 open_quote
, Gogo::message_name(p
->name()).c_str(),
6686 reason
->assign(buf
);
6691 std::string subreason
;
6692 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
6696 std::string n
= Gogo::message_name(p
->name());
6697 size_t len
= 100 + n
.length() + subreason
.length();
6698 char* buf
= new char[len
];
6699 if (subreason
.empty())
6700 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
6701 open_quote
, n
.c_str(), close_quote
);
6704 _("incompatible type for method %s%s%s (%s)"),
6705 open_quote
, n
.c_str(), close_quote
,
6707 reason
->assign(buf
);
6720 Interface_type::do_hash_for_method(Gogo
*) const
6722 go_assert(this->methods_are_finalized_
);
6723 unsigned int ret
= 0;
6724 if (this->all_methods_
!= NULL
)
6726 for (Typed_identifier_list::const_iterator p
=
6727 this->all_methods_
->begin();
6728 p
!= this->all_methods_
->end();
6731 ret
= Type::hash_string(p
->name(), ret
);
6732 // We don't use the method type in the hash, to avoid
6733 // infinite recursion if an interface method uses a type
6734 // which is an interface which inherits from the interface
6736 // type T interface { F() interface {T}}
6743 // Return true if T implements the interface. If it does not, and
6744 // REASON is not NULL, set *REASON to a useful error message.
6747 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
6749 go_assert(this->methods_are_finalized_
);
6750 if (this->all_methods_
== NULL
)
6753 bool is_pointer
= false;
6754 const Named_type
* nt
= t
->named_type();
6755 const Struct_type
* st
= t
->struct_type();
6756 // If we start with a named type, we don't dereference it to find
6760 const Type
* pt
= t
->points_to();
6763 // If T is a pointer to a named type, then we need to look at
6764 // the type to which it points.
6766 nt
= pt
->named_type();
6767 st
= pt
->struct_type();
6771 // If we have a named type, get the methods from it rather than from
6776 // Only named and struct types have methods.
6777 if (nt
== NULL
&& st
== NULL
)
6781 if (t
->points_to() != NULL
6782 && t
->points_to()->interface_type() != NULL
)
6783 reason
->assign(_("pointer to interface type has no methods"));
6785 reason
->assign(_("type has no methods"));
6790 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
6794 if (t
->points_to() != NULL
6795 && t
->points_to()->interface_type() != NULL
)
6796 reason
->assign(_("pointer to interface type has no methods"));
6798 reason
->assign(_("type has no methods"));
6803 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6804 p
!= this->all_methods_
->end();
6807 bool is_ambiguous
= false;
6808 Method
* m
= (nt
!= NULL
6809 ? nt
->method_function(p
->name(), &is_ambiguous
)
6810 : st
->method_function(p
->name(), &is_ambiguous
));
6815 std::string n
= Gogo::message_name(p
->name());
6816 size_t len
= n
.length() + 100;
6817 char* buf
= new char[len
];
6819 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
6820 open_quote
, n
.c_str(), close_quote
);
6822 snprintf(buf
, len
, _("missing method %s%s%s"),
6823 open_quote
, n
.c_str(), close_quote
);
6824 reason
->assign(buf
);
6830 Function_type
*p_fn_type
= p
->type()->function_type();
6831 Function_type
* m_fn_type
= m
->type()->function_type();
6832 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
6833 std::string subreason
;
6834 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
6838 std::string n
= Gogo::message_name(p
->name());
6839 size_t len
= 100 + n
.length() + subreason
.length();
6840 char* buf
= new char[len
];
6841 if (subreason
.empty())
6842 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
6843 open_quote
, n
.c_str(), close_quote
);
6846 _("incompatible type for method %s%s%s (%s)"),
6847 open_quote
, n
.c_str(), close_quote
,
6849 reason
->assign(buf
);
6855 if (!is_pointer
&& !m
->is_value_method())
6859 std::string n
= Gogo::message_name(p
->name());
6860 size_t len
= 100 + n
.length();
6861 char* buf
= new char[len
];
6863 _("method %s%s%s requires a pointer receiver"),
6864 open_quote
, n
.c_str(), close_quote
);
6865 reason
->assign(buf
);
6875 // Return the backend representation of the empty interface type. We
6876 // use the same struct for all empty interfaces.
6879 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
6881 static Btype
* empty_interface_type
;
6882 if (empty_interface_type
== NULL
)
6884 std::vector
<Backend::Btyped_identifier
> bfields(2);
6886 Location bloc
= Linemap::predeclared_location();
6888 Type
* pdt
= Type::make_type_descriptor_ptr_type();
6889 bfields
[0].name
= "__type_descriptor";
6890 bfields
[0].btype
= pdt
->get_backend(gogo
);
6891 bfields
[0].location
= bloc
;
6893 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6894 bfields
[1].name
= "__object";
6895 bfields
[1].btype
= vt
->get_backend(gogo
);
6896 bfields
[1].location
= bloc
;
6898 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
6900 return empty_interface_type
;
6903 // Return the fields of a non-empty interface type. This is not
6904 // declared in types.h so that types.h doesn't have to #include
6908 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
6909 bool use_placeholder
,
6910 std::vector
<Backend::Btyped_identifier
>* bfields
)
6912 Location loc
= type
->location();
6914 std::vector
<Backend::Btyped_identifier
> mfields(type
->methods()->size() + 1);
6916 Type
* pdt
= Type::make_type_descriptor_ptr_type();
6917 mfields
[0].name
= "__type_descriptor";
6918 mfields
[0].btype
= pdt
->get_backend(gogo
);
6919 mfields
[0].location
= loc
;
6921 std::string last_name
= "";
6923 for (Typed_identifier_list::const_iterator p
= type
->methods()->begin();
6924 p
!= type
->methods()->end();
6927 // The type of the method in Go only includes the parameters.
6928 // The actual method also has a receiver, which is always a
6929 // pointer. We need to add that pointer type here in order to
6930 // generate the correct type for the backend.
6931 Function_type
* ft
= p
->type()->function_type();
6932 go_assert(ft
->receiver() == NULL
);
6934 const Typed_identifier_list
* params
= ft
->parameters();
6935 Typed_identifier_list
* mparams
= new Typed_identifier_list();
6937 mparams
->reserve(params
->size() + 1);
6938 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6939 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
6942 for (Typed_identifier_list::const_iterator pp
= params
->begin();
6943 pp
!= params
->end();
6945 mparams
->push_back(*pp
);
6948 Typed_identifier_list
* mresults
= (ft
->results() == NULL
6950 : ft
->results()->copy());
6951 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
6954 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
6955 mfields
[i
].btype
= (use_placeholder
6956 ? mft
->get_backend_placeholder(gogo
)
6957 : mft
->get_backend(gogo
));
6958 mfields
[i
].location
= loc
;
6959 // Sanity check: the names should be sorted.
6960 go_assert(p
->name() > last_name
);
6961 last_name
= p
->name();
6964 Btype
* methods
= gogo
->backend()->struct_type(mfields
);
6968 (*bfields
)[0].name
= "__methods";
6969 (*bfields
)[0].btype
= gogo
->backend()->pointer_type(methods
);
6970 (*bfields
)[0].location
= loc
;
6972 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6973 (*bfields
)[1].name
= "__object";
6974 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
6975 (*bfields
)[1].location
= Linemap::predeclared_location();
6978 // Return a tree for an interface type. An interface is a pointer to
6979 // a struct. The struct has three fields. The first field is a
6980 // pointer to the type descriptor for the dynamic type of the object.
6981 // The second field is a pointer to a table of methods for the
6982 // interface to be used with the object. The third field is the value
6983 // of the object itself.
6986 Interface_type::do_get_backend(Gogo
* gogo
)
6988 if (this->is_empty())
6989 return Interface_type::get_backend_empty_interface_type(gogo
);
6992 if (this->interface_btype_
!= NULL
)
6993 return this->interface_btype_
;
6994 this->interface_btype_
=
6995 gogo
->backend()->placeholder_struct_type("", this->location_
);
6996 std::vector
<Backend::Btyped_identifier
> bfields
;
6997 get_backend_interface_fields(gogo
, this, false, &bfields
);
6998 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
7000 this->interface_btype_
= gogo
->backend()->error_type();
7001 return this->interface_btype_
;
7005 // Finish the backend representation of the methods.
7008 Interface_type::finish_backend_methods(Gogo
* gogo
)
7010 if (!this->interface_type()->is_empty())
7012 const Typed_identifier_list
* methods
= this->methods();
7013 if (methods
!= NULL
)
7015 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7016 p
!= methods
->end();
7018 p
->type()->get_backend(gogo
);
7023 // The type of an interface type descriptor.
7026 Interface_type::make_interface_type_descriptor_type()
7031 Type
* tdt
= Type::make_type_descriptor_type();
7032 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7034 Type
* string_type
= Type::lookup_string_type();
7035 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
7038 Type::make_builtin_struct_type(3,
7039 "name", pointer_string_type
,
7040 "pkgPath", pointer_string_type
,
7043 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
7045 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
7047 Struct_type
* s
= Type::make_builtin_struct_type(2,
7049 "methods", slice_nsm
);
7051 ret
= Type::make_builtin_named_type("InterfaceType", s
);
7057 // Build a type descriptor for an interface type.
7060 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7062 Location bloc
= Linemap::predeclared_location();
7064 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
7066 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
7068 Expression_list
* ivals
= new Expression_list();
7071 Struct_field_list::const_iterator pif
= ifields
->begin();
7072 go_assert(pif
->is_field_name("commonType"));
7073 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
7074 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
7078 go_assert(pif
->is_field_name("methods"));
7080 Expression_list
* methods
= new Expression_list();
7081 if (this->all_methods_
!= NULL
)
7083 Type
* elemtype
= pif
->type()->array_type()->element_type();
7085 methods
->reserve(this->all_methods_
->size());
7086 for (Typed_identifier_list::const_iterator pm
=
7087 this->all_methods_
->begin();
7088 pm
!= this->all_methods_
->end();
7091 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
7093 Expression_list
* mvals
= new Expression_list();
7096 Struct_field_list::const_iterator pmf
= mfields
->begin();
7097 go_assert(pmf
->is_field_name("name"));
7098 std::string s
= Gogo::unpack_hidden_name(pm
->name());
7099 Expression
* e
= Expression::make_string(s
, bloc
);
7100 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7103 go_assert(pmf
->is_field_name("pkgPath"));
7104 if (!Gogo::is_hidden_name(pm
->name()))
7105 mvals
->push_back(Expression::make_nil(bloc
));
7108 s
= Gogo::hidden_name_pkgpath(pm
->name());
7109 e
= Expression::make_string(s
, bloc
);
7110 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7114 go_assert(pmf
->is_field_name("typ"));
7115 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
7118 go_assert(pmf
== mfields
->end());
7120 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
7122 methods
->push_back(e
);
7126 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
7130 go_assert(pif
== ifields
->end());
7132 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
7135 // Reflection string.
7138 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7140 ret
->append("interface {");
7141 const Typed_identifier_list
* methods
= this->parse_methods_
;
7142 if (methods
!= NULL
)
7144 ret
->push_back(' ');
7145 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7146 p
!= methods
->end();
7149 if (p
!= methods
->begin())
7151 if (p
->name().empty())
7152 this->append_reflection(p
->type(), gogo
, ret
);
7155 if (!Gogo::is_hidden_name(p
->name()))
7156 ret
->append(p
->name());
7157 else if (gogo
->pkgpath_from_option())
7158 ret
->append(p
->name().substr(1));
7161 // If no -fgo-pkgpath option, backward compatibility
7162 // for how this used to work before -fgo-pkgpath was
7164 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
7165 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
7166 ret
->push_back('.');
7167 ret
->append(Gogo::unpack_hidden_name(p
->name()));
7169 std::string sub
= p
->type()->reflection(gogo
);
7170 go_assert(sub
.compare(0, 4, "func") == 0);
7171 sub
= sub
.substr(4);
7175 ret
->push_back(' ');
7183 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7185 go_assert(this->methods_are_finalized_
);
7187 ret
->push_back('I');
7189 const Typed_identifier_list
* methods
= this->all_methods_
;
7190 if (methods
!= NULL
&& !this->seen_
)
7193 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7194 p
!= methods
->end();
7197 if (!p
->name().empty())
7200 if (!Gogo::is_hidden_name(p
->name()))
7205 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
7206 n
.append(Gogo::pkgpath_for_symbol(pkgpath
));
7208 n
.append(Gogo::unpack_hidden_name(p
->name()));
7211 snprintf(buf
, sizeof buf
, "%u_",
7212 static_cast<unsigned int>(n
.length()));
7216 this->append_mangled_name(p
->type(), gogo
, ret
);
7218 this->seen_
= false;
7221 ret
->push_back('e');
7227 Interface_type::do_export(Export
* exp
) const
7229 exp
->write_c_string("interface { ");
7231 const Typed_identifier_list
* methods
= this->parse_methods_
;
7232 if (methods
!= NULL
)
7234 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
7235 pm
!= methods
->end();
7238 if (pm
->name().empty())
7240 exp
->write_c_string("? ");
7241 exp
->write_type(pm
->type());
7245 exp
->write_string(pm
->name());
7246 exp
->write_c_string(" (");
7248 const Function_type
* fntype
= pm
->type()->function_type();
7251 const Typed_identifier_list
* parameters
= fntype
->parameters();
7252 if (parameters
!= NULL
)
7254 bool is_varargs
= fntype
->is_varargs();
7255 for (Typed_identifier_list::const_iterator pp
=
7256 parameters
->begin();
7257 pp
!= parameters
->end();
7263 exp
->write_c_string(", ");
7264 exp
->write_name(pp
->name());
7265 exp
->write_c_string(" ");
7266 if (!is_varargs
|| pp
+ 1 != parameters
->end())
7267 exp
->write_type(pp
->type());
7270 exp
->write_c_string("...");
7271 Type
*pptype
= pp
->type();
7272 exp
->write_type(pptype
->array_type()->element_type());
7277 exp
->write_c_string(")");
7279 const Typed_identifier_list
* results
= fntype
->results();
7280 if (results
!= NULL
)
7282 exp
->write_c_string(" ");
7283 if (results
->size() == 1 && results
->begin()->name().empty())
7284 exp
->write_type(results
->begin()->type());
7288 exp
->write_c_string("(");
7289 for (Typed_identifier_list::const_iterator p
=
7291 p
!= results
->end();
7297 exp
->write_c_string(", ");
7298 exp
->write_name(p
->name());
7299 exp
->write_c_string(" ");
7300 exp
->write_type(p
->type());
7302 exp
->write_c_string(")");
7307 exp
->write_c_string("; ");
7311 exp
->write_c_string("}");
7314 // Import an interface type.
7317 Interface_type::do_import(Import
* imp
)
7319 imp
->require_c_string("interface { ");
7321 Typed_identifier_list
* methods
= new Typed_identifier_list
;
7322 while (imp
->peek_char() != '}')
7324 std::string name
= imp
->read_identifier();
7328 imp
->require_c_string(" ");
7329 Type
* t
= imp
->read_type();
7330 methods
->push_back(Typed_identifier("", t
, imp
->location()));
7331 imp
->require_c_string("; ");
7335 imp
->require_c_string(" (");
7337 Typed_identifier_list
* parameters
;
7338 bool is_varargs
= false;
7339 if (imp
->peek_char() == ')')
7343 parameters
= new Typed_identifier_list
;
7346 std::string name
= imp
->read_name();
7347 imp
->require_c_string(" ");
7349 if (imp
->match_c_string("..."))
7355 Type
* ptype
= imp
->read_type();
7357 ptype
= Type::make_array_type(ptype
, NULL
);
7358 parameters
->push_back(Typed_identifier(name
, ptype
,
7360 if (imp
->peek_char() != ',')
7362 go_assert(!is_varargs
);
7363 imp
->require_c_string(", ");
7366 imp
->require_c_string(")");
7368 Typed_identifier_list
* results
;
7369 if (imp
->peek_char() != ' ')
7373 results
= new Typed_identifier_list
;
7375 if (imp
->peek_char() != '(')
7377 Type
* rtype
= imp
->read_type();
7378 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
7385 std::string name
= imp
->read_name();
7386 imp
->require_c_string(" ");
7387 Type
* rtype
= imp
->read_type();
7388 results
->push_back(Typed_identifier(name
, rtype
,
7390 if (imp
->peek_char() != ',')
7392 imp
->require_c_string(", ");
7394 imp
->require_c_string(")");
7398 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
7402 fntype
->set_is_varargs();
7403 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
7405 imp
->require_c_string("; ");
7408 imp
->require_c_string("}");
7410 if (methods
->empty())
7416 return Type::make_interface_type(methods
, imp
->location());
7419 // Make an interface type.
7422 Type::make_interface_type(Typed_identifier_list
* methods
,
7425 return new Interface_type(methods
, location
);
7428 // Make an empty interface type.
7431 Type::make_empty_interface_type(Location location
)
7433 Interface_type
* ret
= new Interface_type(NULL
, location
);
7434 ret
->finalize_methods();
7440 // Bind a method to an object.
7443 Method::bind_method(Expression
* expr
, Location location
) const
7445 if (this->stub_
== NULL
)
7447 // When there is no stub object, the binding is determined by
7449 return this->do_bind_method(expr
, location
);
7451 return Expression::make_bound_method(expr
, this->stub_
, location
);
7454 // Return the named object associated with a method. This may only be
7455 // called after methods are finalized.
7458 Method::named_object() const
7460 if (this->stub_
!= NULL
)
7462 return this->do_named_object();
7465 // Class Named_method.
7467 // The type of the method.
7470 Named_method::do_type() const
7472 if (this->named_object_
->is_function())
7473 return this->named_object_
->func_value()->type();
7474 else if (this->named_object_
->is_function_declaration())
7475 return this->named_object_
->func_declaration_value()->type();
7480 // Return the location of the method receiver.
7483 Named_method::do_receiver_location() const
7485 return this->do_type()->receiver()->location();
7488 // Bind a method to an object.
7491 Named_method::do_bind_method(Expression
* expr
, Location location
) const
7493 Named_object
* no
= this->named_object_
;
7494 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, no
,
7496 // If this is not a local method, and it does not use a stub, then
7497 // the real method expects a different type. We need to cast the
7499 if (this->depth() > 0 && !this->needs_stub_method())
7501 Function_type
* ftype
= this->do_type();
7502 go_assert(ftype
->is_method());
7503 Type
* frtype
= ftype
->receiver()->type();
7504 bme
->set_first_argument_type(frtype
);
7509 // Class Interface_method.
7511 // Bind a method to an object.
7514 Interface_method::do_bind_method(Expression
* expr
,
7515 Location location
) const
7517 return Expression::make_interface_field_reference(expr
, this->name_
,
7523 // Insert a new method. Return true if it was inserted, false
7527 Methods::insert(const std::string
& name
, Method
* m
)
7529 std::pair
<Method_map::iterator
, bool> ins
=
7530 this->methods_
.insert(std::make_pair(name
, m
));
7535 Method
* old_method
= ins
.first
->second
;
7536 if (m
->depth() < old_method
->depth())
7539 ins
.first
->second
= m
;
7544 if (m
->depth() == old_method
->depth())
7545 old_method
->set_is_ambiguous();
7551 // Return the number of unambiguous methods.
7554 Methods::count() const
7557 for (Method_map::const_iterator p
= this->methods_
.begin();
7558 p
!= this->methods_
.end();
7560 if (!p
->second
->is_ambiguous())
7565 // Class Named_type.
7567 // Return the name of the type.
7570 Named_type::name() const
7572 return this->named_object_
->name();
7575 // Return the name of the type to use in an error message.
7578 Named_type::message_name() const
7580 return this->named_object_
->message_name();
7583 // Whether this is an alias. There are currently only two aliases so
7584 // we just recognize them by name.
7587 Named_type::is_alias() const
7589 if (!this->is_builtin())
7591 const std::string
& name(this->name());
7592 return name
== "byte" || name
== "rune";
7595 // Return the base type for this type. We have to be careful about
7596 // circular type definitions, which are invalid but may be seen here.
7599 Named_type::named_base()
7604 Type
* ret
= this->type_
->base();
7605 this->seen_
= false;
7610 Named_type::named_base() const
7615 const Type
* ret
= this->type_
->base();
7616 this->seen_
= false;
7620 // Return whether this is an error type. We have to be careful about
7621 // circular type definitions, which are invalid but may be seen here.
7624 Named_type::is_named_error_type() const
7629 bool ret
= this->type_
->is_error_type();
7630 this->seen_
= false;
7634 // Whether this type is comparable. We have to be careful about
7635 // circular type definitions.
7638 Named_type::named_type_is_comparable(std::string
* reason
) const
7643 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
7644 this->type_
, reason
);
7645 this->seen_
= false;
7649 // Add a method to this type.
7652 Named_type::add_method(const std::string
& name
, Function
* function
)
7654 if (this->local_methods_
== NULL
)
7655 this->local_methods_
= new Bindings(NULL
);
7656 return this->local_methods_
->add_function(name
, NULL
, function
);
7659 // Add a method declaration to this type.
7662 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
7663 Function_type
* type
,
7666 if (this->local_methods_
== NULL
)
7667 this->local_methods_
= new Bindings(NULL
);
7668 return this->local_methods_
->add_function_declaration(name
, package
, type
,
7672 // Add an existing method to this type.
7675 Named_type::add_existing_method(Named_object
* no
)
7677 if (this->local_methods_
== NULL
)
7678 this->local_methods_
= new Bindings(NULL
);
7679 this->local_methods_
->add_named_object(no
);
7682 // Look for a local method NAME, and returns its named object, or NULL
7686 Named_type::find_local_method(const std::string
& name
) const
7688 if (this->local_methods_
== NULL
)
7690 return this->local_methods_
->lookup(name
);
7693 // Return whether NAME is an unexported field or method, for better
7697 Named_type::is_unexported_local_method(Gogo
* gogo
,
7698 const std::string
& name
) const
7700 Bindings
* methods
= this->local_methods_
;
7701 if (methods
!= NULL
)
7703 for (Bindings::const_declarations_iterator p
=
7704 methods
->begin_declarations();
7705 p
!= methods
->end_declarations();
7708 if (Gogo::is_hidden_name(p
->first
)
7709 && name
== Gogo::unpack_hidden_name(p
->first
)
7710 && gogo
->pack_hidden_name(name
, false) != p
->first
)
7717 // Build the complete list of methods for this type, which means
7718 // recursively including all methods for anonymous fields. Create all
7722 Named_type::finalize_methods(Gogo
* gogo
)
7724 if (this->all_methods_
!= NULL
)
7727 if (this->local_methods_
!= NULL
7728 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
7730 const Bindings
* lm
= this->local_methods_
;
7731 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
7732 p
!= lm
->end_declarations();
7734 error_at(p
->second
->location(),
7735 "invalid pointer or interface receiver type");
7736 delete this->local_methods_
;
7737 this->local_methods_
= NULL
;
7741 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
7744 // Return the method NAME, or NULL if there isn't one or if it is
7745 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
7749 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
7751 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
7754 // Return a pointer to the interface method table for this type for
7755 // the interface INTERFACE. IS_POINTER is true if this is for a
7759 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
7762 return Type::interface_method_table(gogo
, this, interface
, is_pointer
,
7763 &this->interface_method_tables_
,
7764 &this->pointer_interface_method_tables_
);
7767 // Return whether a named type has any hidden fields.
7770 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
7775 bool ret
= this->type_
->has_hidden_fields(this, reason
);
7776 this->seen_
= false;
7780 // Look for a use of a complete type within another type. This is
7781 // used to check that we don't try to use a type within itself.
7783 class Find_type_use
: public Traverse
7786 Find_type_use(Named_type
* find_type
)
7787 : Traverse(traverse_types
),
7788 find_type_(find_type
), found_(false)
7791 // Whether we found the type.
7794 { return this->found_
; }
7801 // The type we are looking for.
7802 Named_type
* find_type_
;
7803 // Whether we found the type.
7807 // Check for FIND_TYPE in TYPE.
7810 Find_type_use::type(Type
* type
)
7812 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
7814 this->found_
= true;
7815 return TRAVERSE_EXIT
;
7818 // It's OK if we see a reference to the type in any type which is
7819 // essentially a pointer: a pointer, a slice, a function, a map, or
7821 if (type
->points_to() != NULL
7822 || type
->is_slice_type()
7823 || type
->function_type() != NULL
7824 || type
->map_type() != NULL
7825 || type
->channel_type() != NULL
)
7826 return TRAVERSE_SKIP_COMPONENTS
;
7828 // For an interface, a reference to the type in a method type should
7829 // be ignored, but we have to consider direct inheritance. When
7830 // this is called, there may be cases of direct inheritance
7831 // represented as a method with no name.
7832 if (type
->interface_type() != NULL
)
7834 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
7835 if (methods
!= NULL
)
7837 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7838 p
!= methods
->end();
7841 if (p
->name().empty())
7843 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
7844 return TRAVERSE_EXIT
;
7848 return TRAVERSE_SKIP_COMPONENTS
;
7851 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
7852 // to convert TYPE to the backend representation before we convert
7854 if (type
->named_type() != NULL
)
7856 switch (type
->base()->classification())
7858 case Type::TYPE_ERROR
:
7859 case Type::TYPE_BOOLEAN
:
7860 case Type::TYPE_INTEGER
:
7861 case Type::TYPE_FLOAT
:
7862 case Type::TYPE_COMPLEX
:
7863 case Type::TYPE_STRING
:
7864 case Type::TYPE_NIL
:
7867 case Type::TYPE_ARRAY
:
7868 case Type::TYPE_STRUCT
:
7869 this->find_type_
->add_dependency(type
->named_type());
7872 case Type::TYPE_NAMED
:
7873 case Type::TYPE_FORWARD
:
7874 go_assert(saw_errors());
7877 case Type::TYPE_VOID
:
7878 case Type::TYPE_SINK
:
7879 case Type::TYPE_FUNCTION
:
7880 case Type::TYPE_POINTER
:
7881 case Type::TYPE_CALL_MULTIPLE_RESULT
:
7882 case Type::TYPE_MAP
:
7883 case Type::TYPE_CHANNEL
:
7884 case Type::TYPE_INTERFACE
:
7890 return TRAVERSE_CONTINUE
;
7893 // Verify that a named type does not refer to itself.
7896 Named_type::do_verify()
7898 if (this->is_verified_
)
7900 this->is_verified_
= true;
7902 Find_type_use
find(this);
7903 Type::traverse(this->type_
, &find
);
7906 error_at(this->location_
, "invalid recursive type %qs",
7907 this->message_name().c_str());
7908 this->is_error_
= true;
7912 // Check whether any of the local methods overloads an existing
7913 // struct field or interface method. We don't need to check the
7914 // list of methods against itself: that is handled by the Bindings
7916 if (this->local_methods_
!= NULL
)
7918 Struct_type
* st
= this->type_
->struct_type();
7921 for (Bindings::const_declarations_iterator p
=
7922 this->local_methods_
->begin_declarations();
7923 p
!= this->local_methods_
->end_declarations();
7926 const std::string
& name(p
->first
);
7927 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
7929 error_at(p
->second
->location(),
7930 "method %qs redeclares struct field name",
7931 Gogo::message_name(name
).c_str());
7940 // Return whether this type is or contains a pointer.
7943 Named_type::do_has_pointer() const
7948 bool ret
= this->type_
->has_pointer();
7949 this->seen_
= false;
7953 // Return whether comparisons for this type can use the identity
7957 Named_type::do_compare_is_identity(Gogo
* gogo
) const
7959 // We don't use this->seen_ here because compare_is_identity may
7960 // call base() later, and that will mess up if seen_ is set here.
7961 if (this->seen_in_compare_is_identity_
)
7963 this->seen_in_compare_is_identity_
= true;
7964 bool ret
= this->type_
->compare_is_identity(gogo
);
7965 this->seen_in_compare_is_identity_
= false;
7969 // Return a hash code. This is used for method lookup. We simply
7970 // hash on the name itself.
7973 Named_type::do_hash_for_method(Gogo
* gogo
) const
7975 if (this->is_alias())
7976 return this->type_
->named_type()->do_hash_for_method(gogo
);
7978 const std::string
& name(this->named_object()->name());
7979 unsigned int ret
= Type::hash_string(name
, 0);
7981 // GOGO will be NULL here when called from Type_hash_identical.
7982 // That is OK because that is only used for internal hash tables
7983 // where we are going to be comparing named types for equality. In
7984 // other cases, which are cases where the runtime is going to
7985 // compare hash codes to see if the types are the same, we need to
7986 // include the pkgpath in the hash.
7987 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
7989 const Package
* package
= this->named_object()->package();
7990 if (package
== NULL
)
7991 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
7993 ret
= Type::hash_string(package
->pkgpath(), ret
);
7999 // Convert a named type to the backend representation. In order to
8000 // get dependencies right, we fill in a dummy structure for this type,
8001 // then convert all the dependencies, then complete this type. When
8002 // this function is complete, the size of the type is known.
8005 Named_type::convert(Gogo
* gogo
)
8007 if (this->is_error_
|| this->is_converted_
)
8010 this->create_placeholder(gogo
);
8012 // If we are called to turn unsafe.Sizeof into a constant, we may
8013 // not have verified the type yet. We have to make sure it is
8014 // verified, since that sets the list of dependencies.
8017 // Convert all the dependencies. If they refer indirectly back to
8018 // this type, they will pick up the intermediate tree we just
8020 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
8021 p
!= this->dependencies_
.end();
8023 (*p
)->convert(gogo
);
8025 // Complete this type.
8026 Btype
* bt
= this->named_btype_
;
8027 Type
* base
= this->type_
->base();
8028 switch (base
->classification())
8045 // The size of these types is already correct. We don't worry
8046 // about filling them in until later, when we also track
8047 // circular references.
8052 std::vector
<Backend::Btyped_identifier
> bfields
;
8053 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
8055 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8056 bt
= gogo
->backend()->error_type();
8061 // Slice types were completed in create_placeholder.
8062 if (!base
->is_slice_type())
8064 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
8065 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
8066 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
8067 bt
= gogo
->backend()->error_type();
8071 case TYPE_INTERFACE
:
8072 // Interface types were completed in create_placeholder.
8080 case TYPE_CALL_MULTIPLE_RESULT
:
8086 this->named_btype_
= bt
;
8087 this->is_converted_
= true;
8088 this->is_placeholder_
= false;
8091 // Create the placeholder for a named type. This is the first step in
8092 // converting to the backend representation.
8095 Named_type::create_placeholder(Gogo
* gogo
)
8097 if (this->is_error_
)
8098 this->named_btype_
= gogo
->backend()->error_type();
8100 if (this->named_btype_
!= NULL
)
8103 // Create the structure for this type. Note that because we call
8104 // base() here, we don't attempt to represent a named type defined
8105 // as another named type. Instead both named types will point to
8106 // different base representations.
8107 Type
* base
= this->type_
->base();
8109 bool set_name
= true;
8110 switch (base
->classification())
8113 this->is_error_
= true;
8114 this->named_btype_
= gogo
->backend()->error_type();
8124 // These are simple basic types, we can just create them
8126 bt
= Type::get_named_base_btype(gogo
, base
);
8131 // All maps and channels have the same backend representation.
8132 bt
= Type::get_named_base_btype(gogo
, base
);
8138 bool for_function
= base
->classification() == TYPE_FUNCTION
;
8139 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
8147 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8149 this->is_placeholder_
= true;
8154 if (base
->is_slice_type())
8155 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8159 bt
= gogo
->backend()->placeholder_array_type(this->name(),
8161 this->is_placeholder_
= true;
8166 case TYPE_INTERFACE
:
8167 if (base
->interface_type()->is_empty())
8168 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
8171 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8179 case TYPE_CALL_MULTIPLE_RESULT
:
8186 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
8188 this->named_btype_
= bt
;
8190 if (base
->is_slice_type())
8192 // We do not record slices as dependencies of other types,
8193 // because we can fill them in completely here with the final
8195 std::vector
<Backend::Btyped_identifier
> bfields
;
8196 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
8197 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8198 this->named_btype_
= gogo
->backend()->error_type();
8200 else if (base
->interface_type() != NULL
8201 && !base
->interface_type()->is_empty())
8203 // We do not record interfaces as dependencies of other types,
8204 // because we can fill them in completely here with the final
8206 std::vector
<Backend::Btyped_identifier
> bfields
;
8207 get_backend_interface_fields(gogo
, base
->interface_type(), true,
8209 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8210 this->named_btype_
= gogo
->backend()->error_type();
8214 // Get a tree for a named type.
8217 Named_type::do_get_backend(Gogo
* gogo
)
8219 if (this->is_error_
)
8220 return gogo
->backend()->error_type();
8222 Btype
* bt
= this->named_btype_
;
8224 if (!gogo
->named_types_are_converted())
8226 // We have not completed converting named types. NAMED_BTYPE_
8227 // is a placeholder and we shouldn't do anything further.
8231 // We don't build dependencies for types whose sizes do not
8232 // change or are not relevant, so we may see them here while
8233 // converting types.
8234 this->create_placeholder(gogo
);
8235 bt
= this->named_btype_
;
8236 go_assert(bt
!= NULL
);
8240 // We are not converting types. This should only be called if the
8241 // type has already been converted.
8242 if (!this->is_converted_
)
8244 go_assert(saw_errors());
8245 return gogo
->backend()->error_type();
8248 go_assert(bt
!= NULL
);
8250 // Complete the tree.
8251 Type
* base
= this->type_
->base();
8253 switch (base
->classification())
8256 return gogo
->backend()->error_type();
8270 if (!this->seen_in_get_backend_
)
8272 this->seen_in_get_backend_
= true;
8273 base
->struct_type()->finish_backend_fields(gogo
);
8274 this->seen_in_get_backend_
= false;
8279 if (!this->seen_in_get_backend_
)
8281 this->seen_in_get_backend_
= true;
8282 base
->array_type()->finish_backend_element(gogo
);
8283 this->seen_in_get_backend_
= false;
8287 case TYPE_INTERFACE
:
8288 if (!this->seen_in_get_backend_
)
8290 this->seen_in_get_backend_
= true;
8291 base
->interface_type()->finish_backend_methods(gogo
);
8292 this->seen_in_get_backend_
= false;
8297 // Don't build a circular data structure. GENERIC can't handle
8299 if (this->seen_in_get_backend_
)
8301 this->is_circular_
= true;
8302 return gogo
->backend()->circular_pointer_type(bt
, true);
8304 this->seen_in_get_backend_
= true;
8305 bt1
= Type::get_named_base_btype(gogo
, base
);
8306 this->seen_in_get_backend_
= false;
8307 if (this->is_circular_
)
8308 bt1
= gogo
->backend()->circular_pointer_type(bt
, true);
8309 if (!gogo
->backend()->set_placeholder_function_type(bt
, bt1
))
8310 bt
= gogo
->backend()->error_type();
8314 // Don't build a circular data structure. GENERIC can't handle
8316 if (this->seen_in_get_backend_
)
8318 this->is_circular_
= true;
8319 return gogo
->backend()->circular_pointer_type(bt
, false);
8321 this->seen_in_get_backend_
= true;
8322 bt1
= Type::get_named_base_btype(gogo
, base
);
8323 this->seen_in_get_backend_
= false;
8324 if (this->is_circular_
)
8325 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
8326 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
8327 bt
= gogo
->backend()->error_type();
8332 case TYPE_CALL_MULTIPLE_RESULT
:
8341 // Build a type descriptor for a named type.
8344 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8346 if (name
== NULL
&& this->is_alias())
8347 return this->type_
->type_descriptor(gogo
, this->type_
);
8349 // If NAME is not NULL, then we don't really want the type
8350 // descriptor for this type; we want the descriptor for the
8351 // underlying type, giving it the name NAME.
8352 return this->named_type_descriptor(gogo
, this->type_
,
8353 name
== NULL
? this : name
);
8356 // Add to the reflection string. This is used mostly for the name of
8357 // the type used in a type descriptor, not for actual reflection
8361 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8363 if (this->is_alias())
8365 this->append_reflection(this->type_
, gogo
, ret
);
8368 if (!this->is_builtin())
8370 // We handle -fgo-prefix and -fgo-pkgpath differently here for
8371 // compatibility with how the compiler worked before
8372 // -fgo-pkgpath was introduced. When -fgo-pkgpath is specified,
8373 // we use it to make a unique reflection string, so that the
8374 // type canonicalization in the reflect package will work. In
8375 // order to be compatible with the gc compiler, we put tabs into
8376 // the package path, so that the reflect methods can discard it.
8377 const Package
* package
= this->named_object_
->package();
8378 if (gogo
->pkgpath_from_option())
8380 ret
->push_back('\t');
8381 ret
->append(package
!= NULL
8382 ? package
->pkgpath_symbol()
8383 : gogo
->pkgpath_symbol());
8384 ret
->push_back('\t');
8386 ret
->append(package
!= NULL
8387 ? package
->package_name()
8388 : gogo
->package_name());
8389 ret
->push_back('.');
8391 if (this->in_function_
!= NULL
)
8393 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
8394 ret
->push_back('$');
8395 if (this->in_function_index_
> 0)
8398 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
8400 ret
->push_back('$');
8403 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
8406 // Get the mangled name.
8409 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8411 if (this->is_alias())
8413 this->append_mangled_name(this->type_
, gogo
, ret
);
8416 Named_object
* no
= this->named_object_
;
8418 if (this->is_builtin())
8419 go_assert(this->in_function_
== NULL
);
8422 const std::string
& pkgpath(no
->package() == NULL
8423 ? gogo
->pkgpath_symbol()
8424 : no
->package()->pkgpath_symbol());
8426 name
.append(1, '.');
8427 if (this->in_function_
!= NULL
)
8429 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
8430 name
.append(1, '$');
8431 if (this->in_function_index_
> 0)
8434 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
8436 name
.append(1, '$');
8440 name
.append(Gogo::unpack_hidden_name(no
->name()));
8442 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
8447 // Export the type. This is called to export a global type.
8450 Named_type::export_named_type(Export
* exp
, const std::string
&) const
8452 // We don't need to write the name of the type here, because it will
8453 // be written by Export::write_type anyhow.
8454 exp
->write_c_string("type ");
8455 exp
->write_type(this);
8456 exp
->write_c_string(";\n");
8459 // Import a named type.
8462 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
8464 imp
->require_c_string("type ");
8465 Type
*type
= imp
->read_type();
8466 *ptype
= type
->named_type();
8467 go_assert(*ptype
!= NULL
);
8468 imp
->require_c_string(";\n");
8471 // Export the type when it is referenced by another type. In this
8472 // case Export::export_type will already have issued the name.
8475 Named_type::do_export(Export
* exp
) const
8477 exp
->write_type(this->type_
);
8479 // To save space, we only export the methods directly attached to
8481 Bindings
* methods
= this->local_methods_
;
8482 if (methods
== NULL
)
8485 exp
->write_c_string("\n");
8486 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
8487 p
!= methods
->end_definitions();
8490 exp
->write_c_string(" ");
8491 (*p
)->export_named_object(exp
);
8494 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
8495 p
!= methods
->end_declarations();
8498 if (p
->second
->is_function_declaration())
8500 exp
->write_c_string(" ");
8501 p
->second
->export_named_object(exp
);
8506 // Make a named type.
8509 Type::make_named_type(Named_object
* named_object
, Type
* type
,
8512 return new Named_type(named_object
, type
, location
);
8515 // Finalize the methods for TYPE. It will be a named type or a struct
8516 // type. This sets *ALL_METHODS to the list of methods, and builds
8517 // all required stubs.
8520 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
8521 Methods
** all_methods
)
8523 *all_methods
= NULL
;
8524 Types_seen types_seen
;
8525 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
8527 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
8530 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8531 // build up the struct field indexes as we go. DEPTH is the depth of
8532 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8533 // adding these methods for an anonymous field with pointer type.
8534 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8535 // calls the real method. TYPES_SEEN is used to avoid infinite
8539 Type::add_methods_for_type(const Type
* type
,
8540 const Method::Field_indexes
* field_indexes
,
8542 bool is_embedded_pointer
,
8543 bool needs_stub_method
,
8544 Types_seen
* types_seen
,
8547 // Pointer types may not have methods.
8548 if (type
->points_to() != NULL
)
8551 const Named_type
* nt
= type
->named_type();
8554 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
8560 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
8561 is_embedded_pointer
, needs_stub_method
,
8564 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
8565 is_embedded_pointer
, needs_stub_method
,
8566 types_seen
, methods
);
8568 // If we are called with depth > 0, then we are looking at an
8569 // anonymous field of a struct. If such a field has interface type,
8570 // then we need to add the interface methods. We don't want to add
8571 // them when depth == 0, because we will already handle them
8572 // following the usual rules for an interface type.
8574 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
8577 // Add the local methods for the named type NT to *METHODS. The
8578 // parameters are as for add_methods_to_type.
8581 Type::add_local_methods_for_type(const Named_type
* nt
,
8582 const Method::Field_indexes
* field_indexes
,
8584 bool is_embedded_pointer
,
8585 bool needs_stub_method
,
8588 const Bindings
* local_methods
= nt
->local_methods();
8589 if (local_methods
== NULL
)
8592 if (*methods
== NULL
)
8593 *methods
= new Methods();
8595 for (Bindings::const_declarations_iterator p
=
8596 local_methods
->begin_declarations();
8597 p
!= local_methods
->end_declarations();
8600 Named_object
* no
= p
->second
;
8601 bool is_value_method
= (is_embedded_pointer
8602 || !Type::method_expects_pointer(no
));
8603 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
8605 || (depth
> 0 && is_value_method
)));
8606 if (!(*methods
)->insert(no
->name(), m
))
8611 // Add the embedded methods for TYPE to *METHODS. These are the
8612 // methods attached to anonymous fields. The parameters are as for
8613 // add_methods_to_type.
8616 Type::add_embedded_methods_for_type(const Type
* type
,
8617 const Method::Field_indexes
* field_indexes
,
8619 bool is_embedded_pointer
,
8620 bool needs_stub_method
,
8621 Types_seen
* types_seen
,
8624 // Look for anonymous fields in TYPE. TYPE has fields if it is a
8626 const Struct_type
* st
= type
->struct_type();
8630 const Struct_field_list
* fields
= st
->fields();
8635 for (Struct_field_list::const_iterator pf
= fields
->begin();
8636 pf
!= fields
->end();
8639 if (!pf
->is_anonymous())
8642 Type
* ftype
= pf
->type();
8643 bool is_pointer
= false;
8644 if (ftype
->points_to() != NULL
)
8646 ftype
= ftype
->points_to();
8649 Named_type
* fnt
= ftype
->named_type();
8652 // This is an error, but it will be diagnosed elsewhere.
8656 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
8657 sub_field_indexes
->next
= field_indexes
;
8658 sub_field_indexes
->field_index
= i
;
8660 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
8661 (is_embedded_pointer
|| is_pointer
),
8670 // If TYPE is an interface type, then add its method to *METHODS.
8671 // This is for interface methods attached to an anonymous field. The
8672 // parameters are as for add_methods_for_type.
8675 Type::add_interface_methods_for_type(const Type
* type
,
8676 const Method::Field_indexes
* field_indexes
,
8680 const Interface_type
* it
= type
->interface_type();
8684 const Typed_identifier_list
* imethods
= it
->methods();
8685 if (imethods
== NULL
)
8688 if (*methods
== NULL
)
8689 *methods
= new Methods();
8691 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
8692 pm
!= imethods
->end();
8695 Function_type
* fntype
= pm
->type()->function_type();
8698 // This is an error, but it should be reported elsewhere
8699 // when we look at the methods for IT.
8702 go_assert(!fntype
->is_method());
8703 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
8704 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
8705 field_indexes
, depth
);
8706 if (!(*methods
)->insert(pm
->name(), m
))
8711 // Build stub methods for TYPE as needed. METHODS is the set of
8712 // methods for the type. A stub method may be needed when a type
8713 // inherits a method from an anonymous field. When we need the
8714 // address of the method, as in a type descriptor, we need to build a
8715 // little stub which does the required field dereferences and jumps to
8716 // the real method. LOCATION is the location of the type definition.
8719 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
8722 if (methods
== NULL
)
8724 for (Methods::const_iterator p
= methods
->begin();
8725 p
!= methods
->end();
8728 Method
* m
= p
->second
;
8729 if (m
->is_ambiguous() || !m
->needs_stub_method())
8732 const std::string
& name(p
->first
);
8734 // Build a stub method.
8736 const Function_type
* fntype
= m
->type();
8738 static unsigned int counter
;
8740 snprintf(buf
, sizeof buf
, "$this%u", counter
);
8743 Type
* receiver_type
= const_cast<Type
*>(type
);
8744 if (!m
->is_value_method())
8745 receiver_type
= Type::make_pointer_type(receiver_type
);
8746 Location receiver_location
= m
->receiver_location();
8747 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
8750 const Typed_identifier_list
* fnparams
= fntype
->parameters();
8751 Typed_identifier_list
* stub_params
;
8752 if (fnparams
== NULL
|| fnparams
->empty())
8756 // We give each stub parameter a unique name.
8757 stub_params
= new Typed_identifier_list();
8758 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
8759 pp
!= fnparams
->end();
8763 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
8764 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
8770 const Typed_identifier_list
* fnresults
= fntype
->results();
8771 Typed_identifier_list
* stub_results
;
8772 if (fnresults
== NULL
|| fnresults
->empty())
8773 stub_results
= NULL
;
8776 // We create the result parameters without any names, since
8777 // we won't refer to them.
8778 stub_results
= new Typed_identifier_list();
8779 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
8780 pr
!= fnresults
->end();
8782 stub_results
->push_back(Typed_identifier("", pr
->type(),
8786 Function_type
* stub_type
= Type::make_function_type(receiver
,
8789 fntype
->location());
8790 if (fntype
->is_varargs())
8791 stub_type
->set_is_varargs();
8793 // We only create the function in the package which creates the
8795 const Package
* package
;
8796 if (type
->named_type() == NULL
)
8799 package
= type
->named_type()->named_object()->package();
8801 if (package
!= NULL
)
8802 stub
= Named_object::make_function_declaration(name
, package
,
8803 stub_type
, location
);
8806 stub
= gogo
->start_function(name
, stub_type
, false,
8807 fntype
->location());
8808 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
8809 fntype
->is_varargs(), location
);
8810 gogo
->finish_function(fntype
->location());
8813 m
->set_stub_object(stub
);
8817 // Build a stub method which adjusts the receiver as required to call
8818 // METHOD. RECEIVER_NAME is the name we used for the receiver.
8819 // PARAMS is the list of function parameters.
8822 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
8823 const char* receiver_name
,
8824 const Typed_identifier_list
* params
,
8828 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
8829 go_assert(receiver_object
!= NULL
);
8831 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
8832 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
8833 if (expr
->type()->points_to() == NULL
)
8834 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
8836 Expression_list
* arguments
;
8837 if (params
== NULL
|| params
->empty())
8841 arguments
= new Expression_list();
8842 for (Typed_identifier_list::const_iterator p
= params
->begin();
8846 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
8847 go_assert(param
!= NULL
);
8848 Expression
* param_ref
= Expression::make_var_reference(param
,
8850 arguments
->push_back(param_ref
);
8854 Expression
* func
= method
->bind_method(expr
, location
);
8855 go_assert(func
!= NULL
);
8856 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
8858 call
->set_hidden_fields_are_ok();
8859 size_t count
= call
->result_count();
8861 gogo
->add_statement(Statement::make_statement(call
, true));
8864 Expression_list
* retvals
= new Expression_list();
8866 retvals
->push_back(call
);
8869 for (size_t i
= 0; i
< count
; ++i
)
8870 retvals
->push_back(Expression::make_call_result(call
, i
));
8872 Return_statement
* retstat
= Statement::make_return_statement(retvals
,
8875 // We can return values with hidden fields from a stub. This is
8876 // necessary if the method is itself hidden.
8877 retstat
->set_hidden_fields_are_ok();
8879 gogo
->add_statement(retstat
);
8883 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
8884 // in reverse order.
8887 Type::apply_field_indexes(Expression
* expr
,
8888 const Method::Field_indexes
* field_indexes
,
8891 if (field_indexes
== NULL
)
8893 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
8894 Struct_type
* stype
= expr
->type()->deref()->struct_type();
8895 go_assert(stype
!= NULL
8896 && field_indexes
->field_index
< stype
->field_count());
8897 if (expr
->type()->struct_type() == NULL
)
8899 go_assert(expr
->type()->points_to() != NULL
);
8900 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
8901 go_assert(expr
->type()->struct_type() == stype
);
8903 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
8907 // Return whether NO is a method for which the receiver is a pointer.
8910 Type::method_expects_pointer(const Named_object
* no
)
8912 const Function_type
*fntype
;
8913 if (no
->is_function())
8914 fntype
= no
->func_value()->type();
8915 else if (no
->is_function_declaration())
8916 fntype
= no
->func_declaration_value()->type();
8919 return fntype
->receiver()->type()->points_to() != NULL
;
8922 // Given a set of methods for a type, METHODS, return the method NAME,
8923 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
8924 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
8925 // but is ambiguous (and return NULL).
8928 Type::method_function(const Methods
* methods
, const std::string
& name
,
8931 if (is_ambiguous
!= NULL
)
8932 *is_ambiguous
= false;
8933 if (methods
== NULL
)
8935 Methods::const_iterator p
= methods
->find(name
);
8936 if (p
== methods
->end())
8938 Method
* m
= p
->second
;
8939 if (m
->is_ambiguous())
8941 if (is_ambiguous
!= NULL
)
8942 *is_ambiguous
= true;
8948 // Return a pointer to the interface method table for TYPE for the
8949 // interface INTERFACE.
8952 Type::interface_method_table(Gogo
* gogo
, Type
* type
,
8953 const Interface_type
*interface
,
8955 Interface_method_tables
** method_tables
,
8956 Interface_method_tables
** pointer_tables
)
8958 go_assert(!interface
->is_empty());
8960 Interface_method_tables
** pimt
= is_pointer
? method_tables
: pointer_tables
;
8963 *pimt
= new Interface_method_tables(5);
8965 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
8966 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
8970 // This is a new entry in the hash table.
8971 go_assert(ins
.first
->second
== NULL_TREE
);
8972 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
8977 tree decl
= ins
.first
->second
;
8978 if (decl
== error_mark_node
)
8979 return error_mark_node
;
8980 go_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
8981 return build_fold_addr_expr(decl
);
8984 // Look for field or method NAME for TYPE. Return an Expression for
8985 // the field or method bound to EXPR. If there is no such field or
8986 // method, give an appropriate error and return an error expression.
8989 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
8990 const std::string
& name
,
8993 if (type
->deref()->is_error_type())
8994 return Expression::make_error(location
);
8996 const Named_type
* nt
= type
->deref()->named_type();
8997 const Struct_type
* st
= type
->deref()->struct_type();
8998 const Interface_type
* it
= type
->interface_type();
9000 // If this is a pointer to a pointer, then it is possible that the
9001 // pointed-to type has methods.
9002 bool dereferenced
= false;
9006 && type
->points_to() != NULL
9007 && type
->points_to()->points_to() != NULL
)
9009 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
9010 type
= type
->points_to();
9011 if (type
->deref()->is_error_type())
9012 return Expression::make_error(location
);
9013 nt
= type
->points_to()->named_type();
9014 st
= type
->points_to()->struct_type();
9015 dereferenced
= true;
9018 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
9019 || expr
->is_addressable());
9020 std::vector
<const Named_type
*> seen
;
9021 bool is_method
= false;
9022 bool found_pointer_method
= false;
9025 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
9026 &seen
, NULL
, &is_method
,
9027 &found_pointer_method
, &ambig1
, &ambig2
))
9032 go_assert(st
!= NULL
);
9033 if (type
->struct_type() == NULL
)
9035 go_assert(type
->points_to() != NULL
);
9036 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
9038 go_assert(expr
->type()->struct_type() == st
);
9040 ret
= st
->field_reference(expr
, name
, location
);
9042 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
9043 ret
= Expression::make_interface_field_reference(expr
, name
,
9049 m
= nt
->method_function(name
, NULL
);
9050 else if (st
!= NULL
)
9051 m
= st
->method_function(name
, NULL
);
9054 go_assert(m
!= NULL
);
9055 if (dereferenced
&& m
->is_value_method())
9058 "calling value method requires explicit dereference");
9059 return Expression::make_error(location
);
9061 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
9062 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
9063 ret
= m
->bind_method(expr
, location
);
9065 go_assert(ret
!= NULL
);
9070 if (!ambig1
.empty())
9071 error_at(location
, "%qs is ambiguous via %qs and %qs",
9072 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
9074 else if (found_pointer_method
)
9075 error_at(location
, "method requires a pointer receiver");
9076 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
9078 ("reference to field %qs in object which "
9079 "has no fields or methods"),
9080 Gogo::message_name(name
).c_str());
9084 if (!Gogo::is_hidden_name(name
))
9085 is_unexported
= false;
9088 std::string unpacked
= Gogo::unpack_hidden_name(name
);
9090 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
9095 error_at(location
, "reference to unexported field or method %qs",
9096 Gogo::message_name(name
).c_str());
9098 error_at(location
, "reference to undefined field or method %qs",
9099 Gogo::message_name(name
).c_str());
9101 return Expression::make_error(location
);
9105 // Look in TYPE for a field or method named NAME, return true if one
9106 // is found. This looks through embedded anonymous fields and handles
9107 // ambiguity. If a method is found, sets *IS_METHOD to true;
9108 // otherwise, if a field is found, set it to false. If
9109 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9110 // whose address can not be taken. SEEN is used to avoid infinite
9111 // recursion on invalid types.
9113 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9114 // method we couldn't use because it requires a pointer. LEVEL is
9115 // used for recursive calls, and can be NULL for a non-recursive call.
9116 // When this function returns false because it finds that the name is
9117 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9118 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9119 // will be unchanged.
9121 // This function just returns whether or not there is a field or
9122 // method, and whether it is a field or method. It doesn't build an
9123 // expression to refer to it. If it is a method, we then look in the
9124 // list of all methods for the type. If it is a field, the search has
9125 // to be done again, looking only for fields, and building up the
9126 // expression as we go.
9129 Type::find_field_or_method(const Type
* type
,
9130 const std::string
& name
,
9131 bool receiver_can_be_pointer
,
9132 std::vector
<const Named_type
*>* seen
,
9135 bool* found_pointer_method
,
9136 std::string
* ambig1
,
9137 std::string
* ambig2
)
9139 // Named types can have locally defined methods.
9140 const Named_type
* nt
= type
->named_type();
9141 if (nt
== NULL
&& type
->points_to() != NULL
)
9142 nt
= type
->points_to()->named_type();
9145 Named_object
* no
= nt
->find_local_method(name
);
9148 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
9154 // Record that we have found a pointer method in order to
9155 // give a better error message if we don't find anything
9157 *found_pointer_method
= true;
9160 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9166 // We've already seen this type when searching for methods.
9172 // Interface types can have methods.
9173 const Interface_type
* it
= type
->interface_type();
9174 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
9180 // Struct types can have fields. They can also inherit fields and
9181 // methods from anonymous fields.
9182 const Struct_type
* st
= type
->deref()->struct_type();
9185 const Struct_field_list
* fields
= st
->fields();
9190 seen
->push_back(nt
);
9192 int found_level
= 0;
9193 bool found_is_method
= false;
9194 std::string found_ambig1
;
9195 std::string found_ambig2
;
9196 const Struct_field
* found_parent
= NULL
;
9197 for (Struct_field_list::const_iterator pf
= fields
->begin();
9198 pf
!= fields
->end();
9201 if (pf
->is_field_name(name
))
9209 if (!pf
->is_anonymous())
9212 if (pf
->type()->deref()->is_error_type()
9213 || pf
->type()->deref()->is_undefined())
9216 Named_type
* fnt
= pf
->type()->named_type();
9218 fnt
= pf
->type()->deref()->named_type();
9219 go_assert(fnt
!= NULL
);
9221 int sublevel
= level
== NULL
? 1 : *level
+ 1;
9223 std::string subambig1
;
9224 std::string subambig2
;
9225 bool subfound
= Type::find_field_or_method(fnt
,
9227 receiver_can_be_pointer
,
9231 found_pointer_method
,
9236 if (!subambig1
.empty())
9238 // The name was found via this field, but is ambiguous.
9239 // if the ambiguity is lower or at the same level as
9240 // anything else we have already found, then we want to
9241 // pass the ambiguity back to the caller.
9242 if (found_level
== 0 || sublevel
<= found_level
)
9244 found_ambig1
= (Gogo::message_name(pf
->field_name())
9246 found_ambig2
= (Gogo::message_name(pf
->field_name())
9248 found_level
= sublevel
;
9254 // The name was found via this field. Use the level to see
9255 // if we want to use this one, or whether it introduces an
9257 if (found_level
== 0 || sublevel
< found_level
)
9259 found_level
= sublevel
;
9260 found_is_method
= sub_is_method
;
9261 found_ambig1
.clear();
9262 found_ambig2
.clear();
9263 found_parent
= &*pf
;
9265 else if (sublevel
> found_level
)
9267 else if (found_ambig1
.empty())
9269 // We found an ambiguity.
9270 go_assert(found_parent
!= NULL
);
9271 found_ambig1
= Gogo::message_name(found_parent
->field_name());
9272 found_ambig2
= Gogo::message_name(pf
->field_name());
9276 // We found an ambiguity, but we already know of one.
9277 // Just report the earlier one.
9282 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
9283 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
9284 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
9285 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
9290 if (found_level
== 0)
9292 else if (!found_ambig1
.empty())
9294 go_assert(!found_ambig1
.empty());
9295 ambig1
->assign(found_ambig1
);
9296 ambig2
->assign(found_ambig2
);
9298 *level
= found_level
;
9304 *level
= found_level
;
9305 *is_method
= found_is_method
;
9310 // Return whether NAME is an unexported field or method for TYPE.
9313 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
9314 const std::string
& name
,
9315 std::vector
<const Named_type
*>* seen
)
9317 const Named_type
* nt
= type
->named_type();
9319 nt
= type
->deref()->named_type();
9322 if (nt
->is_unexported_local_method(gogo
, name
))
9325 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9331 // We've already seen this type.
9337 const Interface_type
* it
= type
->interface_type();
9338 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
9341 type
= type
->deref();
9343 const Struct_type
* st
= type
->struct_type();
9344 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
9350 const Struct_field_list
* fields
= st
->fields();
9355 seen
->push_back(nt
);
9357 for (Struct_field_list::const_iterator pf
= fields
->begin();
9358 pf
!= fields
->end();
9361 if (pf
->is_anonymous()
9362 && !pf
->type()->deref()->is_error_type()
9363 && !pf
->type()->deref()->is_undefined())
9365 Named_type
* subtype
= pf
->type()->named_type();
9366 if (subtype
== NULL
)
9367 subtype
= pf
->type()->deref()->named_type();
9368 if (subtype
== NULL
)
9370 // This is an error, but it will be diagnosed elsewhere.
9373 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
9388 // Class Forward_declaration.
9390 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
9391 : Type(TYPE_FORWARD
),
9392 named_object_(named_object
->resolve()), warned_(false)
9394 go_assert(this->named_object_
->is_unknown()
9395 || this->named_object_
->is_type_declaration());
9398 // Return the named object.
9401 Forward_declaration_type::named_object()
9403 return this->named_object_
->resolve();
9407 Forward_declaration_type::named_object() const
9409 return this->named_object_
->resolve();
9412 // Return the name of the forward declared type.
9415 Forward_declaration_type::name() const
9417 return this->named_object()->name();
9420 // Warn about a use of a type which has been declared but not defined.
9423 Forward_declaration_type::warn() const
9425 Named_object
* no
= this->named_object_
->resolve();
9426 if (no
->is_unknown())
9428 // The name was not defined anywhere.
9431 error_at(this->named_object_
->location(),
9432 "use of undefined type %qs",
9433 no
->message_name().c_str());
9434 this->warned_
= true;
9437 else if (no
->is_type_declaration())
9439 // The name was seen as a type, but the type was never defined.
9440 if (no
->type_declaration_value()->using_type())
9442 error_at(this->named_object_
->location(),
9443 "use of undefined type %qs",
9444 no
->message_name().c_str());
9445 this->warned_
= true;
9450 // The name was defined, but not as a type.
9453 error_at(this->named_object_
->location(), "expected type");
9454 this->warned_
= true;
9459 // Get the base type of a declaration. This gives an error if the
9460 // type has not yet been defined.
9463 Forward_declaration_type::real_type()
9465 if (this->is_defined())
9466 return this->named_object()->type_value();
9470 return Type::make_error_type();
9475 Forward_declaration_type::real_type() const
9477 if (this->is_defined())
9478 return this->named_object()->type_value();
9482 return Type::make_error_type();
9486 // Return whether the base type is defined.
9489 Forward_declaration_type::is_defined() const
9491 return this->named_object()->is_type();
9494 // Add a method. This is used when methods are defined before the
9498 Forward_declaration_type::add_method(const std::string
& name
,
9501 Named_object
* no
= this->named_object();
9502 if (no
->is_unknown())
9503 no
->declare_as_type();
9504 return no
->type_declaration_value()->add_method(name
, function
);
9507 // Add a method declaration. This is used when methods are declared
9511 Forward_declaration_type::add_method_declaration(const std::string
& name
,
9513 Function_type
* type
,
9516 Named_object
* no
= this->named_object();
9517 if (no
->is_unknown())
9518 no
->declare_as_type();
9519 Type_declaration
* td
= no
->type_declaration_value();
9520 return td
->add_method_declaration(name
, package
, type
, location
);
9526 Forward_declaration_type::do_traverse(Traverse
* traverse
)
9528 if (this->is_defined()
9529 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
9530 return TRAVERSE_EXIT
;
9531 return TRAVERSE_CONTINUE
;
9534 // Get the backend representation for the type.
9537 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
9539 if (this->is_defined())
9540 return Type::get_named_base_btype(gogo
, this->real_type());
9543 return gogo
->backend()->error_type();
9545 // We represent an undefined type as a struct with no fields. That
9546 // should work fine for the backend, since the same case can arise
9548 std::vector
<Backend::Btyped_identifier
> fields
;
9549 Btype
* bt
= gogo
->backend()->struct_type(fields
);
9550 return gogo
->backend()->named_type(this->name(), bt
,
9551 this->named_object()->location());
9554 // Build a type descriptor for a forwarded type.
9557 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9559 Location ploc
= Linemap::predeclared_location();
9560 if (!this->is_defined())
9561 return Expression::make_error(ploc
);
9564 Type
* t
= this->real_type();
9566 return this->named_type_descriptor(gogo
, t
, name
);
9568 return Expression::make_type_descriptor(t
, ploc
);
9572 // The reflection string.
9575 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9577 this->append_reflection(this->real_type(), gogo
, ret
);
9580 // The mangled name.
9583 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
9585 if (this->is_defined())
9586 this->append_mangled_name(this->real_type(), gogo
, ret
);
9589 const Named_object
* no
= this->named_object();
9591 if (no
->package() == NULL
)
9592 name
= gogo
->pkgpath_symbol();
9594 name
= no
->package()->pkgpath_symbol();
9596 name
+= Gogo::unpack_hidden_name(no
->name());
9598 snprintf(buf
, sizeof buf
, "N%u_",
9599 static_cast<unsigned int>(name
.length()));
9605 // Export a forward declaration. This can happen when a defined type
9606 // refers to a type which is only declared (and is presumably defined
9607 // in some other file in the same package).
9610 Forward_declaration_type::do_export(Export
*) const
9612 // If there is a base type, that should be exported instead of this.
9613 go_assert(!this->is_defined());
9615 // We don't output anything.
9618 // Make a forward declaration.
9621 Type::make_forward_declaration(Named_object
* named_object
)
9623 return new Forward_declaration_type(named_object
);
9626 // Class Typed_identifier_list.
9628 // Sort the entries by name.
9630 struct Typed_identifier_list_sort
9634 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
9635 { return t1
.name() < t2
.name(); }
9639 Typed_identifier_list::sort_by_name()
9641 std::sort(this->entries_
.begin(), this->entries_
.end(),
9642 Typed_identifier_list_sort());
9648 Typed_identifier_list::traverse(Traverse
* traverse
)
9650 for (Typed_identifier_list::const_iterator p
= this->begin();
9654 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
9655 return TRAVERSE_EXIT
;
9657 return TRAVERSE_CONTINUE
;
9662 Typed_identifier_list
*
9663 Typed_identifier_list::copy() const
9665 Typed_identifier_list
* ret
= new Typed_identifier_list();
9666 for (Typed_identifier_list::const_iterator p
= this->begin();
9669 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));