Rebase.
[official-gcc.git] / gcc / go / gofrontend / types.cc
blobeba224b4266e030ed238fdad61420e0ad65d0394
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.
7 #include "go-system.h"
9 #include "go-c.h"
10 #include "gogo.h"
11 #include "operator.h"
12 #include "expressions.h"
13 #include "statements.h"
14 #include "export.h"
15 #include "import.h"
16 #include "backend.h"
17 #include "types.h"
19 // Forward declarations so that we don't have to make types.h #include
20 // backend.h.
22 static void
23 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
24 bool use_placeholder,
25 std::vector<Backend::Btyped_identifier>* bfields);
27 static void
28 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
29 std::vector<Backend::Btyped_identifier>* bfields);
31 static void
32 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
33 bool use_placeholder,
34 std::vector<Backend::Btyped_identifier>* bfields);
36 // Class Type.
38 Type::Type(Type_classification classification)
39 : classification_(classification), btype_(NULL), type_descriptor_var_(NULL)
43 Type::~Type()
47 // Get the base type for a type--skip names and forward declarations.
49 Type*
50 Type::base()
52 switch (this->classification_)
54 case TYPE_NAMED:
55 return this->named_type()->named_base();
56 case TYPE_FORWARD:
57 return this->forward_declaration_type()->real_type()->base();
58 default:
59 return this;
63 const Type*
64 Type::base() const
66 switch (this->classification_)
68 case TYPE_NAMED:
69 return this->named_type()->named_base();
70 case TYPE_FORWARD:
71 return this->forward_declaration_type()->real_type()->base();
72 default:
73 return this;
77 // Skip defined forward declarations.
79 Type*
80 Type::forwarded()
82 Type* t = this;
83 Forward_declaration_type* ftype = t->forward_declaration_type();
84 while (ftype != NULL && ftype->is_defined())
86 t = ftype->real_type();
87 ftype = t->forward_declaration_type();
89 return t;
92 const Type*
93 Type::forwarded() const
95 const Type* t = this;
96 const Forward_declaration_type* ftype = t->forward_declaration_type();
97 while (ftype != NULL && ftype->is_defined())
99 t = ftype->real_type();
100 ftype = t->forward_declaration_type();
102 return t;
105 // If this is a named type, return it. Otherwise, return NULL.
107 Named_type*
108 Type::named_type()
110 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
113 const Named_type*
114 Type::named_type() const
116 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
119 // Return true if this type is not defined.
121 bool
122 Type::is_undefined() const
124 return this->forwarded()->forward_declaration_type() != NULL;
127 // Return true if this is a basic type: a type which is not composed
128 // of other types, and is not void.
130 bool
131 Type::is_basic_type() const
133 switch (this->classification_)
135 case TYPE_INTEGER:
136 case TYPE_FLOAT:
137 case TYPE_COMPLEX:
138 case TYPE_BOOLEAN:
139 case TYPE_STRING:
140 case TYPE_NIL:
141 return true;
143 case TYPE_ERROR:
144 case TYPE_VOID:
145 case TYPE_FUNCTION:
146 case TYPE_POINTER:
147 case TYPE_STRUCT:
148 case TYPE_ARRAY:
149 case TYPE_MAP:
150 case TYPE_CHANNEL:
151 case TYPE_INTERFACE:
152 return false;
154 case TYPE_NAMED:
155 case TYPE_FORWARD:
156 return this->base()->is_basic_type();
158 default:
159 go_unreachable();
163 // Return true if this is an abstract type.
165 bool
166 Type::is_abstract() const
168 switch (this->classification())
170 case TYPE_INTEGER:
171 return this->integer_type()->is_abstract();
172 case TYPE_FLOAT:
173 return this->float_type()->is_abstract();
174 case TYPE_COMPLEX:
175 return this->complex_type()->is_abstract();
176 case TYPE_STRING:
177 return this->is_abstract_string_type();
178 case TYPE_BOOLEAN:
179 return this->is_abstract_boolean_type();
180 default:
181 return false;
185 // Return a non-abstract version of an abstract type.
187 Type*
188 Type::make_non_abstract_type()
190 go_assert(this->is_abstract());
191 switch (this->classification())
193 case TYPE_INTEGER:
194 if (this->integer_type()->is_rune())
195 return Type::lookup_integer_type("int32");
196 else
197 return Type::lookup_integer_type("int");
198 case TYPE_FLOAT:
199 return Type::lookup_float_type("float64");
200 case TYPE_COMPLEX:
201 return Type::lookup_complex_type("complex128");
202 case TYPE_STRING:
203 return Type::lookup_string_type();
204 case TYPE_BOOLEAN:
205 return Type::lookup_bool_type();
206 default:
207 go_unreachable();
211 // Return true if this is an error type. Don't give an error if we
212 // try to dereference an undefined forwarding type, as this is called
213 // in the parser when the type may legitimately be undefined.
215 bool
216 Type::is_error_type() const
218 const Type* t = this->forwarded();
219 // Note that we return false for an undefined forward type.
220 switch (t->classification_)
222 case TYPE_ERROR:
223 return true;
224 case TYPE_NAMED:
225 return t->named_type()->is_named_error_type();
226 default:
227 return false;
231 // If this is a pointer type, return the type to which it points.
232 // Otherwise, return NULL.
234 Type*
235 Type::points_to() const
237 const Pointer_type* ptype = this->convert<const Pointer_type,
238 TYPE_POINTER>();
239 return ptype == NULL ? NULL : ptype->points_to();
242 // Return whether this is a slice type.
244 bool
245 Type::is_slice_type() const
247 return this->array_type() != NULL && this->array_type()->length() == NULL;
250 // Return whether this is the predeclared constant nil being used as a
251 // type.
253 bool
254 Type::is_nil_constant_as_type() const
256 const Type* t = this->forwarded();
257 if (t->forward_declaration_type() != NULL)
259 const Named_object* no = t->forward_declaration_type()->named_object();
260 if (no->is_unknown())
261 no = no->unknown_value()->real_named_object();
262 if (no != NULL
263 && no->is_const()
264 && no->const_value()->expr()->is_nil_expression())
265 return true;
267 return false;
270 // Traverse a type.
273 Type::traverse(Type* type, Traverse* traverse)
275 go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
276 || (traverse->traverse_mask()
277 & Traverse::traverse_expressions) != 0);
278 if (traverse->remember_type(type))
280 // We have already traversed this type.
281 return TRAVERSE_CONTINUE;
283 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
285 int t = traverse->type(type);
286 if (t == TRAVERSE_EXIT)
287 return TRAVERSE_EXIT;
288 else if (t == TRAVERSE_SKIP_COMPONENTS)
289 return TRAVERSE_CONTINUE;
291 // An array type has an expression which we need to traverse if
292 // traverse_expressions is set.
293 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
294 return TRAVERSE_EXIT;
295 return TRAVERSE_CONTINUE;
298 // Default implementation for do_traverse for child class.
301 Type::do_traverse(Traverse*)
303 return TRAVERSE_CONTINUE;
306 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
307 // then return true for all erroneous types; this is used to avoid
308 // cascading errors. If REASON is not NULL, optionally set *REASON to
309 // the reason the types are not identical.
311 bool
312 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
313 std::string* reason)
315 if (t1 == NULL || t2 == NULL)
317 // Something is wrong.
318 return errors_are_identical ? true : t1 == t2;
321 // Skip defined forward declarations.
322 t1 = t1->forwarded();
323 t2 = t2->forwarded();
325 // Ignore aliases for purposes of type identity.
326 if (t1->named_type() != NULL && t1->named_type()->is_alias())
327 t1 = t1->named_type()->real_type();
328 if (t2->named_type() != NULL && t2->named_type()->is_alias())
329 t2 = t2->named_type()->real_type();
331 if (t1 == t2)
332 return true;
334 // An undefined forward declaration is an error.
335 if (t1->forward_declaration_type() != NULL
336 || t2->forward_declaration_type() != NULL)
337 return errors_are_identical;
339 // Avoid cascading errors with error types.
340 if (t1->is_error_type() || t2->is_error_type())
342 if (errors_are_identical)
343 return true;
344 return t1->is_error_type() && t2->is_error_type();
347 // Get a good reason for the sink type. Note that the sink type on
348 // the left hand side of an assignment is handled in are_assignable.
349 if (t1->is_sink_type() || t2->is_sink_type())
351 if (reason != NULL)
352 *reason = "invalid use of _";
353 return false;
356 // A named type is only identical to itself.
357 if (t1->named_type() != NULL || t2->named_type() != NULL)
358 return false;
360 // Check type shapes.
361 if (t1->classification() != t2->classification())
362 return false;
364 switch (t1->classification())
366 case TYPE_VOID:
367 case TYPE_BOOLEAN:
368 case TYPE_STRING:
369 case TYPE_NIL:
370 // These types are always identical.
371 return true;
373 case TYPE_INTEGER:
374 return t1->integer_type()->is_identical(t2->integer_type());
376 case TYPE_FLOAT:
377 return t1->float_type()->is_identical(t2->float_type());
379 case TYPE_COMPLEX:
380 return t1->complex_type()->is_identical(t2->complex_type());
382 case TYPE_FUNCTION:
383 return t1->function_type()->is_identical(t2->function_type(),
384 false,
385 errors_are_identical,
386 reason);
388 case TYPE_POINTER:
389 return Type::are_identical(t1->points_to(), t2->points_to(),
390 errors_are_identical, reason);
392 case TYPE_STRUCT:
393 return t1->struct_type()->is_identical(t2->struct_type(),
394 errors_are_identical);
396 case TYPE_ARRAY:
397 return t1->array_type()->is_identical(t2->array_type(),
398 errors_are_identical);
400 case TYPE_MAP:
401 return t1->map_type()->is_identical(t2->map_type(),
402 errors_are_identical);
404 case TYPE_CHANNEL:
405 return t1->channel_type()->is_identical(t2->channel_type(),
406 errors_are_identical);
408 case TYPE_INTERFACE:
409 return t1->interface_type()->is_identical(t2->interface_type(),
410 errors_are_identical);
412 case TYPE_CALL_MULTIPLE_RESULT:
413 if (reason != NULL)
414 *reason = "invalid use of multiple-value function call";
415 return false;
417 default:
418 go_unreachable();
422 // Return true if it's OK to have a binary operation with types LHS
423 // and RHS. This is not used for shifts or comparisons.
425 bool
426 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
428 if (Type::are_identical(lhs, rhs, true, NULL))
429 return true;
431 // A constant of abstract bool type may be mixed with any bool type.
432 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
433 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
434 return true;
436 // A constant of abstract string type may be mixed with any string
437 // type.
438 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
439 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
440 return true;
442 lhs = lhs->base();
443 rhs = rhs->base();
445 // A constant of abstract integer, float, or complex type may be
446 // mixed with an integer, float, or complex type.
447 if ((rhs->is_abstract()
448 && (rhs->integer_type() != NULL
449 || rhs->float_type() != NULL
450 || rhs->complex_type() != NULL)
451 && (lhs->integer_type() != NULL
452 || lhs->float_type() != NULL
453 || lhs->complex_type() != NULL))
454 || (lhs->is_abstract()
455 && (lhs->integer_type() != NULL
456 || lhs->float_type() != NULL
457 || lhs->complex_type() != NULL)
458 && (rhs->integer_type() != NULL
459 || rhs->float_type() != NULL
460 || rhs->complex_type() != NULL)))
461 return true;
463 // The nil type may be compared to a pointer, an interface type, a
464 // slice type, a channel type, a map type, or a function type.
465 if (lhs->is_nil_type()
466 && (rhs->points_to() != NULL
467 || rhs->interface_type() != NULL
468 || rhs->is_slice_type()
469 || rhs->map_type() != NULL
470 || rhs->channel_type() != NULL
471 || rhs->function_type() != NULL))
472 return true;
473 if (rhs->is_nil_type()
474 && (lhs->points_to() != NULL
475 || lhs->interface_type() != NULL
476 || lhs->is_slice_type()
477 || lhs->map_type() != NULL
478 || lhs->channel_type() != NULL
479 || lhs->function_type() != NULL))
480 return true;
482 return false;
485 // Return true if a value with type T1 may be compared with a value of
486 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
488 bool
489 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
490 const Type *t2, std::string *reason)
492 if (t1 != t2
493 && !Type::are_assignable(t1, t2, NULL)
494 && !Type::are_assignable(t2, t1, NULL))
496 if (reason != NULL)
497 *reason = "incompatible types in binary expression";
498 return false;
501 if (!is_equality_op)
503 if (t1->integer_type() == NULL
504 && t1->float_type() == NULL
505 && !t1->is_string_type())
507 if (reason != NULL)
508 *reason = _("invalid comparison of non-ordered type");
509 return false;
512 else if (t1->is_slice_type()
513 || t1->map_type() != NULL
514 || t1->function_type() != NULL
515 || t2->is_slice_type()
516 || t2->map_type() != NULL
517 || t2->function_type() != NULL)
519 if (!t1->is_nil_type() && !t2->is_nil_type())
521 if (reason != NULL)
523 if (t1->is_slice_type() || t2->is_slice_type())
524 *reason = _("slice can only be compared to nil");
525 else if (t1->map_type() != NULL || t2->map_type() != NULL)
526 *reason = _("map can only be compared to nil");
527 else
528 *reason = _("func can only be compared to nil");
530 // Match 6g error messages.
531 if (t1->interface_type() != NULL || t2->interface_type() != NULL)
533 char buf[200];
534 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
535 reason->c_str());
536 *reason = buf;
539 return false;
542 else
544 if (!t1->is_boolean_type()
545 && t1->integer_type() == NULL
546 && t1->float_type() == NULL
547 && t1->complex_type() == NULL
548 && !t1->is_string_type()
549 && t1->points_to() == NULL
550 && t1->channel_type() == NULL
551 && t1->interface_type() == NULL
552 && t1->struct_type() == NULL
553 && t1->array_type() == NULL
554 && !t1->is_nil_type())
556 if (reason != NULL)
557 *reason = _("invalid comparison of non-comparable type");
558 return false;
561 if (t1->named_type() != NULL)
562 return t1->named_type()->named_type_is_comparable(reason);
563 else if (t2->named_type() != NULL)
564 return t2->named_type()->named_type_is_comparable(reason);
565 else if (t1->struct_type() != NULL)
567 const Struct_field_list* fields = t1->struct_type()->fields();
568 for (Struct_field_list::const_iterator p = fields->begin();
569 p != fields->end();
570 ++p)
572 if (!p->type()->is_comparable())
574 if (reason != NULL)
575 *reason = _("invalid comparison of non-comparable struct");
576 return false;
580 else if (t1->array_type() != NULL)
582 if (t1->array_type()->length()->is_nil_expression()
583 || !t1->array_type()->element_type()->is_comparable())
585 if (reason != NULL)
586 *reason = _("invalid comparison of non-comparable array");
587 return false;
592 return true;
595 // Return true if a value with type RHS may be assigned to a variable
596 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
597 // hidden fields are modified. If REASON is not NULL, set *REASON to
598 // the reason the types are not assignable.
600 bool
601 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
602 bool check_hidden_fields,
603 std::string* reason)
605 // Do some checks first. Make sure the types are defined.
606 if (rhs != NULL && !rhs->is_undefined())
608 if (rhs->is_void_type())
610 if (reason != NULL)
611 *reason = "non-value used as value";
612 return false;
614 if (rhs->is_call_multiple_result_type())
616 if (reason != NULL)
617 reason->assign(_("multiple-value function call in "
618 "single-value context"));
619 return false;
623 if (lhs != NULL && !lhs->is_undefined())
625 // Any value may be assigned to the blank identifier.
626 if (lhs->is_sink_type())
627 return true;
629 // All fields of a struct must be exported, or the assignment
630 // must be in the same package.
631 if (check_hidden_fields && rhs != NULL && !rhs->is_undefined())
633 if (lhs->has_hidden_fields(NULL, reason)
634 || rhs->has_hidden_fields(NULL, reason))
635 return false;
639 // Identical types are assignable.
640 if (Type::are_identical(lhs, rhs, true, reason))
641 return true;
643 // The types are assignable if they have identical underlying types
644 // and either LHS or RHS is not a named type.
645 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
646 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
647 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
648 return true;
650 // The types are assignable if LHS is an interface type and RHS
651 // implements the required methods.
652 const Interface_type* lhs_interface_type = lhs->interface_type();
653 if (lhs_interface_type != NULL)
655 if (lhs_interface_type->implements_interface(rhs, reason))
656 return true;
657 const Interface_type* rhs_interface_type = rhs->interface_type();
658 if (rhs_interface_type != NULL
659 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
660 reason))
661 return true;
664 // The type are assignable if RHS is a bidirectional channel type,
665 // LHS is a channel type, they have identical element types, and
666 // either LHS or RHS is not a named type.
667 if (lhs->channel_type() != NULL
668 && rhs->channel_type() != NULL
669 && rhs->channel_type()->may_send()
670 && rhs->channel_type()->may_receive()
671 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
672 && Type::are_identical(lhs->channel_type()->element_type(),
673 rhs->channel_type()->element_type(),
674 true,
675 reason))
676 return true;
678 // The nil type may be assigned to a pointer, function, slice, map,
679 // channel, or interface type.
680 if (rhs->is_nil_type()
681 && (lhs->points_to() != NULL
682 || lhs->function_type() != NULL
683 || lhs->is_slice_type()
684 || lhs->map_type() != NULL
685 || lhs->channel_type() != NULL
686 || lhs->interface_type() != NULL))
687 return true;
689 // An untyped numeric constant may be assigned to a numeric type if
690 // it is representable in that type.
691 if ((rhs->is_abstract()
692 && (rhs->integer_type() != NULL
693 || rhs->float_type() != NULL
694 || rhs->complex_type() != NULL))
695 && (lhs->integer_type() != NULL
696 || lhs->float_type() != NULL
697 || lhs->complex_type() != NULL))
698 return true;
700 // Give some better error messages.
701 if (reason != NULL && reason->empty())
703 if (rhs->interface_type() != NULL)
704 reason->assign(_("need explicit conversion"));
705 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
707 size_t len = (lhs->named_type()->name().length()
708 + rhs->named_type()->name().length()
709 + 100);
710 char* buf = new char[len];
711 snprintf(buf, len, _("cannot use type %s as type %s"),
712 rhs->named_type()->message_name().c_str(),
713 lhs->named_type()->message_name().c_str());
714 reason->assign(buf);
715 delete[] buf;
719 return false;
722 // Return true if a value with type RHS may be assigned to a variable
723 // with type LHS. If REASON is not NULL, set *REASON to the reason
724 // the types are not assignable.
726 bool
727 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
729 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
732 // Like are_assignable but don't check for hidden fields.
734 bool
735 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
736 std::string* reason)
738 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
741 // Return true if a value with type RHS may be converted to type LHS.
742 // If REASON is not NULL, set *REASON to the reason the types are not
743 // convertible.
745 bool
746 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
748 // The types are convertible if they are assignable.
749 if (Type::are_assignable(lhs, rhs, reason))
750 return true;
752 // The types are convertible if they have identical underlying
753 // types.
754 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
755 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
756 return true;
758 // The types are convertible if they are both unnamed pointer types
759 // and their pointer base types have identical underlying types.
760 if (lhs->named_type() == NULL
761 && rhs->named_type() == NULL
762 && lhs->points_to() != NULL
763 && rhs->points_to() != NULL
764 && (lhs->points_to()->named_type() != NULL
765 || rhs->points_to()->named_type() != NULL)
766 && Type::are_identical(lhs->points_to()->base(),
767 rhs->points_to()->base(),
768 true,
769 reason))
770 return true;
772 // Integer and floating point types are convertible to each other.
773 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
774 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
775 return true;
777 // Complex types are convertible to each other.
778 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
779 return true;
781 // An integer, or []byte, or []rune, may be converted to a string.
782 if (lhs->is_string_type())
784 if (rhs->integer_type() != NULL)
785 return true;
786 if (rhs->is_slice_type())
788 const Type* e = rhs->array_type()->element_type()->forwarded();
789 if (e->integer_type() != NULL
790 && (e->integer_type()->is_byte()
791 || e->integer_type()->is_rune()))
792 return true;
796 // A string may be converted to []byte or []rune.
797 if (rhs->is_string_type() && lhs->is_slice_type())
799 const Type* e = lhs->array_type()->element_type()->forwarded();
800 if (e->integer_type() != NULL
801 && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
802 return true;
805 // An unsafe.Pointer type may be converted to any pointer type or to
806 // uintptr, and vice-versa.
807 if (lhs->is_unsafe_pointer_type()
808 && (rhs->points_to() != NULL
809 || (rhs->integer_type() != NULL
810 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
811 return true;
812 if (rhs->is_unsafe_pointer_type()
813 && (lhs->points_to() != NULL
814 || (lhs->integer_type() != NULL
815 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
816 return true;
818 // Give a better error message.
819 if (reason != NULL)
821 if (reason->empty())
822 *reason = "invalid type conversion";
823 else
825 std::string s = "invalid type conversion (";
826 s += *reason;
827 s += ')';
828 *reason = s;
832 return false;
835 // Return whether this type has any hidden fields. This is only a
836 // possibility for a few types.
838 bool
839 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
841 switch (this->forwarded()->classification_)
843 case TYPE_NAMED:
844 return this->named_type()->named_type_has_hidden_fields(reason);
845 case TYPE_STRUCT:
846 return this->struct_type()->struct_has_hidden_fields(within, reason);
847 case TYPE_ARRAY:
848 return this->array_type()->array_has_hidden_fields(within, reason);
849 default:
850 return false;
854 // Return a hash code for the type to be used for method lookup.
856 unsigned int
857 Type::hash_for_method(Gogo* gogo) const
859 unsigned int ret = 0;
860 if (this->classification_ != TYPE_FORWARD)
861 ret += this->classification_;
862 return ret + this->do_hash_for_method(gogo);
865 // Default implementation of do_hash_for_method. This is appropriate
866 // for types with no subfields.
868 unsigned int
869 Type::do_hash_for_method(Gogo*) const
871 return 0;
874 // Return a hash code for a string, given a starting hash.
876 unsigned int
877 Type::hash_string(const std::string& s, unsigned int h)
879 const char* p = s.data();
880 size_t len = s.length();
881 for (; len > 0; --len)
883 h ^= *p++;
884 h*= 16777619;
886 return h;
889 // A hash table mapping unnamed types to the backend representation of
890 // those types.
892 Type::Type_btypes Type::type_btypes;
894 // Return the backend representation for this type.
896 Btype*
897 Type::get_backend(Gogo* gogo)
899 if (this->btype_ != NULL)
900 return this->btype_;
902 if (this->forward_declaration_type() != NULL
903 || this->named_type() != NULL)
904 return this->get_btype_without_hash(gogo);
906 if (this->is_error_type())
907 return gogo->backend()->error_type();
909 // To avoid confusing the backend, translate all identical Go types
910 // to the same backend representation. We use a hash table to do
911 // that. There is no need to use the hash table for named types, as
912 // named types are only identical to themselves.
914 std::pair<Type*, Type_btype_entry> val;
915 val.first = this;
916 val.second.btype = NULL;
917 val.second.is_placeholder = false;
918 std::pair<Type_btypes::iterator, bool> ins =
919 Type::type_btypes.insert(val);
920 if (!ins.second && ins.first->second.btype != NULL)
922 // Note that GOGO can be NULL here, but only when the GCC
923 // middle-end is asking for a frontend type. That will only
924 // happen for simple types, which should never require
925 // placeholders.
926 if (!ins.first->second.is_placeholder)
927 this->btype_ = ins.first->second.btype;
928 else if (gogo->named_types_are_converted())
930 this->finish_backend(gogo, ins.first->second.btype);
931 ins.first->second.is_placeholder = false;
934 return ins.first->second.btype;
937 Btype* bt = this->get_btype_without_hash(gogo);
939 if (ins.first->second.btype == NULL)
941 ins.first->second.btype = bt;
942 ins.first->second.is_placeholder = false;
944 else
946 // We have already created a backend representation for this
947 // type. This can happen when an unnamed type is defined using
948 // a named type which in turns uses an identical unnamed type.
949 // Use the representation we created earlier and ignore the one we just
950 // built.
951 if (this->btype_ == bt)
952 this->btype_ = ins.first->second.btype;
953 bt = ins.first->second.btype;
956 return bt;
959 // Return the backend representation for a type without looking in the
960 // hash table for identical types. This is used for named types,
961 // since a named type is never identical to any other type.
963 Btype*
964 Type::get_btype_without_hash(Gogo* gogo)
966 if (this->btype_ == NULL)
968 Btype* bt = this->do_get_backend(gogo);
970 // For a recursive function or pointer type, we will temporarily
971 // return a circular pointer type during the recursion. We
972 // don't want to record that for a forwarding type, as it may
973 // confuse us later.
974 if (this->forward_declaration_type() != NULL
975 && gogo->backend()->is_circular_pointer_type(bt))
976 return bt;
978 if (gogo == NULL || !gogo->named_types_are_converted())
979 return bt;
981 this->btype_ = bt;
983 return this->btype_;
986 // Get the backend representation of a type without forcing the
987 // creation of the backend representation of all supporting types.
988 // This will return a backend type that has the correct size but may
989 // be incomplete. E.g., a pointer will just be a placeholder pointer,
990 // and will not contain the final representation of the type to which
991 // it points. This is used while converting all named types to the
992 // backend representation, to avoid problems with indirect references
993 // to types which are not yet complete. When this is called, the
994 // sizes of all direct references (e.g., a struct field) should be
995 // known, but the sizes of indirect references (e.g., the type to
996 // which a pointer points) may not.
998 Btype*
999 Type::get_backend_placeholder(Gogo* gogo)
1001 if (gogo->named_types_are_converted())
1002 return this->get_backend(gogo);
1003 if (this->btype_ != NULL)
1004 return this->btype_;
1006 Btype* bt;
1007 switch (this->classification_)
1009 case TYPE_ERROR:
1010 case TYPE_VOID:
1011 case TYPE_BOOLEAN:
1012 case TYPE_INTEGER:
1013 case TYPE_FLOAT:
1014 case TYPE_COMPLEX:
1015 case TYPE_STRING:
1016 case TYPE_NIL:
1017 // These are simple types that can just be created directly.
1018 return this->get_backend(gogo);
1020 case TYPE_MAP:
1021 case TYPE_CHANNEL:
1022 // All maps and channels have the same backend representation.
1023 return this->get_backend(gogo);
1025 case TYPE_NAMED:
1026 case TYPE_FORWARD:
1027 // Named types keep track of their own dependencies and manage
1028 // their own placeholders.
1029 return this->get_backend(gogo);
1031 case TYPE_INTERFACE:
1032 if (this->interface_type()->is_empty())
1033 return Interface_type::get_backend_empty_interface_type(gogo);
1034 break;
1036 default:
1037 break;
1040 std::pair<Type*, Type_btype_entry> val;
1041 val.first = this;
1042 val.second.btype = NULL;
1043 val.second.is_placeholder = false;
1044 std::pair<Type_btypes::iterator, bool> ins =
1045 Type::type_btypes.insert(val);
1046 if (!ins.second && ins.first->second.btype != NULL)
1047 return ins.first->second.btype;
1049 switch (this->classification_)
1051 case TYPE_FUNCTION:
1053 // A Go function type is a pointer to a struct type.
1054 Location loc = this->function_type()->location();
1055 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1057 break;
1059 case TYPE_POINTER:
1061 Location loc = Linemap::unknown_location();
1062 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1064 break;
1066 case TYPE_STRUCT:
1067 // We don't have to make the struct itself be a placeholder. We
1068 // are promised that we know the sizes of the struct fields.
1069 // But we may have to use a placeholder for any particular
1070 // struct field.
1072 std::vector<Backend::Btyped_identifier> bfields;
1073 get_backend_struct_fields(gogo, this->struct_type()->fields(),
1074 true, &bfields);
1075 bt = gogo->backend()->struct_type(bfields);
1077 break;
1079 case TYPE_ARRAY:
1080 if (this->is_slice_type())
1082 std::vector<Backend::Btyped_identifier> bfields;
1083 get_backend_slice_fields(gogo, this->array_type(), true, &bfields);
1084 bt = gogo->backend()->struct_type(bfields);
1086 else
1088 Btype* element = this->array_type()->get_backend_element(gogo, true);
1089 Bexpression* len = this->array_type()->get_backend_length(gogo);
1090 bt = gogo->backend()->array_type(element, len);
1092 break;
1094 case TYPE_INTERFACE:
1096 go_assert(!this->interface_type()->is_empty());
1097 std::vector<Backend::Btyped_identifier> bfields;
1098 get_backend_interface_fields(gogo, this->interface_type(), true,
1099 &bfields);
1100 bt = gogo->backend()->struct_type(bfields);
1102 break;
1104 case TYPE_SINK:
1105 case TYPE_CALL_MULTIPLE_RESULT:
1106 /* Note that various classifications were handled in the earlier
1107 switch. */
1108 default:
1109 go_unreachable();
1112 if (ins.first->second.btype == NULL)
1114 ins.first->second.btype = bt;
1115 ins.first->second.is_placeholder = true;
1117 else
1119 // A placeholder for this type got created along the way. Use
1120 // that one and ignore the one we just built.
1121 bt = ins.first->second.btype;
1124 return bt;
1127 // Complete the backend representation. This is called for a type
1128 // using a placeholder type.
1130 void
1131 Type::finish_backend(Gogo* gogo, Btype *placeholder)
1133 switch (this->classification_)
1135 case TYPE_ERROR:
1136 case TYPE_VOID:
1137 case TYPE_BOOLEAN:
1138 case TYPE_INTEGER:
1139 case TYPE_FLOAT:
1140 case TYPE_COMPLEX:
1141 case TYPE_STRING:
1142 case TYPE_NIL:
1143 go_unreachable();
1145 case TYPE_FUNCTION:
1147 Btype* bt = this->do_get_backend(gogo);
1148 if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1149 go_assert(saw_errors());
1151 break;
1153 case TYPE_POINTER:
1155 Btype* bt = this->do_get_backend(gogo);
1156 if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1157 go_assert(saw_errors());
1159 break;
1161 case TYPE_STRUCT:
1162 // The struct type itself is done, but we have to make sure that
1163 // all the field types are converted.
1164 this->struct_type()->finish_backend_fields(gogo);
1165 break;
1167 case TYPE_ARRAY:
1168 // The array type itself is done, but make sure the element type
1169 // is converted.
1170 this->array_type()->finish_backend_element(gogo);
1171 break;
1173 case TYPE_MAP:
1174 case TYPE_CHANNEL:
1175 go_unreachable();
1177 case TYPE_INTERFACE:
1178 // The interface type itself is done, but make sure the method
1179 // types are converted.
1180 this->interface_type()->finish_backend_methods(gogo);
1181 break;
1183 case TYPE_NAMED:
1184 case TYPE_FORWARD:
1185 go_unreachable();
1187 case TYPE_SINK:
1188 case TYPE_CALL_MULTIPLE_RESULT:
1189 default:
1190 go_unreachable();
1193 this->btype_ = placeholder;
1196 // Return a pointer to the type descriptor for this type.
1198 Bexpression*
1199 Type::type_descriptor_pointer(Gogo* gogo, Location location)
1201 Type* t = this->forwarded();
1202 if (t->named_type() != NULL && t->named_type()->is_alias())
1203 t = t->named_type()->real_type();
1204 if (t->type_descriptor_var_ == NULL)
1206 t->make_type_descriptor_var(gogo);
1207 go_assert(t->type_descriptor_var_ != NULL);
1209 Bexpression* var_expr =
1210 gogo->backend()->var_expression(t->type_descriptor_var_, location);
1211 return gogo->backend()->address_expression(var_expr, location);
1214 // A mapping from unnamed types to type descriptor variables.
1216 Type::Type_descriptor_vars Type::type_descriptor_vars;
1218 // Build the type descriptor for this type.
1220 void
1221 Type::make_type_descriptor_var(Gogo* gogo)
1223 go_assert(this->type_descriptor_var_ == NULL);
1225 Named_type* nt = this->named_type();
1227 // We can have multiple instances of unnamed types, but we only want
1228 // to emit the type descriptor once. We use a hash table. This is
1229 // not necessary for named types, as they are unique, and we store
1230 // the type descriptor in the type itself.
1231 Bvariable** phash = NULL;
1232 if (nt == NULL)
1234 Bvariable* bvnull = NULL;
1235 std::pair<Type_descriptor_vars::iterator, bool> ins =
1236 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1237 if (!ins.second)
1239 // We've already build a type descriptor for this type.
1240 this->type_descriptor_var_ = ins.first->second;
1241 return;
1243 phash = &ins.first->second;
1246 std::string var_name = this->type_descriptor_var_name(gogo, nt);
1248 // Build the contents of the type descriptor.
1249 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1251 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1253 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1255 const Package* dummy;
1256 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1258 this->type_descriptor_var_ =
1259 gogo->backend()->immutable_struct_reference(var_name,
1260 initializer_btype,
1261 loc);
1262 if (phash != NULL)
1263 *phash = this->type_descriptor_var_;
1264 return;
1267 // See if this type descriptor can appear in multiple packages.
1268 bool is_common = false;
1269 if (nt != NULL)
1271 // We create the descriptor for a builtin type whenever we need
1272 // it.
1273 is_common = nt->is_builtin();
1275 else
1277 // This is an unnamed type. The descriptor could be defined in
1278 // any package where it is needed, and the linker will pick one
1279 // descriptor to keep.
1280 is_common = true;
1283 // We are going to build the type descriptor in this package. We
1284 // must create the variable before we convert the initializer to the
1285 // backend representation, because the initializer may refer to the
1286 // type descriptor of this type. By setting type_descriptor_var_ we
1287 // ensure that type_descriptor_pointer will work if called while
1288 // converting INITIALIZER.
1290 this->type_descriptor_var_ =
1291 gogo->backend()->immutable_struct(var_name, false, is_common,
1292 initializer_btype, loc);
1293 if (phash != NULL)
1294 *phash = this->type_descriptor_var_;
1296 Translate_context context(gogo, NULL, NULL, NULL);
1297 context.set_is_const();
1298 Bexpression* binitializer = initializer->get_backend(&context);
1300 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1301 var_name, false, is_common,
1302 initializer_btype, loc,
1303 binitializer);
1306 // Return the name of the type descriptor variable. If NT is not
1307 // NULL, use it to get the name. Otherwise this is an unnamed type.
1309 std::string
1310 Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
1312 if (nt == NULL)
1313 return "__go_td_" + this->mangled_name(gogo);
1315 Named_object* no = nt->named_object();
1316 unsigned int index;
1317 const Named_object* in_function = nt->in_function(&index);
1318 std::string ret = "__go_tdn_";
1319 if (nt->is_builtin())
1320 go_assert(in_function == NULL);
1321 else
1323 const std::string& pkgpath(no->package() == NULL
1324 ? gogo->pkgpath_symbol()
1325 : no->package()->pkgpath_symbol());
1326 ret.append(pkgpath);
1327 ret.append(1, '.');
1328 if (in_function != NULL)
1330 ret.append(Gogo::unpack_hidden_name(in_function->name()));
1331 ret.append(1, '.');
1332 if (index > 0)
1334 char buf[30];
1335 snprintf(buf, sizeof buf, "%u", index);
1336 ret.append(buf);
1337 ret.append(1, '.');
1342 // FIXME: This adds in pkgpath twice for hidden symbols, which is
1343 // pointless.
1344 const std::string& name(no->name());
1345 if (!Gogo::is_hidden_name(name))
1346 ret.append(name);
1347 else
1349 ret.append(1, '.');
1350 ret.append(Gogo::pkgpath_for_symbol(Gogo::hidden_name_pkgpath(name)));
1351 ret.append(1, '.');
1352 ret.append(Gogo::unpack_hidden_name(name));
1355 return ret;
1358 // Return true if this type descriptor is defined in a different
1359 // package. If this returns true it sets *PACKAGE to the package.
1361 bool
1362 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1363 const Package** package)
1365 if (nt != NULL)
1367 if (nt->named_object()->package() != NULL)
1369 // This is a named type defined in a different package. The
1370 // type descriptor should be defined in that package.
1371 *package = nt->named_object()->package();
1372 return true;
1375 else
1377 if (this->points_to() != NULL
1378 && this->points_to()->named_type() != NULL
1379 && this->points_to()->named_type()->named_object()->package() != NULL)
1381 // This is an unnamed pointer to a named type defined in a
1382 // different package. The descriptor should be defined in
1383 // that package.
1384 *package = this->points_to()->named_type()->named_object()->package();
1385 return true;
1388 return false;
1391 // Return a composite literal for a type descriptor.
1393 Expression*
1394 Type::type_descriptor(Gogo* gogo, Type* type)
1396 return type->do_type_descriptor(gogo, NULL);
1399 // Return a composite literal for a type descriptor with a name.
1401 Expression*
1402 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1404 go_assert(name != NULL && type->named_type() != name);
1405 return type->do_type_descriptor(gogo, name);
1408 // Make a builtin struct type from a list of fields. The fields are
1409 // pairs of a name and a type.
1411 Struct_type*
1412 Type::make_builtin_struct_type(int nfields, ...)
1414 va_list ap;
1415 va_start(ap, nfields);
1417 Location bloc = Linemap::predeclared_location();
1418 Struct_field_list* sfl = new Struct_field_list();
1419 for (int i = 0; i < nfields; i++)
1421 const char* field_name = va_arg(ap, const char *);
1422 Type* type = va_arg(ap, Type*);
1423 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1426 va_end(ap);
1428 return Type::make_struct_type(sfl, bloc);
1431 // A list of builtin named types.
1433 std::vector<Named_type*> Type::named_builtin_types;
1435 // Make a builtin named type.
1437 Named_type*
1438 Type::make_builtin_named_type(const char* name, Type* type)
1440 Location bloc = Linemap::predeclared_location();
1441 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1442 Named_type* ret = no->type_value();
1443 Type::named_builtin_types.push_back(ret);
1444 return ret;
1447 // Convert the named builtin types.
1449 void
1450 Type::convert_builtin_named_types(Gogo* gogo)
1452 for (std::vector<Named_type*>::const_iterator p =
1453 Type::named_builtin_types.begin();
1454 p != Type::named_builtin_types.end();
1455 ++p)
1457 bool r = (*p)->verify();
1458 go_assert(r);
1459 (*p)->convert(gogo);
1463 // Return the type of a type descriptor. We should really tie this to
1464 // runtime.Type rather than copying it. This must match commonType in
1465 // libgo/go/runtime/type.go.
1467 Type*
1468 Type::make_type_descriptor_type()
1470 static Type* ret;
1471 if (ret == NULL)
1473 Location bloc = Linemap::predeclared_location();
1475 Type* uint8_type = Type::lookup_integer_type("uint8");
1476 Type* uint32_type = Type::lookup_integer_type("uint32");
1477 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1478 Type* string_type = Type::lookup_string_type();
1479 Type* pointer_string_type = Type::make_pointer_type(string_type);
1481 // This is an unnamed version of unsafe.Pointer. Perhaps we
1482 // should use the named version instead, although that would
1483 // require us to create the unsafe package if it has not been
1484 // imported. It probably doesn't matter.
1485 Type* void_type = Type::make_void_type();
1486 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1488 // Forward declaration for the type descriptor type.
1489 Named_object* named_type_descriptor_type =
1490 Named_object::make_type_declaration("commonType", NULL, bloc);
1491 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1492 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1494 // The type of a method on a concrete type.
1495 Struct_type* method_type =
1496 Type::make_builtin_struct_type(5,
1497 "name", pointer_string_type,
1498 "pkgPath", pointer_string_type,
1499 "mtyp", pointer_type_descriptor_type,
1500 "typ", pointer_type_descriptor_type,
1501 "tfn", unsafe_pointer_type);
1502 Named_type* named_method_type =
1503 Type::make_builtin_named_type("method", method_type);
1505 // Information for types with a name or methods.
1506 Type* slice_named_method_type =
1507 Type::make_array_type(named_method_type, NULL);
1508 Struct_type* uncommon_type =
1509 Type::make_builtin_struct_type(3,
1510 "name", pointer_string_type,
1511 "pkgPath", pointer_string_type,
1512 "methods", slice_named_method_type);
1513 Named_type* named_uncommon_type =
1514 Type::make_builtin_named_type("uncommonType", uncommon_type);
1516 Type* pointer_uncommon_type =
1517 Type::make_pointer_type(named_uncommon_type);
1519 // The type descriptor type.
1521 Struct_type* type_descriptor_type =
1522 Type::make_builtin_struct_type(11,
1523 "Kind", uint8_type,
1524 "align", uint8_type,
1525 "fieldAlign", uint8_type,
1526 "size", uintptr_type,
1527 "hash", uint32_type,
1528 "hashfn", uintptr_type,
1529 "equalfn", uintptr_type,
1530 "string", pointer_string_type,
1531 "", pointer_uncommon_type,
1532 "ptrToThis",
1533 pointer_type_descriptor_type,
1534 "zero", unsafe_pointer_type);
1536 Named_type* named = Type::make_builtin_named_type("commonType",
1537 type_descriptor_type);
1539 named_type_descriptor_type->set_type_value(named);
1541 ret = named;
1544 return ret;
1547 // Make the type of a pointer to a type descriptor as represented in
1548 // Go.
1550 Type*
1551 Type::make_type_descriptor_ptr_type()
1553 static Type* ret;
1554 if (ret == NULL)
1555 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1556 return ret;
1559 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1560 // hash code for this type and which compare whether two values of
1561 // this type are equal. If NAME is not NULL it is the name of this
1562 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1563 // functions, for convenience; they may be NULL.
1565 void
1566 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1567 Function_type* equal_fntype, Named_object** hash_fn,
1568 Named_object** equal_fn)
1570 if (hash_fntype == NULL || equal_fntype == NULL)
1572 Location bloc = Linemap::predeclared_location();
1574 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1575 Type* void_type = Type::make_void_type();
1576 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1578 if (hash_fntype == NULL)
1580 Typed_identifier_list* params = new Typed_identifier_list();
1581 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1582 bloc));
1583 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1585 Typed_identifier_list* results = new Typed_identifier_list();
1586 results->push_back(Typed_identifier("", uintptr_type, bloc));
1588 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1590 if (equal_fntype == NULL)
1592 Typed_identifier_list* params = new Typed_identifier_list();
1593 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1594 bloc));
1595 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1596 bloc));
1597 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1599 Typed_identifier_list* results = new Typed_identifier_list();
1600 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1601 bloc));
1603 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1607 const char* hash_fnname;
1608 const char* equal_fnname;
1609 if (this->compare_is_identity(gogo))
1611 hash_fnname = "__go_type_hash_identity";
1612 equal_fnname = "__go_type_equal_identity";
1614 else if (!this->is_comparable())
1616 hash_fnname = "__go_type_hash_error";
1617 equal_fnname = "__go_type_equal_error";
1619 else
1621 switch (this->base()->classification())
1623 case Type::TYPE_ERROR:
1624 case Type::TYPE_VOID:
1625 case Type::TYPE_NIL:
1626 case Type::TYPE_FUNCTION:
1627 case Type::TYPE_MAP:
1628 // For these types is_comparable should have returned false.
1629 go_unreachable();
1631 case Type::TYPE_BOOLEAN:
1632 case Type::TYPE_INTEGER:
1633 case Type::TYPE_POINTER:
1634 case Type::TYPE_CHANNEL:
1635 // For these types compare_is_identity should have returned true.
1636 go_unreachable();
1638 case Type::TYPE_FLOAT:
1639 hash_fnname = "__go_type_hash_float";
1640 equal_fnname = "__go_type_equal_float";
1641 break;
1643 case Type::TYPE_COMPLEX:
1644 hash_fnname = "__go_type_hash_complex";
1645 equal_fnname = "__go_type_equal_complex";
1646 break;
1648 case Type::TYPE_STRING:
1649 hash_fnname = "__go_type_hash_string";
1650 equal_fnname = "__go_type_equal_string";
1651 break;
1653 case Type::TYPE_STRUCT:
1655 // This is a struct which can not be compared using a
1656 // simple identity function. We need to build a function
1657 // for comparison.
1658 this->specific_type_functions(gogo, name, hash_fntype,
1659 equal_fntype, hash_fn, equal_fn);
1660 return;
1663 case Type::TYPE_ARRAY:
1664 if (this->is_slice_type())
1666 // Type::is_compatible_for_comparison should have
1667 // returned false.
1668 go_unreachable();
1670 else
1672 // This is an array which can not be compared using a
1673 // simple identity function. We need to build a
1674 // function for comparison.
1675 this->specific_type_functions(gogo, name, hash_fntype,
1676 equal_fntype, hash_fn, equal_fn);
1677 return;
1679 break;
1681 case Type::TYPE_INTERFACE:
1682 if (this->interface_type()->is_empty())
1684 hash_fnname = "__go_type_hash_empty_interface";
1685 equal_fnname = "__go_type_equal_empty_interface";
1687 else
1689 hash_fnname = "__go_type_hash_interface";
1690 equal_fnname = "__go_type_equal_interface";
1692 break;
1694 case Type::TYPE_NAMED:
1695 case Type::TYPE_FORWARD:
1696 go_unreachable();
1698 default:
1699 go_unreachable();
1704 Location bloc = Linemap::predeclared_location();
1705 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1706 hash_fntype, bloc);
1707 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1708 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1709 equal_fntype, bloc);
1710 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1713 // A hash table mapping types to the specific hash functions.
1715 Type::Type_functions Type::type_functions_table;
1717 // Handle a type function which is specific to a type: a struct or
1718 // array which can not use an identity comparison.
1720 void
1721 Type::specific_type_functions(Gogo* gogo, Named_type* name,
1722 Function_type* hash_fntype,
1723 Function_type* equal_fntype,
1724 Named_object** hash_fn,
1725 Named_object** equal_fn)
1727 Hash_equal_fn fnull(NULL, NULL);
1728 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1729 std::pair<Type_functions::iterator, bool> ins =
1730 Type::type_functions_table.insert(val);
1731 if (!ins.second)
1733 // We already have functions for this type
1734 *hash_fn = ins.first->second.first;
1735 *equal_fn = ins.first->second.second;
1736 return;
1739 std::string base_name;
1740 if (name == NULL)
1742 // Mangled names can have '.' if they happen to refer to named
1743 // types in some way. That's fine if this is simply a named
1744 // type, but otherwise it will confuse the code that builds
1745 // function identifiers. Remove '.' when necessary.
1746 base_name = this->mangled_name(gogo);
1747 size_t i;
1748 while ((i = base_name.find('.')) != std::string::npos)
1749 base_name[i] = '$';
1750 base_name = gogo->pack_hidden_name(base_name, false);
1752 else
1754 // This name is already hidden or not as appropriate.
1755 base_name = name->name();
1756 unsigned int index;
1757 const Named_object* in_function = name->in_function(&index);
1758 if (in_function != NULL)
1760 base_name += '$' + Gogo::unpack_hidden_name(in_function->name());
1761 if (index > 0)
1763 char buf[30];
1764 snprintf(buf, sizeof buf, "%u", index);
1765 base_name += '$';
1766 base_name += buf;
1770 std::string hash_name = base_name + "$hash";
1771 std::string equal_name = base_name + "$equal";
1773 Location bloc = Linemap::predeclared_location();
1775 const Package* package = NULL;
1776 bool is_defined_elsewhere =
1777 this->type_descriptor_defined_elsewhere(name, &package);
1778 if (is_defined_elsewhere)
1780 *hash_fn = Named_object::make_function_declaration(hash_name, package,
1781 hash_fntype, bloc);
1782 *equal_fn = Named_object::make_function_declaration(equal_name, package,
1783 equal_fntype, bloc);
1785 else
1787 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1788 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1789 bloc);
1792 ins.first->second.first = *hash_fn;
1793 ins.first->second.second = *equal_fn;
1795 if (!is_defined_elsewhere)
1797 if (gogo->in_global_scope())
1798 this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1799 equal_name, equal_fntype);
1800 else
1801 gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1802 equal_name, equal_fntype);
1806 // Write the hash and equality functions for a type which needs to be
1807 // written specially.
1809 void
1810 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1811 const std::string& hash_name,
1812 Function_type* hash_fntype,
1813 const std::string& equal_name,
1814 Function_type* equal_fntype)
1816 Location bloc = Linemap::predeclared_location();
1818 if (gogo->specific_type_functions_are_written())
1820 go_assert(saw_errors());
1821 return;
1824 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1825 bloc);
1826 gogo->start_block(bloc);
1828 if (name != NULL && name->real_type()->named_type() != NULL)
1829 this->write_named_hash(gogo, name, hash_fntype, equal_fntype);
1830 else if (this->struct_type() != NULL)
1831 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1832 equal_fntype);
1833 else if (this->array_type() != NULL)
1834 this->array_type()->write_hash_function(gogo, name, hash_fntype,
1835 equal_fntype);
1836 else
1837 go_unreachable();
1839 Block* b = gogo->finish_block(bloc);
1840 gogo->add_block(b, bloc);
1841 gogo->lower_block(hash_fn, b);
1842 gogo->finish_function(bloc);
1844 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1845 false, bloc);
1846 gogo->start_block(bloc);
1848 if (name != NULL && name->real_type()->named_type() != NULL)
1849 this->write_named_equal(gogo, name);
1850 else if (this->struct_type() != NULL)
1851 this->struct_type()->write_equal_function(gogo, name);
1852 else if (this->array_type() != NULL)
1853 this->array_type()->write_equal_function(gogo, name);
1854 else
1855 go_unreachable();
1857 b = gogo->finish_block(bloc);
1858 gogo->add_block(b, bloc);
1859 gogo->lower_block(equal_fn, b);
1860 gogo->finish_function(bloc);
1863 // Write a hash function that simply calls the hash function for a
1864 // named type. This is used when one named type is defined as
1865 // another. This ensures that this case works when the other named
1866 // type is defined in another package and relies on calling hash
1867 // functions defined only in that package.
1869 void
1870 Type::write_named_hash(Gogo* gogo, Named_type* name,
1871 Function_type* hash_fntype, Function_type* equal_fntype)
1873 Location bloc = Linemap::predeclared_location();
1875 Named_type* base_type = name->real_type()->named_type();
1876 go_assert(base_type != NULL);
1878 // The pointer to the type we are going to hash. This is an
1879 // unsafe.Pointer.
1880 Named_object* key_arg = gogo->lookup("key", NULL);
1881 go_assert(key_arg != NULL);
1883 // The size of the type we are going to hash.
1884 Named_object* keysz_arg = gogo->lookup("key_size", NULL);
1885 go_assert(keysz_arg != NULL);
1887 Named_object* hash_fn;
1888 Named_object* equal_fn;
1889 name->real_type()->type_functions(gogo, base_type, hash_fntype, equal_fntype,
1890 &hash_fn, &equal_fn);
1892 // Call the hash function for the base type.
1893 Expression* key_ref = Expression::make_var_reference(key_arg, bloc);
1894 Expression* keysz_ref = Expression::make_var_reference(keysz_arg, bloc);
1895 Expression_list* args = new Expression_list();
1896 args->push_back(key_ref);
1897 args->push_back(keysz_ref);
1898 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
1899 Expression* call = Expression::make_call(func, args, false, bloc);
1901 // Return the hash of the base type.
1902 Expression_list* vals = new Expression_list();
1903 vals->push_back(call);
1904 Statement* s = Statement::make_return_statement(vals, bloc);
1905 gogo->add_statement(s);
1908 // Write an equality function that simply calls the equality function
1909 // for a named type. This is used when one named type is defined as
1910 // another. This ensures that this case works when the other named
1911 // type is defined in another package and relies on calling equality
1912 // functions defined only in that package.
1914 void
1915 Type::write_named_equal(Gogo* gogo, Named_type* name)
1917 Location bloc = Linemap::predeclared_location();
1919 // The pointers to the types we are going to compare. These have
1920 // type unsafe.Pointer.
1921 Named_object* key1_arg = gogo->lookup("key1", NULL);
1922 Named_object* key2_arg = gogo->lookup("key2", NULL);
1923 go_assert(key1_arg != NULL && key2_arg != NULL);
1925 Named_type* base_type = name->real_type()->named_type();
1926 go_assert(base_type != NULL);
1928 // Build temporaries with the base type.
1929 Type* pt = Type::make_pointer_type(base_type);
1931 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
1932 ref = Expression::make_cast(pt, ref, bloc);
1933 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
1934 gogo->add_statement(p1);
1936 ref = Expression::make_var_reference(key2_arg, bloc);
1937 ref = Expression::make_cast(pt, ref, bloc);
1938 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
1939 gogo->add_statement(p2);
1941 // Compare the values for equality.
1942 Expression* t1 = Expression::make_temporary_reference(p1, bloc);
1943 t1 = Expression::make_unary(OPERATOR_MULT, t1, bloc);
1945 Expression* t2 = Expression::make_temporary_reference(p2, bloc);
1946 t2 = Expression::make_unary(OPERATOR_MULT, t2, bloc);
1948 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, t1, t2, bloc);
1950 // Return the equality comparison.
1951 Expression_list* vals = new Expression_list();
1952 vals->push_back(cond);
1953 Statement* s = Statement::make_return_statement(vals, bloc);
1954 gogo->add_statement(s);
1957 // Return a composite literal for the type descriptor for a plain type
1958 // of kind RUNTIME_TYPE_KIND named NAME.
1960 Expression*
1961 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1962 Named_type* name, const Methods* methods,
1963 bool only_value_methods)
1965 Location bloc = Linemap::predeclared_location();
1967 Type* td_type = Type::make_type_descriptor_type();
1968 const Struct_field_list* fields = td_type->struct_type()->fields();
1970 Expression_list* vals = new Expression_list();
1971 vals->reserve(9);
1973 if (!this->has_pointer())
1974 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1975 Struct_field_list::const_iterator p = fields->begin();
1976 go_assert(p->is_field_name("Kind"));
1977 mpz_t iv;
1978 mpz_init_set_ui(iv, runtime_type_kind);
1979 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1981 ++p;
1982 go_assert(p->is_field_name("align"));
1983 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1984 vals->push_back(Expression::make_type_info(this, type_info));
1986 ++p;
1987 go_assert(p->is_field_name("fieldAlign"));
1988 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1989 vals->push_back(Expression::make_type_info(this, type_info));
1991 ++p;
1992 go_assert(p->is_field_name("size"));
1993 type_info = Expression::TYPE_INFO_SIZE;
1994 vals->push_back(Expression::make_type_info(this, type_info));
1996 ++p;
1997 go_assert(p->is_field_name("hash"));
1998 unsigned int h;
1999 if (name != NULL)
2000 h = name->hash_for_method(gogo);
2001 else
2002 h = this->hash_for_method(gogo);
2003 mpz_set_ui(iv, h);
2004 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
2006 ++p;
2007 go_assert(p->is_field_name("hashfn"));
2008 Function_type* hash_fntype = p->type()->function_type();
2010 ++p;
2011 go_assert(p->is_field_name("equalfn"));
2012 Function_type* equal_fntype = p->type()->function_type();
2014 Named_object* hash_fn;
2015 Named_object* equal_fn;
2016 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
2017 &equal_fn);
2018 vals->push_back(Expression::make_func_code_reference(hash_fn, bloc));
2019 vals->push_back(Expression::make_func_code_reference(equal_fn, bloc));
2021 ++p;
2022 go_assert(p->is_field_name("string"));
2023 Expression* s = Expression::make_string((name != NULL
2024 ? name->reflection(gogo)
2025 : this->reflection(gogo)),
2026 bloc);
2027 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2029 ++p;
2030 go_assert(p->is_field_name("uncommonType"));
2031 if (name == NULL && methods == NULL)
2032 vals->push_back(Expression::make_nil(bloc));
2033 else
2035 if (methods == NULL)
2036 methods = name->methods();
2037 vals->push_back(this->uncommon_type_constructor(gogo,
2038 p->type()->deref(),
2039 name, methods,
2040 only_value_methods));
2043 ++p;
2044 go_assert(p->is_field_name("ptrToThis"));
2045 if (name == NULL)
2046 vals->push_back(Expression::make_nil(bloc));
2047 else
2049 Type* pt = Type::make_pointer_type(name);
2050 vals->push_back(Expression::make_type_descriptor(pt, bloc));
2053 ++p;
2054 go_assert(p->is_field_name("zero"));
2055 Expression* z = Expression::make_var_reference(gogo->zero_value(this), bloc);
2056 z = Expression::make_unary(OPERATOR_AND, z, bloc);
2057 Type* void_type = Type::make_void_type();
2058 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
2059 z = Expression::make_cast(unsafe_pointer_type, z, bloc);
2060 vals->push_back(z);
2062 ++p;
2063 go_assert(p == fields->end());
2065 mpz_clear(iv);
2067 return Expression::make_struct_composite_literal(td_type, vals, bloc);
2070 // Return a composite literal for the uncommon type information for
2071 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
2072 // struct. If name is not NULL, it is the name of the type. If
2073 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
2074 // is true if only value methods should be included. At least one of
2075 // NAME and METHODS must not be NULL.
2077 Expression*
2078 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
2079 Named_type* name, const Methods* methods,
2080 bool only_value_methods) const
2082 Location bloc = Linemap::predeclared_location();
2084 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
2086 Expression_list* vals = new Expression_list();
2087 vals->reserve(3);
2089 Struct_field_list::const_iterator p = fields->begin();
2090 go_assert(p->is_field_name("name"));
2092 ++p;
2093 go_assert(p->is_field_name("pkgPath"));
2095 if (name == NULL)
2097 vals->push_back(Expression::make_nil(bloc));
2098 vals->push_back(Expression::make_nil(bloc));
2100 else
2102 Named_object* no = name->named_object();
2103 std::string n = Gogo::unpack_hidden_name(no->name());
2104 Expression* s = Expression::make_string(n, bloc);
2105 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2107 if (name->is_builtin())
2108 vals->push_back(Expression::make_nil(bloc));
2109 else
2111 const Package* package = no->package();
2112 const std::string& pkgpath(package == NULL
2113 ? gogo->pkgpath()
2114 : package->pkgpath());
2115 n.assign(pkgpath);
2116 unsigned int index;
2117 const Named_object* in_function = name->in_function(&index);
2118 if (in_function != NULL)
2120 n.append(1, '.');
2121 n.append(Gogo::unpack_hidden_name(in_function->name()));
2122 if (index > 0)
2124 char buf[30];
2125 snprintf(buf, sizeof buf, "%u", index);
2126 n.append(1, '.');
2127 n.append(buf);
2130 s = Expression::make_string(n, bloc);
2131 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2135 ++p;
2136 go_assert(p->is_field_name("methods"));
2137 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
2138 only_value_methods));
2140 ++p;
2141 go_assert(p == fields->end());
2143 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
2144 vals, bloc);
2145 return Expression::make_unary(OPERATOR_AND, r, bloc);
2148 // Sort methods by name.
2150 class Sort_methods
2152 public:
2153 bool
2154 operator()(const std::pair<std::string, const Method*>& m1,
2155 const std::pair<std::string, const Method*>& m2) const
2156 { return m1.first < m2.first; }
2159 // Return a composite literal for the type method table for this type.
2160 // METHODS_TYPE is the type of the table, and is a slice type.
2161 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
2162 // then only value methods are used.
2164 Expression*
2165 Type::methods_constructor(Gogo* gogo, Type* methods_type,
2166 const Methods* methods,
2167 bool only_value_methods) const
2169 Location bloc = Linemap::predeclared_location();
2171 std::vector<std::pair<std::string, const Method*> > smethods;
2172 if (methods != NULL)
2174 smethods.reserve(methods->count());
2175 for (Methods::const_iterator p = methods->begin();
2176 p != methods->end();
2177 ++p)
2179 if (p->second->is_ambiguous())
2180 continue;
2181 if (only_value_methods && !p->second->is_value_method())
2182 continue;
2184 // This is where we implement the magic //go:nointerface
2185 // comment. If we saw that comment, we don't add this
2186 // method to the type descriptor.
2187 if (p->second->nointerface())
2188 continue;
2190 smethods.push_back(std::make_pair(p->first, p->second));
2194 if (smethods.empty())
2195 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
2197 std::sort(smethods.begin(), smethods.end(), Sort_methods());
2199 Type* method_type = methods_type->array_type()->element_type();
2201 Expression_list* vals = new Expression_list();
2202 vals->reserve(smethods.size());
2203 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
2204 = smethods.begin();
2205 p != smethods.end();
2206 ++p)
2207 vals->push_back(this->method_constructor(gogo, method_type, p->first,
2208 p->second, only_value_methods));
2210 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
2213 // Return a composite literal for a single method. METHOD_TYPE is the
2214 // type of the entry. METHOD_NAME is the name of the method and M is
2215 // the method information.
2217 Expression*
2218 Type::method_constructor(Gogo*, Type* method_type,
2219 const std::string& method_name,
2220 const Method* m,
2221 bool only_value_methods) const
2223 Location bloc = Linemap::predeclared_location();
2225 const Struct_field_list* fields = method_type->struct_type()->fields();
2227 Expression_list* vals = new Expression_list();
2228 vals->reserve(5);
2230 Struct_field_list::const_iterator p = fields->begin();
2231 go_assert(p->is_field_name("name"));
2232 const std::string n = Gogo::unpack_hidden_name(method_name);
2233 Expression* s = Expression::make_string(n, bloc);
2234 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2236 ++p;
2237 go_assert(p->is_field_name("pkgPath"));
2238 if (!Gogo::is_hidden_name(method_name))
2239 vals->push_back(Expression::make_nil(bloc));
2240 else
2242 s = Expression::make_string(Gogo::hidden_name_pkgpath(method_name),
2243 bloc);
2244 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2247 Named_object* no = (m->needs_stub_method()
2248 ? m->stub_object()
2249 : m->named_object());
2251 Function_type* mtype;
2252 if (no->is_function())
2253 mtype = no->func_value()->type();
2254 else
2255 mtype = no->func_declaration_value()->type();
2256 go_assert(mtype->is_method());
2257 Type* nonmethod_type = mtype->copy_without_receiver();
2259 ++p;
2260 go_assert(p->is_field_name("mtyp"));
2261 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
2263 ++p;
2264 go_assert(p->is_field_name("typ"));
2265 bool want_pointer_receiver = !only_value_methods && m->is_value_method();
2266 nonmethod_type = mtype->copy_with_receiver_as_param(want_pointer_receiver);
2267 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
2269 ++p;
2270 go_assert(p->is_field_name("tfn"));
2271 vals->push_back(Expression::make_func_code_reference(no, bloc));
2273 ++p;
2274 go_assert(p == fields->end());
2276 return Expression::make_struct_composite_literal(method_type, vals, bloc);
2279 // Return a composite literal for the type descriptor of a plain type.
2280 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
2281 // NULL, it is the name to use as well as the list of methods.
2283 Expression*
2284 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
2285 Named_type* name)
2287 return this->type_descriptor_constructor(gogo, runtime_type_kind,
2288 name, NULL, true);
2291 // Return the type reflection string for this type.
2293 std::string
2294 Type::reflection(Gogo* gogo) const
2296 std::string ret;
2298 // The do_reflection virtual function should set RET to the
2299 // reflection string.
2300 this->do_reflection(gogo, &ret);
2302 return ret;
2305 // Return a mangled name for the type.
2307 std::string
2308 Type::mangled_name(Gogo* gogo) const
2310 std::string ret;
2312 // The do_mangled_name virtual function should set RET to the
2313 // mangled name. For a composite type it should append a code for
2314 // the composition and then call do_mangled_name on the components.
2315 this->do_mangled_name(gogo, &ret);
2317 return ret;
2320 // Return whether the backend size of the type is known.
2322 bool
2323 Type::is_backend_type_size_known(Gogo* gogo)
2325 switch (this->classification_)
2327 case TYPE_ERROR:
2328 case TYPE_VOID:
2329 case TYPE_BOOLEAN:
2330 case TYPE_INTEGER:
2331 case TYPE_FLOAT:
2332 case TYPE_COMPLEX:
2333 case TYPE_STRING:
2334 case TYPE_FUNCTION:
2335 case TYPE_POINTER:
2336 case TYPE_NIL:
2337 case TYPE_MAP:
2338 case TYPE_CHANNEL:
2339 case TYPE_INTERFACE:
2340 return true;
2342 case TYPE_STRUCT:
2344 const Struct_field_list* fields = this->struct_type()->fields();
2345 for (Struct_field_list::const_iterator pf = fields->begin();
2346 pf != fields->end();
2347 ++pf)
2348 if (!pf->type()->is_backend_type_size_known(gogo))
2349 return false;
2350 return true;
2353 case TYPE_ARRAY:
2355 const Array_type* at = this->array_type();
2356 if (at->length() == NULL)
2357 return true;
2358 else
2360 Numeric_constant nc;
2361 if (!at->length()->numeric_constant_value(&nc))
2362 return false;
2363 mpz_t ival;
2364 if (!nc.to_int(&ival))
2365 return false;
2366 mpz_clear(ival);
2367 return at->element_type()->is_backend_type_size_known(gogo);
2371 case TYPE_NAMED:
2372 this->named_type()->convert(gogo);
2373 return this->named_type()->is_named_backend_type_size_known();
2375 case TYPE_FORWARD:
2377 Forward_declaration_type* fdt = this->forward_declaration_type();
2378 return fdt->real_type()->is_backend_type_size_known(gogo);
2381 case TYPE_SINK:
2382 case TYPE_CALL_MULTIPLE_RESULT:
2383 go_unreachable();
2385 default:
2386 go_unreachable();
2390 // If the size of the type can be determined, set *PSIZE to the size
2391 // in bytes and return true. Otherwise, return false. This queries
2392 // the backend.
2394 bool
2395 Type::backend_type_size(Gogo* gogo, unsigned long *psize)
2397 if (!this->is_backend_type_size_known(gogo))
2398 return false;
2399 Btype* bt = this->get_backend_placeholder(gogo);
2400 size_t size = gogo->backend()->type_size(bt);
2401 *psize = static_cast<unsigned long>(size);
2402 if (*psize != size)
2403 return false;
2404 return true;
2407 // If the alignment of the type can be determined, set *PALIGN to
2408 // the alignment in bytes and return true. Otherwise, return false.
2410 bool
2411 Type::backend_type_align(Gogo* gogo, unsigned long *palign)
2413 if (!this->is_backend_type_size_known(gogo))
2414 return false;
2415 Btype* bt = this->get_backend_placeholder(gogo);
2416 size_t align = gogo->backend()->type_alignment(bt);
2417 *palign = static_cast<unsigned long>(align);
2418 if (*palign != align)
2419 return false;
2420 return true;
2423 // Like backend_type_align, but return the alignment when used as a
2424 // field.
2426 bool
2427 Type::backend_type_field_align(Gogo* gogo, unsigned long *palign)
2429 if (!this->is_backend_type_size_known(gogo))
2430 return false;
2431 Btype* bt = this->get_backend_placeholder(gogo);
2432 size_t a = gogo->backend()->type_field_alignment(bt);
2433 *palign = static_cast<unsigned long>(a);
2434 if (*palign != a)
2435 return false;
2436 return true;
2439 // Default function to export a type.
2441 void
2442 Type::do_export(Export*) const
2444 go_unreachable();
2447 // Import a type.
2449 Type*
2450 Type::import_type(Import* imp)
2452 if (imp->match_c_string("("))
2453 return Function_type::do_import(imp);
2454 else if (imp->match_c_string("*"))
2455 return Pointer_type::do_import(imp);
2456 else if (imp->match_c_string("struct "))
2457 return Struct_type::do_import(imp);
2458 else if (imp->match_c_string("["))
2459 return Array_type::do_import(imp);
2460 else if (imp->match_c_string("map "))
2461 return Map_type::do_import(imp);
2462 else if (imp->match_c_string("chan "))
2463 return Channel_type::do_import(imp);
2464 else if (imp->match_c_string("interface"))
2465 return Interface_type::do_import(imp);
2466 else
2468 error_at(imp->location(), "import error: expected type");
2469 return Type::make_error_type();
2473 // A type used to indicate a parsing error. This exists to simplify
2474 // later error detection.
2476 class Error_type : public Type
2478 public:
2479 Error_type()
2480 : Type(TYPE_ERROR)
2483 protected:
2484 bool
2485 do_compare_is_identity(Gogo*)
2486 { return false; }
2488 Btype*
2489 do_get_backend(Gogo* gogo)
2490 { return gogo->backend()->error_type(); }
2492 Expression*
2493 do_type_descriptor(Gogo*, Named_type*)
2494 { return Expression::make_error(Linemap::predeclared_location()); }
2496 void
2497 do_reflection(Gogo*, std::string*) const
2498 { go_assert(saw_errors()); }
2500 void
2501 do_mangled_name(Gogo*, std::string* ret) const
2502 { ret->push_back('E'); }
2505 Type*
2506 Type::make_error_type()
2508 static Error_type singleton_error_type;
2509 return &singleton_error_type;
2512 // The void type.
2514 class Void_type : public Type
2516 public:
2517 Void_type()
2518 : Type(TYPE_VOID)
2521 protected:
2522 bool
2523 do_compare_is_identity(Gogo*)
2524 { return false; }
2526 Btype*
2527 do_get_backend(Gogo* gogo)
2528 { return gogo->backend()->void_type(); }
2530 Expression*
2531 do_type_descriptor(Gogo*, Named_type*)
2532 { go_unreachable(); }
2534 void
2535 do_reflection(Gogo*, std::string*) const
2538 void
2539 do_mangled_name(Gogo*, std::string* ret) const
2540 { ret->push_back('v'); }
2543 Type*
2544 Type::make_void_type()
2546 static Void_type singleton_void_type;
2547 return &singleton_void_type;
2550 // The boolean type.
2552 class Boolean_type : public Type
2554 public:
2555 Boolean_type()
2556 : Type(TYPE_BOOLEAN)
2559 protected:
2560 bool
2561 do_compare_is_identity(Gogo*)
2562 { return true; }
2564 Btype*
2565 do_get_backend(Gogo* gogo)
2566 { return gogo->backend()->bool_type(); }
2568 Expression*
2569 do_type_descriptor(Gogo*, Named_type* name);
2571 // We should not be asked for the reflection string of a basic type.
2572 void
2573 do_reflection(Gogo*, std::string* ret) const
2574 { ret->append("bool"); }
2576 void
2577 do_mangled_name(Gogo*, std::string* ret) const
2578 { ret->push_back('b'); }
2581 // Make the type descriptor.
2583 Expression*
2584 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2586 if (name != NULL)
2587 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2588 else
2590 Named_object* no = gogo->lookup_global("bool");
2591 go_assert(no != NULL);
2592 return Type::type_descriptor(gogo, no->type_value());
2596 Type*
2597 Type::make_boolean_type()
2599 static Boolean_type boolean_type;
2600 return &boolean_type;
2603 // The named type "bool".
2605 static Named_type* named_bool_type;
2607 // Get the named type "bool".
2609 Named_type*
2610 Type::lookup_bool_type()
2612 return named_bool_type;
2615 // Make the named type "bool".
2617 Named_type*
2618 Type::make_named_bool_type()
2620 Type* bool_type = Type::make_boolean_type();
2621 Named_object* named_object =
2622 Named_object::make_type("bool", NULL, bool_type,
2623 Linemap::predeclared_location());
2624 Named_type* named_type = named_object->type_value();
2625 named_bool_type = named_type;
2626 return named_type;
2629 // Class Integer_type.
2631 Integer_type::Named_integer_types Integer_type::named_integer_types;
2633 // Create a new integer type. Non-abstract integer types always have
2634 // names.
2636 Named_type*
2637 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2638 int bits, int runtime_type_kind)
2640 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2641 runtime_type_kind);
2642 std::string sname(name);
2643 Named_object* named_object =
2644 Named_object::make_type(sname, NULL, integer_type,
2645 Linemap::predeclared_location());
2646 Named_type* named_type = named_object->type_value();
2647 std::pair<Named_integer_types::iterator, bool> ins =
2648 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2649 go_assert(ins.second);
2650 return named_type;
2653 // Look up an existing integer type.
2655 Named_type*
2656 Integer_type::lookup_integer_type(const char* name)
2658 Named_integer_types::const_iterator p =
2659 Integer_type::named_integer_types.find(name);
2660 go_assert(p != Integer_type::named_integer_types.end());
2661 return p->second;
2664 // Create a new abstract integer type.
2666 Integer_type*
2667 Integer_type::create_abstract_integer_type()
2669 static Integer_type* abstract_type;
2670 if (abstract_type == NULL)
2672 Type* int_type = Type::lookup_integer_type("int");
2673 abstract_type = new Integer_type(true, false,
2674 int_type->integer_type()->bits(),
2675 RUNTIME_TYPE_KIND_INT);
2677 return abstract_type;
2680 // Create a new abstract character type.
2682 Integer_type*
2683 Integer_type::create_abstract_character_type()
2685 static Integer_type* abstract_type;
2686 if (abstract_type == NULL)
2688 abstract_type = new Integer_type(true, false, 32,
2689 RUNTIME_TYPE_KIND_INT32);
2690 abstract_type->set_is_rune();
2692 return abstract_type;
2695 // Integer type compatibility.
2697 bool
2698 Integer_type::is_identical(const Integer_type* t) const
2700 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2701 return false;
2702 return this->is_abstract_ == t->is_abstract_;
2705 // Hash code.
2707 unsigned int
2708 Integer_type::do_hash_for_method(Gogo*) const
2710 return ((this->bits_ << 4)
2711 + ((this->is_unsigned_ ? 1 : 0) << 8)
2712 + ((this->is_abstract_ ? 1 : 0) << 9));
2715 // Convert an Integer_type to the backend representation.
2717 Btype*
2718 Integer_type::do_get_backend(Gogo* gogo)
2720 if (this->is_abstract_)
2722 go_assert(saw_errors());
2723 return gogo->backend()->error_type();
2725 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2728 // The type descriptor for an integer type. Integer types are always
2729 // named.
2731 Expression*
2732 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2734 go_assert(name != NULL || saw_errors());
2735 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2738 // We should not be asked for the reflection string of a basic type.
2740 void
2741 Integer_type::do_reflection(Gogo*, std::string*) const
2743 go_assert(saw_errors());
2746 // Mangled name.
2748 void
2749 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2751 char buf[100];
2752 snprintf(buf, sizeof buf, "i%s%s%de",
2753 this->is_abstract_ ? "a" : "",
2754 this->is_unsigned_ ? "u" : "",
2755 this->bits_);
2756 ret->append(buf);
2759 // Make an integer type.
2761 Named_type*
2762 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2763 int runtime_type_kind)
2765 return Integer_type::create_integer_type(name, is_unsigned, bits,
2766 runtime_type_kind);
2769 // Make an abstract integer type.
2771 Integer_type*
2772 Type::make_abstract_integer_type()
2774 return Integer_type::create_abstract_integer_type();
2777 // Make an abstract character type.
2779 Integer_type*
2780 Type::make_abstract_character_type()
2782 return Integer_type::create_abstract_character_type();
2785 // Look up an integer type.
2787 Named_type*
2788 Type::lookup_integer_type(const char* name)
2790 return Integer_type::lookup_integer_type(name);
2793 // Class Float_type.
2795 Float_type::Named_float_types Float_type::named_float_types;
2797 // Create a new float type. Non-abstract float types always have
2798 // names.
2800 Named_type*
2801 Float_type::create_float_type(const char* name, int bits,
2802 int runtime_type_kind)
2804 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2805 std::string sname(name);
2806 Named_object* named_object =
2807 Named_object::make_type(sname, NULL, float_type,
2808 Linemap::predeclared_location());
2809 Named_type* named_type = named_object->type_value();
2810 std::pair<Named_float_types::iterator, bool> ins =
2811 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2812 go_assert(ins.second);
2813 return named_type;
2816 // Look up an existing float type.
2818 Named_type*
2819 Float_type::lookup_float_type(const char* name)
2821 Named_float_types::const_iterator p =
2822 Float_type::named_float_types.find(name);
2823 go_assert(p != Float_type::named_float_types.end());
2824 return p->second;
2827 // Create a new abstract float type.
2829 Float_type*
2830 Float_type::create_abstract_float_type()
2832 static Float_type* abstract_type;
2833 if (abstract_type == NULL)
2834 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2835 return abstract_type;
2838 // Whether this type is identical with T.
2840 bool
2841 Float_type::is_identical(const Float_type* t) const
2843 if (this->bits_ != t->bits_)
2844 return false;
2845 return this->is_abstract_ == t->is_abstract_;
2848 // Hash code.
2850 unsigned int
2851 Float_type::do_hash_for_method(Gogo*) const
2853 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2856 // Convert to the backend representation.
2858 Btype*
2859 Float_type::do_get_backend(Gogo* gogo)
2861 return gogo->backend()->float_type(this->bits_);
2864 // The type descriptor for a float type. Float types are always named.
2866 Expression*
2867 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2869 go_assert(name != NULL || saw_errors());
2870 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2873 // We should not be asked for the reflection string of a basic type.
2875 void
2876 Float_type::do_reflection(Gogo*, std::string*) const
2878 go_assert(saw_errors());
2881 // Mangled name.
2883 void
2884 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2886 char buf[100];
2887 snprintf(buf, sizeof buf, "f%s%de",
2888 this->is_abstract_ ? "a" : "",
2889 this->bits_);
2890 ret->append(buf);
2893 // Make a floating point type.
2895 Named_type*
2896 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2898 return Float_type::create_float_type(name, bits, runtime_type_kind);
2901 // Make an abstract float type.
2903 Float_type*
2904 Type::make_abstract_float_type()
2906 return Float_type::create_abstract_float_type();
2909 // Look up a float type.
2911 Named_type*
2912 Type::lookup_float_type(const char* name)
2914 return Float_type::lookup_float_type(name);
2917 // Class Complex_type.
2919 Complex_type::Named_complex_types Complex_type::named_complex_types;
2921 // Create a new complex type. Non-abstract complex types always have
2922 // names.
2924 Named_type*
2925 Complex_type::create_complex_type(const char* name, int bits,
2926 int runtime_type_kind)
2928 Complex_type* complex_type = new Complex_type(false, bits,
2929 runtime_type_kind);
2930 std::string sname(name);
2931 Named_object* named_object =
2932 Named_object::make_type(sname, NULL, complex_type,
2933 Linemap::predeclared_location());
2934 Named_type* named_type = named_object->type_value();
2935 std::pair<Named_complex_types::iterator, bool> ins =
2936 Complex_type::named_complex_types.insert(std::make_pair(sname,
2937 named_type));
2938 go_assert(ins.second);
2939 return named_type;
2942 // Look up an existing complex type.
2944 Named_type*
2945 Complex_type::lookup_complex_type(const char* name)
2947 Named_complex_types::const_iterator p =
2948 Complex_type::named_complex_types.find(name);
2949 go_assert(p != Complex_type::named_complex_types.end());
2950 return p->second;
2953 // Create a new abstract complex type.
2955 Complex_type*
2956 Complex_type::create_abstract_complex_type()
2958 static Complex_type* abstract_type;
2959 if (abstract_type == NULL)
2960 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2961 return abstract_type;
2964 // Whether this type is identical with T.
2966 bool
2967 Complex_type::is_identical(const Complex_type *t) const
2969 if (this->bits_ != t->bits_)
2970 return false;
2971 return this->is_abstract_ == t->is_abstract_;
2974 // Hash code.
2976 unsigned int
2977 Complex_type::do_hash_for_method(Gogo*) const
2979 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2982 // Convert to the backend representation.
2984 Btype*
2985 Complex_type::do_get_backend(Gogo* gogo)
2987 return gogo->backend()->complex_type(this->bits_);
2990 // The type descriptor for a complex type. Complex types are always
2991 // named.
2993 Expression*
2994 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2996 go_assert(name != NULL || saw_errors());
2997 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
3000 // We should not be asked for the reflection string of a basic type.
3002 void
3003 Complex_type::do_reflection(Gogo*, std::string*) const
3005 go_assert(saw_errors());
3008 // Mangled name.
3010 void
3011 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
3013 char buf[100];
3014 snprintf(buf, sizeof buf, "c%s%de",
3015 this->is_abstract_ ? "a" : "",
3016 this->bits_);
3017 ret->append(buf);
3020 // Make a complex type.
3022 Named_type*
3023 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
3025 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
3028 // Make an abstract complex type.
3030 Complex_type*
3031 Type::make_abstract_complex_type()
3033 return Complex_type::create_abstract_complex_type();
3036 // Look up a complex type.
3038 Named_type*
3039 Type::lookup_complex_type(const char* name)
3041 return Complex_type::lookup_complex_type(name);
3044 // Class String_type.
3046 // Convert String_type to the backend representation. A string is a
3047 // struct with two fields: a pointer to the characters and a length.
3049 Btype*
3050 String_type::do_get_backend(Gogo* gogo)
3052 static Btype* backend_string_type;
3053 if (backend_string_type == NULL)
3055 std::vector<Backend::Btyped_identifier> fields(2);
3057 Type* b = gogo->lookup_global("byte")->type_value();
3058 Type* pb = Type::make_pointer_type(b);
3060 // We aren't going to get back to this field to finish the
3061 // backend representation, so force it to be finished now.
3062 if (!gogo->named_types_are_converted())
3064 Btype* bt = pb->get_backend_placeholder(gogo);
3065 pb->finish_backend(gogo, bt);
3068 fields[0].name = "__data";
3069 fields[0].btype = pb->get_backend(gogo);
3070 fields[0].location = Linemap::predeclared_location();
3072 Type* int_type = Type::lookup_integer_type("int");
3073 fields[1].name = "__length";
3074 fields[1].btype = int_type->get_backend(gogo);
3075 fields[1].location = fields[0].location;
3077 backend_string_type = gogo->backend()->struct_type(fields);
3079 return backend_string_type;
3082 // The type descriptor for the string type.
3084 Expression*
3085 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3087 if (name != NULL)
3088 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
3089 else
3091 Named_object* no = gogo->lookup_global("string");
3092 go_assert(no != NULL);
3093 return Type::type_descriptor(gogo, no->type_value());
3097 // We should not be asked for the reflection string of a basic type.
3099 void
3100 String_type::do_reflection(Gogo*, std::string* ret) const
3102 ret->append("string");
3105 // Mangled name of a string type.
3107 void
3108 String_type::do_mangled_name(Gogo*, std::string* ret) const
3110 ret->push_back('z');
3113 // Make a string type.
3115 Type*
3116 Type::make_string_type()
3118 static String_type string_type;
3119 return &string_type;
3122 // The named type "string".
3124 static Named_type* named_string_type;
3126 // Get the named type "string".
3128 Named_type*
3129 Type::lookup_string_type()
3131 return named_string_type;
3134 // Make the named type string.
3136 Named_type*
3137 Type::make_named_string_type()
3139 Type* string_type = Type::make_string_type();
3140 Named_object* named_object =
3141 Named_object::make_type("string", NULL, string_type,
3142 Linemap::predeclared_location());
3143 Named_type* named_type = named_object->type_value();
3144 named_string_type = named_type;
3145 return named_type;
3148 // The sink type. This is the type of the blank identifier _. Any
3149 // type may be assigned to it.
3151 class Sink_type : public Type
3153 public:
3154 Sink_type()
3155 : Type(TYPE_SINK)
3158 protected:
3159 bool
3160 do_compare_is_identity(Gogo*)
3161 { return false; }
3163 Btype*
3164 do_get_backend(Gogo*)
3165 { go_unreachable(); }
3167 Expression*
3168 do_type_descriptor(Gogo*, Named_type*)
3169 { go_unreachable(); }
3171 void
3172 do_reflection(Gogo*, std::string*) const
3173 { go_unreachable(); }
3175 void
3176 do_mangled_name(Gogo*, std::string*) const
3177 { go_unreachable(); }
3180 // Make the sink type.
3182 Type*
3183 Type::make_sink_type()
3185 static Sink_type sink_type;
3186 return &sink_type;
3189 // Class Function_type.
3191 // Traversal.
3194 Function_type::do_traverse(Traverse* traverse)
3196 if (this->receiver_ != NULL
3197 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
3198 return TRAVERSE_EXIT;
3199 if (this->parameters_ != NULL
3200 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
3201 return TRAVERSE_EXIT;
3202 if (this->results_ != NULL
3203 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
3204 return TRAVERSE_EXIT;
3205 return TRAVERSE_CONTINUE;
3208 // Returns whether T is a valid redeclaration of this type. If this
3209 // returns false, and REASON is not NULL, *REASON may be set to a
3210 // brief explanation of why it returned false.
3212 bool
3213 Function_type::is_valid_redeclaration(const Function_type* t,
3214 std::string* reason) const
3216 if (!this->is_identical(t, false, true, reason))
3217 return false;
3219 // A redeclaration of a function is required to use the same names
3220 // for the receiver and parameters.
3221 if (this->receiver() != NULL
3222 && this->receiver()->name() != t->receiver()->name())
3224 if (reason != NULL)
3225 *reason = "receiver name changed";
3226 return false;
3229 const Typed_identifier_list* parms1 = this->parameters();
3230 const Typed_identifier_list* parms2 = t->parameters();
3231 if (parms1 != NULL)
3233 Typed_identifier_list::const_iterator p1 = parms1->begin();
3234 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3235 p2 != parms2->end();
3236 ++p2, ++p1)
3238 if (p1->name() != p2->name())
3240 if (reason != NULL)
3241 *reason = "parameter name changed";
3242 return false;
3245 // This is called at parse time, so we may have unknown
3246 // types.
3247 Type* t1 = p1->type()->forwarded();
3248 Type* t2 = p2->type()->forwarded();
3249 if (t1 != t2
3250 && t1->forward_declaration_type() != NULL
3251 && (t2->forward_declaration_type() == NULL
3252 || (t1->forward_declaration_type()->named_object()
3253 != t2->forward_declaration_type()->named_object())))
3254 return false;
3258 const Typed_identifier_list* results1 = this->results();
3259 const Typed_identifier_list* results2 = t->results();
3260 if (results1 != NULL)
3262 Typed_identifier_list::const_iterator res1 = results1->begin();
3263 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3264 res2 != results2->end();
3265 ++res2, ++res1)
3267 if (res1->name() != res2->name())
3269 if (reason != NULL)
3270 *reason = "result name changed";
3271 return false;
3274 // This is called at parse time, so we may have unknown
3275 // types.
3276 Type* t1 = res1->type()->forwarded();
3277 Type* t2 = res2->type()->forwarded();
3278 if (t1 != t2
3279 && t1->forward_declaration_type() != NULL
3280 && (t2->forward_declaration_type() == NULL
3281 || (t1->forward_declaration_type()->named_object()
3282 != t2->forward_declaration_type()->named_object())))
3283 return false;
3287 return true;
3290 // Check whether T is the same as this type.
3292 bool
3293 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
3294 bool errors_are_identical,
3295 std::string* reason) const
3297 if (!ignore_receiver)
3299 const Typed_identifier* r1 = this->receiver();
3300 const Typed_identifier* r2 = t->receiver();
3301 if ((r1 != NULL) != (r2 != NULL))
3303 if (reason != NULL)
3304 *reason = _("different receiver types");
3305 return false;
3307 if (r1 != NULL)
3309 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
3310 reason))
3312 if (reason != NULL && !reason->empty())
3313 *reason = "receiver: " + *reason;
3314 return false;
3319 const Typed_identifier_list* parms1 = this->parameters();
3320 const Typed_identifier_list* parms2 = t->parameters();
3321 if ((parms1 != NULL) != (parms2 != NULL))
3323 if (reason != NULL)
3324 *reason = _("different number of parameters");
3325 return false;
3327 if (parms1 != NULL)
3329 Typed_identifier_list::const_iterator p1 = parms1->begin();
3330 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3331 p2 != parms2->end();
3332 ++p2, ++p1)
3334 if (p1 == parms1->end())
3336 if (reason != NULL)
3337 *reason = _("different number of parameters");
3338 return false;
3341 if (!Type::are_identical(p1->type(), p2->type(),
3342 errors_are_identical, NULL))
3344 if (reason != NULL)
3345 *reason = _("different parameter types");
3346 return false;
3349 if (p1 != parms1->end())
3351 if (reason != NULL)
3352 *reason = _("different number of parameters");
3353 return false;
3357 if (this->is_varargs() != t->is_varargs())
3359 if (reason != NULL)
3360 *reason = _("different varargs");
3361 return false;
3364 const Typed_identifier_list* results1 = this->results();
3365 const Typed_identifier_list* results2 = t->results();
3366 if ((results1 != NULL) != (results2 != NULL))
3368 if (reason != NULL)
3369 *reason = _("different number of results");
3370 return false;
3372 if (results1 != NULL)
3374 Typed_identifier_list::const_iterator res1 = results1->begin();
3375 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3376 res2 != results2->end();
3377 ++res2, ++res1)
3379 if (res1 == results1->end())
3381 if (reason != NULL)
3382 *reason = _("different number of results");
3383 return false;
3386 if (!Type::are_identical(res1->type(), res2->type(),
3387 errors_are_identical, NULL))
3389 if (reason != NULL)
3390 *reason = _("different result types");
3391 return false;
3394 if (res1 != results1->end())
3396 if (reason != NULL)
3397 *reason = _("different number of results");
3398 return false;
3402 return true;
3405 // Hash code.
3407 unsigned int
3408 Function_type::do_hash_for_method(Gogo* gogo) const
3410 unsigned int ret = 0;
3411 // We ignore the receiver type for hash codes, because we need to
3412 // get the same hash code for a method in an interface and a method
3413 // declared for a type. The former will not have a receiver.
3414 if (this->parameters_ != NULL)
3416 int shift = 1;
3417 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3418 p != this->parameters_->end();
3419 ++p, ++shift)
3420 ret += p->type()->hash_for_method(gogo) << shift;
3422 if (this->results_ != NULL)
3424 int shift = 2;
3425 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3426 p != this->results_->end();
3427 ++p, ++shift)
3428 ret += p->type()->hash_for_method(gogo) << shift;
3430 if (this->is_varargs_)
3431 ret += 1;
3432 ret <<= 4;
3433 return ret;
3436 // Hash result parameters.
3438 unsigned int
3439 Function_type::Results_hash::operator()(const Typed_identifier_list* t) const
3441 unsigned int hash = 0;
3442 for (Typed_identifier_list::const_iterator p = t->begin();
3443 p != t->end();
3444 ++p)
3446 hash <<= 2;
3447 hash = Type::hash_string(p->name(), hash);
3448 hash += p->type()->hash_for_method(NULL);
3450 return hash;
3453 // Compare result parameters so that can map identical result
3454 // parameters to a single struct type.
3456 bool
3457 Function_type::Results_equal::operator()(const Typed_identifier_list* a,
3458 const Typed_identifier_list* b) const
3460 if (a->size() != b->size())
3461 return false;
3462 Typed_identifier_list::const_iterator pa = a->begin();
3463 for (Typed_identifier_list::const_iterator pb = b->begin();
3464 pb != b->end();
3465 ++pa, ++pb)
3467 if (pa->name() != pb->name()
3468 || !Type::are_identical(pa->type(), pb->type(), true, NULL))
3469 return false;
3471 return true;
3474 // Hash from results to a backend struct type.
3476 Function_type::Results_structs Function_type::results_structs;
3478 // Get the backend representation for a function type.
3480 Btype*
3481 Function_type::get_backend_fntype(Gogo* gogo)
3483 if (this->fnbtype_ == NULL)
3485 Backend::Btyped_identifier breceiver;
3486 if (this->receiver_ != NULL)
3488 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3490 // We always pass the address of the receiver parameter, in
3491 // order to make interface calls work with unknown types.
3492 Type* rtype = this->receiver_->type();
3493 if (rtype->points_to() == NULL)
3494 rtype = Type::make_pointer_type(rtype);
3495 breceiver.btype = rtype->get_backend(gogo);
3496 breceiver.location = this->receiver_->location();
3499 std::vector<Backend::Btyped_identifier> bparameters;
3500 if (this->parameters_ != NULL)
3502 bparameters.resize(this->parameters_->size());
3503 size_t i = 0;
3504 for (Typed_identifier_list::const_iterator p =
3505 this->parameters_->begin(); p != this->parameters_->end();
3506 ++p, ++i)
3508 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3509 bparameters[i].btype = p->type()->get_backend(gogo);
3510 bparameters[i].location = p->location();
3512 go_assert(i == bparameters.size());
3515 std::vector<Backend::Btyped_identifier> bresults;
3516 Btype* bresult_struct = NULL;
3517 if (this->results_ != NULL)
3519 bresults.resize(this->results_->size());
3520 size_t i = 0;
3521 for (Typed_identifier_list::const_iterator p =
3522 this->results_->begin();
3523 p != this->results_->end();
3524 ++p, ++i)
3526 bresults[i].name = Gogo::unpack_hidden_name(p->name());
3527 bresults[i].btype = p->type()->get_backend(gogo);
3528 bresults[i].location = p->location();
3530 go_assert(i == bresults.size());
3532 if (this->results_->size() > 1)
3534 // Use the same results struct for all functions that
3535 // return the same set of results. This is useful to
3536 // unify calls to interface methods with other calls.
3537 std::pair<Typed_identifier_list*, Btype*> val;
3538 val.first = this->results_;
3539 val.second = NULL;
3540 std::pair<Results_structs::iterator, bool> ins =
3541 Function_type::results_structs.insert(val);
3542 if (ins.second)
3544 // Build a new struct type.
3545 Struct_field_list* sfl = new Struct_field_list;
3546 for (Typed_identifier_list::const_iterator p =
3547 this->results_->begin();
3548 p != this->results_->end();
3549 ++p)
3551 Typed_identifier tid = *p;
3552 if (tid.name().empty())
3553 tid = Typed_identifier("UNNAMED", tid.type(),
3554 tid.location());
3555 sfl->push_back(Struct_field(tid));
3557 Struct_type* st = Type::make_struct_type(sfl,
3558 this->location());
3559 ins.first->second = st->get_backend(gogo);
3561 bresult_struct = ins.first->second;
3565 this->fnbtype_ = gogo->backend()->function_type(breceiver, bparameters,
3566 bresults, bresult_struct,
3567 this->location());
3571 return this->fnbtype_;
3574 // Get the backend representation for a Go function type.
3576 Btype*
3577 Function_type::do_get_backend(Gogo* gogo)
3579 // When we do anything with a function value other than call it, it
3580 // is represented as a pointer to a struct whose first field is the
3581 // actual function. So that is what we return as the type of a Go
3582 // function.
3584 Location loc = this->location();
3585 Btype* struct_type =
3586 gogo->backend()->placeholder_struct_type("__go_descriptor", loc);
3587 Btype* ptr_struct_type = gogo->backend()->pointer_type(struct_type);
3589 std::vector<Backend::Btyped_identifier> fields(1);
3590 fields[0].name = "code";
3591 fields[0].btype = this->get_backend_fntype(gogo);
3592 fields[0].location = loc;
3593 if (!gogo->backend()->set_placeholder_struct_type(struct_type, fields))
3594 return gogo->backend()->error_type();
3595 return ptr_struct_type;
3598 // The type of a function type descriptor.
3600 Type*
3601 Function_type::make_function_type_descriptor_type()
3603 static Type* ret;
3604 if (ret == NULL)
3606 Type* tdt = Type::make_type_descriptor_type();
3607 Type* ptdt = Type::make_type_descriptor_ptr_type();
3609 Type* bool_type = Type::lookup_bool_type();
3611 Type* slice_type = Type::make_array_type(ptdt, NULL);
3613 Struct_type* s = Type::make_builtin_struct_type(4,
3614 "", tdt,
3615 "dotdotdot", bool_type,
3616 "in", slice_type,
3617 "out", slice_type);
3619 ret = Type::make_builtin_named_type("FuncType", s);
3622 return ret;
3625 // The type descriptor for a function type.
3627 Expression*
3628 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3630 Location bloc = Linemap::predeclared_location();
3632 Type* ftdt = Function_type::make_function_type_descriptor_type();
3634 const Struct_field_list* fields = ftdt->struct_type()->fields();
3636 Expression_list* vals = new Expression_list();
3637 vals->reserve(4);
3639 Struct_field_list::const_iterator p = fields->begin();
3640 go_assert(p->is_field_name("commonType"));
3641 vals->push_back(this->type_descriptor_constructor(gogo,
3642 RUNTIME_TYPE_KIND_FUNC,
3643 name, NULL, true));
3645 ++p;
3646 go_assert(p->is_field_name("dotdotdot"));
3647 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3649 ++p;
3650 go_assert(p->is_field_name("in"));
3651 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3652 this->parameters()));
3654 ++p;
3655 go_assert(p->is_field_name("out"));
3656 vals->push_back(this->type_descriptor_params(p->type(), NULL,
3657 this->results()));
3659 ++p;
3660 go_assert(p == fields->end());
3662 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3665 // Return a composite literal for the parameters or results of a type
3666 // descriptor.
3668 Expression*
3669 Function_type::type_descriptor_params(Type* params_type,
3670 const Typed_identifier* receiver,
3671 const Typed_identifier_list* params)
3673 Location bloc = Linemap::predeclared_location();
3675 if (receiver == NULL && params == NULL)
3676 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3678 Expression_list* vals = new Expression_list();
3679 vals->reserve((params == NULL ? 0 : params->size())
3680 + (receiver != NULL ? 1 : 0));
3682 if (receiver != NULL)
3683 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3685 if (params != NULL)
3687 for (Typed_identifier_list::const_iterator p = params->begin();
3688 p != params->end();
3689 ++p)
3690 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3693 return Expression::make_slice_composite_literal(params_type, vals, bloc);
3696 // The reflection string.
3698 void
3699 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3701 // FIXME: Turn this off until we straighten out the type of the
3702 // struct field used in a go statement which calls a method.
3703 // go_assert(this->receiver_ == NULL);
3705 ret->append("func");
3707 if (this->receiver_ != NULL)
3709 ret->push_back('(');
3710 this->append_reflection(this->receiver_->type(), gogo, ret);
3711 ret->push_back(')');
3714 ret->push_back('(');
3715 const Typed_identifier_list* params = this->parameters();
3716 if (params != NULL)
3718 bool is_varargs = this->is_varargs_;
3719 for (Typed_identifier_list::const_iterator p = params->begin();
3720 p != params->end();
3721 ++p)
3723 if (p != params->begin())
3724 ret->append(", ");
3725 if (!is_varargs || p + 1 != params->end())
3726 this->append_reflection(p->type(), gogo, ret);
3727 else
3729 ret->append("...");
3730 this->append_reflection(p->type()->array_type()->element_type(),
3731 gogo, ret);
3735 ret->push_back(')');
3737 const Typed_identifier_list* results = this->results();
3738 if (results != NULL && !results->empty())
3740 if (results->size() == 1)
3741 ret->push_back(' ');
3742 else
3743 ret->append(" (");
3744 for (Typed_identifier_list::const_iterator p = results->begin();
3745 p != results->end();
3746 ++p)
3748 if (p != results->begin())
3749 ret->append(", ");
3750 this->append_reflection(p->type(), gogo, ret);
3752 if (results->size() > 1)
3753 ret->push_back(')');
3757 // Mangled name.
3759 void
3760 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3762 ret->push_back('F');
3764 if (this->receiver_ != NULL)
3766 ret->push_back('m');
3767 this->append_mangled_name(this->receiver_->type(), gogo, ret);
3770 const Typed_identifier_list* params = this->parameters();
3771 if (params != NULL)
3773 ret->push_back('p');
3774 for (Typed_identifier_list::const_iterator p = params->begin();
3775 p != params->end();
3776 ++p)
3777 this->append_mangled_name(p->type(), gogo, ret);
3778 if (this->is_varargs_)
3779 ret->push_back('V');
3780 ret->push_back('e');
3783 const Typed_identifier_list* results = this->results();
3784 if (results != NULL)
3786 ret->push_back('r');
3787 for (Typed_identifier_list::const_iterator p = results->begin();
3788 p != results->end();
3789 ++p)
3790 this->append_mangled_name(p->type(), gogo, ret);
3791 ret->push_back('e');
3794 ret->push_back('e');
3797 // Export a function type.
3799 void
3800 Function_type::do_export(Export* exp) const
3802 // We don't write out the receiver. The only function types which
3803 // should have a receiver are the ones associated with explicitly
3804 // defined methods. For those the receiver type is written out by
3805 // Function::export_func.
3807 exp->write_c_string("(");
3808 bool first = true;
3809 if (this->parameters_ != NULL)
3811 bool is_varargs = this->is_varargs_;
3812 for (Typed_identifier_list::const_iterator p =
3813 this->parameters_->begin();
3814 p != this->parameters_->end();
3815 ++p)
3817 if (first)
3818 first = false;
3819 else
3820 exp->write_c_string(", ");
3821 exp->write_name(p->name());
3822 exp->write_c_string(" ");
3823 if (!is_varargs || p + 1 != this->parameters_->end())
3824 exp->write_type(p->type());
3825 else
3827 exp->write_c_string("...");
3828 exp->write_type(p->type()->array_type()->element_type());
3832 exp->write_c_string(")");
3834 const Typed_identifier_list* results = this->results_;
3835 if (results != NULL)
3837 exp->write_c_string(" ");
3838 if (results->size() == 1 && results->begin()->name().empty())
3839 exp->write_type(results->begin()->type());
3840 else
3842 first = true;
3843 exp->write_c_string("(");
3844 for (Typed_identifier_list::const_iterator p = results->begin();
3845 p != results->end();
3846 ++p)
3848 if (first)
3849 first = false;
3850 else
3851 exp->write_c_string(", ");
3852 exp->write_name(p->name());
3853 exp->write_c_string(" ");
3854 exp->write_type(p->type());
3856 exp->write_c_string(")");
3861 // Import a function type.
3863 Function_type*
3864 Function_type::do_import(Import* imp)
3866 imp->require_c_string("(");
3867 Typed_identifier_list* parameters;
3868 bool is_varargs = false;
3869 if (imp->peek_char() == ')')
3870 parameters = NULL;
3871 else
3873 parameters = new Typed_identifier_list();
3874 while (true)
3876 std::string name = imp->read_name();
3877 imp->require_c_string(" ");
3879 if (imp->match_c_string("..."))
3881 imp->advance(3);
3882 is_varargs = true;
3885 Type* ptype = imp->read_type();
3886 if (is_varargs)
3887 ptype = Type::make_array_type(ptype, NULL);
3888 parameters->push_back(Typed_identifier(name, ptype,
3889 imp->location()));
3890 if (imp->peek_char() != ',')
3891 break;
3892 go_assert(!is_varargs);
3893 imp->require_c_string(", ");
3896 imp->require_c_string(")");
3898 Typed_identifier_list* results;
3899 if (imp->peek_char() != ' ')
3900 results = NULL;
3901 else
3903 imp->advance(1);
3904 results = new Typed_identifier_list;
3905 if (imp->peek_char() != '(')
3907 Type* rtype = imp->read_type();
3908 results->push_back(Typed_identifier("", rtype, imp->location()));
3910 else
3912 imp->advance(1);
3913 while (true)
3915 std::string name = imp->read_name();
3916 imp->require_c_string(" ");
3917 Type* rtype = imp->read_type();
3918 results->push_back(Typed_identifier(name, rtype,
3919 imp->location()));
3920 if (imp->peek_char() != ',')
3921 break;
3922 imp->require_c_string(", ");
3924 imp->require_c_string(")");
3928 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3929 imp->location());
3930 if (is_varargs)
3931 ret->set_is_varargs();
3932 return ret;
3935 // Make a copy of a function type without a receiver.
3937 Function_type*
3938 Function_type::copy_without_receiver() const
3940 go_assert(this->is_method());
3941 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3942 this->results_,
3943 this->location_);
3944 if (this->is_varargs())
3945 ret->set_is_varargs();
3946 if (this->is_builtin())
3947 ret->set_is_builtin();
3948 return ret;
3951 // Make a copy of a function type with a receiver.
3953 Function_type*
3954 Function_type::copy_with_receiver(Type* receiver_type) const
3956 go_assert(!this->is_method());
3957 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3958 this->location_);
3959 Function_type* ret = Type::make_function_type(receiver, this->parameters_,
3960 this->results_,
3961 this->location_);
3962 if (this->is_varargs_)
3963 ret->set_is_varargs();
3964 return ret;
3967 // Make a copy of a function type with the receiver as the first
3968 // parameter.
3970 Function_type*
3971 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver) const
3973 go_assert(this->is_method());
3974 Typed_identifier_list* new_params = new Typed_identifier_list();
3975 Type* rtype = this->receiver_->type();
3976 if (want_pointer_receiver)
3977 rtype = Type::make_pointer_type(rtype);
3978 Typed_identifier receiver(this->receiver_->name(), rtype,
3979 this->receiver_->location());
3980 new_params->push_back(receiver);
3981 const Typed_identifier_list* orig_params = this->parameters_;
3982 if (orig_params != NULL && !orig_params->empty())
3984 for (Typed_identifier_list::const_iterator p = orig_params->begin();
3985 p != orig_params->end();
3986 ++p)
3987 new_params->push_back(*p);
3989 return Type::make_function_type(NULL, new_params, this->results_,
3990 this->location_);
3993 // Make a copy of a function type ignoring any receiver and adding a
3994 // closure parameter.
3996 Function_type*
3997 Function_type::copy_with_names() const
3999 Typed_identifier_list* new_params = new Typed_identifier_list();
4000 const Typed_identifier_list* orig_params = this->parameters_;
4001 if (orig_params != NULL && !orig_params->empty())
4003 static int count;
4004 char buf[50];
4005 for (Typed_identifier_list::const_iterator p = orig_params->begin();
4006 p != orig_params->end();
4007 ++p)
4009 snprintf(buf, sizeof buf, "pt.%u", count);
4010 ++count;
4011 new_params->push_back(Typed_identifier(buf, p->type(),
4012 p->location()));
4016 const Typed_identifier_list* orig_results = this->results_;
4017 Typed_identifier_list* new_results;
4018 if (orig_results == NULL || orig_results->empty())
4019 new_results = NULL;
4020 else
4022 new_results = new Typed_identifier_list();
4023 for (Typed_identifier_list::const_iterator p = orig_results->begin();
4024 p != orig_results->end();
4025 ++p)
4026 new_results->push_back(Typed_identifier("", p->type(),
4027 p->location()));
4030 return Type::make_function_type(NULL, new_params, new_results,
4031 this->location());
4034 // Make a function type.
4036 Function_type*
4037 Type::make_function_type(Typed_identifier* receiver,
4038 Typed_identifier_list* parameters,
4039 Typed_identifier_list* results,
4040 Location location)
4042 return new Function_type(receiver, parameters, results, location);
4045 // Make a backend function type.
4047 Backend_function_type*
4048 Type::make_backend_function_type(Typed_identifier* receiver,
4049 Typed_identifier_list* parameters,
4050 Typed_identifier_list* results,
4051 Location location)
4053 return new Backend_function_type(receiver, parameters, results, location);
4056 // Class Pointer_type.
4058 // Traversal.
4061 Pointer_type::do_traverse(Traverse* traverse)
4063 return Type::traverse(this->to_type_, traverse);
4066 // Hash code.
4068 unsigned int
4069 Pointer_type::do_hash_for_method(Gogo* gogo) const
4071 return this->to_type_->hash_for_method(gogo) << 4;
4074 // Get the backend representation for a pointer type.
4076 Btype*
4077 Pointer_type::do_get_backend(Gogo* gogo)
4079 Btype* to_btype = this->to_type_->get_backend(gogo);
4080 return gogo->backend()->pointer_type(to_btype);
4083 // The type of a pointer type descriptor.
4085 Type*
4086 Pointer_type::make_pointer_type_descriptor_type()
4088 static Type* ret;
4089 if (ret == NULL)
4091 Type* tdt = Type::make_type_descriptor_type();
4092 Type* ptdt = Type::make_type_descriptor_ptr_type();
4094 Struct_type* s = Type::make_builtin_struct_type(2,
4095 "", tdt,
4096 "elem", ptdt);
4098 ret = Type::make_builtin_named_type("PtrType", s);
4101 return ret;
4104 // The type descriptor for a pointer type.
4106 Expression*
4107 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4109 if (this->is_unsafe_pointer_type())
4111 go_assert(name != NULL);
4112 return this->plain_type_descriptor(gogo,
4113 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
4114 name);
4116 else
4118 Location bloc = Linemap::predeclared_location();
4120 const Methods* methods;
4121 Type* deref = this->points_to();
4122 if (deref->named_type() != NULL)
4123 methods = deref->named_type()->methods();
4124 else if (deref->struct_type() != NULL)
4125 methods = deref->struct_type()->methods();
4126 else
4127 methods = NULL;
4129 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
4131 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
4133 Expression_list* vals = new Expression_list();
4134 vals->reserve(2);
4136 Struct_field_list::const_iterator p = fields->begin();
4137 go_assert(p->is_field_name("commonType"));
4138 vals->push_back(this->type_descriptor_constructor(gogo,
4139 RUNTIME_TYPE_KIND_PTR,
4140 name, methods, false));
4142 ++p;
4143 go_assert(p->is_field_name("elem"));
4144 vals->push_back(Expression::make_type_descriptor(deref, bloc));
4146 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
4150 // Reflection string.
4152 void
4153 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
4155 ret->push_back('*');
4156 this->append_reflection(this->to_type_, gogo, ret);
4159 // Mangled name.
4161 void
4162 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4164 ret->push_back('p');
4165 this->append_mangled_name(this->to_type_, gogo, ret);
4168 // Export.
4170 void
4171 Pointer_type::do_export(Export* exp) const
4173 exp->write_c_string("*");
4174 if (this->is_unsafe_pointer_type())
4175 exp->write_c_string("any");
4176 else
4177 exp->write_type(this->to_type_);
4180 // Import.
4182 Pointer_type*
4183 Pointer_type::do_import(Import* imp)
4185 imp->require_c_string("*");
4186 if (imp->match_c_string("any"))
4188 imp->advance(3);
4189 return Type::make_pointer_type(Type::make_void_type());
4191 Type* to = imp->read_type();
4192 return Type::make_pointer_type(to);
4195 // Make a pointer type.
4197 Pointer_type*
4198 Type::make_pointer_type(Type* to_type)
4200 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
4201 static Hashtable pointer_types;
4202 Hashtable::const_iterator p = pointer_types.find(to_type);
4203 if (p != pointer_types.end())
4204 return p->second;
4205 Pointer_type* ret = new Pointer_type(to_type);
4206 pointer_types[to_type] = ret;
4207 return ret;
4210 // The nil type. We use a special type for nil because it is not the
4211 // same as any other type. In C term nil has type void*, but there is
4212 // no such type in Go.
4214 class Nil_type : public Type
4216 public:
4217 Nil_type()
4218 : Type(TYPE_NIL)
4221 protected:
4222 bool
4223 do_compare_is_identity(Gogo*)
4224 { return false; }
4226 Btype*
4227 do_get_backend(Gogo* gogo)
4228 { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
4230 Expression*
4231 do_type_descriptor(Gogo*, Named_type*)
4232 { go_unreachable(); }
4234 void
4235 do_reflection(Gogo*, std::string*) const
4236 { go_unreachable(); }
4238 void
4239 do_mangled_name(Gogo*, std::string* ret) const
4240 { ret->push_back('n'); }
4243 // Make the nil type.
4245 Type*
4246 Type::make_nil_type()
4248 static Nil_type singleton_nil_type;
4249 return &singleton_nil_type;
4252 // The type of a function call which returns multiple values. This is
4253 // really a struct, but we don't want to confuse a function call which
4254 // returns a struct with a function call which returns multiple
4255 // values.
4257 class Call_multiple_result_type : public Type
4259 public:
4260 Call_multiple_result_type(Call_expression* call)
4261 : Type(TYPE_CALL_MULTIPLE_RESULT),
4262 call_(call)
4265 protected:
4266 bool
4267 do_has_pointer() const
4269 go_assert(saw_errors());
4270 return false;
4273 bool
4274 do_compare_is_identity(Gogo*)
4275 { return false; }
4277 Btype*
4278 do_get_backend(Gogo* gogo)
4280 go_assert(saw_errors());
4281 return gogo->backend()->error_type();
4284 Expression*
4285 do_type_descriptor(Gogo*, Named_type*)
4287 go_assert(saw_errors());
4288 return Expression::make_error(Linemap::unknown_location());
4291 void
4292 do_reflection(Gogo*, std::string*) const
4293 { go_assert(saw_errors()); }
4295 void
4296 do_mangled_name(Gogo*, std::string*) const
4297 { go_assert(saw_errors()); }
4299 private:
4300 // The expression being called.
4301 Call_expression* call_;
4304 // Make a call result type.
4306 Type*
4307 Type::make_call_multiple_result_type(Call_expression* call)
4309 return new Call_multiple_result_type(call);
4312 // Class Struct_field.
4314 // Get the name of a field.
4316 const std::string&
4317 Struct_field::field_name() const
4319 const std::string& name(this->typed_identifier_.name());
4320 if (!name.empty())
4321 return name;
4322 else
4324 // This is called during parsing, before anything is lowered, so
4325 // we have to be pretty careful to avoid dereferencing an
4326 // unknown type name.
4327 Type* t = this->typed_identifier_.type();
4328 Type* dt = t;
4329 if (t->classification() == Type::TYPE_POINTER)
4331 // Very ugly.
4332 Pointer_type* ptype = static_cast<Pointer_type*>(t);
4333 dt = ptype->points_to();
4335 if (dt->forward_declaration_type() != NULL)
4336 return dt->forward_declaration_type()->name();
4337 else if (dt->named_type() != NULL)
4338 return dt->named_type()->name();
4339 else if (t->is_error_type() || dt->is_error_type())
4341 static const std::string error_string = "*error*";
4342 return error_string;
4344 else
4346 // Avoid crashing in the erroneous case where T is named but
4347 // DT is not.
4348 go_assert(t != dt);
4349 if (t->forward_declaration_type() != NULL)
4350 return t->forward_declaration_type()->name();
4351 else if (t->named_type() != NULL)
4352 return t->named_type()->name();
4353 else
4354 go_unreachable();
4359 // Return whether this field is named NAME.
4361 bool
4362 Struct_field::is_field_name(const std::string& name) const
4364 const std::string& me(this->typed_identifier_.name());
4365 if (!me.empty())
4366 return me == name;
4367 else
4369 Type* t = this->typed_identifier_.type();
4370 if (t->points_to() != NULL)
4371 t = t->points_to();
4372 Named_type* nt = t->named_type();
4373 if (nt != NULL && nt->name() == name)
4374 return true;
4376 // This is a horrible hack caused by the fact that we don't pack
4377 // the names of builtin types. FIXME.
4378 if (!this->is_imported_
4379 && nt != NULL
4380 && nt->is_builtin()
4381 && nt->name() == Gogo::unpack_hidden_name(name))
4382 return true;
4384 return false;
4388 // Return whether this field is an unexported field named NAME.
4390 bool
4391 Struct_field::is_unexported_field_name(Gogo* gogo,
4392 const std::string& name) const
4394 const std::string& field_name(this->field_name());
4395 if (Gogo::is_hidden_name(field_name)
4396 && name == Gogo::unpack_hidden_name(field_name)
4397 && gogo->pack_hidden_name(name, false) != field_name)
4398 return true;
4400 // Check for the name of a builtin type. This is like the test in
4401 // is_field_name, only there we return false if this->is_imported_,
4402 // and here we return true.
4403 if (this->is_imported_ && this->is_anonymous())
4405 Type* t = this->typed_identifier_.type();
4406 if (t->points_to() != NULL)
4407 t = t->points_to();
4408 Named_type* nt = t->named_type();
4409 if (nt != NULL
4410 && nt->is_builtin()
4411 && nt->name() == Gogo::unpack_hidden_name(name))
4412 return true;
4415 return false;
4418 // Return whether this field is an embedded built-in type.
4420 bool
4421 Struct_field::is_embedded_builtin(Gogo* gogo) const
4423 const std::string& name(this->field_name());
4424 // We know that a field is an embedded type if it is anonymous.
4425 // We can decide if it is a built-in type by checking to see if it is
4426 // registered globally under the field's name.
4427 // This allows us to distinguish between embedded built-in types and
4428 // embedded types that are aliases to built-in types.
4429 return (this->is_anonymous()
4430 && !Gogo::is_hidden_name(name)
4431 && gogo->lookup_global(name.c_str()) != NULL);
4434 // Class Struct_type.
4436 // A hash table used to find identical unnamed structs so that they
4437 // share method tables.
4439 Struct_type::Identical_structs Struct_type::identical_structs;
4441 // A hash table used to merge method sets for identical unnamed
4442 // structs.
4444 Struct_type::Struct_method_tables Struct_type::struct_method_tables;
4446 // Traversal.
4449 Struct_type::do_traverse(Traverse* traverse)
4451 Struct_field_list* fields = this->fields_;
4452 if (fields != NULL)
4454 for (Struct_field_list::iterator p = fields->begin();
4455 p != fields->end();
4456 ++p)
4458 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
4459 return TRAVERSE_EXIT;
4462 return TRAVERSE_CONTINUE;
4465 // Verify that the struct type is complete and valid.
4467 bool
4468 Struct_type::do_verify()
4470 Struct_field_list* fields = this->fields_;
4471 if (fields == NULL)
4472 return true;
4473 for (Struct_field_list::iterator p = fields->begin();
4474 p != fields->end();
4475 ++p)
4477 Type* t = p->type();
4478 if (p->is_anonymous())
4480 if (t->named_type() != NULL && t->points_to() != NULL)
4482 error_at(p->location(), "embedded type may not be a pointer");
4483 p->set_type(Type::make_error_type());
4485 else if (t->points_to() != NULL
4486 && t->points_to()->interface_type() != NULL)
4488 error_at(p->location(),
4489 "embedded type may not be pointer to interface");
4490 p->set_type(Type::make_error_type());
4494 return true;
4497 // Whether this contains a pointer.
4499 bool
4500 Struct_type::do_has_pointer() const
4502 const Struct_field_list* fields = this->fields();
4503 if (fields == NULL)
4504 return false;
4505 for (Struct_field_list::const_iterator p = fields->begin();
4506 p != fields->end();
4507 ++p)
4509 if (p->type()->has_pointer())
4510 return true;
4512 return false;
4515 // Whether this type is identical to T.
4517 bool
4518 Struct_type::is_identical(const Struct_type* t,
4519 bool errors_are_identical) const
4521 const Struct_field_list* fields1 = this->fields();
4522 const Struct_field_list* fields2 = t->fields();
4523 if (fields1 == NULL || fields2 == NULL)
4524 return fields1 == fields2;
4525 Struct_field_list::const_iterator pf2 = fields2->begin();
4526 for (Struct_field_list::const_iterator pf1 = fields1->begin();
4527 pf1 != fields1->end();
4528 ++pf1, ++pf2)
4530 if (pf2 == fields2->end())
4531 return false;
4532 if (pf1->field_name() != pf2->field_name())
4533 return false;
4534 if (pf1->is_anonymous() != pf2->is_anonymous()
4535 || !Type::are_identical(pf1->type(), pf2->type(),
4536 errors_are_identical, NULL))
4537 return false;
4538 if (!pf1->has_tag())
4540 if (pf2->has_tag())
4541 return false;
4543 else
4545 if (!pf2->has_tag())
4546 return false;
4547 if (pf1->tag() != pf2->tag())
4548 return false;
4551 if (pf2 != fields2->end())
4552 return false;
4553 return true;
4556 // Whether this struct type has any hidden fields.
4558 bool
4559 Struct_type::struct_has_hidden_fields(const Named_type* within,
4560 std::string* reason) const
4562 const Struct_field_list* fields = this->fields();
4563 if (fields == NULL)
4564 return false;
4565 const Package* within_package = (within == NULL
4566 ? NULL
4567 : within->named_object()->package());
4568 for (Struct_field_list::const_iterator pf = fields->begin();
4569 pf != fields->end();
4570 ++pf)
4572 if (within_package != NULL
4573 && !pf->is_anonymous()
4574 && Gogo::is_hidden_name(pf->field_name()))
4576 if (reason != NULL)
4578 std::string within_name = within->named_object()->message_name();
4579 std::string name = Gogo::message_name(pf->field_name());
4580 size_t bufsize = 200 + within_name.length() + name.length();
4581 char* buf = new char[bufsize];
4582 snprintf(buf, bufsize,
4583 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4584 open_quote, within_name.c_str(), close_quote,
4585 open_quote, name.c_str(), close_quote);
4586 reason->assign(buf);
4587 delete[] buf;
4589 return true;
4592 if (pf->type()->has_hidden_fields(within, reason))
4593 return true;
4596 return false;
4599 // Whether comparisons of this struct type are simple identity
4600 // comparisons.
4602 bool
4603 Struct_type::do_compare_is_identity(Gogo* gogo)
4605 const Struct_field_list* fields = this->fields_;
4606 if (fields == NULL)
4607 return true;
4608 unsigned long offset = 0;
4609 for (Struct_field_list::const_iterator pf = fields->begin();
4610 pf != fields->end();
4611 ++pf)
4613 if (Gogo::is_sink_name(pf->field_name()))
4614 return false;
4616 if (!pf->type()->compare_is_identity(gogo))
4617 return false;
4619 unsigned long field_align;
4620 if (!pf->type()->backend_type_align(gogo, &field_align))
4621 return false;
4622 if ((offset & (field_align - 1)) != 0)
4624 // This struct has padding. We don't guarantee that that
4625 // padding is zero-initialized for a stack variable, so we
4626 // can't use memcmp to compare struct values.
4627 return false;
4630 unsigned long field_size;
4631 if (!pf->type()->backend_type_size(gogo, &field_size))
4632 return false;
4633 offset += field_size;
4636 unsigned long struct_size;
4637 if (!this->backend_type_size(gogo, &struct_size))
4638 return false;
4639 if (offset != struct_size)
4641 // Trailing padding may not be zero when on the stack.
4642 return false;
4645 return true;
4648 // Build identity and hash functions for this struct.
4650 // Hash code.
4652 unsigned int
4653 Struct_type::do_hash_for_method(Gogo* gogo) const
4655 unsigned int ret = 0;
4656 if (this->fields() != NULL)
4658 for (Struct_field_list::const_iterator pf = this->fields()->begin();
4659 pf != this->fields()->end();
4660 ++pf)
4661 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4663 return ret <<= 2;
4666 // Find the local field NAME.
4668 const Struct_field*
4669 Struct_type::find_local_field(const std::string& name,
4670 unsigned int *pindex) const
4672 const Struct_field_list* fields = this->fields_;
4673 if (fields == NULL)
4674 return NULL;
4675 unsigned int i = 0;
4676 for (Struct_field_list::const_iterator pf = fields->begin();
4677 pf != fields->end();
4678 ++pf, ++i)
4680 if (pf->is_field_name(name))
4682 if (pindex != NULL)
4683 *pindex = i;
4684 return &*pf;
4687 return NULL;
4690 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4692 Field_reference_expression*
4693 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4694 Location location) const
4696 unsigned int depth;
4697 return this->field_reference_depth(struct_expr, name, location, NULL,
4698 &depth);
4701 // Return an expression for a field, along with the depth at which it
4702 // was found.
4704 Field_reference_expression*
4705 Struct_type::field_reference_depth(Expression* struct_expr,
4706 const std::string& name,
4707 Location location,
4708 Saw_named_type* saw,
4709 unsigned int* depth) const
4711 const Struct_field_list* fields = this->fields_;
4712 if (fields == NULL)
4713 return NULL;
4715 // Look for a field with this name.
4716 unsigned int i = 0;
4717 for (Struct_field_list::const_iterator pf = fields->begin();
4718 pf != fields->end();
4719 ++pf, ++i)
4721 if (pf->is_field_name(name))
4723 *depth = 0;
4724 return Expression::make_field_reference(struct_expr, i, location);
4728 // Look for an anonymous field which contains a field with this
4729 // name.
4730 unsigned int found_depth = 0;
4731 Field_reference_expression* ret = NULL;
4732 i = 0;
4733 for (Struct_field_list::const_iterator pf = fields->begin();
4734 pf != fields->end();
4735 ++pf, ++i)
4737 if (!pf->is_anonymous())
4738 continue;
4740 Struct_type* st = pf->type()->deref()->struct_type();
4741 if (st == NULL)
4742 continue;
4744 Saw_named_type* hold_saw = saw;
4745 Saw_named_type saw_here;
4746 Named_type* nt = pf->type()->named_type();
4747 if (nt == NULL)
4748 nt = pf->type()->deref()->named_type();
4749 if (nt != NULL)
4751 Saw_named_type* q;
4752 for (q = saw; q != NULL; q = q->next)
4754 if (q->nt == nt)
4756 // If this is an error, it will be reported
4757 // elsewhere.
4758 break;
4761 if (q != NULL)
4762 continue;
4763 saw_here.next = saw;
4764 saw_here.nt = nt;
4765 saw = &saw_here;
4768 // Look for a reference using a NULL struct expression. If we
4769 // find one, fill in the struct expression with a reference to
4770 // this field.
4771 unsigned int subdepth;
4772 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
4773 location,
4774 saw,
4775 &subdepth);
4777 saw = hold_saw;
4779 if (sub == NULL)
4780 continue;
4782 if (ret == NULL || subdepth < found_depth)
4784 if (ret != NULL)
4785 delete ret;
4786 ret = sub;
4787 found_depth = subdepth;
4788 Expression* here = Expression::make_field_reference(struct_expr, i,
4789 location);
4790 if (pf->type()->points_to() != NULL)
4791 here = Expression::make_unary(OPERATOR_MULT, here, location);
4792 while (sub->expr() != NULL)
4794 sub = sub->expr()->deref()->field_reference_expression();
4795 go_assert(sub != NULL);
4797 sub->set_struct_expression(here);
4798 sub->set_implicit(true);
4800 else if (subdepth > found_depth)
4801 delete sub;
4802 else
4804 // We do not handle ambiguity here--it should be handled by
4805 // Type::bind_field_or_method.
4806 delete sub;
4807 found_depth = 0;
4808 ret = NULL;
4812 if (ret != NULL)
4813 *depth = found_depth + 1;
4815 return ret;
4818 // Return the total number of fields, including embedded fields.
4820 unsigned int
4821 Struct_type::total_field_count() const
4823 if (this->fields_ == NULL)
4824 return 0;
4825 unsigned int ret = 0;
4826 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4827 pf != this->fields_->end();
4828 ++pf)
4830 if (!pf->is_anonymous() || pf->type()->struct_type() == NULL)
4831 ++ret;
4832 else
4833 ret += pf->type()->struct_type()->total_field_count();
4835 return ret;
4838 // Return whether NAME is an unexported field, for better error reporting.
4840 bool
4841 Struct_type::is_unexported_local_field(Gogo* gogo,
4842 const std::string& name) const
4844 const Struct_field_list* fields = this->fields_;
4845 if (fields != NULL)
4847 for (Struct_field_list::const_iterator pf = fields->begin();
4848 pf != fields->end();
4849 ++pf)
4850 if (pf->is_unexported_field_name(gogo, name))
4851 return true;
4853 return false;
4856 // Finalize the methods of an unnamed struct.
4858 void
4859 Struct_type::finalize_methods(Gogo* gogo)
4861 if (this->all_methods_ != NULL)
4862 return;
4864 // It is possible to have multiple identical structs that have
4865 // methods. We want them to share method tables. Otherwise we will
4866 // emit identical methods more than once, which is bad since they
4867 // will even have the same names.
4868 std::pair<Identical_structs::iterator, bool> ins =
4869 Struct_type::identical_structs.insert(std::make_pair(this, this));
4870 if (!ins.second)
4872 // An identical struct was already entered into the hash table.
4873 // Note that finalize_methods is, fortunately, not recursive.
4874 this->all_methods_ = ins.first->second->all_methods_;
4875 return;
4878 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
4881 // Return the method NAME, or NULL if there isn't one or if it is
4882 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
4883 // ambiguous.
4885 Method*
4886 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
4888 return Type::method_function(this->all_methods_, name, is_ambiguous);
4891 // Return a pointer to the interface method table for this type for
4892 // the interface INTERFACE. IS_POINTER is true if this is for a
4893 // pointer to THIS.
4895 Expression*
4896 Struct_type::interface_method_table(Interface_type* interface,
4897 bool is_pointer)
4899 std::pair<Struct_type*, Struct_type::Struct_method_table_pair*>
4900 val(this, NULL);
4901 std::pair<Struct_type::Struct_method_tables::iterator, bool> ins =
4902 Struct_type::struct_method_tables.insert(val);
4904 Struct_method_table_pair* smtp;
4905 if (!ins.second)
4906 smtp = ins.first->second;
4907 else
4909 smtp = new Struct_method_table_pair();
4910 smtp->first = NULL;
4911 smtp->second = NULL;
4912 ins.first->second = smtp;
4915 return Type::interface_method_table(this, interface, is_pointer,
4916 &smtp->first, &smtp->second);
4919 // Convert struct fields to the backend representation. This is not
4920 // declared in types.h so that types.h doesn't have to #include
4921 // backend.h.
4923 static void
4924 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
4925 bool use_placeholder,
4926 std::vector<Backend::Btyped_identifier>* bfields)
4928 bfields->resize(fields->size());
4929 size_t i = 0;
4930 for (Struct_field_list::const_iterator p = fields->begin();
4931 p != fields->end();
4932 ++p, ++i)
4934 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
4935 (*bfields)[i].btype = (use_placeholder
4936 ? p->type()->get_backend_placeholder(gogo)
4937 : p->type()->get_backend(gogo));
4938 (*bfields)[i].location = p->location();
4940 go_assert(i == fields->size());
4943 // Get the backend representation for a struct type.
4945 Btype*
4946 Struct_type::do_get_backend(Gogo* gogo)
4948 std::vector<Backend::Btyped_identifier> bfields;
4949 get_backend_struct_fields(gogo, this->fields_, false, &bfields);
4950 return gogo->backend()->struct_type(bfields);
4953 // Finish the backend representation of the fields of a struct.
4955 void
4956 Struct_type::finish_backend_fields(Gogo* gogo)
4958 const Struct_field_list* fields = this->fields_;
4959 if (fields != NULL)
4961 for (Struct_field_list::const_iterator p = fields->begin();
4962 p != fields->end();
4963 ++p)
4964 p->type()->get_backend(gogo);
4968 // The type of a struct type descriptor.
4970 Type*
4971 Struct_type::make_struct_type_descriptor_type()
4973 static Type* ret;
4974 if (ret == NULL)
4976 Type* tdt = Type::make_type_descriptor_type();
4977 Type* ptdt = Type::make_type_descriptor_ptr_type();
4979 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4980 Type* string_type = Type::lookup_string_type();
4981 Type* pointer_string_type = Type::make_pointer_type(string_type);
4983 Struct_type* sf =
4984 Type::make_builtin_struct_type(5,
4985 "name", pointer_string_type,
4986 "pkgPath", pointer_string_type,
4987 "typ", ptdt,
4988 "tag", pointer_string_type,
4989 "offset", uintptr_type);
4990 Type* nsf = Type::make_builtin_named_type("structField", sf);
4992 Type* slice_type = Type::make_array_type(nsf, NULL);
4994 Struct_type* s = Type::make_builtin_struct_type(2,
4995 "", tdt,
4996 "fields", slice_type);
4998 ret = Type::make_builtin_named_type("StructType", s);
5001 return ret;
5004 // Build a type descriptor for a struct type.
5006 Expression*
5007 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5009 Location bloc = Linemap::predeclared_location();
5011 Type* stdt = Struct_type::make_struct_type_descriptor_type();
5013 const Struct_field_list* fields = stdt->struct_type()->fields();
5015 Expression_list* vals = new Expression_list();
5016 vals->reserve(2);
5018 const Methods* methods = this->methods();
5019 // A named struct should not have methods--the methods should attach
5020 // to the named type.
5021 go_assert(methods == NULL || name == NULL);
5023 Struct_field_list::const_iterator ps = fields->begin();
5024 go_assert(ps->is_field_name("commonType"));
5025 vals->push_back(this->type_descriptor_constructor(gogo,
5026 RUNTIME_TYPE_KIND_STRUCT,
5027 name, methods, true));
5029 ++ps;
5030 go_assert(ps->is_field_name("fields"));
5032 Expression_list* elements = new Expression_list();
5033 elements->reserve(this->fields_->size());
5034 Type* element_type = ps->type()->array_type()->element_type();
5035 for (Struct_field_list::const_iterator pf = this->fields_->begin();
5036 pf != this->fields_->end();
5037 ++pf)
5039 const Struct_field_list* f = element_type->struct_type()->fields();
5041 Expression_list* fvals = new Expression_list();
5042 fvals->reserve(5);
5044 Struct_field_list::const_iterator q = f->begin();
5045 go_assert(q->is_field_name("name"));
5046 if (pf->is_anonymous())
5047 fvals->push_back(Expression::make_nil(bloc));
5048 else
5050 std::string n = Gogo::unpack_hidden_name(pf->field_name());
5051 Expression* s = Expression::make_string(n, bloc);
5052 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
5055 ++q;
5056 go_assert(q->is_field_name("pkgPath"));
5057 bool is_embedded_builtin = pf->is_embedded_builtin(gogo);
5058 if (!Gogo::is_hidden_name(pf->field_name()) && !is_embedded_builtin)
5059 fvals->push_back(Expression::make_nil(bloc));
5060 else
5062 std::string n;
5063 if (is_embedded_builtin)
5064 n = gogo->package_name();
5065 else
5066 n = Gogo::hidden_name_pkgpath(pf->field_name());
5067 Expression* s = Expression::make_string(n, bloc);
5068 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
5071 ++q;
5072 go_assert(q->is_field_name("typ"));
5073 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
5075 ++q;
5076 go_assert(q->is_field_name("tag"));
5077 if (!pf->has_tag())
5078 fvals->push_back(Expression::make_nil(bloc));
5079 else
5081 Expression* s = Expression::make_string(pf->tag(), bloc);
5082 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
5085 ++q;
5086 go_assert(q->is_field_name("offset"));
5087 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
5089 Expression* v = Expression::make_struct_composite_literal(element_type,
5090 fvals, bloc);
5091 elements->push_back(v);
5094 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
5095 elements, bloc));
5097 return Expression::make_struct_composite_literal(stdt, vals, bloc);
5100 // Write the hash function for a struct which can not use the identity
5101 // function.
5103 void
5104 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
5105 Function_type* hash_fntype,
5106 Function_type* equal_fntype)
5108 Location bloc = Linemap::predeclared_location();
5110 // The pointer to the struct that we are going to hash. This is an
5111 // argument to the hash function we are implementing here.
5112 Named_object* key_arg = gogo->lookup("key", NULL);
5113 go_assert(key_arg != NULL);
5114 Type* key_arg_type = key_arg->var_value()->type();
5116 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5118 // Get a 0.
5119 mpz_t ival;
5120 mpz_init_set_ui(ival, 0);
5121 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
5122 mpz_clear(ival);
5124 // Make a temporary to hold the return value, initialized to 0.
5125 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
5126 bloc);
5127 gogo->add_statement(retval);
5129 // Make a temporary to hold the key as a uintptr.
5130 Expression* ref = Expression::make_var_reference(key_arg, bloc);
5131 ref = Expression::make_cast(uintptr_type, ref, bloc);
5132 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
5133 bloc);
5134 gogo->add_statement(key);
5136 // Loop over the struct fields.
5137 bool first = true;
5138 const Struct_field_list* fields = this->fields_;
5139 for (Struct_field_list::const_iterator pf = fields->begin();
5140 pf != fields->end();
5141 ++pf)
5143 if (Gogo::is_sink_name(pf->field_name()))
5144 continue;
5146 if (first)
5147 first = false;
5148 else
5150 // Multiply retval by 33.
5151 mpz_init_set_ui(ival, 33);
5152 Expression* i33 = Expression::make_integer(&ival, uintptr_type,
5153 bloc);
5154 mpz_clear(ival);
5156 ref = Expression::make_temporary_reference(retval, bloc);
5157 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ,
5158 ref, i33, bloc);
5159 gogo->add_statement(s);
5162 // Get a pointer to the value of this field.
5163 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
5164 ref = Expression::make_temporary_reference(key, bloc);
5165 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
5166 bloc);
5167 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
5169 // Get the size of this field.
5170 Expression* size = Expression::make_type_info(pf->type(),
5171 Expression::TYPE_INFO_SIZE);
5173 // Get the hash function to use for the type of this field.
5174 Named_object* hash_fn;
5175 Named_object* equal_fn;
5176 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
5177 equal_fntype, &hash_fn, &equal_fn);
5179 // Call the hash function for the field.
5180 Expression_list* args = new Expression_list();
5181 args->push_back(subkey);
5182 args->push_back(size);
5183 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
5184 Expression* call = Expression::make_call(func, args, false, bloc);
5186 // Add the field's hash value to retval.
5187 Temporary_reference_expression* tref =
5188 Expression::make_temporary_reference(retval, bloc);
5189 tref->set_is_lvalue();
5190 Statement* s = Statement::make_assignment_operation(OPERATOR_PLUSEQ,
5191 tref, call, bloc);
5192 gogo->add_statement(s);
5195 // Return retval to the caller of the hash function.
5196 Expression_list* vals = new Expression_list();
5197 ref = Expression::make_temporary_reference(retval, bloc);
5198 vals->push_back(ref);
5199 Statement* s = Statement::make_return_statement(vals, bloc);
5200 gogo->add_statement(s);
5203 // Write the equality function for a struct which can not use the
5204 // identity function.
5206 void
5207 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
5209 Location bloc = Linemap::predeclared_location();
5211 // The pointers to the structs we are going to compare.
5212 Named_object* key1_arg = gogo->lookup("key1", NULL);
5213 Named_object* key2_arg = gogo->lookup("key2", NULL);
5214 go_assert(key1_arg != NULL && key2_arg != NULL);
5216 // Build temporaries with the right types.
5217 Type* pt = Type::make_pointer_type(name != NULL
5218 ? static_cast<Type*>(name)
5219 : static_cast<Type*>(this));
5221 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
5222 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5223 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
5224 gogo->add_statement(p1);
5226 ref = Expression::make_var_reference(key2_arg, bloc);
5227 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5228 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
5229 gogo->add_statement(p2);
5231 const Struct_field_list* fields = this->fields_;
5232 unsigned int field_index = 0;
5233 for (Struct_field_list::const_iterator pf = fields->begin();
5234 pf != fields->end();
5235 ++pf, ++field_index)
5237 if (Gogo::is_sink_name(pf->field_name()))
5238 continue;
5240 // Compare one field in both P1 and P2.
5241 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
5242 f1 = Expression::make_unary(OPERATOR_MULT, f1, bloc);
5243 f1 = Expression::make_field_reference(f1, field_index, bloc);
5245 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
5246 f2 = Expression::make_unary(OPERATOR_MULT, f2, bloc);
5247 f2 = Expression::make_field_reference(f2, field_index, bloc);
5249 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
5251 // If the values are not equal, return false.
5252 gogo->start_block(bloc);
5253 Expression_list* vals = new Expression_list();
5254 vals->push_back(Expression::make_boolean(false, bloc));
5255 Statement* s = Statement::make_return_statement(vals, bloc);
5256 gogo->add_statement(s);
5257 Block* then_block = gogo->finish_block(bloc);
5259 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
5260 gogo->add_statement(s);
5263 // All the fields are equal, so return true.
5264 Expression_list* vals = new Expression_list();
5265 vals->push_back(Expression::make_boolean(true, bloc));
5266 Statement* s = Statement::make_return_statement(vals, bloc);
5267 gogo->add_statement(s);
5270 // Reflection string.
5272 void
5273 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
5275 ret->append("struct {");
5277 for (Struct_field_list::const_iterator p = this->fields_->begin();
5278 p != this->fields_->end();
5279 ++p)
5281 if (p != this->fields_->begin())
5282 ret->push_back(';');
5283 ret->push_back(' ');
5284 if (p->is_anonymous())
5285 ret->push_back('?');
5286 else
5287 ret->append(Gogo::unpack_hidden_name(p->field_name()));
5288 ret->push_back(' ');
5289 this->append_reflection(p->type(), gogo, ret);
5291 if (p->has_tag())
5293 const std::string& tag(p->tag());
5294 ret->append(" \"");
5295 for (std::string::const_iterator p = tag.begin();
5296 p != tag.end();
5297 ++p)
5299 if (*p == '\0')
5300 ret->append("\\x00");
5301 else if (*p == '\n')
5302 ret->append("\\n");
5303 else if (*p == '\t')
5304 ret->append("\\t");
5305 else if (*p == '"')
5306 ret->append("\\\"");
5307 else if (*p == '\\')
5308 ret->append("\\\\");
5309 else
5310 ret->push_back(*p);
5312 ret->push_back('"');
5316 if (!this->fields_->empty())
5317 ret->push_back(' ');
5319 ret->push_back('}');
5322 // Mangled name.
5324 void
5325 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5327 ret->push_back('S');
5329 const Struct_field_list* fields = this->fields_;
5330 if (fields != NULL)
5332 for (Struct_field_list::const_iterator p = fields->begin();
5333 p != fields->end();
5334 ++p)
5336 if (p->is_anonymous())
5337 ret->append("0_");
5338 else
5340 std::string n = Gogo::unpack_hidden_name(p->field_name());
5341 char buf[20];
5342 snprintf(buf, sizeof buf, "%u_",
5343 static_cast<unsigned int>(n.length()));
5344 ret->append(buf);
5345 ret->append(n);
5347 this->append_mangled_name(p->type(), gogo, ret);
5348 if (p->has_tag())
5350 const std::string& tag(p->tag());
5351 std::string out;
5352 for (std::string::const_iterator p = tag.begin();
5353 p != tag.end();
5354 ++p)
5356 if (ISALNUM(*p) || *p == '_')
5357 out.push_back(*p);
5358 else
5360 char buf[20];
5361 snprintf(buf, sizeof buf, ".%x.",
5362 static_cast<unsigned int>(*p));
5363 out.append(buf);
5366 char buf[20];
5367 snprintf(buf, sizeof buf, "T%u_",
5368 static_cast<unsigned int>(out.length()));
5369 ret->append(buf);
5370 ret->append(out);
5375 ret->push_back('e');
5378 // If the offset of field INDEX in the backend implementation can be
5379 // determined, set *POFFSET to the offset in bytes and return true.
5380 // Otherwise, return false.
5382 bool
5383 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
5384 unsigned int* poffset)
5386 if (!this->is_backend_type_size_known(gogo))
5387 return false;
5388 Btype* bt = this->get_backend_placeholder(gogo);
5389 size_t offset = gogo->backend()->type_field_offset(bt, index);
5390 *poffset = static_cast<unsigned int>(offset);
5391 if (*poffset != offset)
5392 return false;
5393 return true;
5396 // Export.
5398 void
5399 Struct_type::do_export(Export* exp) const
5401 exp->write_c_string("struct { ");
5402 const Struct_field_list* fields = this->fields_;
5403 go_assert(fields != NULL);
5404 for (Struct_field_list::const_iterator p = fields->begin();
5405 p != fields->end();
5406 ++p)
5408 if (p->is_anonymous())
5409 exp->write_string("? ");
5410 else
5412 exp->write_string(p->field_name());
5413 exp->write_c_string(" ");
5415 exp->write_type(p->type());
5417 if (p->has_tag())
5419 exp->write_c_string(" ");
5420 Expression* expr =
5421 Expression::make_string(p->tag(), Linemap::predeclared_location());
5422 expr->export_expression(exp);
5423 delete expr;
5426 exp->write_c_string("; ");
5428 exp->write_c_string("}");
5431 // Import.
5433 Struct_type*
5434 Struct_type::do_import(Import* imp)
5436 imp->require_c_string("struct { ");
5437 Struct_field_list* fields = new Struct_field_list;
5438 if (imp->peek_char() != '}')
5440 while (true)
5442 std::string name;
5443 if (imp->match_c_string("? "))
5444 imp->advance(2);
5445 else
5447 name = imp->read_identifier();
5448 imp->require_c_string(" ");
5450 Type* ftype = imp->read_type();
5452 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
5453 sf.set_is_imported();
5455 if (imp->peek_char() == ' ')
5457 imp->advance(1);
5458 Expression* expr = Expression::import_expression(imp);
5459 String_expression* sexpr = expr->string_expression();
5460 go_assert(sexpr != NULL);
5461 sf.set_tag(sexpr->val());
5462 delete sexpr;
5465 imp->require_c_string("; ");
5466 fields->push_back(sf);
5467 if (imp->peek_char() == '}')
5468 break;
5471 imp->require_c_string("}");
5473 return Type::make_struct_type(fields, imp->location());
5476 // Make a struct type.
5478 Struct_type*
5479 Type::make_struct_type(Struct_field_list* fields,
5480 Location location)
5482 return new Struct_type(fields, location);
5485 // Class Array_type.
5487 // Whether two array types are identical.
5489 bool
5490 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
5492 if (!Type::are_identical(this->element_type(), t->element_type(),
5493 errors_are_identical, NULL))
5494 return false;
5496 Expression* l1 = this->length();
5497 Expression* l2 = t->length();
5499 // Slices of the same element type are identical.
5500 if (l1 == NULL && l2 == NULL)
5501 return true;
5503 // Arrays of the same element type are identical if they have the
5504 // same length.
5505 if (l1 != NULL && l2 != NULL)
5507 if (l1 == l2)
5508 return true;
5510 // Try to determine the lengths. If we can't, assume the arrays
5511 // are not identical.
5512 bool ret = false;
5513 Numeric_constant nc1, nc2;
5514 if (l1->numeric_constant_value(&nc1)
5515 && l2->numeric_constant_value(&nc2))
5517 mpz_t v1;
5518 if (nc1.to_int(&v1))
5520 mpz_t v2;
5521 if (nc2.to_int(&v2))
5523 ret = mpz_cmp(v1, v2) == 0;
5524 mpz_clear(v2);
5526 mpz_clear(v1);
5529 return ret;
5532 // Otherwise the arrays are not identical.
5533 return false;
5536 // Traversal.
5539 Array_type::do_traverse(Traverse* traverse)
5541 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
5542 return TRAVERSE_EXIT;
5543 if (this->length_ != NULL
5544 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
5545 return TRAVERSE_EXIT;
5546 return TRAVERSE_CONTINUE;
5549 // Check that the length is valid.
5551 bool
5552 Array_type::verify_length()
5554 if (this->length_ == NULL)
5555 return true;
5557 Type_context context(Type::lookup_integer_type("int"), false);
5558 this->length_->determine_type(&context);
5560 if (!this->length_->is_constant())
5562 error_at(this->length_->location(), "array bound is not constant");
5563 return false;
5566 Numeric_constant nc;
5567 if (!this->length_->numeric_constant_value(&nc))
5569 if (this->length_->type()->integer_type() != NULL
5570 || this->length_->type()->float_type() != NULL)
5571 error_at(this->length_->location(), "array bound is not constant");
5572 else
5573 error_at(this->length_->location(), "array bound is not numeric");
5574 return false;
5577 unsigned long val;
5578 switch (nc.to_unsigned_long(&val))
5580 case Numeric_constant::NC_UL_VALID:
5581 break;
5582 case Numeric_constant::NC_UL_NOTINT:
5583 error_at(this->length_->location(), "array bound truncated to integer");
5584 return false;
5585 case Numeric_constant::NC_UL_NEGATIVE:
5586 error_at(this->length_->location(), "negative array bound");
5587 return false;
5588 case Numeric_constant::NC_UL_BIG:
5589 error_at(this->length_->location(), "array bound overflows");
5590 return false;
5591 default:
5592 go_unreachable();
5595 Type* int_type = Type::lookup_integer_type("int");
5596 unsigned int tbits = int_type->integer_type()->bits();
5597 if (sizeof(val) <= tbits * 8
5598 && val >> (tbits - 1) != 0)
5600 error_at(this->length_->location(), "array bound overflows");
5601 return false;
5604 return true;
5607 // Verify the type.
5609 bool
5610 Array_type::do_verify()
5612 if (!this->verify_length())
5613 this->length_ = Expression::make_error(this->length_->location());
5614 return true;
5617 // Whether we can use memcmp to compare this array.
5619 bool
5620 Array_type::do_compare_is_identity(Gogo* gogo)
5622 if (this->length_ == NULL)
5623 return false;
5625 // Check for [...], which indicates that this is not a real type.
5626 if (this->length_->is_nil_expression())
5627 return false;
5629 if (!this->element_type_->compare_is_identity(gogo))
5630 return false;
5632 // If there is any padding, then we can't use memcmp.
5633 unsigned long size;
5634 unsigned long align;
5635 if (!this->element_type_->backend_type_size(gogo, &size)
5636 || !this->element_type_->backend_type_align(gogo, &align))
5637 return false;
5638 if ((size & (align - 1)) != 0)
5639 return false;
5641 return true;
5644 // Array type hash code.
5646 unsigned int
5647 Array_type::do_hash_for_method(Gogo* gogo) const
5649 // There is no very convenient way to get a hash code for the
5650 // length.
5651 return this->element_type_->hash_for_method(gogo) + 1;
5654 // Write the hash function for an array which can not use the identify
5655 // function.
5657 void
5658 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
5659 Function_type* hash_fntype,
5660 Function_type* equal_fntype)
5662 Location bloc = Linemap::predeclared_location();
5664 // The pointer to the array that we are going to hash. This is an
5665 // argument to the hash function we are implementing here.
5666 Named_object* key_arg = gogo->lookup("key", NULL);
5667 go_assert(key_arg != NULL);
5668 Type* key_arg_type = key_arg->var_value()->type();
5670 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5672 // Get a 0.
5673 mpz_t ival;
5674 mpz_init_set_ui(ival, 0);
5675 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
5676 mpz_clear(ival);
5678 // Make a temporary to hold the return value, initialized to 0.
5679 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
5680 bloc);
5681 gogo->add_statement(retval);
5683 // Make a temporary to hold the key as a uintptr.
5684 Expression* ref = Expression::make_var_reference(key_arg, bloc);
5685 ref = Expression::make_cast(uintptr_type, ref, bloc);
5686 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
5687 bloc);
5688 gogo->add_statement(key);
5690 // Loop over the array elements.
5691 // for i = range a
5692 Type* int_type = Type::lookup_integer_type("int");
5693 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5694 gogo->add_statement(index);
5696 Expression* iref = Expression::make_temporary_reference(index, bloc);
5697 Expression* aref = Expression::make_var_reference(key_arg, bloc);
5698 Type* pt = Type::make_pointer_type(name != NULL
5699 ? static_cast<Type*>(name)
5700 : static_cast<Type*>(this));
5701 aref = Expression::make_cast(pt, aref, bloc);
5702 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5703 NULL,
5704 aref,
5705 bloc);
5707 gogo->start_block(bloc);
5709 // Multiply retval by 33.
5710 mpz_init_set_ui(ival, 33);
5711 Expression* i33 = Expression::make_integer(&ival, uintptr_type, bloc);
5712 mpz_clear(ival);
5714 ref = Expression::make_temporary_reference(retval, bloc);
5715 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ, ref,
5716 i33, bloc);
5717 gogo->add_statement(s);
5719 // Get the hash function for the element type.
5720 Named_object* hash_fn;
5721 Named_object* equal_fn;
5722 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
5723 hash_fntype, equal_fntype, &hash_fn,
5724 &equal_fn);
5726 // Get a pointer to this element in the loop.
5727 Expression* subkey = Expression::make_temporary_reference(key, bloc);
5728 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
5730 // Get the size of each element.
5731 Expression* ele_size = Expression::make_type_info(this->element_type_,
5732 Expression::TYPE_INFO_SIZE);
5734 // Get the hash of this element.
5735 Expression_list* args = new Expression_list();
5736 args->push_back(subkey);
5737 args->push_back(ele_size);
5738 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
5739 Expression* call = Expression::make_call(func, args, false, bloc);
5741 // Add the element's hash value to retval.
5742 Temporary_reference_expression* tref =
5743 Expression::make_temporary_reference(retval, bloc);
5744 tref->set_is_lvalue();
5745 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, call, bloc);
5746 gogo->add_statement(s);
5748 // Increase the element pointer.
5749 tref = Expression::make_temporary_reference(key, bloc);
5750 tref->set_is_lvalue();
5751 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
5752 bloc);
5754 Block* statements = gogo->finish_block(bloc);
5756 for_range->add_statements(statements);
5757 gogo->add_statement(for_range);
5759 // Return retval to the caller of the hash function.
5760 Expression_list* vals = new Expression_list();
5761 ref = Expression::make_temporary_reference(retval, bloc);
5762 vals->push_back(ref);
5763 s = Statement::make_return_statement(vals, bloc);
5764 gogo->add_statement(s);
5767 // Write the equality function for an array which can not use the
5768 // identity function.
5770 void
5771 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
5773 Location bloc = Linemap::predeclared_location();
5775 // The pointers to the arrays we are going to compare.
5776 Named_object* key1_arg = gogo->lookup("key1", NULL);
5777 Named_object* key2_arg = gogo->lookup("key2", NULL);
5778 go_assert(key1_arg != NULL && key2_arg != NULL);
5780 // Build temporaries for the keys with the right types.
5781 Type* pt = Type::make_pointer_type(name != NULL
5782 ? static_cast<Type*>(name)
5783 : static_cast<Type*>(this));
5785 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
5786 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5787 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
5788 gogo->add_statement(p1);
5790 ref = Expression::make_var_reference(key2_arg, bloc);
5791 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5792 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
5793 gogo->add_statement(p2);
5795 // Loop over the array elements.
5796 // for i = range a
5797 Type* int_type = Type::lookup_integer_type("int");
5798 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5799 gogo->add_statement(index);
5801 Expression* iref = Expression::make_temporary_reference(index, bloc);
5802 Expression* aref = Expression::make_temporary_reference(p1, bloc);
5803 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5804 NULL,
5805 aref,
5806 bloc);
5808 gogo->start_block(bloc);
5810 // Compare element in P1 and P2.
5811 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
5812 e1 = Expression::make_unary(OPERATOR_MULT, e1, bloc);
5813 ref = Expression::make_temporary_reference(index, bloc);
5814 e1 = Expression::make_array_index(e1, ref, NULL, NULL, bloc);
5816 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
5817 e2 = Expression::make_unary(OPERATOR_MULT, e2, bloc);
5818 ref = Expression::make_temporary_reference(index, bloc);
5819 e2 = Expression::make_array_index(e2, ref, NULL, NULL, bloc);
5821 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
5823 // If the elements are not equal, return false.
5824 gogo->start_block(bloc);
5825 Expression_list* vals = new Expression_list();
5826 vals->push_back(Expression::make_boolean(false, bloc));
5827 Statement* s = Statement::make_return_statement(vals, bloc);
5828 gogo->add_statement(s);
5829 Block* then_block = gogo->finish_block(bloc);
5831 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
5832 gogo->add_statement(s);
5834 Block* statements = gogo->finish_block(bloc);
5836 for_range->add_statements(statements);
5837 gogo->add_statement(for_range);
5839 // All the elements are equal, so return true.
5840 vals = new Expression_list();
5841 vals->push_back(Expression::make_boolean(true, bloc));
5842 s = Statement::make_return_statement(vals, bloc);
5843 gogo->add_statement(s);
5846 // Get the backend representation of the fields of a slice. This is
5847 // not declared in types.h so that types.h doesn't have to #include
5848 // backend.h.
5850 // We use int for the count and capacity fields. This matches 6g.
5851 // The language more or less assumes that we can't allocate space of a
5852 // size which does not fit in int.
5854 static void
5855 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
5856 std::vector<Backend::Btyped_identifier>* bfields)
5858 bfields->resize(3);
5860 Type* pet = Type::make_pointer_type(type->element_type());
5861 Btype* pbet = (use_placeholder
5862 ? pet->get_backend_placeholder(gogo)
5863 : pet->get_backend(gogo));
5864 Location ploc = Linemap::predeclared_location();
5866 Backend::Btyped_identifier* p = &(*bfields)[0];
5867 p->name = "__values";
5868 p->btype = pbet;
5869 p->location = ploc;
5871 Type* int_type = Type::lookup_integer_type("int");
5873 p = &(*bfields)[1];
5874 p->name = "__count";
5875 p->btype = int_type->get_backend(gogo);
5876 p->location = ploc;
5878 p = &(*bfields)[2];
5879 p->name = "__capacity";
5880 p->btype = int_type->get_backend(gogo);
5881 p->location = ploc;
5884 // Get the backend representation for the type of this array. A fixed array is
5885 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
5886 // just like an array in C. An open array is a struct with three
5887 // fields: a data pointer, the length, and the capacity.
5889 Btype*
5890 Array_type::do_get_backend(Gogo* gogo)
5892 if (this->length_ == NULL)
5894 std::vector<Backend::Btyped_identifier> bfields;
5895 get_backend_slice_fields(gogo, this, false, &bfields);
5896 return gogo->backend()->struct_type(bfields);
5898 else
5900 Btype* element = this->get_backend_element(gogo, false);
5901 Bexpression* len = this->get_backend_length(gogo);
5902 return gogo->backend()->array_type(element, len);
5906 // Return the backend representation of the element type.
5908 Btype*
5909 Array_type::get_backend_element(Gogo* gogo, bool use_placeholder)
5911 if (use_placeholder)
5912 return this->element_type_->get_backend_placeholder(gogo);
5913 else
5914 return this->element_type_->get_backend(gogo);
5917 // Return the backend representation of the length. The length may be
5918 // computed using a function call, so we must only evaluate it once.
5920 Bexpression*
5921 Array_type::get_backend_length(Gogo* gogo)
5923 go_assert(this->length_ != NULL);
5924 if (this->blength_ == NULL)
5926 Numeric_constant nc;
5927 mpz_t val;
5928 if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val))
5930 if (mpz_sgn(val) < 0)
5932 this->blength_ = gogo->backend()->error_expression();
5933 return this->blength_;
5935 Type* t = nc.type();
5936 if (t == NULL)
5937 t = Type::lookup_integer_type("int");
5938 else if (t->is_abstract())
5939 t = t->make_non_abstract_type();
5940 Btype* btype = t->get_backend(gogo);
5941 this->blength_ =
5942 gogo->backend()->integer_constant_expression(btype, val);
5943 mpz_clear(val);
5945 else
5947 // Make up a translation context for the array length
5948 // expression. FIXME: This won't work in general.
5949 Translate_context context(gogo, NULL, NULL, NULL);
5950 this->blength_ = this->length_->get_backend(&context);
5952 Btype* ibtype = Type::lookup_integer_type("int")->get_backend(gogo);
5953 this->blength_ =
5954 gogo->backend()->convert_expression(ibtype, this->blength_,
5955 this->length_->location());
5958 return this->blength_;
5961 // Finish backend representation of the array.
5963 void
5964 Array_type::finish_backend_element(Gogo* gogo)
5966 Type* et = this->array_type()->element_type();
5967 et->get_backend(gogo);
5968 if (this->is_slice_type())
5970 // This relies on the fact that we always use the same
5971 // structure for a pointer to any given type.
5972 Type* pet = Type::make_pointer_type(et);
5973 pet->get_backend(gogo);
5977 // Return an expression for a pointer to the values in ARRAY.
5979 Expression*
5980 Array_type::get_value_pointer(Gogo*, Expression* array) const
5982 if (this->length() != NULL)
5984 // Fixed array.
5985 go_assert(array->type()->array_type() != NULL);
5986 Type* etype = array->type()->array_type()->element_type();
5987 array = Expression::make_unary(OPERATOR_AND, array, array->location());
5988 return Expression::make_cast(Type::make_pointer_type(etype), array,
5989 array->location());
5992 // Slice.
5993 return Expression::make_slice_info(array,
5994 Expression::SLICE_INFO_VALUE_POINTER,
5995 array->location());
5998 // Return an expression for the length of the array ARRAY which has this
5999 // type.
6001 Expression*
6002 Array_type::get_length(Gogo*, Expression* array) const
6004 if (this->length_ != NULL)
6005 return this->length_;
6007 // This is a slice. We need to read the length field.
6008 return Expression::make_slice_info(array, Expression::SLICE_INFO_LENGTH,
6009 array->location());
6012 // Return an expression for the capacity of the array ARRAY which has this
6013 // type.
6015 Expression*
6016 Array_type::get_capacity(Gogo*, Expression* array) const
6018 if (this->length_ != NULL)
6019 return this->length_;
6021 // This is a slice. We need to read the capacity field.
6022 return Expression::make_slice_info(array, Expression::SLICE_INFO_CAPACITY,
6023 array->location());
6026 // Export.
6028 void
6029 Array_type::do_export(Export* exp) const
6031 exp->write_c_string("[");
6032 if (this->length_ != NULL)
6033 this->length_->export_expression(exp);
6034 exp->write_c_string("] ");
6035 exp->write_type(this->element_type_);
6038 // Import.
6040 Array_type*
6041 Array_type::do_import(Import* imp)
6043 imp->require_c_string("[");
6044 Expression* length;
6045 if (imp->peek_char() == ']')
6046 length = NULL;
6047 else
6048 length = Expression::import_expression(imp);
6049 imp->require_c_string("] ");
6050 Type* element_type = imp->read_type();
6051 return Type::make_array_type(element_type, length);
6054 // The type of an array type descriptor.
6056 Type*
6057 Array_type::make_array_type_descriptor_type()
6059 static Type* ret;
6060 if (ret == NULL)
6062 Type* tdt = Type::make_type_descriptor_type();
6063 Type* ptdt = Type::make_type_descriptor_ptr_type();
6065 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6067 Struct_type* sf =
6068 Type::make_builtin_struct_type(4,
6069 "", tdt,
6070 "elem", ptdt,
6071 "slice", ptdt,
6072 "len", uintptr_type);
6074 ret = Type::make_builtin_named_type("ArrayType", sf);
6077 return ret;
6080 // The type of an slice type descriptor.
6082 Type*
6083 Array_type::make_slice_type_descriptor_type()
6085 static Type* ret;
6086 if (ret == NULL)
6088 Type* tdt = Type::make_type_descriptor_type();
6089 Type* ptdt = Type::make_type_descriptor_ptr_type();
6091 Struct_type* sf =
6092 Type::make_builtin_struct_type(2,
6093 "", tdt,
6094 "elem", ptdt);
6096 ret = Type::make_builtin_named_type("SliceType", sf);
6099 return ret;
6102 // Build a type descriptor for an array/slice type.
6104 Expression*
6105 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6107 if (this->length_ != NULL)
6108 return this->array_type_descriptor(gogo, name);
6109 else
6110 return this->slice_type_descriptor(gogo, name);
6113 // Build a type descriptor for an array type.
6115 Expression*
6116 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
6118 Location bloc = Linemap::predeclared_location();
6120 Type* atdt = Array_type::make_array_type_descriptor_type();
6122 const Struct_field_list* fields = atdt->struct_type()->fields();
6124 Expression_list* vals = new Expression_list();
6125 vals->reserve(3);
6127 Struct_field_list::const_iterator p = fields->begin();
6128 go_assert(p->is_field_name("commonType"));
6129 vals->push_back(this->type_descriptor_constructor(gogo,
6130 RUNTIME_TYPE_KIND_ARRAY,
6131 name, NULL, true));
6133 ++p;
6134 go_assert(p->is_field_name("elem"));
6135 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6137 ++p;
6138 go_assert(p->is_field_name("slice"));
6139 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
6140 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
6142 ++p;
6143 go_assert(p->is_field_name("len"));
6144 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
6146 ++p;
6147 go_assert(p == fields->end());
6149 return Expression::make_struct_composite_literal(atdt, vals, bloc);
6152 // Build a type descriptor for a slice type.
6154 Expression*
6155 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
6157 Location bloc = Linemap::predeclared_location();
6159 Type* stdt = Array_type::make_slice_type_descriptor_type();
6161 const Struct_field_list* fields = stdt->struct_type()->fields();
6163 Expression_list* vals = new Expression_list();
6164 vals->reserve(2);
6166 Struct_field_list::const_iterator p = fields->begin();
6167 go_assert(p->is_field_name("commonType"));
6168 vals->push_back(this->type_descriptor_constructor(gogo,
6169 RUNTIME_TYPE_KIND_SLICE,
6170 name, NULL, true));
6172 ++p;
6173 go_assert(p->is_field_name("elem"));
6174 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6176 ++p;
6177 go_assert(p == fields->end());
6179 return Expression::make_struct_composite_literal(stdt, vals, bloc);
6182 // Reflection string.
6184 void
6185 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
6187 ret->push_back('[');
6188 if (this->length_ != NULL)
6190 Numeric_constant nc;
6191 unsigned long val;
6192 if (!this->length_->numeric_constant_value(&nc)
6193 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
6194 error_at(this->length_->location(), "invalid array length");
6195 else
6197 char buf[50];
6198 snprintf(buf, sizeof buf, "%lu", val);
6199 ret->append(buf);
6202 ret->push_back(']');
6204 this->append_reflection(this->element_type_, gogo, ret);
6207 // Mangled name.
6209 void
6210 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6212 ret->push_back('A');
6213 this->append_mangled_name(this->element_type_, gogo, ret);
6214 if (this->length_ != NULL)
6216 Numeric_constant nc;
6217 unsigned long val;
6218 if (!this->length_->numeric_constant_value(&nc)
6219 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
6220 error_at(this->length_->location(), "invalid array length");
6221 else
6223 char buf[50];
6224 snprintf(buf, sizeof buf, "%lu", val);
6225 ret->append(buf);
6228 ret->push_back('e');
6231 // Make an array type.
6233 Array_type*
6234 Type::make_array_type(Type* element_type, Expression* length)
6236 return new Array_type(element_type, length);
6239 // Class Map_type.
6241 // Traversal.
6244 Map_type::do_traverse(Traverse* traverse)
6246 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
6247 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
6248 return TRAVERSE_EXIT;
6249 return TRAVERSE_CONTINUE;
6252 // Check that the map type is OK.
6254 bool
6255 Map_type::do_verify()
6257 // The runtime support uses "map[void]void".
6258 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
6259 error_at(this->location_, "invalid map key type");
6260 return true;
6263 // Whether two map types are identical.
6265 bool
6266 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
6268 return (Type::are_identical(this->key_type(), t->key_type(),
6269 errors_are_identical, NULL)
6270 && Type::are_identical(this->val_type(), t->val_type(),
6271 errors_are_identical, NULL));
6274 // Hash code.
6276 unsigned int
6277 Map_type::do_hash_for_method(Gogo* gogo) const
6279 return (this->key_type_->hash_for_method(gogo)
6280 + this->val_type_->hash_for_method(gogo)
6281 + 2);
6284 // Get the backend representation for a map type. A map type is
6285 // represented as a pointer to a struct. The struct is __go_map in
6286 // libgo/map.h.
6288 Btype*
6289 Map_type::do_get_backend(Gogo* gogo)
6291 static Btype* backend_map_type;
6292 if (backend_map_type == NULL)
6294 std::vector<Backend::Btyped_identifier> bfields(4);
6296 Location bloc = Linemap::predeclared_location();
6298 Type* pdt = Type::make_type_descriptor_ptr_type();
6299 bfields[0].name = "__descriptor";
6300 bfields[0].btype = pdt->get_backend(gogo);
6301 bfields[0].location = bloc;
6303 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6304 bfields[1].name = "__element_count";
6305 bfields[1].btype = uintptr_type->get_backend(gogo);
6306 bfields[1].location = bloc;
6308 bfields[2].name = "__bucket_count";
6309 bfields[2].btype = bfields[1].btype;
6310 bfields[2].location = bloc;
6312 Btype* bvt = gogo->backend()->void_type();
6313 Btype* bpvt = gogo->backend()->pointer_type(bvt);
6314 Btype* bppvt = gogo->backend()->pointer_type(bpvt);
6315 bfields[3].name = "__buckets";
6316 bfields[3].btype = bppvt;
6317 bfields[3].location = bloc;
6319 Btype *bt = gogo->backend()->struct_type(bfields);
6320 bt = gogo->backend()->named_type("__go_map", bt, bloc);
6321 backend_map_type = gogo->backend()->pointer_type(bt);
6323 return backend_map_type;
6326 // The type of a map type descriptor.
6328 Type*
6329 Map_type::make_map_type_descriptor_type()
6331 static Type* ret;
6332 if (ret == NULL)
6334 Type* tdt = Type::make_type_descriptor_type();
6335 Type* ptdt = Type::make_type_descriptor_ptr_type();
6337 Struct_type* sf =
6338 Type::make_builtin_struct_type(3,
6339 "", tdt,
6340 "key", ptdt,
6341 "elem", ptdt);
6343 ret = Type::make_builtin_named_type("MapType", sf);
6346 return ret;
6349 // Build a type descriptor for a map type.
6351 Expression*
6352 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6354 Location bloc = Linemap::predeclared_location();
6356 Type* mtdt = Map_type::make_map_type_descriptor_type();
6358 const Struct_field_list* fields = mtdt->struct_type()->fields();
6360 Expression_list* vals = new Expression_list();
6361 vals->reserve(3);
6363 Struct_field_list::const_iterator p = fields->begin();
6364 go_assert(p->is_field_name("commonType"));
6365 vals->push_back(this->type_descriptor_constructor(gogo,
6366 RUNTIME_TYPE_KIND_MAP,
6367 name, NULL, true));
6369 ++p;
6370 go_assert(p->is_field_name("key"));
6371 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
6373 ++p;
6374 go_assert(p->is_field_name("elem"));
6375 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
6377 ++p;
6378 go_assert(p == fields->end());
6380 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
6383 // A mapping from map types to map descriptors.
6385 Map_type::Map_descriptors Map_type::map_descriptors;
6387 // Build a map descriptor for this type. Return a pointer to it.
6389 Bexpression*
6390 Map_type::map_descriptor_pointer(Gogo* gogo, Location location)
6392 Bvariable* bvar = this->map_descriptor(gogo);
6393 Bexpression* var_expr = gogo->backend()->var_expression(bvar, location);
6394 return gogo->backend()->address_expression(var_expr, location);
6397 // Build a map descriptor for this type.
6399 Bvariable*
6400 Map_type::map_descriptor(Gogo* gogo)
6402 std::pair<Map_type*, Bvariable*> val(this, NULL);
6403 std::pair<Map_type::Map_descriptors::iterator, bool> ins =
6404 Map_type::map_descriptors.insert(val);
6405 if (!ins.second)
6406 return ins.first->second;
6408 Type* key_type = this->key_type_;
6409 Type* val_type = this->val_type_;
6411 // The map entry type is a struct with three fields. Build that
6412 // struct so that we can get the offsets of the key and value within
6413 // a map entry. The first field should technically be a pointer to
6414 // this type itself, but since we only care about field offsets we
6415 // just use pointer to bool.
6416 Type* pbool = Type::make_pointer_type(Type::make_boolean_type());
6417 Struct_type* map_entry_type =
6418 Type::make_builtin_struct_type(3,
6419 "__next", pbool,
6420 "__key", key_type,
6421 "__val", val_type);
6423 Type* map_descriptor_type = Map_type::make_map_descriptor_type();
6425 const Struct_field_list* fields =
6426 map_descriptor_type->struct_type()->fields();
6428 Expression_list* vals = new Expression_list();
6429 vals->reserve(4);
6431 Location bloc = Linemap::predeclared_location();
6433 Struct_field_list::const_iterator p = fields->begin();
6435 go_assert(p->is_field_name("__map_descriptor"));
6436 vals->push_back(Expression::make_type_descriptor(this, bloc));
6438 ++p;
6439 go_assert(p->is_field_name("__entry_size"));
6440 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
6441 vals->push_back(Expression::make_type_info(map_entry_type, type_info));
6443 Struct_field_list::const_iterator pf = map_entry_type->fields()->begin();
6444 ++pf;
6445 go_assert(pf->is_field_name("__key"));
6447 ++p;
6448 go_assert(p->is_field_name("__key_offset"));
6449 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6451 ++pf;
6452 go_assert(pf->is_field_name("__val"));
6454 ++p;
6455 go_assert(p->is_field_name("__val_offset"));
6456 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6458 ++p;
6459 go_assert(p == fields->end());
6461 Expression* initializer =
6462 Expression::make_struct_composite_literal(map_descriptor_type, vals, bloc);
6464 std::string mangled_name = "__go_map_" + this->mangled_name(gogo);
6465 Btype* map_descriptor_btype = map_descriptor_type->get_backend(gogo);
6466 Bvariable* bvar = gogo->backend()->immutable_struct(mangled_name, false,
6467 true,
6468 map_descriptor_btype,
6469 bloc);
6471 Translate_context context(gogo, NULL, NULL, NULL);
6472 context.set_is_const();
6473 Bexpression* binitializer = initializer->get_backend(&context);
6475 gogo->backend()->immutable_struct_set_init(bvar, mangled_name, false, true,
6476 map_descriptor_btype, bloc,
6477 binitializer);
6479 ins.first->second = bvar;
6480 return bvar;
6483 // Build the type of a map descriptor. This must match the struct
6484 // __go_map_descriptor in libgo/runtime/map.h.
6486 Type*
6487 Map_type::make_map_descriptor_type()
6489 static Type* ret;
6490 if (ret == NULL)
6492 Type* ptdt = Type::make_type_descriptor_ptr_type();
6493 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6494 Struct_type* sf =
6495 Type::make_builtin_struct_type(4,
6496 "__map_descriptor", ptdt,
6497 "__entry_size", uintptr_type,
6498 "__key_offset", uintptr_type,
6499 "__val_offset", uintptr_type);
6500 ret = Type::make_builtin_named_type("__go_map_descriptor", sf);
6502 return ret;
6505 // Reflection string for a map.
6507 void
6508 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
6510 ret->append("map[");
6511 this->append_reflection(this->key_type_, gogo, ret);
6512 ret->append("]");
6513 this->append_reflection(this->val_type_, gogo, ret);
6516 // Mangled name for a map.
6518 void
6519 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6521 ret->push_back('M');
6522 this->append_mangled_name(this->key_type_, gogo, ret);
6523 ret->append("__");
6524 this->append_mangled_name(this->val_type_, gogo, ret);
6527 // Export a map type.
6529 void
6530 Map_type::do_export(Export* exp) const
6532 exp->write_c_string("map [");
6533 exp->write_type(this->key_type_);
6534 exp->write_c_string("] ");
6535 exp->write_type(this->val_type_);
6538 // Import a map type.
6540 Map_type*
6541 Map_type::do_import(Import* imp)
6543 imp->require_c_string("map [");
6544 Type* key_type = imp->read_type();
6545 imp->require_c_string("] ");
6546 Type* val_type = imp->read_type();
6547 return Type::make_map_type(key_type, val_type, imp->location());
6550 // Make a map type.
6552 Map_type*
6553 Type::make_map_type(Type* key_type, Type* val_type, Location location)
6555 return new Map_type(key_type, val_type, location);
6558 // Class Channel_type.
6560 // Hash code.
6562 unsigned int
6563 Channel_type::do_hash_for_method(Gogo* gogo) const
6565 unsigned int ret = 0;
6566 if (this->may_send_)
6567 ret += 1;
6568 if (this->may_receive_)
6569 ret += 2;
6570 if (this->element_type_ != NULL)
6571 ret += this->element_type_->hash_for_method(gogo) << 2;
6572 return ret << 3;
6575 // Whether this type is the same as T.
6577 bool
6578 Channel_type::is_identical(const Channel_type* t,
6579 bool errors_are_identical) const
6581 if (!Type::are_identical(this->element_type(), t->element_type(),
6582 errors_are_identical, NULL))
6583 return false;
6584 return (this->may_send_ == t->may_send_
6585 && this->may_receive_ == t->may_receive_);
6588 // Return the backend representation for a channel type. A channel is a pointer
6589 // to a __go_channel struct. The __go_channel struct is defined in
6590 // libgo/runtime/channel.h.
6592 Btype*
6593 Channel_type::do_get_backend(Gogo* gogo)
6595 static Btype* backend_channel_type;
6596 if (backend_channel_type == NULL)
6598 std::vector<Backend::Btyped_identifier> bfields;
6599 Btype* bt = gogo->backend()->struct_type(bfields);
6600 bt = gogo->backend()->named_type("__go_channel", bt,
6601 Linemap::predeclared_location());
6602 backend_channel_type = gogo->backend()->pointer_type(bt);
6604 return backend_channel_type;
6607 // Build a type descriptor for a channel type.
6609 Type*
6610 Channel_type::make_chan_type_descriptor_type()
6612 static Type* ret;
6613 if (ret == NULL)
6615 Type* tdt = Type::make_type_descriptor_type();
6616 Type* ptdt = Type::make_type_descriptor_ptr_type();
6618 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6620 Struct_type* sf =
6621 Type::make_builtin_struct_type(3,
6622 "", tdt,
6623 "elem", ptdt,
6624 "dir", uintptr_type);
6626 ret = Type::make_builtin_named_type("ChanType", sf);
6629 return ret;
6632 // Build a type descriptor for a map type.
6634 Expression*
6635 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6637 Location bloc = Linemap::predeclared_location();
6639 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
6641 const Struct_field_list* fields = ctdt->struct_type()->fields();
6643 Expression_list* vals = new Expression_list();
6644 vals->reserve(3);
6646 Struct_field_list::const_iterator p = fields->begin();
6647 go_assert(p->is_field_name("commonType"));
6648 vals->push_back(this->type_descriptor_constructor(gogo,
6649 RUNTIME_TYPE_KIND_CHAN,
6650 name, NULL, true));
6652 ++p;
6653 go_assert(p->is_field_name("elem"));
6654 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6656 ++p;
6657 go_assert(p->is_field_name("dir"));
6658 // These bits must match the ones in libgo/runtime/go-type.h.
6659 int val = 0;
6660 if (this->may_receive_)
6661 val |= 1;
6662 if (this->may_send_)
6663 val |= 2;
6664 mpz_t iv;
6665 mpz_init_set_ui(iv, val);
6666 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
6667 mpz_clear(iv);
6669 ++p;
6670 go_assert(p == fields->end());
6672 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
6675 // Reflection string.
6677 void
6678 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
6680 if (!this->may_send_)
6681 ret->append("<-");
6682 ret->append("chan");
6683 if (!this->may_receive_)
6684 ret->append("<-");
6685 ret->push_back(' ');
6686 this->append_reflection(this->element_type_, gogo, ret);
6689 // Mangled name.
6691 void
6692 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6694 ret->push_back('C');
6695 this->append_mangled_name(this->element_type_, gogo, ret);
6696 if (this->may_send_)
6697 ret->push_back('s');
6698 if (this->may_receive_)
6699 ret->push_back('r');
6700 ret->push_back('e');
6703 // Export.
6705 void
6706 Channel_type::do_export(Export* exp) const
6708 exp->write_c_string("chan ");
6709 if (this->may_send_ && !this->may_receive_)
6710 exp->write_c_string("-< ");
6711 else if (this->may_receive_ && !this->may_send_)
6712 exp->write_c_string("<- ");
6713 exp->write_type(this->element_type_);
6716 // Import.
6718 Channel_type*
6719 Channel_type::do_import(Import* imp)
6721 imp->require_c_string("chan ");
6723 bool may_send;
6724 bool may_receive;
6725 if (imp->match_c_string("-< "))
6727 imp->advance(3);
6728 may_send = true;
6729 may_receive = false;
6731 else if (imp->match_c_string("<- "))
6733 imp->advance(3);
6734 may_receive = true;
6735 may_send = false;
6737 else
6739 may_send = true;
6740 may_receive = true;
6743 Type* element_type = imp->read_type();
6745 return Type::make_channel_type(may_send, may_receive, element_type);
6748 // Make a new channel type.
6750 Channel_type*
6751 Type::make_channel_type(bool send, bool receive, Type* element_type)
6753 return new Channel_type(send, receive, element_type);
6756 // Class Interface_type.
6758 // Return the list of methods.
6760 const Typed_identifier_list*
6761 Interface_type::methods() const
6763 go_assert(this->methods_are_finalized_ || saw_errors());
6764 return this->all_methods_;
6767 // Return the number of methods.
6769 size_t
6770 Interface_type::method_count() const
6772 go_assert(this->methods_are_finalized_ || saw_errors());
6773 return this->all_methods_ == NULL ? 0 : this->all_methods_->size();
6776 // Traversal.
6779 Interface_type::do_traverse(Traverse* traverse)
6781 Typed_identifier_list* methods = (this->methods_are_finalized_
6782 ? this->all_methods_
6783 : this->parse_methods_);
6784 if (methods == NULL)
6785 return TRAVERSE_CONTINUE;
6786 return methods->traverse(traverse);
6789 // Finalize the methods. This handles interface inheritance.
6791 void
6792 Interface_type::finalize_methods()
6794 if (this->methods_are_finalized_)
6795 return;
6796 this->methods_are_finalized_ = true;
6797 if (this->parse_methods_ == NULL)
6798 return;
6800 this->all_methods_ = new Typed_identifier_list();
6801 this->all_methods_->reserve(this->parse_methods_->size());
6802 Typed_identifier_list inherit;
6803 for (Typed_identifier_list::const_iterator pm =
6804 this->parse_methods_->begin();
6805 pm != this->parse_methods_->end();
6806 ++pm)
6808 const Typed_identifier* p = &*pm;
6809 if (p->name().empty())
6810 inherit.push_back(*p);
6811 else if (this->find_method(p->name()) == NULL)
6812 this->all_methods_->push_back(*p);
6813 else
6814 error_at(p->location(), "duplicate method %qs",
6815 Gogo::message_name(p->name()).c_str());
6818 std::vector<Named_type*> seen;
6819 seen.reserve(inherit.size());
6820 bool issued_recursive_error = false;
6821 while (!inherit.empty())
6823 Type* t = inherit.back().type();
6824 Location tl = inherit.back().location();
6825 inherit.pop_back();
6827 Interface_type* it = t->interface_type();
6828 if (it == NULL)
6830 if (!t->is_error())
6831 error_at(tl, "interface contains embedded non-interface");
6832 continue;
6834 if (it == this)
6836 if (!issued_recursive_error)
6838 error_at(tl, "invalid recursive interface");
6839 issued_recursive_error = true;
6841 continue;
6844 Named_type* nt = t->named_type();
6845 if (nt != NULL && it->parse_methods_ != NULL)
6847 std::vector<Named_type*>::const_iterator q;
6848 for (q = seen.begin(); q != seen.end(); ++q)
6850 if (*q == nt)
6852 error_at(tl, "inherited interface loop");
6853 break;
6856 if (q != seen.end())
6857 continue;
6858 seen.push_back(nt);
6861 const Typed_identifier_list* imethods = it->parse_methods_;
6862 if (imethods == NULL)
6863 continue;
6864 for (Typed_identifier_list::const_iterator q = imethods->begin();
6865 q != imethods->end();
6866 ++q)
6868 if (q->name().empty())
6869 inherit.push_back(*q);
6870 else if (this->find_method(q->name()) == NULL)
6871 this->all_methods_->push_back(Typed_identifier(q->name(),
6872 q->type(), tl));
6873 else
6874 error_at(tl, "inherited method %qs is ambiguous",
6875 Gogo::message_name(q->name()).c_str());
6879 if (!this->all_methods_->empty())
6880 this->all_methods_->sort_by_name();
6881 else
6883 delete this->all_methods_;
6884 this->all_methods_ = NULL;
6888 // Return the method NAME, or NULL.
6890 const Typed_identifier*
6891 Interface_type::find_method(const std::string& name) const
6893 go_assert(this->methods_are_finalized_);
6894 if (this->all_methods_ == NULL)
6895 return NULL;
6896 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6897 p != this->all_methods_->end();
6898 ++p)
6899 if (p->name() == name)
6900 return &*p;
6901 return NULL;
6904 // Return the method index.
6906 size_t
6907 Interface_type::method_index(const std::string& name) const
6909 go_assert(this->methods_are_finalized_ && this->all_methods_ != NULL);
6910 size_t ret = 0;
6911 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6912 p != this->all_methods_->end();
6913 ++p, ++ret)
6914 if (p->name() == name)
6915 return ret;
6916 go_unreachable();
6919 // Return whether NAME is an unexported method, for better error
6920 // reporting.
6922 bool
6923 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
6925 go_assert(this->methods_are_finalized_);
6926 if (this->all_methods_ == NULL)
6927 return false;
6928 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6929 p != this->all_methods_->end();
6930 ++p)
6932 const std::string& method_name(p->name());
6933 if (Gogo::is_hidden_name(method_name)
6934 && name == Gogo::unpack_hidden_name(method_name)
6935 && gogo->pack_hidden_name(name, false) != method_name)
6936 return true;
6938 return false;
6941 // Whether this type is identical with T.
6943 bool
6944 Interface_type::is_identical(const Interface_type* t,
6945 bool errors_are_identical) const
6947 // If methods have not been finalized, then we are asking whether
6948 // func redeclarations are the same. This is an error, so for
6949 // simplicity we say they are never the same.
6950 if (!this->methods_are_finalized_ || !t->methods_are_finalized_)
6951 return false;
6953 // We require the same methods with the same types. The methods
6954 // have already been sorted.
6955 if (this->all_methods_ == NULL || t->all_methods_ == NULL)
6956 return this->all_methods_ == t->all_methods_;
6958 if (this->assume_identical(this, t) || t->assume_identical(t, this))
6959 return true;
6961 Assume_identical* hold_ai = this->assume_identical_;
6962 Assume_identical ai;
6963 ai.t1 = this;
6964 ai.t2 = t;
6965 ai.next = hold_ai;
6966 this->assume_identical_ = &ai;
6968 Typed_identifier_list::const_iterator p1 = this->all_methods_->begin();
6969 Typed_identifier_list::const_iterator p2;
6970 for (p2 = t->all_methods_->begin(); p2 != t->all_methods_->end(); ++p1, ++p2)
6972 if (p1 == this->all_methods_->end())
6973 break;
6974 if (p1->name() != p2->name()
6975 || !Type::are_identical(p1->type(), p2->type(),
6976 errors_are_identical, NULL))
6977 break;
6980 this->assume_identical_ = hold_ai;
6982 return p1 == this->all_methods_->end() && p2 == t->all_methods_->end();
6985 // Return true if T1 and T2 are assumed to be identical during a type
6986 // comparison.
6988 bool
6989 Interface_type::assume_identical(const Interface_type* t1,
6990 const Interface_type* t2) const
6992 for (Assume_identical* p = this->assume_identical_;
6993 p != NULL;
6994 p = p->next)
6995 if ((p->t1 == t1 && p->t2 == t2) || (p->t1 == t2 && p->t2 == t1))
6996 return true;
6997 return false;
7000 // Whether we can assign the interface type T to this type. The types
7001 // are known to not be identical. An interface assignment is only
7002 // permitted if T is known to implement all methods in THIS.
7003 // Otherwise a type guard is required.
7005 bool
7006 Interface_type::is_compatible_for_assign(const Interface_type* t,
7007 std::string* reason) const
7009 go_assert(this->methods_are_finalized_ && t->methods_are_finalized_);
7010 if (this->all_methods_ == NULL)
7011 return true;
7012 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
7013 p != this->all_methods_->end();
7014 ++p)
7016 const Typed_identifier* m = t->find_method(p->name());
7017 if (m == NULL)
7019 if (reason != NULL)
7021 char buf[200];
7022 snprintf(buf, sizeof buf,
7023 _("need explicit conversion; missing method %s%s%s"),
7024 open_quote, Gogo::message_name(p->name()).c_str(),
7025 close_quote);
7026 reason->assign(buf);
7028 return false;
7031 std::string subreason;
7032 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
7034 if (reason != NULL)
7036 std::string n = Gogo::message_name(p->name());
7037 size_t len = 100 + n.length() + subreason.length();
7038 char* buf = new char[len];
7039 if (subreason.empty())
7040 snprintf(buf, len, _("incompatible type for method %s%s%s"),
7041 open_quote, n.c_str(), close_quote);
7042 else
7043 snprintf(buf, len,
7044 _("incompatible type for method %s%s%s (%s)"),
7045 open_quote, n.c_str(), close_quote,
7046 subreason.c_str());
7047 reason->assign(buf);
7048 delete[] buf;
7050 return false;
7054 return true;
7057 // Hash code.
7059 unsigned int
7060 Interface_type::do_hash_for_method(Gogo*) const
7062 go_assert(this->methods_are_finalized_);
7063 unsigned int ret = 0;
7064 if (this->all_methods_ != NULL)
7066 for (Typed_identifier_list::const_iterator p =
7067 this->all_methods_->begin();
7068 p != this->all_methods_->end();
7069 ++p)
7071 ret = Type::hash_string(p->name(), ret);
7072 // We don't use the method type in the hash, to avoid
7073 // infinite recursion if an interface method uses a type
7074 // which is an interface which inherits from the interface
7075 // itself.
7076 // type T interface { F() interface {T}}
7077 ret <<= 1;
7080 return ret;
7083 // Return true if T implements the interface. If it does not, and
7084 // REASON is not NULL, set *REASON to a useful error message.
7086 bool
7087 Interface_type::implements_interface(const Type* t, std::string* reason) const
7089 go_assert(this->methods_are_finalized_);
7090 if (this->all_methods_ == NULL)
7091 return true;
7093 bool is_pointer = false;
7094 const Named_type* nt = t->named_type();
7095 const Struct_type* st = t->struct_type();
7096 // If we start with a named type, we don't dereference it to find
7097 // methods.
7098 if (nt == NULL)
7100 const Type* pt = t->points_to();
7101 if (pt != NULL)
7103 // If T is a pointer to a named type, then we need to look at
7104 // the type to which it points.
7105 is_pointer = true;
7106 nt = pt->named_type();
7107 st = pt->struct_type();
7111 // If we have a named type, get the methods from it rather than from
7112 // any struct type.
7113 if (nt != NULL)
7114 st = NULL;
7116 // Only named and struct types have methods.
7117 if (nt == NULL && st == NULL)
7119 if (reason != NULL)
7121 if (t->points_to() != NULL
7122 && t->points_to()->interface_type() != NULL)
7123 reason->assign(_("pointer to interface type has no methods"));
7124 else
7125 reason->assign(_("type has no methods"));
7127 return false;
7130 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
7132 if (reason != NULL)
7134 if (t->points_to() != NULL
7135 && t->points_to()->interface_type() != NULL)
7136 reason->assign(_("pointer to interface type has no methods"));
7137 else
7138 reason->assign(_("type has no methods"));
7140 return false;
7143 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
7144 p != this->all_methods_->end();
7145 ++p)
7147 bool is_ambiguous = false;
7148 Method* m = (nt != NULL
7149 ? nt->method_function(p->name(), &is_ambiguous)
7150 : st->method_function(p->name(), &is_ambiguous));
7151 if (m == NULL)
7153 if (reason != NULL)
7155 std::string n = Gogo::message_name(p->name());
7156 size_t len = n.length() + 100;
7157 char* buf = new char[len];
7158 if (is_ambiguous)
7159 snprintf(buf, len, _("ambiguous method %s%s%s"),
7160 open_quote, n.c_str(), close_quote);
7161 else
7162 snprintf(buf, len, _("missing method %s%s%s"),
7163 open_quote, n.c_str(), close_quote);
7164 reason->assign(buf);
7165 delete[] buf;
7167 return false;
7170 Function_type *p_fn_type = p->type()->function_type();
7171 Function_type* m_fn_type = m->type()->function_type();
7172 go_assert(p_fn_type != NULL && m_fn_type != NULL);
7173 std::string subreason;
7174 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
7176 if (reason != NULL)
7178 std::string n = Gogo::message_name(p->name());
7179 size_t len = 100 + n.length() + subreason.length();
7180 char* buf = new char[len];
7181 if (subreason.empty())
7182 snprintf(buf, len, _("incompatible type for method %s%s%s"),
7183 open_quote, n.c_str(), close_quote);
7184 else
7185 snprintf(buf, len,
7186 _("incompatible type for method %s%s%s (%s)"),
7187 open_quote, n.c_str(), close_quote,
7188 subreason.c_str());
7189 reason->assign(buf);
7190 delete[] buf;
7192 return false;
7195 if (!is_pointer && !m->is_value_method())
7197 if (reason != NULL)
7199 std::string n = Gogo::message_name(p->name());
7200 size_t len = 100 + n.length();
7201 char* buf = new char[len];
7202 snprintf(buf, len,
7203 _("method %s%s%s requires a pointer receiver"),
7204 open_quote, n.c_str(), close_quote);
7205 reason->assign(buf);
7206 delete[] buf;
7208 return false;
7211 // If the magic //go:nointerface comment was used, the method
7212 // may not be used to implement interfaces.
7213 if (m->nointerface())
7215 if (reason != NULL)
7217 std::string n = Gogo::message_name(p->name());
7218 size_t len = 100 + n.length();
7219 char* buf = new char[len];
7220 snprintf(buf, len,
7221 _("method %s%s%s is marked go:nointerface"),
7222 open_quote, n.c_str(), close_quote);
7223 reason->assign(buf);
7224 delete[] buf;
7226 return false;
7230 return true;
7233 // Return the backend representation of the empty interface type. We
7234 // use the same struct for all empty interfaces.
7236 Btype*
7237 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
7239 static Btype* empty_interface_type;
7240 if (empty_interface_type == NULL)
7242 std::vector<Backend::Btyped_identifier> bfields(2);
7244 Location bloc = Linemap::predeclared_location();
7246 Type* pdt = Type::make_type_descriptor_ptr_type();
7247 bfields[0].name = "__type_descriptor";
7248 bfields[0].btype = pdt->get_backend(gogo);
7249 bfields[0].location = bloc;
7251 Type* vt = Type::make_pointer_type(Type::make_void_type());
7252 bfields[1].name = "__object";
7253 bfields[1].btype = vt->get_backend(gogo);
7254 bfields[1].location = bloc;
7256 empty_interface_type = gogo->backend()->struct_type(bfields);
7258 return empty_interface_type;
7261 // Return a pointer to the backend representation of the method table.
7263 Btype*
7264 Interface_type::get_backend_methods(Gogo* gogo)
7266 if (this->bmethods_ != NULL && !this->bmethods_is_placeholder_)
7267 return this->bmethods_;
7269 Location loc = this->location();
7271 std::vector<Backend::Btyped_identifier>
7272 mfields(this->all_methods_->size() + 1);
7274 Type* pdt = Type::make_type_descriptor_ptr_type();
7275 mfields[0].name = "__type_descriptor";
7276 mfields[0].btype = pdt->get_backend(gogo);
7277 mfields[0].location = loc;
7279 std::string last_name = "";
7280 size_t i = 1;
7281 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
7282 p != this->all_methods_->end();
7283 ++p, ++i)
7285 // The type of the method in Go only includes the parameters.
7286 // The actual method also has a receiver, which is always a
7287 // pointer. We need to add that pointer type here in order to
7288 // generate the correct type for the backend.
7289 Function_type* ft = p->type()->function_type();
7290 go_assert(ft->receiver() == NULL);
7292 const Typed_identifier_list* params = ft->parameters();
7293 Typed_identifier_list* mparams = new Typed_identifier_list();
7294 if (params != NULL)
7295 mparams->reserve(params->size() + 1);
7296 Type* vt = Type::make_pointer_type(Type::make_void_type());
7297 mparams->push_back(Typed_identifier("", vt, ft->location()));
7298 if (params != NULL)
7300 for (Typed_identifier_list::const_iterator pp = params->begin();
7301 pp != params->end();
7302 ++pp)
7303 mparams->push_back(*pp);
7306 Typed_identifier_list* mresults = (ft->results() == NULL
7307 ? NULL
7308 : ft->results()->copy());
7309 Function_type* mft = Type::make_function_type(NULL, mparams, mresults,
7310 ft->location());
7312 mfields[i].name = Gogo::unpack_hidden_name(p->name());
7313 mfields[i].btype = mft->get_backend_fntype(gogo);
7314 mfields[i].location = loc;
7316 // Sanity check: the names should be sorted.
7317 go_assert(p->name() > last_name);
7318 last_name = p->name();
7321 Btype* st = gogo->backend()->struct_type(mfields);
7322 Btype* ret = gogo->backend()->pointer_type(st);
7324 if (this->bmethods_ != NULL && this->bmethods_is_placeholder_)
7325 gogo->backend()->set_placeholder_pointer_type(this->bmethods_, ret);
7326 this->bmethods_ = ret;
7327 this->bmethods_is_placeholder_ = false;
7328 return ret;
7331 // Return a placeholder for the pointer to the backend methods table.
7333 Btype*
7334 Interface_type::get_backend_methods_placeholder(Gogo* gogo)
7336 if (this->bmethods_ == NULL)
7338 Location loc = this->location();
7339 this->bmethods_ = gogo->backend()->placeholder_pointer_type("", loc,
7340 false);
7341 this->bmethods_is_placeholder_ = true;
7343 return this->bmethods_;
7346 // Return the fields of a non-empty interface type. This is not
7347 // declared in types.h so that types.h doesn't have to #include
7348 // backend.h.
7350 static void
7351 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
7352 bool use_placeholder,
7353 std::vector<Backend::Btyped_identifier>* bfields)
7355 Location loc = type->location();
7357 bfields->resize(2);
7359 (*bfields)[0].name = "__methods";
7360 (*bfields)[0].btype = (use_placeholder
7361 ? type->get_backend_methods_placeholder(gogo)
7362 : type->get_backend_methods(gogo));
7363 (*bfields)[0].location = loc;
7365 Type* vt = Type::make_pointer_type(Type::make_void_type());
7366 (*bfields)[1].name = "__object";
7367 (*bfields)[1].btype = vt->get_backend(gogo);
7368 (*bfields)[1].location = Linemap::predeclared_location();
7371 // Return the backend representation for an interface type. An interface is a
7372 // pointer to a struct. The struct has three fields. The first field is a
7373 // pointer to the type descriptor for the dynamic type of the object.
7374 // The second field is a pointer to a table of methods for the
7375 // interface to be used with the object. The third field is the value
7376 // of the object itself.
7378 Btype*
7379 Interface_type::do_get_backend(Gogo* gogo)
7381 if (this->is_empty())
7382 return Interface_type::get_backend_empty_interface_type(gogo);
7383 else
7385 if (this->interface_btype_ != NULL)
7386 return this->interface_btype_;
7387 this->interface_btype_ =
7388 gogo->backend()->placeholder_struct_type("", this->location_);
7389 std::vector<Backend::Btyped_identifier> bfields;
7390 get_backend_interface_fields(gogo, this, false, &bfields);
7391 if (!gogo->backend()->set_placeholder_struct_type(this->interface_btype_,
7392 bfields))
7393 this->interface_btype_ = gogo->backend()->error_type();
7394 return this->interface_btype_;
7398 // Finish the backend representation of the methods.
7400 void
7401 Interface_type::finish_backend_methods(Gogo* gogo)
7403 if (!this->is_empty())
7405 const Typed_identifier_list* methods = this->methods();
7406 if (methods != NULL)
7408 for (Typed_identifier_list::const_iterator p = methods->begin();
7409 p != methods->end();
7410 ++p)
7411 p->type()->get_backend(gogo);
7414 // Getting the backend methods now will set the placeholder
7415 // pointer.
7416 this->get_backend_methods(gogo);
7420 // The type of an interface type descriptor.
7422 Type*
7423 Interface_type::make_interface_type_descriptor_type()
7425 static Type* ret;
7426 if (ret == NULL)
7428 Type* tdt = Type::make_type_descriptor_type();
7429 Type* ptdt = Type::make_type_descriptor_ptr_type();
7431 Type* string_type = Type::lookup_string_type();
7432 Type* pointer_string_type = Type::make_pointer_type(string_type);
7434 Struct_type* sm =
7435 Type::make_builtin_struct_type(3,
7436 "name", pointer_string_type,
7437 "pkgPath", pointer_string_type,
7438 "typ", ptdt);
7440 Type* nsm = Type::make_builtin_named_type("imethod", sm);
7442 Type* slice_nsm = Type::make_array_type(nsm, NULL);
7444 Struct_type* s = Type::make_builtin_struct_type(2,
7445 "", tdt,
7446 "methods", slice_nsm);
7448 ret = Type::make_builtin_named_type("InterfaceType", s);
7451 return ret;
7454 // Build a type descriptor for an interface type.
7456 Expression*
7457 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7459 Location bloc = Linemap::predeclared_location();
7461 Type* itdt = Interface_type::make_interface_type_descriptor_type();
7463 const Struct_field_list* ifields = itdt->struct_type()->fields();
7465 Expression_list* ivals = new Expression_list();
7466 ivals->reserve(2);
7468 Struct_field_list::const_iterator pif = ifields->begin();
7469 go_assert(pif->is_field_name("commonType"));
7470 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
7471 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
7472 true));
7474 ++pif;
7475 go_assert(pif->is_field_name("methods"));
7477 Expression_list* methods = new Expression_list();
7478 if (this->all_methods_ != NULL)
7480 Type* elemtype = pif->type()->array_type()->element_type();
7482 methods->reserve(this->all_methods_->size());
7483 for (Typed_identifier_list::const_iterator pm =
7484 this->all_methods_->begin();
7485 pm != this->all_methods_->end();
7486 ++pm)
7488 const Struct_field_list* mfields = elemtype->struct_type()->fields();
7490 Expression_list* mvals = new Expression_list();
7491 mvals->reserve(3);
7493 Struct_field_list::const_iterator pmf = mfields->begin();
7494 go_assert(pmf->is_field_name("name"));
7495 std::string s = Gogo::unpack_hidden_name(pm->name());
7496 Expression* e = Expression::make_string(s, bloc);
7497 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7499 ++pmf;
7500 go_assert(pmf->is_field_name("pkgPath"));
7501 if (!Gogo::is_hidden_name(pm->name()))
7502 mvals->push_back(Expression::make_nil(bloc));
7503 else
7505 s = Gogo::hidden_name_pkgpath(pm->name());
7506 e = Expression::make_string(s, bloc);
7507 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7510 ++pmf;
7511 go_assert(pmf->is_field_name("typ"));
7512 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
7514 ++pmf;
7515 go_assert(pmf == mfields->end());
7517 e = Expression::make_struct_composite_literal(elemtype, mvals,
7518 bloc);
7519 methods->push_back(e);
7523 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
7524 methods, bloc));
7526 ++pif;
7527 go_assert(pif == ifields->end());
7529 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
7532 // Reflection string.
7534 void
7535 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
7537 ret->append("interface {");
7538 const Typed_identifier_list* methods = this->parse_methods_;
7539 if (methods != NULL)
7541 ret->push_back(' ');
7542 for (Typed_identifier_list::const_iterator p = methods->begin();
7543 p != methods->end();
7544 ++p)
7546 if (p != methods->begin())
7547 ret->append("; ");
7548 if (p->name().empty())
7549 this->append_reflection(p->type(), gogo, ret);
7550 else
7552 if (!Gogo::is_hidden_name(p->name()))
7553 ret->append(p->name());
7554 else if (gogo->pkgpath_from_option())
7555 ret->append(p->name().substr(1));
7556 else
7558 // If no -fgo-pkgpath option, backward compatibility
7559 // for how this used to work before -fgo-pkgpath was
7560 // introduced.
7561 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
7562 ret->append(pkgpath.substr(pkgpath.find('.') + 1));
7563 ret->push_back('.');
7564 ret->append(Gogo::unpack_hidden_name(p->name()));
7566 std::string sub = p->type()->reflection(gogo);
7567 go_assert(sub.compare(0, 4, "func") == 0);
7568 sub = sub.substr(4);
7569 ret->append(sub);
7572 ret->push_back(' ');
7574 ret->append("}");
7577 // Mangled name.
7579 void
7580 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7582 go_assert(this->methods_are_finalized_);
7584 ret->push_back('I');
7586 const Typed_identifier_list* methods = this->all_methods_;
7587 if (methods != NULL && !this->seen_)
7589 this->seen_ = true;
7590 for (Typed_identifier_list::const_iterator p = methods->begin();
7591 p != methods->end();
7592 ++p)
7594 if (!p->name().empty())
7596 std::string n;
7597 if (!Gogo::is_hidden_name(p->name()))
7598 n = p->name();
7599 else
7601 n = ".";
7602 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
7603 n.append(Gogo::pkgpath_for_symbol(pkgpath));
7604 n.append(1, '.');
7605 n.append(Gogo::unpack_hidden_name(p->name()));
7607 char buf[20];
7608 snprintf(buf, sizeof buf, "%u_",
7609 static_cast<unsigned int>(n.length()));
7610 ret->append(buf);
7611 ret->append(n);
7613 this->append_mangled_name(p->type(), gogo, ret);
7615 this->seen_ = false;
7618 ret->push_back('e');
7621 // Export.
7623 void
7624 Interface_type::do_export(Export* exp) const
7626 exp->write_c_string("interface { ");
7628 const Typed_identifier_list* methods = this->parse_methods_;
7629 if (methods != NULL)
7631 for (Typed_identifier_list::const_iterator pm = methods->begin();
7632 pm != methods->end();
7633 ++pm)
7635 if (pm->name().empty())
7637 exp->write_c_string("? ");
7638 exp->write_type(pm->type());
7640 else
7642 exp->write_string(pm->name());
7643 exp->write_c_string(" (");
7645 const Function_type* fntype = pm->type()->function_type();
7647 bool first = true;
7648 const Typed_identifier_list* parameters = fntype->parameters();
7649 if (parameters != NULL)
7651 bool is_varargs = fntype->is_varargs();
7652 for (Typed_identifier_list::const_iterator pp =
7653 parameters->begin();
7654 pp != parameters->end();
7655 ++pp)
7657 if (first)
7658 first = false;
7659 else
7660 exp->write_c_string(", ");
7661 exp->write_name(pp->name());
7662 exp->write_c_string(" ");
7663 if (!is_varargs || pp + 1 != parameters->end())
7664 exp->write_type(pp->type());
7665 else
7667 exp->write_c_string("...");
7668 Type *pptype = pp->type();
7669 exp->write_type(pptype->array_type()->element_type());
7674 exp->write_c_string(")");
7676 const Typed_identifier_list* results = fntype->results();
7677 if (results != NULL)
7679 exp->write_c_string(" ");
7680 if (results->size() == 1 && results->begin()->name().empty())
7681 exp->write_type(results->begin()->type());
7682 else
7684 first = true;
7685 exp->write_c_string("(");
7686 for (Typed_identifier_list::const_iterator p =
7687 results->begin();
7688 p != results->end();
7689 ++p)
7691 if (first)
7692 first = false;
7693 else
7694 exp->write_c_string(", ");
7695 exp->write_name(p->name());
7696 exp->write_c_string(" ");
7697 exp->write_type(p->type());
7699 exp->write_c_string(")");
7704 exp->write_c_string("; ");
7708 exp->write_c_string("}");
7711 // Import an interface type.
7713 Interface_type*
7714 Interface_type::do_import(Import* imp)
7716 imp->require_c_string("interface { ");
7718 Typed_identifier_list* methods = new Typed_identifier_list;
7719 while (imp->peek_char() != '}')
7721 std::string name = imp->read_identifier();
7723 if (name == "?")
7725 imp->require_c_string(" ");
7726 Type* t = imp->read_type();
7727 methods->push_back(Typed_identifier("", t, imp->location()));
7728 imp->require_c_string("; ");
7729 continue;
7732 imp->require_c_string(" (");
7734 Typed_identifier_list* parameters;
7735 bool is_varargs = false;
7736 if (imp->peek_char() == ')')
7737 parameters = NULL;
7738 else
7740 parameters = new Typed_identifier_list;
7741 while (true)
7743 std::string name = imp->read_name();
7744 imp->require_c_string(" ");
7746 if (imp->match_c_string("..."))
7748 imp->advance(3);
7749 is_varargs = true;
7752 Type* ptype = imp->read_type();
7753 if (is_varargs)
7754 ptype = Type::make_array_type(ptype, NULL);
7755 parameters->push_back(Typed_identifier(name, ptype,
7756 imp->location()));
7757 if (imp->peek_char() != ',')
7758 break;
7759 go_assert(!is_varargs);
7760 imp->require_c_string(", ");
7763 imp->require_c_string(")");
7765 Typed_identifier_list* results;
7766 if (imp->peek_char() != ' ')
7767 results = NULL;
7768 else
7770 results = new Typed_identifier_list;
7771 imp->advance(1);
7772 if (imp->peek_char() != '(')
7774 Type* rtype = imp->read_type();
7775 results->push_back(Typed_identifier("", rtype, imp->location()));
7777 else
7779 imp->advance(1);
7780 while (true)
7782 std::string name = imp->read_name();
7783 imp->require_c_string(" ");
7784 Type* rtype = imp->read_type();
7785 results->push_back(Typed_identifier(name, rtype,
7786 imp->location()));
7787 if (imp->peek_char() != ',')
7788 break;
7789 imp->require_c_string(", ");
7791 imp->require_c_string(")");
7795 Function_type* fntype = Type::make_function_type(NULL, parameters,
7796 results,
7797 imp->location());
7798 if (is_varargs)
7799 fntype->set_is_varargs();
7800 methods->push_back(Typed_identifier(name, fntype, imp->location()));
7802 imp->require_c_string("; ");
7805 imp->require_c_string("}");
7807 if (methods->empty())
7809 delete methods;
7810 methods = NULL;
7813 return Type::make_interface_type(methods, imp->location());
7816 // Make an interface type.
7818 Interface_type*
7819 Type::make_interface_type(Typed_identifier_list* methods,
7820 Location location)
7822 return new Interface_type(methods, location);
7825 // Make an empty interface type.
7827 Interface_type*
7828 Type::make_empty_interface_type(Location location)
7830 Interface_type* ret = new Interface_type(NULL, location);
7831 ret->finalize_methods();
7832 return ret;
7835 // Class Method.
7837 // Bind a method to an object.
7839 Expression*
7840 Method::bind_method(Expression* expr, Location location) const
7842 if (this->stub_ == NULL)
7844 // When there is no stub object, the binding is determined by
7845 // the child class.
7846 return this->do_bind_method(expr, location);
7848 return Expression::make_bound_method(expr, this, this->stub_, location);
7851 // Return the named object associated with a method. This may only be
7852 // called after methods are finalized.
7854 Named_object*
7855 Method::named_object() const
7857 if (this->stub_ != NULL)
7858 return this->stub_;
7859 return this->do_named_object();
7862 // Class Named_method.
7864 // The type of the method.
7866 Function_type*
7867 Named_method::do_type() const
7869 if (this->named_object_->is_function())
7870 return this->named_object_->func_value()->type();
7871 else if (this->named_object_->is_function_declaration())
7872 return this->named_object_->func_declaration_value()->type();
7873 else
7874 go_unreachable();
7877 // Return the location of the method receiver.
7879 Location
7880 Named_method::do_receiver_location() const
7882 return this->do_type()->receiver()->location();
7885 // Bind a method to an object.
7887 Expression*
7888 Named_method::do_bind_method(Expression* expr, Location location) const
7890 Named_object* no = this->named_object_;
7891 Bound_method_expression* bme = Expression::make_bound_method(expr, this,
7892 no, location);
7893 // If this is not a local method, and it does not use a stub, then
7894 // the real method expects a different type. We need to cast the
7895 // first argument.
7896 if (this->depth() > 0 && !this->needs_stub_method())
7898 Function_type* ftype = this->do_type();
7899 go_assert(ftype->is_method());
7900 Type* frtype = ftype->receiver()->type();
7901 bme->set_first_argument_type(frtype);
7903 return bme;
7906 // Return whether this method should not participate in interfaces.
7908 bool
7909 Named_method::do_nointerface() const
7911 Named_object* no = this->named_object_;
7912 return no->is_function() && no->func_value()->nointerface();
7915 // Class Interface_method.
7917 // Bind a method to an object.
7919 Expression*
7920 Interface_method::do_bind_method(Expression* expr,
7921 Location location) const
7923 return Expression::make_interface_field_reference(expr, this->name_,
7924 location);
7927 // Class Methods.
7929 // Insert a new method. Return true if it was inserted, false
7930 // otherwise.
7932 bool
7933 Methods::insert(const std::string& name, Method* m)
7935 std::pair<Method_map::iterator, bool> ins =
7936 this->methods_.insert(std::make_pair(name, m));
7937 if (ins.second)
7938 return true;
7939 else
7941 Method* old_method = ins.first->second;
7942 if (m->depth() < old_method->depth())
7944 delete old_method;
7945 ins.first->second = m;
7946 return true;
7948 else
7950 if (m->depth() == old_method->depth())
7951 old_method->set_is_ambiguous();
7952 return false;
7957 // Return the number of unambiguous methods.
7959 size_t
7960 Methods::count() const
7962 size_t ret = 0;
7963 for (Method_map::const_iterator p = this->methods_.begin();
7964 p != this->methods_.end();
7965 ++p)
7966 if (!p->second->is_ambiguous())
7967 ++ret;
7968 return ret;
7971 // Class Named_type.
7973 // Return the name of the type.
7975 const std::string&
7976 Named_type::name() const
7978 return this->named_object_->name();
7981 // Return the name of the type to use in an error message.
7983 std::string
7984 Named_type::message_name() const
7986 return this->named_object_->message_name();
7989 // Whether this is an alias. There are currently only two aliases so
7990 // we just recognize them by name.
7992 bool
7993 Named_type::is_alias() const
7995 if (!this->is_builtin())
7996 return false;
7997 const std::string& name(this->name());
7998 return name == "byte" || name == "rune";
8001 // Return the base type for this type. We have to be careful about
8002 // circular type definitions, which are invalid but may be seen here.
8004 Type*
8005 Named_type::named_base()
8007 if (this->seen_)
8008 return this;
8009 this->seen_ = true;
8010 Type* ret = this->type_->base();
8011 this->seen_ = false;
8012 return ret;
8015 const Type*
8016 Named_type::named_base() const
8018 if (this->seen_)
8019 return this;
8020 this->seen_ = true;
8021 const Type* ret = this->type_->base();
8022 this->seen_ = false;
8023 return ret;
8026 // Return whether this is an error type. We have to be careful about
8027 // circular type definitions, which are invalid but may be seen here.
8029 bool
8030 Named_type::is_named_error_type() const
8032 if (this->seen_)
8033 return false;
8034 this->seen_ = true;
8035 bool ret = this->type_->is_error_type();
8036 this->seen_ = false;
8037 return ret;
8040 // Whether this type is comparable. We have to be careful about
8041 // circular type definitions.
8043 bool
8044 Named_type::named_type_is_comparable(std::string* reason) const
8046 if (this->seen_)
8047 return false;
8048 this->seen_ = true;
8049 bool ret = Type::are_compatible_for_comparison(true, this->type_,
8050 this->type_, reason);
8051 this->seen_ = false;
8052 return ret;
8055 // Add a method to this type.
8057 Named_object*
8058 Named_type::add_method(const std::string& name, Function* function)
8060 if (this->local_methods_ == NULL)
8061 this->local_methods_ = new Bindings(NULL);
8062 return this->local_methods_->add_function(name, NULL, function);
8065 // Add a method declaration to this type.
8067 Named_object*
8068 Named_type::add_method_declaration(const std::string& name, Package* package,
8069 Function_type* type,
8070 Location location)
8072 if (this->local_methods_ == NULL)
8073 this->local_methods_ = new Bindings(NULL);
8074 return this->local_methods_->add_function_declaration(name, package, type,
8075 location);
8078 // Add an existing method to this type.
8080 void
8081 Named_type::add_existing_method(Named_object* no)
8083 if (this->local_methods_ == NULL)
8084 this->local_methods_ = new Bindings(NULL);
8085 this->local_methods_->add_named_object(no);
8088 // Look for a local method NAME, and returns its named object, or NULL
8089 // if not there.
8091 Named_object*
8092 Named_type::find_local_method(const std::string& name) const
8094 if (this->local_methods_ == NULL)
8095 return NULL;
8096 return this->local_methods_->lookup(name);
8099 // Return whether NAME is an unexported field or method, for better
8100 // error reporting.
8102 bool
8103 Named_type::is_unexported_local_method(Gogo* gogo,
8104 const std::string& name) const
8106 Bindings* methods = this->local_methods_;
8107 if (methods != NULL)
8109 for (Bindings::const_declarations_iterator p =
8110 methods->begin_declarations();
8111 p != methods->end_declarations();
8112 ++p)
8114 if (Gogo::is_hidden_name(p->first)
8115 && name == Gogo::unpack_hidden_name(p->first)
8116 && gogo->pack_hidden_name(name, false) != p->first)
8117 return true;
8120 return false;
8123 // Build the complete list of methods for this type, which means
8124 // recursively including all methods for anonymous fields. Create all
8125 // stub methods.
8127 void
8128 Named_type::finalize_methods(Gogo* gogo)
8130 if (this->all_methods_ != NULL)
8131 return;
8133 if (this->local_methods_ != NULL
8134 && (this->points_to() != NULL || this->interface_type() != NULL))
8136 const Bindings* lm = this->local_methods_;
8137 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
8138 p != lm->end_declarations();
8139 ++p)
8140 error_at(p->second->location(),
8141 "invalid pointer or interface receiver type");
8142 delete this->local_methods_;
8143 this->local_methods_ = NULL;
8144 return;
8147 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
8150 // Return the method NAME, or NULL if there isn't one or if it is
8151 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
8152 // ambiguous.
8154 Method*
8155 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
8157 return Type::method_function(this->all_methods_, name, is_ambiguous);
8160 // Return a pointer to the interface method table for this type for
8161 // the interface INTERFACE. IS_POINTER is true if this is for a
8162 // pointer to THIS.
8164 Expression*
8165 Named_type::interface_method_table(Interface_type* interface, bool is_pointer)
8167 return Type::interface_method_table(this, interface, is_pointer,
8168 &this->interface_method_tables_,
8169 &this->pointer_interface_method_tables_);
8172 // Return whether a named type has any hidden fields.
8174 bool
8175 Named_type::named_type_has_hidden_fields(std::string* reason) const
8177 if (this->seen_)
8178 return false;
8179 this->seen_ = true;
8180 bool ret = this->type_->has_hidden_fields(this, reason);
8181 this->seen_ = false;
8182 return ret;
8185 // Look for a use of a complete type within another type. This is
8186 // used to check that we don't try to use a type within itself.
8188 class Find_type_use : public Traverse
8190 public:
8191 Find_type_use(Named_type* find_type)
8192 : Traverse(traverse_types),
8193 find_type_(find_type), found_(false)
8196 // Whether we found the type.
8197 bool
8198 found() const
8199 { return this->found_; }
8201 protected:
8203 type(Type*);
8205 private:
8206 // The type we are looking for.
8207 Named_type* find_type_;
8208 // Whether we found the type.
8209 bool found_;
8212 // Check for FIND_TYPE in TYPE.
8215 Find_type_use::type(Type* type)
8217 if (type->named_type() != NULL && this->find_type_ == type->named_type())
8219 this->found_ = true;
8220 return TRAVERSE_EXIT;
8223 // It's OK if we see a reference to the type in any type which is
8224 // essentially a pointer: a pointer, a slice, a function, a map, or
8225 // a channel.
8226 if (type->points_to() != NULL
8227 || type->is_slice_type()
8228 || type->function_type() != NULL
8229 || type->map_type() != NULL
8230 || type->channel_type() != NULL)
8231 return TRAVERSE_SKIP_COMPONENTS;
8233 // For an interface, a reference to the type in a method type should
8234 // be ignored, but we have to consider direct inheritance. When
8235 // this is called, there may be cases of direct inheritance
8236 // represented as a method with no name.
8237 if (type->interface_type() != NULL)
8239 const Typed_identifier_list* methods = type->interface_type()->methods();
8240 if (methods != NULL)
8242 for (Typed_identifier_list::const_iterator p = methods->begin();
8243 p != methods->end();
8244 ++p)
8246 if (p->name().empty())
8248 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
8249 return TRAVERSE_EXIT;
8253 return TRAVERSE_SKIP_COMPONENTS;
8256 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
8257 // to convert TYPE to the backend representation before we convert
8258 // FIND_TYPE_.
8259 if (type->named_type() != NULL)
8261 switch (type->base()->classification())
8263 case Type::TYPE_ERROR:
8264 case Type::TYPE_BOOLEAN:
8265 case Type::TYPE_INTEGER:
8266 case Type::TYPE_FLOAT:
8267 case Type::TYPE_COMPLEX:
8268 case Type::TYPE_STRING:
8269 case Type::TYPE_NIL:
8270 break;
8272 case Type::TYPE_ARRAY:
8273 case Type::TYPE_STRUCT:
8274 this->find_type_->add_dependency(type->named_type());
8275 break;
8277 case Type::TYPE_NAMED:
8278 case Type::TYPE_FORWARD:
8279 go_assert(saw_errors());
8280 break;
8282 case Type::TYPE_VOID:
8283 case Type::TYPE_SINK:
8284 case Type::TYPE_FUNCTION:
8285 case Type::TYPE_POINTER:
8286 case Type::TYPE_CALL_MULTIPLE_RESULT:
8287 case Type::TYPE_MAP:
8288 case Type::TYPE_CHANNEL:
8289 case Type::TYPE_INTERFACE:
8290 default:
8291 go_unreachable();
8295 return TRAVERSE_CONTINUE;
8298 // Verify that a named type does not refer to itself.
8300 bool
8301 Named_type::do_verify()
8303 if (this->is_verified_)
8304 return true;
8305 this->is_verified_ = true;
8307 Find_type_use find(this);
8308 Type::traverse(this->type_, &find);
8309 if (find.found())
8311 error_at(this->location_, "invalid recursive type %qs",
8312 this->message_name().c_str());
8313 this->is_error_ = true;
8314 return false;
8317 // Check whether any of the local methods overloads an existing
8318 // struct field or interface method. We don't need to check the
8319 // list of methods against itself: that is handled by the Bindings
8320 // code.
8321 if (this->local_methods_ != NULL)
8323 Struct_type* st = this->type_->struct_type();
8324 if (st != NULL)
8326 for (Bindings::const_declarations_iterator p =
8327 this->local_methods_->begin_declarations();
8328 p != this->local_methods_->end_declarations();
8329 ++p)
8331 const std::string& name(p->first);
8332 if (st != NULL && st->find_local_field(name, NULL) != NULL)
8334 error_at(p->second->location(),
8335 "method %qs redeclares struct field name",
8336 Gogo::message_name(name).c_str());
8342 return true;
8345 // Return whether this type is or contains a pointer.
8347 bool
8348 Named_type::do_has_pointer() const
8350 if (this->seen_)
8351 return false;
8352 this->seen_ = true;
8353 bool ret = this->type_->has_pointer();
8354 this->seen_ = false;
8355 return ret;
8358 // Return whether comparisons for this type can use the identity
8359 // function.
8361 bool
8362 Named_type::do_compare_is_identity(Gogo* gogo)
8364 // We don't use this->seen_ here because compare_is_identity may
8365 // call base() later, and that will mess up if seen_ is set here.
8366 if (this->seen_in_compare_is_identity_)
8367 return false;
8368 this->seen_in_compare_is_identity_ = true;
8369 bool ret = this->type_->compare_is_identity(gogo);
8370 this->seen_in_compare_is_identity_ = false;
8371 return ret;
8374 // Return a hash code. This is used for method lookup. We simply
8375 // hash on the name itself.
8377 unsigned int
8378 Named_type::do_hash_for_method(Gogo* gogo) const
8380 if (this->is_alias())
8381 return this->type_->named_type()->do_hash_for_method(gogo);
8383 const std::string& name(this->named_object()->name());
8384 unsigned int ret = Type::hash_string(name, 0);
8386 // GOGO will be NULL here when called from Type_hash_identical.
8387 // That is OK because that is only used for internal hash tables
8388 // where we are going to be comparing named types for equality. In
8389 // other cases, which are cases where the runtime is going to
8390 // compare hash codes to see if the types are the same, we need to
8391 // include the pkgpath in the hash.
8392 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
8394 const Package* package = this->named_object()->package();
8395 if (package == NULL)
8396 ret = Type::hash_string(gogo->pkgpath(), ret);
8397 else
8398 ret = Type::hash_string(package->pkgpath(), ret);
8401 return ret;
8404 // Convert a named type to the backend representation. In order to
8405 // get dependencies right, we fill in a dummy structure for this type,
8406 // then convert all the dependencies, then complete this type. When
8407 // this function is complete, the size of the type is known.
8409 void
8410 Named_type::convert(Gogo* gogo)
8412 if (this->is_error_ || this->is_converted_)
8413 return;
8415 this->create_placeholder(gogo);
8417 // If we are called to turn unsafe.Sizeof into a constant, we may
8418 // not have verified the type yet. We have to make sure it is
8419 // verified, since that sets the list of dependencies.
8420 this->verify();
8422 // Convert all the dependencies. If they refer indirectly back to
8423 // this type, they will pick up the intermediate representation we just
8424 // created.
8425 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
8426 p != this->dependencies_.end();
8427 ++p)
8428 (*p)->convert(gogo);
8430 // Complete this type.
8431 Btype* bt = this->named_btype_;
8432 Type* base = this->type_->base();
8433 switch (base->classification())
8435 case TYPE_VOID:
8436 case TYPE_BOOLEAN:
8437 case TYPE_INTEGER:
8438 case TYPE_FLOAT:
8439 case TYPE_COMPLEX:
8440 case TYPE_STRING:
8441 case TYPE_NIL:
8442 break;
8444 case TYPE_MAP:
8445 case TYPE_CHANNEL:
8446 break;
8448 case TYPE_FUNCTION:
8449 case TYPE_POINTER:
8450 // The size of these types is already correct. We don't worry
8451 // about filling them in until later, when we also track
8452 // circular references.
8453 break;
8455 case TYPE_STRUCT:
8457 std::vector<Backend::Btyped_identifier> bfields;
8458 get_backend_struct_fields(gogo, base->struct_type()->fields(),
8459 true, &bfields);
8460 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8461 bt = gogo->backend()->error_type();
8463 break;
8465 case TYPE_ARRAY:
8466 // Slice types were completed in create_placeholder.
8467 if (!base->is_slice_type())
8469 Btype* bet = base->array_type()->get_backend_element(gogo, true);
8470 Bexpression* blen = base->array_type()->get_backend_length(gogo);
8471 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
8472 bt = gogo->backend()->error_type();
8474 break;
8476 case TYPE_INTERFACE:
8477 // Interface types were completed in create_placeholder.
8478 break;
8480 case TYPE_ERROR:
8481 return;
8483 default:
8484 case TYPE_SINK:
8485 case TYPE_CALL_MULTIPLE_RESULT:
8486 case TYPE_NAMED:
8487 case TYPE_FORWARD:
8488 go_unreachable();
8491 this->named_btype_ = bt;
8492 this->is_converted_ = true;
8493 this->is_placeholder_ = false;
8496 // Create the placeholder for a named type. This is the first step in
8497 // converting to the backend representation.
8499 void
8500 Named_type::create_placeholder(Gogo* gogo)
8502 if (this->is_error_)
8503 this->named_btype_ = gogo->backend()->error_type();
8505 if (this->named_btype_ != NULL)
8506 return;
8508 // Create the structure for this type. Note that because we call
8509 // base() here, we don't attempt to represent a named type defined
8510 // as another named type. Instead both named types will point to
8511 // different base representations.
8512 Type* base = this->type_->base();
8513 Btype* bt;
8514 bool set_name = true;
8515 switch (base->classification())
8517 case TYPE_ERROR:
8518 this->is_error_ = true;
8519 this->named_btype_ = gogo->backend()->error_type();
8520 return;
8522 case TYPE_VOID:
8523 case TYPE_BOOLEAN:
8524 case TYPE_INTEGER:
8525 case TYPE_FLOAT:
8526 case TYPE_COMPLEX:
8527 case TYPE_STRING:
8528 case TYPE_NIL:
8529 // These are simple basic types, we can just create them
8530 // directly.
8531 bt = Type::get_named_base_btype(gogo, base);
8532 break;
8534 case TYPE_MAP:
8535 case TYPE_CHANNEL:
8536 // All maps and channels have the same backend representation.
8537 bt = Type::get_named_base_btype(gogo, base);
8538 break;
8540 case TYPE_FUNCTION:
8541 case TYPE_POINTER:
8543 bool for_function = base->classification() == TYPE_FUNCTION;
8544 bt = gogo->backend()->placeholder_pointer_type(this->name(),
8545 this->location_,
8546 for_function);
8547 set_name = false;
8549 break;
8551 case TYPE_STRUCT:
8552 bt = gogo->backend()->placeholder_struct_type(this->name(),
8553 this->location_);
8554 this->is_placeholder_ = true;
8555 set_name = false;
8556 break;
8558 case TYPE_ARRAY:
8559 if (base->is_slice_type())
8560 bt = gogo->backend()->placeholder_struct_type(this->name(),
8561 this->location_);
8562 else
8564 bt = gogo->backend()->placeholder_array_type(this->name(),
8565 this->location_);
8566 this->is_placeholder_ = true;
8568 set_name = false;
8569 break;
8571 case TYPE_INTERFACE:
8572 if (base->interface_type()->is_empty())
8573 bt = Interface_type::get_backend_empty_interface_type(gogo);
8574 else
8576 bt = gogo->backend()->placeholder_struct_type(this->name(),
8577 this->location_);
8578 set_name = false;
8580 break;
8582 default:
8583 case TYPE_SINK:
8584 case TYPE_CALL_MULTIPLE_RESULT:
8585 case TYPE_NAMED:
8586 case TYPE_FORWARD:
8587 go_unreachable();
8590 if (set_name)
8591 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
8593 this->named_btype_ = bt;
8595 if (base->is_slice_type())
8597 // We do not record slices as dependencies of other types,
8598 // because we can fill them in completely here with the final
8599 // size.
8600 std::vector<Backend::Btyped_identifier> bfields;
8601 get_backend_slice_fields(gogo, base->array_type(), true, &bfields);
8602 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8603 this->named_btype_ = gogo->backend()->error_type();
8605 else if (base->interface_type() != NULL
8606 && !base->interface_type()->is_empty())
8608 // We do not record interfaces as dependencies of other types,
8609 // because we can fill them in completely here with the final
8610 // size.
8611 std::vector<Backend::Btyped_identifier> bfields;
8612 get_backend_interface_fields(gogo, base->interface_type(), true,
8613 &bfields);
8614 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8615 this->named_btype_ = gogo->backend()->error_type();
8619 // Get the backend representation for a named type.
8621 Btype*
8622 Named_type::do_get_backend(Gogo* gogo)
8624 if (this->is_error_)
8625 return gogo->backend()->error_type();
8627 Btype* bt = this->named_btype_;
8629 if (!gogo->named_types_are_converted())
8631 // We have not completed converting named types. NAMED_BTYPE_
8632 // is a placeholder and we shouldn't do anything further.
8633 if (bt != NULL)
8634 return bt;
8636 // We don't build dependencies for types whose sizes do not
8637 // change or are not relevant, so we may see them here while
8638 // converting types.
8639 this->create_placeholder(gogo);
8640 bt = this->named_btype_;
8641 go_assert(bt != NULL);
8642 return bt;
8645 // We are not converting types. This should only be called if the
8646 // type has already been converted.
8647 if (!this->is_converted_)
8649 go_assert(saw_errors());
8650 return gogo->backend()->error_type();
8653 go_assert(bt != NULL);
8655 // Complete the backend representation.
8656 Type* base = this->type_->base();
8657 Btype* bt1;
8658 switch (base->classification())
8660 case TYPE_ERROR:
8661 return gogo->backend()->error_type();
8663 case TYPE_VOID:
8664 case TYPE_BOOLEAN:
8665 case TYPE_INTEGER:
8666 case TYPE_FLOAT:
8667 case TYPE_COMPLEX:
8668 case TYPE_STRING:
8669 case TYPE_NIL:
8670 case TYPE_MAP:
8671 case TYPE_CHANNEL:
8672 return bt;
8674 case TYPE_STRUCT:
8675 if (!this->seen_in_get_backend_)
8677 this->seen_in_get_backend_ = true;
8678 base->struct_type()->finish_backend_fields(gogo);
8679 this->seen_in_get_backend_ = false;
8681 return bt;
8683 case TYPE_ARRAY:
8684 if (!this->seen_in_get_backend_)
8686 this->seen_in_get_backend_ = true;
8687 base->array_type()->finish_backend_element(gogo);
8688 this->seen_in_get_backend_ = false;
8690 return bt;
8692 case TYPE_INTERFACE:
8693 if (!this->seen_in_get_backend_)
8695 this->seen_in_get_backend_ = true;
8696 base->interface_type()->finish_backend_methods(gogo);
8697 this->seen_in_get_backend_ = false;
8699 return bt;
8701 case TYPE_FUNCTION:
8702 // Don't build a circular data structure. GENERIC can't handle
8703 // it.
8704 if (this->seen_in_get_backend_)
8706 this->is_circular_ = true;
8707 return gogo->backend()->circular_pointer_type(bt, false);
8709 this->seen_in_get_backend_ = true;
8710 bt1 = Type::get_named_base_btype(gogo, base);
8711 this->seen_in_get_backend_ = false;
8712 if (this->is_circular_)
8713 bt1 = gogo->backend()->circular_pointer_type(bt, false);
8714 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
8715 bt = gogo->backend()->error_type();
8716 return bt;
8718 case TYPE_POINTER:
8719 // Don't build a circular data structure. GENERIC can't handle
8720 // it.
8721 if (this->seen_in_get_backend_)
8723 this->is_circular_ = true;
8724 return gogo->backend()->circular_pointer_type(bt, false);
8726 this->seen_in_get_backend_ = true;
8727 bt1 = Type::get_named_base_btype(gogo, base);
8728 this->seen_in_get_backend_ = false;
8729 if (this->is_circular_)
8730 bt1 = gogo->backend()->circular_pointer_type(bt, false);
8731 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
8732 bt = gogo->backend()->error_type();
8733 return bt;
8735 default:
8736 case TYPE_SINK:
8737 case TYPE_CALL_MULTIPLE_RESULT:
8738 case TYPE_NAMED:
8739 case TYPE_FORWARD:
8740 go_unreachable();
8743 go_unreachable();
8746 // Build a type descriptor for a named type.
8748 Expression*
8749 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8751 if (name == NULL && this->is_alias())
8752 return this->type_->type_descriptor(gogo, this->type_);
8754 // If NAME is not NULL, then we don't really want the type
8755 // descriptor for this type; we want the descriptor for the
8756 // underlying type, giving it the name NAME.
8757 return this->named_type_descriptor(gogo, this->type_,
8758 name == NULL ? this : name);
8761 // Add to the reflection string. This is used mostly for the name of
8762 // the type used in a type descriptor, not for actual reflection
8763 // strings.
8765 void
8766 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
8768 if (this->is_alias())
8770 this->append_reflection(this->type_, gogo, ret);
8771 return;
8773 if (!this->is_builtin())
8775 // We handle -fgo-prefix and -fgo-pkgpath differently here for
8776 // compatibility with how the compiler worked before
8777 // -fgo-pkgpath was introduced. When -fgo-pkgpath is specified,
8778 // we use it to make a unique reflection string, so that the
8779 // type canonicalization in the reflect package will work. In
8780 // order to be compatible with the gc compiler, we put tabs into
8781 // the package path, so that the reflect methods can discard it.
8782 const Package* package = this->named_object_->package();
8783 if (gogo->pkgpath_from_option())
8785 ret->push_back('\t');
8786 ret->append(package != NULL
8787 ? package->pkgpath_symbol()
8788 : gogo->pkgpath_symbol());
8789 ret->push_back('\t');
8791 ret->append(package != NULL
8792 ? package->package_name()
8793 : gogo->package_name());
8794 ret->push_back('.');
8796 if (this->in_function_ != NULL)
8798 ret->push_back('\t');
8799 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
8800 ret->push_back('$');
8801 if (this->in_function_index_ > 0)
8803 char buf[30];
8804 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8805 ret->append(buf);
8806 ret->push_back('$');
8808 ret->push_back('\t');
8810 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
8813 // Get the mangled name.
8815 void
8816 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8818 if (this->is_alias())
8820 this->append_mangled_name(this->type_, gogo, ret);
8821 return;
8823 Named_object* no = this->named_object_;
8824 std::string name;
8825 if (this->is_builtin())
8826 go_assert(this->in_function_ == NULL);
8827 else
8829 const std::string& pkgpath(no->package() == NULL
8830 ? gogo->pkgpath_symbol()
8831 : no->package()->pkgpath_symbol());
8832 name = pkgpath;
8833 name.append(1, '.');
8834 if (this->in_function_ != NULL)
8836 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
8837 name.append(1, '$');
8838 if (this->in_function_index_ > 0)
8840 char buf[30];
8841 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8842 name.append(buf);
8843 name.append(1, '$');
8847 name.append(Gogo::unpack_hidden_name(no->name()));
8848 char buf[20];
8849 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
8850 ret->append(buf);
8851 ret->append(name);
8854 // Export the type. This is called to export a global type.
8856 void
8857 Named_type::export_named_type(Export* exp, const std::string&) const
8859 // We don't need to write the name of the type here, because it will
8860 // be written by Export::write_type anyhow.
8861 exp->write_c_string("type ");
8862 exp->write_type(this);
8863 exp->write_c_string(";\n");
8866 // Import a named type.
8868 void
8869 Named_type::import_named_type(Import* imp, Named_type** ptype)
8871 imp->require_c_string("type ");
8872 Type *type = imp->read_type();
8873 *ptype = type->named_type();
8874 go_assert(*ptype != NULL);
8875 imp->require_c_string(";\n");
8878 // Export the type when it is referenced by another type. In this
8879 // case Export::export_type will already have issued the name.
8881 void
8882 Named_type::do_export(Export* exp) const
8884 exp->write_type(this->type_);
8886 // To save space, we only export the methods directly attached to
8887 // this type.
8888 Bindings* methods = this->local_methods_;
8889 if (methods == NULL)
8890 return;
8892 exp->write_c_string("\n");
8893 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
8894 p != methods->end_definitions();
8895 ++p)
8897 exp->write_c_string(" ");
8898 (*p)->export_named_object(exp);
8901 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
8902 p != methods->end_declarations();
8903 ++p)
8905 if (p->second->is_function_declaration())
8907 exp->write_c_string(" ");
8908 p->second->export_named_object(exp);
8913 // Make a named type.
8915 Named_type*
8916 Type::make_named_type(Named_object* named_object, Type* type,
8917 Location location)
8919 return new Named_type(named_object, type, location);
8922 // Finalize the methods for TYPE. It will be a named type or a struct
8923 // type. This sets *ALL_METHODS to the list of methods, and builds
8924 // all required stubs.
8926 void
8927 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
8928 Methods** all_methods)
8930 *all_methods = NULL;
8931 std::vector<const Named_type*> seen;
8932 Type::add_methods_for_type(type, NULL, 0, false, false, &seen, all_methods);
8933 Type::build_stub_methods(gogo, type, *all_methods, location);
8936 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8937 // build up the struct field indexes as we go. DEPTH is the depth of
8938 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8939 // adding these methods for an anonymous field with pointer type.
8940 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8941 // calls the real method. TYPES_SEEN is used to avoid infinite
8942 // recursion.
8944 void
8945 Type::add_methods_for_type(const Type* type,
8946 const Method::Field_indexes* field_indexes,
8947 unsigned int depth,
8948 bool is_embedded_pointer,
8949 bool needs_stub_method,
8950 std::vector<const Named_type*>* seen,
8951 Methods** methods)
8953 // Pointer types may not have methods.
8954 if (type->points_to() != NULL)
8955 return;
8957 const Named_type* nt = type->named_type();
8958 if (nt != NULL)
8960 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8961 p != seen->end();
8962 ++p)
8964 if (*p == nt)
8965 return;
8968 seen->push_back(nt);
8970 Type::add_local_methods_for_type(nt, field_indexes, depth,
8971 is_embedded_pointer, needs_stub_method,
8972 methods);
8975 Type::add_embedded_methods_for_type(type, field_indexes, depth,
8976 is_embedded_pointer, needs_stub_method,
8977 seen, methods);
8979 // If we are called with depth > 0, then we are looking at an
8980 // anonymous field of a struct. If such a field has interface type,
8981 // then we need to add the interface methods. We don't want to add
8982 // them when depth == 0, because we will already handle them
8983 // following the usual rules for an interface type.
8984 if (depth > 0)
8985 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
8987 if (nt != NULL)
8988 seen->pop_back();
8991 // Add the local methods for the named type NT to *METHODS. The
8992 // parameters are as for add_methods_to_type.
8994 void
8995 Type::add_local_methods_for_type(const Named_type* nt,
8996 const Method::Field_indexes* field_indexes,
8997 unsigned int depth,
8998 bool is_embedded_pointer,
8999 bool needs_stub_method,
9000 Methods** methods)
9002 const Bindings* local_methods = nt->local_methods();
9003 if (local_methods == NULL)
9004 return;
9006 if (*methods == NULL)
9007 *methods = new Methods();
9009 for (Bindings::const_declarations_iterator p =
9010 local_methods->begin_declarations();
9011 p != local_methods->end_declarations();
9012 ++p)
9014 Named_object* no = p->second;
9015 bool is_value_method = (is_embedded_pointer
9016 || !Type::method_expects_pointer(no));
9017 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
9018 (needs_stub_method || depth > 0));
9019 if (!(*methods)->insert(no->name(), m))
9020 delete m;
9024 // Add the embedded methods for TYPE to *METHODS. These are the
9025 // methods attached to anonymous fields. The parameters are as for
9026 // add_methods_to_type.
9028 void
9029 Type::add_embedded_methods_for_type(const Type* type,
9030 const Method::Field_indexes* field_indexes,
9031 unsigned int depth,
9032 bool is_embedded_pointer,
9033 bool needs_stub_method,
9034 std::vector<const Named_type*>* seen,
9035 Methods** methods)
9037 // Look for anonymous fields in TYPE. TYPE has fields if it is a
9038 // struct.
9039 const Struct_type* st = type->struct_type();
9040 if (st == NULL)
9041 return;
9043 const Struct_field_list* fields = st->fields();
9044 if (fields == NULL)
9045 return;
9047 unsigned int i = 0;
9048 for (Struct_field_list::const_iterator pf = fields->begin();
9049 pf != fields->end();
9050 ++pf, ++i)
9052 if (!pf->is_anonymous())
9053 continue;
9055 Type* ftype = pf->type();
9056 bool is_pointer = false;
9057 if (ftype->points_to() != NULL)
9059 ftype = ftype->points_to();
9060 is_pointer = true;
9062 Named_type* fnt = ftype->named_type();
9063 if (fnt == NULL)
9065 // This is an error, but it will be diagnosed elsewhere.
9066 continue;
9069 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
9070 sub_field_indexes->next = field_indexes;
9071 sub_field_indexes->field_index = i;
9073 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
9074 (is_embedded_pointer || is_pointer),
9075 (needs_stub_method
9076 || is_pointer
9077 || i > 0),
9078 seen,
9079 methods);
9083 // If TYPE is an interface type, then add its method to *METHODS.
9084 // This is for interface methods attached to an anonymous field. The
9085 // parameters are as for add_methods_for_type.
9087 void
9088 Type::add_interface_methods_for_type(const Type* type,
9089 const Method::Field_indexes* field_indexes,
9090 unsigned int depth,
9091 Methods** methods)
9093 const Interface_type* it = type->interface_type();
9094 if (it == NULL)
9095 return;
9097 const Typed_identifier_list* imethods = it->methods();
9098 if (imethods == NULL)
9099 return;
9101 if (*methods == NULL)
9102 *methods = new Methods();
9104 for (Typed_identifier_list::const_iterator pm = imethods->begin();
9105 pm != imethods->end();
9106 ++pm)
9108 Function_type* fntype = pm->type()->function_type();
9109 if (fntype == NULL)
9111 // This is an error, but it should be reported elsewhere
9112 // when we look at the methods for IT.
9113 continue;
9115 go_assert(!fntype->is_method());
9116 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
9117 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
9118 field_indexes, depth);
9119 if (!(*methods)->insert(pm->name(), m))
9120 delete m;
9124 // Build stub methods for TYPE as needed. METHODS is the set of
9125 // methods for the type. A stub method may be needed when a type
9126 // inherits a method from an anonymous field. When we need the
9127 // address of the method, as in a type descriptor, we need to build a
9128 // little stub which does the required field dereferences and jumps to
9129 // the real method. LOCATION is the location of the type definition.
9131 void
9132 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
9133 Location location)
9135 if (methods == NULL)
9136 return;
9137 for (Methods::const_iterator p = methods->begin();
9138 p != methods->end();
9139 ++p)
9141 Method* m = p->second;
9142 if (m->is_ambiguous() || !m->needs_stub_method())
9143 continue;
9145 const std::string& name(p->first);
9147 // Build a stub method.
9149 const Function_type* fntype = m->type();
9151 static unsigned int counter;
9152 char buf[100];
9153 snprintf(buf, sizeof buf, "$this%u", counter);
9154 ++counter;
9156 Type* receiver_type = const_cast<Type*>(type);
9157 if (!m->is_value_method())
9158 receiver_type = Type::make_pointer_type(receiver_type);
9159 Location receiver_location = m->receiver_location();
9160 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
9161 receiver_location);
9163 const Typed_identifier_list* fnparams = fntype->parameters();
9164 Typed_identifier_list* stub_params;
9165 if (fnparams == NULL || fnparams->empty())
9166 stub_params = NULL;
9167 else
9169 // We give each stub parameter a unique name.
9170 stub_params = new Typed_identifier_list();
9171 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
9172 pp != fnparams->end();
9173 ++pp)
9175 char pbuf[100];
9176 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
9177 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
9178 pp->location()));
9179 ++counter;
9183 const Typed_identifier_list* fnresults = fntype->results();
9184 Typed_identifier_list* stub_results;
9185 if (fnresults == NULL || fnresults->empty())
9186 stub_results = NULL;
9187 else
9189 // We create the result parameters without any names, since
9190 // we won't refer to them.
9191 stub_results = new Typed_identifier_list();
9192 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
9193 pr != fnresults->end();
9194 ++pr)
9195 stub_results->push_back(Typed_identifier("", pr->type(),
9196 pr->location()));
9199 Function_type* stub_type = Type::make_function_type(receiver,
9200 stub_params,
9201 stub_results,
9202 fntype->location());
9203 if (fntype->is_varargs())
9204 stub_type->set_is_varargs();
9206 // We only create the function in the package which creates the
9207 // type.
9208 const Package* package;
9209 if (type->named_type() == NULL)
9210 package = NULL;
9211 else
9212 package = type->named_type()->named_object()->package();
9213 Named_object* stub;
9214 if (package != NULL)
9215 stub = Named_object::make_function_declaration(name, package,
9216 stub_type, location);
9217 else
9219 stub = gogo->start_function(name, stub_type, false,
9220 fntype->location());
9221 Type::build_one_stub_method(gogo, m, buf, stub_params,
9222 fntype->is_varargs(), location);
9223 gogo->finish_function(fntype->location());
9225 if (type->named_type() == NULL && stub->is_function())
9226 stub->func_value()->set_is_unnamed_type_stub_method();
9227 if (m->nointerface() && stub->is_function())
9228 stub->func_value()->set_nointerface();
9231 m->set_stub_object(stub);
9235 // Build a stub method which adjusts the receiver as required to call
9236 // METHOD. RECEIVER_NAME is the name we used for the receiver.
9237 // PARAMS is the list of function parameters.
9239 void
9240 Type::build_one_stub_method(Gogo* gogo, Method* method,
9241 const char* receiver_name,
9242 const Typed_identifier_list* params,
9243 bool is_varargs,
9244 Location location)
9246 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
9247 go_assert(receiver_object != NULL);
9249 Expression* expr = Expression::make_var_reference(receiver_object, location);
9250 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
9251 if (expr->type()->points_to() == NULL)
9252 expr = Expression::make_unary(OPERATOR_AND, expr, location);
9254 Expression_list* arguments;
9255 if (params == NULL || params->empty())
9256 arguments = NULL;
9257 else
9259 arguments = new Expression_list();
9260 for (Typed_identifier_list::const_iterator p = params->begin();
9261 p != params->end();
9262 ++p)
9264 Named_object* param = gogo->lookup(p->name(), NULL);
9265 go_assert(param != NULL);
9266 Expression* param_ref = Expression::make_var_reference(param,
9267 location);
9268 arguments->push_back(param_ref);
9272 Expression* func = method->bind_method(expr, location);
9273 go_assert(func != NULL);
9274 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
9275 location);
9276 call->set_hidden_fields_are_ok();
9278 Statement* s = Statement::make_return_from_call(call, location);
9279 Return_statement* retstat = s->return_statement();
9280 if (retstat != NULL)
9282 // We can return values with hidden fields from a stub. This is
9283 // necessary if the method is itself hidden.
9284 retstat->set_hidden_fields_are_ok();
9286 gogo->add_statement(s);
9289 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
9290 // in reverse order.
9292 Expression*
9293 Type::apply_field_indexes(Expression* expr,
9294 const Method::Field_indexes* field_indexes,
9295 Location location)
9297 if (field_indexes == NULL)
9298 return expr;
9299 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
9300 Struct_type* stype = expr->type()->deref()->struct_type();
9301 go_assert(stype != NULL
9302 && field_indexes->field_index < stype->field_count());
9303 if (expr->type()->struct_type() == NULL)
9305 go_assert(expr->type()->points_to() != NULL);
9306 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
9307 go_assert(expr->type()->struct_type() == stype);
9309 return Expression::make_field_reference(expr, field_indexes->field_index,
9310 location);
9313 // Return whether NO is a method for which the receiver is a pointer.
9315 bool
9316 Type::method_expects_pointer(const Named_object* no)
9318 const Function_type *fntype;
9319 if (no->is_function())
9320 fntype = no->func_value()->type();
9321 else if (no->is_function_declaration())
9322 fntype = no->func_declaration_value()->type();
9323 else
9324 go_unreachable();
9325 return fntype->receiver()->type()->points_to() != NULL;
9328 // Given a set of methods for a type, METHODS, return the method NAME,
9329 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
9330 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
9331 // but is ambiguous (and return NULL).
9333 Method*
9334 Type::method_function(const Methods* methods, const std::string& name,
9335 bool* is_ambiguous)
9337 if (is_ambiguous != NULL)
9338 *is_ambiguous = false;
9339 if (methods == NULL)
9340 return NULL;
9341 Methods::const_iterator p = methods->find(name);
9342 if (p == methods->end())
9343 return NULL;
9344 Method* m = p->second;
9345 if (m->is_ambiguous())
9347 if (is_ambiguous != NULL)
9348 *is_ambiguous = true;
9349 return NULL;
9351 return m;
9354 // Return a pointer to the interface method table for TYPE for the
9355 // interface INTERFACE.
9357 Expression*
9358 Type::interface_method_table(Type* type,
9359 Interface_type *interface,
9360 bool is_pointer,
9361 Interface_method_tables** method_tables,
9362 Interface_method_tables** pointer_tables)
9364 go_assert(!interface->is_empty());
9366 Interface_method_tables** pimt = is_pointer ? method_tables : pointer_tables;
9368 if (*pimt == NULL)
9369 *pimt = new Interface_method_tables(5);
9371 std::pair<Interface_type*, Expression*> val(interface, NULL);
9372 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
9374 Location loc = Linemap::predeclared_location();
9375 if (ins.second)
9377 // This is a new entry in the hash table.
9378 go_assert(ins.first->second == NULL);
9379 ins.first->second =
9380 Expression::make_interface_mtable_ref(interface, type, is_pointer, loc);
9382 return Expression::make_unary(OPERATOR_AND, ins.first->second, loc);
9385 // Look for field or method NAME for TYPE. Return an Expression for
9386 // the field or method bound to EXPR. If there is no such field or
9387 // method, give an appropriate error and return an error expression.
9389 Expression*
9390 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
9391 const std::string& name,
9392 Location location)
9394 if (type->deref()->is_error_type())
9395 return Expression::make_error(location);
9397 const Named_type* nt = type->deref()->named_type();
9398 const Struct_type* st = type->deref()->struct_type();
9399 const Interface_type* it = type->interface_type();
9401 // If this is a pointer to a pointer, then it is possible that the
9402 // pointed-to type has methods.
9403 bool dereferenced = false;
9404 if (nt == NULL
9405 && st == NULL
9406 && it == NULL
9407 && type->points_to() != NULL
9408 && type->points_to()->points_to() != NULL)
9410 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
9411 type = type->points_to();
9412 if (type->deref()->is_error_type())
9413 return Expression::make_error(location);
9414 nt = type->points_to()->named_type();
9415 st = type->points_to()->struct_type();
9416 dereferenced = true;
9419 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
9420 || expr->is_addressable());
9421 std::vector<const Named_type*> seen;
9422 bool is_method = false;
9423 bool found_pointer_method = false;
9424 std::string ambig1;
9425 std::string ambig2;
9426 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
9427 &seen, NULL, &is_method,
9428 &found_pointer_method, &ambig1, &ambig2))
9430 Expression* ret;
9431 if (!is_method)
9433 go_assert(st != NULL);
9434 if (type->struct_type() == NULL)
9436 go_assert(type->points_to() != NULL);
9437 expr = Expression::make_unary(OPERATOR_MULT, expr,
9438 location);
9439 go_assert(expr->type()->struct_type() == st);
9441 ret = st->field_reference(expr, name, location);
9443 else if (it != NULL && it->find_method(name) != NULL)
9444 ret = Expression::make_interface_field_reference(expr, name,
9445 location);
9446 else
9448 Method* m;
9449 if (nt != NULL)
9450 m = nt->method_function(name, NULL);
9451 else if (st != NULL)
9452 m = st->method_function(name, NULL);
9453 else
9454 go_unreachable();
9455 go_assert(m != NULL);
9456 if (dereferenced && m->is_value_method())
9458 error_at(location,
9459 "calling value method requires explicit dereference");
9460 return Expression::make_error(location);
9462 if (!m->is_value_method() && expr->type()->points_to() == NULL)
9463 expr = Expression::make_unary(OPERATOR_AND, expr, location);
9464 ret = m->bind_method(expr, location);
9466 go_assert(ret != NULL);
9467 return ret;
9469 else
9471 if (Gogo::is_erroneous_name(name))
9473 // An error was already reported.
9475 else if (!ambig1.empty())
9476 error_at(location, "%qs is ambiguous via %qs and %qs",
9477 Gogo::message_name(name).c_str(), ambig1.c_str(),
9478 ambig2.c_str());
9479 else if (found_pointer_method)
9480 error_at(location, "method requires a pointer receiver");
9481 else if (nt == NULL && st == NULL && it == NULL)
9482 error_at(location,
9483 ("reference to field %qs in object which "
9484 "has no fields or methods"),
9485 Gogo::message_name(name).c_str());
9486 else
9488 bool is_unexported;
9489 // The test for 'a' and 'z' is to handle builtin names,
9490 // which are not hidden.
9491 if (!Gogo::is_hidden_name(name) && (name[0] < 'a' || name[0] > 'z'))
9492 is_unexported = false;
9493 else
9495 std::string unpacked = Gogo::unpack_hidden_name(name);
9496 seen.clear();
9497 is_unexported = Type::is_unexported_field_or_method(gogo, type,
9498 unpacked,
9499 &seen);
9501 if (is_unexported)
9502 error_at(location, "reference to unexported field or method %qs",
9503 Gogo::message_name(name).c_str());
9504 else
9505 error_at(location, "reference to undefined field or method %qs",
9506 Gogo::message_name(name).c_str());
9508 return Expression::make_error(location);
9512 // Look in TYPE for a field or method named NAME, return true if one
9513 // is found. This looks through embedded anonymous fields and handles
9514 // ambiguity. If a method is found, sets *IS_METHOD to true;
9515 // otherwise, if a field is found, set it to false. If
9516 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9517 // whose address can not be taken. SEEN is used to avoid infinite
9518 // recursion on invalid types.
9520 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9521 // method we couldn't use because it requires a pointer. LEVEL is
9522 // used for recursive calls, and can be NULL for a non-recursive call.
9523 // When this function returns false because it finds that the name is
9524 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9525 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9526 // will be unchanged.
9528 // This function just returns whether or not there is a field or
9529 // method, and whether it is a field or method. It doesn't build an
9530 // expression to refer to it. If it is a method, we then look in the
9531 // list of all methods for the type. If it is a field, the search has
9532 // to be done again, looking only for fields, and building up the
9533 // expression as we go.
9535 bool
9536 Type::find_field_or_method(const Type* type,
9537 const std::string& name,
9538 bool receiver_can_be_pointer,
9539 std::vector<const Named_type*>* seen,
9540 int* level,
9541 bool* is_method,
9542 bool* found_pointer_method,
9543 std::string* ambig1,
9544 std::string* ambig2)
9546 // Named types can have locally defined methods.
9547 const Named_type* nt = type->named_type();
9548 if (nt == NULL && type->points_to() != NULL)
9549 nt = type->points_to()->named_type();
9550 if (nt != NULL)
9552 Named_object* no = nt->find_local_method(name);
9553 if (no != NULL)
9555 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
9557 *is_method = true;
9558 return true;
9561 // Record that we have found a pointer method in order to
9562 // give a better error message if we don't find anything
9563 // else.
9564 *found_pointer_method = true;
9567 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9568 p != seen->end();
9569 ++p)
9571 if (*p == nt)
9573 // We've already seen this type when searching for methods.
9574 return false;
9579 // Interface types can have methods.
9580 const Interface_type* it = type->interface_type();
9581 if (it != NULL && it->find_method(name) != NULL)
9583 *is_method = true;
9584 return true;
9587 // Struct types can have fields. They can also inherit fields and
9588 // methods from anonymous fields.
9589 const Struct_type* st = type->deref()->struct_type();
9590 if (st == NULL)
9591 return false;
9592 const Struct_field_list* fields = st->fields();
9593 if (fields == NULL)
9594 return false;
9596 if (nt != NULL)
9597 seen->push_back(nt);
9599 int found_level = 0;
9600 bool found_is_method = false;
9601 std::string found_ambig1;
9602 std::string found_ambig2;
9603 const Struct_field* found_parent = NULL;
9604 for (Struct_field_list::const_iterator pf = fields->begin();
9605 pf != fields->end();
9606 ++pf)
9608 if (pf->is_field_name(name))
9610 *is_method = false;
9611 if (nt != NULL)
9612 seen->pop_back();
9613 return true;
9616 if (!pf->is_anonymous())
9617 continue;
9619 if (pf->type()->deref()->is_error_type()
9620 || pf->type()->deref()->is_undefined())
9621 continue;
9623 Named_type* fnt = pf->type()->named_type();
9624 if (fnt == NULL)
9625 fnt = pf->type()->deref()->named_type();
9626 go_assert(fnt != NULL);
9628 // Methods with pointer receivers on embedded field are
9629 // inherited by the pointer to struct, and also by the struct
9630 // type if the field itself is a pointer.
9631 bool can_be_pointer = (receiver_can_be_pointer
9632 || pf->type()->points_to() != NULL);
9633 int sublevel = level == NULL ? 1 : *level + 1;
9634 bool sub_is_method;
9635 std::string subambig1;
9636 std::string subambig2;
9637 bool subfound = Type::find_field_or_method(fnt,
9638 name,
9639 can_be_pointer,
9640 seen,
9641 &sublevel,
9642 &sub_is_method,
9643 found_pointer_method,
9644 &subambig1,
9645 &subambig2);
9646 if (!subfound)
9648 if (!subambig1.empty())
9650 // The name was found via this field, but is ambiguous.
9651 // if the ambiguity is lower or at the same level as
9652 // anything else we have already found, then we want to
9653 // pass the ambiguity back to the caller.
9654 if (found_level == 0 || sublevel <= found_level)
9656 found_ambig1 = (Gogo::message_name(pf->field_name())
9657 + '.' + subambig1);
9658 found_ambig2 = (Gogo::message_name(pf->field_name())
9659 + '.' + subambig2);
9660 found_level = sublevel;
9664 else
9666 // The name was found via this field. Use the level to see
9667 // if we want to use this one, or whether it introduces an
9668 // ambiguity.
9669 if (found_level == 0 || sublevel < found_level)
9671 found_level = sublevel;
9672 found_is_method = sub_is_method;
9673 found_ambig1.clear();
9674 found_ambig2.clear();
9675 found_parent = &*pf;
9677 else if (sublevel > found_level)
9679 else if (found_ambig1.empty())
9681 // We found an ambiguity.
9682 go_assert(found_parent != NULL);
9683 found_ambig1 = Gogo::message_name(found_parent->field_name());
9684 found_ambig2 = Gogo::message_name(pf->field_name());
9686 else
9688 // We found an ambiguity, but we already know of one.
9689 // Just report the earlier one.
9694 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
9695 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
9696 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
9697 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
9699 if (nt != NULL)
9700 seen->pop_back();
9702 if (found_level == 0)
9703 return false;
9704 else if (!found_ambig1.empty())
9706 go_assert(!found_ambig1.empty());
9707 ambig1->assign(found_ambig1);
9708 ambig2->assign(found_ambig2);
9709 if (level != NULL)
9710 *level = found_level;
9711 return false;
9713 else
9715 if (level != NULL)
9716 *level = found_level;
9717 *is_method = found_is_method;
9718 return true;
9722 // Return whether NAME is an unexported field or method for TYPE.
9724 bool
9725 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
9726 const std::string& name,
9727 std::vector<const Named_type*>* seen)
9729 const Named_type* nt = type->named_type();
9730 if (nt == NULL)
9731 nt = type->deref()->named_type();
9732 if (nt != NULL)
9734 if (nt->is_unexported_local_method(gogo, name))
9735 return true;
9737 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9738 p != seen->end();
9739 ++p)
9741 if (*p == nt)
9743 // We've already seen this type.
9744 return false;
9749 const Interface_type* it = type->interface_type();
9750 if (it != NULL && it->is_unexported_method(gogo, name))
9751 return true;
9753 type = type->deref();
9755 const Struct_type* st = type->struct_type();
9756 if (st != NULL && st->is_unexported_local_field(gogo, name))
9757 return true;
9759 if (st == NULL)
9760 return false;
9762 const Struct_field_list* fields = st->fields();
9763 if (fields == NULL)
9764 return false;
9766 if (nt != NULL)
9767 seen->push_back(nt);
9769 for (Struct_field_list::const_iterator pf = fields->begin();
9770 pf != fields->end();
9771 ++pf)
9773 if (pf->is_anonymous()
9774 && !pf->type()->deref()->is_error_type()
9775 && !pf->type()->deref()->is_undefined())
9777 Named_type* subtype = pf->type()->named_type();
9778 if (subtype == NULL)
9779 subtype = pf->type()->deref()->named_type();
9780 if (subtype == NULL)
9782 // This is an error, but it will be diagnosed elsewhere.
9783 continue;
9785 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
9787 if (nt != NULL)
9788 seen->pop_back();
9789 return true;
9794 if (nt != NULL)
9795 seen->pop_back();
9797 return false;
9800 // Class Forward_declaration.
9802 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
9803 : Type(TYPE_FORWARD),
9804 named_object_(named_object->resolve()), warned_(false)
9806 go_assert(this->named_object_->is_unknown()
9807 || this->named_object_->is_type_declaration());
9810 // Return the named object.
9812 Named_object*
9813 Forward_declaration_type::named_object()
9815 return this->named_object_->resolve();
9818 const Named_object*
9819 Forward_declaration_type::named_object() const
9821 return this->named_object_->resolve();
9824 // Return the name of the forward declared type.
9826 const std::string&
9827 Forward_declaration_type::name() const
9829 return this->named_object()->name();
9832 // Warn about a use of a type which has been declared but not defined.
9834 void
9835 Forward_declaration_type::warn() const
9837 Named_object* no = this->named_object_->resolve();
9838 if (no->is_unknown())
9840 // The name was not defined anywhere.
9841 if (!this->warned_)
9843 error_at(this->named_object_->location(),
9844 "use of undefined type %qs",
9845 no->message_name().c_str());
9846 this->warned_ = true;
9849 else if (no->is_type_declaration())
9851 // The name was seen as a type, but the type was never defined.
9852 if (no->type_declaration_value()->using_type())
9854 error_at(this->named_object_->location(),
9855 "use of undefined type %qs",
9856 no->message_name().c_str());
9857 this->warned_ = true;
9860 else
9862 // The name was defined, but not as a type.
9863 if (!this->warned_)
9865 error_at(this->named_object_->location(), "expected type");
9866 this->warned_ = true;
9871 // Get the base type of a declaration. This gives an error if the
9872 // type has not yet been defined.
9874 Type*
9875 Forward_declaration_type::real_type()
9877 if (this->is_defined())
9878 return this->named_object()->type_value();
9879 else
9881 this->warn();
9882 return Type::make_error_type();
9886 const Type*
9887 Forward_declaration_type::real_type() const
9889 if (this->is_defined())
9890 return this->named_object()->type_value();
9891 else
9893 this->warn();
9894 return Type::make_error_type();
9898 // Return whether the base type is defined.
9900 bool
9901 Forward_declaration_type::is_defined() const
9903 return this->named_object()->is_type();
9906 // Add a method. This is used when methods are defined before the
9907 // type.
9909 Named_object*
9910 Forward_declaration_type::add_method(const std::string& name,
9911 Function* function)
9913 Named_object* no = this->named_object();
9914 if (no->is_unknown())
9915 no->declare_as_type();
9916 return no->type_declaration_value()->add_method(name, function);
9919 // Add a method declaration. This is used when methods are declared
9920 // before the type.
9922 Named_object*
9923 Forward_declaration_type::add_method_declaration(const std::string& name,
9924 Package* package,
9925 Function_type* type,
9926 Location location)
9928 Named_object* no = this->named_object();
9929 if (no->is_unknown())
9930 no->declare_as_type();
9931 Type_declaration* td = no->type_declaration_value();
9932 return td->add_method_declaration(name, package, type, location);
9935 // Traversal.
9938 Forward_declaration_type::do_traverse(Traverse* traverse)
9940 if (this->is_defined()
9941 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
9942 return TRAVERSE_EXIT;
9943 return TRAVERSE_CONTINUE;
9946 // Verify the type.
9948 bool
9949 Forward_declaration_type::do_verify()
9951 if (!this->is_defined() && !this->is_nil_constant_as_type())
9953 this->warn();
9954 return false;
9956 return true;
9959 // Get the backend representation for the type.
9961 Btype*
9962 Forward_declaration_type::do_get_backend(Gogo* gogo)
9964 if (this->is_defined())
9965 return Type::get_named_base_btype(gogo, this->real_type());
9967 if (this->warned_)
9968 return gogo->backend()->error_type();
9970 // We represent an undefined type as a struct with no fields. That
9971 // should work fine for the backend, since the same case can arise
9972 // in C.
9973 std::vector<Backend::Btyped_identifier> fields;
9974 Btype* bt = gogo->backend()->struct_type(fields);
9975 return gogo->backend()->named_type(this->name(), bt,
9976 this->named_object()->location());
9979 // Build a type descriptor for a forwarded type.
9981 Expression*
9982 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9984 Location ploc = Linemap::predeclared_location();
9985 if (!this->is_defined())
9986 return Expression::make_error(ploc);
9987 else
9989 Type* t = this->real_type();
9990 if (name != NULL)
9991 return this->named_type_descriptor(gogo, t, name);
9992 else
9993 return Expression::make_type_descriptor(t, ploc);
9997 // The reflection string.
9999 void
10000 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
10002 this->append_reflection(this->real_type(), gogo, ret);
10005 // The mangled name.
10007 void
10008 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
10010 if (this->is_defined())
10011 this->append_mangled_name(this->real_type(), gogo, ret);
10012 else
10014 const Named_object* no = this->named_object();
10015 std::string name;
10016 if (no->package() == NULL)
10017 name = gogo->pkgpath_symbol();
10018 else
10019 name = no->package()->pkgpath_symbol();
10020 name += '.';
10021 name += Gogo::unpack_hidden_name(no->name());
10022 char buf[20];
10023 snprintf(buf, sizeof buf, "N%u_",
10024 static_cast<unsigned int>(name.length()));
10025 ret->append(buf);
10026 ret->append(name);
10030 // Export a forward declaration. This can happen when a defined type
10031 // refers to a type which is only declared (and is presumably defined
10032 // in some other file in the same package).
10034 void
10035 Forward_declaration_type::do_export(Export*) const
10037 // If there is a base type, that should be exported instead of this.
10038 go_assert(!this->is_defined());
10040 // We don't output anything.
10043 // Make a forward declaration.
10045 Type*
10046 Type::make_forward_declaration(Named_object* named_object)
10048 return new Forward_declaration_type(named_object);
10051 // Class Typed_identifier_list.
10053 // Sort the entries by name.
10055 struct Typed_identifier_list_sort
10057 public:
10058 bool
10059 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
10060 { return t1.name() < t2.name(); }
10063 void
10064 Typed_identifier_list::sort_by_name()
10066 std::sort(this->entries_.begin(), this->entries_.end(),
10067 Typed_identifier_list_sort());
10070 // Traverse types.
10073 Typed_identifier_list::traverse(Traverse* traverse)
10075 for (Typed_identifier_list::const_iterator p = this->begin();
10076 p != this->end();
10077 ++p)
10079 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
10080 return TRAVERSE_EXIT;
10082 return TRAVERSE_CONTINUE;
10085 // Copy the list.
10087 Typed_identifier_list*
10088 Typed_identifier_list::copy() const
10090 Typed_identifier_list* ret = new Typed_identifier_list();
10091 for (Typed_identifier_list::const_iterator p = this->begin();
10092 p != this->end();
10093 ++p)
10094 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
10095 return ret;