1 // types.cc -- Go frontend types.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
23 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
31 #include "statements.h"
38 Type::Type(Type_classification classification)
39 : classification_(classification), tree_(NULL_TREE),
40 type_descriptor_decl_(NULL_TREE)
48 // Get the base type for a type--skip names and forward declarations.
53 switch (this->classification_)
56 return static_cast<Named_type*>(this)->real_type()->base();
58 return static_cast<Forward_declaration_type*>(this)->real_type()->base();
67 switch (this->classification_)
70 return static_cast<const Named_type*>(this)->real_type()->base();
73 const Forward_declaration_type* ftype =
74 static_cast<const Forward_declaration_type*>(this);
75 return ftype->real_type()->base();
82 // Skip defined forward declarations.
88 Forward_declaration_type* ftype = t->forward_declaration_type();
89 while (ftype != NULL && ftype->is_defined())
91 t = ftype->real_type();
92 ftype = t->forward_declaration_type();
98 Type::forwarded() const
100 const Type* t = this;
101 const Forward_declaration_type* ftype = t->forward_declaration_type();
102 while (ftype != NULL && ftype->is_defined())
104 t = ftype->real_type();
105 ftype = t->forward_declaration_type();
110 // If this is a named type, return it. Otherwise, return NULL.
115 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
119 Type::named_type() const
121 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
124 // Return true if this type is not defined.
127 Type::is_undefined() const
129 return this->forwarded()->forward_declaration_type() != NULL;
132 // Return true if this is a basic type: a type which is not composed
133 // of other types, and is not void.
136 Type::is_basic_type() const
138 switch (this->classification_)
161 return this->base()->is_basic_type();
168 // Return true if this is an abstract type.
171 Type::is_abstract() const
173 switch (this->classification())
176 return this->integer_type()->is_abstract();
178 return this->float_type()->is_abstract();
180 return this->complex_type()->is_abstract();
182 return this->is_abstract_string_type();
184 return this->is_abstract_boolean_type();
190 // Return a non-abstract version of an abstract type.
193 Type::make_non_abstract_type()
195 gcc_assert(this->is_abstract());
196 switch (this->classification())
199 return Type::lookup_integer_type("int");
201 return Type::lookup_float_type("float");
203 return Type::lookup_complex_type("complex");
205 return Type::lookup_string_type();
207 return Type::lookup_bool_type();
213 // Return true if this is an error type. Don't give an error if we
214 // try to dereference an undefined forwarding type, as this is called
215 // in the parser when the type may legitimately be undefined.
218 Type::is_error_type() const
220 const Type* t = this->forwarded();
221 // Note that we return false for an undefined forward type.
222 switch (t->classification_)
227 return t->named_type()->real_type()->is_error_type();
233 // If this is a pointer type, return the type to which it points.
234 // Otherwise, return NULL.
237 Type::points_to() const
239 const Pointer_type* ptype = this->convert<const Pointer_type,
241 return ptype == NULL ? NULL : ptype->points_to();
244 // Return whether this is an open array type.
247 Type::is_open_array_type() const
249 return this->array_type() != NULL && this->array_type()->length() == NULL;
252 // Return whether this is the predeclared constant nil being used as a
256 Type::is_nil_constant_as_type() const
258 const Type* t = this->forwarded();
259 if (t->forward_declaration_type() != NULL)
261 const Named_object* no = t->forward_declaration_type()->named_object();
262 if (no->is_unknown())
263 no = no->unknown_value()->real_named_object();
266 && no->const_value()->expr()->is_nil_expression())
275 Type::traverse(Type* type, Traverse* traverse)
277 gcc_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
278 || (traverse->traverse_mask()
279 & Traverse::traverse_expressions) != 0);
280 if (traverse->remember_type(type))
282 // We have already traversed this type.
283 return TRAVERSE_CONTINUE;
285 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
287 int t = traverse->type(type);
288 if (t == TRAVERSE_EXIT)
289 return TRAVERSE_EXIT;
290 else if (t == TRAVERSE_SKIP_COMPONENTS)
291 return TRAVERSE_CONTINUE;
293 // An array type has an expression which we need to traverse if
294 // traverse_expressions is set.
295 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
296 return TRAVERSE_EXIT;
297 return TRAVERSE_CONTINUE;
300 // Default implementation for do_traverse for child class.
303 Type::do_traverse(Traverse*)
305 return TRAVERSE_CONTINUE;
308 // Return whether two types are identical. If REASON is not NULL,
309 // optionally set *REASON to the reason the types are not identical.
312 Type::are_identical(const Type* t1, const Type* t2, std::string* reason)
314 if (t1 == NULL || t2 == NULL)
316 // Something is wrong. Return true to avoid cascading errors.
320 // Skip defined forward declarations.
321 t1 = t1->forwarded();
322 t2 = t2->forwarded();
327 // An undefined forward declaration is an error, so we return true
328 // to avoid cascading errors.
329 if (t1->forward_declaration_type() != NULL
330 || t2->forward_declaration_type() != NULL)
333 // Avoid cascading errors with error types.
334 if (t1->is_error_type() || t2->is_error_type())
337 // Get a good reason for the sink type. Note that the sink type on
338 // the left hand side of an assignment is handled in are_assignable.
339 if (t1->is_sink_type() || t2->is_sink_type())
342 *reason = "invalid use of _";
346 // A named type is only identical to itself.
347 if (t1->named_type() != NULL || t2->named_type() != NULL)
350 // Check type shapes.
351 if (t1->classification() != t2->classification())
354 switch (t1->classification())
360 // These types are always identical.
364 return t1->integer_type()->is_identical(t2->integer_type());
367 return t1->float_type()->is_identical(t2->float_type());
370 return t1->complex_type()->is_identical(t2->complex_type());
373 return t1->function_type()->is_identical(t2->function_type(),
378 return Type::are_identical(t1->points_to(), t2->points_to(), reason);
381 return t1->struct_type()->is_identical(t2->struct_type());
384 return t1->array_type()->is_identical(t2->array_type());
387 return t1->map_type()->is_identical(t2->map_type());
390 return t1->channel_type()->is_identical(t2->channel_type());
393 return t1->interface_type()->is_identical(t2->interface_type());
400 // Return true if it's OK to have a binary operation with types LHS
401 // and RHS. This is not used for shifts or comparisons.
404 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
406 if (Type::are_identical(lhs, rhs, NULL))
409 // A constant of abstract bool type may be mixed with any bool type.
410 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
411 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
414 // A constant of abstract string type may be mixed with any string
416 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
417 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
423 // A constant of abstract integer, float, or complex type may be
424 // mixed with an integer, float, or complex type.
425 if ((rhs->is_abstract()
426 && (rhs->integer_type() != NULL
427 || rhs->float_type() != NULL
428 || rhs->complex_type() != NULL)
429 && (lhs->integer_type() != NULL
430 || lhs->float_type() != NULL
431 || lhs->complex_type() != NULL))
432 || (lhs->is_abstract()
433 && (lhs->integer_type() != NULL
434 || lhs->float_type() != NULL
435 || lhs->complex_type() != NULL)
436 && (rhs->integer_type() != NULL
437 || rhs->float_type() != NULL
438 || rhs->complex_type() != NULL)))
441 // The nil type may be compared to a pointer, an interface type, a
442 // slice type, a channel type, a map type, or a function type.
443 if (lhs->is_nil_type()
444 && (rhs->points_to() != NULL
445 || rhs->interface_type() != NULL
446 || rhs->is_open_array_type()
447 || rhs->map_type() != NULL
448 || rhs->channel_type() != NULL
449 || rhs->function_type() != NULL))
451 if (rhs->is_nil_type()
452 && (lhs->points_to() != NULL
453 || lhs->interface_type() != NULL
454 || lhs->is_open_array_type()
455 || lhs->map_type() != NULL
456 || lhs->channel_type() != NULL
457 || lhs->function_type() != NULL))
463 // Return true if a value with type RHS may be assigned to a variable
464 // with type LHS. If REASON is not NULL, set *REASON to the reason
465 // the types are not assignable.
468 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
470 // Do some checks first. Make sure the types are defined.
471 if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
473 // Any value may be assigned to the blank identifier.
474 if (lhs->is_sink_type())
477 // All fields of a struct must be exported, or the assignment
478 // must be in the same package.
479 if (rhs != NULL && rhs->forwarded()->forward_declaration_type() == NULL)
481 if (lhs->has_hidden_fields(NULL, reason)
482 || rhs->has_hidden_fields(NULL, reason))
487 // Identical types are assignable.
488 if (Type::are_identical(lhs, rhs, reason))
491 // The types are assignable if they have identical underlying types
492 // and either LHS or RHS is not a named type.
493 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
494 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
495 && Type::are_identical(lhs->base(), rhs->base(), reason))
498 // The types are assignable if LHS is an interface type and RHS
499 // implements the required methods.
500 const Interface_type* lhs_interface_type = lhs->interface_type();
501 if (lhs_interface_type != NULL)
503 if (lhs_interface_type->implements_interface(rhs, reason))
505 const Interface_type* rhs_interface_type = rhs->interface_type();
506 if (rhs_interface_type != NULL
507 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
512 // The type are assignable if RHS is a bidirectional channel type,
513 // LHS is a channel type, they have identical element types, and
514 // either LHS or RHS is not a named type.
515 if (lhs->channel_type() != NULL
516 && rhs->channel_type() != NULL
517 && rhs->channel_type()->may_send()
518 && rhs->channel_type()->may_receive()
519 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
520 && Type::are_identical(lhs->channel_type()->element_type(),
521 rhs->channel_type()->element_type(),
525 // The nil type may be assigned to a pointer, function, slice, map,
526 // channel, or interface type.
527 if (rhs->is_nil_type()
528 && (lhs->points_to() != NULL
529 || lhs->function_type() != NULL
530 || lhs->is_open_array_type()
531 || lhs->map_type() != NULL
532 || lhs->channel_type() != NULL
533 || lhs->interface_type() != NULL))
536 // An untyped constant may be assigned to a numeric type if it is
537 // representable in that type.
538 if (rhs->is_abstract()
539 && (lhs->integer_type() != NULL
540 || lhs->float_type() != NULL
541 || lhs->complex_type() != NULL))
545 // Give some better error messages.
546 if (reason != NULL && reason->empty())
548 if (rhs->interface_type() != NULL)
549 reason->assign(_("need explicit conversion"));
550 else if (rhs->is_call_multiple_result_type())
551 reason->assign(_("multiple value function call in "
552 "single value context"));
553 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
555 size_t len = (lhs->named_type()->name().length()
556 + rhs->named_type()->name().length()
558 char* buf = new char[len];
559 snprintf(buf, len, _("cannot use type %s as type %s"),
560 rhs->named_type()->message_name().c_str(),
561 lhs->named_type()->message_name().c_str());
570 // Return true if a value with type RHS may be converted to type LHS.
571 // If REASON is not NULL, set *REASON to the reason the types are not
575 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
577 // The types are convertible if they are assignable.
578 if (Type::are_assignable(lhs, rhs, reason))
581 // The types are convertible if they have identical underlying
583 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
584 && Type::are_identical(lhs->base(), rhs->base(), reason))
587 // The types are convertible if they are both unnamed pointer types
588 // and their pointer base types have identical underlying types.
589 if (lhs->named_type() == NULL
590 && rhs->named_type() == NULL
591 && lhs->points_to() != NULL
592 && rhs->points_to() != NULL
593 && (lhs->points_to()->named_type() != NULL
594 || rhs->points_to()->named_type() != NULL)
595 && Type::are_identical(lhs->points_to()->base(),
596 rhs->points_to()->base(),
600 // Integer and floating point types are convertible to each other.
601 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
602 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
605 // Complex types are convertible to each other.
606 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
609 // An integer, or []byte, or []int, may be converted to a string.
610 if (lhs->is_string_type())
612 if (rhs->integer_type() != NULL)
614 if (rhs->is_open_array_type() && rhs->named_type() == NULL)
616 const Type* e = rhs->array_type()->element_type()->forwarded();
617 if (e->integer_type() != NULL
618 && (e == Type::lookup_integer_type("uint8")
619 || e == Type::lookup_integer_type("int")))
624 // A string may be converted to []byte or []int.
625 if (rhs->is_string_type()
626 && lhs->is_open_array_type()
627 && lhs->named_type() == NULL)
629 const Type* e = lhs->array_type()->element_type()->forwarded();
630 if (e->integer_type() != NULL
631 && (e == Type::lookup_integer_type("uint8")
632 || e == Type::lookup_integer_type("int")))
636 // An unsafe.Pointer type may be converted to any pointer type or to
637 // uintptr, and vice-versa.
638 if (lhs->is_unsafe_pointer_type()
639 && (rhs->points_to() != NULL
640 || (rhs->integer_type() != NULL
641 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
643 if (rhs->is_unsafe_pointer_type()
644 && (lhs->points_to() != NULL
645 || (lhs->integer_type() != NULL
646 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
649 // Give a better error message.
653 *reason = "invalid type conversion";
656 std::string s = "invalid type conversion (";
666 // Return whether this type has any hidden fields. This is only a
667 // possibility for a few types.
670 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
672 switch (this->forwarded()->classification_)
675 return this->named_type()->named_type_has_hidden_fields(reason);
677 return this->struct_type()->struct_has_hidden_fields(within, reason);
679 return this->array_type()->array_has_hidden_fields(within, reason);
685 // Return a hash code for the type to be used for method lookup.
688 Type::hash_for_method(Gogo* gogo) const
690 unsigned int ret = 0;
691 if (this->classification_ != TYPE_FORWARD)
692 ret += this->classification_;
693 return ret + this->do_hash_for_method(gogo);
696 // Default implementation of do_hash_for_method. This is appropriate
697 // for types with no subfields.
700 Type::do_hash_for_method(Gogo*) const
705 // Return a hash code for a string, given a starting hash.
708 Type::hash_string(const std::string& s, unsigned int h)
710 const char* p = s.data();
711 size_t len = s.length();
712 for (; len > 0; --len)
720 // Default check for the expression passed to make. Any type which
721 // may be used with make implements its own version of this.
724 Type::do_check_make_expression(Expression_list*, source_location)
729 // Return whether an expression has an integer value. Report an error
730 // if not. This is used when handling calls to the predeclared make
734 Type::check_int_value(Expression* e, const char* errmsg,
735 source_location location)
737 if (e->type()->integer_type() != NULL)
740 // Check for a floating point constant with integer value.
745 if (e->float_constant_value(fval, &dummy))
752 mpfr_clear_overflow();
753 mpfr_clear_erangeflag();
754 mpfr_get_z(ival, fval, GMP_RNDN);
755 if (!mpfr_overflow_p()
756 && !mpfr_erangeflag_p()
757 && mpz_sgn(ival) >= 0)
759 Named_type* ntype = Type::lookup_integer_type("int");
760 Integer_type* inttype = ntype->integer_type();
762 mpz_init_set_ui(max, 1);
763 mpz_mul_2exp(max, max, inttype->bits() - 1);
764 ok = mpz_cmp(ival, max) < 0;
778 error_at(location, "%s", errmsg);
782 // A hash table mapping unnamed types to trees.
784 Type::Type_trees Type::type_trees;
786 // Return a tree representing this type.
789 Type::get_tree(Gogo* gogo)
791 if (this->tree_ != NULL)
794 if (this->forward_declaration_type() != NULL
795 || this->named_type() != NULL)
796 return this->get_tree_without_hash(gogo);
798 // To avoid confusing GIMPLE, we need to translate all identical Go
799 // types to the same GIMPLE type. We use a hash table to do that.
800 // There is no need to use the hash table for named types, as named
801 // types are only identical to themselves.
803 std::pair<Type*, tree> val(this, NULL);
804 std::pair<Type_trees::iterator, bool> ins =
805 Type::type_trees.insert(val);
806 if (!ins.second && ins.first->second != NULL_TREE)
808 this->tree_ = ins.first->second;
812 tree t = this->get_tree_without_hash(gogo);
814 if (ins.first->second == NULL_TREE)
815 ins.first->second = t;
818 // We have already created a tree for this type. This can
819 // happen when an unnamed type is defined using a named type
820 // which in turns uses an identical unnamed type. Use the tree
821 // we created earlier and ignore the one we just built.
822 t = ins.first->second;
829 // Return a tree for a type without looking in the hash table for
830 // identical types. This is used for named types, since there is no
831 // point to looking in the hash table for them.
834 Type::get_tree_without_hash(Gogo* gogo)
836 if (this->tree_ == NULL_TREE)
838 tree t = this->do_get_tree(gogo);
840 // For a recursive function or pointer type, we will temporarily
841 // return ptr_type_node during the recursion. We don't want to
842 // record that for a forwarding type, as it may confuse us
844 if (t == ptr_type_node && this->forward_declaration_type() != NULL)
848 go_preserve_from_gc(t);
854 // Return a tree representing a zero initialization for this type.
857 Type::get_init_tree(Gogo* gogo, bool is_clear)
859 tree type_tree = this->get_tree(gogo);
860 if (type_tree == error_mark_node)
861 return error_mark_node;
862 return this->do_get_init_tree(gogo, type_tree, is_clear);
865 // Any type which supports the builtin make function must implement
869 Type::do_make_expression_tree(Translate_context*, Expression_list*,
875 // Return a pointer to the type descriptor for this type.
878 Type::type_descriptor_pointer(Gogo* gogo)
880 Type* t = this->forwarded();
881 if (t->type_descriptor_decl_ == NULL_TREE)
883 Expression* e = t->do_type_descriptor(gogo, NULL);
884 gogo->build_type_descriptor_decl(t, e, &t->type_descriptor_decl_);
885 gcc_assert(t->type_descriptor_decl_ != NULL_TREE
886 && (t->type_descriptor_decl_ == error_mark_node
887 || DECL_P(t->type_descriptor_decl_)));
889 if (t->type_descriptor_decl_ == error_mark_node)
890 return error_mark_node;
891 return build_fold_addr_expr(t->type_descriptor_decl_);
894 // Return a composite literal for a type descriptor.
897 Type::type_descriptor(Gogo* gogo, Type* type)
899 return type->do_type_descriptor(gogo, NULL);
902 // Return a composite literal for a type descriptor with a name.
905 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
907 gcc_assert(name != NULL && type->named_type() != name);
908 return type->do_type_descriptor(gogo, name);
911 // Make a builtin struct type from a list of fields. The fields are
912 // pairs of a name and a type.
915 Type::make_builtin_struct_type(int nfields, ...)
918 va_start(ap, nfields);
920 source_location bloc = BUILTINS_LOCATION;
921 Struct_field_list* sfl = new Struct_field_list();
922 for (int i = 0; i < nfields; i++)
924 const char* field_name = va_arg(ap, const char *);
925 Type* type = va_arg(ap, Type*);
926 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
931 return Type::make_struct_type(sfl, bloc);
934 // Make a builtin named type.
937 Type::make_builtin_named_type(const char* name, Type* type)
939 source_location bloc = BUILTINS_LOCATION;
940 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
941 return no->type_value();
944 // Return the type of a type descriptor. We should really tie this to
945 // runtime.Type rather than copying it. This must match commonType in
946 // libgo/go/runtime/type.go.
949 Type::make_type_descriptor_type()
954 source_location bloc = BUILTINS_LOCATION;
956 Type* uint8_type = Type::lookup_integer_type("uint8");
957 Type* uint32_type = Type::lookup_integer_type("uint32");
958 Type* uintptr_type = Type::lookup_integer_type("uintptr");
959 Type* string_type = Type::lookup_string_type();
960 Type* pointer_string_type = Type::make_pointer_type(string_type);
962 // This is an unnamed version of unsafe.Pointer. Perhaps we
963 // should use the named version instead, although that would
964 // require us to create the unsafe package if it has not been
965 // imported. It probably doesn't matter.
966 Type* void_type = Type::make_void_type();
967 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
969 // Forward declaration for the type descriptor type.
970 Named_object* named_type_descriptor_type =
971 Named_object::make_type_declaration("commonType", NULL, bloc);
972 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
973 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
975 // The type of a method on a concrete type.
976 Struct_type* method_type =
977 Type::make_builtin_struct_type(5,
978 "name", pointer_string_type,
979 "pkgPath", pointer_string_type,
980 "mtyp", pointer_type_descriptor_type,
981 "typ", pointer_type_descriptor_type,
982 "tfn", unsafe_pointer_type);
983 Named_type* named_method_type =
984 Type::make_builtin_named_type("method", method_type);
986 // Information for types with a name or methods.
987 Type* slice_named_method_type =
988 Type::make_array_type(named_method_type, NULL);
989 Struct_type* uncommon_type =
990 Type::make_builtin_struct_type(3,
991 "name", pointer_string_type,
992 "pkgPath", pointer_string_type,
993 "methods", slice_named_method_type);
994 Named_type* named_uncommon_type =
995 Type::make_builtin_named_type("uncommonType", uncommon_type);
997 Type* pointer_uncommon_type =
998 Type::make_pointer_type(named_uncommon_type);
1000 // The type descriptor type.
1002 Typed_identifier_list* params = new Typed_identifier_list();
1003 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1004 params->push_back(Typed_identifier("", uintptr_type, bloc));
1006 Typed_identifier_list* results = new Typed_identifier_list();
1007 results->push_back(Typed_identifier("", uintptr_type, bloc));
1009 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1011 params = new Typed_identifier_list();
1012 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1013 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1014 params->push_back(Typed_identifier("", uintptr_type, bloc));
1016 results = new Typed_identifier_list();
1017 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1019 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1022 Struct_type* type_descriptor_type =
1023 Type::make_builtin_struct_type(9,
1025 "align", uint8_type,
1026 "fieldAlign", uint8_type,
1027 "size", uintptr_type,
1028 "hash", uint32_type,
1029 "hashfn", hashfn_type,
1030 "equalfn", equalfn_type,
1031 "string", pointer_string_type,
1032 "", pointer_uncommon_type);
1034 Named_type* named = Type::make_builtin_named_type("commonType",
1035 type_descriptor_type);
1037 named_type_descriptor_type->set_type_value(named);
1045 // Make the type of a pointer to a type descriptor as represented in
1049 Type::make_type_descriptor_ptr_type()
1053 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1057 // Return the names of runtime functions which compute a hash code for
1058 // this type and which compare whether two values of this type are
1062 Type::type_functions(const char** hash_fn, const char** equal_fn) const
1064 switch (this->base()->classification())
1066 case Type::TYPE_ERROR:
1067 case Type::TYPE_VOID:
1068 case Type::TYPE_NIL:
1069 // These types can not be hashed or compared.
1070 *hash_fn = "__go_type_hash_error";
1071 *equal_fn = "__go_type_equal_error";
1074 case Type::TYPE_BOOLEAN:
1075 case Type::TYPE_INTEGER:
1076 case Type::TYPE_FLOAT:
1077 case Type::TYPE_COMPLEX:
1078 case Type::TYPE_POINTER:
1079 case Type::TYPE_FUNCTION:
1080 case Type::TYPE_MAP:
1081 case Type::TYPE_CHANNEL:
1082 *hash_fn = "__go_type_hash_identity";
1083 *equal_fn = "__go_type_equal_identity";
1086 case Type::TYPE_STRING:
1087 *hash_fn = "__go_type_hash_string";
1088 *equal_fn = "__go_type_equal_string";
1091 case Type::TYPE_STRUCT:
1092 case Type::TYPE_ARRAY:
1093 // These types can not be hashed or compared.
1094 *hash_fn = "__go_type_hash_error";
1095 *equal_fn = "__go_type_equal_error";
1098 case Type::TYPE_INTERFACE:
1099 if (this->interface_type()->is_empty())
1101 *hash_fn = "__go_type_hash_empty_interface";
1102 *equal_fn = "__go_type_equal_empty_interface";
1106 *hash_fn = "__go_type_hash_interface";
1107 *equal_fn = "__go_type_equal_interface";
1111 case Type::TYPE_NAMED:
1112 case Type::TYPE_FORWARD:
1120 // Return a composite literal for the type descriptor for a plain type
1121 // of kind RUNTIME_TYPE_KIND named NAME.
1124 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1125 Named_type* name, const Methods* methods,
1126 bool only_value_methods)
1128 source_location bloc = BUILTINS_LOCATION;
1130 Type* td_type = Type::make_type_descriptor_type();
1131 const Struct_field_list* fields = td_type->struct_type()->fields();
1133 Expression_list* vals = new Expression_list();
1136 Struct_field_list::const_iterator p = fields->begin();
1137 gcc_assert(p->field_name() == "Kind");
1139 mpz_init_set_ui(iv, runtime_type_kind);
1140 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1143 gcc_assert(p->field_name() == "align");
1144 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1145 vals->push_back(Expression::make_type_info(this, type_info));
1148 gcc_assert(p->field_name() == "fieldAlign");
1149 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1150 vals->push_back(Expression::make_type_info(this, type_info));
1153 gcc_assert(p->field_name() == "size");
1154 type_info = Expression::TYPE_INFO_SIZE;
1155 vals->push_back(Expression::make_type_info(this, type_info));
1158 gcc_assert(p->field_name() == "hash");
1159 mpz_set_ui(iv, this->hash_for_method(gogo));
1160 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1162 const char* hash_fn;
1163 const char* equal_fn;
1164 this->type_functions(&hash_fn, &equal_fn);
1167 gcc_assert(p->field_name() == "hashfn");
1168 Function_type* fntype = p->type()->function_type();
1169 Named_object* no = Named_object::make_function_declaration(hash_fn, NULL,
1172 no->func_declaration_value()->set_asm_name(hash_fn);
1173 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1176 gcc_assert(p->field_name() == "equalfn");
1177 fntype = p->type()->function_type();
1178 no = Named_object::make_function_declaration(equal_fn, NULL, fntype, bloc);
1179 no->func_declaration_value()->set_asm_name(equal_fn);
1180 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1183 gcc_assert(p->field_name() == "string");
1184 Expression* s = Expression::make_string((name != NULL
1185 ? name->reflection(gogo)
1186 : this->reflection(gogo)),
1188 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1191 gcc_assert(p->field_name() == "uncommonType");
1192 if (name == NULL && methods == NULL)
1193 vals->push_back(Expression::make_nil(bloc));
1196 if (methods == NULL)
1197 methods = name->methods();
1198 vals->push_back(this->uncommon_type_constructor(gogo,
1201 only_value_methods));
1205 gcc_assert(p == fields->end());
1209 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1212 // Return a composite literal for the uncommon type information for
1213 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1214 // struct. If name is not NULL, it is the name of the type. If
1215 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1216 // is true if only value methods should be included. At least one of
1217 // NAME and METHODS must not be NULL.
1220 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1221 Named_type* name, const Methods* methods,
1222 bool only_value_methods) const
1224 source_location bloc = BUILTINS_LOCATION;
1226 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1228 Expression_list* vals = new Expression_list();
1231 Struct_field_list::const_iterator p = fields->begin();
1232 gcc_assert(p->field_name() == "name");
1235 gcc_assert(p->field_name() == "pkgPath");
1239 vals->push_back(Expression::make_nil(bloc));
1240 vals->push_back(Expression::make_nil(bloc));
1244 Named_object* no = name->named_object();
1245 std::string n = Gogo::unpack_hidden_name(no->name());
1246 Expression* s = Expression::make_string(n, bloc);
1247 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1249 if (name->is_builtin())
1250 vals->push_back(Expression::make_nil(bloc));
1253 const Package* package = no->package();
1254 const std::string& unique_prefix(package == NULL
1255 ? gogo->unique_prefix()
1256 : package->unique_prefix());
1257 const std::string& package_name(package == NULL
1258 ? gogo->package_name()
1260 n.assign(unique_prefix);
1262 n.append(package_name);
1263 if (name->in_function() != NULL)
1266 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1268 s = Expression::make_string(n, bloc);
1269 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1274 gcc_assert(p->field_name() == "methods");
1275 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1276 only_value_methods));
1279 gcc_assert(p == fields->end());
1281 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1283 return Expression::make_unary(OPERATOR_AND, r, bloc);
1286 // Sort methods by name.
1292 operator()(const std::pair<std::string, const Method*>& m1,
1293 const std::pair<std::string, const Method*>& m2) const
1294 { return m1.first < m2.first; }
1297 // Return a composite literal for the type method table for this type.
1298 // METHODS_TYPE is the type of the table, and is a slice type.
1299 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1300 // then only value methods are used.
1303 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1304 const Methods* methods,
1305 bool only_value_methods) const
1307 source_location bloc = BUILTINS_LOCATION;
1309 std::vector<std::pair<std::string, const Method*> > smethods;
1310 if (methods != NULL)
1312 smethods.reserve(methods->count());
1313 for (Methods::const_iterator p = methods->begin();
1314 p != methods->end();
1317 if (p->second->is_ambiguous())
1319 if (only_value_methods && !p->second->is_value_method())
1321 smethods.push_back(std::make_pair(p->first, p->second));
1325 if (smethods.empty())
1326 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1328 std::sort(smethods.begin(), smethods.end(), Sort_methods());
1330 Type* method_type = methods_type->array_type()->element_type();
1332 Expression_list* vals = new Expression_list();
1333 vals->reserve(smethods.size());
1334 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1336 p != smethods.end();
1338 vals->push_back(this->method_constructor(gogo, method_type, p->first,
1341 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1344 // Return a composite literal for a single method. METHOD_TYPE is the
1345 // type of the entry. METHOD_NAME is the name of the method and M is
1346 // the method information.
1349 Type::method_constructor(Gogo*, Type* method_type,
1350 const std::string& method_name,
1351 const Method* m) const
1353 source_location bloc = BUILTINS_LOCATION;
1355 const Struct_field_list* fields = method_type->struct_type()->fields();
1357 Expression_list* vals = new Expression_list();
1360 Struct_field_list::const_iterator p = fields->begin();
1361 gcc_assert(p->field_name() == "name");
1362 const std::string n = Gogo::unpack_hidden_name(method_name);
1363 Expression* s = Expression::make_string(n, bloc);
1364 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1367 gcc_assert(p->field_name() == "pkgPath");
1368 if (!Gogo::is_hidden_name(method_name))
1369 vals->push_back(Expression::make_nil(bloc));
1372 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1373 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1376 Named_object* no = (m->needs_stub_method()
1378 : m->named_object());
1380 Function_type* mtype;
1381 if (no->is_function())
1382 mtype = no->func_value()->type();
1384 mtype = no->func_declaration_value()->type();
1385 gcc_assert(mtype->is_method());
1386 Type* nonmethod_type = mtype->copy_without_receiver();
1389 gcc_assert(p->field_name() == "mtyp");
1390 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1393 gcc_assert(p->field_name() == "typ");
1394 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1397 gcc_assert(p->field_name() == "tfn");
1398 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1401 gcc_assert(p == fields->end());
1403 return Expression::make_struct_composite_literal(method_type, vals, bloc);
1406 // Return a composite literal for the type descriptor of a plain type.
1407 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1408 // NULL, it is the name to use as well as the list of methods.
1411 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1414 return this->type_descriptor_constructor(gogo, runtime_type_kind,
1418 // Return the type reflection string for this type.
1421 Type::reflection(Gogo* gogo) const
1425 // The do_reflection virtual function should set RET to the
1426 // reflection string.
1427 this->do_reflection(gogo, &ret);
1432 // Return a mangled name for the type.
1435 Type::mangled_name(Gogo* gogo) const
1439 // The do_mangled_name virtual function should set RET to the
1440 // mangled name. For a composite type it should append a code for
1441 // the composition and then call do_mangled_name on the components.
1442 this->do_mangled_name(gogo, &ret);
1447 // Default function to export a type.
1450 Type::do_export(Export*) const
1458 Type::import_type(Import* imp)
1460 if (imp->match_c_string("("))
1461 return Function_type::do_import(imp);
1462 else if (imp->match_c_string("*"))
1463 return Pointer_type::do_import(imp);
1464 else if (imp->match_c_string("struct "))
1465 return Struct_type::do_import(imp);
1466 else if (imp->match_c_string("["))
1467 return Array_type::do_import(imp);
1468 else if (imp->match_c_string("map "))
1469 return Map_type::do_import(imp);
1470 else if (imp->match_c_string("chan "))
1471 return Channel_type::do_import(imp);
1472 else if (imp->match_c_string("interface"))
1473 return Interface_type::do_import(imp);
1476 error_at(imp->location(), "import error: expected type");
1477 return Type::make_error_type();
1481 // A type used to indicate a parsing error. This exists to simplify
1482 // later error detection.
1484 class Error_type : public Type
1494 { return error_mark_node; }
1497 do_get_init_tree(Gogo*, tree, bool)
1498 { return error_mark_node; }
1501 do_type_descriptor(Gogo*, Named_type*)
1502 { return Expression::make_error(BUILTINS_LOCATION); }
1505 do_reflection(Gogo*, std::string*) const
1506 { gcc_assert(saw_errors()); }
1509 do_mangled_name(Gogo*, std::string* ret) const
1510 { ret->push_back('E'); }
1514 Type::make_error_type()
1516 static Error_type singleton_error_type;
1517 return &singleton_error_type;
1522 class Void_type : public Type
1532 { return void_type_node; }
1535 do_get_init_tree(Gogo*, tree, bool)
1536 { gcc_unreachable(); }
1539 do_type_descriptor(Gogo*, Named_type*)
1540 { gcc_unreachable(); }
1543 do_reflection(Gogo*, std::string*) const
1547 do_mangled_name(Gogo*, std::string* ret) const
1548 { ret->push_back('v'); }
1552 Type::make_void_type()
1554 static Void_type singleton_void_type;
1555 return &singleton_void_type;
1558 // The boolean type.
1560 class Boolean_type : public Type
1564 : Type(TYPE_BOOLEAN)
1570 { return boolean_type_node; }
1573 do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1574 { return is_clear ? NULL : fold_convert(type_tree, boolean_false_node); }
1577 do_type_descriptor(Gogo*, Named_type* name);
1579 // We should not be asked for the reflection string of a basic type.
1581 do_reflection(Gogo*, std::string* ret) const
1582 { ret->append("bool"); }
1585 do_mangled_name(Gogo*, std::string* ret) const
1586 { ret->push_back('b'); }
1589 // Make the type descriptor.
1592 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1595 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
1598 Named_object* no = gogo->lookup_global("bool");
1599 gcc_assert(no != NULL);
1600 return Type::type_descriptor(gogo, no->type_value());
1605 Type::make_boolean_type()
1607 static Boolean_type boolean_type;
1608 return &boolean_type;
1611 // The named type "bool".
1613 static Named_type* named_bool_type;
1615 // Get the named type "bool".
1618 Type::lookup_bool_type()
1620 return named_bool_type;
1623 // Make the named type "bool".
1626 Type::make_named_bool_type()
1628 Type* bool_type = Type::make_boolean_type();
1629 Named_object* named_object = Named_object::make_type("bool", NULL,
1632 Named_type* named_type = named_object->type_value();
1633 named_bool_type = named_type;
1637 // Class Integer_type.
1639 Integer_type::Named_integer_types Integer_type::named_integer_types;
1641 // Create a new integer type. Non-abstract integer types always have
1645 Integer_type::create_integer_type(const char* name, bool is_unsigned,
1646 int bits, int runtime_type_kind)
1648 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
1650 std::string sname(name);
1651 Named_object* named_object = Named_object::make_type(sname, NULL,
1654 Named_type* named_type = named_object->type_value();
1655 std::pair<Named_integer_types::iterator, bool> ins =
1656 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
1657 gcc_assert(ins.second);
1661 // Look up an existing integer type.
1664 Integer_type::lookup_integer_type(const char* name)
1666 Named_integer_types::const_iterator p =
1667 Integer_type::named_integer_types.find(name);
1668 gcc_assert(p != Integer_type::named_integer_types.end());
1672 // Create a new abstract integer type.
1675 Integer_type::create_abstract_integer_type()
1677 static Integer_type* abstract_type;
1678 if (abstract_type == NULL)
1679 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
1680 RUNTIME_TYPE_KIND_INT);
1681 return abstract_type;
1684 // Integer type compatibility.
1687 Integer_type::is_identical(const Integer_type* t) const
1689 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
1691 return this->is_abstract_ == t->is_abstract_;
1697 Integer_type::do_hash_for_method(Gogo*) const
1699 return ((this->bits_ << 4)
1700 + ((this->is_unsigned_ ? 1 : 0) << 8)
1701 + ((this->is_abstract_ ? 1 : 0) << 9));
1704 // Get the tree for an Integer_type.
1707 Integer_type::do_get_tree(Gogo*)
1709 gcc_assert(!this->is_abstract_);
1710 if (this->is_unsigned_)
1712 if (this->bits_ == INT_TYPE_SIZE)
1713 return unsigned_type_node;
1714 else if (this->bits_ == CHAR_TYPE_SIZE)
1715 return unsigned_char_type_node;
1716 else if (this->bits_ == SHORT_TYPE_SIZE)
1717 return short_unsigned_type_node;
1718 else if (this->bits_ == LONG_TYPE_SIZE)
1719 return long_unsigned_type_node;
1720 else if (this->bits_ == LONG_LONG_TYPE_SIZE)
1721 return long_long_unsigned_type_node;
1723 return make_unsigned_type(this->bits_);
1727 if (this->bits_ == INT_TYPE_SIZE)
1728 return integer_type_node;
1729 else if (this->bits_ == CHAR_TYPE_SIZE)
1730 return signed_char_type_node;
1731 else if (this->bits_ == SHORT_TYPE_SIZE)
1732 return short_integer_type_node;
1733 else if (this->bits_ == LONG_TYPE_SIZE)
1734 return long_integer_type_node;
1735 else if (this->bits_ == LONG_LONG_TYPE_SIZE)
1736 return long_long_integer_type_node;
1738 return make_signed_type(this->bits_);
1743 Integer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1745 return is_clear ? NULL : build_int_cst(type_tree, 0);
1748 // The type descriptor for an integer type. Integer types are always
1752 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1754 gcc_assert(name != NULL);
1755 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1758 // We should not be asked for the reflection string of a basic type.
1761 Integer_type::do_reflection(Gogo*, std::string*) const
1769 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
1772 snprintf(buf, sizeof buf, "i%s%s%de",
1773 this->is_abstract_ ? "a" : "",
1774 this->is_unsigned_ ? "u" : "",
1779 // Make an integer type.
1782 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
1783 int runtime_type_kind)
1785 return Integer_type::create_integer_type(name, is_unsigned, bits,
1789 // Make an abstract integer type.
1792 Type::make_abstract_integer_type()
1794 return Integer_type::create_abstract_integer_type();
1797 // Look up an integer type.
1800 Type::lookup_integer_type(const char* name)
1802 return Integer_type::lookup_integer_type(name);
1805 // Class Float_type.
1807 Float_type::Named_float_types Float_type::named_float_types;
1809 // Create a new float type. Non-abstract float types always have
1813 Float_type::create_float_type(const char* name, int bits,
1814 int runtime_type_kind)
1816 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
1817 std::string sname(name);
1818 Named_object* named_object = Named_object::make_type(sname, NULL, float_type,
1820 Named_type* named_type = named_object->type_value();
1821 std::pair<Named_float_types::iterator, bool> ins =
1822 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
1823 gcc_assert(ins.second);
1827 // Look up an existing float type.
1830 Float_type::lookup_float_type(const char* name)
1832 Named_float_types::const_iterator p =
1833 Float_type::named_float_types.find(name);
1834 gcc_assert(p != Float_type::named_float_types.end());
1838 // Create a new abstract float type.
1841 Float_type::create_abstract_float_type()
1843 static Float_type* abstract_type;
1844 if (abstract_type == NULL)
1845 abstract_type = new Float_type(true, FLOAT_TYPE_SIZE,
1846 RUNTIME_TYPE_KIND_FLOAT);
1847 return abstract_type;
1850 // Whether this type is identical with T.
1853 Float_type::is_identical(const Float_type* t) const
1855 if (this->bits_ != t->bits_)
1857 return this->is_abstract_ == t->is_abstract_;
1863 Float_type::do_hash_for_method(Gogo*) const
1865 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
1868 // Get a tree without using a Gogo*.
1871 Float_type::type_tree() const
1873 if (this->bits_ == FLOAT_TYPE_SIZE)
1874 return float_type_node;
1875 else if (this->bits_ == DOUBLE_TYPE_SIZE)
1876 return double_type_node;
1877 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE)
1878 return long_double_type_node;
1881 tree ret = make_node(REAL_TYPE);
1882 TYPE_PRECISION(ret) = this->bits_;
1891 Float_type::do_get_tree(Gogo*)
1893 return this->type_tree();
1897 Float_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1902 real_from_integer(&r, TYPE_MODE(type_tree), 0, 0, 0);
1903 return build_real(type_tree, r);
1906 // The type descriptor for a float type. Float types are always named.
1909 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1911 gcc_assert(name != NULL);
1912 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1915 // We should not be asked for the reflection string of a basic type.
1918 Float_type::do_reflection(Gogo*, std::string*) const
1926 Float_type::do_mangled_name(Gogo*, std::string* ret) const
1929 snprintf(buf, sizeof buf, "f%s%de",
1930 this->is_abstract_ ? "a" : "",
1935 // Make a floating point type.
1938 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
1940 return Float_type::create_float_type(name, bits, runtime_type_kind);
1943 // Make an abstract float type.
1946 Type::make_abstract_float_type()
1948 return Float_type::create_abstract_float_type();
1951 // Look up a float type.
1954 Type::lookup_float_type(const char* name)
1956 return Float_type::lookup_float_type(name);
1959 // Class Complex_type.
1961 Complex_type::Named_complex_types Complex_type::named_complex_types;
1963 // Create a new complex type. Non-abstract complex types always have
1967 Complex_type::create_complex_type(const char* name, int bits,
1968 int runtime_type_kind)
1970 Complex_type* complex_type = new Complex_type(false, bits,
1972 std::string sname(name);
1973 Named_object* named_object = Named_object::make_type(sname, NULL,
1976 Named_type* named_type = named_object->type_value();
1977 std::pair<Named_complex_types::iterator, bool> ins =
1978 Complex_type::named_complex_types.insert(std::make_pair(sname,
1980 gcc_assert(ins.second);
1984 // Look up an existing complex type.
1987 Complex_type::lookup_complex_type(const char* name)
1989 Named_complex_types::const_iterator p =
1990 Complex_type::named_complex_types.find(name);
1991 gcc_assert(p != Complex_type::named_complex_types.end());
1995 // Create a new abstract complex type.
1998 Complex_type::create_abstract_complex_type()
2000 static Complex_type* abstract_type;
2001 if (abstract_type == NULL)
2002 abstract_type = new Complex_type(true, FLOAT_TYPE_SIZE * 2,
2003 RUNTIME_TYPE_KIND_FLOAT);
2004 return abstract_type;
2007 // Whether this type is identical with T.
2010 Complex_type::is_identical(const Complex_type *t) const
2012 if (this->bits_ != t->bits_)
2014 return this->is_abstract_ == t->is_abstract_;
2020 Complex_type::do_hash_for_method(Gogo*) const
2022 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2025 // Get a tree without using a Gogo*.
2028 Complex_type::type_tree() const
2030 if (this->bits_ == FLOAT_TYPE_SIZE * 2)
2031 return complex_float_type_node;
2032 else if (this->bits_ == DOUBLE_TYPE_SIZE * 2)
2033 return complex_double_type_node;
2034 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE * 2)
2035 return complex_long_double_type_node;
2038 tree ret = make_node(REAL_TYPE);
2039 TYPE_PRECISION(ret) = this->bits_ / 2;
2041 return build_complex_type(ret);
2048 Complex_type::do_get_tree(Gogo*)
2050 return this->type_tree();
2053 // Zero initializer.
2056 Complex_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2061 real_from_integer(&r, TYPE_MODE(TREE_TYPE(type_tree)), 0, 0, 0);
2062 return build_complex(type_tree, build_real(TREE_TYPE(type_tree), r),
2063 build_real(TREE_TYPE(type_tree), r));
2066 // The type descriptor for a complex type. Complex types are always
2070 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2072 gcc_assert(name != NULL);
2073 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2076 // We should not be asked for the reflection string of a basic type.
2079 Complex_type::do_reflection(Gogo*, std::string*) const
2087 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2090 snprintf(buf, sizeof buf, "c%s%de",
2091 this->is_abstract_ ? "a" : "",
2096 // Make a complex type.
2099 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2101 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2104 // Make an abstract complex type.
2107 Type::make_abstract_complex_type()
2109 return Complex_type::create_abstract_complex_type();
2112 // Look up a complex type.
2115 Type::lookup_complex_type(const char* name)
2117 return Complex_type::lookup_complex_type(name);
2120 // Class String_type.
2122 // Return the tree for String_type. A string is a struct with two
2123 // fields: a pointer to the characters and a length.
2126 String_type::do_get_tree(Gogo*)
2128 static tree struct_type;
2129 return Gogo::builtin_struct(&struct_type, "__go_string", NULL_TREE, 2,
2131 build_pointer_type(unsigned_char_type_node),
2136 // Return a tree for the length of STRING.
2139 String_type::length_tree(Gogo*, tree string)
2141 tree string_type = TREE_TYPE(string);
2142 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2143 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2144 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2146 return fold_build3(COMPONENT_REF, integer_type_node, string,
2147 length_field, NULL_TREE);
2150 // Return a tree for a pointer to the bytes of STRING.
2153 String_type::bytes_tree(Gogo*, tree string)
2155 tree string_type = TREE_TYPE(string);
2156 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2157 tree bytes_field = TYPE_FIELDS(string_type);
2158 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2160 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2161 bytes_field, NULL_TREE);
2164 // We initialize a string to { NULL, 0 }.
2167 String_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2172 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2174 VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
2176 for (tree field = TYPE_FIELDS(type_tree);
2178 field = DECL_CHAIN(field))
2180 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2182 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
2185 tree ret = build_constructor(type_tree, init);
2186 TREE_CONSTANT(ret) = 1;
2190 // The type descriptor for the string type.
2193 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2196 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2199 Named_object* no = gogo->lookup_global("string");
2200 gcc_assert(no != NULL);
2201 return Type::type_descriptor(gogo, no->type_value());
2205 // We should not be asked for the reflection string of a basic type.
2208 String_type::do_reflection(Gogo*, std::string* ret) const
2210 ret->append("string");
2213 // Mangled name of a string type.
2216 String_type::do_mangled_name(Gogo*, std::string* ret) const
2218 ret->push_back('z');
2221 // Make a string type.
2224 Type::make_string_type()
2226 static String_type string_type;
2227 return &string_type;
2230 // The named type "string".
2232 static Named_type* named_string_type;
2234 // Get the named type "string".
2237 Type::lookup_string_type()
2239 return named_string_type;
2242 // Make the named type string.
2245 Type::make_named_string_type()
2247 Type* string_type = Type::make_string_type();
2248 Named_object* named_object = Named_object::make_type("string", NULL,
2251 Named_type* named_type = named_object->type_value();
2252 named_string_type = named_type;
2256 // The sink type. This is the type of the blank identifier _. Any
2257 // type may be assigned to it.
2259 class Sink_type : public Type
2269 { gcc_unreachable(); }
2272 do_get_init_tree(Gogo*, tree, bool)
2273 { gcc_unreachable(); }
2276 do_type_descriptor(Gogo*, Named_type*)
2277 { gcc_unreachable(); }
2280 do_reflection(Gogo*, std::string*) const
2281 { gcc_unreachable(); }
2284 do_mangled_name(Gogo*, std::string*) const
2285 { gcc_unreachable(); }
2288 // Make the sink type.
2291 Type::make_sink_type()
2293 static Sink_type sink_type;
2297 // Class Function_type.
2302 Function_type::do_traverse(Traverse* traverse)
2304 if (this->receiver_ != NULL
2305 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2306 return TRAVERSE_EXIT;
2307 if (this->parameters_ != NULL
2308 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2309 return TRAVERSE_EXIT;
2310 if (this->results_ != NULL
2311 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2312 return TRAVERSE_EXIT;
2313 return TRAVERSE_CONTINUE;
2316 // Returns whether T is a valid redeclaration of this type. If this
2317 // returns false, and REASON is not NULL, *REASON may be set to a
2318 // brief explanation of why it returned false.
2321 Function_type::is_valid_redeclaration(const Function_type* t,
2322 std::string* reason) const
2324 if (!this->is_identical(t, false, reason))
2327 // A redeclaration of a function is required to use the same names
2328 // for the receiver and parameters.
2329 if (this->receiver() != NULL
2330 && this->receiver()->name() != t->receiver()->name()
2331 && this->receiver()->name() != Import::import_marker
2332 && t->receiver()->name() != Import::import_marker)
2335 *reason = "receiver name changed";
2339 const Typed_identifier_list* parms1 = this->parameters();
2340 const Typed_identifier_list* parms2 = t->parameters();
2343 Typed_identifier_list::const_iterator p1 = parms1->begin();
2344 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2345 p2 != parms2->end();
2348 if (p1->name() != p2->name()
2349 && p1->name() != Import::import_marker
2350 && p2->name() != Import::import_marker)
2353 *reason = "parameter name changed";
2357 // This is called at parse time, so we may have unknown
2359 Type* t1 = p1->type()->forwarded();
2360 Type* t2 = p2->type()->forwarded();
2362 && t1->forward_declaration_type() != NULL
2363 && (t2->forward_declaration_type() == NULL
2364 || (t1->forward_declaration_type()->named_object()
2365 != t2->forward_declaration_type()->named_object())))
2370 const Typed_identifier_list* results1 = this->results();
2371 const Typed_identifier_list* results2 = t->results();
2372 if (results1 != NULL)
2374 Typed_identifier_list::const_iterator res1 = results1->begin();
2375 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2376 res2 != results2->end();
2379 if (res1->name() != res2->name()
2380 && res1->name() != Import::import_marker
2381 && res2->name() != Import::import_marker)
2384 *reason = "result name changed";
2388 // This is called at parse time, so we may have unknown
2390 Type* t1 = res1->type()->forwarded();
2391 Type* t2 = res2->type()->forwarded();
2393 && t1->forward_declaration_type() != NULL
2394 && (t2->forward_declaration_type() == NULL
2395 || (t1->forward_declaration_type()->named_object()
2396 != t2->forward_declaration_type()->named_object())))
2404 // Check whether T is the same as this type.
2407 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2408 std::string* reason) const
2410 if (!ignore_receiver)
2412 const Typed_identifier* r1 = this->receiver();
2413 const Typed_identifier* r2 = t->receiver();
2414 if ((r1 != NULL) != (r2 != NULL))
2417 *reason = _("different receiver types");
2422 if (!Type::are_identical(r1->type(), r2->type(), reason))
2424 if (reason != NULL && !reason->empty())
2425 *reason = "receiver: " + *reason;
2431 const Typed_identifier_list* parms1 = this->parameters();
2432 const Typed_identifier_list* parms2 = t->parameters();
2433 if ((parms1 != NULL) != (parms2 != NULL))
2436 *reason = _("different number of parameters");
2441 Typed_identifier_list::const_iterator p1 = parms1->begin();
2442 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2443 p2 != parms2->end();
2446 if (p1 == parms1->end())
2449 *reason = _("different number of parameters");
2453 if (!Type::are_identical(p1->type(), p2->type(), NULL))
2456 *reason = _("different parameter types");
2460 if (p1 != parms1->end())
2463 *reason = _("different number of parameters");
2468 if (this->is_varargs() != t->is_varargs())
2471 *reason = _("different varargs");
2475 const Typed_identifier_list* results1 = this->results();
2476 const Typed_identifier_list* results2 = t->results();
2477 if ((results1 != NULL) != (results2 != NULL))
2480 *reason = _("different number of results");
2483 if (results1 != NULL)
2485 Typed_identifier_list::const_iterator res1 = results1->begin();
2486 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2487 res2 != results2->end();
2490 if (res1 == results1->end())
2493 *reason = _("different number of results");
2497 if (!Type::are_identical(res1->type(), res2->type(), NULL))
2500 *reason = _("different result types");
2504 if (res1 != results1->end())
2507 *reason = _("different number of results");
2518 Function_type::do_hash_for_method(Gogo* gogo) const
2520 unsigned int ret = 0;
2521 // We ignore the receiver type for hash codes, because we need to
2522 // get the same hash code for a method in an interface and a method
2523 // declared for a type. The former will not have a receiver.
2524 if (this->parameters_ != NULL)
2527 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2528 p != this->parameters_->end();
2530 ret += p->type()->hash_for_method(gogo) << shift;
2532 if (this->results_ != NULL)
2535 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2536 p != this->results_->end();
2538 ret += p->type()->hash_for_method(gogo) << shift;
2540 if (this->is_varargs_)
2546 // Get the tree for a function type.
2549 Function_type::do_get_tree(Gogo* gogo)
2551 tree args = NULL_TREE;
2554 if (this->receiver_ != NULL)
2556 Type* rtype = this->receiver_->type();
2557 tree ptype = rtype->get_tree(gogo);
2558 if (ptype == error_mark_node)
2559 return error_mark_node;
2561 // We always pass the address of the receiver parameter, in
2562 // order to make interface calls work with unknown types.
2563 if (rtype->points_to() == NULL)
2564 ptype = build_pointer_type(ptype);
2566 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2567 pp = &TREE_CHAIN (*pp);
2570 if (this->parameters_ != NULL)
2572 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2573 p != this->parameters_->end();
2576 tree ptype = p->type()->get_tree(gogo);
2577 if (ptype == error_mark_node)
2578 return error_mark_node;
2579 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2580 pp = &TREE_CHAIN (*pp);
2584 // Varargs is handled entirely at the Go level. At the tree level,
2585 // functions are not varargs.
2586 *pp = void_list_node;
2589 if (this->results_ == NULL)
2590 result = void_type_node;
2591 else if (this->results_->size() == 1)
2592 result = this->results_->begin()->type()->get_tree(gogo);
2595 result = make_node(RECORD_TYPE);
2596 tree field_trees = NULL_TREE;
2597 tree* pp = &field_trees;
2598 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2599 p != this->results_->end();
2602 const std::string name = (p->name().empty()
2604 : Gogo::unpack_hidden_name(p->name()));
2605 tree name_tree = get_identifier_with_length(name.data(),
2607 tree field_type_tree = p->type()->get_tree(gogo);
2608 if (field_type_tree == error_mark_node)
2609 return error_mark_node;
2610 tree field = build_decl(this->location_, FIELD_DECL, name_tree,
2612 DECL_CONTEXT(field) = result;
2614 pp = &DECL_CHAIN(field);
2616 TYPE_FIELDS(result) = field_trees;
2617 layout_type(result);
2620 if (result == error_mark_node)
2621 return error_mark_node;
2623 tree fntype = build_function_type(result, args);
2624 if (fntype == error_mark_node)
2627 return build_pointer_type(fntype);
2630 // Functions are initialized to NULL.
2633 Function_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2637 return fold_convert(type_tree, null_pointer_node);
2640 // The type of a function type descriptor.
2643 Function_type::make_function_type_descriptor_type()
2648 Type* tdt = Type::make_type_descriptor_type();
2649 Type* ptdt = Type::make_type_descriptor_ptr_type();
2651 Type* bool_type = Type::lookup_bool_type();
2653 Type* slice_type = Type::make_array_type(ptdt, NULL);
2655 Struct_type* s = Type::make_builtin_struct_type(4,
2657 "dotdotdot", bool_type,
2661 ret = Type::make_builtin_named_type("FuncType", s);
2667 // The type descriptor for a function type.
2670 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2672 source_location bloc = BUILTINS_LOCATION;
2674 Type* ftdt = Function_type::make_function_type_descriptor_type();
2676 const Struct_field_list* fields = ftdt->struct_type()->fields();
2678 Expression_list* vals = new Expression_list();
2681 Struct_field_list::const_iterator p = fields->begin();
2682 gcc_assert(p->field_name() == "commonType");
2683 vals->push_back(this->type_descriptor_constructor(gogo,
2684 RUNTIME_TYPE_KIND_FUNC,
2688 gcc_assert(p->field_name() == "dotdotdot");
2689 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
2692 gcc_assert(p->field_name() == "in");
2693 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
2694 this->parameters()));
2697 gcc_assert(p->field_name() == "out");
2698 vals->push_back(this->type_descriptor_params(p->type(), NULL,
2702 gcc_assert(p == fields->end());
2704 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
2707 // Return a composite literal for the parameters or results of a type
2711 Function_type::type_descriptor_params(Type* params_type,
2712 const Typed_identifier* receiver,
2713 const Typed_identifier_list* params)
2715 source_location bloc = BUILTINS_LOCATION;
2717 if (receiver == NULL && params == NULL)
2718 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
2720 Expression_list* vals = new Expression_list();
2721 vals->reserve((params == NULL ? 0 : params->size())
2722 + (receiver != NULL ? 1 : 0));
2724 if (receiver != NULL)
2726 Type* rtype = receiver->type();
2727 // The receiver is always passed as a pointer. FIXME: Is this
2728 // right? Should that fact affect the type descriptor?
2729 if (rtype->points_to() == NULL)
2730 rtype = Type::make_pointer_type(rtype);
2731 vals->push_back(Expression::make_type_descriptor(rtype, bloc));
2736 for (Typed_identifier_list::const_iterator p = params->begin();
2739 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
2742 return Expression::make_slice_composite_literal(params_type, vals, bloc);
2745 // The reflection string.
2748 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
2750 // FIXME: Turn this off until we straighten out the type of the
2751 // struct field used in a go statement which calls a method.
2752 // gcc_assert(this->receiver_ == NULL);
2754 ret->append("func");
2756 if (this->receiver_ != NULL)
2758 ret->push_back('(');
2759 this->append_reflection(this->receiver_->type(), gogo, ret);
2760 ret->push_back(')');
2763 ret->push_back('(');
2764 const Typed_identifier_list* params = this->parameters();
2767 bool is_varargs = this->is_varargs_;
2768 for (Typed_identifier_list::const_iterator p = params->begin();
2772 if (p != params->begin())
2774 if (!is_varargs || p + 1 != params->end())
2775 this->append_reflection(p->type(), gogo, ret);
2779 this->append_reflection(p->type()->array_type()->element_type(),
2784 ret->push_back(')');
2786 const Typed_identifier_list* results = this->results();
2787 if (results != NULL && !results->empty())
2789 if (results->size() == 1)
2790 ret->push_back(' ');
2793 for (Typed_identifier_list::const_iterator p = results->begin();
2794 p != results->end();
2797 if (p != results->begin())
2799 this->append_reflection(p->type(), gogo, ret);
2801 if (results->size() > 1)
2802 ret->push_back(')');
2809 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
2811 ret->push_back('F');
2813 if (this->receiver_ != NULL)
2815 ret->push_back('m');
2816 this->append_mangled_name(this->receiver_->type(), gogo, ret);
2819 const Typed_identifier_list* params = this->parameters();
2822 ret->push_back('p');
2823 for (Typed_identifier_list::const_iterator p = params->begin();
2826 this->append_mangled_name(p->type(), gogo, ret);
2827 if (this->is_varargs_)
2828 ret->push_back('V');
2829 ret->push_back('e');
2832 const Typed_identifier_list* results = this->results();
2833 if (results != NULL)
2835 ret->push_back('r');
2836 for (Typed_identifier_list::const_iterator p = results->begin();
2837 p != results->end();
2839 this->append_mangled_name(p->type(), gogo, ret);
2840 ret->push_back('e');
2843 ret->push_back('e');
2846 // Export a function type.
2849 Function_type::do_export(Export* exp) const
2851 // We don't write out the receiver. The only function types which
2852 // should have a receiver are the ones associated with explicitly
2853 // defined methods. For those the receiver type is written out by
2854 // Function::export_func.
2856 exp->write_c_string("(");
2858 if (this->parameters_ != NULL)
2860 bool is_varargs = this->is_varargs_;
2861 for (Typed_identifier_list::const_iterator p =
2862 this->parameters_->begin();
2863 p != this->parameters_->end();
2869 exp->write_c_string(", ");
2870 if (!is_varargs || p + 1 != this->parameters_->end())
2871 exp->write_type(p->type());
2874 exp->write_c_string("...");
2875 exp->write_type(p->type()->array_type()->element_type());
2879 exp->write_c_string(")");
2881 const Typed_identifier_list* results = this->results_;
2882 if (results != NULL)
2884 exp->write_c_string(" ");
2885 if (results->size() == 1)
2886 exp->write_type(results->begin()->type());
2890 exp->write_c_string("(");
2891 for (Typed_identifier_list::const_iterator p = results->begin();
2892 p != results->end();
2898 exp->write_c_string(", ");
2899 exp->write_type(p->type());
2901 exp->write_c_string(")");
2906 // Import a function type.
2909 Function_type::do_import(Import* imp)
2911 imp->require_c_string("(");
2912 Typed_identifier_list* parameters;
2913 bool is_varargs = false;
2914 if (imp->peek_char() == ')')
2918 parameters = new Typed_identifier_list();
2921 if (imp->match_c_string("..."))
2927 Type* ptype = imp->read_type();
2929 ptype = Type::make_array_type(ptype, NULL);
2930 parameters->push_back(Typed_identifier(Import::import_marker,
2931 ptype, imp->location()));
2932 if (imp->peek_char() != ',')
2934 gcc_assert(!is_varargs);
2935 imp->require_c_string(", ");
2938 imp->require_c_string(")");
2940 Typed_identifier_list* results;
2941 if (imp->peek_char() != ' ')
2946 results = new Typed_identifier_list;
2947 if (imp->peek_char() != '(')
2949 Type* rtype = imp->read_type();
2950 results->push_back(Typed_identifier(Import::import_marker, rtype,
2958 Type* rtype = imp->read_type();
2959 results->push_back(Typed_identifier(Import::import_marker,
2960 rtype, imp->location()));
2961 if (imp->peek_char() != ',')
2963 imp->require_c_string(", ");
2965 imp->require_c_string(")");
2969 Function_type* ret = Type::make_function_type(NULL, parameters, results,
2972 ret->set_is_varargs();
2976 // Make a copy of a function type without a receiver.
2979 Function_type::copy_without_receiver() const
2981 gcc_assert(this->is_method());
2982 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
2985 if (this->is_varargs())
2986 ret->set_is_varargs();
2987 if (this->is_builtin())
2988 ret->set_is_builtin();
2992 // Make a copy of a function type with a receiver.
2995 Function_type::copy_with_receiver(Type* receiver_type) const
2997 gcc_assert(!this->is_method());
2998 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3000 return Type::make_function_type(receiver, this->parameters_,
3001 this->results_, this->location_);
3004 // Make a function type.
3007 Type::make_function_type(Typed_identifier* receiver,
3008 Typed_identifier_list* parameters,
3009 Typed_identifier_list* results,
3010 source_location location)
3012 return new Function_type(receiver, parameters, results, location);
3015 // Class Pointer_type.
3020 Pointer_type::do_traverse(Traverse* traverse)
3022 return Type::traverse(this->to_type_, traverse);
3028 Pointer_type::do_hash_for_method(Gogo* gogo) const
3030 return this->to_type_->hash_for_method(gogo) << 4;
3033 // The tree for a pointer type.
3036 Pointer_type::do_get_tree(Gogo* gogo)
3038 return build_pointer_type(this->to_type_->get_tree(gogo));
3041 // Initialize a pointer type.
3044 Pointer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3048 return fold_convert(type_tree, null_pointer_node);
3051 // The type of a pointer type descriptor.
3054 Pointer_type::make_pointer_type_descriptor_type()
3059 Type* tdt = Type::make_type_descriptor_type();
3060 Type* ptdt = Type::make_type_descriptor_ptr_type();
3062 Struct_type* s = Type::make_builtin_struct_type(2,
3066 ret = Type::make_builtin_named_type("PtrType", s);
3072 // The type descriptor for a pointer type.
3075 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3077 if (this->is_unsafe_pointer_type())
3079 gcc_assert(name != NULL);
3080 return this->plain_type_descriptor(gogo,
3081 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3086 source_location bloc = BUILTINS_LOCATION;
3088 const Methods* methods;
3089 Type* deref = this->points_to();
3090 if (deref->named_type() != NULL)
3091 methods = deref->named_type()->methods();
3092 else if (deref->struct_type() != NULL)
3093 methods = deref->struct_type()->methods();
3097 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3099 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3101 Expression_list* vals = new Expression_list();
3104 Struct_field_list::const_iterator p = fields->begin();
3105 gcc_assert(p->field_name() == "commonType");
3106 vals->push_back(this->type_descriptor_constructor(gogo,
3107 RUNTIME_TYPE_KIND_PTR,
3108 name, methods, false));
3111 gcc_assert(p->field_name() == "elem");
3112 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3114 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3118 // Reflection string.
3121 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3123 ret->push_back('*');
3124 this->append_reflection(this->to_type_, gogo, ret);
3130 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3132 ret->push_back('p');
3133 this->append_mangled_name(this->to_type_, gogo, ret);
3139 Pointer_type::do_export(Export* exp) const
3141 exp->write_c_string("*");
3142 if (this->is_unsafe_pointer_type())
3143 exp->write_c_string("any");
3145 exp->write_type(this->to_type_);
3151 Pointer_type::do_import(Import* imp)
3153 imp->require_c_string("*");
3154 if (imp->match_c_string("any"))
3157 return Type::make_pointer_type(Type::make_void_type());
3159 Type* to = imp->read_type();
3160 return Type::make_pointer_type(to);
3163 // Make a pointer type.
3166 Type::make_pointer_type(Type* to_type)
3168 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3169 static Hashtable pointer_types;
3170 Hashtable::const_iterator p = pointer_types.find(to_type);
3171 if (p != pointer_types.end())
3173 Pointer_type* ret = new Pointer_type(to_type);
3174 pointer_types[to_type] = ret;
3178 // The nil type. We use a special type for nil because it is not the
3179 // same as any other type. In C term nil has type void*, but there is
3180 // no such type in Go.
3182 class Nil_type : public Type
3192 { return ptr_type_node; }
3195 do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3196 { return is_clear ? NULL : fold_convert(type_tree, null_pointer_node); }
3199 do_type_descriptor(Gogo*, Named_type*)
3200 { gcc_unreachable(); }
3203 do_reflection(Gogo*, std::string*) const
3204 { gcc_unreachable(); }
3207 do_mangled_name(Gogo*, std::string* ret) const
3208 { ret->push_back('n'); }
3211 // Make the nil type.
3214 Type::make_nil_type()
3216 static Nil_type singleton_nil_type;
3217 return &singleton_nil_type;
3220 // The type of a function call which returns multiple values. This is
3221 // really a struct, but we don't want to confuse a function call which
3222 // returns a struct with a function call which returns multiple
3225 class Call_multiple_result_type : public Type
3228 Call_multiple_result_type(Call_expression* call)
3229 : Type(TYPE_CALL_MULTIPLE_RESULT),
3235 do_has_pointer() const
3236 { gcc_unreachable(); }
3242 do_get_init_tree(Gogo*, tree, bool)
3243 { gcc_unreachable(); }
3246 do_type_descriptor(Gogo*, Named_type*)
3247 { gcc_unreachable(); }
3250 do_reflection(Gogo*, std::string*) const
3251 { gcc_unreachable(); }
3254 do_mangled_name(Gogo*, std::string*) const
3255 { gcc_unreachable(); }
3258 // The expression being called.
3259 Call_expression* call_;
3262 // Return the tree for a call result.
3265 Call_multiple_result_type::do_get_tree(Gogo* gogo)
3267 Function_type* fntype = this->call_->get_function_type();
3268 gcc_assert(fntype != NULL);
3269 const Typed_identifier_list* results = fntype->results();
3270 gcc_assert(results != NULL && results->size() > 1);
3272 Struct_field_list* sfl = new Struct_field_list;
3273 for (Typed_identifier_list::const_iterator p = results->begin();
3274 p != results->end();
3277 const std::string name = ((p->name().empty()
3278 || p->name() == Import::import_marker)
3281 sfl->push_back(Struct_field(Typed_identifier(name, p->type(),
3282 this->call_->location())));
3284 return Type::make_struct_type(sfl, this->call_->location())->get_tree(gogo);
3287 // Make a call result type.
3290 Type::make_call_multiple_result_type(Call_expression* call)
3292 return new Call_multiple_result_type(call);
3295 // Class Struct_field.
3297 // Get the name of a field.
3300 Struct_field::field_name() const
3302 const std::string& name(this->typed_identifier_.name());
3307 // This is called during parsing, before anything is lowered, so
3308 // we have to be pretty careful to avoid dereferencing an
3309 // unknown type name.
3310 Type* t = this->typed_identifier_.type();
3312 if (t->classification() == Type::TYPE_POINTER)
3315 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3316 dt = ptype->points_to();
3318 if (dt->forward_declaration_type() != NULL)
3319 return dt->forward_declaration_type()->name();
3320 else if (dt->named_type() != NULL)
3321 return dt->named_type()->name();
3322 else if (t->is_error_type() || dt->is_error_type())
3324 static const std::string error_string = "*error*";
3325 return error_string;
3329 // Avoid crashing in the erroneous case where T is named but
3331 gcc_assert(t != dt);
3332 if (t->forward_declaration_type() != NULL)
3333 return t->forward_declaration_type()->name();
3334 else if (t->named_type() != NULL)
3335 return t->named_type()->name();
3342 // Class Struct_type.
3347 Struct_type::do_traverse(Traverse* traverse)
3349 Struct_field_list* fields = this->fields_;
3352 for (Struct_field_list::iterator p = fields->begin();
3356 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3357 return TRAVERSE_EXIT;
3360 return TRAVERSE_CONTINUE;
3363 // Verify that the struct type is complete and valid.
3366 Struct_type::do_verify()
3368 Struct_field_list* fields = this->fields_;
3371 for (Struct_field_list::iterator p = fields->begin();
3375 Type* t = p->type();
3376 if (t->is_undefined())
3378 error_at(p->location(), "struct field type is incomplete");
3379 p->set_type(Type::make_error_type());
3382 else if (p->is_anonymous())
3384 if (t->named_type() != NULL && t->points_to() != NULL)
3386 error_at(p->location(), "embedded type may not be a pointer");
3387 p->set_type(Type::make_error_type());
3395 // Whether this contains a pointer.
3398 Struct_type::do_has_pointer() const
3400 const Struct_field_list* fields = this->fields();
3403 for (Struct_field_list::const_iterator p = fields->begin();
3407 if (p->type()->has_pointer())
3413 // Whether this type is identical to T.
3416 Struct_type::is_identical(const Struct_type* t) const
3418 const Struct_field_list* fields1 = this->fields();
3419 const Struct_field_list* fields2 = t->fields();
3420 if (fields1 == NULL || fields2 == NULL)
3421 return fields1 == fields2;
3422 Struct_field_list::const_iterator pf2 = fields2->begin();
3423 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3424 pf1 != fields1->end();
3427 if (pf2 == fields2->end())
3429 if (pf1->field_name() != pf2->field_name())
3431 if (pf1->is_anonymous() != pf2->is_anonymous()
3432 || !Type::are_identical(pf1->type(), pf2->type(), NULL))
3434 if (!pf1->has_tag())
3441 if (!pf2->has_tag())
3443 if (pf1->tag() != pf2->tag())
3447 if (pf2 != fields2->end())
3452 // Whether this struct type has any hidden fields.
3455 Struct_type::struct_has_hidden_fields(const Named_type* within,
3456 std::string* reason) const
3458 const Struct_field_list* fields = this->fields();
3461 const Package* within_package = (within == NULL
3463 : within->named_object()->package());
3464 for (Struct_field_list::const_iterator pf = fields->begin();
3465 pf != fields->end();
3468 if (within_package != NULL
3469 && !pf->is_anonymous()
3470 && Gogo::is_hidden_name(pf->field_name()))
3474 std::string within_name = within->named_object()->message_name();
3475 std::string name = Gogo::message_name(pf->field_name());
3476 size_t bufsize = 200 + within_name.length() + name.length();
3477 char* buf = new char[bufsize];
3478 snprintf(buf, bufsize,
3479 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3480 open_quote, within_name.c_str(), close_quote,
3481 open_quote, name.c_str(), close_quote);
3482 reason->assign(buf);
3488 if (pf->type()->has_hidden_fields(within, reason))
3498 Struct_type::do_hash_for_method(Gogo* gogo) const
3500 unsigned int ret = 0;
3501 if (this->fields() != NULL)
3503 for (Struct_field_list::const_iterator pf = this->fields()->begin();
3504 pf != this->fields()->end();
3506 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
3511 // Find the local field NAME.
3514 Struct_type::find_local_field(const std::string& name,
3515 unsigned int *pindex) const
3517 const Struct_field_list* fields = this->fields_;
3521 for (Struct_field_list::const_iterator pf = fields->begin();
3522 pf != fields->end();
3525 if (pf->field_name() == name)
3535 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3537 Field_reference_expression*
3538 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
3539 source_location location) const
3542 return this->field_reference_depth(struct_expr, name, location, &depth);
3545 // Return an expression for a field, along with the depth at which it
3548 Field_reference_expression*
3549 Struct_type::field_reference_depth(Expression* struct_expr,
3550 const std::string& name,
3551 source_location location,
3552 unsigned int* depth) const
3554 const Struct_field_list* fields = this->fields_;
3558 // Look for a field with this name.
3560 for (Struct_field_list::const_iterator pf = fields->begin();
3561 pf != fields->end();
3564 if (pf->field_name() == name)
3567 return Expression::make_field_reference(struct_expr, i, location);
3571 // Look for an anonymous field which contains a field with this
3573 unsigned int found_depth = 0;
3574 Field_reference_expression* ret = NULL;
3576 for (Struct_field_list::const_iterator pf = fields->begin();
3577 pf != fields->end();
3580 if (!pf->is_anonymous())
3583 Struct_type* st = pf->type()->deref()->struct_type();
3587 // Look for a reference using a NULL struct expression. If we
3588 // find one, fill in the struct expression with a reference to
3590 unsigned int subdepth;
3591 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
3597 if (ret == NULL || subdepth < found_depth)
3602 found_depth = subdepth;
3603 Expression* here = Expression::make_field_reference(struct_expr, i,
3605 if (pf->type()->points_to() != NULL)
3606 here = Expression::make_unary(OPERATOR_MULT, here, location);
3607 while (sub->expr() != NULL)
3609 sub = sub->expr()->deref()->field_reference_expression();
3610 gcc_assert(sub != NULL);
3612 sub->set_struct_expression(here);
3614 else if (subdepth > found_depth)
3618 // We do not handle ambiguity here--it should be handled by
3619 // Type::bind_field_or_method.
3627 *depth = found_depth + 1;
3632 // Return the total number of fields, including embedded fields.
3635 Struct_type::total_field_count() const
3637 if (this->fields_ == NULL)
3639 unsigned int ret = 0;
3640 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3641 pf != this->fields_->end();
3644 if (!pf->is_anonymous() || pf->type()->deref()->struct_type() == NULL)
3647 ret += pf->type()->struct_type()->total_field_count();
3652 // Return whether NAME is an unexported field, for better error reporting.
3655 Struct_type::is_unexported_local_field(Gogo* gogo,
3656 const std::string& name) const
3658 const Struct_field_list* fields = this->fields_;
3661 for (Struct_field_list::const_iterator pf = fields->begin();
3662 pf != fields->end();
3665 const std::string& field_name(pf->field_name());
3666 if (Gogo::is_hidden_name(field_name)
3667 && name == Gogo::unpack_hidden_name(field_name)
3668 && gogo->pack_hidden_name(name, false) != field_name)
3675 // Finalize the methods of an unnamed struct.
3678 Struct_type::finalize_methods(Gogo* gogo)
3680 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
3683 // Return the method NAME, or NULL if there isn't one or if it is
3684 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3688 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
3690 return Type::method_function(this->all_methods_, name, is_ambiguous);
3693 // Get the tree for a struct type.
3696 Struct_type::do_get_tree(Gogo* gogo)
3698 tree type = make_node(RECORD_TYPE);
3699 return this->fill_in_tree(gogo, type);
3702 // Fill in the fields for a struct type.
3705 Struct_type::fill_in_tree(Gogo* gogo, tree type)
3707 tree field_trees = NULL_TREE;
3708 tree* pp = &field_trees;
3709 for (Struct_field_list::const_iterator p = this->fields_->begin();
3710 p != this->fields_->end();
3713 std::string name = Gogo::unpack_hidden_name(p->field_name());
3714 tree name_tree = get_identifier_with_length(name.data(), name.length());
3715 tree field_type_tree = p->type()->get_tree(gogo);
3716 if (field_type_tree == error_mark_node)
3717 return error_mark_node;
3718 tree field = build_decl(p->location(), FIELD_DECL, name_tree,
3720 DECL_CONTEXT(field) = type;
3722 pp = &DECL_CHAIN(field);
3725 TYPE_FIELDS(type) = field_trees;
3732 // Initialize struct fields.
3735 Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
3737 if (this->fields_ == NULL || this->fields_->empty())
3743 tree ret = build_constructor(type_tree,
3744 VEC_alloc(constructor_elt, gc, 0));
3745 TREE_CONSTANT(ret) = 1;
3750 bool is_constant = true;
3751 bool any_fields_set = false;
3752 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc,
3753 this->fields_->size());
3754 Struct_field_list::const_iterator p = this->fields_->begin();
3755 for (tree field = TYPE_FIELDS(type_tree);
3757 field = DECL_CHAIN(field), ++p)
3759 gcc_assert(p != this->fields_->end());
3760 tree value = p->type()->get_init_tree(gogo, is_clear);
3763 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
3766 any_fields_set = true;
3767 if (!TREE_CONSTANT(value))
3768 is_constant = false;
3771 gcc_assert(p == this->fields_->end());
3773 if (!any_fields_set)
3775 gcc_assert(is_clear);
3776 VEC_free(constructor_elt, gc, init);
3780 tree ret = build_constructor(type_tree, init);
3782 TREE_CONSTANT(ret) = 1;
3786 // The type of a struct type descriptor.
3789 Struct_type::make_struct_type_descriptor_type()
3794 Type* tdt = Type::make_type_descriptor_type();
3795 Type* ptdt = Type::make_type_descriptor_ptr_type();
3797 Type* uintptr_type = Type::lookup_integer_type("uintptr");
3798 Type* string_type = Type::lookup_string_type();
3799 Type* pointer_string_type = Type::make_pointer_type(string_type);
3802 Type::make_builtin_struct_type(5,
3803 "name", pointer_string_type,
3804 "pkgPath", pointer_string_type,
3806 "tag", pointer_string_type,
3807 "offset", uintptr_type);
3808 Type* nsf = Type::make_builtin_named_type("structField", sf);
3810 Type* slice_type = Type::make_array_type(nsf, NULL);
3812 Struct_type* s = Type::make_builtin_struct_type(2,
3814 "fields", slice_type);
3816 ret = Type::make_builtin_named_type("StructType", s);
3822 // Build a type descriptor for a struct type.
3825 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3827 source_location bloc = BUILTINS_LOCATION;
3829 Type* stdt = Struct_type::make_struct_type_descriptor_type();
3831 const Struct_field_list* fields = stdt->struct_type()->fields();
3833 Expression_list* vals = new Expression_list();
3836 const Methods* methods = this->methods();
3837 // A named struct should not have methods--the methods should attach
3838 // to the named type.
3839 gcc_assert(methods == NULL || name == NULL);
3841 Struct_field_list::const_iterator ps = fields->begin();
3842 gcc_assert(ps->field_name() == "commonType");
3843 vals->push_back(this->type_descriptor_constructor(gogo,
3844 RUNTIME_TYPE_KIND_STRUCT,
3845 name, methods, true));
3848 gcc_assert(ps->field_name() == "fields");
3850 Expression_list* elements = new Expression_list();
3851 elements->reserve(this->fields_->size());
3852 Type* element_type = ps->type()->array_type()->element_type();
3853 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3854 pf != this->fields_->end();
3857 const Struct_field_list* f = element_type->struct_type()->fields();
3859 Expression_list* fvals = new Expression_list();
3862 Struct_field_list::const_iterator q = f->begin();
3863 gcc_assert(q->field_name() == "name");
3864 if (pf->is_anonymous())
3865 fvals->push_back(Expression::make_nil(bloc));
3868 std::string n = Gogo::unpack_hidden_name(pf->field_name());
3869 Expression* s = Expression::make_string(n, bloc);
3870 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3874 gcc_assert(q->field_name() == "pkgPath");
3875 if (!Gogo::is_hidden_name(pf->field_name()))
3876 fvals->push_back(Expression::make_nil(bloc));
3879 std::string n = Gogo::hidden_name_prefix(pf->field_name());
3880 Expression* s = Expression::make_string(n, bloc);
3881 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3885 gcc_assert(q->field_name() == "typ");
3886 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
3889 gcc_assert(q->field_name() == "tag");
3891 fvals->push_back(Expression::make_nil(bloc));
3894 Expression* s = Expression::make_string(pf->tag(), bloc);
3895 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3899 gcc_assert(q->field_name() == "offset");
3900 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
3902 Expression* v = Expression::make_struct_composite_literal(element_type,
3904 elements->push_back(v);
3907 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
3910 return Expression::make_struct_composite_literal(stdt, vals, bloc);
3913 // Reflection string.
3916 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
3918 ret->append("struct { ");
3920 for (Struct_field_list::const_iterator p = this->fields_->begin();
3921 p != this->fields_->end();
3924 if (p != this->fields_->begin())
3926 if (p->is_anonymous())
3927 ret->push_back('?');
3929 ret->append(Gogo::unpack_hidden_name(p->field_name()));
3930 ret->push_back(' ');
3931 this->append_reflection(p->type(), gogo, ret);
3935 const std::string& tag(p->tag());
3937 for (std::string::const_iterator p = tag.begin();
3942 ret->append("\\x00");
3943 else if (*p == '\n')
3945 else if (*p == '\t')
3948 ret->append("\\\"");
3949 else if (*p == '\\')
3950 ret->append("\\\\");
3954 ret->push_back('"');
3964 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3966 ret->push_back('S');
3968 const Struct_field_list* fields = this->fields_;
3971 for (Struct_field_list::const_iterator p = fields->begin();
3975 if (p->is_anonymous())
3979 std::string n = Gogo::unpack_hidden_name(p->field_name());
3981 snprintf(buf, sizeof buf, "%u_",
3982 static_cast<unsigned int>(n.length()));
3986 this->append_mangled_name(p->type(), gogo, ret);
3989 const std::string& tag(p->tag());
3991 for (std::string::const_iterator p = tag.begin();
3995 if (ISALNUM(*p) || *p == '_')
4000 snprintf(buf, sizeof buf, ".%x.",
4001 static_cast<unsigned int>(*p));
4006 snprintf(buf, sizeof buf, "T%u_",
4007 static_cast<unsigned int>(out.length()));
4014 ret->push_back('e');
4020 Struct_type::do_export(Export* exp) const
4022 exp->write_c_string("struct { ");
4023 const Struct_field_list* fields = this->fields_;
4024 gcc_assert(fields != NULL);
4025 for (Struct_field_list::const_iterator p = fields->begin();
4029 if (p->is_anonymous())
4030 exp->write_string("? ");
4033 exp->write_string(p->field_name());
4034 exp->write_c_string(" ");
4036 exp->write_type(p->type());
4040 exp->write_c_string(" ");
4041 Expression* expr = Expression::make_string(p->tag(),
4043 expr->export_expression(exp);
4047 exp->write_c_string("; ");
4049 exp->write_c_string("}");
4055 Struct_type::do_import(Import* imp)
4057 imp->require_c_string("struct { ");
4058 Struct_field_list* fields = new Struct_field_list;
4059 if (imp->peek_char() != '}')
4064 if (imp->match_c_string("? "))
4068 name = imp->read_identifier();
4069 imp->require_c_string(" ");
4071 Type* ftype = imp->read_type();
4073 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4075 if (imp->peek_char() == ' ')
4078 Expression* expr = Expression::import_expression(imp);
4079 String_expression* sexpr = expr->string_expression();
4080 gcc_assert(sexpr != NULL);
4081 sf.set_tag(sexpr->val());
4085 imp->require_c_string("; ");
4086 fields->push_back(sf);
4087 if (imp->peek_char() == '}')
4091 imp->require_c_string("}");
4093 return Type::make_struct_type(fields, imp->location());
4096 // Make a struct type.
4099 Type::make_struct_type(Struct_field_list* fields,
4100 source_location location)
4102 return new Struct_type(fields, location);
4105 // Class Array_type.
4107 // Whether two array types are identical.
4110 Array_type::is_identical(const Array_type* t) const
4112 if (!Type::are_identical(this->element_type(), t->element_type(), NULL))
4115 Expression* l1 = this->length();
4116 Expression* l2 = t->length();
4118 // Slices of the same element type are identical.
4119 if (l1 == NULL && l2 == NULL)
4122 // Arrays of the same element type are identical if they have the
4124 if (l1 != NULL && l2 != NULL)
4129 // Try to determine the lengths. If we can't, assume the arrays
4130 // are not identical.
4138 if (l1->integer_constant_value(true, v1, &type1)
4139 && l2->integer_constant_value(true, v2, &type2))
4140 ret = mpz_cmp(v1, v2) == 0;
4146 // Otherwise the arrays are not identical.
4153 Array_type::do_traverse(Traverse* traverse)
4155 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
4156 return TRAVERSE_EXIT;
4157 if (this->length_ != NULL
4158 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
4159 return TRAVERSE_EXIT;
4160 return TRAVERSE_CONTINUE;
4163 // Check that the length is valid.
4166 Array_type::verify_length()
4168 if (this->length_ == NULL)
4170 if (!this->length_->is_constant())
4172 error_at(this->length_->location(), "array bound is not constant");
4178 Type* t = this->length_->type();
4179 if (t->integer_type() != NULL)
4183 if (!this->length_->integer_constant_value(true, val, &vt))
4185 error_at(this->length_->location(),
4186 "array bound is not constant");
4191 else if (t->float_type() != NULL)
4196 if (!this->length_->float_constant_value(fval, &vt))
4198 error_at(this->length_->location(),
4199 "array bound is not constant");
4203 if (!mpfr_integer_p(fval))