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 // Convert struct fields to the backend representation. This is not
4558 // declared in types.h so that types.h doesn't have to #include
4562 get_backend_struct_fields(Gogo
* gogo
, const Struct_field_list
* fields
,
4563 bool use_placeholder
,
4564 std::vector
<Backend::Btyped_identifier
>* bfields
)
4566 bfields
->resize(fields
->size());
4568 for (Struct_field_list::const_iterator p
= fields
->begin();
4572 (*bfields
)[i
].name
= Gogo::unpack_hidden_name(p
->field_name());
4573 (*bfields
)[i
].btype
= (use_placeholder
4574 ? p
->type()->get_backend_placeholder(gogo
)
4575 : p
->type()->get_backend(gogo
));
4576 (*bfields
)[i
].location
= p
->location();
4578 go_assert(i
== fields
->size());
4581 // Get the tree for a struct type.
4584 Struct_type::do_get_backend(Gogo
* gogo
)
4586 std::vector
<Backend::Btyped_identifier
> bfields
;
4587 get_backend_struct_fields(gogo
, this->fields_
, false, &bfields
);
4588 return gogo
->backend()->struct_type(bfields
);
4591 // Finish the backend representation of the fields of a struct.
4594 Struct_type::finish_backend_fields(Gogo
* gogo
)
4596 const Struct_field_list
* fields
= this->fields_
;
4599 for (Struct_field_list::const_iterator p
= fields
->begin();
4602 p
->type()->get_backend(gogo
);
4606 // The type of a struct type descriptor.
4609 Struct_type::make_struct_type_descriptor_type()
4614 Type
* tdt
= Type::make_type_descriptor_type();
4615 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4617 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4618 Type
* string_type
= Type::lookup_string_type();
4619 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
4622 Type::make_builtin_struct_type(5,
4623 "name", pointer_string_type
,
4624 "pkgPath", pointer_string_type
,
4626 "tag", pointer_string_type
,
4627 "offset", uintptr_type
);
4628 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
4630 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
4632 Struct_type
* s
= Type::make_builtin_struct_type(2,
4634 "fields", slice_type
);
4636 ret
= Type::make_builtin_named_type("StructType", s
);
4642 // Build a type descriptor for a struct type.
4645 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4647 Location bloc
= Linemap::predeclared_location();
4649 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
4651 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4653 Expression_list
* vals
= new Expression_list();
4656 const Methods
* methods
= this->methods();
4657 // A named struct should not have methods--the methods should attach
4658 // to the named type.
4659 go_assert(methods
== NULL
|| name
== NULL
);
4661 Struct_field_list::const_iterator ps
= fields
->begin();
4662 go_assert(ps
->is_field_name("commonType"));
4663 vals
->push_back(this->type_descriptor_constructor(gogo
,
4664 RUNTIME_TYPE_KIND_STRUCT
,
4665 name
, methods
, true));
4668 go_assert(ps
->is_field_name("fields"));
4670 Expression_list
* elements
= new Expression_list();
4671 elements
->reserve(this->fields_
->size());
4672 Type
* element_type
= ps
->type()->array_type()->element_type();
4673 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
4674 pf
!= this->fields_
->end();
4677 const Struct_field_list
* f
= element_type
->struct_type()->fields();
4679 Expression_list
* fvals
= new Expression_list();
4682 Struct_field_list::const_iterator q
= f
->begin();
4683 go_assert(q
->is_field_name("name"));
4684 if (pf
->is_anonymous())
4685 fvals
->push_back(Expression::make_nil(bloc
));
4688 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
4689 Expression
* s
= Expression::make_string(n
, bloc
);
4690 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4694 go_assert(q
->is_field_name("pkgPath"));
4695 if (!Gogo::is_hidden_name(pf
->field_name()))
4696 fvals
->push_back(Expression::make_nil(bloc
));
4699 std::string n
= Gogo::hidden_name_pkgpath(pf
->field_name());
4700 Expression
* s
= Expression::make_string(n
, bloc
);
4701 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4705 go_assert(q
->is_field_name("typ"));
4706 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
4709 go_assert(q
->is_field_name("tag"));
4711 fvals
->push_back(Expression::make_nil(bloc
));
4714 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
4715 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4719 go_assert(q
->is_field_name("offset"));
4720 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
4722 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
4724 elements
->push_back(v
);
4727 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
4730 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4733 // Write the hash function for a struct which can not use the identity
4737 Struct_type::write_hash_function(Gogo
* gogo
, Named_type
*,
4738 Function_type
* hash_fntype
,
4739 Function_type
* equal_fntype
)
4741 Location bloc
= Linemap::predeclared_location();
4743 // The pointer to the struct that we are going to hash. This is an
4744 // argument to the hash function we are implementing here.
4745 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
4746 go_assert(key_arg
!= NULL
);
4747 Type
* key_arg_type
= key_arg
->var_value()->type();
4749 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4753 mpz_init_set_ui(ival
, 0);
4754 Expression
* zero
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
4757 // Make a temporary to hold the return value, initialized to 0.
4758 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
4760 gogo
->add_statement(retval
);
4762 // Make a temporary to hold the key as a uintptr.
4763 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
4764 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
4765 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
4767 gogo
->add_statement(key
);
4769 // Loop over the struct fields.
4771 const Struct_field_list
* fields
= this->fields_
;
4772 for (Struct_field_list::const_iterator pf
= fields
->begin();
4773 pf
!= fields
->end();
4776 if (Gogo::is_sink_name(pf
->field_name()))
4783 // Multiply retval by 33.
4784 mpz_init_set_ui(ival
, 33);
4785 Expression
* i33
= Expression::make_integer(&ival
, uintptr_type
,
4789 ref
= Expression::make_temporary_reference(retval
, bloc
);
4790 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
,
4792 gogo
->add_statement(s
);
4795 // Get a pointer to the value of this field.
4796 Expression
* offset
= Expression::make_struct_field_offset(this, &*pf
);
4797 ref
= Expression::make_temporary_reference(key
, bloc
);
4798 Expression
* subkey
= Expression::make_binary(OPERATOR_PLUS
, ref
, offset
,
4800 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
4802 // Get the size of this field.
4803 Expression
* size
= Expression::make_type_info(pf
->type(),
4804 Expression::TYPE_INFO_SIZE
);
4806 // Get the hash function to use for the type of this field.
4807 Named_object
* hash_fn
;
4808 Named_object
* equal_fn
;
4809 pf
->type()->type_functions(gogo
, pf
->type()->named_type(), hash_fntype
,
4810 equal_fntype
, &hash_fn
, &equal_fn
);
4812 // Call the hash function for the field.
4813 Expression_list
* args
= new Expression_list();
4814 args
->push_back(subkey
);
4815 args
->push_back(size
);
4816 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
4817 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
4819 // Add the field's hash value to retval.
4820 Temporary_reference_expression
* tref
=
4821 Expression::make_temporary_reference(retval
, bloc
);
4822 tref
->set_is_lvalue();
4823 Statement
* s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
,
4825 gogo
->add_statement(s
);
4828 // Return retval to the caller of the hash function.
4829 Expression_list
* vals
= new Expression_list();
4830 ref
= Expression::make_temporary_reference(retval
, bloc
);
4831 vals
->push_back(ref
);
4832 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
4833 gogo
->add_statement(s
);
4836 // Write the equality function for a struct which can not use the
4837 // identity function.
4840 Struct_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
4842 Location bloc
= Linemap::predeclared_location();
4844 // The pointers to the structs we are going to compare.
4845 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
4846 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
4847 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
4849 // Build temporaries with the right types.
4850 Type
* pt
= Type::make_pointer_type(name
!= NULL
4851 ? static_cast<Type
*>(name
)
4852 : static_cast<Type
*>(this));
4854 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
4855 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
4856 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
4857 gogo
->add_statement(p1
);
4859 ref
= Expression::make_var_reference(key2_arg
, bloc
);
4860 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
4861 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
4862 gogo
->add_statement(p2
);
4864 const Struct_field_list
* fields
= this->fields_
;
4865 unsigned int field_index
= 0;
4866 for (Struct_field_list::const_iterator pf
= fields
->begin();
4867 pf
!= fields
->end();
4868 ++pf
, ++field_index
)
4870 if (Gogo::is_sink_name(pf
->field_name()))
4873 // Compare one field in both P1 and P2.
4874 Expression
* f1
= Expression::make_temporary_reference(p1
, bloc
);
4875 f1
= Expression::make_unary(OPERATOR_MULT
, f1
, bloc
);
4876 f1
= Expression::make_field_reference(f1
, field_index
, bloc
);
4878 Expression
* f2
= Expression::make_temporary_reference(p2
, bloc
);
4879 f2
= Expression::make_unary(OPERATOR_MULT
, f2
, bloc
);
4880 f2
= Expression::make_field_reference(f2
, field_index
, bloc
);
4882 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, f1
, f2
, bloc
);
4884 // If the values are not equal, return false.
4885 gogo
->start_block(bloc
);
4886 Expression_list
* vals
= new Expression_list();
4887 vals
->push_back(Expression::make_boolean(false, bloc
));
4888 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
4889 gogo
->add_statement(s
);
4890 Block
* then_block
= gogo
->finish_block(bloc
);
4892 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
4893 gogo
->add_statement(s
);
4896 // All the fields are equal, so return true.
4897 Expression_list
* vals
= new Expression_list();
4898 vals
->push_back(Expression::make_boolean(true, bloc
));
4899 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
4900 gogo
->add_statement(s
);
4903 // Reflection string.
4906 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4908 ret
->append("struct { ");
4910 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
4911 p
!= this->fields_
->end();
4914 if (p
!= this->fields_
->begin())
4916 if (p
->is_anonymous())
4917 ret
->push_back('?');
4919 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
4920 ret
->push_back(' ');
4921 this->append_reflection(p
->type(), gogo
, ret
);
4925 const std::string
& tag(p
->tag());
4927 for (std::string::const_iterator p
= tag
.begin();
4932 ret
->append("\\x00");
4933 else if (*p
== '\n')
4935 else if (*p
== '\t')
4938 ret
->append("\\\"");
4939 else if (*p
== '\\')
4940 ret
->append("\\\\");
4944 ret
->push_back('"');
4954 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4956 ret
->push_back('S');
4958 const Struct_field_list
* fields
= this->fields_
;
4961 for (Struct_field_list::const_iterator p
= fields
->begin();
4965 if (p
->is_anonymous())
4969 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
4971 snprintf(buf
, sizeof buf
, "%u_",
4972 static_cast<unsigned int>(n
.length()));
4976 this->append_mangled_name(p
->type(), gogo
, ret
);
4979 const std::string
& tag(p
->tag());
4981 for (std::string::const_iterator p
= tag
.begin();
4985 if (ISALNUM(*p
) || *p
== '_')
4990 snprintf(buf
, sizeof buf
, ".%x.",
4991 static_cast<unsigned int>(*p
));
4996 snprintf(buf
, sizeof buf
, "T%u_",
4997 static_cast<unsigned int>(out
.length()));
5004 ret
->push_back('e');
5007 // If the offset of field INDEX in the backend implementation can be
5008 // determined, set *POFFSET to the offset in bytes and return true.
5009 // Otherwise, return false.
5012 Struct_type::backend_field_offset(Gogo
* gogo
, unsigned int index
,
5013 unsigned int* poffset
)
5015 if (!this->is_backend_type_size_known(gogo
))
5017 Btype
* bt
= this->get_backend_placeholder(gogo
);
5018 size_t offset
= gogo
->backend()->type_field_offset(bt
, index
);
5019 *poffset
= static_cast<unsigned int>(offset
);
5020 if (*poffset
!= offset
)
5028 Struct_type::do_export(Export
* exp
) const
5030 exp
->write_c_string("struct { ");
5031 const Struct_field_list
* fields
= this->fields_
;
5032 go_assert(fields
!= NULL
);
5033 for (Struct_field_list::const_iterator p
= fields
->begin();
5037 if (p
->is_anonymous())
5038 exp
->write_string("? ");
5041 exp
->write_string(p
->field_name());
5042 exp
->write_c_string(" ");
5044 exp
->write_type(p
->type());
5048 exp
->write_c_string(" ");
5050 Expression::make_string(p
->tag(), Linemap::predeclared_location());
5051 expr
->export_expression(exp
);
5055 exp
->write_c_string("; ");
5057 exp
->write_c_string("}");
5063 Struct_type::do_import(Import
* imp
)
5065 imp
->require_c_string("struct { ");
5066 Struct_field_list
* fields
= new Struct_field_list
;
5067 if (imp
->peek_char() != '}')
5072 if (imp
->match_c_string("? "))
5076 name
= imp
->read_identifier();
5077 imp
->require_c_string(" ");
5079 Type
* ftype
= imp
->read_type();
5081 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
5083 if (imp
->peek_char() == ' ')
5086 Expression
* expr
= Expression::import_expression(imp
);
5087 String_expression
* sexpr
= expr
->string_expression();
5088 go_assert(sexpr
!= NULL
);
5089 sf
.set_tag(sexpr
->val());
5093 imp
->require_c_string("; ");
5094 fields
->push_back(sf
);
5095 if (imp
->peek_char() == '}')
5099 imp
->require_c_string("}");
5101 return Type::make_struct_type(fields
, imp
->location());
5104 // Make a struct type.
5107 Type::make_struct_type(Struct_field_list
* fields
,
5110 return new Struct_type(fields
, location
);
5113 // Class Array_type.
5115 // Whether two array types are identical.
5118 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
5120 if (!Type::are_identical(this->element_type(), t
->element_type(),
5121 errors_are_identical
, NULL
))
5124 Expression
* l1
= this->length();
5125 Expression
* l2
= t
->length();
5127 // Slices of the same element type are identical.
5128 if (l1
== NULL
&& l2
== NULL
)
5131 // Arrays of the same element type are identical if they have the
5133 if (l1
!= NULL
&& l2
!= NULL
)
5138 // Try to determine the lengths. If we can't, assume the arrays
5139 // are not identical.
5141 Numeric_constant nc1
, nc2
;
5142 if (l1
->numeric_constant_value(&nc1
)
5143 && l2
->numeric_constant_value(&nc2
))
5146 if (nc1
.to_int(&v1
))
5149 if (nc2
.to_int(&v2
))
5151 ret
= mpz_cmp(v1
, v2
) == 0;
5160 // Otherwise the arrays are not identical.
5167 Array_type::do_traverse(Traverse
* traverse
)
5169 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
5170 return TRAVERSE_EXIT
;
5171 if (this->length_
!= NULL
5172 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
5173 return TRAVERSE_EXIT
;
5174 return TRAVERSE_CONTINUE
;
5177 // Check that the length is valid.
5180 Array_type::verify_length()
5182 if (this->length_
== NULL
)
5185 Type_context
context(Type::lookup_integer_type("int"), false);
5186 this->length_
->determine_type(&context
);
5188 if (!this->length_
->is_constant())
5190 error_at(this->length_
->location(), "array bound is not constant");
5194 Numeric_constant nc
;
5195 if (!this->length_
->numeric_constant_value(&nc
))
5197 if (this->length_
->type()->integer_type() != NULL
5198 || this->length_
->type()->float_type() != NULL
)
5199 error_at(this->length_
->location(), "array bound is not constant");
5201 error_at(this->length_
->location(), "array bound is not numeric");
5206 switch (nc
.to_unsigned_long(&val
))
5208 case Numeric_constant::NC_UL_VALID
:
5210 case Numeric_constant::NC_UL_NOTINT
:
5211 error_at(this->length_
->location(), "array bound truncated to integer");
5213 case Numeric_constant::NC_UL_NEGATIVE
:
5214 error_at(this->length_
->location(), "negative array bound");
5216 case Numeric_constant::NC_UL_BIG
:
5217 error_at(this->length_
->location(), "array bound overflows");
5223 Type
* int_type
= Type::lookup_integer_type("int");
5224 unsigned int tbits
= int_type
->integer_type()->bits();
5225 if (sizeof(val
) <= tbits
* 8
5226 && val
>> (tbits
- 1) != 0)
5228 error_at(this->length_
->location(), "array bound overflows");
5238 Array_type::do_verify()
5240 if (!this->verify_length())
5241 this->length_
= Expression::make_error(this->length_
->location());
5245 // Whether we can use memcmp to compare this array.
5248 Array_type::do_compare_is_identity(Gogo
* gogo
) const
5250 if (this->length_
== NULL
)
5253 // Check for [...], which indicates that this is not a real type.
5254 if (this->length_
->is_nil_expression())
5257 if (!this->element_type_
->compare_is_identity(gogo
))
5260 // If there is any padding, then we can't use memcmp.
5263 if (!this->element_type_
->backend_type_size(gogo
, &size
)
5264 || !this->element_type_
->backend_type_align(gogo
, &align
))
5266 if ((size
& (align
- 1)) != 0)
5272 // Array type hash code.
5275 Array_type::do_hash_for_method(Gogo
* gogo
) const
5277 // There is no very convenient way to get a hash code for the
5279 return this->element_type_
->hash_for_method(gogo
) + 1;
5282 // Write the hash function for an array which can not use the identify
5286 Array_type::write_hash_function(Gogo
* gogo
, Named_type
* name
,
5287 Function_type
* hash_fntype
,
5288 Function_type
* equal_fntype
)
5290 Location bloc
= Linemap::predeclared_location();
5292 // The pointer to the array that we are going to hash. This is an
5293 // argument to the hash function we are implementing here.
5294 Named_object
* key_arg
= gogo
->lookup("key", NULL
);
5295 go_assert(key_arg
!= NULL
);
5296 Type
* key_arg_type
= key_arg
->var_value()->type();
5298 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5302 mpz_init_set_ui(ival
, 0);
5303 Expression
* zero
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
5306 // Make a temporary to hold the return value, initialized to 0.
5307 Temporary_statement
* retval
= Statement::make_temporary(uintptr_type
, zero
,
5309 gogo
->add_statement(retval
);
5311 // Make a temporary to hold the key as a uintptr.
5312 Expression
* ref
= Expression::make_var_reference(key_arg
, bloc
);
5313 ref
= Expression::make_cast(uintptr_type
, ref
, bloc
);
5314 Temporary_statement
* key
= Statement::make_temporary(uintptr_type
, ref
,
5316 gogo
->add_statement(key
);
5318 // Loop over the array elements.
5320 Type
* int_type
= Type::lookup_integer_type("int");
5321 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
5322 gogo
->add_statement(index
);
5324 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
5325 Expression
* aref
= Expression::make_var_reference(key_arg
, bloc
);
5326 Type
* pt
= Type::make_pointer_type(name
!= NULL
5327 ? static_cast<Type
*>(name
)
5328 : static_cast<Type
*>(this));
5329 aref
= Expression::make_cast(pt
, aref
, bloc
);
5330 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
5335 gogo
->start_block(bloc
);
5337 // Multiply retval by 33.
5338 mpz_init_set_ui(ival
, 33);
5339 Expression
* i33
= Expression::make_integer(&ival
, uintptr_type
, bloc
);
5342 ref
= Expression::make_temporary_reference(retval
, bloc
);
5343 Statement
* s
= Statement::make_assignment_operation(OPERATOR_MULTEQ
, ref
,
5345 gogo
->add_statement(s
);
5347 // Get the hash function for the element type.
5348 Named_object
* hash_fn
;
5349 Named_object
* equal_fn
;
5350 this->element_type_
->type_functions(gogo
, this->element_type_
->named_type(),
5351 hash_fntype
, equal_fntype
, &hash_fn
,
5354 // Get a pointer to this element in the loop.
5355 Expression
* subkey
= Expression::make_temporary_reference(key
, bloc
);
5356 subkey
= Expression::make_cast(key_arg_type
, subkey
, bloc
);
5358 // Get the size of each element.
5359 Expression
* ele_size
= Expression::make_type_info(this->element_type_
,
5360 Expression::TYPE_INFO_SIZE
);
5362 // Get the hash of this element.
5363 Expression_list
* args
= new Expression_list();
5364 args
->push_back(subkey
);
5365 args
->push_back(ele_size
);
5366 Expression
* func
= Expression::make_func_reference(hash_fn
, NULL
, bloc
);
5367 Expression
* call
= Expression::make_call(func
, args
, false, bloc
);
5369 // Add the element's hash value to retval.
5370 Temporary_reference_expression
* tref
=
5371 Expression::make_temporary_reference(retval
, bloc
);
5372 tref
->set_is_lvalue();
5373 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, call
, bloc
);
5374 gogo
->add_statement(s
);
5376 // Increase the element pointer.
5377 tref
= Expression::make_temporary_reference(key
, bloc
);
5378 tref
->set_is_lvalue();
5379 s
= Statement::make_assignment_operation(OPERATOR_PLUSEQ
, tref
, ele_size
,
5382 Block
* statements
= gogo
->finish_block(bloc
);
5384 for_range
->add_statements(statements
);
5385 gogo
->add_statement(for_range
);
5387 // Return retval to the caller of the hash function.
5388 Expression_list
* vals
= new Expression_list();
5389 ref
= Expression::make_temporary_reference(retval
, bloc
);
5390 vals
->push_back(ref
);
5391 s
= Statement::make_return_statement(vals
, bloc
);
5392 gogo
->add_statement(s
);
5395 // Write the equality function for an array which can not use the
5396 // identity function.
5399 Array_type::write_equal_function(Gogo
* gogo
, Named_type
* name
)
5401 Location bloc
= Linemap::predeclared_location();
5403 // The pointers to the arrays we are going to compare.
5404 Named_object
* key1_arg
= gogo
->lookup("key1", NULL
);
5405 Named_object
* key2_arg
= gogo
->lookup("key2", NULL
);
5406 go_assert(key1_arg
!= NULL
&& key2_arg
!= NULL
);
5408 // Build temporaries for the keys with the right types.
5409 Type
* pt
= Type::make_pointer_type(name
!= NULL
5410 ? static_cast<Type
*>(name
)
5411 : static_cast<Type
*>(this));
5413 Expression
* ref
= Expression::make_var_reference(key1_arg
, bloc
);
5414 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5415 Temporary_statement
* p1
= Statement::make_temporary(pt
, ref
, bloc
);
5416 gogo
->add_statement(p1
);
5418 ref
= Expression::make_var_reference(key2_arg
, bloc
);
5419 ref
= Expression::make_unsafe_cast(pt
, ref
, bloc
);
5420 Temporary_statement
* p2
= Statement::make_temporary(pt
, ref
, bloc
);
5421 gogo
->add_statement(p2
);
5423 // Loop over the array elements.
5425 Type
* int_type
= Type::lookup_integer_type("int");
5426 Temporary_statement
* index
= Statement::make_temporary(int_type
, NULL
, bloc
);
5427 gogo
->add_statement(index
);
5429 Expression
* iref
= Expression::make_temporary_reference(index
, bloc
);
5430 Expression
* aref
= Expression::make_temporary_reference(p1
, bloc
);
5431 For_range_statement
* for_range
= Statement::make_for_range_statement(iref
,
5436 gogo
->start_block(bloc
);
5438 // Compare element in P1 and P2.
5439 Expression
* e1
= Expression::make_temporary_reference(p1
, bloc
);
5440 e1
= Expression::make_unary(OPERATOR_MULT
, e1
, bloc
);
5441 ref
= Expression::make_temporary_reference(index
, bloc
);
5442 e1
= Expression::make_array_index(e1
, ref
, NULL
, bloc
);
5444 Expression
* e2
= Expression::make_temporary_reference(p2
, bloc
);
5445 e2
= Expression::make_unary(OPERATOR_MULT
, e2
, bloc
);
5446 ref
= Expression::make_temporary_reference(index
, bloc
);
5447 e2
= Expression::make_array_index(e2
, ref
, NULL
, bloc
);
5449 Expression
* cond
= Expression::make_binary(OPERATOR_NOTEQ
, e1
, e2
, bloc
);
5451 // If the elements are not equal, return false.
5452 gogo
->start_block(bloc
);
5453 Expression_list
* vals
= new Expression_list();
5454 vals
->push_back(Expression::make_boolean(false, bloc
));
5455 Statement
* s
= Statement::make_return_statement(vals
, bloc
);
5456 gogo
->add_statement(s
);
5457 Block
* then_block
= gogo
->finish_block(bloc
);
5459 s
= Statement::make_if_statement(cond
, then_block
, NULL
, bloc
);
5460 gogo
->add_statement(s
);
5462 Block
* statements
= gogo
->finish_block(bloc
);
5464 for_range
->add_statements(statements
);
5465 gogo
->add_statement(for_range
);
5467 // All the elements are equal, so return true.
5468 vals
= new Expression_list();
5469 vals
->push_back(Expression::make_boolean(true, bloc
));
5470 s
= Statement::make_return_statement(vals
, bloc
);
5471 gogo
->add_statement(s
);
5474 // Get a tree for the length of a fixed array. The length may be
5475 // computed using a function call, so we must only evaluate it once.
5478 Array_type::get_length_tree(Gogo
* gogo
)
5480 go_assert(this->length_
!= NULL
);
5481 if (this->length_tree_
== NULL_TREE
)
5483 Numeric_constant nc
;
5485 if (this->length_
->numeric_constant_value(&nc
) && nc
.to_int(&val
))
5487 if (mpz_sgn(val
) < 0)
5489 this->length_tree_
= error_mark_node
;
5490 return this->length_tree_
;
5492 Type
* t
= nc
.type();
5494 t
= Type::lookup_integer_type("int");
5495 else if (t
->is_abstract())
5496 t
= t
->make_non_abstract_type();
5497 tree tt
= type_to_tree(t
->get_backend(gogo
));
5498 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
5503 // Make up a translation context for the array length
5504 // expression. FIXME: This won't work in general.
5505 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
5506 tree len
= this->length_
->get_tree(&context
);
5507 if (len
!= error_mark_node
)
5509 len
= convert_to_integer(integer_type_node
, len
);
5510 len
= save_expr(len
);
5512 this->length_tree_
= len
;
5515 return this->length_tree_
;
5518 // Get the backend representation of the fields of a slice. This is
5519 // not declared in types.h so that types.h doesn't have to #include
5522 // We use int for the count and capacity fields. This matches 6g.
5523 // The language more or less assumes that we can't allocate space of a
5524 // size which does not fit in int.
5527 get_backend_slice_fields(Gogo
* gogo
, Array_type
* type
, bool use_placeholder
,
5528 std::vector
<Backend::Btyped_identifier
>* bfields
)
5532 Type
* pet
= Type::make_pointer_type(type
->element_type());
5533 Btype
* pbet
= (use_placeholder
5534 ? pet
->get_backend_placeholder(gogo
)
5535 : pet
->get_backend(gogo
));
5536 Location ploc
= Linemap::predeclared_location();
5538 Backend::Btyped_identifier
* p
= &(*bfields
)[0];
5539 p
->name
= "__values";
5543 Type
* int_type
= Type::lookup_integer_type("int");
5546 p
->name
= "__count";
5547 p
->btype
= int_type
->get_backend(gogo
);
5551 p
->name
= "__capacity";
5552 p
->btype
= int_type
->get_backend(gogo
);
5556 // Get a tree for the type of this array. A fixed array is simply
5557 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
5558 // just like an array in C. An open array is a struct with three
5559 // fields: a data pointer, the length, and the capacity.
5562 Array_type::do_get_backend(Gogo
* gogo
)
5564 if (this->length_
== NULL
)
5566 std::vector
<Backend::Btyped_identifier
> bfields
;
5567 get_backend_slice_fields(gogo
, this, false, &bfields
);
5568 return gogo
->backend()->struct_type(bfields
);
5572 Btype
* element
= this->get_backend_element(gogo
, false);
5573 Bexpression
* len
= this->get_backend_length(gogo
);
5574 return gogo
->backend()->array_type(element
, len
);
5578 // Return the backend representation of the element type.
5581 Array_type::get_backend_element(Gogo
* gogo
, bool use_placeholder
)
5583 if (use_placeholder
)
5584 return this->element_type_
->get_backend_placeholder(gogo
);
5586 return this->element_type_
->get_backend(gogo
);
5589 // Return the backend representation of the length.
5592 Array_type::get_backend_length(Gogo
* gogo
)
5594 return tree_to_expr(this->get_length_tree(gogo
));
5597 // Finish backend representation of the array.
5600 Array_type::finish_backend_element(Gogo
* gogo
)
5602 Type
* et
= this->array_type()->element_type();
5603 et
->get_backend(gogo
);
5604 if (this->is_slice_type())
5606 // This relies on the fact that we always use the same
5607 // structure for a pointer to any given type.
5608 Type
* pet
= Type::make_pointer_type(et
);
5609 pet
->get_backend(gogo
);
5613 // Return a tree for a pointer to the values in ARRAY.
5616 Array_type::value_pointer_tree(Gogo
*, tree array
) const
5619 if (this->length() != NULL
)
5622 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
5623 build_fold_addr_expr(array
));
5628 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
5629 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
5631 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
5634 if (TREE_CONSTANT(array
))
5635 TREE_CONSTANT(ret
) = 1;
5639 // Return a tree for the length of the array ARRAY which has this
5643 Array_type::length_tree(Gogo
* gogo
, tree array
)
5645 if (this->length_
!= NULL
)
5647 if (TREE_CODE(array
) == SAVE_EXPR
)
5648 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
5650 return omit_one_operand(integer_type_node
,
5651 this->get_length_tree(gogo
), array
);
5654 // This is an open array. We need to read the length field.
5656 tree type
= TREE_TYPE(array
);
5657 go_assert(TREE_CODE(type
) == RECORD_TYPE
);
5659 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
5660 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
5662 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
5663 if (TREE_CONSTANT(array
))
5664 TREE_CONSTANT(ret
) = 1;
5668 // Return a tree for the capacity of the array ARRAY which has this
5672 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
5674 if (this->length_
!= NULL
)
5675 return omit_one_operand(integer_type_node
, this->get_length_tree(gogo
),
5678 // This is an open array. We need to read the capacity field.
5680 tree type
= TREE_TYPE(array
);
5681 go_assert(TREE_CODE(type
) == RECORD_TYPE
);
5683 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
5684 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
5686 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
5692 Array_type::do_export(Export
* exp
) const
5694 exp
->write_c_string("[");
5695 if (this->length_
!= NULL
)
5696 this->length_
->export_expression(exp
);
5697 exp
->write_c_string("] ");
5698 exp
->write_type(this->element_type_
);
5704 Array_type::do_import(Import
* imp
)
5706 imp
->require_c_string("[");
5708 if (imp
->peek_char() == ']')
5711 length
= Expression::import_expression(imp
);
5712 imp
->require_c_string("] ");
5713 Type
* element_type
= imp
->read_type();
5714 return Type::make_array_type(element_type
, length
);
5717 // The type of an array type descriptor.
5720 Array_type::make_array_type_descriptor_type()
5725 Type
* tdt
= Type::make_type_descriptor_type();
5726 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5728 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5731 Type::make_builtin_struct_type(4,
5735 "len", uintptr_type
);
5737 ret
= Type::make_builtin_named_type("ArrayType", sf
);
5743 // The type of an slice type descriptor.
5746 Array_type::make_slice_type_descriptor_type()
5751 Type
* tdt
= Type::make_type_descriptor_type();
5752 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5755 Type::make_builtin_struct_type(2,
5759 ret
= Type::make_builtin_named_type("SliceType", sf
);
5765 // Build a type descriptor for an array/slice type.
5768 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5770 if (this->length_
!= NULL
)
5771 return this->array_type_descriptor(gogo
, name
);
5773 return this->slice_type_descriptor(gogo
, name
);
5776 // Build a type descriptor for an array type.
5779 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5781 Location bloc
= Linemap::predeclared_location();
5783 Type
* atdt
= Array_type::make_array_type_descriptor_type();
5785 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
5787 Expression_list
* vals
= new Expression_list();
5790 Struct_field_list::const_iterator p
= fields
->begin();
5791 go_assert(p
->is_field_name("commonType"));
5792 vals
->push_back(this->type_descriptor_constructor(gogo
,
5793 RUNTIME_TYPE_KIND_ARRAY
,
5797 go_assert(p
->is_field_name("elem"));
5798 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5801 go_assert(p
->is_field_name("slice"));
5802 Type
* slice_type
= Type::make_array_type(this->element_type_
, NULL
);
5803 vals
->push_back(Expression::make_type_descriptor(slice_type
, bloc
));
5806 go_assert(p
->is_field_name("len"));
5807 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
5810 go_assert(p
== fields
->end());
5812 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
5815 // Build a type descriptor for a slice type.
5818 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5820 Location bloc
= Linemap::predeclared_location();
5822 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
5824 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
5826 Expression_list
* vals
= new Expression_list();
5829 Struct_field_list::const_iterator p
= fields
->begin();
5830 go_assert(p
->is_field_name("commonType"));
5831 vals
->push_back(this->type_descriptor_constructor(gogo
,
5832 RUNTIME_TYPE_KIND_SLICE
,
5836 go_assert(p
->is_field_name("elem"));
5837 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5840 go_assert(p
== fields
->end());
5842 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
5845 // Reflection string.
5848 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5850 ret
->push_back('[');
5851 if (this->length_
!= NULL
)
5853 Numeric_constant nc
;
5855 if (!this->length_
->numeric_constant_value(&nc
)
5856 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
5857 error_at(this->length_
->location(), "invalid array length");
5861 snprintf(buf
, sizeof buf
, "%lu", val
);
5865 ret
->push_back(']');
5867 this->append_reflection(this->element_type_
, gogo
, ret
);
5873 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5875 ret
->push_back('A');
5876 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5877 if (this->length_
!= NULL
)
5879 Numeric_constant nc
;
5881 if (!this->length_
->numeric_constant_value(&nc
)
5882 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
5883 error_at(this->length_
->location(), "invalid array length");
5887 snprintf(buf
, sizeof buf
, "%lu", val
);
5891 ret
->push_back('e');
5894 // Make an array type.
5897 Type::make_array_type(Type
* element_type
, Expression
* length
)
5899 return new Array_type(element_type
, length
);
5907 Map_type::do_traverse(Traverse
* traverse
)
5909 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
5910 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
5911 return TRAVERSE_EXIT
;
5912 return TRAVERSE_CONTINUE
;
5915 // Check that the map type is OK.
5918 Map_type::do_verify()
5920 // The runtime support uses "map[void]void".
5921 if (!this->key_type_
->is_comparable() && !this->key_type_
->is_void_type())
5922 error_at(this->location_
, "invalid map key type");
5926 // Whether two map types are identical.
5929 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
5931 return (Type::are_identical(this->key_type(), t
->key_type(),
5932 errors_are_identical
, NULL
)
5933 && Type::are_identical(this->val_type(), t
->val_type(),
5934 errors_are_identical
, NULL
));
5940 Map_type::do_hash_for_method(Gogo
* gogo
) const
5942 return (this->key_type_
->hash_for_method(gogo
)
5943 + this->val_type_
->hash_for_method(gogo
)
5947 // Get the backend representation for a map type. A map type is
5948 // represented as a pointer to a struct. The struct is __go_map in
5952 Map_type::do_get_backend(Gogo
* gogo
)
5954 static Btype
* backend_map_type
;
5955 if (backend_map_type
== NULL
)
5957 std::vector
<Backend::Btyped_identifier
> bfields(4);
5959 Location bloc
= Linemap::predeclared_location();
5961 Type
* pdt
= Type::make_type_descriptor_ptr_type();
5962 bfields
[0].name
= "__descriptor";
5963 bfields
[0].btype
= pdt
->get_backend(gogo
);
5964 bfields
[0].location
= bloc
;
5966 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5967 bfields
[1].name
= "__element_count";
5968 bfields
[1].btype
= uintptr_type
->get_backend(gogo
);
5969 bfields
[1].location
= bloc
;
5971 bfields
[2].name
= "__bucket_count";
5972 bfields
[2].btype
= bfields
[1].btype
;
5973 bfields
[2].location
= bloc
;
5975 Btype
* bvt
= gogo
->backend()->void_type();
5976 Btype
* bpvt
= gogo
->backend()->pointer_type(bvt
);
5977 Btype
* bppvt
= gogo
->backend()->pointer_type(bpvt
);
5978 bfields
[3].name
= "__buckets";
5979 bfields
[3].btype
= bppvt
;
5980 bfields
[3].location
= bloc
;
5982 Btype
*bt
= gogo
->backend()->struct_type(bfields
);
5983 bt
= gogo
->backend()->named_type("__go_map", bt
, bloc
);
5984 backend_map_type
= gogo
->backend()->pointer_type(bt
);
5986 return backend_map_type
;
5989 // The type of a map type descriptor.
5992 Map_type::make_map_type_descriptor_type()
5997 Type
* tdt
= Type::make_type_descriptor_type();
5998 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6001 Type::make_builtin_struct_type(3,
6006 ret
= Type::make_builtin_named_type("MapType", sf
);
6012 // Build a type descriptor for a map type.
6015 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6017 Location bloc
= Linemap::predeclared_location();
6019 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
6021 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
6023 Expression_list
* vals
= new Expression_list();
6026 Struct_field_list::const_iterator p
= fields
->begin();
6027 go_assert(p
->is_field_name("commonType"));
6028 vals
->push_back(this->type_descriptor_constructor(gogo
,
6029 RUNTIME_TYPE_KIND_MAP
,
6033 go_assert(p
->is_field_name("key"));
6034 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
6037 go_assert(p
->is_field_name("elem"));
6038 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
6041 go_assert(p
== fields
->end());
6043 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
6046 // A mapping from map types to map descriptors.
6048 Map_type::Map_descriptors
Map_type::map_descriptors
;
6050 // Build a map descriptor for this type. Return a pointer to it.
6053 Map_type::map_descriptor_pointer(Gogo
* gogo
, Location location
)
6055 Bvariable
* bvar
= this->map_descriptor(gogo
);
6056 tree var_tree
= var_to_tree(bvar
);
6057 if (var_tree
== error_mark_node
)
6058 return error_mark_node
;
6059 return build_fold_addr_expr_loc(location
.gcc_location(), var_tree
);
6062 // Build a map descriptor for this type.
6065 Map_type::map_descriptor(Gogo
* gogo
)
6067 std::pair
<Map_type
*, Bvariable
*> val(this, NULL
);
6068 std::pair
<Map_type::Map_descriptors::iterator
, bool> ins
=
6069 Map_type::map_descriptors
.insert(val
);
6071 return ins
.first
->second
;
6073 Type
* key_type
= this->key_type_
;
6074 Type
* val_type
= this->val_type_
;
6076 // The map entry type is a struct with three fields. Build that
6077 // struct so that we can get the offsets of the key and value within
6078 // a map entry. The first field should technically be a pointer to
6079 // this type itself, but since we only care about field offsets we
6080 // just use pointer to bool.
6081 Type
* pbool
= Type::make_pointer_type(Type::make_boolean_type());
6082 Struct_type
* map_entry_type
=
6083 Type::make_builtin_struct_type(3,
6088 Type
* map_descriptor_type
= Map_type::make_map_descriptor_type();
6090 const Struct_field_list
* fields
=
6091 map_descriptor_type
->struct_type()->fields();
6093 Expression_list
* vals
= new Expression_list();
6096 Location bloc
= Linemap::predeclared_location();
6098 Struct_field_list::const_iterator p
= fields
->begin();
6100 go_assert(p
->is_field_name("__map_descriptor"));
6101 vals
->push_back(Expression::make_type_descriptor(this, bloc
));
6104 go_assert(p
->is_field_name("__entry_size"));
6105 Expression::Type_info type_info
= Expression::TYPE_INFO_SIZE
;
6106 vals
->push_back(Expression::make_type_info(map_entry_type
, type_info
));
6108 Struct_field_list::const_iterator pf
= map_entry_type
->fields()->begin();
6110 go_assert(pf
->is_field_name("__key"));
6113 go_assert(p
->is_field_name("__key_offset"));
6114 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6117 go_assert(pf
->is_field_name("__val"));
6120 go_assert(p
->is_field_name("__val_offset"));
6121 vals
->push_back(Expression::make_struct_field_offset(map_entry_type
, &*pf
));
6124 go_assert(p
== fields
->end());
6126 Expression
* initializer
=
6127 Expression::make_struct_composite_literal(map_descriptor_type
, vals
, bloc
);
6129 std::string mangled_name
= "__go_map_" + this->mangled_name(gogo
);
6130 Btype
* map_descriptor_btype
= map_descriptor_type
->get_backend(gogo
);
6131 Bvariable
* bvar
= gogo
->backend()->immutable_struct(mangled_name
, true,
6132 map_descriptor_btype
,
6135 Translate_context
context(gogo
, NULL
, NULL
, NULL
);
6136 context
.set_is_const();
6137 Bexpression
* binitializer
= tree_to_expr(initializer
->get_tree(&context
));
6139 gogo
->backend()->immutable_struct_set_init(bvar
, mangled_name
, true,
6140 map_descriptor_btype
, bloc
,
6143 ins
.first
->second
= bvar
;
6147 // Build the type of a map descriptor. This must match the struct
6148 // __go_map_descriptor in libgo/runtime/map.h.
6151 Map_type::make_map_descriptor_type()
6156 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6157 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6159 Type::make_builtin_struct_type(4,
6160 "__map_descriptor", ptdt
,
6161 "__entry_size", uintptr_type
,
6162 "__key_offset", uintptr_type
,
6163 "__val_offset", uintptr_type
);
6164 ret
= Type::make_builtin_named_type("__go_map_descriptor", sf
);
6169 // Reflection string for a map.
6172 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6174 ret
->append("map[");
6175 this->append_reflection(this->key_type_
, gogo
, ret
);
6177 this->append_reflection(this->val_type_
, gogo
, ret
);
6180 // Mangled name for a map.
6183 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6185 ret
->push_back('M');
6186 this->append_mangled_name(this->key_type_
, gogo
, ret
);
6188 this->append_mangled_name(this->val_type_
, gogo
, ret
);
6191 // Export a map type.
6194 Map_type::do_export(Export
* exp
) const
6196 exp
->write_c_string("map [");
6197 exp
->write_type(this->key_type_
);
6198 exp
->write_c_string("] ");
6199 exp
->write_type(this->val_type_
);
6202 // Import a map type.
6205 Map_type::do_import(Import
* imp
)
6207 imp
->require_c_string("map [");
6208 Type
* key_type
= imp
->read_type();
6209 imp
->require_c_string("] ");
6210 Type
* val_type
= imp
->read_type();
6211 return Type::make_map_type(key_type
, val_type
, imp
->location());
6217 Type::make_map_type(Type
* key_type
, Type
* val_type
, Location location
)
6219 return new Map_type(key_type
, val_type
, location
);
6222 // Class Channel_type.
6227 Channel_type::do_hash_for_method(Gogo
* gogo
) const
6229 unsigned int ret
= 0;
6230 if (this->may_send_
)
6232 if (this->may_receive_
)
6234 if (this->element_type_
!= NULL
)
6235 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
6239 // Whether this type is the same as T.
6242 Channel_type::is_identical(const Channel_type
* t
,
6243 bool errors_are_identical
) const
6245 if (!Type::are_identical(this->element_type(), t
->element_type(),
6246 errors_are_identical
, NULL
))
6248 return (this->may_send_
== t
->may_send_
6249 && this->may_receive_
== t
->may_receive_
);
6252 // Return the tree for a channel type. A channel is a pointer to a
6253 // __go_channel struct. The __go_channel struct is defined in
6254 // libgo/runtime/channel.h.
6257 Channel_type::do_get_backend(Gogo
* gogo
)
6259 static Btype
* backend_channel_type
;
6260 if (backend_channel_type
== NULL
)
6262 std::vector
<Backend::Btyped_identifier
> bfields
;
6263 Btype
* bt
= gogo
->backend()->struct_type(bfields
);
6264 bt
= gogo
->backend()->named_type("__go_channel", bt
,
6265 Linemap::predeclared_location());
6266 backend_channel_type
= gogo
->backend()->pointer_type(bt
);
6268 return backend_channel_type
;
6271 // Build a type descriptor for a channel type.
6274 Channel_type::make_chan_type_descriptor_type()
6279 Type
* tdt
= Type::make_type_descriptor_type();
6280 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6282 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6285 Type::make_builtin_struct_type(3,
6288 "dir", uintptr_type
);
6290 ret
= Type::make_builtin_named_type("ChanType", sf
);
6296 // Build a type descriptor for a map type.
6299 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6301 Location bloc
= Linemap::predeclared_location();
6303 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
6305 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
6307 Expression_list
* vals
= new Expression_list();
6310 Struct_field_list::const_iterator p
= fields
->begin();
6311 go_assert(p
->is_field_name("commonType"));
6312 vals
->push_back(this->type_descriptor_constructor(gogo
,
6313 RUNTIME_TYPE_KIND_CHAN
,
6317 go_assert(p
->is_field_name("elem"));
6318 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
6321 go_assert(p
->is_field_name("dir"));
6322 // These bits must match the ones in libgo/runtime/go-type.h.
6324 if (this->may_receive_
)
6326 if (this->may_send_
)
6329 mpz_init_set_ui(iv
, val
);
6330 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
6334 go_assert(p
== fields
->end());
6336 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
6339 // Reflection string.
6342 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6344 if (!this->may_send_
)
6346 ret
->append("chan");
6347 if (!this->may_receive_
)
6349 ret
->push_back(' ');
6350 this->append_reflection(this->element_type_
, gogo
, ret
);
6356 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6358 ret
->push_back('C');
6359 this->append_mangled_name(this->element_type_
, gogo
, ret
);
6360 if (this->may_send_
)
6361 ret
->push_back('s');
6362 if (this->may_receive_
)
6363 ret
->push_back('r');
6364 ret
->push_back('e');
6370 Channel_type::do_export(Export
* exp
) const
6372 exp
->write_c_string("chan ");
6373 if (this->may_send_
&& !this->may_receive_
)
6374 exp
->write_c_string("-< ");
6375 else if (this->may_receive_
&& !this->may_send_
)
6376 exp
->write_c_string("<- ");
6377 exp
->write_type(this->element_type_
);
6383 Channel_type::do_import(Import
* imp
)
6385 imp
->require_c_string("chan ");
6389 if (imp
->match_c_string("-< "))
6393 may_receive
= false;
6395 else if (imp
->match_c_string("<- "))
6407 Type
* element_type
= imp
->read_type();
6409 return Type::make_channel_type(may_send
, may_receive
, element_type
);
6412 // Make a new channel type.
6415 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
6417 return new Channel_type(send
, receive
, element_type
);
6420 // Class Interface_type.
6425 Interface_type::do_traverse(Traverse
* traverse
)
6427 Typed_identifier_list
* methods
= (this->methods_are_finalized_
6428 ? this->all_methods_
6429 : this->parse_methods_
);
6430 if (methods
== NULL
)
6431 return TRAVERSE_CONTINUE
;
6432 return methods
->traverse(traverse
);
6435 // Finalize the methods. This handles interface inheritance.
6438 Interface_type::finalize_methods()
6440 if (this->methods_are_finalized_
)
6442 this->methods_are_finalized_
= true;
6443 if (this->parse_methods_
== NULL
)
6446 this->all_methods_
= new Typed_identifier_list();
6447 this->all_methods_
->reserve(this->parse_methods_
->size());
6448 Typed_identifier_list inherit
;
6449 for (Typed_identifier_list::const_iterator pm
=
6450 this->parse_methods_
->begin();
6451 pm
!= this->parse_methods_
->end();
6454 const Typed_identifier
* p
= &*pm
;
6455 if (p
->name().empty())
6456 inherit
.push_back(*p
);
6457 else if (this->find_method(p
->name()) == NULL
)
6458 this->all_methods_
->push_back(*p
);
6460 error_at(p
->location(), "duplicate method %qs",
6461 Gogo::message_name(p
->name()).c_str());
6464 std::vector
<Named_type
*> seen
;
6465 seen
.reserve(inherit
.size());
6466 bool issued_recursive_error
= false;
6467 while (!inherit
.empty())
6469 Type
* t
= inherit
.back().type();
6470 Location tl
= inherit
.back().location();
6473 Interface_type
* it
= t
->interface_type();
6477 error_at(tl
, "interface contains embedded non-interface");
6482 if (!issued_recursive_error
)
6484 error_at(tl
, "invalid recursive interface");
6485 issued_recursive_error
= true;
6490 Named_type
* nt
= t
->named_type();
6491 if (nt
!= NULL
&& it
->parse_methods_
!= NULL
)
6493 std::vector
<Named_type
*>::const_iterator q
;
6494 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
6498 error_at(tl
, "inherited interface loop");
6502 if (q
!= seen
.end())
6507 const Typed_identifier_list
* imethods
= it
->parse_methods_
;
6508 if (imethods
== NULL
)
6510 for (Typed_identifier_list::const_iterator q
= imethods
->begin();
6511 q
!= imethods
->end();
6514 if (q
->name().empty())
6515 inherit
.push_back(*q
);
6516 else if (this->find_method(q
->name()) == NULL
)
6517 this->all_methods_
->push_back(Typed_identifier(q
->name(),
6520 error_at(tl
, "inherited method %qs is ambiguous",
6521 Gogo::message_name(q
->name()).c_str());
6525 if (!this->all_methods_
->empty())
6526 this->all_methods_
->sort_by_name();
6529 delete this->all_methods_
;
6530 this->all_methods_
= NULL
;
6534 // Return the method NAME, or NULL.
6536 const Typed_identifier
*
6537 Interface_type::find_method(const std::string
& name
) const
6539 go_assert(this->methods_are_finalized_
);
6540 if (this->all_methods_
== NULL
)
6542 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6543 p
!= this->all_methods_
->end();
6545 if (p
->name() == name
)
6550 // Return the method index.
6553 Interface_type::method_index(const std::string
& name
) const
6555 go_assert(this->methods_are_finalized_
&& this->all_methods_
!= NULL
);
6557 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6558 p
!= this->all_methods_
->end();
6560 if (p
->name() == name
)
6565 // Return whether NAME is an unexported method, for better error
6569 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
6571 go_assert(this->methods_are_finalized_
);
6572 if (this->all_methods_
== NULL
)
6574 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6575 p
!= this->all_methods_
->end();
6578 const std::string
& method_name(p
->name());
6579 if (Gogo::is_hidden_name(method_name
)
6580 && name
== Gogo::unpack_hidden_name(method_name
)
6581 && gogo
->pack_hidden_name(name
, false) != method_name
)
6587 // Whether this type is identical with T.
6590 Interface_type::is_identical(const Interface_type
* t
,
6591 bool errors_are_identical
) const
6593 // If methods have not been finalized, then we are asking whether
6594 // func redeclarations are the same. This is an error, so for
6595 // simplicity we say they are never the same.
6596 if (!this->methods_are_finalized_
|| !t
->methods_are_finalized_
)
6599 // We require the same methods with the same types. The methods
6600 // have already been sorted.
6601 if (this->all_methods_
== NULL
|| t
->all_methods_
== NULL
)
6602 return this->all_methods_
== t
->all_methods_
;
6604 if (this->assume_identical(this, t
) || t
->assume_identical(t
, this))
6607 Assume_identical
* hold_ai
= this->assume_identical_
;
6608 Assume_identical ai
;
6612 this->assume_identical_
= &ai
;
6614 Typed_identifier_list::const_iterator p1
= this->all_methods_
->begin();
6615 Typed_identifier_list::const_iterator p2
;
6616 for (p2
= t
->all_methods_
->begin(); p2
!= t
->all_methods_
->end(); ++p1
, ++p2
)
6618 if (p1
== this->all_methods_
->end())
6620 if (p1
->name() != p2
->name()
6621 || !Type::are_identical(p1
->type(), p2
->type(),
6622 errors_are_identical
, NULL
))
6626 this->assume_identical_
= hold_ai
;
6628 return p1
== this->all_methods_
->end() && p2
== t
->all_methods_
->end();
6631 // Return true if T1 and T2 are assumed to be identical during a type
6635 Interface_type::assume_identical(const Interface_type
* t1
,
6636 const Interface_type
* t2
) const
6638 for (Assume_identical
* p
= this->assume_identical_
;
6641 if ((p
->t1
== t1
&& p
->t2
== t2
) || (p
->t1
== t2
&& p
->t2
== t1
))
6646 // Whether we can assign the interface type T to this type. The types
6647 // are known to not be identical. An interface assignment is only
6648 // permitted if T is known to implement all methods in THIS.
6649 // Otherwise a type guard is required.
6652 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
6653 std::string
* reason
) const
6655 go_assert(this->methods_are_finalized_
&& t
->methods_are_finalized_
);
6656 if (this->all_methods_
== NULL
)
6658 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6659 p
!= this->all_methods_
->end();
6662 const Typed_identifier
* m
= t
->find_method(p
->name());
6668 snprintf(buf
, sizeof buf
,
6669 _("need explicit conversion; missing method %s%s%s"),
6670 open_quote
, Gogo::message_name(p
->name()).c_str(),
6672 reason
->assign(buf
);
6677 std::string subreason
;
6678 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
6682 std::string n
= Gogo::message_name(p
->name());
6683 size_t len
= 100 + n
.length() + subreason
.length();
6684 char* buf
= new char[len
];
6685 if (subreason
.empty())
6686 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
6687 open_quote
, n
.c_str(), close_quote
);
6690 _("incompatible type for method %s%s%s (%s)"),
6691 open_quote
, n
.c_str(), close_quote
,
6693 reason
->assign(buf
);
6706 Interface_type::do_hash_for_method(Gogo
*) const
6708 go_assert(this->methods_are_finalized_
);
6709 unsigned int ret
= 0;
6710 if (this->all_methods_
!= NULL
)
6712 for (Typed_identifier_list::const_iterator p
=
6713 this->all_methods_
->begin();
6714 p
!= this->all_methods_
->end();
6717 ret
= Type::hash_string(p
->name(), ret
);
6718 // We don't use the method type in the hash, to avoid
6719 // infinite recursion if an interface method uses a type
6720 // which is an interface which inherits from the interface
6722 // type T interface { F() interface {T}}
6729 // Return true if T implements the interface. If it does not, and
6730 // REASON is not NULL, set *REASON to a useful error message.
6733 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
6735 go_assert(this->methods_are_finalized_
);
6736 if (this->all_methods_
== NULL
)
6739 bool is_pointer
= false;
6740 const Named_type
* nt
= t
->named_type();
6741 const Struct_type
* st
= t
->struct_type();
6742 // If we start with a named type, we don't dereference it to find
6746 const Type
* pt
= t
->points_to();
6749 // If T is a pointer to a named type, then we need to look at
6750 // the type to which it points.
6752 nt
= pt
->named_type();
6753 st
= pt
->struct_type();
6757 // If we have a named type, get the methods from it rather than from
6762 // Only named and struct types have methods.
6763 if (nt
== NULL
&& st
== NULL
)
6767 if (t
->points_to() != NULL
6768 && t
->points_to()->interface_type() != NULL
)
6769 reason
->assign(_("pointer to interface type has no methods"));
6771 reason
->assign(_("type has no methods"));
6776 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
6780 if (t
->points_to() != NULL
6781 && t
->points_to()->interface_type() != NULL
)
6782 reason
->assign(_("pointer to interface type has no methods"));
6784 reason
->assign(_("type has no methods"));
6789 for (Typed_identifier_list::const_iterator p
= this->all_methods_
->begin();
6790 p
!= this->all_methods_
->end();
6793 bool is_ambiguous
= false;
6794 Method
* m
= (nt
!= NULL
6795 ? nt
->method_function(p
->name(), &is_ambiguous
)
6796 : st
->method_function(p
->name(), &is_ambiguous
));
6801 std::string n
= Gogo::message_name(p
->name());
6802 size_t len
= n
.length() + 100;
6803 char* buf
= new char[len
];
6805 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
6806 open_quote
, n
.c_str(), close_quote
);
6808 snprintf(buf
, len
, _("missing method %s%s%s"),
6809 open_quote
, n
.c_str(), close_quote
);
6810 reason
->assign(buf
);
6816 Function_type
*p_fn_type
= p
->type()->function_type();
6817 Function_type
* m_fn_type
= m
->type()->function_type();
6818 go_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
6819 std::string subreason
;
6820 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
6824 std::string n
= Gogo::message_name(p
->name());
6825 size_t len
= 100 + n
.length() + subreason
.length();
6826 char* buf
= new char[len
];
6827 if (subreason
.empty())
6828 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
6829 open_quote
, n
.c_str(), close_quote
);
6832 _("incompatible type for method %s%s%s (%s)"),
6833 open_quote
, n
.c_str(), close_quote
,
6835 reason
->assign(buf
);
6841 if (!is_pointer
&& !m
->is_value_method())
6845 std::string n
= Gogo::message_name(p
->name());
6846 size_t len
= 100 + n
.length();
6847 char* buf
= new char[len
];
6849 _("method %s%s%s requires a pointer receiver"),
6850 open_quote
, n
.c_str(), close_quote
);
6851 reason
->assign(buf
);
6861 // Return the backend representation of the empty interface type. We
6862 // use the same struct for all empty interfaces.
6865 Interface_type::get_backend_empty_interface_type(Gogo
* gogo
)
6867 static Btype
* empty_interface_type
;
6868 if (empty_interface_type
== NULL
)
6870 std::vector
<Backend::Btyped_identifier
> bfields(2);
6872 Location bloc
= Linemap::predeclared_location();
6874 Type
* pdt
= Type::make_type_descriptor_ptr_type();
6875 bfields
[0].name
= "__type_descriptor";
6876 bfields
[0].btype
= pdt
->get_backend(gogo
);
6877 bfields
[0].location
= bloc
;
6879 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6880 bfields
[1].name
= "__object";
6881 bfields
[1].btype
= vt
->get_backend(gogo
);
6882 bfields
[1].location
= bloc
;
6884 empty_interface_type
= gogo
->backend()->struct_type(bfields
);
6886 return empty_interface_type
;
6889 // Return the fields of a non-empty interface type. This is not
6890 // declared in types.h so that types.h doesn't have to #include
6894 get_backend_interface_fields(Gogo
* gogo
, Interface_type
* type
,
6895 bool use_placeholder
,
6896 std::vector
<Backend::Btyped_identifier
>* bfields
)
6898 Location loc
= type
->location();
6900 std::vector
<Backend::Btyped_identifier
> mfields(type
->methods()->size() + 1);
6902 Type
* pdt
= Type::make_type_descriptor_ptr_type();
6903 mfields
[0].name
= "__type_descriptor";
6904 mfields
[0].btype
= pdt
->get_backend(gogo
);
6905 mfields
[0].location
= loc
;
6907 std::string last_name
= "";
6909 for (Typed_identifier_list::const_iterator p
= type
->methods()->begin();
6910 p
!= type
->methods()->end();
6913 // The type of the method in Go only includes the parameters.
6914 // The actual method also has a receiver, which is always a
6915 // pointer. We need to add that pointer type here in order to
6916 // generate the correct type for the backend.
6917 Function_type
* ft
= p
->type()->function_type();
6918 go_assert(ft
->receiver() == NULL
);
6920 const Typed_identifier_list
* params
= ft
->parameters();
6921 Typed_identifier_list
* mparams
= new Typed_identifier_list();
6923 mparams
->reserve(params
->size() + 1);
6924 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6925 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
6928 for (Typed_identifier_list::const_iterator pp
= params
->begin();
6929 pp
!= params
->end();
6931 mparams
->push_back(*pp
);
6934 Typed_identifier_list
* mresults
= (ft
->results() == NULL
6936 : ft
->results()->copy());
6937 Function_type
* mft
= Type::make_function_type(NULL
, mparams
, mresults
,
6940 mfields
[i
].name
= Gogo::unpack_hidden_name(p
->name());
6941 mfields
[i
].btype
= (use_placeholder
6942 ? mft
->get_backend_placeholder(gogo
)
6943 : mft
->get_backend(gogo
));
6944 mfields
[i
].location
= loc
;
6945 // Sanity check: the names should be sorted.
6946 go_assert(p
->name() > last_name
);
6947 last_name
= p
->name();
6950 Btype
* methods
= gogo
->backend()->struct_type(mfields
);
6954 (*bfields
)[0].name
= "__methods";
6955 (*bfields
)[0].btype
= gogo
->backend()->pointer_type(methods
);
6956 (*bfields
)[0].location
= loc
;
6958 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6959 (*bfields
)[1].name
= "__object";
6960 (*bfields
)[1].btype
= vt
->get_backend(gogo
);
6961 (*bfields
)[1].location
= Linemap::predeclared_location();
6964 // Return a tree for an interface type. An interface is a pointer to
6965 // a struct. The struct has three fields. The first field is a
6966 // pointer to the type descriptor for the dynamic type of the object.
6967 // The second field is a pointer to a table of methods for the
6968 // interface to be used with the object. The third field is the value
6969 // of the object itself.
6972 Interface_type::do_get_backend(Gogo
* gogo
)
6974 if (this->is_empty())
6975 return Interface_type::get_backend_empty_interface_type(gogo
);
6978 if (this->interface_btype_
!= NULL
)
6979 return this->interface_btype_
;
6980 this->interface_btype_
=
6981 gogo
->backend()->placeholder_struct_type("", this->location_
);
6982 std::vector
<Backend::Btyped_identifier
> bfields
;
6983 get_backend_interface_fields(gogo
, this, false, &bfields
);
6984 if (!gogo
->backend()->set_placeholder_struct_type(this->interface_btype_
,
6986 this->interface_btype_
= gogo
->backend()->error_type();
6987 return this->interface_btype_
;
6991 // Finish the backend representation of the methods.
6994 Interface_type::finish_backend_methods(Gogo
* gogo
)
6996 if (!this->interface_type()->is_empty())
6998 const Typed_identifier_list
* methods
= this->methods();
6999 if (methods
!= NULL
)
7001 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7002 p
!= methods
->end();
7004 p
->type()->get_backend(gogo
);
7009 // The type of an interface type descriptor.
7012 Interface_type::make_interface_type_descriptor_type()
7017 Type
* tdt
= Type::make_type_descriptor_type();
7018 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
7020 Type
* string_type
= Type::lookup_string_type();
7021 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
7024 Type::make_builtin_struct_type(3,
7025 "name", pointer_string_type
,
7026 "pkgPath", pointer_string_type
,
7029 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
7031 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
7033 Struct_type
* s
= Type::make_builtin_struct_type(2,
7035 "methods", slice_nsm
);
7037 ret
= Type::make_builtin_named_type("InterfaceType", s
);
7043 // Build a type descriptor for an interface type.
7046 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7048 Location bloc
= Linemap::predeclared_location();
7050 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
7052 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
7054 Expression_list
* ivals
= new Expression_list();
7057 Struct_field_list::const_iterator pif
= ifields
->begin();
7058 go_assert(pif
->is_field_name("commonType"));
7059 const int rt
= RUNTIME_TYPE_KIND_INTERFACE
;
7060 ivals
->push_back(this->type_descriptor_constructor(gogo
, rt
, name
, NULL
,
7064 go_assert(pif
->is_field_name("methods"));
7066 Expression_list
* methods
= new Expression_list();
7067 if (this->all_methods_
!= NULL
)
7069 Type
* elemtype
= pif
->type()->array_type()->element_type();
7071 methods
->reserve(this->all_methods_
->size());
7072 for (Typed_identifier_list::const_iterator pm
=
7073 this->all_methods_
->begin();
7074 pm
!= this->all_methods_
->end();
7077 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
7079 Expression_list
* mvals
= new Expression_list();
7082 Struct_field_list::const_iterator pmf
= mfields
->begin();
7083 go_assert(pmf
->is_field_name("name"));
7084 std::string s
= Gogo::unpack_hidden_name(pm
->name());
7085 Expression
* e
= Expression::make_string(s
, bloc
);
7086 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7089 go_assert(pmf
->is_field_name("pkgPath"));
7090 if (!Gogo::is_hidden_name(pm
->name()))
7091 mvals
->push_back(Expression::make_nil(bloc
));
7094 s
= Gogo::hidden_name_pkgpath(pm
->name());
7095 e
= Expression::make_string(s
, bloc
);
7096 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
7100 go_assert(pmf
->is_field_name("typ"));
7101 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
7104 go_assert(pmf
== mfields
->end());
7106 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
7108 methods
->push_back(e
);
7112 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
7116 go_assert(pif
== ifields
->end());
7118 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
7121 // Reflection string.
7124 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7126 ret
->append("interface {");
7127 const Typed_identifier_list
* methods
= this->parse_methods_
;
7128 if (methods
!= NULL
)
7130 ret
->push_back(' ');
7131 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7132 p
!= methods
->end();
7135 if (p
!= methods
->begin())
7137 if (p
->name().empty())
7138 this->append_reflection(p
->type(), gogo
, ret
);
7141 if (!Gogo::is_hidden_name(p
->name()))
7142 ret
->append(p
->name());
7143 else if (gogo
->pkgpath_from_option())
7144 ret
->append(p
->name().substr(1));
7147 // If no -fgo-pkgpath option, backward compatibility
7148 // for how this used to work before -fgo-pkgpath was
7150 std::string pkgpath
= Gogo::hidden_name_pkgpath(p
->name());
7151 ret
->append(pkgpath
.substr(pkgpath
.find('.') + 1));
7152 ret
->push_back('.');
7153 ret
->append(Gogo::unpack_hidden_name(p
->name()));
7155 std::string sub
= p
->type()->reflection(gogo
);
7156 go_assert(sub
.compare(0, 4, "func") == 0);
7157 sub
= sub
.substr(4);
7161 ret
->push_back(' ');
7169 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7171 go_assert(this->methods_are_finalized_
);
7173 ret
->push_back('I');
7175 const Typed_identifier_list
* methods
= this->all_methods_
;
7176 if (methods
!= NULL
&& !this->seen_
)
7179 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7180 p
!= methods
->end();
7183 if (!p
->name().empty())
7185 std::string n
= Gogo::unpack_hidden_name(p
->name());
7187 snprintf(buf
, sizeof buf
, "%u_",
7188 static_cast<unsigned int>(n
.length()));
7192 this->append_mangled_name(p
->type(), gogo
, ret
);
7194 this->seen_
= false;
7197 ret
->push_back('e');
7203 Interface_type::do_export(Export
* exp
) const
7205 exp
->write_c_string("interface { ");
7207 const Typed_identifier_list
* methods
= this->parse_methods_
;
7208 if (methods
!= NULL
)
7210 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
7211 pm
!= methods
->end();
7214 if (pm
->name().empty())
7216 exp
->write_c_string("? ");
7217 exp
->write_type(pm
->type());
7221 exp
->write_string(pm
->name());
7222 exp
->write_c_string(" (");
7224 const Function_type
* fntype
= pm
->type()->function_type();
7227 const Typed_identifier_list
* parameters
= fntype
->parameters();
7228 if (parameters
!= NULL
)
7230 bool is_varargs
= fntype
->is_varargs();
7231 for (Typed_identifier_list::const_iterator pp
=
7232 parameters
->begin();
7233 pp
!= parameters
->end();
7239 exp
->write_c_string(", ");
7240 exp
->write_name(pp
->name());
7241 exp
->write_c_string(" ");
7242 if (!is_varargs
|| pp
+ 1 != parameters
->end())
7243 exp
->write_type(pp
->type());
7246 exp
->write_c_string("...");
7247 Type
*pptype
= pp
->type();
7248 exp
->write_type(pptype
->array_type()->element_type());
7253 exp
->write_c_string(")");
7255 const Typed_identifier_list
* results
= fntype
->results();
7256 if (results
!= NULL
)
7258 exp
->write_c_string(" ");
7259 if (results
->size() == 1 && results
->begin()->name().empty())
7260 exp
->write_type(results
->begin()->type());
7264 exp
->write_c_string("(");
7265 for (Typed_identifier_list::const_iterator p
=
7267 p
!= results
->end();
7273 exp
->write_c_string(", ");
7274 exp
->write_name(p
->name());
7275 exp
->write_c_string(" ");
7276 exp
->write_type(p
->type());
7278 exp
->write_c_string(")");
7283 exp
->write_c_string("; ");
7287 exp
->write_c_string("}");
7290 // Import an interface type.
7293 Interface_type::do_import(Import
* imp
)
7295 imp
->require_c_string("interface { ");
7297 Typed_identifier_list
* methods
= new Typed_identifier_list
;
7298 while (imp
->peek_char() != '}')
7300 std::string name
= imp
->read_identifier();
7304 imp
->require_c_string(" ");
7305 Type
* t
= imp
->read_type();
7306 methods
->push_back(Typed_identifier("", t
, imp
->location()));
7307 imp
->require_c_string("; ");
7311 imp
->require_c_string(" (");
7313 Typed_identifier_list
* parameters
;
7314 bool is_varargs
= false;
7315 if (imp
->peek_char() == ')')
7319 parameters
= new Typed_identifier_list
;
7322 std::string name
= imp
->read_name();
7323 imp
->require_c_string(" ");
7325 if (imp
->match_c_string("..."))
7331 Type
* ptype
= imp
->read_type();
7333 ptype
= Type::make_array_type(ptype
, NULL
);
7334 parameters
->push_back(Typed_identifier(name
, ptype
,
7336 if (imp
->peek_char() != ',')
7338 go_assert(!is_varargs
);
7339 imp
->require_c_string(", ");
7342 imp
->require_c_string(")");
7344 Typed_identifier_list
* results
;
7345 if (imp
->peek_char() != ' ')
7349 results
= new Typed_identifier_list
;
7351 if (imp
->peek_char() != '(')
7353 Type
* rtype
= imp
->read_type();
7354 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
7361 std::string name
= imp
->read_name();
7362 imp
->require_c_string(" ");
7363 Type
* rtype
= imp
->read_type();
7364 results
->push_back(Typed_identifier(name
, rtype
,
7366 if (imp
->peek_char() != ',')
7368 imp
->require_c_string(", ");
7370 imp
->require_c_string(")");
7374 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
7378 fntype
->set_is_varargs();
7379 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
7381 imp
->require_c_string("; ");
7384 imp
->require_c_string("}");
7386 if (methods
->empty())
7392 return Type::make_interface_type(methods
, imp
->location());
7395 // Make an interface type.
7398 Type::make_interface_type(Typed_identifier_list
* methods
,
7401 return new Interface_type(methods
, location
);
7404 // Make an empty interface type.
7407 Type::make_empty_interface_type(Location location
)
7409 Interface_type
* ret
= new Interface_type(NULL
, location
);
7410 ret
->finalize_methods();
7416 // Bind a method to an object.
7419 Method::bind_method(Expression
* expr
, Location location
) const
7421 if (this->stub_
== NULL
)
7423 // When there is no stub object, the binding is determined by
7425 return this->do_bind_method(expr
, location
);
7427 return Expression::make_bound_method(expr
, this->stub_
, location
);
7430 // Return the named object associated with a method. This may only be
7431 // called after methods are finalized.
7434 Method::named_object() const
7436 if (this->stub_
!= NULL
)
7438 return this->do_named_object();
7441 // Class Named_method.
7443 // The type of the method.
7446 Named_method::do_type() const
7448 if (this->named_object_
->is_function())
7449 return this->named_object_
->func_value()->type();
7450 else if (this->named_object_
->is_function_declaration())
7451 return this->named_object_
->func_declaration_value()->type();
7456 // Return the location of the method receiver.
7459 Named_method::do_receiver_location() const
7461 return this->do_type()->receiver()->location();
7464 // Bind a method to an object.
7467 Named_method::do_bind_method(Expression
* expr
, Location location
) const
7469 Named_object
* no
= this->named_object_
;
7470 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, no
,
7472 // If this is not a local method, and it does not use a stub, then
7473 // the real method expects a different type. We need to cast the
7475 if (this->depth() > 0 && !this->needs_stub_method())
7477 Function_type
* ftype
= this->do_type();
7478 go_assert(ftype
->is_method());
7479 Type
* frtype
= ftype
->receiver()->type();
7480 bme
->set_first_argument_type(frtype
);
7485 // Class Interface_method.
7487 // Bind a method to an object.
7490 Interface_method::do_bind_method(Expression
* expr
,
7491 Location location
) const
7493 return Expression::make_interface_field_reference(expr
, this->name_
,
7499 // Insert a new method. Return true if it was inserted, false
7503 Methods::insert(const std::string
& name
, Method
* m
)
7505 std::pair
<Method_map::iterator
, bool> ins
=
7506 this->methods_
.insert(std::make_pair(name
, m
));
7511 Method
* old_method
= ins
.first
->second
;
7512 if (m
->depth() < old_method
->depth())
7515 ins
.first
->second
= m
;
7520 if (m
->depth() == old_method
->depth())
7521 old_method
->set_is_ambiguous();
7527 // Return the number of unambiguous methods.
7530 Methods::count() const
7533 for (Method_map::const_iterator p
= this->methods_
.begin();
7534 p
!= this->methods_
.end();
7536 if (!p
->second
->is_ambiguous())
7541 // Class Named_type.
7543 // Return the name of the type.
7546 Named_type::name() const
7548 return this->named_object_
->name();
7551 // Return the name of the type to use in an error message.
7554 Named_type::message_name() const
7556 return this->named_object_
->message_name();
7559 // Whether this is an alias. There are currently only two aliases so
7560 // we just recognize them by name.
7563 Named_type::is_alias() const
7565 if (!this->is_builtin())
7567 const std::string
& name(this->name());
7568 return name
== "byte" || name
== "rune";
7571 // Return the base type for this type. We have to be careful about
7572 // circular type definitions, which are invalid but may be seen here.
7575 Named_type::named_base()
7580 Type
* ret
= this->type_
->base();
7581 this->seen_
= false;
7586 Named_type::named_base() const
7591 const Type
* ret
= this->type_
->base();
7592 this->seen_
= false;
7596 // Return whether this is an error type. We have to be careful about
7597 // circular type definitions, which are invalid but may be seen here.
7600 Named_type::is_named_error_type() const
7605 bool ret
= this->type_
->is_error_type();
7606 this->seen_
= false;
7610 // Whether this type is comparable. We have to be careful about
7611 // circular type definitions.
7614 Named_type::named_type_is_comparable(std::string
* reason
) const
7619 bool ret
= Type::are_compatible_for_comparison(true, this->type_
,
7620 this->type_
, reason
);
7621 this->seen_
= false;
7625 // Add a method to this type.
7628 Named_type::add_method(const std::string
& name
, Function
* function
)
7630 if (this->local_methods_
== NULL
)
7631 this->local_methods_
= new Bindings(NULL
);
7632 return this->local_methods_
->add_function(name
, NULL
, function
);
7635 // Add a method declaration to this type.
7638 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
7639 Function_type
* type
,
7642 if (this->local_methods_
== NULL
)
7643 this->local_methods_
= new Bindings(NULL
);
7644 return this->local_methods_
->add_function_declaration(name
, package
, type
,
7648 // Add an existing method to this type.
7651 Named_type::add_existing_method(Named_object
* no
)
7653 if (this->local_methods_
== NULL
)
7654 this->local_methods_
= new Bindings(NULL
);
7655 this->local_methods_
->add_named_object(no
);
7658 // Look for a local method NAME, and returns its named object, or NULL
7662 Named_type::find_local_method(const std::string
& name
) const
7664 if (this->local_methods_
== NULL
)
7666 return this->local_methods_
->lookup(name
);
7669 // Return whether NAME is an unexported field or method, for better
7673 Named_type::is_unexported_local_method(Gogo
* gogo
,
7674 const std::string
& name
) const
7676 Bindings
* methods
= this->local_methods_
;
7677 if (methods
!= NULL
)
7679 for (Bindings::const_declarations_iterator p
=
7680 methods
->begin_declarations();
7681 p
!= methods
->end_declarations();
7684 if (Gogo::is_hidden_name(p
->first
)
7685 && name
== Gogo::unpack_hidden_name(p
->first
)
7686 && gogo
->pack_hidden_name(name
, false) != p
->first
)
7693 // Build the complete list of methods for this type, which means
7694 // recursively including all methods for anonymous fields. Create all
7698 Named_type::finalize_methods(Gogo
* gogo
)
7700 if (this->all_methods_
!= NULL
)
7703 if (this->local_methods_
!= NULL
7704 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
7706 const Bindings
* lm
= this->local_methods_
;
7707 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
7708 p
!= lm
->end_declarations();
7710 error_at(p
->second
->location(),
7711 "invalid pointer or interface receiver type");
7712 delete this->local_methods_
;
7713 this->local_methods_
= NULL
;
7717 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
7720 // Return the method NAME, or NULL if there isn't one or if it is
7721 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
7725 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
7727 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
7730 // Return a pointer to the interface method table for this type for
7731 // the interface INTERFACE. IS_POINTER is true if this is for a
7735 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
7738 go_assert(!interface
->is_empty());
7740 Interface_method_tables
** pimt
= (is_pointer
7741 ? &this->interface_method_tables_
7742 : &this->pointer_interface_method_tables_
);
7745 *pimt
= new Interface_method_tables(5);
7747 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
7748 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
7752 // This is a new entry in the hash table.
7753 go_assert(ins
.first
->second
== NULL_TREE
);
7754 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
7759 tree decl
= ins
.first
->second
;
7760 if (decl
== error_mark_node
)
7761 return error_mark_node
;
7762 go_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
7763 return build_fold_addr_expr(decl
);
7766 // Return whether a named type has any hidden fields.
7769 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
7774 bool ret
= this->type_
->has_hidden_fields(this, reason
);
7775 this->seen_
= false;
7779 // Look for a use of a complete type within another type. This is
7780 // used to check that we don't try to use a type within itself.
7782 class Find_type_use
: public Traverse
7785 Find_type_use(Named_type
* find_type
)
7786 : Traverse(traverse_types
),
7787 find_type_(find_type
), found_(false)
7790 // Whether we found the type.
7793 { return this->found_
; }
7800 // The type we are looking for.
7801 Named_type
* find_type_
;
7802 // Whether we found the type.
7806 // Check for FIND_TYPE in TYPE.
7809 Find_type_use::type(Type
* type
)
7811 if (type
->named_type() != NULL
&& this->find_type_
== type
->named_type())
7813 this->found_
= true;
7814 return TRAVERSE_EXIT
;
7817 // It's OK if we see a reference to the type in any type which is
7818 // essentially a pointer: a pointer, a slice, a function, a map, or
7820 if (type
->points_to() != NULL
7821 || type
->is_slice_type()
7822 || type
->function_type() != NULL
7823 || type
->map_type() != NULL
7824 || type
->channel_type() != NULL
)
7825 return TRAVERSE_SKIP_COMPONENTS
;
7827 // For an interface, a reference to the type in a method type should
7828 // be ignored, but we have to consider direct inheritance. When
7829 // this is called, there may be cases of direct inheritance
7830 // represented as a method with no name.
7831 if (type
->interface_type() != NULL
)
7833 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
7834 if (methods
!= NULL
)
7836 for (Typed_identifier_list::const_iterator p
= methods
->begin();
7837 p
!= methods
->end();
7840 if (p
->name().empty())
7842 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
7843 return TRAVERSE_EXIT
;
7847 return TRAVERSE_SKIP_COMPONENTS
;
7850 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
7851 // to convert TYPE to the backend representation before we convert
7853 if (type
->named_type() != NULL
)
7855 switch (type
->base()->classification())
7857 case Type::TYPE_ERROR
:
7858 case Type::TYPE_BOOLEAN
:
7859 case Type::TYPE_INTEGER
:
7860 case Type::TYPE_FLOAT
:
7861 case Type::TYPE_COMPLEX
:
7862 case Type::TYPE_STRING
:
7863 case Type::TYPE_NIL
:
7866 case Type::TYPE_ARRAY
:
7867 case Type::TYPE_STRUCT
:
7868 this->find_type_
->add_dependency(type
->named_type());
7871 case Type::TYPE_NAMED
:
7872 case Type::TYPE_FORWARD
:
7873 go_assert(saw_errors());
7876 case Type::TYPE_VOID
:
7877 case Type::TYPE_SINK
:
7878 case Type::TYPE_FUNCTION
:
7879 case Type::TYPE_POINTER
:
7880 case Type::TYPE_CALL_MULTIPLE_RESULT
:
7881 case Type::TYPE_MAP
:
7882 case Type::TYPE_CHANNEL
:
7883 case Type::TYPE_INTERFACE
:
7889 return TRAVERSE_CONTINUE
;
7892 // Verify that a named type does not refer to itself.
7895 Named_type::do_verify()
7897 if (this->is_verified_
)
7899 this->is_verified_
= true;
7901 Find_type_use
find(this);
7902 Type::traverse(this->type_
, &find
);
7905 error_at(this->location_
, "invalid recursive type %qs",
7906 this->message_name().c_str());
7907 this->is_error_
= true;
7911 // Check whether any of the local methods overloads an existing
7912 // struct field or interface method. We don't need to check the
7913 // list of methods against itself: that is handled by the Bindings
7915 if (this->local_methods_
!= NULL
)
7917 Struct_type
* st
= this->type_
->struct_type();
7920 for (Bindings::const_declarations_iterator p
=
7921 this->local_methods_
->begin_declarations();
7922 p
!= this->local_methods_
->end_declarations();
7925 const std::string
& name(p
->first
);
7926 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
7928 error_at(p
->second
->location(),
7929 "method %qs redeclares struct field name",
7930 Gogo::message_name(name
).c_str());
7939 // Return whether this type is or contains a pointer.
7942 Named_type::do_has_pointer() const
7947 bool ret
= this->type_
->has_pointer();
7948 this->seen_
= false;
7952 // Return whether comparisons for this type can use the identity
7956 Named_type::do_compare_is_identity(Gogo
* gogo
) const
7958 // We don't use this->seen_ here because compare_is_identity may
7959 // call base() later, and that will mess up if seen_ is set here.
7960 if (this->seen_in_compare_is_identity_
)
7962 this->seen_in_compare_is_identity_
= true;
7963 bool ret
= this->type_
->compare_is_identity(gogo
);
7964 this->seen_in_compare_is_identity_
= false;
7968 // Return a hash code. This is used for method lookup. We simply
7969 // hash on the name itself.
7972 Named_type::do_hash_for_method(Gogo
* gogo
) const
7974 if (this->is_alias())
7975 return this->type_
->named_type()->do_hash_for_method(gogo
);
7977 const std::string
& name(this->named_object()->name());
7978 unsigned int ret
= Type::hash_string(name
, 0);
7980 // GOGO will be NULL here when called from Type_hash_identical.
7981 // That is OK because that is only used for internal hash tables
7982 // where we are going to be comparing named types for equality. In
7983 // other cases, which are cases where the runtime is going to
7984 // compare hash codes to see if the types are the same, we need to
7985 // include the pkgpath in the hash.
7986 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
7988 const Package
* package
= this->named_object()->package();
7989 if (package
== NULL
)
7990 ret
= Type::hash_string(gogo
->pkgpath(), ret
);
7992 ret
= Type::hash_string(package
->pkgpath(), ret
);
7998 // Convert a named type to the backend representation. In order to
7999 // get dependencies right, we fill in a dummy structure for this type,
8000 // then convert all the dependencies, then complete this type. When
8001 // this function is complete, the size of the type is known.
8004 Named_type::convert(Gogo
* gogo
)
8006 if (this->is_error_
|| this->is_converted_
)
8009 this->create_placeholder(gogo
);
8011 // If we are called to turn unsafe.Sizeof into a constant, we may
8012 // not have verified the type yet. We have to make sure it is
8013 // verified, since that sets the list of dependencies.
8016 // Convert all the dependencies. If they refer indirectly back to
8017 // this type, they will pick up the intermediate tree we just
8019 for (std::vector
<Named_type
*>::const_iterator p
= this->dependencies_
.begin();
8020 p
!= this->dependencies_
.end();
8022 (*p
)->convert(gogo
);
8024 // Complete this type.
8025 Btype
* bt
= this->named_btype_
;
8026 Type
* base
= this->type_
->base();
8027 switch (base
->classification())
8044 // The size of these types is already correct. We don't worry
8045 // about filling them in until later, when we also track
8046 // circular references.
8051 std::vector
<Backend::Btyped_identifier
> bfields
;
8052 get_backend_struct_fields(gogo
, base
->struct_type()->fields(),
8054 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8055 bt
= gogo
->backend()->error_type();
8060 // Slice types were completed in create_placeholder.
8061 if (!base
->is_slice_type())
8063 Btype
* bet
= base
->array_type()->get_backend_element(gogo
, true);
8064 Bexpression
* blen
= base
->array_type()->get_backend_length(gogo
);
8065 if (!gogo
->backend()->set_placeholder_array_type(bt
, bet
, blen
))
8066 bt
= gogo
->backend()->error_type();
8070 case TYPE_INTERFACE
:
8071 // Interface types were completed in create_placeholder.
8079 case TYPE_CALL_MULTIPLE_RESULT
:
8085 this->named_btype_
= bt
;
8086 this->is_converted_
= true;
8087 this->is_placeholder_
= false;
8090 // Create the placeholder for a named type. This is the first step in
8091 // converting to the backend representation.
8094 Named_type::create_placeholder(Gogo
* gogo
)
8096 if (this->is_error_
)
8097 this->named_btype_
= gogo
->backend()->error_type();
8099 if (this->named_btype_
!= NULL
)
8102 // Create the structure for this type. Note that because we call
8103 // base() here, we don't attempt to represent a named type defined
8104 // as another named type. Instead both named types will point to
8105 // different base representations.
8106 Type
* base
= this->type_
->base();
8108 bool set_name
= true;
8109 switch (base
->classification())
8112 this->is_error_
= true;
8113 this->named_btype_
= gogo
->backend()->error_type();
8123 // These are simple basic types, we can just create them
8125 bt
= Type::get_named_base_btype(gogo
, base
);
8130 // All maps and channels have the same backend representation.
8131 bt
= Type::get_named_base_btype(gogo
, base
);
8137 bool for_function
= base
->classification() == TYPE_FUNCTION
;
8138 bt
= gogo
->backend()->placeholder_pointer_type(this->name(),
8146 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8148 this->is_placeholder_
= true;
8153 if (base
->is_slice_type())
8154 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8158 bt
= gogo
->backend()->placeholder_array_type(this->name(),
8160 this->is_placeholder_
= true;
8165 case TYPE_INTERFACE
:
8166 if (base
->interface_type()->is_empty())
8167 bt
= Interface_type::get_backend_empty_interface_type(gogo
);
8170 bt
= gogo
->backend()->placeholder_struct_type(this->name(),
8178 case TYPE_CALL_MULTIPLE_RESULT
:
8185 bt
= gogo
->backend()->named_type(this->name(), bt
, this->location_
);
8187 this->named_btype_
= bt
;
8189 if (base
->is_slice_type())
8191 // We do not record slices as dependencies of other types,
8192 // because we can fill them in completely here with the final
8194 std::vector
<Backend::Btyped_identifier
> bfields
;
8195 get_backend_slice_fields(gogo
, base
->array_type(), true, &bfields
);
8196 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8197 this->named_btype_
= gogo
->backend()->error_type();
8199 else if (base
->interface_type() != NULL
8200 && !base
->interface_type()->is_empty())
8202 // We do not record interfaces as dependencies of other types,
8203 // because we can fill them in completely here with the final
8205 std::vector
<Backend::Btyped_identifier
> bfields
;
8206 get_backend_interface_fields(gogo
, base
->interface_type(), true,
8208 if (!gogo
->backend()->set_placeholder_struct_type(bt
, bfields
))
8209 this->named_btype_
= gogo
->backend()->error_type();
8213 // Get a tree for a named type.
8216 Named_type::do_get_backend(Gogo
* gogo
)
8218 if (this->is_error_
)
8219 return gogo
->backend()->error_type();
8221 Btype
* bt
= this->named_btype_
;
8223 if (!gogo
->named_types_are_converted())
8225 // We have not completed converting named types. NAMED_BTYPE_
8226 // is a placeholder and we shouldn't do anything further.
8230 // We don't build dependencies for types whose sizes do not
8231 // change or are not relevant, so we may see them here while
8232 // converting types.
8233 this->create_placeholder(gogo
);
8234 bt
= this->named_btype_
;
8235 go_assert(bt
!= NULL
);
8239 // We are not converting types. This should only be called if the
8240 // type has already been converted.
8241 if (!this->is_converted_
)
8243 go_assert(saw_errors());
8244 return gogo
->backend()->error_type();
8247 go_assert(bt
!= NULL
);
8249 // Complete the tree.
8250 Type
* base
= this->type_
->base();
8252 switch (base
->classification())
8255 return gogo
->backend()->error_type();
8269 if (!this->seen_in_get_backend_
)
8271 this->seen_in_get_backend_
= true;
8272 base
->struct_type()->finish_backend_fields(gogo
);
8273 this->seen_in_get_backend_
= false;
8278 if (!this->seen_in_get_backend_
)
8280 this->seen_in_get_backend_
= true;
8281 base
->array_type()->finish_backend_element(gogo
);
8282 this->seen_in_get_backend_
= false;
8286 case TYPE_INTERFACE
:
8287 if (!this->seen_in_get_backend_
)
8289 this->seen_in_get_backend_
= true;
8290 base
->interface_type()->finish_backend_methods(gogo
);
8291 this->seen_in_get_backend_
= false;
8296 // Don't build a circular data structure. GENERIC can't handle
8298 if (this->seen_in_get_backend_
)
8300 this->is_circular_
= true;
8301 return gogo
->backend()->circular_pointer_type(bt
, true);
8303 this->seen_in_get_backend_
= true;
8304 bt1
= Type::get_named_base_btype(gogo
, base
);
8305 this->seen_in_get_backend_
= false;
8306 if (this->is_circular_
)
8307 bt1
= gogo
->backend()->circular_pointer_type(bt
, true);
8308 if (!gogo
->backend()->set_placeholder_function_type(bt
, bt1
))
8309 bt
= gogo
->backend()->error_type();
8313 // Don't build a circular data structure. GENERIC can't handle
8315 if (this->seen_in_get_backend_
)
8317 this->is_circular_
= true;
8318 return gogo
->backend()->circular_pointer_type(bt
, false);
8320 this->seen_in_get_backend_
= true;
8321 bt1
= Type::get_named_base_btype(gogo
, base
);
8322 this->seen_in_get_backend_
= false;
8323 if (this->is_circular_
)
8324 bt1
= gogo
->backend()->circular_pointer_type(bt
, false);
8325 if (!gogo
->backend()->set_placeholder_pointer_type(bt
, bt1
))
8326 bt
= gogo
->backend()->error_type();
8331 case TYPE_CALL_MULTIPLE_RESULT
:
8340 // Build a type descriptor for a named type.
8343 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8345 if (name
== NULL
&& this->is_alias())
8346 return this->type_
->type_descriptor(gogo
, this->type_
);
8348 // If NAME is not NULL, then we don't really want the type
8349 // descriptor for this type; we want the descriptor for the
8350 // underlying type, giving it the name NAME.
8351 return this->named_type_descriptor(gogo
, this->type_
,
8352 name
== NULL
? this : name
);
8355 // Add to the reflection string. This is used mostly for the name of
8356 // the type used in a type descriptor, not for actual reflection
8360 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8362 if (this->is_alias())
8364 this->append_reflection(this->type_
, gogo
, ret
);
8367 if (!this->is_builtin())
8369 // We handle -fgo-prefix and -fgo-pkgpath differently here for
8370 // compatibility with how the compiler worked before
8371 // -fgo-pkgpath was introduced. When -fgo-pkgpath is specified,
8372 // we use it to make a unique reflection string, so that the
8373 // type canonicalization in the reflect package will work. In
8374 // order to be compatible with the gc compiler, we put tabs into
8375 // the package path, so that the reflect methods can discard it.
8376 const Package
* package
= this->named_object_
->package();
8377 if (gogo
->pkgpath_from_option())
8379 ret
->push_back('\t');
8380 ret
->append(package
!= NULL
8381 ? package
->pkgpath_symbol()
8382 : gogo
->pkgpath_symbol());
8383 ret
->push_back('\t');
8385 ret
->append(package
!= NULL
8386 ? package
->package_name()
8387 : gogo
->package_name());
8388 ret
->push_back('.');
8390 if (this->in_function_
!= NULL
)
8392 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
8393 ret
->push_back('$');
8394 if (this->in_function_index_
> 0)
8397 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
8399 ret
->push_back('$');
8402 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
8405 // Get the mangled name.
8408 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8410 if (this->is_alias())
8412 this->append_mangled_name(this->type_
, gogo
, ret
);
8415 Named_object
* no
= this->named_object_
;
8417 if (this->is_builtin())
8418 go_assert(this->in_function_
== NULL
);
8421 const std::string
& pkgpath(no
->package() == NULL
8422 ? gogo
->pkgpath_symbol()
8423 : no
->package()->pkgpath_symbol());
8425 name
.append(1, '.');
8426 if (this->in_function_
!= NULL
)
8428 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
8429 name
.append(1, '$');
8430 if (this->in_function_index_
> 0)
8433 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
8435 name
.append(1, '$');
8439 name
.append(Gogo::unpack_hidden_name(no
->name()));
8441 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
8446 // Export the type. This is called to export a global type.
8449 Named_type::export_named_type(Export
* exp
, const std::string
&) const
8451 // We don't need to write the name of the type here, because it will
8452 // be written by Export::write_type anyhow.
8453 exp
->write_c_string("type ");
8454 exp
->write_type(this);
8455 exp
->write_c_string(";\n");
8458 // Import a named type.
8461 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
8463 imp
->require_c_string("type ");
8464 Type
*type
= imp
->read_type();
8465 *ptype
= type
->named_type();
8466 go_assert(*ptype
!= NULL
);
8467 imp
->require_c_string(";\n");
8470 // Export the type when it is referenced by another type. In this
8471 // case Export::export_type will already have issued the name.
8474 Named_type::do_export(Export
* exp
) const
8476 exp
->write_type(this->type_
);
8478 // To save space, we only export the methods directly attached to
8480 Bindings
* methods
= this->local_methods_
;
8481 if (methods
== NULL
)
8484 exp
->write_c_string("\n");
8485 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
8486 p
!= methods
->end_definitions();
8489 exp
->write_c_string(" ");
8490 (*p
)->export_named_object(exp
);
8493 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
8494 p
!= methods
->end_declarations();
8497 if (p
->second
->is_function_declaration())
8499 exp
->write_c_string(" ");
8500 p
->second
->export_named_object(exp
);
8505 // Make a named type.
8508 Type::make_named_type(Named_object
* named_object
, Type
* type
,
8511 return new Named_type(named_object
, type
, location
);
8514 // Finalize the methods for TYPE. It will be a named type or a struct
8515 // type. This sets *ALL_METHODS to the list of methods, and builds
8516 // all required stubs.
8519 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, Location location
,
8520 Methods
** all_methods
)
8522 *all_methods
= NULL
;
8523 Types_seen types_seen
;
8524 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
8526 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
8529 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8530 // build up the struct field indexes as we go. DEPTH is the depth of
8531 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8532 // adding these methods for an anonymous field with pointer type.
8533 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8534 // calls the real method. TYPES_SEEN is used to avoid infinite
8538 Type::add_methods_for_type(const Type
* type
,
8539 const Method::Field_indexes
* field_indexes
,
8541 bool is_embedded_pointer
,
8542 bool needs_stub_method
,
8543 Types_seen
* types_seen
,
8546 // Pointer types may not have methods.
8547 if (type
->points_to() != NULL
)
8550 const Named_type
* nt
= type
->named_type();
8553 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
8559 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
8560 is_embedded_pointer
, needs_stub_method
,
8563 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
8564 is_embedded_pointer
, needs_stub_method
,
8565 types_seen
, methods
);
8567 // If we are called with depth > 0, then we are looking at an
8568 // anonymous field of a struct. If such a field has interface type,
8569 // then we need to add the interface methods. We don't want to add
8570 // them when depth == 0, because we will already handle them
8571 // following the usual rules for an interface type.
8573 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
8576 // Add the local methods for the named type NT to *METHODS. The
8577 // parameters are as for add_methods_to_type.
8580 Type::add_local_methods_for_type(const Named_type
* nt
,
8581 const Method::Field_indexes
* field_indexes
,
8583 bool is_embedded_pointer
,
8584 bool needs_stub_method
,
8587 const Bindings
* local_methods
= nt
->local_methods();
8588 if (local_methods
== NULL
)
8591 if (*methods
== NULL
)
8592 *methods
= new Methods();
8594 for (Bindings::const_declarations_iterator p
=
8595 local_methods
->begin_declarations();
8596 p
!= local_methods
->end_declarations();
8599 Named_object
* no
= p
->second
;
8600 bool is_value_method
= (is_embedded_pointer
8601 || !Type::method_expects_pointer(no
));
8602 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
8604 || (depth
> 0 && is_value_method
)));
8605 if (!(*methods
)->insert(no
->name(), m
))
8610 // Add the embedded methods for TYPE to *METHODS. These are the
8611 // methods attached to anonymous fields. The parameters are as for
8612 // add_methods_to_type.
8615 Type::add_embedded_methods_for_type(const Type
* type
,
8616 const Method::Field_indexes
* field_indexes
,
8618 bool is_embedded_pointer
,
8619 bool needs_stub_method
,
8620 Types_seen
* types_seen
,
8623 // Look for anonymous fields in TYPE. TYPE has fields if it is a
8625 const Struct_type
* st
= type
->struct_type();
8629 const Struct_field_list
* fields
= st
->fields();
8634 for (Struct_field_list::const_iterator pf
= fields
->begin();
8635 pf
!= fields
->end();
8638 if (!pf
->is_anonymous())
8641 Type
* ftype
= pf
->type();
8642 bool is_pointer
= false;
8643 if (ftype
->points_to() != NULL
)
8645 ftype
= ftype
->points_to();
8648 Named_type
* fnt
= ftype
->named_type();
8651 // This is an error, but it will be diagnosed elsewhere.
8655 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
8656 sub_field_indexes
->next
= field_indexes
;
8657 sub_field_indexes
->field_index
= i
;
8659 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
8660 (is_embedded_pointer
|| is_pointer
),
8669 // If TYPE is an interface type, then add its method to *METHODS.
8670 // This is for interface methods attached to an anonymous field. The
8671 // parameters are as for add_methods_for_type.
8674 Type::add_interface_methods_for_type(const Type
* type
,
8675 const Method::Field_indexes
* field_indexes
,
8679 const Interface_type
* it
= type
->interface_type();
8683 const Typed_identifier_list
* imethods
= it
->methods();
8684 if (imethods
== NULL
)
8687 if (*methods
== NULL
)
8688 *methods
= new Methods();
8690 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
8691 pm
!= imethods
->end();
8694 Function_type
* fntype
= pm
->type()->function_type();
8697 // This is an error, but it should be reported elsewhere
8698 // when we look at the methods for IT.
8701 go_assert(!fntype
->is_method());
8702 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
8703 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
8704 field_indexes
, depth
);
8705 if (!(*methods
)->insert(pm
->name(), m
))
8710 // Build stub methods for TYPE as needed. METHODS is the set of
8711 // methods for the type. A stub method may be needed when a type
8712 // inherits a method from an anonymous field. When we need the
8713 // address of the method, as in a type descriptor, we need to build a
8714 // little stub which does the required field dereferences and jumps to
8715 // the real method. LOCATION is the location of the type definition.
8718 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
8721 if (methods
== NULL
)
8723 for (Methods::const_iterator p
= methods
->begin();
8724 p
!= methods
->end();
8727 Method
* m
= p
->second
;
8728 if (m
->is_ambiguous() || !m
->needs_stub_method())
8731 const std::string
& name(p
->first
);
8733 // Build a stub method.
8735 const Function_type
* fntype
= m
->type();
8737 static unsigned int counter
;
8739 snprintf(buf
, sizeof buf
, "$this%u", counter
);
8742 Type
* receiver_type
= const_cast<Type
*>(type
);
8743 if (!m
->is_value_method())
8744 receiver_type
= Type::make_pointer_type(receiver_type
);
8745 Location receiver_location
= m
->receiver_location();
8746 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
8749 const Typed_identifier_list
* fnparams
= fntype
->parameters();
8750 Typed_identifier_list
* stub_params
;
8751 if (fnparams
== NULL
|| fnparams
->empty())
8755 // We give each stub parameter a unique name.
8756 stub_params
= new Typed_identifier_list();
8757 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
8758 pp
!= fnparams
->end();
8762 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
8763 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
8769 const Typed_identifier_list
* fnresults
= fntype
->results();
8770 Typed_identifier_list
* stub_results
;
8771 if (fnresults
== NULL
|| fnresults
->empty())
8772 stub_results
= NULL
;
8775 // We create the result parameters without any names, since
8776 // we won't refer to them.
8777 stub_results
= new Typed_identifier_list();
8778 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
8779 pr
!= fnresults
->end();
8781 stub_results
->push_back(Typed_identifier("", pr
->type(),
8785 Function_type
* stub_type
= Type::make_function_type(receiver
,
8788 fntype
->location());
8789 if (fntype
->is_varargs())
8790 stub_type
->set_is_varargs();
8792 // We only create the function in the package which creates the
8794 const Package
* package
;
8795 if (type
->named_type() == NULL
)
8798 package
= type
->named_type()->named_object()->package();
8800 if (package
!= NULL
)
8801 stub
= Named_object::make_function_declaration(name
, package
,
8802 stub_type
, location
);
8805 stub
= gogo
->start_function(name
, stub_type
, false,
8806 fntype
->location());
8807 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
8808 fntype
->is_varargs(), location
);
8809 gogo
->finish_function(fntype
->location());
8812 m
->set_stub_object(stub
);
8816 // Build a stub method which adjusts the receiver as required to call
8817 // METHOD. RECEIVER_NAME is the name we used for the receiver.
8818 // PARAMS is the list of function parameters.
8821 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
8822 const char* receiver_name
,
8823 const Typed_identifier_list
* params
,
8827 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
8828 go_assert(receiver_object
!= NULL
);
8830 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
8831 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
8832 if (expr
->type()->points_to() == NULL
)
8833 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
8835 Expression_list
* arguments
;
8836 if (params
== NULL
|| params
->empty())
8840 arguments
= new Expression_list();
8841 for (Typed_identifier_list::const_iterator p
= params
->begin();
8845 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
8846 go_assert(param
!= NULL
);
8847 Expression
* param_ref
= Expression::make_var_reference(param
,
8849 arguments
->push_back(param_ref
);
8853 Expression
* func
= method
->bind_method(expr
, location
);
8854 go_assert(func
!= NULL
);
8855 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
8857 call
->set_hidden_fields_are_ok();
8858 size_t count
= call
->result_count();
8860 gogo
->add_statement(Statement::make_statement(call
, true));
8863 Expression_list
* retvals
= new Expression_list();
8865 retvals
->push_back(call
);
8868 for (size_t i
= 0; i
< count
; ++i
)
8869 retvals
->push_back(Expression::make_call_result(call
, i
));
8871 Return_statement
* retstat
= Statement::make_return_statement(retvals
,
8874 // We can return values with hidden fields from a stub. This is
8875 // necessary if the method is itself hidden.
8876 retstat
->set_hidden_fields_are_ok();
8878 gogo
->add_statement(retstat
);
8882 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
8883 // in reverse order.
8886 Type::apply_field_indexes(Expression
* expr
,
8887 const Method::Field_indexes
* field_indexes
,
8890 if (field_indexes
== NULL
)
8892 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
8893 Struct_type
* stype
= expr
->type()->deref()->struct_type();
8894 go_assert(stype
!= NULL
8895 && field_indexes
->field_index
< stype
->field_count());
8896 if (expr
->type()->struct_type() == NULL
)
8898 go_assert(expr
->type()->points_to() != NULL
);
8899 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
8900 go_assert(expr
->type()->struct_type() == stype
);
8902 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
8906 // Return whether NO is a method for which the receiver is a pointer.
8909 Type::method_expects_pointer(const Named_object
* no
)
8911 const Function_type
*fntype
;
8912 if (no
->is_function())
8913 fntype
= no
->func_value()->type();
8914 else if (no
->is_function_declaration())
8915 fntype
= no
->func_declaration_value()->type();
8918 return fntype
->receiver()->type()->points_to() != NULL
;
8921 // Given a set of methods for a type, METHODS, return the method NAME,
8922 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
8923 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
8924 // but is ambiguous (and return NULL).
8927 Type::method_function(const Methods
* methods
, const std::string
& name
,
8930 if (is_ambiguous
!= NULL
)
8931 *is_ambiguous
= false;
8932 if (methods
== NULL
)
8934 Methods::const_iterator p
= methods
->find(name
);
8935 if (p
== methods
->end())
8937 Method
* m
= p
->second
;
8938 if (m
->is_ambiguous())
8940 if (is_ambiguous
!= NULL
)
8941 *is_ambiguous
= true;
8947 // Look for field or method NAME for TYPE. Return an Expression for
8948 // the field or method bound to EXPR. If there is no such field or
8949 // method, give an appropriate error and return an error expression.
8952 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
8953 const std::string
& name
,
8956 if (type
->deref()->is_error_type())
8957 return Expression::make_error(location
);
8959 const Named_type
* nt
= type
->deref()->named_type();
8960 const Struct_type
* st
= type
->deref()->struct_type();
8961 const Interface_type
* it
= type
->interface_type();
8963 // If this is a pointer to a pointer, then it is possible that the
8964 // pointed-to type has methods.
8965 bool dereferenced
= false;
8969 && type
->points_to() != NULL
8970 && type
->points_to()->points_to() != NULL
)
8972 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
8973 type
= type
->points_to();
8974 if (type
->deref()->is_error_type())
8975 return Expression::make_error(location
);
8976 nt
= type
->points_to()->named_type();
8977 st
= type
->points_to()->struct_type();
8978 dereferenced
= true;
8981 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
8982 || expr
->is_addressable());
8983 std::vector
<const Named_type
*> seen
;
8984 bool is_method
= false;
8985 bool found_pointer_method
= false;
8988 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
8989 &seen
, NULL
, &is_method
,
8990 &found_pointer_method
, &ambig1
, &ambig2
))
8995 go_assert(st
!= NULL
);
8996 if (type
->struct_type() == NULL
)
8998 go_assert(type
->points_to() != NULL
);
8999 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
9001 go_assert(expr
->type()->struct_type() == st
);
9003 ret
= st
->field_reference(expr
, name
, location
);
9005 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
9006 ret
= Expression::make_interface_field_reference(expr
, name
,
9012 m
= nt
->method_function(name
, NULL
);
9013 else if (st
!= NULL
)
9014 m
= st
->method_function(name
, NULL
);
9017 go_assert(m
!= NULL
);
9018 if (dereferenced
&& m
->is_value_method())
9021 "calling value method requires explicit dereference");
9022 return Expression::make_error(location
);
9024 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
9025 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
9026 ret
= m
->bind_method(expr
, location
);
9028 go_assert(ret
!= NULL
);
9033 if (!ambig1
.empty())
9034 error_at(location
, "%qs is ambiguous via %qs and %qs",
9035 Gogo::message_name(name
).c_str(), ambig1
.c_str(),
9037 else if (found_pointer_method
)
9038 error_at(location
, "method requires a pointer receiver");
9039 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
9041 ("reference to field %qs in object which "
9042 "has no fields or methods"),
9043 Gogo::message_name(name
).c_str());
9047 if (!Gogo::is_hidden_name(name
))
9048 is_unexported
= false;
9051 std::string unpacked
= Gogo::unpack_hidden_name(name
);
9053 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
9058 error_at(location
, "reference to unexported field or method %qs",
9059 Gogo::message_name(name
).c_str());
9061 error_at(location
, "reference to undefined field or method %qs",
9062 Gogo::message_name(name
).c_str());
9064 return Expression::make_error(location
);
9068 // Look in TYPE for a field or method named NAME, return true if one
9069 // is found. This looks through embedded anonymous fields and handles
9070 // ambiguity. If a method is found, sets *IS_METHOD to true;
9071 // otherwise, if a field is found, set it to false. If
9072 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9073 // whose address can not be taken. SEEN is used to avoid infinite
9074 // recursion on invalid types.
9076 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9077 // method we couldn't use because it requires a pointer. LEVEL is
9078 // used for recursive calls, and can be NULL for a non-recursive call.
9079 // When this function returns false because it finds that the name is
9080 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9081 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9082 // will be unchanged.
9084 // This function just returns whether or not there is a field or
9085 // method, and whether it is a field or method. It doesn't build an
9086 // expression to refer to it. If it is a method, we then look in the
9087 // list of all methods for the type. If it is a field, the search has
9088 // to be done again, looking only for fields, and building up the
9089 // expression as we go.
9092 Type::find_field_or_method(const Type
* type
,
9093 const std::string
& name
,
9094 bool receiver_can_be_pointer
,
9095 std::vector
<const Named_type
*>* seen
,
9098 bool* found_pointer_method
,
9099 std::string
* ambig1
,
9100 std::string
* ambig2
)
9102 // Named types can have locally defined methods.
9103 const Named_type
* nt
= type
->named_type();
9104 if (nt
== NULL
&& type
->points_to() != NULL
)
9105 nt
= type
->points_to()->named_type();
9108 Named_object
* no
= nt
->find_local_method(name
);
9111 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
9117 // Record that we have found a pointer method in order to
9118 // give a better error message if we don't find anything
9120 *found_pointer_method
= true;
9123 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9129 // We've already seen this type when searching for methods.
9135 // Interface types can have methods.
9136 const Interface_type
* it
= type
->interface_type();
9137 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
9143 // Struct types can have fields. They can also inherit fields and
9144 // methods from anonymous fields.
9145 const Struct_type
* st
= type
->deref()->struct_type();
9148 const Struct_field_list
* fields
= st
->fields();
9153 seen
->push_back(nt
);
9155 int found_level
= 0;
9156 bool found_is_method
= false;
9157 std::string found_ambig1
;
9158 std::string found_ambig2
;
9159 const Struct_field
* found_parent
= NULL
;
9160 for (Struct_field_list::const_iterator pf
= fields
->begin();
9161 pf
!= fields
->end();
9164 if (pf
->is_field_name(name
))
9172 if (!pf
->is_anonymous())
9175 if (pf
->type()->deref()->is_error_type()
9176 || pf
->type()->deref()->is_undefined())
9179 Named_type
* fnt
= pf
->type()->named_type();
9181 fnt
= pf
->type()->deref()->named_type();
9182 go_assert(fnt
!= NULL
);
9184 int sublevel
= level
== NULL
? 1 : *level
+ 1;
9186 std::string subambig1
;
9187 std::string subambig2
;
9188 bool subfound
= Type::find_field_or_method(fnt
,
9190 receiver_can_be_pointer
,
9194 found_pointer_method
,
9199 if (!subambig1
.empty())
9201 // The name was found via this field, but is ambiguous.
9202 // if the ambiguity is lower or at the same level as
9203 // anything else we have already found, then we want to
9204 // pass the ambiguity back to the caller.
9205 if (found_level
== 0 || sublevel
<= found_level
)
9207 found_ambig1
= (Gogo::message_name(pf
->field_name())
9209 found_ambig2
= (Gogo::message_name(pf
->field_name())
9211 found_level
= sublevel
;
9217 // The name was found via this field. Use the level to see
9218 // if we want to use this one, or whether it introduces an
9220 if (found_level
== 0 || sublevel
< found_level
)
9222 found_level
= sublevel
;
9223 found_is_method
= sub_is_method
;
9224 found_ambig1
.clear();
9225 found_ambig2
.clear();
9226 found_parent
= &*pf
;
9228 else if (sublevel
> found_level
)
9230 else if (found_ambig1
.empty())
9232 // We found an ambiguity.
9233 go_assert(found_parent
!= NULL
);
9234 found_ambig1
= Gogo::message_name(found_parent
->field_name());
9235 found_ambig2
= Gogo::message_name(pf
->field_name());
9239 // We found an ambiguity, but we already know of one.
9240 // Just report the earlier one.
9245 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
9246 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
9247 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
9248 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
9253 if (found_level
== 0)
9255 else if (!found_ambig1
.empty())
9257 go_assert(!found_ambig1
.empty());
9258 ambig1
->assign(found_ambig1
);
9259 ambig2
->assign(found_ambig2
);
9261 *level
= found_level
;
9267 *level
= found_level
;
9268 *is_method
= found_is_method
;
9273 // Return whether NAME is an unexported field or method for TYPE.
9276 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
9277 const std::string
& name
,
9278 std::vector
<const Named_type
*>* seen
)
9280 const Named_type
* nt
= type
->named_type();
9282 nt
= type
->deref()->named_type();
9285 if (nt
->is_unexported_local_method(gogo
, name
))
9288 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
9294 // We've already seen this type.
9300 const Interface_type
* it
= type
->interface_type();
9301 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
9304 type
= type
->deref();
9306 const Struct_type
* st
= type
->struct_type();
9307 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
9313 const Struct_field_list
* fields
= st
->fields();
9318 seen
->push_back(nt
);
9320 for (Struct_field_list::const_iterator pf
= fields
->begin();
9321 pf
!= fields
->end();
9324 if (pf
->is_anonymous()
9325 && !pf
->type()->deref()->is_error_type()
9326 && !pf
->type()->deref()->is_undefined())
9328 Named_type
* subtype
= pf
->type()->named_type();
9329 if (subtype
== NULL
)
9330 subtype
= pf
->type()->deref()->named_type();
9331 if (subtype
== NULL
)
9333 // This is an error, but it will be diagnosed elsewhere.
9336 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
9351 // Class Forward_declaration.
9353 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
9354 : Type(TYPE_FORWARD
),
9355 named_object_(named_object
->resolve()), warned_(false)
9357 go_assert(this->named_object_
->is_unknown()
9358 || this->named_object_
->is_type_declaration());
9361 // Return the named object.
9364 Forward_declaration_type::named_object()
9366 return this->named_object_
->resolve();
9370 Forward_declaration_type::named_object() const
9372 return this->named_object_
->resolve();
9375 // Return the name of the forward declared type.
9378 Forward_declaration_type::name() const
9380 return this->named_object()->name();
9383 // Warn about a use of a type which has been declared but not defined.
9386 Forward_declaration_type::warn() const
9388 Named_object
* no
= this->named_object_
->resolve();
9389 if (no
->is_unknown())
9391 // The name was not defined anywhere.
9394 error_at(this->named_object_
->location(),
9395 "use of undefined type %qs",
9396 no
->message_name().c_str());
9397 this->warned_
= true;
9400 else if (no
->is_type_declaration())
9402 // The name was seen as a type, but the type was never defined.
9403 if (no
->type_declaration_value()->using_type())
9405 error_at(this->named_object_
->location(),
9406 "use of undefined type %qs",
9407 no
->message_name().c_str());
9408 this->warned_
= true;
9413 // The name was defined, but not as a type.
9416 error_at(this->named_object_
->location(), "expected type");
9417 this->warned_
= true;
9422 // Get the base type of a declaration. This gives an error if the
9423 // type has not yet been defined.
9426 Forward_declaration_type::real_type()
9428 if (this->is_defined())
9429 return this->named_object()->type_value();
9433 return Type::make_error_type();
9438 Forward_declaration_type::real_type() const
9440 if (this->is_defined())
9441 return this->named_object()->type_value();
9445 return Type::make_error_type();
9449 // Return whether the base type is defined.
9452 Forward_declaration_type::is_defined() const
9454 return this->named_object()->is_type();
9457 // Add a method. This is used when methods are defined before the
9461 Forward_declaration_type::add_method(const std::string
& name
,
9464 Named_object
* no
= this->named_object();
9465 if (no
->is_unknown())
9466 no
->declare_as_type();
9467 return no
->type_declaration_value()->add_method(name
, function
);
9470 // Add a method declaration. This is used when methods are declared
9474 Forward_declaration_type::add_method_declaration(const std::string
& name
,
9476 Function_type
* type
,
9479 Named_object
* no
= this->named_object();
9480 if (no
->is_unknown())
9481 no
->declare_as_type();
9482 Type_declaration
* td
= no
->type_declaration_value();
9483 return td
->add_method_declaration(name
, package
, type
, location
);
9489 Forward_declaration_type::do_traverse(Traverse
* traverse
)
9491 if (this->is_defined()
9492 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
9493 return TRAVERSE_EXIT
;
9494 return TRAVERSE_CONTINUE
;
9497 // Get the backend representation for the type.
9500 Forward_declaration_type::do_get_backend(Gogo
* gogo
)
9502 if (this->is_defined())
9503 return Type::get_named_base_btype(gogo
, this->real_type());
9506 return gogo
->backend()->error_type();
9508 // We represent an undefined type as a struct with no fields. That
9509 // should work fine for the backend, since the same case can arise
9511 std::vector
<Backend::Btyped_identifier
> fields
;
9512 Btype
* bt
= gogo
->backend()->struct_type(fields
);
9513 return gogo
->backend()->named_type(this->name(), bt
,
9514 this->named_object()->location());
9517 // Build a type descriptor for a forwarded type.
9520 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
9522 Location ploc
= Linemap::predeclared_location();
9523 if (!this->is_defined())
9524 return Expression::make_error(ploc
);
9527 Type
* t
= this->real_type();
9529 return this->named_type_descriptor(gogo
, t
, name
);
9531 return Expression::make_type_descriptor(t
, ploc
);
9535 // The reflection string.
9538 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
9540 this->append_reflection(this->real_type(), gogo
, ret
);
9543 // The mangled name.
9546 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
9548 if (this->is_defined())
9549 this->append_mangled_name(this->real_type(), gogo
, ret
);
9552 const Named_object
* no
= this->named_object();
9554 if (no
->package() == NULL
)
9555 name
= gogo
->pkgpath_symbol();
9557 name
= no
->package()->pkgpath_symbol();
9559 name
+= Gogo::unpack_hidden_name(no
->name());
9561 snprintf(buf
, sizeof buf
, "N%u_",
9562 static_cast<unsigned int>(name
.length()));
9568 // Export a forward declaration. This can happen when a defined type
9569 // refers to a type which is only declared (and is presumably defined
9570 // in some other file in the same package).
9573 Forward_declaration_type::do_export(Export
*) const
9575 // If there is a base type, that should be exported instead of this.
9576 go_assert(!this->is_defined());
9578 // We don't output anything.
9581 // Make a forward declaration.
9584 Type::make_forward_declaration(Named_object
* named_object
)
9586 return new Forward_declaration_type(named_object
);
9589 // Class Typed_identifier_list.
9591 // Sort the entries by name.
9593 struct Typed_identifier_list_sort
9597 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
9598 { return t1
.name() < t2
.name(); }
9602 Typed_identifier_list::sort_by_name()
9604 std::sort(this->entries_
.begin(), this->entries_
.end(),
9605 Typed_identifier_list_sort());
9611 Typed_identifier_list::traverse(Traverse
* traverse
)
9613 for (Typed_identifier_list::const_iterator p
= this->begin();
9617 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
9618 return TRAVERSE_EXIT
;
9620 return TRAVERSE_CONTINUE
;
9625 Typed_identifier_list
*
9626 Typed_identifier_list::copy() const
9628 Typed_identifier_list
* ret
= new Typed_identifier_list();
9629 for (Typed_identifier_list::const_iterator p
= this->begin();
9632 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));