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 if (this->is_error_type())
795 return error_mark_node
;
797 // To avoid confusing GIMPLE, we need to translate all identical Go
798 // types to the same GIMPLE type. We use a hash table to do that.
799 // There is no need to use the hash table for named types, as named
800 // types are only identical to themselves.
802 std::pair
<Type
*, tree
> val(this, NULL
);
803 std::pair
<Type_trees::iterator
, bool> ins
=
804 Type::type_trees
.insert(val
);
805 if (!ins
.second
&& ins
.first
->second
!= NULL_TREE
)
807 this->tree_
= ins
.first
->second
;
811 tree t
= this->get_tree_without_hash(gogo
);
813 // Don't store errors in the hash table. This type might be a
814 // pointer to an error type or something like that. Since error
815 // types are identical to everything else, that could cause us to
816 // return error_mark_node for pointers to any type, which will then
818 if (t
== error_mark_node
)
819 return error_mark_node
;
821 if (ins
.first
->second
== NULL_TREE
)
822 ins
.first
->second
= t
;
825 // We have already created a tree for this type. This can
826 // happen when an unnamed type is defined using a named type
827 // which in turns uses an identical unnamed type. Use the tree
828 // we created earlier and ignore the one we just built.
829 t
= ins
.first
->second
;
836 // Return a tree for a type without looking in the hash table for
837 // identical types. This is used for named types, since there is no
838 // point to looking in the hash table for them.
841 Type::get_tree_without_hash(Gogo
* gogo
)
843 if (this->tree_
== NULL_TREE
)
845 tree t
= this->do_get_tree(gogo
);
847 // For a recursive function or pointer type, we will temporarily
848 // return ptr_type_node during the recursion. We don't want to
849 // record that for a forwarding type, as it may confuse us
851 if (t
== ptr_type_node
&& this->forward_declaration_type() != NULL
)
855 go_preserve_from_gc(t
);
861 // Return a tree representing a zero initialization for this type.
864 Type::get_init_tree(Gogo
* gogo
, bool is_clear
)
866 tree type_tree
= this->get_tree(gogo
);
867 if (type_tree
== error_mark_node
)
868 return error_mark_node
;
869 return this->do_get_init_tree(gogo
, type_tree
, is_clear
);
872 // Any type which supports the builtin make function must implement
876 Type::do_make_expression_tree(Translate_context
*, Expression_list
*,
882 // Return a pointer to the type descriptor for this type.
885 Type::type_descriptor_pointer(Gogo
* gogo
)
887 Type
* t
= this->forwarded();
888 if (t
->type_descriptor_decl_
== NULL_TREE
)
890 Expression
* e
= t
->do_type_descriptor(gogo
, NULL
);
891 gogo
->build_type_descriptor_decl(t
, e
, &t
->type_descriptor_decl_
);
892 gcc_assert(t
->type_descriptor_decl_
!= NULL_TREE
893 && (t
->type_descriptor_decl_
== error_mark_node
894 || DECL_P(t
->type_descriptor_decl_
)));
896 if (t
->type_descriptor_decl_
== error_mark_node
)
897 return error_mark_node
;
898 return build_fold_addr_expr(t
->type_descriptor_decl_
);
901 // Return a composite literal for a type descriptor.
904 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
906 return type
->do_type_descriptor(gogo
, NULL
);
909 // Return a composite literal for a type descriptor with a name.
912 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
914 gcc_assert(name
!= NULL
&& type
->named_type() != name
);
915 return type
->do_type_descriptor(gogo
, name
);
918 // Make a builtin struct type from a list of fields. The fields are
919 // pairs of a name and a type.
922 Type::make_builtin_struct_type(int nfields
, ...)
925 va_start(ap
, nfields
);
927 source_location bloc
= BUILTINS_LOCATION
;
928 Struct_field_list
* sfl
= new Struct_field_list();
929 for (int i
= 0; i
< nfields
; i
++)
931 const char* field_name
= va_arg(ap
, const char *);
932 Type
* type
= va_arg(ap
, Type
*);
933 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
938 return Type::make_struct_type(sfl
, bloc
);
941 // Make a builtin named type.
944 Type::make_builtin_named_type(const char* name
, Type
* type
)
946 source_location bloc
= BUILTINS_LOCATION
;
947 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
948 return no
->type_value();
951 // Return the type of a type descriptor. We should really tie this to
952 // runtime.Type rather than copying it. This must match commonType in
953 // libgo/go/runtime/type.go.
956 Type::make_type_descriptor_type()
961 source_location bloc
= BUILTINS_LOCATION
;
963 Type
* uint8_type
= Type::lookup_integer_type("uint8");
964 Type
* uint32_type
= Type::lookup_integer_type("uint32");
965 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
966 Type
* string_type
= Type::lookup_string_type();
967 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
969 // This is an unnamed version of unsafe.Pointer. Perhaps we
970 // should use the named version instead, although that would
971 // require us to create the unsafe package if it has not been
972 // imported. It probably doesn't matter.
973 Type
* void_type
= Type::make_void_type();
974 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
976 // Forward declaration for the type descriptor type.
977 Named_object
* named_type_descriptor_type
=
978 Named_object::make_type_declaration("commonType", NULL
, bloc
);
979 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
980 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
982 // The type of a method on a concrete type.
983 Struct_type
* method_type
=
984 Type::make_builtin_struct_type(5,
985 "name", pointer_string_type
,
986 "pkgPath", pointer_string_type
,
987 "mtyp", pointer_type_descriptor_type
,
988 "typ", pointer_type_descriptor_type
,
989 "tfn", unsafe_pointer_type
);
990 Named_type
* named_method_type
=
991 Type::make_builtin_named_type("method", method_type
);
993 // Information for types with a name or methods.
994 Type
* slice_named_method_type
=
995 Type::make_array_type(named_method_type
, NULL
);
996 Struct_type
* uncommon_type
=
997 Type::make_builtin_struct_type(3,
998 "name", pointer_string_type
,
999 "pkgPath", pointer_string_type
,
1000 "methods", slice_named_method_type
);
1001 Named_type
* named_uncommon_type
=
1002 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1004 Type
* pointer_uncommon_type
=
1005 Type::make_pointer_type(named_uncommon_type
);
1007 // The type descriptor type.
1009 Typed_identifier_list
* params
= new Typed_identifier_list();
1010 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1011 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1013 Typed_identifier_list
* results
= new Typed_identifier_list();
1014 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1016 Type
* hashfn_type
= Type::make_function_type(NULL
, params
, results
, bloc
);
1018 params
= new Typed_identifier_list();
1019 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1020 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1021 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1023 results
= new Typed_identifier_list();
1024 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1026 Type
* equalfn_type
= Type::make_function_type(NULL
, params
, results
,
1029 Struct_type
* type_descriptor_type
=
1030 Type::make_builtin_struct_type(9,
1032 "align", uint8_type
,
1033 "fieldAlign", uint8_type
,
1034 "size", uintptr_type
,
1035 "hash", uint32_type
,
1036 "hashfn", hashfn_type
,
1037 "equalfn", equalfn_type
,
1038 "string", pointer_string_type
,
1039 "", pointer_uncommon_type
);
1041 Named_type
* named
= Type::make_builtin_named_type("commonType",
1042 type_descriptor_type
);
1044 named_type_descriptor_type
->set_type_value(named
);
1052 // Make the type of a pointer to a type descriptor as represented in
1056 Type::make_type_descriptor_ptr_type()
1060 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1064 // Return the names of runtime functions which compute a hash code for
1065 // this type and which compare whether two values of this type are
1069 Type::type_functions(const char** hash_fn
, const char** equal_fn
) const
1071 switch (this->base()->classification())
1073 case Type::TYPE_ERROR
:
1074 case Type::TYPE_VOID
:
1075 case Type::TYPE_NIL
:
1076 // These types can not be hashed or compared.
1077 *hash_fn
= "__go_type_hash_error";
1078 *equal_fn
= "__go_type_equal_error";
1081 case Type::TYPE_BOOLEAN
:
1082 case Type::TYPE_INTEGER
:
1083 case Type::TYPE_FLOAT
:
1084 case Type::TYPE_COMPLEX
:
1085 case Type::TYPE_POINTER
:
1086 case Type::TYPE_FUNCTION
:
1087 case Type::TYPE_MAP
:
1088 case Type::TYPE_CHANNEL
:
1089 *hash_fn
= "__go_type_hash_identity";
1090 *equal_fn
= "__go_type_equal_identity";
1093 case Type::TYPE_STRING
:
1094 *hash_fn
= "__go_type_hash_string";
1095 *equal_fn
= "__go_type_equal_string";
1098 case Type::TYPE_STRUCT
:
1099 case Type::TYPE_ARRAY
:
1100 // These types can not be hashed or compared.
1101 *hash_fn
= "__go_type_hash_error";
1102 *equal_fn
= "__go_type_equal_error";
1105 case Type::TYPE_INTERFACE
:
1106 if (this->interface_type()->is_empty())
1108 *hash_fn
= "__go_type_hash_empty_interface";
1109 *equal_fn
= "__go_type_equal_empty_interface";
1113 *hash_fn
= "__go_type_hash_interface";
1114 *equal_fn
= "__go_type_equal_interface";
1118 case Type::TYPE_NAMED
:
1119 case Type::TYPE_FORWARD
:
1127 // Return a composite literal for the type descriptor for a plain type
1128 // of kind RUNTIME_TYPE_KIND named NAME.
1131 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1132 Named_type
* name
, const Methods
* methods
,
1133 bool only_value_methods
)
1135 source_location bloc
= BUILTINS_LOCATION
;
1137 Type
* td_type
= Type::make_type_descriptor_type();
1138 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1140 Expression_list
* vals
= new Expression_list();
1143 Struct_field_list::const_iterator p
= fields
->begin();
1144 gcc_assert(p
->field_name() == "Kind");
1146 mpz_init_set_ui(iv
, runtime_type_kind
);
1147 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1150 gcc_assert(p
->field_name() == "align");
1151 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1152 vals
->push_back(Expression::make_type_info(this, type_info
));
1155 gcc_assert(p
->field_name() == "fieldAlign");
1156 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1157 vals
->push_back(Expression::make_type_info(this, type_info
));
1160 gcc_assert(p
->field_name() == "size");
1161 type_info
= Expression::TYPE_INFO_SIZE
;
1162 vals
->push_back(Expression::make_type_info(this, type_info
));
1165 gcc_assert(p
->field_name() == "hash");
1166 mpz_set_ui(iv
, this->hash_for_method(gogo
));
1167 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1169 const char* hash_fn
;
1170 const char* equal_fn
;
1171 this->type_functions(&hash_fn
, &equal_fn
);
1174 gcc_assert(p
->field_name() == "hashfn");
1175 Function_type
* fntype
= p
->type()->function_type();
1176 Named_object
* no
= Named_object::make_function_declaration(hash_fn
, NULL
,
1179 no
->func_declaration_value()->set_asm_name(hash_fn
);
1180 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1183 gcc_assert(p
->field_name() == "equalfn");
1184 fntype
= p
->type()->function_type();
1185 no
= Named_object::make_function_declaration(equal_fn
, NULL
, fntype
, bloc
);
1186 no
->func_declaration_value()->set_asm_name(equal_fn
);
1187 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1190 gcc_assert(p
->field_name() == "string");
1191 Expression
* s
= Expression::make_string((name
!= NULL
1192 ? name
->reflection(gogo
)
1193 : this->reflection(gogo
)),
1195 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1198 gcc_assert(p
->field_name() == "uncommonType");
1199 if (name
== NULL
&& methods
== NULL
)
1200 vals
->push_back(Expression::make_nil(bloc
));
1203 if (methods
== NULL
)
1204 methods
= name
->methods();
1205 vals
->push_back(this->uncommon_type_constructor(gogo
,
1208 only_value_methods
));
1212 gcc_assert(p
== fields
->end());
1216 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
1219 // Return a composite literal for the uncommon type information for
1220 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1221 // struct. If name is not NULL, it is the name of the type. If
1222 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1223 // is true if only value methods should be included. At least one of
1224 // NAME and METHODS must not be NULL.
1227 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
1228 Named_type
* name
, const Methods
* methods
,
1229 bool only_value_methods
) const
1231 source_location bloc
= BUILTINS_LOCATION
;
1233 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
1235 Expression_list
* vals
= new Expression_list();
1238 Struct_field_list::const_iterator p
= fields
->begin();
1239 gcc_assert(p
->field_name() == "name");
1242 gcc_assert(p
->field_name() == "pkgPath");
1246 vals
->push_back(Expression::make_nil(bloc
));
1247 vals
->push_back(Expression::make_nil(bloc
));
1251 Named_object
* no
= name
->named_object();
1252 std::string n
= Gogo::unpack_hidden_name(no
->name());
1253 Expression
* s
= Expression::make_string(n
, bloc
);
1254 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1256 if (name
->is_builtin())
1257 vals
->push_back(Expression::make_nil(bloc
));
1260 const Package
* package
= no
->package();
1261 const std::string
& unique_prefix(package
== NULL
1262 ? gogo
->unique_prefix()
1263 : package
->unique_prefix());
1264 const std::string
& package_name(package
== NULL
1265 ? gogo
->package_name()
1267 n
.assign(unique_prefix
);
1269 n
.append(package_name
);
1270 if (name
->in_function() != NULL
)
1273 n
.append(Gogo::unpack_hidden_name(name
->in_function()->name()));
1275 s
= Expression::make_string(n
, bloc
);
1276 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1281 gcc_assert(p
->field_name() == "methods");
1282 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
1283 only_value_methods
));
1286 gcc_assert(p
== fields
->end());
1288 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
1290 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
1293 // Sort methods by name.
1299 operator()(const std::pair
<std::string
, const Method
*>& m1
,
1300 const std::pair
<std::string
, const Method
*>& m2
) const
1301 { return m1
.first
< m2
.first
; }
1304 // Return a composite literal for the type method table for this type.
1305 // METHODS_TYPE is the type of the table, and is a slice type.
1306 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1307 // then only value methods are used.
1310 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
1311 const Methods
* methods
,
1312 bool only_value_methods
) const
1314 source_location bloc
= BUILTINS_LOCATION
;
1316 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
1317 if (methods
!= NULL
)
1319 smethods
.reserve(methods
->count());
1320 for (Methods::const_iterator p
= methods
->begin();
1321 p
!= methods
->end();
1324 if (p
->second
->is_ambiguous())
1326 if (only_value_methods
&& !p
->second
->is_value_method())
1328 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
1332 if (smethods
.empty())
1333 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
1335 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
1337 Type
* method_type
= methods_type
->array_type()->element_type();
1339 Expression_list
* vals
= new Expression_list();
1340 vals
->reserve(smethods
.size());
1341 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
1343 p
!= smethods
.end();
1345 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
1348 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
1351 // Return a composite literal for a single method. METHOD_TYPE is the
1352 // type of the entry. METHOD_NAME is the name of the method and M is
1353 // the method information.
1356 Type::method_constructor(Gogo
*, Type
* method_type
,
1357 const std::string
& method_name
,
1358 const Method
* m
) const
1360 source_location bloc
= BUILTINS_LOCATION
;
1362 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
1364 Expression_list
* vals
= new Expression_list();
1367 Struct_field_list::const_iterator p
= fields
->begin();
1368 gcc_assert(p
->field_name() == "name");
1369 const std::string n
= Gogo::unpack_hidden_name(method_name
);
1370 Expression
* s
= Expression::make_string(n
, bloc
);
1371 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1374 gcc_assert(p
->field_name() == "pkgPath");
1375 if (!Gogo::is_hidden_name(method_name
))
1376 vals
->push_back(Expression::make_nil(bloc
));
1379 s
= Expression::make_string(Gogo::hidden_name_prefix(method_name
), bloc
);
1380 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1383 Named_object
* no
= (m
->needs_stub_method()
1385 : m
->named_object());
1387 Function_type
* mtype
;
1388 if (no
->is_function())
1389 mtype
= no
->func_value()->type();
1391 mtype
= no
->func_declaration_value()->type();
1392 gcc_assert(mtype
->is_method());
1393 Type
* nonmethod_type
= mtype
->copy_without_receiver();
1396 gcc_assert(p
->field_name() == "mtyp");
1397 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
1400 gcc_assert(p
->field_name() == "typ");
1401 vals
->push_back(Expression::make_type_descriptor(mtype
, bloc
));
1404 gcc_assert(p
->field_name() == "tfn");
1405 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1408 gcc_assert(p
== fields
->end());
1410 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
1413 // Return a composite literal for the type descriptor of a plain type.
1414 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1415 // NULL, it is the name to use as well as the list of methods.
1418 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
1421 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
1425 // Return the type reflection string for this type.
1428 Type::reflection(Gogo
* gogo
) const
1432 // The do_reflection virtual function should set RET to the
1433 // reflection string.
1434 this->do_reflection(gogo
, &ret
);
1439 // Return a mangled name for the type.
1442 Type::mangled_name(Gogo
* gogo
) const
1446 // The do_mangled_name virtual function should set RET to the
1447 // mangled name. For a composite type it should append a code for
1448 // the composition and then call do_mangled_name on the components.
1449 this->do_mangled_name(gogo
, &ret
);
1454 // Default function to export a type.
1457 Type::do_export(Export
*) const
1465 Type::import_type(Import
* imp
)
1467 if (imp
->match_c_string("("))
1468 return Function_type::do_import(imp
);
1469 else if (imp
->match_c_string("*"))
1470 return Pointer_type::do_import(imp
);
1471 else if (imp
->match_c_string("struct "))
1472 return Struct_type::do_import(imp
);
1473 else if (imp
->match_c_string("["))
1474 return Array_type::do_import(imp
);
1475 else if (imp
->match_c_string("map "))
1476 return Map_type::do_import(imp
);
1477 else if (imp
->match_c_string("chan "))
1478 return Channel_type::do_import(imp
);
1479 else if (imp
->match_c_string("interface"))
1480 return Interface_type::do_import(imp
);
1483 error_at(imp
->location(), "import error: expected type");
1484 return Type::make_error_type();
1488 // A type used to indicate a parsing error. This exists to simplify
1489 // later error detection.
1491 class Error_type
: public Type
1501 { return error_mark_node
; }
1504 do_get_init_tree(Gogo
*, tree
, bool)
1505 { return error_mark_node
; }
1508 do_type_descriptor(Gogo
*, Named_type
*)
1509 { return Expression::make_error(BUILTINS_LOCATION
); }
1512 do_reflection(Gogo
*, std::string
*) const
1513 { gcc_assert(saw_errors()); }
1516 do_mangled_name(Gogo
*, std::string
* ret
) const
1517 { ret
->push_back('E'); }
1521 Type::make_error_type()
1523 static Error_type singleton_error_type
;
1524 return &singleton_error_type
;
1529 class Void_type
: public Type
1539 { return void_type_node
; }
1542 do_get_init_tree(Gogo
*, tree
, bool)
1543 { gcc_unreachable(); }
1546 do_type_descriptor(Gogo
*, Named_type
*)
1547 { gcc_unreachable(); }
1550 do_reflection(Gogo
*, std::string
*) const
1554 do_mangled_name(Gogo
*, std::string
* ret
) const
1555 { ret
->push_back('v'); }
1559 Type::make_void_type()
1561 static Void_type singleton_void_type
;
1562 return &singleton_void_type
;
1565 // The boolean type.
1567 class Boolean_type
: public Type
1571 : Type(TYPE_BOOLEAN
)
1577 { return boolean_type_node
; }
1580 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1581 { return is_clear
? NULL
: fold_convert(type_tree
, boolean_false_node
); }
1584 do_type_descriptor(Gogo
*, Named_type
* name
);
1586 // We should not be asked for the reflection string of a basic type.
1588 do_reflection(Gogo
*, std::string
* ret
) const
1589 { ret
->append("bool"); }
1592 do_mangled_name(Gogo
*, std::string
* ret
) const
1593 { ret
->push_back('b'); }
1596 // Make the type descriptor.
1599 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1602 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
1605 Named_object
* no
= gogo
->lookup_global("bool");
1606 gcc_assert(no
!= NULL
);
1607 return Type::type_descriptor(gogo
, no
->type_value());
1612 Type::make_boolean_type()
1614 static Boolean_type boolean_type
;
1615 return &boolean_type
;
1618 // The named type "bool".
1620 static Named_type
* named_bool_type
;
1622 // Get the named type "bool".
1625 Type::lookup_bool_type()
1627 return named_bool_type
;
1630 // Make the named type "bool".
1633 Type::make_named_bool_type()
1635 Type
* bool_type
= Type::make_boolean_type();
1636 Named_object
* named_object
= Named_object::make_type("bool", NULL
,
1639 Named_type
* named_type
= named_object
->type_value();
1640 named_bool_type
= named_type
;
1644 // Class Integer_type.
1646 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
1648 // Create a new integer type. Non-abstract integer types always have
1652 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
1653 int bits
, int runtime_type_kind
)
1655 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
1657 std::string
sname(name
);
1658 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1661 Named_type
* named_type
= named_object
->type_value();
1662 std::pair
<Named_integer_types::iterator
, bool> ins
=
1663 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
1664 gcc_assert(ins
.second
);
1668 // Look up an existing integer type.
1671 Integer_type::lookup_integer_type(const char* name
)
1673 Named_integer_types::const_iterator p
=
1674 Integer_type::named_integer_types
.find(name
);
1675 gcc_assert(p
!= Integer_type::named_integer_types
.end());
1679 // Create a new abstract integer type.
1682 Integer_type::create_abstract_integer_type()
1684 static Integer_type
* abstract_type
;
1685 if (abstract_type
== NULL
)
1686 abstract_type
= new Integer_type(true, false, INT_TYPE_SIZE
,
1687 RUNTIME_TYPE_KIND_INT
);
1688 return abstract_type
;
1691 // Integer type compatibility.
1694 Integer_type::is_identical(const Integer_type
* t
) const
1696 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
1698 return this->is_abstract_
== t
->is_abstract_
;
1704 Integer_type::do_hash_for_method(Gogo
*) const
1706 return ((this->bits_
<< 4)
1707 + ((this->is_unsigned_
? 1 : 0) << 8)
1708 + ((this->is_abstract_
? 1 : 0) << 9));
1711 // Get the tree for an Integer_type.
1714 Integer_type::do_get_tree(Gogo
*)
1716 gcc_assert(!this->is_abstract_
);
1717 if (this->is_unsigned_
)
1719 if (this->bits_
== INT_TYPE_SIZE
)
1720 return unsigned_type_node
;
1721 else if (this->bits_
== CHAR_TYPE_SIZE
)
1722 return unsigned_char_type_node
;
1723 else if (this->bits_
== SHORT_TYPE_SIZE
)
1724 return short_unsigned_type_node
;
1725 else if (this->bits_
== LONG_TYPE_SIZE
)
1726 return long_unsigned_type_node
;
1727 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1728 return long_long_unsigned_type_node
;
1730 return make_unsigned_type(this->bits_
);
1734 if (this->bits_
== INT_TYPE_SIZE
)
1735 return integer_type_node
;
1736 else if (this->bits_
== CHAR_TYPE_SIZE
)
1737 return signed_char_type_node
;
1738 else if (this->bits_
== SHORT_TYPE_SIZE
)
1739 return short_integer_type_node
;
1740 else if (this->bits_
== LONG_TYPE_SIZE
)
1741 return long_integer_type_node
;
1742 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1743 return long_long_integer_type_node
;
1745 return make_signed_type(this->bits_
);
1750 Integer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1752 return is_clear
? NULL
: build_int_cst(type_tree
, 0);
1755 // The type descriptor for an integer type. Integer types are always
1759 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1761 gcc_assert(name
!= NULL
);
1762 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1765 // We should not be asked for the reflection string of a basic type.
1768 Integer_type::do_reflection(Gogo
*, std::string
*) const
1776 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1779 snprintf(buf
, sizeof buf
, "i%s%s%de",
1780 this->is_abstract_
? "a" : "",
1781 this->is_unsigned_
? "u" : "",
1786 // Make an integer type.
1789 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
1790 int runtime_type_kind
)
1792 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
1796 // Make an abstract integer type.
1799 Type::make_abstract_integer_type()
1801 return Integer_type::create_abstract_integer_type();
1804 // Look up an integer type.
1807 Type::lookup_integer_type(const char* name
)
1809 return Integer_type::lookup_integer_type(name
);
1812 // Class Float_type.
1814 Float_type::Named_float_types
Float_type::named_float_types
;
1816 // Create a new float type. Non-abstract float types always have
1820 Float_type::create_float_type(const char* name
, int bits
,
1821 int runtime_type_kind
)
1823 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
1824 std::string
sname(name
);
1825 Named_object
* named_object
= Named_object::make_type(sname
, NULL
, float_type
,
1827 Named_type
* named_type
= named_object
->type_value();
1828 std::pair
<Named_float_types::iterator
, bool> ins
=
1829 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
1830 gcc_assert(ins
.second
);
1834 // Look up an existing float type.
1837 Float_type::lookup_float_type(const char* name
)
1839 Named_float_types::const_iterator p
=
1840 Float_type::named_float_types
.find(name
);
1841 gcc_assert(p
!= Float_type::named_float_types
.end());
1845 // Create a new abstract float type.
1848 Float_type::create_abstract_float_type()
1850 static Float_type
* abstract_type
;
1851 if (abstract_type
== NULL
)
1852 abstract_type
= new Float_type(true, FLOAT_TYPE_SIZE
,
1853 RUNTIME_TYPE_KIND_FLOAT
);
1854 return abstract_type
;
1857 // Whether this type is identical with T.
1860 Float_type::is_identical(const Float_type
* t
) const
1862 if (this->bits_
!= t
->bits_
)
1864 return this->is_abstract_
== t
->is_abstract_
;
1870 Float_type::do_hash_for_method(Gogo
*) const
1872 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
1875 // Get a tree without using a Gogo*.
1878 Float_type::type_tree() const
1880 if (this->bits_
== FLOAT_TYPE_SIZE
)
1881 return float_type_node
;
1882 else if (this->bits_
== DOUBLE_TYPE_SIZE
)
1883 return double_type_node
;
1884 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
)
1885 return long_double_type_node
;
1888 tree ret
= make_node(REAL_TYPE
);
1889 TYPE_PRECISION(ret
) = this->bits_
;
1898 Float_type::do_get_tree(Gogo
*)
1900 return this->type_tree();
1904 Float_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1909 real_from_integer(&r
, TYPE_MODE(type_tree
), 0, 0, 0);
1910 return build_real(type_tree
, r
);
1913 // The type descriptor for a float type. Float types are always named.
1916 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1918 gcc_assert(name
!= NULL
);
1919 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1922 // We should not be asked for the reflection string of a basic type.
1925 Float_type::do_reflection(Gogo
*, std::string
*) const
1933 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1936 snprintf(buf
, sizeof buf
, "f%s%de",
1937 this->is_abstract_
? "a" : "",
1942 // Make a floating point type.
1945 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
1947 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
1950 // Make an abstract float type.
1953 Type::make_abstract_float_type()
1955 return Float_type::create_abstract_float_type();
1958 // Look up a float type.
1961 Type::lookup_float_type(const char* name
)
1963 return Float_type::lookup_float_type(name
);
1966 // Class Complex_type.
1968 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
1970 // Create a new complex type. Non-abstract complex types always have
1974 Complex_type::create_complex_type(const char* name
, int bits
,
1975 int runtime_type_kind
)
1977 Complex_type
* complex_type
= new Complex_type(false, bits
,
1979 std::string
sname(name
);
1980 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1983 Named_type
* named_type
= named_object
->type_value();
1984 std::pair
<Named_complex_types::iterator
, bool> ins
=
1985 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
1987 gcc_assert(ins
.second
);
1991 // Look up an existing complex type.
1994 Complex_type::lookup_complex_type(const char* name
)
1996 Named_complex_types::const_iterator p
=
1997 Complex_type::named_complex_types
.find(name
);
1998 gcc_assert(p
!= Complex_type::named_complex_types
.end());
2002 // Create a new abstract complex type.
2005 Complex_type::create_abstract_complex_type()
2007 static Complex_type
* abstract_type
;
2008 if (abstract_type
== NULL
)
2009 abstract_type
= new Complex_type(true, FLOAT_TYPE_SIZE
* 2,
2010 RUNTIME_TYPE_KIND_FLOAT
);
2011 return abstract_type
;
2014 // Whether this type is identical with T.
2017 Complex_type::is_identical(const Complex_type
*t
) const
2019 if (this->bits_
!= t
->bits_
)
2021 return this->is_abstract_
== t
->is_abstract_
;
2027 Complex_type::do_hash_for_method(Gogo
*) const
2029 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2032 // Get a tree without using a Gogo*.
2035 Complex_type::type_tree() const
2037 if (this->bits_
== FLOAT_TYPE_SIZE
* 2)
2038 return complex_float_type_node
;
2039 else if (this->bits_
== DOUBLE_TYPE_SIZE
* 2)
2040 return complex_double_type_node
;
2041 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
* 2)
2042 return complex_long_double_type_node
;
2045 tree ret
= make_node(REAL_TYPE
);
2046 TYPE_PRECISION(ret
) = this->bits_
/ 2;
2048 return build_complex_type(ret
);
2055 Complex_type::do_get_tree(Gogo
*)
2057 return this->type_tree();
2060 // Zero initializer.
2063 Complex_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2068 real_from_integer(&r
, TYPE_MODE(TREE_TYPE(type_tree
)), 0, 0, 0);
2069 return build_complex(type_tree
, build_real(TREE_TYPE(type_tree
), r
),
2070 build_real(TREE_TYPE(type_tree
), r
));
2073 // The type descriptor for a complex type. Complex types are always
2077 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2079 gcc_assert(name
!= NULL
);
2080 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2083 // We should not be asked for the reflection string of a basic type.
2086 Complex_type::do_reflection(Gogo
*, std::string
*) const
2094 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2097 snprintf(buf
, sizeof buf
, "c%s%de",
2098 this->is_abstract_
? "a" : "",
2103 // Make a complex type.
2106 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
2108 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
2111 // Make an abstract complex type.
2114 Type::make_abstract_complex_type()
2116 return Complex_type::create_abstract_complex_type();
2119 // Look up a complex type.
2122 Type::lookup_complex_type(const char* name
)
2124 return Complex_type::lookup_complex_type(name
);
2127 // Class String_type.
2129 // Return the tree for String_type. A string is a struct with two
2130 // fields: a pointer to the characters and a length.
2133 String_type::do_get_tree(Gogo
*)
2135 static tree struct_type
;
2136 return Gogo::builtin_struct(&struct_type
, "__go_string", NULL_TREE
, 2,
2138 build_pointer_type(unsigned_char_type_node
),
2143 // Return a tree for the length of STRING.
2146 String_type::length_tree(Gogo
*, tree string
)
2148 tree string_type
= TREE_TYPE(string
);
2149 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2150 tree length_field
= DECL_CHAIN(TYPE_FIELDS(string_type
));
2151 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field
)),
2153 return fold_build3(COMPONENT_REF
, integer_type_node
, string
,
2154 length_field
, NULL_TREE
);
2157 // Return a tree for a pointer to the bytes of STRING.
2160 String_type::bytes_tree(Gogo
*, tree string
)
2162 tree string_type
= TREE_TYPE(string
);
2163 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2164 tree bytes_field
= TYPE_FIELDS(string_type
);
2165 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field
)),
2167 return fold_build3(COMPONENT_REF
, TREE_TYPE(bytes_field
), string
,
2168 bytes_field
, NULL_TREE
);
2171 // We initialize a string to { NULL, 0 }.
2174 String_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2179 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2181 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2183 for (tree field
= TYPE_FIELDS(type_tree
);
2185 field
= DECL_CHAIN(field
))
2187 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2189 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
2192 tree ret
= build_constructor(type_tree
, init
);
2193 TREE_CONSTANT(ret
) = 1;
2197 // The type descriptor for the string type.
2200 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2203 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
2206 Named_object
* no
= gogo
->lookup_global("string");
2207 gcc_assert(no
!= NULL
);
2208 return Type::type_descriptor(gogo
, no
->type_value());
2212 // We should not be asked for the reflection string of a basic type.
2215 String_type::do_reflection(Gogo
*, std::string
* ret
) const
2217 ret
->append("string");
2220 // Mangled name of a string type.
2223 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2225 ret
->push_back('z');
2228 // Make a string type.
2231 Type::make_string_type()
2233 static String_type string_type
;
2234 return &string_type
;
2237 // The named type "string".
2239 static Named_type
* named_string_type
;
2241 // Get the named type "string".
2244 Type::lookup_string_type()
2246 return named_string_type
;
2249 // Make the named type string.
2252 Type::make_named_string_type()
2254 Type
* string_type
= Type::make_string_type();
2255 Named_object
* named_object
= Named_object::make_type("string", NULL
,
2258 Named_type
* named_type
= named_object
->type_value();
2259 named_string_type
= named_type
;
2263 // The sink type. This is the type of the blank identifier _. Any
2264 // type may be assigned to it.
2266 class Sink_type
: public Type
2276 { gcc_unreachable(); }
2279 do_get_init_tree(Gogo
*, tree
, bool)
2280 { gcc_unreachable(); }
2283 do_type_descriptor(Gogo
*, Named_type
*)
2284 { gcc_unreachable(); }
2287 do_reflection(Gogo
*, std::string
*) const
2288 { gcc_unreachable(); }
2291 do_mangled_name(Gogo
*, std::string
*) const
2292 { gcc_unreachable(); }
2295 // Make the sink type.
2298 Type::make_sink_type()
2300 static Sink_type sink_type
;
2304 // Class Function_type.
2309 Function_type::do_traverse(Traverse
* traverse
)
2311 if (this->receiver_
!= NULL
2312 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
2313 return TRAVERSE_EXIT
;
2314 if (this->parameters_
!= NULL
2315 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
2316 return TRAVERSE_EXIT
;
2317 if (this->results_
!= NULL
2318 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
2319 return TRAVERSE_EXIT
;
2320 return TRAVERSE_CONTINUE
;
2323 // Returns whether T is a valid redeclaration of this type. If this
2324 // returns false, and REASON is not NULL, *REASON may be set to a
2325 // brief explanation of why it returned false.
2328 Function_type::is_valid_redeclaration(const Function_type
* t
,
2329 std::string
* reason
) const
2331 if (!this->is_identical(t
, false, reason
))
2334 // A redeclaration of a function is required to use the same names
2335 // for the receiver and parameters.
2336 if (this->receiver() != NULL
2337 && this->receiver()->name() != t
->receiver()->name()
2338 && this->receiver()->name() != Import::import_marker
2339 && t
->receiver()->name() != Import::import_marker
)
2342 *reason
= "receiver name changed";
2346 const Typed_identifier_list
* parms1
= this->parameters();
2347 const Typed_identifier_list
* parms2
= t
->parameters();
2350 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2351 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2352 p2
!= parms2
->end();
2355 if (p1
->name() != p2
->name()
2356 && p1
->name() != Import::import_marker
2357 && p2
->name() != Import::import_marker
)
2360 *reason
= "parameter name changed";
2364 // This is called at parse time, so we may have unknown
2366 Type
* t1
= p1
->type()->forwarded();
2367 Type
* t2
= p2
->type()->forwarded();
2369 && t1
->forward_declaration_type() != NULL
2370 && (t2
->forward_declaration_type() == NULL
2371 || (t1
->forward_declaration_type()->named_object()
2372 != t2
->forward_declaration_type()->named_object())))
2377 const Typed_identifier_list
* results1
= this->results();
2378 const Typed_identifier_list
* results2
= t
->results();
2379 if (results1
!= NULL
)
2381 Typed_identifier_list::const_iterator res1
= results1
->begin();
2382 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2383 res2
!= results2
->end();
2386 if (res1
->name() != res2
->name()
2387 && res1
->name() != Import::import_marker
2388 && res2
->name() != Import::import_marker
)
2391 *reason
= "result name changed";
2395 // This is called at parse time, so we may have unknown
2397 Type
* t1
= res1
->type()->forwarded();
2398 Type
* t2
= res2
->type()->forwarded();
2400 && t1
->forward_declaration_type() != NULL
2401 && (t2
->forward_declaration_type() == NULL
2402 || (t1
->forward_declaration_type()->named_object()
2403 != t2
->forward_declaration_type()->named_object())))
2411 // Check whether T is the same as this type.
2414 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
2415 std::string
* reason
) const
2417 if (!ignore_receiver
)
2419 const Typed_identifier
* r1
= this->receiver();
2420 const Typed_identifier
* r2
= t
->receiver();
2421 if ((r1
!= NULL
) != (r2
!= NULL
))
2424 *reason
= _("different receiver types");
2429 if (!Type::are_identical(r1
->type(), r2
->type(), reason
))
2431 if (reason
!= NULL
&& !reason
->empty())
2432 *reason
= "receiver: " + *reason
;
2438 const Typed_identifier_list
* parms1
= this->parameters();
2439 const Typed_identifier_list
* parms2
= t
->parameters();
2440 if ((parms1
!= NULL
) != (parms2
!= NULL
))
2443 *reason
= _("different number of parameters");
2448 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2449 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2450 p2
!= parms2
->end();
2453 if (p1
== parms1
->end())
2456 *reason
= _("different number of parameters");
2460 if (!Type::are_identical(p1
->type(), p2
->type(), NULL
))
2463 *reason
= _("different parameter types");
2467 if (p1
!= parms1
->end())
2470 *reason
= _("different number of parameters");
2475 if (this->is_varargs() != t
->is_varargs())
2478 *reason
= _("different varargs");
2482 const Typed_identifier_list
* results1
= this->results();
2483 const Typed_identifier_list
* results2
= t
->results();
2484 if ((results1
!= NULL
) != (results2
!= NULL
))
2487 *reason
= _("different number of results");
2490 if (results1
!= NULL
)
2492 Typed_identifier_list::const_iterator res1
= results1
->begin();
2493 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2494 res2
!= results2
->end();
2497 if (res1
== results1
->end())
2500 *reason
= _("different number of results");
2504 if (!Type::are_identical(res1
->type(), res2
->type(), NULL
))
2507 *reason
= _("different result types");
2511 if (res1
!= results1
->end())
2514 *reason
= _("different number of results");
2525 Function_type::do_hash_for_method(Gogo
* gogo
) const
2527 unsigned int ret
= 0;
2528 // We ignore the receiver type for hash codes, because we need to
2529 // get the same hash code for a method in an interface and a method
2530 // declared for a type. The former will not have a receiver.
2531 if (this->parameters_
!= NULL
)
2534 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2535 p
!= this->parameters_
->end();
2537 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2539 if (this->results_
!= NULL
)
2542 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2543 p
!= this->results_
->end();
2545 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2547 if (this->is_varargs_
)
2553 // Get the tree for a function type.
2556 Function_type::do_get_tree(Gogo
* gogo
)
2558 tree args
= NULL_TREE
;
2561 if (this->receiver_
!= NULL
)
2563 Type
* rtype
= this->receiver_
->type();
2564 tree ptype
= rtype
->get_tree(gogo
);
2565 if (ptype
== error_mark_node
)
2566 return error_mark_node
;
2568 // We always pass the address of the receiver parameter, in
2569 // order to make interface calls work with unknown types.
2570 if (rtype
->points_to() == NULL
)
2571 ptype
= build_pointer_type(ptype
);
2573 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2574 pp
= &TREE_CHAIN (*pp
);
2577 if (this->parameters_
!= NULL
)
2579 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2580 p
!= this->parameters_
->end();
2583 tree ptype
= p
->type()->get_tree(gogo
);
2584 if (ptype
== error_mark_node
)
2585 return error_mark_node
;
2586 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2587 pp
= &TREE_CHAIN (*pp
);
2591 // Varargs is handled entirely at the Go level. At the tree level,
2592 // functions are not varargs.
2593 *pp
= void_list_node
;
2596 if (this->results_
== NULL
)
2597 result
= void_type_node
;
2598 else if (this->results_
->size() == 1)
2599 result
= this->results_
->begin()->type()->get_tree(gogo
);
2602 result
= make_node(RECORD_TYPE
);
2603 tree field_trees
= NULL_TREE
;
2604 tree
* pp
= &field_trees
;
2605 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2606 p
!= this->results_
->end();
2609 const std::string name
= (p
->name().empty()
2611 : Gogo::unpack_hidden_name(p
->name()));
2612 tree name_tree
= get_identifier_with_length(name
.data(),
2614 tree field_type_tree
= p
->type()->get_tree(gogo
);
2615 if (field_type_tree
== error_mark_node
)
2616 return error_mark_node
;
2617 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
2619 DECL_CONTEXT(field
) = result
;
2621 pp
= &DECL_CHAIN(field
);
2623 TYPE_FIELDS(result
) = field_trees
;
2624 layout_type(result
);
2627 if (result
== error_mark_node
)
2628 return error_mark_node
;
2630 tree fntype
= build_function_type(result
, args
);
2631 if (fntype
== error_mark_node
)
2634 return build_pointer_type(fntype
);
2637 // Functions are initialized to NULL.
2640 Function_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2644 return fold_convert(type_tree
, null_pointer_node
);
2647 // The type of a function type descriptor.
2650 Function_type::make_function_type_descriptor_type()
2655 Type
* tdt
= Type::make_type_descriptor_type();
2656 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
2658 Type
* bool_type
= Type::lookup_bool_type();
2660 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
2662 Struct_type
* s
= Type::make_builtin_struct_type(4,
2664 "dotdotdot", bool_type
,
2668 ret
= Type::make_builtin_named_type("FuncType", s
);
2674 // The type descriptor for a function type.
2677 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2679 source_location bloc
= BUILTINS_LOCATION
;
2681 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
2683 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
2685 Expression_list
* vals
= new Expression_list();
2688 Struct_field_list::const_iterator p
= fields
->begin();
2689 gcc_assert(p
->field_name() == "commonType");
2690 vals
->push_back(this->type_descriptor_constructor(gogo
,
2691 RUNTIME_TYPE_KIND_FUNC
,
2695 gcc_assert(p
->field_name() == "dotdotdot");
2696 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
2699 gcc_assert(p
->field_name() == "in");
2700 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
2701 this->parameters()));
2704 gcc_assert(p
->field_name() == "out");
2705 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
2709 gcc_assert(p
== fields
->end());
2711 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
2714 // Return a composite literal for the parameters or results of a type
2718 Function_type::type_descriptor_params(Type
* params_type
,
2719 const Typed_identifier
* receiver
,
2720 const Typed_identifier_list
* params
)
2722 source_location bloc
= BUILTINS_LOCATION
;
2724 if (receiver
== NULL
&& params
== NULL
)
2725 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
2727 Expression_list
* vals
= new Expression_list();
2728 vals
->reserve((params
== NULL
? 0 : params
->size())
2729 + (receiver
!= NULL
? 1 : 0));
2731 if (receiver
!= NULL
)
2733 Type
* rtype
= receiver
->type();
2734 // The receiver is always passed as a pointer. FIXME: Is this
2735 // right? Should that fact affect the type descriptor?
2736 if (rtype
->points_to() == NULL
)
2737 rtype
= Type::make_pointer_type(rtype
);
2738 vals
->push_back(Expression::make_type_descriptor(rtype
, bloc
));
2743 for (Typed_identifier_list::const_iterator p
= params
->begin();
2746 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
2749 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
2752 // The reflection string.
2755 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
2757 // FIXME: Turn this off until we straighten out the type of the
2758 // struct field used in a go statement which calls a method.
2759 // gcc_assert(this->receiver_ == NULL);
2761 ret
->append("func");
2763 if (this->receiver_
!= NULL
)
2765 ret
->push_back('(');
2766 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
2767 ret
->push_back(')');
2770 ret
->push_back('(');
2771 const Typed_identifier_list
* params
= this->parameters();
2774 bool is_varargs
= this->is_varargs_
;
2775 for (Typed_identifier_list::const_iterator p
= params
->begin();
2779 if (p
!= params
->begin())
2781 if (!is_varargs
|| p
+ 1 != params
->end())
2782 this->append_reflection(p
->type(), gogo
, ret
);
2786 this->append_reflection(p
->type()->array_type()->element_type(),
2791 ret
->push_back(')');
2793 const Typed_identifier_list
* results
= this->results();
2794 if (results
!= NULL
&& !results
->empty())
2796 if (results
->size() == 1)
2797 ret
->push_back(' ');
2800 for (Typed_identifier_list::const_iterator p
= results
->begin();
2801 p
!= results
->end();
2804 if (p
!= results
->begin())
2806 this->append_reflection(p
->type(), gogo
, ret
);
2808 if (results
->size() > 1)
2809 ret
->push_back(')');
2816 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
2818 ret
->push_back('F');
2820 if (this->receiver_
!= NULL
)
2822 ret
->push_back('m');
2823 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
2826 const Typed_identifier_list
* params
= this->parameters();
2829 ret
->push_back('p');
2830 for (Typed_identifier_list::const_iterator p
= params
->begin();
2833 this->append_mangled_name(p
->type(), gogo
, ret
);
2834 if (this->is_varargs_
)
2835 ret
->push_back('V');
2836 ret
->push_back('e');
2839 const Typed_identifier_list
* results
= this->results();
2840 if (results
!= NULL
)
2842 ret
->push_back('r');
2843 for (Typed_identifier_list::const_iterator p
= results
->begin();
2844 p
!= results
->end();
2846 this->append_mangled_name(p
->type(), gogo
, ret
);
2847 ret
->push_back('e');
2850 ret
->push_back('e');
2853 // Export a function type.
2856 Function_type::do_export(Export
* exp
) const
2858 // We don't write out the receiver. The only function types which
2859 // should have a receiver are the ones associated with explicitly
2860 // defined methods. For those the receiver type is written out by
2861 // Function::export_func.
2863 exp
->write_c_string("(");
2865 if (this->parameters_
!= NULL
)
2867 bool is_varargs
= this->is_varargs_
;
2868 for (Typed_identifier_list::const_iterator p
=
2869 this->parameters_
->begin();
2870 p
!= this->parameters_
->end();
2876 exp
->write_c_string(", ");
2877 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
2878 exp
->write_type(p
->type());
2881 exp
->write_c_string("...");
2882 exp
->write_type(p
->type()->array_type()->element_type());
2886 exp
->write_c_string(")");
2888 const Typed_identifier_list
* results
= this->results_
;
2889 if (results
!= NULL
)
2891 exp
->write_c_string(" ");
2892 if (results
->size() == 1)
2893 exp
->write_type(results
->begin()->type());
2897 exp
->write_c_string("(");
2898 for (Typed_identifier_list::const_iterator p
= results
->begin();
2899 p
!= results
->end();
2905 exp
->write_c_string(", ");
2906 exp
->write_type(p
->type());
2908 exp
->write_c_string(")");
2913 // Import a function type.
2916 Function_type::do_import(Import
* imp
)
2918 imp
->require_c_string("(");
2919 Typed_identifier_list
* parameters
;
2920 bool is_varargs
= false;
2921 if (imp
->peek_char() == ')')
2925 parameters
= new Typed_identifier_list();
2928 if (imp
->match_c_string("..."))
2934 Type
* ptype
= imp
->read_type();
2936 ptype
= Type::make_array_type(ptype
, NULL
);
2937 parameters
->push_back(Typed_identifier(Import::import_marker
,
2938 ptype
, imp
->location()));
2939 if (imp
->peek_char() != ',')
2941 gcc_assert(!is_varargs
);
2942 imp
->require_c_string(", ");
2945 imp
->require_c_string(")");
2947 Typed_identifier_list
* results
;
2948 if (imp
->peek_char() != ' ')
2953 results
= new Typed_identifier_list
;
2954 if (imp
->peek_char() != '(')
2956 Type
* rtype
= imp
->read_type();
2957 results
->push_back(Typed_identifier(Import::import_marker
, rtype
,
2965 Type
* rtype
= imp
->read_type();
2966 results
->push_back(Typed_identifier(Import::import_marker
,
2967 rtype
, imp
->location()));
2968 if (imp
->peek_char() != ',')
2970 imp
->require_c_string(", ");
2972 imp
->require_c_string(")");
2976 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
2979 ret
->set_is_varargs();
2983 // Make a copy of a function type without a receiver.
2986 Function_type::copy_without_receiver() const
2988 gcc_assert(this->is_method());
2989 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
2992 if (this->is_varargs())
2993 ret
->set_is_varargs();
2994 if (this->is_builtin())
2995 ret
->set_is_builtin();
2999 // Make a copy of a function type with a receiver.
3002 Function_type::copy_with_receiver(Type
* receiver_type
) const
3004 gcc_assert(!this->is_method());
3005 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
3007 return Type::make_function_type(receiver
, this->parameters_
,
3008 this->results_
, this->location_
);
3011 // Make a function type.
3014 Type::make_function_type(Typed_identifier
* receiver
,
3015 Typed_identifier_list
* parameters
,
3016 Typed_identifier_list
* results
,
3017 source_location location
)
3019 return new Function_type(receiver
, parameters
, results
, location
);
3022 // Class Pointer_type.
3027 Pointer_type::do_traverse(Traverse
* traverse
)
3029 return Type::traverse(this->to_type_
, traverse
);
3035 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
3037 return this->to_type_
->hash_for_method(gogo
) << 4;
3040 // The tree for a pointer type.
3043 Pointer_type::do_get_tree(Gogo
* gogo
)
3045 return build_pointer_type(this->to_type_
->get_tree(gogo
));
3048 // Initialize a pointer type.
3051 Pointer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3055 return fold_convert(type_tree
, null_pointer_node
);
3058 // The type of a pointer type descriptor.
3061 Pointer_type::make_pointer_type_descriptor_type()
3066 Type
* tdt
= Type::make_type_descriptor_type();
3067 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3069 Struct_type
* s
= Type::make_builtin_struct_type(2,
3073 ret
= Type::make_builtin_named_type("PtrType", s
);
3079 // The type descriptor for a pointer type.
3082 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3084 if (this->is_unsafe_pointer_type())
3086 gcc_assert(name
!= NULL
);
3087 return this->plain_type_descriptor(gogo
,
3088 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
3093 source_location bloc
= BUILTINS_LOCATION
;
3095 const Methods
* methods
;
3096 Type
* deref
= this->points_to();
3097 if (deref
->named_type() != NULL
)
3098 methods
= deref
->named_type()->methods();
3099 else if (deref
->struct_type() != NULL
)
3100 methods
= deref
->struct_type()->methods();
3104 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
3106 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
3108 Expression_list
* vals
= new Expression_list();
3111 Struct_field_list::const_iterator p
= fields
->begin();
3112 gcc_assert(p
->field_name() == "commonType");
3113 vals
->push_back(this->type_descriptor_constructor(gogo
,
3114 RUNTIME_TYPE_KIND_PTR
,
3115 name
, methods
, false));
3118 gcc_assert(p
->field_name() == "elem");
3119 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
3121 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
3125 // Reflection string.
3128 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3130 ret
->push_back('*');
3131 this->append_reflection(this->to_type_
, gogo
, ret
);
3137 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3139 ret
->push_back('p');
3140 this->append_mangled_name(this->to_type_
, gogo
, ret
);
3146 Pointer_type::do_export(Export
* exp
) const
3148 exp
->write_c_string("*");
3149 if (this->is_unsafe_pointer_type())
3150 exp
->write_c_string("any");
3152 exp
->write_type(this->to_type_
);
3158 Pointer_type::do_import(Import
* imp
)
3160 imp
->require_c_string("*");
3161 if (imp
->match_c_string("any"))
3164 return Type::make_pointer_type(Type::make_void_type());
3166 Type
* to
= imp
->read_type();
3167 return Type::make_pointer_type(to
);
3170 // Make a pointer type.
3173 Type::make_pointer_type(Type
* to_type
)
3175 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
3176 static Hashtable pointer_types
;
3177 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
3178 if (p
!= pointer_types
.end())
3180 Pointer_type
* ret
= new Pointer_type(to_type
);
3181 pointer_types
[to_type
] = ret
;
3185 // The nil type. We use a special type for nil because it is not the
3186 // same as any other type. In C term nil has type void*, but there is
3187 // no such type in Go.
3189 class Nil_type
: public Type
3199 { return ptr_type_node
; }
3202 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3203 { return is_clear
? NULL
: fold_convert(type_tree
, null_pointer_node
); }
3206 do_type_descriptor(Gogo
*, Named_type
*)
3207 { gcc_unreachable(); }
3210 do_reflection(Gogo
*, std::string
*) const
3211 { gcc_unreachable(); }
3214 do_mangled_name(Gogo
*, std::string
* ret
) const
3215 { ret
->push_back('n'); }
3218 // Make the nil type.
3221 Type::make_nil_type()
3223 static Nil_type singleton_nil_type
;
3224 return &singleton_nil_type
;
3227 // The type of a function call which returns multiple values. This is
3228 // really a struct, but we don't want to confuse a function call which
3229 // returns a struct with a function call which returns multiple
3232 class Call_multiple_result_type
: public Type
3235 Call_multiple_result_type(Call_expression
* call
)
3236 : Type(TYPE_CALL_MULTIPLE_RESULT
),
3242 do_has_pointer() const
3243 { gcc_unreachable(); }
3249 do_get_init_tree(Gogo
*, tree
, bool)
3250 { gcc_unreachable(); }
3253 do_type_descriptor(Gogo
*, Named_type
*)
3254 { gcc_unreachable(); }
3257 do_reflection(Gogo
*, std::string
*) const
3258 { gcc_unreachable(); }
3261 do_mangled_name(Gogo
*, std::string
*) const
3262 { gcc_unreachable(); }
3265 // The expression being called.
3266 Call_expression
* call_
;
3269 // Return the tree for a call result.
3272 Call_multiple_result_type::do_get_tree(Gogo
* gogo
)
3274 Function_type
* fntype
= this->call_
->get_function_type();
3275 gcc_assert(fntype
!= NULL
);
3276 const Typed_identifier_list
* results
= fntype
->results();
3277 gcc_assert(results
!= NULL
&& results
->size() > 1);
3279 Struct_field_list
* sfl
= new Struct_field_list
;
3280 for (Typed_identifier_list::const_iterator p
= results
->begin();
3281 p
!= results
->end();
3284 const std::string name
= ((p
->name().empty()
3285 || p
->name() == Import::import_marker
)
3288 sfl
->push_back(Struct_field(Typed_identifier(name
, p
->type(),
3289 this->call_
->location())));
3291 return Type::make_struct_type(sfl
, this->call_
->location())->get_tree(gogo
);
3294 // Make a call result type.
3297 Type::make_call_multiple_result_type(Call_expression
* call
)
3299 return new Call_multiple_result_type(call
);
3302 // Class Struct_field.
3304 // Get the name of a field.
3307 Struct_field::field_name() const
3309 const std::string
& name(this->typed_identifier_
.name());
3314 // This is called during parsing, before anything is lowered, so
3315 // we have to be pretty careful to avoid dereferencing an
3316 // unknown type name.
3317 Type
* t
= this->typed_identifier_
.type();
3319 if (t
->classification() == Type::TYPE_POINTER
)
3322 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
3323 dt
= ptype
->points_to();
3325 if (dt
->forward_declaration_type() != NULL
)
3326 return dt
->forward_declaration_type()->name();
3327 else if (dt
->named_type() != NULL
)
3328 return dt
->named_type()->name();
3329 else if (t
->is_error_type() || dt
->is_error_type())
3331 static const std::string error_string
= "*error*";
3332 return error_string
;
3336 // Avoid crashing in the erroneous case where T is named but
3338 gcc_assert(t
!= dt
);
3339 if (t
->forward_declaration_type() != NULL
)
3340 return t
->forward_declaration_type()->name();
3341 else if (t
->named_type() != NULL
)
3342 return t
->named_type()->name();
3349 // Class Struct_type.
3354 Struct_type::do_traverse(Traverse
* traverse
)
3356 Struct_field_list
* fields
= this->fields_
;
3359 for (Struct_field_list::iterator p
= fields
->begin();
3363 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
3364 return TRAVERSE_EXIT
;
3367 return TRAVERSE_CONTINUE
;
3370 // Verify that the struct type is complete and valid.
3373 Struct_type::do_verify()
3375 Struct_field_list
* fields
= this->fields_
;
3379 for (Struct_field_list::iterator p
= fields
->begin();
3383 Type
* t
= p
->type();
3384 if (t
->is_undefined())
3386 error_at(p
->location(), "struct field type is incomplete");
3387 p
->set_type(Type::make_error_type());
3390 else if (p
->is_anonymous())
3392 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
3394 error_at(p
->location(), "embedded type may not be a pointer");
3395 p
->set_type(Type::make_error_type());
3403 // Whether this contains a pointer.
3406 Struct_type::do_has_pointer() const
3408 const Struct_field_list
* fields
= this->fields();
3411 for (Struct_field_list::const_iterator p
= fields
->begin();
3415 if (p
->type()->has_pointer())
3421 // Whether this type is identical to T.
3424 Struct_type::is_identical(const Struct_type
* t
) const
3426 const Struct_field_list
* fields1
= this->fields();
3427 const Struct_field_list
* fields2
= t
->fields();
3428 if (fields1
== NULL
|| fields2
== NULL
)
3429 return fields1
== fields2
;
3430 Struct_field_list::const_iterator pf2
= fields2
->begin();
3431 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
3432 pf1
!= fields1
->end();
3435 if (pf2
== fields2
->end())
3437 if (pf1
->field_name() != pf2
->field_name())
3439 if (pf1
->is_anonymous() != pf2
->is_anonymous()
3440 || !Type::are_identical(pf1
->type(), pf2
->type(), NULL
))
3442 if (!pf1
->has_tag())
3449 if (!pf2
->has_tag())
3451 if (pf1
->tag() != pf2
->tag())
3455 if (pf2
!= fields2
->end())
3460 // Whether this struct type has any hidden fields.
3463 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
3464 std::string
* reason
) const
3466 const Struct_field_list
* fields
= this->fields();
3469 const Package
* within_package
= (within
== NULL
3471 : within
->named_object()->package());
3472 for (Struct_field_list::const_iterator pf
= fields
->begin();
3473 pf
!= fields
->end();
3476 if (within_package
!= NULL
3477 && !pf
->is_anonymous()
3478 && Gogo::is_hidden_name(pf
->field_name()))
3482 std::string within_name
= within
->named_object()->message_name();
3483 std::string name
= Gogo::message_name(pf
->field_name());
3484 size_t bufsize
= 200 + within_name
.length() + name
.length();
3485 char* buf
= new char[bufsize
];
3486 snprintf(buf
, bufsize
,
3487 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3488 open_quote
, within_name
.c_str(), close_quote
,
3489 open_quote
, name
.c_str(), close_quote
);
3490 reason
->assign(buf
);
3496 if (pf
->type()->has_hidden_fields(within
, reason
))
3506 Struct_type::do_hash_for_method(Gogo
* gogo
) const
3508 unsigned int ret
= 0;
3509 if (this->fields() != NULL
)
3511 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
3512 pf
!= this->fields()->end();
3514 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
3519 // Find the local field NAME.
3522 Struct_type::find_local_field(const std::string
& name
,
3523 unsigned int *pindex
) const
3525 const Struct_field_list
* fields
= this->fields_
;
3529 for (Struct_field_list::const_iterator pf
= fields
->begin();
3530 pf
!= fields
->end();
3533 if (pf
->field_name() == name
)
3543 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3545 Field_reference_expression
*
3546 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
3547 source_location location
) const
3550 return this->field_reference_depth(struct_expr
, name
, location
, &depth
);
3553 // Return an expression for a field, along with the depth at which it
3556 Field_reference_expression
*
3557 Struct_type::field_reference_depth(Expression
* struct_expr
,
3558 const std::string
& name
,
3559 source_location location
,
3560 unsigned int* depth
) const
3562 const Struct_field_list
* fields
= this->fields_
;
3566 // Look for a field with this name.
3568 for (Struct_field_list::const_iterator pf
= fields
->begin();
3569 pf
!= fields
->end();
3572 if (pf
->field_name() == name
)
3575 return Expression::make_field_reference(struct_expr
, i
, location
);
3579 // Look for an anonymous field which contains a field with this
3581 unsigned int found_depth
= 0;
3582 Field_reference_expression
* ret
= NULL
;
3584 for (Struct_field_list::const_iterator pf
= fields
->begin();
3585 pf
!= fields
->end();
3588 if (!pf
->is_anonymous())
3591 Struct_type
* st
= pf
->type()->deref()->struct_type();
3595 // Look for a reference using a NULL struct expression. If we
3596 // find one, fill in the struct expression with a reference to
3598 unsigned int subdepth
;
3599 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
3605 if (ret
== NULL
|| subdepth
< found_depth
)
3610 found_depth
= subdepth
;
3611 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
3613 if (pf
->type()->points_to() != NULL
)
3614 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
3615 while (sub
->expr() != NULL
)
3617 sub
= sub
->expr()->deref()->field_reference_expression();
3618 gcc_assert(sub
!= NULL
);
3620 sub
->set_struct_expression(here
);
3622 else if (subdepth
> found_depth
)
3626 // We do not handle ambiguity here--it should be handled by
3627 // Type::bind_field_or_method.
3635 *depth
= found_depth
+ 1;
3640 // Return the total number of fields, including embedded fields.
3643 Struct_type::total_field_count() const
3645 if (this->fields_
== NULL
)
3647 unsigned int ret
= 0;
3648 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3649 pf
!= this->fields_
->end();
3652 if (!pf
->is_anonymous() || pf
->type()->deref()->struct_type() == NULL
)
3655 ret
+= pf
->type()->struct_type()->total_field_count();
3660 // Return whether NAME is an unexported field, for better error reporting.
3663 Struct_type::is_unexported_local_field(Gogo
* gogo
,
3664 const std::string
& name
) const
3666 const Struct_field_list
* fields
= this->fields_
;
3669 for (Struct_field_list::const_iterator pf
= fields
->begin();
3670 pf
!= fields
->end();
3673 const std::string
& field_name(pf
->field_name());
3674 if (Gogo::is_hidden_name(field_name
)
3675 && name
== Gogo::unpack_hidden_name(field_name
)
3676 && gogo
->pack_hidden_name(name
, false) != field_name
)
3683 // Finalize the methods of an unnamed struct.
3686 Struct_type::finalize_methods(Gogo
* gogo
)
3688 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
3691 // Return the method NAME, or NULL if there isn't one or if it is
3692 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3696 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
3698 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
3701 // Get the tree for a struct type.
3704 Struct_type::do_get_tree(Gogo
* gogo
)
3706 tree type
= make_node(RECORD_TYPE
);
3707 return this->fill_in_tree(gogo
, type
);
3710 // Fill in the fields for a struct type.
3713 Struct_type::fill_in_tree(Gogo
* gogo
, tree type
)
3715 tree field_trees
= NULL_TREE
;
3716 tree
* pp
= &field_trees
;
3717 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3718 p
!= this->fields_
->end();
3721 std::string name
= Gogo::unpack_hidden_name(p
->field_name());
3722 tree name_tree
= get_identifier_with_length(name
.data(), name
.length());
3723 tree field_type_tree
= p
->type()->get_tree(gogo
);
3724 if (field_type_tree
== error_mark_node
)
3725 return error_mark_node
;
3726 tree field
= build_decl(p
->location(), FIELD_DECL
, name_tree
,
3728 DECL_CONTEXT(field
) = type
;
3730 pp
= &DECL_CHAIN(field
);
3733 TYPE_FIELDS(type
) = field_trees
;
3740 // Initialize struct fields.
3743 Struct_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
3745 if (this->fields_
== NULL
|| this->fields_
->empty())
3751 tree ret
= build_constructor(type_tree
,
3752 VEC_alloc(constructor_elt
, gc
, 0));
3753 TREE_CONSTANT(ret
) = 1;
3758 bool is_constant
= true;
3759 bool any_fields_set
= false;
3760 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
,
3761 this->fields_
->size());
3763 tree field
= TYPE_FIELDS(type_tree
);
3764 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3765 p
!= this->fields_
->end();
3766 ++p
, field
= DECL_CHAIN(field
))
3768 tree value
= p
->type()->get_init_tree(gogo
, is_clear
);
3769 if (value
== error_mark_node
)
3770 return error_mark_node
;
3771 gcc_assert(field
!= NULL_TREE
);
3774 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
3777 any_fields_set
= true;
3778 if (!TREE_CONSTANT(value
))
3779 is_constant
= false;
3782 gcc_assert(field
== NULL_TREE
);
3784 if (!any_fields_set
)
3786 gcc_assert(is_clear
);
3787 VEC_free(constructor_elt
, gc
, init
);
3791 tree ret
= build_constructor(type_tree
, init
);
3793 TREE_CONSTANT(ret
) = 1;
3797 // The type of a struct type descriptor.
3800 Struct_type::make_struct_type_descriptor_type()
3805 Type
* tdt
= Type::make_type_descriptor_type();
3806 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3808 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3809 Type
* string_type
= Type::lookup_string_type();
3810 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
3813 Type::make_builtin_struct_type(5,
3814 "name", pointer_string_type
,
3815 "pkgPath", pointer_string_type
,
3817 "tag", pointer_string_type
,
3818 "offset", uintptr_type
);
3819 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
3821 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
3823 Struct_type
* s
= Type::make_builtin_struct_type(2,
3825 "fields", slice_type
);
3827 ret
= Type::make_builtin_named_type("StructType", s
);
3833 // Build a type descriptor for a struct type.
3836 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3838 source_location bloc
= BUILTINS_LOCATION
;
3840 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
3842 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
3844 Expression_list
* vals
= new Expression_list();
3847 const Methods
* methods
= this->methods();
3848 // A named struct should not have methods--the methods should attach
3849 // to the named type.
3850 gcc_assert(methods
== NULL
|| name
== NULL
);
3852 Struct_field_list::const_iterator ps
= fields
->begin();
3853 gcc_assert(ps
->field_name() == "commonType");
3854 vals
->push_back(this->type_descriptor_constructor(gogo
,
3855 RUNTIME_TYPE_KIND_STRUCT
,
3856 name
, methods
, true));
3859 gcc_assert(ps
->field_name() == "fields");
3861 Expression_list
* elements
= new Expression_list();
3862 elements
->reserve(this->fields_
->size());
3863 Type
* element_type
= ps
->type()->array_type()->element_type();
3864 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3865 pf
!= this->fields_
->end();
3868 const Struct_field_list
* f
= element_type
->struct_type()->fields();
3870 Expression_list
* fvals
= new Expression_list();
3873 Struct_field_list::const_iterator q
= f
->begin();
3874 gcc_assert(q
->field_name() == "name");
3875 if (pf
->is_anonymous())
3876 fvals
->push_back(Expression::make_nil(bloc
));
3879 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
3880 Expression
* s
= Expression::make_string(n
, bloc
);
3881 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3885 gcc_assert(q
->field_name() == "pkgPath");
3886 if (!Gogo::is_hidden_name(pf
->field_name()))
3887 fvals
->push_back(Expression::make_nil(bloc
));
3890 std::string n
= Gogo::hidden_name_prefix(pf
->field_name());
3891 Expression
* s
= Expression::make_string(n
, bloc
);
3892 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3896 gcc_assert(q
->field_name() == "typ");
3897 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
3900 gcc_assert(q
->field_name() == "tag");
3902 fvals
->push_back(Expression::make_nil(bloc
));
3905 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
3906 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3910 gcc_assert(q
->field_name() == "offset");
3911 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
3913 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
3915 elements
->push_back(v
);
3918 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
3921 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
3924 // Reflection string.
3927 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3929 ret
->append("struct { ");
3931 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3932 p
!= this->fields_
->end();
3935 if (p
!= this->fields_
->begin())
3937 if (p
->is_anonymous())
3938 ret
->push_back('?');
3940 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
3941 ret
->push_back(' ');
3942 this->append_reflection(p
->type(), gogo
, ret
);
3946 const std::string
& tag(p
->tag());
3948 for (std::string::const_iterator p
= tag
.begin();
3953 ret
->append("\\x00");
3954 else if (*p
== '\n')
3956 else if (*p
== '\t')
3959 ret
->append("\\\"");
3960 else if (*p
== '\\')
3961 ret
->append("\\\\");
3965 ret
->push_back('"');
3975 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3977 ret
->push_back('S');
3979 const Struct_field_list
* fields
= this->fields_
;
3982 for (Struct_field_list::const_iterator p
= fields
->begin();
3986 if (p
->is_anonymous())
3990 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
3992 snprintf(buf
, sizeof buf
, "%u_",
3993 static_cast<unsigned int>(n
.length()));
3997 this->append_mangled_name(p
->type(), gogo
, ret
);
4000 const std::string
& tag(p
->tag());
4002 for (std::string::const_iterator p
= tag
.begin();
4006 if (ISALNUM(*p
) || *p
== '_')
4011 snprintf(buf
, sizeof buf
, ".%x.",
4012 static_cast<unsigned int>(*p
));
4017 snprintf(buf
, sizeof buf
, "T%u_",
4018 static_cast<unsigned int>(out
.length()));
4025 ret
->push_back('e');
4031 Struct_type::do_export(Export
* exp
) const
4033 exp
->write_c_string("struct { ");
4034 const Struct_field_list
* fields
= this->fields_
;
4035 gcc_assert(fields
!= NULL
);
4036 for (Struct_field_list::const_iterator p
= fields
->begin();
4040 if (p
->is_anonymous())
4041 exp
->write_string("? ");
4044 exp
->write_string(p
->field_name());
4045 exp
->write_c_string(" ");
4047 exp
->write_type(p
->type());
4051 exp
->write_c_string(" ");
4052 Expression
* expr
= Expression::make_string(p
->tag(),
4054 expr
->export_expression(exp
);
4058 exp
->write_c_string("; ");
4060 exp
->write_c_string("}");
4066 Struct_type::do_import(Import
* imp
)
4068 imp
->require_c_string("struct { ");
4069 Struct_field_list
* fields
= new Struct_field_list
;
4070 if (imp
->peek_char() != '}')
4075 if (imp
->match_c_string("? "))
4079 name
= imp
->read_identifier();
4080 imp
->require_c_string(" ");
4082 Type
* ftype
= imp
->read_type();
4084 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
4086 if (imp
->peek_char() == ' ')
4089 Expression
* expr
= Expression::import_expression(imp
);
4090 String_expression
* sexpr
= expr
->string_expression();
4091 gcc_assert(sexpr
!= NULL
);
4092 sf
.set_tag(sexpr
->val());
4096 imp
->require_c_string("; ");
4097 fields
->push_back(sf
);
4098 if (imp
->peek_char() == '}')
4102 imp
->require_c_string("}");
4104 return Type::make_struct_type(fields
, imp
->location());
4107 // Make a struct type.
4110 Type::make_struct_type(Struct_field_list
* fields
,
4111 source_location location
)
4113 return new Struct_type(fields
, location
);
4116 // Class Array_type.
4118 // Whether two array types are identical.
4121 Array_type::is_identical(const Array_type
* t
) const
4123 if (!Type::are_identical(this->element_type(), t
->element_type(), NULL
))
4126 Expression
* l1
= this->length();
4127 Expression
* l2
= t
->length();
4129 // Slices of the same element type are identical.
4130 if (l1
== NULL
&& l2
== NULL
)
4133 // Arrays of the same element type are identical if they have the
4135 if (l1
!= NULL
&& l2
!= NULL
)
4140 // Try to determine the lengths. If we can't, assume the arrays
4141 // are not identical.
4149 if (l1
->integer_constant_value(true, v1
, &type1
)
4150 && l2
->integer_constant_value(true, v2
, &type2
))
4151 ret
= mpz_cmp(v1
, v2
) == 0;
4157 // Otherwise the arrays are not identical.
4164 Array_type::do_traverse(Traverse
* traverse
)
4166 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
4167 return TRAVERSE_EXIT
;
4168 if (this->length_
!= NULL
4169 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
4170 return TRAVERSE_EXIT
;
4171 return TRAVERSE_CONTINUE
;
4174 // Check that the length is valid.
4177 Array_type::verify_length()
4179 if (this->length_
== NULL
)
4181 if (!this->length_
->is_constant())
4183 error_at(this->length_
->location(), "array bound is not constant");
4189 Type
* t
= this->length_
->type();
4190 if (t
->integer_type() != NULL
)
4194 if (!this->length_
->integer_constant_value(true, val
, &vt
))
4196 error_at(this->length_
->location(),
4197 "array bound is not constant");
4202 else if (t
->float_type() != NULL
)
4207 if (!this->length_
->float_constant_value(fval
, &vt
))
4209 error_at(this->length_
->location(),
4210 "array bound is not constant");
4214 if (!mpfr_integer_p(fval
))
4216 error_at(this->length_
->location(),
4217 "array bound truncated to integer");
4222 mpfr_get_z(val
, fval
, GMP_RNDN
);
4227 if (!t
->is_error_type())
4228 error_at(this->length_
->location(), "array bound is not numeric");
4232 if (mpz_sgn(val
) < 0)
4234 error_at(this->length_
->location(), "negative array bound");
4239 Type
* int_type
= Type::lookup_integer_type("int");
4240 int tbits
= int_type
->integer_type()->bits();
4241 int vbits
= mpz_sizeinbase(val
, 2);
4242 if (vbits
+ 1 > tbits
)
4244 error_at(this->length_
->location(), "array bound overflows");
4257 Array_type::do_verify()
4259 if (!this->verify_length())
4261 this->length_
= Expression::make_error(this->length_
->location());
4267 // Array type hash code.
4270 Array_type::do_hash_for_method(Gogo
* gogo
) const
4272 // There is no very convenient way to get a hash code for the
4274 return this->element_type_
->hash_for_method(gogo
) + 1;
4277 // See if the expression passed to make is suitable. The first
4278 // argument is required, and gives the length. An optional second
4279 // argument is permitted for the capacity.
4282 Array_type::do_check_make_expression(Expression_list
* args
,
4283 source_location location
)
4285 gcc_assert(this->length_
== NULL
);
4286 if (args
== NULL
|| args
->empty())
4288 error_at(location
, "length required when allocating a slice");
4291 else if (args
->size() > 2)
4293 error_at(location
, "too many expressions passed to make");
4298 if (!Type::check_int_value(args
->front(),
4299 _("bad length when making slice"), location
))
4302 if (args
->size() > 1)
4304 if (!Type::check_int_value(args
->back(),
4305 _("bad capacity when making slice"),
4314 // Get a tree for the length of a fixed array. The length may be
4315 // computed using a function call, so we must only evaluate it once.
4318 Array_type::get_length_tree(Gogo
* gogo
)
4320 gcc_assert(this->length_
!= NULL
);
4321 if (this->length_tree_
== NULL_TREE
)
4326 if (this->length_
->integer_constant_value(true, val
, &t
))
4329 t
= Type::lookup_integer_type("int");
4330 else if (t
->is_abstract())
4331 t
= t
->make_non_abstract_type();
4332 tree tt
= t
->get_tree(gogo
);
4333 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
4340 // Make up a translation context for the array length
4341 // expression. FIXME: This won't work in general.
4342 Translate_context
context(gogo
, NULL
, NULL
, NULL_TREE
);
4343 tree len
= this->length_
->get_tree(&context
);
4344 len
= convert_to_integer(integer_type_node
, len
);
4345 this->length_tree_
= save_expr(len
);
4348 return this->length_tree_
;
4351 // Get a tree for the type of this array. A fixed array is simply
4352 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4353 // just like an array in C. An open array is a struct with three
4354 // fields: a data pointer, the length, and the capacity.
4357 Array_type::do_get_tree(Gogo
* gogo
)
4359 if (this->length_
== NULL
)
4361 tree struct_type
= gogo
->slice_type_tree(void_type_node
);
4362 return this->fill_in_tree(gogo
, struct_type
);
4366 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4367 tree length_tree
= this->get_length_tree(gogo
);
4368 if (element_type_tree
== error_mark_node
4369 || length_tree
== error_mark_node
)
4370 return error_mark_node
;
4372 length_tree
= fold_convert(sizetype
, length_tree
);
4374 // build_index_type takes the maximum index, which is one less
4376 tree index_type
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
4380 return build_array_type(element_type_tree
, index_type
);
4384 // Fill in the fields for a slice type. This is used for named slice
4388 Array_type::fill_in_tree(Gogo
* gogo
, tree struct_type
)
4390 gcc_assert(this->length_
== NULL
);
4392 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4393 tree field
= TYPE_FIELDS(struct_type
);
4394 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
4395 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field
))
4396 && TREE_TYPE(TREE_TYPE(field
)) == void_type_node
);
4397 TREE_TYPE(field
) = build_pointer_type(element_type_tree
);
4402 // Return an initializer for an array type.
4405 Array_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
4407 if (this->length_
== NULL
)
4414 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
4416 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
4418 for (tree field
= TYPE_FIELDS(type_tree
);
4420 field
= DECL_CHAIN(field
))
4422 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
,
4425 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
4428 tree ret
= build_constructor(type_tree
, init
);
4429 TREE_CONSTANT(ret
) = 1;
4436 tree value
= this->element_type_
->get_init_tree(gogo
, is_clear
);
4439 if (value
== error_mark_node
)
4440 return error_mark_node
;
4442 tree length_tree
= this->get_length_tree(gogo
);
4443 if (length_tree
== error_mark_node
)
4444 return error_mark_node
;
4446 length_tree
= fold_convert(sizetype
, length_tree
);
4447 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
,
4448 fold_build2(MINUS_EXPR
, sizetype
,
4449 length_tree
, size_one_node
));
4450 tree ret
= build_constructor_single(type_tree
, range
, value
);
4451 if (TREE_CONSTANT(value
))
4452 TREE_CONSTANT(ret
) = 1;
4457 // Handle the builtin make function for a slice.
4460 Array_type::do_make_expression_tree(Translate_context
* context
,
4461 Expression_list
* args
,
4462 source_location location
)
4464 gcc_assert(this->length_
== NULL
);
4466 Gogo
* gogo
= context
->gogo();
4467 tree type_tree
= this->get_tree(gogo
);
4468 if (type_tree
== error_mark_node
)
4469 return error_mark_node
;
4471 tree values_field
= TYPE_FIELDS(type_tree
);
4472 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field
)),
4475 tree count_field
= DECL_CHAIN(values_field
);
4476 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field
)),
4479 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4480 if (element_type_tree
== error_mark_node
)
4481 return error_mark_node
;
4482 tree element_size_tree
= TYPE_SIZE_UNIT(element_type_tree
);
4484 tree value
= this->element_type_
->get_init_tree(gogo
, true);
4486 // The first argument is the number of elements, the optional second
4487 // argument is the capacity.
4488 gcc_assert(args
!= NULL
&& args
->size() >= 1 && args
->size() <= 2);
4490 tree length_tree
= args
->front()->get_tree(context
);
4491 if (length_tree
== error_mark_node
)
4492 return error_mark_node
;
4493 if (!DECL_P(length_tree
))
4494 length_tree
= save_expr(length_tree
);
4495 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree
)))
4496 length_tree
= convert_to_integer(TREE_TYPE(count_field
), length_tree
);
4498 tree bad_index
= Expression::check_bounds(length_tree
,
4499 TREE_TYPE(count_field
),
4500 NULL_TREE
, location
);
4502 length_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
), length_tree
);
4504 if (args
->size() == 1)
4505 capacity_tree
= length_tree
;
4508 capacity_tree
= args
->back()->get_tree(context
);
4509 if (capacity_tree
== error_mark_node
)
4510 return error_mark_node
;
4511 if (!DECL_P(capacity_tree
))
4512 capacity_tree
= save_expr(capacity_tree
);
4513 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree
)))
4514 capacity_tree
= convert_to_integer(TREE_TYPE(count_field
),
4517 bad_index
= Expression::check_bounds(capacity_tree
,
4518 TREE_TYPE(count_field
),
4519 bad_index
, location
);
4521 tree chktype
= (((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4522 > TYPE_SIZE(TREE_TYPE(length_tree
)))
4523 || ((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4524 == TYPE_SIZE(TREE_TYPE(length_tree
)))
4525 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree
))))
4526 ? TREE_TYPE(capacity_tree
)
4527 : TREE_TYPE(length_tree
));
4528 tree chk
= fold_build2_loc(location
, LT_EXPR
, boolean_type_node
,
4529 fold_convert_loc(location
, chktype
,
4531 fold_convert_loc(location
, chktype
,
4533 if (bad_index
== NULL_TREE
)
4536 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4539 capacity_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
),
4543 tree size_tree
= fold_build2_loc(location
, MULT_EXPR
, sizetype
,
4545 fold_convert_loc(location
, sizetype
,
4548 tree chk
= fold_build2_loc(location
, TRUTH_AND_EXPR
, boolean_type_node
,
4549 fold_build2_loc(location
, GT_EXPR
,
4551 fold_convert_loc(location
,
4555 fold_build2_loc(location
, LT_EXPR
,
4557 size_tree
, element_size_tree
));
4558 if (bad_index
== NULL_TREE
)
4561 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4564 tree space
= context
->gogo()->allocate_memory(this->element_type_
,
4565 size_tree
, location
);
4567 if (value
!= NULL_TREE
)
4568 space
= save_expr(space
);
4570 space
= fold_convert(TREE_TYPE(values_field
), space
);
4572 if (bad_index
!= NULL_TREE
&& bad_index
!= boolean_false_node
)
4574 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
,
4576 space
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4577 build3(COND_EXPR
, void_type_node
,
4578 bad_index
, crash
, NULL_TREE
),
4582 tree constructor
= gogo
->slice_constructor(type_tree
, space
, length_tree
,
4585 if (value
== NULL_TREE
)
4587 // The array contents are zero initialized.
4591 // The elements must be initialized.
4593 tree max
= fold_build2_loc(location
, MINUS_EXPR
, TREE_TYPE(count_field
),
4595 fold_convert_loc(location
, TREE_TYPE(count_field
),
4598 tree array_type
= build_array_type(element_type_tree
,
4599 build_index_type(max
));
4601 tree value_pointer
= fold_convert_loc(location
,
4602 build_pointer_type(array_type
),
4605 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
, max
);
4606 tree space_init
= build_constructor_single(array_type
, range
, value
);
4608 return build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4609 build2(MODIFY_EXPR
, void_type_node
,
4610 build_fold_indirect_ref(value_pointer
),
4615 // Return a tree for a pointer to the values in ARRAY.
4618 Array_type::value_pointer_tree(Gogo
*, tree array
) const
4621 if (this->length() != NULL
)
4624 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
4625 build_fold_addr_expr(array
));
4630 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
4631 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
4633 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
4636 if (TREE_CONSTANT(array
))
4637 TREE_CONSTANT(ret
) = 1;
4641 // Return a tree for the length of the array ARRAY which has this
4645 Array_type::length_tree(Gogo
* gogo
, tree array
)
4647 if (this->length_
!= NULL
)
4649 if (TREE_CODE(array
) == SAVE_EXPR
)
4650 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
4652 return omit_one_operand(integer_type_node
,
4653 this->get_length_tree(gogo
), array
);
4656 // This is an open array. We need to read the length field.
4658 tree type
= TREE_TYPE(array
);
4659 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4661 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
4662 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
4664 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4665 if (TREE_CONSTANT(array
))
4666 TREE_CONSTANT(ret
) = 1;
4670 // Return a tree for the capacity of the array ARRAY which has this
4674 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
4676 if (this->length_
!= NULL
)
4677 return omit_one_operand(sizetype
, this->get_length_tree(gogo
), array
);
4679 // This is an open array. We need to read the capacity field.
4681 tree type
= TREE_TYPE(array
);
4682 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4684 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
4685 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
4687 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4693 Array_type::do_export(Export
* exp
) const
4695 exp
->write_c_string("[");
4696 if (this->length_
!= NULL
)
4697 this->length_
->export_expression(exp
);
4698 exp
->write_c_string("] ");
4699 exp
->write_type(this->element_type_
);
4705 Array_type::do_import(Import
* imp
)
4707 imp
->require_c_string("[");
4709 if (imp
->peek_char() == ']')
4712 length
= Expression::import_expression(imp
);
4713 imp
->require_c_string("] ");
4714 Type
* element_type
= imp
->read_type();
4715 return Type::make_array_type(element_type
, length
);
4718 // The type of an array type descriptor.
4721 Array_type::make_array_type_descriptor_type()
4726 Type
* tdt
= Type::make_type_descriptor_type();
4727 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4729 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4732 Type::make_builtin_struct_type(3,
4735 "len", uintptr_type
);
4737 ret
= Type::make_builtin_named_type("ArrayType", sf
);
4743 // The type of an slice type descriptor.
4746 Array_type::make_slice_type_descriptor_type()
4751 Type
* tdt
= Type::make_type_descriptor_type();
4752 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4755 Type::make_builtin_struct_type(2,
4759 ret
= Type::make_builtin_named_type("SliceType", sf
);
4765 // Build a type descriptor for an array/slice type.
4768 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4770 if (this->length_
!= NULL
)
4771 return this->array_type_descriptor(gogo
, name
);
4773 return this->slice_type_descriptor(gogo
, name
);
4776 // Build a type descriptor for an array type.
4779 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4781 source_location bloc
= BUILTINS_LOCATION
;
4783 Type
* atdt
= Array_type::make_array_type_descriptor_type();
4785 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
4787 Expression_list
* vals
= new Expression_list();
4790 Struct_field_list::const_iterator p
= fields
->begin();
4791 gcc_assert(p
->field_name() == "commonType");
4792 vals
->push_back(this->type_descriptor_constructor(gogo
,
4793 RUNTIME_TYPE_KIND_ARRAY
,
4797 gcc_assert(p
->field_name() == "elem");
4798 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4801 gcc_assert(p
->field_name() == "len");
4802 vals
->push_back(this->length_
);
4805 gcc_assert(p
== fields
->end());
4807 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
4810 // Build a type descriptor for a slice type.
4813 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4815 source_location bloc
= BUILTINS_LOCATION
;
4817 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
4819 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4821 Expression_list
* vals
= new Expression_list();
4824 Struct_field_list::const_iterator p
= fields
->begin();
4825 gcc_assert(p
->field_name() == "commonType");
4826 vals
->push_back(this->type_descriptor_constructor(gogo
,
4827 RUNTIME_TYPE_KIND_SLICE
,
4831 gcc_assert(p
->field_name() == "elem");
4832 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4835 gcc_assert(p
== fields
->end());
4837 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4840 // Reflection string.
4843 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4845 ret
->push_back('[');
4846 if (this->length_
!= NULL
)
4851 if (!this->length_
->integer_constant_value(true, val
, &type
))
4852 error_at(this->length_
->location(),
4853 "array length must be integer constant expression");
4854 else if (mpz_cmp_si(val
, 0) < 0)
4855 error_at(this->length_
->location(), "array length is negative");
4856 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4857 error_at(this->length_
->location(), "array length is too large");
4861 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4866 ret
->push_back(']');
4868 this->append_reflection(this->element_type_
, gogo
, ret
);
4874 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4876 ret
->push_back('A');
4877 this->append_mangled_name(this->element_type_
, gogo
, ret
);
4878 if (this->length_
!= NULL
)
4883 if (!this->length_
->integer_constant_value(true, val
, &type
))
4884 error_at(this->length_
->location(),
4885 "array length must be integer constant expression");
4886 else if (mpz_cmp_si(val
, 0) < 0)
4887 error_at(this->length_
->location(), "array length is negative");
4888 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4889 error_at(this->length_
->location(), "array size is too large");
4893 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4898 ret
->push_back('e');
4901 // Make an array type.
4904 Type::make_array_type(Type
* element_type
, Expression
* length
)
4906 return new Array_type(element_type
, length
);
4914 Map_type::do_traverse(Traverse
* traverse
)
4916 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
4917 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
4918 return TRAVERSE_EXIT
;
4919 return TRAVERSE_CONTINUE
;
4922 // Check that the map type is OK.
4925 Map_type::do_verify()
4927 if (this->key_type_
->struct_type() != NULL
4928 || this->key_type_
->array_type() != NULL
)
4930 error_at(this->location_
, "invalid map key type");
4936 // Whether two map types are identical.
4939 Map_type::is_identical(const Map_type
* t
) const
4941 return (Type::are_identical(this->key_type(), t
->key_type(), NULL
)
4942 && Type::are_identical(this->val_type(), t
->val_type(), NULL
));
4948 Map_type::do_hash_for_method(Gogo
* gogo
) const
4950 return (this->key_type_
->hash_for_method(gogo
)
4951 + this->val_type_
->hash_for_method(gogo
)
4955 // Check that a call to the builtin make function is valid. For a map
4956 // the optional argument is the number of spaces to preallocate for
4960 Map_type::do_check_make_expression(Expression_list
* args
,
4961 source_location location
)
4963 if (args
!= NULL
&& !args
->empty())
4965 if (!Type::check_int_value(args
->front(), _("bad size when making map"),
4968 else if (args
->size() > 1)
4970 error_at(location
, "too many arguments when making map");
4977 // Get a tree for a map type. A map type is represented as a pointer
4978 // to a struct. The struct is __go_map in libgo/map.h.
4981 Map_type::do_get_tree(Gogo
* gogo
)
4983 static tree type_tree
;
4984 if (type_tree
== NULL_TREE
)
4986 tree struct_type
= make_node(RECORD_TYPE
);
4988 tree map_descriptor_type
= gogo
->map_descriptor_type();
4989 tree const_map_descriptor_type
=
4990 build_qualified_type(map_descriptor_type
, TYPE_QUAL_CONST
);
4991 tree name
= get_identifier("__descriptor");
4992 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
4993 build_pointer_type(const_map_descriptor_type
));
4994 DECL_CONTEXT(field
) = struct_type
;
4995 TYPE_FIELDS(struct_type
) = field
;
4996 tree last_field
= field
;
4998 name
= get_identifier("__element_count");
4999 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5000 DECL_CONTEXT(field
) = struct_type
;
5001 DECL_CHAIN(last_field
) = field
;
5004 name
= get_identifier("__bucket_count");
5005 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5006 DECL_CONTEXT(field
) = struct_type
;
5007 DECL_CHAIN(last_field
) = field
;
5010 name
= get_identifier("__buckets");
5011 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5012 build_pointer_type(ptr_type_node
));
5013 DECL_CONTEXT(field
) = struct_type
;
5014 DECL_CHAIN(last_field
) = field
;
5016 layout_type(struct_type
);
5018 // Give the struct a name for better debugging info.
5019 name
= get_identifier("__go_map");
5020 tree type_decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
, name
,
5022 DECL_ARTIFICIAL(type_decl
) = 1;
5023 TYPE_NAME(struct_type
) = type_decl
;
5024 go_preserve_from_gc(type_decl
);
5025 rest_of_decl_compilation(type_decl
, 1, 0);
5027 type_tree
= build_pointer_type(struct_type
);
5028 go_preserve_from_gc(type_tree
);
5034 // Initialize a map.
5037 Map_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5041 return fold_convert(type_tree
, null_pointer_node
);
5044 // Return an expression for a newly allocated map.
5047 Map_type::do_make_expression_tree(Translate_context
* context
,
5048 Expression_list
* args
,
5049 source_location location
)
5051 tree bad_index
= NULL_TREE
;
5054 if (args
== NULL
|| args
->empty())
5055 expr_tree
= size_zero_node
;
5058 expr_tree
= args
->front()->get_tree(context
);
5059 if (expr_tree
== error_mark_node
)
5060 return error_mark_node
;
5061 if (!DECL_P(expr_tree
))
5062 expr_tree
= save_expr(expr_tree
);
5063 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5064 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5065 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5069 tree map_type
= this->get_tree(context
->gogo());
5071 static tree new_map_fndecl
;
5072 tree ret
= Gogo::call_builtin(&new_map_fndecl
,
5077 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type
))),
5078 context
->gogo()->map_descriptor(this),
5081 // This can panic if the capacity is out of range.
5082 TREE_NOTHROW(new_map_fndecl
) = 0;
5084 if (bad_index
== NULL_TREE
)
5088 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
,
5090 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5091 build3(COND_EXPR
, void_type_node
,
5092 bad_index
, crash
, NULL_TREE
),
5097 // The type of a map type descriptor.
5100 Map_type::make_map_type_descriptor_type()
5105 Type
* tdt
= Type::make_type_descriptor_type();
5106 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5109 Type::make_builtin_struct_type(3,
5114 ret
= Type::make_builtin_named_type("MapType", sf
);
5120 // Build a type descriptor for a map type.
5123 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5125 source_location bloc
= BUILTINS_LOCATION
;
5127 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
5129 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
5131 Expression_list
* vals
= new Expression_list();
5134 Struct_field_list::const_iterator p
= fields
->begin();
5135 gcc_assert(p
->field_name() == "commonType");
5136 vals
->push_back(this->type_descriptor_constructor(gogo
,
5137 RUNTIME_TYPE_KIND_MAP
,
5141 gcc_assert(p
->field_name() == "key");
5142 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
5145 gcc_assert(p
->field_name() == "elem");
5146 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
5149 gcc_assert(p
== fields
->end());
5151 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
5154 // Reflection string for a map.
5157 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5159 ret
->append("map[");
5160 this->append_reflection(this->key_type_
, gogo
, ret
);
5162 this->append_reflection(this->val_type_
, gogo
, ret
);
5165 // Mangled name for a map.
5168 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5170 ret
->push_back('M');
5171 this->append_mangled_name(this->key_type_
, gogo
, ret
);
5173 this->append_mangled_name(this->val_type_
, gogo
, ret
);
5176 // Export a map type.
5179 Map_type::do_export(Export
* exp
) const
5181 exp
->write_c_string("map [");
5182 exp
->write_type(this->key_type_
);
5183 exp
->write_c_string("] ");
5184 exp
->write_type(this->val_type_
);
5187 // Import a map type.
5190 Map_type::do_import(Import
* imp
)
5192 imp
->require_c_string("map [");
5193 Type
* key_type
= imp
->read_type();
5194 imp
->require_c_string("] ");
5195 Type
* val_type
= imp
->read_type();
5196 return Type::make_map_type(key_type
, val_type
, imp
->location());
5202 Type::make_map_type(Type
* key_type
, Type
* val_type
, source_location location
)
5204 return new Map_type(key_type
, val_type
, location
);
5207 // Class Channel_type.
5212 Channel_type::do_hash_for_method(Gogo
* gogo
) const
5214 unsigned int ret
= 0;
5215 if (this->may_send_
)
5217 if (this->may_receive_
)
5219 if (this->element_type_
!= NULL
)
5220 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
5224 // Whether this type is the same as T.
5227 Channel_type::is_identical(const Channel_type
* t
) const
5229 if (!Type::are_identical(this->element_type(), t
->element_type(), NULL
))
5231 return (this->may_send_
== t
->may_send_
5232 && this->may_receive_
== t
->may_receive_
);
5235 // Check whether the parameters for a call to the builtin function
5236 // make are OK for a channel. A channel can take an optional single
5237 // parameter which is the buffer size.
5240 Channel_type::do_check_make_expression(Expression_list
* args
,
5241 source_location location
)
5243 if (args
!= NULL
&& !args
->empty())
5245 if (!Type::check_int_value(args
->front(),
5246 _("bad buffer size when making channel"),
5249 else if (args
->size() > 1)
5251 error_at(location
, "too many arguments when making channel");
5258 // Return the tree for a channel type. A channel is a pointer to a
5259 // __go_channel struct. The __go_channel struct is defined in
5260 // libgo/runtime/channel.h.
5263 Channel_type::do_get_tree(Gogo
*)
5265 static tree type_tree
;
5266 if (type_tree
== NULL_TREE
)
5268 tree ret
= make_node(RECORD_TYPE
);
5269 TYPE_NAME(ret
) = get_identifier("__go_channel");
5270 TYPE_STUB_DECL(ret
) = build_decl(BUILTINS_LOCATION
, TYPE_DECL
, NULL_TREE
,
5272 type_tree
= build_pointer_type(ret
);
5273 go_preserve_from_gc(type_tree
);
5278 // Initialize a channel variable.
5281 Channel_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5285 return fold_convert(type_tree
, null_pointer_node
);
5288 // Handle the builtin function make for a channel.
5291 Channel_type::do_make_expression_tree(Translate_context
* context
,
5292 Expression_list
* args
,
5293 source_location location
)
5295 Gogo
* gogo
= context
->gogo();
5296 tree channel_type
= this->get_tree(gogo
);
5298 tree element_tree
= this->element_type_
->get_tree(gogo
);
5299 tree element_size_tree
= size_in_bytes(element_tree
);
5301 tree bad_index
= NULL_TREE
;
5304 if (args
== NULL
|| args
->empty())
5305 expr_tree
= size_zero_node
;
5308 expr_tree
= args
->front()->get_tree(context
);
5309 if (expr_tree
== error_mark_node
)
5310 return error_mark_node
;
5311 if (!DECL_P(expr_tree
))
5312 expr_tree
= save_expr(expr_tree
);
5313 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5314 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5315 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5319 static tree new_channel_fndecl
;
5320 tree ret
= Gogo::call_builtin(&new_channel_fndecl
,
5329 // This can panic if the capacity is out of range.
5330 TREE_NOTHROW(new_channel_fndecl
) = 0;
5332 if (bad_index
== NULL_TREE
)
5336 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
,
5338 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5339 build3(COND_EXPR
, void_type_node
,
5340 bad_index
, crash
, NULL_TREE
),
5345 // Build a type descriptor for a channel type.
5348 Channel_type::make_chan_type_descriptor_type()
5353 Type
* tdt
= Type::make_type_descriptor_type();
5354 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5356 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5359 Type::make_builtin_struct_type(3,
5362 "dir", uintptr_type
);
5364 ret
= Type::make_builtin_named_type("ChanType", sf
);
5370 // Build a type descriptor for a map type.
5373 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5375 source_location bloc
= BUILTINS_LOCATION
;
5377 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
5379 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
5381 Expression_list
* vals
= new Expression_list();
5384 Struct_field_list::const_iterator p
= fields
->begin();
5385 gcc_assert(p
->field_name() == "commonType");
5386 vals
->push_back(this->type_descriptor_constructor(gogo
,
5387 RUNTIME_TYPE_KIND_CHAN
,
5391 gcc_assert(p
->field_name() == "elem");
5392 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5395 gcc_assert(p
->field_name() == "dir");
5396 // These bits must match the ones in libgo/runtime/go-type.h.
5398 if (this->may_receive_
)
5400 if (this->may_send_
)
5403 mpz_init_set_ui(iv
, val
);
5404 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
5408 gcc_assert(p
== fields
->end());
5410 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
5413 // Reflection string.
5416 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5418 if (!this->may_send_
)
5420 ret
->append("chan");
5421 if (!this->may_receive_
)
5423 ret
->push_back(' ');
5424 this->append_reflection(this->element_type_
, gogo
, ret
);
5430 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5432 ret
->push_back('C');
5433 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5434 if (this->may_send_
)
5435 ret
->push_back('s');
5436 if (this->may_receive_
)
5437 ret
->push_back('r');
5438 ret
->push_back('e');
5444 Channel_type::do_export(Export
* exp
) const
5446 exp
->write_c_string("chan ");
5447 if (this->may_send_
&& !this->may_receive_
)
5448 exp
->write_c_string("-< ");
5449 else if (this->may_receive_
&& !this->may_send_
)
5450 exp
->write_c_string("<- ");
5451 exp
->write_type(this->element_type_
);
5457 Channel_type::do_import(Import
* imp
)
5459 imp
->require_c_string("chan ");
5463 if (imp
->match_c_string("-< "))
5467 may_receive
= false;
5469 else if (imp
->match_c_string("<- "))
5481 Type
* element_type
= imp
->read_type();
5483 return Type::make_channel_type(may_send
, may_receive
, element_type
);
5486 // Make a new channel type.
5489 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
5491 return new Channel_type(send
, receive
, element_type
);
5494 // Class Interface_type.
5499 Interface_type::do_traverse(Traverse
* traverse
)
5501 if (this->methods_
== NULL
)
5502 return TRAVERSE_CONTINUE
;
5503 return this->methods_
->traverse(traverse
);
5506 // Finalize the methods. This handles interface inheritance.
5509 Interface_type::finalize_methods()
5511 if (this->methods_
== NULL
)
5513 bool is_recursive
= false;
5516 while (from
< this->methods_
->size())
5518 const Typed_identifier
* p
= &this->methods_
->at(from
);
5519 if (!p
->name().empty())
5522 for (i
= 0; i
< to
; ++i
)
5524 if (this->methods_
->at(i
).name() == p
->name())
5526 error_at(p
->location(), "duplicate method %qs",
5527 Gogo::message_name(p
->name()).c_str());
5534 this->methods_
->set(to
, *p
);
5540 Interface_type
* it
= p
->type()->interface_type();
5543 error_at(p
->location(), "interface contains embedded non-interface");
5551 error_at(p
->location(), "invalid recursive interface");
5552 is_recursive
= true;
5557 const Typed_identifier_list
* methods
= it
->methods();
5558 if (methods
== NULL
)
5563 for (Typed_identifier_list::const_iterator q
= methods
->begin();
5564 q
!= methods
->end();
5567 if (q
->name().empty() || this->find_method(q
->name()) == NULL
)
5568 this->methods_
->push_back(Typed_identifier(q
->name(), q
->type(),
5573 error_at(p
->location(), "inherited method %qs is ambiguous",
5574 Gogo::message_name(q
->name()).c_str());
5581 delete this->methods_
;
5582 this->methods_
= NULL
;
5586 this->methods_
->resize(to
);
5587 this->methods_
->sort_by_name();
5591 // Return the method NAME, or NULL.
5593 const Typed_identifier
*
5594 Interface_type::find_method(const std::string
& name
) const
5596 if (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 the method index.
5609 Interface_type::method_index(const std::string
& name
) const
5611 gcc_assert(this->methods_
!= NULL
);
5613 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5614 p
!= this->methods_
->end();
5616 if (p
->name() == name
)
5621 // Return whether NAME is an unexported method, for better error
5625 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
5627 if (this->methods_
== NULL
)
5629 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5630 p
!= this->methods_
->end();
5633 const std::string
& method_name(p
->name());
5634 if (Gogo::is_hidden_name(method_name
)
5635 && name
== Gogo::unpack_hidden_name(method_name
)
5636 && gogo
->pack_hidden_name(name
, false) != method_name
)
5642 // Whether this type is identical with T.
5645 Interface_type::is_identical(const Interface_type
* t
) const
5647 // We require the same methods with the same types. The methods
5648 // have already been sorted.
5649 if (this->methods() == NULL
|| t
->methods() == NULL
)
5650 return this->methods() == t
->methods();
5652 Typed_identifier_list::const_iterator p1
= this->methods()->begin();
5653 for (Typed_identifier_list::const_iterator p2
= t
->methods()->begin();
5654 p2
!= t
->methods()->end();
5657 if (p1
== this->methods()->end())
5659 if (p1
->name() != p2
->name()
5660 || !Type::are_identical(p1
->type(), p2
->type(), NULL
))
5663 if (p1
!= this->methods()->end())
5668 // Whether we can assign the interface type T to this type. The types
5669 // are known to not be identical. An interface assignment is only
5670 // permitted if T is known to implement all methods in THIS.
5671 // Otherwise a type guard is required.
5674 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
5675 std::string
* reason
) const
5677 if (this->methods() == NULL
)
5679 for (Typed_identifier_list::const_iterator p
= this->methods()->begin();
5680 p
!= this->methods()->end();
5683 const Typed_identifier
* m
= t
->find_method(p
->name());
5689 snprintf(buf
, sizeof buf
,
5690 _("need explicit conversion; missing method %s%s%s"),
5691 open_quote
, Gogo::message_name(p
->name()).c_str(),
5693 reason
->assign(buf
);
5698 std::string subreason
;
5699 if (!Type::are_identical(p
->type(), m
->type(), &subreason
))
5703 std::string n
= Gogo::message_name(p
->name());
5704 size_t len
= 100 + n
.length() + subreason
.length();
5705 char* buf
= new char[len
];
5706 if (subreason
.empty())
5707 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5708 open_quote
, n
.c_str(), close_quote
);
5711 _("incompatible type for method %s%s%s (%s)"),
5712 open_quote
, n
.c_str(), close_quote
,
5714 reason
->assign(buf
);
5727 Interface_type::do_hash_for_method(Gogo
* gogo
) const
5729 unsigned int ret
= 0;
5730 if (this->methods_
!= NULL
)
5732 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5733 p
!= this->methods_
->end();
5736 ret
= Type::hash_string(p
->name(), ret
);
5737 ret
+= p
->type()->hash_for_method(gogo
);
5744 // Return true if T implements the interface. If it does not, and
5745 // REASON is not NULL, set *REASON to a useful error message.
5748 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
5750 if (this->methods_
== NULL
)
5753 bool is_pointer
= false;
5754 const Named_type
* nt
= t
->named_type();
5755 const Struct_type
* st
= t
->struct_type();
5756 // If we start with a named type, we don't dereference it to find
5760 const Type
* pt
= t
->points_to();
5763 // If T is a pointer to a named type, then we need to look at
5764 // the type to which it points.
5766 nt
= pt
->named_type();
5767 st
= pt
->struct_type();
5771 // If we have a named type, get the methods from it rather than from
5776 // Only named and struct types have methods.
5777 if (nt
== NULL
&& st
== NULL
)
5781 if (t
->points_to() != NULL
5782 && t
->points_to()->interface_type() != NULL
)
5783 reason
->assign(_("pointer to interface type has no methods"));
5785 reason
->assign(_("type has no methods"));
5790 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
5794 if (t
->points_to() != NULL
5795 && t
->points_to()->interface_type() != NULL
)
5796 reason
->assign(_("pointer to interface type has no methods"));
5798 reason
->assign(_("type has no methods"));
5803 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5804 p
!= this->methods_
->end();
5807 bool is_ambiguous
= false;
5808 Method
* m
= (nt
!= NULL
5809 ? nt
->method_function(p
->name(), &is_ambiguous
)
5810 : st
->method_function(p
->name(), &is_ambiguous
));
5815 std::string n
= Gogo::message_name(p
->name());
5816 size_t len
= n
.length() + 100;
5817 char* buf
= new char[len
];
5819 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
5820 open_quote
, n
.c_str(), close_quote
);
5822 snprintf(buf
, len
, _("missing method %s%s%s"),
5823 open_quote
, n
.c_str(), close_quote
);
5824 reason
->assign(buf
);
5830 Function_type
*p_fn_type
= p
->type()->function_type();
5831 Function_type
* m_fn_type
= m
->type()->function_type();
5832 gcc_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
5833 std::string subreason
;
5834 if (!p_fn_type
->is_identical(m_fn_type
, true, &subreason
))
5838 std::string n
= Gogo::message_name(p
->name());
5839 size_t len
= 100 + n
.length() + subreason
.length();
5840 char* buf
= new char[len
];
5841 if (subreason
.empty())
5842 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5843 open_quote
, n
.c_str(), close_quote
);
5846 _("incompatible type for method %s%s%s (%s)"),
5847 open_quote
, n
.c_str(), close_quote
,
5849 reason
->assign(buf
);
5855 if (!is_pointer
&& !m
->is_value_method())
5859 std::string n
= Gogo::message_name(p
->name());
5860 size_t len
= 100 + n
.length();
5861 char* buf
= new char[len
];
5862 snprintf(buf
, len
, _("method %s%s%s requires a pointer"),
5863 open_quote
, n
.c_str(), close_quote
);
5864 reason
->assign(buf
);
5874 // Return a tree for an interface type. An interface is a pointer to
5875 // a struct. The struct has three fields. The first field is a
5876 // pointer to the type descriptor for the dynamic type of the object.
5877 // The second field is a pointer to a table of methods for the
5878 // interface to be used with the object. The third field is the value
5879 // of the object itself.
5882 Interface_type::do_get_tree(Gogo
* gogo
)
5884 if (this->methods_
== NULL
)
5886 // At the tree level, use the same type for all empty
5887 // interfaces. This lets us assign them to each other directly
5888 // without triggering GIMPLE type errors.
5889 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5890 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5891 static tree empty_interface
;
5892 return Gogo::builtin_struct(&empty_interface
, "__go_empty_interface",
5894 "__type_descriptor",
5900 return this->fill_in_tree(gogo
, make_node(RECORD_TYPE
));
5903 // Fill in the tree for an interface type. This is used for named
5907 Interface_type::fill_in_tree(Gogo
* gogo
, tree type
)
5909 gcc_assert(this->methods_
!= NULL
);
5911 // Build the type of the table of methods.
5913 tree method_table
= make_node(RECORD_TYPE
);
5915 // The first field is a pointer to the type descriptor.
5916 tree name_tree
= get_identifier("__type_descriptor");
5917 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5918 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5919 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, dtype
);
5920 DECL_CONTEXT(field
) = method_table
;
5921 TYPE_FIELDS(method_table
) = field
;
5923 std::string last_name
= "";
5924 tree
* pp
= &DECL_CHAIN(field
);
5925 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5926 p
!= this->methods_
->end();
5929 std::string name
= Gogo::unpack_hidden_name(p
->name());
5930 name_tree
= get_identifier_with_length(name
.data(), name
.length());
5931 tree field_type
= p
->type()->get_tree(gogo
);
5932 if (field_type
== error_mark_node
)
5933 return error_mark_node
;
5934 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, field_type
);
5935 DECL_CONTEXT(field
) = method_table
;
5937 pp
= &DECL_CHAIN(field
);
5938 // Sanity check: the names should be sorted.
5939 gcc_assert(p
->name() > last_name
);
5940 last_name
= p
->name();
5942 layout_type(method_table
);
5944 tree mtype
= build_pointer_type(method_table
);
5946 tree field_trees
= NULL_TREE
;
5949 name_tree
= get_identifier("__methods");
5950 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, mtype
);
5951 DECL_CONTEXT(field
) = type
;
5953 pp
= &DECL_CHAIN(field
);
5955 name_tree
= get_identifier("__object");
5956 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, ptr_type_node
);
5957 DECL_CONTEXT(field
) = type
;
5960 TYPE_FIELDS(type
) = field_trees
;
5967 // Initialization value.
5970 Interface_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5975 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
5976 for (tree field
= TYPE_FIELDS(type_tree
);
5978 field
= DECL_CHAIN(field
))
5980 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
5982 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
5985 tree ret
= build_constructor(type_tree
, init
);
5986 TREE_CONSTANT(ret
) = 1;
5990 // The type of an interface type descriptor.
5993 Interface_type::make_interface_type_descriptor_type()
5998 Type
* tdt
= Type::make_type_descriptor_type();
5999 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6001 Type
* string_type
= Type::lookup_string_type();
6002 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6005 Type::make_builtin_struct_type(3,
6006 "name", pointer_string_type
,
6007 "pkgPath", pointer_string_type
,
6010 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
6012 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
6014 Struct_type
* s
= Type::make_builtin_struct_type(2,
6016 "methods", slice_nsm
);
6018 ret
= Type::make_builtin_named_type("InterfaceType", s
);
6024 // Build a type descriptor for an interface type.
6027 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6029 source_location bloc
= BUILTINS_LOCATION
;
6031 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
6033 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
6035 Expression_list
* ivals
= new Expression_list();
6038 Struct_field_list::const_iterator pif
= ifields
->begin();
6039 gcc_assert(pif
->field_name() == "commonType");
6040 ivals
->push_back(this->type_descriptor_constructor(gogo
,
6041 RUNTIME_TYPE_KIND_INTERFACE
,
6045 gcc_assert(pif
->field_name() == "methods");
6047 Expression_list
* methods
= new Expression_list();
6048 if (this->methods_
!= NULL
&& !this->methods_
->empty())
6050 Type
* elemtype
= pif
->type()->array_type()->element_type();
6052 methods
->reserve(this->methods_
->size());
6053 for (Typed_identifier_list::const_iterator pm
= this->methods_
->begin();
6054 pm
!= this->methods_
->end();
6057 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
6059 Expression_list
* mvals
= new Expression_list();
6062 Struct_field_list::const_iterator pmf
= mfields
->begin();
6063 gcc_assert(pmf
->field_name() == "name");
6064 std::string s
= Gogo::unpack_hidden_name(pm
->name());
6065 Expression
* e
= Expression::make_string(s
, bloc
);
6066 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6069 gcc_assert(pmf
->field_name() == "pkgPath");
6070 if (!Gogo::is_hidden_name(pm
->name()))
6071 mvals
->push_back(Expression::make_nil(bloc
));
6074 s
= Gogo::hidden_name_prefix(pm
->name());
6075 e
= Expression::make_string(s
, bloc
);
6076 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6080 gcc_assert(pmf
->field_name() == "typ");
6081 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
6084 gcc_assert(pmf
== mfields
->end());
6086 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
6088 methods
->push_back(e
);
6092 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
6096 gcc_assert(pif
== ifields
->end());
6098 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
6101 // Reflection string.
6104 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6106 ret
->append("interface {");
6107 if (this->methods_
!= NULL
)
6109 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6110 p
!= this->methods_
->end();
6113 if (p
!= this->methods_
->begin())
6115 ret
->push_back(' ');
6116 ret
->append(Gogo::unpack_hidden_name(p
->name()));
6117 std::string sub
= p
->type()->reflection(gogo
);
6118 gcc_assert(sub
.compare(0, 4, "func") == 0);
6119 sub
= sub
.substr(4);
6129 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6131 ret
->push_back('I');
6133 const Typed_identifier_list
* methods
= this->methods_
;
6134 if (methods
!= NULL
)
6136 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6137 p
!= methods
->end();
6140 std::string n
= Gogo::unpack_hidden_name(p
->name());
6142 snprintf(buf
, sizeof buf
, "%u_",
6143 static_cast<unsigned int>(n
.length()));
6146 this->append_mangled_name(p
->type(), gogo
, ret
);
6150 ret
->push_back('e');
6156 Interface_type::do_export(Export
* exp
) const
6158 exp
->write_c_string("interface { ");
6160 const Typed_identifier_list
* methods
= this->methods_
;
6161 if (methods
!= NULL
)
6163 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
6164 pm
!= methods
->end();
6167 exp
->write_string(pm
->name());
6168 exp
->write_c_string(" (");
6170 const Function_type
* fntype
= pm
->type()->function_type();
6173 const Typed_identifier_list
* parameters
= fntype
->parameters();
6174 if (parameters
!= NULL
)
6176 bool is_varargs
= fntype
->is_varargs();
6177 for (Typed_identifier_list::const_iterator pp
=
6178 parameters
->begin();
6179 pp
!= parameters
->end();
6185 exp
->write_c_string(", ");
6186 if (!is_varargs
|| pp
+ 1 != parameters
->end())
6187 exp
->write_type(pp
->type());
6190 exp
->write_c_string("...");
6191 Type
*pptype
= pp
->type();
6192 exp
->write_type(pptype
->array_type()->element_type());
6197 exp
->write_c_string(")");
6199 const Typed_identifier_list
* results
= fntype
->results();
6200 if (results
!= NULL
)
6202 exp
->write_c_string(" ");
6203 if (results
->size() == 1)
6204 exp
->write_type(results
->begin()->type());
6208 exp
->write_c_string("(");
6209 for (Typed_identifier_list::const_iterator p
=
6211 p
!= results
->end();
6217 exp
->write_c_string(", ");
6218 exp
->write_type(p
->type());
6220 exp
->write_c_string(")");
6224 exp
->write_c_string("; ");
6228 exp
->write_c_string("}");
6231 // Import an interface type.
6234 Interface_type::do_import(Import
* imp
)
6236 imp
->require_c_string("interface { ");
6238 Typed_identifier_list
* methods
= new Typed_identifier_list
;
6239 while (imp
->peek_char() != '}')
6241 std::string name
= imp
->read_identifier();
6242 imp
->require_c_string(" (");
6244 Typed_identifier_list
* parameters
;
6245 bool is_varargs
= false;
6246 if (imp
->peek_char() == ')')
6250 parameters
= new Typed_identifier_list
;
6253 if (imp
->match_c_string("..."))
6259 Type
* ptype
= imp
->read_type();
6261 ptype
= Type::make_array_type(ptype
, NULL
);
6262 parameters
->push_back(Typed_identifier(Import::import_marker
,
6263 ptype
, imp
->location()));
6264 if (imp
->peek_char() != ',')
6266 gcc_assert(!is_varargs
);
6267 imp
->require_c_string(", ");
6270 imp
->require_c_string(")");
6272 Typed_identifier_list
* results
;
6273 if (imp
->peek_char() != ' ')
6277 results
= new Typed_identifier_list
;
6279 if (imp
->peek_char() != '(')
6281 Type
* rtype
= imp
->read_type();
6282 results
->push_back(Typed_identifier(Import::import_marker
,
6283 rtype
, imp
->location()));
6290 Type
* rtype
= imp
->read_type();
6291 results
->push_back(Typed_identifier(Import::import_marker
,
6292 rtype
, imp
->location()));
6293 if (imp
->peek_char() != ',')
6295 imp
->require_c_string(", ");
6297 imp
->require_c_string(")");
6301 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
6305 fntype
->set_is_varargs();
6306 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
6308 imp
->require_c_string("; ");
6311 imp
->require_c_string("}");
6313 if (methods
->empty())
6319 return Type::make_interface_type(methods
, imp
->location());
6322 // Make an interface type.
6325 Type::make_interface_type(Typed_identifier_list
* methods
,
6326 source_location location
)
6328 return new Interface_type(methods
, location
);
6333 // Bind a method to an object.
6336 Method::bind_method(Expression
* expr
, source_location location
) const
6338 if (this->stub_
== NULL
)
6340 // When there is no stub object, the binding is determined by
6342 return this->do_bind_method(expr
, location
);
6345 Expression
* func
= Expression::make_func_reference(this->stub_
, NULL
,
6347 return Expression::make_bound_method(expr
, func
, location
);
6350 // Return the named object associated with a method. This may only be
6351 // called after methods are finalized.
6354 Method::named_object() const
6356 if (this->stub_
!= NULL
)
6358 return this->do_named_object();
6361 // Class Named_method.
6363 // The type of the method.
6366 Named_method::do_type() const
6368 if (this->named_object_
->is_function())
6369 return this->named_object_
->func_value()->type();
6370 else if (this->named_object_
->is_function_declaration())
6371 return this->named_object_
->func_declaration_value()->type();
6376 // Return the location of the method receiver.
6379 Named_method::do_receiver_location() const
6381 return this->do_type()->receiver()->location();
6384 // Bind a method to an object.
6387 Named_method::do_bind_method(Expression
* expr
, source_location location
) const
6389 Expression
* func
= Expression::make_func_reference(this->named_object_
, NULL
,
6391 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, func
,
6393 // If this is not a local method, and it does not use a stub, then
6394 // the real method expects a different type. We need to cast the
6396 if (this->depth() > 0 && !this->needs_stub_method())
6398 Function_type
* ftype
= this->do_type();
6399 gcc_assert(ftype
->is_method());
6400 Type
* frtype
= ftype
->receiver()->type();
6401 bme
->set_first_argument_type(frtype
);
6406 // Class Interface_method.
6408 // Bind a method to an object.
6411 Interface_method::do_bind_method(Expression
* expr
,
6412 source_location location
) const
6414 return Expression::make_interface_field_reference(expr
, this->name_
,
6420 // Insert a new method. Return true if it was inserted, false
6424 Methods::insert(const std::string
& name
, Method
* m
)
6426 std::pair
<Method_map::iterator
, bool> ins
=
6427 this->methods_
.insert(std::make_pair(name
, m
));
6432 Method
* old_method
= ins
.first
->second
;
6433 if (m
->depth() < old_method
->depth())
6436 ins
.first
->second
= m
;
6441 if (m
->depth() == old_method
->depth())
6442 old_method
->set_is_ambiguous();
6448 // Return the number of unambiguous methods.
6451 Methods::count() const
6454 for (Method_map::const_iterator p
= this->methods_
.begin();
6455 p
!= this->methods_
.end();
6457 if (!p
->second
->is_ambiguous())
6462 // Class Named_type.
6464 // Return the name of the type.
6467 Named_type::name() const
6469 return this->named_object_
->name();
6472 // Return the name of the type to use in an error message.
6475 Named_type::message_name() const
6477 return this->named_object_
->message_name();
6480 // Return the base type for this type. We have to be careful about
6481 // circular type definitions, which are invalid but may be seen here.
6484 Named_type::named_base()
6489 Type
* ret
= this->type_
->base();
6490 this->seen_
= false;
6495 Named_type::named_base() const
6500 const Type
* ret
= this->type_
->base();
6501 this->seen_
= false;
6505 // Return whether this is an error type. We have to be careful about
6506 // circular type definitions, which are invalid but may be seen here.
6509 Named_type::is_named_error_type() const
6514 bool ret
= this->type_
->is_error_type();
6515 this->seen_
= false;
6519 // Add a method to this type.
6522 Named_type::add_method(const std::string
& name
, Function
* function
)
6524 if (this->local_methods_
== NULL
)
6525 this->local_methods_
= new Bindings(NULL
);
6526 return this->local_methods_
->add_function(name
, NULL
, function
);
6529 // Add a method declaration to this type.
6532 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
6533 Function_type
* type
,
6534 source_location location
)
6536 if (this->local_methods_
== NULL
)
6537 this->local_methods_
= new Bindings(NULL
);
6538 return this->local_methods_
->add_function_declaration(name
, package
, type
,
6542 // Add an existing method to this type.
6545 Named_type::add_existing_method(Named_object
* no
)
6547 if (this->local_methods_
== NULL
)
6548 this->local_methods_
= new Bindings(NULL
);
6549 this->local_methods_
->add_named_object(no
);
6552 // Look for a local method NAME, and returns its named object, or NULL
6556 Named_type::find_local_method(const std::string
& name
) const
6558 if (this->local_methods_
== NULL
)
6560 return this->local_methods_
->lookup(name
);
6563 // Return whether NAME is an unexported field or method, for better
6567 Named_type::is_unexported_local_method(Gogo
* gogo
,
6568 const std::string
& name
) const
6570 Bindings
* methods
= this->local_methods_
;
6571 if (methods
!= NULL
)
6573 for (Bindings::const_declarations_iterator p
=
6574 methods
->begin_declarations();
6575 p
!= methods
->end_declarations();
6578 if (Gogo::is_hidden_name(p
->first
)
6579 && name
== Gogo::unpack_hidden_name(p
->first
)
6580 && gogo
->pack_hidden_name(name
, false) != p
->first
)
6587 // Build the complete list of methods for this type, which means
6588 // recursively including all methods for anonymous fields. Create all
6592 Named_type::finalize_methods(Gogo
* gogo
)
6594 if (this->local_methods_
!= NULL
6595 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
6597 const Bindings
* lm
= this->local_methods_
;
6598 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
6599 p
!= lm
->end_declarations();
6601 error_at(p
->second
->location(),
6602 "invalid pointer or interface receiver type");
6603 delete this->local_methods_
;
6604 this->local_methods_
= NULL
;
6608 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6611 // Return the method NAME, or NULL if there isn't one or if it is
6612 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6616 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6618 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6621 // Return a pointer to the interface method table for this type for
6622 // the interface INTERFACE. IS_POINTER is true if this is for a
6626 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
6629 gcc_assert(!interface
->is_empty());
6631 Interface_method_tables
** pimt
= (is_pointer
6632 ? &this->interface_method_tables_
6633 : &this->pointer_interface_method_tables_
);
6636 *pimt
= new Interface_method_tables(5);
6638 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
6639 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
6643 // This is a new entry in the hash table.
6644 gcc_assert(ins
.first
->second
== NULL_TREE
);
6645 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
6650 tree decl
= ins
.first
->second
;
6651 if (decl
== error_mark_node
)
6652 return error_mark_node
;
6653 gcc_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
6654 return build_fold_addr_expr(decl
);
6657 // Return whether a named type has any hidden fields.
6660 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
6665 bool ret
= this->type_
->has_hidden_fields(this, reason
);
6666 this->seen_
= false;
6670 // Look for a use of a complete type within another type. This is
6671 // used to check that we don't try to use a type within itself.
6673 class Find_type_use
: public Traverse
6676 Find_type_use(Type
* find_type
)
6677 : Traverse(traverse_types
),
6678 find_type_(find_type
), found_(false)
6681 // Whether we found the type.
6684 { return this->found_
; }
6691 // The type we are looking for.
6693 // Whether we found the type.
6697 // Check for FIND_TYPE in TYPE.
6700 Find_type_use::type(Type
* type
)
6702 if (this->find_type_
== type
)
6704 this->found_
= true;
6705 return TRAVERSE_EXIT
;
6707 // It's OK if we see a reference to the type in any type which is
6708 // essentially a pointer: a pointer, a slice, a function, a map, or
6710 if (type
->points_to() != NULL
6711 || type
->is_open_array_type()
6712 || type
->function_type() != NULL
6713 || type
->map_type() != NULL
6714 || type
->channel_type() != NULL
)
6715 return TRAVERSE_SKIP_COMPONENTS
;
6717 // For an interface, a reference to the type in a method type should
6718 // be ignored, but we have to consider direct inheritance. When
6719 // this is called, there may be cases of direct inheritance
6720 // represented as a method with no name.
6721 if (type
->interface_type() != NULL
)
6723 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
6724 if (methods
!= NULL
)
6726 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6727 p
!= methods
->end();
6730 if (p
->name().empty())
6732 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
6733 return TRAVERSE_EXIT
;
6737 return TRAVERSE_SKIP_COMPONENTS
;
6740 return TRAVERSE_CONTINUE
;
6743 // Verify that a named type does not refer to itself.
6746 Named_type::do_verify()
6748 Find_type_use
find(this);
6749 Type::traverse(this->type_
, &find
);
6752 error_at(this->location_
, "invalid recursive type %qs",
6753 this->message_name().c_str());
6754 this->is_error_
= true;
6758 // Check whether any of the local methods overloads an existing
6759 // struct field or interface method. We don't need to check the
6760 // list of methods against itself: that is handled by the Bindings
6762 if (this->local_methods_
!= NULL
)
6764 Struct_type
* st
= this->type_
->struct_type();
6765 Interface_type
* it
= this->type_
->interface_type();
6766 bool found_dup
= false;
6767 if (st
!= NULL
|| it
!= NULL
)
6769 for (Bindings::const_declarations_iterator p
=
6770 this->local_methods_
->begin_declarations();
6771 p
!= this->local_methods_
->end_declarations();
6774 const std::string
& name(p
->first
);
6775 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
6777 error_at(p
->second
->location(),
6778 "method %qs redeclares struct field name",
6779 Gogo::message_name(name
).c_str());
6782 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
6784 error_at(p
->second
->location(),
6785 "method %qs redeclares interface method name",
6786 Gogo::message_name(name
).c_str());
6798 // Return a hash code. This is used for method lookup. We simply
6799 // hash on the name itself.
6802 Named_type::do_hash_for_method(Gogo
* gogo
) const
6804 const std::string
& name(this->named_object()->name());
6805 unsigned int ret
= Type::hash_string(name
, 0);
6807 // GOGO will be NULL here when called from Type_hash_identical.
6808 // That is OK because that is only used for internal hash tables
6809 // where we are going to be comparing named types for equality. In
6810 // other cases, which are cases where the runtime is going to
6811 // compare hash codes to see if the types are the same, we need to
6812 // include the package prefix and name in the hash.
6813 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
6815 const Package
* package
= this->named_object()->package();
6816 if (package
== NULL
)
6818 ret
= Type::hash_string(gogo
->unique_prefix(), ret
);
6819 ret
= Type::hash_string(gogo
->package_name(), ret
);
6823 ret
= Type::hash_string(package
->unique_prefix(), ret
);
6824 ret
= Type::hash_string(package
->name(), ret
);
6831 // Get a tree for a named type.
6834 Named_type::do_get_tree(Gogo
* gogo
)
6836 if (this->is_error_
)
6837 return error_mark_node
;
6839 // Go permits types to refer to themselves in various ways. Break
6840 // the recursion here.
6842 switch (this->type_
->forwarded()->classification())
6845 return error_mark_node
;
6854 // These types can not refer to themselves.
6857 // All maps and channels have the same type in GENERIC.
6858 t
= Type::get_named_type_tree(gogo
, this->type_
);
6859 if (t
== error_mark_node
)
6860 return error_mark_node
;
6861 // Build a copy to set TYPE_NAME.
6862 t
= build_variant_type_copy(t
);
6866 // Don't recur infinitely if a function type refers to itself.
6867 // Ideally we would build a circular data structure here, but
6868 // GENERIC can't handle them.
6870 return ptr_type_node
;
6872 t
= Type::get_named_type_tree(gogo
, this->type_
);
6873 this->seen_
= false;
6874 if (t
== error_mark_node
)
6875 return error_mark_node
;
6876 t
= build_variant_type_copy(t
);
6880 // Don't recur infinitely if a pointer type refers to itself.
6881 // Ideally we would build a circular data structure here, but
6882 // GENERIC can't handle them.
6884 return ptr_type_node
;
6886 t
= Type::get_named_type_tree(gogo
, this->type_
);
6887 this->seen_
= false;
6888 if (t
== error_mark_node
)
6889 return error_mark_node
;
6890 t
= build_variant_type_copy(t
);
6894 if (this->named_tree_
!= NULL_TREE
)
6895 return this->named_tree_
;
6896 t
= make_node(RECORD_TYPE
);
6897 this->named_tree_
= t
;
6898 t
= this->type_
->struct_type()->fill_in_tree(gogo
, t
);
6899 if (t
== error_mark_node
)
6900 return error_mark_node
;
6904 if (!this->is_open_array_type())
6905 t
= Type::get_named_type_tree(gogo
, this->type_
);
6908 if (this->named_tree_
!= NULL_TREE
)
6909 return this->named_tree_
;
6910 t
= gogo
->slice_type_tree(void_type_node
);
6911 this->named_tree_
= t
;
6912 t
= this->type_
->array_type()->fill_in_tree(gogo
, t
);
6914 if (t
== error_mark_node
)
6915 return error_mark_node
;
6916 t
= build_variant_type_copy(t
);
6919 case TYPE_INTERFACE
:
6920 if (this->type_
->interface_type()->is_empty())
6922 t
= Type::get_named_type_tree(gogo
, this->type_
);
6923 if (t
== error_mark_node
)
6924 return error_mark_node
;
6925 t
= build_variant_type_copy(t
);
6929 if (this->named_tree_
!= NULL_TREE
)
6930 return this->named_tree_
;
6931 t
= make_node(RECORD_TYPE
);
6932 this->named_tree_
= t
;
6933 t
= this->type_
->interface_type()->fill_in_tree(gogo
, t
);
6934 if (t
== error_mark_node
)
6935 return error_mark_node
;
6941 // When a named type T1 is defined as another named type T2,
6942 // the definition must simply be "type T1 T2". If the
6943 // definition of T2 may refer to T1, then we must simply
6944 // return the type for T2 here. It's not precisely correct,
6945 // but it's as close as we can get with GENERIC.
6946 bool was_seen
= this->seen_
;
6948 t
= Type::get_named_type_tree(gogo
, this->type_
);
6949 this->seen_
= was_seen
;
6952 if (t
== error_mark_node
)
6953 return error_mark_node
;
6954 t
= build_variant_type_copy(t
);
6959 // An undefined forwarding type. Make sure the error is
6961 this->type_
->forward_declaration_type()->real_type();
6962 return error_mark_node
;
6966 case TYPE_CALL_MULTIPLE_RESULT
:
6970 tree id
= this->named_object_
->get_id(gogo
);
6971 tree decl
= build_decl(this->location_
, TYPE_DECL
, id
, t
);
6972 TYPE_NAME(t
) = decl
;
6977 // Build a type descriptor for a named type.
6980 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6982 // If NAME is not NULL, then we don't really want the type
6983 // descriptor for this type; we want the descriptor for the
6984 // underlying type, giving it the name NAME.
6985 return this->named_type_descriptor(gogo
, this->type_
,
6986 name
== NULL
? this : name
);
6989 // Add to the reflection string. This is used mostly for the name of
6990 // the type used in a type descriptor, not for actual reflection
6994 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6996 if (this->location() != BUILTINS_LOCATION
)
6998 const Package
* package
= this->named_object_
->package();
6999 if (package
!= NULL
)
7000 ret
->append(package
->name());
7002 ret
->append(gogo
->package_name());
7003 ret
->push_back('.');
7005 if (this->in_function_
!= NULL
)
7007 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7008 ret
->push_back('$');
7010 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
7013 // Get the mangled name.
7016 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7018 Named_object
* no
= this->named_object_
;
7020 if (this->location() == BUILTINS_LOCATION
)
7021 gcc_assert(this->in_function_
== NULL
);
7024 const std::string
& unique_prefix(no
->package() == NULL
7025 ? gogo
->unique_prefix()
7026 : no
->package()->unique_prefix());
7027 const std::string
& package_name(no
->package() == NULL
7028 ? gogo
->package_name()
7029 : no
->package()->name());
7030 name
= unique_prefix
;
7031 name
.append(1, '.');
7032 name
.append(package_name
);
7033 name
.append(1, '.');
7034 if (this->in_function_
!= NULL
)
7036 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7037 name
.append(1, '$');
7040 name
.append(Gogo::unpack_hidden_name(no
->name()));
7042 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
7047 // Export the type. This is called to export a global type.
7050 Named_type::export_named_type(Export
* exp
, const std::string
&) const
7052 // We don't need to write the name of the type here, because it will
7053 // be written by Export::write_type anyhow.
7054 exp
->write_c_string("type ");
7055 exp
->write_type(this);
7056 exp
->write_c_string(";\n");
7059 // Import a named type.
7062 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
7064 imp
->require_c_string("type ");
7065 Type
*type
= imp
->read_type();
7066 *ptype
= type
->named_type();
7067 gcc_assert(*ptype
!= NULL
);
7068 imp
->require_c_string(";\n");
7071 // Export the type when it is referenced by another type. In this
7072 // case Export::export_type will already have issued the name.
7075 Named_type::do_export(Export
* exp
) const
7077 exp
->write_type(this->type_
);
7079 // To save space, we only export the methods directly attached to
7081 Bindings
* methods
= this->local_methods_
;
7082 if (methods
== NULL
)
7085 exp
->write_c_string("\n");
7086 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
7087 p
!= methods
->end_definitions();
7090 exp
->write_c_string(" ");
7091 (*p
)->export_named_object(exp
);
7094 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
7095 p
!= methods
->end_declarations();
7098 if (p
->second
->is_function_declaration())
7100 exp
->write_c_string(" ");
7101 p
->second
->export_named_object(exp
);
7106 // Make a named type.
7109 Type::make_named_type(Named_object
* named_object
, Type
* type
,
7110 source_location location
)
7112 return new Named_type(named_object
, type
, location
);
7115 // Finalize the methods for TYPE. It will be a named type or a struct
7116 // type. This sets *ALL_METHODS to the list of methods, and builds
7117 // all required stubs.
7120 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, source_location location
,
7121 Methods
** all_methods
)
7123 *all_methods
= NULL
;
7124 Types_seen types_seen
;
7125 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
7127 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
7130 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7131 // build up the struct field indexes as we go. DEPTH is the depth of
7132 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7133 // adding these methods for an anonymous field with pointer type.
7134 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7135 // calls the real method. TYPES_SEEN is used to avoid infinite
7139 Type::add_methods_for_type(const Type
* type
,
7140 const Method::Field_indexes
* field_indexes
,
7142 bool is_embedded_pointer
,
7143 bool needs_stub_method
,
7144 Types_seen
* types_seen
,
7147 // Pointer types may not have methods.
7148 if (type
->points_to() != NULL
)
7151 const Named_type
* nt
= type
->named_type();
7154 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
7160 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
7161 is_embedded_pointer
, needs_stub_method
,
7164 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
7165 is_embedded_pointer
, needs_stub_method
,
7166 types_seen
, methods
);
7168 // If we are called with depth > 0, then we are looking at an
7169 // anonymous field of a struct. If such a field has interface type,
7170 // then we need to add the interface methods. We don't want to add
7171 // them when depth == 0, because we will already handle them
7172 // following the usual rules for an interface type.
7174 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
7177 // Add the local methods for the named type NT to *METHODS. The
7178 // parameters are as for add_methods_to_type.
7181 Type::add_local_methods_for_type(const Named_type
* nt
,
7182 const Method::Field_indexes
* field_indexes
,
7184 bool is_embedded_pointer
,
7185 bool needs_stub_method
,
7188 const Bindings
* local_methods
= nt
->local_methods();
7189 if (local_methods
== NULL
)
7192 if (*methods
== NULL
)
7193 *methods
= new Methods();
7195 for (Bindings::const_declarations_iterator p
=
7196 local_methods
->begin_declarations();
7197 p
!= local_methods
->end_declarations();
7200 Named_object
* no
= p
->second
;
7201 bool is_value_method
= (is_embedded_pointer
7202 || !Type::method_expects_pointer(no
));
7203 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
7205 || (depth
> 0 && is_value_method
)));
7206 if (!(*methods
)->insert(no
->name(), m
))
7211 // Add the embedded methods for TYPE to *METHODS. These are the
7212 // methods attached to anonymous fields. The parameters are as for
7213 // add_methods_to_type.
7216 Type::add_embedded_methods_for_type(const Type
* type
,
7217 const Method::Field_indexes
* field_indexes
,
7219 bool is_embedded_pointer
,
7220 bool needs_stub_method
,
7221 Types_seen
* types_seen
,
7224 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7226 const Struct_type
* st
= type
->struct_type();
7230 const Struct_field_list
* fields
= st
->fields();
7235 for (Struct_field_list::const_iterator pf
= fields
->begin();
7236 pf
!= fields
->end();
7239 if (!pf
->is_anonymous())
7242 Type
* ftype
= pf
->type();
7243 bool is_pointer
= false;
7244 if (ftype
->points_to() != NULL
)
7246 ftype
= ftype
->points_to();
7249 Named_type
* fnt
= ftype
->named_type();
7252 // This is an error, but it will be diagnosed elsewhere.
7256 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
7257 sub_field_indexes
->next
= field_indexes
;
7258 sub_field_indexes
->field_index
= i
;
7260 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
7261 (is_embedded_pointer
|| is_pointer
),
7270 // If TYPE is an interface type, then add its method to *METHODS.
7271 // This is for interface methods attached to an anonymous field. The
7272 // parameters are as for add_methods_for_type.
7275 Type::add_interface_methods_for_type(const Type
* type
,
7276 const Method::Field_indexes
* field_indexes
,
7280 const Interface_type
* it
= type
->interface_type();
7284 const Typed_identifier_list
* imethods
= it
->methods();
7285 if (imethods
== NULL
)
7288 if (*methods
== NULL
)
7289 *methods
= new Methods();
7291 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
7292 pm
!= imethods
->end();
7295 Function_type
* fntype
= pm
->type()->function_type();
7296 gcc_assert(fntype
!= NULL
&& !fntype
->is_method());
7297 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
7298 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
7299 field_indexes
, depth
);
7300 if (!(*methods
)->insert(pm
->name(), m
))
7305 // Build stub methods for TYPE as needed. METHODS is the set of
7306 // methods for the type. A stub method may be needed when a type
7307 // inherits a method from an anonymous field. When we need the
7308 // address of the method, as in a type descriptor, we need to build a
7309 // little stub which does the required field dereferences and jumps to
7310 // the real method. LOCATION is the location of the type definition.
7313 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
7314 source_location location
)
7316 if (methods
== NULL
)
7318 for (Methods::const_iterator p
= methods
->begin();
7319 p
!= methods
->end();
7322 Method
* m
= p
->second
;
7323 if (m
->is_ambiguous() || !m
->needs_stub_method())
7326 const std::string
& name(p
->first
);
7328 // Build a stub method.
7330 const Function_type
* fntype
= m
->type();
7332 static unsigned int counter
;
7334 snprintf(buf
, sizeof buf
, "$this%u", counter
);
7337 Type
* receiver_type
= const_cast<Type
*>(type
);
7338 if (!m
->is_value_method())
7339 receiver_type
= Type::make_pointer_type(receiver_type
);
7340 source_location receiver_location
= m
->receiver_location();
7341 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
7344 const Typed_identifier_list
* fnparams
= fntype
->parameters();
7345 Typed_identifier_list
* stub_params
;
7346 if (fnparams
== NULL
|| fnparams
->empty())
7350 // We give each stub parameter a unique name.
7351 stub_params
= new Typed_identifier_list();
7352 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
7353 pp
!= fnparams
->end();
7357 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
7358 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
7364 const Typed_identifier_list
* fnresults
= fntype
->results();
7365 Typed_identifier_list
* stub_results
;
7366 if (fnresults
== NULL
|| fnresults
->empty())
7367 stub_results
= NULL
;
7370 // We create the result parameters without any names, since
7371 // we won't refer to them.
7372 stub_results
= new Typed_identifier_list();
7373 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
7374 pr
!= fnresults
->end();
7376 stub_results
->push_back(Typed_identifier("", pr
->type(),
7380 Function_type
* stub_type
= Type::make_function_type(receiver
,
7383 fntype
->location());
7384 if (fntype
->is_varargs())
7385 stub_type
->set_is_varargs();
7387 // We only create the function in the package which creates the
7389 const Package
* package
;
7390 if (type
->named_type() == NULL
)
7393 package
= type
->named_type()->named_object()->package();
7395 if (package
!= NULL
)
7396 stub
= Named_object::make_function_declaration(name
, package
,
7397 stub_type
, location
);
7400 stub
= gogo
->start_function(name
, stub_type
, false,
7401 fntype
->location());
7402 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
7403 fntype
->is_varargs(), location
);
7404 gogo
->finish_function(fntype
->location());
7407 m
->set_stub_object(stub
);
7411 // Build a stub method which adjusts the receiver as required to call
7412 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7413 // PARAMS is the list of function parameters.
7416 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
7417 const char* receiver_name
,
7418 const Typed_identifier_list
* params
,
7420 source_location location
)
7422 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
7423 gcc_assert(receiver_object
!= NULL
);
7425 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
7426 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
7427 if (expr
->type()->points_to() == NULL
)
7428 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7430 Expression_list
* arguments
;
7431 if (params
== NULL
|| params
->empty())
7435 arguments
= new Expression_list();
7436 for (Typed_identifier_list::const_iterator p
= params
->begin();
7440 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
7441 gcc_assert(param
!= NULL
);
7442 Expression
* param_ref
= Expression::make_var_reference(param
,
7444 arguments
->push_back(param_ref
);
7448 Expression
* func
= method
->bind_method(expr
, location
);
7449 gcc_assert(func
!= NULL
);
7450 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
7452 size_t count
= call
->result_count();
7454 gogo
->add_statement(Statement::make_statement(call
));
7457 Expression_list
* retvals
= new Expression_list();
7459 retvals
->push_back(call
);
7462 for (size_t i
= 0; i
< count
; ++i
)
7463 retvals
->push_back(Expression::make_call_result(call
, i
));
7465 const Function
* function
= gogo
->current_function()->func_value();
7466 const Typed_identifier_list
* results
= function
->type()->results();
7467 Statement
* retstat
= Statement::make_return_statement(results
, retvals
,
7469 gogo
->add_statement(retstat
);
7473 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7474 // in reverse order.
7477 Type::apply_field_indexes(Expression
* expr
,
7478 const Method::Field_indexes
* field_indexes
,
7479 source_location location
)
7481 if (field_indexes
== NULL
)
7483 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
7484 Struct_type
* stype
= expr
->type()->deref()->struct_type();
7485 gcc_assert(stype
!= NULL
7486 && field_indexes
->field_index
< stype
->field_count());
7487 if (expr
->type()->struct_type() == NULL
)
7489 gcc_assert(expr
->type()->points_to() != NULL
);
7490 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7491 gcc_assert(expr
->type()->struct_type() == stype
);
7493 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
7497 // Return whether NO is a method for which the receiver is a pointer.
7500 Type::method_expects_pointer(const Named_object
* no
)
7502 const Function_type
*fntype
;
7503 if (no
->is_function())
7504 fntype
= no
->func_value()->type();
7505 else if (no
->is_function_declaration())
7506 fntype
= no
->func_declaration_value()->type();
7509 return fntype
->receiver()->type()->points_to() != NULL
;
7512 // Given a set of methods for a type, METHODS, return the method NAME,
7513 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7514 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7515 // but is ambiguous (and return NULL).
7518 Type::method_function(const Methods
* methods
, const std::string
& name
,
7521 if (is_ambiguous
!= NULL
)
7522 *is_ambiguous
= false;
7523 if (methods
== NULL
)
7525 Methods::const_iterator p
= methods
->find(name
);
7526 if (p
== methods
->end())
7528 Method
* m
= p
->second
;
7529 if (m
->is_ambiguous())
7531 if (is_ambiguous
!= NULL
)
7532 *is_ambiguous
= true;
7538 // Look for field or method NAME for TYPE. Return an Expression for
7539 // the field or method bound to EXPR. If there is no such field or
7540 // method, give an appropriate error and return an error expression.
7543 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
7544 const std::string
& name
,
7545 source_location location
)
7547 if (type
->is_error_type())
7548 return Expression::make_error(location
);
7550 const Named_type
* nt
= type
->named_type();
7552 nt
= type
->deref()->named_type();
7553 const Struct_type
* st
= type
->deref()->struct_type();
7554 const Interface_type
* it
= type
->deref()->interface_type();
7556 // If this is a pointer to a pointer, then it is possible that the
7557 // pointed-to type has methods.
7561 && type
->points_to() != NULL
7562 && type
->points_to()->points_to() != NULL
)
7564 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7565 type
= type
->points_to();
7566 nt
= type
->points_to()->named_type();
7567 st
= type
->points_to()->struct_type();
7568 it
= type
->points_to()->interface_type();
7571 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
7572 || expr
->is_addressable());
7573 bool is_method
= false;
7574 bool found_pointer_method
= false;
7577 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
, NULL
,
7578 &is_method
, &found_pointer_method
,
7584 gcc_assert(st
!= NULL
);
7585 if (type
->struct_type() == NULL
)
7587 gcc_assert(type
->points_to() != NULL
);
7588 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
7590 gcc_assert(expr
->type()->struct_type() == st
);
7592 ret
= st
->field_reference(expr
, name
, location
);
7594 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7595 ret
= Expression::make_interface_field_reference(expr
, name
,
7601 m
= nt
->method_function(name
, NULL
);
7602 else if (st
!= NULL
)
7603 m
= st
->method_function(name
, NULL
);
7606 gcc_assert(m
!= NULL
);
7607 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
7608 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7609 ret
= m
->bind_method(expr
, location
);
7611 gcc_assert(ret
!= NULL
);
7616 if (!ambig1
.empty())
7617 error_at(location
, "%qs is ambiguous via %qs and %qs",
7618 Gogo::message_name(name
).c_str(),
7619 Gogo::message_name(ambig1
).c_str(),
7620 Gogo::message_name(ambig2
).c_str());
7621 else if (found_pointer_method
)
7622 error_at(location
, "method requires a pointer");
7623 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
7625 ("reference to field %qs in object which "
7626 "has no fields or methods"),
7627 Gogo::message_name(name
).c_str());
7631 if (!Gogo::is_hidden_name(name
))
7632 is_unexported
= false;
7635 std::string unpacked
= Gogo::unpack_hidden_name(name
);
7636 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
7640 error_at(location
, "reference to unexported field or method %qs",
7641 Gogo::message_name(name
).c_str());
7643 error_at(location
, "reference to undefined field or method %qs",
7644 Gogo::message_name(name
).c_str());
7646 return Expression::make_error(location
);
7650 // Look in TYPE for a field or method named NAME, return true if one
7651 // is found. This looks through embedded anonymous fields and handles
7652 // ambiguity. If a method is found, sets *IS_METHOD to true;
7653 // otherwise, if a field is found, set it to false. If
7654 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7655 // whose address can not be taken. When returning false, this sets
7656 // *FOUND_POINTER_METHOD if we found a method we couldn't use because
7657 // it requires a pointer. LEVEL is used for recursive calls, and can
7658 // be NULL for a non-recursive call. When this function returns false
7659 // because it finds that the name is ambiguous, it will store a path
7660 // to the ambiguous names in *AMBIG1 and *AMBIG2. If the name is not
7661 // found at all, *AMBIG1 and *AMBIG2 will be unchanged.
7663 // This function just returns whether or not there is a field or
7664 // method, and whether it is a field or method. It doesn't build an
7665 // expression to refer to it. If it is a method, we then look in the
7666 // list of all methods for the type. If it is a field, the search has
7667 // to be done again, looking only for fields, and building up the
7668 // expression as we go.
7671 Type::find_field_or_method(const Type
* type
,
7672 const std::string
& name
,
7673 bool receiver_can_be_pointer
,
7676 bool* found_pointer_method
,
7677 std::string
* ambig1
,
7678 std::string
* ambig2
)
7680 // Named types can have locally defined methods.
7681 const Named_type
* nt
= type
->named_type();
7682 if (nt
== NULL
&& type
->points_to() != NULL
)
7683 nt
= type
->points_to()->named_type();
7686 Named_object
* no
= nt
->find_local_method(name
);
7689 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
7695 // Record that we have found a pointer method in order to
7696 // give a better error message if we don't find anything
7698 *found_pointer_method
= true;
7702 // Interface types can have methods.
7703 const Interface_type
* it
= type
->deref()->interface_type();
7704 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7710 // Struct types can have fields. They can also inherit fields and
7711 // methods from anonymous fields.
7712 const Struct_type
* st
= type
->deref()->struct_type();
7715 const Struct_field_list
* fields
= st
->fields();
7719 int found_level
= 0;
7720 bool found_is_method
= false;
7721 std::string found_ambig1
;
7722 std::string found_ambig2
;
7723 const Struct_field
* found_parent
= NULL
;
7724 for (Struct_field_list::const_iterator pf
= fields
->begin();
7725 pf
!= fields
->end();
7728 if (pf
->field_name() == name
)
7734 if (!pf
->is_anonymous())
7737 if (pf
->type()->is_error_type() || pf
->type()->is_undefined())
7740 Named_type
* fnt
= pf
->type()->deref()->named_type();
7741 gcc_assert(fnt
!= NULL
);
7743 int sublevel
= level
== NULL
? 1 : *level
+ 1;
7745 std::string subambig1
;
7746 std::string subambig2
;
7747 bool subfound
= Type::find_field_or_method(fnt
,
7749 receiver_can_be_pointer
,
7752 found_pointer_method
,
7757 if (!subambig1
.empty())
7759 // The name was found via this field, but is ambiguous.
7760 // if the ambiguity is lower or at the same level as
7761 // anything else we have already found, then we want to
7762 // pass the ambiguity back to the caller.
7763 if (found_level
== 0 || sublevel
<= found_level
)
7765 found_ambig1
= pf
->field_name() + '.' + subambig1
;
7766 found_ambig2
= pf
->field_name() + '.' + subambig2
;
7767 found_level
= sublevel
;
7773 // The name was found via this field. Use the level to see
7774 // if we want to use this one, or whether it introduces an
7776 if (found_level
== 0 || sublevel
< found_level
)
7778 found_level
= sublevel
;
7779 found_is_method
= sub_is_method
;
7780 found_ambig1
.clear();
7781 found_ambig2
.clear();
7782 found_parent
= &*pf
;
7784 else if (sublevel
> found_level
)
7786 else if (found_ambig1
.empty())
7788 // We found an ambiguity.
7789 gcc_assert(found_parent
!= NULL
);
7790 found_ambig1
= found_parent
->field_name();
7791 found_ambig2
= pf
->field_name();
7795 // We found an ambiguity, but we already know of one.
7796 // Just report the earlier one.
7801 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7802 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7803 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7804 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7806 if (found_level
== 0)
7808 else if (!found_ambig1
.empty())
7810 gcc_assert(!found_ambig1
.empty());
7811 ambig1
->assign(found_ambig1
);
7812 ambig2
->assign(found_ambig2
);
7814 *level
= found_level
;
7820 *level
= found_level
;
7821 *is_method
= found_is_method
;
7826 // Return whether NAME is an unexported field or method for TYPE.
7829 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
7830 const std::string
& name
)
7832 type
= type
->deref();
7834 const Named_type
* nt
= type
->named_type();
7835 if (nt
!= NULL
&& nt
->is_unexported_local_method(gogo
, name
))
7838 const Interface_type
* it
= type
->interface_type();
7839 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
7842 const Struct_type
* st
= type
->struct_type();
7843 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
7849 const Struct_field_list
* fields
= st
->fields();
7853 for (Struct_field_list::const_iterator pf
= fields
->begin();
7854 pf
!= fields
->end();
7857 if (pf
->is_anonymous()
7858 && (!pf
->type()->is_error_type() && !pf
->type()->is_undefined()))
7860 Named_type
* subtype
= pf
->type()->deref()->named_type();
7861 gcc_assert(subtype
!= NULL
);
7862 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
))
7870 // Class Forward_declaration.
7872 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
7873 : Type(TYPE_FORWARD
),
7874 named_object_(named_object
->resolve()), warned_(false)
7876 gcc_assert(this->named_object_
->is_unknown()
7877 || this->named_object_
->is_type_declaration());
7880 // Return the named object.
7883 Forward_declaration_type::named_object()
7885 return this->named_object_
->resolve();
7889 Forward_declaration_type::named_object() const
7891 return this->named_object_
->resolve();
7894 // Return the name of the forward declared type.
7897 Forward_declaration_type::name() const
7899 return this->named_object()->name();
7902 // Warn about a use of a type which has been declared but not defined.
7905 Forward_declaration_type::warn() const
7907 Named_object
* no
= this->named_object_
->resolve();
7908 if (no
->is_unknown())
7910 // The name was not defined anywhere.
7913 error_at(this->named_object_
->location(),
7914 "use of undefined type %qs",
7915 no
->message_name().c_str());
7916 this->warned_
= true;
7919 else if (no
->is_type_declaration())
7921 // The name was seen as a type, but the type was never defined.
7922 if (no
->type_declaration_value()->using_type())
7924 error_at(this->named_object_
->location(),
7925 "use of undefined type %qs",
7926 no
->message_name().c_str());
7927 this->warned_
= true;
7932 // The name was defined, but not as a type.
7935 error_at(this->named_object_
->location(), "expected type");
7936 this->warned_
= true;
7941 // Get the base type of a declaration. This gives an error if the
7942 // type has not yet been defined.
7945 Forward_declaration_type::real_type()
7947 if (this->is_defined())
7948 return this->named_object()->type_value();
7952 return Type::make_error_type();
7957 Forward_declaration_type::real_type() const
7959 if (this->is_defined())
7960 return this->named_object()->type_value();
7964 return Type::make_error_type();
7968 // Return whether the base type is defined.
7971 Forward_declaration_type::is_defined() const
7973 return this->named_object()->is_type();
7976 // Add a method. This is used when methods are defined before the
7980 Forward_declaration_type::add_method(const std::string
& name
,
7983 Named_object
* no
= this->named_object();
7984 gcc_assert(no
->is_type_declaration());
7985 return no
->type_declaration_value()->add_method(name
, function
);
7988 // Add a method declaration. This is used when methods are declared
7992 Forward_declaration_type::add_method_declaration(const std::string
& name
,
7993 Function_type
* type
,
7994 source_location location
)
7996 Named_object
* no
= this->named_object();
7997 gcc_assert(no
->is_type_declaration());
7998 Type_declaration
* td
= no
->type_declaration_value();
7999 return td
->add_method_declaration(name
, type
, location
);
8005 Forward_declaration_type::do_traverse(Traverse
* traverse
)
8007 if (this->is_defined()
8008 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
8009 return TRAVERSE_EXIT
;
8010 return TRAVERSE_CONTINUE
;
8013 // Get a tree for the type.
8016 Forward_declaration_type::do_get_tree(Gogo
* gogo
)
8018 if (this->is_defined())
8019 return Type::get_named_type_tree(gogo
, this->real_type());
8022 return error_mark_node
;
8024 // We represent an undefined type as a struct with no fields. That
8025 // should work fine for the middle-end, since the same case can
8027 Named_object
* no
= this->named_object();
8028 tree type_tree
= make_node(RECORD_TYPE
);
8029 tree id
= no
->get_id(gogo
);
8030 tree decl
= build_decl(no
->location(), TYPE_DECL
, id
, type_tree
);
8031 TYPE_NAME(type_tree
) = decl
;
8035 // Build a type descriptor for a forwarded type.
8038 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8040 if (!this->is_defined())
8041 return Expression::make_nil(BUILTINS_LOCATION
);
8044 Type
* t
= this->real_type();
8046 return this->named_type_descriptor(gogo
, t
, name
);
8048 return Expression::make_type_descriptor(t
, BUILTINS_LOCATION
);
8052 // The reflection string.
8055 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8057 this->append_reflection(this->real_type(), gogo
, ret
);
8060 // The mangled name.
8063 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8065 if (this->is_defined())
8066 this->append_mangled_name(this->real_type(), gogo
, ret
);
8069 const Named_object
* no
= this->named_object();
8071 if (no
->package() == NULL
)
8072 name
= gogo
->package_name();
8074 name
= no
->package()->name();
8076 name
+= Gogo::unpack_hidden_name(no
->name());
8078 snprintf(buf
, sizeof buf
, "N%u_",
8079 static_cast<unsigned int>(name
.length()));
8085 // Export a forward declaration. This can happen when a defined type
8086 // refers to a type which is only declared (and is presumably defined
8087 // in some other file in the same package).
8090 Forward_declaration_type::do_export(Export
*) const
8092 // If there is a base type, that should be exported instead of this.
8093 gcc_assert(!this->is_defined());
8095 // We don't output anything.
8098 // Make a forward declaration.
8101 Type::make_forward_declaration(Named_object
* named_object
)
8103 return new Forward_declaration_type(named_object
);
8106 // Class Typed_identifier_list.
8108 // Sort the entries by name.
8110 struct Typed_identifier_list_sort
8114 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
8115 { return t1
.name() < t2
.name(); }
8119 Typed_identifier_list::sort_by_name()
8121 std::sort(this->entries_
.begin(), this->entries_
.end(),
8122 Typed_identifier_list_sort());
8128 Typed_identifier_list::traverse(Traverse
* traverse
)
8130 for (Typed_identifier_list::const_iterator p
= this->begin();
8134 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
8135 return TRAVERSE_EXIT
;
8137 return TRAVERSE_CONTINUE
;
8142 Typed_identifier_list
*
8143 Typed_identifier_list::copy() const
8145 Typed_identifier_list
* ret
= new Typed_identifier_list();
8146 for (Typed_identifier_list::const_iterator p
= this->begin();
8149 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));