1 // types.cc -- Go frontend types.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
23 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
31 #include "statements.h"
38 Type::Type(Type_classification classification
)
39 : classification_(classification
), tree_(NULL_TREE
),
40 type_descriptor_decl_(NULL_TREE
)
48 // Get the base type for a type--skip names and forward declarations.
53 switch (this->classification_
)
56 return this->named_type()->named_base();
58 return this->forward_declaration_type()->real_type()->base();
67 switch (this->classification_
)
70 return this->named_type()->named_base();
72 return this->forward_declaration_type()->real_type()->base();
78 // Skip defined forward declarations.
84 Forward_declaration_type
* ftype
= t
->forward_declaration_type();
85 while (ftype
!= NULL
&& ftype
->is_defined())
87 t
= ftype
->real_type();
88 ftype
= t
->forward_declaration_type();
94 Type::forwarded() const
97 const Forward_declaration_type
* ftype
= t
->forward_declaration_type();
98 while (ftype
!= NULL
&& ftype
->is_defined())
100 t
= ftype
->real_type();
101 ftype
= t
->forward_declaration_type();
106 // If this is a named type, return it. Otherwise, return NULL.
111 return this->forwarded()->convert_no_base
<Named_type
, TYPE_NAMED
>();
115 Type::named_type() const
117 return this->forwarded()->convert_no_base
<const Named_type
, TYPE_NAMED
>();
120 // Return true if this type is not defined.
123 Type::is_undefined() const
125 return this->forwarded()->forward_declaration_type() != NULL
;
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
132 Type::is_basic_type() const
134 switch (this->classification_
)
157 return this->base()->is_basic_type();
164 // Return true if this is an abstract type.
167 Type::is_abstract() const
169 switch (this->classification())
172 return this->integer_type()->is_abstract();
174 return this->float_type()->is_abstract();
176 return this->complex_type()->is_abstract();
178 return this->is_abstract_string_type();
180 return this->is_abstract_boolean_type();
186 // Return a non-abstract version of an abstract type.
189 Type::make_non_abstract_type()
191 gcc_assert(this->is_abstract());
192 switch (this->classification())
195 return Type::lookup_integer_type("int");
197 return Type::lookup_float_type("float");
199 return Type::lookup_complex_type("complex");
201 return Type::lookup_string_type();
203 return Type::lookup_bool_type();
209 // Return true if this is an error type. Don't give an error if we
210 // try to dereference an undefined forwarding type, as this is called
211 // in the parser when the type may legitimately be undefined.
214 Type::is_error_type() const
216 const Type
* t
= this->forwarded();
217 // Note that we return false for an undefined forward type.
218 switch (t
->classification_
)
223 return t
->named_type()->is_named_error_type();
229 // If this is a pointer type, return the type to which it points.
230 // Otherwise, return NULL.
233 Type::points_to() const
235 const Pointer_type
* ptype
= this->convert
<const Pointer_type
,
237 return ptype
== NULL
? NULL
: ptype
->points_to();
240 // Return whether this is an open array type.
243 Type::is_open_array_type() const
245 return this->array_type() != NULL
&& this->array_type()->length() == NULL
;
248 // Return whether this is the predeclared constant nil being used as a
252 Type::is_nil_constant_as_type() const
254 const Type
* t
= this->forwarded();
255 if (t
->forward_declaration_type() != NULL
)
257 const Named_object
* no
= t
->forward_declaration_type()->named_object();
258 if (no
->is_unknown())
259 no
= no
->unknown_value()->real_named_object();
262 && no
->const_value()->expr()->is_nil_expression())
271 Type::traverse(Type
* type
, Traverse
* traverse
)
273 gcc_assert((traverse
->traverse_mask() & Traverse::traverse_types
) != 0
274 || (traverse
->traverse_mask()
275 & Traverse::traverse_expressions
) != 0);
276 if (traverse
->remember_type(type
))
278 // We have already traversed this type.
279 return TRAVERSE_CONTINUE
;
281 if ((traverse
->traverse_mask() & Traverse::traverse_types
) != 0)
283 int t
= traverse
->type(type
);
284 if (t
== TRAVERSE_EXIT
)
285 return TRAVERSE_EXIT
;
286 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
287 return TRAVERSE_CONTINUE
;
289 // An array type has an expression which we need to traverse if
290 // traverse_expressions is set.
291 if (type
->do_traverse(traverse
) == TRAVERSE_EXIT
)
292 return TRAVERSE_EXIT
;
293 return TRAVERSE_CONTINUE
;
296 // Default implementation for do_traverse for child class.
299 Type::do_traverse(Traverse
*)
301 return TRAVERSE_CONTINUE
;
304 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
305 // then return true for all erroneous types; this is used to avoid
306 // cascading errors. If REASON is not NULL, optionally set *REASON to
307 // the reason the types are not identical.
310 Type::are_identical(const Type
* t1
, const Type
* t2
, bool errors_are_identical
,
313 if (t1
== NULL
|| t2
== NULL
)
315 // Something is wrong.
316 return errors_are_identical
? true : t1
== t2
;
319 // Skip defined forward declarations.
320 t1
= t1
->forwarded();
321 t2
= t2
->forwarded();
326 // An undefined forward declaration is an error.
327 if (t1
->forward_declaration_type() != NULL
328 || t2
->forward_declaration_type() != NULL
)
329 return errors_are_identical
;
331 // Avoid cascading errors with error types.
332 if (t1
->is_error_type() || t2
->is_error_type())
334 if (errors_are_identical
)
336 return t1
->is_error_type() && t2
->is_error_type();
339 // Get a good reason for the sink type. Note that the sink type on
340 // the left hand side of an assignment is handled in are_assignable.
341 if (t1
->is_sink_type() || t2
->is_sink_type())
344 *reason
= "invalid use of _";
348 // A named type is only identical to itself.
349 if (t1
->named_type() != NULL
|| t2
->named_type() != NULL
)
352 // Check type shapes.
353 if (t1
->classification() != t2
->classification())
356 switch (t1
->classification())
362 // These types are always identical.
366 return t1
->integer_type()->is_identical(t2
->integer_type());
369 return t1
->float_type()->is_identical(t2
->float_type());
372 return t1
->complex_type()->is_identical(t2
->complex_type());
375 return t1
->function_type()->is_identical(t2
->function_type(),
377 errors_are_identical
,
381 return Type::are_identical(t1
->points_to(), t2
->points_to(),
382 errors_are_identical
, reason
);
385 return t1
->struct_type()->is_identical(t2
->struct_type(),
386 errors_are_identical
);
389 return t1
->array_type()->is_identical(t2
->array_type(),
390 errors_are_identical
);
393 return t1
->map_type()->is_identical(t2
->map_type(),
394 errors_are_identical
);
397 return t1
->channel_type()->is_identical(t2
->channel_type(),
398 errors_are_identical
);
401 return t1
->interface_type()->is_identical(t2
->interface_type(),
402 errors_are_identical
);
409 // Return true if it's OK to have a binary operation with types LHS
410 // and RHS. This is not used for shifts or comparisons.
413 Type::are_compatible_for_binop(const Type
* lhs
, const Type
* rhs
)
415 if (Type::are_identical(lhs
, rhs
, true, NULL
))
418 // A constant of abstract bool type may be mixed with any bool type.
419 if ((rhs
->is_abstract_boolean_type() && lhs
->is_boolean_type())
420 || (lhs
->is_abstract_boolean_type() && rhs
->is_boolean_type()))
423 // A constant of abstract string type may be mixed with any string
425 if ((rhs
->is_abstract_string_type() && lhs
->is_string_type())
426 || (lhs
->is_abstract_string_type() && rhs
->is_string_type()))
432 // A constant of abstract integer, float, or complex type may be
433 // mixed with an integer, float, or complex type.
434 if ((rhs
->is_abstract()
435 && (rhs
->integer_type() != NULL
436 || rhs
->float_type() != NULL
437 || rhs
->complex_type() != NULL
)
438 && (lhs
->integer_type() != NULL
439 || lhs
->float_type() != NULL
440 || lhs
->complex_type() != NULL
))
441 || (lhs
->is_abstract()
442 && (lhs
->integer_type() != NULL
443 || lhs
->float_type() != NULL
444 || lhs
->complex_type() != NULL
)
445 && (rhs
->integer_type() != NULL
446 || rhs
->float_type() != NULL
447 || rhs
->complex_type() != NULL
)))
450 // The nil type may be compared to a pointer, an interface type, a
451 // slice type, a channel type, a map type, or a function type.
452 if (lhs
->is_nil_type()
453 && (rhs
->points_to() != NULL
454 || rhs
->interface_type() != NULL
455 || rhs
->is_open_array_type()
456 || rhs
->map_type() != NULL
457 || rhs
->channel_type() != NULL
458 || rhs
->function_type() != NULL
))
460 if (rhs
->is_nil_type()
461 && (lhs
->points_to() != NULL
462 || lhs
->interface_type() != NULL
463 || lhs
->is_open_array_type()
464 || lhs
->map_type() != NULL
465 || lhs
->channel_type() != NULL
466 || lhs
->function_type() != NULL
))
472 // Return true if a value with type RHS may be assigned to a variable
473 // with type LHS. If REASON is not NULL, set *REASON to the reason
474 // the types are not assignable.
477 Type::are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
479 // Do some checks first. Make sure the types are defined.
480 if (lhs
!= NULL
&& lhs
->forwarded()->forward_declaration_type() == NULL
)
482 // Any value may be assigned to the blank identifier.
483 if (lhs
->is_sink_type())
486 // All fields of a struct must be exported, or the assignment
487 // must be in the same package.
488 if (rhs
!= NULL
&& rhs
->forwarded()->forward_declaration_type() == NULL
)
490 if (lhs
->has_hidden_fields(NULL
, reason
)
491 || rhs
->has_hidden_fields(NULL
, reason
))
496 // Identical types are assignable.
497 if (Type::are_identical(lhs
, rhs
, true, reason
))
500 // The types are assignable if they have identical underlying types
501 // and either LHS or RHS is not a named type.
502 if (((lhs
->named_type() != NULL
&& rhs
->named_type() == NULL
)
503 || (rhs
->named_type() != NULL
&& lhs
->named_type() == NULL
))
504 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
507 // The types are assignable if LHS is an interface type and RHS
508 // implements the required methods.
509 const Interface_type
* lhs_interface_type
= lhs
->interface_type();
510 if (lhs_interface_type
!= NULL
)
512 if (lhs_interface_type
->implements_interface(rhs
, reason
))
514 const Interface_type
* rhs_interface_type
= rhs
->interface_type();
515 if (rhs_interface_type
!= NULL
516 && lhs_interface_type
->is_compatible_for_assign(rhs_interface_type
,
521 // The type are assignable if RHS is a bidirectional channel type,
522 // LHS is a channel type, they have identical element types, and
523 // either LHS or RHS is not a named type.
524 if (lhs
->channel_type() != NULL
525 && rhs
->channel_type() != NULL
526 && rhs
->channel_type()->may_send()
527 && rhs
->channel_type()->may_receive()
528 && (lhs
->named_type() == NULL
|| rhs
->named_type() == NULL
)
529 && Type::are_identical(lhs
->channel_type()->element_type(),
530 rhs
->channel_type()->element_type(),
535 // The nil type may be assigned to a pointer, function, slice, map,
536 // channel, or interface type.
537 if (rhs
->is_nil_type()
538 && (lhs
->points_to() != NULL
539 || lhs
->function_type() != NULL
540 || lhs
->is_open_array_type()
541 || lhs
->map_type() != NULL
542 || lhs
->channel_type() != NULL
543 || lhs
->interface_type() != NULL
))
546 // An untyped constant may be assigned to a numeric type if it is
547 // representable in that type.
548 if (rhs
->is_abstract()
549 && (lhs
->integer_type() != NULL
550 || lhs
->float_type() != NULL
551 || lhs
->complex_type() != NULL
))
555 // Give some better error messages.
556 if (reason
!= NULL
&& reason
->empty())
558 if (rhs
->interface_type() != NULL
)
559 reason
->assign(_("need explicit conversion"));
560 else if (rhs
->is_call_multiple_result_type())
561 reason
->assign(_("multiple value function call in "
562 "single value context"));
563 else if (lhs
->named_type() != NULL
&& rhs
->named_type() != NULL
)
565 size_t len
= (lhs
->named_type()->name().length()
566 + rhs
->named_type()->name().length()
568 char* buf
= new char[len
];
569 snprintf(buf
, len
, _("cannot use type %s as type %s"),
570 rhs
->named_type()->message_name().c_str(),
571 lhs
->named_type()->message_name().c_str());
580 // Return true if a value with type RHS may be converted to type LHS.
581 // If REASON is not NULL, set *REASON to the reason the types are not
585 Type::are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
)
587 // The types are convertible if they are assignable.
588 if (Type::are_assignable(lhs
, rhs
, reason
))
591 // The types are convertible if they have identical underlying
593 if ((lhs
->named_type() != NULL
|| rhs
->named_type() != NULL
)
594 && Type::are_identical(lhs
->base(), rhs
->base(), true, reason
))
597 // The types are convertible if they are both unnamed pointer types
598 // and their pointer base types have identical underlying types.
599 if (lhs
->named_type() == NULL
600 && rhs
->named_type() == NULL
601 && lhs
->points_to() != NULL
602 && rhs
->points_to() != NULL
603 && (lhs
->points_to()->named_type() != NULL
604 || rhs
->points_to()->named_type() != NULL
)
605 && Type::are_identical(lhs
->points_to()->base(),
606 rhs
->points_to()->base(),
611 // Integer and floating point types are convertible to each other.
612 if ((lhs
->integer_type() != NULL
|| lhs
->float_type() != NULL
)
613 && (rhs
->integer_type() != NULL
|| rhs
->float_type() != NULL
))
616 // Complex types are convertible to each other.
617 if (lhs
->complex_type() != NULL
&& rhs
->complex_type() != NULL
)
620 // An integer, or []byte, or []int, may be converted to a string.
621 if (lhs
->is_string_type())
623 if (rhs
->integer_type() != NULL
)
625 if (rhs
->is_open_array_type() && rhs
->named_type() == NULL
)
627 const Type
* e
= rhs
->array_type()->element_type()->forwarded();
628 if (e
->integer_type() != NULL
629 && (e
== Type::lookup_integer_type("uint8")
630 || e
== Type::lookup_integer_type("int")))
635 // A string may be converted to []byte or []int.
636 if (rhs
->is_string_type()
637 && lhs
->is_open_array_type()
638 && lhs
->named_type() == NULL
)
640 const Type
* e
= lhs
->array_type()->element_type()->forwarded();
641 if (e
->integer_type() != NULL
642 && (e
== Type::lookup_integer_type("uint8")
643 || e
== Type::lookup_integer_type("int")))
647 // An unsafe.Pointer type may be converted to any pointer type or to
648 // uintptr, and vice-versa.
649 if (lhs
->is_unsafe_pointer_type()
650 && (rhs
->points_to() != NULL
651 || (rhs
->integer_type() != NULL
652 && rhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
654 if (rhs
->is_unsafe_pointer_type()
655 && (lhs
->points_to() != NULL
656 || (lhs
->integer_type() != NULL
657 && lhs
->forwarded() == Type::lookup_integer_type("uintptr"))))
660 // Give a better error message.
664 *reason
= "invalid type conversion";
667 std::string s
= "invalid type conversion (";
677 // Return whether this type has any hidden fields. This is only a
678 // possibility for a few types.
681 Type::has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
683 switch (this->forwarded()->classification_
)
686 return this->named_type()->named_type_has_hidden_fields(reason
);
688 return this->struct_type()->struct_has_hidden_fields(within
, reason
);
690 return this->array_type()->array_has_hidden_fields(within
, reason
);
696 // Return a hash code for the type to be used for method lookup.
699 Type::hash_for_method(Gogo
* gogo
) const
701 unsigned int ret
= 0;
702 if (this->classification_
!= TYPE_FORWARD
)
703 ret
+= this->classification_
;
704 return ret
+ this->do_hash_for_method(gogo
);
707 // Default implementation of do_hash_for_method. This is appropriate
708 // for types with no subfields.
711 Type::do_hash_for_method(Gogo
*) const
716 // Return a hash code for a string, given a starting hash.
719 Type::hash_string(const std::string
& s
, unsigned int h
)
721 const char* p
= s
.data();
722 size_t len
= s
.length();
723 for (; len
> 0; --len
)
731 // Default check for the expression passed to make. Any type which
732 // may be used with make implements its own version of this.
735 Type::do_check_make_expression(Expression_list
*, source_location
)
740 // Return whether an expression has an integer value. Report an error
741 // if not. This is used when handling calls to the predeclared make
745 Type::check_int_value(Expression
* e
, const char* errmsg
,
746 source_location location
)
748 if (e
->type()->integer_type() != NULL
)
751 // Check for a floating point constant with integer value.
756 if (e
->float_constant_value(fval
, &dummy
))
763 mpfr_clear_overflow();
764 mpfr_clear_erangeflag();
765 mpfr_get_z(ival
, fval
, GMP_RNDN
);
766 if (!mpfr_overflow_p()
767 && !mpfr_erangeflag_p()
768 && mpz_sgn(ival
) >= 0)
770 Named_type
* ntype
= Type::lookup_integer_type("int");
771 Integer_type
* inttype
= ntype
->integer_type();
773 mpz_init_set_ui(max
, 1);
774 mpz_mul_2exp(max
, max
, inttype
->bits() - 1);
775 ok
= mpz_cmp(ival
, max
) < 0;
789 error_at(location
, "%s", errmsg
);
793 // A hash table mapping unnamed types to trees.
795 Type::Type_trees
Type::type_trees
;
797 // Return a tree representing this type.
800 Type::get_tree(Gogo
* gogo
)
802 if (this->tree_
!= NULL
)
805 if (this->forward_declaration_type() != NULL
806 || this->named_type() != NULL
)
807 return this->get_tree_without_hash(gogo
);
809 if (this->is_error_type())
810 return error_mark_node
;
812 // To avoid confusing GIMPLE, we need to translate all identical Go
813 // types to the same GIMPLE type. We use a hash table to do that.
814 // There is no need to use the hash table for named types, as named
815 // types are only identical to themselves.
817 std::pair
<Type
*, tree
> val(this, NULL
);
818 std::pair
<Type_trees::iterator
, bool> ins
=
819 Type::type_trees
.insert(val
);
820 if (!ins
.second
&& ins
.first
->second
!= NULL_TREE
)
822 this->tree_
= ins
.first
->second
;
826 tree t
= this->get_tree_without_hash(gogo
);
828 if (ins
.first
->second
== NULL_TREE
)
829 ins
.first
->second
= t
;
832 // We have already created a tree for this type. This can
833 // happen when an unnamed type is defined using a named type
834 // which in turns uses an identical unnamed type. Use the tree
835 // we created earlier and ignore the one we just built.
836 t
= ins
.first
->second
;
843 // Return a tree for a type without looking in the hash table for
844 // identical types. This is used for named types, since there is no
845 // point to looking in the hash table for them.
848 Type::get_tree_without_hash(Gogo
* gogo
)
850 if (this->tree_
== NULL_TREE
)
852 tree t
= this->do_get_tree(gogo
);
854 // For a recursive function or pointer type, we will temporarily
855 // return ptr_type_node during the recursion. We don't want to
856 // record that for a forwarding type, as it may confuse us
858 if (t
== ptr_type_node
&& this->forward_declaration_type() != NULL
)
862 go_preserve_from_gc(t
);
868 // Return a tree representing a zero initialization for this type.
871 Type::get_init_tree(Gogo
* gogo
, bool is_clear
)
873 tree type_tree
= this->get_tree(gogo
);
874 if (type_tree
== error_mark_node
)
875 return error_mark_node
;
876 return this->do_get_init_tree(gogo
, type_tree
, is_clear
);
879 // Any type which supports the builtin make function must implement
883 Type::do_make_expression_tree(Translate_context
*, Expression_list
*,
889 // Return a pointer to the type descriptor for this type.
892 Type::type_descriptor_pointer(Gogo
* gogo
)
894 Type
* t
= this->forwarded();
895 if (t
->type_descriptor_decl_
== NULL_TREE
)
897 Expression
* e
= t
->do_type_descriptor(gogo
, NULL
);
898 gogo
->build_type_descriptor_decl(t
, e
, &t
->type_descriptor_decl_
);
899 gcc_assert(t
->type_descriptor_decl_
!= NULL_TREE
900 && (t
->type_descriptor_decl_
== error_mark_node
901 || DECL_P(t
->type_descriptor_decl_
)));
903 if (t
->type_descriptor_decl_
== error_mark_node
)
904 return error_mark_node
;
905 return build_fold_addr_expr(t
->type_descriptor_decl_
);
908 // Return a composite literal for a type descriptor.
911 Type::type_descriptor(Gogo
* gogo
, Type
* type
)
913 return type
->do_type_descriptor(gogo
, NULL
);
916 // Return a composite literal for a type descriptor with a name.
919 Type::named_type_descriptor(Gogo
* gogo
, Type
* type
, Named_type
* name
)
921 gcc_assert(name
!= NULL
&& type
->named_type() != name
);
922 return type
->do_type_descriptor(gogo
, name
);
925 // Make a builtin struct type from a list of fields. The fields are
926 // pairs of a name and a type.
929 Type::make_builtin_struct_type(int nfields
, ...)
932 va_start(ap
, nfields
);
934 source_location bloc
= BUILTINS_LOCATION
;
935 Struct_field_list
* sfl
= new Struct_field_list();
936 for (int i
= 0; i
< nfields
; i
++)
938 const char* field_name
= va_arg(ap
, const char *);
939 Type
* type
= va_arg(ap
, Type
*);
940 sfl
->push_back(Struct_field(Typed_identifier(field_name
, type
, bloc
)));
945 return Type::make_struct_type(sfl
, bloc
);
948 // Make a builtin named type.
951 Type::make_builtin_named_type(const char* name
, Type
* type
)
953 source_location bloc
= BUILTINS_LOCATION
;
954 Named_object
* no
= Named_object::make_type(name
, NULL
, type
, bloc
);
955 return no
->type_value();
958 // Return the type of a type descriptor. We should really tie this to
959 // runtime.Type rather than copying it. This must match commonType in
960 // libgo/go/runtime/type.go.
963 Type::make_type_descriptor_type()
968 source_location bloc
= BUILTINS_LOCATION
;
970 Type
* uint8_type
= Type::lookup_integer_type("uint8");
971 Type
* uint32_type
= Type::lookup_integer_type("uint32");
972 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
973 Type
* string_type
= Type::lookup_string_type();
974 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
976 // This is an unnamed version of unsafe.Pointer. Perhaps we
977 // should use the named version instead, although that would
978 // require us to create the unsafe package if it has not been
979 // imported. It probably doesn't matter.
980 Type
* void_type
= Type::make_void_type();
981 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
983 // Forward declaration for the type descriptor type.
984 Named_object
* named_type_descriptor_type
=
985 Named_object::make_type_declaration("commonType", NULL
, bloc
);
986 Type
* ft
= Type::make_forward_declaration(named_type_descriptor_type
);
987 Type
* pointer_type_descriptor_type
= Type::make_pointer_type(ft
);
989 // The type of a method on a concrete type.
990 Struct_type
* method_type
=
991 Type::make_builtin_struct_type(5,
992 "name", pointer_string_type
,
993 "pkgPath", pointer_string_type
,
994 "mtyp", pointer_type_descriptor_type
,
995 "typ", pointer_type_descriptor_type
,
996 "tfn", unsafe_pointer_type
);
997 Named_type
* named_method_type
=
998 Type::make_builtin_named_type("method", method_type
);
1000 // Information for types with a name or methods.
1001 Type
* slice_named_method_type
=
1002 Type::make_array_type(named_method_type
, NULL
);
1003 Struct_type
* uncommon_type
=
1004 Type::make_builtin_struct_type(3,
1005 "name", pointer_string_type
,
1006 "pkgPath", pointer_string_type
,
1007 "methods", slice_named_method_type
);
1008 Named_type
* named_uncommon_type
=
1009 Type::make_builtin_named_type("uncommonType", uncommon_type
);
1011 Type
* pointer_uncommon_type
=
1012 Type::make_pointer_type(named_uncommon_type
);
1014 // The type descriptor type.
1016 Typed_identifier_list
* params
= new Typed_identifier_list();
1017 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1018 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1020 Typed_identifier_list
* results
= new Typed_identifier_list();
1021 results
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1023 Type
* hashfn_type
= Type::make_function_type(NULL
, params
, results
, bloc
);
1025 params
= new Typed_identifier_list();
1026 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1027 params
->push_back(Typed_identifier("", unsafe_pointer_type
, bloc
));
1028 params
->push_back(Typed_identifier("", uintptr_type
, bloc
));
1030 results
= new Typed_identifier_list();
1031 results
->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc
));
1033 Type
* equalfn_type
= Type::make_function_type(NULL
, params
, results
,
1036 Struct_type
* type_descriptor_type
=
1037 Type::make_builtin_struct_type(9,
1039 "align", uint8_type
,
1040 "fieldAlign", uint8_type
,
1041 "size", uintptr_type
,
1042 "hash", uint32_type
,
1043 "hashfn", hashfn_type
,
1044 "equalfn", equalfn_type
,
1045 "string", pointer_string_type
,
1046 "", pointer_uncommon_type
);
1048 Named_type
* named
= Type::make_builtin_named_type("commonType",
1049 type_descriptor_type
);
1051 named_type_descriptor_type
->set_type_value(named
);
1059 // Make the type of a pointer to a type descriptor as represented in
1063 Type::make_type_descriptor_ptr_type()
1067 ret
= Type::make_pointer_type(Type::make_type_descriptor_type());
1071 // Return the names of runtime functions which compute a hash code for
1072 // this type and which compare whether two values of this type are
1076 Type::type_functions(const char** hash_fn
, const char** equal_fn
) const
1078 switch (this->base()->classification())
1080 case Type::TYPE_ERROR
:
1081 case Type::TYPE_VOID
:
1082 case Type::TYPE_NIL
:
1083 // These types can not be hashed or compared.
1084 *hash_fn
= "__go_type_hash_error";
1085 *equal_fn
= "__go_type_equal_error";
1088 case Type::TYPE_BOOLEAN
:
1089 case Type::TYPE_INTEGER
:
1090 case Type::TYPE_FLOAT
:
1091 case Type::TYPE_COMPLEX
:
1092 case Type::TYPE_POINTER
:
1093 case Type::TYPE_FUNCTION
:
1094 case Type::TYPE_MAP
:
1095 case Type::TYPE_CHANNEL
:
1096 *hash_fn
= "__go_type_hash_identity";
1097 *equal_fn
= "__go_type_equal_identity";
1100 case Type::TYPE_STRING
:
1101 *hash_fn
= "__go_type_hash_string";
1102 *equal_fn
= "__go_type_equal_string";
1105 case Type::TYPE_STRUCT
:
1106 case Type::TYPE_ARRAY
:
1107 // These types can not be hashed or compared.
1108 *hash_fn
= "__go_type_hash_error";
1109 *equal_fn
= "__go_type_equal_error";
1112 case Type::TYPE_INTERFACE
:
1113 if (this->interface_type()->is_empty())
1115 *hash_fn
= "__go_type_hash_empty_interface";
1116 *equal_fn
= "__go_type_equal_empty_interface";
1120 *hash_fn
= "__go_type_hash_interface";
1121 *equal_fn
= "__go_type_equal_interface";
1125 case Type::TYPE_NAMED
:
1126 case Type::TYPE_FORWARD
:
1134 // Return a composite literal for the type descriptor for a plain type
1135 // of kind RUNTIME_TYPE_KIND named NAME.
1138 Type::type_descriptor_constructor(Gogo
* gogo
, int runtime_type_kind
,
1139 Named_type
* name
, const Methods
* methods
,
1140 bool only_value_methods
)
1142 source_location bloc
= BUILTINS_LOCATION
;
1144 Type
* td_type
= Type::make_type_descriptor_type();
1145 const Struct_field_list
* fields
= td_type
->struct_type()->fields();
1147 Expression_list
* vals
= new Expression_list();
1150 Struct_field_list::const_iterator p
= fields
->begin();
1151 gcc_assert(p
->field_name() == "Kind");
1153 mpz_init_set_ui(iv
, runtime_type_kind
);
1154 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1157 gcc_assert(p
->field_name() == "align");
1158 Expression::Type_info type_info
= Expression::TYPE_INFO_ALIGNMENT
;
1159 vals
->push_back(Expression::make_type_info(this, type_info
));
1162 gcc_assert(p
->field_name() == "fieldAlign");
1163 type_info
= Expression::TYPE_INFO_FIELD_ALIGNMENT
;
1164 vals
->push_back(Expression::make_type_info(this, type_info
));
1167 gcc_assert(p
->field_name() == "size");
1168 type_info
= Expression::TYPE_INFO_SIZE
;
1169 vals
->push_back(Expression::make_type_info(this, type_info
));
1172 gcc_assert(p
->field_name() == "hash");
1173 mpz_set_ui(iv
, this->hash_for_method(gogo
));
1174 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
1176 const char* hash_fn
;
1177 const char* equal_fn
;
1178 this->type_functions(&hash_fn
, &equal_fn
);
1181 gcc_assert(p
->field_name() == "hashfn");
1182 Function_type
* fntype
= p
->type()->function_type();
1183 Named_object
* no
= Named_object::make_function_declaration(hash_fn
, NULL
,
1186 no
->func_declaration_value()->set_asm_name(hash_fn
);
1187 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1190 gcc_assert(p
->field_name() == "equalfn");
1191 fntype
= p
->type()->function_type();
1192 no
= Named_object::make_function_declaration(equal_fn
, NULL
, fntype
, bloc
);
1193 no
->func_declaration_value()->set_asm_name(equal_fn
);
1194 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1197 gcc_assert(p
->field_name() == "string");
1198 Expression
* s
= Expression::make_string((name
!= NULL
1199 ? name
->reflection(gogo
)
1200 : this->reflection(gogo
)),
1202 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1205 gcc_assert(p
->field_name() == "uncommonType");
1206 if (name
== NULL
&& methods
== NULL
)
1207 vals
->push_back(Expression::make_nil(bloc
));
1210 if (methods
== NULL
)
1211 methods
= name
->methods();
1212 vals
->push_back(this->uncommon_type_constructor(gogo
,
1215 only_value_methods
));
1219 gcc_assert(p
== fields
->end());
1223 return Expression::make_struct_composite_literal(td_type
, vals
, bloc
);
1226 // Return a composite literal for the uncommon type information for
1227 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1228 // struct. If name is not NULL, it is the name of the type. If
1229 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1230 // is true if only value methods should be included. At least one of
1231 // NAME and METHODS must not be NULL.
1234 Type::uncommon_type_constructor(Gogo
* gogo
, Type
* uncommon_type
,
1235 Named_type
* name
, const Methods
* methods
,
1236 bool only_value_methods
) const
1238 source_location bloc
= BUILTINS_LOCATION
;
1240 const Struct_field_list
* fields
= uncommon_type
->struct_type()->fields();
1242 Expression_list
* vals
= new Expression_list();
1245 Struct_field_list::const_iterator p
= fields
->begin();
1246 gcc_assert(p
->field_name() == "name");
1249 gcc_assert(p
->field_name() == "pkgPath");
1253 vals
->push_back(Expression::make_nil(bloc
));
1254 vals
->push_back(Expression::make_nil(bloc
));
1258 Named_object
* no
= name
->named_object();
1259 std::string n
= Gogo::unpack_hidden_name(no
->name());
1260 Expression
* s
= Expression::make_string(n
, bloc
);
1261 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1263 if (name
->is_builtin())
1264 vals
->push_back(Expression::make_nil(bloc
));
1267 const Package
* package
= no
->package();
1268 const std::string
& unique_prefix(package
== NULL
1269 ? gogo
->unique_prefix()
1270 : package
->unique_prefix());
1271 const std::string
& package_name(package
== NULL
1272 ? gogo
->package_name()
1274 n
.assign(unique_prefix
);
1276 n
.append(package_name
);
1277 if (name
->in_function() != NULL
)
1280 n
.append(Gogo::unpack_hidden_name(name
->in_function()->name()));
1282 s
= Expression::make_string(n
, bloc
);
1283 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1288 gcc_assert(p
->field_name() == "methods");
1289 vals
->push_back(this->methods_constructor(gogo
, p
->type(), methods
,
1290 only_value_methods
));
1293 gcc_assert(p
== fields
->end());
1295 Expression
* r
= Expression::make_struct_composite_literal(uncommon_type
,
1297 return Expression::make_unary(OPERATOR_AND
, r
, bloc
);
1300 // Sort methods by name.
1306 operator()(const std::pair
<std::string
, const Method
*>& m1
,
1307 const std::pair
<std::string
, const Method
*>& m2
) const
1308 { return m1
.first
< m2
.first
; }
1311 // Return a composite literal for the type method table for this type.
1312 // METHODS_TYPE is the type of the table, and is a slice type.
1313 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1314 // then only value methods are used.
1317 Type::methods_constructor(Gogo
* gogo
, Type
* methods_type
,
1318 const Methods
* methods
,
1319 bool only_value_methods
) const
1321 source_location bloc
= BUILTINS_LOCATION
;
1323 std::vector
<std::pair
<std::string
, const Method
*> > smethods
;
1324 if (methods
!= NULL
)
1326 smethods
.reserve(methods
->count());
1327 for (Methods::const_iterator p
= methods
->begin();
1328 p
!= methods
->end();
1331 if (p
->second
->is_ambiguous())
1333 if (only_value_methods
&& !p
->second
->is_value_method())
1335 smethods
.push_back(std::make_pair(p
->first
, p
->second
));
1339 if (smethods
.empty())
1340 return Expression::make_slice_composite_literal(methods_type
, NULL
, bloc
);
1342 std::sort(smethods
.begin(), smethods
.end(), Sort_methods());
1344 Type
* method_type
= methods_type
->array_type()->element_type();
1346 Expression_list
* vals
= new Expression_list();
1347 vals
->reserve(smethods
.size());
1348 for (std::vector
<std::pair
<std::string
, const Method
*> >::const_iterator p
1350 p
!= smethods
.end();
1352 vals
->push_back(this->method_constructor(gogo
, method_type
, p
->first
,
1355 return Expression::make_slice_composite_literal(methods_type
, vals
, bloc
);
1358 // Return a composite literal for a single method. METHOD_TYPE is the
1359 // type of the entry. METHOD_NAME is the name of the method and M is
1360 // the method information.
1363 Type::method_constructor(Gogo
*, Type
* method_type
,
1364 const std::string
& method_name
,
1365 const Method
* m
) const
1367 source_location bloc
= BUILTINS_LOCATION
;
1369 const Struct_field_list
* fields
= method_type
->struct_type()->fields();
1371 Expression_list
* vals
= new Expression_list();
1374 Struct_field_list::const_iterator p
= fields
->begin();
1375 gcc_assert(p
->field_name() == "name");
1376 const std::string n
= Gogo::unpack_hidden_name(method_name
);
1377 Expression
* s
= Expression::make_string(n
, bloc
);
1378 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1381 gcc_assert(p
->field_name() == "pkgPath");
1382 if (!Gogo::is_hidden_name(method_name
))
1383 vals
->push_back(Expression::make_nil(bloc
));
1386 s
= Expression::make_string(Gogo::hidden_name_prefix(method_name
), bloc
);
1387 vals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
1390 Named_object
* no
= (m
->needs_stub_method()
1392 : m
->named_object());
1394 Function_type
* mtype
;
1395 if (no
->is_function())
1396 mtype
= no
->func_value()->type();
1398 mtype
= no
->func_declaration_value()->type();
1399 gcc_assert(mtype
->is_method());
1400 Type
* nonmethod_type
= mtype
->copy_without_receiver();
1403 gcc_assert(p
->field_name() == "mtyp");
1404 vals
->push_back(Expression::make_type_descriptor(nonmethod_type
, bloc
));
1407 gcc_assert(p
->field_name() == "typ");
1408 vals
->push_back(Expression::make_type_descriptor(mtype
, bloc
));
1411 gcc_assert(p
->field_name() == "tfn");
1412 vals
->push_back(Expression::make_func_reference(no
, NULL
, bloc
));
1415 gcc_assert(p
== fields
->end());
1417 return Expression::make_struct_composite_literal(method_type
, vals
, bloc
);
1420 // Return a composite literal for the type descriptor of a plain type.
1421 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1422 // NULL, it is the name to use as well as the list of methods.
1425 Type::plain_type_descriptor(Gogo
* gogo
, int runtime_type_kind
,
1428 return this->type_descriptor_constructor(gogo
, runtime_type_kind
,
1432 // Return the type reflection string for this type.
1435 Type::reflection(Gogo
* gogo
) const
1439 // The do_reflection virtual function should set RET to the
1440 // reflection string.
1441 this->do_reflection(gogo
, &ret
);
1446 // Return a mangled name for the type.
1449 Type::mangled_name(Gogo
* gogo
) const
1453 // The do_mangled_name virtual function should set RET to the
1454 // mangled name. For a composite type it should append a code for
1455 // the composition and then call do_mangled_name on the components.
1456 this->do_mangled_name(gogo
, &ret
);
1461 // Default function to export a type.
1464 Type::do_export(Export
*) const
1472 Type::import_type(Import
* imp
)
1474 if (imp
->match_c_string("("))
1475 return Function_type::do_import(imp
);
1476 else if (imp
->match_c_string("*"))
1477 return Pointer_type::do_import(imp
);
1478 else if (imp
->match_c_string("struct "))
1479 return Struct_type::do_import(imp
);
1480 else if (imp
->match_c_string("["))
1481 return Array_type::do_import(imp
);
1482 else if (imp
->match_c_string("map "))
1483 return Map_type::do_import(imp
);
1484 else if (imp
->match_c_string("chan "))
1485 return Channel_type::do_import(imp
);
1486 else if (imp
->match_c_string("interface"))
1487 return Interface_type::do_import(imp
);
1490 error_at(imp
->location(), "import error: expected type");
1491 return Type::make_error_type();
1495 // A type used to indicate a parsing error. This exists to simplify
1496 // later error detection.
1498 class Error_type
: public Type
1508 { return error_mark_node
; }
1511 do_get_init_tree(Gogo
*, tree
, bool)
1512 { return error_mark_node
; }
1515 do_type_descriptor(Gogo
*, Named_type
*)
1516 { return Expression::make_error(BUILTINS_LOCATION
); }
1519 do_reflection(Gogo
*, std::string
*) const
1520 { gcc_assert(saw_errors()); }
1523 do_mangled_name(Gogo
*, std::string
* ret
) const
1524 { ret
->push_back('E'); }
1528 Type::make_error_type()
1530 static Error_type singleton_error_type
;
1531 return &singleton_error_type
;
1536 class Void_type
: public Type
1546 { return void_type_node
; }
1549 do_get_init_tree(Gogo
*, tree
, bool)
1550 { gcc_unreachable(); }
1553 do_type_descriptor(Gogo
*, Named_type
*)
1554 { gcc_unreachable(); }
1557 do_reflection(Gogo
*, std::string
*) const
1561 do_mangled_name(Gogo
*, std::string
* ret
) const
1562 { ret
->push_back('v'); }
1566 Type::make_void_type()
1568 static Void_type singleton_void_type
;
1569 return &singleton_void_type
;
1572 // The boolean type.
1574 class Boolean_type
: public Type
1578 : Type(TYPE_BOOLEAN
)
1584 { return boolean_type_node
; }
1587 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1588 { return is_clear
? NULL
: fold_convert(type_tree
, boolean_false_node
); }
1591 do_type_descriptor(Gogo
*, Named_type
* name
);
1593 // We should not be asked for the reflection string of a basic type.
1595 do_reflection(Gogo
*, std::string
* ret
) const
1596 { ret
->append("bool"); }
1599 do_mangled_name(Gogo
*, std::string
* ret
) const
1600 { ret
->push_back('b'); }
1603 // Make the type descriptor.
1606 Boolean_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1609 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_BOOL
, name
);
1612 Named_object
* no
= gogo
->lookup_global("bool");
1613 gcc_assert(no
!= NULL
);
1614 return Type::type_descriptor(gogo
, no
->type_value());
1619 Type::make_boolean_type()
1621 static Boolean_type boolean_type
;
1622 return &boolean_type
;
1625 // The named type "bool".
1627 static Named_type
* named_bool_type
;
1629 // Get the named type "bool".
1632 Type::lookup_bool_type()
1634 return named_bool_type
;
1637 // Make the named type "bool".
1640 Type::make_named_bool_type()
1642 Type
* bool_type
= Type::make_boolean_type();
1643 Named_object
* named_object
= Named_object::make_type("bool", NULL
,
1646 Named_type
* named_type
= named_object
->type_value();
1647 named_bool_type
= named_type
;
1651 // Class Integer_type.
1653 Integer_type::Named_integer_types
Integer_type::named_integer_types
;
1655 // Create a new integer type. Non-abstract integer types always have
1659 Integer_type::create_integer_type(const char* name
, bool is_unsigned
,
1660 int bits
, int runtime_type_kind
)
1662 Integer_type
* integer_type
= new Integer_type(false, is_unsigned
, bits
,
1664 std::string
sname(name
);
1665 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1668 Named_type
* named_type
= named_object
->type_value();
1669 std::pair
<Named_integer_types::iterator
, bool> ins
=
1670 Integer_type::named_integer_types
.insert(std::make_pair(sname
, named_type
));
1671 gcc_assert(ins
.second
);
1675 // Look up an existing integer type.
1678 Integer_type::lookup_integer_type(const char* name
)
1680 Named_integer_types::const_iterator p
=
1681 Integer_type::named_integer_types
.find(name
);
1682 gcc_assert(p
!= Integer_type::named_integer_types
.end());
1686 // Create a new abstract integer type.
1689 Integer_type::create_abstract_integer_type()
1691 static Integer_type
* abstract_type
;
1692 if (abstract_type
== NULL
)
1693 abstract_type
= new Integer_type(true, false, INT_TYPE_SIZE
,
1694 RUNTIME_TYPE_KIND_INT
);
1695 return abstract_type
;
1698 // Integer type compatibility.
1701 Integer_type::is_identical(const Integer_type
* t
) const
1703 if (this->is_unsigned_
!= t
->is_unsigned_
|| this->bits_
!= t
->bits_
)
1705 return this->is_abstract_
== t
->is_abstract_
;
1711 Integer_type::do_hash_for_method(Gogo
*) const
1713 return ((this->bits_
<< 4)
1714 + ((this->is_unsigned_
? 1 : 0) << 8)
1715 + ((this->is_abstract_
? 1 : 0) << 9));
1718 // Get the tree for an Integer_type.
1721 Integer_type::do_get_tree(Gogo
*)
1723 gcc_assert(!this->is_abstract_
);
1724 if (this->is_unsigned_
)
1726 if (this->bits_
== INT_TYPE_SIZE
)
1727 return unsigned_type_node
;
1728 else if (this->bits_
== CHAR_TYPE_SIZE
)
1729 return unsigned_char_type_node
;
1730 else if (this->bits_
== SHORT_TYPE_SIZE
)
1731 return short_unsigned_type_node
;
1732 else if (this->bits_
== LONG_TYPE_SIZE
)
1733 return long_unsigned_type_node
;
1734 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1735 return long_long_unsigned_type_node
;
1737 return make_unsigned_type(this->bits_
);
1741 if (this->bits_
== INT_TYPE_SIZE
)
1742 return integer_type_node
;
1743 else if (this->bits_
== CHAR_TYPE_SIZE
)
1744 return signed_char_type_node
;
1745 else if (this->bits_
== SHORT_TYPE_SIZE
)
1746 return short_integer_type_node
;
1747 else if (this->bits_
== LONG_TYPE_SIZE
)
1748 return long_integer_type_node
;
1749 else if (this->bits_
== LONG_LONG_TYPE_SIZE
)
1750 return long_long_integer_type_node
;
1752 return make_signed_type(this->bits_
);
1757 Integer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1759 return is_clear
? NULL
: build_int_cst(type_tree
, 0);
1762 // The type descriptor for an integer type. Integer types are always
1766 Integer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1768 gcc_assert(name
!= NULL
);
1769 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1772 // We should not be asked for the reflection string of a basic type.
1775 Integer_type::do_reflection(Gogo
*, std::string
*) const
1783 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1786 snprintf(buf
, sizeof buf
, "i%s%s%de",
1787 this->is_abstract_
? "a" : "",
1788 this->is_unsigned_
? "u" : "",
1793 // Make an integer type.
1796 Type::make_integer_type(const char* name
, bool is_unsigned
, int bits
,
1797 int runtime_type_kind
)
1799 return Integer_type::create_integer_type(name
, is_unsigned
, bits
,
1803 // Make an abstract integer type.
1806 Type::make_abstract_integer_type()
1808 return Integer_type::create_abstract_integer_type();
1811 // Look up an integer type.
1814 Type::lookup_integer_type(const char* name
)
1816 return Integer_type::lookup_integer_type(name
);
1819 // Class Float_type.
1821 Float_type::Named_float_types
Float_type::named_float_types
;
1823 // Create a new float type. Non-abstract float types always have
1827 Float_type::create_float_type(const char* name
, int bits
,
1828 int runtime_type_kind
)
1830 Float_type
* float_type
= new Float_type(false, bits
, runtime_type_kind
);
1831 std::string
sname(name
);
1832 Named_object
* named_object
= Named_object::make_type(sname
, NULL
, float_type
,
1834 Named_type
* named_type
= named_object
->type_value();
1835 std::pair
<Named_float_types::iterator
, bool> ins
=
1836 Float_type::named_float_types
.insert(std::make_pair(sname
, named_type
));
1837 gcc_assert(ins
.second
);
1841 // Look up an existing float type.
1844 Float_type::lookup_float_type(const char* name
)
1846 Named_float_types::const_iterator p
=
1847 Float_type::named_float_types
.find(name
);
1848 gcc_assert(p
!= Float_type::named_float_types
.end());
1852 // Create a new abstract float type.
1855 Float_type::create_abstract_float_type()
1857 static Float_type
* abstract_type
;
1858 if (abstract_type
== NULL
)
1859 abstract_type
= new Float_type(true, FLOAT_TYPE_SIZE
,
1860 RUNTIME_TYPE_KIND_FLOAT
);
1861 return abstract_type
;
1864 // Whether this type is identical with T.
1867 Float_type::is_identical(const Float_type
* t
) const
1869 if (this->bits_
!= t
->bits_
)
1871 return this->is_abstract_
== t
->is_abstract_
;
1877 Float_type::do_hash_for_method(Gogo
*) const
1879 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
1882 // Get a tree without using a Gogo*.
1885 Float_type::type_tree() const
1887 if (this->bits_
== FLOAT_TYPE_SIZE
)
1888 return float_type_node
;
1889 else if (this->bits_
== DOUBLE_TYPE_SIZE
)
1890 return double_type_node
;
1891 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
)
1892 return long_double_type_node
;
1895 tree ret
= make_node(REAL_TYPE
);
1896 TYPE_PRECISION(ret
) = this->bits_
;
1905 Float_type::do_get_tree(Gogo
*)
1907 return this->type_tree();
1911 Float_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
1916 real_from_integer(&r
, TYPE_MODE(type_tree
), 0, 0, 0);
1917 return build_real(type_tree
, r
);
1920 // The type descriptor for a float type. Float types are always named.
1923 Float_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
1925 gcc_assert(name
!= NULL
);
1926 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
1929 // We should not be asked for the reflection string of a basic type.
1932 Float_type::do_reflection(Gogo
*, std::string
*) const
1940 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
1943 snprintf(buf
, sizeof buf
, "f%s%de",
1944 this->is_abstract_
? "a" : "",
1949 // Make a floating point type.
1952 Type::make_float_type(const char* name
, int bits
, int runtime_type_kind
)
1954 return Float_type::create_float_type(name
, bits
, runtime_type_kind
);
1957 // Make an abstract float type.
1960 Type::make_abstract_float_type()
1962 return Float_type::create_abstract_float_type();
1965 // Look up a float type.
1968 Type::lookup_float_type(const char* name
)
1970 return Float_type::lookup_float_type(name
);
1973 // Class Complex_type.
1975 Complex_type::Named_complex_types
Complex_type::named_complex_types
;
1977 // Create a new complex type. Non-abstract complex types always have
1981 Complex_type::create_complex_type(const char* name
, int bits
,
1982 int runtime_type_kind
)
1984 Complex_type
* complex_type
= new Complex_type(false, bits
,
1986 std::string
sname(name
);
1987 Named_object
* named_object
= Named_object::make_type(sname
, NULL
,
1990 Named_type
* named_type
= named_object
->type_value();
1991 std::pair
<Named_complex_types::iterator
, bool> ins
=
1992 Complex_type::named_complex_types
.insert(std::make_pair(sname
,
1994 gcc_assert(ins
.second
);
1998 // Look up an existing complex type.
2001 Complex_type::lookup_complex_type(const char* name
)
2003 Named_complex_types::const_iterator p
=
2004 Complex_type::named_complex_types
.find(name
);
2005 gcc_assert(p
!= Complex_type::named_complex_types
.end());
2009 // Create a new abstract complex type.
2012 Complex_type::create_abstract_complex_type()
2014 static Complex_type
* abstract_type
;
2015 if (abstract_type
== NULL
)
2016 abstract_type
= new Complex_type(true, FLOAT_TYPE_SIZE
* 2,
2017 RUNTIME_TYPE_KIND_FLOAT
);
2018 return abstract_type
;
2021 // Whether this type is identical with T.
2024 Complex_type::is_identical(const Complex_type
*t
) const
2026 if (this->bits_
!= t
->bits_
)
2028 return this->is_abstract_
== t
->is_abstract_
;
2034 Complex_type::do_hash_for_method(Gogo
*) const
2036 return (this->bits_
<< 4) + ((this->is_abstract_
? 1 : 0) << 8);
2039 // Get a tree without using a Gogo*.
2042 Complex_type::type_tree() const
2044 if (this->bits_
== FLOAT_TYPE_SIZE
* 2)
2045 return complex_float_type_node
;
2046 else if (this->bits_
== DOUBLE_TYPE_SIZE
* 2)
2047 return complex_double_type_node
;
2048 else if (this->bits_
== LONG_DOUBLE_TYPE_SIZE
* 2)
2049 return complex_long_double_type_node
;
2052 tree ret
= make_node(REAL_TYPE
);
2053 TYPE_PRECISION(ret
) = this->bits_
/ 2;
2055 return build_complex_type(ret
);
2062 Complex_type::do_get_tree(Gogo
*)
2064 return this->type_tree();
2067 // Zero initializer.
2070 Complex_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2075 real_from_integer(&r
, TYPE_MODE(TREE_TYPE(type_tree
)), 0, 0, 0);
2076 return build_complex(type_tree
, build_real(TREE_TYPE(type_tree
), r
),
2077 build_real(TREE_TYPE(type_tree
), r
));
2080 // The type descriptor for a complex type. Complex types are always
2084 Complex_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2086 gcc_assert(name
!= NULL
);
2087 return this->plain_type_descriptor(gogo
, this->runtime_type_kind_
, name
);
2090 // We should not be asked for the reflection string of a basic type.
2093 Complex_type::do_reflection(Gogo
*, std::string
*) const
2101 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2104 snprintf(buf
, sizeof buf
, "c%s%de",
2105 this->is_abstract_
? "a" : "",
2110 // Make a complex type.
2113 Type::make_complex_type(const char* name
, int bits
, int runtime_type_kind
)
2115 return Complex_type::create_complex_type(name
, bits
, runtime_type_kind
);
2118 // Make an abstract complex type.
2121 Type::make_abstract_complex_type()
2123 return Complex_type::create_abstract_complex_type();
2126 // Look up a complex type.
2129 Type::lookup_complex_type(const char* name
)
2131 return Complex_type::lookup_complex_type(name
);
2134 // Class String_type.
2136 // Return the tree for String_type. A string is a struct with two
2137 // fields: a pointer to the characters and a length.
2140 String_type::do_get_tree(Gogo
*)
2142 static tree struct_type
;
2143 return Gogo::builtin_struct(&struct_type
, "__go_string", NULL_TREE
, 2,
2145 build_pointer_type(unsigned_char_type_node
),
2150 // Return a tree for the length of STRING.
2153 String_type::length_tree(Gogo
*, tree string
)
2155 tree string_type
= TREE_TYPE(string
);
2156 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2157 tree length_field
= DECL_CHAIN(TYPE_FIELDS(string_type
));
2158 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field
)),
2160 return fold_build3(COMPONENT_REF
, integer_type_node
, string
,
2161 length_field
, NULL_TREE
);
2164 // Return a tree for a pointer to the bytes of STRING.
2167 String_type::bytes_tree(Gogo
*, tree string
)
2169 tree string_type
= TREE_TYPE(string
);
2170 gcc_assert(TREE_CODE(string_type
) == RECORD_TYPE
);
2171 tree bytes_field
= TYPE_FIELDS(string_type
);
2172 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field
)),
2174 return fold_build3(COMPONENT_REF
, TREE_TYPE(bytes_field
), string
,
2175 bytes_field
, NULL_TREE
);
2178 // We initialize a string to { NULL, 0 }.
2181 String_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2186 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2188 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2190 for (tree field
= TYPE_FIELDS(type_tree
);
2192 field
= DECL_CHAIN(field
))
2194 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2196 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
2199 tree ret
= build_constructor(type_tree
, init
);
2200 TREE_CONSTANT(ret
) = 1;
2204 // The type descriptor for the string type.
2207 String_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2210 return this->plain_type_descriptor(gogo
, RUNTIME_TYPE_KIND_STRING
, name
);
2213 Named_object
* no
= gogo
->lookup_global("string");
2214 gcc_assert(no
!= NULL
);
2215 return Type::type_descriptor(gogo
, no
->type_value());
2219 // We should not be asked for the reflection string of a basic type.
2222 String_type::do_reflection(Gogo
*, std::string
* ret
) const
2224 ret
->append("string");
2227 // Mangled name of a string type.
2230 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
2232 ret
->push_back('z');
2235 // Make a string type.
2238 Type::make_string_type()
2240 static String_type string_type
;
2241 return &string_type
;
2244 // The named type "string".
2246 static Named_type
* named_string_type
;
2248 // Get the named type "string".
2251 Type::lookup_string_type()
2253 return named_string_type
;
2256 // Make the named type string.
2259 Type::make_named_string_type()
2261 Type
* string_type
= Type::make_string_type();
2262 Named_object
* named_object
= Named_object::make_type("string", NULL
,
2265 Named_type
* named_type
= named_object
->type_value();
2266 named_string_type
= named_type
;
2270 // The sink type. This is the type of the blank identifier _. Any
2271 // type may be assigned to it.
2273 class Sink_type
: public Type
2283 { gcc_unreachable(); }
2286 do_get_init_tree(Gogo
*, tree
, bool)
2287 { gcc_unreachable(); }
2290 do_type_descriptor(Gogo
*, Named_type
*)
2291 { gcc_unreachable(); }
2294 do_reflection(Gogo
*, std::string
*) const
2295 { gcc_unreachable(); }
2298 do_mangled_name(Gogo
*, std::string
*) const
2299 { gcc_unreachable(); }
2302 // Make the sink type.
2305 Type::make_sink_type()
2307 static Sink_type sink_type
;
2311 // Class Function_type.
2316 Function_type::do_traverse(Traverse
* traverse
)
2318 if (this->receiver_
!= NULL
2319 && Type::traverse(this->receiver_
->type(), traverse
) == TRAVERSE_EXIT
)
2320 return TRAVERSE_EXIT
;
2321 if (this->parameters_
!= NULL
2322 && this->parameters_
->traverse(traverse
) == TRAVERSE_EXIT
)
2323 return TRAVERSE_EXIT
;
2324 if (this->results_
!= NULL
2325 && this->results_
->traverse(traverse
) == TRAVERSE_EXIT
)
2326 return TRAVERSE_EXIT
;
2327 return TRAVERSE_CONTINUE
;
2330 // Returns whether T is a valid redeclaration of this type. If this
2331 // returns false, and REASON is not NULL, *REASON may be set to a
2332 // brief explanation of why it returned false.
2335 Function_type::is_valid_redeclaration(const Function_type
* t
,
2336 std::string
* reason
) const
2338 if (!this->is_identical(t
, false, true, reason
))
2341 // A redeclaration of a function is required to use the same names
2342 // for the receiver and parameters.
2343 if (this->receiver() != NULL
2344 && this->receiver()->name() != t
->receiver()->name()
2345 && this->receiver()->name() != Import::import_marker
2346 && t
->receiver()->name() != Import::import_marker
)
2349 *reason
= "receiver name changed";
2353 const Typed_identifier_list
* parms1
= this->parameters();
2354 const Typed_identifier_list
* parms2
= t
->parameters();
2357 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2358 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2359 p2
!= parms2
->end();
2362 if (p1
->name() != p2
->name()
2363 && p1
->name() != Import::import_marker
2364 && p2
->name() != Import::import_marker
)
2367 *reason
= "parameter name changed";
2371 // This is called at parse time, so we may have unknown
2373 Type
* t1
= p1
->type()->forwarded();
2374 Type
* t2
= p2
->type()->forwarded();
2376 && t1
->forward_declaration_type() != NULL
2377 && (t2
->forward_declaration_type() == NULL
2378 || (t1
->forward_declaration_type()->named_object()
2379 != t2
->forward_declaration_type()->named_object())))
2384 const Typed_identifier_list
* results1
= this->results();
2385 const Typed_identifier_list
* results2
= t
->results();
2386 if (results1
!= NULL
)
2388 Typed_identifier_list::const_iterator res1
= results1
->begin();
2389 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2390 res2
!= results2
->end();
2393 if (res1
->name() != res2
->name()
2394 && res1
->name() != Import::import_marker
2395 && res2
->name() != Import::import_marker
)
2398 *reason
= "result name changed";
2402 // This is called at parse time, so we may have unknown
2404 Type
* t1
= res1
->type()->forwarded();
2405 Type
* t2
= res2
->type()->forwarded();
2407 && t1
->forward_declaration_type() != NULL
2408 && (t2
->forward_declaration_type() == NULL
2409 || (t1
->forward_declaration_type()->named_object()
2410 != t2
->forward_declaration_type()->named_object())))
2418 // Check whether T is the same as this type.
2421 Function_type::is_identical(const Function_type
* t
, bool ignore_receiver
,
2422 bool errors_are_identical
,
2423 std::string
* reason
) const
2425 if (!ignore_receiver
)
2427 const Typed_identifier
* r1
= this->receiver();
2428 const Typed_identifier
* r2
= t
->receiver();
2429 if ((r1
!= NULL
) != (r2
!= NULL
))
2432 *reason
= _("different receiver types");
2437 if (!Type::are_identical(r1
->type(), r2
->type(), errors_are_identical
,
2440 if (reason
!= NULL
&& !reason
->empty())
2441 *reason
= "receiver: " + *reason
;
2447 const Typed_identifier_list
* parms1
= this->parameters();
2448 const Typed_identifier_list
* parms2
= t
->parameters();
2449 if ((parms1
!= NULL
) != (parms2
!= NULL
))
2452 *reason
= _("different number of parameters");
2457 Typed_identifier_list::const_iterator p1
= parms1
->begin();
2458 for (Typed_identifier_list::const_iterator p2
= parms2
->begin();
2459 p2
!= parms2
->end();
2462 if (p1
== parms1
->end())
2465 *reason
= _("different number of parameters");
2469 if (!Type::are_identical(p1
->type(), p2
->type(),
2470 errors_are_identical
, NULL
))
2473 *reason
= _("different parameter types");
2477 if (p1
!= parms1
->end())
2480 *reason
= _("different number of parameters");
2485 if (this->is_varargs() != t
->is_varargs())
2488 *reason
= _("different varargs");
2492 const Typed_identifier_list
* results1
= this->results();
2493 const Typed_identifier_list
* results2
= t
->results();
2494 if ((results1
!= NULL
) != (results2
!= NULL
))
2497 *reason
= _("different number of results");
2500 if (results1
!= NULL
)
2502 Typed_identifier_list::const_iterator res1
= results1
->begin();
2503 for (Typed_identifier_list::const_iterator res2
= results2
->begin();
2504 res2
!= results2
->end();
2507 if (res1
== results1
->end())
2510 *reason
= _("different number of results");
2514 if (!Type::are_identical(res1
->type(), res2
->type(),
2515 errors_are_identical
, NULL
))
2518 *reason
= _("different result types");
2522 if (res1
!= results1
->end())
2525 *reason
= _("different number of results");
2536 Function_type::do_hash_for_method(Gogo
* gogo
) const
2538 unsigned int ret
= 0;
2539 // We ignore the receiver type for hash codes, because we need to
2540 // get the same hash code for a method in an interface and a method
2541 // declared for a type. The former will not have a receiver.
2542 if (this->parameters_
!= NULL
)
2545 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2546 p
!= this->parameters_
->end();
2548 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2550 if (this->results_
!= NULL
)
2553 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2554 p
!= this->results_
->end();
2556 ret
+= p
->type()->hash_for_method(gogo
) << shift
;
2558 if (this->is_varargs_
)
2564 // Get the tree for a function type.
2567 Function_type::do_get_tree(Gogo
* gogo
)
2569 tree args
= NULL_TREE
;
2572 if (this->receiver_
!= NULL
)
2574 Type
* rtype
= this->receiver_
->type();
2575 tree ptype
= rtype
->get_tree(gogo
);
2576 if (ptype
== error_mark_node
)
2577 return error_mark_node
;
2579 // We always pass the address of the receiver parameter, in
2580 // order to make interface calls work with unknown types.
2581 if (rtype
->points_to() == NULL
)
2582 ptype
= build_pointer_type(ptype
);
2584 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2585 pp
= &TREE_CHAIN (*pp
);
2588 if (this->parameters_
!= NULL
)
2590 for (Typed_identifier_list::const_iterator p
= this->parameters_
->begin();
2591 p
!= this->parameters_
->end();
2594 tree ptype
= p
->type()->get_tree(gogo
);
2595 if (ptype
== error_mark_node
)
2596 return error_mark_node
;
2597 *pp
= tree_cons (NULL_TREE
, ptype
, NULL_TREE
);
2598 pp
= &TREE_CHAIN (*pp
);
2602 // Varargs is handled entirely at the Go level. At the tree level,
2603 // functions are not varargs.
2604 *pp
= void_list_node
;
2607 if (this->results_
== NULL
)
2608 result
= void_type_node
;
2609 else if (this->results_
->size() == 1)
2610 result
= this->results_
->begin()->type()->get_tree(gogo
);
2613 result
= make_node(RECORD_TYPE
);
2614 tree field_trees
= NULL_TREE
;
2615 tree
* pp
= &field_trees
;
2616 for (Typed_identifier_list::const_iterator p
= this->results_
->begin();
2617 p
!= this->results_
->end();
2620 const std::string name
= (p
->name().empty()
2622 : Gogo::unpack_hidden_name(p
->name()));
2623 tree name_tree
= get_identifier_with_length(name
.data(),
2625 tree field_type_tree
= p
->type()->get_tree(gogo
);
2626 if (field_type_tree
== error_mark_node
)
2627 return error_mark_node
;
2628 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
,
2630 DECL_CONTEXT(field
) = result
;
2632 pp
= &DECL_CHAIN(field
);
2634 TYPE_FIELDS(result
) = field_trees
;
2635 layout_type(result
);
2638 if (result
== error_mark_node
)
2639 return error_mark_node
;
2641 tree fntype
= build_function_type(result
, args
);
2642 if (fntype
== error_mark_node
)
2645 return build_pointer_type(fntype
);
2648 // Functions are initialized to NULL.
2651 Function_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
2655 return fold_convert(type_tree
, null_pointer_node
);
2658 // The type of a function type descriptor.
2661 Function_type::make_function_type_descriptor_type()
2666 Type
* tdt
= Type::make_type_descriptor_type();
2667 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
2669 Type
* bool_type
= Type::lookup_bool_type();
2671 Type
* slice_type
= Type::make_array_type(ptdt
, NULL
);
2673 Struct_type
* s
= Type::make_builtin_struct_type(4,
2675 "dotdotdot", bool_type
,
2679 ret
= Type::make_builtin_named_type("FuncType", s
);
2685 // The type descriptor for a function type.
2688 Function_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
2690 source_location bloc
= BUILTINS_LOCATION
;
2692 Type
* ftdt
= Function_type::make_function_type_descriptor_type();
2694 const Struct_field_list
* fields
= ftdt
->struct_type()->fields();
2696 Expression_list
* vals
= new Expression_list();
2699 Struct_field_list::const_iterator p
= fields
->begin();
2700 gcc_assert(p
->field_name() == "commonType");
2701 vals
->push_back(this->type_descriptor_constructor(gogo
,
2702 RUNTIME_TYPE_KIND_FUNC
,
2706 gcc_assert(p
->field_name() == "dotdotdot");
2707 vals
->push_back(Expression::make_boolean(this->is_varargs(), bloc
));
2710 gcc_assert(p
->field_name() == "in");
2711 vals
->push_back(this->type_descriptor_params(p
->type(), this->receiver(),
2712 this->parameters()));
2715 gcc_assert(p
->field_name() == "out");
2716 vals
->push_back(this->type_descriptor_params(p
->type(), NULL
,
2720 gcc_assert(p
== fields
->end());
2722 return Expression::make_struct_composite_literal(ftdt
, vals
, bloc
);
2725 // Return a composite literal for the parameters or results of a type
2729 Function_type::type_descriptor_params(Type
* params_type
,
2730 const Typed_identifier
* receiver
,
2731 const Typed_identifier_list
* params
)
2733 source_location bloc
= BUILTINS_LOCATION
;
2735 if (receiver
== NULL
&& params
== NULL
)
2736 return Expression::make_slice_composite_literal(params_type
, NULL
, bloc
);
2738 Expression_list
* vals
= new Expression_list();
2739 vals
->reserve((params
== NULL
? 0 : params
->size())
2740 + (receiver
!= NULL
? 1 : 0));
2742 if (receiver
!= NULL
)
2744 Type
* rtype
= receiver
->type();
2745 // The receiver is always passed as a pointer. FIXME: Is this
2746 // right? Should that fact affect the type descriptor?
2747 if (rtype
->points_to() == NULL
)
2748 rtype
= Type::make_pointer_type(rtype
);
2749 vals
->push_back(Expression::make_type_descriptor(rtype
, bloc
));
2754 for (Typed_identifier_list::const_iterator p
= params
->begin();
2757 vals
->push_back(Expression::make_type_descriptor(p
->type(), bloc
));
2760 return Expression::make_slice_composite_literal(params_type
, vals
, bloc
);
2763 // The reflection string.
2766 Function_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
2768 // FIXME: Turn this off until we straighten out the type of the
2769 // struct field used in a go statement which calls a method.
2770 // gcc_assert(this->receiver_ == NULL);
2772 ret
->append("func");
2774 if (this->receiver_
!= NULL
)
2776 ret
->push_back('(');
2777 this->append_reflection(this->receiver_
->type(), gogo
, ret
);
2778 ret
->push_back(')');
2781 ret
->push_back('(');
2782 const Typed_identifier_list
* params
= this->parameters();
2785 bool is_varargs
= this->is_varargs_
;
2786 for (Typed_identifier_list::const_iterator p
= params
->begin();
2790 if (p
!= params
->begin())
2792 if (!is_varargs
|| p
+ 1 != params
->end())
2793 this->append_reflection(p
->type(), gogo
, ret
);
2797 this->append_reflection(p
->type()->array_type()->element_type(),
2802 ret
->push_back(')');
2804 const Typed_identifier_list
* results
= this->results();
2805 if (results
!= NULL
&& !results
->empty())
2807 if (results
->size() == 1)
2808 ret
->push_back(' ');
2811 for (Typed_identifier_list::const_iterator p
= results
->begin();
2812 p
!= results
->end();
2815 if (p
!= results
->begin())
2817 this->append_reflection(p
->type(), gogo
, ret
);
2819 if (results
->size() > 1)
2820 ret
->push_back(')');
2827 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
2829 ret
->push_back('F');
2831 if (this->receiver_
!= NULL
)
2833 ret
->push_back('m');
2834 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
2837 const Typed_identifier_list
* params
= this->parameters();
2840 ret
->push_back('p');
2841 for (Typed_identifier_list::const_iterator p
= params
->begin();
2844 this->append_mangled_name(p
->type(), gogo
, ret
);
2845 if (this->is_varargs_
)
2846 ret
->push_back('V');
2847 ret
->push_back('e');
2850 const Typed_identifier_list
* results
= this->results();
2851 if (results
!= NULL
)
2853 ret
->push_back('r');
2854 for (Typed_identifier_list::const_iterator p
= results
->begin();
2855 p
!= results
->end();
2857 this->append_mangled_name(p
->type(), gogo
, ret
);
2858 ret
->push_back('e');
2861 ret
->push_back('e');
2864 // Export a function type.
2867 Function_type::do_export(Export
* exp
) const
2869 // We don't write out the receiver. The only function types which
2870 // should have a receiver are the ones associated with explicitly
2871 // defined methods. For those the receiver type is written out by
2872 // Function::export_func.
2874 exp
->write_c_string("(");
2876 if (this->parameters_
!= NULL
)
2878 bool is_varargs
= this->is_varargs_
;
2879 for (Typed_identifier_list::const_iterator p
=
2880 this->parameters_
->begin();
2881 p
!= this->parameters_
->end();
2887 exp
->write_c_string(", ");
2888 if (!is_varargs
|| p
+ 1 != this->parameters_
->end())
2889 exp
->write_type(p
->type());
2892 exp
->write_c_string("...");
2893 exp
->write_type(p
->type()->array_type()->element_type());
2897 exp
->write_c_string(")");
2899 const Typed_identifier_list
* results
= this->results_
;
2900 if (results
!= NULL
)
2902 exp
->write_c_string(" ");
2903 if (results
->size() == 1)
2904 exp
->write_type(results
->begin()->type());
2908 exp
->write_c_string("(");
2909 for (Typed_identifier_list::const_iterator p
= results
->begin();
2910 p
!= results
->end();
2916 exp
->write_c_string(", ");
2917 exp
->write_type(p
->type());
2919 exp
->write_c_string(")");
2924 // Import a function type.
2927 Function_type::do_import(Import
* imp
)
2929 imp
->require_c_string("(");
2930 Typed_identifier_list
* parameters
;
2931 bool is_varargs
= false;
2932 if (imp
->peek_char() == ')')
2936 parameters
= new Typed_identifier_list();
2939 if (imp
->match_c_string("..."))
2945 Type
* ptype
= imp
->read_type();
2947 ptype
= Type::make_array_type(ptype
, NULL
);
2948 parameters
->push_back(Typed_identifier(Import::import_marker
,
2949 ptype
, imp
->location()));
2950 if (imp
->peek_char() != ',')
2952 gcc_assert(!is_varargs
);
2953 imp
->require_c_string(", ");
2956 imp
->require_c_string(")");
2958 Typed_identifier_list
* results
;
2959 if (imp
->peek_char() != ' ')
2964 results
= new Typed_identifier_list
;
2965 if (imp
->peek_char() != '(')
2967 Type
* rtype
= imp
->read_type();
2968 results
->push_back(Typed_identifier(Import::import_marker
, rtype
,
2976 Type
* rtype
= imp
->read_type();
2977 results
->push_back(Typed_identifier(Import::import_marker
,
2978 rtype
, imp
->location()));
2979 if (imp
->peek_char() != ',')
2981 imp
->require_c_string(", ");
2983 imp
->require_c_string(")");
2987 Function_type
* ret
= Type::make_function_type(NULL
, parameters
, results
,
2990 ret
->set_is_varargs();
2994 // Make a copy of a function type without a receiver.
2997 Function_type::copy_without_receiver() const
2999 gcc_assert(this->is_method());
3000 Function_type
*ret
= Type::make_function_type(NULL
, this->parameters_
,
3003 if (this->is_varargs())
3004 ret
->set_is_varargs();
3005 if (this->is_builtin())
3006 ret
->set_is_builtin();
3010 // Make a copy of a function type with a receiver.
3013 Function_type::copy_with_receiver(Type
* receiver_type
) const
3015 gcc_assert(!this->is_method());
3016 Typed_identifier
* receiver
= new Typed_identifier("", receiver_type
,
3018 return Type::make_function_type(receiver
, this->parameters_
,
3019 this->results_
, this->location_
);
3022 // Make a function type.
3025 Type::make_function_type(Typed_identifier
* receiver
,
3026 Typed_identifier_list
* parameters
,
3027 Typed_identifier_list
* results
,
3028 source_location location
)
3030 return new Function_type(receiver
, parameters
, results
, location
);
3033 // Class Pointer_type.
3038 Pointer_type::do_traverse(Traverse
* traverse
)
3040 return Type::traverse(this->to_type_
, traverse
);
3046 Pointer_type::do_hash_for_method(Gogo
* gogo
) const
3048 return this->to_type_
->hash_for_method(gogo
) << 4;
3051 // The tree for a pointer type.
3054 Pointer_type::do_get_tree(Gogo
* gogo
)
3056 return build_pointer_type(this->to_type_
->get_tree(gogo
));
3059 // Initialize a pointer type.
3062 Pointer_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3066 return fold_convert(type_tree
, null_pointer_node
);
3069 // The type of a pointer type descriptor.
3072 Pointer_type::make_pointer_type_descriptor_type()
3077 Type
* tdt
= Type::make_type_descriptor_type();
3078 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3080 Struct_type
* s
= Type::make_builtin_struct_type(2,
3084 ret
= Type::make_builtin_named_type("PtrType", s
);
3090 // The type descriptor for a pointer type.
3093 Pointer_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3095 if (this->is_unsafe_pointer_type())
3097 gcc_assert(name
!= NULL
);
3098 return this->plain_type_descriptor(gogo
,
3099 RUNTIME_TYPE_KIND_UNSAFE_POINTER
,
3104 source_location bloc
= BUILTINS_LOCATION
;
3106 const Methods
* methods
;
3107 Type
* deref
= this->points_to();
3108 if (deref
->named_type() != NULL
)
3109 methods
= deref
->named_type()->methods();
3110 else if (deref
->struct_type() != NULL
)
3111 methods
= deref
->struct_type()->methods();
3115 Type
* ptr_tdt
= Pointer_type::make_pointer_type_descriptor_type();
3117 const Struct_field_list
* fields
= ptr_tdt
->struct_type()->fields();
3119 Expression_list
* vals
= new Expression_list();
3122 Struct_field_list::const_iterator p
= fields
->begin();
3123 gcc_assert(p
->field_name() == "commonType");
3124 vals
->push_back(this->type_descriptor_constructor(gogo
,
3125 RUNTIME_TYPE_KIND_PTR
,
3126 name
, methods
, false));
3129 gcc_assert(p
->field_name() == "elem");
3130 vals
->push_back(Expression::make_type_descriptor(deref
, bloc
));
3132 return Expression::make_struct_composite_literal(ptr_tdt
, vals
, bloc
);
3136 // Reflection string.
3139 Pointer_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3141 ret
->push_back('*');
3142 this->append_reflection(this->to_type_
, gogo
, ret
);
3148 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3150 ret
->push_back('p');
3151 this->append_mangled_name(this->to_type_
, gogo
, ret
);
3157 Pointer_type::do_export(Export
* exp
) const
3159 exp
->write_c_string("*");
3160 if (this->is_unsafe_pointer_type())
3161 exp
->write_c_string("any");
3163 exp
->write_type(this->to_type_
);
3169 Pointer_type::do_import(Import
* imp
)
3171 imp
->require_c_string("*");
3172 if (imp
->match_c_string("any"))
3175 return Type::make_pointer_type(Type::make_void_type());
3177 Type
* to
= imp
->read_type();
3178 return Type::make_pointer_type(to
);
3181 // Make a pointer type.
3184 Type::make_pointer_type(Type
* to_type
)
3186 typedef Unordered_map(Type
*, Pointer_type
*) Hashtable
;
3187 static Hashtable pointer_types
;
3188 Hashtable::const_iterator p
= pointer_types
.find(to_type
);
3189 if (p
!= pointer_types
.end())
3191 Pointer_type
* ret
= new Pointer_type(to_type
);
3192 pointer_types
[to_type
] = ret
;
3196 // The nil type. We use a special type for nil because it is not the
3197 // same as any other type. In C term nil has type void*, but there is
3198 // no such type in Go.
3200 class Nil_type
: public Type
3210 { return ptr_type_node
; }
3213 do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
3214 { return is_clear
? NULL
: fold_convert(type_tree
, null_pointer_node
); }
3217 do_type_descriptor(Gogo
*, Named_type
*)
3218 { gcc_unreachable(); }
3221 do_reflection(Gogo
*, std::string
*) const
3222 { gcc_unreachable(); }
3225 do_mangled_name(Gogo
*, std::string
* ret
) const
3226 { ret
->push_back('n'); }
3229 // Make the nil type.
3232 Type::make_nil_type()
3234 static Nil_type singleton_nil_type
;
3235 return &singleton_nil_type
;
3238 // The type of a function call which returns multiple values. This is
3239 // really a struct, but we don't want to confuse a function call which
3240 // returns a struct with a function call which returns multiple
3243 class Call_multiple_result_type
: public Type
3246 Call_multiple_result_type(Call_expression
* call
)
3247 : Type(TYPE_CALL_MULTIPLE_RESULT
),
3253 do_has_pointer() const
3254 { gcc_unreachable(); }
3260 do_get_init_tree(Gogo
*, tree
, bool)
3261 { gcc_unreachable(); }
3264 do_type_descriptor(Gogo
*, Named_type
*)
3265 { gcc_unreachable(); }
3268 do_reflection(Gogo
*, std::string
*) const
3269 { gcc_unreachable(); }
3272 do_mangled_name(Gogo
*, std::string
*) const
3273 { gcc_unreachable(); }
3276 // The expression being called.
3277 Call_expression
* call_
;
3280 // Return the tree for a call result.
3283 Call_multiple_result_type::do_get_tree(Gogo
* gogo
)
3285 Function_type
* fntype
= this->call_
->get_function_type();
3286 gcc_assert(fntype
!= NULL
);
3287 const Typed_identifier_list
* results
= fntype
->results();
3288 gcc_assert(results
!= NULL
&& results
->size() > 1);
3290 Struct_field_list
* sfl
= new Struct_field_list
;
3291 for (Typed_identifier_list::const_iterator p
= results
->begin();
3292 p
!= results
->end();
3295 const std::string name
= ((p
->name().empty()
3296 || p
->name() == Import::import_marker
)
3299 sfl
->push_back(Struct_field(Typed_identifier(name
, p
->type(),
3300 this->call_
->location())));
3302 return Type::make_struct_type(sfl
, this->call_
->location())->get_tree(gogo
);
3305 // Make a call result type.
3308 Type::make_call_multiple_result_type(Call_expression
* call
)
3310 return new Call_multiple_result_type(call
);
3313 // Class Struct_field.
3315 // Get the name of a field.
3318 Struct_field::field_name() const
3320 const std::string
& name(this->typed_identifier_
.name());
3325 // This is called during parsing, before anything is lowered, so
3326 // we have to be pretty careful to avoid dereferencing an
3327 // unknown type name.
3328 Type
* t
= this->typed_identifier_
.type();
3330 if (t
->classification() == Type::TYPE_POINTER
)
3333 Pointer_type
* ptype
= static_cast<Pointer_type
*>(t
);
3334 dt
= ptype
->points_to();
3336 if (dt
->forward_declaration_type() != NULL
)
3337 return dt
->forward_declaration_type()->name();
3338 else if (dt
->named_type() != NULL
)
3339 return dt
->named_type()->name();
3340 else if (t
->is_error_type() || dt
->is_error_type())
3342 static const std::string error_string
= "*error*";
3343 return error_string
;
3347 // Avoid crashing in the erroneous case where T is named but
3349 gcc_assert(t
!= dt
);
3350 if (t
->forward_declaration_type() != NULL
)
3351 return t
->forward_declaration_type()->name();
3352 else if (t
->named_type() != NULL
)
3353 return t
->named_type()->name();
3360 // Class Struct_type.
3365 Struct_type::do_traverse(Traverse
* traverse
)
3367 Struct_field_list
* fields
= this->fields_
;
3370 for (Struct_field_list::iterator p
= fields
->begin();
3374 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
3375 return TRAVERSE_EXIT
;
3378 return TRAVERSE_CONTINUE
;
3381 // Verify that the struct type is complete and valid.
3384 Struct_type::do_verify()
3386 Struct_field_list
* fields
= this->fields_
;
3390 for (Struct_field_list::iterator p
= fields
->begin();
3394 Type
* t
= p
->type();
3395 if (t
->is_undefined())
3397 error_at(p
->location(), "struct field type is incomplete");
3398 p
->set_type(Type::make_error_type());
3401 else if (p
->is_anonymous())
3403 if (t
->named_type() != NULL
&& t
->points_to() != NULL
)
3405 error_at(p
->location(), "embedded type may not be a pointer");
3406 p
->set_type(Type::make_error_type());
3414 // Whether this contains a pointer.
3417 Struct_type::do_has_pointer() const
3419 const Struct_field_list
* fields
= this->fields();
3422 for (Struct_field_list::const_iterator p
= fields
->begin();
3426 if (p
->type()->has_pointer())
3432 // Whether this type is identical to T.
3435 Struct_type::is_identical(const Struct_type
* t
,
3436 bool errors_are_identical
) const
3438 const Struct_field_list
* fields1
= this->fields();
3439 const Struct_field_list
* fields2
= t
->fields();
3440 if (fields1
== NULL
|| fields2
== NULL
)
3441 return fields1
== fields2
;
3442 Struct_field_list::const_iterator pf2
= fields2
->begin();
3443 for (Struct_field_list::const_iterator pf1
= fields1
->begin();
3444 pf1
!= fields1
->end();
3447 if (pf2
== fields2
->end())
3449 if (pf1
->field_name() != pf2
->field_name())
3451 if (pf1
->is_anonymous() != pf2
->is_anonymous()
3452 || !Type::are_identical(pf1
->type(), pf2
->type(),
3453 errors_are_identical
, NULL
))
3455 if (!pf1
->has_tag())
3462 if (!pf2
->has_tag())
3464 if (pf1
->tag() != pf2
->tag())
3468 if (pf2
!= fields2
->end())
3473 // Whether this struct type has any hidden fields.
3476 Struct_type::struct_has_hidden_fields(const Named_type
* within
,
3477 std::string
* reason
) const
3479 const Struct_field_list
* fields
= this->fields();
3482 const Package
* within_package
= (within
== NULL
3484 : within
->named_object()->package());
3485 for (Struct_field_list::const_iterator pf
= fields
->begin();
3486 pf
!= fields
->end();
3489 if (within_package
!= NULL
3490 && !pf
->is_anonymous()
3491 && Gogo::is_hidden_name(pf
->field_name()))
3495 std::string within_name
= within
->named_object()->message_name();
3496 std::string name
= Gogo::message_name(pf
->field_name());
3497 size_t bufsize
= 200 + within_name
.length() + name
.length();
3498 char* buf
= new char[bufsize
];
3499 snprintf(buf
, bufsize
,
3500 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3501 open_quote
, within_name
.c_str(), close_quote
,
3502 open_quote
, name
.c_str(), close_quote
);
3503 reason
->assign(buf
);
3509 if (pf
->type()->has_hidden_fields(within
, reason
))
3519 Struct_type::do_hash_for_method(Gogo
* gogo
) const
3521 unsigned int ret
= 0;
3522 if (this->fields() != NULL
)
3524 for (Struct_field_list::const_iterator pf
= this->fields()->begin();
3525 pf
!= this->fields()->end();
3527 ret
= (ret
<< 1) + pf
->type()->hash_for_method(gogo
);
3532 // Find the local field NAME.
3535 Struct_type::find_local_field(const std::string
& name
,
3536 unsigned int *pindex
) const
3538 const Struct_field_list
* fields
= this->fields_
;
3542 for (Struct_field_list::const_iterator pf
= fields
->begin();
3543 pf
!= fields
->end();
3546 if (pf
->field_name() == name
)
3556 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3558 Field_reference_expression
*
3559 Struct_type::field_reference(Expression
* struct_expr
, const std::string
& name
,
3560 source_location location
) const
3563 return this->field_reference_depth(struct_expr
, name
, location
, &depth
);
3566 // Return an expression for a field, along with the depth at which it
3569 Field_reference_expression
*
3570 Struct_type::field_reference_depth(Expression
* struct_expr
,
3571 const std::string
& name
,
3572 source_location location
,
3573 unsigned int* depth
) const
3575 const Struct_field_list
* fields
= this->fields_
;
3579 // Look for a field with this name.
3581 for (Struct_field_list::const_iterator pf
= fields
->begin();
3582 pf
!= fields
->end();
3585 if (pf
->field_name() == name
)
3588 return Expression::make_field_reference(struct_expr
, i
, location
);
3592 // Look for an anonymous field which contains a field with this
3594 unsigned int found_depth
= 0;
3595 Field_reference_expression
* ret
= NULL
;
3597 for (Struct_field_list::const_iterator pf
= fields
->begin();
3598 pf
!= fields
->end();
3601 if (!pf
->is_anonymous())
3604 Struct_type
* st
= pf
->type()->deref()->struct_type();
3608 // Look for a reference using a NULL struct expression. If we
3609 // find one, fill in the struct expression with a reference to
3611 unsigned int subdepth
;
3612 Field_reference_expression
* sub
= st
->field_reference_depth(NULL
, name
,
3618 if (ret
== NULL
|| subdepth
< found_depth
)
3623 found_depth
= subdepth
;
3624 Expression
* here
= Expression::make_field_reference(struct_expr
, i
,
3626 if (pf
->type()->points_to() != NULL
)
3627 here
= Expression::make_unary(OPERATOR_MULT
, here
, location
);
3628 while (sub
->expr() != NULL
)
3630 sub
= sub
->expr()->deref()->field_reference_expression();
3631 gcc_assert(sub
!= NULL
);
3633 sub
->set_struct_expression(here
);
3635 else if (subdepth
> found_depth
)
3639 // We do not handle ambiguity here--it should be handled by
3640 // Type::bind_field_or_method.
3648 *depth
= found_depth
+ 1;
3653 // Return the total number of fields, including embedded fields.
3656 Struct_type::total_field_count() const
3658 if (this->fields_
== NULL
)
3660 unsigned int ret
= 0;
3661 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3662 pf
!= this->fields_
->end();
3665 if (!pf
->is_anonymous() || pf
->type()->deref()->struct_type() == NULL
)
3668 ret
+= pf
->type()->struct_type()->total_field_count();
3673 // Return whether NAME is an unexported field, for better error reporting.
3676 Struct_type::is_unexported_local_field(Gogo
* gogo
,
3677 const std::string
& name
) const
3679 const Struct_field_list
* fields
= this->fields_
;
3682 for (Struct_field_list::const_iterator pf
= fields
->begin();
3683 pf
!= fields
->end();
3686 const std::string
& field_name(pf
->field_name());
3687 if (Gogo::is_hidden_name(field_name
)
3688 && name
== Gogo::unpack_hidden_name(field_name
)
3689 && gogo
->pack_hidden_name(name
, false) != field_name
)
3696 // Finalize the methods of an unnamed struct.
3699 Struct_type::finalize_methods(Gogo
* gogo
)
3701 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
3704 // Return the method NAME, or NULL if there isn't one or if it is
3705 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3709 Struct_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
3711 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
3714 // Get the tree for a struct type.
3717 Struct_type::do_get_tree(Gogo
* gogo
)
3719 tree type
= make_node(RECORD_TYPE
);
3720 return this->fill_in_tree(gogo
, type
);
3723 // Fill in the fields for a struct type.
3726 Struct_type::fill_in_tree(Gogo
* gogo
, tree type
)
3728 tree field_trees
= NULL_TREE
;
3729 tree
* pp
= &field_trees
;
3730 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3731 p
!= this->fields_
->end();
3734 std::string name
= Gogo::unpack_hidden_name(p
->field_name());
3735 tree name_tree
= get_identifier_with_length(name
.data(), name
.length());
3736 tree field_type_tree
= p
->type()->get_tree(gogo
);
3737 if (field_type_tree
== error_mark_node
)
3738 return error_mark_node
;
3739 tree field
= build_decl(p
->location(), FIELD_DECL
, name_tree
,
3741 DECL_CONTEXT(field
) = type
;
3743 pp
= &DECL_CHAIN(field
);
3746 TYPE_FIELDS(type
) = field_trees
;
3753 // Initialize struct fields.
3756 Struct_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
3758 if (this->fields_
== NULL
|| this->fields_
->empty())
3764 tree ret
= build_constructor(type_tree
,
3765 VEC_alloc(constructor_elt
, gc
, 0));
3766 TREE_CONSTANT(ret
) = 1;
3771 bool is_constant
= true;
3772 bool any_fields_set
= false;
3773 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
,
3774 this->fields_
->size());
3776 tree field
= TYPE_FIELDS(type_tree
);
3777 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3778 p
!= this->fields_
->end();
3779 ++p
, field
= DECL_CHAIN(field
))
3781 tree value
= p
->type()->get_init_tree(gogo
, is_clear
);
3782 if (value
== error_mark_node
)
3783 return error_mark_node
;
3784 gcc_assert(field
!= NULL_TREE
);
3787 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
3790 any_fields_set
= true;
3791 if (!TREE_CONSTANT(value
))
3792 is_constant
= false;
3795 gcc_assert(field
== NULL_TREE
);
3797 if (!any_fields_set
)
3799 gcc_assert(is_clear
);
3800 VEC_free(constructor_elt
, gc
, init
);
3804 tree ret
= build_constructor(type_tree
, init
);
3806 TREE_CONSTANT(ret
) = 1;
3810 // The type of a struct type descriptor.
3813 Struct_type::make_struct_type_descriptor_type()
3818 Type
* tdt
= Type::make_type_descriptor_type();
3819 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
3821 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
3822 Type
* string_type
= Type::lookup_string_type();
3823 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
3826 Type::make_builtin_struct_type(5,
3827 "name", pointer_string_type
,
3828 "pkgPath", pointer_string_type
,
3830 "tag", pointer_string_type
,
3831 "offset", uintptr_type
);
3832 Type
* nsf
= Type::make_builtin_named_type("structField", sf
);
3834 Type
* slice_type
= Type::make_array_type(nsf
, NULL
);
3836 Struct_type
* s
= Type::make_builtin_struct_type(2,
3838 "fields", slice_type
);
3840 ret
= Type::make_builtin_named_type("StructType", s
);
3846 // Build a type descriptor for a struct type.
3849 Struct_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
3851 source_location bloc
= BUILTINS_LOCATION
;
3853 Type
* stdt
= Struct_type::make_struct_type_descriptor_type();
3855 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
3857 Expression_list
* vals
= new Expression_list();
3860 const Methods
* methods
= this->methods();
3861 // A named struct should not have methods--the methods should attach
3862 // to the named type.
3863 gcc_assert(methods
== NULL
|| name
== NULL
);
3865 Struct_field_list::const_iterator ps
= fields
->begin();
3866 gcc_assert(ps
->field_name() == "commonType");
3867 vals
->push_back(this->type_descriptor_constructor(gogo
,
3868 RUNTIME_TYPE_KIND_STRUCT
,
3869 name
, methods
, true));
3872 gcc_assert(ps
->field_name() == "fields");
3874 Expression_list
* elements
= new Expression_list();
3875 elements
->reserve(this->fields_
->size());
3876 Type
* element_type
= ps
->type()->array_type()->element_type();
3877 for (Struct_field_list::const_iterator pf
= this->fields_
->begin();
3878 pf
!= this->fields_
->end();
3881 const Struct_field_list
* f
= element_type
->struct_type()->fields();
3883 Expression_list
* fvals
= new Expression_list();
3886 Struct_field_list::const_iterator q
= f
->begin();
3887 gcc_assert(q
->field_name() == "name");
3888 if (pf
->is_anonymous())
3889 fvals
->push_back(Expression::make_nil(bloc
));
3892 std::string n
= Gogo::unpack_hidden_name(pf
->field_name());
3893 Expression
* s
= Expression::make_string(n
, bloc
);
3894 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3898 gcc_assert(q
->field_name() == "pkgPath");
3899 if (!Gogo::is_hidden_name(pf
->field_name()))
3900 fvals
->push_back(Expression::make_nil(bloc
));
3903 std::string n
= Gogo::hidden_name_prefix(pf
->field_name());
3904 Expression
* s
= Expression::make_string(n
, bloc
);
3905 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3909 gcc_assert(q
->field_name() == "typ");
3910 fvals
->push_back(Expression::make_type_descriptor(pf
->type(), bloc
));
3913 gcc_assert(q
->field_name() == "tag");
3915 fvals
->push_back(Expression::make_nil(bloc
));
3918 Expression
* s
= Expression::make_string(pf
->tag(), bloc
);
3919 fvals
->push_back(Expression::make_unary(OPERATOR_AND
, s
, bloc
));
3923 gcc_assert(q
->field_name() == "offset");
3924 fvals
->push_back(Expression::make_struct_field_offset(this, &*pf
));
3926 Expression
* v
= Expression::make_struct_composite_literal(element_type
,
3928 elements
->push_back(v
);
3931 vals
->push_back(Expression::make_slice_composite_literal(ps
->type(),
3934 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
3937 // Reflection string.
3940 Struct_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
3942 ret
->append("struct { ");
3944 for (Struct_field_list::const_iterator p
= this->fields_
->begin();
3945 p
!= this->fields_
->end();
3948 if (p
!= this->fields_
->begin())
3950 if (p
->is_anonymous())
3951 ret
->push_back('?');
3953 ret
->append(Gogo::unpack_hidden_name(p
->field_name()));
3954 ret
->push_back(' ');
3955 this->append_reflection(p
->type(), gogo
, ret
);
3959 const std::string
& tag(p
->tag());
3961 for (std::string::const_iterator p
= tag
.begin();
3966 ret
->append("\\x00");
3967 else if (*p
== '\n')
3969 else if (*p
== '\t')
3972 ret
->append("\\\"");
3973 else if (*p
== '\\')
3974 ret
->append("\\\\");
3978 ret
->push_back('"');
3988 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
3990 ret
->push_back('S');
3992 const Struct_field_list
* fields
= this->fields_
;
3995 for (Struct_field_list::const_iterator p
= fields
->begin();
3999 if (p
->is_anonymous())
4003 std::string n
= Gogo::unpack_hidden_name(p
->field_name());
4005 snprintf(buf
, sizeof buf
, "%u_",
4006 static_cast<unsigned int>(n
.length()));
4010 this->append_mangled_name(p
->type(), gogo
, ret
);
4013 const std::string
& tag(p
->tag());
4015 for (std::string::const_iterator p
= tag
.begin();
4019 if (ISALNUM(*p
) || *p
== '_')
4024 snprintf(buf
, sizeof buf
, ".%x.",
4025 static_cast<unsigned int>(*p
));
4030 snprintf(buf
, sizeof buf
, "T%u_",
4031 static_cast<unsigned int>(out
.length()));
4038 ret
->push_back('e');
4044 Struct_type::do_export(Export
* exp
) const
4046 exp
->write_c_string("struct { ");
4047 const Struct_field_list
* fields
= this->fields_
;
4048 gcc_assert(fields
!= NULL
);
4049 for (Struct_field_list::const_iterator p
= fields
->begin();
4053 if (p
->is_anonymous())
4054 exp
->write_string("? ");
4057 exp
->write_string(p
->field_name());
4058 exp
->write_c_string(" ");
4060 exp
->write_type(p
->type());
4064 exp
->write_c_string(" ");
4065 Expression
* expr
= Expression::make_string(p
->tag(),
4067 expr
->export_expression(exp
);
4071 exp
->write_c_string("; ");
4073 exp
->write_c_string("}");
4079 Struct_type::do_import(Import
* imp
)
4081 imp
->require_c_string("struct { ");
4082 Struct_field_list
* fields
= new Struct_field_list
;
4083 if (imp
->peek_char() != '}')
4088 if (imp
->match_c_string("? "))
4092 name
= imp
->read_identifier();
4093 imp
->require_c_string(" ");
4095 Type
* ftype
= imp
->read_type();
4097 Struct_field
sf(Typed_identifier(name
, ftype
, imp
->location()));
4099 if (imp
->peek_char() == ' ')
4102 Expression
* expr
= Expression::import_expression(imp
);
4103 String_expression
* sexpr
= expr
->string_expression();
4104 gcc_assert(sexpr
!= NULL
);
4105 sf
.set_tag(sexpr
->val());
4109 imp
->require_c_string("; ");
4110 fields
->push_back(sf
);
4111 if (imp
->peek_char() == '}')
4115 imp
->require_c_string("}");
4117 return Type::make_struct_type(fields
, imp
->location());
4120 // Make a struct type.
4123 Type::make_struct_type(Struct_field_list
* fields
,
4124 source_location location
)
4126 return new Struct_type(fields
, location
);
4129 // Class Array_type.
4131 // Whether two array types are identical.
4134 Array_type::is_identical(const Array_type
* t
, bool errors_are_identical
) const
4136 if (!Type::are_identical(this->element_type(), t
->element_type(),
4137 errors_are_identical
, NULL
))
4140 Expression
* l1
= this->length();
4141 Expression
* l2
= t
->length();
4143 // Slices of the same element type are identical.
4144 if (l1
== NULL
&& l2
== NULL
)
4147 // Arrays of the same element type are identical if they have the
4149 if (l1
!= NULL
&& l2
!= NULL
)
4154 // Try to determine the lengths. If we can't, assume the arrays
4155 // are not identical.
4163 if (l1
->integer_constant_value(true, v1
, &type1
)
4164 && l2
->integer_constant_value(true, v2
, &type2
))
4165 ret
= mpz_cmp(v1
, v2
) == 0;
4171 // Otherwise the arrays are not identical.
4178 Array_type::do_traverse(Traverse
* traverse
)
4180 if (Type::traverse(this->element_type_
, traverse
) == TRAVERSE_EXIT
)
4181 return TRAVERSE_EXIT
;
4182 if (this->length_
!= NULL
4183 && Expression::traverse(&this->length_
, traverse
) == TRAVERSE_EXIT
)
4184 return TRAVERSE_EXIT
;
4185 return TRAVERSE_CONTINUE
;
4188 // Check that the length is valid.
4191 Array_type::verify_length()
4193 if (this->length_
== NULL
)
4195 if (!this->length_
->is_constant())
4197 error_at(this->length_
->location(), "array bound is not constant");
4203 Type
* t
= this->length_
->type();
4204 if (t
->integer_type() != NULL
)
4208 if (!this->length_
->integer_constant_value(true, val
, &vt
))
4210 error_at(this->length_
->location(),
4211 "array bound is not constant");
4216 else if (t
->float_type() != NULL
)
4221 if (!this->length_
->float_constant_value(fval
, &vt
))
4223 error_at(this->length_
->location(),
4224 "array bound is not constant");
4228 if (!mpfr_integer_p(fval
))
4230 error_at(this->length_
->location(),
4231 "array bound truncated to integer");
4236 mpfr_get_z(val
, fval
, GMP_RNDN
);
4241 if (!t
->is_error_type())
4242 error_at(this->length_
->location(), "array bound is not numeric");
4246 if (mpz_sgn(val
) < 0)
4248 error_at(this->length_
->location(), "negative array bound");
4253 Type
* int_type
= Type::lookup_integer_type("int");
4254 int tbits
= int_type
->integer_type()->bits();
4255 int vbits
= mpz_sizeinbase(val
, 2);
4256 if (vbits
+ 1 > tbits
)
4258 error_at(this->length_
->location(), "array bound overflows");
4271 Array_type::do_verify()
4273 if (!this->verify_length())
4275 this->length_
= Expression::make_error(this->length_
->location());
4281 // Array type hash code.
4284 Array_type::do_hash_for_method(Gogo
* gogo
) const
4286 // There is no very convenient way to get a hash code for the
4288 return this->element_type_
->hash_for_method(gogo
) + 1;
4291 // See if the expression passed to make is suitable. The first
4292 // argument is required, and gives the length. An optional second
4293 // argument is permitted for the capacity.
4296 Array_type::do_check_make_expression(Expression_list
* args
,
4297 source_location location
)
4299 gcc_assert(this->length_
== NULL
);
4300 if (args
== NULL
|| args
->empty())
4302 error_at(location
, "length required when allocating a slice");
4305 else if (args
->size() > 2)
4307 error_at(location
, "too many expressions passed to make");
4312 if (!Type::check_int_value(args
->front(),
4313 _("bad length when making slice"), location
))
4316 if (args
->size() > 1)
4318 if (!Type::check_int_value(args
->back(),
4319 _("bad capacity when making slice"),
4328 // Get a tree for the length of a fixed array. The length may be
4329 // computed using a function call, so we must only evaluate it once.
4332 Array_type::get_length_tree(Gogo
* gogo
)
4334 gcc_assert(this->length_
!= NULL
);
4335 if (this->length_tree_
== NULL_TREE
)
4340 if (this->length_
->integer_constant_value(true, val
, &t
))
4343 t
= Type::lookup_integer_type("int");
4344 else if (t
->is_abstract())
4345 t
= t
->make_non_abstract_type();
4346 tree tt
= t
->get_tree(gogo
);
4347 this->length_tree_
= Expression::integer_constant_tree(val
, tt
);
4354 // Make up a translation context for the array length
4355 // expression. FIXME: This won't work in general.
4356 Translate_context
context(gogo
, NULL
, NULL
, NULL_TREE
);
4357 tree len
= this->length_
->get_tree(&context
);
4358 len
= convert_to_integer(integer_type_node
, len
);
4359 this->length_tree_
= save_expr(len
);
4362 return this->length_tree_
;
4365 // Get a tree for the type of this array. A fixed array is simply
4366 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4367 // just like an array in C. An open array is a struct with three
4368 // fields: a data pointer, the length, and the capacity.
4371 Array_type::do_get_tree(Gogo
* gogo
)
4373 if (this->length_
== NULL
)
4375 tree struct_type
= gogo
->slice_type_tree(void_type_node
);
4376 return this->fill_in_tree(gogo
, struct_type
);
4380 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4381 tree length_tree
= this->get_length_tree(gogo
);
4382 if (element_type_tree
== error_mark_node
4383 || length_tree
== error_mark_node
)
4384 return error_mark_node
;
4386 length_tree
= fold_convert(sizetype
, length_tree
);
4388 // build_index_type takes the maximum index, which is one less
4390 tree index_type
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
4394 return build_array_type(element_type_tree
, index_type
);
4398 // Fill in the fields for a slice type. This is used for named slice
4402 Array_type::fill_in_tree(Gogo
* gogo
, tree struct_type
)
4404 gcc_assert(this->length_
== NULL
);
4406 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4407 tree field
= TYPE_FIELDS(struct_type
);
4408 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
4409 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field
))
4410 && TREE_TYPE(TREE_TYPE(field
)) == void_type_node
);
4411 TREE_TYPE(field
) = build_pointer_type(element_type_tree
);
4416 // Return an initializer for an array type.
4419 Array_type::do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
4421 if (this->length_
== NULL
)
4428 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
4430 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
4432 for (tree field
= TYPE_FIELDS(type_tree
);
4434 field
= DECL_CHAIN(field
))
4436 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
,
4439 elt
->value
= fold_convert(TREE_TYPE(field
), size_zero_node
);
4442 tree ret
= build_constructor(type_tree
, init
);
4443 TREE_CONSTANT(ret
) = 1;
4450 tree value
= this->element_type_
->get_init_tree(gogo
, is_clear
);
4453 if (value
== error_mark_node
)
4454 return error_mark_node
;
4456 tree length_tree
= this->get_length_tree(gogo
);
4457 if (length_tree
== error_mark_node
)
4458 return error_mark_node
;
4460 length_tree
= fold_convert(sizetype
, length_tree
);
4461 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
,
4462 fold_build2(MINUS_EXPR
, sizetype
,
4463 length_tree
, size_one_node
));
4464 tree ret
= build_constructor_single(type_tree
, range
, value
);
4465 if (TREE_CONSTANT(value
))
4466 TREE_CONSTANT(ret
) = 1;
4471 // Handle the builtin make function for a slice.
4474 Array_type::do_make_expression_tree(Translate_context
* context
,
4475 Expression_list
* args
,
4476 source_location location
)
4478 gcc_assert(this->length_
== NULL
);
4480 Gogo
* gogo
= context
->gogo();
4481 tree type_tree
= this->get_tree(gogo
);
4482 if (type_tree
== error_mark_node
)
4483 return error_mark_node
;
4485 tree values_field
= TYPE_FIELDS(type_tree
);
4486 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field
)),
4489 tree count_field
= DECL_CHAIN(values_field
);
4490 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field
)),
4493 tree element_type_tree
= this->element_type_
->get_tree(gogo
);
4494 if (element_type_tree
== error_mark_node
)
4495 return error_mark_node
;
4496 tree element_size_tree
= TYPE_SIZE_UNIT(element_type_tree
);
4498 tree value
= this->element_type_
->get_init_tree(gogo
, true);
4500 // The first argument is the number of elements, the optional second
4501 // argument is the capacity.
4502 gcc_assert(args
!= NULL
&& args
->size() >= 1 && args
->size() <= 2);
4504 tree length_tree
= args
->front()->get_tree(context
);
4505 if (length_tree
== error_mark_node
)
4506 return error_mark_node
;
4507 if (!DECL_P(length_tree
))
4508 length_tree
= save_expr(length_tree
);
4509 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree
)))
4510 length_tree
= convert_to_integer(TREE_TYPE(count_field
), length_tree
);
4512 tree bad_index
= Expression::check_bounds(length_tree
,
4513 TREE_TYPE(count_field
),
4514 NULL_TREE
, location
);
4516 length_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
), length_tree
);
4518 if (args
->size() == 1)
4519 capacity_tree
= length_tree
;
4522 capacity_tree
= args
->back()->get_tree(context
);
4523 if (capacity_tree
== error_mark_node
)
4524 return error_mark_node
;
4525 if (!DECL_P(capacity_tree
))
4526 capacity_tree
= save_expr(capacity_tree
);
4527 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree
)))
4528 capacity_tree
= convert_to_integer(TREE_TYPE(count_field
),
4531 bad_index
= Expression::check_bounds(capacity_tree
,
4532 TREE_TYPE(count_field
),
4533 bad_index
, location
);
4535 tree chktype
= (((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4536 > TYPE_SIZE(TREE_TYPE(length_tree
)))
4537 || ((TYPE_SIZE(TREE_TYPE(capacity_tree
))
4538 == TYPE_SIZE(TREE_TYPE(length_tree
)))
4539 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree
))))
4540 ? TREE_TYPE(capacity_tree
)
4541 : TREE_TYPE(length_tree
));
4542 tree chk
= fold_build2_loc(location
, LT_EXPR
, boolean_type_node
,
4543 fold_convert_loc(location
, chktype
,
4545 fold_convert_loc(location
, chktype
,
4547 if (bad_index
== NULL_TREE
)
4550 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4553 capacity_tree
= fold_convert_loc(location
, TREE_TYPE(count_field
),
4557 tree size_tree
= fold_build2_loc(location
, MULT_EXPR
, sizetype
,
4559 fold_convert_loc(location
, sizetype
,
4562 tree chk
= fold_build2_loc(location
, TRUTH_AND_EXPR
, boolean_type_node
,
4563 fold_build2_loc(location
, GT_EXPR
,
4565 fold_convert_loc(location
,
4569 fold_build2_loc(location
, LT_EXPR
,
4571 size_tree
, element_size_tree
));
4572 if (bad_index
== NULL_TREE
)
4575 bad_index
= fold_build2_loc(location
, TRUTH_OR_EXPR
, boolean_type_node
,
4578 tree space
= context
->gogo()->allocate_memory(this->element_type_
,
4579 size_tree
, location
);
4581 if (value
!= NULL_TREE
)
4582 space
= save_expr(space
);
4584 space
= fold_convert(TREE_TYPE(values_field
), space
);
4586 if (bad_index
!= NULL_TREE
&& bad_index
!= boolean_false_node
)
4588 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
,
4590 space
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4591 build3(COND_EXPR
, void_type_node
,
4592 bad_index
, crash
, NULL_TREE
),
4596 tree constructor
= gogo
->slice_constructor(type_tree
, space
, length_tree
,
4599 if (value
== NULL_TREE
)
4601 // The array contents are zero initialized.
4605 // The elements must be initialized.
4607 tree max
= fold_build2_loc(location
, MINUS_EXPR
, TREE_TYPE(count_field
),
4609 fold_convert_loc(location
, TREE_TYPE(count_field
),
4612 tree array_type
= build_array_type(element_type_tree
,
4613 build_index_type(max
));
4615 tree value_pointer
= fold_convert_loc(location
,
4616 build_pointer_type(array_type
),
4619 tree range
= build2(RANGE_EXPR
, sizetype
, size_zero_node
, max
);
4620 tree space_init
= build_constructor_single(array_type
, range
, value
);
4622 return build2(COMPOUND_EXPR
, TREE_TYPE(space
),
4623 build2(MODIFY_EXPR
, void_type_node
,
4624 build_fold_indirect_ref(value_pointer
),
4629 // Return a tree for a pointer to the values in ARRAY.
4632 Array_type::value_pointer_tree(Gogo
*, tree array
) const
4635 if (this->length() != NULL
)
4638 ret
= fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array
))),
4639 build_fold_addr_expr(array
));
4644 tree field
= TYPE_FIELDS(TREE_TYPE(array
));
4645 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
4647 ret
= fold_build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
,
4650 if (TREE_CONSTANT(array
))
4651 TREE_CONSTANT(ret
) = 1;
4655 // Return a tree for the length of the array ARRAY which has this
4659 Array_type::length_tree(Gogo
* gogo
, tree array
)
4661 if (this->length_
!= NULL
)
4663 if (TREE_CODE(array
) == SAVE_EXPR
)
4664 return fold_convert(integer_type_node
, this->get_length_tree(gogo
));
4666 return omit_one_operand(integer_type_node
,
4667 this->get_length_tree(gogo
), array
);
4670 // This is an open array. We need to read the length field.
4672 tree type
= TREE_TYPE(array
);
4673 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4675 tree field
= DECL_CHAIN(TYPE_FIELDS(type
));
4676 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
4678 tree ret
= build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4679 if (TREE_CONSTANT(array
))
4680 TREE_CONSTANT(ret
) = 1;
4684 // Return a tree for the capacity of the array ARRAY which has this
4688 Array_type::capacity_tree(Gogo
* gogo
, tree array
)
4690 if (this->length_
!= NULL
)
4691 return omit_one_operand(sizetype
, this->get_length_tree(gogo
), array
);
4693 // This is an open array. We need to read the capacity field.
4695 tree type
= TREE_TYPE(array
);
4696 gcc_assert(TREE_CODE(type
) == RECORD_TYPE
);
4698 tree field
= DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type
)));
4699 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
4701 return build3(COMPONENT_REF
, TREE_TYPE(field
), array
, field
, NULL_TREE
);
4707 Array_type::do_export(Export
* exp
) const
4709 exp
->write_c_string("[");
4710 if (this->length_
!= NULL
)
4711 this->length_
->export_expression(exp
);
4712 exp
->write_c_string("] ");
4713 exp
->write_type(this->element_type_
);
4719 Array_type::do_import(Import
* imp
)
4721 imp
->require_c_string("[");
4723 if (imp
->peek_char() == ']')
4726 length
= Expression::import_expression(imp
);
4727 imp
->require_c_string("] ");
4728 Type
* element_type
= imp
->read_type();
4729 return Type::make_array_type(element_type
, length
);
4732 // The type of an array type descriptor.
4735 Array_type::make_array_type_descriptor_type()
4740 Type
* tdt
= Type::make_type_descriptor_type();
4741 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4743 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
4746 Type::make_builtin_struct_type(3,
4749 "len", uintptr_type
);
4751 ret
= Type::make_builtin_named_type("ArrayType", sf
);
4757 // The type of an slice type descriptor.
4760 Array_type::make_slice_type_descriptor_type()
4765 Type
* tdt
= Type::make_type_descriptor_type();
4766 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
4769 Type::make_builtin_struct_type(2,
4773 ret
= Type::make_builtin_named_type("SliceType", sf
);
4779 // Build a type descriptor for an array/slice type.
4782 Array_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4784 if (this->length_
!= NULL
)
4785 return this->array_type_descriptor(gogo
, name
);
4787 return this->slice_type_descriptor(gogo
, name
);
4790 // Build a type descriptor for an array type.
4793 Array_type::array_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4795 source_location bloc
= BUILTINS_LOCATION
;
4797 Type
* atdt
= Array_type::make_array_type_descriptor_type();
4799 const Struct_field_list
* fields
= atdt
->struct_type()->fields();
4801 Expression_list
* vals
= new Expression_list();
4804 Struct_field_list::const_iterator p
= fields
->begin();
4805 gcc_assert(p
->field_name() == "commonType");
4806 vals
->push_back(this->type_descriptor_constructor(gogo
,
4807 RUNTIME_TYPE_KIND_ARRAY
,
4811 gcc_assert(p
->field_name() == "elem");
4812 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4815 gcc_assert(p
->field_name() == "len");
4816 vals
->push_back(this->length_
);
4819 gcc_assert(p
== fields
->end());
4821 return Expression::make_struct_composite_literal(atdt
, vals
, bloc
);
4824 // Build a type descriptor for a slice type.
4827 Array_type::slice_type_descriptor(Gogo
* gogo
, Named_type
* name
)
4829 source_location bloc
= BUILTINS_LOCATION
;
4831 Type
* stdt
= Array_type::make_slice_type_descriptor_type();
4833 const Struct_field_list
* fields
= stdt
->struct_type()->fields();
4835 Expression_list
* vals
= new Expression_list();
4838 Struct_field_list::const_iterator p
= fields
->begin();
4839 gcc_assert(p
->field_name() == "commonType");
4840 vals
->push_back(this->type_descriptor_constructor(gogo
,
4841 RUNTIME_TYPE_KIND_SLICE
,
4845 gcc_assert(p
->field_name() == "elem");
4846 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
4849 gcc_assert(p
== fields
->end());
4851 return Expression::make_struct_composite_literal(stdt
, vals
, bloc
);
4854 // Reflection string.
4857 Array_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
4859 ret
->push_back('[');
4860 if (this->length_
!= NULL
)
4865 if (!this->length_
->integer_constant_value(true, val
, &type
))
4866 error_at(this->length_
->location(),
4867 "array length must be integer constant expression");
4868 else if (mpz_cmp_si(val
, 0) < 0)
4869 error_at(this->length_
->location(), "array length is negative");
4870 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4871 error_at(this->length_
->location(), "array length is too large");
4875 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4880 ret
->push_back(']');
4882 this->append_reflection(this->element_type_
, gogo
, ret
);
4888 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
4890 ret
->push_back('A');
4891 this->append_mangled_name(this->element_type_
, gogo
, ret
);
4892 if (this->length_
!= NULL
)
4897 if (!this->length_
->integer_constant_value(true, val
, &type
))
4898 error_at(this->length_
->location(),
4899 "array length must be integer constant expression");
4900 else if (mpz_cmp_si(val
, 0) < 0)
4901 error_at(this->length_
->location(), "array length is negative");
4902 else if (mpz_cmp_ui(val
, mpz_get_ui(val
)) != 0)
4903 error_at(this->length_
->location(), "array size is too large");
4907 snprintf(buf
, sizeof buf
, "%lu", mpz_get_ui(val
));
4912 ret
->push_back('e');
4915 // Make an array type.
4918 Type::make_array_type(Type
* element_type
, Expression
* length
)
4920 return new Array_type(element_type
, length
);
4928 Map_type::do_traverse(Traverse
* traverse
)
4930 if (Type::traverse(this->key_type_
, traverse
) == TRAVERSE_EXIT
4931 || Type::traverse(this->val_type_
, traverse
) == TRAVERSE_EXIT
)
4932 return TRAVERSE_EXIT
;
4933 return TRAVERSE_CONTINUE
;
4936 // Check that the map type is OK.
4939 Map_type::do_verify()
4941 if (this->key_type_
->struct_type() != NULL
4942 || this->key_type_
->array_type() != NULL
)
4944 error_at(this->location_
, "invalid map key type");
4950 // Whether two map types are identical.
4953 Map_type::is_identical(const Map_type
* t
, bool errors_are_identical
) const
4955 return (Type::are_identical(this->key_type(), t
->key_type(),
4956 errors_are_identical
, NULL
)
4957 && Type::are_identical(this->val_type(), t
->val_type(),
4958 errors_are_identical
, NULL
));
4964 Map_type::do_hash_for_method(Gogo
* gogo
) const
4966 return (this->key_type_
->hash_for_method(gogo
)
4967 + this->val_type_
->hash_for_method(gogo
)
4971 // Check that a call to the builtin make function is valid. For a map
4972 // the optional argument is the number of spaces to preallocate for
4976 Map_type::do_check_make_expression(Expression_list
* args
,
4977 source_location location
)
4979 if (args
!= NULL
&& !args
->empty())
4981 if (!Type::check_int_value(args
->front(), _("bad size when making map"),
4984 else if (args
->size() > 1)
4986 error_at(location
, "too many arguments when making map");
4993 // Get a tree for a map type. A map type is represented as a pointer
4994 // to a struct. The struct is __go_map in libgo/map.h.
4997 Map_type::do_get_tree(Gogo
* gogo
)
4999 static tree type_tree
;
5000 if (type_tree
== NULL_TREE
)
5002 tree struct_type
= make_node(RECORD_TYPE
);
5004 tree map_descriptor_type
= gogo
->map_descriptor_type();
5005 tree const_map_descriptor_type
=
5006 build_qualified_type(map_descriptor_type
, TYPE_QUAL_CONST
);
5007 tree name
= get_identifier("__descriptor");
5008 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5009 build_pointer_type(const_map_descriptor_type
));
5010 DECL_CONTEXT(field
) = struct_type
;
5011 TYPE_FIELDS(struct_type
) = field
;
5012 tree last_field
= field
;
5014 name
= get_identifier("__element_count");
5015 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5016 DECL_CONTEXT(field
) = struct_type
;
5017 DECL_CHAIN(last_field
) = field
;
5020 name
= get_identifier("__bucket_count");
5021 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
, sizetype
);
5022 DECL_CONTEXT(field
) = struct_type
;
5023 DECL_CHAIN(last_field
) = field
;
5026 name
= get_identifier("__buckets");
5027 field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
, name
,
5028 build_pointer_type(ptr_type_node
));
5029 DECL_CONTEXT(field
) = struct_type
;
5030 DECL_CHAIN(last_field
) = field
;
5032 layout_type(struct_type
);
5034 // Give the struct a name for better debugging info.
5035 name
= get_identifier("__go_map");
5036 tree type_decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
, name
,
5038 DECL_ARTIFICIAL(type_decl
) = 1;
5039 TYPE_NAME(struct_type
) = type_decl
;
5040 go_preserve_from_gc(type_decl
);
5041 rest_of_decl_compilation(type_decl
, 1, 0);
5043 type_tree
= build_pointer_type(struct_type
);
5044 go_preserve_from_gc(type_tree
);
5050 // Initialize a map.
5053 Map_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5057 return fold_convert(type_tree
, null_pointer_node
);
5060 // Return an expression for a newly allocated map.
5063 Map_type::do_make_expression_tree(Translate_context
* context
,
5064 Expression_list
* args
,
5065 source_location location
)
5067 tree bad_index
= NULL_TREE
;
5070 if (args
== NULL
|| args
->empty())
5071 expr_tree
= size_zero_node
;
5074 expr_tree
= args
->front()->get_tree(context
);
5075 if (expr_tree
== error_mark_node
)
5076 return error_mark_node
;
5077 if (!DECL_P(expr_tree
))
5078 expr_tree
= save_expr(expr_tree
);
5079 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5080 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5081 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5085 tree map_type
= this->get_tree(context
->gogo());
5087 static tree new_map_fndecl
;
5088 tree ret
= Gogo::call_builtin(&new_map_fndecl
,
5093 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type
))),
5094 context
->gogo()->map_descriptor(this),
5097 if (ret
== error_mark_node
)
5098 return error_mark_node
;
5099 // This can panic if the capacity is out of range.
5100 TREE_NOTHROW(new_map_fndecl
) = 0;
5102 if (bad_index
== NULL_TREE
)
5106 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
,
5108 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5109 build3(COND_EXPR
, void_type_node
,
5110 bad_index
, crash
, NULL_TREE
),
5115 // The type of a map type descriptor.
5118 Map_type::make_map_type_descriptor_type()
5123 Type
* tdt
= Type::make_type_descriptor_type();
5124 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5127 Type::make_builtin_struct_type(3,
5132 ret
= Type::make_builtin_named_type("MapType", sf
);
5138 // Build a type descriptor for a map type.
5141 Map_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5143 source_location bloc
= BUILTINS_LOCATION
;
5145 Type
* mtdt
= Map_type::make_map_type_descriptor_type();
5147 const Struct_field_list
* fields
= mtdt
->struct_type()->fields();
5149 Expression_list
* vals
= new Expression_list();
5152 Struct_field_list::const_iterator p
= fields
->begin();
5153 gcc_assert(p
->field_name() == "commonType");
5154 vals
->push_back(this->type_descriptor_constructor(gogo
,
5155 RUNTIME_TYPE_KIND_MAP
,
5159 gcc_assert(p
->field_name() == "key");
5160 vals
->push_back(Expression::make_type_descriptor(this->key_type_
, bloc
));
5163 gcc_assert(p
->field_name() == "elem");
5164 vals
->push_back(Expression::make_type_descriptor(this->val_type_
, bloc
));
5167 gcc_assert(p
== fields
->end());
5169 return Expression::make_struct_composite_literal(mtdt
, vals
, bloc
);
5172 // Reflection string for a map.
5175 Map_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5177 ret
->append("map[");
5178 this->append_reflection(this->key_type_
, gogo
, ret
);
5180 this->append_reflection(this->val_type_
, gogo
, ret
);
5183 // Mangled name for a map.
5186 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5188 ret
->push_back('M');
5189 this->append_mangled_name(this->key_type_
, gogo
, ret
);
5191 this->append_mangled_name(this->val_type_
, gogo
, ret
);
5194 // Export a map type.
5197 Map_type::do_export(Export
* exp
) const
5199 exp
->write_c_string("map [");
5200 exp
->write_type(this->key_type_
);
5201 exp
->write_c_string("] ");
5202 exp
->write_type(this->val_type_
);
5205 // Import a map type.
5208 Map_type::do_import(Import
* imp
)
5210 imp
->require_c_string("map [");
5211 Type
* key_type
= imp
->read_type();
5212 imp
->require_c_string("] ");
5213 Type
* val_type
= imp
->read_type();
5214 return Type::make_map_type(key_type
, val_type
, imp
->location());
5220 Type::make_map_type(Type
* key_type
, Type
* val_type
, source_location location
)
5222 return new Map_type(key_type
, val_type
, location
);
5225 // Class Channel_type.
5230 Channel_type::do_hash_for_method(Gogo
* gogo
) const
5232 unsigned int ret
= 0;
5233 if (this->may_send_
)
5235 if (this->may_receive_
)
5237 if (this->element_type_
!= NULL
)
5238 ret
+= this->element_type_
->hash_for_method(gogo
) << 2;
5242 // Whether this type is the same as T.
5245 Channel_type::is_identical(const Channel_type
* t
,
5246 bool errors_are_identical
) const
5248 if (!Type::are_identical(this->element_type(), t
->element_type(),
5249 errors_are_identical
, NULL
))
5251 return (this->may_send_
== t
->may_send_
5252 && this->may_receive_
== t
->may_receive_
);
5255 // Check whether the parameters for a call to the builtin function
5256 // make are OK for a channel. A channel can take an optional single
5257 // parameter which is the buffer size.
5260 Channel_type::do_check_make_expression(Expression_list
* args
,
5261 source_location location
)
5263 if (args
!= NULL
&& !args
->empty())
5265 if (!Type::check_int_value(args
->front(),
5266 _("bad buffer size when making channel"),
5269 else if (args
->size() > 1)
5271 error_at(location
, "too many arguments when making channel");
5278 // Return the tree for a channel type. A channel is a pointer to a
5279 // __go_channel struct. The __go_channel struct is defined in
5280 // libgo/runtime/channel.h.
5283 Channel_type::do_get_tree(Gogo
*)
5285 static tree type_tree
;
5286 if (type_tree
== NULL_TREE
)
5288 tree ret
= make_node(RECORD_TYPE
);
5289 TYPE_NAME(ret
) = get_identifier("__go_channel");
5290 TYPE_STUB_DECL(ret
) = build_decl(BUILTINS_LOCATION
, TYPE_DECL
, NULL_TREE
,
5292 type_tree
= build_pointer_type(ret
);
5293 go_preserve_from_gc(type_tree
);
5298 // Initialize a channel variable.
5301 Channel_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5305 return fold_convert(type_tree
, null_pointer_node
);
5308 // Handle the builtin function make for a channel.
5311 Channel_type::do_make_expression_tree(Translate_context
* context
,
5312 Expression_list
* args
,
5313 source_location location
)
5315 Gogo
* gogo
= context
->gogo();
5316 tree channel_type
= this->get_tree(gogo
);
5318 tree element_tree
= this->element_type_
->get_tree(gogo
);
5319 tree element_size_tree
= size_in_bytes(element_tree
);
5321 tree bad_index
= NULL_TREE
;
5324 if (args
== NULL
|| args
->empty())
5325 expr_tree
= size_zero_node
;
5328 expr_tree
= args
->front()->get_tree(context
);
5329 if (expr_tree
== error_mark_node
)
5330 return error_mark_node
;
5331 if (!DECL_P(expr_tree
))
5332 expr_tree
= save_expr(expr_tree
);
5333 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
5334 expr_tree
= convert_to_integer(sizetype
, expr_tree
);
5335 bad_index
= Expression::check_bounds(expr_tree
, sizetype
, bad_index
,
5339 static tree new_channel_fndecl
;
5340 tree ret
= Gogo::call_builtin(&new_channel_fndecl
,
5349 if (ret
== error_mark_node
)
5350 return error_mark_node
;
5351 // This can panic if the capacity is out of range.
5352 TREE_NOTHROW(new_channel_fndecl
) = 0;
5354 if (bad_index
== NULL_TREE
)
5358 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
,
5360 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
5361 build3(COND_EXPR
, void_type_node
,
5362 bad_index
, crash
, NULL_TREE
),
5367 // Build a type descriptor for a channel type.
5370 Channel_type::make_chan_type_descriptor_type()
5375 Type
* tdt
= Type::make_type_descriptor_type();
5376 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
5378 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
5381 Type::make_builtin_struct_type(3,
5384 "dir", uintptr_type
);
5386 ret
= Type::make_builtin_named_type("ChanType", sf
);
5392 // Build a type descriptor for a map type.
5395 Channel_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
5397 source_location bloc
= BUILTINS_LOCATION
;
5399 Type
* ctdt
= Channel_type::make_chan_type_descriptor_type();
5401 const Struct_field_list
* fields
= ctdt
->struct_type()->fields();
5403 Expression_list
* vals
= new Expression_list();
5406 Struct_field_list::const_iterator p
= fields
->begin();
5407 gcc_assert(p
->field_name() == "commonType");
5408 vals
->push_back(this->type_descriptor_constructor(gogo
,
5409 RUNTIME_TYPE_KIND_CHAN
,
5413 gcc_assert(p
->field_name() == "elem");
5414 vals
->push_back(Expression::make_type_descriptor(this->element_type_
, bloc
));
5417 gcc_assert(p
->field_name() == "dir");
5418 // These bits must match the ones in libgo/runtime/go-type.h.
5420 if (this->may_receive_
)
5422 if (this->may_send_
)
5425 mpz_init_set_ui(iv
, val
);
5426 vals
->push_back(Expression::make_integer(&iv
, p
->type(), bloc
));
5430 gcc_assert(p
== fields
->end());
5432 return Expression::make_struct_composite_literal(ctdt
, vals
, bloc
);
5435 // Reflection string.
5438 Channel_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
5440 if (!this->may_send_
)
5442 ret
->append("chan");
5443 if (!this->may_receive_
)
5445 ret
->push_back(' ');
5446 this->append_reflection(this->element_type_
, gogo
, ret
);
5452 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
5454 ret
->push_back('C');
5455 this->append_mangled_name(this->element_type_
, gogo
, ret
);
5456 if (this->may_send_
)
5457 ret
->push_back('s');
5458 if (this->may_receive_
)
5459 ret
->push_back('r');
5460 ret
->push_back('e');
5466 Channel_type::do_export(Export
* exp
) const
5468 exp
->write_c_string("chan ");
5469 if (this->may_send_
&& !this->may_receive_
)
5470 exp
->write_c_string("-< ");
5471 else if (this->may_receive_
&& !this->may_send_
)
5472 exp
->write_c_string("<- ");
5473 exp
->write_type(this->element_type_
);
5479 Channel_type::do_import(Import
* imp
)
5481 imp
->require_c_string("chan ");
5485 if (imp
->match_c_string("-< "))
5489 may_receive
= false;
5491 else if (imp
->match_c_string("<- "))
5503 Type
* element_type
= imp
->read_type();
5505 return Type::make_channel_type(may_send
, may_receive
, element_type
);
5508 // Make a new channel type.
5511 Type::make_channel_type(bool send
, bool receive
, Type
* element_type
)
5513 return new Channel_type(send
, receive
, element_type
);
5516 // Class Interface_type.
5521 Interface_type::do_traverse(Traverse
* traverse
)
5523 if (this->methods_
== NULL
)
5524 return TRAVERSE_CONTINUE
;
5525 return this->methods_
->traverse(traverse
);
5528 // Finalize the methods. This handles interface inheritance.
5531 Interface_type::finalize_methods()
5533 if (this->methods_
== NULL
)
5535 bool is_recursive
= false;
5538 while (from
< this->methods_
->size())
5540 const Typed_identifier
* p
= &this->methods_
->at(from
);
5541 if (!p
->name().empty())
5544 for (i
= 0; i
< to
; ++i
)
5546 if (this->methods_
->at(i
).name() == p
->name())
5548 error_at(p
->location(), "duplicate method %qs",
5549 Gogo::message_name(p
->name()).c_str());
5556 this->methods_
->set(to
, *p
);
5562 Interface_type
* it
= p
->type()->interface_type();
5565 error_at(p
->location(), "interface contains embedded non-interface");
5573 error_at(p
->location(), "invalid recursive interface");
5574 is_recursive
= true;
5579 const Typed_identifier_list
* methods
= it
->methods();
5580 if (methods
== NULL
)
5585 for (Typed_identifier_list::const_iterator q
= methods
->begin();
5586 q
!= methods
->end();
5589 if (q
->name().empty() || this->find_method(q
->name()) == NULL
)
5590 this->methods_
->push_back(Typed_identifier(q
->name(), q
->type(),
5595 error_at(p
->location(), "inherited method %qs is ambiguous",
5596 Gogo::message_name(q
->name()).c_str());
5603 delete this->methods_
;
5604 this->methods_
= NULL
;
5608 this->methods_
->resize(to
);
5609 this->methods_
->sort_by_name();
5613 // Return the method NAME, or NULL.
5615 const Typed_identifier
*
5616 Interface_type::find_method(const std::string
& name
) const
5618 if (this->methods_
== NULL
)
5620 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5621 p
!= this->methods_
->end();
5623 if (p
->name() == name
)
5628 // Return the method index.
5631 Interface_type::method_index(const std::string
& name
) const
5633 gcc_assert(this->methods_
!= NULL
);
5635 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5636 p
!= this->methods_
->end();
5638 if (p
->name() == name
)
5643 // Return whether NAME is an unexported method, for better error
5647 Interface_type::is_unexported_method(Gogo
* gogo
, const std::string
& name
) const
5649 if (this->methods_
== NULL
)
5651 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5652 p
!= this->methods_
->end();
5655 const std::string
& method_name(p
->name());
5656 if (Gogo::is_hidden_name(method_name
)
5657 && name
== Gogo::unpack_hidden_name(method_name
)
5658 && gogo
->pack_hidden_name(name
, false) != method_name
)
5664 // Whether this type is identical with T.
5667 Interface_type::is_identical(const Interface_type
* t
,
5668 bool errors_are_identical
) const
5670 // We require the same methods with the same types. The methods
5671 // have already been sorted.
5672 if (this->methods() == NULL
|| t
->methods() == NULL
)
5673 return this->methods() == t
->methods();
5675 Typed_identifier_list::const_iterator p1
= this->methods()->begin();
5676 for (Typed_identifier_list::const_iterator p2
= t
->methods()->begin();
5677 p2
!= t
->methods()->end();
5680 if (p1
== this->methods()->end())
5682 if (p1
->name() != p2
->name()
5683 || !Type::are_identical(p1
->type(), p2
->type(),
5684 errors_are_identical
, NULL
))
5687 if (p1
!= this->methods()->end())
5692 // Whether we can assign the interface type T to this type. The types
5693 // are known to not be identical. An interface assignment is only
5694 // permitted if T is known to implement all methods in THIS.
5695 // Otherwise a type guard is required.
5698 Interface_type::is_compatible_for_assign(const Interface_type
* t
,
5699 std::string
* reason
) const
5701 if (this->methods() == NULL
)
5703 for (Typed_identifier_list::const_iterator p
= this->methods()->begin();
5704 p
!= this->methods()->end();
5707 const Typed_identifier
* m
= t
->find_method(p
->name());
5713 snprintf(buf
, sizeof buf
,
5714 _("need explicit conversion; missing method %s%s%s"),
5715 open_quote
, Gogo::message_name(p
->name()).c_str(),
5717 reason
->assign(buf
);
5722 std::string subreason
;
5723 if (!Type::are_identical(p
->type(), m
->type(), true, &subreason
))
5727 std::string n
= Gogo::message_name(p
->name());
5728 size_t len
= 100 + n
.length() + subreason
.length();
5729 char* buf
= new char[len
];
5730 if (subreason
.empty())
5731 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5732 open_quote
, n
.c_str(), close_quote
);
5735 _("incompatible type for method %s%s%s (%s)"),
5736 open_quote
, n
.c_str(), close_quote
,
5738 reason
->assign(buf
);
5751 Interface_type::do_hash_for_method(Gogo
* gogo
) const
5753 unsigned int ret
= 0;
5754 if (this->methods_
!= NULL
)
5756 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5757 p
!= this->methods_
->end();
5760 ret
= Type::hash_string(p
->name(), ret
);
5761 ret
+= p
->type()->hash_for_method(gogo
);
5768 // Return true if T implements the interface. If it does not, and
5769 // REASON is not NULL, set *REASON to a useful error message.
5772 Interface_type::implements_interface(const Type
* t
, std::string
* reason
) const
5774 if (this->methods_
== NULL
)
5777 bool is_pointer
= false;
5778 const Named_type
* nt
= t
->named_type();
5779 const Struct_type
* st
= t
->struct_type();
5780 // If we start with a named type, we don't dereference it to find
5784 const Type
* pt
= t
->points_to();
5787 // If T is a pointer to a named type, then we need to look at
5788 // the type to which it points.
5790 nt
= pt
->named_type();
5791 st
= pt
->struct_type();
5795 // If we have a named type, get the methods from it rather than from
5800 // Only named and struct types have methods.
5801 if (nt
== NULL
&& st
== NULL
)
5805 if (t
->points_to() != NULL
5806 && t
->points_to()->interface_type() != NULL
)
5807 reason
->assign(_("pointer to interface type has no methods"));
5809 reason
->assign(_("type has no methods"));
5814 if (nt
!= NULL
? !nt
->has_any_methods() : !st
->has_any_methods())
5818 if (t
->points_to() != NULL
5819 && t
->points_to()->interface_type() != NULL
)
5820 reason
->assign(_("pointer to interface type has no methods"));
5822 reason
->assign(_("type has no methods"));
5827 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5828 p
!= this->methods_
->end();
5831 bool is_ambiguous
= false;
5832 Method
* m
= (nt
!= NULL
5833 ? nt
->method_function(p
->name(), &is_ambiguous
)
5834 : st
->method_function(p
->name(), &is_ambiguous
));
5839 std::string n
= Gogo::message_name(p
->name());
5840 size_t len
= n
.length() + 100;
5841 char* buf
= new char[len
];
5843 snprintf(buf
, len
, _("ambiguous method %s%s%s"),
5844 open_quote
, n
.c_str(), close_quote
);
5846 snprintf(buf
, len
, _("missing method %s%s%s"),
5847 open_quote
, n
.c_str(), close_quote
);
5848 reason
->assign(buf
);
5854 Function_type
*p_fn_type
= p
->type()->function_type();
5855 Function_type
* m_fn_type
= m
->type()->function_type();
5856 gcc_assert(p_fn_type
!= NULL
&& m_fn_type
!= NULL
);
5857 std::string subreason
;
5858 if (!p_fn_type
->is_identical(m_fn_type
, true, true, &subreason
))
5862 std::string n
= Gogo::message_name(p
->name());
5863 size_t len
= 100 + n
.length() + subreason
.length();
5864 char* buf
= new char[len
];
5865 if (subreason
.empty())
5866 snprintf(buf
, len
, _("incompatible type for method %s%s%s"),
5867 open_quote
, n
.c_str(), close_quote
);
5870 _("incompatible type for method %s%s%s (%s)"),
5871 open_quote
, n
.c_str(), close_quote
,
5873 reason
->assign(buf
);
5879 if (!is_pointer
&& !m
->is_value_method())
5883 std::string n
= Gogo::message_name(p
->name());
5884 size_t len
= 100 + n
.length();
5885 char* buf
= new char[len
];
5886 snprintf(buf
, len
, _("method %s%s%s requires a pointer"),
5887 open_quote
, n
.c_str(), close_quote
);
5888 reason
->assign(buf
);
5898 // Return a tree for an interface type. An interface is a pointer to
5899 // a struct. The struct has three fields. The first field is a
5900 // pointer to the type descriptor for the dynamic type of the object.
5901 // The second field is a pointer to a table of methods for the
5902 // interface to be used with the object. The third field is the value
5903 // of the object itself.
5906 Interface_type::do_get_tree(Gogo
* gogo
)
5908 if (this->methods_
== NULL
)
5910 // At the tree level, use the same type for all empty
5911 // interfaces. This lets us assign them to each other directly
5912 // without triggering GIMPLE type errors.
5913 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5914 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5915 static tree empty_interface
;
5916 return Gogo::builtin_struct(&empty_interface
, "__go_empty_interface",
5918 "__type_descriptor",
5924 return this->fill_in_tree(gogo
, make_node(RECORD_TYPE
));
5927 // Fill in the tree for an interface type. This is used for named
5931 Interface_type::fill_in_tree(Gogo
* gogo
, tree type
)
5933 gcc_assert(this->methods_
!= NULL
);
5935 // Build the type of the table of methods.
5937 tree method_table
= make_node(RECORD_TYPE
);
5939 // The first field is a pointer to the type descriptor.
5940 tree name_tree
= get_identifier("__type_descriptor");
5941 tree dtype
= Type::make_type_descriptor_type()->get_tree(gogo
);
5942 dtype
= build_pointer_type(build_qualified_type(dtype
, TYPE_QUAL_CONST
));
5943 tree field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, dtype
);
5944 DECL_CONTEXT(field
) = method_table
;
5945 TYPE_FIELDS(method_table
) = field
;
5947 std::string last_name
= "";
5948 tree
* pp
= &DECL_CHAIN(field
);
5949 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
5950 p
!= this->methods_
->end();
5953 std::string name
= Gogo::unpack_hidden_name(p
->name());
5954 name_tree
= get_identifier_with_length(name
.data(), name
.length());
5955 tree field_type
= p
->type()->get_tree(gogo
);
5956 if (field_type
== error_mark_node
)
5957 return error_mark_node
;
5958 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, field_type
);
5959 DECL_CONTEXT(field
) = method_table
;
5961 pp
= &DECL_CHAIN(field
);
5962 // Sanity check: the names should be sorted.
5963 gcc_assert(p
->name() > last_name
);
5964 last_name
= p
->name();
5966 layout_type(method_table
);
5968 tree mtype
= build_pointer_type(method_table
);
5970 tree field_trees
= NULL_TREE
;
5973 name_tree
= get_identifier("__methods");
5974 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, mtype
);
5975 DECL_CONTEXT(field
) = type
;
5977 pp
= &DECL_CHAIN(field
);
5979 name_tree
= get_identifier("__object");
5980 field
= build_decl(this->location_
, FIELD_DECL
, name_tree
, ptr_type_node
);
5981 DECL_CONTEXT(field
) = type
;
5984 TYPE_FIELDS(type
) = field_trees
;
5991 // Initialization value.
5994 Interface_type::do_get_init_tree(Gogo
*, tree type_tree
, bool is_clear
)
5999 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
6000 for (tree field
= TYPE_FIELDS(type_tree
);
6002 field
= DECL_CHAIN(field
))
6004 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
6006 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
6009 tree ret
= build_constructor(type_tree
, init
);
6010 TREE_CONSTANT(ret
) = 1;
6014 // The type of an interface type descriptor.
6017 Interface_type::make_interface_type_descriptor_type()
6022 Type
* tdt
= Type::make_type_descriptor_type();
6023 Type
* ptdt
= Type::make_type_descriptor_ptr_type();
6025 Type
* string_type
= Type::lookup_string_type();
6026 Type
* pointer_string_type
= Type::make_pointer_type(string_type
);
6029 Type::make_builtin_struct_type(3,
6030 "name", pointer_string_type
,
6031 "pkgPath", pointer_string_type
,
6034 Type
* nsm
= Type::make_builtin_named_type("imethod", sm
);
6036 Type
* slice_nsm
= Type::make_array_type(nsm
, NULL
);
6038 Struct_type
* s
= Type::make_builtin_struct_type(2,
6040 "methods", slice_nsm
);
6042 ret
= Type::make_builtin_named_type("InterfaceType", s
);
6048 // Build a type descriptor for an interface type.
6051 Interface_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
6053 source_location bloc
= BUILTINS_LOCATION
;
6055 Type
* itdt
= Interface_type::make_interface_type_descriptor_type();
6057 const Struct_field_list
* ifields
= itdt
->struct_type()->fields();
6059 Expression_list
* ivals
= new Expression_list();
6062 Struct_field_list::const_iterator pif
= ifields
->begin();
6063 gcc_assert(pif
->field_name() == "commonType");
6064 ivals
->push_back(this->type_descriptor_constructor(gogo
,
6065 RUNTIME_TYPE_KIND_INTERFACE
,
6069 gcc_assert(pif
->field_name() == "methods");
6071 Expression_list
* methods
= new Expression_list();
6072 if (this->methods_
!= NULL
&& !this->methods_
->empty())
6074 Type
* elemtype
= pif
->type()->array_type()->element_type();
6076 methods
->reserve(this->methods_
->size());
6077 for (Typed_identifier_list::const_iterator pm
= this->methods_
->begin();
6078 pm
!= this->methods_
->end();
6081 const Struct_field_list
* mfields
= elemtype
->struct_type()->fields();
6083 Expression_list
* mvals
= new Expression_list();
6086 Struct_field_list::const_iterator pmf
= mfields
->begin();
6087 gcc_assert(pmf
->field_name() == "name");
6088 std::string s
= Gogo::unpack_hidden_name(pm
->name());
6089 Expression
* e
= Expression::make_string(s
, bloc
);
6090 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6093 gcc_assert(pmf
->field_name() == "pkgPath");
6094 if (!Gogo::is_hidden_name(pm
->name()))
6095 mvals
->push_back(Expression::make_nil(bloc
));
6098 s
= Gogo::hidden_name_prefix(pm
->name());
6099 e
= Expression::make_string(s
, bloc
);
6100 mvals
->push_back(Expression::make_unary(OPERATOR_AND
, e
, bloc
));
6104 gcc_assert(pmf
->field_name() == "typ");
6105 mvals
->push_back(Expression::make_type_descriptor(pm
->type(), bloc
));
6108 gcc_assert(pmf
== mfields
->end());
6110 e
= Expression::make_struct_composite_literal(elemtype
, mvals
,
6112 methods
->push_back(e
);
6116 ivals
->push_back(Expression::make_slice_composite_literal(pif
->type(),
6120 gcc_assert(pif
== ifields
->end());
6122 return Expression::make_struct_composite_literal(itdt
, ivals
, bloc
);
6125 // Reflection string.
6128 Interface_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
6130 ret
->append("interface {");
6131 if (this->methods_
!= NULL
)
6133 for (Typed_identifier_list::const_iterator p
= this->methods_
->begin();
6134 p
!= this->methods_
->end();
6137 if (p
!= this->methods_
->begin())
6139 ret
->push_back(' ');
6140 ret
->append(Gogo::unpack_hidden_name(p
->name()));
6141 std::string sub
= p
->type()->reflection(gogo
);
6142 gcc_assert(sub
.compare(0, 4, "func") == 0);
6143 sub
= sub
.substr(4);
6153 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
6155 ret
->push_back('I');
6157 const Typed_identifier_list
* methods
= this->methods_
;
6158 if (methods
!= NULL
)
6160 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6161 p
!= methods
->end();
6164 std::string n
= Gogo::unpack_hidden_name(p
->name());
6166 snprintf(buf
, sizeof buf
, "%u_",
6167 static_cast<unsigned int>(n
.length()));
6170 this->append_mangled_name(p
->type(), gogo
, ret
);
6174 ret
->push_back('e');
6180 Interface_type::do_export(Export
* exp
) const
6182 exp
->write_c_string("interface { ");
6184 const Typed_identifier_list
* methods
= this->methods_
;
6185 if (methods
!= NULL
)
6187 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
6188 pm
!= methods
->end();
6191 exp
->write_string(pm
->name());
6192 exp
->write_c_string(" (");
6194 const Function_type
* fntype
= pm
->type()->function_type();
6197 const Typed_identifier_list
* parameters
= fntype
->parameters();
6198 if (parameters
!= NULL
)
6200 bool is_varargs
= fntype
->is_varargs();
6201 for (Typed_identifier_list::const_iterator pp
=
6202 parameters
->begin();
6203 pp
!= parameters
->end();
6209 exp
->write_c_string(", ");
6210 if (!is_varargs
|| pp
+ 1 != parameters
->end())
6211 exp
->write_type(pp
->type());
6214 exp
->write_c_string("...");
6215 Type
*pptype
= pp
->type();
6216 exp
->write_type(pptype
->array_type()->element_type());
6221 exp
->write_c_string(")");
6223 const Typed_identifier_list
* results
= fntype
->results();
6224 if (results
!= NULL
)
6226 exp
->write_c_string(" ");
6227 if (results
->size() == 1)
6228 exp
->write_type(results
->begin()->type());
6232 exp
->write_c_string("(");
6233 for (Typed_identifier_list::const_iterator p
=
6235 p
!= results
->end();
6241 exp
->write_c_string(", ");
6242 exp
->write_type(p
->type());
6244 exp
->write_c_string(")");
6248 exp
->write_c_string("; ");
6252 exp
->write_c_string("}");
6255 // Import an interface type.
6258 Interface_type::do_import(Import
* imp
)
6260 imp
->require_c_string("interface { ");
6262 Typed_identifier_list
* methods
= new Typed_identifier_list
;
6263 while (imp
->peek_char() != '}')
6265 std::string name
= imp
->read_identifier();
6266 imp
->require_c_string(" (");
6268 Typed_identifier_list
* parameters
;
6269 bool is_varargs
= false;
6270 if (imp
->peek_char() == ')')
6274 parameters
= new Typed_identifier_list
;
6277 if (imp
->match_c_string("..."))
6283 Type
* ptype
= imp
->read_type();
6285 ptype
= Type::make_array_type(ptype
, NULL
);
6286 parameters
->push_back(Typed_identifier(Import::import_marker
,
6287 ptype
, imp
->location()));
6288 if (imp
->peek_char() != ',')
6290 gcc_assert(!is_varargs
);
6291 imp
->require_c_string(", ");
6294 imp
->require_c_string(")");
6296 Typed_identifier_list
* results
;
6297 if (imp
->peek_char() != ' ')
6301 results
= new Typed_identifier_list
;
6303 if (imp
->peek_char() != '(')
6305 Type
* rtype
= imp
->read_type();
6306 results
->push_back(Typed_identifier(Import::import_marker
,
6307 rtype
, imp
->location()));
6314 Type
* rtype
= imp
->read_type();
6315 results
->push_back(Typed_identifier(Import::import_marker
,
6316 rtype
, imp
->location()));
6317 if (imp
->peek_char() != ',')
6319 imp
->require_c_string(", ");
6321 imp
->require_c_string(")");
6325 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
,
6329 fntype
->set_is_varargs();
6330 methods
->push_back(Typed_identifier(name
, fntype
, imp
->location()));
6332 imp
->require_c_string("; ");
6335 imp
->require_c_string("}");
6337 if (methods
->empty())
6343 return Type::make_interface_type(methods
, imp
->location());
6346 // Make an interface type.
6349 Type::make_interface_type(Typed_identifier_list
* methods
,
6350 source_location location
)
6352 return new Interface_type(methods
, location
);
6357 // Bind a method to an object.
6360 Method::bind_method(Expression
* expr
, source_location location
) const
6362 if (this->stub_
== NULL
)
6364 // When there is no stub object, the binding is determined by
6366 return this->do_bind_method(expr
, location
);
6369 Expression
* func
= Expression::make_func_reference(this->stub_
, NULL
,
6371 return Expression::make_bound_method(expr
, func
, location
);
6374 // Return the named object associated with a method. This may only be
6375 // called after methods are finalized.
6378 Method::named_object() const
6380 if (this->stub_
!= NULL
)
6382 return this->do_named_object();
6385 // Class Named_method.
6387 // The type of the method.
6390 Named_method::do_type() const
6392 if (this->named_object_
->is_function())
6393 return this->named_object_
->func_value()->type();
6394 else if (this->named_object_
->is_function_declaration())
6395 return this->named_object_
->func_declaration_value()->type();
6400 // Return the location of the method receiver.
6403 Named_method::do_receiver_location() const
6405 return this->do_type()->receiver()->location();
6408 // Bind a method to an object.
6411 Named_method::do_bind_method(Expression
* expr
, source_location location
) const
6413 Expression
* func
= Expression::make_func_reference(this->named_object_
, NULL
,
6415 Bound_method_expression
* bme
= Expression::make_bound_method(expr
, func
,
6417 // If this is not a local method, and it does not use a stub, then
6418 // the real method expects a different type. We need to cast the
6420 if (this->depth() > 0 && !this->needs_stub_method())
6422 Function_type
* ftype
= this->do_type();
6423 gcc_assert(ftype
->is_method());
6424 Type
* frtype
= ftype
->receiver()->type();
6425 bme
->set_first_argument_type(frtype
);
6430 // Class Interface_method.
6432 // Bind a method to an object.
6435 Interface_method::do_bind_method(Expression
* expr
,
6436 source_location location
) const
6438 return Expression::make_interface_field_reference(expr
, this->name_
,
6444 // Insert a new method. Return true if it was inserted, false
6448 Methods::insert(const std::string
& name
, Method
* m
)
6450 std::pair
<Method_map::iterator
, bool> ins
=
6451 this->methods_
.insert(std::make_pair(name
, m
));
6456 Method
* old_method
= ins
.first
->second
;
6457 if (m
->depth() < old_method
->depth())
6460 ins
.first
->second
= m
;
6465 if (m
->depth() == old_method
->depth())
6466 old_method
->set_is_ambiguous();
6472 // Return the number of unambiguous methods.
6475 Methods::count() const
6478 for (Method_map::const_iterator p
= this->methods_
.begin();
6479 p
!= this->methods_
.end();
6481 if (!p
->second
->is_ambiguous())
6486 // Class Named_type.
6488 // Return the name of the type.
6491 Named_type::name() const
6493 return this->named_object_
->name();
6496 // Return the name of the type to use in an error message.
6499 Named_type::message_name() const
6501 return this->named_object_
->message_name();
6504 // Return the base type for this type. We have to be careful about
6505 // circular type definitions, which are invalid but may be seen here.
6508 Named_type::named_base()
6513 Type
* ret
= this->type_
->base();
6514 this->seen_
= false;
6519 Named_type::named_base() const
6524 const Type
* ret
= this->type_
->base();
6525 this->seen_
= false;
6529 // Return whether this is an error type. We have to be careful about
6530 // circular type definitions, which are invalid but may be seen here.
6533 Named_type::is_named_error_type() const
6538 bool ret
= this->type_
->is_error_type();
6539 this->seen_
= false;
6543 // Add a method to this type.
6546 Named_type::add_method(const std::string
& name
, Function
* function
)
6548 if (this->local_methods_
== NULL
)
6549 this->local_methods_
= new Bindings(NULL
);
6550 return this->local_methods_
->add_function(name
, NULL
, function
);
6553 // Add a method declaration to this type.
6556 Named_type::add_method_declaration(const std::string
& name
, Package
* package
,
6557 Function_type
* type
,
6558 source_location location
)
6560 if (this->local_methods_
== NULL
)
6561 this->local_methods_
= new Bindings(NULL
);
6562 return this->local_methods_
->add_function_declaration(name
, package
, type
,
6566 // Add an existing method to this type.
6569 Named_type::add_existing_method(Named_object
* no
)
6571 if (this->local_methods_
== NULL
)
6572 this->local_methods_
= new Bindings(NULL
);
6573 this->local_methods_
->add_named_object(no
);
6576 // Look for a local method NAME, and returns its named object, or NULL
6580 Named_type::find_local_method(const std::string
& name
) const
6582 if (this->local_methods_
== NULL
)
6584 return this->local_methods_
->lookup(name
);
6587 // Return whether NAME is an unexported field or method, for better
6591 Named_type::is_unexported_local_method(Gogo
* gogo
,
6592 const std::string
& name
) const
6594 Bindings
* methods
= this->local_methods_
;
6595 if (methods
!= NULL
)
6597 for (Bindings::const_declarations_iterator p
=
6598 methods
->begin_declarations();
6599 p
!= methods
->end_declarations();
6602 if (Gogo::is_hidden_name(p
->first
)
6603 && name
== Gogo::unpack_hidden_name(p
->first
)
6604 && gogo
->pack_hidden_name(name
, false) != p
->first
)
6611 // Build the complete list of methods for this type, which means
6612 // recursively including all methods for anonymous fields. Create all
6616 Named_type::finalize_methods(Gogo
* gogo
)
6618 if (this->local_methods_
!= NULL
6619 && (this->points_to() != NULL
|| this->interface_type() != NULL
))
6621 const Bindings
* lm
= this->local_methods_
;
6622 for (Bindings::const_declarations_iterator p
= lm
->begin_declarations();
6623 p
!= lm
->end_declarations();
6625 error_at(p
->second
->location(),
6626 "invalid pointer or interface receiver type");
6627 delete this->local_methods_
;
6628 this->local_methods_
= NULL
;
6632 Type::finalize_methods(gogo
, this, this->location_
, &this->all_methods_
);
6635 // Return the method NAME, or NULL if there isn't one or if it is
6636 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6640 Named_type::method_function(const std::string
& name
, bool* is_ambiguous
) const
6642 return Type::method_function(this->all_methods_
, name
, is_ambiguous
);
6645 // Return a pointer to the interface method table for this type for
6646 // the interface INTERFACE. IS_POINTER is true if this is for a
6650 Named_type::interface_method_table(Gogo
* gogo
, const Interface_type
* interface
,
6653 gcc_assert(!interface
->is_empty());
6655 Interface_method_tables
** pimt
= (is_pointer
6656 ? &this->interface_method_tables_
6657 : &this->pointer_interface_method_tables_
);
6660 *pimt
= new Interface_method_tables(5);
6662 std::pair
<const Interface_type
*, tree
> val(interface
, NULL_TREE
);
6663 std::pair
<Interface_method_tables::iterator
, bool> ins
= (*pimt
)->insert(val
);
6667 // This is a new entry in the hash table.
6668 gcc_assert(ins
.first
->second
== NULL_TREE
);
6669 ins
.first
->second
= gogo
->interface_method_table_for_type(interface
,
6674 tree decl
= ins
.first
->second
;
6675 if (decl
== error_mark_node
)
6676 return error_mark_node
;
6677 gcc_assert(decl
!= NULL_TREE
&& TREE_CODE(decl
) == VAR_DECL
);
6678 return build_fold_addr_expr(decl
);
6681 // Return whether a named type has any hidden fields.
6684 Named_type::named_type_has_hidden_fields(std::string
* reason
) const
6689 bool ret
= this->type_
->has_hidden_fields(this, reason
);
6690 this->seen_
= false;
6694 // Look for a use of a complete type within another type. This is
6695 // used to check that we don't try to use a type within itself.
6697 class Find_type_use
: public Traverse
6700 Find_type_use(Type
* find_type
)
6701 : Traverse(traverse_types
),
6702 find_type_(find_type
), found_(false)
6705 // Whether we found the type.
6708 { return this->found_
; }
6715 // The type we are looking for.
6717 // Whether we found the type.
6721 // Check for FIND_TYPE in TYPE.
6724 Find_type_use::type(Type
* type
)
6726 if (this->find_type_
== type
)
6728 this->found_
= true;
6729 return TRAVERSE_EXIT
;
6731 // It's OK if we see a reference to the type in any type which is
6732 // essentially a pointer: a pointer, a slice, a function, a map, or
6734 if (type
->points_to() != NULL
6735 || type
->is_open_array_type()
6736 || type
->function_type() != NULL
6737 || type
->map_type() != NULL
6738 || type
->channel_type() != NULL
)
6739 return TRAVERSE_SKIP_COMPONENTS
;
6741 // For an interface, a reference to the type in a method type should
6742 // be ignored, but we have to consider direct inheritance. When
6743 // this is called, there may be cases of direct inheritance
6744 // represented as a method with no name.
6745 if (type
->interface_type() != NULL
)
6747 const Typed_identifier_list
* methods
= type
->interface_type()->methods();
6748 if (methods
!= NULL
)
6750 for (Typed_identifier_list::const_iterator p
= methods
->begin();
6751 p
!= methods
->end();
6754 if (p
->name().empty())
6756 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
6757 return TRAVERSE_EXIT
;
6761 return TRAVERSE_SKIP_COMPONENTS
;
6764 return TRAVERSE_CONTINUE
;
6767 // Verify that a named type does not refer to itself.
6770 Named_type::do_verify()
6772 Find_type_use
find(this);
6773 Type::traverse(this->type_
, &find
);
6776 error_at(this->location_
, "invalid recursive type %qs",
6777 this->message_name().c_str());
6778 this->is_error_
= true;
6782 // Check whether any of the local methods overloads an existing
6783 // struct field or interface method. We don't need to check the
6784 // list of methods against itself: that is handled by the Bindings
6786 if (this->local_methods_
!= NULL
)
6788 Struct_type
* st
= this->type_
->struct_type();
6789 Interface_type
* it
= this->type_
->interface_type();
6790 bool found_dup
= false;
6791 if (st
!= NULL
|| it
!= NULL
)
6793 for (Bindings::const_declarations_iterator p
=
6794 this->local_methods_
->begin_declarations();
6795 p
!= this->local_methods_
->end_declarations();
6798 const std::string
& name(p
->first
);
6799 if (st
!= NULL
&& st
->find_local_field(name
, NULL
) != NULL
)
6801 error_at(p
->second
->location(),
6802 "method %qs redeclares struct field name",
6803 Gogo::message_name(name
).c_str());
6806 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
6808 error_at(p
->second
->location(),
6809 "method %qs redeclares interface method name",
6810 Gogo::message_name(name
).c_str());
6822 // Return a hash code. This is used for method lookup. We simply
6823 // hash on the name itself.
6826 Named_type::do_hash_for_method(Gogo
* gogo
) const
6828 const std::string
& name(this->named_object()->name());
6829 unsigned int ret
= Type::hash_string(name
, 0);
6831 // GOGO will be NULL here when called from Type_hash_identical.
6832 // That is OK because that is only used for internal hash tables
6833 // where we are going to be comparing named types for equality. In
6834 // other cases, which are cases where the runtime is going to
6835 // compare hash codes to see if the types are the same, we need to
6836 // include the package prefix and name in the hash.
6837 if (gogo
!= NULL
&& !Gogo::is_hidden_name(name
) && !this->is_builtin())
6839 const Package
* package
= this->named_object()->package();
6840 if (package
== NULL
)
6842 ret
= Type::hash_string(gogo
->unique_prefix(), ret
);
6843 ret
= Type::hash_string(gogo
->package_name(), ret
);
6847 ret
= Type::hash_string(package
->unique_prefix(), ret
);
6848 ret
= Type::hash_string(package
->name(), ret
);
6855 // Get a tree for a named type.
6858 Named_type::do_get_tree(Gogo
* gogo
)
6860 if (this->is_error_
)
6861 return error_mark_node
;
6863 // Go permits types to refer to themselves in various ways. Break
6864 // the recursion here.
6866 switch (this->type_
->forwarded()->classification())
6869 return error_mark_node
;
6878 // These types can not refer to themselves.
6881 // All maps and channels have the same type in GENERIC.
6882 t
= Type::get_named_type_tree(gogo
, this->type_
);
6883 if (t
== error_mark_node
)
6884 return error_mark_node
;
6885 // Build a copy to set TYPE_NAME.
6886 t
= build_variant_type_copy(t
);
6890 // Don't recur infinitely if a function type refers to itself.
6891 // Ideally we would build a circular data structure here, but
6892 // GENERIC can't handle them.
6894 return ptr_type_node
;
6896 t
= Type::get_named_type_tree(gogo
, this->type_
);
6897 this->seen_
= false;
6898 if (t
== error_mark_node
)
6899 return error_mark_node
;
6900 t
= build_variant_type_copy(t
);
6904 // Don't recur infinitely if a pointer type refers to itself.
6905 // Ideally we would build a circular data structure here, but
6906 // GENERIC can't handle them.
6908 return ptr_type_node
;
6910 t
= Type::get_named_type_tree(gogo
, this->type_
);
6911 this->seen_
= false;
6912 if (t
== error_mark_node
)
6913 return error_mark_node
;
6914 t
= build_variant_type_copy(t
);
6918 if (this->named_tree_
!= NULL_TREE
)
6919 return this->named_tree_
;
6920 t
= make_node(RECORD_TYPE
);
6921 this->named_tree_
= t
;
6922 t
= this->type_
->struct_type()->fill_in_tree(gogo
, t
);
6923 if (t
== error_mark_node
)
6924 return error_mark_node
;
6928 if (!this->is_open_array_type())
6929 t
= Type::get_named_type_tree(gogo
, this->type_
);
6932 if (this->named_tree_
!= NULL_TREE
)
6933 return this->named_tree_
;
6934 t
= gogo
->slice_type_tree(void_type_node
);
6935 this->named_tree_
= t
;
6936 t
= this->type_
->array_type()->fill_in_tree(gogo
, t
);
6938 if (t
== error_mark_node
)
6939 return error_mark_node
;
6940 t
= build_variant_type_copy(t
);
6943 case TYPE_INTERFACE
:
6944 if (this->type_
->interface_type()->is_empty())
6946 t
= Type::get_named_type_tree(gogo
, this->type_
);
6947 if (t
== error_mark_node
)
6948 return error_mark_node
;
6949 t
= build_variant_type_copy(t
);
6953 if (this->named_tree_
!= NULL_TREE
)
6954 return this->named_tree_
;
6955 t
= make_node(RECORD_TYPE
);
6956 this->named_tree_
= t
;
6957 t
= this->type_
->interface_type()->fill_in_tree(gogo
, t
);
6958 if (t
== error_mark_node
)
6959 return error_mark_node
;
6965 // When a named type T1 is defined as another named type T2,
6966 // the definition must simply be "type T1 T2". If the
6967 // definition of T2 may refer to T1, then we must simply
6968 // return the type for T2 here. It's not precisely correct,
6969 // but it's as close as we can get with GENERIC.
6970 bool was_seen
= this->seen_
;
6972 t
= Type::get_named_type_tree(gogo
, this->type_
);
6973 this->seen_
= was_seen
;
6976 if (t
== error_mark_node
)
6977 return error_mark_node
;
6978 t
= build_variant_type_copy(t
);
6983 // An undefined forwarding type. Make sure the error is
6985 this->type_
->forward_declaration_type()->real_type();
6986 return error_mark_node
;
6990 case TYPE_CALL_MULTIPLE_RESULT
:
6994 tree id
= this->named_object_
->get_id(gogo
);
6995 tree decl
= build_decl(this->location_
, TYPE_DECL
, id
, t
);
6996 TYPE_NAME(t
) = decl
;
7001 // Build a type descriptor for a named type.
7004 Named_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
7006 // If NAME is not NULL, then we don't really want the type
7007 // descriptor for this type; we want the descriptor for the
7008 // underlying type, giving it the name NAME.
7009 return this->named_type_descriptor(gogo
, this->type_
,
7010 name
== NULL
? this : name
);
7013 // Add to the reflection string. This is used mostly for the name of
7014 // the type used in a type descriptor, not for actual reflection
7018 Named_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
7020 if (this->location() != BUILTINS_LOCATION
)
7022 const Package
* package
= this->named_object_
->package();
7023 if (package
!= NULL
)
7024 ret
->append(package
->name());
7026 ret
->append(gogo
->package_name());
7027 ret
->push_back('.');
7029 if (this->in_function_
!= NULL
)
7031 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7032 ret
->push_back('$');
7034 ret
->append(Gogo::unpack_hidden_name(this->named_object_
->name()));
7037 // Get the mangled name.
7040 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
7042 Named_object
* no
= this->named_object_
;
7044 if (this->location() == BUILTINS_LOCATION
)
7045 gcc_assert(this->in_function_
== NULL
);
7048 const std::string
& unique_prefix(no
->package() == NULL
7049 ? gogo
->unique_prefix()
7050 : no
->package()->unique_prefix());
7051 const std::string
& package_name(no
->package() == NULL
7052 ? gogo
->package_name()
7053 : no
->package()->name());
7054 name
= unique_prefix
;
7055 name
.append(1, '.');
7056 name
.append(package_name
);
7057 name
.append(1, '.');
7058 if (this->in_function_
!= NULL
)
7060 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
7061 name
.append(1, '$');
7064 name
.append(Gogo::unpack_hidden_name(no
->name()));
7066 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
7071 // Export the type. This is called to export a global type.
7074 Named_type::export_named_type(Export
* exp
, const std::string
&) const
7076 // We don't need to write the name of the type here, because it will
7077 // be written by Export::write_type anyhow.
7078 exp
->write_c_string("type ");
7079 exp
->write_type(this);
7080 exp
->write_c_string(";\n");
7083 // Import a named type.
7086 Named_type::import_named_type(Import
* imp
, Named_type
** ptype
)
7088 imp
->require_c_string("type ");
7089 Type
*type
= imp
->read_type();
7090 *ptype
= type
->named_type();
7091 gcc_assert(*ptype
!= NULL
);
7092 imp
->require_c_string(";\n");
7095 // Export the type when it is referenced by another type. In this
7096 // case Export::export_type will already have issued the name.
7099 Named_type::do_export(Export
* exp
) const
7101 exp
->write_type(this->type_
);
7103 // To save space, we only export the methods directly attached to
7105 Bindings
* methods
= this->local_methods_
;
7106 if (methods
== NULL
)
7109 exp
->write_c_string("\n");
7110 for (Bindings::const_definitions_iterator p
= methods
->begin_definitions();
7111 p
!= methods
->end_definitions();
7114 exp
->write_c_string(" ");
7115 (*p
)->export_named_object(exp
);
7118 for (Bindings::const_declarations_iterator p
= methods
->begin_declarations();
7119 p
!= methods
->end_declarations();
7122 if (p
->second
->is_function_declaration())
7124 exp
->write_c_string(" ");
7125 p
->second
->export_named_object(exp
);
7130 // Make a named type.
7133 Type::make_named_type(Named_object
* named_object
, Type
* type
,
7134 source_location location
)
7136 return new Named_type(named_object
, type
, location
);
7139 // Finalize the methods for TYPE. It will be a named type or a struct
7140 // type. This sets *ALL_METHODS to the list of methods, and builds
7141 // all required stubs.
7144 Type::finalize_methods(Gogo
* gogo
, const Type
* type
, source_location location
,
7145 Methods
** all_methods
)
7147 *all_methods
= NULL
;
7148 Types_seen types_seen
;
7149 Type::add_methods_for_type(type
, NULL
, 0, false, false, &types_seen
,
7151 Type::build_stub_methods(gogo
, type
, *all_methods
, location
);
7154 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7155 // build up the struct field indexes as we go. DEPTH is the depth of
7156 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7157 // adding these methods for an anonymous field with pointer type.
7158 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7159 // calls the real method. TYPES_SEEN is used to avoid infinite
7163 Type::add_methods_for_type(const Type
* type
,
7164 const Method::Field_indexes
* field_indexes
,
7166 bool is_embedded_pointer
,
7167 bool needs_stub_method
,
7168 Types_seen
* types_seen
,
7171 // Pointer types may not have methods.
7172 if (type
->points_to() != NULL
)
7175 const Named_type
* nt
= type
->named_type();
7178 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
->insert(nt
);
7184 Type::add_local_methods_for_type(nt
, field_indexes
, depth
,
7185 is_embedded_pointer
, needs_stub_method
,
7188 Type::add_embedded_methods_for_type(type
, field_indexes
, depth
,
7189 is_embedded_pointer
, needs_stub_method
,
7190 types_seen
, methods
);
7192 // If we are called with depth > 0, then we are looking at an
7193 // anonymous field of a struct. If such a field has interface type,
7194 // then we need to add the interface methods. We don't want to add
7195 // them when depth == 0, because we will already handle them
7196 // following the usual rules for an interface type.
7198 Type::add_interface_methods_for_type(type
, field_indexes
, depth
, methods
);
7201 // Add the local methods for the named type NT to *METHODS. The
7202 // parameters are as for add_methods_to_type.
7205 Type::add_local_methods_for_type(const Named_type
* nt
,
7206 const Method::Field_indexes
* field_indexes
,
7208 bool is_embedded_pointer
,
7209 bool needs_stub_method
,
7212 const Bindings
* local_methods
= nt
->local_methods();
7213 if (local_methods
== NULL
)
7216 if (*methods
== NULL
)
7217 *methods
= new Methods();
7219 for (Bindings::const_declarations_iterator p
=
7220 local_methods
->begin_declarations();
7221 p
!= local_methods
->end_declarations();
7224 Named_object
* no
= p
->second
;
7225 bool is_value_method
= (is_embedded_pointer
7226 || !Type::method_expects_pointer(no
));
7227 Method
* m
= new Named_method(no
, field_indexes
, depth
, is_value_method
,
7229 || (depth
> 0 && is_value_method
)));
7230 if (!(*methods
)->insert(no
->name(), m
))
7235 // Add the embedded methods for TYPE to *METHODS. These are the
7236 // methods attached to anonymous fields. The parameters are as for
7237 // add_methods_to_type.
7240 Type::add_embedded_methods_for_type(const Type
* type
,
7241 const Method::Field_indexes
* field_indexes
,
7243 bool is_embedded_pointer
,
7244 bool needs_stub_method
,
7245 Types_seen
* types_seen
,
7248 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7250 const Struct_type
* st
= type
->struct_type();
7254 const Struct_field_list
* fields
= st
->fields();
7259 for (Struct_field_list::const_iterator pf
= fields
->begin();
7260 pf
!= fields
->end();
7263 if (!pf
->is_anonymous())
7266 Type
* ftype
= pf
->type();
7267 bool is_pointer
= false;
7268 if (ftype
->points_to() != NULL
)
7270 ftype
= ftype
->points_to();
7273 Named_type
* fnt
= ftype
->named_type();
7276 // This is an error, but it will be diagnosed elsewhere.
7280 Method::Field_indexes
* sub_field_indexes
= new Method::Field_indexes();
7281 sub_field_indexes
->next
= field_indexes
;
7282 sub_field_indexes
->field_index
= i
;
7284 Type::add_methods_for_type(fnt
, sub_field_indexes
, depth
+ 1,
7285 (is_embedded_pointer
|| is_pointer
),
7294 // If TYPE is an interface type, then add its method to *METHODS.
7295 // This is for interface methods attached to an anonymous field. The
7296 // parameters are as for add_methods_for_type.
7299 Type::add_interface_methods_for_type(const Type
* type
,
7300 const Method::Field_indexes
* field_indexes
,
7304 const Interface_type
* it
= type
->interface_type();
7308 const Typed_identifier_list
* imethods
= it
->methods();
7309 if (imethods
== NULL
)
7312 if (*methods
== NULL
)
7313 *methods
= new Methods();
7315 for (Typed_identifier_list::const_iterator pm
= imethods
->begin();
7316 pm
!= imethods
->end();
7319 Function_type
* fntype
= pm
->type()->function_type();
7320 gcc_assert(fntype
!= NULL
&& !fntype
->is_method());
7321 fntype
= fntype
->copy_with_receiver(const_cast<Type
*>(type
));
7322 Method
* m
= new Interface_method(pm
->name(), pm
->location(), fntype
,
7323 field_indexes
, depth
);
7324 if (!(*methods
)->insert(pm
->name(), m
))
7329 // Build stub methods for TYPE as needed. METHODS is the set of
7330 // methods for the type. A stub method may be needed when a type
7331 // inherits a method from an anonymous field. When we need the
7332 // address of the method, as in a type descriptor, we need to build a
7333 // little stub which does the required field dereferences and jumps to
7334 // the real method. LOCATION is the location of the type definition.
7337 Type::build_stub_methods(Gogo
* gogo
, const Type
* type
, const Methods
* methods
,
7338 source_location location
)
7340 if (methods
== NULL
)
7342 for (Methods::const_iterator p
= methods
->begin();
7343 p
!= methods
->end();
7346 Method
* m
= p
->second
;
7347 if (m
->is_ambiguous() || !m
->needs_stub_method())
7350 const std::string
& name(p
->first
);
7352 // Build a stub method.
7354 const Function_type
* fntype
= m
->type();
7356 static unsigned int counter
;
7358 snprintf(buf
, sizeof buf
, "$this%u", counter
);
7361 Type
* receiver_type
= const_cast<Type
*>(type
);
7362 if (!m
->is_value_method())
7363 receiver_type
= Type::make_pointer_type(receiver_type
);
7364 source_location receiver_location
= m
->receiver_location();
7365 Typed_identifier
* receiver
= new Typed_identifier(buf
, receiver_type
,
7368 const Typed_identifier_list
* fnparams
= fntype
->parameters();
7369 Typed_identifier_list
* stub_params
;
7370 if (fnparams
== NULL
|| fnparams
->empty())
7374 // We give each stub parameter a unique name.
7375 stub_params
= new Typed_identifier_list();
7376 for (Typed_identifier_list::const_iterator pp
= fnparams
->begin();
7377 pp
!= fnparams
->end();
7381 snprintf(pbuf
, sizeof pbuf
, "$p%u", counter
);
7382 stub_params
->push_back(Typed_identifier(pbuf
, pp
->type(),
7388 const Typed_identifier_list
* fnresults
= fntype
->results();
7389 Typed_identifier_list
* stub_results
;
7390 if (fnresults
== NULL
|| fnresults
->empty())
7391 stub_results
= NULL
;
7394 // We create the result parameters without any names, since
7395 // we won't refer to them.
7396 stub_results
= new Typed_identifier_list();
7397 for (Typed_identifier_list::const_iterator pr
= fnresults
->begin();
7398 pr
!= fnresults
->end();
7400 stub_results
->push_back(Typed_identifier("", pr
->type(),
7404 Function_type
* stub_type
= Type::make_function_type(receiver
,
7407 fntype
->location());
7408 if (fntype
->is_varargs())
7409 stub_type
->set_is_varargs();
7411 // We only create the function in the package which creates the
7413 const Package
* package
;
7414 if (type
->named_type() == NULL
)
7417 package
= type
->named_type()->named_object()->package();
7419 if (package
!= NULL
)
7420 stub
= Named_object::make_function_declaration(name
, package
,
7421 stub_type
, location
);
7424 stub
= gogo
->start_function(name
, stub_type
, false,
7425 fntype
->location());
7426 Type::build_one_stub_method(gogo
, m
, buf
, stub_params
,
7427 fntype
->is_varargs(), location
);
7428 gogo
->finish_function(fntype
->location());
7431 m
->set_stub_object(stub
);
7435 // Build a stub method which adjusts the receiver as required to call
7436 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7437 // PARAMS is the list of function parameters.
7440 Type::build_one_stub_method(Gogo
* gogo
, Method
* method
,
7441 const char* receiver_name
,
7442 const Typed_identifier_list
* params
,
7444 source_location location
)
7446 Named_object
* receiver_object
= gogo
->lookup(receiver_name
, NULL
);
7447 gcc_assert(receiver_object
!= NULL
);
7449 Expression
* expr
= Expression::make_var_reference(receiver_object
, location
);
7450 expr
= Type::apply_field_indexes(expr
, method
->field_indexes(), location
);
7451 if (expr
->type()->points_to() == NULL
)
7452 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7454 Expression_list
* arguments
;
7455 if (params
== NULL
|| params
->empty())
7459 arguments
= new Expression_list();
7460 for (Typed_identifier_list::const_iterator p
= params
->begin();
7464 Named_object
* param
= gogo
->lookup(p
->name(), NULL
);
7465 gcc_assert(param
!= NULL
);
7466 Expression
* param_ref
= Expression::make_var_reference(param
,
7468 arguments
->push_back(param_ref
);
7472 Expression
* func
= method
->bind_method(expr
, location
);
7473 gcc_assert(func
!= NULL
);
7474 Call_expression
* call
= Expression::make_call(func
, arguments
, is_varargs
,
7476 size_t count
= call
->result_count();
7478 gogo
->add_statement(Statement::make_statement(call
));
7481 Expression_list
* retvals
= new Expression_list();
7483 retvals
->push_back(call
);
7486 for (size_t i
= 0; i
< count
; ++i
)
7487 retvals
->push_back(Expression::make_call_result(call
, i
));
7489 const Function
* function
= gogo
->current_function()->func_value();
7490 const Typed_identifier_list
* results
= function
->type()->results();
7491 Statement
* retstat
= Statement::make_return_statement(results
, retvals
,
7493 gogo
->add_statement(retstat
);
7497 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7498 // in reverse order.
7501 Type::apply_field_indexes(Expression
* expr
,
7502 const Method::Field_indexes
* field_indexes
,
7503 source_location location
)
7505 if (field_indexes
== NULL
)
7507 expr
= Type::apply_field_indexes(expr
, field_indexes
->next
, location
);
7508 Struct_type
* stype
= expr
->type()->deref()->struct_type();
7509 gcc_assert(stype
!= NULL
7510 && field_indexes
->field_index
< stype
->field_count());
7511 if (expr
->type()->struct_type() == NULL
)
7513 gcc_assert(expr
->type()->points_to() != NULL
);
7514 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7515 gcc_assert(expr
->type()->struct_type() == stype
);
7517 return Expression::make_field_reference(expr
, field_indexes
->field_index
,
7521 // Return whether NO is a method for which the receiver is a pointer.
7524 Type::method_expects_pointer(const Named_object
* no
)
7526 const Function_type
*fntype
;
7527 if (no
->is_function())
7528 fntype
= no
->func_value()->type();
7529 else if (no
->is_function_declaration())
7530 fntype
= no
->func_declaration_value()->type();
7533 return fntype
->receiver()->type()->points_to() != NULL
;
7536 // Given a set of methods for a type, METHODS, return the method NAME,
7537 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7538 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7539 // but is ambiguous (and return NULL).
7542 Type::method_function(const Methods
* methods
, const std::string
& name
,
7545 if (is_ambiguous
!= NULL
)
7546 *is_ambiguous
= false;
7547 if (methods
== NULL
)
7549 Methods::const_iterator p
= methods
->find(name
);
7550 if (p
== methods
->end())
7552 Method
* m
= p
->second
;
7553 if (m
->is_ambiguous())
7555 if (is_ambiguous
!= NULL
)
7556 *is_ambiguous
= true;
7562 // Look for field or method NAME for TYPE. Return an Expression for
7563 // the field or method bound to EXPR. If there is no such field or
7564 // method, give an appropriate error and return an error expression.
7567 Type::bind_field_or_method(Gogo
* gogo
, const Type
* type
, Expression
* expr
,
7568 const std::string
& name
,
7569 source_location location
)
7571 if (type
->deref()->is_error_type())
7572 return Expression::make_error(location
);
7574 const Named_type
* nt
= type
->named_type();
7576 nt
= type
->deref()->named_type();
7577 const Struct_type
* st
= type
->deref()->struct_type();
7578 const Interface_type
* it
= type
->deref()->interface_type();
7580 // If this is a pointer to a pointer, then it is possible that the
7581 // pointed-to type has methods.
7585 && type
->points_to() != NULL
7586 && type
->points_to()->points_to() != NULL
)
7588 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, location
);
7589 type
= type
->points_to();
7590 nt
= type
->points_to()->named_type();
7591 st
= type
->points_to()->struct_type();
7592 it
= type
->points_to()->interface_type();
7595 bool receiver_can_be_pointer
= (expr
->type()->points_to() != NULL
7596 || expr
->is_addressable());
7597 bool is_method
= false;
7598 bool found_pointer_method
= false;
7601 if (Type::find_field_or_method(type
, name
, receiver_can_be_pointer
, NULL
,
7602 &is_method
, &found_pointer_method
,
7608 gcc_assert(st
!= NULL
);
7609 if (type
->struct_type() == NULL
)
7611 gcc_assert(type
->points_to() != NULL
);
7612 expr
= Expression::make_unary(OPERATOR_MULT
, expr
,
7614 gcc_assert(expr
->type()->struct_type() == st
);
7616 ret
= st
->field_reference(expr
, name
, location
);
7618 else if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7619 ret
= Expression::make_interface_field_reference(expr
, name
,
7625 m
= nt
->method_function(name
, NULL
);
7626 else if (st
!= NULL
)
7627 m
= st
->method_function(name
, NULL
);
7630 gcc_assert(m
!= NULL
);
7631 if (!m
->is_value_method() && expr
->type()->points_to() == NULL
)
7632 expr
= Expression::make_unary(OPERATOR_AND
, expr
, location
);
7633 ret
= m
->bind_method(expr
, location
);
7635 gcc_assert(ret
!= NULL
);
7640 if (!ambig1
.empty())
7641 error_at(location
, "%qs is ambiguous via %qs and %qs",
7642 Gogo::message_name(name
).c_str(),
7643 Gogo::message_name(ambig1
).c_str(),
7644 Gogo::message_name(ambig2
).c_str());
7645 else if (found_pointer_method
)
7646 error_at(location
, "method requires a pointer");
7647 else if (nt
== NULL
&& st
== NULL
&& it
== NULL
)
7649 ("reference to field %qs in object which "
7650 "has no fields or methods"),
7651 Gogo::message_name(name
).c_str());
7655 if (!Gogo::is_hidden_name(name
))
7656 is_unexported
= false;
7659 std::string unpacked
= Gogo::unpack_hidden_name(name
);
7660 is_unexported
= Type::is_unexported_field_or_method(gogo
, type
,
7664 error_at(location
, "reference to unexported field or method %qs",
7665 Gogo::message_name(name
).c_str());
7667 error_at(location
, "reference to undefined field or method %qs",
7668 Gogo::message_name(name
).c_str());
7670 return Expression::make_error(location
);
7674 // Look in TYPE for a field or method named NAME, return true if one
7675 // is found. This looks through embedded anonymous fields and handles
7676 // ambiguity. If a method is found, sets *IS_METHOD to true;
7677 // otherwise, if a field is found, set it to false. If
7678 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7679 // whose address can not be taken. When returning false, this sets
7680 // *FOUND_POINTER_METHOD if we found a method we couldn't use because
7681 // it requires a pointer. LEVEL is used for recursive calls, and can
7682 // be NULL for a non-recursive call. When this function returns false
7683 // because it finds that the name is ambiguous, it will store a path
7684 // to the ambiguous names in *AMBIG1 and *AMBIG2. If the name is not
7685 // found at all, *AMBIG1 and *AMBIG2 will be unchanged.
7687 // This function just returns whether or not there is a field or
7688 // method, and whether it is a field or method. It doesn't build an
7689 // expression to refer to it. If it is a method, we then look in the
7690 // list of all methods for the type. If it is a field, the search has
7691 // to be done again, looking only for fields, and building up the
7692 // expression as we go.
7695 Type::find_field_or_method(const Type
* type
,
7696 const std::string
& name
,
7697 bool receiver_can_be_pointer
,
7700 bool* found_pointer_method
,
7701 std::string
* ambig1
,
7702 std::string
* ambig2
)
7704 // Named types can have locally defined methods.
7705 const Named_type
* nt
= type
->named_type();
7706 if (nt
== NULL
&& type
->points_to() != NULL
)
7707 nt
= type
->points_to()->named_type();
7710 Named_object
* no
= nt
->find_local_method(name
);
7713 if (receiver_can_be_pointer
|| !Type::method_expects_pointer(no
))
7719 // Record that we have found a pointer method in order to
7720 // give a better error message if we don't find anything
7722 *found_pointer_method
= true;
7726 // Interface types can have methods.
7727 const Interface_type
* it
= type
->deref()->interface_type();
7728 if (it
!= NULL
&& it
->find_method(name
) != NULL
)
7734 // Struct types can have fields. They can also inherit fields and
7735 // methods from anonymous fields.
7736 const Struct_type
* st
= type
->deref()->struct_type();
7739 const Struct_field_list
* fields
= st
->fields();
7743 int found_level
= 0;
7744 bool found_is_method
= false;
7745 std::string found_ambig1
;
7746 std::string found_ambig2
;
7747 const Struct_field
* found_parent
= NULL
;
7748 for (Struct_field_list::const_iterator pf
= fields
->begin();
7749 pf
!= fields
->end();
7752 if (pf
->field_name() == name
)
7758 if (!pf
->is_anonymous())
7761 if (pf
->type()->deref()->is_error_type()
7762 || pf
->type()->deref()->is_undefined())
7765 Named_type
* fnt
= pf
->type()->deref()->named_type();
7766 gcc_assert(fnt
!= NULL
);
7768 int sublevel
= level
== NULL
? 1 : *level
+ 1;
7770 std::string subambig1
;
7771 std::string subambig2
;
7772 bool subfound
= Type::find_field_or_method(fnt
,
7774 receiver_can_be_pointer
,
7777 found_pointer_method
,
7782 if (!subambig1
.empty())
7784 // The name was found via this field, but is ambiguous.
7785 // if the ambiguity is lower or at the same level as
7786 // anything else we have already found, then we want to
7787 // pass the ambiguity back to the caller.
7788 if (found_level
== 0 || sublevel
<= found_level
)
7790 found_ambig1
= pf
->field_name() + '.' + subambig1
;
7791 found_ambig2
= pf
->field_name() + '.' + subambig2
;
7792 found_level
= sublevel
;
7798 // The name was found via this field. Use the level to see
7799 // if we want to use this one, or whether it introduces an
7801 if (found_level
== 0 || sublevel
< found_level
)
7803 found_level
= sublevel
;
7804 found_is_method
= sub_is_method
;
7805 found_ambig1
.clear();
7806 found_ambig2
.clear();
7807 found_parent
= &*pf
;
7809 else if (sublevel
> found_level
)
7811 else if (found_ambig1
.empty())
7813 // We found an ambiguity.
7814 gcc_assert(found_parent
!= NULL
);
7815 found_ambig1
= found_parent
->field_name();
7816 found_ambig2
= pf
->field_name();
7820 // We found an ambiguity, but we already know of one.
7821 // Just report the earlier one.
7826 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7827 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7828 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7829 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7831 if (found_level
== 0)
7833 else if (!found_ambig1
.empty())
7835 gcc_assert(!found_ambig1
.empty());
7836 ambig1
->assign(found_ambig1
);
7837 ambig2
->assign(found_ambig2
);
7839 *level
= found_level
;
7845 *level
= found_level
;
7846 *is_method
= found_is_method
;
7851 // Return whether NAME is an unexported field or method for TYPE.
7854 Type::is_unexported_field_or_method(Gogo
* gogo
, const Type
* type
,
7855 const std::string
& name
)
7857 type
= type
->deref();
7859 const Named_type
* nt
= type
->named_type();
7860 if (nt
!= NULL
&& nt
->is_unexported_local_method(gogo
, name
))
7863 const Interface_type
* it
= type
->interface_type();
7864 if (it
!= NULL
&& it
->is_unexported_method(gogo
, name
))
7867 const Struct_type
* st
= type
->struct_type();
7868 if (st
!= NULL
&& st
->is_unexported_local_field(gogo
, name
))
7874 const Struct_field_list
* fields
= st
->fields();
7878 for (Struct_field_list::const_iterator pf
= fields
->begin();
7879 pf
!= fields
->end();
7882 if (pf
->is_anonymous()
7883 && (!pf
->type()->deref()->is_error_type()
7884 && !pf
->type()->deref()->is_undefined()))
7886 Named_type
* subtype
= pf
->type()->deref()->named_type();
7887 gcc_assert(subtype
!= NULL
);
7888 if (Type::is_unexported_field_or_method(gogo
, subtype
, name
))
7896 // Class Forward_declaration.
7898 Forward_declaration_type::Forward_declaration_type(Named_object
* named_object
)
7899 : Type(TYPE_FORWARD
),
7900 named_object_(named_object
->resolve()), warned_(false)
7902 gcc_assert(this->named_object_
->is_unknown()
7903 || this->named_object_
->is_type_declaration());
7906 // Return the named object.
7909 Forward_declaration_type::named_object()
7911 return this->named_object_
->resolve();
7915 Forward_declaration_type::named_object() const
7917 return this->named_object_
->resolve();
7920 // Return the name of the forward declared type.
7923 Forward_declaration_type::name() const
7925 return this->named_object()->name();
7928 // Warn about a use of a type which has been declared but not defined.
7931 Forward_declaration_type::warn() const
7933 Named_object
* no
= this->named_object_
->resolve();
7934 if (no
->is_unknown())
7936 // The name was not defined anywhere.
7939 error_at(this->named_object_
->location(),
7940 "use of undefined type %qs",
7941 no
->message_name().c_str());
7942 this->warned_
= true;
7945 else if (no
->is_type_declaration())
7947 // The name was seen as a type, but the type was never defined.
7948 if (no
->type_declaration_value()->using_type())
7950 error_at(this->named_object_
->location(),
7951 "use of undefined type %qs",
7952 no
->message_name().c_str());
7953 this->warned_
= true;
7958 // The name was defined, but not as a type.
7961 error_at(this->named_object_
->location(), "expected type");
7962 this->warned_
= true;
7967 // Get the base type of a declaration. This gives an error if the
7968 // type has not yet been defined.
7971 Forward_declaration_type::real_type()
7973 if (this->is_defined())
7974 return this->named_object()->type_value();
7978 return Type::make_error_type();
7983 Forward_declaration_type::real_type() const
7985 if (this->is_defined())
7986 return this->named_object()->type_value();
7990 return Type::make_error_type();
7994 // Return whether the base type is defined.
7997 Forward_declaration_type::is_defined() const
7999 return this->named_object()->is_type();
8002 // Add a method. This is used when methods are defined before the
8006 Forward_declaration_type::add_method(const std::string
& name
,
8009 Named_object
* no
= this->named_object();
8010 gcc_assert(no
->is_type_declaration());
8011 return no
->type_declaration_value()->add_method(name
, function
);
8014 // Add a method declaration. This is used when methods are declared
8018 Forward_declaration_type::add_method_declaration(const std::string
& name
,
8019 Function_type
* type
,
8020 source_location location
)
8022 Named_object
* no
= this->named_object();
8023 gcc_assert(no
->is_type_declaration());
8024 Type_declaration
* td
= no
->type_declaration_value();
8025 return td
->add_method_declaration(name
, type
, location
);
8031 Forward_declaration_type::do_traverse(Traverse
* traverse
)
8033 if (this->is_defined()
8034 && Type::traverse(this->real_type(), traverse
) == TRAVERSE_EXIT
)
8035 return TRAVERSE_EXIT
;
8036 return TRAVERSE_CONTINUE
;
8039 // Get a tree for the type.
8042 Forward_declaration_type::do_get_tree(Gogo
* gogo
)
8044 if (this->is_defined())
8045 return Type::get_named_type_tree(gogo
, this->real_type());
8048 return error_mark_node
;
8050 // We represent an undefined type as a struct with no fields. That
8051 // should work fine for the middle-end, since the same case can
8053 Named_object
* no
= this->named_object();
8054 tree type_tree
= make_node(RECORD_TYPE
);
8055 tree id
= no
->get_id(gogo
);
8056 tree decl
= build_decl(no
->location(), TYPE_DECL
, id
, type_tree
);
8057 TYPE_NAME(type_tree
) = decl
;
8061 // Build a type descriptor for a forwarded type.
8064 Forward_declaration_type::do_type_descriptor(Gogo
* gogo
, Named_type
* name
)
8066 if (!this->is_defined())
8067 return Expression::make_nil(BUILTINS_LOCATION
);
8070 Type
* t
= this->real_type();
8072 return this->named_type_descriptor(gogo
, t
, name
);
8074 return Expression::make_type_descriptor(t
, BUILTINS_LOCATION
);
8078 // The reflection string.
8081 Forward_declaration_type::do_reflection(Gogo
* gogo
, std::string
* ret
) const
8083 this->append_reflection(this->real_type(), gogo
, ret
);
8086 // The mangled name.
8089 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
8091 if (this->is_defined())
8092 this->append_mangled_name(this->real_type(), gogo
, ret
);
8095 const Named_object
* no
= this->named_object();
8097 if (no
->package() == NULL
)
8098 name
= gogo
->package_name();
8100 name
= no
->package()->name();
8102 name
+= Gogo::unpack_hidden_name(no
->name());
8104 snprintf(buf
, sizeof buf
, "N%u_",
8105 static_cast<unsigned int>(name
.length()));
8111 // Export a forward declaration. This can happen when a defined type
8112 // refers to a type which is only declared (and is presumably defined
8113 // in some other file in the same package).
8116 Forward_declaration_type::do_export(Export
*) const
8118 // If there is a base type, that should be exported instead of this.
8119 gcc_assert(!this->is_defined());
8121 // We don't output anything.
8124 // Make a forward declaration.
8127 Type::make_forward_declaration(Named_object
* named_object
)
8129 return new Forward_declaration_type(named_object
);
8132 // Class Typed_identifier_list.
8134 // Sort the entries by name.
8136 struct Typed_identifier_list_sort
8140 operator()(const Typed_identifier
& t1
, const Typed_identifier
& t2
) const
8141 { return t1
.name() < t2
.name(); }
8145 Typed_identifier_list::sort_by_name()
8147 std::sort(this->entries_
.begin(), this->entries_
.end(),
8148 Typed_identifier_list_sort());
8154 Typed_identifier_list::traverse(Traverse
* traverse
)
8156 for (Typed_identifier_list::const_iterator p
= this->begin();
8160 if (Type::traverse(p
->type(), traverse
) == TRAVERSE_EXIT
)
8161 return TRAVERSE_EXIT
;
8163 return TRAVERSE_CONTINUE
;
8168 Typed_identifier_list
*
8169 Typed_identifier_list::copy() const
8171 Typed_identifier_list
* ret
= new Typed_identifier_list();
8172 for (Typed_identifier_list::const_iterator p
= this->begin();
8175 ret
->push_back(Typed_identifier(p
->name(), p
->type(), p
->location()));