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