1 // types.cc -- Go frontend types.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
23 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
31 #include "statements.h"
39 Type::Type(Type_classification classification)
40 : classification_(classification), btype_(NULL), type_descriptor_var_(NULL)
48 // Get the base type for a type--skip names and forward declarations.
53 switch (this->classification_)
56 return this->named_type()->named_base();
58 return this->forward_declaration_type()->real_type()->base();
67 switch (this->classification_)
70 return this->named_type()->named_base();
72 return this->forward_declaration_type()->real_type()->base();
78 // Skip defined forward declarations.
84 Forward_declaration_type* ftype = t->forward_declaration_type();
85 while (ftype != NULL && ftype->is_defined())
87 t = ftype->real_type();
88 ftype = t->forward_declaration_type();
94 Type::forwarded() const
97 const Forward_declaration_type* ftype = t->forward_declaration_type();
98 while (ftype != NULL && ftype->is_defined())
100 t = ftype->real_type();
101 ftype = t->forward_declaration_type();
106 // If this is a named type, return it. Otherwise, return NULL.
111 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
115 Type::named_type() const
117 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
120 // Return true if this type is not defined.
123 Type::is_undefined() const
125 return this->forwarded()->forward_declaration_type() != NULL;
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
132 Type::is_basic_type() const
134 switch (this->classification_)
157 return this->base()->is_basic_type();
164 // Return true if this is an abstract type.
167 Type::is_abstract() const
169 switch (this->classification())
172 return this->integer_type()->is_abstract();
174 return this->float_type()->is_abstract();
176 return this->complex_type()->is_abstract();
178 return this->is_abstract_string_type();
180 return this->is_abstract_boolean_type();
186 // Return a non-abstract version of an abstract type.
189 Type::make_non_abstract_type()
191 go_assert(this->is_abstract());
192 switch (this->classification())
195 return Type::lookup_integer_type("int");
197 return Type::lookup_float_type("float64");
199 return Type::lookup_complex_type("complex128");
201 return Type::lookup_string_type();
203 return Type::lookup_bool_type();
209 // Return true if this is an error type. Don't give an error if we
210 // try to dereference an undefined forwarding type, as this is called
211 // in the parser when the type may legitimately be undefined.
214 Type::is_error_type() const
216 const Type* t = this->forwarded();
217 // Note that we return false for an undefined forward type.
218 switch (t->classification_)
223 return t->named_type()->is_named_error_type();
229 // If this is a pointer type, return the type to which it points.
230 // Otherwise, return NULL.
233 Type::points_to() const
235 const Pointer_type* ptype = this->convert<const Pointer_type,
237 return ptype == NULL ? NULL : ptype->points_to();
240 // Return whether this is an open array type.
243 Type::is_slice_type() const
245 return this->array_type() != NULL && this->array_type()->length() == NULL;
248 // Return whether this is the predeclared constant nil being used as a
252 Type::is_nil_constant_as_type() const
254 const Type* t = this->forwarded();
255 if (t->forward_declaration_type() != NULL)
257 const Named_object* no = t->forward_declaration_type()->named_object();
258 if (no->is_unknown())
259 no = no->unknown_value()->real_named_object();
262 && no->const_value()->expr()->is_nil_expression())
271 Type::traverse(Type* type, Traverse* traverse)
273 go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
274 || (traverse->traverse_mask()
275 & Traverse::traverse_expressions) != 0);
276 if (traverse->remember_type(type))
278 // We have already traversed this type.
279 return TRAVERSE_CONTINUE;
281 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
283 int t = traverse->type(type);
284 if (t == TRAVERSE_EXIT)
285 return TRAVERSE_EXIT;
286 else if (t == TRAVERSE_SKIP_COMPONENTS)
287 return TRAVERSE_CONTINUE;
289 // An array type has an expression which we need to traverse if
290 // traverse_expressions is set.
291 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
292 return TRAVERSE_EXIT;
293 return TRAVERSE_CONTINUE;
296 // Default implementation for do_traverse for child class.
299 Type::do_traverse(Traverse*)
301 return TRAVERSE_CONTINUE;
304 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
305 // then return true for all erroneous types; this is used to avoid
306 // cascading errors. If REASON is not NULL, optionally set *REASON to
307 // the reason the types are not identical.
310 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
313 if (t1 == NULL || t2 == NULL)
315 // Something is wrong.
316 return errors_are_identical ? true : t1 == t2;
319 // Skip defined forward declarations.
320 t1 = t1->forwarded();
321 t2 = t2->forwarded();
326 // An undefined forward declaration is an error.
327 if (t1->forward_declaration_type() != NULL
328 || t2->forward_declaration_type() != NULL)
329 return errors_are_identical;
331 // Avoid cascading errors with error types.
332 if (t1->is_error_type() || t2->is_error_type())
334 if (errors_are_identical)
336 return t1->is_error_type() && t2->is_error_type();
339 // Get a good reason for the sink type. Note that the sink type on
340 // the left hand side of an assignment is handled in are_assignable.
341 if (t1->is_sink_type() || t2->is_sink_type())
344 *reason = "invalid use of _";
348 // A named type is only identical to itself.
349 if (t1->named_type() != NULL || t2->named_type() != NULL)
352 // Check type shapes.
353 if (t1->classification() != t2->classification())
356 switch (t1->classification())
362 // These types are always identical.
366 return t1->integer_type()->is_identical(t2->integer_type());
369 return t1->float_type()->is_identical(t2->float_type());
372 return t1->complex_type()->is_identical(t2->complex_type());
375 return t1->function_type()->is_identical(t2->function_type(),
377 errors_are_identical,
381 return Type::are_identical(t1->points_to(), t2->points_to(),
382 errors_are_identical, reason);
385 return t1->struct_type()->is_identical(t2->struct_type(),
386 errors_are_identical);
389 return t1->array_type()->is_identical(t2->array_type(),
390 errors_are_identical);
393 return t1->map_type()->is_identical(t2->map_type(),
394 errors_are_identical);
397 return t1->channel_type()->is_identical(t2->channel_type(),
398 errors_are_identical);
401 return t1->interface_type()->is_identical(t2->interface_type(),
402 errors_are_identical);
404 case TYPE_CALL_MULTIPLE_RESULT:
406 *reason = "invalid use of multiple value function call";
414 // Return true if it's OK to have a binary operation with types LHS
415 // and RHS. This is not used for shifts or comparisons.
418 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
420 if (Type::are_identical(lhs, rhs, true, NULL))
423 // A constant of abstract bool type may be mixed with any bool type.
424 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
425 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
428 // A constant of abstract string type may be mixed with any string
430 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
431 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
437 // A constant of abstract integer, float, or complex type may be
438 // mixed with an integer, float, or complex type.
439 if ((rhs->is_abstract()
440 && (rhs->integer_type() != NULL
441 || rhs->float_type() != NULL
442 || rhs->complex_type() != NULL)
443 && (lhs->integer_type() != NULL
444 || lhs->float_type() != NULL
445 || lhs->complex_type() != NULL))
446 || (lhs->is_abstract()
447 && (lhs->integer_type() != NULL
448 || lhs->float_type() != NULL
449 || lhs->complex_type() != NULL)
450 && (rhs->integer_type() != NULL
451 || rhs->float_type() != NULL
452 || rhs->complex_type() != NULL)))
455 // The nil type may be compared to a pointer, an interface type, a
456 // slice type, a channel type, a map type, or a function type.
457 if (lhs->is_nil_type()
458 && (rhs->points_to() != NULL
459 || rhs->interface_type() != NULL
460 || rhs->is_slice_type()
461 || rhs->map_type() != NULL
462 || rhs->channel_type() != NULL
463 || rhs->function_type() != NULL))
465 if (rhs->is_nil_type()
466 && (lhs->points_to() != NULL
467 || lhs->interface_type() != NULL
468 || lhs->is_slice_type()
469 || lhs->map_type() != NULL
470 || lhs->channel_type() != NULL
471 || lhs->function_type() != NULL))
477 // Return true if a value with type T1 may be compared with a value of
478 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
481 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
482 const Type *t2, std::string *reason)
485 && !Type::are_assignable(t1, t2, NULL)
486 && !Type::are_assignable(t2, t1, NULL))
489 *reason = "incompatible types in binary expression";
495 if (t1->integer_type() == NULL
496 && t1->float_type() == NULL
497 && !t1->is_string_type())
500 *reason = _("invalid comparison of non-ordered type");
504 else if (t1->is_slice_type()
505 || t1->map_type() != NULL
506 || t1->function_type() != NULL
507 || t2->is_slice_type()
508 || t2->map_type() != NULL
509 || t2->function_type() != NULL)
511 if (!t1->is_nil_type() && !t2->is_nil_type())
515 if (t1->is_slice_type() || t2->is_slice_type())
516 *reason = _("slice can only be compared to nil");
517 else if (t1->map_type() != NULL || t2->map_type() != NULL)
518 *reason = _("map can only be compared to nil");
520 *reason = _("func can only be compared to nil");
522 // Match 6g error messages.
523 if (t1->interface_type() != NULL || t2->interface_type() != NULL)
526 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
536 if (!t1->is_boolean_type()
537 && t1->integer_type() == NULL
538 && t1->float_type() == NULL
539 && t1->complex_type() == NULL
540 && !t1->is_string_type()
541 && t1->points_to() == NULL
542 && t1->channel_type() == NULL
543 && t1->interface_type() == NULL
544 && t1->struct_type() == NULL
545 && t1->array_type() == NULL
546 && !t1->is_nil_type())
549 *reason = _("invalid comparison of non-comparable type");
553 if (t1->named_type() != NULL)
554 return t1->named_type()->named_type_is_comparable(reason);
555 else if (t2->named_type() != NULL)
556 return t2->named_type()->named_type_is_comparable(reason);
557 else if (t1->struct_type() != NULL)
559 const Struct_field_list* fields = t1->struct_type()->fields();
560 for (Struct_field_list::const_iterator p = fields->begin();
564 if (!p->type()->is_comparable())
567 *reason = _("invalid comparison of non-comparable struct");
572 else if (t1->array_type() != NULL)
574 if (t1->array_type()->length()->is_nil_expression()
575 || !t1->array_type()->element_type()->is_comparable())
578 *reason = _("invalid comparison of non-comparable array");
587 // Return true if a value with type RHS may be assigned to a variable
588 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
589 // hidden fields are modified. If REASON is not NULL, set *REASON to
590 // the reason the types are not assignable.
593 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
594 bool check_hidden_fields,
597 // Do some checks first. Make sure the types are defined.
599 && rhs->forwarded()->forward_declaration_type() == NULL
600 && rhs->is_void_type())
603 *reason = "non-value used as value";
607 if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
609 // Any value may be assigned to the blank identifier.
610 if (lhs->is_sink_type())
613 // All fields of a struct must be exported, or the assignment
614 // must be in the same package.
615 if (check_hidden_fields
617 && rhs->forwarded()->forward_declaration_type() == NULL)
619 if (lhs->has_hidden_fields(NULL, reason)
620 || rhs->has_hidden_fields(NULL, reason))
625 // Identical types are assignable.
626 if (Type::are_identical(lhs, rhs, true, reason))
629 // The types are assignable if they have identical underlying types
630 // and either LHS or RHS is not a named type.
631 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
632 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
633 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
636 // The types are assignable if LHS is an interface type and RHS
637 // implements the required methods.
638 const Interface_type* lhs_interface_type = lhs->interface_type();
639 if (lhs_interface_type != NULL)
641 if (lhs_interface_type->implements_interface(rhs, reason))
643 const Interface_type* rhs_interface_type = rhs->interface_type();
644 if (rhs_interface_type != NULL
645 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
650 // The type are assignable if RHS is a bidirectional channel type,
651 // LHS is a channel type, they have identical element types, and
652 // either LHS or RHS is not a named type.
653 if (lhs->channel_type() != NULL
654 && rhs->channel_type() != NULL
655 && rhs->channel_type()->may_send()
656 && rhs->channel_type()->may_receive()
657 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
658 && Type::are_identical(lhs->channel_type()->element_type(),
659 rhs->channel_type()->element_type(),
664 // The nil type may be assigned to a pointer, function, slice, map,
665 // channel, or interface type.
666 if (rhs->is_nil_type()
667 && (lhs->points_to() != NULL
668 || lhs->function_type() != NULL
669 || lhs->is_slice_type()
670 || lhs->map_type() != NULL
671 || lhs->channel_type() != NULL
672 || lhs->interface_type() != NULL))
675 // An untyped numeric constant may be assigned to a numeric type if
676 // it is representable in that type.
677 if ((rhs->is_abstract()
678 && (rhs->integer_type() != NULL
679 || rhs->float_type() != NULL
680 || rhs->complex_type() != NULL))
681 && (lhs->integer_type() != NULL
682 || lhs->float_type() != NULL
683 || lhs->complex_type() != NULL))
686 // Give some better error messages.
687 if (reason != NULL && reason->empty())
689 if (rhs->interface_type() != NULL)
690 reason->assign(_("need explicit conversion"));
691 else if (rhs->is_call_multiple_result_type())
692 reason->assign(_("multiple value function call in "
693 "single value context"));
694 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
696 size_t len = (lhs->named_type()->name().length()
697 + rhs->named_type()->name().length()
699 char* buf = new char[len];
700 snprintf(buf, len, _("cannot use type %s as type %s"),
701 rhs->named_type()->message_name().c_str(),
702 lhs->named_type()->message_name().c_str());
711 // Return true if a value with type RHS may be assigned to a variable
712 // with type LHS. If REASON is not NULL, set *REASON to the reason
713 // the types are not assignable.
716 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
718 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
721 // Like are_assignable but don't check for hidden fields.
724 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
727 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
730 // Return true if a value with type RHS may be converted to type LHS.
731 // If REASON is not NULL, set *REASON to the reason the types are not
735 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
737 // The types are convertible if they are assignable.
738 if (Type::are_assignable(lhs, rhs, reason))
741 // The types are convertible if they have identical underlying
743 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
744 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
747 // The types are convertible if they are both unnamed pointer types
748 // and their pointer base types have identical underlying types.
749 if (lhs->named_type() == NULL
750 && rhs->named_type() == NULL
751 && lhs->points_to() != NULL
752 && rhs->points_to() != NULL
753 && (lhs->points_to()->named_type() != NULL
754 || rhs->points_to()->named_type() != NULL)
755 && Type::are_identical(lhs->points_to()->base(),
756 rhs->points_to()->base(),
761 // Integer and floating point types are convertible to each other.
762 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
763 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
766 // Complex types are convertible to each other.
767 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
770 // An integer, or []byte, or []rune, may be converted to a string.
771 if (lhs->is_string_type())
773 if (rhs->integer_type() != NULL)
775 if (rhs->is_slice_type())
777 const Type* e = rhs->array_type()->element_type()->forwarded();
778 if (e->integer_type() != NULL
779 && (e->integer_type()->is_byte()
780 || e->integer_type()->is_rune()))
785 // A string may be converted to []byte or []rune.
786 if (rhs->is_string_type() && lhs->is_slice_type())
788 const Type* e = lhs->array_type()->element_type()->forwarded();
789 if (e->integer_type() != NULL
790 && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
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(gogo))
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;
1508 // Mangled names can have '.' if they happen to refer to named
1509 // types in some way. That's fine if this is simply a named
1510 // type, but otherwise it will confuse the code that builds
1511 // function identifiers. Remove '.' when necessary.
1512 base_name = this->mangled_name(gogo);
1514 while ((i = base_name.find('.')) != std::string::npos)
1516 base_name = gogo->pack_hidden_name(base_name, false);
1520 // This name is already hidden or not as appropriate.
1521 base_name = name->name();
1522 const Named_object* in_function = name->in_function();
1523 if (in_function != NULL)
1524 base_name += '$' + in_function->name();
1526 std::string hash_name = base_name + "$hash";
1527 std::string equal_name = base_name + "$equal";
1529 Location bloc = Linemap::predeclared_location();
1531 const Package* package = NULL;
1532 bool is_defined_elsewhere =
1533 this->type_descriptor_defined_elsewhere(name, &package);
1534 if (is_defined_elsewhere)
1536 *hash_fn = Named_object::make_function_declaration(hash_name, package,
1538 *equal_fn = Named_object::make_function_declaration(equal_name, package,
1539 equal_fntype, bloc);
1543 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1544 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1548 ins.first->second.first = *hash_fn;
1549 ins.first->second.second = *equal_fn;
1551 if (!is_defined_elsewhere)
1553 if (gogo->in_global_scope())
1554 this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1555 equal_name, equal_fntype);
1557 gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1558 equal_name, equal_fntype);
1562 // Write the hash and equality functions for a type which needs to be
1563 // written specially.
1566 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1567 const std::string& hash_name,
1568 Function_type* hash_fntype,
1569 const std::string& equal_name,
1570 Function_type* equal_fntype)
1572 Location bloc = Linemap::predeclared_location();
1574 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1576 gogo->start_block(bloc);
1578 if (this->struct_type() != NULL)
1579 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1581 else if (this->array_type() != NULL)
1582 this->array_type()->write_hash_function(gogo, name, hash_fntype,
1587 Block* b = gogo->finish_block(bloc);
1588 gogo->add_block(b, bloc);
1589 gogo->lower_block(hash_fn, b);
1590 gogo->finish_function(bloc);
1592 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1594 gogo->start_block(bloc);
1596 if (this->struct_type() != NULL)
1597 this->struct_type()->write_equal_function(gogo, name);
1598 else if (this->array_type() != NULL)
1599 this->array_type()->write_equal_function(gogo, name);
1603 b = gogo->finish_block(bloc);
1604 gogo->add_block(b, bloc);
1605 gogo->lower_block(equal_fn, b);
1606 gogo->finish_function(bloc);
1609 // Return a composite literal for the type descriptor for a plain type
1610 // of kind RUNTIME_TYPE_KIND named NAME.
1613 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1614 Named_type* name, const Methods* methods,
1615 bool only_value_methods)
1617 Location bloc = Linemap::predeclared_location();
1619 Type* td_type = Type::make_type_descriptor_type();
1620 const Struct_field_list* fields = td_type->struct_type()->fields();
1622 Expression_list* vals = new Expression_list();
1625 if (!this->has_pointer())
1626 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1627 Struct_field_list::const_iterator p = fields->begin();
1628 go_assert(p->is_field_name("Kind"));
1630 mpz_init_set_ui(iv, runtime_type_kind);
1631 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1634 go_assert(p->is_field_name("align"));
1635 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1636 vals->push_back(Expression::make_type_info(this, type_info));
1639 go_assert(p->is_field_name("fieldAlign"));
1640 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1641 vals->push_back(Expression::make_type_info(this, type_info));
1644 go_assert(p->is_field_name("size"));
1645 type_info = Expression::TYPE_INFO_SIZE;
1646 vals->push_back(Expression::make_type_info(this, type_info));
1649 go_assert(p->is_field_name("hash"));
1650 mpz_set_ui(iv, this->hash_for_method(gogo));
1651 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1654 go_assert(p->is_field_name("hashfn"));
1655 Function_type* hash_fntype = p->type()->function_type();
1658 go_assert(p->is_field_name("equalfn"));
1659 Function_type* equal_fntype = p->type()->function_type();
1661 Named_object* hash_fn;
1662 Named_object* equal_fn;
1663 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1665 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1666 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1669 go_assert(p->is_field_name("string"));
1670 Expression* s = Expression::make_string((name != NULL
1671 ? name->reflection(gogo)
1672 : this->reflection(gogo)),
1674 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1677 go_assert(p->is_field_name("uncommonType"));
1678 if (name == NULL && methods == NULL)
1679 vals->push_back(Expression::make_nil(bloc));
1682 if (methods == NULL)
1683 methods = name->methods();
1684 vals->push_back(this->uncommon_type_constructor(gogo,
1687 only_value_methods));
1691 go_assert(p->is_field_name("ptrToThis"));
1693 vals->push_back(Expression::make_nil(bloc));
1696 Type* pt = Type::make_pointer_type(name);
1697 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1701 go_assert(p == fields->end());
1705 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1708 // Return a composite literal for the uncommon type information for
1709 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1710 // struct. If name is not NULL, it is the name of the type. If
1711 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1712 // is true if only value methods should be included. At least one of
1713 // NAME and METHODS must not be NULL.
1716 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1717 Named_type* name, const Methods* methods,
1718 bool only_value_methods) const
1720 Location bloc = Linemap::predeclared_location();
1722 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1724 Expression_list* vals = new Expression_list();
1727 Struct_field_list::const_iterator p = fields->begin();
1728 go_assert(p->is_field_name("name"));
1731 go_assert(p->is_field_name("pkgPath"));
1735 vals->push_back(Expression::make_nil(bloc));
1736 vals->push_back(Expression::make_nil(bloc));
1740 Named_object* no = name->named_object();
1741 std::string n = Gogo::unpack_hidden_name(no->name());
1742 Expression* s = Expression::make_string(n, bloc);
1743 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1745 if (name->is_builtin())
1746 vals->push_back(Expression::make_nil(bloc));
1749 const Package* package = no->package();
1750 const std::string& unique_prefix(package == NULL
1751 ? gogo->unique_prefix()
1752 : package->unique_prefix());
1753 const std::string& package_name(package == NULL
1754 ? gogo->package_name()
1756 n.assign(unique_prefix);
1758 n.append(package_name);
1759 if (name->in_function() != NULL)
1762 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1764 s = Expression::make_string(n, bloc);
1765 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1770 go_assert(p->is_field_name("methods"));
1771 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1772 only_value_methods));
1775 go_assert(p == fields->end());
1777 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1779 return Expression::make_unary(OPERATOR_AND, r, bloc);
1782 // Sort methods by name.
1788 operator()(const std::pair<std::string, const Method*>& m1,
1789 const std::pair<std::string, const Method*>& m2) const
1790 { return m1.first < m2.first; }
1793 // Return a composite literal for the type method table for this type.
1794 // METHODS_TYPE is the type of the table, and is a slice type.
1795 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1796 // then only value methods are used.
1799 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1800 const Methods* methods,
1801 bool only_value_methods) const
1803 Location bloc = Linemap::predeclared_location();
1805 std::vector<std::pair<std::string, const Method*> > smethods;
1806 if (methods != NULL)
1808 smethods.reserve(methods->count());
1809 for (Methods::const_iterator p = methods->begin();
1810 p != methods->end();
1813 if (p->second->is_ambiguous())
1815 if (only_value_methods && !p->second->is_value_method())
1817 smethods.push_back(std::make_pair(p->first, p->second));
1821 if (smethods.empty())
1822 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1824 std::sort(smethods.begin(), smethods.end(), Sort_methods());
1826 Type* method_type = methods_type->array_type()->element_type();
1828 Expression_list* vals = new Expression_list();
1829 vals->reserve(smethods.size());
1830 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1832 p != smethods.end();
1834 vals->push_back(this->method_constructor(gogo, method_type, p->first,
1835 p->second, only_value_methods));
1837 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1840 // Return a composite literal for a single method. METHOD_TYPE is the
1841 // type of the entry. METHOD_NAME is the name of the method and M is
1842 // the method information.
1845 Type::method_constructor(Gogo*, Type* method_type,
1846 const std::string& method_name,
1848 bool only_value_methods) const
1850 Location bloc = Linemap::predeclared_location();
1852 const Struct_field_list* fields = method_type->struct_type()->fields();
1854 Expression_list* vals = new Expression_list();
1857 Struct_field_list::const_iterator p = fields->begin();
1858 go_assert(p->is_field_name("name"));
1859 const std::string n = Gogo::unpack_hidden_name(method_name);
1860 Expression* s = Expression::make_string(n, bloc);
1861 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1864 go_assert(p->is_field_name("pkgPath"));
1865 if (!Gogo::is_hidden_name(method_name))
1866 vals->push_back(Expression::make_nil(bloc));
1869 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1870 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1873 Named_object* no = (m->needs_stub_method()
1875 : m->named_object());
1877 Function_type* mtype;
1878 if (no->is_function())
1879 mtype = no->func_value()->type();
1881 mtype = no->func_declaration_value()->type();
1882 go_assert(mtype->is_method());
1883 Type* nonmethod_type = mtype->copy_without_receiver();
1886 go_assert(p->is_field_name("mtyp"));
1887 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1890 go_assert(p->is_field_name("typ"));
1891 if (!only_value_methods && m->is_value_method())
1893 // This is a value method on a pointer type. Change the type of
1894 // the method to use a pointer receiver. The implementation
1895 // always uses a pointer receiver anyhow.
1896 Type* rtype = mtype->receiver()->type();
1897 Type* prtype = Type::make_pointer_type(rtype);
1898 Typed_identifier* receiver =
1899 new Typed_identifier(mtype->receiver()->name(), prtype,
1900 mtype->receiver()->location());
1901 mtype = Type::make_function_type(receiver,
1902 (mtype->parameters() == NULL
1904 : mtype->parameters()->copy()),
1905 (mtype->results() == NULL
1907 : mtype->results()->copy()),
1910 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1913 go_assert(p->is_field_name("tfn"));
1914 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1917 go_assert(p == fields->end());
1919 return Expression::make_struct_composite_literal(method_type, vals, bloc);
1922 // Return a composite literal for the type descriptor of a plain type.
1923 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1924 // NULL, it is the name to use as well as the list of methods.
1927 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1930 return this->type_descriptor_constructor(gogo, runtime_type_kind,
1934 // Return the type reflection string for this type.
1937 Type::reflection(Gogo* gogo) const
1941 // The do_reflection virtual function should set RET to the
1942 // reflection string.
1943 this->do_reflection(gogo, &ret);
1948 // Return a mangled name for the type.
1951 Type::mangled_name(Gogo* gogo) const
1955 // The do_mangled_name virtual function should set RET to the
1956 // mangled name. For a composite type it should append a code for
1957 // the composition and then call do_mangled_name on the components.
1958 this->do_mangled_name(gogo, &ret);
1963 // Return whether the backend size of the type is known.
1966 Type::is_backend_type_size_known(Gogo* gogo)
1968 switch (this->classification_)
1982 case TYPE_INTERFACE:
1987 const Struct_field_list* fields = this->struct_type()->fields();
1988 for (Struct_field_list::const_iterator pf = fields->begin();
1989 pf != fields->end();
1991 if (!pf->type()->is_backend_type_size_known(gogo))
1998 const Array_type* at = this->array_type();
1999 if (at->length() == NULL)
2006 bool length_known = at->length()->integer_constant_value(true,
2012 return at->element_type()->is_backend_type_size_known(gogo);
2017 // Begin converting this type to the backend representation.
2018 // This will create a placeholder if necessary.
2019 this->get_backend(gogo);
2020 return this->named_type()->is_named_backend_type_size_known();
2024 Forward_declaration_type* fdt = this->forward_declaration_type();
2025 return fdt->real_type()->is_backend_type_size_known(gogo);
2029 case TYPE_CALL_MULTIPLE_RESULT:
2037 // If the size of the type can be determined, set *PSIZE to the size
2038 // in bytes and return true. Otherwise, return false. This queries
2042 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2044 if (!this->is_backend_type_size_known(gogo))
2046 size_t size = gogo->backend()->type_size(this->get_backend(gogo));
2047 *psize = static_cast<unsigned int>(size);
2053 // If the alignment of the type can be determined, set *PALIGN to
2054 // the alignment in bytes and return true. Otherwise, return false.
2057 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2059 if (!this->is_backend_type_size_known(gogo))
2061 size_t align = gogo->backend()->type_alignment(this->get_backend(gogo));
2062 *palign = static_cast<unsigned int>(align);
2063 if (*palign != align)
2068 // Like backend_type_align, but return the alignment when used as a
2072 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2074 if (!this->is_backend_type_size_known(gogo))
2076 size_t a = gogo->backend()->type_field_alignment(this->get_backend(gogo));
2077 *palign = static_cast<unsigned int>(a);
2083 // Default function to export a type.
2086 Type::do_export(Export*) const
2094 Type::import_type(Import* imp)
2096 if (imp->match_c_string("("))
2097 return Function_type::do_import(imp);
2098 else if (imp->match_c_string("*"))
2099 return Pointer_type::do_import(imp);
2100 else if (imp->match_c_string("struct "))
2101 return Struct_type::do_import(imp);
2102 else if (imp->match_c_string("["))
2103 return Array_type::do_import(imp);
2104 else if (imp->match_c_string("map "))
2105 return Map_type::do_import(imp);
2106 else if (imp->match_c_string("chan "))
2107 return Channel_type::do_import(imp);
2108 else if (imp->match_c_string("interface"))
2109 return Interface_type::do_import(imp);
2112 error_at(imp->location(), "import error: expected type");
2113 return Type::make_error_type();
2117 // A type used to indicate a parsing error. This exists to simplify
2118 // later error detection.
2120 class Error_type : public Type
2129 do_compare_is_identity(Gogo*) const
2133 do_get_backend(Gogo* gogo)
2134 { return gogo->backend()->error_type(); }
2137 do_type_descriptor(Gogo*, Named_type*)
2138 { return Expression::make_error(Linemap::predeclared_location()); }
2141 do_reflection(Gogo*, std::string*) const
2142 { go_assert(saw_errors()); }
2145 do_mangled_name(Gogo*, std::string* ret) const
2146 { ret->push_back('E'); }
2150 Type::make_error_type()
2152 static Error_type singleton_error_type;
2153 return &singleton_error_type;
2158 class Void_type : public Type
2167 do_compare_is_identity(Gogo*) const
2171 do_get_backend(Gogo* gogo)
2172 { return gogo->backend()->void_type(); }
2175 do_type_descriptor(Gogo*, Named_type*)
2176 { go_unreachable(); }
2179 do_reflection(Gogo*, std::string*) const
2183 do_mangled_name(Gogo*, std::string* ret) const
2184 { ret->push_back('v'); }
2188 Type::make_void_type()
2190 static Void_type singleton_void_type;
2191 return &singleton_void_type;
2194 // The boolean type.
2196 class Boolean_type : public Type
2200 : Type(TYPE_BOOLEAN)
2205 do_compare_is_identity(Gogo*) const
2209 do_get_backend(Gogo* gogo)
2210 { return gogo->backend()->bool_type(); }
2213 do_type_descriptor(Gogo*, Named_type* name);
2215 // We should not be asked for the reflection string of a basic type.
2217 do_reflection(Gogo*, std::string* ret) const
2218 { ret->append("bool"); }
2221 do_mangled_name(Gogo*, std::string* ret) const
2222 { ret->push_back('b'); }
2225 // Make the type descriptor.
2228 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2231 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2234 Named_object* no = gogo->lookup_global("bool");
2235 go_assert(no != NULL);
2236 return Type::type_descriptor(gogo, no->type_value());
2241 Type::make_boolean_type()
2243 static Boolean_type boolean_type;
2244 return &boolean_type;
2247 // The named type "bool".
2249 static Named_type* named_bool_type;
2251 // Get the named type "bool".
2254 Type::lookup_bool_type()
2256 return named_bool_type;
2259 // Make the named type "bool".
2262 Type::make_named_bool_type()
2264 Type* bool_type = Type::make_boolean_type();
2265 Named_object* named_object =
2266 Named_object::make_type("bool", NULL, bool_type,
2267 Linemap::predeclared_location());
2268 Named_type* named_type = named_object->type_value();
2269 named_bool_type = named_type;
2273 // Class Integer_type.
2275 Integer_type::Named_integer_types Integer_type::named_integer_types;
2277 // Create a new integer type. Non-abstract integer types always have
2281 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2282 int bits, int runtime_type_kind)
2284 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2286 std::string sname(name);
2287 Named_object* named_object =
2288 Named_object::make_type(sname, NULL, integer_type,
2289 Linemap::predeclared_location());
2290 Named_type* named_type = named_object->type_value();
2291 std::pair<Named_integer_types::iterator, bool> ins =
2292 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2293 go_assert(ins.second);
2297 // Look up an existing integer type.
2300 Integer_type::lookup_integer_type(const char* name)
2302 Named_integer_types::const_iterator p =
2303 Integer_type::named_integer_types.find(name);
2304 go_assert(p != Integer_type::named_integer_types.end());
2308 // Create a new abstract integer type.
2311 Integer_type::create_abstract_integer_type()
2313 static Integer_type* abstract_type;
2314 if (abstract_type == NULL)
2315 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2316 RUNTIME_TYPE_KIND_INT);
2317 return abstract_type;
2320 // Integer type compatibility.
2323 Integer_type::is_identical(const Integer_type* t) const
2325 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2327 return this->is_abstract_ == t->is_abstract_;
2333 Integer_type::do_hash_for_method(Gogo*) const
2335 return ((this->bits_ << 4)
2336 + ((this->is_unsigned_ ? 1 : 0) << 8)
2337 + ((this->is_abstract_ ? 1 : 0) << 9));
2340 // Convert an Integer_type to the backend representation.
2343 Integer_type::do_get_backend(Gogo* gogo)
2345 if (this->is_abstract_)
2347 go_assert(saw_errors());
2348 return gogo->backend()->error_type();
2350 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2353 // The type descriptor for an integer type. Integer types are always
2357 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2359 go_assert(name != NULL);
2360 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2363 // We should not be asked for the reflection string of a basic type.
2366 Integer_type::do_reflection(Gogo*, std::string*) const
2368 go_assert(saw_errors());
2374 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2377 snprintf(buf, sizeof buf, "i%s%s%de",
2378 this->is_abstract_ ? "a" : "",
2379 this->is_unsigned_ ? "u" : "",
2384 // Make an integer type.
2387 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2388 int runtime_type_kind)
2390 return Integer_type::create_integer_type(name, is_unsigned, bits,
2394 // Make an abstract integer type.
2397 Type::make_abstract_integer_type()
2399 return Integer_type::create_abstract_integer_type();
2402 // Look up an integer type.
2405 Type::lookup_integer_type(const char* name)
2407 return Integer_type::lookup_integer_type(name);
2410 // Class Float_type.
2412 Float_type::Named_float_types Float_type::named_float_types;
2414 // Create a new float type. Non-abstract float types always have
2418 Float_type::create_float_type(const char* name, int bits,
2419 int runtime_type_kind)
2421 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2422 std::string sname(name);
2423 Named_object* named_object =
2424 Named_object::make_type(sname, NULL, float_type,
2425 Linemap::predeclared_location());
2426 Named_type* named_type = named_object->type_value();
2427 std::pair<Named_float_types::iterator, bool> ins =
2428 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2429 go_assert(ins.second);
2433 // Look up an existing float type.
2436 Float_type::lookup_float_type(const char* name)
2438 Named_float_types::const_iterator p =
2439 Float_type::named_float_types.find(name);
2440 go_assert(p != Float_type::named_float_types.end());
2444 // Create a new abstract float type.
2447 Float_type::create_abstract_float_type()
2449 static Float_type* abstract_type;
2450 if (abstract_type == NULL)
2451 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2452 return abstract_type;
2455 // Whether this type is identical with T.
2458 Float_type::is_identical(const Float_type* t) const
2460 if (this->bits_ != t->bits_)
2462 return this->is_abstract_ == t->is_abstract_;
2468 Float_type::do_hash_for_method(Gogo*) const
2470 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2473 // Convert to the backend representation.
2476 Float_type::do_get_backend(Gogo* gogo)
2478 return gogo->backend()->float_type(this->bits_);
2481 // The type descriptor for a float type. Float types are always named.
2484 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2486 go_assert(name != NULL);
2487 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2490 // We should not be asked for the reflection string of a basic type.
2493 Float_type::do_reflection(Gogo*, std::string*) const
2495 go_assert(saw_errors());
2501 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2504 snprintf(buf, sizeof buf, "f%s%de",
2505 this->is_abstract_ ? "a" : "",
2510 // Make a floating point type.
2513 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2515 return Float_type::create_float_type(name, bits, runtime_type_kind);
2518 // Make an abstract float type.
2521 Type::make_abstract_float_type()
2523 return Float_type::create_abstract_float_type();
2526 // Look up a float type.
2529 Type::lookup_float_type(const char* name)
2531 return Float_type::lookup_float_type(name);
2534 // Class Complex_type.
2536 Complex_type::Named_complex_types Complex_type::named_complex_types;
2538 // Create a new complex type. Non-abstract complex types always have
2542 Complex_type::create_complex_type(const char* name, int bits,
2543 int runtime_type_kind)
2545 Complex_type* complex_type = new Complex_type(false, bits,
2547 std::string sname(name);
2548 Named_object* named_object =
2549 Named_object::make_type(sname, NULL, complex_type,
2550 Linemap::predeclared_location());
2551 Named_type* named_type = named_object->type_value();
2552 std::pair<Named_complex_types::iterator, bool> ins =
2553 Complex_type::named_complex_types.insert(std::make_pair(sname,
2555 go_assert(ins.second);
2559 // Look up an existing complex type.
2562 Complex_type::lookup_complex_type(const char* name)
2564 Named_complex_types::const_iterator p =
2565 Complex_type::named_complex_types.find(name);
2566 go_assert(p != Complex_type::named_complex_types.end());
2570 // Create a new abstract complex type.
2573 Complex_type::create_abstract_complex_type()
2575 static Complex_type* abstract_type;
2576 if (abstract_type == NULL)
2577 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2578 return abstract_type;
2581 // Whether this type is identical with T.
2584 Complex_type::is_identical(const Complex_type *t) const
2586 if (this->bits_ != t->bits_)
2588 return this->is_abstract_ == t->is_abstract_;
2594 Complex_type::do_hash_for_method(Gogo*) const
2596 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2599 // Convert to the backend representation.
2602 Complex_type::do_get_backend(Gogo* gogo)
2604 return gogo->backend()->complex_type(this->bits_);
2607 // The type descriptor for a complex type. Complex types are always
2611 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2613 go_assert(name != NULL);
2614 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2617 // We should not be asked for the reflection string of a basic type.
2620 Complex_type::do_reflection(Gogo*, std::string*) const
2622 go_assert(saw_errors());
2628 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2631 snprintf(buf, sizeof buf, "c%s%de",
2632 this->is_abstract_ ? "a" : "",
2637 // Make a complex type.
2640 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2642 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2645 // Make an abstract complex type.
2648 Type::make_abstract_complex_type()
2650 return Complex_type::create_abstract_complex_type();
2653 // Look up a complex type.
2656 Type::lookup_complex_type(const char* name)
2658 return Complex_type::lookup_complex_type(name);
2661 // Class String_type.
2663 // Convert String_type to the backend representation. A string is a
2664 // struct with two fields: a pointer to the characters and a length.
2667 String_type::do_get_backend(Gogo* gogo)
2669 static Btype* backend_string_type;
2670 if (backend_string_type == NULL)
2672 std::vector<Backend::Btyped_identifier> fields(2);
2674 Type* b = gogo->lookup_global("byte")->type_value();
2675 Type* pb = Type::make_pointer_type(b);
2676 fields[0].name = "__data";
2677 fields[0].btype = pb->get_backend(gogo);
2678 fields[0].location = Linemap::predeclared_location();
2680 Type* int_type = Type::lookup_integer_type("int");
2681 fields[1].name = "__length";
2682 fields[1].btype = int_type->get_backend(gogo);
2683 fields[1].location = fields[0].location;
2685 backend_string_type = gogo->backend()->struct_type(fields);
2687 return backend_string_type;
2690 // Return a tree for the length of STRING.
2693 String_type::length_tree(Gogo*, tree string)
2695 tree string_type = TREE_TYPE(string);
2696 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2697 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2698 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2700 return fold_build3(COMPONENT_REF, integer_type_node, string,
2701 length_field, NULL_TREE);
2704 // Return a tree for a pointer to the bytes of STRING.
2707 String_type::bytes_tree(Gogo*, tree string)
2709 tree string_type = TREE_TYPE(string);
2710 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2711 tree bytes_field = TYPE_FIELDS(string_type);
2712 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2714 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2715 bytes_field, NULL_TREE);
2718 // The type descriptor for the string type.
2721 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2724 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2727 Named_object* no = gogo->lookup_global("string");
2728 go_assert(no != NULL);
2729 return Type::type_descriptor(gogo, no->type_value());
2733 // We should not be asked for the reflection string of a basic type.
2736 String_type::do_reflection(Gogo*, std::string* ret) const
2738 ret->append("string");
2741 // Mangled name of a string type.
2744 String_type::do_mangled_name(Gogo*, std::string* ret) const
2746 ret->push_back('z');
2749 // Make a string type.
2752 Type::make_string_type()
2754 static String_type string_type;
2755 return &string_type;
2758 // The named type "string".
2760 static Named_type* named_string_type;
2762 // Get the named type "string".
2765 Type::lookup_string_type()
2767 return named_string_type;
2770 // Make the named type string.
2773 Type::make_named_string_type()
2775 Type* string_type = Type::make_string_type();
2776 Named_object* named_object =
2777 Named_object::make_type("string", NULL, string_type,
2778 Linemap::predeclared_location());
2779 Named_type* named_type = named_object->type_value();
2780 named_string_type = named_type;
2784 // The sink type. This is the type of the blank identifier _. Any
2785 // type may be assigned to it.
2787 class Sink_type : public Type
2796 do_compare_is_identity(Gogo*) const
2800 do_get_backend(Gogo*)
2801 { go_unreachable(); }
2804 do_type_descriptor(Gogo*, Named_type*)
2805 { go_unreachable(); }
2808 do_reflection(Gogo*, std::string*) const
2809 { go_unreachable(); }
2812 do_mangled_name(Gogo*, std::string*) const
2813 { go_unreachable(); }
2816 // Make the sink type.
2819 Type::make_sink_type()
2821 static Sink_type sink_type;
2825 // Class Function_type.
2830 Function_type::do_traverse(Traverse* traverse)
2832 if (this->receiver_ != NULL
2833 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2834 return TRAVERSE_EXIT;
2835 if (this->parameters_ != NULL
2836 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2837 return TRAVERSE_EXIT;
2838 if (this->results_ != NULL
2839 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2840 return TRAVERSE_EXIT;
2841 return TRAVERSE_CONTINUE;
2844 // Returns whether T is a valid redeclaration of this type. If this
2845 // returns false, and REASON is not NULL, *REASON may be set to a
2846 // brief explanation of why it returned false.
2849 Function_type::is_valid_redeclaration(const Function_type* t,
2850 std::string* reason) const
2852 if (!this->is_identical(t, false, true, reason))
2855 // A redeclaration of a function is required to use the same names
2856 // for the receiver and parameters.
2857 if (this->receiver() != NULL
2858 && this->receiver()->name() != t->receiver()->name()
2859 && this->receiver()->name() != Import::import_marker
2860 && t->receiver()->name() != Import::import_marker)
2863 *reason = "receiver name changed";
2867 const Typed_identifier_list* parms1 = this->parameters();
2868 const Typed_identifier_list* parms2 = t->parameters();
2871 Typed_identifier_list::const_iterator p1 = parms1->begin();
2872 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2873 p2 != parms2->end();
2876 if (p1->name() != p2->name()
2877 && p1->name() != Import::import_marker
2878 && p2->name() != Import::import_marker)
2881 *reason = "parameter name changed";
2885 // This is called at parse time, so we may have unknown
2887 Type* t1 = p1->type()->forwarded();
2888 Type* t2 = p2->type()->forwarded();
2890 && t1->forward_declaration_type() != NULL
2891 && (t2->forward_declaration_type() == NULL
2892 || (t1->forward_declaration_type()->named_object()
2893 != t2->forward_declaration_type()->named_object())))
2898 const Typed_identifier_list* results1 = this->results();
2899 const Typed_identifier_list* results2 = t->results();
2900 if (results1 != NULL)
2902 Typed_identifier_list::const_iterator res1 = results1->begin();
2903 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2904 res2 != results2->end();
2907 if (res1->name() != res2->name()
2908 && res1->name() != Import::import_marker
2909 && res2->name() != Import::import_marker)
2912 *reason = "result name changed";
2916 // This is called at parse time, so we may have unknown
2918 Type* t1 = res1->type()->forwarded();
2919 Type* t2 = res2->type()->forwarded();
2921 && t1->forward_declaration_type() != NULL
2922 && (t2->forward_declaration_type() == NULL
2923 || (t1->forward_declaration_type()->named_object()
2924 != t2->forward_declaration_type()->named_object())))
2932 // Check whether T is the same as this type.
2935 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2936 bool errors_are_identical,
2937 std::string* reason) const
2939 if (!ignore_receiver)
2941 const Typed_identifier* r1 = this->receiver();
2942 const Typed_identifier* r2 = t->receiver();
2943 if ((r1 != NULL) != (r2 != NULL))
2946 *reason = _("different receiver types");
2951 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2954 if (reason != NULL && !reason->empty())
2955 *reason = "receiver: " + *reason;
2961 const Typed_identifier_list* parms1 = this->parameters();
2962 const Typed_identifier_list* parms2 = t->parameters();
2963 if ((parms1 != NULL) != (parms2 != NULL))
2966 *reason = _("different number of parameters");
2971 Typed_identifier_list::const_iterator p1 = parms1->begin();
2972 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2973 p2 != parms2->end();
2976 if (p1 == parms1->end())
2979 *reason = _("different number of parameters");
2983 if (!Type::are_identical(p1->type(), p2->type(),
2984 errors_are_identical, NULL))
2987 *reason = _("different parameter types");
2991 if (p1 != parms1->end())
2994 *reason = _("different number of parameters");
2999 if (this->is_varargs() != t->is_varargs())
3002 *reason = _("different varargs");
3006 const Typed_identifier_list* results1 = this->results();
3007 const Typed_identifier_list* results2 = t->results();
3008 if ((results1 != NULL) != (results2 != NULL))
3011 *reason = _("different number of results");
3014 if (results1 != NULL)
3016 Typed_identifier_list::const_iterator res1 = results1->begin();
3017 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3018 res2 != results2->end();
3021 if (res1 == results1->end())
3024 *reason = _("different number of results");
3028 if (!Type::are_identical(res1->type(), res2->type(),
3029 errors_are_identical, NULL))
3032 *reason = _("different result types");
3036 if (res1 != results1->end())
3039 *reason = _("different number of results");
3050 Function_type::do_hash_for_method(Gogo* gogo) const
3052 unsigned int ret = 0;
3053 // We ignore the receiver type for hash codes, because we need to
3054 // get the same hash code for a method in an interface and a method
3055 // declared for a type. The former will not have a receiver.
3056 if (this->parameters_ != NULL)
3059 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3060 p != this->parameters_->end();
3062 ret += p->type()->hash_for_method(gogo) << shift;
3064 if (this->results_ != NULL)
3067 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3068 p != this->results_->end();
3070 ret += p->type()->hash_for_method(gogo) << shift;
3072 if (this->is_varargs_)
3078 // Get the backend representation for a function type.
3081 Function_type::get_function_backend(Gogo* gogo)
3083 Backend::Btyped_identifier breceiver;
3084 if (this->receiver_ != NULL)
3086 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3088 // We always pass the address of the receiver parameter, in
3089 // order to make interface calls work with unknown types.
3090 Type* rtype = this->receiver_->type();
3091 if (rtype->points_to() == NULL)
3092 rtype = Type::make_pointer_type(rtype);
3093 breceiver.btype = rtype->get_backend(gogo);
3094 breceiver.location = this->receiver_->location();
3097 std::vector<Backend::Btyped_identifier> bparameters;
3098 if (this->parameters_ != NULL)
3100 bparameters.resize(this->parameters_->size());
3102 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3103 p != this->parameters_->end();
3106 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3107 bparameters[i].btype = p->type()->get_backend(gogo);
3108 bparameters[i].location = p->location();
3110 go_assert(i == bparameters.size());
3113 std::vector<Backend::Btyped_identifier> bresults;
3114 if (this->results_ != NULL)
3116 bresults.resize(this->results_->size());
3118 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3119 p != this->results_->end();
3122 bresults[i].name = Gogo::unpack_hidden_name(p->name());
3123 bresults[i].btype = p->type()->get_backend(gogo);
3124 bresults[i].location = p->location();
3126 go_assert(i == bresults.size());
3129 return gogo->backend()->function_type(breceiver, bparameters, bresults,
3133 // A hash table mapping function types to their backend placeholders.
3135 Function_type::Placeholders Function_type::placeholders;
3137 // Get the backend representation for a function type. If we are
3138 // still converting types, and this types has multiple results, return
3139 // a placeholder instead. We do this because for multiple results we
3140 // build a struct, and we need to make sure that all the types in the
3141 // struct are valid before we create the struct.
3144 Function_type::do_get_backend(Gogo* gogo)
3146 if (!gogo->named_types_are_converted()
3147 && this->results_ != NULL
3148 && this->results_->size() > 1)
3150 Btype* placeholder =
3151 gogo->backend()->placeholder_pointer_type("", this->location(), true);
3152 Function_type::placeholders.push_back(std::make_pair(this, placeholder));
3155 return this->get_function_backend(gogo);
3158 // Convert function types after all named types are converted.
3161 Function_type::convert_types(Gogo* gogo)
3163 for (Placeholders::const_iterator p = Function_type::placeholders.begin();
3164 p != Function_type::placeholders.end();
3167 Btype* bt = p->first->get_function_backend(gogo);
3168 if (!gogo->backend()->set_placeholder_function_type(p->second, bt))
3169 go_assert(saw_errors());
3173 // The type of a function type descriptor.
3176 Function_type::make_function_type_descriptor_type()
3181 Type* tdt = Type::make_type_descriptor_type();
3182 Type* ptdt = Type::make_type_descriptor_ptr_type();
3184 Type* bool_type = Type::lookup_bool_type();
3186 Type* slice_type = Type::make_array_type(ptdt, NULL);
3188 Struct_type* s = Type::make_builtin_struct_type(4,
3190 "dotdotdot", bool_type,
3194 ret = Type::make_builtin_named_type("FuncType", s);
3200 // The type descriptor for a function type.
3203 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3205 Location bloc = Linemap::predeclared_location();
3207 Type* ftdt = Function_type::make_function_type_descriptor_type();
3209 const Struct_field_list* fields = ftdt->struct_type()->fields();
3211 Expression_list* vals = new Expression_list();
3214 Struct_field_list::const_iterator p = fields->begin();
3215 go_assert(p->is_field_name("commonType"));
3216 vals->push_back(this->type_descriptor_constructor(gogo,
3217 RUNTIME_TYPE_KIND_FUNC,
3221 go_assert(p->is_field_name("dotdotdot"));
3222 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3225 go_assert(p->is_field_name("in"));
3226 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3227 this->parameters()));
3230 go_assert(p->is_field_name("out"));
3231 vals->push_back(this->type_descriptor_params(p->type(), NULL,
3235 go_assert(p == fields->end());
3237 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3240 // Return a composite literal for the parameters or results of a type
3244 Function_type::type_descriptor_params(Type* params_type,
3245 const Typed_identifier* receiver,
3246 const Typed_identifier_list* params)
3248 Location bloc = Linemap::predeclared_location();
3250 if (receiver == NULL && params == NULL)
3251 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3253 Expression_list* vals = new Expression_list();
3254 vals->reserve((params == NULL ? 0 : params->size())
3255 + (receiver != NULL ? 1 : 0));
3257 if (receiver != NULL)
3258 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3262 for (Typed_identifier_list::const_iterator p = params->begin();
3265 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3268 return Expression::make_slice_composite_literal(params_type, vals, bloc);
3271 // The reflection string.
3274 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3276 // FIXME: Turn this off until we straighten out the type of the
3277 // struct field used in a go statement which calls a method.
3278 // go_assert(this->receiver_ == NULL);
3280 ret->append("func");
3282 if (this->receiver_ != NULL)
3284 ret->push_back('(');
3285 this->append_reflection(this->receiver_->type(), gogo, ret);
3286 ret->push_back(')');
3289 ret->push_back('(');
3290 const Typed_identifier_list* params = this->parameters();
3293 bool is_varargs = this->is_varargs_;
3294 for (Typed_identifier_list::const_iterator p = params->begin();
3298 if (p != params->begin())
3300 if (!is_varargs || p + 1 != params->end())
3301 this->append_reflection(p->type(), gogo, ret);
3305 this->append_reflection(p->type()->array_type()->element_type(),
3310 ret->push_back(')');
3312 const Typed_identifier_list* results = this->results();
3313 if (results != NULL && !results->empty())
3315 if (results->size() == 1)
3316 ret->push_back(' ');
3319 for (Typed_identifier_list::const_iterator p = results->begin();
3320 p != results->end();
3323 if (p != results->begin())
3325 this->append_reflection(p->type(), gogo, ret);
3327 if (results->size() > 1)
3328 ret->push_back(')');
3335 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3337 ret->push_back('F');
3339 if (this->receiver_ != NULL)
3341 ret->push_back('m');
3342 this->append_mangled_name(this->receiver_->type(), gogo, ret);
3345 const Typed_identifier_list* params = this->parameters();
3348 ret->push_back('p');
3349 for (Typed_identifier_list::const_iterator p = params->begin();
3352 this->append_mangled_name(p->type(), gogo, ret);
3353 if (this->is_varargs_)
3354 ret->push_back('V');
3355 ret->push_back('e');
3358 const Typed_identifier_list* results = this->results();
3359 if (results != NULL)
3361 ret->push_back('r');
3362 for (Typed_identifier_list::const_iterator p = results->begin();
3363 p != results->end();
3365 this->append_mangled_name(p->type(), gogo, ret);
3366 ret->push_back('e');
3369 ret->push_back('e');
3372 // Export a function type.
3375 Function_type::do_export(Export* exp) const
3377 // We don't write out the receiver. The only function types which
3378 // should have a receiver are the ones associated with explicitly
3379 // defined methods. For those the receiver type is written out by
3380 // Function::export_func.
3382 exp->write_c_string("(");
3384 if (this->parameters_ != NULL)
3386 bool is_varargs = this->is_varargs_;
3387 for (Typed_identifier_list::const_iterator p =
3388 this->parameters_->begin();
3389 p != this->parameters_->end();
3395 exp->write_c_string(", ");
3396 if (!is_varargs || p + 1 != this->parameters_->end())
3397 exp->write_type(p->type());
3400 exp->write_c_string("...");
3401 exp->write_type(p->type()->array_type()->element_type());
3405 exp->write_c_string(")");
3407 const Typed_identifier_list* results = this->results_;
3408 if (results != NULL)
3410 exp->write_c_string(" ");
3411 if (results->size() == 1)
3412 exp->write_type(results->begin()->type());
3416 exp->write_c_string("(");
3417 for (Typed_identifier_list::const_iterator p = results->begin();
3418 p != results->end();
3424 exp->write_c_string(", ");
3425 exp->write_type(p->type());
3427 exp->write_c_string(")");
3432 // Import a function type.
3435 Function_type::do_import(Import* imp)
3437 imp->require_c_string("(");
3438 Typed_identifier_list* parameters;
3439 bool is_varargs = false;
3440 if (imp->peek_char() == ')')
3444 parameters = new Typed_identifier_list();
3447 if (imp->match_c_string("..."))
3453 Type* ptype = imp->read_type();
3455 ptype = Type::make_array_type(ptype, NULL);
3456 parameters->push_back(Typed_identifier(Import::import_marker,
3457 ptype, imp->location()));
3458 if (imp->peek_char() != ',')
3460 go_assert(!is_varargs);
3461 imp->require_c_string(", ");
3464 imp->require_c_string(")");
3466 Typed_identifier_list* results;
3467 if (imp->peek_char() != ' ')
3472 results = new Typed_identifier_list;
3473 if (imp->peek_char() != '(')
3475 Type* rtype = imp->read_type();
3476 results->push_back(Typed_identifier(Import::import_marker, rtype,
3484 Type* rtype = imp->read_type();
3485 results->push_back(Typed_identifier(Import::import_marker,
3486 rtype, imp->location()));
3487 if (imp->peek_char() != ',')
3489 imp->require_c_string(", ");
3491 imp->require_c_string(")");
3495 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3498 ret->set_is_varargs();
3502 // Make a copy of a function type without a receiver.
3505 Function_type::copy_without_receiver() const
3507 go_assert(this->is_method());
3508 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3511 if (this->is_varargs())
3512 ret->set_is_varargs();
3513 if (this->is_builtin())
3514 ret->set_is_builtin();
3518 // Make a copy of a function type with a receiver.
3521 Function_type::copy_with_receiver(Type* receiver_type) const
3523 go_assert(!this->is_method());
3524 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3526 return Type::make_function_type(receiver, this->parameters_,
3527 this->results_, this->location_);
3530 // Make a function type.
3533 Type::make_function_type(Typed_identifier* receiver,
3534 Typed_identifier_list* parameters,
3535 Typed_identifier_list* results,
3538 return new Function_type(receiver, parameters, results, location);
3541 // Class Pointer_type.
3546 Pointer_type::do_traverse(Traverse* traverse)
3548 return Type::traverse(this->to_type_, traverse);
3554 Pointer_type::do_hash_for_method(Gogo* gogo) const
3556 return this->to_type_->hash_for_method(gogo) << 4;
3559 // The tree for a pointer type.
3562 Pointer_type::do_get_backend(Gogo* gogo)
3564 Btype* to_btype = this->to_type_->get_backend(gogo);
3565 return gogo->backend()->pointer_type(to_btype);
3568 // The type of a pointer type descriptor.
3571 Pointer_type::make_pointer_type_descriptor_type()
3576 Type* tdt = Type::make_type_descriptor_type();
3577 Type* ptdt = Type::make_type_descriptor_ptr_type();
3579 Struct_type* s = Type::make_builtin_struct_type(2,
3583 ret = Type::make_builtin_named_type("PtrType", s);
3589 // The type descriptor for a pointer type.
3592 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3594 if (this->is_unsafe_pointer_type())
3596 go_assert(name != NULL);
3597 return this->plain_type_descriptor(gogo,
3598 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3603 Location bloc = Linemap::predeclared_location();
3605 const Methods* methods;
3606 Type* deref = this->points_to();
3607 if (deref->named_type() != NULL)
3608 methods = deref->named_type()->methods();
3609 else if (deref->struct_type() != NULL)
3610 methods = deref->struct_type()->methods();
3614 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3616 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3618 Expression_list* vals = new Expression_list();
3621 Struct_field_list::const_iterator p = fields->begin();
3622 go_assert(p->is_field_name("commonType"));
3623 vals->push_back(this->type_descriptor_constructor(gogo,
3624 RUNTIME_TYPE_KIND_PTR,
3625 name, methods, false));
3628 go_assert(p->is_field_name("elem"));
3629 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3631 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3635 // Reflection string.
3638 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3640 ret->push_back('*');
3641 this->append_reflection(this->to_type_, gogo, ret);
3647 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3649 ret->push_back('p');
3650 this->append_mangled_name(this->to_type_, gogo, ret);
3656 Pointer_type::do_export(Export* exp) const
3658 exp->write_c_string("*");
3659 if (this->is_unsafe_pointer_type())
3660 exp->write_c_string("any");
3662 exp->write_type(this->to_type_);
3668 Pointer_type::do_import(Import* imp)
3670 imp->require_c_string("*");
3671 if (imp->match_c_string("any"))
3674 return Type::make_pointer_type(Type::make_void_type());
3676 Type* to = imp->read_type();
3677 return Type::make_pointer_type(to);
3680 // Make a pointer type.
3683 Type::make_pointer_type(Type* to_type)
3685 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3686 static Hashtable pointer_types;
3687 Hashtable::const_iterator p = pointer_types.find(to_type);
3688 if (p != pointer_types.end())
3690 Pointer_type* ret = new Pointer_type(to_type);
3691 pointer_types[to_type] = ret;
3695 // The nil type. We use a special type for nil because it is not the
3696 // same as any other type. In C term nil has type void*, but there is
3697 // no such type in Go.
3699 class Nil_type : public Type
3708 do_compare_is_identity(Gogo*) const
3712 do_get_backend(Gogo* gogo)
3713 { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3716 do_type_descriptor(Gogo*, Named_type*)
3717 { go_unreachable(); }
3720 do_reflection(Gogo*, std::string*) const
3721 { go_unreachable(); }
3724 do_mangled_name(Gogo*, std::string* ret) const
3725 { ret->push_back('n'); }
3728 // Make the nil type.
3731 Type::make_nil_type()
3733 static Nil_type singleton_nil_type;
3734 return &singleton_nil_type;
3737 // The type of a function call which returns multiple values. This is
3738 // really a struct, but we don't want to confuse a function call which
3739 // returns a struct with a function call which returns multiple
3742 class Call_multiple_result_type : public Type
3745 Call_multiple_result_type(Call_expression* call)
3746 : Type(TYPE_CALL_MULTIPLE_RESULT),
3752 do_has_pointer() const
3754 go_assert(saw_errors());
3759 do_compare_is_identity(Gogo*) const
3763 do_get_backend(Gogo* gogo)
3765 go_assert(saw_errors());
3766 return gogo->backend()->error_type();
3770 do_type_descriptor(Gogo*, Named_type*)
3772 go_assert(saw_errors());
3773 return Expression::make_error(Linemap::unknown_location());
3777 do_reflection(Gogo*, std::string*) const
3778 { go_assert(saw_errors()); }
3781 do_mangled_name(Gogo*, std::string*) const
3782 { go_assert(saw_errors()); }
3785 // The expression being called.
3786 Call_expression* call_;
3789 // Make a call result type.
3792 Type::make_call_multiple_result_type(Call_expression* call)
3794 return new Call_multiple_result_type(call);
3797 // Class Struct_field.
3799 // Get the name of a field.
3802 Struct_field::field_name() const
3804 const std::string& name(this->typed_identifier_.name());
3809 // This is called during parsing, before anything is lowered, so
3810 // we have to be pretty careful to avoid dereferencing an
3811 // unknown type name.
3812 Type* t = this->typed_identifier_.type();
3814 if (t->classification() == Type::TYPE_POINTER)
3817 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3818 dt = ptype->points_to();
3820 if (dt->forward_declaration_type() != NULL)
3821 return dt->forward_declaration_type()->name();
3822 else if (dt->named_type() != NULL)
3823 return dt->named_type()->name();
3824 else if (t->is_error_type() || dt->is_error_type())
3826 static const std::string error_string = "*error*";
3827 return error_string;
3831 // Avoid crashing in the erroneous case where T is named but
3834 if (t->forward_declaration_type() != NULL)
3835 return t->forward_declaration_type()->name();
3836 else if (t->named_type() != NULL)
3837 return t->named_type()->name();
3844 // Return whether this field is named NAME.
3847 Struct_field::is_field_name(const std::string& name) const
3849 const std::string& me(this->typed_identifier_.name());
3854 Type* t = this->typed_identifier_.type();
3855 if (t->points_to() != NULL)
3857 Named_type* nt = t->named_type();
3858 if (nt != NULL && nt->name() == name)
3861 // This is a horrible hack caused by the fact that we don't pack
3862 // the names of builtin types. FIXME.
3865 && nt->name() == Gogo::unpack_hidden_name(name))
3872 // Class Struct_type.
3877 Struct_type::do_traverse(Traverse* traverse)
3879 Struct_field_list* fields = this->fields_;
3882 for (Struct_field_list::iterator p = fields->begin();
3886 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3887 return TRAVERSE_EXIT;
3890 return TRAVERSE_CONTINUE;
3893 // Verify that the struct type is complete and valid.
3896 Struct_type::do_verify()
3898 Struct_field_list* fields = this->fields_;
3902 for (Struct_field_list::iterator p = fields->begin();
3906 Type* t = p->type();
3907 if (t->is_undefined())
3909 error_at(p->location(), "struct field type is incomplete");
3910 p->set_type(Type::make_error_type());
3913 else if (p->is_anonymous())
3915 if (t->named_type() != NULL && t->points_to() != NULL)
3917 error_at(p->location(), "embedded type may not be a pointer");
3918 p->set_type(Type::make_error_type());
3921 if (t->points_to() != NULL
3922 && t->points_to()->interface_type() != NULL)
3924 error_at(p->location(),
3925 "embedded type may not be pointer to interface");
3926 p->set_type(Type::make_error_type());
3934 // Whether this contains a pointer.
3937 Struct_type::do_has_pointer() const
3939 const Struct_field_list* fields = this->fields();
3942 for (Struct_field_list::const_iterator p = fields->begin();
3946 if (p->type()->has_pointer())
3952 // Whether this type is identical to T.
3955 Struct_type::is_identical(const Struct_type* t,
3956 bool errors_are_identical) const
3958 const Struct_field_list* fields1 = this->fields();
3959 const Struct_field_list* fields2 = t->fields();
3960 if (fields1 == NULL || fields2 == NULL)
3961 return fields1 == fields2;
3962 Struct_field_list::const_iterator pf2 = fields2->begin();
3963 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3964 pf1 != fields1->end();
3967 if (pf2 == fields2->end())
3969 if (pf1->field_name() != pf2->field_name())
3971 if (pf1->is_anonymous() != pf2->is_anonymous()
3972 || !Type::are_identical(pf1->type(), pf2->type(),
3973 errors_are_identical, NULL))
3975 if (!pf1->has_tag())
3982 if (!pf2->has_tag())
3984 if (pf1->tag() != pf2->tag())
3988 if (pf2 != fields2->end())
3993 // Whether this struct type has any hidden fields.
3996 Struct_type::struct_has_hidden_fields(const Named_type* within,
3997 std::string* reason) const
3999 const Struct_field_list* fields = this->fields();
4002 const Package* within_package = (within == NULL
4004 : within->named_object()->package());
4005 for (Struct_field_list::const_iterator pf = fields->begin();
4006 pf != fields->end();
4009 if (within_package != NULL
4010 && !pf->is_anonymous()
4011 && Gogo::is_hidden_name(pf->field_name()))
4015 std::string within_name = within->named_object()->message_name();
4016 std::string name = Gogo::message_name(pf->field_name());
4017 size_t bufsize = 200 + within_name.length() + name.length();
4018 char* buf = new char[bufsize];
4019 snprintf(buf, bufsize,
4020 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4021 open_quote, within_name.c_str(), close_quote,
4022 open_quote, name.c_str(), close_quote);
4023 reason->assign(buf);
4029 if (pf->type()->has_hidden_fields(within, reason))
4036 // Whether comparisons of this struct type are simple identity
4040 Struct_type::do_compare_is_identity(Gogo* gogo) const
4042 const Struct_field_list* fields = this->fields_;
4045 unsigned int offset = 0;
4046 for (Struct_field_list::const_iterator pf = fields->begin();
4047 pf != fields->end();
4050 if (!pf->type()->compare_is_identity(gogo))
4053 unsigned int field_align;
4054 if (!pf->type()->backend_type_align(gogo, &field_align))
4056 if ((offset & (field_align - 1)) != 0)
4058 // This struct has padding. We don't guarantee that that
4059 // padding is zero-initialized for a stack variable, so we
4060 // can't use memcmp to compare struct values.
4064 unsigned int field_size;
4065 if (!pf->type()->backend_type_size(gogo, &field_size))
4067 offset += field_size;
4072 // Build identity and hash functions for this struct.
4077 Struct_type::do_hash_for_method(Gogo* gogo) const
4079 unsigned int ret = 0;
4080 if (this->fields() != NULL)
4082 for (Struct_field_list::const_iterator pf = this->fields()->begin();
4083 pf != this->fields()->end();
4085 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4090 // Find the local field NAME.
4093 Struct_type::find_local_field(const std::string& name,
4094 unsigned int *pindex) const
4096 const Struct_field_list* fields = this->fields_;
4100 for (Struct_field_list::const_iterator pf = fields->begin();
4101 pf != fields->end();
4104 if (pf->is_field_name(name))
4114 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4116 Field_reference_expression*
4117 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4118 Location location) const
4121 return this->field_reference_depth(struct_expr, name, location, NULL,
4125 // Return an expression for a field, along with the depth at which it
4128 Field_reference_expression*
4129 Struct_type::field_reference_depth(Expression* struct_expr,
4130 const std::string& name,
4132 Saw_named_type* saw,
4133 unsigned int* depth) const
4135 const Struct_field_list* fields = this->fields_;
4139 // Look for a field with this name.
4141 for (Struct_field_list::const_iterator pf = fields->begin();
4142 pf != fields->end();
4145 if (pf->is_field_name(name))
4148 return Expression::make_field_reference(struct_expr, i, location);
4152 // Look for an anonymous field which contains a field with this
4154 unsigned int found_depth = 0;