1 // types.cc -- Go frontend types.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
23 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
31 #include "statements.h"
38 Type::Type(Type_classification classification
)
39 : classification_(classification
), tree_(NULL_TREE
),
40 type_descriptor_decl_(NULL_TREE
)
48 // Get the base type for a type--skip names and forward declarations.
53 switch (this->classification_
)
56 return this->named_type()->named_base();
58 return this->forward_declaration_type()->real_type()->base();
67 switch (this->classification_
)
70 return this->named_type()->named_base();
72 return this->forward_declaration_type()->real_type()->base();
78 // Skip defined forward declarations.
84 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
85 while (ftype
!= NULL
&& ftype
->is_defined())
87 t
= ftype
->real_type();
88 ftype
= t
->forward_declaration_type();
94 Type::forwarded() const
97 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
98 while (ftype
!= NULL
&& ftype
->is_defined())
100 t
= ftype
->real_type();
101 ftype
= t
->forward_declaration_type();
106 // If this is a named type, return it. Otherwise, return NULL.
111 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
115 Type::named_type() const
117 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
120 // Return true if this type is not defined.
123 Type::is_undefined() const
125 return this->forwarded()->forward_declaration_type() != NULL
;
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
132 Type::is_basic_type() const
134 switch (this->classification_
)
157 return this->base()->is_basic_type();
164 // Return true if this is an abstract type.
167 Type::is_abstract() const
169 switch (this->classification())
172 return this->integer_type()->is_abstract();
174 return this->float_type()->is_abstract();
176 return this->complex_type()->is_abstract();
178 return this->is_abstract_string_type();
180 return this->is_abstract_boolean_type();
186 // Return a non-abstract version of an abstract type.
189 Type::make_non_abstract_type()
191 gcc_assert(this->is_abstract());
192 switch (this->classification())
195 return Type::lookup_integer_type("int");
197 return Type::lookup_float_type("float");
199 return Type::lookup_complex_type("complex");
201 return Type::lookup_string_type();
203 return Type::lookup_bool_type();
209 // Return true if this is an error type. Don't give an error if we
210 // try to dereference an undefined forwarding type, as this is called
211 // in the parser when the type may legitimately be undefined.
214 Type::is_error_type() const
216 const Type
* t
= this->forwarded();
217 // Note that we return false for an undefined forward type.
218 switch (t
->classification_
)
223 return t
->named_type()->is_named_error_type();
229 // If this is a pointer type, return the type to which it points.
230 // Otherwise, return NULL.
233 Type::points_to() const
235 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
237 return ptype
== NULL
? NULL
: ptype
->points_to();
240 // Return whether this is an open array type.
243 Type::is_open_array_type() const
245 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
248 // Return whether this is the predeclared constant nil being used as a
252 Type::is_nil_constant_as_type() const
254 const Type
* t
= this->forwarded();
255 if (t
->forward_declaration_type() != NULL
)
257 const Named_object
* no
= t
->forward_declaration_type()->named_object();
258 if (no
->is_unknown())
259 no
= no
->unknown_value()->real_named_object();
262 && no
->const_value()->expr()->is_nil_expression())
271 Type::traverse(Type
* type
, Traverse
* traverse
)
273 gcc_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
274 || (traverse
->traverse_mask()
275 & Traverse::traverse_expressions
) != 0);
276 if (traverse
->remember_type(type
))
278 // We have already traversed this type.
279 return TRAVERSE_CONTINUE
;
281 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
283 int t
= traverse
->type(type
);
284 if (t
== TRAVERSE_EXIT
)
285 return TRAVERSE_EXIT
;
286 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
287 return TRAVERSE_CONTINUE
;
289 // An array type has an expression which we need to traverse if
290 // traverse_expressions is set.
291 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
292 return TRAVERSE_EXIT
;
293 return TRAVERSE_CONTINUE
;
296 // Default implementation for do_traverse for child class.
299 Type::do_traverse(Traverse
*)
301 return TRAVERSE_CONTINUE
;
304 // Return whether two types are identical. If REASON is not NULL,
305 // optionally set *REASON to the reason the types are not identical.
308 Type::are_identical(const Type
* t1
, const Type
* t2
, std::string
* reason
)
310 if (t1
== NULL
|| t2
== NULL
)
312 // Something is wrong. Return true to avoid cascading errors.
316 // Skip defined forward declarations.
317 t1
= t1
->forwarded();
318 t2
= t2
->forwarded();
323 // An undefined forward declaration is an error, so we return true
324 // to avoid cascading errors.
325 if (t1
->forward_declaration_type() != NULL
326 || t2
->forward_declaration_type() != NULL
)
329 // Avoid cascading errors with error types.
330 if (t1
->is_error_type() || t2
->is_error_type())
333 // Get a good reason for the sink type. Note that the sink type on
334 // the left hand side of an assignment is handled in are_assignable.
335 if (t1
->is_sink_type() || t2
->is_sink_type())
338 *reason
= "invalid use of _";
342 // A named type is only identical to itself.
343 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
346 // Check type shapes.
347 if (t1
->classification() != t2
->classification())
350 switch (t1
->classification())
356 // These types are always identical.
360 return t1
->integer_type()->is_identical(t2
->integer_type());
363 return t1
->float_type()->is_identical(t2
->float_type());
366 return t1
->complex_type()->is_identical(t2
->complex_type());
369 return t1
->function_type()->is_identical(t2
->function_type(),
374 return Type::are_identical(t1
->points_to(), t2
->points_to(), reason
);
377 return t1
->struct_type()->is_identical(t2
->struct_type());
380 return t1
->array_type()->is_identical(t2
->array_type());
383 return t1
->map_type()->is_identical(t2
->map_type());
386 return t1
->channel_type()->is_identical(t2
->channel_type());
389 return t1
->interface_type()->is_identical(t2
->interface_type());
396 // Return true if it's OK to have a binary operation with types LHS
397 // and RHS. This is not used for shifts or comparisons.
400 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
402 if (Type::are_identical(lhs
, rhs
, NULL
))
405 // A constant of abstract bool type may be mixed with any bool type.
406 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
407 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
410 // A constant of abstract string type may be mixed with any string
412 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
413 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
419 // A constant of abstract integer, float, or complex type may be
420 // mixed with an integer, float, or complex type.
421 if ((rhs
->is_abstract()
422 && (rhs
->integer_type() != NULL
423 || rhs
->float_type() != NULL
424 || rhs
->complex_type() != NULL
)
425 && (lhs
->integer_type() != NULL
426 || lhs
->float_type() != NULL
427 || lhs
->complex_type() != NULL
))
428 || (lhs
->is_abstract()
429 && (lhs
->integer_type() != NULL
430 || lhs
->float_type() != NULL
431 || lhs
->complex_type() != NULL
)
432 && (rhs
->integer_type() != NULL
433 || rhs
->float_type() != NULL
434 || rhs
->complex_type() != NULL
)))
437 // The nil type may be compared to a pointer, an interface type, a
438 // slice type, a channel type, a map type, or a function type.
439 if (lhs
->is_nil_type()
440 && (rhs
->points_to() != NULL
441 || rhs
->interface_type() != NULL
442 || rhs
->is_open_array_type()
443 || rhs
->map_type() != NULL
444 || rhs
->channel_type() != NULL
445 || rhs
->function_type() != NULL
))
447 if (rhs
->is_nil_type()
448 && (lhs
->points_to() != NULL
449 || lhs
->interface_type() != NULL
450 || lhs
->is_open_array_type()
451 || lhs
->map_type() != NULL
452 || lhs
->channel_type() != NULL
453 || lhs
->function_type() != NULL
))
459 // Return true if a value with type RHS may be assigned to a variable
460 // with type LHS. If REASON is not NULL, set *REASON to the reason
461 // the types are not assignable.
464 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
466 // Do some checks first. Make sure the types are defined.
467 if (lhs
!= NULL
&& lhs
->forwarded()->forward_declaration_type() == NULL
)
469 // Any value may be assigned to the blank identifier.
470 if (lhs
->is_sink_type())
473 // All fields of a struct must be exported, or the assignment
474 // must be in the same package.
475 if (rhs
!= NULL
&& rhs
->forwarded()->forward_declaration_type() == NULL
)
477 if (lhs
->has_hidden_fields(NULL
, reason
)
478 || rhs
->has_hidden_fields(NULL
, reason
))
483 // Identical types are assignable.
484 if (Type::are_identical(lhs
, rhs
, reason
))
487 // The types are assignable if they have identical underlying types
488 // and either LHS or RHS is not a named type.
489 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
490 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
491 && Type::are_identical(lhs
->base(), rhs
->base(), reason
))
494 // The types are assignable if LHS is an interface type and RHS
495 // implements the required methods.
496 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
497 if (lhs_interface_type
!= NULL
)
499 if (lhs_interface_type
->implements_interface(rhs
, reason
))
501 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
502 if (rhs_interface_type
!= NULL
503 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
508 // The type are assignable if RHS is a bidirectional channel type,
509 // LHS is a channel type, they have identical element types, and
510 // either LHS or RHS is not a named type.
511 if (lhs
->channel_type() != NULL
512 && rhs
->channel_type() != NULL
513 && rhs
->channel_type()->may_send()
514 && rhs
->channel_type()->may_receive()
515 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
516 && Type::are_identical(lhs
->channel_type()->element_type(),
517 rhs
->channel_type()->element_type(),
521 // The nil type may be assigned to a pointer, function, slice, map,
522 // channel, or interface type.
523 if (rhs
->is_nil_type()
524 && (lhs
->points_to() != NULL
525 || lhs
->function_type() != NULL
526 || lhs
->is_open_array_type()
527 || lhs
->map_type() != NULL
528 || lhs
->channel_type() != NULL
529 || lhs
->interface_type() != NULL
))
532 // An untyped constant may be assigned to a numeric type if it is
533 // representable in that type.
534 if (rhs
->is_abstract()
535 && (lhs
->integer_type() != NULL
536 || lhs
->float_type() != NULL
537 || lhs
->complex_type() != NULL
))
541 // Give some better error messages.
542 if (reason
!= NULL
&& reason
->empty())
544 if (rhs
->interface_type() != NULL
)
545 reason
->assign(_("need explicit conversion"));
546 else if (rhs
->is_call_multiple_result_type())
547 reason
->assign(_("multiple value function call in "
548 "single value context"));
549 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
551 size_t len
= (lhs
->named_type()->name().length()
552 + rhs
->named_type()->name().length()
554 char* buf
= new char[len
];
555 snprintf(buf
, len
, _("cannot use type %s as type %s"),
556 rhs
->named_type()->message_name().c_str(),
557 lhs
->named_type()->message_name().c_str());
566 // Return true if a value with type RHS may be converted to type LHS.
567 // If REASON is not NULL, set *REASON to the reason the types are not
571 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
573 // The types are convertible if they are assignable.
574 if (Type::are_assignable(lhs
, rhs
, reason
))
577 // The types are convertible if they have identical underlying
579 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
580 && Type::are_identical(lhs
->base(), rhs
->base(), reason
))
583 // The types are convertible if they are both unnamed pointer types
584 // and their pointer base types have identical underlying types.
585 if (lhs
->named_type() == NULL
586 && rhs
->named_type() == NULL
587 && lhs
->points_to() != NULL
588 && rhs
->points_to() != NULL
589 && (lhs
->points_to()->named_type() != NULL
590 || rhs
->points_to()->named_type() != NULL
)
591 && Type::are_identical(lhs
->points_to()->base(),
592 rhs
->points_to()->base(),
596 // Integer and floating point types are convertible to each other.
597 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
598 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
601 // Complex types are convertible to each other.
602 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
605 // An integer, or []byte, or []int, may be converted to a string.
606 if (lhs
->is_string_type())
608 if (rhs
->integer_type() != NULL
)
610 if (rhs
->is_open_array_type() && rhs
->named_type() == NULL
)
612 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
613 if (e
->integer_type() != NULL
614 && (e
== Type::lookup_integer_type("uint8")
615 || e
== Type::lookup_integer_type("int")))
620 // A string may be converted to []byte or []int.
621 if (rhs
->is_string_type()
622 && lhs
->is_open_array_type()
623 && lhs
->named_type() == NULL
)
625 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
626 if (e
->integer_type() != NULL
627 && (e
== Type::lookup_integer_type("uint8")
628 || e
== Type::lookup_integer_type("int")))
632 // An unsafe.Pointer type may be converted to any pointer type or to
633 // uintptr, and vice-versa.
634 if (lhs
->is_unsafe_pointer_type()
635 && (rhs
->points_to() != NULL
636 || (rhs
->integer_type() != NULL
637 && rhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
639 if (rhs
->is_unsafe_pointer_type()
640 && (lhs
->points_to() != NULL
641 || (lhs
->integer_type() != NULL
642 && lhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
645 // Give a better error message.
649 *reason
= "invalid type conversion";
652 std::string s
= "invalid type conversion (";
662 // Return whether this type has any hidden fields. This is only a
663 // possibility for a few types.
666 Type::has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
668 switch (this->forwarded()->classification_
)
671 return this->named_type()->named_type_has_hidden_fields(reason
);
673 return this->struct_type()->struct_has_hidden_fields(within
, reason
);
675 return this->array_type()->array_has_hidden_fields(within
, reason
);
681 // Return a hash code for the type to be used for method lookup.
684 Type::hash_for_method(Gogo
* gogo
) const
686 unsigned int ret
= 0;
687 if (this->classification_
!= TYPE_FORWARD
)
688 ret
+= this->classification_
;
689 return ret
+ this->do_hash_for_method(gogo
);
692 // Default implementation of do_hash_for_method. This is appropriate
693 // for types with no subfields.
696 Type::do_hash_for_method(Gogo
*) const
701 // Return a hash code for a string, given a starting hash.
704 Type::hash_string(const std::string
& s
, unsigned int h
)
706 const char* p
= s
.data();
707 size_t len
= s
.length();
708 for (; len
> 0; --len
)
716 // Default check for the expression passed to make. Any type which
717 // may be used with make implements its own version of this.
720 Type::do_check_make_expression(Expression_list
*, source_location
)
725 // Return whether an expression has an integer value. Report an error
726 // if not. This is used when handling calls to the predeclared make
730 Type::check_int_value(Expression
* e
, const char* errmsg
,
731 source_location location
)
733 if (e
->type()->integer_type() != NULL
)
736 // Check for a floating point constant with integer value.
741 if (e
->float_constant_value(fval
, &dummy
))
748 mpfr_clear_overflow();
749 mpfr_clear_erangeflag();
750 mpfr_get_z(ival
, fval
, GMP_RNDN
);
751 if (!mpfr_overflow_p()
752 && !mpfr_erangeflag_p()
753 && mpz_sgn(ival
) >= 0)
755 Named_type
* ntype
= Type::lookup_integer_type("int");
756 Integer_type
* inttype
= ntype
->integer_type();
758 mpz_init_set_ui(max
, 1);
759 mpz_mul_2exp(max
, max
, inttype
->bits() - 1);
760 ok
= mpz_cmp(ival
, max
) < 0;
774 error_at(location
, "%s", errmsg
);
778 // A hash table mapping unnamed types to trees.
780 Type::Type_trees
Type::type_trees
;
782 // Return a tree representing this type.
785 Type::get_tree(Gogo
* gogo
)
787 if (this->tree_
!= NULL
)
790 if (this->forward_declaration_type() != NULL
791 || this->named_type() != NULL
)
792 return this->get_tree_without_hash(gogo
);
794 // To avoid confusing GIMPLE, we need to translate all identical Go
795 // types to the same GIMPLE type. We use a hash table to do that.
796 // There is no need to use the hash table for named types, as named
797 // types are only identical to themselves.
799 std::pair
<Type
*, tree
> val(this, NULL
);
800 std::pair
<Type_trees::iterator
, bool> ins
=
801 Type::type_trees
.insert(val
);
802 if (!ins
.second
&& ins
.first
->second
!= NULL_TREE
)
804 this->tree_
= ins
.first
->second
;
808 tree t
= this->get_tree_without_hash(gogo
);
810 if (ins
.first
->second
== NULL_TREE
)
811 ins
.first
->second
= t
;
814 // We have already created a tree for this type. This can
815 // happen when an unnamed type is defined using a named type
816 // which in turns uses an identical unnamed type. Use the tree
817 // we created earlier and ignore the one we just built.
818 t
= ins
.first
->second
;
825 // Return a tree for a type without looking in the hash table for
826 // identical types. This is used for named types, since there is no
827 // point to looking in the hash table for them.
830 Type::get_tree_without_hash(Gogo
* gogo
)
832 if (this->tree_
== NULL_TREE
)
834 tree t
= this->do_get_tree(gogo
);
836 // For a recursive function or pointer type, we will temporarily
837 // return ptr_type_node during the recursion. We don't want to
838 // record that for a forwarding type, as it may confuse us
840 if (t
== ptr_type_node
&& this->forward_declaration_type() != NULL
)
844 go_preserve_from_gc(t
);
850 // Return a tree representing a zero initialization for this type.
853 Type::get_init_tree(Gogo
* gogo
, bool is_clear
)
855 tree type_tree
= this->get_tree(gogo
);
856 if (type_tree
== error_mark_node
)
857 return error_mark_node
;
858 return this->do_get_init_tree(gogo
, type_tree
, is_clear
);
861 // Any type which supports the builtin make function must implement
865 Type::do_make_expression_tree(Translate_context
*, Expression_list
*,
871 // Return a pointer to the type descriptor for this type.
874 Type::type_descriptor_pointer(Gogo
* gogo
)
876 Type
* t
= this->forwarded();
877 if (t
->type_descriptor_decl_
== NULL_TREE
)
879 Expression
* e
= t
->do_type_descriptor(gogo
, NULL
);
880 gogo
->build_type_descriptor_decl(t
, e
, &t
->type_descriptor_decl_
);
881 gcc_assert(t
->type_descriptor_decl_
!= NULL_TREE
882 && (t
->type_descriptor_decl_
== error_mark_node
883 || DECL_P(t
->type_descriptor_decl_
)));
885 if (t
->type_descriptor_decl_
== error_mark_node
)
886 return error_mark_node
;
887 return build_fold_addr_expr(t
->type_descriptor_decl_
);
890 // Return a composite literal for a type descriptor.
893 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
895 return type
->do_type_descriptor(gogo
, NULL
);
898 // Return a composite literal for a type descriptor with a name.
901 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
903 gcc_assert(name
!= NULL
&& type
->named_type() != name
);
904 return type
->do_type_descriptor(gogo
, name
);
907 // Make a builtin struct type from a list of fields. The fields are
908 // pairs of a name and a type.
911 Type::make_builtin_struct_type(int nfields
, ...)
914 va_start(ap
, nfields
);
916 source_location bloc
= BUILTINS_LOCATION
;
917 Struct_field_list
* sfl
= new Struct_field_list();
918 for (int i
= 0; i
< nfields
; i
++)
920 const char* field_name
= va_arg(ap
, const char *);
921 Type
* type
= va_arg(ap
, Type
*);
922 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
927 return Type::make_struct_type(sfl
, bloc
);
930 // Make a builtin named type.
933 Type::make_builtin_named_type(const char* name
, Type
* type
)
935 source_location bloc
= BUILTINS_LOCATION
;
936 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
937 return no
->type_value();
940 // Return the type of a type descriptor. We should really tie this to
941 // runtime.Type rather than copying it. This must match commonType in
942 // libgo/go/runtime/type.go.
945 Type::make_type_descriptor_type()
950 source_location bloc
= BUILTINS_LOCATION
;
952 Type
* uint8_type
= Type::lookup_integer_type("uint8");
953 Type
* uint32_type
= Type::lookup_integer_type("uint32");
954 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
955 Type
* string_type
= Type::lookup_string_type();
956 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
958 // This is an unnamed version of unsafe.Pointer. Perhaps we
959 // should use the named version instead, although that would
960 // require us to create the unsafe package if it has not been
961 // imported. It probably doesn't matter.
962 Type
* void_type
= Type::make_void_type();
963 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
965 // Forward declaration for the type descriptor type.
966 Named_object
* named_type_descriptor_type
=
967 Named_object::make_type_declaration("commonType", NULL
, bloc
);
968 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
969 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
971 // The type of a method on a concrete type.
972 Struct_type
* method_type
=
973 Type::make_builtin_struct_type(5,
974 "name", pointer_string_type
,
975 "pkgPath", pointer_string_type
,
976 "mtyp", pointer_type_descriptor_type
,
977 "typ", pointer_type_descriptor_type
,
978 "tfn", unsafe_pointer_type
);
979 Named_type
* named_method_type
=
980 Type::make_builtin_named_type("method", method_type
);
982 // Information for types with a name or methods.
983 Type
* slice_named_method_type
=
984 Type::make_array_type(named_method_type
, NULL
);
985 Struct_type
* uncommon_type
=
986 Type::make_builtin_struct_type(3,
987 "name", pointer_string_type
,
988 "pkgPath", pointer_string_type
,
989 "methods", slice_named_method_type
);
990 Named_type
* named_uncommon_type
=
991 Type::make_builtin_named_type("uncommonType", uncommon_type
);
993 Type
* pointer_uncommon_type
=
994 Type::make_pointer_type(named_uncommon_type
);
996 // The type descriptor type.
998 Typed_identifier_list
* params
= new Typed_identifier_list();
999 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1000 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1002 Typed_identifier_list
* results
= new Typed_identifier_list();
1003 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1005 Type
* hashfn_type
= Type::make_function_type(NULL
, params
, results
, bloc
);
1007 params
= new Typed_identifier_list();
1008 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1009 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1010 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1012 results
= new Typed_identifier_list();
1013 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1015 Type
* equalfn_type
= Type::make_function_type(NULL
, params
, results
,
1018 Struct_type
* type_descriptor_type
=
1019 Type::make_builtin_struct_type(9,
1021 "align", uint8_type
,
1022 "fieldAlign", uint8_type
,
1023 "size", uintptr_type
,
1024 "hash", uint32_type
,
1025 "hashfn", hashfn_type
,
1026 "equalfn", equalfn_type
,
1027 "string", pointer_string_type
,
1028 "", pointer_uncommon_type
);
1030 Named_type
* named
= Type::make_builtin_named_type("commonType",
1031 type_descriptor_type
);
1033 named_type_descriptor_type
->set_type_value(named
);
1041 // Make the type of a pointer to a type descriptor as represented in
1045 Type::make_type_descriptor_ptr_type()
1049 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1053 // Return the names of runtime functions which compute a hash code for
1054 // this type and which compare whether two values of this type are
1058 Type::type_functions(const char** hash_fn
, const char** equal_fn
) const
1060 switch (this->base()->classification())
1062 case Type::TYPE_ERROR
:
1063 case Type::TYPE_VOID
:
1064 case Type::TYPE_NIL
:
1065 // These types can not be hashed or compared.
1066 *hash_fn
= "__go_type_hash_error";
1067 *equal_fn
= "__go_type_equal_error";
1070 case Type::TYPE_BOOLEAN
:
1071 case Type::TYPE_INTEGER
:
1072 case Type::TYPE_FLOAT
:
1073 case Type::TYPE_COMPLEX
:
1074 case Type::TYPE_POINTER
:
1075 case Type::TYPE_FUNCTION
:
1076 case Type::TYPE_MAP
:
1077 case Type::TYPE_CHANNEL
:
1078 *hash_fn
= "__go_type_hash_identity";
1079 *equal_fn
= "__go_type_equal_identity";
1082 case Type::TYPE_STRING
:
1083 *hash_fn
= "__go_type_hash_string";
1084 *equal_fn
= "__go_type_equal_string";
1087 case Type::TYPE_STRUCT
:
1088 case Type::TYPE_ARRAY
:
1089 // These types can not be hashed or compared.
1090 *hash_fn
= "__go_type_hash_error";
1091 *equal_fn
= "__go_type_equal_error";
1094 case Type::TYPE_INTERFACE
:
1095 if (this->interface_type()->is_empty())
1097 *hash_fn
= "__go_type_hash_empty_interface";
1098 *equal_fn
= "__go_type_equal_empty_interface";
1102 *hash_fn
= "__go_type_hash_interface";
1103 *equal_fn
= "__go_type_equal_interface";
1107 case Type::TYPE_NAMED
:
1108 case Type::TYPE_FORWARD
:
1116 // Return a composite literal for the type descriptor for a plain type
1117 // of kind RUNTIME_TYPE_KIND named NAME.
1120 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1121 Named_type
* name
, const Methods
* methods
,
1122 bool only_value_methods
)
1124 source_location bloc
= BUILTINS_LOCATION
;
1126 Type
* td_type
= Type::make_type_descriptor_type();
1127 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1129 Expression_list
* vals
= new Expression_list();
1132 Struct_field_list::const_iterator p
= fields
->begin();
1133 gcc_assert(p
->field_name() == "Kind");
1135 mpz_init_set_ui(iv
, runtime_type_kind
);
1136 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1139 gcc_assert(p
->field_name() == "align");
1140 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1141 vals
->push_back(Expression::make_type_info(this, type_info
));
1144 gcc_assert(p
->field_name() == "fieldAlign");
1145 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1146 vals
->push_back(Expression::make_type_info(this, type_info
));
1149 gcc_assert(p
->field_name() == "size");
1150 type_info
= Expression::TYPE_INFO_SIZE
;
1151 vals
->push_back(Expression::make_type_info(this, type_info
));
1154 gcc_assert(p
->field_name() == "hash");
1155 mpz_set_ui(iv
, this->hash_for_method(gogo
));
1156 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1158 const char* hash_fn
;
1159 const char* equal_fn
;
1160 this->type_functions(&hash_fn
, &equal_fn
);
1163 gcc_assert(p
->field_name() == "hashfn");
1164 Function_type
* fntype
= p
->type()->function_type();
1165 Named_object
* no
= Named_object::make_function_declaration(hash_fn
, NULL
,
1168 no
->func_declaration_value()->set_asm_name(hash_fn
);
1169 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1172 gcc_assert(p
->field_name() == "equalfn");
1173 fntype
= p
->type()->function_type();
1174 no
= Named_object::make_function_declaration(equal_fn
, NULL
, fntype
, bloc
);
1175 no
->func_declaration_value()->set_asm_name(equal_fn
);
1176 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1179 gcc_assert(p
->field_name() == "string");
1180 Expression
* s
= Expression::make_string((name
!= NULL
1181 ? name
->reflection(gogo
)
1182 : this->reflection(gogo
)),
1184 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1187 gcc_assert(p
->field_name() == "uncommonType");
1188 if (name
== NULL
&& methods
== NULL
)
1189 vals
->push_back(Expression::make_nil(bloc
));
1192 if (methods
== NULL
)
1193 methods
= name
->methods();
1194 vals
->push_back(this->uncommon_type_constructor(gogo
,
1197 only_value_methods
));
1201 gcc_assert(p
== fields
->end());
1205 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
1208 // Return a composite literal for the uncommon type information for
1209 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1210 // struct. If name is not NULL, it is the name of the type. If
1211 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1212 // is true if only value methods should be included. At least one of
1213 // NAME and METHODS must not be NULL.
1216 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
1217 Named_type
* name
, const Methods
* methods
,
1218 bool only_value_methods
) const
1220 source_location bloc
= BUILTINS_LOCATION
;
1222 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
1224 Expression_list
* vals
= new Expression_list();
1227 Struct_field_list::const_iterator p
= fields
->begin();
1228 gcc_assert(p
->field_name() == "name");
1231 gcc_assert(p
->field_name() == "pkgPath");
1235 vals
->push_back(Expression::make_nil(bloc
));
1236 vals
->push_back(Expression::make_nil(bloc
));
1240 Named_object
* no
= name
->named_object();
1241 std::string n
= Gogo::unpack_hidden_name(no
->name());
1242 Expression
* s
= Expression::make_string(n
, bloc
);
1243 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1245 if (name
->is_builtin())
1246 vals
->push_back(Expression::make_nil(bloc
));
1249 const Package
* package
= no
->package();
1250 const std::string
& unique_prefix(package
== NULL
1251 ? gogo
->unique_prefix()
1252 : package
->unique_prefix());
1253 const std::string
& package_name(package
== NULL
1254 ? gogo
->package_name()
1256 n
.assign(unique_prefix
);
1258 n
.append(package_name
);
1259 if (name
->in_function() != NULL
)
1262 n
.append(Gogo::unpack_hidden_name(name
->in_function()->name()));
1264 s
= Expression::make_string(n
, bloc
);
1265 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1270 gcc_assert(p
->field_name() == "methods");
1271 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
1272 only_value_methods
));
1275 gcc_assert(p
== fields
->end());
1277 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
1279 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
1282 // Sort methods by name.
1288 operator()(const std::pair
<std::string
, const Method
*>& m1
,
1289 const std::pair
<std::string
, const Method
*>& m2
) const
1290 { return m1
.first
< m2
.first
; }
1293 // Return a composite literal for the type method table for this type.
1294 // METHODS_TYPE is the type of the table, and is a slice type.
1295 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1296 // then only value methods are used.
1299 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
1300 const Methods
* methods
,
1301 bool only_value_methods
) const
1303 source_location bloc
= BUILTINS_LOCATION
;
1305 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
1306 if (methods
!= NULL
)
1308 smethods
.reserve(methods
->count());
1309 for (Methods::const_iterator p
= methods
->begin();
1310 p
!= methods
->end();
1313 if (p
->second
->is_ambiguous())
1315 if (only_value_methods
&& !p
->second
->is_value_method())
1317 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
1321 if (smethods
.empty())
1322 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
1324 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
1326 Type
* method_type
= methods_type
->array_type()->element_type();
1328 Expression_list
* vals
= new Expression_list();
1329 vals
->reserve(smethods
.size());
1330 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
1332 p
!= smethods
.end();
1334 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
1337 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
1340 // Return a composite literal for a single method. METHOD_TYPE is the
1341 // type of the entry. METHOD_NAME is the name of the method and M is
1342 // the method information.
1345 Type::method_constructor(Gogo
*, Type
* method_type
,
1346 const std::string
& method_name
,
1347 const Method
* m
) const
1349 source_location bloc
= BUILTINS_LOCATION
;
1351 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
1353 Expression_list
* vals
= new Expression_list();
1356 Struct_field_list::const_iterator p
= fields
->begin();
1357 gcc_assert(p
->field_name() == "name");
1358 const std::string n
= Gogo::unpack_hidden_name(method_name
);
1359 Expression
* s
= Expression::make_string(n
, bloc
);
1360 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1363 gcc_assert(p
->field_name() == "pkgPath");
1364 if (!Gogo::is_hidden_name(method_name
))
1365 vals
->push_back(Expression::make_nil(bloc
));
1368 s
= Expression::make_string(Gogo::hidden_name_prefix(method_name
), bloc
);
1369 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1372 Named_object
* no
= (m
->needs_stub_method()
1374 : m
->named_object());
1376 Function_type
* mtype
;
1377 if (no
->is_function())
1378 mtype
= no
->func_value()->type();
1380 mtype
= no
->func_declaration_value()->type();
1381 gcc_assert(mtype
->is_method());
1382 Type
* nonmethod_type
= mtype
->copy_without_receiver();
1385 gcc_assert(p
->field_name() == "mtyp");
1386 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
1389 gcc_assert(p
->field_name() == "typ");
1390 vals
->push_back(Expression::make_type_descriptor(mtype
, bloc
));
1393 gcc_assert(p
->field_name() == "tfn");
1394 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1397 gcc_assert(p
== fields
->end());
1399 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
1402 // Return a composite literal for the type descriptor of a plain type.
1403 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1404 // NULL, it is the name to use as well as the list of methods.
1407 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
1410 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
1414 // Return the type reflection string for this type.
1417 Type::reflection(Gogo
* gogo
) const
1421 // The do_reflection virtual function should set RET to the
1422 // reflection string.
1423 this->do_reflection(gogo
, &ret
);
1428 // Return a mangled name for the type.
1431 Type::mangled_name(Gogo
* gogo
) const
1435 // The do_mangled_name virtual function should set RET to the
1436 // mangled name. For a composite type it should append a code for
1437 // the composition and then call do_mangled_name on the components.
1438 this->do_mangled_name(gogo
, &ret
);
1443 // Default function to export a type.
1446 Type::do_export(Export
*) const
1454 Type::import_type(Import
* imp
)
1456 if (imp
->match_c_string("("))
1457 return Function_type::do_import(imp
);
1458 else if (imp
->match_c_string("*"))
1459 return Pointer_type::do_import(imp
);
1460 else if (imp
->match_c_string("struct "))
1461 return Struct_type::do_import(imp
);
1462 else if (imp
->match_c_string("["))
1463 return Array_type::do_import(imp
);
1464 else if (imp
->match_c_string("map "))
1465 return Map_type::do_import(imp
);
1466 else if (imp
->match_c_string("chan "))
1467 return Channel_type::do_import(imp
);
1468 else if (imp
->match_c_string("interface"))
1469 return Interface_type::do_import(imp
);
1472 error_at(imp
->location(), "import error: expected type");
1473 return Type::make_error_type();
1477 // A type used to indicate a parsing error. This exists to simplify
1478 // later error detection.
1480 class Error_type
: public Type
1490 { return error_mark_node
; }
1493 do_get_init_tree(Gogo
*, tree
, bool)
1494 { return error_mark_node
; }
1497 do_type_descriptor(Gogo
*, Named_type
*)
1498 { return Expression::make_error(BUILTINS_LOCATION
); }
1501 do_reflection(Gogo
*, std::string
*) const
1502 { gcc_assert(saw_errors()); }
1505 do_mangled_name(Gogo
*, std::string
* ret
) const
1506 { ret
->push_back('E'); }
1510 Type::make_error_type()
1512 static Error_type singleton_error_type
;
1513 return &singleton_error_type
;
1518 class Void_type
: public Type
1528 { return void_type_node
; }
1531 do_get_init_tree(Gogo
*, tree
, bool)
1532 { gcc_unreachable(); }
1535 do_type_descriptor(Gogo
*, Named_type
*)
1536 { gcc_unreachable(); }
1539 do_reflection(Gogo
*, std::string
*) const
1543 do_mangled_name(Gogo
*, std::string
* ret
) const
1544 { ret
->push_back('v'); }
1548 Type::make_void_type()
1550 static Void_type singleton_void_type
;
1551 return &singleton_void_type
;
1554 // The boolean type.
1556 class Boolean_type
: public Type
1560 : Type(TYPE_BOOLEAN
)
1566 { return boolean_type_node
; }
1569 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1570 { return is_clear
? NULL
: fold_convert(type_tree
, boolean_false_node
); }
1573 do_type_descriptor(Gogo
*, Named_type
* name
);
1575 // We should not be asked for the reflection string of a basic type.
1577 do_reflection(Gogo
*, std::string
* ret
) const
1578 { ret
->append("bool"); }
1581 do_mangled_name(Gogo
*, std::string
* ret
) const
1582 { ret
->push_back('b'); }
1585 // Make the type descriptor.
1588 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1591 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
1594 Named_object
* no
= gogo
->lookup_global("bool");
1595 gcc_assert(no
!= NULL
);
1596 return Type::type_descriptor(gogo
, no
->type_value());
1601 Type::make_boolean_type()
1603 static Boolean_type boolean_type
;
1604 return &boolean_type
;
1607 // The named type "bool".
1609 static Named_type
* named_bool_type
;
1611 // Get the named type "bool".
1614 Type::lookup_bool_type()
1616 return named_bool_type
;
1619 // Make the named type "bool".
1622 Type::make_named_bool_type()
1624 Type
* bool_type
= Type::make_boolean_type();
1625 Named_object
* named_object
= Named_object::make_type("bool", NULL
,
1628 Named_type
* named_type
= named_object
->type_value();
1629 named_bool_type
= named_type
;
1633 // Class Integer_type.
1635 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
1637 // Create a new integer type. Non-abstract integer types always have
1641 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
1642 int bits
, int runtime_type_kind
)
1644 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
1646 std::string
sname(name
);
1647 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1650 Named_type
* named_type
= named_object
->type_value();
1651 std::pair
<Named_integer_types::iterator
, bool> ins
=
1652 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
1653 gcc_assert(ins
.second
);
1657 // Look up an existing integer type.
1660 Integer_type::lookup_integer_type(const char* name
)
1662 Named_integer_types::const_iterator p
=
1663 Integer_type::named_integer_types
.find(name
);
1664 gcc_assert(p
!= Integer_type::named_integer_types
.end());
1668 // Create a new abstract integer type.
1671 Integer_type::create_abstract_integer_type()
1673 static Integer_type
* abstract_type
;
1674 if (abstract_type
== NULL
)
1675 abstract_type
= new Integer_type(true, false, INT_TYPE_SIZE
,
1676 RUNTIME_TYPE_KIND_INT
);
1677 return abstract_type
;
1680 // Integer type compatibility.
1683 Integer_type::is_identical(const Integer_type
* t
) const
1685 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
1687 return this->is_abstract_
== t
->is_abstract_
;
1693 Integer_type::do_hash_for_method(Gogo
*) const
1695 return ((this->bits_
<< 4)
1696 + ((this->is_unsigned_
? 1 : 0) << 8)
1697 + ((this->is_abstract_
? 1 : 0) << 9));
1700 // Get the tree for an Integer_type.
1703 Integer_type::do_get_tree(Gogo
*)
1705 gcc_assert(!this->is_abstract_
);
1706 if (this->is_unsigned_
)
1708 if (this->bits_
== INT_TYPE_SIZE
)
1709 return unsigned_type_node
;
1710 else if (this->bits_
== CHAR_TYPE_SIZE
)
1711 return unsigned_char_type_node
;
1712 else if (this->bits_
== SHORT_TYPE_SIZE
)
1713 return short_unsigned_type_node
;
1714 else if (this->bits_
== LONG_TYPE_SIZE
)
1715 return long_unsigned_type_node
;
1716 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1717 return long_long_unsigned_type_node
;
1719 return make_unsigned_type(this->bits_
);
1723 if (this->bits_
== INT_TYPE_SIZE
)
1724 return integer_type_node
;
1725 else if (this->bits_
== CHAR_TYPE_SIZE
)
1726 return signed_char_type_node
;
1727 else if (this->bits_
== SHORT_TYPE_SIZE
)
1728 return short_integer_type_node
;
1729 else if (this->bits_
== LONG_TYPE_SIZE
)
1730 return long_integer_type_node
;
1731 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1732 return long_long_integer_type_node
;
1734 return make_signed_type(this->bits_
);
1739 Integer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1741 return is_clear
? NULL
: build_int_cst(type_tree
, 0);
1744 // The type descriptor for an integer type. Integer types are always
1748 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1750 gcc_assert(name
!= NULL
);
1751 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1754 // We should not be asked for the reflection string of a basic type.
1757 Integer_type::do_reflection(Gogo
*, std::string
*) const
1765 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1768 snprintf(buf
, sizeof buf
, "i%s%s%de",
1769 this->is_abstract_
? "a" : "",
1770 this->is_unsigned_
? "u" : "",
1775 // Make an integer type.
1778 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
1779 int runtime_type_kind
)
1781 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
1785 // Make an abstract integer type.
1788 Type::make_abstract_integer_type()
1790 return Integer_type::create_abstract_integer_type();
1793 // Look up an integer type.
1796 Type::lookup_integer_type(const char* name
)
1798 return Integer_type::lookup_integer_type(name
);
1801 // Class Float_type.
1803 Float_type::Named_float_types
Float_type::named_float_types
;
1805 // Create a new float type. Non-abstract float types always have
1809 Float_type::create_float_type(const char* name
, int bits
,
1810 int runtime_type_kind
)
1812 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
1813 std::string
sname(name
);
1814 Named_object
* named_object
= Named_object::make_type(sname
, NULL
, float_type
,
1816 Named_type
* named_type
= named_object
->type_value();
1817 std::pair
<Named_float_types::iterator
, bool> ins
=
1818 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
1819 gcc_assert(ins
.second
);
1823 // Look up an existing float type.
1826 Float_type::lookup_float_type(const char* name
)
1828 Named_float_types::const_iterator p
=
1829 Float_type::named_float_types
.find(name
);
1830 gcc_assert(p
!= Float_type::named_float_types
.end());
1834 // Create a new abstract float type.
1837 Float_type::create_abstract_float_type()
1839 static Float_type
* abstract_type
;
1840 if (abstract_type
== NULL
)
1841 abstract_type
= new Float_type(true, FLOAT_TYPE_SIZE
,
1842 RUNTIME_TYPE_KIND_FLOAT
);
1843 return abstract_type
;
1846 // Whether this type is identical with T.
1849 Float_type::is_identical(const Float_type
* t
) const
1851 if (this->bits_
!= t
->bits_
)
1853 return this->is_abstract_
== t
->is_abstract_
;
1859 Float_type::do_hash_for_method(Gogo
*) const
1861 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
1864 // Get a tree without using a Gogo*.
1867 Float_type::type_tree() const
1869 if (this->bits_
== FLOAT_TYPE_SIZE
)
1870 return float_type_node
;
1871 else if (this->bits_
== DOUBLE_TYPE_SIZE
)
1872 return double_type_node
;
1873 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
)
1874 return long_double_type_node
;
1877 tree ret
= make_node(REAL_TYPE
);
1878 TYPE_PRECISION(ret
) = this->bits_
;
1887 Float_type::do_get_tree(Gogo
*)
1889 return this->type_tree();
1893 Float_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1898 real_from_integer(&r
, TYPE_MODE(type_tree
), 0, 0, 0);
1899 return build_real(type_tree
, r
);
1902 // The type descriptor for a float type. Float types are always named.
1905 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1907 gcc_assert(name
!= NULL
);
1908 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1911 // We should not be asked for the reflection string of a basic type.
1914 Float_type::do_reflection(Gogo
*, std::string
*) const
1922 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1925 snprintf(buf
, sizeof buf
, "f%s%de",
1926 this->is_abstract_
? "a" : "",
1931 // Make a floating point type.
1934 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
1936 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
1939 // Make an abstract float type.
1942 Type::make_abstract_float_type()
1944 return Float_type::create_abstract_float_type();
1947 // Look up a float type.
1950 Type::lookup_float_type(const char* name
)
1952 return Float_type::lookup_float_type(name
);
1955 // Class Complex_type.
1957 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
1959 // Create a new complex type. Non-abstract complex types always have
1963 Complex_type::create_complex_type(const char* name
, int bits
,
1964 int runtime_type_kind
)
1966 Complex_type
* complex_type
= new Complex_type(false, bits
,
1968 std::string
sname(name
);
1969 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1972 Named_type
* named_type
= named_object
->type_value();
1973 std::pair
<Named_complex_types::iterator
, bool> ins
=
1974 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
1976 gcc_assert(ins
.second
);
1980 // Look up an existing complex type.
1983 Complex_type::lookup_complex_type(const char* name
)
1985 Named_complex_types::const_iterator p
=
1986 Complex_type::named_complex_types
.find(name
);
1987 gcc_assert(p
!= Complex_type::named_complex_types
.end());
1991 // Create a new abstract complex type.
1994 Complex_type::create_abstract_complex_type()
1996 static Complex_type
* abstract_type
;
1997 if (abstract_type
== NULL
)
1998 abstract_type
= new Complex_type(true, FLOAT_TYPE_SIZE
* 2,
1999 RUNTIME_TYPE_KIND_FLOAT
);
2000 return abstract_type
;
2003 // Whether this type is identical with T.
2006 Complex_type::is_identical(const Complex_type
*t
) const
2008 if (this->bits_
!= t
->bits_
)
2010 return this->is_abstract_
== t
->is_abstract_
;
2016 Complex_type::do_hash_for_method(Gogo
*) const
2018 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2021 // Get a tree without using a Gogo*.
2024 Complex_type::type_tree() const
2026 if (this->bits_
== FLOAT_TYPE_SIZE
* 2)
2027 return complex_float_type_node
;
2028 else if (this->bits_
== DOUBLE_TYPE_SIZE
* 2)
2029 return complex_double_type_node
;
2030 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
* 2)
2031 return complex_long_double_type_node
;
2034 tree ret
= make_node(REAL_TYPE
);
2035 TYPE_PRECISION(ret
) = this->bits_
/ 2;
2037 return build_complex_type(ret
);
2044 Complex_type::do_get_tree(Gogo
*)
2046 return this->type_tree();
2049 // Zero initializer.
2052 Complex_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2057 real_from_integer(&r
, TYPE_MODE(TREE_TYPE(type_tree
)), 0, 0, 0);
2058 return build_complex(type_tree
, build_real(TREE_TYPE(type_tree
), r
),
2059 build_real(TREE_TYPE(type_tree
), r
));
2062 // The type descriptor for a complex type. Complex types are always
2066 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2068 gcc_assert(name
!= NULL
);
2069 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2072 // We should not be asked for the reflection string of a basic type.
2075 Complex_type::do_reflection(Gogo
*, std::string
*) const
2083 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2086 snprintf(buf
, sizeof buf
, "c%s%de",
2087 this->is_abstract_
? "a" : "",
2092 // Make a complex type.
2095 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
2097 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
2100 // Make an abstract complex type.
2103 Type::make_abstract_complex_type()
2105 return Complex_type::create_abstract_complex_type();
2108 // Look up a complex type.
2111 Type::lookup_complex_type(const char* name
)
2113 return Complex_type::lookup_complex_type(name
);
2116 // Class String_type.
2118 // Return the tree for String_type. A string is a struct with two
2119 // fields: a pointer to the characters and a length.
2122 String_type::do_get_tree(Gogo
*)
2124 static tree struct_type
;
2125 return Gogo::builtin_struct(&struct_type
, "__go_string", NULL_TREE
, 2,
2127 build_pointer_type(unsigned_char_type_node
),
2132 // Return a tree for the length of STRING.
2135 String_type::length_tree(Gogo
*, tree string
)
2137 tree string_type
= TREE_TYPE(string
);
2138 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2139 tree length_field
= DECL_CHAIN(TYPE_FIELDS(string_type
));
2140 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field
)),
2142 return fold_build3(COMPONENT_REF
, integer_type_node
, string
,
2143 length_field
, NULL_TREE
);
2146 // Return a tree for a pointer to the bytes of STRING.
2149 String_type::bytes_tree(Gogo
*, tree string
)
2151 tree string_type
= TREE_TYPE(string
);
2152 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2153 tree bytes_field
= TYPE_FIELDS(string_type
);
2154 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field
)),
2156 return fold_build3(COMPONENT_REF
, TREE_TYPE(bytes_field
), string
,
2157 bytes_field
, NULL_TREE
);
2160 // We initialize a string to { NULL, 0 }.
2163 String_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2168 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2170 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2172 for (tree field
= TYPE_FIELDS(type_tree
);
2174 field
= DECL_CHAIN(field
))
2176 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2178 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
2181 tree ret
= build_constructor(type_tree
, init
);
2182 TREE_CONSTANT(ret
) = 1;
2186 // The type descriptor for the string type.
2189 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2192 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
2195 Named_object
* no
= gogo
->lookup_global("string");
2196 gcc_assert(no
!= NULL
);
2197 return Type::type_descriptor(gogo
, no
->type_value());
2201 // We should not be asked for the reflection string of a basic type.
2204 String_type::do_reflection(Gogo
*, std::string
* ret
) const
2206 ret
->append("string");
2209 // Mangled name of a string type.
2212 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2214 ret
->push_back('z');
2217 // Make a string type.
2220 Type::make_string_type()
2222 static String_type string_type
;
2223 return &string_type
;
2226 // The named type "string".
2228 static Named_type
* named_string_type
;
2230 // Get the named type "string".
2233 Type::lookup_string_type()
2235 return named_string_type
;
2238 // Make the named type string.
2241 Type::make_named_string_type()
2243 Type
* string_type
= Type::make_string_type();
2244 Named_object
* named_object
= Named_object::make_type("string", NULL
,
2247 Named_type
* named_type
= named_object
->type_value();
2248 named_string_type
= named_type
;
2252 // The sink type. This is the type of the blank identifier _. Any
2253 // type may be assigned to it.
2255 class Sink_type
: public Type
2265 { gcc_unreachable(); }
2268 do_get_init_tree(Gogo
*, tree
, bool)
2269 { gcc_unreachable(); }
2272 do_type_descriptor(Gogo
*, Named_type
*)
2273 { gcc_unreachable(); }
2276 do_reflection(Gogo
*, std::string
*) const
2277 { gcc_unreachable(); }
2280 do_mangled_name(Gogo
*, std::string
*) const
2281 { gcc_unreachable(); }
2284 // Make the sink type.
2287 Type::make_sink_type()
2289 static Sink_type sink_type
;
2293 // Class Function_type.
2298 Function_type::do_traverse(Traverse
* traverse
)
2300 if (this->receiver_
!= NULL
2301 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
2302 return TRAVERSE_EXIT
;
2303 if (this->parameters_
!= NULL
2304 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
2305 return TRAVERSE_EXIT
;
2306 if (this->results_
!= NULL
2307 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
2308 return TRAVERSE_EXIT
;
2309 return TRAVERSE_CONTINUE
;
2312 // Returns whether T is a valid redeclaration of this type. If this
2313 // returns false, and REASON is not NULL, *REASON may be set to a
2314 // brief explanation of why it returned false.
2317 Function_type::is_valid_redeclaration(const Function_type
* t
,
2318 std::string
* reason
) const
2320 if (!this->is_identical(t
, false, reason
))
2323 // A redeclaration of a function is required to use the same names
2324 // for the receiver and parameters.
2325 if (this->receiver() != NULL
2326 && this->receiver()->name() != t
->receiver()->name()
2327 && this->receiver()->name() != Import::import_marker
2328 && t
->receiver()->name() != Import::import_marker
)
2331 *reason
= "receiver name changed";
2335 const Typed_identifier_list
* parms1
= this->parameters();
2336 const Typed_identifier_list
* parms2
= t
->parameters();
2339 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2340 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2341 p2
!= parms2
->end();
2344 if (p1
->name() != p2
->name()
2345 && p1
->name() != Import::import_marker
2346 && p2
->name() != Import::import_marker
)
2349 *reason
= "parameter name changed";
2353 // This is called at parse time, so we may have unknown
2355 Type
* t1
= p1
->type()->forwarded();
2356 Type
* t2
= p2
->type()->forwarded();
2358 && t1
->forward_declaration_type() != NULL
2359 && (t2
->forward_declaration_type() == NULL
2360 || (t1
->forward_declaration_type()->named_object()
2361 != t2
->forward_declaration_type()->named_object())))
2366 const Typed_identifier_list
* results1
= this->results();
2367 const Typed_identifier_list
* results2
= t
->results();
2368 if (results1
!= NULL
)
2370 Typed_identifier_list::const_iterator res1
= results1
->begin();
2371 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2372 res2
!= results2
->end();
2375 if (res1
->name() != res2
->name()
2376 && res1
->name() != Import::import_marker
2377 && res2
->name() != Import::import_marker
)
2380 *reason
= "result name changed";
2384 // This is called at parse time, so we may have unknown
2386 Type
* t1
= res1
->type()->forwarded();
2387 Type
* t2
= res2
->type()->forwarded();
2389 && t1
->forward_declaration_type() != NULL
2390 && (t2
->forward_declaration_type() == NULL
2391 || (t1
->forward_declaration_type()->named_object()
2392 != t2
->forward_declaration_type()->named_object())))
2400 // Check whether T is the same as this type.
2403 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
2404 std::string
* reason
) const
2406 if (!ignore_receiver
)
2408 const Typed_identifier
* r1
= this->receiver();
2409 const Typed_identifier
* r2
= t
->receiver();
2410 if ((r1
!= NULL
) != (r2
!= NULL
))
2413 *reason
= _("different receiver types");
2418 if (!Type::are_identical(r1
->type(), r2
->type(), reason
))
2420 if (reason
!= NULL
&& !reason
->empty())
2421 *reason
= "receiver: " + *reason
;
2427 const Typed_identifier_list
* parms1
= this->parameters();
2428 const Typed_identifier_list
* parms2
= t
->parameters();
2429 if ((parms1
!= NULL
) != (parms2
!= NULL
))
2432 *reason
= _("different number of parameters");
2437 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2438 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2439 p2
!= parms2
->end();
2442 if (p1
== parms1
->end())
2445 *reason
= _("different number of parameters");
2449 if (!Type::are_identical(p1
->type(), p2
->type(), NULL
))
2452 *reason
= _("different parameter types");
2456 if (p1
!= parms1
->end())
2459 *reason
= _("different number of parameters");
2464 if (this->is_varargs() != t
->is_varargs())
2467 *reason
= _("different varargs");
2471 const Typed_identifier_list
* results1
= this->results();
2472 const Typed_identifier_list
* results2
= t
->results();
2473 if ((results1
!= NULL
) != (results2
!= NULL
))
2476 *reason
= _("different number of results");
2479 if (results1
!= NULL
)
2481 Typed_identifier_list::const_iterator res1
= results1
->begin();
2482 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2483 res2
!= results2
->end();
2486 if (res1
== results1
->end())
2489 *reason
= _("different number of results");
2493 if (!Type::are_identical(res1
->type(), res2
->type(), NULL
))
2496 *reason
= _("different result types");
2500 if (res1
!= results1
->end())
2503 *reason
= _("different number of results");
2514 Function_type::do_hash_for_method(Gogo
* gogo
) const
2516 unsigned int ret
= 0;
2517 // We ignore the receiver type for hash codes, because we need to
2518 // get the same hash code for a method in an interface and a method
2519 // declared for a type. The former will not have a receiver.
2520 if (this->parameters_
!= NULL
)
2523 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2524 p
!= this->parameters_
->end();
2526 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2528 if (this->results_
!= NULL
)
2531 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2532 p
!= this->results_
->end();
2534 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2536 if (this->is_varargs_
)
2542 // Get the tree for a function type.
2545 Function_type::do_get_tree(Gogo
* gogo
)
2547 tree args
= NULL_TREE
;
2550 if (this->receiver_
!= NULL
)
2552 Type
* rtype
= this->receiver_
->type();
2553 tree ptype
= rtype
->get_tree(gogo
);
2554 if (ptype
== error_mark_node
)
2555 return error_mark_node
;
2557 // We always pass the address of the receiver parameter, in
2558 // order to make interface calls work with unknown types.
2559 if (rtype
->points_to() == NULL
)
2560 ptype
= build_pointer_type(ptype
);
2562 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2563 pp
= &TREE_CHAIN (*pp
);
2566 if (this->parameters_
!= NULL
)
2568 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2569 p
!= this->parameters_
->end();
2572 tree ptype
= p
->type()->get_tree(gogo
);
2573 if (ptype
== error_mark_node
)
2574 return error_mark_node
;
2575 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2576 pp
= &TREE_CHAIN (*pp
);
2580 // Varargs is handled entirely at the Go level. At the tree level,
2581 // functions are not varargs.
2582 *pp
= void_list_node
;
2585 if (this->results_
== NULL
)
2586 result
= void_type_node
;
2587 else if (this->results_
->size() == 1)
2588 result
= this->results_
->begin()->type()->get_tree(gogo
);
2591 result
= make_node(RECORD_TYPE
);
2592 tree field_trees
= NULL_TREE
;
2593 tree
* pp
= &field_trees
;
2594 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2595 p
!= this->results_
->end();
2598 const std::string name
= (p
->name().empty()
2600 : Gogo::unpack_hidden_name(p
->name()));
2601 tree name_tree
= get_identifier_with_length(name
.data(),
2603 tree field_type_tree
= p
->type()->get_tree(gogo
);
2604 if (field_type_tree
== error_mark_node
)
2605 return error_mark_node
;
2606 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
2608 DECL_CONTEXT(field
) = result
;
2610 pp
= &DECL_CHAIN(field
);
2612 TYPE_FIELDS(result
) = field_trees
;
2613 layout_type(result
);
2616 if (result
== error_mark_node
)
2617 return error_mark_node
;
2619 tree fntype
= build_function_type(result
, args
);
2620 if (fntype
== error_mark_node
)
2623 return build_pointer_type(fntype
);
2626 // Functions are initialized to NULL.
2629 Function_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2633 return fold_convert(type_tree
, null_pointer_node
);
2636 // The type of a function type descriptor.
2639 Function_type::make_function_type_descriptor_type()
2644 Type
* tdt
= Type::make_type_descriptor_type();
2645 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
2647 Type
* bool_type
= Type::lookup_bool_type();
2649 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
2651 Struct_type
* s
= Type::make_builtin_struct_type(4,
2653 "dotdotdot", bool_type
,
2657 ret
= Type::make_builtin_named_type("FuncType", s
);
2663 // The type descriptor for a function type.
2666 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2668 source_location bloc
= BUILTINS_LOCATION
;
2670 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
2672 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
2674 Expression_list
* vals
= new Expression_list();
2677 Struct_field_list::const_iterator p
= fields
->begin();
2678 gcc_assert(p
->field_name() == "commonType");
2679 vals
->push_back(this->type_descriptor_constructor(gogo
,
2680 RUNTIME_TYPE_KIND_FUNC
,
2684 gcc_assert(p
->field_name() == "dotdotdot");
2685 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
2688 gcc_assert(p
->field_name() == "in");
2689 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
2690 this->parameters()));
2693 gcc_assert(p
->field_name() == "out");
2694 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
2698 gcc_assert(p
== fields
->end());
2700 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
2703 // Return a composite literal for the parameters or results of a type
2707 Function_type::type_descriptor_params(Type
* params_type
,
2708 const Typed_identifier
* receiver
,
2709 const Typed_identifier_list
* params
)
2711 source_location bloc
= BUILTINS_LOCATION
;
2713 if (receiver
== NULL
&& params
== NULL
)
2714 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
2716 Expression_list
* vals
= new Expression_list();
2717 vals
->reserve((params
== NULL
? 0 : params
->size())
2718 + (receiver
!= NULL
? 1 : 0));
2720 if (receiver
!= NULL
)
2722 Type
* rtype
= receiver
->type();
2723 // The receiver is always passed as a pointer. FIXME: Is this
2724 // right? Should that fact affect the type descriptor?
2725 if (rtype
->points_to() == NULL
)
2726 rtype
= Type::make_pointer_type(rtype
);
2727 vals
->push_back(Expression::make_type_descriptor(rtype
, bloc
));
2732 for (Typed_identifier_list::const_iterator p
= params
->begin();
2735 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
2738 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
2741 // The reflection string.
2744 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
2746 // FIXME: Turn this off until we straighten out the type of the
2747 // struct field used in a go statement which calls a method.
2748 // gcc_assert(this->receiver_ == NULL);
2750 ret
->append("func");
2752 if (this->receiver_
!= NULL
)
2754 ret
->push_back('(');
2755 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
2756 ret
->push_back(')');
2759 ret
->push_back('(');
2760 const Typed_identifier_list
* params
= this->parameters();
2763 bool is_varargs
= this->is_varargs_
;
2764 for (Typed_identifier_list::const_iterator p
= params
->begin();
2768 if (p
!= params
->begin())
2770 if (!is_varargs
|| p
+ 1 != params
->end())
2771 this->append_reflection(p
->type(), gogo
, ret
);
2775 this->append_reflection(p
->type()->array_type()->element_type(),
2780 ret
->push_back(')');
2782 const Typed_identifier_list
* results
= this->results();
2783 if (results
!= NULL
&& !results
->empty())
2785 if (results
->size() == 1)
2786 ret
->push_back(' ');
2789 for (Typed_identifier_list::const_iterator p
= results
->begin();
2790 p
!= results
->end();
2793 if (p
!= results
->begin())
2795 this->append_reflection(p
->type(), gogo
, ret
);
2797 if (results
->size() > 1)
2798 ret
->push_back(')');
2805 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
2807 ret
->push_back('F');
2809 if (this->receiver_
!= NULL
)
2811 ret
->push_back('m');
2812 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
2815 const Typed_identifier_list
* params
= this->parameters();
2818 ret
->push_back('p');
2819 for (Typed_identifier_list::const_iterator p
= params
->begin();
2822 this->append_mangled_name(p
->type(), gogo
, ret
);
2823 if (this->is_varargs_
)
2824 ret
->push_back('V');
2825 ret
->push_back('e');
2828 const Typed_identifier_list
* results
= this->results();
2829 if (results
!= NULL
)
2831 ret
->push_back('r');
2832 for (Typed_identifier_list::const_iterator p
= results
->begin();
2833 p
!= results
->end();
2835 this->append_mangled_name(p
->type(), gogo
, ret
);
2836 ret
->push_back('e');
2839 ret
->push_back('e');
2842 // Export a function type.
2845 Function_type::do_export(Export
* exp
) const
2847 // We don't write out the receiver. The only function types which
2848 // should have a receiver are the ones associated with explicitly
2849 // defined methods. For those the receiver type is written out by
2850 // Function::export_func.
2852 exp
->write_c_string("(");
2854 if (this->parameters_
!= NULL
)
2856 bool is_varargs
= this->is_varargs_
;
2857 for (Typed_identifier_list::const_iterator p
=
2858 this->parameters_
->begin();
2859 p
!= this->parameters_
->end();
2865 exp
->write_c_string(", ");
2866 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
2867 exp
->write_type(p
->type());
2870 exp
->write_c_string("...");
2871 exp
->write_type(p
->type()->array_type()->element_type());
2875 exp
->write_c_string(")");
2877 const Typed_identifier_list
* results
= this->results_
;
2878 if (results
!= NULL
)
2880 exp
->write_c_string(" ");
2881 if (results
->size() == 1)
2882 exp
->write_type(results
->begin()->type());
2886 exp
->write_c_string("(");
2887 for (Typed_identifier_list::const_iterator p
= results
->begin();
2888 p
!= results
->end();
2894 exp
->write_c_string(", ");
2895 exp
->write_type(p
->type());
2897 exp
->write_c_string(")");
2902 // Import a function type.
2905 Function_type::do_import(Import
* imp
)
2907 imp
->require_c_string("(");
2908 Typed_identifier_list
* parameters
;
2909 bool is_varargs
= false;
2910 if (imp
->peek_char() == ')')
2914 parameters
= new Typed_identifier_list();
2917 if (imp
->match_c_string("..."))
2923 Type
* ptype
= imp
->read_type();
2925 ptype
= Type::make_array_type(ptype
, NULL
);
2926 parameters
->push_back(Typed_identifier(Import::import_marker
,
2927 ptype
, imp
->location()));
2928 if (imp
->peek_char() != ',')
2930 gcc_assert(!is_varargs
);
2931 imp
->require_c_string(", ");
2934 imp
->require_c_string(")");
2936 Typed_identifier_list
* results
;
2937 if (imp
->peek_char() != ' ')
2942 results
= new Typed_identifier_list
;
2943 if (imp
->peek_char() != '(')
2945 Type
* rtype
= imp
->read_type();
2946 results
->push_back(Typed_identifier(Import::import_marker
, rtype
,
2954 Type
* rtype
= imp
->read_type();
2955 results
->push_back(Typed_identifier(Import::import_marker
,
2956 rtype
, imp
->location()));
2957 if (imp
->peek_char() != ',')
2959 imp
->require_c_string(", ");
2961 imp
->require_c_string(")");
2965 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
2968 ret
->set_is_varargs();
2972 // Make a copy of a function type without a receiver.
2975 Function_type::copy_without_receiver() const
2977 gcc_assert(this->is_method());
2978 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
2981 if (this->is_varargs())
2982 ret
->set_is_varargs();
2983 if (this->is_builtin())
2984 ret
->set_is_builtin();
2988 // Make a copy of a function type with a receiver.
2991 Function_type::copy_with_receiver(Type
* receiver_type
) const
2993 gcc_assert(!this->is_method());
2994 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
2996 return Type::make_function_type(receiver
, this->parameters_
,
2997 this->results_
, this->location_
);
3000 // Make a function type.
3003 Type::make_function_type(Typed_identifier
* receiver
,
3004 Typed_identifier_list
* parameters
,
3005 Typed_identifier_list
* results
,
3006 source_location location
)
3008 return new Function_type(receiver
, parameters
, results
, location
);
3011 // Class Pointer_type.
3016 Pointer_type::do_traverse(Traverse
* traverse
)
3018 return Type::traverse(this->to_type_
, traverse
);
3024 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
3026 return this->to_type_
->hash_for_method(gogo
) << 4;
3029 // The tree for a pointer type.
3032 Pointer_type::do_get_tree(Gogo
* gogo
)
3034 return build_pointer_type(this->to_type_
->get_tree(gogo
));
3037 // Initialize a pointer type.
3040 Pointer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3044 return fold_convert(type_tree
, null_pointer_node
);
3047 // The type of a pointer type descriptor.
3050 Pointer_type::make_pointer_type_descriptor_type()
3055 Type
* tdt
= Type::make_type_descriptor_type();
3056 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3058 Struct_type
* s
= Type::make_builtin_struct_type(2,
3062 ret
= Type::make_builtin_named_type("PtrType", s
);
3068 // The type descriptor for a pointer type.
3071 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3073 if (this->is_unsafe_pointer_type())
3075 gcc_assert(name
!= NULL
);
3076 return this->plain_type_descriptor(gogo
,
3077 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
3082 source_location bloc
= BUILTINS_LOCATION
;
3084 const Methods
* methods
;
3085 Type
* deref
= this->points_to();
3086 if (deref
->named_type() != NULL
)
3087 methods
= deref
->named_type()->methods();
3088 else if (deref
->struct_type() != NULL
)
3089 methods
= deref
->struct_type()->methods();
3093 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
3095 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
3097 Expression_list
* vals
= new Expression_list();
3100 Struct_field_list::const_iterator p
= fields
->begin();
3101 gcc_assert(p
->field_name() == "commonType");
3102 vals
->push_back(this->type_descriptor_constructor(gogo
,
3103 RUNTIME_TYPE_KIND_PTR
,
3104 name
, methods
, false));
3107 gcc_assert(p
->field_name() == "elem");
3108 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
3110 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
3114 // Reflection string.
3117 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3119 ret
->push_back('*');
3120 this->append_reflection(this->to_type_
, gogo
, ret
);
3126 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3128 ret
->push_back('p');
3129 this->append_mangled_name(this->to_type_
, gogo
, ret
);
3135 Pointer_type::do_export(Export
* exp
) const
3137 exp
->write_c_string("*");
3138 if (this->is_unsafe_pointer_type())
3139 exp
->write_c_string("any");
3141 exp
->write_type(this->to_type_
);
3147 Pointer_type::do_import(Import
* imp
)
3149 imp
->require_c_string("*");
3150 if (imp
->match_c_string("any"))
3153 return Type::make_pointer_type(Type::make_void_type());
3155 Type
* to
= imp
->read_type();
3156 return Type::make_pointer_type(to
);
3159 // Make a pointer type.
3162 Type::make_pointer_type(Type
* to_type
)
3164 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
3165 static Hashtable pointer_types
;
3166 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
3167 if (p
!= pointer_types
.end())
3169 Pointer_type
* ret
= new Pointer_type(to_type
);
3170 pointer_types
[to_type
] = ret
;
3174 // The nil type. We use a special type for nil because it is not the
3175 // same as any other type. In C term nil has type void*, but there is
3176 // no such type in Go.
3178 class Nil_type
: public Type
3188 { return ptr_type_node
; }
3191 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3192 { return is_clear
? NULL
: fold_convert(type_tree
, null_pointer_node
); }
3195 do_type_descriptor(Gogo
*, Named_type
*)
3196 { gcc_unreachable(); }
3199 do_reflection(Gogo
*, std::string
*) const
3200 { gcc_unreachable(); }
3203 do_mangled_name(Gogo
*, std::string
* ret
) const
3204 { ret
->push_back('n'); }
3207 // Make the nil type.
3210 Type::make_nil_type()
3212 static Nil_type singleton_nil_type
;
3213 return &singleton_nil_type
;
3216 // The type of a function call which returns multiple values. This is
3217 // really a struct, but we don't want to confuse a function call which
3218 // returns a struct with a function call which returns multiple
3221 class Call_multiple_result_type
: public Type
3224 Call_multiple_result_type(Call_expression
* call
)
3225 : Type(TYPE_CALL_MULTIPLE_RESULT
),
3231 do_has_pointer() const
3232 { gcc_unreachable(); }
3238 do_get_init_tree(Gogo
*, tree
, bool)
3239 { gcc_unreachable(); }
3242 do_type_descriptor(Gogo
*, Named_type
*)
3243 { gcc_unreachable(); }
3246 do_reflection(Gogo
*, std::string
*) const
3247 { gcc_unreachable(); }
3250 do_mangled_name(Gogo
*, std::string
*) const
3251 { gcc_unreachable(); }
3254 // The expression being called.
3255 Call_expression
* call_
;
3258 // Return the tree for a call result.
3261 Call_multiple_result_type::do_get_tree(Gogo
* gogo
)
3263 Function_type
* fntype
= this->call_
->get_function_type();
3264 gcc_assert(fntype
!= NULL
);
3265 const Typed_identifier_list
* results
= fntype
->results();
3266 gcc_assert(results
!= NULL
&& results
->size() > 1);
3268 Struct_field_list
* sfl
= new Struct_field_list
;
3269 for (Typed_identifier_list::const_iterator p
= results
->begin();
3270 p
!= results
->end();
3273 const std::string name
= ((p
->name().empty()
3274 || p
->name() == Import::import_marker
)
3277 sfl
->push_back(Struct_field(Typed_identifier(name
, p
->type(),
3278 this->call_
->location())));
3280 return Type::make_struct_type(sfl
, this->call_
->location())->get_tree(gogo
);
3283 // Make a call result type.
3286 Type::make_call_multiple_result_type(Call_expression
* call
)
3288 return new Call_multiple_result_type(call
);
3291 // Class Struct_field.
3293 // Get the name of a field.
3296 Struct_field::field_name() const
3298 const std::string
& name(this->typed_identifier_
.name());
3303 // This is called during parsing, before anything is lowered, so
3304 // we have to be pretty careful to avoid dereferencing an
3305 // unknown type name.
3306 Type
* t
= this->typed_identifier_
.type();
3308 if (t
->classification() == Type::TYPE_POINTER
)
3311 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
3312 dt
= ptype
->points_to();
3314 if (dt
->forward_declaration_type() != NULL
)
3315 return dt
->forward_declaration_type()->name();
3316 else if (dt
->named_type() != NULL
)
3317 return dt
->named_type()->name();
3318 else if (t
->is_error_type() || dt
->is_error_type())
3320 static const std::string error_string
= "*error*";
3321 return error_string
;
3325 // Avoid crashing in the erroneous case where T is named but
3327 gcc_assert(t
!= dt
);
3328 if (t
->forward_declaration_type() != NULL
)
3329 return t
->forward_declaration_type()->name();
3330 else if (t
->named_type() != NULL
)
3331 return t
->named_type()->name();
3338 // Class Struct_type.
3343 Struct_type::do_traverse(Traverse
* traverse
)
3345 Struct_field_list
* fields
= this->fields_
;
3348 for (Struct_field_list::iterator p
= fields
->begin();
3352 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
3353 return TRAVERSE_EXIT
;
3356 return TRAVERSE_CONTINUE
;
3359 // Verify that the struct type is complete and valid.
3362 Struct_type::do_verify()
3364 Struct_field_list
* fields
= this->fields_
;
3367 for (Struct_field_list::iterator p
= fields
->begin();
3371 Type
* t
= p
->type();
3372 if (t
->is_undefined())
3374 error_at(p
->location(), "struct field type is incomplete");
3375 p
->set_type(Type::make_error_type());
3378 else if (p
->is_anonymous())
3380 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
3382 error_at(p
->location(), "embedded type may not be a pointer");
3383 p
->set_type(Type::make_error_type());
3391 // Whether this contains a pointer.
3394 Struct_type::do_has_pointer() const
3396 const Struct_field_list
* fields
= this->fields();
3399 for (Struct_field_list::const_iterator p
= fields
->begin();
3403 if (p
->type()->has_pointer())
3409 // Whether this type is identical to T.
3412 Struct_type::is_identical(const Struct_type
* t
) const
3414 const Struct_field_list
* fields1
= this->fields();
3415 const Struct_field_list
* fields2
= t
->fields();
3416 if (fields1
== NULL
|| fields2
== NULL
)
3417 return fields1
== fields2
;
3418 Struct_field_list::const_iterator pf2
= fields2
->begin();
3419 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
3420 pf1
!= fields1
->end();
3423 if (pf2
== fields2
->end())
3425 if (pf1
->field_name() != pf2
->field_name())
3427 if (pf1
->is_anonymous() != pf2
->is_anonymous()
3428 || !Type::are_identical(pf1
->type(), pf2
->type(), NULL
))
3430 if (!pf1
->has_tag())
3437 if (!pf2
->has_tag())
3439 if (pf1
->tag() != pf2
->tag())
3443 if (pf2
!= fields2
->end())
3448 // Whether this struct type has any hidden fields.
3451 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
3452 std::string
* reason
) const
3454 const Struct_field_list
* fields
= this->fields();
3457 const Package
* within_package
= (within
== NULL
3459 : within
->named_object()->package());
3460 for (Struct_field_list::const_iterator pf
= fields
->begin();
3461 pf
!= fields
->end();
3464 if (within_package
!= NULL
3465 && !pf
->is_anonymous()
3466 && Gogo::is_hidden_name(pf
->field_name()))
3470 std::string within_name
= within
->named_object()->message_name();
3471 std::string name
= Gogo::message_name(pf
->field_name());
3472 size_t bufsize
= 200 + within_name
.length() + name
.length();
3473 char* buf
= new char[bufsize
];
3474 snprintf(buf
, bufsize
,
3475 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3476 open_quote
, within_name
.c_str(), close_quote
,
3477 open_quote
, name
.c_str(), close_quote
);
3478 reason
->assign(buf
);
3484 if (pf
->type()->has_hidden_fields(within
, reason
))
3494 Struct_type::do_hash_for_method(Gogo
* gogo
) const
3496 unsigned int ret
= 0;
3497 if (this->fields() != NULL
)
3499 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
3500 pf
!= this->fields()->end();
3502 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
3507 // Find the local field NAME.
3510 Struct_type::find_local_field(const std::string
& name
,
3511 unsigned int *pindex
) const
3513 const Struct_field_list
* fields
= this->fields_
;
3517 for (Struct_field_list::const_iterator pf
= fields
->begin();
3518 pf
!= fields
->end();
3521 if (pf
->field_name() == name
)
3531 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3533 Field_reference_expression
*
3534 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
3535 source_location location
) const
3538 return this->field_reference_depth(struct_expr
, name
, location
, &depth
);
3541 // Return an expression for a field, along with the depth at which it
3544 Field_reference_expression
*
3545 Struct_type::field_reference_depth(Expression
* struct_expr
,
3546 const std::string
& name
,
3547 source_location location
,
3548 unsigned int* depth
) const
3550 const Struct_field_list
* fields
= this->fields_
;
3554 // Look for a field with this name.
3556 for (Struct_field_list::const_iterator pf
= fields
->begin();
3557 pf
!= fields
->end();
3560 if (pf
->field_name() == name
)
3563 return Expression::make_field_reference(struct_expr
, i
, location
);
3567 // Look for an anonymous field which contains a field with this
3569 unsigned int found_depth
= 0;
3570 Field_reference_expression
* ret
= NULL
;
3572 for (Struct_field_list::const_iterator pf
= fields
->begin();
3573 pf
!= fields
->end();
3576 if (!pf
->is_anonymous())
3579 Struct_type
* st
= pf
->type()->deref()->struct_type();
3583 // Look for a reference using a NULL struct expression. If we
3584 // find one, fill in the struct expression with a reference to
3586 unsigned int subdepth
;
3587 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
3593 if (ret
== NULL
|| subdepth
< found_depth
)
3598 found_depth
= subdepth
;
3599 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
3601 if (pf
->type()->points_to() != NULL
)
3602 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
3603 while (sub
->expr() != NULL
)
3605 sub
= sub
->expr()->deref()->field_reference_expression();
3606 gcc_assert(sub
!= NULL
);
3608 sub
->set_struct_expression(here
);
3610 else if (subdepth
> found_depth
)
3614 // We do not handle ambiguity here--it should be handled by
3615 // Type::bind_field_or_method.
3623 *depth
= found_depth
+ 1;
3628 // Return the total number of fields, including embedded fields.
3631 Struct_type::total_field_count() const
3633 if (this->fields_
== NULL
)
3635 unsigned int ret
= 0;
3636 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3637 pf
!= this->fields_
->end();
3640 if (!pf
->is_anonymous() || pf
->type()->deref()->struct_type() == NULL
)
3643 ret
+= pf
->type()->struct_type()->total_field_count();
3648 // Return whether NAME is an unexported field, for better error reporting.
3651 Struct_type::is_unexported_local_field(Gogo
* gogo
,
3652 const std::string
& name
) const
3654 const Struct_field_list
* fields
= this->fields_
;
3657 for (Struct_field_list::const_iterator pf
= fields
->begin();
3658 pf
!= fields
->end();
3661 const std::string
& field_name(pf
->field_name());
3662 if (Gogo::is_hidden_name(field_name
)
3663 && name
== Gogo::unpack_hidden_name(field_name
)
3664 && gogo
->pack_hidden_name(name
, false) != field_name
)
3671 // Finalize the methods of an unnamed struct.
3674 Struct_type::finalize_methods(Gogo
* gogo
)
3676 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
3679 // Return the method NAME, or NULL if there isn't one or if it is
3680 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3684 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
3686 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
3689 // Get the tree for a struct type.
3692 Struct_type::do_get_tree(Gogo
* gogo
)
3694 tree type
= make_node(RECORD_TYPE
);
3695 return this->fill_in_tree(gogo
, type
);
3698 // Fill in the fields for a struct type.
3701 Struct_type::fill_in_tree(Gogo
* gogo
, tree type
)
3703 tree field_trees
= NULL_TREE
;
3704 tree
* pp
= &field_trees
;
3705 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3706 p
!= this->fields_
->end();
3709 std::string name
= Gogo::unpack_hidden_name(p
->field_name());
3710 tree name_tree
= get_identifier_with_length(name
.data(), name
.length());
3711 tree field_type_tree
= p
->type()->get_tree(gogo
);
3712 if (field_type_tree
== error_mark_node
)
3713 return error_mark_node
;
3714 tree field
= build_decl(p
->location(), FIELD_DECL
, name_tree
,
3716 DECL_CONTEXT(field
) = type
;
3718 pp
= &DECL_CHAIN(field
);
3721 TYPE_FIELDS(type
) = field_trees
;
3728 // Initialize struct fields.
3731 Struct_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
3733 if (this->fields_
== NULL
|| this->fields_
->empty())
3739 tree ret
= build_constructor(type_tree
,
3740 VEC_alloc(constructor_elt
, gc
, 0));
3741 TREE_CONSTANT(ret
) = 1;
3746 bool is_constant
= true;
3747 bool any_fields_set
= false;
3748 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
,
3749 this->fields_
->size());
3750 Struct_field_list::const_iterator p
= this->fields_
->begin();
3751 for (tree field
= TYPE_FIELDS(type_tree
);
3753 field
= DECL_CHAIN(field
), ++p
)
3755 gcc_assert(p
!= this->fields_
->end());
3756 tree value
= p
->type()->get_init_tree(gogo
, is_clear
);
3759 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
3762 any_fields_set
= true;
3763 if (!TREE_CONSTANT(value
))
3764 is_constant
= false;
3767 gcc_assert(p
== this->fields_
->end());
3769 if (!any_fields_set
)
3771 gcc_assert(is_clear
);
3772 VEC_free(constructor_elt
, gc
, init
);
3776 tree ret
= build_constructor(type_tree
, init
);
3778 TREE_CONSTANT(ret
) = 1;
3782 // The type of a struct type descriptor.
3785 Struct_type::make_struct_type_descriptor_type()
3790 Type
* tdt
= Type::make_type_descriptor_type();
3791 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3793 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3794 Type
* string_type
= Type::lookup_string_type();
3795 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
3798 Type::make_builtin_struct_type(5,
3799 "name", pointer_string_type
,
3800 "pkgPath", pointer_string_type
,
3802 "tag", pointer_string_type
,
3803 "offset", uintptr_type
);
3804 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
3806 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
3808 Struct_type
* s
= Type::make_builtin_struct_type(2,
3810 "fields", slice_type
);
3812 ret
= Type::make_builtin_named_type("StructType", s
);
3818 // Build a type descriptor for a struct type.
3821 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3823 source_location bloc
= BUILTINS_LOCATION
;
3825 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
3827 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
3829 Expression_list
* vals
= new Expression_list();
3832 const Methods
* methods
= this->methods();
3833 // A named struct should not have methods--the methods should attach
3834 // to the named type.
3835 gcc_assert(methods
== NULL
|| name
== NULL
);
3837 Struct_field_list::const_iterator ps
= fields
->begin();
3838 gcc_assert(ps
->field_name() == "commonType");
3839 vals
->push_back(this->type_descriptor_constructor(gogo
,
3840 RUNTIME_TYPE_KIND_STRUCT
,
3841 name
, methods
, true));
3844 gcc_assert(ps
->field_name() == "fields");
3846 Expression_list
* elements
= new Expression_list();
3847 elements
->reserve(this->fields_
->size());
3848 Type
* element_type
= ps
->type()->array_type()->element_type();
3849 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3850 pf
!= this->fields_
->end();
3853 const Struct_field_list
* f
= element_type
->struct_type()->fields();
3855 Expression_list
* fvals
= new Expression_list();
3858 Struct_field_list::const_iterator q
= f
->begin();
3859 gcc_assert(q
->field_name() == "name");
3860 if (pf
->is_anonymous())
3861 fvals
->push_back(Expression::make_nil(bloc
));
3864 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
3865 Expression
* s
= Expression::make_string(n
, bloc
);
3866 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3870 gcc_assert(q
->field_name() == "pkgPath");
3871 if (!Gogo::is_hidden_name(pf
->field_name()))
3872 fvals
->push_back(Expression::make_nil(bloc
));
3875 std::string n
= Gogo::hidden_name_prefix(pf
->field_name());
3876 Expression
* s
= Expression::make_string(n
, bloc
);
3877 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3881 gcc_assert(q
->field_name() == "typ");
3882 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
3885 gcc_assert(q
->field_name() == "tag");
3887 fvals
->push_back(Expression::make_nil(bloc
));
3890 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
3891 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3895 gcc_assert(q
->field_name() == "offset");
3896 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
3898 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
3900 elements
->push_back(v
);
3903 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
3906 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
3909 // Reflection string.
3912 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3914 ret
->append("struct { ");
3916 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3917 p
!= this->fields_
->end();
3920 if (p
!= this->fields_
->begin())
3922 if (p
->is_anonymous())
3923 ret
->push_back('?');
3925 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
3926 ret
->push_back(' ');
3927 this->append_reflection(p
->type(), gogo
, ret
);
3931 const std::string
& tag(p
->tag());
3933 for (std::string::const_iterator p
= tag
.begin();
3938 ret
->append("\\x00");
3939 else if (*p
== '\n')
3941 else if (*p
== '\t')
3944 ret
->append("\\\"");
3945 else if (*p
== '\\')
3946 ret
->append("\\\\");
3950 ret
->push_back('"');
3960 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3962 ret
->push_back('S');
3964 const Struct_field_list
* fields
= this->fields_
;
3967 for (Struct_field_list::const_iterator p
= fields
->begin();
3971 if (p
->is_anonymous())
3975 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
3977 snprintf(buf
, sizeof buf
, "%u_",
3978 static_cast<unsigned int>(n
.length()));
3982 this->append_mangled_name(p
->type(), gogo
, ret
);
3985 const std::string
& tag(p
->tag());
3987 for (std::string::const_iterator p
= tag
.begin();
3991 if (ISALNUM(*p
) || *p
== '_')
3996 snprintf(buf
, sizeof buf
, ".%x.",
3997 static_cast<unsigned int>(*p
));
4002 snprintf(buf
, sizeof buf
, "T%u_",
4003 static_cast<unsigned int>(out
.length()));
4010 ret
->push_back('e');
4016 Struct_type::do_export(Export
* exp
) const
4018 exp
->write_c_string("struct { ");
4019 const Struct_field_list
* fields
= this->fields_
;
4020 gcc_assert(fields
!= NULL
);
4021 for (Struct_field_list::const_iterator p
= fields
->begin();
4025 if (p
->is_anonymous())
4026 exp
->write_string("? ");
4029 exp
->write_string(p
->field_name());
4030 exp
->write_c_string(" ");
4032 exp
->write_type(p
->type());
4036 exp
->write_c_string(" ");
4037 Expression
* expr
= Expression::make_string(p
->tag(),
4039 expr
->export_expression(exp
);
4043 exp
->write_c_string("; ");
4045 exp
->write_c_string("}");
4051 Struct_type::do_import(Import
* imp
)
4053 imp
->require_c_string("struct { ");
4054 Struct_field_list
* fields
= new Struct_field_list
;
4055 if (imp
->peek_char() != '}')
4060 if (imp
->match_c_string("? "))
4064 name
= imp
->read_identifier();
4065 imp
->require_c_string(" ");
4067 Type
* ftype
= imp
->read_type();
4069 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
4071 if (imp
->peek_char() == ' ')
4074 Expression
* expr
= Expression::import_expression(imp
);
4075 String_expression
* sexpr
= expr
->string_expression();
4076 gcc_assert(sexpr
!= NULL
);
4077 sf
.set_tag(sexpr
->val());
4081 imp
->require_c_string("; ");
4082 fields
->push_back(sf
);
4083 if (imp
->peek_char() == '}')
4087 imp
->require_c_string("}");
4089 return Type::make_struct_type(fields
, imp
->location());
4092 // Make a struct type.
4095 Type::make_struct_type(Struct_field_list
* fields
,
4096 source_location location
)
4098 return new Struct_type(fields
, location
);
4101 // Class Array_type.
4103 // Whether two array types are identical.
4106 Array_type::is_identical(const Array_type
* t
) const
4108 if (!Type::are_identical(this->element_type(), t
->element_type(), NULL
))
4111 Expression
* l1
= this->length();
4112 Expression
* l2
= t
->length();
4114 // Slices of the same element type are identical.
4115 if (l1
== NULL
&& l2
== NULL
)
4118 // Arrays of the same element type are identical if they have the
4120 if (l1
!= NULL
&& l2
!= NULL
)
4125 // Try to determine the lengths. If we can't, assume the arrays
4126 // are not identical.
4134 if (l1
->integer_constant_value(true, v1
, &type1
)
4135 && l2
->integer_constant_value(true, v2
, &type2
))
4136 ret
= mpz_cmp(v1
, v2
) == 0;
4142 // Otherwise the arrays are not identical.
4149 Array_type::do_traverse(Traverse
* traverse
)
4151 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
4152 return TRAVERSE_EXIT
;
4153 if (this->length_
!= NULL
4154 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
4155 return TRAVERSE_EXIT
;
4156 return TRAVERSE_CONTINUE
;
4159 // Check that the length is valid.
4162 Array_type::verify_length()
4164 if (this->length_
== NULL
)
4166 if (!this->length_
->is_constant())
4168 error_at(this->length_
->location(), "array bound is not constant");
4174 Type
* t
= this->length_
->type();
4175 if (t
->integer_type() != NULL
)
4179 if (!this->length_
->integer_constant_value(true, val
, &vt
))
4181 error_at(this->length_
->location(),
4182 "array bound is not constant");
4187 else if (t
->float_type() != NULL
)
4192 if (!this->length_
->float_constant_value(fval
, &vt
))
4194 error_at(this->length_
->location(),
4195 "array bound is not constant");
4199 if (!mpfr_integer_p(fval
))
4201 error_at(this->length_
->location(),
4202 "array bound truncated to integer");
4207 mpfr_get_z(val
, fval
, GMP_RNDN
);
4212 if (!t
->is_error_type())
4213 error_at(this->length_
->location(), "array bound is not numeric");
4217 if (mpz_sgn(val
) < 0)
4219 error_at(this->length_
->location(), "negative array bound");
4224 Type
* int_type
= Type::lookup_integer_type("int");
4225 int tbits
= int_type
->integer_type()->bits();
4226 int vbits
= mpz_sizeinbase(val
, 2);
4227 if (vbits
+ 1 > tbits
)
4229 error_at(this->length_
->location(), "array bound overflows");
4242 Array_type::do_verify()
4244 if (!this->verify_length())
4246 this->length_
= Expression::make_error(this->length_
->location());
4252 // Array type hash code.
4255 Array_type::do_hash_for_method(Gogo
* gogo
) const
4257 // There is no very convenient way to get a hash code for the
4259 return this->element_type_
->hash_for_method(gogo
) + 1;
4262 // See if the expression passed to make is suitable. The first
4263 // argument is required, and gives the length. An optional second
4264 // argument is permitted for the capacity.
4267 Array_type::do_check_make_expression(Expression_list
* args
,
4268 source_location location
)
4270 gcc_assert(this->length_
== NULL
);
4271 if (args
== NULL
|| args
->empty())
4273 error_at(location
, "length required when allocating a slice");
4276 else if (args
->size() > 2)
4278 error_at(location
, "too many expressions passed to make");
4283 if (!Type::check_int_value(args
->front(),
4284 _("bad length when making slice"), location
))
4287 if (args
->size() > 1)
4289 if (!Type::check_int_value(args
->back(),
4290 _("bad capacity when making slice"),
4299 // Get a tree for the length of a fixed array. The length may be
4300 // computed using a function call, so we must only evaluate it once.
4303 Array_type::get_length_tree(Gogo
* gogo
)
4305 gcc_assert(this->length_
!= NULL
);
4306 if (this->length_tree_
== NULL_TREE
)
4311 if (this->length_
->integer_constant_value(true, val
, &t
))
4314 t
= Type::lookup_integer_type("int");
4315 else if (t
->is_abstract())
4316 t
= t
->make_non_abstract_type();
4317 tree tt
= t
->get_tree(gogo
);
4318 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
4325 // Make up a translation context for the array length
4326 // expression. FIXME: This won't work in general.
4327 Translate_context
context(gogo
, NULL
, NULL
, NULL_TREE
);
4328 tree len
= this->length_
->get_tree(&context
);
4329 len
= convert_to_integer(integer_type_node
, len
);
4330 this->length_tree_
= save_expr(len
);
4333 return this->length_tree_
;
4336 // Get a tree for the type of this array. A fixed array is simply
4337 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4338 // just like an array in C. An open array is a struct with three
4339 // fields: a data pointer, the length, and the capacity.
4342 Array_type::do_get_tree(Gogo
* gogo
)
4344 if (this->length_
== NULL
)
4346 tree struct_type
= gogo
->slice_type_tree(void_type_node
);
4347 return this->fill_in_tree(gogo
, struct_type
);
4351 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4352 tree length_tree
= this->get_length_tree(gogo
);
4353 if (element_type_tree
== error_mark_node
4354 || length_tree
== error_mark_node
)
4355 return error_mark_node
;
4357 length_tree
= fold_convert(sizetype
, length_tree
);
4359 // build_index_type takes the maximum index, which is one less
4361 tree index_type
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
4365 return build_array_type(element_type_tree
, index_type
);
4369 // Fill in the fields for a slice type. This is used for named slice
4373 Array_type::fill_in_tree(Gogo
* gogo
, tree struct_type
)
4375 gcc_assert(this->length_
== NULL
);
4377 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4378 tree field
= TYPE_FIELDS(struct_type
);
4379 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
4380 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field
))
4381 && TREE_TYPE(TREE_TYPE(field
)) == void_type_node
);
4382 TREE_TYPE(field
) = build_pointer_type(element_type_tree
);
4387 // Return an initializer for an array type.
4390 Array_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
4392 if (this->length_
== NULL
)
4399 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
4401 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
4403 for (tree field
= TYPE_FIELDS(type_tree
);
4405 field
= DECL_CHAIN(field
))
4407 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
,
4410 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
4413 tree ret
= build_constructor(type_tree
, init
);
4414 TREE_CONSTANT(ret
) = 1;
4421 tree value
= this->element_type_
->get_init_tree(gogo
, is_clear
);
4424 if (value
== error_mark_node
)
4425 return error_mark_node
;
4427 tree length_tree
= this->get_length_tree(gogo
);
4428 if (length_tree
== error_mark_node
)
4429 return error_mark_node
;
4431 length_tree
= fold_convert(sizetype
, length_tree
);
4432 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
,
4433 fold_build2(MINUS_EXPR
, sizetype
,
4434 length_tree
, size_one_node
));
4435 tree ret
= build_constructor_single(type_tree
, range
, value
);
4436 if (TREE_CONSTANT(value
))
4437 TREE_CONSTANT(ret
) = 1;
4442 // Handle the builtin make function for a slice.
4445 Array_type::do_make_expression_tree(Translate_context
* context
,
4446 Expression_list
* args
,
4447 source_location location
)
4449 gcc_assert(this->length_
== NULL
);
4451 Gogo
* gogo
= context
->gogo();
4452 tree type_tree
= this->get_tree(gogo
);
4453 if (type_tree
== error_mark_node
)
4454 return error_mark_node
;
4456 tree values_field
= TYPE_FIELDS(type_tree
);
4457 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field
)),
4460 tree count_field
= DECL_CHAIN(values_field
);
4461 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field
)),
4464 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4465 if (element_type_tree
== error_mark_node
)
4466 return error_mark_node
;
4467 tree element_size_tree
= TYPE_SIZE_UNIT(element_type_tree
);
4469 tree value
= this->element_type_
->get_init_tree(gogo
, true);
4471 // The first argument is the number of elements, the optional second
4472 // argument is the capacity.
4473 gcc_assert(args
!= NULL
&& args
->size() >= 1 && args
->size() <= 2);
4475 tree length_tree
= args
->front()->get_tree(context
);
4476 if (length_tree
== error_mark_node
)
4477 return error_mark_node
;
4478 if (!DECL_P(length_tree
))
4479 length_tree
= save_expr(length_tree
);
4480 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree
)))
4481 length_tree
= convert_to_integer(TREE_TYPE(count_field
), length_tree
);
4483 tree bad_index
= Expression::check_bounds(length_tree
,
4484 TREE_TYPE(count_field
),
4485 NULL_TREE
, location
);
4487 length_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
), length_tree
);
4489 if (args
->size() == 1)
4490 capacity_tree
= length_tree
;
4493 capacity_tree
= args
->back()->get_tree(context
);
4494 if (capacity_tree
== error_mark_node
)
4495 return error_mark_node
;
4496 if (!DECL_P(capacity_tree
))
4497 capacity_tree
= save_expr(capacity_tree
);
4498 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree
)))
4499 capacity_tree
= convert_to_integer(TREE_TYPE(count_field
),
4502 bad_index
= Expression::check_bounds(capacity_tree
,
4503 TREE_TYPE(count_field
),
4504 bad_index
, location
);
4506 tree chktype
= (((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4507 > TYPE_SIZE(TREE_TYPE(length_tree
)))
4508 || ((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4509 == TYPE_SIZE(TREE_TYPE(length_tree
)))
4510 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree
))))
4511 ? TREE_TYPE(capacity_tree
)
4512 : TREE_TYPE(length_tree
));
4513 tree chk
= fold_build2_loc(location
, LT_EXPR
, boolean_type_node
,
4514 fold_convert_loc(location
, chktype
,
4516 fold_convert_loc(location
, chktype
,
4518 if (bad_index
== NULL_TREE
)
4521 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4524 capacity_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
),
4528 tree size_tree
= fold_build2_loc(location
, MULT_EXPR
, sizetype
,
4530 fold_convert_loc(location
, sizetype
,
4533 tree chk
= fold_build2_loc(location
, TRUTH_AND_EXPR
, boolean_type_node
,
4534 fold_build2_loc(location
, GT_EXPR
,
4536 fold_convert_loc(location
,
4540 fold_build2_loc(location
, LT_EXPR
,
4542 size_tree
, element_size_tree
));
4543 if (bad_index
== NULL_TREE
)
4546 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4549 tree space
= context
->gogo()->allocate_memory(this->element_type_
,
4550 size_tree
, location
);
4552 if (value
!= NULL_TREE
)
4553 space
= save_expr(space
);
4555 space
= fold_convert(TREE_TYPE(values_field
), space
);
4557 if (bad_index
!= NULL_TREE
&& bad_index
!= boolean_false_node
)
4559 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
,
4561 space
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4562 build3(COND_EXPR
, void_type_node
,
4563 bad_index
, crash
, NULL_TREE
),
4567 tree constructor
= gogo
->slice_constructor(type_tree
, space
, length_tree
,
4570 if (value
== NULL_TREE
)
4572 // The array contents are zero initialized.
4576 // The elements must be initialized.
4578 tree max
= fold_build2_loc(location
, MINUS_EXPR
, TREE_TYPE(count_field
),
4580 fold_convert_loc(location
, TREE_TYPE(count_field
),
4583 tree array_type
= build_array_type(element_type_tree
,
4584 build_index_type(max
));
4586 tree value_pointer
= fold_convert_loc(location
,
4587 build_pointer_type(array_type
),
4590 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
, max
);
4591 tree space_init
= build_constructor_single(array_type
, range
, value
);
4593 return build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4594 build2(MODIFY_EXPR
, void_type_node
,
4595 build_fold_indirect_ref(value_pointer
),
4600 // Return a tree for a pointer to the values in ARRAY.
4603 Array_type::value_pointer_tree(Gogo
*, tree array
) const
4606 if (this->length() != NULL
)
4609 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
4610 build_fold_addr_expr(array
));
4615 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
4616 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
4618 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
4621 if (TREE_CONSTANT(array
))
4622 TREE_CONSTANT(ret
) = 1;
4626 // Return a tree for the length of the array ARRAY which has this
4630 Array_type::length_tree(Gogo
* gogo
, tree array
)
4632 if (this->length_
!= NULL
)
4634 if (TREE_CODE(array
) == SAVE_EXPR
)
4635 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
4637 return omit_one_operand(integer_type_node
,
4638 this->get_length_tree(gogo
), array
);
4641 // This is an open array. We need to read the length field.
4643 tree type
= TREE_TYPE(array
);
4644 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4646 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
4647 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
4649 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4650 if (TREE_CONSTANT(array
))
4651 TREE_CONSTANT(ret
) = 1;
4655 // Return a tree for the capacity of the array ARRAY which has this
4659 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
4661 if (this->length_
!= NULL
)
4662 return omit_one_operand(sizetype
, this->get_length_tree(gogo
), array
);
4664 // This is an open array. We need to read the capacity field.
4666 tree type
= TREE_TYPE(array
);
4667 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4669 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
4670 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
4672 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4678 Array_type::do_export(Export
* exp
) const
4680 exp
->write_c_string("[");
4681 if (this->length_
!= NULL
)
4682 this->length_
->export_expression(exp
);
4683 exp
->write_c_string("] ");
4684 exp
->write_type(this->element_type_
);
4690 Array_type::do_import(Import
* imp
)
4692 imp
->require_c_string("[");
4694 if (imp
->peek_char() == ']')
4697 length
= Expression::import_expression(imp
);
4698 imp
->require_c_string("] ");
4699 Type
* element_type
= imp
->read_type();
4700 return Type::make_array_type(element_type
, length
);
4703 // The type of an array type descriptor.
4706 Array_type::make_array_type_descriptor_type()
4711 Type
* tdt
= Type::make_type_descriptor_type();
4712 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4714 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4717 Type::make_builtin_struct_type(3,
4720 "len", uintptr_type
);
4722 ret
= Type::make_builtin_named_type("ArrayType", sf
);
4728 // The type of an slice type descriptor.
4731 Array_type::make_slice_type_descriptor_type()
4736 Type
* tdt
= Type::make_type_descriptor_type();
4737 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4740 Type::make_builtin_struct_type(2,
4744 ret
= Type::make_builtin_named_type("SliceType", sf
);
4750 // Build a type descriptor for an array/slice type.
4753 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4755 if (this->length_
!= NULL
)
4756 return this->array_type_descriptor(gogo
, name
);
4758 return this->slice_type_descriptor(gogo
, name
);
4761 // Build a type descriptor for an array type.
4764 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4766 source_location bloc
= BUILTINS_LOCATION
;
4768 Type
* atdt
= Array_type::make_array_type_descriptor_type();
4770 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
4772 Expression_list
* vals
= new Expression_list();
4775 Struct_field_list::const_iterator p
= fields
->begin();
4776 gcc_assert(p
->field_name() == "commonType");
4777 vals
->push_back(this->type_descriptor_constructor(gogo
,
4778 RUNTIME_TYPE_KIND_ARRAY
,
4782 gcc_assert(p
->field_name() == "elem");
4783 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4786 gcc_assert(p
->field_name() == "len");
4787 vals
->push_back(this->length_
);
4790 gcc_assert(p
== fields
->end());
4792 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
4795 // Build a type descriptor for a slice type.
4798 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4800 source_location bloc
= BUILTINS_LOCATION
;
4802 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
4804 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4806 Expression_list
* vals
= new Expression_list();
4809 Struct_field_list::const_iterator p
= fields
->begin();
4810 gcc_assert(p
->field_name() == "commonType");
4811 vals
->push_back(this->type_descriptor_constructor(gogo
,
4812 RUNTIME_TYPE_KIND_SLICE
,
4816 gcc_assert(p
->field_name() == "elem");
4817 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4820 gcc_assert(p
== fields
->end());
4822 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4825 // Reflection string.
4828 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4830 ret
->push_back('[');
4831 if (this->length_
!= NULL
)
4836 if (!this->length_
->integer_constant_value(true, val
, &type
))
4837 error_at(this->length_
->location(),
4838 "array length must be integer constant expression");
4839 else if (mpz_cmp_si(val
, 0) < 0)
4840 error_at(this->length_
->location(), "array length is negative");
4841 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4842 error_at(this->length_
->location(), "array length is too large");
4846 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4851 ret
->push_back(']');
4853 this->append_reflection(this->element_type_
, gogo
, ret
);
4859 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4861 ret
->push_back('A');
4862 this->append_mangled_name(this->element_type_
, gogo
, ret
);
4863 if (this->length_
!= NULL
)
4868 if (!this->length_
->integer_constant_value(true, val
, &type
))
4869 error_at(this->length_
->location(),
4870 "array length must be integer constant expression");
4871 else if (mpz_cmp_si(val
, 0) < 0)
4872 error_at(this->length_
->location(), "array length is negative");
4873 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4874 error_at(this->length_
->location(), "array size is too large");
4878 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4883 ret
->push_back('e');
4886 // Make an array type.
4889 Type::make_array_type(Type
* element_type
, Expression
* length
)
4891 return new Array_type(element_type
, length
);
4899 Map_type::do_traverse(Traverse
* traverse
)
4901 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
4902 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
4903 return TRAVERSE_EXIT
;
4904 return TRAVERSE_CONTINUE
;
4907 // Check that the map type is OK.
4910 Map_type::do_verify()
4912 if (this->key_type_
->struct_type() != NULL
4913 || this->key_type_
->array_type() != NULL
)
4915 error_at(this->location_
, "invalid map key type");
4921 // Whether two map types are identical.
4924 Map_type::is_identical(const Map_type
* t
) const
4926 return (Type::are_identical(this->key_type(), t
->key_type(), NULL
)
4927 && Type::are_identical(this->val_type(), t
->val_type(), NULL
));
4933 Map_type::do_hash_for_method(Gogo
* gogo
) const
4935 return (this->key_type_
->hash_for_method(gogo
)
4936 + this->val_type_
->hash_for_method(gogo
)
4940 // Check that a call to the builtin make function is valid. For a map
4941 // the optional argument is the number of spaces to preallocate for
4945 Map_type::do_check_make_expression(Expression_list
* args
,
4946 source_location location
)
4948 if (args
!= NULL
&& !args
->empty())
4950 if (!Type::check_int_value(args
->front(), _("bad size when making map"),
4953 else if (args
->size() > 1)
4955 error_at(location
, "too many arguments when making map");
4962 // Get a tree for a map type. A map type is represented as a pointer
4963 // to a struct. The struct is __go_map in libgo/map.h.
4966 Map_type::do_get_tree(Gogo
* gogo
)
4968 static tree type_tree
;
4969 if (type_tree
== NULL_TREE
)
4971 tree struct_type
= make_node(RECORD_TYPE
);
4973 tree map_descriptor_type
= gogo
->map_descriptor_type();
4974 tree const_map_descriptor_type
=
4975 build_qualified_type(map_descriptor_type
, TYPE_QUAL_CONST
);
4976 tree name
= get_identifier("__descriptor");
4977 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
4978 build_pointer_type(const_map_descriptor_type
));
4979 DECL_CONTEXT(field
) = struct_type
;
4980 TYPE_FIELDS(struct_type
) = field
;
4981 tree last_field
= field
;
4983 name
= get_identifier("__element_count");
4984 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
4985 DECL_CONTEXT(field
) = struct_type
;
4986 DECL_CHAIN(last_field
) = field
;
4989 name
= get_identifier("__bucket_count");
4990 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
4991 DECL_CONTEXT(field
) = struct_type
;
4992 DECL_CHAIN(last_field
) = field
;
4995 name
= get_identifier("__buckets");
4996 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
4997 build_pointer_type(ptr_type_node
));
4998 DECL_CONTEXT(field
) = struct_type
;
4999 DECL_CHAIN(last_field
) = field
;
5001 layout_type(struct_type
);
5003 // Give the struct a name for better debugging info.
5004 name
= get_identifier("__go_map");
5005 tree type_decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
, name
,
5007 DECL_ARTIFICIAL(type_decl
) = 1;
5008 TYPE_NAME(struct_type
) = type_decl
;
5009 go_preserve_from_gc(type_decl
);
5010 rest_of_decl_compilation(type_decl
, 1, 0);
5012 type_tree
= build_pointer_type(struct_type
);
5013 go_preserve_from_gc(type_tree
);
5019 // Initialize a map.
5022 Map_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5026 return fold_convert(type_tree
, null_pointer_node
);
5029 // Return an expression for a newly allocated map.
5032 Map_type::do_make_expression_tree(Translate_context
* context
,
5033 Expression_list
* args
,
5034 source_location location
)
5036 tree bad_index
= NULL_TREE
;
5039 if (args
== NULL
|| args
->empty())
5040 expr_tree
= size_zero_node
;
5043 expr_tree
= args
->front()->get_tree(context
);
5044 if (expr_tree
== error_mark_node
)
5045 return error_mark_node
;
5046 if (!DECL_P(expr_tree
))
5047 expr_tree
= save_expr(expr_tree
);
5048 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5049 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5050 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5054 tree map_type
= this->get_tree(context
->gogo());
5056 static tree new_map_fndecl
;
5057 tree ret
= Gogo::call_builtin(&new_map_fndecl
,
5062 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type
))),
5063 context
->gogo()->map_descriptor(this),
5066 // This can panic if the capacity is out of range.
5067 TREE_NOTHROW(new_map_fndecl
) = 0;
5069 if (bad_index
== NULL_TREE
)
5073 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
,
5075 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5076 build3(COND_EXPR
, void_type_node
,
5077 bad_index
, crash
, NULL_TREE
),
5082 // The type of a map type descriptor.
5085 Map_type::make_map_type_descriptor_type()
5090 Type
* tdt
= Type::make_type_descriptor_type();
5091 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5094 Type::make_builtin_struct_type(3,
5099 ret
= Type::make_builtin_named_type("MapType", sf
);
5105 // Build a type descriptor for a map type.
5108 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5110 source_location bloc
= BUILTINS_LOCATION
;
5112 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
5114 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
5116 Expression_list
* vals
= new Expression_list();
5119 Struct_field_list::const_iterator p
= fields
->begin();
5120 gcc_assert(p
->field_name() == "commonType");
5121 vals
->push_back(this->type_descriptor_constructor(gogo
,
5122 RUNTIME_TYPE_KIND_MAP
,
5126 gcc_assert(p
->field_name() == "key");
5127 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
5130 gcc_assert(p
->field_name() == "elem");
5131 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
5134 gcc_assert(p
== fields
->end());
5136 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
5139 // Reflection string for a map.
5142 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5144 ret
->append("map[");
5145 this->append_reflection(this->key_type_
, gogo
, ret
);
5147 this->append_reflection(this->val_type_
, gogo
, ret
);
5150 // Mangled name for a map.
5153 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5155 ret
->push_back('M');
5156 this->append_mangled_name(this->key_type_
, gogo
, ret
);
5158 this->append_mangled_name(this->val_type_
, gogo
, ret
);
5161 // Export a map type.
5164 Map_type::do_export(Export
* exp
) const
5166 exp
->write_c_string("map [");
5167 exp
->write_type(this->key_type_
);
5168 exp
->write_c_string("] ");
5169 exp
->write_type(this->val_type_
);
5172 // Import a map type.
5175 Map_type::do_import(Import
* imp
)
5177 imp
->require_c_string("map [");
5178 Type
* key_type
= imp
->read_type();
5179 imp
->require_c_string("] ");
5180 Type
* val_type
= imp
->read_type();
5181 return Type::make_map_type(key_type
, val_type
, imp
->location());
5187 Type::make_map_type(Type
* key_type
, Type
* val_type
, source_location location
)
5189 return new Map_type(key_type
, val_type
, location
);
5192 // Class Channel_type.
5197 Channel_type::do_hash_for_method(Gogo
* gogo
) const
5199 unsigned int ret
= 0;
5200 if (this->may_send_
)
5202 if (this->may_receive_
)
5204 if (this->element_type_
!= NULL
)
5205 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
5209 // Whether this type is the same as T.
5212 Channel_type::is_identical(const Channel_type
* t
) const
5214 if (!Type::are_identical(this->element_type(), t
->element_type(), NULL
))
5216 return (this->may_send_
== t
->may_send_
5217 && this->may_receive_
== t
->may_receive_
);
5220 // Check whether the parameters for a call to the builtin function
5221 // make are OK for a channel. A channel can take an optional single
5222 // parameter which is the buffer size.
5225 Channel_type::do_check_make_expression(Expression_list
* args
,
5226 source_location location
)
5228 if (args
!= NULL
&& !args
->empty())
5230 if (!Type::check_int_value(args
->front(),
5231 _("bad buffer size when making channel"),
5234 else if (args
->size() > 1)
5236 error_at(location
, "too many arguments when making channel");
5243 // Return the tree for a channel type. A channel is a pointer to a
5244 // __go_channel struct. The __go_channel struct is defined in
5245 // libgo/runtime/channel.h.
5248 Channel_type::do_get_tree(Gogo
*)
5250 static tree type_tree
;
5251 if (type_tree
== NULL_TREE
)
5253 tree ret
= make_node(RECORD_TYPE
);
5254 TYPE_NAME(ret
) = get_identifier("__go_channel");
5255 TYPE_STUB_DECL(ret
) = build_decl(BUILTINS_LOCATION
, TYPE_DECL
, NULL_TREE
,
5257 type_tree
= build_pointer_type(ret
);
5258 go_preserve_from_gc(type_tree
);
5263 // Initialize a channel variable.
5266 Channel_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5270 return fold_convert(type_tree
, null_pointer_node
);
5273 // Handle the builtin function make for a channel.
5276 Channel_type::do_make_expression_tree(Translate_context
* context
,
5277 Expression_list
* args
,
5278 source_location location
)
5280 Gogo
* gogo
= context
->gogo();
5281 tree channel_type
= this->get_tree(gogo
);
5283 tree element_tree
= this->element_type_
->get_tree(gogo
);
5284 tree element_size_tree
= size_in_bytes(element_tree
);
5286 tree bad_index
= NULL_TREE
;
5289 if (args
== NULL
|| args
->empty())
5290 expr_tree
= size_zero_node
;
5293 expr_tree
= args
->front()->get_tree(context
);
5294 if (expr_tree
== error_mark_node
)
5295 return error_mark_node
;
5296 if (!DECL_P(expr_tree
))
5297 expr_tree
= save_expr(expr_tree
);
5298 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5299 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5300 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5304 static tree new_channel_fndecl
;
5305 tree ret
= Gogo::call_builtin(&new_channel_fndecl
,
5314 // This can panic if the capacity is out of range.
5315 TREE_NOTHROW(new_channel_fndecl
) = 0;
5317 if (bad_index
== NULL_TREE
)
5321 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
,
5323 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5324 build3(COND_EXPR
, void_type_node
,
5325 bad_index
, crash
, NULL_TREE
),
5330 // Build a type descriptor for a channel type.
5333 Channel_type::make_chan_type_descriptor_type()
5338 Type
* tdt
= Type::make_type_descriptor_type();
5339 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5341 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5344 Type::make_builtin_struct_type(3,
5347 "dir", uintptr_type
);
5349 ret
= Type::make_builtin_named_type("ChanType", sf
);
5355 // Build a type descriptor for a map type.
5358 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5360 source_location bloc
= BUILTINS_LOCATION
;
5362 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
5364 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
5366 Expression_list
* vals
= new Expression_list();
5369 Struct_field_list::const_iterator p
= fields
->begin();
5370 gcc_assert(p
->field_name() == "commonType");
5371 vals
->push_back(this->type_descriptor_constructor(gogo
,
5372 RUNTIME_TYPE_KIND_CHAN
,
5376 gcc_assert(p
->field_name() == "elem");
5377 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5380 gcc_assert(p
->field_name() == "dir");
5381 // These bits must match the ones in libgo/runtime/go-type.h.
5383 if (this->may_receive_
)
5385 if (this->may_send_
)
5388 mpz_init_set_ui(iv
, val
);
5389 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
5393 gcc_assert(p
== fields
->end());
5395 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
5398 // Reflection string.
5401 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5403 if (!this->may_send_
)
5405 ret
->append("chan");
5406 if (!this->may_receive_
)
5408 ret
->push_back(' ');
5409 this->append_reflection(this->element_type_
, gogo
, ret
);
5415 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5417 ret
->push_back('C');
5418 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5419 if (this->may_send_
)
5420 ret
->push_back('s');
5421 if (this->may_receive_
)
5422 ret
->push_back('r');
5423 ret
->push_back('e');
5429 Channel_type::do_export(Export
* exp
) const
5431 exp
->write_c_string("chan ");
5432 if (this->may_send_
&& !this->may_receive_
)
5433 exp
->write_c_string("-< ");
5434 else if (this->may_receive_
&& !this->may_send_
)
5435 exp
->write_c_string("<- ");
5436 exp
->write_type(this->element_type_
);
5442 Channel_type::do_import(Import
* imp
)
5444 imp
->require_c_string("chan ");
5448 if (imp
->match_c_string("-< "))
5452 may_receive
= false;
5454 else if (imp
->match_c_string("<- "))
5466 Type
* element_type
= imp
->read_type();
5468 return Type::make_channel_type(may_send
, may_receive
, element_type
);
5471 // Make a new channel type.
5474 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
5476 return new Channel_type(send
, receive
, element_type
);
5479 // Class Interface_type.
5484 Interface_type::do_traverse(Traverse
* traverse
)
5486 if (this->methods_
== NULL
)
5487 return TRAVERSE_CONTINUE
;
5488 return this->methods_
->traverse(traverse
);
5491 // Finalize the methods. This handles interface inheritance.
5494 Interface_type::finalize_methods()
5496 if (this->methods_
== NULL
)
5498 bool is_recursive
= false;
5501 while (from
< this->methods_
->size())
5503 const Typed_identifier
* p
= &this->methods_
->at(from
);
5504 if (!p
->name().empty())
5507 for (i
= 0; i
< to
; ++i
)
5509 if (this->methods_
->at(i
).name() == p
->name())
5511 error_at(p
->location(), "duplicate method %qs",
5512 Gogo::message_name(p
->name()).c_str());
5519 this->methods_
->set(to
, *p
);
5525 Interface_type
* it
= p
->type()->interface_type();
5528 error_at(p
->location(), "interface contains embedded non-interface");
5536 error_at(p
->location(), "invalid recursive interface");
5537 is_recursive
= true;
5542 const Typed_identifier_list
* methods
= it
->methods();
5543 if (methods
== NULL
)
5548 for (Typed_identifier_list::const_iterator q
= methods
->begin();
5549 q
!= methods
->end();
5552 if (q
->name().empty() || this->find_method(q
->name()) == NULL
)
5553 this->methods_
->push_back(Typed_identifier(q
->name(), q
->type(),
5558 error_at(p
->location(), "inherited method %qs is ambiguous",
5559 Gogo::message_name(q
->name()).c_str());
5566 delete this->methods_
;
5567 this->methods_
= NULL
;
5571 this->methods_
->resize(to
);
5572 this->methods_
->sort_by_name();
5576 // Return the method NAME, or NULL.
5578 const Typed_identifier
*
5579 Interface_type::find_method(const std::string
& name
) const
5581 if (this->methods_
== NULL
)
5583 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5584 p
!= this->methods_
->end();
5586 if (p
->name() == name
)
5591 // Return the method index.
5594 Interface_type::method_index(const std::string
& name
) const
5596 gcc_assert(this->methods_
!= NULL
);
5598 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5599 p
!= this->methods_
->end();
5601 if (p
->name() == name
)
5606 // Return whether NAME is an unexported method, for better error
5610 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
5612 if (this->methods_
== NULL
)
5614 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5615 p
!= this->methods_
->end();
5618 const std::string
& method_name(p
->name());
5619 if (Gogo::is_hidden_name(method_name
)
5620 && name
== Gogo::unpack_hidden_name(method_name
)
5621 && gogo
->pack_hidden_name(name
, false) != method_name
)
5627 // Whether this type is identical with T.
5630 Interface_type::is_identical(const Interface_type
* t
) const
5632 // We require the same methods with the same types. The methods
5633 // have already been sorted.
5634 if (this->methods() == NULL
|| t
->methods() == NULL
)
5635 return this->methods() == t
->methods();
5637 Typed_identifier_list::const_iterator p1
= this->methods()->begin();
5638 for (Typed_identifier_list::const_iterator p2
= t
->methods()->begin();
5639 p2
!= t
->methods()->end();
5642 if (p1
== this->methods()->end())
5644 if (p1
->name() != p2
->name()
5645 || !Type::are_identical(p1
->type(), p2
->type(), NULL
))
5648 if (p1
!= this->methods()->end())
5653 // Whether we can assign the interface type T to this type. The types
5654 // are known to not be identical. An interface assignment is only
5655 // permitted if T is known to implement all methods in THIS.
5656 // Otherwise a type guard is required.
5659 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
5660 std::string
* reason
) const
5662 if (this->methods() == NULL
)
5664 for (Typed_identifier_list::const_iterator p
= this->methods()->begin();
5665 p
!= this->methods()->end();
5668 const Typed_identifier
* m
= t
->find_method(p
->name());
5674 snprintf(buf
, sizeof buf
,
5675 _("need explicit conversion; missing method %s%s%s"),
5676 open_quote
, Gogo::message_name(p
->name()).c_str(),
5678 reason
->assign(buf
);
5683 std::string subreason
;
5684 if (!Type::are_identical(p
->type(), m
->type(), &subreason
))
5688 std::string n
= Gogo::message_name(p
->name());
5689 size_t len
= 100 + n
.length() + subreason
.length();
5690 char* buf
= new char[len
];
5691 if (subreason
.empty())
5692 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5693 open_quote
, n
.c_str(), close_quote
);
5696 _("incompatible type for method %s%s%s (%s)"),
5697 open_quote
, n
.c_str(), close_quote
,
5699 reason
->assign(buf
);
5712 Interface_type::do_hash_for_method(Gogo
* gogo
) const
5714 unsigned int ret
= 0;
5715 if (this->methods_
!= NULL
)
5717 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5718 p
!= this->methods_
->end();
5721 ret
= Type::hash_string(p
->name(), ret
);
5722 ret
+= p
->type()->hash_for_method(gogo
);
5729 // Return true if T implements the interface. If it does not, and
5730 // REASON is not NULL, set *REASON to a useful error message.
5733 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
5735 if (this->methods_
== NULL
)
5738 bool is_pointer
= false;
5739 const Named_type
* nt
= t
->named_type();
5740 const Struct_type
* st
= t
->struct_type();
5741 // If we start with a named type, we don't dereference it to find
5745 const Type
* pt
= t
->points_to();
5748 // If T is a pointer to a named type, then we need to look at
5749 // the type to which it points.
5751 nt
= pt
->named_type();
5752 st
= pt
->struct_type();
5756 // If we have a named type, get the methods from it rather than from
5761 // Only named and struct types have methods.
5762 if (nt
== NULL
&& st
== NULL
)
5766 if (t
->points_to() != NULL
5767 && t
->points_to()->interface_type() != NULL
)
5768 reason
->assign(_("pointer to interface type has no methods"));
5770 reason
->assign(_("type has no methods"));
5775 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
5779 if (t
->points_to() != NULL
5780 && t
->points_to()->interface_type() != NULL
)
5781 reason
->assign(_("pointer to interface type has no methods"));
5783 reason
->assign(_("type has no methods"));
5788 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5789 p
!= this->methods_
->end();
5792 bool is_ambiguous
= false;
5793 Method
* m
= (nt
!= NULL
5794 ? nt
->method_function(p
->name(), &is_ambiguous
)
5795 : st
->method_function(p
->name(), &is_ambiguous
));
5800 std::string n
= Gogo::message_name(p
->name());
5801 size_t len
= n
.length() + 100;
5802 char* buf
= new char[len
];
5804 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
5805 open_quote
, n
.c_str(), close_quote
);
5807 snprintf(buf
, len
, _("missing method %s%s%s"),
5808 open_quote
, n
.c_str(), close_quote
);
5809 reason
->assign(buf
);
5815 Function_type
*p_fn_type
= p
->type()->function_type();
5816 Function_type
* m_fn_type
= m
->type()->function_type();
5817 gcc_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
5818 std::string subreason
;
5819 if (!p_fn_type
->is_identical(m_fn_type
, true, &subreason
))
5823 std::string n
= Gogo::message_name(p
->name());
5824 size_t len
= 100 + n
.length() + subreason
.length();
5825 char* buf
= new char[len
];
5826 if (subreason
.empty())
5827 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5828 open_quote
, n
.c_str(), close_quote
);
5831 _("incompatible type for method %s%s%s (%s)"),
5832 open_quote
, n
.c_str(), close_quote
,
5834 reason
->assign(buf
);
5840 if (!is_pointer
&& !m
->is_value_method())
5844 std::string n
= Gogo::message_name(p
->name());
5845 size_t len
= 100 + n
.length();
5846 char* buf
= new char[len
];
5847 snprintf(buf
, len
, _("method %s%s%s requires a pointer"),
5848 open_quote
, n
.c_str(), close_quote
);
5849 reason
->assign(buf
);
5859 // Return a tree for an interface type. An interface is a pointer to
5860 // a struct. The struct has three fields. The first field is a
5861 // pointer to the type descriptor for the dynamic type of the object.
5862 // The second field is a pointer to a table of methods for the
5863 // interface to be used with the object. The third field is the value
5864 // of the object itself.
5867 Interface_type::do_get_tree(Gogo
* gogo
)
5869 if (this->methods_
== NULL
)
5871 // At the tree level, use the same type for all empty
5872 // interfaces. This lets us assign them to each other directly
5873 // without triggering GIMPLE type errors.
5874 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5875 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5876 static tree empty_interface
;
5877 return Gogo::builtin_struct(&empty_interface
, "__go_empty_interface",
5879 "__type_descriptor",
5885 return this->fill_in_tree(gogo
, make_node(RECORD_TYPE
));
5888 // Fill in the tree for an interface type. This is used for named
5892 Interface_type::fill_in_tree(Gogo
* gogo
, tree type
)
5894 gcc_assert(this->methods_
!= NULL
);
5896 // Build the type of the table of methods.
5898 tree method_table
= make_node(RECORD_TYPE
);
5900 // The first field is a pointer to the type descriptor.
5901 tree name_tree
= get_identifier("__type_descriptor");
5902 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5903 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5904 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, dtype
);
5905 DECL_CONTEXT(field
) = method_table
;
5906 TYPE_FIELDS(method_table
) = field
;
5908 std::string last_name
= "";
5909 tree
* pp
= &DECL_CHAIN(field
);
5910 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5911 p
!= this->methods_
->end();
5914 std::string name
= Gogo::unpack_hidden_name(p
->name());
5915 name_tree
= get_identifier_with_length(name
.data(), name
.length());
5916 tree field_type
= p
->type()->get_tree(gogo
);
5917 if (field_type
== error_mark_node
)
5918 return error_mark_node
;
5919 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, field_type
);
5920 DECL_CONTEXT(field
) = method_table
;
5922 pp
= &DECL_CHAIN(field
);
5923 // Sanity check: the names should be sorted.
5924 gcc_assert(p
->name() > last_name
);
5925 last_name
= p
->name();
5927 layout_type(method_table
);
5929 tree mtype
= build_pointer_type(method_table
);
5931 tree field_trees
= NULL_TREE
;
5934 name_tree
= get_identifier("__methods");
5935 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, mtype
);
5936 DECL_CONTEXT(field
) = type
;
5938 pp
= &DECL_CHAIN(field
);
5940 name_tree
= get_identifier("__object");
5941 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, ptr_type_node
);
5942 DECL_CONTEXT(field
) = type
;
5945 TYPE_FIELDS(type
) = field_trees
;
5952 // Initialization value.
5955 Interface_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5960 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
5961 for (tree field
= TYPE_FIELDS(type_tree
);
5963 field
= DECL_CHAIN(field
))
5965 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
5967 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
5970 tree ret
= build_constructor(type_tree
, init
);
5971 TREE_CONSTANT(ret
) = 1;
5975 // The type of an interface type descriptor.
5978 Interface_type::make_interface_type_descriptor_type()
5983 Type
* tdt
= Type::make_type_descriptor_type();
5984 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5986 Type
* string_type
= Type::lookup_string_type();
5987 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
5990 Type::make_builtin_struct_type(3,
5991 "name", pointer_string_type
,
5992 "pkgPath", pointer_string_type
,
5995 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
5997 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
5999 Struct_type
* s
= Type::make_builtin_struct_type(2,
6001 "methods", slice_nsm
);
6003 ret
= Type::make_builtin_named_type("InterfaceType", s
);
6009 // Build a type descriptor for an interface type.
6012 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6014 source_location bloc
= BUILTINS_LOCATION
;
6016 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
6018 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
6020 Expression_list
* ivals
= new Expression_list();
6023 Struct_field_list::const_iterator pif
= ifields
->begin();
6024 gcc_assert(pif
->field_name() == "commonType");
6025 ivals
->push_back(this->type_descriptor_constructor(gogo
,
6026 RUNTIME_TYPE_KIND_INTERFACE
,
6030 gcc_assert(pif
->field_name() == "methods");
6032 Expression_list
* methods
= new Expression_list();
6033 if (this->methods_
!= NULL
&& !this->methods_
->empty())
6035 Type
* elemtype
= pif
->type()->array_type()->element_type();
6037 methods
->reserve(this->methods_
->size());
6038 for (Typed_identifier_list::const_iterator pm
= this->methods_
->begin();
6039 pm
!= this->methods_
->end();
6042 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
6044 Expression_list
* mvals
= new Expression_list();
6047 Struct_field_list::const_iterator pmf
= mfields
->begin();
6048 gcc_assert(pmf
->field_name() == "name");
6049 std::string s
= Gogo::unpack_hidden_name(pm
->name());
6050 Expression
* e
= Expression::make_string(s
, bloc
);
6051 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6054 gcc_assert(pmf
->field_name() == "pkgPath");
6055 if (!Gogo::is_hidden_name(pm
->name()))
6056 mvals
->push_back(Expression::make_nil(bloc
));
6059 s
= Gogo::hidden_name_prefix(pm
->name());
6060 e
= Expression::make_string(s
, bloc
);
6061 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6065 gcc_assert(pmf
->field_name() == "typ");
6066 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
6069 gcc_assert(pmf
== mfields
->end());
6071 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
6073 methods
->push_back(e
);
6077 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
6081 gcc_assert(pif
== ifields
->end());
6083 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
6086 // Reflection string.
6089 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6091 ret
->append("interface {");
6092 if (this->methods_
!= NULL
)
6094 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6095 p
!= this->methods_
->end();
6098 if (p
!= this->methods_
->begin())
6100 ret
->push_back(' ');
6101 ret
->append(Gogo::unpack_hidden_name(p
->name()));
6102 std::string sub
= p
->type()->reflection(gogo
);
6103 gcc_assert(sub
.compare(0, 4, "func") == 0);
6104 sub
= sub
.substr(4);
6114 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6116 ret
->push_back('I');
6118 const Typed_identifier_list
* methods
= this->methods_
;
6119 if (methods
!= NULL
)
6121 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6122 p
!= methods
->end();
6125 std::string n
= Gogo::unpack_hidden_name(p
->name());
6127 snprintf(buf
, sizeof buf
, "%u_",
6128 static_cast<unsigned int>(n
.length()));
6131 this->append_mangled_name(p
->type(), gogo
, ret
);
6135 ret
->push_back('e');
6141 Interface_type::do_export(Export
* exp
) const
6143 exp
->write_c_string("interface { ");
6145 const Typed_identifier_list
* methods
= this->methods_
;
6146 if (methods
!= NULL
)
6148 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
6149 pm
!= methods
->end();
6152 exp
->write_string(pm
->name());
6153 exp
->write_c_string(" (");
6155 const Function_type
* fntype
= pm
->type()->function_type();
6158 const Typed_identifier_list
* parameters
= fntype
->parameters();
6159 if (parameters
!= NULL
)
6161 bool is_varargs
= fntype
->is_varargs();
6162 for (Typed_identifier_list::const_iterator pp
=
6163 parameters
->begin();
6164 pp
!= parameters
->end();
6170 exp
->write_c_string(", ");
6171 if (!is_varargs
|| pp
+ 1 != parameters
->end())
6172 exp
->write_type(pp
->type());
6175 exp
->write_c_string("...");
6176 Type
*pptype
= pp
->type();
6177 exp
->write_type(pptype
->array_type()->element_type());
6182 exp
->write_c_string(")");
6184 const Typed_identifier_list
* results
= fntype
->results();
6185 if (results
!= NULL
)
6187 exp
->write_c_string(" ");
6188 if (results
->size() == 1)
6189 exp
->write_type(results
->begin()->type());
6193 exp
->write_c_string("(");
6194 for (Typed_identifier_list::const_iterator p
=
6196 p
!= results
->end();
6202 exp
->write_c_string(", ");
6203 exp
->write_type(p
->type());
6205 exp
->write_c_string(")");
6209 exp
->write_c_string("; ");
6213 exp
->write_c_string("}");
6216 // Import an interface type.
6219 Interface_type::do_import(Import
* imp
)
6221 imp
->require_c_string("interface { ");
6223 Typed_identifier_list
* methods
= new Typed_identifier_list
;
6224 while (imp
->peek_char() != '}')
6226 std::string name
= imp
->read_identifier();
6227 imp
->require_c_string(" (");
6229 Typed_identifier_list
* parameters
;
6230 bool is_varargs
= false;
6231 if (imp
->peek_char() == ')')
6235 parameters
= new Typed_identifier_list
;
6238 if (imp
->match_c_string("..."))
6244 Type
* ptype
= imp
->read_type();
6246 ptype
= Type::make_array_type(ptype
, NULL
);
6247 parameters
->push_back(Typed_identifier(Import::import_marker
,
6248 ptype
, imp
->location()));
6249 if (imp
->peek_char() != ',')
6251 gcc_assert(!is_varargs
);
6252 imp
->require_c_string(", ");
6255 imp
->require_c_string(")");
6257 Typed_identifier_list
* results
;
6258 if (imp
->peek_char() != ' ')
6262 results
= new Typed_identifier_list
;
6264 if (imp
->peek_char() != '(')
6266 Type
* rtype
= imp
->read_type();
6267 results
->push_back(Typed_identifier(Import::import_marker
,
6268 rtype
, imp
->location()));
6275 Type
* rtype
= imp
->read_type();
6276 results
->push_back(Typed_identifier(Import::import_marker
,
6277 rtype
, imp
->location()));
6278 if (imp
->peek_char() != ',')
6280 imp
->require_c_string(", ");
6282 imp
->require_c_string(")");
6286 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
6290 fntype
->set_is_varargs();
6291 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
6293 imp
->require_c_string("; ");
6296 imp
->require_c_string("}");
6298 if (methods
->empty())
6304 return Type::make_interface_type(methods
, imp
->location());
6307 // Make an interface type.
6310 Type::make_interface_type(Typed_identifier_list
* methods
,
6311 source_location location
)
6313 return new Interface_type(methods
, location
);
6318 // Bind a method to an object.
6321 Method::bind_method(Expression
* expr
, source_location location
) const
6323 if (this->stub_
== NULL
)
6325 // When there is no stub object, the binding is determined by
6327 return this->do_bind_method(expr
, location
);
6330 Expression
* func
= Expression::make_func_reference(this->stub_
, NULL
,
6332 return Expression::make_bound_method(expr
, func
, location
);
6335 // Return the named object associated with a method. This may only be
6336 // called after methods are finalized.
6339 Method::named_object() const
6341 if (this->stub_
!= NULL
)
6343 return this->do_named_object();
6346 // Class Named_method.
6348 // The type of the method.
6351 Named_method::do_type() const
6353 if (this->named_object_
->is_function())
6354 return this->named_object_
->func_value()->type();
6355 else if (this->named_object_
->is_function_declaration())
6356 return this->named_object_
->func_declaration_value()->type();
6361 // Return the location of the method receiver.
6364 Named_method::do_receiver_location() const
6366 return this->do_type()->receiver()->location();
6369 // Bind a method to an object.
6372 Named_method::do_bind_method(Expression
* expr
, source_location location
) const
6374 Expression
* func
= Expression::make_func_reference(this->named_object_
, NULL
,
6376 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, func
,
6378 // If this is not a local method, and it does not use a stub, then
6379 // the real method expects a different type. We need to cast the
6381 if (this->depth() > 0 && !this->needs_stub_method())
6383 Function_type
* ftype
= this->do_type();
6384 gcc_assert(ftype
->is_method());
6385 Type
* frtype
= ftype
->receiver()->type();
6386 bme
->set_first_argument_type(frtype
);
6391 // Class Interface_method.
6393 // Bind a method to an object.
6396 Interface_method::do_bind_method(Expression
* expr
,
6397 source_location location
) const
6399 return Expression::make_interface_field_reference(expr
, this->name_
,
6405 // Insert a new method. Return true if it was inserted, false
6409 Methods::insert(const std::string
& name
, Method
* m
)
6411 std::pair
<Method_map::iterator
, bool> ins
=
6412 this->methods_
.insert(std::make_pair(name
, m
));
6417 Method
* old_method
= ins
.first
->second
;
6418 if (m
->depth() < old_method
->depth())
6421 ins
.first
->second
= m
;
6426 if (m
->depth() == old_method
->depth())
6427 old_method
->set_is_ambiguous();
6433 // Return the number of unambiguous methods.
6436 Methods::count() const
6439 for (Method_map::const_iterator p
= this->methods_
.begin();
6440 p
!= this->methods_
.end();
6442 if (!p
->second
->is_ambiguous())
6447 // Class Named_type.
6449 // Return the name of the type.
6452 Named_type::name() const
6454 return this->named_object_
->name();
6457 // Return the name of the type to use in an error message.
6460 Named_type::message_name() const
6462 return this->named_object_
->message_name();
6465 // Return the base type for this type. We have to be careful about
6466 // circular type definitions, which are invalid but may be seen here.
6469 Named_type::named_base()
6474 Type
* ret
= this->type_
->base();
6475 this->seen_
= false;
6480 Named_type::named_base() const
6485 const Type
* ret
= this->type_
->base();
6486 this->seen_
= false;
6490 // Return whether this is an error type. We have to be careful about
6491 // circular type definitions, which are invalid but may be seen here.
6494 Named_type::is_named_error_type() const
6499 bool ret
= this->type_
->is_error_type();
6500 this->seen_
= false;
6504 // Add a method to this type.
6507 Named_type::add_method(const std::string
& name
, Function
* function
)
6509 if (this->local_methods_
== NULL
)
6510 this->local_methods_
= new Bindings(NULL
);
6511 return this->local_methods_
->add_function(name
, NULL
, function
);
6514 // Add a method declaration to this type.
6517 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
6518 Function_type
* type
,
6519 source_location location
)
6521 if (this->local_methods_
== NULL
)
6522 this->local_methods_
= new Bindings(NULL
);
6523 return this->local_methods_
->add_function_declaration(name
, package
, type
,
6527 // Add an existing method to this type.
6530 Named_type::add_existing_method(Named_object
* no
)
6532 if (this->local_methods_
== NULL
)
6533 this->local_methods_
= new Bindings(NULL
);
6534 this->local_methods_
->add_named_object(no
);
6537 // Look for a local method NAME, and returns its named object, or NULL
6541 Named_type::find_local_method(const std::string
& name
) const
6543 if (this->local_methods_
== NULL
)
6545 return this->local_methods_
->lookup(name
);
6548 // Return whether NAME is an unexported field or method, for better
6552 Named_type::is_unexported_local_method(Gogo
* gogo
,
6553 const std::string
& name
) const
6555 Bindings
* methods
= this->local_methods_
;
6556 if (methods
!= NULL
)
6558 for (Bindings::const_declarations_iterator p
=
6559 methods
->begin_declarations();
6560 p
!= methods
->end_declarations();
6563 if (Gogo::is_hidden_name(p
->first
)
6564 && name
== Gogo::unpack_hidden_name(p
->first
)
6565 && gogo
->pack_hidden_name(name
, false) != p
->first
)
6572 // Build the complete list of methods for this type, which means
6573 // recursively including all methods for anonymous fields. Create all
6577 Named_type::finalize_methods(Gogo
* gogo
)
6579 if (this->local_methods_
!= NULL
6580 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
6582 const Bindings
* lm
= this->local_methods_
;
6583 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
6584 p
!= lm
->end_declarations();
6586 error_at(p
->second
->location(),
6587 "invalid pointer or interface receiver type");
6588 delete this->local_methods_
;
6589 this->local_methods_
= NULL
;
6593 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6596 // Return the method NAME, or NULL if there isn't one or if it is
6597 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6601 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6603 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6606 // Return a pointer to the interface method table for this type for
6607 // the interface INTERFACE. IS_POINTER is true if this is for a
6611 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
6614 gcc_assert(!interface
->is_empty());
6616 Interface_method_tables
** pimt
= (is_pointer
6617 ? &this->interface_method_tables_
6618 : &this->pointer_interface_method_tables_
);
6621 *pimt
= new Interface_method_tables(5);
6623 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
6624 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
6628 // This is a new entry in the hash table.
6629 gcc_assert(ins
.first
->second
== NULL_TREE
);
6630 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
6635 tree decl
= ins
.first
->second
;
6636 if (decl
== error_mark_node
)
6637 return error_mark_node
;
6638 gcc_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
6639 return build_fold_addr_expr(decl
);
6642 // Return whether a named type has any hidden fields.
6645 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
6650 bool ret
= this->type_
->has_hidden_fields(this, reason
);
6651 this->seen_
= false;
6655 // Look for a use of a complete type within another type. This is
6656 // used to check that we don't try to use a type within itself.
6658 class Find_type_use
: public Traverse
6661 Find_type_use(Type
* find_type
)
6662 : Traverse(traverse_types
),
6663 find_type_(find_type
), found_(false)
6666 // Whether we found the type.
6669 { return this->found_
; }
6676 // The type we are looking for.
6678 // Whether we found the type.
6682 // Check for FIND_TYPE in TYPE.
6685 Find_type_use::type(Type
* type
)
6687 if (this->find_type_
== type
)
6689 this->found_
= true;
6690 return TRAVERSE_EXIT
;
6692 // It's OK if we see a reference to the type in any type which is
6693 // essentially a pointer: a pointer, a slice, a function, a map, or
6695 if (type
->points_to() != NULL
6696 || type
->is_open_array_type()
6697 || type
->function_type() != NULL
6698 || type
->map_type() != NULL
6699 || type
->channel_type() != NULL
)
6700 return TRAVERSE_SKIP_COMPONENTS
;
6702 // For an interface, a reference to the type in a method type should
6703 // be ignored, but we have to consider direct inheritance. When
6704 // this is called, there may be cases of direct inheritance
6705 // represented as a method with no name.
6706 if (type
->interface_type() != NULL
)
6708 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
6709 if (methods
!= NULL
)
6711 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6712 p
!= methods
->end();
6715 if (p
->name().empty())
6717 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
6718 return TRAVERSE_EXIT
;
6722 return TRAVERSE_SKIP_COMPONENTS
;
6725 return TRAVERSE_CONTINUE
;
6728 // Verify that a named type does not refer to itself.
6731 Named_type::do_verify()
6733 Find_type_use
find(this);
6734 Type::traverse(this->type_
, &find
);
6737 error_at(this->location_
, "invalid recursive type %qs",
6738 this->message_name().c_str());
6739 this->is_error_
= true;
6743 // Check whether any of the local methods overloads an existing
6744 // struct field or interface method. We don't need to check the
6745 // list of methods against itself: that is handled by the Bindings
6747 if (this->local_methods_
!= NULL
)
6749 Struct_type
* st
= this->type_
->struct_type();
6750 Interface_type
* it
= this->type_
->interface_type();
6751 bool found_dup
= false;
6752 if (st
!= NULL
|| it
!= NULL
)
6754 for (Bindings::const_declarations_iterator p
=
6755 this->local_methods_
->begin_declarations();
6756 p
!= this->local_methods_
->end_declarations();
6759 const std::string
& name(p
->first
);
6760 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
6762 error_at(p
->second
->location(),
6763 "method %qs redeclares struct field name",
6764 Gogo::message_name(name
).c_str());
6767 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
6769 error_at(p
->second
->location(),
6770 "method %qs redeclares interface method name",
6771 Gogo::message_name(name
).c_str());
6783 // Return a hash code. This is used for method lookup. We simply
6784 // hash on the name itself.
6787 Named_type::do_hash_for_method(Gogo
* gogo
) const
6789 const std::string
& name(this->named_object()->name());
6790 unsigned int ret
= Type::hash_string(name
, 0);
6792 // GOGO will be NULL here when called from Type_hash_identical.
6793 // That is OK because that is only used for internal hash tables
6794 // where we are going to be comparing named types for equality. In
6795 // other cases, which are cases where the runtime is going to
6796 // compare hash codes to see if the types are the same, we need to
6797 // include the package prefix and name in the hash.
6798 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
6800 const Package
* package
= this->named_object()->package();
6801 if (package
== NULL
)
6803 ret
= Type::hash_string(gogo
->unique_prefix(), ret
);
6804 ret
= Type::hash_string(gogo
->package_name(), ret
);
6808 ret
= Type::hash_string(package
->unique_prefix(), ret
);
6809 ret
= Type::hash_string(package
->name(), ret
);
6816 // Get a tree for a named type.
6819 Named_type::do_get_tree(Gogo
* gogo
)
6821 if (this->is_error_
)
6822 return error_mark_node
;
6824 // Go permits types to refer to themselves in various ways. Break
6825 // the recursion here.
6827 switch (this->type_
->forwarded()->classification())
6830 return error_mark_node
;
6839 // These types can not refer to themselves.
6842 // All maps and channels have the same type in GENERIC.
6843 t
= Type::get_named_type_tree(gogo
, this->type_
);
6844 if (t
== error_mark_node
)
6845 return error_mark_node
;
6846 // Build a copy to set TYPE_NAME.
6847 t
= build_variant_type_copy(t
);
6851 // Don't recur infinitely if a function type refers to itself.
6852 // Ideally we would build a circular data structure here, but
6853 // GENERIC can't handle them.
6855 return ptr_type_node
;
6857 t
= Type::get_named_type_tree(gogo
, this->type_
);
6858 this->seen_
= false;
6859 if (t
== error_mark_node
)
6860 return error_mark_node
;
6861 t
= build_variant_type_copy(t
);
6865 // Don't recur infinitely if a pointer type refers to itself.
6866 // Ideally we would build a circular data structure here, but
6867 // GENERIC can't handle them.
6869 return ptr_type_node
;
6871 t
= Type::get_named_type_tree(gogo
, this->type_
);
6872 this->seen_
= false;
6873 if (t
== error_mark_node
)
6874 return error_mark_node
;
6875 t
= build_variant_type_copy(t
);
6879 if (this->named_tree_
!= NULL_TREE
)
6880 return this->named_tree_
;
6881 t
= make_node(RECORD_TYPE
);
6882 this->named_tree_
= t
;
6883 this->type_
->struct_type()->fill_in_tree(gogo
, t
);
6887 if (!this->is_open_array_type())
6888 t
= Type::get_named_type_tree(gogo
, this->type_
);
6891 if (this->named_tree_
!= NULL_TREE
)
6892 return this->named_tree_
;
6893 t
= gogo
->slice_type_tree(void_type_node
);
6894 this->named_tree_
= t
;
6895 t
= this->type_
->array_type()->fill_in_tree(gogo
, t
);
6897 if (t
== error_mark_node
)
6898 return error_mark_node
;
6899 t
= build_variant_type_copy(t
);
6902 case TYPE_INTERFACE
:
6903 if (this->type_
->interface_type()->is_empty())
6905 t
= Type::get_named_type_tree(gogo
, this->type_
);
6906 if (t
== error_mark_node
)
6907 return error_mark_node
;
6908 t
= build_variant_type_copy(t
);
6912 if (this->named_tree_
!= NULL_TREE
)
6913 return this->named_tree_
;
6914 t
= make_node(RECORD_TYPE
);
6915 this->named_tree_
= t
;
6916 t
= this->type_
->interface_type()->fill_in_tree(gogo
, t
);
6917 if (t
== error_mark_node
)
6918 return error_mark_node
;
6924 // When a named type T1 is defined as another named type T2,
6925 // the definition must simply be "type T1 T2". If the
6926 // definition of T2 may refer to T1, then we must simply
6927 // return the type for T2 here. It's not precisely correct,
6928 // but it's as close as we can get with GENERIC.
6929 bool was_seen
= this->seen_
;
6931 t
= Type::get_named_type_tree(gogo
, this->type_
);
6932 this->seen_
= was_seen
;
6935 if (t
== error_mark_node
)
6936 return error_mark_node
;
6937 t
= build_variant_type_copy(t
);
6942 // An undefined forwarding type. Make sure the error is
6944 this->type_
->forward_declaration_type()->real_type();
6945 return error_mark_node
;
6949 case TYPE_CALL_MULTIPLE_RESULT
:
6953 tree id
= this->named_object_
->get_id(gogo
);
6954 tree decl
= build_decl(this->location_
, TYPE_DECL
, id
, t
);
6955 TYPE_NAME(t
) = decl
;
6960 // Build a type descriptor for a named type.
6963 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6965 // If NAME is not NULL, then we don't really want the type
6966 // descriptor for this type; we want the descriptor for the
6967 // underlying type, giving it the name NAME.
6968 return this->named_type_descriptor(gogo
, this->type_
,
6969 name
== NULL
? this : name
);
6972 // Add to the reflection string. This is used mostly for the name of
6973 // the type used in a type descriptor, not for actual reflection
6977 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6979 if (this->location() != BUILTINS_LOCATION
)
6981 const Package
* package
= this->named_object_
->package();
6982 if (package
!= NULL
)
6983 ret
->append(package
->name());
6985 ret
->append(gogo
->package_name());
6986 ret
->push_back('.');
6988 if (this->in_function_
!= NULL
)
6990 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
6991 ret
->push_back('$');
6993 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
6996 // Get the mangled name.
6999 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7001 Named_object
* no
= this->named_object_
;
7003 if (this->location() == BUILTINS_LOCATION
)
7004 gcc_assert(this->in_function_
== NULL
);
7007 const std::string
& unique_prefix(no
->package() == NULL
7008 ? gogo
->unique_prefix()
7009 : no
->package()->unique_prefix());
7010 const std::string
& package_name(no
->package() == NULL
7011 ? gogo
->package_name()
7012 : no
->package()->name());
7013 name
= unique_prefix
;
7014 name
.append(1, '.');
7015 name
.append(package_name
);
7016 name
.append(1, '.');
7017 if (this->in_function_
!= NULL
)
7019 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7020 name
.append(1, '$');
7023 name
.append(Gogo::unpack_hidden_name(no
->name()));
7025 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
7030 // Export the type. This is called to export a global type.
7033 Named_type::export_named_type(Export
* exp
, const std::string
&) const
7035 // We don't need to write the name of the type here, because it will
7036 // be written by Export::write_type anyhow.
7037 exp
->write_c_string("type ");
7038 exp
->write_type(this);
7039 exp
->write_c_string(";\n");
7042 // Import a named type.
7045 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
7047 imp
->require_c_string("type ");
7048 Type
*type
= imp
->read_type();
7049 *ptype
= type
->named_type();
7050 gcc_assert(*ptype
!= NULL
);
7051 imp
->require_c_string(";\n");
7054 // Export the type when it is referenced by another type. In this
7055 // case Export::export_type will already have issued the name.
7058 Named_type::do_export(Export
* exp
) const
7060 exp
->write_type(this->type_
);
7062 // To save space, we only export the methods directly attached to
7064 Bindings
* methods
= this->local_methods_
;
7065 if (methods
== NULL
)
7068 exp
->write_c_string("\n");
7069 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
7070 p
!= methods
->end_definitions();
7073 exp
->write_c_string(" ");
7074 (*p
)->export_named_object(exp
);
7077 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
7078 p
!= methods
->end_declarations();
7081 if (p
->second
->is_function_declaration())
7083 exp
->write_c_string(" ");
7084 p
->second
->export_named_object(exp
);
7089 // Make a named type.
7092 Type::make_named_type(Named_object
* named_object
, Type
* type
,
7093 source_location location
)
7095 return new Named_type(named_object
, type
, location
);
7098 // Finalize the methods for TYPE. It will be a named type or a struct
7099 // type. This sets *ALL_METHODS to the list of methods, and builds
7100 // all required stubs.
7103 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, source_location location
,
7104 Methods
** all_methods
)
7106 *all_methods
= NULL
;
7107 Types_seen types_seen
;
7108 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
7110 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
7113 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7114 // build up the struct field indexes as we go. DEPTH is the depth of
7115 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7116 // adding these methods for an anonymous field with pointer type.
7117 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7118 // calls the real method. TYPES_SEEN is used to avoid infinite
7122 Type::add_methods_for_type(const Type
* type
,
7123 const Method::Field_indexes
* field_indexes
,
7125 bool is_embedded_pointer
,
7126 bool needs_stub_method
,
7127 Types_seen
* types_seen
,
7130 // Pointer types may not have methods.
7131 if (type
->points_to() != NULL
)
7134 const Named_type
* nt
= type
->named_type();
7137 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
7143 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
7144 is_embedded_pointer
, needs_stub_method
,
7147 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
7148 is_embedded_pointer
, needs_stub_method
,
7149 types_seen
, methods
);
7151 // If we are called with depth > 0, then we are looking at an
7152 // anonymous field of a struct. If such a field has interface type,
7153 // then we need to add the interface methods. We don't want to add
7154 // them when depth == 0, because we will already handle them
7155 // following the usual rules for an interface type.
7157 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
7160 // Add the local methods for the named type NT to *METHODS. The
7161 // parameters are as for add_methods_to_type.
7164 Type::add_local_methods_for_type(const Named_type
* nt
,
7165 const Method::Field_indexes
* field_indexes
,
7167 bool is_embedded_pointer
,
7168 bool needs_stub_method
,
7171 const Bindings
* local_methods
= nt
->local_methods();
7172 if (local_methods
== NULL
)
7175 if (*methods
== NULL
)
7176 *methods
= new Methods();
7178 for (Bindings::const_declarations_iterator p
=
7179 local_methods
->begin_declarations();
7180 p
!= local_methods
->end_declarations();
7183 Named_object
* no
= p
->second
;
7184 bool is_value_method
= (is_embedded_pointer
7185 || !Type::method_expects_pointer(no
));
7186 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
7188 || (depth
> 0 && is_value_method
)));
7189 if (!(*methods
)->insert(no
->name(), m
))
7194 // Add the embedded methods for TYPE to *METHODS. These are the
7195 // methods attached to anonymous fields. The parameters are as for
7196 // add_methods_to_type.
7199 Type::add_embedded_methods_for_type(const Type
* type
,
7200 const Method::Field_indexes
* field_indexes
,
7202 bool is_embedded_pointer
,
7203 bool needs_stub_method
,
7204 Types_seen
* types_seen
,
7207 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7209 const Struct_type
* st
= type
->struct_type();
7213 const Struct_field_list
* fields
= st
->fields();
7218 for (Struct_field_list::const_iterator pf
= fields
->begin();
7219 pf
!= fields
->end();
7222 if (!pf
->is_anonymous())
7225 Type
* ftype
= pf
->type();
7226 bool is_pointer
= false;
7227 if (ftype
->points_to() != NULL
)
7229 ftype
= ftype
->points_to();
7232 Named_type
* fnt
= ftype
->named_type();
7235 // This is an error, but it will be diagnosed elsewhere.
7239 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
7240 sub_field_indexes
->next
= field_indexes
;
7241 sub_field_indexes
->field_index
= i
;
7243 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
7244 (is_embedded_pointer
|| is_pointer
),
7253 // If TYPE is an interface type, then add its method to *METHODS.
7254 // This is for interface methods attached to an anonymous field. The
7255 // parameters are as for add_methods_for_type.
7258 Type::add_interface_methods_for_type(const Type
* type
,
7259 const Method::Field_indexes
* field_indexes
,
7263 const Interface_type
* it
= type
->interface_type();
7267 const Typed_identifier_list
* imethods
= it
->methods();
7268 if (imethods
== NULL
)
7271 if (*methods
== NULL
)
7272 *methods
= new Methods();
7274 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
7275 pm
!= imethods
->end();
7278 Function_type
* fntype
= pm
->type()->function_type();
7279 gcc_assert(fntype
!= NULL
&& !fntype
->is_method());
7280 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
7281 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
7282 field_indexes
, depth
);
7283 if (!(*methods
)->insert(pm
->name(), m
))
7288 // Build stub methods for TYPE as needed. METHODS is the set of
7289 // methods for the type. A stub method may be needed when a type
7290 // inherits a method from an anonymous field. When we need the
7291 // address of the method, as in a type descriptor, we need to build a
7292 // little stub which does the required field dereferences and jumps to
7293 // the real method. LOCATION is the location of the type definition.
7296 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
7297 source_location location
)
7299 if (methods
== NULL
)
7301 for (Methods::const_iterator p
= methods
->begin();
7302 p
!= methods
->end();
7305 Method
* m
= p
->second
;
7306 if (m
->is_ambiguous() || !m
->needs_stub_method())
7309 const std::string
& name(p
->first
);
7311 // Build a stub method.
7313 const Function_type
* fntype
= m
->type();
7315 static unsigned int counter
;
7317 snprintf(buf
, sizeof buf
, "$this%u", counter
);
7320 Type
* receiver_type
= const_cast<Type
*>(type
);
7321 if (!m
->is_value_method())
7322 receiver_type
= Type::make_pointer_type(receiver_type
);
7323 source_location receiver_location
= m
->receiver_location();
7324 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
7327 const Typed_identifier_list
* fnparams
= fntype
->parameters();
7328 Typed_identifier_list
* stub_params
;
7329 if (fnparams
== NULL
|| fnparams
->empty())
7333 // We give each stub parameter a unique name.
7334 stub_params
= new Typed_identifier_list();
7335 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
7336 pp
!= fnparams
->end();
7340 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
7341 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
7347 const Typed_identifier_list
* fnresults
= fntype
->results();
7348 Typed_identifier_list
* stub_results
;
7349 if (fnresults
== NULL
|| fnresults
->empty())
7350 stub_results
= NULL
;
7353 // We create the result parameters without any names, since
7354 // we won't refer to them.
7355 stub_results
= new Typed_identifier_list();
7356 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
7357 pr
!= fnresults
->end();
7359 stub_results
->push_back(Typed_identifier("", pr
->type(),
7363 Function_type
* stub_type
= Type::make_function_type(receiver
,
7366 fntype
->location());
7367 if (fntype
->is_varargs())
7368 stub_type
->set_is_varargs();
7370 // We only create the function in the package which creates the
7372 const Package
* package
;
7373 if (type
->named_type() == NULL
)
7376 package
= type
->named_type()->named_object()->package();
7378 if (package
!= NULL
)
7379 stub
= Named_object::make_function_declaration(name
, package
,
7380 stub_type
, location
);
7383 stub
= gogo
->start_function(name
, stub_type
, false,
7384 fntype
->location());
7385 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
7386 fntype
->is_varargs(), location
);
7387 gogo
->finish_function(fntype
->location());
7390 m
->set_stub_object(stub
);
7394 // Build a stub method which adjusts the receiver as required to call
7395 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7396 // PARAMS is the list of function parameters.
7399 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
7400 const char* receiver_name
,
7401 const Typed_identifier_list
* params
,
7403 source_location location
)
7405 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
7406 gcc_assert(receiver_object
!= NULL
);
7408 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
7409 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
7410 if (expr
->type()->points_to() == NULL
)
7411 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7413 Expression_list
* arguments
;
7414 if (params
== NULL
|| params
->empty())
7418 arguments
= new Expression_list();
7419 for (Typed_identifier_list::const_iterator p
= params
->begin();
7423 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
7424 gcc_assert(param
!= NULL
);
7425 Expression
* param_ref
= Expression::make_var_reference(param
,
7427 arguments
->push_back(param_ref
);
7431 Expression
* func
= method
->bind_method(expr
, location
);
7432 gcc_assert(func
!= NULL
);
7433 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
7435 size_t count
= call
->result_count();
7437 gogo
->add_statement(Statement::make_statement(call
));
7440 Expression_list
* retvals
= new Expression_list();
7442 retvals
->push_back(call
);
7445 for (size_t i
= 0; i
< count
; ++i
)
7446 retvals
->push_back(Expression::make_call_result(call
, i
));
7448 const Function
* function
= gogo
->current_function()->func_value();
7449 const Typed_identifier_list
* results
= function
->type()->results();
7450 Statement
* retstat
= Statement::make_return_statement(results
, retvals
,
7452 gogo
->add_statement(retstat
);
7456 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7457 // in reverse order.
7460 Type::apply_field_indexes(Expression
* expr
,
7461 const Method::Field_indexes
* field_indexes
,
7462 source_location location
)
7464 if (field_indexes
== NULL
)
7466 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
7467 Struct_type
* stype
= expr
->type()->deref()->struct_type();
7468 gcc_assert(stype
!= NULL
7469 && field_indexes
->field_index
< stype
->field_count());
7470 if (expr
->type()->struct_type() == NULL
)
7472 gcc_assert(expr
->type()->points_to() != NULL
);
7473 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7474 gcc_assert(expr
->type()->struct_type() == stype
);
7476 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
7480 // Return whether NO is a method for which the receiver is a pointer.
7483 Type::method_expects_pointer(const Named_object
* no
)
7485 const Function_type
*fntype
;
7486 if (no
->is_function())
7487 fntype
= no
->func_value()->type();
7488 else if (no
->is_function_declaration())
7489 fntype
= no
->func_declaration_value()->type();
7492 return fntype
->receiver()->type()->points_to() != NULL
;
7495 // Given a set of methods for a type, METHODS, return the method NAME,
7496 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7497 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7498 // but is ambiguous (and return NULL).
7501 Type::method_function(const Methods
* methods
, const std::string
& name
,
7504 if (is_ambiguous
!= NULL
)
7505 *is_ambiguous
= false;
7506 if (methods
== NULL
)
7508 Methods::const_iterator p
= methods
->find(name
);
7509 if (p
== methods
->end())
7511 Method
* m
= p
->second
;
7512 if (m
->is_ambiguous())
7514 if (is_ambiguous
!= NULL
)
7515 *is_ambiguous
= true;
7521 // Look for field or method NAME for TYPE. Return an Expression for
7522 // the field or method bound to EXPR. If there is no such field or
7523 // method, give an appropriate error and return an error expression.
7526 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
7527 const std::string
& name
,
7528 source_location location
)
7530 if (type
->is_error_type())
7531 return Expression::make_error(location
);
7533 const Named_type
* nt
= type
->named_type();
7535 nt
= type
->deref()->named_type();
7536 const Struct_type
* st
= type
->deref()->struct_type();
7537 const Interface_type
* it
= type
->deref()->interface_type();
7539 // If this is a pointer to a pointer, then it is possible that the
7540 // pointed-to type has methods.
7544 && type
->points_to() != NULL
7545 && type
->points_to()->points_to() != NULL
)
7547 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7548 type
= type
->points_to();
7549 nt
= type
->points_to()->named_type();
7550 st
= type
->points_to()->struct_type();
7551 it
= type
->points_to()->interface_type();
7554 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
7555 || expr
->is_addressable());
7556 bool is_method
= false;
7557 bool found_pointer_method
= false;
7560 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
, NULL
,
7561 &is_method
, &found_pointer_method
,
7567 gcc_assert(st
!= NULL
);
7568 if (type
->struct_type() == NULL
)
7570 gcc_assert(type
->points_to() != NULL
);
7571 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
7573 gcc_assert(expr
->type()->struct_type() == st
);
7575 ret
= st
->field_reference(expr
, name
, location
);
7577 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7578 ret
= Expression::make_interface_field_reference(expr
, name
,
7584 m
= nt
->method_function(name
, NULL
);
7585 else if (st
!= NULL
)
7586 m
= st
->method_function(name
, NULL
);
7589 gcc_assert(m
!= NULL
);
7590 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
7591 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7592 ret
= m
->bind_method(expr
, location
);
7594 gcc_assert(ret
!= NULL
);
7599 if (!ambig1
.empty())
7600 error_at(location
, "%qs is ambiguous via %qs and %qs",
7601 Gogo::message_name(name
).c_str(),
7602 Gogo::message_name(ambig1
).c_str(),
7603 Gogo::message_name(ambig2
).c_str());
7604 else if (found_pointer_method
)
7605 error_at(location
, "method requires a pointer");
7606 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
7608 ("reference to field %qs in object which "
7609 "has no fields or methods"),
7610 Gogo::message_name(name
).c_str());
7614 if (!Gogo::is_hidden_name(name
))
7615 is_unexported
= false;
7618 std::string unpacked
= Gogo::unpack_hidden_name(name
);
7619 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
7623 error_at(location
, "reference to unexported field or method %qs",
7624 Gogo::message_name(name
).c_str());
7626 error_at(location
, "reference to undefined field or method %qs",
7627 Gogo::message_name(name
).c_str());
7629 return Expression::make_error(location
);
7633 // Look in TYPE for a field or method named NAME, return true if one
7634 // is found. This looks through embedded anonymous fields and handles
7635 // ambiguity. If a method is found, sets *IS_METHOD to true;
7636 // otherwise, if a field is found, set it to false. If
7637 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7638 // whose address can not be taken. When returning false, this sets
7639 // *FOUND_POINTER_METHOD if we found a method we couldn't use because
7640 // it requires a pointer. LEVEL is used for recursive calls, and can
7641 // be NULL for a non-recursive call. When this function returns false
7642 // because it finds that the name is ambiguous, it will store a path
7643 // to the ambiguous names in *AMBIG1 and *AMBIG2. If the name is not
7644 // found at all, *AMBIG1 and *AMBIG2 will be unchanged.
7646 // This function just returns whether or not there is a field or
7647 // method, and whether it is a field or method. It doesn't build an
7648 // expression to refer to it. If it is a method, we then look in the
7649 // list of all methods for the type. If it is a field, the search has
7650 // to be done again, looking only for fields, and building up the
7651 // expression as we go.
7654 Type::find_field_or_method(const Type
* type
,
7655 const std::string
& name
,
7656 bool receiver_can_be_pointer
,
7659 bool* found_pointer_method
,
7660 std::string
* ambig1
,
7661 std::string
* ambig2
)
7663 // Named types can have locally defined methods.
7664 const Named_type
* nt
= type
->named_type();
7665 if (nt
== NULL
&& type
->points_to() != NULL
)
7666 nt
= type
->points_to()->named_type();
7669 Named_object
* no
= nt
->find_local_method(name
);
7672 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
7678 // Record that we have found a pointer method in order to
7679 // give a better error message if we don't find anything
7681 *found_pointer_method
= true;
7685 // Interface types can have methods.
7686 const Interface_type
* it
= type
->deref()->interface_type();
7687 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7693 // Struct types can have fields. They can also inherit fields and
7694 // methods from anonymous fields.
7695 const Struct_type
* st
= type
->deref()->struct_type();
7698 const Struct_field_list
* fields
= st
->fields();
7702 int found_level
= 0;
7703 bool found_is_method
= false;
7704 std::string found_ambig1
;
7705 std::string found_ambig2
;
7706 const Struct_field
* found_parent
= NULL
;
7707 for (Struct_field_list::const_iterator pf
= fields
->begin();
7708 pf
!= fields
->end();
7711 if (pf
->field_name() == name
)
7717 if (!pf
->is_anonymous())
7720 Named_type
* fnt
= pf
->type()->deref()->named_type();
7721 gcc_assert(fnt
!= NULL
);
7723 int sublevel
= level
== NULL
? 1 : *level
+ 1;
7725 std::string subambig1
;
7726 std::string subambig2
;
7727 bool subfound
= Type::find_field_or_method(fnt
,
7729 receiver_can_be_pointer
,
7732 found_pointer_method
,
7737 if (!subambig1
.empty())
7739 // The name was found via this field, but is ambiguous.
7740 // if the ambiguity is lower or at the same level as
7741 // anything else we have already found, then we want to
7742 // pass the ambiguity back to the caller.
7743 if (found_level
== 0 || sublevel
<= found_level
)
7745 found_ambig1
= pf
->field_name() + '.' + subambig1
;
7746 found_ambig2
= pf
->field_name() + '.' + subambig2
;
7747 found_level
= sublevel
;
7753 // The name was found via this field. Use the level to see
7754 // if we want to use this one, or whether it introduces an
7756 if (found_level
== 0 || sublevel
< found_level
)
7758 found_level
= sublevel
;
7759 found_is_method
= sub_is_method
;
7760 found_ambig1
.clear();
7761 found_ambig2
.clear();
7762 found_parent
= &*pf
;
7764 else if (sublevel
> found_level
)
7766 else if (found_ambig1
.empty())
7768 // We found an ambiguity.
7769 gcc_assert(found_parent
!= NULL
);
7770 found_ambig1
= found_parent
->field_name();
7771 found_ambig2
= pf
->field_name();
7775 // We found an ambiguity, but we already know of one.
7776 // Just report the earlier one.
7781 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7782 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7783 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7784 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7786 if (found_level
== 0)
7788 else if (!found_ambig1
.empty())
7790 gcc_assert(!found_ambig1
.empty());
7791 ambig1
->assign(found_ambig1
);
7792 ambig2
->assign(found_ambig2
);
7794 *level
= found_level
;
7800 *level
= found_level
;
7801 *is_method
= found_is_method
;
7806 // Return whether NAME is an unexported field or method for TYPE.
7809 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
7810 const std::string
& name
)
7812 type
= type
->deref();
7814 const Named_type
* nt
= type
->named_type();
7815 if (nt
!= NULL
&& nt
->is_unexported_local_method(gogo
, name
))
7818 const Interface_type
* it
= type
->interface_type();
7819 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
7822 const Struct_type
* st
= type
->struct_type();
7823 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
7829 const Struct_field_list
* fields
= st
->fields();
7833 for (Struct_field_list::const_iterator pf
= fields
->begin();
7834 pf
!= fields
->end();
7837 if (pf
->is_anonymous())
7839 Named_type
* subtype
= pf
->type()->deref()->named_type();
7840 gcc_assert(subtype
!= NULL
);
7841 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
))
7849 // Class Forward_declaration.
7851 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
7852 : Type(TYPE_FORWARD
),
7853 named_object_(named_object
->resolve()), warned_(false)
7855 gcc_assert(this->named_object_
->is_unknown()
7856 || this->named_object_
->is_type_declaration());
7859 // Return the named object.
7862 Forward_declaration_type::named_object()
7864 return this->named_object_
->resolve();
7868 Forward_declaration_type::named_object() const
7870 return this->named_object_
->resolve();
7873 // Return the name of the forward declared type.
7876 Forward_declaration_type::name() const
7878 return this->named_object()->name();
7881 // Warn about a use of a type which has been declared but not defined.
7884 Forward_declaration_type::warn() const
7886 Named_object
* no
= this->named_object_
->resolve();
7887 if (no
->is_unknown())
7889 // The name was not defined anywhere.
7892 error_at(this->named_object_
->location(),
7893 "use of undefined type %qs",
7894 no
->message_name().c_str());
7895 this->warned_
= true;
7898 else if (no
->is_type_declaration())
7900 // The name was seen as a type, but the type was never defined.
7901 if (no
->type_declaration_value()->using_type())
7903 error_at(this->named_object_
->location(),
7904 "use of undefined type %qs",
7905 no
->message_name().c_str());
7906 this->warned_
= true;
7911 // The name was defined, but not as a type.
7914 error_at(this->named_object_
->location(), "expected type");
7915 this->warned_
= true;
7920 // Get the base type of a declaration. This gives an error if the
7921 // type has not yet been defined.
7924 Forward_declaration_type::real_type()
7926 if (this->is_defined())
7927 return this->named_object()->type_value();
7931 return Type::make_error_type();
7936 Forward_declaration_type::real_type() const
7938 if (this->is_defined())
7939 return this->named_object()->type_value();
7943 return Type::make_error_type();
7947 // Return whether the base type is defined.
7950 Forward_declaration_type::is_defined() const
7952 return this->named_object()->is_type();
7955 // Add a method. This is used when methods are defined before the
7959 Forward_declaration_type::add_method(const std::string
& name
,
7962 Named_object
* no
= this->named_object();
7963 gcc_assert(no
->is_type_declaration());
7964 return no
->type_declaration_value()->add_method(name
, function
);
7967 // Add a method declaration. This is used when methods are declared
7971 Forward_declaration_type::add_method_declaration(const std::string
& name
,
7972 Function_type
* type
,
7973 source_location location
)
7975 Named_object
* no
= this->named_object();
7976 gcc_assert(no
->is_type_declaration());
7977 Type_declaration
* td
= no
->type_declaration_value();
7978 return td
->add_method_declaration(name
, type
, location
);
7984 Forward_declaration_type::do_traverse(Traverse
* traverse
)
7986 if (this->is_defined()
7987 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
7988 return TRAVERSE_EXIT
;
7989 return TRAVERSE_CONTINUE
;
7992 // Get a tree for the type.
7995 Forward_declaration_type::do_get_tree(Gogo
* gogo
)
7997 if (this->is_defined())
7998 return Type::get_named_type_tree(gogo
, this->real_type());
8001 return error_mark_node
;
8003 // We represent an undefined type as a struct with no fields. That
8004 // should work fine for the middle-end, since the same case can
8006 Named_object
* no
= this->named_object();
8007 tree type_tree
= make_node(RECORD_TYPE
);
8008 tree id
= no
->get_id(gogo
);
8009 tree decl
= build_decl(no
->location(), TYPE_DECL
, id
, type_tree
);
8010 TYPE_NAME(type_tree
) = decl
;
8014 // Build a type descriptor for a forwarded type.
8017 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8019 if (!this->is_defined())
8020 return Expression::make_nil(BUILTINS_LOCATION
);
8023 Type
* t
= this->real_type();
8025 return this->named_type_descriptor(gogo
, t
, name
);
8027 return Expression::make_type_descriptor(t
, BUILTINS_LOCATION
);
8031 // The reflection string.
8034 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8036 this->append_reflection(this->real_type(), gogo
, ret
);
8039 // The mangled name.
8042 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8044 if (this->is_defined())
8045 this->append_mangled_name(this->real_type(), gogo
, ret
);
8048 const Named_object
* no
= this->named_object();
8050 if (no
->package() == NULL
)
8051 name
= gogo
->package_name();
8053 name
= no
->package()->name();
8055 name
+= Gogo::unpack_hidden_name(no
->name());
8057 snprintf(buf
, sizeof buf
, "N%u_",
8058 static_cast<unsigned int>(name
.length()));
8064 // Export a forward declaration. This can happen when a defined type
8065 // refers to a type which is only declared (and is presumably defined
8066 // in some other file in the same package).
8069 Forward_declaration_type::do_export(Export
*) const
8071 // If there is a base type, that should be exported instead of this.
8072 gcc_assert(!this->is_defined());
8074 // We don't output anything.
8077 // Make a forward declaration.
8080 Type::make_forward_declaration(Named_object
* named_object
)
8082 return new Forward_declaration_type(named_object
);
8085 // Class Typed_identifier_list.
8087 // Sort the entries by name.
8089 struct Typed_identifier_list_sort
8093 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
8094 { return t1
.name() < t2
.name(); }
8098 Typed_identifier_list::sort_by_name()
8100 std::sort(this->entries_
.begin(), this->entries_
.end(),
8101 Typed_identifier_list_sort());
8107 Typed_identifier_list::traverse(Traverse
* traverse
)
8109 for (Typed_identifier_list::const_iterator p
= this->begin();
8113 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
8114 return TRAVERSE_EXIT
;
8116 return TRAVERSE_CONTINUE
;
8121 Typed_identifier_list
*
8122 Typed_identifier_list::copy() const
8124 Typed_identifier_list
* ret
= new Typed_identifier_list();
8125 for (Typed_identifier_list::const_iterator p
= this->begin();
8128 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));