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"
39 Type::Type(Type_classification classification)
40 : classification_(classification), btype_(NULL), type_descriptor_var_(NULL)
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 go_assert(this->is_abstract());
192 switch (this->classification())
195 return Type::lookup_integer_type("int");
197 return Type::lookup_float_type("float64");
199 return Type::lookup_complex_type("complex128");
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_slice_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 go_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);
404 case TYPE_CALL_MULTIPLE_RESULT:
406 *reason = "invalid use of multiple value function call";
414 // Return true if it's OK to have a binary operation with types LHS
415 // and RHS. This is not used for shifts or comparisons.
418 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
420 if (Type::are_identical(lhs, rhs, true, NULL))
423 // A constant of abstract bool type may be mixed with any bool type.
424 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
425 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
428 // A constant of abstract string type may be mixed with any string
430 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
431 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
437 // A constant of abstract integer, float, or complex type may be
438 // mixed with an integer, float, or complex type.
439 if ((rhs->is_abstract()
440 && (rhs->integer_type() != NULL
441 || rhs->float_type() != NULL
442 || rhs->complex_type() != NULL)
443 && (lhs->integer_type() != NULL
444 || lhs->float_type() != NULL
445 || lhs->complex_type() != NULL))
446 || (lhs->is_abstract()
447 && (lhs->integer_type() != NULL
448 || lhs->float_type() != NULL
449 || lhs->complex_type() != NULL)
450 && (rhs->integer_type() != NULL
451 || rhs->float_type() != NULL
452 || rhs->complex_type() != NULL)))
455 // The nil type may be compared to a pointer, an interface type, a
456 // slice type, a channel type, a map type, or a function type.
457 if (lhs->is_nil_type()
458 && (rhs->points_to() != NULL
459 || rhs->interface_type() != NULL
460 || rhs->is_slice_type()
461 || rhs->map_type() != NULL
462 || rhs->channel_type() != NULL
463 || rhs->function_type() != NULL))
465 if (rhs->is_nil_type()
466 && (lhs->points_to() != NULL
467 || lhs->interface_type() != NULL
468 || lhs->is_slice_type()
469 || lhs->map_type() != NULL
470 || lhs->channel_type() != NULL
471 || lhs->function_type() != NULL))
477 // Return true if a value with type T1 may be compared with a value of
478 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
481 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
482 const Type *t2, std::string *reason)
485 && !Type::are_assignable(t1, t2, NULL)
486 && !Type::are_assignable(t2, t1, NULL))
489 *reason = "incompatible types in binary expression";
495 if (t1->integer_type() == NULL
496 && t1->float_type() == NULL
497 && !t1->is_string_type())
500 *reason = _("invalid comparison of non-ordered type");
504 else if (t1->is_slice_type()
505 || t1->map_type() != NULL
506 || t1->function_type() != NULL
507 || t2->is_slice_type()
508 || t2->map_type() != NULL
509 || t2->function_type() != NULL)
511 if (!t1->is_nil_type() && !t2->is_nil_type())
515 if (t1->is_slice_type() || t2->is_slice_type())
516 *reason = _("slice can only be compared to nil");
517 else if (t1->map_type() != NULL || t2->map_type() != NULL)
518 *reason = _("map can only be compared to nil");
520 *reason = _("func can only be compared to nil");
522 // Match 6g error messages.
523 if (t1->interface_type() != NULL || t2->interface_type() != NULL)
526 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
536 if (!t1->is_boolean_type()
537 && t1->integer_type() == NULL
538 && t1->float_type() == NULL
539 && t1->complex_type() == NULL
540 && !t1->is_string_type()
541 && t1->points_to() == NULL
542 && t1->channel_type() == NULL
543 && t1->interface_type() == NULL
544 && t1->struct_type() == NULL
545 && t1->array_type() == NULL
546 && !t1->is_nil_type())
549 *reason = _("invalid comparison of non-comparable type");
553 if (t1->named_type() != NULL)
554 return t1->named_type()->named_type_is_comparable(reason);
555 else if (t2->named_type() != NULL)
556 return t2->named_type()->named_type_is_comparable(reason);
557 else if (t1->struct_type() != NULL)
559 const Struct_field_list* fields = t1->struct_type()->fields();
560 for (Struct_field_list::const_iterator p = fields->begin();
564 if (!p->type()->is_comparable())
567 *reason = _("invalid comparison of non-comparable struct");
572 else if (t1->array_type() != NULL)
574 if (t1->array_type()->length()->is_nil_expression()
575 || !t1->array_type()->element_type()->is_comparable())
578 *reason = _("invalid comparison of non-comparable array");
587 // Return true if a value with type RHS may be assigned to a variable
588 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
589 // hidden fields are modified. If REASON is not NULL, set *REASON to
590 // the reason the types are not assignable.
593 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
594 bool check_hidden_fields,
597 // Do some checks first. Make sure the types are defined.
599 && rhs->forwarded()->forward_declaration_type() == NULL
600 && rhs->is_void_type())
603 *reason = "non-value used as value";
607 if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
609 // Any value may be assigned to the blank identifier.
610 if (lhs->is_sink_type())
613 // All fields of a struct must be exported, or the assignment
614 // must be in the same package.
615 if (check_hidden_fields
617 && rhs->forwarded()->forward_declaration_type() == NULL)
619 if (lhs->has_hidden_fields(NULL, reason)
620 || rhs->has_hidden_fields(NULL, reason))
625 // Identical types are assignable.
626 if (Type::are_identical(lhs, rhs, true, reason))
629 // The types are assignable if they have identical underlying types
630 // and either LHS or RHS is not a named type.
631 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
632 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
633 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
636 // The types are assignable if LHS is an interface type and RHS
637 // implements the required methods.
638 const Interface_type* lhs_interface_type = lhs->interface_type();
639 if (lhs_interface_type != NULL)
641 if (lhs_interface_type->implements_interface(rhs, reason))
643 const Interface_type* rhs_interface_type = rhs->interface_type();
644 if (rhs_interface_type != NULL
645 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
650 // The type are assignable if RHS is a bidirectional channel type,
651 // LHS is a channel type, they have identical element types, and
652 // either LHS or RHS is not a named type.
653 if (lhs->channel_type() != NULL
654 && rhs->channel_type() != NULL
655 && rhs->channel_type()->may_send()
656 && rhs->channel_type()->may_receive()
657 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
658 && Type::are_identical(lhs->channel_type()->element_type(),
659 rhs->channel_type()->element_type(),
664 // The nil type may be assigned to a pointer, function, slice, map,
665 // channel, or interface type.
666 if (rhs->is_nil_type()
667 && (lhs->points_to() != NULL
668 || lhs->function_type() != NULL
669 || lhs->is_slice_type()
670 || lhs->map_type() != NULL
671 || lhs->channel_type() != NULL
672 || lhs->interface_type() != NULL))
675 // An untyped numeric constant may be assigned to a numeric type if
676 // it is representable in that type.
677 if ((rhs->is_abstract()
678 && (rhs->integer_type() != NULL
679 || rhs->float_type() != NULL
680 || rhs->complex_type() != NULL))
681 && (lhs->integer_type() != NULL
682 || lhs->float_type() != NULL
683 || lhs->complex_type() != NULL))
686 // Give some better error messages.
687 if (reason != NULL && reason->empty())
689 if (rhs->interface_type() != NULL)
690 reason->assign(_("need explicit conversion"));
691 else if (rhs->is_call_multiple_result_type())
692 reason->assign(_("multiple value function call in "
693 "single value context"));
694 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
696 size_t len = (lhs->named_type()->name().length()
697 + rhs->named_type()->name().length()
699 char* buf = new char[len];
700 snprintf(buf, len, _("cannot use type %s as type %s"),
701 rhs->named_type()->message_name().c_str(),
702 lhs->named_type()->message_name().c_str());
711 // Return true if a value with type RHS may be assigned to a variable
712 // with type LHS. If REASON is not NULL, set *REASON to the reason
713 // the types are not assignable.
716 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
718 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
721 // Like are_assignable but don't check for hidden fields.
724 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
727 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
730 // Return true if a value with type RHS may be converted to type LHS.
731 // If REASON is not NULL, set *REASON to the reason the types are not
735 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
737 // The types are convertible if they are assignable.
738 if (Type::are_assignable(lhs, rhs, reason))
741 // The types are convertible if they have identical underlying
743 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
744 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
747 // The types are convertible if they are both unnamed pointer types
748 // and their pointer base types have identical underlying types.
749 if (lhs->named_type() == NULL
750 && rhs->named_type() == NULL
751 && lhs->points_to() != NULL
752 && rhs->points_to() != NULL
753 && (lhs->points_to()->named_type() != NULL
754 || rhs->points_to()->named_type() != NULL)
755 && Type::are_identical(lhs->points_to()->base(),
756 rhs->points_to()->base(),
761 // Integer and floating point types are convertible to each other.
762 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
763 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
766 // Complex types are convertible to each other.
767 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
770 // An integer, or []byte, or []int, may be converted to a string.
771 if (lhs->is_string_type())
773 if (rhs->integer_type() != NULL)
775 if (rhs->is_slice_type())
777 const Type* e = rhs->array_type()->element_type()->forwarded();
778 if (e->integer_type() != NULL
779 && (e == Type::lookup_integer_type("uint8")
780 || e == Type::lookup_integer_type("int")))
785 // A string may be converted to []byte or []int.
786 if (rhs->is_string_type() && lhs->is_slice_type())
788 const Type* e = lhs->array_type()->element_type()->forwarded();
789 if (e->integer_type() != NULL
790 && (e == Type::lookup_integer_type("uint8")
791 || e == Type::lookup_integer_type("int")))
795 // An unsafe.Pointer type may be converted to any pointer type or to
796 // uintptr, and vice-versa.
797 if (lhs->is_unsafe_pointer_type()
798 && (rhs->points_to() != NULL
799 || (rhs->integer_type() != NULL
800 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
802 if (rhs->is_unsafe_pointer_type()
803 && (lhs->points_to() != NULL
804 || (lhs->integer_type() != NULL
805 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
808 // Give a better error message.
812 *reason = "invalid type conversion";
815 std::string s = "invalid type conversion (";
825 // Return whether this type has any hidden fields. This is only a
826 // possibility for a few types.
829 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
831 switch (this->forwarded()->classification_)
834 return this->named_type()->named_type_has_hidden_fields(reason);
836 return this->struct_type()->struct_has_hidden_fields(within, reason);
838 return this->array_type()->array_has_hidden_fields(within, reason);
844 // Return a hash code for the type to be used for method lookup.
847 Type::hash_for_method(Gogo* gogo) const
849 unsigned int ret = 0;
850 if (this->classification_ != TYPE_FORWARD)
851 ret += this->classification_;
852 return ret + this->do_hash_for_method(gogo);
855 // Default implementation of do_hash_for_method. This is appropriate
856 // for types with no subfields.
859 Type::do_hash_for_method(Gogo*) const
864 // Return a hash code for a string, given a starting hash.
867 Type::hash_string(const std::string& s, unsigned int h)
869 const char* p = s.data();
870 size_t len = s.length();
871 for (; len > 0; --len)
879 // A hash table mapping unnamed types to the backend representation of
882 Type::Type_btypes Type::type_btypes;
884 // Return a tree representing this type.
887 Type::get_backend(Gogo* gogo)
889 if (this->btype_ != NULL)
892 if (this->forward_declaration_type() != NULL
893 || this->named_type() != NULL)
894 return this->get_btype_without_hash(gogo);
896 if (this->is_error_type())
897 return gogo->backend()->error_type();
899 // To avoid confusing the backend, translate all identical Go types
900 // to the same backend representation. We use a hash table to do
901 // that. There is no need to use the hash table for named types, as
902 // named types are only identical to themselves.
904 std::pair<Type*, Btype*> val(this, NULL);
905 std::pair<Type_btypes::iterator, bool> ins =
906 Type::type_btypes.insert(val);
907 if (!ins.second && ins.first->second != NULL)
909 if (gogo != NULL && gogo->named_types_are_converted())
910 this->btype_ = ins.first->second;
911 return ins.first->second;
914 Btype* bt = this->get_btype_without_hash(gogo);
916 if (ins.first->second == NULL)
917 ins.first->second = bt;
920 // We have already created a backend representation for this
921 // type. This can happen when an unnamed type is defined using
922 // a named type which in turns uses an identical unnamed type.
923 // Use the tree we created earlier and ignore the one we just
925 bt = ins.first->second;
926 if (gogo == NULL || !gogo->named_types_are_converted())
934 // Return the backend representation for a type without looking in the
935 // hash table for identical types. This is used for named types,
936 // since a named type is never identical to any other type.
939 Type::get_btype_without_hash(Gogo* gogo)
941 if (this->btype_ == NULL)
943 Btype* bt = this->do_get_backend(gogo);
945 // For a recursive function or pointer type, we will temporarily
946 // return a circular pointer type during the recursion. We
947 // don't want to record that for a forwarding type, as it may
949 if (this->forward_declaration_type() != NULL
950 && gogo->backend()->is_circular_pointer_type(bt))
953 if (gogo == NULL || !gogo->named_types_are_converted())
961 // Return a pointer to the type descriptor for this type.
964 Type::type_descriptor_pointer(Gogo* gogo, Location location)
966 Type* t = this->forwarded();
967 if (t->type_descriptor_var_ == NULL)
969 t->make_type_descriptor_var(gogo);
970 go_assert(t->type_descriptor_var_ != NULL);
972 tree var_tree = var_to_tree(t->type_descriptor_var_);
973 if (var_tree == error_mark_node)
974 return error_mark_node;
975 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
978 // A mapping from unnamed types to type descriptor variables.
980 Type::Type_descriptor_vars Type::type_descriptor_vars;
982 // Build the type descriptor for this type.
985 Type::make_type_descriptor_var(Gogo* gogo)
987 go_assert(this->type_descriptor_var_ == NULL);
989 Named_type* nt = this->named_type();
991 // We can have multiple instances of unnamed types, but we only want
992 // to emit the type descriptor once. We use a hash table. This is
993 // not necessary for named types, as they are unique, and we store
994 // the type descriptor in the type itself.
995 Bvariable** phash = NULL;
998 Bvariable* bvnull = NULL;
999 std::pair<Type_descriptor_vars::iterator, bool> ins =
1000 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1003 // We've already build a type descriptor for this type.
1004 this->type_descriptor_var_ = ins.first->second;
1007 phash = &ins.first->second;
1010 std::string var_name = this->type_descriptor_var_name(gogo, nt);
1012 // Build the contents of the type descriptor.
1013 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1015 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1017 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1019 const Package* dummy;
1020 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1022 this->type_descriptor_var_ =
1023 gogo->backend()->immutable_struct_reference(var_name,
1027 *phash = this->type_descriptor_var_;
1031 // See if this type descriptor can appear in multiple packages.
1032 bool is_common = false;
1035 // We create the descriptor for a builtin type whenever we need
1037 is_common = nt->is_builtin();
1041 // This is an unnamed type. The descriptor could be defined in
1042 // any package where it is needed, and the linker will pick one
1043 // descriptor to keep.
1047 // We are going to build the type descriptor in this package. We
1048 // must create the variable before we convert the initializer to the
1049 // backend representation, because the initializer may refer to the
1050 // type descriptor of this type. By setting type_descriptor_var_ we
1051 // ensure that type_descriptor_pointer will work if called while
1052 // converting INITIALIZER.
1054 this->type_descriptor_var_ =
1055 gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
1058 *phash = this->type_descriptor_var_;
1060 Translate_context context(gogo, NULL, NULL, NULL);
1061 context.set_is_const();
1062 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
1064 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1065 var_name, is_common,
1066 initializer_btype, loc,
1070 // Return the name of the type descriptor variable. If NT is not
1071 // NULL, use it to get the name. Otherwise this is an unnamed type.
1074 Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
1077 return "__go_td_" + this->mangled_name(gogo);
1079 Named_object* no = nt->named_object();
1080 const Named_object* in_function = nt->in_function();
1081 std::string ret = "__go_tdn_";
1082 if (nt->is_builtin())
1083 go_assert(in_function == NULL);
1086 const std::string& unique_prefix(no->package() == NULL
1087 ? gogo->unique_prefix()
1088 : no->package()->unique_prefix());
1089 const std::string& package_name(no->package() == NULL
1090 ? gogo->package_name()
1091 : no->package()->name());
1092 ret.append(unique_prefix);
1094 ret.append(package_name);
1096 if (in_function != NULL)
1098 ret.append(Gogo::unpack_hidden_name(in_function->name()));
1102 ret.append(no->name());
1106 // Return true if this type descriptor is defined in a different
1107 // package. If this returns true it sets *PACKAGE to the package.
1110 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1111 const Package** package)
1115 if (nt->named_object()->package() != NULL)
1117 // This is a named type defined in a different package. The
1118 // type descriptor should be defined in that package.
1119 *package = nt->named_object()->package();
1125 if (this->points_to() != NULL
1126 && this->points_to()->named_type() != NULL
1127 && this->points_to()->named_type()->named_object()->package() != NULL)
1129 // This is an unnamed pointer to a named type defined in a
1130 // different package. The descriptor should be defined in
1132 *package = this->points_to()->named_type()->named_object()->package();
1139 // Return a composite literal for a type descriptor.
1142 Type::type_descriptor(Gogo* gogo, Type* type)
1144 return type->do_type_descriptor(gogo, NULL);
1147 // Return a composite literal for a type descriptor with a name.
1150 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1152 go_assert(name != NULL && type->named_type() != name);
1153 return type->do_type_descriptor(gogo, name);
1156 // Make a builtin struct type from a list of fields. The fields are
1157 // pairs of a name and a type.
1160 Type::make_builtin_struct_type(int nfields, ...)
1163 va_start(ap, nfields);
1165 Location bloc = Linemap::predeclared_location();
1166 Struct_field_list* sfl = new Struct_field_list();
1167 for (int i = 0; i < nfields; i++)
1169 const char* field_name = va_arg(ap, const char *);
1170 Type* type = va_arg(ap, Type*);
1171 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1176 return Type::make_struct_type(sfl, bloc);
1179 // A list of builtin named types.
1181 std::vector<Named_type*> Type::named_builtin_types;
1183 // Make a builtin named type.
1186 Type::make_builtin_named_type(const char* name, Type* type)
1188 Location bloc = Linemap::predeclared_location();
1189 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1190 Named_type* ret = no->type_value();
1191 Type::named_builtin_types.push_back(ret);
1195 // Convert the named builtin types.
1198 Type::convert_builtin_named_types(Gogo* gogo)
1200 for (std::vector<Named_type*>::const_iterator p =
1201 Type::named_builtin_types.begin();
1202 p != Type::named_builtin_types.end();
1205 bool r = (*p)->verify();
1207 (*p)->convert(gogo);
1211 // Return the type of a type descriptor. We should really tie this to
1212 // runtime.Type rather than copying it. This must match commonType in
1213 // libgo/go/runtime/type.go.
1216 Type::make_type_descriptor_type()
1221 Location bloc = Linemap::predeclared_location();
1223 Type* uint8_type = Type::lookup_integer_type("uint8");
1224 Type* uint32_type = Type::lookup_integer_type("uint32");
1225 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1226 Type* string_type = Type::lookup_string_type();
1227 Type* pointer_string_type = Type::make_pointer_type(string_type);
1229 // This is an unnamed version of unsafe.Pointer. Perhaps we
1230 // should use the named version instead, although that would
1231 // require us to create the unsafe package if it has not been
1232 // imported. It probably doesn't matter.
1233 Type* void_type = Type::make_void_type();
1234 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1236 // Forward declaration for the type descriptor type.
1237 Named_object* named_type_descriptor_type =
1238 Named_object::make_type_declaration("commonType", NULL, bloc);
1239 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1240 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1242 // The type of a method on a concrete type.
1243 Struct_type* method_type =
1244 Type::make_builtin_struct_type(5,
1245 "name", pointer_string_type,
1246 "pkgPath", pointer_string_type,
1247 "mtyp", pointer_type_descriptor_type,
1248 "typ", pointer_type_descriptor_type,
1249 "tfn", unsafe_pointer_type);
1250 Named_type* named_method_type =
1251 Type::make_builtin_named_type("method", method_type);
1253 // Information for types with a name or methods.
1254 Type* slice_named_method_type =
1255 Type::make_array_type(named_method_type, NULL);
1256 Struct_type* uncommon_type =
1257 Type::make_builtin_struct_type(3,
1258 "name", pointer_string_type,
1259 "pkgPath", pointer_string_type,
1260 "methods", slice_named_method_type);
1261 Named_type* named_uncommon_type =
1262 Type::make_builtin_named_type("uncommonType", uncommon_type);
1264 Type* pointer_uncommon_type =
1265 Type::make_pointer_type(named_uncommon_type);
1267 // The type descriptor type.
1269 Typed_identifier_list* params = new Typed_identifier_list();
1270 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1271 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1273 Typed_identifier_list* results = new Typed_identifier_list();
1274 results->push_back(Typed_identifier("", uintptr_type, bloc));
1276 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1278 params = new Typed_identifier_list();
1279 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1280 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1281 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1283 results = new Typed_identifier_list();
1284 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1286 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1289 Struct_type* type_descriptor_type =
1290 Type::make_builtin_struct_type(10,
1292 "align", uint8_type,
1293 "fieldAlign", uint8_type,
1294 "size", uintptr_type,
1295 "hash", uint32_type,
1296 "hashfn", hashfn_type,
1297 "equalfn", equalfn_type,
1298 "string", pointer_string_type,
1299 "", pointer_uncommon_type,
1301 pointer_type_descriptor_type);
1303 Named_type* named = Type::make_builtin_named_type("commonType",
1304 type_descriptor_type);
1306 named_type_descriptor_type->set_type_value(named);
1314 // Make the type of a pointer to a type descriptor as represented in
1318 Type::make_type_descriptor_ptr_type()
1322 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1326 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1327 // hash code for this type and which compare whether two values of
1328 // this type are equal. If NAME is not NULL it is the name of this
1329 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1330 // functions, for convenience; they may be NULL.
1333 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1334 Function_type* equal_fntype, Named_object** hash_fn,
1335 Named_object** equal_fn)
1337 if (hash_fntype == NULL || equal_fntype == NULL)
1339 Location bloc = Linemap::predeclared_location();
1341 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1342 Type* void_type = Type::make_void_type();
1343 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1345 if (hash_fntype == NULL)
1347 Typed_identifier_list* params = new Typed_identifier_list();
1348 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1350 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1352 Typed_identifier_list* results = new Typed_identifier_list();
1353 results->push_back(Typed_identifier("", uintptr_type, bloc));
1355 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1357 if (equal_fntype == NULL)
1359 Typed_identifier_list* params = new Typed_identifier_list();
1360 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1362 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1364 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1366 Typed_identifier_list* results = new Typed_identifier_list();
1367 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1370 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1374 const char* hash_fnname;
1375 const char* equal_fnname;
1376 if (this->compare_is_identity(gogo))
1378 hash_fnname = "__go_type_hash_identity";
1379 equal_fnname = "__go_type_equal_identity";
1381 else if (!this->is_comparable())
1383 hash_fnname = "__go_type_hash_error";
1384 equal_fnname = "__go_type_equal_error";
1388 switch (this->base()->classification())
1390 case Type::TYPE_ERROR:
1391 case Type::TYPE_VOID:
1392 case Type::TYPE_NIL:
1393 case Type::TYPE_FUNCTION:
1394 case Type::TYPE_MAP:
1395 // For these types is_comparable should have returned false.
1398 case Type::TYPE_BOOLEAN:
1399 case Type::TYPE_INTEGER:
1400 case Type::TYPE_POINTER:
1401 case Type::TYPE_CHANNEL:
1402 // For these types compare_is_identity should have returned true.
1405 case Type::TYPE_FLOAT:
1406 hash_fnname = "__go_type_hash_float";
1407 equal_fnname = "__go_type_equal_float";
1410 case Type::TYPE_COMPLEX:
1411 hash_fnname = "__go_type_hash_complex";
1412 equal_fnname = "__go_type_equal_complex";
1415 case Type::TYPE_STRING:
1416 hash_fnname = "__go_type_hash_string";
1417 equal_fnname = "__go_type_equal_string";
1420 case Type::TYPE_STRUCT:
1422 // This is a struct which can not be compared using a
1423 // simple identity function. We need to build a function
1425 this->specific_type_functions(gogo, name, hash_fntype,
1426 equal_fntype, hash_fn, equal_fn);
1430 case Type::TYPE_ARRAY:
1431 if (this->is_slice_type())
1433 // Type::is_compatible_for_comparison should have
1439 // This is an array which can not be compared using a
1440 // simple identity function. We need to build a
1441 // function for comparison.
1442 this->specific_type_functions(gogo, name, hash_fntype,
1443 equal_fntype, hash_fn, equal_fn);
1448 case Type::TYPE_INTERFACE:
1449 if (this->interface_type()->is_empty())
1451 hash_fnname = "__go_type_hash_empty_interface";
1452 equal_fnname = "__go_type_equal_empty_interface";
1456 hash_fnname = "__go_type_hash_interface";
1457 equal_fnname = "__go_type_equal_interface";
1461 case Type::TYPE_NAMED:
1462 case Type::TYPE_FORWARD:
1471 Location bloc = Linemap::predeclared_location();
1472 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1474 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1475 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1476 equal_fntype, bloc);
1477 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1480 // A hash table mapping types to the specific hash functions.
1482 Type::Type_functions Type::type_functions_table;
1484 // Handle a type function which is specific to a type: a struct or
1485 // array which can not use an identity comparison.
1488 Type::specific_type_functions(Gogo* gogo, Named_type* name,
1489 Function_type* hash_fntype,
1490 Function_type* equal_fntype,
1491 Named_object** hash_fn,
1492 Named_object** equal_fn)
1494 Hash_equal_fn fnull(NULL, NULL);
1495 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1496 std::pair<Type_functions::iterator, bool> ins =
1497 Type::type_functions_table.insert(val);
1500 // We already have functions for this type
1501 *hash_fn = ins.first->second.first;
1502 *equal_fn = ins.first->second.second;
1506 std::string base_name;
1509 // Mangled names can have '.' if they happen to refer to named
1510 // types in some way. That's fine if this is simply a named
1511 // type, but otherwise it will confuse the code that builds
1512 // function identifiers. Remove '.' when necessary.
1513 base_name = this->mangled_name(gogo);
1515 while ((i = base_name.find('.')) != std::string::npos)
1517 base_name = gogo->pack_hidden_name(base_name, false);
1521 // This name is already hidden or not as appropriate.
1522 base_name = name->name();
1523 const Named_object* in_function = name->in_function();
1524 if (in_function != NULL)
1525 base_name += '$' + in_function->name();
1527 std::string hash_name = base_name + "$hash";
1528 std::string equal_name = base_name + "$equal";
1530 Location bloc = Linemap::predeclared_location();
1532 const Package* package = NULL;
1533 bool is_defined_elsewhere =
1534 this->type_descriptor_defined_elsewhere(name, &package);
1535 if (is_defined_elsewhere)
1537 *hash_fn = Named_object::make_function_declaration(hash_name, package,
1539 *equal_fn = Named_object::make_function_declaration(equal_name, package,
1540 equal_fntype, bloc);
1544 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1545 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1549 ins.first->second.first = *hash_fn;
1550 ins.first->second.second = *equal_fn;
1552 if (!is_defined_elsewhere)
1554 if (gogo->in_global_scope())
1555 this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1556 equal_name, equal_fntype);
1558 gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1559 equal_name, equal_fntype);
1563 // Write the hash and equality functions for a type which needs to be
1564 // written specially.
1567 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1568 const std::string& hash_name,
1569 Function_type* hash_fntype,
1570 const std::string& equal_name,
1571 Function_type* equal_fntype)
1573 Location bloc = Linemap::predeclared_location();
1575 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1577 gogo->start_block(bloc);
1579 if (this->struct_type() != NULL)
1580 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1582 else if (this->array_type() != NULL)
1583 this->array_type()->write_hash_function(gogo, name, hash_fntype,
1588 Block* b = gogo->finish_block(bloc);
1589 gogo->add_block(b, bloc);
1590 gogo->lower_block(hash_fn, b);
1591 gogo->finish_function(bloc);
1593 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1595 gogo->start_block(bloc);
1597 if (this->struct_type() != NULL)
1598 this->struct_type()->write_equal_function(gogo, name);
1599 else if (this->array_type() != NULL)
1600 this->array_type()->write_equal_function(gogo, name);
1604 b = gogo->finish_block(bloc);
1605 gogo->add_block(b, bloc);
1606 gogo->lower_block(equal_fn, b);
1607 gogo->finish_function(bloc);
1610 // Return a composite literal for the type descriptor for a plain type
1611 // of kind RUNTIME_TYPE_KIND named NAME.
1614 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1615 Named_type* name, const Methods* methods,
1616 bool only_value_methods)
1618 Location bloc = Linemap::predeclared_location();
1620 Type* td_type = Type::make_type_descriptor_type();
1621 const Struct_field_list* fields = td_type->struct_type()->fields();
1623 Expression_list* vals = new Expression_list();
1626 if (!this->has_pointer())
1627 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1628 Struct_field_list::const_iterator p = fields->begin();
1629 go_assert(p->is_field_name("Kind"));
1631 mpz_init_set_ui(iv, runtime_type_kind);
1632 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1635 go_assert(p->is_field_name("align"));
1636 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1637 vals->push_back(Expression::make_type_info(this, type_info));
1640 go_assert(p->is_field_name("fieldAlign"));
1641 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1642 vals->push_back(Expression::make_type_info(this, type_info));
1645 go_assert(p->is_field_name("size"));
1646 type_info = Expression::TYPE_INFO_SIZE;
1647 vals->push_back(Expression::make_type_info(this, type_info));
1650 go_assert(p->is_field_name("hash"));
1651 mpz_set_ui(iv, this->hash_for_method(gogo));
1652 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1655 go_assert(p->is_field_name("hashfn"));
1656 Function_type* hash_fntype = p->type()->function_type();
1659 go_assert(p->is_field_name("equalfn"));
1660 Function_type* equal_fntype = p->type()->function_type();
1662 Named_object* hash_fn;
1663 Named_object* equal_fn;
1664 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1666 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1667 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1670 go_assert(p->is_field_name("string"));
1671 Expression* s = Expression::make_string((name != NULL
1672 ? name->reflection(gogo)
1673 : this->reflection(gogo)),
1675 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1678 go_assert(p->is_field_name("uncommonType"));
1679 if (name == NULL && methods == NULL)
1680 vals->push_back(Expression::make_nil(bloc));
1683 if (methods == NULL)
1684 methods = name->methods();
1685 vals->push_back(this->uncommon_type_constructor(gogo,
1688 only_value_methods));
1692 go_assert(p->is_field_name("ptrToThis"));
1694 vals->push_back(Expression::make_nil(bloc));
1697 Type* pt = Type::make_pointer_type(name);
1698 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1702 go_assert(p == fields->end());
1706 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1709 // Return a composite literal for the uncommon type information for
1710 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1711 // struct. If name is not NULL, it is the name of the type. If
1712 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1713 // is true if only value methods should be included. At least one of
1714 // NAME and METHODS must not be NULL.
1717 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1718 Named_type* name, const Methods* methods,
1719 bool only_value_methods) const
1721 Location bloc = Linemap::predeclared_location();
1723 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1725 Expression_list* vals = new Expression_list();
1728 Struct_field_list::const_iterator p = fields->begin();
1729 go_assert(p->is_field_name("name"));
1732 go_assert(p->is_field_name("pkgPath"));
1736 vals->push_back(Expression::make_nil(bloc));
1737 vals->push_back(Expression::make_nil(bloc));
1741 Named_object* no = name->named_object();
1742 std::string n = Gogo::unpack_hidden_name(no->name());
1743 Expression* s = Expression::make_string(n, bloc);
1744 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1746 if (name->is_builtin())
1747 vals->push_back(Expression::make_nil(bloc));
1750 const Package* package = no->package();
1751 const std::string& unique_prefix(package == NULL
1752 ? gogo->unique_prefix()
1753 : package->unique_prefix());
1754 const std::string& package_name(package == NULL
1755 ? gogo->package_name()
1757 n.assign(unique_prefix);
1759 n.append(package_name);
1760 if (name->in_function() != NULL)
1763 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1765 s = Expression::make_string(n, bloc);
1766 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1771 go_assert(p->is_field_name("methods"));
1772 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1773 only_value_methods));
1776 go_assert(p == fields->end());
1778 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1780 return Expression::make_unary(OPERATOR_AND, r, bloc);
1783 // Sort methods by name.
1789 operator()(const std::pair<std::string, const Method*>& m1,
1790 const std::pair<std::string, const Method*>& m2) const
1791 { return m1.first < m2.first; }
1794 // Return a composite literal for the type method table for this type.
1795 // METHODS_TYPE is the type of the table, and is a slice type.
1796 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1797 // then only value methods are used.
1800 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1801 const Methods* methods,
1802 bool only_value_methods) const
1804 Location bloc = Linemap::predeclared_location();
1806 std::vector<std::pair<std::string, const Method*> > smethods;
1807 if (methods != NULL)
1809 smethods.reserve(methods->count());
1810 for (Methods::const_iterator p = methods->begin();
1811 p != methods->end();
1814 if (p->second->is_ambiguous())
1816 if (only_value_methods && !p->second->is_value_method())
1818 smethods.push_back(std::make_pair(p->first, p->second));
1822 if (smethods.empty())
1823 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1825 std::sort(smethods.begin(), smethods.end(), Sort_methods());
1827 Type* method_type = methods_type->array_type()->element_type();
1829 Expression_list* vals = new Expression_list();
1830 vals->reserve(smethods.size());
1831 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1833 p != smethods.end();
1835 vals->push_back(this->method_constructor(gogo, method_type, p->first,
1836 p->second, only_value_methods));
1838 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1841 // Return a composite literal for a single method. METHOD_TYPE is the
1842 // type of the entry. METHOD_NAME is the name of the method and M is
1843 // the method information.
1846 Type::method_constructor(Gogo*, Type* method_type,
1847 const std::string& method_name,
1849 bool only_value_methods) const
1851 Location bloc = Linemap::predeclared_location();
1853 const Struct_field_list* fields = method_type->struct_type()->fields();
1855 Expression_list* vals = new Expression_list();
1858 Struct_field_list::const_iterator p = fields->begin();
1859 go_assert(p->is_field_name("name"));
1860 const std::string n = Gogo::unpack_hidden_name(method_name);
1861 Expression* s = Expression::make_string(n, bloc);
1862 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1865 go_assert(p->is_field_name("pkgPath"));
1866 if (!Gogo::is_hidden_name(method_name))
1867 vals->push_back(Expression::make_nil(bloc));
1870 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1871 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1874 Named_object* no = (m->needs_stub_method()
1876 : m->named_object());
1878 Function_type* mtype;
1879 if (no->is_function())
1880 mtype = no->func_value()->type();
1882 mtype = no->func_declaration_value()->type();
1883 go_assert(mtype->is_method());
1884 Type* nonmethod_type = mtype->copy_without_receiver();
1887 go_assert(p->is_field_name("mtyp"));
1888 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1891 go_assert(p->is_field_name("typ"));
1892 if (!only_value_methods && m->is_value_method())
1894 // This is a value method on a pointer type. Change the type of
1895 // the method to use a pointer receiver. The implementation
1896 // always uses a pointer receiver anyhow.
1897 Type* rtype = mtype->receiver()->type();
1898 Type* prtype = Type::make_pointer_type(rtype);
1899 Typed_identifier* receiver =
1900 new Typed_identifier(mtype->receiver()->name(), prtype,
1901 mtype->receiver()->location());
1902 mtype = Type::make_function_type(receiver,
1903 (mtype->parameters() == NULL
1905 : mtype->parameters()->copy()),
1906 (mtype->results() == NULL
1908 : mtype->results()->copy()),
1911 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1914 go_assert(p->is_field_name("tfn"));
1915 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1918 go_assert(p == fields->end());
1920 return Expression::make_struct_composite_literal(method_type, vals, bloc);
1923 // Return a composite literal for the type descriptor of a plain type.
1924 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1925 // NULL, it is the name to use as well as the list of methods.
1928 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1931 return this->type_descriptor_constructor(gogo, runtime_type_kind,
1935 // Return the type reflection string for this type.
1938 Type::reflection(Gogo* gogo) const
1942 // The do_reflection virtual function should set RET to the
1943 // reflection string.
1944 this->do_reflection(gogo, &ret);
1949 // Return a mangled name for the type.
1952 Type::mangled_name(Gogo* gogo) const
1956 // The do_mangled_name virtual function should set RET to the
1957 // mangled name. For a composite type it should append a code for
1958 // the composition and then call do_mangled_name on the components.
1959 this->do_mangled_name(gogo, &ret);
1964 // Return whether the backend size of the type is known.
1967 Type::is_backend_type_size_known(Gogo* gogo)
1969 switch (this->classification_)
1983 case TYPE_INTERFACE:
1988 const Struct_field_list* fields = this->struct_type()->fields();
1989 for (Struct_field_list::const_iterator pf = fields->begin();
1990 pf != fields->end();
1992 if (!pf->type()->is_backend_type_size_known(gogo))
1999 const Array_type* at = this->array_type();
2000 if (at->length() == NULL)
2007 bool length_known = at->length()->integer_constant_value(true,
2013 return at->element_type()->is_backend_type_size_known(gogo);
2018 // Begin converting this type to the backend representation.
2019 // This will create a placeholder if necessary.
2020 this->get_backend(gogo);
2021 return this->named_type()->is_named_backend_type_size_known();
2025 Forward_declaration_type* fdt = this->forward_declaration_type();
2026 return fdt->real_type()->is_backend_type_size_known(gogo);
2030 case TYPE_CALL_MULTIPLE_RESULT:
2038 // If the size of the type can be determined, set *PSIZE to the size
2039 // in bytes and return true. Otherwise, return false. This queries
2043 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2045 if (!this->is_backend_type_size_known(gogo))
2047 size_t size = gogo->backend()->type_size(this->get_backend(gogo));
2048 *psize = static_cast<unsigned int>(size);
2054 // If the alignment of the type can be determined, set *PALIGN to
2055 // the alignment in bytes and return true. Otherwise, return false.
2058 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2060 if (!this->is_backend_type_size_known(gogo))
2062 size_t align = gogo->backend()->type_alignment(this->get_backend(gogo));
2063 *palign = static_cast<unsigned int>(align);
2064 if (*palign != align)
2069 // Like backend_type_align, but return the alignment when used as a
2073 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2075 if (!this->is_backend_type_size_known(gogo))
2077 size_t a = gogo->backend()->type_field_alignment(this->get_backend(gogo));
2078 *palign = static_cast<unsigned int>(a);
2084 // Default function to export a type.
2087 Type::do_export(Export*) const
2095 Type::import_type(Import* imp)
2097 if (imp->match_c_string("("))
2098 return Function_type::do_import(imp);
2099 else if (imp->match_c_string("*"))
2100 return Pointer_type::do_import(imp);
2101 else if (imp->match_c_string("struct "))
2102 return Struct_type::do_import(imp);
2103 else if (imp->match_c_string("["))
2104 return Array_type::do_import(imp);
2105 else if (imp->match_c_string("map "))
2106 return Map_type::do_import(imp);
2107 else if (imp->match_c_string("chan "))
2108 return Channel_type::do_import(imp);
2109 else if (imp->match_c_string("interface"))
2110 return Interface_type::do_import(imp);
2113 error_at(imp->location(), "import error: expected type");
2114 return Type::make_error_type();
2118 // A type used to indicate a parsing error. This exists to simplify
2119 // later error detection.
2121 class Error_type : public Type
2130 do_compare_is_identity(Gogo*) const
2134 do_get_backend(Gogo* gogo)
2135 { return gogo->backend()->error_type(); }
2138 do_type_descriptor(Gogo*, Named_type*)
2139 { return Expression::make_error(Linemap::predeclared_location()); }
2142 do_reflection(Gogo*, std::string*) const
2143 { go_assert(saw_errors()); }
2146 do_mangled_name(Gogo*, std::string* ret) const
2147 { ret->push_back('E'); }
2151 Type::make_error_type()
2153 static Error_type singleton_error_type;
2154 return &singleton_error_type;
2159 class Void_type : public Type
2168 do_compare_is_identity(Gogo*) const
2172 do_get_backend(Gogo* gogo)
2173 { return gogo->backend()->void_type(); }
2176 do_type_descriptor(Gogo*, Named_type*)
2177 { go_unreachable(); }
2180 do_reflection(Gogo*, std::string*) const
2184 do_mangled_name(Gogo*, std::string* ret) const
2185 { ret->push_back('v'); }
2189 Type::make_void_type()
2191 static Void_type singleton_void_type;
2192 return &singleton_void_type;
2195 // The boolean type.
2197 class Boolean_type : public Type
2201 : Type(TYPE_BOOLEAN)
2206 do_compare_is_identity(Gogo*) const
2210 do_get_backend(Gogo* gogo)
2211 { return gogo->backend()->bool_type(); }
2214 do_type_descriptor(Gogo*, Named_type* name);
2216 // We should not be asked for the reflection string of a basic type.
2218 do_reflection(Gogo*, std::string* ret) const
2219 { ret->append("bool"); }
2222 do_mangled_name(Gogo*, std::string* ret) const
2223 { ret->push_back('b'); }
2226 // Make the type descriptor.
2229 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2232 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2235 Named_object* no = gogo->lookup_global("bool");
2236 go_assert(no != NULL);
2237 return Type::type_descriptor(gogo, no->type_value());
2242 Type::make_boolean_type()
2244 static Boolean_type boolean_type;
2245 return &boolean_type;
2248 // The named type "bool".
2250 static Named_type* named_bool_type;
2252 // Get the named type "bool".
2255 Type::lookup_bool_type()
2257 return named_bool_type;
2260 // Make the named type "bool".
2263 Type::make_named_bool_type()
2265 Type* bool_type = Type::make_boolean_type();
2266 Named_object* named_object =
2267 Named_object::make_type("bool", NULL, bool_type,
2268 Linemap::predeclared_location());
2269 Named_type* named_type = named_object->type_value();
2270 named_bool_type = named_type;
2274 // Class Integer_type.
2276 Integer_type::Named_integer_types Integer_type::named_integer_types;
2278 // Create a new integer type. Non-abstract integer types always have
2282 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2283 int bits, int runtime_type_kind)
2285 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2287 std::string sname(name);
2288 Named_object* named_object =
2289 Named_object::make_type(sname, NULL, integer_type,
2290 Linemap::predeclared_location());
2291 Named_type* named_type = named_object->type_value();
2292 std::pair<Named_integer_types::iterator, bool> ins =
2293 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2294 go_assert(ins.second);
2298 // Look up an existing integer type.
2301 Integer_type::lookup_integer_type(const char* name)
2303 Named_integer_types::const_iterator p =
2304 Integer_type::named_integer_types.find(name);
2305 go_assert(p != Integer_type::named_integer_types.end());
2309 // Create a new abstract integer type.
2312 Integer_type::create_abstract_integer_type()
2314 static Integer_type* abstract_type;
2315 if (abstract_type == NULL)
2316 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2317 RUNTIME_TYPE_KIND_INT);
2318 return abstract_type;
2321 // Integer type compatibility.
2324 Integer_type::is_identical(const Integer_type* t) const
2326 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2328 return this->is_abstract_ == t->is_abstract_;
2334 Integer_type::do_hash_for_method(Gogo*) const
2336 return ((this->bits_ << 4)
2337 + ((this->is_unsigned_ ? 1 : 0) << 8)
2338 + ((this->is_abstract_ ? 1 : 0) << 9));
2341 // Convert an Integer_type to the backend representation.
2344 Integer_type::do_get_backend(Gogo* gogo)
2346 if (this->is_abstract_)
2348 go_assert(saw_errors());
2349 return gogo->backend()->error_type();
2351 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2354 // The type descriptor for an integer type. Integer types are always
2358 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2360 go_assert(name != NULL);
2361 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2364 // We should not be asked for the reflection string of a basic type.
2367 Integer_type::do_reflection(Gogo*, std::string*) const
2369 go_assert(saw_errors());
2375 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2378 snprintf(buf, sizeof buf, "i%s%s%de",
2379 this->is_abstract_ ? "a" : "",
2380 this->is_unsigned_ ? "u" : "",
2385 // Make an integer type.
2388 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2389 int runtime_type_kind)
2391 return Integer_type::create_integer_type(name, is_unsigned, bits,
2395 // Make an abstract integer type.
2398 Type::make_abstract_integer_type()
2400 return Integer_type::create_abstract_integer_type();
2403 // Look up an integer type.
2406 Type::lookup_integer_type(const char* name)
2408 return Integer_type::lookup_integer_type(name);
2411 // Class Float_type.
2413 Float_type::Named_float_types Float_type::named_float_types;
2415 // Create a new float type. Non-abstract float types always have
2419 Float_type::create_float_type(const char* name, int bits,
2420 int runtime_type_kind)
2422 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2423 std::string sname(name);
2424 Named_object* named_object =
2425 Named_object::make_type(sname, NULL, float_type,
2426 Linemap::predeclared_location());
2427 Named_type* named_type = named_object->type_value();
2428 std::pair<Named_float_types::iterator, bool> ins =
2429 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2430 go_assert(ins.second);
2434 // Look up an existing float type.
2437 Float_type::lookup_float_type(const char* name)
2439 Named_float_types::const_iterator p =
2440 Float_type::named_float_types.find(name);
2441 go_assert(p != Float_type::named_float_types.end());
2445 // Create a new abstract float type.
2448 Float_type::create_abstract_float_type()
2450 static Float_type* abstract_type;
2451 if (abstract_type == NULL)
2452 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2453 return abstract_type;
2456 // Whether this type is identical with T.
2459 Float_type::is_identical(const Float_type* t) const
2461 if (this->bits_ != t->bits_)
2463 return this->is_abstract_ == t->is_abstract_;
2469 Float_type::do_hash_for_method(Gogo*) const
2471 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2474 // Convert to the backend representation.
2477 Float_type::do_get_backend(Gogo* gogo)
2479 return gogo->backend()->float_type(this->bits_);
2482 // The type descriptor for a float type. Float types are always named.
2485 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2487 go_assert(name != NULL);
2488 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2491 // We should not be asked for the reflection string of a basic type.
2494 Float_type::do_reflection(Gogo*, std::string*) const
2496 go_assert(saw_errors());
2502 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2505 snprintf(buf, sizeof buf, "f%s%de",
2506 this->is_abstract_ ? "a" : "",
2511 // Make a floating point type.
2514 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2516 return Float_type::create_float_type(name, bits, runtime_type_kind);
2519 // Make an abstract float type.
2522 Type::make_abstract_float_type()
2524 return Float_type::create_abstract_float_type();
2527 // Look up a float type.
2530 Type::lookup_float_type(const char* name)
2532 return Float_type::lookup_float_type(name);
2535 // Class Complex_type.
2537 Complex_type::Named_complex_types Complex_type::named_complex_types;
2539 // Create a new complex type. Non-abstract complex types always have
2543 Complex_type::create_complex_type(const char* name, int bits,
2544 int runtime_type_kind)
2546 Complex_type* complex_type = new Complex_type(false, bits,
2548 std::string sname(name);
2549 Named_object* named_object =
2550 Named_object::make_type(sname, NULL, complex_type,
2551 Linemap::predeclared_location());
2552 Named_type* named_type = named_object->type_value();
2553 std::pair<Named_complex_types::iterator, bool> ins =
2554 Complex_type::named_complex_types.insert(std::make_pair(sname,
2556 go_assert(ins.second);
2560 // Look up an existing complex type.
2563 Complex_type::lookup_complex_type(const char* name)
2565 Named_complex_types::const_iterator p =
2566 Complex_type::named_complex_types.find(name);
2567 go_assert(p != Complex_type::named_complex_types.end());
2571 // Create a new abstract complex type.
2574 Complex_type::create_abstract_complex_type()
2576 static Complex_type* abstract_type;
2577 if (abstract_type == NULL)
2578 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2579 return abstract_type;
2582 // Whether this type is identical with T.
2585 Complex_type::is_identical(const Complex_type *t) const
2587 if (this->bits_ != t->bits_)
2589 return this->is_abstract_ == t->is_abstract_;
2595 Complex_type::do_hash_for_method(Gogo*) const
2597 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2600 // Convert to the backend representation.
2603 Complex_type::do_get_backend(Gogo* gogo)
2605 return gogo->backend()->complex_type(this->bits_);
2608 // The type descriptor for a complex type. Complex types are always
2612 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2614 go_assert(name != NULL);
2615 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2618 // We should not be asked for the reflection string of a basic type.
2621 Complex_type::do_reflection(Gogo*, std::string*) const
2623 go_assert(saw_errors());
2629 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2632 snprintf(buf, sizeof buf, "c%s%de",
2633 this->is_abstract_ ? "a" : "",
2638 // Make a complex type.
2641 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2643 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2646 // Make an abstract complex type.
2649 Type::make_abstract_complex_type()
2651 return Complex_type::create_abstract_complex_type();
2654 // Look up a complex type.
2657 Type::lookup_complex_type(const char* name)
2659 return Complex_type::lookup_complex_type(name);
2662 // Class String_type.
2664 // Convert String_type to the backend representation. A string is a
2665 // struct with two fields: a pointer to the characters and a length.
2668 String_type::do_get_backend(Gogo* gogo)
2670 static Btype* backend_string_type;
2671 if (backend_string_type == NULL)
2673 std::vector<Backend::Btyped_identifier> fields(2);
2675 Type* b = gogo->lookup_global("byte")->type_value();
2676 Type* pb = Type::make_pointer_type(b);
2677 fields[0].name = "__data";
2678 fields[0].btype = pb->get_backend(gogo);
2679 fields[0].location = Linemap::predeclared_location();
2681 Type* int_type = Type::lookup_integer_type("int");
2682 fields[1].name = "__length";
2683 fields[1].btype = int_type->get_backend(gogo);
2684 fields[1].location = fields[0].location;
2686 backend_string_type = gogo->backend()->struct_type(fields);
2688 return backend_string_type;
2691 // Return a tree for the length of STRING.
2694 String_type::length_tree(Gogo*, tree string)
2696 tree string_type = TREE_TYPE(string);
2697 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2698 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2699 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2701 return fold_build3(COMPONENT_REF, integer_type_node, string,
2702 length_field, NULL_TREE);
2705 // Return a tree for a pointer to the bytes of STRING.
2708 String_type::bytes_tree(Gogo*, tree string)
2710 tree string_type = TREE_TYPE(string);
2711 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2712 tree bytes_field = TYPE_FIELDS(string_type);
2713 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2715 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2716 bytes_field, NULL_TREE);
2719 // The type descriptor for the string type.
2722 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2725 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2728 Named_object* no = gogo->lookup_global("string");
2729 go_assert(no != NULL);
2730 return Type::type_descriptor(gogo, no->type_value());
2734 // We should not be asked for the reflection string of a basic type.
2737 String_type::do_reflection(Gogo*, std::string* ret) const
2739 ret->append("string");
2742 // Mangled name of a string type.
2745 String_type::do_mangled_name(Gogo*, std::string* ret) const
2747 ret->push_back('z');
2750 // Make a string type.
2753 Type::make_string_type()
2755 static String_type string_type;
2756 return &string_type;
2759 // The named type "string".
2761 static Named_type* named_string_type;
2763 // Get the named type "string".
2766 Type::lookup_string_type()
2768 return named_string_type;
2771 // Make the named type string.
2774 Type::make_named_string_type()
2776 Type* string_type = Type::make_string_type();
2777 Named_object* named_object =
2778 Named_object::make_type("string", NULL, string_type,
2779 Linemap::predeclared_location());
2780 Named_type* named_type = named_object->type_value();
2781 named_string_type = named_type;
2785 // The sink type. This is the type of the blank identifier _. Any
2786 // type may be assigned to it.
2788 class Sink_type : public Type
2797 do_compare_is_identity(Gogo*) const
2801 do_get_backend(Gogo*)
2802 { go_unreachable(); }
2805 do_type_descriptor(Gogo*, Named_type*)
2806 { go_unreachable(); }
2809 do_reflection(Gogo*, std::string*) const
2810 { go_unreachable(); }
2813 do_mangled_name(Gogo*, std::string*) const
2814 { go_unreachable(); }
2817 // Make the sink type.
2820 Type::make_sink_type()
2822 static Sink_type sink_type;
2826 // Class Function_type.
2831 Function_type::do_traverse(Traverse* traverse)
2833 if (this->receiver_ != NULL
2834 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2835 return TRAVERSE_EXIT;
2836 if (this->parameters_ != NULL
2837 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2838 return TRAVERSE_EXIT;
2839 if (this->results_ != NULL
2840 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2841 return TRAVERSE_EXIT;
2842 return TRAVERSE_CONTINUE;
2845 // Returns whether T is a valid redeclaration of this type. If this
2846 // returns false, and REASON is not NULL, *REASON may be set to a
2847 // brief explanation of why it returned false.
2850 Function_type::is_valid_redeclaration(const Function_type* t,
2851 std::string* reason) const
2853 if (!this->is_identical(t, false, true, reason))
2856 // A redeclaration of a function is required to use the same names
2857 // for the receiver and parameters.
2858 if (this->receiver() != NULL
2859 && this->receiver()->name() != t->receiver()->name()
2860 && this->receiver()->name() != Import::import_marker
2861 && t->receiver()->name() != Import::import_marker)
2864 *reason = "receiver name changed";
2868 const Typed_identifier_list* parms1 = this->parameters();
2869 const Typed_identifier_list* parms2 = t->parameters();
2872 Typed_identifier_list::const_iterator p1 = parms1->begin();
2873 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2874 p2 != parms2->end();
2877 if (p1->name() != p2->name()
2878 && p1->name() != Import::import_marker
2879 && p2->name() != Import::import_marker)
2882 *reason = "parameter name changed";
2886 // This is called at parse time, so we may have unknown
2888 Type* t1 = p1->type()->forwarded();
2889 Type* t2 = p2->type()->forwarded();
2891 && t1->forward_declaration_type() != NULL
2892 && (t2->forward_declaration_type() == NULL
2893 || (t1->forward_declaration_type()->named_object()
2894 != t2->forward_declaration_type()->named_object())))
2899 const Typed_identifier_list* results1 = this->results();
2900 const Typed_identifier_list* results2 = t->results();
2901 if (results1 != NULL)
2903 Typed_identifier_list::const_iterator res1 = results1->begin();
2904 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2905 res2 != results2->end();
2908 if (res1->name() != res2->name()
2909 && res1->name() != Import::import_marker
2910 && res2->name() != Import::import_marker)
2913 *reason = "result name changed";
2917 // This is called at parse time, so we may have unknown
2919 Type* t1 = res1->type()->forwarded();
2920 Type* t2 = res2->type()->forwarded();
2922 && t1->forward_declaration_type() != NULL
2923 && (t2->forward_declaration_type() == NULL
2924 || (t1->forward_declaration_type()->named_object()
2925 != t2->forward_declaration_type()->named_object())))
2933 // Check whether T is the same as this type.
2936 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2937 bool errors_are_identical,
2938 std::string* reason) const
2940 if (!ignore_receiver)
2942 const Typed_identifier* r1 = this->receiver();
2943 const Typed_identifier* r2 = t->receiver();
2944 if ((r1 != NULL) != (r2 != NULL))
2947 *reason = _("different receiver types");
2952 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2955 if (reason != NULL && !reason->empty())
2956 *reason = "receiver: " + *reason;
2962 const Typed_identifier_list* parms1 = this->parameters();
2963 const Typed_identifier_list* parms2 = t->parameters();
2964 if ((parms1 != NULL) != (parms2 != NULL))
2967 *reason = _("different number of parameters");
2972 Typed_identifier_list::const_iterator p1 = parms1->begin();
2973 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2974 p2 != parms2->end();
2977 if (p1 == parms1->end())
2980 *reason = _("different number of parameters");
2984 if (!Type::are_identical(p1->type(), p2->type(),
2985 errors_are_identical, NULL))
2988 *reason = _("different parameter types");
2992 if (p1 != parms1->end())
2995 *reason = _("different number of parameters");
3000 if (this->is_varargs() != t->is_varargs())
3003 *reason = _("different varargs");
3007 const Typed_identifier_list* results1 = this->results();
3008 const Typed_identifier_list* results2 = t->results();
3009 if ((results1 != NULL) != (results2 != NULL))
3012 *reason = _("different number of results");
3015 if (results1 != NULL)
3017 Typed_identifier_list::const_iterator res1 = results1->begin();
3018 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3019 res2 != results2->end();
3022 if (res1 == results1->end())
3025 *reason = _("different number of results");
3029 if (!Type::are_identical(res1->type(), res2->type(),
3030 errors_are_identical, NULL))
3033 *reason = _("different result types");
3037 if (res1 != results1->end())
3040 *reason = _("different number of results");
3051 Function_type::do_hash_for_method(Gogo* gogo) const
3053 unsigned int ret = 0;
3054 // We ignore the receiver type for hash codes, because we need to
3055 // get the same hash code for a method in an interface and a method
3056 // declared for a type. The former will not have a receiver.
3057 if (this->parameters_ != NULL)
3060 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3061 p != this->parameters_->end();
3063 ret += p->type()->hash_for_method(gogo) << shift;
3065 if (this->results_ != NULL)
3068 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3069 p != this->results_->end();
3071 ret += p->type()->hash_for_method(gogo) << shift;
3073 if (this->is_varargs_)
3079 // Get the backend representation for a function type.
3082 Function_type::get_function_backend(Gogo* gogo)
3084 Backend::Btyped_identifier breceiver;
3085 if (this->receiver_ != NULL)
3087 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3089 // We always pass the address of the receiver parameter, in
3090 // order to make interface calls work with unknown types.
3091 Type* rtype = this->receiver_->type();
3092 if (rtype->points_to() == NULL)
3093 rtype = Type::make_pointer_type(rtype);
3094 breceiver.btype = rtype->get_backend(gogo);
3095 breceiver.location = this->receiver_->location();
3098 std::vector<Backend::Btyped_identifier> bparameters;
3099 if (this->parameters_ != NULL)
3101 bparameters.resize(this->parameters_->size());
3103 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3104 p != this->parameters_->end();
3107 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3108 bparameters[i].btype = p->type()->get_backend(gogo);
3109 bparameters[i].location = p->location();
3111 go_assert(i == bparameters.size());
3114 std::vector<Backend::Btyped_identifier> bresults;
3115 if (this->results_ != NULL)
3117 bresults.resize(this->results_->size());
3119 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3120 p != this->results_->end();
3123 bresults[i].name = Gogo::unpack_hidden_name(p->name());
3124 bresults[i].btype = p->type()->get_backend(gogo);
3125 bresults[i].location = p->location();
3127 go_assert(i == bresults.size());
3130 return gogo->backend()->function_type(breceiver, bparameters, bresults,
3134 // A hash table mapping function types to their backend placeholders.
3136 Function_type::Placeholders Function_type::placeholders;
3138 // Get the backend representation for a function type. If we are
3139 // still converting types, and this types has multiple results, return
3140 // a placeholder instead. We do this because for multiple results we
3141 // build a struct, and we need to make sure that all the types in the
3142 // struct are valid before we create the struct.
3145 Function_type::do_get_backend(Gogo* gogo)
3147 if (!gogo->named_types_are_converted()
3148 && this->results_ != NULL
3149 && this->results_->size() > 1)
3151 Btype* placeholder =
3152 gogo->backend()->placeholder_pointer_type("", this->location(), true);
3153 Function_type::placeholders.push_back(std::make_pair(this, placeholder));
3156 return this->get_function_backend(gogo);
3159 // Convert function types after all named types are converted.
3162 Function_type::convert_types(Gogo* gogo)
3164 for (Placeholders::const_iterator p = Function_type::placeholders.begin();
3165 p != Function_type::placeholders.end();
3168 Btype* bt = p->first->get_function_backend(gogo);
3169 if (!gogo->backend()->set_placeholder_function_type(p->second, bt))
3170 go_assert(saw_errors());
3174 // The type of a function type descriptor.
3177 Function_type::make_function_type_descriptor_type()
3182 Type* tdt = Type::make_type_descriptor_type();
3183 Type* ptdt = Type::make_type_descriptor_ptr_type();
3185 Type* bool_type = Type::lookup_bool_type();
3187 Type* slice_type = Type::make_array_type(ptdt, NULL);
3189 Struct_type* s = Type::make_builtin_struct_type(4,
3191 "dotdotdot", bool_type,
3195 ret = Type::make_builtin_named_type("FuncType", s);
3201 // The type descriptor for a function type.
3204 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3206 Location bloc = Linemap::predeclared_location();
3208 Type* ftdt = Function_type::make_function_type_descriptor_type();
3210 const Struct_field_list* fields = ftdt->struct_type()->fields();
3212 Expression_list* vals = new Expression_list();
3215 Struct_field_list::const_iterator p = fields->begin();
3216 go_assert(p->is_field_name("commonType"));
3217 vals->push_back(this->type_descriptor_constructor(gogo,
3218 RUNTIME_TYPE_KIND_FUNC,
3222 go_assert(p->is_field_name("dotdotdot"));
3223 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3226 go_assert(p->is_field_name("in"));
3227 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3228 this->parameters()));
3231 go_assert(p->is_field_name("out"));
3232 vals->push_back(this->type_descriptor_params(p->type(), NULL,
3236 go_assert(p == fields->end());
3238 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3241 // Return a composite literal for the parameters or results of a type
3245 Function_type::type_descriptor_params(Type* params_type,
3246 const Typed_identifier* receiver,
3247 const Typed_identifier_list* params)
3249 Location bloc = Linemap::predeclared_location();
3251 if (receiver == NULL && params == NULL)
3252 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3254 Expression_list* vals = new Expression_list();
3255 vals->reserve((params == NULL ? 0 : params->size())
3256 + (receiver != NULL ? 1 : 0));
3258 if (receiver != NULL)
3259 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3263 for (Typed_identifier_list::const_iterator p = params->begin();
3266 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3269 return Expression::make_slice_composite_literal(params_type, vals, bloc);
3272 // The reflection string.
3275 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3277 // FIXME: Turn this off until we straighten out the type of the
3278 // struct field used in a go statement which calls a method.
3279 // go_assert(this->receiver_ == NULL);
3281 ret->append("func");
3283 if (this->receiver_ != NULL)
3285 ret->push_back('(');
3286 this->append_reflection(this->receiver_->type(), gogo, ret);
3287 ret->push_back(')');
3290 ret->push_back('(');
3291 const Typed_identifier_list* params = this->parameters();
3294 bool is_varargs = this->is_varargs_;
3295 for (Typed_identifier_list::const_iterator p = params->begin();
3299 if (p != params->begin())
3301 if (!is_varargs || p + 1 != params->end())
3302 this->append_reflection(p->type(), gogo, ret);
3306 this->append_reflection(p->type()->array_type()->element_type(),
3311 ret->push_back(')');
3313 const Typed_identifier_list* results = this->results();
3314 if (results != NULL && !results->empty())
3316 if (results->size() == 1)
3317 ret->push_back(' ');
3320 for (Typed_identifier_list::const_iterator p = results->begin();
3321 p != results->end();
3324 if (p != results->begin())
3326 this->append_reflection(p->type(), gogo, ret);
3328 if (results->size() > 1)
3329 ret->push_back(')');
3336 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3338 ret->push_back('F');
3340 if (this->receiver_ != NULL)
3342 ret->push_back('m');
3343 this->append_mangled_name(this->receiver_->type(), gogo, ret);
3346 const Typed_identifier_list* params = this->parameters();
3349 ret->push_back('p');
3350 for (Typed_identifier_list::const_iterator p = params->begin();
3353 this->append_mangled_name(p->type(), gogo, ret);
3354 if (this->is_varargs_)
3355 ret->push_back('V');
3356 ret->push_back('e');
3359 const Typed_identifier_list* results = this->results();
3360 if (results != NULL)
3362 ret->push_back('r');
3363 for (Typed_identifier_list::const_iterator p = results->begin();
3364 p != results->end();
3366 this->append_mangled_name(p->type(), gogo, ret);
3367 ret->push_back('e');
3370 ret->push_back('e');
3373 // Export a function type.
3376 Function_type::do_export(Export* exp) const
3378 // We don't write out the receiver. The only function types which
3379 // should have a receiver are the ones associated with explicitly
3380 // defined methods. For those the receiver type is written out by
3381 // Function::export_func.
3383 exp->write_c_string("(");
3385 if (this->parameters_ != NULL)
3387 bool is_varargs = this->is_varargs_;
3388 for (Typed_identifier_list::const_iterator p =
3389 this->parameters_->begin();
3390 p != this->parameters_->end();
3396 exp->write_c_string(", ");
3397 if (!is_varargs || p + 1 != this->parameters_->end())
3398 exp->write_type(p->type());
3401 exp->write_c_string("...");
3402 exp->write_type(p->type()->array_type()->element_type());
3406 exp->write_c_string(")");
3408 const Typed_identifier_list* results = this->results_;
3409 if (results != NULL)
3411 exp->write_c_string(" ");
3412 if (results->size() == 1)
3413 exp->write_type(results->begin()->type());
3417 exp->write_c_string("(");
3418 for (Typed_identifier_list::const_iterator p = results->begin();
3419 p != results->end();
3425 exp->write_c_string(", ");
3426 exp->write_type(p->type());
3428 exp->write_c_string(")");
3433 // Import a function type.
3436 Function_type::do_import(Import* imp)
3438 imp->require_c_string("(");
3439 Typed_identifier_list* parameters;
3440 bool is_varargs = false;
3441 if (imp->peek_char() == ')')
3445 parameters = new Typed_identifier_list();
3448 if (imp->match_c_string("..."))
3454 Type* ptype = imp->read_type();
3456 ptype = Type::make_array_type(ptype, NULL);
3457 parameters->push_back(Typed_identifier(Import::import_marker,
3458 ptype, imp->location()));
3459 if (imp->peek_char() != ',')
3461 go_assert(!is_varargs);
3462 imp->require_c_string(", ");
3465 imp->require_c_string(")");
3467 Typed_identifier_list* results;
3468 if (imp->peek_char() != ' ')
3473 results = new Typed_identifier_list;
3474 if (imp->peek_char() != '(')
3476 Type* rtype = imp->read_type();
3477 results->push_back(Typed_identifier(Import::import_marker, rtype,
3485 Type* rtype = imp->read_type();
3486 results->push_back(Typed_identifier(Import::import_marker,
3487 rtype, imp->location()));
3488 if (imp->peek_char() != ',')
3490 imp->require_c_string(", ");
3492 imp->require_c_string(")");
3496 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3499 ret->set_is_varargs();
3503 // Make a copy of a function type without a receiver.
3506 Function_type::copy_without_receiver() const
3508 go_assert(this->is_method());
3509 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3512 if (this->is_varargs())
3513 ret->set_is_varargs();
3514 if (this->is_builtin())
3515 ret->set_is_builtin();
3519 // Make a copy of a function type with a receiver.
3522 Function_type::copy_with_receiver(Type* receiver_type) const
3524 go_assert(!this->is_method());
3525 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3527 return Type::make_function_type(receiver, this->parameters_,
3528 this->results_, this->location_);
3531 // Make a function type.
3534 Type::make_function_type(Typed_identifier* receiver,
3535 Typed_identifier_list* parameters,
3536 Typed_identifier_list* results,
3539 return new Function_type(receiver, parameters, results, location);
3542 // Class Pointer_type.
3547 Pointer_type::do_traverse(Traverse* traverse)
3549 return Type::traverse(this->to_type_, traverse);
3555 Pointer_type::do_hash_for_method(Gogo* gogo) const
3557 return this->to_type_->hash_for_method(gogo) << 4;
3560 // The tree for a pointer type.
3563 Pointer_type::do_get_backend(Gogo* gogo)
3565 Btype* to_btype = this->to_type_->get_backend(gogo);
3566 return gogo->backend()->pointer_type(to_btype);
3569 // The type of a pointer type descriptor.
3572 Pointer_type::make_pointer_type_descriptor_type()
3577 Type* tdt = Type::make_type_descriptor_type();
3578 Type* ptdt = Type::make_type_descriptor_ptr_type();
3580 Struct_type* s = Type::make_builtin_struct_type(2,
3584 ret = Type::make_builtin_named_type("PtrType", s);
3590 // The type descriptor for a pointer type.
3593 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3595 if (this->is_unsafe_pointer_type())
3597 go_assert(name != NULL);
3598 return this->plain_type_descriptor(gogo,
3599 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3604 Location bloc = Linemap::predeclared_location();
3606 const Methods* methods;
3607 Type* deref = this->points_to();
3608 if (deref->named_type() != NULL)
3609 methods = deref->named_type()->methods();
3610 else if (deref->struct_type() != NULL)
3611 methods = deref->struct_type()->methods();
3615 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3617 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3619 Expression_list* vals = new Expression_list();
3622 Struct_field_list::const_iterator p = fields->begin();
3623 go_assert(p->is_field_name("commonType"));
3624 vals->push_back(this->type_descriptor_constructor(gogo,
3625 RUNTIME_TYPE_KIND_PTR,
3626 name, methods, false));
3629 go_assert(p->is_field_name("elem"));
3630 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3632 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3636 // Reflection string.
3639 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3641 ret->push_back('*');
3642 this->append_reflection(this->to_type_, gogo, ret);
3648 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3650 ret->push_back('p');
3651 this->append_mangled_name(this->to_type_, gogo, ret);
3657 Pointer_type::do_export(Export* exp) const
3659 exp->write_c_string("*");
3660 if (this->is_unsafe_pointer_type())
3661 exp->write_c_string("any");
3663 exp->write_type(this->to_type_);
3669 Pointer_type::do_import(Import* imp)
3671 imp->require_c_string("*");
3672 if (imp->match_c_string("any"))
3675 return Type::make_pointer_type(Type::make_void_type());
3677 Type* to = imp->read_type();
3678 return Type::make_pointer_type(to);
3681 // Make a pointer type.
3684 Type::make_pointer_type(Type* to_type)
3686 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3687 static Hashtable pointer_types;
3688 Hashtable::const_iterator p = pointer_types.find(to_type);
3689 if (p != pointer_types.end())
3691 Pointer_type* ret = new Pointer_type(to_type);
3692 pointer_types[to_type] = ret;
3696 // The nil type. We use a special type for nil because it is not the
3697 // same as any other type. In C term nil has type void*, but there is
3698 // no such type in Go.
3700 class Nil_type : public Type
3709 do_compare_is_identity(Gogo*) const
3713 do_get_backend(Gogo* gogo)
3714 { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3717 do_type_descriptor(Gogo*, Named_type*)
3718 { go_unreachable(); }
3721 do_reflection(Gogo*, std::string*) const
3722 { go_unreachable(); }
3725 do_mangled_name(Gogo*, std::string* ret) const
3726 { ret->push_back('n'); }
3729 // Make the nil type.
3732 Type::make_nil_type()
3734 static Nil_type singleton_nil_type;
3735 return &singleton_nil_type;
3738 // The type of a function call which returns multiple values. This is
3739 // really a struct, but we don't want to confuse a function call which
3740 // returns a struct with a function call which returns multiple
3743 class Call_multiple_result_type : public Type
3746 Call_multiple_result_type(Call_expression* call)
3747 : Type(TYPE_CALL_MULTIPLE_RESULT),
3753 do_has_pointer() const
3755 go_assert(saw_errors());
3760 do_compare_is_identity(Gogo*) const
3764 do_get_backend(Gogo* gogo)
3766 go_assert(saw_errors());
3767 return gogo->backend()->error_type();
3771 do_type_descriptor(Gogo*, Named_type*)
3773 go_assert(saw_errors());
3774 return Expression::make_error(Linemap::unknown_location());
3778 do_reflection(Gogo*, std::string*) const
3779 { go_assert(saw_errors()); }
3782 do_mangled_name(Gogo*, std::string*) const
3783 { go_assert(saw_errors()); }
3786 // The expression being called.
3787 Call_expression* call_;
3790 // Make a call result type.
3793 Type::make_call_multiple_result_type(Call_expression* call)
3795 return new Call_multiple_result_type(call);
3798 // Class Struct_field.
3800 // Get the name of a field.
3803 Struct_field::field_name() const
3805 const std::string& name(this->typed_identifier_.name());
3810 // This is called during parsing, before anything is lowered, so
3811 // we have to be pretty careful to avoid dereferencing an
3812 // unknown type name.
3813 Type* t = this->typed_identifier_.type();
3815 if (t->classification() == Type::TYPE_POINTER)
3818 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3819 dt = ptype->points_to();
3821 if (dt->forward_declaration_type() != NULL)
3822 return dt->forward_declaration_type()->name();
3823 else if (dt->named_type() != NULL)
3824 return dt->named_type()->name();
3825 else if (t->is_error_type() || dt->is_error_type())
3827 static const std::string error_string = "*error*";
3828 return error_string;
3832 // Avoid crashing in the erroneous case where T is named but
3835 if (t->forward_declaration_type() != NULL)
3836 return t->forward_declaration_type()->name();
3837 else if (t->named_type() != NULL)
3838 return t->named_type()->name();
3845 // Return whether this field is named NAME.
3848 Struct_field::is_field_name(const std::string& name) const
3850 const std::string& me(this->typed_identifier_.name());
3855 Type* t = this->typed_identifier_.type();
3856 if (t->points_to() != NULL)
3858 Named_type* nt = t->named_type();
3859 if (nt != NULL && nt->name() == name)
3862 // This is a horrible hack caused by the fact that we don't pack
3863 // the names of builtin types. FIXME.
3866 && nt->name() == Gogo::unpack_hidden_name(name))
3873 // Class Struct_type.
3878 Struct_type::do_traverse(Traverse* traverse)
3880 Struct_field_list* fields = this->fields_;
3883 for (Struct_field_list::iterator p = fields->begin();
3887 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3888 return TRAVERSE_EXIT;
3891 return TRAVERSE_CONTINUE;
3894 // Verify that the struct type is complete and valid.
3897 Struct_type::do_verify()
3899 Struct_field_list* fields = this->fields_;
3903 for (Struct_field_list::iterator p = fields->begin();
3907 Type* t = p->type();
3908 if (t->is_undefined())
3910 error_at(p->location(), "struct field type is incomplete");
3911 p->set_type(Type::make_error_type());
3914 else if (p->is_anonymous())
3916 if (t->named_type() != NULL && t->points_to() != NULL)
3918 error_at(p->location(), "embedded type may not be a pointer");
3919 p->set_type(Type::make_error_type());
3922 if (t->points_to() != NULL
3923 && t->points_to()->interface_type() != NULL)
3925 error_at(p->location(),
3926 "embedded type may not be pointer to interface");
3927 p->set_type(Type::make_error_type());
3935 // Whether this contains a pointer.
3938 Struct_type::do_has_pointer() const
3940 const Struct_field_list* fields = this->fields();
3943 for (Struct_field_list::const_iterator p = fields->begin();
3947 if (p->type()->has_pointer())
3953 // Whether this type is identical to T.
3956 Struct_type::is_identical(const Struct_type* t,
3957 bool errors_are_identical) const
3959 const Struct_field_list* fields1 = this->fields();
3960 const Struct_field_list* fields2 = t->fields();
3961 if (fields1 == NULL || fields2 == NULL)
3962 return fields1 == fields2;
3963 Struct_field_list::const_iterator pf2 = fields2->begin();
3964 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3965 pf1 != fields1->end();
3968 if (pf2 == fields2->end())
3970 if (pf1->field_name() != pf2->field_name())
3972 if (pf1->is_anonymous() != pf2->is_anonymous()
3973 || !Type::are_identical(pf1->type(), pf2->type(),
3974 errors_are_identical, NULL))
3976 if (!pf1->has_tag())
3983 if (!pf2->has_tag())
3985 if (pf1->tag() != pf2->tag())
3989 if (pf2 != fields2->end())
3994 // Whether this struct type has any hidden fields.
3997 Struct_type::struct_has_hidden_fields(const Named_type* within,
3998 std::string* reason) const
4000 const Struct_field_list* fields = this->fields();
4003 const Package* within_package = (within == NULL
4005 : within->named_object()->package());
4006 for (Struct_field_list::const_iterator pf = fields->begin();
4007 pf != fields->end();
4010 if (within_package != NULL
4011 && !pf->is_anonymous()
4012 && Gogo::is_hidden_name(pf->field_name()))
4016 std::string within_name = within->named_object()->message_name();
4017 std::string name = Gogo::message_name(pf->field_name());
4018 size_t bufsize = 200 + within_name.length() + name.length();
4019 char* buf = new char[bufsize];
4020 snprintf(buf, bufsize,
4021 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4022 open_quote, within_name.c_str(), close_quote,
4023 open_quote, name.c_str(), close_quote);
4024 reason->assign(buf);
4030 if (pf->type()->has_hidden_fields(within, reason))
4037 // Whether comparisons of this struct type are simple identity
4041 Struct_type::do_compare_is_identity(Gogo* gogo) const
4043 const Struct_field_list* fields = this->fields_;
4046 unsigned int offset = 0;
4047 for (Struct_field_list::const_iterator pf = fields->begin();
4048 pf != fields->end();
4051 if (!pf->type()->compare_is_identity(gogo))
4054 unsigned int field_align;
4055 if (!pf->type()->backend_type_align(gogo, &field_align))
4057 if ((offset & (field_align - 1)) != 0)
4059 // This struct has padding. We don't guarantee that that
4060 // padding is zero-initialized for a stack variable, so we
4061 // can't use memcmp to compare struct values.
4065 unsigned int field_size;
4066 if (!pf->type()->backend_type_size(gogo, &field_size))
4068 offset += field_size;
4073 // Build identity and hash functions for this struct.
4078 Struct_type::do_hash_for_method(Gogo* gogo) const
4080 unsigned int ret = 0;
4081 if (this->fields() != NULL)
4083 for (Struct_field_list::const_iterator pf = this->fields()->begin();
4084 pf != this->fields()->end();
4086 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4091 // Find the local field NAME.
4094 Struct_type::find_local_field(const std::string& name,
4095 unsigned int *pindex) const
4097 const Struct_field_list* fields = this->fields_;
4101 for (Struct_field_list::const_iterator pf = fields->begin();
4102 pf != fields->end();
4105 if (pf->is_field_name(name))
4115 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4117 Field_reference_expression*
4118 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4119 Location location) const
4122 return this->field_reference_depth(struct_expr, name, location, NULL,
4126 // Return an expression for a field, along with the depth at which it
4129 Field_reference_expression*
4130 Struct_type::field_reference_depth(Expression* struct_expr,
4131 const std::string& name,
4133 Saw_named_type* saw,
4134 unsigned int* depth) const
4136 const Struct_field_list* fields = this->fields_;
4140 // Look for a field with this name.
4142 for (Struct_field_list::const_iterator pf = fields->begin();
4143 pf != fields->end();
4146 if (pf->is_field_name(name))
4149 return Expression::make_field_reference(struct_expr, i, location);
4153 // Look for an anonymous field which contains a field with this
4155 unsigned int found_depth = 0;
4156 Field_reference_expression* ret = NULL;
4158 for (Struct_field_list::const_iterator pf = fields->begin();
4159 pf != fields->end();
4162 if (!pf->is_anonymous())
4165 Struct_type* st = pf->type()->deref()->struct_type();
4169 Saw_named_type* hold_saw = saw;
4170 Saw_named_type saw_here;
4171 Named_type* nt = pf->type()->named_type();
4173 nt = pf->type()->deref()->named_type();
4177 for (q = saw; q != NULL; q = q->next)
4181 // If this is an error, it will be reported
4188 saw_here.next = saw;
4193 // Look for a reference using a NULL struct expression. If we
4194 // find one, fill in the struct expression with a reference to
4196 unsigned int subdepth;
4197 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
4207 if (ret == NULL || subdepth < found_depth)
4212 found_depth = subdepth;
4213 Expression* here = Expression::make_field_reference(struct_expr, i,
4215 if (pf->type()->points_to() != NULL)
4216 here = Expression::make_unary(OPERATOR_MULT, here, location);
4217 while (sub->expr() != NULL)
4219 sub = sub->expr()->deref()->field_reference_expression();
4220 go_assert(sub != NULL);
4222 sub->set_struct_expression(here);
4224 else if (subdepth > found_depth)
4228 // We do not handle ambiguity here--it should be handled by
4229 // Type::bind_field_or_method.
4237 *depth = found_depth + 1;
4242 // Return the total number of fields, including embedded fields.
4245 Struct_type::total_field_count() const
4247 if (this->fields_ == NULL)
4249 unsigned int ret = 0;
4250 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4251 pf != this->fields_->end();
4254 if (!pf->is_anonymous() || pf->type()->struct_type() == NULL)
4257 ret += pf->type()->struct_type()->total_field_count();
4262 // Return whether NAME is an unexported field, for better error reporting.
4265 Struct_type::is_unexported_local_field(Gogo* gogo,
4266 const std::string& name) const
4268 const Struct_field_list* fields = this->fields_;
4271 for (Struct_field_list::const_iterator pf = fields->begin();
4272 pf != fields->end();
4275 const std::string& field_name(pf->field_name());
4276 if (Gogo::is_hidden_name(field_name)
4277 && name == Gogo::unpack_hidden_name(field_name)
4278 && gogo->pack_hidden_name(name, false) != field_name)
4285 // Finalize the methods of an unnamed struct.
4288 Struct_type::finalize_methods(Gogo* gogo)
4290 if (this->all_methods_ != NULL)
4292 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
4295 // Return the method NAME, or NULL if there isn't one or if it is
4296 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
4300 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
4302 return Type::method_function(this->all_methods_, name, is_ambiguous);
4305 // Convert struct fields to the backend representation. This is not
4306 // declared in types.h so that types.h doesn't have to #include
4310 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
4311 std::vector<Backend::Btyped_identifier>* bfields)
4313 bfields->resize(fields->size());
4315 for (Struct_field_list::const_iterator p = fields->begin();
4319 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
4320 (*bfields)[i].btype = p->type()->get_backend(gogo);
4321 (*bfields)[i].location = p->location();
4323 go_assert(i == fields->size());
4326 // Get the tree for a struct type.
4329 Struct_type::do_get_backend(Gogo* gogo)
4331 std::vector<Backend::Btyped_identifier> bfields;
4332 get_backend_struct_fields(gogo, this->fields_, &bfields);
4333 return gogo->backend()->struct_type(bfields);
4336 // The type of a struct type descriptor.
4339 Struct_type::make_struct_type_descriptor_type()
4344 Type* tdt = Type::make_type_descriptor_type();
4345 Type* ptdt = Type::make_type_descriptor_ptr_type();
4347 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4348 Type* string_type = Type::lookup_string_type();
4349 Type* pointer_string_type = Type::make_pointer_type(string_type);
4352 Type::make_builtin_struct_type(5,
4353 "name", pointer_string_type,
4354 "pkgPath", pointer_string_type,
4356 "tag", pointer_string_type,
4357 "offset", uintptr_type);
4358 Type* nsf = Type::make_builtin_named_type("structField", sf);
4360 Type* slice_type = Type::make_array_type(nsf, NULL);
4362 Struct_type* s = Type::make_builtin_struct_type(2,
4364 "fields", slice_type);
4366 ret = Type::make_builtin_named_type("StructType", s);
4372 // Build a type descriptor for a struct type.
4375 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4377 Location bloc = Linemap::predeclared_location();
4379 Type* stdt = Struct_type::make_struct_type_descriptor_type();
4381 const Struct_field_list* fields = stdt->struct_type()->fields();
4383 Expression_list* vals = new Expression_list();
4386 const Methods* methods = this->methods();
4387 // A named struct should not have methods--the methods should attach
4388 // to the named type.
4389 go_assert(methods == NULL || name == NULL);
4391 Struct_field_list::const_iterator ps = fields->begin();
4392 go_assert(ps->is_field_name("commonType"));
4393 vals->push_back(this->type_descriptor_constructor(gogo,
4394 RUNTIME_TYPE_KIND_STRUCT,
4395 name, methods, true));
4398 go_assert(ps->is_field_name("fields"));
4400 Expression_list* elements = new Expression_list();
4401 elements->reserve(this->fields_->size());
4402 Type* element_type = ps->type()->array_type()->element_type();
4403 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4404 pf != this->fields_->end();
4407 const Struct_field_list* f = element_type->struct_type()->fields();
4409 Expression_list* fvals = new Expression_list();
4412 Struct_field_list::const_iterator q = f->begin();
4413 go_assert(q->is_field_name("name"));
4414 if (pf->is_anonymous())
4415 fvals->push_back(Expression::make_nil(bloc));
4418 std::string n = Gogo::unpack_hidden_name(pf->field_name());
4419 Expression* s = Expression::make_string(n, bloc);
4420 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4424 go_assert(q->is_field_name("pkgPath"));
4425 if (!Gogo::is_hidden_name(pf->field_name()))
4426 fvals->push_back(Expression::make_nil(bloc));
4429 std::string n = Gogo::hidden_name_prefix(pf->field_name());
4430 Expression* s = Expression::make_string(n, bloc);
4431 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4435 go_assert(q->is_field_name("typ"));
4436 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
4439 go_assert(q->is_field_name("tag"));
4441 fvals->push_back(Expression::make_nil(bloc));
4444 Expression* s = Expression::make_string(pf->tag(), bloc);
4445 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4449 go_assert(q->is_field_name("offset"));
4450 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
4452 Expression* v = Expression::make_struct_composite_literal(element_type,
4454 elements->push_back(v);
4457 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
4460 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4463 // Write the hash function for a struct which can not use the identity
4467 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
4468 Function_type* hash_fntype,
4469 Function_type* equal_fntype)
4471 Location bloc = Linemap::predeclared_location();
4473 // The pointer to the struct that we are going to hash. This is an
4474 // argument to the hash function we are implementing here.
4475 Named_object* key_arg = gogo->lookup("key", NULL);
4476 go_assert(key_arg != NULL);
4477 Type* key_arg_type = key_arg->var_value()->type();
4479 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4483 mpz_init_set_ui(ival, 0);
4484 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
4487 // Make a temporary to hold the return value, initialized to 0.
4488 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
4490 gogo->add_statement(retval);
4492 // Make a temporary to hold the key as a uintptr.
4493 Expression* ref = Expression::make_var_reference(key_arg, bloc);
4494 ref = Expression::make_cast(uintptr_type, ref, bloc);
4495 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
4497 gogo->add_statement(key);
4499 // Loop over the struct fields.
4501 const Struct_field_list* fields = this->fields_;
4502 for (Struct_field_list::const_iterator pf = fields->begin();
4503 pf != fields->end();
4510 // Multiply retval by 33.
4511 mpz_init_set_ui(ival, 33);
4512 Expression* i33 = Expression::make_integer(&ival, uintptr_type,
4516 ref = Expression::make_temporary_reference(retval, bloc);
4517 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ,
4519 gogo->add_statement(s);
4522 // Get a pointer to the value of this field.
4523 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
4524 ref = Expression::make_temporary_reference(key, bloc);
4525 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
4527 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
4529 // Get the size of this field.
4530 Expression* size = Expression::make_type_info(pf->type(),
4531 Expression::TYPE_INFO_SIZE);
4533 // Get the hash function to use for the type of this field.
4534 Named_object* hash_fn;
4535 Named_object* equal_fn;
4536 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
4537 equal_fntype, &hash_fn, &equal_fn);
4539 // Call the hash function for the field.
4540 Expression_list* args = new Expression_list();
4541 args->push_back(subkey);
4542 args->push_back(size);
4543 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
4544 Expression* call = Expression::make_call(func, args, false, bloc);
4546 // Add the field's hash value to retval.
4547 Temporary_reference_expression* tref =
4548 Expression::make_temporary_reference(retval, bloc);
4549 tref->set_is_lvalue();
4550 Statement* s = Statement::make_assignment_operation(OPERATOR_PLUSEQ,
4552 gogo->add_statement(s);
4555 // Return retval to the caller of the hash function.
4556 Expression_list* vals = new Expression_list();
4557 ref = Expression::make_temporary_reference(retval, bloc);
4558 vals->push_back(ref);
4559 Statement* s = Statement::make_return_statement(vals, bloc);
4560 gogo->add_statement(s);
4563 // Write the equality function for a struct which can not use the
4564 // identity function.
4567 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
4569 Location bloc = Linemap::predeclared_location();
4571 // The pointers to the structs we are going to compare.
4572 Named_object* key1_arg = gogo->lookup("key1", NULL);
4573 Named_object* key2_arg = gogo->lookup("key2", NULL);
4574 go_assert(key1_arg != NULL && key2_arg != NULL);
4576 // Build temporaries with the right types.
4577 Type* pt = Type::make_pointer_type(name != NULL
4578 ? static_cast<Type*>(name)
4579 : static_cast<Type*>(this));
4581 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
4582 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4583 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
4584 gogo->add_statement(p1);
4586 ref = Expression::make_var_reference(key2_arg, bloc);
4587 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4588 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
4589 gogo->add_statement(p2);
4591 const Struct_field_list* fields = this->fields_;
4592 unsigned int field_index = 0;
4593 for (Struct_field_list::const_iterator pf = fields->begin();
4594 pf != fields->end();
4595 ++pf, ++field_index)
4597 // Compare one field in both P1 and P2.
4598 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
4599 f1 = Expression::make_unary(OPERATOR_MULT, f1, bloc);
4600 f1 = Expression::make_field_reference(f1, field_index, bloc);
4602 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
4603 f2 = Expression::make_unary(OPERATOR_MULT, f2, bloc);
4604 f2 = Expression::make_field_reference(f2, field_index, bloc);
4606 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
4608 // If the values are not equal, return false.
4609 gogo->start_block(bloc);
4610 Expression_list* vals = new Expression_list();
4611 vals->push_back(Expression::make_boolean(false, bloc));
4612 Statement* s = Statement::make_return_statement(vals, bloc);
4613 gogo->add_statement(s);
4614 Block* then_block = gogo->finish_block(bloc);
4616 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
4617 gogo->add_statement(s);
4620 // All the fields are equal, so return true.
4621 Expression_list* vals = new Expression_list();
4622 vals->push_back(Expression::make_boolean(true, bloc));
4623 Statement* s = Statement::make_return_statement(vals, bloc);
4624 gogo->add_statement(s);
4627 // Reflection string.
4630 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
4632 ret->append("struct { ");
4634 for (Struct_field_list::const_iterator p = this->fields_->begin();
4635 p != this->fields_->end();
4638 if (p != this->fields_->begin())
4640 if (p->is_anonymous())
4641 ret->push_back('?');
4643 ret->append(Gogo::unpack_hidden_name(p->field_name()));
4644 ret->push_back(' ');
4645 this->append_reflection(p->type(), gogo, ret);
4649 const std::string& tag(p->tag());
4651 for (std::string::const_iterator p = tag.begin();
4656 ret->append("\\x00");
4657 else if (*p == '\n')
4659 else if (*p == '\t')
4662 ret->append("\\\"");
4663 else if (*p == '\\')
4664 ret->append("\\\\");
4668 ret->push_back('"');
4678 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4680 ret->push_back('S');
4682 const Struct_field_list* fields = this->fields_;
4685 for (Struct_field_list::const_iterator p = fields->begin();
4689 if (p->is_anonymous())
4693 std::string n = Gogo::unpack_hidden_name(p->field_name());
4695 snprintf(buf, sizeof buf, "%u_",
4696 static_cast<unsigned int>(n.length()));
4700 this->append_mangled_name(p->type(), gogo, ret);
4703 const std::string& tag(p->tag());
4705 for (std::string::const_iterator p = tag.begin();
4709 if (ISALNUM(*p) || *p == '_')
4714 snprintf(buf, sizeof buf, ".%x.",
4715 static_cast<unsigned int>(*p));
4720 snprintf(buf, sizeof buf, "T%u_",
4721 static_cast<unsigned int>(out.length()));
4728 ret->push_back('e');
4731 // If the offset of field INDEX in the backend implementation can be
4732 // determined, set *POFFSET to the offset in bytes and return true.
4733 // Otherwise, return false.
4736 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
4737 unsigned int* poffset)
4739 if (!this->is_backend_type_size_known(gogo))
4741 size_t offset = gogo->backend()->type_field_offset(this->get_backend(gogo),
4743 *poffset = static_cast<unsigned int>(offset);
4744 if (*poffset != offset)
4752 Struct_type::do_export(Export* exp) const
4754 exp->write_c_string("struct { ");
4755 const Struct_field_list* fields = this->fields_;
4756 go_assert(fields != NULL);
4757 for (Struct_field_list::const_iterator p = fields->begin();
4761 if (p->is_anonymous())
4762 exp->write_string("? ");
4765 exp->write_string(p->field_name());
4766 exp->write_c_string(" ");
4768 exp->write_type(p->type());
4772 exp->write_c_string(" ");
4774 Expression::make_string(p->tag(), Linemap::predeclared_location());
4775 expr->export_expression(exp);
4779 exp->write_c_string("; ");
4781 exp->write_c_string("}");
4787 Struct_type::do_import(Import* imp)
4789 imp->require_c_string("struct { ");
4790 Struct_field_list* fields = new Struct_field_list;
4791 if (imp->peek_char() != '}')
4796 if (imp->match_c_string("? "))
4800 name = imp->read_identifier();
4801 imp->require_c_string(" ");
4803 Type* ftype = imp->read_type();
4805 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4807 if (imp->peek_char() == ' ')
4810 Expression* expr = Expression::import_expression(imp);
4811 String_expression* sexpr = expr->string_expression();
4812 go_assert(sexpr != NULL);
4813 sf.set_tag(sexpr->val());
4817 imp->require_c_string("; ");
4818 fields->push_back(sf);
4819 if (imp->peek_char() == '}')
4823 imp->require_c_string("}");
4825 return Type::make_struct_type(fields, imp->location());
4828 // Make a struct type.
4831 Type::make_struct_type(Struct_field_list* fields,
4834 return new Struct_type(fields, location);
4837 // Class Array_type.
4839 // Whether two array types are identical.
4842 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
4844 if (!Type::are_identical(this->element_type(), t->element_type(),
4845 errors_are_identical, NULL))
4848 Expression* l1 = this->length();
4849 Expression* l2 = t->length();
4851 // Slices of the same element type are identical.
4852 if (l1 == NULL && l2 == NULL)
4855 // Arrays of the same element type are identical if they have the
4857 if (l1 != NULL && l2 != NULL)
4862 // Try to determine the lengths. If we can't, assume the arrays
4863 // are not identical.
4871 if (l1->integer_constant_value(true, v1, &type1)
4872 && l2->integer_constant_value(true, v2, &type2))
4873 ret = mpz_cmp(v1, v2) == 0;
4879 // Otherwise the arrays are not identical.
4886 Array_type::do_traverse(Traverse* traverse)
4888 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
4889 return TRAVERSE_EXIT;
4890 if (this->length_ != NULL
4891 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
4892 return TRAVERSE_EXIT;
4893 return TRAVERSE_CONTINUE;
4896 // Check that the length is valid.
4899 Array_type::verify_length()
4901 if (this->length_ == NULL)
4904 Type_context context(Type::lookup_integer_type("int"), false);
4905 this->length_->determine_type(&context);
4907 if (!this->length_->is_constant())
4909 error_at(this->length_->location(), "array bound is not constant");
4916 if (!this->length_->integer_constant_value(true, val, &vt))
4920 if (!this->length_->float_constant_value(fval, &vt))
4922 if (this->length_->type()->integer_type() != NULL
4923 || this->length_->type()->float_type() != NULL)
4924 error_at(this->length_->location(),
4925 "array bound is not constant");
4927 error_at(this->length_->location(),
4928 "array bound is not numeric");
4933 if (!mpfr_integer_p(fval))
4935 error_at(this->length_->location(),
4936 "array bound truncated to integer");
4942 mpfr_get_z(val, fval, GMP_RNDN);
4946 if (mpz_sgn(val) < 0)
4948 error_at(this->length_->location(), "negative array bound");
4953 Type* int_type = Type::lookup_integer_type("int");
4954 int tbits = int_type->integer_type()->bits();
4955 int vbits = mpz_sizeinbase(val, 2);
4956 if (vbits + 1 > tbits)
4958 error_at(this->length_->location(), "array bound overflows");
4971 Array_type::do_verify()
4973 if (!this->verify_length())
4975 this->length_ = Expression::make_error(this->length_->location());
4981 // Whether we can use memcmp to compare this array.
4984 Array_type::do_compare_is_identity(Gogo* gogo) const
4986 if (this->length_ == NULL)
4989 // Check for [...], which indicates that this is not a real type.
4990 if (this->length_->is_nil_expression())
4993 if (!this->element_type_->compare_is_identity(gogo))
4996 // If there is any padding, then we can't use memcmp.
4999 if (!this->element_type_->backend_type_size(gogo, &size)
5000 || !this->element_type_->backend_type_align(gogo, &align))
5002 if ((size & (align - 1)) != 0)
5008 // Array type hash code.
5011 Array_type::do_hash_for_method(Gogo* gogo) const
5013 // There is no very convenient way to get a hash code for the
5015 return this->element_type_->hash_for_method(gogo) + 1;
5018 // Write the hash function for an array which can not use the identify
5022 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
5023 Function_type* hash_fntype,
5024 Function_type* equal_fntype)
5026 Location bloc = Linemap::predeclared_location();
5028 // The pointer to the array that we are going to hash. This is an
5029 // argument to the hash function we are implementing here.
5030 Named_object* key_arg = gogo->lookup("key", NULL);
5031 go_assert(key_arg != NULL);
5032 Type* key_arg_type = key_arg->var_value()->type();
5034 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5038 mpz_init_set_ui(ival, 0);
5039 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
5042 // Make a temporary to hold the return value, initialized to 0.
5043 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
5045 gogo->add_statement(retval);
5047 // Make a temporary to hold the key as a uintptr.
5048 Expression* ref = Expression::make_var_reference(key_arg, bloc);
5049 ref = Expression::make_cast(uintptr_type, ref, bloc);
5050 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
5052 gogo->add_statement(key);
5054 // Loop over the array elements.
5056 Type* int_type = Type::lookup_integer_type("int");
5057 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5058 gogo->add_statement(index);
5060 Expression* iref = Expression::make_temporary_reference(index, bloc);
5061 Expression* aref = Expression::make_var_reference(key_arg, bloc);
5062 Type* pt = Type::make_pointer_type(name != NULL
5063 ? static_cast<Type*>(name)
5064 : static_cast<Type*>(this));
5065 aref = Expression::make_cast(pt, aref, bloc);
5066 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5071 gogo->start_block(bloc);
5073 // Multiply retval by 33.
5074 mpz_init_set_ui(ival, 33);
5075 Expression* i33 = Expression::make_integer(&ival, uintptr_type, bloc);
5078 ref = Expression::make_temporary_reference(retval, bloc);
5079 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ, ref,
5081 gogo->add_statement(s);
5083 // Get the hash function for the element type.
5084 Named_object* hash_fn;
5085 Named_object* equal_fn;
5086 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
5087 hash_fntype, equal_fntype, &hash_fn,
5090 // Get a pointer to this element in the loop.
5091 Expression* subkey = Expression::make_temporary_reference(key, bloc);
5092 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
5094 // Get the size of each element.
5095 Expression* ele_size = Expression::make_type_info(this->element_type_,
5096 Expression::TYPE_INFO_SIZE);
5098 // Get the hash of this element.
5099 Expression_list* args = new Expression_list();
5100 args->push_back(subkey);
5101 args->push_back(ele_size);
5102 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
5103 Expression* call = Expression::make_call(func, args, false, bloc);
5105 // Add the element's hash value to retval.
5106 Temporary_reference_expression* tref =
5107 Expression::make_temporary_reference(retval, bloc);
5108 tref->set_is_lvalue();
5109 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, call, bloc);
5110 gogo->add_statement(s);
5112 // Increase the element pointer.
5113 tref = Expression::make_temporary_reference(key, bloc);
5114 tref->set_is_lvalue();
5115 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
5118 Block* statements = gogo->finish_block(bloc);
5120 for_range->add_statements(statements);
5121 gogo->add_statement(for_range);
5123 // Return retval to the caller of the hash function.
5124 Expression_list* vals = new Expression_list();
5125 ref = Expression::make_temporary_reference(retval, bloc);
5126 vals->push_back(ref);
5127 s = Statement::make_return_statement(vals, bloc);
5128 gogo->add_statement(s);
5131 // Write the equality function for an array which can not use the
5132 // identity function.
5135 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
5137 Location bloc = Linemap::predeclared_location();
5139 // The pointers to the arrays we are going to compare.
5140 Named_object* key1_arg = gogo->lookup("key1", NULL);
5141 Named_object* key2_arg = gogo->lookup("key2", NULL);
5142 go_assert(key1_arg != NULL && key2_arg != NULL);
5144 // Build temporaries for the keys with the right types.
5145 Type* pt = Type::make_pointer_type(name != NULL
5146 ? static_cast<Type*>(name)
5147 : static_cast<Type*>(this));
5149 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
5150 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5151 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
5152 gogo->add_statement(p1);
5154 ref = Expression::make_var_reference(key2_arg, bloc);
5155 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5156 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
5157 gogo->add_statement(p2);
5159 // Loop over the array elements.
5161 Type* int_type = Type::lookup_integer_type("int");
5162 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5163 gogo->add_statement(index);
5165 Expression* iref = Expression::make_temporary_reference(index, bloc);
5166 Expression* aref = Expression::make_temporary_reference(p1, bloc);
5167 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5172 gogo->start_block(bloc);
5174 // Compare element in P1 and P2.
5175 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
5176 e1 = Expression::make_unary(OPERATOR_MULT, e1, bloc);
5177 ref = Expression::make_temporary_reference(index, bloc);
5178 e1 = Expression::make_array_index(e1, ref, NULL, bloc);
5180 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
5181 e2 = Expression::make_unary(OPERATOR_MULT, e2, bloc);
5182 ref = Expression::make_temporary_reference(index, bloc);
5183 e2 = Expression::make_array_index(e2, ref, NULL, bloc);
5185 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
5187 // If the elements are not equal, return false.
5188 gogo->start_block(bloc);
5189 Expression_list* vals = new Expression_list();
5190 vals->push_back(Expression::make_boolean(false, bloc));
5191 Statement* s = Statement::make_return_statement(vals, bloc);
5192 gogo->add_statement(s);
5193 Block* then_block = gogo->finish_block(bloc);
5195 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
5196 gogo->add_statement(s);
5198 Block* statements = gogo->finish_block(bloc);
5200 for_range->add_statements(statements);
5201 gogo->add_statement(for_range);
5203 // All the elements are equal, so return true.
5204 vals = new Expression_list();
5205 vals->push_back(Expression::make_boolean(true, bloc));
5206 s = Statement::make_return_statement(vals, bloc);
5207 gogo->add_statement(s);
5210 // Get a tree for the length of a fixed array. The length may be
5211 // computed using a function call, so we must only evaluate it once.
5214 Array_type::get_length_tree(Gogo* gogo)
5216 go_assert(this->length_ != NULL);
5217 if (this->length_tree_ == NULL_TREE)
5222 if (this->length_->integer_constant_value(true, val, &t))
5225 t = Type::lookup_integer_type("int");
5226 else if (t->is_abstract())
5227 t = t->make_non_abstract_type();
5228 tree tt = type_to_tree(t->get_backend(gogo));
5229 this->length_tree_ = Expression::integer_constant_tree(val, tt);
5236 // Make up a translation context for the array length
5237 // expression. FIXME: This won't work in general.
5238 Translate_context context(gogo, NULL, NULL, NULL);
5239 tree len = this->length_->get_tree(&context);
5240 if (len != error_mark_node)
5242 len = convert_to_integer(integer_type_node, len);
5243 len = save_expr(len);
5245 this->length_tree_ = len;
5248 return this->length_tree_;
5251 // Get the backend representation of the fields of a slice. This is
5252 // not declared in types.h so that types.h doesn't have to #include
5255 // We use int for the count and capacity fields. This matches 6g.
5256 // The language more or less assumes that we can't allocate space of a
5257 // size which does not fit in int.
5260 get_backend_slice_fields(Gogo* gogo, Array_type* type,
5261 std::vector<Backend::Btyped_identifier>* bfields)
5265 Type* pet = Type::make_pointer_type(type->element_type());
5266 Btype* pbet = pet->get_backend(gogo);
5267 Location ploc = Linemap::predeclared_location();
5269 Backend::Btyped_identifier* p = &(*bfields)[0];
5270 p->name = "__values";
5274 Type* int_type = Type::lookup_integer_type("int");
5277 p->name = "__count";
5278 p->btype = int_type->get_backend(gogo);
5282 p->name = "__capacity";
5283 p->btype = int_type->get_backend(gogo);
5287 // Get a tree for the type of this array. A fixed array is simply
5288 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
5289 // just like an array in C. An open array is a struct with three
5290 // fields: a data pointer, the length, and the capacity.
5293 Array_type::do_get_backend(Gogo* gogo)
5295 if (this->length_ == NULL)
5297 std::vector<Backend::Btyped_identifier> bfields;
5298 get_backend_slice_fields(gogo, this, &bfields);
5299 return gogo->backend()->struct_type(bfields);
5303 Btype* element = this->get_backend_element(gogo);
5304 Bexpression* len = this->get_backend_length(gogo);
5305 return gogo->backend()->array_type(element, len);
5309 // Return the backend representation of the element type.
5311 Array_type::get_backend_element(Gogo* gogo)
5313 return this->element_type_->get_backend(gogo);
5316 // Return the backend representation of the length.
5319 Array_type::get_backend_length(Gogo* gogo)
5321 return tree_to_expr(this->get_length_tree(gogo));
5324 // Return a tree for a pointer to the values in ARRAY.
5327 Array_type::value_pointer_tree(Gogo*, tree array) const
5330 if (this->length() != NULL)
5333 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
5334 build_fold_addr_expr(array));
5339 tree field = TYPE_FIELDS(TREE_TYPE(array));
5340 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
5342 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
5345 if (TREE_CONSTANT(array))
5346 TREE_CONSTANT(ret) = 1;
5350 // Return a tree for the length of the array ARRAY which has this
5354 Array_type::length_tree(Gogo* gogo, tree array)
5356 if (this->length_ != NULL)
5358 if (TREE_CODE(array) == SAVE_EXPR)
5359 return fold_convert(integer_type_node, this->get_length_tree(gogo));
5361 return omit_one_operand(integer_type_node,
5362 this->get_length_tree(gogo), array);
5365 // This is an open array. We need to read the length field.
5367 tree type = TREE_TYPE(array);
5368 go_assert(TREE_CODE(type) == RECORD_TYPE);
5370 tree field = DECL_CHAIN(TYPE_FIELDS(type));
5371 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
5373 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5374 if (TREE_CONSTANT(array))
5375 TREE_CONSTANT(ret) = 1;
5379 // Return a tree for the capacity of the array ARRAY which has this
5383 Array_type::capacity_tree(Gogo* gogo, tree array)
5385 if (this->length_ != NULL)
5386 return omit_one_operand(sizetype, this->get_length_tree(gogo), array);
5388 // This is an open array. We need to read the capacity field.
5390 tree type = TREE_TYPE(array);
5391 go_assert(TREE_CODE(type) == RECORD_TYPE);
5393 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
5394 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
5396 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5402 Array_type::do_export(Export* exp) const
5404 exp->write_c_string("[");
5405 if (this->length_ != NULL)
5406 this->length_->export_expression(exp);
5407 exp->write_c_string("] ");
5408 exp->write_type(this->element_type_);
5414 Array_type::do_import(Import* imp)
5416 imp->require_c_string("[");
5418 if (imp->peek_char() == ']')
5421 length = Expression::import_expression(imp);
5422 imp->require_c_string("] ");
5423 Type* element_type = imp->read_type();
5424 return Type::make_array_type(element_type, length);
5427 // The type of an array type descriptor.
5430 Array_type::make_array_type_descriptor_type()
5435 Type* tdt = Type::make_type_descriptor_type();
5436 Type* ptdt = Type::make_type_descriptor_ptr_type();
5438 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5441 Type::make_builtin_struct_type(4,
5445 "len", uintptr_type);
5447 ret = Type::make_builtin_named_type("ArrayType", sf);
5453 // The type of an slice type descriptor.
5456 Array_type::make_slice_type_descriptor_type()
5461 Type* tdt = Type::make_type_descriptor_type();
5462 Type* ptdt = Type::make_type_descriptor_ptr_type();
5465 Type::make_builtin_struct_type(2,
5469 ret = Type::make_builtin_named_type("SliceType", sf);
5475 // Build a type descriptor for an array/slice type.
5478 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5480 if (this->length_ != NULL)
5481 return this->array_type_descriptor(gogo, name);
5483 return this->slice_type_descriptor(gogo, name);
5486 // Build a type descriptor for an array type.
5489 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
5491 Location bloc = Linemap::predeclared_location();
5493 Type* atdt = Array_type::make_array_type_descriptor_type();
5495 const Struct_field_list* fields = atdt->struct_type()->fields();
5497 Expression_list* vals = new Expression_list();
5500 Struct_field_list::const_iterator p = fields->begin();
5501 go_assert(p->is_field_name("commonType"));
5502 vals->push_back(this->type_descriptor_constructor(gogo,
5503 RUNTIME_TYPE_KIND_ARRAY,
5507 go_assert(p->is_field_name("elem"));
5508 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5511 go_assert(p->is_field_name("slice"));
5512 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
5513 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
5516 go_assert(p->is_field_name("len"));
5517 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
5520 go_assert(p == fields->end());
5522 return Expression::make_struct_composite_literal(atdt, vals, bloc);
5525 // Build a type descriptor for a slice type.
5528 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
5530 Location bloc = Linemap::predeclared_location();
5532 Type* stdt = Array_type::make_slice_type_descriptor_type();
5534 const Struct_field_list* fields = stdt->struct_type()->fields();
5536 Expression_list* vals = new Expression_list();
5539 Struct_field_list::const_iterator p = fields->begin();
5540 go_assert(p->is_field_name("commonType"));
5541 vals->push_back(this->type_descriptor_constructor(gogo,
5542 RUNTIME_TYPE_KIND_SLICE,
5546 go_assert(p->is_field_name("elem"));
5547 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5550 go_assert(p == fields->end());
5552 return Expression::make_struct_composite_literal(stdt, vals, bloc);
5555 // Reflection string.
5558 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
5560 ret->push_back('[');
5561 if (this->length_ != NULL)
5566 if (!this->length_->integer_constant_value(true, val, &type))
5567 error_at(this->length_->location(),
5568 "array length must be integer constant expression");
5569 else if (mpz_cmp_si(val, 0) < 0)
5570 error_at(this->length_->location(), "array length is negative");
5571 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
5572 error_at(this->length_->location(), "array length is too large");
5576 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
5581 ret->push_back(']');
5583 this->append_reflection(this->element_type_, gogo, ret);
5589 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5591 ret->push_back('A');
5592 this->append_mangled_name(this->element_type_, gogo, ret);
5593 if (this->length_ != NULL)
5598 if (!this->length_->integer_constant_value(true, val, &type))
5599 error_at(this->length_->location(),
5600 "array length must be integer constant expression");
5601 else if (mpz_cmp_si(val, 0) < 0)
5602 error_at(this->length_->location(), "array length is negative");
5603 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
5604 error_at(this->length_->location(), "array size is too large");
5608 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
5613 ret->push_back('e');
5616 // Make an array type.
5619 Type::make_array_type(Type* element_type, Expression* length)
5621 return new Array_type(element_type, length);
5629 Map_type::do_traverse(Traverse* traverse)
5631 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
5632 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
5633 return TRAVERSE_EXIT;
5634 return TRAVERSE_CONTINUE;
5637 // Check that the map type is OK.
5640 Map_type::do_verify()
5642 // The runtime support uses "map[void]void".
5643 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
5645 error_at(this->location_, "invalid map key type");
5651 // Whether two map types are identical.
5654 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
5656 return (Type::are_identical(this->key_type(), t->key_type(),
5657 errors_are_identical, NULL)
5658 && Type::are_identical(this->val_type(), t->val_type(),
5659 errors_are_identical, NULL));
5665 Map_type::do_hash_for_method(Gogo* gogo) const
5667 return (this->key_type_->hash_for_method(gogo)
5668 + this->val_type_->hash_for_method(gogo)
5672 // Get the backend representation for a map type. A map type is
5673 // represented as a pointer to a struct. The struct is __go_map in
5677 Map_type::do_get_backend(Gogo* gogo)
5679 static Btype* backend_map_type;
5680 if (backend_map_type == NULL)
5682 std::vector<Backend::Btyped_identifier> bfields(4);
5684 Location bloc = Linemap::predeclared_location();
5686 Type* pdt = Type::make_type_descriptor_ptr_type();
5687 bfields[0].name = "__descriptor";
5688 bfields[0].btype = pdt->get_backend(gogo);
5689 bfields[0].location = bloc;
5691 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5692 bfields[1].name = "__element_count";
5693 bfields[1].btype = uintptr_type->get_backend(gogo);
5694 bfields[1].location = bloc;
5696 bfields[2].name = "__bucket_count";
5697 bfields[2].btype = bfields[1].btype;
5698 bfields[2].location = bloc;
5700 Btype* bvt = gogo->backend()->void_type();
5701 Btype* bpvt = gogo->backend()->pointer_type(bvt);
5702 Btype* bppvt = gogo->backend()->pointer_type(bpvt);
5703 bfields[3].name = "__buckets";
5704 bfields[3].btype = bppvt;
5705 bfields[3].location = bloc;
5707 Btype *bt = gogo->backend()->struct_type(bfields);
5708 bt = gogo->backend()->named_type("__go_map", bt, bloc);
5709 backend_map_type = gogo->backend()->pointer_type(bt);
5711 return backend_map_type;
5714 // The type of a map type descriptor.
5717 Map_type::make_map_type_descriptor_type()
5722 Type* tdt = Type::make_type_descriptor_type();
5723 Type* ptdt = Type::make_type_descriptor_ptr_type();
5726 Type::make_builtin_struct_type(3,
5731 ret = Type::make_builtin_named_type("MapType", sf);
5737 // Build a type descriptor for a map type.
5740 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5742 Location bloc = Linemap::predeclared_location();
5744 Type* mtdt = Map_type::make_map_type_descriptor_type();
5746 const Struct_field_list* fields = mtdt->struct_type()->fields();
5748 Expression_list* vals = new Expression_list();
5751 Struct_field_list::const_iterator p = fields->begin();
5752 go_assert(p->is_field_name("commonType"));
5753 vals->push_back(this->type_descriptor_constructor(gogo,
5754 RUNTIME_TYPE_KIND_MAP,
5758 go_assert(p->is_field_name("key"));
5759 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
5762 go_assert(p->is_field_name("elem"));
5763 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
5766 go_assert(p == fields->end());
5768 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
5771 // A mapping from map types to map descriptors.
5773 Map_type::Map_descriptors Map_type::map_descriptors;
5775 // Build a map descriptor for this type. Return a pointer to it.
5778 Map_type::map_descriptor_pointer(Gogo* gogo, Location location)
5780 Bvariable* bvar = this->map_descriptor(gogo);
5781 tree var_tree = var_to_tree(bvar);
5782 if (var_tree == error_mark_node)
5783 return error_mark_node;
5784 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
5787 // Build a map descriptor for this type.
5790 Map_type::map_descriptor(Gogo* gogo)
5792 std::pair<Map_type*, Bvariable*> val(this, NULL);
5793 std::pair<Map_type::Map_descriptors::iterator, bool> ins =
5794 Map_type::map_descriptors.insert(val);
5796 return ins.first->second;
5798 Type* key_type = this->key_type_;
5799 Type* val_type = this->val_type_;
5801 // The map entry type is a struct with three fields. Build that
5802 // struct so that we can get the offsets of the key and value within
5803 // a map entry. The first field should technically be a pointer to
5804 // this type itself, but since we only care about field offsets we
5805 // just use pointer to bool.
5806 Type* pbool = Type::make_pointer_type(Type::make_boolean_type());
5807 Struct_type* map_entry_type =
5808 Type::make_builtin_struct_type(3,
5813 Type* map_descriptor_type = Map_type::make_map_descriptor_type();
5815 const Struct_field_list* fields =
5816 map_descriptor_type->struct_type()->fields();
5818 Expression_list* vals = new Expression_list();
5821 Location bloc = Linemap::predeclared_location();
5823 Struct_field_list::const_iterator p = fields->begin();
5825 go_assert(p->is_field_name("__map_descriptor"));
5826 vals->push_back(Expression::make_type_descriptor(this, bloc));
5829 go_assert(p->is_field_name("__entry_size"));
5830 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
5831 vals->push_back(Expression::make_type_info(map_entry_type, type_info));
5833 Struct_field_list::const_iterator pf = map_entry_type->fields()->begin();
5835 go_assert(pf->is_field_name("__key"));
5838 go_assert(p->is_field_name("__key_offset"));
5839 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
5842 go_assert(pf->is_field_name("__val"));
5845 go_assert(p->is_field_name("__val_offset"));
5846 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
5849 go_assert(p == fields->end());
5851 Expression* initializer =
5852 Expression::make_struct_composite_literal(map_descriptor_type, vals, bloc);
5854 std::string mangled_name = "__go_map_" + this->mangled_name(gogo);
5855 Btype* map_descriptor_btype = map_descriptor_type->get_backend(gogo);
5856 Bvariable* bvar = gogo->backend()->immutable_struct(mangled_name, true,
5857 map_descriptor_btype,
5860 Translate_context context(gogo, NULL, NULL, NULL);
5861 context.set_is_const();
5862 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
5864 gogo->backend()->immutable_struct_set_init(bvar, mangled_name, true,
5865 map_descriptor_btype, bloc,
5868 ins.first->second = bvar;
5872 // Build the type of a map descriptor. This must match the struct
5873 // __go_map_descriptor in libgo/runtime/map.h.
5876 Map_type::make_map_descriptor_type()
5881 Type* ptdt = Type::make_type_descriptor_ptr_type();
5882 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5884 Type::make_builtin_struct_type(4,
5885 "__map_descriptor", ptdt,
5886 "__entry_size", uintptr_type,
5887 "__key_offset", uintptr_type,
5888 "__val_offset", uintptr_type);
5889 ret = Type::make_builtin_named_type("__go_map_descriptor", sf);
5894 // Reflection string for a map.
5897 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
5899 ret->append("map[");
5900 this->append_reflection(this->key_type_, gogo, ret);
5902 this->append_reflection(this->val_type_, gogo, ret);
5905 // Mangled name for a map.
5908 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5910 ret->push_back('M');
5911 this->append_mangled_name(this->key_type_, gogo, ret);
5913 this->append_mangled_name(this->val_type_, gogo, ret);
5916 // Export a map type.
5919 Map_type::do_export(Export* exp) const
5921 exp->write_c_string("map [");
5922 exp->write_type(this->key_type_);
5923 exp->write_c_string("] ");
5924 exp->write_type(this->val_type_);
5927 // Import a map type.
5930 Map_type::do_import(Import* imp)
5932 imp->require_c_string("map [");
5933 Type* key_type = imp->read_type();
5934 imp->require_c_string("] ");
5935 Type* val_type = imp->read_type();
5936 return Type::make_map_type(key_type, val_type, imp->location());
5942 Type::make_map_type(Type* key_type, Type* val_type, Location location)
5944 return new Map_type(key_type, val_type, location);
5947 // Class Channel_type.
5952 Channel_type::do_hash_for_method(Gogo* gogo) const
5954 unsigned int ret = 0;
5955 if (this->may_send_)
5957 if (this->may_receive_)
5959 if (this->element_type_ != NULL)
5960 ret += this->element_type_->hash_for_method(gogo) << 2;
5964 // Whether this type is the same as T.
5967 Channel_type::is_identical(const Channel_type* t,
5968 bool errors_are_identical) const
5970 if (!Type::are_identical(this->element_type(), t->element_type(),
5971 errors_are_identical, NULL))
5973 return (this->may_send_ == t->may_send_
5974 && this->may_receive_ == t->may_receive_);
5977 // Return the tree for a channel type. A channel is a pointer to a
5978 // __go_channel struct. The __go_channel struct is defined in
5979 // libgo/runtime/channel.h.
5982 Channel_type::do_get_backend(Gogo* gogo)
5984 static Btype* backend_channel_type;
5985 if (backend_channel_type == NULL)
5987 std::vector<Backend::Btyped_identifier> bfields;
5988 Btype* bt = gogo->backend()->struct_type(bfields);
5989 bt = gogo->backend()->named_type("__go_channel", bt,
5990 Linemap::predeclared_location());
5991 backend_channel_type = gogo->backend()->pointer_type(bt);
5993 return backend_channel_type;
5996 // Build a type descriptor for a channel type.
5999 Channel_type::make_chan_type_descriptor_type()
6004 Type* tdt = Type::make_type_descriptor_type();
6005 Type* ptdt = Type::make_type_descriptor_ptr_type();
6007 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6010 Type::make_builtin_struct_type(3,
6013 "dir", uintptr_type);
6015 ret = Type::make_builtin_named_type("ChanType", sf);
6021 // Build a type descriptor for a map type.
6024 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6026 Location bloc = Linemap::predeclared_location();
6028 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
6030 const Struct_field_list* fields = ctdt->struct_type()->fields();
6032 Expression_list* vals = new Expression_list();
6035 Struct_field_list::const_iterator p = fields->begin();
6036 go_assert(p->is_field_name("commonType"));
6037 vals->push_back(this->type_descriptor_constructor(gogo,
6038 RUNTIME_TYPE_KIND_CHAN,
6042 go_assert(p->is_field_name("elem"));
6043 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6046 go_assert(p->is_field_name("dir"));
6047 // These bits must match the ones in libgo/runtime/go-type.h.
6049 if (this->may_receive_)
6051 if (this->may_send_)
6054 mpz_init_set_ui(iv, val);
6055 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
6059 go_assert(p == fields->end());
6061 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
6064 // Reflection string.
6067 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
6069 if (!this->may_send_)
6071 ret->append("chan");
6072 if (!this->may_receive_)
6074 ret->push_back(' ');
6075 this->append_reflection(this->element_type_, gogo, ret);
6081 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6083 ret->push_back('C');
6084 this->append_mangled_name(this->element_type_, gogo, ret);
6085 if (this->may_send_)
6086 ret->push_back('s');
6087 if (this->may_receive_)
6088 ret->push_back('r');
6089 ret->push_back('e');
6095 Channel_type::do_export(Export* exp) const
6097 exp->write_c_string("chan ");
6098 if (this->may_send_ && !this->may_receive_)
6099 exp->write_c_string("-< ");
6100 else if (this->may_receive_ && !this->may_send_)
6101 exp->write_c_string("<- ");
6102 exp->write_type(this->element_type_);
6108 Channel_type::do_import(Import* imp)
6110 imp->require_c_string("chan ");
6114 if (imp->match_c_string("-< "))
6118 may_receive = false;
6120 else if (imp->match_c_string("<- "))
6132 Type* element_type = imp->read_type();
6134 return Type::make_channel_type(may_send, may_receive, element_type);
6137 // Make a new channel type.
6140 Type::make_channel_type(bool send, bool receive, Type* element_type)
6142 return new Channel_type(send, receive, element_type);
6145 // Class Interface_type.
6150 Interface_type::do_traverse(Traverse* traverse)
6152 if (this->methods_ == NULL)
6153 return TRAVERSE_CONTINUE;
6154 return this->methods_->traverse(traverse);
6157 // Finalize the methods. This handles interface inheritance.
6160 Interface_type::finalize_methods()
6162 if (this->methods_ == NULL)
6164 std::vector<Named_type*> seen;
6165 bool is_recursive = false;
6168 while (from < this->methods_->size())
6170 const Typed_identifier* p = &this->methods_->at(from);
6171 if (!p->name().empty())
6174 for (i = 0; i < to; ++i)
6176 if (this->methods_->at(i).name() == p->name())
6178 error_at(p->location(), "duplicate method %qs",
6179 Gogo::message_name(p->name()).c_str());
6186 this->methods_->set(to, *p);
6193 Interface_type* it = p->type()->interface_type();
6196 error_at(p->location(), "interface contains embedded non-interface");
6204 error_at(p->location(), "invalid recursive interface");
6205 is_recursive = true;
6211 Named_type* nt = p->type()->named_type();
6214 std::vector<Named_type*>::const_iterator q;
6215 for (q = seen.begin(); q != seen.end(); ++q)
6219 error_at(p->location(), "inherited interface loop");
6223 if (q != seen.end())
6231 const Typed_identifier_list* methods = it->methods();
6232 if (methods == NULL)
6237 for (Typed_identifier_list::const_iterator q = methods->begin();
6238 q != methods->end();
6241 if (q->name().empty())
6243 if (q->type()->forwarded() == p->type()->forwarded())
6244 error_at(p->location(), "interface inheritance loop");
6248 for (i = from + 1; i < this->methods_->size(); ++i)
6250 const Typed_identifier* r = &this->methods_->at(i);
6251 if (r->name().empty()
6252 && r->type()->forwarded() == q->type()->forwarded())
6254 error_at(p->location(),
6255 "inherited interface listed twice");
6259 if (i == this->methods_->size())
6260 this->methods_->push_back(Typed_identifier(q->name(),
6265 else if (this->find_method(q->name()) == NULL)
6266 this->methods_->push_back(Typed_identifier(q->name(), q->type(),
6271 error_at(p->location(), "inherited method %qs is ambiguous",
6272 Gogo::message_name(q->name()).c_str());
6279 delete this->methods_;
6280 this->methods_ = NULL;
6284 this->methods_->resize(to);
6285 this->methods_->sort_by_name();
6289 // Return the method NAME, or NULL.
6291 const Typed_identifier*
6292 Interface_type::find_method(const std::string& name) const
6294 if (this->methods_ == NULL)
6296 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6297 p != this->methods_->end();
6299 if (p->name() == name)
6304 // Return the method index.
6307 Interface_type::method_index(const std::string& name) const
6309 go_assert(this->methods_ != NULL);
6311 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6312 p != this->methods_->end();
6314 if (p->name() == name)
6319 // Return whether NAME is an unexported method, for better error
6323 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
6325 if (this->methods_ == NULL)
6327 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6328 p != this->methods_->end();
6331 const std::string& method_name(p->name());
6332 if (Gogo::is_hidden_name(method_name)
6333 && name == Gogo::unpack_hidden_name(method_name)
6334 && gogo->pack_hidden_name(name, false) != method_name)
6340 // Whether this type is identical with T.
6343 Interface_type::is_identical(const Interface_type* t,
6344 bool errors_are_identical) const
6346 // We require the same methods with the same types. The methods
6347 // have already been sorted.
6348 if (this->methods() == NULL || t->methods() == NULL)
6349 return this->methods() == t->methods();
6351 Typed_identifier_list::const_iterator p1 = this->methods()->begin();
6352 for (Typed_identifier_list::const_iterator p2 = t->methods()->begin();
6353 p2 != t->methods()->end();
6356 if (p1 == this->methods()->end())
6358 if (p1->name() != p2->name()
6359 || !Type::are_identical(p1->type(), p2->type(),
6360 errors_are_identical, NULL))
6363 if (p1 != this->methods()->end())
6368 // Whether we can assign the interface type T to this type. The types
6369 // are known to not be identical. An interface assignment is only
6370 // permitted if T is known to implement all methods in THIS.
6371 // Otherwise a type guard is required.
6374 Interface_type::is_compatible_for_assign(const Interface_type* t,
6375 std::string* reason) const
6377 if (this->methods() == NULL)
6379 for (Typed_identifier_list::const_iterator p = this->methods()->begin();
6380 p != this->methods()->end();
6383 const Typed_identifier* m = t->find_method(p->name());
6389 snprintf(buf, sizeof buf,
6390 _("need explicit conversion; missing method %s%s%s"),
6391 open_quote, Gogo::message_name(p->name()).c_str(),
6393 reason->assign(buf);
6398 std::string subreason;
6399 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
6403 std::string n = Gogo::message_name(p->name());
6404 size_t len = 100 + n.length() + subreason.length();
6405 char* buf = new char[len];
6406 if (subreason.empty())
6407 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6408 open_quote, n.c_str(), close_quote);
6411 _("incompatible type for method %s%s%s (%s)"),
6412 open_quote, n.c_str(), close_quote,
6414 reason->assign(buf);
6427 Interface_type::do_hash_for_method(Gogo* gogo) const
6429 unsigned int ret = 0;
6430 if (this->methods_ != NULL)
6432 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6433 p != this->methods_->end();
6436 ret = Type::hash_string(p->name(), ret);
6437 ret += p->type()->hash_for_method(gogo);
6444 // Return true if T implements the interface. If it does not, and
6445 // REASON is not NULL, set *REASON to a useful error message.
6448 Interface_type::implements_interface(const Type* t, std::string* reason) const
6450 if (this->methods_ == NULL)
6453 bool is_pointer = false;
6454 const Named_type* nt = t->named_type();
6455 const Struct_type* st = t->struct_type();
6456 // If we start with a named type, we don't dereference it to find
6460 const Type* pt = t->points_to();
6463 // If T is a pointer to a named type, then we need to look at
6464 // the type to which it points.
6466 nt = pt->named_type();
6467 st = pt->struct_type();
6471 // If we have a named type, get the methods from it rather than from
6476 // Only named and struct types have methods.
6477 if (nt == NULL && st == NULL)
6481 if (t->points_to() != NULL
6482 && t->points_to()->interface_type() != NULL)
6483 reason->assign(_("pointer to interface type has no methods"));
6485 reason->assign(_("type has no methods"));
6490 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
6494 if (t->points_to() != NULL
6495 && t->points_to()->interface_type() != NULL)
6496 reason->assign(_("pointer to interface type has no methods"));
6498 reason->assign(_("type has no methods"));
6503 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6504 p != this->methods_->end();
6507 bool is_ambiguous = false;
6508 Method* m = (nt != NULL
6509 ? nt->method_function(p->name(), &is_ambiguous)
6510 : st->method_function(p->name(), &is_ambiguous));
6515 std::string n = Gogo::message_name(p->name());
6516 size_t len = n.length() + 100;
6517 char* buf = new char[len];
6519 snprintf(buf, len, _("ambiguous method %s%s%s"),
6520 open_quote, n.c_str(), close_quote);
6522 snprintf(buf, len, _("missing method %s%s%s"),
6523 open_quote, n.c_str(), close_quote);
6524 reason->assign(buf);
6530 Function_type *p_fn_type = p->type()->function_type();
6531 Function_type* m_fn_type = m->type()->function_type();
6532 go_assert(p_fn_type != NULL && m_fn_type != NULL);
6533 std::string subreason;
6534 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
6538 std::string n = Gogo::message_name(p->name());
6539 size_t len = 100 + n.length() + subreason.length();
6540 char* buf = new char[len];
6541 if (subreason.empty())
6542 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6543 open_quote, n.c_str(), close_quote);
6546 _("incompatible type for method %s%s%s (%s)"),
6547 open_quote, n.c_str(), close_quote,
6549 reason->assign(buf);
6555 if (!is_pointer && !m->is_value_method())
6559 std::string n = Gogo::message_name(p->name());
6560 size_t len = 100 + n.length();
6561 char* buf = new char[len];
6562 snprintf(buf, len, _("method %s%s%s requires a pointer"),
6563 open_quote, n.c_str(), close_quote);
6564 reason->assign(buf);
6574 // Return the backend representation of the empty interface type. We
6575 // use the same struct for all empty interfaces.
6578 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
6580 static Btype* empty_interface_type;
6581 if (empty_interface_type == NULL)
6583 std::vector<Backend::Btyped_identifier> bfields(2);
6585 Location bloc = Linemap::predeclared_location();
6587 Type* pdt = Type::make_type_descriptor_ptr_type();
6588 bfields[0].name = "__type_descriptor";
6589 bfields[0].btype = pdt->get_backend(gogo);
6590 bfields[0].location = bloc;
6592 Type* vt = Type::make_pointer_type(Type::make_void_type());
6593 bfields[1].name = "__object";
6594 bfields[1].btype = vt->get_backend(gogo);
6595 bfields[1].location = bloc;
6597 empty_interface_type = gogo->backend()->struct_type(bfields);
6599 return empty_interface_type;
6602 // Return the fields of a non-empty interface type. This is not
6603 // declared in types.h so that types.h doesn't have to #include
6607 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
6608 std::vector<Backend::Btyped_identifier>* bfields)
6610 Location loc = type->location();
6612 std::vector<Backend::Btyped_identifier> mfields(type->methods()->size() + 1);
6614 Type* pdt = Type::make_type_descriptor_ptr_type();
6615 mfields[0].name = "__type_descriptor";
6616 mfields[0].btype = pdt->get_backend(gogo);
6617 mfields[0].location = loc;
6619 std::string last_name = "";
6621 for (Typed_identifier_list::const_iterator p = type->methods()->begin();
6622 p != type->methods()->end();
6625 mfields[i].name = Gogo::unpack_hidden_name(p->name());
6626 mfields[i].btype = p->type()->get_backend(gogo);
6627 mfields[i].location = loc;
6628 // Sanity check: the names should be sorted.
6629 go_assert(p->name() > last_name);
6630 last_name = p->name();
6633 Btype* methods = gogo->backend()->struct_type(mfields);
6637 (*bfields)[0].name = "__methods";
6638 (*bfields)[0].btype = gogo->backend()->pointer_type(methods);
6639 (*bfields)[0].location = loc;
6641 Type* vt = Type::make_pointer_type(Type::make_void_type());
6642 (*bfields)[1].name = "__object";
6643 (*bfields)[1].btype = vt->get_backend(gogo);
6644 (*bfields)[1].location = Linemap::predeclared_location();
6647 // Return a tree for an interface type. An interface is a pointer to
6648 // a struct. The struct has three fields. The first field is a
6649 // pointer to the type descriptor for the dynamic type of the object.
6650 // The second field is a pointer to a table of methods for the
6651 // interface to be used with the object. The third field is the value
6652 // of the object itself.
6655 Interface_type::do_get_backend(Gogo* gogo)
6657 if (this->methods_ == NULL)
6658 return Interface_type::get_backend_empty_interface_type(gogo);
6661 std::vector<Backend::Btyped_identifier> bfields;
6662 get_backend_interface_fields(gogo, this, &bfields);
6663 return gogo->backend()->struct_type(bfields);
6667 // The type of an interface type descriptor.
6670 Interface_type::make_interface_type_descriptor_type()
6675 Type* tdt = Type::make_type_descriptor_type();
6676 Type* ptdt = Type::make_type_descriptor_ptr_type();
6678 Type* string_type = Type::lookup_string_type();
6679 Type* pointer_string_type = Type::make_pointer_type(string_type);
6682 Type::make_builtin_struct_type(3,
6683 "name", pointer_string_type,
6684 "pkgPath", pointer_string_type,
6687 Type* nsm = Type::make_builtin_named_type("imethod", sm);
6689 Type* slice_nsm = Type::make_array_type(nsm, NULL);
6691 Struct_type* s = Type::make_builtin_struct_type(2,
6693 "methods", slice_nsm);
6695 ret = Type::make_builtin_named_type("InterfaceType", s);
6701 // Build a type descriptor for an interface type.
6704 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6706 Location bloc = Linemap::predeclared_location();
6708 Type* itdt = Interface_type::make_interface_type_descriptor_type();
6710 const Struct_field_list* ifields = itdt->struct_type()->fields();
6712 Expression_list* ivals = new Expression_list();
6715 Struct_field_list::const_iterator pif = ifields->begin();
6716 go_assert(pif->is_field_name("commonType"));
6717 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
6718 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
6722 go_assert(pif->is_field_name("methods"));
6724 Expression_list* methods = new Expression_list();
6725 if (this->methods_ != NULL && !this->methods_->empty())
6727 Type* elemtype = pif->type()->array_type()->element_type();
6729 methods->reserve(this->methods_->size());
6730 for (Typed_identifier_list::const_iterator pm = this->methods_->begin();
6731 pm != this->methods_->end();
6734 const Struct_field_list* mfields = elemtype->struct_type()->fields();
6736 Expression_list* mvals = new Expression_list();
6739 Struct_field_list::const_iterator pmf = mfields->begin();
6740 go_assert(pmf->is_field_name("name"));
6741 std::string s = Gogo::unpack_hidden_name(pm->name());
6742 Expression* e = Expression::make_string(s, bloc);
6743 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6746 go_assert(pmf->is_field_name("pkgPath"));
6747 if (!Gogo::is_hidden_name(pm->name()))
6748 mvals->push_back(Expression::make_nil(bloc));
6751 s = Gogo::hidden_name_prefix(pm->name());
6752 e = Expression::make_string(s, bloc);
6753 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6757 go_assert(pmf->is_field_name("typ"));
6758 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
6761 go_assert(pmf == mfields->end());
6763 e = Expression::make_struct_composite_literal(elemtype, mvals,
6765 methods->push_back(e);
6769 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
6773 go_assert(pif == ifields->end());
6775 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
6778 // Reflection string.
6781 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
6783 ret->append("interface {");
6784 if (this->methods_ != NULL)
6786 ret->push_back(' ');
6787 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6788 p != this->methods_->end();
6791 if (p != this->methods_->begin())
6793 if (!Gogo::is_hidden_name(p->name()))
6794 ret->append(p->name());
6797 // This matches what the gc compiler does.
6798 std::string prefix = Gogo::hidden_name_prefix(p->name());
6799 ret->append(prefix.substr(prefix.find('.') + 1));
6800 ret->push_back('.');
6801 ret->append(Gogo::unpack_hidden_name(p->name()));
6803 std::string sub = p->type()->reflection(gogo);
6804 go_assert(sub.compare(0, 4, "func") == 0);
6805 sub = sub.substr(4);
6808 ret->push_back(' ');
6816 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6818 ret->push_back('I');
6820 const Typed_identifier_list* methods = this->methods_;
6821 if (methods != NULL)
6823 for (Typed_identifier_list::const_iterator p = methods->begin();
6824 p != methods->end();
6827 std::string n = Gogo::unpack_hidden_name(p->name());
6829 snprintf(buf, sizeof buf, "%u_",
6830 static_cast<unsigned int>(n.length()));
6833 this->append_mangled_name(p->type(), gogo, ret);
6837 ret->push_back('e');
6843 Interface_type::do_export(Export* exp) const
6845 exp->write_c_string("interface { ");
6847 const Typed_identifier_list* methods = this->methods_;
6848 if (methods != NULL)
6850 for (Typed_identifier_list::const_iterator pm = methods->begin();
6851 pm != methods->end();
6854 exp->write_string(pm->name());
6855 exp->write_c_string(" (");
6857 const Function_type* fntype = pm->type()->function_type();
6860 const Typed_identifier_list* parameters = fntype->parameters();
6861 if (parameters != NULL)
6863 bool is_varargs = fntype->is_varargs();
6864 for (Typed_identifier_list::const_iterator pp =
6865 parameters->begin();
6866 pp != parameters->end();
6872 exp->write_c_string(", ");
6873 if (!is_varargs || pp + 1 != parameters->end())
6874 exp->write_type(pp->type());
6877 exp->write_c_string("...");
6878 Type *pptype = pp->type();
6879 exp->write_type(pptype->array_type()->element_type());
6884 exp->write_c_string(")");
6886 const Typed_identifier_list* results = fntype->results();
6887 if (results != NULL)
6889 exp->write_c_string(" ");
6890 if (results->size() == 1)
6891 exp->write_type(results->begin()->type());
6895 exp->write_c_string("(");
6896 for (Typed_identifier_list::const_iterator p =
6898 p != results->end();
6904 exp->write_c_string(", ");
6905 exp->write_type(p->type());
6907 exp->write_c_string(")");
6911 exp->write_c_string("; ");
6915 exp->write_c_string("}");
6918 // Import an interface type.
6921 Interface_type::do_import(Import* imp)
6923 imp->require_c_string("interface { ");
6925 Typed_identifier_list* methods = new Typed_identifier_list;
6926 while (imp->peek_char() != '}')
6928 std::string name = imp->read_identifier();
6929 imp->require_c_string(" (");
6931 Typed_identifier_list* parameters;
6932 bool is_varargs = false;
6933 if (imp->peek_char() == ')')
6937 parameters = new Typed_identifier_list;
6940 if (imp->match_c_string("..."))
6946 Type* ptype = imp->read_type();
6948 ptype = Type::make_array_type(ptype, NULL);
6949 parameters->push_back(Typed_identifier(Import::import_marker,
6950 ptype, imp->location()));
6951 if (imp->peek_char() != ',')
6953 go_assert(!is_varargs);
6954 imp->require_c_string(", ");
6957 imp->require_c_string(")");
6959 Typed_identifier_list* results;
6960 if (imp->peek_char() != ' ')
6964 results = new Typed_identifier_list;
6966 if (imp->peek_char() != '(')
6968 Type* rtype = imp->read_type();
6969 results->push_back(Typed_identifier(Import::import_marker,
6970 rtype, imp->location()));
6977 Type* rtype = imp->read_type();
6978 results->push_back(Typed_identifier(Import::import_marker,
6979 rtype, imp->location()));
6980 if (imp->peek_char() != ',')
6982 imp->require_c_string(", ");
6984 imp->require_c_string(")");
6988 Function_type* fntype = Type::make_function_type(NULL, parameters,
6992 fntype->set_is_varargs();
6993 methods->push_back(Typed_identifier(name, fntype, imp->location()));
6995 imp->require_c_string("; ");
6998 imp->require_c_string("}");
7000 if (methods->empty())
7006 return Type::make_interface_type(methods, imp->location());
7009 // Make an interface type.
7012 Type::make_interface_type(Typed_identifier_list* methods,
7015 return new Interface_type(methods, location);
7020 // Bind a method to an object.
7023 Method::bind_method(Expression* expr, Location location) const
7025 if (this->stub_ == NULL)
7027 // When there is no stub object, the binding is determined by
7029 return this->do_bind_method(expr, location);
7031 return Expression::make_bound_method(expr, this->stub_, location);
7034 // Return the named object associated with a method. This may only be
7035 // called after methods are finalized.
7038 Method::named_object() const
7040 if (this->stub_ != NULL)
7042 return this->do_named_object();
7045 // Class Named_method.
7047 // The type of the method.
7050 Named_method::do_type() const
7052 if (this->named_object_->is_function())
7053 return this->named_object_->func_value()->type();
7054 else if (this->named_object_->is_function_declaration())
7055 return this->named_object_->func_declaration_value()->type();
7060 // Return the location of the method receiver.
7063 Named_method::do_receiver_location() const
7065 return this->do_type()->receiver()->location();
7068 // Bind a method to an object.
7071 Named_method::do_bind_method(Expression* expr, Location location) const
7073 Named_object* no = this->named_object_;
7074 Bound_method_expression* bme = Expression::make_bound_method(expr, no,
7076 // If this is not a local method, and it does not use a stub, then
7077 // the real method expects a different type. We need to cast the
7079 if (this->depth() > 0 && !this->needs_stub_method())
7081 Function_type* ftype = this->do_type();
7082 go_assert(ftype->is_method());
7083 Type* frtype = ftype->receiver()->type();
7084 bme->set_first_argument_type(frtype);
7089 // Class Interface_method.
7091 // Bind a method to an object.
7094 Interface_method::do_bind_method(Expression* expr,
7095 Location location) const
7097 return Expression::make_interface_field_reference(expr, this->name_,
7103 // Insert a new method. Return true if it was inserted, false
7107 Methods::insert(const std::string& name, Method* m)
7109 std::pair<Method_map::iterator, bool> ins =
7110 this->methods_.insert(std::make_pair(name, m));
7115 Method* old_method = ins.first->second;
7116 if (m->depth() < old_method->depth())
7119 ins.first->second = m;
7124 if (m->depth() == old_method->depth())
7125 old_method->set_is_ambiguous();
7131 // Return the number of unambiguous methods.
7134 Methods::count() const
7137 for (Method_map::const_iterator p = this->methods_.begin();
7138 p != this->methods_.end();
7140 if (!p->second->is_ambiguous())
7145 // Class Named_type.
7147 // Return the name of the type.
7150 Named_type::name() const
7152 return this->named_object_->name();
7155 // Return the name of the type to use in an error message.
7158 Named_type::message_name() const
7160 return this->named_object_->message_name();
7163 // Return the base type for this type. We have to be careful about
7164 // circular type definitions, which are invalid but may be seen here.
7167 Named_type::named_base()
7172 Type* ret = this->type_->base();
7173 this->seen_ = false;
7178 Named_type::named_base() const
7183 const Type* ret = this->type_->base();
7184 this->seen_ = false;
7188 // Return whether this is an error type. We have to be careful about
7189 // circular type definitions, which are invalid but may be seen here.
7192 Named_type::is_named_error_type() const
7197 bool ret = this->type_->is_error_type();
7198 this->seen_ = false;
7202 // Whether this type is comparable. We have to be careful about
7203 // circular type definitions.
7206 Named_type::named_type_is_comparable(std::string* reason) const
7211 bool ret = Type::are_compatible_for_comparison(true, this->type_,
7212 this->type_, reason);
7213 this->seen_ = false;
7217 // Add a method to this type.
7220 Named_type::add_method(const std::string& name, Function* function)
7222 if (this->local_methods_ == NULL)
7223 this->local_methods_ = new Bindings(NULL);
7224 return this->local_methods_->add_function(name, NULL, function);
7227 // Add a method declaration to this type.
7230 Named_type::add_method_declaration(const std::string& name, Package* package,
7231 Function_type* type,
7234 if (this->local_methods_ == NULL)
7235 this->local_methods_ = new Bindings(NULL);
7236 return this->local_methods_->add_function_declaration(name, package, type,
7240 // Add an existing method to this type.
7243 Named_type::add_existing_method(Named_object* no)
7245 if (this->local_methods_ == NULL)
7246 this->local_methods_ = new Bindings(NULL);
7247 this->local_methods_->add_named_object(no);
7250 // Look for a local method NAME, and returns its named object, or NULL
7254 Named_type::find_local_method(const std::string& name) const
7256 if (this->local_methods_ == NULL)
7258 return this->local_methods_->lookup(name);
7261 // Return whether NAME is an unexported field or method, for better
7265 Named_type::is_unexported_local_method(Gogo* gogo,
7266 const std::string& name) const
7268 Bindings* methods = this->local_methods_;
7269 if (methods != NULL)
7271 for (Bindings::const_declarations_iterator p =
7272 methods->begin_declarations();
7273 p != methods->end_declarations();
7276 if (Gogo::is_hidden_name(p->first)
7277 && name == Gogo::unpack_hidden_name(p->first)
7278 && gogo->pack_hidden_name(name, false) != p->first)
7285 // Build the complete list of methods for this type, which means
7286 // recursively including all methods for anonymous fields. Create all
7290 Named_type::finalize_methods(Gogo* gogo)
7292 if (this->all_methods_ != NULL)
7295 if (this->local_methods_ != NULL
7296 && (this->points_to() != NULL || this->interface_type() != NULL))
7298 const Bindings* lm = this->local_methods_;
7299 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
7300 p != lm->end_declarations();
7302 error_at(p->second->location(),
7303 "invalid pointer or interface receiver type");
7304 delete this->local_methods_;
7305 this->local_methods_ = NULL;
7309 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
7312 // Return the method NAME, or NULL if there isn't one or if it is
7313 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
7317 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
7319 return Type::method_function(this->all_methods_, name, is_ambiguous);
7322 // Return a pointer to the interface method table for this type for
7323 // the interface INTERFACE. IS_POINTER is true if this is for a
7327 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
7330 go_assert(!interface->is_empty());
7332 Interface_method_tables** pimt = (is_pointer
7333 ? &this->interface_method_tables_
7334 : &this->pointer_interface_method_tables_);
7337 *pimt = new Interface_method_tables(5);
7339 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
7340 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
7344 // This is a new entry in the hash table.
7345 go_assert(ins.first->second == NULL_TREE);
7346 ins.first->second = gogo->interface_method_table_for_type(interface,
7351 tree decl = ins.first->second;
7352 if (decl == error_mark_node)
7353 return error_mark_node;
7354 go_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
7355 return build_fold_addr_expr(decl);
7358 // Return whether a named type has any hidden fields.
7361 Named_type::named_type_has_hidden_fields(std::string* reason) const
7366 bool ret = this->type_->has_hidden_fields(this, reason);
7367 this->seen_ = false;
7371 // Look for a use of a complete type within another type. This is
7372 // used to check that we don't try to use a type within itself.
7374 class Find_type_use : public Traverse
7377 Find_type_use(Named_type* find_type)
7378 : Traverse(traverse_types),
7379 find_type_(find_type), found_(false)
7382 // Whether we found the type.
7385 { return this->found_; }
7392 // The type we are looking for.
7393 Named_type* find_type_;
7394 // Whether we found the type.
7398 // Check for FIND_TYPE in TYPE.
7401 Find_type_use::type(Type* type)
7403 if (type->named_type() != NULL && this->find_type_ == type->named_type())
7405 this->found_ = true;
7406 return TRAVERSE_EXIT;
7409 // It's OK if we see a reference to the type in any type which is
7410 // essentially a pointer: a pointer, a slice, a function, a map, or
7412 if (type->points_to() != NULL
7413 || type->is_slice_type()
7414 || type->function_type() != NULL
7415 || type->map_type() != NULL
7416 || type->channel_type() != NULL)
7417 return TRAVERSE_SKIP_COMPONENTS;
7419 // For an interface, a reference to the type in a method type should
7420 // be ignored, but we have to consider direct inheritance. When
7421 // this is called, there may be cases of direct inheritance
7422 // represented as a method with no name.
7423 if (type->interface_type() != NULL)
7425 const Typed_identifier_list* methods = type->interface_type()->methods();
7426 if (methods != NULL)
7428 for (Typed_identifier_list::const_iterator p = methods->begin();
7429 p != methods->end();
7432 if (p->name().empty())
7434 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
7435 return TRAVERSE_EXIT;
7439 return TRAVERSE_SKIP_COMPONENTS;
7442 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
7443 // to convert TYPE to the backend representation before we convert
7445 if (type->named_type() != NULL)
7447 switch (type->base()->classification())
7449 case Type::TYPE_ERROR:
7450 case Type::TYPE_BOOLEAN:
7451 case Type::TYPE_INTEGER:
7452 case Type::TYPE_FLOAT:
7453 case Type::TYPE_COMPLEX:
7454 case Type::TYPE_STRING:
7455 case Type::TYPE_NIL:
7458 case Type::TYPE_ARRAY:
7459 case Type::TYPE_STRUCT:
7460 this->find_type_->add_dependency(type->named_type());
7463 case Type::TYPE_VOID:
7464 case Type::TYPE_SINK:
7465 case Type::TYPE_FUNCTION:
7466 case Type::TYPE_POINTER:
7467 case Type::TYPE_CALL_MULTIPLE_RESULT:
7468 case Type::TYPE_MAP:
7469 case Type::TYPE_CHANNEL:
7470 case Type::TYPE_INTERFACE:
7471 case Type::TYPE_NAMED:
7472 case Type::TYPE_FORWARD:
7478 return TRAVERSE_CONTINUE;
7481 // Verify that a named type does not refer to itself.
7484 Named_type::do_verify()
7486 Find_type_use find(this);
7487 Type::traverse(this->type_, &find);
7490 error_at(this->location_, "invalid recursive type %qs",
7491 this->message_name().c_str());
7492 this->is_error_ = true;
7496 // Check whether any of the local methods overloads an existing
7497 // struct field or interface method. We don't need to check the
7498 // list of methods against itself: that is handled by the Bindings
7500 if (this->local_methods_ != NULL)
7502 Struct_type* st = this->type_->struct_type();
7503 bool found_dup = false;
7506 for (Bindings::const_declarations_iterator p =
7507 this->local_methods_->begin_declarations();
7508 p != this->local_methods_->end_declarations();
7511 const std::string& name(p->first);
7512 if (st != NULL && st->find_local_field(name, NULL) != NULL)
7514 error_at(p->second->location(),
7515 "method %qs redeclares struct field name",
7516 Gogo::message_name(name).c_str());
7528 // Return whether this type is or contains a pointer.
7531 Named_type::do_has_pointer() const
7536 bool ret = this->type_->has_pointer();
7537 this->seen_ = false;
7541 // Return whether comparisons for this type can use the identity
7545 Named_type::do_compare_is_identity(Gogo* gogo) const
7547 // We don't use this->seen_ here because compare_is_identity may
7548 // call base() later, and that will mess up if seen_ is set here.
7549 if (this->seen_in_compare_is_identity_)
7551 this->seen_in_compare_is_identity_ = true;
7552 bool ret = this->type_->compare_is_identity(gogo);
7553 this->seen_in_compare_is_identity_ = false;
7557 // Return a hash code. This is used for method lookup. We simply
7558 // hash on the name itself.
7561 Named_type::do_hash_for_method(Gogo* gogo) const
7563 const std::string& name(this->named_object()->name());
7564 unsigned int ret = Type::hash_string(name, 0);
7566 // GOGO will be NULL here when called from Type_hash_identical.
7567 // That is OK because that is only used for internal hash tables
7568 // where we are going to be comparing named types for equality. In
7569 // other cases, which are cases where the runtime is going to
7570 // compare hash codes to see if the types are the same, we need to
7571 // include the package prefix and name in the hash.
7572 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
7574 const Package* package = this->named_object()->package();
7575 if (package == NULL)
7577 ret = Type::hash_string(gogo->unique_prefix(), ret);
7578 ret = Type::hash_string(gogo->package_name(), ret);
7582 ret = Type::hash_string(package->unique_prefix(), ret);
7583 ret = Type::hash_string(package->name(), ret);
7590 // Convert a named type to the backend representation. In order to
7591 // get dependencies right, we fill in a dummy structure for this type,
7592 // then convert all the dependencies, then complete this type. When
7593 // this function is complete, the size of the type is known.
7596 Named_type::convert(Gogo* gogo)
7598 if (this->is_error_ || this->is_converted_)
7601 this->create_placeholder(gogo);
7603 // Convert all the dependencies. If they refer indirectly back to
7604 // this type, they will pick up the intermediate tree we just
7606 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
7607 p != this->dependencies_.end();
7609 (*p)->convert(gogo);
7611 // Complete this type.
7612 Btype* bt = this->named_btype_;
7613 Type* base = this->type_->base();
7614 switch (base->classification())
7631 // The size of these types is already correct. We don't worry
7632 // about filling them in until later, when we also track
7633 // circular references.
7638 std::vector<Backend::Btyped_identifier> bfields;
7639 get_backend_struct_fields(gogo, base->struct_type()->fields(),
7641 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
7642 bt = gogo->backend()->error_type();
7647 // Slice types were completed in create_placeholder.
7648 if (!base->is_slice_type())
7650 Btype* bet = base->array_type()->get_backend_element(gogo);
7651 Bexpression* blen = base->array_type()->get_backend_length(gogo);
7652 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
7653 bt = gogo->backend()->error_type();
7657 case TYPE_INTERFACE:
7658 // Interface types were completed in create_placeholder.
7666 case TYPE_CALL_MULTIPLE_RESULT:
7672 this->named_btype_ = bt;
7673 this->is_converted_ = true;
7674 this->is_placeholder_ = false;
7677 // Create the placeholder for a named type. This is the first step in
7678 // converting to the backend representation.
7681 Named_type::create_placeholder(Gogo* gogo)
7683 if (this->is_error_)
7684 this->named_btype_ = gogo->backend()->error_type();
7686 if (this->named_btype_ != NULL)
7689 // Create the structure for this type. Note that because we call
7690 // base() here, we don't attempt to represent a named type defined
7691 // as another named type. Instead both named types will point to
7692 // different base representations.
7693 Type* base = this->type_->base();
7695 bool set_name = true;
7696 switch (base->classification())
7699 this->is_error_ = true;
7700 this->named_btype_ = gogo->backend()->error_type();
7710 // These are simple basic types, we can just create them
7712 bt = Type::get_named_base_btype(gogo, base);
7717 // All maps and channels have the same backend representation.
7718 bt = Type::get_named_base_btype(gogo, base);
7724 bool for_function = base->classification() == TYPE_FUNCTION;
7725 bt = gogo->backend()->placeholder_pointer_type(this->name(),
7733 bt = gogo->backend()->placeholder_struct_type(this->name(),
7735 this->is_placeholder_ = true;
7740 if (base->is_slice_type())
7741 bt = gogo->backend()->placeholder_struct_type(this->name(),
7745 bt = gogo->backend()->placeholder_array_type(this->name(),
7747 this->is_placeholder_ = true;
7752 case TYPE_INTERFACE:
7753 if (base->interface_type()->is_empty())
7754 bt = Interface_type::get_backend_empty_interface_type(gogo);
7757 bt = gogo->backend()->placeholder_struct_type(this->name(),
7765 case TYPE_CALL_MULTIPLE_RESULT:
7772 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
7774 this->named_btype_ = bt;
7776 if (base->is_slice_type())
7778 // We do not record slices as dependencies of other types,
7779 // because we can fill them in completely here with the final
7781 std::vector<Backend::Btyped_identifier> bfields;
7782 get_backend_slice_fields(gogo, base->array_type(), &bfields);
7783 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
7784 this->named_btype_ = gogo->backend()->error_type();
7786 else if (base->interface_type() != NULL
7787 && !base->interface_type()->is_empty())
7789 // We do not record interfaces as dependencies of other types,
7790 // because we can fill them in completely here with the final
7792 std::vector<Backend::Btyped_identifier> bfields;
7793 get_backend_interface_fields(gogo, base->interface_type(), &bfields);
7794 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
7795 this->named_btype_ = gogo->backend()->error_type();
7799 // Get a tree for a named type.
7802 Named_type::do_get_backend(Gogo* gogo)
7804 if (this->is_error_)
7805 return gogo->backend()->error_type();
7807 Btype* bt = this->named_btype_;
7809 if (!gogo->named_types_are_converted())
7811 // We have not completed converting named types. NAMED_BTYPE_
7812 // is a placeholder and we shouldn't do anything further.
7816 // We don't build dependencies for types whose sizes do not
7817 // change or are not relevant, so we may see them here while
7818 // converting types.
7819 this->create_placeholder(gogo);
7820 bt = this->named_btype_;
7821 go_assert(bt != NULL);
7825 // We are not converting types. This should only be called if the
7826 // type has already been converted.
7827 if (!this->is_converted_)
7829 go_assert(saw_errors());
7830 return gogo->backend()->error_type();
7833 go_assert(bt != NULL);
7835 // Complete the tree.
7836 Type* base = this->type_->base();
7838 switch (base->classification())
7841 return gogo->backend()->error_type();
7854 case TYPE_INTERFACE:
7858 // Don't build a circular data structure. GENERIC can't handle
7860 if (this->seen_in_get_backend_)
7862 this->is_circular_ = true;
7863 return gogo->backend()->circular_pointer_type(bt, true);
7865 this->seen_in_get_backend_ = true;
7866 bt1 = Type::get_named_base_btype(gogo, base);
7867 this->seen_in_get_backend_ = false;
7868 if (this->is_circular_)
7869 bt1 = gogo->backend()->circular_pointer_type(bt, true);
7870 if (!gogo->backend()->set_placeholder_function_type(bt, bt1))
7871 bt = gogo->backend()->error_type();
7875 // Don't build a circular data structure. GENERIC can't handle
7877 if (this->seen_in_get_backend_)
7879 this->is_circular_ = true;
7880 return gogo->backend()->circular_pointer_type(bt, false);
7882 this->seen_in_get_backend_ = true;
7883 bt1 = Type::get_named_base_btype(gogo, base);
7884 this->seen_in_get_backend_ = false;
7885 if (this->is_circular_)
7886 bt1 = gogo->backend()->circular_pointer_type(bt, false);
7887 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
7888 bt = gogo->backend()->error_type();
7893 case TYPE_CALL_MULTIPLE_RESULT:
7902 // Build a type descriptor for a named type.
7905 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7907 // If NAME is not NULL, then we don't really want the type
7908 // descriptor for this type; we want the descriptor for the
7909 // underlying type, giving it the name NAME.
7910 return this->named_type_descriptor(gogo, this->type_,
7911 name == NULL ? this : name);
7914 // Add to the reflection string. This is used mostly for the name of
7915 // the type used in a type descriptor, not for actual reflection
7919 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
7921 if (!Linemap::is_predeclared_location(this->location()))
7923 const Package* package = this->named_object_->package();
7924 if (package != NULL)
7925 ret->append(package->name());
7927 ret->append(gogo->package_name());
7928 ret->push_back('.');
7930 if (this->in_function_ != NULL)
7932 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
7933 ret->push_back('$');
7935 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
7938 // Get the mangled name.
7941 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7943 Named_object* no = this->named_object_;
7945 if (Linemap::is_predeclared_location(this->location()))
7946 go_assert(this->in_function_ == NULL);
7949 const std::string& unique_prefix(no->package() == NULL
7950 ? gogo->unique_prefix()
7951 : no->package()->unique_prefix());
7952 const std::string& package_name(no->package() == NULL
7953 ? gogo->package_name()
7954 : no->package()->name());
7955 name = unique_prefix;
7956 name.append(1, '.');
7957 name.append(package_name);
7958 name.append(1, '.');
7959 if (this->in_function_ != NULL)
7961 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
7962 name.append(1, '$');
7965 name.append(Gogo::unpack_hidden_name(no->name()));
7967 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
7972 // Export the type. This is called to export a global type.
7975 Named_type::export_named_type(Export* exp, const std::string&) const
7977 // We don't need to write the name of the type here, because it will
7978 // be written by Export::write_type anyhow.
7979 exp->write_c_string("type ");
7980 exp->write_type(this);
7981 exp->write_c_string(";\n");
7984 // Import a named type.
7987 Named_type::import_named_type(Import* imp, Named_type** ptype)
7989 imp->require_c_string("type ");
7990 Type *type = imp->read_type();
7991 *ptype = type->named_type();
7992 go_assert(*ptype != NULL);
7993 imp->require_c_string(";\n");
7996 // Export the type when it is referenced by another type. In this
7997 // case Export::export_type will already have issued the name.
8000 Named_type::do_export(Export* exp) const
8002 exp->write_type(this->type_);
8004 // To save space, we only export the methods directly attached to
8006 Bindings* methods = this->local_methods_;
8007 if (methods == NULL)
8010 exp->write_c_string("\n");
8011 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
8012 p != methods->end_definitions();
8015 exp->write_c_string(" ");
8016 (*p)->export_named_object(exp);
8019 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
8020 p != methods->end_declarations();
8023 if (p->second->is_function_declaration())
8025 exp->write_c_string(" ");
8026 p->second->export_named_object(exp);
8031 // Make a named type.
8034 Type::make_named_type(Named_object* named_object, Type* type,
8037 return new Named_type(named_object, type, location);
8040 // Finalize the methods for TYPE. It will be a named type or a struct
8041 // type. This sets *ALL_METHODS to the list of methods, and builds
8042 // all required stubs.
8045 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
8046 Methods** all_methods)
8048 *all_methods = NULL;
8049 Types_seen types_seen;
8050 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
8052 Type::build_stub_methods(gogo, type, *all_methods, location);
8055 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8056 // build up the struct field indexes as we go. DEPTH is the depth of
8057 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8058 // adding these methods for an anonymous field with pointer type.
8059 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8060 // calls the real method. TYPES_SEEN is used to avoid infinite
8064 Type::add_methods_for_type(const Type* type,
8065 const Method::Field_indexes* field_indexes,
8067 bool is_embedded_pointer,
8068 bool needs_stub_method,
8069 Types_seen* types_seen,
8072 // Pointer types may not have methods.
8073 if (type->points_to() != NULL)
8076 const Named_type* nt = type->named_type();
8079 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
8085 Type::add_local_methods_for_type(nt, field_indexes, depth,
8086 is_embedded_pointer, needs_stub_method,
8089 Type::add_embedded_methods_for_type(type, field_indexes, depth,
8090 is_embedded_pointer, needs_stub_method,
8091 types_seen, methods);
8093 // If we are called with depth > 0, then we are looking at an
8094 // anonymous field of a struct. If such a field has interface type,
8095 // then we need to add the interface methods. We don't want to add
8096 // them when depth == 0, because we will already handle them
8097 // following the usual rules for an interface type.
8099 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
8102 // Add the local methods for the named type NT to *METHODS. The
8103 // parameters are as for add_methods_to_type.
8106 Type::add_local_methods_for_type(const Named_type* nt,
8107 const Method::Field_indexes* field_indexes,
8109 bool is_embedded_pointer,
8110 bool needs_stub_method,
8113 const Bindings* local_methods = nt->local_methods();
8114 if (local_methods == NULL)
8117 if (*methods == NULL)
8118 *methods = new Methods();
8120 for (Bindings::const_declarations_iterator p =
8121 local_methods->begin_declarations();
8122 p != local_methods->end_declarations();
8125 Named_object* no = p->second;
8126 bool is_value_method = (is_embedded_pointer
8127 || !Type::method_expects_pointer(no));
8128 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
8130 || (depth > 0 && is_value_method)));
8131 if (!(*methods)->insert(no->name(), m))
8136 // Add the embedded methods for TYPE to *METHODS. These are the
8137 // methods attached to anonymous fields. The parameters are as for
8138 // add_methods_to_type.
8141 Type::add_embedded_methods_for_type(const Type* type,
8142 const Method::Field_indexes* field_indexes,
8144 bool is_embedded_pointer,
8145 bool needs_stub_method,
8146 Types_seen* types_seen,
8149 // Look for anonymous fields in TYPE. TYPE has fields if it is a
8151 const Struct_type* st = type->struct_type();
8155 const Struct_field_list* fields = st->fields();
8160 for (Struct_field_list::const_iterator pf = fields->begin();
8161 pf != fields->end();
8164 if (!pf->is_anonymous())
8167 Type* ftype = pf->type();
8168 bool is_pointer = false;
8169 if (ftype->points_to() != NULL)
8171 ftype = ftype->points_to();
8174 Named_type* fnt = ftype->named_type();
8177 // This is an error, but it will be diagnosed elsewhere.
8181 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
8182 sub_field_indexes->next = field_indexes;
8183 sub_field_indexes->field_index = i;
8185 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
8186 (is_embedded_pointer || is_pointer),
8195 // If TYPE is an interface type, then add its method to *METHODS.
8196 // This is for interface methods attached to an anonymous field. The
8197 // parameters are as for add_methods_for_type.
8200 Type::add_interface_methods_for_type(const Type* type,
8201 const Method::Field_indexes* field_indexes,
8205 const Interface_type* it = type->interface_type();
8209 const Typed_identifier_list* imethods = it->methods();
8210 if (imethods == NULL)
8213 if (*methods == NULL)
8214 *methods = new Methods();
8216 for (Typed_identifier_list::const_iterator pm = imethods->begin();
8217 pm != imethods->end();
8220 Function_type* fntype = pm->type()->function_type();
8223 // This is an error, but it should be reported elsewhere
8224 // when we look at the methods for IT.
8227 go_assert(!fntype->is_method());
8228 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
8229 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
8230 field_indexes, depth);
8231 if (!(*methods)->insert(pm->name(), m))
8236 // Build stub methods for TYPE as needed. METHODS is the set of
8237 // methods for the type. A stub method may be needed when a type
8238 // inherits a method from an anonymous field. When we need the
8239 // address of the method, as in a type descriptor, we need to build a
8240 // little stub which does the required field dereferences and jumps to
8241 // the real method. LOCATION is the location of the type definition.
8244 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
8247 if (methods == NULL)
8249 for (Methods::const_iterator p = methods->begin();
8250 p != methods->end();
8253 Method* m = p->second;
8254 if (m->is_ambiguous() || !m->needs_stub_method())
8257 const std::string& name(p->first);
8259 // Build a stub method.
8261 const Function_type* fntype = m->type();
8263 static unsigned int counter;
8265 snprintf(buf, sizeof buf, "$this%u", counter);
8268 Type* receiver_type = const_cast<Type*>(type);
8269 if (!m->is_value_method())
8270 receiver_type = Type::make_pointer_type(receiver_type);
8271 Location receiver_location = m->receiver_location();
8272 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
8275 const Typed_identifier_list* fnparams = fntype->parameters();
8276 Typed_identifier_list* stub_params;
8277 if (fnparams == NULL || fnparams->empty())
8281 // We give each stub parameter a unique name.
8282 stub_params = new Typed_identifier_list();
8283 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
8284 pp != fnparams->end();
8288 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
8289 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
8295 const Typed_identifier_list* fnresults = fntype->results();
8296 Typed_identifier_list* stub_results;
8297 if (fnresults == NULL || fnresults->empty())
8298 stub_results = NULL;
8301 // We create the result parameters without any names, since
8302 // we won't refer to them.
8303 stub_results = new Typed_identifier_list();
8304 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
8305 pr != fnresults->end();
8307 stub_results->push_back(Typed_identifier("", pr->type(),
8311 Function_type* stub_type = Type::make_function_type(receiver,
8314 fntype->location());
8315 if (fntype->is_varargs())
8316 stub_type->set_is_varargs();
8318 // We only create the function in the package which creates the
8320 const Package* package;
8321 if (type->named_type() == NULL)
8324 package = type->named_type()->named_object()->package();
8326 if (package != NULL)
8327 stub = Named_object::make_function_declaration(name, package,
8328 stub_type, location);
8331 stub = gogo->start_function(name, stub_type, false,
8332 fntype->location());
8333 Type::build_one_stub_method(gogo, m, buf, stub_params,
8334 fntype->is_varargs(), location);
8335 gogo->finish_function(fntype->location());
8338 m->set_stub_object(stub);
8342 // Build a stub method which adjusts the receiver as required to call
8343 // METHOD. RECEIVER_NAME is the name we used for the receiver.
8344 // PARAMS is the list of function parameters.
8347 Type::build_one_stub_method(Gogo* gogo, Method* method,
8348 const char* receiver_name,
8349 const Typed_identifier_list* params,
8353 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
8354 go_assert(receiver_object != NULL);
8356 Expression* expr = Expression::make_var_reference(receiver_object, location);
8357 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
8358 if (expr->type()->points_to() == NULL)
8359 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8361 Expression_list* arguments;
8362 if (params == NULL || params->empty())
8366 arguments = new Expression_list();
8367 for (Typed_identifier_list::const_iterator p = params->begin();
8371 Named_object* param = gogo->lookup(p->name(), NULL);
8372 go_assert(param != NULL);
8373 Expression* param_ref = Expression::make_var_reference(param,
8375 arguments->push_back(param_ref);
8379 Expression* func = method->bind_method(expr, location);
8380 go_assert(func != NULL);
8381 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
8383 call->set_hidden_fields_are_ok();
8384 size_t count = call->result_count();
8386 gogo->add_statement(Statement::make_statement(call, true));
8389 Expression_list* retvals = new Expression_list();
8391 retvals->push_back(call);
8394 for (size_t i = 0; i < count; ++i)
8395 retvals->push_back(Expression::make_call_result(call, i));
8397 Return_statement* retstat = Statement::make_return_statement(retvals,
8400 // We can return values with hidden fields from a stub. This is
8401 // necessary if the method is itself hidden.
8402 retstat->set_hidden_fields_are_ok();
8404 gogo->add_statement(retstat);
8408 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
8409 // in reverse order.
8412 Type::apply_field_indexes(Expression* expr,
8413 const Method::Field_indexes* field_indexes,
8416 if (field_indexes == NULL)
8418 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
8419 Struct_type* stype = expr->type()->deref()->struct_type();
8420 go_assert(stype != NULL
8421 && field_indexes->field_index < stype->field_count());
8422 if (expr->type()->struct_type() == NULL)
8424 go_assert(expr->type()->points_to() != NULL);
8425 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8426 go_assert(expr->type()->struct_type() == stype);
8428 return Expression::make_field_reference(expr, field_indexes->field_index,
8432 // Return whether NO is a method for which the receiver is a pointer.
8435 Type::method_expects_pointer(const Named_object* no)
8437 const Function_type *fntype;
8438 if (no->is_function())
8439 fntype = no->func_value()->type();
8440 else if (no->is_function_declaration())
8441 fntype = no->func_declaration_value()->type();
8444 return fntype->receiver()->type()->points_to() != NULL;
8447 // Given a set of methods for a type, METHODS, return the method NAME,
8448 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
8449 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
8450 // but is ambiguous (and return NULL).
8453 Type::method_function(const Methods* methods, const std::string& name,
8456 if (is_ambiguous != NULL)
8457 *is_ambiguous = false;
8458 if (methods == NULL)
8460 Methods::const_iterator p = methods->find(name);
8461 if (p == methods->end())
8463 Method* m = p->second;
8464 if (m->is_ambiguous())
8466 if (is_ambiguous != NULL)
8467 *is_ambiguous = true;
8473 // Look for field or method NAME for TYPE. Return an Expression for
8474 // the field or method bound to EXPR. If there is no such field or
8475 // method, give an appropriate error and return an error expression.
8478 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
8479 const std::string& name,
8482 if (type->deref()->is_error_type())
8483 return Expression::make_error(location);
8485 const Named_type* nt = type->deref()->named_type();
8486 const Struct_type* st = type->deref()->struct_type();
8487 const Interface_type* it = type->interface_type();
8489 // If this is a pointer to a pointer, then it is possible that the
8490 // pointed-to type has methods.
8494 && type->points_to() != NULL
8495 && type->points_to()->points_to() != NULL)
8497 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8498 type = type->points_to();
8499 if (type->deref()->is_error_type())
8500 return Expression::make_error(location);
8501 nt = type->points_to()->named_type();
8502 st = type->points_to()->struct_type();
8505 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
8506 || expr->is_addressable());
8507 std::vector<const Named_type*> seen;
8508 bool is_method = false;
8509 bool found_pointer_method = false;
8512 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
8513 &seen, NULL, &is_method,
8514 &found_pointer_method, &ambig1, &ambig2))
8519 go_assert(st != NULL);
8520 if (type->struct_type() == NULL)
8522 go_assert(type->points_to() != NULL);
8523 expr = Expression::make_unary(OPERATOR_MULT, expr,
8525 go_assert(expr->type()->struct_type() == st);
8527 ret = st->field_reference(expr, name, location);
8529 else if (it != NULL && it->find_method(name) != NULL)
8530 ret = Expression::make_interface_field_reference(expr, name,
8536 m = nt->method_function(name, NULL);
8537 else if (st != NULL)
8538 m = st->method_function(name, NULL);
8541 go_assert(m != NULL);
8542 if (!m->is_value_method() && expr->type()->points_to() == NULL)
8543 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8544 ret = m->bind_method(expr, location);
8546 go_assert(ret != NULL);
8551 if (!ambig1.empty())
8552 error_at(location, "%qs is ambiguous via %qs and %qs",
8553 Gogo::message_name(name).c_str(), ambig1.c_str(),
8555 else if (found_pointer_method)
8556 error_at(location, "method requires a pointer");
8557 else if (nt == NULL && st == NULL && it == NULL)
8559 ("reference to field %qs in object which "
8560 "has no fields or methods"),
8561 Gogo::message_name(name).c_str());
8565 if (!Gogo::is_hidden_name(name))
8566 is_unexported = false;
8569 std::string unpacked = Gogo::unpack_hidden_name(name);
8571 is_unexported = Type::is_unexported_field_or_method(gogo, type,
8576 error_at(location, "reference to unexported field or method %qs",
8577 Gogo::message_name(name).c_str());
8579 error_at(location, "reference to undefined field or method %qs",
8580 Gogo::message_name(name).c_str());
8582 return Expression::make_error(location);
8586 // Look in TYPE for a field or method named NAME, return true if one
8587 // is found. This looks through embedded anonymous fields and handles
8588 // ambiguity. If a method is found, sets *IS_METHOD to true;
8589 // otherwise, if a field is found, set it to false. If
8590 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
8591 // whose address can not be taken. SEEN is used to avoid infinite
8592 // recursion on invalid types.
8594 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
8595 // method we couldn't use because it requires a pointer. LEVEL is
8596 // used for recursive calls, and can be NULL for a non-recursive call.
8597 // When this function returns false because it finds that the name is
8598 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
8599 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
8600 // will be unchanged.
8602 // This function just returns whether or not there is a field or
8603 // method, and whether it is a field or method. It doesn't build an
8604 // expression to refer to it. If it is a method, we then look in the
8605 // list of all methods for the type. If it is a field, the search has
8606 // to be done again, looking only for fields, and building up the
8607 // expression as we go.
8610 Type::find_field_or_method(const Type* type,
8611 const std::string& name,
8612 bool receiver_can_be_pointer,
8613 std::vector<const Named_type*>* seen,
8616 bool* found_pointer_method,
8617 std::string* ambig1,
8618 std::string* ambig2)
8620 // Named types can have locally defined methods.
8621 const Named_type* nt = type->named_type();
8622 if (nt == NULL && type->points_to() != NULL)
8623 nt = type->points_to()->named_type();
8626 Named_object* no = nt->find_local_method(name);
8629 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
8635 // Record that we have found a pointer method in order to
8636 // give a better error message if we don't find anything
8638 *found_pointer_method = true;
8641 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8647 // We've already seen this type when searching for methods.
8653 // Interface types can have methods.
8654 const Interface_type* it = type->interface_type();
8655 if (it != NULL && it->find_method(name) != NULL)
8661 // Struct types can have fields. They can also inherit fields and
8662 // methods from anonymous fields.
8663 const Struct_type* st = type->deref()->struct_type();
8666 const Struct_field_list* fields = st->fields();
8671 seen->push_back(nt);
8673 int found_level = 0;
8674 bool found_is_method = false;
8675 std::string found_ambig1;
8676 std::string found_ambig2;
8677 const Struct_field* found_parent = NULL;
8678 for (Struct_field_list::const_iterator pf = fields->begin();
8679 pf != fields->end();
8682 if (pf->is_field_name(name))
8690 if (!pf->is_anonymous())
8693 if (pf->type()->deref()->is_error_type()
8694 || pf->type()->deref()->is_undefined())
8697 Named_type* fnt = pf->type()->named_type();
8699 fnt = pf->type()->deref()->named_type();
8700 go_assert(fnt != NULL);
8702 int sublevel = level == NULL ? 1 : *level + 1;
8704 std::string subambig1;
8705 std::string subambig2;
8706 bool subfound = Type::find_field_or_method(fnt,
8708 receiver_can_be_pointer,
8712 found_pointer_method,
8717 if (!subambig1.empty())
8719 // The name was found via this field, but is ambiguous.
8720 // if the ambiguity is lower or at the same level as
8721 // anything else we have already found, then we want to
8722 // pass the ambiguity back to the caller.
8723 if (found_level == 0 || sublevel <= found_level)
8725 found_ambig1 = (Gogo::message_name(pf->field_name())
8727 found_ambig2 = (Gogo::message_name(pf->field_name())
8729 found_level = sublevel;
8735 // The name was found via this field. Use the level to see
8736 // if we want to use this one, or whether it introduces an
8738 if (found_level == 0 || sublevel < found_level)
8740 found_level = sublevel;
8741 found_is_method = sub_is_method;
8742 found_ambig1.clear();
8743 found_ambig2.clear();
8744 found_parent = &*pf;
8746 else if (sublevel > found_level)
8748 else if (found_ambig1.empty())
8750 // We found an ambiguity.
8751 go_assert(found_parent != NULL);
8752 found_ambig1 = Gogo::message_name(found_parent->field_name());
8753 found_ambig2 = Gogo::message_name(pf->field_name());
8757 // We found an ambiguity, but we already know of one.
8758 // Just report the earlier one.
8763 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
8764 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
8765 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
8766 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
8771 if (found_level == 0)
8773 else if (!found_ambig1.empty())
8775 go_assert(!found_ambig1.empty());
8776 ambig1->assign(found_ambig1);
8777 ambig2->assign(found_ambig2);
8779 *level = found_level;
8785 *level = found_level;
8786 *is_method = found_is_method;
8791 // Return whether NAME is an unexported field or method for TYPE.
8794 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
8795 const std::string& name,
8796 std::vector<const Named_type*>* seen)
8798 const Named_type* nt = type->named_type();
8800 nt = type->deref()->named_type();
8803 if (nt->is_unexported_local_method(gogo, name))
8806 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8812 // We've already seen this type.
8818 const Interface_type* it = type->interface_type();
8819 if (it != NULL && it->is_unexported_method(gogo, name))
8822 type = type->deref();
8824 const Struct_type* st = type->struct_type();
8825 if (st != NULL && st->is_unexported_local_field(gogo, name))
8831 const Struct_field_list* fields = st->fields();
8836 seen->push_back(nt);
8838 for (Struct_field_list::const_iterator pf = fields->begin();
8839 pf != fields->end();
8842 if (pf->is_anonymous()
8843 && !pf->type()->deref()->is_error_type()
8844 && !pf->type()->deref()->is_undefined())
8846 Named_type* subtype = pf->type()->named_type();
8847 if (subtype == NULL)
8848 subtype = pf->type()->deref()->named_type();
8849 if (subtype == NULL)
8851 // This is an error, but it will be diagnosed elsewhere.
8854 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
8869 // Class Forward_declaration.
8871 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
8872 : Type(TYPE_FORWARD),
8873 named_object_(named_object->resolve()), warned_(false)
8875 go_assert(this->named_object_->is_unknown()
8876 || this->named_object_->is_type_declaration());
8879 // Return the named object.
8882 Forward_declaration_type::named_object()
8884 return this->named_object_->resolve();
8888 Forward_declaration_type::named_object() const
8890 return this->named_object_->resolve();
8893 // Return the name of the forward declared type.
8896 Forward_declaration_type::name() const
8898 return this->named_object()->name();
8901 // Warn about a use of a type which has been declared but not defined.
8904 Forward_declaration_type::warn() const
8906 Named_object* no = this->named_object_->resolve();
8907 if (no->is_unknown())
8909 // The name was not defined anywhere.
8912 error_at(this->named_object_->location(),
8913 "use of undefined type %qs",
8914 no->message_name().c_str());
8915 this->warned_ = true;
8918 else if (no->is_type_declaration())
8920 // The name was seen as a type, but the type was never defined.
8921 if (no->type_declaration_value()->using_type())
8923 error_at(this->named_object_->location(),
8924 "use of undefined type %qs",
8925 no->message_name().c_str());
8926 this->warned_ = true;
8931 // The name was defined, but not as a type.
8934 error_at(this->named_object_->location(), "expected type");
8935 this->warned_ = true;
8940 // Get the base type of a declaration. This gives an error if the
8941 // type has not yet been defined.
8944 Forward_declaration_type::real_type()
8946 if (this->is_defined())
8947 return this->named_object()->type_value();
8951 return Type::make_error_type();
8956 Forward_declaration_type::real_type() const
8958 if (this->is_defined())
8959 return this->named_object()->type_value();
8963 return Type::make_error_type();
8967 // Return whether the base type is defined.
8970 Forward_declaration_type::is_defined() const
8972 return this->named_object()->is_type();
8975 // Add a method. This is used when methods are defined before the
8979 Forward_declaration_type::add_method(const std::string& name,
8982 Named_object* no = this->named_object();
8983 if (no->is_unknown())
8984 no->declare_as_type();
8985 return no->type_declaration_value()->add_method(name, function);
8988 // Add a method declaration. This is used when methods are declared
8992 Forward_declaration_type::add_method_declaration(const std::string& name,
8993 Function_type* type,
8996 Named_object* no = this->named_object();
8997 if (no->is_unknown())
8998 no->declare_as_type();
8999 Type_declaration* td = no->type_declaration_value();
9000 return td->add_method_declaration(name, type, location);
9006 Forward_declaration_type::do_traverse(Traverse* traverse)
9008 if (this->is_defined()
9009 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
9010 return TRAVERSE_EXIT;
9011 return TRAVERSE_CONTINUE;
9014 // Get the backend representation for the type.
9017 Forward_declaration_type::do_get_backend(Gogo* gogo)
9019 if (this->is_defined())
9020 return Type::get_named_base_btype(gogo, this->real_type());
9023 return gogo->backend()->error_type();
9025 // We represent an undefined type as a struct with no fields. That
9026 // should work fine for the backend, since the same case can arise
9028 std::vector<Backend::Btyped_identifier> fields;
9029 Btype* bt = gogo->backend()->struct_type(fields);
9030 return gogo->backend()->named_type(this->name(), bt,
9031 this->named_object()->location());
9034 // Build a type descriptor for a forwarded type.
9037 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9039 Location ploc = Linemap::predeclared_location();
9040 if (!this->is_defined())
9041 return Expression::make_nil(ploc);
9044 Type* t = this->real_type();
9046 return this->named_type_descriptor(gogo, t, name);
9048 return Expression::make_type_descriptor(t, ploc);
9052 // The reflection string.
9055 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
9057 this->append_reflection(this->real_type(), gogo, ret);
9060 // The mangled name.
9063 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
9065 if (this->is_defined())
9066 this->append_mangled_name(this->real_type(), gogo, ret);
9069 const Named_object* no = this->named_object();
9071 if (no->package() == NULL)
9072 name = gogo->package_name();
9074 name = no->package()->name();
9076 name += Gogo::unpack_hidden_name(no->name());
9078 snprintf(buf, sizeof buf, "N%u_",
9079 static_cast<unsigned int>(name.length()));
9085 // Export a forward declaration. This can happen when a defined type
9086 // refers to a type which is only declared (and is presumably defined
9087 // in some other file in the same package).
9090 Forward_declaration_type::do_export(Export*) const
9092 // If there is a base type, that should be exported instead of this.
9093 go_assert(!this->is_defined());
9095 // We don't output anything.
9098 // Make a forward declaration.
9101 Type::make_forward_declaration(Named_object* named_object)
9103 return new Forward_declaration_type(named_object);
9106 // Class Typed_identifier_list.
9108 // Sort the entries by name.
9110 struct Typed_identifier_list_sort
9114 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
9115 { return t1.name() < t2.name(); }
9119 Typed_identifier_list::sort_by_name()
9121 std::sort(this->entries_.begin(), this->entries_.end(),
9122 Typed_identifier_list_sort());
9128 Typed_identifier_list::traverse(Traverse* traverse)
9130 for (Typed_identifier_list::const_iterator p = this->begin();
9134 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
9135 return TRAVERSE_EXIT;
9137 return TRAVERSE_CONTINUE;
9142 Typed_identifier_list*
9143 Typed_identifier_list::copy() const
9145 Typed_identifier_list* ret = new Typed_identifier_list();
9146 for (Typed_identifier_list::const_iterator p = this->begin();
9149 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));