]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/go/gofrontend/types.cc
PR go/90482
[thirdparty/gcc.git] / gcc / go / gofrontend / types.cc
1 // types.cc -- Go frontend types.
2
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.
6
7 #include "go-system.h"
8
9 #include <ostream>
10
11 #include "go-c.h"
12 #include "gogo.h"
13 #include "go-diagnostics.h"
14 #include "go-encode-id.h"
15 #include "operator.h"
16 #include "expressions.h"
17 #include "statements.h"
18 #include "export.h"
19 #include "import.h"
20 #include "backend.h"
21 #include "types.h"
22
23 // Forward declarations so that we don't have to make types.h #include
24 // backend.h.
25
26 static void
27 get_backend_struct_fields(Gogo* gogo, Struct_type* type, bool use_placeholder,
28 std::vector<Backend::Btyped_identifier>* bfields);
29
30 static void
31 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
32 std::vector<Backend::Btyped_identifier>* bfields);
33
34 static void
35 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
36 bool use_placeholder,
37 std::vector<Backend::Btyped_identifier>* bfields);
38
39 // Class Type.
40
41 Type::Type(Type_classification classification)
42 : classification_(classification), btype_(NULL), type_descriptor_var_(NULL),
43 gc_symbol_var_(NULL)
44 {
45 }
46
47 Type::~Type()
48 {
49 }
50
51 // Get the base type for a type--skip names and forward declarations.
52
53 Type*
54 Type::base()
55 {
56 switch (this->classification_)
57 {
58 case TYPE_NAMED:
59 return this->named_type()->named_base();
60 case TYPE_FORWARD:
61 return this->forward_declaration_type()->real_type()->base();
62 default:
63 return this;
64 }
65 }
66
67 const Type*
68 Type::base() const
69 {
70 switch (this->classification_)
71 {
72 case TYPE_NAMED:
73 return this->named_type()->named_base();
74 case TYPE_FORWARD:
75 return this->forward_declaration_type()->real_type()->base();
76 default:
77 return this;
78 }
79 }
80
81 // Skip defined forward declarations.
82
83 Type*
84 Type::forwarded()
85 {
86 Type* t = this;
87 Forward_declaration_type* ftype = t->forward_declaration_type();
88 while (ftype != NULL && ftype->is_defined())
89 {
90 t = ftype->real_type();
91 ftype = t->forward_declaration_type();
92 }
93 return t;
94 }
95
96 const Type*
97 Type::forwarded() const
98 {
99 const Type* t = this;
100 const Forward_declaration_type* ftype = t->forward_declaration_type();
101 while (ftype != NULL && ftype->is_defined())
102 {
103 t = ftype->real_type();
104 ftype = t->forward_declaration_type();
105 }
106 return t;
107 }
108
109 // Skip alias definitions.
110
111 Type*
112 Type::unalias()
113 {
114 Type* t = this->forwarded();
115 Named_type* nt = t->named_type();
116 while (nt != NULL && nt->is_alias())
117 {
118 t = nt->real_type()->forwarded();
119 nt = t->named_type();
120 }
121 return t;
122 }
123
124 const Type*
125 Type::unalias() const
126 {
127 const Type* t = this->forwarded();
128 const Named_type* nt = t->named_type();
129 while (nt != NULL && nt->is_alias())
130 {
131 t = nt->real_type()->forwarded();
132 nt = t->named_type();
133 }
134 return t;
135 }
136
137 // If this is a named type, return it. Otherwise, return NULL.
138
139 Named_type*
140 Type::named_type()
141 {
142 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
143 }
144
145 const Named_type*
146 Type::named_type() const
147 {
148 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
149 }
150
151 // Return true if this type is not defined.
152
153 bool
154 Type::is_undefined() const
155 {
156 return this->forwarded()->forward_declaration_type() != NULL;
157 }
158
159 // Return true if this is a basic type: a type which is not composed
160 // of other types, and is not void.
161
162 bool
163 Type::is_basic_type() const
164 {
165 switch (this->classification_)
166 {
167 case TYPE_INTEGER:
168 case TYPE_FLOAT:
169 case TYPE_COMPLEX:
170 case TYPE_BOOLEAN:
171 case TYPE_STRING:
172 case TYPE_NIL:
173 return true;
174
175 case TYPE_ERROR:
176 case TYPE_VOID:
177 case TYPE_FUNCTION:
178 case TYPE_POINTER:
179 case TYPE_STRUCT:
180 case TYPE_ARRAY:
181 case TYPE_MAP:
182 case TYPE_CHANNEL:
183 case TYPE_INTERFACE:
184 return false;
185
186 case TYPE_NAMED:
187 case TYPE_FORWARD:
188 return this->base()->is_basic_type();
189
190 default:
191 go_unreachable();
192 }
193 }
194
195 // Return true if this is an abstract type.
196
197 bool
198 Type::is_abstract() const
199 {
200 switch (this->classification())
201 {
202 case TYPE_INTEGER:
203 return this->integer_type()->is_abstract();
204 case TYPE_FLOAT:
205 return this->float_type()->is_abstract();
206 case TYPE_COMPLEX:
207 return this->complex_type()->is_abstract();
208 case TYPE_STRING:
209 return this->is_abstract_string_type();
210 case TYPE_BOOLEAN:
211 return this->is_abstract_boolean_type();
212 default:
213 return false;
214 }
215 }
216
217 // Return a non-abstract version of an abstract type.
218
219 Type*
220 Type::make_non_abstract_type()
221 {
222 go_assert(this->is_abstract());
223 switch (this->classification())
224 {
225 case TYPE_INTEGER:
226 if (this->integer_type()->is_rune())
227 return Type::lookup_integer_type("int32");
228 else
229 return Type::lookup_integer_type("int");
230 case TYPE_FLOAT:
231 return Type::lookup_float_type("float64");
232 case TYPE_COMPLEX:
233 return Type::lookup_complex_type("complex128");
234 case TYPE_STRING:
235 return Type::lookup_string_type();
236 case TYPE_BOOLEAN:
237 return Type::lookup_bool_type();
238 default:
239 go_unreachable();
240 }
241 }
242
243 // Return true if this is an error type. Don't give an error if we
244 // try to dereference an undefined forwarding type, as this is called
245 // in the parser when the type may legitimately be undefined.
246
247 bool
248 Type::is_error_type() const
249 {
250 const Type* t = this->forwarded();
251 // Note that we return false for an undefined forward type.
252 switch (t->classification_)
253 {
254 case TYPE_ERROR:
255 return true;
256 case TYPE_NAMED:
257 return t->named_type()->is_named_error_type();
258 default:
259 return false;
260 }
261 }
262
263 // If this is a pointer type, return the type to which it points.
264 // Otherwise, return NULL.
265
266 Type*
267 Type::points_to() const
268 {
269 const Pointer_type* ptype = this->convert<const Pointer_type,
270 TYPE_POINTER>();
271 return ptype == NULL ? NULL : ptype->points_to();
272 }
273
274 // Return whether this is a slice type.
275
276 bool
277 Type::is_slice_type() const
278 {
279 return this->array_type() != NULL && this->array_type()->length() == NULL;
280 }
281
282 // Return whether this is the predeclared constant nil being used as a
283 // type.
284
285 bool
286 Type::is_nil_constant_as_type() const
287 {
288 const Type* t = this->forwarded();
289 if (t->forward_declaration_type() != NULL)
290 {
291 const Named_object* no = t->forward_declaration_type()->named_object();
292 if (no->is_unknown())
293 no = no->unknown_value()->real_named_object();
294 if (no != NULL
295 && no->is_const()
296 && no->const_value()->expr()->is_nil_expression())
297 return true;
298 }
299 return false;
300 }
301
302 // Traverse a type.
303
304 int
305 Type::traverse(Type* type, Traverse* traverse)
306 {
307 go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
308 || (traverse->traverse_mask()
309 & Traverse::traverse_expressions) != 0);
310 if (traverse->remember_type(type))
311 {
312 // We have already traversed this type.
313 return TRAVERSE_CONTINUE;
314 }
315 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
316 {
317 int t = traverse->type(type);
318 if (t == TRAVERSE_EXIT)
319 return TRAVERSE_EXIT;
320 else if (t == TRAVERSE_SKIP_COMPONENTS)
321 return TRAVERSE_CONTINUE;
322 }
323 // An array type has an expression which we need to traverse if
324 // traverse_expressions is set.
325 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
326 return TRAVERSE_EXIT;
327 return TRAVERSE_CONTINUE;
328 }
329
330 // Default implementation for do_traverse for child class.
331
332 int
333 Type::do_traverse(Traverse*)
334 {
335 return TRAVERSE_CONTINUE;
336 }
337
338 // Return whether two types are identical. If REASON is not NULL,
339 // optionally set *REASON to the reason the types are not identical.
340
341 bool
342 Type::are_identical(const Type* t1, const Type* t2, int flags,
343 std::string* reason)
344 {
345 if (t1 == NULL || t2 == NULL)
346 {
347 // Something is wrong.
348 return (flags & COMPARE_ERRORS) == 0 ? true : t1 == t2;
349 }
350
351 // Skip defined forward declarations.
352 t1 = t1->forwarded();
353 t2 = t2->forwarded();
354
355 if ((flags & COMPARE_ALIASES) == 0)
356 {
357 // Ignore aliases.
358 t1 = t1->unalias();
359 t2 = t2->unalias();
360 }
361
362 if (t1 == t2)
363 return true;
364
365 // An undefined forward declaration is an error.
366 if (t1->forward_declaration_type() != NULL
367 || t2->forward_declaration_type() != NULL)
368 return (flags & COMPARE_ERRORS) == 0;
369
370 // Avoid cascading errors with error types.
371 if (t1->is_error_type() || t2->is_error_type())
372 {
373 if ((flags & COMPARE_ERRORS) == 0)
374 return true;
375 return t1->is_error_type() && t2->is_error_type();
376 }
377
378 // Get a good reason for the sink type. Note that the sink type on
379 // the left hand side of an assignment is handled in are_assignable.
380 if (t1->is_sink_type() || t2->is_sink_type())
381 {
382 if (reason != NULL)
383 *reason = "invalid use of _";
384 return false;
385 }
386
387 // A named type is only identical to itself.
388 if (t1->named_type() != NULL || t2->named_type() != NULL)
389 return false;
390
391 // Check type shapes.
392 if (t1->classification() != t2->classification())
393 return false;
394
395 switch (t1->classification())
396 {
397 case TYPE_VOID:
398 case TYPE_BOOLEAN:
399 case TYPE_STRING:
400 case TYPE_NIL:
401 // These types are always identical.
402 return true;
403
404 case TYPE_INTEGER:
405 return t1->integer_type()->is_identical(t2->integer_type());
406
407 case TYPE_FLOAT:
408 return t1->float_type()->is_identical(t2->float_type());
409
410 case TYPE_COMPLEX:
411 return t1->complex_type()->is_identical(t2->complex_type());
412
413 case TYPE_FUNCTION:
414 return t1->function_type()->is_identical(t2->function_type(),
415 false, flags, reason);
416
417 case TYPE_POINTER:
418 return Type::are_identical(t1->points_to(), t2->points_to(), flags,
419 reason);
420
421 case TYPE_STRUCT:
422 return t1->struct_type()->is_identical(t2->struct_type(), flags);
423
424 case TYPE_ARRAY:
425 return t1->array_type()->is_identical(t2->array_type(), flags);
426
427 case TYPE_MAP:
428 return t1->map_type()->is_identical(t2->map_type(), flags);
429
430 case TYPE_CHANNEL:
431 return t1->channel_type()->is_identical(t2->channel_type(), flags);
432
433 case TYPE_INTERFACE:
434 return t1->interface_type()->is_identical(t2->interface_type(), flags);
435
436 case TYPE_CALL_MULTIPLE_RESULT:
437 if (reason != NULL)
438 *reason = "invalid use of multiple-value function call";
439 return false;
440
441 default:
442 go_unreachable();
443 }
444 }
445
446 // Return true if it's OK to have a binary operation with types LHS
447 // and RHS. This is not used for shifts or comparisons.
448
449 bool
450 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
451 {
452 if (Type::are_identical(lhs, rhs, Type::COMPARE_TAGS, NULL))
453 return true;
454
455 // A constant of abstract bool type may be mixed with any bool type.
456 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
457 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
458 return true;
459
460 // A constant of abstract string type may be mixed with any string
461 // type.
462 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
463 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
464 return true;
465
466 lhs = lhs->base();
467 rhs = rhs->base();
468
469 // A constant of abstract integer, float, or complex type may be
470 // mixed with an integer, float, or complex type.
471 if ((rhs->is_abstract()
472 && (rhs->integer_type() != NULL
473 || rhs->float_type() != NULL
474 || rhs->complex_type() != NULL)
475 && (lhs->integer_type() != NULL
476 || lhs->float_type() != NULL
477 || lhs->complex_type() != NULL))
478 || (lhs->is_abstract()
479 && (lhs->integer_type() != NULL
480 || lhs->float_type() != NULL
481 || lhs->complex_type() != NULL)
482 && (rhs->integer_type() != NULL
483 || rhs->float_type() != NULL
484 || rhs->complex_type() != NULL)))
485 return true;
486
487 // The nil type may be compared to a pointer, an interface type, a
488 // slice type, a channel type, a map type, or a function type.
489 if (lhs->is_nil_type()
490 && (rhs->points_to() != NULL
491 || rhs->interface_type() != NULL
492 || rhs->is_slice_type()
493 || rhs->map_type() != NULL
494 || rhs->channel_type() != NULL
495 || rhs->function_type() != NULL))
496 return true;
497 if (rhs->is_nil_type()
498 && (lhs->points_to() != NULL
499 || lhs->interface_type() != NULL
500 || lhs->is_slice_type()
501 || lhs->map_type() != NULL
502 || lhs->channel_type() != NULL
503 || lhs->function_type() != NULL))
504 return true;
505
506 return false;
507 }
508
509 // Return true if a value with type T1 may be compared with a value of
510 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
511
512 bool
513 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
514 const Type *t2, std::string *reason)
515 {
516 if (t1 != t2
517 && !Type::are_assignable(t1, t2, NULL)
518 && !Type::are_assignable(t2, t1, NULL))
519 {
520 if (reason != NULL)
521 *reason = "incompatible types in binary expression";
522 return false;
523 }
524
525 if (!is_equality_op)
526 {
527 if (t1->integer_type() == NULL
528 && t1->float_type() == NULL
529 && !t1->is_string_type())
530 {
531 if (reason != NULL)
532 *reason = _("invalid comparison of non-ordered type");
533 return false;
534 }
535 }
536 else if (t1->is_slice_type()
537 || t1->map_type() != NULL
538 || t1->function_type() != NULL
539 || t2->is_slice_type()
540 || t2->map_type() != NULL
541 || t2->function_type() != NULL)
542 {
543 if (!t1->is_nil_type() && !t2->is_nil_type())
544 {
545 if (reason != NULL)
546 {
547 if (t1->is_slice_type() || t2->is_slice_type())
548 *reason = _("slice can only be compared to nil");
549 else if (t1->map_type() != NULL || t2->map_type() != NULL)
550 *reason = _("map can only be compared to nil");
551 else
552 *reason = _("func can only be compared to nil");
553
554 // Match 6g error messages.
555 if (t1->interface_type() != NULL || t2->interface_type() != NULL)
556 {
557 char buf[200];
558 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
559 reason->c_str());
560 *reason = buf;
561 }
562 }
563 return false;
564 }
565 }
566 else
567 {
568 if (!t1->is_boolean_type()
569 && t1->integer_type() == NULL
570 && t1->float_type() == NULL
571 && t1->complex_type() == NULL
572 && !t1->is_string_type()
573 && t1->points_to() == NULL
574 && t1->channel_type() == NULL
575 && t1->interface_type() == NULL
576 && t1->struct_type() == NULL
577 && t1->array_type() == NULL
578 && !t1->is_nil_type())
579 {
580 if (reason != NULL)
581 *reason = _("invalid comparison of non-comparable type");
582 return false;
583 }
584
585 if (t1->unalias()->named_type() != NULL)
586 return t1->unalias()->named_type()->named_type_is_comparable(reason);
587 else if (t2->unalias()->named_type() != NULL)
588 return t2->unalias()->named_type()->named_type_is_comparable(reason);
589 else if (t1->struct_type() != NULL)
590 {
591 if (t1->struct_type()->is_struct_incomparable())
592 {
593 if (reason != NULL)
594 *reason = _("invalid comparison of generated struct");
595 return false;
596 }
597 const Struct_field_list* fields = t1->struct_type()->fields();
598 for (Struct_field_list::const_iterator p = fields->begin();
599 p != fields->end();
600 ++p)
601 {
602 if (!p->type()->is_comparable())
603 {
604 if (reason != NULL)
605 *reason = _("invalid comparison of non-comparable struct");
606 return false;
607 }
608 }
609 }
610 else if (t1->array_type() != NULL)
611 {
612 if (t1->array_type()->is_array_incomparable())
613 {
614 if (reason != NULL)
615 *reason = _("invalid comparison of generated array");
616 return false;
617 }
618 if (t1->array_type()->length()->is_nil_expression()
619 || !t1->array_type()->element_type()->is_comparable())
620 {
621 if (reason != NULL)
622 *reason = _("invalid comparison of non-comparable array");
623 return false;
624 }
625 }
626 }
627
628 return true;
629 }
630
631 // Return true if a value with type RHS may be assigned to a variable
632 // with type LHS. If REASON is not NULL, set *REASON to the reason
633 // the types are not assignable.
634
635 bool
636 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
637 {
638 // Do some checks first. Make sure the types are defined.
639 if (rhs != NULL && !rhs->is_undefined())
640 {
641 if (rhs->is_void_type())
642 {
643 if (reason != NULL)
644 *reason = "non-value used as value";
645 return false;
646 }
647 if (rhs->is_call_multiple_result_type())
648 {
649 if (reason != NULL)
650 reason->assign(_("multiple-value function call in "
651 "single-value context"));
652 return false;
653 }
654 }
655
656 // Any value may be assigned to the blank identifier.
657 if (lhs != NULL
658 && !lhs->is_undefined()
659 && lhs->is_sink_type())
660 return true;
661
662 // Identical types are assignable.
663 if (Type::are_identical(lhs, rhs, Type::COMPARE_TAGS, reason))
664 return true;
665
666 // Ignore aliases, except for error messages.
667 const Type* lhs_orig = lhs;
668 const Type* rhs_orig = rhs;
669 lhs = lhs->unalias();
670 rhs = rhs->unalias();
671
672 // The types are assignable if they have identical underlying types
673 // and either LHS or RHS is not a named type.
674 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
675 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
676 && Type::are_identical(lhs->base(), rhs->base(), Type::COMPARE_TAGS,
677 reason))
678 return true;
679
680 // The types are assignable if LHS is an interface type and RHS
681 // implements the required methods.
682 const Interface_type* lhs_interface_type = lhs->interface_type();
683 if (lhs_interface_type != NULL)
684 {
685 if (lhs_interface_type->implements_interface(rhs, reason))
686 return true;
687 const Interface_type* rhs_interface_type = rhs->interface_type();
688 if (rhs_interface_type != NULL
689 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
690 reason))
691 return true;
692 }
693
694 // The type are assignable if RHS is a bidirectional channel type,
695 // LHS is a channel type, they have identical element types, and
696 // either LHS or RHS is not a named type.
697 if (lhs->channel_type() != NULL
698 && rhs->channel_type() != NULL
699 && rhs->channel_type()->may_send()
700 && rhs->channel_type()->may_receive()
701 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
702 && Type::are_identical(lhs->channel_type()->element_type(),
703 rhs->channel_type()->element_type(),
704 Type::COMPARE_TAGS,
705 reason))
706 return true;
707
708 // The nil type may be assigned to a pointer, function, slice, map,
709 // channel, or interface type.
710 if (rhs->is_nil_type()
711 && (lhs->points_to() != NULL
712 || lhs->function_type() != NULL
713 || lhs->is_slice_type()
714 || lhs->map_type() != NULL
715 || lhs->channel_type() != NULL
716 || lhs->interface_type() != NULL))
717 return true;
718
719 // An untyped numeric constant may be assigned to a numeric type if
720 // it is representable in that type.
721 if ((rhs->is_abstract()
722 && (rhs->integer_type() != NULL
723 || rhs->float_type() != NULL
724 || rhs->complex_type() != NULL))
725 && (lhs->integer_type() != NULL
726 || lhs->float_type() != NULL
727 || lhs->complex_type() != NULL))
728 return true;
729
730 // Give some better error messages.
731 if (reason != NULL && reason->empty())
732 {
733 if (rhs->interface_type() != NULL)
734 reason->assign(_("need explicit conversion"));
735 else if (lhs_orig->named_type() != NULL
736 && rhs_orig->named_type() != NULL)
737 {
738 size_t len = (lhs_orig->named_type()->name().length()
739 + rhs_orig->named_type()->name().length()
740 + 100);
741 char* buf = new char[len];
742 snprintf(buf, len, _("cannot use type %s as type %s"),
743 rhs_orig->named_type()->message_name().c_str(),
744 lhs_orig->named_type()->message_name().c_str());
745 reason->assign(buf);
746 delete[] buf;
747 }
748 }
749
750 return false;
751 }
752
753 // Return true if a value with type RHS may be converted to type LHS.
754 // If REASON is not NULL, set *REASON to the reason the types are not
755 // convertible.
756
757 bool
758 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
759 {
760 // The types are convertible if they are assignable.
761 if (Type::are_assignable(lhs, rhs, reason))
762 return true;
763
764 // Ignore aliases.
765 lhs = lhs->unalias();
766 rhs = rhs->unalias();
767
768 // A pointer to a regular type may not be converted to a pointer to
769 // a type that may not live in the heap, except when converting from
770 // unsafe.Pointer.
771 if (lhs->points_to() != NULL
772 && rhs->points_to() != NULL
773 && !lhs->points_to()->in_heap()
774 && rhs->points_to()->in_heap()
775 && !rhs->is_unsafe_pointer_type())
776 {
777 if (reason != NULL)
778 reason->assign(_("conversion from normal type to notinheap type"));
779 return false;
780 }
781
782 // The types are convertible if they have identical underlying
783 // types, ignoring struct field tags.
784 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
785 && Type::are_identical(lhs->base(), rhs->base(), 0, reason))
786 return true;
787
788 // The types are convertible if they are both unnamed pointer types
789 // and their pointer base types have identical underlying types,
790 // ignoring struct field tags.
791 if (lhs->named_type() == NULL
792 && rhs->named_type() == NULL
793 && lhs->points_to() != NULL
794 && rhs->points_to() != NULL
795 && (lhs->points_to()->named_type() != NULL
796 || rhs->points_to()->named_type() != NULL)
797 && Type::are_identical(lhs->points_to()->base(),
798 rhs->points_to()->base(),
799 0, reason))
800 return true;
801
802 // Integer and floating point types are convertible to each other.
803 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
804 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
805 return true;
806
807 // Complex types are convertible to each other.
808 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
809 return true;
810
811 // An integer, or []byte, or []rune, may be converted to a string.
812 if (lhs->is_string_type())
813 {
814 if (rhs->integer_type() != NULL)
815 return true;
816 if (rhs->is_slice_type())
817 {
818 const Type* e = rhs->array_type()->element_type()->forwarded();
819 if (e->integer_type() != NULL
820 && (e->integer_type()->is_byte()
821 || e->integer_type()->is_rune()))
822 return true;
823 }
824 }
825
826 // A string may be converted to []byte or []rune.
827 if (rhs->is_string_type() && lhs->is_slice_type())
828 {
829 const Type* e = lhs->array_type()->element_type()->forwarded();
830 if (e->integer_type() != NULL
831 && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
832 return true;
833 }
834
835 // An unsafe.Pointer type may be converted to any pointer type or to
836 // a type whose underlying type is uintptr, and vice-versa.
837 if (lhs->is_unsafe_pointer_type()
838 && (rhs->points_to() != NULL
839 || (rhs->integer_type() != NULL
840 && rhs->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
841 return true;
842 if (rhs->is_unsafe_pointer_type()
843 && (lhs->points_to() != NULL
844 || (lhs->integer_type() != NULL
845 && lhs->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
846 return true;
847
848 // Give a better error message.
849 if (reason != NULL)
850 {
851 if (reason->empty())
852 *reason = "invalid type conversion";
853 else
854 {
855 std::string s = "invalid type conversion (";
856 s += *reason;
857 s += ')';
858 *reason = s;
859 }
860 }
861
862 return false;
863 }
864
865 // Copy expressions if it may change the size.
866 //
867 // The only type that has an expression is an array type. The only
868 // types whose size can be changed by the size of an array type are an
869 // array type itself, or a struct type with an array field.
870 Type*
871 Type::copy_expressions()
872 {
873 // This is run during parsing, so types may not be valid yet.
874 // We only have to worry about array type literals.
875 switch (this->classification_)
876 {
877 default:
878 return this;
879
880 case TYPE_ARRAY:
881 {
882 Array_type* at = this->array_type();
883 if (at->length() == NULL)
884 return this;
885 Expression* len = at->length()->copy();
886 if (at->length() == len)
887 return this;
888 return Type::make_array_type(at->element_type(), len);
889 }
890
891 case TYPE_STRUCT:
892 {
893 Struct_type* st = this->struct_type();
894 const Struct_field_list* sfl = st->fields();
895 if (sfl == NULL)
896 return this;
897 bool changed = false;
898 Struct_field_list *nsfl = new Struct_field_list();
899 for (Struct_field_list::const_iterator pf = sfl->begin();
900 pf != sfl->end();
901 ++pf)
902 {
903 Type* ft = pf->type()->copy_expressions();
904 Struct_field nf(Typed_identifier((pf->is_anonymous()
905 ? ""
906 : pf->field_name()),
907 ft,
908 pf->location()));
909 if (pf->has_tag())
910 nf.set_tag(pf->tag());
911 nsfl->push_back(nf);
912 if (ft != pf->type())
913 changed = true;
914 }
915 if (!changed)
916 {
917 delete(nsfl);
918 return this;
919 }
920 return Type::make_struct_type(nsfl, st->location());
921 }
922 }
923
924 go_unreachable();
925 }
926
927 // Return a hash code for the type to be used for method lookup.
928
929 unsigned int
930 Type::hash_for_method(Gogo* gogo, int flags) const
931 {
932 const Type* t = this->forwarded();
933 if (t->named_type() != NULL && t->named_type()->is_alias())
934 {
935 unsigned int r =
936 t->named_type()->real_type()->hash_for_method(gogo, flags);
937 if ((flags & Type::COMPARE_ALIASES) != 0)
938 r += TYPE_FORWARD;
939 return r;
940 }
941 unsigned int ret = t->classification_;
942 return ret + t->do_hash_for_method(gogo, flags);
943 }
944
945 // Default implementation of do_hash_for_method. This is appropriate
946 // for types with no subfields.
947
948 unsigned int
949 Type::do_hash_for_method(Gogo*, int) const
950 {
951 return 0;
952 }
953
954 // A hash table mapping unnamed types to the backend representation of
955 // those types.
956
957 Type::Type_btypes Type::type_btypes;
958
959 // Return the backend representation for this type.
960
961 Btype*
962 Type::get_backend(Gogo* gogo)
963 {
964 if (this->btype_ != NULL)
965 return this->btype_;
966
967 if (this->named_type() != NULL && this->named_type()->is_alias()) {
968 Btype* bt = this->unalias()->get_backend(gogo);
969 if (gogo != NULL && gogo->named_types_are_converted())
970 this->btype_ = bt;
971 return bt;
972 }
973
974 if (this->forward_declaration_type() != NULL
975 || this->named_type() != NULL)
976 return this->get_btype_without_hash(gogo);
977
978 if (this->is_error_type())
979 return gogo->backend()->error_type();
980
981 // To avoid confusing the backend, translate all identical Go types
982 // to the same backend representation. We use a hash table to do
983 // that. There is no need to use the hash table for named types, as
984 // named types are only identical to themselves.
985
986 std::pair<Type*, Type_btype_entry> val;
987 val.first = this;
988 val.second.btype = NULL;
989 val.second.is_placeholder = false;
990 std::pair<Type_btypes::iterator, bool> ins =
991 Type::type_btypes.insert(val);
992 if (!ins.second && ins.first->second.btype != NULL)
993 {
994 // Note that GOGO can be NULL here, but only when the GCC
995 // middle-end is asking for a frontend type. That will only
996 // happen for simple types, which should never require
997 // placeholders.
998 if (!ins.first->second.is_placeholder)
999 this->btype_ = ins.first->second.btype;
1000 else if (gogo->named_types_are_converted())
1001 {
1002 this->finish_backend(gogo, ins.first->second.btype);
1003 ins.first->second.is_placeholder = false;
1004 }
1005
1006 // We set the has_padding field of a Struct_type when we convert
1007 // to the backend type, so if we have multiple Struct_type's
1008 // mapping to the same backend type we need to copy the
1009 // has_padding field. FIXME: This is awkward. We shouldn't
1010 // really change the type when setting the backend type, but
1011 // there isn't any other good time to add the padding field.
1012 if (ins.first->first->struct_type() != NULL
1013 && ins.first->first->struct_type()->has_padding())
1014 this->struct_type()->set_has_padding();
1015
1016 return ins.first->second.btype;
1017 }
1018
1019 Btype* bt = this->get_btype_without_hash(gogo);
1020
1021 if (ins.first->second.btype == NULL)
1022 {
1023 ins.first->second.btype = bt;
1024 ins.first->second.is_placeholder = false;
1025 }
1026 else
1027 {
1028 // We have already created a backend representation for this
1029 // type. This can happen when an unnamed type is defined using
1030 // a named type which in turns uses an identical unnamed type.
1031 // Use the representation we created earlier and ignore the one we just
1032 // built.
1033 if (this->btype_ == bt)
1034 this->btype_ = ins.first->second.btype;
1035 bt = ins.first->second.btype;
1036 }
1037
1038 return bt;
1039 }
1040
1041 // Return the backend representation for a type without looking in the
1042 // hash table for identical types. This is used for named types,
1043 // since a named type is never identical to any other type.
1044
1045 Btype*
1046 Type::get_btype_without_hash(Gogo* gogo)
1047 {
1048 if (this->btype_ == NULL)
1049 {
1050 Btype* bt = this->do_get_backend(gogo);
1051
1052 // For a recursive function or pointer type, we will temporarily
1053 // return a circular pointer type during the recursion. We
1054 // don't want to record that for a forwarding type, as it may
1055 // confuse us later.
1056 if (this->forward_declaration_type() != NULL
1057 && gogo->backend()->is_circular_pointer_type(bt))
1058 return bt;
1059
1060 if (gogo == NULL || !gogo->named_types_are_converted())
1061 return bt;
1062
1063 this->btype_ = bt;
1064 }
1065 return this->btype_;
1066 }
1067
1068 // Get the backend representation of a type without forcing the
1069 // creation of the backend representation of all supporting types.
1070 // This will return a backend type that has the correct size but may
1071 // be incomplete. E.g., a pointer will just be a placeholder pointer,
1072 // and will not contain the final representation of the type to which
1073 // it points. This is used while converting all named types to the
1074 // backend representation, to avoid problems with indirect references
1075 // to types which are not yet complete. When this is called, the
1076 // sizes of all direct references (e.g., a struct field) should be
1077 // known, but the sizes of indirect references (e.g., the type to
1078 // which a pointer points) may not.
1079
1080 Btype*
1081 Type::get_backend_placeholder(Gogo* gogo)
1082 {
1083 if (gogo->named_types_are_converted())
1084 return this->get_backend(gogo);
1085 if (this->btype_ != NULL)
1086 return this->btype_;
1087
1088 Btype* bt;
1089 switch (this->classification_)
1090 {
1091 case TYPE_ERROR:
1092 case TYPE_VOID:
1093 case TYPE_BOOLEAN:
1094 case TYPE_INTEGER:
1095 case TYPE_FLOAT:
1096 case TYPE_COMPLEX:
1097 case TYPE_STRING:
1098 case TYPE_NIL:
1099 // These are simple types that can just be created directly.
1100 return this->get_backend(gogo);
1101
1102 case TYPE_MAP:
1103 case TYPE_CHANNEL:
1104 // All maps and channels have the same backend representation.
1105 return this->get_backend(gogo);
1106
1107 case TYPE_NAMED:
1108 case TYPE_FORWARD:
1109 // Named types keep track of their own dependencies and manage
1110 // their own placeholders.
1111 if (this->named_type() != NULL && this->named_type()->is_alias())
1112 return this->unalias()->get_backend_placeholder(gogo);
1113 return this->get_backend(gogo);
1114
1115 case TYPE_INTERFACE:
1116 if (this->interface_type()->is_empty())
1117 return Interface_type::get_backend_empty_interface_type(gogo);
1118 break;
1119
1120 default:
1121 break;
1122 }
1123
1124 std::pair<Type*, Type_btype_entry> val;
1125 val.first = this;
1126 val.second.btype = NULL;
1127 val.second.is_placeholder = false;
1128 std::pair<Type_btypes::iterator, bool> ins =
1129 Type::type_btypes.insert(val);
1130 if (!ins.second && ins.first->second.btype != NULL)
1131 return ins.first->second.btype;
1132
1133 switch (this->classification_)
1134 {
1135 case TYPE_FUNCTION:
1136 {
1137 // A Go function type is a pointer to a struct type.
1138 Location loc = this->function_type()->location();
1139 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1140 }
1141 break;
1142
1143 case TYPE_POINTER:
1144 {
1145 Location loc = Linemap::unknown_location();
1146 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1147 Pointer_type* pt = this->convert<Pointer_type, TYPE_POINTER>();
1148 Type::placeholder_pointers.push_back(pt);
1149 }
1150 break;
1151
1152 case TYPE_STRUCT:
1153 // We don't have to make the struct itself be a placeholder. We
1154 // are promised that we know the sizes of the struct fields.
1155 // But we may have to use a placeholder for any particular
1156 // struct field.
1157 {
1158 std::vector<Backend::Btyped_identifier> bfields;
1159 get_backend_struct_fields(gogo, this->struct_type(), true, &bfields);
1160 bt = gogo->backend()->struct_type(bfields);
1161 }
1162 break;
1163
1164 case TYPE_ARRAY:
1165 if (this->is_slice_type())
1166 {
1167 std::vector<Backend::Btyped_identifier> bfields;
1168 get_backend_slice_fields(gogo, this->array_type(), true, &bfields);
1169 bt = gogo->backend()->struct_type(bfields);
1170 }
1171 else
1172 {
1173 Btype* element = this->array_type()->get_backend_element(gogo, true);
1174 Bexpression* len = this->array_type()->get_backend_length(gogo);
1175 bt = gogo->backend()->array_type(element, len);
1176 }
1177 break;
1178
1179 case TYPE_INTERFACE:
1180 {
1181 go_assert(!this->interface_type()->is_empty());
1182 std::vector<Backend::Btyped_identifier> bfields;
1183 get_backend_interface_fields(gogo, this->interface_type(), true,
1184 &bfields);
1185 bt = gogo->backend()->struct_type(bfields);
1186 }
1187 break;
1188
1189 case TYPE_SINK:
1190 case TYPE_CALL_MULTIPLE_RESULT:
1191 /* Note that various classifications were handled in the earlier
1192 switch. */
1193 default:
1194 go_unreachable();
1195 }
1196
1197 if (ins.first->second.btype == NULL)
1198 {
1199 ins.first->second.btype = bt;
1200 ins.first->second.is_placeholder = true;
1201 }
1202 else
1203 {
1204 // A placeholder for this type got created along the way. Use
1205 // that one and ignore the one we just built.
1206 bt = ins.first->second.btype;
1207 }
1208
1209 return bt;
1210 }
1211
1212 // Complete the backend representation. This is called for a type
1213 // using a placeholder type.
1214
1215 void
1216 Type::finish_backend(Gogo* gogo, Btype *placeholder)
1217 {
1218 switch (this->classification_)
1219 {
1220 case TYPE_ERROR:
1221 case TYPE_VOID:
1222 case TYPE_BOOLEAN:
1223 case TYPE_INTEGER:
1224 case TYPE_FLOAT:
1225 case TYPE_COMPLEX:
1226 case TYPE_STRING:
1227 case TYPE_NIL:
1228 go_unreachable();
1229
1230 case TYPE_FUNCTION:
1231 {
1232 Btype* bt = this->do_get_backend(gogo);
1233 if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1234 go_assert(saw_errors());
1235 }
1236 break;
1237
1238 case TYPE_POINTER:
1239 {
1240 Btype* bt = this->do_get_backend(gogo);
1241 if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1242 go_assert(saw_errors());
1243 }
1244 break;
1245
1246 case TYPE_STRUCT:
1247 // The struct type itself is done, but we have to make sure that
1248 // all the field types are converted.
1249 this->struct_type()->finish_backend_fields(gogo);
1250 break;
1251
1252 case TYPE_ARRAY:
1253 // The array type itself is done, but make sure the element type
1254 // is converted.
1255 this->array_type()->finish_backend_element(gogo);
1256 break;
1257
1258 case TYPE_MAP:
1259 case TYPE_CHANNEL:
1260 go_unreachable();
1261
1262 case TYPE_INTERFACE:
1263 // The interface type itself is done, but make sure the method
1264 // types are converted.
1265 this->interface_type()->finish_backend_methods(gogo);
1266 break;
1267
1268 case TYPE_NAMED:
1269 case TYPE_FORWARD:
1270 go_unreachable();
1271
1272 case TYPE_SINK:
1273 case TYPE_CALL_MULTIPLE_RESULT:
1274 default:
1275 go_unreachable();
1276 }
1277
1278 this->btype_ = placeholder;
1279 }
1280
1281 // Return a pointer to the type descriptor for this type.
1282
1283 Bexpression*
1284 Type::type_descriptor_pointer(Gogo* gogo, Location location)
1285 {
1286 Type* t = this->unalias();
1287 if (t->type_descriptor_var_ == NULL)
1288 {
1289 t->make_type_descriptor_var(gogo);
1290 go_assert(t->type_descriptor_var_ != NULL);
1291 }
1292 Bexpression* var_expr =
1293 gogo->backend()->var_expression(t->type_descriptor_var_, location);
1294 Bexpression* var_addr =
1295 gogo->backend()->address_expression(var_expr, location);
1296 Type* td_type = Type::make_type_descriptor_type();
1297 Btype* td_btype = td_type->get_backend(gogo);
1298 Btype* ptd_btype = gogo->backend()->pointer_type(td_btype);
1299 return gogo->backend()->convert_expression(ptd_btype, var_addr, location);
1300 }
1301
1302 // A mapping from unnamed types to type descriptor variables.
1303
1304 Type::Type_descriptor_vars Type::type_descriptor_vars;
1305
1306 // Build the type descriptor for this type.
1307
1308 void
1309 Type::make_type_descriptor_var(Gogo* gogo)
1310 {
1311 go_assert(this->type_descriptor_var_ == NULL);
1312
1313 Named_type* nt = this->named_type();
1314
1315 // We can have multiple instances of unnamed types, but we only want
1316 // to emit the type descriptor once. We use a hash table. This is
1317 // not necessary for named types, as they are unique, and we store
1318 // the type descriptor in the type itself.
1319 Bvariable** phash = NULL;
1320 if (nt == NULL)
1321 {
1322 Bvariable* bvnull = NULL;
1323 std::pair<Type_descriptor_vars::iterator, bool> ins =
1324 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1325 if (!ins.second)
1326 {
1327 // We've already built a type descriptor for this type.
1328 this->type_descriptor_var_ = ins.first->second;
1329 return;
1330 }
1331 phash = &ins.first->second;
1332 }
1333
1334 // The type descriptor symbol for the unsafe.Pointer type is defined in
1335 // libgo/go-unsafe-pointer.c, so we just return a reference to that
1336 // symbol if necessary.
1337 if (this->is_unsafe_pointer_type())
1338 {
1339 Location bloc = Linemap::predeclared_location();
1340
1341 Type* td_type = Type::make_type_descriptor_type();
1342 Btype* td_btype = td_type->get_backend(gogo);
1343 std::string name = gogo->type_descriptor_name(this, nt);
1344 std::string asm_name(go_selectively_encode_id(name));
1345 this->type_descriptor_var_ =
1346 gogo->backend()->immutable_struct_reference(name, asm_name,
1347 td_btype,
1348 bloc);
1349
1350 if (phash != NULL)
1351 *phash = this->type_descriptor_var_;
1352 return;
1353 }
1354
1355 std::string var_name = gogo->type_descriptor_name(this, nt);
1356
1357 // Build the contents of the type descriptor.
1358 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1359
1360 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1361
1362 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1363
1364 const Package* dummy;
1365 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1366 {
1367 std::string asm_name(go_selectively_encode_id(var_name));
1368 this->type_descriptor_var_ =
1369 gogo->backend()->immutable_struct_reference(var_name, asm_name,
1370 initializer_btype,
1371 loc);
1372 if (phash != NULL)
1373 *phash = this->type_descriptor_var_;
1374 return;
1375 }
1376
1377 // See if this type descriptor can appear in multiple packages.
1378 bool is_common = false;
1379 if (nt != NULL)
1380 {
1381 // We create the descriptor for a builtin type whenever we need
1382 // it.
1383 is_common = nt->is_builtin();
1384 }
1385 else
1386 {
1387 // This is an unnamed type. The descriptor could be defined in
1388 // any package where it is needed, and the linker will pick one
1389 // descriptor to keep.
1390 is_common = true;
1391 }
1392
1393 // We are going to build the type descriptor in this package. We
1394 // must create the variable before we convert the initializer to the
1395 // backend representation, because the initializer may refer to the
1396 // type descriptor of this type. By setting type_descriptor_var_ we
1397 // ensure that type_descriptor_pointer will work if called while
1398 // converting INITIALIZER.
1399
1400 std::string asm_name(go_selectively_encode_id(var_name));
1401 this->type_descriptor_var_ =
1402 gogo->backend()->immutable_struct(var_name, asm_name, false, is_common,
1403 initializer_btype, loc);
1404 if (phash != NULL)
1405 *phash = this->type_descriptor_var_;
1406
1407 Translate_context context(gogo, NULL, NULL, NULL);
1408 context.set_is_const();
1409 Bexpression* binitializer = initializer->get_backend(&context);
1410
1411 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1412 var_name, false, is_common,
1413 initializer_btype, loc,
1414 binitializer);
1415 }
1416
1417 // Return true if this type descriptor is defined in a different
1418 // package. If this returns true it sets *PACKAGE to the package.
1419
1420 bool
1421 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1422 const Package** package)
1423 {
1424 if (nt != NULL)
1425 {
1426 if (nt->named_object()->package() != NULL)
1427 {
1428 // This is a named type defined in a different package. The
1429 // type descriptor should be defined in that package.
1430 *package = nt->named_object()->package();
1431 return true;
1432 }
1433 }
1434 else
1435 {
1436 if (this->points_to() != NULL
1437 && this->points_to()->named_type() != NULL
1438 && this->points_to()->named_type()->named_object()->package() != NULL)
1439 {
1440 // This is an unnamed pointer to a named type defined in a
1441 // different package. The descriptor should be defined in
1442 // that package.
1443 *package = this->points_to()->named_type()->named_object()->package();
1444 return true;
1445 }
1446 }
1447 return false;
1448 }
1449
1450 // Return a composite literal for a type descriptor.
1451
1452 Expression*
1453 Type::type_descriptor(Gogo* gogo, Type* type)
1454 {
1455 return type->do_type_descriptor(gogo, NULL);
1456 }
1457
1458 // Return a composite literal for a type descriptor with a name.
1459
1460 Expression*
1461 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1462 {
1463 go_assert(name != NULL && type->named_type() != name);
1464 return type->do_type_descriptor(gogo, name);
1465 }
1466
1467 // Make a builtin struct type from a list of fields. The fields are
1468 // pairs of a name and a type.
1469
1470 Struct_type*
1471 Type::make_builtin_struct_type(int nfields, ...)
1472 {
1473 va_list ap;
1474 va_start(ap, nfields);
1475
1476 Location bloc = Linemap::predeclared_location();
1477 Struct_field_list* sfl = new Struct_field_list();
1478 for (int i = 0; i < nfields; i++)
1479 {
1480 const char* field_name = va_arg(ap, const char *);
1481 Type* type = va_arg(ap, Type*);
1482 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1483 }
1484
1485 va_end(ap);
1486
1487 Struct_type* ret = Type::make_struct_type(sfl, bloc);
1488 ret->set_is_struct_incomparable();
1489 return ret;
1490 }
1491
1492 // A list of builtin named types.
1493
1494 std::vector<Named_type*> Type::named_builtin_types;
1495
1496 // Make a builtin named type.
1497
1498 Named_type*
1499 Type::make_builtin_named_type(const char* name, Type* type)
1500 {
1501 Location bloc = Linemap::predeclared_location();
1502 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1503 Named_type* ret = no->type_value();
1504 Type::named_builtin_types.push_back(ret);
1505 return ret;
1506 }
1507
1508 // Convert the named builtin types.
1509
1510 void
1511 Type::convert_builtin_named_types(Gogo* gogo)
1512 {
1513 for (std::vector<Named_type*>::const_iterator p =
1514 Type::named_builtin_types.begin();
1515 p != Type::named_builtin_types.end();
1516 ++p)
1517 {
1518 bool r = (*p)->verify();
1519 go_assert(r);
1520 (*p)->convert(gogo);
1521 }
1522 }
1523
1524 // Return the type of a type descriptor. We should really tie this to
1525 // runtime.Type rather than copying it. This must match the struct "_type"
1526 // declared in libgo/go/runtime/type.go.
1527
1528 Type*
1529 Type::make_type_descriptor_type()
1530 {
1531 static Type* ret;
1532 if (ret == NULL)
1533 {
1534 Location bloc = Linemap::predeclared_location();
1535
1536 Type* uint8_type = Type::lookup_integer_type("uint8");
1537 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
1538 Type* uint32_type = Type::lookup_integer_type("uint32");
1539 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1540 Type* string_type = Type::lookup_string_type();
1541 Type* pointer_string_type = Type::make_pointer_type(string_type);
1542
1543 // This is an unnamed version of unsafe.Pointer. Perhaps we
1544 // should use the named version instead, although that would
1545 // require us to create the unsafe package if it has not been
1546 // imported. It probably doesn't matter.
1547 Type* void_type = Type::make_void_type();
1548 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1549
1550 Typed_identifier_list *params = new Typed_identifier_list();
1551 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1552 params->push_back(Typed_identifier("seed", uintptr_type, bloc));
1553
1554 Typed_identifier_list* results = new Typed_identifier_list();
1555 results->push_back(Typed_identifier("", uintptr_type, bloc));
1556
1557 Type* hash_fntype = Type::make_function_type(NULL, params, results,
1558 bloc);
1559
1560 params = new Typed_identifier_list();
1561 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1562 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1563
1564 results = new Typed_identifier_list();
1565 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1566
1567 Type* equal_fntype = Type::make_function_type(NULL, params, results,
1568 bloc);
1569
1570 // Forward declaration for the type descriptor type.
1571 Named_object* named_type_descriptor_type =
1572 Named_object::make_type_declaration("_type", NULL, bloc);
1573 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1574 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1575
1576 // The type of a method on a concrete type.
1577 Struct_type* method_type =
1578 Type::make_builtin_struct_type(5,
1579 "name", pointer_string_type,
1580 "pkgPath", pointer_string_type,
1581 "mtyp", pointer_type_descriptor_type,
1582 "typ", pointer_type_descriptor_type,
1583 "tfn", unsafe_pointer_type);
1584 Named_type* named_method_type =
1585 Type::make_builtin_named_type("method", method_type);
1586
1587 // Information for types with a name or methods.
1588 Type* slice_named_method_type =
1589 Type::make_array_type(named_method_type, NULL);
1590 Struct_type* uncommon_type =
1591 Type::make_builtin_struct_type(3,
1592 "name", pointer_string_type,
1593 "pkgPath", pointer_string_type,
1594 "methods", slice_named_method_type);
1595 Named_type* named_uncommon_type =
1596 Type::make_builtin_named_type("uncommonType", uncommon_type);
1597
1598 Type* pointer_uncommon_type =
1599 Type::make_pointer_type(named_uncommon_type);
1600
1601 // The type descriptor type.
1602
1603 Struct_type* type_descriptor_type =
1604 Type::make_builtin_struct_type(12,
1605 "size", uintptr_type,
1606 "ptrdata", uintptr_type,
1607 "hash", uint32_type,
1608 "kind", uint8_type,
1609 "align", uint8_type,
1610 "fieldAlign", uint8_type,
1611 "hashfn", hash_fntype,
1612 "equalfn", equal_fntype,
1613 "gcdata", pointer_uint8_type,
1614 "string", pointer_string_type,
1615 "", pointer_uncommon_type,
1616 "ptrToThis",
1617 pointer_type_descriptor_type);
1618
1619 Named_type* named = Type::make_builtin_named_type("_type",
1620 type_descriptor_type);
1621
1622 named_type_descriptor_type->set_type_value(named);
1623
1624 ret = named;
1625 }
1626
1627 return ret;
1628 }
1629
1630 // Make the type of a pointer to a type descriptor as represented in
1631 // Go.
1632
1633 Type*
1634 Type::make_type_descriptor_ptr_type()
1635 {
1636 static Type* ret;
1637 if (ret == NULL)
1638 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1639 return ret;
1640 }
1641
1642 // Return the alignment required by the memequalN function. N is a
1643 // type size: 16, 32, 64, or 128. The memequalN functions are defined
1644 // in libgo/go/runtime/alg.go.
1645
1646 int64_t
1647 Type::memequal_align(Gogo* gogo, int size)
1648 {
1649 const char* tn;
1650 switch (size)
1651 {
1652 case 16:
1653 tn = "int16";
1654 break;
1655 case 32:
1656 tn = "int32";
1657 break;
1658 case 64:
1659 tn = "int64";
1660 break;
1661 case 128:
1662 // The code uses [2]int64, which must have the same alignment as
1663 // int64.
1664 tn = "int64";
1665 break;
1666 default:
1667 go_unreachable();
1668 }
1669
1670 Type* t = Type::lookup_integer_type(tn);
1671
1672 int64_t ret;
1673 if (!t->backend_type_align(gogo, &ret))
1674 go_unreachable();
1675 return ret;
1676 }
1677
1678 // Return whether this type needs specially built type functions.
1679 // This returns true for types that are comparable and either can not
1680 // use an identity comparison, or are a non-standard size.
1681
1682 bool
1683 Type::needs_specific_type_functions(Gogo* gogo)
1684 {
1685 Named_type* nt = this->named_type();
1686 if (nt != NULL && nt->is_alias())
1687 return false;
1688 if (!this->is_comparable())
1689 return false;
1690 if (!this->compare_is_identity(gogo))
1691 return true;
1692
1693 // We create a few predeclared types for type descriptors; they are
1694 // really just for the backend and don't need hash or equality
1695 // functions.
1696 if (nt != NULL && Linemap::is_predeclared_location(nt->location()))
1697 return false;
1698
1699 int64_t size, align;
1700 if (!this->backend_type_size(gogo, &size)
1701 || !this->backend_type_align(gogo, &align))
1702 {
1703 go_assert(saw_errors());
1704 return false;
1705 }
1706 // This switch matches the one in Type::type_functions.
1707 switch (size)
1708 {
1709 case 0:
1710 case 1:
1711 case 2:
1712 return align < Type::memequal_align(gogo, 16);
1713 case 4:
1714 return align < Type::memequal_align(gogo, 32);
1715 case 8:
1716 return align < Type::memequal_align(gogo, 64);
1717 case 16:
1718 return align < Type::memequal_align(gogo, 128);
1719 default:
1720 return true;
1721 }
1722 }
1723
1724 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1725 // hash code for this type and which compare whether two values of
1726 // this type are equal. If NAME is not NULL it is the name of this
1727 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1728 // functions, for convenience; they may be NULL.
1729
1730 void
1731 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1732 Function_type* equal_fntype, Named_object** hash_fn,
1733 Named_object** equal_fn)
1734 {
1735 // If the unaliased type is not a named type, then the type does not
1736 // have a name after all.
1737 if (name != NULL)
1738 name = name->unalias()->named_type();
1739
1740 if (!this->is_comparable())
1741 {
1742 *hash_fn = NULL;
1743 *equal_fn = NULL;
1744 return;
1745 }
1746
1747 if (hash_fntype == NULL || equal_fntype == NULL)
1748 {
1749 Location bloc = Linemap::predeclared_location();
1750
1751 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1752 Type* void_type = Type::make_void_type();
1753 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1754
1755 if (hash_fntype == NULL)
1756 {
1757 Typed_identifier_list* params = new Typed_identifier_list();
1758 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1759 bloc));
1760 params->push_back(Typed_identifier("seed", uintptr_type, bloc));
1761
1762 Typed_identifier_list* results = new Typed_identifier_list();
1763 results->push_back(Typed_identifier("", uintptr_type, bloc));
1764
1765 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1766 }
1767 if (equal_fntype == NULL)
1768 {
1769 Typed_identifier_list* params = new Typed_identifier_list();
1770 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1771 bloc));
1772 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1773 bloc));
1774
1775 Typed_identifier_list* results = new Typed_identifier_list();
1776 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1777 bloc));
1778
1779 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1780 }
1781 }
1782
1783 const char* hash_fnname;
1784 const char* equal_fnname;
1785 if (this->compare_is_identity(gogo))
1786 {
1787 int64_t size, align;
1788 if (!this->backend_type_size(gogo, &size)
1789 || !this->backend_type_align(gogo, &align))
1790 {
1791 go_assert(saw_errors());
1792 return;
1793 }
1794 bool build_functions = false;
1795 // This switch matches the one in Type::needs_specific_type_functions.
1796 // The alignment tests are because of the memequal functions,
1797 // which assume that the values are aligned as required for an
1798 // integer of that size.
1799 switch (size)
1800 {
1801 case 0:
1802 hash_fnname = "runtime.memhash0";
1803 equal_fnname = "runtime.memequal0";
1804 break;
1805 case 1:
1806 hash_fnname = "runtime.memhash8";
1807 equal_fnname = "runtime.memequal8";
1808 break;
1809 case 2:
1810 if (align < Type::memequal_align(gogo, 16))
1811 build_functions = true;
1812 else
1813 {
1814 hash_fnname = "runtime.memhash16";
1815 equal_fnname = "runtime.memequal16";
1816 }
1817 break;
1818 case 4:
1819 if (align < Type::memequal_align(gogo, 32))
1820 build_functions = true;
1821 else
1822 {
1823 hash_fnname = "runtime.memhash32";
1824 equal_fnname = "runtime.memequal32";
1825 }
1826 break;
1827 case 8:
1828 if (align < Type::memequal_align(gogo, 64))
1829 build_functions = true;
1830 else
1831 {
1832 hash_fnname = "runtime.memhash64";
1833 equal_fnname = "runtime.memequal64";
1834 }
1835 break;
1836 case 16:
1837 if (align < Type::memequal_align(gogo, 128))
1838 build_functions = true;
1839 else
1840 {
1841 hash_fnname = "runtime.memhash128";
1842 equal_fnname = "runtime.memequal128";
1843 }
1844 break;
1845 default:
1846 build_functions = true;
1847 break;
1848 }
1849 if (build_functions)
1850 {
1851 // We don't have a built-in function for a type of this size
1852 // and alignment. Build a function to use that calls the
1853 // generic hash/equality functions for identity, passing the size.
1854 this->specific_type_functions(gogo, name, size, hash_fntype,
1855 equal_fntype, hash_fn, equal_fn);
1856 return;
1857 }
1858 }
1859 else
1860 {
1861 switch (this->base()->classification())
1862 {
1863 case Type::TYPE_ERROR:
1864 case Type::TYPE_VOID:
1865 case Type::TYPE_NIL:
1866 case Type::TYPE_FUNCTION:
1867 case Type::TYPE_MAP:
1868 // For these types is_comparable should have returned false.
1869 go_unreachable();
1870
1871 case Type::TYPE_BOOLEAN:
1872 case Type::TYPE_INTEGER:
1873 case Type::TYPE_POINTER:
1874 case Type::TYPE_CHANNEL:
1875 // For these types compare_is_identity should have returned true.
1876 go_unreachable();
1877
1878 case Type::TYPE_FLOAT:
1879 switch (this->float_type()->bits())
1880 {
1881 case 32:
1882 hash_fnname = "runtime.f32hash";
1883 equal_fnname = "runtime.f32equal";
1884 break;
1885 case 64:
1886 hash_fnname = "runtime.f64hash";
1887 equal_fnname = "runtime.f64equal";
1888 break;
1889 default:
1890 go_unreachable();
1891 }
1892 break;
1893
1894 case Type::TYPE_COMPLEX:
1895 switch (this->complex_type()->bits())
1896 {
1897 case 64:
1898 hash_fnname = "runtime.c64hash";
1899 equal_fnname = "runtime.c64equal";
1900 break;
1901 case 128:
1902 hash_fnname = "runtime.c128hash";
1903 equal_fnname = "runtime.c128equal";
1904 break;
1905 default:
1906 go_unreachable();
1907 }
1908 break;
1909
1910 case Type::TYPE_STRING:
1911 hash_fnname = "runtime.strhash";
1912 equal_fnname = "runtime.strequal";
1913 break;
1914
1915 case Type::TYPE_STRUCT:
1916 {
1917 // This is a struct which can not be compared using a
1918 // simple identity function. We need to build a function
1919 // for comparison.
1920 this->specific_type_functions(gogo, name, -1, hash_fntype,
1921 equal_fntype, hash_fn, equal_fn);
1922 return;
1923 }
1924
1925 case Type::TYPE_ARRAY:
1926 if (this->is_slice_type())
1927 {
1928 // Type::is_compatible_for_comparison should have
1929 // returned false.
1930 go_unreachable();
1931 }
1932 else
1933 {
1934 // This is an array which can not be compared using a
1935 // simple identity function. We need to build a
1936 // function for comparison.
1937 this->specific_type_functions(gogo, name, -1, hash_fntype,
1938 equal_fntype, hash_fn, equal_fn);
1939 return;
1940 }
1941 break;
1942
1943 case Type::TYPE_INTERFACE:
1944 if (this->interface_type()->is_empty())
1945 {
1946 hash_fnname = "runtime.nilinterhash";
1947 equal_fnname = "runtime.nilinterequal";
1948 }
1949 else
1950 {
1951 hash_fnname = "runtime.interhash";
1952 equal_fnname = "runtime.interequal";
1953 }
1954 break;
1955
1956 case Type::TYPE_NAMED:
1957 case Type::TYPE_FORWARD:
1958 go_unreachable();
1959
1960 default:
1961 go_unreachable();
1962 }
1963 }
1964
1965
1966 Location bloc = Linemap::predeclared_location();
1967 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1968 hash_fntype, bloc);
1969 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1970 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1971 equal_fntype, bloc);
1972 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1973 }
1974
1975 // A hash table mapping types to the specific hash functions.
1976
1977 Type::Type_functions Type::type_functions_table;
1978
1979 // Handle a type function which is specific to a type: if SIZE == -1,
1980 // this is a struct or array that can not use an identity comparison.
1981 // Otherwise, it is a type that uses an identity comparison but is not
1982 // one of the standard supported sizes.
1983
1984 void
1985 Type::specific_type_functions(Gogo* gogo, Named_type* name, int64_t size,
1986 Function_type* hash_fntype,
1987 Function_type* equal_fntype,
1988 Named_object** hash_fn,
1989 Named_object** equal_fn)
1990 {
1991 Hash_equal_fn fnull(NULL, NULL);
1992 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1993 std::pair<Type_functions::iterator, bool> ins =
1994 Type::type_functions_table.insert(val);
1995 if (!ins.second)
1996 {
1997 // We already have functions for this type
1998 *hash_fn = ins.first->second.first;
1999 *equal_fn = ins.first->second.second;
2000 return;
2001 }
2002
2003 std::string hash_name;
2004 std::string equal_name;
2005 gogo->specific_type_function_names(this, name, &hash_name, &equal_name);
2006
2007 Location bloc = Linemap::predeclared_location();
2008
2009 const Package* package = NULL;
2010 bool is_defined_elsewhere =
2011 this->type_descriptor_defined_elsewhere(name, &package);
2012 if (is_defined_elsewhere)
2013 {
2014 *hash_fn = Named_object::make_function_declaration(hash_name, package,
2015 hash_fntype, bloc);
2016 *equal_fn = Named_object::make_function_declaration(equal_name, package,
2017 equal_fntype, bloc);
2018 }
2019 else
2020 {
2021 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
2022 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
2023 bloc);
2024 }
2025
2026 ins.first->second.first = *hash_fn;
2027 ins.first->second.second = *equal_fn;
2028
2029 if (!is_defined_elsewhere)
2030 {
2031 if (gogo->in_global_scope())
2032 this->write_specific_type_functions(gogo, name, size, hash_name,
2033 hash_fntype, equal_name,
2034 equal_fntype);
2035 else
2036 gogo->queue_specific_type_function(this, name, size, hash_name,
2037 hash_fntype, equal_name,
2038 equal_fntype);
2039 }
2040 }
2041
2042 // Write the hash and equality functions for a type which needs to be
2043 // written specially.
2044
2045 void
2046 Type::write_specific_type_functions(Gogo* gogo, Named_type* name, int64_t size,
2047 const std::string& hash_name,
2048 Function_type* hash_fntype,
2049 const std::string& equal_name,
2050 Function_type* equal_fntype)
2051 {
2052 Location bloc = Linemap::predeclared_location();
2053
2054 if (gogo->specific_type_functions_are_written())
2055 {
2056 go_assert(saw_errors());
2057 return;
2058 }
2059
2060 go_assert(this->is_comparable());
2061
2062 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
2063 bloc);
2064 hash_fn->func_value()->set_is_type_specific_function();
2065 gogo->start_block(bloc);
2066
2067 if (size != -1)
2068 this->write_identity_hash(gogo, size);
2069 else if (name != NULL && name->real_type()->named_type() != NULL)
2070 this->write_named_hash(gogo, name, hash_fntype, equal_fntype);
2071 else if (this->struct_type() != NULL)
2072 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
2073 equal_fntype);
2074 else if (this->array_type() != NULL)
2075 this->array_type()->write_hash_function(gogo, name, hash_fntype,
2076 equal_fntype);
2077 else
2078 go_unreachable();
2079
2080 Block* b = gogo->finish_block(bloc);
2081 gogo->add_block(b, bloc);
2082 gogo->lower_block(hash_fn, b);
2083 gogo->finish_function(bloc);
2084
2085 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
2086 false, bloc);
2087 equal_fn->func_value()->set_is_type_specific_function();
2088 gogo->start_block(bloc);
2089
2090 if (size != -1)
2091 this->write_identity_equal(gogo, size);
2092 else if (name != NULL && name->real_type()->named_type() != NULL)
2093 this->write_named_equal(gogo, name);
2094 else if (this->struct_type() != NULL)
2095 this->struct_type()->write_equal_function(gogo, name);
2096 else if (this->array_type() != NULL)
2097 this->array_type()->write_equal_function(gogo, name);
2098 else
2099 go_unreachable();
2100
2101 b = gogo->finish_block(bloc);
2102 gogo->add_block(b, bloc);
2103 gogo->lower_block(equal_fn, b);
2104 gogo->finish_function(bloc);
2105
2106 // Build the function descriptors for the type descriptor to refer to.
2107 hash_fn->func_value()->descriptor(gogo, hash_fn);
2108 equal_fn->func_value()->descriptor(gogo, equal_fn);
2109 }
2110
2111 // Write a hash function for a type that can use an identity hash but
2112 // is not one of the standard supported sizes. For example, this
2113 // would be used for the type [3]byte. This builds a return statement
2114 // that returns a call to the memhash function, passing the key and
2115 // seed from the function arguments (already constructed before this
2116 // is called), and the constant size.
2117
2118 void
2119 Type::write_identity_hash(Gogo* gogo, int64_t size)
2120 {
2121 Location bloc = Linemap::predeclared_location();
2122
2123 Type* unsafe_pointer_type = Type::make_pointer_type(Type::make_void_type());
2124 Type* uintptr_type = Type::lookup_integer_type("uintptr");
2125
2126 Typed_identifier_list* params = new Typed_identifier_list();
2127 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
2128 params->push_back(Typed_identifier("seed", uintptr_type, bloc));
2129 params->push_back(Typed_identifier("size", uintptr_type, bloc));
2130
2131 Typed_identifier_list* results = new Typed_identifier_list();
2132 results->push_back(Typed_identifier("", uintptr_type, bloc));
2133
2134 Function_type* memhash_fntype = Type::make_function_type(NULL, params,
2135 results, bloc);
2136
2137 Named_object* memhash =
2138 Named_object::make_function_declaration("runtime.memhash", NULL,
2139 memhash_fntype, bloc);
2140 memhash->func_declaration_value()->set_asm_name("runtime.memhash");
2141
2142 Named_object* key_arg = gogo->lookup("key", NULL);
2143 go_assert(key_arg != NULL);
2144 Named_object* seed_arg = gogo->lookup("seed", NULL);
2145 go_assert(seed_arg != NULL);
2146
2147 Expression* key_ref = Expression::make_var_reference(key_arg, bloc);
2148 Expression* seed_ref = Expression::make_var_reference(seed_arg, bloc);
2149 Expression* size_arg = Expression::make_integer_int64(size, uintptr_type,
2150 bloc);
2151 Expression_list* args = new Expression_list();
2152 args->push_back(key_ref);
2153 args->push_back(seed_ref);
2154 args->push_back(size_arg);
2155 Expression* func = Expression::make_func_reference(memhash, NULL, bloc);
2156 Expression* call = Expression::make_call(func, args, false, bloc);
2157
2158 Expression_list* vals = new Expression_list();
2159 vals->push_back(call);
2160 Statement* s = Statement::make_return_statement(vals, bloc);
2161 gogo->add_statement(s);
2162 }
2163
2164 // Write an equality function for a type that can use an identity
2165 // equality comparison but is not one of the standard supported sizes.
2166 // For example, this would be used for the type [3]byte. This builds
2167 // a return statement that returns a call to the memequal function,
2168 // passing the two keys from the function arguments (already
2169 // constructed before this is called), and the constant size.
2170
2171 void
2172 Type::write_identity_equal(Gogo* gogo, int64_t size)
2173 {
2174 Location bloc = Linemap::predeclared_location();
2175
2176 Type* unsafe_pointer_type = Type::make_pointer_type(Type::make_void_type());
2177 Type* uintptr_type = Type::lookup_integer_type("uintptr");
2178
2179 Typed_identifier_list* params = new Typed_identifier_list();
2180 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
2181 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
2182 params->push_back(Typed_identifier("size", uintptr_type, bloc));
2183
2184 Typed_identifier_list* results = new Typed_identifier_list();
2185 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
2186
2187 Function_type* memequal_fntype = Type::make_function_type(NULL, params,
2188 results, bloc);
2189
2190 Named_object* memequal =
2191 Named_object::make_function_declaration("runtime.memequal", NULL,
2192 memequal_fntype, bloc);
2193 memequal->func_declaration_value()->set_asm_name("runtime.memequal");
2194
2195 Named_object* key1_arg = gogo->lookup("key1", NULL);
2196 go_assert(key1_arg != NULL);
2197 Named_object* key2_arg = gogo->lookup("key2", NULL);
2198 go_assert(key2_arg != NULL);
2199
2200 Expression* key1_ref = Expression::make_var_reference(key1_arg, bloc);
2201 Expression* key2_ref = Expression::make_var_reference(key2_arg, bloc);
2202 Expression* size_arg = Expression::make_integer_int64(size, uintptr_type,
2203 bloc);
2204 Expression_list* args = new Expression_list();
2205 args->push_back(key1_ref);
2206 args->push_back(key2_ref);
2207 args->push_back(size_arg);
2208 Expression* func = Expression::make_func_reference(memequal, NULL, bloc);
2209 Expression* call = Expression::make_call(func, args, false, bloc);
2210
2211 Expression_list* vals = new Expression_list();
2212 vals->push_back(call);
2213 Statement* s = Statement::make_return_statement(vals, bloc);
2214 gogo->add_statement(s);
2215 }
2216
2217 // Write a hash function that simply calls the hash function for a
2218 // named type. This is used when one named type is defined as
2219 // another. This ensures that this case works when the other named
2220 // type is defined in another package and relies on calling hash
2221 // functions defined only in that package.
2222
2223 void
2224 Type::write_named_hash(Gogo* gogo, Named_type* name,
2225 Function_type* hash_fntype, Function_type* equal_fntype)
2226 {
2227 Location bloc = Linemap::predeclared_location();
2228
2229 Named_type* base_type = name->real_type()->named_type();
2230 while (base_type->is_alias())
2231 {
2232 base_type = base_type->real_type()->named_type();
2233 go_assert(base_type != NULL);
2234 }
2235 go_assert(base_type != NULL);
2236
2237 // The pointer to the type we are going to hash. This is an
2238 // unsafe.Pointer.
2239 Named_object* key_arg = gogo->lookup("key", NULL);
2240 go_assert(key_arg != NULL);
2241
2242 // The seed argument to the hash function.
2243 Named_object* seed_arg = gogo->lookup("seed", NULL);
2244 go_assert(seed_arg != NULL);
2245
2246 Named_object* hash_fn;
2247 Named_object* equal_fn;
2248 name->real_type()->type_functions(gogo, base_type, hash_fntype, equal_fntype,
2249 &hash_fn, &equal_fn);
2250
2251 // Call the hash function for the base type.
2252 Expression* key_ref = Expression::make_var_reference(key_arg, bloc);
2253 Expression* seed_ref = Expression::make_var_reference(seed_arg, bloc);
2254 Expression_list* args = new Expression_list();
2255 args->push_back(key_ref);
2256 args->push_back(seed_ref);
2257 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
2258 Expression* call = Expression::make_call(func, args, false, bloc);
2259
2260 // Return the hash of the base type.
2261 Expression_list* vals = new Expression_list();
2262 vals->push_back(call);
2263 Statement* s = Statement::make_return_statement(vals, bloc);
2264 gogo->add_statement(s);
2265 }
2266
2267 // Write an equality function that simply calls the equality function
2268 // for a named type. This is used when one named type is defined as
2269 // another. This ensures that this case works when the other named
2270 // type is defined in another package and relies on calling equality
2271 // functions defined only in that package.
2272
2273 void
2274 Type::write_named_equal(Gogo* gogo, Named_type* name)
2275 {
2276 Location bloc = Linemap::predeclared_location();
2277
2278 // The pointers to the types we are going to compare. These have
2279 // type unsafe.Pointer.
2280 Named_object* key1_arg = gogo->lookup("key1", NULL);
2281 Named_object* key2_arg = gogo->lookup("key2", NULL);
2282 go_assert(key1_arg != NULL && key2_arg != NULL);
2283
2284 Named_type* base_type = name->real_type()->named_type();
2285 go_assert(base_type != NULL);
2286
2287 // Build temporaries with the base type.
2288 Type* pt = Type::make_pointer_type(base_type);
2289
2290 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
2291 ref = Expression::make_cast(pt, ref, bloc);
2292 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
2293 gogo->add_statement(p1);
2294
2295 ref = Expression::make_var_reference(key2_arg, bloc);
2296 ref = Expression::make_cast(pt, ref, bloc);
2297 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
2298 gogo->add_statement(p2);
2299
2300 // Compare the values for equality.
2301 Expression* t1 = Expression::make_temporary_reference(p1, bloc);
2302 t1 = Expression::make_dereference(t1, Expression::NIL_CHECK_NOT_NEEDED, bloc);
2303
2304 Expression* t2 = Expression::make_temporary_reference(p2, bloc);
2305 t2 = Expression::make_dereference(t2, Expression::NIL_CHECK_NOT_NEEDED, bloc);
2306
2307 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, t1, t2, bloc);
2308
2309 // Return the equality comparison.
2310 Expression_list* vals = new Expression_list();
2311 vals->push_back(cond);
2312 Statement* s = Statement::make_return_statement(vals, bloc);
2313 gogo->add_statement(s);
2314 }
2315
2316 // Return whether this type is stored directly in an interface's
2317 // data word.
2318 //
2319 // Since finalize_methods runs before type checking, we may see a
2320 // malformed type like 'type T struct { x T }'. Use a visited map
2321 // to avoid infinite recursion.
2322
2323 bool
2324 Type::is_direct_iface_type() const
2325 {
2326 Unordered_set(const Type*) visited;
2327 return this->is_direct_iface_type_helper(&visited);
2328 }
2329
2330 bool
2331 Type::is_direct_iface_type_helper(Unordered_set(const Type*)* visited) const
2332 {
2333 if (this->points_to() != NULL
2334 || this->channel_type() != NULL
2335 || this->function_type() != NULL
2336 || this->map_type() != NULL)
2337 return true;
2338
2339 std::pair<Unordered_set(const Type*)::iterator, bool> ins
2340 = visited->insert(this);
2341 if (!ins.second)
2342 // malformed circular type
2343 return false;
2344
2345 const Struct_type* st = this->struct_type();
2346 if (st != NULL)
2347 return (st->field_count() == 1
2348 && st->field(0)->type()->is_direct_iface_type_helper(visited));
2349 const Array_type* at = this->array_type();
2350 if (at != NULL && !at->is_slice_type())
2351 {
2352 int64_t len;
2353 return (at->int_length(&len) && len == 1
2354 && at->element_type()->is_direct_iface_type_helper(visited));
2355 }
2356 return false;
2357 }
2358
2359 // Return a composite literal for the type descriptor for a plain type
2360 // of kind RUNTIME_TYPE_KIND named NAME.
2361
2362 Expression*
2363 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
2364 Named_type* name, const Methods* methods,
2365 bool only_value_methods)
2366 {
2367 Location bloc = Linemap::predeclared_location();
2368
2369 Type* td_type = Type::make_type_descriptor_type();
2370 const Struct_field_list* fields = td_type->struct_type()->fields();
2371
2372 Expression_list* vals = new Expression_list();
2373 vals->reserve(12);
2374
2375 if (!this->has_pointer())
2376 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
2377 if (this->is_direct_iface_type())
2378 runtime_type_kind |= RUNTIME_TYPE_KIND_DIRECT_IFACE;
2379 int64_t ptrsize;
2380 int64_t ptrdata;
2381 if (this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2382 runtime_type_kind |= RUNTIME_TYPE_KIND_GC_PROG;
2383
2384 Struct_field_list::const_iterator p = fields->begin();
2385 go_assert(p->is_field_name("size"));
2386 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
2387 vals->push_back(Expression::make_type_info(this, type_info));
2388
2389 ++p;
2390 go_assert(p->is_field_name("ptrdata"));
2391 type_info = Expression::TYPE_INFO_DESCRIPTOR_PTRDATA;
2392 vals->push_back(Expression::make_type_info(this, type_info));
2393
2394 ++p;
2395 go_assert(p->is_field_name("hash"));
2396 unsigned int h;
2397 if (name != NULL)
2398 h = name->hash_for_method(gogo, Type::COMPARE_TAGS);
2399 else
2400 h = this->hash_for_method(gogo, Type::COMPARE_TAGS);
2401 vals->push_back(Expression::make_integer_ul(h, p->type(), bloc));
2402
2403 ++p;
2404 go_assert(p->is_field_name("kind"));
2405 vals->push_back(Expression::make_integer_ul(runtime_type_kind, p->type(),
2406 bloc));
2407
2408 ++p;
2409 go_assert(p->is_field_name("align"));
2410 type_info = Expression::TYPE_INFO_ALIGNMENT;
2411 vals->push_back(Expression::make_type_info(this, type_info));
2412
2413 ++p;
2414 go_assert(p->is_field_name("fieldAlign"));
2415 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
2416 vals->push_back(Expression::make_type_info(this, type_info));
2417
2418 ++p;
2419 go_assert(p->is_field_name("hashfn"));
2420 Function_type* hash_fntype = p->type()->function_type();
2421
2422 ++p;
2423 go_assert(p->is_field_name("equalfn"));
2424 Function_type* equal_fntype = p->type()->function_type();
2425
2426 Named_object* hash_fn;
2427 Named_object* equal_fn;
2428 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
2429 &equal_fn);
2430 if (hash_fn == NULL)
2431 vals->push_back(Expression::make_cast(hash_fntype,
2432 Expression::make_nil(bloc),
2433 bloc));
2434 else
2435 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
2436 if (equal_fn == NULL)
2437 vals->push_back(Expression::make_cast(equal_fntype,
2438 Expression::make_nil(bloc),
2439 bloc));
2440 else
2441 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
2442
2443 ++p;
2444 go_assert(p->is_field_name("gcdata"));
2445 vals->push_back(Expression::make_gc_symbol(this));
2446
2447 ++p;
2448 go_assert(p->is_field_name("string"));
2449 Expression* s = Expression::make_string((name != NULL
2450 ? name->reflection(gogo)
2451 : this->reflection(gogo)),
2452 bloc);
2453 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2454
2455 ++p;
2456 go_assert(p->is_field_name("uncommonType"));
2457 if (name == NULL && methods == NULL)
2458 vals->push_back(Expression::make_nil(bloc));
2459 else
2460 {
2461 if (methods == NULL)
2462 methods = name->methods();
2463 vals->push_back(this->uncommon_type_constructor(gogo,
2464 p->type()->deref(),
2465 name, methods,
2466 only_value_methods));
2467 }
2468
2469 ++p;
2470 go_assert(p->is_field_name("ptrToThis"));
2471 if (name == NULL && methods == NULL)
2472 vals->push_back(Expression::make_nil(bloc));
2473 else
2474 {
2475 Type* pt;
2476 if (name != NULL)
2477 pt = Type::make_pointer_type(name);
2478 else
2479 pt = Type::make_pointer_type(this);
2480 vals->push_back(Expression::make_type_descriptor(pt, bloc));
2481 }
2482
2483 ++p;
2484 go_assert(p == fields->end());
2485
2486 return Expression::make_struct_composite_literal(td_type, vals, bloc);
2487 }
2488
2489 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2490 // length used by the gc toolchain, and also appears in
2491 // libgo/go/reflect/type.go.
2492
2493 static const int64_t max_ptrmask_bytes = 2048;
2494
2495 // Return a pointer to the Garbage Collection information for this type.
2496
2497 Bexpression*
2498 Type::gc_symbol_pointer(Gogo* gogo)
2499 {
2500 Type* t = this->unalias();
2501
2502 if (!t->has_pointer())
2503 return gogo->backend()->nil_pointer_expression();
2504
2505 if (t->gc_symbol_var_ == NULL)
2506 {
2507 t->make_gc_symbol_var(gogo);
2508 go_assert(t->gc_symbol_var_ != NULL);
2509 }
2510 Location bloc = Linemap::predeclared_location();
2511 Bexpression* var_expr =
2512 gogo->backend()->var_expression(t->gc_symbol_var_, bloc);
2513 Bexpression* addr_expr =
2514 gogo->backend()->address_expression(var_expr, bloc);
2515
2516 Type* uint8_type = Type::lookup_integer_type("uint8");
2517 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
2518 Btype* ubtype = pointer_uint8_type->get_backend(gogo);
2519 return gogo->backend()->convert_expression(ubtype, addr_expr, bloc);
2520 }
2521
2522 // A mapping from unnamed types to GC symbol variables.
2523
2524 Type::GC_symbol_vars Type::gc_symbol_vars;
2525
2526 // Build the GC symbol for this type.
2527
2528 void
2529 Type::make_gc_symbol_var(Gogo* gogo)
2530 {
2531 go_assert(this->gc_symbol_var_ == NULL);
2532
2533 Named_type* nt = this->named_type();
2534
2535 // We can have multiple instances of unnamed types and similar to type
2536 // descriptors, we only want to the emit the GC data once, so we use a
2537 // hash table.
2538 Bvariable** phash = NULL;
2539 if (nt == NULL)
2540 {
2541 Bvariable* bvnull = NULL;
2542 std::pair<GC_symbol_vars::iterator, bool> ins =
2543 Type::gc_symbol_vars.insert(std::make_pair(this, bvnull));
2544 if (!ins.second)
2545 {
2546 // We've already built a gc symbol for this type.
2547 this->gc_symbol_var_ = ins.first->second;
2548 return;
2549 }
2550 phash = &ins.first->second;
2551 }
2552
2553 int64_t ptrsize;
2554 int64_t ptrdata;
2555 if (!this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2556 {
2557 this->gc_symbol_var_ = this->gc_ptrmask_var(gogo, ptrsize, ptrdata);
2558 if (phash != NULL)
2559 *phash = this->gc_symbol_var_;
2560 return;
2561 }
2562
2563 std::string sym_name = gogo->gc_symbol_name(this);
2564
2565 // Build the contents of the gc symbol.
2566 Expression* sym_init = this->gcprog_constructor(gogo, ptrsize, ptrdata);
2567 Btype* sym_btype = sym_init->type()->get_backend(gogo);
2568
2569 // If the type descriptor for this type is defined somewhere else, so is the
2570 // GC symbol.
2571 const Package* dummy;
2572 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
2573 {
2574 std::string asm_name(go_selectively_encode_id(sym_name));
2575 this->gc_symbol_var_ =
2576 gogo->backend()->implicit_variable_reference(sym_name, asm_name,
2577 sym_btype);
2578 if (phash != NULL)
2579 *phash = this->gc_symbol_var_;
2580 return;
2581 }
2582
2583 // See if this gc symbol can appear in multiple packages.
2584 bool is_common = false;
2585 if (nt != NULL)
2586 {
2587 // We create the symbol for a builtin type whenever we need
2588 // it.
2589 is_common = nt->is_builtin();
2590 }
2591 else
2592 {
2593 // This is an unnamed type. The descriptor could be defined in
2594 // any package where it is needed, and the linker will pick one
2595 // descriptor to keep.
2596 is_common = true;
2597 }
2598
2599 // Since we are building the GC symbol in this package, we must create the
2600 // variable before converting the initializer to its backend representation
2601 // because the initializer may refer to the GC symbol for this type.
2602 std::string asm_name(go_selectively_encode_id(sym_name));
2603 this->gc_symbol_var_ =
2604 gogo->backend()->implicit_variable(sym_name, asm_name,
2605 sym_btype, false, true, is_common, 0);
2606 if (phash != NULL)
2607 *phash = this->gc_symbol_var_;
2608
2609 Translate_context context(gogo, NULL, NULL, NULL);
2610 context.set_is_const();
2611 Bexpression* sym_binit = sym_init->get_backend(&context);
2612 gogo->backend()->implicit_variable_set_init(this->gc_symbol_var_, sym_name,
2613 sym_btype, false, true, is_common,
2614 sym_binit);
2615 }
2616
2617 // Return whether this type needs a GC program, and set *PTRDATA to
2618 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2619 // pointer.
2620
2621 bool
2622 Type::needs_gcprog(Gogo* gogo, int64_t* ptrsize, int64_t* ptrdata)
2623 {
2624 Type* voidptr = Type::make_pointer_type(Type::make_void_type());
2625 if (!voidptr->backend_type_size(gogo, ptrsize))
2626 go_unreachable();
2627
2628 if (!this->backend_type_ptrdata(gogo, ptrdata))
2629 {
2630 go_assert(saw_errors());
2631 return false;
2632 }
2633
2634 return *ptrdata / *ptrsize > max_ptrmask_bytes;
2635 }
2636
2637 // A simple class used to build a GC ptrmask for a type.
2638
2639 class Ptrmask
2640 {
2641 public:
2642 Ptrmask(size_t count)
2643 : bits_((count + 7) / 8, 0)
2644 {}
2645
2646 void
2647 set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
2648
2649 std::string
2650 symname() const;
2651
2652 Expression*
2653 constructor(Gogo* gogo) const;
2654
2655 private:
2656 void
2657 set(size_t index)
2658 { this->bits_.at(index / 8) |= 1 << (index % 8); }
2659
2660 // The actual bits.
2661 std::vector<unsigned char> bits_;
2662 };
2663
2664 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2665 // counts in bytes. PTRSIZE is the size of a pointer on the target
2666 // system.
2667
2668 void
2669 Ptrmask::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
2670 {
2671 switch (type->base()->classification())
2672 {
2673 default:
2674 case Type::TYPE_NIL:
2675 case Type::TYPE_CALL_MULTIPLE_RESULT:
2676 case Type::TYPE_NAMED:
2677 case Type::TYPE_FORWARD:
2678 go_unreachable();
2679
2680 case Type::TYPE_ERROR:
2681 case Type::TYPE_VOID:
2682 case Type::TYPE_BOOLEAN:
2683 case Type::TYPE_INTEGER:
2684 case Type::TYPE_FLOAT:
2685 case Type::TYPE_COMPLEX:
2686 case Type::TYPE_SINK:
2687 break;
2688
2689 case Type::TYPE_FUNCTION:
2690 case Type::TYPE_POINTER:
2691 case Type::TYPE_MAP:
2692 case Type::TYPE_CHANNEL:
2693 // These types are all a single pointer.
2694 go_assert((offset % ptrsize) == 0);
2695 this->set(offset / ptrsize);
2696 break;
2697
2698 case Type::TYPE_STRING:
2699 // A string starts with a single pointer.
2700 go_assert((offset % ptrsize) == 0);
2701 this->set(offset / ptrsize);
2702 break;
2703
2704 case Type::TYPE_INTERFACE:
2705 // An interface is two pointers.
2706 go_assert((offset % ptrsize) == 0);
2707 this->set(offset / ptrsize);
2708 this->set((offset / ptrsize) + 1);
2709 break;
2710
2711 case Type::TYPE_STRUCT:
2712 {
2713 if (!type->has_pointer())
2714 return;
2715
2716 const Struct_field_list* fields = type->struct_type()->fields();
2717 int64_t soffset = 0;
2718 for (Struct_field_list::const_iterator pf = fields->begin();
2719 pf != fields->end();
2720 ++pf)
2721 {
2722 int64_t field_align;
2723 if (!pf->type()->backend_type_field_align(gogo, &field_align))
2724 {
2725 go_assert(saw_errors());
2726 return;
2727 }
2728 soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
2729
2730 this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
2731
2732 int64_t field_size;
2733 if (!pf->type()->backend_type_size(gogo, &field_size))
2734 {
2735 go_assert(saw_errors());
2736 return;
2737 }
2738 soffset += field_size;
2739 }
2740 }
2741 break;
2742
2743 case Type::TYPE_ARRAY:
2744 if (type->is_slice_type())
2745 {
2746 // A slice starts with a single pointer.
2747 go_assert((offset % ptrsize) == 0);
2748 this->set(offset / ptrsize);
2749 break;
2750 }
2751 else
2752 {
2753 if (!type->has_pointer())
2754 return;
2755
2756 int64_t len;
2757 if (!type->array_type()->int_length(&len))
2758 {
2759 go_assert(saw_errors());
2760 return;
2761 }
2762
2763 Type* element_type = type->array_type()->element_type();
2764 int64_t ele_size;
2765 if (!element_type->backend_type_size(gogo, &ele_size))
2766 {
2767 go_assert(saw_errors());
2768 return;
2769 }
2770
2771 int64_t eoffset = 0;
2772 for (int64_t i = 0; i < len; i++, eoffset += ele_size)
2773 this->set_from(gogo, element_type, ptrsize, offset + eoffset);
2774 break;
2775 }
2776 }
2777 }
2778
2779 // Return a symbol name for this ptrmask. This is used to coalesce
2780 // identical ptrmasks, which are common. The symbol name must use
2781 // only characters that are valid in symbols. It's nice if it's
2782 // short. We convert it to a string that uses only 32 characters,
2783 // avoiding digits and u and U.
2784
2785 std::string
2786 Ptrmask::symname() const
2787 {
2788 const char chars[33] = "abcdefghijklmnopqrstvwxyzABCDEFG";
2789 go_assert(chars[32] == '\0');
2790 std::string ret;
2791 unsigned int b = 0;
2792 int remaining = 0;
2793 for (std::vector<unsigned char>::const_iterator p = this->bits_.begin();
2794 p != this->bits_.end();
2795 ++p)
2796 {
2797 b |= *p << remaining;
2798 remaining += 8;
2799 while (remaining >= 5)
2800 {
2801 ret += chars[b & 0x1f];
2802 b >>= 5;
2803 remaining -= 5;
2804 }
2805 }
2806 while (remaining > 0)
2807 {
2808 ret += chars[b & 0x1f];
2809 b >>= 5;
2810 remaining -= 5;
2811 }
2812 return ret;
2813 }
2814
2815 // Return a constructor for this ptrmask. This will be used to
2816 // initialize the runtime ptrmask value.
2817
2818 Expression*
2819 Ptrmask::constructor(Gogo* gogo) const
2820 {
2821 Location bloc = Linemap::predeclared_location();
2822 Type* byte_type = gogo->lookup_global("byte")->type_value();
2823 Expression* len = Expression::make_integer_ul(this->bits_.size(), NULL,
2824 bloc);
2825 Array_type* at = Type::make_array_type(byte_type, len);
2826 Expression_list* vals = new Expression_list();
2827 vals->reserve(this->bits_.size());
2828 for (std::vector<unsigned char>::const_iterator p = this->bits_.begin();
2829 p != this->bits_.end();
2830 ++p)
2831 vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
2832 return Expression::make_array_composite_literal(at, vals, bloc);
2833 }
2834
2835 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2836 Type::GC_gcbits_vars Type::gc_gcbits_vars;
2837
2838 // Return a ptrmask variable for a type. For a type descriptor this
2839 // is only used for variables that are small enough to not need a
2840 // gcprog, but for a global variable this is used for a variable of
2841 // any size. PTRDATA is the number of bytes of the type that contain
2842 // pointer data. PTRSIZE is the size of a pointer on the target
2843 // system.
2844
2845 Bvariable*
2846 Type::gc_ptrmask_var(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
2847 {
2848 Ptrmask ptrmask(ptrdata / ptrsize);
2849 if (ptrdata >= ptrsize)
2850 ptrmask.set_from(gogo, this, ptrsize, 0);
2851 else
2852 {
2853 // This can happen in error cases. Just build an empty gcbits.
2854 go_assert(saw_errors());
2855 }
2856
2857 std::string sym_name = gogo->ptrmask_symbol_name(ptrmask.symname());
2858 Bvariable* bvnull = NULL;
2859 std::pair<GC_gcbits_vars::iterator, bool> ins =
2860 Type::gc_gcbits_vars.insert(std::make_pair(sym_name, bvnull));
2861 if (!ins.second)
2862 {
2863 // We've already built a GC symbol for this set of gcbits.
2864 return ins.first->second;
2865 }
2866
2867 Expression* val = ptrmask.constructor(gogo);
2868 Translate_context context(gogo, NULL, NULL, NULL);
2869 context.set_is_const();
2870 Bexpression* bval = val->get_backend(&context);
2871
2872 std::string asm_name(go_selectively_encode_id(sym_name));
2873 Btype *btype = val->type()->get_backend(gogo);
2874 Bvariable* ret = gogo->backend()->implicit_variable(sym_name, asm_name,
2875 btype, false, true,
2876 true, 0);
2877 gogo->backend()->implicit_variable_set_init(ret, sym_name, btype, false,
2878 true, true, bval);
2879 ins.first->second = ret;
2880 return ret;
2881 }
2882
2883 // A GCProg is used to build a program for the garbage collector.
2884 // This is used for types with a lot of pointer data, to reduce the
2885 // size of the data in the compiled program. The program is expanded
2886 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2887
2888 class GCProg
2889 {
2890 public:
2891 GCProg()
2892 : bytes_(), index_(0), nb_(0)
2893 {}
2894
2895 // The number of bits described so far.
2896 int64_t
2897 bit_index() const
2898 { return this->index_; }
2899
2900 void
2901 set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
2902
2903 void
2904 end();
2905
2906 Expression*
2907 constructor(Gogo* gogo) const;
2908
2909 private:
2910 void
2911 ptr(int64_t);
2912
2913 bool
2914 should_repeat(int64_t, int64_t);
2915
2916 void
2917 repeat(int64_t, int64_t);
2918
2919 void
2920 zero_until(int64_t);
2921
2922 void
2923 lit(unsigned char);
2924
2925 void
2926 varint(int64_t);
2927
2928 void
2929 flushlit();
2930
2931 // Add a byte to the program.
2932 void
2933 byte(unsigned char x)
2934 { this->bytes_.push_back(x); }
2935
2936 // The maximum number of bytes of literal bits.
2937 static const int max_literal = 127;
2938
2939 // The program.
2940 std::vector<unsigned char> bytes_;
2941 // The index of the last bit described.
2942 int64_t index_;
2943 // The current set of literal bits.
2944 unsigned char b_[max_literal];
2945 // The current number of literal bits.
2946 int nb_;
2947 };
2948
2949 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2950 // counts in bytes. PTRSIZE is the size of a pointer on the target
2951 // system.
2952
2953 void
2954 GCProg::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
2955 {
2956 switch (type->base()->classification())
2957 {
2958 default:
2959 case Type::TYPE_NIL:
2960 case Type::TYPE_CALL_MULTIPLE_RESULT:
2961 case Type::TYPE_NAMED:
2962 case Type::TYPE_FORWARD:
2963 go_unreachable();
2964
2965 case Type::TYPE_ERROR:
2966 case Type::TYPE_VOID:
2967 case Type::TYPE_BOOLEAN:
2968 case Type::TYPE_INTEGER:
2969 case Type::TYPE_FLOAT:
2970 case Type::TYPE_COMPLEX:
2971 case Type::TYPE_SINK:
2972 break;
2973
2974 case Type::TYPE_FUNCTION:
2975 case Type::TYPE_POINTER:
2976 case Type::TYPE_MAP:
2977 case Type::TYPE_CHANNEL:
2978 // These types are all a single pointer.
2979 go_assert((offset % ptrsize) == 0);
2980 this->ptr(offset / ptrsize);
2981 break;
2982
2983 case Type::TYPE_STRING:
2984 // A string starts with a single pointer.
2985 go_assert((offset % ptrsize) == 0);
2986 this->ptr(offset / ptrsize);
2987 break;
2988
2989 case Type::TYPE_INTERFACE:
2990 // An interface is two pointers.
2991 go_assert((offset % ptrsize) == 0);
2992 this->ptr(offset / ptrsize);
2993 this->ptr((offset / ptrsize) + 1);
2994 break;
2995
2996 case Type::TYPE_STRUCT:
2997 {
2998 if (!type->has_pointer())
2999 return;
3000
3001 const Struct_field_list* fields = type->struct_type()->fields();
3002 int64_t soffset = 0;
3003 for (Struct_field_list::const_iterator pf = fields->begin();
3004 pf != fields->end();
3005 ++pf)
3006 {
3007 int64_t field_align;
3008 if (!pf->type()->backend_type_field_align(gogo, &field_align))
3009 {
3010 go_assert(saw_errors());
3011 return;
3012 }
3013 soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
3014
3015 this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
3016
3017 int64_t field_size;
3018 if (!pf->type()->backend_type_size(gogo, &field_size))
3019 {
3020 go_assert(saw_errors());
3021 return;
3022 }
3023 soffset += field_size;
3024 }
3025 }
3026 break;
3027
3028 case Type::TYPE_ARRAY:
3029 if (type->is_slice_type())
3030 {
3031 // A slice starts with a single pointer.
3032 go_assert((offset % ptrsize) == 0);
3033 this->ptr(offset / ptrsize);
3034 break;
3035 }
3036 else
3037 {
3038 if (!type->has_pointer())
3039 return;
3040
3041 int64_t len;
3042 if (!type->array_type()->int_length(&len))
3043 {
3044 go_assert(saw_errors());
3045 return;
3046 }
3047
3048 Type* element_type = type->array_type()->element_type();
3049
3050 // Flatten array of array to a big array by multiplying counts.
3051 while (element_type->array_type() != NULL
3052 && !element_type->is_slice_type())
3053 {
3054 int64_t ele_len;
3055 if (!element_type->array_type()->int_length(&ele_len))
3056 {
3057 go_assert(saw_errors());
3058 return;
3059 }
3060
3061 len *= ele_len;
3062 element_type = element_type->array_type()->element_type();
3063 }
3064
3065 int64_t ele_size;
3066 if (!element_type->backend_type_size(gogo, &ele_size))
3067 {
3068 go_assert(saw_errors());
3069 return;
3070 }
3071
3072 go_assert(len > 0 && ele_size > 0);
3073
3074 if (!this->should_repeat(ele_size / ptrsize, len))
3075 {
3076 // Cheaper to just emit the bits.
3077 int64_t eoffset = 0;
3078 for (int64_t i = 0; i < len; i++, eoffset += ele_size)
3079 this->set_from(gogo, element_type, ptrsize, offset + eoffset);
3080 }
3081 else
3082 {
3083 go_assert((offset % ptrsize) == 0);
3084 go_assert((ele_size % ptrsize) == 0);
3085 this->set_from(gogo, element_type, ptrsize, offset);
3086 this->zero_until((offset + ele_size) / ptrsize);
3087 this->repeat(ele_size / ptrsize, len - 1);
3088 }
3089
3090 break;
3091 }
3092 }
3093 }
3094
3095 // Emit a 1 into the bit stream of a GC program at the given bit index.
3096
3097 void
3098 GCProg::ptr(int64_t index)
3099 {
3100 go_assert(index >= this->index_);
3101 this->zero_until(index);
3102 this->lit(1);
3103 }
3104
3105 // Return whether it is worthwhile to use a repeat to describe c
3106 // elements of n bits each, compared to just emitting c copies of the
3107 // n-bit description.
3108
3109 bool
3110 GCProg::should_repeat(int64_t n, int64_t c)
3111 {
3112 // Repeat if there is more than 1 item and if the total data doesn't
3113 // fit into four bytes.
3114 return c > 1 && c * n > 4 * 8;
3115 }
3116
3117 // Emit an instruction to repeat the description of the last n words c
3118 // times (including the initial description, so c + 1 times in total).
3119
3120 void
3121 GCProg::repeat(int64_t n, int64_t c)
3122 {
3123 if (n == 0 || c == 0)
3124 return;
3125 this->flushlit();
3126 if (n < 128)
3127 this->byte(0x80 | static_cast<unsigned char>(n & 0x7f));
3128 else
3129 {
3130 this->byte(0x80);
3131 this->varint(n);
3132 }
3133 this->varint(c);
3134 this->index_ += n * c;
3135 }
3136
3137 // Add zeros to the bit stream up to the given index.
3138
3139 void
3140 GCProg::zero_until(int64_t index)
3141 {
3142 go_assert(index >= this->index_);
3143 int64_t skip = index - this->index_;
3144 if (skip == 0)
3145 return;
3146 if (skip < 4 * 8)
3147 {
3148 for (int64_t i = 0; i < skip; ++i)
3149 this->lit(0);
3150 return;
3151 }
3152 this->lit(0);
3153 this->flushlit();
3154 this->repeat(1, skip - 1);
3155 }
3156
3157 // Add a single literal bit to the program.
3158
3159 void
3160 GCProg::lit(unsigned char x)
3161 {
3162 if (this->nb_ == GCProg::max_literal)
3163 this->flushlit();
3164 this->b_[this->nb_] = x;
3165 ++this->nb_;
3166 ++this->index_;
3167 }
3168
3169 // Emit the varint encoding of x.
3170
3171 void
3172 GCProg::varint(int64_t x)
3173 {
3174 go_assert(x >= 0);
3175 while (x >= 0x80)
3176 {
3177 this->byte(0x80 | static_cast<unsigned char>(x & 0x7f));
3178 x >>= 7;
3179 }
3180 this->byte(static_cast<unsigned char>(x & 0x7f));
3181 }
3182
3183 // Flush any pending literal bits.
3184
3185 void
3186 GCProg::flushlit()
3187 {
3188 if (this->nb_ == 0)
3189 return;
3190 this->byte(static_cast<unsigned char>(this->nb_));
3191 unsigned char bits = 0;
3192 for (int i = 0; i < this->nb_; ++i)
3193 {
3194 bits |= this->b_[i] << (i % 8);
3195 if ((i + 1) % 8 == 0)
3196 {
3197 this->byte(bits);
3198 bits = 0;
3199 }
3200 }
3201 if (this->nb_ % 8 != 0)
3202 this->byte(bits);
3203 this->nb_ = 0;
3204 }
3205
3206 // Mark the end of a GC program.
3207
3208 void
3209 GCProg::end()
3210 {
3211 this->flushlit();
3212 this->byte(0);
3213 }
3214
3215 // Return an Expression for the bytes in a GC program.
3216
3217 Expression*
3218 GCProg::constructor(Gogo* gogo) const
3219 {
3220 Location bloc = Linemap::predeclared_location();
3221
3222 // The first four bytes are the length of the program in target byte
3223 // order. Build a struct whose first type is uint32 to make this
3224 // work.
3225
3226 Type* uint32_type = Type::lookup_integer_type("uint32");
3227
3228 Type* byte_type = gogo->lookup_global("byte")->type_value();
3229 Expression* len = Expression::make_integer_ul(this->bytes_.size(), NULL,
3230 bloc);
3231 Array_type* at = Type::make_array_type(byte_type, len);
3232
3233 Struct_type* st = Type::make_builtin_struct_type(2, "len", uint32_type,
3234 "bytes", at);
3235
3236 Expression_list* vals = new Expression_list();
3237 vals->reserve(this->bytes_.size());
3238 for (std::vector<unsigned char>::const_iterator p = this->bytes_.begin();
3239 p != this->bytes_.end();
3240 ++p)
3241 vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
3242 Expression* bytes = Expression::make_array_composite_literal(at, vals, bloc);
3243
3244 vals = new Expression_list();
3245 vals->push_back(Expression::make_integer_ul(this->bytes_.size(), uint32_type,
3246 bloc));
3247 vals->push_back(bytes);
3248
3249 return Expression::make_struct_composite_literal(st, vals, bloc);
3250 }
3251
3252 // Return a composite literal for the garbage collection program for
3253 // this type. This is only used for types that are too large to use a
3254 // ptrmask.
3255
3256 Expression*
3257 Type::gcprog_constructor(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
3258 {
3259 Location bloc = Linemap::predeclared_location();
3260
3261 GCProg prog;
3262 prog.set_from(gogo, this, ptrsize, 0);
3263 int64_t offset = prog.bit_index() * ptrsize;
3264 prog.end();
3265
3266 int64_t type_size;
3267 if (!this->backend_type_size(gogo, &type_size))
3268 {
3269 go_assert(saw_errors());
3270 return Expression::make_error(bloc);
3271 }
3272
3273 go_assert(offset >= ptrdata && offset <= type_size);
3274
3275 return prog.constructor(gogo);
3276 }
3277
3278 // Return a composite literal for the uncommon type information for
3279 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3280 // struct. If name is not NULL, it is the name of the type. If
3281 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3282 // is true if only value methods should be included. At least one of
3283 // NAME and METHODS must not be NULL.
3284
3285 Expression*
3286 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
3287 Named_type* name, const Methods* methods,
3288 bool only_value_methods) const
3289 {
3290 Location bloc = Linemap::predeclared_location();
3291
3292 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
3293
3294 Expression_list* vals = new Expression_list();
3295 vals->reserve(3);
3296
3297 Struct_field_list::const_iterator p = fields->begin();
3298 go_assert(p->is_field_name("name"));
3299
3300 ++p;
3301 go_assert(p->is_field_name("pkgPath"));
3302
3303 if (name == NULL)
3304 {
3305 vals->push_back(Expression::make_nil(bloc));
3306 vals->push_back(Expression::make_nil(bloc));
3307 }
3308 else
3309 {
3310 Named_object* no = name->named_object();
3311 std::string n = Gogo::unpack_hidden_name(no->name());
3312 Expression* s = Expression::make_string(n, bloc);
3313 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3314
3315 if (name->is_builtin())
3316 vals->push_back(Expression::make_nil(bloc));
3317 else
3318 {
3319 const Package* package = no->package();
3320 const std::string& pkgpath(package == NULL
3321 ? gogo->pkgpath()
3322 : package->pkgpath());
3323 s = Expression::make_string(pkgpath, bloc);
3324 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3325 }
3326 }
3327
3328 ++p;
3329 go_assert(p->is_field_name("methods"));
3330 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
3331 only_value_methods));
3332
3333 ++p;
3334 go_assert(p == fields->end());
3335
3336 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
3337 vals, bloc);
3338 return Expression::make_unary(OPERATOR_AND, r, bloc);
3339 }
3340
3341 // Sort methods by name.
3342
3343 class Sort_methods
3344 {
3345 public:
3346 bool
3347 operator()(const std::pair<std::string, const Method*>& m1,
3348 const std::pair<std::string, const Method*>& m2) const
3349 {
3350 return (Gogo::unpack_hidden_name(m1.first)
3351 < Gogo::unpack_hidden_name(m2.first));
3352 }
3353 };
3354
3355 // Return a composite literal for the type method table for this type.
3356 // METHODS_TYPE is the type of the table, and is a slice type.
3357 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3358 // then only value methods are used.
3359
3360 Expression*
3361 Type::methods_constructor(Gogo* gogo, Type* methods_type,
3362 const Methods* methods,
3363 bool only_value_methods) const
3364 {
3365 Location bloc = Linemap::predeclared_location();
3366
3367 std::vector<std::pair<std::string, const Method*> > smethods;
3368 if (methods != NULL)
3369 {
3370 smethods.reserve(methods->count());
3371 for (Methods::const_iterator p = methods->begin();
3372 p != methods->end();
3373 ++p)
3374 {
3375 if (p->second->is_ambiguous())
3376 continue;
3377 if (only_value_methods && !p->second->is_value_method())
3378 continue;
3379
3380 // This is where we implement the magic //go:nointerface
3381 // comment. If we saw that comment, we don't add this
3382 // method to the type descriptor.
3383 if (p->second->nointerface())
3384 continue;
3385
3386 smethods.push_back(std::make_pair(p->first, p->second));
3387 }
3388 }
3389
3390 if (smethods.empty())
3391 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
3392
3393 std::sort(smethods.begin(), smethods.end(), Sort_methods());
3394
3395 Type* method_type = methods_type->array_type()->element_type();
3396
3397 Expression_list* vals = new Expression_list();
3398 vals->reserve(smethods.size());
3399 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
3400 = smethods.begin();
3401 p != smethods.end();
3402 ++p)
3403 vals->push_back(this->method_constructor(gogo, method_type, p->first,
3404 p->second, only_value_methods));
3405
3406 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
3407 }
3408
3409 // Return a composite literal for a single method. METHOD_TYPE is the
3410 // type of the entry. METHOD_NAME is the name of the method and M is
3411 // the method information.
3412
3413 Expression*
3414 Type::method_constructor(Gogo*, Type* method_type,
3415 const std::string& method_name,
3416 const Method* m,
3417 bool only_value_methods) const
3418 {
3419 Location bloc = Linemap::predeclared_location();
3420
3421 const Struct_field_list* fields = method_type->struct_type()->fields();
3422
3423 Expression_list* vals = new Expression_list();
3424 vals->reserve(5);
3425
3426 Struct_field_list::const_iterator p = fields->begin();
3427 go_assert(p->is_field_name("name"));
3428 const std::string n = Gogo::unpack_hidden_name(method_name);
3429 Expression* s = Expression::make_string(n, bloc);
3430 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3431
3432 ++p;
3433 go_assert(p->is_field_name("pkgPath"));
3434 if (!Gogo::is_hidden_name(method_name))
3435 vals->push_back(Expression::make_nil(bloc));
3436 else
3437 {
3438 s = Expression::make_string(Gogo::hidden_name_pkgpath(method_name),
3439 bloc);
3440 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3441 }
3442
3443 bool use_direct_iface_stub =
3444 this->points_to() != NULL
3445 && this->points_to()->is_direct_iface_type()
3446 && m->is_value_method();
3447 Named_object* no = (use_direct_iface_stub
3448 ? m->iface_stub_object()
3449 : (m->needs_stub_method()
3450 ? m->stub_object()
3451 : m->named_object()));
3452
3453 Function_type* mtype;
3454 if (no->is_function())
3455 mtype = no->func_value()->type();
3456 else
3457 mtype = no->func_declaration_value()->type();
3458 go_assert(mtype->is_method());
3459 Type* nonmethod_type = mtype->copy_without_receiver();
3460
3461 ++p;
3462 go_assert(p->is_field_name("mtyp"));
3463 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3464
3465 ++p;
3466 go_assert(p->is_field_name("typ"));
3467 bool want_pointer_receiver = (!only_value_methods && m->is_value_method()
3468 && !use_direct_iface_stub);
3469 nonmethod_type = mtype->copy_with_receiver_as_param(want_pointer_receiver);
3470 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3471
3472 ++p;
3473 go_assert(p->is_field_name("tfn"));
3474 vals->push_back(Expression::make_func_code_reference(no, bloc));
3475
3476 ++p;
3477 go_assert(p == fields->end());
3478
3479 return Expression::make_struct_composite_literal(method_type, vals, bloc);
3480 }
3481
3482 // Return a composite literal for the type descriptor of a plain type.
3483 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3484 // NULL, it is the name to use as well as the list of methods.
3485
3486 Expression*
3487 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
3488 Named_type* name)
3489 {
3490 return this->type_descriptor_constructor(gogo, runtime_type_kind,
3491 name, NULL, true);
3492 }
3493
3494 // Return the type reflection string for this type.
3495
3496 std::string
3497 Type::reflection(Gogo* gogo) const
3498 {
3499 std::string ret;
3500
3501 // The do_reflection virtual function should set RET to the
3502 // reflection string.
3503 this->do_reflection(gogo, &ret);
3504
3505 return ret;
3506 }
3507
3508 // Return whether the backend size of the type is known.
3509
3510 bool
3511 Type::is_backend_type_size_known(Gogo* gogo)
3512 {
3513 switch (this->classification_)
3514 {
3515 case TYPE_ERROR:
3516 case TYPE_VOID:
3517 case TYPE_BOOLEAN:
3518 case TYPE_INTEGER:
3519 case TYPE_FLOAT:
3520 case TYPE_COMPLEX:
3521 case TYPE_STRING:
3522 case TYPE_FUNCTION:
3523 case TYPE_POINTER:
3524 case TYPE_NIL:
3525 case TYPE_MAP:
3526 case TYPE_CHANNEL:
3527 case TYPE_INTERFACE:
3528 return true;
3529
3530 case TYPE_STRUCT:
3531 {
3532 const Struct_field_list* fields = this->struct_type()->fields();
3533 for (Struct_field_list::const_iterator pf = fields->begin();
3534 pf != fields->end();
3535 ++pf)
3536 if (!pf->type()->is_backend_type_size_known(gogo))
3537 return false;
3538 return true;
3539 }
3540
3541 case TYPE_ARRAY:
3542 {
3543 const Array_type* at = this->array_type();
3544 if (at->length() == NULL)
3545 return true;
3546 else
3547 {
3548 Numeric_constant nc;
3549 if (!at->length()->numeric_constant_value(&nc))
3550 return false;
3551 mpz_t ival;
3552 if (!nc.to_int(&ival))
3553 return false;
3554 mpz_clear(ival);
3555 return at->element_type()->is_backend_type_size_known(gogo);
3556 }
3557 }
3558
3559 case TYPE_NAMED:
3560 this->named_type()->convert(gogo);
3561 return this->named_type()->is_named_backend_type_size_known();
3562
3563 case TYPE_FORWARD:
3564 {
3565 Forward_declaration_type* fdt = this->forward_declaration_type();
3566 return fdt->real_type()->is_backend_type_size_known(gogo);
3567 }
3568
3569 case TYPE_SINK:
3570 case TYPE_CALL_MULTIPLE_RESULT:
3571 go_unreachable();
3572
3573 default:
3574 go_unreachable();
3575 }
3576 }
3577
3578 // If the size of the type can be determined, set *PSIZE to the size
3579 // in bytes and return true. Otherwise, return false. This queries
3580 // the backend.
3581
3582 bool
3583 Type::backend_type_size(Gogo* gogo, int64_t *psize)
3584 {
3585 if (!this->is_backend_type_size_known(gogo))
3586 return false;
3587 if (this->is_error_type())
3588 return false;
3589 Btype* bt = this->get_backend_placeholder(gogo);
3590 *psize = gogo->backend()->type_size(bt);
3591 if (*psize == -1)
3592 {
3593 if (this->named_type() != NULL)
3594 go_error_at(this->named_type()->location(),
3595 "type %s larger than address space",
3596 Gogo::message_name(this->named_type()->name()).c_str());
3597 else
3598 go_error_at(Linemap::unknown_location(),
3599 "type %s larger than address space",
3600 this->reflection(gogo).c_str());
3601
3602 // Make this an error type to avoid knock-on errors.
3603 this->classification_ = TYPE_ERROR;
3604 return false;
3605 }
3606 return true;
3607 }
3608
3609 // If the alignment of the type can be determined, set *PALIGN to
3610 // the alignment in bytes and return true. Otherwise, return false.
3611
3612 bool
3613 Type::backend_type_align(Gogo* gogo, int64_t *palign)
3614 {
3615 if (!this->is_backend_type_size_known(gogo))
3616 return false;
3617 Btype* bt = this->get_backend_placeholder(gogo);
3618 *palign = gogo->backend()->type_alignment(bt);
3619 return true;
3620 }
3621
3622 // Like backend_type_align, but return the alignment when used as a
3623 // field.
3624
3625 bool
3626 Type::backend_type_field_align(Gogo* gogo, int64_t *palign)
3627 {
3628 if (!this->is_backend_type_size_known(gogo))
3629 return false;
3630 Btype* bt = this->get_backend_placeholder(gogo);
3631 *palign = gogo->backend()->type_field_alignment(bt);
3632 return true;
3633 }
3634
3635 // Get the ptrdata value for a type. This is the size of the prefix
3636 // of the type that contains all pointers. Store the ptrdata in
3637 // *PPTRDATA and return whether we found it.
3638
3639 bool
3640 Type::backend_type_ptrdata(Gogo* gogo, int64_t* pptrdata)
3641 {
3642 *pptrdata = 0;
3643
3644 if (!this->has_pointer())
3645 return true;
3646
3647 if (!this->is_backend_type_size_known(gogo))
3648 return false;
3649
3650 switch (this->classification_)
3651 {
3652 case TYPE_ERROR:
3653 return true;
3654
3655 case TYPE_FUNCTION:
3656 case TYPE_POINTER:
3657 case TYPE_MAP:
3658 case TYPE_CHANNEL:
3659 // These types are nothing but a pointer.
3660 return this->backend_type_size(gogo, pptrdata);
3661
3662 case TYPE_INTERFACE:
3663 // An interface is a struct of two pointers.
3664 return this->backend_type_size(gogo, pptrdata);
3665
3666 case TYPE_STRING:
3667 {
3668 // A string is a struct whose first field is a pointer, and
3669 // whose second field is not.
3670 Type* uint8_type = Type::lookup_integer_type("uint8");
3671 Type* ptr = Type::make_pointer_type(uint8_type);
3672 return ptr->backend_type_size(gogo, pptrdata);
3673 }
3674
3675 case TYPE_NAMED:
3676 case TYPE_FORWARD:
3677 return this->base()->backend_type_ptrdata(gogo, pptrdata);
3678
3679 case TYPE_STRUCT:
3680 {
3681 const Struct_field_list* fields = this->struct_type()->fields();
3682 int64_t offset = 0;
3683 const Struct_field *ptr = NULL;
3684 int64_t ptr_offset = 0;
3685 for (Struct_field_list::const_iterator pf = fields->begin();
3686 pf != fields->end();
3687 ++pf)
3688 {
3689 int64_t field_align;
3690 if (!pf->type()->backend_type_field_align(gogo, &field_align))
3691 return false;
3692 offset = (offset + (field_align - 1)) &~ (field_align - 1);
3693
3694 if (pf->type()->has_pointer())
3695 {
3696 ptr = &*pf;
3697 ptr_offset = offset;
3698 }
3699
3700 int64_t field_size;
3701 if (!pf->type()->backend_type_size(gogo, &field_size))
3702 return false;
3703 offset += field_size;
3704 }
3705
3706 if (ptr != NULL)
3707 {
3708 int64_t ptr_ptrdata;
3709 if (!ptr->type()->backend_type_ptrdata(gogo, &ptr_ptrdata))
3710 return false;
3711 *pptrdata = ptr_offset + ptr_ptrdata;
3712 }
3713 return true;
3714 }
3715
3716 case TYPE_ARRAY:
3717 if (this->is_slice_type())
3718 {
3719 // A slice is a struct whose first field is a pointer, and
3720 // whose remaining fields are not.
3721 Type* element_type = this->array_type()->element_type();
3722 Type* ptr = Type::make_pointer_type(element_type);
3723 return ptr->backend_type_size(gogo, pptrdata);
3724 }
3725 else
3726 {
3727 Numeric_constant nc;
3728 if (!this->array_type()->length()->numeric_constant_value(&nc))
3729 return false;
3730 int64_t len;
3731 if (!nc.to_memory_size(&len))
3732 return false;
3733
3734 Type* element_type = this->array_type()->element_type();
3735 int64_t ele_size;
3736 int64_t ele_ptrdata;
3737 if (!element_type->backend_type_size(gogo, &ele_size)
3738 || !element_type->backend_type_ptrdata(gogo, &ele_ptrdata))
3739 return false;
3740 go_assert(ele_size > 0 && ele_ptrdata > 0);
3741
3742 *pptrdata = (len - 1) * ele_size + ele_ptrdata;
3743 return true;
3744 }
3745
3746 default:
3747 case TYPE_VOID:
3748 case TYPE_BOOLEAN:
3749 case TYPE_INTEGER:
3750 case TYPE_FLOAT:
3751 case TYPE_COMPLEX:
3752 case TYPE_SINK:
3753 case TYPE_NIL:
3754 case TYPE_CALL_MULTIPLE_RESULT:
3755 go_unreachable();
3756 }
3757 }
3758
3759 // Get the ptrdata value to store in a type descriptor. This is
3760 // normally the same as backend_type_ptrdata, but for a type that is
3761 // large enough to use a gcprog we may need to store a different value
3762 // if it ends with an array. If the gcprog uses a repeat descriptor
3763 // for the array, and if the array element ends with non-pointer data,
3764 // then the gcprog will produce a value that describes the complete
3765 // array where the backend ptrdata will omit the non-pointer elements
3766 // of the final array element. This is a subtle difference but the
3767 // run time code checks it to verify that it has expanded a gcprog as
3768 // expected.
3769
3770 bool
3771 Type::descriptor_ptrdata(Gogo* gogo, int64_t* pptrdata)
3772 {
3773 int64_t backend_ptrdata;
3774 if (!this->backend_type_ptrdata(gogo, &backend_ptrdata))
3775 return false;
3776
3777 int64_t ptrsize;
3778 if (!this->needs_gcprog(gogo, &ptrsize, &backend_ptrdata))
3779 {
3780 *pptrdata = backend_ptrdata;
3781 return true;
3782 }
3783
3784 GCProg prog;
3785 prog.set_from(gogo, this, ptrsize, 0);
3786 int64_t offset = prog.bit_index() * ptrsize;
3787
3788 go_assert(offset >= backend_ptrdata);
3789 *pptrdata = offset;
3790 return true;
3791 }
3792
3793 // Default function to export a type.
3794
3795 void
3796 Type::do_export(Export*) const
3797 {
3798 go_unreachable();
3799 }
3800
3801 // Import a type.
3802
3803 Type*
3804 Type::import_type(Import* imp)
3805 {
3806 if (imp->match_c_string("("))
3807 return Function_type::do_import(imp);
3808 else if (imp->match_c_string("*"))
3809 return Pointer_type::do_import(imp);
3810 else if (imp->match_c_string("struct "))
3811 return Struct_type::do_import(imp);
3812 else if (imp->match_c_string("["))
3813 return Array_type::do_import(imp);
3814 else if (imp->match_c_string("map "))
3815 return Map_type::do_import(imp);
3816 else if (imp->match_c_string("chan "))
3817 return Channel_type::do_import(imp);
3818 else if (imp->match_c_string("interface"))
3819 return Interface_type::do_import(imp);
3820 else
3821 {
3822 go_error_at(imp->location(), "import error: expected type");
3823 return Type::make_error_type();
3824 }
3825 }
3826
3827 // Class Error_type.
3828
3829 // Return the backend representation of an Error type.
3830
3831 Btype*
3832 Error_type::do_get_backend(Gogo* gogo)
3833 {
3834 return gogo->backend()->error_type();
3835 }
3836
3837 // Return an expression for the type descriptor for an error type.
3838
3839
3840 Expression*
3841 Error_type::do_type_descriptor(Gogo*, Named_type*)
3842 {
3843 return Expression::make_error(Linemap::predeclared_location());
3844 }
3845
3846 // We should not be asked for the reflection string for an error type.
3847
3848 void
3849 Error_type::do_reflection(Gogo*, std::string*) const
3850 {
3851 go_assert(saw_errors());
3852 }
3853
3854 Type*
3855 Type::make_error_type()
3856 {
3857 static Error_type singleton_error_type;
3858 return &singleton_error_type;
3859 }
3860
3861 // Class Void_type.
3862
3863 // Get the backend representation of a void type.
3864
3865 Btype*
3866 Void_type::do_get_backend(Gogo* gogo)
3867 {
3868 return gogo->backend()->void_type();
3869 }
3870
3871 Type*
3872 Type::make_void_type()
3873 {
3874 static Void_type singleton_void_type;
3875 return &singleton_void_type;
3876 }
3877
3878 // Class Boolean_type.
3879
3880 // Return the backend representation of the boolean type.
3881
3882 Btype*
3883 Boolean_type::do_get_backend(Gogo* gogo)
3884 {
3885 return gogo->backend()->bool_type();
3886 }
3887
3888 // Make the type descriptor.
3889
3890 Expression*
3891 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3892 {
3893 if (name != NULL)
3894 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
3895 else
3896 {
3897 Named_object* no = gogo->lookup_global("bool");
3898 go_assert(no != NULL);
3899 return Type::type_descriptor(gogo, no->type_value());
3900 }
3901 }
3902
3903 Type*
3904 Type::make_boolean_type()
3905 {
3906 static Boolean_type boolean_type;
3907 return &boolean_type;
3908 }
3909
3910 // The named type "bool".
3911
3912 static Named_type* named_bool_type;
3913
3914 // Get the named type "bool".
3915
3916 Named_type*
3917 Type::lookup_bool_type()
3918 {
3919 return named_bool_type;
3920 }
3921
3922 // Make the named type "bool".
3923
3924 Named_type*
3925 Type::make_named_bool_type()
3926 {
3927 Type* bool_type = Type::make_boolean_type();
3928 Named_object* named_object =
3929 Named_object::make_type("bool", NULL, bool_type,
3930 Linemap::predeclared_location());
3931 Named_type* named_type = named_object->type_value();
3932 named_bool_type = named_type;
3933 return named_type;
3934 }
3935
3936 // Class Integer_type.
3937
3938 Integer_type::Named_integer_types Integer_type::named_integer_types;
3939
3940 // Create a new integer type. Non-abstract integer types always have
3941 // names.
3942
3943 Named_type*
3944 Integer_type::create_integer_type(const char* name, bool is_unsigned,
3945 int bits, int runtime_type_kind)
3946 {
3947 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
3948 runtime_type_kind);
3949 std::string sname(name);
3950 Named_object* named_object =
3951 Named_object::make_type(sname, NULL, integer_type,
3952 Linemap::predeclared_location());
3953 Named_type* named_type = named_object->type_value();
3954 std::pair<Named_integer_types::iterator, bool> ins =
3955 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
3956 go_assert(ins.second);
3957 return named_type;
3958 }
3959
3960 // Look up an existing integer type.
3961
3962 Named_type*
3963 Integer_type::lookup_integer_type(const char* name)
3964 {
3965 Named_integer_types::const_iterator p =
3966 Integer_type::named_integer_types.find(name);
3967 go_assert(p != Integer_type::named_integer_types.end());
3968 return p->second;
3969 }
3970
3971 // Create a new abstract integer type.
3972
3973 Integer_type*
3974 Integer_type::create_abstract_integer_type()
3975 {
3976 static Integer_type* abstract_type;
3977 if (abstract_type == NULL)
3978 {
3979 Type* int_type = Type::lookup_integer_type("int");
3980 abstract_type = new Integer_type(true, false,
3981 int_type->integer_type()->bits(),
3982 RUNTIME_TYPE_KIND_INT);
3983 }
3984 return abstract_type;
3985 }
3986
3987 // Create a new abstract character type.
3988
3989 Integer_type*
3990 Integer_type::create_abstract_character_type()
3991 {
3992 static Integer_type* abstract_type;
3993 if (abstract_type == NULL)
3994 {
3995 abstract_type = new Integer_type(true, false, 32,
3996 RUNTIME_TYPE_KIND_INT32);
3997 abstract_type->set_is_rune();
3998 }
3999 return abstract_type;
4000 }
4001
4002 // Integer type compatibility.
4003
4004 bool
4005 Integer_type::is_identical(const Integer_type* t) const
4006 {
4007 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
4008 return false;
4009 return this->is_abstract_ == t->is_abstract_;
4010 }
4011
4012 // Hash code.
4013
4014 unsigned int
4015 Integer_type::do_hash_for_method(Gogo*, int) const
4016 {
4017 return ((this->bits_ << 4)
4018 + ((this->is_unsigned_ ? 1 : 0) << 8)
4019 + ((this->is_abstract_ ? 1 : 0) << 9));
4020 }
4021
4022 // Convert an Integer_type to the backend representation.
4023
4024 Btype*
4025 Integer_type::do_get_backend(Gogo* gogo)
4026 {
4027 if (this->is_abstract_)
4028 {
4029 go_assert(saw_errors());
4030 return gogo->backend()->error_type();
4031 }
4032 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
4033 }
4034
4035 // The type descriptor for an integer type. Integer types are always
4036 // named.
4037
4038 Expression*
4039 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4040 {
4041 go_assert(name != NULL || saw_errors());
4042 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4043 }
4044
4045 // We should not be asked for the reflection string of a basic type.
4046
4047 void
4048 Integer_type::do_reflection(Gogo*, std::string*) const
4049 {
4050 go_assert(saw_errors());
4051 }
4052
4053 // Make an integer type.
4054
4055 Named_type*
4056 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
4057 int runtime_type_kind)
4058 {
4059 return Integer_type::create_integer_type(name, is_unsigned, bits,
4060 runtime_type_kind);
4061 }
4062
4063 // Make an abstract integer type.
4064
4065 Integer_type*
4066 Type::make_abstract_integer_type()
4067 {
4068 return Integer_type::create_abstract_integer_type();
4069 }
4070
4071 // Make an abstract character type.
4072
4073 Integer_type*
4074 Type::make_abstract_character_type()
4075 {
4076 return Integer_type::create_abstract_character_type();
4077 }
4078
4079 // Look up an integer type.
4080
4081 Named_type*
4082 Type::lookup_integer_type(const char* name)
4083 {
4084 return Integer_type::lookup_integer_type(name);
4085 }
4086
4087 // Class Float_type.
4088
4089 Float_type::Named_float_types Float_type::named_float_types;
4090
4091 // Create a new float type. Non-abstract float types always have
4092 // names.
4093
4094 Named_type*
4095 Float_type::create_float_type(const char* name, int bits,
4096 int runtime_type_kind)
4097 {
4098 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
4099 std::string sname(name);
4100 Named_object* named_object =
4101 Named_object::make_type(sname, NULL, float_type,
4102 Linemap::predeclared_location());
4103 Named_type* named_type = named_object->type_value();
4104 std::pair<Named_float_types::iterator, bool> ins =
4105 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
4106 go_assert(ins.second);
4107 return named_type;
4108 }
4109
4110 // Look up an existing float type.
4111
4112 Named_type*
4113 Float_type::lookup_float_type(const char* name)
4114 {
4115 Named_float_types::const_iterator p =
4116 Float_type::named_float_types.find(name);
4117 go_assert(p != Float_type::named_float_types.end());
4118 return p->second;
4119 }
4120
4121 // Create a new abstract float type.
4122
4123 Float_type*
4124 Float_type::create_abstract_float_type()
4125 {
4126 static Float_type* abstract_type;
4127 if (abstract_type == NULL)
4128 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
4129 return abstract_type;
4130 }
4131
4132 // Whether this type is identical with T.
4133
4134 bool
4135 Float_type::is_identical(const Float_type* t) const
4136 {
4137 if (this->bits_ != t->bits_)
4138 return false;
4139 return this->is_abstract_ == t->is_abstract_;
4140 }
4141
4142 // Hash code.
4143
4144 unsigned int
4145 Float_type::do_hash_for_method(Gogo*, int) const
4146 {
4147 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
4148 }
4149
4150 // Convert to the backend representation.
4151
4152 Btype*
4153 Float_type::do_get_backend(Gogo* gogo)
4154 {
4155 return gogo->backend()->float_type(this->bits_);
4156 }
4157
4158 // The type descriptor for a float type. Float types are always named.
4159
4160 Expression*
4161 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4162 {
4163 go_assert(name != NULL || saw_errors());
4164 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4165 }
4166
4167 // We should not be asked for the reflection string of a basic type.
4168
4169 void
4170 Float_type::do_reflection(Gogo*, std::string*) const
4171 {
4172 go_assert(saw_errors());
4173 }
4174
4175 // Make a floating point type.
4176
4177 Named_type*
4178 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
4179 {
4180 return Float_type::create_float_type(name, bits, runtime_type_kind);
4181 }
4182
4183 // Make an abstract float type.
4184
4185 Float_type*
4186 Type::make_abstract_float_type()
4187 {
4188 return Float_type::create_abstract_float_type();
4189 }
4190
4191 // Look up a float type.
4192
4193 Named_type*
4194 Type::lookup_float_type(const char* name)
4195 {
4196 return Float_type::lookup_float_type(name);
4197 }
4198
4199 // Class Complex_type.
4200
4201 Complex_type::Named_complex_types Complex_type::named_complex_types;
4202
4203 // Create a new complex type. Non-abstract complex types always have
4204 // names.
4205
4206 Named_type*
4207 Complex_type::create_complex_type(const char* name, int bits,
4208 int runtime_type_kind)
4209 {
4210 Complex_type* complex_type = new Complex_type(false, bits,
4211 runtime_type_kind);
4212 std::string sname(name);
4213 Named_object* named_object =
4214 Named_object::make_type(sname, NULL, complex_type,
4215 Linemap::predeclared_location());
4216 Named_type* named_type = named_object->type_value();
4217 std::pair<Named_complex_types::iterator, bool> ins =
4218 Complex_type::named_complex_types.insert(std::make_pair(sname,
4219 named_type));
4220 go_assert(ins.second);
4221 return named_type;
4222 }
4223
4224 // Look up an existing complex type.
4225
4226 Named_type*
4227 Complex_type::lookup_complex_type(const char* name)
4228 {
4229 Named_complex_types::const_iterator p =
4230 Complex_type::named_complex_types.find(name);
4231 go_assert(p != Complex_type::named_complex_types.end());
4232 return p->second;
4233 }
4234
4235 // Create a new abstract complex type.
4236
4237 Complex_type*
4238 Complex_type::create_abstract_complex_type()
4239 {
4240 static Complex_type* abstract_type;
4241 if (abstract_type == NULL)
4242 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
4243 return abstract_type;
4244 }
4245
4246 // Whether this type is identical with T.
4247
4248 bool
4249 Complex_type::is_identical(const Complex_type *t) const
4250 {
4251 if (this->bits_ != t->bits_)
4252 return false;
4253 return this->is_abstract_ == t->is_abstract_;
4254 }
4255
4256 // Hash code.
4257
4258 unsigned int
4259 Complex_type::do_hash_for_method(Gogo*, int) const
4260 {
4261 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
4262 }
4263
4264 // Convert to the backend representation.
4265
4266 Btype*
4267 Complex_type::do_get_backend(Gogo* gogo)
4268 {
4269 return gogo->backend()->complex_type(this->bits_);
4270 }
4271
4272 // The type descriptor for a complex type. Complex types are always
4273 // named.
4274
4275 Expression*
4276 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4277 {
4278 go_assert(name != NULL || saw_errors());
4279 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4280 }
4281
4282 // We should not be asked for the reflection string of a basic type.
4283
4284 void
4285 Complex_type::do_reflection(Gogo*, std::string*) const
4286 {
4287 go_assert(saw_errors());
4288 }
4289
4290 // Make a complex type.
4291
4292 Named_type*
4293 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
4294 {
4295 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
4296 }
4297
4298 // Make an abstract complex type.
4299
4300 Complex_type*
4301 Type::make_abstract_complex_type()
4302 {
4303 return Complex_type::create_abstract_complex_type();
4304 }
4305
4306 // Look up a complex type.
4307
4308 Named_type*
4309 Type::lookup_complex_type(const char* name)
4310 {
4311 return Complex_type::lookup_complex_type(name);
4312 }
4313
4314 // Class String_type.
4315
4316 // Convert String_type to the backend representation. A string is a
4317 // struct with two fields: a pointer to the characters and a length.
4318
4319 Btype*
4320 String_type::do_get_backend(Gogo* gogo)
4321 {
4322 static Btype* backend_string_type;
4323 if (backend_string_type == NULL)
4324 {
4325 std::vector<Backend::Btyped_identifier> fields(2);
4326
4327 Type* b = gogo->lookup_global("byte")->type_value();
4328 Type* pb = Type::make_pointer_type(b);
4329
4330 // We aren't going to get back to this field to finish the
4331 // backend representation, so force it to be finished now.
4332 if (!gogo->named_types_are_converted())
4333 {
4334 Btype* bt = pb->get_backend_placeholder(gogo);
4335 pb->finish_backend(gogo, bt);
4336 }
4337
4338 fields[0].name = "__data";
4339 fields[0].btype = pb->get_backend(gogo);
4340 fields[0].location = Linemap::predeclared_location();
4341
4342 Type* int_type = Type::lookup_integer_type("int");
4343 fields[1].name = "__length";
4344 fields[1].btype = int_type->get_backend(gogo);
4345 fields[1].location = fields[0].location;
4346
4347 backend_string_type = gogo->backend()->struct_type(fields);
4348 }
4349 return backend_string_type;
4350 }
4351
4352 // The type descriptor for the string type.
4353
4354 Expression*
4355 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4356 {
4357 if (name != NULL)
4358 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
4359 else
4360 {
4361 Named_object* no = gogo->lookup_global("string");
4362 go_assert(no != NULL);
4363 return Type::type_descriptor(gogo, no->type_value());
4364 }
4365 }
4366
4367 // We should not be asked for the reflection string of a basic type.
4368
4369 void
4370 String_type::do_reflection(Gogo*, std::string* ret) const
4371 {
4372 ret->append("string");
4373 }
4374
4375 // Make a string type.
4376
4377 Type*
4378 Type::make_string_type()
4379 {
4380 static String_type string_type;
4381 return &string_type;
4382 }
4383
4384 // The named type "string".
4385
4386 static Named_type* named_string_type;
4387
4388 // Get the named type "string".
4389
4390 Named_type*
4391 Type::lookup_string_type()
4392 {
4393 return named_string_type;
4394 }
4395
4396 // Make the named type string.
4397
4398 Named_type*
4399 Type::make_named_string_type()
4400 {
4401 Type* string_type = Type::make_string_type();
4402 Named_object* named_object =
4403 Named_object::make_type("string", NULL, string_type,
4404 Linemap::predeclared_location());
4405 Named_type* named_type = named_object->type_value();
4406 named_string_type = named_type;
4407 return named_type;
4408 }
4409
4410 // The sink type. This is the type of the blank identifier _. Any
4411 // type may be assigned to it.
4412
4413 class Sink_type : public Type
4414 {
4415 public:
4416 Sink_type()
4417 : Type(TYPE_SINK)
4418 { }
4419
4420 protected:
4421 bool
4422 do_compare_is_identity(Gogo*)
4423 { return false; }
4424
4425 Btype*
4426 do_get_backend(Gogo*)
4427 { go_unreachable(); }
4428
4429 Expression*
4430 do_type_descriptor(Gogo*, Named_type*)
4431 { go_unreachable(); }
4432
4433 void
4434 do_reflection(Gogo*, std::string*) const
4435 { go_unreachable(); }
4436
4437 void
4438 do_mangled_name(Gogo*, std::string*) const
4439 { go_unreachable(); }
4440 };
4441
4442 // Make the sink type.
4443
4444 Type*
4445 Type::make_sink_type()
4446 {
4447 static Sink_type sink_type;
4448 return &sink_type;
4449 }
4450
4451 // Class Function_type.
4452
4453 // Traversal.
4454
4455 int
4456 Function_type::do_traverse(Traverse* traverse)
4457 {
4458 if (this->receiver_ != NULL
4459 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
4460 return TRAVERSE_EXIT;
4461 if (this->parameters_ != NULL
4462 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
4463 return TRAVERSE_EXIT;
4464 if (this->results_ != NULL
4465 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
4466 return TRAVERSE_EXIT;
4467 return TRAVERSE_CONTINUE;
4468 }
4469
4470 // Returns whether T is a valid redeclaration of this type. If this
4471 // returns false, and REASON is not NULL, *REASON may be set to a
4472 // brief explanation of why it returned false.
4473
4474 bool
4475 Function_type::is_valid_redeclaration(const Function_type* t,
4476 std::string* reason) const
4477 {
4478 if (!this->is_identical(t, false, COMPARE_TAGS, reason))
4479 return false;
4480
4481 // A redeclaration of a function is required to use the same names
4482 // for the receiver and parameters.
4483 if (this->receiver() != NULL
4484 && this->receiver()->name() != t->receiver()->name())
4485 {
4486 if (reason != NULL)
4487 *reason = "receiver name changed";
4488 return false;
4489 }
4490
4491 const Typed_identifier_list* parms1 = this->parameters();
4492 const Typed_identifier_list* parms2 = t->parameters();
4493 if (parms1 != NULL)
4494 {
4495 Typed_identifier_list::const_iterator p1 = parms1->begin();
4496 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
4497 p2 != parms2->end();
4498 ++p2, ++p1)
4499 {
4500 if (p1->name() != p2->name())
4501 {
4502 if (reason != NULL)
4503 *reason = "parameter name changed";
4504 return false;
4505 }
4506
4507 // This is called at parse time, so we may have unknown
4508 // types.
4509 Type* t1 = p1->type()->forwarded();
4510 Type* t2 = p2->type()->forwarded();
4511 if (t1 != t2
4512 && t1->forward_declaration_type() != NULL
4513 && (t2->forward_declaration_type() == NULL
4514 || (t1->forward_declaration_type()->named_object()
4515 != t2->forward_declaration_type()->named_object())))
4516 return false;
4517 }
4518 }
4519
4520 const Typed_identifier_list* results1 = this->results();
4521 const Typed_identifier_list* results2 = t->results();
4522 if (results1 != NULL)
4523 {
4524 Typed_identifier_list::const_iterator res1 = results1->begin();
4525 for (Typed_identifier_list::const_iterator res2 = results2->begin();
4526 res2 != results2->end();
4527 ++res2, ++res1)
4528 {
4529 if (res1->name() != res2->name())
4530 {
4531 if (reason != NULL)
4532 *reason = "result name changed";
4533 return false;
4534 }
4535
4536 // This is called at parse time, so we may have unknown
4537 // types.
4538 Type* t1 = res1->type()->forwarded();
4539 Type* t2 = res2->type()->forwarded();
4540 if (t1 != t2
4541 && t1->forward_declaration_type() != NULL
4542 && (t2->forward_declaration_type() == NULL
4543 || (t1->forward_declaration_type()->named_object()
4544 != t2->forward_declaration_type()->named_object())))
4545 return false;
4546 }
4547 }
4548
4549 return true;
4550 }
4551
4552 // Check whether T is the same as this type.
4553
4554 bool
4555 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
4556 int flags, std::string* reason) const
4557 {
4558 if (this->is_backend_function_type() != t->is_backend_function_type())
4559 return false;
4560
4561 if (!ignore_receiver)
4562 {
4563 const Typed_identifier* r1 = this->receiver();
4564 const Typed_identifier* r2 = t->receiver();
4565 if ((r1 != NULL) != (r2 != NULL))
4566 {
4567 if (reason != NULL)
4568 *reason = _("different receiver types");
4569 return false;
4570 }
4571 if (r1 != NULL)
4572 {
4573 if (!Type::are_identical(r1->type(), r2->type(), flags, reason))
4574 {
4575 if (reason != NULL && !reason->empty())
4576 *reason = "receiver: " + *reason;
4577 return false;
4578 }
4579 }
4580 }
4581
4582 const Typed_identifier_list* parms1 = this->parameters();
4583 if (parms1 != NULL && parms1->empty())
4584 parms1 = NULL;
4585 const Typed_identifier_list* parms2 = t->parameters();
4586 if (parms2 != NULL && parms2->empty())
4587 parms2 = NULL;
4588 if ((parms1 != NULL) != (parms2 != NULL))
4589 {
4590 if (reason != NULL)
4591 *reason = _("different number of parameters");
4592 return false;
4593 }
4594 if (parms1 != NULL)
4595 {
4596 Typed_identifier_list::const_iterator p1 = parms1->begin();
4597 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
4598 p2 != parms2->end();
4599 ++p2, ++p1)
4600 {
4601 if (p1 == parms1->end())
4602 {
4603 if (reason != NULL)
4604 *reason = _("different number of parameters");
4605 return false;
4606 }
4607
4608 if (!Type::are_identical(p1->type(), p2->type(), flags, NULL))
4609 {
4610 if (reason != NULL)
4611 *reason = _("different parameter types");
4612 return false;
4613 }
4614 }
4615 if (p1 != parms1->end())
4616 {
4617 if (reason != NULL)
4618 *reason = _("different number of parameters");
4619 return false;
4620 }
4621 }
4622
4623 if (this->is_varargs() != t->is_varargs())
4624 {
4625 if (reason != NULL)
4626 *reason = _("different varargs");
4627 return false;
4628 }
4629
4630 const Typed_identifier_list* results1 = this->results();
4631 if (results1 != NULL && results1->empty())
4632 results1 = NULL;
4633 const Typed_identifier_list* results2 = t->results();
4634 if (results2 != NULL && results2->empty())
4635 results2 = NULL;
4636 if ((results1 != NULL) != (results2 != NULL))
4637 {
4638 if (reason != NULL)
4639 *reason = _("different number of results");
4640 return false;
4641 }
4642 if (results1 != NULL)
4643 {
4644 Typed_identifier_list::const_iterator res1 = results1->begin();
4645 for (Typed_identifier_list::const_iterator res2 = results2->begin();
4646 res2 != results2->end();
4647 ++res2, ++res1)
4648 {
4649 if (res1 == results1->end())
4650 {
4651 if (reason != NULL)
4652 *reason = _("different number of results");
4653 return false;
4654 }
4655
4656 if (!Type::are_identical(res1->type(), res2->type(), flags, NULL))
4657 {
4658 if (reason != NULL)
4659 *reason = _("different result types");
4660 return false;
4661 }
4662 }
4663 if (res1 != results1->end())
4664 {
4665 if (reason != NULL)
4666 *reason = _("different number of results");
4667 return false;
4668 }
4669 }
4670
4671 return true;
4672 }
4673
4674 // Hash code.
4675
4676 unsigned int
4677 Function_type::do_hash_for_method(Gogo* gogo, int flags) const
4678 {
4679 unsigned int ret = 0;
4680 // We ignore the receiver type for hash codes, because we need to
4681 // get the same hash code for a method in an interface and a method
4682 // declared for a type. The former will not have a receiver.
4683 if (this->parameters_ != NULL)
4684 {
4685 int shift = 1;
4686 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
4687 p != this->parameters_->end();
4688 ++p, ++shift)
4689 ret += p->type()->hash_for_method(gogo, flags) << shift;
4690 }
4691 if (this->results_ != NULL)
4692 {
4693 int shift = 2;
4694 for (Typed_identifier_list::const_iterator p = this->results_->begin();
4695 p != this->results_->end();
4696 ++p, ++shift)
4697 ret += p->type()->hash_for_method(gogo, flags) << shift;
4698 }
4699 if (this->is_varargs_)
4700 ret += 1;
4701 ret <<= 4;
4702 return ret;
4703 }
4704
4705 // Hash result parameters.
4706
4707 unsigned int
4708 Function_type::Results_hash::operator()(const Typed_identifier_list* t) const
4709 {
4710 unsigned int hash = 0;
4711 for (Typed_identifier_list::const_iterator p = t->begin();
4712 p != t->end();
4713 ++p)
4714 {
4715 hash <<= 2;
4716 hash = Gogo::hash_string(p->name(), hash);
4717 hash += p->type()->hash_for_method(NULL, Type::COMPARE_TAGS);
4718 }
4719 return hash;
4720 }
4721
4722 // Compare result parameters so that can map identical result
4723 // parameters to a single struct type.
4724
4725 bool
4726 Function_type::Results_equal::operator()(const Typed_identifier_list* a,
4727 const Typed_identifier_list* b) const
4728 {
4729 if (a->size() != b->size())
4730 return false;
4731 Typed_identifier_list::const_iterator pa = a->begin();
4732 for (Typed_identifier_list::const_iterator pb = b->begin();
4733 pb != b->end();
4734 ++pa, ++pb)
4735 {
4736 if (pa->name() != pb->name()
4737 || !Type::are_identical(pa->type(), pb->type(), Type::COMPARE_TAGS,
4738 NULL))
4739 return false;
4740 }
4741 return true;
4742 }
4743
4744 // Hash from results to a backend struct type.
4745
4746 Function_type::Results_structs Function_type::results_structs;
4747
4748 // Get the backend representation for a function type.
4749
4750 Btype*
4751 Function_type::get_backend_fntype(Gogo* gogo)
4752 {
4753 if (this->fnbtype_ == NULL)
4754 {
4755 Backend::Btyped_identifier breceiver;
4756 if (this->receiver_ != NULL)
4757 {
4758 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
4759
4760 // We always pass the address of the receiver parameter, in
4761 // order to make interface calls work with unknown types,
4762 // except for direct interface types where the interface call
4763 // actually passes the underlying pointer of the value.
4764 Type* rtype = this->receiver_->type();
4765 if (rtype->points_to() == NULL)
4766 {
4767 if (rtype->is_direct_iface_type())
4768 rtype = Type::make_pointer_type(Type::make_void_type());
4769 else
4770 rtype = Type::make_pointer_type(rtype);
4771 }
4772 breceiver.btype = rtype->get_backend(gogo);
4773 breceiver.location = this->receiver_->location();
4774 }
4775
4776 std::vector<Backend::Btyped_identifier> bparameters;
4777 if (this->parameters_ != NULL)
4778 {
4779 bparameters.resize(this->parameters_->size());
4780 size_t i = 0;
4781 for (Typed_identifier_list::const_iterator p =
4782 this->parameters_->begin(); p != this->parameters_->end();
4783 ++p, ++i)
4784 {
4785 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
4786 bparameters[i].btype = p->type()->get_backend(gogo);
4787 bparameters[i].location = p->location();
4788 }
4789 go_assert(i == bparameters.size());
4790 }
4791
4792 std::vector<Backend::Btyped_identifier> bresults;
4793 Btype* bresult_struct = NULL;
4794 if (this->results_ != NULL)
4795 {
4796 bresults.resize(this->results_->size());
4797 size_t i = 0;
4798 for (Typed_identifier_list::const_iterator p =
4799 this->results_->begin();
4800 p != this->results_->end();
4801 ++p, ++i)
4802 {
4803 bresults[i].name = Gogo::unpack_hidden_name(p->name());
4804 bresults[i].btype = p->type()->get_backend(gogo);
4805 bresults[i].location = p->location();
4806 }
4807 go_assert(i == bresults.size());
4808
4809 if (this->results_->size() > 1)
4810 {
4811 // Use the same results struct for all functions that
4812 // return the same set of results. This is useful to
4813 // unify calls to interface methods with other calls.
4814 std::pair<Typed_identifier_list*, Btype*> val;
4815 val.first = this->results_;
4816 val.second = NULL;
4817 std::pair<Results_structs::iterator, bool> ins =
4818 Function_type::results_structs.insert(val);
4819 if (ins.second)
4820 {
4821 // Build a new struct type.
4822 Struct_field_list* sfl = new Struct_field_list;
4823 for (Typed_identifier_list::const_iterator p =
4824 this->results_->begin();
4825 p != this->results_->end();
4826 ++p)
4827 {
4828 Typed_identifier tid = *p;
4829 if (tid.name().empty())
4830 tid = Typed_identifier("UNNAMED", tid.type(),
4831 tid.location());
4832 sfl->push_back(Struct_field(tid));
4833 }
4834 Struct_type* st = Type::make_struct_type(sfl,
4835 this->location());
4836 st->set_is_struct_incomparable();
4837 ins.first->second = st->get_backend(gogo);
4838 }
4839 bresult_struct = ins.first->second;
4840 }
4841 }
4842
4843 this->fnbtype_ = gogo->backend()->function_type(breceiver, bparameters,
4844 bresults, bresult_struct,
4845 this->location());
4846
4847 }
4848
4849 return this->fnbtype_;
4850 }
4851
4852 // Get the backend representation for a Go function type.
4853
4854 Btype*
4855 Function_type::do_get_backend(Gogo* gogo)
4856 {
4857 // When we do anything with a function value other than call it, it
4858 // is represented as a pointer to a struct whose first field is the
4859 // actual function. So that is what we return as the type of a Go
4860 // function.
4861
4862 Location loc = this->location();
4863 Btype* struct_type =
4864 gogo->backend()->placeholder_struct_type("__go_descriptor", loc);
4865 Btype* ptr_struct_type = gogo->backend()->pointer_type(struct_type);
4866
4867 std::vector<Backend::Btyped_identifier> fields(1);
4868 fields[0].name = "code";
4869 fields[0].btype = this->get_backend_fntype(gogo);
4870 fields[0].location = loc;
4871 if (!gogo->backend()->set_placeholder_struct_type(struct_type, fields))
4872 return gogo->backend()->error_type();
4873 return ptr_struct_type;
4874 }
4875
4876 // The type of a function type descriptor.
4877
4878 Type*
4879 Function_type::make_function_type_descriptor_type()
4880 {
4881 static Type* ret;
4882 if (ret == NULL)
4883 {
4884 Type* tdt = Type::make_type_descriptor_type();
4885 Type* ptdt = Type::make_type_descriptor_ptr_type();
4886
4887 Type* bool_type = Type::lookup_bool_type();
4888
4889 Type* slice_type = Type::make_array_type(ptdt, NULL);
4890
4891 Struct_type* s = Type::make_builtin_struct_type(4,
4892 "", tdt,
4893 "dotdotdot", bool_type,
4894 "in", slice_type,
4895 "out", slice_type);
4896
4897 ret = Type::make_builtin_named_type("FuncType", s);
4898 }
4899
4900 return ret;
4901 }
4902
4903 // The type descriptor for a function type.
4904
4905 Expression*
4906 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4907 {
4908 Location bloc = Linemap::predeclared_location();
4909
4910 Type* ftdt = Function_type::make_function_type_descriptor_type();
4911
4912 const Struct_field_list* fields = ftdt->struct_type()->fields();
4913
4914 Expression_list* vals = new Expression_list();
4915 vals->reserve(4);
4916
4917 Struct_field_list::const_iterator p = fields->begin();
4918 go_assert(p->is_field_name("_type"));
4919 vals->push_back(this->type_descriptor_constructor(gogo,
4920 RUNTIME_TYPE_KIND_FUNC,
4921 name, NULL, true));
4922
4923 ++p;
4924 go_assert(p->is_field_name("dotdotdot"));
4925 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
4926
4927 ++p;
4928 go_assert(p->is_field_name("in"));
4929 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
4930 this->parameters()));
4931
4932 ++p;
4933 go_assert(p->is_field_name("out"));
4934 vals->push_back(this->type_descriptor_params(p->type(), NULL,
4935 this->results()));
4936
4937 ++p;
4938 go_assert(p == fields->end());
4939
4940 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
4941 }
4942
4943 // Return a composite literal for the parameters or results of a type
4944 // descriptor.
4945
4946 Expression*
4947 Function_type::type_descriptor_params(Type* params_type,
4948 const Typed_identifier* receiver,
4949 const Typed_identifier_list* params)
4950 {
4951 Location bloc = Linemap::predeclared_location();
4952
4953 if (receiver == NULL && params == NULL)
4954 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
4955
4956 Expression_list* vals = new Expression_list();
4957 vals->reserve((params == NULL ? 0 : params->size())
4958 + (receiver != NULL ? 1 : 0));
4959
4960 if (receiver != NULL)
4961 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
4962
4963 if (params != NULL)
4964 {
4965 for (Typed_identifier_list::const_iterator p = params->begin();
4966 p != params->end();
4967 ++p)
4968 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
4969 }
4970
4971 return Expression::make_slice_composite_literal(params_type, vals, bloc);
4972 }
4973
4974 // The reflection string.
4975
4976 void
4977 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
4978 {
4979 // FIXME: Turn this off until we straighten out the type of the
4980 // struct field used in a go statement which calls a method.
4981 // go_assert(this->receiver_ == NULL);
4982
4983 ret->append("func");
4984
4985 if (this->receiver_ != NULL)
4986 {
4987 ret->push_back('(');
4988 this->append_reflection(this->receiver_->type(), gogo, ret);
4989 ret->push_back(')');
4990 }
4991
4992 ret->push_back('(');
4993 const Typed_identifier_list* params = this->parameters();
4994 if (params != NULL)
4995 {
4996 bool is_varargs = this->is_varargs_;
4997 for (Typed_identifier_list::const_iterator p = params->begin();
4998 p != params->end();
4999 ++p)
5000 {
5001 if (p != params->begin())
5002 ret->append(", ");
5003 if (!is_varargs || p + 1 != params->end())
5004 this->append_reflection(p->type(), gogo, ret);
5005 else
5006 {
5007 ret->append("...");
5008 this->append_reflection(p->type()->array_type()->element_type(),
5009 gogo, ret);
5010 }
5011 }
5012 }
5013 ret->push_back(')');
5014
5015 const Typed_identifier_list* results = this->results();
5016 if (results != NULL && !results->empty())
5017 {
5018 if (results->size() == 1)
5019 ret->push_back(' ');
5020 else
5021 ret->append(" (");
5022 for (Typed_identifier_list::const_iterator p = results->begin();
5023 p != results->end();
5024 ++p)
5025 {
5026 if (p != results->begin())
5027 ret->append(", ");
5028 this->append_reflection(p->type(), gogo, ret);
5029 }
5030 if (results->size() > 1)
5031 ret->push_back(')');
5032 }
5033 }
5034
5035 // Export a function type.
5036
5037 void
5038 Function_type::do_export(Export* exp) const
5039 {
5040 // We don't write out the receiver. The only function types which
5041 // should have a receiver are the ones associated with explicitly
5042 // defined methods. For those the receiver type is written out by
5043 // Function::export_func.
5044
5045 exp->write_c_string("(");
5046 bool first = true;
5047 if (this->parameters_ != NULL)
5048 {
5049 bool is_varargs = this->is_varargs_;
5050 for (Typed_identifier_list::const_iterator p =
5051 this->parameters_->begin();
5052 p != this->parameters_->end();
5053 ++p)
5054 {
5055 if (first)
5056 first = false;
5057 else
5058 exp->write_c_string(", ");
5059 exp->write_name(p->name());
5060 exp->write_c_string(" ");
5061 if (!is_varargs || p + 1 != this->parameters_->end())
5062 exp->write_type(p->type());
5063 else
5064 {
5065 exp->write_c_string("...");
5066 exp->write_type(p->type()->array_type()->element_type());
5067 }
5068 }
5069 }
5070 exp->write_c_string(")");
5071
5072 const Typed_identifier_list* results = this->results_;
5073 if (results != NULL)
5074 {
5075 exp->write_c_string(" ");
5076 if (results->size() == 1 && results->begin()->name().empty())
5077 exp->write_type(results->begin()->type());
5078 else
5079 {
5080 first = true;
5081 exp->write_c_string("(");
5082 for (Typed_identifier_list::const_iterator p = results->begin();
5083 p != results->end();
5084 ++p)
5085 {
5086 if (first)
5087 first = false;
5088 else
5089 exp->write_c_string(", ");
5090 exp->write_name(p->name());
5091 exp->write_c_string(" ");
5092 exp->write_type(p->type());
5093 }
5094 exp->write_c_string(")");
5095 }
5096 }
5097 }
5098
5099 // Import a function type.
5100
5101 Function_type*
5102 Function_type::do_import(Import* imp)
5103 {
5104 imp->require_c_string("(");
5105 Typed_identifier_list* parameters;
5106 bool is_varargs = false;
5107 if (imp->peek_char() == ')')
5108 parameters = NULL;
5109 else
5110 {
5111 parameters = new Typed_identifier_list();
5112 while (true)
5113 {
5114 std::string name = imp->read_name();
5115 imp->require_c_string(" ");
5116
5117 if (imp->match_c_string("..."))
5118 {
5119 imp->advance(3);
5120 is_varargs = true;
5121 }
5122
5123 Type* ptype = imp->read_type();
5124 if (is_varargs)
5125 ptype = Type::make_array_type(ptype, NULL);
5126 parameters->push_back(Typed_identifier(name, ptype,
5127 imp->location()));
5128 if (imp->peek_char() != ',')
5129 break;
5130 go_assert(!is_varargs);
5131 imp->require_c_string(", ");
5132 }
5133 }
5134 imp->require_c_string(")");
5135
5136 Typed_identifier_list* results;
5137 if (imp->peek_char() != ' ')
5138 results = NULL;
5139 else
5140 {
5141 imp->advance(1);
5142 results = new Typed_identifier_list;
5143 if (imp->peek_char() != '(')
5144 {
5145 Type* rtype = imp->read_type();
5146 results->push_back(Typed_identifier("", rtype, imp->location()));
5147 }
5148 else
5149 {
5150 imp->advance(1);
5151 while (true)
5152 {
5153 std::string name = imp->read_name();
5154 imp->require_c_string(" ");
5155 Type* rtype = imp->read_type();
5156 results->push_back(Typed_identifier(name, rtype,
5157 imp->location()));
5158 if (imp->peek_char() != ',')
5159 break;
5160 imp->require_c_string(", ");
5161 }
5162 imp->require_c_string(")");
5163 }
5164 }
5165
5166 Function_type* ret = Type::make_function_type(NULL, parameters, results,
5167 imp->location());
5168 if (is_varargs)
5169 ret->set_is_varargs();
5170 return ret;
5171 }
5172
5173 // Make a copy of a function type without a receiver.
5174
5175 Function_type*
5176 Function_type::copy_without_receiver() const
5177 {
5178 go_assert(this->is_method());
5179 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
5180 this->results_,
5181 this->location_);
5182 if (this->is_varargs())
5183 ret->set_is_varargs();
5184 if (this->is_builtin())
5185 ret->set_is_builtin();
5186 return ret;
5187 }
5188
5189 // Make a copy of a function type with a receiver.
5190
5191 Function_type*
5192 Function_type::copy_with_receiver(Type* receiver_type) const
5193 {
5194 go_assert(!this->is_method());
5195 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
5196 this->location_);
5197 Function_type* ret = Type::make_function_type(receiver, this->parameters_,
5198 this->results_,
5199 this->location_);
5200 if (this->is_varargs_)
5201 ret->set_is_varargs();
5202 return ret;
5203 }
5204
5205 // Make a copy of a function type with the receiver as the first
5206 // parameter.
5207
5208 Function_type*
5209 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver) const
5210 {
5211 go_assert(this->is_method());
5212 Typed_identifier_list* new_params = new Typed_identifier_list();
5213 Type* rtype = this->receiver_->type();
5214 if (want_pointer_receiver)
5215 rtype = Type::make_pointer_type(rtype);
5216 Typed_identifier receiver(this->receiver_->name(), rtype,
5217 this->receiver_->location());
5218 new_params->push_back(receiver);
5219 const Typed_identifier_list* orig_params = this->parameters_;
5220 if (orig_params != NULL && !orig_params->empty())
5221 {
5222 for (Typed_identifier_list::const_iterator p = orig_params->begin();
5223 p != orig_params->end();
5224 ++p)
5225 new_params->push_back(*p);
5226 }
5227 return Type::make_function_type(NULL, new_params, this->results_,
5228 this->location_);
5229 }
5230
5231 // Make a copy of a function type ignoring any receiver and adding a
5232 // closure parameter.
5233
5234 Function_type*
5235 Function_type::copy_with_names() const
5236 {
5237 Typed_identifier_list* new_params = new Typed_identifier_list();
5238 const Typed_identifier_list* orig_params = this->parameters_;
5239 if (orig_params != NULL && !orig_params->empty())
5240 {
5241 static int count;
5242 char buf[50];
5243 for (Typed_identifier_list::const_iterator p = orig_params->begin();
5244 p != orig_params->end();
5245 ++p)
5246 {
5247 snprintf(buf, sizeof buf, "pt.%u", count);
5248 ++count;
5249 new_params->push_back(Typed_identifier(buf, p->type(),
5250 p->location()));
5251 }
5252 }
5253
5254 const Typed_identifier_list* orig_results = this->results_;
5255 Typed_identifier_list* new_results;
5256 if (orig_results == NULL || orig_results->empty())
5257 new_results = NULL;
5258 else
5259 {
5260 new_results = new Typed_identifier_list();
5261 for (Typed_identifier_list::const_iterator p = orig_results->begin();
5262 p != orig_results->end();
5263 ++p)
5264 new_results->push_back(Typed_identifier("", p->type(),
5265 p->location()));
5266 }
5267
5268 return Type::make_function_type(NULL, new_params, new_results,
5269 this->location());
5270 }
5271
5272 // Make a function type.
5273
5274 Function_type*
5275 Type::make_function_type(Typed_identifier* receiver,
5276 Typed_identifier_list* parameters,
5277 Typed_identifier_list* results,
5278 Location location)
5279 {
5280 return new Function_type(receiver, parameters, results, location);
5281 }
5282
5283 // Make a backend function type.
5284
5285 Backend_function_type*
5286 Type::make_backend_function_type(Typed_identifier* receiver,
5287 Typed_identifier_list* parameters,
5288 Typed_identifier_list* results,
5289 Location location)
5290 {
5291 return new Backend_function_type(receiver, parameters, results, location);
5292 }
5293
5294 // Class Pointer_type.
5295
5296 // Traversal.
5297
5298 int
5299 Pointer_type::do_traverse(Traverse* traverse)
5300 {
5301 return Type::traverse(this->to_type_, traverse);
5302 }
5303
5304 // Hash code.
5305
5306 unsigned int
5307 Pointer_type::do_hash_for_method(Gogo* gogo, int flags) const
5308 {
5309 return this->to_type_->hash_for_method(gogo, flags) << 4;
5310 }
5311
5312 // Get the backend representation for a pointer type.
5313
5314 Btype*
5315 Pointer_type::do_get_backend(Gogo* gogo)
5316 {
5317 Btype* to_btype = this->to_type_->get_backend(gogo);
5318 return gogo->backend()->pointer_type(to_btype);
5319 }
5320
5321 // The type of a pointer type descriptor.
5322
5323 Type*
5324 Pointer_type::make_pointer_type_descriptor_type()
5325 {
5326 static Type* ret;
5327 if (ret == NULL)
5328 {
5329 Type* tdt = Type::make_type_descriptor_type();
5330 Type* ptdt = Type::make_type_descriptor_ptr_type();
5331
5332 Struct_type* s = Type::make_builtin_struct_type(2,
5333 "", tdt,
5334 "elem", ptdt);
5335
5336 ret = Type::make_builtin_named_type("PtrType", s);
5337 }
5338
5339 return ret;
5340 }
5341
5342 // The type descriptor for a pointer type.
5343
5344 Expression*
5345 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5346 {
5347 if (this->is_unsafe_pointer_type())
5348 {
5349 go_assert(name != NULL);
5350 return this->plain_type_descriptor(gogo,
5351 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
5352 name);
5353 }
5354 else
5355 {
5356 Location bloc = Linemap::predeclared_location();
5357
5358 const Methods* methods;
5359 Type* deref = this->points_to();
5360 if (deref->named_type() != NULL)
5361 methods = deref->named_type()->methods();
5362 else if (deref->struct_type() != NULL)
5363 methods = deref->struct_type()->methods();
5364 else
5365 methods = NULL;
5366
5367 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
5368
5369 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
5370
5371 Expression_list* vals = new Expression_list();
5372 vals->reserve(2);
5373
5374 Struct_field_list::const_iterator p = fields->begin();
5375 go_assert(p->is_field_name("_type"));
5376 vals->push_back(this->type_descriptor_constructor(gogo,
5377 RUNTIME_TYPE_KIND_PTR,
5378 name, methods, false));
5379
5380 ++p;
5381 go_assert(p->is_field_name("elem"));
5382 vals->push_back(Expression::make_type_descriptor(deref, bloc));
5383
5384 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
5385 }
5386 }
5387
5388 // Reflection string.
5389
5390 void
5391 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
5392 {
5393 ret->push_back('*');
5394 this->append_reflection(this->to_type_, gogo, ret);
5395 }
5396
5397 // Export.
5398
5399 void
5400 Pointer_type::do_export(Export* exp) const
5401 {
5402 exp->write_c_string("*");
5403 if (this->is_unsafe_pointer_type())
5404 exp->write_c_string("any");
5405 else
5406 exp->write_type(this->to_type_);
5407 }
5408
5409 // Import.
5410
5411 Pointer_type*
5412 Pointer_type::do_import(Import* imp)
5413 {
5414 imp->require_c_string("*");
5415 if (imp->match_c_string("any"))
5416 {
5417 imp->advance(3);
5418 return Type::make_pointer_type(Type::make_void_type());
5419 }
5420 Type* to = imp->read_type();
5421 return Type::make_pointer_type(to);
5422 }
5423
5424 // Cache of pointer types. Key is "to" type, value is pointer type
5425 // that points to key.
5426
5427 Type::Pointer_type_table Type::pointer_types;
5428
5429 // A list of placeholder pointer types. We keep this so we can ensure
5430 // they are finalized.
5431
5432 std::vector<Pointer_type*> Type::placeholder_pointers;
5433
5434 // Make a pointer type.
5435
5436 Pointer_type*
5437 Type::make_pointer_type(Type* to_type)
5438 {
5439 Pointer_type_table::const_iterator p = pointer_types.find(to_type);
5440 if (p != pointer_types.end())
5441 return p->second;
5442 Pointer_type* ret = new Pointer_type(to_type);
5443 pointer_types[to_type] = ret;
5444 return ret;
5445 }
5446
5447 // This helper is invoked immediately after named types have been
5448 // converted, to clean up any unresolved pointer types remaining in
5449 // the pointer type cache.
5450 //
5451 // The motivation for this routine: occasionally the compiler creates
5452 // some specific pointer type as part of a lowering operation (ex:
5453 // pointer-to-void), then Type::backend_type_size() is invoked on the
5454 // type (which creates a Btype placeholder for it), that placeholder
5455 // passed somewhere along the line to the back end, but since there is
5456 // no reference to the type in user code, there is never a call to
5457 // Type::finish_backend for the type (hence the Btype remains as an
5458 // unresolved placeholder). Calling this routine will clean up such
5459 // instances.
5460
5461 void
5462 Type::finish_pointer_types(Gogo* gogo)
5463 {
5464 // We don't use begin() and end() because it is possible to add new
5465 // placeholder pointer types as we finalized existing ones.
5466 for (size_t i = 0; i < Type::placeholder_pointers.size(); i++)
5467 {
5468 Pointer_type* pt = Type::placeholder_pointers[i];
5469 Type_btypes::iterator tbti = Type::type_btypes.find(pt);
5470 if (tbti != Type::type_btypes.end() && tbti->second.is_placeholder)
5471 {
5472 pt->finish_backend(gogo, tbti->second.btype);
5473 tbti->second.is_placeholder = false;
5474 }
5475 }
5476 }
5477
5478 // Class Nil_type.
5479
5480 // Get the backend representation of a nil type. FIXME: Is this ever
5481 // actually called?
5482
5483 Btype*
5484 Nil_type::do_get_backend(Gogo* gogo)
5485 {
5486 return gogo->backend()->pointer_type(gogo->backend()->void_type());
5487 }
5488
5489 // Make the nil type.
5490
5491 Type*
5492 Type::make_nil_type()
5493 {
5494 static Nil_type singleton_nil_type;
5495 return &singleton_nil_type;
5496 }
5497
5498 // The type of a function call which returns multiple values. This is
5499 // really a struct, but we don't want to confuse a function call which
5500 // returns a struct with a function call which returns multiple
5501 // values.
5502
5503 class Call_multiple_result_type : public Type
5504 {
5505 public:
5506 Call_multiple_result_type(Call_expression* call)
5507 : Type(TYPE_CALL_MULTIPLE_RESULT),
5508 call_(call)
5509 { }
5510
5511 protected:
5512 bool
5513 do_has_pointer() const
5514 { return false; }
5515
5516 bool
5517 do_compare_is_identity(Gogo*)
5518 { return false; }
5519
5520 Btype*
5521 do_get_backend(Gogo* gogo)
5522 {
5523 go_assert(saw_errors());
5524 return gogo->backend()->error_type();
5525 }
5526
5527 Expression*
5528 do_type_descriptor(Gogo*, Named_type*)
5529 {
5530 go_assert(saw_errors());
5531 return Expression::make_error(Linemap::unknown_location());
5532 }
5533
5534 void
5535 do_reflection(Gogo*, std::string*) const
5536 { go_assert(saw_errors()); }
5537
5538 void
5539 do_mangled_name(Gogo*, std::string*) const
5540 { go_assert(saw_errors()); }
5541
5542 private:
5543 // The expression being called.
5544 Call_expression* call_;
5545 };
5546
5547 // Make a call result type.
5548
5549 Type*
5550 Type::make_call_multiple_result_type(Call_expression* call)
5551 {
5552 return new Call_multiple_result_type(call);
5553 }
5554
5555 // Class Struct_field.
5556
5557 // Get the name of a field.
5558
5559 const std::string&
5560 Struct_field::field_name() const
5561 {
5562 const std::string& name(this->typed_identifier_.name());
5563 if (!name.empty())
5564 return name;
5565 else
5566 {
5567 // This is called during parsing, before anything is lowered, so
5568 // we have to be pretty careful to avoid dereferencing an
5569 // unknown type name.
5570 Type* t = this->typed_identifier_.type();
5571 Type* dt = t;
5572 if (t->classification() == Type::TYPE_POINTER)
5573 {
5574 // Very ugly.
5575 Pointer_type* ptype = static_cast<Pointer_type*>(t);
5576 dt = ptype->points_to();
5577 }
5578 if (dt->forward_declaration_type() != NULL)
5579 return dt->forward_declaration_type()->name();
5580 else if (dt->named_type() != NULL)
5581 {
5582 // Note that this can be an alias name.
5583 return dt->named_type()->name();
5584 }
5585 else if (t->is_error_type() || dt->is_error_type())
5586 {
5587 static const std::string error_string = "*error*";
5588 return error_string;
5589 }
5590 else
5591 {
5592 // Avoid crashing in the erroneous case where T is named but
5593 // DT is not.
5594 go_assert(t != dt);
5595 if (t->forward_declaration_type() != NULL)
5596 return t->forward_declaration_type()->name();
5597 else if (t->named_type() != NULL)
5598 return t->named_type()->name();
5599 else
5600 go_unreachable();
5601 }
5602 }
5603 }
5604
5605 // Return whether this field is named NAME.
5606
5607 bool
5608 Struct_field::is_field_name(const std::string& name) const
5609 {
5610 const std::string& me(this->typed_identifier_.name());
5611 if (!me.empty())
5612 return me == name;
5613 else
5614 {
5615 Type* t = this->typed_identifier_.type();
5616 if (t->points_to() != NULL)
5617 t = t->points_to();
5618 Named_type* nt = t->named_type();
5619 if (nt != NULL && nt->name() == name)
5620 return true;
5621
5622 // This is a horrible hack caused by the fact that we don't pack
5623 // the names of builtin types. FIXME.
5624 if (!this->is_imported_
5625 && nt != NULL
5626 && nt->is_builtin()
5627 && nt->name() == Gogo::unpack_hidden_name(name))
5628 return true;
5629
5630 return false;
5631 }
5632 }
5633
5634 // Return whether this field is an unexported field named NAME.
5635
5636 bool
5637 Struct_field::is_unexported_field_name(Gogo* gogo,
5638 const std::string& name) const
5639 {
5640 const std::string& field_name(this->field_name());
5641 if (Gogo::is_hidden_name(field_name)
5642 && name == Gogo::unpack_hidden_name(field_name)
5643 && gogo->pack_hidden_name(name, false) != field_name)
5644 return true;
5645
5646 // Check for the name of a builtin type. This is like the test in
5647 // is_field_name, only there we return false if this->is_imported_,
5648 // and here we return true.
5649 if (this->is_imported_ && this->is_anonymous())
5650 {
5651 Type* t = this->typed_identifier_.type();
5652 if (t->points_to() != NULL)
5653 t = t->points_to();
5654 Named_type* nt = t->named_type();
5655 if (nt != NULL
5656 && nt->is_builtin()
5657 && nt->name() == Gogo::unpack_hidden_name(name))
5658 return true;
5659 }
5660
5661 return false;
5662 }
5663
5664 // Return whether this field is an embedded built-in type.
5665
5666 bool
5667 Struct_field::is_embedded_builtin(Gogo* gogo) const
5668 {
5669 const std::string& name(this->field_name());
5670 // We know that a field is an embedded type if it is anonymous.
5671 // We can decide if it is a built-in type by checking to see if it is
5672 // registered globally under the field's name.
5673 // This allows us to distinguish between embedded built-in types and
5674 // embedded types that are aliases to built-in types.
5675 return (this->is_anonymous()
5676 && !Gogo::is_hidden_name(name)
5677 && gogo->lookup_global(name.c_str()) != NULL);
5678 }
5679
5680 // Class Struct_type.
5681
5682 // A hash table used to find identical unnamed structs so that they
5683 // share method tables.
5684
5685 Struct_type::Identical_structs Struct_type::identical_structs;
5686
5687 // A hash table used to merge method sets for identical unnamed
5688 // structs.
5689
5690 Struct_type::Struct_method_tables Struct_type::struct_method_tables;
5691
5692 // Traversal.
5693
5694 int
5695 Struct_type::do_traverse(Traverse* traverse)
5696 {
5697 Struct_field_list* fields = this->fields_;
5698 if (fields != NULL)
5699 {
5700 for (Struct_field_list::iterator p = fields->begin();
5701 p != fields->end();
5702 ++p)
5703 {
5704 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
5705 return TRAVERSE_EXIT;
5706 }
5707 }
5708 return TRAVERSE_CONTINUE;
5709 }
5710
5711 // Verify that the struct type is complete and valid.
5712
5713 bool
5714 Struct_type::do_verify()
5715 {
5716 Struct_field_list* fields = this->fields_;
5717 if (fields == NULL)
5718 return true;
5719 for (Struct_field_list::iterator p = fields->begin();
5720 p != fields->end();
5721 ++p)
5722 {
5723 Type* t = p->type();
5724 if (p->is_anonymous())
5725 {
5726 if ((t->named_type() != NULL && t->points_to() != NULL)
5727 || (t->named_type() == NULL && t->points_to() != NULL
5728 && t->points_to()->points_to() != NULL))
5729 {
5730 go_error_at(p->location(), "embedded type may not be a pointer");
5731 p->set_type(Type::make_error_type());
5732 }
5733 else if (t->points_to() != NULL
5734 && t->points_to()->interface_type() != NULL)
5735 {
5736 go_error_at(p->location(),
5737 "embedded type may not be pointer to interface");
5738 p->set_type(Type::make_error_type());
5739 }
5740 }
5741 }
5742 return true;
5743 }
5744
5745 // Whether this contains a pointer.
5746
5747 bool
5748 Struct_type::do_has_pointer() const
5749 {
5750 const Struct_field_list* fields = this->fields();
5751 if (fields == NULL)
5752 return false;
5753 for (Struct_field_list::const_iterator p = fields->begin();
5754 p != fields->end();
5755 ++p)
5756 {
5757 if (p->type()->has_pointer())
5758 return true;
5759 }
5760 return false;
5761 }
5762
5763 // Whether this type is identical to T.
5764
5765 bool
5766 Struct_type::is_identical(const Struct_type* t, int flags) const
5767 {
5768 if (this->is_struct_incomparable_ != t->is_struct_incomparable_)
5769 return false;
5770 const Struct_field_list* fields1 = this->fields();
5771 const Struct_field_list* fields2 = t->fields();
5772 if (fields1 == NULL || fields2 == NULL)
5773 return fields1 == fields2;
5774 Struct_field_list::const_iterator pf2 = fields2->begin();
5775 for (Struct_field_list::const_iterator pf1 = fields1->begin();
5776 pf1 != fields1->end();
5777 ++pf1, ++pf2)
5778 {
5779 if (pf2 == fields2->end())
5780 return false;
5781 if (pf1->field_name() != pf2->field_name())
5782 return false;
5783 if (pf1->is_anonymous() != pf2->is_anonymous()
5784 || !Type::are_identical(pf1->type(), pf2->type(), flags, NULL))
5785 return false;
5786 if ((flags & Type::COMPARE_TAGS) != 0)
5787 {
5788 if (!pf1->has_tag())
5789 {
5790 if (pf2->has_tag())
5791 return false;
5792 }
5793 else
5794 {
5795 if (!pf2->has_tag())
5796 return false;
5797 if (pf1->tag() != pf2->tag())
5798 return false;
5799 }
5800 }
5801 }
5802 if (pf2 != fields2->end())
5803 return false;
5804 return true;
5805 }
5806
5807 // Whether comparisons of this struct type are simple identity
5808 // comparisons.
5809
5810 bool
5811 Struct_type::do_compare_is_identity(Gogo* gogo)
5812 {
5813 const Struct_field_list* fields = this->fields_;
5814 if (fields == NULL)
5815 return true;
5816 int64_t offset = 0;
5817 for (Struct_field_list::const_iterator pf = fields->begin();
5818 pf != fields->end();
5819 ++pf)
5820 {
5821 if (Gogo::is_sink_name(pf->field_name()))
5822 return false;
5823
5824 if (!pf->type()->compare_is_identity(gogo))
5825 return false;
5826
5827 int64_t field_align;
5828 if (!pf->type()->backend_type_align(gogo, &field_align))
5829 return false;
5830 if ((offset & (field_align - 1)) != 0)
5831 {
5832 // This struct has padding. We don't guarantee that that
5833 // padding is zero-initialized for a stack variable, so we
5834 // can't use memcmp to compare struct values.
5835 return false;
5836 }
5837
5838 int64_t field_size;
5839 if (!pf->type()->backend_type_size(gogo, &field_size))
5840 return false;
5841 offset += field_size;
5842 }
5843
5844 int64_t struct_size;
5845 if (!this->backend_type_size(gogo, &struct_size))
5846 return false;
5847 if (offset != struct_size)
5848 {
5849 // Trailing padding may not be zero when on the stack.
5850 return false;
5851 }
5852
5853 return true;
5854 }
5855
5856 // Return whether this struct type is reflexive--whether a value of
5857 // this type is always equal to itself.
5858
5859 bool
5860 Struct_type::do_is_reflexive()
5861 {
5862 const Struct_field_list* fields = this->fields_;
5863 if (fields == NULL)
5864 return true;
5865 for (Struct_field_list::const_iterator pf = fields->begin();
5866 pf != fields->end();
5867 ++pf)
5868 {
5869 if (!pf->type()->is_reflexive())
5870 return false;
5871 }
5872 return true;
5873 }
5874
5875 // Return whether this struct type needs a key update when used as a
5876 // map key.
5877
5878 bool
5879 Struct_type::do_needs_key_update()
5880 {
5881 const Struct_field_list* fields = this->fields_;
5882 if (fields == NULL)
5883 return false;
5884 for (Struct_field_list::const_iterator pf = fields->begin();
5885 pf != fields->end();
5886 ++pf)
5887 {
5888 if (pf->type()->needs_key_update())
5889 return true;
5890 }
5891 return false;
5892 }
5893
5894 // Return whether computing the hash value of an instance of this
5895 // struct type might panic.
5896
5897 bool
5898 Struct_type::do_hash_might_panic()
5899 {
5900 const Struct_field_list* fields = this->fields_;
5901 if (fields == NULL)
5902 return false;
5903 for (Struct_field_list::const_iterator pf = fields->begin();
5904 pf != fields->end();
5905 ++pf)
5906 {
5907 if (pf->type()->hash_might_panic())
5908 return true;
5909 }
5910 return false;
5911 }
5912
5913 // Return whether this struct type is permitted to be in the heap.
5914
5915 bool
5916 Struct_type::do_in_heap()
5917 {
5918 const Struct_field_list* fields = this->fields_;
5919 if (fields == NULL)
5920 return true;
5921 for (Struct_field_list::const_iterator pf = fields->begin();
5922 pf != fields->end();
5923 ++pf)
5924 {
5925 if (!pf->type()->in_heap())
5926 return false;
5927 }
5928 return true;
5929 }
5930
5931 // Build identity and hash functions for this struct.
5932
5933 // Hash code.
5934
5935 unsigned int
5936 Struct_type::do_hash_for_method(Gogo* gogo, int flags) const
5937 {
5938 unsigned int ret = 0;
5939 if (this->fields() != NULL)
5940 {
5941 for (Struct_field_list::const_iterator pf = this->fields()->begin();
5942 pf != this->fields()->end();
5943 ++pf)
5944 ret = (ret << 1) + pf->type()->hash_for_method(gogo, flags);
5945 }
5946 ret <<= 2;
5947 if (this->is_struct_incomparable_)
5948 ret <<= 1;
5949 return ret;
5950 }
5951
5952 // Find the local field NAME.
5953
5954 const Struct_field*
5955 Struct_type::find_local_field(const std::string& name,
5956 unsigned int *pindex) const
5957 {
5958 const Struct_field_list* fields = this->fields_;
5959 if (fields == NULL)
5960 return NULL;
5961 unsigned int i = 0;
5962 for (Struct_field_list::const_iterator pf = fields->begin();
5963 pf != fields->end();
5964 ++pf, ++i)
5965 {
5966 if (pf->is_field_name(name))
5967 {
5968 if (pindex != NULL)
5969 *pindex = i;
5970 return &*pf;
5971 }
5972 }
5973 return NULL;
5974 }
5975
5976 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
5977
5978 Field_reference_expression*
5979 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
5980 Location location) const
5981 {
5982 unsigned int depth;
5983 return this->field_reference_depth(struct_expr, name, location, NULL,
5984 &depth);
5985 }
5986
5987 // Return an expression for a field, along with the depth at which it
5988 // was found.
5989
5990 Field_reference_expression*
5991 Struct_type::field_reference_depth(Expression* struct_expr,
5992 const std::string& name,
5993 Location location,
5994 Saw_named_type* saw,
5995 unsigned int* depth) const
5996 {
5997 const Struct_field_list* fields = this->fields_;
5998 if (fields == NULL)
5999 return NULL;
6000
6001 // Look for a field with this name.
6002 unsigned int i = 0;
6003 for (Struct_field_list::const_iterator pf = fields->begin();
6004 pf != fields->end();
6005 ++pf, ++i)
6006 {
6007 if (pf->is_field_name(name))
6008 {
6009 *depth = 0;
6010 return Expression::make_field_reference(struct_expr, i, location);
6011 }
6012 }
6013
6014 // Look for an anonymous field which contains a field with this
6015 // name.
6016 unsigned int found_depth = 0;
6017 Field_reference_expression* ret = NULL;
6018 i = 0;
6019 for (Struct_field_list::const_iterator pf = fields->begin();
6020 pf != fields->end();
6021 ++pf, ++i)
6022 {
6023 if (!pf->is_anonymous())
6024 continue;
6025
6026 Struct_type* st = pf->type()->deref()->struct_type();
6027 if (st == NULL)
6028 continue;
6029
6030 Saw_named_type* hold_saw = saw;
6031 Saw_named_type saw_here;
6032 Named_type* nt = pf->type()->named_type();
6033 if (nt == NULL)
6034 nt = pf->type()->deref()->named_type();
6035 if (nt != NULL)
6036 {
6037 Saw_named_type* q;
6038 for (q = saw; q != NULL; q = q->next)
6039 {
6040 if (q->nt == nt)
6041 {
6042 // If this is an error, it will be reported
6043 // elsewhere.
6044 break;
6045 }
6046 }
6047 if (q != NULL)
6048 continue;
6049 saw_here.next = saw;
6050 saw_here.nt = nt;
6051 saw = &saw_here;
6052 }
6053
6054 // Look for a reference using a NULL struct expression. If we
6055 // find one, fill in the struct expression with a reference to
6056 // this field.
6057 unsigned int subdepth;
6058 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
6059 location,
6060 saw,
6061 &subdepth);
6062
6063 saw = hold_saw;
6064
6065 if (sub == NULL)
6066 continue;
6067
6068 if (ret == NULL || subdepth < found_depth)
6069 {
6070 if (ret != NULL)
6071 delete ret;
6072 ret = sub;
6073 found_depth = subdepth;
6074 Expression* here = Expression::make_field_reference(struct_expr, i,
6075 location);
6076 if (pf->type()->points_to() != NULL)
6077 here = Expression::make_dereference(here,
6078 Expression::NIL_CHECK_DEFAULT,
6079 location);
6080 while (sub->expr() != NULL)
6081 {
6082 sub = sub->expr()->deref()->field_reference_expression();
6083 go_assert(sub != NULL);
6084 }
6085 sub->set_struct_expression(here);
6086 sub->set_implicit(true);
6087 }
6088 else if (subdepth > found_depth)
6089 delete sub;
6090 else
6091 {
6092 // We do not handle ambiguity here--it should be handled by
6093 // Type::bind_field_or_method.
6094 delete sub;
6095 found_depth = 0;
6096 ret = NULL;
6097 }
6098 }
6099
6100 if (ret != NULL)
6101 *depth = found_depth + 1;
6102
6103 return ret;
6104 }
6105
6106 // Return the total number of fields, including embedded fields.
6107
6108 unsigned int
6109 Struct_type::total_field_count() const
6110 {
6111 if (this->fields_ == NULL)
6112 return 0;
6113 unsigned int ret = 0;
6114 for (Struct_field_list::const_iterator pf = this->fields_->begin();
6115 pf != this->fields_->end();
6116 ++pf)
6117 {
6118 if (!pf->is_anonymous() || pf->type()->struct_type() == NULL)
6119 ++ret;
6120 else
6121 ret += pf->type()->struct_type()->total_field_count();
6122 }
6123 return ret;
6124 }
6125
6126 // Return whether NAME is an unexported field, for better error reporting.
6127
6128 bool
6129 Struct_type::is_unexported_local_field(Gogo* gogo,
6130 const std::string& name) const
6131 {
6132 const Struct_field_list* fields = this->fields_;
6133 if (fields != NULL)
6134 {
6135 for (Struct_field_list::const_iterator pf = fields->begin();
6136 pf != fields->end();
6137 ++pf)
6138 if (pf->is_unexported_field_name(gogo, name))
6139 return true;
6140 }
6141 return false;
6142 }
6143
6144 // Finalize the methods of an unnamed struct.
6145
6146 void
6147 Struct_type::finalize_methods(Gogo* gogo)
6148 {
6149 if (this->all_methods_ != NULL)
6150 return;
6151
6152 // It is possible to have multiple identical structs that have
6153 // methods. We want them to share method tables. Otherwise we will
6154 // emit identical methods more than once, which is bad since they
6155 // will even have the same names.
6156 std::pair<Identical_structs::iterator, bool> ins =
6157 Struct_type::identical_structs.insert(std::make_pair(this, this));
6158 if (!ins.second)
6159 {
6160 // An identical struct was already entered into the hash table.
6161 // Note that finalize_methods is, fortunately, not recursive.
6162 this->all_methods_ = ins.first->second->all_methods_;
6163 return;
6164 }
6165
6166 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
6167 }
6168
6169 // Return the method NAME, or NULL if there isn't one or if it is
6170 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6171 // ambiguous.
6172
6173 Method*
6174 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
6175 {
6176 return Type::method_function(this->all_methods_, name, is_ambiguous);
6177 }
6178
6179 // Return a pointer to the interface method table for this type for
6180 // the interface INTERFACE. IS_POINTER is true if this is for a
6181 // pointer to THIS.
6182
6183 Expression*
6184 Struct_type::interface_method_table(Interface_type* interface,
6185 bool is_pointer)
6186 {
6187 std::pair<Struct_type*, Struct_type::Struct_method_table_pair*>
6188 val(this, NULL);
6189 std::pair<Struct_type::Struct_method_tables::iterator, bool> ins =
6190 Struct_type::struct_method_tables.insert(val);
6191
6192 Struct_method_table_pair* smtp;
6193 if (!ins.second)
6194 smtp = ins.first->second;
6195 else
6196 {
6197 smtp = new Struct_method_table_pair();
6198 smtp->first = NULL;
6199 smtp->second = NULL;
6200 ins.first->second = smtp;
6201 }
6202
6203 return Type::interface_method_table(this, interface, is_pointer,
6204 &smtp->first, &smtp->second);
6205 }
6206
6207 // Convert struct fields to the backend representation. This is not
6208 // declared in types.h so that types.h doesn't have to #include
6209 // backend.h.
6210
6211 static void
6212 get_backend_struct_fields(Gogo* gogo, Struct_type* type, bool use_placeholder,
6213 std::vector<Backend::Btyped_identifier>* bfields)
6214 {
6215 const Struct_field_list* fields = type->fields();
6216 bfields->resize(fields->size());
6217 size_t i = 0;
6218 int64_t lastsize = 0;
6219 bool saw_nonzero = false;
6220 for (Struct_field_list::const_iterator p = fields->begin();
6221 p != fields->end();
6222 ++p, ++i)
6223 {
6224 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
6225 (*bfields)[i].btype = (use_placeholder
6226 ? p->type()->get_backend_placeholder(gogo)
6227 : p->type()->get_backend(gogo));
6228 (*bfields)[i].location = p->location();
6229 lastsize = gogo->backend()->type_size((*bfields)[i].btype);
6230 if (lastsize != 0)
6231 saw_nonzero = true;
6232 }
6233 go_assert(i == fields->size());
6234 if (saw_nonzero && lastsize == 0)
6235 {
6236 // For nonzero-sized structs which end in a zero-sized thing, we add
6237 // an extra byte of padding to the type. This padding ensures that
6238 // taking the address of the zero-sized thing can't manufacture a
6239 // pointer to the next object in the heap. See issue 9401.
6240 size_t n = fields->size();
6241 bfields->resize(n + 1);
6242 (*bfields)[n].name = "_";
6243 (*bfields)[n].btype = Type::lookup_integer_type("uint8")->get_backend(gogo);
6244 (*bfields)[n].location = (*bfields)[n-1].location;
6245 type->set_has_padding();
6246 }
6247 }
6248
6249 // Get the backend representation for a struct type.
6250
6251 Btype*
6252 Struct_type::do_get_backend(Gogo* gogo)
6253 {
6254 std::vector<Backend::Btyped_identifier> bfields;
6255 get_backend_struct_fields(gogo, this, false, &bfields);
6256 return gogo->backend()->struct_type(bfields);
6257 }
6258
6259 // Finish the backend representation of the fields of a struct.
6260
6261 void
6262 Struct_type::finish_backend_fields(Gogo* gogo)
6263 {
6264 const Struct_field_list* fields = this->fields_;
6265 if (fields != NULL)
6266 {
6267 for (Struct_field_list::const_iterator p = fields->begin();
6268 p != fields->end();
6269 ++p)
6270 p->type()->get_backend(gogo);
6271 }
6272 }
6273
6274 // The type of a struct type descriptor.
6275
6276 Type*
6277 Struct_type::make_struct_type_descriptor_type()
6278 {
6279 static Type* ret;
6280 if (ret == NULL)
6281 {
6282 Type* tdt = Type::make_type_descriptor_type();
6283 Type* ptdt = Type::make_type_descriptor_ptr_type();
6284
6285 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6286 Type* string_type = Type::lookup_string_type();
6287 Type* pointer_string_type = Type::make_pointer_type(string_type);
6288
6289 Struct_type* sf =
6290 Type::make_builtin_struct_type(5,
6291 "name", pointer_string_type,
6292 "pkgPath", pointer_string_type,
6293 "typ", ptdt,
6294 "tag", pointer_string_type,
6295 "offsetAnon", uintptr_type);
6296 Type* nsf = Type::make_builtin_named_type("structField", sf);
6297
6298 Type* slice_type = Type::make_array_type(nsf, NULL);
6299
6300 Struct_type* s = Type::make_builtin_struct_type(2,
6301 "", tdt,
6302 "fields", slice_type);
6303
6304 ret = Type::make_builtin_named_type("StructType", s);
6305 }
6306
6307 return ret;
6308 }
6309
6310 // Build a type descriptor for a struct type.
6311
6312 Expression*
6313 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6314 {
6315 Location bloc = Linemap::predeclared_location();
6316
6317 Type* stdt = Struct_type::make_struct_type_descriptor_type();
6318
6319 const Struct_field_list* fields = stdt->struct_type()->fields();
6320
6321 Expression_list* vals = new Expression_list();
6322 vals->reserve(2);
6323
6324 const Methods* methods = this->methods();
6325 // A named struct should not have methods--the methods should attach
6326 // to the named type.
6327 go_assert(methods == NULL || name == NULL);
6328
6329 Struct_field_list::const_iterator ps = fields->begin();
6330 go_assert(ps->is_field_name("_type"));
6331 vals->push_back(this->type_descriptor_constructor(gogo,
6332 RUNTIME_TYPE_KIND_STRUCT,
6333 name, methods, true));
6334
6335 ++ps;
6336 go_assert(ps->is_field_name("fields"));
6337
6338 Expression_list* elements = new Expression_list();
6339 elements->reserve(this->fields_->size());
6340 Type* element_type = ps->type()->array_type()->element_type();
6341 for (Struct_field_list::const_iterator pf = this->fields_->begin();
6342 pf != this->fields_->end();
6343 ++pf)
6344 {
6345 const Struct_field_list* f = element_type->struct_type()->fields();
6346
6347 Expression_list* fvals = new Expression_list();
6348 fvals->reserve(5);
6349
6350 Struct_field_list::const_iterator q = f->begin();
6351 go_assert(q->is_field_name("name"));
6352 std::string n = Gogo::unpack_hidden_name(pf->field_name());
6353 Expression* s = Expression::make_string(n, bloc);
6354 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6355
6356 ++q;
6357 go_assert(q->is_field_name("pkgPath"));
6358 bool is_embedded_builtin = pf->is_embedded_builtin(gogo);
6359 if (!Gogo::is_hidden_name(pf->field_name()) && !is_embedded_builtin)
6360 fvals->push_back(Expression::make_nil(bloc));
6361 else
6362 {
6363 std::string n;
6364 if (is_embedded_builtin)
6365 n = gogo->package_name();
6366 else
6367 n = Gogo::hidden_name_pkgpath(pf->field_name());
6368 Expression* s = Expression::make_string(n, bloc);
6369 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6370 }
6371
6372 ++q;
6373 go_assert(q->is_field_name("typ"));
6374 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
6375
6376 ++q;
6377 go_assert(q->is_field_name("tag"));
6378 if (!pf->has_tag())
6379 fvals->push_back(Expression::make_nil(bloc));
6380 else
6381 {
6382 Expression* s = Expression::make_string(pf->tag(), bloc);
6383 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6384 }
6385
6386 ++q;
6387 go_assert(q->is_field_name("offsetAnon"));
6388 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6389 Expression* o = Expression::make_struct_field_offset(this, &*pf);
6390 Expression* one = Expression::make_integer_ul(1, uintptr_type, bloc);
6391 o = Expression::make_binary(OPERATOR_LSHIFT, o, one, bloc);
6392 int av = pf->is_anonymous() ? 1 : 0;
6393 Expression* anon = Expression::make_integer_ul(av, uintptr_type, bloc);
6394 o = Expression::make_binary(OPERATOR_OR, o, anon, bloc);
6395 fvals->push_back(o);
6396
6397 Expression* v = Expression::make_struct_composite_literal(element_type,
6398 fvals, bloc);
6399 elements->push_back(v);
6400 }
6401
6402 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
6403 elements, bloc));
6404
6405 return Expression::make_struct_composite_literal(stdt, vals, bloc);
6406 }
6407
6408 // Write the hash function for a struct which can not use the identity
6409 // function.
6410
6411 void
6412 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
6413 Function_type* hash_fntype,
6414 Function_type* equal_fntype)
6415 {
6416 Location bloc = Linemap::predeclared_location();
6417
6418 // The pointer to the struct that we are going to hash. This is an
6419 // argument to the hash function we are implementing here.
6420 Named_object* key_arg = gogo->lookup("key", NULL);
6421 go_assert(key_arg != NULL);
6422 Type* key_arg_type = key_arg->var_value()->type();
6423
6424 // The seed argument to the hash function.
6425 Named_object* seed_arg = gogo->lookup("seed", NULL);
6426 go_assert(seed_arg != NULL);
6427
6428 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6429
6430 // Make a temporary to hold the return value, initialized to the seed.
6431 Expression* ref = Expression::make_var_reference(seed_arg, bloc);
6432 Temporary_statement* retval = Statement::make_temporary(uintptr_type, ref,
6433 bloc);
6434 gogo->add_statement(retval);
6435
6436 // Make a temporary to hold the key as a uintptr.
6437 ref = Expression::make_var_reference(key_arg, bloc);
6438 ref = Expression::make_cast(uintptr_type, ref, bloc);
6439 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
6440 bloc);
6441 gogo->add_statement(key);
6442
6443 // Loop over the struct fields.
6444 const Struct_field_list* fields = this->fields_;
6445 for (Struct_field_list::const_iterator pf = fields->begin();
6446 pf != fields->end();
6447 ++pf)
6448 {
6449 if (Gogo::is_sink_name(pf->field_name()))
6450 continue;
6451
6452 // Get a pointer to the value of this field.
6453 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
6454 ref = Expression::make_temporary_reference(key, bloc);
6455 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
6456 bloc);
6457 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
6458
6459 // Get the hash function to use for the type of this field.
6460 Named_object* hash_fn;
6461 Named_object* equal_fn;
6462 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
6463 equal_fntype, &hash_fn, &equal_fn);
6464
6465 // Call the hash function for the field, passing retval as the seed.
6466 ref = Expression::make_temporary_reference(retval, bloc);
6467 Expression_list* args = new Expression_list();
6468 args->push_back(subkey);
6469 args->push_back(ref);
6470 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
6471 Expression* call = Expression::make_call(func, args, false, bloc);
6472
6473 // Set retval to the result.
6474 Temporary_reference_expression* tref =
6475 Expression::make_temporary_reference(retval, bloc);
6476 tref->set_is_lvalue();
6477 Statement* s = Statement::make_assignment(tref, call, bloc);
6478 gogo->add_statement(s);
6479 }
6480
6481 // Return retval to the caller of the hash function.
6482 Expression_list* vals = new Expression_list();
6483 ref = Expression::make_temporary_reference(retval, bloc);
6484 vals->push_back(ref);
6485 Statement* s = Statement::make_return_statement(vals, bloc);
6486 gogo->add_statement(s);
6487 }
6488
6489 // Write the equality function for a struct which can not use the
6490 // identity function.
6491
6492 void
6493 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
6494 {
6495 Location bloc = Linemap::predeclared_location();
6496
6497 // The pointers to the structs we are going to compare.
6498 Named_object* key1_arg = gogo->lookup("key1", NULL);
6499 Named_object* key2_arg = gogo->lookup("key2", NULL);
6500 go_assert(key1_arg != NULL && key2_arg != NULL);
6501
6502 // Build temporaries with the right types.
6503 Type* pt = Type::make_pointer_type(name != NULL
6504 ? static_cast<Type*>(name)
6505 : static_cast<Type*>(this));
6506
6507 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
6508 ref = Expression::make_unsafe_cast(pt, ref, bloc);
6509 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
6510 gogo->add_statement(p1);
6511
6512 ref = Expression::make_var_reference(key2_arg, bloc);
6513 ref = Expression::make_unsafe_cast(pt, ref, bloc);
6514 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
6515 gogo->add_statement(p2);
6516
6517 const Struct_field_list* fields = this->fields_;
6518 unsigned int field_index = 0;
6519 for (Struct_field_list::const_iterator pf = fields->begin();
6520 pf != fields->end();
6521 ++pf, ++field_index)
6522 {
6523 if (Gogo::is_sink_name(pf->field_name()))
6524 continue;
6525
6526 // Compare one field in both P1 and P2.
6527 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
6528 f1 = Expression::make_dereference(f1, Expression::NIL_CHECK_DEFAULT,
6529 bloc);
6530 f1 = Expression::make_field_reference(f1, field_index, bloc);
6531
6532 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
6533 f2 = Expression::make_dereference(f2, Expression::NIL_CHECK_DEFAULT,
6534 bloc);
6535 f2 = Expression::make_field_reference(f2, field_index, bloc);
6536
6537 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
6538
6539 // If the values are not equal, return false.
6540 gogo->start_block(bloc);
6541 Expression_list* vals = new Expression_list();
6542 vals->push_back(Expression::make_boolean(false, bloc));
6543 Statement* s = Statement::make_return_statement(vals, bloc);
6544 gogo->add_statement(s);
6545 Block* then_block = gogo->finish_block(bloc);
6546
6547 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
6548 gogo->add_statement(s);
6549 }
6550
6551 // All the fields are equal, so return true.
6552 Expression_list* vals = new Expression_list();
6553 vals->push_back(Expression::make_boolean(true, bloc));
6554 Statement* s = Statement::make_return_statement(vals, bloc);
6555 gogo->add_statement(s);
6556 }
6557
6558 // Reflection string.
6559
6560 void
6561 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
6562 {
6563 ret->append("struct {");
6564
6565 for (Struct_field_list::const_iterator p = this->fields_->begin();
6566 p != this->fields_->end();
6567 ++p)
6568 {
6569 if (p != this->fields_->begin())
6570 ret->push_back(';');
6571 ret->push_back(' ');
6572 if (!p->is_anonymous())
6573 {
6574 ret->append(Gogo::unpack_hidden_name(p->field_name()));
6575 ret->push_back(' ');
6576 }
6577 if (p->is_anonymous()
6578 && p->type()->named_type() != NULL
6579 && p->type()->named_type()->is_alias())
6580 p->type()->named_type()->append_reflection_type_name(gogo, true, ret);
6581 else
6582 this->append_reflection(p->type(), gogo, ret);
6583
6584 if (p->has_tag())
6585 {
6586 const std::string& tag(p->tag());
6587 ret->append(" \"");
6588 for (std::string::const_iterator p = tag.begin();
6589 p != tag.end();
6590 ++p)
6591 {
6592 if (*p == '\0')
6593 ret->append("\\x00");
6594 else if (*p == '\n')
6595 ret->append("\\n");
6596 else if (*p == '\t')
6597 ret->append("\\t");
6598 else if (*p == '"')
6599 ret->append("\\\"");
6600 else if (*p == '\\')
6601 ret->append("\\\\");
6602 else
6603 ret->push_back(*p);
6604 }
6605 ret->push_back('"');
6606 }
6607 }
6608
6609 if (!this->fields_->empty())
6610 ret->push_back(' ');
6611
6612 ret->push_back('}');
6613 }
6614
6615 // If the offset of field INDEX in the backend implementation can be
6616 // determined, set *POFFSET to the offset in bytes and return true.
6617 // Otherwise, return false.
6618
6619 bool
6620 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
6621 int64_t* poffset)
6622 {
6623 if (!this->is_backend_type_size_known(gogo))
6624 return false;
6625 Btype* bt = this->get_backend_placeholder(gogo);
6626 *poffset = gogo->backend()->type_field_offset(bt, index);
6627 return true;
6628 }
6629
6630 // Export.
6631
6632 void
6633 Struct_type::do_export(Export* exp) const
6634 {
6635 exp->write_c_string("struct { ");
6636 const Struct_field_list* fields = this->fields_;
6637 go_assert(fields != NULL);
6638 for (Struct_field_list::const_iterator p = fields->begin();
6639 p != fields->end();
6640 ++p)
6641 {
6642 if (p->is_anonymous())
6643 exp->write_string("? ");
6644 else
6645 {
6646 exp->write_string(p->field_name());
6647 exp->write_c_string(" ");
6648 }
6649 exp->write_type(p->type());
6650
6651 if (p->has_tag())
6652 {
6653 exp->write_c_string(" ");
6654 Expression* expr =
6655 Expression::make_string(p->tag(), Linemap::predeclared_location());
6656
6657 Export_function_body efb(exp, 0);
6658 expr->export_expression(&efb);
6659 exp->write_string(efb.body());
6660
6661 delete expr;
6662 }
6663
6664 exp->write_c_string("; ");
6665 }
6666 exp->write_c_string("}");
6667 }
6668
6669 // Import.
6670
6671 Struct_type*
6672 Struct_type::do_import(Import* imp)
6673 {
6674 imp->require_c_string("struct { ");
6675 Struct_field_list* fields = new Struct_field_list;
6676 if (imp->peek_char() != '}')
6677 {
6678 while (true)
6679 {
6680 std::string name;
6681 if (imp->match_c_string("? "))
6682 imp->advance(2);
6683 else
6684 {
6685 name = imp->read_identifier();
6686 imp->require_c_string(" ");
6687 }
6688 Type* ftype = imp->read_type();
6689
6690 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
6691 sf.set_is_imported();
6692
6693 if (imp->peek_char() == ' ')
6694 {
6695 imp->advance(1);
6696 Expression* expr = Expression::import_expression(imp,
6697 imp->location());
6698 String_expression* sexpr = expr->string_expression();
6699 go_assert(sexpr != NULL);
6700 sf.set_tag(sexpr->val());
6701 delete sexpr;
6702 }
6703
6704 imp->require_c_string("; ");
6705 fields->push_back(sf);
6706 if (imp->peek_char() == '}')
6707 break;
6708 }
6709 }
6710 imp->require_c_string("}");
6711
6712 return Type::make_struct_type(fields, imp->location());
6713 }
6714
6715 // Whether we can write this struct type to a C header file.
6716 // We can't if any of the fields are structs defined in a different package.
6717
6718 bool
6719 Struct_type::can_write_to_c_header(
6720 std::vector<const Named_object*>* requires,
6721 std::vector<const Named_object*>* declare) const
6722 {
6723 const Struct_field_list* fields = this->fields_;
6724 if (fields == NULL || fields->empty())
6725 return false;
6726 int sinks = 0;
6727 for (Struct_field_list::const_iterator p = fields->begin();
6728 p != fields->end();
6729 ++p)
6730 {
6731 if (p->is_anonymous())
6732 return false;
6733 if (!this->can_write_type_to_c_header(p->type(), requires, declare))
6734 return false;
6735 if (Gogo::message_name(p->field_name()) == "_")
6736 sinks++;
6737 }
6738 if (sinks > 1)
6739 return false;
6740 return true;
6741 }
6742
6743 // Whether we can write the type T to a C header file.
6744
6745 bool
6746 Struct_type::can_write_type_to_c_header(
6747 const Type* t,
6748 std::vector<const Named_object*>* requires,
6749 std::vector<const Named_object*>* declare) const
6750 {
6751 t = t->forwarded();
6752 switch (t->classification())
6753 {
6754 case TYPE_ERROR:
6755 case TYPE_FORWARD:
6756 return false;
6757
6758 case TYPE_VOID:
6759 case TYPE_BOOLEAN:
6760 case TYPE_INTEGER:
6761 case TYPE_FLOAT:
6762 case TYPE_COMPLEX:
6763 case TYPE_STRING:
6764 case TYPE_FUNCTION:
6765 case TYPE_MAP:
6766 case TYPE_CHANNEL:
6767 case TYPE_INTERFACE:
6768 return true;
6769
6770 case TYPE_POINTER:
6771 // Don't try to handle a pointer to an array.
6772 if (t->points_to()->array_type() != NULL
6773 && !t->points_to()->is_slice_type())
6774 return false;
6775
6776 if (t->points_to()->named_type() != NULL
6777 && t->points_to()->struct_type() != NULL)
6778 declare->push_back(t->points_to()->named_type()->named_object());
6779 return true;
6780
6781 case TYPE_STRUCT:
6782 return t->struct_type()->can_write_to_c_header(requires, declare);
6783
6784 case TYPE_ARRAY:
6785 if (t->is_slice_type())
6786 return true;
6787 return this->can_write_type_to_c_header(t->array_type()->element_type(),
6788 requires, declare);
6789
6790 case TYPE_NAMED:
6791 {
6792 const Named_object* no = t->named_type()->named_object();
6793 if (no->package() != NULL)
6794 {
6795 if (t->is_unsafe_pointer_type())
6796 return true;
6797 return false;
6798 }
6799 if (t->struct_type() != NULL)
6800 {
6801 requires->push_back(no);
6802 return t->struct_type()->can_write_to_c_header(requires, declare);
6803 }
6804 return this->can_write_type_to_c_header(t->base(), requires, declare);
6805 }
6806
6807 case TYPE_CALL_MULTIPLE_RESULT:
6808 case TYPE_NIL:
6809 case TYPE_SINK:
6810 default:
6811 go_unreachable();
6812 }
6813 }
6814
6815 // Write this struct to a C header file.
6816
6817 void
6818 Struct_type::write_to_c_header(std::ostream& os) const
6819 {
6820 const Struct_field_list* fields = this->fields_;
6821 for (Struct_field_list::const_iterator p = fields->begin();
6822 p != fields->end();
6823 ++p)
6824 {
6825 os << '\t';
6826 this->write_field_to_c_header(os, p->field_name(), p->type());
6827 os << ';' << std::endl;
6828 }
6829 }
6830
6831 // Write the type of a struct field to a C header file.
6832
6833 void
6834 Struct_type::write_field_to_c_header(std::ostream& os, const std::string& name,
6835 const Type *t) const
6836 {
6837 bool print_name = true;
6838 t = t->forwarded();
6839 switch (t->classification())
6840 {
6841 case TYPE_VOID:
6842 os << "void";
6843 break;
6844
6845 case TYPE_BOOLEAN:
6846 os << "_Bool";
6847 break;
6848
6849 case TYPE_INTEGER:
6850 {
6851 const Integer_type* it = t->integer_type();
6852 if (it->is_unsigned())
6853 os << 'u';
6854 os << "int" << it->bits() << "_t";
6855 }
6856 break;
6857
6858 case TYPE_FLOAT:
6859 switch (t->float_type()->bits())
6860 {
6861 case 32:
6862 os << "float";
6863 break;
6864 case 64:
6865 os << "double";
6866 break;
6867 default:
6868 go_unreachable();
6869 }
6870 break;
6871
6872 case TYPE_COMPLEX:
6873 switch (t->complex_type()->bits())
6874 {
6875 case 64:
6876 os << "float _Complex";
6877 break;
6878 case 128:
6879 os << "double _Complex";
6880 break;
6881 default:
6882 go_unreachable();
6883 }
6884 break;
6885
6886 case TYPE_STRING:
6887 os << "String";
6888 break;
6889
6890 case TYPE_FUNCTION:
6891 os << "FuncVal*";
6892 break;
6893
6894 case TYPE_POINTER:
6895 {
6896 std::vector<const Named_object*> requires;
6897 std::vector<const Named_object*> declare;
6898 if (!this->can_write_type_to_c_header(t->points_to(), &requires,
6899 &declare))
6900 os << "void*";
6901 else
6902 {
6903 this->write_field_to_c_header(os, "", t->points_to());
6904 os << '*';
6905 }
6906 }
6907 break;
6908
6909 case TYPE_MAP:
6910 os << "Map*";
6911 break;
6912
6913 case TYPE_CHANNEL:
6914 os << "Chan*";
6915 break;
6916
6917 case TYPE_INTERFACE:
6918 if (t->interface_type()->is_empty())
6919 os << "Eface";
6920 else
6921 os << "Iface";
6922 break;
6923
6924 case TYPE_STRUCT:
6925 os << "struct {" << std::endl;
6926 t->struct_type()->write_to_c_header(os);
6927 os << "\t}";
6928 break;
6929
6930 case TYPE_ARRAY:
6931 if (t->is_slice_type())
6932 os << "Slice";
6933 else
6934 {
6935 const Type *ele = t;
6936 std::vector<const Type*> array_types;
6937 while (ele->array_type() != NULL && !ele->is_slice_type())
6938 {
6939 array_types.push_back(ele);
6940 ele = ele->array_type()->element_type();
6941 }
6942 this->write_field_to_c_header(os, "", ele);
6943 os << ' ' << Gogo::message_name(name);
6944 print_name = false;
6945 while (!array_types.empty())
6946 {
6947 ele = array_types.back();
6948 array_types.pop_back();
6949 os << '[';
6950 Numeric_constant nc;
6951 if (!ele->array_type()->length()->numeric_constant_value(&nc))
6952 go_unreachable();
6953 mpz_t val;
6954 if (!nc.to_int(&val))
6955 go_unreachable();
6956 char* s = mpz_get_str(NULL, 10, val);
6957 os << s;
6958 free(s);
6959 mpz_clear(val);
6960 os << ']';
6961 }
6962 }
6963 break;
6964
6965 case TYPE_NAMED:
6966 {
6967 const Named_object* no = t->named_type()->named_object();
6968 if (t->struct_type() != NULL)
6969 os << "struct " << no->message_name();
6970 else if (t->is_unsafe_pointer_type())
6971 os << "void*";
6972 else if (t == Type::lookup_integer_type("uintptr"))
6973 os << "uintptr_t";
6974 else
6975 {
6976 this->write_field_to_c_header(os, name, t->base());
6977 print_name = false;
6978 }
6979 }
6980 break;
6981
6982 case TYPE_ERROR:
6983 case TYPE_FORWARD:
6984 case TYPE_CALL_MULTIPLE_RESULT:
6985 case TYPE_NIL:
6986 case TYPE_SINK:
6987 default:
6988 go_unreachable();
6989 }
6990
6991 if (print_name && !name.empty())
6992 os << ' ' << Gogo::message_name(name);
6993 }
6994
6995 // Make a struct type.
6996
6997 Struct_type*
6998 Type::make_struct_type(Struct_field_list* fields,
6999 Location location)
7000 {
7001 return new Struct_type(fields, location);
7002 }
7003
7004 // Class Array_type.
7005
7006 // Store the length of an array as an int64_t into *PLEN. Return
7007 // false if the length can not be determined. This will assert if
7008 // called for a slice.
7009
7010 bool
7011 Array_type::int_length(int64_t* plen) const
7012 {
7013 go_assert(this->length_ != NULL);
7014 Numeric_constant nc;
7015 if (!this->length_->numeric_constant_value(&nc))
7016 return false;
7017 return nc.to_memory_size(plen);
7018 }
7019
7020 // Whether two array types are identical.
7021
7022 bool
7023 Array_type::is_identical(const Array_type* t, int flags) const
7024 {
7025 if (!Type::are_identical(this->element_type(), t->element_type(),
7026 flags, NULL))
7027 return false;
7028
7029 if (this->is_array_incomparable_ != t->is_array_incomparable_)
7030 return false;
7031
7032 Expression* l1 = this->length();
7033 Expression* l2 = t->length();
7034
7035 // Slices of the same element type are identical.
7036 if (l1 == NULL && l2 == NULL)
7037 return true;
7038
7039 // Arrays of the same element type are identical if they have the
7040 // same length.
7041 if (l1 != NULL && l2 != NULL)
7042 {
7043 if (l1 == l2)
7044 return true;
7045
7046 // Try to determine the lengths. If we can't, assume the arrays
7047 // are not identical.
7048 bool ret = false;
7049 Numeric_constant nc1, nc2;
7050 if (l1->numeric_constant_value(&nc1)
7051 && l2->numeric_constant_value(&nc2))
7052 {
7053 mpz_t v1;
7054 if (nc1.to_int(&v1))
7055 {
7056 mpz_t v2;
7057 if (nc2.to_int(&v2))
7058 {
7059 ret = mpz_cmp(v1, v2) == 0;
7060 mpz_clear(v2);
7061 }
7062 mpz_clear(v1);
7063 }
7064 }
7065 return ret;
7066 }
7067
7068 // Otherwise the arrays are not identical.
7069 return false;
7070 }
7071
7072 // Traversal.
7073
7074 int
7075 Array_type::do_traverse(Traverse* traverse)
7076 {
7077 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
7078 return TRAVERSE_EXIT;
7079 if (this->length_ != NULL
7080 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
7081 return TRAVERSE_EXIT;
7082 return TRAVERSE_CONTINUE;
7083 }
7084
7085 // Check that the length is valid.
7086
7087 bool
7088 Array_type::verify_length()
7089 {
7090 if (this->length_ == NULL)
7091 return true;
7092
7093 Type_context context(Type::lookup_integer_type("int"), false);
7094 this->length_->determine_type(&context);
7095
7096 if (!this->length_->is_constant())
7097 {
7098 go_error_at(this->length_->location(), "array bound is not constant");
7099 return false;
7100 }
7101
7102 // For array types, the length expression can be an untyped constant
7103 // representable as an int, but we don't allow explicitly non-integer
7104 // values such as "float64(10)". See issues #13485 and #13486.
7105 if (this->length_->type()->integer_type() == NULL
7106 && !this->length_->type()->is_error_type())
7107 {
7108 go_error_at(this->length_->location(), "invalid array bound");
7109 return false;
7110 }
7111
7112 Numeric_constant nc;
7113 if (!this->length_->numeric_constant_value(&nc))
7114 {
7115 if (this->length_->type()->integer_type() != NULL
7116 || this->length_->type()->float_type() != NULL)
7117 go_error_at(this->length_->location(), "array bound is not constant");
7118 else
7119 go_error_at(this->length_->location(), "array bound is not numeric");
7120 return false;
7121 }
7122
7123 Type* int_type = Type::lookup_integer_type("int");
7124 unsigned int tbits = int_type->integer_type()->bits();
7125 unsigned long val;
7126 switch (nc.to_unsigned_long(&val))
7127 {
7128 case Numeric_constant::NC_UL_VALID:
7129 if (sizeof(val) >= tbits / 8 && val >> (tbits - 1) != 0)
7130 {
7131 go_error_at(this->length_->location(), "array bound overflows");
7132 return false;
7133 }
7134 break;
7135 case Numeric_constant::NC_UL_NOTINT:
7136 go_error_at(this->length_->location(), "array bound truncated to integer");
7137 return false;
7138 case Numeric_constant::NC_UL_NEGATIVE:
7139 go_error_at(this->length_->location(), "negative array bound");
7140 return false;
7141 case Numeric_constant::NC_UL_BIG:
7142 {
7143 mpz_t val;
7144 if (!nc.to_int(&val))
7145 go_unreachable();
7146 unsigned int bits = mpz_sizeinbase(val, 2);
7147 mpz_clear(val);
7148 if (bits >= tbits)
7149 {
7150 go_error_at(this->length_->location(), "array bound overflows");
7151 return false;
7152 }
7153 }
7154 break;
7155 default:
7156 go_unreachable();
7157 }
7158
7159 return true;
7160 }
7161
7162 // Verify the type.
7163
7164 bool
7165 Array_type::do_verify()
7166 {
7167 if (this->element_type()->is_error_type())
7168 return false;
7169 if (!this->verify_length())
7170 this->length_ = Expression::make_error(this->length_->location());
7171 return true;
7172 }
7173
7174 // Whether the type contains pointers. This is always true for a
7175 // slice. For an array it is true if the element type has pointers
7176 // and the length is greater than zero.
7177
7178 bool
7179 Array_type::do_has_pointer() const
7180 {
7181 if (this->length_ == NULL)
7182 return true;
7183 if (!this->element_type_->has_pointer())
7184 return false;
7185
7186 Numeric_constant nc;
7187 if (!this->length_->numeric_constant_value(&nc))
7188 {
7189 // Error reported elsewhere.
7190 return false;
7191 }
7192
7193 unsigned long val;
7194 switch (nc.to_unsigned_long(&val))
7195 {
7196 case Numeric_constant::NC_UL_VALID:
7197 return val > 0;
7198 case Numeric_constant::NC_UL_BIG:
7199 return true;
7200 default:
7201 // Error reported elsewhere.
7202 return false;
7203 }
7204 }
7205
7206 // Whether we can use memcmp to compare this array.
7207
7208 bool
7209 Array_type::do_compare_is_identity(Gogo* gogo)
7210 {
7211 if (this->length_ == NULL)
7212 return false;
7213
7214 // Check for [...], which indicates that this is not a real type.
7215 if (this->length_->is_nil_expression())
7216 return false;
7217
7218 if (!this->element_type_->compare_is_identity(gogo))
7219 return false;
7220
7221 // If there is any padding, then we can't use memcmp.
7222 int64_t size;
7223 int64_t align;
7224 if (!this->element_type_->backend_type_size(gogo, &size)
7225 || !this->element_type_->backend_type_align(gogo, &align))
7226 return false;
7227 if ((size & (align - 1)) != 0)
7228 return false;
7229
7230 return true;
7231 }
7232
7233 // Array type hash code.
7234
7235 unsigned int
7236 Array_type::do_hash_for_method(Gogo* gogo, int flags) const
7237 {
7238 unsigned int ret;
7239
7240 // There is no very convenient way to get a hash code for the
7241 // length.
7242 ret = this->element_type_->hash_for_method(gogo, flags) + 1;
7243 if (this->is_array_incomparable_)
7244 ret <<= 1;
7245 return ret;
7246 }
7247
7248 // Write the hash function for an array which can not use the identify
7249 // function.
7250
7251 void
7252 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
7253 Function_type* hash_fntype,
7254 Function_type* equal_fntype)
7255 {
7256 Location bloc = Linemap::predeclared_location();
7257
7258 // The pointer to the array that we are going to hash. This is an
7259 // argument to the hash function we are implementing here.
7260 Named_object* key_arg = gogo->lookup("key", NULL);
7261 go_assert(key_arg != NULL);
7262 Type* key_arg_type = key_arg->var_value()->type();
7263
7264 // The seed argument to the hash function.
7265 Named_object* seed_arg = gogo->lookup("seed", NULL);
7266 go_assert(seed_arg != NULL);
7267
7268 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7269
7270 // Make a temporary to hold the return value, initialized to the seed.
7271 Expression* ref = Expression::make_var_reference(seed_arg, bloc);
7272 Temporary_statement* retval = Statement::make_temporary(uintptr_type, ref,
7273 bloc);
7274 gogo->add_statement(retval);
7275
7276 // Make a temporary to hold the key as a uintptr.
7277 ref = Expression::make_var_reference(key_arg, bloc);
7278 ref = Expression::make_cast(uintptr_type, ref, bloc);
7279 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
7280 bloc);
7281 gogo->add_statement(key);
7282
7283 // Loop over the array elements.
7284 // for i = range a
7285 Type* int_type = Type::lookup_integer_type("int");
7286 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
7287 gogo->add_statement(index);
7288
7289 Expression* iref = Expression::make_temporary_reference(index, bloc);
7290 Expression* aref = Expression::make_var_reference(key_arg, bloc);
7291 Type* pt = Type::make_pointer_type(name != NULL
7292 ? static_cast<Type*>(name)
7293 : static_cast<Type*>(this));
7294 aref = Expression::make_cast(pt, aref, bloc);
7295 For_range_statement* for_range = Statement::make_for_range_statement(iref,
7296 NULL,
7297 aref,
7298 bloc);
7299
7300 gogo->start_block(bloc);
7301
7302 // Get the hash function for the element type.
7303 Named_object* hash_fn;
7304 Named_object* equal_fn;
7305 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
7306 hash_fntype, equal_fntype, &hash_fn,
7307 &equal_fn);
7308
7309 // Get a pointer to this element in the loop.
7310 Expression* subkey = Expression::make_temporary_reference(key, bloc);
7311 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
7312
7313 // Get the size of each element.
7314 Expression* ele_size = Expression::make_type_info(this->element_type_,
7315 Expression::TYPE_INFO_SIZE);
7316
7317 // Get the hash of this element, passing retval as the seed.
7318 ref = Expression::make_temporary_reference(retval, bloc);
7319 Expression_list* args = new Expression_list();
7320 args->push_back(subkey);
7321 args->push_back(ref);
7322 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
7323 Expression* call = Expression::make_call(func, args, false, bloc);
7324
7325 // Set retval to the result.
7326 Temporary_reference_expression* tref =
7327 Expression::make_temporary_reference(retval, bloc);
7328 tref->set_is_lvalue();
7329 Statement* s = Statement::make_assignment(tref, call, bloc);
7330 gogo->add_statement(s);
7331
7332 // Increase the element pointer.
7333 tref = Expression::make_temporary_reference(key, bloc);
7334 tref->set_is_lvalue();
7335 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
7336 bloc);
7337 Block* statements = gogo->finish_block(bloc);
7338
7339 for_range->add_statements(statements);
7340 gogo->add_statement(for_range);
7341
7342 // Return retval to the caller of the hash function.
7343 Expression_list* vals = new Expression_list();
7344 ref = Expression::make_temporary_reference(retval, bloc);
7345 vals->push_back(ref);
7346 s = Statement::make_return_statement(vals, bloc);
7347 gogo->add_statement(s);
7348 }
7349
7350 // Write the equality function for an array which can not use the
7351 // identity function.
7352
7353 void
7354 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
7355 {
7356 Location bloc = Linemap::predeclared_location();
7357
7358 // The pointers to the arrays we are going to compare.
7359 Named_object* key1_arg = gogo->lookup("key1", NULL);
7360 Named_object* key2_arg = gogo->lookup("key2", NULL);
7361 go_assert(key1_arg != NULL && key2_arg != NULL);
7362
7363 // Build temporaries for the keys with the right types.
7364 Type* pt = Type::make_pointer_type(name != NULL
7365 ? static_cast<Type*>(name)
7366 : static_cast<Type*>(this));
7367
7368 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
7369 ref = Expression::make_unsafe_cast(pt, ref, bloc);
7370 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
7371 gogo->add_statement(p1);
7372
7373 ref = Expression::make_var_reference(key2_arg, bloc);
7374 ref = Expression::make_unsafe_cast(pt, ref, bloc);
7375 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
7376 gogo->add_statement(p2);
7377
7378 // Loop over the array elements.
7379 // for i = range a
7380 Type* int_type = Type::lookup_integer_type("int");
7381 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
7382 gogo->add_statement(index);
7383
7384 Expression* iref = Expression::make_temporary_reference(index, bloc);
7385 Expression* aref = Expression::make_temporary_reference(p1, bloc);
7386 For_range_statement* for_range = Statement::make_for_range_statement(iref,
7387 NULL,
7388 aref,
7389 bloc);
7390
7391 gogo->start_block(bloc);
7392
7393 // Compare element in P1 and P2.
7394 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
7395 e1 = Expression::make_dereference(e1, Expression::NIL_CHECK_DEFAULT, bloc);
7396 ref = Expression::make_temporary_reference(index, bloc);
7397 e1 = Expression::make_array_index(e1, ref, NULL, NULL, bloc);
7398
7399 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
7400 e2 = Expression::make_dereference(e2, Expression::NIL_CHECK_DEFAULT, bloc);
7401 ref = Expression::make_temporary_reference(index, bloc);
7402 e2 = Expression::make_array_index(e2, ref, NULL, NULL, bloc);
7403
7404 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
7405
7406 // If the elements are not equal, return false.
7407 gogo->start_block(bloc);
7408 Expression_list* vals = new Expression_list();
7409 vals->push_back(Expression::make_boolean(false, bloc));
7410 Statement* s = Statement::make_return_statement(vals, bloc);
7411 gogo->add_statement(s);
7412 Block* then_block = gogo->finish_block(bloc);
7413
7414 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
7415 gogo->add_statement(s);
7416
7417 Block* statements = gogo->finish_block(bloc);
7418
7419 for_range->add_statements(statements);
7420 gogo->add_statement(for_range);
7421
7422 // All the elements are equal, so return true.
7423 vals = new Expression_list();
7424 vals->push_back(Expression::make_boolean(true, bloc));
7425 s = Statement::make_return_statement(vals, bloc);
7426 gogo->add_statement(s);
7427 }
7428
7429 // Get the backend representation of the fields of a slice. This is
7430 // not declared in types.h so that types.h doesn't have to #include
7431 // backend.h.
7432 //
7433 // We use int for the count and capacity fields. This matches 6g.
7434 // The language more or less assumes that we can't allocate space of a
7435 // size which does not fit in int.
7436
7437 static void
7438 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
7439 std::vector<Backend::Btyped_identifier>* bfields)
7440 {
7441 bfields->resize(3);
7442
7443 Type* pet = Type::make_pointer_type(type->element_type());
7444 Btype* pbet = (use_placeholder
7445 ? pet->get_backend_placeholder(gogo)
7446 : pet->get_backend(gogo));
7447 Location ploc = Linemap::predeclared_location();
7448
7449 Backend::Btyped_identifier* p = &(*bfields)[0];
7450 p->name = "__values";
7451 p->btype = pbet;
7452 p->location = ploc;
7453
7454 Type* int_type = Type::lookup_integer_type("int");
7455
7456 p = &(*bfields)[1];
7457 p->name = "__count";
7458 p->btype = int_type->get_backend(gogo);
7459 p->location = ploc;
7460
7461 p = &(*bfields)[2];
7462 p->name = "__capacity";
7463 p->btype = int_type->get_backend(gogo);
7464 p->location = ploc;
7465 }
7466
7467 // Get the backend representation for the type of this array. A fixed array is
7468 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7469 // just like an array in C. An open array is a struct with three
7470 // fields: a data pointer, the length, and the capacity.
7471
7472 Btype*
7473 Array_type::do_get_backend(Gogo* gogo)
7474 {
7475 if (this->length_ == NULL)
7476 {
7477 std::vector<Backend::Btyped_identifier> bfields;
7478 get_backend_slice_fields(gogo, this, false, &bfields);
7479 return gogo->backend()->struct_type(bfields);
7480 }
7481 else
7482 {
7483 Btype* element = this->get_backend_element(gogo, false);
7484 Bexpression* len = this->get_backend_length(gogo);
7485 return gogo->backend()->array_type(element, len);
7486 }
7487 }
7488
7489 // Return the backend representation of the element type.
7490
7491 Btype*
7492 Array_type::get_backend_element(Gogo* gogo, bool use_placeholder)
7493 {
7494 if (use_placeholder)
7495 return this->element_type_->get_backend_placeholder(gogo);
7496 else
7497 return this->element_type_->get_backend(gogo);
7498 }
7499
7500 // Return the backend representation of the length. The length may be
7501 // computed using a function call, so we must only evaluate it once.
7502
7503 Bexpression*
7504 Array_type::get_backend_length(Gogo* gogo)
7505 {
7506 go_assert(this->length_ != NULL);
7507 if (this->blength_ == NULL)
7508 {
7509 if (this->length_->is_error_expression())
7510 {
7511 this->blength_ = gogo->backend()->error_expression();
7512 return this->blength_;
7513 }
7514 Numeric_constant nc;
7515 mpz_t val;
7516 if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val))
7517 {
7518 if (mpz_sgn(val) < 0)
7519 {
7520 this->blength_ = gogo->backend()->error_expression();
7521 return this->blength_;
7522 }
7523 Type* t = nc.type();
7524 if (t == NULL)
7525 t = Type::lookup_integer_type("int");
7526 else if (t->is_abstract())
7527 t = t->make_non_abstract_type();
7528 Btype* btype = t->get_backend(gogo);
7529 this->blength_ =
7530 gogo->backend()->integer_constant_expression(btype, val);
7531 mpz_clear(val);
7532 }
7533 else
7534 {
7535 // Make up a translation context for the array length
7536 // expression. FIXME: This won't work in general.
7537 Translate_context context(gogo, NULL, NULL, NULL);
7538 this->blength_ = this->length_->get_backend(&context);
7539
7540 Btype* ibtype = Type::lookup_integer_type("int")->get_backend(gogo);
7541 this->blength_ =
7542 gogo->backend()->convert_expression(ibtype, this->blength_,
7543 this->length_->location());
7544 }
7545 }
7546 return this->blength_;
7547 }
7548
7549 // Finish backend representation of the array.
7550
7551 void
7552 Array_type::finish_backend_element(Gogo* gogo)
7553 {
7554 Type* et = this->array_type()->element_type();
7555 et->get_backend(gogo);
7556 if (this->is_slice_type())
7557 {
7558 // This relies on the fact that we always use the same
7559 // structure for a pointer to any given type.
7560 Type* pet = Type::make_pointer_type(et);
7561 pet->get_backend(gogo);
7562 }
7563 }
7564
7565 // Return an expression for a pointer to the values in ARRAY.
7566
7567 Expression*
7568 Array_type::get_value_pointer(Gogo*, Expression* array, bool is_lvalue) const
7569 {
7570 if (this->length() != NULL)
7571 {
7572 // Fixed array.
7573 go_assert(array->type()->array_type() != NULL);
7574 Type* etype = array->type()->array_type()->element_type();
7575 array = Expression::make_unary(OPERATOR_AND, array, array->location());
7576 return Expression::make_cast(Type::make_pointer_type(etype), array,
7577 array->location());
7578 }
7579
7580 // Slice.
7581
7582 if (is_lvalue)
7583 {
7584 Temporary_reference_expression* tref =
7585 array->temporary_reference_expression();
7586 Var_expression* ve = array->var_expression();
7587 if (tref != NULL)
7588 {
7589 tref = tref->copy()->temporary_reference_expression();
7590 tref->set_is_lvalue();
7591 array = tref;
7592 }
7593 else if (ve != NULL)
7594 {
7595 ve = new Var_expression(ve->named_object(), ve->location());
7596 array = ve;
7597 }
7598 }
7599
7600 return Expression::make_slice_info(array,
7601 Expression::SLICE_INFO_VALUE_POINTER,
7602 array->location());
7603 }
7604
7605 // Return an expression for the length of the array ARRAY which has this
7606 // type.
7607
7608 Expression*
7609 Array_type::get_length(Gogo*, Expression* array) const
7610 {
7611 if (this->length_ != NULL)
7612 return this->length_;
7613
7614 // This is a slice. We need to read the length field.
7615 return Expression::make_slice_info(array, Expression::SLICE_INFO_LENGTH,
7616 array->location());
7617 }
7618
7619 // Return an expression for the capacity of the array ARRAY which has this
7620 // type.
7621
7622 Expression*
7623 Array_type::get_capacity(Gogo*, Expression* array) const
7624 {
7625 if (this->length_ != NULL)
7626 return this->length_;
7627
7628 // This is a slice. We need to read the capacity field.
7629 return Expression::make_slice_info(array, Expression::SLICE_INFO_CAPACITY,
7630 array->location());
7631 }
7632
7633 // Export.
7634
7635 void
7636 Array_type::do_export(Export* exp) const
7637 {
7638 exp->write_c_string("[");
7639 if (this->length_ != NULL)
7640 {
7641 Numeric_constant nc;
7642 mpz_t val;
7643 if (!this->length_->numeric_constant_value(&nc) || !nc.to_int(&val))
7644 {
7645 go_assert(saw_errors());
7646 return;
7647 }
7648 char* s = mpz_get_str(NULL, 10, val);
7649 exp->write_string(s);
7650 exp->write_string(" ");
7651 mpz_clear(val);
7652 }
7653 exp->write_c_string("] ");
7654 exp->write_type(this->element_type_);
7655 }
7656
7657 // Import.
7658
7659 Array_type*
7660 Array_type::do_import(Import* imp)
7661 {
7662 imp->require_c_string("[");
7663 Expression* length;
7664 if (imp->peek_char() == ']')
7665 length = NULL;
7666 else
7667 length = Expression::import_expression(imp, imp->location());
7668 imp->require_c_string("] ");
7669 Type* element_type = imp->read_type();
7670 return Type::make_array_type(element_type, length);
7671 }
7672
7673 // The type of an array type descriptor.
7674
7675 Type*
7676 Array_type::make_array_type_descriptor_type()
7677 {
7678 static Type* ret;
7679 if (ret == NULL)
7680 {
7681 Type* tdt = Type::make_type_descriptor_type();
7682 Type* ptdt = Type::make_type_descriptor_ptr_type();
7683
7684 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7685
7686 Struct_type* sf =
7687 Type::make_builtin_struct_type(4,
7688 "", tdt,
7689 "elem", ptdt,
7690 "slice", ptdt,
7691 "len", uintptr_type);
7692
7693 ret = Type::make_builtin_named_type("ArrayType", sf);
7694 }
7695
7696 return ret;
7697 }
7698
7699 // The type of an slice type descriptor.
7700
7701 Type*
7702 Array_type::make_slice_type_descriptor_type()
7703 {
7704 static Type* ret;
7705 if (ret == NULL)
7706 {
7707 Type* tdt = Type::make_type_descriptor_type();
7708 Type* ptdt = Type::make_type_descriptor_ptr_type();
7709
7710 Struct_type* sf =
7711 Type::make_builtin_struct_type(2,
7712 "", tdt,
7713 "elem", ptdt);
7714
7715 ret = Type::make_builtin_named_type("SliceType", sf);
7716 }
7717
7718 return ret;
7719 }
7720
7721 // Build a type descriptor for an array/slice type.
7722
7723 Expression*
7724 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7725 {
7726 if (this->length_ != NULL)
7727 return this->array_type_descriptor(gogo, name);
7728 else
7729 return this->slice_type_descriptor(gogo, name);
7730 }
7731
7732 // Build a type descriptor for an array type.
7733
7734 Expression*
7735 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
7736 {
7737 Location bloc = Linemap::predeclared_location();
7738
7739 Type* atdt = Array_type::make_array_type_descriptor_type();
7740
7741 const Struct_field_list* fields = atdt->struct_type()->fields();
7742
7743 Expression_list* vals = new Expression_list();
7744 vals->reserve(3);
7745
7746 Struct_field_list::const_iterator p = fields->begin();
7747 go_assert(p->is_field_name("_type"));
7748 vals->push_back(this->type_descriptor_constructor(gogo,
7749 RUNTIME_TYPE_KIND_ARRAY,
7750 name, NULL, true));
7751
7752 ++p;
7753 go_assert(p->is_field_name("elem"));
7754 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
7755
7756 ++p;
7757 go_assert(p->is_field_name("slice"));
7758 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
7759 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
7760
7761 ++p;
7762 go_assert(p->is_field_name("len"));
7763 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
7764
7765 ++p;
7766 go_assert(p == fields->end());
7767
7768 return Expression::make_struct_composite_literal(atdt, vals, bloc);
7769 }
7770
7771 // Build a type descriptor for a slice type.
7772
7773 Expression*
7774 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
7775 {
7776 Location bloc = Linemap::predeclared_location();
7777
7778 Type* stdt = Array_type::make_slice_type_descriptor_type();
7779
7780 const Struct_field_list* fields = stdt->struct_type()->fields();
7781
7782 Expression_list* vals = new Expression_list();
7783 vals->reserve(2);
7784
7785 Struct_field_list::const_iterator p = fields->begin();
7786 go_assert(p->is_field_name("_type"));
7787 vals->push_back(this->type_descriptor_constructor(gogo,
7788 RUNTIME_TYPE_KIND_SLICE,
7789 name, NULL, true));
7790
7791 ++p;
7792 go_assert(p->is_field_name("elem"));
7793 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
7794
7795 ++p;
7796 go_assert(p == fields->end());
7797
7798 return Expression::make_struct_composite_literal(stdt, vals, bloc);
7799 }
7800
7801 // Reflection string.
7802
7803 void
7804 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
7805 {
7806 ret->push_back('[');
7807 if (this->length_ != NULL)
7808 {
7809 Numeric_constant nc;
7810 if (!this->length_->numeric_constant_value(&nc))
7811 {
7812 go_assert(saw_errors());
7813 return;
7814 }
7815 mpz_t val;
7816 if (!nc.to_int(&val))
7817 {
7818 go_assert(saw_errors());
7819 return;
7820 }
7821 char* s = mpz_get_str(NULL, 10, val);
7822 ret->append(s);
7823 free(s);
7824 mpz_clear(val);
7825 }
7826 ret->push_back(']');
7827
7828 this->append_reflection(this->element_type_, gogo, ret);
7829 }
7830
7831 // Make an array type.
7832
7833 Array_type*
7834 Type::make_array_type(Type* element_type, Expression* length)
7835 {
7836 return new Array_type(element_type, length);
7837 }
7838
7839 // Class Map_type.
7840
7841 Named_object* Map_type::zero_value;
7842 int64_t Map_type::zero_value_size;
7843 int64_t Map_type::zero_value_align;
7844
7845 // If this map requires the "fat" functions, return the pointer to
7846 // pass as the zero value to those functions. Otherwise, in the
7847 // normal case, return NULL. The map requires the "fat" functions if
7848 // the value size is larger than max_zero_size bytes. max_zero_size
7849 // must match maxZero in libgo/go/runtime/hashmap.go.
7850
7851 Expression*
7852 Map_type::fat_zero_value(Gogo* gogo)
7853 {
7854 int64_t valsize;
7855 if (!this->val_type_->backend_type_size(gogo, &valsize))
7856 {
7857 go_assert(saw_errors());
7858 return NULL;
7859 }
7860 if (valsize <= Map_type::max_zero_size)
7861 return NULL;
7862
7863 if (Map_type::zero_value_size < valsize)
7864 Map_type::zero_value_size = valsize;
7865
7866 int64_t valalign;
7867 if (!this->val_type_->backend_type_align(gogo, &valalign))
7868 {
7869 go_assert(saw_errors());
7870 return NULL;
7871 }
7872
7873 if (Map_type::zero_value_align < valalign)
7874 Map_type::zero_value_align = valalign;
7875
7876 Location bloc = Linemap::predeclared_location();
7877
7878 if (Map_type::zero_value == NULL)
7879 {
7880 // The final type will be set in backend_zero_value.
7881 Type* uint8_type = Type::lookup_integer_type("uint8");
7882 Expression* size = Expression::make_integer_ul(0, NULL, bloc);
7883 Array_type* array_type = Type::make_array_type(uint8_type, size);
7884 array_type->set_is_array_incomparable();
7885 Variable* var = new Variable(array_type, NULL, true, false, false, bloc);
7886 std::string name = gogo->map_zero_value_name();
7887 Map_type::zero_value = Named_object::make_variable(name, NULL, var);
7888 }
7889
7890 Expression* z = Expression::make_var_reference(Map_type::zero_value, bloc);
7891 z = Expression::make_unary(OPERATOR_AND, z, bloc);
7892 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
7893 z = Expression::make_cast(unsafe_ptr_type, z, bloc);
7894 return z;
7895 }
7896
7897 // Return whether VAR is the map zero value.
7898
7899 bool
7900 Map_type::is_zero_value(Variable* var)
7901 {
7902 return (Map_type::zero_value != NULL
7903 && Map_type::zero_value->var_value() == var);
7904 }
7905
7906 // Return the backend representation for the zero value.
7907
7908 Bvariable*
7909 Map_type::backend_zero_value(Gogo* gogo)
7910 {
7911 Location bloc = Linemap::predeclared_location();
7912
7913 go_assert(Map_type::zero_value != NULL);
7914
7915 Type* uint8_type = Type::lookup_integer_type("uint8");
7916 Btype* buint8_type = uint8_type->get_backend(gogo);
7917
7918 Type* int_type = Type::lookup_integer_type("int");
7919
7920 Expression* e = Expression::make_integer_int64(Map_type::zero_value_size,
7921 int_type, bloc);
7922 Translate_context context(gogo, NULL, NULL, NULL);
7923 Bexpression* blength = e->get_backend(&context);
7924
7925 Btype* barray_type = gogo->backend()->array_type(buint8_type, blength);
7926
7927 std::string zname = Map_type::zero_value->name();
7928 std::string asm_name(go_selectively_encode_id(zname));
7929 Bvariable* zvar =
7930 gogo->backend()->implicit_variable(zname, asm_name,
7931 barray_type, false, false, true,
7932 Map_type::zero_value_align);
7933 gogo->backend()->implicit_variable_set_init(zvar, zname, barray_type,
7934 false, false, true, NULL);
7935 return zvar;
7936 }
7937
7938 // Traversal.
7939
7940 int
7941 Map_type::do_traverse(Traverse* traverse)
7942 {
7943 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
7944 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
7945 return TRAVERSE_EXIT;
7946 return TRAVERSE_CONTINUE;
7947 }
7948
7949 // Check that the map type is OK.
7950
7951 bool
7952 Map_type::do_verify()
7953 {
7954 // The runtime support uses "map[void]void".
7955 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
7956 go_error_at(this->location_, "invalid map key type");
7957 if (!this->key_type_->in_heap())
7958 go_error_at(this->location_, "go:notinheap map key not allowed");
7959 if (!this->val_type_->in_heap())
7960 go_error_at(this->location_, "go:notinheap map value not allowed");
7961 return true;
7962 }
7963
7964 // Whether two map types are identical.
7965
7966 bool
7967 Map_type::is_identical(const Map_type* t, int flags) const
7968 {
7969 return (Type::are_identical(this->key_type(), t->key_type(), flags, NULL)
7970 && Type::are_identical(this->val_type(), t->val_type(), flags,
7971 NULL));
7972 }
7973
7974 // Hash code.
7975
7976 unsigned int
7977 Map_type::do_hash_for_method(Gogo* gogo, int flags) const
7978 {
7979 return (this->key_type_->hash_for_method(gogo, flags)
7980 + this->val_type_->hash_for_method(gogo, flags)
7981 + 2);
7982 }
7983
7984 // Get the backend representation for a map type. A map type is
7985 // represented as a pointer to a struct. The struct is hmap in
7986 // runtime/hashmap.go.
7987
7988 Btype*
7989 Map_type::do_get_backend(Gogo* gogo)
7990 {
7991 static Btype* backend_map_type;
7992 if (backend_map_type == NULL)
7993 {
7994 std::vector<Backend::Btyped_identifier> bfields(9);
7995
7996 Location bloc = Linemap::predeclared_location();
7997
7998 Type* int_type = Type::lookup_integer_type("int");
7999 bfields[0].name = "count";
8000 bfields[0].btype = int_type->get_backend(gogo);
8001 bfields[0].location = bloc;
8002
8003 Type* uint8_type = Type::lookup_integer_type("uint8");
8004 bfields[1].name = "flags";
8005 bfields[1].btype = uint8_type->get_backend(gogo);
8006 bfields[1].location = bloc;
8007
8008 bfields[2].name = "B";
8009 bfields[2].btype = bfields[1].btype;
8010 bfields[2].location = bloc;
8011
8012 Type* uint16_type = Type::lookup_integer_type("uint16");
8013 bfields[3].name = "noverflow";
8014 bfields[3].btype = uint16_type->get_backend(gogo);
8015 bfields[3].location = bloc;
8016
8017 Type* uint32_type = Type::lookup_integer_type("uint32");
8018 bfields[4].name = "hash0";
8019 bfields[4].btype = uint32_type->get_backend(gogo);
8020 bfields[4].location = bloc;
8021
8022 Btype* bvt = gogo->backend()->void_type();
8023 Btype* bpvt = gogo->backend()->pointer_type(bvt);
8024 bfields[5].name = "buckets";
8025 bfields[5].btype = bpvt;
8026 bfields[5].location = bloc;
8027
8028 bfields[6].name = "oldbuckets";
8029 bfields[6].btype = bpvt;
8030 bfields[6].location = bloc;
8031
8032 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8033 bfields[7].name = "nevacuate";
8034 bfields[7].btype = uintptr_type->get_backend(gogo);
8035 bfields[7].location = bloc;
8036
8037 bfields[8].name = "extra";
8038 bfields[8].btype = bpvt;
8039 bfields[8].location = bloc;
8040
8041 Btype *bt = gogo->backend()->struct_type(bfields);
8042 bt = gogo->backend()->named_type("runtime.hmap", bt, bloc);
8043 backend_map_type = gogo->backend()->pointer_type(bt);
8044 }
8045 return backend_map_type;
8046 }
8047
8048 // The type of a map type descriptor.
8049
8050 Type*
8051 Map_type::make_map_type_descriptor_type()
8052 {
8053 static Type* ret;
8054 if (ret == NULL)
8055 {
8056 Type* tdt = Type::make_type_descriptor_type();
8057 Type* ptdt = Type::make_type_descriptor_ptr_type();
8058 Type* uint8_type = Type::lookup_integer_type("uint8");
8059 Type* uint16_type = Type::lookup_integer_type("uint16");
8060 Type* uint32_type = Type::lookup_integer_type("uint32");
8061
8062 Struct_type* sf =
8063 Type::make_builtin_struct_type(8,
8064 "", tdt,
8065 "key", ptdt,
8066 "elem", ptdt,
8067 "bucket", ptdt,
8068 "keysize", uint8_type,
8069 "valuesize", uint8_type,
8070 "bucketsize", uint16_type,
8071 "flags", uint32_type);
8072
8073 ret = Type::make_builtin_named_type("MapType", sf);
8074 }
8075
8076 return ret;
8077 }
8078
8079 // Build a type descriptor for a map type.
8080
8081 Expression*
8082 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8083 {
8084 Location bloc = Linemap::predeclared_location();
8085
8086 Type* mtdt = Map_type::make_map_type_descriptor_type();
8087 Type* uint8_type = Type::lookup_integer_type("uint8");
8088 Type* uint16_type = Type::lookup_integer_type("uint16");
8089 Type* uint32_type = Type::lookup_integer_type("uint32");
8090
8091 int64_t keysize;
8092 if (!this->key_type_->backend_type_size(gogo, &keysize))
8093 {
8094 go_error_at(this->location_, "error determining map key type size");
8095 return Expression::make_error(this->location_);
8096 }
8097
8098 int64_t valsize;
8099 if (!this->val_type_->backend_type_size(gogo, &valsize))
8100 {
8101 go_error_at(this->location_, "error determining map value type size");
8102 return Expression::make_error(this->location_);
8103 }
8104
8105 int64_t ptrsize;
8106 if (!Type::make_pointer_type(uint8_type)->backend_type_size(gogo, &ptrsize))
8107 {
8108 go_assert(saw_errors());
8109 return Expression::make_error(this->location_);
8110 }
8111
8112 Type* bucket_type = this->bucket_type(gogo, keysize, valsize);
8113 if (bucket_type == NULL)
8114 {
8115 go_assert(saw_errors());
8116 return Expression::make_error(this->location_);
8117 }
8118
8119 int64_t bucketsize;
8120 if (!bucket_type->backend_type_size(gogo, &bucketsize))
8121 {
8122 go_assert(saw_errors());
8123 return Expression::make_error(this->location_);
8124 }
8125
8126 const Struct_field_list* fields = mtdt->struct_type()->fields();
8127
8128 Expression_list* vals = new Expression_list();
8129 vals->reserve(12);
8130
8131 Struct_field_list::const_iterator p = fields->begin();
8132 go_assert(p->is_field_name("_type"));
8133 vals->push_back(this->type_descriptor_constructor(gogo,
8134 RUNTIME_TYPE_KIND_MAP,
8135 name, NULL, true));
8136
8137 ++p;
8138 go_assert(p->is_field_name("key"));
8139 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
8140
8141 ++p;
8142 go_assert(p->is_field_name("elem"));
8143 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
8144
8145 ++p;
8146 go_assert(p->is_field_name("bucket"));
8147 vals->push_back(Expression::make_type_descriptor(bucket_type, bloc));
8148
8149 ++p;
8150 go_assert(p->is_field_name("keysize"));
8151 if (keysize > Map_type::max_key_size)
8152 vals->push_back(Expression::make_integer_int64(ptrsize, uint8_type, bloc));
8153 else
8154 vals->push_back(Expression::make_integer_int64(keysize, uint8_type, bloc));
8155
8156 ++p;
8157 go_assert(p->is_field_name("valuesize"));
8158 if (valsize > Map_type::max_val_size)
8159 vals->push_back(Expression::make_integer_int64(ptrsize, uint8_type, bloc));
8160 else
8161 vals->push_back(Expression::make_integer_int64(valsize, uint8_type, bloc));
8162
8163 ++p;
8164 go_assert(p->is_field_name("bucketsize"));
8165 vals->push_back(Expression::make_integer_int64(bucketsize, uint16_type,
8166 bloc));
8167
8168 ++p;
8169 go_assert(p->is_field_name("flags"));
8170 // As with the other fields, the flag bits must match the reflect
8171 // and runtime packages.
8172 unsigned long flags = 0;
8173 if (keysize > Map_type::max_key_size)
8174 flags |= 1;
8175 if (valsize > Map_type::max_val_size)
8176 flags |= 2;
8177 if (this->key_type_->is_reflexive())
8178 flags |= 4;
8179 if (this->key_type_->needs_key_update())
8180 flags |= 8;
8181 if (this->key_type_->hash_might_panic())
8182 flags |= 16;
8183 vals->push_back(Expression::make_integer_ul(flags, uint32_type, bloc));
8184
8185 ++p;
8186 go_assert(p == fields->end());
8187
8188 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
8189 }
8190
8191 // Return the bucket type to use for a map type. This must correspond
8192 // to libgo/go/runtime/hashmap.go.
8193
8194 Type*
8195 Map_type::bucket_type(Gogo* gogo, int64_t keysize, int64_t valsize)
8196 {
8197 if (this->bucket_type_ != NULL)
8198 return this->bucket_type_;
8199
8200 Type* key_type = this->key_type_;
8201 if (keysize > Map_type::max_key_size)
8202 key_type = Type::make_pointer_type(key_type);
8203
8204 Type* val_type = this->val_type_;
8205 if (valsize > Map_type::max_val_size)
8206 val_type = Type::make_pointer_type(val_type);
8207
8208 Expression* bucket_size = Expression::make_integer_ul(Map_type::bucket_size,
8209 NULL, this->location_);
8210
8211 Type* uint8_type = Type::lookup_integer_type("uint8");
8212 Array_type* topbits_type = Type::make_array_type(uint8_type, bucket_size);
8213 topbits_type->set_is_array_incomparable();
8214 Array_type* keys_type = Type::make_array_type(key_type, bucket_size);
8215 keys_type->set_is_array_incomparable();
8216 Array_type* values_type = Type::make_array_type(val_type, bucket_size);
8217 values_type->set_is_array_incomparable();
8218
8219 // If keys and values have no pointers, the map implementation can
8220 // keep a list of overflow pointers on the side so that buckets can
8221 // be marked as having no pointers. Arrange for the bucket to have
8222 // no pointers by changing the type of the overflow field to uintptr
8223 // in this case. See comment on the hmap.overflow field in
8224 // libgo/go/runtime/hashmap.go.
8225 Type* overflow_type;
8226 if (!key_type->has_pointer() && !val_type->has_pointer())
8227 overflow_type = Type::lookup_integer_type("uintptr");
8228 else
8229 {
8230 // This should really be a pointer to the bucket type itself,
8231 // but that would require us to construct a Named_type for it to
8232 // give it a way to refer to itself. Since nothing really cares
8233 // (except perhaps for someone using a debugger) just use an
8234 // unsafe pointer.
8235 overflow_type = Type::make_pointer_type(Type::make_void_type());
8236 }
8237
8238 // Make sure the overflow pointer is the last memory in the struct,
8239 // because the runtime assumes it can use size-ptrSize as the offset
8240 // of the overflow pointer. We double-check that property below
8241 // once the offsets and size are computed.
8242
8243 int64_t topbits_field_size, topbits_field_align;
8244 int64_t keys_field_size, keys_field_align;
8245 int64_t values_field_size, values_field_align;
8246 int64_t overflow_field_size, overflow_field_align;
8247 if (!topbits_type->backend_type_size(gogo, &topbits_field_size)
8248 || !topbits_type->backend_type_field_align(gogo, &topbits_field_align)
8249 || !keys_type->backend_type_size(gogo, &keys_field_size)
8250 || !keys_type->backend_type_field_align(gogo, &keys_field_align)
8251 || !values_type->backend_type_size(gogo, &values_field_size)
8252 || !values_type->backend_type_field_align(gogo, &values_field_align)
8253 || !overflow_type->backend_type_size(gogo, &overflow_field_size)
8254 || !overflow_type->backend_type_field_align(gogo, &overflow_field_align))
8255 {
8256 go_assert(saw_errors());
8257 return NULL;
8258 }
8259
8260 Struct_type* ret;
8261 int64_t max_align = std::max(std::max(topbits_field_align, keys_field_align),
8262 values_field_align);
8263 if (max_align <= overflow_field_align)
8264 ret = make_builtin_struct_type(4,
8265 "topbits", topbits_type,
8266 "keys", keys_type,
8267 "values", values_type,
8268 "overflow", overflow_type);
8269 else
8270 {
8271 size_t off = topbits_field_size;
8272 off = ((off + keys_field_align - 1)
8273 &~ static_cast<size_t>(keys_field_align - 1));
8274 off += keys_field_size;
8275 off = ((off + values_field_align - 1)
8276 &~ static_cast<size_t>(values_field_align - 1));
8277 off += values_field_size;
8278
8279 int64_t padded_overflow_field_size =
8280 ((overflow_field_size + max_align - 1)
8281 &~ static_cast<size_t>(max_align - 1));
8282
8283 size_t ovoff = off;
8284 ovoff = ((ovoff + max_align - 1)
8285 &~ static_cast<size_t>(max_align - 1));
8286 size_t pad = (ovoff - off
8287 + padded_overflow_field_size - overflow_field_size);
8288
8289 Expression* pad_expr = Expression::make_integer_ul(pad, NULL,
8290 this->location_);
8291 Array_type* pad_type = Type::make_array_type(uint8_type, pad_expr);
8292 pad_type->set_is_array_incomparable();
8293
8294 ret = make_builtin_struct_type(5,
8295 "topbits", topbits_type,
8296 "keys", keys_type,
8297 "values", values_type,
8298 "pad", pad_type,
8299 "overflow", overflow_type);
8300 }
8301
8302 // Verify that the overflow field is just before the end of the
8303 // bucket type.
8304
8305 Btype* btype = ret->get_backend(gogo);
8306 int64_t offset = gogo->backend()->type_field_offset(btype,
8307 ret->field_count() - 1);
8308 int64_t size;
8309 if (!ret->backend_type_size(gogo, &size))
8310 {
8311 go_assert(saw_errors());
8312 return NULL;
8313 }
8314
8315 int64_t ptr_size;
8316 if (!Type::make_pointer_type(uint8_type)->backend_type_size(gogo, &ptr_size))
8317 {
8318 go_assert(saw_errors());
8319 return NULL;
8320 }
8321
8322 go_assert(offset + ptr_size == size);
8323
8324 ret->set_is_struct_incomparable();
8325
8326 this->bucket_type_ = ret;
8327 return ret;
8328 }
8329
8330 // Return the hashmap type for a map type.
8331
8332 Type*
8333 Map_type::hmap_type(Type* bucket_type)
8334 {
8335 if (this->hmap_type_ != NULL)
8336 return this->hmap_type_;
8337
8338 Type* int_type = Type::lookup_integer_type("int");
8339 Type* uint8_type = Type::lookup_integer_type("uint8");
8340 Type* uint16_type = Type::lookup_integer_type("uint16");
8341 Type* uint32_type = Type::lookup_integer_type("uint32");
8342 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8343 Type* void_ptr_type = Type::make_pointer_type(Type::make_void_type());
8344
8345 Type* ptr_bucket_type = Type::make_pointer_type(bucket_type);
8346
8347 Struct_type* ret = make_builtin_struct_type(9,
8348 "count", int_type,
8349 "flags", uint8_type,
8350 "B", uint8_type,
8351 "noverflow", uint16_type,
8352 "hash0", uint32_type,
8353 "buckets", ptr_bucket_type,
8354 "oldbuckets", ptr_bucket_type,
8355 "nevacuate", uintptr_type,
8356 "extra", void_ptr_type);
8357 ret->set_is_struct_incomparable();
8358 this->hmap_type_ = ret;
8359 return ret;
8360 }
8361
8362 // Return the iterator type for a map type. This is the type of the
8363 // value used when doing a range over a map.
8364
8365 Type*
8366 Map_type::hiter_type(Gogo* gogo)
8367 {
8368 if (this->hiter_type_ != NULL)
8369 return this->hiter_type_;
8370
8371 int64_t keysize, valsize;
8372 if (!this->key_type_->backend_type_size(gogo, &keysize)
8373 || !this->val_type_->backend_type_size(gogo, &valsize))
8374 {
8375 go_assert(saw_errors());
8376 return NULL;
8377 }
8378
8379 Type* key_ptr_type = Type::make_pointer_type(this->key_type_);
8380 Type* val_ptr_type = Type::make_pointer_type(this->val_type_);
8381 Type* uint8_type = Type::lookup_integer_type("uint8");
8382 Type* uint8_ptr_type = Type::make_pointer_type(uint8_type);
8383 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8384 Type* bucket_type = this->bucket_type(gogo, keysize, valsize);
8385 Type* bucket_ptr_type = Type::make_pointer_type(bucket_type);
8386 Type* hmap_type = this->hmap_type(bucket_type);
8387 Type* hmap_ptr_type = Type::make_pointer_type(hmap_type);
8388 Type* void_ptr_type = Type::make_pointer_type(Type::make_void_type());
8389 Type* bool_type = Type::lookup_bool_type();
8390
8391 Struct_type* ret = make_builtin_struct_type(15,
8392 "key", key_ptr_type,
8393 "val", val_ptr_type,
8394 "t", uint8_ptr_type,
8395 "h", hmap_ptr_type,
8396 "buckets", bucket_ptr_type,
8397 "bptr", bucket_ptr_type,
8398 "overflow", void_ptr_type,
8399 "oldoverflow", void_ptr_type,
8400 "startBucket", uintptr_type,
8401 "offset", uint8_type,
8402 "wrapped", bool_type,
8403 "B", uint8_type,
8404 "i", uint8_type,
8405 "bucket", uintptr_type,
8406 "checkBucket", uintptr_type);
8407 ret->set_is_struct_incomparable();
8408 this->hiter_type_ = ret;
8409 return ret;
8410 }
8411
8412 // Reflection string for a map.
8413
8414 void
8415 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
8416 {
8417 ret->append("map[");
8418 this->append_reflection(this->key_type_, gogo, ret);
8419 ret->append("]");
8420 this->append_reflection(this->val_type_, gogo, ret);
8421 }
8422
8423 // Export a map type.
8424
8425 void
8426 Map_type::do_export(Export* exp) const
8427 {
8428 exp->write_c_string("map [");
8429 exp->write_type(this->key_type_);
8430 exp->write_c_string("] ");
8431 exp->write_type(this->val_type_);
8432 }
8433
8434 // Import a map type.
8435
8436 Map_type*
8437 Map_type::do_import(Import* imp)
8438 {
8439 imp->require_c_string("map [");
8440 Type* key_type = imp->read_type();
8441 imp->require_c_string("] ");
8442 Type* val_type = imp->read_type();
8443 return Type::make_map_type(key_type, val_type, imp->location());
8444 }
8445
8446 // Make a map type.
8447
8448 Map_type*
8449 Type::make_map_type(Type* key_type, Type* val_type, Location location)
8450 {
8451 return new Map_type(key_type, val_type, location);
8452 }
8453
8454 // Class Channel_type.
8455
8456 // Verify.
8457
8458 bool
8459 Channel_type::do_verify()
8460 {
8461 // We have no location for this error, but this is not something the
8462 // ordinary user will see.
8463 if (!this->element_type_->in_heap())
8464 go_error_at(Linemap::unknown_location(),
8465 "chan of go:notinheap type not allowed");
8466 return true;
8467 }
8468
8469 // Hash code.
8470
8471 unsigned int
8472 Channel_type::do_hash_for_method(Gogo* gogo, int flags) const
8473 {
8474 unsigned int ret = 0;
8475 if (this->may_send_)
8476 ret += 1;
8477 if (this->may_receive_)
8478 ret += 2;
8479 if (this->element_type_ != NULL)
8480 ret += this->element_type_->hash_for_method(gogo, flags) << 2;
8481 return ret << 3;
8482 }
8483
8484 // Whether this type is the same as T.
8485
8486 bool
8487 Channel_type::is_identical(const Channel_type* t, int flags) const
8488 {
8489 if (!Type::are_identical(this->element_type(), t->element_type(), flags,
8490 NULL))
8491 return false;
8492 return (this->may_send_ == t->may_send_
8493 && this->may_receive_ == t->may_receive_);
8494 }
8495
8496 // Return the backend representation for a channel type. A channel is a pointer
8497 // to a __go_channel struct. The __go_channel struct is defined in
8498 // libgo/runtime/channel.h.
8499
8500 Btype*
8501 Channel_type::do_get_backend(Gogo* gogo)
8502 {
8503 static Btype* backend_channel_type;
8504 if (backend_channel_type == NULL)
8505 {
8506 std::vector<Backend::Btyped_identifier> bfields;
8507 Btype* bt = gogo->backend()->struct_type(bfields);
8508 bt = gogo->backend()->named_type("__go_channel", bt,
8509 Linemap::predeclared_location());
8510 backend_channel_type = gogo->backend()->pointer_type(bt);
8511 }
8512 return backend_channel_type;
8513 }
8514
8515 // Build a type descriptor for a channel type.
8516
8517 Type*
8518 Channel_type::make_chan_type_descriptor_type()
8519 {
8520 static Type* ret;
8521 if (ret == NULL)
8522 {
8523 Type* tdt = Type::make_type_descriptor_type();
8524 Type* ptdt = Type::make_type_descriptor_ptr_type();
8525
8526 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8527
8528 Struct_type* sf =
8529 Type::make_builtin_struct_type(3,
8530 "", tdt,
8531 "elem", ptdt,
8532 "dir", uintptr_type);
8533
8534 ret = Type::make_builtin_named_type("ChanType", sf);
8535 }
8536
8537 return ret;
8538 }
8539
8540 // Build a type descriptor for a map type.
8541
8542 Expression*
8543 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8544 {
8545 Location bloc = Linemap::predeclared_location();
8546
8547 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
8548
8549 const Struct_field_list* fields = ctdt->struct_type()->fields();
8550
8551 Expression_list* vals = new Expression_list();
8552 vals->reserve(3);
8553
8554 Struct_field_list::const_iterator p = fields->begin();
8555 go_assert(p->is_field_name("_type"));
8556 vals->push_back(this->type_descriptor_constructor(gogo,
8557 RUNTIME_TYPE_KIND_CHAN,
8558 name, NULL, true));
8559
8560 ++p;
8561 go_assert(p->is_field_name("elem"));
8562 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
8563
8564 ++p;
8565 go_assert(p->is_field_name("dir"));
8566 // These bits must match the ones in libgo/runtime/go-type.h.
8567 int val = 0;
8568 if (this->may_receive_)
8569 val |= 1;
8570 if (this->may_send_)
8571 val |= 2;
8572 vals->push_back(Expression::make_integer_ul(val, p->type(), bloc));
8573
8574 ++p;
8575 go_assert(p == fields->end());
8576
8577 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
8578 }
8579
8580 // Reflection string.
8581
8582 void
8583 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
8584 {
8585 if (!this->may_send_)
8586 ret->append("<-");
8587 ret->append("chan");
8588 if (!this->may_receive_)
8589 ret->append("<-");
8590 ret->push_back(' ');
8591 this->append_reflection(this->element_type_, gogo, ret);
8592 }
8593
8594 // Export.
8595
8596 void
8597 Channel_type::do_export(Export* exp) const
8598 {
8599 exp->write_c_string("chan ");
8600 if (this->may_send_ && !this->may_receive_)
8601 exp->write_c_string("-< ");
8602 else if (this->may_receive_ && !this->may_send_)
8603 exp->write_c_string("<- ");
8604 exp->write_type(this->element_type_);
8605 }
8606
8607 // Import.
8608
8609 Channel_type*
8610 Channel_type::do_import(Import* imp)
8611 {
8612 imp->require_c_string("chan ");
8613
8614 bool may_send;
8615 bool may_receive;
8616 if (imp->match_c_string("-< "))
8617 {
8618 imp->advance(3);
8619 may_send = true;
8620 may_receive = false;
8621 }
8622 else if (imp->match_c_string("<- "))
8623 {
8624 imp->advance(3);
8625 may_receive = true;
8626 may_send = false;
8627 }
8628 else
8629 {
8630 may_send = true;
8631 may_receive = true;
8632 }
8633
8634 Type* element_type = imp->read_type();
8635
8636 return Type::make_channel_type(may_send, may_receive, element_type);
8637 }
8638
8639 // Return the type that the runtime package uses for one case of a
8640 // select statement. An array of values of this type is allocated on
8641 // the stack. This must match scase in libgo/go/runtime/select.go.
8642
8643 Type*
8644 Channel_type::select_case_type()
8645 {
8646 static Struct_type* scase_type;
8647 if (scase_type == NULL)
8648 {
8649 Type* unsafe_pointer_type =
8650 Type::make_pointer_type(Type::make_void_type());
8651 Type* uint16_type = Type::lookup_integer_type("uint16");
8652 Type* int64_type = Type::lookup_integer_type("int64");
8653 scase_type =
8654 Type::make_builtin_struct_type(4,
8655 "c", unsafe_pointer_type,
8656 "elem", unsafe_pointer_type,
8657 "kind", uint16_type,
8658 "releasetime", int64_type);
8659 scase_type->set_is_struct_incomparable();
8660 }
8661 return scase_type;
8662 }
8663
8664 // Make a new channel type.
8665
8666 Channel_type*
8667 Type::make_channel_type(bool send, bool receive, Type* element_type)
8668 {
8669 return new Channel_type(send, receive, element_type);
8670 }
8671
8672 // Class Interface_type.
8673
8674 // Return the list of methods.
8675
8676 const Typed_identifier_list*
8677 Interface_type::methods() const
8678 {
8679 go_assert(this->methods_are_finalized_ || saw_errors());
8680 return this->all_methods_;
8681 }
8682
8683 // Return the number of methods.
8684
8685 size_t
8686 Interface_type::method_count() const
8687 {
8688 go_assert(this->methods_are_finalized_ || saw_errors());
8689 return this->all_methods_ == NULL ? 0 : this->all_methods_->size();
8690 }
8691
8692 // Traversal.
8693
8694 int
8695 Interface_type::do_traverse(Traverse* traverse)
8696 {
8697 Typed_identifier_list* methods = (this->methods_are_finalized_
8698 ? this->all_methods_
8699 : this->parse_methods_);
8700 if (methods == NULL)
8701 return TRAVERSE_CONTINUE;
8702 return methods->traverse(traverse);
8703 }
8704
8705 // Finalize the methods. This handles interface inheritance.
8706
8707 void
8708 Interface_type::finalize_methods()
8709 {
8710 if (this->methods_are_finalized_)
8711 return;
8712 this->methods_are_finalized_ = true;
8713 if (this->parse_methods_ == NULL)
8714 return;
8715
8716 this->all_methods_ = new Typed_identifier_list();
8717 this->all_methods_->reserve(this->parse_methods_->size());
8718 Typed_identifier_list inherit;
8719 for (Typed_identifier_list::const_iterator pm =
8720 this->parse_methods_->begin();
8721 pm != this->parse_methods_->end();
8722 ++pm)
8723 {
8724 const Typed_identifier* p = &*pm;
8725 if (p->name().empty())
8726 inherit.push_back(*p);
8727 else if (this->find_method(p->name()) == NULL)
8728 this->all_methods_->push_back(*p);
8729 else
8730 go_error_at(p->location(), "duplicate method %qs",
8731 Gogo::message_name(p->name()).c_str());
8732 }
8733
8734 std::vector<Named_type*> seen;
8735 seen.reserve(inherit.size());
8736 bool issued_recursive_error = false;
8737 while (!inherit.empty())
8738 {
8739 Type* t = inherit.back().type();
8740 Location tl = inherit.back().location();
8741 inherit.pop_back();
8742
8743 Interface_type* it = t->interface_type();
8744 if (it == NULL)
8745 {
8746 if (!t->is_error())
8747 go_error_at(tl, "interface contains embedded non-interface");
8748 continue;
8749 }
8750 if (it == this)
8751 {
8752 if (!issued_recursive_error)
8753 {
8754 go_error_at(tl, "invalid recursive interface");
8755 issued_recursive_error = true;
8756 }
8757 continue;
8758 }
8759
8760 Named_type* nt = t->named_type();
8761 if (nt != NULL && it->parse_methods_ != NULL)
8762 {
8763 std::vector<Named_type*>::const_iterator q;
8764 for (q = seen.begin(); q != seen.end(); ++q)
8765 {
8766 if (*q == nt)
8767 {
8768 go_error_at(tl, "inherited interface loop");
8769 break;
8770 }
8771 }
8772 if (q != seen.end())
8773 continue;
8774 seen.push_back(nt);
8775 }
8776
8777 const Typed_identifier_list* imethods = it->parse_methods_;
8778 if (imethods == NULL)
8779 continue;
8780 for (Typed_identifier_list::const_iterator q = imethods->begin();
8781 q != imethods->end();
8782 ++q)
8783 {
8784 if (q->name().empty())
8785 inherit.push_back(*q);
8786 else if (this->find_method(q->name()) == NULL)
8787 this->all_methods_->push_back(Typed_identifier(q->name(),
8788 q->type(), tl));
8789 else
8790 go_error_at(tl, "inherited method %qs is ambiguous",
8791 Gogo::message_name(q->name()).c_str());
8792 }
8793 }
8794
8795 if (!this->all_methods_->empty())
8796 this->all_methods_->sort_by_name();
8797 else
8798 {
8799 delete this->all_methods_;
8800 this->all_methods_ = NULL;
8801 }
8802 }
8803
8804 // Return the method NAME, or NULL.
8805
8806 const Typed_identifier*
8807 Interface_type::find_method(const std::string& name) const
8808 {
8809 go_assert(this->methods_are_finalized_);
8810 if (this->all_methods_ == NULL)
8811 return NULL;
8812 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8813 p != this->all_methods_->end();
8814 ++p)
8815 if (p->name() == name)
8816 return &*p;
8817 return NULL;
8818 }
8819
8820 // Return the method index.
8821
8822 size_t
8823 Interface_type::method_index(const std::string& name) const
8824 {
8825 go_assert(this->methods_are_finalized_ && this->all_methods_ != NULL);
8826 size_t ret = 0;
8827 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8828 p != this->all_methods_->end();
8829 ++p, ++ret)
8830 if (p->name() == name)
8831 return ret;
8832 go_unreachable();
8833 }
8834
8835 // Return whether NAME is an unexported method, for better error
8836 // reporting.
8837
8838 bool
8839 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
8840 {
8841 go_assert(this->methods_are_finalized_);
8842 if (this->all_methods_ == NULL)
8843 return false;
8844 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8845 p != this->all_methods_->end();
8846 ++p)
8847 {
8848 const std::string& method_name(p->name());
8849 if (Gogo::is_hidden_name(method_name)
8850 && name == Gogo::unpack_hidden_name(method_name)
8851 && gogo->pack_hidden_name(name, false) != method_name)
8852 return true;
8853 }
8854 return false;
8855 }
8856
8857 // Whether this type is identical with T.
8858
8859 bool
8860 Interface_type::is_identical(const Interface_type* t, int flags) const
8861 {
8862 // If methods have not been finalized, then we are asking whether
8863 // func redeclarations are the same. This is an error, so for
8864 // simplicity we say they are never the same.
8865 if (!this->methods_are_finalized_ || !t->methods_are_finalized_)
8866 return false;
8867
8868 // Consult a flag to see whether we need to compare based on
8869 // parse methods or all methods.
8870 Typed_identifier_list* methods = (((flags & COMPARE_EMBEDDED_INTERFACES) != 0)
8871 ? this->parse_methods_
8872 : this->all_methods_);
8873 Typed_identifier_list* tmethods = (((flags & COMPARE_EMBEDDED_INTERFACES) != 0)
8874 ? t->parse_methods_
8875 : t->all_methods_);
8876
8877 // We require the same methods with the same types. The methods
8878 // have already been sorted.
8879 if (methods == NULL || tmethods == NULL)
8880 return methods == tmethods;
8881
8882 if (this->assume_identical(this, t) || t->assume_identical(t, this))
8883 return true;
8884
8885 Assume_identical* hold_ai = this->assume_identical_;
8886 Assume_identical ai;
8887 ai.t1 = this;
8888 ai.t2 = t;
8889 ai.next = hold_ai;
8890 this->assume_identical_ = &ai;
8891
8892 Typed_identifier_list::const_iterator p1 = methods->begin();
8893 Typed_identifier_list::const_iterator p2;
8894 for (p2 = tmethods->begin(); p2 != tmethods->end(); ++p1, ++p2)
8895 {
8896 if (p1 == methods->end())
8897 break;
8898 if (p1->name() != p2->name()
8899 || !Type::are_identical(p1->type(), p2->type(), flags, NULL))
8900 break;
8901 }
8902
8903 this->assume_identical_ = hold_ai;
8904
8905 return p1 == methods->end() && p2 == tmethods->end();
8906 }
8907
8908 // Return true if T1 and T2 are assumed to be identical during a type
8909 // comparison.
8910
8911 bool
8912 Interface_type::assume_identical(const Interface_type* t1,
8913 const Interface_type* t2) const
8914 {
8915 for (Assume_identical* p = this->assume_identical_;
8916 p != NULL;
8917 p = p->next)
8918 if ((p->t1 == t1 && p->t2 == t2) || (p->t1 == t2 && p->t2 == t1))
8919 return true;
8920 return false;
8921 }
8922
8923 // Whether we can assign the interface type T to this type. The types
8924 // are known to not be identical. An interface assignment is only
8925 // permitted if T is known to implement all methods in THIS.
8926 // Otherwise a type guard is required.
8927
8928 bool
8929 Interface_type::is_compatible_for_assign(const Interface_type* t,
8930 std::string* reason) const
8931 {
8932 go_assert(this->methods_are_finalized_ && t->methods_are_finalized_);
8933 if (this->all_methods_ == NULL)
8934 return true;
8935 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8936 p != this->all_methods_->end();
8937 ++p)
8938 {
8939 const Typed_identifier* m = t->find_method(p->name());
8940 if (m == NULL)
8941 {
8942 if (reason != NULL)
8943 {
8944 char buf[200];
8945 snprintf(buf, sizeof buf,
8946 _("need explicit conversion; missing method %s%s%s"),
8947 go_open_quote(), Gogo::message_name(p->name()).c_str(),
8948 go_close_quote());
8949 reason->assign(buf);
8950 }
8951 return false;
8952 }
8953
8954 std::string subreason;
8955 if (!Type::are_identical(p->type(), m->type(), Type::COMPARE_TAGS,
8956 &subreason))
8957 {
8958 if (reason != NULL)
8959 {
8960 std::string n = Gogo::message_name(p->name());
8961 size_t len = 100 + n.length() + subreason.length();
8962 char* buf = new char[len];
8963 if (subreason.empty())
8964 snprintf(buf, len, _("incompatible type for method %s%s%s"),
8965 go_open_quote(), n.c_str(), go_close_quote());
8966 else
8967 snprintf(buf, len,
8968 _("incompatible type for method %s%s%s (%s)"),
8969 go_open_quote(), n.c_str(), go_close_quote(),
8970 subreason.c_str());
8971 reason->assign(buf);
8972 delete[] buf;
8973 }
8974 return false;
8975 }
8976 }
8977
8978 return true;
8979 }
8980
8981 // Hash code.
8982
8983 unsigned int
8984 Interface_type::do_hash_for_method(Gogo*, int) const
8985 {
8986 go_assert(this->methods_are_finalized_);
8987 unsigned int ret = 0;
8988 if (this->all_methods_ != NULL)
8989 {
8990 for (Typed_identifier_list::const_iterator p =
8991 this->all_methods_->begin();
8992 p != this->all_methods_->end();
8993 ++p)
8994 {
8995 ret = Gogo::hash_string(p->name(), ret);
8996 // We don't use the method type in the hash, to avoid
8997 // infinite recursion if an interface method uses a type
8998 // which is an interface which inherits from the interface
8999 // itself.
9000 // type T interface { F() interface {T}}
9001 ret <<= 1;
9002 }
9003 }
9004 return ret;
9005 }
9006
9007 // Return true if T implements the interface. If it does not, and
9008 // REASON is not NULL, set *REASON to a useful error message.
9009
9010 bool
9011 Interface_type::implements_interface(const Type* t, std::string* reason) const
9012 {
9013 go_assert(this->methods_are_finalized_);
9014 if (this->all_methods_ == NULL)
9015 return true;
9016
9017 bool is_pointer = false;
9018 const Named_type* nt = t->named_type();
9019 const Struct_type* st = t->struct_type();
9020 // If we start with a named type, we don't dereference it to find
9021 // methods.
9022 if (nt == NULL)
9023 {
9024 const Type* pt = t->points_to();
9025 if (pt != NULL)
9026 {
9027 // If T is a pointer to a named type, then we need to look at
9028 // the type to which it points.
9029 is_pointer = true;
9030 nt = pt->named_type();
9031 st = pt->struct_type();
9032 }
9033 }
9034
9035 // If we have a named type, get the methods from it rather than from
9036 // any struct type.
9037 if (nt != NULL)
9038 st = NULL;
9039
9040 // Only named and struct types have methods.
9041 if (nt == NULL && st == NULL)
9042 {
9043 if (reason != NULL)
9044 {
9045 if (t->points_to() != NULL
9046 && t->points_to()->interface_type() != NULL)
9047 reason->assign(_("pointer to interface type has no methods"));
9048 else
9049 reason->assign(_("type has no methods"));
9050 }
9051 return false;
9052 }
9053
9054 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
9055 {
9056 if (reason != NULL)
9057 {
9058 if (t->points_to() != NULL
9059 && t->points_to()->interface_type() != NULL)
9060 reason->assign(_("pointer to interface type has no methods"));
9061 else
9062 reason->assign(_("type has no methods"));
9063 }
9064 return false;
9065 }
9066
9067 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9068 p != this->all_methods_->end();
9069 ++p)
9070 {
9071 bool is_ambiguous = false;
9072 Method* m = (nt != NULL
9073 ? nt->method_function(p->name(), &is_ambiguous)
9074 : st->method_function(p->name(), &is_ambiguous));
9075 if (m == NULL)
9076 {
9077 if (reason != NULL)
9078 {
9079 std::string n = Gogo::message_name(p->name());
9080 size_t len = n.length() + 100;
9081 char* buf = new char[len];
9082 if (is_ambiguous)
9083 snprintf(buf, len, _("ambiguous method %s%s%s"),
9084 go_open_quote(), n.c_str(), go_close_quote());
9085 else
9086 snprintf(buf, len, _("missing method %s%s%s"),
9087 go_open_quote(), n.c_str(), go_close_quote());
9088 reason->assign(buf);
9089 delete[] buf;
9090 }
9091 return false;
9092 }
9093
9094 Function_type *p_fn_type = p->type()->function_type();
9095 Function_type* m_fn_type = m->type()->function_type();
9096 go_assert(p_fn_type != NULL && m_fn_type != NULL);
9097 std::string subreason;
9098 if (!p_fn_type->is_identical(m_fn_type, true, Type::COMPARE_TAGS,
9099 &subreason))
9100 {
9101 if (reason != NULL)
9102 {
9103 std::string n = Gogo::message_name(p->name());
9104 size_t len = 100 + n.length() + subreason.length();
9105 char* buf = new char[len];
9106 if (subreason.empty())
9107 snprintf(buf, len, _("incompatible type for method %s%s%s"),
9108 go_open_quote(), n.c_str(), go_close_quote());
9109 else
9110 snprintf(buf, len,
9111 _("incompatible type for method %s%s%s (%s)"),
9112 go_open_quote(), n.c_str(), go_close_quote(),
9113 subreason.c_str());
9114 reason->assign(buf);
9115 delete[] buf;
9116 }
9117 return false;
9118 }
9119
9120 if (!is_pointer && !m->is_value_method())
9121 {
9122 if (reason != NULL)
9123 {
9124 std::string n = Gogo::message_name(p->name());
9125 size_t len = 100 + n.length();
9126 char* buf = new char[len];
9127 snprintf(buf, len,
9128 _("method %s%s%s requires a pointer receiver"),
9129 go_open_quote(), n.c_str(), go_close_quote());
9130 reason->assign(buf);
9131 delete[] buf;
9132 }
9133 return false;
9134 }
9135
9136 // If the magic //go:nointerface comment was used, the method
9137 // may not be used to implement interfaces.
9138 if (m->nointerface())
9139 {
9140 if (reason != NULL)
9141 {
9142 std::string n = Gogo::message_name(p->name());
9143 size_t len = 100 + n.length();
9144 char* buf = new char[len];
9145 snprintf(buf, len,
9146 _("method %s%s%s is marked go:nointerface"),
9147 go_open_quote(), n.c_str(), go_close_quote());
9148 reason->assign(buf);
9149 delete[] buf;
9150 }
9151 return false;
9152 }
9153 }
9154
9155 return true;
9156 }
9157
9158 // Return the backend representation of the empty interface type. We
9159 // use the same struct for all empty interfaces.
9160
9161 Btype*
9162 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
9163 {
9164 static Btype* empty_interface_type;
9165 if (empty_interface_type == NULL)
9166 {
9167 std::vector<Backend::Btyped_identifier> bfields(2);
9168
9169 Location bloc = Linemap::predeclared_location();
9170
9171 Type* pdt = Type::make_type_descriptor_ptr_type();
9172 bfields[0].name = "__type_descriptor";
9173 bfields[0].btype = pdt->get_backend(gogo);
9174 bfields[0].location = bloc;
9175
9176 Type* vt = Type::make_pointer_type(Type::make_void_type());
9177 bfields[1].name = "__object";
9178 bfields[1].btype = vt->get_backend(gogo);
9179 bfields[1].location = bloc;
9180
9181 empty_interface_type = gogo->backend()->struct_type(bfields);
9182 }
9183 return empty_interface_type;
9184 }
9185
9186 Interface_type::Bmethods_map Interface_type::bmethods_map;
9187
9188 // Return a pointer to the backend representation of the method table.
9189
9190 Btype*
9191 Interface_type::get_backend_methods(Gogo* gogo)
9192 {
9193 if (this->bmethods_ != NULL && !this->bmethods_is_placeholder_)
9194 return this->bmethods_;
9195
9196 std::pair<Interface_type*, Bmethods_map_entry> val;
9197 val.first = this;
9198 val.second.btype = NULL;
9199 val.second.is_placeholder = false;
9200 std::pair<Bmethods_map::iterator, bool> ins =
9201 Interface_type::bmethods_map.insert(val);
9202 if (!ins.second
9203 && ins.first->second.btype != NULL
9204 && !ins.first->second.is_placeholder)
9205 {
9206 this->bmethods_ = ins.first->second.btype;
9207 this->bmethods_is_placeholder_ = false;
9208 return this->bmethods_;
9209 }
9210
9211 Location loc = this->location();
9212
9213 std::vector<Backend::Btyped_identifier>
9214 mfields(this->all_methods_->size() + 1);
9215
9216 Type* pdt = Type::make_type_descriptor_ptr_type();
9217 mfields[0].name = "__type_descriptor";
9218 mfields[0].btype = pdt->get_backend(gogo);
9219 mfields[0].location = loc;
9220
9221 std::string last_name = "";
9222 size_t i = 1;
9223 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9224 p != this->all_methods_->end();
9225 ++p, ++i)
9226 {
9227 // The type of the method in Go only includes the parameters.
9228 // The actual method also has a receiver, which is always a
9229 // pointer. We need to add that pointer type here in order to
9230 // generate the correct type for the backend.
9231 Function_type* ft = p->type()->function_type();
9232 go_assert(ft->receiver() == NULL);
9233
9234 const Typed_identifier_list* params = ft->parameters();
9235 Typed_identifier_list* mparams = new Typed_identifier_list();
9236 if (params != NULL)
9237 mparams->reserve(params->size() + 1);
9238 Type* vt = Type::make_pointer_type(Type::make_void_type());
9239 mparams->push_back(Typed_identifier("", vt, ft->location()));
9240 if (params != NULL)
9241 {
9242 for (Typed_identifier_list::const_iterator pp = params->begin();
9243 pp != params->end();
9244 ++pp)
9245 mparams->push_back(*pp);
9246 }
9247
9248 Typed_identifier_list* mresults = (ft->results() == NULL
9249 ? NULL
9250 : ft->results()->copy());
9251 Function_type* mft = Type::make_function_type(NULL, mparams, mresults,
9252 ft->location());
9253
9254 mfields[i].name = Gogo::unpack_hidden_name(p->name());
9255 mfields[i].btype = mft->get_backend_fntype(gogo);
9256 mfields[i].location = loc;
9257
9258 // Sanity check: the names should be sorted.
9259 go_assert(Gogo::unpack_hidden_name(p->name())
9260 > Gogo::unpack_hidden_name(last_name));
9261 last_name = p->name();
9262 }
9263
9264 Btype* st = gogo->backend()->struct_type(mfields);
9265 Btype* ret = gogo->backend()->pointer_type(st);
9266
9267 if (ins.first->second.btype != NULL
9268 && ins.first->second.is_placeholder)
9269 gogo->backend()->set_placeholder_pointer_type(ins.first->second.btype,
9270 ret);
9271 this->bmethods_ = ret;
9272 ins.first->second.btype = ret;
9273 this->bmethods_is_placeholder_ = false;
9274 ins.first->second.is_placeholder = false;
9275 return ret;
9276 }
9277
9278 // Return a placeholder for the pointer to the backend methods table.
9279
9280 Btype*
9281 Interface_type::get_backend_methods_placeholder(Gogo* gogo)
9282 {
9283 if (this->bmethods_ == NULL)
9284 {
9285 std::pair<Interface_type*, Bmethods_map_entry> val;
9286 val.first = this;
9287 val.second.btype = NULL;
9288 val.second.is_placeholder = false;
9289 std::pair<Bmethods_map::iterator, bool> ins =
9290 Interface_type::bmethods_map.insert(val);
9291 if (!ins.second && ins.first->second.btype != NULL)
9292 {
9293 this->bmethods_ = ins.first->second.btype;
9294 this->bmethods_is_placeholder_ = ins.first->second.is_placeholder;
9295 return this->bmethods_;
9296 }
9297
9298 Location loc = this->location();
9299 Btype* bt = gogo->backend()->placeholder_pointer_type("", loc, false);
9300 this->bmethods_ = bt;
9301 ins.first->second.btype = bt;
9302 this->bmethods_is_placeholder_ = true;
9303 ins.first->second.is_placeholder = true;
9304 }
9305 return this->bmethods_;
9306 }
9307
9308 // Return the fields of a non-empty interface type. This is not
9309 // declared in types.h so that types.h doesn't have to #include
9310 // backend.h.
9311
9312 static void
9313 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
9314 bool use_placeholder,
9315 std::vector<Backend::Btyped_identifier>* bfields)
9316 {
9317 Location loc = type->location();
9318
9319 bfields->resize(2);
9320
9321 (*bfields)[0].name = "__methods";
9322 (*bfields)[0].btype = (use_placeholder
9323 ? type->get_backend_methods_placeholder(gogo)
9324 : type->get_backend_methods(gogo));
9325 (*bfields)[0].location = loc;
9326
9327 Type* vt = Type::make_pointer_type(Type::make_void_type());
9328 (*bfields)[1].name = "__object";
9329 (*bfields)[1].btype = vt->get_backend(gogo);
9330 (*bfields)[1].location = Linemap::predeclared_location();
9331 }
9332
9333 // Return the backend representation for an interface type. An interface is a
9334 // pointer to a struct. The struct has three fields. The first field is a
9335 // pointer to the type descriptor for the dynamic type of the object.
9336 // The second field is a pointer to a table of methods for the
9337 // interface to be used with the object. The third field is the value
9338 // of the object itself.
9339
9340 Btype*
9341 Interface_type::do_get_backend(Gogo* gogo)
9342 {
9343 if (this->is_empty())
9344 return Interface_type::get_backend_empty_interface_type(gogo);
9345 else
9346 {
9347 if (this->interface_btype_ != NULL)
9348 return this->interface_btype_;
9349 this->interface_btype_ =
9350 gogo->backend()->placeholder_struct_type("", this->location_);
9351 std::vector<Backend::Btyped_identifier> bfields;
9352 get_backend_interface_fields(gogo, this, false, &bfields);
9353 if (!gogo->backend()->set_placeholder_struct_type(this->interface_btype_,
9354 bfields))
9355 this->interface_btype_ = gogo->backend()->error_type();
9356 return this->interface_btype_;
9357 }
9358 }
9359
9360 // Finish the backend representation of the methods.
9361
9362 void
9363 Interface_type::finish_backend_methods(Gogo* gogo)
9364 {
9365 if (!this->is_empty())
9366 {
9367 const Typed_identifier_list* methods = this->methods();
9368 if (methods != NULL)
9369 {
9370 for (Typed_identifier_list::const_iterator p = methods->begin();
9371 p != methods->end();
9372 ++p)
9373 p->type()->get_backend(gogo);
9374 }
9375
9376 // Getting the backend methods now will set the placeholder
9377 // pointer.
9378 this->get_backend_methods(gogo);
9379 }
9380 }
9381
9382 // The type of an interface type descriptor.
9383
9384 Type*
9385 Interface_type::make_interface_type_descriptor_type()
9386 {
9387 static Type* ret;
9388 if (ret == NULL)
9389 {
9390 Type* tdt = Type::make_type_descriptor_type();
9391 Type* ptdt = Type::make_type_descriptor_ptr_type();
9392
9393 Type* string_type = Type::lookup_string_type();
9394 Type* pointer_string_type = Type::make_pointer_type(string_type);
9395
9396 Struct_type* sm =
9397 Type::make_builtin_struct_type(3,
9398 "name", pointer_string_type,
9399 "pkgPath", pointer_string_type,
9400 "typ", ptdt);
9401
9402 Type* nsm = Type::make_builtin_named_type("imethod", sm);
9403
9404 Type* slice_nsm = Type::make_array_type(nsm, NULL);
9405
9406 Struct_type* s = Type::make_builtin_struct_type(2,
9407 "", tdt,
9408 "methods", slice_nsm);
9409
9410 ret = Type::make_builtin_named_type("InterfaceType", s);
9411 }
9412
9413 return ret;
9414 }
9415
9416 // Build a type descriptor for an interface type.
9417
9418 Expression*
9419 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9420 {
9421 Location bloc = Linemap::predeclared_location();
9422
9423 Type* itdt = Interface_type::make_interface_type_descriptor_type();
9424
9425 const Struct_field_list* ifields = itdt->struct_type()->fields();
9426
9427 Expression_list* ivals = new Expression_list();
9428 ivals->reserve(2);
9429
9430 Struct_field_list::const_iterator pif = ifields->begin();
9431 go_assert(pif->is_field_name("_type"));
9432 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
9433 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
9434 true));
9435
9436 ++pif;
9437 go_assert(pif->is_field_name("methods"));
9438
9439 Expression_list* methods = new Expression_list();
9440 if (this->all_methods_ != NULL)
9441 {
9442 Type* elemtype = pif->type()->array_type()->element_type();
9443
9444 methods->reserve(this->all_methods_->size());
9445 for (Typed_identifier_list::const_iterator pm =
9446 this->all_methods_->begin();
9447 pm != this->all_methods_->end();
9448 ++pm)
9449 {
9450 const Struct_field_list* mfields = elemtype->struct_type()->fields();
9451
9452 Expression_list* mvals = new Expression_list();
9453 mvals->reserve(3);
9454
9455 Struct_field_list::const_iterator pmf = mfields->begin();
9456 go_assert(pmf->is_field_name("name"));
9457 std::string s = Gogo::unpack_hidden_name(pm->name());
9458 Expression* e = Expression::make_string(s, bloc);
9459 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
9460
9461 ++pmf;
9462 go_assert(pmf->is_field_name("pkgPath"));
9463 if (!Gogo::is_hidden_name(pm->name()))
9464 mvals->push_back(Expression::make_nil(bloc));
9465 else
9466 {
9467 s = Gogo::hidden_name_pkgpath(pm->name());
9468 e = Expression::make_string(s, bloc);
9469 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
9470 }
9471
9472 ++pmf;
9473 go_assert(pmf->is_field_name("typ"));
9474 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
9475
9476 ++pmf;
9477 go_assert(pmf == mfields->end());
9478
9479 e = Expression::make_struct_composite_literal(elemtype, mvals,
9480 bloc);
9481 methods->push_back(e);
9482 }
9483 }
9484
9485 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
9486 methods, bloc));
9487
9488 ++pif;
9489 go_assert(pif == ifields->end());
9490
9491 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
9492 }
9493
9494 // Reflection string.
9495
9496 void
9497 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
9498 {
9499 ret->append("interface {");
9500 const Typed_identifier_list* methods = this->parse_methods_;
9501 if (methods != NULL)
9502 {
9503 ret->push_back(' ');
9504 for (Typed_identifier_list::const_iterator p = methods->begin();
9505 p != methods->end();
9506 ++p)
9507 {
9508 if (p != methods->begin())
9509 ret->append("; ");
9510 if (p->name().empty())
9511 this->append_reflection(p->type(), gogo, ret);
9512 else
9513 {
9514 if (!Gogo::is_hidden_name(p->name()))
9515 ret->append(p->name());
9516 else if (gogo->pkgpath_from_option())
9517 ret->append(p->name().substr(1));
9518 else
9519 {
9520 // If no -fgo-pkgpath option, backward compatibility
9521 // for how this used to work before -fgo-pkgpath was
9522 // introduced.
9523 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
9524 ret->append(pkgpath.substr(pkgpath.find('.') + 1));
9525 ret->push_back('.');
9526 ret->append(Gogo::unpack_hidden_name(p->name()));
9527 }
9528 std::string sub = p->type()->reflection(gogo);
9529 go_assert(sub.compare(0, 4, "func") == 0);
9530 sub = sub.substr(4);
9531 ret->append(sub);
9532 }
9533 }
9534 ret->push_back(' ');
9535 }
9536 ret->append("}");
9537 }
9538
9539 // Export.
9540
9541 void
9542 Interface_type::do_export(Export* exp) const
9543 {
9544 exp->write_c_string("interface { ");
9545
9546 const Typed_identifier_list* methods = this->parse_methods_;
9547 if (methods != NULL)
9548 {
9549 for (Typed_identifier_list::const_iterator pm = methods->begin();
9550 pm != methods->end();
9551 ++pm)
9552 {
9553 if (pm->name().empty())
9554 {
9555 exp->write_c_string("? ");
9556 exp->write_type(pm->type());
9557 }
9558 else
9559 {
9560 exp->write_string(pm->name());
9561 exp->write_c_string(" (");
9562
9563 const Function_type* fntype = pm->type()->function_type();
9564
9565 bool first = true;
9566 const Typed_identifier_list* parameters = fntype->parameters();
9567 if (parameters != NULL)
9568 {
9569 bool is_varargs = fntype->is_varargs();
9570 for (Typed_identifier_list::const_iterator pp =
9571 parameters->begin();
9572 pp != parameters->end();
9573 ++pp)
9574 {
9575 if (first)
9576 first = false;
9577 else
9578 exp->write_c_string(", ");
9579 exp->write_name(pp->name());
9580 exp->write_c_string(" ");
9581 if (!is_varargs || pp + 1 != parameters->end())
9582 exp->write_type(pp->type());
9583 else
9584 {
9585 exp->write_c_string("...");
9586 Type *pptype = pp->type();
9587 exp->write_type(pptype->array_type()->element_type());
9588 }
9589 }
9590 }
9591
9592 exp->write_c_string(")");
9593
9594 const Typed_identifier_list* results = fntype->results();
9595 if (results != NULL)
9596 {
9597 exp->write_c_string(" ");
9598 if (results->size() == 1 && results->begin()->name().empty())
9599 exp->write_type(results->begin()->type());
9600 else
9601 {
9602 first = true;
9603 exp->write_c_string("(");
9604 for (Typed_identifier_list::const_iterator p =
9605 results->begin();
9606 p != results->end();
9607 ++p)
9608 {
9609 if (first)
9610 first = false;
9611 else
9612 exp->write_c_string(", ");
9613 exp->write_name(p->name());
9614 exp->write_c_string(" ");
9615 exp->write_type(p->type());
9616 }
9617 exp->write_c_string(")");
9618 }
9619 }
9620 }
9621
9622 exp->write_c_string("; ");
9623 }
9624 }
9625
9626 exp->write_c_string("}");
9627 }
9628
9629 // Import an interface type.
9630
9631 Interface_type*
9632 Interface_type::do_import(Import* imp)
9633 {
9634 imp->require_c_string("interface { ");
9635
9636 Typed_identifier_list* methods = new Typed_identifier_list;
9637 while (imp->peek_char() != '}')
9638 {
9639 std::string name = imp->read_identifier();
9640
9641 if (name == "?")
9642 {
9643 imp->require_c_string(" ");
9644 Type* t = imp->read_type();
9645 methods->push_back(Typed_identifier("", t, imp->location()));
9646 imp->require_c_string("; ");
9647 continue;
9648 }
9649
9650 imp->require_c_string(" (");
9651
9652 Typed_identifier_list* parameters;
9653 bool is_varargs = false;
9654 if (imp->peek_char() == ')')
9655 parameters = NULL;
9656 else
9657 {
9658 parameters = new Typed_identifier_list;
9659 while (true)
9660 {
9661 std::string name = imp->read_name();
9662 imp->require_c_string(" ");
9663
9664 if (imp->match_c_string("..."))
9665 {
9666 imp->advance(3);
9667 is_varargs = true;
9668 }
9669
9670 Type* ptype = imp->read_type();
9671 if (is_varargs)
9672 ptype = Type::make_array_type(ptype, NULL);
9673 parameters->push_back(Typed_identifier(name, ptype,
9674 imp->location()));
9675 if (imp->peek_char() != ',')
9676 break;
9677 go_assert(!is_varargs);
9678 imp->require_c_string(", ");
9679 }
9680 }
9681 imp->require_c_string(")");
9682
9683 Typed_identifier_list* results;
9684 if (imp->peek_char() != ' ')
9685 results = NULL;
9686 else
9687 {
9688 results = new Typed_identifier_list;
9689 imp->advance(1);
9690 if (imp->peek_char() != '(')
9691 {
9692 Type* rtype = imp->read_type();
9693 results->push_back(Typed_identifier("", rtype, imp->location()));
9694 }
9695 else
9696 {
9697 imp->advance(1);
9698 while (true)
9699 {
9700 std::string name = imp->read_name();
9701 imp->require_c_string(" ");
9702 Type* rtype = imp->read_type();
9703 results->push_back(Typed_identifier(name, rtype,
9704 imp->location()));
9705 if (imp->peek_char() != ',')
9706 break;
9707 imp->require_c_string(", ");
9708 }
9709 imp->require_c_string(")");
9710 }
9711 }
9712
9713 Function_type* fntype = Type::make_function_type(NULL, parameters,
9714 results,
9715 imp->location());
9716 if (is_varargs)
9717 fntype->set_is_varargs();
9718 methods->push_back(Typed_identifier(name, fntype, imp->location()));
9719
9720 imp->require_c_string("; ");
9721 }
9722
9723 imp->require_c_string("}");
9724
9725 if (methods->empty())
9726 {
9727 delete methods;
9728 methods = NULL;
9729 }
9730
9731 Interface_type* ret = Type::make_interface_type(methods, imp->location());
9732 ret->package_ = imp->package();
9733 return ret;
9734 }
9735
9736 // Make an interface type.
9737
9738 Interface_type*
9739 Type::make_interface_type(Typed_identifier_list* methods,
9740 Location location)
9741 {
9742 return new Interface_type(methods, location);
9743 }
9744
9745 // Make an empty interface type.
9746
9747 Interface_type*
9748 Type::make_empty_interface_type(Location location)
9749 {
9750 Interface_type* ret = new Interface_type(NULL, location);
9751 ret->finalize_methods();
9752 return ret;
9753 }
9754
9755 // Class Method.
9756
9757 // Bind a method to an object.
9758
9759 Expression*
9760 Method::bind_method(Expression* expr, Location location) const
9761 {
9762 if (this->stub_ == NULL)
9763 {
9764 // When there is no stub object, the binding is determined by
9765 // the child class.
9766 return this->do_bind_method(expr, location);
9767 }
9768 return Expression::make_bound_method(expr, this, this->stub_, location);
9769 }
9770
9771 // Return the named object associated with a method. This may only be
9772 // called after methods are finalized.
9773
9774 Named_object*
9775 Method::named_object() const
9776 {
9777 if (this->stub_ != NULL)
9778 return this->stub_;
9779 return this->do_named_object();
9780 }
9781
9782 // Class Named_method.
9783
9784 // The type of the method.
9785
9786 Function_type*
9787 Named_method::do_type() const
9788 {
9789 if (this->named_object_->is_function())
9790 return this->named_object_->func_value()->type();
9791 else if (this->named_object_->is_function_declaration())
9792 return this->named_object_->func_declaration_value()->type();
9793 else
9794 go_unreachable();
9795 }
9796
9797 // Return the location of the method receiver.
9798
9799 Location
9800 Named_method::do_receiver_location() const
9801 {
9802 return this->do_type()->receiver()->location();
9803 }
9804
9805 // Bind a method to an object.
9806
9807 Expression*
9808 Named_method::do_bind_method(Expression* expr, Location location) const
9809 {
9810 Named_object* no = this->named_object_;
9811 Bound_method_expression* bme = Expression::make_bound_method(expr, this,
9812 no, location);
9813 // If this is not a local method, and it does not use a stub, then
9814 // the real method expects a different type. We need to cast the
9815 // first argument.
9816 if (this->depth() > 0 && !this->needs_stub_method())
9817 {
9818 Function_type* ftype = this->do_type();
9819 go_assert(ftype->is_method());
9820 Type* frtype = ftype->receiver()->type();
9821 bme->set_first_argument_type(frtype);
9822 }
9823 return bme;
9824 }
9825
9826 // Return whether this method should not participate in interfaces.
9827
9828 bool
9829 Named_method::do_nointerface() const
9830 {
9831 Named_object* no = this->named_object_;
9832 if (no->is_function())
9833 return no->func_value()->nointerface();
9834 else if (no->is_function_declaration())
9835 return no->func_declaration_value()->nointerface();
9836 else
9837 go_unreachable();
9838 }
9839
9840 // Class Interface_method.
9841
9842 // Bind a method to an object.
9843
9844 Expression*
9845 Interface_method::do_bind_method(Expression* expr,
9846 Location location) const
9847 {
9848 return Expression::make_interface_field_reference(expr, this->name_,
9849 location);
9850 }
9851
9852 // Class Methods.
9853
9854 // Insert a new method. Return true if it was inserted, false
9855 // otherwise.
9856
9857 bool
9858 Methods::insert(const std::string& name, Method* m)
9859 {
9860 std::pair<Method_map::iterator, bool> ins =
9861 this->methods_.insert(std::make_pair(name, m));
9862 if (ins.second)
9863 return true;
9864 else
9865 {
9866 Method* old_method = ins.first->second;
9867 if (m->depth() < old_method->depth())
9868 {
9869 delete old_method;
9870 ins.first->second = m;
9871 return true;
9872 }
9873 else
9874 {
9875 if (m->depth() == old_method->depth())
9876 old_method->set_is_ambiguous();
9877 return false;
9878 }
9879 }
9880 }
9881
9882 // Return the number of unambiguous methods.
9883
9884 size_t
9885 Methods::count() const
9886 {
9887 size_t ret = 0;
9888 for (Method_map::const_iterator p = this->methods_.begin();
9889 p != this->methods_.end();
9890 ++p)
9891 if (!p->second->is_ambiguous())
9892 ++ret;
9893 return ret;
9894 }
9895
9896 // Class Named_type.
9897
9898 // Return the name of the type.
9899
9900 const std::string&
9901 Named_type::name() const
9902 {
9903 return this->named_object_->name();
9904 }
9905
9906 // Return the name of the type to use in an error message.
9907
9908 std::string
9909 Named_type::message_name() const
9910 {
9911 return this->named_object_->message_name();
9912 }
9913
9914 // Return the base type for this type. We have to be careful about
9915 // circular type definitions, which are invalid but may be seen here.
9916
9917 Type*
9918 Named_type::named_base()
9919 {
9920 if (this->seen_)
9921 return this;
9922 this->seen_ = true;
9923 Type* ret = this->type_->base();
9924 this->seen_ = false;
9925 return ret;
9926 }
9927
9928 const Type*
9929 Named_type::named_base() const
9930 {
9931 if (this->seen_)
9932 return this;
9933 this->seen_ = true;
9934 const Type* ret = this->type_->base();
9935 this->seen_ = false;
9936 return ret;
9937 }
9938
9939 // Return whether this is an error type. We have to be careful about
9940 // circular type definitions, which are invalid but may be seen here.
9941
9942 bool
9943 Named_type::is_named_error_type() const
9944 {
9945 if (this->seen_)
9946 return false;
9947 this->seen_ = true;
9948 bool ret = this->type_->is_error_type();
9949 this->seen_ = false;
9950 return ret;
9951 }
9952
9953 // Whether this type is comparable. We have to be careful about
9954 // circular type definitions.
9955
9956 bool
9957 Named_type::named_type_is_comparable(std::string* reason) const
9958 {
9959 if (this->seen_)
9960 return false;
9961 this->seen_ = true;
9962 bool ret = Type::are_compatible_for_comparison(true, this->type_,
9963 this->type_, reason);
9964 this->seen_ = false;
9965 return ret;
9966 }
9967
9968 // Add a method to this type.
9969
9970 Named_object*
9971 Named_type::add_method(const std::string& name, Function* function)
9972 {
9973 go_assert(!this->is_alias_);
9974 if (this->local_methods_ == NULL)
9975 this->local_methods_ = new Bindings(NULL);
9976 return this->local_methods_->add_function(name,
9977 this->named_object_->package(),
9978 function);
9979 }
9980
9981 // Add a method declaration to this type.
9982
9983 Named_object*
9984 Named_type::add_method_declaration(const std::string& name, Package* package,
9985 Function_type* type,
9986 Location location)
9987 {
9988 go_assert(!this->is_alias_);
9989 if (this->local_methods_ == NULL)
9990 this->local_methods_ = new Bindings(NULL);
9991 return this->local_methods_->add_function_declaration(name, package, type,
9992 location);
9993 }
9994
9995 // Add an existing method to this type.
9996
9997 void
9998 Named_type::add_existing_method(Named_object* no)
9999 {
10000 go_assert(!this->is_alias_);
10001 if (this->local_methods_ == NULL)
10002 this->local_methods_ = new Bindings(NULL);
10003 this->local_methods_->add_named_object(no);
10004 }
10005
10006 // Look for a local method NAME, and returns its named object, or NULL
10007 // if not there.
10008
10009 Named_object*
10010 Named_type::find_local_method(const std::string& name) const
10011 {
10012 if (this->is_error_)
10013 return NULL;
10014 if (this->is_alias_)
10015 {
10016 Named_type* nt = this->type_->named_type();
10017 if (nt != NULL)
10018 {
10019 if (this->seen_alias_)
10020 return NULL;
10021 this->seen_alias_ = true;
10022 Named_object* ret = nt->find_local_method(name);
10023 this->seen_alias_ = false;
10024 return ret;
10025 }
10026 return NULL;
10027 }
10028 if (this->local_methods_ == NULL)
10029 return NULL;
10030 return this->local_methods_->lookup(name);
10031 }
10032
10033 // Return the list of local methods.
10034
10035 const Bindings*
10036 Named_type::local_methods() const
10037 {
10038 if (this->is_error_)
10039 return NULL;
10040 if (this->is_alias_)
10041 {
10042 Named_type* nt = this->type_->named_type();
10043 if (nt != NULL)
10044 {
10045 if (this->seen_alias_)
10046 return NULL;
10047 this->seen_alias_ = true;
10048 const Bindings* ret = nt->local_methods();
10049 this->seen_alias_ = false;
10050 return ret;
10051 }
10052 return NULL;
10053 }
10054 return this->local_methods_;
10055 }
10056
10057 // Return whether NAME is an unexported field or method, for better
10058 // error reporting.
10059
10060 bool
10061 Named_type::is_unexported_local_method(Gogo* gogo,
10062 const std::string& name) const
10063 {
10064 if (this->is_error_)
10065 return false;
10066 if (this->is_alias_)
10067 {
10068 Named_type* nt = this->type_->named_type();
10069 if (nt != NULL)
10070 {
10071 if (this->seen_alias_)
10072 return false;
10073 this->seen_alias_ = true;
10074 bool ret = nt->is_unexported_local_method(gogo, name);
10075 this->seen_alias_ = false;
10076 return ret;
10077 }
10078 return false;
10079 }
10080 Bindings* methods = this->local_methods_;
10081 if (methods != NULL)
10082 {
10083 for (Bindings::const_declarations_iterator p =
10084 methods->begin_declarations();
10085 p != methods->end_declarations();
10086 ++p)
10087 {
10088 if (Gogo::is_hidden_name(p->first)
10089 && name == Gogo::unpack_hidden_name(p->first)
10090 && gogo->pack_hidden_name(name, false) != p->first)
10091 return true;
10092 }
10093 }
10094 return false;
10095 }
10096
10097 // Build the complete list of methods for this type, which means
10098 // recursively including all methods for anonymous fields. Create all
10099 // stub methods.
10100
10101 void
10102 Named_type::finalize_methods(Gogo* gogo)
10103 {
10104 if (this->is_alias_)
10105 return;
10106 if (this->all_methods_ != NULL)
10107 return;
10108
10109 if (this->local_methods_ != NULL
10110 && (this->points_to() != NULL || this->interface_type() != NULL))
10111 {
10112 const Bindings* lm = this->local_methods_;
10113 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
10114 p != lm->end_declarations();
10115 ++p)
10116 go_error_at(p->second->location(),
10117 "invalid pointer or interface receiver type");
10118 delete this->local_methods_;
10119 this->local_methods_ = NULL;
10120 return;
10121 }
10122
10123 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
10124 }
10125
10126 // Return whether this type has any methods.
10127
10128 bool
10129 Named_type::has_any_methods() const
10130 {
10131 if (this->is_error_)
10132 return false;
10133 if (this->is_alias_)
10134 {
10135 if (this->type_->named_type() != NULL)
10136 {
10137 if (this->seen_alias_)
10138 return false;
10139 this->seen_alias_ = true;
10140 bool ret = this->type_->named_type()->has_any_methods();
10141 this->seen_alias_ = false;
10142 return ret;
10143 }
10144 if (this->type_->struct_type() != NULL)
10145 return this->type_->struct_type()->has_any_methods();
10146 return false;
10147 }
10148 return this->all_methods_ != NULL;
10149 }
10150
10151 // Return the methods for this type.
10152
10153 const Methods*
10154 Named_type::methods() const
10155 {
10156 if (this->is_error_)
10157 return NULL;
10158 if (this->is_alias_)
10159 {
10160 if (this->type_->named_type() != NULL)
10161 {
10162 if (this->seen_alias_)
10163 return NULL;
10164 this->seen_alias_ = true;
10165 const Methods* ret = this->type_->named_type()->methods();
10166 this->seen_alias_ = false;
10167 return ret;
10168 }
10169 if (this->type_->struct_type() != NULL)
10170 return this->type_->struct_type()->methods();
10171 return NULL;
10172 }
10173 return this->all_methods_;
10174 }
10175
10176 // Return the method NAME, or NULL if there isn't one or if it is
10177 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
10178 // ambiguous.
10179
10180 Method*
10181 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
10182 {
10183 if (this->is_error_)
10184 return NULL;
10185 if (this->is_alias_)
10186 {
10187 if (is_ambiguous != NULL)
10188 *is_ambiguous = false;
10189 if (this->type_->named_type() != NULL)
10190 {
10191 if (this->seen_alias_)
10192 return NULL;
10193 this->seen_alias_ = true;
10194 Named_type* nt = this->type_->named_type();
10195 Method* ret = nt->method_function(name, is_ambiguous);
10196 this->seen_alias_ = false;
10197 return ret;
10198 }
10199 if (this->type_->struct_type() != NULL)
10200 return this->type_->struct_type()->method_function(name, is_ambiguous);
10201 return NULL;
10202 }
10203 return Type::method_function(this->all_methods_, name, is_ambiguous);
10204 }
10205
10206 // Return a pointer to the interface method table for this type for
10207 // the interface INTERFACE. IS_POINTER is true if this is for a
10208 // pointer to THIS.
10209
10210 Expression*
10211 Named_type::interface_method_table(Interface_type* interface, bool is_pointer)
10212 {
10213 if (this->is_error_)
10214 return Expression::make_error(this->location_);
10215 if (this->is_alias_)
10216 {
10217 if (this->type_->named_type() != NULL)
10218 {
10219 if (this->seen_alias_)
10220 return Expression::make_error(this->location_);
10221 this->seen_alias_ = true;
10222 Named_type* nt = this->type_->named_type();
10223 Expression* ret = nt->interface_method_table(interface, is_pointer);
10224 this->seen_alias_ = false;
10225 return ret;
10226 }
10227 if (this->type_->struct_type() != NULL)
10228 return this->type_->struct_type()->interface_method_table(interface,
10229 is_pointer);
10230 go_unreachable();
10231 }
10232 return Type::interface_method_table(this, interface, is_pointer,
10233 &this->interface_method_tables_,
10234 &this->pointer_interface_method_tables_);
10235 }
10236
10237 // Look for a use of a complete type within another type. This is
10238 // used to check that we don't try to use a type within itself.
10239
10240 class Find_type_use : public Traverse
10241 {
10242 public:
10243 Find_type_use(Named_type* find_type)
10244 : Traverse(traverse_types),
10245 find_type_(find_type), found_(false)
10246 { }
10247
10248 // Whether we found the type.
10249 bool
10250 found() const
10251 { return this->found_; }
10252
10253 protected:
10254 int
10255 type(Type*);
10256
10257 private:
10258 // The type we are looking for.
10259 Named_type* find_type_;
10260 // Whether we found the type.
10261 bool found_;
10262 };
10263
10264 // Check for FIND_TYPE in TYPE.
10265
10266 int
10267 Find_type_use::type(Type* type)
10268 {
10269 if (type->named_type() != NULL && this->find_type_ == type->named_type())
10270 {
10271 this->found_ = true;
10272 return TRAVERSE_EXIT;
10273 }
10274
10275 // It's OK if we see a reference to the type in any type which is
10276 // essentially a pointer: a pointer, a slice, a function, a map, or
10277 // a channel.
10278 if (type->points_to() != NULL
10279 || type->is_slice_type()
10280 || type->function_type() != NULL
10281 || type->map_type() != NULL
10282 || type->channel_type() != NULL)
10283 return TRAVERSE_SKIP_COMPONENTS;
10284
10285 // For an interface, a reference to the type in a method type should
10286 // be ignored, but we have to consider direct inheritance. When
10287 // this is called, there may be cases of direct inheritance
10288 // represented as a method with no name.
10289 if (type->interface_type() != NULL)
10290 {
10291 const Typed_identifier_list* methods = type->interface_type()->methods();
10292 if (methods != NULL)
10293 {
10294 for (Typed_identifier_list::const_iterator p = methods->begin();
10295 p != methods->end();
10296 ++p)
10297 {
10298 if (p->name().empty())
10299 {
10300 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
10301 return TRAVERSE_EXIT;
10302 }
10303 }
10304 }
10305 return TRAVERSE_SKIP_COMPONENTS;
10306 }
10307
10308 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
10309 // to convert TYPE to the backend representation before we convert
10310 // FIND_TYPE_.
10311 if (type->named_type() != NULL)
10312 {
10313 switch (type->base()->classification())
10314 {
10315 case Type::TYPE_ERROR:
10316 case Type::TYPE_BOOLEAN:
10317 case Type::TYPE_INTEGER:
10318 case Type::TYPE_FLOAT:
10319 case Type::TYPE_COMPLEX:
10320 case Type::TYPE_STRING:
10321 case Type::TYPE_NIL:
10322 break;
10323
10324 case Type::TYPE_ARRAY:
10325 case Type::TYPE_STRUCT:
10326 this->find_type_->add_dependency(type->named_type());
10327 break;
10328
10329 case Type::TYPE_NAMED:
10330 if (type->named_type() == type->base()->named_type())
10331 {
10332 this->found_ = true;
10333 return TRAVERSE_EXIT;
10334 }
10335 else
10336 go_assert(saw_errors());
10337 break;
10338
10339 case Type::TYPE_FORWARD:
10340 go_assert(saw_errors());
10341 break;
10342
10343 case Type::TYPE_VOID:
10344 case Type::TYPE_SINK:
10345 case Type::TYPE_FUNCTION:
10346 case Type::TYPE_POINTER:
10347 case Type::TYPE_CALL_MULTIPLE_RESULT:
10348 case Type::TYPE_MAP:
10349 case Type::TYPE_CHANNEL:
10350 case Type::TYPE_INTERFACE:
10351 default:
10352 go_unreachable();
10353 }
10354 }
10355
10356 return TRAVERSE_CONTINUE;
10357 }
10358
10359 // Look for a circular reference of an alias.
10360
10361 class Find_alias : public Traverse
10362 {
10363 public:
10364 Find_alias(Named_type* find_type)
10365 : Traverse(traverse_types),
10366 find_type_(find_type), found_(false)
10367 { }
10368
10369 // Whether we found the type.
10370 bool
10371 found() const
10372 { return this->found_; }
10373
10374 protected:
10375 int
10376 type(Type*);
10377
10378 private:
10379 // The type we are looking for.
10380 Named_type* find_type_;
10381 // Whether we found the type.
10382 bool found_;
10383 };
10384
10385 int
10386 Find_alias::type(Type* type)
10387 {
10388 Named_type* nt = type->named_type();
10389 if (nt != NULL)
10390 {
10391 if (nt == this->find_type_)
10392 {
10393 this->found_ = true;
10394 return TRAVERSE_EXIT;
10395 }
10396
10397 // We started from `type T1 = T2`, where T1 is find_type_ and T2
10398 // is, perhaps indirectly, the parameter TYPE. If TYPE is not
10399 // an alias itself, it's OK if whatever T2 is defined as refers
10400 // to T1.
10401 if (!nt->is_alias())
10402 return TRAVERSE_SKIP_COMPONENTS;
10403 }
10404
10405 // Check if there are recursive inherited interface aliases.
10406 Interface_type* ift = type->interface_type();
10407 if (ift != NULL)
10408 {
10409 const Typed_identifier_list* methods = ift->local_methods();
10410 if (methods == NULL)
10411 return TRAVERSE_CONTINUE;
10412 for (Typed_identifier_list::const_iterator p = methods->begin();
10413 p != methods->end();
10414 ++p)
10415 if (p->name().empty() && p->type()->named_type() == this->find_type_)
10416 {
10417 this->found_ = true;
10418 return TRAVERSE_EXIT;
10419 }
10420 }
10421
10422 return TRAVERSE_CONTINUE;
10423 }
10424
10425 // Verify that a named type does not refer to itself.
10426
10427 bool
10428 Named_type::do_verify()
10429 {
10430 if (this->is_verified_)
10431 return true;
10432 this->is_verified_ = true;
10433
10434 if (this->is_error_)
10435 return false;
10436
10437 if (this->is_alias_)
10438 {
10439 Find_alias find(this);
10440 Type::traverse(this->type_, &find);
10441 if (find.found())
10442 {
10443 go_error_at(this->location_, "invalid recursive alias %qs",
10444 this->message_name().c_str());
10445 this->is_error_ = true;
10446 return false;
10447 }
10448 }
10449
10450 Find_type_use find(this);
10451 Type::traverse(this->type_, &find);
10452 if (find.found())
10453 {
10454 go_error_at(this->location_, "invalid recursive type %qs",
10455 this->message_name().c_str());
10456 this->is_error_ = true;
10457 return false;
10458 }
10459
10460 // Check whether any of the local methods overloads an existing
10461 // struct field or interface method. We don't need to check the
10462 // list of methods against itself: that is handled by the Bindings
10463 // code.
10464 if (this->local_methods_ != NULL)
10465 {
10466 Struct_type* st = this->type_->struct_type();
10467 if (st != NULL)
10468 {
10469 for (Bindings::const_declarations_iterator p =
10470 this->local_methods_->begin_declarations();
10471 p != this->local_methods_->end_declarations();
10472 ++p)
10473 {
10474 const std::string& name(p->first);
10475 if (st != NULL && st->find_local_field(name, NULL) != NULL)
10476 {
10477 go_error_at(p->second->location(),
10478 "method %qs redeclares struct field name",
10479 Gogo::message_name(name).c_str());
10480 }
10481 }
10482 }
10483 }
10484
10485 return true;
10486 }
10487
10488 // Return whether this type is or contains a pointer.
10489
10490 bool
10491 Named_type::do_has_pointer() const
10492 {
10493 if (this->seen_)
10494 return false;
10495 this->seen_ = true;
10496 bool ret = this->type_->has_pointer();
10497 this->seen_ = false;
10498 return ret;
10499 }
10500
10501 // Return whether comparisons for this type can use the identity
10502 // function.
10503
10504 bool
10505 Named_type::do_compare_is_identity(Gogo* gogo)
10506 {
10507 // We don't use this->seen_ here because compare_is_identity may
10508 // call base() later, and that will mess up if seen_ is set here.
10509 if (this->seen_in_compare_is_identity_)
10510 return false;
10511 this->seen_in_compare_is_identity_ = true;
10512 bool ret = this->type_->compare_is_identity(gogo);
10513 this->seen_in_compare_is_identity_ = false;
10514 return ret;
10515 }
10516
10517 // Return whether this type is reflexive--whether it is always equal
10518 // to itself.
10519
10520 bool
10521 Named_type::do_is_reflexive()
10522 {
10523 if (this->seen_in_compare_is_identity_)
10524 return false;
10525 this->seen_in_compare_is_identity_ = true;
10526 bool ret = this->type_->is_reflexive();
10527 this->seen_in_compare_is_identity_ = false;
10528 return ret;
10529 }
10530
10531 // Return whether this type needs a key update when used as a map key.
10532
10533 bool
10534 Named_type::do_needs_key_update()
10535 {
10536 if (this->seen_in_compare_is_identity_)
10537 return true;
10538 this->seen_in_compare_is_identity_ = true;
10539 bool ret = this->type_->needs_key_update();
10540 this->seen_in_compare_is_identity_ = false;
10541 return ret;
10542 }
10543
10544 // Return a hash code. This is used for method lookup. We simply
10545 // hash on the name itself.
10546
10547 unsigned int
10548 Named_type::do_hash_for_method(Gogo* gogo, int) const
10549 {
10550 if (this->is_error_)
10551 return 0;
10552
10553 // Aliases are handled in Type::hash_for_method.
10554 go_assert(!this->is_alias_);
10555
10556 const std::string& name(this->named_object()->name());
10557 unsigned int ret = Gogo::hash_string(name, 0);
10558
10559 // GOGO will be NULL here when called from Type_hash_identical.
10560 // That is OK because that is only used for internal hash tables
10561 // where we are going to be comparing named types for equality. In
10562 // other cases, which are cases where the runtime is going to
10563 // compare hash codes to see if the types are the same, we need to
10564 // include the pkgpath in the hash.
10565 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
10566 {
10567 const Package* package = this->named_object()->package();
10568 if (package == NULL)
10569 ret = Gogo::hash_string(gogo->pkgpath(), ret);
10570 else
10571 ret = Gogo::hash_string(package->pkgpath(), ret);
10572 }
10573
10574 return ret;
10575 }
10576
10577 // Convert a named type to the backend representation. In order to
10578 // get dependencies right, we fill in a dummy structure for this type,
10579 // then convert all the dependencies, then complete this type. When
10580 // this function is complete, the size of the type is known.
10581
10582 void
10583 Named_type::convert(Gogo* gogo)
10584 {
10585 if (this->is_error_ || this->is_converted_)
10586 return;
10587
10588 this->create_placeholder(gogo);
10589
10590 // If we are called to turn unsafe.Sizeof into a constant, we may
10591 // not have verified the type yet. We have to make sure it is
10592 // verified, since that sets the list of dependencies.
10593 this->verify();
10594
10595 // Convert all the dependencies. If they refer indirectly back to
10596 // this type, they will pick up the intermediate representation we just
10597 // created.
10598 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
10599 p != this->dependencies_.end();
10600 ++p)
10601 (*p)->convert(gogo);
10602
10603 // Complete this type.
10604 Btype* bt = this->named_btype_;
10605 Type* base = this->type_->base();
10606 switch (base->classification())
10607 {
10608 case TYPE_VOID:
10609 case TYPE_BOOLEAN:
10610 case TYPE_INTEGER:
10611 case TYPE_FLOAT:
10612 case TYPE_COMPLEX:
10613 case TYPE_STRING:
10614 case TYPE_NIL:
10615 break;
10616
10617 case TYPE_MAP:
10618 case TYPE_CHANNEL:
10619 break;
10620
10621 case TYPE_FUNCTION:
10622 case TYPE_POINTER:
10623 // The size of these types is already correct. We don't worry
10624 // about filling them in until later, when we also track
10625 // circular references.
10626 break;
10627
10628 case TYPE_STRUCT:
10629 {
10630 std::vector<Backend::Btyped_identifier> bfields;
10631 get_backend_struct_fields(gogo, base->struct_type(), true, &bfields);
10632 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
10633 bt = gogo->backend()->error_type();
10634 }
10635 break;
10636
10637 case TYPE_ARRAY:
10638 // Slice types were completed in create_placeholder.
10639 if (!base->is_slice_type())
10640 {
10641 Btype* bet = base->array_type()->get_backend_element(gogo, true);
10642 Bexpression* blen = base->array_type()->get_backend_length(gogo);
10643 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
10644 bt = gogo->backend()->error_type();
10645 }
10646 break;
10647
10648 case TYPE_INTERFACE:
10649 // Interface types were completed in create_placeholder.
10650 break;
10651
10652 case TYPE_ERROR:
10653 return;
10654
10655 default:
10656 case TYPE_SINK:
10657 case TYPE_CALL_MULTIPLE_RESULT:
10658 case TYPE_NAMED:
10659 case TYPE_FORWARD:
10660 go_unreachable();
10661 }
10662
10663 this->named_btype_ = bt;
10664 this->is_converted_ = true;
10665 this->is_placeholder_ = false;
10666 }
10667
10668 // Create the placeholder for a named type. This is the first step in
10669 // converting to the backend representation.
10670
10671 void
10672 Named_type::create_placeholder(Gogo* gogo)
10673 {
10674 if (this->is_error_)
10675 this->named_btype_ = gogo->backend()->error_type();
10676
10677 if (this->named_btype_ != NULL)
10678 return;
10679
10680 // Create the structure for this type. Note that because we call
10681 // base() here, we don't attempt to represent a named type defined
10682 // as another named type. Instead both named types will point to
10683 // different base representations.
10684 Type* base = this->type_->base();
10685 Btype* bt;
10686 bool set_name = true;
10687 switch (base->classification())
10688 {
10689 case TYPE_ERROR:
10690 this->is_error_ = true;
10691 this->named_btype_ = gogo->backend()->error_type();
10692 return;
10693
10694 case TYPE_VOID:
10695 case TYPE_BOOLEAN:
10696 case TYPE_INTEGER:
10697 case TYPE_FLOAT:
10698 case TYPE_COMPLEX:
10699 case TYPE_STRING:
10700 case TYPE_NIL:
10701 // These are simple basic types, we can just create them
10702 // directly.
10703 bt = Type::get_named_base_btype(gogo, base);
10704 break;
10705
10706 case TYPE_MAP:
10707 case TYPE_CHANNEL:
10708 // All maps and channels have the same backend representation.
10709 bt = Type::get_named_base_btype(gogo, base);
10710 break;
10711
10712 case TYPE_FUNCTION:
10713 case TYPE_POINTER:
10714 {
10715 bool for_function = base->classification() == TYPE_FUNCTION;
10716 bt = gogo->backend()->placeholder_pointer_type(this->name(),
10717 this->location_,
10718 for_function);
10719 set_name = false;
10720 }
10721 break;
10722
10723 case TYPE_STRUCT:
10724 bt = gogo->backend()->placeholder_struct_type(this->name(),
10725 this->location_);
10726 this->is_placeholder_ = true;
10727 set_name = false;
10728 break;
10729
10730 case TYPE_ARRAY:
10731 if (base->is_slice_type())
10732 bt = gogo->backend()->placeholder_struct_type(this->name(),
10733 this->location_);
10734 else
10735 {
10736 bt = gogo->backend()->placeholder_array_type(this->name(),
10737 this->location_);
10738 this->is_placeholder_ = true;
10739 }
10740 set_name = false;
10741 break;
10742
10743 case TYPE_INTERFACE:
10744 if (base->interface_type()->is_empty())
10745 bt = Interface_type::get_backend_empty_interface_type(gogo);
10746 else
10747 {
10748 bt = gogo->backend()->placeholder_struct_type(this->name(),
10749 this->location_);
10750 set_name = false;
10751 }
10752 break;
10753
10754 default:
10755 case TYPE_SINK:
10756 case TYPE_CALL_MULTIPLE_RESULT:
10757 case TYPE_NAMED:
10758 case TYPE_FORWARD:
10759 go_unreachable();
10760 }
10761
10762 if (set_name)
10763 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
10764
10765 this->named_btype_ = bt;
10766
10767 if (base->is_slice_type())
10768 {
10769 // We do not record slices as dependencies of other types,
10770 // because we can fill them in completely here with the final
10771 // size.
10772 std::vector<Backend::Btyped_identifier> bfields;
10773 get_backend_slice_fields(gogo, base->array_type(), true, &bfields);
10774 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
10775 this->named_btype_ = gogo->backend()->error_type();
10776 }
10777 else if (base->interface_type() != NULL
10778 && !base->interface_type()->is_empty())
10779 {
10780 // We do not record interfaces as dependencies of other types,
10781 // because we can fill them in completely here with the final
10782 // size.
10783 std::vector<Backend::Btyped_identifier> bfields;
10784 get_backend_interface_fields(gogo, base->interface_type(), true,
10785 &bfields);
10786 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
10787 this->named_btype_ = gogo->backend()->error_type();
10788 }
10789 }
10790
10791 // Get the backend representation for a named type.
10792
10793 Btype*
10794 Named_type::do_get_backend(Gogo* gogo)
10795 {
10796 if (this->is_error_)
10797 return gogo->backend()->error_type();
10798
10799 Btype* bt = this->named_btype_;
10800
10801 if (!gogo->named_types_are_converted())
10802 {
10803 // We have not completed converting named types. NAMED_BTYPE_
10804 // is a placeholder and we shouldn't do anything further.
10805 if (bt != NULL)
10806 return bt;
10807
10808 // We don't build dependencies for types whose sizes do not
10809 // change or are not relevant, so we may see them here while
10810 // converting types.
10811 this->create_placeholder(gogo);
10812 bt = this->named_btype_;
10813 go_assert(bt != NULL);
10814 return bt;
10815 }
10816
10817 // We are not converting types. This should only be called if the
10818 // type has already been converted.
10819 if (!this->is_converted_)
10820 {
10821 go_assert(saw_errors());
10822 return gogo->backend()->error_type();
10823 }
10824
10825 go_assert(bt != NULL);
10826
10827 // Complete the backend representation.
10828 Type* base = this->type_->base();
10829 Btype* bt1;
10830 switch (base->classification())
10831 {
10832 case TYPE_ERROR:
10833 return gogo->backend()->error_type();
10834
10835 case TYPE_VOID:
10836 case TYPE_BOOLEAN:
10837 case TYPE_INTEGER:
10838 case TYPE_FLOAT:
10839 case TYPE_COMPLEX:
10840 case TYPE_STRING:
10841 case TYPE_NIL:
10842 case TYPE_MAP:
10843 case TYPE_CHANNEL:
10844 return bt;
10845
10846 case TYPE_STRUCT:
10847 if (!this->seen_in_get_backend_)
10848 {
10849 this->seen_in_get_backend_ = true;
10850 base->struct_type()->finish_backend_fields(gogo);
10851 this->seen_in_get_backend_ = false;
10852 }
10853 return bt;
10854
10855 case TYPE_ARRAY:
10856 if (!this->seen_in_get_backend_)
10857 {
10858 this->seen_in_get_backend_ = true;
10859 base->array_type()->finish_backend_element(gogo);
10860 this->seen_in_get_backend_ = false;
10861 }
10862 return bt;
10863
10864 case TYPE_INTERFACE:
10865 if (!this->seen_in_get_backend_)
10866 {
10867 this->seen_in_get_backend_ = true;
10868 base->interface_type()->finish_backend_methods(gogo);
10869 this->seen_in_get_backend_ = false;
10870 }
10871 return bt;
10872
10873 case TYPE_FUNCTION:
10874 // Don't build a circular data structure. GENERIC can't handle
10875 // it.
10876 if (this->seen_in_get_backend_)
10877 return gogo->backend()->circular_pointer_type(bt, true);
10878 this->seen_in_get_backend_ = true;
10879 bt1 = Type::get_named_base_btype(gogo, base);
10880 this->seen_in_get_backend_ = false;
10881 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
10882 bt = gogo->backend()->error_type();
10883 return bt;
10884
10885 case TYPE_POINTER:
10886 // Don't build a circular data structure. GENERIC can't handle
10887 // it.
10888 if (this->seen_in_get_backend_)
10889 return gogo->backend()->circular_pointer_type(bt, false);
10890 this->seen_in_get_backend_ = true;
10891 bt1 = Type::get_named_base_btype(gogo, base);
10892 this->seen_in_get_backend_ = false;
10893 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
10894 bt = gogo->backend()->error_type();
10895 return bt;
10896
10897 default:
10898 case TYPE_SINK:
10899 case TYPE_CALL_MULTIPLE_RESULT:
10900 case TYPE_NAMED:
10901 case TYPE_FORWARD:
10902 go_unreachable();
10903 }
10904
10905 go_unreachable();
10906 }
10907
10908 // Build a type descriptor for a named type.
10909
10910 Expression*
10911 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
10912 {
10913 if (this->is_error_)
10914 return Expression::make_error(this->location_);
10915 if (name == NULL && this->is_alias_)
10916 {
10917 if (this->seen_alias_)
10918 return Expression::make_error(this->location_);
10919 this->seen_alias_ = true;
10920 Expression* ret = this->type_->type_descriptor(gogo, NULL);
10921 this->seen_alias_ = false;
10922 return ret;
10923 }
10924
10925 // If NAME is not NULL, then we don't really want the type
10926 // descriptor for this type; we want the descriptor for the
10927 // underlying type, giving it the name NAME.
10928 return this->named_type_descriptor(gogo, this->type_,
10929 name == NULL ? this : name);
10930 }
10931
10932 // Add to the reflection string. This is used mostly for the name of
10933 // the type used in a type descriptor, not for actual reflection
10934 // strings.
10935
10936 void
10937 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
10938 {
10939 this->append_reflection_type_name(gogo, false, ret);
10940 }
10941
10942 // Add to the reflection string. For an alias we normally use the
10943 // real name, but if USE_ALIAS is true we use the alias name itself.
10944
10945 void
10946 Named_type::append_reflection_type_name(Gogo* gogo, bool use_alias,
10947 std::string* ret) const
10948 {
10949 if (this->is_error_)
10950 return;
10951 if (this->is_alias_ && !use_alias)
10952 {
10953 if (this->seen_alias_)
10954 return;
10955 this->seen_alias_ = true;
10956 this->append_reflection(this->type_, gogo, ret);
10957 this->seen_alias_ = false;
10958 return;
10959 }
10960 if (!this->is_builtin())
10961 {
10962 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
10963 // make a unique reflection string, so that the type
10964 // canonicalization in the reflect package will work. In order
10965 // to be compatible with the gc compiler, we put tabs into the
10966 // package path, so that the reflect methods can discard it.
10967 const Package* package = this->named_object_->package();
10968 ret->push_back('\t');
10969 ret->append(package != NULL
10970 ? package->pkgpath_symbol()
10971 : gogo->pkgpath_symbol());
10972 ret->push_back('\t');
10973 ret->append(package != NULL
10974 ? package->package_name()
10975 : gogo->package_name());
10976 ret->push_back('.');
10977 }
10978 if (this->in_function_ != NULL)
10979 {
10980 ret->push_back('\t');
10981 const Typed_identifier* rcvr =
10982 this->in_function_->func_value()->type()->receiver();
10983 if (rcvr != NULL)
10984 {
10985 Named_type* rcvr_type = rcvr->type()->deref()->named_type();
10986 ret->append(Gogo::unpack_hidden_name(rcvr_type->name()));
10987 ret->push_back('.');
10988 }
10989 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
10990 ret->push_back('$');
10991 if (this->in_function_index_ > 0)
10992 {
10993 char buf[30];
10994 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
10995 ret->append(buf);
10996 ret->push_back('$');
10997 }
10998 ret->push_back('\t');
10999 }
11000 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
11001 }
11002
11003 // Import a named type. This is only used for export format versions
11004 // before version 3.
11005
11006 void
11007 Named_type::import_named_type(Import* imp, Named_type** ptype)
11008 {
11009 imp->require_c_string("type ");
11010 Type *type = imp->read_type();
11011 *ptype = type->named_type();
11012 go_assert(*ptype != NULL);
11013 imp->require_semicolon_if_old_version();
11014 imp->require_c_string("\n");
11015 }
11016
11017 // Export the type when it is referenced by another type. In this
11018 // case Export::export_type will already have issued the name. The
11019 // output always ends with a newline, since that is convenient if
11020 // there are methods.
11021
11022 void
11023 Named_type::do_export(Export* exp) const
11024 {
11025 exp->write_type(this->type_);
11026 exp->write_c_string("\n");
11027
11028 // To save space, we only export the methods directly attached to
11029 // this type.
11030 Bindings* methods = this->local_methods_;
11031 if (methods == NULL)
11032 return;
11033
11034 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
11035 p != methods->end_definitions();
11036 ++p)
11037 {
11038 exp->write_c_string(" ");
11039 (*p)->export_named_object(exp);
11040 }
11041
11042 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
11043 p != methods->end_declarations();
11044 ++p)
11045 {
11046 if (p->second->is_function_declaration())
11047 {
11048 exp->write_c_string(" ");
11049 p->second->export_named_object(exp);
11050 }
11051 }
11052 }
11053
11054 // Make a named type.
11055
11056 Named_type*
11057 Type::make_named_type(Named_object* named_object, Type* type,
11058 Location location)
11059 {
11060 return new Named_type(named_object, type, location);
11061 }
11062
11063 // Finalize the methods for TYPE. It will be a named type or a struct
11064 // type. This sets *ALL_METHODS to the list of methods, and builds
11065 // all required stubs.
11066
11067 void
11068 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
11069 Methods** all_methods)
11070 {
11071 *all_methods = new Methods();
11072 std::vector<const Named_type*> seen;
11073 Type::add_methods_for_type(type, NULL, 0, false, false, &seen, *all_methods);
11074 if ((*all_methods)->empty())
11075 {
11076 delete *all_methods;
11077 *all_methods = NULL;
11078 }
11079 Type::build_stub_methods(gogo, type, *all_methods, location);
11080 if (type->is_direct_iface_type())
11081 Type::build_direct_iface_stub_methods(gogo, type, *all_methods, location);
11082 }
11083
11084 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
11085 // build up the struct field indexes as we go. DEPTH is the depth of
11086 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
11087 // adding these methods for an anonymous field with pointer type.
11088 // NEEDS_STUB_METHOD is true if we need to use a stub method which
11089 // calls the real method. TYPES_SEEN is used to avoid infinite
11090 // recursion.
11091
11092 void
11093 Type::add_methods_for_type(const Type* type,
11094 const Method::Field_indexes* field_indexes,
11095 unsigned int depth,
11096 bool is_embedded_pointer,
11097 bool needs_stub_method,
11098 std::vector<const Named_type*>* seen,
11099 Methods* methods)
11100 {
11101 // Pointer types may not have methods.
11102 if (type->points_to() != NULL)
11103 return;
11104
11105 const Named_type* nt = type->named_type();
11106 if (nt != NULL)
11107 {
11108 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
11109 p != seen->end();
11110 ++p)
11111 {
11112 if (*p == nt)
11113 return;
11114 }
11115
11116 seen->push_back(nt);
11117
11118 Type::add_local_methods_for_type(nt, field_indexes, depth,
11119 is_embedded_pointer, needs_stub_method,
11120 methods);
11121 }
11122
11123 Type::add_embedded_methods_for_type(type, field_indexes, depth,
11124 is_embedded_pointer, needs_stub_method,
11125 seen, methods);
11126
11127 // If we are called with depth > 0, then we are looking at an
11128 // anonymous field of a struct. If such a field has interface type,
11129 // then we need to add the interface methods. We don't want to add
11130 // them when depth == 0, because we will already handle them
11131 // following the usual rules for an interface type.
11132 if (depth > 0)
11133 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
11134
11135 if (nt != NULL)
11136 seen->pop_back();
11137 }
11138
11139 // Add the local methods for the named type NT to *METHODS. The
11140 // parameters are as for add_methods_to_type.
11141
11142 void
11143 Type::add_local_methods_for_type(const Named_type* nt,
11144 const Method::Field_indexes* field_indexes,
11145 unsigned int depth,
11146 bool is_embedded_pointer,
11147 bool needs_stub_method,
11148 Methods* methods)
11149 {
11150 const Bindings* local_methods = nt->local_methods();
11151 if (local_methods == NULL)
11152 return;
11153
11154 for (Bindings::const_declarations_iterator p =
11155 local_methods->begin_declarations();
11156 p != local_methods->end_declarations();
11157 ++p)
11158 {
11159 Named_object* no = p->second;
11160 bool is_value_method = (is_embedded_pointer
11161 || !Type::method_expects_pointer(no));
11162 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
11163 (needs_stub_method || depth > 0));
11164 if (!methods->insert(no->name(), m))
11165 delete m;
11166 }
11167 }
11168
11169 // Add the embedded methods for TYPE to *METHODS. These are the
11170 // methods attached to anonymous fields. The parameters are as for
11171 // add_methods_to_type.
11172
11173 void
11174 Type::add_embedded_methods_for_type(const Type* type,
11175 const Method::Field_indexes* field_indexes,
11176 unsigned int depth,
11177 bool is_embedded_pointer,
11178 bool needs_stub_method,
11179 std::vector<const Named_type*>* seen,
11180 Methods* methods)
11181 {
11182 // Look for anonymous fields in TYPE. TYPE has fields if it is a
11183 // struct.
11184 const Struct_type* st = type->struct_type();
11185 if (st == NULL)
11186 return;
11187
11188 const Struct_field_list* fields = st->fields();
11189 if (fields == NULL)
11190 return;
11191
11192 unsigned int i = 0;
11193 for (Struct_field_list::const_iterator pf = fields->begin();
11194 pf != fields->end();
11195 ++pf, ++i)
11196 {
11197 if (!pf->is_anonymous())
11198 continue;
11199
11200 Type* ftype = pf->type();
11201 bool is_pointer = false;
11202 if (ftype->points_to() != NULL)
11203 {
11204 ftype = ftype->points_to();
11205 is_pointer = true;
11206 }
11207 Named_type* fnt = ftype->named_type();
11208 if (fnt == NULL)
11209 {
11210 // This is an error, but it will be diagnosed elsewhere.
11211 continue;
11212 }
11213
11214 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
11215 sub_field_indexes->next = field_indexes;
11216 sub_field_indexes->field_index = i;
11217
11218 Methods tmp_methods;
11219 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
11220 (is_embedded_pointer || is_pointer),
11221 (needs_stub_method
11222 || is_pointer
11223 || i > 0),
11224 seen,
11225 &tmp_methods);
11226 // Check if there are promoted methods that conflict with field names and
11227 // don't add them to the method map.
11228 for (Methods::const_iterator p = tmp_methods.begin();
11229 p != tmp_methods.end();
11230 ++p)
11231 {
11232 bool found = false;
11233 for (Struct_field_list::const_iterator fp = fields->begin();
11234 fp != fields->end();
11235 ++fp)
11236 {
11237 if (fp->field_name() == p->first)
11238 {
11239 found = true;
11240 break;
11241 }
11242 }
11243 if (!found &&
11244 !methods->insert(p->first, p->second))
11245 delete p->second;
11246 }
11247 }
11248 }
11249
11250 // If TYPE is an interface type, then add its method to *METHODS.
11251 // This is for interface methods attached to an anonymous field. The
11252 // parameters are as for add_methods_for_type.
11253
11254 void
11255 Type::add_interface_methods_for_type(const Type* type,
11256 const Method::Field_indexes* field_indexes,
11257 unsigned int depth,
11258 Methods* methods)
11259 {
11260 const Interface_type* it = type->interface_type();
11261 if (it == NULL)
11262 return;
11263
11264 const Typed_identifier_list* imethods = it->methods();
11265 if (imethods == NULL)
11266 return;
11267
11268 for (Typed_identifier_list::const_iterator pm = imethods->begin();
11269 pm != imethods->end();
11270 ++pm)
11271 {
11272 Function_type* fntype = pm->type()->function_type();
11273 if (fntype == NULL)
11274 {
11275 // This is an error, but it should be reported elsewhere
11276 // when we look at the methods for IT.
11277 continue;
11278 }
11279 go_assert(!fntype->is_method());
11280 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
11281 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
11282 field_indexes, depth);
11283 if (!methods->insert(pm->name(), m))
11284 delete m;
11285 }
11286 }
11287
11288 // Build stub methods for TYPE as needed. METHODS is the set of
11289 // methods for the type. A stub method may be needed when a type
11290 // inherits a method from an anonymous field. When we need the
11291 // address of the method, as in a type descriptor, we need to build a
11292 // little stub which does the required field dereferences and jumps to
11293 // the real method. LOCATION is the location of the type definition.
11294
11295 void
11296 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
11297 Location location)
11298 {
11299 if (methods == NULL)
11300 return;
11301 for (Methods::const_iterator p = methods->begin();
11302 p != methods->end();
11303 ++p)
11304 {
11305 Method* m = p->second;
11306 if (m->is_ambiguous() || !m->needs_stub_method())
11307 continue;
11308
11309 const std::string& name(p->first);
11310
11311 // Build a stub method.
11312
11313 const Function_type* fntype = m->type();
11314
11315 static unsigned int counter;
11316 char buf[100];
11317 snprintf(buf, sizeof buf, "$this%u", counter);
11318 ++counter;
11319
11320 Type* receiver_type = const_cast<Type*>(type);
11321 if (!m->is_value_method())
11322 receiver_type = Type::make_pointer_type(receiver_type);
11323 Location receiver_location = m->receiver_location();
11324 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
11325 receiver_location);
11326
11327 const Typed_identifier_list* fnparams = fntype->parameters();
11328 Typed_identifier_list* stub_params;
11329 if (fnparams == NULL || fnparams->empty())
11330 stub_params = NULL;
11331 else
11332 {
11333 // We give each stub parameter a unique name.
11334 stub_params = new Typed_identifier_list();
11335 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
11336 pp != fnparams->end();
11337 ++pp)
11338 {
11339 char pbuf[100];
11340 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
11341 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
11342 pp->location()));
11343 ++counter;
11344 }
11345 }
11346
11347 const Typed_identifier_list* fnresults = fntype->results();
11348 Typed_identifier_list* stub_results;
11349 if (fnresults == NULL || fnresults->empty())
11350 stub_results = NULL;
11351 else
11352 {
11353 // We create the result parameters without any names, since
11354 // we won't refer to them.
11355 stub_results = new Typed_identifier_list();
11356 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
11357 pr != fnresults->end();
11358 ++pr)
11359 stub_results->push_back(Typed_identifier("", pr->type(),
11360 pr->location()));
11361 }
11362
11363 Function_type* stub_type = Type::make_function_type(receiver,
11364 stub_params,
11365 stub_results,
11366 fntype->location());
11367 if (fntype->is_varargs())
11368 stub_type->set_is_varargs();
11369
11370 // We only create the function in the package which creates the
11371 // type.
11372 const Package* package;
11373 if (type->named_type() == NULL)
11374 package = NULL;
11375 else
11376 package = type->named_type()->named_object()->package();
11377 std::string stub_name = gogo->stub_method_name(package, name);
11378 Named_object* stub;
11379 if (package != NULL)
11380 stub = Named_object::make_function_declaration(stub_name, package,
11381 stub_type, location);
11382 else
11383 {
11384 stub = gogo->start_function(stub_name, stub_type, false,
11385 fntype->location());
11386 Type::build_one_stub_method(gogo, m, buf, stub_params,
11387 fntype->is_varargs(), location);
11388 gogo->finish_function(fntype->location());
11389
11390 if (type->named_type() == NULL && stub->is_function())
11391 stub->func_value()->set_is_unnamed_type_stub_method();
11392 if (m->nointerface() && stub->is_function())
11393 stub->func_value()->set_nointerface();
11394 }
11395
11396 m->set_stub_object(stub);
11397 }
11398 }
11399
11400 // Build a stub method which adjusts the receiver as required to call
11401 // METHOD. RECEIVER_NAME is the name we used for the receiver.
11402 // PARAMS is the list of function parameters.
11403
11404 void
11405 Type::build_one_stub_method(Gogo* gogo, Method* method,
11406 const char* receiver_name,
11407 const Typed_identifier_list* params,
11408 bool is_varargs,
11409 Location location)
11410 {
11411 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
11412 go_assert(receiver_object != NULL);
11413
11414 Expression* expr = Expression::make_var_reference(receiver_object, location);
11415 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
11416 if (expr->type()->points_to() == NULL)
11417 expr = Expression::make_unary(OPERATOR_AND, expr, location);
11418
11419 Expression_list* arguments;
11420 if (params == NULL || params->empty())
11421 arguments = NULL;
11422 else
11423 {
11424 arguments = new Expression_list();
11425 for (Typed_identifier_list::const_iterator p = params->begin();
11426 p != params->end();
11427 ++p)
11428 {
11429 Named_object* param = gogo->lookup(p->name(), NULL);
11430 go_assert(param != NULL);
11431 Expression* param_ref = Expression::make_var_reference(param,
11432 location);
11433 arguments->push_back(param_ref);
11434 }
11435 }
11436
11437 Expression* func = method->bind_method(expr, location);
11438 go_assert(func != NULL);
11439 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
11440 location);
11441
11442 gogo->add_statement(Statement::make_return_from_call(call, location));
11443 }
11444
11445 // Build direct interface stub methods for TYPE as needed. METHODS
11446 // is the set of methods for the type. LOCATION is the location of
11447 // the type definition.
11448 //
11449 // This is for an interface holding a pointer to the type and invoking
11450 // a value method. The interface data is the pointer, and is passed
11451 // to the stub, which dereferences it and passes to the actual method.
11452
11453 void
11454 Type::build_direct_iface_stub_methods(Gogo* gogo, const Type* type,
11455 Methods* methods, Location loc)
11456 {
11457 if (methods == NULL)
11458 return;
11459
11460 for (Methods::const_iterator p = methods->begin();
11461 p != methods->end();
11462 ++p)
11463 {
11464 Method* m = p->second;
11465 if (!m->is_value_method())
11466 continue;
11467
11468 Type* receiver_type = const_cast<Type*>(type);
11469 receiver_type = Type::make_pointer_type(receiver_type);
11470 const std::string& name(p->first);
11471 Function_type* fntype = m->type();
11472
11473 static unsigned int counter;
11474 char buf[100];
11475 snprintf(buf, sizeof buf, "$ptr%u", counter);
11476 ++counter;
11477 Typed_identifier* receiver =
11478 new Typed_identifier(buf, receiver_type, m->receiver_location());
11479
11480 const Typed_identifier_list* params = fntype->parameters();
11481 Typed_identifier_list* stub_params;
11482 if (params == NULL || params->empty())
11483 stub_params = NULL;
11484 else
11485 {
11486 // We give each stub parameter a unique name.
11487 stub_params = new Typed_identifier_list();
11488 for (Typed_identifier_list::const_iterator pp = params->begin();
11489 pp != params->end();
11490 ++pp)
11491 {
11492 char pbuf[100];
11493 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
11494 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
11495 pp->location()));
11496 ++counter;
11497 }
11498 }
11499
11500 const Typed_identifier_list* fnresults = fntype->results();
11501 Typed_identifier_list* stub_results;
11502 if (fnresults == NULL || fnresults->empty())
11503 stub_results = NULL;
11504 else
11505 {
11506 // We create the result parameters without any names, since
11507 // we won't refer to them.
11508 stub_results = new Typed_identifier_list();
11509 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
11510 pr != fnresults->end();
11511 ++pr)
11512 stub_results->push_back(Typed_identifier("", pr->type(),
11513 pr->location()));
11514 }
11515
11516 Function_type* stub_type = Type::make_function_type(receiver,
11517 stub_params,
11518 stub_results,
11519 fntype->location());
11520 if (fntype->is_varargs())
11521 stub_type->set_is_varargs();
11522
11523 // We only create the function in the package which creates the
11524 // type.
11525 const Package* package;
11526 if (type->named_type() == NULL)
11527 package = NULL;
11528 else
11529 package = type->named_type()->named_object()->package();
11530
11531 std::string stub_name = gogo->stub_method_name(package, name) + "2";
11532 Named_object* stub;
11533 if (package != NULL)
11534 stub = Named_object::make_function_declaration(stub_name, package,
11535 stub_type, loc);
11536 else
11537 {
11538 stub = gogo->start_function(stub_name, stub_type, false,
11539 fntype->location());
11540 Type::build_one_iface_stub_method(gogo, m, buf, stub_params,
11541 fntype->is_varargs(), loc);
11542 gogo->finish_function(fntype->location());
11543
11544 if (type->named_type() == NULL && stub->is_function())
11545 stub->func_value()->set_is_unnamed_type_stub_method();
11546 if (m->nointerface() && stub->is_function())
11547 stub->func_value()->set_nointerface();
11548 }
11549
11550 m->set_iface_stub_object(stub);
11551 }
11552 }
11553
11554 // Build a stub method for METHOD of direct interface type T.
11555 // RECEIVER_NAME is the name we used for the receiver.
11556 // PARAMS is the list of function parameters.
11557 //
11558 // The stub looks like
11559 //
11560 // func ($ptr *T, PARAMS) {
11561 // (*$ptr).METHOD(PARAMS)
11562 // }
11563
11564 void
11565 Type::build_one_iface_stub_method(Gogo* gogo, Method* method,
11566 const char* receiver_name,
11567 const Typed_identifier_list* params,
11568 bool is_varargs, Location loc)
11569 {
11570 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
11571 go_assert(receiver_object != NULL);
11572
11573 Expression* expr = Expression::make_var_reference(receiver_object, loc);
11574 expr = Expression::make_dereference(expr,
11575 Expression::NIL_CHECK_DEFAULT,
11576 loc);
11577
11578 Expression_list* arguments;
11579 if (params == NULL || params->empty())
11580 arguments = NULL;
11581 else
11582 {
11583 arguments = new Expression_list();
11584 for (Typed_identifier_list::const_iterator p = params->begin();
11585 p != params->end();
11586 ++p)
11587 {
11588 Named_object* param = gogo->lookup(p->name(), NULL);
11589 go_assert(param != NULL);
11590 Expression* param_ref = Expression::make_var_reference(param,
11591 loc);
11592 arguments->push_back(param_ref);
11593 }
11594 }
11595
11596 Expression* func = method->bind_method(expr, loc);
11597 go_assert(func != NULL);
11598 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
11599 loc);
11600
11601 gogo->add_statement(Statement::make_return_from_call(call, loc));
11602 }
11603
11604 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
11605 // in reverse order.
11606
11607 Expression*
11608 Type::apply_field_indexes(Expression* expr,
11609 const Method::Field_indexes* field_indexes,
11610 Location location)
11611 {
11612 if (field_indexes == NULL)
11613 return expr;
11614 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
11615 Struct_type* stype = expr->type()->deref()->struct_type();
11616 go_assert(stype != NULL
11617 && field_indexes->field_index < stype->field_count());
11618 if (expr->type()->struct_type() == NULL)
11619 {
11620 go_assert(expr->type()->points_to() != NULL);
11621 expr = Expression::make_dereference(expr, Expression::NIL_CHECK_DEFAULT,
11622 location);
11623 go_assert(expr->type()->struct_type() == stype);
11624 }
11625 return Expression::make_field_reference(expr, field_indexes->field_index,
11626 location);
11627 }
11628
11629 // Return whether NO is a method for which the receiver is a pointer.
11630
11631 bool
11632 Type::method_expects_pointer(const Named_object* no)
11633 {
11634 const Function_type *fntype;
11635 if (no->is_function())
11636 fntype = no->func_value()->type();
11637 else if (no->is_function_declaration())
11638 fntype = no->func_declaration_value()->type();
11639 else
11640 go_unreachable();
11641 return fntype->receiver()->type()->points_to() != NULL;
11642 }
11643
11644 // Given a set of methods for a type, METHODS, return the method NAME,
11645 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
11646 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
11647 // but is ambiguous (and return NULL).
11648
11649 Method*
11650 Type::method_function(const Methods* methods, const std::string& name,
11651 bool* is_ambiguous)
11652 {
11653 if (is_ambiguous != NULL)
11654 *is_ambiguous = false;
11655 if (methods == NULL)
11656 return NULL;
11657 Methods::const_iterator p = methods->find(name);
11658 if (p == methods->end())
11659 return NULL;
11660 Method* m = p->second;
11661 if (m->is_ambiguous())
11662 {
11663 if (is_ambiguous != NULL)
11664 *is_ambiguous = true;
11665 return NULL;
11666 }
11667 return m;
11668 }
11669
11670 // Return a pointer to the interface method table for TYPE for the
11671 // interface INTERFACE.
11672
11673 Expression*
11674 Type::interface_method_table(Type* type,
11675 Interface_type *interface,
11676 bool is_pointer,
11677 Interface_method_tables** method_tables,
11678 Interface_method_tables** pointer_tables)
11679 {
11680 go_assert(!interface->is_empty());
11681
11682 Interface_method_tables** pimt = is_pointer ? method_tables : pointer_tables;
11683
11684 if (*pimt == NULL)
11685 *pimt = new Interface_method_tables(5);
11686
11687 std::pair<Interface_type*, Expression*> val(interface, NULL);
11688 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
11689
11690 Location loc = Linemap::predeclared_location();
11691 if (ins.second)
11692 {
11693 // This is a new entry in the hash table.
11694 go_assert(ins.first->second == NULL);
11695 ins.first->second =
11696 Expression::make_interface_mtable_ref(interface, type, is_pointer, loc);
11697 }
11698 return Expression::make_unary(OPERATOR_AND, ins.first->second, loc);
11699 }
11700
11701 // Look for field or method NAME for TYPE. Return an Expression for
11702 // the field or method bound to EXPR. If there is no such field or
11703 // method, give an appropriate error and return an error expression.
11704
11705 Expression*
11706 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
11707 const std::string& name,
11708 Location location)
11709 {
11710 if (type->deref()->is_error_type())
11711 return Expression::make_error(location);
11712
11713 const Named_type* nt = type->deref()->named_type();
11714 const Struct_type* st = type->deref()->struct_type();
11715 const Interface_type* it = type->interface_type();
11716
11717 // If this is a pointer to a pointer, then it is possible that the
11718 // pointed-to type has methods.
11719 bool dereferenced = false;
11720 if (nt == NULL
11721 && st == NULL
11722 && it == NULL
11723 && type->points_to() != NULL
11724 && type->points_to()->points_to() != NULL)
11725 {
11726 expr = Expression::make_dereference(expr, Expression::NIL_CHECK_DEFAULT,
11727 location);
11728 type = type->points_to();
11729 if (type->deref()->is_error_type())
11730 return Expression::make_error(location);
11731 nt = type->points_to()->named_type();
11732 st = type->points_to()->struct_type();
11733 dereferenced = true;
11734 }
11735
11736 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
11737 || expr->is_addressable());
11738 std::vector<const Named_type*> seen;
11739 bool is_method = false;
11740 bool found_pointer_method = false;
11741 std::string ambig1;
11742 std::string ambig2;
11743 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
11744 &seen, NULL, &is_method,
11745 &found_pointer_method, &ambig1, &ambig2))
11746 {
11747 Expression* ret;
11748 if (!is_method)
11749 {
11750 go_assert(st != NULL);
11751 if (type->struct_type() == NULL)
11752 {
11753 if (dereferenced)
11754 {
11755 go_error_at(location, "pointer type has no field %qs",
11756 Gogo::message_name(name).c_str());
11757 return Expression::make_error(location);
11758 }
11759 go_assert(type->points_to() != NULL);
11760 expr = Expression::make_dereference(expr,
11761 Expression::NIL_CHECK_DEFAULT,
11762 location);
11763 go_assert(expr->type()->struct_type() == st);
11764 }
11765 ret = st->field_reference(expr, name, location);
11766 if (ret == NULL)
11767 {
11768 go_error_at(location, "type has no field %qs",
11769 Gogo::message_name(name).c_str());
11770 return Expression::make_error(location);
11771 }
11772 }
11773 else if (it != NULL && it->find_method(name) != NULL)
11774 ret = Expression::make_interface_field_reference(expr, name,
11775 location);
11776 else
11777 {
11778 Method* m;
11779 if (nt != NULL)
11780 m = nt->method_function(name, NULL);
11781 else if (st != NULL)
11782 m = st->method_function(name, NULL);
11783 else
11784 go_unreachable();
11785 go_assert(m != NULL);
11786 if (dereferenced)
11787 {
11788 go_error_at(location,
11789 "calling method %qs requires explicit dereference",
11790 Gogo::message_name(name).c_str());
11791 return Expression::make_error(location);
11792 }
11793 if (!m->is_value_method() && expr->type()->points_to() == NULL)
11794 expr = Expression::make_unary(OPERATOR_AND, expr, location);
11795 ret = m->bind_method(expr, location);
11796 }
11797 go_assert(ret != NULL);
11798 return ret;
11799 }
11800 else
11801 {
11802 if (Gogo::is_erroneous_name(name))
11803 {
11804 // An error was already reported.
11805 }
11806 else if (!ambig1.empty())
11807 go_error_at(location, "%qs is ambiguous via %qs and %qs",
11808 Gogo::message_name(name).c_str(), ambig1.c_str(),
11809 ambig2.c_str());
11810 else if (found_pointer_method)
11811 go_error_at(location, "method requires a pointer receiver");
11812 else if (nt == NULL && st == NULL && it == NULL)
11813 go_error_at(location,
11814 ("reference to field %qs in object which "
11815 "has no fields or methods"),
11816 Gogo::message_name(name).c_str());
11817 else
11818 {
11819 bool is_unexported;
11820 // The test for 'a' and 'z' is to handle builtin names,
11821 // which are not hidden.
11822 if (!Gogo::is_hidden_name(name) && (name[0] < 'a' || name[0] > 'z'))
11823 is_unexported = false;
11824 else
11825 {
11826 std::string unpacked = Gogo::unpack_hidden_name(name);
11827 seen.clear();
11828 is_unexported = Type::is_unexported_field_or_method(gogo, type,
11829 unpacked,
11830 &seen);
11831 }
11832 if (is_unexported)
11833 go_error_at(location, "reference to unexported field or method %qs",
11834 Gogo::message_name(name).c_str());
11835 else
11836 go_error_at(location, "reference to undefined field or method %qs",
11837 Gogo::message_name(name).c_str());
11838 }
11839 return Expression::make_error(location);
11840 }
11841 }
11842
11843 // Look in TYPE for a field or method named NAME, return true if one
11844 // is found. This looks through embedded anonymous fields and handles
11845 // ambiguity. If a method is found, sets *IS_METHOD to true;
11846 // otherwise, if a field is found, set it to false. If
11847 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
11848 // whose address can not be taken. SEEN is used to avoid infinite
11849 // recursion on invalid types.
11850
11851 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
11852 // method we couldn't use because it requires a pointer. LEVEL is
11853 // used for recursive calls, and can be NULL for a non-recursive call.
11854 // When this function returns false because it finds that the name is
11855 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
11856 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
11857 // will be unchanged.
11858
11859 // This function just returns whether or not there is a field or
11860 // method, and whether it is a field or method. It doesn't build an
11861 // expression to refer to it. If it is a method, we then look in the
11862 // list of all methods for the type. If it is a field, the search has
11863 // to be done again, looking only for fields, and building up the
11864 // expression as we go.
11865
11866 bool
11867 Type::find_field_or_method(const Type* type,
11868 const std::string& name,
11869 bool receiver_can_be_pointer,
11870 std::vector<const Named_type*>* seen,
11871 int* level,
11872 bool* is_method,
11873 bool* found_pointer_method,
11874 std::string* ambig1,
11875 std::string* ambig2)
11876 {
11877 // Named types can have locally defined methods.
11878 const Named_type* nt = type->unalias()->named_type();
11879 if (nt == NULL && type->points_to() != NULL)
11880 nt = type->points_to()->unalias()->named_type();
11881 if (nt != NULL)
11882 {
11883 Named_object* no = nt->find_local_method(name);
11884 if (no != NULL)
11885 {
11886 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
11887 {
11888 *is_method = true;
11889 return true;
11890 }
11891
11892 // Record that we have found a pointer method in order to
11893 // give a better error message if we don't find anything
11894 // else.
11895 *found_pointer_method = true;
11896 }
11897
11898 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
11899 p != seen->end();
11900 ++p)
11901 {
11902 if (*p == nt)
11903 {
11904 // We've already seen this type when searching for methods.
11905 return false;
11906 }
11907 }
11908 }
11909
11910 // Interface types can have methods.
11911 const Interface_type* it = type->interface_type();
11912 if (it != NULL && it->find_method(name) != NULL)
11913 {
11914 *is_method = true;
11915 return true;
11916 }
11917
11918 // Struct types can have fields. They can also inherit fields and
11919 // methods from anonymous fields.
11920 const Struct_type* st = type->deref()->struct_type();
11921 if (st == NULL)
11922 return false;
11923 const Struct_field_list* fields = st->fields();
11924 if (fields == NULL)
11925 return false;
11926
11927 if (nt != NULL)
11928 seen->push_back(nt);
11929
11930 int found_level = 0;
11931 bool found_is_method = false;
11932 std::string found_ambig1;
11933 std::string found_ambig2;
11934 const Struct_field* found_parent = NULL;
11935 for (Struct_field_list::const_iterator pf = fields->begin();
11936 pf != fields->end();
11937 ++pf)
11938 {
11939 if (pf->is_field_name(name))
11940 {
11941 *is_method = false;
11942 if (nt != NULL)
11943 seen->pop_back();
11944 return true;
11945 }
11946
11947 if (!pf->is_anonymous())
11948 continue;
11949
11950 if (pf->type()->deref()->is_error_type()
11951 || pf->type()->deref()->is_undefined())
11952 continue;
11953
11954 Named_type* fnt = pf->type()->named_type();
11955 if (fnt == NULL)
11956 fnt = pf->type()->deref()->named_type();
11957 go_assert(fnt != NULL);
11958
11959 // Methods with pointer receivers on embedded field are
11960 // inherited by the pointer to struct, and also by the struct
11961 // type if the field itself is a pointer.
11962 bool can_be_pointer = (receiver_can_be_pointer
11963 || pf->type()->points_to() != NULL);
11964 int sublevel = level == NULL ? 1 : *level + 1;
11965 bool sub_is_method;
11966 std::string subambig1;
11967 std::string subambig2;
11968 bool subfound = Type::find_field_or_method(fnt,
11969 name,
11970 can_be_pointer,
11971 seen,
11972 &sublevel,
11973 &sub_is_method,
11974 found_pointer_method,
11975 &subambig1,
11976 &subambig2);
11977 if (!subfound)
11978 {
11979 if (!subambig1.empty())
11980 {
11981 // The name was found via this field, but is ambiguous.
11982 // if the ambiguity is lower or at the same level as
11983 // anything else we have already found, then we want to
11984 // pass the ambiguity back to the caller.
11985 if (found_level == 0 || sublevel <= found_level)
11986 {
11987 found_ambig1 = (Gogo::message_name(pf->field_name())
11988 + '.' + subambig1);
11989 found_ambig2 = (Gogo::message_name(pf->field_name())
11990 + '.' + subambig2);
11991 found_level = sublevel;
11992 }
11993 }
11994 }
11995 else
11996 {
11997 // The name was found via this field. Use the level to see
11998 // if we want to use this one, or whether it introduces an
11999 // ambiguity.
12000 if (found_level == 0 || sublevel < found_level)
12001 {
12002 found_level = sublevel;
12003 found_is_method = sub_is_method;
12004 found_ambig1.clear();
12005 found_ambig2.clear();
12006 found_parent = &*pf;
12007 }
12008 else if (sublevel > found_level)
12009 ;
12010 else if (found_ambig1.empty())
12011 {
12012 // We found an ambiguity.
12013 go_assert(found_parent != NULL);
12014 found_ambig1 = Gogo::message_name(found_parent->field_name());
12015 found_ambig2 = Gogo::message_name(pf->field_name());
12016 }
12017 else
12018 {
12019 // We found an ambiguity, but we already know of one.
12020 // Just report the earlier one.
12021 }
12022 }
12023 }
12024
12025 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
12026 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
12027 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
12028 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
12029
12030 if (nt != NULL)
12031 seen->pop_back();
12032
12033 if (found_level == 0)
12034 return false;
12035 else if (found_is_method
12036 && type->named_type() != NULL
12037 && type->points_to() != NULL)
12038 {
12039 // If this is a method inherited from a struct field in a named pointer
12040 // type, it is invalid to automatically dereference the pointer to the
12041 // struct to find this method.
12042 if (level != NULL)
12043 *level = found_level;
12044 *is_method = true;
12045 return false;
12046 }
12047 else if (!found_ambig1.empty())
12048 {
12049 go_assert(!found_ambig1.empty());
12050 ambig1->assign(found_ambig1);
12051 ambig2->assign(found_ambig2);
12052 if (level != NULL)
12053 *level = found_level;
12054 return false;
12055 }
12056 else
12057 {
12058 if (level != NULL)
12059 *level = found_level;
12060 *is_method = found_is_method;
12061 return true;
12062 }
12063 }
12064
12065 // Return whether NAME is an unexported field or method for TYPE.
12066
12067 bool
12068 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
12069 const std::string& name,
12070 std::vector<const Named_type*>* seen)
12071 {
12072 const Named_type* nt = type->named_type();
12073 if (nt == NULL)
12074 nt = type->deref()->named_type();
12075 if (nt != NULL)
12076 {
12077 if (nt->is_unexported_local_method(gogo, name))
12078 return true;
12079
12080 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
12081 p != seen->end();
12082 ++p)
12083 {
12084 if (*p == nt)
12085 {
12086 // We've already seen this type.
12087 return false;
12088 }
12089 }
12090 }
12091
12092 const Interface_type* it = type->interface_type();
12093 if (it != NULL && it->is_unexported_method(gogo, name))
12094 return true;
12095
12096 type = type->deref();
12097
12098 const Struct_type* st = type->struct_type();
12099 if (st != NULL && st->is_unexported_local_field(gogo, name))
12100 return true;
12101
12102 if (st == NULL)
12103 return false;
12104
12105 const Struct_field_list* fields = st->fields();
12106 if (fields == NULL)
12107 return false;
12108
12109 if (nt != NULL)
12110 seen->push_back(nt);
12111
12112 for (Struct_field_list::const_iterator pf = fields->begin();
12113 pf != fields->end();
12114 ++pf)
12115 {
12116 if (pf->is_anonymous()
12117 && !pf->type()->deref()->is_error_type()
12118 && !pf->type()->deref()->is_undefined())
12119 {
12120 Named_type* subtype = pf->type()->named_type();
12121 if (subtype == NULL)
12122 subtype = pf->type()->deref()->named_type();
12123 if (subtype == NULL)
12124 {
12125 // This is an error, but it will be diagnosed elsewhere.
12126 continue;
12127 }
12128 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
12129 {
12130 if (nt != NULL)
12131 seen->pop_back();
12132 return true;
12133 }
12134 }
12135 }
12136
12137 if (nt != NULL)
12138 seen->pop_back();
12139
12140 return false;
12141 }
12142
12143 // Class Forward_declaration.
12144
12145 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
12146 : Type(TYPE_FORWARD),
12147 named_object_(named_object->resolve()), warned_(false)
12148 {
12149 go_assert(this->named_object_->is_unknown()
12150 || this->named_object_->is_type_declaration());
12151 }
12152
12153 // Return the named object.
12154
12155 Named_object*
12156 Forward_declaration_type::named_object()
12157 {
12158 return this->named_object_->resolve();
12159 }
12160
12161 const Named_object*
12162 Forward_declaration_type::named_object() const
12163 {
12164 return this->named_object_->resolve();
12165 }
12166
12167 // Return the name of the forward declared type.
12168
12169 const std::string&
12170 Forward_declaration_type::name() const
12171 {
12172 return this->named_object()->name();
12173 }
12174
12175 // Warn about a use of a type which has been declared but not defined.
12176
12177 void
12178 Forward_declaration_type::warn() const
12179 {
12180 Named_object* no = this->named_object_->resolve();
12181 if (no->is_unknown())
12182 {
12183 // The name was not defined anywhere.
12184 if (!this->warned_)
12185 {
12186 go_error_at(this->named_object_->location(),
12187 "use of undefined type %qs",
12188 no->message_name().c_str());
12189 this->warned_ = true;
12190 }
12191 }
12192 else if (no->is_type_declaration())
12193 {
12194 // The name was seen as a type, but the type was never defined.
12195 if (no->type_declaration_value()->using_type())
12196 {
12197 go_error_at(this->named_object_->location(),
12198 "use of undefined type %qs",
12199 no->message_name().c_str());
12200 this->warned_ = true;
12201 }
12202 }
12203 else
12204 {
12205 // The name was defined, but not as a type.
12206 if (!this->warned_)
12207 {
12208 go_error_at(this->named_object_->location(), "expected type");
12209 this->warned_ = true;
12210 }
12211 }
12212 }
12213
12214 // Get the base type of a declaration. This gives an error if the
12215 // type has not yet been defined.
12216
12217 Type*
12218 Forward_declaration_type::real_type()
12219 {
12220 if (this->is_defined())
12221 {
12222 Named_type* nt = this->named_object()->type_value();
12223 if (!nt->is_valid())
12224 return Type::make_error_type();
12225 return this->named_object()->type_value();
12226 }
12227 else
12228 {
12229 this->warn();
12230 return Type::make_error_type();
12231 }
12232 }
12233
12234 const Type*
12235 Forward_declaration_type::real_type() const
12236 {
12237 if (this->is_defined())
12238 {
12239 const Named_type* nt = this->named_object()->type_value();
12240 if (!nt->is_valid())
12241 return Type::make_error_type();
12242 return this->named_object()->type_value();
12243 }
12244 else
12245 {
12246 this->warn();
12247 return Type::make_error_type();
12248 }
12249 }
12250
12251 // Return whether the base type is defined.
12252
12253 bool
12254 Forward_declaration_type::is_defined() const
12255 {
12256 return this->named_object()->is_type();
12257 }
12258
12259 // Add a method. This is used when methods are defined before the
12260 // type.
12261
12262 Named_object*
12263 Forward_declaration_type::add_method(const std::string& name,
12264 Function* function)
12265 {
12266 Named_object* no = this->named_object();
12267 if (no->is_unknown())
12268 no->declare_as_type();
12269 return no->type_declaration_value()->add_method(name, function);
12270 }
12271
12272 // Add a method declaration. This is used when methods are declared
12273 // before the type.
12274
12275 Named_object*
12276 Forward_declaration_type::add_method_declaration(const std::string& name,
12277 Package* package,
12278 Function_type* type,
12279 Location location)
12280 {
12281 Named_object* no = this->named_object();
12282 if (no->is_unknown())
12283 no->declare_as_type();
12284 Type_declaration* td = no->type_declaration_value();
12285 return td->add_method_declaration(name, package, type, location);
12286 }
12287
12288 // Add an already created object as a method.
12289
12290 void
12291 Forward_declaration_type::add_existing_method(Named_object* nom)
12292 {
12293 Named_object* no = this->named_object();
12294 if (no->is_unknown())
12295 no->declare_as_type();
12296 no->type_declaration_value()->add_existing_method(nom);
12297 }
12298
12299 // Traversal.
12300
12301 int
12302 Forward_declaration_type::do_traverse(Traverse* traverse)
12303 {
12304 if (this->is_defined()
12305 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
12306 return TRAVERSE_EXIT;
12307 return TRAVERSE_CONTINUE;
12308 }
12309
12310 // Verify the type.
12311
12312 bool
12313 Forward_declaration_type::do_verify()
12314 {
12315 if (!this->is_defined() && !this->is_nil_constant_as_type())
12316 {
12317 this->warn();
12318 return false;
12319 }
12320 return true;
12321 }
12322
12323 // Get the backend representation for the type.
12324
12325 Btype*
12326 Forward_declaration_type::do_get_backend(Gogo* gogo)
12327 {
12328 if (this->is_defined())
12329 return Type::get_named_base_btype(gogo, this->real_type());
12330
12331 if (this->warned_)
12332 return gogo->backend()->error_type();
12333
12334 // We represent an undefined type as a struct with no fields. That
12335 // should work fine for the backend, since the same case can arise
12336 // in C.
12337 std::vector<Backend::Btyped_identifier> fields;
12338 Btype* bt = gogo->backend()->struct_type(fields);
12339 return gogo->backend()->named_type(this->name(), bt,
12340 this->named_object()->location());
12341 }
12342
12343 // Build a type descriptor for a forwarded type.
12344
12345 Expression*
12346 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
12347 {
12348 Location ploc = Linemap::predeclared_location();
12349 if (!this->is_defined())
12350 return Expression::make_error(ploc);
12351 else
12352 {
12353 Type* t = this->real_type();
12354 if (name != NULL)
12355 return this->named_type_descriptor(gogo, t, name);
12356 else
12357 return Expression::make_error(this->named_object_->location());
12358 }
12359 }
12360
12361 // The reflection string.
12362
12363 void
12364 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
12365 {
12366 this->append_reflection(this->real_type(), gogo, ret);
12367 }
12368
12369 // Export a forward declaration. This can happen when a defined type
12370 // refers to a type which is only declared (and is presumably defined
12371 // in some other file in the same package).
12372
12373 void
12374 Forward_declaration_type::do_export(Export*) const
12375 {
12376 // If there is a base type, that should be exported instead of this.
12377 go_assert(!this->is_defined());
12378
12379 // We don't output anything.
12380 }
12381
12382 // Make a forward declaration.
12383
12384 Type*
12385 Type::make_forward_declaration(Named_object* named_object)
12386 {
12387 return new Forward_declaration_type(named_object);
12388 }
12389
12390 // Class Typed_identifier_list.
12391
12392 // Sort the entries by name.
12393
12394 struct Typed_identifier_list_sort
12395 {
12396 public:
12397 bool
12398 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
12399 {
12400 return (Gogo::unpack_hidden_name(t1.name())
12401 < Gogo::unpack_hidden_name(t2.name()));
12402 }
12403 };
12404
12405 void
12406 Typed_identifier_list::sort_by_name()
12407 {
12408 std::sort(this->entries_.begin(), this->entries_.end(),
12409 Typed_identifier_list_sort());
12410 }
12411
12412 // Traverse types.
12413
12414 int
12415 Typed_identifier_list::traverse(Traverse* traverse) const
12416 {
12417 for (Typed_identifier_list::const_iterator p = this->begin();
12418 p != this->end();
12419 ++p)
12420 {
12421 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
12422 return TRAVERSE_EXIT;
12423 }
12424 return TRAVERSE_CONTINUE;
12425 }
12426
12427 // Copy the list.
12428
12429 Typed_identifier_list*
12430 Typed_identifier_list::copy() const
12431 {
12432 Typed_identifier_list* ret = new Typed_identifier_list();
12433 for (Typed_identifier_list::const_iterator p = this->begin();
12434 p != this->end();
12435 ++p)
12436 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
12437 return ret;
12438 }