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()->element_type()->is_comparable())
577 *reason = _("invalid comparison of non-comparable array");
586 // Return true if a value with type RHS may be assigned to a variable
587 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
588 // hidden fields are modified. If REASON is not NULL, set *REASON to
589 // the reason the types are not assignable.
592 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
593 bool check_hidden_fields,
596 // Do some checks first. Make sure the types are defined.
598 && rhs->forwarded()->forward_declaration_type() == NULL
599 && rhs->is_void_type())
602 *reason = "non-value used as value";
606 if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
608 // Any value may be assigned to the blank identifier.
609 if (lhs->is_sink_type())
612 // All fields of a struct must be exported, or the assignment
613 // must be in the same package.
614 if (check_hidden_fields
616 && rhs->forwarded()->forward_declaration_type() == NULL)
618 if (lhs->has_hidden_fields(NULL, reason)
619 || rhs->has_hidden_fields(NULL, reason))
624 // Identical types are assignable.
625 if (Type::are_identical(lhs, rhs, true, reason))
628 // The types are assignable if they have identical underlying types
629 // and either LHS or RHS is not a named type.
630 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
631 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
632 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
635 // The types are assignable if LHS is an interface type and RHS
636 // implements the required methods.
637 const Interface_type* lhs_interface_type = lhs->interface_type();
638 if (lhs_interface_type != NULL)
640 if (lhs_interface_type->implements_interface(rhs, reason))
642 const Interface_type* rhs_interface_type = rhs->interface_type();
643 if (rhs_interface_type != NULL
644 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
649 // The type are assignable if RHS is a bidirectional channel type,
650 // LHS is a channel type, they have identical element types, and
651 // either LHS or RHS is not a named type.
652 if (lhs->channel_type() != NULL
653 && rhs->channel_type() != NULL
654 && rhs->channel_type()->may_send()
655 && rhs->channel_type()->may_receive()
656 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
657 && Type::are_identical(lhs->channel_type()->element_type(),
658 rhs->channel_type()->element_type(),
663 // The nil type may be assigned to a pointer, function, slice, map,
664 // channel, or interface type.
665 if (rhs->is_nil_type()
666 && (lhs->points_to() != NULL
667 || lhs->function_type() != NULL
668 || lhs->is_slice_type()
669 || lhs->map_type() != NULL
670 || lhs->channel_type() != NULL
671 || lhs->interface_type() != NULL))
674 // An untyped numeric constant may be assigned to a numeric type if
675 // it is representable in that type.
676 if ((rhs->is_abstract()
677 && (rhs->integer_type() != NULL
678 || rhs->float_type() != NULL
679 || rhs->complex_type() != NULL))
680 && (lhs->integer_type() != NULL
681 || lhs->float_type() != NULL
682 || lhs->complex_type() != NULL))
685 // Give some better error messages.
686 if (reason != NULL && reason->empty())
688 if (rhs->interface_type() != NULL)
689 reason->assign(_("need explicit conversion"));
690 else if (rhs->is_call_multiple_result_type())
691 reason->assign(_("multiple value function call in "
692 "single value context"));
693 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
695 size_t len = (lhs->named_type()->name().length()
696 + rhs->named_type()->name().length()
698 char* buf = new char[len];
699 snprintf(buf, len, _("cannot use type %s as type %s"),
700 rhs->named_type()->message_name().c_str(),
701 lhs->named_type()->message_name().c_str());
710 // Return true if a value with type RHS may be assigned to a variable
711 // with type LHS. If REASON is not NULL, set *REASON to the reason
712 // the types are not assignable.
715 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
717 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
720 // Like are_assignable but don't check for hidden fields.
723 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
726 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
729 // Return true if a value with type RHS may be converted to type LHS.
730 // If REASON is not NULL, set *REASON to the reason the types are not
734 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
736 // The types are convertible if they are assignable.
737 if (Type::are_assignable(lhs, rhs, reason))
740 // The types are convertible if they have identical underlying
742 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
743 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
746 // The types are convertible if they are both unnamed pointer types
747 // and their pointer base types have identical underlying types.
748 if (lhs->named_type() == NULL
749 && rhs->named_type() == NULL
750 && lhs->points_to() != NULL
751 && rhs->points_to() != NULL
752 && (lhs->points_to()->named_type() != NULL
753 || rhs->points_to()->named_type() != NULL)
754 && Type::are_identical(lhs->points_to()->base(),
755 rhs->points_to()->base(),
760 // Integer and floating point types are convertible to each other.
761 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
762 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
765 // Complex types are convertible to each other.
766 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
769 // An integer, or []byte, or []int, may be converted to a string.
770 if (lhs->is_string_type())
772 if (rhs->integer_type() != NULL)
774 if (rhs->is_slice_type())
776 const Type* e = rhs->array_type()->element_type()->forwarded();
777 if (e->integer_type() != NULL
778 && (e == Type::lookup_integer_type("uint8")
779 || e == Type::lookup_integer_type("int")))
784 // A string may be converted to []byte or []int.
785 if (rhs->is_string_type() && lhs->is_slice_type())
787 const Type* e = lhs->array_type()->element_type()->forwarded();
788 if (e->integer_type() != NULL
789 && (e == Type::lookup_integer_type("uint8")
790 || e == Type::lookup_integer_type("int")))
794 // An unsafe.Pointer type may be converted to any pointer type or to
795 // uintptr, and vice-versa.
796 if (lhs->is_unsafe_pointer_type()
797 && (rhs->points_to() != NULL
798 || (rhs->integer_type() != NULL
799 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
801 if (rhs->is_unsafe_pointer_type()
802 && (lhs->points_to() != NULL
803 || (lhs->integer_type() != NULL
804 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
807 // Give a better error message.
811 *reason = "invalid type conversion";
814 std::string s = "invalid type conversion (";
824 // Return whether this type has any hidden fields. This is only a
825 // possibility for a few types.
828 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
830 switch (this->forwarded()->classification_)
833 return this->named_type()->named_type_has_hidden_fields(reason);
835 return this->struct_type()->struct_has_hidden_fields(within, reason);
837 return this->array_type()->array_has_hidden_fields(within, reason);
843 // Return a hash code for the type to be used for method lookup.
846 Type::hash_for_method(Gogo* gogo) const
848 unsigned int ret = 0;
849 if (this->classification_ != TYPE_FORWARD)
850 ret += this->classification_;
851 return ret + this->do_hash_for_method(gogo);
854 // Default implementation of do_hash_for_method. This is appropriate
855 // for types with no subfields.
858 Type::do_hash_for_method(Gogo*) const
863 // Return a hash code for a string, given a starting hash.
866 Type::hash_string(const std::string& s, unsigned int h)
868 const char* p = s.data();
869 size_t len = s.length();
870 for (; len > 0; --len)
878 // A hash table mapping unnamed types to the backend representation of
881 Type::Type_btypes Type::type_btypes;
883 // Return a tree representing this type.
886 Type::get_backend(Gogo* gogo)
888 if (this->btype_ != NULL)
891 if (this->forward_declaration_type() != NULL
892 || this->named_type() != NULL)
893 return this->get_btype_without_hash(gogo);
895 if (this->is_error_type())
896 return gogo->backend()->error_type();
898 // To avoid confusing the backend, translate all identical Go types
899 // to the same backend representation. We use a hash table to do
900 // that. There is no need to use the hash table for named types, as
901 // named types are only identical to themselves.
903 std::pair<Type*, Btype*> val(this, NULL);
904 std::pair<Type_btypes::iterator, bool> ins =
905 Type::type_btypes.insert(val);
906 if (!ins.second && ins.first->second != NULL)
908 if (gogo != NULL && gogo->named_types_are_converted())
909 this->btype_ = ins.first->second;
910 return ins.first->second;
913 Btype* bt = this->get_btype_without_hash(gogo);
915 if (ins.first->second == NULL)
916 ins.first->second = bt;
919 // We have already created a backend representation for this
920 // type. This can happen when an unnamed type is defined using
921 // a named type which in turns uses an identical unnamed type.
922 // Use the tree we created earlier and ignore the one we just
924 bt = ins.first->second;
925 if (gogo == NULL || !gogo->named_types_are_converted())
933 // Return the backend representation for a type without looking in the
934 // hash table for identical types. This is used for named types,
935 // since a named type is never identical to any other type.
938 Type::get_btype_without_hash(Gogo* gogo)
940 if (this->btype_ == NULL)
942 Btype* bt = this->do_get_backend(gogo);
944 // For a recursive function or pointer type, we will temporarily
945 // return a circular pointer type during the recursion. We
946 // don't want to record that for a forwarding type, as it may
948 if (this->forward_declaration_type() != NULL
949 && gogo->backend()->is_circular_pointer_type(bt))
952 if (gogo == NULL || !gogo->named_types_are_converted())
960 // Return a pointer to the type descriptor for this type.
963 Type::type_descriptor_pointer(Gogo* gogo, Location location)
965 Type* t = this->forwarded();
966 if (t->type_descriptor_var_ == NULL)
968 t->make_type_descriptor_var(gogo);
969 go_assert(t->type_descriptor_var_ != NULL);
971 tree var_tree = var_to_tree(t->type_descriptor_var_);
972 if (var_tree == error_mark_node)
973 return error_mark_node;
974 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
977 // A mapping from unnamed types to type descriptor variables.
979 Type::Type_descriptor_vars Type::type_descriptor_vars;
981 // Build the type descriptor for this type.
984 Type::make_type_descriptor_var(Gogo* gogo)
986 go_assert(this->type_descriptor_var_ == NULL);
988 Named_type* nt = this->named_type();
990 // We can have multiple instances of unnamed types, but we only want
991 // to emit the type descriptor once. We use a hash table. This is
992 // not necessary for named types, as they are unique, and we store
993 // the type descriptor in the type itself.
994 Bvariable** phash = NULL;
997 Bvariable* bvnull = NULL;
998 std::pair<Type_descriptor_vars::iterator, bool> ins =
999 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1002 // We've already build a type descriptor for this type.
1003 this->type_descriptor_var_ = ins.first->second;
1006 phash = &ins.first->second;
1009 std::string var_name = this->type_descriptor_var_name(gogo, nt);
1011 // Build the contents of the type descriptor.
1012 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1014 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1016 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1018 const Package* dummy;
1019 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1021 this->type_descriptor_var_ =
1022 gogo->backend()->immutable_struct_reference(var_name,
1026 *phash = this->type_descriptor_var_;
1030 // See if this type descriptor can appear in multiple packages.
1031 bool is_common = false;
1034 // We create the descriptor for a builtin type whenever we need
1036 is_common = nt->is_builtin();
1040 // This is an unnamed type. The descriptor could be defined in
1041 // any package where it is needed, and the linker will pick one
1042 // descriptor to keep.
1046 // We are going to build the type descriptor in this package. We
1047 // must create the variable before we convert the initializer to the
1048 // backend representation, because the initializer may refer to the
1049 // type descriptor of this type. By setting type_descriptor_var_ we
1050 // ensure that type_descriptor_pointer will work if called while
1051 // converting INITIALIZER.
1053 this->type_descriptor_var_ =
1054 gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
1057 *phash = this->type_descriptor_var_;
1059 Translate_context context(gogo, NULL, NULL, NULL);
1060 context.set_is_const();
1061 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
1063 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1064 var_name, is_common,
1065 initializer_btype, loc,
1069 // Return the name of the type descriptor variable. If NT is not
1070 // NULL, use it to get the name. Otherwise this is an unnamed type.
1073 Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
1076 return "__go_td_" + this->mangled_name(gogo);
1078 Named_object* no = nt->named_object();
1079 const Named_object* in_function = nt->in_function();
1080 std::string ret = "__go_tdn_";
1081 if (nt->is_builtin())
1082 go_assert(in_function == NULL);
1085 const std::string& unique_prefix(no->package() == NULL
1086 ? gogo->unique_prefix()
1087 : no->package()->unique_prefix());
1088 const std::string& package_name(no->package() == NULL
1089 ? gogo->package_name()
1090 : no->package()->name());
1091 ret.append(unique_prefix);
1093 ret.append(package_name);
1095 if (in_function != NULL)
1097 ret.append(Gogo::unpack_hidden_name(in_function->name()));
1101 ret.append(no->name());
1105 // Return true if this type descriptor is defined in a different
1106 // package. If this returns true it sets *PACKAGE to the package.
1109 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1110 const Package** package)
1114 if (nt->named_object()->package() != NULL)
1116 // This is a named type defined in a different package. The
1117 // type descriptor should be defined in that package.
1118 *package = nt->named_object()->package();
1124 if (this->points_to() != NULL
1125 && this->points_to()->named_type() != NULL
1126 && this->points_to()->named_type()->named_object()->package() != NULL)
1128 // This is an unnamed pointer to a named type defined in a
1129 // different package. The descriptor should be defined in
1131 *package = this->points_to()->named_type()->named_object()->package();
1138 // Return a composite literal for a type descriptor.
1141 Type::type_descriptor(Gogo* gogo, Type* type)
1143 return type->do_type_descriptor(gogo, NULL);
1146 // Return a composite literal for a type descriptor with a name.
1149 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1151 go_assert(name != NULL && type->named_type() != name);
1152 return type->do_type_descriptor(gogo, name);
1155 // Make a builtin struct type from a list of fields. The fields are
1156 // pairs of a name and a type.
1159 Type::make_builtin_struct_type(int nfields, ...)
1162 va_start(ap, nfields);
1164 Location bloc = Linemap::predeclared_location();
1165 Struct_field_list* sfl = new Struct_field_list();
1166 for (int i = 0; i < nfields; i++)
1168 const char* field_name = va_arg(ap, const char *);
1169 Type* type = va_arg(ap, Type*);
1170 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1175 return Type::make_struct_type(sfl, bloc);
1178 // A list of builtin named types.
1180 std::vector<Named_type*> Type::named_builtin_types;
1182 // Make a builtin named type.
1185 Type::make_builtin_named_type(const char* name, Type* type)
1187 Location bloc = Linemap::predeclared_location();
1188 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1189 Named_type* ret = no->type_value();
1190 Type::named_builtin_types.push_back(ret);
1194 // Convert the named builtin types.
1197 Type::convert_builtin_named_types(Gogo* gogo)
1199 for (std::vector<Named_type*>::const_iterator p =
1200 Type::named_builtin_types.begin();
1201 p != Type::named_builtin_types.end();
1204 bool r = (*p)->verify();
1206 (*p)->convert(gogo);
1210 // Return the type of a type descriptor. We should really tie this to
1211 // runtime.Type rather than copying it. This must match commonType in
1212 // libgo/go/runtime/type.go.
1215 Type::make_type_descriptor_type()
1220 Location bloc = Linemap::predeclared_location();
1222 Type* uint8_type = Type::lookup_integer_type("uint8");
1223 Type* uint32_type = Type::lookup_integer_type("uint32");
1224 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1225 Type* string_type = Type::lookup_string_type();
1226 Type* pointer_string_type = Type::make_pointer_type(string_type);
1228 // This is an unnamed version of unsafe.Pointer. Perhaps we
1229 // should use the named version instead, although that would
1230 // require us to create the unsafe package if it has not been
1231 // imported. It probably doesn't matter.
1232 Type* void_type = Type::make_void_type();
1233 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1235 // Forward declaration for the type descriptor type.
1236 Named_object* named_type_descriptor_type =
1237 Named_object::make_type_declaration("commonType", NULL, bloc);
1238 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1239 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1241 // The type of a method on a concrete type.
1242 Struct_type* method_type =
1243 Type::make_builtin_struct_type(5,
1244 "name", pointer_string_type,
1245 "pkgPath", pointer_string_type,
1246 "mtyp", pointer_type_descriptor_type,
1247 "typ", pointer_type_descriptor_type,
1248 "tfn", unsafe_pointer_type);
1249 Named_type* named_method_type =
1250 Type::make_builtin_named_type("method", method_type);
1252 // Information for types with a name or methods.
1253 Type* slice_named_method_type =
1254 Type::make_array_type(named_method_type, NULL);
1255 Struct_type* uncommon_type =
1256 Type::make_builtin_struct_type(3,
1257 "name", pointer_string_type,
1258 "pkgPath", pointer_string_type,
1259 "methods", slice_named_method_type);
1260 Named_type* named_uncommon_type =
1261 Type::make_builtin_named_type("uncommonType", uncommon_type);
1263 Type* pointer_uncommon_type =
1264 Type::make_pointer_type(named_uncommon_type);
1266 // The type descriptor type.
1268 Typed_identifier_list* params = new Typed_identifier_list();
1269 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1270 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1272 Typed_identifier_list* results = new Typed_identifier_list();
1273 results->push_back(Typed_identifier("", uintptr_type, bloc));
1275 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1277 params = new Typed_identifier_list();
1278 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1279 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1280 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1282 results = new Typed_identifier_list();
1283 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1285 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1288 Struct_type* type_descriptor_type =
1289 Type::make_builtin_struct_type(10,
1291 "align", uint8_type,
1292 "fieldAlign", uint8_type,
1293 "size", uintptr_type,
1294 "hash", uint32_type,
1295 "hashfn", hashfn_type,
1296 "equalfn", equalfn_type,
1297 "string", pointer_string_type,
1298 "", pointer_uncommon_type,
1300 pointer_type_descriptor_type);
1302 Named_type* named = Type::make_builtin_named_type("commonType",
1303 type_descriptor_type);
1305 named_type_descriptor_type->set_type_value(named);
1313 // Make the type of a pointer to a type descriptor as represented in
1317 Type::make_type_descriptor_ptr_type()
1321 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1325 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1326 // hash code for this type and which compare whether two values of
1327 // this type are equal. If NAME is not NULL it is the name of this
1328 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1329 // functions, for convenience; they may be NULL.
1332 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1333 Function_type* equal_fntype, Named_object** hash_fn,
1334 Named_object** equal_fn)
1336 if (hash_fntype == NULL || equal_fntype == NULL)
1338 Location bloc = Linemap::predeclared_location();
1340 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1341 Type* void_type = Type::make_void_type();
1342 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1344 if (hash_fntype == NULL)
1346 Typed_identifier_list* params = new Typed_identifier_list();
1347 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1349 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1351 Typed_identifier_list* results = new Typed_identifier_list();
1352 results->push_back(Typed_identifier("", uintptr_type, bloc));
1354 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1356 if (equal_fntype == NULL)
1358 Typed_identifier_list* params = new Typed_identifier_list();
1359 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1361 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1363 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1365 Typed_identifier_list* results = new Typed_identifier_list();
1366 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1369 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1373 const char* hash_fnname;
1374 const char* equal_fnname;
1375 if (this->compare_is_identity())
1377 hash_fnname = "__go_type_hash_identity";
1378 equal_fnname = "__go_type_equal_identity";
1380 else if (!this->is_comparable())
1382 hash_fnname = "__go_type_hash_error";
1383 equal_fnname = "__go_type_equal_error";
1387 switch (this->base()->classification())
1389 case Type::TYPE_ERROR:
1390 case Type::TYPE_VOID:
1391 case Type::TYPE_NIL:
1392 case Type::TYPE_FUNCTION:
1393 case Type::TYPE_MAP:
1394 // For these types is_comparable should have returned false.
1397 case Type::TYPE_BOOLEAN:
1398 case Type::TYPE_INTEGER:
1399 case Type::TYPE_POINTER:
1400 case Type::TYPE_CHANNEL:
1401 // For these types compare_is_identity should have returned true.
1404 case Type::TYPE_FLOAT:
1405 hash_fnname = "__go_type_hash_float";
1406 equal_fnname = "__go_type_equal_float";
1409 case Type::TYPE_COMPLEX:
1410 hash_fnname = "__go_type_hash_complex";
1411 equal_fnname = "__go_type_equal_complex";
1414 case Type::TYPE_STRING:
1415 hash_fnname = "__go_type_hash_string";
1416 equal_fnname = "__go_type_equal_string";
1419 case Type::TYPE_STRUCT:
1421 // This is a struct which can not be compared using a
1422 // simple identity function. We need to build a function
1424 this->specific_type_functions(gogo, name, hash_fntype,
1425 equal_fntype, hash_fn, equal_fn);
1429 case Type::TYPE_ARRAY:
1430 if (this->is_slice_type())
1432 // Type::is_compatible_for_comparison should have
1438 // This is an array which can not be compared using a
1439 // simple identity function. We need to build a
1440 // function for comparison.
1441 this->specific_type_functions(gogo, name, hash_fntype,
1442 equal_fntype, hash_fn, equal_fn);
1447 case Type::TYPE_INTERFACE:
1448 if (this->interface_type()->is_empty())
1450 hash_fnname = "__go_type_hash_empty_interface";
1451 equal_fnname = "__go_type_equal_empty_interface";
1455 hash_fnname = "__go_type_hash_interface";
1456 equal_fnname = "__go_type_equal_interface";
1460 case Type::TYPE_NAMED:
1461 case Type::TYPE_FORWARD:
1470 Location bloc = Linemap::predeclared_location();
1471 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1473 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1474 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1475 equal_fntype, bloc);
1476 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1479 // A hash table mapping types to the specific hash functions.
1481 Type::Type_functions Type::type_functions_table;
1483 // Handle a type function which is specific to a type: a struct or
1484 // array which can not use an identity comparison.
1487 Type::specific_type_functions(Gogo* gogo, Named_type* name,
1488 Function_type* hash_fntype,
1489 Function_type* equal_fntype,
1490 Named_object** hash_fn,
1491 Named_object** equal_fn)
1493 Hash_equal_fn fnull(NULL, NULL);
1494 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1495 std::pair<Type_functions::iterator, bool> ins =
1496 Type::type_functions_table.insert(val);
1499 // We already have functions for this type
1500 *hash_fn = ins.first->second.first;
1501 *equal_fn = ins.first->second.second;
1505 std::string base_name;
1507 base_name = gogo->pack_hidden_name(this->mangled_name(gogo), false);
1510 // This name is already hidden or not as appropriate.
1511 base_name = name->name();
1512 const Named_object* in_function = name->in_function();
1513 if (in_function != NULL)
1514 base_name += '$' + in_function->name();
1516 std::string hash_name = base_name + "$hash";
1517 std::string equal_name = base_name + "$equal";
1519 Location bloc = Linemap::predeclared_location();
1521 const Package* package = NULL;
1522 bool is_defined_elsewhere =
1523 this->type_descriptor_defined_elsewhere(name, &package);
1524 if (is_defined_elsewhere)
1526 *hash_fn = Named_object::make_function_declaration(hash_name, package,
1528 *equal_fn = Named_object::make_function_declaration(equal_name, package,
1529 equal_fntype, bloc);
1533 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1534 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1538 ins.first->second.first = *hash_fn;
1539 ins.first->second.second = *equal_fn;
1541 if (!is_defined_elsewhere)
1543 if (gogo->in_global_scope())
1544 this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1545 equal_name, equal_fntype);
1547 gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1548 equal_name, equal_fntype);
1552 // Write the hash and equality functions for a type which needs to be
1553 // written specially.
1556 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1557 const std::string& hash_name,
1558 Function_type* hash_fntype,
1559 const std::string& equal_name,
1560 Function_type* equal_fntype)
1562 Location bloc = Linemap::predeclared_location();
1564 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1566 gogo->start_block(bloc);
1568 if (this->struct_type() != NULL)
1569 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1571 else if (this->array_type() != NULL)
1572 this->array_type()->write_hash_function(gogo, name, hash_fntype,
1577 Block* b = gogo->finish_block(bloc);
1578 gogo->add_block(b, bloc);
1579 gogo->lower_block(hash_fn, b);
1580 gogo->finish_function(bloc);
1582 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1584 gogo->start_block(bloc);
1586 if (this->struct_type() != NULL)
1587 this->struct_type()->write_equal_function(gogo, name);
1588 else if (this->array_type() != NULL)
1589 this->array_type()->write_equal_function(gogo, name);
1593 b = gogo->finish_block(bloc);
1594 gogo->add_block(b, bloc);
1595 gogo->lower_block(equal_fn, b);
1596 gogo->finish_function(bloc);
1599 // Return a composite literal for the type descriptor for a plain type
1600 // of kind RUNTIME_TYPE_KIND named NAME.
1603 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1604 Named_type* name, const Methods* methods,
1605 bool only_value_methods)
1607 Location bloc = Linemap::predeclared_location();
1609 Type* td_type = Type::make_type_descriptor_type();
1610 const Struct_field_list* fields = td_type->struct_type()->fields();
1612 Expression_list* vals = new Expression_list();
1615 if (!this->has_pointer())
1616 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1617 Struct_field_list::const_iterator p = fields->begin();
1618 go_assert(p->is_field_name("Kind"));
1620 mpz_init_set_ui(iv, runtime_type_kind);
1621 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1624 go_assert(p->is_field_name("align"));
1625 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1626 vals->push_back(Expression::make_type_info(this, type_info));
1629 go_assert(p->is_field_name("fieldAlign"));
1630 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1631 vals->push_back(Expression::make_type_info(this, type_info));
1634 go_assert(p->is_field_name("size"));
1635 type_info = Expression::TYPE_INFO_SIZE;
1636 vals->push_back(Expression::make_type_info(this, type_info));
1639 go_assert(p->is_field_name("hash"));
1640 mpz_set_ui(iv, this->hash_for_method(gogo));
1641 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1644 go_assert(p->is_field_name("hashfn"));
1645 Function_type* hash_fntype = p->type()->function_type();
1648 go_assert(p->is_field_name("equalfn"));
1649 Function_type* equal_fntype = p->type()->function_type();
1651 Named_object* hash_fn;
1652 Named_object* equal_fn;
1653 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1655 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1656 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1659 go_assert(p->is_field_name("string"));
1660 Expression* s = Expression::make_string((name != NULL
1661 ? name->reflection(gogo)
1662 : this->reflection(gogo)),
1664 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1667 go_assert(p->is_field_name("uncommonType"));
1668 if (name == NULL && methods == NULL)
1669 vals->push_back(Expression::make_nil(bloc));
1672 if (methods == NULL)
1673 methods = name->methods();
1674 vals->push_back(this->uncommon_type_constructor(gogo,
1677 only_value_methods));
1681 go_assert(p->is_field_name("ptrToThis"));
1683 vals->push_back(Expression::make_nil(bloc));
1686 Type* pt = Type::make_pointer_type(name);
1687 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1691 go_assert(p == fields->end());
1695 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1698 // Return a composite literal for the uncommon type information for
1699 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1700 // struct. If name is not NULL, it is the name of the type. If
1701 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1702 // is true if only value methods should be included. At least one of
1703 // NAME and METHODS must not be NULL.
1706 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1707 Named_type* name, const Methods* methods,
1708 bool only_value_methods) const
1710 Location bloc = Linemap::predeclared_location();
1712 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1714 Expression_list* vals = new Expression_list();
1717 Struct_field_list::const_iterator p = fields->begin();
1718 go_assert(p->is_field_name("name"));
1721 go_assert(p->is_field_name("pkgPath"));
1725 vals->push_back(Expression::make_nil(bloc));
1726 vals->push_back(Expression::make_nil(bloc));
1730 Named_object* no = name->named_object();
1731 std::string n = Gogo::unpack_hidden_name(no->name());
1732 Expression* s = Expression::make_string(n, bloc);
1733 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1735 if (name->is_builtin())
1736 vals->push_back(Expression::make_nil(bloc));
1739 const Package* package = no->package();
1740 const std::string& unique_prefix(package == NULL
1741 ? gogo->unique_prefix()
1742 : package->unique_prefix());
1743 const std::string& package_name(package == NULL
1744 ? gogo->package_name()
1746 n.assign(unique_prefix);
1748 n.append(package_name);
1749 if (name->in_function() != NULL)
1752 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1754 s = Expression::make_string(n, bloc);
1755 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1760 go_assert(p->is_field_name("methods"));
1761 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1762 only_value_methods));
1765 go_assert(p == fields->end());
1767 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1769 return Expression::make_unary(OPERATOR_AND, r, bloc);
1772 // Sort methods by name.
1778 operator()(const std::pair<std::string, const Method*>& m1,
1779 const std::pair<std::string, const Method*>& m2) const
1780 { return m1.first < m2.first; }
1783 // Return a composite literal for the type method table for this type.
1784 // METHODS_TYPE is the type of the table, and is a slice type.
1785 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1786 // then only value methods are used.
1789 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1790 const Methods* methods,
1791 bool only_value_methods) const
1793 Location bloc = Linemap::predeclared_location();
1795 std::vector<std::pair<std::string, const Method*> > smethods;
1796 if (methods != NULL)
1798 smethods.reserve(methods->count());
1799 for (Methods::const_iterator p = methods->begin();
1800 p != methods->end();
1803 if (p->second->is_ambiguous())
1805 if (only_value_methods && !p->second->is_value_method())
1807 smethods.push_back(std::make_pair(p->first, p->second));
1811 if (smethods.empty())
1812 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1814 std::sort(smethods.begin(), smethods.end(), Sort_methods());
1816 Type* method_type = methods_type->array_type()->element_type();
1818 Expression_list* vals = new Expression_list();
1819 vals->reserve(smethods.size());
1820 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1822 p != smethods.end();
1824 vals->push_back(this->method_constructor(gogo, method_type, p->first,
1825 p->second, only_value_methods));
1827 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1830 // Return a composite literal for a single method. METHOD_TYPE is the
1831 // type of the entry. METHOD_NAME is the name of the method and M is
1832 // the method information.
1835 Type::method_constructor(Gogo*, Type* method_type,
1836 const std::string& method_name,
1838 bool only_value_methods) const
1840 Location bloc = Linemap::predeclared_location();
1842 const Struct_field_list* fields = method_type->struct_type()->fields();
1844 Expression_list* vals = new Expression_list();
1847 Struct_field_list::const_iterator p = fields->begin();
1848 go_assert(p->is_field_name("name"));
1849 const std::string n = Gogo::unpack_hidden_name(method_name);
1850 Expression* s = Expression::make_string(n, bloc);
1851 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1854 go_assert(p->is_field_name("pkgPath"));
1855 if (!Gogo::is_hidden_name(method_name))
1856 vals->push_back(Expression::make_nil(bloc));
1859 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1860 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1863 Named_object* no = (m->needs_stub_method()
1865 : m->named_object());
1867 Function_type* mtype;
1868 if (no->is_function())
1869 mtype = no->func_value()->type();
1871 mtype = no->func_declaration_value()->type();
1872 go_assert(mtype->is_method());
1873 Type* nonmethod_type = mtype->copy_without_receiver();
1876 go_assert(p->is_field_name("mtyp"));
1877 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1880 go_assert(p->is_field_name("typ"));
1881 if (!only_value_methods && m->is_value_method())
1883 // This is a value method on a pointer type. Change the type of
1884 // the method to use a pointer receiver. The implementation
1885 // always uses a pointer receiver anyhow.
1886 Type* rtype = mtype->receiver()->type();
1887 Type* prtype = Type::make_pointer_type(rtype);
1888 Typed_identifier* receiver =
1889 new Typed_identifier(mtype->receiver()->name(), prtype,
1890 mtype->receiver()->location());
1891 mtype = Type::make_function_type(receiver,
1892 (mtype->parameters() == NULL
1894 : mtype->parameters()->copy()),
1895 (mtype->results() == NULL
1897 : mtype->results()->copy()),
1900 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1903 go_assert(p->is_field_name("tfn"));
1904 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1907 go_assert(p == fields->end());
1909 return Expression::make_struct_composite_literal(method_type, vals, bloc);
1912 // Return a composite literal for the type descriptor of a plain type.
1913 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1914 // NULL, it is the name to use as well as the list of methods.
1917 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1920 return this->type_descriptor_constructor(gogo, runtime_type_kind,
1924 // Return the type reflection string for this type.
1927 Type::reflection(Gogo* gogo) const
1931 // The do_reflection virtual function should set RET to the
1932 // reflection string.
1933 this->do_reflection(gogo, &ret);
1938 // Return a mangled name for the type.
1941 Type::mangled_name(Gogo* gogo) const
1945 // The do_mangled_name virtual function should set RET to the
1946 // mangled name. For a composite type it should append a code for
1947 // the composition and then call do_mangled_name on the components.
1948 this->do_mangled_name(gogo, &ret);
1953 // Return whether the backend size of the type is known.
1956 Type::is_backend_type_size_known(Gogo* gogo) const
1958 switch (this->classification_)
1972 case TYPE_INTERFACE:
1977 const Struct_field_list* fields = this->struct_type()->fields();
1978 for (Struct_field_list::const_iterator pf = fields->begin();
1979 pf != fields->end();
1981 if (!pf->type()->is_backend_type_size_known(gogo))
1988 const Array_type* at = this->array_type();
1989 if (at->length() == NULL)
1996 bool length_known = at->length()->integer_constant_value(true,
2002 return at->element_type()->is_backend_type_size_known(gogo);
2007 return this->named_type()->is_named_backend_type_size_known();
2011 const Forward_declaration_type* fdt = this->forward_declaration_type();
2012 return fdt->real_type()->is_backend_type_size_known(gogo);
2016 case TYPE_CALL_MULTIPLE_RESULT:
2024 // If the size of the type can be determined, set *PSIZE to the size
2025 // in bytes and return true. Otherwise, return false. This queries
2029 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2031 Btype* btype = this->get_backend(gogo);
2032 if (!this->is_backend_type_size_known(gogo))
2034 size_t size = gogo->backend()->type_size(btype);
2035 *psize = static_cast<unsigned int>(size);
2041 // If the alignment of the type can be determined, set *PALIGN to
2042 // the alignment in bytes and return true. Otherwise, return false.
2045 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2047 Btype* btype = this->get_backend(gogo);
2048 if (!this->is_backend_type_size_known(gogo))
2050 size_t align = gogo->backend()->type_alignment(btype);
2051 *palign = static_cast<unsigned int>(align);
2052 if (*palign != align)
2057 // Like backend_type_align, but return the alignment when used as a
2061 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2063 Btype* btype = this->get_backend(gogo);
2064 if (!this->is_backend_type_size_known(gogo))
2066 size_t a = gogo->backend()->type_field_alignment(btype);
2067 *palign = static_cast<unsigned int>(a);
2073 // Default function to export a type.
2076 Type::do_export(Export*) const
2084 Type::import_type(Import* imp)
2086 if (imp->match_c_string("("))
2087 return Function_type::do_import(imp);
2088 else if (imp->match_c_string("*"))
2089 return Pointer_type::do_import(imp);
2090 else if (imp->match_c_string("struct "))
2091 return Struct_type::do_import(imp);
2092 else if (imp->match_c_string("["))
2093 return Array_type::do_import(imp);
2094 else if (imp->match_c_string("map "))
2095 return Map_type::do_import(imp);
2096 else if (imp->match_c_string("chan "))
2097 return Channel_type::do_import(imp);
2098 else if (imp->match_c_string("interface"))
2099 return Interface_type::do_import(imp);
2102 error_at(imp->location(), "import error: expected type");
2103 return Type::make_error_type();
2107 // A type used to indicate a parsing error. This exists to simplify
2108 // later error detection.
2110 class Error_type : public Type
2119 do_compare_is_identity() const
2123 do_get_backend(Gogo* gogo)
2124 { return gogo->backend()->error_type(); }
2127 do_type_descriptor(Gogo*, Named_type*)
2128 { return Expression::make_error(Linemap::predeclared_location()); }
2131 do_reflection(Gogo*, std::string*) const
2132 { go_assert(saw_errors()); }
2135 do_mangled_name(Gogo*, std::string* ret) const
2136 { ret->push_back('E'); }
2140 Type::make_error_type()
2142 static Error_type singleton_error_type;
2143 return &singleton_error_type;
2148 class Void_type : public Type
2157 do_compare_is_identity() const
2161 do_get_backend(Gogo* gogo)
2162 { return gogo->backend()->void_type(); }
2165 do_type_descriptor(Gogo*, Named_type*)
2166 { go_unreachable(); }
2169 do_reflection(Gogo*, std::string*) const
2173 do_mangled_name(Gogo*, std::string* ret) const
2174 { ret->push_back('v'); }
2178 Type::make_void_type()
2180 static Void_type singleton_void_type;
2181 return &singleton_void_type;
2184 // The boolean type.
2186 class Boolean_type : public Type
2190 : Type(TYPE_BOOLEAN)
2195 do_compare_is_identity() const
2199 do_get_backend(Gogo* gogo)
2200 { return gogo->backend()->bool_type(); }
2203 do_type_descriptor(Gogo*, Named_type* name);
2205 // We should not be asked for the reflection string of a basic type.
2207 do_reflection(Gogo*, std::string* ret) const
2208 { ret->append("bool"); }
2211 do_mangled_name(Gogo*, std::string* ret) const
2212 { ret->push_back('b'); }
2215 // Make the type descriptor.
2218 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2221 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2224 Named_object* no = gogo->lookup_global("bool");
2225 go_assert(no != NULL);
2226 return Type::type_descriptor(gogo, no->type_value());
2231 Type::make_boolean_type()
2233 static Boolean_type boolean_type;
2234 return &boolean_type;
2237 // The named type "bool".
2239 static Named_type* named_bool_type;
2241 // Get the named type "bool".
2244 Type::lookup_bool_type()
2246 return named_bool_type;
2249 // Make the named type "bool".
2252 Type::make_named_bool_type()
2254 Type* bool_type = Type::make_boolean_type();
2255 Named_object* named_object =
2256 Named_object::make_type("bool", NULL, bool_type,
2257 Linemap::predeclared_location());
2258 Named_type* named_type = named_object->type_value();
2259 named_bool_type = named_type;
2263 // Class Integer_type.
2265 Integer_type::Named_integer_types Integer_type::named_integer_types;
2267 // Create a new integer type. Non-abstract integer types always have
2271 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2272 int bits, int runtime_type_kind)
2274 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2276 std::string sname(name);
2277 Named_object* named_object =
2278 Named_object::make_type(sname, NULL, integer_type,
2279 Linemap::predeclared_location());
2280 Named_type* named_type = named_object->type_value();
2281 std::pair<Named_integer_types::iterator, bool> ins =
2282 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2283 go_assert(ins.second);
2287 // Look up an existing integer type.
2290 Integer_type::lookup_integer_type(const char* name)
2292 Named_integer_types::const_iterator p =
2293 Integer_type::named_integer_types.find(name);
2294 go_assert(p != Integer_type::named_integer_types.end());
2298 // Create a new abstract integer type.
2301 Integer_type::create_abstract_integer_type()
2303 static Integer_type* abstract_type;
2304 if (abstract_type == NULL)
2305 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2306 RUNTIME_TYPE_KIND_INT);
2307 return abstract_type;
2310 // Integer type compatibility.
2313 Integer_type::is_identical(const Integer_type* t) const
2315 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2317 return this->is_abstract_ == t->is_abstract_;
2323 Integer_type::do_hash_for_method(Gogo*) const
2325 return ((this->bits_ << 4)
2326 + ((this->is_unsigned_ ? 1 : 0) << 8)
2327 + ((this->is_abstract_ ? 1 : 0) << 9));
2330 // Convert an Integer_type to the backend representation.
2333 Integer_type::do_get_backend(Gogo* gogo)
2335 if (this->is_abstract_)
2337 go_assert(saw_errors());
2338 return gogo->backend()->error_type();
2340 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2343 // The type descriptor for an integer type. Integer types are always
2347 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2349 go_assert(name != NULL);
2350 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2353 // We should not be asked for the reflection string of a basic type.
2356 Integer_type::do_reflection(Gogo*, std::string*) const
2358 go_assert(saw_errors());
2364 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2367 snprintf(buf, sizeof buf, "i%s%s%de",
2368 this->is_abstract_ ? "a" : "",
2369 this->is_unsigned_ ? "u" : "",
2374 // Make an integer type.
2377 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2378 int runtime_type_kind)
2380 return Integer_type::create_integer_type(name, is_unsigned, bits,
2384 // Make an abstract integer type.
2387 Type::make_abstract_integer_type()
2389 return Integer_type::create_abstract_integer_type();
2392 // Look up an integer type.
2395 Type::lookup_integer_type(const char* name)
2397 return Integer_type::lookup_integer_type(name);
2400 // Class Float_type.
2402 Float_type::Named_float_types Float_type::named_float_types;
2404 // Create a new float type. Non-abstract float types always have
2408 Float_type::create_float_type(const char* name, int bits,
2409 int runtime_type_kind)
2411 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2412 std::string sname(name);
2413 Named_object* named_object =
2414 Named_object::make_type(sname, NULL, float_type,
2415 Linemap::predeclared_location());
2416 Named_type* named_type = named_object->type_value();
2417 std::pair<Named_float_types::iterator, bool> ins =
2418 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2419 go_assert(ins.second);
2423 // Look up an existing float type.
2426 Float_type::lookup_float_type(const char* name)
2428 Named_float_types::const_iterator p =
2429 Float_type::named_float_types.find(name);
2430 go_assert(p != Float_type::named_float_types.end());
2434 // Create a new abstract float type.
2437 Float_type::create_abstract_float_type()
2439 static Float_type* abstract_type;
2440 if (abstract_type == NULL)
2441 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2442 return abstract_type;
2445 // Whether this type is identical with T.
2448 Float_type::is_identical(const Float_type* t) const
2450 if (this->bits_ != t->bits_)
2452 return this->is_abstract_ == t->is_abstract_;
2458 Float_type::do_hash_for_method(Gogo*) const
2460 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2463 // Convert to the backend representation.
2466 Float_type::do_get_backend(Gogo* gogo)
2468 return gogo->backend()->float_type(this->bits_);
2471 // The type descriptor for a float type. Float types are always named.
2474 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2476 go_assert(name != NULL);
2477 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2480 // We should not be asked for the reflection string of a basic type.
2483 Float_type::do_reflection(Gogo*, std::string*) const
2485 go_assert(saw_errors());
2491 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2494 snprintf(buf, sizeof buf, "f%s%de",
2495 this->is_abstract_ ? "a" : "",
2500 // Make a floating point type.
2503 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2505 return Float_type::create_float_type(name, bits, runtime_type_kind);
2508 // Make an abstract float type.
2511 Type::make_abstract_float_type()
2513 return Float_type::create_abstract_float_type();
2516 // Look up a float type.
2519 Type::lookup_float_type(const char* name)
2521 return Float_type::lookup_float_type(name);
2524 // Class Complex_type.
2526 Complex_type::Named_complex_types Complex_type::named_complex_types;
2528 // Create a new complex type. Non-abstract complex types always have
2532 Complex_type::create_complex_type(const char* name, int bits,
2533 int runtime_type_kind)
2535 Complex_type* complex_type = new Complex_type(false, bits,
2537 std::string sname(name);
2538 Named_object* named_object =
2539 Named_object::make_type(sname, NULL, complex_type,
2540 Linemap::predeclared_location());
2541 Named_type* named_type = named_object->type_value();
2542 std::pair<Named_complex_types::iterator, bool> ins =
2543 Complex_type::named_complex_types.insert(std::make_pair(sname,
2545 go_assert(ins.second);
2549 // Look up an existing complex type.
2552 Complex_type::lookup_complex_type(const char* name)
2554 Named_complex_types::const_iterator p =
2555 Complex_type::named_complex_types.find(name);
2556 go_assert(p != Complex_type::named_complex_types.end());
2560 // Create a new abstract complex type.
2563 Complex_type::create_abstract_complex_type()
2565 static Complex_type* abstract_type;
2566 if (abstract_type == NULL)
2567 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2568 return abstract_type;
2571 // Whether this type is identical with T.
2574 Complex_type::is_identical(const Complex_type *t) const
2576 if (this->bits_ != t->bits_)
2578 return this->is_abstract_ == t->is_abstract_;
2584 Complex_type::do_hash_for_method(Gogo*) const
2586 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2589 // Convert to the backend representation.
2592 Complex_type::do_get_backend(Gogo* gogo)
2594 return gogo->backend()->complex_type(this->bits_);
2597 // The type descriptor for a complex type. Complex types are always
2601 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2603 go_assert(name != NULL);
2604 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2607 // We should not be asked for the reflection string of a basic type.
2610 Complex_type::do_reflection(Gogo*, std::string*) const
2612 go_assert(saw_errors());
2618 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2621 snprintf(buf, sizeof buf, "c%s%de",
2622 this->is_abstract_ ? "a" : "",
2627 // Make a complex type.
2630 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2632 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2635 // Make an abstract complex type.
2638 Type::make_abstract_complex_type()
2640 return Complex_type::create_abstract_complex_type();
2643 // Look up a complex type.
2646 Type::lookup_complex_type(const char* name)
2648 return Complex_type::lookup_complex_type(name);
2651 // Class String_type.
2653 // Convert String_type to the backend representation. A string is a
2654 // struct with two fields: a pointer to the characters and a length.
2657 String_type::do_get_backend(Gogo* gogo)
2659 static Btype* backend_string_type;
2660 if (backend_string_type == NULL)
2662 std::vector<Backend::Btyped_identifier> fields(2);
2664 Type* b = gogo->lookup_global("byte")->type_value();
2665 Type* pb = Type::make_pointer_type(b);
2666 fields[0].name = "__data";
2667 fields[0].btype = pb->get_backend(gogo);
2668 fields[0].location = Linemap::predeclared_location();
2670 Type* int_type = Type::lookup_integer_type("int");
2671 fields[1].name = "__length";
2672 fields[1].btype = int_type->get_backend(gogo);
2673 fields[1].location = fields[0].location;
2675 backend_string_type = gogo->backend()->struct_type(fields);
2677 return backend_string_type;
2680 // Return a tree for the length of STRING.
2683 String_type::length_tree(Gogo*, tree string)
2685 tree string_type = TREE_TYPE(string);
2686 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2687 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2688 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2690 return fold_build3(COMPONENT_REF, integer_type_node, string,
2691 length_field, NULL_TREE);
2694 // Return a tree for a pointer to the bytes of STRING.
2697 String_type::bytes_tree(Gogo*, tree string)
2699 tree string_type = TREE_TYPE(string);
2700 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2701 tree bytes_field = TYPE_FIELDS(string_type);
2702 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2704 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2705 bytes_field, NULL_TREE);
2708 // The type descriptor for the string type.
2711 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2714 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2717 Named_object* no = gogo->lookup_global("string");
2718 go_assert(no != NULL);
2719 return Type::type_descriptor(gogo, no->type_value());
2723 // We should not be asked for the reflection string of a basic type.
2726 String_type::do_reflection(Gogo*, std::string* ret) const
2728 ret->append("string");
2731 // Mangled name of a string type.
2734 String_type::do_mangled_name(Gogo*, std::string* ret) const
2736 ret->push_back('z');
2739 // Make a string type.
2742 Type::make_string_type()
2744 static String_type string_type;
2745 return &string_type;
2748 // The named type "string".
2750 static Named_type* named_string_type;
2752 // Get the named type "string".
2755 Type::lookup_string_type()
2757 return named_string_type;
2760 // Make the named type string.
2763 Type::make_named_string_type()
2765 Type* string_type = Type::make_string_type();
2766 Named_object* named_object =
2767 Named_object::make_type("string", NULL, string_type,
2768 Linemap::predeclared_location());
2769 Named_type* named_type = named_object->type_value();
2770 named_string_type = named_type;
2774 // The sink type. This is the type of the blank identifier _. Any
2775 // type may be assigned to it.
2777 class Sink_type : public Type
2786 do_compare_is_identity() const
2790 do_get_backend(Gogo*)
2791 { go_unreachable(); }
2794 do_type_descriptor(Gogo*, Named_type*)
2795 { go_unreachable(); }
2798 do_reflection(Gogo*, std::string*) const
2799 { go_unreachable(); }
2802 do_mangled_name(Gogo*, std::string*) const
2803 { go_unreachable(); }
2806 // Make the sink type.
2809 Type::make_sink_type()
2811 static Sink_type sink_type;
2815 // Class Function_type.
2820 Function_type::do_traverse(Traverse* traverse)
2822 if (this->receiver_ != NULL
2823 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2824 return TRAVERSE_EXIT;
2825 if (this->parameters_ != NULL
2826 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2827 return TRAVERSE_EXIT;
2828 if (this->results_ != NULL
2829 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2830 return TRAVERSE_EXIT;
2831 return TRAVERSE_CONTINUE;
2834 // Returns whether T is a valid redeclaration of this type. If this
2835 // returns false, and REASON is not NULL, *REASON may be set to a
2836 // brief explanation of why it returned false.
2839 Function_type::is_valid_redeclaration(const Function_type* t,
2840 std::string* reason) const
2842 if (!this->is_identical(t, false, true, reason))
2845 // A redeclaration of a function is required to use the same names
2846 // for the receiver and parameters.
2847 if (this->receiver() != NULL
2848 && this->receiver()->name() != t->receiver()->name()
2849 && this->receiver()->name() != Import::import_marker
2850 && t->receiver()->name() != Import::import_marker)
2853 *reason = "receiver name changed";
2857 const Typed_identifier_list* parms1 = this->parameters();
2858 const Typed_identifier_list* parms2 = t->parameters();
2861 Typed_identifier_list::const_iterator p1 = parms1->begin();
2862 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2863 p2 != parms2->end();
2866 if (p1->name() != p2->name()
2867 && p1->name() != Import::import_marker
2868 && p2->name() != Import::import_marker)
2871 *reason = "parameter name changed";
2875 // This is called at parse time, so we may have unknown
2877 Type* t1 = p1->type()->forwarded();
2878 Type* t2 = p2->type()->forwarded();
2880 && t1->forward_declaration_type() != NULL
2881 && (t2->forward_declaration_type() == NULL
2882 || (t1->forward_declaration_type()->named_object()
2883 != t2->forward_declaration_type()->named_object())))
2888 const Typed_identifier_list* results1 = this->results();
2889 const Typed_identifier_list* results2 = t->results();
2890 if (results1 != NULL)
2892 Typed_identifier_list::const_iterator res1 = results1->begin();
2893 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2894 res2 != results2->end();
2897 if (res1->name() != res2->name()
2898 && res1->name() != Import::import_marker
2899 && res2->name() != Import::import_marker)
2902 *reason = "result name changed";
2906 // This is called at parse time, so we may have unknown
2908 Type* t1 = res1->type()->forwarded();
2909 Type* t2 = res2->type()->forwarded();
2911 && t1->forward_declaration_type() != NULL
2912 && (t2->forward_declaration_type() == NULL
2913 || (t1->forward_declaration_type()->named_object()
2914 != t2->forward_declaration_type()->named_object())))
2922 // Check whether T is the same as this type.
2925 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2926 bool errors_are_identical,
2927 std::string* reason) const
2929 if (!ignore_receiver)
2931 const Typed_identifier* r1 = this->receiver();
2932 const Typed_identifier* r2 = t->receiver();
2933 if ((r1 != NULL) != (r2 != NULL))
2936 *reason = _("different receiver types");
2941 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2944 if (reason != NULL && !reason->empty())
2945 *reason = "receiver: " + *reason;
2951 const Typed_identifier_list* parms1 = this->parameters();
2952 const Typed_identifier_list* parms2 = t->parameters();
2953 if ((parms1 != NULL) != (parms2 != NULL))
2956 *reason = _("different number of parameters");
2961 Typed_identifier_list::const_iterator p1 = parms1->begin();
2962 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2963 p2 != parms2->end();
2966 if (p1 == parms1->end())
2969 *reason = _("different number of parameters");
2973 if (!Type::are_identical(p1->type(), p2->type(),
2974 errors_are_identical, NULL))
2977 *reason = _("different parameter types");
2981 if (p1 != parms1->end())
2984 *reason = _("different number of parameters");
2989 if (this->is_varargs() != t->is_varargs())
2992 *reason = _("different varargs");
2996 const Typed_identifier_list* results1 = this->results();
2997 const Typed_identifier_list* results2 = t->results();
2998 if ((results1 != NULL) != (results2 != NULL))
3001 *reason = _("different number of results");
3004 if (results1 != NULL)
3006 Typed_identifier_list::const_iterator res1 = results1->begin();
3007 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3008 res2 != results2->end();
3011 if (res1 == results1->end())
3014 *reason = _("different number of results");
3018 if (!Type::are_identical(res1->type(), res2->type(),
3019 errors_are_identical, NULL))
3022 *reason = _("different result types");
3026 if (res1 != results1->end())
3029 *reason = _("different number of results");
3040 Function_type::do_hash_for_method(Gogo* gogo) const
3042 unsigned int ret = 0;
3043 // We ignore the receiver type for hash codes, because we need to
3044 // get the same hash code for a method in an interface and a method
3045 // declared for a type. The former will not have a receiver.
3046 if (this->parameters_ != NULL)
3049 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3050 p != this->parameters_->end();
3052 ret += p->type()->hash_for_method(gogo) << shift;
3054 if (this->results_ != NULL)
3057 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3058 p != this->results_->end();
3060 ret += p->type()->hash_for_method(gogo) << shift;
3062 if (this->is_varargs_)
3068 // Get the backend representation for a function type.
3071 Function_type::get_function_backend(Gogo* gogo)
3073 Backend::Btyped_identifier breceiver;
3074 if (this->receiver_ != NULL)
3076 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3078 // We always pass the address of the receiver parameter, in
3079 // order to make interface calls work with unknown types.
3080 Type* rtype = this->receiver_->type();
3081 if (rtype->points_to() == NULL)
3082 rtype = Type::make_pointer_type(rtype);
3083 breceiver.btype = rtype->get_backend(gogo);
3084 breceiver.location = this->receiver_->location();
3087 std::vector<Backend::Btyped_identifier> bparameters;
3088 if (this->parameters_ != NULL)
3090 bparameters.resize(this->parameters_->size());
3092 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3093 p != this->parameters_->end();
3096 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3097 bparameters[i].btype = p->type()->get_backend(gogo);
3098 bparameters[i].location = p->location();
3100 go_assert(i == bparameters.size());
3103 std::vector<Backend::Btyped_identifier> bresults;
3104 if (this->results_ != NULL)
3106 bresults.resize(this->results_->size());
3108 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3109 p != this->results_->end();
3112 bresults[i].name = Gogo::unpack_hidden_name(p->name());
3113 bresults[i].btype = p->type()->get_backend(gogo);
3114 bresults[i].location = p->location();
3116 go_assert(i == bresults.size());
3119 return gogo->backend()->function_type(breceiver, bparameters, bresults,
3123 // A hash table mapping function types to their backend placeholders.
3125 Function_type::Placeholders Function_type::placeholders;
3127 // Get the backend representation for a function type. If we are
3128 // still converting types, and this types has multiple results, return
3129 // a placeholder instead. We do this because for multiple results we
3130 // build a struct, and we need to make sure that all the types in the
3131 // struct are valid before we create the struct.
3134 Function_type::do_get_backend(Gogo* gogo)
3136 if (!gogo->named_types_are_converted()
3137 && this->results_ != NULL
3138 && this->results_->size() > 1)
3140 Btype* placeholder =
3141 gogo->backend()->placeholder_pointer_type("", this->location(), true);
3142 Function_type::placeholders.push_back(std::make_pair(this, placeholder));
3145 return this->get_function_backend(gogo);
3148 // Convert function types after all named types are converted.
3151 Function_type::convert_types(Gogo* gogo)
3153 for (Placeholders::const_iterator p = Function_type::placeholders.begin();
3154 p != Function_type::placeholders.end();
3157 Btype* bt = p->first->get_function_backend(gogo);
3158 if (!gogo->backend()->set_placeholder_function_type(p->second, bt))
3159 go_assert(saw_errors());
3163 // The type of a function type descriptor.
3166 Function_type::make_function_type_descriptor_type()
3171 Type* tdt = Type::make_type_descriptor_type();
3172 Type* ptdt = Type::make_type_descriptor_ptr_type();
3174 Type* bool_type = Type::lookup_bool_type();
3176 Type* slice_type = Type::make_array_type(ptdt, NULL);
3178 Struct_type* s = Type::make_builtin_struct_type(4,
3180 "dotdotdot", bool_type,
3184 ret = Type::make_builtin_named_type("FuncType", s);
3190 // The type descriptor for a function type.
3193 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3195 Location bloc = Linemap::predeclared_location();
3197 Type* ftdt = Function_type::make_function_type_descriptor_type();
3199 const Struct_field_list* fields = ftdt->struct_type()->fields();
3201 Expression_list* vals = new Expression_list();
3204 Struct_field_list::const_iterator p = fields->begin();
3205 go_assert(p->is_field_name("commonType"));
3206 vals->push_back(this->type_descriptor_constructor(gogo,
3207 RUNTIME_TYPE_KIND_FUNC,
3211 go_assert(p->is_field_name("dotdotdot"));
3212 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3215 go_assert(p->is_field_name("in"));
3216 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3217 this->parameters()));
3220 go_assert(p->is_field_name("out"));
3221 vals->push_back(this->type_descriptor_params(p->type(), NULL,
3225 go_assert(p == fields->end());
3227 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3230 // Return a composite literal for the parameters or results of a type
3234 Function_type::type_descriptor_params(Type* params_type,
3235 const Typed_identifier* receiver,
3236 const Typed_identifier_list* params)
3238 Location bloc = Linemap::predeclared_location();
3240 if (receiver == NULL && params == NULL)
3241 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3243 Expression_list* vals = new Expression_list();
3244 vals->reserve((params == NULL ? 0 : params->size())
3245 + (receiver != NULL ? 1 : 0));
3247 if (receiver != NULL)
3248 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3252 for (Typed_identifier_list::const_iterator p = params->begin();
3255 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3258 return Expression::make_slice_composite_literal(params_type, vals, bloc);
3261 // The reflection string.
3264 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3266 // FIXME: Turn this off until we straighten out the type of the
3267 // struct field used in a go statement which calls a method.
3268 // go_assert(this->receiver_ == NULL);
3270 ret->append("func");
3272 if (this->receiver_ != NULL)
3274 ret->push_back('(');
3275 this->append_reflection(this->receiver_->type(), gogo, ret);
3276 ret->push_back(')');
3279 ret->push_back('(');
3280 const Typed_identifier_list* params = this->parameters();
3283 bool is_varargs = this->is_varargs_;
3284 for (Typed_identifier_list::const_iterator p = params->begin();
3288 if (p != params->begin())
3290 if (!is_varargs || p + 1 != params->end())
3291 this->append_reflection(p->type(), gogo, ret);
3295 this->append_reflection(p->type()->array_type()->element_type(),
3300 ret->push_back(')');
3302 const Typed_identifier_list* results = this->results();
3303 if (results != NULL && !results->empty())
3305 if (results->size() == 1)
3306 ret->push_back(' ');
3309 for (Typed_identifier_list::const_iterator p = results->begin();
3310 p != results->end();
3313 if (p != results->begin())
3315 this->append_reflection(p->type(), gogo, ret);
3317 if (results->size() > 1)
3318 ret->push_back(')');
3325 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3327 ret->push_back('F');
3329 if (this->receiver_ != NULL)
3331 ret->push_back('m');
3332 this->append_mangled_name(this->receiver_->type(), gogo, ret);
3335 const Typed_identifier_list* params = this->parameters();
3338 ret->push_back('p');
3339 for (Typed_identifier_list::const_iterator p = params->begin();
3342 this->append_mangled_name(p->type(), gogo, ret);
3343 if (this->is_varargs_)
3344 ret->push_back('V');
3345 ret->push_back('e');
3348 const Typed_identifier_list* results = this->results();
3349 if (results != NULL)
3351 ret->push_back('r');
3352 for (Typed_identifier_list::const_iterator p = results->begin();
3353 p != results->end();
3355 this->append_mangled_name(p->type(), gogo, ret);
3356 ret->push_back('e');
3359 ret->push_back('e');
3362 // Export a function type.
3365 Function_type::do_export(Export* exp) const
3367 // We don't write out the receiver. The only function types which
3368 // should have a receiver are the ones associated with explicitly
3369 // defined methods. For those the receiver type is written out by
3370 // Function::export_func.
3372 exp->write_c_string("(");
3374 if (this->parameters_ != NULL)
3376 bool is_varargs = this->is_varargs_;
3377 for (Typed_identifier_list::const_iterator p =
3378 this->parameters_->begin();
3379 p != this->parameters_->end();
3385 exp->write_c_string(", ");
3386 if (!is_varargs || p + 1 != this->parameters_->end())
3387 exp->write_type(p->type());
3390 exp->write_c_string("...");
3391 exp->write_type(p->type()->array_type()->element_type());
3395 exp->write_c_string(")");
3397 const Typed_identifier_list* results = this->results_;
3398 if (results != NULL)
3400 exp->write_c_string(" ");
3401 if (results->size() == 1)
3402 exp->write_type(results->begin()->type());
3406 exp->write_c_string("(");
3407 for (Typed_identifier_list::const_iterator p = results->begin();
3408 p != results->end();
3414 exp->write_c_string(", ");
3415 exp->write_type(p->type());
3417 exp->write_c_string(")");
3422 // Import a function type.
3425 Function_type::do_import(Import* imp)
3427 imp->require_c_string("(");
3428 Typed_identifier_list* parameters;
3429 bool is_varargs = false;
3430 if (imp->peek_char() == ')')
3434 parameters = new Typed_identifier_list();
3437 if (imp->match_c_string("..."))
3443 Type* ptype = imp->read_type();
3445 ptype = Type::make_array_type(ptype, NULL);
3446 parameters->push_back(Typed_identifier(Import::import_marker,
3447 ptype, imp->location()));
3448 if (imp->peek_char() != ',')
3450 go_assert(!is_varargs);
3451 imp->require_c_string(", ");
3454 imp->require_c_string(")");
3456 Typed_identifier_list* results;
3457 if (imp->peek_char() != ' ')
3462 results = new Typed_identifier_list;
3463 if (imp->peek_char() != '(')
3465 Type* rtype = imp->read_type();
3466 results->push_back(Typed_identifier(Import::import_marker, rtype,
3474 Type* rtype = imp->read_type();
3475 results->push_back(Typed_identifier(Import::import_marker,
3476 rtype, imp->location()));
3477 if (imp->peek_char() != ',')
3479 imp->require_c_string(", ");
3481 imp->require_c_string(")");
3485 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3488 ret->set_is_varargs();
3492 // Make a copy of a function type without a receiver.
3495 Function_type::copy_without_receiver() const
3497 go_assert(this->is_method());
3498 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3501 if (this->is_varargs())
3502 ret->set_is_varargs();
3503 if (this->is_builtin())
3504 ret->set_is_builtin();
3508 // Make a copy of a function type with a receiver.
3511 Function_type::copy_with_receiver(Type* receiver_type) const
3513 go_assert(!this->is_method());
3514 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3516 return Type::make_function_type(receiver, this->parameters_,
3517 this->results_, this->location_);
3520 // Make a function type.
3523 Type::make_function_type(Typed_identifier* receiver,
3524 Typed_identifier_list* parameters,
3525 Typed_identifier_list* results,
3528 return new Function_type(receiver, parameters, results, location);
3531 // Class Pointer_type.
3536 Pointer_type::do_traverse(Traverse* traverse)
3538 return Type::traverse(this->to_type_, traverse);
3544 Pointer_type::do_hash_for_method(Gogo* gogo) const
3546 return this->to_type_->hash_for_method(gogo) << 4;
3549 // The tree for a pointer type.
3552 Pointer_type::do_get_backend(Gogo* gogo)
3554 Btype* to_btype = this->to_type_->get_backend(gogo);
3555 return gogo->backend()->pointer_type(to_btype);
3558 // The type of a pointer type descriptor.
3561 Pointer_type::make_pointer_type_descriptor_type()
3566 Type* tdt = Type::make_type_descriptor_type();
3567 Type* ptdt = Type::make_type_descriptor_ptr_type();
3569 Struct_type* s = Type::make_builtin_struct_type(2,
3573 ret = Type::make_builtin_named_type("PtrType", s);
3579 // The type descriptor for a pointer type.
3582 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3584 if (this->is_unsafe_pointer_type())
3586 go_assert(name != NULL);
3587 return this->plain_type_descriptor(gogo,
3588 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3593 Location bloc = Linemap::predeclared_location();
3595 const Methods* methods;
3596 Type* deref = this->points_to();
3597 if (deref->named_type() != NULL)
3598 methods = deref->named_type()->methods();
3599 else if (deref->struct_type() != NULL)
3600 methods = deref->struct_type()->methods();
3604 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3606 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3608 Expression_list* vals = new Expression_list();
3611 Struct_field_list::const_iterator p = fields->begin();
3612 go_assert(p->is_field_name("commonType"));
3613 vals->push_back(this->type_descriptor_constructor(gogo,
3614 RUNTIME_TYPE_KIND_PTR,
3615 name, methods, false));
3618 go_assert(p->is_field_name("elem"));
3619 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3621 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3625 // Reflection string.
3628 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3630 ret->push_back('*');
3631 this->append_reflection(this->to_type_, gogo, ret);
3637 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3639 ret->push_back('p');
3640 this->append_mangled_name(this->to_type_, gogo, ret);
3646 Pointer_type::do_export(Export* exp) const
3648 exp->write_c_string("*");
3649 if (this->is_unsafe_pointer_type())
3650 exp->write_c_string("any");
3652 exp->write_type(this->to_type_);
3658 Pointer_type::do_import(Import* imp)
3660 imp->require_c_string("*");
3661 if (imp->match_c_string("any"))
3664 return Type::make_pointer_type(Type::make_void_type());
3666 Type* to = imp->read_type();
3667 return Type::make_pointer_type(to);
3670 // Make a pointer type.
3673 Type::make_pointer_type(Type* to_type)
3675 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3676 static Hashtable pointer_types;
3677 Hashtable::const_iterator p = pointer_types.find(to_type);
3678 if (p != pointer_types.end())
3680 Pointer_type* ret = new Pointer_type(to_type);
3681 pointer_types[to_type] = ret;
3685 // The nil type. We use a special type for nil because it is not the
3686 // same as any other type. In C term nil has type void*, but there is
3687 // no such type in Go.
3689 class Nil_type : public Type
3698 do_compare_is_identity() const
3702 do_get_backend(Gogo* gogo)
3703 { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3706 do_type_descriptor(Gogo*, Named_type*)
3707 { go_unreachable(); }
3710 do_reflection(Gogo*, std::string*) const
3711 { go_unreachable(); }
3714 do_mangled_name(Gogo*, std::string* ret) const
3715 { ret->push_back('n'); }
3718 // Make the nil type.
3721 Type::make_nil_type()
3723 static Nil_type singleton_nil_type;
3724 return &singleton_nil_type;
3727 // The type of a function call which returns multiple values. This is
3728 // really a struct, but we don't want to confuse a function call which
3729 // returns a struct with a function call which returns multiple
3732 class Call_multiple_result_type : public Type
3735 Call_multiple_result_type(Call_expression* call)
3736 : Type(TYPE_CALL_MULTIPLE_RESULT),
3742 do_has_pointer() const
3744 go_assert(saw_errors());
3749 do_compare_is_identity() const
3753 do_get_backend(Gogo* gogo)
3755 go_assert(saw_errors());
3756 return gogo->backend()->error_type();
3760 do_type_descriptor(Gogo*, Named_type*)
3762 go_assert(saw_errors());
3763 return Expression::make_error(Linemap::unknown_location());
3767 do_reflection(Gogo*, std::string*) const
3768 { go_assert(saw_errors()); }
3771 do_mangled_name(Gogo*, std::string*) const
3772 { go_assert(saw_errors()); }
3775 // The expression being called.
3776 Call_expression* call_;
3779 // Make a call result type.
3782 Type::make_call_multiple_result_type(Call_expression* call)
3784 return new Call_multiple_result_type(call);
3787 // Class Struct_field.
3789 // Get the name of a field.
3792 Struct_field::field_name() const
3794 const std::string& name(this->typed_identifier_.name());
3799 // This is called during parsing, before anything is lowered, so
3800 // we have to be pretty careful to avoid dereferencing an
3801 // unknown type name.
3802 Type* t = this->typed_identifier_.type();
3804 if (t->classification() == Type::TYPE_POINTER)
3807 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3808 dt = ptype->points_to();
3810 if (dt->forward_declaration_type() != NULL)
3811 return dt->forward_declaration_type()->name();
3812 else if (dt->named_type() != NULL)
3813 return dt->named_type()->name();
3814 else if (t->is_error_type() || dt->is_error_type())
3816 static const std::string error_string = "*error*";
3817 return error_string;
3821 // Avoid crashing in the erroneous case where T is named but
3824 if (t->forward_declaration_type() != NULL)
3825 return t->forward_declaration_type()->name();
3826 else if (t->named_type() != NULL)
3827 return t->named_type()->name();
3834 // Return whether this field is named NAME.
3837 Struct_field::is_field_name(const std::string& name) const
3839 const std::string& me(this->typed_identifier_.name());
3844 Type* t = this->typed_identifier_.type();
3845 if (t->points_to() != NULL)
3847 Named_type* nt = t->named_type();
3848 if (nt != NULL && nt->name() == name)
3851 // This is a horrible hack caused by the fact that we don't pack
3852 // the names of builtin types. FIXME.
3855 && nt->name() == Gogo::unpack_hidden_name(name))
3862 // Class Struct_type.
3867 Struct_type::do_traverse(Traverse* traverse)
3869 Struct_field_list* fields = this->fields_;
3872 for (Struct_field_list::iterator p = fields->begin();
3876 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3877 return TRAVERSE_EXIT;
3880 return TRAVERSE_CONTINUE;
3883 // Verify that the struct type is complete and valid.
3886 Struct_type::do_verify()
3888 Struct_field_list* fields = this->fields_;
3892 for (Struct_field_list::iterator p = fields->begin();
3896 Type* t = p->type();
3897 if (t->is_undefined())
3899 error_at(p->location(), "struct field type is incomplete");
3900 p->set_type(Type::make_error_type());
3903 else if (p->is_anonymous())
3905 if (t->named_type() != NULL && t->points_to() != NULL)
3907 error_at(p->location(), "embedded type may not be a pointer");
3908 p->set_type(Type::make_error_type());
3911 if (t->points_to() != NULL
3912 && t->points_to()->interface_type() != NULL)
3914 error_at(p->location(),
3915 "embedded type may not be pointer to interface");
3916 p->set_type(Type::make_error_type());
3924 // Whether this contains a pointer.
3927 Struct_type::do_has_pointer() const
3929 const Struct_field_list* fields = this->fields();
3932 for (Struct_field_list::const_iterator p = fields->begin();
3936 if (p->type()->has_pointer())
3942 // Whether this type is identical to T.
3945 Struct_type::is_identical(const Struct_type* t,
3946 bool errors_are_identical) const
3948 const Struct_field_list* fields1 = this->fields();
3949 const Struct_field_list* fields2 = t->fields();
3950 if (fields1 == NULL || fields2 == NULL)
3951 return fields1 == fields2;
3952 Struct_field_list::const_iterator pf2 = fields2->begin();
3953 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3954 pf1 != fields1->end();
3957 if (pf2 == fields2->end())
3959 if (pf1->field_name() != pf2->field_name())
3961 if (pf1->is_anonymous() != pf2->is_anonymous()
3962 || !Type::are_identical(pf1->type(), pf2->type(),
3963 errors_are_identical, NULL))
3965 if (!pf1->has_tag())
3972 if (!pf2->has_tag())
3974 if (pf1->tag() != pf2->tag())
3978 if (pf2 != fields2->end())
3983 // Whether this struct type has any hidden fields.
3986 Struct_type::struct_has_hidden_fields(const Named_type* within,
3987 std::string* reason) const
3989 const Struct_field_list* fields = this->fields();
3992 const Package* within_package = (within == NULL
3994 : within->named_object()->package());
3995 for (Struct_field_list::const_iterator pf = fields->begin();
3996 pf != fields->end();
3999 if (within_package != NULL
4000 && !pf->is_anonymous()
4001 && Gogo::is_hidden_name(pf->field_name()))
4005 std::string within_name = within->named_object()->message_name();
4006 std::string name = Gogo::message_name(pf->field_name());
4007 size_t bufsize = 200 + within_name.length() + name.length();
4008 char* buf = new char[bufsize];
4009 snprintf(buf, bufsize,
4010 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4011 open_quote, within_name.c_str(), close_quote,
4012 open_quote, name.c_str(), close_quote);
4013 reason->assign(buf);
4019 if (pf->type()->has_hidden_fields(within, reason))
4026 // Whether comparisons of this struct type are simple identity
4030 Struct_type::do_compare_is_identity() const
4032 const Struct_field_list* fields = this->fields_;
4035 for (Struct_field_list::const_iterator pf = fields->begin();
4036 pf != fields->end();
4038 if (!pf->type()->compare_is_identity())
4043 // Build identity and hash functions for this struct.
4048 Struct_type::do_hash_for_method(Gogo* gogo) const
4050 unsigned int ret = 0;
4051 if (this->fields() != NULL)
4053 for (Struct_field_list::const_iterator pf = this->fields()->begin();
4054 pf != this->fields()->end();
4056 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4061 // Find the local field NAME.
4064 Struct_type::find_local_field(const std::string& name,
4065 unsigned int *pindex) const
4067 const Struct_field_list* fields = this->fields_;
4071 for (Struct_field_list::const_iterator pf = fields->begin();
4072 pf != fields->end();
4075 if (pf->is_field_name(name))
4085 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4087 Field_reference_expression*
4088 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4089 Location location) const
4092 return this->field_reference_depth(struct_expr, name, location, NULL,
4096 // Return an expression for a field, along with the depth at which it
4099 Field_reference_expression*
4100 Struct_type::field_reference_depth(Expression* struct_expr,
4101 const std::string& name,
4103 Saw_named_type* saw,
4104 unsigned int* depth) const
4106 const Struct_field_list* fields = this->fields_;
4110 // Look for a field with this name.
4112 for (Struct_field_list::const_iterator pf = fields->begin();
4113 pf != fields->end();
4116 if (pf->is_field_name(name))
4119 return Expression::make_field_reference(struct_expr, i, location);
4123 // Look for an anonymous field which contains a field with this
4125 unsigned int found_depth = 0;
4126 Field_reference_expression* ret = NULL;
4128 for (Struct_field_list::const_iterator pf = fields->begin();
4129 pf != fields->end();
4132 if (!pf->is_anonymous())
4135 Struct_type* st = pf->type()->deref()->struct_type();
4139 Saw_named_type* hold_saw = saw;
4140 Saw_named_type saw_here;
4141 Named_type* nt = pf->type()->named_type();
4143 nt = pf->type()->deref()->named_type();
4147 for (q = saw; q != NULL; q = q->next)
4151 // If this is an error, it will be reported
4158 saw_here.next = saw;
4163 // Look for a reference using a NULL struct expression. If we
4164 // find one, fill in the struct expression with a reference to
4166 unsigned int subdepth;
4167 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
4177 if (ret == NULL || subdepth < found_depth)
4182 found_depth = subdepth;
4183 Expression* here = Expression::make_field_reference(struct_expr, i,
4185 if (pf->type()->points_to() != NULL)
4186 here = Expression::make_unary(OPERATOR_MULT, here, location);
4187 while (sub->expr() != NULL)
4189 sub = sub->expr()->deref()->field_reference_expression();
4190 go_assert(sub != NULL);
4192 sub->set_struct_expression(here);
4194 else if (subdepth > found_depth)
4198 // We do not handle ambiguity here--it should be handled by
4199 // Type::bind_field_or_method.
4207 *depth = found_depth + 1;
4212 // Return the total number of fields, including embedded fields.
4215 Struct_type::total_field_count() const
4217 if (this->fields_ == NULL)
4219 unsigned int ret = 0;
4220 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4221 pf != this->fields_->end();
4224 if (!pf->is_anonymous() || pf->type()->deref()->struct_type() == NULL)
4227 ret += pf->type()->struct_type()->total_field_count();
4232 // Return whether NAME is an unexported field, for better error reporting.
4235 Struct_type::is_unexported_local_field(Gogo* gogo,
4236 const std::string& name) const
4238 const Struct_field_list* fields = this->fields_;
4241 for (Struct_field_list::const_iterator pf = fields->begin();
4242 pf != fields->end();
4245 const std::string& field_name(pf->field_name());
4246 if (Gogo::is_hidden_name(field_name)
4247 && name == Gogo::unpack_hidden_name(field_name)
4248 && gogo->pack_hidden_name(name, false) != field_name)
4255 // Finalize the methods of an unnamed struct.
4258 Struct_type::finalize_methods(Gogo* gogo)
4260 // If this type needs explicit comparison and hash functions, create
4261 // them now. It would be a bit better to do this only if the
4262 // functions are needed, but they will be static so the backend can
4263 // discard them if they are not used.
4264 if (!this->compare_is_identity() && this->is_comparable())
4266 Named_object* hash_fn;
4267 Named_object* equal_fn;
4268 this->type_functions(gogo, NULL, NULL, NULL, &hash_fn, &equal_fn);
4271 if (this->all_methods_ != NULL)
4273 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
4276 // Return the method NAME, or NULL if there isn't one or if it is
4277 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
4281 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
4283 return Type::method_function(this->all_methods_, name, is_ambiguous);
4286 // Convert struct fields to the backend representation. This is not
4287 // declared in types.h so that types.h doesn't have to #include
4291 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
4292 std::vector<Backend::Btyped_identifier>* bfields)
4294 bfields->resize(fields->size());
4296 for (Struct_field_list::const_iterator p = fields->begin();
4300 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
4301 (*bfields)[i].btype = p->type()->get_backend(gogo);
4302 (*bfields)[i].location = p->location();
4304 go_assert(i == fields->size());
4307 // Get the tree for a struct type.
4310 Struct_type::do_get_backend(Gogo* gogo)
4312 std::vector<Backend::Btyped_identifier> bfields;
4313 get_backend_struct_fields(gogo, this->fields_, &bfields);
4314 return gogo->backend()->struct_type(bfields);
4317 // The type of a struct type descriptor.
4320 Struct_type::make_struct_type_descriptor_type()
4325 Type* tdt = Type::make_type_descriptor_type();
4326 Type* ptdt = Type::make_type_descriptor_ptr_type();
4328 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4329 Type* string_type = Type::lookup_string_type();
4330 Type* pointer_string_type = Type::make_pointer_type(string_type);
4333 Type::make_builtin_struct_type(5,
4334 "name", pointer_string_type,
4335 "pkgPath", pointer_string_type,
4337 "tag", pointer_string_type,
4338 "offset", uintptr_type);
4339 Type* nsf = Type::make_builtin_named_type("structField", sf);
4341 Type* slice_type = Type::make_array_type(nsf, NULL);
4343 Struct_type* s = Type::make_builtin_struct_type(2,
4345 "fields", slice_type);
4347 ret = Type::make_builtin_named_type("StructType", s);
4353 // Build a type descriptor for a struct type.
4356 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4358 Location bloc = Linemap::predeclared_location();
4360 Type* stdt = Struct_type::make_struct_type_descriptor_type();
4362 const Struct_field_list* fields = stdt->struct_type()->fields();
4364 Expression_list* vals = new Expression_list();
4367 const Methods* methods = this->methods();
4368 // A named struct should not have methods--the methods should attach
4369 // to the named type.
4370 go_assert(methods == NULL || name == NULL);
4372 Struct_field_list::const_iterator ps = fields->begin();
4373 go_assert(ps->is_field_name("commonType"));
4374 vals->push_back(this->type_descriptor_constructor(gogo,
4375 RUNTIME_TYPE_KIND_STRUCT,
4376 name, methods, true));
4379 go_assert(ps->is_field_name("fields"));
4381 Expression_list* elements = new Expression_list();
4382 elements->reserve(this->fields_->size());
4383 Type* element_type = ps->type()->array_type()->element_type();
4384 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4385 pf != this->fields_->end();
4388 const Struct_field_list* f = element_type->struct_type()->fields();
4390 Expression_list* fvals = new Expression_list();
4393 Struct_field_list::const_iterator q = f->begin();
4394 go_assert(q->is_field_name("name"));
4395 if (pf->is_anonymous())
4396 fvals->push_back(Expression::make_nil(bloc));
4399 std::string n = Gogo::unpack_hidden_name(pf->field_name());
4400 Expression* s = Expression::make_string(n, bloc);
4401 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4405 go_assert(q->is_field_name("pkgPath"));
4406 if (!Gogo::is_hidden_name(pf->field_name()))
4407 fvals->push_back(Expression::make_nil(bloc));
4410 std::string n = Gogo::hidden_name_prefix(pf->field_name());
4411 Expression* s = Expression::make_string(n, bloc);
4412 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4416 go_assert(q->is_field_name("typ"));
4417 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
4420 go_assert(q->is_field_name("tag"));
4422 fvals->push_back(Expression::make_nil(bloc));
4425 Expression* s = Expression::make_string(pf->tag(), bloc);
4426 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4430 go_assert(q->is_field_name("offset"));
4431 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
4433 Expression* v = Expression::make_struct_composite_literal(element_type,
4435 elements->push_back(v);
4438 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
4441 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4444 // Write the hash function for a struct which can not use the identity
4448 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
4449 Function_type* hash_fntype,
4450 Function_type* equal_fntype)
4452 Location bloc = Linemap::predeclared_location();
4454 // The pointer to the struct that we are going to hash. This is an
4455 // argument to the hash function we are implementing here.
4456 Named_object* key_arg = gogo->lookup("key", NULL);
4457 go_assert(key_arg != NULL);
4458 Type* key_arg_type = key_arg->var_value()->type();
4460 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4464 mpz_init_set_ui(ival, 0);
4465 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
4468 // Make a temporary to hold the return value, initialized to 0.
4469 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
4471 gogo->add_statement(retval);
4473 // Make a temporary to hold the key as a uintptr.
4474 Expression* ref = Expression::make_var_reference(key_arg, bloc);
4475 ref = Expression::make_cast(uintptr_type, ref, bloc);
4476 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
4478 gogo->add_statement(key);
4480 // Loop over the struct fields.
4482 const Struct_field_list* fields = this->fields_;
4483 for (Struct_field_list::const_iterator pf = fields->begin();
4484 pf != fields->end();
4491 // Multiply retval by 33.
4492 mpz_init_set_ui(ival, 33);
4493 Expression* i33 = Expression::make_integer(&ival, uintptr_type,
4497 ref = Expression::make_temporary_reference(retval, bloc);
4498 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ,
4500 gogo->add_statement(s);
4503 // Get a pointer to the value of this field.
4504 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
4505 ref = Expression::make_temporary_reference(key, bloc);
4506 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
4508 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
4510 // Get the size of this field.
4511 Expression* size = Expression::make_type_info(pf->type(),
4512 Expression::TYPE_INFO_SIZE);
4514 // Get the hash function to use for the type of this field.
4515 Named_object* hash_fn;
4516 Named_object* equal_fn;
4517 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
4518 equal_fntype, &hash_fn, &equal_fn);
4520 // Call the hash function for the field.
4521 Expression_list* args = new Expression_list();
4522 args->push_back(subkey);
4523 args->push_back(size);
4524 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
4525 Expression* call = Expression::make_call(func, args, false, bloc);
4527 // Add the field's hash value to retval.
4528 Temporary_reference_expression* tref =
4529 Expression::make_temporary_reference(retval, bloc);
4530 tref->set_is_lvalue();
4531 Statement* s = Statement::make_assignment_operation(OPERATOR_PLUSEQ,
4533 gogo->add_statement(s);
4536 // Return retval to the caller of the hash function.
4537 Expression_list* vals = new Expression_list();
4538 ref = Expression::make_temporary_reference(retval, bloc);
4539 vals->push_back(ref);
4540 Statement* s = Statement::make_return_statement(vals, bloc);
4541 gogo->add_statement(s);
4544 // Write the equality function for a struct which can not use the
4545 // identity function.
4548 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
4550 Location bloc = Linemap::predeclared_location();
4552 // The pointers to the structs we are going to compare.
4553 Named_object* key1_arg = gogo->lookup("key1", NULL);
4554 Named_object* key2_arg = gogo->lookup("key2", NULL);
4555 go_assert(key1_arg != NULL && key2_arg != NULL);
4557 // Build temporaries with the right types.
4558 Type* pt = Type::make_pointer_type(name != NULL
4559 ? static_cast<Type*>(name)
4560 : static_cast<Type*>(this));
4562 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
4563 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4564 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
4565 gogo->add_statement(p1);
4567 ref = Expression::make_var_reference(key2_arg, bloc);
4568 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4569 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
4570 gogo->add_statement(p2);
4572 const Struct_field_list* fields = this->fields_;
4573 unsigned int field_index = 0;
4574 for (Struct_field_list::const_iterator pf = fields->begin();
4575 pf != fields->end();
4576 ++pf, ++field_index)
4578 // Compare one field in both P1 and P2.
4579 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
4580 f1 = Expression::make_unary(OPERATOR_MULT, f1, bloc);
4581 f1 = Expression::make_field_reference(f1, field_index, bloc);
4583 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
4584 f2 = Expression::make_unary(OPERATOR_MULT, f2, bloc);
4585 f2 = Expression::make_field_reference(f2, field_index, bloc);
4587 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
4589 // If the values are not equal, return false.
4590 gogo->start_block(bloc);
4591 Expression_list* vals = new Expression_list();
4592 vals->push_back(Expression::make_boolean(false, bloc));
4593 Statement* s = Statement::make_return_statement(vals, bloc);
4594 gogo->add_statement(s);
4595 Block* then_block = gogo->finish_block(bloc);
4597 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
4598 gogo->add_statement(s);
4601 // All the fields are equal, so return true.
4602 Expression_list* vals = new Expression_list();
4603 vals->push_back(Expression::make_boolean(true, bloc));
4604 Statement* s = Statement::make_return_statement(vals, bloc);
4605 gogo->add_statement(s);
4608 // Reflection string.
4611 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
4613 ret->append("struct { ");
4615 for (Struct_field_list::const_iterator p = this->fields_->begin();
4616 p != this->fields_->end();
4619 if (p != this->fields_->begin())
4621 if (p->is_anonymous())
4622 ret->push_back('?');
4624 ret->append(Gogo::unpack_hidden_name(p->field_name()));
4625 ret->push_back(' ');
4626 this->append_reflection(p->type(), gogo, ret);
4630 const std::string& tag(p->tag());
4632 for (std::string::const_iterator p = tag.begin();
4637 ret->append("\\x00");
4638 else if (*p == '\n')
4640 else if (*p == '\t')
4643 ret->append("\\\"");
4644 else if (*p == '\\')
4645 ret->append("\\\\");
4649 ret->push_back('"');
4659 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4661 ret->push_back('S');
4663 const Struct_field_list* fields = this->fields_;
4666 for (Struct_field_list::const_iterator p = fields->begin();
4670 if (p->is_anonymous())
4674 std::string n = Gogo::unpack_hidden_name(p->field_name());
4676 snprintf(buf, sizeof buf, "%u_",
4677 static_cast<unsigned int>(n.length()));
4681 this->append_mangled_name(p->type(), gogo, ret);
4684 const std::string& tag(p->tag());
4686 for (std::string::const_iterator p = tag.begin();
4690 if (ISALNUM(*p) || *p == '_')
4695 snprintf(buf, sizeof buf, ".%x.",
4696 static_cast<unsigned int>(*p));
4701 snprintf(buf, sizeof buf, "T%u_",
4702 static_cast<unsigned int>(out.length()));
4709 ret->push_back('e');
4712 // If the offset of field INDEX in the backend implementation can be
4713 // determined, set *POFFSET to the offset in bytes and return true.
4714 // Otherwise, return false.
4717 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
4718 unsigned int* poffset)
4720 Btype* btype = this->get_backend(gogo);
4721 if (!this->is_backend_type_size_known(gogo))
4723 size_t offset = gogo->backend()->type_field_offset(btype, index);
4724 *poffset = static_cast<unsigned int>(offset);
4725 if (*poffset != offset)
4733 Struct_type::do_export(Export* exp) const
4735 exp->write_c_string("struct { ");
4736 const Struct_field_list* fields = this->fields_;
4737 go_assert(fields != NULL);
4738 for (Struct_field_list::const_iterator p = fields->begin();
4742 if (p->is_anonymous())
4743 exp->write_string("? ");
4746 exp->write_string(p->field_name());
4747 exp->write_c_string(" ");
4749 exp->write_type(p->type());
4753 exp->write_c_string(" ");
4755 Expression::make_string(p->tag(), Linemap::predeclared_location());
4756 expr->export_expression(exp);
4760 exp->write_c_string("; ");
4762 exp->write_c_string("}");
4768 Struct_type::do_import(Import* imp)
4770 imp->require_c_string("struct { ");
4771 Struct_field_list* fields = new Struct_field_list;
4772 if (imp->peek_char() != '}')
4777 if (imp->match_c_string("? "))
4781 name = imp->read_identifier();
4782 imp->require_c_string(" ");
4784 Type* ftype = imp->read_type();
4786 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4788 if (imp->peek_char() == ' ')
4791 Expression* expr = Expression::import_expression(imp);
4792 String_expression* sexpr = expr->string_expression();
4793 go_assert(sexpr != NULL);
4794 sf.set_tag(sexpr->val());
4798 imp->require_c_string("; ");
4799 fields->push_back(sf);
4800 if (imp->peek_char() == '}')
4804 imp->require_c_string("}");
4806 return Type::make_struct_type(fields, imp->location());
4809 // Make a struct type.
4812 Type::make_struct_type(Struct_field_list* fields,
4815 return new Struct_type(fields, location);
4818 // Class Array_type.
4820 // Whether two array types are identical.
4823 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
4825 if (!Type::are_identical(this->element_type(), t->element_type(),
4826 errors_are_identical, NULL))
4829 Expression* l1 = this->length();
4830 Expression* l2 = t->length();
4832 // Slices of the same element type are identical.
4833 if (l1 == NULL && l2 == NULL)
4836 // Arrays of the same element type are identical if they have the
4838 if (l1 != NULL && l2 != NULL)
4843 // Try to determine the lengths. If we can't, assume the arrays
4844 // are not identical.
4852 if (l1->integer_constant_value(true, v1, &type1)
4853 && l2->integer_constant_value(true, v2, &type2))
4854 ret = mpz_cmp(v1, v2) == 0;
4860 // Otherwise the arrays are not identical.
4864 // If this type needs explicit comparison and hash functions, create
4865 // them now. It would be a bit better to do this only if the
4866 // functions are needed, but they will be static so the backend can
4867 // discard them if they are not used.
4870 Array_type::finalize_methods(Gogo* gogo)
4872 if (this->length_ != NULL
4873 && !this->length_->is_nil_expression()
4874 && !this->compare_is_identity()
4875 && this->is_comparable())
4877 Named_object* hash_fn;
4878 Named_object* equal_fn;
4879 this->type_functions(gogo, NULL, NULL, NULL, &hash_fn, &equal_fn);
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 // Array type hash code.
4984 Array_type::do_hash_for_method(Gogo* gogo) const
4986 // There is no very convenient way to get a hash code for the
4988 return this->element_type_->hash_for_method(gogo) + 1;
4991 // Write the hash function for an array which can not use the identify
4995 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
4996 Function_type* hash_fntype,
4997 Function_type* equal_fntype)
4999 Location bloc = Linemap::predeclared_location();
5001 // The pointer to the array that we are going to hash. This is an
5002 // argument to the hash function we are implementing here.
5003 Named_object* key_arg = gogo->lookup("key", NULL);
5004 go_assert(key_arg != NULL);
5005 Type* key_arg_type = key_arg->var_value()->type();
5007 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5011 mpz_init_set_ui(ival, 0);
5012 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
5015 // Make a temporary to hold the return value, initialized to 0.
5016 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
5018 gogo->add_statement(retval);
5020 // Make a temporary to hold the key as a uintptr.
5021 Expression* ref = Expression::make_var_reference(key_arg, bloc);
5022 ref = Expression::make_cast(uintptr_type, ref, bloc);
5023 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
5025 gogo->add_statement(key);
5027 // Loop over the array elements.
5029 Type* int_type = Type::lookup_integer_type("int");
5030 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5031 gogo->add_statement(index);
5033 Expression* iref = Expression::make_temporary_reference(index, bloc);
5034 Expression* aref = Expression::make_var_reference(key_arg, bloc);
5035 Type* pt = Type::make_pointer_type(name != NULL
5036 ? static_cast<Type*>(name)
5037 : static_cast<Type*>(this));
5038 aref = Expression::make_cast(pt, aref, bloc);
5039 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5044 gogo->start_block(bloc);
5046 // Multiply retval by 33.
5047 mpz_init_set_ui(ival, 33);
5048 Expression* i33 = Expression::make_integer(&ival, uintptr_type, bloc);
5051 ref = Expression::make_temporary_reference(retval, bloc);
5052 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ, ref,
5054 gogo->add_statement(s);
5056 // Get the hash function for the element type.
5057 Named_object* hash_fn;
5058 Named_object* equal_fn;
5059 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
5060 hash_fntype, equal_fntype, &hash_fn,
5063 // Get a pointer to this element in the loop.
5064 Expression* subkey = Expression::make_temporary_reference(key, bloc);
5065 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
5067 // Get the size of each element.
5068 Expression* ele_size = Expression::make_type_info(this->element_type_,
5069 Expression::TYPE_INFO_SIZE);
5071 // Get the hash of this element.
5072 Expression_list* args = new Expression_list();
5073 args->push_back(subkey);
5074 args->push_back(ele_size);
5075 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
5076 Expression* call = Expression::make_call(func, args, false, bloc);
5078 // Add the element's hash value to retval.
5079 Temporary_reference_expression* tref =
5080 Expression::make_temporary_reference(retval, bloc);
5081 tref->set_is_lvalue();
5082 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, call, bloc);
5083 gogo->add_statement(s);
5085 // Increase the element pointer.
5086 tref = Expression::make_temporary_reference(key, bloc);
5087 tref->set_is_lvalue();
5088 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
5091 Block* statements = gogo->finish_block(bloc);
5093 for_range->add_statements(statements);
5094 gogo->add_statement(for_range);
5096 // Return retval to the caller of the hash function.
5097 Expression_list* vals = new Expression_list();
5098 ref = Expression::make_temporary_reference(retval, bloc);
5099 vals->push_back(ref);
5100 s = Statement::make_return_statement(vals, bloc);
5101 gogo->add_statement(s);
5104 // Write the equality function for an array which can not use the
5105 // identity function.
5108 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
5110 Location bloc = Linemap::predeclared_location();
5112 // The pointers to the arrays we are going to compare.
5113 Named_object* key1_arg = gogo->lookup("key1", NULL);
5114 Named_object* key2_arg = gogo->lookup("key2", NULL);
5115 go_assert(key1_arg != NULL && key2_arg != NULL);
5117 // Build temporaries for the keys with the right types.
5118 Type* pt = Type::make_pointer_type(name != NULL
5119 ? static_cast<Type*>(name)
5120 : static_cast<Type*>(this));
5122 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
5123 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5124 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
5125 gogo->add_statement(p1);
5127 ref = Expression::make_var_reference(key2_arg, bloc);
5128 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5129 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
5130 gogo->add_statement(p2);
5132 // Loop over the array elements.
5134 Type* int_type = Type::lookup_integer_type("int");
5135 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5136 gogo->add_statement(index);
5138 Expression* iref = Expression::make_temporary_reference(index, bloc);
5139 Expression* aref = Expression::make_temporary_reference(p1, bloc);
5140 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5145 gogo->start_block(bloc);
5147 // Compare element in P1 and P2.
5148 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
5149 e1 = Expression::make_unary(OPERATOR_MULT, e1, bloc);
5150 ref = Expression::make_temporary_reference(index, bloc);
5151 e1 = Expression::make_array_index(e1, ref, NULL, bloc);
5153 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
5154 e2 = Expression::make_unary(OPERATOR_MULT, e2, bloc);
5155 ref = Expression::make_temporary_reference(index, bloc);
5156 e2 = Expression::make_array_index(e2, ref, NULL, bloc);
5158 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
5160 // If the elements are not equal, return false.
5161 gogo->start_block(bloc);
5162 Expression_list* vals = new Expression_list();
5163 vals->push_back(Expression::make_boolean(false, bloc));
5164 Statement* s = Statement::make_return_statement(vals, bloc);
5165 gogo->add_statement(s);
5166 Block* then_block = gogo->finish_block(bloc);
5168 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
5169 gogo->add_statement(s);
5171 Block* statements = gogo->finish_block(bloc);
5173 for_range->add_statements(statements);
5174 gogo->add_statement(for_range);
5176 // All the elements are equal, so return true.
5177 vals = new Expression_list();
5178 vals->push_back(Expression::make_boolean(true, bloc));
5179 s = Statement::make_return_statement(vals, bloc);
5180 gogo->add_statement(s);
5183 // Get a tree for the length of a fixed array. The length may be
5184 // computed using a function call, so we must only evaluate it once.
5187 Array_type::get_length_tree(Gogo* gogo)
5189 go_assert(this->length_ != NULL);
5190 if (this->length_tree_ == NULL_TREE)
5195 if (this->length_->integer_constant_value(true, val, &t))
5198 t = Type::lookup_integer_type("int");
5199 else if (t->is_abstract())
5200 t = t->make_non_abstract_type();
5201 tree tt = type_to_tree(t->get_backend(gogo));
5202 this->length_tree_ = Expression::integer_constant_tree(val, tt);
5209 // Make up a translation context for the array length
5210 // expression. FIXME: This won't work in general.
5211 Translate_context context(gogo, NULL, NULL, NULL);
5212 tree len = this->length_->get_tree(&context);
5213 if (len != error_mark_node)
5215 len = convert_to_integer(integer_type_node, len);
5216 len = save_expr(len);
5218 this->length_tree_ = len;
5221 return this->length_tree_;
5224 // Get the backend representation of the fields of a slice. This is
5225 // not declared in types.h so that types.h doesn't have to #include
5228 // We use int for the count and capacity fields. This matches 6g.
5229 // The language more or less assumes that we can't allocate space of a
5230 // size which does not fit in int.
5233 get_backend_slice_fields(Gogo* gogo, Array_type* type,
5234 std::vector<Backend::Btyped_identifier>* bfields)
5238 Type* pet = Type::make_pointer_type(type->element_type());
5239 Btype* pbet = pet->get_backend(gogo);
5240 Location ploc = Linemap::predeclared_location();
5242 Backend::Btyped_identifier* p = &(*bfields)[0];
5243 p->name = "__values";
5247 Type* int_type = Type::lookup_integer_type("int");
5250 p->name = "__count";
5251 p->btype = int_type->get_backend(gogo);
5255 p->name = "__capacity";
5256 p->btype = int_type->get_backend(gogo);
5260 // Get a tree for the type of this array. A fixed array is simply
5261 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
5262 // just like an array in C. An open array is a struct with three
5263 // fields: a data pointer, the length, and the capacity.
5266 Array_type::do_get_backend(Gogo* gogo)
5268 if (this->length_ == NULL)
5270 std::vector<Backend::Btyped_identifier> bfields;
5271 get_backend_slice_fields(gogo, this, &bfields);
5272 return gogo->backend()->struct_type(bfields);
5276 Btype* element = this->get_backend_element(gogo);
5277 Bexpression* len = this->get_backend_length(gogo);
5278 return gogo->backend()->array_type(element, len);
5282 // Return the backend representation of the element type.
5284 Array_type::get_backend_element(Gogo* gogo)
5286 return this->element_type_->get_backend(gogo);
5289 // Return the backend representation of the length.
5292 Array_type::get_backend_length(Gogo* gogo)
5294 return tree_to_expr(this->get_length_tree(gogo));
5297 // Return a tree for a pointer to the values in ARRAY.
5300 Array_type::value_pointer_tree(Gogo*, tree array) const
5303 if (this->length() != NULL)
5306 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
5307 build_fold_addr_expr(array));
5312 tree field = TYPE_FIELDS(TREE_TYPE(array));
5313 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
5315 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
5318 if (TREE_CONSTANT(array))
5319 TREE_CONSTANT(ret) = 1;
5323 // Return a tree for the length of the array ARRAY which has this
5327 Array_type::length_tree(Gogo* gogo, tree array)
5329 if (this->length_ != NULL)
5331 if (TREE_CODE(array) == SAVE_EXPR)
5332 return fold_convert(integer_type_node, this->get_length_tree(gogo));
5334 return omit_one_operand(integer_type_node,
5335 this->get_length_tree(gogo), array);
5338 // This is an open array. We need to read the length field.
5340 tree type = TREE_TYPE(array);
5341 go_assert(TREE_CODE(type) == RECORD_TYPE);
5343 tree field = DECL_CHAIN(TYPE_FIELDS(type));
5344 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
5346 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5347 if (TREE_CONSTANT(array))
5348 TREE_CONSTANT(ret) = 1;
5352 // Return a tree for the capacity of the array ARRAY which has this
5356 Array_type::capacity_tree(Gogo* gogo, tree array)
5358 if (this->length_ != NULL)
5359 return omit_one_operand(sizetype, this->get_length_tree(gogo), array);
5361 // This is an open array. We need to read the capacity field.
5363 tree type = TREE_TYPE(array);
5364 go_assert(TREE_CODE(type) == RECORD_TYPE);
5366 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
5367 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
5369 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5375 Array_type::do_export(Export* exp) const
5377 exp->write_c_string("[");
5378 if (this->length_ != NULL)
5379 this->length_->export_expression(exp);
5380 exp->write_c_string("] ");
5381 exp->write_type(this->element_type_);
5387 Array_type::do_import(Import* imp)
5389 imp->require_c_string("[");
5391 if (imp->peek_char() == ']')
5394 length = Expression::import_expression(imp);
5395 imp->require_c_string("] ");
5396 Type* element_type = imp->read_type();
5397 return Type::make_array_type(element_type, length);
5400 // The type of an array type descriptor.
5403 Array_type::make_array_type_descriptor_type()
5408 Type* tdt = Type::make_type_descriptor_type();
5409 Type* ptdt = Type::make_type_descriptor_ptr_type();
5411 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5414 Type::make_builtin_struct_type(4,
5418 "len", uintptr_type);
5420 ret = Type::make_builtin_named_type("ArrayType", sf);
5426 // The type of an slice type descriptor.
5429 Array_type::make_slice_type_descriptor_type()
5434 Type* tdt = Type::make_type_descriptor_type();
5435 Type* ptdt = Type::make_type_descriptor_ptr_type();
5438 Type::make_builtin_struct_type(2,
5442 ret = Type::make_builtin_named_type("SliceType", sf);
5448 // Build a type descriptor for an array/slice type.
5451 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5453 if (this->length_ != NULL)
5454 return this->array_type_descriptor(gogo, name);
5456 return this->slice_type_descriptor(gogo, name);
5459 // Build a type descriptor for an array type.
5462 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
5464 Location bloc = Linemap::predeclared_location();
5466 Type* atdt = Array_type::make_array_type_descriptor_type();
5468 const Struct_field_list* fields = atdt->struct_type()->fields();
5470 Expression_list* vals = new Expression_list();
5473 Struct_field_list::const_iterator p = fields->begin();
5474 go_assert(p->is_field_name("commonType"));
5475 vals->push_back(this->type_descriptor_constructor(gogo,
5476 RUNTIME_TYPE_KIND_ARRAY,
5480 go_assert(p->is_field_name("elem"));
5481 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5484 go_assert(p->is_field_name("slice"));
5485 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
5486 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
5489 go_assert(p->is_field_name("len"));
5490 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
5493 go_assert(p == fields->end());
5495 return Expression::make_struct_composite_literal(atdt, vals, bloc);
5498 // Build a type descriptor for a slice type.
5501 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
5503 Location bloc = Linemap::predeclared_location();
5505 Type* stdt = Array_type::make_slice_type_descriptor_type();
5507 const Struct_field_list* fields = stdt->struct_type()->fields();
5509 Expression_list* vals = new Expression_list();
5512 Struct_field_list::const_iterator p = fields->begin();
5513 go_assert(p->is_field_name("commonType"));
5514 vals->push_back(this->type_descriptor_constructor(gogo,
5515 RUNTIME_TYPE_KIND_SLICE,
5519 go_assert(p->is_field_name("elem"));
5520 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5523 go_assert(p == fields->end());
5525 return Expression::make_struct_composite_literal(stdt, vals, bloc);
5528 // Reflection string.
5531 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
5533 ret->push_back('[');
5534 if (this->length_ != NULL)
5539 if (!this->length_->integer_constant_value(true, val, &type))
5540 error_at(this->length_->location(),
5541 "array length must be integer constant expression");
5542 else if (mpz_cmp_si(val, 0) < 0)
5543 error_at(this->length_->location(), "array length is negative");
5544 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
5545 error_at(this->length_->location(), "array length is too large");
5549 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
5554 ret->push_back(']');
5556 this->append_reflection(this->element_type_, gogo, ret);
5562 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5564 ret->push_back('A');
5565 this->append_mangled_name(this->element_type_, gogo, ret);
5566 if (this->length_ != NULL)
5571 if (!this->length_->integer_constant_value(true, val, &type))
5572 error_at(this->length_->location(),
5573 "array length must be integer constant expression");
5574 else if (mpz_cmp_si(val, 0) < 0)
5575 error_at(this->length_->location(), "array length is negative");
5576 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
5577 error_at(this->length_->location(), "array size is too large");
5581 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
5586 ret->push_back('e');
5589 // Make an array type.
5592 Type::make_array_type(Type* element_type, Expression* length)
5594 return new Array_type(element_type, length);
5602 Map_type::do_traverse(Traverse* traverse)
5604 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
5605 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
5606 return TRAVERSE_EXIT;
5607 return TRAVERSE_CONTINUE;
5610 // Check that the map type is OK.
5613 Map_type::do_verify()
5615 // The runtime support uses "map[void]void".
5616 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
5618 error_at(this->location_, "invalid map key type");
5624 // Whether two map types are identical.
5627 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
5629 return (Type::are_identical(this->key_type(), t->key_type(),
5630 errors_are_identical, NULL)
5631 && Type::are_identical(this->val_type(), t->val_type(),
5632 errors_are_identical, NULL));
5638 Map_type::do_hash_for_method(Gogo* gogo) const
5640 return (this->key_type_->hash_for_method(gogo)
5641 + this->val_type_->hash_for_method(gogo)
5645 // Get the backend representation for a map type. A map type is
5646 // represented as a pointer to a struct. The struct is __go_map in
5650 Map_type::do_get_backend(Gogo* gogo)
5652 static Btype* backend_map_type;
5653 if (backend_map_type == NULL)
5655 std::vector<Backend::Btyped_identifier> bfields(4);
5657 Location bloc = Linemap::predeclared_location();
5659 Type* pdt = Type::make_type_descriptor_ptr_type();
5660 bfields[0].name = "__descriptor";
5661 bfields[0].btype = pdt->get_backend(gogo);
5662 bfields[0].location = bloc;
5664 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5665 bfields[1].name = "__element_count";
5666 bfields[1].btype = uintptr_type->get_backend(gogo);
5667 bfields[1].location = bloc;
5669 bfields[2].name = "__bucket_count";
5670 bfields[2].btype = bfields[1].btype;
5671 bfields[2].location = bloc;
5673 Btype* bvt = gogo->backend()->void_type();
5674 Btype* bpvt = gogo->backend()->pointer_type(bvt);
5675 Btype* bppvt = gogo->backend()->pointer_type(bpvt);
5676 bfields[3].name = "__buckets";
5677 bfields[3].btype = bppvt;
5678 bfields[3].location = bloc;
5680 Btype *bt = gogo->backend()->struct_type(bfields);
5681 bt = gogo->backend()->named_type("__go_map", bt, bloc);
5682 backend_map_type = gogo->backend()->pointer_type(bt);
5684 return backend_map_type;
5687 // The type of a map type descriptor.
5690 Map_type::make_map_type_descriptor_type()
5695 Type* tdt = Type::make_type_descriptor_type();
5696 Type* ptdt = Type::make_type_descriptor_ptr_type();
5699 Type::make_builtin_struct_type(3,
5704 ret = Type::make_builtin_named_type("MapType", sf);
5710 // Build a type descriptor for a map type.
5713 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5715 Location bloc = Linemap::predeclared_location();
5717 Type* mtdt = Map_type::make_map_type_descriptor_type();
5719 const Struct_field_list* fields = mtdt->struct_type()->fields();
5721 Expression_list* vals = new Expression_list();
5724 Struct_field_list::const_iterator p = fields->begin();
5725 go_assert(p->is_field_name("commonType"));
5726 vals->push_back(this->type_descriptor_constructor(gogo,
5727 RUNTIME_TYPE_KIND_MAP,
5731 go_assert(p->is_field_name("key"));
5732 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
5735 go_assert(p->is_field_name("elem"));
5736 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
5739 go_assert(p == fields->end());
5741 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
5744 // A mapping from map types to map descriptors.
5746 Map_type::Map_descriptors Map_type::map_descriptors;
5748 // Build a map descriptor for this type. Return a pointer to it.
5751 Map_type::map_descriptor_pointer(Gogo* gogo, Location location)
5753 Bvariable* bvar = this->map_descriptor(gogo);
5754 tree var_tree = var_to_tree(bvar);
5755 if (var_tree == error_mark_node)
5756 return error_mark_node;
5757 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
5760 // Build a map descriptor for this type.
5763 Map_type::map_descriptor(Gogo* gogo)
5765 std::pair<Map_type*, Bvariable*> val(this, NULL);
5766 std::pair<Map_type::Map_descriptors::iterator, bool> ins =
5767 Map_type::map_descriptors.insert(val);
5769 return ins.first->second;
5771 Type* key_type = this->key_type_;
5772 Type* val_type = this->val_type_;
5774 // The map entry type is a struct with three fields. Build that
5775 // struct so that we can get the offsets of the key and value within
5776 // a map entry. The first field should technically be a pointer to
5777 // this type itself, but since we only care about field offsets we
5778 // just use pointer to bool.
5779 Type* pbool = Type::make_pointer_type(Type::make_boolean_type());
5780 Struct_type* map_entry_type =
5781 Type::make_builtin_struct_type(3,
5786 Type* map_descriptor_type = Map_type::make_map_descriptor_type();
5788 const Struct_field_list* fields =
5789 map_descriptor_type->struct_type()->fields();
5791 Expression_list* vals = new Expression_list();
5794 Location bloc = Linemap::predeclared_location();
5796 Struct_field_list::const_iterator p = fields->begin();
5798 go_assert(p->is_field_name("__map_descriptor"));
5799 vals->push_back(Expression::make_type_descriptor(this, bloc));
5802 go_assert(p->is_field_name("__entry_size"));
5803 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
5804 vals->push_back(Expression::make_type_info(map_entry_type, type_info));
5806 Struct_field_list::const_iterator pf = map_entry_type->fields()->begin();
5808 go_assert(pf->is_field_name("__key"));
5811 go_assert(p->is_field_name("__key_offset"));
5812 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
5815 go_assert(pf->is_field_name("__val"));
5818 go_assert(p->is_field_name("__val_offset"));
5819 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
5822 go_assert(p == fields->end());
5824 Expression* initializer =
5825 Expression::make_struct_composite_literal(map_descriptor_type, vals, bloc);
5827 std::string mangled_name = "__go_map_" + this->mangled_name(gogo);
5828 Btype* map_descriptor_btype = map_descriptor_type->get_backend(gogo);
5829 Bvariable* bvar = gogo->backend()->immutable_struct(mangled_name, true,
5830 map_descriptor_btype,
5833 Translate_context context(gogo, NULL, NULL, NULL);
5834 context.set_is_const();
5835 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
5837 gogo->backend()->immutable_struct_set_init(bvar, mangled_name, true,
5838 map_descriptor_btype, bloc,
5841 ins.first->second = bvar;
5845 // Build the type of a map descriptor. This must match the struct
5846 // __go_map_descriptor in libgo/runtime/map.h.
5849 Map_type::make_map_descriptor_type()
5854 Type* ptdt = Type::make_type_descriptor_ptr_type();
5855 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5857 Type::make_builtin_struct_type(4,
5858 "__map_descriptor", ptdt,
5859 "__entry_size", uintptr_type,
5860 "__key_offset", uintptr_type,
5861 "__val_offset", uintptr_type);
5862 ret = Type::make_builtin_named_type("__go_map_descriptor", sf);
5867 // Reflection string for a map.
5870 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
5872 ret->append("map[");
5873 this->append_reflection(this->key_type_, gogo, ret);
5875 this->append_reflection(this->val_type_, gogo, ret);
5878 // Mangled name for a map.
5881 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5883 ret->push_back('M');
5884 this->append_mangled_name(this->key_type_, gogo, ret);
5886 this->append_mangled_name(this->val_type_, gogo, ret);
5889 // Export a map type.
5892 Map_type::do_export(Export* exp) const
5894 exp->write_c_string("map [");
5895 exp->write_type(this->key_type_);
5896 exp->write_c_string("] ");
5897 exp->write_type(this->val_type_);
5900 // Import a map type.
5903 Map_type::do_import(Import* imp)
5905 imp->require_c_string("map [");
5906 Type* key_type = imp->read_type();
5907 imp->require_c_string("] ");
5908 Type* val_type = imp->read_type();
5909 return Type::make_map_type(key_type, val_type, imp->location());
5915 Type::make_map_type(Type* key_type, Type* val_type, Location location)
5917 return new Map_type(key_type, val_type, location);
5920 // Class Channel_type.
5925 Channel_type::do_hash_for_method(Gogo* gogo) const
5927 unsigned int ret = 0;
5928 if (this->may_send_)
5930 if (this->may_receive_)
5932 if (this->element_type_ != NULL)
5933 ret += this->element_type_->hash_for_method(gogo) << 2;
5937 // Whether this type is the same as T.
5940 Channel_type::is_identical(const Channel_type* t,
5941 bool errors_are_identical) const
5943 if (!Type::are_identical(this->element_type(), t->element_type(),
5944 errors_are_identical, NULL))
5946 return (this->may_send_ == t->may_send_
5947 && this->may_receive_ == t->may_receive_);
5950 // Return the tree for a channel type. A channel is a pointer to a
5951 // __go_channel struct. The __go_channel struct is defined in
5952 // libgo/runtime/channel.h.
5955 Channel_type::do_get_backend(Gogo* gogo)
5957 static Btype* backend_channel_type;
5958 if (backend_channel_type == NULL)
5960 std::vector<Backend::Btyped_identifier> bfields;
5961 Btype* bt = gogo->backend()->struct_type(bfields);
5962 bt = gogo->backend()->named_type("__go_channel", bt,
5963 Linemap::predeclared_location());
5964 backend_channel_type = gogo->backend()->pointer_type(bt);
5966 return backend_channel_type;
5969 // Build a type descriptor for a channel type.
5972 Channel_type::make_chan_type_descriptor_type()
5977 Type* tdt = Type::make_type_descriptor_type();
5978 Type* ptdt = Type::make_type_descriptor_ptr_type();
5980 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5983 Type::make_builtin_struct_type(3,
5986 "dir", uintptr_type);
5988 ret = Type::make_builtin_named_type("ChanType", sf);
5994 // Build a type descriptor for a map type.
5997 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5999 Location bloc = Linemap::predeclared_location();
6001 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
6003 const Struct_field_list* fields = ctdt->struct_type()->fields();
6005 Expression_list* vals = new Expression_list();
6008 Struct_field_list::const_iterator p = fields->begin();
6009 go_assert(p->is_field_name("commonType"));
6010 vals->push_back(this->type_descriptor_constructor(gogo,
6011 RUNTIME_TYPE_KIND_CHAN,
6015 go_assert(p->is_field_name("elem"));
6016 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6019 go_assert(p->is_field_name("dir"));
6020 // These bits must match the ones in libgo/runtime/go-type.h.
6022 if (this->may_receive_)
6024 if (this->may_send_)
6027 mpz_init_set_ui(iv, val);
6028 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
6032 go_assert(p == fields->end());
6034 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
6037 // Reflection string.
6040 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
6042 if (!this->may_send_)
6044 ret->append("chan");
6045 if (!this->may_receive_)
6047 ret->push_back(' ');
6048 this->append_reflection(this->element_type_, gogo, ret);
6054 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6056 ret->push_back('C');
6057 this->append_mangled_name(this->element_type_, gogo, ret);
6058 if (this->may_send_)
6059 ret->push_back('s');
6060 if (this->may_receive_)
6061 ret->push_back('r');
6062 ret->push_back('e');
6068 Channel_type::do_export(Export* exp) const
6070 exp->write_c_string("chan ");
6071 if (this->may_send_ && !this->may_receive_)
6072 exp->write_c_string("-< ");
6073 else if (this->may_receive_ && !this->may_send_)
6074 exp->write_c_string("<- ");
6075 exp->write_type(this->element_type_);
6081 Channel_type::do_import(Import* imp)
6083 imp->require_c_string("chan ");
6087 if (imp->match_c_string("-< "))
6091 may_receive = false;
6093 else if (imp->match_c_string("<- "))
6105 Type* element_type = imp->read_type();
6107 return Type::make_channel_type(may_send, may_receive, element_type);
6110 // Make a new channel type.
6113 Type::make_channel_type(bool send, bool receive, Type* element_type)
6115 return new Channel_type(send, receive, element_type);
6118 // Class Interface_type.
6123 Interface_type::do_traverse(Traverse* traverse)
6125 if (this->methods_ == NULL)
6126 return TRAVERSE_CONTINUE;
6127 return this->methods_->traverse(traverse);
6130 // Finalize the methods. This handles interface inheritance.
6133 Interface_type::finalize_methods()
6135 if (this->methods_ == NULL)
6137 std::vector<Named_type*> seen;
6138 bool is_recursive = false;
6141 while (from < this->methods_->size())
6143 const Typed_identifier* p = &this->methods_->at(from);
6144 if (!p->name().empty())
6147 for (i = 0; i < to; ++i)
6149 if (this->methods_->at(i).name() == p->name())
6151 error_at(p->location(), "duplicate method %qs",
6152 Gogo::message_name(p->name()).c_str());
6159 this->methods_->set(to, *p);
6166 Interface_type* it = p->type()->interface_type();
6169 error_at(p->location(), "interface contains embedded non-interface");
6177 error_at(p->location(), "invalid recursive interface");
6178 is_recursive = true;
6184 Named_type* nt = p->type()->named_type();
6187 std::vector<Named_type*>::const_iterator q;
6188 for (q = seen.begin(); q != seen.end(); ++q)
6192 error_at(p->location(), "inherited interface loop");
6196 if (q != seen.end())
6204 const Typed_identifier_list* methods = it->methods();
6205 if (methods == NULL)
6210 for (Typed_identifier_list::const_iterator q = methods->begin();
6211 q != methods->end();
6214 if (q->name().empty())
6216 if (q->type()->forwarded() == p->type()->forwarded())
6217 error_at(p->location(), "interface inheritance loop");
6221 for (i = from + 1; i < this->methods_->size(); ++i)
6223 const Typed_identifier* r = &this->methods_->at(i);
6224 if (r->name().empty()
6225 && r->type()->forwarded() == q->type()->forwarded())
6227 error_at(p->location(),
6228 "inherited interface listed twice");
6232 if (i == this->methods_->size())
6233 this->methods_->push_back(Typed_identifier(q->name(),
6238 else if (this->find_method(q->name()) == NULL)
6239 this->methods_->push_back(Typed_identifier(q->name(), q->type(),
6244 error_at(p->location(), "inherited method %qs is ambiguous",
6245 Gogo::message_name(q->name()).c_str());
6252 delete this->methods_;
6253 this->methods_ = NULL;
6257 this->methods_->resize(to);
6258 this->methods_->sort_by_name();
6262 // Return the method NAME, or NULL.
6264 const Typed_identifier*
6265 Interface_type::find_method(const std::string& name) const
6267 if (this->methods_ == NULL)
6269 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6270 p != this->methods_->end();
6272 if (p->name() == name)
6277 // Return the method index.
6280 Interface_type::method_index(const std::string& name) const
6282 go_assert(this->methods_ != NULL);
6284 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6285 p != this->methods_->end();
6287 if (p->name() == name)
6292 // Return whether NAME is an unexported method, for better error
6296 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
6298 if (this->methods_ == NULL)
6300 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6301 p != this->methods_->end();
6304 const std::string& method_name(p->name());
6305 if (Gogo::is_hidden_name(method_name)
6306 && name == Gogo::unpack_hidden_name(method_name)
6307 && gogo->pack_hidden_name(name, false) != method_name)
6313 // Whether this type is identical with T.
6316 Interface_type::is_identical(const Interface_type* t,
6317 bool errors_are_identical) const
6319 // We require the same methods with the same types. The methods
6320 // have already been sorted.
6321 if (this->methods() == NULL || t->methods() == NULL)
6322 return this->methods() == t->methods();
6324 Typed_identifier_list::const_iterator p1 = this->methods()->begin();
6325 for (Typed_identifier_list::const_iterator p2 = t->methods()->begin();
6326 p2 != t->methods()->end();
6329 if (p1 == this->methods()->end())
6331 if (p1->name() != p2->name()
6332 || !Type::are_identical(p1->type(), p2->type(),
6333 errors_are_identical, NULL))
6336 if (p1 != this->methods()->end())
6341 // Whether we can assign the interface type T to this type. The types
6342 // are known to not be identical. An interface assignment is only
6343 // permitted if T is known to implement all methods in THIS.
6344 // Otherwise a type guard is required.
6347 Interface_type::is_compatible_for_assign(const Interface_type* t,
6348 std::string* reason) const
6350 if (this->methods() == NULL)
6352 for (Typed_identifier_list::const_iterator p = this->methods()->begin();
6353 p != this->methods()->end();
6356 const Typed_identifier* m = t->find_method(p->name());
6362 snprintf(buf, sizeof buf,
6363 _("need explicit conversion; missing method %s%s%s"),
6364 open_quote, Gogo::message_name(p->name()).c_str(),
6366 reason->assign(buf);
6371 std::string subreason;
6372 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
6376 std::string n = Gogo::message_name(p->name());
6377 size_t len = 100 + n.length() + subreason.length();
6378 char* buf = new char[len];
6379 if (subreason.empty())
6380 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6381 open_quote, n.c_str(), close_quote);
6384 _("incompatible type for method %s%s%s (%s)"),
6385 open_quote, n.c_str(), close_quote,
6387 reason->assign(buf);
6400 Interface_type::do_hash_for_method(Gogo* gogo) const
6402 unsigned int ret = 0;
6403 if (this->methods_ != NULL)
6405 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6406 p != this->methods_->end();
6409 ret = Type::hash_string(p->name(), ret);
6410 ret += p->type()->hash_for_method(gogo);
6417 // Return true if T implements the interface. If it does not, and
6418 // REASON is not NULL, set *REASON to a useful error message.
6421 Interface_type::implements_interface(const Type* t, std::string* reason) const
6423 if (this->methods_ == NULL)
6426 bool is_pointer = false;
6427 const Named_type* nt = t->named_type();
6428 const Struct_type* st = t->struct_type();
6429 // If we start with a named type, we don't dereference it to find
6433 const Type* pt = t->points_to();
6436 // If T is a pointer to a named type, then we need to look at
6437 // the type to which it points.
6439 nt = pt->named_type();
6440 st = pt->struct_type();
6444 // If we have a named type, get the methods from it rather than from
6449 // Only named and struct types have methods.
6450 if (nt == NULL && st == NULL)
6454 if (t->points_to() != NULL
6455 && t->points_to()->interface_type() != NULL)
6456 reason->assign(_("pointer to interface type has no methods"));
6458 reason->assign(_("type has no methods"));
6463 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
6467 if (t->points_to() != NULL
6468 && t->points_to()->interface_type() != NULL)
6469 reason->assign(_("pointer to interface type has no methods"));
6471 reason->assign(_("type has no methods"));
6476 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6477 p != this->methods_->end();
6480 bool is_ambiguous = false;
6481 Method* m = (nt != NULL
6482 ? nt->method_function(p->name(), &is_ambiguous)
6483 : st->method_function(p->name(), &is_ambiguous));
6488 std::string n = Gogo::message_name(p->name());
6489 size_t len = n.length() + 100;
6490 char* buf = new char[len];
6492 snprintf(buf, len, _("ambiguous method %s%s%s"),
6493 open_quote, n.c_str(), close_quote);
6495 snprintf(buf, len, _("missing method %s%s%s"),
6496 open_quote, n.c_str(), close_quote);
6497 reason->assign(buf);
6503 Function_type *p_fn_type = p->type()->function_type();
6504 Function_type* m_fn_type = m->type()->function_type();
6505 go_assert(p_fn_type != NULL && m_fn_type != NULL);
6506 std::string subreason;
6507 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
6511 std::string n = Gogo::message_name(p->name());
6512 size_t len = 100 + n.length() + subreason.length();
6513 char* buf = new char[len];
6514 if (subreason.empty())
6515 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6516 open_quote, n.c_str(), close_quote);
6519 _("incompatible type for method %s%s%s (%s)"),
6520 open_quote, n.c_str(), close_quote,
6522 reason->assign(buf);
6528 if (!is_pointer && !m->is_value_method())
6532 std::string n = Gogo::message_name(p->name());
6533 size_t len = 100 + n.length();
6534 char* buf = new char[len];
6535 snprintf(buf, len, _("method %s%s%s requires a pointer"),
6536 open_quote, n.c_str(), close_quote);
6537 reason->assign(buf);
6547 // Return the backend representation of the empty interface type. We
6548 // use the same struct for all empty interfaces.
6551 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
6553 static Btype* empty_interface_type;
6554 if (empty_interface_type == NULL)
6556 std::vector<Backend::Btyped_identifier> bfields(2);
6558 Location bloc = Linemap::predeclared_location();
6560 Type* pdt = Type::make_type_descriptor_ptr_type();
6561 bfields[0].name = "__type_descriptor";
6562 bfields[0].btype = pdt->get_backend(gogo);
6563 bfields[0].location = bloc;
6565 Type* vt = Type::make_pointer_type(Type::make_void_type());
6566 bfields[1].name = "__object";
6567 bfields[1].btype = vt->get_backend(gogo);
6568 bfields[1].location = bloc;
6570 empty_interface_type = gogo->backend()->struct_type(bfields);
6572 return empty_interface_type;
6575 // Return the fields of a non-empty interface type. This is not
6576 // declared in types.h so that types.h doesn't have to #include
6580 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
6581 std::vector<Backend::Btyped_identifier>* bfields)
6583 Location loc = type->location();
6585 std::vector<Backend::Btyped_identifier> mfields(type->methods()->size() + 1);
6587 Type* pdt = Type::make_type_descriptor_ptr_type();
6588 mfields[0].name = "__type_descriptor";
6589 mfields[0].btype = pdt->get_backend(gogo);
6590 mfields[0].location = loc;
6592 std::string last_name = "";
6594 for (Typed_identifier_list::const_iterator p = type->methods()->begin();
6595 p != type->methods()->end();
6598 mfields[i].name = Gogo::unpack_hidden_name(p->name());
6599 mfields[i].btype = p->type()->get_backend(gogo);
6600 mfields[i].location = loc;
6601 // Sanity check: the names should be sorted.
6602 go_assert(p->name() > last_name);
6603 last_name = p->name();
6606 Btype* methods = gogo->backend()->struct_type(mfields);
6610 (*bfields)[0].name = "__methods";
6611 (*bfields)[0].btype = gogo->backend()->pointer_type(methods);
6612 (*bfields)[0].location = loc;
6614 Type* vt = Type::make_pointer_type(Type::make_void_type());
6615 (*bfields)[1].name = "__object";
6616 (*bfields)[1].btype = vt->get_backend(gogo);
6617 (*bfields)[1].location = Linemap::predeclared_location();
6620 // Return a tree for an interface type. An interface is a pointer to
6621 // a struct. The struct has three fields. The first field is a
6622 // pointer to the type descriptor for the dynamic type of the object.
6623 // The second field is a pointer to a table of methods for the
6624 // interface to be used with the object. The third field is the value
6625 // of the object itself.
6628 Interface_type::do_get_backend(Gogo* gogo)
6630 if (this->methods_ == NULL)
6631 return Interface_type::get_backend_empty_interface_type(gogo);
6634 std::vector<Backend::Btyped_identifier> bfields;
6635 get_backend_interface_fields(gogo, this, &bfields);
6636 return gogo->backend()->struct_type(bfields);
6640 // The type of an interface type descriptor.
6643 Interface_type::make_interface_type_descriptor_type()
6648 Type* tdt = Type::make_type_descriptor_type();
6649 Type* ptdt = Type::make_type_descriptor_ptr_type();
6651 Type* string_type = Type::lookup_string_type();
6652 Type* pointer_string_type = Type::make_pointer_type(string_type);
6655 Type::make_builtin_struct_type(3,
6656 "name", pointer_string_type,
6657 "pkgPath", pointer_string_type,
6660 Type* nsm = Type::make_builtin_named_type("imethod", sm);
6662 Type* slice_nsm = Type::make_array_type(nsm, NULL);
6664 Struct_type* s = Type::make_builtin_struct_type(2,
6666 "methods", slice_nsm);
6668 ret = Type::make_builtin_named_type("InterfaceType", s);
6674 // Build a type descriptor for an interface type.
6677 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6679 Location bloc = Linemap::predeclared_location();
6681 Type* itdt = Interface_type::make_interface_type_descriptor_type();
6683 const Struct_field_list* ifields = itdt->struct_type()->fields();
6685 Expression_list* ivals = new Expression_list();
6688 Struct_field_list::const_iterator pif = ifields->begin();
6689 go_assert(pif->is_field_name("commonType"));
6690 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
6691 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
6695 go_assert(pif->is_field_name("methods"));
6697 Expression_list* methods = new Expression_list();
6698 if (this->methods_ != NULL && !this->methods_->empty())
6700 Type* elemtype = pif->type()->array_type()->element_type();
6702 methods->reserve(this->methods_->size());
6703 for (Typed_identifier_list::const_iterator pm = this->methods_->begin();
6704 pm != this->methods_->end();
6707 const Struct_field_list* mfields = elemtype->struct_type()->fields();
6709 Expression_list* mvals = new Expression_list();
6712 Struct_field_list::const_iterator pmf = mfields->begin();
6713 go_assert(pmf->is_field_name("name"));
6714 std::string s = Gogo::unpack_hidden_name(pm->name());
6715 Expression* e = Expression::make_string(s, bloc);
6716 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6719 go_assert(pmf->is_field_name("pkgPath"));
6720 if (!Gogo::is_hidden_name(pm->name()))
6721 mvals->push_back(Expression::make_nil(bloc));
6724 s = Gogo::hidden_name_prefix(pm->name());
6725 e = Expression::make_string(s, bloc);
6726 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6730 go_assert(pmf->is_field_name("typ"));
6731 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
6734 go_assert(pmf == mfields->end());
6736 e = Expression::make_struct_composite_literal(elemtype, mvals,
6738 methods->push_back(e);
6742 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
6746 go_assert(pif == ifields->end());
6748 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
6751 // Reflection string.
6754 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
6756 ret->append("interface {");
6757 if (this->methods_ != NULL)
6759 ret->push_back(' ');
6760 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6761 p != this->methods_->end();
6764 if (p != this->methods_->begin())
6766 if (!Gogo::is_hidden_name(p->name()))
6767 ret->append(p->name());
6770 // This matches what the gc compiler does.
6771 std::string prefix = Gogo::hidden_name_prefix(p->name());
6772 ret->append(prefix.substr(prefix.find('.') + 1));
6773 ret->push_back('.');
6774 ret->append(Gogo::unpack_hidden_name(p->name()));
6776 std::string sub = p->type()->reflection(gogo);
6777 go_assert(sub.compare(0, 4, "func") == 0);
6778 sub = sub.substr(4);
6781 ret->push_back(' ');
6789 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6791 ret->push_back('I');
6793 const Typed_identifier_list* methods = this->methods_;
6794 if (methods != NULL)
6796 for (Typed_identifier_list::const_iterator p = methods->begin();
6797 p != methods->end();
6800 std::string n = Gogo::unpack_hidden_name(p->name());
6802 snprintf(buf, sizeof buf, "%u_",
6803 static_cast<unsigned int>(n.length()));
6806 this->append_mangled_name(p->type(), gogo, ret);
6810 ret->push_back('e');
6816 Interface_type::do_export(Export* exp) const
6818 exp->write_c_string("interface { ");
6820 const Typed_identifier_list* methods = this->methods_;
6821 if (methods != NULL)
6823 for (Typed_identifier_list::const_iterator pm = methods->begin();
6824 pm != methods->end();
6827 exp->write_string(pm->name());
6828 exp->write_c_string(" (");
6830 const Function_type* fntype = pm->type()->function_type();
6833 const Typed_identifier_list* parameters = fntype->parameters();
6834 if (parameters != NULL)
6836 bool is_varargs = fntype->is_varargs();
6837 for (Typed_identifier_list::const_iterator pp =
6838 parameters->begin();
6839 pp != parameters->end();
6845 exp->write_c_string(", ");
6846 if (!is_varargs || pp + 1 != parameters->end())
6847 exp->write_type(pp->type());
6850 exp->write_c_string("...");
6851 Type *pptype = pp->type();
6852 exp->write_type(pptype->array_type()->element_type());
6857 exp->write_c_string(")");
6859 const Typed_identifier_list* results = fntype->results();
6860 if (results != NULL)
6862 exp->write_c_string(" ");
6863 if (results->size() == 1)
6864 exp->write_type(results->begin()->type());
6868 exp->write_c_string("(");
6869 for (Typed_identifier_list::const_iterator p =
6871 p != results->end();
6877 exp->write_c_string(", ");
6878 exp->write_type(p->type());
6880 exp->write_c_string(")");
6884 exp->write_c_string("; ");
6888 exp->write_c_string("}");
6891 // Import an interface type.
6894 Interface_type::do_import(Import* imp)
6896 imp->require_c_string("interface { ");
6898 Typed_identifier_list* methods = new Typed_identifier_list;
6899 while (imp->peek_char() != '}')
6901 std::string name = imp->read_identifier();
6902 imp->require_c_string(" (");
6904 Typed_identifier_list* parameters;
6905 bool is_varargs = false;
6906 if (imp->peek_char() == ')')
6910 parameters = new Typed_identifier_list;
6913 if (imp->match_c_string("..."))
6919 Type* ptype = imp->read_type();
6921 ptype = Type::make_array_type(ptype, NULL);
6922 parameters->push_back(Typed_identifier(Import::import_marker,
6923 ptype, imp->location()));
6924 if (imp->peek_char() != ',')
6926 go_assert(!is_varargs);
6927 imp->require_c_string(", ");
6930 imp->require_c_string(")");
6932 Typed_identifier_list* results;
6933 if (imp->peek_char() != ' ')
6937 results = new Typed_identifier_list;
6939 if (imp->peek_char() != '(')
6941 Type* rtype = imp->read_type();
6942 results->push_back(Typed_identifier(Import::import_marker,
6943 rtype, imp->location()));
6950 Type* rtype = imp->read_type();
6951 results->push_back(Typed_identifier(Import::import_marker,
6952 rtype, imp->location()));
6953 if (imp->peek_char() != ',')
6955 imp->require_c_string(", ");
6957 imp->require_c_string(")");
6961 Function_type* fntype = Type::make_function_type(NULL, parameters,
6965 fntype->set_is_varargs();
6966 methods->push_back(Typed_identifier(name, fntype, imp->location()));
6968 imp->require_c_string("; ");
6971 imp->require_c_string("}");
6973 if (methods->empty())
6979 return Type::make_interface_type(methods, imp->location());
6982 // Make an interface type.
6985 Type::make_interface_type(Typed_identifier_list* methods,
6988 return new Interface_type(methods, location);
6993 // Bind a method to an object.
6996 Method::bind_method(Expression* expr, Location location) const
6998 if (this->stub_ == NULL)
7000 // When there is no stub object, the binding is determined by
7002 return this->do_bind_method(expr, location);
7004 return Expression::make_bound_method(expr, this->stub_, location);
7007 // Return the named object associated with a method. This may only be
7008 // called after methods are finalized.
7011 Method::named_object() const
7013 if (this->stub_ != NULL)
7015 return this->do_named_object();
7018 // Class Named_method.
7020 // The type of the method.
7023 Named_method::do_type() const
7025 if (this->named_object_->is_function())
7026 return this->named_object_->func_value()->type();
7027 else if (this->named_object_->is_function_declaration())
7028 return this->named_object_->func_declaration_value()->type();
7033 // Return the location of the method receiver.
7036 Named_method::do_receiver_location() const
7038 return this->do_type()->receiver()->location();
7041 // Bind a method to an object.
7044 Named_method::do_bind_method(Expression* expr, Location location) const
7046 Named_object* no = this->named_object_;
7047 Bound_method_expression* bme = Expression::make_bound_method(expr, no,
7049 // If this is not a local method, and it does not use a stub, then
7050 // the real method expects a different type. We need to cast the
7052 if (this->depth() > 0 && !this->needs_stub_method())
7054 Function_type* ftype = this->do_type();
7055 go_assert(ftype->is_method());
7056 Type* frtype = ftype->receiver()->type();
7057 bme->set_first_argument_type(frtype);
7062 // Class Interface_method.
7064 // Bind a method to an object.
7067 Interface_method::do_bind_method(Expression* expr,
7068 Location location) const
7070 return Expression::make_interface_field_reference(expr, this->name_,
7076 // Insert a new method. Return true if it was inserted, false
7080 Methods::insert(const std::string& name, Method* m)
7082 std::pair<Method_map::iterator, bool> ins =
7083 this->methods_.insert(std::make_pair(name, m));
7088 Method* old_method = ins.first->second;
7089 if (m->depth() < old_method->depth())
7092 ins.first->second = m;
7097 if (m->depth() == old_method->depth())
7098 old_method->set_is_ambiguous();
7104 // Return the number of unambiguous methods.
7107 Methods::count() const
7110 for (Method_map::const_iterator p = this->methods_.begin();
7111 p != this->methods_.end();
7113 if (!p->second->is_ambiguous())
7118 // Class Named_type.
7120 // Return the name of the type.
7123 Named_type::name() const
7125 return this->named_object_->name();
7128 // Return the name of the type to use in an error message.
7131 Named_type::message_name() const
7133 return this->named_object_->message_name();
7136 // Return the base type for this type. We have to be careful about
7137 // circular type definitions, which are invalid but may be seen here.
7140 Named_type::named_base()
7145 Type* ret = this->type_->base();
7146 this->seen_ = false;
7151 Named_type::named_base() const
7156 const Type* ret = this->type_->base();
7157 this->seen_ = false;
7161 // Return whether this is an error type. We have to be careful about
7162 // circular type definitions, which are invalid but may be seen here.
7165 Named_type::is_named_error_type() const
7170 bool ret = this->type_->is_error_type();
7171 this->seen_ = false;
7175 // Whether this type is comparable. We have to be careful about
7176 // circular type definitions.
7179 Named_type::named_type_is_comparable(std::string* reason) const
7184 bool ret = Type::are_compatible_for_comparison(true, this->type_,
7185 this->type_, reason);
7186 this->seen_ = false;
7190 // Add a method to this type.
7193 Named_type::add_method(const std::string& name, Function* function)
7195 if (this->local_methods_ == NULL)
7196 this->local_methods_ = new Bindings(NULL);
7197 return this->local_methods_->add_function(name, NULL, function);
7200 // Add a method declaration to this type.
7203 Named_type::add_method_declaration(const std::string& name, Package* package,
7204 Function_type* type,
7207 if (this->local_methods_ == NULL)
7208 this->local_methods_ = new Bindings(NULL);
7209 return this->local_methods_->add_function_declaration(name, package, type,
7213 // Add an existing method to this type.
7216 Named_type::add_existing_method(Named_object* no)
7218 if (this->local_methods_ == NULL)
7219 this->local_methods_ = new Bindings(NULL);
7220 this->local_methods_->add_named_object(no);
7223 // Look for a local method NAME, and returns its named object, or NULL
7227 Named_type::find_local_method(const std::string& name) const
7229 if (this->local_methods_ == NULL)
7231 return this->local_methods_->lookup(name);
7234 // Return whether NAME is an unexported field or method, for better
7238 Named_type::is_unexported_local_method(Gogo* gogo,
7239 const std::string& name) const
7241 Bindings* methods = this->local_methods_;
7242 if (methods != NULL)
7244 for (Bindings::const_declarations_iterator p =
7245 methods->begin_declarations();
7246 p != methods->end_declarations();
7249 if (Gogo::is_hidden_name(p->first)
7250 && name == Gogo::unpack_hidden_name(p->first)
7251 && gogo->pack_hidden_name(name, false) != p->first)
7258 // Build the complete list of methods for this type, which means
7259 // recursively including all methods for anonymous fields. Create all
7263 Named_type::finalize_methods(Gogo* gogo)
7265 // If this type needs explicit comparison and hash functions, create
7266 // them now. It would be a bit better to do this only if the
7267 // functions are needed, but they will be static so the backend can
7268 // discard them if they are not used.
7269 if ((this->struct_type() != NULL
7270 || (this->array_type() != NULL && !this->is_slice_type()))
7271 && !this->compare_is_identity()
7272 && this->is_comparable())
7274 Named_object* hash_fn;
7275 Named_object* equal_fn;
7276 this->type_functions(gogo, this, NULL, NULL, &hash_fn, &equal_fn);
7279 if (this->all_methods_ != NULL)
7282 if (this->local_methods_ != NULL
7283 && (this->points_to() != NULL || this->interface_type() != NULL))
7285 const Bindings* lm = this->local_methods_;
7286 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
7287 p != lm->end_declarations();
7289 error_at(p->second->location(),
7290 "invalid pointer or interface receiver type");
7291 delete this->local_methods_;
7292 this->local_methods_ = NULL;
7296 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
7299 // Return the method NAME, or NULL if there isn't one or if it is
7300 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
7304 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
7306 return Type::method_function(this->all_methods_, name, is_ambiguous);
7309 // Return a pointer to the interface method table for this type for
7310 // the interface INTERFACE. IS_POINTER is true if this is for a
7314 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
7317 go_assert(!interface->is_empty());
7319 Interface_method_tables** pimt = (is_pointer
7320 ? &this->interface_method_tables_
7321 : &this->pointer_interface_method_tables_);
7324 *pimt = new Interface_method_tables(5);
7326 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
7327 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
7331 // This is a new entry in the hash table.
7332 go_assert(ins.first->second == NULL_TREE);
7333 ins.first->second = gogo->interface_method_table_for_type(interface,
7338 tree decl = ins.first->second;
7339 if (decl == error_mark_node)
7340 return error_mark_node;
7341 go_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
7342 return build_fold_addr_expr(decl);
7345 // Return whether a named type has any hidden fields.
7348 Named_type::named_type_has_hidden_fields(std::string* reason) const
7353 bool ret = this->type_->has_hidden_fields(this, reason);
7354 this->seen_ = false;
7358 // Look for a use of a complete type within another type. This is
7359 // used to check that we don't try to use a type within itself.
7361 class Find_type_use : public Traverse
7364 Find_type_use(Named_type* find_type)
7365 : Traverse(traverse_types),
7366 find_type_(find_type), found_(false)
7369 // Whether we found the type.
7372 { return this->found_; }
7379 // The type we are looking for.
7380 Named_type* find_type_;
7381 // Whether we found the type.
7385 // Check for FIND_TYPE in TYPE.
7388 Find_type_use::type(Type* type)
7390 if (type->named_type() != NULL && this->find_type_ == type->named_type())
7392 this->found_ = true;
7393 return TRAVERSE_EXIT;
7396 // It's OK if we see a reference to the type in any type which is
7397 // essentially a pointer: a pointer, a slice, a function, a map, or
7399 if (type->points_to() != NULL
7400 || type->is_slice_type()
7401 || type->function_type() != NULL
7402 || type->map_type() != NULL
7403 || type->channel_type() != NULL)
7404 return TRAVERSE_SKIP_COMPONENTS;
7406 // For an interface, a reference to the type in a method type should
7407 // be ignored, but we have to consider direct inheritance. When
7408 // this is called, there may be cases of direct inheritance
7409 // represented as a method with no name.
7410 if (type->interface_type() != NULL)
7412 const Typed_identifier_list* methods = type->interface_type()->methods();
7413 if (methods != NULL)
7415 for (Typed_identifier_list::const_iterator p = methods->begin();
7416 p != methods->end();
7419 if (p->name().empty())
7421 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
7422 return TRAVERSE_EXIT;
7426 return TRAVERSE_SKIP_COMPONENTS;
7429 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
7430 // to convert TYPE to the backend representation before we convert
7432 if (type->named_type() != NULL)
7434 switch (type->base()->classification())
7436 case Type::TYPE_ERROR:
7437 case Type::TYPE_BOOLEAN:
7438 case Type::TYPE_INTEGER:
7439 case Type::TYPE_FLOAT:
7440 case Type::TYPE_COMPLEX:
7441 case Type::TYPE_STRING:
7442 case Type::TYPE_NIL:
7445 case Type::TYPE_ARRAY:
7446 case Type::TYPE_STRUCT:
7447 this->find_type_->add_dependency(type->named_type());
7450 case Type::TYPE_VOID:
7451 case Type::TYPE_SINK:
7452 case Type::TYPE_FUNCTION:
7453 case Type::TYPE_POINTER:
7454 case Type::TYPE_CALL_MULTIPLE_RESULT:
7455 case Type::TYPE_MAP:
7456 case Type::TYPE_CHANNEL:
7457 case Type::TYPE_INTERFACE:
7458 case Type::TYPE_NAMED:
7459 case Type::TYPE_FORWARD:
7465 return TRAVERSE_CONTINUE;
7468 // Verify that a named type does not refer to itself.
7471 Named_type::do_verify()
7473 Find_type_use find(this);
7474 Type::traverse(this->type_, &find);
7477 error_at(this->location_, "invalid recursive type %qs",
7478 this->message_name().c_str());
7479 this->is_error_ = true;
7483 // Check whether any of the local methods overloads an existing
7484 // struct field or interface method. We don't need to check the
7485 // list of methods against itself: that is handled by the Bindings
7487 if (this->local_methods_ != NULL)
7489 Struct_type* st = this->type_->struct_type();
7490 bool found_dup = false;
7493 for (Bindings::const_declarations_iterator p =
7494 this->local_methods_->begin_declarations();
7495 p != this->local_methods_->end_declarations();
7498 const std::string& name(p->first);
7499 if (st != NULL && st->find_local_field(name, NULL) != NULL)
7501 error_at(p->second->location(),
7502 "method %qs redeclares struct field name",
7503 Gogo::message_name(name).c_str());
7515 // Return whether this type is or contains a pointer.
7518 Named_type::do_has_pointer() const
7523 bool ret = this->type_->has_pointer();
7524 this->seen_ = false;
7528 // Return whether comparisons for this type can use the identity
7532 Named_type::do_compare_is_identity() const
7537 bool ret = this->type_->compare_is_identity();
7538 this->seen_ = false;
7542 // Return a hash code. This is used for method lookup. We simply
7543 // hash on the name itself.
7546 Named_type::do_hash_for_method(Gogo* gogo) const
7548 const std::string& name(this->named_object()->name());
7549 unsigned int ret = Type::hash_string(name, 0);
7551 // GOGO will be NULL here when called from Type_hash_identical.
7552 // That is OK because that is only used for internal hash tables
7553 // where we are going to be comparing named types for equality. In
7554 // other cases, which are cases where the runtime is going to
7555 // compare hash codes to see if the types are the same, we need to
7556 // include the package prefix and name in the hash.
7557 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
7559 const Package* package = this->named_object()->package();
7560 if (package == NULL)
7562 ret = Type::hash_string(gogo->unique_prefix(), ret);
7563 ret = Type::hash_string(gogo->package_name(), ret);
7567 ret = Type::hash_string(package->unique_prefix(), ret);
7568 ret = Type::hash_string(package->name(), ret);
7575 // Convert a named type to the backend representation. In order to
7576 // get dependencies right, we fill in a dummy structure for this type,
7577 // then convert all the dependencies, then complete this type. When
7578 // this function is complete, the size of the type is known.
7581 Named_type::convert(Gogo* gogo)
7583 if (this->is_error_ || this->is_converted_)
7586 this->create_placeholder(gogo);
7588 // Convert all the dependencies. If they refer indirectly back to
7589 // this type, they will pick up the intermediate tree we just
7591 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
7592 p != this->dependencies_.end();
7594 (*p)->convert(gogo);
7596 // Complete this type.
7597 Btype* bt = this->named_btype_;
7598 Type* base = this->type_->base();
7599 switch (base->classification())
7616 // The size of these types is already correct. We don't worry
7617 // about filling them in until later, when we also track
7618 // circular references.
7623 std::vector<Backend::Btyped_identifier> bfields;
7624 get_backend_struct_fields(gogo, base->struct_type()->fields(),
7626 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
7627 bt = gogo->backend()->error_type();
7632 // Slice types were completed in create_placeholder.
7633 if (!base->is_slice_type())
7635 Btype* bet = base->array_type()->get_backend_element(gogo);
7636 Bexpression* blen = base->array_type()->get_backend_length(gogo);
7637 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
7638 bt = gogo->backend()->error_type();
7642 case TYPE_INTERFACE:
7643 // Interface types were completed in create_placeholder.
7651 case TYPE_CALL_MULTIPLE_RESULT:
7657 this->named_btype_ = bt;
7658 this->is_converted_ = true;
7659 this->is_placeholder_ = false;
7662 // Create the placeholder for a named type. This is the first step in
7663 // converting to the backend representation.
7666 Named_type::create_placeholder(Gogo* gogo)
7668 if (this->is_error_)
7669 this->named_btype_ = gogo->backend()->error_type();
7671 if (this->named_btype_ != NULL)
7674 // Create the structure for this type. Note that because we call
7675 // base() here, we don't attempt to represent a named type defined
7676 // as another named type. Instead both named types will point to
7677 // different base representations.
7678 Type* base = this->type_->base();
7680 bool set_name = true;
7681 switch (base->classification())
7684 this->is_error_ = true;
7685 this->named_btype_ = gogo->backend()->error_type();
7695 // These are simple basic types, we can just create them
7697 bt = Type::get_named_base_btype(gogo, base);
7702 // All maps and channels have the same backend representation.
7703 bt = Type::get_named_base_btype(gogo, base);
7709 bool for_function = base->classification() == TYPE_FUNCTION;
7710 bt = gogo->backend()->placeholder_pointer_type(this->name(),
7718 bt = gogo->backend()->placeholder_struct_type(this->name(),
7720 this->is_placeholder_ = true;
7725 if (base->is_slice_type())
7726 bt = gogo->backend()->placeholder_struct_type(this->name(),
7730 bt = gogo->backend()->placeholder_array_type(this->name(),
7732 this->is_placeholder_ = true;
7737 case TYPE_INTERFACE:
7738 if (base->interface_type()->is_empty())
7739 bt = Interface_type::get_backend_empty_interface_type(gogo);
7742 bt = gogo->backend()->placeholder_struct_type(this->name(),
7750 case TYPE_CALL_MULTIPLE_RESULT:
7757 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
7759 this->named_btype_ = bt;
7761 if (base->is_slice_type())
7763 // We do not record slices as dependencies of other types,
7764 // because we can fill them in completely here with the final
7766 std::vector<Backend::Btyped_identifier> bfields;
7767 get_backend_slice_fields(gogo, base->array_type(), &bfields);
7768 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
7769 this->named_btype_ = gogo->backend()->error_type();
7771 else if (base->interface_type() != NULL
7772 && !base->interface_type()->is_empty())
7774 // We do not record interfaces as dependencies of other types,
7775 // because we can fill them in completely here with the final
7777 std::vector<Backend::Btyped_identifier> bfields;
7778 get_backend_interface_fields(gogo, base->interface_type(), &bfields);
7779 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
7780 this->named_btype_ = gogo->backend()->error_type();
7784 // Get a tree for a named type.
7787 Named_type::do_get_backend(Gogo* gogo)
7789 if (this->is_error_)
7790 return gogo->backend()->error_type();
7792 Btype* bt = this->named_btype_;
7794 if (!gogo->named_types_are_converted())
7796 // We have not completed converting named types. NAMED_BTYPE_
7797 // is a placeholder and we shouldn't do anything further.
7801 // We don't build dependencies for types whose sizes do not
7802 // change or are not relevant, so we may see them here while
7803 // converting types.
7804 this->create_placeholder(gogo);
7805 bt = this->named_btype_;
7806 go_assert(bt != NULL);
7810 // We are not converting types. This should only be called if the
7811 // type has already been converted.
7812 if (!this->is_converted_)
7814 go_assert(saw_errors());
7815 return gogo->backend()->error_type();
7818 go_assert(bt != NULL);
7820 // Complete the tree.
7821 Type* base = this->type_->base();
7823 switch (base->classification())
7826 return gogo->backend()->error_type();
7839 case TYPE_INTERFACE:
7843 // Don't build a circular data structure. GENERIC can't handle
7845 if (this->seen_in_get_backend_)
7847 this->is_circular_ = true;
7848 return gogo->backend()->circular_pointer_type(bt, true);
7850 this->seen_in_get_backend_ = true;
7851 bt1 = Type::get_named_base_btype(gogo, base);
7852 this->seen_in_get_backend_ = false;
7853 if (this->is_circular_)
7854 bt1 = gogo->backend()->circular_pointer_type(bt, true);
7855 if (!gogo->backend()->set_placeholder_function_type(bt, bt1))
7856 bt = gogo->backend()->error_type();
7860 // Don't build a circular data structure. GENERIC can't handle
7862 if (this->seen_in_get_backend_)
7864 this->is_circular_ = true;
7865 return gogo->backend()->circular_pointer_type(bt, false);
7867 this->seen_in_get_backend_ = true;
7868 bt1 = Type::get_named_base_btype(gogo, base);
7869 this->seen_in_get_backend_ = false;
7870 if (this->is_circular_)
7871 bt1 = gogo->backend()->circular_pointer_type(bt, false);
7872 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
7873 bt = gogo->backend()->error_type();
7878 case TYPE_CALL_MULTIPLE_RESULT:
7887 // Build a type descriptor for a named type.
7890 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7892 // If NAME is not NULL, then we don't really want the type
7893 // descriptor for this type; we want the descriptor for the
7894 // underlying type, giving it the name NAME.
7895 return this->named_type_descriptor(gogo, this->type_,
7896 name == NULL ? this : name);
7899 // Add to the reflection string. This is used mostly for the name of
7900 // the type used in a type descriptor, not for actual reflection
7904 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
7906 if (!Linemap::is_predeclared_location(this->location()))
7908 const Package* package = this->named_object_->package();
7909 if (package != NULL)
7910 ret->append(package->name());
7912 ret->append(gogo->package_name());
7913 ret->push_back('.');
7915 if (this->in_function_ != NULL)
7917 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
7918 ret->push_back('$');
7920 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
7923 // Get the mangled name.
7926 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7928 Named_object* no = this->named_object_;
7930 if (Linemap::is_predeclared_location(this->location()))
7931 go_assert(this->in_function_ == NULL);
7934 const std::string& unique_prefix(no->package() == NULL
7935 ? gogo->unique_prefix()
7936 : no->package()->unique_prefix());
7937 const std::string& package_name(no->package() == NULL
7938 ? gogo->package_name()
7939 : no->package()->name());
7940 name = unique_prefix;
7941 name.append(1, '.');
7942 name.append(package_name);
7943 name.append(1, '.');
7944 if (this->in_function_ != NULL)
7946 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
7947 name.append(1, '$');
7950 name.append(Gogo::unpack_hidden_name(no->name()));
7952 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
7957 // Export the type. This is called to export a global type.
7960 Named_type::export_named_type(Export* exp, const std::string&) const
7962 // We don't need to write the name of the type here, because it will
7963 // be written by Export::write_type anyhow.
7964 exp->write_c_string("type ");
7965 exp->write_type(this);
7966 exp->write_c_string(";\n");
7969 // Import a named type.
7972 Named_type::import_named_type(Import* imp, Named_type** ptype)
7974 imp->require_c_string("type ");
7975 Type *type = imp->read_type();
7976 *ptype = type->named_type();
7977 go_assert(*ptype != NULL);
7978 imp->require_c_string(";\n");
7981 // Export the type when it is referenced by another type. In this
7982 // case Export::export_type will already have issued the name.
7985 Named_type::do_export(Export* exp) const
7987 exp->write_type(this->type_);
7989 // To save space, we only export the methods directly attached to
7991 Bindings* methods = this->local_methods_;
7992 if (methods == NULL)
7995 exp->write_c_string("\n");
7996 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
7997 p != methods->end_definitions();
8000 exp->write_c_string(" ");
8001 (*p)->export_named_object(exp);
8004 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
8005 p != methods->end_declarations();
8008 if (p->second->is_function_declaration())
8010 exp->write_c_string(" ");
8011 p->second->export_named_object(exp);
8016 // Make a named type.
8019 Type::make_named_type(Named_object* named_object, Type* type,
8022 return new Named_type(named_object, type, location);
8025 // Finalize the methods for TYPE. It will be a named type or a struct
8026 // type. This sets *ALL_METHODS to the list of methods, and builds
8027 // all required stubs.
8030 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
8031 Methods** all_methods)
8033 *all_methods = NULL;
8034 Types_seen types_seen;
8035 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
8037 Type::build_stub_methods(gogo, type, *all_methods, location);
8040 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8041 // build up the struct field indexes as we go. DEPTH is the depth of
8042 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8043 // adding these methods for an anonymous field with pointer type.
8044 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8045 // calls the real method. TYPES_SEEN is used to avoid infinite
8049 Type::add_methods_for_type(const Type* type,
8050 const Method::Field_indexes* field_indexes,
8052 bool is_embedded_pointer,
8053 bool needs_stub_method,
8054 Types_seen* types_seen,
8057 // Pointer types may not have methods.
8058 if (type->points_to() != NULL)
8061 const Named_type* nt = type->named_type();
8064 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
8070 Type::add_local_methods_for_type(nt, field_indexes, depth,
8071 is_embedded_pointer, needs_stub_method,
8074 Type::add_embedded_methods_for_type(type, field_indexes, depth,
8075 is_embedded_pointer, needs_stub_method,
8076 types_seen, methods);
8078 // If we are called with depth > 0, then we are looking at an
8079 // anonymous field of a struct. If such a field has interface type,
8080 // then we need to add the interface methods. We don't want to add
8081 // them when depth == 0, because we will already handle them
8082 // following the usual rules for an interface type.
8084 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
8087 // Add the local methods for the named type NT to *METHODS. The
8088 // parameters are as for add_methods_to_type.
8091 Type::add_local_methods_for_type(const Named_type* nt,
8092 const Method::Field_indexes* field_indexes,
8094 bool is_embedded_pointer,
8095 bool needs_stub_method,
8098 const Bindings* local_methods = nt->local_methods();
8099 if (local_methods == NULL)
8102 if (*methods == NULL)
8103 *methods = new Methods();
8105 for (Bindings::const_declarations_iterator p =
8106 local_methods->begin_declarations();
8107 p != local_methods->end_declarations();
8110 Named_object* no = p->second;
8111 bool is_value_method = (is_embedded_pointer
8112 || !Type::method_expects_pointer(no));
8113 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
8115 || (depth > 0 && is_value_method)));
8116 if (!(*methods)->insert(no->name(), m))
8121 // Add the embedded methods for TYPE to *METHODS. These are the
8122 // methods attached to anonymous fields. The parameters are as for
8123 // add_methods_to_type.
8126 Type::add_embedded_methods_for_type(const Type* type,
8127 const Method::Field_indexes* field_indexes,
8129 bool is_embedded_pointer,
8130 bool needs_stub_method,
8131 Types_seen* types_seen,
8134 // Look for anonymous fields in TYPE. TYPE has fields if it is a
8136 const Struct_type* st = type->struct_type();
8140 const Struct_field_list* fields = st->fields();
8145 for (Struct_field_list::const_iterator pf = fields->begin();
8146 pf != fields->end();
8149 if (!pf->is_anonymous())
8152 Type* ftype = pf->type();
8153 bool is_pointer = false;
8154 if (ftype->points_to() != NULL)
8156 ftype = ftype->points_to();
8159 Named_type* fnt = ftype->named_type();
8162 // This is an error, but it will be diagnosed elsewhere.
8166 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
8167 sub_field_indexes->next = field_indexes;
8168 sub_field_indexes->field_index = i;
8170 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
8171 (is_embedded_pointer || is_pointer),
8180 // If TYPE is an interface type, then add its method to *METHODS.
8181 // This is for interface methods attached to an anonymous field. The
8182 // parameters are as for add_methods_for_type.
8185 Type::add_interface_methods_for_type(const Type* type,
8186 const Method::Field_indexes* field_indexes,
8190 const Interface_type* it = type->interface_type();
8194 const Typed_identifier_list* imethods = it->methods();
8195 if (imethods == NULL)
8198 if (*methods == NULL)
8199 *methods = new Methods();
8201 for (Typed_identifier_list::const_iterator pm = imethods->begin();
8202 pm != imethods->end();
8205 Function_type* fntype = pm->type()->function_type();
8208 // This is an error, but it should be reported elsewhere
8209 // when we look at the methods for IT.
8212 go_assert(!fntype->is_method());
8213 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
8214 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
8215 field_indexes, depth);
8216 if (!(*methods)->insert(pm->name(), m))
8221 // Build stub methods for TYPE as needed. METHODS is the set of
8222 // methods for the type. A stub method may be needed when a type
8223 // inherits a method from an anonymous field. When we need the
8224 // address of the method, as in a type descriptor, we need to build a
8225 // little stub which does the required field dereferences and jumps to
8226 // the real method. LOCATION is the location of the type definition.
8229 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
8232 if (methods == NULL)
8234 for (Methods::const_iterator p = methods->begin();
8235 p != methods->end();
8238 Method* m = p->second;
8239 if (m->is_ambiguous() || !m->needs_stub_method())
8242 const std::string& name(p->first);
8244 // Build a stub method.
8246 const Function_type* fntype = m->type();
8248 static unsigned int counter;
8250 snprintf(buf, sizeof buf, "$this%u", counter);
8253 Type* receiver_type = const_cast<Type*>(type);
8254 if (!m->is_value_method())
8255 receiver_type = Type::make_pointer_type(receiver_type);
8256 Location receiver_location = m->receiver_location();
8257 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
8260 const Typed_identifier_list* fnparams = fntype->parameters();
8261 Typed_identifier_list* stub_params;
8262 if (fnparams == NULL || fnparams->empty())
8266 // We give each stub parameter a unique name.
8267 stub_params = new Typed_identifier_list();
8268 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
8269 pp != fnparams->end();
8273 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
8274 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
8280 const Typed_identifier_list* fnresults = fntype->results();
8281 Typed_identifier_list* stub_results;
8282 if (fnresults == NULL || fnresults->empty())
8283 stub_results = NULL;
8286 // We create the result parameters without any names, since
8287 // we won't refer to them.
8288 stub_results = new Typed_identifier_list();
8289 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
8290 pr != fnresults->end();
8292 stub_results->push_back(Typed_identifier("", pr->type(),
8296 Function_type* stub_type = Type::make_function_type(receiver,
8299 fntype->location());
8300 if (fntype->is_varargs())
8301 stub_type->set_is_varargs();
8303 // We only create the function in the package which creates the
8305 const Package* package;
8306 if (type->named_type() == NULL)
8309 package = type->named_type()->named_object()->package();
8311 if (package != NULL)
8312 stub = Named_object::make_function_declaration(name, package,
8313 stub_type, location);
8316 stub = gogo->start_function(name, stub_type, false,
8317 fntype->location());
8318 Type::build_one_stub_method(gogo, m, buf, stub_params,
8319 fntype->is_varargs(), location);
8320 gogo->finish_function(fntype->location());
8323 m->set_stub_object(stub);
8327 // Build a stub method which adjusts the receiver as required to call
8328 // METHOD. RECEIVER_NAME is the name we used for the receiver.
8329 // PARAMS is the list of function parameters.
8332 Type::build_one_stub_method(Gogo* gogo, Method* method,
8333 const char* receiver_name,
8334 const Typed_identifier_list* params,
8338 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
8339 go_assert(receiver_object != NULL);
8341 Expression* expr = Expression::make_var_reference(receiver_object, location);
8342 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
8343 if (expr->type()->points_to() == NULL)
8344 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8346 Expression_list* arguments;
8347 if (params == NULL || params->empty())
8351 arguments = new Expression_list();
8352 for (Typed_identifier_list::const_iterator p = params->begin();
8356 Named_object* param = gogo->lookup(p->name(), NULL);
8357 go_assert(param != NULL);
8358 Expression* param_ref = Expression::make_var_reference(param,
8360 arguments->push_back(param_ref);
8364 Expression* func = method->bind_method(expr, location);
8365 go_assert(func != NULL);
8366 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
8368 call->set_hidden_fields_are_ok();
8369 size_t count = call->result_count();
8371 gogo->add_statement(Statement::make_statement(call, true));
8374 Expression_list* retvals = new Expression_list();
8376 retvals->push_back(call);
8379 for (size_t i = 0; i < count; ++i)
8380 retvals->push_back(Expression::make_call_result(call, i));
8382 Return_statement* retstat = Statement::make_return_statement(retvals,
8385 // We can return values with hidden fields from a stub. This is
8386 // necessary if the method is itself hidden.
8387 retstat->set_hidden_fields_are_ok();
8389 gogo->add_statement(retstat);
8393 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
8394 // in reverse order.
8397 Type::apply_field_indexes(Expression* expr,
8398 const Method::Field_indexes* field_indexes,
8401 if (field_indexes == NULL)
8403 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
8404 Struct_type* stype = expr->type()->deref()->struct_type();
8405 go_assert(stype != NULL
8406 && field_indexes->field_index < stype->field_count());
8407 if (expr->type()->struct_type() == NULL)
8409 go_assert(expr->type()->points_to() != NULL);
8410 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8411 go_assert(expr->type()->struct_type() == stype);
8413 return Expression::make_field_reference(expr, field_indexes->field_index,
8417 // Return whether NO is a method for which the receiver is a pointer.
8420 Type::method_expects_pointer(const Named_object* no)
8422 const Function_type *fntype;
8423 if (no->is_function())
8424 fntype = no->func_value()->type();
8425 else if (no->is_function_declaration())
8426 fntype = no->func_declaration_value()->type();
8429 return fntype->receiver()->type()->points_to() != NULL;
8432 // Given a set of methods for a type, METHODS, return the method NAME,
8433 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
8434 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
8435 // but is ambiguous (and return NULL).
8438 Type::method_function(const Methods* methods, const std::string& name,
8441 if (is_ambiguous != NULL)
8442 *is_ambiguous = false;
8443 if (methods == NULL)
8445 Methods::const_iterator p = methods->find(name);
8446 if (p == methods->end())
8448 Method* m = p->second;
8449 if (m->is_ambiguous())
8451 if (is_ambiguous != NULL)
8452 *is_ambiguous = true;
8458 // Look for field or method NAME for TYPE. Return an Expression for
8459 // the field or method bound to EXPR. If there is no such field or
8460 // method, give an appropriate error and return an error expression.
8463 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
8464 const std::string& name,
8467 if (type->deref()->is_error_type())
8468 return Expression::make_error(location);
8470 const Named_type* nt = type->deref()->named_type();
8471 const Struct_type* st = type->deref()->struct_type();
8472 const Interface_type* it = type->interface_type();
8474 // If this is a pointer to a pointer, then it is possible that the
8475 // pointed-to type has methods.
8479 && type->points_to() != NULL
8480 && type->points_to()->points_to() != NULL)
8482 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8483 type = type->points_to();
8484 if (type->deref()->is_error_type())
8485 return Expression::make_error(location);
8486 nt = type->points_to()->named_type();
8487 st = type->points_to()->struct_type();
8490 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
8491 || expr->is_addressable());
8492 std::vector<const Named_type*> seen;
8493 bool is_method = false;
8494 bool found_pointer_method = false;
8497 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
8498 &seen, NULL, &is_method,
8499 &found_pointer_method, &ambig1, &ambig2))
8504 go_assert(st != NULL);
8505 if (type->struct_type() == NULL)
8507 go_assert(type->points_to() != NULL);
8508 expr = Expression::make_unary(OPERATOR_MULT, expr,
8510 go_assert(expr->type()->struct_type() == st);
8512 ret = st->field_reference(expr, name, location);
8514 else if (it != NULL && it->find_method(name) != NULL)
8515 ret = Expression::make_interface_field_reference(expr, name,
8521 m = nt->method_function(name, NULL);
8522 else if (st != NULL)
8523 m = st->method_function(name, NULL);
8526 go_assert(m != NULL);
8527 if (!m->is_value_method() && expr->type()->points_to() == NULL)
8528 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8529 ret = m->bind_method(expr, location);
8531 go_assert(ret != NULL);
8536 if (!ambig1.empty())
8537 error_at(location, "%qs is ambiguous via %qs and %qs",
8538 Gogo::message_name(name).c_str(), ambig1.c_str(),
8540 else if (found_pointer_method)
8541 error_at(location, "method requires a pointer");
8542 else if (nt == NULL && st == NULL && it == NULL)
8544 ("reference to field %qs in object which "
8545 "has no fields or methods"),
8546 Gogo::message_name(name).c_str());
8550 if (!Gogo::is_hidden_name(name))
8551 is_unexported = false;
8554 std::string unpacked = Gogo::unpack_hidden_name(name);
8556 is_unexported = Type::is_unexported_field_or_method(gogo, type,
8561 error_at(location, "reference to unexported field or method %qs",
8562 Gogo::message_name(name).c_str());
8564 error_at(location, "reference to undefined field or method %qs",
8565 Gogo::message_name(name).c_str());
8567 return Expression::make_error(location);
8571 // Look in TYPE for a field or method named NAME, return true if one
8572 // is found. This looks through embedded anonymous fields and handles
8573 // ambiguity. If a method is found, sets *IS_METHOD to true;
8574 // otherwise, if a field is found, set it to false. If
8575 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
8576 // whose address can not be taken. SEEN is used to avoid infinite
8577 // recursion on invalid types.
8579 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
8580 // method we couldn't use because it requires a pointer. LEVEL is
8581 // used for recursive calls, and can be NULL for a non-recursive call.
8582 // When this function returns false because it finds that the name is
8583 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
8584 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
8585 // will be unchanged.
8587 // This function just returns whether or not there is a field or
8588 // method, and whether it is a field or method. It doesn't build an
8589 // expression to refer to it. If it is a method, we then look in the
8590 // list of all methods for the type. If it is a field, the search has
8591 // to be done again, looking only for fields, and building up the
8592 // expression as we go.
8595 Type::find_field_or_method(const Type* type,
8596 const std::string& name,
8597 bool receiver_can_be_pointer,
8598 std::vector<const Named_type*>* seen,
8601 bool* found_pointer_method,
8602 std::string* ambig1,
8603 std::string* ambig2)
8605 // Named types can have locally defined methods.
8606 const Named_type* nt = type->named_type();
8607 if (nt == NULL && type->points_to() != NULL)
8608 nt = type->points_to()->named_type();
8611 Named_object* no = nt->find_local_method(name);
8614 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
8620 // Record that we have found a pointer method in order to
8621 // give a better error message if we don't find anything
8623 *found_pointer_method = true;
8626 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8632 // We've already seen this type when searching for methods.
8638 // Interface types can have methods.
8639 const Interface_type* it = type->interface_type();
8640 if (it != NULL && it->find_method(name) != NULL)
8646 // Struct types can have fields. They can also inherit fields and
8647 // methods from anonymous fields.
8648 const Struct_type* st = type->deref()->struct_type();
8651 const Struct_field_list* fields = st->fields();
8656 seen->push_back(nt);
8658 int found_level = 0;
8659 bool found_is_method = false;
8660 std::string found_ambig1;
8661 std::string found_ambig2;
8662 const Struct_field* found_parent = NULL;
8663 for (Struct_field_list::const_iterator pf = fields->begin();
8664 pf != fields->end();
8667 if (pf->is_field_name(name))
8675 if (!pf->is_anonymous())
8678 if (pf->type()->deref()->is_error_type()
8679 || pf->type()->deref()->is_undefined())
8682 Named_type* fnt = pf->type()->named_type();
8684 fnt = pf->type()->deref()->named_type();
8685 go_assert(fnt != NULL);
8687 int sublevel = level == NULL ? 1 : *level + 1;
8689 std::string subambig1;
8690 std::string subambig2;
8691 bool subfound = Type::find_field_or_method(fnt,
8693 receiver_can_be_pointer,
8697 found_pointer_method,
8702 if (!subambig1.empty())
8704 // The name was found via this field, but is ambiguous.
8705 // if the ambiguity is lower or at the same level as
8706 // anything else we have already found, then we want to
8707 // pass the ambiguity back to the caller.
8708 if (found_level == 0 || sublevel <= found_level)
8710 found_ambig1 = (Gogo::message_name(pf->field_name())
8712 found_ambig2 = (Gogo::message_name(pf->field_name())
8714 found_level = sublevel;
8720 // The name was found via this field. Use the level to see
8721 // if we want to use this one, or whether it introduces an
8723 if (found_level == 0 || sublevel < found_level)
8725 found_level = sublevel;
8726 found_is_method = sub_is_method;
8727 found_ambig1.clear();
8728 found_ambig2.clear();
8729 found_parent = &*pf;
8731 else if (sublevel > found_level)
8733 else if (found_ambig1.empty())
8735 // We found an ambiguity.
8736 go_assert(found_parent != NULL);
8737 found_ambig1 = Gogo::message_name(found_parent->field_name());
8738 found_ambig2 = Gogo::message_name(pf->field_name());
8742 // We found an ambiguity, but we already know of one.
8743 // Just report the earlier one.
8748 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
8749 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
8750 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
8751 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
8756 if (found_level == 0)
8758 else if (!found_ambig1.empty())
8760 go_assert(!found_ambig1.empty());
8761 ambig1->assign(found_ambig1);
8762 ambig2->assign(found_ambig2);
8764 *level = found_level;
8770 *level = found_level;
8771 *is_method = found_is_method;
8776 // Return whether NAME is an unexported field or method for TYPE.
8779 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
8780 const std::string& name,
8781 std::vector<const Named_type*>* seen)
8783 const Named_type* nt = type->named_type();
8785 nt = type->deref()->named_type();
8788 if (nt->is_unexported_local_method(gogo, name))
8791 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8797 // We've already seen this type.
8803 const Interface_type* it = type->interface_type();
8804 if (it != NULL && it->is_unexported_method(gogo, name))
8807 type = type->deref();
8809 const Struct_type* st = type->struct_type();
8810 if (st != NULL && st->is_unexported_local_field(gogo, name))
8816 const Struct_field_list* fields = st->fields();
8821 seen->push_back(nt);
8823 for (Struct_field_list::const_iterator pf = fields->begin();
8824 pf != fields->end();
8827 if (pf->is_anonymous()
8828 && !pf->type()->deref()->is_error_type()
8829 && !pf->type()->deref()->is_undefined())
8831 Named_type* subtype = pf->type()->named_type();
8832 if (subtype == NULL)
8833 subtype = pf->type()->deref()->named_type();
8834 if (subtype == NULL)
8836 // This is an error, but it will be diagnosed elsewhere.
8839 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
8854 // Class Forward_declaration.
8856 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
8857 : Type(TYPE_FORWARD),
8858 named_object_(named_object->resolve()), warned_(false)
8860 go_assert(this->named_object_->is_unknown()
8861 || this->named_object_->is_type_declaration());
8864 // Return the named object.
8867 Forward_declaration_type::named_object()
8869 return this->named_object_->resolve();
8873 Forward_declaration_type::named_object() const
8875 return this->named_object_->resolve();
8878 // Return the name of the forward declared type.
8881 Forward_declaration_type::name() const
8883 return this->named_object()->name();
8886 // Warn about a use of a type which has been declared but not defined.
8889 Forward_declaration_type::warn() const
8891 Named_object* no = this->named_object_->resolve();
8892 if (no->is_unknown())
8894 // The name was not defined anywhere.
8897 error_at(this->named_object_->location(),
8898 "use of undefined type %qs",
8899 no->message_name().c_str());
8900 this->warned_ = true;
8903 else if (no->is_type_declaration())
8905 // The name was seen as a type, but the type was never defined.
8906 if (no->type_declaration_value()->using_type())
8908 error_at(this->named_object_->location(),
8909 "use of undefined type %qs",
8910 no->message_name().c_str());
8911 this->warned_ = true;
8916 // The name was defined, but not as a type.
8919 error_at(this->named_object_->location(), "expected type");
8920 this->warned_ = true;
8925 // Get the base type of a declaration. This gives an error if the
8926 // type has not yet been defined.
8929 Forward_declaration_type::real_type()
8931 if (this->is_defined())
8932 return this->named_object()->type_value();
8936 return Type::make_error_type();
8941 Forward_declaration_type::real_type() const
8943 if (this->is_defined())
8944 return this->named_object()->type_value();
8948 return Type::make_error_type();
8952 // Return whether the base type is defined.
8955 Forward_declaration_type::is_defined() const
8957 return this->named_object()->is_type();
8960 // Add a method. This is used when methods are defined before the
8964 Forward_declaration_type::add_method(const std::string& name,
8967 Named_object* no = this->named_object();
8968 if (no->is_unknown())
8969 no->declare_as_type();
8970 return no->type_declaration_value()->add_method(name, function);
8973 // Add a method declaration. This is used when methods are declared
8977 Forward_declaration_type::add_method_declaration(const std::string& name,
8978 Function_type* type,
8981 Named_object* no = this->named_object();
8982 if (no->is_unknown())
8983 no->declare_as_type();
8984 Type_declaration* td = no->type_declaration_value();
8985 return td->add_method_declaration(name, type, location);
8991 Forward_declaration_type::do_traverse(Traverse* traverse)
8993 if (this->is_defined()
8994 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
8995 return TRAVERSE_EXIT;
8996 return TRAVERSE_CONTINUE;
8999 // Get the backend representation for the type.
9002 Forward_declaration_type::do_get_backend(Gogo* gogo)
9004 if (this->is_defined())
9005 return Type::get_named_base_btype(gogo, this->real_type());
9008 return gogo->backend()->error_type();
9010 // We represent an undefined type as a struct with no fields. That
9011 // should work fine for the backend, since the same case can arise
9013 std::vector<Backend::Btyped_identifier> fields;
9014 Btype* bt = gogo->backend()->struct_type(fields);
9015 return gogo->backend()->named_type(this->name(), bt,
9016 this->named_object()->location());
9019 // Build a type descriptor for a forwarded type.
9022 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9024 Location ploc = Linemap::predeclared_location();
9025 if (!this->is_defined())
9026 return Expression::make_nil(ploc);
9029 Type* t = this->real_type();
9031 return this->named_type_descriptor(gogo, t, name);
9033 return Expression::make_type_descriptor(t, ploc);
9037 // The reflection string.
9040 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
9042 this->append_reflection(this->real_type(), gogo, ret);
9045 // The mangled name.
9048 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
9050 if (this->is_defined())
9051 this->append_mangled_name(this->real_type(), gogo, ret);
9054 const Named_object* no = this->named_object();
9056 if (no->package() == NULL)
9057 name = gogo->package_name();
9059 name = no->package()->name();
9061 name += Gogo::unpack_hidden_name(no->name());
9063 snprintf(buf, sizeof buf, "N%u_",
9064 static_cast<unsigned int>(name.length()));
9070 // Export a forward declaration. This can happen when a defined type
9071 // refers to a type which is only declared (and is presumably defined
9072 // in some other file in the same package).
9075 Forward_declaration_type::do_export(Export*) const
9077 // If there is a base type, that should be exported instead of this.
9078 go_assert(!this->is_defined());
9080 // We don't output anything.
9083 // Make a forward declaration.
9086 Type::make_forward_declaration(Named_object* named_object)
9088 return new Forward_declaration_type(named_object);
9091 // Class Typed_identifier_list.
9093 // Sort the entries by name.
9095 struct Typed_identifier_list_sort
9099 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
9100 { return t1.name() < t2.name(); }
9104 Typed_identifier_list::sort_by_name()
9106 std::sort(this->entries_.begin(), this->entries_.end(),
9107 Typed_identifier_list_sort());
9113 Typed_identifier_list::traverse(Traverse* traverse)
9115 for (Typed_identifier_list::const_iterator p = this->begin();
9119 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
9120 return TRAVERSE_EXIT;
9122 return TRAVERSE_CONTINUE;
9127 Typed_identifier_list*
9128 Typed_identifier_list::copy() const
9130 Typed_identifier_list* ret = new Typed_identifier_list();
9131 for (Typed_identifier_list::const_iterator p = this->begin();
9134 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));