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 static_cast<Named_type
*>(this)->real_type()->base();
58 return static_cast<Forward_declaration_type
*>(this)->real_type()->base();
67 switch (this->classification_
)
70 return static_cast<const Named_type
*>(this)->real_type()->base();
73 const Forward_declaration_type
* ftype
=
74 static_cast<const Forward_declaration_type
*>(this);
75 return ftype
->real_type()->base();
82 // Skip defined forward declarations.
88 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
89 while (ftype
!= NULL
&& ftype
->is_defined())
91 t
= ftype
->real_type();
92 ftype
= t
->forward_declaration_type();
98 Type::forwarded() const
100 const Type
* t
= this;
101 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
102 while (ftype
!= NULL
&& ftype
->is_defined())
104 t
= ftype
->real_type();
105 ftype
= t
->forward_declaration_type();
110 // If this is a named type, return it. Otherwise, return NULL.
115 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
119 Type::named_type() const
121 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
124 // Return true if this type is not defined.
127 Type::is_undefined() const
129 return this->forwarded()->forward_declaration_type() != NULL
;
132 // Return true if this is a basic type: a type which is not composed
133 // of other types, and is not void.
136 Type::is_basic_type() const
138 switch (this->classification_
)
161 return this->base()->is_basic_type();
168 // Return true if this is an abstract type.
171 Type::is_abstract() const
173 switch (this->classification())
176 return this->integer_type()->is_abstract();
178 return this->float_type()->is_abstract();
180 return this->complex_type()->is_abstract();
182 return this->is_abstract_string_type();
184 return this->is_abstract_boolean_type();
190 // Return a non-abstract version of an abstract type.
193 Type::make_non_abstract_type()
195 gcc_assert(this->is_abstract());
196 switch (this->classification())
199 return Type::lookup_integer_type("int");
201 return Type::lookup_float_type("float");
203 return Type::lookup_complex_type("complex");
205 return Type::lookup_string_type();
207 return Type::lookup_bool_type();
213 // Return true if this is an error type. Don't give an error if we
214 // try to dereference an undefined forwarding type, as this is called
215 // in the parser when the type may legitimately be undefined.
218 Type::is_error_type() const
220 const Type
* t
= this->forwarded();
221 // Note that we return false for an undefined forward type.
222 switch (t
->classification_
)
227 return t
->named_type()->real_type()->is_error_type();
233 // If this is a pointer type, return the type to which it points.
234 // Otherwise, return NULL.
237 Type::points_to() const
239 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
241 return ptype
== NULL
? NULL
: ptype
->points_to();
244 // Return whether this is an open array type.
247 Type::is_open_array_type() const
249 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
252 // Return whether this is the predeclared constant nil being used as a
256 Type::is_nil_constant_as_type() const
258 const Type
* t
= this->forwarded();
259 if (t
->forward_declaration_type() != NULL
)
261 const Named_object
* no
= t
->forward_declaration_type()->named_object();
262 if (no
->is_unknown())
263 no
= no
->unknown_value()->real_named_object();
266 && no
->const_value()->expr()->is_nil_expression())
275 Type::traverse(Type
* type
, Traverse
* traverse
)
277 gcc_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
278 || (traverse
->traverse_mask()
279 & Traverse::traverse_expressions
) != 0);
280 if (traverse
->remember_type(type
))
282 // We have already traversed this type.
283 return TRAVERSE_CONTINUE
;
285 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
287 int t
= traverse
->type(type
);
288 if (t
== TRAVERSE_EXIT
)
289 return TRAVERSE_EXIT
;
290 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
291 return TRAVERSE_CONTINUE
;
293 // An array type has an expression which we need to traverse if
294 // traverse_expressions is set.
295 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
296 return TRAVERSE_EXIT
;
297 return TRAVERSE_CONTINUE
;
300 // Default implementation for do_traverse for child class.
303 Type::do_traverse(Traverse
*)
305 return TRAVERSE_CONTINUE
;
308 // Return whether two types are identical. If REASON is not NULL,
309 // optionally set *REASON to the reason the types are not identical.
312 Type::are_identical(const Type
* t1
, const Type
* t2
, std::string
* reason
)
314 if (t1
== NULL
|| t2
== NULL
)
316 // Something is wrong. Return true to avoid cascading errors.
320 // Skip defined forward declarations.
321 t1
= t1
->forwarded();
322 t2
= t2
->forwarded();
327 // An undefined forward declaration is an error, so we return true
328 // to avoid cascading errors.
329 if (t1
->forward_declaration_type() != NULL
330 || t2
->forward_declaration_type() != NULL
)
333 // Avoid cascading errors with error types.
334 if (t1
->is_error_type() || t2
->is_error_type())
337 // Get a good reason for the sink type. Note that the sink type on
338 // the left hand side of an assignment is handled in are_assignable.
339 if (t1
->is_sink_type() || t2
->is_sink_type())
342 *reason
= "invalid use of _";
346 // A named type is only identical to itself.
347 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
350 // Check type shapes.
351 if (t1
->classification() != t2
->classification())
354 switch (t1
->classification())
360 // These types are always identical.
364 return t1
->integer_type()->is_identical(t2
->integer_type());
367 return t1
->float_type()->is_identical(t2
->float_type());
370 return t1
->complex_type()->is_identical(t2
->complex_type());
373 return t1
->function_type()->is_identical(t2
->function_type(),
378 return Type::are_identical(t1
->points_to(), t2
->points_to(), reason
);
381 return t1
->struct_type()->is_identical(t2
->struct_type());
384 return t1
->array_type()->is_identical(t2
->array_type());
387 return t1
->map_type()->is_identical(t2
->map_type());
390 return t1
->channel_type()->is_identical(t2
->channel_type());
393 return t1
->interface_type()->is_identical(t2
->interface_type());
400 // Return true if it's OK to have a binary operation with types LHS
401 // and RHS. This is not used for shifts or comparisons.
404 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
406 if (Type::are_identical(lhs
, rhs
, NULL
))
409 // A constant of abstract bool type may be mixed with any bool type.
410 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
411 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
414 // A constant of abstract string type may be mixed with any string
416 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
417 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
423 // A constant of abstract integer, float, or complex type may be
424 // mixed with an integer, float, or complex type.
425 if ((rhs
->is_abstract()
426 && (rhs
->integer_type() != NULL
427 || rhs
->float_type() != NULL
428 || rhs
->complex_type() != NULL
)
429 && (lhs
->integer_type() != NULL
430 || lhs
->float_type() != NULL
431 || lhs
->complex_type() != NULL
))
432 || (lhs
->is_abstract()
433 && (lhs
->integer_type() != NULL
434 || lhs
->float_type() != NULL
435 || lhs
->complex_type() != NULL
)
436 && (rhs
->integer_type() != NULL
437 || rhs
->float_type() != NULL
438 || rhs
->complex_type() != NULL
)))
441 // The nil type may be compared to a pointer, an interface type, a
442 // slice type, a channel type, a map type, or a function type.
443 if (lhs
->is_nil_type()
444 && (rhs
->points_to() != NULL
445 || rhs
->interface_type() != NULL
446 || rhs
->is_open_array_type()
447 || rhs
->map_type() != NULL
448 || rhs
->channel_type() != NULL
449 || rhs
->function_type() != NULL
))
451 if (rhs
->is_nil_type()
452 && (lhs
->points_to() != NULL
453 || lhs
->interface_type() != NULL
454 || lhs
->is_open_array_type()
455 || lhs
->map_type() != NULL
456 || lhs
->channel_type() != NULL
457 || lhs
->function_type() != NULL
))
463 // Return true if a value with type RHS may be assigned to a variable
464 // with type LHS. If REASON is not NULL, set *REASON to the reason
465 // the types are not assignable.
468 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
470 // Do some checks first. Make sure the types are defined.
471 if (lhs
!= NULL
&& lhs
->forwarded()->forward_declaration_type() == NULL
)
473 // Any value may be assigned to the blank identifier.
474 if (lhs
->is_sink_type())
477 // All fields of a struct must be exported, or the assignment
478 // must be in the same package.
479 if (rhs
!= NULL
&& rhs
->forwarded()->forward_declaration_type() == NULL
)
481 if (lhs
->has_hidden_fields(NULL
, reason
)
482 || rhs
->has_hidden_fields(NULL
, reason
))
487 // Identical types are assignable.
488 if (Type::are_identical(lhs
, rhs
, reason
))
491 // The types are assignable if they have identical underlying types
492 // and either LHS or RHS is not a named type.
493 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
494 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
495 && Type::are_identical(lhs
->base(), rhs
->base(), reason
))
498 // The types are assignable if LHS is an interface type and RHS
499 // implements the required methods.
500 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
501 if (lhs_interface_type
!= NULL
)
503 if (lhs_interface_type
->implements_interface(rhs
, reason
))
505 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
506 if (rhs_interface_type
!= NULL
507 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
512 // The type are assignable if RHS is a bidirectional channel type,
513 // LHS is a channel type, they have identical element types, and
514 // either LHS or RHS is not a named type.
515 if (lhs
->channel_type() != NULL
516 && rhs
->channel_type() != NULL
517 && rhs
->channel_type()->may_send()
518 && rhs
->channel_type()->may_receive()
519 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
520 && Type::are_identical(lhs
->channel_type()->element_type(),
521 rhs
->channel_type()->element_type(),
525 // The nil type may be assigned to a pointer, function, slice, map,
526 // channel, or interface type.
527 if (rhs
->is_nil_type()
528 && (lhs
->points_to() != NULL
529 || lhs
->function_type() != NULL
530 || lhs
->is_open_array_type()
531 || lhs
->map_type() != NULL
532 || lhs
->channel_type() != NULL
533 || lhs
->interface_type() != NULL
))
536 // An untyped constant may be assigned to a numeric type if it is
537 // representable in that type.
538 if (rhs
->is_abstract()
539 && (lhs
->integer_type() != NULL
540 || lhs
->float_type() != NULL
541 || lhs
->complex_type() != NULL
))
545 // Give some better error messages.
546 if (reason
!= NULL
&& reason
->empty())
548 if (rhs
->interface_type() != NULL
)
549 reason
->assign(_("need explicit conversion"));
550 else if (rhs
->is_call_multiple_result_type())
551 reason
->assign(_("multiple value function call in "
552 "single value context"));
553 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
555 size_t len
= (lhs
->named_type()->name().length()
556 + rhs
->named_type()->name().length()
558 char* buf
= new char[len
];
559 snprintf(buf
, len
, _("cannot use type %s as type %s"),
560 rhs
->named_type()->message_name().c_str(),
561 lhs
->named_type()->message_name().c_str());
570 // Return true if a value with type RHS may be converted to type LHS.
571 // If REASON is not NULL, set *REASON to the reason the types are not
575 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
577 // The types are convertible if they are assignable.
578 if (Type::are_assignable(lhs
, rhs
, reason
))
581 // The types are convertible if they have identical underlying
583 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
584 && Type::are_identical(lhs
->base(), rhs
->base(), reason
))
587 // The types are convertible if they are both unnamed pointer types
588 // and their pointer base types have identical underlying types.
589 if (lhs
->named_type() == NULL
590 && rhs
->named_type() == NULL
591 && lhs
->points_to() != NULL
592 && rhs
->points_to() != NULL
593 && (lhs
->points_to()->named_type() != NULL
594 || rhs
->points_to()->named_type() != NULL
)
595 && Type::are_identical(lhs
->points_to()->base(),
596 rhs
->points_to()->base(),
600 // Integer and floating point types are convertible to each other.
601 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
602 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
605 // Complex types are convertible to each other.
606 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
609 // An integer, or []byte, or []int, may be converted to a string.
610 if (lhs
->is_string_type())
612 if (rhs
->integer_type() != NULL
)
614 if (rhs
->is_open_array_type() && rhs
->named_type() == NULL
)
616 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
617 if (e
->integer_type() != NULL
618 && (e
== Type::lookup_integer_type("uint8")
619 || e
== Type::lookup_integer_type("int")))
624 // A string may be converted to []byte or []int.
625 if (rhs
->is_string_type()
626 && lhs
->is_open_array_type()
627 && lhs
->named_type() == NULL
)
629 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
630 if (e
->integer_type() != NULL
631 && (e
== Type::lookup_integer_type("uint8")
632 || e
== Type::lookup_integer_type("int")))
636 // An unsafe.Pointer type may be converted to any pointer type or to
637 // uintptr, and vice-versa.
638 if (lhs
->is_unsafe_pointer_type()
639 && (rhs
->points_to() != NULL
640 || (rhs
->integer_type() != NULL
641 && rhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
643 if (rhs
->is_unsafe_pointer_type()
644 && (lhs
->points_to() != NULL
645 || (lhs
->integer_type() != NULL
646 && lhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
649 // Give a better error message.
653 *reason
= "invalid type conversion";
656 std::string s
= "invalid type conversion (";
666 // Return whether this type has any hidden fields. This is only a
667 // possibility for a few types.
670 Type::has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
672 switch (this->forwarded()->classification_
)
675 return this->named_type()->named_type_has_hidden_fields(reason
);
677 return this->struct_type()->struct_has_hidden_fields(within
, reason
);
679 return this->array_type()->array_has_hidden_fields(within
, reason
);
685 // Return a hash code for the type to be used for method lookup.
688 Type::hash_for_method(Gogo
* gogo
) const
690 unsigned int ret
= 0;
691 if (this->classification_
!= TYPE_FORWARD
)
692 ret
+= this->classification_
;
693 return ret
+ this->do_hash_for_method(gogo
);
696 // Default implementation of do_hash_for_method. This is appropriate
697 // for types with no subfields.
700 Type::do_hash_for_method(Gogo
*) const
705 // Return a hash code for a string, given a starting hash.
708 Type::hash_string(const std::string
& s
, unsigned int h
)
710 const char* p
= s
.data();
711 size_t len
= s
.length();
712 for (; len
> 0; --len
)
720 // Default check for the expression passed to make. Any type which
721 // may be used with make implements its own version of this.
724 Type::do_check_make_expression(Expression_list
*, source_location
)
729 // Return whether an expression has an integer value. Report an error
730 // if not. This is used when handling calls to the predeclared make
734 Type::check_int_value(Expression
* e
, const char* errmsg
,
735 source_location location
)
737 if (e
->type()->integer_type() != NULL
)
740 // Check for a floating point constant with integer value.
745 if (e
->float_constant_value(fval
, &dummy
))
752 mpfr_clear_overflow();
753 mpfr_clear_erangeflag();
754 mpfr_get_z(ival
, fval
, GMP_RNDN
);
755 if (!mpfr_overflow_p()
756 && !mpfr_erangeflag_p()
757 && mpz_sgn(ival
) >= 0)
759 Named_type
* ntype
= Type::lookup_integer_type("int");
760 Integer_type
* inttype
= ntype
->integer_type();
762 mpz_init_set_ui(max
, 1);
763 mpz_mul_2exp(max
, max
, inttype
->bits() - 1);
764 ok
= mpz_cmp(ival
, max
) < 0;
778 error_at(location
, "%s", errmsg
);
782 // A hash table mapping unnamed types to trees.
784 Type::Type_trees
Type::type_trees
;
786 // Return a tree representing this type.
789 Type::get_tree(Gogo
* gogo
)
791 if (this->tree_
!= NULL
)
794 if (this->forward_declaration_type() != NULL
795 || this->named_type() != NULL
)
796 return this->get_tree_without_hash(gogo
);
798 // To avoid confusing GIMPLE, we need to translate all identical Go
799 // types to the same GIMPLE type. We use a hash table to do that.
800 // There is no need to use the hash table for named types, as named
801 // types are only identical to themselves.
803 std::pair
<Type
*, tree
> val(this, NULL
);
804 std::pair
<Type_trees::iterator
, bool> ins
=
805 Type::type_trees
.insert(val
);
806 if (!ins
.second
&& ins
.first
->second
!= NULL_TREE
)
808 this->tree_
= ins
.first
->second
;
812 tree t
= this->get_tree_without_hash(gogo
);
814 if (ins
.first
->second
== NULL_TREE
)
815 ins
.first
->second
= t
;
818 // We have already created a tree for this type. This can
819 // happen when an unnamed type is defined using a named type
820 // which in turns uses an identical unnamed type. Use the tree
821 // we created earlier and ignore the one we just built.
822 t
= ins
.first
->second
;
829 // Return a tree for a type without looking in the hash table for
830 // identical types. This is used for named types, since there is no
831 // point to looking in the hash table for them.
834 Type::get_tree_without_hash(Gogo
* gogo
)
836 if (this->tree_
== NULL_TREE
)
838 tree t
= this->do_get_tree(gogo
);
840 // For a recursive function or pointer type, we will temporarily
841 // return ptr_type_node during the recursion. We don't want to
842 // record that for a forwarding type, as it may confuse us
844 if (t
== ptr_type_node
&& this->forward_declaration_type() != NULL
)
848 go_preserve_from_gc(t
);
854 // Return a tree representing a zero initialization for this type.
857 Type::get_init_tree(Gogo
* gogo
, bool is_clear
)
859 tree type_tree
= this->get_tree(gogo
);
860 if (type_tree
== error_mark_node
)
861 return error_mark_node
;
862 return this->do_get_init_tree(gogo
, type_tree
, is_clear
);
865 // Any type which supports the builtin make function must implement
869 Type::do_make_expression_tree(Translate_context
*, Expression_list
*,
875 // Return a pointer to the type descriptor for this type.
878 Type::type_descriptor_pointer(Gogo
* gogo
)
880 Type
* t
= this->forwarded();
881 if (t
->type_descriptor_decl_
== NULL_TREE
)
883 Expression
* e
= t
->do_type_descriptor(gogo
, NULL
);
884 gogo
->build_type_descriptor_decl(t
, e
, &t
->type_descriptor_decl_
);
885 gcc_assert(t
->type_descriptor_decl_
!= NULL_TREE
886 && (t
->type_descriptor_decl_
== error_mark_node
887 || DECL_P(t
->type_descriptor_decl_
)));
889 if (t
->type_descriptor_decl_
== error_mark_node
)
890 return error_mark_node
;
891 return build_fold_addr_expr(t
->type_descriptor_decl_
);
894 // Return a composite literal for a type descriptor.
897 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
899 return type
->do_type_descriptor(gogo
, NULL
);
902 // Return a composite literal for a type descriptor with a name.
905 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
907 gcc_assert(name
!= NULL
&& type
->named_type() != name
);
908 return type
->do_type_descriptor(gogo
, name
);
911 // Make a builtin struct type from a list of fields. The fields are
912 // pairs of a name and a type.
915 Type::make_builtin_struct_type(int nfields
, ...)
918 va_start(ap
, nfields
);
920 source_location bloc
= BUILTINS_LOCATION
;
921 Struct_field_list
* sfl
= new Struct_field_list();
922 for (int i
= 0; i
< nfields
; i
++)
924 const char* field_name
= va_arg(ap
, const char *);
925 Type
* type
= va_arg(ap
, Type
*);
926 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
931 return Type::make_struct_type(sfl
, bloc
);
934 // Make a builtin named type.
937 Type::make_builtin_named_type(const char* name
, Type
* type
)
939 source_location bloc
= BUILTINS_LOCATION
;
940 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
941 return no
->type_value();
944 // Return the type of a type descriptor. We should really tie this to
945 // runtime.Type rather than copying it. This must match commonType in
946 // libgo/go/runtime/type.go.
949 Type::make_type_descriptor_type()
954 source_location bloc
= BUILTINS_LOCATION
;
956 Type
* uint8_type
= Type::lookup_integer_type("uint8");
957 Type
* uint32_type
= Type::lookup_integer_type("uint32");
958 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
959 Type
* string_type
= Type::lookup_string_type();
960 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
962 // This is an unnamed version of unsafe.Pointer. Perhaps we
963 // should use the named version instead, although that would
964 // require us to create the unsafe package if it has not been
965 // imported. It probably doesn't matter.
966 Type
* void_type
= Type::make_void_type();
967 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
969 // Forward declaration for the type descriptor type.
970 Named_object
* named_type_descriptor_type
=
971 Named_object::make_type_declaration("commonType", NULL
, bloc
);
972 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
973 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
975 // The type of a method on a concrete type.
976 Struct_type
* method_type
=
977 Type::make_builtin_struct_type(5,
978 "name", pointer_string_type
,
979 "pkgPath", pointer_string_type
,
980 "mtyp", pointer_type_descriptor_type
,
981 "typ", pointer_type_descriptor_type
,
982 "tfn", unsafe_pointer_type
);
983 Named_type
* named_method_type
=
984 Type::make_builtin_named_type("method", method_type
);
986 // Information for types with a name or methods.
987 Type
* slice_named_method_type
=
988 Type::make_array_type(named_method_type
, NULL
);
989 Struct_type
* uncommon_type
=
990 Type::make_builtin_struct_type(3,
991 "name", pointer_string_type
,
992 "pkgPath", pointer_string_type
,
993 "methods", slice_named_method_type
);
994 Named_type
* named_uncommon_type
=
995 Type::make_builtin_named_type("uncommonType", uncommon_type
);
997 Type
* pointer_uncommon_type
=
998 Type::make_pointer_type(named_uncommon_type
);
1000 // The type descriptor type.
1002 Typed_identifier_list
* params
= new Typed_identifier_list();
1003 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1004 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1006 Typed_identifier_list
* results
= new Typed_identifier_list();
1007 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1009 Type
* hashfn_type
= Type::make_function_type(NULL
, params
, results
, bloc
);
1011 params
= new Typed_identifier_list();
1012 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1013 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1014 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1016 results
= new Typed_identifier_list();
1017 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1019 Type
* equalfn_type
= Type::make_function_type(NULL
, params
, results
,
1022 Struct_type
* type_descriptor_type
=
1023 Type::make_builtin_struct_type(9,
1025 "align", uint8_type
,
1026 "fieldAlign", uint8_type
,
1027 "size", uintptr_type
,
1028 "hash", uint32_type
,
1029 "hashfn", hashfn_type
,
1030 "equalfn", equalfn_type
,
1031 "string", pointer_string_type
,
1032 "", pointer_uncommon_type
);
1034 Named_type
* named
= Type::make_builtin_named_type("commonType",
1035 type_descriptor_type
);
1037 named_type_descriptor_type
->set_type_value(named
);
1045 // Make the type of a pointer to a type descriptor as represented in
1049 Type::make_type_descriptor_ptr_type()
1053 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1057 // Return the names of runtime functions which compute a hash code for
1058 // this type and which compare whether two values of this type are
1062 Type::type_functions(const char** hash_fn
, const char** equal_fn
) const
1064 switch (this->base()->classification())
1066 case Type::TYPE_ERROR
:
1067 case Type::TYPE_VOID
:
1068 case Type::TYPE_NIL
:
1069 // These types can not be hashed or compared.
1070 *hash_fn
= "__go_type_hash_error";
1071 *equal_fn
= "__go_type_equal_error";
1074 case Type::TYPE_BOOLEAN
:
1075 case Type::TYPE_INTEGER
:
1076 case Type::TYPE_FLOAT
:
1077 case Type::TYPE_COMPLEX
:
1078 case Type::TYPE_POINTER
:
1079 case Type::TYPE_FUNCTION
:
1080 case Type::TYPE_MAP
:
1081 case Type::TYPE_CHANNEL
:
1082 *hash_fn
= "__go_type_hash_identity";
1083 *equal_fn
= "__go_type_equal_identity";
1086 case Type::TYPE_STRING
:
1087 *hash_fn
= "__go_type_hash_string";
1088 *equal_fn
= "__go_type_equal_string";
1091 case Type::TYPE_STRUCT
:
1092 case Type::TYPE_ARRAY
:
1093 // These types can not be hashed or compared.
1094 *hash_fn
= "__go_type_hash_error";
1095 *equal_fn
= "__go_type_equal_error";
1098 case Type::TYPE_INTERFACE
:
1099 if (this->interface_type()->is_empty())
1101 *hash_fn
= "__go_type_hash_empty_interface";
1102 *equal_fn
= "__go_type_equal_empty_interface";
1106 *hash_fn
= "__go_type_hash_interface";
1107 *equal_fn
= "__go_type_equal_interface";
1111 case Type::TYPE_NAMED
:
1112 case Type::TYPE_FORWARD
:
1120 // Return a composite literal for the type descriptor for a plain type
1121 // of kind RUNTIME_TYPE_KIND named NAME.
1124 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1125 Named_type
* name
, const Methods
* methods
,
1126 bool only_value_methods
)
1128 source_location bloc
= BUILTINS_LOCATION
;
1130 Type
* td_type
= Type::make_type_descriptor_type();
1131 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1133 Expression_list
* vals
= new Expression_list();
1136 Struct_field_list::const_iterator p
= fields
->begin();
1137 gcc_assert(p
->field_name() == "Kind");
1139 mpz_init_set_ui(iv
, runtime_type_kind
);
1140 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1143 gcc_assert(p
->field_name() == "align");
1144 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1145 vals
->push_back(Expression::make_type_info(this, type_info
));
1148 gcc_assert(p
->field_name() == "fieldAlign");
1149 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1150 vals
->push_back(Expression::make_type_info(this, type_info
));
1153 gcc_assert(p
->field_name() == "size");
1154 type_info
= Expression::TYPE_INFO_SIZE
;
1155 vals
->push_back(Expression::make_type_info(this, type_info
));
1158 gcc_assert(p
->field_name() == "hash");
1159 mpz_set_ui(iv
, this->hash_for_method(gogo
));
1160 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1162 const char* hash_fn
;
1163 const char* equal_fn
;
1164 this->type_functions(&hash_fn
, &equal_fn
);
1167 gcc_assert(p
->field_name() == "hashfn");
1168 Function_type
* fntype
= p
->type()->function_type();
1169 Named_object
* no
= Named_object::make_function_declaration(hash_fn
, NULL
,
1172 no
->func_declaration_value()->set_asm_name(hash_fn
);
1173 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1176 gcc_assert(p
->field_name() == "equalfn");
1177 fntype
= p
->type()->function_type();
1178 no
= Named_object::make_function_declaration(equal_fn
, NULL
, fntype
, bloc
);
1179 no
->func_declaration_value()->set_asm_name(equal_fn
);
1180 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1183 gcc_assert(p
->field_name() == "string");
1184 Expression
* s
= Expression::make_string((name
!= NULL
1185 ? name
->reflection(gogo
)
1186 : this->reflection(gogo
)),
1188 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1191 gcc_assert(p
->field_name() == "uncommonType");
1192 if (name
== NULL
&& methods
== NULL
)
1193 vals
->push_back(Expression::make_nil(bloc
));
1196 if (methods
== NULL
)
1197 methods
= name
->methods();
1198 vals
->push_back(this->uncommon_type_constructor(gogo
,
1201 only_value_methods
));
1205 gcc_assert(p
== fields
->end());
1209 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
1212 // Return a composite literal for the uncommon type information for
1213 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1214 // struct. If name is not NULL, it is the name of the type. If
1215 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1216 // is true if only value methods should be included. At least one of
1217 // NAME and METHODS must not be NULL.
1220 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
1221 Named_type
* name
, const Methods
* methods
,
1222 bool only_value_methods
) const
1224 source_location bloc
= BUILTINS_LOCATION
;
1226 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
1228 Expression_list
* vals
= new Expression_list();
1231 Struct_field_list::const_iterator p
= fields
->begin();
1232 gcc_assert(p
->field_name() == "name");
1235 gcc_assert(p
->field_name() == "pkgPath");
1239 vals
->push_back(Expression::make_nil(bloc
));
1240 vals
->push_back(Expression::make_nil(bloc
));
1244 Named_object
* no
= name
->named_object();
1245 std::string n
= Gogo::unpack_hidden_name(no
->name());
1246 Expression
* s
= Expression::make_string(n
, bloc
);
1247 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1249 if (name
->is_builtin())
1250 vals
->push_back(Expression::make_nil(bloc
));
1253 const Package
* package
= no
->package();
1254 const std::string
& unique_prefix(package
== NULL
1255 ? gogo
->unique_prefix()
1256 : package
->unique_prefix());
1257 const std::string
& package_name(package
== NULL
1258 ? gogo
->package_name()
1260 n
.assign(unique_prefix
);
1262 n
.append(package_name
);
1263 if (name
->in_function() != NULL
)
1266 n
.append(Gogo::unpack_hidden_name(name
->in_function()->name()));
1268 s
= Expression::make_string(n
, bloc
);
1269 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1274 gcc_assert(p
->field_name() == "methods");
1275 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
1276 only_value_methods
));
1279 gcc_assert(p
== fields
->end());
1281 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
1283 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
1286 // Sort methods by name.
1292 operator()(const std::pair
<std::string
, const Method
*>& m1
,
1293 const std::pair
<std::string
, const Method
*>& m2
) const
1294 { return m1
.first
< m2
.first
; }
1297 // Return a composite literal for the type method table for this type.
1298 // METHODS_TYPE is the type of the table, and is a slice type.
1299 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1300 // then only value methods are used.
1303 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
1304 const Methods
* methods
,
1305 bool only_value_methods
) const
1307 source_location bloc
= BUILTINS_LOCATION
;
1309 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
1310 if (methods
!= NULL
)
1312 smethods
.reserve(methods
->count());
1313 for (Methods::const_iterator p
= methods
->begin();
1314 p
!= methods
->end();
1317 if (p
->second
->is_ambiguous())
1319 if (only_value_methods
&& !p
->second
->is_value_method())
1321 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
1325 if (smethods
.empty())
1326 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
1328 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
1330 Type
* method_type
= methods_type
->array_type()->element_type();
1332 Expression_list
* vals
= new Expression_list();
1333 vals
->reserve(smethods
.size());
1334 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
1336 p
!= smethods
.end();
1338 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
1341 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
1344 // Return a composite literal for a single method. METHOD_TYPE is the
1345 // type of the entry. METHOD_NAME is the name of the method and M is
1346 // the method information.
1349 Type::method_constructor(Gogo
*, Type
* method_type
,
1350 const std::string
& method_name
,
1351 const Method
* m
) const
1353 source_location bloc
= BUILTINS_LOCATION
;
1355 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
1357 Expression_list
* vals
= new Expression_list();
1360 Struct_field_list::const_iterator p
= fields
->begin();
1361 gcc_assert(p
->field_name() == "name");
1362 const std::string n
= Gogo::unpack_hidden_name(method_name
);
1363 Expression
* s
= Expression::make_string(n
, bloc
);
1364 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1367 gcc_assert(p
->field_name() == "pkgPath");
1368 if (!Gogo::is_hidden_name(method_name
))
1369 vals
->push_back(Expression::make_nil(bloc
));
1372 s
= Expression::make_string(Gogo::hidden_name_prefix(method_name
), bloc
);
1373 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1376 Named_object
* no
= (m
->needs_stub_method()
1378 : m
->named_object());
1380 Function_type
* mtype
;
1381 if (no
->is_function())
1382 mtype
= no
->func_value()->type();
1384 mtype
= no
->func_declaration_value()->type();
1385 gcc_assert(mtype
->is_method());
1386 Type
* nonmethod_type
= mtype
->copy_without_receiver();
1389 gcc_assert(p
->field_name() == "mtyp");
1390 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
1393 gcc_assert(p
->field_name() == "typ");
1394 vals
->push_back(Expression::make_type_descriptor(mtype
, bloc
));
1397 gcc_assert(p
->field_name() == "tfn");
1398 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1401 gcc_assert(p
== fields
->end());
1403 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
1406 // Return a composite literal for the type descriptor of a plain type.
1407 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1408 // NULL, it is the name to use as well as the list of methods.
1411 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
1414 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
1418 // Return the type reflection string for this type.
1421 Type::reflection(Gogo
* gogo
) const
1425 // The do_reflection virtual function should set RET to the
1426 // reflection string.
1427 this->do_reflection(gogo
, &ret
);
1432 // Return a mangled name for the type.
1435 Type::mangled_name(Gogo
* gogo
) const
1439 // The do_mangled_name virtual function should set RET to the
1440 // mangled name. For a composite type it should append a code for
1441 // the composition and then call do_mangled_name on the components.
1442 this->do_mangled_name(gogo
, &ret
);
1447 // Default function to export a type.
1450 Type::do_export(Export
*) const
1458 Type::import_type(Import
* imp
)
1460 if (imp
->match_c_string("("))
1461 return Function_type::do_import(imp
);
1462 else if (imp
->match_c_string("*"))
1463 return Pointer_type::do_import(imp
);
1464 else if (imp
->match_c_string("struct "))
1465 return Struct_type::do_import(imp
);
1466 else if (imp
->match_c_string("["))
1467 return Array_type::do_import(imp
);
1468 else if (imp
->match_c_string("map "))
1469 return Map_type::do_import(imp
);
1470 else if (imp
->match_c_string("chan "))
1471 return Channel_type::do_import(imp
);
1472 else if (imp
->match_c_string("interface"))
1473 return Interface_type::do_import(imp
);
1476 error_at(imp
->location(), "import error: expected type");
1477 return Type::make_error_type();
1481 // A type used to indicate a parsing error. This exists to simplify
1482 // later error detection.
1484 class Error_type
: public Type
1494 { return error_mark_node
; }
1497 do_get_init_tree(Gogo
*, tree
, bool)
1498 { return error_mark_node
; }
1501 do_type_descriptor(Gogo
*, Named_type
*)
1502 { return Expression::make_error(BUILTINS_LOCATION
); }
1505 do_reflection(Gogo
*, std::string
*) const
1506 { gcc_assert(saw_errors()); }
1509 do_mangled_name(Gogo
*, std::string
* ret
) const
1510 { ret
->push_back('E'); }
1514 Type::make_error_type()
1516 static Error_type singleton_error_type
;
1517 return &singleton_error_type
;
1522 class Void_type
: public Type
1532 { return void_type_node
; }
1535 do_get_init_tree(Gogo
*, tree
, bool)
1536 { gcc_unreachable(); }
1539 do_type_descriptor(Gogo
*, Named_type
*)
1540 { gcc_unreachable(); }
1543 do_reflection(Gogo
*, std::string
*) const
1547 do_mangled_name(Gogo
*, std::string
* ret
) const
1548 { ret
->push_back('v'); }
1552 Type::make_void_type()
1554 static Void_type singleton_void_type
;
1555 return &singleton_void_type
;
1558 // The boolean type.
1560 class Boolean_type
: public Type
1564 : Type(TYPE_BOOLEAN
)
1570 { return boolean_type_node
; }
1573 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1574 { return is_clear
? NULL
: fold_convert(type_tree
, boolean_false_node
); }
1577 do_type_descriptor(Gogo
*, Named_type
* name
);
1579 // We should not be asked for the reflection string of a basic type.
1581 do_reflection(Gogo
*, std::string
* ret
) const
1582 { ret
->append("bool"); }
1585 do_mangled_name(Gogo
*, std::string
* ret
) const
1586 { ret
->push_back('b'); }
1589 // Make the type descriptor.
1592 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1595 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
1598 Named_object
* no
= gogo
->lookup_global("bool");
1599 gcc_assert(no
!= NULL
);
1600 return Type::type_descriptor(gogo
, no
->type_value());
1605 Type::make_boolean_type()
1607 static Boolean_type boolean_type
;
1608 return &boolean_type
;
1611 // The named type "bool".
1613 static Named_type
* named_bool_type
;
1615 // Get the named type "bool".
1618 Type::lookup_bool_type()
1620 return named_bool_type
;
1623 // Make the named type "bool".
1626 Type::make_named_bool_type()
1628 Type
* bool_type
= Type::make_boolean_type();
1629 Named_object
* named_object
= Named_object::make_type("bool", NULL
,
1632 Named_type
* named_type
= named_object
->type_value();
1633 named_bool_type
= named_type
;
1637 // Class Integer_type.
1639 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
1641 // Create a new integer type. Non-abstract integer types always have
1645 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
1646 int bits
, int runtime_type_kind
)
1648 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
1650 std::string
sname(name
);
1651 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1654 Named_type
* named_type
= named_object
->type_value();
1655 std::pair
<Named_integer_types::iterator
, bool> ins
=
1656 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
1657 gcc_assert(ins
.second
);
1661 // Look up an existing integer type.
1664 Integer_type::lookup_integer_type(const char* name
)
1666 Named_integer_types::const_iterator p
=
1667 Integer_type::named_integer_types
.find(name
);
1668 gcc_assert(p
!= Integer_type::named_integer_types
.end());
1672 // Create a new abstract integer type.
1675 Integer_type::create_abstract_integer_type()
1677 static Integer_type
* abstract_type
;
1678 if (abstract_type
== NULL
)
1679 abstract_type
= new Integer_type(true, false, INT_TYPE_SIZE
,
1680 RUNTIME_TYPE_KIND_INT
);
1681 return abstract_type
;
1684 // Integer type compatibility.
1687 Integer_type::is_identical(const Integer_type
* t
) const
1689 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
1691 return this->is_abstract_
== t
->is_abstract_
;
1697 Integer_type::do_hash_for_method(Gogo
*) const
1699 return ((this->bits_
<< 4)
1700 + ((this->is_unsigned_
? 1 : 0) << 8)
1701 + ((this->is_abstract_
? 1 : 0) << 9));
1704 // Get the tree for an Integer_type.
1707 Integer_type::do_get_tree(Gogo
*)
1709 gcc_assert(!this->is_abstract_
);
1710 if (this->is_unsigned_
)
1712 if (this->bits_
== INT_TYPE_SIZE
)
1713 return unsigned_type_node
;
1714 else if (this->bits_
== CHAR_TYPE_SIZE
)
1715 return unsigned_char_type_node
;
1716 else if (this->bits_
== SHORT_TYPE_SIZE
)
1717 return short_unsigned_type_node
;
1718 else if (this->bits_
== LONG_TYPE_SIZE
)
1719 return long_unsigned_type_node
;
1720 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1721 return long_long_unsigned_type_node
;
1723 return make_unsigned_type(this->bits_
);
1727 if (this->bits_
== INT_TYPE_SIZE
)
1728 return integer_type_node
;
1729 else if (this->bits_
== CHAR_TYPE_SIZE
)
1730 return signed_char_type_node
;
1731 else if (this->bits_
== SHORT_TYPE_SIZE
)
1732 return short_integer_type_node
;
1733 else if (this->bits_
== LONG_TYPE_SIZE
)
1734 return long_integer_type_node
;
1735 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1736 return long_long_integer_type_node
;
1738 return make_signed_type(this->bits_
);
1743 Integer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1745 return is_clear
? NULL
: build_int_cst(type_tree
, 0);
1748 // The type descriptor for an integer type. Integer types are always
1752 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1754 gcc_assert(name
!= NULL
);
1755 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1758 // We should not be asked for the reflection string of a basic type.
1761 Integer_type::do_reflection(Gogo
*, std::string
*) const
1769 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1772 snprintf(buf
, sizeof buf
, "i%s%s%de",
1773 this->is_abstract_
? "a" : "",
1774 this->is_unsigned_
? "u" : "",
1779 // Make an integer type.
1782 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
1783 int runtime_type_kind
)
1785 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
1789 // Make an abstract integer type.
1792 Type::make_abstract_integer_type()
1794 return Integer_type::create_abstract_integer_type();
1797 // Look up an integer type.
1800 Type::lookup_integer_type(const char* name
)
1802 return Integer_type::lookup_integer_type(name
);
1805 // Class Float_type.
1807 Float_type::Named_float_types
Float_type::named_float_types
;
1809 // Create a new float type. Non-abstract float types always have
1813 Float_type::create_float_type(const char* name
, int bits
,
1814 int runtime_type_kind
)
1816 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
1817 std::string
sname(name
);
1818 Named_object
* named_object
= Named_object::make_type(sname
, NULL
, float_type
,
1820 Named_type
* named_type
= named_object
->type_value();
1821 std::pair
<Named_float_types::iterator
, bool> ins
=
1822 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
1823 gcc_assert(ins
.second
);
1827 // Look up an existing float type.
1830 Float_type::lookup_float_type(const char* name
)
1832 Named_float_types::const_iterator p
=
1833 Float_type::named_float_types
.find(name
);
1834 gcc_assert(p
!= Float_type::named_float_types
.end());
1838 // Create a new abstract float type.
1841 Float_type::create_abstract_float_type()
1843 static Float_type
* abstract_type
;
1844 if (abstract_type
== NULL
)
1845 abstract_type
= new Float_type(true, FLOAT_TYPE_SIZE
,
1846 RUNTIME_TYPE_KIND_FLOAT
);
1847 return abstract_type
;
1850 // Whether this type is identical with T.
1853 Float_type::is_identical(const Float_type
* t
) const
1855 if (this->bits_
!= t
->bits_
)
1857 return this->is_abstract_
== t
->is_abstract_
;
1863 Float_type::do_hash_for_method(Gogo
*) const
1865 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
1868 // Get a tree without using a Gogo*.
1871 Float_type::type_tree() const
1873 if (this->bits_
== FLOAT_TYPE_SIZE
)
1874 return float_type_node
;
1875 else if (this->bits_
== DOUBLE_TYPE_SIZE
)
1876 return double_type_node
;
1877 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
)
1878 return long_double_type_node
;
1881 tree ret
= make_node(REAL_TYPE
);
1882 TYPE_PRECISION(ret
) = this->bits_
;
1891 Float_type::do_get_tree(Gogo
*)
1893 return this->type_tree();
1897 Float_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1902 real_from_integer(&r
, TYPE_MODE(type_tree
), 0, 0, 0);
1903 return build_real(type_tree
, r
);
1906 // The type descriptor for a float type. Float types are always named.
1909 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1911 gcc_assert(name
!= NULL
);
1912 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1915 // We should not be asked for the reflection string of a basic type.
1918 Float_type::do_reflection(Gogo
*, std::string
*) const
1926 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1929 snprintf(buf
, sizeof buf
, "f%s%de",
1930 this->is_abstract_
? "a" : "",
1935 // Make a floating point type.
1938 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
1940 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
1943 // Make an abstract float type.
1946 Type::make_abstract_float_type()
1948 return Float_type::create_abstract_float_type();
1951 // Look up a float type.
1954 Type::lookup_float_type(const char* name
)
1956 return Float_type::lookup_float_type(name
);
1959 // Class Complex_type.
1961 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
1963 // Create a new complex type. Non-abstract complex types always have
1967 Complex_type::create_complex_type(const char* name
, int bits
,
1968 int runtime_type_kind
)
1970 Complex_type
* complex_type
= new Complex_type(false, bits
,
1972 std::string
sname(name
);
1973 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1976 Named_type
* named_type
= named_object
->type_value();
1977 std::pair
<Named_complex_types::iterator
, bool> ins
=
1978 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
1980 gcc_assert(ins
.second
);
1984 // Look up an existing complex type.
1987 Complex_type::lookup_complex_type(const char* name
)
1989 Named_complex_types::const_iterator p
=
1990 Complex_type::named_complex_types
.find(name
);
1991 gcc_assert(p
!= Complex_type::named_complex_types
.end());
1995 // Create a new abstract complex type.
1998 Complex_type::create_abstract_complex_type()
2000 static Complex_type
* abstract_type
;
2001 if (abstract_type
== NULL
)
2002 abstract_type
= new Complex_type(true, FLOAT_TYPE_SIZE
* 2,
2003 RUNTIME_TYPE_KIND_FLOAT
);
2004 return abstract_type
;
2007 // Whether this type is identical with T.
2010 Complex_type::is_identical(const Complex_type
*t
) const
2012 if (this->bits_
!= t
->bits_
)
2014 return this->is_abstract_
== t
->is_abstract_
;
2020 Complex_type::do_hash_for_method(Gogo
*) const
2022 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2025 // Get a tree without using a Gogo*.
2028 Complex_type::type_tree() const
2030 if (this->bits_
== FLOAT_TYPE_SIZE
* 2)
2031 return complex_float_type_node
;
2032 else if (this->bits_
== DOUBLE_TYPE_SIZE
* 2)
2033 return complex_double_type_node
;
2034 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
* 2)
2035 return complex_long_double_type_node
;
2038 tree ret
= make_node(REAL_TYPE
);
2039 TYPE_PRECISION(ret
) = this->bits_
/ 2;
2041 return build_complex_type(ret
);
2048 Complex_type::do_get_tree(Gogo
*)
2050 return this->type_tree();
2053 // Zero initializer.
2056 Complex_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2061 real_from_integer(&r
, TYPE_MODE(TREE_TYPE(type_tree
)), 0, 0, 0);
2062 return build_complex(type_tree
, build_real(TREE_TYPE(type_tree
), r
),
2063 build_real(TREE_TYPE(type_tree
), r
));
2066 // The type descriptor for a complex type. Complex types are always
2070 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2072 gcc_assert(name
!= NULL
);
2073 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2076 // We should not be asked for the reflection string of a basic type.
2079 Complex_type::do_reflection(Gogo
*, std::string
*) const
2087 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2090 snprintf(buf
, sizeof buf
, "c%s%de",
2091 this->is_abstract_
? "a" : "",
2096 // Make a complex type.
2099 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
2101 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
2104 // Make an abstract complex type.
2107 Type::make_abstract_complex_type()
2109 return Complex_type::create_abstract_complex_type();
2112 // Look up a complex type.
2115 Type::lookup_complex_type(const char* name
)
2117 return Complex_type::lookup_complex_type(name
);
2120 // Class String_type.
2122 // Return the tree for String_type. A string is a struct with two
2123 // fields: a pointer to the characters and a length.
2126 String_type::do_get_tree(Gogo
*)
2128 static tree struct_type
;
2129 return Gogo::builtin_struct(&struct_type
, "__go_string", NULL_TREE
, 2,
2131 build_pointer_type(unsigned_char_type_node
),
2136 // Return a tree for the length of STRING.
2139 String_type::length_tree(Gogo
*, tree string
)
2141 tree string_type
= TREE_TYPE(string
);
2142 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2143 tree length_field
= DECL_CHAIN(TYPE_FIELDS(string_type
));
2144 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field
)),
2146 return fold_build3(COMPONENT_REF
, integer_type_node
, string
,
2147 length_field
, NULL_TREE
);
2150 // Return a tree for a pointer to the bytes of STRING.
2153 String_type::bytes_tree(Gogo
*, tree string
)
2155 tree string_type
= TREE_TYPE(string
);
2156 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2157 tree bytes_field
= TYPE_FIELDS(string_type
);
2158 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field
)),
2160 return fold_build3(COMPONENT_REF
, TREE_TYPE(bytes_field
), string
,
2161 bytes_field
, NULL_TREE
);
2164 // We initialize a string to { NULL, 0 }.
2167 String_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2172 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2174 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2176 for (tree field
= TYPE_FIELDS(type_tree
);
2178 field
= DECL_CHAIN(field
))
2180 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2182 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
2185 tree ret
= build_constructor(type_tree
, init
);
2186 TREE_CONSTANT(ret
) = 1;
2190 // The type descriptor for the string type.
2193 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2196 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
2199 Named_object
* no
= gogo
->lookup_global("string");
2200 gcc_assert(no
!= NULL
);
2201 return Type::type_descriptor(gogo
, no
->type_value());
2205 // We should not be asked for the reflection string of a basic type.
2208 String_type::do_reflection(Gogo
*, std::string
* ret
) const
2210 ret
->append("string");
2213 // Mangled name of a string type.
2216 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2218 ret
->push_back('z');
2221 // Make a string type.
2224 Type::make_string_type()
2226 static String_type string_type
;
2227 return &string_type
;
2230 // The named type "string".
2232 static Named_type
* named_string_type
;
2234 // Get the named type "string".
2237 Type::lookup_string_type()
2239 return named_string_type
;
2242 // Make the named type string.
2245 Type::make_named_string_type()
2247 Type
* string_type
= Type::make_string_type();
2248 Named_object
* named_object
= Named_object::make_type("string", NULL
,
2251 Named_type
* named_type
= named_object
->type_value();
2252 named_string_type
= named_type
;
2256 // The sink type. This is the type of the blank identifier _. Any
2257 // type may be assigned to it.
2259 class Sink_type
: public Type
2269 { gcc_unreachable(); }
2272 do_get_init_tree(Gogo
*, tree
, bool)
2273 { gcc_unreachable(); }
2276 do_type_descriptor(Gogo
*, Named_type
*)
2277 { gcc_unreachable(); }
2280 do_reflection(Gogo
*, std::string
*) const
2281 { gcc_unreachable(); }
2284 do_mangled_name(Gogo
*, std::string
*) const
2285 { gcc_unreachable(); }
2288 // Make the sink type.
2291 Type::make_sink_type()
2293 static Sink_type sink_type
;
2297 // Class Function_type.
2302 Function_type::do_traverse(Traverse
* traverse
)
2304 if (this->receiver_
!= NULL
2305 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
2306 return TRAVERSE_EXIT
;
2307 if (this->parameters_
!= NULL
2308 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
2309 return TRAVERSE_EXIT
;
2310 if (this->results_
!= NULL
2311 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
2312 return TRAVERSE_EXIT
;
2313 return TRAVERSE_CONTINUE
;
2316 // Returns whether T is a valid redeclaration of this type. If this
2317 // returns false, and REASON is not NULL, *REASON may be set to a
2318 // brief explanation of why it returned false.
2321 Function_type::is_valid_redeclaration(const Function_type
* t
,
2322 std::string
* reason
) const
2324 if (!this->is_identical(t
, false, reason
))
2327 // A redeclaration of a function is required to use the same names
2328 // for the receiver and parameters.
2329 if (this->receiver() != NULL
2330 && this->receiver()->name() != t
->receiver()->name()
2331 && this->receiver()->name() != Import::import_marker
2332 && t
->receiver()->name() != Import::import_marker
)
2335 *reason
= "receiver name changed";
2339 const Typed_identifier_list
* parms1
= this->parameters();
2340 const Typed_identifier_list
* parms2
= t
->parameters();
2343 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2344 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2345 p2
!= parms2
->end();
2348 if (p1
->name() != p2
->name()
2349 && p1
->name() != Import::import_marker
2350 && p2
->name() != Import::import_marker
)
2353 *reason
= "parameter name changed";
2357 // This is called at parse time, so we may have unknown
2359 Type
* t1
= p1
->type()->forwarded();
2360 Type
* t2
= p2
->type()->forwarded();
2362 && t1
->forward_declaration_type() != NULL
2363 && (t2
->forward_declaration_type() == NULL
2364 || (t1
->forward_declaration_type()->named_object()
2365 != t2
->forward_declaration_type()->named_object())))
2370 const Typed_identifier_list
* results1
= this->results();
2371 const Typed_identifier_list
* results2
= t
->results();
2372 if (results1
!= NULL
)
2374 Typed_identifier_list::const_iterator res1
= results1
->begin();
2375 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2376 res2
!= results2
->end();
2379 if (res1
->name() != res2
->name()
2380 && res1
->name() != Import::import_marker
2381 && res2
->name() != Import::import_marker
)
2384 *reason
= "result name changed";
2388 // This is called at parse time, so we may have unknown
2390 Type
* t1
= res1
->type()->forwarded();
2391 Type
* t2
= res2
->type()->forwarded();
2393 && t1
->forward_declaration_type() != NULL
2394 && (t2
->forward_declaration_type() == NULL
2395 || (t1
->forward_declaration_type()->named_object()
2396 != t2
->forward_declaration_type()->named_object())))
2404 // Check whether T is the same as this type.
2407 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
2408 std::string
* reason
) const
2410 if (!ignore_receiver
)
2412 const Typed_identifier
* r1
= this->receiver();
2413 const Typed_identifier
* r2
= t
->receiver();
2414 if ((r1
!= NULL
) != (r2
!= NULL
))
2417 *reason
= _("different receiver types");
2422 if (!Type::are_identical(r1
->type(), r2
->type(), reason
))
2424 if (reason
!= NULL
&& !reason
->empty())
2425 *reason
= "receiver: " + *reason
;
2431 const Typed_identifier_list
* parms1
= this->parameters();
2432 const Typed_identifier_list
* parms2
= t
->parameters();
2433 if ((parms1
!= NULL
) != (parms2
!= NULL
))
2436 *reason
= _("different number of parameters");
2441 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2442 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2443 p2
!= parms2
->end();
2446 if (p1
== parms1
->end())
2449 *reason
= _("different number of parameters");
2453 if (!Type::are_identical(p1
->type(), p2
->type(), NULL
))
2456 *reason
= _("different parameter types");
2460 if (p1
!= parms1
->end())
2463 *reason
= _("different number of parameters");
2468 if (this->is_varargs() != t
->is_varargs())
2471 *reason
= _("different varargs");
2475 const Typed_identifier_list
* results1
= this->results();
2476 const Typed_identifier_list
* results2
= t
->results();
2477 if ((results1
!= NULL
) != (results2
!= NULL
))
2480 *reason
= _("different number of results");
2483 if (results1
!= NULL
)
2485 Typed_identifier_list::const_iterator res1
= results1
->begin();
2486 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2487 res2
!= results2
->end();
2490 if (res1
== results1
->end())
2493 *reason
= _("different number of results");
2497 if (!Type::are_identical(res1
->type(), res2
->type(), NULL
))
2500 *reason
= _("different result types");
2504 if (res1
!= results1
->end())
2507 *reason
= _("different number of results");
2518 Function_type::do_hash_for_method(Gogo
* gogo
) const
2520 unsigned int ret
= 0;
2521 // We ignore the receiver type for hash codes, because we need to
2522 // get the same hash code for a method in an interface and a method
2523 // declared for a type. The former will not have a receiver.
2524 if (this->parameters_
!= NULL
)
2527 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2528 p
!= this->parameters_
->end();
2530 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2532 if (this->results_
!= NULL
)
2535 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2536 p
!= this->results_
->end();
2538 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2540 if (this->is_varargs_
)
2546 // Get the tree for a function type.
2549 Function_type::do_get_tree(Gogo
* gogo
)
2551 tree args
= NULL_TREE
;
2554 if (this->receiver_
!= NULL
)
2556 Type
* rtype
= this->receiver_
->type();
2557 tree ptype
= rtype
->get_tree(gogo
);
2558 if (ptype
== error_mark_node
)
2559 return error_mark_node
;
2561 // We always pass the address of the receiver parameter, in
2562 // order to make interface calls work with unknown types.
2563 if (rtype
->points_to() == NULL
)
2564 ptype
= build_pointer_type(ptype
);
2566 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2567 pp
= &TREE_CHAIN (*pp
);
2570 if (this->parameters_
!= NULL
)
2572 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2573 p
!= this->parameters_
->end();
2576 tree ptype
= p
->type()->get_tree(gogo
);
2577 if (ptype
== error_mark_node
)
2578 return error_mark_node
;
2579 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2580 pp
= &TREE_CHAIN (*pp
);
2584 // Varargs is handled entirely at the Go level. At the tree level,
2585 // functions are not varargs.
2586 *pp
= void_list_node
;
2589 if (this->results_
== NULL
)
2590 result
= void_type_node
;
2591 else if (this->results_
->size() == 1)
2592 result
= this->results_
->begin()->type()->get_tree(gogo
);
2595 result
= make_node(RECORD_TYPE
);
2596 tree field_trees
= NULL_TREE
;
2597 tree
* pp
= &field_trees
;
2598 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2599 p
!= this->results_
->end();
2602 const std::string name
= (p
->name().empty()
2604 : Gogo::unpack_hidden_name(p
->name()));
2605 tree name_tree
= get_identifier_with_length(name
.data(),
2607 tree field_type_tree
= p
->type()->get_tree(gogo
);
2608 if (field_type_tree
== error_mark_node
)
2609 return error_mark_node
;
2610 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
2612 DECL_CONTEXT(field
) = result
;
2614 pp
= &DECL_CHAIN(field
);
2616 TYPE_FIELDS(result
) = field_trees
;
2617 layout_type(result
);
2620 if (result
== error_mark_node
)
2621 return error_mark_node
;
2623 tree fntype
= build_function_type(result
, args
);
2624 if (fntype
== error_mark_node
)
2627 return build_pointer_type(fntype
);
2630 // Functions are initialized to NULL.
2633 Function_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2637 return fold_convert(type_tree
, null_pointer_node
);
2640 // The type of a function type descriptor.
2643 Function_type::make_function_type_descriptor_type()
2648 Type
* tdt
= Type::make_type_descriptor_type();
2649 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
2651 Type
* bool_type
= Type::lookup_bool_type();
2653 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
2655 Struct_type
* s
= Type::make_builtin_struct_type(4,
2657 "dotdotdot", bool_type
,
2661 ret
= Type::make_builtin_named_type("FuncType", s
);
2667 // The type descriptor for a function type.
2670 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2672 source_location bloc
= BUILTINS_LOCATION
;
2674 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
2676 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
2678 Expression_list
* vals
= new Expression_list();
2681 Struct_field_list::const_iterator p
= fields
->begin();
2682 gcc_assert(p
->field_name() == "commonType");
2683 vals
->push_back(this->type_descriptor_constructor(gogo
,
2684 RUNTIME_TYPE_KIND_FUNC
,
2688 gcc_assert(p
->field_name() == "dotdotdot");
2689 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
2692 gcc_assert(p
->field_name() == "in");
2693 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
2694 this->parameters()));
2697 gcc_assert(p
->field_name() == "out");
2698 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
2702 gcc_assert(p
== fields
->end());
2704 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
2707 // Return a composite literal for the parameters or results of a type
2711 Function_type::type_descriptor_params(Type
* params_type
,
2712 const Typed_identifier
* receiver
,
2713 const Typed_identifier_list
* params
)
2715 source_location bloc
= BUILTINS_LOCATION
;
2717 if (receiver
== NULL
&& params
== NULL
)
2718 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
2720 Expression_list
* vals
= new Expression_list();
2721 vals
->reserve((params
== NULL
? 0 : params
->size())
2722 + (receiver
!= NULL
? 1 : 0));
2724 if (receiver
!= NULL
)
2726 Type
* rtype
= receiver
->type();
2727 // The receiver is always passed as a pointer. FIXME: Is this
2728 // right? Should that fact affect the type descriptor?
2729 if (rtype
->points_to() == NULL
)
2730 rtype
= Type::make_pointer_type(rtype
);
2731 vals
->push_back(Expression::make_type_descriptor(rtype
, bloc
));
2736 for (Typed_identifier_list::const_iterator p
= params
->begin();
2739 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
2742 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
2745 // The reflection string.
2748 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
2750 // FIXME: Turn this off until we straighten out the type of the
2751 // struct field used in a go statement which calls a method.
2752 // gcc_assert(this->receiver_ == NULL);
2754 ret
->append("func");
2756 if (this->receiver_
!= NULL
)
2758 ret
->push_back('(');
2759 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
2760 ret
->push_back(')');
2763 ret
->push_back('(');
2764 const Typed_identifier_list
* params
= this->parameters();
2767 bool is_varargs
= this->is_varargs_
;
2768 for (Typed_identifier_list::const_iterator p
= params
->begin();
2772 if (p
!= params
->begin())
2774 if (!is_varargs
|| p
+ 1 != params
->end())
2775 this->append_reflection(p
->type(), gogo
, ret
);
2779 this->append_reflection(p
->type()->array_type()->element_type(),
2784 ret
->push_back(')');
2786 const Typed_identifier_list
* results
= this->results();
2787 if (results
!= NULL
&& !results
->empty())
2789 if (results
->size() == 1)
2790 ret
->push_back(' ');
2793 for (Typed_identifier_list::const_iterator p
= results
->begin();
2794 p
!= results
->end();
2797 if (p
!= results
->begin())
2799 this->append_reflection(p
->type(), gogo
, ret
);
2801 if (results
->size() > 1)
2802 ret
->push_back(')');
2809 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
2811 ret
->push_back('F');
2813 if (this->receiver_
!= NULL
)
2815 ret
->push_back('m');
2816 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
2819 const Typed_identifier_list
* params
= this->parameters();
2822 ret
->push_back('p');
2823 for (Typed_identifier_list::const_iterator p
= params
->begin();
2826 this->append_mangled_name(p
->type(), gogo
, ret
);
2827 if (this->is_varargs_
)
2828 ret
->push_back('V');
2829 ret
->push_back('e');
2832 const Typed_identifier_list
* results
= this->results();
2833 if (results
!= NULL
)
2835 ret
->push_back('r');
2836 for (Typed_identifier_list::const_iterator p
= results
->begin();
2837 p
!= results
->end();
2839 this->append_mangled_name(p
->type(), gogo
, ret
);
2840 ret
->push_back('e');
2843 ret
->push_back('e');
2846 // Export a function type.
2849 Function_type::do_export(Export
* exp
) const
2851 // We don't write out the receiver. The only function types which
2852 // should have a receiver are the ones associated with explicitly
2853 // defined methods. For those the receiver type is written out by
2854 // Function::export_func.
2856 exp
->write_c_string("(");
2858 if (this->parameters_
!= NULL
)
2860 bool is_varargs
= this->is_varargs_
;
2861 for (Typed_identifier_list::const_iterator p
=
2862 this->parameters_
->begin();
2863 p
!= this->parameters_
->end();
2869 exp
->write_c_string(", ");
2870 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
2871 exp
->write_type(p
->type());
2874 exp
->write_c_string("...");
2875 exp
->write_type(p
->type()->array_type()->element_type());
2879 exp
->write_c_string(")");
2881 const Typed_identifier_list
* results
= this->results_
;
2882 if (results
!= NULL
)
2884 exp
->write_c_string(" ");
2885 if (results
->size() == 1)
2886 exp
->write_type(results
->begin()->type());
2890 exp
->write_c_string("(");
2891 for (Typed_identifier_list::const_iterator p
= results
->begin();
2892 p
!= results
->end();
2898 exp
->write_c_string(", ");
2899 exp
->write_type(p
->type());
2901 exp
->write_c_string(")");
2906 // Import a function type.
2909 Function_type::do_import(Import
* imp
)
2911 imp
->require_c_string("(");
2912 Typed_identifier_list
* parameters
;
2913 bool is_varargs
= false;
2914 if (imp
->peek_char() == ')')
2918 parameters
= new Typed_identifier_list();
2921 if (imp
->match_c_string("..."))
2927 Type
* ptype
= imp
->read_type();
2929 ptype
= Type::make_array_type(ptype
, NULL
);
2930 parameters
->push_back(Typed_identifier(Import::import_marker
,
2931 ptype
, imp
->location()));
2932 if (imp
->peek_char() != ',')
2934 gcc_assert(!is_varargs
);
2935 imp
->require_c_string(", ");
2938 imp
->require_c_string(")");
2940 Typed_identifier_list
* results
;
2941 if (imp
->peek_char() != ' ')
2946 results
= new Typed_identifier_list
;
2947 if (imp
->peek_char() != '(')
2949 Type
* rtype
= imp
->read_type();
2950 results
->push_back(Typed_identifier(Import::import_marker
, rtype
,
2958 Type
* rtype
= imp
->read_type();
2959 results
->push_back(Typed_identifier(Import::import_marker
,
2960 rtype
, imp
->location()));
2961 if (imp
->peek_char() != ',')
2963 imp
->require_c_string(", ");
2965 imp
->require_c_string(")");
2969 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
2972 ret
->set_is_varargs();
2976 // Make a copy of a function type without a receiver.
2979 Function_type::copy_without_receiver() const
2981 gcc_assert(this->is_method());
2982 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
2985 if (this->is_varargs())
2986 ret
->set_is_varargs();
2987 if (this->is_builtin())
2988 ret
->set_is_builtin();
2992 // Make a copy of a function type with a receiver.
2995 Function_type::copy_with_receiver(Type
* receiver_type
) const
2997 gcc_assert(!this->is_method());
2998 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
3000 return Type::make_function_type(receiver
, this->parameters_
,
3001 this->results_
, this->location_
);
3004 // Make a function type.
3007 Type::make_function_type(Typed_identifier
* receiver
,
3008 Typed_identifier_list
* parameters
,
3009 Typed_identifier_list
* results
,
3010 source_location location
)
3012 return new Function_type(receiver
, parameters
, results
, location
);
3015 // Class Pointer_type.
3020 Pointer_type::do_traverse(Traverse
* traverse
)
3022 return Type::traverse(this->to_type_
, traverse
);
3028 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
3030 return this->to_type_
->hash_for_method(gogo
) << 4;
3033 // The tree for a pointer type.
3036 Pointer_type::do_get_tree(Gogo
* gogo
)
3038 return build_pointer_type(this->to_type_
->get_tree(gogo
));
3041 // Initialize a pointer type.
3044 Pointer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3048 return fold_convert(type_tree
, null_pointer_node
);
3051 // The type of a pointer type descriptor.
3054 Pointer_type::make_pointer_type_descriptor_type()
3059 Type
* tdt
= Type::make_type_descriptor_type();
3060 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3062 Struct_type
* s
= Type::make_builtin_struct_type(2,
3066 ret
= Type::make_builtin_named_type("PtrType", s
);
3072 // The type descriptor for a pointer type.
3075 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3077 if (this->is_unsafe_pointer_type())
3079 gcc_assert(name
!= NULL
);
3080 return this->plain_type_descriptor(gogo
,
3081 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
3086 source_location bloc
= BUILTINS_LOCATION
;
3088 const Methods
* methods
;
3089 Type
* deref
= this->points_to();
3090 if (deref
->named_type() != NULL
)
3091 methods
= deref
->named_type()->methods();
3092 else if (deref
->struct_type() != NULL
)
3093 methods
= deref
->struct_type()->methods();
3097 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
3099 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
3101 Expression_list
* vals
= new Expression_list();
3104 Struct_field_list::const_iterator p
= fields
->begin();
3105 gcc_assert(p
->field_name() == "commonType");
3106 vals
->push_back(this->type_descriptor_constructor(gogo
,
3107 RUNTIME_TYPE_KIND_PTR
,
3108 name
, methods
, false));
3111 gcc_assert(p
->field_name() == "elem");
3112 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
3114 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
3118 // Reflection string.
3121 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3123 ret
->push_back('*');
3124 this->append_reflection(this->to_type_
, gogo
, ret
);
3130 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3132 ret
->push_back('p');
3133 this->append_mangled_name(this->to_type_
, gogo
, ret
);
3139 Pointer_type::do_export(Export
* exp
) const
3141 exp
->write_c_string("*");
3142 if (this->is_unsafe_pointer_type())
3143 exp
->write_c_string("any");
3145 exp
->write_type(this->to_type_
);
3151 Pointer_type::do_import(Import
* imp
)
3153 imp
->require_c_string("*");
3154 if (imp
->match_c_string("any"))
3157 return Type::make_pointer_type(Type::make_void_type());
3159 Type
* to
= imp
->read_type();
3160 return Type::make_pointer_type(to
);
3163 // Make a pointer type.
3166 Type::make_pointer_type(Type
* to_type
)
3168 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
3169 static Hashtable pointer_types
;
3170 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
3171 if (p
!= pointer_types
.end())
3173 Pointer_type
* ret
= new Pointer_type(to_type
);
3174 pointer_types
[to_type
] = ret
;
3178 // The nil type. We use a special type for nil because it is not the
3179 // same as any other type. In C term nil has type void*, but there is
3180 // no such type in Go.
3182 class Nil_type
: public Type
3192 { return ptr_type_node
; }
3195 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3196 { return is_clear
? NULL
: fold_convert(type_tree
, null_pointer_node
); }
3199 do_type_descriptor(Gogo
*, Named_type
*)
3200 { gcc_unreachable(); }
3203 do_reflection(Gogo
*, std::string
*) const
3204 { gcc_unreachable(); }
3207 do_mangled_name(Gogo
*, std::string
* ret
) const
3208 { ret
->push_back('n'); }
3211 // Make the nil type.
3214 Type::make_nil_type()
3216 static Nil_type singleton_nil_type
;
3217 return &singleton_nil_type
;
3220 // The type of a function call which returns multiple values. This is
3221 // really a struct, but we don't want to confuse a function call which
3222 // returns a struct with a function call which returns multiple
3225 class Call_multiple_result_type
: public Type
3228 Call_multiple_result_type(Call_expression
* call
)
3229 : Type(TYPE_CALL_MULTIPLE_RESULT
),
3235 do_has_pointer() const
3236 { gcc_unreachable(); }
3242 do_get_init_tree(Gogo
*, tree
, bool)
3243 { gcc_unreachable(); }
3246 do_type_descriptor(Gogo
*, Named_type
*)
3247 { gcc_unreachable(); }
3250 do_reflection(Gogo
*, std::string
*) const
3251 { gcc_unreachable(); }
3254 do_mangled_name(Gogo
*, std::string
*) const
3255 { gcc_unreachable(); }
3258 // The expression being called.
3259 Call_expression
* call_
;
3262 // Return the tree for a call result.
3265 Call_multiple_result_type::do_get_tree(Gogo
* gogo
)
3267 Function_type
* fntype
= this->call_
->get_function_type();
3268 gcc_assert(fntype
!= NULL
);
3269 const Typed_identifier_list
* results
= fntype
->results();
3270 gcc_assert(results
!= NULL
&& results
->size() > 1);
3272 Struct_field_list
* sfl
= new Struct_field_list
;
3273 for (Typed_identifier_list::const_iterator p
= results
->begin();
3274 p
!= results
->end();
3277 const std::string name
= ((p
->name().empty()
3278 || p
->name() == Import::import_marker
)
3281 sfl
->push_back(Struct_field(Typed_identifier(name
, p
->type(),
3282 this->call_
->location())));
3284 return Type::make_struct_type(sfl
, this->call_
->location())->get_tree(gogo
);
3287 // Make a call result type.
3290 Type::make_call_multiple_result_type(Call_expression
* call
)
3292 return new Call_multiple_result_type(call
);
3295 // Class Struct_field.
3297 // Get the name of a field.
3300 Struct_field::field_name() const
3302 const std::string
& name(this->typed_identifier_
.name());
3307 // This is called during parsing, before anything is lowered, so
3308 // we have to be pretty careful to avoid dereferencing an
3309 // unknown type name.
3310 Type
* t
= this->typed_identifier_
.type();
3312 if (t
->classification() == Type::TYPE_POINTER
)
3315 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
3316 dt
= ptype
->points_to();
3318 if (dt
->forward_declaration_type() != NULL
)
3319 return dt
->forward_declaration_type()->name();
3320 else if (dt
->named_type() != NULL
)
3321 return dt
->named_type()->name();
3322 else if (t
->is_error_type() || dt
->is_error_type())
3324 static const std::string error_string
= "*error*";
3325 return error_string
;
3329 // Avoid crashing in the erroneous case where T is named but
3331 gcc_assert(t
!= dt
);
3332 if (t
->forward_declaration_type() != NULL
)
3333 return t
->forward_declaration_type()->name();
3334 else if (t
->named_type() != NULL
)
3335 return t
->named_type()->name();
3342 // Class Struct_type.
3347 Struct_type::do_traverse(Traverse
* traverse
)
3349 Struct_field_list
* fields
= this->fields_
;
3352 for (Struct_field_list::iterator p
= fields
->begin();
3356 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
3357 return TRAVERSE_EXIT
;
3360 return TRAVERSE_CONTINUE
;
3363 // Verify that the struct type is complete and valid.
3366 Struct_type::do_verify()
3368 Struct_field_list
* fields
= this->fields_
;
3371 for (Struct_field_list::iterator p
= fields
->begin();
3375 Type
* t
= p
->type();
3376 if (t
->is_undefined())
3378 error_at(p
->location(), "struct field type is incomplete");
3379 p
->set_type(Type::make_error_type());
3382 else if (p
->is_anonymous())
3384 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
3386 error_at(p
->location(), "embedded type may not be a pointer");
3387 p
->set_type(Type::make_error_type());
3395 // Whether this contains a pointer.
3398 Struct_type::do_has_pointer() const
3400 const Struct_field_list
* fields
= this->fields();
3403 for (Struct_field_list::const_iterator p
= fields
->begin();
3407 if (p
->type()->has_pointer())
3413 // Whether this type is identical to T.
3416 Struct_type::is_identical(const Struct_type
* t
) const
3418 const Struct_field_list
* fields1
= this->fields();
3419 const Struct_field_list
* fields2
= t
->fields();
3420 if (fields1
== NULL
|| fields2
== NULL
)
3421 return fields1
== fields2
;
3422 Struct_field_list::const_iterator pf2
= fields2
->begin();
3423 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
3424 pf1
!= fields1
->end();
3427 if (pf2
== fields2
->end())
3429 if (pf1
->field_name() != pf2
->field_name())
3431 if (pf1
->is_anonymous() != pf2
->is_anonymous()
3432 || !Type::are_identical(pf1
->type(), pf2
->type(), NULL
))
3434 if (!pf1
->has_tag())
3441 if (!pf2
->has_tag())
3443 if (pf1
->tag() != pf2
->tag())
3447 if (pf2
!= fields2
->end())
3452 // Whether this struct type has any hidden fields.
3455 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
3456 std::string
* reason
) const
3458 const Struct_field_list
* fields
= this->fields();
3461 const Package
* within_package
= (within
== NULL
3463 : within
->named_object()->package());
3464 for (Struct_field_list::const_iterator pf
= fields
->begin();
3465 pf
!= fields
->end();
3468 if (within_package
!= NULL
3469 && !pf
->is_anonymous()
3470 && Gogo::is_hidden_name(pf
->field_name()))
3474 std::string within_name
= within
->named_object()->message_name();
3475 std::string name
= Gogo::message_name(pf
->field_name());
3476 size_t bufsize
= 200 + within_name
.length() + name
.length();
3477 char* buf
= new char[bufsize
];
3478 snprintf(buf
, bufsize
,
3479 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3480 open_quote
, within_name
.c_str(), close_quote
,
3481 open_quote
, name
.c_str(), close_quote
);
3482 reason
->assign(buf
);
3488 if (pf
->type()->has_hidden_fields(within
, reason
))
3498 Struct_type::do_hash_for_method(Gogo
* gogo
) const
3500 unsigned int ret
= 0;
3501 if (this->fields() != NULL
)
3503 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
3504 pf
!= this->fields()->end();
3506 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
3511 // Find the local field NAME.
3514 Struct_type::find_local_field(const std::string
& name
,
3515 unsigned int *pindex
) const
3517 const Struct_field_list
* fields
= this->fields_
;
3521 for (Struct_field_list::const_iterator pf
= fields
->begin();
3522 pf
!= fields
->end();
3525 if (pf
->field_name() == name
)
3535 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3537 Field_reference_expression
*
3538 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
3539 source_location location
) const
3542 return this->field_reference_depth(struct_expr
, name
, location
, &depth
);
3545 // Return an expression for a field, along with the depth at which it
3548 Field_reference_expression
*
3549 Struct_type::field_reference_depth(Expression
* struct_expr
,
3550 const std::string
& name
,
3551 source_location location
,
3552 unsigned int* depth
) const
3554 const Struct_field_list
* fields
= this->fields_
;
3558 // Look for a field with this name.
3560 for (Struct_field_list::const_iterator pf
= fields
->begin();
3561 pf
!= fields
->end();
3564 if (pf
->field_name() == name
)
3567 return Expression::make_field_reference(struct_expr
, i
, location
);
3571 // Look for an anonymous field which contains a field with this
3573 unsigned int found_depth
= 0;
3574 Field_reference_expression
* ret
= NULL
;
3576 for (Struct_field_list::const_iterator pf
= fields
->begin();
3577 pf
!= fields
->end();
3580 if (!pf
->is_anonymous())
3583 Struct_type
* st
= pf
->type()->deref()->struct_type();
3587 // Look for a reference using a NULL struct expression. If we
3588 // find one, fill in the struct expression with a reference to
3590 unsigned int subdepth
;
3591 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
3597 if (ret
== NULL
|| subdepth
< found_depth
)
3602 found_depth
= subdepth
;
3603 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
3605 if (pf
->type()->points_to() != NULL
)
3606 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
3607 while (sub
->expr() != NULL
)
3609 sub
= sub
->expr()->deref()->field_reference_expression();
3610 gcc_assert(sub
!= NULL
);
3612 sub
->set_struct_expression(here
);
3614 else if (subdepth
> found_depth
)
3618 // We do not handle ambiguity here--it should be handled by
3619 // Type::bind_field_or_method.
3627 *depth
= found_depth
+ 1;
3632 // Return the total number of fields, including embedded fields.
3635 Struct_type::total_field_count() const
3637 if (this->fields_
== NULL
)
3639 unsigned int ret
= 0;
3640 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3641 pf
!= this->fields_
->end();
3644 if (!pf
->is_anonymous() || pf
->type()->deref()->struct_type() == NULL
)
3647 ret
+= pf
->type()->struct_type()->total_field_count();
3652 // Return whether NAME is an unexported field, for better error reporting.
3655 Struct_type::is_unexported_local_field(Gogo
* gogo
,
3656 const std::string
& name
) const
3658 const Struct_field_list
* fields
= this->fields_
;
3661 for (Struct_field_list::const_iterator pf
= fields
->begin();
3662 pf
!= fields
->end();
3665 const std::string
& field_name(pf
->field_name());
3666 if (Gogo::is_hidden_name(field_name
)
3667 && name
== Gogo::unpack_hidden_name(field_name
)
3668 && gogo
->pack_hidden_name(name
, false) != field_name
)
3675 // Finalize the methods of an unnamed struct.
3678 Struct_type::finalize_methods(Gogo
* gogo
)
3680 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
3683 // Return the method NAME, or NULL if there isn't one or if it is
3684 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3688 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
3690 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
3693 // Get the tree for a struct type.
3696 Struct_type::do_get_tree(Gogo
* gogo
)
3698 tree type
= make_node(RECORD_TYPE
);
3699 return this->fill_in_tree(gogo
, type
);
3702 // Fill in the fields for a struct type.
3705 Struct_type::fill_in_tree(Gogo
* gogo
, tree type
)
3707 tree field_trees
= NULL_TREE
;
3708 tree
* pp
= &field_trees
;
3709 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3710 p
!= this->fields_
->end();
3713 std::string name
= Gogo::unpack_hidden_name(p
->field_name());
3714 tree name_tree
= get_identifier_with_length(name
.data(), name
.length());
3715 tree field_type_tree
= p
->type()->get_tree(gogo
);
3716 if (field_type_tree
== error_mark_node
)
3717 return error_mark_node
;
3718 tree field
= build_decl(p
->location(), FIELD_DECL
, name_tree
,
3720 DECL_CONTEXT(field
) = type
;
3722 pp
= &DECL_CHAIN(field
);
3725 TYPE_FIELDS(type
) = field_trees
;
3732 // Initialize struct fields.
3735 Struct_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
3737 if (this->fields_
== NULL
|| this->fields_
->empty())
3743 tree ret
= build_constructor(type_tree
,
3744 VEC_alloc(constructor_elt
, gc
, 0));
3745 TREE_CONSTANT(ret
) = 1;
3750 bool is_constant
= true;
3751 bool any_fields_set
= false;
3752 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
,
3753 this->fields_
->size());
3754 Struct_field_list::const_iterator p
= this->fields_
->begin();
3755 for (tree field
= TYPE_FIELDS(type_tree
);
3757 field
= DECL_CHAIN(field
), ++p
)
3759 gcc_assert(p
!= this->fields_
->end());
3760 tree value
= p
->type()->get_init_tree(gogo
, is_clear
);
3763 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
3766 any_fields_set
= true;
3767 if (!TREE_CONSTANT(value
))
3768 is_constant
= false;
3771 gcc_assert(p
== this->fields_
->end());
3773 if (!any_fields_set
)
3775 gcc_assert(is_clear
);
3776 VEC_free(constructor_elt
, gc
, init
);
3780 tree ret
= build_constructor(type_tree
, init
);
3782 TREE_CONSTANT(ret
) = 1;
3786 // The type of a struct type descriptor.
3789 Struct_type::make_struct_type_descriptor_type()
3794 Type
* tdt
= Type::make_type_descriptor_type();
3795 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3797 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3798 Type
* string_type
= Type::lookup_string_type();
3799 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
3802 Type::make_builtin_struct_type(5,
3803 "name", pointer_string_type
,
3804 "pkgPath", pointer_string_type
,
3806 "tag", pointer_string_type
,
3807 "offset", uintptr_type
);
3808 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
3810 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
3812 Struct_type
* s
= Type::make_builtin_struct_type(2,
3814 "fields", slice_type
);
3816 ret
= Type::make_builtin_named_type("StructType", s
);
3822 // Build a type descriptor for a struct type.
3825 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3827 source_location bloc
= BUILTINS_LOCATION
;
3829 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
3831 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
3833 Expression_list
* vals
= new Expression_list();
3836 const Methods
* methods
= this->methods();
3837 // A named struct should not have methods--the methods should attach
3838 // to the named type.
3839 gcc_assert(methods
== NULL
|| name
== NULL
);
3841 Struct_field_list::const_iterator ps
= fields
->begin();
3842 gcc_assert(ps
->field_name() == "commonType");
3843 vals
->push_back(this->type_descriptor_constructor(gogo
,
3844 RUNTIME_TYPE_KIND_STRUCT
,
3845 name
, methods
, true));
3848 gcc_assert(ps
->field_name() == "fields");
3850 Expression_list
* elements
= new Expression_list();
3851 elements
->reserve(this->fields_
->size());
3852 Type
* element_type
= ps
->type()->array_type()->element_type();
3853 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3854 pf
!= this->fields_
->end();
3857 const Struct_field_list
* f
= element_type
->struct_type()->fields();
3859 Expression_list
* fvals
= new Expression_list();
3862 Struct_field_list::const_iterator q
= f
->begin();
3863 gcc_assert(q
->field_name() == "name");
3864 if (pf
->is_anonymous())
3865 fvals
->push_back(Expression::make_nil(bloc
));
3868 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
3869 Expression
* s
= Expression::make_string(n
, bloc
);
3870 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3874 gcc_assert(q
->field_name() == "pkgPath");
3875 if (!Gogo::is_hidden_name(pf
->field_name()))
3876 fvals
->push_back(Expression::make_nil(bloc
));
3879 std::string n
= Gogo::hidden_name_prefix(pf
->field_name());
3880 Expression
* s
= Expression::make_string(n
, bloc
);
3881 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3885 gcc_assert(q
->field_name() == "typ");
3886 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
3889 gcc_assert(q
->field_name() == "tag");
3891 fvals
->push_back(Expression::make_nil(bloc
));
3894 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
3895 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3899 gcc_assert(q
->field_name() == "offset");
3900 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
3902 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
3904 elements
->push_back(v
);
3907 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
3910 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
3913 // Reflection string.
3916 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3918 ret
->append("struct { ");
3920 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3921 p
!= this->fields_
->end();
3924 if (p
!= this->fields_
->begin())
3926 if (p
->is_anonymous())
3927 ret
->push_back('?');
3929 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
3930 ret
->push_back(' ');
3931 this->append_reflection(p
->type(), gogo
, ret
);
3935 const std::string
& tag(p
->tag());
3937 for (std::string::const_iterator p
= tag
.begin();
3942 ret
->append("\\x00");
3943 else if (*p
== '\n')
3945 else if (*p
== '\t')
3948 ret
->append("\\\"");
3949 else if (*p
== '\\')
3950 ret
->append("\\\\");
3954 ret
->push_back('"');
3964 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3966 ret
->push_back('S');
3968 const Struct_field_list
* fields
= this->fields_
;
3971 for (Struct_field_list::const_iterator p
= fields
->begin();
3975 if (p
->is_anonymous())
3979 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
3981 snprintf(buf
, sizeof buf
, "%u_",
3982 static_cast<unsigned int>(n
.length()));
3986 this->append_mangled_name(p
->type(), gogo
, ret
);
3989 const std::string
& tag(p
->tag());
3991 for (std::string::const_iterator p
= tag
.begin();
3995 if (ISALNUM(*p
) || *p
== '_')
4000 snprintf(buf
, sizeof buf
, ".%x.",
4001 static_cast<unsigned int>(*p
));
4006 snprintf(buf
, sizeof buf
, "T%u_",
4007 static_cast<unsigned int>(out
.length()));
4014 ret
->push_back('e');
4020 Struct_type::do_export(Export
* exp
) const
4022 exp
->write_c_string("struct { ");
4023 const Struct_field_list
* fields
= this->fields_
;
4024 gcc_assert(fields
!= NULL
);
4025 for (Struct_field_list::const_iterator p
= fields
->begin();
4029 if (p
->is_anonymous())
4030 exp
->write_string("? ");
4033 exp
->write_string(p
->field_name());
4034 exp
->write_c_string(" ");
4036 exp
->write_type(p
->type());
4040 exp
->write_c_string(" ");
4041 Expression
* expr
= Expression::make_string(p
->tag(),
4043 expr
->export_expression(exp
);
4047 exp
->write_c_string("; ");
4049 exp
->write_c_string("}");
4055 Struct_type::do_import(Import
* imp
)
4057 imp
->require_c_string("struct { ");
4058 Struct_field_list
* fields
= new Struct_field_list
;
4059 if (imp
->peek_char() != '}')
4064 if (imp
->match_c_string("? "))
4068 name
= imp
->read_identifier();
4069 imp
->require_c_string(" ");
4071 Type
* ftype
= imp
->read_type();
4073 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
4075 if (imp
->peek_char() == ' ')
4078 Expression
* expr
= Expression::import_expression(imp
);
4079 String_expression
* sexpr
= expr
->string_expression();
4080 gcc_assert(sexpr
!= NULL
);
4081 sf
.set_tag(sexpr
->val());
4085 imp
->require_c_string("; ");
4086 fields
->push_back(sf
);
4087 if (imp
->peek_char() == '}')
4091 imp
->require_c_string("}");
4093 return Type::make_struct_type(fields
, imp
->location());
4096 // Make a struct type.
4099 Type::make_struct_type(Struct_field_list
* fields
,
4100 source_location location
)
4102 return new Struct_type(fields
, location
);
4105 // Class Array_type.
4107 // Whether two array types are identical.
4110 Array_type::is_identical(const Array_type
* t
) const
4112 if (!Type::are_identical(this->element_type(), t
->element_type(), NULL
))
4115 Expression
* l1
= this->length();
4116 Expression
* l2
= t
->length();
4118 // Slices of the same element type are identical.
4119 if (l1
== NULL
&& l2
== NULL
)
4122 // Arrays of the same element type are identical if they have the
4124 if (l1
!= NULL
&& l2
!= NULL
)
4129 // Try to determine the lengths. If we can't, assume the arrays
4130 // are not identical.
4138 if (l1
->integer_constant_value(true, v1
, &type1
)
4139 && l2
->integer_constant_value(true, v2
, &type2
))
4140 ret
= mpz_cmp(v1
, v2
) == 0;
4146 // Otherwise the arrays are not identical.
4153 Array_type::do_traverse(Traverse
* traverse
)
4155 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
4156 return TRAVERSE_EXIT
;
4157 if (this->length_
!= NULL
4158 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
4159 return TRAVERSE_EXIT
;
4160 return TRAVERSE_CONTINUE
;
4163 // Check that the length is valid.
4166 Array_type::verify_length()
4168 if (this->length_
== NULL
)
4170 if (!this->length_
->is_constant())
4172 error_at(this->length_
->location(), "array bound is not constant");
4178 Type
* t
= this->length_
->type();
4179 if (t
->integer_type() != NULL
)
4183 if (!this->length_
->integer_constant_value(true, val
, &vt
))
4185 error_at(this->length_
->location(),
4186 "array bound is not constant");
4191 else if (t
->float_type() != NULL
)
4196 if (!this->length_
->float_constant_value(fval
, &vt
))
4198 error_at(this->length_
->location(),
4199 "array bound is not constant");
4203 if (!mpfr_integer_p(fval
))
4205 error_at(this->length_
->location(),
4206 "array bound truncated to integer");
4211 mpfr_get_z(val
, fval
, GMP_RNDN
);
4216 if (!t
->is_error_type())
4217 error_at(this->length_
->location(), "array bound is not numeric");
4221 if (mpz_sgn(val
) < 0)
4223 error_at(this->length_
->location(), "negative array bound");
4228 Type
* int_type
= Type::lookup_integer_type("int");
4229 int tbits
= int_type
->integer_type()->bits();
4230 int vbits
= mpz_sizeinbase(val
, 2);
4231 if (vbits
+ 1 > tbits
)
4233 error_at(this->length_
->location(), "array bound overflows");
4246 Array_type::do_verify()
4248 if (!this->verify_length())
4250 this->length_
= Expression::make_error(this->length_
->location());
4256 // Array type hash code.
4259 Array_type::do_hash_for_method(Gogo
* gogo
) const
4261 // There is no very convenient way to get a hash code for the
4263 return this->element_type_
->hash_for_method(gogo
) + 1;
4266 // See if the expression passed to make is suitable. The first
4267 // argument is required, and gives the length. An optional second
4268 // argument is permitted for the capacity.
4271 Array_type::do_check_make_expression(Expression_list
* args
,
4272 source_location location
)
4274 gcc_assert(this->length_
== NULL
);
4275 if (args
== NULL
|| args
->empty())
4277 error_at(location
, "length required when allocating a slice");
4280 else if (args
->size() > 2)
4282 error_at(location
, "too many expressions passed to make");
4287 if (!Type::check_int_value(args
->front(),
4288 _("bad length when making slice"), location
))
4291 if (args
->size() > 1)
4293 if (!Type::check_int_value(args
->back(),
4294 _("bad capacity when making slice"),
4303 // Get a tree for the length of a fixed array. The length may be
4304 // computed using a function call, so we must only evaluate it once.
4307 Array_type::get_length_tree(Gogo
* gogo
)
4309 gcc_assert(this->length_
!= NULL
);
4310 if (this->length_tree_
== NULL_TREE
)
4315 if (this->length_
->integer_constant_value(true, val
, &t
))
4318 t
= Type::lookup_integer_type("int");
4319 else if (t
->is_abstract())
4320 t
= t
->make_non_abstract_type();
4321 tree tt
= t
->get_tree(gogo
);
4322 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
4329 // Make up a translation context for the array length
4330 // expression. FIXME: This won't work in general.
4331 Translate_context
context(gogo
, NULL
, NULL
, NULL_TREE
);
4332 tree len
= this->length_
->get_tree(&context
);
4333 len
= convert_to_integer(integer_type_node
, len
);
4334 this->length_tree_
= save_expr(len
);
4337 return this->length_tree_
;
4340 // Get a tree for the type of this array. A fixed array is simply
4341 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4342 // just like an array in C. An open array is a struct with three
4343 // fields: a data pointer, the length, and the capacity.
4346 Array_type::do_get_tree(Gogo
* gogo
)
4348 if (this->length_
== NULL
)
4350 tree struct_type
= gogo
->slice_type_tree(void_type_node
);
4351 return this->fill_in_tree(gogo
, struct_type
);
4355 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4356 tree length_tree
= this->get_length_tree(gogo
);
4357 if (element_type_tree
== error_mark_node
4358 || length_tree
== error_mark_node
)
4359 return error_mark_node
;
4361 length_tree
= fold_convert(sizetype
, length_tree
);
4363 // build_index_type takes the maximum index, which is one less
4365 tree index_type
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
4369 return build_array_type(element_type_tree
, index_type
);
4373 // Fill in the fields for a slice type. This is used for named slice
4377 Array_type::fill_in_tree(Gogo
* gogo
, tree struct_type
)
4379 gcc_assert(this->length_
== NULL
);
4381 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4382 tree field
= TYPE_FIELDS(struct_type
);
4383 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
4384 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field
))
4385 && TREE_TYPE(TREE_TYPE(field
)) == void_type_node
);
4386 TREE_TYPE(field
) = build_pointer_type(element_type_tree
);
4391 // Return an initializer for an array type.
4394 Array_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
4396 if (this->length_
== NULL
)
4403 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
4405 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
4407 for (tree field
= TYPE_FIELDS(type_tree
);
4409 field
= DECL_CHAIN(field
))
4411 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
,
4414 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
4417 tree ret
= build_constructor(type_tree
, init
);
4418 TREE_CONSTANT(ret
) = 1;
4425 tree value
= this->element_type_
->get_init_tree(gogo
, is_clear
);
4429 tree length_tree
= this->get_length_tree(gogo
);
4430 length_tree
= fold_convert(sizetype
, length_tree
);
4431 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
,
4432 fold_build2(MINUS_EXPR
, sizetype
,
4433 length_tree
, size_one_node
));
4434 tree ret
= build_constructor_single(type_tree
, range
, value
);
4435 if (TREE_CONSTANT(value
))
4436 TREE_CONSTANT(ret
) = 1;
4441 // Handle the builtin make function for a slice.
4444 Array_type::do_make_expression_tree(Translate_context
* context
,
4445 Expression_list
* args
,
4446 source_location location
)
4448 gcc_assert(this->length_
== NULL
);
4450 Gogo
* gogo
= context
->gogo();
4451 tree type_tree
= this->get_tree(gogo
);
4452 if (type_tree
== error_mark_node
)
4453 return error_mark_node
;
4455 tree values_field
= TYPE_FIELDS(type_tree
);
4456 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field
)),
4459 tree count_field
= DECL_CHAIN(values_field
);
4460 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field
)),
4463 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4464 if (element_type_tree
== error_mark_node
)
4465 return error_mark_node
;
4466 tree element_size_tree
= TYPE_SIZE_UNIT(element_type_tree
);
4468 tree value
= this->element_type_
->get_init_tree(gogo
, true);
4470 // The first argument is the number of elements, the optional second
4471 // argument is the capacity.
4472 gcc_assert(args
!= NULL
&& args
->size() >= 1 && args
->size() <= 2);
4474 tree length_tree
= args
->front()->get_tree(context
);
4475 if (length_tree
== error_mark_node
)
4476 return error_mark_node
;
4477 if (!DECL_P(length_tree
))
4478 length_tree
= save_expr(length_tree
);
4479 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree
)))
4480 length_tree
= convert_to_integer(TREE_TYPE(count_field
), length_tree
);
4482 tree bad_index
= Expression::check_bounds(length_tree
,
4483 TREE_TYPE(count_field
),
4484 NULL_TREE
, location
);
4486 length_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
), length_tree
);
4488 if (args
->size() == 1)
4489 capacity_tree
= length_tree
;
4492 capacity_tree
= args
->back()->get_tree(context
);
4493 if (capacity_tree
== error_mark_node
)
4494 return error_mark_node
;
4495 if (!DECL_P(capacity_tree
))
4496 capacity_tree
= save_expr(capacity_tree
);
4497 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree
)))
4498 capacity_tree
= convert_to_integer(TREE_TYPE(count_field
),
4501 bad_index
= Expression::check_bounds(capacity_tree
,
4502 TREE_TYPE(count_field
),
4503 bad_index
, location
);
4505 tree chktype
= (((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4506 > TYPE_SIZE(TREE_TYPE(length_tree
)))
4507 || ((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4508 == TYPE_SIZE(TREE_TYPE(length_tree
)))
4509 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree
))))
4510 ? TREE_TYPE(capacity_tree
)
4511 : TREE_TYPE(length_tree
));
4512 tree chk
= fold_build2_loc(location
, LT_EXPR
, boolean_type_node
,
4513 fold_convert_loc(location
, chktype
,
4515 fold_convert_loc(location
, chktype
,
4517 if (bad_index
== NULL_TREE
)
4520 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4523 capacity_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
),
4527 tree size_tree
= fold_build2_loc(location
, MULT_EXPR
, sizetype
,
4529 fold_convert_loc(location
, sizetype
,
4532 tree chk
= fold_build2_loc(location
, TRUTH_AND_EXPR
, boolean_type_node
,
4533 fold_build2_loc(location
, GT_EXPR
,
4535 fold_convert_loc(location
,
4539 fold_build2_loc(location
, LT_EXPR
,
4541 size_tree
, element_size_tree
));
4542 if (bad_index
== NULL_TREE
)
4545 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4548 tree space
= context
->gogo()->allocate_memory(this->element_type_
,
4549 size_tree
, location
);
4551 if (value
!= NULL_TREE
)
4552 space
= save_expr(space
);
4554 space
= fold_convert(TREE_TYPE(values_field
), space
);
4556 if (bad_index
!= NULL_TREE
&& bad_index
!= boolean_false_node
)
4558 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
,
4560 space
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4561 build3(COND_EXPR
, void_type_node
,
4562 bad_index
, crash
, NULL_TREE
),
4566 tree constructor
= gogo
->slice_constructor(type_tree
, space
, length_tree
,
4569 if (value
== NULL_TREE
)
4571 // The array contents are zero initialized.
4575 // The elements must be initialized.
4577 tree max
= fold_build2_loc(location
, MINUS_EXPR
, TREE_TYPE(count_field
),
4579 fold_convert_loc(location
, TREE_TYPE(count_field
),
4582 tree array_type
= build_array_type(element_type_tree
,
4583 build_index_type(max
));
4585 tree value_pointer
= fold_convert_loc(location
,
4586 build_pointer_type(array_type
),
4589 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
, max
);
4590 tree space_init
= build_constructor_single(array_type
, range
, value
);
4592 return build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4593 build2(MODIFY_EXPR
, void_type_node
,
4594 build_fold_indirect_ref(value_pointer
),
4599 // Return a tree for a pointer to the values in ARRAY.
4602 Array_type::value_pointer_tree(Gogo
*, tree array
) const
4605 if (this->length() != NULL
)
4608 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
4609 build_fold_addr_expr(array
));
4614 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
4615 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
4617 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
4620 if (TREE_CONSTANT(array
))
4621 TREE_CONSTANT(ret
) = 1;
4625 // Return a tree for the length of the array ARRAY which has this
4629 Array_type::length_tree(Gogo
* gogo
, tree array
)
4631 if (this->length_
!= NULL
)
4633 if (TREE_CODE(array
) == SAVE_EXPR
)
4634 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
4636 return omit_one_operand(integer_type_node
,
4637 this->get_length_tree(gogo
), array
);
4640 // This is an open array. We need to read the length field.
4642 tree type
= TREE_TYPE(array
);
4643 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4645 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
4646 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
4648 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4649 if (TREE_CONSTANT(array
))
4650 TREE_CONSTANT(ret
) = 1;
4654 // Return a tree for the capacity of the array ARRAY which has this
4658 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
4660 if (this->length_
!= NULL
)
4661 return omit_one_operand(sizetype
, this->get_length_tree(gogo
), array
);
4663 // This is an open array. We need to read the capacity field.
4665 tree type
= TREE_TYPE(array
);
4666 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4668 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
4669 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
4671 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4677 Array_type::do_export(Export
* exp
) const
4679 exp
->write_c_string("[");
4680 if (this->length_
!= NULL
)
4681 this->length_
->export_expression(exp
);
4682 exp
->write_c_string("] ");
4683 exp
->write_type(this->element_type_
);
4689 Array_type::do_import(Import
* imp
)
4691 imp
->require_c_string("[");
4693 if (imp
->peek_char() == ']')
4696 length
= Expression::import_expression(imp
);
4697 imp
->require_c_string("] ");
4698 Type
* element_type
= imp
->read_type();
4699 return Type::make_array_type(element_type
, length
);
4702 // The type of an array type descriptor.
4705 Array_type::make_array_type_descriptor_type()
4710 Type
* tdt
= Type::make_type_descriptor_type();
4711 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4713 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4716 Type::make_builtin_struct_type(3,
4719 "len", uintptr_type
);
4721 ret
= Type::make_builtin_named_type("ArrayType", sf
);
4727 // The type of an slice type descriptor.
4730 Array_type::make_slice_type_descriptor_type()
4735 Type
* tdt
= Type::make_type_descriptor_type();
4736 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4739 Type::make_builtin_struct_type(2,
4743 ret
= Type::make_builtin_named_type("SliceType", sf
);
4749 // Build a type descriptor for an array/slice type.
4752 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4754 if (this->length_
!= NULL
)
4755 return this->array_type_descriptor(gogo
, name
);
4757 return this->slice_type_descriptor(gogo
, name
);
4760 // Build a type descriptor for an array type.
4763 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4765 source_location bloc
= BUILTINS_LOCATION
;
4767 Type
* atdt
= Array_type::make_array_type_descriptor_type();
4769 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
4771 Expression_list
* vals
= new Expression_list();
4774 Struct_field_list::const_iterator p
= fields
->begin();
4775 gcc_assert(p
->field_name() == "commonType");
4776 vals
->push_back(this->type_descriptor_constructor(gogo
,
4777 RUNTIME_TYPE_KIND_ARRAY
,
4781 gcc_assert(p
->field_name() == "elem");
4782 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4785 gcc_assert(p
->field_name() == "len");
4786 vals
->push_back(this->length_
);
4789 gcc_assert(p
== fields
->end());
4791 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
4794 // Build a type descriptor for a slice type.
4797 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4799 source_location bloc
= BUILTINS_LOCATION
;
4801 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
4803 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4805 Expression_list
* vals
= new Expression_list();
4808 Struct_field_list::const_iterator p
= fields
->begin();
4809 gcc_assert(p
->field_name() == "commonType");
4810 vals
->push_back(this->type_descriptor_constructor(gogo
,
4811 RUNTIME_TYPE_KIND_SLICE
,
4815 gcc_assert(p
->field_name() == "elem");
4816 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4819 gcc_assert(p
== fields
->end());
4821 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4824 // Reflection string.
4827 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4829 ret
->push_back('[');
4830 if (this->length_
!= NULL
)
4835 if (!this->length_
->integer_constant_value(true, val
, &type
))
4836 error_at(this->length_
->location(),
4837 "array length must be integer constant expression");
4838 else if (mpz_cmp_si(val
, 0) < 0)
4839 error_at(this->length_
->location(), "array length is negative");
4840 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4841 error_at(this->length_
->location(), "array length is too large");
4845 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4850 ret
->push_back(']');
4852 this->append_reflection(this->element_type_
, gogo
, ret
);
4858 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4860 ret
->push_back('A');
4861 this->append_mangled_name(this->element_type_
, gogo
, ret
);
4862 if (this->length_
!= NULL
)
4867 if (!this->length_
->integer_constant_value(true, val
, &type
))
4868 error_at(this->length_
->location(),
4869 "array length must be integer constant expression");
4870 else if (mpz_cmp_si(val
, 0) < 0)
4871 error_at(this->length_
->location(), "array length is negative");
4872 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4873 error_at(this->length_
->location(), "array size is too large");
4877 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4882 ret
->push_back('e');
4885 // Make an array type.
4888 Type::make_array_type(Type
* element_type
, Expression
* length
)
4890 return new Array_type(element_type
, length
);
4898 Map_type::do_traverse(Traverse
* traverse
)
4900 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
4901 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
4902 return TRAVERSE_EXIT
;
4903 return TRAVERSE_CONTINUE
;
4906 // Check that the map type is OK.
4909 Map_type::do_verify()
4911 if (this->key_type_
->struct_type() != NULL
4912 || this->key_type_
->array_type() != NULL
)
4914 error_at(this->location_
, "invalid map key type");
4920 // Whether two map types are identical.
4923 Map_type::is_identical(const Map_type
* t
) const
4925 return (Type::are_identical(this->key_type(), t
->key_type(), NULL
)
4926 && Type::are_identical(this->val_type(), t
->val_type(), NULL
));
4932 Map_type::do_hash_for_method(Gogo
* gogo
) const
4934 return (this->key_type_
->hash_for_method(gogo
)
4935 + this->val_type_
->hash_for_method(gogo
)
4939 // Check that a call to the builtin make function is valid. For a map
4940 // the optional argument is the number of spaces to preallocate for
4944 Map_type::do_check_make_expression(Expression_list
* args
,
4945 source_location location
)
4947 if (args
!= NULL
&& !args
->empty())
4949 if (!Type::check_int_value(args
->front(), _("bad size when making map"),
4952 else if (args
->size() > 1)
4954 error_at(location
, "too many arguments when making map");
4961 // Get a tree for a map type. A map type is represented as a pointer
4962 // to a struct. The struct is __go_map in libgo/map.h.
4965 Map_type::do_get_tree(Gogo
* gogo
)
4967 static tree type_tree
;
4968 if (type_tree
== NULL_TREE
)
4970 tree struct_type
= make_node(RECORD_TYPE
);
4972 tree map_descriptor_type
= gogo
->map_descriptor_type();
4973 tree const_map_descriptor_type
=
4974 build_qualified_type(map_descriptor_type
, TYPE_QUAL_CONST
);
4975 tree name
= get_identifier("__descriptor");
4976 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
4977 build_pointer_type(const_map_descriptor_type
));
4978 DECL_CONTEXT(field
) = struct_type
;
4979 TYPE_FIELDS(struct_type
) = field
;
4980 tree last_field
= field
;
4982 name
= get_identifier("__element_count");
4983 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
4984 DECL_CONTEXT(field
) = struct_type
;
4985 DECL_CHAIN(last_field
) = field
;
4988 name
= get_identifier("__bucket_count");
4989 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
4990 DECL_CONTEXT(field
) = struct_type
;
4991 DECL_CHAIN(last_field
) = field
;
4994 name
= get_identifier("__buckets");
4995 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
4996 build_pointer_type(ptr_type_node
));
4997 DECL_CONTEXT(field
) = struct_type
;
4998 DECL_CHAIN(last_field
) = field
;
5000 layout_type(struct_type
);
5002 // Give the struct a name for better debugging info.
5003 name
= get_identifier("__go_map");
5004 tree type_decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
, name
,
5006 DECL_ARTIFICIAL(type_decl
) = 1;
5007 TYPE_NAME(struct_type
) = type_decl
;
5008 go_preserve_from_gc(type_decl
);
5009 rest_of_decl_compilation(type_decl
, 1, 0);
5011 type_tree
= build_pointer_type(struct_type
);
5012 go_preserve_from_gc(type_tree
);
5018 // Initialize a map.
5021 Map_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5025 return fold_convert(type_tree
, null_pointer_node
);
5028 // Return an expression for a newly allocated map.
5031 Map_type::do_make_expression_tree(Translate_context
* context
,
5032 Expression_list
* args
,
5033 source_location location
)
5035 tree bad_index
= NULL_TREE
;
5038 if (args
== NULL
|| args
->empty())
5039 expr_tree
= size_zero_node
;
5042 expr_tree
= args
->front()->get_tree(context
);
5043 if (expr_tree
== error_mark_node
)
5044 return error_mark_node
;
5045 if (!DECL_P(expr_tree
))
5046 expr_tree
= save_expr(expr_tree
);
5047 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5048 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5049 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5053 tree map_type
= this->get_tree(context
->gogo());
5055 static tree new_map_fndecl
;
5056 tree ret
= Gogo::call_builtin(&new_map_fndecl
,
5061 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type
))),
5062 context
->gogo()->map_descriptor(this),
5065 // This can panic if the capacity is out of range.
5066 TREE_NOTHROW(new_map_fndecl
) = 0;
5068 if (bad_index
== NULL_TREE
)
5072 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
,
5074 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5075 build3(COND_EXPR
, void_type_node
,
5076 bad_index
, crash
, NULL_TREE
),
5081 // The type of a map type descriptor.
5084 Map_type::make_map_type_descriptor_type()
5089 Type
* tdt
= Type::make_type_descriptor_type();
5090 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5093 Type::make_builtin_struct_type(3,
5098 ret
= Type::make_builtin_named_type("MapType", sf
);
5104 // Build a type descriptor for a map type.
5107 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5109 source_location bloc
= BUILTINS_LOCATION
;
5111 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
5113 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
5115 Expression_list
* vals
= new Expression_list();
5118 Struct_field_list::const_iterator p
= fields
->begin();
5119 gcc_assert(p
->field_name() == "commonType");
5120 vals
->push_back(this->type_descriptor_constructor(gogo
,
5121 RUNTIME_TYPE_KIND_MAP
,
5125 gcc_assert(p
->field_name() == "key");
5126 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
5129 gcc_assert(p
->field_name() == "elem");
5130 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
5133 gcc_assert(p
== fields
->end());
5135 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
5138 // Reflection string for a map.
5141 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5143 ret
->append("map[");
5144 this->append_reflection(this->key_type_
, gogo
, ret
);
5146 this->append_reflection(this->val_type_
, gogo
, ret
);
5149 // Mangled name for a map.
5152 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5154 ret
->push_back('M');
5155 this->append_mangled_name(this->key_type_
, gogo
, ret
);
5157 this->append_mangled_name(this->val_type_
, gogo
, ret
);
5160 // Export a map type.
5163 Map_type::do_export(Export
* exp
) const
5165 exp
->write_c_string("map [");
5166 exp
->write_type(this->key_type_
);
5167 exp
->write_c_string("] ");
5168 exp
->write_type(this->val_type_
);
5171 // Import a map type.
5174 Map_type::do_import(Import
* imp
)
5176 imp
->require_c_string("map [");
5177 Type
* key_type
= imp
->read_type();
5178 imp
->require_c_string("] ");
5179 Type
* val_type
= imp
->read_type();
5180 return Type::make_map_type(key_type
, val_type
, imp
->location());
5186 Type::make_map_type(Type
* key_type
, Type
* val_type
, source_location location
)
5188 return new Map_type(key_type
, val_type
, location
);
5191 // Class Channel_type.
5196 Channel_type::do_hash_for_method(Gogo
* gogo
) const
5198 unsigned int ret
= 0;
5199 if (this->may_send_
)
5201 if (this->may_receive_
)
5203 if (this->element_type_
!= NULL
)
5204 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
5208 // Whether this type is the same as T.
5211 Channel_type::is_identical(const Channel_type
* t
) const
5213 if (!Type::are_identical(this->element_type(), t
->element_type(), NULL
))
5215 return (this->may_send_
== t
->may_send_
5216 && this->may_receive_
== t
->may_receive_
);
5219 // Check whether the parameters for a call to the builtin function
5220 // make are OK for a channel. A channel can take an optional single
5221 // parameter which is the buffer size.
5224 Channel_type::do_check_make_expression(Expression_list
* args
,
5225 source_location location
)
5227 if (args
!= NULL
&& !args
->empty())
5229 if (!Type::check_int_value(args
->front(),
5230 _("bad buffer size when making channel"),
5233 else if (args
->size() > 1)
5235 error_at(location
, "too many arguments when making channel");
5242 // Return the tree for a channel type. A channel is a pointer to a
5243 // __go_channel struct. The __go_channel struct is defined in
5244 // libgo/runtime/channel.h.
5247 Channel_type::do_get_tree(Gogo
*)
5249 static tree type_tree
;
5250 if (type_tree
== NULL_TREE
)
5252 tree ret
= make_node(RECORD_TYPE
);
5253 TYPE_NAME(ret
) = get_identifier("__go_channel");
5254 TYPE_STUB_DECL(ret
) = build_decl(BUILTINS_LOCATION
, TYPE_DECL
, NULL_TREE
,
5256 type_tree
= build_pointer_type(ret
);
5257 go_preserve_from_gc(type_tree
);
5262 // Initialize a channel variable.
5265 Channel_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5269 return fold_convert(type_tree
, null_pointer_node
);
5272 // Handle the builtin function make for a channel.
5275 Channel_type::do_make_expression_tree(Translate_context
* context
,
5276 Expression_list
* args
,
5277 source_location location
)
5279 Gogo
* gogo
= context
->gogo();
5280 tree channel_type
= this->get_tree(gogo
);
5282 tree element_tree
= this->element_type_
->get_tree(gogo
);
5283 tree element_size_tree
= size_in_bytes(element_tree
);
5285 tree bad_index
= NULL_TREE
;
5288 if (args
== NULL
|| args
->empty())
5289 expr_tree
= size_zero_node
;
5292 expr_tree
= args
->front()->get_tree(context
);
5293 if (expr_tree
== error_mark_node
)
5294 return error_mark_node
;
5295 if (!DECL_P(expr_tree
))
5296 expr_tree
= save_expr(expr_tree
);
5297 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5298 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5299 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5303 static tree new_channel_fndecl
;
5304 tree ret
= Gogo::call_builtin(&new_channel_fndecl
,
5313 // This can panic if the capacity is out of range.
5314 TREE_NOTHROW(new_channel_fndecl
) = 0;
5316 if (bad_index
== NULL_TREE
)
5320 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
,
5322 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5323 build3(COND_EXPR
, void_type_node
,
5324 bad_index
, crash
, NULL_TREE
),
5329 // Build a type descriptor for a channel type.
5332 Channel_type::make_chan_type_descriptor_type()
5337 Type
* tdt
= Type::make_type_descriptor_type();
5338 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5340 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5343 Type::make_builtin_struct_type(3,
5346 "dir", uintptr_type
);
5348 ret
= Type::make_builtin_named_type("ChanType", sf
);
5354 // Build a type descriptor for a map type.
5357 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5359 source_location bloc
= BUILTINS_LOCATION
;
5361 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
5363 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
5365 Expression_list
* vals
= new Expression_list();
5368 Struct_field_list::const_iterator p
= fields
->begin();
5369 gcc_assert(p
->field_name() == "commonType");
5370 vals
->push_back(this->type_descriptor_constructor(gogo
,
5371 RUNTIME_TYPE_KIND_CHAN
,
5375 gcc_assert(p
->field_name() == "elem");
5376 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5379 gcc_assert(p
->field_name() == "dir");
5380 // These bits must match the ones in libgo/runtime/go-type.h.
5382 if (this->may_receive_
)
5384 if (this->may_send_
)
5387 mpz_init_set_ui(iv
, val
);
5388 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
5392 gcc_assert(p
== fields
->end());
5394 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
5397 // Reflection string.
5400 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5402 if (!this->may_send_
)
5404 ret
->append("chan");
5405 if (!this->may_receive_
)
5407 ret
->push_back(' ');
5408 this->append_reflection(this->element_type_
, gogo
, ret
);
5414 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5416 ret
->push_back('C');
5417 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5418 if (this->may_send_
)
5419 ret
->push_back('s');
5420 if (this->may_receive_
)
5421 ret
->push_back('r');
5422 ret
->push_back('e');
5428 Channel_type::do_export(Export
* exp
) const
5430 exp
->write_c_string("chan ");
5431 if (this->may_send_
&& !this->may_receive_
)
5432 exp
->write_c_string("-< ");
5433 else if (this->may_receive_
&& !this->may_send_
)
5434 exp
->write_c_string("<- ");
5435 exp
->write_type(this->element_type_
);
5441 Channel_type::do_import(Import
* imp
)
5443 imp
->require_c_string("chan ");
5447 if (imp
->match_c_string("-< "))
5451 may_receive
= false;
5453 else if (imp
->match_c_string("<- "))
5465 Type
* element_type
= imp
->read_type();
5467 return Type::make_channel_type(may_send
, may_receive
, element_type
);
5470 // Make a new channel type.
5473 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
5475 return new Channel_type(send
, receive
, element_type
);
5478 // Class Interface_type.
5483 Interface_type::do_traverse(Traverse
* traverse
)
5485 if (this->methods_
== NULL
)
5486 return TRAVERSE_CONTINUE
;
5487 return this->methods_
->traverse(traverse
);
5490 // Finalize the methods. This handles interface inheritance.
5493 Interface_type::finalize_methods()
5495 if (this->methods_
== NULL
)
5497 bool is_recursive
= false;
5500 while (from
< this->methods_
->size())
5502 const Typed_identifier
* p
= &this->methods_
->at(from
);
5503 if (!p
->name().empty())
5506 this->methods_
->set(to
, *p
);
5511 Interface_type
* it
= p
->type()->interface_type();
5514 error_at(p
->location(), "interface contains embedded non-interface");
5522 error_at(p
->location(), "invalid recursive interface");
5523 is_recursive
= true;
5528 const Typed_identifier_list
* methods
= it
->methods();
5529 if (methods
== NULL
)
5534 for (Typed_identifier_list::const_iterator q
= methods
->begin();
5535 q
!= methods
->end();
5538 if (q
->name().empty() || this->find_method(q
->name()) == NULL
)
5539 this->methods_
->push_back(Typed_identifier(q
->name(), q
->type(),
5544 error_at(p
->location(), "inherited method %qs is ambiguous",
5545 Gogo::message_name(q
->name()).c_str());
5552 delete this->methods_
;
5553 this->methods_
= NULL
;
5557 this->methods_
->resize(to
);
5558 this->methods_
->sort_by_name();
5562 // Return the method NAME, or NULL.
5564 const Typed_identifier
*
5565 Interface_type::find_method(const std::string
& name
) const
5567 if (this->methods_
== NULL
)
5569 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5570 p
!= this->methods_
->end();
5572 if (p
->name() == name
)
5577 // Return the method index.
5580 Interface_type::method_index(const std::string
& name
) const
5582 gcc_assert(this->methods_
!= NULL
);
5584 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5585 p
!= this->methods_
->end();
5587 if (p
->name() == name
)
5592 // Return whether NAME is an unexported method, for better error
5596 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
5598 if (this->methods_
== NULL
)
5600 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5601 p
!= this->methods_
->end();
5604 const std::string
& method_name(p
->name());
5605 if (Gogo::is_hidden_name(method_name
)
5606 && name
== Gogo::unpack_hidden_name(method_name
)
5607 && gogo
->pack_hidden_name(name
, false) != method_name
)
5613 // Whether this type is identical with T.
5616 Interface_type::is_identical(const Interface_type
* t
) const
5618 // We require the same methods with the same types. The methods
5619 // have already been sorted.
5620 if (this->methods() == NULL
|| t
->methods() == NULL
)
5621 return this->methods() == t
->methods();
5623 Typed_identifier_list::const_iterator p1
= this->methods()->begin();
5624 for (Typed_identifier_list::const_iterator p2
= t
->methods()->begin();
5625 p2
!= t
->methods()->end();
5628 if (p1
== this->methods()->end())
5630 if (p1
->name() != p2
->name()
5631 || !Type::are_identical(p1
->type(), p2
->type(), NULL
))
5634 if (p1
!= this->methods()->end())
5639 // Whether we can assign the interface type T to this type. The types
5640 // are known to not be identical. An interface assignment is only
5641 // permitted if T is known to implement all methods in THIS.
5642 // Otherwise a type guard is required.
5645 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
5646 std::string
* reason
) const
5648 if (this->methods() == NULL
)
5650 for (Typed_identifier_list::const_iterator p
= this->methods()->begin();
5651 p
!= this->methods()->end();
5654 const Typed_identifier
* m
= t
->find_method(p
->name());
5660 snprintf(buf
, sizeof buf
,
5661 _("need explicit conversion; missing method %s%s%s"),
5662 open_quote
, Gogo::message_name(p
->name()).c_str(),
5664 reason
->assign(buf
);
5669 std::string subreason
;
5670 if (!Type::are_identical(p
->type(), m
->type(), &subreason
))
5674 std::string n
= Gogo::message_name(p
->name());
5675 size_t len
= 100 + n
.length() + subreason
.length();
5676 char* buf
= new char[len
];
5677 if (subreason
.empty())
5678 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5679 open_quote
, n
.c_str(), close_quote
);
5682 _("incompatible type for method %s%s%s (%s)"),
5683 open_quote
, n
.c_str(), close_quote
,
5685 reason
->assign(buf
);
5698 Interface_type::do_hash_for_method(Gogo
* gogo
) const
5700 unsigned int ret
= 0;
5701 if (this->methods_
!= NULL
)
5703 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5704 p
!= this->methods_
->end();
5707 ret
= Type::hash_string(p
->name(), ret
);
5708 ret
+= p
->type()->hash_for_method(gogo
);
5715 // Return true if T implements the interface. If it does not, and
5716 // REASON is not NULL, set *REASON to a useful error message.
5719 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
5721 if (this->methods_
== NULL
)
5724 bool is_pointer
= false;
5725 const Named_type
* nt
= t
->named_type();
5726 const Struct_type
* st
= t
->struct_type();
5727 // If we start with a named type, we don't dereference it to find
5731 const Type
* pt
= t
->points_to();
5734 // If T is a pointer to a named type, then we need to look at
5735 // the type to which it points.
5737 nt
= pt
->named_type();
5738 st
= pt
->struct_type();
5742 // If we have a named type, get the methods from it rather than from
5747 // Only named and struct types have methods.
5748 if (nt
== NULL
&& st
== NULL
)
5752 if (t
->points_to() != NULL
5753 && t
->points_to()->interface_type() != NULL
)
5754 reason
->assign(_("pointer to interface type has no methods"));
5756 reason
->assign(_("type has no methods"));
5761 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
5765 if (t
->points_to() != NULL
5766 && t
->points_to()->interface_type() != NULL
)
5767 reason
->assign(_("pointer to interface type has no methods"));
5769 reason
->assign(_("type has no methods"));
5774 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5775 p
!= this->methods_
->end();
5778 bool is_ambiguous
= false;
5779 Method
* m
= (nt
!= NULL
5780 ? nt
->method_function(p
->name(), &is_ambiguous
)
5781 : st
->method_function(p
->name(), &is_ambiguous
));
5786 std::string n
= Gogo::message_name(p
->name());
5787 size_t len
= n
.length() + 100;
5788 char* buf
= new char[len
];
5790 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
5791 open_quote
, n
.c_str(), close_quote
);
5793 snprintf(buf
, len
, _("missing method %s%s%s"),
5794 open_quote
, n
.c_str(), close_quote
);
5795 reason
->assign(buf
);
5801 Function_type
*p_fn_type
= p
->type()->function_type();
5802 Function_type
* m_fn_type
= m
->type()->function_type();
5803 gcc_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
5804 std::string subreason
;
5805 if (!p_fn_type
->is_identical(m_fn_type
, true, &subreason
))
5809 std::string n
= Gogo::message_name(p
->name());
5810 size_t len
= 100 + n
.length() + subreason
.length();
5811 char* buf
= new char[len
];
5812 if (subreason
.empty())
5813 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5814 open_quote
, n
.c_str(), close_quote
);
5817 _("incompatible type for method %s%s%s (%s)"),
5818 open_quote
, n
.c_str(), close_quote
,
5820 reason
->assign(buf
);
5826 if (!is_pointer
&& !m
->is_value_method())
5830 std::string n
= Gogo::message_name(p
->name());
5831 size_t len
= 100 + n
.length();
5832 char* buf
= new char[len
];
5833 snprintf(buf
, len
, _("method %s%s%s requires a pointer"),
5834 open_quote
, n
.c_str(), close_quote
);
5835 reason
->assign(buf
);
5845 // Return a tree for an interface type. An interface is a pointer to
5846 // a struct. The struct has three fields. The first field is a
5847 // pointer to the type descriptor for the dynamic type of the object.
5848 // The second field is a pointer to a table of methods for the
5849 // interface to be used with the object. The third field is the value
5850 // of the object itself.
5853 Interface_type::do_get_tree(Gogo
* gogo
)
5855 if (this->methods_
== NULL
)
5857 // At the tree level, use the same type for all empty
5858 // interfaces. This lets us assign them to each other directly
5859 // without triggering GIMPLE type errors.
5860 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5861 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5862 static tree empty_interface
;
5863 return Gogo::builtin_struct(&empty_interface
, "__go_empty_interface",
5865 "__type_descriptor",
5871 return this->fill_in_tree(gogo
, make_node(RECORD_TYPE
));
5874 // Fill in the tree for an interface type. This is used for named
5878 Interface_type::fill_in_tree(Gogo
* gogo
, tree type
)
5880 gcc_assert(this->methods_
!= NULL
);
5882 // Build the type of the table of methods.
5884 tree method_table
= make_node(RECORD_TYPE
);
5886 // The first field is a pointer to the type descriptor.
5887 tree name_tree
= get_identifier("__type_descriptor");
5888 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5889 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5890 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, dtype
);
5891 DECL_CONTEXT(field
) = method_table
;
5892 TYPE_FIELDS(method_table
) = field
;
5894 std::string last_name
= "";
5895 tree
* pp
= &DECL_CHAIN(field
);
5896 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5897 p
!= this->methods_
->end();
5900 std::string name
= Gogo::unpack_hidden_name(p
->name());
5901 name_tree
= get_identifier_with_length(name
.data(), name
.length());
5902 tree field_type
= p
->type()->get_tree(gogo
);
5903 if (field_type
== error_mark_node
)
5904 return error_mark_node
;
5905 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, field_type
);
5906 DECL_CONTEXT(field
) = method_table
;
5908 pp
= &DECL_CHAIN(field
);
5909 // Sanity check: the names should be sorted.
5910 gcc_assert(p
->name() > last_name
);
5911 last_name
= p
->name();
5913 layout_type(method_table
);
5915 tree mtype
= build_pointer_type(method_table
);
5917 tree field_trees
= NULL_TREE
;
5920 name_tree
= get_identifier("__methods");
5921 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, mtype
);
5922 DECL_CONTEXT(field
) = type
;
5924 pp
= &DECL_CHAIN(field
);
5926 name_tree
= get_identifier("__object");
5927 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, ptr_type_node
);
5928 DECL_CONTEXT(field
) = type
;
5931 TYPE_FIELDS(type
) = field_trees
;
5938 // Initialization value.
5941 Interface_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5946 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
5947 for (tree field
= TYPE_FIELDS(type_tree
);
5949 field
= DECL_CHAIN(field
))
5951 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
5953 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
5956 tree ret
= build_constructor(type_tree
, init
);
5957 TREE_CONSTANT(ret
) = 1;
5961 // The type of an interface type descriptor.
5964 Interface_type::make_interface_type_descriptor_type()
5969 Type
* tdt
= Type::make_type_descriptor_type();
5970 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5972 Type
* string_type
= Type::lookup_string_type();
5973 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
5976 Type::make_builtin_struct_type(3,
5977 "name", pointer_string_type
,
5978 "pkgPath", pointer_string_type
,
5981 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
5983 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
5985 Struct_type
* s
= Type::make_builtin_struct_type(2,
5987 "methods", slice_nsm
);
5989 ret
= Type::make_builtin_named_type("InterfaceType", s
);
5995 // Build a type descriptor for an interface type.
5998 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6000 source_location bloc
= BUILTINS_LOCATION
;
6002 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
6004 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
6006 Expression_list
* ivals
= new Expression_list();
6009 Struct_field_list::const_iterator pif
= ifields
->begin();
6010 gcc_assert(pif
->field_name() == "commonType");
6011 ivals
->push_back(this->type_descriptor_constructor(gogo
,
6012 RUNTIME_TYPE_KIND_INTERFACE
,
6016 gcc_assert(pif
->field_name() == "methods");
6018 Expression_list
* methods
= new Expression_list();
6019 if (this->methods_
!= NULL
&& !this->methods_
->empty())
6021 Type
* elemtype
= pif
->type()->array_type()->element_type();
6023 methods
->reserve(this->methods_
->size());
6024 for (Typed_identifier_list::const_iterator pm
= this->methods_
->begin();
6025 pm
!= this->methods_
->end();
6028 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
6030 Expression_list
* mvals
= new Expression_list();
6033 Struct_field_list::const_iterator pmf
= mfields
->begin();
6034 gcc_assert(pmf
->field_name() == "name");
6035 std::string s
= Gogo::unpack_hidden_name(pm
->name());
6036 Expression
* e
= Expression::make_string(s
, bloc
);
6037 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6040 gcc_assert(pmf
->field_name() == "pkgPath");
6041 if (!Gogo::is_hidden_name(pm
->name()))
6042 mvals
->push_back(Expression::make_nil(bloc
));
6045 s
= Gogo::hidden_name_prefix(pm
->name());
6046 e
= Expression::make_string(s
, bloc
);
6047 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6051 gcc_assert(pmf
->field_name() == "typ");
6052 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
6055 gcc_assert(pmf
== mfields
->end());
6057 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
6059 methods
->push_back(e
);
6063 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
6067 gcc_assert(pif
== ifields
->end());
6069 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
6072 // Reflection string.
6075 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6077 ret
->append("interface {");
6078 if (this->methods_
!= NULL
)
6080 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6081 p
!= this->methods_
->end();
6084 if (p
!= this->methods_
->begin())
6086 ret
->push_back(' ');
6087 ret
->append(Gogo::unpack_hidden_name(p
->name()));
6088 std::string sub
= p
->type()->reflection(gogo
);
6089 gcc_assert(sub
.compare(0, 4, "func") == 0);
6090 sub
= sub
.substr(4);
6100 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6102 ret
->push_back('I');
6104 const Typed_identifier_list
* methods
= this->methods_
;
6105 if (methods
!= NULL
)
6107 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6108 p
!= methods
->end();
6111 std::string n
= Gogo::unpack_hidden_name(p
->name());
6113 snprintf(buf
, sizeof buf
, "%u_",
6114 static_cast<unsigned int>(n
.length()));
6117 this->append_mangled_name(p
->type(), gogo
, ret
);
6121 ret
->push_back('e');
6127 Interface_type::do_export(Export
* exp
) const
6129 exp
->write_c_string("interface { ");
6131 const Typed_identifier_list
* methods
= this->methods_
;
6132 if (methods
!= NULL
)
6134 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
6135 pm
!= methods
->end();
6138 exp
->write_string(pm
->name());
6139 exp
->write_c_string(" (");
6141 const Function_type
* fntype
= pm
->type()->function_type();
6144 const Typed_identifier_list
* parameters
= fntype
->parameters();
6145 if (parameters
!= NULL
)
6147 bool is_varargs
= fntype
->is_varargs();
6148 for (Typed_identifier_list::const_iterator pp
=
6149 parameters
->begin();
6150 pp
!= parameters
->end();
6156 exp
->write_c_string(", ");
6157 if (!is_varargs
|| pp
+ 1 != parameters
->end())
6158 exp
->write_type(pp
->type());
6161 exp
->write_c_string("...");
6162 Type
*pptype
= pp
->type();
6163 exp
->write_type(pptype
->array_type()->element_type());
6168 exp
->write_c_string(")");
6170 const Typed_identifier_list
* results
= fntype
->results();
6171 if (results
!= NULL
)
6173 exp
->write_c_string(" ");
6174 if (results
->size() == 1)
6175 exp
->write_type(results
->begin()->type());
6179 exp
->write_c_string("(");
6180 for (Typed_identifier_list::const_iterator p
=
6182 p
!= results
->end();
6188 exp
->write_c_string(", ");
6189 exp
->write_type(p
->type());
6191 exp
->write_c_string(")");
6195 exp
->write_c_string("; ");
6199 exp
->write_c_string("}");
6202 // Import an interface type.
6205 Interface_type::do_import(Import
* imp
)
6207 imp
->require_c_string("interface { ");
6209 Typed_identifier_list
* methods
= new Typed_identifier_list
;
6210 while (imp
->peek_char() != '}')
6212 std::string name
= imp
->read_identifier();
6213 imp
->require_c_string(" (");
6215 Typed_identifier_list
* parameters
;
6216 bool is_varargs
= false;
6217 if (imp
->peek_char() == ')')
6221 parameters
= new Typed_identifier_list
;
6224 if (imp
->match_c_string("..."))
6230 Type
* ptype
= imp
->read_type();
6232 ptype
= Type::make_array_type(ptype
, NULL
);
6233 parameters
->push_back(Typed_identifier(Import::import_marker
,
6234 ptype
, imp
->location()));
6235 if (imp
->peek_char() != ',')
6237 gcc_assert(!is_varargs
);
6238 imp
->require_c_string(", ");
6241 imp
->require_c_string(")");
6243 Typed_identifier_list
* results
;
6244 if (imp
->peek_char() != ' ')
6248 results
= new Typed_identifier_list
;
6250 if (imp
->peek_char() != '(')
6252 Type
* rtype
= imp
->read_type();
6253 results
->push_back(Typed_identifier(Import::import_marker
,
6254 rtype
, imp
->location()));
6261 Type
* rtype
= imp
->read_type();
6262 results
->push_back(Typed_identifier(Import::import_marker
,
6263 rtype
, imp
->location()));
6264 if (imp
->peek_char() != ',')
6266 imp
->require_c_string(", ");
6268 imp
->require_c_string(")");
6272 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
6276 fntype
->set_is_varargs();
6277 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
6279 imp
->require_c_string("; ");
6282 imp
->require_c_string("}");
6284 if (methods
->empty())
6290 return Type::make_interface_type(methods
, imp
->location());
6293 // Make an interface type.
6296 Type::make_interface_type(Typed_identifier_list
* methods
,
6297 source_location location
)
6299 return new Interface_type(methods
, location
);
6304 // Bind a method to an object.
6307 Method::bind_method(Expression
* expr
, source_location location
) const
6309 if (this->stub_
== NULL
)
6311 // When there is no stub object, the binding is determined by
6313 return this->do_bind_method(expr
, location
);
6316 Expression
* func
= Expression::make_func_reference(this->stub_
, NULL
,
6318 return Expression::make_bound_method(expr
, func
, location
);
6321 // Return the named object associated with a method. This may only be
6322 // called after methods are finalized.
6325 Method::named_object() const
6327 if (this->stub_
!= NULL
)
6329 return this->do_named_object();
6332 // Class Named_method.
6334 // The type of the method.
6337 Named_method::do_type() const
6339 if (this->named_object_
->is_function())
6340 return this->named_object_
->func_value()->type();
6341 else if (this->named_object_
->is_function_declaration())
6342 return this->named_object_
->func_declaration_value()->type();
6347 // Return the location of the method receiver.
6350 Named_method::do_receiver_location() const
6352 return this->do_type()->receiver()->location();
6355 // Bind a method to an object.
6358 Named_method::do_bind_method(Expression
* expr
, source_location location
) const
6360 Expression
* func
= Expression::make_func_reference(this->named_object_
, NULL
,
6362 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, func
,
6364 // If this is not a local method, and it does not use a stub, then
6365 // the real method expects a different type. We need to cast the
6367 if (this->depth() > 0 && !this->needs_stub_method())
6369 Function_type
* ftype
= this->do_type();
6370 gcc_assert(ftype
->is_method());
6371 Type
* frtype
= ftype
->receiver()->type();
6372 bme
->set_first_argument_type(frtype
);
6377 // Class Interface_method.
6379 // Bind a method to an object.
6382 Interface_method::do_bind_method(Expression
* expr
,
6383 source_location location
) const
6385 return Expression::make_interface_field_reference(expr
, this->name_
,
6391 // Insert a new method. Return true if it was inserted, false
6395 Methods::insert(const std::string
& name
, Method
* m
)
6397 std::pair
<Method_map::iterator
, bool> ins
=
6398 this->methods_
.insert(std::make_pair(name
, m
));
6403 Method
* old_method
= ins
.first
->second
;
6404 if (m
->depth() < old_method
->depth())
6407 ins
.first
->second
= m
;
6412 if (m
->depth() == old_method
->depth())
6413 old_method
->set_is_ambiguous();
6419 // Return the number of unambiguous methods.
6422 Methods::count() const
6425 for (Method_map::const_iterator p
= this->methods_
.begin();
6426 p
!= this->methods_
.end();
6428 if (!p
->second
->is_ambiguous())
6433 // Class Named_type.
6435 // Return the name of the type.
6438 Named_type::name() const
6440 return this->named_object_
->name();
6443 // Return the name of the type to use in an error message.
6446 Named_type::message_name() const
6448 return this->named_object_
->message_name();
6451 // Add a method to this type.
6454 Named_type::add_method(const std::string
& name
, Function
* function
)
6456 if (this->local_methods_
== NULL
)
6457 this->local_methods_
= new Bindings(NULL
);
6458 return this->local_methods_
->add_function(name
, NULL
, function
);
6461 // Add a method declaration to this type.
6464 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
6465 Function_type
* type
,
6466 source_location location
)
6468 if (this->local_methods_
== NULL
)
6469 this->local_methods_
= new Bindings(NULL
);
6470 return this->local_methods_
->add_function_declaration(name
, package
, type
,
6474 // Add an existing method to this type.
6477 Named_type::add_existing_method(Named_object
* no
)
6479 if (this->local_methods_
== NULL
)
6480 this->local_methods_
= new Bindings(NULL
);
6481 this->local_methods_
->add_named_object(no
);
6484 // Look for a local method NAME, and returns its named object, or NULL
6488 Named_type::find_local_method(const std::string
& name
) const
6490 if (this->local_methods_
== NULL
)
6492 return this->local_methods_
->lookup(name
);
6495 // Return whether NAME is an unexported field or method, for better
6499 Named_type::is_unexported_local_method(Gogo
* gogo
,
6500 const std::string
& name
) const
6502 Bindings
* methods
= this->local_methods_
;
6503 if (methods
!= NULL
)
6505 for (Bindings::const_declarations_iterator p
=
6506 methods
->begin_declarations();
6507 p
!= methods
->end_declarations();
6510 if (Gogo::is_hidden_name(p
->first
)
6511 && name
== Gogo::unpack_hidden_name(p
->first
)
6512 && gogo
->pack_hidden_name(name
, false) != p
->first
)
6519 // Build the complete list of methods for this type, which means
6520 // recursively including all methods for anonymous fields. Create all
6524 Named_type::finalize_methods(Gogo
* gogo
)
6526 if (this->local_methods_
!= NULL
6527 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
6529 const Bindings
* lm
= this->local_methods_
;
6530 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
6531 p
!= lm
->end_declarations();
6533 error_at(p
->second
->location(),
6534 "invalid pointer or interface receiver type");
6535 delete this->local_methods_
;
6536 this->local_methods_
= NULL
;
6540 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6543 // Return the method NAME, or NULL if there isn't one or if it is
6544 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6548 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6550 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6553 // Return a pointer to the interface method table for this type for
6554 // the interface INTERFACE. IS_POINTER is true if this is for a
6558 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
6561 gcc_assert(!interface
->is_empty());
6563 Interface_method_tables
** pimt
= (is_pointer
6564 ? &this->interface_method_tables_
6565 : &this->pointer_interface_method_tables_
);
6568 *pimt
= new Interface_method_tables(5);
6570 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
6571 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
6575 // This is a new entry in the hash table.
6576 gcc_assert(ins
.first
->second
== NULL_TREE
);
6577 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
6582 tree decl
= ins
.first
->second
;
6583 if (decl
== error_mark_node
)
6584 return error_mark_node
;
6585 gcc_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
6586 return build_fold_addr_expr(decl
);
6589 // Return whether a named type has any hidden fields.
6592 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
6597 bool ret
= this->type_
->has_hidden_fields(this, reason
);
6598 this->seen_
= false;
6602 // Look for a use of a complete type within another type. This is
6603 // used to check that we don't try to use a type within itself.
6605 class Find_type_use
: public Traverse
6608 Find_type_use(Type
* find_type
)
6609 : Traverse(traverse_types
),
6610 find_type_(find_type
), found_(false)
6613 // Whether we found the type.
6616 { return this->found_
; }
6623 // The type we are looking for.
6625 // Whether we found the type.
6629 // Check for FIND_TYPE in TYPE.
6632 Find_type_use::type(Type
* type
)
6634 if (this->find_type_
== type
)
6636 this->found_
= true;
6637 return TRAVERSE_EXIT
;
6639 // It's OK if we see a reference to the type in any type which is
6640 // essentially a pointer: a pointer, a slice, a function, a map, or
6642 if (type
->points_to() != NULL
6643 || type
->is_open_array_type()
6644 || type
->function_type() != NULL
6645 || type
->map_type() != NULL
6646 || type
->channel_type() != NULL
)
6647 return TRAVERSE_SKIP_COMPONENTS
;
6649 // For an interface, a reference to the type in a method type should
6650 // be ignored, but we have to consider direct inheritance. When
6651 // this is called, there may be cases of direct inheritance
6652 // represented as a method with no name.
6653 if (type
->interface_type() != NULL
)
6655 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
6656 if (methods
!= NULL
)
6658 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6659 p
!= methods
->end();
6662 if (p
->name().empty())
6664 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
6665 return TRAVERSE_EXIT
;
6669 return TRAVERSE_SKIP_COMPONENTS
;
6672 return TRAVERSE_CONTINUE
;
6675 // Verify that a named type does not refer to itself.
6678 Named_type::do_verify()
6680 Find_type_use
find(this);
6681 Type::traverse(this->type_
, &find
);
6684 error_at(this->location_
, "invalid recursive type %qs",
6685 this->message_name().c_str());
6686 this->is_error_
= true;
6690 // Check whether any of the local methods overloads an existing
6691 // struct field or interface method. We don't need to check the
6692 // list of methods against itself: that is handled by the Bindings
6694 if (this->local_methods_
!= NULL
)
6696 Struct_type
* st
= this->type_
->struct_type();
6697 Interface_type
* it
= this->type_
->interface_type();
6698 bool found_dup
= false;
6699 if (st
!= NULL
|| it
!= NULL
)
6701 for (Bindings::const_declarations_iterator p
=
6702 this->local_methods_
->begin_declarations();
6703 p
!= this->local_methods_
->end_declarations();
6706 const std::string
& name(p
->first
);
6707 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
6709 error_at(p
->second
->location(),
6710 "method %qs redeclares struct field name",
6711 Gogo::message_name(name
).c_str());
6714 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
6716 error_at(p
->second
->location(),
6717 "method %qs redeclares interface method name",
6718 Gogo::message_name(name
).c_str());
6730 // Return a hash code. This is used for method lookup. We simply
6731 // hash on the name itself.
6734 Named_type::do_hash_for_method(Gogo
* gogo
) const
6736 const std::string
& name(this->named_object()->name());
6737 unsigned int ret
= Type::hash_string(name
, 0);
6739 // GOGO will be NULL here when called from Type_hash_identical.
6740 // That is OK because that is only used for internal hash tables
6741 // where we are going to be comparing named types for equality. In
6742 // other cases, which are cases where the runtime is going to
6743 // compare hash codes to see if the types are the same, we need to
6744 // include the package prefix and name in the hash.
6745 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
6747 const Package
* package
= this->named_object()->package();
6748 if (package
== NULL
)
6750 ret
= Type::hash_string(gogo
->unique_prefix(), ret
);
6751 ret
= Type::hash_string(gogo
->package_name(), ret
);
6755 ret
= Type::hash_string(package
->unique_prefix(), ret
);
6756 ret
= Type::hash_string(package
->name(), ret
);
6763 // Get a tree for a named type.
6766 Named_type::do_get_tree(Gogo
* gogo
)
6768 if (this->is_error_
)
6769 return error_mark_node
;
6771 // Go permits types to refer to themselves in various ways. Break
6772 // the recursion here.
6774 switch (this->type_
->forwarded()->classification())
6777 return error_mark_node
;
6786 // These types can not refer to themselves.
6789 // All maps and channels have the same type in GENERIC.
6790 t
= Type::get_named_type_tree(gogo
, this->type_
);
6791 if (t
== error_mark_node
)
6792 return error_mark_node
;
6793 // Build a copy to set TYPE_NAME.
6794 t
= build_variant_type_copy(t
);
6798 // GENERIC can't handle a pointer to a function type whose
6799 // return type is a pointer to the function type itself. It
6800 // does into infinite loops when walking the types.
6802 && this->function_type()->results() != NULL
6803 && this->function_type()->results()->size() == 1
6804 && (this->function_type()->results()->front().type()->forwarded()
6806 return ptr_type_node
;
6808 t
= Type::get_named_type_tree(gogo
, this->type_
);
6809 this->seen_
= false;
6810 if (t
== error_mark_node
)
6811 return error_mark_node
;
6812 t
= build_variant_type_copy(t
);
6816 // GENERIC can't handle a pointer type which points to itself.
6817 // It goes into infinite loops when walking the types.
6818 if (this->seen_
&& this->points_to()->forwarded() == this)
6819 return ptr_type_node
;
6821 t
= Type::get_named_type_tree(gogo
, this->type_
);
6822 this->seen_
= false;
6823 if (t
== error_mark_node
)
6824 return error_mark_node
;
6825 t
= build_variant_type_copy(t
);
6829 if (this->named_tree_
!= NULL_TREE
)
6830 return this->named_tree_
;
6831 t
= make_node(RECORD_TYPE
);
6832 this->named_tree_
= t
;
6833 this->type_
->struct_type()->fill_in_tree(gogo
, t
);
6837 if (!this->is_open_array_type())
6838 t
= Type::get_named_type_tree(gogo
, this->type_
);
6841 if (this->named_tree_
!= NULL_TREE
)
6842 return this->named_tree_
;
6843 t
= gogo
->slice_type_tree(void_type_node
);
6844 this->named_tree_
= t
;
6845 t
= this->type_
->array_type()->fill_in_tree(gogo
, t
);
6847 if (t
== error_mark_node
)
6848 return error_mark_node
;
6849 t
= build_variant_type_copy(t
);
6852 case TYPE_INTERFACE
:
6853 if (this->type_
->interface_type()->is_empty())
6855 t
= Type::get_named_type_tree(gogo
, this->type_
);
6856 if (t
== error_mark_node
)
6857 return error_mark_node
;
6858 t
= build_variant_type_copy(t
);
6862 if (this->named_tree_
!= NULL_TREE
)
6863 return this->named_tree_
;
6864 t
= make_node(RECORD_TYPE
);
6865 this->named_tree_
= t
;
6866 t
= this->type_
->interface_type()->fill_in_tree(gogo
, t
);
6872 // When a named type T1 is defined as another named type T2,
6873 // the definition must simply be "type T1 T2". If the
6874 // definition of T2 may refer to T1, then we must simply
6875 // return the type for T2 here. It's not precisely correct,
6876 // but it's as close as we can get with GENERIC.
6877 bool was_seen
= this->seen_
;
6879 t
= Type::get_named_type_tree(gogo
, this->type_
);
6880 this->seen_
= was_seen
;
6883 if (t
== error_mark_node
)
6884 return error_mark_node
;
6885 t
= build_variant_type_copy(t
);
6890 // An undefined forwarding type. Make sure the error is
6892 this->type_
->forward_declaration_type()->real_type();
6893 return error_mark_node
;
6897 case TYPE_CALL_MULTIPLE_RESULT
:
6901 tree id
= this->named_object_
->get_id(gogo
);
6902 tree decl
= build_decl(this->location_
, TYPE_DECL
, id
, t
);
6903 TYPE_NAME(t
) = decl
;
6908 // Build a type descriptor for a named type.
6911 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6913 // If NAME is not NULL, then we don't really want the type
6914 // descriptor for this type; we want the descriptor for the
6915 // underlying type, giving it the name NAME.
6916 return this->named_type_descriptor(gogo
, this->type_
,
6917 name
== NULL
? this : name
);
6920 // Add to the reflection string. This is used mostly for the name of
6921 // the type used in a type descriptor, not for actual reflection
6925 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6927 if (this->location() != BUILTINS_LOCATION
)
6929 const Package
* package
= this->named_object_
->package();
6930 if (package
!= NULL
)
6931 ret
->append(package
->name());
6933 ret
->append(gogo
->package_name());
6934 ret
->push_back('.');
6936 if (this->in_function_
!= NULL
)
6938 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
6939 ret
->push_back('$');
6941 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
6944 // Get the mangled name.
6947 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6949 Named_object
* no
= this->named_object_
;
6951 if (this->location() == BUILTINS_LOCATION
)
6952 gcc_assert(this->in_function_
== NULL
);
6955 const std::string
& unique_prefix(no
->package() == NULL
6956 ? gogo
->unique_prefix()
6957 : no
->package()->unique_prefix());
6958 const std::string
& package_name(no
->package() == NULL
6959 ? gogo
->package_name()
6960 : no
->package()->name());
6961 name
= unique_prefix
;
6962 name
.append(1, '.');
6963 name
.append(package_name
);
6964 name
.append(1, '.');
6965 if (this->in_function_
!= NULL
)
6967 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
6968 name
.append(1, '$');
6971 name
.append(Gogo::unpack_hidden_name(no
->name()));
6973 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
6978 // Export the type. This is called to export a global type.
6981 Named_type::export_named_type(Export
* exp
, const std::string
&) const
6983 // We don't need to write the name of the type here, because it will
6984 // be written by Export::write_type anyhow.
6985 exp
->write_c_string("type ");
6986 exp
->write_type(this);
6987 exp
->write_c_string(";\n");
6990 // Import a named type.
6993 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
6995 imp
->require_c_string("type ");
6996 Type
*type
= imp
->read_type();
6997 *ptype
= type
->named_type();
6998 gcc_assert(*ptype
!= NULL
);
6999 imp
->require_c_string(";\n");
7002 // Export the type when it is referenced by another type. In this
7003 // case Export::export_type will already have issued the name.
7006 Named_type::do_export(Export
* exp
) const
7008 exp
->write_type(this->type_
);
7010 // To save space, we only export the methods directly attached to
7012 Bindings
* methods
= this->local_methods_
;
7013 if (methods
== NULL
)
7016 exp
->write_c_string("\n");
7017 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
7018 p
!= methods
->end_definitions();
7021 exp
->write_c_string(" ");
7022 (*p
)->export_named_object(exp
);
7025 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
7026 p
!= methods
->end_declarations();
7029 if (p
->second
->is_function_declaration())
7031 exp
->write_c_string(" ");
7032 p
->second
->export_named_object(exp
);
7037 // Make a named type.
7040 Type::make_named_type(Named_object
* named_object
, Type
* type
,
7041 source_location location
)
7043 return new Named_type(named_object
, type
, location
);
7046 // Finalize the methods for TYPE. It will be a named type or a struct
7047 // type. This sets *ALL_METHODS to the list of methods, and builds
7048 // all required stubs.
7051 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, source_location location
,
7052 Methods
** all_methods
)
7054 *all_methods
= NULL
;
7055 Types_seen types_seen
;
7056 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
7058 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
7061 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7062 // build up the struct field indexes as we go. DEPTH is the depth of
7063 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7064 // adding these methods for an anonymous field with pointer type.
7065 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7066 // calls the real method. TYPES_SEEN is used to avoid infinite
7070 Type::add_methods_for_type(const Type
* type
,
7071 const Method::Field_indexes
* field_indexes
,
7073 bool is_embedded_pointer
,
7074 bool needs_stub_method
,
7075 Types_seen
* types_seen
,
7078 // Pointer types may not have methods.
7079 if (type
->points_to() != NULL
)
7082 const Named_type
* nt
= type
->named_type();
7085 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
7091 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
7092 is_embedded_pointer
, needs_stub_method
,
7095 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
7096 is_embedded_pointer
, needs_stub_method
,
7097 types_seen
, methods
);
7099 // If we are called with depth > 0, then we are looking at an
7100 // anonymous field of a struct. If such a field has interface type,
7101 // then we need to add the interface methods. We don't want to add
7102 // them when depth == 0, because we will already handle them
7103 // following the usual rules for an interface type.
7105 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
7108 // Add the local methods for the named type NT to *METHODS. The
7109 // parameters are as for add_methods_to_type.
7112 Type::add_local_methods_for_type(const Named_type
* nt
,
7113 const Method::Field_indexes
* field_indexes
,
7115 bool is_embedded_pointer
,
7116 bool needs_stub_method
,
7119 const Bindings
* local_methods
= nt
->local_methods();
7120 if (local_methods
== NULL
)
7123 if (*methods
== NULL
)
7124 *methods
= new Methods();
7126 for (Bindings::const_declarations_iterator p
=
7127 local_methods
->begin_declarations();
7128 p
!= local_methods
->end_declarations();
7131 Named_object
* no
= p
->second
;
7132 bool is_value_method
= (is_embedded_pointer
7133 || !Type::method_expects_pointer(no
));
7134 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
7136 || (depth
> 0 && is_value_method
)));
7137 if (!(*methods
)->insert(no
->name(), m
))
7142 // Add the embedded methods for TYPE to *METHODS. These are the
7143 // methods attached to anonymous fields. The parameters are as for
7144 // add_methods_to_type.
7147 Type::add_embedded_methods_for_type(const Type
* type
,
7148 const Method::Field_indexes
* field_indexes
,
7150 bool is_embedded_pointer
,
7151 bool needs_stub_method
,
7152 Types_seen
* types_seen
,
7155 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7157 const Struct_type
* st
= type
->struct_type();
7161 const Struct_field_list
* fields
= st
->fields();
7166 for (Struct_field_list::const_iterator pf
= fields
->begin();
7167 pf
!= fields
->end();
7170 if (!pf
->is_anonymous())
7173 Type
* ftype
= pf
->type();
7174 bool is_pointer
= false;
7175 if (ftype
->points_to() != NULL
)
7177 ftype
= ftype
->points_to();
7180 Named_type
* fnt
= ftype
->named_type();
7183 // This is an error, but it will be diagnosed elsewhere.
7187 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
7188 sub_field_indexes
->next
= field_indexes
;
7189 sub_field_indexes
->field_index
= i
;
7191 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
7192 (is_embedded_pointer
|| is_pointer
),
7201 // If TYPE is an interface type, then add its method to *METHODS.
7202 // This is for interface methods attached to an anonymous field. The
7203 // parameters are as for add_methods_for_type.
7206 Type::add_interface_methods_for_type(const Type
* type
,
7207 const Method::Field_indexes
* field_indexes
,
7211 const Interface_type
* it
= type
->interface_type();
7215 const Typed_identifier_list
* imethods
= it
->methods();
7216 if (imethods
== NULL
)
7219 if (*methods
== NULL
)
7220 *methods
= new Methods();
7222 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
7223 pm
!= imethods
->end();
7226 Function_type
* fntype
= pm
->type()->function_type();
7227 gcc_assert(fntype
!= NULL
&& !fntype
->is_method());
7228 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
7229 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
7230 field_indexes
, depth
);
7231 if (!(*methods
)->insert(pm
->name(), m
))
7236 // Build stub methods for TYPE as needed. METHODS is the set of
7237 // methods for the type. A stub method may be needed when a type
7238 // inherits a method from an anonymous field. When we need the
7239 // address of the method, as in a type descriptor, we need to build a
7240 // little stub which does the required field dereferences and jumps to
7241 // the real method. LOCATION is the location of the type definition.
7244 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
7245 source_location location
)
7247 if (methods
== NULL
)
7249 for (Methods::const_iterator p
= methods
->begin();
7250 p
!= methods
->end();
7253 Method
* m
= p
->second
;
7254 if (m
->is_ambiguous() || !m
->needs_stub_method())
7257 const std::string
& name(p
->first
);
7259 // Build a stub method.
7261 const Function_type
* fntype
= m
->type();
7263 static unsigned int counter
;
7265 snprintf(buf
, sizeof buf
, "$this%u", counter
);
7268 Type
* receiver_type
= const_cast<Type
*>(type
);
7269 if (!m
->is_value_method())
7270 receiver_type
= Type::make_pointer_type(receiver_type
);
7271 source_location receiver_location
= m
->receiver_location();
7272 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
7275 const Typed_identifier_list
* fnparams
= fntype
->parameters();
7276 Typed_identifier_list
* stub_params
;
7277 if (fnparams
== NULL
|| fnparams
->empty())
7281 // We give each stub parameter a unique name.
7282 stub_params
= new Typed_identifier_list();
7283 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
7284 pp
!= fnparams
->end();
7288 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
7289 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
7295 const Typed_identifier_list
* fnresults
= fntype
->results();
7296 Typed_identifier_list
* stub_results
;
7297 if (fnresults
== NULL
|| fnresults
->empty())
7298 stub_results
= NULL
;
7301 // We create the result parameters without any names, since
7302 // we won't refer to them.
7303 stub_results
= new Typed_identifier_list();
7304 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
7305 pr
!= fnresults
->end();
7307 stub_results
->push_back(Typed_identifier("", pr
->type(),
7311 Function_type
* stub_type
= Type::make_function_type(receiver
,
7314 fntype
->location());
7315 if (fntype
->is_varargs())
7316 stub_type
->set_is_varargs();
7318 // We only create the function in the package which creates the
7320 const Package
* package
;
7321 if (type
->named_type() == NULL
)
7324 package
= type
->named_type()->named_object()->package();
7326 if (package
!= NULL
)
7327 stub
= Named_object::make_function_declaration(name
, package
,
7328 stub_type
, location
);
7331 stub
= gogo
->start_function(name
, stub_type
, false,
7332 fntype
->location());
7333 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
7334 fntype
->is_varargs(), location
);
7335 gogo
->finish_function(fntype
->location());
7338 m
->set_stub_object(stub
);
7342 // Build a stub method which adjusts the receiver as required to call
7343 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7344 // PARAMS is the list of function parameters.
7347 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
7348 const char* receiver_name
,
7349 const Typed_identifier_list
* params
,
7351 source_location location
)
7353 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
7354 gcc_assert(receiver_object
!= NULL
);
7356 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
7357 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
7358 if (expr
->type()->points_to() == NULL
)
7359 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7361 Expression_list
* arguments
;
7362 if (params
== NULL
|| params
->empty())
7366 arguments
= new Expression_list();
7367 for (Typed_identifier_list::const_iterator p
= params
->begin();
7371 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
7372 gcc_assert(param
!= NULL
);
7373 Expression
* param_ref
= Expression::make_var_reference(param
,
7375 arguments
->push_back(param_ref
);
7379 Expression
* func
= method
->bind_method(expr
, location
);
7380 gcc_assert(func
!= NULL
);
7381 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
7383 size_t count
= call
->result_count();
7385 gogo
->add_statement(Statement::make_statement(call
));
7388 Expression_list
* retvals
= new Expression_list();
7390 retvals
->push_back(call
);
7393 for (size_t i
= 0; i
< count
; ++i
)
7394 retvals
->push_back(Expression::make_call_result(call
, i
));
7396 const Function
* function
= gogo
->current_function()->func_value();
7397 const Typed_identifier_list
* results
= function
->type()->results();
7398 Statement
* retstat
= Statement::make_return_statement(results
, retvals
,
7400 gogo
->add_statement(retstat
);
7404 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7405 // in reverse order.
7408 Type::apply_field_indexes(Expression
* expr
,
7409 const Method::Field_indexes
* field_indexes
,
7410 source_location location
)
7412 if (field_indexes
== NULL
)
7414 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
7415 Struct_type
* stype
= expr
->type()->deref()->struct_type();
7416 gcc_assert(stype
!= NULL
7417 && field_indexes
->field_index
< stype
->field_count());
7418 if (expr
->type()->struct_type() == NULL
)
7420 gcc_assert(expr
->type()->points_to() != NULL
);
7421 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7422 gcc_assert(expr
->type()->struct_type() == stype
);
7424 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
7428 // Return whether NO is a method for which the receiver is a pointer.
7431 Type::method_expects_pointer(const Named_object
* no
)
7433 const Function_type
*fntype
;
7434 if (no
->is_function())
7435 fntype
= no
->func_value()->type();
7436 else if (no
->is_function_declaration())
7437 fntype
= no
->func_declaration_value()->type();
7440 return fntype
->receiver()->type()->points_to() != NULL
;
7443 // Given a set of methods for a type, METHODS, return the method NAME,
7444 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7445 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7446 // but is ambiguous (and return NULL).
7449 Type::method_function(const Methods
* methods
, const std::string
& name
,
7452 if (is_ambiguous
!= NULL
)
7453 *is_ambiguous
= false;
7454 if (methods
== NULL
)
7456 Methods::const_iterator p
= methods
->find(name
);
7457 if (p
== methods
->end())
7459 Method
* m
= p
->second
;
7460 if (m
->is_ambiguous())
7462 if (is_ambiguous
!= NULL
)
7463 *is_ambiguous
= true;
7469 // Look for field or method NAME for TYPE. Return an Expression for
7470 // the field or method bound to EXPR. If there is no such field or
7471 // method, give an appropriate error and return an error expression.
7474 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
7475 const std::string
& name
,
7476 source_location location
)
7478 if (type
->is_error_type())
7479 return Expression::make_error(location
);
7481 const Named_type
* nt
= type
->named_type();
7483 nt
= type
->deref()->named_type();
7484 const Struct_type
* st
= type
->deref()->struct_type();
7485 const Interface_type
* it
= type
->deref()->interface_type();
7487 // If this is a pointer to a pointer, then it is possible that the
7488 // pointed-to type has methods.
7492 && type
->points_to() != NULL
7493 && type
->points_to()->points_to() != NULL
)
7495 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7496 type
= type
->points_to();
7497 nt
= type
->points_to()->named_type();
7498 st
= type
->points_to()->struct_type();
7499 it
= type
->points_to()->interface_type();
7502 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
7503 || expr
->is_addressable());
7504 bool is_method
= false;
7505 bool found_pointer_method
= false;
7508 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
, NULL
,
7509 &is_method
, &found_pointer_method
,
7515 gcc_assert(st
!= NULL
);
7516 if (type
->struct_type() == NULL
)
7518 gcc_assert(type
->points_to() != NULL
);
7519 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
7521 gcc_assert(expr
->type()->struct_type() == st
);
7523 ret
= st
->field_reference(expr
, name
, location
);
7525 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7526 ret
= Expression::make_interface_field_reference(expr
, name
,
7532 m
= nt
->method_function(name
, NULL
);
7533 else if (st
!= NULL
)
7534 m
= st
->method_function(name
, NULL
);
7537 gcc_assert(m
!= NULL
);
7538 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
7539 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7540 ret
= m
->bind_method(expr
, location
);
7542 gcc_assert(ret
!= NULL
);
7547 if (!ambig1
.empty())
7548 error_at(location
, "%qs is ambiguous via %qs and %qs",
7549 Gogo::message_name(name
).c_str(),
7550 Gogo::message_name(ambig1
).c_str(),
7551 Gogo::message_name(ambig2
).c_str());
7552 else if (found_pointer_method
)
7553 error_at(location
, "method requires a pointer");
7554 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
7556 ("reference to field %qs in object which "
7557 "has no fields or methods"),
7558 Gogo::message_name(name
).c_str());
7562 if (!Gogo::is_hidden_name(name
))
7563 is_unexported
= false;
7566 std::string unpacked
= Gogo::unpack_hidden_name(name
);
7567 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
7571 error_at(location
, "reference to unexported field or method %qs",
7572 Gogo::message_name(name
).c_str());
7574 error_at(location
, "reference to undefined field or method %qs",
7575 Gogo::message_name(name
).c_str());
7577 return Expression::make_error(location
);
7581 // Look in TYPE for a field or method named NAME, return true if one
7582 // is found. This looks through embedded anonymous fields and handles
7583 // ambiguity. If a method is found, sets *IS_METHOD to true;
7584 // otherwise, if a field is found, set it to false. If
7585 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7586 // whose address can not be taken. When returning false, this sets
7587 // *FOUND_POINTER_METHOD if we found a method we couldn't use because
7588 // it requires a pointer. LEVEL is used for recursive calls, and can
7589 // be NULL for a non-recursive call. When this function returns false
7590 // because it finds that the name is ambiguous, it will store a path
7591 // to the ambiguous names in *AMBIG1 and *AMBIG2. If the name is not
7592 // found at all, *AMBIG1 and *AMBIG2 will be unchanged.
7594 // This function just returns whether or not there is a field or
7595 // method, and whether it is a field or method. It doesn't build an
7596 // expression to refer to it. If it is a method, we then look in the
7597 // list of all methods for the type. If it is a field, the search has
7598 // to be done again, looking only for fields, and building up the
7599 // expression as we go.
7602 Type::find_field_or_method(const Type
* type
,
7603 const std::string
& name
,
7604 bool receiver_can_be_pointer
,
7607 bool* found_pointer_method
,
7608 std::string
* ambig1
,
7609 std::string
* ambig2
)
7611 // Named types can have locally defined methods.
7612 const Named_type
* nt
= type
->named_type();
7613 if (nt
== NULL
&& type
->points_to() != NULL
)
7614 nt
= type
->points_to()->named_type();
7617 Named_object
* no
= nt
->find_local_method(name
);
7620 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
7626 // Record that we have found a pointer method in order to
7627 // give a better error message if we don't find anything
7629 *found_pointer_method
= true;
7633 // Interface types can have methods.
7634 const Interface_type
* it
= type
->deref()->interface_type();
7635 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7641 // Struct types can have fields. They can also inherit fields and
7642 // methods from anonymous fields.
7643 const Struct_type
* st
= type
->deref()->struct_type();
7646 const Struct_field_list
* fields
= st
->fields();
7650 int found_level
= 0;
7651 bool found_is_method
= false;
7652 std::string found_ambig1
;
7653 std::string found_ambig2
;
7654 const Struct_field
* found_parent
= NULL
;
7655 for (Struct_field_list::const_iterator pf
= fields
->begin();
7656 pf
!= fields
->end();
7659 if (pf
->field_name() == name
)
7665 if (!pf
->is_anonymous())
7668 Named_type
* fnt
= pf
->type()->deref()->named_type();
7669 gcc_assert(fnt
!= NULL
);
7671 int sublevel
= level
== NULL
? 1 : *level
+ 1;
7673 std::string subambig1
;
7674 std::string subambig2
;
7675 bool subfound
= Type::find_field_or_method(fnt
,
7677 receiver_can_be_pointer
,
7680 found_pointer_method
,
7685 if (!subambig1
.empty())
7687 // The name was found via this field, but is ambiguous.
7688 // if the ambiguity is lower or at the same level as
7689 // anything else we have already found, then we want to
7690 // pass the ambiguity back to the caller.
7691 if (found_level
== 0 || sublevel
<= found_level
)
7693 found_ambig1
= pf
->field_name() + '.' + subambig1
;
7694 found_ambig2
= pf
->field_name() + '.' + subambig2
;
7695 found_level
= sublevel
;
7701 // The name was found via this field. Use the level to see
7702 // if we want to use this one, or whether it introduces an
7704 if (found_level
== 0 || sublevel
< found_level
)
7706 found_level
= sublevel
;
7707 found_is_method
= sub_is_method
;
7708 found_ambig1
.clear();
7709 found_ambig2
.clear();
7710 found_parent
= &*pf
;
7712 else if (sublevel
> found_level
)
7714 else if (found_ambig1
.empty())
7716 // We found an ambiguity.
7717 gcc_assert(found_parent
!= NULL
);
7718 found_ambig1
= found_parent
->field_name();
7719 found_ambig2
= pf
->field_name();
7723 // We found an ambiguity, but we already know of one.
7724 // Just report the earlier one.
7729 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7730 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7731 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7732 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7734 if (found_level
== 0)
7736 else if (!found_ambig1
.empty())
7738 gcc_assert(!found_ambig1
.empty());
7739 ambig1
->assign(found_ambig1
);
7740 ambig2
->assign(found_ambig2
);
7742 *level
= found_level
;
7748 *level
= found_level
;
7749 *is_method
= found_is_method
;
7754 // Return whether NAME is an unexported field or method for TYPE.
7757 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
7758 const std::string
& name
)
7760 type
= type
->deref();
7762 const Named_type
* nt
= type
->named_type();
7763 if (nt
!= NULL
&& nt
->is_unexported_local_method(gogo
, name
))
7766 const Interface_type
* it
= type
->interface_type();
7767 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
7770 const Struct_type
* st
= type
->struct_type();
7771 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
7777 const Struct_field_list
* fields
= st
->fields();
7781 for (Struct_field_list::const_iterator pf
= fields
->begin();
7782 pf
!= fields
->end();
7785 if (pf
->is_anonymous())
7787 Named_type
* subtype
= pf
->type()->deref()->named_type();
7788 gcc_assert(subtype
!= NULL
);
7789 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
))
7797 // Class Forward_declaration.
7799 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
7800 : Type(TYPE_FORWARD
),
7801 named_object_(named_object
->resolve()), warned_(false)
7803 gcc_assert(this->named_object_
->is_unknown()
7804 || this->named_object_
->is_type_declaration());
7807 // Return the named object.
7810 Forward_declaration_type::named_object()
7812 return this->named_object_
->resolve();
7816 Forward_declaration_type::named_object() const
7818 return this->named_object_
->resolve();
7821 // Return the name of the forward declared type.
7824 Forward_declaration_type::name() const
7826 return this->named_object()->name();
7829 // Warn about a use of a type which has been declared but not defined.
7832 Forward_declaration_type::warn() const
7834 Named_object
* no
= this->named_object_
->resolve();
7835 if (no
->is_unknown())
7837 // The name was not defined anywhere.
7840 error_at(this->named_object_
->location(),
7841 "use of undefined type %qs",
7842 no
->message_name().c_str());
7843 this->warned_
= true;
7846 else if (no
->is_type_declaration())
7848 // The name was seen as a type, but the type was never defined.
7849 if (no
->type_declaration_value()->using_type())
7851 error_at(this->named_object_
->location(),
7852 "use of undefined type %qs",
7853 no
->message_name().c_str());
7854 this->warned_
= true;
7859 // The name was defined, but not as a type.
7862 error_at(this->named_object_
->location(), "expected type");
7863 this->warned_
= true;
7868 // Get the base type of a declaration. This gives an error if the
7869 // type has not yet been defined.
7872 Forward_declaration_type::real_type()
7874 if (this->is_defined())
7875 return this->named_object()->type_value();
7879 return Type::make_error_type();
7884 Forward_declaration_type::real_type() const
7886 if (this->is_defined())
7887 return this->named_object()->type_value();
7891 return Type::make_error_type();
7895 // Return whether the base type is defined.
7898 Forward_declaration_type::is_defined() const
7900 return this->named_object()->is_type();
7903 // Add a method. This is used when methods are defined before the
7907 Forward_declaration_type::add_method(const std::string
& name
,
7910 Named_object
* no
= this->named_object();
7911 gcc_assert(no
->is_type_declaration());
7912 return no
->type_declaration_value()->add_method(name
, function
);
7915 // Add a method declaration. This is used when methods are declared
7919 Forward_declaration_type::add_method_declaration(const std::string
& name
,
7920 Function_type
* type
,
7921 source_location location
)
7923 Named_object
* no
= this->named_object();
7924 gcc_assert(no
->is_type_declaration());
7925 Type_declaration
* td
= no
->type_declaration_value();
7926 return td
->add_method_declaration(name
, type
, location
);
7932 Forward_declaration_type::do_traverse(Traverse
* traverse
)
7934 if (this->is_defined()
7935 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
7936 return TRAVERSE_EXIT
;
7937 return TRAVERSE_CONTINUE
;
7940 // Get a tree for the type.
7943 Forward_declaration_type::do_get_tree(Gogo
* gogo
)
7945 if (this->is_defined())
7946 return Type::get_named_type_tree(gogo
, this->real_type());
7949 return error_mark_node
;
7951 // We represent an undefined type as a struct with no fields. That
7952 // should work fine for the middle-end, since the same case can
7954 Named_object
* no
= this->named_object();
7955 tree type_tree
= make_node(RECORD_TYPE
);
7956 tree id
= no
->get_id(gogo
);
7957 tree decl
= build_decl(no
->location(), TYPE_DECL
, id
, type_tree
);
7958 TYPE_NAME(type_tree
) = decl
;
7962 // Build a type descriptor for a forwarded type.
7965 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7967 if (!this->is_defined())
7968 return Expression::make_nil(BUILTINS_LOCATION
);
7971 Type
* t
= this->real_type();
7973 return this->named_type_descriptor(gogo
, t
, name
);
7975 return Expression::make_type_descriptor(t
, BUILTINS_LOCATION
);
7979 // The reflection string.
7982 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7984 this->append_reflection(this->real_type(), gogo
, ret
);
7987 // The mangled name.
7990 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7992 if (this->is_defined())
7993 this->append_mangled_name(this->real_type(), gogo
, ret
);
7996 const Named_object
* no
= this->named_object();
7998 if (no
->package() == NULL
)
7999 name
= gogo
->package_name();
8001 name
= no
->package()->name();
8003 name
+= Gogo::unpack_hidden_name(no
->name());
8005 snprintf(buf
, sizeof buf
, "N%u_",
8006 static_cast<unsigned int>(name
.length()));
8012 // Export a forward declaration. This can happen when a defined type
8013 // refers to a type which is only declared (and is presumably defined
8014 // in some other file in the same package).
8017 Forward_declaration_type::do_export(Export
*) const
8019 // If there is a base type, that should be exported instead of this.
8020 gcc_assert(!this->is_defined());
8022 // We don't output anything.
8025 // Make a forward declaration.
8028 Type::make_forward_declaration(Named_object
* named_object
)
8030 return new Forward_declaration_type(named_object
);
8033 // Class Typed_identifier_list.
8035 // Sort the entries by name.
8037 struct Typed_identifier_list_sort
8041 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
8042 { return t1
.name() < t2
.name(); }
8046 Typed_identifier_list::sort_by_name()
8048 std::sort(this->entries_
.begin(), this->entries_
.end(),
8049 Typed_identifier_list_sort());
8055 Typed_identifier_list::traverse(Traverse
* traverse
)
8057 for (Typed_identifier_list::const_iterator p
= this->begin();
8061 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
8062 return TRAVERSE_EXIT
;
8064 return TRAVERSE_CONTINUE
;
8069 Typed_identifier_list
*
8070 Typed_identifier_list::copy() const
8072 Typed_identifier_list
* ret
= new Typed_identifier_list();
8073 for (Typed_identifier_list::const_iterator p
= this->begin();
8076 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));