3ae54a43806a221d2740e4f4344c483ad6acd189
[gcc.git] / gcc / go / gofrontend / types.cc
1 // types.cc -- Go frontend types.
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #include "toplev.h"
12 #include "intl.h"
13 #include "tree.h"
14 #include "gimple.h"
15 #include "real.h"
16 #include "convert.h"
17
18 #include "go-c.h"
19 #include "gogo.h"
20 #include "operator.h"
21 #include "expressions.h"
22 #include "statements.h"
23 #include "export.h"
24 #include "import.h"
25 #include "backend.h"
26 #include "types.h"
27
28 // Forward declarations so that we don't have to make types.h #include
29 // backend.h.
30
31 static void
32 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
33 bool use_placeholder,
34 std::vector<Backend::Btyped_identifier>* bfields);
35
36 static void
37 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
38 std::vector<Backend::Btyped_identifier>* bfields);
39
40 static void
41 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
42 bool use_placeholder,
43 std::vector<Backend::Btyped_identifier>* bfields);
44
45 // Class Type.
46
47 Type::Type(Type_classification classification)
48 : classification_(classification), btype_is_placeholder_(false),
49 btype_(NULL), type_descriptor_var_(NULL)
50 {
51 }
52
53 Type::~Type()
54 {
55 }
56
57 // Get the base type for a type--skip names and forward declarations.
58
59 Type*
60 Type::base()
61 {
62 switch (this->classification_)
63 {
64 case TYPE_NAMED:
65 return this->named_type()->named_base();
66 case TYPE_FORWARD:
67 return this->forward_declaration_type()->real_type()->base();
68 default:
69 return this;
70 }
71 }
72
73 const Type*
74 Type::base() const
75 {
76 switch (this->classification_)
77 {
78 case TYPE_NAMED:
79 return this->named_type()->named_base();
80 case TYPE_FORWARD:
81 return this->forward_declaration_type()->real_type()->base();
82 default:
83 return this;
84 }
85 }
86
87 // Skip defined forward declarations.
88
89 Type*
90 Type::forwarded()
91 {
92 Type* t = this;
93 Forward_declaration_type* ftype = t->forward_declaration_type();
94 while (ftype != NULL && ftype->is_defined())
95 {
96 t = ftype->real_type();
97 ftype = t->forward_declaration_type();
98 }
99 return t;
100 }
101
102 const Type*
103 Type::forwarded() const
104 {
105 const Type* t = this;
106 const Forward_declaration_type* ftype = t->forward_declaration_type();
107 while (ftype != NULL && ftype->is_defined())
108 {
109 t = ftype->real_type();
110 ftype = t->forward_declaration_type();
111 }
112 return t;
113 }
114
115 // If this is a named type, return it. Otherwise, return NULL.
116
117 Named_type*
118 Type::named_type()
119 {
120 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
121 }
122
123 const Named_type*
124 Type::named_type() const
125 {
126 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
127 }
128
129 // Return true if this type is not defined.
130
131 bool
132 Type::is_undefined() const
133 {
134 return this->forwarded()->forward_declaration_type() != NULL;
135 }
136
137 // Return true if this is a basic type: a type which is not composed
138 // of other types, and is not void.
139
140 bool
141 Type::is_basic_type() const
142 {
143 switch (this->classification_)
144 {
145 case TYPE_INTEGER:
146 case TYPE_FLOAT:
147 case TYPE_COMPLEX:
148 case TYPE_BOOLEAN:
149 case TYPE_STRING:
150 case TYPE_NIL:
151 return true;
152
153 case TYPE_ERROR:
154 case TYPE_VOID:
155 case TYPE_FUNCTION:
156 case TYPE_POINTER:
157 case TYPE_STRUCT:
158 case TYPE_ARRAY:
159 case TYPE_MAP:
160 case TYPE_CHANNEL:
161 case TYPE_INTERFACE:
162 return false;
163
164 case TYPE_NAMED:
165 case TYPE_FORWARD:
166 return this->base()->is_basic_type();
167
168 default:
169 go_unreachable();
170 }
171 }
172
173 // Return true if this is an abstract type.
174
175 bool
176 Type::is_abstract() const
177 {
178 switch (this->classification())
179 {
180 case TYPE_INTEGER:
181 return this->integer_type()->is_abstract();
182 case TYPE_FLOAT:
183 return this->float_type()->is_abstract();
184 case TYPE_COMPLEX:
185 return this->complex_type()->is_abstract();
186 case TYPE_STRING:
187 return this->is_abstract_string_type();
188 case TYPE_BOOLEAN:
189 return this->is_abstract_boolean_type();
190 default:
191 return false;
192 }
193 }
194
195 // Return a non-abstract version of an abstract type.
196
197 Type*
198 Type::make_non_abstract_type()
199 {
200 go_assert(this->is_abstract());
201 switch (this->classification())
202 {
203 case TYPE_INTEGER:
204 if (this->integer_type()->is_rune())
205 return Type::lookup_integer_type("int32");
206 else
207 return Type::lookup_integer_type("int");
208 case TYPE_FLOAT:
209 return Type::lookup_float_type("float64");
210 case TYPE_COMPLEX:
211 return Type::lookup_complex_type("complex128");
212 case TYPE_STRING:
213 return Type::lookup_string_type();
214 case TYPE_BOOLEAN:
215 return Type::lookup_bool_type();
216 default:
217 go_unreachable();
218 }
219 }
220
221 // Return true if this is an error type. Don't give an error if we
222 // try to dereference an undefined forwarding type, as this is called
223 // in the parser when the type may legitimately be undefined.
224
225 bool
226 Type::is_error_type() const
227 {
228 const Type* t = this->forwarded();
229 // Note that we return false for an undefined forward type.
230 switch (t->classification_)
231 {
232 case TYPE_ERROR:
233 return true;
234 case TYPE_NAMED:
235 return t->named_type()->is_named_error_type();
236 default:
237 return false;
238 }
239 }
240
241 // If this is a pointer type, return the type to which it points.
242 // Otherwise, return NULL.
243
244 Type*
245 Type::points_to() const
246 {
247 const Pointer_type* ptype = this->convert<const Pointer_type,
248 TYPE_POINTER>();
249 return ptype == NULL ? NULL : ptype->points_to();
250 }
251
252 // Return whether this is an open array type.
253
254 bool
255 Type::is_slice_type() const
256 {
257 return this->array_type() != NULL && this->array_type()->length() == NULL;
258 }
259
260 // Return whether this is the predeclared constant nil being used as a
261 // type.
262
263 bool
264 Type::is_nil_constant_as_type() const
265 {
266 const Type* t = this->forwarded();
267 if (t->forward_declaration_type() != NULL)
268 {
269 const Named_object* no = t->forward_declaration_type()->named_object();
270 if (no->is_unknown())
271 no = no->unknown_value()->real_named_object();
272 if (no != NULL
273 && no->is_const()
274 && no->const_value()->expr()->is_nil_expression())
275 return true;
276 }
277 return false;
278 }
279
280 // Traverse a type.
281
282 int
283 Type::traverse(Type* type, Traverse* traverse)
284 {
285 go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
286 || (traverse->traverse_mask()
287 & Traverse::traverse_expressions) != 0);
288 if (traverse->remember_type(type))
289 {
290 // We have already traversed this type.
291 return TRAVERSE_CONTINUE;
292 }
293 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
294 {
295 int t = traverse->type(type);
296 if (t == TRAVERSE_EXIT)
297 return TRAVERSE_EXIT;
298 else if (t == TRAVERSE_SKIP_COMPONENTS)
299 return TRAVERSE_CONTINUE;
300 }
301 // An array type has an expression which we need to traverse if
302 // traverse_expressions is set.
303 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
304 return TRAVERSE_EXIT;
305 return TRAVERSE_CONTINUE;
306 }
307
308 // Default implementation for do_traverse for child class.
309
310 int
311 Type::do_traverse(Traverse*)
312 {
313 return TRAVERSE_CONTINUE;
314 }
315
316 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
317 // then return true for all erroneous types; this is used to avoid
318 // cascading errors. If REASON is not NULL, optionally set *REASON to
319 // the reason the types are not identical.
320
321 bool
322 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
323 std::string* reason)
324 {
325 if (t1 == NULL || t2 == NULL)
326 {
327 // Something is wrong.
328 return errors_are_identical ? true : t1 == t2;
329 }
330
331 // Skip defined forward declarations.
332 t1 = t1->forwarded();
333 t2 = t2->forwarded();
334
335 // Ignore aliases for purposes of type identity.
336 if (t1->named_type() != NULL && t1->named_type()->is_alias())
337 t1 = t1->named_type()->real_type();
338 if (t2->named_type() != NULL && t2->named_type()->is_alias())
339 t2 = t2->named_type()->real_type();
340
341 if (t1 == t2)
342 return true;
343
344 // An undefined forward declaration is an error.
345 if (t1->forward_declaration_type() != NULL
346 || t2->forward_declaration_type() != NULL)
347 return errors_are_identical;
348
349 // Avoid cascading errors with error types.
350 if (t1->is_error_type() || t2->is_error_type())
351 {
352 if (errors_are_identical)
353 return true;
354 return t1->is_error_type() && t2->is_error_type();
355 }
356
357 // Get a good reason for the sink type. Note that the sink type on
358 // the left hand side of an assignment is handled in are_assignable.
359 if (t1->is_sink_type() || t2->is_sink_type())
360 {
361 if (reason != NULL)
362 *reason = "invalid use of _";
363 return false;
364 }
365
366 // A named type is only identical to itself.
367 if (t1->named_type() != NULL || t2->named_type() != NULL)
368 return false;
369
370 // Check type shapes.
371 if (t1->classification() != t2->classification())
372 return false;
373
374 switch (t1->classification())
375 {
376 case TYPE_VOID:
377 case TYPE_BOOLEAN:
378 case TYPE_STRING:
379 case TYPE_NIL:
380 // These types are always identical.
381 return true;
382
383 case TYPE_INTEGER:
384 return t1->integer_type()->is_identical(t2->integer_type());
385
386 case TYPE_FLOAT:
387 return t1->float_type()->is_identical(t2->float_type());
388
389 case TYPE_COMPLEX:
390 return t1->complex_type()->is_identical(t2->complex_type());
391
392 case TYPE_FUNCTION:
393 return t1->function_type()->is_identical(t2->function_type(),
394 false,
395 errors_are_identical,
396 reason);
397
398 case TYPE_POINTER:
399 return Type::are_identical(t1->points_to(), t2->points_to(),
400 errors_are_identical, reason);
401
402 case TYPE_STRUCT:
403 return t1->struct_type()->is_identical(t2->struct_type(),
404 errors_are_identical);
405
406 case TYPE_ARRAY:
407 return t1->array_type()->is_identical(t2->array_type(),
408 errors_are_identical);
409
410 case TYPE_MAP:
411 return t1->map_type()->is_identical(t2->map_type(),
412 errors_are_identical);
413
414 case TYPE_CHANNEL:
415 return t1->channel_type()->is_identical(t2->channel_type(),
416 errors_are_identical);
417
418 case TYPE_INTERFACE:
419 return t1->interface_type()->is_identical(t2->interface_type(),
420 errors_are_identical);
421
422 case TYPE_CALL_MULTIPLE_RESULT:
423 if (reason != NULL)
424 *reason = "invalid use of multiple-value function call";
425 return false;
426
427 default:
428 go_unreachable();
429 }
430 }
431
432 // Return true if it's OK to have a binary operation with types LHS
433 // and RHS. This is not used for shifts or comparisons.
434
435 bool
436 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
437 {
438 if (Type::are_identical(lhs, rhs, true, NULL))
439 return true;
440
441 // A constant of abstract bool type may be mixed with any bool type.
442 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
443 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
444 return true;
445
446 // A constant of abstract string type may be mixed with any string
447 // type.
448 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
449 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
450 return true;
451
452 lhs = lhs->base();
453 rhs = rhs->base();
454
455 // A constant of abstract integer, float, or complex type may be
456 // mixed with an integer, float, or complex type.
457 if ((rhs->is_abstract()
458 && (rhs->integer_type() != NULL
459 || rhs->float_type() != NULL
460 || rhs->complex_type() != NULL)
461 && (lhs->integer_type() != NULL
462 || lhs->float_type() != NULL
463 || lhs->complex_type() != NULL))
464 || (lhs->is_abstract()
465 && (lhs->integer_type() != NULL
466 || lhs->float_type() != NULL
467 || lhs->complex_type() != NULL)
468 && (rhs->integer_type() != NULL
469 || rhs->float_type() != NULL
470 || rhs->complex_type() != NULL)))
471 return true;
472
473 // The nil type may be compared to a pointer, an interface type, a
474 // slice type, a channel type, a map type, or a function type.
475 if (lhs->is_nil_type()
476 && (rhs->points_to() != NULL
477 || rhs->interface_type() != NULL
478 || rhs->is_slice_type()
479 || rhs->map_type() != NULL
480 || rhs->channel_type() != NULL
481 || rhs->function_type() != NULL))
482 return true;
483 if (rhs->is_nil_type()
484 && (lhs->points_to() != NULL
485 || lhs->interface_type() != NULL
486 || lhs->is_slice_type()
487 || lhs->map_type() != NULL
488 || lhs->channel_type() != NULL
489 || lhs->function_type() != NULL))
490 return true;
491
492 return false;
493 }
494
495 // Return true if a value with type T1 may be compared with a value of
496 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
497
498 bool
499 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
500 const Type *t2, std::string *reason)
501 {
502 if (t1 != t2
503 && !Type::are_assignable(t1, t2, NULL)
504 && !Type::are_assignable(t2, t1, NULL))
505 {
506 if (reason != NULL)
507 *reason = "incompatible types in binary expression";
508 return false;
509 }
510
511 if (!is_equality_op)
512 {
513 if (t1->integer_type() == NULL
514 && t1->float_type() == NULL
515 && !t1->is_string_type())
516 {
517 if (reason != NULL)
518 *reason = _("invalid comparison of non-ordered type");
519 return false;
520 }
521 }
522 else if (t1->is_slice_type()
523 || t1->map_type() != NULL
524 || t1->function_type() != NULL
525 || t2->is_slice_type()
526 || t2->map_type() != NULL
527 || t2->function_type() != NULL)
528 {
529 if (!t1->is_nil_type() && !t2->is_nil_type())
530 {
531 if (reason != NULL)
532 {
533 if (t1->is_slice_type() || t2->is_slice_type())
534 *reason = _("slice can only be compared to nil");
535 else if (t1->map_type() != NULL || t2->map_type() != NULL)
536 *reason = _("map can only be compared to nil");
537 else
538 *reason = _("func can only be compared to nil");
539
540 // Match 6g error messages.
541 if (t1->interface_type() != NULL || t2->interface_type() != NULL)
542 {
543 char buf[200];
544 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
545 reason->c_str());
546 *reason = buf;
547 }
548 }
549 return false;
550 }
551 }
552 else
553 {
554 if (!t1->is_boolean_type()
555 && t1->integer_type() == NULL
556 && t1->float_type() == NULL
557 && t1->complex_type() == NULL
558 && !t1->is_string_type()
559 && t1->points_to() == NULL
560 && t1->channel_type() == NULL
561 && t1->interface_type() == NULL
562 && t1->struct_type() == NULL
563 && t1->array_type() == NULL
564 && !t1->is_nil_type())
565 {
566 if (reason != NULL)
567 *reason = _("invalid comparison of non-comparable type");
568 return false;
569 }
570
571 if (t1->named_type() != NULL)
572 return t1->named_type()->named_type_is_comparable(reason);
573 else if (t2->named_type() != NULL)
574 return t2->named_type()->named_type_is_comparable(reason);
575 else if (t1->struct_type() != NULL)
576 {
577 const Struct_field_list* fields = t1->struct_type()->fields();
578 for (Struct_field_list::const_iterator p = fields->begin();
579 p != fields->end();
580 ++p)
581 {
582 if (Gogo::is_sink_name(p->field_name()))
583 continue;
584
585 if (!p->type()->is_comparable())
586 {
587 if (reason != NULL)
588 *reason = _("invalid comparison of non-comparable struct");
589 return false;
590 }
591 }
592 }
593 else if (t1->array_type() != NULL)
594 {
595 if (t1->array_type()->length()->is_nil_expression()
596 || !t1->array_type()->element_type()->is_comparable())
597 {
598 if (reason != NULL)
599 *reason = _("invalid comparison of non-comparable array");
600 return false;
601 }
602 }
603 }
604
605 return true;
606 }
607
608 // Return true if a value with type RHS may be assigned to a variable
609 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
610 // hidden fields are modified. If REASON is not NULL, set *REASON to
611 // the reason the types are not assignable.
612
613 bool
614 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
615 bool check_hidden_fields,
616 std::string* reason)
617 {
618 // Do some checks first. Make sure the types are defined.
619 if (rhs != NULL && !rhs->is_undefined())
620 {
621 if (rhs->is_void_type())
622 {
623 if (reason != NULL)
624 *reason = "non-value used as value";
625 return false;
626 }
627 if (rhs->is_call_multiple_result_type())
628 {
629 if (reason != NULL)
630 reason->assign(_("multiple-value function call in "
631 "single-value context"));
632 return false;
633 }
634 }
635
636 if (lhs != NULL && !lhs->is_undefined())
637 {
638 // Any value may be assigned to the blank identifier.
639 if (lhs->is_sink_type())
640 return true;
641
642 // All fields of a struct must be exported, or the assignment
643 // must be in the same package.
644 if (check_hidden_fields && rhs != NULL && !rhs->is_undefined())
645 {
646 if (lhs->has_hidden_fields(NULL, reason)
647 || rhs->has_hidden_fields(NULL, reason))
648 return false;
649 }
650 }
651
652 // Identical types are assignable.
653 if (Type::are_identical(lhs, rhs, true, reason))
654 return true;
655
656 // The types are assignable if they have identical underlying types
657 // and either LHS or RHS is not a named type.
658 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
659 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
660 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
661 return true;
662
663 // The types are assignable if LHS is an interface type and RHS
664 // implements the required methods.
665 const Interface_type* lhs_interface_type = lhs->interface_type();
666 if (lhs_interface_type != NULL)
667 {
668 if (lhs_interface_type->implements_interface(rhs, reason))
669 return true;
670 const Interface_type* rhs_interface_type = rhs->interface_type();
671 if (rhs_interface_type != NULL
672 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
673 reason))
674 return true;
675 }
676
677 // The type are assignable if RHS is a bidirectional channel type,
678 // LHS is a channel type, they have identical element types, and
679 // either LHS or RHS is not a named type.
680 if (lhs->channel_type() != NULL
681 && rhs->channel_type() != NULL
682 && rhs->channel_type()->may_send()
683 && rhs->channel_type()->may_receive()
684 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
685 && Type::are_identical(lhs->channel_type()->element_type(),
686 rhs->channel_type()->element_type(),
687 true,
688 reason))
689 return true;
690
691 // The nil type may be assigned to a pointer, function, slice, map,
692 // channel, or interface type.
693 if (rhs->is_nil_type()
694 && (lhs->points_to() != NULL
695 || lhs->function_type() != NULL
696 || lhs->is_slice_type()
697 || lhs->map_type() != NULL
698 || lhs->channel_type() != NULL
699 || lhs->interface_type() != NULL))
700 return true;
701
702 // An untyped numeric constant may be assigned to a numeric type if
703 // it is representable in that type.
704 if ((rhs->is_abstract()
705 && (rhs->integer_type() != NULL
706 || rhs->float_type() != NULL
707 || rhs->complex_type() != NULL))
708 && (lhs->integer_type() != NULL
709 || lhs->float_type() != NULL
710 || lhs->complex_type() != NULL))
711 return true;
712
713 // Give some better error messages.
714 if (reason != NULL && reason->empty())
715 {
716 if (rhs->interface_type() != NULL)
717 reason->assign(_("need explicit conversion"));
718 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
719 {
720 size_t len = (lhs->named_type()->name().length()
721 + rhs->named_type()->name().length()
722 + 100);
723 char* buf = new char[len];
724 snprintf(buf, len, _("cannot use type %s as type %s"),
725 rhs->named_type()->message_name().c_str(),
726 lhs->named_type()->message_name().c_str());
727 reason->assign(buf);
728 delete[] buf;
729 }
730 }
731
732 return false;
733 }
734
735 // Return true if a value with type RHS may be assigned to a variable
736 // with type LHS. If REASON is not NULL, set *REASON to the reason
737 // the types are not assignable.
738
739 bool
740 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
741 {
742 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
743 }
744
745 // Like are_assignable but don't check for hidden fields.
746
747 bool
748 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
749 std::string* reason)
750 {
751 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
752 }
753
754 // Return true if a value with type RHS may be converted to type LHS.
755 // If REASON is not NULL, set *REASON to the reason the types are not
756 // convertible.
757
758 bool
759 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
760 {
761 // The types are convertible if they are assignable.
762 if (Type::are_assignable(lhs, rhs, reason))
763 return true;
764
765 // The types are convertible if they have identical underlying
766 // types.
767 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
768 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
769 return true;
770
771 // The types are convertible if they are both unnamed pointer types
772 // and their pointer base types have identical underlying types.
773 if (lhs->named_type() == NULL
774 && rhs->named_type() == NULL
775 && lhs->points_to() != NULL
776 && rhs->points_to() != NULL
777 && (lhs->points_to()->named_type() != NULL
778 || rhs->points_to()->named_type() != NULL)
779 && Type::are_identical(lhs->points_to()->base(),
780 rhs->points_to()->base(),
781 true,
782 reason))
783 return true;
784
785 // Integer and floating point types are convertible to each other.
786 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
787 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
788 return true;
789
790 // Complex types are convertible to each other.
791 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
792 return true;
793
794 // An integer, or []byte, or []rune, may be converted to a string.
795 if (lhs->is_string_type())
796 {
797 if (rhs->integer_type() != NULL)
798 return true;
799 if (rhs->is_slice_type())
800 {
801 const Type* e = rhs->array_type()->element_type()->forwarded();
802 if (e->integer_type() != NULL
803 && (e->integer_type()->is_byte()
804 || e->integer_type()->is_rune()))
805 return true;
806 }
807 }
808
809 // A string may be converted to []byte or []rune.
810 if (rhs->is_string_type() && lhs->is_slice_type())
811 {
812 const Type* e = lhs->array_type()->element_type()->forwarded();
813 if (e->integer_type() != NULL
814 && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
815 return true;
816 }
817
818 // An unsafe.Pointer type may be converted to any pointer type or to
819 // uintptr, and vice-versa.
820 if (lhs->is_unsafe_pointer_type()
821 && (rhs->points_to() != NULL
822 || (rhs->integer_type() != NULL
823 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
824 return true;
825 if (rhs->is_unsafe_pointer_type()
826 && (lhs->points_to() != NULL
827 || (lhs->integer_type() != NULL
828 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
829 return true;
830
831 // Give a better error message.
832 if (reason != NULL)
833 {
834 if (reason->empty())
835 *reason = "invalid type conversion";
836 else
837 {
838 std::string s = "invalid type conversion (";
839 s += *reason;
840 s += ')';
841 *reason = s;
842 }
843 }
844
845 return false;
846 }
847
848 // Return whether this type has any hidden fields. This is only a
849 // possibility for a few types.
850
851 bool
852 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
853 {
854 switch (this->forwarded()->classification_)
855 {
856 case TYPE_NAMED:
857 return this->named_type()->named_type_has_hidden_fields(reason);
858 case TYPE_STRUCT:
859 return this->struct_type()->struct_has_hidden_fields(within, reason);
860 case TYPE_ARRAY:
861 return this->array_type()->array_has_hidden_fields(within, reason);
862 default:
863 return false;
864 }
865 }
866
867 // Return a hash code for the type to be used for method lookup.
868
869 unsigned int
870 Type::hash_for_method(Gogo* gogo) const
871 {
872 unsigned int ret = 0;
873 if (this->classification_ != TYPE_FORWARD)
874 ret += this->classification_;
875 return ret + this->do_hash_for_method(gogo);
876 }
877
878 // Default implementation of do_hash_for_method. This is appropriate
879 // for types with no subfields.
880
881 unsigned int
882 Type::do_hash_for_method(Gogo*) const
883 {
884 return 0;
885 }
886
887 // Return a hash code for a string, given a starting hash.
888
889 unsigned int
890 Type::hash_string(const std::string& s, unsigned int h)
891 {
892 const char* p = s.data();
893 size_t len = s.length();
894 for (; len > 0; --len)
895 {
896 h ^= *p++;
897 h*= 16777619;
898 }
899 return h;
900 }
901
902 // A hash table mapping unnamed types to the backend representation of
903 // those types.
904
905 Type::Type_btypes Type::type_btypes;
906
907 // Return a tree representing this type.
908
909 Btype*
910 Type::get_backend(Gogo* gogo)
911 {
912 if (this->btype_ != NULL)
913 {
914 if (this->btype_is_placeholder_ && gogo->named_types_are_converted())
915 this->finish_backend(gogo);
916 return this->btype_;
917 }
918
919 if (this->forward_declaration_type() != NULL
920 || this->named_type() != NULL)
921 return this->get_btype_without_hash(gogo);
922
923 if (this->is_error_type())
924 return gogo->backend()->error_type();
925
926 // To avoid confusing the backend, translate all identical Go types
927 // to the same backend representation. We use a hash table to do
928 // that. There is no need to use the hash table for named types, as
929 // named types are only identical to themselves.
930
931 std::pair<Type*, Btype*> val(this, NULL);
932 std::pair<Type_btypes::iterator, bool> ins =
933 Type::type_btypes.insert(val);
934 if (!ins.second && ins.first->second != NULL)
935 {
936 if (gogo != NULL && gogo->named_types_are_converted())
937 this->btype_ = ins.first->second;
938 return ins.first->second;
939 }
940
941 Btype* bt = this->get_btype_without_hash(gogo);
942
943 if (ins.first->second == NULL)
944 ins.first->second = bt;
945 else
946 {
947 // We have already created a backend representation for this
948 // type. This can happen when an unnamed type is defined using
949 // a named type which in turns uses an identical unnamed type.
950 // Use the tree we created earlier and ignore the one we just
951 // built.
952 bt = ins.first->second;
953 if (gogo == NULL || !gogo->named_types_are_converted())
954 return bt;
955 this->btype_ = bt;
956 }
957
958 return bt;
959 }
960
961 // Return the backend representation for a type without looking in the
962 // hash table for identical types. This is used for named types,
963 // since a named type is never identical to any other type.
964
965 Btype*
966 Type::get_btype_without_hash(Gogo* gogo)
967 {
968 if (this->btype_ == NULL)
969 {
970 Btype* bt = this->do_get_backend(gogo);
971
972 // For a recursive function or pointer type, we will temporarily
973 // return a circular pointer type during the recursion. We
974 // don't want to record that for a forwarding type, as it may
975 // confuse us later.
976 if (this->forward_declaration_type() != NULL
977 && gogo->backend()->is_circular_pointer_type(bt))
978 return bt;
979
980 if (gogo == NULL || !gogo->named_types_are_converted())
981 return bt;
982
983 this->btype_ = bt;
984 }
985 return this->btype_;
986 }
987
988 // Get the backend representation of a type without forcing the
989 // creation of the backend representation of all supporting types.
990 // This will return a backend type that has the correct size but may
991 // be incomplete. E.g., a pointer will just be a placeholder pointer,
992 // and will not contain the final representation of the type to which
993 // it points. This is used while converting all named types to the
994 // backend representation, to avoid problems with indirect references
995 // to types which are not yet complete. When this is called, the
996 // sizes of all direct references (e.g., a struct field) should be
997 // known, but the sizes of indirect references (e.g., the type to
998 // which a pointer points) may not.
999
1000 Btype*
1001 Type::get_backend_placeholder(Gogo* gogo)
1002 {
1003 if (gogo->named_types_are_converted())
1004 return this->get_backend(gogo);
1005 if (this->btype_ != NULL)
1006 return this->btype_;
1007
1008 Btype* bt;
1009 switch (this->classification_)
1010 {
1011 case TYPE_ERROR:
1012 case TYPE_VOID:
1013 case TYPE_BOOLEAN:
1014 case TYPE_INTEGER:
1015 case TYPE_FLOAT:
1016 case TYPE_COMPLEX:
1017 case TYPE_STRING:
1018 case TYPE_NIL:
1019 // These are simple types that can just be created directly.
1020 return this->get_backend(gogo);
1021
1022 case TYPE_FUNCTION:
1023 {
1024 Location loc = this->function_type()->location();
1025 bt = gogo->backend()->placeholder_pointer_type("", loc, true);
1026 }
1027 break;
1028
1029 case TYPE_POINTER:
1030 {
1031 Location loc = Linemap::unknown_location();
1032 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1033 }
1034 break;
1035
1036 case TYPE_STRUCT:
1037 // We don't have to make the struct itself be a placeholder. We
1038 // are promised that we know the sizes of the struct fields.
1039 // But we may have to use a placeholder for any particular
1040 // struct field.
1041 {
1042 std::vector<Backend::Btyped_identifier> bfields;
1043 get_backend_struct_fields(gogo, this->struct_type()->fields(),
1044 true, &bfields);
1045 bt = gogo->backend()->struct_type(bfields);
1046 }
1047 break;
1048
1049 case TYPE_ARRAY:
1050 if (this->is_slice_type())
1051 {
1052 std::vector<Backend::Btyped_identifier> bfields;
1053 get_backend_slice_fields(gogo, this->array_type(), true, &bfields);
1054 bt = gogo->backend()->struct_type(bfields);
1055 }
1056 else
1057 {
1058 Btype* element = this->array_type()->get_backend_element(gogo, true);
1059 Bexpression* len = this->array_type()->get_backend_length(gogo);
1060 bt = gogo->backend()->array_type(element, len);
1061 }
1062 break;
1063
1064 case TYPE_MAP:
1065 case TYPE_CHANNEL:
1066 // All maps and channels have the same backend representation.
1067 return this->get_backend(gogo);
1068
1069 case TYPE_INTERFACE:
1070 if (this->interface_type()->is_empty())
1071 return Interface_type::get_backend_empty_interface_type(gogo);
1072 else
1073 {
1074 std::vector<Backend::Btyped_identifier> bfields;
1075 get_backend_interface_fields(gogo, this->interface_type(), true,
1076 &bfields);
1077 bt = gogo->backend()->struct_type(bfields);
1078 }
1079 break;
1080
1081 case TYPE_NAMED:
1082 case TYPE_FORWARD:
1083 // Named types keep track of their own dependencies and manage
1084 // their own placeholders.
1085 return this->get_backend(gogo);
1086
1087 case TYPE_SINK:
1088 case TYPE_CALL_MULTIPLE_RESULT:
1089 default:
1090 go_unreachable();
1091 }
1092
1093 this->btype_ = bt;
1094 this->btype_is_placeholder_ = true;
1095 return bt;
1096 }
1097
1098 // Complete the backend representation. This is called for a type
1099 // using a placeholder type.
1100
1101 void
1102 Type::finish_backend(Gogo* gogo)
1103 {
1104 go_assert(this->btype_ != NULL);
1105 if (!this->btype_is_placeholder_)
1106 return;
1107
1108 switch (this->classification_)
1109 {
1110 case TYPE_ERROR:
1111 case TYPE_VOID:
1112 case TYPE_BOOLEAN:
1113 case TYPE_INTEGER:
1114 case TYPE_FLOAT:
1115 case TYPE_COMPLEX:
1116 case TYPE_STRING:
1117 case TYPE_NIL:
1118 go_unreachable();
1119
1120 case TYPE_FUNCTION:
1121 {
1122 Btype* bt = this->do_get_backend(gogo);
1123 if (!gogo->backend()->set_placeholder_function_type(this->btype_, bt))
1124 go_assert(saw_errors());
1125 }
1126 break;
1127
1128 case TYPE_POINTER:
1129 {
1130 Btype* bt = this->do_get_backend(gogo);
1131 if (!gogo->backend()->set_placeholder_pointer_type(this->btype_, bt))
1132 go_assert(saw_errors());
1133 }
1134 break;
1135
1136 case TYPE_STRUCT:
1137 // The struct type itself is done, but we have to make sure that
1138 // all the field types are converted.
1139 this->struct_type()->finish_backend_fields(gogo);
1140 break;
1141
1142 case TYPE_ARRAY:
1143 // The array type itself is done, but make sure the element type
1144 // is converted.
1145 this->array_type()->finish_backend_element(gogo);
1146 break;
1147
1148 case TYPE_MAP:
1149 case TYPE_CHANNEL:
1150 go_unreachable();
1151
1152 case TYPE_INTERFACE:
1153 // The interface type itself is done, but make sure the method
1154 // types are converted.
1155 this->interface_type()->finish_backend_methods(gogo);
1156 break;
1157
1158 case TYPE_NAMED:
1159 case TYPE_FORWARD:
1160 go_unreachable();
1161
1162 case TYPE_SINK:
1163 case TYPE_CALL_MULTIPLE_RESULT:
1164 default:
1165 go_unreachable();
1166 }
1167
1168 this->btype_is_placeholder_ = false;
1169 }
1170
1171 // Return a pointer to the type descriptor for this type.
1172
1173 tree
1174 Type::type_descriptor_pointer(Gogo* gogo, Location location)
1175 {
1176 Type* t = this->forwarded();
1177 if (t->named_type() != NULL && t->named_type()->is_alias())
1178 t = t->named_type()->real_type();
1179 if (t->type_descriptor_var_ == NULL)
1180 {
1181 t->make_type_descriptor_var(gogo);
1182 go_assert(t->type_descriptor_var_ != NULL);
1183 }
1184 tree var_tree = var_to_tree(t->type_descriptor_var_);
1185 if (var_tree == error_mark_node)
1186 return error_mark_node;
1187 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
1188 }
1189
1190 // A mapping from unnamed types to type descriptor variables.
1191
1192 Type::Type_descriptor_vars Type::type_descriptor_vars;
1193
1194 // Build the type descriptor for this type.
1195
1196 void
1197 Type::make_type_descriptor_var(Gogo* gogo)
1198 {
1199 go_assert(this->type_descriptor_var_ == NULL);
1200
1201 Named_type* nt = this->named_type();
1202
1203 // We can have multiple instances of unnamed types, but we only want
1204 // to emit the type descriptor once. We use a hash table. This is
1205 // not necessary for named types, as they are unique, and we store
1206 // the type descriptor in the type itself.
1207 Bvariable** phash = NULL;
1208 if (nt == NULL)
1209 {
1210 Bvariable* bvnull = NULL;
1211 std::pair<Type_descriptor_vars::iterator, bool> ins =
1212 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1213 if (!ins.second)
1214 {
1215 // We've already build a type descriptor for this type.
1216 this->type_descriptor_var_ = ins.first->second;
1217 return;
1218 }
1219 phash = &ins.first->second;
1220 }
1221
1222 std::string var_name = this->type_descriptor_var_name(gogo, nt);
1223
1224 // Build the contents of the type descriptor.
1225 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1226
1227 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1228
1229 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1230
1231 const Package* dummy;
1232 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1233 {
1234 this->type_descriptor_var_ =
1235 gogo->backend()->immutable_struct_reference(var_name,
1236 initializer_btype,
1237 loc);
1238 if (phash != NULL)
1239 *phash = this->type_descriptor_var_;
1240 return;
1241 }
1242
1243 // See if this type descriptor can appear in multiple packages.
1244 bool is_common = false;
1245 if (nt != NULL)
1246 {
1247 // We create the descriptor for a builtin type whenever we need
1248 // it.
1249 is_common = nt->is_builtin();
1250 }
1251 else
1252 {
1253 // This is an unnamed type. The descriptor could be defined in
1254 // any package where it is needed, and the linker will pick one
1255 // descriptor to keep.
1256 is_common = true;
1257 }
1258
1259 // We are going to build the type descriptor in this package. We
1260 // must create the variable before we convert the initializer to the
1261 // backend representation, because the initializer may refer to the
1262 // type descriptor of this type. By setting type_descriptor_var_ we
1263 // ensure that type_descriptor_pointer will work if called while
1264 // converting INITIALIZER.
1265
1266 this->type_descriptor_var_ =
1267 gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
1268 loc);
1269 if (phash != NULL)
1270 *phash = this->type_descriptor_var_;
1271
1272 Translate_context context(gogo, NULL, NULL, NULL);
1273 context.set_is_const();
1274 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
1275
1276 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1277 var_name, is_common,
1278 initializer_btype, loc,
1279 binitializer);
1280 }
1281
1282 // Return the name of the type descriptor variable. If NT is not
1283 // NULL, use it to get the name. Otherwise this is an unnamed type.
1284
1285 std::string
1286 Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
1287 {
1288 if (nt == NULL)
1289 return "__go_td_" + this->mangled_name(gogo);
1290
1291 Named_object* no = nt->named_object();
1292 unsigned int index;
1293 const Named_object* in_function = nt->in_function(&index);
1294 std::string ret = "__go_tdn_";
1295 if (nt->is_builtin())
1296 go_assert(in_function == NULL);
1297 else
1298 {
1299 const std::string& pkgpath(no->package() == NULL
1300 ? gogo->pkgpath_symbol()
1301 : no->package()->pkgpath_symbol());
1302 ret.append(pkgpath);
1303 ret.append(1, '.');
1304 if (in_function != NULL)
1305 {
1306 ret.append(Gogo::unpack_hidden_name(in_function->name()));
1307 ret.append(1, '.');
1308 if (index > 0)
1309 {
1310 char buf[30];
1311 snprintf(buf, sizeof buf, "%u", index);
1312 ret.append(buf);
1313 ret.append(1, '.');
1314 }
1315 }
1316 }
1317
1318 // FIXME: This adds in pkgpath twice for hidden symbols, which is
1319 // pointless.
1320 const std::string& name(no->name());
1321 if (!Gogo::is_hidden_name(name))
1322 ret.append(name);
1323 else
1324 {
1325 ret.append(1, '.');
1326 ret.append(Gogo::pkgpath_for_symbol(Gogo::hidden_name_pkgpath(name)));
1327 ret.append(1, '.');
1328 ret.append(Gogo::unpack_hidden_name(name));
1329 }
1330
1331 return ret;
1332 }
1333
1334 // Return true if this type descriptor is defined in a different
1335 // package. If this returns true it sets *PACKAGE to the package.
1336
1337 bool
1338 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1339 const Package** package)
1340 {
1341 if (nt != NULL)
1342 {
1343 if (nt->named_object()->package() != NULL)
1344 {
1345 // This is a named type defined in a different package. The
1346 // type descriptor should be defined in that package.
1347 *package = nt->named_object()->package();
1348 return true;
1349 }
1350 }
1351 else
1352 {
1353 if (this->points_to() != NULL
1354 && this->points_to()->named_type() != NULL
1355 && this->points_to()->named_type()->named_object()->package() != NULL)
1356 {
1357 // This is an unnamed pointer to a named type defined in a
1358 // different package. The descriptor should be defined in
1359 // that package.
1360 *package = this->points_to()->named_type()->named_object()->package();
1361 return true;
1362 }
1363 }
1364 return false;
1365 }
1366
1367 // Return a composite literal for a type descriptor.
1368
1369 Expression*
1370 Type::type_descriptor(Gogo* gogo, Type* type)
1371 {
1372 return type->do_type_descriptor(gogo, NULL);
1373 }
1374
1375 // Return a composite literal for a type descriptor with a name.
1376
1377 Expression*
1378 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1379 {
1380 go_assert(name != NULL && type->named_type() != name);
1381 return type->do_type_descriptor(gogo, name);
1382 }
1383
1384 // Make a builtin struct type from a list of fields. The fields are
1385 // pairs of a name and a type.
1386
1387 Struct_type*
1388 Type::make_builtin_struct_type(int nfields, ...)
1389 {
1390 va_list ap;
1391 va_start(ap, nfields);
1392
1393 Location bloc = Linemap::predeclared_location();
1394 Struct_field_list* sfl = new Struct_field_list();
1395 for (int i = 0; i < nfields; i++)
1396 {
1397 const char* field_name = va_arg(ap, const char *);
1398 Type* type = va_arg(ap, Type*);
1399 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1400 }
1401
1402 va_end(ap);
1403
1404 return Type::make_struct_type(sfl, bloc);
1405 }
1406
1407 // A list of builtin named types.
1408
1409 std::vector<Named_type*> Type::named_builtin_types;
1410
1411 // Make a builtin named type.
1412
1413 Named_type*
1414 Type::make_builtin_named_type(const char* name, Type* type)
1415 {
1416 Location bloc = Linemap::predeclared_location();
1417 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1418 Named_type* ret = no->type_value();
1419 Type::named_builtin_types.push_back(ret);
1420 return ret;
1421 }
1422
1423 // Convert the named builtin types.
1424
1425 void
1426 Type::convert_builtin_named_types(Gogo* gogo)
1427 {
1428 for (std::vector<Named_type*>::const_iterator p =
1429 Type::named_builtin_types.begin();
1430 p != Type::named_builtin_types.end();
1431 ++p)
1432 {
1433 bool r = (*p)->verify();
1434 go_assert(r);
1435 (*p)->convert(gogo);
1436 }
1437 }
1438
1439 // Return the type of a type descriptor. We should really tie this to
1440 // runtime.Type rather than copying it. This must match commonType in
1441 // libgo/go/runtime/type.go.
1442
1443 Type*
1444 Type::make_type_descriptor_type()
1445 {
1446 static Type* ret;
1447 if (ret == NULL)
1448 {
1449 Location bloc = Linemap::predeclared_location();
1450
1451 Type* uint8_type = Type::lookup_integer_type("uint8");
1452 Type* uint32_type = Type::lookup_integer_type("uint32");
1453 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1454 Type* string_type = Type::lookup_string_type();
1455 Type* pointer_string_type = Type::make_pointer_type(string_type);
1456
1457 // This is an unnamed version of unsafe.Pointer. Perhaps we
1458 // should use the named version instead, although that would
1459 // require us to create the unsafe package if it has not been
1460 // imported. It probably doesn't matter.
1461 Type* void_type = Type::make_void_type();
1462 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1463
1464 // Forward declaration for the type descriptor type.
1465 Named_object* named_type_descriptor_type =
1466 Named_object::make_type_declaration("commonType", NULL, bloc);
1467 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1468 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1469
1470 // The type of a method on a concrete type.
1471 Struct_type* method_type =
1472 Type::make_builtin_struct_type(5,
1473 "name", pointer_string_type,
1474 "pkgPath", pointer_string_type,
1475 "mtyp", pointer_type_descriptor_type,
1476 "typ", pointer_type_descriptor_type,
1477 "tfn", unsafe_pointer_type);
1478 Named_type* named_method_type =
1479 Type::make_builtin_named_type("method", method_type);
1480
1481 // Information for types with a name or methods.
1482 Type* slice_named_method_type =
1483 Type::make_array_type(named_method_type, NULL);
1484 Struct_type* uncommon_type =
1485 Type::make_builtin_struct_type(3,
1486 "name", pointer_string_type,
1487 "pkgPath", pointer_string_type,
1488 "methods", slice_named_method_type);
1489 Named_type* named_uncommon_type =
1490 Type::make_builtin_named_type("uncommonType", uncommon_type);
1491
1492 Type* pointer_uncommon_type =
1493 Type::make_pointer_type(named_uncommon_type);
1494
1495 // The type descriptor type.
1496
1497 Typed_identifier_list* params = new Typed_identifier_list();
1498 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1499 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1500
1501 Typed_identifier_list* results = new Typed_identifier_list();
1502 results->push_back(Typed_identifier("", uintptr_type, bloc));
1503
1504 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1505
1506 params = new Typed_identifier_list();
1507 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1508 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1509 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1510
1511 results = new Typed_identifier_list();
1512 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1513
1514 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1515 bloc);
1516
1517 Struct_type* type_descriptor_type =
1518 Type::make_builtin_struct_type(10,
1519 "Kind", uint8_type,
1520 "align", uint8_type,
1521 "fieldAlign", uint8_type,
1522 "size", uintptr_type,
1523 "hash", uint32_type,
1524 "hashfn", hashfn_type,
1525 "equalfn", equalfn_type,
1526 "string", pointer_string_type,
1527 "", pointer_uncommon_type,
1528 "ptrToThis",
1529 pointer_type_descriptor_type);
1530
1531 Named_type* named = Type::make_builtin_named_type("commonType",
1532 type_descriptor_type);
1533
1534 named_type_descriptor_type->set_type_value(named);
1535
1536 ret = named;
1537 }
1538
1539 return ret;
1540 }
1541
1542 // Make the type of a pointer to a type descriptor as represented in
1543 // Go.
1544
1545 Type*
1546 Type::make_type_descriptor_ptr_type()
1547 {
1548 static Type* ret;
1549 if (ret == NULL)
1550 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1551 return ret;
1552 }
1553
1554 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1555 // hash code for this type and which compare whether two values of
1556 // this type are equal. If NAME is not NULL it is the name of this
1557 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1558 // functions, for convenience; they may be NULL.
1559
1560 void
1561 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1562 Function_type* equal_fntype, Named_object** hash_fn,
1563 Named_object** equal_fn)
1564 {
1565 if (hash_fntype == NULL || equal_fntype == NULL)
1566 {
1567 Location bloc = Linemap::predeclared_location();
1568
1569 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1570 Type* void_type = Type::make_void_type();
1571 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1572
1573 if (hash_fntype == NULL)
1574 {
1575 Typed_identifier_list* params = new Typed_identifier_list();
1576 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1577 bloc));
1578 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1579
1580 Typed_identifier_list* results = new Typed_identifier_list();
1581 results->push_back(Typed_identifier("", uintptr_type, bloc));
1582
1583 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1584 }
1585 if (equal_fntype == NULL)
1586 {
1587 Typed_identifier_list* params = new Typed_identifier_list();
1588 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1589 bloc));
1590 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1591 bloc));
1592 params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1593
1594 Typed_identifier_list* results = new Typed_identifier_list();
1595 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1596 bloc));
1597
1598 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1599 }
1600 }
1601
1602 const char* hash_fnname;
1603 const char* equal_fnname;
1604 if (this->compare_is_identity(gogo))
1605 {
1606 hash_fnname = "__go_type_hash_identity";
1607 equal_fnname = "__go_type_equal_identity";
1608 }
1609 else if (!this->is_comparable())
1610 {
1611 hash_fnname = "__go_type_hash_error";
1612 equal_fnname = "__go_type_equal_error";
1613 }
1614 else
1615 {
1616 switch (this->base()->classification())
1617 {
1618 case Type::TYPE_ERROR:
1619 case Type::TYPE_VOID:
1620 case Type::TYPE_NIL:
1621 case Type::TYPE_FUNCTION:
1622 case Type::TYPE_MAP:
1623 // For these types is_comparable should have returned false.
1624 go_unreachable();
1625
1626 case Type::TYPE_BOOLEAN:
1627 case Type::TYPE_INTEGER:
1628 case Type::TYPE_POINTER:
1629 case Type::TYPE_CHANNEL:
1630 // For these types compare_is_identity should have returned true.
1631 go_unreachable();
1632
1633 case Type::TYPE_FLOAT:
1634 hash_fnname = "__go_type_hash_float";
1635 equal_fnname = "__go_type_equal_float";
1636 break;
1637
1638 case Type::TYPE_COMPLEX:
1639 hash_fnname = "__go_type_hash_complex";
1640 equal_fnname = "__go_type_equal_complex";
1641 break;
1642
1643 case Type::TYPE_STRING:
1644 hash_fnname = "__go_type_hash_string";
1645 equal_fnname = "__go_type_equal_string";
1646 break;
1647
1648 case Type::TYPE_STRUCT:
1649 {
1650 // This is a struct which can not be compared using a
1651 // simple identity function. We need to build a function
1652 // for comparison.
1653 this->specific_type_functions(gogo, name, hash_fntype,
1654 equal_fntype, hash_fn, equal_fn);
1655 return;
1656 }
1657
1658 case Type::TYPE_ARRAY:
1659 if (this->is_slice_type())
1660 {
1661 // Type::is_compatible_for_comparison should have
1662 // returned false.
1663 go_unreachable();
1664 }
1665 else
1666 {
1667 // This is an array which can not be compared using a
1668 // simple identity function. We need to build a
1669 // function for comparison.
1670 this->specific_type_functions(gogo, name, hash_fntype,
1671 equal_fntype, hash_fn, equal_fn);
1672 return;
1673 }
1674 break;
1675
1676 case Type::TYPE_INTERFACE:
1677 if (this->interface_type()->is_empty())
1678 {
1679 hash_fnname = "__go_type_hash_empty_interface";
1680 equal_fnname = "__go_type_equal_empty_interface";
1681 }
1682 else
1683 {
1684 hash_fnname = "__go_type_hash_interface";
1685 equal_fnname = "__go_type_equal_interface";
1686 }
1687 break;
1688
1689 case Type::TYPE_NAMED:
1690 case Type::TYPE_FORWARD:
1691 go_unreachable();
1692
1693 default:
1694 go_unreachable();
1695 }
1696 }
1697
1698
1699 Location bloc = Linemap::predeclared_location();
1700 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1701 hash_fntype, bloc);
1702 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1703 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1704 equal_fntype, bloc);
1705 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1706 }
1707
1708 // A hash table mapping types to the specific hash functions.
1709
1710 Type::Type_functions Type::type_functions_table;
1711
1712 // Handle a type function which is specific to a type: a struct or
1713 // array which can not use an identity comparison.
1714
1715 void
1716 Type::specific_type_functions(Gogo* gogo, Named_type* name,
1717 Function_type* hash_fntype,
1718 Function_type* equal_fntype,
1719 Named_object** hash_fn,
1720 Named_object** equal_fn)
1721 {
1722 Hash_equal_fn fnull(NULL, NULL);
1723 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1724 std::pair<Type_functions::iterator, bool> ins =
1725 Type::type_functions_table.insert(val);
1726 if (!ins.second)
1727 {
1728 // We already have functions for this type
1729 *hash_fn = ins.first->second.first;
1730 *equal_fn = ins.first->second.second;
1731 return;
1732 }
1733
1734 std::string base_name;
1735 if (name == NULL)
1736 {
1737 // Mangled names can have '.' if they happen to refer to named
1738 // types in some way. That's fine if this is simply a named
1739 // type, but otherwise it will confuse the code that builds
1740 // function identifiers. Remove '.' when necessary.
1741 base_name = this->mangled_name(gogo);
1742 size_t i;
1743 while ((i = base_name.find('.')) != std::string::npos)
1744 base_name[i] = '$';
1745 base_name = gogo->pack_hidden_name(base_name, false);
1746 }
1747 else
1748 {
1749 // This name is already hidden or not as appropriate.
1750 base_name = name->name();
1751 unsigned int index;
1752 const Named_object* in_function = name->in_function(&index);
1753 if (in_function != NULL)
1754 {
1755 base_name += '$' + Gogo::unpack_hidden_name(in_function->name());
1756 if (index > 0)
1757 {
1758 char buf[30];
1759 snprintf(buf, sizeof buf, "%u", index);
1760 base_name += '$';
1761 base_name += buf;
1762 }
1763 }
1764 }
1765 std::string hash_name = base_name + "$hash";
1766 std::string equal_name = base_name + "$equal";
1767
1768 Location bloc = Linemap::predeclared_location();
1769
1770 const Package* package = NULL;
1771 bool is_defined_elsewhere =
1772 this->type_descriptor_defined_elsewhere(name, &package);
1773 if (is_defined_elsewhere)
1774 {
1775 *hash_fn = Named_object::make_function_declaration(hash_name, package,
1776 hash_fntype, bloc);
1777 *equal_fn = Named_object::make_function_declaration(equal_name, package,
1778 equal_fntype, bloc);
1779 }
1780 else
1781 {
1782 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1783 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1784 bloc);
1785 }
1786
1787 ins.first->second.first = *hash_fn;
1788 ins.first->second.second = *equal_fn;
1789
1790 if (!is_defined_elsewhere)
1791 {
1792 if (gogo->in_global_scope())
1793 this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1794 equal_name, equal_fntype);
1795 else
1796 gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1797 equal_name, equal_fntype);
1798 }
1799 }
1800
1801 // Write the hash and equality functions for a type which needs to be
1802 // written specially.
1803
1804 void
1805 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1806 const std::string& hash_name,
1807 Function_type* hash_fntype,
1808 const std::string& equal_name,
1809 Function_type* equal_fntype)
1810 {
1811 Location bloc = Linemap::predeclared_location();
1812
1813 if (gogo->specific_type_functions_are_written())
1814 {
1815 go_assert(saw_errors());
1816 return;
1817 }
1818
1819 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1820 bloc);
1821 gogo->start_block(bloc);
1822
1823 if (this->struct_type() != NULL)
1824 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1825 equal_fntype);
1826 else if (this->array_type() != NULL)
1827 this->array_type()->write_hash_function(gogo, name, hash_fntype,
1828 equal_fntype);
1829 else
1830 go_unreachable();
1831
1832 Block* b = gogo->finish_block(bloc);
1833 gogo->add_block(b, bloc);
1834 gogo->lower_block(hash_fn, b);
1835 gogo->finish_function(bloc);
1836
1837 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1838 false, bloc);
1839 gogo->start_block(bloc);
1840
1841 if (this->struct_type() != NULL)
1842 this->struct_type()->write_equal_function(gogo, name);
1843 else if (this->array_type() != NULL)
1844 this->array_type()->write_equal_function(gogo, name);
1845 else
1846 go_unreachable();
1847
1848 b = gogo->finish_block(bloc);
1849 gogo->add_block(b, bloc);
1850 gogo->lower_block(equal_fn, b);
1851 gogo->finish_function(bloc);
1852 }
1853
1854 // Return a composite literal for the type descriptor for a plain type
1855 // of kind RUNTIME_TYPE_KIND named NAME.
1856
1857 Expression*
1858 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1859 Named_type* name, const Methods* methods,
1860 bool only_value_methods)
1861 {
1862 Location bloc = Linemap::predeclared_location();
1863
1864 Type* td_type = Type::make_type_descriptor_type();
1865 const Struct_field_list* fields = td_type->struct_type()->fields();
1866
1867 Expression_list* vals = new Expression_list();
1868 vals->reserve(9);
1869
1870 if (!this->has_pointer())
1871 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1872 Struct_field_list::const_iterator p = fields->begin();
1873 go_assert(p->is_field_name("Kind"));
1874 mpz_t iv;
1875 mpz_init_set_ui(iv, runtime_type_kind);
1876 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1877
1878 ++p;
1879 go_assert(p->is_field_name("align"));
1880 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1881 vals->push_back(Expression::make_type_info(this, type_info));
1882
1883 ++p;
1884 go_assert(p->is_field_name("fieldAlign"));
1885 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1886 vals->push_back(Expression::make_type_info(this, type_info));
1887
1888 ++p;
1889 go_assert(p->is_field_name("size"));
1890 type_info = Expression::TYPE_INFO_SIZE;
1891 vals->push_back(Expression::make_type_info(this, type_info));
1892
1893 ++p;
1894 go_assert(p->is_field_name("hash"));
1895 unsigned int h;
1896 if (name != NULL)
1897 h = name->hash_for_method(gogo);
1898 else
1899 h = this->hash_for_method(gogo);
1900 mpz_set_ui(iv, h);
1901 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1902
1903 ++p;
1904 go_assert(p->is_field_name("hashfn"));
1905 Function_type* hash_fntype = p->type()->function_type();
1906
1907 ++p;
1908 go_assert(p->is_field_name("equalfn"));
1909 Function_type* equal_fntype = p->type()->function_type();
1910
1911 Named_object* hash_fn;
1912 Named_object* equal_fn;
1913 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1914 &equal_fn);
1915 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1916 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1917
1918 ++p;
1919 go_assert(p->is_field_name("string"));
1920 Expression* s = Expression::make_string((name != NULL
1921 ? name->reflection(gogo)
1922 : this->reflection(gogo)),
1923 bloc);
1924 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1925
1926 ++p;
1927 go_assert(p->is_field_name("uncommonType"));
1928 if (name == NULL && methods == NULL)
1929 vals->push_back(Expression::make_nil(bloc));
1930 else
1931 {
1932 if (methods == NULL)
1933 methods = name->methods();
1934 vals->push_back(this->uncommon_type_constructor(gogo,
1935 p->type()->deref(),
1936 name, methods,
1937 only_value_methods));
1938 }
1939
1940 ++p;
1941 go_assert(p->is_field_name("ptrToThis"));
1942 if (name == NULL)
1943 vals->push_back(Expression::make_nil(bloc));
1944 else
1945 {
1946 Type* pt = Type::make_pointer_type(name);
1947 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1948 }
1949
1950 ++p;
1951 go_assert(p == fields->end());
1952
1953 mpz_clear(iv);
1954
1955 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1956 }
1957
1958 // Return a composite literal for the uncommon type information for
1959 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1960 // struct. If name is not NULL, it is the name of the type. If
1961 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1962 // is true if only value methods should be included. At least one of
1963 // NAME and METHODS must not be NULL.
1964
1965 Expression*
1966 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1967 Named_type* name, const Methods* methods,
1968 bool only_value_methods) const
1969 {
1970 Location bloc = Linemap::predeclared_location();
1971
1972 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1973
1974 Expression_list* vals = new Expression_list();
1975 vals->reserve(3);
1976
1977 Struct_field_list::const_iterator p = fields->begin();
1978 go_assert(p->is_field_name("name"));
1979
1980 ++p;
1981 go_assert(p->is_field_name("pkgPath"));
1982
1983 if (name == NULL)
1984 {
1985 vals->push_back(Expression::make_nil(bloc));
1986 vals->push_back(Expression::make_nil(bloc));
1987 }
1988 else
1989 {
1990 Named_object* no = name->named_object();
1991 std::string n = Gogo::unpack_hidden_name(no->name());
1992 Expression* s = Expression::make_string(n, bloc);
1993 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1994
1995 if (name->is_builtin())
1996 vals->push_back(Expression::make_nil(bloc));
1997 else
1998 {
1999 const Package* package = no->package();
2000 const std::string& pkgpath(package == NULL
2001 ? gogo->pkgpath()
2002 : package->pkgpath());
2003 n.assign(pkgpath);
2004 unsigned int index;
2005 const Named_object* in_function = name->in_function(&index);
2006 if (in_function != NULL)
2007 {
2008 n.append(1, '.');
2009 n.append(Gogo::unpack_hidden_name(in_function->name()));
2010 if (index > 0)
2011 {
2012 char buf[30];
2013 snprintf(buf, sizeof buf, "%u", index);
2014 n.append(1, '.');
2015 n.append(buf);
2016 }
2017 }
2018 s = Expression::make_string(n, bloc);
2019 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2020 }
2021 }
2022
2023 ++p;
2024 go_assert(p->is_field_name("methods"));
2025 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
2026 only_value_methods));
2027
2028 ++p;
2029 go_assert(p == fields->end());
2030
2031 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
2032 vals, bloc);
2033 return Expression::make_unary(OPERATOR_AND, r, bloc);
2034 }
2035
2036 // Sort methods by name.
2037
2038 class Sort_methods
2039 {
2040 public:
2041 bool
2042 operator()(const std::pair<std::string, const Method*>& m1,
2043 const std::pair<std::string, const Method*>& m2) const
2044 { return m1.first < m2.first; }
2045 };
2046
2047 // Return a composite literal for the type method table for this type.
2048 // METHODS_TYPE is the type of the table, and is a slice type.
2049 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
2050 // then only value methods are used.
2051
2052 Expression*
2053 Type::methods_constructor(Gogo* gogo, Type* methods_type,
2054 const Methods* methods,
2055 bool only_value_methods) const
2056 {
2057 Location bloc = Linemap::predeclared_location();
2058
2059 std::vector<std::pair<std::string, const Method*> > smethods;
2060 if (methods != NULL)
2061 {
2062 smethods.reserve(methods->count());
2063 for (Methods::const_iterator p = methods->begin();
2064 p != methods->end();
2065 ++p)
2066 {
2067 if (p->second->is_ambiguous())
2068 continue;
2069 if (only_value_methods && !p->second->is_value_method())
2070 continue;
2071 smethods.push_back(std::make_pair(p->first, p->second));
2072 }
2073 }
2074
2075 if (smethods.empty())
2076 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
2077
2078 std::sort(smethods.begin(), smethods.end(), Sort_methods());
2079
2080 Type* method_type = methods_type->array_type()->element_type();
2081
2082 Expression_list* vals = new Expression_list();
2083 vals->reserve(smethods.size());
2084 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
2085 = smethods.begin();
2086 p != smethods.end();
2087 ++p)
2088 vals->push_back(this->method_constructor(gogo, method_type, p->first,
2089 p->second, only_value_methods));
2090
2091 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
2092 }
2093
2094 // Return a composite literal for a single method. METHOD_TYPE is the
2095 // type of the entry. METHOD_NAME is the name of the method and M is
2096 // the method information.
2097
2098 Expression*
2099 Type::method_constructor(Gogo*, Type* method_type,
2100 const std::string& method_name,
2101 const Method* m,
2102 bool only_value_methods) const
2103 {
2104 Location bloc = Linemap::predeclared_location();
2105
2106 const Struct_field_list* fields = method_type->struct_type()->fields();
2107
2108 Expression_list* vals = new Expression_list();
2109 vals->reserve(5);
2110
2111 Struct_field_list::const_iterator p = fields->begin();
2112 go_assert(p->is_field_name("name"));
2113 const std::string n = Gogo::unpack_hidden_name(method_name);
2114 Expression* s = Expression::make_string(n, bloc);
2115 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2116
2117 ++p;
2118 go_assert(p->is_field_name("pkgPath"));
2119 if (!Gogo::is_hidden_name(method_name))
2120 vals->push_back(Expression::make_nil(bloc));
2121 else
2122 {
2123 s = Expression::make_string(Gogo::hidden_name_pkgpath(method_name),
2124 bloc);
2125 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2126 }
2127
2128 Named_object* no = (m->needs_stub_method()
2129 ? m->stub_object()
2130 : m->named_object());
2131
2132 Function_type* mtype;
2133 if (no->is_function())
2134 mtype = no->func_value()->type();
2135 else
2136 mtype = no->func_declaration_value()->type();
2137 go_assert(mtype->is_method());
2138 Type* nonmethod_type = mtype->copy_without_receiver();
2139
2140 ++p;
2141 go_assert(p->is_field_name("mtyp"));
2142 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
2143
2144 ++p;
2145 go_assert(p->is_field_name("typ"));
2146 if (!only_value_methods && m->is_value_method())
2147 {
2148 // This is a value method on a pointer type. Change the type of
2149 // the method to use a pointer receiver. The implementation
2150 // always uses a pointer receiver anyhow.
2151 Type* rtype = mtype->receiver()->type();
2152 Type* prtype = Type::make_pointer_type(rtype);
2153 Typed_identifier* receiver =
2154 new Typed_identifier(mtype->receiver()->name(), prtype,
2155 mtype->receiver()->location());
2156 mtype = Type::make_function_type(receiver,
2157 (mtype->parameters() == NULL
2158 ? NULL
2159 : mtype->parameters()->copy()),
2160 (mtype->results() == NULL
2161 ? NULL
2162 : mtype->results()->copy()),
2163 mtype->location());
2164 }
2165 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
2166
2167 ++p;
2168 go_assert(p->is_field_name("tfn"));
2169 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
2170
2171 ++p;
2172 go_assert(p == fields->end());
2173
2174 return Expression::make_struct_composite_literal(method_type, vals, bloc);
2175 }
2176
2177 // Return a composite literal for the type descriptor of a plain type.
2178 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
2179 // NULL, it is the name to use as well as the list of methods.
2180
2181 Expression*
2182 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
2183 Named_type* name)
2184 {
2185 return this->type_descriptor_constructor(gogo, runtime_type_kind,
2186 name, NULL, true);
2187 }
2188
2189 // Return the type reflection string for this type.
2190
2191 std::string
2192 Type::reflection(Gogo* gogo) const
2193 {
2194 std::string ret;
2195
2196 // The do_reflection virtual function should set RET to the
2197 // reflection string.
2198 this->do_reflection(gogo, &ret);
2199
2200 return ret;
2201 }
2202
2203 // Return a mangled name for the type.
2204
2205 std::string
2206 Type::mangled_name(Gogo* gogo) const
2207 {
2208 std::string ret;
2209
2210 // The do_mangled_name virtual function should set RET to the
2211 // mangled name. For a composite type it should append a code for
2212 // the composition and then call do_mangled_name on the components.
2213 this->do_mangled_name(gogo, &ret);
2214
2215 return ret;
2216 }
2217
2218 // Return whether the backend size of the type is known.
2219
2220 bool
2221 Type::is_backend_type_size_known(Gogo* gogo)
2222 {
2223 switch (this->classification_)
2224 {
2225 case TYPE_ERROR:
2226 case TYPE_VOID:
2227 case TYPE_BOOLEAN:
2228 case TYPE_INTEGER:
2229 case TYPE_FLOAT:
2230 case TYPE_COMPLEX:
2231 case TYPE_STRING:
2232 case TYPE_FUNCTION:
2233 case TYPE_POINTER:
2234 case TYPE_NIL:
2235 case TYPE_MAP:
2236 case TYPE_CHANNEL:
2237 case TYPE_INTERFACE:
2238 return true;
2239
2240 case TYPE_STRUCT:
2241 {
2242 const Struct_field_list* fields = this->struct_type()->fields();
2243 for (Struct_field_list::const_iterator pf = fields->begin();
2244 pf != fields->end();
2245 ++pf)
2246 if (!pf->type()->is_backend_type_size_known(gogo))
2247 return false;
2248 return true;
2249 }
2250
2251 case TYPE_ARRAY:
2252 {
2253 const Array_type* at = this->array_type();
2254 if (at->length() == NULL)
2255 return true;
2256 else
2257 {
2258 Numeric_constant nc;
2259 if (!at->length()->numeric_constant_value(&nc))
2260 return false;
2261 mpz_t ival;
2262 if (!nc.to_int(&ival))
2263 return false;
2264 mpz_clear(ival);
2265 return at->element_type()->is_backend_type_size_known(gogo);
2266 }
2267 }
2268
2269 case TYPE_NAMED:
2270 // Begin converting this type to the backend representation.
2271 // This will create a placeholder if necessary.
2272 this->get_backend(gogo);
2273 return this->named_type()->is_named_backend_type_size_known();
2274
2275 case TYPE_FORWARD:
2276 {
2277 Forward_declaration_type* fdt = this->forward_declaration_type();
2278 return fdt->real_type()->is_backend_type_size_known(gogo);
2279 }
2280
2281 case TYPE_SINK:
2282 case TYPE_CALL_MULTIPLE_RESULT:
2283 go_unreachable();
2284
2285 default:
2286 go_unreachable();
2287 }
2288 }
2289
2290 // If the size of the type can be determined, set *PSIZE to the size
2291 // in bytes and return true. Otherwise, return false. This queries
2292 // the backend.
2293
2294 bool
2295 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2296 {
2297 if (!this->is_backend_type_size_known(gogo))
2298 return false;
2299 Btype* bt = this->get_backend_placeholder(gogo);
2300 size_t size = gogo->backend()->type_size(bt);
2301 *psize = static_cast<unsigned int>(size);
2302 if (*psize != size)
2303 return false;
2304 return true;
2305 }
2306
2307 // If the alignment of the type can be determined, set *PALIGN to
2308 // the alignment in bytes and return true. Otherwise, return false.
2309
2310 bool
2311 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2312 {
2313 if (!this->is_backend_type_size_known(gogo))
2314 return false;
2315 Btype* bt = this->get_backend_placeholder(gogo);
2316 size_t align = gogo->backend()->type_alignment(bt);
2317 *palign = static_cast<unsigned int>(align);
2318 if (*palign != align)
2319 return false;
2320 return true;
2321 }
2322
2323 // Like backend_type_align, but return the alignment when used as a
2324 // field.
2325
2326 bool
2327 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2328 {
2329 if (!this->is_backend_type_size_known(gogo))
2330 return false;
2331 Btype* bt = this->get_backend_placeholder(gogo);
2332 size_t a = gogo->backend()->type_field_alignment(bt);
2333 *palign = static_cast<unsigned int>(a);
2334 if (*palign != a)
2335 return false;
2336 return true;
2337 }
2338
2339 // Default function to export a type.
2340
2341 void
2342 Type::do_export(Export*) const
2343 {
2344 go_unreachable();
2345 }
2346
2347 // Import a type.
2348
2349 Type*
2350 Type::import_type(Import* imp)
2351 {
2352 if (imp->match_c_string("("))
2353 return Function_type::do_import(imp);
2354 else if (imp->match_c_string("*"))
2355 return Pointer_type::do_import(imp);
2356 else if (imp->match_c_string("struct "))
2357 return Struct_type::do_import(imp);
2358 else if (imp->match_c_string("["))
2359 return Array_type::do_import(imp);
2360 else if (imp->match_c_string("map "))
2361 return Map_type::do_import(imp);
2362 else if (imp->match_c_string("chan "))
2363 return Channel_type::do_import(imp);
2364 else if (imp->match_c_string("interface"))
2365 return Interface_type::do_import(imp);
2366 else
2367 {
2368 error_at(imp->location(), "import error: expected type");
2369 return Type::make_error_type();
2370 }
2371 }
2372
2373 // A type used to indicate a parsing error. This exists to simplify
2374 // later error detection.
2375
2376 class Error_type : public Type
2377 {
2378 public:
2379 Error_type()
2380 : Type(TYPE_ERROR)
2381 { }
2382
2383 protected:
2384 bool
2385 do_compare_is_identity(Gogo*) const
2386 { return false; }
2387
2388 Btype*
2389 do_get_backend(Gogo* gogo)
2390 { return gogo->backend()->error_type(); }
2391
2392 Expression*
2393 do_type_descriptor(Gogo*, Named_type*)
2394 { return Expression::make_error(Linemap::predeclared_location()); }
2395
2396 void
2397 do_reflection(Gogo*, std::string*) const
2398 { go_assert(saw_errors()); }
2399
2400 void
2401 do_mangled_name(Gogo*, std::string* ret) const
2402 { ret->push_back('E'); }
2403 };
2404
2405 Type*
2406 Type::make_error_type()
2407 {
2408 static Error_type singleton_error_type;
2409 return &singleton_error_type;
2410 }
2411
2412 // The void type.
2413
2414 class Void_type : public Type
2415 {
2416 public:
2417 Void_type()
2418 : Type(TYPE_VOID)
2419 { }
2420
2421 protected:
2422 bool
2423 do_compare_is_identity(Gogo*) const
2424 { return false; }
2425
2426 Btype*
2427 do_get_backend(Gogo* gogo)
2428 { return gogo->backend()->void_type(); }
2429
2430 Expression*
2431 do_type_descriptor(Gogo*, Named_type*)
2432 { go_unreachable(); }
2433
2434 void
2435 do_reflection(Gogo*, std::string*) const
2436 { }
2437
2438 void
2439 do_mangled_name(Gogo*, std::string* ret) const
2440 { ret->push_back('v'); }
2441 };
2442
2443 Type*
2444 Type::make_void_type()
2445 {
2446 static Void_type singleton_void_type;
2447 return &singleton_void_type;
2448 }
2449
2450 // The boolean type.
2451
2452 class Boolean_type : public Type
2453 {
2454 public:
2455 Boolean_type()
2456 : Type(TYPE_BOOLEAN)
2457 { }
2458
2459 protected:
2460 bool
2461 do_compare_is_identity(Gogo*) const
2462 { return true; }
2463
2464 Btype*
2465 do_get_backend(Gogo* gogo)
2466 { return gogo->backend()->bool_type(); }
2467
2468 Expression*
2469 do_type_descriptor(Gogo*, Named_type* name);
2470
2471 // We should not be asked for the reflection string of a basic type.
2472 void
2473 do_reflection(Gogo*, std::string* ret) const
2474 { ret->append("bool"); }
2475
2476 void
2477 do_mangled_name(Gogo*, std::string* ret) const
2478 { ret->push_back('b'); }
2479 };
2480
2481 // Make the type descriptor.
2482
2483 Expression*
2484 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2485 {
2486 if (name != NULL)
2487 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2488 else
2489 {
2490 Named_object* no = gogo->lookup_global("bool");
2491 go_assert(no != NULL);
2492 return Type::type_descriptor(gogo, no->type_value());
2493 }
2494 }
2495
2496 Type*
2497 Type::make_boolean_type()
2498 {
2499 static Boolean_type boolean_type;
2500 return &boolean_type;
2501 }
2502
2503 // The named type "bool".
2504
2505 static Named_type* named_bool_type;
2506
2507 // Get the named type "bool".
2508
2509 Named_type*
2510 Type::lookup_bool_type()
2511 {
2512 return named_bool_type;
2513 }
2514
2515 // Make the named type "bool".
2516
2517 Named_type*
2518 Type::make_named_bool_type()
2519 {
2520 Type* bool_type = Type::make_boolean_type();
2521 Named_object* named_object =
2522 Named_object::make_type("bool", NULL, bool_type,
2523 Linemap::predeclared_location());
2524 Named_type* named_type = named_object->type_value();
2525 named_bool_type = named_type;
2526 return named_type;
2527 }
2528
2529 // Class Integer_type.
2530
2531 Integer_type::Named_integer_types Integer_type::named_integer_types;
2532
2533 // Create a new integer type. Non-abstract integer types always have
2534 // names.
2535
2536 Named_type*
2537 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2538 int bits, int runtime_type_kind)
2539 {
2540 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2541 runtime_type_kind);
2542 std::string sname(name);
2543 Named_object* named_object =
2544 Named_object::make_type(sname, NULL, integer_type,
2545 Linemap::predeclared_location());
2546 Named_type* named_type = named_object->type_value();
2547 std::pair<Named_integer_types::iterator, bool> ins =
2548 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2549 go_assert(ins.second);
2550 return named_type;
2551 }
2552
2553 // Look up an existing integer type.
2554
2555 Named_type*
2556 Integer_type::lookup_integer_type(const char* name)
2557 {
2558 Named_integer_types::const_iterator p =
2559 Integer_type::named_integer_types.find(name);
2560 go_assert(p != Integer_type::named_integer_types.end());
2561 return p->second;
2562 }
2563
2564 // Create a new abstract integer type.
2565
2566 Integer_type*
2567 Integer_type::create_abstract_integer_type()
2568 {
2569 static Integer_type* abstract_type;
2570 if (abstract_type == NULL)
2571 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2572 RUNTIME_TYPE_KIND_INT);
2573 return abstract_type;
2574 }
2575
2576 // Create a new abstract character type.
2577
2578 Integer_type*
2579 Integer_type::create_abstract_character_type()
2580 {
2581 static Integer_type* abstract_type;
2582 if (abstract_type == NULL)
2583 {
2584 abstract_type = new Integer_type(true, false, 32,
2585 RUNTIME_TYPE_KIND_INT32);
2586 abstract_type->set_is_rune();
2587 }
2588 return abstract_type;
2589 }
2590
2591 // Integer type compatibility.
2592
2593 bool
2594 Integer_type::is_identical(const Integer_type* t) const
2595 {
2596 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2597 return false;
2598 return this->is_abstract_ == t->is_abstract_;
2599 }
2600
2601 // Hash code.
2602
2603 unsigned int
2604 Integer_type::do_hash_for_method(Gogo*) const
2605 {
2606 return ((this->bits_ << 4)
2607 + ((this->is_unsigned_ ? 1 : 0) << 8)
2608 + ((this->is_abstract_ ? 1 : 0) << 9));
2609 }
2610
2611 // Convert an Integer_type to the backend representation.
2612
2613 Btype*
2614 Integer_type::do_get_backend(Gogo* gogo)
2615 {
2616 if (this->is_abstract_)
2617 {
2618 go_assert(saw_errors());
2619 return gogo->backend()->error_type();
2620 }
2621 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2622 }
2623
2624 // The type descriptor for an integer type. Integer types are always
2625 // named.
2626
2627 Expression*
2628 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2629 {
2630 go_assert(name != NULL || saw_errors());
2631 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2632 }
2633
2634 // We should not be asked for the reflection string of a basic type.
2635
2636 void
2637 Integer_type::do_reflection(Gogo*, std::string*) const
2638 {
2639 go_assert(saw_errors());
2640 }
2641
2642 // Mangled name.
2643
2644 void
2645 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2646 {
2647 char buf[100];
2648 snprintf(buf, sizeof buf, "i%s%s%de",
2649 this->is_abstract_ ? "a" : "",
2650 this->is_unsigned_ ? "u" : "",
2651 this->bits_);
2652 ret->append(buf);
2653 }
2654
2655 // Make an integer type.
2656
2657 Named_type*
2658 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2659 int runtime_type_kind)
2660 {
2661 return Integer_type::create_integer_type(name, is_unsigned, bits,
2662 runtime_type_kind);
2663 }
2664
2665 // Make an abstract integer type.
2666
2667 Integer_type*
2668 Type::make_abstract_integer_type()
2669 {
2670 return Integer_type::create_abstract_integer_type();
2671 }
2672
2673 // Make an abstract character type.
2674
2675 Integer_type*
2676 Type::make_abstract_character_type()
2677 {
2678 return Integer_type::create_abstract_character_type();
2679 }
2680
2681 // Look up an integer type.
2682
2683 Named_type*
2684 Type::lookup_integer_type(const char* name)
2685 {
2686 return Integer_type::lookup_integer_type(name);
2687 }
2688
2689 // Class Float_type.
2690
2691 Float_type::Named_float_types Float_type::named_float_types;
2692
2693 // Create a new float type. Non-abstract float types always have
2694 // names.
2695
2696 Named_type*
2697 Float_type::create_float_type(const char* name, int bits,
2698 int runtime_type_kind)
2699 {
2700 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2701 std::string sname(name);
2702 Named_object* named_object =
2703 Named_object::make_type(sname, NULL, float_type,
2704 Linemap::predeclared_location());
2705 Named_type* named_type = named_object->type_value();
2706 std::pair<Named_float_types::iterator, bool> ins =
2707 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2708 go_assert(ins.second);
2709 return named_type;
2710 }
2711
2712 // Look up an existing float type.
2713
2714 Named_type*
2715 Float_type::lookup_float_type(const char* name)
2716 {
2717 Named_float_types::const_iterator p =
2718 Float_type::named_float_types.find(name);
2719 go_assert(p != Float_type::named_float_types.end());
2720 return p->second;
2721 }
2722
2723 // Create a new abstract float type.
2724
2725 Float_type*
2726 Float_type::create_abstract_float_type()
2727 {
2728 static Float_type* abstract_type;
2729 if (abstract_type == NULL)
2730 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2731 return abstract_type;
2732 }
2733
2734 // Whether this type is identical with T.
2735
2736 bool
2737 Float_type::is_identical(const Float_type* t) const
2738 {
2739 if (this->bits_ != t->bits_)
2740 return false;
2741 return this->is_abstract_ == t->is_abstract_;
2742 }
2743
2744 // Hash code.
2745
2746 unsigned int
2747 Float_type::do_hash_for_method(Gogo*) const
2748 {
2749 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2750 }
2751
2752 // Convert to the backend representation.
2753
2754 Btype*
2755 Float_type::do_get_backend(Gogo* gogo)
2756 {
2757 return gogo->backend()->float_type(this->bits_);
2758 }
2759
2760 // The type descriptor for a float type. Float types are always named.
2761
2762 Expression*
2763 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2764 {
2765 go_assert(name != NULL || saw_errors());
2766 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2767 }
2768
2769 // We should not be asked for the reflection string of a basic type.
2770
2771 void
2772 Float_type::do_reflection(Gogo*, std::string*) const
2773 {
2774 go_assert(saw_errors());
2775 }
2776
2777 // Mangled name.
2778
2779 void
2780 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2781 {
2782 char buf[100];
2783 snprintf(buf, sizeof buf, "f%s%de",
2784 this->is_abstract_ ? "a" : "",
2785 this->bits_);
2786 ret->append(buf);
2787 }
2788
2789 // Make a floating point type.
2790
2791 Named_type*
2792 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2793 {
2794 return Float_type::create_float_type(name, bits, runtime_type_kind);
2795 }
2796
2797 // Make an abstract float type.
2798
2799 Float_type*
2800 Type::make_abstract_float_type()
2801 {
2802 return Float_type::create_abstract_float_type();
2803 }
2804
2805 // Look up a float type.
2806
2807 Named_type*
2808 Type::lookup_float_type(const char* name)
2809 {
2810 return Float_type::lookup_float_type(name);
2811 }
2812
2813 // Class Complex_type.
2814
2815 Complex_type::Named_complex_types Complex_type::named_complex_types;
2816
2817 // Create a new complex type. Non-abstract complex types always have
2818 // names.
2819
2820 Named_type*
2821 Complex_type::create_complex_type(const char* name, int bits,
2822 int runtime_type_kind)
2823 {
2824 Complex_type* complex_type = new Complex_type(false, bits,
2825 runtime_type_kind);
2826 std::string sname(name);
2827 Named_object* named_object =
2828 Named_object::make_type(sname, NULL, complex_type,
2829 Linemap::predeclared_location());
2830 Named_type* named_type = named_object->type_value();
2831 std::pair<Named_complex_types::iterator, bool> ins =
2832 Complex_type::named_complex_types.insert(std::make_pair(sname,
2833 named_type));
2834 go_assert(ins.second);
2835 return named_type;
2836 }
2837
2838 // Look up an existing complex type.
2839
2840 Named_type*
2841 Complex_type::lookup_complex_type(const char* name)
2842 {
2843 Named_complex_types::const_iterator p =
2844 Complex_type::named_complex_types.find(name);
2845 go_assert(p != Complex_type::named_complex_types.end());
2846 return p->second;
2847 }
2848
2849 // Create a new abstract complex type.
2850
2851 Complex_type*
2852 Complex_type::create_abstract_complex_type()
2853 {
2854 static Complex_type* abstract_type;
2855 if (abstract_type == NULL)
2856 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2857 return abstract_type;
2858 }
2859
2860 // Whether this type is identical with T.
2861
2862 bool
2863 Complex_type::is_identical(const Complex_type *t) const
2864 {
2865 if (this->bits_ != t->bits_)
2866 return false;
2867 return this->is_abstract_ == t->is_abstract_;
2868 }
2869
2870 // Hash code.
2871
2872 unsigned int
2873 Complex_type::do_hash_for_method(Gogo*) const
2874 {
2875 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2876 }
2877
2878 // Convert to the backend representation.
2879
2880 Btype*
2881 Complex_type::do_get_backend(Gogo* gogo)
2882 {
2883 return gogo->backend()->complex_type(this->bits_);
2884 }
2885
2886 // The type descriptor for a complex type. Complex types are always
2887 // named.
2888
2889 Expression*
2890 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2891 {
2892 go_assert(name != NULL || saw_errors());
2893 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2894 }
2895
2896 // We should not be asked for the reflection string of a basic type.
2897
2898 void
2899 Complex_type::do_reflection(Gogo*, std::string*) const
2900 {
2901 go_assert(saw_errors());
2902 }
2903
2904 // Mangled name.
2905
2906 void
2907 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2908 {
2909 char buf[100];
2910 snprintf(buf, sizeof buf, "c%s%de",
2911 this->is_abstract_ ? "a" : "",
2912 this->bits_);
2913 ret->append(buf);
2914 }
2915
2916 // Make a complex type.
2917
2918 Named_type*
2919 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2920 {
2921 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2922 }
2923
2924 // Make an abstract complex type.
2925
2926 Complex_type*
2927 Type::make_abstract_complex_type()
2928 {
2929 return Complex_type::create_abstract_complex_type();
2930 }
2931
2932 // Look up a complex type.
2933
2934 Named_type*
2935 Type::lookup_complex_type(const char* name)
2936 {
2937 return Complex_type::lookup_complex_type(name);
2938 }
2939
2940 // Class String_type.
2941
2942 // Convert String_type to the backend representation. A string is a
2943 // struct with two fields: a pointer to the characters and a length.
2944
2945 Btype*
2946 String_type::do_get_backend(Gogo* gogo)
2947 {
2948 static Btype* backend_string_type;
2949 if (backend_string_type == NULL)
2950 {
2951 std::vector<Backend::Btyped_identifier> fields(2);
2952
2953 Type* b = gogo->lookup_global("byte")->type_value();
2954 Type* pb = Type::make_pointer_type(b);
2955
2956 // We aren't going to get back to this field to finish the
2957 // backend representation, so force it to be finished now.
2958 if (!gogo->named_types_are_converted())
2959 {
2960 pb->get_backend_placeholder(gogo);
2961 pb->finish_backend(gogo);
2962 }
2963
2964 fields[0].name = "__data";
2965 fields[0].btype = pb->get_backend(gogo);
2966 fields[0].location = Linemap::predeclared_location();
2967
2968 Type* int_type = Type::lookup_integer_type("int");
2969 fields[1].name = "__length";
2970 fields[1].btype = int_type->get_backend(gogo);
2971 fields[1].location = fields[0].location;
2972
2973 backend_string_type = gogo->backend()->struct_type(fields);
2974 }
2975 return backend_string_type;
2976 }
2977
2978 // Return a tree for the length of STRING.
2979
2980 tree
2981 String_type::length_tree(Gogo*, tree string)
2982 {
2983 tree string_type = TREE_TYPE(string);
2984 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2985 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2986 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2987 "__length") == 0);
2988 return fold_build3(COMPONENT_REF, integer_type_node, string,
2989 length_field, NULL_TREE);
2990 }
2991
2992 // Return a tree for a pointer to the bytes of STRING.
2993
2994 tree
2995 String_type::bytes_tree(Gogo*, tree string)
2996 {
2997 tree string_type = TREE_TYPE(string);
2998 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2999 tree bytes_field = TYPE_FIELDS(string_type);
3000 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
3001 "__data") == 0);
3002 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
3003 bytes_field, NULL_TREE);
3004 }
3005
3006 // The type descriptor for the string type.
3007
3008 Expression*
3009 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3010 {
3011 if (name != NULL)
3012 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
3013 else
3014 {
3015 Named_object* no = gogo->lookup_global("string");
3016 go_assert(no != NULL);
3017 return Type::type_descriptor(gogo, no->type_value());
3018 }
3019 }
3020
3021 // We should not be asked for the reflection string of a basic type.
3022
3023 void
3024 String_type::do_reflection(Gogo*, std::string* ret) const
3025 {
3026 ret->append("string");
3027 }
3028
3029 // Mangled name of a string type.
3030
3031 void
3032 String_type::do_mangled_name(Gogo*, std::string* ret) const
3033 {
3034 ret->push_back('z');
3035 }
3036
3037 // Make a string type.
3038
3039 Type*
3040 Type::make_string_type()
3041 {
3042 static String_type string_type;
3043 return &string_type;
3044 }
3045
3046 // The named type "string".
3047
3048 static Named_type* named_string_type;
3049
3050 // Get the named type "string".
3051
3052 Named_type*
3053 Type::lookup_string_type()
3054 {
3055 return named_string_type;
3056 }
3057
3058 // Make the named type string.
3059
3060 Named_type*
3061 Type::make_named_string_type()
3062 {
3063 Type* string_type = Type::make_string_type();
3064 Named_object* named_object =
3065 Named_object::make_type("string", NULL, string_type,
3066 Linemap::predeclared_location());
3067 Named_type* named_type = named_object->type_value();
3068 named_string_type = named_type;
3069 return named_type;
3070 }
3071
3072 // The sink type. This is the type of the blank identifier _. Any
3073 // type may be assigned to it.
3074
3075 class Sink_type : public Type
3076 {
3077 public:
3078 Sink_type()
3079 : Type(TYPE_SINK)
3080 { }
3081
3082 protected:
3083 bool
3084 do_compare_is_identity(Gogo*) const
3085 { return false; }
3086
3087 Btype*
3088 do_get_backend(Gogo*)
3089 { go_unreachable(); }
3090
3091 Expression*
3092 do_type_descriptor(Gogo*, Named_type*)
3093 { go_unreachable(); }
3094
3095 void
3096 do_reflection(Gogo*, std::string*) const
3097 { go_unreachable(); }
3098
3099 void
3100 do_mangled_name(Gogo*, std::string*) const
3101 { go_unreachable(); }
3102 };
3103
3104 // Make the sink type.
3105
3106 Type*
3107 Type::make_sink_type()
3108 {
3109 static Sink_type sink_type;
3110 return &sink_type;
3111 }
3112
3113 // Class Function_type.
3114
3115 // Traversal.
3116
3117 int
3118 Function_type::do_traverse(Traverse* traverse)
3119 {
3120 if (this->receiver_ != NULL
3121 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
3122 return TRAVERSE_EXIT;
3123 if (this->parameters_ != NULL
3124 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
3125 return TRAVERSE_EXIT;
3126 if (this->results_ != NULL
3127 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
3128 return TRAVERSE_EXIT;
3129 return TRAVERSE_CONTINUE;
3130 }
3131
3132 // Returns whether T is a valid redeclaration of this type. If this
3133 // returns false, and REASON is not NULL, *REASON may be set to a
3134 // brief explanation of why it returned false.
3135
3136 bool
3137 Function_type::is_valid_redeclaration(const Function_type* t,
3138 std::string* reason) const
3139 {
3140 if (!this->is_identical(t, false, true, reason))
3141 return false;
3142
3143 // A redeclaration of a function is required to use the same names
3144 // for the receiver and parameters.
3145 if (this->receiver() != NULL
3146 && this->receiver()->name() != t->receiver()->name())
3147 {
3148 if (reason != NULL)
3149 *reason = "receiver name changed";
3150 return false;
3151 }
3152
3153 const Typed_identifier_list* parms1 = this->parameters();
3154 const Typed_identifier_list* parms2 = t->parameters();
3155 if (parms1 != NULL)
3156 {
3157 Typed_identifier_list::const_iterator p1 = parms1->begin();
3158 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3159 p2 != parms2->end();
3160 ++p2, ++p1)
3161 {
3162 if (p1->name() != p2->name())
3163 {
3164 if (reason != NULL)
3165 *reason = "parameter name changed";
3166 return false;
3167 }
3168
3169 // This is called at parse time, so we may have unknown
3170 // types.
3171 Type* t1 = p1->type()->forwarded();
3172 Type* t2 = p2->type()->forwarded();
3173 if (t1 != t2
3174 && t1->forward_declaration_type() != NULL
3175 && (t2->forward_declaration_type() == NULL
3176 || (t1->forward_declaration_type()->named_object()
3177 != t2->forward_declaration_type()->named_object())))
3178 return false;
3179 }
3180 }
3181
3182 const Typed_identifier_list* results1 = this->results();
3183 const Typed_identifier_list* results2 = t->results();
3184 if (results1 != NULL)
3185 {
3186 Typed_identifier_list::const_iterator res1 = results1->begin();
3187 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3188 res2 != results2->end();
3189 ++res2, ++res1)
3190 {
3191 if (res1->name() != res2->name())
3192 {
3193 if (reason != NULL)
3194 *reason = "result name changed";
3195 return false;
3196 }
3197
3198 // This is called at parse time, so we may have unknown
3199 // types.
3200 Type* t1 = res1->type()->forwarded();
3201 Type* t2 = res2->type()->forwarded();
3202 if (t1 != t2
3203 && t1->forward_declaration_type() != NULL
3204 && (t2->forward_declaration_type() == NULL
3205 || (t1->forward_declaration_type()->named_object()
3206 != t2->forward_declaration_type()->named_object())))
3207 return false;
3208 }
3209 }
3210
3211 return true;
3212 }
3213
3214 // Check whether T is the same as this type.
3215
3216 bool
3217 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
3218 bool errors_are_identical,
3219 std::string* reason) const
3220 {
3221 if (!ignore_receiver)
3222 {
3223 const Typed_identifier* r1 = this->receiver();
3224 const Typed_identifier* r2 = t->receiver();
3225 if ((r1 != NULL) != (r2 != NULL))
3226 {
3227 if (reason != NULL)
3228 *reason = _("different receiver types");
3229 return false;
3230 }
3231 if (r1 != NULL)
3232 {
3233 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
3234 reason))
3235 {
3236 if (reason != NULL && !reason->empty())
3237 *reason = "receiver: " + *reason;
3238 return false;
3239 }
3240 }
3241 }
3242
3243 const Typed_identifier_list* parms1 = this->parameters();
3244 const Typed_identifier_list* parms2 = t->parameters();
3245 if ((parms1 != NULL) != (parms2 != NULL))
3246 {
3247 if (reason != NULL)
3248 *reason = _("different number of parameters");
3249 return false;
3250 }
3251 if (parms1 != NULL)
3252 {
3253 Typed_identifier_list::const_iterator p1 = parms1->begin();
3254 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
3255 p2 != parms2->end();
3256 ++p2, ++p1)
3257 {
3258 if (p1 == parms1->end())
3259 {
3260 if (reason != NULL)
3261 *reason = _("different number of parameters");
3262 return false;
3263 }
3264
3265 if (!Type::are_identical(p1->type(), p2->type(),
3266 errors_are_identical, NULL))
3267 {
3268 if (reason != NULL)
3269 *reason = _("different parameter types");
3270 return false;
3271 }
3272 }
3273 if (p1 != parms1->end())
3274 {
3275 if (reason != NULL)
3276 *reason = _("different number of parameters");
3277 return false;
3278 }
3279 }
3280
3281 if (this->is_varargs() != t->is_varargs())
3282 {
3283 if (reason != NULL)
3284 *reason = _("different varargs");
3285 return false;
3286 }
3287
3288 const Typed_identifier_list* results1 = this->results();
3289 const Typed_identifier_list* results2 = t->results();
3290 if ((results1 != NULL) != (results2 != NULL))
3291 {
3292 if (reason != NULL)
3293 *reason = _("different number of results");
3294 return false;
3295 }
3296 if (results1 != NULL)
3297 {
3298 Typed_identifier_list::const_iterator res1 = results1->begin();
3299 for (Typed_identifier_list::const_iterator res2 = results2->begin();
3300 res2 != results2->end();
3301 ++res2, ++res1)
3302 {
3303 if (res1 == results1->end())
3304 {
3305 if (reason != NULL)
3306 *reason = _("different number of results");
3307 return false;
3308 }
3309
3310 if (!Type::are_identical(res1->type(), res2->type(),
3311 errors_are_identical, NULL))
3312 {
3313 if (reason != NULL)
3314 *reason = _("different result types");
3315 return false;
3316 }
3317 }
3318 if (res1 != results1->end())
3319 {
3320 if (reason != NULL)
3321 *reason = _("different number of results");
3322 return false;
3323 }
3324 }
3325
3326 return true;
3327 }
3328
3329 // Hash code.
3330
3331 unsigned int
3332 Function_type::do_hash_for_method(Gogo* gogo) const
3333 {
3334 unsigned int ret = 0;
3335 // We ignore the receiver type for hash codes, because we need to
3336 // get the same hash code for a method in an interface and a method
3337 // declared for a type. The former will not have a receiver.
3338 if (this->parameters_ != NULL)
3339 {
3340 int shift = 1;
3341 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3342 p != this->parameters_->end();
3343 ++p, ++shift)
3344 ret += p->type()->hash_for_method(gogo) << shift;
3345 }
3346 if (this->results_ != NULL)
3347 {
3348 int shift = 2;
3349 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3350 p != this->results_->end();
3351 ++p, ++shift)
3352 ret += p->type()->hash_for_method(gogo) << shift;
3353 }
3354 if (this->is_varargs_)
3355 ret += 1;
3356 ret <<= 4;
3357 return ret;
3358 }
3359
3360 // Get the backend representation for a function type.
3361
3362 Btype*
3363 Function_type::do_get_backend(Gogo* gogo)
3364 {
3365 Backend::Btyped_identifier breceiver;
3366 if (this->receiver_ != NULL)
3367 {
3368 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3369
3370 // We always pass the address of the receiver parameter, in
3371 // order to make interface calls work with unknown types.
3372 Type* rtype = this->receiver_->type();
3373 if (rtype->points_to() == NULL)
3374 rtype = Type::make_pointer_type(rtype);
3375 breceiver.btype = rtype->get_backend(gogo);
3376 breceiver.location = this->receiver_->location();
3377 }
3378
3379 std::vector<Backend::Btyped_identifier> bparameters;
3380 if (this->parameters_ != NULL)
3381 {
3382 bparameters.resize(this->parameters_->size());
3383 size_t i = 0;
3384 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3385 p != this->parameters_->end();
3386 ++p, ++i)
3387 {
3388 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3389 bparameters[i].btype = p->type()->get_backend(gogo);
3390 bparameters[i].location = p->location();
3391 }
3392 go_assert(i == bparameters.size());
3393 }
3394
3395 std::vector<Backend::Btyped_identifier> bresults;
3396 if (this->results_ != NULL)
3397 {
3398 bresults.resize(this->results_->size());
3399 size_t i = 0;
3400 for (Typed_identifier_list::const_iterator p = this->results_->begin();
3401 p != this->results_->end();
3402 ++p, ++i)
3403 {
3404 bresults[i].name = Gogo::unpack_hidden_name(p->name());
3405 bresults[i].btype = p->type()->get_backend(gogo);
3406 bresults[i].location = p->location();
3407 }
3408 go_assert(i == bresults.size());
3409 }
3410
3411 return gogo->backend()->function_type(breceiver, bparameters, bresults,
3412 this->location());
3413 }
3414
3415 // The type of a function type descriptor.
3416
3417 Type*
3418 Function_type::make_function_type_descriptor_type()
3419 {
3420 static Type* ret;
3421 if (ret == NULL)
3422 {
3423 Type* tdt = Type::make_type_descriptor_type();
3424 Type* ptdt = Type::make_type_descriptor_ptr_type();
3425
3426 Type* bool_type = Type::lookup_bool_type();
3427
3428 Type* slice_type = Type::make_array_type(ptdt, NULL);
3429
3430 Struct_type* s = Type::make_builtin_struct_type(4,
3431 "", tdt,
3432 "dotdotdot", bool_type,
3433 "in", slice_type,
3434 "out", slice_type);
3435
3436 ret = Type::make_builtin_named_type("FuncType", s);
3437 }
3438
3439 return ret;
3440 }
3441
3442 // The type descriptor for a function type.
3443
3444 Expression*
3445 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3446 {
3447 Location bloc = Linemap::predeclared_location();
3448
3449 Type* ftdt = Function_type::make_function_type_descriptor_type();
3450
3451 const Struct_field_list* fields = ftdt->struct_type()->fields();
3452
3453 Expression_list* vals = new Expression_list();
3454 vals->reserve(4);
3455
3456 Struct_field_list::const_iterator p = fields->begin();
3457 go_assert(p->is_field_name("commonType"));
3458 vals->push_back(this->type_descriptor_constructor(gogo,
3459 RUNTIME_TYPE_KIND_FUNC,
3460 name, NULL, true));
3461
3462 ++p;
3463 go_assert(p->is_field_name("dotdotdot"));
3464 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3465
3466 ++p;
3467 go_assert(p->is_field_name("in"));
3468 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3469 this->parameters()));
3470
3471 ++p;
3472 go_assert(p->is_field_name("out"));
3473 vals->push_back(this->type_descriptor_params(p->type(), NULL,
3474 this->results()));
3475
3476 ++p;
3477 go_assert(p == fields->end());
3478
3479 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3480 }
3481
3482 // Return a composite literal for the parameters or results of a type
3483 // descriptor.
3484
3485 Expression*
3486 Function_type::type_descriptor_params(Type* params_type,
3487 const Typed_identifier* receiver,
3488 const Typed_identifier_list* params)
3489 {
3490 Location bloc = Linemap::predeclared_location();
3491
3492 if (receiver == NULL && params == NULL)
3493 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3494
3495 Expression_list* vals = new Expression_list();
3496 vals->reserve((params == NULL ? 0 : params->size())
3497 + (receiver != NULL ? 1 : 0));
3498
3499 if (receiver != NULL)
3500 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3501
3502 if (params != NULL)
3503 {
3504 for (Typed_identifier_list::const_iterator p = params->begin();
3505 p != params->end();
3506 ++p)
3507 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3508 }
3509
3510 return Expression::make_slice_composite_literal(params_type, vals, bloc);
3511 }
3512
3513 // The reflection string.
3514
3515 void
3516 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3517 {
3518 // FIXME: Turn this off until we straighten out the type of the
3519 // struct field used in a go statement which calls a method.
3520 // go_assert(this->receiver_ == NULL);
3521
3522 ret->append("func");
3523
3524 if (this->receiver_ != NULL)
3525 {
3526 ret->push_back('(');
3527 this->append_reflection(this->receiver_->type(), gogo, ret);
3528 ret->push_back(')');
3529 }
3530
3531 ret->push_back('(');
3532 const Typed_identifier_list* params = this->parameters();
3533 if (params != NULL)
3534 {
3535 bool is_varargs = this->is_varargs_;
3536 for (Typed_identifier_list::const_iterator p = params->begin();
3537 p != params->end();
3538 ++p)
3539 {
3540 if (p != params->begin())
3541 ret->append(", ");
3542 if (!is_varargs || p + 1 != params->end())
3543 this->append_reflection(p->type(), gogo, ret);
3544 else
3545 {
3546 ret->append("...");
3547 this->append_reflection(p->type()->array_type()->element_type(),
3548 gogo, ret);
3549 }
3550 }
3551 }
3552 ret->push_back(')');
3553
3554 const Typed_identifier_list* results = this->results();
3555 if (results != NULL && !results->empty())
3556 {
3557 if (results->size() == 1)
3558 ret->push_back(' ');
3559 else
3560 ret->append(" (");
3561 for (Typed_identifier_list::const_iterator p = results->begin();
3562 p != results->end();
3563 ++p)
3564 {
3565 if (p != results->begin())
3566 ret->append(", ");
3567 this->append_reflection(p->type(), gogo, ret);
3568 }
3569 if (results->size() > 1)
3570 ret->push_back(')');
3571 }
3572 }
3573
3574 // Mangled name.
3575
3576 void
3577 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3578 {
3579 ret->push_back('F');
3580
3581 if (this->receiver_ != NULL)
3582 {
3583 ret->push_back('m');
3584 this->append_mangled_name(this->receiver_->type(), gogo, ret);
3585 }
3586
3587 const Typed_identifier_list* params = this->parameters();
3588 if (params != NULL)
3589 {
3590 ret->push_back('p');
3591 for (Typed_identifier_list::const_iterator p = params->begin();
3592 p != params->end();
3593 ++p)
3594 this->append_mangled_name(p->type(), gogo, ret);
3595 if (this->is_varargs_)
3596 ret->push_back('V');
3597 ret->push_back('e');
3598 }
3599
3600 const Typed_identifier_list* results = this->results();
3601 if (results != NULL)
3602 {
3603 ret->push_back('r');
3604 for (Typed_identifier_list::const_iterator p = results->begin();
3605 p != results->end();
3606 ++p)
3607 this->append_mangled_name(p->type(), gogo, ret);
3608 ret->push_back('e');
3609 }
3610
3611 ret->push_back('e');
3612 }
3613
3614 // Export a function type.
3615
3616 void
3617 Function_type::do_export(Export* exp) const
3618 {
3619 // We don't write out the receiver. The only function types which
3620 // should have a receiver are the ones associated with explicitly
3621 // defined methods. For those the receiver type is written out by
3622 // Function::export_func.
3623
3624 exp->write_c_string("(");
3625 bool first = true;
3626 if (this->parameters_ != NULL)
3627 {
3628 bool is_varargs = this->is_varargs_;
3629 for (Typed_identifier_list::const_iterator p =
3630 this->parameters_->begin();
3631 p != this->parameters_->end();
3632 ++p)
3633 {
3634 if (first)
3635 first = false;
3636 else
3637 exp->write_c_string(", ");
3638 exp->write_name(p->name());
3639 exp->write_c_string(" ");
3640 if (!is_varargs || p + 1 != this->parameters_->end())
3641 exp->write_type(p->type());
3642 else
3643 {
3644 exp->write_c_string("...");
3645 exp->write_type(p->type()->array_type()->element_type());
3646 }
3647 }
3648 }
3649 exp->write_c_string(")");
3650
3651 const Typed_identifier_list* results = this->results_;
3652 if (results != NULL)
3653 {
3654 exp->write_c_string(" ");
3655 if (results->size() == 1 && results->begin()->name().empty())
3656 exp->write_type(results->begin()->type());
3657 else
3658 {
3659 first = true;
3660 exp->write_c_string("(");
3661 for (Typed_identifier_list::const_iterator p = results->begin();
3662 p != results->end();
3663 ++p)
3664 {
3665 if (first)
3666 first = false;
3667 else
3668 exp->write_c_string(", ");
3669 exp->write_name(p->name());
3670 exp->write_c_string(" ");
3671 exp->write_type(p->type());
3672 }
3673 exp->write_c_string(")");
3674 }
3675 }
3676 }
3677
3678 // Import a function type.
3679
3680 Function_type*
3681 Function_type::do_import(Import* imp)
3682 {
3683 imp->require_c_string("(");
3684 Typed_identifier_list* parameters;
3685 bool is_varargs = false;
3686 if (imp->peek_char() == ')')
3687 parameters = NULL;
3688 else
3689 {
3690 parameters = new Typed_identifier_list();
3691 while (true)
3692 {
3693 std::string name = imp->read_name();
3694 imp->require_c_string(" ");
3695
3696 if (imp->match_c_string("..."))
3697 {
3698 imp->advance(3);
3699 is_varargs = true;
3700 }
3701
3702 Type* ptype = imp->read_type();
3703 if (is_varargs)
3704 ptype = Type::make_array_type(ptype, NULL);
3705 parameters->push_back(Typed_identifier(name, ptype,
3706 imp->location()));
3707 if (imp->peek_char() != ',')
3708 break;
3709 go_assert(!is_varargs);
3710 imp->require_c_string(", ");
3711 }
3712 }
3713 imp->require_c_string(")");
3714
3715 Typed_identifier_list* results;
3716 if (imp->peek_char() != ' ')
3717 results = NULL;
3718 else
3719 {
3720 imp->advance(1);
3721 results = new Typed_identifier_list;
3722 if (imp->peek_char() != '(')
3723 {
3724 Type* rtype = imp->read_type();
3725 results->push_back(Typed_identifier("", rtype, imp->location()));
3726 }
3727 else
3728 {
3729 imp->advance(1);
3730 while (true)
3731 {
3732 std::string name = imp->read_name();
3733 imp->require_c_string(" ");
3734 Type* rtype = imp->read_type();
3735 results->push_back(Typed_identifier(name, rtype,
3736 imp->location()));
3737 if (imp->peek_char() != ',')
3738 break;
3739 imp->require_c_string(", ");
3740 }
3741 imp->require_c_string(")");
3742 }
3743 }
3744
3745 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3746 imp->location());
3747 if (is_varargs)
3748 ret->set_is_varargs();
3749 return ret;
3750 }
3751
3752 // Make a copy of a function type without a receiver.
3753
3754 Function_type*
3755 Function_type::copy_without_receiver() const
3756 {
3757 go_assert(this->is_method());
3758 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3759 this->results_,
3760 this->location_);
3761 if (this->is_varargs())
3762 ret->set_is_varargs();
3763 if (this->is_builtin())
3764 ret->set_is_builtin();
3765 return ret;
3766 }
3767
3768 // Make a copy of a function type with a receiver.
3769
3770 Function_type*
3771 Function_type::copy_with_receiver(Type* receiver_type) const
3772 {
3773 go_assert(!this->is_method());
3774 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3775 this->location_);
3776 Function_type* ret = Type::make_function_type(receiver, this->parameters_,
3777 this->results_,
3778 this->location_);
3779 if (this->is_varargs_)
3780 ret->set_is_varargs();
3781 return ret;
3782 }
3783
3784 // Make a function type.
3785
3786 Function_type*
3787 Type::make_function_type(Typed_identifier* receiver,
3788 Typed_identifier_list* parameters,
3789 Typed_identifier_list* results,
3790 Location location)
3791 {
3792 return new Function_type(receiver, parameters, results, location);
3793 }
3794
3795 // Class Pointer_type.
3796
3797 // Traversal.
3798
3799 int
3800 Pointer_type::do_traverse(Traverse* traverse)
3801 {
3802 return Type::traverse(this->to_type_, traverse);
3803 }
3804
3805 // Hash code.
3806
3807 unsigned int
3808 Pointer_type::do_hash_for_method(Gogo* gogo) const
3809 {
3810 return this->to_type_->hash_for_method(gogo) << 4;
3811 }
3812
3813 // Get the backend representation for a pointer type.
3814
3815 Btype*
3816 Pointer_type::do_get_backend(Gogo* gogo)
3817 {
3818 Btype* to_btype = this->to_type_->get_backend(gogo);
3819 return gogo->backend()->pointer_type(to_btype);
3820 }
3821
3822 // The type of a pointer type descriptor.
3823
3824 Type*
3825 Pointer_type::make_pointer_type_descriptor_type()
3826 {
3827 static Type* ret;
3828 if (ret == NULL)
3829 {
3830 Type* tdt = Type::make_type_descriptor_type();
3831 Type* ptdt = Type::make_type_descriptor_ptr_type();
3832
3833 Struct_type* s = Type::make_builtin_struct_type(2,
3834 "", tdt,
3835 "elem", ptdt);
3836
3837 ret = Type::make_builtin_named_type("PtrType", s);
3838 }
3839
3840 return ret;
3841 }
3842
3843 // The type descriptor for a pointer type.
3844
3845 Expression*
3846 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3847 {
3848 if (this->is_unsafe_pointer_type())
3849 {
3850 go_assert(name != NULL);
3851 return this->plain_type_descriptor(gogo,
3852 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3853 name);
3854 }
3855 else
3856 {
3857 Location bloc = Linemap::predeclared_location();
3858
3859 const Methods* methods;
3860 Type* deref = this->points_to();
3861 if (deref->named_type() != NULL)
3862 methods = deref->named_type()->methods();
3863 else if (deref->struct_type() != NULL)
3864 methods = deref->struct_type()->methods();
3865 else
3866 methods = NULL;
3867
3868 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3869
3870 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3871
3872 Expression_list* vals = new Expression_list();
3873 vals->reserve(2);
3874
3875 Struct_field_list::const_iterator p = fields->begin();
3876 go_assert(p->is_field_name("commonType"));
3877 vals->push_back(this->type_descriptor_constructor(gogo,
3878 RUNTIME_TYPE_KIND_PTR,
3879 name, methods, false));
3880
3881 ++p;
3882 go_assert(p->is_field_name("elem"));
3883 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3884
3885 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3886 }
3887 }
3888
3889 // Reflection string.
3890
3891 void
3892 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3893 {
3894 ret->push_back('*');
3895 this->append_reflection(this->to_type_, gogo, ret);
3896 }
3897
3898 // Mangled name.
3899
3900 void
3901 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3902 {
3903 ret->push_back('p');
3904 this->append_mangled_name(this->to_type_, gogo, ret);
3905 }
3906
3907 // Export.
3908
3909 void
3910 Pointer_type::do_export(Export* exp) const
3911 {
3912 exp->write_c_string("*");
3913 if (this->is_unsafe_pointer_type())
3914 exp->write_c_string("any");
3915 else
3916 exp->write_type(this->to_type_);
3917 }
3918
3919 // Import.
3920
3921 Pointer_type*
3922 Pointer_type::do_import(Import* imp)
3923 {
3924 imp->require_c_string("*");
3925 if (imp->match_c_string("any"))
3926 {
3927 imp->advance(3);
3928 return Type::make_pointer_type(Type::make_void_type());
3929 }
3930 Type* to = imp->read_type();
3931 return Type::make_pointer_type(to);
3932 }
3933
3934 // Make a pointer type.
3935
3936 Pointer_type*
3937 Type::make_pointer_type(Type* to_type)
3938 {
3939 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3940 static Hashtable pointer_types;
3941 Hashtable::const_iterator p = pointer_types.find(to_type);
3942 if (p != pointer_types.end())
3943 return p->second;
3944 Pointer_type* ret = new Pointer_type(to_type);
3945 pointer_types[to_type] = ret;
3946 return ret;
3947 }
3948
3949 // The nil type. We use a special type for nil because it is not the
3950 // same as any other type. In C term nil has type void*, but there is
3951 // no such type in Go.
3952
3953 class Nil_type : public Type
3954 {
3955 public:
3956 Nil_type()
3957 : Type(TYPE_NIL)
3958 { }
3959
3960 protected:
3961 bool
3962 do_compare_is_identity(Gogo*) const
3963 { return false; }
3964
3965 Btype*
3966 do_get_backend(Gogo* gogo)
3967 { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3968
3969 Expression*
3970 do_type_descriptor(Gogo*, Named_type*)
3971 { go_unreachable(); }
3972
3973 void
3974 do_reflection(Gogo*, std::string*) const
3975 { go_unreachable(); }
3976
3977 void
3978 do_mangled_name(Gogo*, std::string* ret) const
3979 { ret->push_back('n'); }
3980 };
3981
3982 // Make the nil type.
3983
3984 Type*
3985 Type::make_nil_type()
3986 {
3987 static Nil_type singleton_nil_type;
3988 return &singleton_nil_type;
3989 }
3990
3991 // The type of a function call which returns multiple values. This is
3992 // really a struct, but we don't want to confuse a function call which
3993 // returns a struct with a function call which returns multiple
3994 // values.
3995
3996 class Call_multiple_result_type : public Type
3997 {
3998 public:
3999 Call_multiple_result_type(Call_expression* call)
4000 : Type(TYPE_CALL_MULTIPLE_RESULT),
4001 call_(call)
4002 { }
4003
4004 protected:
4005 bool
4006 do_has_pointer() const
4007 {
4008 go_assert(saw_errors());
4009 return false;
4010 }
4011
4012 bool
4013 do_compare_is_identity(Gogo*) const
4014 { return false; }
4015
4016 Btype*
4017 do_get_backend(Gogo* gogo)
4018 {
4019 go_assert(saw_errors());
4020 return gogo->backend()->error_type();
4021 }
4022
4023 Expression*
4024 do_type_descriptor(Gogo*, Named_type*)
4025 {
4026 go_assert(saw_errors());
4027 return Expression::make_error(Linemap::unknown_location());
4028 }
4029
4030 void
4031 do_reflection(Gogo*, std::string*) const
4032 { go_assert(saw_errors()); }
4033
4034 void
4035 do_mangled_name(Gogo*, std::string*) const
4036 { go_assert(saw_errors()); }
4037
4038 private:
4039 // The expression being called.
4040 Call_expression* call_;
4041 };
4042
4043 // Make a call result type.
4044
4045 Type*
4046 Type::make_call_multiple_result_type(Call_expression* call)
4047 {
4048 return new Call_multiple_result_type(call);
4049 }
4050
4051 // Class Struct_field.
4052
4053 // Get the name of a field.
4054
4055 const std::string&
4056 Struct_field::field_name() const
4057 {
4058 const std::string& name(this->typed_identifier_.name());
4059 if (!name.empty())
4060 return name;
4061 else
4062 {
4063 // This is called during parsing, before anything is lowered, so
4064 // we have to be pretty careful to avoid dereferencing an
4065 // unknown type name.
4066 Type* t = this->typed_identifier_.type();
4067 Type* dt = t;
4068 if (t->classification() == Type::TYPE_POINTER)
4069 {
4070 // Very ugly.
4071 Pointer_type* ptype = static_cast<Pointer_type*>(t);
4072 dt = ptype->points_to();
4073 }
4074 if (dt->forward_declaration_type() != NULL)
4075 return dt->forward_declaration_type()->name();
4076 else if (dt->named_type() != NULL)
4077 return dt->named_type()->name();
4078 else if (t->is_error_type() || dt->is_error_type())
4079 {
4080 static const std::string error_string = "*error*";
4081 return error_string;
4082 }
4083 else
4084 {
4085 // Avoid crashing in the erroneous case where T is named but
4086 // DT is not.
4087 go_assert(t != dt);
4088 if (t->forward_declaration_type() != NULL)
4089 return t->forward_declaration_type()->name();
4090 else if (t->named_type() != NULL)
4091 return t->named_type()->name();
4092 else
4093 go_unreachable();
4094 }
4095 }
4096 }
4097
4098 // Return whether this field is named NAME.
4099
4100 bool
4101 Struct_field::is_field_name(const std::string& name) const
4102 {
4103 const std::string& me(this->typed_identifier_.name());
4104 if (!me.empty())
4105 return me == name;
4106 else
4107 {
4108 Type* t = this->typed_identifier_.type();
4109 if (t->points_to() != NULL)
4110 t = t->points_to();
4111 Named_type* nt = t->named_type();
4112 if (nt != NULL && nt->name() == name)
4113 return true;
4114
4115 // This is a horrible hack caused by the fact that we don't pack
4116 // the names of builtin types. FIXME.
4117 if (nt != NULL
4118 && nt->is_builtin()
4119 && nt->name() == Gogo::unpack_hidden_name(name))
4120 return true;
4121
4122 return false;
4123 }
4124 }
4125
4126 // Class Struct_type.
4127
4128 // Traversal.
4129
4130 int
4131 Struct_type::do_traverse(Traverse* traverse)
4132 {
4133 Struct_field_list* fields = this->fields_;
4134 if (fields != NULL)
4135 {
4136 for (Struct_field_list::iterator p = fields->begin();
4137 p != fields->end();
4138 ++p)
4139 {
4140 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
4141 return TRAVERSE_EXIT;
4142 }
4143 }
4144 return TRAVERSE_CONTINUE;
4145 }
4146
4147 // Verify that the struct type is complete and valid.
4148
4149 bool
4150 Struct_type::do_verify()
4151 {
4152 Struct_field_list* fields = this->fields_;
4153 if (fields == NULL)
4154 return true;
4155 for (Struct_field_list::iterator p = fields->begin();
4156 p != fields->end();
4157 ++p)
4158 {
4159 Type* t = p->type();
4160 if (t->is_undefined())
4161 {
4162 error_at(p->location(), "struct field type is incomplete");
4163 p->set_type(Type::make_error_type());
4164 }
4165 else if (p->is_anonymous())
4166 {
4167 if (t->named_type() != NULL && t->points_to() != NULL)
4168 {
4169 error_at(p->location(), "embedded type may not be a pointer");
4170 p->set_type(Type::make_error_type());
4171 }
4172 else if (t->points_to() != NULL
4173 && t->points_to()->interface_type() != NULL)
4174 {
4175 error_at(p->location(),
4176 "embedded type may not be pointer to interface");
4177 p->set_type(Type::make_error_type());
4178 }
4179 }
4180 }
4181 return true;
4182 }
4183
4184 // Whether this contains a pointer.
4185
4186 bool
4187 Struct_type::do_has_pointer() const
4188 {
4189 const Struct_field_list* fields = this->fields();
4190 if (fields == NULL)
4191 return false;
4192 for (Struct_field_list::const_iterator p = fields->begin();
4193 p != fields->end();
4194 ++p)
4195 {
4196 if (p->type()->has_pointer())
4197 return true;
4198 }
4199 return false;
4200 }
4201
4202 // Whether this type is identical to T.
4203
4204 bool
4205 Struct_type::is_identical(const Struct_type* t,
4206 bool errors_are_identical) const
4207 {
4208 const Struct_field_list* fields1 = this->fields();
4209 const Struct_field_list* fields2 = t->fields();
4210 if (fields1 == NULL || fields2 == NULL)
4211 return fields1 == fields2;
4212 Struct_field_list::const_iterator pf2 = fields2->begin();
4213 for (Struct_field_list::const_iterator pf1 = fields1->begin();
4214 pf1 != fields1->end();
4215 ++pf1, ++pf2)
4216 {
4217 if (pf2 == fields2->end())
4218 return false;
4219 if (pf1->field_name() != pf2->field_name())
4220 return false;
4221 if (pf1->is_anonymous() != pf2->is_anonymous()
4222 || !Type::are_identical(pf1->type(), pf2->type(),
4223 errors_are_identical, NULL))
4224 return false;
4225 if (!pf1->has_tag())
4226 {
4227 if (pf2->has_tag())
4228 return false;
4229 }
4230 else
4231 {
4232 if (!pf2->has_tag())
4233 return false;
4234 if (pf1->tag() != pf2->tag())
4235 return false;
4236 }
4237 }
4238 if (pf2 != fields2->end())
4239 return false;
4240 return true;
4241 }
4242
4243 // Whether this struct type has any hidden fields.
4244
4245 bool
4246 Struct_type::struct_has_hidden_fields(const Named_type* within,
4247 std::string* reason) const
4248 {
4249 const Struct_field_list* fields = this->fields();
4250 if (fields == NULL)
4251 return false;
4252 const Package* within_package = (within == NULL
4253 ? NULL
4254 : within->named_object()->package());
4255 for (Struct_field_list::const_iterator pf = fields->begin();
4256 pf != fields->end();
4257 ++pf)
4258 {
4259 if (within_package != NULL
4260 && !pf->is_anonymous()
4261 && Gogo::is_hidden_name(pf->field_name()))
4262 {
4263 if (reason != NULL)
4264 {
4265 std::string within_name = within->named_object()->message_name();
4266 std::string name = Gogo::message_name(pf->field_name());
4267 size_t bufsize = 200 + within_name.length() + name.length();
4268 char* buf = new char[bufsize];
4269 snprintf(buf, bufsize,
4270 _("implicit assignment of %s%s%s hidden field %s%s%s"),
4271 open_quote, within_name.c_str(), close_quote,
4272 open_quote, name.c_str(), close_quote);
4273 reason->assign(buf);
4274 delete[] buf;
4275 }
4276 return true;
4277 }
4278
4279 if (pf->type()->has_hidden_fields(within, reason))
4280 return true;
4281 }
4282
4283 return false;
4284 }
4285
4286 // Whether comparisons of this struct type are simple identity
4287 // comparisons.
4288
4289 bool
4290 Struct_type::do_compare_is_identity(Gogo* gogo) const
4291 {
4292 const Struct_field_list* fields = this->fields_;
4293 if (fields == NULL)
4294 return true;
4295 unsigned int offset = 0;
4296 for (Struct_field_list::const_iterator pf = fields->begin();
4297 pf != fields->end();
4298 ++pf)
4299 {
4300 if (Gogo::is_sink_name(pf->field_name()))
4301 return false;
4302
4303 if (!pf->type()->compare_is_identity(gogo))
4304 return false;
4305
4306 unsigned int field_align;
4307 if (!pf->type()->backend_type_align(gogo, &field_align))
4308 return false;
4309 if ((offset & (field_align - 1)) != 0)
4310 {
4311 // This struct has padding. We don't guarantee that that
4312 // padding is zero-initialized for a stack variable, so we
4313 // can't use memcmp to compare struct values.
4314 return false;
4315 }
4316
4317 unsigned int field_size;
4318 if (!pf->type()->backend_type_size(gogo, &field_size))
4319 return false;
4320 offset += field_size;
4321 }
4322 return true;
4323 }
4324
4325 // Build identity and hash functions for this struct.
4326
4327 // Hash code.
4328
4329 unsigned int
4330 Struct_type::do_hash_for_method(Gogo* gogo) const
4331 {
4332 unsigned int ret = 0;
4333 if (this->fields() != NULL)
4334 {
4335 for (Struct_field_list::const_iterator pf = this->fields()->begin();
4336 pf != this->fields()->end();
4337 ++pf)
4338 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4339 }
4340 return ret <<= 2;
4341 }
4342
4343 // Find the local field NAME.
4344
4345 const Struct_field*
4346 Struct_type::find_local_field(const std::string& name,
4347 unsigned int *pindex) const
4348 {
4349 const Struct_field_list* fields = this->fields_;
4350 if (fields == NULL)
4351 return NULL;
4352 unsigned int i = 0;
4353 for (Struct_field_list::const_iterator pf = fields->begin();
4354 pf != fields->end();
4355 ++pf, ++i)
4356 {
4357 if (pf->is_field_name(name))
4358 {
4359 if (pindex != NULL)
4360 *pindex = i;
4361 return &*pf;
4362 }
4363 }
4364 return NULL;
4365 }
4366
4367 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4368
4369 Field_reference_expression*
4370 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4371 Location location) const
4372 {
4373 unsigned int depth;
4374 return this->field_reference_depth(struct_expr, name, location, NULL,
4375 &depth);
4376 }
4377
4378 // Return an expression for a field, along with the depth at which it
4379 // was found.
4380
4381 Field_reference_expression*
4382 Struct_type::field_reference_depth(Expression* struct_expr,
4383 const std::string& name,
4384 Location location,
4385 Saw_named_type* saw,
4386 unsigned int* depth) const
4387 {
4388 const Struct_field_list* fields = this->fields_;
4389 if (fields == NULL)
4390 return NULL;
4391
4392 // Look for a field with this name.
4393 unsigned int i = 0;
4394 for (Struct_field_list::const_iterator pf = fields->begin();
4395 pf != fields->end();
4396 ++pf, ++i)
4397 {
4398 if (pf->is_field_name(name))
4399 {
4400 *depth = 0;
4401 return Expression::make_field_reference(struct_expr, i, location);
4402 }
4403 }
4404
4405 // Look for an anonymous field which contains a field with this
4406 // name.
4407 unsigned int found_depth = 0;
4408 Field_reference_expression* ret = NULL;
4409 i = 0;
4410 for (Struct_field_list::const_iterator pf = fields->begin();
4411 pf != fields->end();
4412 ++pf, ++i)
4413 {
4414 if (!pf->is_anonymous())
4415 continue;
4416
4417 Struct_type* st = pf->type()->deref()->struct_type();
4418 if (st == NULL)
4419 continue;
4420
4421 Saw_named_type* hold_saw = saw;
4422 Saw_named_type saw_here;
4423 Named_type* nt = pf->type()->named_type();
4424 if (nt == NULL)
4425 nt = pf->type()->deref()->named_type();
4426 if (nt != NULL)
4427 {
4428 Saw_named_type* q;
4429 for (q = saw; q != NULL; q = q->next)
4430 {
4431 if (q->nt == nt)
4432 {
4433 // If this is an error, it will be reported
4434 // elsewhere.
4435 break;
4436 }
4437 }
4438 if (q != NULL)
4439 continue;
4440 saw_here.next = saw;
4441 saw_here.nt = nt;
4442 saw = &saw_here;
4443 }
4444
4445 // Look for a reference using a NULL struct expression. If we
4446 // find one, fill in the struct expression with a reference to
4447 // this field.
4448 unsigned int subdepth;
4449 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
4450 location,
4451 saw,
4452 &subdepth);
4453
4454 saw = hold_saw;
4455
4456 if (sub == NULL)
4457 continue;
4458
4459 if (ret == NULL || subdepth < found_depth)
4460 {
4461 if (ret != NULL)
4462 delete ret;
4463 ret = sub;
4464 found_depth = subdepth;
4465 Expression* here = Expression::make_field_reference(struct_expr, i,
4466 location);
4467 if (pf->type()->points_to() != NULL)
4468 here = Expression::make_unary(OPERATOR_MULT, here, location);
4469 while (sub->expr() != NULL)
4470 {
4471 sub = sub->expr()->deref()->field_reference_expression();
4472 go_assert(sub != NULL);
4473 }
4474 sub->set_struct_expression(here);
4475 }
4476 else if (subdepth > found_depth)
4477 delete sub;
4478 else
4479 {
4480 // We do not handle ambiguity here--it should be handled by
4481 // Type::bind_field_or_method.
4482 delete sub;
4483 found_depth = 0;
4484 ret = NULL;
4485 }
4486 }
4487
4488 if (ret != NULL)
4489 *depth = found_depth + 1;
4490
4491 return ret;
4492 }
4493
4494 // Return the total number of fields, including embedded fields.
4495
4496 unsigned int
4497 Struct_type::total_field_count() const
4498 {
4499 if (this->fields_ == NULL)
4500 return 0;
4501 unsigned int ret = 0;
4502 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4503 pf != this->fields_->end();
4504 ++pf)
4505 {
4506 if (!pf->is_anonymous() || pf->type()->struct_type() == NULL)
4507 ++ret;
4508 else
4509 ret += pf->type()->struct_type()->total_field_count();
4510 }
4511 return ret;
4512 }
4513
4514 // Return whether NAME is an unexported field, for better error reporting.
4515
4516 bool
4517 Struct_type::is_unexported_local_field(Gogo* gogo,
4518 const std::string& name) const
4519 {
4520 const Struct_field_list* fields = this->fields_;
4521 if (fields != NULL)
4522 {
4523 for (Struct_field_list::const_iterator pf = fields->begin();
4524 pf != fields->end();
4525 ++pf)
4526 {
4527 const std::string& field_name(pf->field_name());
4528 if (Gogo::is_hidden_name(field_name)
4529 && name == Gogo::unpack_hidden_name(field_name)
4530 && gogo->pack_hidden_name(name, false) != field_name)
4531 return true;
4532 }
4533 }
4534 return false;
4535 }
4536
4537 // Finalize the methods of an unnamed struct.
4538
4539 void
4540 Struct_type::finalize_methods(Gogo* gogo)
4541 {
4542 if (this->all_methods_ != NULL)
4543 return;
4544 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
4545 }
4546
4547 // Return the method NAME, or NULL if there isn't one or if it is
4548 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
4549 // ambiguous.
4550
4551 Method*
4552 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
4553 {
4554 return Type::method_function(this->all_methods_, name, is_ambiguous);
4555 }
4556
4557 // Return a pointer to the interface method table for this type for
4558 // the interface INTERFACE. IS_POINTER is true if this is for a
4559 // pointer to THIS.
4560
4561 tree
4562 Struct_type::interface_method_table(Gogo* gogo,
4563 const Interface_type* interface,
4564 bool is_pointer)
4565 {
4566 return Type::interface_method_table(gogo, this, interface, is_pointer,
4567 &this->interface_method_tables_,
4568 &this->pointer_interface_method_tables_);
4569 }
4570
4571 // Convert struct fields to the backend representation. This is not
4572 // declared in types.h so that types.h doesn't have to #include
4573 // backend.h.
4574
4575 static void
4576 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
4577 bool use_placeholder,
4578 std::vector<Backend::Btyped_identifier>* bfields)
4579 {
4580 bfields->resize(fields->size());
4581 size_t i = 0;
4582 for (Struct_field_list::const_iterator p = fields->begin();
4583 p != fields->end();
4584 ++p, ++i)
4585 {
4586 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
4587 (*bfields)[i].btype = (use_placeholder
4588 ? p->type()->get_backend_placeholder(gogo)
4589 : p->type()->get_backend(gogo));
4590 (*bfields)[i].location = p->location();
4591 }
4592 go_assert(i == fields->size());
4593 }
4594
4595 // Get the tree for a struct type.
4596
4597 Btype*
4598 Struct_type::do_get_backend(Gogo* gogo)
4599 {
4600 std::vector<Backend::Btyped_identifier> bfields;
4601 get_backend_struct_fields(gogo, this->fields_, false, &bfields);
4602 return gogo->backend()->struct_type(bfields);
4603 }
4604
4605 // Finish the backend representation of the fields of a struct.
4606
4607 void
4608 Struct_type::finish_backend_fields(Gogo* gogo)
4609 {
4610 const Struct_field_list* fields = this->fields_;
4611 if (fields != NULL)
4612 {
4613 for (Struct_field_list::const_iterator p = fields->begin();
4614 p != fields->end();
4615 ++p)
4616 p->type()->get_backend(gogo);
4617 }
4618 }
4619
4620 // The type of a struct type descriptor.
4621
4622 Type*
4623 Struct_type::make_struct_type_descriptor_type()
4624 {
4625 static Type* ret;
4626 if (ret == NULL)
4627 {
4628 Type* tdt = Type::make_type_descriptor_type();
4629 Type* ptdt = Type::make_type_descriptor_ptr_type();
4630
4631 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4632 Type* string_type = Type::lookup_string_type();
4633 Type* pointer_string_type = Type::make_pointer_type(string_type);
4634
4635 Struct_type* sf =
4636 Type::make_builtin_struct_type(5,
4637 "name", pointer_string_type,
4638 "pkgPath", pointer_string_type,
4639 "typ", ptdt,
4640 "tag", pointer_string_type,
4641 "offset", uintptr_type);
4642 Type* nsf = Type::make_builtin_named_type("structField", sf);
4643
4644 Type* slice_type = Type::make_array_type(nsf, NULL);
4645
4646 Struct_type* s = Type::make_builtin_struct_type(2,
4647 "", tdt,
4648 "fields", slice_type);
4649
4650 ret = Type::make_builtin_named_type("StructType", s);
4651 }
4652
4653 return ret;
4654 }
4655
4656 // Build a type descriptor for a struct type.
4657
4658 Expression*
4659 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4660 {
4661 Location bloc = Linemap::predeclared_location();
4662
4663 Type* stdt = Struct_type::make_struct_type_descriptor_type();
4664
4665 const Struct_field_list* fields = stdt->struct_type()->fields();
4666
4667 Expression_list* vals = new Expression_list();
4668 vals->reserve(2);
4669
4670 const Methods* methods = this->methods();
4671 // A named struct should not have methods--the methods should attach
4672 // to the named type.
4673 go_assert(methods == NULL || name == NULL);
4674
4675 Struct_field_list::const_iterator ps = fields->begin();
4676 go_assert(ps->is_field_name("commonType"));
4677 vals->push_back(this->type_descriptor_constructor(gogo,
4678 RUNTIME_TYPE_KIND_STRUCT,
4679 name, methods, true));
4680
4681 ++ps;
4682 go_assert(ps->is_field_name("fields"));
4683
4684 Expression_list* elements = new Expression_list();
4685 elements->reserve(this->fields_->size());
4686 Type* element_type = ps->type()->array_type()->element_type();
4687 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4688 pf != this->fields_->end();
4689 ++pf)
4690 {
4691 const Struct_field_list* f = element_type->struct_type()->fields();
4692
4693 Expression_list* fvals = new Expression_list();
4694 fvals->reserve(5);
4695
4696 Struct_field_list::const_iterator q = f->begin();
4697 go_assert(q->is_field_name("name"));
4698 if (pf->is_anonymous())
4699 fvals->push_back(Expression::make_nil(bloc));
4700 else
4701 {
4702 std::string n = Gogo::unpack_hidden_name(pf->field_name());
4703 Expression* s = Expression::make_string(n, bloc);
4704 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4705 }
4706
4707 ++q;
4708 go_assert(q->is_field_name("pkgPath"));
4709 if (!Gogo::is_hidden_name(pf->field_name()))
4710 fvals->push_back(Expression::make_nil(bloc));
4711 else
4712 {
4713 std::string n = Gogo::hidden_name_pkgpath(pf->field_name());
4714 Expression* s = Expression::make_string(n, bloc);
4715 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4716 }
4717
4718 ++q;
4719 go_assert(q->is_field_name("typ"));
4720 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
4721
4722 ++q;
4723 go_assert(q->is_field_name("tag"));
4724 if (!pf->has_tag())
4725 fvals->push_back(Expression::make_nil(bloc));
4726 else
4727 {
4728 Expression* s = Expression::make_string(pf->tag(), bloc);
4729 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4730 }
4731
4732 ++q;
4733 go_assert(q->is_field_name("offset"));
4734 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
4735
4736 Expression* v = Expression::make_struct_composite_literal(element_type,
4737 fvals, bloc);
4738 elements->push_back(v);
4739 }
4740
4741 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
4742 elements, bloc));
4743
4744 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4745 }
4746
4747 // Write the hash function for a struct which can not use the identity
4748 // function.
4749
4750 void
4751 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
4752 Function_type* hash_fntype,
4753 Function_type* equal_fntype)
4754 {
4755 Location bloc = Linemap::predeclared_location();
4756
4757 // The pointer to the struct that we are going to hash. This is an
4758 // argument to the hash function we are implementing here.
4759 Named_object* key_arg = gogo->lookup("key", NULL);
4760 go_assert(key_arg != NULL);
4761 Type* key_arg_type = key_arg->var_value()->type();
4762
4763 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4764
4765 // Get a 0.
4766 mpz_t ival;
4767 mpz_init_set_ui(ival, 0);
4768 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
4769 mpz_clear(ival);
4770
4771 // Make a temporary to hold the return value, initialized to 0.
4772 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
4773 bloc);
4774 gogo->add_statement(retval);
4775
4776 // Make a temporary to hold the key as a uintptr.
4777 Expression* ref = Expression::make_var_reference(key_arg, bloc);
4778 ref = Expression::make_cast(uintptr_type, ref, bloc);
4779 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
4780 bloc);
4781 gogo->add_statement(key);
4782
4783 // Loop over the struct fields.
4784 bool first = true;
4785 const Struct_field_list* fields = this->fields_;
4786 for (Struct_field_list::const_iterator pf = fields->begin();
4787 pf != fields->end();
4788 ++pf)
4789 {
4790 if (Gogo::is_sink_name(pf->field_name()))
4791 continue;
4792
4793 if (first)
4794 first = false;
4795 else
4796 {
4797 // Multiply retval by 33.
4798 mpz_init_set_ui(ival, 33);
4799 Expression* i33 = Expression::make_integer(&ival, uintptr_type,
4800 bloc);
4801 mpz_clear(ival);
4802
4803 ref = Expression::make_temporary_reference(retval, bloc);
4804 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ,
4805 ref, i33, bloc);
4806 gogo->add_statement(s);
4807 }
4808
4809 // Get a pointer to the value of this field.
4810 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
4811 ref = Expression::make_temporary_reference(key, bloc);
4812 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
4813 bloc);
4814 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
4815
4816 // Get the size of this field.
4817 Expression* size = Expression::make_type_info(pf->type(),
4818 Expression::TYPE_INFO_SIZE);
4819
4820 // Get the hash function to use for the type of this field.
4821 Named_object* hash_fn;
4822 Named_object* equal_fn;
4823 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
4824 equal_fntype, &hash_fn, &equal_fn);
4825
4826 // Call the hash function for the field.
4827 Expression_list* args = new Expression_list();
4828 args->push_back(subkey);
4829 args->push_back(size);
4830 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
4831 Expression* call = Expression::make_call(func, args, false, bloc);
4832
4833 // Add the field's hash value to retval.
4834 Temporary_reference_expression* tref =
4835 Expression::make_temporary_reference(retval, bloc);
4836 tref->set_is_lvalue();
4837 Statement* s = Statement::make_assignment_operation(OPERATOR_PLUSEQ,
4838 tref, call, bloc);
4839 gogo->add_statement(s);
4840 }
4841
4842 // Return retval to the caller of the hash function.
4843 Expression_list* vals = new Expression_list();
4844 ref = Expression::make_temporary_reference(retval, bloc);
4845 vals->push_back(ref);
4846 Statement* s = Statement::make_return_statement(vals, bloc);
4847 gogo->add_statement(s);
4848 }
4849
4850 // Write the equality function for a struct which can not use the
4851 // identity function.
4852
4853 void
4854 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
4855 {
4856 Location bloc = Linemap::predeclared_location();
4857
4858 // The pointers to the structs we are going to compare.
4859 Named_object* key1_arg = gogo->lookup("key1", NULL);
4860 Named_object* key2_arg = gogo->lookup("key2", NULL);
4861 go_assert(key1_arg != NULL && key2_arg != NULL);
4862
4863 // Build temporaries with the right types.
4864 Type* pt = Type::make_pointer_type(name != NULL
4865 ? static_cast<Type*>(name)
4866 : static_cast<Type*>(this));
4867
4868 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
4869 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4870 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
4871 gogo->add_statement(p1);
4872
4873 ref = Expression::make_var_reference(key2_arg, bloc);
4874 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4875 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
4876 gogo->add_statement(p2);
4877
4878 const Struct_field_list* fields = this->fields_;
4879 unsigned int field_index = 0;
4880 for (Struct_field_list::const_iterator pf = fields->begin();
4881 pf != fields->end();
4882 ++pf, ++field_index)
4883 {
4884 if (Gogo::is_sink_name(pf->field_name()))
4885 continue;
4886
4887 // Compare one field in both P1 and P2.
4888 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
4889 f1 = Expression::make_unary(OPERATOR_MULT, f1, bloc);
4890 f1 = Expression::make_field_reference(f1, field_index, bloc);
4891
4892 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
4893 f2 = Expression::make_unary(OPERATOR_MULT, f2, bloc);
4894 f2 = Expression::make_field_reference(f2, field_index, bloc);
4895
4896 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
4897
4898 // If the values are not equal, return false.
4899 gogo->start_block(bloc);
4900 Expression_list* vals = new Expression_list();
4901 vals->push_back(Expression::make_boolean(false, bloc));
4902 Statement* s = Statement::make_return_statement(vals, bloc);
4903 gogo->add_statement(s);
4904 Block* then_block = gogo->finish_block(bloc);
4905
4906 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
4907 gogo->add_statement(s);
4908 }
4909
4910 // All the fields are equal, so return true.
4911 Expression_list* vals = new Expression_list();
4912 vals->push_back(Expression::make_boolean(true, bloc));
4913 Statement* s = Statement::make_return_statement(vals, bloc);
4914 gogo->add_statement(s);
4915 }
4916
4917 // Reflection string.
4918
4919 void
4920 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
4921 {
4922 ret->append("struct { ");
4923
4924 for (Struct_field_list::const_iterator p = this->fields_->begin();
4925 p != this->fields_->end();
4926 ++p)
4927 {
4928 if (p != this->fields_->begin())
4929 ret->append("; ");
4930 if (p->is_anonymous())
4931 ret->push_back('?');
4932 else
4933 ret->append(Gogo::unpack_hidden_name(p->field_name()));
4934 ret->push_back(' ');
4935 this->append_reflection(p->type(), gogo, ret);
4936
4937 if (p->has_tag())
4938 {
4939 const std::string& tag(p->tag());
4940 ret->append(" \"");
4941 for (std::string::const_iterator p = tag.begin();
4942 p != tag.end();
4943 ++p)
4944 {
4945 if (*p == '\0')
4946 ret->append("\\x00");
4947 else if (*p == '\n')
4948 ret->append("\\n");
4949 else if (*p == '\t')
4950 ret->append("\\t");
4951 else if (*p == '"')
4952 ret->append("\\\"");
4953 else if (*p == '\\')
4954 ret->append("\\\\");
4955 else
4956 ret->push_back(*p);
4957 }
4958 ret->push_back('"');
4959 }
4960 }
4961
4962 ret->append(" }");
4963 }
4964
4965 // Mangled name.
4966
4967 void
4968 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4969 {
4970 ret->push_back('S');
4971
4972 const Struct_field_list* fields = this->fields_;
4973 if (fields != NULL)
4974 {
4975 for (Struct_field_list::const_iterator p = fields->begin();
4976 p != fields->end();
4977 ++p)
4978 {
4979 if (p->is_anonymous())
4980 ret->append("0_");
4981 else
4982 {
4983 std::string n = Gogo::unpack_hidden_name(p->field_name());
4984 char buf[20];
4985 snprintf(buf, sizeof buf, "%u_",
4986 static_cast<unsigned int>(n.length()));
4987 ret->append(buf);
4988 ret->append(n);
4989 }
4990 this->append_mangled_name(p->type(), gogo, ret);
4991 if (p->has_tag())
4992 {
4993 const std::string& tag(p->tag());
4994 std::string out;
4995 for (std::string::const_iterator p = tag.begin();
4996 p != tag.end();
4997 ++p)
4998 {
4999 if (ISALNUM(*p) || *p == '_')
5000 out.push_back(*p);
5001 else
5002 {
5003 char buf[20];
5004 snprintf(buf, sizeof buf, ".%x.",
5005 static_cast<unsigned int>(*p));
5006 out.append(buf);
5007 }
5008 }
5009 char buf[20];
5010 snprintf(buf, sizeof buf, "T%u_",
5011 static_cast<unsigned int>(out.length()));
5012 ret->append(buf);
5013 ret->append(out);
5014 }
5015 }
5016 }
5017
5018 ret->push_back('e');
5019 }
5020
5021 // If the offset of field INDEX in the backend implementation can be
5022 // determined, set *POFFSET to the offset in bytes and return true.
5023 // Otherwise, return false.
5024
5025 bool
5026 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
5027 unsigned int* poffset)
5028 {
5029 if (!this->is_backend_type_size_known(gogo))
5030 return false;
5031 Btype* bt = this->get_backend_placeholder(gogo);
5032 size_t offset = gogo->backend()->type_field_offset(bt, index);
5033 *poffset = static_cast<unsigned int>(offset);
5034 if (*poffset != offset)
5035 return false;
5036 return true;
5037 }
5038
5039 // Export.
5040
5041 void
5042 Struct_type::do_export(Export* exp) const
5043 {
5044 exp->write_c_string("struct { ");
5045 const Struct_field_list* fields = this->fields_;
5046 go_assert(fields != NULL);
5047 for (Struct_field_list::const_iterator p = fields->begin();
5048 p != fields->end();
5049 ++p)
5050 {
5051 if (p->is_anonymous())
5052 exp->write_string("? ");
5053 else
5054 {
5055 exp->write_string(p->field_name());
5056 exp->write_c_string(" ");
5057 }
5058 exp->write_type(p->type());
5059
5060 if (p->has_tag())
5061 {
5062 exp->write_c_string(" ");
5063 Expression* expr =
5064 Expression::make_string(p->tag(), Linemap::predeclared_location());
5065 expr->export_expression(exp);
5066 delete expr;
5067 }
5068
5069 exp->write_c_string("; ");
5070 }
5071 exp->write_c_string("}");
5072 }
5073
5074 // Import.
5075
5076 Struct_type*
5077 Struct_type::do_import(Import* imp)
5078 {
5079 imp->require_c_string("struct { ");
5080 Struct_field_list* fields = new Struct_field_list;
5081 if (imp->peek_char() != '}')
5082 {
5083 while (true)
5084 {
5085 std::string name;
5086 if (imp->match_c_string("? "))
5087 imp->advance(2);
5088 else
5089 {
5090 name = imp->read_identifier();
5091 imp->require_c_string(" ");
5092 }
5093 Type* ftype = imp->read_type();
5094
5095 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
5096
5097 if (imp->peek_char() == ' ')
5098 {
5099 imp->advance(1);
5100 Expression* expr = Expression::import_expression(imp);
5101 String_expression* sexpr = expr->string_expression();
5102 go_assert(sexpr != NULL);
5103 sf.set_tag(sexpr->val());
5104 delete sexpr;
5105 }
5106
5107 imp->require_c_string("; ");
5108 fields->push_back(sf);
5109 if (imp->peek_char() == '}')
5110 break;
5111 }
5112 }
5113 imp->require_c_string("}");
5114
5115 return Type::make_struct_type(fields, imp->location());
5116 }
5117
5118 // Make a struct type.
5119
5120 Struct_type*
5121 Type::make_struct_type(Struct_field_list* fields,
5122 Location location)
5123 {
5124 return new Struct_type(fields, location);
5125 }
5126
5127 // Class Array_type.
5128
5129 // Whether two array types are identical.
5130
5131 bool
5132 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
5133 {
5134 if (!Type::are_identical(this->element_type(), t->element_type(),
5135 errors_are_identical, NULL))
5136 return false;
5137
5138 Expression* l1 = this->length();
5139 Expression* l2 = t->length();
5140
5141 // Slices of the same element type are identical.
5142 if (l1 == NULL && l2 == NULL)
5143 return true;
5144
5145 // Arrays of the same element type are identical if they have the
5146 // same length.
5147 if (l1 != NULL && l2 != NULL)
5148 {
5149 if (l1 == l2)
5150 return true;
5151
5152 // Try to determine the lengths. If we can't, assume the arrays
5153 // are not identical.
5154 bool ret = false;
5155 Numeric_constant nc1, nc2;
5156 if (l1->numeric_constant_value(&nc1)
5157 && l2->numeric_constant_value(&nc2))
5158 {
5159 mpz_t v1;
5160 if (nc1.to_int(&v1))
5161 {
5162 mpz_t v2;
5163 if (nc2.to_int(&v2))
5164 {
5165 ret = mpz_cmp(v1, v2) == 0;
5166 mpz_clear(v2);
5167 }
5168 mpz_clear(v1);
5169 }
5170 }
5171 return ret;
5172 }
5173
5174 // Otherwise the arrays are not identical.
5175 return false;
5176 }
5177
5178 // Traversal.
5179
5180 int
5181 Array_type::do_traverse(Traverse* traverse)
5182 {
5183 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
5184 return TRAVERSE_EXIT;
5185 if (this->length_ != NULL
5186 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
5187 return TRAVERSE_EXIT;
5188 return TRAVERSE_CONTINUE;
5189 }
5190
5191 // Check that the length is valid.
5192
5193 bool
5194 Array_type::verify_length()
5195 {
5196 if (this->length_ == NULL)
5197 return true;
5198
5199 Type_context context(Type::lookup_integer_type("int"), false);
5200 this->length_->determine_type(&context);
5201
5202 if (!this->length_->is_constant())
5203 {
5204 error_at(this->length_->location(), "array bound is not constant");
5205 return false;
5206 }
5207
5208 Numeric_constant nc;
5209 if (!this->length_->numeric_constant_value(&nc))
5210 {
5211 if (this->length_->type()->integer_type() != NULL
5212 || this->length_->type()->float_type() != NULL)
5213 error_at(this->length_->location(), "array bound is not constant");
5214 else
5215 error_at(this->length_->location(), "array bound is not numeric");
5216 return false;
5217 }
5218
5219 unsigned long val;
5220 switch (nc.to_unsigned_long(&val))
5221 {
5222 case Numeric_constant::NC_UL_VALID:
5223 break;
5224 case Numeric_constant::NC_UL_NOTINT:
5225 error_at(this->length_->location(), "array bound truncated to integer");
5226 return false;
5227 case Numeric_constant::NC_UL_NEGATIVE:
5228 error_at(this->length_->location(), "negative array bound");
5229 return false;
5230 case Numeric_constant::NC_UL_BIG:
5231 error_at(this->length_->location(), "array bound overflows");
5232 return false;
5233 default:
5234 go_unreachable();
5235 }
5236
5237 Type* int_type = Type::lookup_integer_type("int");
5238 unsigned int tbits = int_type->integer_type()->bits();
5239 if (sizeof(val) <= tbits * 8
5240 && val >> (tbits - 1) != 0)
5241 {
5242 error_at(this->length_->location(), "array bound overflows");
5243 return false;
5244 }
5245
5246 return true;
5247 }
5248
5249 // Verify the type.
5250
5251 bool
5252 Array_type::do_verify()
5253 {
5254 if (!this->verify_length())
5255 this->length_ = Expression::make_error(this->length_->location());
5256 return true;
5257 }
5258
5259 // Whether we can use memcmp to compare this array.
5260
5261 bool
5262 Array_type::do_compare_is_identity(Gogo* gogo) const
5263 {
5264 if (this->length_ == NULL)
5265 return false;
5266
5267 // Check for [...], which indicates that this is not a real type.
5268 if (this->length_->is_nil_expression())
5269 return false;
5270
5271 if (!this->element_type_->compare_is_identity(gogo))
5272 return false;
5273
5274 // If there is any padding, then we can't use memcmp.
5275 unsigned int size;
5276 unsigned int align;
5277 if (!this->element_type_->backend_type_size(gogo, &size)
5278 || !this->element_type_->backend_type_align(gogo, &align))
5279 return false;
5280 if ((size & (align - 1)) != 0)
5281 return false;
5282
5283 return true;
5284 }
5285
5286 // Array type hash code.
5287
5288 unsigned int
5289 Array_type::do_hash_for_method(Gogo* gogo) const
5290 {
5291 // There is no very convenient way to get a hash code for the
5292 // length.
5293 return this->element_type_->hash_for_method(gogo) + 1;
5294 }
5295
5296 // Write the hash function for an array which can not use the identify
5297 // function.
5298
5299 void
5300 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
5301 Function_type* hash_fntype,
5302 Function_type* equal_fntype)
5303 {
5304 Location bloc = Linemap::predeclared_location();
5305
5306 // The pointer to the array that we are going to hash. This is an
5307 // argument to the hash function we are implementing here.
5308 Named_object* key_arg = gogo->lookup("key", NULL);
5309 go_assert(key_arg != NULL);
5310 Type* key_arg_type = key_arg->var_value()->type();
5311
5312 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5313
5314 // Get a 0.
5315 mpz_t ival;
5316 mpz_init_set_ui(ival, 0);
5317 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
5318 mpz_clear(ival);
5319
5320 // Make a temporary to hold the return value, initialized to 0.
5321 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
5322 bloc);
5323 gogo->add_statement(retval);
5324
5325 // Make a temporary to hold the key as a uintptr.
5326 Expression* ref = Expression::make_var_reference(key_arg, bloc);
5327 ref = Expression::make_cast(uintptr_type, ref, bloc);
5328 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
5329 bloc);
5330 gogo->add_statement(key);
5331
5332 // Loop over the array elements.
5333 // for i = range a
5334 Type* int_type = Type::lookup_integer_type("int");
5335 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5336 gogo->add_statement(index);
5337
5338 Expression* iref = Expression::make_temporary_reference(index, bloc);
5339 Expression* aref = Expression::make_var_reference(key_arg, bloc);
5340 Type* pt = Type::make_pointer_type(name != NULL
5341 ? static_cast<Type*>(name)
5342 : static_cast<Type*>(this));
5343 aref = Expression::make_cast(pt, aref, bloc);
5344 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5345 NULL,
5346 aref,
5347 bloc);
5348
5349 gogo->start_block(bloc);
5350
5351 // Multiply retval by 33.
5352 mpz_init_set_ui(ival, 33);
5353 Expression* i33 = Expression::make_integer(&ival, uintptr_type, bloc);
5354 mpz_clear(ival);
5355
5356 ref = Expression::make_temporary_reference(retval, bloc);
5357 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ, ref,
5358 i33, bloc);
5359 gogo->add_statement(s);
5360
5361 // Get the hash function for the element type.
5362 Named_object* hash_fn;
5363 Named_object* equal_fn;
5364 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
5365 hash_fntype, equal_fntype, &hash_fn,
5366 &equal_fn);
5367
5368 // Get a pointer to this element in the loop.
5369 Expression* subkey = Expression::make_temporary_reference(key, bloc);
5370 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
5371
5372 // Get the size of each element.
5373 Expression* ele_size = Expression::make_type_info(this->element_type_,
5374 Expression::TYPE_INFO_SIZE);
5375
5376 // Get the hash of this element.
5377 Expression_list* args = new Expression_list();
5378 args->push_back(subkey);
5379 args->push_back(ele_size);
5380 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
5381 Expression* call = Expression::make_call(func, args, false, bloc);
5382
5383 // Add the element's hash value to retval.
5384 Temporary_reference_expression* tref =
5385 Expression::make_temporary_reference(retval, bloc);
5386 tref->set_is_lvalue();
5387 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, call, bloc);
5388 gogo->add_statement(s);
5389
5390 // Increase the element pointer.
5391 tref = Expression::make_temporary_reference(key, bloc);
5392 tref->set_is_lvalue();
5393 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
5394 bloc);
5395
5396 Block* statements = gogo->finish_block(bloc);
5397
5398 for_range->add_statements(statements);
5399 gogo->add_statement(for_range);
5400
5401 // Return retval to the caller of the hash function.
5402 Expression_list* vals = new Expression_list();
5403 ref = Expression::make_temporary_reference(retval, bloc);
5404 vals->push_back(ref);
5405 s = Statement::make_return_statement(vals, bloc);
5406 gogo->add_statement(s);
5407 }
5408
5409 // Write the equality function for an array which can not use the
5410 // identity function.
5411
5412 void
5413 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
5414 {
5415 Location bloc = Linemap::predeclared_location();
5416
5417 // The pointers to the arrays we are going to compare.
5418 Named_object* key1_arg = gogo->lookup("key1", NULL);
5419 Named_object* key2_arg = gogo->lookup("key2", NULL);
5420 go_assert(key1_arg != NULL && key2_arg != NULL);
5421
5422 // Build temporaries for the keys with the right types.
5423 Type* pt = Type::make_pointer_type(name != NULL
5424 ? static_cast<Type*>(name)
5425 : static_cast<Type*>(this));
5426
5427 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
5428 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5429 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
5430 gogo->add_statement(p1);
5431
5432 ref = Expression::make_var_reference(key2_arg, bloc);
5433 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5434 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
5435 gogo->add_statement(p2);
5436
5437 // Loop over the array elements.
5438 // for i = range a
5439 Type* int_type = Type::lookup_integer_type("int");
5440 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5441 gogo->add_statement(index);
5442
5443 Expression* iref = Expression::make_temporary_reference(index, bloc);
5444 Expression* aref = Expression::make_temporary_reference(p1, bloc);
5445 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5446 NULL,
5447 aref,
5448 bloc);
5449
5450 gogo->start_block(bloc);
5451
5452 // Compare element in P1 and P2.
5453 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
5454 e1 = Expression::make_unary(OPERATOR_MULT, e1, bloc);
5455 ref = Expression::make_temporary_reference(index, bloc);
5456 e1 = Expression::make_array_index(e1, ref, NULL, bloc);
5457
5458 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
5459 e2 = Expression::make_unary(OPERATOR_MULT, e2, bloc);
5460 ref = Expression::make_temporary_reference(index, bloc);
5461 e2 = Expression::make_array_index(e2, ref, NULL, bloc);
5462
5463 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
5464
5465 // If the elements are not equal, return false.
5466 gogo->start_block(bloc);
5467 Expression_list* vals = new Expression_list();
5468 vals->push_back(Expression::make_boolean(false, bloc));
5469 Statement* s = Statement::make_return_statement(vals, bloc);
5470 gogo->add_statement(s);
5471 Block* then_block = gogo->finish_block(bloc);
5472
5473 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
5474 gogo->add_statement(s);
5475
5476 Block* statements = gogo->finish_block(bloc);
5477
5478 for_range->add_statements(statements);
5479 gogo->add_statement(for_range);
5480
5481 // All the elements are equal, so return true.
5482 vals = new Expression_list();
5483 vals->push_back(Expression::make_boolean(true, bloc));
5484 s = Statement::make_return_statement(vals, bloc);
5485 gogo->add_statement(s);
5486 }
5487
5488 // Get a tree for the length of a fixed array. The length may be
5489 // computed using a function call, so we must only evaluate it once.
5490
5491 tree
5492 Array_type::get_length_tree(Gogo* gogo)
5493 {
5494 go_assert(this->length_ != NULL);
5495 if (this->length_tree_ == NULL_TREE)
5496 {
5497 Numeric_constant nc;
5498 mpz_t val;
5499 if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val))
5500 {
5501 if (mpz_sgn(val) < 0)
5502 {
5503 this->length_tree_ = error_mark_node;
5504 return this->length_tree_;
5505 }
5506 Type* t = nc.type();
5507 if (t == NULL)
5508 t = Type::lookup_integer_type("int");
5509 else if (t->is_abstract())
5510 t = t->make_non_abstract_type();
5511 tree tt = type_to_tree(t->get_backend(gogo));
5512 this->length_tree_ = Expression::integer_constant_tree(val, tt);
5513 mpz_clear(val);
5514 }
5515 else
5516 {
5517 // Make up a translation context for the array length
5518 // expression. FIXME: This won't work in general.
5519 Translate_context context(gogo, NULL, NULL, NULL);
5520 tree len = this->length_->get_tree(&context);
5521 if (len != error_mark_node)
5522 {
5523 len = convert_to_integer(integer_type_node, len);
5524 len = save_expr(len);
5525 }
5526 this->length_tree_ = len;
5527 }
5528 }
5529 return this->length_tree_;
5530 }
5531
5532 // Get the backend representation of the fields of a slice. This is
5533 // not declared in types.h so that types.h doesn't have to #include
5534 // backend.h.
5535 //
5536 // We use int for the count and capacity fields. This matches 6g.
5537 // The language more or less assumes that we can't allocate space of a
5538 // size which does not fit in int.
5539
5540 static void
5541 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
5542 std::vector<Backend::Btyped_identifier>* bfields)
5543 {
5544 bfields->resize(3);
5545
5546 Type* pet = Type::make_pointer_type(type->element_type());
5547 Btype* pbet = (use_placeholder
5548 ? pet->get_backend_placeholder(gogo)
5549 : pet->get_backend(gogo));
5550 Location ploc = Linemap::predeclared_location();
5551
5552 Backend::Btyped_identifier* p = &(*bfields)[0];
5553 p->name = "__values";
5554 p->btype = pbet;
5555 p->location = ploc;
5556
5557 Type* int_type = Type::lookup_integer_type("int");
5558
5559 p = &(*bfields)[1];
5560 p->name = "__count";
5561 p->btype = int_type->get_backend(gogo);
5562 p->location = ploc;
5563
5564 p = &(*bfields)[2];
5565 p->name = "__capacity";
5566 p->btype = int_type->get_backend(gogo);
5567 p->location = ploc;
5568 }
5569
5570 // Get a tree for the type of this array. A fixed array is simply
5571 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
5572 // just like an array in C. An open array is a struct with three
5573 // fields: a data pointer, the length, and the capacity.
5574
5575 Btype*
5576 Array_type::do_get_backend(Gogo* gogo)
5577 {
5578 if (this->length_ == NULL)
5579 {
5580 std::vector<Backend::Btyped_identifier> bfields;
5581 get_backend_slice_fields(gogo, this, false, &bfields);
5582 return gogo->backend()->struct_type(bfields);
5583 }
5584 else
5585 {
5586 Btype* element = this->get_backend_element(gogo, false);
5587 Bexpression* len = this->get_backend_length(gogo);
5588 return gogo->backend()->array_type(element, len);
5589 }
5590 }
5591
5592 // Return the backend representation of the element type.
5593
5594 Btype*
5595 Array_type::get_backend_element(Gogo* gogo, bool use_placeholder)
5596 {
5597 if (use_placeholder)
5598 return this->element_type_->get_backend_placeholder(gogo);
5599 else
5600 return this->element_type_->get_backend(gogo);
5601 }
5602
5603 // Return the backend representation of the length.
5604
5605 Bexpression*
5606 Array_type::get_backend_length(Gogo* gogo)
5607 {
5608 return tree_to_expr(this->get_length_tree(gogo));
5609 }
5610
5611 // Finish backend representation of the array.
5612
5613 void
5614 Array_type::finish_backend_element(Gogo* gogo)
5615 {
5616 Type* et = this->array_type()->element_type();
5617 et->get_backend(gogo);
5618 if (this->is_slice_type())
5619 {
5620 // This relies on the fact that we always use the same
5621 // structure for a pointer to any given type.
5622 Type* pet = Type::make_pointer_type(et);
5623 pet->get_backend(gogo);
5624 }
5625 }
5626
5627 // Return a tree for a pointer to the values in ARRAY.
5628
5629 tree
5630 Array_type::value_pointer_tree(Gogo*, tree array) const
5631 {
5632 tree ret;
5633 if (this->length() != NULL)
5634 {
5635 // Fixed array.
5636 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
5637 build_fold_addr_expr(array));
5638 }
5639 else
5640 {
5641 // Open array.
5642 tree field = TYPE_FIELDS(TREE_TYPE(array));
5643 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
5644 "__values") == 0);
5645 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
5646 NULL_TREE);
5647 }
5648 if (TREE_CONSTANT(array))
5649 TREE_CONSTANT(ret) = 1;
5650 return ret;
5651 }
5652
5653 // Return a tree for the length of the array ARRAY which has this
5654 // type.
5655
5656 tree
5657 Array_type::length_tree(Gogo* gogo, tree array)
5658 {
5659 if (this->length_ != NULL)
5660 {
5661 if (TREE_CODE(array) == SAVE_EXPR)
5662 return fold_convert(integer_type_node, this->get_length_tree(gogo));
5663 else
5664 return omit_one_operand(integer_type_node,
5665 this->get_length_tree(gogo), array);
5666 }
5667
5668 // This is an open array. We need to read the length field.
5669
5670 tree type = TREE_TYPE(array);
5671 go_assert(TREE_CODE(type) == RECORD_TYPE);
5672
5673 tree field = DECL_CHAIN(TYPE_FIELDS(type));
5674 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
5675
5676 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5677 if (TREE_CONSTANT(array))
5678 TREE_CONSTANT(ret) = 1;
5679 return ret;
5680 }
5681
5682 // Return a tree for the capacity of the array ARRAY which has this
5683 // type.
5684
5685 tree
5686 Array_type::capacity_tree(Gogo* gogo, tree array)
5687 {
5688 if (this->length_ != NULL)
5689 return omit_one_operand(integer_type_node, this->get_length_tree(gogo),
5690 array);
5691
5692 // This is an open array. We need to read the capacity field.
5693
5694 tree type = TREE_TYPE(array);
5695 go_assert(TREE_CODE(type) == RECORD_TYPE);
5696
5697 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
5698 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
5699
5700 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5701 }
5702
5703 // Export.
5704
5705 void
5706 Array_type::do_export(Export* exp) const
5707 {
5708 exp->write_c_string("[");
5709 if (this->length_ != NULL)
5710 this->length_->export_expression(exp);
5711 exp->write_c_string("] ");
5712 exp->write_type(this->element_type_);
5713 }
5714
5715 // Import.
5716
5717 Array_type*
5718 Array_type::do_import(Import* imp)
5719 {
5720 imp->require_c_string("[");
5721 Expression* length;
5722 if (imp->peek_char() == ']')
5723 length = NULL;
5724 else
5725 length = Expression::import_expression(imp);
5726 imp->require_c_string("] ");
5727 Type* element_type = imp->read_type();
5728 return Type::make_array_type(element_type, length);
5729 }
5730
5731 // The type of an array type descriptor.
5732
5733 Type*
5734 Array_type::make_array_type_descriptor_type()
5735 {
5736 static Type* ret;
5737 if (ret == NULL)
5738 {
5739 Type* tdt = Type::make_type_descriptor_type();
5740 Type* ptdt = Type::make_type_descriptor_ptr_type();
5741
5742 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5743
5744 Struct_type* sf =
5745 Type::make_builtin_struct_type(4,
5746 "", tdt,
5747 "elem", ptdt,
5748 "slice", ptdt,
5749 "len", uintptr_type);
5750
5751 ret = Type::make_builtin_named_type("ArrayType", sf);
5752 }
5753
5754 return ret;
5755 }
5756
5757 // The type of an slice type descriptor.
5758
5759 Type*
5760 Array_type::make_slice_type_descriptor_type()
5761 {
5762 static Type* ret;
5763 if (ret == NULL)
5764 {
5765 Type* tdt = Type::make_type_descriptor_type();
5766 Type* ptdt = Type::make_type_descriptor_ptr_type();
5767
5768 Struct_type* sf =
5769 Type::make_builtin_struct_type(2,
5770 "", tdt,
5771 "elem", ptdt);
5772
5773 ret = Type::make_builtin_named_type("SliceType", sf);
5774 }
5775
5776 return ret;
5777 }
5778
5779 // Build a type descriptor for an array/slice type.
5780
5781 Expression*
5782 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5783 {
5784 if (this->length_ != NULL)
5785 return this->array_type_descriptor(gogo, name);
5786 else
5787 return this->slice_type_descriptor(gogo, name);
5788 }
5789
5790 // Build a type descriptor for an array type.
5791
5792 Expression*
5793 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
5794 {
5795 Location bloc = Linemap::predeclared_location();
5796
5797 Type* atdt = Array_type::make_array_type_descriptor_type();
5798
5799 const Struct_field_list* fields = atdt->struct_type()->fields();
5800
5801 Expression_list* vals = new Expression_list();
5802 vals->reserve(3);
5803
5804 Struct_field_list::const_iterator p = fields->begin();
5805 go_assert(p->is_field_name("commonType"));
5806 vals->push_back(this->type_descriptor_constructor(gogo,
5807 RUNTIME_TYPE_KIND_ARRAY,
5808 name, NULL, true));
5809
5810 ++p;
5811 go_assert(p->is_field_name("elem"));
5812 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5813
5814 ++p;
5815 go_assert(p->is_field_name("slice"));
5816 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
5817 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
5818
5819 ++p;
5820 go_assert(p->is_field_name("len"));
5821 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
5822
5823 ++p;
5824 go_assert(p == fields->end());
5825
5826 return Expression::make_struct_composite_literal(atdt, vals, bloc);
5827 }
5828
5829 // Build a type descriptor for a slice type.
5830
5831 Expression*
5832 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
5833 {
5834 Location bloc = Linemap::predeclared_location();
5835
5836 Type* stdt = Array_type::make_slice_type_descriptor_type();
5837
5838 const Struct_field_list* fields = stdt->struct_type()->fields();
5839
5840 Expression_list* vals = new Expression_list();
5841 vals->reserve(2);
5842
5843 Struct_field_list::const_iterator p = fields->begin();
5844 go_assert(p->is_field_name("commonType"));
5845 vals->push_back(this->type_descriptor_constructor(gogo,
5846 RUNTIME_TYPE_KIND_SLICE,
5847 name, NULL, true));
5848
5849 ++p;
5850 go_assert(p->is_field_name("elem"));
5851 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5852
5853 ++p;
5854 go_assert(p == fields->end());
5855
5856 return Expression::make_struct_composite_literal(stdt, vals, bloc);
5857 }
5858
5859 // Reflection string.
5860
5861 void
5862 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
5863 {
5864 ret->push_back('[');
5865 if (this->length_ != NULL)
5866 {
5867 Numeric_constant nc;
5868 unsigned long val;
5869 if (!this->length_->numeric_constant_value(&nc)
5870 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
5871 error_at(this->length_->location(), "invalid array length");
5872 else
5873 {
5874 char buf[50];
5875 snprintf(buf, sizeof buf, "%lu", val);
5876 ret->append(buf);
5877 }
5878 }
5879 ret->push_back(']');
5880
5881 this->append_reflection(this->element_type_, gogo, ret);
5882 }
5883
5884 // Mangled name.
5885
5886 void
5887 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5888 {
5889 ret->push_back('A');
5890 this->append_mangled_name(this->element_type_, gogo, ret);
5891 if (this->length_ != NULL)
5892 {
5893 Numeric_constant nc;
5894 unsigned long val;
5895 if (!this->length_->numeric_constant_value(&nc)
5896 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
5897 error_at(this->length_->location(), "invalid array length");
5898 else
5899 {
5900 char buf[50];
5901 snprintf(buf, sizeof buf, "%lu", val);
5902 ret->append(buf);
5903 }
5904 }
5905 ret->push_back('e');
5906 }
5907
5908 // Make an array type.
5909
5910 Array_type*
5911 Type::make_array_type(Type* element_type, Expression* length)
5912 {
5913 return new Array_type(element_type, length);
5914 }
5915
5916 // Class Map_type.
5917
5918 // Traversal.
5919
5920 int
5921 Map_type::do_traverse(Traverse* traverse)
5922 {
5923 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
5924 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
5925 return TRAVERSE_EXIT;
5926 return TRAVERSE_CONTINUE;
5927 }
5928
5929 // Check that the map type is OK.
5930
5931 bool
5932 Map_type::do_verify()
5933 {
5934 // The runtime support uses "map[void]void".
5935 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
5936 error_at(this->location_, "invalid map key type");
5937 return true;
5938 }
5939
5940 // Whether two map types are identical.
5941
5942 bool
5943 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
5944 {
5945 return (Type::are_identical(this->key_type(), t->key_type(),
5946 errors_are_identical, NULL)
5947 && Type::are_identical(this->val_type(), t->val_type(),
5948 errors_are_identical, NULL));
5949 }
5950
5951 // Hash code.
5952
5953 unsigned int
5954 Map_type::do_hash_for_method(Gogo* gogo) const
5955 {
5956 return (this->key_type_->hash_for_method(gogo)
5957 + this->val_type_->hash_for_method(gogo)
5958 + 2);
5959 }
5960
5961 // Get the backend representation for a map type. A map type is
5962 // represented as a pointer to a struct. The struct is __go_map in
5963 // libgo/map.h.
5964
5965 Btype*
5966 Map_type::do_get_backend(Gogo* gogo)
5967 {
5968 static Btype* backend_map_type;
5969 if (backend_map_type == NULL)
5970 {
5971 std::vector<Backend::Btyped_identifier> bfields(4);
5972
5973 Location bloc = Linemap::predeclared_location();
5974
5975 Type* pdt = Type::make_type_descriptor_ptr_type();
5976 bfields[0].name = "__descriptor";
5977 bfields[0].btype = pdt->get_backend(gogo);
5978 bfields[0].location = bloc;
5979
5980 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5981 bfields[1].name = "__element_count";
5982 bfields[1].btype = uintptr_type->get_backend(gogo);
5983 bfields[1].location = bloc;
5984
5985 bfields[2].name = "__bucket_count";
5986 bfields[2].btype = bfields[1].btype;
5987 bfields[2].location = bloc;
5988
5989 Btype* bvt = gogo->backend()->void_type();
5990 Btype* bpvt = gogo->backend()->pointer_type(bvt);
5991 Btype* bppvt = gogo->backend()->pointer_type(bpvt);
5992 bfields[3].name = "__buckets";
5993 bfields[3].btype = bppvt;
5994 bfields[3].location = bloc;
5995
5996 Btype *bt = gogo->backend()->struct_type(bfields);
5997 bt = gogo->backend()->named_type("__go_map", bt, bloc);
5998 backend_map_type = gogo->backend()->pointer_type(bt);
5999 }
6000 return backend_map_type;
6001 }
6002
6003 // The type of a map type descriptor.
6004
6005 Type*
6006 Map_type::make_map_type_descriptor_type()
6007 {
6008 static Type* ret;
6009 if (ret == NULL)
6010 {
6011 Type* tdt = Type::make_type_descriptor_type();
6012 Type* ptdt = Type::make_type_descriptor_ptr_type();
6013
6014 Struct_type* sf =
6015 Type::make_builtin_struct_type(3,
6016 "", tdt,
6017 "key", ptdt,
6018 "elem", ptdt);
6019
6020 ret = Type::make_builtin_named_type("MapType", sf);
6021 }
6022
6023 return ret;
6024 }
6025
6026 // Build a type descriptor for a map type.
6027
6028 Expression*
6029 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6030 {
6031 Location bloc = Linemap::predeclared_location();
6032
6033 Type* mtdt = Map_type::make_map_type_descriptor_type();
6034
6035 const Struct_field_list* fields = mtdt->struct_type()->fields();
6036
6037 Expression_list* vals = new Expression_list();
6038 vals->reserve(3);
6039
6040 Struct_field_list::const_iterator p = fields->begin();
6041 go_assert(p->is_field_name("commonType"));
6042 vals->push_back(this->type_descriptor_constructor(gogo,
6043 RUNTIME_TYPE_KIND_MAP,
6044 name, NULL, true));
6045
6046 ++p;
6047 go_assert(p->is_field_name("key"));
6048 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
6049
6050 ++p;
6051 go_assert(p->is_field_name("elem"));
6052 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
6053
6054 ++p;
6055 go_assert(p == fields->end());
6056
6057 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
6058 }
6059
6060 // A mapping from map types to map descriptors.
6061
6062 Map_type::Map_descriptors Map_type::map_descriptors;
6063
6064 // Build a map descriptor for this type. Return a pointer to it.
6065
6066 tree
6067 Map_type::map_descriptor_pointer(Gogo* gogo, Location location)
6068 {
6069 Bvariable* bvar = this->map_descriptor(gogo);
6070 tree var_tree = var_to_tree(bvar);
6071 if (var_tree == error_mark_node)
6072 return error_mark_node;
6073 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
6074 }
6075
6076 // Build a map descriptor for this type.
6077
6078 Bvariable*
6079 Map_type::map_descriptor(Gogo* gogo)
6080 {
6081 std::pair<Map_type*, Bvariable*> val(this, NULL);
6082 std::pair<Map_type::Map_descriptors::iterator, bool> ins =
6083 Map_type::map_descriptors.insert(val);
6084 if (!ins.second)
6085 return ins.first->second;
6086
6087 Type* key_type = this->key_type_;
6088 Type* val_type = this->val_type_;
6089
6090 // The map entry type is a struct with three fields. Build that
6091 // struct so that we can get the offsets of the key and value within
6092 // a map entry. The first field should technically be a pointer to
6093 // this type itself, but since we only care about field offsets we
6094 // just use pointer to bool.
6095 Type* pbool = Type::make_pointer_type(Type::make_boolean_type());
6096 Struct_type* map_entry_type =
6097 Type::make_builtin_struct_type(3,
6098 "__next", pbool,
6099 "__key", key_type,
6100 "__val", val_type);
6101
6102 Type* map_descriptor_type = Map_type::make_map_descriptor_type();
6103
6104 const Struct_field_list* fields =
6105 map_descriptor_type->struct_type()->fields();
6106
6107 Expression_list* vals = new Expression_list();
6108 vals->reserve(4);
6109
6110 Location bloc = Linemap::predeclared_location();
6111
6112 Struct_field_list::const_iterator p = fields->begin();
6113
6114 go_assert(p->is_field_name("__map_descriptor"));
6115 vals->push_back(Expression::make_type_descriptor(this, bloc));
6116
6117 ++p;
6118 go_assert(p->is_field_name("__entry_size"));
6119 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
6120 vals->push_back(Expression::make_type_info(map_entry_type, type_info));
6121
6122 Struct_field_list::const_iterator pf = map_entry_type->fields()->begin();
6123 ++pf;
6124 go_assert(pf->is_field_name("__key"));
6125
6126 ++p;
6127 go_assert(p->is_field_name("__key_offset"));
6128 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6129
6130 ++pf;
6131 go_assert(pf->is_field_name("__val"));
6132
6133 ++p;
6134 go_assert(p->is_field_name("__val_offset"));
6135 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6136
6137 ++p;
6138 go_assert(p == fields->end());
6139
6140 Expression* initializer =
6141 Expression::make_struct_composite_literal(map_descriptor_type, vals, bloc);
6142
6143 std::string mangled_name = "__go_map_" + this->mangled_name(gogo);
6144 Btype* map_descriptor_btype = map_descriptor_type->get_backend(gogo);
6145 Bvariable* bvar = gogo->backend()->immutable_struct(mangled_name, true,
6146 map_descriptor_btype,
6147 bloc);
6148
6149 Translate_context context(gogo, NULL, NULL, NULL);
6150 context.set_is_const();
6151 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
6152
6153 gogo->backend()->immutable_struct_set_init(bvar, mangled_name, true,
6154 map_descriptor_btype, bloc,
6155 binitializer);
6156
6157 ins.first->second = bvar;
6158 return bvar;
6159 }
6160
6161 // Build the type of a map descriptor. This must match the struct
6162 // __go_map_descriptor in libgo/runtime/map.h.
6163
6164 Type*
6165 Map_type::make_map_descriptor_type()
6166 {
6167 static Type* ret;
6168 if (ret == NULL)
6169 {
6170 Type* ptdt = Type::make_type_descriptor_ptr_type();
6171 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6172 Struct_type* sf =
6173 Type::make_builtin_struct_type(4,
6174 "__map_descriptor", ptdt,
6175 "__entry_size", uintptr_type,
6176 "__key_offset", uintptr_type,
6177 "__val_offset", uintptr_type);
6178 ret = Type::make_builtin_named_type("__go_map_descriptor", sf);
6179 }
6180 return ret;
6181 }
6182
6183 // Reflection string for a map.
6184
6185 void
6186 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
6187 {
6188 ret->append("map[");
6189 this->append_reflection(this->key_type_, gogo, ret);
6190 ret->append("]");
6191 this->append_reflection(this->val_type_, gogo, ret);
6192 }
6193
6194 // Mangled name for a map.
6195
6196 void
6197 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6198 {
6199 ret->push_back('M');
6200 this->append_mangled_name(this->key_type_, gogo, ret);
6201 ret->append("__");
6202 this->append_mangled_name(this->val_type_, gogo, ret);
6203 }
6204
6205 // Export a map type.
6206
6207 void
6208 Map_type::do_export(Export* exp) const
6209 {
6210 exp->write_c_string("map [");
6211 exp->write_type(this->key_type_);
6212 exp->write_c_string("] ");
6213 exp->write_type(this->val_type_);
6214 }
6215
6216 // Import a map type.
6217
6218 Map_type*
6219 Map_type::do_import(Import* imp)
6220 {
6221 imp->require_c_string("map [");
6222 Type* key_type = imp->read_type();
6223 imp->require_c_string("] ");
6224 Type* val_type = imp->read_type();
6225 return Type::make_map_type(key_type, val_type, imp->location());
6226 }
6227
6228 // Make a map type.
6229
6230 Map_type*
6231 Type::make_map_type(Type* key_type, Type* val_type, Location location)
6232 {
6233 return new Map_type(key_type, val_type, location);
6234 }
6235
6236 // Class Channel_type.
6237
6238 // Hash code.
6239
6240 unsigned int
6241 Channel_type::do_hash_for_method(Gogo* gogo) const
6242 {
6243 unsigned int ret = 0;
6244 if (this->may_send_)
6245 ret += 1;
6246 if (this->may_receive_)
6247 ret += 2;
6248 if (this->element_type_ != NULL)
6249 ret += this->element_type_->hash_for_method(gogo) << 2;
6250 return ret << 3;
6251 }
6252
6253 // Whether this type is the same as T.
6254
6255 bool
6256 Channel_type::is_identical(const Channel_type* t,
6257 bool errors_are_identical) const
6258 {
6259 if (!Type::are_identical(this->element_type(), t->element_type(),
6260 errors_are_identical, NULL))
6261 return false;
6262 return (this->may_send_ == t->may_send_
6263 && this->may_receive_ == t->may_receive_);
6264 }
6265
6266 // Return the tree for a channel type. A channel is a pointer to a
6267 // __go_channel struct. The __go_channel struct is defined in
6268 // libgo/runtime/channel.h.
6269
6270 Btype*
6271 Channel_type::do_get_backend(Gogo* gogo)
6272 {
6273 static Btype* backend_channel_type;
6274 if (backend_channel_type == NULL)
6275 {
6276 std::vector<Backend::Btyped_identifier> bfields;
6277 Btype* bt = gogo->backend()->struct_type(bfields);
6278 bt = gogo->backend()->named_type("__go_channel", bt,
6279 Linemap::predeclared_location());
6280 backend_channel_type = gogo->backend()->pointer_type(bt);
6281 }
6282 return backend_channel_type;
6283 }
6284
6285 // Build a type descriptor for a channel type.
6286
6287 Type*
6288 Channel_type::make_chan_type_descriptor_type()
6289 {
6290 static Type* ret;
6291 if (ret == NULL)
6292 {
6293 Type* tdt = Type::make_type_descriptor_type();
6294 Type* ptdt = Type::make_type_descriptor_ptr_type();
6295
6296 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6297
6298 Struct_type* sf =
6299 Type::make_builtin_struct_type(3,
6300 "", tdt,
6301 "elem", ptdt,
6302 "dir", uintptr_type);
6303
6304 ret = Type::make_builtin_named_type("ChanType", sf);
6305 }
6306
6307 return ret;
6308 }
6309
6310 // Build a type descriptor for a map type.
6311
6312 Expression*
6313 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6314 {
6315 Location bloc = Linemap::predeclared_location();
6316
6317 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
6318
6319 const Struct_field_list* fields = ctdt->struct_type()->fields();
6320
6321 Expression_list* vals = new Expression_list();
6322 vals->reserve(3);
6323
6324 Struct_field_list::const_iterator p = fields->begin();
6325 go_assert(p->is_field_name("commonType"));
6326 vals->push_back(this->type_descriptor_constructor(gogo,
6327 RUNTIME_TYPE_KIND_CHAN,
6328 name, NULL, true));
6329
6330 ++p;
6331 go_assert(p->is_field_name("elem"));
6332 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6333
6334 ++p;
6335 go_assert(p->is_field_name("dir"));
6336 // These bits must match the ones in libgo/runtime/go-type.h.
6337 int val = 0;
6338 if (this->may_receive_)
6339 val |= 1;
6340 if (this->may_send_)
6341 val |= 2;
6342 mpz_t iv;
6343 mpz_init_set_ui(iv, val);
6344 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
6345 mpz_clear(iv);
6346
6347 ++p;
6348 go_assert(p == fields->end());
6349
6350 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
6351 }
6352
6353 // Reflection string.
6354
6355 void
6356 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
6357 {
6358 if (!this->may_send_)
6359 ret->append("<-");
6360 ret->append("chan");
6361 if (!this->may_receive_)
6362 ret->append("<-");
6363 ret->push_back(' ');
6364 this->append_reflection(this->element_type_, gogo, ret);
6365 }
6366
6367 // Mangled name.
6368
6369 void
6370 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6371 {
6372 ret->push_back('C');
6373 this->append_mangled_name(this->element_type_, gogo, ret);
6374 if (this->may_send_)
6375 ret->push_back('s');
6376 if (this->may_receive_)
6377 ret->push_back('r');
6378 ret->push_back('e');
6379 }
6380
6381 // Export.
6382
6383 void
6384 Channel_type::do_export(Export* exp) const
6385 {
6386 exp->write_c_string("chan ");
6387 if (this->may_send_ && !this->may_receive_)
6388 exp->write_c_string("-< ");
6389 else if (this->may_receive_ && !this->may_send_)
6390 exp->write_c_string("<- ");
6391 exp->write_type(this->element_type_);
6392 }
6393
6394 // Import.
6395
6396 Channel_type*
6397 Channel_type::do_import(Import* imp)
6398 {
6399 imp->require_c_string("chan ");
6400
6401 bool may_send;
6402 bool may_receive;
6403 if (imp->match_c_string("-< "))
6404 {
6405 imp->advance(3);
6406 may_send = true;
6407 may_receive = false;
6408 }
6409 else if (imp->match_c_string("<- "))
6410 {
6411 imp->advance(3);
6412 may_receive = true;
6413 may_send = false;
6414 }
6415 else
6416 {
6417 may_send = true;
6418 may_receive = true;
6419 }
6420
6421 Type* element_type = imp->read_type();
6422
6423 return Type::make_channel_type(may_send, may_receive, element_type);
6424 }
6425
6426 // Make a new channel type.
6427
6428 Channel_type*
6429 Type::make_channel_type(bool send, bool receive, Type* element_type)
6430 {
6431 return new Channel_type(send, receive, element_type);
6432 }
6433
6434 // Class Interface_type.
6435
6436 // Traversal.
6437
6438 int
6439 Interface_type::do_traverse(Traverse* traverse)
6440 {
6441 Typed_identifier_list* methods = (this->methods_are_finalized_
6442 ? this->all_methods_
6443 : this->parse_methods_);
6444 if (methods == NULL)
6445 return TRAVERSE_CONTINUE;
6446 return methods->traverse(traverse);
6447 }
6448
6449 // Finalize the methods. This handles interface inheritance.
6450
6451 void
6452 Interface_type::finalize_methods()
6453 {
6454 if (this->methods_are_finalized_)
6455 return;
6456 this->methods_are_finalized_ = true;
6457 if (this->parse_methods_ == NULL)
6458 return;
6459
6460 this->all_methods_ = new Typed_identifier_list();
6461 this->all_methods_->reserve(this->parse_methods_->size());
6462 Typed_identifier_list inherit;
6463 for (Typed_identifier_list::const_iterator pm =
6464 this->parse_methods_->begin();
6465 pm != this->parse_methods_->end();
6466 ++pm)
6467 {
6468 const Typed_identifier* p = &*pm;
6469 if (p->name().empty())
6470 inherit.push_back(*p);
6471 else if (this->find_method(p->name()) == NULL)
6472 this->all_methods_->push_back(*p);
6473 else
6474 error_at(p->location(), "duplicate method %qs",
6475 Gogo::message_name(p->name()).c_str());
6476 }
6477
6478 std::vector<Named_type*> seen;
6479 seen.reserve(inherit.size());
6480 bool issued_recursive_error = false;
6481 while (!inherit.empty())
6482 {
6483 Type* t = inherit.back().type();
6484 Location tl = inherit.back().location();
6485 inherit.pop_back();
6486
6487 Interface_type* it = t->interface_type();
6488 if (it == NULL)
6489 {
6490 if (!t->is_error())
6491 error_at(tl, "interface contains embedded non-interface");
6492 continue;
6493 }
6494 if (it == this)
6495 {
6496 if (!issued_recursive_error)
6497 {
6498 error_at(tl, "invalid recursive interface");
6499 issued_recursive_error = true;
6500 }
6501 continue;
6502 }
6503
6504 Named_type* nt = t->named_type();
6505 if (nt != NULL && it->parse_methods_ != NULL)
6506 {
6507 std::vector<Named_type*>::const_iterator q;
6508 for (q = seen.begin(); q != seen.end(); ++q)
6509 {
6510 if (*q == nt)
6511 {
6512 error_at(tl, "inherited interface loop");
6513 break;
6514 }
6515 }
6516 if (q != seen.end())
6517 continue;
6518 seen.push_back(nt);
6519 }
6520
6521 const Typed_identifier_list* imethods = it->parse_methods_;
6522 if (imethods == NULL)
6523 continue;
6524 for (Typed_identifier_list::const_iterator q = imethods->begin();
6525 q != imethods->end();
6526 ++q)
6527 {
6528 if (q->name().empty())
6529 inherit.push_back(*q);
6530 else if (this->find_method(q->name()) == NULL)
6531 this->all_methods_->push_back(Typed_identifier(q->name(),
6532 q->type(), tl));
6533 else
6534 error_at(tl, "inherited method %qs is ambiguous",
6535 Gogo::message_name(q->name()).c_str());
6536 }
6537 }
6538
6539 if (!this->all_methods_->empty())
6540 this->all_methods_->sort_by_name();
6541 else
6542 {
6543 delete this->all_methods_;
6544 this->all_methods_ = NULL;
6545 }
6546 }
6547
6548 // Return the method NAME, or NULL.
6549
6550 const Typed_identifier*
6551 Interface_type::find_method(const std::string& name) const
6552 {
6553 go_assert(this->methods_are_finalized_);
6554 if (this->all_methods_ == NULL)
6555 return NULL;
6556 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6557 p != this->all_methods_->end();
6558 ++p)
6559 if (p->name() == name)
6560 return &*p;
6561 return NULL;
6562 }
6563
6564 // Return the method index.
6565
6566 size_t
6567 Interface_type::method_index(const std::string& name) const
6568 {
6569 go_assert(this->methods_are_finalized_ && this->all_methods_ != NULL);
6570 size_t ret = 0;
6571 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6572 p != this->all_methods_->end();
6573 ++p, ++ret)
6574 if (p->name() == name)
6575 return ret;
6576 go_unreachable();
6577 }
6578
6579 // Return whether NAME is an unexported method, for better error
6580 // reporting.
6581
6582 bool
6583 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
6584 {
6585 go_assert(this->methods_are_finalized_);
6586 if (this->all_methods_ == NULL)
6587 return false;
6588 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6589 p != this->all_methods_->end();
6590 ++p)
6591 {
6592 const std::string& method_name(p->name());
6593 if (Gogo::is_hidden_name(method_name)
6594 && name == Gogo::unpack_hidden_name(method_name)
6595 && gogo->pack_hidden_name(name, false) != method_name)
6596 return true;
6597 }
6598 return false;
6599 }
6600
6601 // Whether this type is identical with T.
6602
6603 bool
6604 Interface_type::is_identical(const Interface_type* t,
6605 bool errors_are_identical) const
6606 {
6607 // If methods have not been finalized, then we are asking whether
6608 // func redeclarations are the same. This is an error, so for
6609 // simplicity we say they are never the same.
6610 if (!this->methods_are_finalized_ || !t->methods_are_finalized_)
6611 return false;
6612
6613 // We require the same methods with the same types. The methods
6614 // have already been sorted.
6615 if (this->all_methods_ == NULL || t->all_methods_ == NULL)
6616 return this->all_methods_ == t->all_methods_;
6617
6618 if (this->assume_identical(this, t) || t->assume_identical(t, this))
6619 return true;
6620
6621 Assume_identical* hold_ai = this->assume_identical_;
6622 Assume_identical ai;
6623 ai.t1 = this;
6624 ai.t2 = t;
6625 ai.next = hold_ai;
6626 this->assume_identical_ = &ai;
6627
6628 Typed_identifier_list::const_iterator p1 = this->all_methods_->begin();
6629 Typed_identifier_list::const_iterator p2;
6630 for (p2 = t->all_methods_->begin(); p2 != t->all_methods_->end(); ++p1, ++p2)
6631 {
6632 if (p1 == this->all_methods_->end())
6633 break;
6634 if (p1->name() != p2->name()
6635 || !Type::are_identical(p1->type(), p2->type(),
6636 errors_are_identical, NULL))
6637 break;
6638 }
6639
6640 this->assume_identical_ = hold_ai;
6641
6642 return p1 == this->all_methods_->end() && p2 == t->all_methods_->end();
6643 }
6644
6645 // Return true if T1 and T2 are assumed to be identical during a type
6646 // comparison.
6647
6648 bool
6649 Interface_type::assume_identical(const Interface_type* t1,
6650 const Interface_type* t2) const
6651 {
6652 for (Assume_identical* p = this->assume_identical_;
6653 p != NULL;
6654 p = p->next)
6655 if ((p->t1 == t1 && p->t2 == t2) || (p->t1 == t2 && p->t2 == t1))
6656 return true;
6657 return false;
6658 }
6659
6660 // Whether we can assign the interface type T to this type. The types
6661 // are known to not be identical. An interface assignment is only
6662 // permitted if T is known to implement all methods in THIS.
6663 // Otherwise a type guard is required.
6664
6665 bool
6666 Interface_type::is_compatible_for_assign(const Interface_type* t,
6667 std::string* reason) const
6668 {
6669 go_assert(this->methods_are_finalized_ && t->methods_are_finalized_);
6670 if (this->all_methods_ == NULL)
6671 return true;
6672 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6673 p != this->all_methods_->end();
6674 ++p)
6675 {
6676 const Typed_identifier* m = t->find_method(p->name());
6677 if (m == NULL)
6678 {
6679 if (reason != NULL)
6680 {
6681 char buf[200];
6682 snprintf(buf, sizeof buf,
6683 _("need explicit conversion; missing method %s%s%s"),
6684 open_quote, Gogo::message_name(p->name()).c_str(),
6685 close_quote);
6686 reason->assign(buf);
6687 }
6688 return false;
6689 }
6690
6691 std::string subreason;
6692 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
6693 {
6694 if (reason != NULL)
6695 {
6696 std::string n = Gogo::message_name(p->name());
6697 size_t len = 100 + n.length() + subreason.length();
6698 char* buf = new char[len];
6699 if (subreason.empty())
6700 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6701 open_quote, n.c_str(), close_quote);
6702 else
6703 snprintf(buf, len,
6704 _("incompatible type for method %s%s%s (%s)"),
6705 open_quote, n.c_str(), close_quote,
6706 subreason.c_str());
6707 reason->assign(buf);
6708 delete[] buf;
6709 }
6710 return false;
6711 }
6712 }
6713
6714 return true;
6715 }
6716
6717 // Hash code.
6718
6719 unsigned int
6720 Interface_type::do_hash_for_method(Gogo*) const
6721 {
6722 go_assert(this->methods_are_finalized_);
6723 unsigned int ret = 0;
6724 if (this->all_methods_ != NULL)
6725 {
6726 for (Typed_identifier_list::const_iterator p =
6727 this->all_methods_->begin();
6728 p != this->all_methods_->end();
6729 ++p)
6730 {
6731 ret = Type::hash_string(p->name(), ret);
6732 // We don't use the method type in the hash, to avoid
6733 // infinite recursion if an interface method uses a type
6734 // which is an interface which inherits from the interface
6735 // itself.
6736 // type T interface { F() interface {T}}
6737 ret <<= 1;
6738 }
6739 }
6740 return ret;
6741 }
6742
6743 // Return true if T implements the interface. If it does not, and
6744 // REASON is not NULL, set *REASON to a useful error message.
6745
6746 bool
6747 Interface_type::implements_interface(const Type* t, std::string* reason) const
6748 {
6749 go_assert(this->methods_are_finalized_);
6750 if (this->all_methods_ == NULL)
6751 return true;
6752
6753 bool is_pointer = false;
6754 const Named_type* nt = t->named_type();
6755 const Struct_type* st = t->struct_type();
6756 // If we start with a named type, we don't dereference it to find
6757 // methods.
6758 if (nt == NULL)
6759 {
6760 const Type* pt = t->points_to();
6761 if (pt != NULL)
6762 {
6763 // If T is a pointer to a named type, then we need to look at
6764 // the type to which it points.
6765 is_pointer = true;
6766 nt = pt->named_type();
6767 st = pt->struct_type();
6768 }
6769 }
6770
6771 // If we have a named type, get the methods from it rather than from
6772 // any struct type.
6773 if (nt != NULL)
6774 st = NULL;
6775
6776 // Only named and struct types have methods.
6777 if (nt == NULL && st == NULL)
6778 {
6779 if (reason != NULL)
6780 {
6781 if (t->points_to() != NULL
6782 && t->points_to()->interface_type() != NULL)
6783 reason->assign(_("pointer to interface type has no methods"));
6784 else
6785 reason->assign(_("type has no methods"));
6786 }
6787 return false;
6788 }
6789
6790 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
6791 {
6792 if (reason != NULL)
6793 {
6794 if (t->points_to() != NULL
6795 && t->points_to()->interface_type() != NULL)
6796 reason->assign(_("pointer to interface type has no methods"));
6797 else
6798 reason->assign(_("type has no methods"));
6799 }
6800 return false;
6801 }
6802
6803 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6804 p != this->all_methods_->end();
6805 ++p)
6806 {
6807 bool is_ambiguous = false;
6808 Method* m = (nt != NULL
6809 ? nt->method_function(p->name(), &is_ambiguous)
6810 : st->method_function(p->name(), &is_ambiguous));
6811 if (m == NULL)
6812 {
6813 if (reason != NULL)
6814 {
6815 std::string n = Gogo::message_name(p->name());
6816 size_t len = n.length() + 100;
6817 char* buf = new char[len];
6818 if (is_ambiguous)
6819 snprintf(buf, len, _("ambiguous method %s%s%s"),
6820 open_quote, n.c_str(), close_quote);
6821 else
6822 snprintf(buf, len, _("missing method %s%s%s"),
6823 open_quote, n.c_str(), close_quote);
6824 reason->assign(buf);
6825 delete[] buf;
6826 }
6827 return false;
6828 }
6829
6830 Function_type *p_fn_type = p->type()->function_type();
6831 Function_type* m_fn_type = m->type()->function_type();
6832 go_assert(p_fn_type != NULL && m_fn_type != NULL);
6833 std::string subreason;
6834 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
6835 {
6836 if (reason != NULL)
6837 {
6838 std::string n = Gogo::message_name(p->name());
6839 size_t len = 100 + n.length() + subreason.length();
6840 char* buf = new char[len];
6841 if (subreason.empty())
6842 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6843 open_quote, n.c_str(), close_quote);
6844 else
6845 snprintf(buf, len,
6846 _("incompatible type for method %s%s%s (%s)"),
6847 open_quote, n.c_str(), close_quote,
6848 subreason.c_str());
6849 reason->assign(buf);
6850 delete[] buf;
6851 }
6852 return false;
6853 }
6854
6855 if (!is_pointer && !m->is_value_method())
6856 {
6857 if (reason != NULL)
6858 {
6859 std::string n = Gogo::message_name(p->name());
6860 size_t len = 100 + n.length();
6861 char* buf = new char[len];
6862 snprintf(buf, len,
6863 _("method %s%s%s requires a pointer receiver"),
6864 open_quote, n.c_str(), close_quote);
6865 reason->assign(buf);
6866 delete[] buf;
6867 }
6868 return false;
6869 }
6870 }
6871
6872 return true;
6873 }
6874
6875 // Return the backend representation of the empty interface type. We
6876 // use the same struct for all empty interfaces.
6877
6878 Btype*
6879 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
6880 {
6881 static Btype* empty_interface_type;
6882 if (empty_interface_type == NULL)
6883 {
6884 std::vector<Backend::Btyped_identifier> bfields(2);
6885
6886 Location bloc = Linemap::predeclared_location();
6887
6888 Type* pdt = Type::make_type_descriptor_ptr_type();
6889 bfields[0].name = "__type_descriptor";
6890 bfields[0].btype = pdt->get_backend(gogo);
6891 bfields[0].location = bloc;
6892
6893 Type* vt = Type::make_pointer_type(Type::make_void_type());
6894 bfields[1].name = "__object";
6895 bfields[1].btype = vt->get_backend(gogo);
6896 bfields[1].location = bloc;
6897
6898 empty_interface_type = gogo->backend()->struct_type(bfields);
6899 }
6900 return empty_interface_type;
6901 }
6902
6903 // Return the fields of a non-empty interface type. This is not
6904 // declared in types.h so that types.h doesn't have to #include
6905 // backend.h.
6906
6907 static void
6908 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
6909 bool use_placeholder,
6910 std::vector<Backend::Btyped_identifier>* bfields)
6911 {
6912 Location loc = type->location();
6913
6914 std::vector<Backend::Btyped_identifier> mfields(type->methods()->size() + 1);
6915
6916 Type* pdt = Type::make_type_descriptor_ptr_type();
6917 mfields[0].name = "__type_descriptor";
6918 mfields[0].btype = pdt->get_backend(gogo);
6919 mfields[0].location = loc;
6920
6921 std::string last_name = "";
6922 size_t i = 1;
6923 for (Typed_identifier_list::const_iterator p = type->methods()->begin();
6924 p != type->methods()->end();
6925 ++p, ++i)
6926 {
6927 // The type of the method in Go only includes the parameters.
6928 // The actual method also has a receiver, which is always a
6929 // pointer. We need to add that pointer type here in order to
6930 // generate the correct type for the backend.
6931 Function_type* ft = p->type()->function_type();
6932 go_assert(ft->receiver() == NULL);
6933
6934 const Typed_identifier_list* params = ft->parameters();
6935 Typed_identifier_list* mparams = new Typed_identifier_list();
6936 if (params != NULL)
6937 mparams->reserve(params->size() + 1);
6938 Type* vt = Type::make_pointer_type(Type::make_void_type());
6939 mparams->push_back(Typed_identifier("", vt, ft->location()));
6940 if (params != NULL)
6941 {
6942 for (Typed_identifier_list::const_iterator pp = params->begin();
6943 pp != params->end();
6944 ++pp)
6945 mparams->push_back(*pp);
6946 }
6947
6948 Typed_identifier_list* mresults = (ft->results() == NULL
6949 ? NULL
6950 : ft->results()->copy());
6951 Function_type* mft = Type::make_function_type(NULL, mparams, mresults,
6952 ft->location());
6953
6954 mfields[i].name = Gogo::unpack_hidden_name(p->name());
6955 mfields[i].btype = (use_placeholder
6956 ? mft->get_backend_placeholder(gogo)
6957 : mft->get_backend(gogo));
6958 mfields[i].location = loc;
6959 // Sanity check: the names should be sorted.
6960 go_assert(p->name() > last_name);
6961 last_name = p->name();
6962 }
6963
6964 Btype* methods = gogo->backend()->struct_type(mfields);
6965
6966 bfields->resize(2);
6967
6968 (*bfields)[0].name = "__methods";
6969 (*bfields)[0].btype = gogo->backend()->pointer_type(methods);
6970 (*bfields)[0].location = loc;
6971
6972 Type* vt = Type::make_pointer_type(Type::make_void_type());
6973 (*bfields)[1].name = "__object";
6974 (*bfields)[1].btype = vt->get_backend(gogo);
6975 (*bfields)[1].location = Linemap::predeclared_location();
6976 }
6977
6978 // Return a tree for an interface type. An interface is a pointer to
6979 // a struct. The struct has three fields. The first field is a
6980 // pointer to the type descriptor for the dynamic type of the object.
6981 // The second field is a pointer to a table of methods for the
6982 // interface to be used with the object. The third field is the value
6983 // of the object itself.
6984
6985 Btype*
6986 Interface_type::do_get_backend(Gogo* gogo)
6987 {
6988 if (this->is_empty())
6989 return Interface_type::get_backend_empty_interface_type(gogo);
6990 else
6991 {
6992 if (this->interface_btype_ != NULL)
6993 return this->interface_btype_;
6994 this->interface_btype_ =
6995 gogo->backend()->placeholder_struct_type("", this->location_);
6996 std::vector<Backend::Btyped_identifier> bfields;
6997 get_backend_interface_fields(gogo, this, false, &bfields);
6998 if (!gogo->backend()->set_placeholder_struct_type(this->interface_btype_,
6999 bfields))
7000 this->interface_btype_ = gogo->backend()->error_type();
7001 return this->interface_btype_;
7002 }
7003 }
7004
7005 // Finish the backend representation of the methods.
7006
7007 void
7008 Interface_type::finish_backend_methods(Gogo* gogo)
7009 {
7010 if (!this->interface_type()->is_empty())
7011 {
7012 const Typed_identifier_list* methods = this->methods();
7013 if (methods != NULL)
7014 {
7015 for (Typed_identifier_list::const_iterator p = methods->begin();
7016 p != methods->end();
7017 ++p)
7018 p->type()->get_backend(gogo);
7019 }
7020 }
7021 }
7022
7023 // The type of an interface type descriptor.
7024
7025 Type*
7026 Interface_type::make_interface_type_descriptor_type()
7027 {
7028 static Type* ret;
7029 if (ret == NULL)
7030 {
7031 Type* tdt = Type::make_type_descriptor_type();
7032 Type* ptdt = Type::make_type_descriptor_ptr_type();
7033
7034 Type* string_type = Type::lookup_string_type();
7035 Type* pointer_string_type = Type::make_pointer_type(string_type);
7036
7037 Struct_type* sm =
7038 Type::make_builtin_struct_type(3,
7039 "name", pointer_string_type,
7040 "pkgPath", pointer_string_type,
7041 "typ", ptdt);
7042
7043 Type* nsm = Type::make_builtin_named_type("imethod", sm);
7044
7045 Type* slice_nsm = Type::make_array_type(nsm, NULL);
7046
7047 Struct_type* s = Type::make_builtin_struct_type(2,
7048 "", tdt,
7049 "methods", slice_nsm);
7050
7051 ret = Type::make_builtin_named_type("InterfaceType", s);
7052 }
7053
7054 return ret;
7055 }
7056
7057 // Build a type descriptor for an interface type.
7058
7059 Expression*
7060 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7061 {
7062 Location bloc = Linemap::predeclared_location();
7063
7064 Type* itdt = Interface_type::make_interface_type_descriptor_type();
7065
7066 const Struct_field_list* ifields = itdt->struct_type()->fields();
7067
7068 Expression_list* ivals = new Expression_list();
7069 ivals->reserve(2);
7070
7071 Struct_field_list::const_iterator pif = ifields->begin();
7072 go_assert(pif->is_field_name("commonType"));
7073 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
7074 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
7075 true));
7076
7077 ++pif;
7078 go_assert(pif->is_field_name("methods"));
7079
7080 Expression_list* methods = new Expression_list();
7081 if (this->all_methods_ != NULL)
7082 {
7083 Type* elemtype = pif->type()->array_type()->element_type();
7084
7085 methods->reserve(this->all_methods_->size());
7086 for (Typed_identifier_list::const_iterator pm =
7087 this->all_methods_->begin();
7088 pm != this->all_methods_->end();
7089 ++pm)
7090 {
7091 const Struct_field_list* mfields = elemtype->struct_type()->fields();
7092
7093 Expression_list* mvals = new Expression_list();
7094 mvals->reserve(3);
7095
7096 Struct_field_list::const_iterator pmf = mfields->begin();
7097 go_assert(pmf->is_field_name("name"));
7098 std::string s = Gogo::unpack_hidden_name(pm->name());
7099 Expression* e = Expression::make_string(s, bloc);
7100 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7101
7102 ++pmf;
7103 go_assert(pmf->is_field_name("pkgPath"));
7104 if (!Gogo::is_hidden_name(pm->name()))
7105 mvals->push_back(Expression::make_nil(bloc));
7106 else
7107 {
7108 s = Gogo::hidden_name_pkgpath(pm->name());
7109 e = Expression::make_string(s, bloc);
7110 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7111 }
7112
7113 ++pmf;
7114 go_assert(pmf->is_field_name("typ"));
7115 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
7116
7117 ++pmf;
7118 go_assert(pmf == mfields->end());
7119
7120 e = Expression::make_struct_composite_literal(elemtype, mvals,
7121 bloc);
7122 methods->push_back(e);
7123 }
7124 }
7125
7126 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
7127 methods, bloc));
7128
7129 ++pif;
7130 go_assert(pif == ifields->end());
7131
7132 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
7133 }
7134
7135 // Reflection string.
7136
7137 void
7138 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
7139 {
7140 ret->append("interface {");
7141 const Typed_identifier_list* methods = this->parse_methods_;
7142 if (methods != NULL)
7143 {
7144 ret->push_back(' ');
7145 for (Typed_identifier_list::const_iterator p = methods->begin();
7146 p != methods->end();
7147 ++p)
7148 {
7149 if (p != methods->begin())
7150 ret->append("; ");
7151 if (p->name().empty())
7152 this->append_reflection(p->type(), gogo, ret);
7153 else
7154 {
7155 if (!Gogo::is_hidden_name(p->name()))
7156 ret->append(p->name());
7157 else if (gogo->pkgpath_from_option())
7158 ret->append(p->name().substr(1));
7159 else
7160 {
7161 // If no -fgo-pkgpath option, backward compatibility
7162 // for how this used to work before -fgo-pkgpath was
7163 // introduced.
7164 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
7165 ret->append(pkgpath.substr(pkgpath.find('.') + 1));
7166 ret->push_back('.');
7167 ret->append(Gogo::unpack_hidden_name(p->name()));
7168 }
7169 std::string sub = p->type()->reflection(gogo);
7170 go_assert(sub.compare(0, 4, "func") == 0);
7171 sub = sub.substr(4);
7172 ret->append(sub);
7173 }
7174 }
7175 ret->push_back(' ');
7176 }
7177 ret->append("}");
7178 }
7179
7180 // Mangled name.
7181
7182 void
7183 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7184 {
7185 go_assert(this->methods_are_finalized_);
7186
7187 ret->push_back('I');
7188
7189 const Typed_identifier_list* methods = this->all_methods_;
7190 if (methods != NULL && !this->seen_)
7191 {
7192 this->seen_ = true;
7193 for (Typed_identifier_list::const_iterator p = methods->begin();
7194 p != methods->end();
7195 ++p)
7196 {
7197 if (!p->name().empty())
7198 {
7199 std::string n;
7200 if (!Gogo::is_hidden_name(p->name()))
7201 n = p->name();
7202 else
7203 {
7204 n = ".";
7205 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
7206 n.append(Gogo::pkgpath_for_symbol(pkgpath));
7207 n.append(1, '.');
7208 n.append(Gogo::unpack_hidden_name(p->name()));
7209 }
7210 char buf[20];
7211 snprintf(buf, sizeof buf, "%u_",
7212 static_cast<unsigned int>(n.length()));
7213 ret->append(buf);
7214 ret->append(n);
7215 }
7216 this->append_mangled_name(p->type(), gogo, ret);
7217 }
7218 this->seen_ = false;
7219 }
7220
7221 ret->push_back('e');
7222 }
7223
7224 // Export.
7225
7226 void
7227 Interface_type::do_export(Export* exp) const
7228 {
7229 exp->write_c_string("interface { ");
7230
7231 const Typed_identifier_list* methods = this->parse_methods_;
7232 if (methods != NULL)
7233 {
7234 for (Typed_identifier_list::const_iterator pm = methods->begin();
7235 pm != methods->end();
7236 ++pm)
7237 {
7238 if (pm->name().empty())
7239 {
7240 exp->write_c_string("? ");
7241 exp->write_type(pm->type());
7242 }
7243 else
7244 {
7245 exp->write_string(pm->name());
7246 exp->write_c_string(" (");
7247
7248 const Function_type* fntype = pm->type()->function_type();
7249
7250 bool first = true;
7251 const Typed_identifier_list* parameters = fntype->parameters();
7252 if (parameters != NULL)
7253 {
7254 bool is_varargs = fntype->is_varargs();
7255 for (Typed_identifier_list::const_iterator pp =
7256 parameters->begin();
7257 pp != parameters->end();
7258 ++pp)
7259 {
7260 if (first)
7261 first = false;
7262 else
7263 exp->write_c_string(", ");
7264 exp->write_name(pp->name());
7265 exp->write_c_string(" ");
7266 if (!is_varargs || pp + 1 != parameters->end())
7267 exp->write_type(pp->type());
7268 else
7269 {
7270 exp->write_c_string("...");
7271 Type *pptype = pp->type();
7272 exp->write_type(pptype->array_type()->element_type());
7273 }
7274 }
7275 }
7276
7277 exp->write_c_string(")");
7278
7279 const Typed_identifier_list* results = fntype->results();
7280 if (results != NULL)
7281 {
7282 exp->write_c_string(" ");
7283 if (results->size() == 1 && results->begin()->name().empty())
7284 exp->write_type(results->begin()->type());
7285 else
7286 {
7287 first = true;
7288 exp->write_c_string("(");
7289 for (Typed_identifier_list::const_iterator p =
7290 results->begin();
7291 p != results->end();
7292 ++p)
7293 {
7294 if (first)
7295 first = false;
7296 else
7297 exp->write_c_string(", ");
7298 exp->write_name(p->name());
7299 exp->write_c_string(" ");
7300 exp->write_type(p->type());
7301 }
7302 exp->write_c_string(")");
7303 }
7304 }
7305 }
7306
7307 exp->write_c_string("; ");
7308 }
7309 }
7310
7311 exp->write_c_string("}");
7312 }
7313
7314 // Import an interface type.
7315
7316 Interface_type*
7317 Interface_type::do_import(Import* imp)
7318 {
7319 imp->require_c_string("interface { ");
7320
7321 Typed_identifier_list* methods = new Typed_identifier_list;
7322 while (imp->peek_char() != '}')
7323 {
7324 std::string name = imp->read_identifier();
7325
7326 if (name == "?")
7327 {
7328 imp->require_c_string(" ");
7329 Type* t = imp->read_type();
7330 methods->push_back(Typed_identifier("", t, imp->location()));
7331 imp->require_c_string("; ");
7332 continue;
7333 }
7334
7335 imp->require_c_string(" (");
7336
7337 Typed_identifier_list* parameters;
7338 bool is_varargs = false;
7339 if (imp->peek_char() == ')')
7340 parameters = NULL;
7341 else
7342 {
7343 parameters = new Typed_identifier_list;
7344 while (true)
7345 {
7346 std::string name = imp->read_name();
7347 imp->require_c_string(" ");
7348
7349 if (imp->match_c_string("..."))
7350 {
7351 imp->advance(3);
7352 is_varargs = true;
7353 }
7354
7355 Type* ptype = imp->read_type();
7356 if (is_varargs)
7357 ptype = Type::make_array_type(ptype, NULL);
7358 parameters->push_back(Typed_identifier(name, ptype,
7359 imp->location()));
7360 if (imp->peek_char() != ',')
7361 break;
7362 go_assert(!is_varargs);
7363 imp->require_c_string(", ");
7364 }
7365 }
7366 imp->require_c_string(")");
7367
7368 Typed_identifier_list* results;
7369 if (imp->peek_char() != ' ')
7370 results = NULL;
7371 else
7372 {
7373 results = new Typed_identifier_list;
7374 imp->advance(1);
7375 if (imp->peek_char() != '(')
7376 {
7377 Type* rtype = imp->read_type();
7378 results->push_back(Typed_identifier("", rtype, imp->location()));
7379 }
7380 else
7381 {
7382 imp->advance(1);
7383 while (true)
7384 {
7385 std::string name = imp->read_name();
7386 imp->require_c_string(" ");
7387 Type* rtype = imp->read_type();
7388 results->push_back(Typed_identifier(name, rtype,
7389 imp->location()));
7390 if (imp->peek_char() != ',')
7391 break;
7392 imp->require_c_string(", ");
7393 }
7394 imp->require_c_string(")");
7395 }
7396 }
7397
7398 Function_type* fntype = Type::make_function_type(NULL, parameters,
7399 results,
7400 imp->location());
7401 if (is_varargs)
7402 fntype->set_is_varargs();
7403 methods->push_back(Typed_identifier(name, fntype, imp->location()));
7404
7405 imp->require_c_string("; ");
7406 }
7407
7408 imp->require_c_string("}");
7409
7410 if (methods->empty())
7411 {
7412 delete methods;
7413 methods = NULL;
7414 }
7415
7416 return Type::make_interface_type(methods, imp->location());
7417 }
7418
7419 // Make an interface type.
7420
7421 Interface_type*
7422 Type::make_interface_type(Typed_identifier_list* methods,
7423 Location location)
7424 {
7425 return new Interface_type(methods, location);
7426 }
7427
7428 // Make an empty interface type.
7429
7430 Interface_type*
7431 Type::make_empty_interface_type(Location location)
7432 {
7433 Interface_type* ret = new Interface_type(NULL, location);
7434 ret->finalize_methods();
7435 return ret;
7436 }
7437
7438 // Class Method.
7439
7440 // Bind a method to an object.
7441
7442 Expression*
7443 Method::bind_method(Expression* expr, Location location) const
7444 {
7445 if (this->stub_ == NULL)
7446 {
7447 // When there is no stub object, the binding is determined by
7448 // the child class.
7449 return this->do_bind_method(expr, location);
7450 }
7451 return Expression::make_bound_method(expr, this->stub_, location);
7452 }
7453
7454 // Return the named object associated with a method. This may only be
7455 // called after methods are finalized.
7456
7457 Named_object*
7458 Method::named_object() const
7459 {
7460 if (this->stub_ != NULL)
7461 return this->stub_;
7462 return this->do_named_object();
7463 }
7464
7465 // Class Named_method.
7466
7467 // The type of the method.
7468
7469 Function_type*
7470 Named_method::do_type() const
7471 {
7472 if (this->named_object_->is_function())
7473 return this->named_object_->func_value()->type();
7474 else if (this->named_object_->is_function_declaration())
7475 return this->named_object_->func_declaration_value()->type();
7476 else
7477 go_unreachable();
7478 }
7479
7480 // Return the location of the method receiver.
7481
7482 Location
7483 Named_method::do_receiver_location() const
7484 {
7485 return this->do_type()->receiver()->location();
7486 }
7487
7488 // Bind a method to an object.
7489
7490 Expression*
7491 Named_method::do_bind_method(Expression* expr, Location location) const
7492 {
7493 Named_object* no = this->named_object_;
7494 Bound_method_expression* bme = Expression::make_bound_method(expr, no,
7495 location);
7496 // If this is not a local method, and it does not use a stub, then
7497 // the real method expects a different type. We need to cast the
7498 // first argument.
7499 if (this->depth() > 0 && !this->needs_stub_method())
7500 {
7501 Function_type* ftype = this->do_type();
7502 go_assert(ftype->is_method());
7503 Type* frtype = ftype->receiver()->type();
7504 bme->set_first_argument_type(frtype);
7505 }
7506 return bme;
7507 }
7508
7509 // Class Interface_method.
7510
7511 // Bind a method to an object.
7512
7513 Expression*
7514 Interface_method::do_bind_method(Expression* expr,
7515 Location location) const
7516 {
7517 return Expression::make_interface_field_reference(expr, this->name_,
7518 location);
7519 }
7520
7521 // Class Methods.
7522
7523 // Insert a new method. Return true if it was inserted, false
7524 // otherwise.
7525
7526 bool
7527 Methods::insert(const std::string& name, Method* m)
7528 {
7529 std::pair<Method_map::iterator, bool> ins =
7530 this->methods_.insert(std::make_pair(name, m));
7531 if (ins.second)
7532 return true;
7533 else
7534 {
7535 Method* old_method = ins.first->second;
7536 if (m->depth() < old_method->depth())
7537 {
7538 delete old_method;
7539 ins.first->second = m;
7540 return true;
7541 }
7542 else
7543 {
7544 if (m->depth() == old_method->depth())
7545 old_method->set_is_ambiguous();
7546 return false;
7547 }
7548 }
7549 }
7550
7551 // Return the number of unambiguous methods.
7552
7553 size_t
7554 Methods::count() const
7555 {
7556 size_t ret = 0;
7557 for (Method_map::const_iterator p = this->methods_.begin();
7558 p != this->methods_.end();
7559 ++p)
7560 if (!p->second->is_ambiguous())
7561 ++ret;
7562 return ret;
7563 }
7564
7565 // Class Named_type.
7566
7567 // Return the name of the type.
7568
7569 const std::string&
7570 Named_type::name() const
7571 {
7572 return this->named_object_->name();
7573 }
7574
7575 // Return the name of the type to use in an error message.
7576
7577 std::string
7578 Named_type::message_name() const
7579 {
7580 return this->named_object_->message_name();
7581 }
7582
7583 // Whether this is an alias. There are currently only two aliases so
7584 // we just recognize them by name.
7585
7586 bool
7587 Named_type::is_alias() const
7588 {
7589 if (!this->is_builtin())
7590 return false;
7591 const std::string& name(this->name());
7592 return name == "byte" || name == "rune";
7593 }
7594
7595 // Return the base type for this type. We have to be careful about
7596 // circular type definitions, which are invalid but may be seen here.
7597
7598 Type*
7599 Named_type::named_base()
7600 {
7601 if (this->seen_)
7602 return this;
7603 this->seen_ = true;
7604 Type* ret = this->type_->base();
7605 this->seen_ = false;
7606 return ret;
7607 }
7608
7609 const Type*
7610 Named_type::named_base() const
7611 {
7612 if (this->seen_)
7613 return this;
7614 this->seen_ = true;
7615 const Type* ret = this->type_->base();
7616 this->seen_ = false;
7617 return ret;
7618 }
7619
7620 // Return whether this is an error type. We have to be careful about
7621 // circular type definitions, which are invalid but may be seen here.
7622
7623 bool
7624 Named_type::is_named_error_type() const
7625 {
7626 if (this->seen_)
7627 return false;
7628 this->seen_ = true;
7629 bool ret = this->type_->is_error_type();
7630 this->seen_ = false;
7631 return ret;
7632 }
7633
7634 // Whether this type is comparable. We have to be careful about
7635 // circular type definitions.
7636
7637 bool
7638 Named_type::named_type_is_comparable(std::string* reason) const
7639 {
7640 if (this->seen_)
7641 return false;
7642 this->seen_ = true;
7643 bool ret = Type::are_compatible_for_comparison(true, this->type_,
7644 this->type_, reason);
7645 this->seen_ = false;
7646 return ret;
7647 }
7648
7649 // Add a method to this type.
7650
7651 Named_object*
7652 Named_type::add_method(const std::string& name, Function* function)
7653 {
7654 if (this->local_methods_ == NULL)
7655 this->local_methods_ = new Bindings(NULL);
7656 return this->local_methods_->add_function(name, NULL, function);
7657 }
7658
7659 // Add a method declaration to this type.
7660
7661 Named_object*
7662 Named_type::add_method_declaration(const std::string& name, Package* package,
7663 Function_type* type,
7664 Location location)
7665 {
7666 if (this->local_methods_ == NULL)
7667 this->local_methods_ = new Bindings(NULL);
7668 return this->local_methods_->add_function_declaration(name, package, type,
7669 location);
7670 }
7671
7672 // Add an existing method to this type.
7673
7674 void
7675 Named_type::add_existing_method(Named_object* no)
7676 {
7677 if (this->local_methods_ == NULL)
7678 this->local_methods_ = new Bindings(NULL);
7679 this->local_methods_->add_named_object(no);
7680 }
7681
7682 // Look for a local method NAME, and returns its named object, or NULL
7683 // if not there.
7684
7685 Named_object*
7686 Named_type::find_local_method(const std::string& name) const
7687 {
7688 if (this->local_methods_ == NULL)
7689 return NULL;
7690 return this->local_methods_->lookup(name);
7691 }
7692
7693 // Return whether NAME is an unexported field or method, for better
7694 // error reporting.
7695
7696 bool
7697 Named_type::is_unexported_local_method(Gogo* gogo,
7698 const std::string& name) const
7699 {
7700 Bindings* methods = this->local_methods_;
7701 if (methods != NULL)
7702 {
7703 for (Bindings::const_declarations_iterator p =
7704 methods->begin_declarations();
7705 p != methods->end_declarations();
7706 ++p)
7707 {
7708 if (Gogo::is_hidden_name(p->first)
7709 && name == Gogo::unpack_hidden_name(p->first)
7710 && gogo->pack_hidden_name(name, false) != p->first)
7711 return true;
7712 }
7713 }
7714 return false;
7715 }
7716
7717 // Build the complete list of methods for this type, which means
7718 // recursively including all methods for anonymous fields. Create all
7719 // stub methods.
7720
7721 void
7722 Named_type::finalize_methods(Gogo* gogo)
7723 {
7724 if (this->all_methods_ != NULL)
7725 return;
7726
7727 if (this->local_methods_ != NULL
7728 && (this->points_to() != NULL || this->interface_type() != NULL))
7729 {
7730 const Bindings* lm = this->local_methods_;
7731 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
7732 p != lm->end_declarations();
7733 ++p)
7734 error_at(p->second->location(),
7735 "invalid pointer or interface receiver type");
7736 delete this->local_methods_;
7737 this->local_methods_ = NULL;
7738 return;
7739 }
7740
7741 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
7742 }
7743
7744 // Return the method NAME, or NULL if there isn't one or if it is
7745 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
7746 // ambiguous.
7747
7748 Method*
7749 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
7750 {
7751 return Type::method_function(this->all_methods_, name, is_ambiguous);
7752 }
7753
7754 // Return a pointer to the interface method table for this type for
7755 // the interface INTERFACE. IS_POINTER is true if this is for a
7756 // pointer to THIS.
7757
7758 tree
7759 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
7760 bool is_pointer)
7761 {
7762 return Type::interface_method_table(gogo, this, interface, is_pointer,
7763 &this->interface_method_tables_,
7764 &this->pointer_interface_method_tables_);
7765 }
7766
7767 // Return whether a named type has any hidden fields.
7768
7769 bool
7770 Named_type::named_type_has_hidden_fields(std::string* reason) const
7771 {
7772 if (this->seen_)
7773 return false;
7774 this->seen_ = true;
7775 bool ret = this->type_->has_hidden_fields(this, reason);
7776 this->seen_ = false;
7777 return ret;
7778 }
7779
7780 // Look for a use of a complete type within another type. This is
7781 // used to check that we don't try to use a type within itself.
7782
7783 class Find_type_use : public Traverse
7784 {
7785 public:
7786 Find_type_use(Named_type* find_type)
7787 : Traverse(traverse_types),
7788 find_type_(find_type), found_(false)
7789 { }
7790
7791 // Whether we found the type.
7792 bool
7793 found() const
7794 { return this->found_; }
7795
7796 protected:
7797 int
7798 type(Type*);
7799
7800 private:
7801 // The type we are looking for.
7802 Named_type* find_type_;
7803 // Whether we found the type.
7804 bool found_;
7805 };
7806
7807 // Check for FIND_TYPE in TYPE.
7808
7809 int
7810 Find_type_use::type(Type* type)
7811 {
7812 if (type->named_type() != NULL && this->find_type_ == type->named_type())
7813 {
7814 this->found_ = true;
7815 return TRAVERSE_EXIT;
7816 }
7817
7818 // It's OK if we see a reference to the type in any type which is
7819 // essentially a pointer: a pointer, a slice, a function, a map, or
7820 // a channel.
7821 if (type->points_to() != NULL
7822 || type->is_slice_type()
7823 || type->function_type() != NULL
7824 || type->map_type() != NULL
7825 || type->channel_type() != NULL)
7826 return TRAVERSE_SKIP_COMPONENTS;
7827
7828 // For an interface, a reference to the type in a method type should
7829 // be ignored, but we have to consider direct inheritance. When
7830 // this is called, there may be cases of direct inheritance
7831 // represented as a method with no name.
7832 if (type->interface_type() != NULL)
7833 {
7834 const Typed_identifier_list* methods = type->interface_type()->methods();
7835 if (methods != NULL)
7836 {
7837 for (Typed_identifier_list::const_iterator p = methods->begin();
7838 p != methods->end();
7839 ++p)
7840 {
7841 if (p->name().empty())
7842 {
7843 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
7844 return TRAVERSE_EXIT;
7845 }
7846 }
7847 }
7848 return TRAVERSE_SKIP_COMPONENTS;
7849 }
7850
7851 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
7852 // to convert TYPE to the backend representation before we convert
7853 // FIND_TYPE_.
7854 if (type->named_type() != NULL)
7855 {
7856 switch (type->base()->classification())
7857 {
7858 case Type::TYPE_ERROR:
7859 case Type::TYPE_BOOLEAN:
7860 case Type::TYPE_INTEGER:
7861 case Type::TYPE_FLOAT:
7862 case Type::TYPE_COMPLEX:
7863 case Type::TYPE_STRING:
7864 case Type::TYPE_NIL:
7865 break;
7866
7867 case Type::TYPE_ARRAY:
7868 case Type::TYPE_STRUCT:
7869 this->find_type_->add_dependency(type->named_type());
7870 break;
7871
7872 case Type::TYPE_NAMED:
7873 case Type::TYPE_FORWARD:
7874 go_assert(saw_errors());
7875 break;
7876
7877 case Type::TYPE_VOID:
7878 case Type::TYPE_SINK:
7879 case Type::TYPE_FUNCTION:
7880 case Type::TYPE_POINTER:
7881 case Type::TYPE_CALL_MULTIPLE_RESULT:
7882 case Type::TYPE_MAP:
7883 case Type::TYPE_CHANNEL:
7884 case Type::TYPE_INTERFACE:
7885 default:
7886 go_unreachable();
7887 }
7888 }
7889
7890 return TRAVERSE_CONTINUE;
7891 }
7892
7893 // Verify that a named type does not refer to itself.
7894
7895 bool
7896 Named_type::do_verify()
7897 {
7898 if (this->is_verified_)
7899 return true;
7900 this->is_verified_ = true;
7901
7902 Find_type_use find(this);
7903 Type::traverse(this->type_, &find);
7904 if (find.found())
7905 {
7906 error_at(this->location_, "invalid recursive type %qs",
7907 this->message_name().c_str());
7908 this->is_error_ = true;
7909 return false;
7910 }
7911
7912 // Check whether any of the local methods overloads an existing
7913 // struct field or interface method. We don't need to check the
7914 // list of methods against itself: that is handled by the Bindings
7915 // code.
7916 if (this->local_methods_ != NULL)
7917 {
7918 Struct_type* st = this->type_->struct_type();
7919 if (st != NULL)
7920 {
7921 for (Bindings::const_declarations_iterator p =
7922 this->local_methods_->begin_declarations();
7923 p != this->local_methods_->end_declarations();
7924 ++p)
7925 {
7926 const std::string& name(p->first);
7927 if (st != NULL && st->find_local_field(name, NULL) != NULL)
7928 {
7929 error_at(p->second->location(),
7930 "method %qs redeclares struct field name",
7931 Gogo::message_name(name).c_str());
7932 }
7933 }
7934 }
7935 }
7936
7937 return true;
7938 }
7939
7940 // Return whether this type is or contains a pointer.
7941
7942 bool
7943 Named_type::do_has_pointer() const
7944 {
7945 if (this->seen_)
7946 return false;
7947 this->seen_ = true;
7948 bool ret = this->type_->has_pointer();
7949 this->seen_ = false;
7950 return ret;
7951 }
7952
7953 // Return whether comparisons for this type can use the identity
7954 // function.
7955
7956 bool
7957 Named_type::do_compare_is_identity(Gogo* gogo) const
7958 {
7959 // We don't use this->seen_ here because compare_is_identity may
7960 // call base() later, and that will mess up if seen_ is set here.
7961 if (this->seen_in_compare_is_identity_)
7962 return false;
7963 this->seen_in_compare_is_identity_ = true;
7964 bool ret = this->type_->compare_is_identity(gogo);
7965 this->seen_in_compare_is_identity_ = false;
7966 return ret;
7967 }
7968
7969 // Return a hash code. This is used for method lookup. We simply
7970 // hash on the name itself.
7971
7972 unsigned int
7973 Named_type::do_hash_for_method(Gogo* gogo) const
7974 {
7975 if (this->is_alias())
7976 return this->type_->named_type()->do_hash_for_method(gogo);
7977
7978 const std::string& name(this->named_object()->name());
7979 unsigned int ret = Type::hash_string(name, 0);
7980
7981 // GOGO will be NULL here when called from Type_hash_identical.
7982 // That is OK because that is only used for internal hash tables
7983 // where we are going to be comparing named types for equality. In
7984 // other cases, which are cases where the runtime is going to
7985 // compare hash codes to see if the types are the same, we need to
7986 // include the pkgpath in the hash.
7987 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
7988 {
7989 const Package* package = this->named_object()->package();
7990 if (package == NULL)
7991 ret = Type::hash_string(gogo->pkgpath(), ret);
7992 else
7993 ret = Type::hash_string(package->pkgpath(), ret);
7994 }
7995
7996 return ret;
7997 }
7998
7999 // Convert a named type to the backend representation. In order to
8000 // get dependencies right, we fill in a dummy structure for this type,
8001 // then convert all the dependencies, then complete this type. When
8002 // this function is complete, the size of the type is known.
8003
8004 void
8005 Named_type::convert(Gogo* gogo)
8006 {
8007 if (this->is_error_ || this->is_converted_)
8008 return;
8009
8010 this->create_placeholder(gogo);
8011
8012 // If we are called to turn unsafe.Sizeof into a constant, we may
8013 // not have verified the type yet. We have to make sure it is
8014 // verified, since that sets the list of dependencies.
8015 this->verify();
8016
8017 // Convert all the dependencies. If they refer indirectly back to
8018 // this type, they will pick up the intermediate tree we just
8019 // created.
8020 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
8021 p != this->dependencies_.end();
8022 ++p)
8023 (*p)->convert(gogo);
8024
8025 // Complete this type.
8026 Btype* bt = this->named_btype_;
8027 Type* base = this->type_->base();
8028 switch (base->classification())
8029 {
8030 case TYPE_VOID:
8031 case TYPE_BOOLEAN:
8032 case TYPE_INTEGER:
8033 case TYPE_FLOAT:
8034 case TYPE_COMPLEX:
8035 case TYPE_STRING:
8036 case TYPE_NIL:
8037 break;
8038
8039 case TYPE_MAP:
8040 case TYPE_CHANNEL:
8041 break;
8042
8043 case TYPE_FUNCTION:
8044 case TYPE_POINTER:
8045 // The size of these types is already correct. We don't worry
8046 // about filling them in until later, when we also track
8047 // circular references.
8048 break;
8049
8050 case TYPE_STRUCT:
8051 {
8052 std::vector<Backend::Btyped_identifier> bfields;
8053 get_backend_struct_fields(gogo, base->struct_type()->fields(),
8054 true, &bfields);
8055 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8056 bt = gogo->backend()->error_type();
8057 }
8058 break;
8059
8060 case TYPE_ARRAY:
8061 // Slice types were completed in create_placeholder.
8062 if (!base->is_slice_type())
8063 {
8064 Btype* bet = base->array_type()->get_backend_element(gogo, true);
8065 Bexpression* blen = base->array_type()->get_backend_length(gogo);
8066 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
8067 bt = gogo->backend()->error_type();
8068 }
8069 break;
8070
8071 case TYPE_INTERFACE:
8072 // Interface types were completed in create_placeholder.
8073 break;
8074
8075 case TYPE_ERROR:
8076 return;
8077
8078 default:
8079 case TYPE_SINK:
8080 case TYPE_CALL_MULTIPLE_RESULT:
8081 case TYPE_NAMED:
8082 case TYPE_FORWARD:
8083 go_unreachable();
8084 }
8085
8086 this->named_btype_ = bt;
8087 this->is_converted_ = true;
8088 this->is_placeholder_ = false;
8089 }
8090
8091 // Create the placeholder for a named type. This is the first step in
8092 // converting to the backend representation.
8093
8094 void
8095 Named_type::create_placeholder(Gogo* gogo)
8096 {
8097 if (this->is_error_)
8098 this->named_btype_ = gogo->backend()->error_type();
8099
8100 if (this->named_btype_ != NULL)
8101 return;
8102
8103 // Create the structure for this type. Note that because we call
8104 // base() here, we don't attempt to represent a named type defined
8105 // as another named type. Instead both named types will point to
8106 // different base representations.
8107 Type* base = this->type_->base();
8108 Btype* bt;
8109 bool set_name = true;
8110 switch (base->classification())
8111 {
8112 case TYPE_ERROR:
8113 this->is_error_ = true;
8114 this->named_btype_ = gogo->backend()->error_type();
8115 return;
8116
8117 case TYPE_VOID:
8118 case TYPE_BOOLEAN:
8119 case TYPE_INTEGER:
8120 case TYPE_FLOAT:
8121 case TYPE_COMPLEX:
8122 case TYPE_STRING:
8123 case TYPE_NIL:
8124 // These are simple basic types, we can just create them
8125 // directly.
8126 bt = Type::get_named_base_btype(gogo, base);
8127 break;
8128
8129 case TYPE_MAP:
8130 case TYPE_CHANNEL:
8131 // All maps and channels have the same backend representation.
8132 bt = Type::get_named_base_btype(gogo, base);
8133 break;
8134
8135 case TYPE_FUNCTION:
8136 case TYPE_POINTER:
8137 {
8138 bool for_function = base->classification() == TYPE_FUNCTION;
8139 bt = gogo->backend()->placeholder_pointer_type(this->name(),
8140 this->location_,
8141 for_function);
8142 set_name = false;
8143 }
8144 break;
8145
8146 case TYPE_STRUCT:
8147 bt = gogo->backend()->placeholder_struct_type(this->name(),
8148 this->location_);
8149 this->is_placeholder_ = true;
8150 set_name = false;
8151 break;
8152
8153 case TYPE_ARRAY:
8154 if (base->is_slice_type())
8155 bt = gogo->backend()->placeholder_struct_type(this->name(),
8156 this->location_);
8157 else
8158 {
8159 bt = gogo->backend()->placeholder_array_type(this->name(),
8160 this->location_);
8161 this->is_placeholder_ = true;
8162 }
8163 set_name = false;
8164 break;
8165
8166 case TYPE_INTERFACE:
8167 if (base->interface_type()->is_empty())
8168 bt = Interface_type::get_backend_empty_interface_type(gogo);
8169 else
8170 {
8171 bt = gogo->backend()->placeholder_struct_type(this->name(),
8172 this->location_);
8173 set_name = false;
8174 }
8175 break;
8176
8177 default:
8178 case TYPE_SINK:
8179 case TYPE_CALL_MULTIPLE_RESULT:
8180 case TYPE_NAMED:
8181 case TYPE_FORWARD:
8182 go_unreachable();
8183 }
8184
8185 if (set_name)
8186 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
8187
8188 this->named_btype_ = bt;
8189
8190 if (base->is_slice_type())
8191 {
8192 // We do not record slices as dependencies of other types,
8193 // because we can fill them in completely here with the final
8194 // size.
8195 std::vector<Backend::Btyped_identifier> bfields;
8196 get_backend_slice_fields(gogo, base->array_type(), true, &bfields);
8197 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8198 this->named_btype_ = gogo->backend()->error_type();
8199 }
8200 else if (base->interface_type() != NULL
8201 && !base->interface_type()->is_empty())
8202 {
8203 // We do not record interfaces as dependencies of other types,
8204 // because we can fill them in completely here with the final
8205 // size.
8206 std::vector<Backend::Btyped_identifier> bfields;
8207 get_backend_interface_fields(gogo, base->interface_type(), true,
8208 &bfields);
8209 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8210 this->named_btype_ = gogo->backend()->error_type();
8211 }
8212 }
8213
8214 // Get a tree for a named type.
8215
8216 Btype*
8217 Named_type::do_get_backend(Gogo* gogo)
8218 {
8219 if (this->is_error_)
8220 return gogo->backend()->error_type();
8221
8222 Btype* bt = this->named_btype_;
8223
8224 if (!gogo->named_types_are_converted())
8225 {
8226 // We have not completed converting named types. NAMED_BTYPE_
8227 // is a placeholder and we shouldn't do anything further.
8228 if (bt != NULL)
8229 return bt;
8230
8231 // We don't build dependencies for types whose sizes do not
8232 // change or are not relevant, so we may see them here while
8233 // converting types.
8234 this->create_placeholder(gogo);
8235 bt = this->named_btype_;
8236 go_assert(bt != NULL);
8237 return bt;
8238 }
8239
8240 // We are not converting types. This should only be called if the
8241 // type has already been converted.
8242 if (!this->is_converted_)
8243 {
8244 go_assert(saw_errors());
8245 return gogo->backend()->error_type();
8246 }
8247
8248 go_assert(bt != NULL);
8249
8250 // Complete the tree.
8251 Type* base = this->type_->base();
8252 Btype* bt1;
8253 switch (base->classification())
8254 {
8255 case TYPE_ERROR:
8256 return gogo->backend()->error_type();
8257
8258 case TYPE_VOID:
8259 case TYPE_BOOLEAN:
8260 case TYPE_INTEGER:
8261 case TYPE_FLOAT:
8262 case TYPE_COMPLEX:
8263 case TYPE_STRING:
8264 case TYPE_NIL:
8265 case TYPE_MAP:
8266 case TYPE_CHANNEL:
8267 return bt;
8268
8269 case TYPE_STRUCT:
8270 if (!this->seen_in_get_backend_)
8271 {
8272 this->seen_in_get_backend_ = true;
8273 base->struct_type()->finish_backend_fields(gogo);
8274 this->seen_in_get_backend_ = false;
8275 }
8276 return bt;
8277
8278 case TYPE_ARRAY:
8279 if (!this->seen_in_get_backend_)
8280 {
8281 this->seen_in_get_backend_ = true;
8282 base->array_type()->finish_backend_element(gogo);
8283 this->seen_in_get_backend_ = false;
8284 }
8285 return bt;
8286
8287 case TYPE_INTERFACE:
8288 if (!this->seen_in_get_backend_)
8289 {
8290 this->seen_in_get_backend_ = true;
8291 base->interface_type()->finish_backend_methods(gogo);
8292 this->seen_in_get_backend_ = false;
8293 }
8294 return bt;
8295
8296 case TYPE_FUNCTION:
8297 // Don't build a circular data structure. GENERIC can't handle
8298 // it.
8299 if (this->seen_in_get_backend_)
8300 {
8301 this->is_circular_ = true;
8302 return gogo->backend()->circular_pointer_type(bt, true);
8303 }
8304 this->seen_in_get_backend_ = true;
8305 bt1 = Type::get_named_base_btype(gogo, base);
8306 this->seen_in_get_backend_ = false;
8307 if (this->is_circular_)
8308 bt1 = gogo->backend()->circular_pointer_type(bt, true);
8309 if (!gogo->backend()->set_placeholder_function_type(bt, bt1))
8310 bt = gogo->backend()->error_type();
8311 return bt;
8312
8313 case TYPE_POINTER:
8314 // Don't build a circular data structure. GENERIC can't handle
8315 // it.
8316 if (this->seen_in_get_backend_)
8317 {
8318 this->is_circular_ = true;
8319 return gogo->backend()->circular_pointer_type(bt, false);
8320 }
8321 this->seen_in_get_backend_ = true;
8322 bt1 = Type::get_named_base_btype(gogo, base);
8323 this->seen_in_get_backend_ = false;
8324 if (this->is_circular_)
8325 bt1 = gogo->backend()->circular_pointer_type(bt, false);
8326 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
8327 bt = gogo->backend()->error_type();
8328 return bt;
8329
8330 default:
8331 case TYPE_SINK:
8332 case TYPE_CALL_MULTIPLE_RESULT:
8333 case TYPE_NAMED:
8334 case TYPE_FORWARD:
8335 go_unreachable();
8336 }
8337
8338 go_unreachable();
8339 }
8340
8341 // Build a type descriptor for a named type.
8342
8343 Expression*
8344 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8345 {
8346 if (name == NULL && this->is_alias())
8347 return this->type_->type_descriptor(gogo, this->type_);
8348
8349 // If NAME is not NULL, then we don't really want the type
8350 // descriptor for this type; we want the descriptor for the
8351 // underlying type, giving it the name NAME.
8352 return this->named_type_descriptor(gogo, this->type_,
8353 name == NULL ? this : name);
8354 }
8355
8356 // Add to the reflection string. This is used mostly for the name of
8357 // the type used in a type descriptor, not for actual reflection
8358 // strings.
8359
8360 void
8361 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
8362 {
8363 if (this->is_alias())
8364 {
8365 this->append_reflection(this->type_, gogo, ret);
8366 return;
8367 }
8368 if (!this->is_builtin())
8369 {
8370 // We handle -fgo-prefix and -fgo-pkgpath differently here for
8371 // compatibility with how the compiler worked before
8372 // -fgo-pkgpath was introduced. When -fgo-pkgpath is specified,
8373 // we use it to make a unique reflection string, so that the
8374 // type canonicalization in the reflect package will work. In
8375 // order to be compatible with the gc compiler, we put tabs into
8376 // the package path, so that the reflect methods can discard it.
8377 const Package* package = this->named_object_->package();
8378 if (gogo->pkgpath_from_option())
8379 {
8380 ret->push_back('\t');
8381 ret->append(package != NULL
8382 ? package->pkgpath_symbol()
8383 : gogo->pkgpath_symbol());
8384 ret->push_back('\t');
8385 }
8386 ret->append(package != NULL
8387 ? package->package_name()
8388 : gogo->package_name());
8389 ret->push_back('.');
8390 }
8391 if (this->in_function_ != NULL)
8392 {
8393 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
8394 ret->push_back('$');
8395 if (this->in_function_index_ > 0)
8396 {
8397 char buf[30];
8398 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8399 ret->append(buf);
8400 ret->push_back('$');
8401 }
8402 }
8403 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
8404 }
8405
8406 // Get the mangled name.
8407
8408 void
8409 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8410 {
8411 if (this->is_alias())
8412 {
8413 this->append_mangled_name(this->type_, gogo, ret);
8414 return;
8415 }
8416 Named_object* no = this->named_object_;
8417 std::string name;
8418 if (this->is_builtin())
8419 go_assert(this->in_function_ == NULL);
8420 else
8421 {
8422 const std::string& pkgpath(no->package() == NULL
8423 ? gogo->pkgpath_symbol()
8424 : no->package()->pkgpath_symbol());
8425 name = pkgpath;
8426 name.append(1, '.');
8427 if (this->in_function_ != NULL)
8428 {
8429 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
8430 name.append(1, '$');
8431 if (this->in_function_index_ > 0)
8432 {
8433 char buf[30];
8434 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8435 name.append(buf);
8436 name.append(1, '$');
8437 }
8438 }
8439 }
8440 name.append(Gogo::unpack_hidden_name(no->name()));
8441 char buf[20];
8442 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
8443 ret->append(buf);
8444 ret->append(name);
8445 }
8446
8447 // Export the type. This is called to export a global type.
8448
8449 void
8450 Named_type::export_named_type(Export* exp, const std::string&) const
8451 {
8452 // We don't need to write the name of the type here, because it will
8453 // be written by Export::write_type anyhow.
8454 exp->write_c_string("type ");
8455 exp->write_type(this);
8456 exp->write_c_string(";\n");
8457 }
8458
8459 // Import a named type.
8460
8461 void
8462 Named_type::import_named_type(Import* imp, Named_type** ptype)
8463 {
8464 imp->require_c_string("type ");
8465 Type *type = imp->read_type();
8466 *ptype = type->named_type();
8467 go_assert(*ptype != NULL);
8468 imp->require_c_string(";\n");
8469 }
8470
8471 // Export the type when it is referenced by another type. In this
8472 // case Export::export_type will already have issued the name.
8473
8474 void
8475 Named_type::do_export(Export* exp) const
8476 {
8477 exp->write_type(this->type_);
8478
8479 // To save space, we only export the methods directly attached to
8480 // this type.
8481 Bindings* methods = this->local_methods_;
8482 if (methods == NULL)
8483 return;
8484
8485 exp->write_c_string("\n");
8486 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
8487 p != methods->end_definitions();
8488 ++p)
8489 {
8490 exp->write_c_string(" ");
8491 (*p)->export_named_object(exp);
8492 }
8493
8494 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
8495 p != methods->end_declarations();
8496 ++p)
8497 {
8498 if (p->second->is_function_declaration())
8499 {
8500 exp->write_c_string(" ");
8501 p->second->export_named_object(exp);
8502 }
8503 }
8504 }
8505
8506 // Make a named type.
8507
8508 Named_type*
8509 Type::make_named_type(Named_object* named_object, Type* type,
8510 Location location)
8511 {
8512 return new Named_type(named_object, type, location);
8513 }
8514
8515 // Finalize the methods for TYPE. It will be a named type or a struct
8516 // type. This sets *ALL_METHODS to the list of methods, and builds
8517 // all required stubs.
8518
8519 void
8520 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
8521 Methods** all_methods)
8522 {
8523 *all_methods = NULL;
8524 Types_seen types_seen;
8525 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
8526 all_methods);
8527 Type::build_stub_methods(gogo, type, *all_methods, location);
8528 }
8529
8530 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8531 // build up the struct field indexes as we go. DEPTH is the depth of
8532 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8533 // adding these methods for an anonymous field with pointer type.
8534 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8535 // calls the real method. TYPES_SEEN is used to avoid infinite
8536 // recursion.
8537
8538 void
8539 Type::add_methods_for_type(const Type* type,
8540 const Method::Field_indexes* field_indexes,
8541 unsigned int depth,
8542 bool is_embedded_pointer,
8543 bool needs_stub_method,
8544 Types_seen* types_seen,
8545 Methods** methods)
8546 {
8547 // Pointer types may not have methods.
8548 if (type->points_to() != NULL)
8549 return;
8550
8551 const Named_type* nt = type->named_type();
8552 if (nt != NULL)
8553 {
8554 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
8555 if (!ins.second)
8556 return;
8557 }
8558
8559 if (nt != NULL)
8560 Type::add_local_methods_for_type(nt, field_indexes, depth,
8561 is_embedded_pointer, needs_stub_method,
8562 methods);
8563
8564 Type::add_embedded_methods_for_type(type, field_indexes, depth,
8565 is_embedded_pointer, needs_stub_method,
8566 types_seen, methods);
8567
8568 // If we are called with depth > 0, then we are looking at an
8569 // anonymous field of a struct. If such a field has interface type,
8570 // then we need to add the interface methods. We don't want to add
8571 // them when depth == 0, because we will already handle them
8572 // following the usual rules for an interface type.
8573 if (depth > 0)
8574 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
8575 }
8576
8577 // Add the local methods for the named type NT to *METHODS. The
8578 // parameters are as for add_methods_to_type.
8579
8580 void
8581 Type::add_local_methods_for_type(const Named_type* nt,
8582 const Method::Field_indexes* field_indexes,
8583 unsigned int depth,
8584 bool is_embedded_pointer,
8585 bool needs_stub_method,
8586 Methods** methods)
8587 {
8588 const Bindings* local_methods = nt->local_methods();
8589 if (local_methods == NULL)
8590 return;
8591
8592 if (*methods == NULL)
8593 *methods = new Methods();
8594
8595 for (Bindings::const_declarations_iterator p =
8596 local_methods->begin_declarations();
8597 p != local_methods->end_declarations();
8598 ++p)
8599 {
8600 Named_object* no = p->second;
8601 bool is_value_method = (is_embedded_pointer
8602 || !Type::method_expects_pointer(no));
8603 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
8604 (needs_stub_method
8605 || (depth > 0 && is_value_method)));
8606 if (!(*methods)->insert(no->name(), m))
8607 delete m;
8608 }
8609 }
8610
8611 // Add the embedded methods for TYPE to *METHODS. These are the
8612 // methods attached to anonymous fields. The parameters are as for
8613 // add_methods_to_type.
8614
8615 void
8616 Type::add_embedded_methods_for_type(const Type* type,
8617 const Method::Field_indexes* field_indexes,
8618 unsigned int depth,
8619 bool is_embedded_pointer,
8620 bool needs_stub_method,
8621 Types_seen* types_seen,
8622 Methods** methods)
8623 {
8624 // Look for anonymous fields in TYPE. TYPE has fields if it is a
8625 // struct.
8626 const Struct_type* st = type->struct_type();
8627 if (st == NULL)
8628 return;
8629
8630 const Struct_field_list* fields = st->fields();
8631 if (fields == NULL)
8632 return;
8633
8634 unsigned int i = 0;
8635 for (Struct_field_list::const_iterator pf = fields->begin();
8636 pf != fields->end();
8637 ++pf, ++i)
8638 {
8639 if (!pf->is_anonymous())
8640 continue;
8641
8642 Type* ftype = pf->type();
8643 bool is_pointer = false;
8644 if (ftype->points_to() != NULL)
8645 {
8646 ftype = ftype->points_to();
8647 is_pointer = true;
8648 }
8649 Named_type* fnt = ftype->named_type();
8650 if (fnt == NULL)
8651 {
8652 // This is an error, but it will be diagnosed elsewhere.
8653 continue;
8654 }
8655
8656 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
8657 sub_field_indexes->next = field_indexes;
8658 sub_field_indexes->field_index = i;
8659
8660 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
8661 (is_embedded_pointer || is_pointer),
8662 (needs_stub_method
8663 || is_pointer
8664 || i > 0),
8665 types_seen,
8666 methods);
8667 }
8668 }
8669
8670 // If TYPE is an interface type, then add its method to *METHODS.
8671 // This is for interface methods attached to an anonymous field. The
8672 // parameters are as for add_methods_for_type.
8673
8674 void
8675 Type::add_interface_methods_for_type(const Type* type,
8676 const Method::Field_indexes* field_indexes,
8677 unsigned int depth,
8678 Methods** methods)
8679 {
8680 const Interface_type* it = type->interface_type();
8681 if (it == NULL)
8682 return;
8683
8684 const Typed_identifier_list* imethods = it->methods();
8685 if (imethods == NULL)
8686 return;
8687
8688 if (*methods == NULL)
8689 *methods = new Methods();
8690
8691 for (Typed_identifier_list::const_iterator pm = imethods->begin();
8692 pm != imethods->end();
8693 ++pm)
8694 {
8695 Function_type* fntype = pm->type()->function_type();
8696 if (fntype == NULL)
8697 {
8698 // This is an error, but it should be reported elsewhere
8699 // when we look at the methods for IT.
8700 continue;
8701 }
8702 go_assert(!fntype->is_method());
8703 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
8704 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
8705 field_indexes, depth);
8706 if (!(*methods)->insert(pm->name(), m))
8707 delete m;
8708 }
8709 }
8710
8711 // Build stub methods for TYPE as needed. METHODS is the set of
8712 // methods for the type. A stub method may be needed when a type
8713 // inherits a method from an anonymous field. When we need the
8714 // address of the method, as in a type descriptor, we need to build a
8715 // little stub which does the required field dereferences and jumps to
8716 // the real method. LOCATION is the location of the type definition.
8717
8718 void
8719 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
8720 Location location)
8721 {
8722 if (methods == NULL)
8723 return;
8724 for (Methods::const_iterator p = methods->begin();
8725 p != methods->end();
8726 ++p)
8727 {
8728 Method* m = p->second;
8729 if (m->is_ambiguous() || !m->needs_stub_method())
8730 continue;
8731
8732 const std::string& name(p->first);
8733
8734 // Build a stub method.
8735
8736 const Function_type* fntype = m->type();
8737
8738 static unsigned int counter;
8739 char buf[100];
8740 snprintf(buf, sizeof buf, "$this%u", counter);
8741 ++counter;
8742
8743 Type* receiver_type = const_cast<Type*>(type);
8744 if (!m->is_value_method())
8745 receiver_type = Type::make_pointer_type(receiver_type);
8746 Location receiver_location = m->receiver_location();
8747 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
8748 receiver_location);
8749
8750 const Typed_identifier_list* fnparams = fntype->parameters();
8751 Typed_identifier_list* stub_params;
8752 if (fnparams == NULL || fnparams->empty())
8753 stub_params = NULL;
8754 else
8755 {
8756 // We give each stub parameter a unique name.
8757 stub_params = new Typed_identifier_list();
8758 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
8759 pp != fnparams->end();
8760 ++pp)
8761 {
8762 char pbuf[100];
8763 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
8764 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
8765 pp->location()));
8766 ++counter;
8767 }
8768 }
8769
8770 const Typed_identifier_list* fnresults = fntype->results();
8771 Typed_identifier_list* stub_results;
8772 if (fnresults == NULL || fnresults->empty())
8773 stub_results = NULL;
8774 else
8775 {
8776 // We create the result parameters without any names, since
8777 // we won't refer to them.
8778 stub_results = new Typed_identifier_list();
8779 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
8780 pr != fnresults->end();
8781 ++pr)
8782 stub_results->push_back(Typed_identifier("", pr->type(),
8783 pr->location()));
8784 }
8785
8786 Function_type* stub_type = Type::make_function_type(receiver,
8787 stub_params,
8788 stub_results,
8789 fntype->location());
8790 if (fntype->is_varargs())
8791 stub_type->set_is_varargs();
8792
8793 // We only create the function in the package which creates the
8794 // type.
8795 const Package* package;
8796 if (type->named_type() == NULL)
8797 package = NULL;
8798 else
8799 package = type->named_type()->named_object()->package();
8800 Named_object* stub;
8801 if (package != NULL)
8802 stub = Named_object::make_function_declaration(name, package,
8803 stub_type, location);
8804 else
8805 {
8806 stub = gogo->start_function(name, stub_type, false,
8807 fntype->location());
8808 Type::build_one_stub_method(gogo, m, buf, stub_params,
8809 fntype->is_varargs(), location);
8810 gogo->finish_function(fntype->location());
8811 }
8812
8813 m->set_stub_object(stub);
8814 }
8815 }
8816
8817 // Build a stub method which adjusts the receiver as required to call
8818 // METHOD. RECEIVER_NAME is the name we used for the receiver.
8819 // PARAMS is the list of function parameters.
8820
8821 void
8822 Type::build_one_stub_method(Gogo* gogo, Method* method,
8823 const char* receiver_name,
8824 const Typed_identifier_list* params,
8825 bool is_varargs,
8826 Location location)
8827 {
8828 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
8829 go_assert(receiver_object != NULL);
8830
8831 Expression* expr = Expression::make_var_reference(receiver_object, location);
8832 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
8833 if (expr->type()->points_to() == NULL)
8834 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8835
8836 Expression_list* arguments;
8837 if (params == NULL || params->empty())
8838 arguments = NULL;
8839 else
8840 {
8841 arguments = new Expression_list();
8842 for (Typed_identifier_list::const_iterator p = params->begin();
8843 p != params->end();
8844 ++p)
8845 {
8846 Named_object* param = gogo->lookup(p->name(), NULL);
8847 go_assert(param != NULL);
8848 Expression* param_ref = Expression::make_var_reference(param,
8849 location);
8850 arguments->push_back(param_ref);
8851 }
8852 }
8853
8854 Expression* func = method->bind_method(expr, location);
8855 go_assert(func != NULL);
8856 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
8857 location);
8858 call->set_hidden_fields_are_ok();
8859 size_t count = call->result_count();
8860 if (count == 0)
8861 gogo->add_statement(Statement::make_statement(call, true));
8862 else
8863 {
8864 Expression_list* retvals = new Expression_list();
8865 if (count <= 1)
8866 retvals->push_back(call);
8867 else
8868 {
8869 for (size_t i = 0; i < count; ++i)
8870 retvals->push_back(Expression::make_call_result(call, i));
8871 }
8872 Return_statement* retstat = Statement::make_return_statement(retvals,
8873 location);
8874
8875 // We can return values with hidden fields from a stub. This is
8876 // necessary if the method is itself hidden.
8877 retstat->set_hidden_fields_are_ok();
8878
8879 gogo->add_statement(retstat);
8880 }
8881 }
8882
8883 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
8884 // in reverse order.
8885
8886 Expression*
8887 Type::apply_field_indexes(Expression* expr,
8888 const Method::Field_indexes* field_indexes,
8889 Location location)
8890 {
8891 if (field_indexes == NULL)
8892 return expr;
8893 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
8894 Struct_type* stype = expr->type()->deref()->struct_type();
8895 go_assert(stype != NULL
8896 && field_indexes->field_index < stype->field_count());
8897 if (expr->type()->struct_type() == NULL)
8898 {
8899 go_assert(expr->type()->points_to() != NULL);
8900 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8901 go_assert(expr->type()->struct_type() == stype);
8902 }
8903 return Expression::make_field_reference(expr, field_indexes->field_index,
8904 location);
8905 }
8906
8907 // Return whether NO is a method for which the receiver is a pointer.
8908
8909 bool
8910 Type::method_expects_pointer(const Named_object* no)
8911 {
8912 const Function_type *fntype;
8913 if (no->is_function())
8914 fntype = no->func_value()->type();
8915 else if (no->is_function_declaration())
8916 fntype = no->func_declaration_value()->type();
8917 else
8918 go_unreachable();
8919 return fntype->receiver()->type()->points_to() != NULL;
8920 }
8921
8922 // Given a set of methods for a type, METHODS, return the method NAME,
8923 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
8924 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
8925 // but is ambiguous (and return NULL).
8926
8927 Method*
8928 Type::method_function(const Methods* methods, const std::string& name,
8929 bool* is_ambiguous)
8930 {
8931 if (is_ambiguous != NULL)
8932 *is_ambiguous = false;
8933 if (methods == NULL)
8934 return NULL;
8935 Methods::const_iterator p = methods->find(name);
8936 if (p == methods->end())
8937 return NULL;
8938 Method* m = p->second;
8939 if (m->is_ambiguous())
8940 {
8941 if (is_ambiguous != NULL)
8942 *is_ambiguous = true;
8943 return NULL;
8944 }
8945 return m;
8946 }
8947
8948 // Return a pointer to the interface method table for TYPE for the
8949 // interface INTERFACE.
8950
8951 tree
8952 Type::interface_method_table(Gogo* gogo, Type* type,
8953 const Interface_type *interface,
8954 bool is_pointer,
8955 Interface_method_tables** method_tables,
8956 Interface_method_tables** pointer_tables)
8957 {
8958 go_assert(!interface->is_empty());
8959
8960 Interface_method_tables** pimt = is_pointer ? method_tables : pointer_tables;
8961
8962 if (*pimt == NULL)
8963 *pimt = new Interface_method_tables(5);
8964
8965 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
8966 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
8967
8968 if (ins.second)
8969 {
8970 // This is a new entry in the hash table.
8971 go_assert(ins.first->second == NULL_TREE);
8972 ins.first->second = gogo->interface_method_table_for_type(interface,
8973 type,
8974 is_pointer);
8975 }
8976
8977 tree decl = ins.first->second;
8978 if (decl == error_mark_node)
8979 return error_mark_node;
8980 go_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
8981 return build_fold_addr_expr(decl);
8982 }
8983
8984 // Look for field or method NAME for TYPE. Return an Expression for
8985 // the field or method bound to EXPR. If there is no such field or
8986 // method, give an appropriate error and return an error expression.
8987
8988 Expression*
8989 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
8990 const std::string& name,
8991 Location location)
8992 {
8993 if (type->deref()->is_error_type())
8994 return Expression::make_error(location);
8995
8996 const Named_type* nt = type->deref()->named_type();
8997 const Struct_type* st = type->deref()->struct_type();
8998 const Interface_type* it = type->interface_type();
8999
9000 // If this is a pointer to a pointer, then it is possible that the
9001 // pointed-to type has methods.
9002 bool dereferenced = false;
9003 if (nt == NULL
9004 && st == NULL
9005 && it == NULL
9006 && type->points_to() != NULL
9007 && type->points_to()->points_to() != NULL)
9008 {
9009 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
9010 type = type->points_to();
9011 if (type->deref()->is_error_type())
9012 return Expression::make_error(location);
9013 nt = type->points_to()->named_type();
9014 st = type->points_to()->struct_type();
9015 dereferenced = true;
9016 }
9017
9018 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
9019 || expr->is_addressable());
9020 std::vector<const Named_type*> seen;
9021 bool is_method = false;
9022 bool found_pointer_method = false;
9023 std::string ambig1;
9024 std::string ambig2;
9025 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
9026 &seen, NULL, &is_method,
9027 &found_pointer_method, &ambig1, &ambig2))
9028 {
9029 Expression* ret;
9030 if (!is_method)
9031 {
9032 go_assert(st != NULL);
9033 if (type->struct_type() == NULL)
9034 {
9035 go_assert(type->points_to() != NULL);
9036 expr = Expression::make_unary(OPERATOR_MULT, expr,
9037 location);
9038 go_assert(expr->type()->struct_type() == st);
9039 }
9040 ret = st->field_reference(expr, name, location);
9041 }
9042 else if (it != NULL && it->find_method(name) != NULL)
9043 ret = Expression::make_interface_field_reference(expr, name,
9044 location);
9045 else
9046 {
9047 Method* m;
9048 if (nt != NULL)
9049 m = nt->method_function(name, NULL);
9050 else if (st != NULL)
9051 m = st->method_function(name, NULL);
9052 else
9053 go_unreachable();
9054 go_assert(m != NULL);
9055 if (dereferenced && m->is_value_method())
9056 {
9057 error_at(location,
9058 "calling value method requires explicit dereference");
9059 return Expression::make_error(location);
9060 }
9061 if (!m->is_value_method() && expr->type()->points_to() == NULL)
9062 expr = Expression::make_unary(OPERATOR_AND, expr, location);
9063 ret = m->bind_method(expr, location);
9064 }
9065 go_assert(ret != NULL);
9066 return ret;
9067 }
9068 else
9069 {
9070 if (!ambig1.empty())
9071 error_at(location, "%qs is ambiguous via %qs and %qs",
9072 Gogo::message_name(name).c_str(), ambig1.c_str(),
9073 ambig2.c_str());
9074 else if (found_pointer_method)
9075 error_at(location, "method requires a pointer receiver");
9076 else if (nt == NULL && st == NULL && it == NULL)
9077 error_at(location,
9078 ("reference to field %qs in object which "
9079 "has no fields or methods"),
9080 Gogo::message_name(name).c_str());
9081 else
9082 {
9083 bool is_unexported;
9084 if (!Gogo::is_hidden_name(name))
9085 is_unexported = false;
9086 else
9087 {
9088 std::string unpacked = Gogo::unpack_hidden_name(name);
9089 seen.clear();
9090 is_unexported = Type::is_unexported_field_or_method(gogo, type,
9091 unpacked,
9092 &seen);
9093 }
9094 if (is_unexported)
9095 error_at(location, "reference to unexported field or method %qs",
9096 Gogo::message_name(name).c_str());
9097 else
9098 error_at(location, "reference to undefined field or method %qs",
9099 Gogo::message_name(name).c_str());
9100 }
9101 return Expression::make_error(location);
9102 }
9103 }
9104
9105 // Look in TYPE for a field or method named NAME, return true if one
9106 // is found. This looks through embedded anonymous fields and handles
9107 // ambiguity. If a method is found, sets *IS_METHOD to true;
9108 // otherwise, if a field is found, set it to false. If
9109 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9110 // whose address can not be taken. SEEN is used to avoid infinite
9111 // recursion on invalid types.
9112
9113 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9114 // method we couldn't use because it requires a pointer. LEVEL is
9115 // used for recursive calls, and can be NULL for a non-recursive call.
9116 // When this function returns false because it finds that the name is
9117 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9118 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9119 // will be unchanged.
9120
9121 // This function just returns whether or not there is a field or
9122 // method, and whether it is a field or method. It doesn't build an
9123 // expression to refer to it. If it is a method, we then look in the
9124 // list of all methods for the type. If it is a field, the search has
9125 // to be done again, looking only for fields, and building up the
9126 // expression as we go.
9127
9128 bool
9129 Type::find_field_or_method(const Type* type,
9130 const std::string& name,
9131 bool receiver_can_be_pointer,
9132 std::vector<const Named_type*>* seen,
9133 int* level,
9134 bool* is_method,
9135 bool* found_pointer_method,
9136 std::string* ambig1,
9137 std::string* ambig2)
9138 {
9139 // Named types can have locally defined methods.
9140 const Named_type* nt = type->named_type();
9141 if (nt == NULL && type->points_to() != NULL)
9142 nt = type->points_to()->named_type();
9143 if (nt != NULL)
9144 {
9145 Named_object* no = nt->find_local_method(name);
9146 if (no != NULL)
9147 {
9148 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
9149 {
9150 *is_method = true;
9151 return true;
9152 }
9153
9154 // Record that we have found a pointer method in order to
9155 // give a better error message if we don't find anything
9156 // else.
9157 *found_pointer_method = true;
9158 }
9159
9160 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9161 p != seen->end();
9162 ++p)
9163 {
9164 if (*p == nt)
9165 {
9166 // We've already seen this type when searching for methods.
9167 return false;
9168 }
9169 }
9170 }
9171
9172 // Interface types can have methods.
9173 const Interface_type* it = type->interface_type();
9174 if (it != NULL && it->find_method(name) != NULL)
9175 {
9176 *is_method = true;
9177 return true;
9178 }
9179
9180 // Struct types can have fields. They can also inherit fields and
9181 // methods from anonymous fields.
9182 const Struct_type* st = type->deref()->struct_type();
9183 if (st == NULL)
9184 return false;
9185 const Struct_field_list* fields = st->fields();
9186 if (fields == NULL)
9187 return false;
9188
9189 if (nt != NULL)
9190 seen->push_back(nt);
9191
9192 int found_level = 0;
9193 bool found_is_method = false;
9194 std::string found_ambig1;
9195 std::string found_ambig2;
9196 const Struct_field* found_parent = NULL;
9197 for (Struct_field_list::const_iterator pf = fields->begin();
9198 pf != fields->end();
9199 ++pf)
9200 {
9201 if (pf->is_field_name(name))
9202 {
9203 *is_method = false;
9204 if (nt != NULL)
9205 seen->pop_back();
9206 return true;
9207 }
9208
9209 if (!pf->is_anonymous())
9210 continue;
9211
9212 if (pf->type()->deref()->is_error_type()
9213 || pf->type()->deref()->is_undefined())
9214 continue;
9215
9216 Named_type* fnt = pf->type()->named_type();
9217 if (fnt == NULL)
9218 fnt = pf->type()->deref()->named_type();
9219 go_assert(fnt != NULL);
9220
9221 int sublevel = level == NULL ? 1 : *level + 1;
9222 bool sub_is_method;
9223 std::string subambig1;
9224 std::string subambig2;
9225 bool subfound = Type::find_field_or_method(fnt,
9226 name,
9227 receiver_can_be_pointer,
9228 seen,
9229 &sublevel,
9230 &sub_is_method,
9231 found_pointer_method,
9232 &subambig1,
9233 &subambig2);
9234 if (!subfound)
9235 {
9236 if (!subambig1.empty())
9237 {
9238 // The name was found via this field, but is ambiguous.
9239 // if the ambiguity is lower or at the same level as
9240 // anything else we have already found, then we want to
9241 // pass the ambiguity back to the caller.
9242 if (found_level == 0 || sublevel <= found_level)
9243 {
9244 found_ambig1 = (Gogo::message_name(pf->field_name())
9245 + '.' + subambig1);
9246 found_ambig2 = (Gogo::message_name(pf->field_name())
9247 + '.' + subambig2);
9248 found_level = sublevel;
9249 }
9250 }
9251 }
9252 else
9253 {
9254 // The name was found via this field. Use the level to see
9255 // if we want to use this one, or whether it introduces an
9256 // ambiguity.
9257 if (found_level == 0 || sublevel < found_level)
9258 {
9259 found_level = sublevel;
9260 found_is_method = sub_is_method;
9261 found_ambig1.clear();
9262 found_ambig2.clear();
9263 found_parent = &*pf;
9264 }
9265 else if (sublevel > found_level)
9266 ;
9267 else if (found_ambig1.empty())
9268 {
9269 // We found an ambiguity.
9270 go_assert(found_parent != NULL);
9271 found_ambig1 = Gogo::message_name(found_parent->field_name());
9272 found_ambig2 = Gogo::message_name(pf->field_name());
9273 }
9274 else
9275 {
9276 // We found an ambiguity, but we already know of one.
9277 // Just report the earlier one.
9278 }
9279 }
9280 }
9281
9282 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
9283 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
9284 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
9285 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
9286
9287 if (nt != NULL)
9288 seen->pop_back();
9289
9290 if (found_level == 0)
9291 return false;
9292 else if (!found_ambig1.empty())
9293 {
9294 go_assert(!found_ambig1.empty());
9295 ambig1->assign(found_ambig1);
9296 ambig2->assign(found_ambig2);
9297 if (level != NULL)
9298 *level = found_level;
9299 return false;
9300 }
9301 else
9302 {
9303 if (level != NULL)
9304 *level = found_level;
9305 *is_method = found_is_method;
9306 return true;
9307 }
9308 }
9309
9310 // Return whether NAME is an unexported field or method for TYPE.
9311
9312 bool
9313 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
9314 const std::string& name,
9315 std::vector<const Named_type*>* seen)
9316 {
9317 const Named_type* nt = type->named_type();
9318 if (nt == NULL)
9319 nt = type->deref()->named_type();
9320 if (nt != NULL)
9321 {
9322 if (nt->is_unexported_local_method(gogo, name))
9323 return true;
9324
9325 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9326 p != seen->end();
9327 ++p)
9328 {
9329 if (*p == nt)
9330 {
9331 // We've already seen this type.
9332 return false;
9333 }
9334 }
9335 }
9336
9337 const Interface_type* it = type->interface_type();
9338 if (it != NULL && it->is_unexported_method(gogo, name))
9339 return true;
9340
9341 type = type->deref();
9342
9343 const Struct_type* st = type->struct_type();
9344 if (st != NULL && st->is_unexported_local_field(gogo, name))
9345 return true;
9346
9347 if (st == NULL)
9348 return false;
9349
9350 const Struct_field_list* fields = st->fields();
9351 if (fields == NULL)
9352 return false;
9353
9354 if (nt != NULL)
9355 seen->push_back(nt);
9356
9357 for (Struct_field_list::const_iterator pf = fields->begin();
9358 pf != fields->end();
9359 ++pf)
9360 {
9361 if (pf->is_anonymous()
9362 && !pf->type()->deref()->is_error_type()
9363 && !pf->type()->deref()->is_undefined())
9364 {
9365 Named_type* subtype = pf->type()->named_type();
9366 if (subtype == NULL)
9367 subtype = pf->type()->deref()->named_type();
9368 if (subtype == NULL)
9369 {
9370 // This is an error, but it will be diagnosed elsewhere.
9371 continue;
9372 }
9373 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
9374 {
9375 if (nt != NULL)
9376 seen->pop_back();
9377 return true;
9378 }
9379 }
9380 }
9381
9382 if (nt != NULL)
9383 seen->pop_back();
9384
9385 return false;
9386 }
9387
9388 // Class Forward_declaration.
9389
9390 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
9391 : Type(TYPE_FORWARD),
9392 named_object_(named_object->resolve()), warned_(false)
9393 {
9394 go_assert(this->named_object_->is_unknown()
9395 || this->named_object_->is_type_declaration());
9396 }
9397
9398 // Return the named object.
9399
9400 Named_object*
9401 Forward_declaration_type::named_object()
9402 {
9403 return this->named_object_->resolve();
9404 }
9405
9406 const Named_object*
9407 Forward_declaration_type::named_object() const
9408 {
9409 return this->named_object_->resolve();
9410 }
9411
9412 // Return the name of the forward declared type.
9413
9414 const std::string&
9415 Forward_declaration_type::name() const
9416 {
9417 return this->named_object()->name();
9418 }
9419
9420 // Warn about a use of a type which has been declared but not defined.
9421
9422 void
9423 Forward_declaration_type::warn() const
9424 {
9425 Named_object* no = this->named_object_->resolve();
9426 if (no->is_unknown())
9427 {
9428 // The name was not defined anywhere.
9429 if (!this->warned_)
9430 {
9431 error_at(this->named_object_->location(),
9432 "use of undefined type %qs",
9433 no->message_name().c_str());
9434 this->warned_ = true;
9435 }
9436 }
9437 else if (no->is_type_declaration())
9438 {
9439 // The name was seen as a type, but the type was never defined.
9440 if (no->type_declaration_value()->using_type())
9441 {
9442 error_at(this->named_object_->location(),
9443 "use of undefined type %qs",
9444 no->message_name().c_str());
9445 this->warned_ = true;
9446 }
9447 }
9448 else
9449 {
9450 // The name was defined, but not as a type.
9451 if (!this->warned_)
9452 {
9453 error_at(this->named_object_->location(), "expected type");
9454 this->warned_ = true;
9455 }
9456 }
9457 }
9458
9459 // Get the base type of a declaration. This gives an error if the
9460 // type has not yet been defined.
9461
9462 Type*
9463 Forward_declaration_type::real_type()
9464 {
9465 if (this->is_defined())
9466 return this->named_object()->type_value();
9467 else
9468 {
9469 this->warn();
9470 return Type::make_error_type();
9471 }
9472 }
9473
9474 const Type*
9475 Forward_declaration_type::real_type() const
9476 {
9477 if (this->is_defined())
9478 return this->named_object()->type_value();
9479 else
9480 {
9481 this->warn();
9482 return Type::make_error_type();
9483 }
9484 }
9485
9486 // Return whether the base type is defined.
9487
9488 bool
9489 Forward_declaration_type::is_defined() const
9490 {
9491 return this->named_object()->is_type();
9492 }
9493
9494 // Add a method. This is used when methods are defined before the
9495 // type.
9496
9497 Named_object*
9498 Forward_declaration_type::add_method(const std::string& name,
9499 Function* function)
9500 {
9501 Named_object* no = this->named_object();
9502 if (no->is_unknown())
9503 no->declare_as_type();
9504 return no->type_declaration_value()->add_method(name, function);
9505 }
9506
9507 // Add a method declaration. This is used when methods are declared
9508 // before the type.
9509
9510 Named_object*
9511 Forward_declaration_type::add_method_declaration(const std::string& name,
9512 Package* package,
9513 Function_type* type,
9514 Location location)
9515 {
9516 Named_object* no = this->named_object();
9517 if (no->is_unknown())
9518 no->declare_as_type();
9519 Type_declaration* td = no->type_declaration_value();
9520 return td->add_method_declaration(name, package, type, location);
9521 }
9522
9523 // Traversal.
9524
9525 int
9526 Forward_declaration_type::do_traverse(Traverse* traverse)
9527 {
9528 if (this->is_defined()
9529 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
9530 return TRAVERSE_EXIT;
9531 return TRAVERSE_CONTINUE;
9532 }
9533
9534 // Get the backend representation for the type.
9535
9536 Btype*
9537 Forward_declaration_type::do_get_backend(Gogo* gogo)
9538 {
9539 if (this->is_defined())
9540 return Type::get_named_base_btype(gogo, this->real_type());
9541
9542 if (this->warned_)
9543 return gogo->backend()->error_type();
9544
9545 // We represent an undefined type as a struct with no fields. That
9546 // should work fine for the backend, since the same case can arise
9547 // in C.
9548 std::vector<Backend::Btyped_identifier> fields;
9549 Btype* bt = gogo->backend()->struct_type(fields);
9550 return gogo->backend()->named_type(this->name(), bt,
9551 this->named_object()->location());
9552 }
9553
9554 // Build a type descriptor for a forwarded type.
9555
9556 Expression*
9557 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9558 {
9559 Location ploc = Linemap::predeclared_location();
9560 if (!this->is_defined())
9561 return Expression::make_error(ploc);
9562 else
9563 {
9564 Type* t = this->real_type();
9565 if (name != NULL)
9566 return this->named_type_descriptor(gogo, t, name);
9567 else
9568 return Expression::make_type_descriptor(t, ploc);
9569 }
9570 }
9571
9572 // The reflection string.
9573
9574 void
9575 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
9576 {
9577 this->append_reflection(this->real_type(), gogo, ret);
9578 }
9579
9580 // The mangled name.
9581
9582 void
9583 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
9584 {
9585 if (this->is_defined())
9586 this->append_mangled_name(this->real_type(), gogo, ret);
9587 else
9588 {
9589 const Named_object* no = this->named_object();
9590 std::string name;
9591 if (no->package() == NULL)
9592 name = gogo->pkgpath_symbol();
9593 else
9594 name = no->package()->pkgpath_symbol();
9595 name += '.';
9596 name += Gogo::unpack_hidden_name(no->name());
9597 char buf[20];
9598 snprintf(buf, sizeof buf, "N%u_",
9599 static_cast<unsigned int>(name.length()));
9600 ret->append(buf);
9601 ret->append(name);
9602 }
9603 }
9604
9605 // Export a forward declaration. This can happen when a defined type
9606 // refers to a type which is only declared (and is presumably defined
9607 // in some other file in the same package).
9608
9609 void
9610 Forward_declaration_type::do_export(Export*) const
9611 {
9612 // If there is a base type, that should be exported instead of this.
9613 go_assert(!this->is_defined());
9614
9615 // We don't output anything.
9616 }
9617
9618 // Make a forward declaration.
9619
9620 Type*
9621 Type::make_forward_declaration(Named_object* named_object)
9622 {
9623 return new Forward_declaration_type(named_object);
9624 }
9625
9626 // Class Typed_identifier_list.
9627
9628 // Sort the entries by name.
9629
9630 struct Typed_identifier_list_sort
9631 {
9632 public:
9633 bool
9634 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
9635 { return t1.name() < t2.name(); }
9636 };
9637
9638 void
9639 Typed_identifier_list::sort_by_name()
9640 {
9641 std::sort(this->entries_.begin(), this->entries_.end(),
9642 Typed_identifier_list_sort());
9643 }
9644
9645 // Traverse types.
9646
9647 int
9648 Typed_identifier_list::traverse(Traverse* traverse)
9649 {
9650 for (Typed_identifier_list::const_iterator p = this->begin();
9651 p != this->end();
9652 ++p)
9653 {
9654 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
9655 return TRAVERSE_EXIT;
9656 }
9657 return TRAVERSE_CONTINUE;
9658 }
9659
9660 // Copy the list.
9661
9662 Typed_identifier_list*
9663 Typed_identifier_list::copy() const
9664 {
9665 Typed_identifier_list* ret = new Typed_identifier_list();
9666 for (Typed_identifier_list::const_iterator p = this->begin();
9667 p != this->end();
9668 ++p)
9669 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
9670 return ret;
9671 }