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 // Make sure that all structs which must be converted to the backend
3772 // representation before this one are in fact converted.
3775 Struct_type::convert_prerequisites(Gogo
* gogo
)
3777 for (std::vector
<Named_type
*>::const_iterator p
3778 = this->prerequisites_
.begin();
3779 p
!= this->prerequisites_
.end();
3781 (*p
)->get_tree(gogo
);
3784 // Initialize struct fields.
3787 Struct_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
3789 if (this->fields_
== NULL
|| this->fields_
->empty())
3795 tree ret
= build_constructor(type_tree
,
3796 VEC_alloc(constructor_elt
, gc
, 0));
3797 TREE_CONSTANT(ret
) = 1;
3802 bool is_constant
= true;
3803 bool any_fields_set
= false;
3804 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
,
3805 this->fields_
->size());
3807 tree field
= TYPE_FIELDS(type_tree
);
3808 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3809 p
!= this->fields_
->end();
3810 ++p
, field
= DECL_CHAIN(field
))
3812 tree value
= p
->type()->get_init_tree(gogo
, is_clear
);
3813 if (value
== error_mark_node
)
3814 return error_mark_node
;
3815 gcc_assert(field
!= NULL_TREE
);
3818 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
3821 any_fields_set
= true;
3822 if (!TREE_CONSTANT(value
))
3823 is_constant
= false;
3826 gcc_assert(field
== NULL_TREE
);
3828 if (!any_fields_set
)
3830 gcc_assert(is_clear
);
3831 VEC_free(constructor_elt
, gc
, init
);
3835 tree ret
= build_constructor(type_tree
, init
);
3837 TREE_CONSTANT(ret
) = 1;
3841 // The type of a struct type descriptor.
3844 Struct_type::make_struct_type_descriptor_type()
3849 Type
* tdt
= Type::make_type_descriptor_type();
3850 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3852 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3853 Type
* string_type
= Type::lookup_string_type();
3854 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
3857 Type::make_builtin_struct_type(5,
3858 "name", pointer_string_type
,
3859 "pkgPath", pointer_string_type
,
3861 "tag", pointer_string_type
,
3862 "offset", uintptr_type
);
3863 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
3865 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
3867 Struct_type
* s
= Type::make_builtin_struct_type(2,
3869 "fields", slice_type
);
3871 ret
= Type::make_builtin_named_type("StructType", s
);
3877 // Build a type descriptor for a struct type.
3880 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3882 source_location bloc
= BUILTINS_LOCATION
;
3884 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
3886 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
3888 Expression_list
* vals
= new Expression_list();
3891 const Methods
* methods
= this->methods();
3892 // A named struct should not have methods--the methods should attach
3893 // to the named type.
3894 gcc_assert(methods
== NULL
|| name
== NULL
);
3896 Struct_field_list::const_iterator ps
= fields
->begin();
3897 gcc_assert(ps
->field_name() == "commonType");
3898 vals
->push_back(this->type_descriptor_constructor(gogo
,
3899 RUNTIME_TYPE_KIND_STRUCT
,
3900 name
, methods
, true));
3903 gcc_assert(ps
->field_name() == "fields");
3905 Expression_list
* elements
= new Expression_list();
3906 elements
->reserve(this->fields_
->size());
3907 Type
* element_type
= ps
->type()->array_type()->element_type();
3908 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3909 pf
!= this->fields_
->end();
3912 const Struct_field_list
* f
= element_type
->struct_type()->fields();
3914 Expression_list
* fvals
= new Expression_list();
3917 Struct_field_list::const_iterator q
= f
->begin();
3918 gcc_assert(q
->field_name() == "name");
3919 if (pf
->is_anonymous())
3920 fvals
->push_back(Expression::make_nil(bloc
));
3923 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
3924 Expression
* s
= Expression::make_string(n
, bloc
);
3925 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3929 gcc_assert(q
->field_name() == "pkgPath");
3930 if (!Gogo::is_hidden_name(pf
->field_name()))
3931 fvals
->push_back(Expression::make_nil(bloc
));
3934 std::string n
= Gogo::hidden_name_prefix(pf
->field_name());
3935 Expression
* s
= Expression::make_string(n
, bloc
);
3936 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3940 gcc_assert(q
->field_name() == "typ");
3941 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
3944 gcc_assert(q
->field_name() == "tag");
3946 fvals
->push_back(Expression::make_nil(bloc
));
3949 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
3950 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3954 gcc_assert(q
->field_name() == "offset");
3955 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
3957 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
3959 elements
->push_back(v
);
3962 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
3965 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
3968 // Reflection string.
3971 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3973 ret
->append("struct { ");
3975 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3976 p
!= this->fields_
->end();
3979 if (p
!= this->fields_
->begin())
3981 if (p
->is_anonymous())
3982 ret
->push_back('?');
3984 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
3985 ret
->push_back(' ');
3986 this->append_reflection(p
->type(), gogo
, ret
);
3990 const std::string
& tag(p
->tag());
3992 for (std::string::const_iterator p
= tag
.begin();
3997 ret
->append("\\x00");
3998 else if (*p
== '\n')
4000 else if (*p
== '\t')
4003 ret
->append("\\\"");
4004 else if (*p
== '\\')
4005 ret
->append("\\\\");
4009 ret
->push_back('"');
4019 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4021 ret
->push_back('S');
4023 const Struct_field_list
* fields
= this->fields_
;
4026 for (Struct_field_list::const_iterator p
= fields
->begin();
4030 if (p
->is_anonymous())
4034 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
4036 snprintf(buf
, sizeof buf
, "%u_",
4037 static_cast<unsigned int>(n
.length()));
4041 this->append_mangled_name(p
->type(), gogo
, ret
);
4044 const std::string
& tag(p
->tag());
4046 for (std::string::const_iterator p
= tag
.begin();
4050 if (ISALNUM(*p
) || *p
== '_')
4055 snprintf(buf
, sizeof buf
, ".%x.",
4056 static_cast<unsigned int>(*p
));
4061 snprintf(buf
, sizeof buf
, "T%u_",
4062 static_cast<unsigned int>(out
.length()));
4069 ret
->push_back('e');
4075 Struct_type::do_export(Export
* exp
) const
4077 exp
->write_c_string("struct { ");
4078 const Struct_field_list
* fields
= this->fields_
;
4079 gcc_assert(fields
!= NULL
);
4080 for (Struct_field_list::const_iterator p
= fields
->begin();
4084 if (p
->is_anonymous())
4085 exp
->write_string("? ");
4088 exp
->write_string(p
->field_name());
4089 exp
->write_c_string(" ");
4091 exp
->write_type(p
->type());
4095 exp
->write_c_string(" ");
4096 Expression
* expr
= Expression::make_string(p
->tag(),
4098 expr
->export_expression(exp
);
4102 exp
->write_c_string("; ");
4104 exp
->write_c_string("}");
4110 Struct_type::do_import(Import
* imp
)
4112 imp
->require_c_string("struct { ");
4113 Struct_field_list
* fields
= new Struct_field_list
;
4114 if (imp
->peek_char() != '}')
4119 if (imp
->match_c_string("? "))
4123 name
= imp
->read_identifier();
4124 imp
->require_c_string(" ");
4126 Type
* ftype
= imp
->read_type();
4128 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
4130 if (imp
->peek_char() == ' ')
4133 Expression
* expr
= Expression::import_expression(imp
);
4134 String_expression
* sexpr
= expr
->string_expression();
4135 gcc_assert(sexpr
!= NULL
);
4136 sf
.set_tag(sexpr
->val());
4140 imp
->require_c_string("; ");
4141 fields
->push_back(sf
);
4142 if (imp
->peek_char() == '}')
4146 imp
->require_c_string("}");
4148 return Type::make_struct_type(fields
, imp
->location());
4151 // Make a struct type.
4154 Type::make_struct_type(Struct_field_list
* fields
,
4155 source_location location
)
4157 return new Struct_type(fields
, location
);
4160 // Class Array_type.
4162 // Whether two array types are identical.
4165 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
4167 if (!Type::are_identical(this->element_type(), t
->element_type(),
4168 errors_are_identical
, NULL
))
4171 Expression
* l1
= this->length();
4172 Expression
* l2
= t
->length();
4174 // Slices of the same element type are identical.
4175 if (l1
== NULL
&& l2
== NULL
)
4178 // Arrays of the same element type are identical if they have the
4180 if (l1
!= NULL
&& l2
!= NULL
)
4185 // Try to determine the lengths. If we can't, assume the arrays
4186 // are not identical.
4194 if (l1
->integer_constant_value(true, v1
, &type1
)
4195 && l2
->integer_constant_value(true, v2
, &type2
))
4196 ret
= mpz_cmp(v1
, v2
) == 0;
4202 // Otherwise the arrays are not identical.
4209 Array_type::do_traverse(Traverse
* traverse
)
4211 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
4212 return TRAVERSE_EXIT
;
4213 if (this->length_
!= NULL
4214 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
4215 return TRAVERSE_EXIT
;
4216 return TRAVERSE_CONTINUE
;
4219 // Check that the length is valid.
4222 Array_type::verify_length()
4224 if (this->length_
== NULL
)
4226 if (!this->length_
->is_constant())
4228 error_at(this->length_
->location(), "array bound is not constant");
4234 Type
* t
= this->length_
->type();
4235 if (t
->integer_type() != NULL
)
4239 if (!this->length_
->integer_constant_value(true, val
, &vt
))
4241 error_at(this->length_
->location(),
4242 "array bound is not constant");
4247 else if (t
->float_type() != NULL
)
4252 if (!this->length_
->float_constant_value(fval
, &vt
))
4254 error_at(this->length_
->location(),
4255 "array bound is not constant");
4259 if (!mpfr_integer_p(fval
))
4261 error_at(this->length_
->location(),
4262 "array bound truncated to integer");
4267 mpfr_get_z(val
, fval
, GMP_RNDN
);
4272 if (!t
->is_error_type())
4273 error_at(this->length_
->location(), "array bound is not numeric");
4277 if (mpz_sgn(val
) < 0)
4279 error_at(this->length_
->location(), "negative array bound");
4284 Type
* int_type
= Type::lookup_integer_type("int");
4285 int tbits
= int_type
->integer_type()->bits();
4286 int vbits
= mpz_sizeinbase(val
, 2);
4287 if (vbits
+ 1 > tbits
)
4289 error_at(this->length_
->location(), "array bound overflows");
4302 Array_type::do_verify()
4304 if (!this->verify_length())
4306 this->length_
= Expression::make_error(this->length_
->location());
4312 // Array type hash code.
4315 Array_type::do_hash_for_method(Gogo
* gogo
) const
4317 // There is no very convenient way to get a hash code for the
4319 return this->element_type_
->hash_for_method(gogo
) + 1;
4322 // See if the expression passed to make is suitable. The first
4323 // argument is required, and gives the length. An optional second
4324 // argument is permitted for the capacity.
4327 Array_type::do_check_make_expression(Expression_list
* args
,
4328 source_location location
)
4330 gcc_assert(this->length_
== NULL
);
4331 if (args
== NULL
|| args
->empty())
4333 error_at(location
, "length required when allocating a slice");
4336 else if (args
->size() > 2)
4338 error_at(location
, "too many expressions passed to make");
4343 if (!Type::check_int_value(args
->front(),
4344 _("bad length when making slice"), location
))
4347 if (args
->size() > 1)
4349 if (!Type::check_int_value(args
->back(),
4350 _("bad capacity when making slice"),
4359 // Get a tree for the length of a fixed array. The length may be
4360 // computed using a function call, so we must only evaluate it once.
4363 Array_type::get_length_tree(Gogo
* gogo
)
4365 gcc_assert(this->length_
!= NULL
);
4366 if (this->length_tree_
== NULL_TREE
)
4371 if (this->length_
->integer_constant_value(true, val
, &t
))
4374 t
= Type::lookup_integer_type("int");
4375 else if (t
->is_abstract())
4376 t
= t
->make_non_abstract_type();
4377 tree tt
= t
->get_tree(gogo
);
4378 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
4385 // Make up a translation context for the array length
4386 // expression. FIXME: This won't work in general.
4387 Translate_context
context(gogo
, NULL
, NULL
, NULL_TREE
);
4388 tree len
= this->length_
->get_tree(&context
);
4389 if (len
!= error_mark_node
)
4391 len
= convert_to_integer(integer_type_node
, len
);
4392 len
= save_expr(len
);
4394 this->length_tree_
= len
;
4397 return this->length_tree_
;
4400 // Get a tree for the type of this array. A fixed array is simply
4401 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4402 // just like an array in C. An open array is a struct with three
4403 // fields: a data pointer, the length, and the capacity.
4406 Array_type::do_get_tree(Gogo
* gogo
)
4408 if (this->length_
== NULL
)
4410 tree struct_type
= gogo
->slice_type_tree(void_type_node
);
4411 return this->fill_in_tree(gogo
, struct_type
);
4415 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4416 tree length_tree
= this->get_length_tree(gogo
);
4417 if (element_type_tree
== error_mark_node
4418 || length_tree
== error_mark_node
)
4419 return error_mark_node
;
4421 length_tree
= fold_convert(sizetype
, length_tree
);
4423 // build_index_type takes the maximum index, which is one less
4425 tree index_type
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
4429 return build_array_type(element_type_tree
, index_type
);
4433 // Fill in the fields for a slice type. This is used for named slice
4437 Array_type::fill_in_tree(Gogo
* gogo
, tree struct_type
)
4439 gcc_assert(this->length_
== NULL
);
4441 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4442 tree field
= TYPE_FIELDS(struct_type
);
4443 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
4444 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field
))
4445 && TREE_TYPE(TREE_TYPE(field
)) == void_type_node
);
4446 TREE_TYPE(field
) = build_pointer_type(element_type_tree
);
4451 // Return an initializer for an array type.
4454 Array_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
4456 if (this->length_
== NULL
)
4463 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
4465 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
4467 for (tree field
= TYPE_FIELDS(type_tree
);
4469 field
= DECL_CHAIN(field
))
4471 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
,
4474 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
4477 tree ret
= build_constructor(type_tree
, init
);
4478 TREE_CONSTANT(ret
) = 1;
4485 tree value
= this->element_type_
->get_init_tree(gogo
, is_clear
);
4488 if (value
== error_mark_node
)
4489 return error_mark_node
;
4491 tree length_tree
= this->get_length_tree(gogo
);
4492 if (length_tree
== error_mark_node
)
4493 return error_mark_node
;
4495 length_tree
= fold_convert(sizetype
, length_tree
);
4496 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
,
4497 fold_build2(MINUS_EXPR
, sizetype
,
4498 length_tree
, size_one_node
));
4499 tree ret
= build_constructor_single(type_tree
, range
, value
);
4500 if (TREE_CONSTANT(value
))
4501 TREE_CONSTANT(ret
) = 1;
4506 // Handle the builtin make function for a slice.
4509 Array_type::do_make_expression_tree(Translate_context
* context
,
4510 Expression_list
* args
,
4511 source_location location
)
4513 gcc_assert(this->length_
== NULL
);
4515 Gogo
* gogo
= context
->gogo();
4516 tree type_tree
= this->get_tree(gogo
);
4517 if (type_tree
== error_mark_node
)
4518 return error_mark_node
;
4520 tree values_field
= TYPE_FIELDS(type_tree
);
4521 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field
)),
4524 tree count_field
= DECL_CHAIN(values_field
);
4525 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field
)),
4528 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4529 if (element_type_tree
== error_mark_node
)
4530 return error_mark_node
;
4531 tree element_size_tree
= TYPE_SIZE_UNIT(element_type_tree
);
4533 tree value
= this->element_type_
->get_init_tree(gogo
, true);
4535 // The first argument is the number of elements, the optional second
4536 // argument is the capacity.
4537 gcc_assert(args
!= NULL
&& args
->size() >= 1 && args
->size() <= 2);
4539 tree length_tree
= args
->front()->get_tree(context
);
4540 if (length_tree
== error_mark_node
)
4541 return error_mark_node
;
4542 if (!DECL_P(length_tree
))
4543 length_tree
= save_expr(length_tree
);
4544 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree
)))
4545 length_tree
= convert_to_integer(TREE_TYPE(count_field
), length_tree
);
4547 tree bad_index
= Expression::check_bounds(length_tree
,
4548 TREE_TYPE(count_field
),
4549 NULL_TREE
, location
);
4551 length_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
), length_tree
);
4553 if (args
->size() == 1)
4554 capacity_tree
= length_tree
;
4557 capacity_tree
= args
->back()->get_tree(context
);
4558 if (capacity_tree
== error_mark_node
)
4559 return error_mark_node
;
4560 if (!DECL_P(capacity_tree
))
4561 capacity_tree
= save_expr(capacity_tree
);
4562 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree
)))
4563 capacity_tree
= convert_to_integer(TREE_TYPE(count_field
),
4566 bad_index
= Expression::check_bounds(capacity_tree
,
4567 TREE_TYPE(count_field
),
4568 bad_index
, location
);
4570 tree chktype
= (((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4571 > TYPE_SIZE(TREE_TYPE(length_tree
)))
4572 || ((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4573 == TYPE_SIZE(TREE_TYPE(length_tree
)))
4574 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree
))))
4575 ? TREE_TYPE(capacity_tree
)
4576 : TREE_TYPE(length_tree
));
4577 tree chk
= fold_build2_loc(location
, LT_EXPR
, boolean_type_node
,
4578 fold_convert_loc(location
, chktype
,
4580 fold_convert_loc(location
, chktype
,
4582 if (bad_index
== NULL_TREE
)
4585 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4588 capacity_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
),
4592 tree size_tree
= fold_build2_loc(location
, MULT_EXPR
, sizetype
,
4594 fold_convert_loc(location
, sizetype
,
4597 tree chk
= fold_build2_loc(location
, TRUTH_AND_EXPR
, boolean_type_node
,
4598 fold_build2_loc(location
, GT_EXPR
,
4600 fold_convert_loc(location
,
4604 fold_build2_loc(location
, LT_EXPR
,
4606 size_tree
, element_size_tree
));
4607 if (bad_index
== NULL_TREE
)
4610 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4613 tree space
= context
->gogo()->allocate_memory(this->element_type_
,
4614 size_tree
, location
);
4616 if (value
!= NULL_TREE
)
4617 space
= save_expr(space
);
4619 space
= fold_convert(TREE_TYPE(values_field
), space
);
4621 if (bad_index
!= NULL_TREE
&& bad_index
!= boolean_false_node
)
4623 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
,
4625 space
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4626 build3(COND_EXPR
, void_type_node
,
4627 bad_index
, crash
, NULL_TREE
),
4631 tree constructor
= gogo
->slice_constructor(type_tree
, space
, length_tree
,
4634 if (value
== NULL_TREE
)
4636 // The array contents are zero initialized.
4640 // The elements must be initialized.
4642 tree max
= fold_build2_loc(location
, MINUS_EXPR
, TREE_TYPE(count_field
),
4644 fold_convert_loc(location
, TREE_TYPE(count_field
),
4647 tree array_type
= build_array_type(element_type_tree
,
4648 build_index_type(max
));
4650 tree value_pointer
= fold_convert_loc(location
,
4651 build_pointer_type(array_type
),
4654 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
, max
);
4655 tree space_init
= build_constructor_single(array_type
, range
, value
);
4657 return build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4658 build2(MODIFY_EXPR
, void_type_node
,
4659 build_fold_indirect_ref(value_pointer
),
4664 // Return a tree for a pointer to the values in ARRAY.
4667 Array_type::value_pointer_tree(Gogo
*, tree array
) const
4670 if (this->length() != NULL
)
4673 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
4674 build_fold_addr_expr(array
));
4679 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
4680 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
4682 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
4685 if (TREE_CONSTANT(array
))
4686 TREE_CONSTANT(ret
) = 1;
4690 // Return a tree for the length of the array ARRAY which has this
4694 Array_type::length_tree(Gogo
* gogo
, tree array
)
4696 if (this->length_
!= NULL
)
4698 if (TREE_CODE(array
) == SAVE_EXPR
)
4699 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
4701 return omit_one_operand(integer_type_node
,
4702 this->get_length_tree(gogo
), array
);
4705 // This is an open array. We need to read the length field.
4707 tree type
= TREE_TYPE(array
);
4708 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4710 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
4711 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
4713 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4714 if (TREE_CONSTANT(array
))
4715 TREE_CONSTANT(ret
) = 1;
4719 // Return a tree for the capacity of the array ARRAY which has this
4723 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
4725 if (this->length_
!= NULL
)
4726 return omit_one_operand(sizetype
, this->get_length_tree(gogo
), array
);
4728 // This is an open array. We need to read the capacity field.
4730 tree type
= TREE_TYPE(array
);
4731 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4733 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
4734 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
4736 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4742 Array_type::do_export(Export
* exp
) const
4744 exp
->write_c_string("[");
4745 if (this->length_
!= NULL
)
4746 this->length_
->export_expression(exp
);
4747 exp
->write_c_string("] ");
4748 exp
->write_type(this->element_type_
);
4754 Array_type::do_import(Import
* imp
)
4756 imp
->require_c_string("[");
4758 if (imp
->peek_char() == ']')
4761 length
= Expression::import_expression(imp
);
4762 imp
->require_c_string("] ");
4763 Type
* element_type
= imp
->read_type();
4764 return Type::make_array_type(element_type
, length
);
4767 // The type of an array type descriptor.
4770 Array_type::make_array_type_descriptor_type()
4775 Type
* tdt
= Type::make_type_descriptor_type();
4776 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4778 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4781 Type::make_builtin_struct_type(3,
4784 "len", uintptr_type
);
4786 ret
= Type::make_builtin_named_type("ArrayType", sf
);
4792 // The type of an slice type descriptor.
4795 Array_type::make_slice_type_descriptor_type()
4800 Type
* tdt
= Type::make_type_descriptor_type();
4801 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4804 Type::make_builtin_struct_type(2,
4808 ret
= Type::make_builtin_named_type("SliceType", sf
);
4814 // Build a type descriptor for an array/slice type.
4817 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4819 if (this->length_
!= NULL
)
4820 return this->array_type_descriptor(gogo
, name
);
4822 return this->slice_type_descriptor(gogo
, name
);
4825 // Build a type descriptor for an array type.
4828 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4830 source_location bloc
= BUILTINS_LOCATION
;
4832 Type
* atdt
= Array_type::make_array_type_descriptor_type();
4834 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
4836 Expression_list
* vals
= new Expression_list();
4839 Struct_field_list::const_iterator p
= fields
->begin();
4840 gcc_assert(p
->field_name() == "commonType");
4841 vals
->push_back(this->type_descriptor_constructor(gogo
,
4842 RUNTIME_TYPE_KIND_ARRAY
,
4846 gcc_assert(p
->field_name() == "elem");
4847 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4850 gcc_assert(p
->field_name() == "len");
4851 vals
->push_back(this->length_
);
4854 gcc_assert(p
== fields
->end());
4856 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
4859 // Build a type descriptor for a slice type.
4862 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4864 source_location bloc
= BUILTINS_LOCATION
;
4866 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
4868 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4870 Expression_list
* vals
= new Expression_list();
4873 Struct_field_list::const_iterator p
= fields
->begin();
4874 gcc_assert(p
->field_name() == "commonType");
4875 vals
->push_back(this->type_descriptor_constructor(gogo
,
4876 RUNTIME_TYPE_KIND_SLICE
,
4880 gcc_assert(p
->field_name() == "elem");
4881 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4884 gcc_assert(p
== fields
->end());
4886 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4889 // Reflection string.
4892 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4894 ret
->push_back('[');
4895 if (this->length_
!= NULL
)
4900 if (!this->length_
->integer_constant_value(true, val
, &type
))
4901 error_at(this->length_
->location(),
4902 "array length must be integer constant expression");
4903 else if (mpz_cmp_si(val
, 0) < 0)
4904 error_at(this->length_
->location(), "array length is negative");
4905 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4906 error_at(this->length_
->location(), "array length is too large");
4910 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4915 ret
->push_back(']');
4917 this->append_reflection(this->element_type_
, gogo
, ret
);
4923 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4925 ret
->push_back('A');
4926 this->append_mangled_name(this->element_type_
, gogo
, ret
);
4927 if (this->length_
!= NULL
)
4932 if (!this->length_
->integer_constant_value(true, val
, &type
))
4933 error_at(this->length_
->location(),
4934 "array length must be integer constant expression");
4935 else if (mpz_cmp_si(val
, 0) < 0)
4936 error_at(this->length_
->location(), "array length is negative");
4937 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4938 error_at(this->length_
->location(), "array size is too large");
4942 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4947 ret
->push_back('e');
4950 // Make an array type.
4953 Type::make_array_type(Type
* element_type
, Expression
* length
)
4955 return new Array_type(element_type
, length
);
4963 Map_type::do_traverse(Traverse
* traverse
)
4965 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
4966 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
4967 return TRAVERSE_EXIT
;
4968 return TRAVERSE_CONTINUE
;
4971 // Check that the map type is OK.
4974 Map_type::do_verify()
4976 if (this->key_type_
->struct_type() != NULL
4977 || this->key_type_
->array_type() != NULL
)
4979 error_at(this->location_
, "invalid map key type");
4985 // Whether two map types are identical.
4988 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
4990 return (Type::are_identical(this->key_type(), t
->key_type(),
4991 errors_are_identical
, NULL
)
4992 && Type::are_identical(this->val_type(), t
->val_type(),
4993 errors_are_identical
, NULL
));
4999 Map_type::do_hash_for_method(Gogo
* gogo
) const
5001 return (this->key_type_
->hash_for_method(gogo
)
5002 + this->val_type_
->hash_for_method(gogo
)
5006 // Check that a call to the builtin make function is valid. For a map
5007 // the optional argument is the number of spaces to preallocate for
5011 Map_type::do_check_make_expression(Expression_list
* args
,
5012 source_location location
)
5014 if (args
!= NULL
&& !args
->empty())
5016 if (!Type::check_int_value(args
->front(), _("bad size when making map"),
5019 else if (args
->size() > 1)
5021 error_at(location
, "too many arguments when making map");
5028 // Get a tree for a map type. A map type is represented as a pointer
5029 // to a struct. The struct is __go_map in libgo/map.h.
5032 Map_type::do_get_tree(Gogo
* gogo
)
5034 static tree type_tree
;
5035 if (type_tree
== NULL_TREE
)
5037 tree struct_type
= make_node(RECORD_TYPE
);
5039 tree map_descriptor_type
= gogo
->map_descriptor_type();
5040 tree const_map_descriptor_type
=
5041 build_qualified_type(map_descriptor_type
, TYPE_QUAL_CONST
);
5042 tree name
= get_identifier("__descriptor");
5043 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5044 build_pointer_type(const_map_descriptor_type
));
5045 DECL_CONTEXT(field
) = struct_type
;
5046 TYPE_FIELDS(struct_type
) = field
;
5047 tree last_field
= field
;
5049 name
= get_identifier("__element_count");
5050 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5051 DECL_CONTEXT(field
) = struct_type
;
5052 DECL_CHAIN(last_field
) = field
;
5055 name
= get_identifier("__bucket_count");
5056 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5057 DECL_CONTEXT(field
) = struct_type
;
5058 DECL_CHAIN(last_field
) = field
;
5061 name
= get_identifier("__buckets");
5062 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5063 build_pointer_type(ptr_type_node
));
5064 DECL_CONTEXT(field
) = struct_type
;
5065 DECL_CHAIN(last_field
) = field
;
5067 layout_type(struct_type
);
5069 // Give the struct a name for better debugging info.
5070 name
= get_identifier("__go_map");
5071 tree type_decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
, name
,
5073 DECL_ARTIFICIAL(type_decl
) = 1;
5074 TYPE_NAME(struct_type
) = type_decl
;
5075 go_preserve_from_gc(type_decl
);
5076 rest_of_decl_compilation(type_decl
, 1, 0);
5078 type_tree
= build_pointer_type(struct_type
);
5079 go_preserve_from_gc(type_tree
);
5085 // Initialize a map.
5088 Map_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5092 return fold_convert(type_tree
, null_pointer_node
);
5095 // Return an expression for a newly allocated map.
5098 Map_type::do_make_expression_tree(Translate_context
* context
,
5099 Expression_list
* args
,
5100 source_location location
)
5102 tree bad_index
= NULL_TREE
;
5105 if (args
== NULL
|| args
->empty())
5106 expr_tree
= size_zero_node
;
5109 expr_tree
= args
->front()->get_tree(context
);
5110 if (expr_tree
== error_mark_node
)
5111 return error_mark_node
;
5112 if (!DECL_P(expr_tree
))
5113 expr_tree
= save_expr(expr_tree
);
5114 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5115 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5116 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5120 tree map_type
= this->get_tree(context
->gogo());
5122 static tree new_map_fndecl
;
5123 tree ret
= Gogo::call_builtin(&new_map_fndecl
,
5128 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type
))),
5129 context
->gogo()->map_descriptor(this),
5132 if (ret
== error_mark_node
)
5133 return error_mark_node
;
5134 // This can panic if the capacity is out of range.
5135 TREE_NOTHROW(new_map_fndecl
) = 0;
5137 if (bad_index
== NULL_TREE
)
5141 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
,
5143 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5144 build3(COND_EXPR
, void_type_node
,
5145 bad_index
, crash
, NULL_TREE
),
5150 // The type of a map type descriptor.
5153 Map_type::make_map_type_descriptor_type()
5158 Type
* tdt
= Type::make_type_descriptor_type();
5159 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5162 Type::make_builtin_struct_type(3,
5167 ret
= Type::make_builtin_named_type("MapType", sf
);
5173 // Build a type descriptor for a map type.
5176 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5178 source_location bloc
= BUILTINS_LOCATION
;
5180 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
5182 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
5184 Expression_list
* vals
= new Expression_list();
5187 Struct_field_list::const_iterator p
= fields
->begin();
5188 gcc_assert(p
->field_name() == "commonType");
5189 vals
->push_back(this->type_descriptor_constructor(gogo
,
5190 RUNTIME_TYPE_KIND_MAP
,
5194 gcc_assert(p
->field_name() == "key");
5195 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
5198 gcc_assert(p
->field_name() == "elem");
5199 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
5202 gcc_assert(p
== fields
->end());
5204 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
5207 // Reflection string for a map.
5210 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5212 ret
->append("map[");
5213 this->append_reflection(this->key_type_
, gogo
, ret
);
5215 this->append_reflection(this->val_type_
, gogo
, ret
);
5218 // Mangled name for a map.
5221 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5223 ret
->push_back('M');
5224 this->append_mangled_name(this->key_type_
, gogo
, ret
);
5226 this->append_mangled_name(this->val_type_
, gogo
, ret
);
5229 // Export a map type.
5232 Map_type::do_export(Export
* exp
) const
5234 exp
->write_c_string("map [");
5235 exp
->write_type(this->key_type_
);
5236 exp
->write_c_string("] ");
5237 exp
->write_type(this->val_type_
);
5240 // Import a map type.
5243 Map_type::do_import(Import
* imp
)
5245 imp
->require_c_string("map [");
5246 Type
* key_type
= imp
->read_type();
5247 imp
->require_c_string("] ");
5248 Type
* val_type
= imp
->read_type();
5249 return Type::make_map_type(key_type
, val_type
, imp
->location());
5255 Type::make_map_type(Type
* key_type
, Type
* val_type
, source_location location
)
5257 return new Map_type(key_type
, val_type
, location
);
5260 // Class Channel_type.
5265 Channel_type::do_hash_for_method(Gogo
* gogo
) const
5267 unsigned int ret
= 0;
5268 if (this->may_send_
)
5270 if (this->may_receive_
)
5272 if (this->element_type_
!= NULL
)
5273 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
5277 // Whether this type is the same as T.
5280 Channel_type::is_identical(const Channel_type
* t
,
5281 bool errors_are_identical
) const
5283 if (!Type::are_identical(this->element_type(), t
->element_type(),
5284 errors_are_identical
, NULL
))
5286 return (this->may_send_
== t
->may_send_
5287 && this->may_receive_
== t
->may_receive_
);
5290 // Check whether the parameters for a call to the builtin function
5291 // make are OK for a channel. A channel can take an optional single
5292 // parameter which is the buffer size.
5295 Channel_type::do_check_make_expression(Expression_list
* args
,
5296 source_location location
)
5298 if (args
!= NULL
&& !args
->empty())
5300 if (!Type::check_int_value(args
->front(),
5301 _("bad buffer size when making channel"),
5304 else if (args
->size() > 1)
5306 error_at(location
, "too many arguments when making channel");
5313 // Return the tree for a channel type. A channel is a pointer to a
5314 // __go_channel struct. The __go_channel struct is defined in
5315 // libgo/runtime/channel.h.
5318 Channel_type::do_get_tree(Gogo
*)
5320 static tree type_tree
;
5321 if (type_tree
== NULL_TREE
)
5323 tree ret
= make_node(RECORD_TYPE
);
5324 TYPE_NAME(ret
) = get_identifier("__go_channel");
5325 TYPE_STUB_DECL(ret
) = build_decl(BUILTINS_LOCATION
, TYPE_DECL
, NULL_TREE
,
5327 type_tree
= build_pointer_type(ret
);
5328 go_preserve_from_gc(type_tree
);
5333 // Initialize a channel variable.
5336 Channel_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5340 return fold_convert(type_tree
, null_pointer_node
);
5343 // Handle the builtin function make for a channel.
5346 Channel_type::do_make_expression_tree(Translate_context
* context
,
5347 Expression_list
* args
,
5348 source_location location
)
5350 Gogo
* gogo
= context
->gogo();
5351 tree channel_type
= this->get_tree(gogo
);
5353 tree element_tree
= this->element_type_
->get_tree(gogo
);
5354 tree element_size_tree
= size_in_bytes(element_tree
);
5356 tree bad_index
= NULL_TREE
;
5359 if (args
== NULL
|| args
->empty())
5360 expr_tree
= size_zero_node
;
5363 expr_tree
= args
->front()->get_tree(context
);
5364 if (expr_tree
== error_mark_node
)
5365 return error_mark_node
;
5366 if (!DECL_P(expr_tree
))
5367 expr_tree
= save_expr(expr_tree
);
5368 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5369 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5370 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5374 static tree new_channel_fndecl
;
5375 tree ret
= Gogo::call_builtin(&new_channel_fndecl
,
5384 if (ret
== error_mark_node
)
5385 return error_mark_node
;
5386 // This can panic if the capacity is out of range.
5387 TREE_NOTHROW(new_channel_fndecl
) = 0;
5389 if (bad_index
== NULL_TREE
)
5393 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
,
5395 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5396 build3(COND_EXPR
, void_type_node
,
5397 bad_index
, crash
, NULL_TREE
),
5402 // Build a type descriptor for a channel type.
5405 Channel_type::make_chan_type_descriptor_type()
5410 Type
* tdt
= Type::make_type_descriptor_type();
5411 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5413 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5416 Type::make_builtin_struct_type(3,
5419 "dir", uintptr_type
);
5421 ret
= Type::make_builtin_named_type("ChanType", sf
);
5427 // Build a type descriptor for a map type.
5430 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5432 source_location bloc
= BUILTINS_LOCATION
;
5434 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
5436 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
5438 Expression_list
* vals
= new Expression_list();
5441 Struct_field_list::const_iterator p
= fields
->begin();
5442 gcc_assert(p
->field_name() == "commonType");
5443 vals
->push_back(this->type_descriptor_constructor(gogo
,
5444 RUNTIME_TYPE_KIND_CHAN
,
5448 gcc_assert(p
->field_name() == "elem");
5449 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5452 gcc_assert(p
->field_name() == "dir");
5453 // These bits must match the ones in libgo/runtime/go-type.h.
5455 if (this->may_receive_
)
5457 if (this->may_send_
)
5460 mpz_init_set_ui(iv
, val
);
5461 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
5465 gcc_assert(p
== fields
->end());
5467 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
5470 // Reflection string.
5473 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5475 if (!this->may_send_
)
5477 ret
->append("chan");
5478 if (!this->may_receive_
)
5480 ret
->push_back(' ');
5481 this->append_reflection(this->element_type_
, gogo
, ret
);
5487 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5489 ret
->push_back('C');
5490 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5491 if (this->may_send_
)
5492 ret
->push_back('s');
5493 if (this->may_receive_
)
5494 ret
->push_back('r');
5495 ret
->push_back('e');
5501 Channel_type::do_export(Export
* exp
) const
5503 exp
->write_c_string("chan ");
5504 if (this->may_send_
&& !this->may_receive_
)
5505 exp
->write_c_string("-< ");
5506 else if (this->may_receive_
&& !this->may_send_
)
5507 exp
->write_c_string("<- ");
5508 exp
->write_type(this->element_type_
);
5514 Channel_type::do_import(Import
* imp
)
5516 imp
->require_c_string("chan ");
5520 if (imp
->match_c_string("-< "))
5524 may_receive
= false;
5526 else if (imp
->match_c_string("<- "))
5538 Type
* element_type
= imp
->read_type();
5540 return Type::make_channel_type(may_send
, may_receive
, element_type
);
5543 // Make a new channel type.
5546 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
5548 return new Channel_type(send
, receive
, element_type
);
5551 // Class Interface_type.
5556 Interface_type::do_traverse(Traverse
* traverse
)
5558 if (this->methods_
== NULL
)
5559 return TRAVERSE_CONTINUE
;
5560 return this->methods_
->traverse(traverse
);
5563 // Finalize the methods. This handles interface inheritance.
5566 Interface_type::finalize_methods()
5568 if (this->methods_
== NULL
)
5570 bool is_recursive
= false;
5573 while (from
< this->methods_
->size())
5575 const Typed_identifier
* p
= &this->methods_
->at(from
);
5576 if (!p
->name().empty())
5579 for (i
= 0; i
< to
; ++i
)
5581 if (this->methods_
->at(i
).name() == p
->name())
5583 error_at(p
->location(), "duplicate method %qs",
5584 Gogo::message_name(p
->name()).c_str());
5591 this->methods_
->set(to
, *p
);
5597 Interface_type
* it
= p
->type()->interface_type();
5600 error_at(p
->location(), "interface contains embedded non-interface");
5608 error_at(p
->location(), "invalid recursive interface");
5609 is_recursive
= true;
5614 const Typed_identifier_list
* methods
= it
->methods();
5615 if (methods
== NULL
)
5620 for (Typed_identifier_list::const_iterator q
= methods
->begin();
5621 q
!= methods
->end();
5624 if (q
->name().empty())
5626 if (q
->type() == p
->type())
5627 error_at(p
->location(), "interface inheritance loop");
5631 for (i
= from
+ 1; i
< this->methods_
->size(); ++i
)
5633 const Typed_identifier
* r
= &this->methods_
->at(i
);
5634 if (r
->name().empty() && r
->type() == q
->type())
5636 error_at(p
->location(),
5637 "inherited interface listed twice");
5641 if (i
== this->methods_
->size())
5642 this->methods_
->push_back(Typed_identifier(q
->name(),
5647 else if (this->find_method(q
->name()) == NULL
)
5648 this->methods_
->push_back(Typed_identifier(q
->name(), q
->type(),
5653 error_at(p
->location(), "inherited method %qs is ambiguous",
5654 Gogo::message_name(q
->name()).c_str());
5661 delete this->methods_
;
5662 this->methods_
= NULL
;
5666 this->methods_
->resize(to
);
5667 this->methods_
->sort_by_name();
5671 // Return the method NAME, or NULL.
5673 const Typed_identifier
*
5674 Interface_type::find_method(const std::string
& name
) const
5676 if (this->methods_
== NULL
)
5678 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5679 p
!= this->methods_
->end();
5681 if (p
->name() == name
)
5686 // Return the method index.
5689 Interface_type::method_index(const std::string
& name
) const
5691 gcc_assert(this->methods_
!= NULL
);
5693 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5694 p
!= this->methods_
->end();
5696 if (p
->name() == name
)
5701 // Return whether NAME is an unexported method, for better error
5705 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
5707 if (this->methods_
== NULL
)
5709 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5710 p
!= this->methods_
->end();
5713 const std::string
& method_name(p
->name());
5714 if (Gogo::is_hidden_name(method_name
)
5715 && name
== Gogo::unpack_hidden_name(method_name
)
5716 && gogo
->pack_hidden_name(name
, false) != method_name
)
5722 // Whether this type is identical with T.
5725 Interface_type::is_identical(const Interface_type
* t
,
5726 bool errors_are_identical
) const
5728 // We require the same methods with the same types. The methods
5729 // have already been sorted.
5730 if (this->methods() == NULL
|| t
->methods() == NULL
)
5731 return this->methods() == t
->methods();
5733 Typed_identifier_list::const_iterator p1
= this->methods()->begin();
5734 for (Typed_identifier_list::const_iterator p2
= t
->methods()->begin();
5735 p2
!= t
->methods()->end();
5738 if (p1
== this->methods()->end())
5740 if (p1
->name() != p2
->name()
5741 || !Type::are_identical(p1
->type(), p2
->type(),
5742 errors_are_identical
, NULL
))
5745 if (p1
!= this->methods()->end())
5750 // Whether we can assign the interface type T to this type. The types
5751 // are known to not be identical. An interface assignment is only
5752 // permitted if T is known to implement all methods in THIS.
5753 // Otherwise a type guard is required.
5756 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
5757 std::string
* reason
) const
5759 if (this->methods() == NULL
)
5761 for (Typed_identifier_list::const_iterator p
= this->methods()->begin();
5762 p
!= this->methods()->end();
5765 const Typed_identifier
* m
= t
->find_method(p
->name());
5771 snprintf(buf
, sizeof buf
,
5772 _("need explicit conversion; missing method %s%s%s"),
5773 open_quote
, Gogo::message_name(p
->name()).c_str(),
5775 reason
->assign(buf
);
5780 std::string subreason
;
5781 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
5785 std::string n
= Gogo::message_name(p
->name());
5786 size_t len
= 100 + n
.length() + subreason
.length();
5787 char* buf
= new char[len
];
5788 if (subreason
.empty())
5789 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5790 open_quote
, n
.c_str(), close_quote
);
5793 _("incompatible type for method %s%s%s (%s)"),
5794 open_quote
, n
.c_str(), close_quote
,
5796 reason
->assign(buf
);
5809 Interface_type::do_hash_for_method(Gogo
* gogo
) const
5811 unsigned int ret
= 0;
5812 if (this->methods_
!= NULL
)
5814 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5815 p
!= this->methods_
->end();
5818 ret
= Type::hash_string(p
->name(), ret
);
5819 ret
+= p
->type()->hash_for_method(gogo
);
5826 // Return true if T implements the interface. If it does not, and
5827 // REASON is not NULL, set *REASON to a useful error message.
5830 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
5832 if (this->methods_
== NULL
)
5835 bool is_pointer
= false;
5836 const Named_type
* nt
= t
->named_type();
5837 const Struct_type
* st
= t
->struct_type();
5838 // If we start with a named type, we don't dereference it to find
5842 const Type
* pt
= t
->points_to();
5845 // If T is a pointer to a named type, then we need to look at
5846 // the type to which it points.
5848 nt
= pt
->named_type();
5849 st
= pt
->struct_type();
5853 // If we have a named type, get the methods from it rather than from
5858 // Only named and struct types have methods.
5859 if (nt
== NULL
&& st
== NULL
)
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 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
5876 if (t
->points_to() != NULL
5877 && t
->points_to()->interface_type() != NULL
)
5878 reason
->assign(_("pointer to interface type has no methods"));
5880 reason
->assign(_("type has no methods"));
5885 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5886 p
!= this->methods_
->end();
5889 bool is_ambiguous
= false;
5890 Method
* m
= (nt
!= NULL
5891 ? nt
->method_function(p
->name(), &is_ambiguous
)
5892 : st
->method_function(p
->name(), &is_ambiguous
));
5897 std::string n
= Gogo::message_name(p
->name());
5898 size_t len
= n
.length() + 100;
5899 char* buf
= new char[len
];
5901 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
5902 open_quote
, n
.c_str(), close_quote
);
5904 snprintf(buf
, len
, _("missing method %s%s%s"),
5905 open_quote
, n
.c_str(), close_quote
);
5906 reason
->assign(buf
);
5912 Function_type
*p_fn_type
= p
->type()->function_type();
5913 Function_type
* m_fn_type
= m
->type()->function_type();
5914 gcc_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
5915 std::string subreason
;
5916 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
5920 std::string n
= Gogo::message_name(p
->name());
5921 size_t len
= 100 + n
.length() + subreason
.length();
5922 char* buf
= new char[len
];
5923 if (subreason
.empty())
5924 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5925 open_quote
, n
.c_str(), close_quote
);
5928 _("incompatible type for method %s%s%s (%s)"),
5929 open_quote
, n
.c_str(), close_quote
,
5931 reason
->assign(buf
);
5937 if (!is_pointer
&& !m
->is_value_method())
5941 std::string n
= Gogo::message_name(p
->name());
5942 size_t len
= 100 + n
.length();
5943 char* buf
= new char[len
];
5944 snprintf(buf
, len
, _("method %s%s%s requires a pointer"),
5945 open_quote
, n
.c_str(), close_quote
);
5946 reason
->assign(buf
);
5956 // Return a tree for an interface type. An interface is a pointer to
5957 // a struct. The struct has three fields. The first field is a
5958 // pointer to the type descriptor for the dynamic type of the object.
5959 // The second field is a pointer to a table of methods for the
5960 // interface to be used with the object. The third field is the value
5961 // of the object itself.
5964 Interface_type::do_get_tree(Gogo
* gogo
)
5966 if (this->methods_
== NULL
)
5968 // At the tree level, use the same type for all empty
5969 // interfaces. This lets us assign them to each other directly
5970 // without triggering GIMPLE type errors.
5971 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5972 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5973 static tree empty_interface
;
5974 return Gogo::builtin_struct(&empty_interface
, "__go_empty_interface",
5976 "__type_descriptor",
5982 return this->fill_in_tree(gogo
, make_node(RECORD_TYPE
));
5985 // Fill in the tree for an interface type. This is used for named
5989 Interface_type::fill_in_tree(Gogo
* gogo
, tree type
)
5991 gcc_assert(this->methods_
!= NULL
);
5993 // Because the methods may refer to the interface type itself, we
5994 // need to build the interface type first, and then update the
5995 // method pointer later.
5997 tree field_trees
= NULL_TREE
;
5998 tree
* pp
= &field_trees
;
6000 tree name_tree
= get_identifier("__methods");
6001 tree methods_field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
6003 DECL_CONTEXT(methods_field
) = type
;
6004 *pp
= methods_field
;
6005 pp
= &DECL_CHAIN(methods_field
);
6007 name_tree
= get_identifier("__object");
6008 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
6010 DECL_CONTEXT(field
) = type
;
6013 TYPE_FIELDS(type
) = field_trees
;
6017 // Build the type of the table of methods.
6019 tree method_table
= make_node(RECORD_TYPE
);
6021 // The first field is a pointer to the type descriptor.
6022 name_tree
= get_identifier("__type_descriptor");
6023 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
6024 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
6025 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, dtype
);
6026 DECL_CONTEXT(field
) = method_table
;
6027 TYPE_FIELDS(method_table
) = field
;
6029 std::string last_name
= "";
6030 pp
= &DECL_CHAIN(field
);
6031 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6032 p
!= this->methods_
->end();
6035 std::string name
= Gogo::unpack_hidden_name(p
->name());
6036 name_tree
= get_identifier_with_length(name
.data(), name
.length());
6037 tree field_type
= p
->type()->get_tree(gogo
);
6038 if (field_type
== error_mark_node
)
6039 return error_mark_node
;
6040 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, field_type
);
6041 DECL_CONTEXT(field
) = method_table
;
6043 pp
= &DECL_CHAIN(field
);
6044 // Sanity check: the names should be sorted.
6045 gcc_assert(p
->name() > last_name
);
6046 last_name
= p
->name();
6048 layout_type(method_table
);
6050 // Update the type of the __methods field from a generic pointer to
6051 // a pointer to the method table.
6052 TREE_TYPE(methods_field
) = build_pointer_type(method_table
);
6057 // Initialization value.
6060 Interface_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
6065 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
6066 for (tree field
= TYPE_FIELDS(type_tree
);
6068 field
= DECL_CHAIN(field
))
6070 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
6072 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
6075 tree ret
= build_constructor(type_tree
, init
);
6076 TREE_CONSTANT(ret
) = 1;
6080 // The type of an interface type descriptor.
6083 Interface_type::make_interface_type_descriptor_type()
6088 Type
* tdt
= Type::make_type_descriptor_type();
6089 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6091 Type
* string_type
= Type::lookup_string_type();
6092 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6095 Type::make_builtin_struct_type(3,
6096 "name", pointer_string_type
,
6097 "pkgPath", pointer_string_type
,
6100 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
6102 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
6104 Struct_type
* s
= Type::make_builtin_struct_type(2,
6106 "methods", slice_nsm
);
6108 ret
= Type::make_builtin_named_type("InterfaceType", s
);
6114 // Build a type descriptor for an interface type.
6117 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6119 source_location bloc
= BUILTINS_LOCATION
;
6121 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
6123 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
6125 Expression_list
* ivals
= new Expression_list();
6128 Struct_field_list::const_iterator pif
= ifields
->begin();
6129 gcc_assert(pif
->field_name() == "commonType");
6130 ivals
->push_back(this->type_descriptor_constructor(gogo
,
6131 RUNTIME_TYPE_KIND_INTERFACE
,
6135 gcc_assert(pif
->field_name() == "methods");
6137 Expression_list
* methods
= new Expression_list();
6138 if (this->methods_
!= NULL
&& !this->methods_
->empty())
6140 Type
* elemtype
= pif
->type()->array_type()->element_type();
6142 methods
->reserve(this->methods_
->size());
6143 for (Typed_identifier_list::const_iterator pm
= this->methods_
->begin();
6144 pm
!= this->methods_
->end();
6147 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
6149 Expression_list
* mvals
= new Expression_list();
6152 Struct_field_list::const_iterator pmf
= mfields
->begin();
6153 gcc_assert(pmf
->field_name() == "name");
6154 std::string s
= Gogo::unpack_hidden_name(pm
->name());
6155 Expression
* e
= Expression::make_string(s
, bloc
);
6156 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6159 gcc_assert(pmf
->field_name() == "pkgPath");
6160 if (!Gogo::is_hidden_name(pm
->name()))
6161 mvals
->push_back(Expression::make_nil(bloc
));
6164 s
= Gogo::hidden_name_prefix(pm
->name());
6165 e
= Expression::make_string(s
, bloc
);
6166 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6170 gcc_assert(pmf
->field_name() == "typ");
6171 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
6174 gcc_assert(pmf
== mfields
->end());
6176 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
6178 methods
->push_back(e
);
6182 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
6186 gcc_assert(pif
== ifields
->end());
6188 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
6191 // Reflection string.
6194 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6196 ret
->append("interface {");
6197 if (this->methods_
!= NULL
)
6199 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6200 p
!= this->methods_
->end();
6203 if (p
!= this->methods_
->begin())
6205 ret
->push_back(' ');
6206 ret
->append(Gogo::unpack_hidden_name(p
->name()));
6207 std::string sub
= p
->type()->reflection(gogo
);
6208 gcc_assert(sub
.compare(0, 4, "func") == 0);
6209 sub
= sub
.substr(4);
6219 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6221 ret
->push_back('I');
6223 const Typed_identifier_list
* methods
= this->methods_
;
6224 if (methods
!= NULL
)
6226 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6227 p
!= methods
->end();
6230 std::string n
= Gogo::unpack_hidden_name(p
->name());
6232 snprintf(buf
, sizeof buf
, "%u_",
6233 static_cast<unsigned int>(n
.length()));
6236 this->append_mangled_name(p
->type(), gogo
, ret
);
6240 ret
->push_back('e');
6246 Interface_type::do_export(Export
* exp
) const
6248 exp
->write_c_string("interface { ");
6250 const Typed_identifier_list
* methods
= this->methods_
;
6251 if (methods
!= NULL
)
6253 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
6254 pm
!= methods
->end();
6257 exp
->write_string(pm
->name());
6258 exp
->write_c_string(" (");
6260 const Function_type
* fntype
= pm
->type()->function_type();
6263 const Typed_identifier_list
* parameters
= fntype
->parameters();
6264 if (parameters
!= NULL
)
6266 bool is_varargs
= fntype
->is_varargs();
6267 for (Typed_identifier_list::const_iterator pp
=
6268 parameters
->begin();
6269 pp
!= parameters
->end();
6275 exp
->write_c_string(", ");
6276 if (!is_varargs
|| pp
+ 1 != parameters
->end())
6277 exp
->write_type(pp
->type());
6280 exp
->write_c_string("...");
6281 Type
*pptype
= pp
->type();
6282 exp
->write_type(pptype
->array_type()->element_type());
6287 exp
->write_c_string(")");
6289 const Typed_identifier_list
* results
= fntype
->results();
6290 if (results
!= NULL
)
6292 exp
->write_c_string(" ");
6293 if (results
->size() == 1)
6294 exp
->write_type(results
->begin()->type());
6298 exp
->write_c_string("(");
6299 for (Typed_identifier_list::const_iterator p
=
6301 p
!= results
->end();
6307 exp
->write_c_string(", ");
6308 exp
->write_type(p
->type());
6310 exp
->write_c_string(")");
6314 exp
->write_c_string("; ");
6318 exp
->write_c_string("}");
6321 // Import an interface type.
6324 Interface_type::do_import(Import
* imp
)
6326 imp
->require_c_string("interface { ");
6328 Typed_identifier_list
* methods
= new Typed_identifier_list
;
6329 while (imp
->peek_char() != '}')
6331 std::string name
= imp
->read_identifier();
6332 imp
->require_c_string(" (");
6334 Typed_identifier_list
* parameters
;
6335 bool is_varargs
= false;
6336 if (imp
->peek_char() == ')')
6340 parameters
= new Typed_identifier_list
;
6343 if (imp
->match_c_string("..."))
6349 Type
* ptype
= imp
->read_type();
6351 ptype
= Type::make_array_type(ptype
, NULL
);
6352 parameters
->push_back(Typed_identifier(Import::import_marker
,
6353 ptype
, imp
->location()));
6354 if (imp
->peek_char() != ',')
6356 gcc_assert(!is_varargs
);
6357 imp
->require_c_string(", ");
6360 imp
->require_c_string(")");
6362 Typed_identifier_list
* results
;
6363 if (imp
->peek_char() != ' ')
6367 results
= new Typed_identifier_list
;
6369 if (imp
->peek_char() != '(')
6371 Type
* rtype
= imp
->read_type();
6372 results
->push_back(Typed_identifier(Import::import_marker
,
6373 rtype
, imp
->location()));
6380 Type
* rtype
= imp
->read_type();
6381 results
->push_back(Typed_identifier(Import::import_marker
,
6382 rtype
, imp
->location()));
6383 if (imp
->peek_char() != ',')
6385 imp
->require_c_string(", ");
6387 imp
->require_c_string(")");
6391 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
6395 fntype
->set_is_varargs();
6396 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
6398 imp
->require_c_string("; ");
6401 imp
->require_c_string("}");
6403 if (methods
->empty())
6409 return Type::make_interface_type(methods
, imp
->location());
6412 // Make an interface type.
6415 Type::make_interface_type(Typed_identifier_list
* methods
,
6416 source_location location
)
6418 return new Interface_type(methods
, location
);
6423 // Bind a method to an object.
6426 Method::bind_method(Expression
* expr
, source_location location
) const
6428 if (this->stub_
== NULL
)
6430 // When there is no stub object, the binding is determined by
6432 return this->do_bind_method(expr
, location
);
6435 Expression
* func
= Expression::make_func_reference(this->stub_
, NULL
,
6437 return Expression::make_bound_method(expr
, func
, location
);
6440 // Return the named object associated with a method. This may only be
6441 // called after methods are finalized.
6444 Method::named_object() const
6446 if (this->stub_
!= NULL
)
6448 return this->do_named_object();
6451 // Class Named_method.
6453 // The type of the method.
6456 Named_method::do_type() const
6458 if (this->named_object_
->is_function())
6459 return this->named_object_
->func_value()->type();
6460 else if (this->named_object_
->is_function_declaration())
6461 return this->named_object_
->func_declaration_value()->type();
6466 // Return the location of the method receiver.
6469 Named_method::do_receiver_location() const
6471 return this->do_type()->receiver()->location();
6474 // Bind a method to an object.
6477 Named_method::do_bind_method(Expression
* expr
, source_location location
) const
6479 Expression
* func
= Expression::make_func_reference(this->named_object_
, NULL
,
6481 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, func
,
6483 // If this is not a local method, and it does not use a stub, then
6484 // the real method expects a different type. We need to cast the
6486 if (this->depth() > 0 && !this->needs_stub_method())
6488 Function_type
* ftype
= this->do_type();
6489 gcc_assert(ftype
->is_method());
6490 Type
* frtype
= ftype
->receiver()->type();
6491 bme
->set_first_argument_type(frtype
);
6496 // Class Interface_method.
6498 // Bind a method to an object.
6501 Interface_method::do_bind_method(Expression
* expr
,
6502 source_location location
) const
6504 return Expression::make_interface_field_reference(expr
, this->name_
,
6510 // Insert a new method. Return true if it was inserted, false
6514 Methods::insert(const std::string
& name
, Method
* m
)
6516 std::pair
<Method_map::iterator
, bool> ins
=
6517 this->methods_
.insert(std::make_pair(name
, m
));
6522 Method
* old_method
= ins
.first
->second
;
6523 if (m
->depth() < old_method
->depth())
6526 ins
.first
->second
= m
;
6531 if (m
->depth() == old_method
->depth())
6532 old_method
->set_is_ambiguous();
6538 // Return the number of unambiguous methods.
6541 Methods::count() const
6544 for (Method_map::const_iterator p
= this->methods_
.begin();
6545 p
!= this->methods_
.end();
6547 if (!p
->second
->is_ambiguous())
6552 // Class Named_type.
6554 // Return the name of the type.
6557 Named_type::name() const
6559 return this->named_object_
->name();
6562 // Return the name of the type to use in an error message.
6565 Named_type::message_name() const
6567 return this->named_object_
->message_name();
6570 // Return the base type for this type. We have to be careful about
6571 // circular type definitions, which are invalid but may be seen here.
6574 Named_type::named_base()
6576 if (this->seen_
> 0)
6579 Type
* ret
= this->type_
->base();
6585 Named_type::named_base() const
6587 if (this->seen_
> 0)
6590 const Type
* ret
= this->type_
->base();
6595 // Return whether this is an error type. We have to be careful about
6596 // circular type definitions, which are invalid but may be seen here.
6599 Named_type::is_named_error_type() const
6601 if (this->seen_
> 0)
6604 bool ret
= this->type_
->is_error_type();
6609 // Add a method to this type.
6612 Named_type::add_method(const std::string
& name
, Function
* function
)
6614 if (this->local_methods_
== NULL
)
6615 this->local_methods_
= new Bindings(NULL
);
6616 return this->local_methods_
->add_function(name
, NULL
, function
);
6619 // Add a method declaration to this type.
6622 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
6623 Function_type
* type
,
6624 source_location location
)
6626 if (this->local_methods_
== NULL
)
6627 this->local_methods_
= new Bindings(NULL
);
6628 return this->local_methods_
->add_function_declaration(name
, package
, type
,
6632 // Add an existing method to this type.
6635 Named_type::add_existing_method(Named_object
* no
)
6637 if (this->local_methods_
== NULL
)
6638 this->local_methods_
= new Bindings(NULL
);
6639 this->local_methods_
->add_named_object(no
);
6642 // Look for a local method NAME, and returns its named object, or NULL
6646 Named_type::find_local_method(const std::string
& name
) const
6648 if (this->local_methods_
== NULL
)
6650 return this->local_methods_
->lookup(name
);
6653 // Return whether NAME is an unexported field or method, for better
6657 Named_type::is_unexported_local_method(Gogo
* gogo
,
6658 const std::string
& name
) const
6660 Bindings
* methods
= this->local_methods_
;
6661 if (methods
!= NULL
)
6663 for (Bindings::const_declarations_iterator p
=
6664 methods
->begin_declarations();
6665 p
!= methods
->end_declarations();
6668 if (Gogo::is_hidden_name(p
->first
)
6669 && name
== Gogo::unpack_hidden_name(p
->first
)
6670 && gogo
->pack_hidden_name(name
, false) != p
->first
)
6677 // Build the complete list of methods for this type, which means
6678 // recursively including all methods for anonymous fields. Create all
6682 Named_type::finalize_methods(Gogo
* gogo
)
6684 if (this->all_methods_
!= NULL
)
6687 if (this->local_methods_
!= NULL
6688 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
6690 const Bindings
* lm
= this->local_methods_
;
6691 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
6692 p
!= lm
->end_declarations();
6694 error_at(p
->second
->location(),
6695 "invalid pointer or interface receiver type");
6696 delete this->local_methods_
;
6697 this->local_methods_
= NULL
;
6701 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6704 // Return the method NAME, or NULL if there isn't one or if it is
6705 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6709 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6711 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6714 // Return a pointer to the interface method table for this type for
6715 // the interface INTERFACE. IS_POINTER is true if this is for a
6719 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
6722 gcc_assert(!interface
->is_empty());
6724 Interface_method_tables
** pimt
= (is_pointer
6725 ? &this->interface_method_tables_
6726 : &this->pointer_interface_method_tables_
);
6729 *pimt
= new Interface_method_tables(5);
6731 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
6732 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
6736 // This is a new entry in the hash table.
6737 gcc_assert(ins
.first
->second
== NULL_TREE
);
6738 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
6743 tree decl
= ins
.first
->second
;
6744 if (decl
== error_mark_node
)
6745 return error_mark_node
;
6746 gcc_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
6747 return build_fold_addr_expr(decl
);
6750 // Return whether a named type has any hidden fields.
6753 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
6755 if (this->seen_
> 0)
6758 bool ret
= this->type_
->has_hidden_fields(this, reason
);
6763 // Look for a use of a complete type within another type. This is
6764 // used to check that we don't try to use a type within itself.
6766 class Find_type_use
: public Traverse
6769 Find_type_use(Type
* find_type
)
6770 : Traverse(traverse_types
),
6771 find_type_(find_type
), found_(false)
6774 // Whether we found the type.
6777 { return this->found_
; }
6784 // The type we are looking for.
6786 // Whether we found the type.
6790 // Check for FIND_TYPE in TYPE.
6793 Find_type_use::type(Type
* type
)
6795 if (this->find_type_
== type
)
6797 this->found_
= true;
6798 return TRAVERSE_EXIT
;
6800 // It's OK if we see a reference to the type in any type which is
6801 // essentially a pointer: a pointer, a slice, a function, a map, or
6803 if (type
->points_to() != NULL
6804 || type
->is_open_array_type()
6805 || type
->function_type() != NULL
6806 || type
->map_type() != NULL
6807 || type
->channel_type() != NULL
)
6808 return TRAVERSE_SKIP_COMPONENTS
;
6810 // For an interface, a reference to the type in a method type should
6811 // be ignored, but we have to consider direct inheritance. When
6812 // this is called, there may be cases of direct inheritance
6813 // represented as a method with no name.
6814 if (type
->interface_type() != NULL
)
6816 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
6817 if (methods
!= NULL
)
6819 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6820 p
!= methods
->end();
6823 if (p
->name().empty())
6825 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
6826 return TRAVERSE_EXIT
;
6830 return TRAVERSE_SKIP_COMPONENTS
;
6833 return TRAVERSE_CONTINUE
;
6836 // Verify that a named type does not refer to itself.
6839 Named_type::do_verify()
6841 Find_type_use
find(this);
6842 Type::traverse(this->type_
, &find
);
6845 error_at(this->location_
, "invalid recursive type %qs",
6846 this->message_name().c_str());
6847 this->is_error_
= true;
6851 // Check whether any of the local methods overloads an existing
6852 // struct field or interface method. We don't need to check the
6853 // list of methods against itself: that is handled by the Bindings
6855 if (this->local_methods_
!= NULL
)
6857 Struct_type
* st
= this->type_
->struct_type();
6858 Interface_type
* it
= this->type_
->interface_type();
6859 bool found_dup
= false;
6860 if (st
!= NULL
|| it
!= NULL
)
6862 for (Bindings::const_declarations_iterator p
=
6863 this->local_methods_
->begin_declarations();
6864 p
!= this->local_methods_
->end_declarations();
6867 const std::string
& name(p
->first
);
6868 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
6870 error_at(p
->second
->location(),
6871 "method %qs redeclares struct field name",
6872 Gogo::message_name(name
).c_str());
6875 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
6877 error_at(p
->second
->location(),
6878 "method %qs redeclares interface method name",
6879 Gogo::message_name(name
).c_str());
6888 // If this is a struct, then if any of the fields of the struct
6889 // themselves have struct type, then this struct must be converted
6890 // to the backend representation before the field's type is
6891 // converted. That may seem backward, but it works because if the
6892 // field's type refers to this one, e.g., via a pointer, then the
6893 // conversion process will pick up the half-built struct and do the
6895 if (this->struct_type() != NULL
)
6897 const Struct_field_list
* fields
= this->struct_type()->fields();
6898 for (Struct_field_list::const_iterator p
= fields
->begin();
6902 Struct_type
* st
= p
->type()->struct_type();
6904 st
->add_prerequisite(this);
6911 // Return whether this type is or contains a pointer.
6914 Named_type::do_has_pointer() const
6916 if (this->seen_
> 0)
6919 bool ret
= this->type_
->has_pointer();
6924 // Return a hash code. This is used for method lookup. We simply
6925 // hash on the name itself.
6928 Named_type::do_hash_for_method(Gogo
* gogo
) const
6930 const std::string
& name(this->named_object()->name());
6931 unsigned int ret
= Type::hash_string(name
, 0);
6933 // GOGO will be NULL here when called from Type_hash_identical.
6934 // That is OK because that is only used for internal hash tables
6935 // where we are going to be comparing named types for equality. In
6936 // other cases, which are cases where the runtime is going to
6937 // compare hash codes to see if the types are the same, we need to
6938 // include the package prefix and name in the hash.
6939 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
6941 const Package
* package
= this->named_object()->package();
6942 if (package
== NULL
)
6944 ret
= Type::hash_string(gogo
->unique_prefix(), ret
);
6945 ret
= Type::hash_string(gogo
->package_name(), ret
);
6949 ret
= Type::hash_string(package
->unique_prefix(), ret
);
6950 ret
= Type::hash_string(package
->name(), ret
);
6957 // Get a tree for a named type.
6960 Named_type::do_get_tree(Gogo
* gogo
)
6962 if (this->is_error_
)
6963 return error_mark_node
;
6965 // Go permits types to refer to themselves in various ways. Break
6966 // the recursion here.
6968 switch (this->type_
->forwarded()->classification())
6971 return error_mark_node
;
6980 // These types can not refer to themselves.
6983 // All maps and channels have the same type in GENERIC.
6984 t
= Type::get_named_type_tree(gogo
, this->type_
);
6985 if (t
== error_mark_node
)
6986 return error_mark_node
;
6987 // Build a copy to set TYPE_NAME.
6988 t
= build_variant_type_copy(t
);
6992 // GENERIC can't handle a pointer to a function type whose
6993 // return type is a pointer to the function type itself. It
6994 // goes into an infinite loop when walking the types.
6995 if (this->seen_
> 0)
6997 Function_type
* fntype
= this->type_
->function_type();
6998 if (fntype
->results() != NULL
6999 && fntype
->results()->size() == 1
7000 && fntype
->results()->front().type()->forwarded() == this)
7001 return ptr_type_node
;
7003 // We can legitimately see ourselves here twice when a named
7004 // type is defined using a struct which refers to the named
7005 // type. If we see ourselves too often we are in a loop.
7006 if (this->seen_
> 3)
7007 return ptr_type_node
;
7010 t
= Type::get_named_type_tree(gogo
, this->type_
);
7012 if (t
== error_mark_node
)
7013 return error_mark_node
;
7014 t
= build_variant_type_copy(t
);
7018 // Don't recur infinitely if a pointer type refers to itself.
7019 // Ideally we would build a circular data structure here, but
7020 // GENERIC can't handle them.
7021 if (this->seen_
> 0)
7023 if (this->type_
->points_to()->forwarded() == this)
7024 return ptr_type_node
;
7026 if (this->seen_
> 3)
7027 return ptr_type_node
;
7030 t
= Type::get_named_type_tree(gogo
, this->type_
);
7032 if (t
== error_mark_node
)
7033 return error_mark_node
;
7034 t
= build_variant_type_copy(t
);
7038 // If there are structs which must be converted first, do them.
7039 if (this->seen_
== 0)
7042 this->type_
->struct_type()->convert_prerequisites(gogo
);
7046 if (this->named_tree_
!= NULL_TREE
)
7047 return this->named_tree_
;
7049 t
= make_node(RECORD_TYPE
);
7050 this->named_tree_
= t
;
7051 t
= this->type_
->struct_type()->fill_in_tree(gogo
, t
);
7052 if (t
== error_mark_node
)
7053 return error_mark_node
;
7057 if (!this->is_open_array_type())
7058 t
= Type::get_named_type_tree(gogo
, this->type_
);
7061 if (this->named_tree_
!= NULL_TREE
)
7062 return this->named_tree_
;
7063 t
= gogo
->slice_type_tree(void_type_node
);
7064 this->named_tree_
= t
;
7065 t
= this->type_
->array_type()->fill_in_tree(gogo
, t
);
7067 if (t
== error_mark_node
)
7068 return error_mark_node
;
7069 t
= build_variant_type_copy(t
);
7072 case TYPE_INTERFACE
:
7073 if (this->type_
->interface_type()->is_empty())
7075 t
= Type::get_named_type_tree(gogo
, this->type_
);
7076 if (t
== error_mark_node
)
7077 return error_mark_node
;
7078 t
= build_variant_type_copy(t
);
7082 if (this->named_tree_
!= NULL_TREE
)
7083 return this->named_tree_
;
7084 t
= make_node(RECORD_TYPE
);
7085 this->named_tree_
= t
;
7086 t
= this->type_
->interface_type()->fill_in_tree(gogo
, t
);
7087 if (t
== error_mark_node
)
7088 return error_mark_node
;
7094 // When a named type T1 is defined as another named type T2,
7095 // the definition must simply be "type T1 T2". If the
7096 // definition of T2 may refer to T1, then we must simply
7097 // return the type for T2 here. It's not precisely correct,
7098 // but it's as close as we can get with GENERIC.
7100 t
= Type::get_named_type_tree(gogo
, this->type_
);
7102 if (this->seen_
> 0)
7104 if (t
== error_mark_node
)
7105 return error_mark_node
;
7106 t
= build_variant_type_copy(t
);
7111 // An undefined forwarding type. Make sure the error is
7113 this->type_
->forward_declaration_type()->real_type();
7114 return error_mark_node
;
7118 case TYPE_CALL_MULTIPLE_RESULT
:
7122 tree id
= this->named_object_
->get_id(gogo
);
7123 tree decl
= build_decl(this->location_
, TYPE_DECL
, id
, t
);
7124 TYPE_NAME(t
) = decl
;
7129 // Build a type descriptor for a named type.
7132 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7134 // If NAME is not NULL, then we don't really want the type
7135 // descriptor for this type; we want the descriptor for the
7136 // underlying type, giving it the name NAME.
7137 return this->named_type_descriptor(gogo
, this->type_
,
7138 name
== NULL
? this : name
);
7141 // Add to the reflection string. This is used mostly for the name of
7142 // the type used in a type descriptor, not for actual reflection
7146 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7148 if (this->location() != BUILTINS_LOCATION
)
7150 const Package
* package
= this->named_object_
->package();
7151 if (package
!= NULL
)
7152 ret
->append(package
->name());
7154 ret
->append(gogo
->package_name());
7155 ret
->push_back('.');
7157 if (this->in_function_
!= NULL
)
7159 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7160 ret
->push_back('$');
7162 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
7165 // Get the mangled name.
7168 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7170 Named_object
* no
= this->named_object_
;
7172 if (this->location() == BUILTINS_LOCATION
)
7173 gcc_assert(this->in_function_
== NULL
);
7176 const std::string
& unique_prefix(no
->package() == NULL
7177 ? gogo
->unique_prefix()
7178 : no
->package()->unique_prefix());
7179 const std::string
& package_name(no
->package() == NULL
7180 ? gogo
->package_name()
7181 : no
->package()->name());
7182 name
= unique_prefix
;
7183 name
.append(1, '.');
7184 name
.append(package_name
);
7185 name
.append(1, '.');
7186 if (this->in_function_
!= NULL
)
7188 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7189 name
.append(1, '$');
7192 name
.append(Gogo::unpack_hidden_name(no
->name()));
7194 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
7199 // Export the type. This is called to export a global type.
7202 Named_type::export_named_type(Export
* exp
, const std::string
&) const
7204 // We don't need to write the name of the type here, because it will
7205 // be written by Export::write_type anyhow.
7206 exp
->write_c_string("type ");
7207 exp
->write_type(this);
7208 exp
->write_c_string(";\n");
7211 // Import a named type.
7214 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
7216 imp
->require_c_string("type ");
7217 Type
*type
= imp
->read_type();
7218 *ptype
= type
->named_type();
7219 gcc_assert(*ptype
!= NULL
);
7220 imp
->require_c_string(";\n");
7223 // Export the type when it is referenced by another type. In this
7224 // case Export::export_type will already have issued the name.
7227 Named_type::do_export(Export
* exp
) const
7229 exp
->write_type(this->type_
);
7231 // To save space, we only export the methods directly attached to
7233 Bindings
* methods
= this->local_methods_
;
7234 if (methods
== NULL
)
7237 exp
->write_c_string("\n");
7238 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
7239 p
!= methods
->end_definitions();
7242 exp
->write_c_string(" ");
7243 (*p
)->export_named_object(exp
);
7246 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
7247 p
!= methods
->end_declarations();
7250 if (p
->second
->is_function_declaration())
7252 exp
->write_c_string(" ");
7253 p
->second
->export_named_object(exp
);
7258 // Make a named type.
7261 Type::make_named_type(Named_object
* named_object
, Type
* type
,
7262 source_location location
)
7264 return new Named_type(named_object
, type
, location
);
7267 // Finalize the methods for TYPE. It will be a named type or a struct
7268 // type. This sets *ALL_METHODS to the list of methods, and builds
7269 // all required stubs.
7272 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, source_location location
,
7273 Methods
** all_methods
)
7275 *all_methods
= NULL
;
7276 Types_seen types_seen
;
7277 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
7279 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
7282 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7283 // build up the struct field indexes as we go. DEPTH is the depth of
7284 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7285 // adding these methods for an anonymous field with pointer type.
7286 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7287 // calls the real method. TYPES_SEEN is used to avoid infinite
7291 Type::add_methods_for_type(const Type
* type
,
7292 const Method::Field_indexes
* field_indexes
,
7294 bool is_embedded_pointer
,
7295 bool needs_stub_method
,
7296 Types_seen
* types_seen
,
7299 // Pointer types may not have methods.
7300 if (type
->points_to() != NULL
)
7303 const Named_type
* nt
= type
->named_type();
7306 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
7312 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
7313 is_embedded_pointer
, needs_stub_method
,
7316 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
7317 is_embedded_pointer
, needs_stub_method
,
7318 types_seen
, methods
);
7320 // If we are called with depth > 0, then we are looking at an
7321 // anonymous field of a struct. If such a field has interface type,
7322 // then we need to add the interface methods. We don't want to add
7323 // them when depth == 0, because we will already handle them
7324 // following the usual rules for an interface type.
7326 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
7329 // Add the local methods for the named type NT to *METHODS. The
7330 // parameters are as for add_methods_to_type.
7333 Type::add_local_methods_for_type(const Named_type
* nt
,
7334 const Method::Field_indexes
* field_indexes
,
7336 bool is_embedded_pointer
,
7337 bool needs_stub_method
,
7340 const Bindings
* local_methods
= nt
->local_methods();
7341 if (local_methods
== NULL
)
7344 if (*methods
== NULL
)
7345 *methods
= new Methods();
7347 for (Bindings::const_declarations_iterator p
=
7348 local_methods
->begin_declarations();
7349 p
!= local_methods
->end_declarations();
7352 Named_object
* no
= p
->second
;
7353 bool is_value_method
= (is_embedded_pointer
7354 || !Type::method_expects_pointer(no
));
7355 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
7357 || (depth
> 0 && is_value_method
)));
7358 if (!(*methods
)->insert(no
->name(), m
))
7363 // Add the embedded methods for TYPE to *METHODS. These are the
7364 // methods attached to anonymous fields. The parameters are as for
7365 // add_methods_to_type.
7368 Type::add_embedded_methods_for_type(const Type
* type
,
7369 const Method::Field_indexes
* field_indexes
,
7371 bool is_embedded_pointer
,
7372 bool needs_stub_method
,
7373 Types_seen
* types_seen
,
7376 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7378 const Struct_type
* st
= type
->struct_type();
7382 const Struct_field_list
* fields
= st
->fields();
7387 for (Struct_field_list::const_iterator pf
= fields
->begin();
7388 pf
!= fields
->end();
7391 if (!pf
->is_anonymous())
7394 Type
* ftype
= pf
->type();
7395 bool is_pointer
= false;
7396 if (ftype
->points_to() != NULL
)
7398 ftype
= ftype
->points_to();
7401 Named_type
* fnt
= ftype
->named_type();
7404 // This is an error, but it will be diagnosed elsewhere.
7408 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
7409 sub_field_indexes
->next
= field_indexes
;
7410 sub_field_indexes
->field_index
= i
;
7412 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
7413 (is_embedded_pointer
|| is_pointer
),
7422 // If TYPE is an interface type, then add its method to *METHODS.
7423 // This is for interface methods attached to an anonymous field. The
7424 // parameters are as for add_methods_for_type.
7427 Type::add_interface_methods_for_type(const Type
* type
,
7428 const Method::Field_indexes
* field_indexes
,
7432 const Interface_type
* it
= type
->interface_type();
7436 const Typed_identifier_list
* imethods
= it
->methods();
7437 if (imethods
== NULL
)
7440 if (*methods
== NULL
)
7441 *methods
= new Methods();
7443 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
7444 pm
!= imethods
->end();
7447 Function_type
* fntype
= pm
->type()->function_type();
7448 gcc_assert(fntype
!= NULL
&& !fntype
->is_method());
7449 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
7450 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
7451 field_indexes
, depth
);
7452 if (!(*methods
)->insert(pm
->name(), m
))
7457 // Build stub methods for TYPE as needed. METHODS is the set of
7458 // methods for the type. A stub method may be needed when a type
7459 // inherits a method from an anonymous field. When we need the
7460 // address of the method, as in a type descriptor, we need to build a
7461 // little stub which does the required field dereferences and jumps to
7462 // the real method. LOCATION is the location of the type definition.
7465 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
7466 source_location location
)
7468 if (methods
== NULL
)
7470 for (Methods::const_iterator p
= methods
->begin();
7471 p
!= methods
->end();
7474 Method
* m
= p
->second
;
7475 if (m
->is_ambiguous() || !m
->needs_stub_method())
7478 const std::string
& name(p
->first
);
7480 // Build a stub method.
7482 const Function_type
* fntype
= m
->type();
7484 static unsigned int counter
;
7486 snprintf(buf
, sizeof buf
, "$this%u", counter
);
7489 Type
* receiver_type
= const_cast<Type
*>(type
);
7490 if (!m
->is_value_method())
7491 receiver_type
= Type::make_pointer_type(receiver_type
);
7492 source_location receiver_location
= m
->receiver_location();
7493 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
7496 const Typed_identifier_list
* fnparams
= fntype
->parameters();
7497 Typed_identifier_list
* stub_params
;
7498 if (fnparams
== NULL
|| fnparams
->empty())
7502 // We give each stub parameter a unique name.
7503 stub_params
= new Typed_identifier_list();
7504 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
7505 pp
!= fnparams
->end();
7509 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
7510 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
7516 const Typed_identifier_list
* fnresults
= fntype
->results();
7517 Typed_identifier_list
* stub_results
;
7518 if (fnresults
== NULL
|| fnresults
->empty())
7519 stub_results
= NULL
;
7522 // We create the result parameters without any names, since
7523 // we won't refer to them.
7524 stub_results
= new Typed_identifier_list();
7525 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
7526 pr
!= fnresults
->end();
7528 stub_results
->push_back(Typed_identifier("", pr
->type(),
7532 Function_type
* stub_type
= Type::make_function_type(receiver
,
7535 fntype
->location());
7536 if (fntype
->is_varargs())
7537 stub_type
->set_is_varargs();
7539 // We only create the function in the package which creates the
7541 const Package
* package
;
7542 if (type
->named_type() == NULL
)
7545 package
= type
->named_type()->named_object()->package();
7547 if (package
!= NULL
)
7548 stub
= Named_object::make_function_declaration(name
, package
,
7549 stub_type
, location
);
7552 stub
= gogo
->start_function(name
, stub_type
, false,
7553 fntype
->location());
7554 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
7555 fntype
->is_varargs(), location
);
7556 gogo
->finish_function(fntype
->location());
7559 m
->set_stub_object(stub
);
7563 // Build a stub method which adjusts the receiver as required to call
7564 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7565 // PARAMS is the list of function parameters.
7568 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
7569 const char* receiver_name
,
7570 const Typed_identifier_list
* params
,
7572 source_location location
)
7574 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
7575 gcc_assert(receiver_object
!= NULL
);
7577 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
7578 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
7579 if (expr
->type()->points_to() == NULL
)
7580 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7582 Expression_list
* arguments
;
7583 if (params
== NULL
|| params
->empty())
7587 arguments
= new Expression_list();
7588 for (Typed_identifier_list::const_iterator p
= params
->begin();
7592 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
7593 gcc_assert(param
!= NULL
);
7594 Expression
* param_ref
= Expression::make_var_reference(param
,
7596 arguments
->push_back(param_ref
);
7600 Expression
* func
= method
->bind_method(expr
, location
);
7601 gcc_assert(func
!= NULL
);
7602 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
7604 size_t count
= call
->result_count();
7606 gogo
->add_statement(Statement::make_statement(call
));
7609 Expression_list
* retvals
= new Expression_list();
7611 retvals
->push_back(call
);
7614 for (size_t i
= 0; i
< count
; ++i
)
7615 retvals
->push_back(Expression::make_call_result(call
, i
));
7617 const Function
* function
= gogo
->current_function()->func_value();
7618 const Typed_identifier_list
* results
= function
->type()->results();
7619 Statement
* retstat
= Statement::make_return_statement(results
, retvals
,
7621 gogo
->add_statement(retstat
);
7625 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7626 // in reverse order.
7629 Type::apply_field_indexes(Expression
* expr
,
7630 const Method::Field_indexes
* field_indexes
,
7631 source_location location
)
7633 if (field_indexes
== NULL
)
7635 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
7636 Struct_type
* stype
= expr
->type()->deref()->struct_type();
7637 gcc_assert(stype
!= NULL
7638 && field_indexes
->field_index
< stype
->field_count());
7639 if (expr
->type()->struct_type() == NULL
)
7641 gcc_assert(expr
->type()->points_to() != NULL
);
7642 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7643 gcc_assert(expr
->type()->struct_type() == stype
);
7645 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
7649 // Return whether NO is a method for which the receiver is a pointer.
7652 Type::method_expects_pointer(const Named_object
* no
)
7654 const Function_type
*fntype
;
7655 if (no
->is_function())
7656 fntype
= no
->func_value()->type();
7657 else if (no
->is_function_declaration())
7658 fntype
= no
->func_declaration_value()->type();
7661 return fntype
->receiver()->type()->points_to() != NULL
;
7664 // Given a set of methods for a type, METHODS, return the method NAME,
7665 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7666 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7667 // but is ambiguous (and return NULL).
7670 Type::method_function(const Methods
* methods
, const std::string
& name
,
7673 if (is_ambiguous
!= NULL
)
7674 *is_ambiguous
= false;
7675 if (methods
== NULL
)
7677 Methods::const_iterator p
= methods
->find(name
);
7678 if (p
== methods
->end())
7680 Method
* m
= p
->second
;
7681 if (m
->is_ambiguous())
7683 if (is_ambiguous
!= NULL
)
7684 *is_ambiguous
= true;
7690 // Look for field or method NAME for TYPE. Return an Expression for
7691 // the field or method bound to EXPR. If there is no such field or
7692 // method, give an appropriate error and return an error expression.
7695 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
7696 const std::string
& name
,
7697 source_location location
)
7699 if (type
->deref()->is_error_type())
7700 return Expression::make_error(location
);
7702 const Named_type
* nt
= type
->named_type();
7704 nt
= type
->deref()->named_type();
7705 const Struct_type
* st
= type
->deref()->struct_type();
7706 const Interface_type
* it
= type
->deref()->interface_type();
7708 // If this is a pointer to a pointer, then it is possible that the
7709 // pointed-to type has methods.
7713 && type
->points_to() != NULL
7714 && type
->points_to()->points_to() != NULL
)
7716 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7717 type
= type
->points_to();
7718 nt
= type
->points_to()->named_type();
7719 st
= type
->points_to()->struct_type();
7720 it
= type
->points_to()->interface_type();
7723 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
7724 || expr
->is_addressable());
7725 std::vector
<const Named_type
*> seen
;
7726 bool is_method
= false;
7727 bool found_pointer_method
= false;
7730 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
7731 &seen
, NULL
, &is_method
,
7732 &found_pointer_method
, &ambig1
, &ambig2
))
7737 gcc_assert(st
!= NULL
);
7738 if (type
->struct_type() == NULL
)
7740 gcc_assert(type
->points_to() != NULL
);
7741 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
7743 gcc_assert(expr
->type()->struct_type() == st
);
7745 ret
= st
->field_reference(expr
, name
, location
);
7747 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7748 ret
= Expression::make_interface_field_reference(expr
, name
,
7754 m
= nt
->method_function(name
, NULL
);
7755 else if (st
!= NULL
)
7756 m
= st
->method_function(name
, NULL
);
7759 gcc_assert(m
!= NULL
);
7760 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
7761 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7762 ret
= m
->bind_method(expr
, location
);
7764 gcc_assert(ret
!= NULL
);
7769 if (!ambig1
.empty())
7770 error_at(location
, "%qs is ambiguous via %qs and %qs",
7771 Gogo::message_name(name
).c_str(),
7772 Gogo::message_name(ambig1
).c_str(),
7773 Gogo::message_name(ambig2
).c_str());
7774 else if (found_pointer_method
)
7775 error_at(location
, "method requires a pointer");
7776 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
7778 ("reference to field %qs in object which "
7779 "has no fields or methods"),
7780 Gogo::message_name(name
).c_str());
7784 if (!Gogo::is_hidden_name(name
))
7785 is_unexported
= false;
7788 std::string unpacked
= Gogo::unpack_hidden_name(name
);
7790 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
7795 error_at(location
, "reference to unexported field or method %qs",
7796 Gogo::message_name(name
).c_str());
7798 error_at(location
, "reference to undefined field or method %qs",
7799 Gogo::message_name(name
).c_str());
7801 return Expression::make_error(location
);
7805 // Look in TYPE for a field or method named NAME, return true if one
7806 // is found. This looks through embedded anonymous fields and handles
7807 // ambiguity. If a method is found, sets *IS_METHOD to true;
7808 // otherwise, if a field is found, set it to false. If
7809 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7810 // whose address can not be taken. SEEN is used to avoid infinite
7811 // recursion on invalid types.
7813 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
7814 // method we couldn't use because it requires a pointer. LEVEL is
7815 // used for recursive calls, and can be NULL for a non-recursive call.
7816 // When this function returns false because it finds that the name is
7817 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
7818 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
7819 // will be unchanged.
7821 // This function just returns whether or not there is a field or
7822 // method, and whether it is a field or method. It doesn't build an
7823 // expression to refer to it. If it is a method, we then look in the
7824 // list of all methods for the type. If it is a field, the search has
7825 // to be done again, looking only for fields, and building up the
7826 // expression as we go.
7829 Type::find_field_or_method(const Type
* type
,
7830 const std::string
& name
,
7831 bool receiver_can_be_pointer
,
7832 std::vector
<const Named_type
*>* seen
,
7835 bool* found_pointer_method
,
7836 std::string
* ambig1
,
7837 std::string
* ambig2
)
7839 // Named types can have locally defined methods.
7840 const Named_type
* nt
= type
->named_type();
7841 if (nt
== NULL
&& type
->points_to() != NULL
)
7842 nt
= type
->points_to()->named_type();
7845 Named_object
* no
= nt
->find_local_method(name
);
7848 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
7854 // Record that we have found a pointer method in order to
7855 // give a better error message if we don't find anything
7857 *found_pointer_method
= true;
7860 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
7866 // We've already seen this type when searching for methods.
7872 // Interface types can have methods.
7873 const Interface_type
* it
= type
->deref()->interface_type();
7874 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7880 // Struct types can have fields. They can also inherit fields and
7881 // methods from anonymous fields.
7882 const Struct_type
* st
= type
->deref()->struct_type();
7885 const Struct_field_list
* fields
= st
->fields();
7890 seen
->push_back(nt
);
7892 int found_level
= 0;
7893 bool found_is_method
= false;
7894 std::string found_ambig1
;
7895 std::string found_ambig2
;
7896 const Struct_field
* found_parent
= NULL
;
7897 for (Struct_field_list::const_iterator pf
= fields
->begin();
7898 pf
!= fields
->end();
7901 if (pf
->field_name() == name
)
7909 if (!pf
->is_anonymous())
7912 if (pf
->type()->deref()->is_error_type()
7913 || pf
->type()->deref()->is_undefined())
7916 Named_type
* fnt
= pf
->type()->deref()->named_type();
7917 gcc_assert(fnt
!= NULL
);
7919 int sublevel
= level
== NULL
? 1 : *level
+ 1;
7921 std::string subambig1
;
7922 std::string subambig2
;
7923 bool subfound
= Type::find_field_or_method(fnt
,
7925 receiver_can_be_pointer
,
7929 found_pointer_method
,
7934 if (!subambig1
.empty())
7936 // The name was found via this field, but is ambiguous.
7937 // if the ambiguity is lower or at the same level as
7938 // anything else we have already found, then we want to
7939 // pass the ambiguity back to the caller.
7940 if (found_level
== 0 || sublevel
<= found_level
)
7942 found_ambig1
= pf
->field_name() + '.' + subambig1
;
7943 found_ambig2
= pf
->field_name() + '.' + subambig2
;
7944 found_level
= sublevel
;
7950 // The name was found via this field. Use the level to see
7951 // if we want to use this one, or whether it introduces an
7953 if (found_level
== 0 || sublevel
< found_level
)
7955 found_level
= sublevel
;
7956 found_is_method
= sub_is_method
;
7957 found_ambig1
.clear();
7958 found_ambig2
.clear();
7959 found_parent
= &*pf
;
7961 else if (sublevel
> found_level
)
7963 else if (found_ambig1
.empty())
7965 // We found an ambiguity.
7966 gcc_assert(found_parent
!= NULL
);
7967 found_ambig1
= found_parent
->field_name();
7968 found_ambig2
= pf
->field_name();
7972 // We found an ambiguity, but we already know of one.
7973 // Just report the earlier one.
7978 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7979 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7980 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7981 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7986 if (found_level
== 0)
7988 else if (!found_ambig1
.empty())
7990 gcc_assert(!found_ambig1
.empty());
7991 ambig1
->assign(found_ambig1
);
7992 ambig2
->assign(found_ambig2
);
7994 *level
= found_level
;
8000 *level
= found_level
;
8001 *is_method
= found_is_method
;
8006 // Return whether NAME is an unexported field or method for TYPE.
8009 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
8010 const std::string
& name
,
8011 std::vector
<const Named_type
*>* seen
)
8013 type
= type
->deref();
8015 const Named_type
* nt
= type
->named_type();
8018 if (nt
->is_unexported_local_method(gogo
, name
))
8021 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
8027 // We've already seen this type.
8033 const Interface_type
* it
= type
->interface_type();
8034 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
8037 const Struct_type
* st
= type
->struct_type();
8038 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
8044 const Struct_field_list
* fields
= st
->fields();
8049 seen
->push_back(nt
);
8051 for (Struct_field_list::const_iterator pf
= fields
->begin();
8052 pf
!= fields
->end();
8055 if (pf
->is_anonymous()
8056 && (!pf
->type()->deref()->is_error_type()
8057 && !pf
->type()->deref()->is_undefined()))
8059 Named_type
* subtype
= pf
->type()->deref()->named_type();
8060 gcc_assert(subtype
!= NULL
);
8061 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
8076 // Class Forward_declaration.
8078 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
8079 : Type(TYPE_FORWARD
),
8080 named_object_(named_object
->resolve()), warned_(false)
8082 gcc_assert(this->named_object_
->is_unknown()
8083 || this->named_object_
->is_type_declaration());
8086 // Return the named object.
8089 Forward_declaration_type::named_object()
8091 return this->named_object_
->resolve();
8095 Forward_declaration_type::named_object() const
8097 return this->named_object_
->resolve();
8100 // Return the name of the forward declared type.
8103 Forward_declaration_type::name() const
8105 return this->named_object()->name();
8108 // Warn about a use of a type which has been declared but not defined.
8111 Forward_declaration_type::warn() const
8113 Named_object
* no
= this->named_object_
->resolve();
8114 if (no
->is_unknown())
8116 // The name was not defined anywhere.
8119 error_at(this->named_object_
->location(),
8120 "use of undefined type %qs",
8121 no
->message_name().c_str());
8122 this->warned_
= true;
8125 else if (no
->is_type_declaration())
8127 // The name was seen as a type, but the type was never defined.
8128 if (no
->type_declaration_value()->using_type())
8130 error_at(this->named_object_
->location(),
8131 "use of undefined type %qs",
8132 no
->message_name().c_str());
8133 this->warned_
= true;
8138 // The name was defined, but not as a type.
8141 error_at(this->named_object_
->location(), "expected type");
8142 this->warned_
= true;
8147 // Get the base type of a declaration. This gives an error if the
8148 // type has not yet been defined.
8151 Forward_declaration_type::real_type()
8153 if (this->is_defined())
8154 return this->named_object()->type_value();
8158 return Type::make_error_type();
8163 Forward_declaration_type::real_type() const
8165 if (this->is_defined())
8166 return this->named_object()->type_value();
8170 return Type::make_error_type();
8174 // Return whether the base type is defined.
8177 Forward_declaration_type::is_defined() const
8179 return this->named_object()->is_type();
8182 // Add a method. This is used when methods are defined before the
8186 Forward_declaration_type::add_method(const std::string
& name
,
8189 Named_object
* no
= this->named_object();
8190 if (no
->is_unknown())
8191 no
->declare_as_type();
8192 return no
->type_declaration_value()->add_method(name
, function
);
8195 // Add a method declaration. This is used when methods are declared
8199 Forward_declaration_type::add_method_declaration(const std::string
& name
,
8200 Function_type
* type
,
8201 source_location location
)
8203 Named_object
* no
= this->named_object();
8204 if (no
->is_unknown())
8205 no
->declare_as_type();
8206 Type_declaration
* td
= no
->type_declaration_value();
8207 return td
->add_method_declaration(name
, type
, location
);
8213 Forward_declaration_type::do_traverse(Traverse
* traverse
)
8215 if (this->is_defined()
8216 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
8217 return TRAVERSE_EXIT
;
8218 return TRAVERSE_CONTINUE
;
8221 // Get a tree for the type.
8224 Forward_declaration_type::do_get_tree(Gogo
* gogo
)
8226 if (this->is_defined())
8227 return Type::get_named_type_tree(gogo
, this->real_type());
8230 return error_mark_node
;
8232 // We represent an undefined type as a struct with no fields. That
8233 // should work fine for the middle-end, since the same case can
8235 Named_object
* no
= this->named_object();
8236 tree type_tree
= make_node(RECORD_TYPE
);
8237 tree id
= no
->get_id(gogo
);
8238 tree decl
= build_decl(no
->location(), TYPE_DECL
, id
, type_tree
);
8239 TYPE_NAME(type_tree
) = decl
;
8240 layout_type(type_tree
);
8244 // Build a type descriptor for a forwarded type.
8247 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8249 if (!this->is_defined())
8250 return Expression::make_nil(BUILTINS_LOCATION
);
8253 Type
* t
= this->real_type();
8255 return this->named_type_descriptor(gogo
, t
, name
);
8257 return Expression::make_type_descriptor(t
, BUILTINS_LOCATION
);
8261 // The reflection string.
8264 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8266 this->append_reflection(this->real_type(), gogo
, ret
);
8269 // The mangled name.
8272 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8274 if (this->is_defined())
8275 this->append_mangled_name(this->real_type(), gogo
, ret
);
8278 const Named_object
* no
= this->named_object();
8280 if (no
->package() == NULL
)
8281 name
= gogo
->package_name();
8283 name
= no
->package()->name();
8285 name
+= Gogo::unpack_hidden_name(no
->name());
8287 snprintf(buf
, sizeof buf
, "N%u_",
8288 static_cast<unsigned int>(name
.length()));
8294 // Export a forward declaration. This can happen when a defined type
8295 // refers to a type which is only declared (and is presumably defined
8296 // in some other file in the same package).
8299 Forward_declaration_type::do_export(Export
*) const
8301 // If there is a base type, that should be exported instead of this.
8302 gcc_assert(!this->is_defined());
8304 // We don't output anything.
8307 // Make a forward declaration.
8310 Type::make_forward_declaration(Named_object
* named_object
)
8312 return new Forward_declaration_type(named_object
);
8315 // Class Typed_identifier_list.
8317 // Sort the entries by name.
8319 struct Typed_identifier_list_sort
8323 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
8324 { return t1
.name() < t2
.name(); }
8328 Typed_identifier_list::sort_by_name()
8330 std::sort(this->entries_
.begin(), this->entries_
.end(),
8331 Typed_identifier_list_sort());
8337 Typed_identifier_list::traverse(Traverse
* traverse
)
8339 for (Typed_identifier_list::const_iterator p
= this->begin();
8343 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
8344 return TRAVERSE_EXIT
;
8346 return TRAVERSE_CONTINUE
;
8351 Typed_identifier_list
*
8352 Typed_identifier_list::copy() const
8354 Typed_identifier_list
* ret
= new Typed_identifier_list();
8355 for (Typed_identifier_list::const_iterator p
= this->begin();
8358 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));