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
Type(Type_classification classification)41 Type::Type(Type_classification classification)
42 : classification_(classification), btype_(NULL), type_descriptor_var_(NULL),
43 gc_symbol_var_(NULL)
44 {
45 }
46
~Type()47 Type::~Type()
48 {
49 }
50
51 // Get the base type for a type--skip names and forward declarations.
52
53 Type*
base()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*
base() const68 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*
forwarded()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*
forwarded() const97 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*
unalias()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*
unalias() const125 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*
named_type()140 Type::named_type()
141 {
142 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
143 }
144
145 const Named_type*
named_type() const146 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
is_undefined() const154 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
is_basic_type() const163 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
is_abstract() const198 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*
make_non_abstract_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
is_error_type() const248 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*
points_to() const267 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
is_slice_type() const277 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
is_nil_constant_as_type() const286 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
traverse(Type * type,Traverse * traverse)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
do_traverse(Traverse *)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
are_identical(const Type * t1,const Type * t2,int flags,std::string * reason)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
are_compatible_for_binop(const Type * lhs,const Type * rhs)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
are_compatible_for_comparison(bool is_equality_op,const Type * t1,const Type * t2,std::string * reason)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
are_assignable(const Type * lhs,const Type * rhs,std::string * reason)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
are_convertible(const Type * lhs,const Type * rhs,std::string * reason)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*
copy_expressions()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
hash_for_method(Gogo * gogo,int flags) const930 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
do_hash_for_method(Gogo *,int) const949 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*
get_backend(Gogo * gogo)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*
get_btype_without_hash(Gogo * gogo)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*
get_backend_placeholder(Gogo * gogo)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
finish_backend(Gogo * gogo,Btype * placeholder)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*
type_descriptor_pointer(Gogo * gogo,Location location)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
make_type_descriptor_var(Gogo * gogo)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
type_descriptor_defined_elsewhere(Named_type * nt,const Package ** package)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*
type_descriptor(Gogo * gogo,Type * type)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*
named_type_descriptor(Gogo * gogo,Type * type,Named_type * name)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*
make_builtin_struct_type(int nfields,...)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*
make_builtin_named_type(const char * name,Type * 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
convert_builtin_named_types(Gogo * gogo)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*
make_type_descriptor_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*
make_type_descriptor_ptr_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
memequal_align(Gogo * gogo,int size)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
needs_specific_type_functions(Gogo * gogo)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
type_functions(Gogo * gogo,Named_type * name,Function_type * hash_fntype,Function_type * equal_fntype,Named_object ** hash_fn,Named_object ** equal_fn)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
specific_type_functions(Gogo * gogo,Named_type * name,int64_t size,Function_type * hash_fntype,Function_type * equal_fntype,Named_object ** hash_fn,Named_object ** equal_fn)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
write_specific_type_functions(Gogo * gogo,Named_type * name,int64_t size,const std::string & hash_name,Function_type * hash_fntype,const std::string & equal_name,Function_type * equal_fntype)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
write_identity_hash(Gogo * gogo,int64_t size)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
write_identity_equal(Gogo * gogo,int64_t size)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
write_named_hash(Gogo * gogo,Named_type * name,Function_type * hash_fntype,Function_type * equal_fntype)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
write_named_equal(Gogo * gogo,Named_type * name)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 a composite literal for the type descriptor for a plain type
2317 // of kind RUNTIME_TYPE_KIND named NAME.
2318
2319 Expression*
type_descriptor_constructor(Gogo * gogo,int runtime_type_kind,Named_type * name,const Methods * methods,bool only_value_methods)2320 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
2321 Named_type* name, const Methods* methods,
2322 bool only_value_methods)
2323 {
2324 Location bloc = Linemap::predeclared_location();
2325
2326 Type* td_type = Type::make_type_descriptor_type();
2327 const Struct_field_list* fields = td_type->struct_type()->fields();
2328
2329 Expression_list* vals = new Expression_list();
2330 vals->reserve(12);
2331
2332 if (!this->has_pointer())
2333 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
2334 if (this->points_to() != NULL)
2335 runtime_type_kind |= RUNTIME_TYPE_KIND_DIRECT_IFACE;
2336 int64_t ptrsize;
2337 int64_t ptrdata;
2338 if (this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2339 runtime_type_kind |= RUNTIME_TYPE_KIND_GC_PROG;
2340
2341 Struct_field_list::const_iterator p = fields->begin();
2342 go_assert(p->is_field_name("size"));
2343 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
2344 vals->push_back(Expression::make_type_info(this, type_info));
2345
2346 ++p;
2347 go_assert(p->is_field_name("ptrdata"));
2348 type_info = Expression::TYPE_INFO_DESCRIPTOR_PTRDATA;
2349 vals->push_back(Expression::make_type_info(this, type_info));
2350
2351 ++p;
2352 go_assert(p->is_field_name("hash"));
2353 unsigned int h;
2354 if (name != NULL)
2355 h = name->hash_for_method(gogo, Type::COMPARE_TAGS);
2356 else
2357 h = this->hash_for_method(gogo, Type::COMPARE_TAGS);
2358 vals->push_back(Expression::make_integer_ul(h, p->type(), bloc));
2359
2360 ++p;
2361 go_assert(p->is_field_name("kind"));
2362 vals->push_back(Expression::make_integer_ul(runtime_type_kind, p->type(),
2363 bloc));
2364
2365 ++p;
2366 go_assert(p->is_field_name("align"));
2367 type_info = Expression::TYPE_INFO_ALIGNMENT;
2368 vals->push_back(Expression::make_type_info(this, type_info));
2369
2370 ++p;
2371 go_assert(p->is_field_name("fieldAlign"));
2372 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
2373 vals->push_back(Expression::make_type_info(this, type_info));
2374
2375 ++p;
2376 go_assert(p->is_field_name("hashfn"));
2377 Function_type* hash_fntype = p->type()->function_type();
2378
2379 ++p;
2380 go_assert(p->is_field_name("equalfn"));
2381 Function_type* equal_fntype = p->type()->function_type();
2382
2383 Named_object* hash_fn;
2384 Named_object* equal_fn;
2385 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
2386 &equal_fn);
2387 if (hash_fn == NULL)
2388 vals->push_back(Expression::make_cast(hash_fntype,
2389 Expression::make_nil(bloc),
2390 bloc));
2391 else
2392 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
2393 if (equal_fn == NULL)
2394 vals->push_back(Expression::make_cast(equal_fntype,
2395 Expression::make_nil(bloc),
2396 bloc));
2397 else
2398 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
2399
2400 ++p;
2401 go_assert(p->is_field_name("gcdata"));
2402 vals->push_back(Expression::make_gc_symbol(this));
2403
2404 ++p;
2405 go_assert(p->is_field_name("string"));
2406 Expression* s = Expression::make_string((name != NULL
2407 ? name->reflection(gogo)
2408 : this->reflection(gogo)),
2409 bloc);
2410 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2411
2412 ++p;
2413 go_assert(p->is_field_name("uncommonType"));
2414 if (name == NULL && methods == NULL)
2415 vals->push_back(Expression::make_nil(bloc));
2416 else
2417 {
2418 if (methods == NULL)
2419 methods = name->methods();
2420 vals->push_back(this->uncommon_type_constructor(gogo,
2421 p->type()->deref(),
2422 name, methods,
2423 only_value_methods));
2424 }
2425
2426 ++p;
2427 go_assert(p->is_field_name("ptrToThis"));
2428 if (name == NULL && methods == NULL)
2429 vals->push_back(Expression::make_nil(bloc));
2430 else
2431 {
2432 Type* pt;
2433 if (name != NULL)
2434 pt = Type::make_pointer_type(name);
2435 else
2436 pt = Type::make_pointer_type(this);
2437 vals->push_back(Expression::make_type_descriptor(pt, bloc));
2438 }
2439
2440 ++p;
2441 go_assert(p == fields->end());
2442
2443 return Expression::make_struct_composite_literal(td_type, vals, bloc);
2444 }
2445
2446 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2447 // length used by the gc toolchain, and also appears in
2448 // libgo/go/reflect/type.go.
2449
2450 static const int64_t max_ptrmask_bytes = 2048;
2451
2452 // Return a pointer to the Garbage Collection information for this type.
2453
2454 Bexpression*
gc_symbol_pointer(Gogo * gogo)2455 Type::gc_symbol_pointer(Gogo* gogo)
2456 {
2457 Type* t = this->unalias();
2458
2459 if (!t->has_pointer())
2460 return gogo->backend()->nil_pointer_expression();
2461
2462 if (t->gc_symbol_var_ == NULL)
2463 {
2464 t->make_gc_symbol_var(gogo);
2465 go_assert(t->gc_symbol_var_ != NULL);
2466 }
2467 Location bloc = Linemap::predeclared_location();
2468 Bexpression* var_expr =
2469 gogo->backend()->var_expression(t->gc_symbol_var_, bloc);
2470 Bexpression* addr_expr =
2471 gogo->backend()->address_expression(var_expr, bloc);
2472
2473 Type* uint8_type = Type::lookup_integer_type("uint8");
2474 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
2475 Btype* ubtype = pointer_uint8_type->get_backend(gogo);
2476 return gogo->backend()->convert_expression(ubtype, addr_expr, bloc);
2477 }
2478
2479 // A mapping from unnamed types to GC symbol variables.
2480
2481 Type::GC_symbol_vars Type::gc_symbol_vars;
2482
2483 // Build the GC symbol for this type.
2484
2485 void
make_gc_symbol_var(Gogo * gogo)2486 Type::make_gc_symbol_var(Gogo* gogo)
2487 {
2488 go_assert(this->gc_symbol_var_ == NULL);
2489
2490 Named_type* nt = this->named_type();
2491
2492 // We can have multiple instances of unnamed types and similar to type
2493 // descriptors, we only want to the emit the GC data once, so we use a
2494 // hash table.
2495 Bvariable** phash = NULL;
2496 if (nt == NULL)
2497 {
2498 Bvariable* bvnull = NULL;
2499 std::pair<GC_symbol_vars::iterator, bool> ins =
2500 Type::gc_symbol_vars.insert(std::make_pair(this, bvnull));
2501 if (!ins.second)
2502 {
2503 // We've already built a gc symbol for this type.
2504 this->gc_symbol_var_ = ins.first->second;
2505 return;
2506 }
2507 phash = &ins.first->second;
2508 }
2509
2510 int64_t ptrsize;
2511 int64_t ptrdata;
2512 if (!this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2513 {
2514 this->gc_symbol_var_ = this->gc_ptrmask_var(gogo, ptrsize, ptrdata);
2515 if (phash != NULL)
2516 *phash = this->gc_symbol_var_;
2517 return;
2518 }
2519
2520 std::string sym_name = gogo->gc_symbol_name(this);
2521
2522 // Build the contents of the gc symbol.
2523 Expression* sym_init = this->gcprog_constructor(gogo, ptrsize, ptrdata);
2524 Btype* sym_btype = sym_init->type()->get_backend(gogo);
2525
2526 // If the type descriptor for this type is defined somewhere else, so is the
2527 // GC symbol.
2528 const Package* dummy;
2529 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
2530 {
2531 std::string asm_name(go_selectively_encode_id(sym_name));
2532 this->gc_symbol_var_ =
2533 gogo->backend()->implicit_variable_reference(sym_name, asm_name,
2534 sym_btype);
2535 if (phash != NULL)
2536 *phash = this->gc_symbol_var_;
2537 return;
2538 }
2539
2540 // See if this gc symbol can appear in multiple packages.
2541 bool is_common = false;
2542 if (nt != NULL)
2543 {
2544 // We create the symbol for a builtin type whenever we need
2545 // it.
2546 is_common = nt->is_builtin();
2547 }
2548 else
2549 {
2550 // This is an unnamed type. The descriptor could be defined in
2551 // any package where it is needed, and the linker will pick one
2552 // descriptor to keep.
2553 is_common = true;
2554 }
2555
2556 // Since we are building the GC symbol in this package, we must create the
2557 // variable before converting the initializer to its backend representation
2558 // because the initializer may refer to the GC symbol for this type.
2559 std::string asm_name(go_selectively_encode_id(sym_name));
2560 this->gc_symbol_var_ =
2561 gogo->backend()->implicit_variable(sym_name, asm_name,
2562 sym_btype, false, true, is_common, 0);
2563 if (phash != NULL)
2564 *phash = this->gc_symbol_var_;
2565
2566 Translate_context context(gogo, NULL, NULL, NULL);
2567 context.set_is_const();
2568 Bexpression* sym_binit = sym_init->get_backend(&context);
2569 gogo->backend()->implicit_variable_set_init(this->gc_symbol_var_, sym_name,
2570 sym_btype, false, true, is_common,
2571 sym_binit);
2572 }
2573
2574 // Return whether this type needs a GC program, and set *PTRDATA to
2575 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2576 // pointer.
2577
2578 bool
needs_gcprog(Gogo * gogo,int64_t * ptrsize,int64_t * ptrdata)2579 Type::needs_gcprog(Gogo* gogo, int64_t* ptrsize, int64_t* ptrdata)
2580 {
2581 Type* voidptr = Type::make_pointer_type(Type::make_void_type());
2582 if (!voidptr->backend_type_size(gogo, ptrsize))
2583 go_unreachable();
2584
2585 if (!this->backend_type_ptrdata(gogo, ptrdata))
2586 {
2587 go_assert(saw_errors());
2588 return false;
2589 }
2590
2591 return *ptrdata / *ptrsize > max_ptrmask_bytes;
2592 }
2593
2594 // A simple class used to build a GC ptrmask for a type.
2595
2596 class Ptrmask
2597 {
2598 public:
Ptrmask(size_t count)2599 Ptrmask(size_t count)
2600 : bits_((count + 7) / 8, 0)
2601 {}
2602
2603 void
2604 set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
2605
2606 std::string
2607 symname() const;
2608
2609 Expression*
2610 constructor(Gogo* gogo) const;
2611
2612 private:
2613 void
set(size_t index)2614 set(size_t index)
2615 { this->bits_.at(index / 8) |= 1 << (index % 8); }
2616
2617 // The actual bits.
2618 std::vector<unsigned char> bits_;
2619 };
2620
2621 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2622 // counts in bytes. PTRSIZE is the size of a pointer on the target
2623 // system.
2624
2625 void
set_from(Gogo * gogo,Type * type,int64_t ptrsize,int64_t offset)2626 Ptrmask::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
2627 {
2628 switch (type->base()->classification())
2629 {
2630 default:
2631 case Type::TYPE_NIL:
2632 case Type::TYPE_CALL_MULTIPLE_RESULT:
2633 case Type::TYPE_NAMED:
2634 case Type::TYPE_FORWARD:
2635 go_unreachable();
2636
2637 case Type::TYPE_ERROR:
2638 case Type::TYPE_VOID:
2639 case Type::TYPE_BOOLEAN:
2640 case Type::TYPE_INTEGER:
2641 case Type::TYPE_FLOAT:
2642 case Type::TYPE_COMPLEX:
2643 case Type::TYPE_SINK:
2644 break;
2645
2646 case Type::TYPE_FUNCTION:
2647 case Type::TYPE_POINTER:
2648 case Type::TYPE_MAP:
2649 case Type::TYPE_CHANNEL:
2650 // These types are all a single pointer.
2651 go_assert((offset % ptrsize) == 0);
2652 this->set(offset / ptrsize);
2653 break;
2654
2655 case Type::TYPE_STRING:
2656 // A string starts with a single pointer.
2657 go_assert((offset % ptrsize) == 0);
2658 this->set(offset / ptrsize);
2659 break;
2660
2661 case Type::TYPE_INTERFACE:
2662 // An interface is two pointers.
2663 go_assert((offset % ptrsize) == 0);
2664 this->set(offset / ptrsize);
2665 this->set((offset / ptrsize) + 1);
2666 break;
2667
2668 case Type::TYPE_STRUCT:
2669 {
2670 if (!type->has_pointer())
2671 return;
2672
2673 const Struct_field_list* fields = type->struct_type()->fields();
2674 int64_t soffset = 0;
2675 for (Struct_field_list::const_iterator pf = fields->begin();
2676 pf != fields->end();
2677 ++pf)
2678 {
2679 int64_t field_align;
2680 if (!pf->type()->backend_type_field_align(gogo, &field_align))
2681 {
2682 go_assert(saw_errors());
2683 return;
2684 }
2685 soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
2686
2687 this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
2688
2689 int64_t field_size;
2690 if (!pf->type()->backend_type_size(gogo, &field_size))
2691 {
2692 go_assert(saw_errors());
2693 return;
2694 }
2695 soffset += field_size;
2696 }
2697 }
2698 break;
2699
2700 case Type::TYPE_ARRAY:
2701 if (type->is_slice_type())
2702 {
2703 // A slice starts with a single pointer.
2704 go_assert((offset % ptrsize) == 0);
2705 this->set(offset / ptrsize);
2706 break;
2707 }
2708 else
2709 {
2710 if (!type->has_pointer())
2711 return;
2712
2713 int64_t len;
2714 if (!type->array_type()->int_length(&len))
2715 {
2716 go_assert(saw_errors());
2717 return;
2718 }
2719
2720 Type* element_type = type->array_type()->element_type();
2721 int64_t ele_size;
2722 if (!element_type->backend_type_size(gogo, &ele_size))
2723 {
2724 go_assert(saw_errors());
2725 return;
2726 }
2727
2728 int64_t eoffset = 0;
2729 for (int64_t i = 0; i < len; i++, eoffset += ele_size)
2730 this->set_from(gogo, element_type, ptrsize, offset + eoffset);
2731 break;
2732 }
2733 }
2734 }
2735
2736 // Return a symbol name for this ptrmask. This is used to coalesce
2737 // identical ptrmasks, which are common. The symbol name must use
2738 // only characters that are valid in symbols. It's nice if it's
2739 // short. We convert it to a string that uses only 32 characters,
2740 // avoiding digits and u and U.
2741
2742 std::string
symname() const2743 Ptrmask::symname() const
2744 {
2745 const char chars[33] = "abcdefghijklmnopqrstvwxyzABCDEFG";
2746 go_assert(chars[32] == '\0');
2747 std::string ret;
2748 unsigned int b = 0;
2749 int remaining = 0;
2750 for (std::vector<unsigned char>::const_iterator p = this->bits_.begin();
2751 p != this->bits_.end();
2752 ++p)
2753 {
2754 b |= *p << remaining;
2755 remaining += 8;
2756 while (remaining >= 5)
2757 {
2758 ret += chars[b & 0x1f];
2759 b >>= 5;
2760 remaining -= 5;
2761 }
2762 }
2763 while (remaining > 0)
2764 {
2765 ret += chars[b & 0x1f];
2766 b >>= 5;
2767 remaining -= 5;
2768 }
2769 return ret;
2770 }
2771
2772 // Return a constructor for this ptrmask. This will be used to
2773 // initialize the runtime ptrmask value.
2774
2775 Expression*
constructor(Gogo * gogo) const2776 Ptrmask::constructor(Gogo* gogo) const
2777 {
2778 Location bloc = Linemap::predeclared_location();
2779 Type* byte_type = gogo->lookup_global("byte")->type_value();
2780 Expression* len = Expression::make_integer_ul(this->bits_.size(), NULL,
2781 bloc);
2782 Array_type* at = Type::make_array_type(byte_type, len);
2783 Expression_list* vals = new Expression_list();
2784 vals->reserve(this->bits_.size());
2785 for (std::vector<unsigned char>::const_iterator p = this->bits_.begin();
2786 p != this->bits_.end();
2787 ++p)
2788 vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
2789 return Expression::make_array_composite_literal(at, vals, bloc);
2790 }
2791
2792 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2793 Type::GC_gcbits_vars Type::gc_gcbits_vars;
2794
2795 // Return a ptrmask variable for a type. For a type descriptor this
2796 // is only used for variables that are small enough to not need a
2797 // gcprog, but for a global variable this is used for a variable of
2798 // any size. PTRDATA is the number of bytes of the type that contain
2799 // pointer data. PTRSIZE is the size of a pointer on the target
2800 // system.
2801
2802 Bvariable*
gc_ptrmask_var(Gogo * gogo,int64_t ptrsize,int64_t ptrdata)2803 Type::gc_ptrmask_var(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
2804 {
2805 Ptrmask ptrmask(ptrdata / ptrsize);
2806 if (ptrdata >= ptrsize)
2807 ptrmask.set_from(gogo, this, ptrsize, 0);
2808 else
2809 {
2810 // This can happen in error cases. Just build an empty gcbits.
2811 go_assert(saw_errors());
2812 }
2813
2814 std::string sym_name = gogo->ptrmask_symbol_name(ptrmask.symname());
2815 Bvariable* bvnull = NULL;
2816 std::pair<GC_gcbits_vars::iterator, bool> ins =
2817 Type::gc_gcbits_vars.insert(std::make_pair(sym_name, bvnull));
2818 if (!ins.second)
2819 {
2820 // We've already built a GC symbol for this set of gcbits.
2821 return ins.first->second;
2822 }
2823
2824 Expression* val = ptrmask.constructor(gogo);
2825 Translate_context context(gogo, NULL, NULL, NULL);
2826 context.set_is_const();
2827 Bexpression* bval = val->get_backend(&context);
2828
2829 std::string asm_name(go_selectively_encode_id(sym_name));
2830 Btype *btype = val->type()->get_backend(gogo);
2831 Bvariable* ret = gogo->backend()->implicit_variable(sym_name, asm_name,
2832 btype, false, true,
2833 true, 0);
2834 gogo->backend()->implicit_variable_set_init(ret, sym_name, btype, false,
2835 true, true, bval);
2836 ins.first->second = ret;
2837 return ret;
2838 }
2839
2840 // A GCProg is used to build a program for the garbage collector.
2841 // This is used for types with a lot of pointer data, to reduce the
2842 // size of the data in the compiled program. The program is expanded
2843 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2844
2845 class GCProg
2846 {
2847 public:
GCProg()2848 GCProg()
2849 : bytes_(), index_(0), nb_(0)
2850 {}
2851
2852 // The number of bits described so far.
2853 int64_t
bit_index() const2854 bit_index() const
2855 { return this->index_; }
2856
2857 void
2858 set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
2859
2860 void
2861 end();
2862
2863 Expression*
2864 constructor(Gogo* gogo) const;
2865
2866 private:
2867 void
2868 ptr(int64_t);
2869
2870 bool
2871 should_repeat(int64_t, int64_t);
2872
2873 void
2874 repeat(int64_t, int64_t);
2875
2876 void
2877 zero_until(int64_t);
2878
2879 void
2880 lit(unsigned char);
2881
2882 void
2883 varint(int64_t);
2884
2885 void
2886 flushlit();
2887
2888 // Add a byte to the program.
2889 void
byte(unsigned char x)2890 byte(unsigned char x)
2891 { this->bytes_.push_back(x); }
2892
2893 // The maximum number of bytes of literal bits.
2894 static const int max_literal = 127;
2895
2896 // The program.
2897 std::vector<unsigned char> bytes_;
2898 // The index of the last bit described.
2899 int64_t index_;
2900 // The current set of literal bits.
2901 unsigned char b_[max_literal];
2902 // The current number of literal bits.
2903 int nb_;
2904 };
2905
2906 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2907 // counts in bytes. PTRSIZE is the size of a pointer on the target
2908 // system.
2909
2910 void
set_from(Gogo * gogo,Type * type,int64_t ptrsize,int64_t offset)2911 GCProg::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
2912 {
2913 switch (type->base()->classification())
2914 {
2915 default:
2916 case Type::TYPE_NIL:
2917 case Type::TYPE_CALL_MULTIPLE_RESULT:
2918 case Type::TYPE_NAMED:
2919 case Type::TYPE_FORWARD:
2920 go_unreachable();
2921
2922 case Type::TYPE_ERROR:
2923 case Type::TYPE_VOID:
2924 case Type::TYPE_BOOLEAN:
2925 case Type::TYPE_INTEGER:
2926 case Type::TYPE_FLOAT:
2927 case Type::TYPE_COMPLEX:
2928 case Type::TYPE_SINK:
2929 break;
2930
2931 case Type::TYPE_FUNCTION:
2932 case Type::TYPE_POINTER:
2933 case Type::TYPE_MAP:
2934 case Type::TYPE_CHANNEL:
2935 // These types are all a single pointer.
2936 go_assert((offset % ptrsize) == 0);
2937 this->ptr(offset / ptrsize);
2938 break;
2939
2940 case Type::TYPE_STRING:
2941 // A string starts with a single pointer.
2942 go_assert((offset % ptrsize) == 0);
2943 this->ptr(offset / ptrsize);
2944 break;
2945
2946 case Type::TYPE_INTERFACE:
2947 // An interface is two pointers.
2948 go_assert((offset % ptrsize) == 0);
2949 this->ptr(offset / ptrsize);
2950 this->ptr((offset / ptrsize) + 1);
2951 break;
2952
2953 case Type::TYPE_STRUCT:
2954 {
2955 if (!type->has_pointer())
2956 return;
2957
2958 const Struct_field_list* fields = type->struct_type()->fields();
2959 int64_t soffset = 0;
2960 for (Struct_field_list::const_iterator pf = fields->begin();
2961 pf != fields->end();
2962 ++pf)
2963 {
2964 int64_t field_align;
2965 if (!pf->type()->backend_type_field_align(gogo, &field_align))
2966 {
2967 go_assert(saw_errors());
2968 return;
2969 }
2970 soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
2971
2972 this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
2973
2974 int64_t field_size;
2975 if (!pf->type()->backend_type_size(gogo, &field_size))
2976 {
2977 go_assert(saw_errors());
2978 return;
2979 }
2980 soffset += field_size;
2981 }
2982 }
2983 break;
2984
2985 case Type::TYPE_ARRAY:
2986 if (type->is_slice_type())
2987 {
2988 // A slice starts with a single pointer.
2989 go_assert((offset % ptrsize) == 0);
2990 this->ptr(offset / ptrsize);
2991 break;
2992 }
2993 else
2994 {
2995 if (!type->has_pointer())
2996 return;
2997
2998 int64_t len;
2999 if (!type->array_type()->int_length(&len))
3000 {
3001 go_assert(saw_errors());
3002 return;
3003 }
3004
3005 Type* element_type = type->array_type()->element_type();
3006
3007 // Flatten array of array to a big array by multiplying counts.
3008 while (element_type->array_type() != NULL
3009 && !element_type->is_slice_type())
3010 {
3011 int64_t ele_len;
3012 if (!element_type->array_type()->int_length(&ele_len))
3013 {
3014 go_assert(saw_errors());
3015 return;
3016 }
3017
3018 len *= ele_len;
3019 element_type = element_type->array_type()->element_type();
3020 }
3021
3022 int64_t ele_size;
3023 if (!element_type->backend_type_size(gogo, &ele_size))
3024 {
3025 go_assert(saw_errors());
3026 return;
3027 }
3028
3029 go_assert(len > 0 && ele_size > 0);
3030
3031 if (!this->should_repeat(ele_size / ptrsize, len))
3032 {
3033 // Cheaper to just emit the bits.
3034 int64_t eoffset = 0;
3035 for (int64_t i = 0; i < len; i++, eoffset += ele_size)
3036 this->set_from(gogo, element_type, ptrsize, offset + eoffset);
3037 }
3038 else
3039 {
3040 go_assert((offset % ptrsize) == 0);
3041 go_assert((ele_size % ptrsize) == 0);
3042 this->set_from(gogo, element_type, ptrsize, offset);
3043 this->zero_until((offset + ele_size) / ptrsize);
3044 this->repeat(ele_size / ptrsize, len - 1);
3045 }
3046
3047 break;
3048 }
3049 }
3050 }
3051
3052 // Emit a 1 into the bit stream of a GC program at the given bit index.
3053
3054 void
ptr(int64_t index)3055 GCProg::ptr(int64_t index)
3056 {
3057 go_assert(index >= this->index_);
3058 this->zero_until(index);
3059 this->lit(1);
3060 }
3061
3062 // Return whether it is worthwhile to use a repeat to describe c
3063 // elements of n bits each, compared to just emitting c copies of the
3064 // n-bit description.
3065
3066 bool
should_repeat(int64_t n,int64_t c)3067 GCProg::should_repeat(int64_t n, int64_t c)
3068 {
3069 // Repeat if there is more than 1 item and if the total data doesn't
3070 // fit into four bytes.
3071 return c > 1 && c * n > 4 * 8;
3072 }
3073
3074 // Emit an instruction to repeat the description of the last n words c
3075 // times (including the initial description, so c + 1 times in total).
3076
3077 void
repeat(int64_t n,int64_t c)3078 GCProg::repeat(int64_t n, int64_t c)
3079 {
3080 if (n == 0 || c == 0)
3081 return;
3082 this->flushlit();
3083 if (n < 128)
3084 this->byte(0x80 | static_cast<unsigned char>(n & 0x7f));
3085 else
3086 {
3087 this->byte(0x80);
3088 this->varint(n);
3089 }
3090 this->varint(c);
3091 this->index_ += n * c;
3092 }
3093
3094 // Add zeros to the bit stream up to the given index.
3095
3096 void
zero_until(int64_t index)3097 GCProg::zero_until(int64_t index)
3098 {
3099 go_assert(index >= this->index_);
3100 int64_t skip = index - this->index_;
3101 if (skip == 0)
3102 return;
3103 if (skip < 4 * 8)
3104 {
3105 for (int64_t i = 0; i < skip; ++i)
3106 this->lit(0);
3107 return;
3108 }
3109 this->lit(0);
3110 this->flushlit();
3111 this->repeat(1, skip - 1);
3112 }
3113
3114 // Add a single literal bit to the program.
3115
3116 void
lit(unsigned char x)3117 GCProg::lit(unsigned char x)
3118 {
3119 if (this->nb_ == GCProg::max_literal)
3120 this->flushlit();
3121 this->b_[this->nb_] = x;
3122 ++this->nb_;
3123 ++this->index_;
3124 }
3125
3126 // Emit the varint encoding of x.
3127
3128 void
varint(int64_t x)3129 GCProg::varint(int64_t x)
3130 {
3131 go_assert(x >= 0);
3132 while (x >= 0x80)
3133 {
3134 this->byte(0x80 | static_cast<unsigned char>(x & 0x7f));
3135 x >>= 7;
3136 }
3137 this->byte(static_cast<unsigned char>(x & 0x7f));
3138 }
3139
3140 // Flush any pending literal bits.
3141
3142 void
flushlit()3143 GCProg::flushlit()
3144 {
3145 if (this->nb_ == 0)
3146 return;
3147 this->byte(static_cast<unsigned char>(this->nb_));
3148 unsigned char bits = 0;
3149 for (int i = 0; i < this->nb_; ++i)
3150 {
3151 bits |= this->b_[i] << (i % 8);
3152 if ((i + 1) % 8 == 0)
3153 {
3154 this->byte(bits);
3155 bits = 0;
3156 }
3157 }
3158 if (this->nb_ % 8 != 0)
3159 this->byte(bits);
3160 this->nb_ = 0;
3161 }
3162
3163 // Mark the end of a GC program.
3164
3165 void
end()3166 GCProg::end()
3167 {
3168 this->flushlit();
3169 this->byte(0);
3170 }
3171
3172 // Return an Expression for the bytes in a GC program.
3173
3174 Expression*
constructor(Gogo * gogo) const3175 GCProg::constructor(Gogo* gogo) const
3176 {
3177 Location bloc = Linemap::predeclared_location();
3178
3179 // The first four bytes are the length of the program in target byte
3180 // order. Build a struct whose first type is uint32 to make this
3181 // work.
3182
3183 Type* uint32_type = Type::lookup_integer_type("uint32");
3184
3185 Type* byte_type = gogo->lookup_global("byte")->type_value();
3186 Expression* len = Expression::make_integer_ul(this->bytes_.size(), NULL,
3187 bloc);
3188 Array_type* at = Type::make_array_type(byte_type, len);
3189
3190 Struct_type* st = Type::make_builtin_struct_type(2, "len", uint32_type,
3191 "bytes", at);
3192
3193 Expression_list* vals = new Expression_list();
3194 vals->reserve(this->bytes_.size());
3195 for (std::vector<unsigned char>::const_iterator p = this->bytes_.begin();
3196 p != this->bytes_.end();
3197 ++p)
3198 vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
3199 Expression* bytes = Expression::make_array_composite_literal(at, vals, bloc);
3200
3201 vals = new Expression_list();
3202 vals->push_back(Expression::make_integer_ul(this->bytes_.size(), uint32_type,
3203 bloc));
3204 vals->push_back(bytes);
3205
3206 return Expression::make_struct_composite_literal(st, vals, bloc);
3207 }
3208
3209 // Return a composite literal for the garbage collection program for
3210 // this type. This is only used for types that are too large to use a
3211 // ptrmask.
3212
3213 Expression*
gcprog_constructor(Gogo * gogo,int64_t ptrsize,int64_t ptrdata)3214 Type::gcprog_constructor(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
3215 {
3216 Location bloc = Linemap::predeclared_location();
3217
3218 GCProg prog;
3219 prog.set_from(gogo, this, ptrsize, 0);
3220 int64_t offset = prog.bit_index() * ptrsize;
3221 prog.end();
3222
3223 int64_t type_size;
3224 if (!this->backend_type_size(gogo, &type_size))
3225 {
3226 go_assert(saw_errors());
3227 return Expression::make_error(bloc);
3228 }
3229
3230 go_assert(offset >= ptrdata && offset <= type_size);
3231
3232 return prog.constructor(gogo);
3233 }
3234
3235 // Return a composite literal for the uncommon type information for
3236 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3237 // struct. If name is not NULL, it is the name of the type. If
3238 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3239 // is true if only value methods should be included. At least one of
3240 // NAME and METHODS must not be NULL.
3241
3242 Expression*
uncommon_type_constructor(Gogo * gogo,Type * uncommon_type,Named_type * name,const Methods * methods,bool only_value_methods) const3243 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
3244 Named_type* name, const Methods* methods,
3245 bool only_value_methods) const
3246 {
3247 Location bloc = Linemap::predeclared_location();
3248
3249 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
3250
3251 Expression_list* vals = new Expression_list();
3252 vals->reserve(3);
3253
3254 Struct_field_list::const_iterator p = fields->begin();
3255 go_assert(p->is_field_name("name"));
3256
3257 ++p;
3258 go_assert(p->is_field_name("pkgPath"));
3259
3260 if (name == NULL)
3261 {
3262 vals->push_back(Expression::make_nil(bloc));
3263 vals->push_back(Expression::make_nil(bloc));
3264 }
3265 else
3266 {
3267 Named_object* no = name->named_object();
3268 std::string n = Gogo::unpack_hidden_name(no->name());
3269 Expression* s = Expression::make_string(n, bloc);
3270 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3271
3272 if (name->is_builtin())
3273 vals->push_back(Expression::make_nil(bloc));
3274 else
3275 {
3276 const Package* package = no->package();
3277 const std::string& pkgpath(package == NULL
3278 ? gogo->pkgpath()
3279 : package->pkgpath());
3280 s = Expression::make_string(pkgpath, bloc);
3281 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3282 }
3283 }
3284
3285 ++p;
3286 go_assert(p->is_field_name("methods"));
3287 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
3288 only_value_methods));
3289
3290 ++p;
3291 go_assert(p == fields->end());
3292
3293 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
3294 vals, bloc);
3295 return Expression::make_unary(OPERATOR_AND, r, bloc);
3296 }
3297
3298 // Sort methods by name.
3299
3300 class Sort_methods
3301 {
3302 public:
3303 bool
operator ()(const std::pair<std::string,const Method * > & m1,const std::pair<std::string,const Method * > & m2) const3304 operator()(const std::pair<std::string, const Method*>& m1,
3305 const std::pair<std::string, const Method*>& m2) const
3306 {
3307 return (Gogo::unpack_hidden_name(m1.first)
3308 < Gogo::unpack_hidden_name(m2.first));
3309 }
3310 };
3311
3312 // Return a composite literal for the type method table for this type.
3313 // METHODS_TYPE is the type of the table, and is a slice type.
3314 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3315 // then only value methods are used.
3316
3317 Expression*
methods_constructor(Gogo * gogo,Type * methods_type,const Methods * methods,bool only_value_methods) const3318 Type::methods_constructor(Gogo* gogo, Type* methods_type,
3319 const Methods* methods,
3320 bool only_value_methods) const
3321 {
3322 Location bloc = Linemap::predeclared_location();
3323
3324 std::vector<std::pair<std::string, const Method*> > smethods;
3325 if (methods != NULL)
3326 {
3327 smethods.reserve(methods->count());
3328 for (Methods::const_iterator p = methods->begin();
3329 p != methods->end();
3330 ++p)
3331 {
3332 if (p->second->is_ambiguous())
3333 continue;
3334 if (only_value_methods && !p->second->is_value_method())
3335 continue;
3336
3337 // This is where we implement the magic //go:nointerface
3338 // comment. If we saw that comment, we don't add this
3339 // method to the type descriptor.
3340 if (p->second->nointerface())
3341 continue;
3342
3343 smethods.push_back(std::make_pair(p->first, p->second));
3344 }
3345 }
3346
3347 if (smethods.empty())
3348 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
3349
3350 std::sort(smethods.begin(), smethods.end(), Sort_methods());
3351
3352 Type* method_type = methods_type->array_type()->element_type();
3353
3354 Expression_list* vals = new Expression_list();
3355 vals->reserve(smethods.size());
3356 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
3357 = smethods.begin();
3358 p != smethods.end();
3359 ++p)
3360 vals->push_back(this->method_constructor(gogo, method_type, p->first,
3361 p->second, only_value_methods));
3362
3363 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
3364 }
3365
3366 // Return a composite literal for a single method. METHOD_TYPE is the
3367 // type of the entry. METHOD_NAME is the name of the method and M is
3368 // the method information.
3369
3370 Expression*
method_constructor(Gogo *,Type * method_type,const std::string & method_name,const Method * m,bool only_value_methods) const3371 Type::method_constructor(Gogo*, Type* method_type,
3372 const std::string& method_name,
3373 const Method* m,
3374 bool only_value_methods) const
3375 {
3376 Location bloc = Linemap::predeclared_location();
3377
3378 const Struct_field_list* fields = method_type->struct_type()->fields();
3379
3380 Expression_list* vals = new Expression_list();
3381 vals->reserve(5);
3382
3383 Struct_field_list::const_iterator p = fields->begin();
3384 go_assert(p->is_field_name("name"));
3385 const std::string n = Gogo::unpack_hidden_name(method_name);
3386 Expression* s = Expression::make_string(n, bloc);
3387 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3388
3389 ++p;
3390 go_assert(p->is_field_name("pkgPath"));
3391 if (!Gogo::is_hidden_name(method_name))
3392 vals->push_back(Expression::make_nil(bloc));
3393 else
3394 {
3395 s = Expression::make_string(Gogo::hidden_name_pkgpath(method_name),
3396 bloc);
3397 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3398 }
3399
3400 Named_object* no = (m->needs_stub_method()
3401 ? m->stub_object()
3402 : m->named_object());
3403
3404 Function_type* mtype;
3405 if (no->is_function())
3406 mtype = no->func_value()->type();
3407 else
3408 mtype = no->func_declaration_value()->type();
3409 go_assert(mtype->is_method());
3410 Type* nonmethod_type = mtype->copy_without_receiver();
3411
3412 ++p;
3413 go_assert(p->is_field_name("mtyp"));
3414 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3415
3416 ++p;
3417 go_assert(p->is_field_name("typ"));
3418 bool want_pointer_receiver = !only_value_methods && m->is_value_method();
3419 nonmethod_type = mtype->copy_with_receiver_as_param(want_pointer_receiver);
3420 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3421
3422 ++p;
3423 go_assert(p->is_field_name("tfn"));
3424 vals->push_back(Expression::make_func_code_reference(no, bloc));
3425
3426 ++p;
3427 go_assert(p == fields->end());
3428
3429 return Expression::make_struct_composite_literal(method_type, vals, bloc);
3430 }
3431
3432 // Return a composite literal for the type descriptor of a plain type.
3433 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3434 // NULL, it is the name to use as well as the list of methods.
3435
3436 Expression*
plain_type_descriptor(Gogo * gogo,int runtime_type_kind,Named_type * name)3437 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
3438 Named_type* name)
3439 {
3440 return this->type_descriptor_constructor(gogo, runtime_type_kind,
3441 name, NULL, true);
3442 }
3443
3444 // Return the type reflection string for this type.
3445
3446 std::string
reflection(Gogo * gogo) const3447 Type::reflection(Gogo* gogo) const
3448 {
3449 std::string ret;
3450
3451 // The do_reflection virtual function should set RET to the
3452 // reflection string.
3453 this->do_reflection(gogo, &ret);
3454
3455 return ret;
3456 }
3457
3458 // Return whether the backend size of the type is known.
3459
3460 bool
is_backend_type_size_known(Gogo * gogo)3461 Type::is_backend_type_size_known(Gogo* gogo)
3462 {
3463 switch (this->classification_)
3464 {
3465 case TYPE_ERROR:
3466 case TYPE_VOID:
3467 case TYPE_BOOLEAN:
3468 case TYPE_INTEGER:
3469 case TYPE_FLOAT:
3470 case TYPE_COMPLEX:
3471 case TYPE_STRING:
3472 case TYPE_FUNCTION:
3473 case TYPE_POINTER:
3474 case TYPE_NIL:
3475 case TYPE_MAP:
3476 case TYPE_CHANNEL:
3477 case TYPE_INTERFACE:
3478 return true;
3479
3480 case TYPE_STRUCT:
3481 {
3482 const Struct_field_list* fields = this->struct_type()->fields();
3483 for (Struct_field_list::const_iterator pf = fields->begin();
3484 pf != fields->end();
3485 ++pf)
3486 if (!pf->type()->is_backend_type_size_known(gogo))
3487 return false;
3488 return true;
3489 }
3490
3491 case TYPE_ARRAY:
3492 {
3493 const Array_type* at = this->array_type();
3494 if (at->length() == NULL)
3495 return true;
3496 else
3497 {
3498 Numeric_constant nc;
3499 if (!at->length()->numeric_constant_value(&nc))
3500 return false;
3501 mpz_t ival;
3502 if (!nc.to_int(&ival))
3503 return false;
3504 mpz_clear(ival);
3505 return at->element_type()->is_backend_type_size_known(gogo);
3506 }
3507 }
3508
3509 case TYPE_NAMED:
3510 this->named_type()->convert(gogo);
3511 return this->named_type()->is_named_backend_type_size_known();
3512
3513 case TYPE_FORWARD:
3514 {
3515 Forward_declaration_type* fdt = this->forward_declaration_type();
3516 return fdt->real_type()->is_backend_type_size_known(gogo);
3517 }
3518
3519 case TYPE_SINK:
3520 case TYPE_CALL_MULTIPLE_RESULT:
3521 go_unreachable();
3522
3523 default:
3524 go_unreachable();
3525 }
3526 }
3527
3528 // If the size of the type can be determined, set *PSIZE to the size
3529 // in bytes and return true. Otherwise, return false. This queries
3530 // the backend.
3531
3532 bool
backend_type_size(Gogo * gogo,int64_t * psize)3533 Type::backend_type_size(Gogo* gogo, int64_t *psize)
3534 {
3535 if (!this->is_backend_type_size_known(gogo))
3536 return false;
3537 if (this->is_error_type())
3538 return false;
3539 Btype* bt = this->get_backend_placeholder(gogo);
3540 *psize = gogo->backend()->type_size(bt);
3541 if (*psize == -1)
3542 {
3543 if (this->named_type() != NULL)
3544 go_error_at(this->named_type()->location(),
3545 "type %s larger than address space",
3546 Gogo::message_name(this->named_type()->name()).c_str());
3547 else
3548 go_error_at(Linemap::unknown_location(),
3549 "type %s larger than address space",
3550 this->reflection(gogo).c_str());
3551
3552 // Make this an error type to avoid knock-on errors.
3553 this->classification_ = TYPE_ERROR;
3554 return false;
3555 }
3556 return true;
3557 }
3558
3559 // If the alignment of the type can be determined, set *PALIGN to
3560 // the alignment in bytes and return true. Otherwise, return false.
3561
3562 bool
backend_type_align(Gogo * gogo,int64_t * palign)3563 Type::backend_type_align(Gogo* gogo, int64_t *palign)
3564 {
3565 if (!this->is_backend_type_size_known(gogo))
3566 return false;
3567 Btype* bt = this->get_backend_placeholder(gogo);
3568 *palign = gogo->backend()->type_alignment(bt);
3569 return true;
3570 }
3571
3572 // Like backend_type_align, but return the alignment when used as a
3573 // field.
3574
3575 bool
backend_type_field_align(Gogo * gogo,int64_t * palign)3576 Type::backend_type_field_align(Gogo* gogo, int64_t *palign)
3577 {
3578 if (!this->is_backend_type_size_known(gogo))
3579 return false;
3580 Btype* bt = this->get_backend_placeholder(gogo);
3581 *palign = gogo->backend()->type_field_alignment(bt);
3582 return true;
3583 }
3584
3585 // Get the ptrdata value for a type. This is the size of the prefix
3586 // of the type that contains all pointers. Store the ptrdata in
3587 // *PPTRDATA and return whether we found it.
3588
3589 bool
backend_type_ptrdata(Gogo * gogo,int64_t * pptrdata)3590 Type::backend_type_ptrdata(Gogo* gogo, int64_t* pptrdata)
3591 {
3592 *pptrdata = 0;
3593
3594 if (!this->has_pointer())
3595 return true;
3596
3597 if (!this->is_backend_type_size_known(gogo))
3598 return false;
3599
3600 switch (this->classification_)
3601 {
3602 case TYPE_ERROR:
3603 return true;
3604
3605 case TYPE_FUNCTION:
3606 case TYPE_POINTER:
3607 case TYPE_MAP:
3608 case TYPE_CHANNEL:
3609 // These types are nothing but a pointer.
3610 return this->backend_type_size(gogo, pptrdata);
3611
3612 case TYPE_INTERFACE:
3613 // An interface is a struct of two pointers.
3614 return this->backend_type_size(gogo, pptrdata);
3615
3616 case TYPE_STRING:
3617 {
3618 // A string is a struct whose first field is a pointer, and
3619 // whose second field is not.
3620 Type* uint8_type = Type::lookup_integer_type("uint8");
3621 Type* ptr = Type::make_pointer_type(uint8_type);
3622 return ptr->backend_type_size(gogo, pptrdata);
3623 }
3624
3625 case TYPE_NAMED:
3626 case TYPE_FORWARD:
3627 return this->base()->backend_type_ptrdata(gogo, pptrdata);
3628
3629 case TYPE_STRUCT:
3630 {
3631 const Struct_field_list* fields = this->struct_type()->fields();
3632 int64_t offset = 0;
3633 const Struct_field *ptr = NULL;
3634 int64_t ptr_offset = 0;
3635 for (Struct_field_list::const_iterator pf = fields->begin();
3636 pf != fields->end();
3637 ++pf)
3638 {
3639 int64_t field_align;
3640 if (!pf->type()->backend_type_field_align(gogo, &field_align))
3641 return false;
3642 offset = (offset + (field_align - 1)) &~ (field_align - 1);
3643
3644 if (pf->type()->has_pointer())
3645 {
3646 ptr = &*pf;
3647 ptr_offset = offset;
3648 }
3649
3650 int64_t field_size;
3651 if (!pf->type()->backend_type_size(gogo, &field_size))
3652 return false;
3653 offset += field_size;
3654 }
3655
3656 if (ptr != NULL)
3657 {
3658 int64_t ptr_ptrdata;
3659 if (!ptr->type()->backend_type_ptrdata(gogo, &ptr_ptrdata))
3660 return false;
3661 *pptrdata = ptr_offset + ptr_ptrdata;
3662 }
3663 return true;
3664 }
3665
3666 case TYPE_ARRAY:
3667 if (this->is_slice_type())
3668 {
3669 // A slice is a struct whose first field is a pointer, and
3670 // whose remaining fields are not.
3671 Type* element_type = this->array_type()->element_type();
3672 Type* ptr = Type::make_pointer_type(element_type);
3673 return ptr->backend_type_size(gogo, pptrdata);
3674 }
3675 else
3676 {
3677 Numeric_constant nc;
3678 if (!this->array_type()->length()->numeric_constant_value(&nc))
3679 return false;
3680 int64_t len;
3681 if (!nc.to_memory_size(&len))
3682 return false;
3683
3684 Type* element_type = this->array_type()->element_type();
3685 int64_t ele_size;
3686 int64_t ele_ptrdata;
3687 if (!element_type->backend_type_size(gogo, &ele_size)
3688 || !element_type->backend_type_ptrdata(gogo, &ele_ptrdata))
3689 return false;
3690 go_assert(ele_size > 0 && ele_ptrdata > 0);
3691
3692 *pptrdata = (len - 1) * ele_size + ele_ptrdata;
3693 return true;
3694 }
3695
3696 default:
3697 case TYPE_VOID:
3698 case TYPE_BOOLEAN:
3699 case TYPE_INTEGER:
3700 case TYPE_FLOAT:
3701 case TYPE_COMPLEX:
3702 case TYPE_SINK:
3703 case TYPE_NIL:
3704 case TYPE_CALL_MULTIPLE_RESULT:
3705 go_unreachable();
3706 }
3707 }
3708
3709 // Get the ptrdata value to store in a type descriptor. This is
3710 // normally the same as backend_type_ptrdata, but for a type that is
3711 // large enough to use a gcprog we may need to store a different value
3712 // if it ends with an array. If the gcprog uses a repeat descriptor
3713 // for the array, and if the array element ends with non-pointer data,
3714 // then the gcprog will produce a value that describes the complete
3715 // array where the backend ptrdata will omit the non-pointer elements
3716 // of the final array element. This is a subtle difference but the
3717 // run time code checks it to verify that it has expanded a gcprog as
3718 // expected.
3719
3720 bool
descriptor_ptrdata(Gogo * gogo,int64_t * pptrdata)3721 Type::descriptor_ptrdata(Gogo* gogo, int64_t* pptrdata)
3722 {
3723 int64_t backend_ptrdata;
3724 if (!this->backend_type_ptrdata(gogo, &backend_ptrdata))
3725 return false;
3726
3727 int64_t ptrsize;
3728 if (!this->needs_gcprog(gogo, &ptrsize, &backend_ptrdata))
3729 {
3730 *pptrdata = backend_ptrdata;
3731 return true;
3732 }
3733
3734 GCProg prog;
3735 prog.set_from(gogo, this, ptrsize, 0);
3736 int64_t offset = prog.bit_index() * ptrsize;
3737
3738 go_assert(offset >= backend_ptrdata);
3739 *pptrdata = offset;
3740 return true;
3741 }
3742
3743 // Default function to export a type.
3744
3745 void
do_export(Export *) const3746 Type::do_export(Export*) const
3747 {
3748 go_unreachable();
3749 }
3750
3751 // Import a type.
3752
3753 Type*
import_type(Import * imp)3754 Type::import_type(Import* imp)
3755 {
3756 if (imp->match_c_string("("))
3757 return Function_type::do_import(imp);
3758 else if (imp->match_c_string("*"))
3759 return Pointer_type::do_import(imp);
3760 else if (imp->match_c_string("struct "))
3761 return Struct_type::do_import(imp);
3762 else if (imp->match_c_string("["))
3763 return Array_type::do_import(imp);
3764 else if (imp->match_c_string("map "))
3765 return Map_type::do_import(imp);
3766 else if (imp->match_c_string("chan "))
3767 return Channel_type::do_import(imp);
3768 else if (imp->match_c_string("interface"))
3769 return Interface_type::do_import(imp);
3770 else
3771 {
3772 go_error_at(imp->location(), "import error: expected type");
3773 return Type::make_error_type();
3774 }
3775 }
3776
3777 // Class Error_type.
3778
3779 // Return the backend representation of an Error type.
3780
3781 Btype*
do_get_backend(Gogo * gogo)3782 Error_type::do_get_backend(Gogo* gogo)
3783 {
3784 return gogo->backend()->error_type();
3785 }
3786
3787 // Return an expression for the type descriptor for an error type.
3788
3789
3790 Expression*
do_type_descriptor(Gogo *,Named_type *)3791 Error_type::do_type_descriptor(Gogo*, Named_type*)
3792 {
3793 return Expression::make_error(Linemap::predeclared_location());
3794 }
3795
3796 // We should not be asked for the reflection string for an error type.
3797
3798 void
do_reflection(Gogo *,std::string *) const3799 Error_type::do_reflection(Gogo*, std::string*) const
3800 {
3801 go_assert(saw_errors());
3802 }
3803
3804 Type*
make_error_type()3805 Type::make_error_type()
3806 {
3807 static Error_type singleton_error_type;
3808 return &singleton_error_type;
3809 }
3810
3811 // Class Void_type.
3812
3813 // Get the backend representation of a void type.
3814
3815 Btype*
do_get_backend(Gogo * gogo)3816 Void_type::do_get_backend(Gogo* gogo)
3817 {
3818 return gogo->backend()->void_type();
3819 }
3820
3821 Type*
make_void_type()3822 Type::make_void_type()
3823 {
3824 static Void_type singleton_void_type;
3825 return &singleton_void_type;
3826 }
3827
3828 // Class Boolean_type.
3829
3830 // Return the backend representation of the boolean type.
3831
3832 Btype*
do_get_backend(Gogo * gogo)3833 Boolean_type::do_get_backend(Gogo* gogo)
3834 {
3835 return gogo->backend()->bool_type();
3836 }
3837
3838 // Make the type descriptor.
3839
3840 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)3841 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3842 {
3843 if (name != NULL)
3844 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
3845 else
3846 {
3847 Named_object* no = gogo->lookup_global("bool");
3848 go_assert(no != NULL);
3849 return Type::type_descriptor(gogo, no->type_value());
3850 }
3851 }
3852
3853 Type*
make_boolean_type()3854 Type::make_boolean_type()
3855 {
3856 static Boolean_type boolean_type;
3857 return &boolean_type;
3858 }
3859
3860 // The named type "bool".
3861
3862 static Named_type* named_bool_type;
3863
3864 // Get the named type "bool".
3865
3866 Named_type*
lookup_bool_type()3867 Type::lookup_bool_type()
3868 {
3869 return named_bool_type;
3870 }
3871
3872 // Make the named type "bool".
3873
3874 Named_type*
make_named_bool_type()3875 Type::make_named_bool_type()
3876 {
3877 Type* bool_type = Type::make_boolean_type();
3878 Named_object* named_object =
3879 Named_object::make_type("bool", NULL, bool_type,
3880 Linemap::predeclared_location());
3881 Named_type* named_type = named_object->type_value();
3882 named_bool_type = named_type;
3883 return named_type;
3884 }
3885
3886 // Class Integer_type.
3887
3888 Integer_type::Named_integer_types Integer_type::named_integer_types;
3889
3890 // Create a new integer type. Non-abstract integer types always have
3891 // names.
3892
3893 Named_type*
create_integer_type(const char * name,bool is_unsigned,int bits,int runtime_type_kind)3894 Integer_type::create_integer_type(const char* name, bool is_unsigned,
3895 int bits, int runtime_type_kind)
3896 {
3897 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
3898 runtime_type_kind);
3899 std::string sname(name);
3900 Named_object* named_object =
3901 Named_object::make_type(sname, NULL, integer_type,
3902 Linemap::predeclared_location());
3903 Named_type* named_type = named_object->type_value();
3904 std::pair<Named_integer_types::iterator, bool> ins =
3905 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
3906 go_assert(ins.second);
3907 return named_type;
3908 }
3909
3910 // Look up an existing integer type.
3911
3912 Named_type*
lookup_integer_type(const char * name)3913 Integer_type::lookup_integer_type(const char* name)
3914 {
3915 Named_integer_types::const_iterator p =
3916 Integer_type::named_integer_types.find(name);
3917 go_assert(p != Integer_type::named_integer_types.end());
3918 return p->second;
3919 }
3920
3921 // Create a new abstract integer type.
3922
3923 Integer_type*
create_abstract_integer_type()3924 Integer_type::create_abstract_integer_type()
3925 {
3926 static Integer_type* abstract_type;
3927 if (abstract_type == NULL)
3928 {
3929 Type* int_type = Type::lookup_integer_type("int");
3930 abstract_type = new Integer_type(true, false,
3931 int_type->integer_type()->bits(),
3932 RUNTIME_TYPE_KIND_INT);
3933 }
3934 return abstract_type;
3935 }
3936
3937 // Create a new abstract character type.
3938
3939 Integer_type*
create_abstract_character_type()3940 Integer_type::create_abstract_character_type()
3941 {
3942 static Integer_type* abstract_type;
3943 if (abstract_type == NULL)
3944 {
3945 abstract_type = new Integer_type(true, false, 32,
3946 RUNTIME_TYPE_KIND_INT32);
3947 abstract_type->set_is_rune();
3948 }
3949 return abstract_type;
3950 }
3951
3952 // Integer type compatibility.
3953
3954 bool
is_identical(const Integer_type * t) const3955 Integer_type::is_identical(const Integer_type* t) const
3956 {
3957 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
3958 return false;
3959 return this->is_abstract_ == t->is_abstract_;
3960 }
3961
3962 // Hash code.
3963
3964 unsigned int
do_hash_for_method(Gogo *,int) const3965 Integer_type::do_hash_for_method(Gogo*, int) const
3966 {
3967 return ((this->bits_ << 4)
3968 + ((this->is_unsigned_ ? 1 : 0) << 8)
3969 + ((this->is_abstract_ ? 1 : 0) << 9));
3970 }
3971
3972 // Convert an Integer_type to the backend representation.
3973
3974 Btype*
do_get_backend(Gogo * gogo)3975 Integer_type::do_get_backend(Gogo* gogo)
3976 {
3977 if (this->is_abstract_)
3978 {
3979 go_assert(saw_errors());
3980 return gogo->backend()->error_type();
3981 }
3982 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
3983 }
3984
3985 // The type descriptor for an integer type. Integer types are always
3986 // named.
3987
3988 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)3989 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3990 {
3991 go_assert(name != NULL || saw_errors());
3992 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
3993 }
3994
3995 // We should not be asked for the reflection string of a basic type.
3996
3997 void
do_reflection(Gogo *,std::string *) const3998 Integer_type::do_reflection(Gogo*, std::string*) const
3999 {
4000 go_assert(saw_errors());
4001 }
4002
4003 // Make an integer type.
4004
4005 Named_type*
make_integer_type(const char * name,bool is_unsigned,int bits,int runtime_type_kind)4006 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
4007 int runtime_type_kind)
4008 {
4009 return Integer_type::create_integer_type(name, is_unsigned, bits,
4010 runtime_type_kind);
4011 }
4012
4013 // Make an abstract integer type.
4014
4015 Integer_type*
make_abstract_integer_type()4016 Type::make_abstract_integer_type()
4017 {
4018 return Integer_type::create_abstract_integer_type();
4019 }
4020
4021 // Make an abstract character type.
4022
4023 Integer_type*
make_abstract_character_type()4024 Type::make_abstract_character_type()
4025 {
4026 return Integer_type::create_abstract_character_type();
4027 }
4028
4029 // Look up an integer type.
4030
4031 Named_type*
lookup_integer_type(const char * name)4032 Type::lookup_integer_type(const char* name)
4033 {
4034 return Integer_type::lookup_integer_type(name);
4035 }
4036
4037 // Class Float_type.
4038
4039 Float_type::Named_float_types Float_type::named_float_types;
4040
4041 // Create a new float type. Non-abstract float types always have
4042 // names.
4043
4044 Named_type*
create_float_type(const char * name,int bits,int runtime_type_kind)4045 Float_type::create_float_type(const char* name, int bits,
4046 int runtime_type_kind)
4047 {
4048 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
4049 std::string sname(name);
4050 Named_object* named_object =
4051 Named_object::make_type(sname, NULL, float_type,
4052 Linemap::predeclared_location());
4053 Named_type* named_type = named_object->type_value();
4054 std::pair<Named_float_types::iterator, bool> ins =
4055 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
4056 go_assert(ins.second);
4057 return named_type;
4058 }
4059
4060 // Look up an existing float type.
4061
4062 Named_type*
lookup_float_type(const char * name)4063 Float_type::lookup_float_type(const char* name)
4064 {
4065 Named_float_types::const_iterator p =
4066 Float_type::named_float_types.find(name);
4067 go_assert(p != Float_type::named_float_types.end());
4068 return p->second;
4069 }
4070
4071 // Create a new abstract float type.
4072
4073 Float_type*
create_abstract_float_type()4074 Float_type::create_abstract_float_type()
4075 {
4076 static Float_type* abstract_type;
4077 if (abstract_type == NULL)
4078 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
4079 return abstract_type;
4080 }
4081
4082 // Whether this type is identical with T.
4083
4084 bool
is_identical(const Float_type * t) const4085 Float_type::is_identical(const Float_type* t) const
4086 {
4087 if (this->bits_ != t->bits_)
4088 return false;
4089 return this->is_abstract_ == t->is_abstract_;
4090 }
4091
4092 // Hash code.
4093
4094 unsigned int
do_hash_for_method(Gogo *,int) const4095 Float_type::do_hash_for_method(Gogo*, int) const
4096 {
4097 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
4098 }
4099
4100 // Convert to the backend representation.
4101
4102 Btype*
do_get_backend(Gogo * gogo)4103 Float_type::do_get_backend(Gogo* gogo)
4104 {
4105 return gogo->backend()->float_type(this->bits_);
4106 }
4107
4108 // The type descriptor for a float type. Float types are always named.
4109
4110 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)4111 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4112 {
4113 go_assert(name != NULL || saw_errors());
4114 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4115 }
4116
4117 // We should not be asked for the reflection string of a basic type.
4118
4119 void
do_reflection(Gogo *,std::string *) const4120 Float_type::do_reflection(Gogo*, std::string*) const
4121 {
4122 go_assert(saw_errors());
4123 }
4124
4125 // Make a floating point type.
4126
4127 Named_type*
make_float_type(const char * name,int bits,int runtime_type_kind)4128 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
4129 {
4130 return Float_type::create_float_type(name, bits, runtime_type_kind);
4131 }
4132
4133 // Make an abstract float type.
4134
4135 Float_type*
make_abstract_float_type()4136 Type::make_abstract_float_type()
4137 {
4138 return Float_type::create_abstract_float_type();
4139 }
4140
4141 // Look up a float type.
4142
4143 Named_type*
lookup_float_type(const char * name)4144 Type::lookup_float_type(const char* name)
4145 {
4146 return Float_type::lookup_float_type(name);
4147 }
4148
4149 // Class Complex_type.
4150
4151 Complex_type::Named_complex_types Complex_type::named_complex_types;
4152
4153 // Create a new complex type. Non-abstract complex types always have
4154 // names.
4155
4156 Named_type*
create_complex_type(const char * name,int bits,int runtime_type_kind)4157 Complex_type::create_complex_type(const char* name, int bits,
4158 int runtime_type_kind)
4159 {
4160 Complex_type* complex_type = new Complex_type(false, bits,
4161 runtime_type_kind);
4162 std::string sname(name);
4163 Named_object* named_object =
4164 Named_object::make_type(sname, NULL, complex_type,
4165 Linemap::predeclared_location());
4166 Named_type* named_type = named_object->type_value();
4167 std::pair<Named_complex_types::iterator, bool> ins =
4168 Complex_type::named_complex_types.insert(std::make_pair(sname,
4169 named_type));
4170 go_assert(ins.second);
4171 return named_type;
4172 }
4173
4174 // Look up an existing complex type.
4175
4176 Named_type*
lookup_complex_type(const char * name)4177 Complex_type::lookup_complex_type(const char* name)
4178 {
4179 Named_complex_types::const_iterator p =
4180 Complex_type::named_complex_types.find(name);
4181 go_assert(p != Complex_type::named_complex_types.end());
4182 return p->second;
4183 }
4184
4185 // Create a new abstract complex type.
4186
4187 Complex_type*
create_abstract_complex_type()4188 Complex_type::create_abstract_complex_type()
4189 {
4190 static Complex_type* abstract_type;
4191 if (abstract_type == NULL)
4192 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
4193 return abstract_type;
4194 }
4195
4196 // Whether this type is identical with T.
4197
4198 bool
is_identical(const Complex_type * t) const4199 Complex_type::is_identical(const Complex_type *t) const
4200 {
4201 if (this->bits_ != t->bits_)
4202 return false;
4203 return this->is_abstract_ == t->is_abstract_;
4204 }
4205
4206 // Hash code.
4207
4208 unsigned int
do_hash_for_method(Gogo *,int) const4209 Complex_type::do_hash_for_method(Gogo*, int) const
4210 {
4211 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
4212 }
4213
4214 // Convert to the backend representation.
4215
4216 Btype*
do_get_backend(Gogo * gogo)4217 Complex_type::do_get_backend(Gogo* gogo)
4218 {
4219 return gogo->backend()->complex_type(this->bits_);
4220 }
4221
4222 // The type descriptor for a complex type. Complex types are always
4223 // named.
4224
4225 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)4226 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4227 {
4228 go_assert(name != NULL || saw_errors());
4229 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4230 }
4231
4232 // We should not be asked for the reflection string of a basic type.
4233
4234 void
do_reflection(Gogo *,std::string *) const4235 Complex_type::do_reflection(Gogo*, std::string*) const
4236 {
4237 go_assert(saw_errors());
4238 }
4239
4240 // Make a complex type.
4241
4242 Named_type*
make_complex_type(const char * name,int bits,int runtime_type_kind)4243 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
4244 {
4245 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
4246 }
4247
4248 // Make an abstract complex type.
4249
4250 Complex_type*
make_abstract_complex_type()4251 Type::make_abstract_complex_type()
4252 {
4253 return Complex_type::create_abstract_complex_type();
4254 }
4255
4256 // Look up a complex type.
4257
4258 Named_type*
lookup_complex_type(const char * name)4259 Type::lookup_complex_type(const char* name)
4260 {
4261 return Complex_type::lookup_complex_type(name);
4262 }
4263
4264 // Class String_type.
4265
4266 // Convert String_type to the backend representation. A string is a
4267 // struct with two fields: a pointer to the characters and a length.
4268
4269 Btype*
do_get_backend(Gogo * gogo)4270 String_type::do_get_backend(Gogo* gogo)
4271 {
4272 static Btype* backend_string_type;
4273 if (backend_string_type == NULL)
4274 {
4275 std::vector<Backend::Btyped_identifier> fields(2);
4276
4277 Type* b = gogo->lookup_global("byte")->type_value();
4278 Type* pb = Type::make_pointer_type(b);
4279
4280 // We aren't going to get back to this field to finish the
4281 // backend representation, so force it to be finished now.
4282 if (!gogo->named_types_are_converted())
4283 {
4284 Btype* bt = pb->get_backend_placeholder(gogo);
4285 pb->finish_backend(gogo, bt);
4286 }
4287
4288 fields[0].name = "__data";
4289 fields[0].btype = pb->get_backend(gogo);
4290 fields[0].location = Linemap::predeclared_location();
4291
4292 Type* int_type = Type::lookup_integer_type("int");
4293 fields[1].name = "__length";
4294 fields[1].btype = int_type->get_backend(gogo);
4295 fields[1].location = fields[0].location;
4296
4297 backend_string_type = gogo->backend()->struct_type(fields);
4298 }
4299 return backend_string_type;
4300 }
4301
4302 // The type descriptor for the string type.
4303
4304 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)4305 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4306 {
4307 if (name != NULL)
4308 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
4309 else
4310 {
4311 Named_object* no = gogo->lookup_global("string");
4312 go_assert(no != NULL);
4313 return Type::type_descriptor(gogo, no->type_value());
4314 }
4315 }
4316
4317 // We should not be asked for the reflection string of a basic type.
4318
4319 void
do_reflection(Gogo *,std::string * ret) const4320 String_type::do_reflection(Gogo*, std::string* ret) const
4321 {
4322 ret->append("string");
4323 }
4324
4325 // Make a string type.
4326
4327 Type*
make_string_type()4328 Type::make_string_type()
4329 {
4330 static String_type string_type;
4331 return &string_type;
4332 }
4333
4334 // The named type "string".
4335
4336 static Named_type* named_string_type;
4337
4338 // Get the named type "string".
4339
4340 Named_type*
lookup_string_type()4341 Type::lookup_string_type()
4342 {
4343 return named_string_type;
4344 }
4345
4346 // Make the named type string.
4347
4348 Named_type*
make_named_string_type()4349 Type::make_named_string_type()
4350 {
4351 Type* string_type = Type::make_string_type();
4352 Named_object* named_object =
4353 Named_object::make_type("string", NULL, string_type,
4354 Linemap::predeclared_location());
4355 Named_type* named_type = named_object->type_value();
4356 named_string_type = named_type;
4357 return named_type;
4358 }
4359
4360 // The sink type. This is the type of the blank identifier _. Any
4361 // type may be assigned to it.
4362
4363 class Sink_type : public Type
4364 {
4365 public:
Sink_type()4366 Sink_type()
4367 : Type(TYPE_SINK)
4368 { }
4369
4370 protected:
4371 bool
do_compare_is_identity(Gogo *)4372 do_compare_is_identity(Gogo*)
4373 { return false; }
4374
4375 Btype*
do_get_backend(Gogo *)4376 do_get_backend(Gogo*)
4377 { go_unreachable(); }
4378
4379 Expression*
do_type_descriptor(Gogo *,Named_type *)4380 do_type_descriptor(Gogo*, Named_type*)
4381 { go_unreachable(); }
4382
4383 void
do_reflection(Gogo *,std::string *) const4384 do_reflection(Gogo*, std::string*) const
4385 { go_unreachable(); }
4386
4387 void
do_mangled_name(Gogo *,std::string *) const4388 do_mangled_name(Gogo*, std::string*) const
4389 { go_unreachable(); }
4390 };
4391
4392 // Make the sink type.
4393
4394 Type*
make_sink_type()4395 Type::make_sink_type()
4396 {
4397 static Sink_type sink_type;
4398 return &sink_type;
4399 }
4400
4401 // Class Function_type.
4402
4403 // Traversal.
4404
4405 int
do_traverse(Traverse * traverse)4406 Function_type::do_traverse(Traverse* traverse)
4407 {
4408 if (this->receiver_ != NULL
4409 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
4410 return TRAVERSE_EXIT;
4411 if (this->parameters_ != NULL
4412 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
4413 return TRAVERSE_EXIT;
4414 if (this->results_ != NULL
4415 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
4416 return TRAVERSE_EXIT;
4417 return TRAVERSE_CONTINUE;
4418 }
4419
4420 // Returns whether T is a valid redeclaration of this type. If this
4421 // returns false, and REASON is not NULL, *REASON may be set to a
4422 // brief explanation of why it returned false.
4423
4424 bool
is_valid_redeclaration(const Function_type * t,std::string * reason) const4425 Function_type::is_valid_redeclaration(const Function_type* t,
4426 std::string* reason) const
4427 {
4428 if (!this->is_identical(t, false, COMPARE_TAGS, reason))
4429 return false;
4430
4431 // A redeclaration of a function is required to use the same names
4432 // for the receiver and parameters.
4433 if (this->receiver() != NULL
4434 && this->receiver()->name() != t->receiver()->name())
4435 {
4436 if (reason != NULL)
4437 *reason = "receiver name changed";
4438 return false;
4439 }
4440
4441 const Typed_identifier_list* parms1 = this->parameters();
4442 const Typed_identifier_list* parms2 = t->parameters();
4443 if (parms1 != NULL)
4444 {
4445 Typed_identifier_list::const_iterator p1 = parms1->begin();
4446 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
4447 p2 != parms2->end();
4448 ++p2, ++p1)
4449 {
4450 if (p1->name() != p2->name())
4451 {
4452 if (reason != NULL)
4453 *reason = "parameter name changed";
4454 return false;
4455 }
4456
4457 // This is called at parse time, so we may have unknown
4458 // types.
4459 Type* t1 = p1->type()->forwarded();
4460 Type* t2 = p2->type()->forwarded();
4461 if (t1 != t2
4462 && t1->forward_declaration_type() != NULL
4463 && (t2->forward_declaration_type() == NULL
4464 || (t1->forward_declaration_type()->named_object()
4465 != t2->forward_declaration_type()->named_object())))
4466 return false;
4467 }
4468 }
4469
4470 const Typed_identifier_list* results1 = this->results();
4471 const Typed_identifier_list* results2 = t->results();
4472 if (results1 != NULL)
4473 {
4474 Typed_identifier_list::const_iterator res1 = results1->begin();
4475 for (Typed_identifier_list::const_iterator res2 = results2->begin();
4476 res2 != results2->end();
4477 ++res2, ++res1)
4478 {
4479 if (res1->name() != res2->name())
4480 {
4481 if (reason != NULL)
4482 *reason = "result name changed";
4483 return false;
4484 }
4485
4486 // This is called at parse time, so we may have unknown
4487 // types.
4488 Type* t1 = res1->type()->forwarded();
4489 Type* t2 = res2->type()->forwarded();
4490 if (t1 != t2
4491 && t1->forward_declaration_type() != NULL
4492 && (t2->forward_declaration_type() == NULL
4493 || (t1->forward_declaration_type()->named_object()
4494 != t2->forward_declaration_type()->named_object())))
4495 return false;
4496 }
4497 }
4498
4499 return true;
4500 }
4501
4502 // Check whether T is the same as this type.
4503
4504 bool
is_identical(const Function_type * t,bool ignore_receiver,int flags,std::string * reason) const4505 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
4506 int flags, std::string* reason) const
4507 {
4508 if (this->is_backend_function_type() != t->is_backend_function_type())
4509 return false;
4510
4511 if (!ignore_receiver)
4512 {
4513 const Typed_identifier* r1 = this->receiver();
4514 const Typed_identifier* r2 = t->receiver();
4515 if ((r1 != NULL) != (r2 != NULL))
4516 {
4517 if (reason != NULL)
4518 *reason = _("different receiver types");
4519 return false;
4520 }
4521 if (r1 != NULL)
4522 {
4523 if (!Type::are_identical(r1->type(), r2->type(), flags, reason))
4524 {
4525 if (reason != NULL && !reason->empty())
4526 *reason = "receiver: " + *reason;
4527 return false;
4528 }
4529 }
4530 }
4531
4532 const Typed_identifier_list* parms1 = this->parameters();
4533 if (parms1 != NULL && parms1->empty())
4534 parms1 = NULL;
4535 const Typed_identifier_list* parms2 = t->parameters();
4536 if (parms2 != NULL && parms2->empty())
4537 parms2 = NULL;
4538 if ((parms1 != NULL) != (parms2 != NULL))
4539 {
4540 if (reason != NULL)
4541 *reason = _("different number of parameters");
4542 return false;
4543 }
4544 if (parms1 != NULL)
4545 {
4546 Typed_identifier_list::const_iterator p1 = parms1->begin();
4547 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
4548 p2 != parms2->end();
4549 ++p2, ++p1)
4550 {
4551 if (p1 == parms1->end())
4552 {
4553 if (reason != NULL)
4554 *reason = _("different number of parameters");
4555 return false;
4556 }
4557
4558 if (!Type::are_identical(p1->type(), p2->type(), flags, NULL))
4559 {
4560 if (reason != NULL)
4561 *reason = _("different parameter types");
4562 return false;
4563 }
4564 }
4565 if (p1 != parms1->end())
4566 {
4567 if (reason != NULL)
4568 *reason = _("different number of parameters");
4569 return false;
4570 }
4571 }
4572
4573 if (this->is_varargs() != t->is_varargs())
4574 {
4575 if (reason != NULL)
4576 *reason = _("different varargs");
4577 return false;
4578 }
4579
4580 const Typed_identifier_list* results1 = this->results();
4581 if (results1 != NULL && results1->empty())
4582 results1 = NULL;
4583 const Typed_identifier_list* results2 = t->results();
4584 if (results2 != NULL && results2->empty())
4585 results2 = NULL;
4586 if ((results1 != NULL) != (results2 != NULL))
4587 {
4588 if (reason != NULL)
4589 *reason = _("different number of results");
4590 return false;
4591 }
4592 if (results1 != NULL)
4593 {
4594 Typed_identifier_list::const_iterator res1 = results1->begin();
4595 for (Typed_identifier_list::const_iterator res2 = results2->begin();
4596 res2 != results2->end();
4597 ++res2, ++res1)
4598 {
4599 if (res1 == results1->end())
4600 {
4601 if (reason != NULL)
4602 *reason = _("different number of results");
4603 return false;
4604 }
4605
4606 if (!Type::are_identical(res1->type(), res2->type(), flags, NULL))
4607 {
4608 if (reason != NULL)
4609 *reason = _("different result types");
4610 return false;
4611 }
4612 }
4613 if (res1 != results1->end())
4614 {
4615 if (reason != NULL)
4616 *reason = _("different number of results");
4617 return false;
4618 }
4619 }
4620
4621 return true;
4622 }
4623
4624 // Hash code.
4625
4626 unsigned int
do_hash_for_method(Gogo * gogo,int flags) const4627 Function_type::do_hash_for_method(Gogo* gogo, int flags) const
4628 {
4629 unsigned int ret = 0;
4630 // We ignore the receiver type for hash codes, because we need to
4631 // get the same hash code for a method in an interface and a method
4632 // declared for a type. The former will not have a receiver.
4633 if (this->parameters_ != NULL)
4634 {
4635 int shift = 1;
4636 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
4637 p != this->parameters_->end();
4638 ++p, ++shift)
4639 ret += p->type()->hash_for_method(gogo, flags) << shift;
4640 }
4641 if (this->results_ != NULL)
4642 {
4643 int shift = 2;
4644 for (Typed_identifier_list::const_iterator p = this->results_->begin();
4645 p != this->results_->end();
4646 ++p, ++shift)
4647 ret += p->type()->hash_for_method(gogo, flags) << shift;
4648 }
4649 if (this->is_varargs_)
4650 ret += 1;
4651 ret <<= 4;
4652 return ret;
4653 }
4654
4655 // Hash result parameters.
4656
4657 unsigned int
operator ()(const Typed_identifier_list * t) const4658 Function_type::Results_hash::operator()(const Typed_identifier_list* t) const
4659 {
4660 unsigned int hash = 0;
4661 for (Typed_identifier_list::const_iterator p = t->begin();
4662 p != t->end();
4663 ++p)
4664 {
4665 hash <<= 2;
4666 hash = Gogo::hash_string(p->name(), hash);
4667 hash += p->type()->hash_for_method(NULL, Type::COMPARE_TAGS);
4668 }
4669 return hash;
4670 }
4671
4672 // Compare result parameters so that can map identical result
4673 // parameters to a single struct type.
4674
4675 bool
operator ()(const Typed_identifier_list * a,const Typed_identifier_list * b) const4676 Function_type::Results_equal::operator()(const Typed_identifier_list* a,
4677 const Typed_identifier_list* b) const
4678 {
4679 if (a->size() != b->size())
4680 return false;
4681 Typed_identifier_list::const_iterator pa = a->begin();
4682 for (Typed_identifier_list::const_iterator pb = b->begin();
4683 pb != b->end();
4684 ++pa, ++pb)
4685 {
4686 if (pa->name() != pb->name()
4687 || !Type::are_identical(pa->type(), pb->type(), Type::COMPARE_TAGS,
4688 NULL))
4689 return false;
4690 }
4691 return true;
4692 }
4693
4694 // Hash from results to a backend struct type.
4695
4696 Function_type::Results_structs Function_type::results_structs;
4697
4698 // Get the backend representation for a function type.
4699
4700 Btype*
get_backend_fntype(Gogo * gogo)4701 Function_type::get_backend_fntype(Gogo* gogo)
4702 {
4703 if (this->fnbtype_ == NULL)
4704 {
4705 Backend::Btyped_identifier breceiver;
4706 if (this->receiver_ != NULL)
4707 {
4708 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
4709
4710 // We always pass the address of the receiver parameter, in
4711 // order to make interface calls work with unknown types.
4712 Type* rtype = this->receiver_->type();
4713 if (rtype->points_to() == NULL)
4714 rtype = Type::make_pointer_type(rtype);
4715 breceiver.btype = rtype->get_backend(gogo);
4716 breceiver.location = this->receiver_->location();
4717 }
4718
4719 std::vector<Backend::Btyped_identifier> bparameters;
4720 if (this->parameters_ != NULL)
4721 {
4722 bparameters.resize(this->parameters_->size());
4723 size_t i = 0;
4724 for (Typed_identifier_list::const_iterator p =
4725 this->parameters_->begin(); p != this->parameters_->end();
4726 ++p, ++i)
4727 {
4728 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
4729 bparameters[i].btype = p->type()->get_backend(gogo);
4730 bparameters[i].location = p->location();
4731 }
4732 go_assert(i == bparameters.size());
4733 }
4734
4735 std::vector<Backend::Btyped_identifier> bresults;
4736 Btype* bresult_struct = NULL;
4737 if (this->results_ != NULL)
4738 {
4739 bresults.resize(this->results_->size());
4740 size_t i = 0;
4741 for (Typed_identifier_list::const_iterator p =
4742 this->results_->begin();
4743 p != this->results_->end();
4744 ++p, ++i)
4745 {
4746 bresults[i].name = Gogo::unpack_hidden_name(p->name());
4747 bresults[i].btype = p->type()->get_backend(gogo);
4748 bresults[i].location = p->location();
4749 }
4750 go_assert(i == bresults.size());
4751
4752 if (this->results_->size() > 1)
4753 {
4754 // Use the same results struct for all functions that
4755 // return the same set of results. This is useful to
4756 // unify calls to interface methods with other calls.
4757 std::pair<Typed_identifier_list*, Btype*> val;
4758 val.first = this->results_;
4759 val.second = NULL;
4760 std::pair<Results_structs::iterator, bool> ins =
4761 Function_type::results_structs.insert(val);
4762 if (ins.second)
4763 {
4764 // Build a new struct type.
4765 Struct_field_list* sfl = new Struct_field_list;
4766 for (Typed_identifier_list::const_iterator p =
4767 this->results_->begin();
4768 p != this->results_->end();
4769 ++p)
4770 {
4771 Typed_identifier tid = *p;
4772 if (tid.name().empty())
4773 tid = Typed_identifier("UNNAMED", tid.type(),
4774 tid.location());
4775 sfl->push_back(Struct_field(tid));
4776 }
4777 Struct_type* st = Type::make_struct_type(sfl,
4778 this->location());
4779 st->set_is_struct_incomparable();
4780 ins.first->second = st->get_backend(gogo);
4781 }
4782 bresult_struct = ins.first->second;
4783 }
4784 }
4785
4786 this->fnbtype_ = gogo->backend()->function_type(breceiver, bparameters,
4787 bresults, bresult_struct,
4788 this->location());
4789
4790 }
4791
4792 return this->fnbtype_;
4793 }
4794
4795 // Get the backend representation for a Go function type.
4796
4797 Btype*
do_get_backend(Gogo * gogo)4798 Function_type::do_get_backend(Gogo* gogo)
4799 {
4800 // When we do anything with a function value other than call it, it
4801 // is represented as a pointer to a struct whose first field is the
4802 // actual function. So that is what we return as the type of a Go
4803 // function.
4804
4805 Location loc = this->location();
4806 Btype* struct_type =
4807 gogo->backend()->placeholder_struct_type("__go_descriptor", loc);
4808 Btype* ptr_struct_type = gogo->backend()->pointer_type(struct_type);
4809
4810 std::vector<Backend::Btyped_identifier> fields(1);
4811 fields[0].name = "code";
4812 fields[0].btype = this->get_backend_fntype(gogo);
4813 fields[0].location = loc;
4814 if (!gogo->backend()->set_placeholder_struct_type(struct_type, fields))
4815 return gogo->backend()->error_type();
4816 return ptr_struct_type;
4817 }
4818
4819 // The type of a function type descriptor.
4820
4821 Type*
make_function_type_descriptor_type()4822 Function_type::make_function_type_descriptor_type()
4823 {
4824 static Type* ret;
4825 if (ret == NULL)
4826 {
4827 Type* tdt = Type::make_type_descriptor_type();
4828 Type* ptdt = Type::make_type_descriptor_ptr_type();
4829
4830 Type* bool_type = Type::lookup_bool_type();
4831
4832 Type* slice_type = Type::make_array_type(ptdt, NULL);
4833
4834 Struct_type* s = Type::make_builtin_struct_type(4,
4835 "", tdt,
4836 "dotdotdot", bool_type,
4837 "in", slice_type,
4838 "out", slice_type);
4839
4840 ret = Type::make_builtin_named_type("FuncType", s);
4841 }
4842
4843 return ret;
4844 }
4845
4846 // The type descriptor for a function type.
4847
4848 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)4849 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4850 {
4851 Location bloc = Linemap::predeclared_location();
4852
4853 Type* ftdt = Function_type::make_function_type_descriptor_type();
4854
4855 const Struct_field_list* fields = ftdt->struct_type()->fields();
4856
4857 Expression_list* vals = new Expression_list();
4858 vals->reserve(4);
4859
4860 Struct_field_list::const_iterator p = fields->begin();
4861 go_assert(p->is_field_name("_type"));
4862 vals->push_back(this->type_descriptor_constructor(gogo,
4863 RUNTIME_TYPE_KIND_FUNC,
4864 name, NULL, true));
4865
4866 ++p;
4867 go_assert(p->is_field_name("dotdotdot"));
4868 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
4869
4870 ++p;
4871 go_assert(p->is_field_name("in"));
4872 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
4873 this->parameters()));
4874
4875 ++p;
4876 go_assert(p->is_field_name("out"));
4877 vals->push_back(this->type_descriptor_params(p->type(), NULL,
4878 this->results()));
4879
4880 ++p;
4881 go_assert(p == fields->end());
4882
4883 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
4884 }
4885
4886 // Return a composite literal for the parameters or results of a type
4887 // descriptor.
4888
4889 Expression*
type_descriptor_params(Type * params_type,const Typed_identifier * receiver,const Typed_identifier_list * params)4890 Function_type::type_descriptor_params(Type* params_type,
4891 const Typed_identifier* receiver,
4892 const Typed_identifier_list* params)
4893 {
4894 Location bloc = Linemap::predeclared_location();
4895
4896 if (receiver == NULL && params == NULL)
4897 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
4898
4899 Expression_list* vals = new Expression_list();
4900 vals->reserve((params == NULL ? 0 : params->size())
4901 + (receiver != NULL ? 1 : 0));
4902
4903 if (receiver != NULL)
4904 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
4905
4906 if (params != NULL)
4907 {
4908 for (Typed_identifier_list::const_iterator p = params->begin();
4909 p != params->end();
4910 ++p)
4911 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
4912 }
4913
4914 return Expression::make_slice_composite_literal(params_type, vals, bloc);
4915 }
4916
4917 // The reflection string.
4918
4919 void
do_reflection(Gogo * gogo,std::string * ret) const4920 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
4921 {
4922 // FIXME: Turn this off until we straighten out the type of the
4923 // struct field used in a go statement which calls a method.
4924 // go_assert(this->receiver_ == NULL);
4925
4926 ret->append("func");
4927
4928 if (this->receiver_ != NULL)
4929 {
4930 ret->push_back('(');
4931 this->append_reflection(this->receiver_->type(), gogo, ret);
4932 ret->push_back(')');
4933 }
4934
4935 ret->push_back('(');
4936 const Typed_identifier_list* params = this->parameters();
4937 if (params != NULL)
4938 {
4939 bool is_varargs = this->is_varargs_;
4940 for (Typed_identifier_list::const_iterator p = params->begin();
4941 p != params->end();
4942 ++p)
4943 {
4944 if (p != params->begin())
4945 ret->append(", ");
4946 if (!is_varargs || p + 1 != params->end())
4947 this->append_reflection(p->type(), gogo, ret);
4948 else
4949 {
4950 ret->append("...");
4951 this->append_reflection(p->type()->array_type()->element_type(),
4952 gogo, ret);
4953 }
4954 }
4955 }
4956 ret->push_back(')');
4957
4958 const Typed_identifier_list* results = this->results();
4959 if (results != NULL && !results->empty())
4960 {
4961 if (results->size() == 1)
4962 ret->push_back(' ');
4963 else
4964 ret->append(" (");
4965 for (Typed_identifier_list::const_iterator p = results->begin();
4966 p != results->end();
4967 ++p)
4968 {
4969 if (p != results->begin())
4970 ret->append(", ");
4971 this->append_reflection(p->type(), gogo, ret);
4972 }
4973 if (results->size() > 1)
4974 ret->push_back(')');
4975 }
4976 }
4977
4978 // Export a function type.
4979
4980 void
do_export(Export * exp) const4981 Function_type::do_export(Export* exp) const
4982 {
4983 // We don't write out the receiver. The only function types which
4984 // should have a receiver are the ones associated with explicitly
4985 // defined methods. For those the receiver type is written out by
4986 // Function::export_func.
4987
4988 exp->write_c_string("(");
4989 bool first = true;
4990 if (this->parameters_ != NULL)
4991 {
4992 bool is_varargs = this->is_varargs_;
4993 for (Typed_identifier_list::const_iterator p =
4994 this->parameters_->begin();
4995 p != this->parameters_->end();
4996 ++p)
4997 {
4998 if (first)
4999 first = false;
5000 else
5001 exp->write_c_string(", ");
5002 exp->write_name(p->name());
5003 exp->write_c_string(" ");
5004 if (!is_varargs || p + 1 != this->parameters_->end())
5005 exp->write_type(p->type());
5006 else
5007 {
5008 exp->write_c_string("...");
5009 exp->write_type(p->type()->array_type()->element_type());
5010 }
5011 }
5012 }
5013 exp->write_c_string(")");
5014
5015 const Typed_identifier_list* results = this->results_;
5016 if (results != NULL)
5017 {
5018 exp->write_c_string(" ");
5019 if (results->size() == 1 && results->begin()->name().empty())
5020 exp->write_type(results->begin()->type());
5021 else
5022 {
5023 first = true;
5024 exp->write_c_string("(");
5025 for (Typed_identifier_list::const_iterator p = results->begin();
5026 p != results->end();
5027 ++p)
5028 {
5029 if (first)
5030 first = false;
5031 else
5032 exp->write_c_string(", ");
5033 exp->write_name(p->name());
5034 exp->write_c_string(" ");
5035 exp->write_type(p->type());
5036 }
5037 exp->write_c_string(")");
5038 }
5039 }
5040 }
5041
5042 // Import a function type.
5043
5044 Function_type*
do_import(Import * imp)5045 Function_type::do_import(Import* imp)
5046 {
5047 imp->require_c_string("(");
5048 Typed_identifier_list* parameters;
5049 bool is_varargs = false;
5050 if (imp->peek_char() == ')')
5051 parameters = NULL;
5052 else
5053 {
5054 parameters = new Typed_identifier_list();
5055 while (true)
5056 {
5057 std::string name = imp->read_name();
5058 imp->require_c_string(" ");
5059
5060 if (imp->match_c_string("..."))
5061 {
5062 imp->advance(3);
5063 is_varargs = true;
5064 }
5065
5066 Type* ptype = imp->read_type();
5067 if (is_varargs)
5068 ptype = Type::make_array_type(ptype, NULL);
5069 parameters->push_back(Typed_identifier(name, ptype,
5070 imp->location()));
5071 if (imp->peek_char() != ',')
5072 break;
5073 go_assert(!is_varargs);
5074 imp->require_c_string(", ");
5075 }
5076 }
5077 imp->require_c_string(")");
5078
5079 Typed_identifier_list* results;
5080 if (imp->peek_char() != ' ')
5081 results = NULL;
5082 else
5083 {
5084 imp->advance(1);
5085 results = new Typed_identifier_list;
5086 if (imp->peek_char() != '(')
5087 {
5088 Type* rtype = imp->read_type();
5089 results->push_back(Typed_identifier("", rtype, imp->location()));
5090 }
5091 else
5092 {
5093 imp->advance(1);
5094 while (true)
5095 {
5096 std::string name = imp->read_name();
5097 imp->require_c_string(" ");
5098 Type* rtype = imp->read_type();
5099 results->push_back(Typed_identifier(name, rtype,
5100 imp->location()));
5101 if (imp->peek_char() != ',')
5102 break;
5103 imp->require_c_string(", ");
5104 }
5105 imp->require_c_string(")");
5106 }
5107 }
5108
5109 Function_type* ret = Type::make_function_type(NULL, parameters, results,
5110 imp->location());
5111 if (is_varargs)
5112 ret->set_is_varargs();
5113 return ret;
5114 }
5115
5116 // Make a copy of a function type without a receiver.
5117
5118 Function_type*
copy_without_receiver() const5119 Function_type::copy_without_receiver() const
5120 {
5121 go_assert(this->is_method());
5122 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
5123 this->results_,
5124 this->location_);
5125 if (this->is_varargs())
5126 ret->set_is_varargs();
5127 if (this->is_builtin())
5128 ret->set_is_builtin();
5129 return ret;
5130 }
5131
5132 // Make a copy of a function type with a receiver.
5133
5134 Function_type*
copy_with_receiver(Type * receiver_type) const5135 Function_type::copy_with_receiver(Type* receiver_type) const
5136 {
5137 go_assert(!this->is_method());
5138 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
5139 this->location_);
5140 Function_type* ret = Type::make_function_type(receiver, this->parameters_,
5141 this->results_,
5142 this->location_);
5143 if (this->is_varargs_)
5144 ret->set_is_varargs();
5145 return ret;
5146 }
5147
5148 // Make a copy of a function type with the receiver as the first
5149 // parameter.
5150
5151 Function_type*
copy_with_receiver_as_param(bool want_pointer_receiver) const5152 Function_type::copy_with_receiver_as_param(bool want_pointer_receiver) const
5153 {
5154 go_assert(this->is_method());
5155 Typed_identifier_list* new_params = new Typed_identifier_list();
5156 Type* rtype = this->receiver_->type();
5157 if (want_pointer_receiver)
5158 rtype = Type::make_pointer_type(rtype);
5159 Typed_identifier receiver(this->receiver_->name(), rtype,
5160 this->receiver_->location());
5161 new_params->push_back(receiver);
5162 const Typed_identifier_list* orig_params = this->parameters_;
5163 if (orig_params != NULL && !orig_params->empty())
5164 {
5165 for (Typed_identifier_list::const_iterator p = orig_params->begin();
5166 p != orig_params->end();
5167 ++p)
5168 new_params->push_back(*p);
5169 }
5170 return Type::make_function_type(NULL, new_params, this->results_,
5171 this->location_);
5172 }
5173
5174 // Make a copy of a function type ignoring any receiver and adding a
5175 // closure parameter.
5176
5177 Function_type*
copy_with_names() const5178 Function_type::copy_with_names() const
5179 {
5180 Typed_identifier_list* new_params = new Typed_identifier_list();
5181 const Typed_identifier_list* orig_params = this->parameters_;
5182 if (orig_params != NULL && !orig_params->empty())
5183 {
5184 static int count;
5185 char buf[50];
5186 for (Typed_identifier_list::const_iterator p = orig_params->begin();
5187 p != orig_params->end();
5188 ++p)
5189 {
5190 snprintf(buf, sizeof buf, "pt.%u", count);
5191 ++count;
5192 new_params->push_back(Typed_identifier(buf, p->type(),
5193 p->location()));
5194 }
5195 }
5196
5197 const Typed_identifier_list* orig_results = this->results_;
5198 Typed_identifier_list* new_results;
5199 if (orig_results == NULL || orig_results->empty())
5200 new_results = NULL;
5201 else
5202 {
5203 new_results = new Typed_identifier_list();
5204 for (Typed_identifier_list::const_iterator p = orig_results->begin();
5205 p != orig_results->end();
5206 ++p)
5207 new_results->push_back(Typed_identifier("", p->type(),
5208 p->location()));
5209 }
5210
5211 return Type::make_function_type(NULL, new_params, new_results,
5212 this->location());
5213 }
5214
5215 // Make a function type.
5216
5217 Function_type*
make_function_type(Typed_identifier * receiver,Typed_identifier_list * parameters,Typed_identifier_list * results,Location location)5218 Type::make_function_type(Typed_identifier* receiver,
5219 Typed_identifier_list* parameters,
5220 Typed_identifier_list* results,
5221 Location location)
5222 {
5223 return new Function_type(receiver, parameters, results, location);
5224 }
5225
5226 // Make a backend function type.
5227
5228 Backend_function_type*
make_backend_function_type(Typed_identifier * receiver,Typed_identifier_list * parameters,Typed_identifier_list * results,Location location)5229 Type::make_backend_function_type(Typed_identifier* receiver,
5230 Typed_identifier_list* parameters,
5231 Typed_identifier_list* results,
5232 Location location)
5233 {
5234 return new Backend_function_type(receiver, parameters, results, location);
5235 }
5236
5237 // Class Pointer_type.
5238
5239 // Traversal.
5240
5241 int
do_traverse(Traverse * traverse)5242 Pointer_type::do_traverse(Traverse* traverse)
5243 {
5244 return Type::traverse(this->to_type_, traverse);
5245 }
5246
5247 // Hash code.
5248
5249 unsigned int
do_hash_for_method(Gogo * gogo,int flags) const5250 Pointer_type::do_hash_for_method(Gogo* gogo, int flags) const
5251 {
5252 return this->to_type_->hash_for_method(gogo, flags) << 4;
5253 }
5254
5255 // Get the backend representation for a pointer type.
5256
5257 Btype*
do_get_backend(Gogo * gogo)5258 Pointer_type::do_get_backend(Gogo* gogo)
5259 {
5260 Btype* to_btype = this->to_type_->get_backend(gogo);
5261 return gogo->backend()->pointer_type(to_btype);
5262 }
5263
5264 // The type of a pointer type descriptor.
5265
5266 Type*
make_pointer_type_descriptor_type()5267 Pointer_type::make_pointer_type_descriptor_type()
5268 {
5269 static Type* ret;
5270 if (ret == NULL)
5271 {
5272 Type* tdt = Type::make_type_descriptor_type();
5273 Type* ptdt = Type::make_type_descriptor_ptr_type();
5274
5275 Struct_type* s = Type::make_builtin_struct_type(2,
5276 "", tdt,
5277 "elem", ptdt);
5278
5279 ret = Type::make_builtin_named_type("PtrType", s);
5280 }
5281
5282 return ret;
5283 }
5284
5285 // The type descriptor for a pointer type.
5286
5287 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)5288 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5289 {
5290 if (this->is_unsafe_pointer_type())
5291 {
5292 go_assert(name != NULL);
5293 return this->plain_type_descriptor(gogo,
5294 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
5295 name);
5296 }
5297 else
5298 {
5299 Location bloc = Linemap::predeclared_location();
5300
5301 const Methods* methods;
5302 Type* deref = this->points_to();
5303 if (deref->named_type() != NULL)
5304 methods = deref->named_type()->methods();
5305 else if (deref->struct_type() != NULL)
5306 methods = deref->struct_type()->methods();
5307 else
5308 methods = NULL;
5309
5310 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
5311
5312 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
5313
5314 Expression_list* vals = new Expression_list();
5315 vals->reserve(2);
5316
5317 Struct_field_list::const_iterator p = fields->begin();
5318 go_assert(p->is_field_name("_type"));
5319 vals->push_back(this->type_descriptor_constructor(gogo,
5320 RUNTIME_TYPE_KIND_PTR,
5321 name, methods, false));
5322
5323 ++p;
5324 go_assert(p->is_field_name("elem"));
5325 vals->push_back(Expression::make_type_descriptor(deref, bloc));
5326
5327 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
5328 }
5329 }
5330
5331 // Reflection string.
5332
5333 void
do_reflection(Gogo * gogo,std::string * ret) const5334 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
5335 {
5336 ret->push_back('*');
5337 this->append_reflection(this->to_type_, gogo, ret);
5338 }
5339
5340 // Export.
5341
5342 void
do_export(Export * exp) const5343 Pointer_type::do_export(Export* exp) const
5344 {
5345 exp->write_c_string("*");
5346 if (this->is_unsafe_pointer_type())
5347 exp->write_c_string("any");
5348 else
5349 exp->write_type(this->to_type_);
5350 }
5351
5352 // Import.
5353
5354 Pointer_type*
do_import(Import * imp)5355 Pointer_type::do_import(Import* imp)
5356 {
5357 imp->require_c_string("*");
5358 if (imp->match_c_string("any"))
5359 {
5360 imp->advance(3);
5361 return Type::make_pointer_type(Type::make_void_type());
5362 }
5363 Type* to = imp->read_type();
5364 return Type::make_pointer_type(to);
5365 }
5366
5367 // Cache of pointer types. Key is "to" type, value is pointer type
5368 // that points to key.
5369
5370 Type::Pointer_type_table Type::pointer_types;
5371
5372 // A list of placeholder pointer types. We keep this so we can ensure
5373 // they are finalized.
5374
5375 std::vector<Pointer_type*> Type::placeholder_pointers;
5376
5377 // Make a pointer type.
5378
5379 Pointer_type*
make_pointer_type(Type * to_type)5380 Type::make_pointer_type(Type* to_type)
5381 {
5382 Pointer_type_table::const_iterator p = pointer_types.find(to_type);
5383 if (p != pointer_types.end())
5384 return p->second;
5385 Pointer_type* ret = new Pointer_type(to_type);
5386 pointer_types[to_type] = ret;
5387 return ret;
5388 }
5389
5390 // This helper is invoked immediately after named types have been
5391 // converted, to clean up any unresolved pointer types remaining in
5392 // the pointer type cache.
5393 //
5394 // The motivation for this routine: occasionally the compiler creates
5395 // some specific pointer type as part of a lowering operation (ex:
5396 // pointer-to-void), then Type::backend_type_size() is invoked on the
5397 // type (which creates a Btype placeholder for it), that placeholder
5398 // passed somewhere along the line to the back end, but since there is
5399 // no reference to the type in user code, there is never a call to
5400 // Type::finish_backend for the type (hence the Btype remains as an
5401 // unresolved placeholder). Calling this routine will clean up such
5402 // instances.
5403
5404 void
finish_pointer_types(Gogo * gogo)5405 Type::finish_pointer_types(Gogo* gogo)
5406 {
5407 // We don't use begin() and end() because it is possible to add new
5408 // placeholder pointer types as we finalized existing ones.
5409 for (size_t i = 0; i < Type::placeholder_pointers.size(); i++)
5410 {
5411 Pointer_type* pt = Type::placeholder_pointers[i];
5412 Type_btypes::iterator tbti = Type::type_btypes.find(pt);
5413 if (tbti != Type::type_btypes.end() && tbti->second.is_placeholder)
5414 {
5415 pt->finish_backend(gogo, tbti->second.btype);
5416 tbti->second.is_placeholder = false;
5417 }
5418 }
5419 }
5420
5421 // Class Nil_type.
5422
5423 // Get the backend representation of a nil type. FIXME: Is this ever
5424 // actually called?
5425
5426 Btype*
do_get_backend(Gogo * gogo)5427 Nil_type::do_get_backend(Gogo* gogo)
5428 {
5429 return gogo->backend()->pointer_type(gogo->backend()->void_type());
5430 }
5431
5432 // Make the nil type.
5433
5434 Type*
make_nil_type()5435 Type::make_nil_type()
5436 {
5437 static Nil_type singleton_nil_type;
5438 return &singleton_nil_type;
5439 }
5440
5441 // The type of a function call which returns multiple values. This is
5442 // really a struct, but we don't want to confuse a function call which
5443 // returns a struct with a function call which returns multiple
5444 // values.
5445
5446 class Call_multiple_result_type : public Type
5447 {
5448 public:
Call_multiple_result_type(Call_expression * call)5449 Call_multiple_result_type(Call_expression* call)
5450 : Type(TYPE_CALL_MULTIPLE_RESULT),
5451 call_(call)
5452 { }
5453
5454 protected:
5455 bool
do_has_pointer() const5456 do_has_pointer() const
5457 { return false; }
5458
5459 bool
do_compare_is_identity(Gogo *)5460 do_compare_is_identity(Gogo*)
5461 { return false; }
5462
5463 Btype*
do_get_backend(Gogo * gogo)5464 do_get_backend(Gogo* gogo)
5465 {
5466 go_assert(saw_errors());
5467 return gogo->backend()->error_type();
5468 }
5469
5470 Expression*
do_type_descriptor(Gogo *,Named_type *)5471 do_type_descriptor(Gogo*, Named_type*)
5472 {
5473 go_assert(saw_errors());
5474 return Expression::make_error(Linemap::unknown_location());
5475 }
5476
5477 void
do_reflection(Gogo *,std::string *) const5478 do_reflection(Gogo*, std::string*) const
5479 { go_assert(saw_errors()); }
5480
5481 void
do_mangled_name(Gogo *,std::string *) const5482 do_mangled_name(Gogo*, std::string*) const
5483 { go_assert(saw_errors()); }
5484
5485 private:
5486 // The expression being called.
5487 Call_expression* call_;
5488 };
5489
5490 // Make a call result type.
5491
5492 Type*
make_call_multiple_result_type(Call_expression * call)5493 Type::make_call_multiple_result_type(Call_expression* call)
5494 {
5495 return new Call_multiple_result_type(call);
5496 }
5497
5498 // Class Struct_field.
5499
5500 // Get the name of a field.
5501
5502 const std::string&
field_name() const5503 Struct_field::field_name() const
5504 {
5505 const std::string& name(this->typed_identifier_.name());
5506 if (!name.empty())
5507 return name;
5508 else
5509 {
5510 // This is called during parsing, before anything is lowered, so
5511 // we have to be pretty careful to avoid dereferencing an
5512 // unknown type name.
5513 Type* t = this->typed_identifier_.type();
5514 Type* dt = t;
5515 if (t->classification() == Type::TYPE_POINTER)
5516 {
5517 // Very ugly.
5518 Pointer_type* ptype = static_cast<Pointer_type*>(t);
5519 dt = ptype->points_to();
5520 }
5521 if (dt->forward_declaration_type() != NULL)
5522 return dt->forward_declaration_type()->name();
5523 else if (dt->named_type() != NULL)
5524 {
5525 // Note that this can be an alias name.
5526 return dt->named_type()->name();
5527 }
5528 else if (t->is_error_type() || dt->is_error_type())
5529 {
5530 static const std::string error_string = "*error*";
5531 return error_string;
5532 }
5533 else
5534 {
5535 // Avoid crashing in the erroneous case where T is named but
5536 // DT is not.
5537 go_assert(t != dt);
5538 if (t->forward_declaration_type() != NULL)
5539 return t->forward_declaration_type()->name();
5540 else if (t->named_type() != NULL)
5541 return t->named_type()->name();
5542 else
5543 go_unreachable();
5544 }
5545 }
5546 }
5547
5548 // Return whether this field is named NAME.
5549
5550 bool
is_field_name(const std::string & name) const5551 Struct_field::is_field_name(const std::string& name) const
5552 {
5553 const std::string& me(this->typed_identifier_.name());
5554 if (!me.empty())
5555 return me == name;
5556 else
5557 {
5558 Type* t = this->typed_identifier_.type();
5559 if (t->points_to() != NULL)
5560 t = t->points_to();
5561 Named_type* nt = t->named_type();
5562 if (nt != NULL && nt->name() == name)
5563 return true;
5564
5565 // This is a horrible hack caused by the fact that we don't pack
5566 // the names of builtin types. FIXME.
5567 if (!this->is_imported_
5568 && nt != NULL
5569 && nt->is_builtin()
5570 && nt->name() == Gogo::unpack_hidden_name(name))
5571 return true;
5572
5573 return false;
5574 }
5575 }
5576
5577 // Return whether this field is an unexported field named NAME.
5578
5579 bool
is_unexported_field_name(Gogo * gogo,const std::string & name) const5580 Struct_field::is_unexported_field_name(Gogo* gogo,
5581 const std::string& name) const
5582 {
5583 const std::string& field_name(this->field_name());
5584 if (Gogo::is_hidden_name(field_name)
5585 && name == Gogo::unpack_hidden_name(field_name)
5586 && gogo->pack_hidden_name(name, false) != field_name)
5587 return true;
5588
5589 // Check for the name of a builtin type. This is like the test in
5590 // is_field_name, only there we return false if this->is_imported_,
5591 // and here we return true.
5592 if (this->is_imported_ && this->is_anonymous())
5593 {
5594 Type* t = this->typed_identifier_.type();
5595 if (t->points_to() != NULL)
5596 t = t->points_to();
5597 Named_type* nt = t->named_type();
5598 if (nt != NULL
5599 && nt->is_builtin()
5600 && nt->name() == Gogo::unpack_hidden_name(name))
5601 return true;
5602 }
5603
5604 return false;
5605 }
5606
5607 // Return whether this field is an embedded built-in type.
5608
5609 bool
is_embedded_builtin(Gogo * gogo) const5610 Struct_field::is_embedded_builtin(Gogo* gogo) const
5611 {
5612 const std::string& name(this->field_name());
5613 // We know that a field is an embedded type if it is anonymous.
5614 // We can decide if it is a built-in type by checking to see if it is
5615 // registered globally under the field's name.
5616 // This allows us to distinguish between embedded built-in types and
5617 // embedded types that are aliases to built-in types.
5618 return (this->is_anonymous()
5619 && !Gogo::is_hidden_name(name)
5620 && gogo->lookup_global(name.c_str()) != NULL);
5621 }
5622
5623 // Class Struct_type.
5624
5625 // A hash table used to find identical unnamed structs so that they
5626 // share method tables.
5627
5628 Struct_type::Identical_structs Struct_type::identical_structs;
5629
5630 // A hash table used to merge method sets for identical unnamed
5631 // structs.
5632
5633 Struct_type::Struct_method_tables Struct_type::struct_method_tables;
5634
5635 // Traversal.
5636
5637 int
do_traverse(Traverse * traverse)5638 Struct_type::do_traverse(Traverse* traverse)
5639 {
5640 Struct_field_list* fields = this->fields_;
5641 if (fields != NULL)
5642 {
5643 for (Struct_field_list::iterator p = fields->begin();
5644 p != fields->end();
5645 ++p)
5646 {
5647 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
5648 return TRAVERSE_EXIT;
5649 }
5650 }
5651 return TRAVERSE_CONTINUE;
5652 }
5653
5654 // Verify that the struct type is complete and valid.
5655
5656 bool
do_verify()5657 Struct_type::do_verify()
5658 {
5659 Struct_field_list* fields = this->fields_;
5660 if (fields == NULL)
5661 return true;
5662 for (Struct_field_list::iterator p = fields->begin();
5663 p != fields->end();
5664 ++p)
5665 {
5666 Type* t = p->type();
5667 if (p->is_anonymous())
5668 {
5669 if ((t->named_type() != NULL && t->points_to() != NULL)
5670 || (t->named_type() == NULL && t->points_to() != NULL
5671 && t->points_to()->points_to() != NULL))
5672 {
5673 go_error_at(p->location(), "embedded type may not be a pointer");
5674 p->set_type(Type::make_error_type());
5675 }
5676 else if (t->points_to() != NULL
5677 && t->points_to()->interface_type() != NULL)
5678 {
5679 go_error_at(p->location(),
5680 "embedded type may not be pointer to interface");
5681 p->set_type(Type::make_error_type());
5682 }
5683 }
5684 }
5685 return true;
5686 }
5687
5688 // Whether this contains a pointer.
5689
5690 bool
do_has_pointer() const5691 Struct_type::do_has_pointer() const
5692 {
5693 const Struct_field_list* fields = this->fields();
5694 if (fields == NULL)
5695 return false;
5696 for (Struct_field_list::const_iterator p = fields->begin();
5697 p != fields->end();
5698 ++p)
5699 {
5700 if (p->type()->has_pointer())
5701 return true;
5702 }
5703 return false;
5704 }
5705
5706 // Whether this type is identical to T.
5707
5708 bool
is_identical(const Struct_type * t,int flags) const5709 Struct_type::is_identical(const Struct_type* t, int flags) const
5710 {
5711 if (this->is_struct_incomparable_ != t->is_struct_incomparable_)
5712 return false;
5713 const Struct_field_list* fields1 = this->fields();
5714 const Struct_field_list* fields2 = t->fields();
5715 if (fields1 == NULL || fields2 == NULL)
5716 return fields1 == fields2;
5717 Struct_field_list::const_iterator pf2 = fields2->begin();
5718 for (Struct_field_list::const_iterator pf1 = fields1->begin();
5719 pf1 != fields1->end();
5720 ++pf1, ++pf2)
5721 {
5722 if (pf2 == fields2->end())
5723 return false;
5724 if (pf1->field_name() != pf2->field_name())
5725 return false;
5726 if (pf1->is_anonymous() != pf2->is_anonymous()
5727 || !Type::are_identical(pf1->type(), pf2->type(), flags, NULL))
5728 return false;
5729 if ((flags & Type::COMPARE_TAGS) != 0)
5730 {
5731 if (!pf1->has_tag())
5732 {
5733 if (pf2->has_tag())
5734 return false;
5735 }
5736 else
5737 {
5738 if (!pf2->has_tag())
5739 return false;
5740 if (pf1->tag() != pf2->tag())
5741 return false;
5742 }
5743 }
5744 }
5745 if (pf2 != fields2->end())
5746 return false;
5747 return true;
5748 }
5749
5750 // Whether comparisons of this struct type are simple identity
5751 // comparisons.
5752
5753 bool
do_compare_is_identity(Gogo * gogo)5754 Struct_type::do_compare_is_identity(Gogo* gogo)
5755 {
5756 const Struct_field_list* fields = this->fields_;
5757 if (fields == NULL)
5758 return true;
5759 int64_t offset = 0;
5760 for (Struct_field_list::const_iterator pf = fields->begin();
5761 pf != fields->end();
5762 ++pf)
5763 {
5764 if (Gogo::is_sink_name(pf->field_name()))
5765 return false;
5766
5767 if (!pf->type()->compare_is_identity(gogo))
5768 return false;
5769
5770 int64_t field_align;
5771 if (!pf->type()->backend_type_align(gogo, &field_align))
5772 return false;
5773 if ((offset & (field_align - 1)) != 0)
5774 {
5775 // This struct has padding. We don't guarantee that that
5776 // padding is zero-initialized for a stack variable, so we
5777 // can't use memcmp to compare struct values.
5778 return false;
5779 }
5780
5781 int64_t field_size;
5782 if (!pf->type()->backend_type_size(gogo, &field_size))
5783 return false;
5784 offset += field_size;
5785 }
5786
5787 int64_t struct_size;
5788 if (!this->backend_type_size(gogo, &struct_size))
5789 return false;
5790 if (offset != struct_size)
5791 {
5792 // Trailing padding may not be zero when on the stack.
5793 return false;
5794 }
5795
5796 return true;
5797 }
5798
5799 // Return whether this struct type is reflexive--whether a value of
5800 // this type is always equal to itself.
5801
5802 bool
do_is_reflexive()5803 Struct_type::do_is_reflexive()
5804 {
5805 const Struct_field_list* fields = this->fields_;
5806 if (fields == NULL)
5807 return true;
5808 for (Struct_field_list::const_iterator pf = fields->begin();
5809 pf != fields->end();
5810 ++pf)
5811 {
5812 if (!pf->type()->is_reflexive())
5813 return false;
5814 }
5815 return true;
5816 }
5817
5818 // Return whether this struct type needs a key update when used as a
5819 // map key.
5820
5821 bool
do_needs_key_update()5822 Struct_type::do_needs_key_update()
5823 {
5824 const Struct_field_list* fields = this->fields_;
5825 if (fields == NULL)
5826 return false;
5827 for (Struct_field_list::const_iterator pf = fields->begin();
5828 pf != fields->end();
5829 ++pf)
5830 {
5831 if (pf->type()->needs_key_update())
5832 return true;
5833 }
5834 return false;
5835 }
5836
5837 // Return whether computing the hash value of an instance of this
5838 // struct type might panic.
5839
5840 bool
do_hash_might_panic()5841 Struct_type::do_hash_might_panic()
5842 {
5843 const Struct_field_list* fields = this->fields_;
5844 if (fields == NULL)
5845 return false;
5846 for (Struct_field_list::const_iterator pf = fields->begin();
5847 pf != fields->end();
5848 ++pf)
5849 {
5850 if (pf->type()->hash_might_panic())
5851 return true;
5852 }
5853 return false;
5854 }
5855
5856 // Return whether this struct type is permitted to be in the heap.
5857
5858 bool
do_in_heap()5859 Struct_type::do_in_heap()
5860 {
5861 const Struct_field_list* fields = this->fields_;
5862 if (fields == NULL)
5863 return true;
5864 for (Struct_field_list::const_iterator pf = fields->begin();
5865 pf != fields->end();
5866 ++pf)
5867 {
5868 if (!pf->type()->in_heap())
5869 return false;
5870 }
5871 return true;
5872 }
5873
5874 // Build identity and hash functions for this struct.
5875
5876 // Hash code.
5877
5878 unsigned int
do_hash_for_method(Gogo * gogo,int flags) const5879 Struct_type::do_hash_for_method(Gogo* gogo, int flags) const
5880 {
5881 unsigned int ret = 0;
5882 if (this->fields() != NULL)
5883 {
5884 for (Struct_field_list::const_iterator pf = this->fields()->begin();
5885 pf != this->fields()->end();
5886 ++pf)
5887 ret = (ret << 1) + pf->type()->hash_for_method(gogo, flags);
5888 }
5889 ret <<= 2;
5890 if (this->is_struct_incomparable_)
5891 ret <<= 1;
5892 return ret;
5893 }
5894
5895 // Find the local field NAME.
5896
5897 const Struct_field*
find_local_field(const std::string & name,unsigned int * pindex) const5898 Struct_type::find_local_field(const std::string& name,
5899 unsigned int *pindex) const
5900 {
5901 const Struct_field_list* fields = this->fields_;
5902 if (fields == NULL)
5903 return NULL;
5904 unsigned int i = 0;
5905 for (Struct_field_list::const_iterator pf = fields->begin();
5906 pf != fields->end();
5907 ++pf, ++i)
5908 {
5909 if (pf->is_field_name(name))
5910 {
5911 if (pindex != NULL)
5912 *pindex = i;
5913 return &*pf;
5914 }
5915 }
5916 return NULL;
5917 }
5918
5919 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
5920
5921 Field_reference_expression*
field_reference(Expression * struct_expr,const std::string & name,Location location) const5922 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
5923 Location location) const
5924 {
5925 unsigned int depth;
5926 return this->field_reference_depth(struct_expr, name, location, NULL,
5927 &depth);
5928 }
5929
5930 // Return an expression for a field, along with the depth at which it
5931 // was found.
5932
5933 Field_reference_expression*
field_reference_depth(Expression * struct_expr,const std::string & name,Location location,Saw_named_type * saw,unsigned int * depth) const5934 Struct_type::field_reference_depth(Expression* struct_expr,
5935 const std::string& name,
5936 Location location,
5937 Saw_named_type* saw,
5938 unsigned int* depth) const
5939 {
5940 const Struct_field_list* fields = this->fields_;
5941 if (fields == NULL)
5942 return NULL;
5943
5944 // Look for a field with this name.
5945 unsigned int i = 0;
5946 for (Struct_field_list::const_iterator pf = fields->begin();
5947 pf != fields->end();
5948 ++pf, ++i)
5949 {
5950 if (pf->is_field_name(name))
5951 {
5952 *depth = 0;
5953 return Expression::make_field_reference(struct_expr, i, location);
5954 }
5955 }
5956
5957 // Look for an anonymous field which contains a field with this
5958 // name.
5959 unsigned int found_depth = 0;
5960 Field_reference_expression* ret = NULL;
5961 i = 0;
5962 for (Struct_field_list::const_iterator pf = fields->begin();
5963 pf != fields->end();
5964 ++pf, ++i)
5965 {
5966 if (!pf->is_anonymous())
5967 continue;
5968
5969 Struct_type* st = pf->type()->deref()->struct_type();
5970 if (st == NULL)
5971 continue;
5972
5973 Saw_named_type* hold_saw = saw;
5974 Saw_named_type saw_here;
5975 Named_type* nt = pf->type()->named_type();
5976 if (nt == NULL)
5977 nt = pf->type()->deref()->named_type();
5978 if (nt != NULL)
5979 {
5980 Saw_named_type* q;
5981 for (q = saw; q != NULL; q = q->next)
5982 {
5983 if (q->nt == nt)
5984 {
5985 // If this is an error, it will be reported
5986 // elsewhere.
5987 break;
5988 }
5989 }
5990 if (q != NULL)
5991 continue;
5992 saw_here.next = saw;
5993 saw_here.nt = nt;
5994 saw = &saw_here;
5995 }
5996
5997 // Look for a reference using a NULL struct expression. If we
5998 // find one, fill in the struct expression with a reference to
5999 // this field.
6000 unsigned int subdepth;
6001 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
6002 location,
6003 saw,
6004 &subdepth);
6005
6006 saw = hold_saw;
6007
6008 if (sub == NULL)
6009 continue;
6010
6011 if (ret == NULL || subdepth < found_depth)
6012 {
6013 if (ret != NULL)
6014 delete ret;
6015 ret = sub;
6016 found_depth = subdepth;
6017 Expression* here = Expression::make_field_reference(struct_expr, i,
6018 location);
6019 if (pf->type()->points_to() != NULL)
6020 here = Expression::make_dereference(here,
6021 Expression::NIL_CHECK_DEFAULT,
6022 location);
6023 while (sub->expr() != NULL)
6024 {
6025 sub = sub->expr()->deref()->field_reference_expression();
6026 go_assert(sub != NULL);
6027 }
6028 sub->set_struct_expression(here);
6029 sub->set_implicit(true);
6030 }
6031 else if (subdepth > found_depth)
6032 delete sub;
6033 else
6034 {
6035 // We do not handle ambiguity here--it should be handled by
6036 // Type::bind_field_or_method.
6037 delete sub;
6038 found_depth = 0;
6039 ret = NULL;
6040 }
6041 }
6042
6043 if (ret != NULL)
6044 *depth = found_depth + 1;
6045
6046 return ret;
6047 }
6048
6049 // Return the total number of fields, including embedded fields.
6050
6051 unsigned int
total_field_count() const6052 Struct_type::total_field_count() const
6053 {
6054 if (this->fields_ == NULL)
6055 return 0;
6056 unsigned int ret = 0;
6057 for (Struct_field_list::const_iterator pf = this->fields_->begin();
6058 pf != this->fields_->end();
6059 ++pf)
6060 {
6061 if (!pf->is_anonymous() || pf->type()->struct_type() == NULL)
6062 ++ret;
6063 else
6064 ret += pf->type()->struct_type()->total_field_count();
6065 }
6066 return ret;
6067 }
6068
6069 // Return whether NAME is an unexported field, for better error reporting.
6070
6071 bool
is_unexported_local_field(Gogo * gogo,const std::string & name) const6072 Struct_type::is_unexported_local_field(Gogo* gogo,
6073 const std::string& name) const
6074 {
6075 const Struct_field_list* fields = this->fields_;
6076 if (fields != NULL)
6077 {
6078 for (Struct_field_list::const_iterator pf = fields->begin();
6079 pf != fields->end();
6080 ++pf)
6081 if (pf->is_unexported_field_name(gogo, name))
6082 return true;
6083 }
6084 return false;
6085 }
6086
6087 // Finalize the methods of an unnamed struct.
6088
6089 void
finalize_methods(Gogo * gogo)6090 Struct_type::finalize_methods(Gogo* gogo)
6091 {
6092 if (this->all_methods_ != NULL)
6093 return;
6094
6095 // It is possible to have multiple identical structs that have
6096 // methods. We want them to share method tables. Otherwise we will
6097 // emit identical methods more than once, which is bad since they
6098 // will even have the same names.
6099 std::pair<Identical_structs::iterator, bool> ins =
6100 Struct_type::identical_structs.insert(std::make_pair(this, this));
6101 if (!ins.second)
6102 {
6103 // An identical struct was already entered into the hash table.
6104 // Note that finalize_methods is, fortunately, not recursive.
6105 this->all_methods_ = ins.first->second->all_methods_;
6106 return;
6107 }
6108
6109 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
6110 }
6111
6112 // Return the method NAME, or NULL if there isn't one or if it is
6113 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6114 // ambiguous.
6115
6116 Method*
method_function(const std::string & name,bool * is_ambiguous) const6117 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
6118 {
6119 return Type::method_function(this->all_methods_, name, is_ambiguous);
6120 }
6121
6122 // Return a pointer to the interface method table for this type for
6123 // the interface INTERFACE. IS_POINTER is true if this is for a
6124 // pointer to THIS.
6125
6126 Expression*
interface_method_table(Interface_type * interface,bool is_pointer)6127 Struct_type::interface_method_table(Interface_type* interface,
6128 bool is_pointer)
6129 {
6130 std::pair<Struct_type*, Struct_type::Struct_method_table_pair*>
6131 val(this, NULL);
6132 std::pair<Struct_type::Struct_method_tables::iterator, bool> ins =
6133 Struct_type::struct_method_tables.insert(val);
6134
6135 Struct_method_table_pair* smtp;
6136 if (!ins.second)
6137 smtp = ins.first->second;
6138 else
6139 {
6140 smtp = new Struct_method_table_pair();
6141 smtp->first = NULL;
6142 smtp->second = NULL;
6143 ins.first->second = smtp;
6144 }
6145
6146 return Type::interface_method_table(this, interface, is_pointer,
6147 &smtp->first, &smtp->second);
6148 }
6149
6150 // Convert struct fields to the backend representation. This is not
6151 // declared in types.h so that types.h doesn't have to #include
6152 // backend.h.
6153
6154 static void
get_backend_struct_fields(Gogo * gogo,Struct_type * type,bool use_placeholder,std::vector<Backend::Btyped_identifier> * bfields)6155 get_backend_struct_fields(Gogo* gogo, Struct_type* type, bool use_placeholder,
6156 std::vector<Backend::Btyped_identifier>* bfields)
6157 {
6158 const Struct_field_list* fields = type->fields();
6159 bfields->resize(fields->size());
6160 size_t i = 0;
6161 int64_t lastsize = 0;
6162 bool saw_nonzero = false;
6163 for (Struct_field_list::const_iterator p = fields->begin();
6164 p != fields->end();
6165 ++p, ++i)
6166 {
6167 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
6168 (*bfields)[i].btype = (use_placeholder
6169 ? p->type()->get_backend_placeholder(gogo)
6170 : p->type()->get_backend(gogo));
6171 (*bfields)[i].location = p->location();
6172 lastsize = gogo->backend()->type_size((*bfields)[i].btype);
6173 if (lastsize != 0)
6174 saw_nonzero = true;
6175 }
6176 go_assert(i == fields->size());
6177 if (saw_nonzero && lastsize == 0)
6178 {
6179 // For nonzero-sized structs which end in a zero-sized thing, we add
6180 // an extra byte of padding to the type. This padding ensures that
6181 // taking the address of the zero-sized thing can't manufacture a
6182 // pointer to the next object in the heap. See issue 9401.
6183 size_t n = fields->size();
6184 bfields->resize(n + 1);
6185 (*bfields)[n].name = "_";
6186 (*bfields)[n].btype = Type::lookup_integer_type("uint8")->get_backend(gogo);
6187 (*bfields)[n].location = (*bfields)[n-1].location;
6188 type->set_has_padding();
6189 }
6190 }
6191
6192 // Get the backend representation for a struct type.
6193
6194 Btype*
do_get_backend(Gogo * gogo)6195 Struct_type::do_get_backend(Gogo* gogo)
6196 {
6197 std::vector<Backend::Btyped_identifier> bfields;
6198 get_backend_struct_fields(gogo, this, false, &bfields);
6199 return gogo->backend()->struct_type(bfields);
6200 }
6201
6202 // Finish the backend representation of the fields of a struct.
6203
6204 void
finish_backend_fields(Gogo * gogo)6205 Struct_type::finish_backend_fields(Gogo* gogo)
6206 {
6207 const Struct_field_list* fields = this->fields_;
6208 if (fields != NULL)
6209 {
6210 for (Struct_field_list::const_iterator p = fields->begin();
6211 p != fields->end();
6212 ++p)
6213 p->type()->get_backend(gogo);
6214 }
6215 }
6216
6217 // The type of a struct type descriptor.
6218
6219 Type*
make_struct_type_descriptor_type()6220 Struct_type::make_struct_type_descriptor_type()
6221 {
6222 static Type* ret;
6223 if (ret == NULL)
6224 {
6225 Type* tdt = Type::make_type_descriptor_type();
6226 Type* ptdt = Type::make_type_descriptor_ptr_type();
6227
6228 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6229 Type* string_type = Type::lookup_string_type();
6230 Type* pointer_string_type = Type::make_pointer_type(string_type);
6231
6232 Struct_type* sf =
6233 Type::make_builtin_struct_type(5,
6234 "name", pointer_string_type,
6235 "pkgPath", pointer_string_type,
6236 "typ", ptdt,
6237 "tag", pointer_string_type,
6238 "offsetAnon", uintptr_type);
6239 Type* nsf = Type::make_builtin_named_type("structField", sf);
6240
6241 Type* slice_type = Type::make_array_type(nsf, NULL);
6242
6243 Struct_type* s = Type::make_builtin_struct_type(2,
6244 "", tdt,
6245 "fields", slice_type);
6246
6247 ret = Type::make_builtin_named_type("StructType", s);
6248 }
6249
6250 return ret;
6251 }
6252
6253 // Build a type descriptor for a struct type.
6254
6255 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)6256 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6257 {
6258 Location bloc = Linemap::predeclared_location();
6259
6260 Type* stdt = Struct_type::make_struct_type_descriptor_type();
6261
6262 const Struct_field_list* fields = stdt->struct_type()->fields();
6263
6264 Expression_list* vals = new Expression_list();
6265 vals->reserve(2);
6266
6267 const Methods* methods = this->methods();
6268 // A named struct should not have methods--the methods should attach
6269 // to the named type.
6270 go_assert(methods == NULL || name == NULL);
6271
6272 Struct_field_list::const_iterator ps = fields->begin();
6273 go_assert(ps->is_field_name("_type"));
6274 vals->push_back(this->type_descriptor_constructor(gogo,
6275 RUNTIME_TYPE_KIND_STRUCT,
6276 name, methods, true));
6277
6278 ++ps;
6279 go_assert(ps->is_field_name("fields"));
6280
6281 Expression_list* elements = new Expression_list();
6282 elements->reserve(this->fields_->size());
6283 Type* element_type = ps->type()->array_type()->element_type();
6284 for (Struct_field_list::const_iterator pf = this->fields_->begin();
6285 pf != this->fields_->end();
6286 ++pf)
6287 {
6288 const Struct_field_list* f = element_type->struct_type()->fields();
6289
6290 Expression_list* fvals = new Expression_list();
6291 fvals->reserve(5);
6292
6293 Struct_field_list::const_iterator q = f->begin();
6294 go_assert(q->is_field_name("name"));
6295 std::string n = Gogo::unpack_hidden_name(pf->field_name());
6296 Expression* s = Expression::make_string(n, bloc);
6297 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6298
6299 ++q;
6300 go_assert(q->is_field_name("pkgPath"));
6301 bool is_embedded_builtin = pf->is_embedded_builtin(gogo);
6302 if (!Gogo::is_hidden_name(pf->field_name()) && !is_embedded_builtin)
6303 fvals->push_back(Expression::make_nil(bloc));
6304 else
6305 {
6306 std::string n;
6307 if (is_embedded_builtin)
6308 n = gogo->package_name();
6309 else
6310 n = Gogo::hidden_name_pkgpath(pf->field_name());
6311 Expression* s = Expression::make_string(n, bloc);
6312 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6313 }
6314
6315 ++q;
6316 go_assert(q->is_field_name("typ"));
6317 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
6318
6319 ++q;
6320 go_assert(q->is_field_name("tag"));
6321 if (!pf->has_tag())
6322 fvals->push_back(Expression::make_nil(bloc));
6323 else
6324 {
6325 Expression* s = Expression::make_string(pf->tag(), bloc);
6326 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6327 }
6328
6329 ++q;
6330 go_assert(q->is_field_name("offsetAnon"));
6331 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6332 Expression* o = Expression::make_struct_field_offset(this, &*pf);
6333 Expression* one = Expression::make_integer_ul(1, uintptr_type, bloc);
6334 o = Expression::make_binary(OPERATOR_LSHIFT, o, one, bloc);
6335 int av = pf->is_anonymous() ? 1 : 0;
6336 Expression* anon = Expression::make_integer_ul(av, uintptr_type, bloc);
6337 o = Expression::make_binary(OPERATOR_OR, o, anon, bloc);
6338 fvals->push_back(o);
6339
6340 Expression* v = Expression::make_struct_composite_literal(element_type,
6341 fvals, bloc);
6342 elements->push_back(v);
6343 }
6344
6345 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
6346 elements, bloc));
6347
6348 return Expression::make_struct_composite_literal(stdt, vals, bloc);
6349 }
6350
6351 // Write the hash function for a struct which can not use the identity
6352 // function.
6353
6354 void
write_hash_function(Gogo * gogo,Named_type *,Function_type * hash_fntype,Function_type * equal_fntype)6355 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
6356 Function_type* hash_fntype,
6357 Function_type* equal_fntype)
6358 {
6359 Location bloc = Linemap::predeclared_location();
6360
6361 // The pointer to the struct that we are going to hash. This is an
6362 // argument to the hash function we are implementing here.
6363 Named_object* key_arg = gogo->lookup("key", NULL);
6364 go_assert(key_arg != NULL);
6365 Type* key_arg_type = key_arg->var_value()->type();
6366
6367 // The seed argument to the hash function.
6368 Named_object* seed_arg = gogo->lookup("seed", NULL);
6369 go_assert(seed_arg != NULL);
6370
6371 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6372
6373 // Make a temporary to hold the return value, initialized to the seed.
6374 Expression* ref = Expression::make_var_reference(seed_arg, bloc);
6375 Temporary_statement* retval = Statement::make_temporary(uintptr_type, ref,
6376 bloc);
6377 gogo->add_statement(retval);
6378
6379 // Make a temporary to hold the key as a uintptr.
6380 ref = Expression::make_var_reference(key_arg, bloc);
6381 ref = Expression::make_cast(uintptr_type, ref, bloc);
6382 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
6383 bloc);
6384 gogo->add_statement(key);
6385
6386 // Loop over the struct fields.
6387 const Struct_field_list* fields = this->fields_;
6388 for (Struct_field_list::const_iterator pf = fields->begin();
6389 pf != fields->end();
6390 ++pf)
6391 {
6392 if (Gogo::is_sink_name(pf->field_name()))
6393 continue;
6394
6395 // Get a pointer to the value of this field.
6396 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
6397 ref = Expression::make_temporary_reference(key, bloc);
6398 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
6399 bloc);
6400 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
6401
6402 // Get the hash function to use for the type of this field.
6403 Named_object* hash_fn;
6404 Named_object* equal_fn;
6405 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
6406 equal_fntype, &hash_fn, &equal_fn);
6407
6408 // Call the hash function for the field, passing retval as the seed.
6409 ref = Expression::make_temporary_reference(retval, bloc);
6410 Expression_list* args = new Expression_list();
6411 args->push_back(subkey);
6412 args->push_back(ref);
6413 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
6414 Expression* call = Expression::make_call(func, args, false, bloc);
6415
6416 // Set retval to the result.
6417 Temporary_reference_expression* tref =
6418 Expression::make_temporary_reference(retval, bloc);
6419 tref->set_is_lvalue();
6420 Statement* s = Statement::make_assignment(tref, call, bloc);
6421 gogo->add_statement(s);
6422 }
6423
6424 // Return retval to the caller of the hash function.
6425 Expression_list* vals = new Expression_list();
6426 ref = Expression::make_temporary_reference(retval, bloc);
6427 vals->push_back(ref);
6428 Statement* s = Statement::make_return_statement(vals, bloc);
6429 gogo->add_statement(s);
6430 }
6431
6432 // Write the equality function for a struct which can not use the
6433 // identity function.
6434
6435 void
write_equal_function(Gogo * gogo,Named_type * name)6436 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
6437 {
6438 Location bloc = Linemap::predeclared_location();
6439
6440 // The pointers to the structs we are going to compare.
6441 Named_object* key1_arg = gogo->lookup("key1", NULL);
6442 Named_object* key2_arg = gogo->lookup("key2", NULL);
6443 go_assert(key1_arg != NULL && key2_arg != NULL);
6444
6445 // Build temporaries with the right types.
6446 Type* pt = Type::make_pointer_type(name != NULL
6447 ? static_cast<Type*>(name)
6448 : static_cast<Type*>(this));
6449
6450 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
6451 ref = Expression::make_unsafe_cast(pt, ref, bloc);
6452 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
6453 gogo->add_statement(p1);
6454
6455 ref = Expression::make_var_reference(key2_arg, bloc);
6456 ref = Expression::make_unsafe_cast(pt, ref, bloc);
6457 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
6458 gogo->add_statement(p2);
6459
6460 const Struct_field_list* fields = this->fields_;
6461 unsigned int field_index = 0;
6462 for (Struct_field_list::const_iterator pf = fields->begin();
6463 pf != fields->end();
6464 ++pf, ++field_index)
6465 {
6466 if (Gogo::is_sink_name(pf->field_name()))
6467 continue;
6468
6469 // Compare one field in both P1 and P2.
6470 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
6471 f1 = Expression::make_dereference(f1, Expression::NIL_CHECK_DEFAULT,
6472 bloc);
6473 f1 = Expression::make_field_reference(f1, field_index, bloc);
6474
6475 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
6476 f2 = Expression::make_dereference(f2, Expression::NIL_CHECK_DEFAULT,
6477 bloc);
6478 f2 = Expression::make_field_reference(f2, field_index, bloc);
6479
6480 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
6481
6482 // If the values are not equal, return false.
6483 gogo->start_block(bloc);
6484 Expression_list* vals = new Expression_list();
6485 vals->push_back(Expression::make_boolean(false, bloc));
6486 Statement* s = Statement::make_return_statement(vals, bloc);
6487 gogo->add_statement(s);
6488 Block* then_block = gogo->finish_block(bloc);
6489
6490 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
6491 gogo->add_statement(s);
6492 }
6493
6494 // All the fields are equal, so return true.
6495 Expression_list* vals = new Expression_list();
6496 vals->push_back(Expression::make_boolean(true, bloc));
6497 Statement* s = Statement::make_return_statement(vals, bloc);
6498 gogo->add_statement(s);
6499 }
6500
6501 // Reflection string.
6502
6503 void
do_reflection(Gogo * gogo,std::string * ret) const6504 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
6505 {
6506 ret->append("struct {");
6507
6508 for (Struct_field_list::const_iterator p = this->fields_->begin();
6509 p != this->fields_->end();
6510 ++p)
6511 {
6512 if (p != this->fields_->begin())
6513 ret->push_back(';');
6514 ret->push_back(' ');
6515 if (!p->is_anonymous())
6516 {
6517 ret->append(Gogo::unpack_hidden_name(p->field_name()));
6518 ret->push_back(' ');
6519 }
6520 if (p->is_anonymous()
6521 && p->type()->named_type() != NULL
6522 && p->type()->named_type()->is_alias())
6523 p->type()->named_type()->append_reflection_type_name(gogo, true, ret);
6524 else
6525 this->append_reflection(p->type(), gogo, ret);
6526
6527 if (p->has_tag())
6528 {
6529 const std::string& tag(p->tag());
6530 ret->append(" \"");
6531 for (std::string::const_iterator p = tag.begin();
6532 p != tag.end();
6533 ++p)
6534 {
6535 if (*p == '\0')
6536 ret->append("\\x00");
6537 else if (*p == '\n')
6538 ret->append("\\n");
6539 else if (*p == '\t')
6540 ret->append("\\t");
6541 else if (*p == '"')
6542 ret->append("\\\"");
6543 else if (*p == '\\')
6544 ret->append("\\\\");
6545 else
6546 ret->push_back(*p);
6547 }
6548 ret->push_back('"');
6549 }
6550 }
6551
6552 if (!this->fields_->empty())
6553 ret->push_back(' ');
6554
6555 ret->push_back('}');
6556 }
6557
6558 // If the offset of field INDEX in the backend implementation can be
6559 // determined, set *POFFSET to the offset in bytes and return true.
6560 // Otherwise, return false.
6561
6562 bool
backend_field_offset(Gogo * gogo,unsigned int index,int64_t * poffset)6563 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
6564 int64_t* poffset)
6565 {
6566 if (!this->is_backend_type_size_known(gogo))
6567 return false;
6568 Btype* bt = this->get_backend_placeholder(gogo);
6569 *poffset = gogo->backend()->type_field_offset(bt, index);
6570 return true;
6571 }
6572
6573 // Export.
6574
6575 void
do_export(Export * exp) const6576 Struct_type::do_export(Export* exp) const
6577 {
6578 exp->write_c_string("struct { ");
6579 const Struct_field_list* fields = this->fields_;
6580 go_assert(fields != NULL);
6581 for (Struct_field_list::const_iterator p = fields->begin();
6582 p != fields->end();
6583 ++p)
6584 {
6585 if (p->is_anonymous())
6586 exp->write_string("? ");
6587 else
6588 {
6589 exp->write_string(p->field_name());
6590 exp->write_c_string(" ");
6591 }
6592 exp->write_type(p->type());
6593
6594 if (p->has_tag())
6595 {
6596 exp->write_c_string(" ");
6597 Expression* expr =
6598 Expression::make_string(p->tag(), Linemap::predeclared_location());
6599
6600 Export_function_body efb(exp, 0);
6601 expr->export_expression(&efb);
6602 exp->write_string(efb.body());
6603
6604 delete expr;
6605 }
6606
6607 exp->write_c_string("; ");
6608 }
6609 exp->write_c_string("}");
6610 }
6611
6612 // Import.
6613
6614 Struct_type*
do_import(Import * imp)6615 Struct_type::do_import(Import* imp)
6616 {
6617 imp->require_c_string("struct { ");
6618 Struct_field_list* fields = new Struct_field_list;
6619 if (imp->peek_char() != '}')
6620 {
6621 while (true)
6622 {
6623 std::string name;
6624 if (imp->match_c_string("? "))
6625 imp->advance(2);
6626 else
6627 {
6628 name = imp->read_identifier();
6629 imp->require_c_string(" ");
6630 }
6631 Type* ftype = imp->read_type();
6632
6633 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
6634 sf.set_is_imported();
6635
6636 if (imp->peek_char() == ' ')
6637 {
6638 imp->advance(1);
6639 Expression* expr = Expression::import_expression(imp,
6640 imp->location());
6641 String_expression* sexpr = expr->string_expression();
6642 go_assert(sexpr != NULL);
6643 sf.set_tag(sexpr->val());
6644 delete sexpr;
6645 }
6646
6647 imp->require_c_string("; ");
6648 fields->push_back(sf);
6649 if (imp->peek_char() == '}')
6650 break;
6651 }
6652 }
6653 imp->require_c_string("}");
6654
6655 return Type::make_struct_type(fields, imp->location());
6656 }
6657
6658 // Whether we can write this struct type to a C header file.
6659 // We can't if any of the fields are structs defined in a different package.
6660
6661 bool
can_write_to_c_header(std::vector<const Named_object * > * requires,std::vector<const Named_object * > * declare) const6662 Struct_type::can_write_to_c_header(
6663 std::vector<const Named_object*>* requires,
6664 std::vector<const Named_object*>* declare) const
6665 {
6666 const Struct_field_list* fields = this->fields_;
6667 if (fields == NULL || fields->empty())
6668 return false;
6669 int sinks = 0;
6670 for (Struct_field_list::const_iterator p = fields->begin();
6671 p != fields->end();
6672 ++p)
6673 {
6674 if (p->is_anonymous())
6675 return false;
6676 if (!this->can_write_type_to_c_header(p->type(), requires, declare))
6677 return false;
6678 if (Gogo::message_name(p->field_name()) == "_")
6679 sinks++;
6680 }
6681 if (sinks > 1)
6682 return false;
6683 return true;
6684 }
6685
6686 // Whether we can write the type T to a C header file.
6687
6688 bool
can_write_type_to_c_header(const Type * t,std::vector<const Named_object * > * requires,std::vector<const Named_object * > * declare) const6689 Struct_type::can_write_type_to_c_header(
6690 const Type* t,
6691 std::vector<const Named_object*>* requires,
6692 std::vector<const Named_object*>* declare) const
6693 {
6694 t = t->forwarded();
6695 switch (t->classification())
6696 {
6697 case TYPE_ERROR:
6698 case TYPE_FORWARD:
6699 return false;
6700
6701 case TYPE_VOID:
6702 case TYPE_BOOLEAN:
6703 case TYPE_INTEGER:
6704 case TYPE_FLOAT:
6705 case TYPE_COMPLEX:
6706 case TYPE_STRING:
6707 case TYPE_FUNCTION:
6708 case TYPE_MAP:
6709 case TYPE_CHANNEL:
6710 case TYPE_INTERFACE:
6711 return true;
6712
6713 case TYPE_POINTER:
6714 // Don't try to handle a pointer to an array.
6715 if (t->points_to()->array_type() != NULL
6716 && !t->points_to()->is_slice_type())
6717 return false;
6718
6719 if (t->points_to()->named_type() != NULL
6720 && t->points_to()->struct_type() != NULL)
6721 declare->push_back(t->points_to()->named_type()->named_object());
6722 return true;
6723
6724 case TYPE_STRUCT:
6725 return t->struct_type()->can_write_to_c_header(requires, declare);
6726
6727 case TYPE_ARRAY:
6728 if (t->is_slice_type())
6729 return true;
6730 return this->can_write_type_to_c_header(t->array_type()->element_type(),
6731 requires, declare);
6732
6733 case TYPE_NAMED:
6734 {
6735 const Named_object* no = t->named_type()->named_object();
6736 if (no->package() != NULL)
6737 {
6738 if (t->is_unsafe_pointer_type())
6739 return true;
6740 return false;
6741 }
6742 if (t->struct_type() != NULL)
6743 {
6744 requires->push_back(no);
6745 return t->struct_type()->can_write_to_c_header(requires, declare);
6746 }
6747 return this->can_write_type_to_c_header(t->base(), requires, declare);
6748 }
6749
6750 case TYPE_CALL_MULTIPLE_RESULT:
6751 case TYPE_NIL:
6752 case TYPE_SINK:
6753 default:
6754 go_unreachable();
6755 }
6756 }
6757
6758 // Write this struct to a C header file.
6759
6760 void
write_to_c_header(std::ostream & os) const6761 Struct_type::write_to_c_header(std::ostream& os) const
6762 {
6763 const Struct_field_list* fields = this->fields_;
6764 for (Struct_field_list::const_iterator p = fields->begin();
6765 p != fields->end();
6766 ++p)
6767 {
6768 os << '\t';
6769 this->write_field_to_c_header(os, p->field_name(), p->type());
6770 os << ';' << std::endl;
6771 }
6772 }
6773
6774 // Write the type of a struct field to a C header file.
6775
6776 void
write_field_to_c_header(std::ostream & os,const std::string & name,const Type * t) const6777 Struct_type::write_field_to_c_header(std::ostream& os, const std::string& name,
6778 const Type *t) const
6779 {
6780 bool print_name = true;
6781 t = t->forwarded();
6782 switch (t->classification())
6783 {
6784 case TYPE_VOID:
6785 os << "void";
6786 break;
6787
6788 case TYPE_BOOLEAN:
6789 os << "_Bool";
6790 break;
6791
6792 case TYPE_INTEGER:
6793 {
6794 const Integer_type* it = t->integer_type();
6795 if (it->is_unsigned())
6796 os << 'u';
6797 os << "int" << it->bits() << "_t";
6798 }
6799 break;
6800
6801 case TYPE_FLOAT:
6802 switch (t->float_type()->bits())
6803 {
6804 case 32:
6805 os << "float";
6806 break;
6807 case 64:
6808 os << "double";
6809 break;
6810 default:
6811 go_unreachable();
6812 }
6813 break;
6814
6815 case TYPE_COMPLEX:
6816 switch (t->complex_type()->bits())
6817 {
6818 case 64:
6819 os << "float _Complex";
6820 break;
6821 case 128:
6822 os << "double _Complex";
6823 break;
6824 default:
6825 go_unreachable();
6826 }
6827 break;
6828
6829 case TYPE_STRING:
6830 os << "String";
6831 break;
6832
6833 case TYPE_FUNCTION:
6834 os << "FuncVal*";
6835 break;
6836
6837 case TYPE_POINTER:
6838 {
6839 std::vector<const Named_object*> requires;
6840 std::vector<const Named_object*> declare;
6841 if (!this->can_write_type_to_c_header(t->points_to(), &requires,
6842 &declare))
6843 os << "void*";
6844 else
6845 {
6846 this->write_field_to_c_header(os, "", t->points_to());
6847 os << '*';
6848 }
6849 }
6850 break;
6851
6852 case TYPE_MAP:
6853 os << "Map*";
6854 break;
6855
6856 case TYPE_CHANNEL:
6857 os << "Chan*";
6858 break;
6859
6860 case TYPE_INTERFACE:
6861 if (t->interface_type()->is_empty())
6862 os << "Eface";
6863 else
6864 os << "Iface";
6865 break;
6866
6867 case TYPE_STRUCT:
6868 os << "struct {" << std::endl;
6869 t->struct_type()->write_to_c_header(os);
6870 os << "\t}";
6871 break;
6872
6873 case TYPE_ARRAY:
6874 if (t->is_slice_type())
6875 os << "Slice";
6876 else
6877 {
6878 const Type *ele = t;
6879 std::vector<const Type*> array_types;
6880 while (ele->array_type() != NULL && !ele->is_slice_type())
6881 {
6882 array_types.push_back(ele);
6883 ele = ele->array_type()->element_type();
6884 }
6885 this->write_field_to_c_header(os, "", ele);
6886 os << ' ' << Gogo::message_name(name);
6887 print_name = false;
6888 while (!array_types.empty())
6889 {
6890 ele = array_types.back();
6891 array_types.pop_back();
6892 os << '[';
6893 Numeric_constant nc;
6894 if (!ele->array_type()->length()->numeric_constant_value(&nc))
6895 go_unreachable();
6896 mpz_t val;
6897 if (!nc.to_int(&val))
6898 go_unreachable();
6899 char* s = mpz_get_str(NULL, 10, val);
6900 os << s;
6901 free(s);
6902 mpz_clear(val);
6903 os << ']';
6904 }
6905 }
6906 break;
6907
6908 case TYPE_NAMED:
6909 {
6910 const Named_object* no = t->named_type()->named_object();
6911 if (t->struct_type() != NULL)
6912 os << "struct " << no->message_name();
6913 else if (t->is_unsafe_pointer_type())
6914 os << "void*";
6915 else if (t == Type::lookup_integer_type("uintptr"))
6916 os << "uintptr_t";
6917 else
6918 {
6919 this->write_field_to_c_header(os, name, t->base());
6920 print_name = false;
6921 }
6922 }
6923 break;
6924
6925 case TYPE_ERROR:
6926 case TYPE_FORWARD:
6927 case TYPE_CALL_MULTIPLE_RESULT:
6928 case TYPE_NIL:
6929 case TYPE_SINK:
6930 default:
6931 go_unreachable();
6932 }
6933
6934 if (print_name && !name.empty())
6935 os << ' ' << Gogo::message_name(name);
6936 }
6937
6938 // Make a struct type.
6939
6940 Struct_type*
make_struct_type(Struct_field_list * fields,Location location)6941 Type::make_struct_type(Struct_field_list* fields,
6942 Location location)
6943 {
6944 return new Struct_type(fields, location);
6945 }
6946
6947 // Class Array_type.
6948
6949 // Store the length of an array as an int64_t into *PLEN. Return
6950 // false if the length can not be determined. This will assert if
6951 // called for a slice.
6952
6953 bool
int_length(int64_t * plen) const6954 Array_type::int_length(int64_t* plen) const
6955 {
6956 go_assert(this->length_ != NULL);
6957 Numeric_constant nc;
6958 if (!this->length_->numeric_constant_value(&nc))
6959 return false;
6960 return nc.to_memory_size(plen);
6961 }
6962
6963 // Whether two array types are identical.
6964
6965 bool
is_identical(const Array_type * t,int flags) const6966 Array_type::is_identical(const Array_type* t, int flags) const
6967 {
6968 if (!Type::are_identical(this->element_type(), t->element_type(),
6969 flags, NULL))
6970 return false;
6971
6972 if (this->is_array_incomparable_ != t->is_array_incomparable_)
6973 return false;
6974
6975 Expression* l1 = this->length();
6976 Expression* l2 = t->length();
6977
6978 // Slices of the same element type are identical.
6979 if (l1 == NULL && l2 == NULL)
6980 return true;
6981
6982 // Arrays of the same element type are identical if they have the
6983 // same length.
6984 if (l1 != NULL && l2 != NULL)
6985 {
6986 if (l1 == l2)
6987 return true;
6988
6989 // Try to determine the lengths. If we can't, assume the arrays
6990 // are not identical.
6991 bool ret = false;
6992 Numeric_constant nc1, nc2;
6993 if (l1->numeric_constant_value(&nc1)
6994 && l2->numeric_constant_value(&nc2))
6995 {
6996 mpz_t v1;
6997 if (nc1.to_int(&v1))
6998 {
6999 mpz_t v2;
7000 if (nc2.to_int(&v2))
7001 {
7002 ret = mpz_cmp(v1, v2) == 0;
7003 mpz_clear(v2);
7004 }
7005 mpz_clear(v1);
7006 }
7007 }
7008 return ret;
7009 }
7010
7011 // Otherwise the arrays are not identical.
7012 return false;
7013 }
7014
7015 // Traversal.
7016
7017 int
do_traverse(Traverse * traverse)7018 Array_type::do_traverse(Traverse* traverse)
7019 {
7020 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
7021 return TRAVERSE_EXIT;
7022 if (this->length_ != NULL
7023 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
7024 return TRAVERSE_EXIT;
7025 return TRAVERSE_CONTINUE;
7026 }
7027
7028 // Check that the length is valid.
7029
7030 bool
verify_length()7031 Array_type::verify_length()
7032 {
7033 if (this->length_ == NULL)
7034 return true;
7035
7036 Type_context context(Type::lookup_integer_type("int"), false);
7037 this->length_->determine_type(&context);
7038
7039 if (!this->length_->is_constant())
7040 {
7041 go_error_at(this->length_->location(), "array bound is not constant");
7042 return false;
7043 }
7044
7045 // For array types, the length expression can be an untyped constant
7046 // representable as an int, but we don't allow explicitly non-integer
7047 // values such as "float64(10)". See issues #13485 and #13486.
7048 if (this->length_->type()->integer_type() == NULL
7049 && !this->length_->type()->is_error_type())
7050 {
7051 go_error_at(this->length_->location(), "invalid array bound");
7052 return false;
7053 }
7054
7055 Numeric_constant nc;
7056 if (!this->length_->numeric_constant_value(&nc))
7057 {
7058 if (this->length_->type()->integer_type() != NULL
7059 || this->length_->type()->float_type() != NULL)
7060 go_error_at(this->length_->location(), "array bound is not constant");
7061 else
7062 go_error_at(this->length_->location(), "array bound is not numeric");
7063 return false;
7064 }
7065
7066 Type* int_type = Type::lookup_integer_type("int");
7067 unsigned int tbits = int_type->integer_type()->bits();
7068 unsigned long val;
7069 switch (nc.to_unsigned_long(&val))
7070 {
7071 case Numeric_constant::NC_UL_VALID:
7072 if (sizeof(val) >= tbits / 8 && val >> (tbits - 1) != 0)
7073 {
7074 go_error_at(this->length_->location(), "array bound overflows");
7075 return false;
7076 }
7077 break;
7078 case Numeric_constant::NC_UL_NOTINT:
7079 go_error_at(this->length_->location(), "array bound truncated to integer");
7080 return false;
7081 case Numeric_constant::NC_UL_NEGATIVE:
7082 go_error_at(this->length_->location(), "negative array bound");
7083 return false;
7084 case Numeric_constant::NC_UL_BIG:
7085 {
7086 mpz_t val;
7087 if (!nc.to_int(&val))
7088 go_unreachable();
7089 unsigned int bits = mpz_sizeinbase(val, 2);
7090 mpz_clear(val);
7091 if (bits >= tbits)
7092 {
7093 go_error_at(this->length_->location(), "array bound overflows");
7094 return false;
7095 }
7096 }
7097 break;
7098 default:
7099 go_unreachable();
7100 }
7101
7102 return true;
7103 }
7104
7105 // Verify the type.
7106
7107 bool
do_verify()7108 Array_type::do_verify()
7109 {
7110 if (this->element_type()->is_error_type())
7111 return false;
7112 if (!this->verify_length())
7113 this->length_ = Expression::make_error(this->length_->location());
7114 return true;
7115 }
7116
7117 // Whether the type contains pointers. This is always true for a
7118 // slice. For an array it is true if the element type has pointers
7119 // and the length is greater than zero.
7120
7121 bool
do_has_pointer() const7122 Array_type::do_has_pointer() const
7123 {
7124 if (this->length_ == NULL)
7125 return true;
7126 if (!this->element_type_->has_pointer())
7127 return false;
7128
7129 Numeric_constant nc;
7130 if (!this->length_->numeric_constant_value(&nc))
7131 {
7132 // Error reported elsewhere.
7133 return false;
7134 }
7135
7136 unsigned long val;
7137 switch (nc.to_unsigned_long(&val))
7138 {
7139 case Numeric_constant::NC_UL_VALID:
7140 return val > 0;
7141 case Numeric_constant::NC_UL_BIG:
7142 return true;
7143 default:
7144 // Error reported elsewhere.
7145 return false;
7146 }
7147 }
7148
7149 // Whether we can use memcmp to compare this array.
7150
7151 bool
do_compare_is_identity(Gogo * gogo)7152 Array_type::do_compare_is_identity(Gogo* gogo)
7153 {
7154 if (this->length_ == NULL)
7155 return false;
7156
7157 // Check for [...], which indicates that this is not a real type.
7158 if (this->length_->is_nil_expression())
7159 return false;
7160
7161 if (!this->element_type_->compare_is_identity(gogo))
7162 return false;
7163
7164 // If there is any padding, then we can't use memcmp.
7165 int64_t size;
7166 int64_t align;
7167 if (!this->element_type_->backend_type_size(gogo, &size)
7168 || !this->element_type_->backend_type_align(gogo, &align))
7169 return false;
7170 if ((size & (align - 1)) != 0)
7171 return false;
7172
7173 return true;
7174 }
7175
7176 // Array type hash code.
7177
7178 unsigned int
do_hash_for_method(Gogo * gogo,int flags) const7179 Array_type::do_hash_for_method(Gogo* gogo, int flags) const
7180 {
7181 unsigned int ret;
7182
7183 // There is no very convenient way to get a hash code for the
7184 // length.
7185 ret = this->element_type_->hash_for_method(gogo, flags) + 1;
7186 if (this->is_array_incomparable_)
7187 ret <<= 1;
7188 return ret;
7189 }
7190
7191 // Write the hash function for an array which can not use the identify
7192 // function.
7193
7194 void
write_hash_function(Gogo * gogo,Named_type * name,Function_type * hash_fntype,Function_type * equal_fntype)7195 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
7196 Function_type* hash_fntype,
7197 Function_type* equal_fntype)
7198 {
7199 Location bloc = Linemap::predeclared_location();
7200
7201 // The pointer to the array that we are going to hash. This is an
7202 // argument to the hash function we are implementing here.
7203 Named_object* key_arg = gogo->lookup("key", NULL);
7204 go_assert(key_arg != NULL);
7205 Type* key_arg_type = key_arg->var_value()->type();
7206
7207 // The seed argument to the hash function.
7208 Named_object* seed_arg = gogo->lookup("seed", NULL);
7209 go_assert(seed_arg != NULL);
7210
7211 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7212
7213 // Make a temporary to hold the return value, initialized to the seed.
7214 Expression* ref = Expression::make_var_reference(seed_arg, bloc);
7215 Temporary_statement* retval = Statement::make_temporary(uintptr_type, ref,
7216 bloc);
7217 gogo->add_statement(retval);
7218
7219 // Make a temporary to hold the key as a uintptr.
7220 ref = Expression::make_var_reference(key_arg, bloc);
7221 ref = Expression::make_cast(uintptr_type, ref, bloc);
7222 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
7223 bloc);
7224 gogo->add_statement(key);
7225
7226 // Loop over the array elements.
7227 // for i = range a
7228 Type* int_type = Type::lookup_integer_type("int");
7229 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
7230 gogo->add_statement(index);
7231
7232 Expression* iref = Expression::make_temporary_reference(index, bloc);
7233 Expression* aref = Expression::make_var_reference(key_arg, bloc);
7234 Type* pt = Type::make_pointer_type(name != NULL
7235 ? static_cast<Type*>(name)
7236 : static_cast<Type*>(this));
7237 aref = Expression::make_cast(pt, aref, bloc);
7238 For_range_statement* for_range = Statement::make_for_range_statement(iref,
7239 NULL,
7240 aref,
7241 bloc);
7242
7243 gogo->start_block(bloc);
7244
7245 // Get the hash function for the element type.
7246 Named_object* hash_fn;
7247 Named_object* equal_fn;
7248 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
7249 hash_fntype, equal_fntype, &hash_fn,
7250 &equal_fn);
7251
7252 // Get a pointer to this element in the loop.
7253 Expression* subkey = Expression::make_temporary_reference(key, bloc);
7254 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
7255
7256 // Get the size of each element.
7257 Expression* ele_size = Expression::make_type_info(this->element_type_,
7258 Expression::TYPE_INFO_SIZE);
7259
7260 // Get the hash of this element, passing retval as the seed.
7261 ref = Expression::make_temporary_reference(retval, bloc);
7262 Expression_list* args = new Expression_list();
7263 args->push_back(subkey);
7264 args->push_back(ref);
7265 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
7266 Expression* call = Expression::make_call(func, args, false, bloc);
7267
7268 // Set retval to the result.
7269 Temporary_reference_expression* tref =
7270 Expression::make_temporary_reference(retval, bloc);
7271 tref->set_is_lvalue();
7272 Statement* s = Statement::make_assignment(tref, call, bloc);
7273 gogo->add_statement(s);
7274
7275 // Increase the element pointer.
7276 tref = Expression::make_temporary_reference(key, bloc);
7277 tref->set_is_lvalue();
7278 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
7279 bloc);
7280 Block* statements = gogo->finish_block(bloc);
7281
7282 for_range->add_statements(statements);
7283 gogo->add_statement(for_range);
7284
7285 // Return retval to the caller of the hash function.
7286 Expression_list* vals = new Expression_list();
7287 ref = Expression::make_temporary_reference(retval, bloc);
7288 vals->push_back(ref);
7289 s = Statement::make_return_statement(vals, bloc);
7290 gogo->add_statement(s);
7291 }
7292
7293 // Write the equality function for an array which can not use the
7294 // identity function.
7295
7296 void
write_equal_function(Gogo * gogo,Named_type * name)7297 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
7298 {
7299 Location bloc = Linemap::predeclared_location();
7300
7301 // The pointers to the arrays we are going to compare.
7302 Named_object* key1_arg = gogo->lookup("key1", NULL);
7303 Named_object* key2_arg = gogo->lookup("key2", NULL);
7304 go_assert(key1_arg != NULL && key2_arg != NULL);
7305
7306 // Build temporaries for the keys with the right types.
7307 Type* pt = Type::make_pointer_type(name != NULL
7308 ? static_cast<Type*>(name)
7309 : static_cast<Type*>(this));
7310
7311 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
7312 ref = Expression::make_unsafe_cast(pt, ref, bloc);
7313 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
7314 gogo->add_statement(p1);
7315
7316 ref = Expression::make_var_reference(key2_arg, bloc);
7317 ref = Expression::make_unsafe_cast(pt, ref, bloc);
7318 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
7319 gogo->add_statement(p2);
7320
7321 // Loop over the array elements.
7322 // for i = range a
7323 Type* int_type = Type::lookup_integer_type("int");
7324 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
7325 gogo->add_statement(index);
7326
7327 Expression* iref = Expression::make_temporary_reference(index, bloc);
7328 Expression* aref = Expression::make_temporary_reference(p1, bloc);
7329 For_range_statement* for_range = Statement::make_for_range_statement(iref,
7330 NULL,
7331 aref,
7332 bloc);
7333
7334 gogo->start_block(bloc);
7335
7336 // Compare element in P1 and P2.
7337 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
7338 e1 = Expression::make_dereference(e1, Expression::NIL_CHECK_DEFAULT, bloc);
7339 ref = Expression::make_temporary_reference(index, bloc);
7340 e1 = Expression::make_array_index(e1, ref, NULL, NULL, bloc);
7341
7342 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
7343 e2 = Expression::make_dereference(e2, Expression::NIL_CHECK_DEFAULT, bloc);
7344 ref = Expression::make_temporary_reference(index, bloc);
7345 e2 = Expression::make_array_index(e2, ref, NULL, NULL, bloc);
7346
7347 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
7348
7349 // If the elements are not equal, return false.
7350 gogo->start_block(bloc);
7351 Expression_list* vals = new Expression_list();
7352 vals->push_back(Expression::make_boolean(false, bloc));
7353 Statement* s = Statement::make_return_statement(vals, bloc);
7354 gogo->add_statement(s);
7355 Block* then_block = gogo->finish_block(bloc);
7356
7357 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
7358 gogo->add_statement(s);
7359
7360 Block* statements = gogo->finish_block(bloc);
7361
7362 for_range->add_statements(statements);
7363 gogo->add_statement(for_range);
7364
7365 // All the elements are equal, so return true.
7366 vals = new Expression_list();
7367 vals->push_back(Expression::make_boolean(true, bloc));
7368 s = Statement::make_return_statement(vals, bloc);
7369 gogo->add_statement(s);
7370 }
7371
7372 // Get the backend representation of the fields of a slice. This is
7373 // not declared in types.h so that types.h doesn't have to #include
7374 // backend.h.
7375 //
7376 // We use int for the count and capacity fields. This matches 6g.
7377 // The language more or less assumes that we can't allocate space of a
7378 // size which does not fit in int.
7379
7380 static void
get_backend_slice_fields(Gogo * gogo,Array_type * type,bool use_placeholder,std::vector<Backend::Btyped_identifier> * bfields)7381 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
7382 std::vector<Backend::Btyped_identifier>* bfields)
7383 {
7384 bfields->resize(3);
7385
7386 Type* pet = Type::make_pointer_type(type->element_type());
7387 Btype* pbet = (use_placeholder
7388 ? pet->get_backend_placeholder(gogo)
7389 : pet->get_backend(gogo));
7390 Location ploc = Linemap::predeclared_location();
7391
7392 Backend::Btyped_identifier* p = &(*bfields)[0];
7393 p->name = "__values";
7394 p->btype = pbet;
7395 p->location = ploc;
7396
7397 Type* int_type = Type::lookup_integer_type("int");
7398
7399 p = &(*bfields)[1];
7400 p->name = "__count";
7401 p->btype = int_type->get_backend(gogo);
7402 p->location = ploc;
7403
7404 p = &(*bfields)[2];
7405 p->name = "__capacity";
7406 p->btype = int_type->get_backend(gogo);
7407 p->location = ploc;
7408 }
7409
7410 // Get the backend representation for the type of this array. A fixed array is
7411 // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7412 // just like an array in C. An open array is a struct with three
7413 // fields: a data pointer, the length, and the capacity.
7414
7415 Btype*
do_get_backend(Gogo * gogo)7416 Array_type::do_get_backend(Gogo* gogo)
7417 {
7418 if (this->length_ == NULL)
7419 {
7420 std::vector<Backend::Btyped_identifier> bfields;
7421 get_backend_slice_fields(gogo, this, false, &bfields);
7422 return gogo->backend()->struct_type(bfields);
7423 }
7424 else
7425 {
7426 Btype* element = this->get_backend_element(gogo, false);
7427 Bexpression* len = this->get_backend_length(gogo);
7428 return gogo->backend()->array_type(element, len);
7429 }
7430 }
7431
7432 // Return the backend representation of the element type.
7433
7434 Btype*
get_backend_element(Gogo * gogo,bool use_placeholder)7435 Array_type::get_backend_element(Gogo* gogo, bool use_placeholder)
7436 {
7437 if (use_placeholder)
7438 return this->element_type_->get_backend_placeholder(gogo);
7439 else
7440 return this->element_type_->get_backend(gogo);
7441 }
7442
7443 // Return the backend representation of the length. The length may be
7444 // computed using a function call, so we must only evaluate it once.
7445
7446 Bexpression*
get_backend_length(Gogo * gogo)7447 Array_type::get_backend_length(Gogo* gogo)
7448 {
7449 go_assert(this->length_ != NULL);
7450 if (this->blength_ == NULL)
7451 {
7452 if (this->length_->is_error_expression())
7453 {
7454 this->blength_ = gogo->backend()->error_expression();
7455 return this->blength_;
7456 }
7457 Numeric_constant nc;
7458 mpz_t val;
7459 if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val))
7460 {
7461 if (mpz_sgn(val) < 0)
7462 {
7463 this->blength_ = gogo->backend()->error_expression();
7464 return this->blength_;
7465 }
7466 Type* t = nc.type();
7467 if (t == NULL)
7468 t = Type::lookup_integer_type("int");
7469 else if (t->is_abstract())
7470 t = t->make_non_abstract_type();
7471 Btype* btype = t->get_backend(gogo);
7472 this->blength_ =
7473 gogo->backend()->integer_constant_expression(btype, val);
7474 mpz_clear(val);
7475 }
7476 else
7477 {
7478 // Make up a translation context for the array length
7479 // expression. FIXME: This won't work in general.
7480 Translate_context context(gogo, NULL, NULL, NULL);
7481 this->blength_ = this->length_->get_backend(&context);
7482
7483 Btype* ibtype = Type::lookup_integer_type("int")->get_backend(gogo);
7484 this->blength_ =
7485 gogo->backend()->convert_expression(ibtype, this->blength_,
7486 this->length_->location());
7487 }
7488 }
7489 return this->blength_;
7490 }
7491
7492 // Finish backend representation of the array.
7493
7494 void
finish_backend_element(Gogo * gogo)7495 Array_type::finish_backend_element(Gogo* gogo)
7496 {
7497 Type* et = this->array_type()->element_type();
7498 et->get_backend(gogo);
7499 if (this->is_slice_type())
7500 {
7501 // This relies on the fact that we always use the same
7502 // structure for a pointer to any given type.
7503 Type* pet = Type::make_pointer_type(et);
7504 pet->get_backend(gogo);
7505 }
7506 }
7507
7508 // Return an expression for a pointer to the values in ARRAY.
7509
7510 Expression*
get_value_pointer(Gogo *,Expression * array,bool is_lvalue) const7511 Array_type::get_value_pointer(Gogo*, Expression* array, bool is_lvalue) const
7512 {
7513 if (this->length() != NULL)
7514 {
7515 // Fixed array.
7516 go_assert(array->type()->array_type() != NULL);
7517 Type* etype = array->type()->array_type()->element_type();
7518 array = Expression::make_unary(OPERATOR_AND, array, array->location());
7519 return Expression::make_cast(Type::make_pointer_type(etype), array,
7520 array->location());
7521 }
7522
7523 // Slice.
7524
7525 if (is_lvalue)
7526 {
7527 Temporary_reference_expression* tref =
7528 array->temporary_reference_expression();
7529 Var_expression* ve = array->var_expression();
7530 if (tref != NULL)
7531 {
7532 tref = tref->copy()->temporary_reference_expression();
7533 tref->set_is_lvalue();
7534 array = tref;
7535 }
7536 else if (ve != NULL)
7537 {
7538 ve = new Var_expression(ve->named_object(), ve->location());
7539 array = ve;
7540 }
7541 }
7542
7543 return Expression::make_slice_info(array,
7544 Expression::SLICE_INFO_VALUE_POINTER,
7545 array->location());
7546 }
7547
7548 // Return an expression for the length of the array ARRAY which has this
7549 // type.
7550
7551 Expression*
get_length(Gogo *,Expression * array) const7552 Array_type::get_length(Gogo*, Expression* array) const
7553 {
7554 if (this->length_ != NULL)
7555 return this->length_;
7556
7557 // This is a slice. We need to read the length field.
7558 return Expression::make_slice_info(array, Expression::SLICE_INFO_LENGTH,
7559 array->location());
7560 }
7561
7562 // Return an expression for the capacity of the array ARRAY which has this
7563 // type.
7564
7565 Expression*
get_capacity(Gogo *,Expression * array) const7566 Array_type::get_capacity(Gogo*, Expression* array) const
7567 {
7568 if (this->length_ != NULL)
7569 return this->length_;
7570
7571 // This is a slice. We need to read the capacity field.
7572 return Expression::make_slice_info(array, Expression::SLICE_INFO_CAPACITY,
7573 array->location());
7574 }
7575
7576 // Export.
7577
7578 void
do_export(Export * exp) const7579 Array_type::do_export(Export* exp) const
7580 {
7581 exp->write_c_string("[");
7582 if (this->length_ != NULL)
7583 {
7584 Numeric_constant nc;
7585 mpz_t val;
7586 if (!this->length_->numeric_constant_value(&nc) || !nc.to_int(&val))
7587 {
7588 go_assert(saw_errors());
7589 return;
7590 }
7591 char* s = mpz_get_str(NULL, 10, val);
7592 exp->write_string(s);
7593 exp->write_string(" ");
7594 mpz_clear(val);
7595 }
7596 exp->write_c_string("] ");
7597 exp->write_type(this->element_type_);
7598 }
7599
7600 // Import.
7601
7602 Array_type*
do_import(Import * imp)7603 Array_type::do_import(Import* imp)
7604 {
7605 imp->require_c_string("[");
7606 Expression* length;
7607 if (imp->peek_char() == ']')
7608 length = NULL;
7609 else
7610 length = Expression::import_expression(imp, imp->location());
7611 imp->require_c_string("] ");
7612 Type* element_type = imp->read_type();
7613 return Type::make_array_type(element_type, length);
7614 }
7615
7616 // The type of an array type descriptor.
7617
7618 Type*
make_array_type_descriptor_type()7619 Array_type::make_array_type_descriptor_type()
7620 {
7621 static Type* ret;
7622 if (ret == NULL)
7623 {
7624 Type* tdt = Type::make_type_descriptor_type();
7625 Type* ptdt = Type::make_type_descriptor_ptr_type();
7626
7627 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7628
7629 Struct_type* sf =
7630 Type::make_builtin_struct_type(4,
7631 "", tdt,
7632 "elem", ptdt,
7633 "slice", ptdt,
7634 "len", uintptr_type);
7635
7636 ret = Type::make_builtin_named_type("ArrayType", sf);
7637 }
7638
7639 return ret;
7640 }
7641
7642 // The type of an slice type descriptor.
7643
7644 Type*
make_slice_type_descriptor_type()7645 Array_type::make_slice_type_descriptor_type()
7646 {
7647 static Type* ret;
7648 if (ret == NULL)
7649 {
7650 Type* tdt = Type::make_type_descriptor_type();
7651 Type* ptdt = Type::make_type_descriptor_ptr_type();
7652
7653 Struct_type* sf =
7654 Type::make_builtin_struct_type(2,
7655 "", tdt,
7656 "elem", ptdt);
7657
7658 ret = Type::make_builtin_named_type("SliceType", sf);
7659 }
7660
7661 return ret;
7662 }
7663
7664 // Build a type descriptor for an array/slice type.
7665
7666 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)7667 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7668 {
7669 if (this->length_ != NULL)
7670 return this->array_type_descriptor(gogo, name);
7671 else
7672 return this->slice_type_descriptor(gogo, name);
7673 }
7674
7675 // Build a type descriptor for an array type.
7676
7677 Expression*
array_type_descriptor(Gogo * gogo,Named_type * name)7678 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
7679 {
7680 Location bloc = Linemap::predeclared_location();
7681
7682 Type* atdt = Array_type::make_array_type_descriptor_type();
7683
7684 const Struct_field_list* fields = atdt->struct_type()->fields();
7685
7686 Expression_list* vals = new Expression_list();
7687 vals->reserve(3);
7688
7689 Struct_field_list::const_iterator p = fields->begin();
7690 go_assert(p->is_field_name("_type"));
7691 vals->push_back(this->type_descriptor_constructor(gogo,
7692 RUNTIME_TYPE_KIND_ARRAY,
7693 name, NULL, true));
7694
7695 ++p;
7696 go_assert(p->is_field_name("elem"));
7697 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
7698
7699 ++p;
7700 go_assert(p->is_field_name("slice"));
7701 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
7702 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
7703
7704 ++p;
7705 go_assert(p->is_field_name("len"));
7706 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
7707
7708 ++p;
7709 go_assert(p == fields->end());
7710
7711 return Expression::make_struct_composite_literal(atdt, vals, bloc);
7712 }
7713
7714 // Build a type descriptor for a slice type.
7715
7716 Expression*
slice_type_descriptor(Gogo * gogo,Named_type * name)7717 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
7718 {
7719 Location bloc = Linemap::predeclared_location();
7720
7721 Type* stdt = Array_type::make_slice_type_descriptor_type();
7722
7723 const Struct_field_list* fields = stdt->struct_type()->fields();
7724
7725 Expression_list* vals = new Expression_list();
7726 vals->reserve(2);
7727
7728 Struct_field_list::const_iterator p = fields->begin();
7729 go_assert(p->is_field_name("_type"));
7730 vals->push_back(this->type_descriptor_constructor(gogo,
7731 RUNTIME_TYPE_KIND_SLICE,
7732 name, NULL, true));
7733
7734 ++p;
7735 go_assert(p->is_field_name("elem"));
7736 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
7737
7738 ++p;
7739 go_assert(p == fields->end());
7740
7741 return Expression::make_struct_composite_literal(stdt, vals, bloc);
7742 }
7743
7744 // Reflection string.
7745
7746 void
do_reflection(Gogo * gogo,std::string * ret) const7747 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
7748 {
7749 ret->push_back('[');
7750 if (this->length_ != NULL)
7751 {
7752 Numeric_constant nc;
7753 if (!this->length_->numeric_constant_value(&nc))
7754 {
7755 go_assert(saw_errors());
7756 return;
7757 }
7758 mpz_t val;
7759 if (!nc.to_int(&val))
7760 {
7761 go_assert(saw_errors());
7762 return;
7763 }
7764 char* s = mpz_get_str(NULL, 10, val);
7765 ret->append(s);
7766 free(s);
7767 mpz_clear(val);
7768 }
7769 ret->push_back(']');
7770
7771 this->append_reflection(this->element_type_, gogo, ret);
7772 }
7773
7774 // Make an array type.
7775
7776 Array_type*
make_array_type(Type * element_type,Expression * length)7777 Type::make_array_type(Type* element_type, Expression* length)
7778 {
7779 return new Array_type(element_type, length);
7780 }
7781
7782 // Class Map_type.
7783
7784 Named_object* Map_type::zero_value;
7785 int64_t Map_type::zero_value_size;
7786 int64_t Map_type::zero_value_align;
7787
7788 // If this map requires the "fat" functions, return the pointer to
7789 // pass as the zero value to those functions. Otherwise, in the
7790 // normal case, return NULL. The map requires the "fat" functions if
7791 // the value size is larger than max_zero_size bytes. max_zero_size
7792 // must match maxZero in libgo/go/runtime/hashmap.go.
7793
7794 Expression*
fat_zero_value(Gogo * gogo)7795 Map_type::fat_zero_value(Gogo* gogo)
7796 {
7797 int64_t valsize;
7798 if (!this->val_type_->backend_type_size(gogo, &valsize))
7799 {
7800 go_assert(saw_errors());
7801 return NULL;
7802 }
7803 if (valsize <= Map_type::max_zero_size)
7804 return NULL;
7805
7806 if (Map_type::zero_value_size < valsize)
7807 Map_type::zero_value_size = valsize;
7808
7809 int64_t valalign;
7810 if (!this->val_type_->backend_type_align(gogo, &valalign))
7811 {
7812 go_assert(saw_errors());
7813 return NULL;
7814 }
7815
7816 if (Map_type::zero_value_align < valalign)
7817 Map_type::zero_value_align = valalign;
7818
7819 Location bloc = Linemap::predeclared_location();
7820
7821 if (Map_type::zero_value == NULL)
7822 {
7823 // The final type will be set in backend_zero_value.
7824 Type* uint8_type = Type::lookup_integer_type("uint8");
7825 Expression* size = Expression::make_integer_ul(0, NULL, bloc);
7826 Array_type* array_type = Type::make_array_type(uint8_type, size);
7827 array_type->set_is_array_incomparable();
7828 Variable* var = new Variable(array_type, NULL, true, false, false, bloc);
7829 std::string name = gogo->map_zero_value_name();
7830 Map_type::zero_value = Named_object::make_variable(name, NULL, var);
7831 }
7832
7833 Expression* z = Expression::make_var_reference(Map_type::zero_value, bloc);
7834 z = Expression::make_unary(OPERATOR_AND, z, bloc);
7835 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
7836 z = Expression::make_cast(unsafe_ptr_type, z, bloc);
7837 return z;
7838 }
7839
7840 // Return whether VAR is the map zero value.
7841
7842 bool
is_zero_value(Variable * var)7843 Map_type::is_zero_value(Variable* var)
7844 {
7845 return (Map_type::zero_value != NULL
7846 && Map_type::zero_value->var_value() == var);
7847 }
7848
7849 // Return the backend representation for the zero value.
7850
7851 Bvariable*
backend_zero_value(Gogo * gogo)7852 Map_type::backend_zero_value(Gogo* gogo)
7853 {
7854 Location bloc = Linemap::predeclared_location();
7855
7856 go_assert(Map_type::zero_value != NULL);
7857
7858 Type* uint8_type = Type::lookup_integer_type("uint8");
7859 Btype* buint8_type = uint8_type->get_backend(gogo);
7860
7861 Type* int_type = Type::lookup_integer_type("int");
7862
7863 Expression* e = Expression::make_integer_int64(Map_type::zero_value_size,
7864 int_type, bloc);
7865 Translate_context context(gogo, NULL, NULL, NULL);
7866 Bexpression* blength = e->get_backend(&context);
7867
7868 Btype* barray_type = gogo->backend()->array_type(buint8_type, blength);
7869
7870 std::string zname = Map_type::zero_value->name();
7871 std::string asm_name(go_selectively_encode_id(zname));
7872 Bvariable* zvar =
7873 gogo->backend()->implicit_variable(zname, asm_name,
7874 barray_type, false, false, true,
7875 Map_type::zero_value_align);
7876 gogo->backend()->implicit_variable_set_init(zvar, zname, barray_type,
7877 false, false, true, NULL);
7878 return zvar;
7879 }
7880
7881 // Traversal.
7882
7883 int
do_traverse(Traverse * traverse)7884 Map_type::do_traverse(Traverse* traverse)
7885 {
7886 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
7887 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
7888 return TRAVERSE_EXIT;
7889 return TRAVERSE_CONTINUE;
7890 }
7891
7892 // Check that the map type is OK.
7893
7894 bool
do_verify()7895 Map_type::do_verify()
7896 {
7897 // The runtime support uses "map[void]void".
7898 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
7899 go_error_at(this->location_, "invalid map key type");
7900 if (!this->key_type_->in_heap())
7901 go_error_at(this->location_, "go:notinheap map key not allowed");
7902 if (!this->val_type_->in_heap())
7903 go_error_at(this->location_, "go:notinheap map value not allowed");
7904 return true;
7905 }
7906
7907 // Whether two map types are identical.
7908
7909 bool
is_identical(const Map_type * t,int flags) const7910 Map_type::is_identical(const Map_type* t, int flags) const
7911 {
7912 return (Type::are_identical(this->key_type(), t->key_type(), flags, NULL)
7913 && Type::are_identical(this->val_type(), t->val_type(), flags,
7914 NULL));
7915 }
7916
7917 // Hash code.
7918
7919 unsigned int
do_hash_for_method(Gogo * gogo,int flags) const7920 Map_type::do_hash_for_method(Gogo* gogo, int flags) const
7921 {
7922 return (this->key_type_->hash_for_method(gogo, flags)
7923 + this->val_type_->hash_for_method(gogo, flags)
7924 + 2);
7925 }
7926
7927 // Get the backend representation for a map type. A map type is
7928 // represented as a pointer to a struct. The struct is hmap in
7929 // runtime/hashmap.go.
7930
7931 Btype*
do_get_backend(Gogo * gogo)7932 Map_type::do_get_backend(Gogo* gogo)
7933 {
7934 static Btype* backend_map_type;
7935 if (backend_map_type == NULL)
7936 {
7937 std::vector<Backend::Btyped_identifier> bfields(9);
7938
7939 Location bloc = Linemap::predeclared_location();
7940
7941 Type* int_type = Type::lookup_integer_type("int");
7942 bfields[0].name = "count";
7943 bfields[0].btype = int_type->get_backend(gogo);
7944 bfields[0].location = bloc;
7945
7946 Type* uint8_type = Type::lookup_integer_type("uint8");
7947 bfields[1].name = "flags";
7948 bfields[1].btype = uint8_type->get_backend(gogo);
7949 bfields[1].location = bloc;
7950
7951 bfields[2].name = "B";
7952 bfields[2].btype = bfields[1].btype;
7953 bfields[2].location = bloc;
7954
7955 Type* uint16_type = Type::lookup_integer_type("uint16");
7956 bfields[3].name = "noverflow";
7957 bfields[3].btype = uint16_type->get_backend(gogo);
7958 bfields[3].location = bloc;
7959
7960 Type* uint32_type = Type::lookup_integer_type("uint32");
7961 bfields[4].name = "hash0";
7962 bfields[4].btype = uint32_type->get_backend(gogo);
7963 bfields[4].location = bloc;
7964
7965 Btype* bvt = gogo->backend()->void_type();
7966 Btype* bpvt = gogo->backend()->pointer_type(bvt);
7967 bfields[5].name = "buckets";
7968 bfields[5].btype = bpvt;
7969 bfields[5].location = bloc;
7970
7971 bfields[6].name = "oldbuckets";
7972 bfields[6].btype = bpvt;
7973 bfields[6].location = bloc;
7974
7975 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7976 bfields[7].name = "nevacuate";
7977 bfields[7].btype = uintptr_type->get_backend(gogo);
7978 bfields[7].location = bloc;
7979
7980 bfields[8].name = "extra";
7981 bfields[8].btype = bpvt;
7982 bfields[8].location = bloc;
7983
7984 Btype *bt = gogo->backend()->struct_type(bfields);
7985 bt = gogo->backend()->named_type("runtime.hmap", bt, bloc);
7986 backend_map_type = gogo->backend()->pointer_type(bt);
7987 }
7988 return backend_map_type;
7989 }
7990
7991 // The type of a map type descriptor.
7992
7993 Type*
make_map_type_descriptor_type()7994 Map_type::make_map_type_descriptor_type()
7995 {
7996 static Type* ret;
7997 if (ret == NULL)
7998 {
7999 Type* tdt = Type::make_type_descriptor_type();
8000 Type* ptdt = Type::make_type_descriptor_ptr_type();
8001 Type* uint8_type = Type::lookup_integer_type("uint8");
8002 Type* uint16_type = Type::lookup_integer_type("uint16");
8003 Type* uint32_type = Type::lookup_integer_type("uint32");
8004
8005 Struct_type* sf =
8006 Type::make_builtin_struct_type(8,
8007 "", tdt,
8008 "key", ptdt,
8009 "elem", ptdt,
8010 "bucket", ptdt,
8011 "keysize", uint8_type,
8012 "valuesize", uint8_type,
8013 "bucketsize", uint16_type,
8014 "flags", uint32_type);
8015
8016 ret = Type::make_builtin_named_type("MapType", sf);
8017 }
8018
8019 return ret;
8020 }
8021
8022 // Build a type descriptor for a map type.
8023
8024 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)8025 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8026 {
8027 Location bloc = Linemap::predeclared_location();
8028
8029 Type* mtdt = Map_type::make_map_type_descriptor_type();
8030 Type* uint8_type = Type::lookup_integer_type("uint8");
8031 Type* uint16_type = Type::lookup_integer_type("uint16");
8032 Type* uint32_type = Type::lookup_integer_type("uint32");
8033
8034 int64_t keysize;
8035 if (!this->key_type_->backend_type_size(gogo, &keysize))
8036 {
8037 go_error_at(this->location_, "error determining map key type size");
8038 return Expression::make_error(this->location_);
8039 }
8040
8041 int64_t valsize;
8042 if (!this->val_type_->backend_type_size(gogo, &valsize))
8043 {
8044 go_error_at(this->location_, "error determining map value type size");
8045 return Expression::make_error(this->location_);
8046 }
8047
8048 int64_t ptrsize;
8049 if (!Type::make_pointer_type(uint8_type)->backend_type_size(gogo, &ptrsize))
8050 {
8051 go_assert(saw_errors());
8052 return Expression::make_error(this->location_);
8053 }
8054
8055 Type* bucket_type = this->bucket_type(gogo, keysize, valsize);
8056 if (bucket_type == NULL)
8057 {
8058 go_assert(saw_errors());
8059 return Expression::make_error(this->location_);
8060 }
8061
8062 int64_t bucketsize;
8063 if (!bucket_type->backend_type_size(gogo, &bucketsize))
8064 {
8065 go_assert(saw_errors());
8066 return Expression::make_error(this->location_);
8067 }
8068
8069 const Struct_field_list* fields = mtdt->struct_type()->fields();
8070
8071 Expression_list* vals = new Expression_list();
8072 vals->reserve(12);
8073
8074 Struct_field_list::const_iterator p = fields->begin();
8075 go_assert(p->is_field_name("_type"));
8076 vals->push_back(this->type_descriptor_constructor(gogo,
8077 RUNTIME_TYPE_KIND_MAP,
8078 name, NULL, true));
8079
8080 ++p;
8081 go_assert(p->is_field_name("key"));
8082 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
8083
8084 ++p;
8085 go_assert(p->is_field_name("elem"));
8086 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
8087
8088 ++p;
8089 go_assert(p->is_field_name("bucket"));
8090 vals->push_back(Expression::make_type_descriptor(bucket_type, bloc));
8091
8092 ++p;
8093 go_assert(p->is_field_name("keysize"));
8094 if (keysize > Map_type::max_key_size)
8095 vals->push_back(Expression::make_integer_int64(ptrsize, uint8_type, bloc));
8096 else
8097 vals->push_back(Expression::make_integer_int64(keysize, uint8_type, bloc));
8098
8099 ++p;
8100 go_assert(p->is_field_name("valuesize"));
8101 if (valsize > Map_type::max_val_size)
8102 vals->push_back(Expression::make_integer_int64(ptrsize, uint8_type, bloc));
8103 else
8104 vals->push_back(Expression::make_integer_int64(valsize, uint8_type, bloc));
8105
8106 ++p;
8107 go_assert(p->is_field_name("bucketsize"));
8108 vals->push_back(Expression::make_integer_int64(bucketsize, uint16_type,
8109 bloc));
8110
8111 ++p;
8112 go_assert(p->is_field_name("flags"));
8113 // As with the other fields, the flag bits must match the reflect
8114 // and runtime packages.
8115 unsigned long flags = 0;
8116 if (keysize > Map_type::max_key_size)
8117 flags |= 1;
8118 if (valsize > Map_type::max_val_size)
8119 flags |= 2;
8120 if (this->key_type_->is_reflexive())
8121 flags |= 4;
8122 if (this->key_type_->needs_key_update())
8123 flags |= 8;
8124 if (this->key_type_->hash_might_panic())
8125 flags |= 16;
8126 vals->push_back(Expression::make_integer_ul(flags, uint32_type, bloc));
8127
8128 ++p;
8129 go_assert(p == fields->end());
8130
8131 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
8132 }
8133
8134 // Return the bucket type to use for a map type. This must correspond
8135 // to libgo/go/runtime/hashmap.go.
8136
8137 Type*
bucket_type(Gogo * gogo,int64_t keysize,int64_t valsize)8138 Map_type::bucket_type(Gogo* gogo, int64_t keysize, int64_t valsize)
8139 {
8140 if (this->bucket_type_ != NULL)
8141 return this->bucket_type_;
8142
8143 Type* key_type = this->key_type_;
8144 if (keysize > Map_type::max_key_size)
8145 key_type = Type::make_pointer_type(key_type);
8146
8147 Type* val_type = this->val_type_;
8148 if (valsize > Map_type::max_val_size)
8149 val_type = Type::make_pointer_type(val_type);
8150
8151 Expression* bucket_size = Expression::make_integer_ul(Map_type::bucket_size,
8152 NULL, this->location_);
8153
8154 Type* uint8_type = Type::lookup_integer_type("uint8");
8155 Array_type* topbits_type = Type::make_array_type(uint8_type, bucket_size);
8156 topbits_type->set_is_array_incomparable();
8157 Array_type* keys_type = Type::make_array_type(key_type, bucket_size);
8158 keys_type->set_is_array_incomparable();
8159 Array_type* values_type = Type::make_array_type(val_type, bucket_size);
8160 values_type->set_is_array_incomparable();
8161
8162 // If keys and values have no pointers, the map implementation can
8163 // keep a list of overflow pointers on the side so that buckets can
8164 // be marked as having no pointers. Arrange for the bucket to have
8165 // no pointers by changing the type of the overflow field to uintptr
8166 // in this case. See comment on the hmap.overflow field in
8167 // libgo/go/runtime/hashmap.go.
8168 Type* overflow_type;
8169 if (!key_type->has_pointer() && !val_type->has_pointer())
8170 overflow_type = Type::lookup_integer_type("uintptr");
8171 else
8172 {
8173 // This should really be a pointer to the bucket type itself,
8174 // but that would require us to construct a Named_type for it to
8175 // give it a way to refer to itself. Since nothing really cares
8176 // (except perhaps for someone using a debugger) just use an
8177 // unsafe pointer.
8178 overflow_type = Type::make_pointer_type(Type::make_void_type());
8179 }
8180
8181 // Make sure the overflow pointer is the last memory in the struct,
8182 // because the runtime assumes it can use size-ptrSize as the offset
8183 // of the overflow pointer. We double-check that property below
8184 // once the offsets and size are computed.
8185
8186 int64_t topbits_field_size, topbits_field_align;
8187 int64_t keys_field_size, keys_field_align;
8188 int64_t values_field_size, values_field_align;
8189 int64_t overflow_field_size, overflow_field_align;
8190 if (!topbits_type->backend_type_size(gogo, &topbits_field_size)
8191 || !topbits_type->backend_type_field_align(gogo, &topbits_field_align)
8192 || !keys_type->backend_type_size(gogo, &keys_field_size)
8193 || !keys_type->backend_type_field_align(gogo, &keys_field_align)
8194 || !values_type->backend_type_size(gogo, &values_field_size)
8195 || !values_type->backend_type_field_align(gogo, &values_field_align)
8196 || !overflow_type->backend_type_size(gogo, &overflow_field_size)
8197 || !overflow_type->backend_type_field_align(gogo, &overflow_field_align))
8198 {
8199 go_assert(saw_errors());
8200 return NULL;
8201 }
8202
8203 Struct_type* ret;
8204 int64_t max_align = std::max(std::max(topbits_field_align, keys_field_align),
8205 values_field_align);
8206 if (max_align <= overflow_field_align)
8207 ret = make_builtin_struct_type(4,
8208 "topbits", topbits_type,
8209 "keys", keys_type,
8210 "values", values_type,
8211 "overflow", overflow_type);
8212 else
8213 {
8214 size_t off = topbits_field_size;
8215 off = ((off + keys_field_align - 1)
8216 &~ static_cast<size_t>(keys_field_align - 1));
8217 off += keys_field_size;
8218 off = ((off + values_field_align - 1)
8219 &~ static_cast<size_t>(values_field_align - 1));
8220 off += values_field_size;
8221
8222 int64_t padded_overflow_field_size =
8223 ((overflow_field_size + max_align - 1)
8224 &~ static_cast<size_t>(max_align - 1));
8225
8226 size_t ovoff = off;
8227 ovoff = ((ovoff + max_align - 1)
8228 &~ static_cast<size_t>(max_align - 1));
8229 size_t pad = (ovoff - off
8230 + padded_overflow_field_size - overflow_field_size);
8231
8232 Expression* pad_expr = Expression::make_integer_ul(pad, NULL,
8233 this->location_);
8234 Array_type* pad_type = Type::make_array_type(uint8_type, pad_expr);
8235 pad_type->set_is_array_incomparable();
8236
8237 ret = make_builtin_struct_type(5,
8238 "topbits", topbits_type,
8239 "keys", keys_type,
8240 "values", values_type,
8241 "pad", pad_type,
8242 "overflow", overflow_type);
8243 }
8244
8245 // Verify that the overflow field is just before the end of the
8246 // bucket type.
8247
8248 Btype* btype = ret->get_backend(gogo);
8249 int64_t offset = gogo->backend()->type_field_offset(btype,
8250 ret->field_count() - 1);
8251 int64_t size;
8252 if (!ret->backend_type_size(gogo, &size))
8253 {
8254 go_assert(saw_errors());
8255 return NULL;
8256 }
8257
8258 int64_t ptr_size;
8259 if (!Type::make_pointer_type(uint8_type)->backend_type_size(gogo, &ptr_size))
8260 {
8261 go_assert(saw_errors());
8262 return NULL;
8263 }
8264
8265 go_assert(offset + ptr_size == size);
8266
8267 ret->set_is_struct_incomparable();
8268
8269 this->bucket_type_ = ret;
8270 return ret;
8271 }
8272
8273 // Return the hashmap type for a map type.
8274
8275 Type*
hmap_type(Type * bucket_type)8276 Map_type::hmap_type(Type* bucket_type)
8277 {
8278 if (this->hmap_type_ != NULL)
8279 return this->hmap_type_;
8280
8281 Type* int_type = Type::lookup_integer_type("int");
8282 Type* uint8_type = Type::lookup_integer_type("uint8");
8283 Type* uint16_type = Type::lookup_integer_type("uint16");
8284 Type* uint32_type = Type::lookup_integer_type("uint32");
8285 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8286 Type* void_ptr_type = Type::make_pointer_type(Type::make_void_type());
8287
8288 Type* ptr_bucket_type = Type::make_pointer_type(bucket_type);
8289
8290 Struct_type* ret = make_builtin_struct_type(9,
8291 "count", int_type,
8292 "flags", uint8_type,
8293 "B", uint8_type,
8294 "noverflow", uint16_type,
8295 "hash0", uint32_type,
8296 "buckets", ptr_bucket_type,
8297 "oldbuckets", ptr_bucket_type,
8298 "nevacuate", uintptr_type,
8299 "extra", void_ptr_type);
8300 ret->set_is_struct_incomparable();
8301 this->hmap_type_ = ret;
8302 return ret;
8303 }
8304
8305 // Return the iterator type for a map type. This is the type of the
8306 // value used when doing a range over a map.
8307
8308 Type*
hiter_type(Gogo * gogo)8309 Map_type::hiter_type(Gogo* gogo)
8310 {
8311 if (this->hiter_type_ != NULL)
8312 return this->hiter_type_;
8313
8314 int64_t keysize, valsize;
8315 if (!this->key_type_->backend_type_size(gogo, &keysize)
8316 || !this->val_type_->backend_type_size(gogo, &valsize))
8317 {
8318 go_assert(saw_errors());
8319 return NULL;
8320 }
8321
8322 Type* key_ptr_type = Type::make_pointer_type(this->key_type_);
8323 Type* val_ptr_type = Type::make_pointer_type(this->val_type_);
8324 Type* uint8_type = Type::lookup_integer_type("uint8");
8325 Type* uint8_ptr_type = Type::make_pointer_type(uint8_type);
8326 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8327 Type* bucket_type = this->bucket_type(gogo, keysize, valsize);
8328 Type* bucket_ptr_type = Type::make_pointer_type(bucket_type);
8329 Type* hmap_type = this->hmap_type(bucket_type);
8330 Type* hmap_ptr_type = Type::make_pointer_type(hmap_type);
8331 Type* void_ptr_type = Type::make_pointer_type(Type::make_void_type());
8332 Type* bool_type = Type::lookup_bool_type();
8333
8334 Struct_type* ret = make_builtin_struct_type(15,
8335 "key", key_ptr_type,
8336 "val", val_ptr_type,
8337 "t", uint8_ptr_type,
8338 "h", hmap_ptr_type,
8339 "buckets", bucket_ptr_type,
8340 "bptr", bucket_ptr_type,
8341 "overflow", void_ptr_type,
8342 "oldoverflow", void_ptr_type,
8343 "startBucket", uintptr_type,
8344 "offset", uint8_type,
8345 "wrapped", bool_type,
8346 "B", uint8_type,
8347 "i", uint8_type,
8348 "bucket", uintptr_type,
8349 "checkBucket", uintptr_type);
8350 ret->set_is_struct_incomparable();
8351 this->hiter_type_ = ret;
8352 return ret;
8353 }
8354
8355 // Reflection string for a map.
8356
8357 void
do_reflection(Gogo * gogo,std::string * ret) const8358 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
8359 {
8360 ret->append("map[");
8361 this->append_reflection(this->key_type_, gogo, ret);
8362 ret->append("]");
8363 this->append_reflection(this->val_type_, gogo, ret);
8364 }
8365
8366 // Export a map type.
8367
8368 void
do_export(Export * exp) const8369 Map_type::do_export(Export* exp) const
8370 {
8371 exp->write_c_string("map [");
8372 exp->write_type(this->key_type_);
8373 exp->write_c_string("] ");
8374 exp->write_type(this->val_type_);
8375 }
8376
8377 // Import a map type.
8378
8379 Map_type*
do_import(Import * imp)8380 Map_type::do_import(Import* imp)
8381 {
8382 imp->require_c_string("map [");
8383 Type* key_type = imp->read_type();
8384 imp->require_c_string("] ");
8385 Type* val_type = imp->read_type();
8386 return Type::make_map_type(key_type, val_type, imp->location());
8387 }
8388
8389 // Make a map type.
8390
8391 Map_type*
make_map_type(Type * key_type,Type * val_type,Location location)8392 Type::make_map_type(Type* key_type, Type* val_type, Location location)
8393 {
8394 return new Map_type(key_type, val_type, location);
8395 }
8396
8397 // Class Channel_type.
8398
8399 // Verify.
8400
8401 bool
do_verify()8402 Channel_type::do_verify()
8403 {
8404 // We have no location for this error, but this is not something the
8405 // ordinary user will see.
8406 if (!this->element_type_->in_heap())
8407 go_error_at(Linemap::unknown_location(),
8408 "chan of go:notinheap type not allowed");
8409 return true;
8410 }
8411
8412 // Hash code.
8413
8414 unsigned int
do_hash_for_method(Gogo * gogo,int flags) const8415 Channel_type::do_hash_for_method(Gogo* gogo, int flags) const
8416 {
8417 unsigned int ret = 0;
8418 if (this->may_send_)
8419 ret += 1;
8420 if (this->may_receive_)
8421 ret += 2;
8422 if (this->element_type_ != NULL)
8423 ret += this->element_type_->hash_for_method(gogo, flags) << 2;
8424 return ret << 3;
8425 }
8426
8427 // Whether this type is the same as T.
8428
8429 bool
is_identical(const Channel_type * t,int flags) const8430 Channel_type::is_identical(const Channel_type* t, int flags) const
8431 {
8432 if (!Type::are_identical(this->element_type(), t->element_type(), flags,
8433 NULL))
8434 return false;
8435 return (this->may_send_ == t->may_send_
8436 && this->may_receive_ == t->may_receive_);
8437 }
8438
8439 // Return the backend representation for a channel type. A channel is a pointer
8440 // to a __go_channel struct. The __go_channel struct is defined in
8441 // libgo/runtime/channel.h.
8442
8443 Btype*
do_get_backend(Gogo * gogo)8444 Channel_type::do_get_backend(Gogo* gogo)
8445 {
8446 static Btype* backend_channel_type;
8447 if (backend_channel_type == NULL)
8448 {
8449 std::vector<Backend::Btyped_identifier> bfields;
8450 Btype* bt = gogo->backend()->struct_type(bfields);
8451 bt = gogo->backend()->named_type("__go_channel", bt,
8452 Linemap::predeclared_location());
8453 backend_channel_type = gogo->backend()->pointer_type(bt);
8454 }
8455 return backend_channel_type;
8456 }
8457
8458 // Build a type descriptor for a channel type.
8459
8460 Type*
make_chan_type_descriptor_type()8461 Channel_type::make_chan_type_descriptor_type()
8462 {
8463 static Type* ret;
8464 if (ret == NULL)
8465 {
8466 Type* tdt = Type::make_type_descriptor_type();
8467 Type* ptdt = Type::make_type_descriptor_ptr_type();
8468
8469 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8470
8471 Struct_type* sf =
8472 Type::make_builtin_struct_type(3,
8473 "", tdt,
8474 "elem", ptdt,
8475 "dir", uintptr_type);
8476
8477 ret = Type::make_builtin_named_type("ChanType", sf);
8478 }
8479
8480 return ret;
8481 }
8482
8483 // Build a type descriptor for a map type.
8484
8485 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)8486 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8487 {
8488 Location bloc = Linemap::predeclared_location();
8489
8490 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
8491
8492 const Struct_field_list* fields = ctdt->struct_type()->fields();
8493
8494 Expression_list* vals = new Expression_list();
8495 vals->reserve(3);
8496
8497 Struct_field_list::const_iterator p = fields->begin();
8498 go_assert(p->is_field_name("_type"));
8499 vals->push_back(this->type_descriptor_constructor(gogo,
8500 RUNTIME_TYPE_KIND_CHAN,
8501 name, NULL, true));
8502
8503 ++p;
8504 go_assert(p->is_field_name("elem"));
8505 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
8506
8507 ++p;
8508 go_assert(p->is_field_name("dir"));
8509 // These bits must match the ones in libgo/runtime/go-type.h.
8510 int val = 0;
8511 if (this->may_receive_)
8512 val |= 1;
8513 if (this->may_send_)
8514 val |= 2;
8515 vals->push_back(Expression::make_integer_ul(val, p->type(), bloc));
8516
8517 ++p;
8518 go_assert(p == fields->end());
8519
8520 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
8521 }
8522
8523 // Reflection string.
8524
8525 void
do_reflection(Gogo * gogo,std::string * ret) const8526 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
8527 {
8528 if (!this->may_send_)
8529 ret->append("<-");
8530 ret->append("chan");
8531 if (!this->may_receive_)
8532 ret->append("<-");
8533 ret->push_back(' ');
8534 this->append_reflection(this->element_type_, gogo, ret);
8535 }
8536
8537 // Export.
8538
8539 void
do_export(Export * exp) const8540 Channel_type::do_export(Export* exp) const
8541 {
8542 exp->write_c_string("chan ");
8543 if (this->may_send_ && !this->may_receive_)
8544 exp->write_c_string("-< ");
8545 else if (this->may_receive_ && !this->may_send_)
8546 exp->write_c_string("<- ");
8547 exp->write_type(this->element_type_);
8548 }
8549
8550 // Import.
8551
8552 Channel_type*
do_import(Import * imp)8553 Channel_type::do_import(Import* imp)
8554 {
8555 imp->require_c_string("chan ");
8556
8557 bool may_send;
8558 bool may_receive;
8559 if (imp->match_c_string("-< "))
8560 {
8561 imp->advance(3);
8562 may_send = true;
8563 may_receive = false;
8564 }
8565 else if (imp->match_c_string("<- "))
8566 {
8567 imp->advance(3);
8568 may_receive = true;
8569 may_send = false;
8570 }
8571 else
8572 {
8573 may_send = true;
8574 may_receive = true;
8575 }
8576
8577 Type* element_type = imp->read_type();
8578
8579 return Type::make_channel_type(may_send, may_receive, element_type);
8580 }
8581
8582 // Return the type that the runtime package uses for one case of a
8583 // select statement. An array of values of this type is allocated on
8584 // the stack. This must match scase in libgo/go/runtime/select.go.
8585
8586 Type*
select_case_type()8587 Channel_type::select_case_type()
8588 {
8589 static Struct_type* scase_type;
8590 if (scase_type == NULL)
8591 {
8592 Type* unsafe_pointer_type =
8593 Type::make_pointer_type(Type::make_void_type());
8594 Type* uint16_type = Type::lookup_integer_type("uint16");
8595 Type* int64_type = Type::lookup_integer_type("int64");
8596 scase_type =
8597 Type::make_builtin_struct_type(4,
8598 "c", unsafe_pointer_type,
8599 "elem", unsafe_pointer_type,
8600 "kind", uint16_type,
8601 "releasetime", int64_type);
8602 scase_type->set_is_struct_incomparable();
8603 }
8604 return scase_type;
8605 }
8606
8607 // Make a new channel type.
8608
8609 Channel_type*
make_channel_type(bool send,bool receive,Type * element_type)8610 Type::make_channel_type(bool send, bool receive, Type* element_type)
8611 {
8612 return new Channel_type(send, receive, element_type);
8613 }
8614
8615 // Class Interface_type.
8616
8617 // Return the list of methods.
8618
8619 const Typed_identifier_list*
methods() const8620 Interface_type::methods() const
8621 {
8622 go_assert(this->methods_are_finalized_ || saw_errors());
8623 return this->all_methods_;
8624 }
8625
8626 // Return the number of methods.
8627
8628 size_t
method_count() const8629 Interface_type::method_count() const
8630 {
8631 go_assert(this->methods_are_finalized_ || saw_errors());
8632 return this->all_methods_ == NULL ? 0 : this->all_methods_->size();
8633 }
8634
8635 // Traversal.
8636
8637 int
do_traverse(Traverse * traverse)8638 Interface_type::do_traverse(Traverse* traverse)
8639 {
8640 Typed_identifier_list* methods = (this->methods_are_finalized_
8641 ? this->all_methods_
8642 : this->parse_methods_);
8643 if (methods == NULL)
8644 return TRAVERSE_CONTINUE;
8645 return methods->traverse(traverse);
8646 }
8647
8648 // Finalize the methods. This handles interface inheritance.
8649
8650 void
finalize_methods()8651 Interface_type::finalize_methods()
8652 {
8653 if (this->methods_are_finalized_)
8654 return;
8655 this->methods_are_finalized_ = true;
8656 if (this->parse_methods_ == NULL)
8657 return;
8658
8659 this->all_methods_ = new Typed_identifier_list();
8660 this->all_methods_->reserve(this->parse_methods_->size());
8661 Typed_identifier_list inherit;
8662 for (Typed_identifier_list::const_iterator pm =
8663 this->parse_methods_->begin();
8664 pm != this->parse_methods_->end();
8665 ++pm)
8666 {
8667 const Typed_identifier* p = &*pm;
8668 if (p->name().empty())
8669 inherit.push_back(*p);
8670 else if (this->find_method(p->name()) == NULL)
8671 this->all_methods_->push_back(*p);
8672 else
8673 go_error_at(p->location(), "duplicate method %qs",
8674 Gogo::message_name(p->name()).c_str());
8675 }
8676
8677 std::vector<Named_type*> seen;
8678 seen.reserve(inherit.size());
8679 bool issued_recursive_error = false;
8680 while (!inherit.empty())
8681 {
8682 Type* t = inherit.back().type();
8683 Location tl = inherit.back().location();
8684 inherit.pop_back();
8685
8686 Interface_type* it = t->interface_type();
8687 if (it == NULL)
8688 {
8689 if (!t->is_error())
8690 go_error_at(tl, "interface contains embedded non-interface");
8691 continue;
8692 }
8693 if (it == this)
8694 {
8695 if (!issued_recursive_error)
8696 {
8697 go_error_at(tl, "invalid recursive interface");
8698 issued_recursive_error = true;
8699 }
8700 continue;
8701 }
8702
8703 Named_type* nt = t->named_type();
8704 if (nt != NULL && it->parse_methods_ != NULL)
8705 {
8706 std::vector<Named_type*>::const_iterator q;
8707 for (q = seen.begin(); q != seen.end(); ++q)
8708 {
8709 if (*q == nt)
8710 {
8711 go_error_at(tl, "inherited interface loop");
8712 break;
8713 }
8714 }
8715 if (q != seen.end())
8716 continue;
8717 seen.push_back(nt);
8718 }
8719
8720 const Typed_identifier_list* imethods = it->parse_methods_;
8721 if (imethods == NULL)
8722 continue;
8723 for (Typed_identifier_list::const_iterator q = imethods->begin();
8724 q != imethods->end();
8725 ++q)
8726 {
8727 if (q->name().empty())
8728 inherit.push_back(*q);
8729 else if (this->find_method(q->name()) == NULL)
8730 this->all_methods_->push_back(Typed_identifier(q->name(),
8731 q->type(), tl));
8732 else
8733 go_error_at(tl, "inherited method %qs is ambiguous",
8734 Gogo::message_name(q->name()).c_str());
8735 }
8736 }
8737
8738 if (!this->all_methods_->empty())
8739 this->all_methods_->sort_by_name();
8740 else
8741 {
8742 delete this->all_methods_;
8743 this->all_methods_ = NULL;
8744 }
8745 }
8746
8747 // Return the method NAME, or NULL.
8748
8749 const Typed_identifier*
find_method(const std::string & name) const8750 Interface_type::find_method(const std::string& name) const
8751 {
8752 go_assert(this->methods_are_finalized_);
8753 if (this->all_methods_ == NULL)
8754 return NULL;
8755 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8756 p != this->all_methods_->end();
8757 ++p)
8758 if (p->name() == name)
8759 return &*p;
8760 return NULL;
8761 }
8762
8763 // Return the method index.
8764
8765 size_t
method_index(const std::string & name) const8766 Interface_type::method_index(const std::string& name) const
8767 {
8768 go_assert(this->methods_are_finalized_ && this->all_methods_ != NULL);
8769 size_t ret = 0;
8770 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8771 p != this->all_methods_->end();
8772 ++p, ++ret)
8773 if (p->name() == name)
8774 return ret;
8775 go_unreachable();
8776 }
8777
8778 // Return whether NAME is an unexported method, for better error
8779 // reporting.
8780
8781 bool
is_unexported_method(Gogo * gogo,const std::string & name) const8782 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
8783 {
8784 go_assert(this->methods_are_finalized_);
8785 if (this->all_methods_ == NULL)
8786 return false;
8787 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8788 p != this->all_methods_->end();
8789 ++p)
8790 {
8791 const std::string& method_name(p->name());
8792 if (Gogo::is_hidden_name(method_name)
8793 && name == Gogo::unpack_hidden_name(method_name)
8794 && gogo->pack_hidden_name(name, false) != method_name)
8795 return true;
8796 }
8797 return false;
8798 }
8799
8800 // Whether this type is identical with T.
8801
8802 bool
is_identical(const Interface_type * t,int flags) const8803 Interface_type::is_identical(const Interface_type* t, int flags) const
8804 {
8805 // If methods have not been finalized, then we are asking whether
8806 // func redeclarations are the same. This is an error, so for
8807 // simplicity we say they are never the same.
8808 if (!this->methods_are_finalized_ || !t->methods_are_finalized_)
8809 return false;
8810
8811 // Consult a flag to see whether we need to compare based on
8812 // parse methods or all methods.
8813 Typed_identifier_list* methods = (((flags & COMPARE_EMBEDDED_INTERFACES) != 0)
8814 ? this->parse_methods_
8815 : this->all_methods_);
8816 Typed_identifier_list* tmethods = (((flags & COMPARE_EMBEDDED_INTERFACES) != 0)
8817 ? t->parse_methods_
8818 : t->all_methods_);
8819
8820 // We require the same methods with the same types. The methods
8821 // have already been sorted.
8822 if (methods == NULL || tmethods == NULL)
8823 return methods == tmethods;
8824
8825 if (this->assume_identical(this, t) || t->assume_identical(t, this))
8826 return true;
8827
8828 Assume_identical* hold_ai = this->assume_identical_;
8829 Assume_identical ai;
8830 ai.t1 = this;
8831 ai.t2 = t;
8832 ai.next = hold_ai;
8833 this->assume_identical_ = &ai;
8834
8835 Typed_identifier_list::const_iterator p1 = methods->begin();
8836 Typed_identifier_list::const_iterator p2;
8837 for (p2 = tmethods->begin(); p2 != tmethods->end(); ++p1, ++p2)
8838 {
8839 if (p1 == methods->end())
8840 break;
8841 if (p1->name() != p2->name()
8842 || !Type::are_identical(p1->type(), p2->type(), flags, NULL))
8843 break;
8844 }
8845
8846 this->assume_identical_ = hold_ai;
8847
8848 return p1 == methods->end() && p2 == tmethods->end();
8849 }
8850
8851 // Return true if T1 and T2 are assumed to be identical during a type
8852 // comparison.
8853
8854 bool
assume_identical(const Interface_type * t1,const Interface_type * t2) const8855 Interface_type::assume_identical(const Interface_type* t1,
8856 const Interface_type* t2) const
8857 {
8858 for (Assume_identical* p = this->assume_identical_;
8859 p != NULL;
8860 p = p->next)
8861 if ((p->t1 == t1 && p->t2 == t2) || (p->t1 == t2 && p->t2 == t1))
8862 return true;
8863 return false;
8864 }
8865
8866 // Whether we can assign the interface type T to this type. The types
8867 // are known to not be identical. An interface assignment is only
8868 // permitted if T is known to implement all methods in THIS.
8869 // Otherwise a type guard is required.
8870
8871 bool
is_compatible_for_assign(const Interface_type * t,std::string * reason) const8872 Interface_type::is_compatible_for_assign(const Interface_type* t,
8873 std::string* reason) const
8874 {
8875 go_assert(this->methods_are_finalized_ && t->methods_are_finalized_);
8876 if (this->all_methods_ == NULL)
8877 return true;
8878 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
8879 p != this->all_methods_->end();
8880 ++p)
8881 {
8882 const Typed_identifier* m = t->find_method(p->name());
8883 if (m == NULL)
8884 {
8885 if (reason != NULL)
8886 {
8887 char buf[200];
8888 snprintf(buf, sizeof buf,
8889 _("need explicit conversion; missing method %s%s%s"),
8890 go_open_quote(), Gogo::message_name(p->name()).c_str(),
8891 go_close_quote());
8892 reason->assign(buf);
8893 }
8894 return false;
8895 }
8896
8897 std::string subreason;
8898 if (!Type::are_identical(p->type(), m->type(), Type::COMPARE_TAGS,
8899 &subreason))
8900 {
8901 if (reason != NULL)
8902 {
8903 std::string n = Gogo::message_name(p->name());
8904 size_t len = 100 + n.length() + subreason.length();
8905 char* buf = new char[len];
8906 if (subreason.empty())
8907 snprintf(buf, len, _("incompatible type for method %s%s%s"),
8908 go_open_quote(), n.c_str(), go_close_quote());
8909 else
8910 snprintf(buf, len,
8911 _("incompatible type for method %s%s%s (%s)"),
8912 go_open_quote(), n.c_str(), go_close_quote(),
8913 subreason.c_str());
8914 reason->assign(buf);
8915 delete[] buf;
8916 }
8917 return false;
8918 }
8919 }
8920
8921 return true;
8922 }
8923
8924 // Hash code.
8925
8926 unsigned int
do_hash_for_method(Gogo *,int) const8927 Interface_type::do_hash_for_method(Gogo*, int) const
8928 {
8929 go_assert(this->methods_are_finalized_);
8930 unsigned int ret = 0;
8931 if (this->all_methods_ != NULL)
8932 {
8933 for (Typed_identifier_list::const_iterator p =
8934 this->all_methods_->begin();
8935 p != this->all_methods_->end();
8936 ++p)
8937 {
8938 ret = Gogo::hash_string(p->name(), ret);
8939 // We don't use the method type in the hash, to avoid
8940 // infinite recursion if an interface method uses a type
8941 // which is an interface which inherits from the interface
8942 // itself.
8943 // type T interface { F() interface {T}}
8944 ret <<= 1;
8945 }
8946 }
8947 return ret;
8948 }
8949
8950 // Return true if T implements the interface. If it does not, and
8951 // REASON is not NULL, set *REASON to a useful error message.
8952
8953 bool
implements_interface(const Type * t,std::string * reason) const8954 Interface_type::implements_interface(const Type* t, std::string* reason) const
8955 {
8956 go_assert(this->methods_are_finalized_);
8957 if (this->all_methods_ == NULL)
8958 return true;
8959
8960 bool is_pointer = false;
8961 const Named_type* nt = t->named_type();
8962 const Struct_type* st = t->struct_type();
8963 // If we start with a named type, we don't dereference it to find
8964 // methods.
8965 if (nt == NULL)
8966 {
8967 const Type* pt = t->points_to();
8968 if (pt != NULL)
8969 {
8970 // If T is a pointer to a named type, then we need to look at
8971 // the type to which it points.
8972 is_pointer = true;
8973 nt = pt->named_type();
8974 st = pt->struct_type();
8975 }
8976 }
8977
8978 // If we have a named type, get the methods from it rather than from
8979 // any struct type.
8980 if (nt != NULL)
8981 st = NULL;
8982
8983 // Only named and struct types have methods.
8984 if (nt == NULL && st == NULL)
8985 {
8986 if (reason != NULL)
8987 {
8988 if (t->points_to() != NULL
8989 && t->points_to()->interface_type() != NULL)
8990 reason->assign(_("pointer to interface type has no methods"));
8991 else
8992 reason->assign(_("type has no methods"));
8993 }
8994 return false;
8995 }
8996
8997 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
8998 {
8999 if (reason != NULL)
9000 {
9001 if (t->points_to() != NULL
9002 && t->points_to()->interface_type() != NULL)
9003 reason->assign(_("pointer to interface type has no methods"));
9004 else
9005 reason->assign(_("type has no methods"));
9006 }
9007 return false;
9008 }
9009
9010 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9011 p != this->all_methods_->end();
9012 ++p)
9013 {
9014 bool is_ambiguous = false;
9015 Method* m = (nt != NULL
9016 ? nt->method_function(p->name(), &is_ambiguous)
9017 : st->method_function(p->name(), &is_ambiguous));
9018 if (m == NULL)
9019 {
9020 if (reason != NULL)
9021 {
9022 std::string n = Gogo::message_name(p->name());
9023 size_t len = n.length() + 100;
9024 char* buf = new char[len];
9025 if (is_ambiguous)
9026 snprintf(buf, len, _("ambiguous method %s%s%s"),
9027 go_open_quote(), n.c_str(), go_close_quote());
9028 else
9029 snprintf(buf, len, _("missing method %s%s%s"),
9030 go_open_quote(), n.c_str(), go_close_quote());
9031 reason->assign(buf);
9032 delete[] buf;
9033 }
9034 return false;
9035 }
9036
9037 Function_type *p_fn_type = p->type()->function_type();
9038 Function_type* m_fn_type = m->type()->function_type();
9039 go_assert(p_fn_type != NULL && m_fn_type != NULL);
9040 std::string subreason;
9041 if (!p_fn_type->is_identical(m_fn_type, true, Type::COMPARE_TAGS,
9042 &subreason))
9043 {
9044 if (reason != NULL)
9045 {
9046 std::string n = Gogo::message_name(p->name());
9047 size_t len = 100 + n.length() + subreason.length();
9048 char* buf = new char[len];
9049 if (subreason.empty())
9050 snprintf(buf, len, _("incompatible type for method %s%s%s"),
9051 go_open_quote(), n.c_str(), go_close_quote());
9052 else
9053 snprintf(buf, len,
9054 _("incompatible type for method %s%s%s (%s)"),
9055 go_open_quote(), n.c_str(), go_close_quote(),
9056 subreason.c_str());
9057 reason->assign(buf);
9058 delete[] buf;
9059 }
9060 return false;
9061 }
9062
9063 if (!is_pointer && !m->is_value_method())
9064 {
9065 if (reason != NULL)
9066 {
9067 std::string n = Gogo::message_name(p->name());
9068 size_t len = 100 + n.length();
9069 char* buf = new char[len];
9070 snprintf(buf, len,
9071 _("method %s%s%s requires a pointer receiver"),
9072 go_open_quote(), n.c_str(), go_close_quote());
9073 reason->assign(buf);
9074 delete[] buf;
9075 }
9076 return false;
9077 }
9078
9079 // If the magic //go:nointerface comment was used, the method
9080 // may not be used to implement interfaces.
9081 if (m->nointerface())
9082 {
9083 if (reason != NULL)
9084 {
9085 std::string n = Gogo::message_name(p->name());
9086 size_t len = 100 + n.length();
9087 char* buf = new char[len];
9088 snprintf(buf, len,
9089 _("method %s%s%s is marked go:nointerface"),
9090 go_open_quote(), n.c_str(), go_close_quote());
9091 reason->assign(buf);
9092 delete[] buf;
9093 }
9094 return false;
9095 }
9096 }
9097
9098 return true;
9099 }
9100
9101 // Return the backend representation of the empty interface type. We
9102 // use the same struct for all empty interfaces.
9103
9104 Btype*
get_backend_empty_interface_type(Gogo * gogo)9105 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
9106 {
9107 static Btype* empty_interface_type;
9108 if (empty_interface_type == NULL)
9109 {
9110 std::vector<Backend::Btyped_identifier> bfields(2);
9111
9112 Location bloc = Linemap::predeclared_location();
9113
9114 Type* pdt = Type::make_type_descriptor_ptr_type();
9115 bfields[0].name = "__type_descriptor";
9116 bfields[0].btype = pdt->get_backend(gogo);
9117 bfields[0].location = bloc;
9118
9119 Type* vt = Type::make_pointer_type(Type::make_void_type());
9120 bfields[1].name = "__object";
9121 bfields[1].btype = vt->get_backend(gogo);
9122 bfields[1].location = bloc;
9123
9124 empty_interface_type = gogo->backend()->struct_type(bfields);
9125 }
9126 return empty_interface_type;
9127 }
9128
9129 Interface_type::Bmethods_map Interface_type::bmethods_map;
9130
9131 // Return a pointer to the backend representation of the method table.
9132
9133 Btype*
get_backend_methods(Gogo * gogo)9134 Interface_type::get_backend_methods(Gogo* gogo)
9135 {
9136 if (this->bmethods_ != NULL && !this->bmethods_is_placeholder_)
9137 return this->bmethods_;
9138
9139 std::pair<Interface_type*, Bmethods_map_entry> val;
9140 val.first = this;
9141 val.second.btype = NULL;
9142 val.second.is_placeholder = false;
9143 std::pair<Bmethods_map::iterator, bool> ins =
9144 Interface_type::bmethods_map.insert(val);
9145 if (!ins.second
9146 && ins.first->second.btype != NULL
9147 && !ins.first->second.is_placeholder)
9148 {
9149 this->bmethods_ = ins.first->second.btype;
9150 this->bmethods_is_placeholder_ = false;
9151 return this->bmethods_;
9152 }
9153
9154 Location loc = this->location();
9155
9156 std::vector<Backend::Btyped_identifier>
9157 mfields(this->all_methods_->size() + 1);
9158
9159 Type* pdt = Type::make_type_descriptor_ptr_type();
9160 mfields[0].name = "__type_descriptor";
9161 mfields[0].btype = pdt->get_backend(gogo);
9162 mfields[0].location = loc;
9163
9164 std::string last_name = "";
9165 size_t i = 1;
9166 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9167 p != this->all_methods_->end();
9168 ++p, ++i)
9169 {
9170 // The type of the method in Go only includes the parameters.
9171 // The actual method also has a receiver, which is always a
9172 // pointer. We need to add that pointer type here in order to
9173 // generate the correct type for the backend.
9174 Function_type* ft = p->type()->function_type();
9175 go_assert(ft->receiver() == NULL);
9176
9177 const Typed_identifier_list* params = ft->parameters();
9178 Typed_identifier_list* mparams = new Typed_identifier_list();
9179 if (params != NULL)
9180 mparams->reserve(params->size() + 1);
9181 Type* vt = Type::make_pointer_type(Type::make_void_type());
9182 mparams->push_back(Typed_identifier("", vt, ft->location()));
9183 if (params != NULL)
9184 {
9185 for (Typed_identifier_list::const_iterator pp = params->begin();
9186 pp != params->end();
9187 ++pp)
9188 mparams->push_back(*pp);
9189 }
9190
9191 Typed_identifier_list* mresults = (ft->results() == NULL
9192 ? NULL
9193 : ft->results()->copy());
9194 Function_type* mft = Type::make_function_type(NULL, mparams, mresults,
9195 ft->location());
9196
9197 mfields[i].name = Gogo::unpack_hidden_name(p->name());
9198 mfields[i].btype = mft->get_backend_fntype(gogo);
9199 mfields[i].location = loc;
9200
9201 // Sanity check: the names should be sorted.
9202 go_assert(Gogo::unpack_hidden_name(p->name())
9203 > Gogo::unpack_hidden_name(last_name));
9204 last_name = p->name();
9205 }
9206
9207 Btype* st = gogo->backend()->struct_type(mfields);
9208 Btype* ret = gogo->backend()->pointer_type(st);
9209
9210 if (ins.first->second.btype != NULL
9211 && ins.first->second.is_placeholder)
9212 gogo->backend()->set_placeholder_pointer_type(ins.first->second.btype,
9213 ret);
9214 this->bmethods_ = ret;
9215 ins.first->second.btype = ret;
9216 this->bmethods_is_placeholder_ = false;
9217 ins.first->second.is_placeholder = false;
9218 return ret;
9219 }
9220
9221 // Return a placeholder for the pointer to the backend methods table.
9222
9223 Btype*
get_backend_methods_placeholder(Gogo * gogo)9224 Interface_type::get_backend_methods_placeholder(Gogo* gogo)
9225 {
9226 if (this->bmethods_ == NULL)
9227 {
9228 std::pair<Interface_type*, Bmethods_map_entry> val;
9229 val.first = this;
9230 val.second.btype = NULL;
9231 val.second.is_placeholder = false;
9232 std::pair<Bmethods_map::iterator, bool> ins =
9233 Interface_type::bmethods_map.insert(val);
9234 if (!ins.second && ins.first->second.btype != NULL)
9235 {
9236 this->bmethods_ = ins.first->second.btype;
9237 this->bmethods_is_placeholder_ = ins.first->second.is_placeholder;
9238 return this->bmethods_;
9239 }
9240
9241 Location loc = this->location();
9242 Btype* bt = gogo->backend()->placeholder_pointer_type("", loc, false);
9243 this->bmethods_ = bt;
9244 ins.first->second.btype = bt;
9245 this->bmethods_is_placeholder_ = true;
9246 ins.first->second.is_placeholder = true;
9247 }
9248 return this->bmethods_;
9249 }
9250
9251 // Return the fields of a non-empty interface type. This is not
9252 // declared in types.h so that types.h doesn't have to #include
9253 // backend.h.
9254
9255 static void
get_backend_interface_fields(Gogo * gogo,Interface_type * type,bool use_placeholder,std::vector<Backend::Btyped_identifier> * bfields)9256 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
9257 bool use_placeholder,
9258 std::vector<Backend::Btyped_identifier>* bfields)
9259 {
9260 Location loc = type->location();
9261
9262 bfields->resize(2);
9263
9264 (*bfields)[0].name = "__methods";
9265 (*bfields)[0].btype = (use_placeholder
9266 ? type->get_backend_methods_placeholder(gogo)
9267 : type->get_backend_methods(gogo));
9268 (*bfields)[0].location = loc;
9269
9270 Type* vt = Type::make_pointer_type(Type::make_void_type());
9271 (*bfields)[1].name = "__object";
9272 (*bfields)[1].btype = vt->get_backend(gogo);
9273 (*bfields)[1].location = Linemap::predeclared_location();
9274 }
9275
9276 // Return the backend representation for an interface type. An interface is a
9277 // pointer to a struct. The struct has three fields. The first field is a
9278 // pointer to the type descriptor for the dynamic type of the object.
9279 // The second field is a pointer to a table of methods for the
9280 // interface to be used with the object. The third field is the value
9281 // of the object itself.
9282
9283 Btype*
do_get_backend(Gogo * gogo)9284 Interface_type::do_get_backend(Gogo* gogo)
9285 {
9286 if (this->is_empty())
9287 return Interface_type::get_backend_empty_interface_type(gogo);
9288 else
9289 {
9290 if (this->interface_btype_ != NULL)
9291 return this->interface_btype_;
9292 this->interface_btype_ =
9293 gogo->backend()->placeholder_struct_type("", this->location_);
9294 std::vector<Backend::Btyped_identifier> bfields;
9295 get_backend_interface_fields(gogo, this, false, &bfields);
9296 if (!gogo->backend()->set_placeholder_struct_type(this->interface_btype_,
9297 bfields))
9298 this->interface_btype_ = gogo->backend()->error_type();
9299 return this->interface_btype_;
9300 }
9301 }
9302
9303 // Finish the backend representation of the methods.
9304
9305 void
finish_backend_methods(Gogo * gogo)9306 Interface_type::finish_backend_methods(Gogo* gogo)
9307 {
9308 if (!this->is_empty())
9309 {
9310 const Typed_identifier_list* methods = this->methods();
9311 if (methods != NULL)
9312 {
9313 for (Typed_identifier_list::const_iterator p = methods->begin();
9314 p != methods->end();
9315 ++p)
9316 p->type()->get_backend(gogo);
9317 }
9318
9319 // Getting the backend methods now will set the placeholder
9320 // pointer.
9321 this->get_backend_methods(gogo);
9322 }
9323 }
9324
9325 // The type of an interface type descriptor.
9326
9327 Type*
make_interface_type_descriptor_type()9328 Interface_type::make_interface_type_descriptor_type()
9329 {
9330 static Type* ret;
9331 if (ret == NULL)
9332 {
9333 Type* tdt = Type::make_type_descriptor_type();
9334 Type* ptdt = Type::make_type_descriptor_ptr_type();
9335
9336 Type* string_type = Type::lookup_string_type();
9337 Type* pointer_string_type = Type::make_pointer_type(string_type);
9338
9339 Struct_type* sm =
9340 Type::make_builtin_struct_type(3,
9341 "name", pointer_string_type,
9342 "pkgPath", pointer_string_type,
9343 "typ", ptdt);
9344
9345 Type* nsm = Type::make_builtin_named_type("imethod", sm);
9346
9347 Type* slice_nsm = Type::make_array_type(nsm, NULL);
9348
9349 Struct_type* s = Type::make_builtin_struct_type(2,
9350 "", tdt,
9351 "methods", slice_nsm);
9352
9353 ret = Type::make_builtin_named_type("InterfaceType", s);
9354 }
9355
9356 return ret;
9357 }
9358
9359 // Build a type descriptor for an interface type.
9360
9361 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)9362 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9363 {
9364 Location bloc = Linemap::predeclared_location();
9365
9366 Type* itdt = Interface_type::make_interface_type_descriptor_type();
9367
9368 const Struct_field_list* ifields = itdt->struct_type()->fields();
9369
9370 Expression_list* ivals = new Expression_list();
9371 ivals->reserve(2);
9372
9373 Struct_field_list::const_iterator pif = ifields->begin();
9374 go_assert(pif->is_field_name("_type"));
9375 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
9376 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
9377 true));
9378
9379 ++pif;
9380 go_assert(pif->is_field_name("methods"));
9381
9382 Expression_list* methods = new Expression_list();
9383 if (this->all_methods_ != NULL)
9384 {
9385 Type* elemtype = pif->type()->array_type()->element_type();
9386
9387 methods->reserve(this->all_methods_->size());
9388 for (Typed_identifier_list::const_iterator pm =
9389 this->all_methods_->begin();
9390 pm != this->all_methods_->end();
9391 ++pm)
9392 {
9393 const Struct_field_list* mfields = elemtype->struct_type()->fields();
9394
9395 Expression_list* mvals = new Expression_list();
9396 mvals->reserve(3);
9397
9398 Struct_field_list::const_iterator pmf = mfields->begin();
9399 go_assert(pmf->is_field_name("name"));
9400 std::string s = Gogo::unpack_hidden_name(pm->name());
9401 Expression* e = Expression::make_string(s, bloc);
9402 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
9403
9404 ++pmf;
9405 go_assert(pmf->is_field_name("pkgPath"));
9406 if (!Gogo::is_hidden_name(pm->name()))
9407 mvals->push_back(Expression::make_nil(bloc));
9408 else
9409 {
9410 s = Gogo::hidden_name_pkgpath(pm->name());
9411 e = Expression::make_string(s, bloc);
9412 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
9413 }
9414
9415 ++pmf;
9416 go_assert(pmf->is_field_name("typ"));
9417 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
9418
9419 ++pmf;
9420 go_assert(pmf == mfields->end());
9421
9422 e = Expression::make_struct_composite_literal(elemtype, mvals,
9423 bloc);
9424 methods->push_back(e);
9425 }
9426 }
9427
9428 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
9429 methods, bloc));
9430
9431 ++pif;
9432 go_assert(pif == ifields->end());
9433
9434 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
9435 }
9436
9437 // Reflection string.
9438
9439 void
do_reflection(Gogo * gogo,std::string * ret) const9440 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
9441 {
9442 ret->append("interface {");
9443 const Typed_identifier_list* methods = this->parse_methods_;
9444 if (methods != NULL)
9445 {
9446 ret->push_back(' ');
9447 for (Typed_identifier_list::const_iterator p = methods->begin();
9448 p != methods->end();
9449 ++p)
9450 {
9451 if (p != methods->begin())
9452 ret->append("; ");
9453 if (p->name().empty())
9454 this->append_reflection(p->type(), gogo, ret);
9455 else
9456 {
9457 if (!Gogo::is_hidden_name(p->name()))
9458 ret->append(p->name());
9459 else if (gogo->pkgpath_from_option())
9460 ret->append(p->name().substr(1));
9461 else
9462 {
9463 // If no -fgo-pkgpath option, backward compatibility
9464 // for how this used to work before -fgo-pkgpath was
9465 // introduced.
9466 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
9467 ret->append(pkgpath.substr(pkgpath.find('.') + 1));
9468 ret->push_back('.');
9469 ret->append(Gogo::unpack_hidden_name(p->name()));
9470 }
9471 std::string sub = p->type()->reflection(gogo);
9472 go_assert(sub.compare(0, 4, "func") == 0);
9473 sub = sub.substr(4);
9474 ret->append(sub);
9475 }
9476 }
9477 ret->push_back(' ');
9478 }
9479 ret->append("}");
9480 }
9481
9482 // Export.
9483
9484 void
do_export(Export * exp) const9485 Interface_type::do_export(Export* exp) const
9486 {
9487 exp->write_c_string("interface { ");
9488
9489 const Typed_identifier_list* methods = this->parse_methods_;
9490 if (methods != NULL)
9491 {
9492 for (Typed_identifier_list::const_iterator pm = methods->begin();
9493 pm != methods->end();
9494 ++pm)
9495 {
9496 if (pm->name().empty())
9497 {
9498 exp->write_c_string("? ");
9499 exp->write_type(pm->type());
9500 }
9501 else
9502 {
9503 exp->write_string(pm->name());
9504 exp->write_c_string(" (");
9505
9506 const Function_type* fntype = pm->type()->function_type();
9507
9508 bool first = true;
9509 const Typed_identifier_list* parameters = fntype->parameters();
9510 if (parameters != NULL)
9511 {
9512 bool is_varargs = fntype->is_varargs();
9513 for (Typed_identifier_list::const_iterator pp =
9514 parameters->begin();
9515 pp != parameters->end();
9516 ++pp)
9517 {
9518 if (first)
9519 first = false;
9520 else
9521 exp->write_c_string(", ");
9522 exp->write_name(pp->name());
9523 exp->write_c_string(" ");
9524 if (!is_varargs || pp + 1 != parameters->end())
9525 exp->write_type(pp->type());
9526 else
9527 {
9528 exp->write_c_string("...");
9529 Type *pptype = pp->type();
9530 exp->write_type(pptype->array_type()->element_type());
9531 }
9532 }
9533 }
9534
9535 exp->write_c_string(")");
9536
9537 const Typed_identifier_list* results = fntype->results();
9538 if (results != NULL)
9539 {
9540 exp->write_c_string(" ");
9541 if (results->size() == 1 && results->begin()->name().empty())
9542 exp->write_type(results->begin()->type());
9543 else
9544 {
9545 first = true;
9546 exp->write_c_string("(");
9547 for (Typed_identifier_list::const_iterator p =
9548 results->begin();
9549 p != results->end();
9550 ++p)
9551 {
9552 if (first)
9553 first = false;
9554 else
9555 exp->write_c_string(", ");
9556 exp->write_name(p->name());
9557 exp->write_c_string(" ");
9558 exp->write_type(p->type());
9559 }
9560 exp->write_c_string(")");
9561 }
9562 }
9563 }
9564
9565 exp->write_c_string("; ");
9566 }
9567 }
9568
9569 exp->write_c_string("}");
9570 }
9571
9572 // Import an interface type.
9573
9574 Interface_type*
do_import(Import * imp)9575 Interface_type::do_import(Import* imp)
9576 {
9577 imp->require_c_string("interface { ");
9578
9579 Typed_identifier_list* methods = new Typed_identifier_list;
9580 while (imp->peek_char() != '}')
9581 {
9582 std::string name = imp->read_identifier();
9583
9584 if (name == "?")
9585 {
9586 imp->require_c_string(" ");
9587 Type* t = imp->read_type();
9588 methods->push_back(Typed_identifier("", t, imp->location()));
9589 imp->require_c_string("; ");
9590 continue;
9591 }
9592
9593 imp->require_c_string(" (");
9594
9595 Typed_identifier_list* parameters;
9596 bool is_varargs = false;
9597 if (imp->peek_char() == ')')
9598 parameters = NULL;
9599 else
9600 {
9601 parameters = new Typed_identifier_list;
9602 while (true)
9603 {
9604 std::string name = imp->read_name();
9605 imp->require_c_string(" ");
9606
9607 if (imp->match_c_string("..."))
9608 {
9609 imp->advance(3);
9610 is_varargs = true;
9611 }
9612
9613 Type* ptype = imp->read_type();
9614 if (is_varargs)
9615 ptype = Type::make_array_type(ptype, NULL);
9616 parameters->push_back(Typed_identifier(name, ptype,
9617 imp->location()));
9618 if (imp->peek_char() != ',')
9619 break;
9620 go_assert(!is_varargs);
9621 imp->require_c_string(", ");
9622 }
9623 }
9624 imp->require_c_string(")");
9625
9626 Typed_identifier_list* results;
9627 if (imp->peek_char() != ' ')
9628 results = NULL;
9629 else
9630 {
9631 results = new Typed_identifier_list;
9632 imp->advance(1);
9633 if (imp->peek_char() != '(')
9634 {
9635 Type* rtype = imp->read_type();
9636 results->push_back(Typed_identifier("", rtype, imp->location()));
9637 }
9638 else
9639 {
9640 imp->advance(1);
9641 while (true)
9642 {
9643 std::string name = imp->read_name();
9644 imp->require_c_string(" ");
9645 Type* rtype = imp->read_type();
9646 results->push_back(Typed_identifier(name, rtype,
9647 imp->location()));
9648 if (imp->peek_char() != ',')
9649 break;
9650 imp->require_c_string(", ");
9651 }
9652 imp->require_c_string(")");
9653 }
9654 }
9655
9656 Function_type* fntype = Type::make_function_type(NULL, parameters,
9657 results,
9658 imp->location());
9659 if (is_varargs)
9660 fntype->set_is_varargs();
9661 methods->push_back(Typed_identifier(name, fntype, imp->location()));
9662
9663 imp->require_c_string("; ");
9664 }
9665
9666 imp->require_c_string("}");
9667
9668 if (methods->empty())
9669 {
9670 delete methods;
9671 methods = NULL;
9672 }
9673
9674 Interface_type* ret = Type::make_interface_type(methods, imp->location());
9675 ret->package_ = imp->package();
9676 return ret;
9677 }
9678
9679 // Make an interface type.
9680
9681 Interface_type*
make_interface_type(Typed_identifier_list * methods,Location location)9682 Type::make_interface_type(Typed_identifier_list* methods,
9683 Location location)
9684 {
9685 return new Interface_type(methods, location);
9686 }
9687
9688 // Make an empty interface type.
9689
9690 Interface_type*
make_empty_interface_type(Location location)9691 Type::make_empty_interface_type(Location location)
9692 {
9693 Interface_type* ret = new Interface_type(NULL, location);
9694 ret->finalize_methods();
9695 return ret;
9696 }
9697
9698 // Class Method.
9699
9700 // Bind a method to an object.
9701
9702 Expression*
bind_method(Expression * expr,Location location) const9703 Method::bind_method(Expression* expr, Location location) const
9704 {
9705 if (this->stub_ == NULL)
9706 {
9707 // When there is no stub object, the binding is determined by
9708 // the child class.
9709 return this->do_bind_method(expr, location);
9710 }
9711 return Expression::make_bound_method(expr, this, this->stub_, location);
9712 }
9713
9714 // Return the named object associated with a method. This may only be
9715 // called after methods are finalized.
9716
9717 Named_object*
named_object() const9718 Method::named_object() const
9719 {
9720 if (this->stub_ != NULL)
9721 return this->stub_;
9722 return this->do_named_object();
9723 }
9724
9725 // Class Named_method.
9726
9727 // The type of the method.
9728
9729 Function_type*
do_type() const9730 Named_method::do_type() const
9731 {
9732 if (this->named_object_->is_function())
9733 return this->named_object_->func_value()->type();
9734 else if (this->named_object_->is_function_declaration())
9735 return this->named_object_->func_declaration_value()->type();
9736 else
9737 go_unreachable();
9738 }
9739
9740 // Return the location of the method receiver.
9741
9742 Location
do_receiver_location() const9743 Named_method::do_receiver_location() const
9744 {
9745 return this->do_type()->receiver()->location();
9746 }
9747
9748 // Bind a method to an object.
9749
9750 Expression*
do_bind_method(Expression * expr,Location location) const9751 Named_method::do_bind_method(Expression* expr, Location location) const
9752 {
9753 Named_object* no = this->named_object_;
9754 Bound_method_expression* bme = Expression::make_bound_method(expr, this,
9755 no, location);
9756 // If this is not a local method, and it does not use a stub, then
9757 // the real method expects a different type. We need to cast the
9758 // first argument.
9759 if (this->depth() > 0 && !this->needs_stub_method())
9760 {
9761 Function_type* ftype = this->do_type();
9762 go_assert(ftype->is_method());
9763 Type* frtype = ftype->receiver()->type();
9764 bme->set_first_argument_type(frtype);
9765 }
9766 return bme;
9767 }
9768
9769 // Return whether this method should not participate in interfaces.
9770
9771 bool
do_nointerface() const9772 Named_method::do_nointerface() const
9773 {
9774 Named_object* no = this->named_object_;
9775 if (no->is_function())
9776 return no->func_value()->nointerface();
9777 else if (no->is_function_declaration())
9778 return no->func_declaration_value()->nointerface();
9779 else
9780 go_unreachable();
9781 }
9782
9783 // Class Interface_method.
9784
9785 // Bind a method to an object.
9786
9787 Expression*
do_bind_method(Expression * expr,Location location) const9788 Interface_method::do_bind_method(Expression* expr,
9789 Location location) const
9790 {
9791 return Expression::make_interface_field_reference(expr, this->name_,
9792 location);
9793 }
9794
9795 // Class Methods.
9796
9797 // Insert a new method. Return true if it was inserted, false
9798 // otherwise.
9799
9800 bool
insert(const std::string & name,Method * m)9801 Methods::insert(const std::string& name, Method* m)
9802 {
9803 std::pair<Method_map::iterator, bool> ins =
9804 this->methods_.insert(std::make_pair(name, m));
9805 if (ins.second)
9806 return true;
9807 else
9808 {
9809 Method* old_method = ins.first->second;
9810 if (m->depth() < old_method->depth())
9811 {
9812 delete old_method;
9813 ins.first->second = m;
9814 return true;
9815 }
9816 else
9817 {
9818 if (m->depth() == old_method->depth())
9819 old_method->set_is_ambiguous();
9820 return false;
9821 }
9822 }
9823 }
9824
9825 // Return the number of unambiguous methods.
9826
9827 size_t
count() const9828 Methods::count() const
9829 {
9830 size_t ret = 0;
9831 for (Method_map::const_iterator p = this->methods_.begin();
9832 p != this->methods_.end();
9833 ++p)
9834 if (!p->second->is_ambiguous())
9835 ++ret;
9836 return ret;
9837 }
9838
9839 // Class Named_type.
9840
9841 // Return the name of the type.
9842
9843 const std::string&
name() const9844 Named_type::name() const
9845 {
9846 return this->named_object_->name();
9847 }
9848
9849 // Return the name of the type to use in an error message.
9850
9851 std::string
message_name() const9852 Named_type::message_name() const
9853 {
9854 return this->named_object_->message_name();
9855 }
9856
9857 // Return the base type for this type. We have to be careful about
9858 // circular type definitions, which are invalid but may be seen here.
9859
9860 Type*
named_base()9861 Named_type::named_base()
9862 {
9863 if (this->seen_)
9864 return this;
9865 this->seen_ = true;
9866 Type* ret = this->type_->base();
9867 this->seen_ = false;
9868 return ret;
9869 }
9870
9871 const Type*
named_base() const9872 Named_type::named_base() const
9873 {
9874 if (this->seen_)
9875 return this;
9876 this->seen_ = true;
9877 const Type* ret = this->type_->base();
9878 this->seen_ = false;
9879 return ret;
9880 }
9881
9882 // Return whether this is an error type. We have to be careful about
9883 // circular type definitions, which are invalid but may be seen here.
9884
9885 bool
is_named_error_type() const9886 Named_type::is_named_error_type() const
9887 {
9888 if (this->seen_)
9889 return false;
9890 this->seen_ = true;
9891 bool ret = this->type_->is_error_type();
9892 this->seen_ = false;
9893 return ret;
9894 }
9895
9896 // Whether this type is comparable. We have to be careful about
9897 // circular type definitions.
9898
9899 bool
named_type_is_comparable(std::string * reason) const9900 Named_type::named_type_is_comparable(std::string* reason) const
9901 {
9902 if (this->seen_)
9903 return false;
9904 this->seen_ = true;
9905 bool ret = Type::are_compatible_for_comparison(true, this->type_,
9906 this->type_, reason);
9907 this->seen_ = false;
9908 return ret;
9909 }
9910
9911 // Add a method to this type.
9912
9913 Named_object*
add_method(const std::string & name,Function * function)9914 Named_type::add_method(const std::string& name, Function* function)
9915 {
9916 go_assert(!this->is_alias_);
9917 if (this->local_methods_ == NULL)
9918 this->local_methods_ = new Bindings(NULL);
9919 return this->local_methods_->add_function(name,
9920 this->named_object_->package(),
9921 function);
9922 }
9923
9924 // Add a method declaration to this type.
9925
9926 Named_object*
add_method_declaration(const std::string & name,Package * package,Function_type * type,Location location)9927 Named_type::add_method_declaration(const std::string& name, Package* package,
9928 Function_type* type,
9929 Location location)
9930 {
9931 go_assert(!this->is_alias_);
9932 if (this->local_methods_ == NULL)
9933 this->local_methods_ = new Bindings(NULL);
9934 return this->local_methods_->add_function_declaration(name, package, type,
9935 location);
9936 }
9937
9938 // Add an existing method to this type.
9939
9940 void
add_existing_method(Named_object * no)9941 Named_type::add_existing_method(Named_object* no)
9942 {
9943 go_assert(!this->is_alias_);
9944 if (this->local_methods_ == NULL)
9945 this->local_methods_ = new Bindings(NULL);
9946 this->local_methods_->add_named_object(no);
9947 }
9948
9949 // Look for a local method NAME, and returns its named object, or NULL
9950 // if not there.
9951
9952 Named_object*
find_local_method(const std::string & name) const9953 Named_type::find_local_method(const std::string& name) const
9954 {
9955 if (this->is_error_)
9956 return NULL;
9957 if (this->is_alias_)
9958 {
9959 Named_type* nt = this->type_->named_type();
9960 if (nt != NULL)
9961 {
9962 if (this->seen_alias_)
9963 return NULL;
9964 this->seen_alias_ = true;
9965 Named_object* ret = nt->find_local_method(name);
9966 this->seen_alias_ = false;
9967 return ret;
9968 }
9969 return NULL;
9970 }
9971 if (this->local_methods_ == NULL)
9972 return NULL;
9973 return this->local_methods_->lookup(name);
9974 }
9975
9976 // Return the list of local methods.
9977
9978 const Bindings*
local_methods() const9979 Named_type::local_methods() const
9980 {
9981 if (this->is_error_)
9982 return NULL;
9983 if (this->is_alias_)
9984 {
9985 Named_type* nt = this->type_->named_type();
9986 if (nt != NULL)
9987 {
9988 if (this->seen_alias_)
9989 return NULL;
9990 this->seen_alias_ = true;
9991 const Bindings* ret = nt->local_methods();
9992 this->seen_alias_ = false;
9993 return ret;
9994 }
9995 return NULL;
9996 }
9997 return this->local_methods_;
9998 }
9999
10000 // Return whether NAME is an unexported field or method, for better
10001 // error reporting.
10002
10003 bool
is_unexported_local_method(Gogo * gogo,const std::string & name) const10004 Named_type::is_unexported_local_method(Gogo* gogo,
10005 const std::string& name) const
10006 {
10007 if (this->is_error_)
10008 return false;
10009 if (this->is_alias_)
10010 {
10011 Named_type* nt = this->type_->named_type();
10012 if (nt != NULL)
10013 {
10014 if (this->seen_alias_)
10015 return false;
10016 this->seen_alias_ = true;
10017 bool ret = nt->is_unexported_local_method(gogo, name);
10018 this->seen_alias_ = false;
10019 return ret;
10020 }
10021 return false;
10022 }
10023 Bindings* methods = this->local_methods_;
10024 if (methods != NULL)
10025 {
10026 for (Bindings::const_declarations_iterator p =
10027 methods->begin_declarations();
10028 p != methods->end_declarations();
10029 ++p)
10030 {
10031 if (Gogo::is_hidden_name(p->first)
10032 && name == Gogo::unpack_hidden_name(p->first)
10033 && gogo->pack_hidden_name(name, false) != p->first)
10034 return true;
10035 }
10036 }
10037 return false;
10038 }
10039
10040 // Build the complete list of methods for this type, which means
10041 // recursively including all methods for anonymous fields. Create all
10042 // stub methods.
10043
10044 void
finalize_methods(Gogo * gogo)10045 Named_type::finalize_methods(Gogo* gogo)
10046 {
10047 if (this->is_alias_)
10048 return;
10049 if (this->all_methods_ != NULL)
10050 return;
10051
10052 if (this->local_methods_ != NULL
10053 && (this->points_to() != NULL || this->interface_type() != NULL))
10054 {
10055 const Bindings* lm = this->local_methods_;
10056 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
10057 p != lm->end_declarations();
10058 ++p)
10059 go_error_at(p->second->location(),
10060 "invalid pointer or interface receiver type");
10061 delete this->local_methods_;
10062 this->local_methods_ = NULL;
10063 return;
10064 }
10065
10066 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
10067 }
10068
10069 // Return whether this type has any methods.
10070
10071 bool
has_any_methods() const10072 Named_type::has_any_methods() const
10073 {
10074 if (this->is_error_)
10075 return false;
10076 if (this->is_alias_)
10077 {
10078 if (this->type_->named_type() != NULL)
10079 {
10080 if (this->seen_alias_)
10081 return false;
10082 this->seen_alias_ = true;
10083 bool ret = this->type_->named_type()->has_any_methods();
10084 this->seen_alias_ = false;
10085 return ret;
10086 }
10087 if (this->type_->struct_type() != NULL)
10088 return this->type_->struct_type()->has_any_methods();
10089 return false;
10090 }
10091 return this->all_methods_ != NULL;
10092 }
10093
10094 // Return the methods for this type.
10095
10096 const Methods*
methods() const10097 Named_type::methods() const
10098 {
10099 if (this->is_error_)
10100 return NULL;
10101 if (this->is_alias_)
10102 {
10103 if (this->type_->named_type() != NULL)
10104 {
10105 if (this->seen_alias_)
10106 return NULL;
10107 this->seen_alias_ = true;
10108 const Methods* ret = this->type_->named_type()->methods();
10109 this->seen_alias_ = false;
10110 return ret;
10111 }
10112 if (this->type_->struct_type() != NULL)
10113 return this->type_->struct_type()->methods();
10114 return NULL;
10115 }
10116 return this->all_methods_;
10117 }
10118
10119 // Return the method NAME, or NULL if there isn't one or if it is
10120 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
10121 // ambiguous.
10122
10123 Method*
method_function(const std::string & name,bool * is_ambiguous) const10124 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
10125 {
10126 if (this->is_error_)
10127 return NULL;
10128 if (this->is_alias_)
10129 {
10130 if (is_ambiguous != NULL)
10131 *is_ambiguous = false;
10132 if (this->type_->named_type() != NULL)
10133 {
10134 if (this->seen_alias_)
10135 return NULL;
10136 this->seen_alias_ = true;
10137 Named_type* nt = this->type_->named_type();
10138 Method* ret = nt->method_function(name, is_ambiguous);
10139 this->seen_alias_ = false;
10140 return ret;
10141 }
10142 if (this->type_->struct_type() != NULL)
10143 return this->type_->struct_type()->method_function(name, is_ambiguous);
10144 return NULL;
10145 }
10146 return Type::method_function(this->all_methods_, name, is_ambiguous);
10147 }
10148
10149 // Return a pointer to the interface method table for this type for
10150 // the interface INTERFACE. IS_POINTER is true if this is for a
10151 // pointer to THIS.
10152
10153 Expression*
interface_method_table(Interface_type * interface,bool is_pointer)10154 Named_type::interface_method_table(Interface_type* interface, bool is_pointer)
10155 {
10156 if (this->is_error_)
10157 return Expression::make_error(this->location_);
10158 if (this->is_alias_)
10159 {
10160 if (this->type_->named_type() != NULL)
10161 {
10162 if (this->seen_alias_)
10163 return Expression::make_error(this->location_);
10164 this->seen_alias_ = true;
10165 Named_type* nt = this->type_->named_type();
10166 Expression* ret = nt->interface_method_table(interface, is_pointer);
10167 this->seen_alias_ = false;
10168 return ret;
10169 }
10170 if (this->type_->struct_type() != NULL)
10171 return this->type_->struct_type()->interface_method_table(interface,
10172 is_pointer);
10173 go_unreachable();
10174 }
10175 return Type::interface_method_table(this, interface, is_pointer,
10176 &this->interface_method_tables_,
10177 &this->pointer_interface_method_tables_);
10178 }
10179
10180 // Look for a use of a complete type within another type. This is
10181 // used to check that we don't try to use a type within itself.
10182
10183 class Find_type_use : public Traverse
10184 {
10185 public:
Find_type_use(Named_type * find_type)10186 Find_type_use(Named_type* find_type)
10187 : Traverse(traverse_types),
10188 find_type_(find_type), found_(false)
10189 { }
10190
10191 // Whether we found the type.
10192 bool
found() const10193 found() const
10194 { return this->found_; }
10195
10196 protected:
10197 int
10198 type(Type*);
10199
10200 private:
10201 // The type we are looking for.
10202 Named_type* find_type_;
10203 // Whether we found the type.
10204 bool found_;
10205 };
10206
10207 // Check for FIND_TYPE in TYPE.
10208
10209 int
type(Type * type)10210 Find_type_use::type(Type* type)
10211 {
10212 if (type->named_type() != NULL && this->find_type_ == type->named_type())
10213 {
10214 this->found_ = true;
10215 return TRAVERSE_EXIT;
10216 }
10217
10218 // It's OK if we see a reference to the type in any type which is
10219 // essentially a pointer: a pointer, a slice, a function, a map, or
10220 // a channel.
10221 if (type->points_to() != NULL
10222 || type->is_slice_type()
10223 || type->function_type() != NULL
10224 || type->map_type() != NULL
10225 || type->channel_type() != NULL)
10226 return TRAVERSE_SKIP_COMPONENTS;
10227
10228 // For an interface, a reference to the type in a method type should
10229 // be ignored, but we have to consider direct inheritance. When
10230 // this is called, there may be cases of direct inheritance
10231 // represented as a method with no name.
10232 if (type->interface_type() != NULL)
10233 {
10234 const Typed_identifier_list* methods = type->interface_type()->methods();
10235 if (methods != NULL)
10236 {
10237 for (Typed_identifier_list::const_iterator p = methods->begin();
10238 p != methods->end();
10239 ++p)
10240 {
10241 if (p->name().empty())
10242 {
10243 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
10244 return TRAVERSE_EXIT;
10245 }
10246 }
10247 }
10248 return TRAVERSE_SKIP_COMPONENTS;
10249 }
10250
10251 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
10252 // to convert TYPE to the backend representation before we convert
10253 // FIND_TYPE_.
10254 if (type->named_type() != NULL)
10255 {
10256 switch (type->base()->classification())
10257 {
10258 case Type::TYPE_ERROR:
10259 case Type::TYPE_BOOLEAN:
10260 case Type::TYPE_INTEGER:
10261 case Type::TYPE_FLOAT:
10262 case Type::TYPE_COMPLEX:
10263 case Type::TYPE_STRING:
10264 case Type::TYPE_NIL:
10265 break;
10266
10267 case Type::TYPE_ARRAY:
10268 case Type::TYPE_STRUCT:
10269 this->find_type_->add_dependency(type->named_type());
10270 break;
10271
10272 case Type::TYPE_NAMED:
10273 if (type->named_type() == type->base()->named_type())
10274 {
10275 this->found_ = true;
10276 return TRAVERSE_EXIT;
10277 }
10278 else
10279 go_assert(saw_errors());
10280 break;
10281
10282 case Type::TYPE_FORWARD:
10283 go_assert(saw_errors());
10284 break;
10285
10286 case Type::TYPE_VOID:
10287 case Type::TYPE_SINK:
10288 case Type::TYPE_FUNCTION:
10289 case Type::TYPE_POINTER:
10290 case Type::TYPE_CALL_MULTIPLE_RESULT:
10291 case Type::TYPE_MAP:
10292 case Type::TYPE_CHANNEL:
10293 case Type::TYPE_INTERFACE:
10294 default:
10295 go_unreachable();
10296 }
10297 }
10298
10299 return TRAVERSE_CONTINUE;
10300 }
10301
10302 // Look for a circular reference of an alias.
10303
10304 class Find_alias : public Traverse
10305 {
10306 public:
Find_alias(Named_type * find_type)10307 Find_alias(Named_type* find_type)
10308 : Traverse(traverse_types),
10309 find_type_(find_type), found_(false)
10310 { }
10311
10312 // Whether we found the type.
10313 bool
found() const10314 found() const
10315 { return this->found_; }
10316
10317 protected:
10318 int
10319 type(Type*);
10320
10321 private:
10322 // The type we are looking for.
10323 Named_type* find_type_;
10324 // Whether we found the type.
10325 bool found_;
10326 };
10327
10328 int
type(Type * type)10329 Find_alias::type(Type* type)
10330 {
10331 Named_type* nt = type->named_type();
10332 if (nt != NULL)
10333 {
10334 if (nt == this->find_type_)
10335 {
10336 this->found_ = true;
10337 return TRAVERSE_EXIT;
10338 }
10339
10340 // We started from `type T1 = T2`, where T1 is find_type_ and T2
10341 // is, perhaps indirectly, the parameter TYPE. If TYPE is not
10342 // an alias itself, it's OK if whatever T2 is defined as refers
10343 // to T1.
10344 if (!nt->is_alias())
10345 return TRAVERSE_SKIP_COMPONENTS;
10346 }
10347
10348 // Check if there are recursive inherited interface aliases.
10349 Interface_type* ift = type->interface_type();
10350 if (ift != NULL)
10351 {
10352 const Typed_identifier_list* methods = ift->local_methods();
10353 if (methods == NULL)
10354 return TRAVERSE_CONTINUE;
10355 for (Typed_identifier_list::const_iterator p = methods->begin();
10356 p != methods->end();
10357 ++p)
10358 if (p->name().empty() && p->type()->named_type() == this->find_type_)
10359 {
10360 this->found_ = true;
10361 return TRAVERSE_EXIT;
10362 }
10363 }
10364
10365 return TRAVERSE_CONTINUE;
10366 }
10367
10368 // Verify that a named type does not refer to itself.
10369
10370 bool
do_verify()10371 Named_type::do_verify()
10372 {
10373 if (this->is_verified_)
10374 return true;
10375 this->is_verified_ = true;
10376
10377 if (this->is_error_)
10378 return false;
10379
10380 if (this->is_alias_)
10381 {
10382 Find_alias find(this);
10383 Type::traverse(this->type_, &find);
10384 if (find.found())
10385 {
10386 go_error_at(this->location_, "invalid recursive alias %qs",
10387 this->message_name().c_str());
10388 this->is_error_ = true;
10389 return false;
10390 }
10391 }
10392
10393 Find_type_use find(this);
10394 Type::traverse(this->type_, &find);
10395 if (find.found())
10396 {
10397 go_error_at(this->location_, "invalid recursive type %qs",
10398 this->message_name().c_str());
10399 this->is_error_ = true;
10400 return false;
10401 }
10402
10403 // Check whether any of the local methods overloads an existing
10404 // struct field or interface method. We don't need to check the
10405 // list of methods against itself: that is handled by the Bindings
10406 // code.
10407 if (this->local_methods_ != NULL)
10408 {
10409 Struct_type* st = this->type_->struct_type();
10410 if (st != NULL)
10411 {
10412 for (Bindings::const_declarations_iterator p =
10413 this->local_methods_->begin_declarations();
10414 p != this->local_methods_->end_declarations();
10415 ++p)
10416 {
10417 const std::string& name(p->first);
10418 if (st != NULL && st->find_local_field(name, NULL) != NULL)
10419 {
10420 go_error_at(p->second->location(),
10421 "method %qs redeclares struct field name",
10422 Gogo::message_name(name).c_str());
10423 }
10424 }
10425 }
10426 }
10427
10428 return true;
10429 }
10430
10431 // Return whether this type is or contains a pointer.
10432
10433 bool
do_has_pointer() const10434 Named_type::do_has_pointer() const
10435 {
10436 if (this->seen_)
10437 return false;
10438 this->seen_ = true;
10439 bool ret = this->type_->has_pointer();
10440 this->seen_ = false;
10441 return ret;
10442 }
10443
10444 // Return whether comparisons for this type can use the identity
10445 // function.
10446
10447 bool
do_compare_is_identity(Gogo * gogo)10448 Named_type::do_compare_is_identity(Gogo* gogo)
10449 {
10450 // We don't use this->seen_ here because compare_is_identity may
10451 // call base() later, and that will mess up if seen_ is set here.
10452 if (this->seen_in_compare_is_identity_)
10453 return false;
10454 this->seen_in_compare_is_identity_ = true;
10455 bool ret = this->type_->compare_is_identity(gogo);
10456 this->seen_in_compare_is_identity_ = false;
10457 return ret;
10458 }
10459
10460 // Return whether this type is reflexive--whether it is always equal
10461 // to itself.
10462
10463 bool
do_is_reflexive()10464 Named_type::do_is_reflexive()
10465 {
10466 if (this->seen_in_compare_is_identity_)
10467 return false;
10468 this->seen_in_compare_is_identity_ = true;
10469 bool ret = this->type_->is_reflexive();
10470 this->seen_in_compare_is_identity_ = false;
10471 return ret;
10472 }
10473
10474 // Return whether this type needs a key update when used as a map key.
10475
10476 bool
do_needs_key_update()10477 Named_type::do_needs_key_update()
10478 {
10479 if (this->seen_in_compare_is_identity_)
10480 return true;
10481 this->seen_in_compare_is_identity_ = true;
10482 bool ret = this->type_->needs_key_update();
10483 this->seen_in_compare_is_identity_ = false;
10484 return ret;
10485 }
10486
10487 // Return a hash code. This is used for method lookup. We simply
10488 // hash on the name itself.
10489
10490 unsigned int
do_hash_for_method(Gogo * gogo,int) const10491 Named_type::do_hash_for_method(Gogo* gogo, int) const
10492 {
10493 if (this->is_error_)
10494 return 0;
10495
10496 // Aliases are handled in Type::hash_for_method.
10497 go_assert(!this->is_alias_);
10498
10499 const std::string& name(this->named_object()->name());
10500 unsigned int ret = Gogo::hash_string(name, 0);
10501
10502 // GOGO will be NULL here when called from Type_hash_identical.
10503 // That is OK because that is only used for internal hash tables
10504 // where we are going to be comparing named types for equality. In
10505 // other cases, which are cases where the runtime is going to
10506 // compare hash codes to see if the types are the same, we need to
10507 // include the pkgpath in the hash.
10508 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
10509 {
10510 const Package* package = this->named_object()->package();
10511 if (package == NULL)
10512 ret = Gogo::hash_string(gogo->pkgpath(), ret);
10513 else
10514 ret = Gogo::hash_string(package->pkgpath(), ret);
10515 }
10516
10517 return ret;
10518 }
10519
10520 // Convert a named type to the backend representation. In order to
10521 // get dependencies right, we fill in a dummy structure for this type,
10522 // then convert all the dependencies, then complete this type. When
10523 // this function is complete, the size of the type is known.
10524
10525 void
convert(Gogo * gogo)10526 Named_type::convert(Gogo* gogo)
10527 {
10528 if (this->is_error_ || this->is_converted_)
10529 return;
10530
10531 this->create_placeholder(gogo);
10532
10533 // If we are called to turn unsafe.Sizeof into a constant, we may
10534 // not have verified the type yet. We have to make sure it is
10535 // verified, since that sets the list of dependencies.
10536 this->verify();
10537
10538 // Convert all the dependencies. If they refer indirectly back to
10539 // this type, they will pick up the intermediate representation we just
10540 // created.
10541 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
10542 p != this->dependencies_.end();
10543 ++p)
10544 (*p)->convert(gogo);
10545
10546 // Complete this type.
10547 Btype* bt = this->named_btype_;
10548 Type* base = this->type_->base();
10549 switch (base->classification())
10550 {
10551 case TYPE_VOID:
10552 case TYPE_BOOLEAN:
10553 case TYPE_INTEGER:
10554 case TYPE_FLOAT:
10555 case TYPE_COMPLEX:
10556 case TYPE_STRING:
10557 case TYPE_NIL:
10558 break;
10559
10560 case TYPE_MAP:
10561 case TYPE_CHANNEL:
10562 break;
10563
10564 case TYPE_FUNCTION:
10565 case TYPE_POINTER:
10566 // The size of these types is already correct. We don't worry
10567 // about filling them in until later, when we also track
10568 // circular references.
10569 break;
10570
10571 case TYPE_STRUCT:
10572 {
10573 std::vector<Backend::Btyped_identifier> bfields;
10574 get_backend_struct_fields(gogo, base->struct_type(), true, &bfields);
10575 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
10576 bt = gogo->backend()->error_type();
10577 }
10578 break;
10579
10580 case TYPE_ARRAY:
10581 // Slice types were completed in create_placeholder.
10582 if (!base->is_slice_type())
10583 {
10584 Btype* bet = base->array_type()->get_backend_element(gogo, true);
10585 Bexpression* blen = base->array_type()->get_backend_length(gogo);
10586 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
10587 bt = gogo->backend()->error_type();
10588 }
10589 break;
10590
10591 case TYPE_INTERFACE:
10592 // Interface types were completed in create_placeholder.
10593 break;
10594
10595 case TYPE_ERROR:
10596 return;
10597
10598 default:
10599 case TYPE_SINK:
10600 case TYPE_CALL_MULTIPLE_RESULT:
10601 case TYPE_NAMED:
10602 case TYPE_FORWARD:
10603 go_unreachable();
10604 }
10605
10606 this->named_btype_ = bt;
10607 this->is_converted_ = true;
10608 this->is_placeholder_ = false;
10609 }
10610
10611 // Create the placeholder for a named type. This is the first step in
10612 // converting to the backend representation.
10613
10614 void
create_placeholder(Gogo * gogo)10615 Named_type::create_placeholder(Gogo* gogo)
10616 {
10617 if (this->is_error_)
10618 this->named_btype_ = gogo->backend()->error_type();
10619
10620 if (this->named_btype_ != NULL)
10621 return;
10622
10623 // Create the structure for this type. Note that because we call
10624 // base() here, we don't attempt to represent a named type defined
10625 // as another named type. Instead both named types will point to
10626 // different base representations.
10627 Type* base = this->type_->base();
10628 Btype* bt;
10629 bool set_name = true;
10630 switch (base->classification())
10631 {
10632 case TYPE_ERROR:
10633 this->is_error_ = true;
10634 this->named_btype_ = gogo->backend()->error_type();
10635 return;
10636
10637 case TYPE_VOID:
10638 case TYPE_BOOLEAN:
10639 case TYPE_INTEGER:
10640 case TYPE_FLOAT:
10641 case TYPE_COMPLEX:
10642 case TYPE_STRING:
10643 case TYPE_NIL:
10644 // These are simple basic types, we can just create them
10645 // directly.
10646 bt = Type::get_named_base_btype(gogo, base);
10647 break;
10648
10649 case TYPE_MAP:
10650 case TYPE_CHANNEL:
10651 // All maps and channels have the same backend representation.
10652 bt = Type::get_named_base_btype(gogo, base);
10653 break;
10654
10655 case TYPE_FUNCTION:
10656 case TYPE_POINTER:
10657 {
10658 bool for_function = base->classification() == TYPE_FUNCTION;
10659 bt = gogo->backend()->placeholder_pointer_type(this->name(),
10660 this->location_,
10661 for_function);
10662 set_name = false;
10663 }
10664 break;
10665
10666 case TYPE_STRUCT:
10667 bt = gogo->backend()->placeholder_struct_type(this->name(),
10668 this->location_);
10669 this->is_placeholder_ = true;
10670 set_name = false;
10671 break;
10672
10673 case TYPE_ARRAY:
10674 if (base->is_slice_type())
10675 bt = gogo->backend()->placeholder_struct_type(this->name(),
10676 this->location_);
10677 else
10678 {
10679 bt = gogo->backend()->placeholder_array_type(this->name(),
10680 this->location_);
10681 this->is_placeholder_ = true;
10682 }
10683 set_name = false;
10684 break;
10685
10686 case TYPE_INTERFACE:
10687 if (base->interface_type()->is_empty())
10688 bt = Interface_type::get_backend_empty_interface_type(gogo);
10689 else
10690 {
10691 bt = gogo->backend()->placeholder_struct_type(this->name(),
10692 this->location_);
10693 set_name = false;
10694 }
10695 break;
10696
10697 default:
10698 case TYPE_SINK:
10699 case TYPE_CALL_MULTIPLE_RESULT:
10700 case TYPE_NAMED:
10701 case TYPE_FORWARD:
10702 go_unreachable();
10703 }
10704
10705 if (set_name)
10706 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
10707
10708 this->named_btype_ = bt;
10709
10710 if (base->is_slice_type())
10711 {
10712 // We do not record slices as dependencies of other types,
10713 // because we can fill them in completely here with the final
10714 // size.
10715 std::vector<Backend::Btyped_identifier> bfields;
10716 get_backend_slice_fields(gogo, base->array_type(), true, &bfields);
10717 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
10718 this->named_btype_ = gogo->backend()->error_type();
10719 }
10720 else if (base->interface_type() != NULL
10721 && !base->interface_type()->is_empty())
10722 {
10723 // We do not record interfaces as dependencies of other types,
10724 // because we can fill them in completely here with the final
10725 // size.
10726 std::vector<Backend::Btyped_identifier> bfields;
10727 get_backend_interface_fields(gogo, base->interface_type(), true,
10728 &bfields);
10729 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
10730 this->named_btype_ = gogo->backend()->error_type();
10731 }
10732 }
10733
10734 // Get the backend representation for a named type.
10735
10736 Btype*
do_get_backend(Gogo * gogo)10737 Named_type::do_get_backend(Gogo* gogo)
10738 {
10739 if (this->is_error_)
10740 return gogo->backend()->error_type();
10741
10742 Btype* bt = this->named_btype_;
10743
10744 if (!gogo->named_types_are_converted())
10745 {
10746 // We have not completed converting named types. NAMED_BTYPE_
10747 // is a placeholder and we shouldn't do anything further.
10748 if (bt != NULL)
10749 return bt;
10750
10751 // We don't build dependencies for types whose sizes do not
10752 // change or are not relevant, so we may see them here while
10753 // converting types.
10754 this->create_placeholder(gogo);
10755 bt = this->named_btype_;
10756 go_assert(bt != NULL);
10757 return bt;
10758 }
10759
10760 // We are not converting types. This should only be called if the
10761 // type has already been converted.
10762 if (!this->is_converted_)
10763 {
10764 go_assert(saw_errors());
10765 return gogo->backend()->error_type();
10766 }
10767
10768 go_assert(bt != NULL);
10769
10770 // Complete the backend representation.
10771 Type* base = this->type_->base();
10772 Btype* bt1;
10773 switch (base->classification())
10774 {
10775 case TYPE_ERROR:
10776 return gogo->backend()->error_type();
10777
10778 case TYPE_VOID:
10779 case TYPE_BOOLEAN:
10780 case TYPE_INTEGER:
10781 case TYPE_FLOAT:
10782 case TYPE_COMPLEX:
10783 case TYPE_STRING:
10784 case TYPE_NIL:
10785 case TYPE_MAP:
10786 case TYPE_CHANNEL:
10787 return bt;
10788
10789 case TYPE_STRUCT:
10790 if (!this->seen_in_get_backend_)
10791 {
10792 this->seen_in_get_backend_ = true;
10793 base->struct_type()->finish_backend_fields(gogo);
10794 this->seen_in_get_backend_ = false;
10795 }
10796 return bt;
10797
10798 case TYPE_ARRAY:
10799 if (!this->seen_in_get_backend_)
10800 {
10801 this->seen_in_get_backend_ = true;
10802 base->array_type()->finish_backend_element(gogo);
10803 this->seen_in_get_backend_ = false;
10804 }
10805 return bt;
10806
10807 case TYPE_INTERFACE:
10808 if (!this->seen_in_get_backend_)
10809 {
10810 this->seen_in_get_backend_ = true;
10811 base->interface_type()->finish_backend_methods(gogo);
10812 this->seen_in_get_backend_ = false;
10813 }
10814 return bt;
10815
10816 case TYPE_FUNCTION:
10817 // Don't build a circular data structure. GENERIC can't handle
10818 // it.
10819 if (this->seen_in_get_backend_)
10820 return gogo->backend()->circular_pointer_type(bt, true);
10821 this->seen_in_get_backend_ = true;
10822 bt1 = Type::get_named_base_btype(gogo, base);
10823 this->seen_in_get_backend_ = false;
10824 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
10825 bt = gogo->backend()->error_type();
10826 return bt;
10827
10828 case TYPE_POINTER:
10829 // Don't build a circular data structure. GENERIC can't handle
10830 // it.
10831 if (this->seen_in_get_backend_)
10832 return gogo->backend()->circular_pointer_type(bt, false);
10833 this->seen_in_get_backend_ = true;
10834 bt1 = Type::get_named_base_btype(gogo, base);
10835 this->seen_in_get_backend_ = false;
10836 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
10837 bt = gogo->backend()->error_type();
10838 return bt;
10839
10840 default:
10841 case TYPE_SINK:
10842 case TYPE_CALL_MULTIPLE_RESULT:
10843 case TYPE_NAMED:
10844 case TYPE_FORWARD:
10845 go_unreachable();
10846 }
10847
10848 go_unreachable();
10849 }
10850
10851 // Build a type descriptor for a named type.
10852
10853 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)10854 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
10855 {
10856 if (this->is_error_)
10857 return Expression::make_error(this->location_);
10858 if (name == NULL && this->is_alias_)
10859 {
10860 if (this->seen_alias_)
10861 return Expression::make_error(this->location_);
10862 this->seen_alias_ = true;
10863 Expression* ret = this->type_->type_descriptor(gogo, NULL);
10864 this->seen_alias_ = false;
10865 return ret;
10866 }
10867
10868 // If NAME is not NULL, then we don't really want the type
10869 // descriptor for this type; we want the descriptor for the
10870 // underlying type, giving it the name NAME.
10871 return this->named_type_descriptor(gogo, this->type_,
10872 name == NULL ? this : name);
10873 }
10874
10875 // Add to the reflection string. This is used mostly for the name of
10876 // the type used in a type descriptor, not for actual reflection
10877 // strings.
10878
10879 void
do_reflection(Gogo * gogo,std::string * ret) const10880 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
10881 {
10882 this->append_reflection_type_name(gogo, false, ret);
10883 }
10884
10885 // Add to the reflection string. For an alias we normally use the
10886 // real name, but if USE_ALIAS is true we use the alias name itself.
10887
10888 void
append_reflection_type_name(Gogo * gogo,bool use_alias,std::string * ret) const10889 Named_type::append_reflection_type_name(Gogo* gogo, bool use_alias,
10890 std::string* ret) const
10891 {
10892 if (this->is_error_)
10893 return;
10894 if (this->is_alias_ && !use_alias)
10895 {
10896 if (this->seen_alias_)
10897 return;
10898 this->seen_alias_ = true;
10899 this->append_reflection(this->type_, gogo, ret);
10900 this->seen_alias_ = false;
10901 return;
10902 }
10903 if (!this->is_builtin())
10904 {
10905 // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
10906 // make a unique reflection string, so that the type
10907 // canonicalization in the reflect package will work. In order
10908 // to be compatible with the gc compiler, we put tabs into the
10909 // package path, so that the reflect methods can discard it.
10910 const Package* package = this->named_object_->package();
10911 ret->push_back('\t');
10912 ret->append(package != NULL
10913 ? package->pkgpath_symbol()
10914 : gogo->pkgpath_symbol());
10915 ret->push_back('\t');
10916 ret->append(package != NULL
10917 ? package->package_name()
10918 : gogo->package_name());
10919 ret->push_back('.');
10920 }
10921 if (this->in_function_ != NULL)
10922 {
10923 ret->push_back('\t');
10924 const Typed_identifier* rcvr =
10925 this->in_function_->func_value()->type()->receiver();
10926 if (rcvr != NULL)
10927 {
10928 Named_type* rcvr_type = rcvr->type()->deref()->named_type();
10929 ret->append(Gogo::unpack_hidden_name(rcvr_type->name()));
10930 ret->push_back('.');
10931 }
10932 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
10933 ret->push_back('$');
10934 if (this->in_function_index_ > 0)
10935 {
10936 char buf[30];
10937 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
10938 ret->append(buf);
10939 ret->push_back('$');
10940 }
10941 ret->push_back('\t');
10942 }
10943 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
10944 }
10945
10946 // Import a named type. This is only used for export format versions
10947 // before version 3.
10948
10949 void
import_named_type(Import * imp,Named_type ** ptype)10950 Named_type::import_named_type(Import* imp, Named_type** ptype)
10951 {
10952 imp->require_c_string("type ");
10953 Type *type = imp->read_type();
10954 *ptype = type->named_type();
10955 go_assert(*ptype != NULL);
10956 imp->require_semicolon_if_old_version();
10957 imp->require_c_string("\n");
10958 }
10959
10960 // Export the type when it is referenced by another type. In this
10961 // case Export::export_type will already have issued the name. The
10962 // output always ends with a newline, since that is convenient if
10963 // there are methods.
10964
10965 void
do_export(Export * exp) const10966 Named_type::do_export(Export* exp) const
10967 {
10968 exp->write_type(this->type_);
10969 exp->write_c_string("\n");
10970
10971 // To save space, we only export the methods directly attached to
10972 // this type.
10973 Bindings* methods = this->local_methods_;
10974 if (methods == NULL)
10975 return;
10976
10977 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
10978 p != methods->end_definitions();
10979 ++p)
10980 {
10981 exp->write_c_string(" ");
10982 (*p)->export_named_object(exp);
10983 }
10984
10985 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
10986 p != methods->end_declarations();
10987 ++p)
10988 {
10989 if (p->second->is_function_declaration())
10990 {
10991 exp->write_c_string(" ");
10992 p->second->export_named_object(exp);
10993 }
10994 }
10995 }
10996
10997 // Make a named type.
10998
10999 Named_type*
make_named_type(Named_object * named_object,Type * type,Location location)11000 Type::make_named_type(Named_object* named_object, Type* type,
11001 Location location)
11002 {
11003 return new Named_type(named_object, type, location);
11004 }
11005
11006 // Finalize the methods for TYPE. It will be a named type or a struct
11007 // type. This sets *ALL_METHODS to the list of methods, and builds
11008 // all required stubs.
11009
11010 void
finalize_methods(Gogo * gogo,const Type * type,Location location,Methods ** all_methods)11011 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
11012 Methods** all_methods)
11013 {
11014 *all_methods = new Methods();
11015 std::vector<const Named_type*> seen;
11016 Type::add_methods_for_type(type, NULL, 0, false, false, &seen, *all_methods);
11017 if ((*all_methods)->empty())
11018 {
11019 delete *all_methods;
11020 *all_methods = NULL;
11021 }
11022 Type::build_stub_methods(gogo, type, *all_methods, location);
11023 }
11024
11025 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
11026 // build up the struct field indexes as we go. DEPTH is the depth of
11027 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
11028 // adding these methods for an anonymous field with pointer type.
11029 // NEEDS_STUB_METHOD is true if we need to use a stub method which
11030 // calls the real method. TYPES_SEEN is used to avoid infinite
11031 // recursion.
11032
11033 void
add_methods_for_type(const Type * type,const Method::Field_indexes * field_indexes,unsigned int depth,bool is_embedded_pointer,bool needs_stub_method,std::vector<const Named_type * > * seen,Methods * methods)11034 Type::add_methods_for_type(const Type* type,
11035 const Method::Field_indexes* field_indexes,
11036 unsigned int depth,
11037 bool is_embedded_pointer,
11038 bool needs_stub_method,
11039 std::vector<const Named_type*>* seen,
11040 Methods* methods)
11041 {
11042 // Pointer types may not have methods.
11043 if (type->points_to() != NULL)
11044 return;
11045
11046 const Named_type* nt = type->named_type();
11047 if (nt != NULL)
11048 {
11049 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
11050 p != seen->end();
11051 ++p)
11052 {
11053 if (*p == nt)
11054 return;
11055 }
11056
11057 seen->push_back(nt);
11058
11059 Type::add_local_methods_for_type(nt, field_indexes, depth,
11060 is_embedded_pointer, needs_stub_method,
11061 methods);
11062 }
11063
11064 Type::add_embedded_methods_for_type(type, field_indexes, depth,
11065 is_embedded_pointer, needs_stub_method,
11066 seen, methods);
11067
11068 // If we are called with depth > 0, then we are looking at an
11069 // anonymous field of a struct. If such a field has interface type,
11070 // then we need to add the interface methods. We don't want to add
11071 // them when depth == 0, because we will already handle them
11072 // following the usual rules for an interface type.
11073 if (depth > 0)
11074 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
11075
11076 if (nt != NULL)
11077 seen->pop_back();
11078 }
11079
11080 // Add the local methods for the named type NT to *METHODS. The
11081 // parameters are as for add_methods_to_type.
11082
11083 void
add_local_methods_for_type(const Named_type * nt,const Method::Field_indexes * field_indexes,unsigned int depth,bool is_embedded_pointer,bool needs_stub_method,Methods * methods)11084 Type::add_local_methods_for_type(const Named_type* nt,
11085 const Method::Field_indexes* field_indexes,
11086 unsigned int depth,
11087 bool is_embedded_pointer,
11088 bool needs_stub_method,
11089 Methods* methods)
11090 {
11091 const Bindings* local_methods = nt->local_methods();
11092 if (local_methods == NULL)
11093 return;
11094
11095 for (Bindings::const_declarations_iterator p =
11096 local_methods->begin_declarations();
11097 p != local_methods->end_declarations();
11098 ++p)
11099 {
11100 Named_object* no = p->second;
11101 bool is_value_method = (is_embedded_pointer
11102 || !Type::method_expects_pointer(no));
11103 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
11104 (needs_stub_method || depth > 0));
11105 if (!methods->insert(no->name(), m))
11106 delete m;
11107 }
11108 }
11109
11110 // Add the embedded methods for TYPE to *METHODS. These are the
11111 // methods attached to anonymous fields. The parameters are as for
11112 // add_methods_to_type.
11113
11114 void
add_embedded_methods_for_type(const Type * type,const Method::Field_indexes * field_indexes,unsigned int depth,bool is_embedded_pointer,bool needs_stub_method,std::vector<const Named_type * > * seen,Methods * methods)11115 Type::add_embedded_methods_for_type(const Type* type,
11116 const Method::Field_indexes* field_indexes,
11117 unsigned int depth,
11118 bool is_embedded_pointer,
11119 bool needs_stub_method,
11120 std::vector<const Named_type*>* seen,
11121 Methods* methods)
11122 {
11123 // Look for anonymous fields in TYPE. TYPE has fields if it is a
11124 // struct.
11125 const Struct_type* st = type->struct_type();
11126 if (st == NULL)
11127 return;
11128
11129 const Struct_field_list* fields = st->fields();
11130 if (fields == NULL)
11131 return;
11132
11133 unsigned int i = 0;
11134 for (Struct_field_list::const_iterator pf = fields->begin();
11135 pf != fields->end();
11136 ++pf, ++i)
11137 {
11138 if (!pf->is_anonymous())
11139 continue;
11140
11141 Type* ftype = pf->type();
11142 bool is_pointer = false;
11143 if (ftype->points_to() != NULL)
11144 {
11145 ftype = ftype->points_to();
11146 is_pointer = true;
11147 }
11148 Named_type* fnt = ftype->named_type();
11149 if (fnt == NULL)
11150 {
11151 // This is an error, but it will be diagnosed elsewhere.
11152 continue;
11153 }
11154
11155 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
11156 sub_field_indexes->next = field_indexes;
11157 sub_field_indexes->field_index = i;
11158
11159 Methods tmp_methods;
11160 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
11161 (is_embedded_pointer || is_pointer),
11162 (needs_stub_method
11163 || is_pointer
11164 || i > 0),
11165 seen,
11166 &tmp_methods);
11167 // Check if there are promoted methods that conflict with field names and
11168 // don't add them to the method map.
11169 for (Methods::const_iterator p = tmp_methods.begin();
11170 p != tmp_methods.end();
11171 ++p)
11172 {
11173 bool found = false;
11174 for (Struct_field_list::const_iterator fp = fields->begin();
11175 fp != fields->end();
11176 ++fp)
11177 {
11178 if (fp->field_name() == p->first)
11179 {
11180 found = true;
11181 break;
11182 }
11183 }
11184 if (!found &&
11185 !methods->insert(p->first, p->second))
11186 delete p->second;
11187 }
11188 }
11189 }
11190
11191 // If TYPE is an interface type, then add its method to *METHODS.
11192 // This is for interface methods attached to an anonymous field. The
11193 // parameters are as for add_methods_for_type.
11194
11195 void
add_interface_methods_for_type(const Type * type,const Method::Field_indexes * field_indexes,unsigned int depth,Methods * methods)11196 Type::add_interface_methods_for_type(const Type* type,
11197 const Method::Field_indexes* field_indexes,
11198 unsigned int depth,
11199 Methods* methods)
11200 {
11201 const Interface_type* it = type->interface_type();
11202 if (it == NULL)
11203 return;
11204
11205 const Typed_identifier_list* imethods = it->methods();
11206 if (imethods == NULL)
11207 return;
11208
11209 for (Typed_identifier_list::const_iterator pm = imethods->begin();
11210 pm != imethods->end();
11211 ++pm)
11212 {
11213 Function_type* fntype = pm->type()->function_type();
11214 if (fntype == NULL)
11215 {
11216 // This is an error, but it should be reported elsewhere
11217 // when we look at the methods for IT.
11218 continue;
11219 }
11220 go_assert(!fntype->is_method());
11221 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
11222 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
11223 field_indexes, depth);
11224 if (!methods->insert(pm->name(), m))
11225 delete m;
11226 }
11227 }
11228
11229 // Build stub methods for TYPE as needed. METHODS is the set of
11230 // methods for the type. A stub method may be needed when a type
11231 // inherits a method from an anonymous field. When we need the
11232 // address of the method, as in a type descriptor, we need to build a
11233 // little stub which does the required field dereferences and jumps to
11234 // the real method. LOCATION is the location of the type definition.
11235
11236 void
build_stub_methods(Gogo * gogo,const Type * type,const Methods * methods,Location location)11237 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
11238 Location location)
11239 {
11240 if (methods == NULL)
11241 return;
11242 for (Methods::const_iterator p = methods->begin();
11243 p != methods->end();
11244 ++p)
11245 {
11246 Method* m = p->second;
11247 if (m->is_ambiguous() || !m->needs_stub_method())
11248 continue;
11249
11250 const std::string& name(p->first);
11251
11252 // Build a stub method.
11253
11254 const Function_type* fntype = m->type();
11255
11256 static unsigned int counter;
11257 char buf[100];
11258 snprintf(buf, sizeof buf, "$this%u", counter);
11259 ++counter;
11260
11261 Type* receiver_type = const_cast<Type*>(type);
11262 if (!m->is_value_method())
11263 receiver_type = Type::make_pointer_type(receiver_type);
11264 Location receiver_location = m->receiver_location();
11265 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
11266 receiver_location);
11267
11268 const Typed_identifier_list* fnparams = fntype->parameters();
11269 Typed_identifier_list* stub_params;
11270 if (fnparams == NULL || fnparams->empty())
11271 stub_params = NULL;
11272 else
11273 {
11274 // We give each stub parameter a unique name.
11275 stub_params = new Typed_identifier_list();
11276 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
11277 pp != fnparams->end();
11278 ++pp)
11279 {
11280 char pbuf[100];
11281 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
11282 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
11283 pp->location()));
11284 ++counter;
11285 }
11286 }
11287
11288 const Typed_identifier_list* fnresults = fntype->results();
11289 Typed_identifier_list* stub_results;
11290 if (fnresults == NULL || fnresults->empty())
11291 stub_results = NULL;
11292 else
11293 {
11294 // We create the result parameters without any names, since
11295 // we won't refer to them.
11296 stub_results = new Typed_identifier_list();
11297 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
11298 pr != fnresults->end();
11299 ++pr)
11300 stub_results->push_back(Typed_identifier("", pr->type(),
11301 pr->location()));
11302 }
11303
11304 Function_type* stub_type = Type::make_function_type(receiver,
11305 stub_params,
11306 stub_results,
11307 fntype->location());
11308 if (fntype->is_varargs())
11309 stub_type->set_is_varargs();
11310
11311 // We only create the function in the package which creates the
11312 // type.
11313 const Package* package;
11314 if (type->named_type() == NULL)
11315 package = NULL;
11316 else
11317 package = type->named_type()->named_object()->package();
11318 std::string stub_name = gogo->stub_method_name(package, name);
11319 Named_object* stub;
11320 if (package != NULL)
11321 stub = Named_object::make_function_declaration(stub_name, package,
11322 stub_type, location);
11323 else
11324 {
11325 stub = gogo->start_function(stub_name, stub_type, false,
11326 fntype->location());
11327 Type::build_one_stub_method(gogo, m, buf, stub_params,
11328 fntype->is_varargs(), location);
11329 gogo->finish_function(fntype->location());
11330
11331 if (type->named_type() == NULL && stub->is_function())
11332 stub->func_value()->set_is_unnamed_type_stub_method();
11333 if (m->nointerface() && stub->is_function())
11334 stub->func_value()->set_nointerface();
11335 }
11336
11337 m->set_stub_object(stub);
11338 }
11339 }
11340
11341 // Build a stub method which adjusts the receiver as required to call
11342 // METHOD. RECEIVER_NAME is the name we used for the receiver.
11343 // PARAMS is the list of function parameters.
11344
11345 void
build_one_stub_method(Gogo * gogo,Method * method,const char * receiver_name,const Typed_identifier_list * params,bool is_varargs,Location location)11346 Type::build_one_stub_method(Gogo* gogo, Method* method,
11347 const char* receiver_name,
11348 const Typed_identifier_list* params,
11349 bool is_varargs,
11350 Location location)
11351 {
11352 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
11353 go_assert(receiver_object != NULL);
11354
11355 Expression* expr = Expression::make_var_reference(receiver_object, location);
11356 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
11357 if (expr->type()->points_to() == NULL)
11358 expr = Expression::make_unary(OPERATOR_AND, expr, location);
11359
11360 Expression_list* arguments;
11361 if (params == NULL || params->empty())
11362 arguments = NULL;
11363 else
11364 {
11365 arguments = new Expression_list();
11366 for (Typed_identifier_list::const_iterator p = params->begin();
11367 p != params->end();
11368 ++p)
11369 {
11370 Named_object* param = gogo->lookup(p->name(), NULL);
11371 go_assert(param != NULL);
11372 Expression* param_ref = Expression::make_var_reference(param,
11373 location);
11374 arguments->push_back(param_ref);
11375 }
11376 }
11377
11378 Expression* func = method->bind_method(expr, location);
11379 go_assert(func != NULL);
11380 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
11381 location);
11382
11383 gogo->add_statement(Statement::make_return_from_call(call, location));
11384 }
11385
11386 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
11387 // in reverse order.
11388
11389 Expression*
apply_field_indexes(Expression * expr,const Method::Field_indexes * field_indexes,Location location)11390 Type::apply_field_indexes(Expression* expr,
11391 const Method::Field_indexes* field_indexes,
11392 Location location)
11393 {
11394 if (field_indexes == NULL)
11395 return expr;
11396 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
11397 Struct_type* stype = expr->type()->deref()->struct_type();
11398 go_assert(stype != NULL
11399 && field_indexes->field_index < stype->field_count());
11400 if (expr->type()->struct_type() == NULL)
11401 {
11402 go_assert(expr->type()->points_to() != NULL);
11403 expr = Expression::make_dereference(expr, Expression::NIL_CHECK_DEFAULT,
11404 location);
11405 go_assert(expr->type()->struct_type() == stype);
11406 }
11407 return Expression::make_field_reference(expr, field_indexes->field_index,
11408 location);
11409 }
11410
11411 // Return whether NO is a method for which the receiver is a pointer.
11412
11413 bool
method_expects_pointer(const Named_object * no)11414 Type::method_expects_pointer(const Named_object* no)
11415 {
11416 const Function_type *fntype;
11417 if (no->is_function())
11418 fntype = no->func_value()->type();
11419 else if (no->is_function_declaration())
11420 fntype = no->func_declaration_value()->type();
11421 else
11422 go_unreachable();
11423 return fntype->receiver()->type()->points_to() != NULL;
11424 }
11425
11426 // Given a set of methods for a type, METHODS, return the method NAME,
11427 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
11428 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
11429 // but is ambiguous (and return NULL).
11430
11431 Method*
method_function(const Methods * methods,const std::string & name,bool * is_ambiguous)11432 Type::method_function(const Methods* methods, const std::string& name,
11433 bool* is_ambiguous)
11434 {
11435 if (is_ambiguous != NULL)
11436 *is_ambiguous = false;
11437 if (methods == NULL)
11438 return NULL;
11439 Methods::const_iterator p = methods->find(name);
11440 if (p == methods->end())
11441 return NULL;
11442 Method* m = p->second;
11443 if (m->is_ambiguous())
11444 {
11445 if (is_ambiguous != NULL)
11446 *is_ambiguous = true;
11447 return NULL;
11448 }
11449 return m;
11450 }
11451
11452 // Return a pointer to the interface method table for TYPE for the
11453 // interface INTERFACE.
11454
11455 Expression*
interface_method_table(Type * type,Interface_type * interface,bool is_pointer,Interface_method_tables ** method_tables,Interface_method_tables ** pointer_tables)11456 Type::interface_method_table(Type* type,
11457 Interface_type *interface,
11458 bool is_pointer,
11459 Interface_method_tables** method_tables,
11460 Interface_method_tables** pointer_tables)
11461 {
11462 go_assert(!interface->is_empty());
11463
11464 Interface_method_tables** pimt = is_pointer ? method_tables : pointer_tables;
11465
11466 if (*pimt == NULL)
11467 *pimt = new Interface_method_tables(5);
11468
11469 std::pair<Interface_type*, Expression*> val(interface, NULL);
11470 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
11471
11472 Location loc = Linemap::predeclared_location();
11473 if (ins.second)
11474 {
11475 // This is a new entry in the hash table.
11476 go_assert(ins.first->second == NULL);
11477 ins.first->second =
11478 Expression::make_interface_mtable_ref(interface, type, is_pointer, loc);
11479 }
11480 return Expression::make_unary(OPERATOR_AND, ins.first->second, loc);
11481 }
11482
11483 // Look for field or method NAME for TYPE. Return an Expression for
11484 // the field or method bound to EXPR. If there is no such field or
11485 // method, give an appropriate error and return an error expression.
11486
11487 Expression*
bind_field_or_method(Gogo * gogo,const Type * type,Expression * expr,const std::string & name,Location location)11488 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
11489 const std::string& name,
11490 Location location)
11491 {
11492 if (type->deref()->is_error_type())
11493 return Expression::make_error(location);
11494
11495 const Named_type* nt = type->deref()->named_type();
11496 const Struct_type* st = type->deref()->struct_type();
11497 const Interface_type* it = type->interface_type();
11498
11499 // If this is a pointer to a pointer, then it is possible that the
11500 // pointed-to type has methods.
11501 bool dereferenced = false;
11502 if (nt == NULL
11503 && st == NULL
11504 && it == NULL
11505 && type->points_to() != NULL
11506 && type->points_to()->points_to() != NULL)
11507 {
11508 expr = Expression::make_dereference(expr, Expression::NIL_CHECK_DEFAULT,
11509 location);
11510 type = type->points_to();
11511 if (type->deref()->is_error_type())
11512 return Expression::make_error(location);
11513 nt = type->points_to()->named_type();
11514 st = type->points_to()->struct_type();
11515 dereferenced = true;
11516 }
11517
11518 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
11519 || expr->is_addressable());
11520 std::vector<const Named_type*> seen;
11521 bool is_method = false;
11522 bool found_pointer_method = false;
11523 std::string ambig1;
11524 std::string ambig2;
11525 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
11526 &seen, NULL, &is_method,
11527 &found_pointer_method, &ambig1, &ambig2))
11528 {
11529 Expression* ret;
11530 if (!is_method)
11531 {
11532 go_assert(st != NULL);
11533 if (type->struct_type() == NULL)
11534 {
11535 if (dereferenced)
11536 {
11537 go_error_at(location, "pointer type has no field %qs",
11538 Gogo::message_name(name).c_str());
11539 return Expression::make_error(location);
11540 }
11541 go_assert(type->points_to() != NULL);
11542 expr = Expression::make_dereference(expr,
11543 Expression::NIL_CHECK_DEFAULT,
11544 location);
11545 go_assert(expr->type()->struct_type() == st);
11546 }
11547 ret = st->field_reference(expr, name, location);
11548 if (ret == NULL)
11549 {
11550 go_error_at(location, "type has no field %qs",
11551 Gogo::message_name(name).c_str());
11552 return Expression::make_error(location);
11553 }
11554 }
11555 else if (it != NULL && it->find_method(name) != NULL)
11556 ret = Expression::make_interface_field_reference(expr, name,
11557 location);
11558 else
11559 {
11560 Method* m;
11561 if (nt != NULL)
11562 m = nt->method_function(name, NULL);
11563 else if (st != NULL)
11564 m = st->method_function(name, NULL);
11565 else
11566 go_unreachable();
11567 go_assert(m != NULL);
11568 if (dereferenced)
11569 {
11570 go_error_at(location,
11571 "calling method %qs requires explicit dereference",
11572 Gogo::message_name(name).c_str());
11573 return Expression::make_error(location);
11574 }
11575 if (!m->is_value_method() && expr->type()->points_to() == NULL)
11576 expr = Expression::make_unary(OPERATOR_AND, expr, location);
11577 ret = m->bind_method(expr, location);
11578 }
11579 go_assert(ret != NULL);
11580 return ret;
11581 }
11582 else
11583 {
11584 if (Gogo::is_erroneous_name(name))
11585 {
11586 // An error was already reported.
11587 }
11588 else if (!ambig1.empty())
11589 go_error_at(location, "%qs is ambiguous via %qs and %qs",
11590 Gogo::message_name(name).c_str(), ambig1.c_str(),
11591 ambig2.c_str());
11592 else if (found_pointer_method)
11593 go_error_at(location, "method requires a pointer receiver");
11594 else if (nt == NULL && st == NULL && it == NULL)
11595 go_error_at(location,
11596 ("reference to field %qs in object which "
11597 "has no fields or methods"),
11598 Gogo::message_name(name).c_str());
11599 else
11600 {
11601 bool is_unexported;
11602 // The test for 'a' and 'z' is to handle builtin names,
11603 // which are not hidden.
11604 if (!Gogo::is_hidden_name(name) && (name[0] < 'a' || name[0] > 'z'))
11605 is_unexported = false;
11606 else
11607 {
11608 std::string unpacked = Gogo::unpack_hidden_name(name);
11609 seen.clear();
11610 is_unexported = Type::is_unexported_field_or_method(gogo, type,
11611 unpacked,
11612 &seen);
11613 }
11614 if (is_unexported)
11615 go_error_at(location, "reference to unexported field or method %qs",
11616 Gogo::message_name(name).c_str());
11617 else
11618 go_error_at(location, "reference to undefined field or method %qs",
11619 Gogo::message_name(name).c_str());
11620 }
11621 return Expression::make_error(location);
11622 }
11623 }
11624
11625 // Look in TYPE for a field or method named NAME, return true if one
11626 // is found. This looks through embedded anonymous fields and handles
11627 // ambiguity. If a method is found, sets *IS_METHOD to true;
11628 // otherwise, if a field is found, set it to false. If
11629 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
11630 // whose address can not be taken. SEEN is used to avoid infinite
11631 // recursion on invalid types.
11632
11633 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
11634 // method we couldn't use because it requires a pointer. LEVEL is
11635 // used for recursive calls, and can be NULL for a non-recursive call.
11636 // When this function returns false because it finds that the name is
11637 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
11638 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
11639 // will be unchanged.
11640
11641 // This function just returns whether or not there is a field or
11642 // method, and whether it is a field or method. It doesn't build an
11643 // expression to refer to it. If it is a method, we then look in the
11644 // list of all methods for the type. If it is a field, the search has
11645 // to be done again, looking only for fields, and building up the
11646 // expression as we go.
11647
11648 bool
find_field_or_method(const Type * type,const std::string & name,bool receiver_can_be_pointer,std::vector<const Named_type * > * seen,int * level,bool * is_method,bool * found_pointer_method,std::string * ambig1,std::string * ambig2)11649 Type::find_field_or_method(const Type* type,
11650 const std::string& name,
11651 bool receiver_can_be_pointer,
11652 std::vector<const Named_type*>* seen,
11653 int* level,
11654 bool* is_method,
11655 bool* found_pointer_method,
11656 std::string* ambig1,
11657 std::string* ambig2)
11658 {
11659 // Named types can have locally defined methods.
11660 const Named_type* nt = type->unalias()->named_type();
11661 if (nt == NULL && type->points_to() != NULL)
11662 nt = type->points_to()->unalias()->named_type();
11663 if (nt != NULL)
11664 {
11665 Named_object* no = nt->find_local_method(name);
11666 if (no != NULL)
11667 {
11668 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
11669 {
11670 *is_method = true;
11671 return true;
11672 }
11673
11674 // Record that we have found a pointer method in order to
11675 // give a better error message if we don't find anything
11676 // else.
11677 *found_pointer_method = true;
11678 }
11679
11680 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
11681 p != seen->end();
11682 ++p)
11683 {
11684 if (*p == nt)
11685 {
11686 // We've already seen this type when searching for methods.
11687 return false;
11688 }
11689 }
11690 }
11691
11692 // Interface types can have methods.
11693 const Interface_type* it = type->interface_type();
11694 if (it != NULL && it->find_method(name) != NULL)
11695 {
11696 *is_method = true;
11697 return true;
11698 }
11699
11700 // Struct types can have fields. They can also inherit fields and
11701 // methods from anonymous fields.
11702 const Struct_type* st = type->deref()->struct_type();
11703 if (st == NULL)
11704 return false;
11705 const Struct_field_list* fields = st->fields();
11706 if (fields == NULL)
11707 return false;
11708
11709 if (nt != NULL)
11710 seen->push_back(nt);
11711
11712 int found_level = 0;
11713 bool found_is_method = false;
11714 std::string found_ambig1;
11715 std::string found_ambig2;
11716 const Struct_field* found_parent = NULL;
11717 for (Struct_field_list::const_iterator pf = fields->begin();
11718 pf != fields->end();
11719 ++pf)
11720 {
11721 if (pf->is_field_name(name))
11722 {
11723 *is_method = false;
11724 if (nt != NULL)
11725 seen->pop_back();
11726 return true;
11727 }
11728
11729 if (!pf->is_anonymous())
11730 continue;
11731
11732 if (pf->type()->deref()->is_error_type()
11733 || pf->type()->deref()->is_undefined())
11734 continue;
11735
11736 Named_type* fnt = pf->type()->named_type();
11737 if (fnt == NULL)
11738 fnt = pf->type()->deref()->named_type();
11739 go_assert(fnt != NULL);
11740
11741 // Methods with pointer receivers on embedded field are
11742 // inherited by the pointer to struct, and also by the struct
11743 // type if the field itself is a pointer.
11744 bool can_be_pointer = (receiver_can_be_pointer
11745 || pf->type()->points_to() != NULL);
11746 int sublevel = level == NULL ? 1 : *level + 1;
11747 bool sub_is_method;
11748 std::string subambig1;
11749 std::string subambig2;
11750 bool subfound = Type::find_field_or_method(fnt,
11751 name,
11752 can_be_pointer,
11753 seen,
11754 &sublevel,
11755 &sub_is_method,
11756 found_pointer_method,
11757 &subambig1,
11758 &subambig2);
11759 if (!subfound)
11760 {
11761 if (!subambig1.empty())
11762 {
11763 // The name was found via this field, but is ambiguous.
11764 // if the ambiguity is lower or at the same level as
11765 // anything else we have already found, then we want to
11766 // pass the ambiguity back to the caller.
11767 if (found_level == 0 || sublevel <= found_level)
11768 {
11769 found_ambig1 = (Gogo::message_name(pf->field_name())
11770 + '.' + subambig1);
11771 found_ambig2 = (Gogo::message_name(pf->field_name())
11772 + '.' + subambig2);
11773 found_level = sublevel;
11774 }
11775 }
11776 }
11777 else
11778 {
11779 // The name was found via this field. Use the level to see
11780 // if we want to use this one, or whether it introduces an
11781 // ambiguity.
11782 if (found_level == 0 || sublevel < found_level)
11783 {
11784 found_level = sublevel;
11785 found_is_method = sub_is_method;
11786 found_ambig1.clear();
11787 found_ambig2.clear();
11788 found_parent = &*pf;
11789 }
11790 else if (sublevel > found_level)
11791 ;
11792 else if (found_ambig1.empty())
11793 {
11794 // We found an ambiguity.
11795 go_assert(found_parent != NULL);
11796 found_ambig1 = Gogo::message_name(found_parent->field_name());
11797 found_ambig2 = Gogo::message_name(pf->field_name());
11798 }
11799 else
11800 {
11801 // We found an ambiguity, but we already know of one.
11802 // Just report the earlier one.
11803 }
11804 }
11805 }
11806
11807 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
11808 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
11809 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
11810 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
11811
11812 if (nt != NULL)
11813 seen->pop_back();
11814
11815 if (found_level == 0)
11816 return false;
11817 else if (found_is_method
11818 && type->named_type() != NULL
11819 && type->points_to() != NULL)
11820 {
11821 // If this is a method inherited from a struct field in a named pointer
11822 // type, it is invalid to automatically dereference the pointer to the
11823 // struct to find this method.
11824 if (level != NULL)
11825 *level = found_level;
11826 *is_method = true;
11827 return false;
11828 }
11829 else if (!found_ambig1.empty())
11830 {
11831 go_assert(!found_ambig1.empty());
11832 ambig1->assign(found_ambig1);
11833 ambig2->assign(found_ambig2);
11834 if (level != NULL)
11835 *level = found_level;
11836 return false;
11837 }
11838 else
11839 {
11840 if (level != NULL)
11841 *level = found_level;
11842 *is_method = found_is_method;
11843 return true;
11844 }
11845 }
11846
11847 // Return whether NAME is an unexported field or method for TYPE.
11848
11849 bool
is_unexported_field_or_method(Gogo * gogo,const Type * type,const std::string & name,std::vector<const Named_type * > * seen)11850 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
11851 const std::string& name,
11852 std::vector<const Named_type*>* seen)
11853 {
11854 const Named_type* nt = type->named_type();
11855 if (nt == NULL)
11856 nt = type->deref()->named_type();
11857 if (nt != NULL)
11858 {
11859 if (nt->is_unexported_local_method(gogo, name))
11860 return true;
11861
11862 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
11863 p != seen->end();
11864 ++p)
11865 {
11866 if (*p == nt)
11867 {
11868 // We've already seen this type.
11869 return false;
11870 }
11871 }
11872 }
11873
11874 const Interface_type* it = type->interface_type();
11875 if (it != NULL && it->is_unexported_method(gogo, name))
11876 return true;
11877
11878 type = type->deref();
11879
11880 const Struct_type* st = type->struct_type();
11881 if (st != NULL && st->is_unexported_local_field(gogo, name))
11882 return true;
11883
11884 if (st == NULL)
11885 return false;
11886
11887 const Struct_field_list* fields = st->fields();
11888 if (fields == NULL)
11889 return false;
11890
11891 if (nt != NULL)
11892 seen->push_back(nt);
11893
11894 for (Struct_field_list::const_iterator pf = fields->begin();
11895 pf != fields->end();
11896 ++pf)
11897 {
11898 if (pf->is_anonymous()
11899 && !pf->type()->deref()->is_error_type()
11900 && !pf->type()->deref()->is_undefined())
11901 {
11902 Named_type* subtype = pf->type()->named_type();
11903 if (subtype == NULL)
11904 subtype = pf->type()->deref()->named_type();
11905 if (subtype == NULL)
11906 {
11907 // This is an error, but it will be diagnosed elsewhere.
11908 continue;
11909 }
11910 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
11911 {
11912 if (nt != NULL)
11913 seen->pop_back();
11914 return true;
11915 }
11916 }
11917 }
11918
11919 if (nt != NULL)
11920 seen->pop_back();
11921
11922 return false;
11923 }
11924
11925 // Class Forward_declaration.
11926
Forward_declaration_type(Named_object * named_object)11927 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
11928 : Type(TYPE_FORWARD),
11929 named_object_(named_object->resolve()), warned_(false)
11930 {
11931 go_assert(this->named_object_->is_unknown()
11932 || this->named_object_->is_type_declaration());
11933 }
11934
11935 // Return the named object.
11936
11937 Named_object*
named_object()11938 Forward_declaration_type::named_object()
11939 {
11940 return this->named_object_->resolve();
11941 }
11942
11943 const Named_object*
named_object() const11944 Forward_declaration_type::named_object() const
11945 {
11946 return this->named_object_->resolve();
11947 }
11948
11949 // Return the name of the forward declared type.
11950
11951 const std::string&
name() const11952 Forward_declaration_type::name() const
11953 {
11954 return this->named_object()->name();
11955 }
11956
11957 // Warn about a use of a type which has been declared but not defined.
11958
11959 void
warn() const11960 Forward_declaration_type::warn() const
11961 {
11962 Named_object* no = this->named_object_->resolve();
11963 if (no->is_unknown())
11964 {
11965 // The name was not defined anywhere.
11966 if (!this->warned_)
11967 {
11968 go_error_at(this->named_object_->location(),
11969 "use of undefined type %qs",
11970 no->message_name().c_str());
11971 this->warned_ = true;
11972 }
11973 }
11974 else if (no->is_type_declaration())
11975 {
11976 // The name was seen as a type, but the type was never defined.
11977 if (no->type_declaration_value()->using_type())
11978 {
11979 go_error_at(this->named_object_->location(),
11980 "use of undefined type %qs",
11981 no->message_name().c_str());
11982 this->warned_ = true;
11983 }
11984 }
11985 else
11986 {
11987 // The name was defined, but not as a type.
11988 if (!this->warned_)
11989 {
11990 go_error_at(this->named_object_->location(), "expected type");
11991 this->warned_ = true;
11992 }
11993 }
11994 }
11995
11996 // Get the base type of a declaration. This gives an error if the
11997 // type has not yet been defined.
11998
11999 Type*
real_type()12000 Forward_declaration_type::real_type()
12001 {
12002 if (this->is_defined())
12003 {
12004 Named_type* nt = this->named_object()->type_value();
12005 if (!nt->is_valid())
12006 return Type::make_error_type();
12007 return this->named_object()->type_value();
12008 }
12009 else
12010 {
12011 this->warn();
12012 return Type::make_error_type();
12013 }
12014 }
12015
12016 const Type*
real_type() const12017 Forward_declaration_type::real_type() const
12018 {
12019 if (this->is_defined())
12020 {
12021 const Named_type* nt = this->named_object()->type_value();
12022 if (!nt->is_valid())
12023 return Type::make_error_type();
12024 return this->named_object()->type_value();
12025 }
12026 else
12027 {
12028 this->warn();
12029 return Type::make_error_type();
12030 }
12031 }
12032
12033 // Return whether the base type is defined.
12034
12035 bool
is_defined() const12036 Forward_declaration_type::is_defined() const
12037 {
12038 return this->named_object()->is_type();
12039 }
12040
12041 // Add a method. This is used when methods are defined before the
12042 // type.
12043
12044 Named_object*
add_method(const std::string & name,Function * function)12045 Forward_declaration_type::add_method(const std::string& name,
12046 Function* function)
12047 {
12048 Named_object* no = this->named_object();
12049 if (no->is_unknown())
12050 no->declare_as_type();
12051 return no->type_declaration_value()->add_method(name, function);
12052 }
12053
12054 // Add a method declaration. This is used when methods are declared
12055 // before the type.
12056
12057 Named_object*
add_method_declaration(const std::string & name,Package * package,Function_type * type,Location location)12058 Forward_declaration_type::add_method_declaration(const std::string& name,
12059 Package* package,
12060 Function_type* type,
12061 Location location)
12062 {
12063 Named_object* no = this->named_object();
12064 if (no->is_unknown())
12065 no->declare_as_type();
12066 Type_declaration* td = no->type_declaration_value();
12067 return td->add_method_declaration(name, package, type, location);
12068 }
12069
12070 // Add an already created object as a method.
12071
12072 void
add_existing_method(Named_object * nom)12073 Forward_declaration_type::add_existing_method(Named_object* nom)
12074 {
12075 Named_object* no = this->named_object();
12076 if (no->is_unknown())
12077 no->declare_as_type();
12078 no->type_declaration_value()->add_existing_method(nom);
12079 }
12080
12081 // Traversal.
12082
12083 int
do_traverse(Traverse * traverse)12084 Forward_declaration_type::do_traverse(Traverse* traverse)
12085 {
12086 if (this->is_defined()
12087 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
12088 return TRAVERSE_EXIT;
12089 return TRAVERSE_CONTINUE;
12090 }
12091
12092 // Verify the type.
12093
12094 bool
do_verify()12095 Forward_declaration_type::do_verify()
12096 {
12097 if (!this->is_defined() && !this->is_nil_constant_as_type())
12098 {
12099 this->warn();
12100 return false;
12101 }
12102 return true;
12103 }
12104
12105 // Get the backend representation for the type.
12106
12107 Btype*
do_get_backend(Gogo * gogo)12108 Forward_declaration_type::do_get_backend(Gogo* gogo)
12109 {
12110 if (this->is_defined())
12111 return Type::get_named_base_btype(gogo, this->real_type());
12112
12113 if (this->warned_)
12114 return gogo->backend()->error_type();
12115
12116 // We represent an undefined type as a struct with no fields. That
12117 // should work fine for the backend, since the same case can arise
12118 // in C.
12119 std::vector<Backend::Btyped_identifier> fields;
12120 Btype* bt = gogo->backend()->struct_type(fields);
12121 return gogo->backend()->named_type(this->name(), bt,
12122 this->named_object()->location());
12123 }
12124
12125 // Build a type descriptor for a forwarded type.
12126
12127 Expression*
do_type_descriptor(Gogo * gogo,Named_type * name)12128 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
12129 {
12130 Location ploc = Linemap::predeclared_location();
12131 if (!this->is_defined())
12132 return Expression::make_error(ploc);
12133 else
12134 {
12135 Type* t = this->real_type();
12136 if (name != NULL)
12137 return this->named_type_descriptor(gogo, t, name);
12138 else
12139 return Expression::make_error(this->named_object_->location());
12140 }
12141 }
12142
12143 // The reflection string.
12144
12145 void
do_reflection(Gogo * gogo,std::string * ret) const12146 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
12147 {
12148 this->append_reflection(this->real_type(), gogo, ret);
12149 }
12150
12151 // Export a forward declaration. This can happen when a defined type
12152 // refers to a type which is only declared (and is presumably defined
12153 // in some other file in the same package).
12154
12155 void
do_export(Export *) const12156 Forward_declaration_type::do_export(Export*) const
12157 {
12158 // If there is a base type, that should be exported instead of this.
12159 go_assert(!this->is_defined());
12160
12161 // We don't output anything.
12162 }
12163
12164 // Make a forward declaration.
12165
12166 Type*
make_forward_declaration(Named_object * named_object)12167 Type::make_forward_declaration(Named_object* named_object)
12168 {
12169 return new Forward_declaration_type(named_object);
12170 }
12171
12172 // Class Typed_identifier_list.
12173
12174 // Sort the entries by name.
12175
12176 struct Typed_identifier_list_sort
12177 {
12178 public:
12179 bool
operator ()Typed_identifier_list_sort12180 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
12181 {
12182 return (Gogo::unpack_hidden_name(t1.name())
12183 < Gogo::unpack_hidden_name(t2.name()));
12184 }
12185 };
12186
12187 void
sort_by_name()12188 Typed_identifier_list::sort_by_name()
12189 {
12190 std::sort(this->entries_.begin(), this->entries_.end(),
12191 Typed_identifier_list_sort());
12192 }
12193
12194 // Traverse types.
12195
12196 int
traverse(Traverse * traverse) const12197 Typed_identifier_list::traverse(Traverse* traverse) const
12198 {
12199 for (Typed_identifier_list::const_iterator p = this->begin();
12200 p != this->end();
12201 ++p)
12202 {
12203 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
12204 return TRAVERSE_EXIT;
12205 }
12206 return TRAVERSE_CONTINUE;
12207 }
12208
12209 // Copy the list.
12210
12211 Typed_identifier_list*
copy() const12212 Typed_identifier_list::copy() const
12213 {
12214 Typed_identifier_list* ret = new Typed_identifier_list();
12215 for (Typed_identifier_list::const_iterator p = this->begin();
12216 p != this->end();
12217 ++p)
12218 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
12219 return ret;
12220 }
12221