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("float64");
199 return Type::lookup_complex_type("complex128");
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.
486 && rhs
->forwarded()->forward_declaration_type() == NULL
487 && rhs
->is_void_type())
490 *reason
= "non-value used as value";
494 if (lhs
!= NULL
&& lhs
->forwarded()->forward_declaration_type() == NULL
)
496 // Any value may be assigned to the blank identifier.
497 if (lhs
->is_sink_type())
500 // All fields of a struct must be exported, or the assignment
501 // must be in the same package.
502 if (rhs
!= NULL
&& rhs
->forwarded()->forward_declaration_type() == NULL
)
504 if (lhs
->has_hidden_fields(NULL
, reason
)
505 || rhs
->has_hidden_fields(NULL
, reason
))
510 // Identical types are assignable.
511 if (Type::are_identical(lhs
, rhs
, true, reason
))
514 // The types are assignable if they have identical underlying types
515 // and either LHS or RHS is not a named type.
516 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
517 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
518 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
521 // The types are assignable if LHS is an interface type and RHS
522 // implements the required methods.
523 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
524 if (lhs_interface_type
!= NULL
)
526 if (lhs_interface_type
->implements_interface(rhs
, reason
))
528 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
529 if (rhs_interface_type
!= NULL
530 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
535 // The type are assignable if RHS is a bidirectional channel type,
536 // LHS is a channel type, they have identical element types, and
537 // either LHS or RHS is not a named type.
538 if (lhs
->channel_type() != NULL
539 && rhs
->channel_type() != NULL
540 && rhs
->channel_type()->may_send()
541 && rhs
->channel_type()->may_receive()
542 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
543 && Type::are_identical(lhs
->channel_type()->element_type(),
544 rhs
->channel_type()->element_type(),
549 // The nil type may be assigned to a pointer, function, slice, map,
550 // channel, or interface type.
551 if (rhs
->is_nil_type()
552 && (lhs
->points_to() != NULL
553 || lhs
->function_type() != NULL
554 || lhs
->is_open_array_type()
555 || lhs
->map_type() != NULL
556 || lhs
->channel_type() != NULL
557 || lhs
->interface_type() != NULL
))
560 // An untyped numeric constant may be assigned to a numeric type if
561 // it is representable in that type.
562 if ((rhs
->is_abstract()
563 && (rhs
->integer_type() != NULL
564 || rhs
->float_type() != NULL
565 || rhs
->complex_type() != NULL
))
566 && (lhs
->integer_type() != NULL
567 || lhs
->float_type() != NULL
568 || lhs
->complex_type() != NULL
))
571 // Give some better error messages.
572 if (reason
!= NULL
&& reason
->empty())
574 if (rhs
->interface_type() != NULL
)
575 reason
->assign(_("need explicit conversion"));
576 else if (rhs
->is_call_multiple_result_type())
577 reason
->assign(_("multiple value function call in "
578 "single value context"));
579 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
581 size_t len
= (lhs
->named_type()->name().length()
582 + rhs
->named_type()->name().length()
584 char* buf
= new char[len
];
585 snprintf(buf
, len
, _("cannot use type %s as type %s"),
586 rhs
->named_type()->message_name().c_str(),
587 lhs
->named_type()->message_name().c_str());
596 // Return true if a value with type RHS may be converted to type LHS.
597 // If REASON is not NULL, set *REASON to the reason the types are not
601 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
603 // The types are convertible if they are assignable.
604 if (Type::are_assignable(lhs
, rhs
, reason
))
607 // The types are convertible if they have identical underlying
609 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
610 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
613 // The types are convertible if they are both unnamed pointer types
614 // and their pointer base types have identical underlying types.
615 if (lhs
->named_type() == NULL
616 && rhs
->named_type() == NULL
617 && lhs
->points_to() != NULL
618 && rhs
->points_to() != NULL
619 && (lhs
->points_to()->named_type() != NULL
620 || rhs
->points_to()->named_type() != NULL
)
621 && Type::are_identical(lhs
->points_to()->base(),
622 rhs
->points_to()->base(),
627 // Integer and floating point types are convertible to each other.
628 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
629 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
632 // Complex types are convertible to each other.
633 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
636 // An integer, or []byte, or []int, may be converted to a string.
637 if (lhs
->is_string_type())
639 if (rhs
->integer_type() != NULL
)
641 if (rhs
->is_open_array_type() && rhs
->named_type() == NULL
)
643 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
644 if (e
->integer_type() != NULL
645 && (e
== Type::lookup_integer_type("uint8")
646 || e
== Type::lookup_integer_type("int")))
651 // A string may be converted to []byte or []int.
652 if (rhs
->is_string_type()
653 && lhs
->is_open_array_type()
654 && lhs
->named_type() == NULL
)
656 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
657 if (e
->integer_type() != NULL
658 && (e
== Type::lookup_integer_type("uint8")
659 || e
== Type::lookup_integer_type("int")))
663 // An unsafe.Pointer type may be converted to any pointer type or to
664 // uintptr, and vice-versa.
665 if (lhs
->is_unsafe_pointer_type()
666 && (rhs
->points_to() != NULL
667 || (rhs
->integer_type() != NULL
668 && rhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
670 if (rhs
->is_unsafe_pointer_type()
671 && (lhs
->points_to() != NULL
672 || (lhs
->integer_type() != NULL
673 && lhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
676 // Give a better error message.
680 *reason
= "invalid type conversion";
683 std::string s
= "invalid type conversion (";
693 // Return whether this type has any hidden fields. This is only a
694 // possibility for a few types.
697 Type::has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
699 switch (this->forwarded()->classification_
)
702 return this->named_type()->named_type_has_hidden_fields(reason
);
704 return this->struct_type()->struct_has_hidden_fields(within
, reason
);
706 return this->array_type()->array_has_hidden_fields(within
, reason
);
712 // Return a hash code for the type to be used for method lookup.
715 Type::hash_for_method(Gogo
* gogo
) const
717 unsigned int ret
= 0;
718 if (this->classification_
!= TYPE_FORWARD
)
719 ret
+= this->classification_
;
720 return ret
+ this->do_hash_for_method(gogo
);
723 // Default implementation of do_hash_for_method. This is appropriate
724 // for types with no subfields.
727 Type::do_hash_for_method(Gogo
*) const
732 // Return a hash code for a string, given a starting hash.
735 Type::hash_string(const std::string
& s
, unsigned int h
)
737 const char* p
= s
.data();
738 size_t len
= s
.length();
739 for (; len
> 0; --len
)
747 // Default check for the expression passed to make. Any type which
748 // may be used with make implements its own version of this.
751 Type::do_check_make_expression(Expression_list
*, source_location
)
756 // Return whether an expression has an integer value. Report an error
757 // if not. This is used when handling calls to the predeclared make
761 Type::check_int_value(Expression
* e
, const char* errmsg
,
762 source_location location
)
764 if (e
->type()->integer_type() != NULL
)
767 // Check for a floating point constant with integer value.
772 if (e
->float_constant_value(fval
, &dummy
))
779 mpfr_clear_overflow();
780 mpfr_clear_erangeflag();
781 mpfr_get_z(ival
, fval
, GMP_RNDN
);
782 if (!mpfr_overflow_p()
783 && !mpfr_erangeflag_p()
784 && mpz_sgn(ival
) >= 0)
786 Named_type
* ntype
= Type::lookup_integer_type("int");
787 Integer_type
* inttype
= ntype
->integer_type();
789 mpz_init_set_ui(max
, 1);
790 mpz_mul_2exp(max
, max
, inttype
->bits() - 1);
791 ok
= mpz_cmp(ival
, max
) < 0;
805 error_at(location
, "%s", errmsg
);
809 // A hash table mapping unnamed types to trees.
811 Type::Type_trees
Type::type_trees
;
813 // Return a tree representing this type.
816 Type::get_tree(Gogo
* gogo
)
818 if (this->tree_
!= NULL
)
821 if (this->forward_declaration_type() != NULL
822 || this->named_type() != NULL
)
823 return this->get_tree_without_hash(gogo
);
825 if (this->is_error_type())
826 return error_mark_node
;
828 // To avoid confusing GIMPLE, we need to translate all identical Go
829 // types to the same GIMPLE type. We use a hash table to do that.
830 // There is no need to use the hash table for named types, as named
831 // types are only identical to themselves.
833 std::pair
<Type
*, tree
> val(this, NULL
);
834 std::pair
<Type_trees::iterator
, bool> ins
=
835 Type::type_trees
.insert(val
);
836 if (!ins
.second
&& ins
.first
->second
!= NULL_TREE
)
838 this->tree_
= ins
.first
->second
;
842 tree t
= this->get_tree_without_hash(gogo
);
844 if (ins
.first
->second
== NULL_TREE
)
845 ins
.first
->second
= t
;
848 // We have already created a tree for this type. This can
849 // happen when an unnamed type is defined using a named type
850 // which in turns uses an identical unnamed type. Use the tree
851 // we created earlier and ignore the one we just built.
852 t
= ins
.first
->second
;
859 // Return a tree for a type without looking in the hash table for
860 // identical types. This is used for named types, since there is no
861 // point to looking in the hash table for them.
864 Type::get_tree_without_hash(Gogo
* gogo
)
866 if (this->tree_
== NULL_TREE
)
868 tree t
= this->do_get_tree(gogo
);
870 // For a recursive function or pointer type, we will temporarily
871 // return ptr_type_node during the recursion. We don't want to
872 // record that for a forwarding type, as it may confuse us
874 if (t
== ptr_type_node
&& this->forward_declaration_type() != NULL
)
878 go_preserve_from_gc(t
);
884 // Return a tree representing a zero initialization for this type.
887 Type::get_init_tree(Gogo
* gogo
, bool is_clear
)
889 tree type_tree
= this->get_tree(gogo
);
890 if (type_tree
== error_mark_node
)
891 return error_mark_node
;
892 return this->do_get_init_tree(gogo
, type_tree
, is_clear
);
895 // Any type which supports the builtin make function must implement
899 Type::do_make_expression_tree(Translate_context
*, Expression_list
*,
905 // Return a pointer to the type descriptor for this type.
908 Type::type_descriptor_pointer(Gogo
* gogo
)
910 Type
* t
= this->forwarded();
911 if (t
->type_descriptor_decl_
== NULL_TREE
)
913 Expression
* e
= t
->do_type_descriptor(gogo
, NULL
);
914 gogo
->build_type_descriptor_decl(t
, e
, &t
->type_descriptor_decl_
);
915 gcc_assert(t
->type_descriptor_decl_
!= NULL_TREE
916 && (t
->type_descriptor_decl_
== error_mark_node
917 || DECL_P(t
->type_descriptor_decl_
)));
919 if (t
->type_descriptor_decl_
== error_mark_node
)
920 return error_mark_node
;
921 return build_fold_addr_expr(t
->type_descriptor_decl_
);
924 // Return a composite literal for a type descriptor.
927 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
929 return type
->do_type_descriptor(gogo
, NULL
);
932 // Return a composite literal for a type descriptor with a name.
935 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
937 gcc_assert(name
!= NULL
&& type
->named_type() != name
);
938 return type
->do_type_descriptor(gogo
, name
);
941 // Make a builtin struct type from a list of fields. The fields are
942 // pairs of a name and a type.
945 Type::make_builtin_struct_type(int nfields
, ...)
948 va_start(ap
, nfields
);
950 source_location bloc
= BUILTINS_LOCATION
;
951 Struct_field_list
* sfl
= new Struct_field_list();
952 for (int i
= 0; i
< nfields
; i
++)
954 const char* field_name
= va_arg(ap
, const char *);
955 Type
* type
= va_arg(ap
, Type
*);
956 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
961 return Type::make_struct_type(sfl
, bloc
);
964 // Make a builtin named type.
967 Type::make_builtin_named_type(const char* name
, Type
* type
)
969 source_location bloc
= BUILTINS_LOCATION
;
970 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
971 return no
->type_value();
974 // Return the type of a type descriptor. We should really tie this to
975 // runtime.Type rather than copying it. This must match commonType in
976 // libgo/go/runtime/type.go.
979 Type::make_type_descriptor_type()
984 source_location bloc
= BUILTINS_LOCATION
;
986 Type
* uint8_type
= Type::lookup_integer_type("uint8");
987 Type
* uint32_type
= Type::lookup_integer_type("uint32");
988 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
989 Type
* string_type
= Type::lookup_string_type();
990 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
992 // This is an unnamed version of unsafe.Pointer. Perhaps we
993 // should use the named version instead, although that would
994 // require us to create the unsafe package if it has not been
995 // imported. It probably doesn't matter.
996 Type
* void_type
= Type::make_void_type();
997 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
999 // Forward declaration for the type descriptor type.
1000 Named_object
* named_type_descriptor_type
=
1001 Named_object::make_type_declaration("commonType", NULL
, bloc
);
1002 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
1003 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
1005 // The type of a method on a concrete type.
1006 Struct_type
* method_type
=
1007 Type::make_builtin_struct_type(5,
1008 "name", pointer_string_type
,
1009 "pkgPath", pointer_string_type
,
1010 "mtyp", pointer_type_descriptor_type
,
1011 "typ", pointer_type_descriptor_type
,
1012 "tfn", unsafe_pointer_type
);
1013 Named_type
* named_method_type
=
1014 Type::make_builtin_named_type("method", method_type
);
1016 // Information for types with a name or methods.
1017 Type
* slice_named_method_type
=
1018 Type::make_array_type(named_method_type
, NULL
);
1019 Struct_type
* uncommon_type
=
1020 Type::make_builtin_struct_type(3,
1021 "name", pointer_string_type
,
1022 "pkgPath", pointer_string_type
,
1023 "methods", slice_named_method_type
);
1024 Named_type
* named_uncommon_type
=
1025 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1027 Type
* pointer_uncommon_type
=
1028 Type::make_pointer_type(named_uncommon_type
);
1030 // The type descriptor type.
1032 Typed_identifier_list
* params
= new Typed_identifier_list();
1033 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1034 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1036 Typed_identifier_list
* results
= new Typed_identifier_list();
1037 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1039 Type
* hashfn_type
= Type::make_function_type(NULL
, params
, results
, bloc
);
1041 params
= new Typed_identifier_list();
1042 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1043 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1044 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1046 results
= new Typed_identifier_list();
1047 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1049 Type
* equalfn_type
= Type::make_function_type(NULL
, params
, results
,
1052 Struct_type
* type_descriptor_type
=
1053 Type::make_builtin_struct_type(9,
1055 "align", uint8_type
,
1056 "fieldAlign", uint8_type
,
1057 "size", uintptr_type
,
1058 "hash", uint32_type
,
1059 "hashfn", hashfn_type
,
1060 "equalfn", equalfn_type
,
1061 "string", pointer_string_type
,
1062 "", pointer_uncommon_type
);
1064 Named_type
* named
= Type::make_builtin_named_type("commonType",
1065 type_descriptor_type
);
1067 named_type_descriptor_type
->set_type_value(named
);
1075 // Make the type of a pointer to a type descriptor as represented in
1079 Type::make_type_descriptor_ptr_type()
1083 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1087 // Return the names of runtime functions which compute a hash code for
1088 // this type and which compare whether two values of this type are
1092 Type::type_functions(const char** hash_fn
, const char** equal_fn
) const
1094 switch (this->base()->classification())
1096 case Type::TYPE_ERROR
:
1097 case Type::TYPE_VOID
:
1098 case Type::TYPE_NIL
:
1099 // These types can not be hashed or compared.
1100 *hash_fn
= "__go_type_hash_error";
1101 *equal_fn
= "__go_type_equal_error";
1104 case Type::TYPE_BOOLEAN
:
1105 case Type::TYPE_INTEGER
:
1106 case Type::TYPE_FLOAT
:
1107 case Type::TYPE_COMPLEX
:
1108 case Type::TYPE_POINTER
:
1109 case Type::TYPE_FUNCTION
:
1110 case Type::TYPE_MAP
:
1111 case Type::TYPE_CHANNEL
:
1112 *hash_fn
= "__go_type_hash_identity";
1113 *equal_fn
= "__go_type_equal_identity";
1116 case Type::TYPE_STRING
:
1117 *hash_fn
= "__go_type_hash_string";
1118 *equal_fn
= "__go_type_equal_string";
1121 case Type::TYPE_STRUCT
:
1122 case Type::TYPE_ARRAY
:
1123 // These types can not be hashed or compared.
1124 *hash_fn
= "__go_type_hash_error";
1125 *equal_fn
= "__go_type_equal_error";
1128 case Type::TYPE_INTERFACE
:
1129 if (this->interface_type()->is_empty())
1131 *hash_fn
= "__go_type_hash_empty_interface";
1132 *equal_fn
= "__go_type_equal_empty_interface";
1136 *hash_fn
= "__go_type_hash_interface";
1137 *equal_fn
= "__go_type_equal_interface";
1141 case Type::TYPE_NAMED
:
1142 case Type::TYPE_FORWARD
:
1150 // Return a composite literal for the type descriptor for a plain type
1151 // of kind RUNTIME_TYPE_KIND named NAME.
1154 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1155 Named_type
* name
, const Methods
* methods
,
1156 bool only_value_methods
)
1158 source_location bloc
= BUILTINS_LOCATION
;
1160 Type
* td_type
= Type::make_type_descriptor_type();
1161 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1163 Expression_list
* vals
= new Expression_list();
1166 Struct_field_list::const_iterator p
= fields
->begin();
1167 gcc_assert(p
->field_name() == "Kind");
1169 mpz_init_set_ui(iv
, runtime_type_kind
);
1170 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1173 gcc_assert(p
->field_name() == "align");
1174 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1175 vals
->push_back(Expression::make_type_info(this, type_info
));
1178 gcc_assert(p
->field_name() == "fieldAlign");
1179 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1180 vals
->push_back(Expression::make_type_info(this, type_info
));
1183 gcc_assert(p
->field_name() == "size");
1184 type_info
= Expression::TYPE_INFO_SIZE
;
1185 vals
->push_back(Expression::make_type_info(this, type_info
));
1188 gcc_assert(p
->field_name() == "hash");
1189 mpz_set_ui(iv
, this->hash_for_method(gogo
));
1190 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1192 const char* hash_fn
;
1193 const char* equal_fn
;
1194 this->type_functions(&hash_fn
, &equal_fn
);
1197 gcc_assert(p
->field_name() == "hashfn");
1198 Function_type
* fntype
= p
->type()->function_type();
1199 Named_object
* no
= Named_object::make_function_declaration(hash_fn
, NULL
,
1202 no
->func_declaration_value()->set_asm_name(hash_fn
);
1203 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1206 gcc_assert(p
->field_name() == "equalfn");
1207 fntype
= p
->type()->function_type();
1208 no
= Named_object::make_function_declaration(equal_fn
, NULL
, fntype
, bloc
);
1209 no
->func_declaration_value()->set_asm_name(equal_fn
);
1210 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1213 gcc_assert(p
->field_name() == "string");
1214 Expression
* s
= Expression::make_string((name
!= NULL
1215 ? name
->reflection(gogo
)
1216 : this->reflection(gogo
)),
1218 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1221 gcc_assert(p
->field_name() == "uncommonType");
1222 if (name
== NULL
&& methods
== NULL
)
1223 vals
->push_back(Expression::make_nil(bloc
));
1226 if (methods
== NULL
)
1227 methods
= name
->methods();
1228 vals
->push_back(this->uncommon_type_constructor(gogo
,
1231 only_value_methods
));
1235 gcc_assert(p
== fields
->end());
1239 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
1242 // Return a composite literal for the uncommon type information for
1243 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1244 // struct. If name is not NULL, it is the name of the type. If
1245 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1246 // is true if only value methods should be included. At least one of
1247 // NAME and METHODS must not be NULL.
1250 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
1251 Named_type
* name
, const Methods
* methods
,
1252 bool only_value_methods
) const
1254 source_location bloc
= BUILTINS_LOCATION
;
1256 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
1258 Expression_list
* vals
= new Expression_list();
1261 Struct_field_list::const_iterator p
= fields
->begin();
1262 gcc_assert(p
->field_name() == "name");
1265 gcc_assert(p
->field_name() == "pkgPath");
1269 vals
->push_back(Expression::make_nil(bloc
));
1270 vals
->push_back(Expression::make_nil(bloc
));
1274 Named_object
* no
= name
->named_object();
1275 std::string n
= Gogo::unpack_hidden_name(no
->name());
1276 Expression
* s
= Expression::make_string(n
, bloc
);
1277 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1279 if (name
->is_builtin())
1280 vals
->push_back(Expression::make_nil(bloc
));
1283 const Package
* package
= no
->package();
1284 const std::string
& unique_prefix(package
== NULL
1285 ? gogo
->unique_prefix()
1286 : package
->unique_prefix());
1287 const std::string
& package_name(package
== NULL
1288 ? gogo
->package_name()
1290 n
.assign(unique_prefix
);
1292 n
.append(package_name
);
1293 if (name
->in_function() != NULL
)
1296 n
.append(Gogo::unpack_hidden_name(name
->in_function()->name()));
1298 s
= Expression::make_string(n
, bloc
);
1299 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1304 gcc_assert(p
->field_name() == "methods");
1305 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
1306 only_value_methods
));
1309 gcc_assert(p
== fields
->end());
1311 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
1313 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
1316 // Sort methods by name.
1322 operator()(const std::pair
<std::string
, const Method
*>& m1
,
1323 const std::pair
<std::string
, const Method
*>& m2
) const
1324 { return m1
.first
< m2
.first
; }
1327 // Return a composite literal for the type method table for this type.
1328 // METHODS_TYPE is the type of the table, and is a slice type.
1329 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1330 // then only value methods are used.
1333 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
1334 const Methods
* methods
,
1335 bool only_value_methods
) const
1337 source_location bloc
= BUILTINS_LOCATION
;
1339 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
1340 if (methods
!= NULL
)
1342 smethods
.reserve(methods
->count());
1343 for (Methods::const_iterator p
= methods
->begin();
1344 p
!= methods
->end();
1347 if (p
->second
->is_ambiguous())
1349 if (only_value_methods
&& !p
->second
->is_value_method())
1351 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
1355 if (smethods
.empty())
1356 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
1358 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
1360 Type
* method_type
= methods_type
->array_type()->element_type();
1362 Expression_list
* vals
= new Expression_list();
1363 vals
->reserve(smethods
.size());
1364 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
1366 p
!= smethods
.end();
1368 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
1371 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
1374 // Return a composite literal for a single method. METHOD_TYPE is the
1375 // type of the entry. METHOD_NAME is the name of the method and M is
1376 // the method information.
1379 Type::method_constructor(Gogo
*, Type
* method_type
,
1380 const std::string
& method_name
,
1381 const Method
* m
) const
1383 source_location bloc
= BUILTINS_LOCATION
;
1385 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
1387 Expression_list
* vals
= new Expression_list();
1390 Struct_field_list::const_iterator p
= fields
->begin();
1391 gcc_assert(p
->field_name() == "name");
1392 const std::string n
= Gogo::unpack_hidden_name(method_name
);
1393 Expression
* s
= Expression::make_string(n
, bloc
);
1394 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1397 gcc_assert(p
->field_name() == "pkgPath");
1398 if (!Gogo::is_hidden_name(method_name
))
1399 vals
->push_back(Expression::make_nil(bloc
));
1402 s
= Expression::make_string(Gogo::hidden_name_prefix(method_name
), bloc
);
1403 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1406 Named_object
* no
= (m
->needs_stub_method()
1408 : m
->named_object());
1410 Function_type
* mtype
;
1411 if (no
->is_function())
1412 mtype
= no
->func_value()->type();
1414 mtype
= no
->func_declaration_value()->type();
1415 gcc_assert(mtype
->is_method());
1416 Type
* nonmethod_type
= mtype
->copy_without_receiver();
1419 gcc_assert(p
->field_name() == "mtyp");
1420 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
1423 gcc_assert(p
->field_name() == "typ");
1424 vals
->push_back(Expression::make_type_descriptor(mtype
, bloc
));
1427 gcc_assert(p
->field_name() == "tfn");
1428 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1431 gcc_assert(p
== fields
->end());
1433 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
1436 // Return a composite literal for the type descriptor of a plain type.
1437 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1438 // NULL, it is the name to use as well as the list of methods.
1441 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
1444 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
1448 // Return the type reflection string for this type.
1451 Type::reflection(Gogo
* gogo
) const
1455 // The do_reflection virtual function should set RET to the
1456 // reflection string.
1457 this->do_reflection(gogo
, &ret
);
1462 // Return a mangled name for the type.
1465 Type::mangled_name(Gogo
* gogo
) const
1469 // The do_mangled_name virtual function should set RET to the
1470 // mangled name. For a composite type it should append a code for
1471 // the composition and then call do_mangled_name on the components.
1472 this->do_mangled_name(gogo
, &ret
);
1477 // Default function to export a type.
1480 Type::do_export(Export
*) const
1488 Type::import_type(Import
* imp
)
1490 if (imp
->match_c_string("("))
1491 return Function_type::do_import(imp
);
1492 else if (imp
->match_c_string("*"))
1493 return Pointer_type::do_import(imp
);
1494 else if (imp
->match_c_string("struct "))
1495 return Struct_type::do_import(imp
);
1496 else if (imp
->match_c_string("["))
1497 return Array_type::do_import(imp
);
1498 else if (imp
->match_c_string("map "))
1499 return Map_type::do_import(imp
);
1500 else if (imp
->match_c_string("chan "))
1501 return Channel_type::do_import(imp
);
1502 else if (imp
->match_c_string("interface"))
1503 return Interface_type::do_import(imp
);
1506 error_at(imp
->location(), "import error: expected type");
1507 return Type::make_error_type();
1511 // A type used to indicate a parsing error. This exists to simplify
1512 // later error detection.
1514 class Error_type
: public Type
1524 { return error_mark_node
; }
1527 do_get_init_tree(Gogo
*, tree
, bool)
1528 { return error_mark_node
; }
1531 do_type_descriptor(Gogo
*, Named_type
*)
1532 { return Expression::make_error(BUILTINS_LOCATION
); }
1535 do_reflection(Gogo
*, std::string
*) const
1536 { gcc_assert(saw_errors()); }
1539 do_mangled_name(Gogo
*, std::string
* ret
) const
1540 { ret
->push_back('E'); }
1544 Type::make_error_type()
1546 static Error_type singleton_error_type
;
1547 return &singleton_error_type
;
1552 class Void_type
: public Type
1562 { return void_type_node
; }
1565 do_get_init_tree(Gogo
*, tree
, bool)
1566 { gcc_unreachable(); }
1569 do_type_descriptor(Gogo
*, Named_type
*)
1570 { gcc_unreachable(); }
1573 do_reflection(Gogo
*, std::string
*) const
1577 do_mangled_name(Gogo
*, std::string
* ret
) const
1578 { ret
->push_back('v'); }
1582 Type::make_void_type()
1584 static Void_type singleton_void_type
;
1585 return &singleton_void_type
;
1588 // The boolean type.
1590 class Boolean_type
: public Type
1594 : Type(TYPE_BOOLEAN
)
1600 { return boolean_type_node
; }
1603 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1604 { return is_clear
? NULL
: fold_convert(type_tree
, boolean_false_node
); }
1607 do_type_descriptor(Gogo
*, Named_type
* name
);
1609 // We should not be asked for the reflection string of a basic type.
1611 do_reflection(Gogo
*, std::string
* ret
) const
1612 { ret
->append("bool"); }
1615 do_mangled_name(Gogo
*, std::string
* ret
) const
1616 { ret
->push_back('b'); }
1619 // Make the type descriptor.
1622 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1625 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
1628 Named_object
* no
= gogo
->lookup_global("bool");
1629 gcc_assert(no
!= NULL
);
1630 return Type::type_descriptor(gogo
, no
->type_value());
1635 Type::make_boolean_type()
1637 static Boolean_type boolean_type
;
1638 return &boolean_type
;
1641 // The named type "bool".
1643 static Named_type
* named_bool_type
;
1645 // Get the named type "bool".
1648 Type::lookup_bool_type()
1650 return named_bool_type
;
1653 // Make the named type "bool".
1656 Type::make_named_bool_type()
1658 Type
* bool_type
= Type::make_boolean_type();
1659 Named_object
* named_object
= Named_object::make_type("bool", NULL
,
1662 Named_type
* named_type
= named_object
->type_value();
1663 named_bool_type
= named_type
;
1667 // Class Integer_type.
1669 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
1671 // Create a new integer type. Non-abstract integer types always have
1675 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
1676 int bits
, int runtime_type_kind
)
1678 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
1680 std::string
sname(name
);
1681 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1684 Named_type
* named_type
= named_object
->type_value();
1685 std::pair
<Named_integer_types::iterator
, bool> ins
=
1686 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
1687 gcc_assert(ins
.second
);
1691 // Look up an existing integer type.
1694 Integer_type::lookup_integer_type(const char* name
)
1696 Named_integer_types::const_iterator p
=
1697 Integer_type::named_integer_types
.find(name
);
1698 gcc_assert(p
!= Integer_type::named_integer_types
.end());
1702 // Create a new abstract integer type.
1705 Integer_type::create_abstract_integer_type()
1707 static Integer_type
* abstract_type
;
1708 if (abstract_type
== NULL
)
1709 abstract_type
= new Integer_type(true, false, INT_TYPE_SIZE
,
1710 RUNTIME_TYPE_KIND_INT
);
1711 return abstract_type
;
1714 // Integer type compatibility.
1717 Integer_type::is_identical(const Integer_type
* t
) const
1719 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
1721 return this->is_abstract_
== t
->is_abstract_
;
1727 Integer_type::do_hash_for_method(Gogo
*) const
1729 return ((this->bits_
<< 4)
1730 + ((this->is_unsigned_
? 1 : 0) << 8)
1731 + ((this->is_abstract_
? 1 : 0) << 9));
1734 // Get the tree for an Integer_type.
1737 Integer_type::do_get_tree(Gogo
*)
1739 gcc_assert(!this->is_abstract_
);
1740 if (this->is_unsigned_
)
1742 if (this->bits_
== INT_TYPE_SIZE
)
1743 return unsigned_type_node
;
1744 else if (this->bits_
== CHAR_TYPE_SIZE
)
1745 return unsigned_char_type_node
;
1746 else if (this->bits_
== SHORT_TYPE_SIZE
)
1747 return short_unsigned_type_node
;
1748 else if (this->bits_
== LONG_TYPE_SIZE
)
1749 return long_unsigned_type_node
;
1750 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1751 return long_long_unsigned_type_node
;
1753 return make_unsigned_type(this->bits_
);
1757 if (this->bits_
== INT_TYPE_SIZE
)
1758 return integer_type_node
;
1759 else if (this->bits_
== CHAR_TYPE_SIZE
)
1760 return signed_char_type_node
;
1761 else if (this->bits_
== SHORT_TYPE_SIZE
)
1762 return short_integer_type_node
;
1763 else if (this->bits_
== LONG_TYPE_SIZE
)
1764 return long_integer_type_node
;
1765 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1766 return long_long_integer_type_node
;
1768 return make_signed_type(this->bits_
);
1773 Integer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1775 return is_clear
? NULL
: build_int_cst(type_tree
, 0);
1778 // The type descriptor for an integer type. Integer types are always
1782 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1784 gcc_assert(name
!= NULL
);
1785 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1788 // We should not be asked for the reflection string of a basic type.
1791 Integer_type::do_reflection(Gogo
*, std::string
*) const
1799 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1802 snprintf(buf
, sizeof buf
, "i%s%s%de",
1803 this->is_abstract_
? "a" : "",
1804 this->is_unsigned_
? "u" : "",
1809 // Make an integer type.
1812 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
1813 int runtime_type_kind
)
1815 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
1819 // Make an abstract integer type.
1822 Type::make_abstract_integer_type()
1824 return Integer_type::create_abstract_integer_type();
1827 // Look up an integer type.
1830 Type::lookup_integer_type(const char* name
)
1832 return Integer_type::lookup_integer_type(name
);
1835 // Class Float_type.
1837 Float_type::Named_float_types
Float_type::named_float_types
;
1839 // Create a new float type. Non-abstract float types always have
1843 Float_type::create_float_type(const char* name
, int bits
,
1844 int runtime_type_kind
)
1846 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
1847 std::string
sname(name
);
1848 Named_object
* named_object
= Named_object::make_type(sname
, NULL
, float_type
,
1850 Named_type
* named_type
= named_object
->type_value();
1851 std::pair
<Named_float_types::iterator
, bool> ins
=
1852 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
1853 gcc_assert(ins
.second
);
1857 // Look up an existing float type.
1860 Float_type::lookup_float_type(const char* name
)
1862 Named_float_types::const_iterator p
=
1863 Float_type::named_float_types
.find(name
);
1864 gcc_assert(p
!= Float_type::named_float_types
.end());
1868 // Create a new abstract float type.
1871 Float_type::create_abstract_float_type()
1873 static Float_type
* abstract_type
;
1874 if (abstract_type
== NULL
)
1875 abstract_type
= new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64
);
1876 return abstract_type
;
1879 // Whether this type is identical with T.
1882 Float_type::is_identical(const Float_type
* t
) const
1884 if (this->bits_
!= t
->bits_
)
1886 return this->is_abstract_
== t
->is_abstract_
;
1892 Float_type::do_hash_for_method(Gogo
*) const
1894 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
1897 // Get a tree without using a Gogo*.
1900 Float_type::type_tree() const
1902 if (this->bits_
== FLOAT_TYPE_SIZE
)
1903 return float_type_node
;
1904 else if (this->bits_
== DOUBLE_TYPE_SIZE
)
1905 return double_type_node
;
1906 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
)
1907 return long_double_type_node
;
1910 tree ret
= make_node(REAL_TYPE
);
1911 TYPE_PRECISION(ret
) = this->bits_
;
1920 Float_type::do_get_tree(Gogo
*)
1922 return this->type_tree();
1926 Float_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1931 real_from_integer(&r
, TYPE_MODE(type_tree
), 0, 0, 0);
1932 return build_real(type_tree
, r
);
1935 // The type descriptor for a float type. Float types are always named.
1938 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1940 gcc_assert(name
!= NULL
);
1941 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1944 // We should not be asked for the reflection string of a basic type.
1947 Float_type::do_reflection(Gogo
*, std::string
*) const
1955 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1958 snprintf(buf
, sizeof buf
, "f%s%de",
1959 this->is_abstract_
? "a" : "",
1964 // Make a floating point type.
1967 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
1969 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
1972 // Make an abstract float type.
1975 Type::make_abstract_float_type()
1977 return Float_type::create_abstract_float_type();
1980 // Look up a float type.
1983 Type::lookup_float_type(const char* name
)
1985 return Float_type::lookup_float_type(name
);
1988 // Class Complex_type.
1990 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
1992 // Create a new complex type. Non-abstract complex types always have
1996 Complex_type::create_complex_type(const char* name
, int bits
,
1997 int runtime_type_kind
)
1999 Complex_type
* complex_type
= new Complex_type(false, bits
,
2001 std::string
sname(name
);
2002 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
2005 Named_type
* named_type
= named_object
->type_value();
2006 std::pair
<Named_complex_types::iterator
, bool> ins
=
2007 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
2009 gcc_assert(ins
.second
);
2013 // Look up an existing complex type.
2016 Complex_type::lookup_complex_type(const char* name
)
2018 Named_complex_types::const_iterator p
=
2019 Complex_type::named_complex_types
.find(name
);
2020 gcc_assert(p
!= Complex_type::named_complex_types
.end());
2024 // Create a new abstract complex type.
2027 Complex_type::create_abstract_complex_type()
2029 static Complex_type
* abstract_type
;
2030 if (abstract_type
== NULL
)
2031 abstract_type
= new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128
);
2032 return abstract_type
;
2035 // Whether this type is identical with T.
2038 Complex_type::is_identical(const Complex_type
*t
) const
2040 if (this->bits_
!= t
->bits_
)
2042 return this->is_abstract_
== t
->is_abstract_
;
2048 Complex_type::do_hash_for_method(Gogo
*) const
2050 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2053 // Get a tree without using a Gogo*.
2056 Complex_type::type_tree() const
2058 if (this->bits_
== FLOAT_TYPE_SIZE
* 2)
2059 return complex_float_type_node
;
2060 else if (this->bits_
== DOUBLE_TYPE_SIZE
* 2)
2061 return complex_double_type_node
;
2062 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
* 2)
2063 return complex_long_double_type_node
;
2066 tree ret
= make_node(REAL_TYPE
);
2067 TYPE_PRECISION(ret
) = this->bits_
/ 2;
2069 return build_complex_type(ret
);
2076 Complex_type::do_get_tree(Gogo
*)
2078 return this->type_tree();
2081 // Zero initializer.
2084 Complex_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2089 real_from_integer(&r
, TYPE_MODE(TREE_TYPE(type_tree
)), 0, 0, 0);
2090 return build_complex(type_tree
, build_real(TREE_TYPE(type_tree
), r
),
2091 build_real(TREE_TYPE(type_tree
), r
));
2094 // The type descriptor for a complex type. Complex types are always
2098 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2100 gcc_assert(name
!= NULL
);
2101 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2104 // We should not be asked for the reflection string of a basic type.
2107 Complex_type::do_reflection(Gogo
*, std::string
*) const
2115 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2118 snprintf(buf
, sizeof buf
, "c%s%de",
2119 this->is_abstract_
? "a" : "",
2124 // Make a complex type.
2127 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
2129 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
2132 // Make an abstract complex type.
2135 Type::make_abstract_complex_type()
2137 return Complex_type::create_abstract_complex_type();
2140 // Look up a complex type.
2143 Type::lookup_complex_type(const char* name
)
2145 return Complex_type::lookup_complex_type(name
);
2148 // Class String_type.
2150 // Return the tree for String_type. A string is a struct with two
2151 // fields: a pointer to the characters and a length.
2154 String_type::do_get_tree(Gogo
*)
2156 static tree struct_type
;
2157 return Gogo::builtin_struct(&struct_type
, "__go_string", NULL_TREE
, 2,
2159 build_pointer_type(unsigned_char_type_node
),
2164 // Return a tree for the length of STRING.
2167 String_type::length_tree(Gogo
*, tree string
)
2169 tree string_type
= TREE_TYPE(string
);
2170 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2171 tree length_field
= DECL_CHAIN(TYPE_FIELDS(string_type
));
2172 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field
)),
2174 return fold_build3(COMPONENT_REF
, integer_type_node
, string
,
2175 length_field
, NULL_TREE
);
2178 // Return a tree for a pointer to the bytes of STRING.
2181 String_type::bytes_tree(Gogo
*, tree string
)
2183 tree string_type
= TREE_TYPE(string
);
2184 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2185 tree bytes_field
= TYPE_FIELDS(string_type
);
2186 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field
)),
2188 return fold_build3(COMPONENT_REF
, TREE_TYPE(bytes_field
), string
,
2189 bytes_field
, NULL_TREE
);
2192 // We initialize a string to { NULL, 0 }.
2195 String_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2200 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2202 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2204 for (tree field
= TYPE_FIELDS(type_tree
);
2206 field
= DECL_CHAIN(field
))
2208 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2210 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
2213 tree ret
= build_constructor(type_tree
, init
);
2214 TREE_CONSTANT(ret
) = 1;
2218 // The type descriptor for the string type.
2221 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2224 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
2227 Named_object
* no
= gogo
->lookup_global("string");
2228 gcc_assert(no
!= NULL
);
2229 return Type::type_descriptor(gogo
, no
->type_value());
2233 // We should not be asked for the reflection string of a basic type.
2236 String_type::do_reflection(Gogo
*, std::string
* ret
) const
2238 ret
->append("string");
2241 // Mangled name of a string type.
2244 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2246 ret
->push_back('z');
2249 // Make a string type.
2252 Type::make_string_type()
2254 static String_type string_type
;
2255 return &string_type
;
2258 // The named type "string".
2260 static Named_type
* named_string_type
;
2262 // Get the named type "string".
2265 Type::lookup_string_type()
2267 return named_string_type
;
2270 // Make the named type string.
2273 Type::make_named_string_type()
2275 Type
* string_type
= Type::make_string_type();
2276 Named_object
* named_object
= Named_object::make_type("string", NULL
,
2279 Named_type
* named_type
= named_object
->type_value();
2280 named_string_type
= named_type
;
2284 // The sink type. This is the type of the blank identifier _. Any
2285 // type may be assigned to it.
2287 class Sink_type
: public Type
2297 { gcc_unreachable(); }
2300 do_get_init_tree(Gogo
*, tree
, bool)
2301 { gcc_unreachable(); }
2304 do_type_descriptor(Gogo
*, Named_type
*)
2305 { gcc_unreachable(); }
2308 do_reflection(Gogo
*, std::string
*) const
2309 { gcc_unreachable(); }
2312 do_mangled_name(Gogo
*, std::string
*) const
2313 { gcc_unreachable(); }
2316 // Make the sink type.
2319 Type::make_sink_type()
2321 static Sink_type sink_type
;
2325 // Class Function_type.
2330 Function_type::do_traverse(Traverse
* traverse
)
2332 if (this->receiver_
!= NULL
2333 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
2334 return TRAVERSE_EXIT
;
2335 if (this->parameters_
!= NULL
2336 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
2337 return TRAVERSE_EXIT
;
2338 if (this->results_
!= NULL
2339 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
2340 return TRAVERSE_EXIT
;
2341 return TRAVERSE_CONTINUE
;
2344 // Returns whether T is a valid redeclaration of this type. If this
2345 // returns false, and REASON is not NULL, *REASON may be set to a
2346 // brief explanation of why it returned false.
2349 Function_type::is_valid_redeclaration(const Function_type
* t
,
2350 std::string
* reason
) const
2352 if (!this->is_identical(t
, false, true, reason
))
2355 // A redeclaration of a function is required to use the same names
2356 // for the receiver and parameters.
2357 if (this->receiver() != NULL
2358 && this->receiver()->name() != t
->receiver()->name()
2359 && this->receiver()->name() != Import::import_marker
2360 && t
->receiver()->name() != Import::import_marker
)
2363 *reason
= "receiver name changed";
2367 const Typed_identifier_list
* parms1
= this->parameters();
2368 const Typed_identifier_list
* parms2
= t
->parameters();
2371 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2372 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2373 p2
!= parms2
->end();
2376 if (p1
->name() != p2
->name()
2377 && p1
->name() != Import::import_marker
2378 && p2
->name() != Import::import_marker
)
2381 *reason
= "parameter name changed";
2385 // This is called at parse time, so we may have unknown
2387 Type
* t1
= p1
->type()->forwarded();
2388 Type
* t2
= p2
->type()->forwarded();
2390 && t1
->forward_declaration_type() != NULL
2391 && (t2
->forward_declaration_type() == NULL
2392 || (t1
->forward_declaration_type()->named_object()
2393 != t2
->forward_declaration_type()->named_object())))
2398 const Typed_identifier_list
* results1
= this->results();
2399 const Typed_identifier_list
* results2
= t
->results();
2400 if (results1
!= NULL
)
2402 Typed_identifier_list::const_iterator res1
= results1
->begin();
2403 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2404 res2
!= results2
->end();
2407 if (res1
->name() != res2
->name()
2408 && res1
->name() != Import::import_marker
2409 && res2
->name() != Import::import_marker
)
2412 *reason
= "result name changed";
2416 // This is called at parse time, so we may have unknown
2418 Type
* t1
= res1
->type()->forwarded();
2419 Type
* t2
= res2
->type()->forwarded();
2421 && t1
->forward_declaration_type() != NULL
2422 && (t2
->forward_declaration_type() == NULL
2423 || (t1
->forward_declaration_type()->named_object()
2424 != t2
->forward_declaration_type()->named_object())))
2432 // Check whether T is the same as this type.
2435 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
2436 bool errors_are_identical
,
2437 std::string
* reason
) const
2439 if (!ignore_receiver
)
2441 const Typed_identifier
* r1
= this->receiver();
2442 const Typed_identifier
* r2
= t
->receiver();
2443 if ((r1
!= NULL
) != (r2
!= NULL
))
2446 *reason
= _("different receiver types");
2451 if (!Type::are_identical(r1
->type(), r2
->type(), errors_are_identical
,
2454 if (reason
!= NULL
&& !reason
->empty())
2455 *reason
= "receiver: " + *reason
;
2461 const Typed_identifier_list
* parms1
= this->parameters();
2462 const Typed_identifier_list
* parms2
= t
->parameters();
2463 if ((parms1
!= NULL
) != (parms2
!= NULL
))
2466 *reason
= _("different number of parameters");
2471 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2472 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2473 p2
!= parms2
->end();
2476 if (p1
== parms1
->end())
2479 *reason
= _("different number of parameters");
2483 if (!Type::are_identical(p1
->type(), p2
->type(),
2484 errors_are_identical
, NULL
))
2487 *reason
= _("different parameter types");
2491 if (p1
!= parms1
->end())
2494 *reason
= _("different number of parameters");
2499 if (this->is_varargs() != t
->is_varargs())
2502 *reason
= _("different varargs");
2506 const Typed_identifier_list
* results1
= this->results();
2507 const Typed_identifier_list
* results2
= t
->results();
2508 if ((results1
!= NULL
) != (results2
!= NULL
))
2511 *reason
= _("different number of results");
2514 if (results1
!= NULL
)
2516 Typed_identifier_list::const_iterator res1
= results1
->begin();
2517 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2518 res2
!= results2
->end();
2521 if (res1
== results1
->end())
2524 *reason
= _("different number of results");
2528 if (!Type::are_identical(res1
->type(), res2
->type(),
2529 errors_are_identical
, NULL
))
2532 *reason
= _("different result types");
2536 if (res1
!= results1
->end())
2539 *reason
= _("different number of results");
2550 Function_type::do_hash_for_method(Gogo
* gogo
) const
2552 unsigned int ret
= 0;
2553 // We ignore the receiver type for hash codes, because we need to
2554 // get the same hash code for a method in an interface and a method
2555 // declared for a type. The former will not have a receiver.
2556 if (this->parameters_
!= NULL
)
2559 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2560 p
!= this->parameters_
->end();
2562 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2564 if (this->results_
!= NULL
)
2567 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2568 p
!= this->results_
->end();
2570 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2572 if (this->is_varargs_
)
2578 // Get the tree for a function type.
2581 Function_type::do_get_tree(Gogo
* gogo
)
2583 tree args
= NULL_TREE
;
2586 if (this->receiver_
!= NULL
)
2588 Type
* rtype
= this->receiver_
->type();
2589 tree ptype
= rtype
->get_tree(gogo
);
2590 if (ptype
== error_mark_node
)
2591 return error_mark_node
;
2593 // We always pass the address of the receiver parameter, in
2594 // order to make interface calls work with unknown types.
2595 if (rtype
->points_to() == NULL
)
2596 ptype
= build_pointer_type(ptype
);
2598 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2599 pp
= &TREE_CHAIN (*pp
);
2602 if (this->parameters_
!= NULL
)
2604 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2605 p
!= this->parameters_
->end();
2608 tree ptype
= p
->type()->get_tree(gogo
);
2609 if (ptype
== error_mark_node
)
2610 return error_mark_node
;
2611 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2612 pp
= &TREE_CHAIN (*pp
);
2616 // Varargs is handled entirely at the Go level. At the tree level,
2617 // functions are not varargs.
2618 *pp
= void_list_node
;
2621 if (this->results_
== NULL
)
2622 result
= void_type_node
;
2623 else if (this->results_
->size() == 1)
2624 result
= this->results_
->begin()->type()->get_tree(gogo
);
2627 result
= make_node(RECORD_TYPE
);
2628 tree field_trees
= NULL_TREE
;
2629 tree
* pp
= &field_trees
;
2630 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2631 p
!= this->results_
->end();
2634 const std::string name
= (p
->name().empty()
2636 : Gogo::unpack_hidden_name(p
->name()));
2637 tree name_tree
= get_identifier_with_length(name
.data(),
2639 tree field_type_tree
= p
->type()->get_tree(gogo
);
2640 if (field_type_tree
== error_mark_node
)
2641 return error_mark_node
;
2642 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
2644 DECL_CONTEXT(field
) = result
;
2646 pp
= &DECL_CHAIN(field
);
2648 TYPE_FIELDS(result
) = field_trees
;
2649 layout_type(result
);
2652 if (result
== error_mark_node
)
2653 return error_mark_node
;
2655 tree fntype
= build_function_type(result
, args
);
2656 if (fntype
== error_mark_node
)
2659 return build_pointer_type(fntype
);
2662 // Functions are initialized to NULL.
2665 Function_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2669 return fold_convert(type_tree
, null_pointer_node
);
2672 // The type of a function type descriptor.
2675 Function_type::make_function_type_descriptor_type()
2680 Type
* tdt
= Type::make_type_descriptor_type();
2681 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
2683 Type
* bool_type
= Type::lookup_bool_type();
2685 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
2687 Struct_type
* s
= Type::make_builtin_struct_type(4,
2689 "dotdotdot", bool_type
,
2693 ret
= Type::make_builtin_named_type("FuncType", s
);
2699 // The type descriptor for a function type.
2702 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2704 source_location bloc
= BUILTINS_LOCATION
;
2706 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
2708 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
2710 Expression_list
* vals
= new Expression_list();
2713 Struct_field_list::const_iterator p
= fields
->begin();
2714 gcc_assert(p
->field_name() == "commonType");
2715 vals
->push_back(this->type_descriptor_constructor(gogo
,
2716 RUNTIME_TYPE_KIND_FUNC
,
2720 gcc_assert(p
->field_name() == "dotdotdot");
2721 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
2724 gcc_assert(p
->field_name() == "in");
2725 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
2726 this->parameters()));
2729 gcc_assert(p
->field_name() == "out");
2730 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
2734 gcc_assert(p
== fields
->end());
2736 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
2739 // Return a composite literal for the parameters or results of a type
2743 Function_type::type_descriptor_params(Type
* params_type
,
2744 const Typed_identifier
* receiver
,
2745 const Typed_identifier_list
* params
)
2747 source_location bloc
= BUILTINS_LOCATION
;
2749 if (receiver
== NULL
&& params
== NULL
)
2750 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
2752 Expression_list
* vals
= new Expression_list();
2753 vals
->reserve((params
== NULL
? 0 : params
->size())
2754 + (receiver
!= NULL
? 1 : 0));
2756 if (receiver
!= NULL
)
2758 Type
* rtype
= receiver
->type();
2759 // The receiver is always passed as a pointer. FIXME: Is this
2760 // right? Should that fact affect the type descriptor?
2761 if (rtype
->points_to() == NULL
)
2762 rtype
= Type::make_pointer_type(rtype
);
2763 vals
->push_back(Expression::make_type_descriptor(rtype
, bloc
));
2768 for (Typed_identifier_list::const_iterator p
= params
->begin();
2771 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
2774 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
2777 // The reflection string.
2780 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
2782 // FIXME: Turn this off until we straighten out the type of the
2783 // struct field used in a go statement which calls a method.
2784 // gcc_assert(this->receiver_ == NULL);
2786 ret
->append("func");
2788 if (this->receiver_
!= NULL
)
2790 ret
->push_back('(');
2791 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
2792 ret
->push_back(')');
2795 ret
->push_back('(');
2796 const Typed_identifier_list
* params
= this->parameters();
2799 bool is_varargs
= this->is_varargs_
;
2800 for (Typed_identifier_list::const_iterator p
= params
->begin();
2804 if (p
!= params
->begin())
2806 if (!is_varargs
|| p
+ 1 != params
->end())
2807 this->append_reflection(p
->type(), gogo
, ret
);
2811 this->append_reflection(p
->type()->array_type()->element_type(),
2816 ret
->push_back(')');
2818 const Typed_identifier_list
* results
= this->results();
2819 if (results
!= NULL
&& !results
->empty())
2821 if (results
->size() == 1)
2822 ret
->push_back(' ');
2825 for (Typed_identifier_list::const_iterator p
= results
->begin();
2826 p
!= results
->end();
2829 if (p
!= results
->begin())
2831 this->append_reflection(p
->type(), gogo
, ret
);
2833 if (results
->size() > 1)
2834 ret
->push_back(')');
2841 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
2843 ret
->push_back('F');
2845 if (this->receiver_
!= NULL
)
2847 ret
->push_back('m');
2848 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
2851 const Typed_identifier_list
* params
= this->parameters();
2854 ret
->push_back('p');
2855 for (Typed_identifier_list::const_iterator p
= params
->begin();
2858 this->append_mangled_name(p
->type(), gogo
, ret
);
2859 if (this->is_varargs_
)
2860 ret
->push_back('V');
2861 ret
->push_back('e');
2864 const Typed_identifier_list
* results
= this->results();
2865 if (results
!= NULL
)
2867 ret
->push_back('r');
2868 for (Typed_identifier_list::const_iterator p
= results
->begin();
2869 p
!= results
->end();
2871 this->append_mangled_name(p
->type(), gogo
, ret
);
2872 ret
->push_back('e');
2875 ret
->push_back('e');
2878 // Export a function type.
2881 Function_type::do_export(Export
* exp
) const
2883 // We don't write out the receiver. The only function types which
2884 // should have a receiver are the ones associated with explicitly
2885 // defined methods. For those the receiver type is written out by
2886 // Function::export_func.
2888 exp
->write_c_string("(");
2890 if (this->parameters_
!= NULL
)
2892 bool is_varargs
= this->is_varargs_
;
2893 for (Typed_identifier_list::const_iterator p
=
2894 this->parameters_
->begin();
2895 p
!= this->parameters_
->end();
2901 exp
->write_c_string(", ");
2902 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
2903 exp
->write_type(p
->type());
2906 exp
->write_c_string("...");
2907 exp
->write_type(p
->type()->array_type()->element_type());
2911 exp
->write_c_string(")");
2913 const Typed_identifier_list
* results
= this->results_
;
2914 if (results
!= NULL
)
2916 exp
->write_c_string(" ");
2917 if (results
->size() == 1)
2918 exp
->write_type(results
->begin()->type());
2922 exp
->write_c_string("(");
2923 for (Typed_identifier_list::const_iterator p
= results
->begin();
2924 p
!= results
->end();
2930 exp
->write_c_string(", ");
2931 exp
->write_type(p
->type());
2933 exp
->write_c_string(")");
2938 // Import a function type.
2941 Function_type::do_import(Import
* imp
)
2943 imp
->require_c_string("(");
2944 Typed_identifier_list
* parameters
;
2945 bool is_varargs
= false;
2946 if (imp
->peek_char() == ')')
2950 parameters
= new Typed_identifier_list();
2953 if (imp
->match_c_string("..."))
2959 Type
* ptype
= imp
->read_type();
2961 ptype
= Type::make_array_type(ptype
, NULL
);
2962 parameters
->push_back(Typed_identifier(Import::import_marker
,
2963 ptype
, imp
->location()));
2964 if (imp
->peek_char() != ',')
2966 gcc_assert(!is_varargs
);
2967 imp
->require_c_string(", ");
2970 imp
->require_c_string(")");
2972 Typed_identifier_list
* results
;
2973 if (imp
->peek_char() != ' ')
2978 results
= new Typed_identifier_list
;
2979 if (imp
->peek_char() != '(')
2981 Type
* rtype
= imp
->read_type();
2982 results
->push_back(Typed_identifier(Import::import_marker
, rtype
,
2990 Type
* rtype
= imp
->read_type();
2991 results
->push_back(Typed_identifier(Import::import_marker
,
2992 rtype
, imp
->location()));
2993 if (imp
->peek_char() != ',')
2995 imp
->require_c_string(", ");
2997 imp
->require_c_string(")");
3001 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
3004 ret
->set_is_varargs();
3008 // Make a copy of a function type without a receiver.
3011 Function_type::copy_without_receiver() const
3013 gcc_assert(this->is_method());
3014 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
3017 if (this->is_varargs())
3018 ret
->set_is_varargs();
3019 if (this->is_builtin())
3020 ret
->set_is_builtin();
3024 // Make a copy of a function type with a receiver.
3027 Function_type::copy_with_receiver(Type
* receiver_type
) const
3029 gcc_assert(!this->is_method());
3030 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
3032 return Type::make_function_type(receiver
, this->parameters_
,
3033 this->results_
, this->location_
);
3036 // Make a function type.
3039 Type::make_function_type(Typed_identifier
* receiver
,
3040 Typed_identifier_list
* parameters
,
3041 Typed_identifier_list
* results
,
3042 source_location location
)
3044 return new Function_type(receiver
, parameters
, results
, location
);
3047 // Class Pointer_type.
3052 Pointer_type::do_traverse(Traverse
* traverse
)
3054 return Type::traverse(this->to_type_
, traverse
);
3060 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
3062 return this->to_type_
->hash_for_method(gogo
) << 4;
3065 // The tree for a pointer type.
3068 Pointer_type::do_get_tree(Gogo
* gogo
)
3070 return build_pointer_type(this->to_type_
->get_tree(gogo
));
3073 // Initialize a pointer type.
3076 Pointer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3080 return fold_convert(type_tree
, null_pointer_node
);
3083 // The type of a pointer type descriptor.
3086 Pointer_type::make_pointer_type_descriptor_type()
3091 Type
* tdt
= Type::make_type_descriptor_type();
3092 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3094 Struct_type
* s
= Type::make_builtin_struct_type(2,
3098 ret
= Type::make_builtin_named_type("PtrType", s
);
3104 // The type descriptor for a pointer type.
3107 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3109 if (this->is_unsafe_pointer_type())
3111 gcc_assert(name
!= NULL
);
3112 return this->plain_type_descriptor(gogo
,
3113 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
3118 source_location bloc
= BUILTINS_LOCATION
;
3120 const Methods
* methods
;
3121 Type
* deref
= this->points_to();
3122 if (deref
->named_type() != NULL
)
3123 methods
= deref
->named_type()->methods();
3124 else if (deref
->struct_type() != NULL
)
3125 methods
= deref
->struct_type()->methods();
3129 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
3131 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
3133 Expression_list
* vals
= new Expression_list();
3136 Struct_field_list::const_iterator p
= fields
->begin();
3137 gcc_assert(p
->field_name() == "commonType");
3138 vals
->push_back(this->type_descriptor_constructor(gogo
,
3139 RUNTIME_TYPE_KIND_PTR
,
3140 name
, methods
, false));
3143 gcc_assert(p
->field_name() == "elem");
3144 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
3146 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
3150 // Reflection string.
3153 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3155 ret
->push_back('*');
3156 this->append_reflection(this->to_type_
, gogo
, ret
);
3162 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3164 ret
->push_back('p');
3165 this->append_mangled_name(this->to_type_
, gogo
, ret
);
3171 Pointer_type::do_export(Export
* exp
) const
3173 exp
->write_c_string("*");
3174 if (this->is_unsafe_pointer_type())
3175 exp
->write_c_string("any");
3177 exp
->write_type(this->to_type_
);
3183 Pointer_type::do_import(Import
* imp
)
3185 imp
->require_c_string("*");
3186 if (imp
->match_c_string("any"))
3189 return Type::make_pointer_type(Type::make_void_type());
3191 Type
* to
= imp
->read_type();
3192 return Type::make_pointer_type(to
);
3195 // Make a pointer type.
3198 Type::make_pointer_type(Type
* to_type
)
3200 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
3201 static Hashtable pointer_types
;
3202 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
3203 if (p
!= pointer_types
.end())
3205 Pointer_type
* ret
= new Pointer_type(to_type
);
3206 pointer_types
[to_type
] = ret
;
3210 // The nil type. We use a special type for nil because it is not the
3211 // same as any other type. In C term nil has type void*, but there is
3212 // no such type in Go.
3214 class Nil_type
: public Type
3224 { return ptr_type_node
; }
3227 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3228 { return is_clear
? NULL
: fold_convert(type_tree
, null_pointer_node
); }
3231 do_type_descriptor(Gogo
*, Named_type
*)
3232 { gcc_unreachable(); }
3235 do_reflection(Gogo
*, std::string
*) const
3236 { gcc_unreachable(); }
3239 do_mangled_name(Gogo
*, std::string
* ret
) const
3240 { ret
->push_back('n'); }
3243 // Make the nil type.
3246 Type::make_nil_type()
3248 static Nil_type singleton_nil_type
;
3249 return &singleton_nil_type
;
3252 // The type of a function call which returns multiple values. This is
3253 // really a struct, but we don't want to confuse a function call which
3254 // returns a struct with a function call which returns multiple
3257 class Call_multiple_result_type
: public Type
3260 Call_multiple_result_type(Call_expression
* call
)
3261 : Type(TYPE_CALL_MULTIPLE_RESULT
),
3267 do_has_pointer() const
3269 gcc_assert(saw_errors());
3277 do_get_init_tree(Gogo
*, tree
, bool)
3279 gcc_assert(saw_errors());
3280 return error_mark_node
;
3284 do_type_descriptor(Gogo
*, Named_type
*)
3286 gcc_assert(saw_errors());
3287 return Expression::make_error(UNKNOWN_LOCATION
);
3291 do_reflection(Gogo
*, std::string
*) const
3292 { gcc_assert(saw_errors()); }
3295 do_mangled_name(Gogo
*, std::string
*) const
3296 { gcc_assert(saw_errors()); }
3299 // The expression being called.
3300 Call_expression
* call_
;
3303 // Return the tree for a call result.
3306 Call_multiple_result_type::do_get_tree(Gogo
* gogo
)
3308 Function_type
* fntype
= this->call_
->get_function_type();
3309 gcc_assert(fntype
!= NULL
);
3310 const Typed_identifier_list
* results
= fntype
->results();
3311 gcc_assert(results
!= NULL
&& results
->size() > 1);
3313 Struct_field_list
* sfl
= new Struct_field_list
;
3314 for (Typed_identifier_list::const_iterator p
= results
->begin();
3315 p
!= results
->end();
3318 const std::string name
= ((p
->name().empty()
3319 || p
->name() == Import::import_marker
)
3322 sfl
->push_back(Struct_field(Typed_identifier(name
, p
->type(),
3323 this->call_
->location())));
3325 return Type::make_struct_type(sfl
, this->call_
->location())->get_tree(gogo
);
3328 // Make a call result type.
3331 Type::make_call_multiple_result_type(Call_expression
* call
)
3333 return new Call_multiple_result_type(call
);
3336 // Class Struct_field.
3338 // Get the name of a field.
3341 Struct_field::field_name() const
3343 const std::string
& name(this->typed_identifier_
.name());
3348 // This is called during parsing, before anything is lowered, so
3349 // we have to be pretty careful to avoid dereferencing an
3350 // unknown type name.
3351 Type
* t
= this->typed_identifier_
.type();
3353 if (t
->classification() == Type::TYPE_POINTER
)
3356 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
3357 dt
= ptype
->points_to();
3359 if (dt
->forward_declaration_type() != NULL
)
3360 return dt
->forward_declaration_type()->name();
3361 else if (dt
->named_type() != NULL
)
3362 return dt
->named_type()->name();
3363 else if (t
->is_error_type() || dt
->is_error_type())
3365 static const std::string error_string
= "*error*";
3366 return error_string
;
3370 // Avoid crashing in the erroneous case where T is named but
3372 gcc_assert(t
!= dt
);
3373 if (t
->forward_declaration_type() != NULL
)
3374 return t
->forward_declaration_type()->name();
3375 else if (t
->named_type() != NULL
)
3376 return t
->named_type()->name();
3383 // Class Struct_type.
3388 Struct_type::do_traverse(Traverse
* traverse
)
3390 Struct_field_list
* fields
= this->fields_
;
3393 for (Struct_field_list::iterator p
= fields
->begin();
3397 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
3398 return TRAVERSE_EXIT
;
3401 return TRAVERSE_CONTINUE
;
3404 // Verify that the struct type is complete and valid.
3407 Struct_type::do_verify()
3409 Struct_field_list
* fields
= this->fields_
;
3413 for (Struct_field_list::iterator p
= fields
->begin();
3417 Type
* t
= p
->type();
3418 if (t
->is_undefined())
3420 error_at(p
->location(), "struct field type is incomplete");
3421 p
->set_type(Type::make_error_type());
3424 else if (p
->is_anonymous())
3426 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
3428 error_at(p
->location(), "embedded type may not be a pointer");
3429 p
->set_type(Type::make_error_type());
3437 // Whether this contains a pointer.
3440 Struct_type::do_has_pointer() const
3442 const Struct_field_list
* fields
= this->fields();
3445 for (Struct_field_list::const_iterator p
= fields
->begin();
3449 if (p
->type()->has_pointer())
3455 // Whether this type is identical to T.
3458 Struct_type::is_identical(const Struct_type
* t
,
3459 bool errors_are_identical
) const
3461 const Struct_field_list
* fields1
= this->fields();
3462 const Struct_field_list
* fields2
= t
->fields();
3463 if (fields1
== NULL
|| fields2
== NULL
)
3464 return fields1
== fields2
;
3465 Struct_field_list::const_iterator pf2
= fields2
->begin();
3466 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
3467 pf1
!= fields1
->end();
3470 if (pf2
== fields2
->end())
3472 if (pf1
->field_name() != pf2
->field_name())
3474 if (pf1
->is_anonymous() != pf2
->is_anonymous()
3475 || !Type::are_identical(pf1
->type(), pf2
->type(),
3476 errors_are_identical
, NULL
))
3478 if (!pf1
->has_tag())
3485 if (!pf2
->has_tag())
3487 if (pf1
->tag() != pf2
->tag())
3491 if (pf2
!= fields2
->end())
3496 // Whether this struct type has any hidden fields.
3499 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
3500 std::string
* reason
) const
3502 const Struct_field_list
* fields
= this->fields();
3505 const Package
* within_package
= (within
== NULL
3507 : within
->named_object()->package());
3508 for (Struct_field_list::const_iterator pf
= fields
->begin();
3509 pf
!= fields
->end();
3512 if (within_package
!= NULL
3513 && !pf
->is_anonymous()
3514 && Gogo::is_hidden_name(pf
->field_name()))
3518 std::string within_name
= within
->named_object()->message_name();
3519 std::string name
= Gogo::message_name(pf
->field_name());
3520 size_t bufsize
= 200 + within_name
.length() + name
.length();
3521 char* buf
= new char[bufsize
];
3522 snprintf(buf
, bufsize
,
3523 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3524 open_quote
, within_name
.c_str(), close_quote
,
3525 open_quote
, name
.c_str(), close_quote
);
3526 reason
->assign(buf
);
3532 if (pf
->type()->has_hidden_fields(within
, reason
))
3542 Struct_type::do_hash_for_method(Gogo
* gogo
) const
3544 unsigned int ret
= 0;
3545 if (this->fields() != NULL
)
3547 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
3548 pf
!= this->fields()->end();
3550 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
3555 // Find the local field NAME.
3558 Struct_type::find_local_field(const std::string
& name
,
3559 unsigned int *pindex
) const
3561 const Struct_field_list
* fields
= this->fields_
;
3565 for (Struct_field_list::const_iterator pf
= fields
->begin();
3566 pf
!= fields
->end();
3569 if (pf
->field_name() == name
)
3579 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3581 Field_reference_expression
*
3582 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
3583 source_location location
) const
3586 return this->field_reference_depth(struct_expr
, name
, location
, NULL
,
3590 // Return an expression for a field, along with the depth at which it
3593 Field_reference_expression
*
3594 Struct_type::field_reference_depth(Expression
* struct_expr
,
3595 const std::string
& name
,
3596 source_location location
,
3597 Saw_named_type
* saw
,
3598 unsigned int* depth
) const
3600 const Struct_field_list
* fields
= this->fields_
;
3604 // Look for a field with this name.
3606 for (Struct_field_list::const_iterator pf
= fields
->begin();
3607 pf
!= fields
->end();
3610 if (pf
->field_name() == name
)
3613 return Expression::make_field_reference(struct_expr
, i
, location
);
3617 // Look for an anonymous field which contains a field with this
3619 unsigned int found_depth
= 0;
3620 Field_reference_expression
* ret
= NULL
;
3622 for (Struct_field_list::const_iterator pf
= fields
->begin();
3623 pf
!= fields
->end();
3626 if (!pf
->is_anonymous())
3629 Struct_type
* st
= pf
->type()->deref()->struct_type();
3633 Saw_named_type
* hold_saw
= saw
;
3634 Saw_named_type saw_here
;
3635 Named_type
* nt
= pf
->type()->named_type();
3637 nt
= pf
->type()->deref()->named_type();
3641 for (q
= saw
; q
!= NULL
; q
= q
->next
)
3645 // If this is an error, it will be reported
3652 saw_here
.next
= saw
;
3657 // Look for a reference using a NULL struct expression. If we
3658 // find one, fill in the struct expression with a reference to
3660 unsigned int subdepth
;
3661 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
3671 if (ret
== NULL
|| subdepth
< found_depth
)
3676 found_depth
= subdepth
;
3677 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
3679 if (pf
->type()->points_to() != NULL
)
3680 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
3681 while (sub
->expr() != NULL
)
3683 sub
= sub
->expr()->deref()->field_reference_expression();
3684 gcc_assert(sub
!= NULL
);
3686 sub
->set_struct_expression(here
);
3688 else if (subdepth
> found_depth
)
3692 // We do not handle ambiguity here--it should be handled by
3693 // Type::bind_field_or_method.
3701 *depth
= found_depth
+ 1;
3706 // Return the total number of fields, including embedded fields.
3709 Struct_type::total_field_count() const
3711 if (this->fields_
== NULL
)
3713 unsigned int ret
= 0;
3714 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3715 pf
!= this->fields_
->end();
3718 if (!pf
->is_anonymous() || pf
->type()->deref()->struct_type() == NULL
)
3721 ret
+= pf
->type()->struct_type()->total_field_count();
3726 // Return whether NAME is an unexported field, for better error reporting.
3729 Struct_type::is_unexported_local_field(Gogo
* gogo
,
3730 const std::string
& name
) const
3732 const Struct_field_list
* fields
= this->fields_
;
3735 for (Struct_field_list::const_iterator pf
= fields
->begin();
3736 pf
!= fields
->end();
3739 const std::string
& field_name(pf
->field_name());
3740 if (Gogo::is_hidden_name(field_name
)
3741 && name
== Gogo::unpack_hidden_name(field_name
)
3742 && gogo
->pack_hidden_name(name
, false) != field_name
)
3749 // Finalize the methods of an unnamed struct.
3752 Struct_type::finalize_methods(Gogo
* gogo
)
3754 if (this->all_methods_
!= NULL
)
3756 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
3759 // Return the method NAME, or NULL if there isn't one or if it is
3760 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3764 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
3766 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
3769 // Get the tree for a struct type.
3772 Struct_type::do_get_tree(Gogo
* gogo
)
3774 tree type
= make_node(RECORD_TYPE
);
3775 return this->fill_in_tree(gogo
, type
);
3778 // Fill in the fields for a struct type.
3781 Struct_type::fill_in_tree(Gogo
* gogo
, tree type
)
3783 tree field_trees
= NULL_TREE
;
3784 tree
* pp
= &field_trees
;
3785 bool has_pointer
= false;
3786 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3787 p
!= this->fields_
->end();
3790 std::string name
= Gogo::unpack_hidden_name(p
->field_name());
3791 tree name_tree
= get_identifier_with_length(name
.data(), name
.length());
3793 // Don't follow pointers yet, so that we don't get confused by a
3794 // pointer to an array of this struct type.
3795 tree field_type_tree
;
3796 if (p
->type()->points_to() != NULL
|| p
->type()->function_type() != NULL
)
3798 field_type_tree
= ptr_type_node
;
3803 field_type_tree
= p
->type()->get_tree(gogo
);
3804 if (field_type_tree
== error_mark_node
)
3805 return error_mark_node
;
3808 tree field
= build_decl(p
->location(), FIELD_DECL
, name_tree
,
3810 DECL_CONTEXT(field
) = type
;
3812 pp
= &DECL_CHAIN(field
);
3815 TYPE_FIELDS(type
) = field_trees
;
3821 tree field
= field_trees
;
3822 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3823 p
!= this->fields_
->end();
3824 ++p
, field
= DECL_CHAIN(field
))
3826 if (p
->type()->points_to() != NULL
3827 || p
->type()->function_type() != NULL
)
3828 TREE_TYPE(field
) = p
->type()->get_tree(gogo
);
3835 // Make sure that all structs which must be converted to the backend
3836 // representation before this one are in fact converted.
3839 Struct_type::convert_prerequisites(Gogo
* gogo
)
3841 for (std::vector
<Named_type
*>::const_iterator p
3842 = this->prerequisites_
.begin();
3843 p
!= this->prerequisites_
.end();
3845 (*p
)->get_tree(gogo
);
3848 // Initialize struct fields.
3851 Struct_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
3853 if (this->fields_
== NULL
|| this->fields_
->empty())
3859 tree ret
= build_constructor(type_tree
,
3860 VEC_alloc(constructor_elt
, gc
, 0));
3861 TREE_CONSTANT(ret
) = 1;
3866 bool is_constant
= true;
3867 bool any_fields_set
= false;
3868 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
,
3869 this->fields_
->size());
3871 tree field
= TYPE_FIELDS(type_tree
);
3872 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3873 p
!= this->fields_
->end();
3874 ++p
, field
= DECL_CHAIN(field
))
3876 tree value
= p
->type()->get_init_tree(gogo
, is_clear
);
3877 if (value
== error_mark_node
)
3878 return error_mark_node
;
3879 gcc_assert(field
!= NULL_TREE
);
3882 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
3885 any_fields_set
= true;
3886 if (!TREE_CONSTANT(value
))
3887 is_constant
= false;
3890 gcc_assert(field
== NULL_TREE
);
3892 if (!any_fields_set
)
3894 gcc_assert(is_clear
);
3895 VEC_free(constructor_elt
, gc
, init
);
3899 tree ret
= build_constructor(type_tree
, init
);
3901 TREE_CONSTANT(ret
) = 1;
3905 // The type of a struct type descriptor.
3908 Struct_type::make_struct_type_descriptor_type()
3913 Type
* tdt
= Type::make_type_descriptor_type();
3914 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3916 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3917 Type
* string_type
= Type::lookup_string_type();
3918 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
3921 Type::make_builtin_struct_type(5,
3922 "name", pointer_string_type
,
3923 "pkgPath", pointer_string_type
,
3925 "tag", pointer_string_type
,
3926 "offset", uintptr_type
);
3927 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
3929 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
3931 Struct_type
* s
= Type::make_builtin_struct_type(2,
3933 "fields", slice_type
);
3935 ret
= Type::make_builtin_named_type("StructType", s
);
3941 // Build a type descriptor for a struct type.
3944 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3946 source_location bloc
= BUILTINS_LOCATION
;
3948 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
3950 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
3952 Expression_list
* vals
= new Expression_list();
3955 const Methods
* methods
= this->methods();
3956 // A named struct should not have methods--the methods should attach
3957 // to the named type.
3958 gcc_assert(methods
== NULL
|| name
== NULL
);
3960 Struct_field_list::const_iterator ps
= fields
->begin();
3961 gcc_assert(ps
->field_name() == "commonType");
3962 vals
->push_back(this->type_descriptor_constructor(gogo
,
3963 RUNTIME_TYPE_KIND_STRUCT
,
3964 name
, methods
, true));
3967 gcc_assert(ps
->field_name() == "fields");
3969 Expression_list
* elements
= new Expression_list();
3970 elements
->reserve(this->fields_
->size());
3971 Type
* element_type
= ps
->type()->array_type()->element_type();
3972 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3973 pf
!= this->fields_
->end();
3976 const Struct_field_list
* f
= element_type
->struct_type()->fields();
3978 Expression_list
* fvals
= new Expression_list();
3981 Struct_field_list::const_iterator q
= f
->begin();
3982 gcc_assert(q
->field_name() == "name");
3983 if (pf
->is_anonymous())
3984 fvals
->push_back(Expression::make_nil(bloc
));
3987 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
3988 Expression
* s
= Expression::make_string(n
, bloc
);
3989 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3993 gcc_assert(q
->field_name() == "pkgPath");
3994 if (!Gogo::is_hidden_name(pf
->field_name()))
3995 fvals
->push_back(Expression::make_nil(bloc
));
3998 std::string n
= Gogo::hidden_name_prefix(pf
->field_name());
3999 Expression
* s
= Expression::make_string(n
, bloc
);
4000 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4004 gcc_assert(q
->field_name() == "typ");
4005 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
4008 gcc_assert(q
->field_name() == "tag");
4010 fvals
->push_back(Expression::make_nil(bloc
));
4013 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
4014 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
4018 gcc_assert(q
->field_name() == "offset");
4019 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
4021 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
4023 elements
->push_back(v
);
4026 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
4029 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4032 // Reflection string.
4035 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4037 ret
->append("struct { ");
4039 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
4040 p
!= this->fields_
->end();
4043 if (p
!= this->fields_
->begin())
4045 if (p
->is_anonymous())
4046 ret
->push_back('?');
4048 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
4049 ret
->push_back(' ');
4050 this->append_reflection(p
->type(), gogo
, ret
);
4054 const std::string
& tag(p
->tag());
4056 for (std::string::const_iterator p
= tag
.begin();
4061 ret
->append("\\x00");
4062 else if (*p
== '\n')
4064 else if (*p
== '\t')
4067 ret
->append("\\\"");
4068 else if (*p
== '\\')
4069 ret
->append("\\\\");
4073 ret
->push_back('"');
4083 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4085 ret
->push_back('S');
4087 const Struct_field_list
* fields
= this->fields_
;
4090 for (Struct_field_list::const_iterator p
= fields
->begin();
4094 if (p
->is_anonymous())
4098 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
4100 snprintf(buf
, sizeof buf
, "%u_",
4101 static_cast<unsigned int>(n
.length()));
4105 this->append_mangled_name(p
->type(), gogo
, ret
);
4108 const std::string
& tag(p
->tag());
4110 for (std::string::const_iterator p
= tag
.begin();
4114 if (ISALNUM(*p
) || *p
== '_')
4119 snprintf(buf
, sizeof buf
, ".%x.",
4120 static_cast<unsigned int>(*p
));
4125 snprintf(buf
, sizeof buf
, "T%u_",
4126 static_cast<unsigned int>(out
.length()));
4133 ret
->push_back('e');
4139 Struct_type::do_export(Export
* exp
) const
4141 exp
->write_c_string("struct { ");
4142 const Struct_field_list
* fields
= this->fields_
;
4143 gcc_assert(fields
!= NULL
);
4144 for (Struct_field_list::const_iterator p
= fields
->begin();
4148 if (p
->is_anonymous())
4149 exp
->write_string("? ");
4152 exp
->write_string(p
->field_name());
4153 exp
->write_c_string(" ");
4155 exp
->write_type(p
->type());
4159 exp
->write_c_string(" ");
4160 Expression
* expr
= Expression::make_string(p
->tag(),
4162 expr
->export_expression(exp
);
4166 exp
->write_c_string("; ");
4168 exp
->write_c_string("}");
4174 Struct_type::do_import(Import
* imp
)
4176 imp
->require_c_string("struct { ");
4177 Struct_field_list
* fields
= new Struct_field_list
;
4178 if (imp
->peek_char() != '}')
4183 if (imp
->match_c_string("? "))
4187 name
= imp
->read_identifier();
4188 imp
->require_c_string(" ");
4190 Type
* ftype
= imp
->read_type();
4192 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
4194 if (imp
->peek_char() == ' ')
4197 Expression
* expr
= Expression::import_expression(imp
);
4198 String_expression
* sexpr
= expr
->string_expression();
4199 gcc_assert(sexpr
!= NULL
);
4200 sf
.set_tag(sexpr
->val());
4204 imp
->require_c_string("; ");
4205 fields
->push_back(sf
);
4206 if (imp
->peek_char() == '}')
4210 imp
->require_c_string("}");
4212 return Type::make_struct_type(fields
, imp
->location());
4215 // Make a struct type.
4218 Type::make_struct_type(Struct_field_list
* fields
,
4219 source_location location
)
4221 return new Struct_type(fields
, location
);
4224 // Class Array_type.
4226 // Whether two array types are identical.
4229 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
4231 if (!Type::are_identical(this->element_type(), t
->element_type(),
4232 errors_are_identical
, NULL
))
4235 Expression
* l1
= this->length();
4236 Expression
* l2
= t
->length();
4238 // Slices of the same element type are identical.
4239 if (l1
== NULL
&& l2
== NULL
)
4242 // Arrays of the same element type are identical if they have the
4244 if (l1
!= NULL
&& l2
!= NULL
)
4249 // Try to determine the lengths. If we can't, assume the arrays
4250 // are not identical.
4258 if (l1
->integer_constant_value(true, v1
, &type1
)
4259 && l2
->integer_constant_value(true, v2
, &type2
))
4260 ret
= mpz_cmp(v1
, v2
) == 0;
4266 // Otherwise the arrays are not identical.
4273 Array_type::do_traverse(Traverse
* traverse
)
4275 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
4276 return TRAVERSE_EXIT
;
4277 if (this->length_
!= NULL
4278 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
4279 return TRAVERSE_EXIT
;
4280 return TRAVERSE_CONTINUE
;
4283 // Check that the length is valid.
4286 Array_type::verify_length()
4288 if (this->length_
== NULL
)
4291 Type_context
context(Type::lookup_integer_type("int"), false);
4292 this->length_
->determine_type(&context
);
4294 if (!this->length_
->is_constant())
4296 error_at(this->length_
->location(), "array bound is not constant");
4303 if (!this->length_
->integer_constant_value(true, val
, &vt
))
4307 if (!this->length_
->float_constant_value(fval
, &vt
))
4309 if (this->length_
->type()->integer_type() != NULL
4310 || this->length_
->type()->float_type() != NULL
)
4311 error_at(this->length_
->location(),
4312 "array bound is not constant");
4314 error_at(this->length_
->location(),
4315 "array bound is not numeric");
4320 if (!mpfr_integer_p(fval
))
4322 error_at(this->length_
->location(),
4323 "array bound truncated to integer");
4329 mpfr_get_z(val
, fval
, GMP_RNDN
);
4333 if (mpz_sgn(val
) < 0)
4335 error_at(this->length_
->location(), "negative array bound");
4340 Type
* int_type
= Type::lookup_integer_type("int");
4341 int tbits
= int_type
->integer_type()->bits();
4342 int vbits
= mpz_sizeinbase(val
, 2);
4343 if (vbits
+ 1 > tbits
)
4345 error_at(this->length_
->location(), "array bound overflows");
4358 Array_type::do_verify()
4360 if (!this->verify_length())
4362 this->length_
= Expression::make_error(this->length_
->location());
4368 // Array type hash code.
4371 Array_type::do_hash_for_method(Gogo
* gogo
) const
4373 // There is no very convenient way to get a hash code for the
4375 return this->element_type_
->hash_for_method(gogo
) + 1;
4378 // See if the expression passed to make is suitable. The first
4379 // argument is required, and gives the length. An optional second
4380 // argument is permitted for the capacity.
4383 Array_type::do_check_make_expression(Expression_list
* args
,
4384 source_location location
)
4386 gcc_assert(this->length_
== NULL
);
4387 if (args
== NULL
|| args
->empty())
4389 error_at(location
, "length required when allocating a slice");
4392 else if (args
->size() > 2)
4394 error_at(location
, "too many expressions passed to make");
4399 if (!Type::check_int_value(args
->front(),
4400 _("bad length when making slice"), location
))
4403 if (args
->size() > 1)
4405 if (!Type::check_int_value(args
->back(),
4406 _("bad capacity when making slice"),
4415 // Get a tree for the length of a fixed array. The length may be
4416 // computed using a function call, so we must only evaluate it once.
4419 Array_type::get_length_tree(Gogo
* gogo
)
4421 gcc_assert(this->length_
!= NULL
);
4422 if (this->length_tree_
== NULL_TREE
)
4427 if (this->length_
->integer_constant_value(true, val
, &t
))
4430 t
= Type::lookup_integer_type("int");
4431 else if (t
->is_abstract())
4432 t
= t
->make_non_abstract_type();
4433 tree tt
= t
->get_tree(gogo
);
4434 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
4441 // Make up a translation context for the array length
4442 // expression. FIXME: This won't work in general.
4443 Translate_context
context(gogo
, NULL
, NULL
, NULL_TREE
);
4444 tree len
= this->length_
->get_tree(&context
);
4445 if (len
!= error_mark_node
)
4447 len
= convert_to_integer(integer_type_node
, len
);
4448 len
= save_expr(len
);
4450 this->length_tree_
= len
;
4453 return this->length_tree_
;
4456 // Get a tree for the type of this array. A fixed array is simply
4457 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4458 // just like an array in C. An open array is a struct with three
4459 // fields: a data pointer, the length, and the capacity.
4462 Array_type::do_get_tree(Gogo
* gogo
)
4464 if (this->length_
== NULL
)
4466 tree struct_type
= gogo
->slice_type_tree(void_type_node
);
4467 return this->fill_in_slice_tree(gogo
, struct_type
);
4471 tree array_type
= make_node(ARRAY_TYPE
);
4472 return this->fill_in_array_tree(gogo
, array_type
);
4476 // Fill in the fields for an array type. This is used for named array
4480 Array_type::fill_in_array_tree(Gogo
* gogo
, tree array_type
)
4482 gcc_assert(this->length_
!= NULL
);
4484 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4485 tree length_tree
= this->get_length_tree(gogo
);
4486 if (element_type_tree
== error_mark_node
4487 || length_tree
== error_mark_node
)
4488 return error_mark_node
;
4490 length_tree
= fold_convert(sizetype
, length_tree
);
4492 // build_index_type takes the maximum index, which is one less than
4494 tree index_type
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
4498 TREE_TYPE(array_type
) = element_type_tree
;
4499 TYPE_DOMAIN(array_type
) = index_type
;
4500 TYPE_ADDR_SPACE(array_type
) = TYPE_ADDR_SPACE(element_type_tree
);
4501 layout_type(array_type
);
4503 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
)
4504 || TYPE_STRUCTURAL_EQUALITY_P(index_type
))
4505 SET_TYPE_STRUCTURAL_EQUALITY(array_type
);
4506 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
4507 || TYPE_CANONICAL(index_type
) != index_type
)
4508 TYPE_CANONICAL(array_type
) =
4509 build_array_type(TYPE_CANONICAL(element_type_tree
),
4510 TYPE_CANONICAL(index_type
));
4515 // Fill in the fields for a slice type. This is used for named slice
4519 Array_type::fill_in_slice_tree(Gogo
* gogo
, tree struct_type
)
4521 gcc_assert(this->length_
== NULL
);
4523 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4524 if (element_type_tree
== error_mark_node
)
4525 return error_mark_node
;
4526 tree field
= TYPE_FIELDS(struct_type
);
4527 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
4528 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field
))
4529 && TREE_TYPE(TREE_TYPE(field
)) == void_type_node
);
4530 TREE_TYPE(field
) = build_pointer_type(element_type_tree
);
4535 // Return an initializer for an array type.
4538 Array_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
4540 if (this->length_
== NULL
)
4547 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
4549 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
4551 for (tree field
= TYPE_FIELDS(type_tree
);
4553 field
= DECL_CHAIN(field
))
4555 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
,
4558 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
4561 tree ret
= build_constructor(type_tree
, init
);
4562 TREE_CONSTANT(ret
) = 1;
4569 tree value
= this->element_type_
->get_init_tree(gogo
, is_clear
);
4572 if (value
== error_mark_node
)
4573 return error_mark_node
;
4575 tree length_tree
= this->get_length_tree(gogo
);
4576 if (length_tree
== error_mark_node
)
4577 return error_mark_node
;
4579 length_tree
= fold_convert(sizetype
, length_tree
);
4580 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
,
4581 fold_build2(MINUS_EXPR
, sizetype
,
4582 length_tree
, size_one_node
));
4583 tree ret
= build_constructor_single(type_tree
, range
, value
);
4584 if (TREE_CONSTANT(value
))
4585 TREE_CONSTANT(ret
) = 1;
4590 // Handle the builtin make function for a slice.
4593 Array_type::do_make_expression_tree(Translate_context
* context
,
4594 Expression_list
* args
,
4595 source_location location
)
4597 gcc_assert(this->length_
== NULL
);
4599 Gogo
* gogo
= context
->gogo();
4600 tree type_tree
= this->get_tree(gogo
);
4601 if (type_tree
== error_mark_node
)
4602 return error_mark_node
;
4604 tree values_field
= TYPE_FIELDS(type_tree
);
4605 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field
)),
4608 tree count_field
= DECL_CHAIN(values_field
);
4609 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field
)),
4612 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4613 if (element_type_tree
== error_mark_node
)
4614 return error_mark_node
;
4615 tree element_size_tree
= TYPE_SIZE_UNIT(element_type_tree
);
4617 tree value
= this->element_type_
->get_init_tree(gogo
, true);
4618 if (value
== error_mark_node
)
4619 return error_mark_node
;
4621 // The first argument is the number of elements, the optional second
4622 // argument is the capacity.
4623 gcc_assert(args
!= NULL
&& args
->size() >= 1 && args
->size() <= 2);
4625 tree length_tree
= args
->front()->get_tree(context
);
4626 if (length_tree
== error_mark_node
)
4627 return error_mark_node
;
4628 if (!DECL_P(length_tree
))
4629 length_tree
= save_expr(length_tree
);
4630 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree
)))
4631 length_tree
= convert_to_integer(TREE_TYPE(count_field
), length_tree
);
4633 tree bad_index
= Expression::check_bounds(length_tree
,
4634 TREE_TYPE(count_field
),
4635 NULL_TREE
, location
);
4637 length_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
), length_tree
);
4639 if (args
->size() == 1)
4640 capacity_tree
= length_tree
;
4643 capacity_tree
= args
->back()->get_tree(context
);
4644 if (capacity_tree
== error_mark_node
)
4645 return error_mark_node
;
4646 if (!DECL_P(capacity_tree
))
4647 capacity_tree
= save_expr(capacity_tree
);
4648 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree
)))
4649 capacity_tree
= convert_to_integer(TREE_TYPE(count_field
),
4652 bad_index
= Expression::check_bounds(capacity_tree
,
4653 TREE_TYPE(count_field
),
4654 bad_index
, location
);
4656 tree chktype
= (((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4657 > TYPE_SIZE(TREE_TYPE(length_tree
)))
4658 || ((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4659 == TYPE_SIZE(TREE_TYPE(length_tree
)))
4660 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree
))))
4661 ? TREE_TYPE(capacity_tree
)
4662 : TREE_TYPE(length_tree
));
4663 tree chk
= fold_build2_loc(location
, LT_EXPR
, boolean_type_node
,
4664 fold_convert_loc(location
, chktype
,
4666 fold_convert_loc(location
, chktype
,
4668 if (bad_index
== NULL_TREE
)
4671 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4674 capacity_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
),
4678 tree size_tree
= fold_build2_loc(location
, MULT_EXPR
, sizetype
,
4680 fold_convert_loc(location
, sizetype
,
4683 tree chk
= fold_build2_loc(location
, TRUTH_AND_EXPR
, boolean_type_node
,
4684 fold_build2_loc(location
, GT_EXPR
,
4686 fold_convert_loc(location
,
4690 fold_build2_loc(location
, LT_EXPR
,
4692 size_tree
, element_size_tree
));
4693 if (bad_index
== NULL_TREE
)
4696 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4699 tree space
= context
->gogo()->allocate_memory(this->element_type_
,
4700 size_tree
, location
);
4702 if (value
!= NULL_TREE
)
4703 space
= save_expr(space
);
4705 space
= fold_convert(TREE_TYPE(values_field
), space
);
4707 if (bad_index
!= NULL_TREE
&& bad_index
!= boolean_false_node
)
4709 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
,
4711 space
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4712 build3(COND_EXPR
, void_type_node
,
4713 bad_index
, crash
, NULL_TREE
),
4717 tree constructor
= gogo
->slice_constructor(type_tree
, space
, length_tree
,
4720 if (value
== NULL_TREE
)
4722 // The array contents are zero initialized.
4726 // The elements must be initialized.
4728 tree max
= fold_build2_loc(location
, MINUS_EXPR
, TREE_TYPE(count_field
),
4730 fold_convert_loc(location
, TREE_TYPE(count_field
),
4733 tree array_type
= build_array_type(element_type_tree
,
4734 build_index_type(max
));
4736 tree value_pointer
= fold_convert_loc(location
,
4737 build_pointer_type(array_type
),
4740 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
, max
);
4741 tree space_init
= build_constructor_single(array_type
, range
, value
);
4743 return build2(COMPOUND_EXPR
, TREE_TYPE(constructor
),
4744 build2(MODIFY_EXPR
, void_type_node
,
4745 build_fold_indirect_ref(value_pointer
),
4750 // Return a tree for a pointer to the values in ARRAY.
4753 Array_type::value_pointer_tree(Gogo
*, tree array
) const
4756 if (this->length() != NULL
)
4759 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
4760 build_fold_addr_expr(array
));
4765 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
4766 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
4768 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
4771 if (TREE_CONSTANT(array
))
4772 TREE_CONSTANT(ret
) = 1;
4776 // Return a tree for the length of the array ARRAY which has this
4780 Array_type::length_tree(Gogo
* gogo
, tree array
)
4782 if (this->length_
!= NULL
)
4784 if (TREE_CODE(array
) == SAVE_EXPR
)
4785 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
4787 return omit_one_operand(integer_type_node
,
4788 this->get_length_tree(gogo
), array
);
4791 // This is an open array. We need to read the length field.
4793 tree type
= TREE_TYPE(array
);
4794 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4796 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
4797 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
4799 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4800 if (TREE_CONSTANT(array
))
4801 TREE_CONSTANT(ret
) = 1;
4805 // Return a tree for the capacity of the array ARRAY which has this
4809 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
4811 if (this->length_
!= NULL
)
4812 return omit_one_operand(sizetype
, this->get_length_tree(gogo
), array
);
4814 // This is an open array. We need to read the capacity field.
4816 tree type
= TREE_TYPE(array
);
4817 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4819 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
4820 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
4822 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4828 Array_type::do_export(Export
* exp
) const
4830 exp
->write_c_string("[");
4831 if (this->length_
!= NULL
)
4832 this->length_
->export_expression(exp
);
4833 exp
->write_c_string("] ");
4834 exp
->write_type(this->element_type_
);
4840 Array_type::do_import(Import
* imp
)
4842 imp
->require_c_string("[");
4844 if (imp
->peek_char() == ']')
4847 length
= Expression::import_expression(imp
);
4848 imp
->require_c_string("] ");
4849 Type
* element_type
= imp
->read_type();
4850 return Type::make_array_type(element_type
, length
);
4853 // The type of an array type descriptor.
4856 Array_type::make_array_type_descriptor_type()
4861 Type
* tdt
= Type::make_type_descriptor_type();
4862 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4864 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4867 Type::make_builtin_struct_type(3,
4870 "len", uintptr_type
);
4872 ret
= Type::make_builtin_named_type("ArrayType", sf
);
4878 // The type of an slice type descriptor.
4881 Array_type::make_slice_type_descriptor_type()
4886 Type
* tdt
= Type::make_type_descriptor_type();
4887 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4890 Type::make_builtin_struct_type(2,
4894 ret
= Type::make_builtin_named_type("SliceType", sf
);
4900 // Build a type descriptor for an array/slice type.
4903 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4905 if (this->length_
!= NULL
)
4906 return this->array_type_descriptor(gogo
, name
);
4908 return this->slice_type_descriptor(gogo
, name
);
4911 // Build a type descriptor for an array type.
4914 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4916 source_location bloc
= BUILTINS_LOCATION
;
4918 Type
* atdt
= Array_type::make_array_type_descriptor_type();
4920 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
4922 Expression_list
* vals
= new Expression_list();
4925 Struct_field_list::const_iterator p
= fields
->begin();
4926 gcc_assert(p
->field_name() == "commonType");
4927 vals
->push_back(this->type_descriptor_constructor(gogo
,
4928 RUNTIME_TYPE_KIND_ARRAY
,
4932 gcc_assert(p
->field_name() == "elem");
4933 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4936 gcc_assert(p
->field_name() == "len");
4937 vals
->push_back(Expression::make_cast(p
->type(), this->length_
, bloc
));
4940 gcc_assert(p
== fields
->end());
4942 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
4945 // Build a type descriptor for a slice type.
4948 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4950 source_location bloc
= BUILTINS_LOCATION
;
4952 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
4954 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4956 Expression_list
* vals
= new Expression_list();
4959 Struct_field_list::const_iterator p
= fields
->begin();
4960 gcc_assert(p
->field_name() == "commonType");
4961 vals
->push_back(this->type_descriptor_constructor(gogo
,
4962 RUNTIME_TYPE_KIND_SLICE
,
4966 gcc_assert(p
->field_name() == "elem");
4967 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4970 gcc_assert(p
== fields
->end());
4972 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4975 // Reflection string.
4978 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4980 ret
->push_back('[');
4981 if (this->length_
!= NULL
)
4986 if (!this->length_
->integer_constant_value(true, val
, &type
))
4987 error_at(this->length_
->location(),
4988 "array length must be integer constant expression");
4989 else if (mpz_cmp_si(val
, 0) < 0)
4990 error_at(this->length_
->location(), "array length is negative");
4991 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4992 error_at(this->length_
->location(), "array length is too large");
4996 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
5001 ret
->push_back(']');
5003 this->append_reflection(this->element_type_
, gogo
, ret
);
5009 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5011 ret
->push_back('A');
5012 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5013 if (this->length_
!= NULL
)
5018 if (!this->length_
->integer_constant_value(true, val
, &type
))
5019 error_at(this->length_
->location(),
5020 "array length must be integer constant expression");
5021 else if (mpz_cmp_si(val
, 0) < 0)
5022 error_at(this->length_
->location(), "array length is negative");
5023 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
5024 error_at(this->length_
->location(), "array size is too large");
5028 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
5033 ret
->push_back('e');
5036 // Make an array type.
5039 Type::make_array_type(Type
* element_type
, Expression
* length
)
5041 return new Array_type(element_type
, length
);
5049 Map_type::do_traverse(Traverse
* traverse
)
5051 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
5052 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
5053 return TRAVERSE_EXIT
;
5054 return TRAVERSE_CONTINUE
;
5057 // Check that the map type is OK.
5060 Map_type::do_verify()
5062 if (this->key_type_
->struct_type() != NULL
5063 || this->key_type_
->array_type() != NULL
)
5065 error_at(this->location_
, "invalid map key type");
5071 // Whether two map types are identical.
5074 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
5076 return (Type::are_identical(this->key_type(), t
->key_type(),
5077 errors_are_identical
, NULL
)
5078 && Type::are_identical(this->val_type(), t
->val_type(),
5079 errors_are_identical
, NULL
));
5085 Map_type::do_hash_for_method(Gogo
* gogo
) const
5087 return (this->key_type_
->hash_for_method(gogo
)
5088 + this->val_type_
->hash_for_method(gogo
)
5092 // Check that a call to the builtin make function is valid. For a map
5093 // the optional argument is the number of spaces to preallocate for
5097 Map_type::do_check_make_expression(Expression_list
* args
,
5098 source_location location
)
5100 if (args
!= NULL
&& !args
->empty())
5102 if (!Type::check_int_value(args
->front(), _("bad size when making map"),
5105 else if (args
->size() > 1)
5107 error_at(location
, "too many arguments when making map");
5114 // Get a tree for a map type. A map type is represented as a pointer
5115 // to a struct. The struct is __go_map in libgo/map.h.
5118 Map_type::do_get_tree(Gogo
* gogo
)
5120 static tree type_tree
;
5121 if (type_tree
== NULL_TREE
)
5123 tree struct_type
= make_node(RECORD_TYPE
);
5125 tree map_descriptor_type
= gogo
->map_descriptor_type();
5126 tree const_map_descriptor_type
=
5127 build_qualified_type(map_descriptor_type
, TYPE_QUAL_CONST
);
5128 tree name
= get_identifier("__descriptor");
5129 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5130 build_pointer_type(const_map_descriptor_type
));
5131 DECL_CONTEXT(field
) = struct_type
;
5132 TYPE_FIELDS(struct_type
) = field
;
5133 tree last_field
= field
;
5135 name
= get_identifier("__element_count");
5136 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5137 DECL_CONTEXT(field
) = struct_type
;
5138 DECL_CHAIN(last_field
) = field
;
5141 name
= get_identifier("__bucket_count");
5142 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5143 DECL_CONTEXT(field
) = struct_type
;
5144 DECL_CHAIN(last_field
) = field
;
5147 name
= get_identifier("__buckets");
5148 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5149 build_pointer_type(ptr_type_node
));
5150 DECL_CONTEXT(field
) = struct_type
;
5151 DECL_CHAIN(last_field
) = field
;
5153 layout_type(struct_type
);
5155 // Give the struct a name for better debugging info.
5156 name
= get_identifier("__go_map");
5157 tree type_decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
, name
,
5159 DECL_ARTIFICIAL(type_decl
) = 1;
5160 TYPE_NAME(struct_type
) = type_decl
;
5161 go_preserve_from_gc(type_decl
);
5162 rest_of_decl_compilation(type_decl
, 1, 0);
5164 type_tree
= build_pointer_type(struct_type
);
5165 go_preserve_from_gc(type_tree
);
5171 // Initialize a map.
5174 Map_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5178 return fold_convert(type_tree
, null_pointer_node
);
5181 // Return an expression for a newly allocated map.
5184 Map_type::do_make_expression_tree(Translate_context
* context
,
5185 Expression_list
* args
,
5186 source_location location
)
5188 tree bad_index
= NULL_TREE
;
5191 if (args
== NULL
|| args
->empty())
5192 expr_tree
= size_zero_node
;
5195 expr_tree
= args
->front()->get_tree(context
);
5196 if (expr_tree
== error_mark_node
)
5197 return error_mark_node
;
5198 if (!DECL_P(expr_tree
))
5199 expr_tree
= save_expr(expr_tree
);
5200 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5201 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5202 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5206 tree map_type
= this->get_tree(context
->gogo());
5208 static tree new_map_fndecl
;
5209 tree ret
= Gogo::call_builtin(&new_map_fndecl
,
5214 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type
))),
5215 context
->gogo()->map_descriptor(this),
5218 if (ret
== error_mark_node
)
5219 return error_mark_node
;
5220 // This can panic if the capacity is out of range.
5221 TREE_NOTHROW(new_map_fndecl
) = 0;
5223 if (bad_index
== NULL_TREE
)
5227 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
,
5229 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5230 build3(COND_EXPR
, void_type_node
,
5231 bad_index
, crash
, NULL_TREE
),
5236 // The type of a map type descriptor.
5239 Map_type::make_map_type_descriptor_type()
5244 Type
* tdt
= Type::make_type_descriptor_type();
5245 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5248 Type::make_builtin_struct_type(3,
5253 ret
= Type::make_builtin_named_type("MapType", sf
);
5259 // Build a type descriptor for a map type.
5262 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5264 source_location bloc
= BUILTINS_LOCATION
;
5266 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
5268 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
5270 Expression_list
* vals
= new Expression_list();
5273 Struct_field_list::const_iterator p
= fields
->begin();
5274 gcc_assert(p
->field_name() == "commonType");
5275 vals
->push_back(this->type_descriptor_constructor(gogo
,
5276 RUNTIME_TYPE_KIND_MAP
,
5280 gcc_assert(p
->field_name() == "key");
5281 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
5284 gcc_assert(p
->field_name() == "elem");
5285 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
5288 gcc_assert(p
== fields
->end());
5290 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
5293 // Reflection string for a map.
5296 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5298 ret
->append("map[");
5299 this->append_reflection(this->key_type_
, gogo
, ret
);
5301 this->append_reflection(this->val_type_
, gogo
, ret
);
5304 // Mangled name for a map.
5307 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5309 ret
->push_back('M');
5310 this->append_mangled_name(this->key_type_
, gogo
, ret
);
5312 this->append_mangled_name(this->val_type_
, gogo
, ret
);
5315 // Export a map type.
5318 Map_type::do_export(Export
* exp
) const
5320 exp
->write_c_string("map [");
5321 exp
->write_type(this->key_type_
);
5322 exp
->write_c_string("] ");
5323 exp
->write_type(this->val_type_
);
5326 // Import a map type.
5329 Map_type::do_import(Import
* imp
)
5331 imp
->require_c_string("map [");
5332 Type
* key_type
= imp
->read_type();
5333 imp
->require_c_string("] ");
5334 Type
* val_type
= imp
->read_type();
5335 return Type::make_map_type(key_type
, val_type
, imp
->location());
5341 Type::make_map_type(Type
* key_type
, Type
* val_type
, source_location location
)
5343 return new Map_type(key_type
, val_type
, location
);
5346 // Class Channel_type.
5351 Channel_type::do_hash_for_method(Gogo
* gogo
) const
5353 unsigned int ret
= 0;
5354 if (this->may_send_
)
5356 if (this->may_receive_
)
5358 if (this->element_type_
!= NULL
)
5359 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
5363 // Whether this type is the same as T.
5366 Channel_type::is_identical(const Channel_type
* t
,
5367 bool errors_are_identical
) const
5369 if (!Type::are_identical(this->element_type(), t
->element_type(),
5370 errors_are_identical
, NULL
))
5372 return (this->may_send_
== t
->may_send_
5373 && this->may_receive_
== t
->may_receive_
);
5376 // Check whether the parameters for a call to the builtin function
5377 // make are OK for a channel. A channel can take an optional single
5378 // parameter which is the buffer size.
5381 Channel_type::do_check_make_expression(Expression_list
* args
,
5382 source_location location
)
5384 if (args
!= NULL
&& !args
->empty())
5386 if (!Type::check_int_value(args
->front(),
5387 _("bad buffer size when making channel"),
5390 else if (args
->size() > 1)
5392 error_at(location
, "too many arguments when making channel");
5399 // Return the tree for a channel type. A channel is a pointer to a
5400 // __go_channel struct. The __go_channel struct is defined in
5401 // libgo/runtime/channel.h.
5404 Channel_type::do_get_tree(Gogo
*)
5406 static tree type_tree
;
5407 if (type_tree
== NULL_TREE
)
5409 tree ret
= make_node(RECORD_TYPE
);
5410 TYPE_NAME(ret
) = get_identifier("__go_channel");
5411 TYPE_STUB_DECL(ret
) = build_decl(BUILTINS_LOCATION
, TYPE_DECL
, NULL_TREE
,
5413 type_tree
= build_pointer_type(ret
);
5414 go_preserve_from_gc(type_tree
);
5419 // Initialize a channel variable.
5422 Channel_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5426 return fold_convert(type_tree
, null_pointer_node
);
5429 // Handle the builtin function make for a channel.
5432 Channel_type::do_make_expression_tree(Translate_context
* context
,
5433 Expression_list
* args
,
5434 source_location location
)
5436 Gogo
* gogo
= context
->gogo();
5437 tree channel_type
= this->get_tree(gogo
);
5439 tree element_tree
= this->element_type_
->get_tree(gogo
);
5440 tree element_size_tree
= size_in_bytes(element_tree
);
5442 tree bad_index
= NULL_TREE
;
5445 if (args
== NULL
|| args
->empty())
5446 expr_tree
= size_zero_node
;
5449 expr_tree
= args
->front()->get_tree(context
);
5450 if (expr_tree
== error_mark_node
)
5451 return error_mark_node
;
5452 if (!DECL_P(expr_tree
))
5453 expr_tree
= save_expr(expr_tree
);
5454 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5455 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5456 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5460 static tree new_channel_fndecl
;
5461 tree ret
= Gogo::call_builtin(&new_channel_fndecl
,
5470 if (ret
== error_mark_node
)
5471 return error_mark_node
;
5472 // This can panic if the capacity is out of range.
5473 TREE_NOTHROW(new_channel_fndecl
) = 0;
5475 if (bad_index
== NULL_TREE
)
5479 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
,
5481 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5482 build3(COND_EXPR
, void_type_node
,
5483 bad_index
, crash
, NULL_TREE
),
5488 // Build a type descriptor for a channel type.
5491 Channel_type::make_chan_type_descriptor_type()
5496 Type
* tdt
= Type::make_type_descriptor_type();
5497 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5499 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5502 Type::make_builtin_struct_type(3,
5505 "dir", uintptr_type
);
5507 ret
= Type::make_builtin_named_type("ChanType", sf
);
5513 // Build a type descriptor for a map type.
5516 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5518 source_location bloc
= BUILTINS_LOCATION
;
5520 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
5522 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
5524 Expression_list
* vals
= new Expression_list();
5527 Struct_field_list::const_iterator p
= fields
->begin();
5528 gcc_assert(p
->field_name() == "commonType");
5529 vals
->push_back(this->type_descriptor_constructor(gogo
,
5530 RUNTIME_TYPE_KIND_CHAN
,
5534 gcc_assert(p
->field_name() == "elem");
5535 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5538 gcc_assert(p
->field_name() == "dir");
5539 // These bits must match the ones in libgo/runtime/go-type.h.
5541 if (this->may_receive_
)
5543 if (this->may_send_
)
5546 mpz_init_set_ui(iv
, val
);
5547 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
5551 gcc_assert(p
== fields
->end());
5553 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
5556 // Reflection string.
5559 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5561 if (!this->may_send_
)
5563 ret
->append("chan");
5564 if (!this->may_receive_
)
5566 ret
->push_back(' ');
5567 this->append_reflection(this->element_type_
, gogo
, ret
);
5573 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5575 ret
->push_back('C');
5576 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5577 if (this->may_send_
)
5578 ret
->push_back('s');
5579 if (this->may_receive_
)
5580 ret
->push_back('r');
5581 ret
->push_back('e');
5587 Channel_type::do_export(Export
* exp
) const
5589 exp
->write_c_string("chan ");
5590 if (this->may_send_
&& !this->may_receive_
)
5591 exp
->write_c_string("-< ");
5592 else if (this->may_receive_
&& !this->may_send_
)
5593 exp
->write_c_string("<- ");
5594 exp
->write_type(this->element_type_
);
5600 Channel_type::do_import(Import
* imp
)
5602 imp
->require_c_string("chan ");
5606 if (imp
->match_c_string("-< "))
5610 may_receive
= false;
5612 else if (imp
->match_c_string("<- "))
5624 Type
* element_type
= imp
->read_type();
5626 return Type::make_channel_type(may_send
, may_receive
, element_type
);
5629 // Make a new channel type.
5632 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
5634 return new Channel_type(send
, receive
, element_type
);
5637 // Class Interface_type.
5642 Interface_type::do_traverse(Traverse
* traverse
)
5644 if (this->methods_
== NULL
)
5645 return TRAVERSE_CONTINUE
;
5646 return this->methods_
->traverse(traverse
);
5649 // Finalize the methods. This handles interface inheritance.
5652 Interface_type::finalize_methods()
5654 if (this->methods_
== NULL
)
5656 std::vector
<Named_type
*> seen
;
5657 bool is_recursive
= false;
5660 while (from
< this->methods_
->size())
5662 const Typed_identifier
* p
= &this->methods_
->at(from
);
5663 if (!p
->name().empty())
5666 for (i
= 0; i
< to
; ++i
)
5668 if (this->methods_
->at(i
).name() == p
->name())
5670 error_at(p
->location(), "duplicate method %qs",
5671 Gogo::message_name(p
->name()).c_str());
5678 this->methods_
->set(to
, *p
);
5685 Interface_type
* it
= p
->type()->interface_type();
5688 error_at(p
->location(), "interface contains embedded non-interface");
5696 error_at(p
->location(), "invalid recursive interface");
5697 is_recursive
= true;
5703 Named_type
* nt
= p
->type()->named_type();
5706 std::vector
<Named_type
*>::const_iterator q
;
5707 for (q
= seen
.begin(); q
!= seen
.end(); ++q
)
5711 error_at(p
->location(), "inherited interface loop");
5715 if (q
!= seen
.end())
5723 const Typed_identifier_list
* methods
= it
->methods();
5724 if (methods
== NULL
)
5729 for (Typed_identifier_list::const_iterator q
= methods
->begin();
5730 q
!= methods
->end();
5733 if (q
->name().empty())
5735 if (q
->type()->forwarded() == p
->type()->forwarded())
5736 error_at(p
->location(), "interface inheritance loop");
5740 for (i
= from
+ 1; i
< this->methods_
->size(); ++i
)
5742 const Typed_identifier
* r
= &this->methods_
->at(i
);
5743 if (r
->name().empty()
5744 && r
->type()->forwarded() == q
->type()->forwarded())
5746 error_at(p
->location(),
5747 "inherited interface listed twice");
5751 if (i
== this->methods_
->size())
5752 this->methods_
->push_back(Typed_identifier(q
->name(),
5757 else if (this->find_method(q
->name()) == NULL
)
5758 this->methods_
->push_back(Typed_identifier(q
->name(), q
->type(),
5763 error_at(p
->location(), "inherited method %qs is ambiguous",
5764 Gogo::message_name(q
->name()).c_str());
5771 delete this->methods_
;
5772 this->methods_
= NULL
;
5776 this->methods_
->resize(to
);
5777 this->methods_
->sort_by_name();
5781 // Return the method NAME, or NULL.
5783 const Typed_identifier
*
5784 Interface_type::find_method(const std::string
& name
) const
5786 if (this->methods_
== NULL
)
5788 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5789 p
!= this->methods_
->end();
5791 if (p
->name() == name
)
5796 // Return the method index.
5799 Interface_type::method_index(const std::string
& name
) const
5801 gcc_assert(this->methods_
!= NULL
);
5803 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5804 p
!= this->methods_
->end();
5806 if (p
->name() == name
)
5811 // Return whether NAME is an unexported method, for better error
5815 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
5817 if (this->methods_
== NULL
)
5819 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5820 p
!= this->methods_
->end();
5823 const std::string
& method_name(p
->name());
5824 if (Gogo::is_hidden_name(method_name
)
5825 && name
== Gogo::unpack_hidden_name(method_name
)
5826 && gogo
->pack_hidden_name(name
, false) != method_name
)
5832 // Whether this type is identical with T.
5835 Interface_type::is_identical(const Interface_type
* t
,
5836 bool errors_are_identical
) const
5838 // We require the same methods with the same types. The methods
5839 // have already been sorted.
5840 if (this->methods() == NULL
|| t
->methods() == NULL
)
5841 return this->methods() == t
->methods();
5843 Typed_identifier_list::const_iterator p1
= this->methods()->begin();
5844 for (Typed_identifier_list::const_iterator p2
= t
->methods()->begin();
5845 p2
!= t
->methods()->end();
5848 if (p1
== this->methods()->end())
5850 if (p1
->name() != p2
->name()
5851 || !Type::are_identical(p1
->type(), p2
->type(),
5852 errors_are_identical
, NULL
))
5855 if (p1
!= this->methods()->end())
5860 // Whether we can assign the interface type T to this type. The types
5861 // are known to not be identical. An interface assignment is only
5862 // permitted if T is known to implement all methods in THIS.
5863 // Otherwise a type guard is required.
5866 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
5867 std::string
* reason
) const
5869 if (this->methods() == NULL
)
5871 for (Typed_identifier_list::const_iterator p
= this->methods()->begin();
5872 p
!= this->methods()->end();
5875 const Typed_identifier
* m
= t
->find_method(p
->name());
5881 snprintf(buf
, sizeof buf
,
5882 _("need explicit conversion; missing method %s%s%s"),
5883 open_quote
, Gogo::message_name(p
->name()).c_str(),
5885 reason
->assign(buf
);
5890 std::string subreason
;
5891 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
5895 std::string n
= Gogo::message_name(p
->name());
5896 size_t len
= 100 + n
.length() + subreason
.length();
5897 char* buf
= new char[len
];
5898 if (subreason
.empty())
5899 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5900 open_quote
, n
.c_str(), close_quote
);
5903 _("incompatible type for method %s%s%s (%s)"),
5904 open_quote
, n
.c_str(), close_quote
,
5906 reason
->assign(buf
);
5919 Interface_type::do_hash_for_method(Gogo
* gogo
) const
5921 unsigned int ret
= 0;
5922 if (this->methods_
!= NULL
)
5924 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5925 p
!= this->methods_
->end();
5928 ret
= Type::hash_string(p
->name(), ret
);
5929 ret
+= p
->type()->hash_for_method(gogo
);
5936 // Return true if T implements the interface. If it does not, and
5937 // REASON is not NULL, set *REASON to a useful error message.
5940 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
5942 if (this->methods_
== NULL
)
5945 bool is_pointer
= false;
5946 const Named_type
* nt
= t
->named_type();
5947 const Struct_type
* st
= t
->struct_type();
5948 // If we start with a named type, we don't dereference it to find
5952 const Type
* pt
= t
->points_to();
5955 // If T is a pointer to a named type, then we need to look at
5956 // the type to which it points.
5958 nt
= pt
->named_type();
5959 st
= pt
->struct_type();
5963 // If we have a named type, get the methods from it rather than from
5968 // Only named and struct types have methods.
5969 if (nt
== NULL
&& st
== NULL
)
5973 if (t
->points_to() != NULL
5974 && t
->points_to()->interface_type() != NULL
)
5975 reason
->assign(_("pointer to interface type has no methods"));
5977 reason
->assign(_("type has no methods"));
5982 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
5986 if (t
->points_to() != NULL
5987 && t
->points_to()->interface_type() != NULL
)
5988 reason
->assign(_("pointer to interface type has no methods"));
5990 reason
->assign(_("type has no methods"));
5995 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5996 p
!= this->methods_
->end();
5999 bool is_ambiguous
= false;
6000 Method
* m
= (nt
!= NULL
6001 ? nt
->method_function(p
->name(), &is_ambiguous
)
6002 : st
->method_function(p
->name(), &is_ambiguous
));
6007 std::string n
= Gogo::message_name(p
->name());
6008 size_t len
= n
.length() + 100;
6009 char* buf
= new char[len
];
6011 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
6012 open_quote
, n
.c_str(), close_quote
);
6014 snprintf(buf
, len
, _("missing method %s%s%s"),
6015 open_quote
, n
.c_str(), close_quote
);
6016 reason
->assign(buf
);
6022 Function_type
*p_fn_type
= p
->type()->function_type();
6023 Function_type
* m_fn_type
= m
->type()->function_type();
6024 gcc_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
6025 std::string subreason
;
6026 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
6030 std::string n
= Gogo::message_name(p
->name());
6031 size_t len
= 100 + n
.length() + subreason
.length();
6032 char* buf
= new char[len
];
6033 if (subreason
.empty())
6034 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
6035 open_quote
, n
.c_str(), close_quote
);
6038 _("incompatible type for method %s%s%s (%s)"),
6039 open_quote
, n
.c_str(), close_quote
,
6041 reason
->assign(buf
);
6047 if (!is_pointer
&& !m
->is_value_method())
6051 std::string n
= Gogo::message_name(p
->name());
6052 size_t len
= 100 + n
.length();
6053 char* buf
= new char[len
];
6054 snprintf(buf
, len
, _("method %s%s%s requires a pointer"),
6055 open_quote
, n
.c_str(), close_quote
);
6056 reason
->assign(buf
);
6066 // Return a tree for an interface type. An interface is a pointer to
6067 // a struct. The struct has three fields. The first field is a
6068 // pointer to the type descriptor for the dynamic type of the object.
6069 // The second field is a pointer to a table of methods for the
6070 // interface to be used with the object. The third field is the value
6071 // of the object itself.
6074 Interface_type::do_get_tree(Gogo
* gogo
)
6076 if (this->methods_
== NULL
)
6078 // At the tree level, use the same type for all empty
6079 // interfaces. This lets us assign them to each other directly
6080 // without triggering GIMPLE type errors.
6081 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
6082 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
6083 static tree empty_interface
;
6084 return Gogo::builtin_struct(&empty_interface
, "__go_empty_interface",
6086 "__type_descriptor",
6092 return this->fill_in_tree(gogo
, make_node(RECORD_TYPE
));
6095 // Fill in the tree for an interface type. This is used for named
6099 Interface_type::fill_in_tree(Gogo
* gogo
, tree type
)
6101 gcc_assert(this->methods_
!= NULL
);
6103 // Because the methods may refer to the interface type itself, we
6104 // need to build the interface type first, and then update the
6105 // method pointer later.
6107 tree field_trees
= NULL_TREE
;
6108 tree
* pp
= &field_trees
;
6110 tree name_tree
= get_identifier("__methods");
6111 tree methods_field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
6113 DECL_CONTEXT(methods_field
) = type
;
6114 *pp
= methods_field
;
6115 pp
= &DECL_CHAIN(methods_field
);
6117 name_tree
= get_identifier("__object");
6118 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
6120 DECL_CONTEXT(field
) = type
;
6123 TYPE_FIELDS(type
) = field_trees
;
6127 // Build the type of the table of methods.
6129 tree method_table
= make_node(RECORD_TYPE
);
6131 // The first field is a pointer to the type descriptor.
6132 name_tree
= get_identifier("__type_descriptor");
6133 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
6134 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
6135 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, dtype
);
6136 DECL_CONTEXT(field
) = method_table
;
6137 TYPE_FIELDS(method_table
) = field
;
6139 std::string last_name
= "";
6140 pp
= &DECL_CHAIN(field
);
6141 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6142 p
!= this->methods_
->end();
6145 std::string name
= Gogo::unpack_hidden_name(p
->name());
6146 name_tree
= get_identifier_with_length(name
.data(), name
.length());
6147 tree field_type
= p
->type()->get_tree(gogo
);
6148 if (field_type
== error_mark_node
)
6149 return error_mark_node
;
6150 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, field_type
);
6151 DECL_CONTEXT(field
) = method_table
;
6153 pp
= &DECL_CHAIN(field
);
6154 // Sanity check: the names should be sorted.
6155 gcc_assert(p
->name() > last_name
);
6156 last_name
= p
->name();
6158 layout_type(method_table
);
6160 // Update the type of the __methods field from a generic pointer to
6161 // a pointer to the method table.
6162 TREE_TYPE(methods_field
) = build_pointer_type(method_table
);
6167 // Initialization value.
6170 Interface_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
6175 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
6176 for (tree field
= TYPE_FIELDS(type_tree
);
6178 field
= DECL_CHAIN(field
))
6180 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
6182 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
6185 tree ret
= build_constructor(type_tree
, init
);
6186 TREE_CONSTANT(ret
) = 1;
6190 // The type of an interface type descriptor.
6193 Interface_type::make_interface_type_descriptor_type()
6198 Type
* tdt
= Type::make_type_descriptor_type();
6199 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6201 Type
* string_type
= Type::lookup_string_type();
6202 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6205 Type::make_builtin_struct_type(3,
6206 "name", pointer_string_type
,
6207 "pkgPath", pointer_string_type
,
6210 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
6212 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
6214 Struct_type
* s
= Type::make_builtin_struct_type(2,
6216 "methods", slice_nsm
);
6218 ret
= Type::make_builtin_named_type("InterfaceType", s
);
6224 // Build a type descriptor for an interface type.
6227 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6229 source_location bloc
= BUILTINS_LOCATION
;
6231 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
6233 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
6235 Expression_list
* ivals
= new Expression_list();
6238 Struct_field_list::const_iterator pif
= ifields
->begin();
6239 gcc_assert(pif
->field_name() == "commonType");
6240 ivals
->push_back(this->type_descriptor_constructor(gogo
,
6241 RUNTIME_TYPE_KIND_INTERFACE
,
6245 gcc_assert(pif
->field_name() == "methods");
6247 Expression_list
* methods
= new Expression_list();
6248 if (this->methods_
!= NULL
&& !this->methods_
->empty())
6250 Type
* elemtype
= pif
->type()->array_type()->element_type();
6252 methods
->reserve(this->methods_
->size());
6253 for (Typed_identifier_list::const_iterator pm
= this->methods_
->begin();
6254 pm
!= this->methods_
->end();
6257 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
6259 Expression_list
* mvals
= new Expression_list();
6262 Struct_field_list::const_iterator pmf
= mfields
->begin();
6263 gcc_assert(pmf
->field_name() == "name");
6264 std::string s
= Gogo::unpack_hidden_name(pm
->name());
6265 Expression
* e
= Expression::make_string(s
, bloc
);
6266 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6269 gcc_assert(pmf
->field_name() == "pkgPath");
6270 if (!Gogo::is_hidden_name(pm
->name()))
6271 mvals
->push_back(Expression::make_nil(bloc
));
6274 s
= Gogo::hidden_name_prefix(pm
->name());
6275 e
= Expression::make_string(s
, bloc
);
6276 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6280 gcc_assert(pmf
->field_name() == "typ");
6281 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
6284 gcc_assert(pmf
== mfields
->end());
6286 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
6288 methods
->push_back(e
);
6292 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
6296 gcc_assert(pif
== ifields
->end());
6298 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
6301 // Reflection string.
6304 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6306 ret
->append("interface {");
6307 if (this->methods_
!= NULL
)
6309 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6310 p
!= this->methods_
->end();
6313 if (p
!= this->methods_
->begin())
6315 ret
->push_back(' ');
6316 ret
->append(Gogo::unpack_hidden_name(p
->name()));
6317 std::string sub
= p
->type()->reflection(gogo
);
6318 gcc_assert(sub
.compare(0, 4, "func") == 0);
6319 sub
= sub
.substr(4);
6329 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6331 ret
->push_back('I');
6333 const Typed_identifier_list
* methods
= this->methods_
;
6334 if (methods
!= NULL
)
6336 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6337 p
!= methods
->end();
6340 std::string n
= Gogo::unpack_hidden_name(p
->name());
6342 snprintf(buf
, sizeof buf
, "%u_",
6343 static_cast<unsigned int>(n
.length()));
6346 this->append_mangled_name(p
->type(), gogo
, ret
);
6350 ret
->push_back('e');
6356 Interface_type::do_export(Export
* exp
) const
6358 exp
->write_c_string("interface { ");
6360 const Typed_identifier_list
* methods
= this->methods_
;
6361 if (methods
!= NULL
)
6363 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
6364 pm
!= methods
->end();
6367 exp
->write_string(pm
->name());
6368 exp
->write_c_string(" (");
6370 const Function_type
* fntype
= pm
->type()->function_type();
6373 const Typed_identifier_list
* parameters
= fntype
->parameters();
6374 if (parameters
!= NULL
)
6376 bool is_varargs
= fntype
->is_varargs();
6377 for (Typed_identifier_list::const_iterator pp
=
6378 parameters
->begin();
6379 pp
!= parameters
->end();
6385 exp
->write_c_string(", ");
6386 if (!is_varargs
|| pp
+ 1 != parameters
->end())
6387 exp
->write_type(pp
->type());
6390 exp
->write_c_string("...");
6391 Type
*pptype
= pp
->type();
6392 exp
->write_type(pptype
->array_type()->element_type());
6397 exp
->write_c_string(")");
6399 const Typed_identifier_list
* results
= fntype
->results();
6400 if (results
!= NULL
)
6402 exp
->write_c_string(" ");
6403 if (results
->size() == 1)
6404 exp
->write_type(results
->begin()->type());
6408 exp
->write_c_string("(");
6409 for (Typed_identifier_list::const_iterator p
=
6411 p
!= results
->end();
6417 exp
->write_c_string(", ");
6418 exp
->write_type(p
->type());
6420 exp
->write_c_string(")");
6424 exp
->write_c_string("; ");
6428 exp
->write_c_string("}");
6431 // Import an interface type.
6434 Interface_type::do_import(Import
* imp
)
6436 imp
->require_c_string("interface { ");
6438 Typed_identifier_list
* methods
= new Typed_identifier_list
;
6439 while (imp
->peek_char() != '}')
6441 std::string name
= imp
->read_identifier();
6442 imp
->require_c_string(" (");
6444 Typed_identifier_list
* parameters
;
6445 bool is_varargs
= false;
6446 if (imp
->peek_char() == ')')
6450 parameters
= new Typed_identifier_list
;
6453 if (imp
->match_c_string("..."))
6459 Type
* ptype
= imp
->read_type();
6461 ptype
= Type::make_array_type(ptype
, NULL
);
6462 parameters
->push_back(Typed_identifier(Import::import_marker
,
6463 ptype
, imp
->location()));
6464 if (imp
->peek_char() != ',')
6466 gcc_assert(!is_varargs
);
6467 imp
->require_c_string(", ");
6470 imp
->require_c_string(")");
6472 Typed_identifier_list
* results
;
6473 if (imp
->peek_char() != ' ')
6477 results
= new Typed_identifier_list
;
6479 if (imp
->peek_char() != '(')
6481 Type
* rtype
= imp
->read_type();
6482 results
->push_back(Typed_identifier(Import::import_marker
,
6483 rtype
, imp
->location()));
6490 Type
* rtype
= imp
->read_type();
6491 results
->push_back(Typed_identifier(Import::import_marker
,
6492 rtype
, imp
->location()));
6493 if (imp
->peek_char() != ',')
6495 imp
->require_c_string(", ");
6497 imp
->require_c_string(")");
6501 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
6505 fntype
->set_is_varargs();
6506 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
6508 imp
->require_c_string("; ");
6511 imp
->require_c_string("}");
6513 if (methods
->empty())
6519 return Type::make_interface_type(methods
, imp
->location());
6522 // Make an interface type.
6525 Type::make_interface_type(Typed_identifier_list
* methods
,
6526 source_location location
)
6528 return new Interface_type(methods
, location
);
6533 // Bind a method to an object.
6536 Method::bind_method(Expression
* expr
, source_location location
) const
6538 if (this->stub_
== NULL
)
6540 // When there is no stub object, the binding is determined by
6542 return this->do_bind_method(expr
, location
);
6545 Expression
* func
= Expression::make_func_reference(this->stub_
, NULL
,
6547 return Expression::make_bound_method(expr
, func
, location
);
6550 // Return the named object associated with a method. This may only be
6551 // called after methods are finalized.
6554 Method::named_object() const
6556 if (this->stub_
!= NULL
)
6558 return this->do_named_object();
6561 // Class Named_method.
6563 // The type of the method.
6566 Named_method::do_type() const
6568 if (this->named_object_
->is_function())
6569 return this->named_object_
->func_value()->type();
6570 else if (this->named_object_
->is_function_declaration())
6571 return this->named_object_
->func_declaration_value()->type();
6576 // Return the location of the method receiver.
6579 Named_method::do_receiver_location() const
6581 return this->do_type()->receiver()->location();
6584 // Bind a method to an object.
6587 Named_method::do_bind_method(Expression
* expr
, source_location location
) const
6589 Expression
* func
= Expression::make_func_reference(this->named_object_
, NULL
,
6591 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, func
,
6593 // If this is not a local method, and it does not use a stub, then
6594 // the real method expects a different type. We need to cast the
6596 if (this->depth() > 0 && !this->needs_stub_method())
6598 Function_type
* ftype
= this->do_type();
6599 gcc_assert(ftype
->is_method());
6600 Type
* frtype
= ftype
->receiver()->type();
6601 bme
->set_first_argument_type(frtype
);
6606 // Class Interface_method.
6608 // Bind a method to an object.
6611 Interface_method::do_bind_method(Expression
* expr
,
6612 source_location location
) const
6614 return Expression::make_interface_field_reference(expr
, this->name_
,
6620 // Insert a new method. Return true if it was inserted, false
6624 Methods::insert(const std::string
& name
, Method
* m
)
6626 std::pair
<Method_map::iterator
, bool> ins
=
6627 this->methods_
.insert(std::make_pair(name
, m
));
6632 Method
* old_method
= ins
.first
->second
;
6633 if (m
->depth() < old_method
->depth())
6636 ins
.first
->second
= m
;
6641 if (m
->depth() == old_method
->depth())
6642 old_method
->set_is_ambiguous();
6648 // Return the number of unambiguous methods.
6651 Methods::count() const
6654 for (Method_map::const_iterator p
= this->methods_
.begin();
6655 p
!= this->methods_
.end();
6657 if (!p
->second
->is_ambiguous())
6662 // Class Named_type.
6664 // Return the name of the type.
6667 Named_type::name() const
6669 return this->named_object_
->name();
6672 // Return the name of the type to use in an error message.
6675 Named_type::message_name() const
6677 return this->named_object_
->message_name();
6680 // Return the base type for this type. We have to be careful about
6681 // circular type definitions, which are invalid but may be seen here.
6684 Named_type::named_base()
6686 if (this->seen_
> 0)
6689 Type
* ret
= this->type_
->base();
6695 Named_type::named_base() const
6697 if (this->seen_
> 0)
6700 const Type
* ret
= this->type_
->base();
6705 // Return whether this is an error type. We have to be careful about
6706 // circular type definitions, which are invalid but may be seen here.
6709 Named_type::is_named_error_type() const
6711 if (this->seen_
> 0)
6714 bool ret
= this->type_
->is_error_type();
6719 // Add a method to this type.
6722 Named_type::add_method(const std::string
& name
, Function
* function
)
6724 if (this->local_methods_
== NULL
)
6725 this->local_methods_
= new Bindings(NULL
);
6726 return this->local_methods_
->add_function(name
, NULL
, function
);
6729 // Add a method declaration to this type.
6732 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
6733 Function_type
* type
,
6734 source_location location
)
6736 if (this->local_methods_
== NULL
)
6737 this->local_methods_
= new Bindings(NULL
);
6738 return this->local_methods_
->add_function_declaration(name
, package
, type
,
6742 // Add an existing method to this type.
6745 Named_type::add_existing_method(Named_object
* no
)
6747 if (this->local_methods_
== NULL
)
6748 this->local_methods_
= new Bindings(NULL
);
6749 this->local_methods_
->add_named_object(no
);
6752 // Look for a local method NAME, and returns its named object, or NULL
6756 Named_type::find_local_method(const std::string
& name
) const
6758 if (this->local_methods_
== NULL
)
6760 return this->local_methods_
->lookup(name
);
6763 // Return whether NAME is an unexported field or method, for better
6767 Named_type::is_unexported_local_method(Gogo
* gogo
,
6768 const std::string
& name
) const
6770 Bindings
* methods
= this->local_methods_
;
6771 if (methods
!= NULL
)
6773 for (Bindings::const_declarations_iterator p
=
6774 methods
->begin_declarations();
6775 p
!= methods
->end_declarations();
6778 if (Gogo::is_hidden_name(p
->first
)
6779 && name
== Gogo::unpack_hidden_name(p
->first
)
6780 && gogo
->pack_hidden_name(name
, false) != p
->first
)
6787 // Build the complete list of methods for this type, which means
6788 // recursively including all methods for anonymous fields. Create all
6792 Named_type::finalize_methods(Gogo
* gogo
)
6794 if (this->all_methods_
!= NULL
)
6797 if (this->local_methods_
!= NULL
6798 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
6800 const Bindings
* lm
= this->local_methods_
;
6801 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
6802 p
!= lm
->end_declarations();
6804 error_at(p
->second
->location(),
6805 "invalid pointer or interface receiver type");
6806 delete this->local_methods_
;
6807 this->local_methods_
= NULL
;
6811 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6814 // Return the method NAME, or NULL if there isn't one or if it is
6815 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6819 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6821 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6824 // Return a pointer to the interface method table for this type for
6825 // the interface INTERFACE. IS_POINTER is true if this is for a
6829 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
6832 gcc_assert(!interface
->is_empty());
6834 Interface_method_tables
** pimt
= (is_pointer
6835 ? &this->interface_method_tables_
6836 : &this->pointer_interface_method_tables_
);
6839 *pimt
= new Interface_method_tables(5);
6841 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
6842 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
6846 // This is a new entry in the hash table.
6847 gcc_assert(ins
.first
->second
== NULL_TREE
);
6848 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
6853 tree decl
= ins
.first
->second
;
6854 if (decl
== error_mark_node
)
6855 return error_mark_node
;
6856 gcc_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
6857 return build_fold_addr_expr(decl
);
6860 // Return whether a named type has any hidden fields.
6863 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
6865 if (this->seen_
> 0)
6868 bool ret
= this->type_
->has_hidden_fields(this, reason
);
6873 // Look for a use of a complete type within another type. This is
6874 // used to check that we don't try to use a type within itself.
6876 class Find_type_use
: public Traverse
6879 Find_type_use(Type
* find_type
)
6880 : Traverse(traverse_types
),
6881 find_type_(find_type
), found_(false)
6884 // Whether we found the type.
6887 { return this->found_
; }
6894 // The type we are looking for.
6896 // Whether we found the type.
6900 // Check for FIND_TYPE in TYPE.
6903 Find_type_use::type(Type
* type
)
6905 if (this->find_type_
== type
)
6907 this->found_
= true;
6908 return TRAVERSE_EXIT
;
6910 // It's OK if we see a reference to the type in any type which is
6911 // essentially a pointer: a pointer, a slice, a function, a map, or
6913 if (type
->points_to() != NULL
6914 || type
->is_open_array_type()
6915 || type
->function_type() != NULL
6916 || type
->map_type() != NULL
6917 || type
->channel_type() != NULL
)
6918 return TRAVERSE_SKIP_COMPONENTS
;
6920 // For an interface, a reference to the type in a method type should
6921 // be ignored, but we have to consider direct inheritance. When
6922 // this is called, there may be cases of direct inheritance
6923 // represented as a method with no name.
6924 if (type
->interface_type() != NULL
)
6926 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
6927 if (methods
!= NULL
)
6929 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6930 p
!= methods
->end();
6933 if (p
->name().empty())
6935 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
6936 return TRAVERSE_EXIT
;
6940 return TRAVERSE_SKIP_COMPONENTS
;
6943 return TRAVERSE_CONTINUE
;
6946 // Verify that a named type does not refer to itself.
6949 Named_type::do_verify()
6951 Find_type_use
find(this);
6952 Type::traverse(this->type_
, &find
);
6955 error_at(this->location_
, "invalid recursive type %qs",
6956 this->message_name().c_str());
6957 this->is_error_
= true;
6961 // Check whether any of the local methods overloads an existing
6962 // struct field or interface method. We don't need to check the
6963 // list of methods against itself: that is handled by the Bindings
6965 if (this->local_methods_
!= NULL
)
6967 Struct_type
* st
= this->type_
->struct_type();
6968 Interface_type
* it
= this->type_
->interface_type();
6969 bool found_dup
= false;
6970 if (st
!= NULL
|| it
!= NULL
)
6972 for (Bindings::const_declarations_iterator p
=
6973 this->local_methods_
->begin_declarations();
6974 p
!= this->local_methods_
->end_declarations();
6977 const std::string
& name(p
->first
);
6978 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
6980 error_at(p
->second
->location(),
6981 "method %qs redeclares struct field name",
6982 Gogo::message_name(name
).c_str());
6985 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
6987 error_at(p
->second
->location(),
6988 "method %qs redeclares interface method name",
6989 Gogo::message_name(name
).c_str());
6998 // If this is a struct, then if any of the fields of the struct
6999 // themselves have struct type, or array of struct type, then this
7000 // struct must be converted to the backend representation before the
7001 // field's type is converted. That may seem backward, but it works
7002 // because if the field's type refers to this one, e.g., via a
7003 // pointer, then the conversion process will pick up the half-built
7004 // struct and do the right thing.
7005 if (this->struct_type() != NULL
)
7007 const Struct_field_list
* fields
= this->struct_type()->fields();
7008 for (Struct_field_list::const_iterator p
= fields
->begin();
7012 Struct_type
* st
= p
->type()->struct_type();
7014 st
->add_prerequisite(this);
7017 Array_type
* at
= p
->type()->array_type();
7018 if (at
!= NULL
&& !at
->is_open_array_type())
7020 st
= at
->element_type()->struct_type();
7022 st
->add_prerequisite(this);
7031 // Return whether this type is or contains a pointer.
7034 Named_type::do_has_pointer() const
7036 if (this->seen_
> 0)
7039 bool ret
= this->type_
->has_pointer();
7044 // Return a hash code. This is used for method lookup. We simply
7045 // hash on the name itself.
7048 Named_type::do_hash_for_method(Gogo
* gogo
) const
7050 const std::string
& name(this->named_object()->name());
7051 unsigned int ret
= Type::hash_string(name
, 0);
7053 // GOGO will be NULL here when called from Type_hash_identical.
7054 // That is OK because that is only used for internal hash tables
7055 // where we are going to be comparing named types for equality. In
7056 // other cases, which are cases where the runtime is going to
7057 // compare hash codes to see if the types are the same, we need to
7058 // include the package prefix and name in the hash.
7059 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
7061 const Package
* package
= this->named_object()->package();
7062 if (package
== NULL
)
7064 ret
= Type::hash_string(gogo
->unique_prefix(), ret
);
7065 ret
= Type::hash_string(gogo
->package_name(), ret
);
7069 ret
= Type::hash_string(package
->unique_prefix(), ret
);
7070 ret
= Type::hash_string(package
->name(), ret
);
7077 // Get a tree for a named type.
7080 Named_type::do_get_tree(Gogo
* gogo
)
7082 if (this->is_error_
)
7083 return error_mark_node
;
7085 // Go permits types to refer to themselves in various ways. Break
7086 // the recursion here.
7088 switch (this->type_
->forwarded()->classification())
7091 return error_mark_node
;
7100 // These types can not refer to themselves.
7103 // All maps and channels have the same type in GENERIC.
7104 t
= Type::get_named_type_tree(gogo
, this->type_
);
7105 if (t
== error_mark_node
)
7106 return error_mark_node
;
7107 // Build a copy to set TYPE_NAME.
7108 t
= build_variant_type_copy(t
);
7112 // GENERIC can't handle a pointer to a function type whose
7113 // return type is a pointer to the function type itself. It
7114 // goes into an infinite loop when walking the types.
7115 if (this->seen_
> 0)
7117 Function_type
* fntype
= this->type_
->function_type();
7118 if (fntype
->results() != NULL
7119 && fntype
->results()->size() == 1
7120 && fntype
->results()->front().type()->forwarded() == this)
7121 return ptr_type_node
;
7123 // We can legitimately see ourselves here twice when a named
7124 // type is defined using a struct which refers to the named
7125 // type. If we see ourselves too often we are in a loop.
7126 if (this->seen_
> 3)
7127 return ptr_type_node
;
7130 t
= Type::get_named_type_tree(gogo
, this->type_
);
7132 if (t
== error_mark_node
)
7133 return error_mark_node
;
7134 t
= build_variant_type_copy(t
);
7138 // Don't recur infinitely if a pointer type refers to itself.
7139 // Ideally we would build a circular data structure here, but
7140 // GENERIC can't handle them.
7141 if (this->seen_
> 0)
7143 if (this->type_
->points_to()->forwarded() == this)
7144 return ptr_type_node
;
7146 if (this->seen_
> 3)
7147 return ptr_type_node
;
7150 t
= Type::get_named_type_tree(gogo
, this->type_
);
7152 if (t
== error_mark_node
)
7153 return error_mark_node
;
7154 t
= build_variant_type_copy(t
);
7158 // If there are structs which must be converted first, do them.
7159 if (this->seen_
== 0)
7162 this->type_
->struct_type()->convert_prerequisites(gogo
);
7166 if (this->named_tree_
!= NULL_TREE
)
7167 return this->named_tree_
;
7169 t
= make_node(RECORD_TYPE
);
7170 this->named_tree_
= t
;
7171 t
= this->type_
->struct_type()->fill_in_tree(gogo
, t
);
7172 if (t
== error_mark_node
)
7174 this->named_tree_
= error_mark_node
;
7175 return error_mark_node
;
7180 if (this->named_tree_
!= NULL_TREE
)
7181 return this->named_tree_
;
7182 if (!this->is_open_array_type())
7184 t
= make_node(ARRAY_TYPE
);
7185 this->named_tree_
= t
;
7186 t
= this->type_
->array_type()->fill_in_array_tree(gogo
, t
);
7190 t
= gogo
->slice_type_tree(void_type_node
);
7191 this->named_tree_
= t
;
7192 t
= this->type_
->array_type()->fill_in_slice_tree(gogo
, t
);
7194 if (t
== error_mark_node
)
7195 return error_mark_node
;
7196 t
= build_variant_type_copy(t
);
7199 case TYPE_INTERFACE
:
7200 if (this->type_
->interface_type()->is_empty())
7202 t
= Type::get_named_type_tree(gogo
, this->type_
);
7203 if (t
== error_mark_node
)
7204 return error_mark_node
;
7205 t
= build_variant_type_copy(t
);
7209 if (this->named_tree_
!= NULL_TREE
)
7210 return this->named_tree_
;
7211 t
= make_node(RECORD_TYPE
);
7212 this->named_tree_
= t
;
7213 t
= this->type_
->interface_type()->fill_in_tree(gogo
, t
);
7214 if (t
== error_mark_node
)
7216 this->named_tree_
= error_mark_node
;
7217 return error_mark_node
;
7224 // When a named type T1 is defined as another named type T2,
7225 // the definition must simply be "type T1 T2". If the
7226 // definition of T2 may refer to T1, then we must simply
7227 // return the type for T2 here. It's not precisely correct,
7228 // but it's as close as we can get with GENERIC.
7230 t
= Type::get_named_type_tree(gogo
, this->type_
);
7232 if (this->seen_
> 0)
7234 if (t
== error_mark_node
)
7235 return error_mark_node
;
7236 t
= build_variant_type_copy(t
);
7241 // An undefined forwarding type. Make sure the error is
7243 this->type_
->forward_declaration_type()->real_type();
7244 return error_mark_node
;
7248 case TYPE_CALL_MULTIPLE_RESULT
:
7252 tree id
= this->named_object_
->get_id(gogo
);
7253 tree decl
= build_decl(this->location_
, TYPE_DECL
, id
, t
);
7254 TYPE_NAME(t
) = decl
;
7259 // Build a type descriptor for a named type.
7262 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7264 // If NAME is not NULL, then we don't really want the type
7265 // descriptor for this type; we want the descriptor for the
7266 // underlying type, giving it the name NAME.
7267 return this->named_type_descriptor(gogo
, this->type_
,
7268 name
== NULL
? this : name
);
7271 // Add to the reflection string. This is used mostly for the name of
7272 // the type used in a type descriptor, not for actual reflection
7276 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7278 if (this->location() != BUILTINS_LOCATION
)
7280 const Package
* package
= this->named_object_
->package();
7281 if (package
!= NULL
)
7282 ret
->append(package
->name());
7284 ret
->append(gogo
->package_name());
7285 ret
->push_back('.');
7287 if (this->in_function_
!= NULL
)
7289 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7290 ret
->push_back('$');
7292 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
7295 // Get the mangled name.
7298 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7300 Named_object
* no
= this->named_object_
;
7302 if (this->location() == BUILTINS_LOCATION
)
7303 gcc_assert(this->in_function_
== NULL
);
7306 const std::string
& unique_prefix(no
->package() == NULL
7307 ? gogo
->unique_prefix()
7308 : no
->package()->unique_prefix());
7309 const std::string
& package_name(no
->package() == NULL
7310 ? gogo
->package_name()
7311 : no
->package()->name());
7312 name
= unique_prefix
;
7313 name
.append(1, '.');
7314 name
.append(package_name
);
7315 name
.append(1, '.');
7316 if (this->in_function_
!= NULL
)
7318 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7319 name
.append(1, '$');
7322 name
.append(Gogo::unpack_hidden_name(no
->name()));
7324 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
7329 // Export the type. This is called to export a global type.
7332 Named_type::export_named_type(Export
* exp
, const std::string
&) const
7334 // We don't need to write the name of the type here, because it will
7335 // be written by Export::write_type anyhow.
7336 exp
->write_c_string("type ");
7337 exp
->write_type(this);
7338 exp
->write_c_string(";\n");
7341 // Import a named type.
7344 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
7346 imp
->require_c_string("type ");
7347 Type
*type
= imp
->read_type();
7348 *ptype
= type
->named_type();
7349 gcc_assert(*ptype
!= NULL
);
7350 imp
->require_c_string(";\n");
7353 // Export the type when it is referenced by another type. In this
7354 // case Export::export_type will already have issued the name.
7357 Named_type::do_export(Export
* exp
) const
7359 exp
->write_type(this->type_
);
7361 // To save space, we only export the methods directly attached to
7363 Bindings
* methods
= this->local_methods_
;
7364 if (methods
== NULL
)
7367 exp
->write_c_string("\n");
7368 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
7369 p
!= methods
->end_definitions();
7372 exp
->write_c_string(" ");
7373 (*p
)->export_named_object(exp
);
7376 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
7377 p
!= methods
->end_declarations();
7380 if (p
->second
->is_function_declaration())
7382 exp
->write_c_string(" ");
7383 p
->second
->export_named_object(exp
);
7388 // Make a named type.
7391 Type::make_named_type(Named_object
* named_object
, Type
* type
,
7392 source_location location
)
7394 return new Named_type(named_object
, type
, location
);
7397 // Finalize the methods for TYPE. It will be a named type or a struct
7398 // type. This sets *ALL_METHODS to the list of methods, and builds
7399 // all required stubs.
7402 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, source_location location
,
7403 Methods
** all_methods
)
7405 *all_methods
= NULL
;
7406 Types_seen types_seen
;
7407 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
7409 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
7412 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7413 // build up the struct field indexes as we go. DEPTH is the depth of
7414 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7415 // adding these methods for an anonymous field with pointer type.
7416 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7417 // calls the real method. TYPES_SEEN is used to avoid infinite
7421 Type::add_methods_for_type(const Type
* type
,
7422 const Method::Field_indexes
* field_indexes
,
7424 bool is_embedded_pointer
,
7425 bool needs_stub_method
,
7426 Types_seen
* types_seen
,
7429 // Pointer types may not have methods.
7430 if (type
->points_to() != NULL
)
7433 const Named_type
* nt
= type
->named_type();
7436 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
7442 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
7443 is_embedded_pointer
, needs_stub_method
,
7446 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
7447 is_embedded_pointer
, needs_stub_method
,
7448 types_seen
, methods
);
7450 // If we are called with depth > 0, then we are looking at an
7451 // anonymous field of a struct. If such a field has interface type,
7452 // then we need to add the interface methods. We don't want to add
7453 // them when depth == 0, because we will already handle them
7454 // following the usual rules for an interface type.
7456 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
7459 // Add the local methods for the named type NT to *METHODS. The
7460 // parameters are as for add_methods_to_type.
7463 Type::add_local_methods_for_type(const Named_type
* nt
,
7464 const Method::Field_indexes
* field_indexes
,
7466 bool is_embedded_pointer
,
7467 bool needs_stub_method
,
7470 const Bindings
* local_methods
= nt
->local_methods();
7471 if (local_methods
== NULL
)
7474 if (*methods
== NULL
)
7475 *methods
= new Methods();
7477 for (Bindings::const_declarations_iterator p
=
7478 local_methods
->begin_declarations();
7479 p
!= local_methods
->end_declarations();
7482 Named_object
* no
= p
->second
;
7483 bool is_value_method
= (is_embedded_pointer
7484 || !Type::method_expects_pointer(no
));
7485 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
7487 || (depth
> 0 && is_value_method
)));
7488 if (!(*methods
)->insert(no
->name(), m
))
7493 // Add the embedded methods for TYPE to *METHODS. These are the
7494 // methods attached to anonymous fields. The parameters are as for
7495 // add_methods_to_type.
7498 Type::add_embedded_methods_for_type(const Type
* type
,
7499 const Method::Field_indexes
* field_indexes
,
7501 bool is_embedded_pointer
,
7502 bool needs_stub_method
,
7503 Types_seen
* types_seen
,
7506 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7508 const Struct_type
* st
= type
->struct_type();
7512 const Struct_field_list
* fields
= st
->fields();
7517 for (Struct_field_list::const_iterator pf
= fields
->begin();
7518 pf
!= fields
->end();
7521 if (!pf
->is_anonymous())
7524 Type
* ftype
= pf
->type();
7525 bool is_pointer
= false;
7526 if (ftype
->points_to() != NULL
)
7528 ftype
= ftype
->points_to();
7531 Named_type
* fnt
= ftype
->named_type();
7534 // This is an error, but it will be diagnosed elsewhere.
7538 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
7539 sub_field_indexes
->next
= field_indexes
;
7540 sub_field_indexes
->field_index
= i
;
7542 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
7543 (is_embedded_pointer
|| is_pointer
),
7552 // If TYPE is an interface type, then add its method to *METHODS.
7553 // This is for interface methods attached to an anonymous field. The
7554 // parameters are as for add_methods_for_type.
7557 Type::add_interface_methods_for_type(const Type
* type
,
7558 const Method::Field_indexes
* field_indexes
,
7562 const Interface_type
* it
= type
->interface_type();
7566 const Typed_identifier_list
* imethods
= it
->methods();
7567 if (imethods
== NULL
)
7570 if (*methods
== NULL
)
7571 *methods
= new Methods();
7573 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
7574 pm
!= imethods
->end();
7577 Function_type
* fntype
= pm
->type()->function_type();
7580 // This is an error, but it should be reported elsewhere
7581 // when we look at the methods for IT.
7584 gcc_assert(!fntype
->is_method());
7585 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
7586 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
7587 field_indexes
, depth
);
7588 if (!(*methods
)->insert(pm
->name(), m
))
7593 // Build stub methods for TYPE as needed. METHODS is the set of
7594 // methods for the type. A stub method may be needed when a type
7595 // inherits a method from an anonymous field. When we need the
7596 // address of the method, as in a type descriptor, we need to build a
7597 // little stub which does the required field dereferences and jumps to
7598 // the real method. LOCATION is the location of the type definition.
7601 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
7602 source_location location
)
7604 if (methods
== NULL
)
7606 for (Methods::const_iterator p
= methods
->begin();
7607 p
!= methods
->end();
7610 Method
* m
= p
->second
;
7611 if (m
->is_ambiguous() || !m
->needs_stub_method())
7614 const std::string
& name(p
->first
);
7616 // Build a stub method.
7618 const Function_type
* fntype
= m
->type();
7620 static unsigned int counter
;
7622 snprintf(buf
, sizeof buf
, "$this%u", counter
);
7625 Type
* receiver_type
= const_cast<Type
*>(type
);
7626 if (!m
->is_value_method())
7627 receiver_type
= Type::make_pointer_type(receiver_type
);
7628 source_location receiver_location
= m
->receiver_location();
7629 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
7632 const Typed_identifier_list
* fnparams
= fntype
->parameters();
7633 Typed_identifier_list
* stub_params
;
7634 if (fnparams
== NULL
|| fnparams
->empty())
7638 // We give each stub parameter a unique name.
7639 stub_params
= new Typed_identifier_list();
7640 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
7641 pp
!= fnparams
->end();
7645 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
7646 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
7652 const Typed_identifier_list
* fnresults
= fntype
->results();
7653 Typed_identifier_list
* stub_results
;
7654 if (fnresults
== NULL
|| fnresults
->empty())
7655 stub_results
= NULL
;
7658 // We create the result parameters without any names, since
7659 // we won't refer to them.
7660 stub_results
= new Typed_identifier_list();
7661 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
7662 pr
!= fnresults
->end();
7664 stub_results
->push_back(Typed_identifier("", pr
->type(),
7668 Function_type
* stub_type
= Type::make_function_type(receiver
,
7671 fntype
->location());
7672 if (fntype
->is_varargs())
7673 stub_type
->set_is_varargs();
7675 // We only create the function in the package which creates the
7677 const Package
* package
;
7678 if (type
->named_type() == NULL
)
7681 package
= type
->named_type()->named_object()->package();
7683 if (package
!= NULL
)
7684 stub
= Named_object::make_function_declaration(name
, package
,
7685 stub_type
, location
);
7688 stub
= gogo
->start_function(name
, stub_type
, false,
7689 fntype
->location());
7690 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
7691 fntype
->is_varargs(), location
);
7692 gogo
->finish_function(fntype
->location());
7695 m
->set_stub_object(stub
);
7699 // Build a stub method which adjusts the receiver as required to call
7700 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7701 // PARAMS is the list of function parameters.
7704 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
7705 const char* receiver_name
,
7706 const Typed_identifier_list
* params
,
7708 source_location location
)
7710 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
7711 gcc_assert(receiver_object
!= NULL
);
7713 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
7714 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
7715 if (expr
->type()->points_to() == NULL
)
7716 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7718 Expression_list
* arguments
;
7719 if (params
== NULL
|| params
->empty())
7723 arguments
= new Expression_list();
7724 for (Typed_identifier_list::const_iterator p
= params
->begin();
7728 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
7729 gcc_assert(param
!= NULL
);
7730 Expression
* param_ref
= Expression::make_var_reference(param
,
7732 arguments
->push_back(param_ref
);
7736 Expression
* func
= method
->bind_method(expr
, location
);
7737 gcc_assert(func
!= NULL
);
7738 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
7740 size_t count
= call
->result_count();
7742 gogo
->add_statement(Statement::make_statement(call
));
7745 Expression_list
* retvals
= new Expression_list();
7747 retvals
->push_back(call
);
7750 for (size_t i
= 0; i
< count
; ++i
)
7751 retvals
->push_back(Expression::make_call_result(call
, i
));
7753 const Function
* function
= gogo
->current_function()->func_value();
7754 const Typed_identifier_list
* results
= function
->type()->results();
7755 Statement
* retstat
= Statement::make_return_statement(results
, retvals
,
7757 gogo
->add_statement(retstat
);
7761 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7762 // in reverse order.
7765 Type::apply_field_indexes(Expression
* expr
,
7766 const Method::Field_indexes
* field_indexes
,
7767 source_location location
)
7769 if (field_indexes
== NULL
)
7771 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
7772 Struct_type
* stype
= expr
->type()->deref()->struct_type();
7773 gcc_assert(stype
!= NULL
7774 && field_indexes
->field_index
< stype
->field_count());
7775 if (expr
->type()->struct_type() == NULL
)
7777 gcc_assert(expr
->type()->points_to() != NULL
);
7778 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7779 gcc_assert(expr
->type()->struct_type() == stype
);
7781 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
7785 // Return whether NO is a method for which the receiver is a pointer.
7788 Type::method_expects_pointer(const Named_object
* no
)
7790 const Function_type
*fntype
;
7791 if (no
->is_function())
7792 fntype
= no
->func_value()->type();
7793 else if (no
->is_function_declaration())
7794 fntype
= no
->func_declaration_value()->type();
7797 return fntype
->receiver()->type()->points_to() != NULL
;
7800 // Given a set of methods for a type, METHODS, return the method NAME,
7801 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7802 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7803 // but is ambiguous (and return NULL).
7806 Type::method_function(const Methods
* methods
, const std::string
& name
,
7809 if (is_ambiguous
!= NULL
)
7810 *is_ambiguous
= false;
7811 if (methods
== NULL
)
7813 Methods::const_iterator p
= methods
->find(name
);
7814 if (p
== methods
->end())
7816 Method
* m
= p
->second
;
7817 if (m
->is_ambiguous())
7819 if (is_ambiguous
!= NULL
)
7820 *is_ambiguous
= true;
7826 // Look for field or method NAME for TYPE. Return an Expression for
7827 // the field or method bound to EXPR. If there is no such field or
7828 // method, give an appropriate error and return an error expression.
7831 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
7832 const std::string
& name
,
7833 source_location location
)
7835 if (type
->deref()->is_error_type())
7836 return Expression::make_error(location
);
7838 const Named_type
* nt
= type
->named_type();
7840 nt
= type
->deref()->named_type();
7841 const Struct_type
* st
= type
->deref()->struct_type();
7842 const Interface_type
* it
= type
->deref()->interface_type();
7844 // If this is a pointer to a pointer, then it is possible that the
7845 // pointed-to type has methods.
7849 && type
->points_to() != NULL
7850 && type
->points_to()->points_to() != NULL
)
7852 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7853 type
= type
->points_to();
7854 if (type
->deref()->is_error_type())
7855 return Expression::make_error(location
);
7856 nt
= type
->points_to()->named_type();
7857 st
= type
->points_to()->struct_type();
7858 it
= type
->points_to()->interface_type();
7861 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
7862 || expr
->is_addressable());
7863 std::vector
<const Named_type
*> seen
;
7864 bool is_method
= false;
7865 bool found_pointer_method
= false;
7868 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
,
7869 &seen
, NULL
, &is_method
,
7870 &found_pointer_method
, &ambig1
, &ambig2
))
7875 gcc_assert(st
!= NULL
);
7876 if (type
->struct_type() == NULL
)
7878 gcc_assert(type
->points_to() != NULL
);
7879 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
7881 gcc_assert(expr
->type()->struct_type() == st
);
7883 ret
= st
->field_reference(expr
, name
, location
);
7885 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7886 ret
= Expression::make_interface_field_reference(expr
, name
,
7892 m
= nt
->method_function(name
, NULL
);
7893 else if (st
!= NULL
)
7894 m
= st
->method_function(name
, NULL
);
7897 gcc_assert(m
!= NULL
);
7898 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
7899 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7900 ret
= m
->bind_method(expr
, location
);
7902 gcc_assert(ret
!= NULL
);
7907 if (!ambig1
.empty())
7908 error_at(location
, "%qs is ambiguous via %qs and %qs",
7909 Gogo::message_name(name
).c_str(),
7910 Gogo::message_name(ambig1
).c_str(),
7911 Gogo::message_name(ambig2
).c_str());
7912 else if (found_pointer_method
)
7913 error_at(location
, "method requires a pointer");
7914 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
7916 ("reference to field %qs in object which "
7917 "has no fields or methods"),
7918 Gogo::message_name(name
).c_str());
7922 if (!Gogo::is_hidden_name(name
))
7923 is_unexported
= false;
7926 std::string unpacked
= Gogo::unpack_hidden_name(name
);
7928 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
7933 error_at(location
, "reference to unexported field or method %qs",
7934 Gogo::message_name(name
).c_str());
7936 error_at(location
, "reference to undefined field or method %qs",
7937 Gogo::message_name(name
).c_str());
7939 return Expression::make_error(location
);
7943 // Look in TYPE for a field or method named NAME, return true if one
7944 // is found. This looks through embedded anonymous fields and handles
7945 // ambiguity. If a method is found, sets *IS_METHOD to true;
7946 // otherwise, if a field is found, set it to false. If
7947 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7948 // whose address can not be taken. SEEN is used to avoid infinite
7949 // recursion on invalid types.
7951 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
7952 // method we couldn't use because it requires a pointer. LEVEL is
7953 // used for recursive calls, and can be NULL for a non-recursive call.
7954 // When this function returns false because it finds that the name is
7955 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
7956 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
7957 // will be unchanged.
7959 // This function just returns whether or not there is a field or
7960 // method, and whether it is a field or method. It doesn't build an
7961 // expression to refer to it. If it is a method, we then look in the
7962 // list of all methods for the type. If it is a field, the search has
7963 // to be done again, looking only for fields, and building up the
7964 // expression as we go.
7967 Type::find_field_or_method(const Type
* type
,
7968 const std::string
& name
,
7969 bool receiver_can_be_pointer
,
7970 std::vector
<const Named_type
*>* seen
,
7973 bool* found_pointer_method
,
7974 std::string
* ambig1
,
7975 std::string
* ambig2
)
7977 // Named types can have locally defined methods.
7978 const Named_type
* nt
= type
->named_type();
7979 if (nt
== NULL
&& type
->points_to() != NULL
)
7980 nt
= type
->points_to()->named_type();
7983 Named_object
* no
= nt
->find_local_method(name
);
7986 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
7992 // Record that we have found a pointer method in order to
7993 // give a better error message if we don't find anything
7995 *found_pointer_method
= true;
7998 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
8004 // We've already seen this type when searching for methods.
8010 // Interface types can have methods.
8011 const Interface_type
* it
= type
->deref()->interface_type();
8012 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
8018 // Struct types can have fields. They can also inherit fields and
8019 // methods from anonymous fields.
8020 const Struct_type
* st
= type
->deref()->struct_type();
8023 const Struct_field_list
* fields
= st
->fields();
8028 seen
->push_back(nt
);
8030 int found_level
= 0;
8031 bool found_is_method
= false;
8032 std::string found_ambig1
;
8033 std::string found_ambig2
;
8034 const Struct_field
* found_parent
= NULL
;
8035 for (Struct_field_list::const_iterator pf
= fields
->begin();
8036 pf
!= fields
->end();
8039 if (pf
->field_name() == name
)
8047 if (!pf
->is_anonymous())
8050 if (pf
->type()->deref()->is_error_type()
8051 || pf
->type()->deref()->is_undefined())
8054 Named_type
* fnt
= pf
->type()->named_type();
8056 fnt
= pf
->type()->deref()->named_type();
8057 gcc_assert(fnt
!= NULL
);
8059 int sublevel
= level
== NULL
? 1 : *level
+ 1;
8061 std::string subambig1
;
8062 std::string subambig2
;
8063 bool subfound
= Type::find_field_or_method(fnt
,
8065 receiver_can_be_pointer
,
8069 found_pointer_method
,
8074 if (!subambig1
.empty())
8076 // The name was found via this field, but is ambiguous.
8077 // if the ambiguity is lower or at the same level as
8078 // anything else we have already found, then we want to
8079 // pass the ambiguity back to the caller.
8080 if (found_level
== 0 || sublevel
<= found_level
)
8082 found_ambig1
= pf
->field_name() + '.' + subambig1
;
8083 found_ambig2
= pf
->field_name() + '.' + subambig2
;
8084 found_level
= sublevel
;
8090 // The name was found via this field. Use the level to see
8091 // if we want to use this one, or whether it introduces an
8093 if (found_level
== 0 || sublevel
< found_level
)
8095 found_level
= sublevel
;
8096 found_is_method
= sub_is_method
;
8097 found_ambig1
.clear();
8098 found_ambig2
.clear();
8099 found_parent
= &*pf
;
8101 else if (sublevel
> found_level
)
8103 else if (found_ambig1
.empty())
8105 // We found an ambiguity.
8106 gcc_assert(found_parent
!= NULL
);
8107 found_ambig1
= found_parent
->field_name();
8108 found_ambig2
= pf
->field_name();
8112 // We found an ambiguity, but we already know of one.
8113 // Just report the earlier one.
8118 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
8119 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
8120 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
8121 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
8126 if (found_level
== 0)
8128 else if (!found_ambig1
.empty())
8130 gcc_assert(!found_ambig1
.empty());
8131 ambig1
->assign(found_ambig1
);
8132 ambig2
->assign(found_ambig2
);
8134 *level
= found_level
;
8140 *level
= found_level
;
8141 *is_method
= found_is_method
;
8146 // Return whether NAME is an unexported field or method for TYPE.
8149 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
8150 const std::string
& name
,
8151 std::vector
<const Named_type
*>* seen
)
8153 const Named_type
* nt
= type
->named_type();
8155 nt
= type
->deref()->named_type();
8158 if (nt
->is_unexported_local_method(gogo
, name
))
8161 for (std::vector
<const Named_type
*>::const_iterator p
= seen
->begin();
8167 // We've already seen this type.
8173 type
= type
->deref();
8175 const Interface_type
* it
= type
->interface_type();
8176 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
8179 const Struct_type
* st
= type
->struct_type();
8180 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
8186 const Struct_field_list
* fields
= st
->fields();
8191 seen
->push_back(nt
);
8193 for (Struct_field_list::const_iterator pf
= fields
->begin();
8194 pf
!= fields
->end();
8197 if (pf
->is_anonymous()
8198 && !pf
->type()->deref()->is_error_type()
8199 && !pf
->type()->deref()->is_undefined())
8201 Named_type
* subtype
= pf
->type()->named_type();
8202 if (subtype
== NULL
)
8203 subtype
= pf
->type()->deref()->named_type();
8204 if (subtype
== NULL
)
8206 // This is an error, but it will be diagnosed elsewhere.
8209 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
, seen
))
8224 // Class Forward_declaration.
8226 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
8227 : Type(TYPE_FORWARD
),
8228 named_object_(named_object
->resolve()), warned_(false)
8230 gcc_assert(this->named_object_
->is_unknown()
8231 || this->named_object_
->is_type_declaration());
8234 // Return the named object.
8237 Forward_declaration_type::named_object()
8239 return this->named_object_
->resolve();
8243 Forward_declaration_type::named_object() const
8245 return this->named_object_
->resolve();
8248 // Return the name of the forward declared type.
8251 Forward_declaration_type::name() const
8253 return this->named_object()->name();
8256 // Warn about a use of a type which has been declared but not defined.
8259 Forward_declaration_type::warn() const
8261 Named_object
* no
= this->named_object_
->resolve();
8262 if (no
->is_unknown())
8264 // The name was not defined anywhere.
8267 error_at(this->named_object_
->location(),
8268 "use of undefined type %qs",
8269 no
->message_name().c_str());
8270 this->warned_
= true;
8273 else if (no
->is_type_declaration())
8275 // The name was seen as a type, but the type was never defined.
8276 if (no
->type_declaration_value()->using_type())
8278 error_at(this->named_object_
->location(),
8279 "use of undefined type %qs",
8280 no
->message_name().c_str());
8281 this->warned_
= true;
8286 // The name was defined, but not as a type.
8289 error_at(this->named_object_
->location(), "expected type");
8290 this->warned_
= true;
8295 // Get the base type of a declaration. This gives an error if the
8296 // type has not yet been defined.
8299 Forward_declaration_type::real_type()
8301 if (this->is_defined())
8302 return this->named_object()->type_value();
8306 return Type::make_error_type();
8311 Forward_declaration_type::real_type() const
8313 if (this->is_defined())
8314 return this->named_object()->type_value();
8318 return Type::make_error_type();
8322 // Return whether the base type is defined.
8325 Forward_declaration_type::is_defined() const
8327 return this->named_object()->is_type();
8330 // Add a method. This is used when methods are defined before the
8334 Forward_declaration_type::add_method(const std::string
& name
,
8337 Named_object
* no
= this->named_object();
8338 if (no
->is_unknown())
8339 no
->declare_as_type();
8340 return no
->type_declaration_value()->add_method(name
, function
);
8343 // Add a method declaration. This is used when methods are declared
8347 Forward_declaration_type::add_method_declaration(const std::string
& name
,
8348 Function_type
* type
,
8349 source_location location
)
8351 Named_object
* no
= this->named_object();
8352 if (no
->is_unknown())
8353 no
->declare_as_type();
8354 Type_declaration
* td
= no
->type_declaration_value();
8355 return td
->add_method_declaration(name
, type
, location
);
8361 Forward_declaration_type::do_traverse(Traverse
* traverse
)
8363 if (this->is_defined()
8364 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
8365 return TRAVERSE_EXIT
;
8366 return TRAVERSE_CONTINUE
;
8369 // Get a tree for the type.
8372 Forward_declaration_type::do_get_tree(Gogo
* gogo
)
8374 if (this->is_defined())
8375 return Type::get_named_type_tree(gogo
, this->real_type());
8378 return error_mark_node
;
8380 // We represent an undefined type as a struct with no fields. That
8381 // should work fine for the middle-end, since the same case can
8383 Named_object
* no
= this->named_object();
8384 tree type_tree
= make_node(RECORD_TYPE
);
8385 tree id
= no
->get_id(gogo
);
8386 tree decl
= build_decl(no
->location(), TYPE_DECL
, id
, type_tree
);
8387 TYPE_NAME(type_tree
) = decl
;
8388 layout_type(type_tree
);
8392 // Build a type descriptor for a forwarded type.
8395 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8397 if (!this->is_defined())
8398 return Expression::make_nil(BUILTINS_LOCATION
);
8401 Type
* t
= this->real_type();
8403 return this->named_type_descriptor(gogo
, t
, name
);
8405 return Expression::make_type_descriptor(t
, BUILTINS_LOCATION
);
8409 // The reflection string.
8412 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8414 this->append_reflection(this->real_type(), gogo
, ret
);
8417 // The mangled name.
8420 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8422 if (this->is_defined())
8423 this->append_mangled_name(this->real_type(), gogo
, ret
);
8426 const Named_object
* no
= this->named_object();
8428 if (no
->package() == NULL
)
8429 name
= gogo
->package_name();
8431 name
= no
->package()->name();
8433 name
+= Gogo::unpack_hidden_name(no
->name());
8435 snprintf(buf
, sizeof buf
, "N%u_",
8436 static_cast<unsigned int>(name
.length()));
8442 // Export a forward declaration. This can happen when a defined type
8443 // refers to a type which is only declared (and is presumably defined
8444 // in some other file in the same package).
8447 Forward_declaration_type::do_export(Export
*) const
8449 // If there is a base type, that should be exported instead of this.
8450 gcc_assert(!this->is_defined());
8452 // We don't output anything.
8455 // Make a forward declaration.
8458 Type::make_forward_declaration(Named_object
* named_object
)
8460 return new Forward_declaration_type(named_object
);
8463 // Class Typed_identifier_list.
8465 // Sort the entries by name.
8467 struct Typed_identifier_list_sort
8471 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
8472 { return t1
.name() < t2
.name(); }
8476 Typed_identifier_list::sort_by_name()
8478 std::sort(this->entries_
.begin(), this->entries_
.end(),
8479 Typed_identifier_list_sort());
8485 Typed_identifier_list::traverse(Traverse
* traverse
)
8487 for (Typed_identifier_list::const_iterator p
= this->begin();
8491 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
8492 return TRAVERSE_EXIT
;
8494 return TRAVERSE_CONTINUE
;
8499 Typed_identifier_list
*
8500 Typed_identifier_list::copy() const
8502 Typed_identifier_list
* ret
= new Typed_identifier_list();
8503 for (Typed_identifier_list::const_iterator p
= this->begin();
8506 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));