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.
11 #ifndef ENABLE_BUILD_WITH_CXX
23 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
31 #include "statements.h"
38 Type::Type(Type_classification classification
)
39 : classification_(classification
), tree_(NULL_TREE
),
40 type_descriptor_decl_(NULL_TREE
)
48 // Get the base type for a type--skip names and forward declarations.
53 switch (this->classification_
)
56 return this->named_type()->named_base();
58 return this->forward_declaration_type()->real_type()->base();
67 switch (this->classification_
)
70 return this->named_type()->named_base();
72 return this->forward_declaration_type()->real_type()->base();
78 // Skip defined forward declarations.
84 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
85 while (ftype
!= NULL
&& ftype
->is_defined())
87 t
= ftype
->real_type();
88 ftype
= t
->forward_declaration_type();
94 Type::forwarded() const
97 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
98 while (ftype
!= NULL
&& ftype
->is_defined())
100 t
= ftype
->real_type();
101 ftype
= t
->forward_declaration_type();
106 // If this is a named type, return it. Otherwise, return NULL.
111 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
115 Type::named_type() const
117 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
120 // Return true if this type is not defined.
123 Type::is_undefined() const
125 return this->forwarded()->forward_declaration_type() != NULL
;
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
132 Type::is_basic_type() const
134 switch (this->classification_
)
157 return this->base()->is_basic_type();
164 // Return true if this is an abstract type.
167 Type::is_abstract() const
169 switch (this->classification())
172 return this->integer_type()->is_abstract();
174 return this->float_type()->is_abstract();
176 return this->complex_type()->is_abstract();
178 return this->is_abstract_string_type();
180 return this->is_abstract_boolean_type();
186 // Return a non-abstract version of an abstract type.
189 Type::make_non_abstract_type()
191 gcc_assert(this->is_abstract());
192 switch (this->classification())
195 return Type::lookup_integer_type("int");
197 return Type::lookup_float_type("float");
199 return Type::lookup_complex_type("complex");
201 return Type::lookup_string_type();
203 return Type::lookup_bool_type();
209 // Return true if this is an error type. Don't give an error if we
210 // try to dereference an undefined forwarding type, as this is called
211 // in the parser when the type may legitimately be undefined.
214 Type::is_error_type() const
216 const Type
* t
= this->forwarded();
217 // Note that we return false for an undefined forward type.
218 switch (t
->classification_
)
223 return t
->named_type()->is_named_error_type();
229 // If this is a pointer type, return the type to which it points.
230 // Otherwise, return NULL.
233 Type::points_to() const
235 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
237 return ptype
== NULL
? NULL
: ptype
->points_to();
240 // Return whether this is an open array type.
243 Type::is_open_array_type() const
245 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
248 // Return whether this is the predeclared constant nil being used as a
252 Type::is_nil_constant_as_type() const
254 const Type
* t
= this->forwarded();
255 if (t
->forward_declaration_type() != NULL
)
257 const Named_object
* no
= t
->forward_declaration_type()->named_object();
258 if (no
->is_unknown())
259 no
= no
->unknown_value()->real_named_object();
262 && no
->const_value()->expr()->is_nil_expression())
271 Type::traverse(Type
* type
, Traverse
* traverse
)
273 gcc_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
274 || (traverse
->traverse_mask()
275 & Traverse::traverse_expressions
) != 0);
276 if (traverse
->remember_type(type
))
278 // We have already traversed this type.
279 return TRAVERSE_CONTINUE
;
281 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
283 int t
= traverse
->type(type
);
284 if (t
== TRAVERSE_EXIT
)
285 return TRAVERSE_EXIT
;
286 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
287 return TRAVERSE_CONTINUE
;
289 // An array type has an expression which we need to traverse if
290 // traverse_expressions is set.
291 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
292 return TRAVERSE_EXIT
;
293 return TRAVERSE_CONTINUE
;
296 // Default implementation for do_traverse for child class.
299 Type::do_traverse(Traverse
*)
301 return TRAVERSE_CONTINUE
;
304 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
305 // then return true for all erroneous types; this is used to avoid
306 // cascading errors. If REASON is not NULL, optionally set *REASON to
307 // the reason the types are not identical.
310 Type::are_identical(const Type
* t1
, const Type
* t2
, bool errors_are_identical
,
313 if (t1
== NULL
|| t2
== NULL
)
315 // Something is wrong.
316 return errors_are_identical
? true : t1
== t2
;
319 // Skip defined forward declarations.
320 t1
= t1
->forwarded();
321 t2
= t2
->forwarded();
326 // An undefined forward declaration is an error.
327 if (t1
->forward_declaration_type() != NULL
328 || t2
->forward_declaration_type() != NULL
)
329 return errors_are_identical
;
331 // Avoid cascading errors with error types.
332 if (t1
->is_error_type() || t2
->is_error_type())
334 if (errors_are_identical
)
336 return t1
->is_error_type() && t2
->is_error_type();
339 // Get a good reason for the sink type. Note that the sink type on
340 // the left hand side of an assignment is handled in are_assignable.
341 if (t1
->is_sink_type() || t2
->is_sink_type())
344 *reason
= "invalid use of _";
348 // A named type is only identical to itself.
349 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
352 // Check type shapes.
353 if (t1
->classification() != t2
->classification())
356 switch (t1
->classification())
362 // These types are always identical.
366 return t1
->integer_type()->is_identical(t2
->integer_type());
369 return t1
->float_type()->is_identical(t2
->float_type());
372 return t1
->complex_type()->is_identical(t2
->complex_type());
375 return t1
->function_type()->is_identical(t2
->function_type(),
377 errors_are_identical
,
381 return Type::are_identical(t1
->points_to(), t2
->points_to(),
382 errors_are_identical
, reason
);
385 return t1
->struct_type()->is_identical(t2
->struct_type(),
386 errors_are_identical
);
389 return t1
->array_type()->is_identical(t2
->array_type(),
390 errors_are_identical
);
393 return t1
->map_type()->is_identical(t2
->map_type(),
394 errors_are_identical
);
397 return t1
->channel_type()->is_identical(t2
->channel_type(),
398 errors_are_identical
);
401 return t1
->interface_type()->is_identical(t2
->interface_type(),
402 errors_are_identical
);
404 case TYPE_CALL_MULTIPLE_RESULT
:
406 *reason
= "invalid use of multiple value function call";
414 // Return true if it's OK to have a binary operation with types LHS
415 // and RHS. This is not used for shifts or comparisons.
418 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
420 if (Type::are_identical(lhs
, rhs
, true, NULL
))
423 // A constant of abstract bool type may be mixed with any bool type.
424 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
425 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
428 // A constant of abstract string type may be mixed with any string
430 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
431 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
437 // A constant of abstract integer, float, or complex type may be
438 // mixed with an integer, float, or complex type.
439 if ((rhs
->is_abstract()
440 && (rhs
->integer_type() != NULL
441 || rhs
->float_type() != NULL
442 || rhs
->complex_type() != NULL
)
443 && (lhs
->integer_type() != NULL
444 || lhs
->float_type() != NULL
445 || lhs
->complex_type() != NULL
))
446 || (lhs
->is_abstract()
447 && (lhs
->integer_type() != NULL
448 || lhs
->float_type() != NULL
449 || lhs
->complex_type() != NULL
)
450 && (rhs
->integer_type() != NULL
451 || rhs
->float_type() != NULL
452 || rhs
->complex_type() != NULL
)))
455 // The nil type may be compared to a pointer, an interface type, a
456 // slice type, a channel type, a map type, or a function type.
457 if (lhs
->is_nil_type()
458 && (rhs
->points_to() != NULL
459 || rhs
->interface_type() != NULL
460 || rhs
->is_open_array_type()
461 || rhs
->map_type() != NULL
462 || rhs
->channel_type() != NULL
463 || rhs
->function_type() != NULL
))
465 if (rhs
->is_nil_type()
466 && (lhs
->points_to() != NULL
467 || lhs
->interface_type() != NULL
468 || lhs
->is_open_array_type()
469 || lhs
->map_type() != NULL
470 || lhs
->channel_type() != NULL
471 || lhs
->function_type() != NULL
))
477 // Return true if a value with type RHS may be assigned to a variable
478 // with type LHS. If REASON is not NULL, set *REASON to the reason
479 // the types are not assignable.
482 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
484 // Do some checks first. Make sure the types are defined.
485 if (lhs
!= NULL
&& lhs
->forwarded()->forward_declaration_type() == NULL
)
487 // Any value may be assigned to the blank identifier.
488 if (lhs
->is_sink_type())
491 // All fields of a struct must be exported, or the assignment
492 // must be in the same package.
493 if (rhs
!= NULL
&& rhs
->forwarded()->forward_declaration_type() == NULL
)
495 if (lhs
->has_hidden_fields(NULL
, reason
)
496 || rhs
->has_hidden_fields(NULL
, reason
))
501 // Identical types are assignable.
502 if (Type::are_identical(lhs
, rhs
, true, reason
))
505 // The types are assignable if they have identical underlying types
506 // and either LHS or RHS is not a named type.
507 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
508 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
509 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
512 // The types are assignable if LHS is an interface type and RHS
513 // implements the required methods.
514 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
515 if (lhs_interface_type
!= NULL
)
517 if (lhs_interface_type
->implements_interface(rhs
, reason
))
519 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
520 if (rhs_interface_type
!= NULL
521 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
526 // The type are assignable if RHS is a bidirectional channel type,
527 // LHS is a channel type, they have identical element types, and
528 // either LHS or RHS is not a named type.
529 if (lhs
->channel_type() != NULL
530 && rhs
->channel_type() != NULL
531 && rhs
->channel_type()->may_send()
532 && rhs
->channel_type()->may_receive()
533 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
534 && Type::are_identical(lhs
->channel_type()->element_type(),
535 rhs
->channel_type()->element_type(),
540 // The nil type may be assigned to a pointer, function, slice, map,
541 // channel, or interface type.
542 if (rhs
->is_nil_type()
543 && (lhs
->points_to() != NULL
544 || lhs
->function_type() != NULL
545 || lhs
->is_open_array_type()
546 || lhs
->map_type() != NULL
547 || lhs
->channel_type() != NULL
548 || lhs
->interface_type() != NULL
))
551 // An untyped numeric constant may be assigned to a numeric type if
552 // it is representable in that type.
553 if ((rhs
->is_abstract()
554 && (rhs
->integer_type() != NULL
555 || rhs
->float_type() != NULL
556 || rhs
->complex_type() != NULL
))
557 && (lhs
->integer_type() != NULL
558 || lhs
->float_type() != NULL
559 || lhs
->complex_type() != NULL
))
562 // Give some better error messages.
563 if (reason
!= NULL
&& reason
->empty())
565 if (rhs
->interface_type() != NULL
)
566 reason
->assign(_("need explicit conversion"));
567 else if (rhs
->is_call_multiple_result_type())
568 reason
->assign(_("multiple value function call in "
569 "single value context"));
570 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
572 size_t len
= (lhs
->named_type()->name().length()
573 + rhs
->named_type()->name().length()
575 char* buf
= new char[len
];
576 snprintf(buf
, len
, _("cannot use type %s as type %s"),
577 rhs
->named_type()->message_name().c_str(),
578 lhs
->named_type()->message_name().c_str());
587 // Return true if a value with type RHS may be converted to type LHS.
588 // If REASON is not NULL, set *REASON to the reason the types are not
592 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
594 // The types are convertible if they are assignable.
595 if (Type::are_assignable(lhs
, rhs
, reason
))
598 // The types are convertible if they have identical underlying
600 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
601 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
604 // The types are convertible if they are both unnamed pointer types
605 // and their pointer base types have identical underlying types.
606 if (lhs
->named_type() == NULL
607 && rhs
->named_type() == NULL
608 && lhs
->points_to() != NULL
609 && rhs
->points_to() != NULL
610 && (lhs
->points_to()->named_type() != NULL
611 || rhs
->points_to()->named_type() != NULL
)
612 && Type::are_identical(lhs
->points_to()->base(),
613 rhs
->points_to()->base(),
618 // Integer and floating point types are convertible to each other.
619 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
620 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
623 // Complex types are convertible to each other.
624 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
627 // An integer, or []byte, or []int, may be converted to a string.
628 if (lhs
->is_string_type())
630 if (rhs
->integer_type() != NULL
)
632 if (rhs
->is_open_array_type() && rhs
->named_type() == NULL
)
634 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
635 if (e
->integer_type() != NULL
636 && (e
== Type::lookup_integer_type("uint8")
637 || e
== Type::lookup_integer_type("int")))
642 // A string may be converted to []byte or []int.
643 if (rhs
->is_string_type()
644 && lhs
->is_open_array_type()
645 && lhs
->named_type() == NULL
)
647 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
648 if (e
->integer_type() != NULL
649 && (e
== Type::lookup_integer_type("uint8")
650 || e
== Type::lookup_integer_type("int")))
654 // An unsafe.Pointer type may be converted to any pointer type or to
655 // uintptr, and vice-versa.
656 if (lhs
->is_unsafe_pointer_type()
657 && (rhs
->points_to() != NULL
658 || (rhs
->integer_type() != NULL
659 && rhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
661 if (rhs
->is_unsafe_pointer_type()
662 && (lhs
->points_to() != NULL
663 || (lhs
->integer_type() != NULL
664 && lhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
667 // Give a better error message.
671 *reason
= "invalid type conversion";
674 std::string s
= "invalid type conversion (";
684 // Return whether this type has any hidden fields. This is only a
685 // possibility for a few types.
688 Type::has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
690 switch (this->forwarded()->classification_
)
693 return this->named_type()->named_type_has_hidden_fields(reason
);
695 return this->struct_type()->struct_has_hidden_fields(within
, reason
);
697 return this->array_type()->array_has_hidden_fields(within
, reason
);
703 // Return a hash code for the type to be used for method lookup.
706 Type::hash_for_method(Gogo
* gogo
) const
708 unsigned int ret
= 0;
709 if (this->classification_
!= TYPE_FORWARD
)
710 ret
+= this->classification_
;
711 return ret
+ this->do_hash_for_method(gogo
);
714 // Default implementation of do_hash_for_method. This is appropriate
715 // for types with no subfields.
718 Type::do_hash_for_method(Gogo
*) const
723 // Return a hash code for a string, given a starting hash.
726 Type::hash_string(const std::string
& s
, unsigned int h
)
728 const char* p
= s
.data();
729 size_t len
= s
.length();
730 for (; len
> 0; --len
)
738 // Default check for the expression passed to make. Any type which
739 // may be used with make implements its own version of this.
742 Type::do_check_make_expression(Expression_list
*, source_location
)
747 // Return whether an expression has an integer value. Report an error
748 // if not. This is used when handling calls to the predeclared make
752 Type::check_int_value(Expression
* e
, const char* errmsg
,
753 source_location location
)
755 if (e
->type()->integer_type() != NULL
)
758 // Check for a floating point constant with integer value.
763 if (e
->float_constant_value(fval
, &dummy
))
770 mpfr_clear_overflow();
771 mpfr_clear_erangeflag();
772 mpfr_get_z(ival
, fval
, GMP_RNDN
);
773 if (!mpfr_overflow_p()
774 && !mpfr_erangeflag_p()
775 && mpz_sgn(ival
) >= 0)
777 Named_type
* ntype
= Type::lookup_integer_type("int");
778 Integer_type
* inttype
= ntype
->integer_type();
780 mpz_init_set_ui(max
, 1);
781 mpz_mul_2exp(max
, max
, inttype
->bits() - 1);
782 ok
= mpz_cmp(ival
, max
) < 0;
796 error_at(location
, "%s", errmsg
);
800 // A hash table mapping unnamed types to trees.
802 Type::Type_trees
Type::type_trees
;
804 // Return a tree representing this type.
807 Type::get_tree(Gogo
* gogo
)
809 if (this->tree_
!= NULL
)
812 if (this->forward_declaration_type() != NULL
813 || this->named_type() != NULL
)
814 return this->get_tree_without_hash(gogo
);
816 if (this->is_error_type())
817 return error_mark_node
;
819 // To avoid confusing GIMPLE, we need to translate all identical Go
820 // types to the same GIMPLE type. We use a hash table to do that.
821 // There is no need to use the hash table for named types, as named
822 // types are only identical to themselves.
824 std::pair
<Type
*, tree
> val(this, NULL
);
825 std::pair
<Type_trees::iterator
, bool> ins
=
826 Type::type_trees
.insert(val
);
827 if (!ins
.second
&& ins
.first
->second
!= NULL_TREE
)
829 this->tree_
= ins
.first
->second
;
833 tree t
= this->get_tree_without_hash(gogo
);
835 if (ins
.first
->second
== NULL_TREE
)
836 ins
.first
->second
= t
;
839 // We have already created a tree for this type. This can
840 // happen when an unnamed type is defined using a named type
841 // which in turns uses an identical unnamed type. Use the tree
842 // we created earlier and ignore the one we just built.
843 t
= ins
.first
->second
;
850 // Return a tree for a type without looking in the hash table for
851 // identical types. This is used for named types, since there is no
852 // point to looking in the hash table for them.
855 Type::get_tree_without_hash(Gogo
* gogo
)
857 if (this->tree_
== NULL_TREE
)
859 tree t
= this->do_get_tree(gogo
);
861 // For a recursive function or pointer type, we will temporarily
862 // return ptr_type_node during the recursion. We don't want to
863 // record that for a forwarding type, as it may confuse us
865 if (t
== ptr_type_node
&& this->forward_declaration_type() != NULL
)
869 go_preserve_from_gc(t
);
875 // Return a tree representing a zero initialization for this type.
878 Type::get_init_tree(Gogo
* gogo
, bool is_clear
)
880 tree type_tree
= this->get_tree(gogo
);
881 if (type_tree
== error_mark_node
)
882 return error_mark_node
;
883 return this->do_get_init_tree(gogo
, type_tree
, is_clear
);
886 // Any type which supports the builtin make function must implement
890 Type::do_make_expression_tree(Translate_context
*, Expression_list
*,
896 // Return a pointer to the type descriptor for this type.
899 Type::type_descriptor_pointer(Gogo
* gogo
)
901 Type
* t
= this->forwarded();
902 if (t
->type_descriptor_decl_
== NULL_TREE
)
904 Expression
* e
= t
->do_type_descriptor(gogo
, NULL
);
905 gogo
->build_type_descriptor_decl(t
, e
, &t
->type_descriptor_decl_
);
906 gcc_assert(t
->type_descriptor_decl_
!= NULL_TREE
907 && (t
->type_descriptor_decl_
== error_mark_node
908 || DECL_P(t
->type_descriptor_decl_
)));
910 if (t
->type_descriptor_decl_
== error_mark_node
)
911 return error_mark_node
;
912 return build_fold_addr_expr(t
->type_descriptor_decl_
);
915 // Return a composite literal for a type descriptor.
918 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
920 return type
->do_type_descriptor(gogo
, NULL
);
923 // Return a composite literal for a type descriptor with a name.
926 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
928 gcc_assert(name
!= NULL
&& type
->named_type() != name
);
929 return type
->do_type_descriptor(gogo
, name
);
932 // Make a builtin struct type from a list of fields. The fields are
933 // pairs of a name and a type.
936 Type::make_builtin_struct_type(int nfields
, ...)
939 va_start(ap
, nfields
);
941 source_location bloc
= BUILTINS_LOCATION
;
942 Struct_field_list
* sfl
= new Struct_field_list();
943 for (int i
= 0; i
< nfields
; i
++)
945 const char* field_name
= va_arg(ap
, const char *);
946 Type
* type
= va_arg(ap
, Type
*);
947 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
952 return Type::make_struct_type(sfl
, bloc
);
955 // Make a builtin named type.
958 Type::make_builtin_named_type(const char* name
, Type
* type
)
960 source_location bloc
= BUILTINS_LOCATION
;
961 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
962 return no
->type_value();
965 // Return the type of a type descriptor. We should really tie this to
966 // runtime.Type rather than copying it. This must match commonType in
967 // libgo/go/runtime/type.go.
970 Type::make_type_descriptor_type()
975 source_location bloc
= BUILTINS_LOCATION
;
977 Type
* uint8_type
= Type::lookup_integer_type("uint8");
978 Type
* uint32_type
= Type::lookup_integer_type("uint32");
979 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
980 Type
* string_type
= Type::lookup_string_type();
981 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
983 // This is an unnamed version of unsafe.Pointer. Perhaps we
984 // should use the named version instead, although that would
985 // require us to create the unsafe package if it has not been
986 // imported. It probably doesn't matter.
987 Type
* void_type
= Type::make_void_type();
988 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
990 // Forward declaration for the type descriptor type.
991 Named_object
* named_type_descriptor_type
=
992 Named_object::make_type_declaration("commonType", NULL
, bloc
);
993 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
994 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
996 // The type of a method on a concrete type.
997 Struct_type
* method_type
=
998 Type::make_builtin_struct_type(5,
999 "name", pointer_string_type
,
1000 "pkgPath", pointer_string_type
,
1001 "mtyp", pointer_type_descriptor_type
,
1002 "typ", pointer_type_descriptor_type
,
1003 "tfn", unsafe_pointer_type
);
1004 Named_type
* named_method_type
=
1005 Type::make_builtin_named_type("method", method_type
);
1007 // Information for types with a name or methods.
1008 Type
* slice_named_method_type
=
1009 Type::make_array_type(named_method_type
, NULL
);
1010 Struct_type
* uncommon_type
=
1011 Type::make_builtin_struct_type(3,
1012 "name", pointer_string_type
,
1013 "pkgPath", pointer_string_type
,
1014 "methods", slice_named_method_type
);
1015 Named_type
* named_uncommon_type
=
1016 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1018 Type
* pointer_uncommon_type
=
1019 Type::make_pointer_type(named_uncommon_type
);
1021 // The type descriptor type.
1023 Typed_identifier_list
* params
= new Typed_identifier_list();
1024 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1025 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1027 Typed_identifier_list
* results
= new Typed_identifier_list();
1028 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1030 Type
* hashfn_type
= Type::make_function_type(NULL
, params
, results
, bloc
);
1032 params
= new Typed_identifier_list();
1033 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1034 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1035 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1037 results
= new Typed_identifier_list();
1038 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1040 Type
* equalfn_type
= Type::make_function_type(NULL
, params
, results
,
1043 Struct_type
* type_descriptor_type
=
1044 Type::make_builtin_struct_type(9,
1046 "align", uint8_type
,
1047 "fieldAlign", uint8_type
,
1048 "size", uintptr_type
,
1049 "hash", uint32_type
,
1050 "hashfn", hashfn_type
,
1051 "equalfn", equalfn_type
,
1052 "string", pointer_string_type
,
1053 "", pointer_uncommon_type
);
1055 Named_type
* named
= Type::make_builtin_named_type("commonType",
1056 type_descriptor_type
);
1058 named_type_descriptor_type
->set_type_value(named
);
1066 // Make the type of a pointer to a type descriptor as represented in
1070 Type::make_type_descriptor_ptr_type()
1074 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1078 // Return the names of runtime functions which compute a hash code for
1079 // this type and which compare whether two values of this type are
1083 Type::type_functions(const char** hash_fn
, const char** equal_fn
) const
1085 switch (this->base()->classification())
1087 case Type::TYPE_ERROR
:
1088 case Type::TYPE_VOID
:
1089 case Type::TYPE_NIL
:
1090 // These types can not be hashed or compared.
1091 *hash_fn
= "__go_type_hash_error";
1092 *equal_fn
= "__go_type_equal_error";
1095 case Type::TYPE_BOOLEAN
:
1096 case Type::TYPE_INTEGER
:
1097 case Type::TYPE_FLOAT
:
1098 case Type::TYPE_COMPLEX
:
1099 case Type::TYPE_POINTER
:
1100 case Type::TYPE_FUNCTION
:
1101 case Type::TYPE_MAP
:
1102 case Type::TYPE_CHANNEL
:
1103 *hash_fn
= "__go_type_hash_identity";
1104 *equal_fn
= "__go_type_equal_identity";
1107 case Type::TYPE_STRING
:
1108 *hash_fn
= "__go_type_hash_string";
1109 *equal_fn
= "__go_type_equal_string";
1112 case Type::TYPE_STRUCT
:
1113 case Type::TYPE_ARRAY
:
1114 // These types can not be hashed or compared.
1115 *hash_fn
= "__go_type_hash_error";
1116 *equal_fn
= "__go_type_equal_error";
1119 case Type::TYPE_INTERFACE
:
1120 if (this->interface_type()->is_empty())
1122 *hash_fn
= "__go_type_hash_empty_interface";
1123 *equal_fn
= "__go_type_equal_empty_interface";
1127 *hash_fn
= "__go_type_hash_interface";
1128 *equal_fn
= "__go_type_equal_interface";
1132 case Type::TYPE_NAMED
:
1133 case Type::TYPE_FORWARD
:
1141 // Return a composite literal for the type descriptor for a plain type
1142 // of kind RUNTIME_TYPE_KIND named NAME.
1145 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1146 Named_type
* name
, const Methods
* methods
,
1147 bool only_value_methods
)
1149 source_location bloc
= BUILTINS_LOCATION
;
1151 Type
* td_type
= Type::make_type_descriptor_type();
1152 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1154 Expression_list
* vals
= new Expression_list();
1157 Struct_field_list::const_iterator p
= fields
->begin();
1158 gcc_assert(p
->field_name() == "Kind");
1160 mpz_init_set_ui(iv
, runtime_type_kind
);
1161 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1164 gcc_assert(p
->field_name() == "align");
1165 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1166 vals
->push_back(Expression::make_type_info(this, type_info
));
1169 gcc_assert(p
->field_name() == "fieldAlign");
1170 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1171 vals
->push_back(Expression::make_type_info(this, type_info
));
1174 gcc_assert(p
->field_name() == "size");
1175 type_info
= Expression::TYPE_INFO_SIZE
;
1176 vals
->push_back(Expression::make_type_info(this, type_info
));
1179 gcc_assert(p
->field_name() == "hash");
1180 mpz_set_ui(iv
, this->hash_for_method(gogo
));
1181 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1183 const char* hash_fn
;
1184 const char* equal_fn
;
1185 this->type_functions(&hash_fn
, &equal_fn
);
1188 gcc_assert(p
->field_name() == "hashfn");
1189 Function_type
* fntype
= p
->type()->function_type();
1190 Named_object
* no
= Named_object::make_function_declaration(hash_fn
, NULL
,
1193 no
->func_declaration_value()->set_asm_name(hash_fn
);
1194 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1197 gcc_assert(p
->field_name() == "equalfn");
1198 fntype
= p
->type()->function_type();
1199 no
= Named_object::make_function_declaration(equal_fn
, NULL
, fntype
, bloc
);
1200 no
->func_declaration_value()->set_asm_name(equal_fn
);
1201 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1204 gcc_assert(p
->field_name() == "string");
1205 Expression
* s
= Expression::make_string((name
!= NULL
1206 ? name
->reflection(gogo
)
1207 : this->reflection(gogo
)),
1209 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1212 gcc_assert(p
->field_name() == "uncommonType");
1213 if (name
== NULL
&& methods
== NULL
)
1214 vals
->push_back(Expression::make_nil(bloc
));
1217 if (methods
== NULL
)
1218 methods
= name
->methods();
1219 vals
->push_back(this->uncommon_type_constructor(gogo
,
1222 only_value_methods
));
1226 gcc_assert(p
== fields
->end());
1230 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
1233 // Return a composite literal for the uncommon type information for
1234 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1235 // struct. If name is not NULL, it is the name of the type. If
1236 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1237 // is true if only value methods should be included. At least one of
1238 // NAME and METHODS must not be NULL.
1241 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
1242 Named_type
* name
, const Methods
* methods
,
1243 bool only_value_methods
) const
1245 source_location bloc
= BUILTINS_LOCATION
;
1247 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
1249 Expression_list
* vals
= new Expression_list();
1252 Struct_field_list::const_iterator p
= fields
->begin();
1253 gcc_assert(p
->field_name() == "name");
1256 gcc_assert(p
->field_name() == "pkgPath");
1260 vals
->push_back(Expression::make_nil(bloc
));
1261 vals
->push_back(Expression::make_nil(bloc
));
1265 Named_object
* no
= name
->named_object();
1266 std::string n
= Gogo::unpack_hidden_name(no
->name());
1267 Expression
* s
= Expression::make_string(n
, bloc
);
1268 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1270 if (name
->is_builtin())
1271 vals
->push_back(Expression::make_nil(bloc
));
1274 const Package
* package
= no
->package();
1275 const std::string
& unique_prefix(package
== NULL
1276 ? gogo
->unique_prefix()
1277 : package
->unique_prefix());
1278 const std::string
& package_name(package
== NULL
1279 ? gogo
->package_name()
1281 n
.assign(unique_prefix
);
1283 n
.append(package_name
);
1284 if (name
->in_function() != NULL
)
1287 n
.append(Gogo::unpack_hidden_name(name
->in_function()->name()));
1289 s
= Expression::make_string(n
, bloc
);
1290 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1295 gcc_assert(p
->field_name() == "methods");
1296 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
1297 only_value_methods
));
1300 gcc_assert(p
== fields
->end());
1302 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
1304 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
1307 // Sort methods by name.
1313 operator()(const std::pair
<std::string
, const Method
*>& m1
,
1314 const std::pair
<std::string
, const Method
*>& m2
) const
1315 { return m1
.first
< m2
.first
; }
1318 // Return a composite literal for the type method table for this type.
1319 // METHODS_TYPE is the type of the table, and is a slice type.
1320 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1321 // then only value methods are used.
1324 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
1325 const Methods
* methods
,
1326 bool only_value_methods
) const
1328 source_location bloc
= BUILTINS_LOCATION
;
1330 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
1331 if (methods
!= NULL
)
1333 smethods
.reserve(methods
->count());
1334 for (Methods::const_iterator p
= methods
->begin();
1335 p
!= methods
->end();
1338 if (p
->second
->is_ambiguous())
1340 if (only_value_methods
&& !p
->second
->is_value_method())
1342 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
1346 if (smethods
.empty())
1347 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
1349 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
1351 Type
* method_type
= methods_type
->array_type()->element_type();
1353 Expression_list
* vals
= new Expression_list();
1354 vals
->reserve(smethods
.size());
1355 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
1357 p
!= smethods
.end();
1359 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
1362 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
1365 // Return a composite literal for a single method. METHOD_TYPE is the
1366 // type of the entry. METHOD_NAME is the name of the method and M is
1367 // the method information.
1370 Type::method_constructor(Gogo
*, Type
* method_type
,
1371 const std::string
& method_name
,
1372 const Method
* m
) const
1374 source_location bloc
= BUILTINS_LOCATION
;
1376 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
1378 Expression_list
* vals
= new Expression_list();
1381 Struct_field_list::const_iterator p
= fields
->begin();
1382 gcc_assert(p
->field_name() == "name");
1383 const std::string n
= Gogo::unpack_hidden_name(method_name
);
1384 Expression
* s
= Expression::make_string(n
, bloc
);
1385 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1388 gcc_assert(p
->field_name() == "pkgPath");
1389 if (!Gogo::is_hidden_name(method_name
))
1390 vals
->push_back(Expression::make_nil(bloc
));
1393 s
= Expression::make_string(Gogo::hidden_name_prefix(method_name
), bloc
);
1394 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1397 Named_object
* no
= (m
->needs_stub_method()
1399 : m
->named_object());
1401 Function_type
* mtype
;
1402 if (no
->is_function())
1403 mtype
= no
->func_value()->type();
1405 mtype
= no
->func_declaration_value()->type();
1406 gcc_assert(mtype
->is_method());
1407 Type
* nonmethod_type
= mtype
->copy_without_receiver();
1410 gcc_assert(p
->field_name() == "mtyp");
1411 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
1414 gcc_assert(p
->field_name() == "typ");
1415 vals
->push_back(Expression::make_type_descriptor(mtype
, bloc
));
1418 gcc_assert(p
->field_name() == "tfn");
1419 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1422 gcc_assert(p
== fields
->end());
1424 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
1427 // Return a composite literal for the type descriptor of a plain type.
1428 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1429 // NULL, it is the name to use as well as the list of methods.
1432 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
1435 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
1439 // Return the type reflection string for this type.
1442 Type::reflection(Gogo
* gogo
) const
1446 // The do_reflection virtual function should set RET to the
1447 // reflection string.
1448 this->do_reflection(gogo
, &ret
);
1453 // Return a mangled name for the type.
1456 Type::mangled_name(Gogo
* gogo
) const
1460 // The do_mangled_name virtual function should set RET to the
1461 // mangled name. For a composite type it should append a code for
1462 // the composition and then call do_mangled_name on the components.
1463 this->do_mangled_name(gogo
, &ret
);
1468 // Default function to export a type.
1471 Type::do_export(Export
*) const
1479 Type::import_type(Import
* imp
)
1481 if (imp
->match_c_string("("))
1482 return Function_type::do_import(imp
);
1483 else if (imp
->match_c_string("*"))
1484 return Pointer_type::do_import(imp
);
1485 else if (imp
->match_c_string("struct "))
1486 return Struct_type::do_import(imp
);
1487 else if (imp
->match_c_string("["))
1488 return Array_type::do_import(imp
);
1489 else if (imp
->match_c_string("map "))
1490 return Map_type::do_import(imp
);
1491 else if (imp
->match_c_string("chan "))
1492 return Channel_type::do_import(imp
);
1493 else if (imp
->match_c_string("interface"))
1494 return Interface_type::do_import(imp
);
1497 error_at(imp
->location(), "import error: expected type");
1498 return Type::make_error_type();
1502 // A type used to indicate a parsing error. This exists to simplify
1503 // later error detection.
1505 class Error_type
: public Type
1515 { return error_mark_node
; }
1518 do_get_init_tree(Gogo
*, tree
, bool)
1519 { return error_mark_node
; }
1522 do_type_descriptor(Gogo
*, Named_type
*)
1523 { return Expression::make_error(BUILTINS_LOCATION
); }
1526 do_reflection(Gogo
*, std::string
*) const
1527 { gcc_assert(saw_errors()); }
1530 do_mangled_name(Gogo
*, std::string
* ret
) const
1531 { ret
->push_back('E'); }
1535 Type::make_error_type()
1537 static Error_type singleton_error_type
;
1538 return &singleton_error_type
;
1543 class Void_type
: public Type
1553 { return void_type_node
; }
1556 do_get_init_tree(Gogo
*, tree
, bool)
1557 { gcc_unreachable(); }
1560 do_type_descriptor(Gogo
*, Named_type
*)
1561 { gcc_unreachable(); }
1564 do_reflection(Gogo
*, std::string
*) const
1568 do_mangled_name(Gogo
*, std::string
* ret
) const
1569 { ret
->push_back('v'); }
1573 Type::make_void_type()
1575 static Void_type singleton_void_type
;
1576 return &singleton_void_type
;
1579 // The boolean type.
1581 class Boolean_type
: public Type
1585 : Type(TYPE_BOOLEAN
)
1591 { return boolean_type_node
; }
1594 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1595 { return is_clear
? NULL
: fold_convert(type_tree
, boolean_false_node
); }
1598 do_type_descriptor(Gogo
*, Named_type
* name
);
1600 // We should not be asked for the reflection string of a basic type.
1602 do_reflection(Gogo
*, std::string
* ret
) const
1603 { ret
->append("bool"); }
1606 do_mangled_name(Gogo
*, std::string
* ret
) const
1607 { ret
->push_back('b'); }
1610 // Make the type descriptor.
1613 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1616 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
1619 Named_object
* no
= gogo
->lookup_global("bool");
1620 gcc_assert(no
!= NULL
);
1621 return Type::type_descriptor(gogo
, no
->type_value());
1626 Type::make_boolean_type()
1628 static Boolean_type boolean_type
;
1629 return &boolean_type
;
1632 // The named type "bool".
1634 static Named_type
* named_bool_type
;
1636 // Get the named type "bool".
1639 Type::lookup_bool_type()
1641 return named_bool_type
;
1644 // Make the named type "bool".
1647 Type::make_named_bool_type()
1649 Type
* bool_type
= Type::make_boolean_type();
1650 Named_object
* named_object
= Named_object::make_type("bool", NULL
,
1653 Named_type
* named_type
= named_object
->type_value();
1654 named_bool_type
= named_type
;
1658 // Class Integer_type.
1660 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
1662 // Create a new integer type. Non-abstract integer types always have
1666 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
1667 int bits
, int runtime_type_kind
)
1669 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
1671 std::string
sname(name
);
1672 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1675 Named_type
* named_type
= named_object
->type_value();
1676 std::pair
<Named_integer_types::iterator
, bool> ins
=
1677 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
1678 gcc_assert(ins
.second
);
1682 // Look up an existing integer type.
1685 Integer_type::lookup_integer_type(const char* name
)
1687 Named_integer_types::const_iterator p
=
1688 Integer_type::named_integer_types
.find(name
);
1689 gcc_assert(p
!= Integer_type::named_integer_types
.end());
1693 // Create a new abstract integer type.
1696 Integer_type::create_abstract_integer_type()
1698 static Integer_type
* abstract_type
;
1699 if (abstract_type
== NULL
)
1700 abstract_type
= new Integer_type(true, false, INT_TYPE_SIZE
,
1701 RUNTIME_TYPE_KIND_INT
);
1702 return abstract_type
;
1705 // Integer type compatibility.
1708 Integer_type::is_identical(const Integer_type
* t
) const
1710 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
1712 return this->is_abstract_
== t
->is_abstract_
;
1718 Integer_type::do_hash_for_method(Gogo
*) const
1720 return ((this->bits_
<< 4)
1721 + ((this->is_unsigned_
? 1 : 0) << 8)
1722 + ((this->is_abstract_
? 1 : 0) << 9));
1725 // Get the tree for an Integer_type.
1728 Integer_type::do_get_tree(Gogo
*)
1730 gcc_assert(!this->is_abstract_
);
1731 if (this->is_unsigned_
)
1733 if (this->bits_
== INT_TYPE_SIZE
)
1734 return unsigned_type_node
;
1735 else if (this->bits_
== CHAR_TYPE_SIZE
)
1736 return unsigned_char_type_node
;
1737 else if (this->bits_
== SHORT_TYPE_SIZE
)
1738 return short_unsigned_type_node
;
1739 else if (this->bits_
== LONG_TYPE_SIZE
)
1740 return long_unsigned_type_node
;
1741 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1742 return long_long_unsigned_type_node
;
1744 return make_unsigned_type(this->bits_
);
1748 if (this->bits_
== INT_TYPE_SIZE
)
1749 return integer_type_node
;
1750 else if (this->bits_
== CHAR_TYPE_SIZE
)
1751 return signed_char_type_node
;
1752 else if (this->bits_
== SHORT_TYPE_SIZE
)
1753 return short_integer_type_node
;
1754 else if (this->bits_
== LONG_TYPE_SIZE
)
1755 return long_integer_type_node
;
1756 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1757 return long_long_integer_type_node
;
1759 return make_signed_type(this->bits_
);
1764 Integer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1766 return is_clear
? NULL
: build_int_cst(type_tree
, 0);
1769 // The type descriptor for an integer type. Integer types are always
1773 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1775 gcc_assert(name
!= NULL
);
1776 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1779 // We should not be asked for the reflection string of a basic type.
1782 Integer_type::do_reflection(Gogo
*, std::string
*) const
1790 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1793 snprintf(buf
, sizeof buf
, "i%s%s%de",
1794 this->is_abstract_
? "a" : "",
1795 this->is_unsigned_
? "u" : "",
1800 // Make an integer type.
1803 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
1804 int runtime_type_kind
)
1806 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
1810 // Make an abstract integer type.
1813 Type::make_abstract_integer_type()
1815 return Integer_type::create_abstract_integer_type();
1818 // Look up an integer type.
1821 Type::lookup_integer_type(const char* name
)
1823 return Integer_type::lookup_integer_type(name
);
1826 // Class Float_type.
1828 Float_type::Named_float_types
Float_type::named_float_types
;
1830 // Create a new float type. Non-abstract float types always have
1834 Float_type::create_float_type(const char* name
, int bits
,
1835 int runtime_type_kind
)
1837 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
1838 std::string
sname(name
);
1839 Named_object
* named_object
= Named_object::make_type(sname
, NULL
, float_type
,
1841 Named_type
* named_type
= named_object
->type_value();
1842 std::pair
<Named_float_types::iterator
, bool> ins
=
1843 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
1844 gcc_assert(ins
.second
);
1848 // Look up an existing float type.
1851 Float_type::lookup_float_type(const char* name
)
1853 Named_float_types::const_iterator p
=
1854 Float_type::named_float_types
.find(name
);
1855 gcc_assert(p
!= Float_type::named_float_types
.end());
1859 // Create a new abstract float type.
1862 Float_type::create_abstract_float_type()
1864 static Float_type
* abstract_type
;
1865 if (abstract_type
== NULL
)
1866 abstract_type
= new Float_type(true, FLOAT_TYPE_SIZE
,
1867 RUNTIME_TYPE_KIND_FLOAT
);
1868 return abstract_type
;
1871 // Whether this type is identical with T.
1874 Float_type::is_identical(const Float_type
* t
) const
1876 if (this->bits_
!= t
->bits_
)
1878 return this->is_abstract_
== t
->is_abstract_
;
1884 Float_type::do_hash_for_method(Gogo
*) const
1886 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
1889 // Get a tree without using a Gogo*.
1892 Float_type::type_tree() const
1894 if (this->bits_
== FLOAT_TYPE_SIZE
)
1895 return float_type_node
;
1896 else if (this->bits_
== DOUBLE_TYPE_SIZE
)
1897 return double_type_node
;
1898 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
)
1899 return long_double_type_node
;
1902 tree ret
= make_node(REAL_TYPE
);
1903 TYPE_PRECISION(ret
) = this->bits_
;
1912 Float_type::do_get_tree(Gogo
*)
1914 return this->type_tree();
1918 Float_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1923 real_from_integer(&r
, TYPE_MODE(type_tree
), 0, 0, 0);
1924 return build_real(type_tree
, r
);
1927 // The type descriptor for a float type. Float types are always named.
1930 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1932 gcc_assert(name
!= NULL
);
1933 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1936 // We should not be asked for the reflection string of a basic type.
1939 Float_type::do_reflection(Gogo
*, std::string
*) const
1947 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1950 snprintf(buf
, sizeof buf
, "f%s%de",
1951 this->is_abstract_
? "a" : "",
1956 // Make a floating point type.
1959 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
1961 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
1964 // Make an abstract float type.
1967 Type::make_abstract_float_type()
1969 return Float_type::create_abstract_float_type();
1972 // Look up a float type.
1975 Type::lookup_float_type(const char* name
)
1977 return Float_type::lookup_float_type(name
);
1980 // Class Complex_type.
1982 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
1984 // Create a new complex type. Non-abstract complex types always have
1988 Complex_type::create_complex_type(const char* name
, int bits
,
1989 int runtime_type_kind
)
1991 Complex_type
* complex_type
= new Complex_type(false, bits
,
1993 std::string
sname(name
);
1994 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1997 Named_type
* named_type
= named_object
->type_value();
1998 std::pair
<Named_complex_types::iterator
, bool> ins
=
1999 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
2001 gcc_assert(ins
.second
);
2005 // Look up an existing complex type.
2008 Complex_type::lookup_complex_type(const char* name
)
2010 Named_complex_types::const_iterator p
=
2011 Complex_type::named_complex_types
.find(name
);
2012 gcc_assert(p
!= Complex_type::named_complex_types
.end());
2016 // Create a new abstract complex type.
2019 Complex_type::create_abstract_complex_type()
2021 static Complex_type
* abstract_type
;
2022 if (abstract_type
== NULL
)
2023 abstract_type
= new Complex_type(true, FLOAT_TYPE_SIZE
* 2,
2024 RUNTIME_TYPE_KIND_FLOAT
);
2025 return abstract_type
;
2028 // Whether this type is identical with T.
2031 Complex_type::is_identical(const Complex_type
*t
) const
2033 if (this->bits_
!= t
->bits_
)
2035 return this->is_abstract_
== t
->is_abstract_
;
2041 Complex_type::do_hash_for_method(Gogo
*) const
2043 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2046 // Get a tree without using a Gogo*.
2049 Complex_type::type_tree() const
2051 if (this->bits_
== FLOAT_TYPE_SIZE
* 2)
2052 return complex_float_type_node
;
2053 else if (this->bits_
== DOUBLE_TYPE_SIZE
* 2)
2054 return complex_double_type_node
;
2055 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
* 2)
2056 return complex_long_double_type_node
;
2059 tree ret
= make_node(REAL_TYPE
);
2060 TYPE_PRECISION(ret
) = this->bits_
/ 2;
2062 return build_complex_type(ret
);
2069 Complex_type::do_get_tree(Gogo
*)
2071 return this->type_tree();
2074 // Zero initializer.
2077 Complex_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2082 real_from_integer(&r
, TYPE_MODE(TREE_TYPE(type_tree
)), 0, 0, 0);
2083 return build_complex(type_tree
, build_real(TREE_TYPE(type_tree
), r
),
2084 build_real(TREE_TYPE(type_tree
), r
));
2087 // The type descriptor for a complex type. Complex types are always
2091 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2093 gcc_assert(name
!= NULL
);
2094 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2097 // We should not be asked for the reflection string of a basic type.
2100 Complex_type::do_reflection(Gogo
*, std::string
*) const
2108 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2111 snprintf(buf
, sizeof buf
, "c%s%de",
2112 this->is_abstract_
? "a" : "",
2117 // Make a complex type.
2120 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
2122 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
2125 // Make an abstract complex type.
2128 Type::make_abstract_complex_type()
2130 return Complex_type::create_abstract_complex_type();
2133 // Look up a complex type.
2136 Type::lookup_complex_type(const char* name
)
2138 return Complex_type::lookup_complex_type(name
);
2141 // Class String_type.
2143 // Return the tree for String_type. A string is a struct with two
2144 // fields: a pointer to the characters and a length.
2147 String_type::do_get_tree(Gogo
*)
2149 static tree struct_type
;
2150 return Gogo::builtin_struct(&struct_type
, "__go_string", NULL_TREE
, 2,
2152 build_pointer_type(unsigned_char_type_node
),
2157 // Return a tree for the length of STRING.
2160 String_type::length_tree(Gogo
*, tree string
)
2162 tree string_type
= TREE_TYPE(string
);
2163 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2164 tree length_field
= DECL_CHAIN(TYPE_FIELDS(string_type
));
2165 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field
)),
2167 return fold_build3(COMPONENT_REF
, integer_type_node
, string
,
2168 length_field
, NULL_TREE
);
2171 // Return a tree for a pointer to the bytes of STRING.
2174 String_type::bytes_tree(Gogo
*, tree string
)
2176 tree string_type
= TREE_TYPE(string
);
2177 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2178 tree bytes_field
= TYPE_FIELDS(string_type
);
2179 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field
)),
2181 return fold_build3(COMPONENT_REF
, TREE_TYPE(bytes_field
), string
,
2182 bytes_field
, NULL_TREE
);
2185 // We initialize a string to { NULL, 0 }.
2188 String_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2193 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2195 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2197 for (tree field
= TYPE_FIELDS(type_tree
);
2199 field
= DECL_CHAIN(field
))
2201 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2203 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
2206 tree ret
= build_constructor(type_tree
, init
);
2207 TREE_CONSTANT(ret
) = 1;
2211 // The type descriptor for the string type.
2214 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2217 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
2220 Named_object
* no
= gogo
->lookup_global("string");
2221 gcc_assert(no
!= NULL
);
2222 return Type::type_descriptor(gogo
, no
->type_value());
2226 // We should not be asked for the reflection string of a basic type.
2229 String_type::do_reflection(Gogo
*, std::string
* ret
) const
2231 ret
->append("string");
2234 // Mangled name of a string type.
2237 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2239 ret
->push_back('z');
2242 // Make a string type.
2245 Type::make_string_type()
2247 static String_type string_type
;
2248 return &string_type
;
2251 // The named type "string".
2253 static Named_type
* named_string_type
;
2255 // Get the named type "string".
2258 Type::lookup_string_type()
2260 return named_string_type
;
2263 // Make the named type string.
2266 Type::make_named_string_type()
2268 Type
* string_type
= Type::make_string_type();
2269 Named_object
* named_object
= Named_object::make_type("string", NULL
,
2272 Named_type
* named_type
= named_object
->type_value();
2273 named_string_type
= named_type
;
2277 // The sink type. This is the type of the blank identifier _. Any
2278 // type may be assigned to it.
2280 class Sink_type
: public Type
2290 { gcc_unreachable(); }
2293 do_get_init_tree(Gogo
*, tree
, bool)
2294 { gcc_unreachable(); }
2297 do_type_descriptor(Gogo
*, Named_type
*)
2298 { gcc_unreachable(); }
2301 do_reflection(Gogo
*, std::string
*) const
2302 { gcc_unreachable(); }
2305 do_mangled_name(Gogo
*, std::string
*) const
2306 { gcc_unreachable(); }
2309 // Make the sink type.
2312 Type::make_sink_type()
2314 static Sink_type sink_type
;
2318 // Class Function_type.
2323 Function_type::do_traverse(Traverse
* traverse
)
2325 if (this->receiver_
!= NULL
2326 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
2327 return TRAVERSE_EXIT
;
2328 if (this->parameters_
!= NULL
2329 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
2330 return TRAVERSE_EXIT
;
2331 if (this->results_
!= NULL
2332 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
2333 return TRAVERSE_EXIT
;
2334 return TRAVERSE_CONTINUE
;
2337 // Returns whether T is a valid redeclaration of this type. If this
2338 // returns false, and REASON is not NULL, *REASON may be set to a
2339 // brief explanation of why it returned false.
2342 Function_type::is_valid_redeclaration(const Function_type
* t
,
2343 std::string
* reason
) const
2345 if (!this->is_identical(t
, false, true, reason
))
2348 // A redeclaration of a function is required to use the same names
2349 // for the receiver and parameters.
2350 if (this->receiver() != NULL
2351 && this->receiver()->name() != t
->receiver()->name()
2352 && this->receiver()->name() != Import::import_marker
2353 && t
->receiver()->name() != Import::import_marker
)
2356 *reason
= "receiver name changed";
2360 const Typed_identifier_list
* parms1
= this->parameters();
2361 const Typed_identifier_list
* parms2
= t
->parameters();
2364 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2365 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2366 p2
!= parms2
->end();
2369 if (p1
->name() != p2
->name()
2370 && p1
->name() != Import::import_marker
2371 && p2
->name() != Import::import_marker
)
2374 *reason
= "parameter name changed";
2378 // This is called at parse time, so we may have unknown
2380 Type
* t1
= p1
->type()->forwarded();
2381 Type
* t2
= p2
->type()->forwarded();
2383 && t1
->forward_declaration_type() != NULL
2384 && (t2
->forward_declaration_type() == NULL
2385 || (t1
->forward_declaration_type()->named_object()
2386 != t2
->forward_declaration_type()->named_object())))
2391 const Typed_identifier_list
* results1
= this->results();
2392 const Typed_identifier_list
* results2
= t
->results();
2393 if (results1
!= NULL
)
2395 Typed_identifier_list::const_iterator res1
= results1
->begin();
2396 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2397 res2
!= results2
->end();
2400 if (res1
->name() != res2
->name()
2401 && res1
->name() != Import::import_marker
2402 && res2
->name() != Import::import_marker
)
2405 *reason
= "result name changed";
2409 // This is called at parse time, so we may have unknown
2411 Type
* t1
= res1
->type()->forwarded();
2412 Type
* t2
= res2
->type()->forwarded();
2414 && t1
->forward_declaration_type() != NULL
2415 && (t2
->forward_declaration_type() == NULL
2416 || (t1
->forward_declaration_type()->named_object()
2417 != t2
->forward_declaration_type()->named_object())))
2425 // Check whether T is the same as this type.
2428 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
2429 bool errors_are_identical
,
2430 std::string
* reason
) const
2432 if (!ignore_receiver
)
2434 const Typed_identifier
* r1
= this->receiver();
2435 const Typed_identifier
* r2
= t
->receiver();
2436 if ((r1
!= NULL
) != (r2
!= NULL
))
2439 *reason
= _("different receiver types");
2444 if (!Type::are_identical(r1
->type(), r2
->type(), errors_are_identical
,
2447 if (reason
!= NULL
&& !reason
->empty())
2448 *reason
= "receiver: " + *reason
;
2454 const Typed_identifier_list
* parms1
= this->parameters();
2455 const Typed_identifier_list
* parms2
= t
->parameters();
2456 if ((parms1
!= NULL
) != (parms2
!= NULL
))
2459 *reason
= _("different number of parameters");
2464 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2465 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2466 p2
!= parms2
->end();
2469 if (p1
== parms1
->end())
2472 *reason
= _("different number of parameters");
2476 if (!Type::are_identical(p1
->type(), p2
->type(),
2477 errors_are_identical
, NULL
))
2480 *reason
= _("different parameter types");
2484 if (p1
!= parms1
->end())
2487 *reason
= _("different number of parameters");
2492 if (this->is_varargs() != t
->is_varargs())
2495 *reason
= _("different varargs");
2499 const Typed_identifier_list
* results1
= this->results();
2500 const Typed_identifier_list
* results2
= t
->results();
2501 if ((results1
!= NULL
) != (results2
!= NULL
))
2504 *reason
= _("different number of results");
2507 if (results1
!= NULL
)
2509 Typed_identifier_list::const_iterator res1
= results1
->begin();
2510 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2511 res2
!= results2
->end();
2514 if (res1
== results1
->end())
2517 *reason
= _("different number of results");
2521 if (!Type::are_identical(res1
->type(), res2
->type(),
2522 errors_are_identical
, NULL
))
2525 *reason
= _("different result types");
2529 if (res1
!= results1
->end())
2532 *reason
= _("different number of results");
2543 Function_type::do_hash_for_method(Gogo
* gogo
) const
2545 unsigned int ret
= 0;
2546 // We ignore the receiver type for hash codes, because we need to
2547 // get the same hash code for a method in an interface and a method
2548 // declared for a type. The former will not have a receiver.
2549 if (this->parameters_
!= NULL
)
2552 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2553 p
!= this->parameters_
->end();
2555 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2557 if (this->results_
!= NULL
)
2560 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2561 p
!= this->results_
->end();
2563 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2565 if (this->is_varargs_
)
2571 // Get the tree for a function type.
2574 Function_type::do_get_tree(Gogo
* gogo
)
2576 tree args
= NULL_TREE
;
2579 if (this->receiver_
!= NULL
)
2581 Type
* rtype
= this->receiver_
->type();
2582 tree ptype
= rtype
->get_tree(gogo
);
2583 if (ptype
== error_mark_node
)
2584 return error_mark_node
;
2586 // We always pass the address of the receiver parameter, in
2587 // order to make interface calls work with unknown types.
2588 if (rtype
->points_to() == NULL
)
2589 ptype
= build_pointer_type(ptype
);
2591 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2592 pp
= &TREE_CHAIN (*pp
);
2595 if (this->parameters_
!= NULL
)
2597 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2598 p
!= this->parameters_
->end();
2601 tree ptype
= p
->type()->get_tree(gogo
);
2602 if (ptype
== error_mark_node
)
2603 return error_mark_node
;
2604 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2605 pp
= &TREE_CHAIN (*pp
);
2609 // Varargs is handled entirely at the Go level. At the tree level,
2610 // functions are not varargs.
2611 *pp
= void_list_node
;
2614 if (this->results_
== NULL
)
2615 result
= void_type_node
;
2616 else if (this->results_
->size() == 1)
2617 result
= this->results_
->begin()->type()->get_tree(gogo
);
2620 result
= make_node(RECORD_TYPE
);
2621 tree field_trees
= NULL_TREE
;
2622 tree
* pp
= &field_trees
;
2623 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2624 p
!= this->results_
->end();
2627 const std::string name
= (p
->name().empty()
2629 : Gogo::unpack_hidden_name(p
->name()));
2630 tree name_tree
= get_identifier_with_length(name
.data(),
2632 tree field_type_tree
= p
->type()->get_tree(gogo
);
2633 if (field_type_tree
== error_mark_node
)
2634 return error_mark_node
;
2635 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
2637 DECL_CONTEXT(field
) = result
;
2639 pp
= &DECL_CHAIN(field
);
2641 TYPE_FIELDS(result
) = field_trees
;
2642 layout_type(result
);
2645 if (result
== error_mark_node
)
2646 return error_mark_node
;
2648 tree fntype
= build_function_type(result
, args
);
2649 if (fntype
== error_mark_node
)
2652 return build_pointer_type(fntype
);
2655 // Functions are initialized to NULL.
2658 Function_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2662 return fold_convert(type_tree
, null_pointer_node
);
2665 // The type of a function type descriptor.
2668 Function_type::make_function_type_descriptor_type()
2673 Type
* tdt
= Type::make_type_descriptor_type();
2674 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
2676 Type
* bool_type
= Type::lookup_bool_type();
2678 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
2680 Struct_type
* s
= Type::make_builtin_struct_type(4,
2682 "dotdotdot", bool_type
,
2686 ret
= Type::make_builtin_named_type("FuncType", s
);
2692 // The type descriptor for a function type.
2695 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2697 source_location bloc
= BUILTINS_LOCATION
;
2699 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
2701 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
2703 Expression_list
* vals
= new Expression_list();
2706 Struct_field_list::const_iterator p
= fields
->begin();
2707 gcc_assert(p
->field_name() == "commonType");
2708 vals
->push_back(this->type_descriptor_constructor(gogo
,
2709 RUNTIME_TYPE_KIND_FUNC
,
2713 gcc_assert(p
->field_name() == "dotdotdot");
2714 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
2717 gcc_assert(p
->field_name() == "in");
2718 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
2719 this->parameters()));
2722 gcc_assert(p
->field_name() == "out");
2723 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
2727 gcc_assert(p
== fields
->end());
2729 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
2732 // Return a composite literal for the parameters or results of a type
2736 Function_type::type_descriptor_params(Type
* params_type
,
2737 const Typed_identifier
* receiver
,
2738 const Typed_identifier_list
* params
)
2740 source_location bloc
= BUILTINS_LOCATION
;
2742 if (receiver
== NULL
&& params
== NULL
)
2743 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
2745 Expression_list
* vals
= new Expression_list();
2746 vals
->reserve((params
== NULL
? 0 : params
->size())
2747 + (receiver
!= NULL
? 1 : 0));
2749 if (receiver
!= NULL
)
2751 Type
* rtype
= receiver
->type();
2752 // The receiver is always passed as a pointer. FIXME: Is this
2753 // right? Should that fact affect the type descriptor?
2754 if (rtype
->points_to() == NULL
)
2755 rtype
= Type::make_pointer_type(rtype
);
2756 vals
->push_back(Expression::make_type_descriptor(rtype
, bloc
));
2761 for (Typed_identifier_list::const_iterator p
= params
->begin();
2764 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
2767 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
2770 // The reflection string.
2773 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
2775 // FIXME: Turn this off until we straighten out the type of the
2776 // struct field used in a go statement which calls a method.
2777 // gcc_assert(this->receiver_ == NULL);
2779 ret
->append("func");
2781 if (this->receiver_
!= NULL
)
2783 ret
->push_back('(');
2784 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
2785 ret
->push_back(')');
2788 ret
->push_back('(');
2789 const Typed_identifier_list
* params
= this->parameters();
2792 bool is_varargs
= this->is_varargs_
;
2793 for (Typed_identifier_list::const_iterator p
= params
->begin();
2797 if (p
!= params
->begin())
2799 if (!is_varargs
|| p
+ 1 != params
->end())
2800 this->append_reflection(p
->type(), gogo
, ret
);
2804 this->append_reflection(p
->type()->array_type()->element_type(),
2809 ret
->push_back(')');
2811 const Typed_identifier_list
* results
= this->results();
2812 if (results
!= NULL
&& !results
->empty())
2814 if (results
->size() == 1)
2815 ret
->push_back(' ');
2818 for (Typed_identifier_list::const_iterator p
= results
->begin();
2819 p
!= results
->end();
2822 if (p
!= results
->begin())
2824 this->append_reflection(p
->type(), gogo
, ret
);
2826 if (results
->size() > 1)
2827 ret
->push_back(')');
2834 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
2836 ret
->push_back('F');
2838 if (this->receiver_
!= NULL
)
2840 ret
->push_back('m');
2841 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
2844 const Typed_identifier_list
* params
= this->parameters();
2847 ret
->push_back('p');
2848 for (Typed_identifier_list::const_iterator p
= params
->begin();
2851 this->append_mangled_name(p
->type(), gogo
, ret
);
2852 if (this->is_varargs_
)
2853 ret
->push_back('V');
2854 ret
->push_back('e');
2857 const Typed_identifier_list
* results
= this->results();
2858 if (results
!= NULL
)
2860 ret
->push_back('r');
2861 for (Typed_identifier_list::const_iterator p
= results
->begin();
2862 p
!= results
->end();
2864 this->append_mangled_name(p
->type(), gogo
, ret
);
2865 ret
->push_back('e');
2868 ret
->push_back('e');
2871 // Export a function type.
2874 Function_type::do_export(Export
* exp
) const
2876 // We don't write out the receiver. The only function types which
2877 // should have a receiver are the ones associated with explicitly
2878 // defined methods. For those the receiver type is written out by
2879 // Function::export_func.
2881 exp
->write_c_string("(");
2883 if (this->parameters_
!= NULL
)
2885 bool is_varargs
= this->is_varargs_
;
2886 for (Typed_identifier_list::const_iterator p
=
2887 this->parameters_
->begin();
2888 p
!= this->parameters_
->end();
2894 exp
->write_c_string(", ");
2895 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
2896 exp
->write_type(p
->type());
2899 exp
->write_c_string("...");
2900 exp
->write_type(p
->type()->array_type()->element_type());
2904 exp
->write_c_string(")");
2906 const Typed_identifier_list
* results
= this->results_
;
2907 if (results
!= NULL
)
2909 exp
->write_c_string(" ");
2910 if (results
->size() == 1)
2911 exp
->write_type(results
->begin()->type());
2915 exp
->write_c_string("(");
2916 for (Typed_identifier_list::const_iterator p
= results
->begin();
2917 p
!= results
->end();
2923 exp
->write_c_string(", ");
2924 exp
->write_type(p
->type());
2926 exp
->write_c_string(")");
2931 // Import a function type.
2934 Function_type::do_import(Import
* imp
)
2936 imp
->require_c_string("(");
2937 Typed_identifier_list
* parameters
;
2938 bool is_varargs
= false;
2939 if (imp
->peek_char() == ')')
2943 parameters
= new Typed_identifier_list();
2946 if (imp
->match_c_string("..."))
2952 Type
* ptype
= imp
->read_type();
2954 ptype
= Type::make_array_type(ptype
, NULL
);
2955 parameters
->push_back(Typed_identifier(Import::import_marker
,
2956 ptype
, imp
->location()));
2957 if (imp
->peek_char() != ',')
2959 gcc_assert(!is_varargs
);
2960 imp
->require_c_string(", ");
2963 imp
->require_c_string(")");
2965 Typed_identifier_list
* results
;
2966 if (imp
->peek_char() != ' ')
2971 results
= new Typed_identifier_list
;
2972 if (imp
->peek_char() != '(')
2974 Type
* rtype
= imp
->read_type();
2975 results
->push_back(Typed_identifier(Import::import_marker
, rtype
,
2983 Type
* rtype
= imp
->read_type();
2984 results
->push_back(Typed_identifier(Import::import_marker
,
2985 rtype
, imp
->location()));
2986 if (imp
->peek_char() != ',')
2988 imp
->require_c_string(", ");
2990 imp
->require_c_string(")");
2994 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
2997 ret
->set_is_varargs();
3001 // Make a copy of a function type without a receiver.
3004 Function_type::copy_without_receiver() const
3006 gcc_assert(this->is_method());
3007 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
3010 if (this->is_varargs())
3011 ret
->set_is_varargs();
3012 if (this->is_builtin())
3013 ret
->set_is_builtin();
3017 // Make a copy of a function type with a receiver.
3020 Function_type::copy_with_receiver(Type
* receiver_type
) const
3022 gcc_assert(!this->is_method());
3023 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
3025 return Type::make_function_type(receiver
, this->parameters_
,
3026 this->results_
, this->location_
);
3029 // Make a function type.
3032 Type::make_function_type(Typed_identifier
* receiver
,
3033 Typed_identifier_list
* parameters
,
3034 Typed_identifier_list
* results
,
3035 source_location location
)
3037 return new Function_type(receiver
, parameters
, results
, location
);
3040 // Class Pointer_type.
3045 Pointer_type::do_traverse(Traverse
* traverse
)
3047 return Type::traverse(this->to_type_
, traverse
);
3053 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
3055 return this->to_type_
->hash_for_method(gogo
) << 4;
3058 // The tree for a pointer type.
3061 Pointer_type::do_get_tree(Gogo
* gogo
)
3063 return build_pointer_type(this->to_type_
->get_tree(gogo
));
3066 // Initialize a pointer type.
3069 Pointer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3073 return fold_convert(type_tree
, null_pointer_node
);
3076 // The type of a pointer type descriptor.
3079 Pointer_type::make_pointer_type_descriptor_type()
3084 Type
* tdt
= Type::make_type_descriptor_type();
3085 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3087 Struct_type
* s
= Type::make_builtin_struct_type(2,
3091 ret
= Type::make_builtin_named_type("PtrType", s
);
3097 // The type descriptor for a pointer type.
3100 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3102 if (this->is_unsafe_pointer_type())
3104 gcc_assert(name
!= NULL
);
3105 return this->plain_type_descriptor(gogo
,
3106 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
3111 source_location bloc
= BUILTINS_LOCATION
;
3113 const Methods
* methods
;
3114 Type
* deref
= this->points_to();
3115 if (deref
->named_type() != NULL
)
3116 methods
= deref
->named_type()->methods();
3117 else if (deref
->struct_type() != NULL
)
3118 methods
= deref
->struct_type()->methods();
3122 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
3124 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
3126 Expression_list
* vals
= new Expression_list();
3129 Struct_field_list::const_iterator p
= fields
->begin();
3130 gcc_assert(p
->field_name() == "commonType");
3131 vals
->push_back(this->type_descriptor_constructor(gogo
,
3132 RUNTIME_TYPE_KIND_PTR
,
3133 name
, methods
, false));
3136 gcc_assert(p
->field_name() == "elem");
3137 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
3139 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
3143 // Reflection string.
3146 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3148 ret
->push_back('*');
3149 this->append_reflection(this->to_type_
, gogo
, ret
);
3155 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3157 ret
->push_back('p');
3158 this->append_mangled_name(this->to_type_
, gogo
, ret
);
3164 Pointer_type::do_export(Export
* exp
) const
3166 exp
->write_c_string("*");
3167 if (this->is_unsafe_pointer_type())
3168 exp
->write_c_string("any");
3170 exp
->write_type(this->to_type_
);
3176 Pointer_type::do_import(Import
* imp
)
3178 imp
->require_c_string("*");
3179 if (imp
->match_c_string("any"))
3182 return Type::make_pointer_type(Type::make_void_type());
3184 Type
* to
= imp
->read_type();
3185 return Type::make_pointer_type(to
);
3188 // Make a pointer type.
3191 Type::make_pointer_type(Type
* to_type
)
3193 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
3194 static Hashtable pointer_types
;
3195 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
3196 if (p
!= pointer_types
.end())
3198 Pointer_type
* ret
= new Pointer_type(to_type
);
3199 pointer_types
[to_type
] = ret
;
3203 // The nil type. We use a special type for nil because it is not the
3204 // same as any other type. In C term nil has type void*, but there is
3205 // no such type in Go.
3207 class Nil_type
: public Type
3217 { return ptr_type_node
; }
3220 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3221 { return is_clear
? NULL
: fold_convert(type_tree
, null_pointer_node
); }
3224 do_type_descriptor(Gogo
*, Named_type
*)
3225 { gcc_unreachable(); }
3228 do_reflection(Gogo
*, std::string
*) const
3229 { gcc_unreachable(); }
3232 do_mangled_name(Gogo
*, std::string
* ret
) const
3233 { ret
->push_back('n'); }
3236 // Make the nil type.
3239 Type::make_nil_type()
3241 static Nil_type singleton_nil_type
;
3242 return &singleton_nil_type
;
3245 // The type of a function call which returns multiple values. This is
3246 // really a struct, but we don't want to confuse a function call which
3247 // returns a struct with a function call which returns multiple
3250 class Call_multiple_result_type
: public Type
3253 Call_multiple_result_type(Call_expression
* call
)
3254 : Type(TYPE_CALL_MULTIPLE_RESULT
),
3260 do_has_pointer() const
3262 gcc_assert(saw_errors());
3270 do_get_init_tree(Gogo
*, tree
, bool)
3272 gcc_assert(saw_errors());
3273 return error_mark_node
;
3277 do_type_descriptor(Gogo
*, Named_type
*)
3279 gcc_assert(saw_errors());
3280 return Expression::make_error(UNKNOWN_LOCATION
);
3284 do_reflection(Gogo
*, std::string
*) const
3285 { gcc_assert(saw_errors()); }
3288 do_mangled_name(Gogo
*, std::string
*) const
3289 { gcc_assert(saw_errors()); }
3292 // The expression being called.
3293 Call_expression
* call_
;
3296 // Return the tree for a call result.
3299 Call_multiple_result_type::do_get_tree(Gogo
* gogo
)
3301 Function_type
* fntype
= this->call_
->get_function_type();
3302 gcc_assert(fntype
!= NULL
);
3303 const Typed_identifier_list
* results
= fntype
->results();
3304 gcc_assert(results
!= NULL
&& results
->size() > 1);
3306 Struct_field_list
* sfl
= new Struct_field_list
;
3307 for (Typed_identifier_list::const_iterator p
= results
->begin();
3308 p
!= results
->end();
3311 const std::string name
= ((p
->name().empty()
3312 || p
->name() == Import::import_marker
)
3315 sfl
->push_back(Struct_field(Typed_identifier(name
, p
->type(),
3316 this->call_
->location())));
3318 return Type::make_struct_type(sfl
, this->call_
->location())->get_tree(gogo
);
3321 // Make a call result type.
3324 Type::make_call_multiple_result_type(Call_expression
* call
)
3326 return new Call_multiple_result_type(call
);
3329 // Class Struct_field.
3331 // Get the name of a field.
3334 Struct_field::field_name() const
3336 const std::string
& name(this->typed_identifier_
.name());
3341 // This is called during parsing, before anything is lowered, so
3342 // we have to be pretty careful to avoid dereferencing an
3343 // unknown type name.
3344 Type
* t
= this->typed_identifier_
.type();
3346 if (t
->classification() == Type::TYPE_POINTER
)
3349 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
3350 dt
= ptype
->points_to();
3352 if (dt
->forward_declaration_type() != NULL
)
3353 return dt
->forward_declaration_type()->name();
3354 else if (dt
->named_type() != NULL
)
3355 return dt
->named_type()->name();
3356 else if (t
->is_error_type() || dt
->is_error_type())
3358 static const std::string error_string
= "*error*";
3359 return error_string
;
3363 // Avoid crashing in the erroneous case where T is named but
3365 gcc_assert(t
!= dt
);
3366 if (t
->forward_declaration_type() != NULL
)
3367 return t
->forward_declaration_type()->name();
3368 else if (t
->named_type() != NULL
)
3369 return t
->named_type()->name();
3376 // Class Struct_type.
3381 Struct_type::do_traverse(Traverse
* traverse
)
3383 Struct_field_list
* fields
= this->fields_
;
3386 for (Struct_field_list::iterator p
= fields
->begin();
3390 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
3391 return TRAVERSE_EXIT
;
3394 return TRAVERSE_CONTINUE
;
3397 // Verify that the struct type is complete and valid.
3400 Struct_type::do_verify()
3402 Struct_field_list
* fields
= this->fields_
;
3406 for (Struct_field_list::iterator p
= fields
->begin();
3410 Type
* t
= p
->type();
3411 if (t
->is_undefined())
3413 error_at(p
->location(), "struct field type is incomplete");
3414 p
->set_type(Type::make_error_type());
3417 else if (p
->is_anonymous())
3419 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
3421 error_at(p
->location(), "embedded type may not be a pointer");
3422 p
->set_type(Type::make_error_type());
3430 // Whether this contains a pointer.
3433 Struct_type::do_has_pointer() const
3435 const Struct_field_list
* fields
= this->fields();
3438 for (Struct_field_list::const_iterator p
= fields
->begin();
3442 if (p
->type()->has_pointer())
3448 // Whether this type is identical to T.
3451 Struct_type::is_identical(const Struct_type
* t
,
3452 bool errors_are_identical
) const
3454 const Struct_field_list
* fields1
= this->fields();
3455 const Struct_field_list
* fields2
= t
->fields();
3456 if (fields1
== NULL
|| fields2
== NULL
)
3457 return fields1
== fields2
;
3458 Struct_field_list::const_iterator pf2
= fields2
->begin();
3459 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
3460 pf1
!= fields1
->end();
3463 if (pf2
== fields2
->end())
3465 if (pf1
->field_name() != pf2
->field_name())
3467 if (pf1
->is_anonymous() != pf2
->is_anonymous()
3468 || !Type::are_identical(pf1
->type(), pf2
->type(),
3469 errors_are_identical
, NULL
))
3471 if (!pf1
->has_tag())
3478 if (!pf2
->has_tag())
3480 if (pf1
->tag() != pf2
->tag())
3484 if (pf2
!= fields2
->end())
3489 // Whether this struct type has any hidden fields.
3492 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
3493 std::string
* reason
) const
3495 const Struct_field_list
* fields
= this->fields();
3498 const Package
* within_package
= (within
== NULL
3500 : within
->named_object()->package());
3501 for (Struct_field_list::const_iterator pf
= fields
->begin();
3502 pf
!= fields
->end();
3505 if (within_package
!= NULL
3506 && !pf
->is_anonymous()
3507 && Gogo::is_hidden_name(pf
->field_name()))
3511 std::string within_name
= within
->named_object()->message_name();
3512 std::string name
= Gogo::message_name(pf
->field_name());
3513 size_t bufsize
= 200 + within_name
.length() + name
.length();
3514 char* buf
= new char[bufsize
];
3515 snprintf(buf
, bufsize
,
3516 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3517 open_quote
, within_name
.c_str(), close_quote
,
3518 open_quote
, name
.c_str(), close_quote
);
3519 reason
->assign(buf
);
3525 if (pf
->type()->has_hidden_fields(within
, reason
))
3535 Struct_type::do_hash_for_method(Gogo
* gogo
) const
3537 unsigned int ret
= 0;
3538 if (this->fields() != NULL
)
3540 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
3541 pf
!= this->fields()->end();
3543 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
3548 // Find the local field NAME.
3551 Struct_type::find_local_field(const std::string
& name
,
3552 unsigned int *pindex
) const
3554 const Struct_field_list
* fields
= this->fields_
;
3558 for (Struct_field_list::const_iterator pf
= fields
->begin();
3559 pf
!= fields
->end();
3562 if (pf
->field_name() == name
)
3572 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3574 Field_reference_expression
*
3575 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
3576 source_location location
) const
3579 return this->field_reference_depth(struct_expr
, name
, location
, &depth
);
3582 // Return an expression for a field, along with the depth at which it
3585 Field_reference_expression
*
3586 Struct_type::field_reference_depth(Expression
* struct_expr
,
3587 const std::string
& name
,
3588 source_location location
,
3589 unsigned int* depth
) const
3591 const Struct_field_list
* fields
= this->fields_
;
3595 // Look for a field with this name.
3597 for (Struct_field_list::const_iterator pf
= fields
->begin();
3598 pf
!= fields
->end();
3601 if (pf
->field_name() == name
)
3604 return Expression::make_field_reference(struct_expr
, i
, location
);
3608 // Look for an anonymous field which contains a field with this
3610 unsigned int found_depth
= 0;
3611 Field_reference_expression
* ret
= NULL
;
3613 for (Struct_field_list::const_iterator pf
= fields
->begin();
3614 pf
!= fields
->end();
3617 if (!pf
->is_anonymous())
3620 Struct_type
* st
= pf
->type()->deref()->struct_type();
3624 // Look for a reference using a NULL struct expression. If we
3625 // find one, fill in the struct expression with a reference to
3627 unsigned int subdepth
;
3628 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
3634 if (ret
== NULL
|| subdepth
< found_depth
)
3639 found_depth
= subdepth
;
3640 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
3642 if (pf
->type()->points_to() != NULL
)
3643 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
3644 while (sub
->expr() != NULL
)
3646 sub
= sub
->expr()->deref()->field_reference_expression();
3647 gcc_assert(sub
!= NULL
);
3649 sub
->set_struct_expression(here
);
3651 else if (subdepth
> found_depth
)
3655 // We do not handle ambiguity here--it should be handled by
3656 // Type::bind_field_or_method.
3664 *depth
= found_depth
+ 1;
3669 // Return the total number of fields, including embedded fields.
3672 Struct_type::total_field_count() const
3674 if (this->fields_
== NULL
)
3676 unsigned int ret
= 0;
3677 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3678 pf
!= this->fields_
->end();
3681 if (!pf
->is_anonymous() || pf
->type()->deref()->struct_type() == NULL
)
3684 ret
+= pf
->type()->struct_type()->total_field_count();
3689 // Return whether NAME is an unexported field, for better error reporting.
3692 Struct_type::is_unexported_local_field(Gogo
* gogo
,
3693 const std::string
& name
) const
3695 const Struct_field_list
* fields
= this->fields_
;
3698 for (Struct_field_list::const_iterator pf
= fields
->begin();
3699 pf
!= fields
->end();
3702 const std::string
& field_name(pf
->field_name());
3703 if (Gogo::is_hidden_name(field_name
)
3704 && name
== Gogo::unpack_hidden_name(field_name
)
3705 && gogo
->pack_hidden_name(name
, false) != field_name
)
3712 // Finalize the methods of an unnamed struct.
3715 Struct_type::finalize_methods(Gogo
* gogo
)
3717 if (this->all_methods_
!= NULL
)
3719 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
3722 // Return the method NAME, or NULL if there isn't one or if it is
3723 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3727 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
3729 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
3732 // Get the tree for a struct type.
3735 Struct_type::do_get_tree(Gogo
* gogo
)
3737 tree type
= make_node(RECORD_TYPE
);
3738 return this->fill_in_tree(gogo
, type
);
3741 // Fill in the fields for a struct type.
3744 Struct_type::fill_in_tree(Gogo
* gogo
, tree type
)
3746 tree field_trees
= NULL_TREE
;
3747 tree
* pp
= &field_trees
;
3748 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3749 p
!= this->fields_
->end();
3752 std::string name
= Gogo::unpack_hidden_name(p
->field_name());
3753 tree name_tree
= get_identifier_with_length(name
.data(), name
.length());
3754 tree field_type_tree
= p
->type()->get_tree(gogo
);
3755 if (field_type_tree
== error_mark_node
)
3756 return error_mark_node
;
3757 tree field
= build_decl(p
->location(), FIELD_DECL
, name_tree
,
3759 DECL_CONTEXT(field
) = type
;
3761 pp
= &DECL_CHAIN(field
);
3764 TYPE_FIELDS(type
) = field_trees
;
3771 // Initialize struct fields.
3774 Struct_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
3776 if (this->fields_
== NULL
|| this->fields_
->empty())
3782 tree ret
= build_constructor(type_tree
,
3783 VEC_alloc(constructor_elt
, gc
, 0));
3784 TREE_CONSTANT(ret
) = 1;
3789 bool is_constant
= true;
3790 bool any_fields_set
= false;
3791 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
,
3792 this->fields_
->size());
3794 tree field
= TYPE_FIELDS(type_tree
);
3795 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3796 p
!= this->fields_
->end();
3797 ++p
, field
= DECL_CHAIN(field
))
3799 tree value
= p
->type()->get_init_tree(gogo
, is_clear
);
3800 if (value
== error_mark_node
)
3801 return error_mark_node
;
3802 gcc_assert(field
!= NULL_TREE
);
3805 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
3808 any_fields_set
= true;
3809 if (!TREE_CONSTANT(value
))
3810 is_constant
= false;
3813 gcc_assert(field
== NULL_TREE
);
3815 if (!any_fields_set
)
3817 gcc_assert(is_clear
);
3818 VEC_free(constructor_elt
, gc
, init
);
3822 tree ret
= build_constructor(type_tree
, init
);
3824 TREE_CONSTANT(ret
) = 1;
3828 // The type of a struct type descriptor.
3831 Struct_type::make_struct_type_descriptor_type()
3836 Type
* tdt
= Type::make_type_descriptor_type();
3837 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3839 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3840 Type
* string_type
= Type::lookup_string_type();
3841 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
3844 Type::make_builtin_struct_type(5,
3845 "name", pointer_string_type
,
3846 "pkgPath", pointer_string_type
,
3848 "tag", pointer_string_type
,
3849 "offset", uintptr_type
);
3850 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
3852 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
3854 Struct_type
* s
= Type::make_builtin_struct_type(2,
3856 "fields", slice_type
);
3858 ret
= Type::make_builtin_named_type("StructType", s
);
3864 // Build a type descriptor for a struct type.
3867 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3869 source_location bloc
= BUILTINS_LOCATION
;
3871 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
3873 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
3875 Expression_list
* vals
= new Expression_list();
3878 const Methods
* methods
= this->methods();
3879 // A named struct should not have methods--the methods should attach
3880 // to the named type.
3881 gcc_assert(methods
== NULL
|| name
== NULL
);
3883 Struct_field_list::const_iterator ps
= fields
->begin();
3884 gcc_assert(ps
->field_name() == "commonType");
3885 vals
->push_back(this->type_descriptor_constructor(gogo
,
3886 RUNTIME_TYPE_KIND_STRUCT
,
3887 name
, methods
, true));
3890 gcc_assert(ps
->field_name() == "fields");
3892 Expression_list
* elements
= new Expression_list();
3893 elements
->reserve(this->fields_
->size());
3894 Type
* element_type
= ps
->type()->array_type()->element_type();
3895 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3896 pf
!= this->fields_
->end();
3899 const Struct_field_list
* f
= element_type
->struct_type()->fields();
3901 Expression_list
* fvals
= new Expression_list();
3904 Struct_field_list::const_iterator q
= f
->begin();
3905 gcc_assert(q
->field_name() == "name");
3906 if (pf
->is_anonymous())
3907 fvals
->push_back(Expression::make_nil(bloc
));
3910 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
3911 Expression
* s
= Expression::make_string(n
, bloc
);
3912 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3916 gcc_assert(q
->field_name() == "pkgPath");
3917 if (!Gogo::is_hidden_name(pf
->field_name()))
3918 fvals
->push_back(Expression::make_nil(bloc
));
3921 std::string n
= Gogo::hidden_name_prefix(pf
->field_name());
3922 Expression
* s
= Expression::make_string(n
, bloc
);
3923 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3927 gcc_assert(q
->field_name() == "typ");
3928 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
3931 gcc_assert(q
->field_name() == "tag");
3933 fvals
->push_back(Expression::make_nil(bloc
));
3936 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
3937 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3941 gcc_assert(q
->field_name() == "offset");
3942 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
3944 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
3946 elements
->push_back(v
);
3949 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
3952 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
3955 // Reflection string.
3958 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3960 ret
->append("struct { ");
3962 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3963 p
!= this->fields_
->end();
3966 if (p
!= this->fields_
->begin())
3968 if (p
->is_anonymous())
3969 ret
->push_back('?');
3971 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
3972 ret
->push_back(' ');
3973 this->append_reflection(p
->type(), gogo
, ret
);
3977 const std::string
& tag(p
->tag());
3979 for (std::string::const_iterator p
= tag
.begin();
3984 ret
->append("\\x00");
3985 else if (*p
== '\n')
3987 else if (*p
== '\t')
3990 ret
->append("\\\"");
3991 else if (*p
== '\\')
3992 ret
->append("\\\\");
3996 ret
->push_back('"');
4006 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4008 ret
->push_back('S');
4010 const Struct_field_list
* fields
= this->fields_
;
4013 for (Struct_field_list::const_iterator p
= fields
->begin();
4017 if (p
->is_anonymous())
4021 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
4023 snprintf(buf
, sizeof buf
, "%u_",
4024 static_cast<unsigned int>(n
.length()));
4028 this->append_mangled_name(p
->type(), gogo
, ret
);
4031 const std::string
& tag(p
->tag());
4033 for (std::string::const_iterator p
= tag
.begin();
4037 if (ISALNUM(*p
) || *p
== '_')
4042 snprintf(buf
, sizeof buf
, ".%x.",
4043 static_cast<unsigned int>(*p
));
4048 snprintf(buf
, sizeof buf
, "T%u_",
4049 static_cast<unsigned int>(out
.length()));
4056 ret
->push_back('e');
4062 Struct_type::do_export(Export
* exp
) const
4064 exp
->write_c_string("struct { ");
4065 const Struct_field_list
* fields
= this->fields_
;
4066 gcc_assert(fields
!= NULL
);
4067 for (Struct_field_list::const_iterator p
= fields
->begin();
4071 if (p
->is_anonymous())
4072 exp
->write_string("? ");
4075 exp
->write_string(p
->field_name());
4076 exp
->write_c_string(" ");
4078 exp
->write_type(p
->type());
4082 exp
->write_c_string(" ");
4083 Expression
* expr
= Expression::make_string(p
->tag(),
4085 expr
->export_expression(exp
);
4089 exp
->write_c_string("; ");
4091 exp
->write_c_string("}");
4097 Struct_type::do_import(Import
* imp
)
4099 imp
->require_c_string("struct { ");
4100 Struct_field_list
* fields
= new Struct_field_list
;
4101 if (imp
->peek_char() != '}')
4106 if (imp
->match_c_string("? "))
4110 name
= imp
->read_identifier();
4111 imp
->require_c_string(" ");
4113 Type
* ftype
= imp
->read_type();
4115 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
4117 if (imp
->peek_char() == ' ')
4120 Expression
* expr
= Expression::import_expression(imp
);
4121 String_expression
* sexpr
= expr
->string_expression();
4122 gcc_assert(sexpr
!= NULL
);
4123 sf
.set_tag(sexpr
->val());
4127 imp
->require_c_string("; ");
4128 fields
->push_back(sf
);
4129 if (imp
->peek_char() == '}')
4133 imp
->require_c_string("}");
4135 return Type::make_struct_type(fields
, imp
->location());
4138 // Make a struct type.
4141 Type::make_struct_type(Struct_field_list
* fields
,
4142 source_location location
)
4144 return new Struct_type(fields
, location
);
4147 // Class Array_type.
4149 // Whether two array types are identical.
4152 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
4154 if (!Type::are_identical(this->element_type(), t
->element_type(),
4155 errors_are_identical
, NULL
))
4158 Expression
* l1
= this->length();
4159 Expression
* l2
= t
->length();
4161 // Slices of the same element type are identical.
4162 if (l1
== NULL
&& l2
== NULL
)
4165 // Arrays of the same element type are identical if they have the
4167 if (l1
!= NULL
&& l2
!= NULL
)
4172 // Try to determine the lengths. If we can't, assume the arrays
4173 // are not identical.
4181 if (l1
->integer_constant_value(true, v1
, &type1
)
4182 && l2
->integer_constant_value(true, v2
, &type2
))
4183 ret
= mpz_cmp(v1
, v2
) == 0;
4189 // Otherwise the arrays are not identical.
4196 Array_type::do_traverse(Traverse
* traverse
)
4198 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
4199 return TRAVERSE_EXIT
;
4200 if (this->length_
!= NULL
4201 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
4202 return TRAVERSE_EXIT
;
4203 return TRAVERSE_CONTINUE
;
4206 // Check that the length is valid.
4209 Array_type::verify_length()
4211 if (this->length_
== NULL
)
4213 if (!this->length_
->is_constant())
4215 error_at(this->length_
->location(), "array bound is not constant");
4221 Type
* t
= this->length_
->type();
4222 if (t
->integer_type() != NULL
)
4226 if (!this->length_
->integer_constant_value(true, val
, &vt
))
4228 error_at(this->length_
->location(),
4229 "array bound is not constant");
4234 else if (t
->float_type() != NULL
)
4239 if (!this->length_
->float_constant_value(fval
, &vt
))
4241 error_at(this->length_
->location(),
4242 "array bound is not constant");
4246 if (!mpfr_integer_p(fval
))
4248 error_at(this->length_
->location(),
4249 "array bound truncated to integer");
4254 mpfr_get_z(val
, fval
, GMP_RNDN
);
4259 if (!t
->is_error_type())
4260 error_at(this->length_
->location(), "array bound is not numeric");
4264 if (mpz_sgn(val
) < 0)
4266 error_at(this->length_
->location(), "negative array bound");
4271 Type
* int_type
= Type::lookup_integer_type("int");
4272 int tbits
= int_type
->integer_type()->bits();
4273 int vbits
= mpz_sizeinbase(val
, 2);
4274 if (vbits
+ 1 > tbits
)
4276 error_at(this->length_
->location(), "array bound overflows");
4289 Array_type::do_verify()
4291 if (!this->verify_length())
4293 this->length_
= Expression::make_error(this->length_
->location());
4299 // Array type hash code.
4302 Array_type::do_hash_for_method(Gogo
* gogo
) const
4304 // There is no very convenient way to get a hash code for the
4306 return this->element_type_
->hash_for_method(gogo
) + 1;
4309 // See if the expression passed to make is suitable. The first
4310 // argument is required, and gives the length. An optional second
4311 // argument is permitted for the capacity.
4314 Array_type::do_check_make_expression(Expression_list
* args
,
4315 source_location location
)
4317 gcc_assert(this->length_
== NULL
);
4318 if (args
== NULL
|| args
->empty())
4320 error_at(location
, "length required when allocating a slice");
4323 else if (args
->size() > 2)
4325 error_at(location
, "too many expressions passed to make");
4330 if (!Type::check_int_value(args
->front(),
4331 _("bad length when making slice"), location
))
4334 if (args
->size() > 1)
4336 if (!Type::check_int_value(args
->back(),
4337 _("bad capacity when making slice"),
4346 // Get a tree for the length of a fixed array. The length may be
4347 // computed using a function call, so we must only evaluate it once.
4350 Array_type::get_length_tree(Gogo
* gogo
)
4352 gcc_assert(this->length_
!= NULL
);
4353 if (this->length_tree_
== NULL_TREE
)
4358 if (this->length_
->integer_constant_value(true, val
, &t
))
4361 t
= Type::lookup_integer_type("int");
4362 else if (t
->is_abstract())
4363 t
= t
->make_non_abstract_type();
4364 tree tt
= t
->get_tree(gogo
);
4365 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
4372 // Make up a translation context for the array length
4373 // expression. FIXME: This won't work in general.
4374 Translate_context
context(gogo
, NULL
, NULL
, NULL_TREE
);
4375 tree len
= this->length_
->get_tree(&context
);
4376 if (len
!= error_mark_node
)
4378 len
= convert_to_integer(integer_type_node
, len
);
4379 len
= save_expr(len
);
4381 this->length_tree_
= len
;
4384 return this->length_tree_
;
4387 // Get a tree for the type of this array. A fixed array is simply
4388 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4389 // just like an array in C. An open array is a struct with three
4390 // fields: a data pointer, the length, and the capacity.
4393 Array_type::do_get_tree(Gogo
* gogo
)
4395 if (this->length_
== NULL
)
4397 tree struct_type
= gogo
->slice_type_tree(void_type_node
);
4398 return this->fill_in_tree(gogo
, struct_type
);
4402 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4403 tree length_tree
= this->get_length_tree(gogo
);
4404 if (element_type_tree
== error_mark_node
4405 || length_tree
== error_mark_node
)
4406 return error_mark_node
;
4408 length_tree
= fold_convert(sizetype
, length_tree
);
4410 // build_index_type takes the maximum index, which is one less
4412 tree index_type
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
4416 return build_array_type(element_type_tree
, index_type
);
4420 // Fill in the fields for a slice type. This is used for named slice
4424 Array_type::fill_in_tree(Gogo
* gogo
, tree struct_type
)
4426 gcc_assert(this->length_
== NULL
);
4428 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4429 tree field
= TYPE_FIELDS(struct_type
);
4430 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
4431 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field
))
4432 && TREE_TYPE(TREE_TYPE(field
)) == void_type_node
);
4433 TREE_TYPE(field
) = build_pointer_type(element_type_tree
);
4438 // Return an initializer for an array type.
4441 Array_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
4443 if (this->length_
== NULL
)
4450 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
4452 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
4454 for (tree field
= TYPE_FIELDS(type_tree
);
4456 field
= DECL_CHAIN(field
))
4458 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
,
4461 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
4464 tree ret
= build_constructor(type_tree
, init
);
4465 TREE_CONSTANT(ret
) = 1;
4472 tree value
= this->element_type_
->get_init_tree(gogo
, is_clear
);
4475 if (value
== error_mark_node
)
4476 return error_mark_node
;
4478 tree length_tree
= this->get_length_tree(gogo
);
4479 if (length_tree
== error_mark_node
)
4480 return error_mark_node
;
4482 length_tree
= fold_convert(sizetype
, length_tree
);
4483 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
,
4484 fold_build2(MINUS_EXPR
, sizetype
,
4485 length_tree
, size_one_node
));
4486 tree ret
= build_constructor_single(type_tree
, range
, value
);
4487 if (TREE_CONSTANT(value
))
4488 TREE_CONSTANT(ret
) = 1;
4493 // Handle the builtin make function for a slice.
4496 Array_type::do_make_expression_tree(Translate_context
* context
,
4497 Expression_list
* args
,
4498 source_location location
)
4500 gcc_assert(this->length_
== NULL
);
4502 Gogo
* gogo
= context
->gogo();
4503 tree type_tree
= this->get_tree(gogo
);
4504 if (type_tree
== error_mark_node
)
4505 return error_mark_node
;
4507 tree values_field
= TYPE_FIELDS(type_tree
);
4508 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field
)),
4511 tree count_field
= DECL_CHAIN(values_field
);
4512 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field
)),
4515 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4516 if (element_type_tree
== error_mark_node
)
4517 return error_mark_node
;
4518 tree element_size_tree
= TYPE_SIZE_UNIT(element_type_tree
);
4520 tree value
= this->element_type_
->get_init_tree(gogo
, true);
4522 // The first argument is the number of elements, the optional second
4523 // argument is the capacity.
4524 gcc_assert(args
!= NULL
&& args
->size() >= 1 && args
->size() <= 2);
4526 tree length_tree
= args
->front()->get_tree(context
);
4527 if (length_tree
== error_mark_node
)
4528 return error_mark_node
;
4529 if (!DECL_P(length_tree
))
4530 length_tree
= save_expr(length_tree
);
4531 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree
)))
4532 length_tree
= convert_to_integer(TREE_TYPE(count_field
), length_tree
);
4534 tree bad_index
= Expression::check_bounds(length_tree
,
4535 TREE_TYPE(count_field
),
4536 NULL_TREE
, location
);
4538 length_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
), length_tree
);
4540 if (args
->size() == 1)
4541 capacity_tree
= length_tree
;
4544 capacity_tree
= args
->back()->get_tree(context
);
4545 if (capacity_tree
== error_mark_node
)
4546 return error_mark_node
;
4547 if (!DECL_P(capacity_tree
))
4548 capacity_tree
= save_expr(capacity_tree
);
4549 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree
)))
4550 capacity_tree
= convert_to_integer(TREE_TYPE(count_field
),
4553 bad_index
= Expression::check_bounds(capacity_tree
,
4554 TREE_TYPE(count_field
),
4555 bad_index
, location
);
4557 tree chktype
= (((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4558 > TYPE_SIZE(TREE_TYPE(length_tree
)))
4559 || ((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4560 == TYPE_SIZE(TREE_TYPE(length_tree
)))
4561 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree
))))
4562 ? TREE_TYPE(capacity_tree
)
4563 : TREE_TYPE(length_tree
));
4564 tree chk
= fold_build2_loc(location
, LT_EXPR
, boolean_type_node
,
4565 fold_convert_loc(location
, chktype
,
4567 fold_convert_loc(location
, chktype
,
4569 if (bad_index
== NULL_TREE
)
4572 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4575 capacity_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
),
4579 tree size_tree
= fold_build2_loc(location
, MULT_EXPR
, sizetype
,
4581 fold_convert_loc(location
, sizetype
,
4584 tree chk
= fold_build2_loc(location
, TRUTH_AND_EXPR
, boolean_type_node
,
4585 fold_build2_loc(location
, GT_EXPR
,
4587 fold_convert_loc(location
,
4591 fold_build2_loc(location
, LT_EXPR
,
4593 size_tree
, element_size_tree
));
4594 if (bad_index
== NULL_TREE
)
4597 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4600 tree space
= context
->gogo()->allocate_memory(this->element_type_
,
4601 size_tree
, location
);
4603 if (value
!= NULL_TREE
)
4604 space
= save_expr(space
);
4606 space
= fold_convert(TREE_TYPE(values_field
), space
);
4608 if (bad_index
!= NULL_TREE
&& bad_index
!= boolean_false_node
)
4610 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
,
4612 space
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4613 build3(COND_EXPR
, void_type_node
,
4614 bad_index
, crash
, NULL_TREE
),
4618 tree constructor
= gogo
->slice_constructor(type_tree
, space
, length_tree
,
4621 if (value
== NULL_TREE
)
4623 // The array contents are zero initialized.
4627 // The elements must be initialized.
4629 tree max
= fold_build2_loc(location
, MINUS_EXPR
, TREE_TYPE(count_field
),
4631 fold_convert_loc(location
, TREE_TYPE(count_field
),
4634 tree array_type
= build_array_type(element_type_tree
,
4635 build_index_type(max
));
4637 tree value_pointer
= fold_convert_loc(location
,
4638 build_pointer_type(array_type
),
4641 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
, max
);
4642 tree space_init
= build_constructor_single(array_type
, range
, value
);
4644 return build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4645 build2(MODIFY_EXPR
, void_type_node
,
4646 build_fold_indirect_ref(value_pointer
),
4651 // Return a tree for a pointer to the values in ARRAY.
4654 Array_type::value_pointer_tree(Gogo
*, tree array
) const
4657 if (this->length() != NULL
)
4660 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
4661 build_fold_addr_expr(array
));
4666 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
4667 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
4669 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
4672 if (TREE_CONSTANT(array
))
4673 TREE_CONSTANT(ret
) = 1;
4677 // Return a tree for the length of the array ARRAY which has this
4681 Array_type::length_tree(Gogo
* gogo
, tree array
)
4683 if (this->length_
!= NULL
)
4685 if (TREE_CODE(array
) == SAVE_EXPR
)
4686 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
4688 return omit_one_operand(integer_type_node
,
4689 this->get_length_tree(gogo
), array
);
4692 // This is an open array. We need to read the length field.
4694 tree type
= TREE_TYPE(array
);
4695 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4697 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
4698 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
4700 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4701 if (TREE_CONSTANT(array
))
4702 TREE_CONSTANT(ret
) = 1;
4706 // Return a tree for the capacity of the array ARRAY which has this
4710 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
4712 if (this->length_
!= NULL
)
4713 return omit_one_operand(sizetype
, this->get_length_tree(gogo
), array
);
4715 // This is an open array. We need to read the capacity field.
4717 tree type
= TREE_TYPE(array
);
4718 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4720 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
4721 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
4723 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4729 Array_type::do_export(Export
* exp
) const
4731 exp
->write_c_string("[");
4732 if (this->length_
!= NULL
)
4733 this->length_
->export_expression(exp
);
4734 exp
->write_c_string("] ");
4735 exp
->write_type(this->element_type_
);
4741 Array_type::do_import(Import
* imp
)
4743 imp
->require_c_string("[");
4745 if (imp
->peek_char() == ']')
4748 length
= Expression::import_expression(imp
);
4749 imp
->require_c_string("] ");
4750 Type
* element_type
= imp
->read_type();
4751 return Type::make_array_type(element_type
, length
);
4754 // The type of an array type descriptor.
4757 Array_type::make_array_type_descriptor_type()
4762 Type
* tdt
= Type::make_type_descriptor_type();
4763 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4765 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4768 Type::make_builtin_struct_type(3,
4771 "len", uintptr_type
);
4773 ret
= Type::make_builtin_named_type("ArrayType", sf
);
4779 // The type of an slice type descriptor.
4782 Array_type::make_slice_type_descriptor_type()
4787 Type
* tdt
= Type::make_type_descriptor_type();
4788 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4791 Type::make_builtin_struct_type(2,
4795 ret
= Type::make_builtin_named_type("SliceType", sf
);
4801 // Build a type descriptor for an array/slice type.
4804 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4806 if (this->length_
!= NULL
)
4807 return this->array_type_descriptor(gogo
, name
);
4809 return this->slice_type_descriptor(gogo
, name
);
4812 // Build a type descriptor for an array type.
4815 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4817 source_location bloc
= BUILTINS_LOCATION
;
4819 Type
* atdt
= Array_type::make_array_type_descriptor_type();
4821 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
4823 Expression_list
* vals
= new Expression_list();
4826 Struct_field_list::const_iterator p
= fields
->begin();
4827 gcc_assert(p
->field_name() == "commonType");
4828 vals
->push_back(this->type_descriptor_constructor(gogo
,
4829 RUNTIME_TYPE_KIND_ARRAY
,
4833 gcc_assert(p
->field_name() == "elem");
4834 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4837 gcc_assert(p
->field_name() == "len");
4838 vals
->push_back(this->length_
);
4841 gcc_assert(p
== fields
->end());
4843 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
4846 // Build a type descriptor for a slice type.
4849 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4851 source_location bloc
= BUILTINS_LOCATION
;
4853 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
4855 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4857 Expression_list
* vals
= new Expression_list();
4860 Struct_field_list::const_iterator p
= fields
->begin();
4861 gcc_assert(p
->field_name() == "commonType");
4862 vals
->push_back(this->type_descriptor_constructor(gogo
,
4863 RUNTIME_TYPE_KIND_SLICE
,
4867 gcc_assert(p
->field_name() == "elem");
4868 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4871 gcc_assert(p
== fields
->end());
4873 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4876 // Reflection string.
4879 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4881 ret
->push_back('[');
4882 if (this->length_
!= NULL
)
4887 if (!this->length_
->integer_constant_value(true, val
, &type
))
4888 error_at(this->length_
->location(),
4889 "array length must be integer constant expression");
4890 else if (mpz_cmp_si(val
, 0) < 0)
4891 error_at(this->length_
->location(), "array length is negative");
4892 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4893 error_at(this->length_
->location(), "array length is too large");
4897 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4902 ret
->push_back(']');
4904 this->append_reflection(this->element_type_
, gogo
, ret
);
4910 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4912 ret
->push_back('A');
4913 this->append_mangled_name(this->element_type_
, gogo
, ret
);
4914 if (this->length_
!= NULL
)
4919 if (!this->length_
->integer_constant_value(true, val
, &type
))
4920 error_at(this->length_
->location(),
4921 "array length must be integer constant expression");
4922 else if (mpz_cmp_si(val
, 0) < 0)
4923 error_at(this->length_
->location(), "array length is negative");
4924 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4925 error_at(this->length_
->location(), "array size is too large");
4929 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4934 ret
->push_back('e');
4937 // Make an array type.
4940 Type::make_array_type(Type
* element_type
, Expression
* length
)
4942 return new Array_type(element_type
, length
);
4950 Map_type::do_traverse(Traverse
* traverse
)
4952 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
4953 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
4954 return TRAVERSE_EXIT
;
4955 return TRAVERSE_CONTINUE
;
4958 // Check that the map type is OK.
4961 Map_type::do_verify()
4963 if (this->key_type_
->struct_type() != NULL
4964 || this->key_type_
->array_type() != NULL
)
4966 error_at(this->location_
, "invalid map key type");
4972 // Whether two map types are identical.
4975 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
4977 return (Type::are_identical(this->key_type(), t
->key_type(),
4978 errors_are_identical
, NULL
)
4979 && Type::are_identical(this->val_type(), t
->val_type(),
4980 errors_are_identical
, NULL
));
4986 Map_type::do_hash_for_method(Gogo
* gogo
) const
4988 return (this->key_type_
->hash_for_method(gogo
)
4989 + this->val_type_
->hash_for_method(gogo
)
4993 // Check that a call to the builtin make function is valid. For a map
4994 // the optional argument is the number of spaces to preallocate for
4998 Map_type::do_check_make_expression(Expression_list
* args
,
4999 source_location location
)
5001 if (args
!= NULL
&& !args
->empty())
5003 if (!Type::check_int_value(args
->front(), _("bad size when making map"),
5006 else if (args
->size() > 1)
5008 error_at(location
, "too many arguments when making map");
5015 // Get a tree for a map type. A map type is represented as a pointer
5016 // to a struct. The struct is __go_map in libgo/map.h.
5019 Map_type::do_get_tree(Gogo
* gogo
)
5021 static tree type_tree
;
5022 if (type_tree
== NULL_TREE
)
5024 tree struct_type
= make_node(RECORD_TYPE
);
5026 tree map_descriptor_type
= gogo
->map_descriptor_type();
5027 tree const_map_descriptor_type
=
5028 build_qualified_type(map_descriptor_type
, TYPE_QUAL_CONST
);
5029 tree name
= get_identifier("__descriptor");
5030 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5031 build_pointer_type(const_map_descriptor_type
));
5032 DECL_CONTEXT(field
) = struct_type
;
5033 TYPE_FIELDS(struct_type
) = field
;
5034 tree last_field
= field
;
5036 name
= get_identifier("__element_count");
5037 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5038 DECL_CONTEXT(field
) = struct_type
;
5039 DECL_CHAIN(last_field
) = field
;
5042 name
= get_identifier("__bucket_count");
5043 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5044 DECL_CONTEXT(field
) = struct_type
;
5045 DECL_CHAIN(last_field
) = field
;
5048 name
= get_identifier("__buckets");
5049 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5050 build_pointer_type(ptr_type_node
));
5051 DECL_CONTEXT(field
) = struct_type
;
5052 DECL_CHAIN(last_field
) = field
;
5054 layout_type(struct_type
);
5056 // Give the struct a name for better debugging info.
5057 name
= get_identifier("__go_map");
5058 tree type_decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
, name
,
5060 DECL_ARTIFICIAL(type_decl
) = 1;
5061 TYPE_NAME(struct_type
) = type_decl
;
5062 go_preserve_from_gc(type_decl
);
5063 rest_of_decl_compilation(type_decl
, 1, 0);
5065 type_tree
= build_pointer_type(struct_type
);
5066 go_preserve_from_gc(type_tree
);
5072 // Initialize a map.
5075 Map_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5079 return fold_convert(type_tree
, null_pointer_node
);
5082 // Return an expression for a newly allocated map.
5085 Map_type::do_make_expression_tree(Translate_context
* context
,
5086 Expression_list
* args
,
5087 source_location location
)
5089 tree bad_index
= NULL_TREE
;
5092 if (args
== NULL
|| args
->empty())
5093 expr_tree
= size_zero_node
;
5096 expr_tree
= args
->front()->get_tree(context
);
5097 if (expr_tree
== error_mark_node
)
5098 return error_mark_node
;
5099 if (!DECL_P(expr_tree
))
5100 expr_tree
= save_expr(expr_tree
);
5101 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5102 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5103 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5107 tree map_type
= this->get_tree(context
->gogo());
5109 static tree new_map_fndecl
;
5110 tree ret
= Gogo::call_builtin(&new_map_fndecl
,
5115 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type
))),
5116 context
->gogo()->map_descriptor(this),
5119 if (ret
== error_mark_node
)
5120 return error_mark_node
;
5121 // This can panic if the capacity is out of range.
5122 TREE_NOTHROW(new_map_fndecl
) = 0;
5124 if (bad_index
== NULL_TREE
)
5128 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
,
5130 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5131 build3(COND_EXPR
, void_type_node
,
5132 bad_index
, crash
, NULL_TREE
),
5137 // The type of a map type descriptor.
5140 Map_type::make_map_type_descriptor_type()
5145 Type
* tdt
= Type::make_type_descriptor_type();
5146 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5149 Type::make_builtin_struct_type(3,
5154 ret
= Type::make_builtin_named_type("MapType", sf
);
5160 // Build a type descriptor for a map type.
5163 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5165 source_location bloc
= BUILTINS_LOCATION
;
5167 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
5169 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
5171 Expression_list
* vals
= new Expression_list();
5174 Struct_field_list::const_iterator p
= fields
->begin();
5175 gcc_assert(p
->field_name() == "commonType");
5176 vals
->push_back(this->type_descriptor_constructor(gogo
,
5177 RUNTIME_TYPE_KIND_MAP
,
5181 gcc_assert(p
->field_name() == "key");
5182 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
5185 gcc_assert(p
->field_name() == "elem");
5186 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
5189 gcc_assert(p
== fields
->end());
5191 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
5194 // Reflection string for a map.
5197 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5199 ret
->append("map[");
5200 this->append_reflection(this->key_type_
, gogo
, ret
);
5202 this->append_reflection(this->val_type_
, gogo
, ret
);
5205 // Mangled name for a map.
5208 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5210 ret
->push_back('M');
5211 this->append_mangled_name(this->key_type_
, gogo
, ret
);
5213 this->append_mangled_name(this->val_type_
, gogo
, ret
);
5216 // Export a map type.
5219 Map_type::do_export(Export
* exp
) const
5221 exp
->write_c_string("map [");
5222 exp
->write_type(this->key_type_
);
5223 exp
->write_c_string("] ");
5224 exp
->write_type(this->val_type_
);
5227 // Import a map type.
5230 Map_type::do_import(Import
* imp
)
5232 imp
->require_c_string("map [");
5233 Type
* key_type
= imp
->read_type();
5234 imp
->require_c_string("] ");
5235 Type
* val_type
= imp
->read_type();
5236 return Type::make_map_type(key_type
, val_type
, imp
->location());
5242 Type::make_map_type(Type
* key_type
, Type
* val_type
, source_location location
)
5244 return new Map_type(key_type
, val_type
, location
);
5247 // Class Channel_type.
5252 Channel_type::do_hash_for_method(Gogo
* gogo
) const
5254 unsigned int ret
= 0;
5255 if (this->may_send_
)
5257 if (this->may_receive_
)
5259 if (this->element_type_
!= NULL
)
5260 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
5264 // Whether this type is the same as T.
5267 Channel_type::is_identical(const Channel_type
* t
,
5268 bool errors_are_identical
) const
5270 if (!Type::are_identical(this->element_type(), t
->element_type(),
5271 errors_are_identical
, NULL
))
5273 return (this->may_send_
== t
->may_send_
5274 && this->may_receive_
== t
->may_receive_
);
5277 // Check whether the parameters for a call to the builtin function
5278 // make are OK for a channel. A channel can take an optional single
5279 // parameter which is the buffer size.
5282 Channel_type::do_check_make_expression(Expression_list
* args
,
5283 source_location location
)
5285 if (args
!= NULL
&& !args
->empty())
5287 if (!Type::check_int_value(args
->front(),
5288 _("bad buffer size when making channel"),
5291 else if (args
->size() > 1)
5293 error_at(location
, "too many arguments when making channel");
5300 // Return the tree for a channel type. A channel is a pointer to a
5301 // __go_channel struct. The __go_channel struct is defined in
5302 // libgo/runtime/channel.h.
5305 Channel_type::do_get_tree(Gogo
*)
5307 static tree type_tree
;
5308 if (type_tree
== NULL_TREE
)
5310 tree ret
= make_node(RECORD_TYPE
);
5311 TYPE_NAME(ret
) = get_identifier("__go_channel");
5312 TYPE_STUB_DECL(ret
) = build_decl(BUILTINS_LOCATION
, TYPE_DECL
, NULL_TREE
,
5314 type_tree
= build_pointer_type(ret
);
5315 go_preserve_from_gc(type_tree
);
5320 // Initialize a channel variable.
5323 Channel_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5327 return fold_convert(type_tree
, null_pointer_node
);
5330 // Handle the builtin function make for a channel.
5333 Channel_type::do_make_expression_tree(Translate_context
* context
,
5334 Expression_list
* args
,
5335 source_location location
)
5337 Gogo
* gogo
= context
->gogo();
5338 tree channel_type
= this->get_tree(gogo
);
5340 tree element_tree
= this->element_type_
->get_tree(gogo
);
5341 tree element_size_tree
= size_in_bytes(element_tree
);
5343 tree bad_index
= NULL_TREE
;
5346 if (args
== NULL
|| args
->empty())
5347 expr_tree
= size_zero_node
;
5350 expr_tree
= args
->front()->get_tree(context
);
5351 if (expr_tree
== error_mark_node
)
5352 return error_mark_node
;
5353 if (!DECL_P(expr_tree
))
5354 expr_tree
= save_expr(expr_tree
);
5355 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5356 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5357 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5361 static tree new_channel_fndecl
;
5362 tree ret
= Gogo::call_builtin(&new_channel_fndecl
,
5371 if (ret
== error_mark_node
)
5372 return error_mark_node
;
5373 // This can panic if the capacity is out of range.
5374 TREE_NOTHROW(new_channel_fndecl
) = 0;
5376 if (bad_index
== NULL_TREE
)
5380 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
,
5382 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5383 build3(COND_EXPR
, void_type_node
,
5384 bad_index
, crash
, NULL_TREE
),
5389 // Build a type descriptor for a channel type.
5392 Channel_type::make_chan_type_descriptor_type()
5397 Type
* tdt
= Type::make_type_descriptor_type();
5398 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5400 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5403 Type::make_builtin_struct_type(3,
5406 "dir", uintptr_type
);
5408 ret
= Type::make_builtin_named_type("ChanType", sf
);
5414 // Build a type descriptor for a map type.
5417 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5419 source_location bloc
= BUILTINS_LOCATION
;
5421 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
5423 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
5425 Expression_list
* vals
= new Expression_list();
5428 Struct_field_list::const_iterator p
= fields
->begin();
5429 gcc_assert(p
->field_name() == "commonType");
5430 vals
->push_back(this->type_descriptor_constructor(gogo
,
5431 RUNTIME_TYPE_KIND_CHAN
,
5435 gcc_assert(p
->field_name() == "elem");
5436 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5439 gcc_assert(p
->field_name() == "dir");
5440 // These bits must match the ones in libgo/runtime/go-type.h.
5442 if (this->may_receive_
)
5444 if (this->may_send_
)
5447 mpz_init_set_ui(iv
, val
);
5448 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
5452 gcc_assert(p
== fields
->end());
5454 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
5457 // Reflection string.
5460 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5462 if (!this->may_send_
)
5464 ret
->append("chan");
5465 if (!this->may_receive_
)
5467 ret
->push_back(' ');
5468 this->append_reflection(this->element_type_
, gogo
, ret
);
5474 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5476 ret
->push_back('C');
5477 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5478 if (this->may_send_
)
5479 ret
->push_back('s');
5480 if (this->may_receive_
)
5481 ret
->push_back('r');
5482 ret
->push_back('e');
5488 Channel_type::do_export(Export
* exp
) const
5490 exp
->write_c_string("chan ");
5491 if (this->may_send_
&& !this->may_receive_
)
5492 exp
->write_c_string("-< ");
5493 else if (this->may_receive_
&& !this->may_send_
)
5494 exp
->write_c_string("<- ");
5495 exp
->write_type(this->element_type_
);
5501 Channel_type::do_import(Import
* imp
)
5503 imp
->require_c_string("chan ");
5507 if (imp
->match_c_string("-< "))
5511 may_receive
= false;
5513 else if (imp
->match_c_string("<- "))
5525 Type
* element_type
= imp
->read_type();
5527 return Type::make_channel_type(may_send
, may_receive
, element_type
);
5530 // Make a new channel type.
5533 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
5535 return new Channel_type(send
, receive
, element_type
);
5538 // Class Interface_type.
5543 Interface_type::do_traverse(Traverse
* traverse
)
5545 if (this->methods_
== NULL
)
5546 return TRAVERSE_CONTINUE
;
5547 return this->methods_
->traverse(traverse
);
5550 // Finalize the methods. This handles interface inheritance.
5553 Interface_type::finalize_methods()
5555 if (this->methods_
== NULL
)
5557 bool is_recursive
= false;
5560 while (from
< this->methods_
->size())
5562 const Typed_identifier
* p
= &this->methods_
->at(from
);
5563 if (!p
->name().empty())
5566 for (i
= 0; i
< to
; ++i
)
5568 if (this->methods_
->at(i
).name() == p
->name())
5570 error_at(p
->location(), "duplicate method %qs",
5571 Gogo::message_name(p
->name()).c_str());
5578 this->methods_
->set(to
, *p
);
5584 Interface_type
* it
= p
->type()->interface_type();
5587 error_at(p
->location(), "interface contains embedded non-interface");
5595 error_at(p
->location(), "invalid recursive interface");
5596 is_recursive
= true;
5601 const Typed_identifier_list
* methods
= it
->methods();
5602 if (methods
== NULL
)
5607 for (Typed_identifier_list::const_iterator q
= methods
->begin();
5608 q
!= methods
->end();
5611 if (q
->name().empty())
5613 if (q
->type() == p
->type())
5614 error_at(p
->location(), "interface inheritance loop");
5618 for (i
= from
+ 1; i
< this->methods_
->size(); ++i
)
5620 const Typed_identifier
* r
= &this->methods_
->at(i
);
5621 if (r
->name().empty() && r
->type() == q
->type())
5623 error_at(p
->location(),
5624 "inherited interface listed twice");
5628 if (i
== this->methods_
->size())
5629 this->methods_
->push_back(Typed_identifier(q
->name(),
5634 else if (this->find_method(q
->name()) == NULL
)
5635 this->methods_
->push_back(Typed_identifier(q
->name(), q
->type(),
5640 error_at(p
->location(), "inherited method %qs is ambiguous",
5641 Gogo::message_name(q
->name()).c_str());
5648 delete this->methods_
;
5649 this->methods_
= NULL
;
5653 this->methods_
->resize(to
);
5654 this->methods_
->sort_by_name();
5658 // Return the method NAME, or NULL.
5660 const Typed_identifier
*
5661 Interface_type::find_method(const std::string
& name
) const
5663 if (this->methods_
== NULL
)
5665 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5666 p
!= this->methods_
->end();
5668 if (p
->name() == name
)
5673 // Return the method index.
5676 Interface_type::method_index(const std::string
& name
) const
5678 gcc_assert(this->methods_
!= NULL
);
5680 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5681 p
!= this->methods_
->end();
5683 if (p
->name() == name
)
5688 // Return whether NAME is an unexported method, for better error
5692 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
5694 if (this->methods_
== NULL
)
5696 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5697 p
!= this->methods_
->end();
5700 const std::string
& method_name(p
->name());
5701 if (Gogo::is_hidden_name(method_name
)
5702 && name
== Gogo::unpack_hidden_name(method_name
)
5703 && gogo
->pack_hidden_name(name
, false) != method_name
)
5709 // Whether this type is identical with T.
5712 Interface_type::is_identical(const Interface_type
* t
,
5713 bool errors_are_identical
) const
5715 // We require the same methods with the same types. The methods
5716 // have already been sorted.
5717 if (this->methods() == NULL
|| t
->methods() == NULL
)
5718 return this->methods() == t
->methods();
5720 Typed_identifier_list::const_iterator p1
= this->methods()->begin();
5721 for (Typed_identifier_list::const_iterator p2
= t
->methods()->begin();
5722 p2
!= t
->methods()->end();
5725 if (p1
== this->methods()->end())
5727 if (p1
->name() != p2
->name()
5728 || !Type::are_identical(p1
->type(), p2
->type(),
5729 errors_are_identical
, NULL
))
5732 if (p1
!= this->methods()->end())
5737 // Whether we can assign the interface type T to this type. The types
5738 // are known to not be identical. An interface assignment is only
5739 // permitted if T is known to implement all methods in THIS.
5740 // Otherwise a type guard is required.
5743 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
5744 std::string
* reason
) const
5746 if (this->methods() == NULL
)
5748 for (Typed_identifier_list::const_iterator p
= this->methods()->begin();
5749 p
!= this->methods()->end();
5752 const Typed_identifier
* m
= t
->find_method(p
->name());
5758 snprintf(buf
, sizeof buf
,
5759 _("need explicit conversion; missing method %s%s%s"),
5760 open_quote
, Gogo::message_name(p
->name()).c_str(),
5762 reason
->assign(buf
);
5767 std::string subreason
;
5768 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
5772 std::string n
= Gogo::message_name(p
->name());
5773 size_t len
= 100 + n
.length() + subreason
.length();
5774 char* buf
= new char[len
];
5775 if (subreason
.empty())
5776 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5777 open_quote
, n
.c_str(), close_quote
);
5780 _("incompatible type for method %s%s%s (%s)"),
5781 open_quote
, n
.c_str(), close_quote
,
5783 reason
->assign(buf
);
5796 Interface_type::do_hash_for_method(Gogo
* gogo
) const
5798 unsigned int ret
= 0;
5799 if (this->methods_
!= NULL
)
5801 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5802 p
!= this->methods_
->end();
5805 ret
= Type::hash_string(p
->name(), ret
);
5806 ret
+= p
->type()->hash_for_method(gogo
);
5813 // Return true if T implements the interface. If it does not, and
5814 // REASON is not NULL, set *REASON to a useful error message.
5817 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
5819 if (this->methods_
== NULL
)
5822 bool is_pointer
= false;
5823 const Named_type
* nt
= t
->named_type();
5824 const Struct_type
* st
= t
->struct_type();
5825 // If we start with a named type, we don't dereference it to find
5829 const Type
* pt
= t
->points_to();
5832 // If T is a pointer to a named type, then we need to look at
5833 // the type to which it points.
5835 nt
= pt
->named_type();
5836 st
= pt
->struct_type();
5840 // If we have a named type, get the methods from it rather than from
5845 // Only named and struct types have methods.
5846 if (nt
== NULL
&& st
== NULL
)
5850 if (t
->points_to() != NULL
5851 && t
->points_to()->interface_type() != NULL
)
5852 reason
->assign(_("pointer to interface type has no methods"));
5854 reason
->assign(_("type has no methods"));
5859 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
5863 if (t
->points_to() != NULL
5864 && t
->points_to()->interface_type() != NULL
)
5865 reason
->assign(_("pointer to interface type has no methods"));
5867 reason
->assign(_("type has no methods"));
5872 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5873 p
!= this->methods_
->end();
5876 bool is_ambiguous
= false;
5877 Method
* m
= (nt
!= NULL
5878 ? nt
->method_function(p
->name(), &is_ambiguous
)
5879 : st
->method_function(p
->name(), &is_ambiguous
));
5884 std::string n
= Gogo::message_name(p
->name());
5885 size_t len
= n
.length() + 100;
5886 char* buf
= new char[len
];
5888 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
5889 open_quote
, n
.c_str(), close_quote
);
5891 snprintf(buf
, len
, _("missing method %s%s%s"),
5892 open_quote
, n
.c_str(), close_quote
);
5893 reason
->assign(buf
);
5899 Function_type
*p_fn_type
= p
->type()->function_type();
5900 Function_type
* m_fn_type
= m
->type()->function_type();
5901 gcc_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
5902 std::string subreason
;
5903 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
5907 std::string n
= Gogo::message_name(p
->name());
5908 size_t len
= 100 + n
.length() + subreason
.length();
5909 char* buf
= new char[len
];
5910 if (subreason
.empty())
5911 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5912 open_quote
, n
.c_str(), close_quote
);
5915 _("incompatible type for method %s%s%s (%s)"),
5916 open_quote
, n
.c_str(), close_quote
,
5918 reason
->assign(buf
);
5924 if (!is_pointer
&& !m
->is_value_method())
5928 std::string n
= Gogo::message_name(p
->name());
5929 size_t len
= 100 + n
.length();
5930 char* buf
= new char[len
];
5931 snprintf(buf
, len
, _("method %s%s%s requires a pointer"),
5932 open_quote
, n
.c_str(), close_quote
);
5933 reason
->assign(buf
);
5943 // Return a tree for an interface type. An interface is a pointer to
5944 // a struct. The struct has three fields. The first field is a
5945 // pointer to the type descriptor for the dynamic type of the object.
5946 // The second field is a pointer to a table of methods for the
5947 // interface to be used with the object. The third field is the value
5948 // of the object itself.
5951 Interface_type::do_get_tree(Gogo
* gogo
)
5953 if (this->methods_
== NULL
)
5955 // At the tree level, use the same type for all empty
5956 // interfaces. This lets us assign them to each other directly
5957 // without triggering GIMPLE type errors.
5958 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5959 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5960 static tree empty_interface
;
5961 return Gogo::builtin_struct(&empty_interface
, "__go_empty_interface",
5963 "__type_descriptor",
5969 return this->fill_in_tree(gogo
, make_node(RECORD_TYPE
));
5972 // Fill in the tree for an interface type. This is used for named
5976 Interface_type::fill_in_tree(Gogo
* gogo
, tree type
)
5978 gcc_assert(this->methods_
!= NULL
);
5980 // Build the type of the table of methods.
5982 tree method_table
= make_node(RECORD_TYPE
);
5984 // The first field is a pointer to the type descriptor.
5985 tree name_tree
= get_identifier("__type_descriptor");
5986 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5987 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5988 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, dtype
);
5989 DECL_CONTEXT(field
) = method_table
;
5990 TYPE_FIELDS(method_table
) = field
;
5992 std::string last_name
= "";
5993 tree
* pp
= &DECL_CHAIN(field
);
5994 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5995 p
!= this->methods_
->end();
5998 std::string name
= Gogo::unpack_hidden_name(p
->name());
5999 name_tree
= get_identifier_with_length(name
.data(), name
.length());
6000 tree field_type
= p
->type()->get_tree(gogo
);
6001 if (field_type
== error_mark_node
)
6002 return error_mark_node
;
6003 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, field_type
);
6004 DECL_CONTEXT(field
) = method_table
;
6006 pp
= &DECL_CHAIN(field
);
6007 // Sanity check: the names should be sorted.
6008 gcc_assert(p
->name() > last_name
);
6009 last_name
= p
->name();
6011 layout_type(method_table
);
6013 tree mtype
= build_pointer_type(method_table
);
6015 tree field_trees
= NULL_TREE
;
6018 name_tree
= get_identifier("__methods");
6019 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, mtype
);
6020 DECL_CONTEXT(field
) = type
;
6022 pp
= &DECL_CHAIN(field
);
6024 name_tree
= get_identifier("__object");
6025 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, ptr_type_node
);
6026 DECL_CONTEXT(field
) = type
;
6029 TYPE_FIELDS(type
) = field_trees
;
6036 // Initialization value.
6039 Interface_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
6044 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
6045 for (tree field
= TYPE_FIELDS(type_tree
);
6047 field
= DECL_CHAIN(field
))
6049 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
6051 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
6054 tree ret
= build_constructor(type_tree
, init
);
6055 TREE_CONSTANT(ret
) = 1;
6059 // The type of an interface type descriptor.
6062 Interface_type::make_interface_type_descriptor_type()
6067 Type
* tdt
= Type::make_type_descriptor_type();
6068 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6070 Type
* string_type
= Type::lookup_string_type();
6071 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6074 Type::make_builtin_struct_type(3,
6075 "name", pointer_string_type
,
6076 "pkgPath", pointer_string_type
,
6079 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
6081 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
6083 Struct_type
* s
= Type::make_builtin_struct_type(2,
6085 "methods", slice_nsm
);
6087 ret
= Type::make_builtin_named_type("InterfaceType", s
);
6093 // Build a type descriptor for an interface type.
6096 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6098 source_location bloc
= BUILTINS_LOCATION
;
6100 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
6102 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
6104 Expression_list
* ivals
= new Expression_list();
6107 Struct_field_list::const_iterator pif
= ifields
->begin();
6108 gcc_assert(pif
->field_name() == "commonType");
6109 ivals
->push_back(this->type_descriptor_constructor(gogo
,
6110 RUNTIME_TYPE_KIND_INTERFACE
,
6114 gcc_assert(pif
->field_name() == "methods");
6116 Expression_list
* methods
= new Expression_list();
6117 if (this->methods_
!= NULL
&& !this->methods_
->empty())
6119 Type
* elemtype
= pif
->type()->array_type()->element_type();
6121 methods
->reserve(this->methods_
->size());
6122 for (Typed_identifier_list::const_iterator pm
= this->methods_
->begin();
6123 pm
!= this->methods_
->end();
6126 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
6128 Expression_list
* mvals
= new Expression_list();
6131 Struct_field_list::const_iterator pmf
= mfields
->begin();
6132 gcc_assert(pmf
->field_name() == "name");
6133 std::string s
= Gogo::unpack_hidden_name(pm
->name());
6134 Expression
* e
= Expression::make_string(s
, bloc
);
6135 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6138 gcc_assert(pmf
->field_name() == "pkgPath");
6139 if (!Gogo::is_hidden_name(pm
->name()))
6140 mvals
->push_back(Expression::make_nil(bloc
));
6143 s
= Gogo::hidden_name_prefix(pm
->name());
6144 e
= Expression::make_string(s
, bloc
);
6145 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6149 gcc_assert(pmf
->field_name() == "typ");
6150 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
6153 gcc_assert(pmf
== mfields
->end());
6155 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
6157 methods
->push_back(e
);
6161 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
6165 gcc_assert(pif
== ifields
->end());
6167 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
6170 // Reflection string.
6173 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6175 ret
->append("interface {");
6176 if (this->methods_
!= NULL
)
6178 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6179 p
!= this->methods_
->end();
6182 if (p
!= this->methods_
->begin())
6184 ret
->push_back(' ');
6185 ret
->append(Gogo::unpack_hidden_name(p
->name()));
6186 std::string sub
= p
->type()->reflection(gogo
);
6187 gcc_assert(sub
.compare(0, 4, "func") == 0);
6188 sub
= sub
.substr(4);
6198 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6200 ret
->push_back('I');
6202 const Typed_identifier_list
* methods
= this->methods_
;
6203 if (methods
!= NULL
)
6205 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6206 p
!= methods
->end();
6209 std::string n
= Gogo::unpack_hidden_name(p
->name());
6211 snprintf(buf
, sizeof buf
, "%u_",
6212 static_cast<unsigned int>(n
.length()));
6215 this->append_mangled_name(p
->type(), gogo
, ret
);
6219 ret
->push_back('e');
6225 Interface_type::do_export(Export
* exp
) const
6227 exp
->write_c_string("interface { ");
6229 const Typed_identifier_list
* methods
= this->methods_
;
6230 if (methods
!= NULL
)
6232 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
6233 pm
!= methods
->end();
6236 exp
->write_string(pm
->name());
6237 exp
->write_c_string(" (");
6239 const Function_type
* fntype
= pm
->type()->function_type();
6242 const Typed_identifier_list
* parameters
= fntype
->parameters();
6243 if (parameters
!= NULL
)
6245 bool is_varargs
= fntype
->is_varargs();
6246 for (Typed_identifier_list::const_iterator pp
=
6247 parameters
->begin();
6248 pp
!= parameters
->end();
6254 exp
->write_c_string(", ");
6255 if (!is_varargs
|| pp
+ 1 != parameters
->end())
6256 exp
->write_type(pp
->type());
6259 exp
->write_c_string("...");
6260 Type
*pptype
= pp
->type();
6261 exp
->write_type(pptype
->array_type()->element_type());
6266 exp
->write_c_string(")");
6268 const Typed_identifier_list
* results
= fntype
->results();
6269 if (results
!= NULL
)
6271 exp
->write_c_string(" ");
6272 if (results
->size() == 1)
6273 exp
->write_type(results
->begin()->type());
6277 exp
->write_c_string("(");
6278 for (Typed_identifier_list::const_iterator p
=
6280 p
!= results
->end();
6286 exp
->write_c_string(", ");
6287 exp
->write_type(p
->type());
6289 exp
->write_c_string(")");
6293 exp
->write_c_string("; ");
6297 exp
->write_c_string("}");
6300 // Import an interface type.
6303 Interface_type::do_import(Import
* imp
)
6305 imp
->require_c_string("interface { ");
6307 Typed_identifier_list
* methods
= new Typed_identifier_list
;
6308 while (imp
->peek_char() != '}')
6310 std::string name
= imp
->read_identifier();
6311 imp
->require_c_string(" (");
6313 Typed_identifier_list
* parameters
;
6314 bool is_varargs
= false;
6315 if (imp
->peek_char() == ')')
6319 parameters
= new Typed_identifier_list
;
6322 if (imp
->match_c_string("..."))
6328 Type
* ptype
= imp
->read_type();
6330 ptype
= Type::make_array_type(ptype
, NULL
);
6331 parameters
->push_back(Typed_identifier(Import::import_marker
,
6332 ptype
, imp
->location()));
6333 if (imp
->peek_char() != ',')
6335 gcc_assert(!is_varargs
);
6336 imp
->require_c_string(", ");
6339 imp
->require_c_string(")");
6341 Typed_identifier_list
* results
;
6342 if (imp
->peek_char() != ' ')
6346 results
= new Typed_identifier_list
;
6348 if (imp
->peek_char() != '(')
6350 Type
* rtype
= imp
->read_type();
6351 results
->push_back(Typed_identifier(Import::import_marker
,
6352 rtype
, imp
->location()));
6359 Type
* rtype
= imp
->read_type();
6360 results
->push_back(Typed_identifier(Import::import_marker
,
6361 rtype
, imp
->location()));
6362 if (imp
->peek_char() != ',')
6364 imp
->require_c_string(", ");
6366 imp
->require_c_string(")");
6370 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
6374 fntype
->set_is_varargs();
6375 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
6377 imp
->require_c_string("; ");
6380 imp
->require_c_string("}");
6382 if (methods
->empty())
6388 return Type::make_interface_type(methods
, imp
->location());
6391 // Make an interface type.
6394 Type::make_interface_type(Typed_identifier_list
* methods
,
6395 source_location location
)
6397 return new Interface_type(methods
, location
);
6402 // Bind a method to an object.
6405 Method::bind_method(Expression
* expr
, source_location location
) const
6407 if (this->stub_
== NULL
)
6409 // When there is no stub object, the binding is determined by
6411 return this->do_bind_method(expr
, location
);
6414 Expression
* func
= Expression::make_func_reference(this->stub_
, NULL
,
6416 return Expression::make_bound_method(expr
, func
, location
);
6419 // Return the named object associated with a method. This may only be
6420 // called after methods are finalized.
6423 Method::named_object() const
6425 if (this->stub_
!= NULL
)
6427 return this->do_named_object();
6430 // Class Named_method.
6432 // The type of the method.
6435 Named_method::do_type() const
6437 if (this->named_object_
->is_function())
6438 return this->named_object_
->func_value()->type();
6439 else if (this->named_object_
->is_function_declaration())
6440 return this->named_object_
->func_declaration_value()->type();
6445 // Return the location of the method receiver.
6448 Named_method::do_receiver_location() const
6450 return this->do_type()->receiver()->location();
6453 // Bind a method to an object.
6456 Named_method::do_bind_method(Expression
* expr
, source_location location
) const
6458 Expression
* func
= Expression::make_func_reference(this->named_object_
, NULL
,
6460 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, func
,
6462 // If this is not a local method, and it does not use a stub, then
6463 // the real method expects a different type. We need to cast the
6465 if (this->depth() > 0 && !this->needs_stub_method())
6467 Function_type
* ftype
= this->do_type();
6468 gcc_assert(ftype
->is_method());
6469 Type
* frtype
= ftype
->receiver()->type();
6470 bme
->set_first_argument_type(frtype
);
6475 // Class Interface_method.
6477 // Bind a method to an object.
6480 Interface_method::do_bind_method(Expression
* expr
,
6481 source_location location
) const
6483 return Expression::make_interface_field_reference(expr
, this->name_
,
6489 // Insert a new method. Return true if it was inserted, false
6493 Methods::insert(const std::string
& name
, Method
* m
)
6495 std::pair
<Method_map::iterator
, bool> ins
=
6496 this->methods_
.insert(std::make_pair(name
, m
));
6501 Method
* old_method
= ins
.first
->second
;
6502 if (m
->depth() < old_method
->depth())
6505 ins
.first
->second
= m
;
6510 if (m
->depth() == old_method
->depth())
6511 old_method
->set_is_ambiguous();
6517 // Return the number of unambiguous methods.
6520 Methods::count() const
6523 for (Method_map::const_iterator p
= this->methods_
.begin();
6524 p
!= this->methods_
.end();
6526 if (!p
->second
->is_ambiguous())
6531 // Class Named_type.
6533 // Return the name of the type.
6536 Named_type::name() const
6538 return this->named_object_
->name();
6541 // Return the name of the type to use in an error message.
6544 Named_type::message_name() const
6546 return this->named_object_
->message_name();
6549 // Return the base type for this type. We have to be careful about
6550 // circular type definitions, which are invalid but may be seen here.
6553 Named_type::named_base()
6555 if (this->seen_
> 0)
6558 Type
* ret
= this->type_
->base();
6564 Named_type::named_base() const
6566 if (this->seen_
> 0)
6569 const Type
* ret
= this->type_
->base();
6574 // Return whether this is an error type. We have to be careful about
6575 // circular type definitions, which are invalid but may be seen here.
6578 Named_type::is_named_error_type() const
6580 if (this->seen_
> 0)
6583 bool ret
= this->type_
->is_error_type();
6588 // Add a method to this type.
6591 Named_type::add_method(const std::string
& name
, Function
* function
)
6593 if (this->local_methods_
== NULL
)
6594 this->local_methods_
= new Bindings(NULL
);
6595 return this->local_methods_
->add_function(name
, NULL
, function
);
6598 // Add a method declaration to this type.
6601 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
6602 Function_type
* type
,
6603 source_location location
)
6605 if (this->local_methods_
== NULL
)
6606 this->local_methods_
= new Bindings(NULL
);
6607 return this->local_methods_
->add_function_declaration(name
, package
, type
,
6611 // Add an existing method to this type.
6614 Named_type::add_existing_method(Named_object
* no
)
6616 if (this->local_methods_
== NULL
)
6617 this->local_methods_
= new Bindings(NULL
);
6618 this->local_methods_
->add_named_object(no
);
6621 // Look for a local method NAME, and returns its named object, or NULL
6625 Named_type::find_local_method(const std::string
& name
) const
6627 if (this->local_methods_
== NULL
)
6629 return this->local_methods_
->lookup(name
);
6632 // Return whether NAME is an unexported field or method, for better
6636 Named_type::is_unexported_local_method(Gogo
* gogo
,
6637 const std::string
& name
) const
6639 Bindings
* methods
= this->local_methods_
;
6640 if (methods
!= NULL
)
6642 for (Bindings::const_declarations_iterator p
=
6643 methods
->begin_declarations();
6644 p
!= methods
->end_declarations();
6647 if (Gogo::is_hidden_name(p
->first
)
6648 && name
== Gogo::unpack_hidden_name(p
->first
)
6649 && gogo
->pack_hidden_name(name
, false) != p
->first
)
6656 // Build the complete list of methods for this type, which means
6657 // recursively including all methods for anonymous fields. Create all
6661 Named_type::finalize_methods(Gogo
* gogo
)
6663 if (this->all_methods_
!= NULL
)
6666 if (this->local_methods_
!= NULL
6667 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
6669 const Bindings
* lm
= this->local_methods_
;
6670 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
6671 p
!= lm
->end_declarations();
6673 error_at(p
->second
->location(),
6674 "invalid pointer or interface receiver type");
6675 delete this->local_methods_
;
6676 this->local_methods_
= NULL
;
6680 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6683 // Return the method NAME, or NULL if there isn't one or if it is
6684 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6688 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6690 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6693 // Return a pointer to the interface method table for this type for
6694 // the interface INTERFACE. IS_POINTER is true if this is for a
6698 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
6701 gcc_assert(!interface
->is_empty());
6703 Interface_method_tables
** pimt
= (is_pointer
6704 ? &this->interface_method_tables_
6705 : &this->pointer_interface_method_tables_
);
6708 *pimt
= new Interface_method_tables(5);
6710 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
6711 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
6715 // This is a new entry in the hash table.
6716 gcc_assert(ins
.first
->second
== NULL_TREE
);
6717 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
6722 tree decl
= ins
.first
->second
;
6723 if (decl
== error_mark_node
)
6724 return error_mark_node
;
6725 gcc_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
6726 return build_fold_addr_expr(decl
);
6729 // Return whether a named type has any hidden fields.
6732 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
6734 if (this->seen_
> 0)
6737 bool ret
= this->type_
->has_hidden_fields(this, reason
);
6742 // Look for a use of a complete type within another type. This is
6743 // used to check that we don't try to use a type within itself.
6745 class Find_type_use
: public Traverse
6748 Find_type_use(Type
* find_type
)
6749 : Traverse(traverse_types
),
6750 find_type_(find_type
), found_(false)
6753 // Whether we found the type.
6756 { return this->found_
; }
6763 // The type we are looking for.
6765 // Whether we found the type.
6769 // Check for FIND_TYPE in TYPE.
6772 Find_type_use::type(Type
* type
)
6774 if (this->find_type_
== type
)
6776 this->found_
= true;
6777 return TRAVERSE_EXIT
;
6779 // It's OK if we see a reference to the type in any type which is
6780 // essentially a pointer: a pointer, a slice, a function, a map, or
6782 if (type
->points_to() != NULL
6783 || type
->is_open_array_type()
6784 || type
->function_type() != NULL
6785 || type
->map_type() != NULL
6786 || type
->channel_type() != NULL
)
6787 return TRAVERSE_SKIP_COMPONENTS
;
6789 // For an interface, a reference to the type in a method type should
6790 // be ignored, but we have to consider direct inheritance. When
6791 // this is called, there may be cases of direct inheritance
6792 // represented as a method with no name.
6793 if (type
->interface_type() != NULL
)
6795 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
6796 if (methods
!= NULL
)
6798 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6799 p
!= methods
->end();
6802 if (p
->name().empty())
6804 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
6805 return TRAVERSE_EXIT
;
6809 return TRAVERSE_SKIP_COMPONENTS
;
6812 return TRAVERSE_CONTINUE
;
6815 // Verify that a named type does not refer to itself.
6818 Named_type::do_verify()
6820 Find_type_use
find(this);
6821 Type::traverse(this->type_
, &find
);
6824 error_at(this->location_
, "invalid recursive type %qs",
6825 this->message_name().c_str());
6826 this->is_error_
= true;
6830 // Check whether any of the local methods overloads an existing
6831 // struct field or interface method. We don't need to check the
6832 // list of methods against itself: that is handled by the Bindings
6834 if (this->local_methods_
!= NULL
)
6836 Struct_type
* st
= this->type_
->struct_type();
6837 Interface_type
* it
= this->type_
->interface_type();
6838 bool found_dup
= false;
6839 if (st
!= NULL
|| it
!= NULL
)
6841 for (Bindings::const_declarations_iterator p
=
6842 this->local_methods_
->begin_declarations();
6843 p
!= this->local_methods_
->end_declarations();
6846 const std::string
& name(p
->first
);
6847 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
6849 error_at(p
->second
->location(),
6850 "method %qs redeclares struct field name",
6851 Gogo::message_name(name
).c_str());
6854 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
6856 error_at(p
->second
->location(),
6857 "method %qs redeclares interface method name",
6858 Gogo::message_name(name
).c_str());
6870 // Return whether this type is or contains a pointer.
6873 Named_type::do_has_pointer() const
6875 if (this->seen_
> 0)
6878 bool ret
= this->type_
->has_pointer();
6883 // Return a hash code. This is used for method lookup. We simply
6884 // hash on the name itself.
6887 Named_type::do_hash_for_method(Gogo
* gogo
) const
6889 const std::string
& name(this->named_object()->name());
6890 unsigned int ret
= Type::hash_string(name
, 0);
6892 // GOGO will be NULL here when called from Type_hash_identical.
6893 // That is OK because that is only used for internal hash tables
6894 // where we are going to be comparing named types for equality. In
6895 // other cases, which are cases where the runtime is going to
6896 // compare hash codes to see if the types are the same, we need to
6897 // include the package prefix and name in the hash.
6898 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
6900 const Package
* package
= this->named_object()->package();
6901 if (package
== NULL
)
6903 ret
= Type::hash_string(gogo
->unique_prefix(), ret
);
6904 ret
= Type::hash_string(gogo
->package_name(), ret
);
6908 ret
= Type::hash_string(package
->unique_prefix(), ret
);
6909 ret
= Type::hash_string(package
->name(), ret
);
6916 // Get a tree for a named type.
6919 Named_type::do_get_tree(Gogo
* gogo
)
6921 if (this->is_error_
)
6922 return error_mark_node
;
6924 // Go permits types to refer to themselves in various ways. Break
6925 // the recursion here.
6927 switch (this->type_
->forwarded()->classification())
6930 return error_mark_node
;
6939 // These types can not refer to themselves.
6942 // All maps and channels have the same type in GENERIC.
6943 t
= Type::get_named_type_tree(gogo
, this->type_
);
6944 if (t
== error_mark_node
)
6945 return error_mark_node
;
6946 // Build a copy to set TYPE_NAME.
6947 t
= build_variant_type_copy(t
);
6951 // GENERIC can't handle a pointer to a function type whose
6952 // return type is a pointer to the function type itself. It
6953 // goes into an infinite loop when walking the types.
6954 if (this->seen_
> 0)
6956 Function_type
* fntype
= this->type_
->function_type();
6957 if (fntype
->results() != NULL
6958 && fntype
->results()->size() == 1
6959 && fntype
->results()->front().type()->forwarded() == this)
6960 return ptr_type_node
;
6962 // We can legitimately see ourselves here twice when a named
6963 // type is defined using a struct which refers to the named
6964 // type. If we see ourselves too often we are in a loop.
6965 if (this->seen_
> 3)
6966 return ptr_type_node
;
6969 t
= Type::get_named_type_tree(gogo
, this->type_
);
6971 if (t
== error_mark_node
)
6972 return error_mark_node
;
6973 t
= build_variant_type_copy(t
);
6977 // Don't recur infinitely if a pointer type refers to itself.
6978 // Ideally we would build a circular data structure here, but
6979 // GENERIC can't handle them.
6980 if (this->seen_
> 0)
6982 if (this->type_
->points_to()->forwarded() == this)
6983 return ptr_type_node
;
6985 if (this->seen_
> 3)
6986 return ptr_type_node
;
6989 t
= Type::get_named_type_tree(gogo
, this->type_
);
6991 if (t
== error_mark_node
)
6992 return error_mark_node
;
6993 t
= build_variant_type_copy(t
);
6997 if (this->named_tree_
!= NULL_TREE
)
6998 return this->named_tree_
;
6999 t
= make_node(RECORD_TYPE
);
7000 this->named_tree_
= t
;
7001 t
= this->type_
->struct_type()->fill_in_tree(gogo
, t
);
7002 if (t
== error_mark_node
)
7003 return error_mark_node
;
7007 if (!this->is_open_array_type())
7008 t
= Type::get_named_type_tree(gogo
, this->type_
);
7011 if (this->named_tree_
!= NULL_TREE
)
7012 return this->named_tree_
;
7013 t
= gogo
->slice_type_tree(void_type_node
);
7014 this->named_tree_
= t
;
7015 t
= this->type_
->array_type()->fill_in_tree(gogo
, t
);
7017 if (t
== error_mark_node
)
7018 return error_mark_node
;
7019 t
= build_variant_type_copy(t
);
7022 case TYPE_INTERFACE
:
7023 if (this->type_
->interface_type()->is_empty())
7025 t
= Type::get_named_type_tree(gogo
, this->type_
);
7026 if (t
== error_mark_node
)
7027 return error_mark_node
;
7028 t
= build_variant_type_copy(t
);
7032 if (this->named_tree_
!= NULL_TREE
)
7033 return this->named_tree_
;
7034 t
= make_node(RECORD_TYPE
);
7035 this->named_tree_
= t
;
7036 t
= this->type_
->interface_type()->fill_in_tree(gogo
, t
);
7037 if (t
== error_mark_node
)
7038 return error_mark_node
;
7044 // When a named type T1 is defined as another named type T2,
7045 // the definition must simply be "type T1 T2". If the
7046 // definition of T2 may refer to T1, then we must simply
7047 // return the type for T2 here. It's not precisely correct,
7048 // but it's as close as we can get with GENERIC.
7050 t
= Type::get_named_type_tree(gogo
, this->type_
);
7052 if (this->seen_
> 0)
7054 if (t
== error_mark_node
)
7055 return error_mark_node
;
7056 t
= build_variant_type_copy(t
);
7061 // An undefined forwarding type. Make sure the error is
7063 this->type_
->forward_declaration_type()->real_type();
7064 return error_mark_node
;
7068 case TYPE_CALL_MULTIPLE_RESULT
:
7072 tree id
= this->named_object_
->get_id(gogo
);
7073 tree decl
= build_decl(this->location_
, TYPE_DECL
, id
, t
);
7074 TYPE_NAME(t
) = decl
;
7079 // Build a type descriptor for a named type.
7082 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7084 // If NAME is not NULL, then we don't really want the type
7085 // descriptor for this type; we want the descriptor for the
7086 // underlying type, giving it the name NAME.
7087 return this->named_type_descriptor(gogo
, this->type_
,
7088 name
== NULL
? this : name
);
7091 // Add to the reflection string. This is used mostly for the name of
7092 // the type used in a type descriptor, not for actual reflection
7096 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7098 if (this->location() != BUILTINS_LOCATION
)
7100 const Package
* package
= this->named_object_
->package();
7101 if (package
!= NULL
)
7102 ret
->append(package
->name());
7104 ret
->append(gogo
->package_name());
7105 ret
->push_back('.');
7107 if (this->in_function_
!= NULL
)
7109 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7110 ret
->push_back('$');
7112 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
7115 // Get the mangled name.
7118 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7120 Named_object
* no
= this->named_object_
;
7122 if (this->location() == BUILTINS_LOCATION
)
7123 gcc_assert(this->in_function_
== NULL
);
7126 const std::string
& unique_prefix(no
->package() == NULL
7127 ? gogo
->unique_prefix()
7128 : no
->package()->unique_prefix());
7129 const std::string
& package_name(no
->package() == NULL
7130 ? gogo
->package_name()
7131 : no
->package()->name());
7132 name
= unique_prefix
;
7133 name
.append(1, '.');
7134 name
.append(package_name
);
7135 name
.append(1, '.');
7136 if (this->in_function_
!= NULL
)
7138 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7139 name
.append(1, '$');
7142 name
.append(Gogo::unpack_hidden_name(no
->name()));
7144 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
7149 // Export the type. This is called to export a global type.
7152 Named_type::export_named_type(Export
* exp
, const std::string
&) const
7154 // We don't need to write the name of the type here, because it will
7155 // be written by Export::write_type anyhow.
7156 exp
->write_c_string("type ");
7157 exp
->write_type(this);
7158 exp
->write_c_string(";\n");
7161 // Import a named type.
7164 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
7166 imp
->require_c_string("type ");
7167 Type
*type
= imp
->read_type();
7168 *ptype
= type
->named_type();
7169 gcc_assert(*ptype
!= NULL
);
7170 imp
->require_c_string(";\n");
7173 // Export the type when it is referenced by another type. In this
7174 // case Export::export_type will already have issued the name.
7177 Named_type::do_export(Export
* exp
) const
7179 exp
->write_type(this->type_
);
7181 // To save space, we only export the methods directly attached to
7183 Bindings
* methods
= this->local_methods_
;
7184 if (methods
== NULL
)
7187 exp
->write_c_string("\n");
7188 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
7189 p
!= methods
->end_definitions();
7192 exp
->write_c_string(" ");
7193 (*p
)->export_named_object(exp
);
7196 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
7197 p
!= methods
->end_declarations();
7200 if (p
->second
->is_function_declaration())
7202 exp
->write_c_string(" ");
7203 p
->second
->export_named_object(exp
);
7208 // Make a named type.
7211 Type::make_named_type(Named_object
* named_object
, Type
* type
,
7212 source_location location
)
7214 return new Named_type(named_object
, type
, location
);
7217 // Finalize the methods for TYPE. It will be a named type or a struct
7218 // type. This sets *ALL_METHODS to the list of methods, and builds
7219 // all required stubs.
7222 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, source_location location
,
7223 Methods
** all_methods
)
7225 *all_methods
= NULL
;
7226 Types_seen types_seen
;
7227 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
7229 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
7232 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7233 // build up the struct field indexes as we go. DEPTH is the depth of
7234 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7235 // adding these methods for an anonymous field with pointer type.
7236 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7237 // calls the real method. TYPES_SEEN is used to avoid infinite
7241 Type::add_methods_for_type(const Type
* type
,
7242 const Method::Field_indexes
* field_indexes
,
7244 bool is_embedded_pointer
,
7245 bool needs_stub_method
,
7246 Types_seen
* types_seen
,
7249 // Pointer types may not have methods.
7250 if (type
->points_to() != NULL
)
7253 const Named_type
* nt
= type
->named_type();
7256 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
7262 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
7263 is_embedded_pointer
, needs_stub_method
,
7266 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
7267 is_embedded_pointer
, needs_stub_method
,
7268 types_seen
, methods
);
7270 // If we are called with depth > 0, then we are looking at an
7271 // anonymous field of a struct. If such a field has interface type,
7272 // then we need to add the interface methods. We don't want to add
7273 // them when depth == 0, because we will already handle them
7274 // following the usual rules for an interface type.
7276 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
7279 // Add the local methods for the named type NT to *METHODS. The
7280 // parameters are as for add_methods_to_type.
7283 Type::add_local_methods_for_type(const Named_type
* nt
,
7284 const Method::Field_indexes
* field_indexes
,
7286 bool is_embedded_pointer
,
7287 bool needs_stub_method
,
7290 const Bindings
* local_methods
= nt
->local_methods();
7291 if (local_methods
== NULL
)
7294 if (*methods
== NULL
)
7295 *methods
= new Methods();
7297 for (Bindings::const_declarations_iterator p
=
7298 local_methods
->begin_declarations();
7299 p
!= local_methods
->end_declarations();
7302 Named_object
* no
= p
->second
;
7303 bool is_value_method
= (is_embedded_pointer
7304 || !Type::method_expects_pointer(no
));
7305 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
7307 || (depth
> 0 && is_value_method
)));
7308 if (!(*methods
)->insert(no
->name(), m
))
7313 // Add the embedded methods for TYPE to *METHODS. These are the
7314 // methods attached to anonymous fields. The parameters are as for
7315 // add_methods_to_type.
7318 Type::add_embedded_methods_for_type(const Type
* type
,
7319 const Method::Field_indexes
* field_indexes
,
7321 bool is_embedded_pointer
,
7322 bool needs_stub_method
,
7323 Types_seen
* types_seen
,
7326 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7328 const Struct_type
* st
= type
->struct_type();
7332 const Struct_field_list
* fields
= st
->fields();
7337 for (Struct_field_list::const_iterator pf
= fields
->begin();
7338 pf
!= fields
->end();
7341 if (!pf
->is_anonymous())
7344 Type
* ftype
= pf
->type();
7345 bool is_pointer
= false;
7346 if (ftype
->points_to() != NULL
)
7348 ftype
= ftype
->points_to();
7351 Named_type
* fnt
= ftype
->named_type();
7354 // This is an error, but it will be diagnosed elsewhere.
7358 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
7359 sub_field_indexes
->next
= field_indexes
;
7360 sub_field_indexes
->field_index
= i
;
7362 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
7363 (is_embedded_pointer
|| is_pointer
),
7372 // If TYPE is an interface type, then add its method to *METHODS.
7373 // This is for interface methods attached to an anonymous field. The
7374 // parameters are as for add_methods_for_type.
7377 Type::add_interface_methods_for_type(const Type
* type
,
7378 const Method::Field_indexes
* field_indexes
,
7382 const Interface_type
* it
= type
->interface_type();
7386 const Typed_identifier_list
* imethods
= it
->methods();
7387 if (imethods
== NULL
)
7390 if (*methods
== NULL
)
7391 *methods
= new Methods();
7393 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
7394 pm
!= imethods
->end();
7397 Function_type
* fntype
= pm
->type()->function_type();
7398 gcc_assert(fntype
!= NULL
&& !fntype
->is_method());
7399 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
7400 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
7401 field_indexes
, depth
);
7402 if (!(*methods
)->insert(pm
->name(), m
))
7407 // Build stub methods for TYPE as needed. METHODS is the set of
7408 // methods for the type. A stub method may be needed when a type
7409 // inherits a method from an anonymous field. When we need the
7410 // address of the method, as in a type descriptor, we need to build a
7411 // little stub which does the required field dereferences and jumps to
7412 // the real method. LOCATION is the location of the type definition.
7415 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
7416 source_location location
)
7418 if (methods
== NULL
)
7420 for (Methods::const_iterator p
= methods
->begin();
7421 p
!= methods
->end();
7424 Method
* m
= p
->second
;
7425 if (m
->is_ambiguous() || !m
->needs_stub_method())
7428 const std::string
& name(p
->first
);
7430 // Build a stub method.
7432 const Function_type
* fntype
= m
->type();
7434 static unsigned int counter
;
7436 snprintf(buf
, sizeof buf
, "$this%u", counter
);
7439 Type
* receiver_type
= const_cast<Type
*>(type
);
7440 if (!m
->is_value_method())
7441 receiver_type
= Type::make_pointer_type(receiver_type
);
7442 source_location receiver_location
= m
->receiver_location();
7443 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
7446 const Typed_identifier_list
* fnparams
= fntype
->parameters();
7447 Typed_identifier_list
* stub_params
;
7448 if (fnparams
== NULL
|| fnparams
->empty())
7452 // We give each stub parameter a unique name.
7453 stub_params
= new Typed_identifier_list();
7454 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
7455 pp
!= fnparams
->end();
7459 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
7460 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
7466 const Typed_identifier_list
* fnresults
= fntype
->results();
7467 Typed_identifier_list
* stub_results
;
7468 if (fnresults
== NULL
|| fnresults
->empty())
7469 stub_results
= NULL
;
7472 // We create the result parameters without any names, since
7473 // we won't refer to them.
7474 stub_results
= new Typed_identifier_list();
7475 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
7476 pr
!= fnresults
->end();
7478 stub_results
->push_back(Typed_identifier("", pr
->type(),
7482 Function_type
* stub_type
= Type::make_function_type(receiver
,
7485 fntype
->location());
7486 if (fntype
->is_varargs())
7487 stub_type
->set_is_varargs();
7489 // We only create the function in the package which creates the
7491 const Package
* package
;
7492 if (type
->named_type() == NULL
)
7495 package
= type
->named_type()->named_object()->package();
7497 if (package
!= NULL
)
7498 stub
= Named_object::make_function_declaration(name
, package
,
7499 stub_type
, location
);
7502 stub
= gogo
->start_function(name
, stub_type
, false,
7503 fntype
->location());
7504 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
7505 fntype
->is_varargs(), location
);
7506 gogo
->finish_function(fntype
->location());
7509 m
->set_stub_object(stub
);
7513 // Build a stub method which adjusts the receiver as required to call
7514 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7515 // PARAMS is the list of function parameters.
7518 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
7519 const char* receiver_name
,
7520 const Typed_identifier_list
* params
,
7522 source_location location
)
7524 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
7525 gcc_assert(receiver_object
!= NULL
);
7527 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
7528 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
7529 if (expr
->type()->points_to() == NULL
)
7530 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7532 Expression_list
* arguments
;
7533 if (params
== NULL
|| params
->empty())
7537 arguments
= new Expression_list();
7538 for (Typed_identifier_list::const_iterator p
= params
->begin();
7542 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
7543 gcc_assert(param
!= NULL
);
7544 Expression
* param_ref
= Expression::make_var_reference(param
,
7546 arguments
->push_back(param_ref
);
7550 Expression
* func
= method
->bind_method(expr
, location
);
7551 gcc_assert(func
!= NULL
);
7552 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
7554 size_t count
= call
->result_count();
7556 gogo
->add_statement(Statement::make_statement(call
));
7559 Expression_list
* retvals
= new Expression_list();
7561 retvals
->push_back(call
);
7564 for (size_t i
= 0; i
< count
; ++i
)
7565 retvals
->push_back(Expression::make_call_result(call
, i
));
7567 const Function
* function
= gogo
->current_function()->func_value();
7568 const Typed_identifier_list
* results
= function
->type()->results();
7569 Statement
* retstat
= Statement::make_return_statement(results
, retvals
,
7571 gogo
->add_statement(retstat
);
7575 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7576 // in reverse order.
7579 Type::apply_field_indexes(Expression
* expr
,
7580 const Method::Field_indexes
* field_indexes
,
7581 source_location location
)
7583 if (field_indexes
== NULL
)
7585 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
7586 Struct_type
* stype
= expr
->type()->deref()->struct_type();
7587 gcc_assert(stype
!= NULL
7588 && field_indexes
->field_index
< stype
->field_count());
7589 if (expr
->type()->struct_type() == NULL
)
7591 gcc_assert(expr
->type()->points_to() != NULL
);
7592 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7593 gcc_assert(expr
->type()->struct_type() == stype
);
7595 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
7599 // Return whether NO is a method for which the receiver is a pointer.
7602 Type::method_expects_pointer(const Named_object
* no
)
7604 const Function_type
*fntype
;
7605 if (no
->is_function())
7606 fntype
= no
->func_value()->type();
7607 else if (no
->is_function_declaration())
7608 fntype
= no
->func_declaration_value()->type();
7611 return fntype
->receiver()->type()->points_to() != NULL
;
7614 // Given a set of methods for a type, METHODS, return the method NAME,
7615 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7616 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7617 // but is ambiguous (and return NULL).
7620 Type::method_function(const Methods
* methods
, const std::string
& name
,
7623 if (is_ambiguous
!= NULL
)
7624 *is_ambiguous
= false;
7625 if (methods
== NULL
)
7627 Methods::const_iterator p
= methods
->find(name
);
7628 if (p
== methods
->end())
7630 Method
* m
= p
->second
;
7631 if (m
->is_ambiguous())
7633 if (is_ambiguous
!= NULL
)
7634 *is_ambiguous
= true;
7640 // Look for field or method NAME for TYPE. Return an Expression for
7641 // the field or method bound to EXPR. If there is no such field or
7642 // method, give an appropriate error and return an error expression.
7645 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
7646 const std::string
& name
,
7647 source_location location
)
7649 if (type
->deref()->is_error_type())
7650 return Expression::make_error(location
);
7652 const Named_type
* nt
= type
->named_type();
7654 nt
= type
->deref()->named_type();
7655 const Struct_type
* st
= type
->deref()->struct_type();
7656 const Interface_type
* it
= type
->deref()->interface_type();
7658 // If this is a pointer to a pointer, then it is possible that the
7659 // pointed-to type has methods.
7663 && type
->points_to() != NULL
7664 && type
->points_to()->points_to() != NULL
)
7666 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7667 type
= type
->points_to();
7668 nt
= type
->points_to()->named_type();
7669 st
= type
->points_to()->struct_type();
7670 it
= type
->points_to()->interface_type();
7673 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
7674 || expr
->is_addressable());
7675 std::vector
<const Named_type
*> seen
;
7676 bool is_method
= false;
7677 bool found_pointer_method
= false;
7680 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
7681 &seen
, NULL
, &is_method
,
7682 &found_pointer_method
, &ambig1
, &ambig2
))
7687 gcc_assert(st
!= NULL
);
7688 if (type
->struct_type() == NULL
)
7690 gcc_assert(type
->points_to() != NULL
);
7691 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
7693 gcc_assert(expr
->type()->struct_type() == st
);
7695 ret
= st
->field_reference(expr
, name
, location
);
7697 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7698 ret
= Expression::make_interface_field_reference(expr
, name
,
7704 m
= nt
->method_function(name
, NULL
);
7705 else if (st
!= NULL
)
7706 m
= st
->method_function(name
, NULL
);
7709 gcc_assert(m
!= NULL
);
7710 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
7711 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7712 ret
= m
->bind_method(expr
, location
);
7714 gcc_assert(ret
!= NULL
);
7719 if (!ambig1
.empty())
7720 error_at(location
, "%qs is ambiguous via %qs and %qs",
7721 Gogo::message_name(name
).c_str(),
7722 Gogo::message_name(ambig1
).c_str(),
7723 Gogo::message_name(ambig2
).c_str());
7724 else if (found_pointer_method
)
7725 error_at(location
, "method requires a pointer");
7726 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
7728 ("reference to field %qs in object which "
7729 "has no fields or methods"),
7730 Gogo::message_name(name
).c_str());
7734 if (!Gogo::is_hidden_name(name
))
7735 is_unexported
= false;
7738 std::string unpacked
= Gogo::unpack_hidden_name(name
);
7740 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
7745 error_at(location
, "reference to unexported field or method %qs",
7746 Gogo::message_name(name
).c_str());
7748 error_at(location
, "reference to undefined field or method %qs",
7749 Gogo::message_name(name
).c_str());
7751 return Expression::make_error(location
);
7755 // Look in TYPE for a field or method named NAME, return true if one
7756 // is found. This looks through embedded anonymous fields and handles
7757 // ambiguity. If a method is found, sets *IS_METHOD to true;
7758 // otherwise, if a field is found, set it to false. If
7759 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7760 // whose address can not be taken. SEEN is used to avoid infinite
7761 // recursion on invalid types.
7763 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
7764 // method we couldn't use because it requires a pointer. LEVEL is
7765 // used for recursive calls, and can be NULL for a non-recursive call.
7766 // When this function returns false because it finds that the name is
7767 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
7768 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
7769 // will be unchanged.
7771 // This function just returns whether or not there is a field or
7772 // method, and whether it is a field or method. It doesn't build an
7773 // expression to refer to it. If it is a method, we then look in the
7774 // list of all methods for the type. If it is a field, the search has
7775 // to be done again, looking only for fields, and building up the
7776 // expression as we go.
7779 Type::find_field_or_method(const Type
* type
,
7780 const std::string
& name
,
7781 bool receiver_can_be_pointer
,
7782 std::vector
<const Named_type
*>* seen
,
7785 bool* found_pointer_method
,
7786 std::string
* ambig1
,
7787 std::string
* ambig2
)
7789 // Named types can have locally defined methods.
7790 const Named_type
* nt
= type
->named_type();
7791 if (nt
== NULL
&& type
->points_to() != NULL
)
7792 nt
= type
->points_to()->named_type();
7795 Named_object
* no
= nt
->find_local_method(name
);
7798 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
7804 // Record that we have found a pointer method in order to
7805 // give a better error message if we don't find anything
7807 *found_pointer_method
= true;
7810 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
7816 // We've already seen this type when searching for methods.
7822 // Interface types can have methods.
7823 const Interface_type
* it
= type
->deref()->interface_type();
7824 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7830 // Struct types can have fields. They can also inherit fields and
7831 // methods from anonymous fields.
7832 const Struct_type
* st
= type
->deref()->struct_type();
7835 const Struct_field_list
* fields
= st
->fields();
7840 seen
->push_back(nt
);
7842 int found_level
= 0;
7843 bool found_is_method
= false;
7844 std::string found_ambig1
;
7845 std::string found_ambig2
;
7846 const Struct_field
* found_parent
= NULL
;
7847 for (Struct_field_list::const_iterator pf
= fields
->begin();
7848 pf
!= fields
->end();
7851 if (pf
->field_name() == name
)
7859 if (!pf
->is_anonymous())
7862 if (pf
->type()->deref()->is_error_type()
7863 || pf
->type()->deref()->is_undefined())
7866 Named_type
* fnt
= pf
->type()->deref()->named_type();
7867 gcc_assert(fnt
!= NULL
);
7869 int sublevel
= level
== NULL
? 1 : *level
+ 1;
7871 std::string subambig1
;
7872 std::string subambig2
;
7873 bool subfound
= Type::find_field_or_method(fnt
,
7875 receiver_can_be_pointer
,
7879 found_pointer_method
,
7884 if (!subambig1
.empty())
7886 // The name was found via this field, but is ambiguous.
7887 // if the ambiguity is lower or at the same level as
7888 // anything else we have already found, then we want to
7889 // pass the ambiguity back to the caller.
7890 if (found_level
== 0 || sublevel
<= found_level
)
7892 found_ambig1
= pf
->field_name() + '.' + subambig1
;
7893 found_ambig2
= pf
->field_name() + '.' + subambig2
;
7894 found_level
= sublevel
;
7900 // The name was found via this field. Use the level to see
7901 // if we want to use this one, or whether it introduces an
7903 if (found_level
== 0 || sublevel
< found_level
)
7905 found_level
= sublevel
;
7906 found_is_method
= sub_is_method
;
7907 found_ambig1
.clear();
7908 found_ambig2
.clear();
7909 found_parent
= &*pf
;
7911 else if (sublevel
> found_level
)
7913 else if (found_ambig1
.empty())
7915 // We found an ambiguity.
7916 gcc_assert(found_parent
!= NULL
);
7917 found_ambig1
= found_parent
->field_name();
7918 found_ambig2
= pf
->field_name();
7922 // We found an ambiguity, but we already know of one.
7923 // Just report the earlier one.
7928 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7929 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7930 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7931 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7936 if (found_level
== 0)
7938 else if (!found_ambig1
.empty())
7940 gcc_assert(!found_ambig1
.empty());
7941 ambig1
->assign(found_ambig1
);
7942 ambig2
->assign(found_ambig2
);
7944 *level
= found_level
;
7950 *level
= found_level
;
7951 *is_method
= found_is_method
;
7956 // Return whether NAME is an unexported field or method for TYPE.
7959 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
7960 const std::string
& name
,
7961 std::vector
<const Named_type
*>* seen
)
7963 type
= type
->deref();
7965 const Named_type
* nt
= type
->named_type();
7968 if (nt
->is_unexported_local_method(gogo
, name
))
7971 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
7977 // We've already seen this type.
7983 const Interface_type
* it
= type
->interface_type();
7984 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
7987 const Struct_type
* st
= type
->struct_type();
7988 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
7994 const Struct_field_list
* fields
= st
->fields();
7999 seen
->push_back(nt
);
8001 for (Struct_field_list::const_iterator pf
= fields
->begin();
8002 pf
!= fields
->end();
8005 if (pf
->is_anonymous()
8006 && (!pf
->type()->deref()->is_error_type()
8007 && !pf
->type()->deref()->is_undefined()))
8009 Named_type
* subtype
= pf
->type()->deref()->named_type();
8010 gcc_assert(subtype
!= NULL
);
8011 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
8026 // Class Forward_declaration.
8028 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
8029 : Type(TYPE_FORWARD
),
8030 named_object_(named_object
->resolve()), warned_(false)
8032 gcc_assert(this->named_object_
->is_unknown()
8033 || this->named_object_
->is_type_declaration());
8036 // Return the named object.
8039 Forward_declaration_type::named_object()
8041 return this->named_object_
->resolve();
8045 Forward_declaration_type::named_object() const
8047 return this->named_object_
->resolve();
8050 // Return the name of the forward declared type.
8053 Forward_declaration_type::name() const
8055 return this->named_object()->name();
8058 // Warn about a use of a type which has been declared but not defined.
8061 Forward_declaration_type::warn() const
8063 Named_object
* no
= this->named_object_
->resolve();
8064 if (no
->is_unknown())
8066 // The name was not defined anywhere.
8069 error_at(this->named_object_
->location(),
8070 "use of undefined type %qs",
8071 no
->message_name().c_str());
8072 this->warned_
= true;
8075 else if (no
->is_type_declaration())
8077 // The name was seen as a type, but the type was never defined.
8078 if (no
->type_declaration_value()->using_type())
8080 error_at(this->named_object_
->location(),
8081 "use of undefined type %qs",
8082 no
->message_name().c_str());
8083 this->warned_
= true;
8088 // The name was defined, but not as a type.
8091 error_at(this->named_object_
->location(), "expected type");
8092 this->warned_
= true;
8097 // Get the base type of a declaration. This gives an error if the
8098 // type has not yet been defined.
8101 Forward_declaration_type::real_type()
8103 if (this->is_defined())
8104 return this->named_object()->type_value();
8108 return Type::make_error_type();
8113 Forward_declaration_type::real_type() const
8115 if (this->is_defined())
8116 return this->named_object()->type_value();
8120 return Type::make_error_type();
8124 // Return whether the base type is defined.
8127 Forward_declaration_type::is_defined() const
8129 return this->named_object()->is_type();
8132 // Add a method. This is used when methods are defined before the
8136 Forward_declaration_type::add_method(const std::string
& name
,
8139 Named_object
* no
= this->named_object();
8140 if (no
->is_unknown())
8141 no
->declare_as_type();
8142 return no
->type_declaration_value()->add_method(name
, function
);
8145 // Add a method declaration. This is used when methods are declared
8149 Forward_declaration_type::add_method_declaration(const std::string
& name
,
8150 Function_type
* type
,
8151 source_location location
)
8153 Named_object
* no
= this->named_object();
8154 if (no
->is_unknown())
8155 no
->declare_as_type();
8156 Type_declaration
* td
= no
->type_declaration_value();
8157 return td
->add_method_declaration(name
, type
, location
);
8163 Forward_declaration_type::do_traverse(Traverse
* traverse
)
8165 if (this->is_defined()
8166 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
8167 return TRAVERSE_EXIT
;
8168 return TRAVERSE_CONTINUE
;
8171 // Get a tree for the type.
8174 Forward_declaration_type::do_get_tree(Gogo
* gogo
)
8176 if (this->is_defined())
8177 return Type::get_named_type_tree(gogo
, this->real_type());
8180 return error_mark_node
;
8182 // We represent an undefined type as a struct with no fields. That
8183 // should work fine for the middle-end, since the same case can
8185 Named_object
* no
= this->named_object();
8186 tree type_tree
= make_node(RECORD_TYPE
);
8187 tree id
= no
->get_id(gogo
);
8188 tree decl
= build_decl(no
->location(), TYPE_DECL
, id
, type_tree
);
8189 TYPE_NAME(type_tree
) = decl
;
8190 layout_type(type_tree
);
8194 // Build a type descriptor for a forwarded type.
8197 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8199 if (!this->is_defined())
8200 return Expression::make_nil(BUILTINS_LOCATION
);
8203 Type
* t
= this->real_type();
8205 return this->named_type_descriptor(gogo
, t
, name
);
8207 return Expression::make_type_descriptor(t
, BUILTINS_LOCATION
);
8211 // The reflection string.
8214 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8216 this->append_reflection(this->real_type(), gogo
, ret
);
8219 // The mangled name.
8222 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8224 if (this->is_defined())
8225 this->append_mangled_name(this->real_type(), gogo
, ret
);
8228 const Named_object
* no
= this->named_object();
8230 if (no
->package() == NULL
)
8231 name
= gogo
->package_name();
8233 name
= no
->package()->name();
8235 name
+= Gogo::unpack_hidden_name(no
->name());
8237 snprintf(buf
, sizeof buf
, "N%u_",
8238 static_cast<unsigned int>(name
.length()));
8244 // Export a forward declaration. This can happen when a defined type
8245 // refers to a type which is only declared (and is presumably defined
8246 // in some other file in the same package).
8249 Forward_declaration_type::do_export(Export
*) const
8251 // If there is a base type, that should be exported instead of this.
8252 gcc_assert(!this->is_defined());
8254 // We don't output anything.
8257 // Make a forward declaration.
8260 Type::make_forward_declaration(Named_object
* named_object
)
8262 return new Forward_declaration_type(named_object
);
8265 // Class Typed_identifier_list.
8267 // Sort the entries by name.
8269 struct Typed_identifier_list_sort
8273 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
8274 { return t1
.name() < t2
.name(); }
8278 Typed_identifier_list::sort_by_name()
8280 std::sort(this->entries_
.begin(), this->entries_
.end(),
8281 Typed_identifier_list_sort());
8287 Typed_identifier_list::traverse(Traverse
* traverse
)
8289 for (Typed_identifier_list::const_iterator p
= this->begin();
8293 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
8294 return TRAVERSE_EXIT
;
8296 return TRAVERSE_CONTINUE
;
8301 Typed_identifier_list
*
8302 Typed_identifier_list::copy() const
8304 Typed_identifier_list
* ret
= new Typed_identifier_list();
8305 for (Typed_identifier_list::const_iterator p
= this->begin();
8308 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));