compiler: Better error message if method requires pointer receiver.
[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 // Convert struct fields to the backend representation. This is not
4558 // declared in types.h so that types.h doesn't have to #include
4559 // backend.h.
4560
4561 static void
4562 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
4563 bool use_placeholder,
4564 std::vector<Backend::Btyped_identifier>* bfields)
4565 {
4566 bfields->resize(fields->size());
4567 size_t i = 0;
4568 for (Struct_field_list::const_iterator p = fields->begin();
4569 p != fields->end();
4570 ++p, ++i)
4571 {
4572 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
4573 (*bfields)[i].btype = (use_placeholder
4574 ? p->type()->get_backend_placeholder(gogo)
4575 : p->type()->get_backend(gogo));
4576 (*bfields)[i].location = p->location();
4577 }
4578 go_assert(i == fields->size());
4579 }
4580
4581 // Get the tree for a struct type.
4582
4583 Btype*
4584 Struct_type::do_get_backend(Gogo* gogo)
4585 {
4586 std::vector<Backend::Btyped_identifier> bfields;
4587 get_backend_struct_fields(gogo, this->fields_, false, &bfields);
4588 return gogo->backend()->struct_type(bfields);
4589 }
4590
4591 // Finish the backend representation of the fields of a struct.
4592
4593 void
4594 Struct_type::finish_backend_fields(Gogo* gogo)
4595 {
4596 const Struct_field_list* fields = this->fields_;
4597 if (fields != NULL)
4598 {
4599 for (Struct_field_list::const_iterator p = fields->begin();
4600 p != fields->end();
4601 ++p)
4602 p->type()->get_backend(gogo);
4603 }
4604 }
4605
4606 // The type of a struct type descriptor.
4607
4608 Type*
4609 Struct_type::make_struct_type_descriptor_type()
4610 {
4611 static Type* ret;
4612 if (ret == NULL)
4613 {
4614 Type* tdt = Type::make_type_descriptor_type();
4615 Type* ptdt = Type::make_type_descriptor_ptr_type();
4616
4617 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4618 Type* string_type = Type::lookup_string_type();
4619 Type* pointer_string_type = Type::make_pointer_type(string_type);
4620
4621 Struct_type* sf =
4622 Type::make_builtin_struct_type(5,
4623 "name", pointer_string_type,
4624 "pkgPath", pointer_string_type,
4625 "typ", ptdt,
4626 "tag", pointer_string_type,
4627 "offset", uintptr_type);
4628 Type* nsf = Type::make_builtin_named_type("structField", sf);
4629
4630 Type* slice_type = Type::make_array_type(nsf, NULL);
4631
4632 Struct_type* s = Type::make_builtin_struct_type(2,
4633 "", tdt,
4634 "fields", slice_type);
4635
4636 ret = Type::make_builtin_named_type("StructType", s);
4637 }
4638
4639 return ret;
4640 }
4641
4642 // Build a type descriptor for a struct type.
4643
4644 Expression*
4645 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4646 {
4647 Location bloc = Linemap::predeclared_location();
4648
4649 Type* stdt = Struct_type::make_struct_type_descriptor_type();
4650
4651 const Struct_field_list* fields = stdt->struct_type()->fields();
4652
4653 Expression_list* vals = new Expression_list();
4654 vals->reserve(2);
4655
4656 const Methods* methods = this->methods();
4657 // A named struct should not have methods--the methods should attach
4658 // to the named type.
4659 go_assert(methods == NULL || name == NULL);
4660
4661 Struct_field_list::const_iterator ps = fields->begin();
4662 go_assert(ps->is_field_name("commonType"));
4663 vals->push_back(this->type_descriptor_constructor(gogo,
4664 RUNTIME_TYPE_KIND_STRUCT,
4665 name, methods, true));
4666
4667 ++ps;
4668 go_assert(ps->is_field_name("fields"));
4669
4670 Expression_list* elements = new Expression_list();
4671 elements->reserve(this->fields_->size());
4672 Type* element_type = ps->type()->array_type()->element_type();
4673 for (Struct_field_list::const_iterator pf = this->fields_->begin();
4674 pf != this->fields_->end();
4675 ++pf)
4676 {
4677 const Struct_field_list* f = element_type->struct_type()->fields();
4678
4679 Expression_list* fvals = new Expression_list();
4680 fvals->reserve(5);
4681
4682 Struct_field_list::const_iterator q = f->begin();
4683 go_assert(q->is_field_name("name"));
4684 if (pf->is_anonymous())
4685 fvals->push_back(Expression::make_nil(bloc));
4686 else
4687 {
4688 std::string n = Gogo::unpack_hidden_name(pf->field_name());
4689 Expression* s = Expression::make_string(n, bloc);
4690 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4691 }
4692
4693 ++q;
4694 go_assert(q->is_field_name("pkgPath"));
4695 if (!Gogo::is_hidden_name(pf->field_name()))
4696 fvals->push_back(Expression::make_nil(bloc));
4697 else
4698 {
4699 std::string n = Gogo::hidden_name_pkgpath(pf->field_name());
4700 Expression* s = Expression::make_string(n, bloc);
4701 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4702 }
4703
4704 ++q;
4705 go_assert(q->is_field_name("typ"));
4706 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
4707
4708 ++q;
4709 go_assert(q->is_field_name("tag"));
4710 if (!pf->has_tag())
4711 fvals->push_back(Expression::make_nil(bloc));
4712 else
4713 {
4714 Expression* s = Expression::make_string(pf->tag(), bloc);
4715 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4716 }
4717
4718 ++q;
4719 go_assert(q->is_field_name("offset"));
4720 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
4721
4722 Expression* v = Expression::make_struct_composite_literal(element_type,
4723 fvals, bloc);
4724 elements->push_back(v);
4725 }
4726
4727 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
4728 elements, bloc));
4729
4730 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4731 }
4732
4733 // Write the hash function for a struct which can not use the identity
4734 // function.
4735
4736 void
4737 Struct_type::write_hash_function(Gogo* gogo, Named_type*,
4738 Function_type* hash_fntype,
4739 Function_type* equal_fntype)
4740 {
4741 Location bloc = Linemap::predeclared_location();
4742
4743 // The pointer to the struct that we are going to hash. This is an
4744 // argument to the hash function we are implementing here.
4745 Named_object* key_arg = gogo->lookup("key", NULL);
4746 go_assert(key_arg != NULL);
4747 Type* key_arg_type = key_arg->var_value()->type();
4748
4749 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4750
4751 // Get a 0.
4752 mpz_t ival;
4753 mpz_init_set_ui(ival, 0);
4754 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
4755 mpz_clear(ival);
4756
4757 // Make a temporary to hold the return value, initialized to 0.
4758 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
4759 bloc);
4760 gogo->add_statement(retval);
4761
4762 // Make a temporary to hold the key as a uintptr.
4763 Expression* ref = Expression::make_var_reference(key_arg, bloc);
4764 ref = Expression::make_cast(uintptr_type, ref, bloc);
4765 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
4766 bloc);
4767 gogo->add_statement(key);
4768
4769 // Loop over the struct fields.
4770 bool first = true;
4771 const Struct_field_list* fields = this->fields_;
4772 for (Struct_field_list::const_iterator pf = fields->begin();
4773 pf != fields->end();
4774 ++pf)
4775 {
4776 if (Gogo::is_sink_name(pf->field_name()))
4777 continue;
4778
4779 if (first)
4780 first = false;
4781 else
4782 {
4783 // Multiply retval by 33.
4784 mpz_init_set_ui(ival, 33);
4785 Expression* i33 = Expression::make_integer(&ival, uintptr_type,
4786 bloc);
4787 mpz_clear(ival);
4788
4789 ref = Expression::make_temporary_reference(retval, bloc);
4790 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ,
4791 ref, i33, bloc);
4792 gogo->add_statement(s);
4793 }
4794
4795 // Get a pointer to the value of this field.
4796 Expression* offset = Expression::make_struct_field_offset(this, &*pf);
4797 ref = Expression::make_temporary_reference(key, bloc);
4798 Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
4799 bloc);
4800 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
4801
4802 // Get the size of this field.
4803 Expression* size = Expression::make_type_info(pf->type(),
4804 Expression::TYPE_INFO_SIZE);
4805
4806 // Get the hash function to use for the type of this field.
4807 Named_object* hash_fn;
4808 Named_object* equal_fn;
4809 pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
4810 equal_fntype, &hash_fn, &equal_fn);
4811
4812 // Call the hash function for the field.
4813 Expression_list* args = new Expression_list();
4814 args->push_back(subkey);
4815 args->push_back(size);
4816 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
4817 Expression* call = Expression::make_call(func, args, false, bloc);
4818
4819 // Add the field's hash value to retval.
4820 Temporary_reference_expression* tref =
4821 Expression::make_temporary_reference(retval, bloc);
4822 tref->set_is_lvalue();
4823 Statement* s = Statement::make_assignment_operation(OPERATOR_PLUSEQ,
4824 tref, call, bloc);
4825 gogo->add_statement(s);
4826 }
4827
4828 // Return retval to the caller of the hash function.
4829 Expression_list* vals = new Expression_list();
4830 ref = Expression::make_temporary_reference(retval, bloc);
4831 vals->push_back(ref);
4832 Statement* s = Statement::make_return_statement(vals, bloc);
4833 gogo->add_statement(s);
4834 }
4835
4836 // Write the equality function for a struct which can not use the
4837 // identity function.
4838
4839 void
4840 Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
4841 {
4842 Location bloc = Linemap::predeclared_location();
4843
4844 // The pointers to the structs we are going to compare.
4845 Named_object* key1_arg = gogo->lookup("key1", NULL);
4846 Named_object* key2_arg = gogo->lookup("key2", NULL);
4847 go_assert(key1_arg != NULL && key2_arg != NULL);
4848
4849 // Build temporaries with the right types.
4850 Type* pt = Type::make_pointer_type(name != NULL
4851 ? static_cast<Type*>(name)
4852 : static_cast<Type*>(this));
4853
4854 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
4855 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4856 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
4857 gogo->add_statement(p1);
4858
4859 ref = Expression::make_var_reference(key2_arg, bloc);
4860 ref = Expression::make_unsafe_cast(pt, ref, bloc);
4861 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
4862 gogo->add_statement(p2);
4863
4864 const Struct_field_list* fields = this->fields_;
4865 unsigned int field_index = 0;
4866 for (Struct_field_list::const_iterator pf = fields->begin();
4867 pf != fields->end();
4868 ++pf, ++field_index)
4869 {
4870 if (Gogo::is_sink_name(pf->field_name()))
4871 continue;
4872
4873 // Compare one field in both P1 and P2.
4874 Expression* f1 = Expression::make_temporary_reference(p1, bloc);
4875 f1 = Expression::make_unary(OPERATOR_MULT, f1, bloc);
4876 f1 = Expression::make_field_reference(f1, field_index, bloc);
4877
4878 Expression* f2 = Expression::make_temporary_reference(p2, bloc);
4879 f2 = Expression::make_unary(OPERATOR_MULT, f2, bloc);
4880 f2 = Expression::make_field_reference(f2, field_index, bloc);
4881
4882 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
4883
4884 // If the values are not equal, return false.
4885 gogo->start_block(bloc);
4886 Expression_list* vals = new Expression_list();
4887 vals->push_back(Expression::make_boolean(false, bloc));
4888 Statement* s = Statement::make_return_statement(vals, bloc);
4889 gogo->add_statement(s);
4890 Block* then_block = gogo->finish_block(bloc);
4891
4892 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
4893 gogo->add_statement(s);
4894 }
4895
4896 // All the fields are equal, so return true.
4897 Expression_list* vals = new Expression_list();
4898 vals->push_back(Expression::make_boolean(true, bloc));
4899 Statement* s = Statement::make_return_statement(vals, bloc);
4900 gogo->add_statement(s);
4901 }
4902
4903 // Reflection string.
4904
4905 void
4906 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
4907 {
4908 ret->append("struct { ");
4909
4910 for (Struct_field_list::const_iterator p = this->fields_->begin();
4911 p != this->fields_->end();
4912 ++p)
4913 {
4914 if (p != this->fields_->begin())
4915 ret->append("; ");
4916 if (p->is_anonymous())
4917 ret->push_back('?');
4918 else
4919 ret->append(Gogo::unpack_hidden_name(p->field_name()));
4920 ret->push_back(' ');
4921 this->append_reflection(p->type(), gogo, ret);
4922
4923 if (p->has_tag())
4924 {
4925 const std::string& tag(p->tag());
4926 ret->append(" \"");
4927 for (std::string::const_iterator p = tag.begin();
4928 p != tag.end();
4929 ++p)
4930 {
4931 if (*p == '\0')
4932 ret->append("\\x00");
4933 else if (*p == '\n')
4934 ret->append("\\n");
4935 else if (*p == '\t')
4936 ret->append("\\t");
4937 else if (*p == '"')
4938 ret->append("\\\"");
4939 else if (*p == '\\')
4940 ret->append("\\\\");
4941 else
4942 ret->push_back(*p);
4943 }
4944 ret->push_back('"');
4945 }
4946 }
4947
4948 ret->append(" }");
4949 }
4950
4951 // Mangled name.
4952
4953 void
4954 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4955 {
4956 ret->push_back('S');
4957
4958 const Struct_field_list* fields = this->fields_;
4959 if (fields != NULL)
4960 {
4961 for (Struct_field_list::const_iterator p = fields->begin();
4962 p != fields->end();
4963 ++p)
4964 {
4965 if (p->is_anonymous())
4966 ret->append("0_");
4967 else
4968 {
4969 std::string n = Gogo::unpack_hidden_name(p->field_name());
4970 char buf[20];
4971 snprintf(buf, sizeof buf, "%u_",
4972 static_cast<unsigned int>(n.length()));
4973 ret->append(buf);
4974 ret->append(n);
4975 }
4976 this->append_mangled_name(p->type(), gogo, ret);
4977 if (p->has_tag())
4978 {
4979 const std::string& tag(p->tag());
4980 std::string out;
4981 for (std::string::const_iterator p = tag.begin();
4982 p != tag.end();
4983 ++p)
4984 {
4985 if (ISALNUM(*p) || *p == '_')
4986 out.push_back(*p);
4987 else
4988 {
4989 char buf[20];
4990 snprintf(buf, sizeof buf, ".%x.",
4991 static_cast<unsigned int>(*p));
4992 out.append(buf);
4993 }
4994 }
4995 char buf[20];
4996 snprintf(buf, sizeof buf, "T%u_",
4997 static_cast<unsigned int>(out.length()));
4998 ret->append(buf);
4999 ret->append(out);
5000 }
5001 }
5002 }
5003
5004 ret->push_back('e');
5005 }
5006
5007 // If the offset of field INDEX in the backend implementation can be
5008 // determined, set *POFFSET to the offset in bytes and return true.
5009 // Otherwise, return false.
5010
5011 bool
5012 Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
5013 unsigned int* poffset)
5014 {
5015 if (!this->is_backend_type_size_known(gogo))
5016 return false;
5017 Btype* bt = this->get_backend_placeholder(gogo);
5018 size_t offset = gogo->backend()->type_field_offset(bt, index);
5019 *poffset = static_cast<unsigned int>(offset);
5020 if (*poffset != offset)
5021 return false;
5022 return true;
5023 }
5024
5025 // Export.
5026
5027 void
5028 Struct_type::do_export(Export* exp) const
5029 {
5030 exp->write_c_string("struct { ");
5031 const Struct_field_list* fields = this->fields_;
5032 go_assert(fields != NULL);
5033 for (Struct_field_list::const_iterator p = fields->begin();
5034 p != fields->end();
5035 ++p)
5036 {
5037 if (p->is_anonymous())
5038 exp->write_string("? ");
5039 else
5040 {
5041 exp->write_string(p->field_name());
5042 exp->write_c_string(" ");
5043 }
5044 exp->write_type(p->type());
5045
5046 if (p->has_tag())
5047 {
5048 exp->write_c_string(" ");
5049 Expression* expr =
5050 Expression::make_string(p->tag(), Linemap::predeclared_location());
5051 expr->export_expression(exp);
5052 delete expr;
5053 }
5054
5055 exp->write_c_string("; ");
5056 }
5057 exp->write_c_string("}");
5058 }
5059
5060 // Import.
5061
5062 Struct_type*
5063 Struct_type::do_import(Import* imp)
5064 {
5065 imp->require_c_string("struct { ");
5066 Struct_field_list* fields = new Struct_field_list;
5067 if (imp->peek_char() != '}')
5068 {
5069 while (true)
5070 {
5071 std::string name;
5072 if (imp->match_c_string("? "))
5073 imp->advance(2);
5074 else
5075 {
5076 name = imp->read_identifier();
5077 imp->require_c_string(" ");
5078 }
5079 Type* ftype = imp->read_type();
5080
5081 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
5082
5083 if (imp->peek_char() == ' ')
5084 {
5085 imp->advance(1);
5086 Expression* expr = Expression::import_expression(imp);
5087 String_expression* sexpr = expr->string_expression();
5088 go_assert(sexpr != NULL);
5089 sf.set_tag(sexpr->val());
5090 delete sexpr;
5091 }
5092
5093 imp->require_c_string("; ");
5094 fields->push_back(sf);
5095 if (imp->peek_char() == '}')
5096 break;
5097 }
5098 }
5099 imp->require_c_string("}");
5100
5101 return Type::make_struct_type(fields, imp->location());
5102 }
5103
5104 // Make a struct type.
5105
5106 Struct_type*
5107 Type::make_struct_type(Struct_field_list* fields,
5108 Location location)
5109 {
5110 return new Struct_type(fields, location);
5111 }
5112
5113 // Class Array_type.
5114
5115 // Whether two array types are identical.
5116
5117 bool
5118 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
5119 {
5120 if (!Type::are_identical(this->element_type(), t->element_type(),
5121 errors_are_identical, NULL))
5122 return false;
5123
5124 Expression* l1 = this->length();
5125 Expression* l2 = t->length();
5126
5127 // Slices of the same element type are identical.
5128 if (l1 == NULL && l2 == NULL)
5129 return true;
5130
5131 // Arrays of the same element type are identical if they have the
5132 // same length.
5133 if (l1 != NULL && l2 != NULL)
5134 {
5135 if (l1 == l2)
5136 return true;
5137
5138 // Try to determine the lengths. If we can't, assume the arrays
5139 // are not identical.
5140 bool ret = false;
5141 Numeric_constant nc1, nc2;
5142 if (l1->numeric_constant_value(&nc1)
5143 && l2->numeric_constant_value(&nc2))
5144 {
5145 mpz_t v1;
5146 if (nc1.to_int(&v1))
5147 {
5148 mpz_t v2;
5149 if (nc2.to_int(&v2))
5150 {
5151 ret = mpz_cmp(v1, v2) == 0;
5152 mpz_clear(v2);
5153 }
5154 mpz_clear(v1);
5155 }
5156 }
5157 return ret;
5158 }
5159
5160 // Otherwise the arrays are not identical.
5161 return false;
5162 }
5163
5164 // Traversal.
5165
5166 int
5167 Array_type::do_traverse(Traverse* traverse)
5168 {
5169 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
5170 return TRAVERSE_EXIT;
5171 if (this->length_ != NULL
5172 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
5173 return TRAVERSE_EXIT;
5174 return TRAVERSE_CONTINUE;
5175 }
5176
5177 // Check that the length is valid.
5178
5179 bool
5180 Array_type::verify_length()
5181 {
5182 if (this->length_ == NULL)
5183 return true;
5184
5185 Type_context context(Type::lookup_integer_type("int"), false);
5186 this->length_->determine_type(&context);
5187
5188 if (!this->length_->is_constant())
5189 {
5190 error_at(this->length_->location(), "array bound is not constant");
5191 return false;
5192 }
5193
5194 Numeric_constant nc;
5195 if (!this->length_->numeric_constant_value(&nc))
5196 {
5197 if (this->length_->type()->integer_type() != NULL
5198 || this->length_->type()->float_type() != NULL)
5199 error_at(this->length_->location(), "array bound is not constant");
5200 else
5201 error_at(this->length_->location(), "array bound is not numeric");
5202 return false;
5203 }
5204
5205 unsigned long val;
5206 switch (nc.to_unsigned_long(&val))
5207 {
5208 case Numeric_constant::NC_UL_VALID:
5209 break;
5210 case Numeric_constant::NC_UL_NOTINT:
5211 error_at(this->length_->location(), "array bound truncated to integer");
5212 return false;
5213 case Numeric_constant::NC_UL_NEGATIVE:
5214 error_at(this->length_->location(), "negative array bound");
5215 return false;
5216 case Numeric_constant::NC_UL_BIG:
5217 error_at(this->length_->location(), "array bound overflows");
5218 return false;
5219 default:
5220 go_unreachable();
5221 }
5222
5223 Type* int_type = Type::lookup_integer_type("int");
5224 unsigned int tbits = int_type->integer_type()->bits();
5225 if (sizeof(val) <= tbits * 8
5226 && val >> (tbits - 1) != 0)
5227 {
5228 error_at(this->length_->location(), "array bound overflows");
5229 return false;
5230 }
5231
5232 return true;
5233 }
5234
5235 // Verify the type.
5236
5237 bool
5238 Array_type::do_verify()
5239 {
5240 if (!this->verify_length())
5241 this->length_ = Expression::make_error(this->length_->location());
5242 return true;
5243 }
5244
5245 // Whether we can use memcmp to compare this array.
5246
5247 bool
5248 Array_type::do_compare_is_identity(Gogo* gogo) const
5249 {
5250 if (this->length_ == NULL)
5251 return false;
5252
5253 // Check for [...], which indicates that this is not a real type.
5254 if (this->length_->is_nil_expression())
5255 return false;
5256
5257 if (!this->element_type_->compare_is_identity(gogo))
5258 return false;
5259
5260 // If there is any padding, then we can't use memcmp.
5261 unsigned int size;
5262 unsigned int align;
5263 if (!this->element_type_->backend_type_size(gogo, &size)
5264 || !this->element_type_->backend_type_align(gogo, &align))
5265 return false;
5266 if ((size & (align - 1)) != 0)
5267 return false;
5268
5269 return true;
5270 }
5271
5272 // Array type hash code.
5273
5274 unsigned int
5275 Array_type::do_hash_for_method(Gogo* gogo) const
5276 {
5277 // There is no very convenient way to get a hash code for the
5278 // length.
5279 return this->element_type_->hash_for_method(gogo) + 1;
5280 }
5281
5282 // Write the hash function for an array which can not use the identify
5283 // function.
5284
5285 void
5286 Array_type::write_hash_function(Gogo* gogo, Named_type* name,
5287 Function_type* hash_fntype,
5288 Function_type* equal_fntype)
5289 {
5290 Location bloc = Linemap::predeclared_location();
5291
5292 // The pointer to the array that we are going to hash. This is an
5293 // argument to the hash function we are implementing here.
5294 Named_object* key_arg = gogo->lookup("key", NULL);
5295 go_assert(key_arg != NULL);
5296 Type* key_arg_type = key_arg->var_value()->type();
5297
5298 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5299
5300 // Get a 0.
5301 mpz_t ival;
5302 mpz_init_set_ui(ival, 0);
5303 Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
5304 mpz_clear(ival);
5305
5306 // Make a temporary to hold the return value, initialized to 0.
5307 Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
5308 bloc);
5309 gogo->add_statement(retval);
5310
5311 // Make a temporary to hold the key as a uintptr.
5312 Expression* ref = Expression::make_var_reference(key_arg, bloc);
5313 ref = Expression::make_cast(uintptr_type, ref, bloc);
5314 Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
5315 bloc);
5316 gogo->add_statement(key);
5317
5318 // Loop over the array elements.
5319 // for i = range a
5320 Type* int_type = Type::lookup_integer_type("int");
5321 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5322 gogo->add_statement(index);
5323
5324 Expression* iref = Expression::make_temporary_reference(index, bloc);
5325 Expression* aref = Expression::make_var_reference(key_arg, bloc);
5326 Type* pt = Type::make_pointer_type(name != NULL
5327 ? static_cast<Type*>(name)
5328 : static_cast<Type*>(this));
5329 aref = Expression::make_cast(pt, aref, bloc);
5330 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5331 NULL,
5332 aref,
5333 bloc);
5334
5335 gogo->start_block(bloc);
5336
5337 // Multiply retval by 33.
5338 mpz_init_set_ui(ival, 33);
5339 Expression* i33 = Expression::make_integer(&ival, uintptr_type, bloc);
5340 mpz_clear(ival);
5341
5342 ref = Expression::make_temporary_reference(retval, bloc);
5343 Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ, ref,
5344 i33, bloc);
5345 gogo->add_statement(s);
5346
5347 // Get the hash function for the element type.
5348 Named_object* hash_fn;
5349 Named_object* equal_fn;
5350 this->element_type_->type_functions(gogo, this->element_type_->named_type(),
5351 hash_fntype, equal_fntype, &hash_fn,
5352 &equal_fn);
5353
5354 // Get a pointer to this element in the loop.
5355 Expression* subkey = Expression::make_temporary_reference(key, bloc);
5356 subkey = Expression::make_cast(key_arg_type, subkey, bloc);
5357
5358 // Get the size of each element.
5359 Expression* ele_size = Expression::make_type_info(this->element_type_,
5360 Expression::TYPE_INFO_SIZE);
5361
5362 // Get the hash of this element.
5363 Expression_list* args = new Expression_list();
5364 args->push_back(subkey);
5365 args->push_back(ele_size);
5366 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
5367 Expression* call = Expression::make_call(func, args, false, bloc);
5368
5369 // Add the element's hash value to retval.
5370 Temporary_reference_expression* tref =
5371 Expression::make_temporary_reference(retval, bloc);
5372 tref->set_is_lvalue();
5373 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, call, bloc);
5374 gogo->add_statement(s);
5375
5376 // Increase the element pointer.
5377 tref = Expression::make_temporary_reference(key, bloc);
5378 tref->set_is_lvalue();
5379 s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
5380 bloc);
5381
5382 Block* statements = gogo->finish_block(bloc);
5383
5384 for_range->add_statements(statements);
5385 gogo->add_statement(for_range);
5386
5387 // Return retval to the caller of the hash function.
5388 Expression_list* vals = new Expression_list();
5389 ref = Expression::make_temporary_reference(retval, bloc);
5390 vals->push_back(ref);
5391 s = Statement::make_return_statement(vals, bloc);
5392 gogo->add_statement(s);
5393 }
5394
5395 // Write the equality function for an array which can not use the
5396 // identity function.
5397
5398 void
5399 Array_type::write_equal_function(Gogo* gogo, Named_type* name)
5400 {
5401 Location bloc = Linemap::predeclared_location();
5402
5403 // The pointers to the arrays we are going to compare.
5404 Named_object* key1_arg = gogo->lookup("key1", NULL);
5405 Named_object* key2_arg = gogo->lookup("key2", NULL);
5406 go_assert(key1_arg != NULL && key2_arg != NULL);
5407
5408 // Build temporaries for the keys with the right types.
5409 Type* pt = Type::make_pointer_type(name != NULL
5410 ? static_cast<Type*>(name)
5411 : static_cast<Type*>(this));
5412
5413 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
5414 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5415 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
5416 gogo->add_statement(p1);
5417
5418 ref = Expression::make_var_reference(key2_arg, bloc);
5419 ref = Expression::make_unsafe_cast(pt, ref, bloc);
5420 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
5421 gogo->add_statement(p2);
5422
5423 // Loop over the array elements.
5424 // for i = range a
5425 Type* int_type = Type::lookup_integer_type("int");
5426 Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
5427 gogo->add_statement(index);
5428
5429 Expression* iref = Expression::make_temporary_reference(index, bloc);
5430 Expression* aref = Expression::make_temporary_reference(p1, bloc);
5431 For_range_statement* for_range = Statement::make_for_range_statement(iref,
5432 NULL,
5433 aref,
5434 bloc);
5435
5436 gogo->start_block(bloc);
5437
5438 // Compare element in P1 and P2.
5439 Expression* e1 = Expression::make_temporary_reference(p1, bloc);
5440 e1 = Expression::make_unary(OPERATOR_MULT, e1, bloc);
5441 ref = Expression::make_temporary_reference(index, bloc);
5442 e1 = Expression::make_array_index(e1, ref, NULL, bloc);
5443
5444 Expression* e2 = Expression::make_temporary_reference(p2, bloc);
5445 e2 = Expression::make_unary(OPERATOR_MULT, e2, bloc);
5446 ref = Expression::make_temporary_reference(index, bloc);
5447 e2 = Expression::make_array_index(e2, ref, NULL, bloc);
5448
5449 Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
5450
5451 // If the elements are not equal, return false.
5452 gogo->start_block(bloc);
5453 Expression_list* vals = new Expression_list();
5454 vals->push_back(Expression::make_boolean(false, bloc));
5455 Statement* s = Statement::make_return_statement(vals, bloc);
5456 gogo->add_statement(s);
5457 Block* then_block = gogo->finish_block(bloc);
5458
5459 s = Statement::make_if_statement(cond, then_block, NULL, bloc);
5460 gogo->add_statement(s);
5461
5462 Block* statements = gogo->finish_block(bloc);
5463
5464 for_range->add_statements(statements);
5465 gogo->add_statement(for_range);
5466
5467 // All the elements are equal, so return true.
5468 vals = new Expression_list();
5469 vals->push_back(Expression::make_boolean(true, bloc));
5470 s = Statement::make_return_statement(vals, bloc);
5471 gogo->add_statement(s);
5472 }
5473
5474 // Get a tree for the length of a fixed array. The length may be
5475 // computed using a function call, so we must only evaluate it once.
5476
5477 tree
5478 Array_type::get_length_tree(Gogo* gogo)
5479 {
5480 go_assert(this->length_ != NULL);
5481 if (this->length_tree_ == NULL_TREE)
5482 {
5483 Numeric_constant nc;
5484 mpz_t val;
5485 if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val))
5486 {
5487 if (mpz_sgn(val) < 0)
5488 {
5489 this->length_tree_ = error_mark_node;
5490 return this->length_tree_;
5491 }
5492 Type* t = nc.type();
5493 if (t == NULL)
5494 t = Type::lookup_integer_type("int");
5495 else if (t->is_abstract())
5496 t = t->make_non_abstract_type();
5497 tree tt = type_to_tree(t->get_backend(gogo));
5498 this->length_tree_ = Expression::integer_constant_tree(val, tt);
5499 mpz_clear(val);
5500 }
5501 else
5502 {
5503 // Make up a translation context for the array length
5504 // expression. FIXME: This won't work in general.
5505 Translate_context context(gogo, NULL, NULL, NULL);
5506 tree len = this->length_->get_tree(&context);
5507 if (len != error_mark_node)
5508 {
5509 len = convert_to_integer(integer_type_node, len);
5510 len = save_expr(len);
5511 }
5512 this->length_tree_ = len;
5513 }
5514 }
5515 return this->length_tree_;
5516 }
5517
5518 // Get the backend representation of the fields of a slice. This is
5519 // not declared in types.h so that types.h doesn't have to #include
5520 // backend.h.
5521 //
5522 // We use int for the count and capacity fields. This matches 6g.
5523 // The language more or less assumes that we can't allocate space of a
5524 // size which does not fit in int.
5525
5526 static void
5527 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
5528 std::vector<Backend::Btyped_identifier>* bfields)
5529 {
5530 bfields->resize(3);
5531
5532 Type* pet = Type::make_pointer_type(type->element_type());
5533 Btype* pbet = (use_placeholder
5534 ? pet->get_backend_placeholder(gogo)
5535 : pet->get_backend(gogo));
5536 Location ploc = Linemap::predeclared_location();
5537
5538 Backend::Btyped_identifier* p = &(*bfields)[0];
5539 p->name = "__values";
5540 p->btype = pbet;
5541 p->location = ploc;
5542
5543 Type* int_type = Type::lookup_integer_type("int");
5544
5545 p = &(*bfields)[1];
5546 p->name = "__count";
5547 p->btype = int_type->get_backend(gogo);
5548 p->location = ploc;
5549
5550 p = &(*bfields)[2];
5551 p->name = "__capacity";
5552 p->btype = int_type->get_backend(gogo);
5553 p->location = ploc;
5554 }
5555
5556 // Get a tree for the type of this array. A fixed array is simply
5557 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
5558 // just like an array in C. An open array is a struct with three
5559 // fields: a data pointer, the length, and the capacity.
5560
5561 Btype*
5562 Array_type::do_get_backend(Gogo* gogo)
5563 {
5564 if (this->length_ == NULL)
5565 {
5566 std::vector<Backend::Btyped_identifier> bfields;
5567 get_backend_slice_fields(gogo, this, false, &bfields);
5568 return gogo->backend()->struct_type(bfields);
5569 }
5570 else
5571 {
5572 Btype* element = this->get_backend_element(gogo, false);
5573 Bexpression* len = this->get_backend_length(gogo);
5574 return gogo->backend()->array_type(element, len);
5575 }
5576 }
5577
5578 // Return the backend representation of the element type.
5579
5580 Btype*
5581 Array_type::get_backend_element(Gogo* gogo, bool use_placeholder)
5582 {
5583 if (use_placeholder)
5584 return this->element_type_->get_backend_placeholder(gogo);
5585 else
5586 return this->element_type_->get_backend(gogo);
5587 }
5588
5589 // Return the backend representation of the length.
5590
5591 Bexpression*
5592 Array_type::get_backend_length(Gogo* gogo)
5593 {
5594 return tree_to_expr(this->get_length_tree(gogo));
5595 }
5596
5597 // Finish backend representation of the array.
5598
5599 void
5600 Array_type::finish_backend_element(Gogo* gogo)
5601 {
5602 Type* et = this->array_type()->element_type();
5603 et->get_backend(gogo);
5604 if (this->is_slice_type())
5605 {
5606 // This relies on the fact that we always use the same
5607 // structure for a pointer to any given type.
5608 Type* pet = Type::make_pointer_type(et);
5609 pet->get_backend(gogo);
5610 }
5611 }
5612
5613 // Return a tree for a pointer to the values in ARRAY.
5614
5615 tree
5616 Array_type::value_pointer_tree(Gogo*, tree array) const
5617 {
5618 tree ret;
5619 if (this->length() != NULL)
5620 {
5621 // Fixed array.
5622 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
5623 build_fold_addr_expr(array));
5624 }
5625 else
5626 {
5627 // Open array.
5628 tree field = TYPE_FIELDS(TREE_TYPE(array));
5629 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
5630 "__values") == 0);
5631 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
5632 NULL_TREE);
5633 }
5634 if (TREE_CONSTANT(array))
5635 TREE_CONSTANT(ret) = 1;
5636 return ret;
5637 }
5638
5639 // Return a tree for the length of the array ARRAY which has this
5640 // type.
5641
5642 tree
5643 Array_type::length_tree(Gogo* gogo, tree array)
5644 {
5645 if (this->length_ != NULL)
5646 {
5647 if (TREE_CODE(array) == SAVE_EXPR)
5648 return fold_convert(integer_type_node, this->get_length_tree(gogo));
5649 else
5650 return omit_one_operand(integer_type_node,
5651 this->get_length_tree(gogo), array);
5652 }
5653
5654 // This is an open array. We need to read the length field.
5655
5656 tree type = TREE_TYPE(array);
5657 go_assert(TREE_CODE(type) == RECORD_TYPE);
5658
5659 tree field = DECL_CHAIN(TYPE_FIELDS(type));
5660 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
5661
5662 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5663 if (TREE_CONSTANT(array))
5664 TREE_CONSTANT(ret) = 1;
5665 return ret;
5666 }
5667
5668 // Return a tree for the capacity of the array ARRAY which has this
5669 // type.
5670
5671 tree
5672 Array_type::capacity_tree(Gogo* gogo, tree array)
5673 {
5674 if (this->length_ != NULL)
5675 return omit_one_operand(integer_type_node, this->get_length_tree(gogo),
5676 array);
5677
5678 // This is an open array. We need to read the capacity field.
5679
5680 tree type = TREE_TYPE(array);
5681 go_assert(TREE_CODE(type) == RECORD_TYPE);
5682
5683 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
5684 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
5685
5686 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
5687 }
5688
5689 // Export.
5690
5691 void
5692 Array_type::do_export(Export* exp) const
5693 {
5694 exp->write_c_string("[");
5695 if (this->length_ != NULL)
5696 this->length_->export_expression(exp);
5697 exp->write_c_string("] ");
5698 exp->write_type(this->element_type_);
5699 }
5700
5701 // Import.
5702
5703 Array_type*
5704 Array_type::do_import(Import* imp)
5705 {
5706 imp->require_c_string("[");
5707 Expression* length;
5708 if (imp->peek_char() == ']')
5709 length = NULL;
5710 else
5711 length = Expression::import_expression(imp);
5712 imp->require_c_string("] ");
5713 Type* element_type = imp->read_type();
5714 return Type::make_array_type(element_type, length);
5715 }
5716
5717 // The type of an array type descriptor.
5718
5719 Type*
5720 Array_type::make_array_type_descriptor_type()
5721 {
5722 static Type* ret;
5723 if (ret == NULL)
5724 {
5725 Type* tdt = Type::make_type_descriptor_type();
5726 Type* ptdt = Type::make_type_descriptor_ptr_type();
5727
5728 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5729
5730 Struct_type* sf =
5731 Type::make_builtin_struct_type(4,
5732 "", tdt,
5733 "elem", ptdt,
5734 "slice", ptdt,
5735 "len", uintptr_type);
5736
5737 ret = Type::make_builtin_named_type("ArrayType", sf);
5738 }
5739
5740 return ret;
5741 }
5742
5743 // The type of an slice type descriptor.
5744
5745 Type*
5746 Array_type::make_slice_type_descriptor_type()
5747 {
5748 static Type* ret;
5749 if (ret == NULL)
5750 {
5751 Type* tdt = Type::make_type_descriptor_type();
5752 Type* ptdt = Type::make_type_descriptor_ptr_type();
5753
5754 Struct_type* sf =
5755 Type::make_builtin_struct_type(2,
5756 "", tdt,
5757 "elem", ptdt);
5758
5759 ret = Type::make_builtin_named_type("SliceType", sf);
5760 }
5761
5762 return ret;
5763 }
5764
5765 // Build a type descriptor for an array/slice type.
5766
5767 Expression*
5768 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5769 {
5770 if (this->length_ != NULL)
5771 return this->array_type_descriptor(gogo, name);
5772 else
5773 return this->slice_type_descriptor(gogo, name);
5774 }
5775
5776 // Build a type descriptor for an array type.
5777
5778 Expression*
5779 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
5780 {
5781 Location bloc = Linemap::predeclared_location();
5782
5783 Type* atdt = Array_type::make_array_type_descriptor_type();
5784
5785 const Struct_field_list* fields = atdt->struct_type()->fields();
5786
5787 Expression_list* vals = new Expression_list();
5788 vals->reserve(3);
5789
5790 Struct_field_list::const_iterator p = fields->begin();
5791 go_assert(p->is_field_name("commonType"));
5792 vals->push_back(this->type_descriptor_constructor(gogo,
5793 RUNTIME_TYPE_KIND_ARRAY,
5794 name, NULL, true));
5795
5796 ++p;
5797 go_assert(p->is_field_name("elem"));
5798 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5799
5800 ++p;
5801 go_assert(p->is_field_name("slice"));
5802 Type* slice_type = Type::make_array_type(this->element_type_, NULL);
5803 vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
5804
5805 ++p;
5806 go_assert(p->is_field_name("len"));
5807 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
5808
5809 ++p;
5810 go_assert(p == fields->end());
5811
5812 return Expression::make_struct_composite_literal(atdt, vals, bloc);
5813 }
5814
5815 // Build a type descriptor for a slice type.
5816
5817 Expression*
5818 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
5819 {
5820 Location bloc = Linemap::predeclared_location();
5821
5822 Type* stdt = Array_type::make_slice_type_descriptor_type();
5823
5824 const Struct_field_list* fields = stdt->struct_type()->fields();
5825
5826 Expression_list* vals = new Expression_list();
5827 vals->reserve(2);
5828
5829 Struct_field_list::const_iterator p = fields->begin();
5830 go_assert(p->is_field_name("commonType"));
5831 vals->push_back(this->type_descriptor_constructor(gogo,
5832 RUNTIME_TYPE_KIND_SLICE,
5833 name, NULL, true));
5834
5835 ++p;
5836 go_assert(p->is_field_name("elem"));
5837 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5838
5839 ++p;
5840 go_assert(p == fields->end());
5841
5842 return Expression::make_struct_composite_literal(stdt, vals, bloc);
5843 }
5844
5845 // Reflection string.
5846
5847 void
5848 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
5849 {
5850 ret->push_back('[');
5851 if (this->length_ != NULL)
5852 {
5853 Numeric_constant nc;
5854 unsigned long val;
5855 if (!this->length_->numeric_constant_value(&nc)
5856 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
5857 error_at(this->length_->location(), "invalid array length");
5858 else
5859 {
5860 char buf[50];
5861 snprintf(buf, sizeof buf, "%lu", val);
5862 ret->append(buf);
5863 }
5864 }
5865 ret->push_back(']');
5866
5867 this->append_reflection(this->element_type_, gogo, ret);
5868 }
5869
5870 // Mangled name.
5871
5872 void
5873 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5874 {
5875 ret->push_back('A');
5876 this->append_mangled_name(this->element_type_, gogo, ret);
5877 if (this->length_ != NULL)
5878 {
5879 Numeric_constant nc;
5880 unsigned long val;
5881 if (!this->length_->numeric_constant_value(&nc)
5882 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
5883 error_at(this->length_->location(), "invalid array length");
5884 else
5885 {
5886 char buf[50];
5887 snprintf(buf, sizeof buf, "%lu", val);
5888 ret->append(buf);
5889 }
5890 }
5891 ret->push_back('e');
5892 }
5893
5894 // Make an array type.
5895
5896 Array_type*
5897 Type::make_array_type(Type* element_type, Expression* length)
5898 {
5899 return new Array_type(element_type, length);
5900 }
5901
5902 // Class Map_type.
5903
5904 // Traversal.
5905
5906 int
5907 Map_type::do_traverse(Traverse* traverse)
5908 {
5909 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
5910 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
5911 return TRAVERSE_EXIT;
5912 return TRAVERSE_CONTINUE;
5913 }
5914
5915 // Check that the map type is OK.
5916
5917 bool
5918 Map_type::do_verify()
5919 {
5920 // The runtime support uses "map[void]void".
5921 if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
5922 error_at(this->location_, "invalid map key type");
5923 return true;
5924 }
5925
5926 // Whether two map types are identical.
5927
5928 bool
5929 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
5930 {
5931 return (Type::are_identical(this->key_type(), t->key_type(),
5932 errors_are_identical, NULL)
5933 && Type::are_identical(this->val_type(), t->val_type(),
5934 errors_are_identical, NULL));
5935 }
5936
5937 // Hash code.
5938
5939 unsigned int
5940 Map_type::do_hash_for_method(Gogo* gogo) const
5941 {
5942 return (this->key_type_->hash_for_method(gogo)
5943 + this->val_type_->hash_for_method(gogo)
5944 + 2);
5945 }
5946
5947 // Get the backend representation for a map type. A map type is
5948 // represented as a pointer to a struct. The struct is __go_map in
5949 // libgo/map.h.
5950
5951 Btype*
5952 Map_type::do_get_backend(Gogo* gogo)
5953 {
5954 static Btype* backend_map_type;
5955 if (backend_map_type == NULL)
5956 {
5957 std::vector<Backend::Btyped_identifier> bfields(4);
5958
5959 Location bloc = Linemap::predeclared_location();
5960
5961 Type* pdt = Type::make_type_descriptor_ptr_type();
5962 bfields[0].name = "__descriptor";
5963 bfields[0].btype = pdt->get_backend(gogo);
5964 bfields[0].location = bloc;
5965
5966 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5967 bfields[1].name = "__element_count";
5968 bfields[1].btype = uintptr_type->get_backend(gogo);
5969 bfields[1].location = bloc;
5970
5971 bfields[2].name = "__bucket_count";
5972 bfields[2].btype = bfields[1].btype;
5973 bfields[2].location = bloc;
5974
5975 Btype* bvt = gogo->backend()->void_type();
5976 Btype* bpvt = gogo->backend()->pointer_type(bvt);
5977 Btype* bppvt = gogo->backend()->pointer_type(bpvt);
5978 bfields[3].name = "__buckets";
5979 bfields[3].btype = bppvt;
5980 bfields[3].location = bloc;
5981
5982 Btype *bt = gogo->backend()->struct_type(bfields);
5983 bt = gogo->backend()->named_type("__go_map", bt, bloc);
5984 backend_map_type = gogo->backend()->pointer_type(bt);
5985 }
5986 return backend_map_type;
5987 }
5988
5989 // The type of a map type descriptor.
5990
5991 Type*
5992 Map_type::make_map_type_descriptor_type()
5993 {
5994 static Type* ret;
5995 if (ret == NULL)
5996 {
5997 Type* tdt = Type::make_type_descriptor_type();
5998 Type* ptdt = Type::make_type_descriptor_ptr_type();
5999
6000 Struct_type* sf =
6001 Type::make_builtin_struct_type(3,
6002 "", tdt,
6003 "key", ptdt,
6004 "elem", ptdt);
6005
6006 ret = Type::make_builtin_named_type("MapType", sf);
6007 }
6008
6009 return ret;
6010 }
6011
6012 // Build a type descriptor for a map type.
6013
6014 Expression*
6015 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6016 {
6017 Location bloc = Linemap::predeclared_location();
6018
6019 Type* mtdt = Map_type::make_map_type_descriptor_type();
6020
6021 const Struct_field_list* fields = mtdt->struct_type()->fields();
6022
6023 Expression_list* vals = new Expression_list();
6024 vals->reserve(3);
6025
6026 Struct_field_list::const_iterator p = fields->begin();
6027 go_assert(p->is_field_name("commonType"));
6028 vals->push_back(this->type_descriptor_constructor(gogo,
6029 RUNTIME_TYPE_KIND_MAP,
6030 name, NULL, true));
6031
6032 ++p;
6033 go_assert(p->is_field_name("key"));
6034 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
6035
6036 ++p;
6037 go_assert(p->is_field_name("elem"));
6038 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
6039
6040 ++p;
6041 go_assert(p == fields->end());
6042
6043 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
6044 }
6045
6046 // A mapping from map types to map descriptors.
6047
6048 Map_type::Map_descriptors Map_type::map_descriptors;
6049
6050 // Build a map descriptor for this type. Return a pointer to it.
6051
6052 tree
6053 Map_type::map_descriptor_pointer(Gogo* gogo, Location location)
6054 {
6055 Bvariable* bvar = this->map_descriptor(gogo);
6056 tree var_tree = var_to_tree(bvar);
6057 if (var_tree == error_mark_node)
6058 return error_mark_node;
6059 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
6060 }
6061
6062 // Build a map descriptor for this type.
6063
6064 Bvariable*
6065 Map_type::map_descriptor(Gogo* gogo)
6066 {
6067 std::pair<Map_type*, Bvariable*> val(this, NULL);
6068 std::pair<Map_type::Map_descriptors::iterator, bool> ins =
6069 Map_type::map_descriptors.insert(val);
6070 if (!ins.second)
6071 return ins.first->second;
6072
6073 Type* key_type = this->key_type_;
6074 Type* val_type = this->val_type_;
6075
6076 // The map entry type is a struct with three fields. Build that
6077 // struct so that we can get the offsets of the key and value within
6078 // a map entry. The first field should technically be a pointer to
6079 // this type itself, but since we only care about field offsets we
6080 // just use pointer to bool.
6081 Type* pbool = Type::make_pointer_type(Type::make_boolean_type());
6082 Struct_type* map_entry_type =
6083 Type::make_builtin_struct_type(3,
6084 "__next", pbool,
6085 "__key", key_type,
6086 "__val", val_type);
6087
6088 Type* map_descriptor_type = Map_type::make_map_descriptor_type();
6089
6090 const Struct_field_list* fields =
6091 map_descriptor_type->struct_type()->fields();
6092
6093 Expression_list* vals = new Expression_list();
6094 vals->reserve(4);
6095
6096 Location bloc = Linemap::predeclared_location();
6097
6098 Struct_field_list::const_iterator p = fields->begin();
6099
6100 go_assert(p->is_field_name("__map_descriptor"));
6101 vals->push_back(Expression::make_type_descriptor(this, bloc));
6102
6103 ++p;
6104 go_assert(p->is_field_name("__entry_size"));
6105 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
6106 vals->push_back(Expression::make_type_info(map_entry_type, type_info));
6107
6108 Struct_field_list::const_iterator pf = map_entry_type->fields()->begin();
6109 ++pf;
6110 go_assert(pf->is_field_name("__key"));
6111
6112 ++p;
6113 go_assert(p->is_field_name("__key_offset"));
6114 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6115
6116 ++pf;
6117 go_assert(pf->is_field_name("__val"));
6118
6119 ++p;
6120 go_assert(p->is_field_name("__val_offset"));
6121 vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
6122
6123 ++p;
6124 go_assert(p == fields->end());
6125
6126 Expression* initializer =
6127 Expression::make_struct_composite_literal(map_descriptor_type, vals, bloc);
6128
6129 std::string mangled_name = "__go_map_" + this->mangled_name(gogo);
6130 Btype* map_descriptor_btype = map_descriptor_type->get_backend(gogo);
6131 Bvariable* bvar = gogo->backend()->immutable_struct(mangled_name, true,
6132 map_descriptor_btype,
6133 bloc);
6134
6135 Translate_context context(gogo, NULL, NULL, NULL);
6136 context.set_is_const();
6137 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
6138
6139 gogo->backend()->immutable_struct_set_init(bvar, mangled_name, true,
6140 map_descriptor_btype, bloc,
6141 binitializer);
6142
6143 ins.first->second = bvar;
6144 return bvar;
6145 }
6146
6147 // Build the type of a map descriptor. This must match the struct
6148 // __go_map_descriptor in libgo/runtime/map.h.
6149
6150 Type*
6151 Map_type::make_map_descriptor_type()
6152 {
6153 static Type* ret;
6154 if (ret == NULL)
6155 {
6156 Type* ptdt = Type::make_type_descriptor_ptr_type();
6157 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6158 Struct_type* sf =
6159 Type::make_builtin_struct_type(4,
6160 "__map_descriptor", ptdt,
6161 "__entry_size", uintptr_type,
6162 "__key_offset", uintptr_type,
6163 "__val_offset", uintptr_type);
6164 ret = Type::make_builtin_named_type("__go_map_descriptor", sf);
6165 }
6166 return ret;
6167 }
6168
6169 // Reflection string for a map.
6170
6171 void
6172 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
6173 {
6174 ret->append("map[");
6175 this->append_reflection(this->key_type_, gogo, ret);
6176 ret->append("]");
6177 this->append_reflection(this->val_type_, gogo, ret);
6178 }
6179
6180 // Mangled name for a map.
6181
6182 void
6183 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6184 {
6185 ret->push_back('M');
6186 this->append_mangled_name(this->key_type_, gogo, ret);
6187 ret->append("__");
6188 this->append_mangled_name(this->val_type_, gogo, ret);
6189 }
6190
6191 // Export a map type.
6192
6193 void
6194 Map_type::do_export(Export* exp) const
6195 {
6196 exp->write_c_string("map [");
6197 exp->write_type(this->key_type_);
6198 exp->write_c_string("] ");
6199 exp->write_type(this->val_type_);
6200 }
6201
6202 // Import a map type.
6203
6204 Map_type*
6205 Map_type::do_import(Import* imp)
6206 {
6207 imp->require_c_string("map [");
6208 Type* key_type = imp->read_type();
6209 imp->require_c_string("] ");
6210 Type* val_type = imp->read_type();
6211 return Type::make_map_type(key_type, val_type, imp->location());
6212 }
6213
6214 // Make a map type.
6215
6216 Map_type*
6217 Type::make_map_type(Type* key_type, Type* val_type, Location location)
6218 {
6219 return new Map_type(key_type, val_type, location);
6220 }
6221
6222 // Class Channel_type.
6223
6224 // Hash code.
6225
6226 unsigned int
6227 Channel_type::do_hash_for_method(Gogo* gogo) const
6228 {
6229 unsigned int ret = 0;
6230 if (this->may_send_)
6231 ret += 1;
6232 if (this->may_receive_)
6233 ret += 2;
6234 if (this->element_type_ != NULL)
6235 ret += this->element_type_->hash_for_method(gogo) << 2;
6236 return ret << 3;
6237 }
6238
6239 // Whether this type is the same as T.
6240
6241 bool
6242 Channel_type::is_identical(const Channel_type* t,
6243 bool errors_are_identical) const
6244 {
6245 if (!Type::are_identical(this->element_type(), t->element_type(),
6246 errors_are_identical, NULL))
6247 return false;
6248 return (this->may_send_ == t->may_send_
6249 && this->may_receive_ == t->may_receive_);
6250 }
6251
6252 // Return the tree for a channel type. A channel is a pointer to a
6253 // __go_channel struct. The __go_channel struct is defined in
6254 // libgo/runtime/channel.h.
6255
6256 Btype*
6257 Channel_type::do_get_backend(Gogo* gogo)
6258 {
6259 static Btype* backend_channel_type;
6260 if (backend_channel_type == NULL)
6261 {
6262 std::vector<Backend::Btyped_identifier> bfields;
6263 Btype* bt = gogo->backend()->struct_type(bfields);
6264 bt = gogo->backend()->named_type("__go_channel", bt,
6265 Linemap::predeclared_location());
6266 backend_channel_type = gogo->backend()->pointer_type(bt);
6267 }
6268 return backend_channel_type;
6269 }
6270
6271 // Build a type descriptor for a channel type.
6272
6273 Type*
6274 Channel_type::make_chan_type_descriptor_type()
6275 {
6276 static Type* ret;
6277 if (ret == NULL)
6278 {
6279 Type* tdt = Type::make_type_descriptor_type();
6280 Type* ptdt = Type::make_type_descriptor_ptr_type();
6281
6282 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6283
6284 Struct_type* sf =
6285 Type::make_builtin_struct_type(3,
6286 "", tdt,
6287 "elem", ptdt,
6288 "dir", uintptr_type);
6289
6290 ret = Type::make_builtin_named_type("ChanType", sf);
6291 }
6292
6293 return ret;
6294 }
6295
6296 // Build a type descriptor for a map type.
6297
6298 Expression*
6299 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6300 {
6301 Location bloc = Linemap::predeclared_location();
6302
6303 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
6304
6305 const Struct_field_list* fields = ctdt->struct_type()->fields();
6306
6307 Expression_list* vals = new Expression_list();
6308 vals->reserve(3);
6309
6310 Struct_field_list::const_iterator p = fields->begin();
6311 go_assert(p->is_field_name("commonType"));
6312 vals->push_back(this->type_descriptor_constructor(gogo,
6313 RUNTIME_TYPE_KIND_CHAN,
6314 name, NULL, true));
6315
6316 ++p;
6317 go_assert(p->is_field_name("elem"));
6318 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
6319
6320 ++p;
6321 go_assert(p->is_field_name("dir"));
6322 // These bits must match the ones in libgo/runtime/go-type.h.
6323 int val = 0;
6324 if (this->may_receive_)
6325 val |= 1;
6326 if (this->may_send_)
6327 val |= 2;
6328 mpz_t iv;
6329 mpz_init_set_ui(iv, val);
6330 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
6331 mpz_clear(iv);
6332
6333 ++p;
6334 go_assert(p == fields->end());
6335
6336 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
6337 }
6338
6339 // Reflection string.
6340
6341 void
6342 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
6343 {
6344 if (!this->may_send_)
6345 ret->append("<-");
6346 ret->append("chan");
6347 if (!this->may_receive_)
6348 ret->append("<-");
6349 ret->push_back(' ');
6350 this->append_reflection(this->element_type_, gogo, ret);
6351 }
6352
6353 // Mangled name.
6354
6355 void
6356 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6357 {
6358 ret->push_back('C');
6359 this->append_mangled_name(this->element_type_, gogo, ret);
6360 if (this->may_send_)
6361 ret->push_back('s');
6362 if (this->may_receive_)
6363 ret->push_back('r');
6364 ret->push_back('e');
6365 }
6366
6367 // Export.
6368
6369 void
6370 Channel_type::do_export(Export* exp) const
6371 {
6372 exp->write_c_string("chan ");
6373 if (this->may_send_ && !this->may_receive_)
6374 exp->write_c_string("-< ");
6375 else if (this->may_receive_ && !this->may_send_)
6376 exp->write_c_string("<- ");
6377 exp->write_type(this->element_type_);
6378 }
6379
6380 // Import.
6381
6382 Channel_type*
6383 Channel_type::do_import(Import* imp)
6384 {
6385 imp->require_c_string("chan ");
6386
6387 bool may_send;
6388 bool may_receive;
6389 if (imp->match_c_string("-< "))
6390 {
6391 imp->advance(3);
6392 may_send = true;
6393 may_receive = false;
6394 }
6395 else if (imp->match_c_string("<- "))
6396 {
6397 imp->advance(3);
6398 may_receive = true;
6399 may_send = false;
6400 }
6401 else
6402 {
6403 may_send = true;
6404 may_receive = true;
6405 }
6406
6407 Type* element_type = imp->read_type();
6408
6409 return Type::make_channel_type(may_send, may_receive, element_type);
6410 }
6411
6412 // Make a new channel type.
6413
6414 Channel_type*
6415 Type::make_channel_type(bool send, bool receive, Type* element_type)
6416 {
6417 return new Channel_type(send, receive, element_type);
6418 }
6419
6420 // Class Interface_type.
6421
6422 // Traversal.
6423
6424 int
6425 Interface_type::do_traverse(Traverse* traverse)
6426 {
6427 Typed_identifier_list* methods = (this->methods_are_finalized_
6428 ? this->all_methods_
6429 : this->parse_methods_);
6430 if (methods == NULL)
6431 return TRAVERSE_CONTINUE;
6432 return methods->traverse(traverse);
6433 }
6434
6435 // Finalize the methods. This handles interface inheritance.
6436
6437 void
6438 Interface_type::finalize_methods()
6439 {
6440 if (this->methods_are_finalized_)
6441 return;
6442 this->methods_are_finalized_ = true;
6443 if (this->parse_methods_ == NULL)
6444 return;
6445
6446 this->all_methods_ = new Typed_identifier_list();
6447 this->all_methods_->reserve(this->parse_methods_->size());
6448 Typed_identifier_list inherit;
6449 for (Typed_identifier_list::const_iterator pm =
6450 this->parse_methods_->begin();
6451 pm != this->parse_methods_->end();
6452 ++pm)
6453 {
6454 const Typed_identifier* p = &*pm;
6455 if (p->name().empty())
6456 inherit.push_back(*p);
6457 else if (this->find_method(p->name()) == NULL)
6458 this->all_methods_->push_back(*p);
6459 else
6460 error_at(p->location(), "duplicate method %qs",
6461 Gogo::message_name(p->name()).c_str());
6462 }
6463
6464 std::vector<Named_type*> seen;
6465 seen.reserve(inherit.size());
6466 bool issued_recursive_error = false;
6467 while (!inherit.empty())
6468 {
6469 Type* t = inherit.back().type();
6470 Location tl = inherit.back().location();
6471 inherit.pop_back();
6472
6473 Interface_type* it = t->interface_type();
6474 if (it == NULL)
6475 {
6476 if (!t->is_error())
6477 error_at(tl, "interface contains embedded non-interface");
6478 continue;
6479 }
6480 if (it == this)
6481 {
6482 if (!issued_recursive_error)
6483 {
6484 error_at(tl, "invalid recursive interface");
6485 issued_recursive_error = true;
6486 }
6487 continue;
6488 }
6489
6490 Named_type* nt = t->named_type();
6491 if (nt != NULL && it->parse_methods_ != NULL)
6492 {
6493 std::vector<Named_type*>::const_iterator q;
6494 for (q = seen.begin(); q != seen.end(); ++q)
6495 {
6496 if (*q == nt)
6497 {
6498 error_at(tl, "inherited interface loop");
6499 break;
6500 }
6501 }
6502 if (q != seen.end())
6503 continue;
6504 seen.push_back(nt);
6505 }
6506
6507 const Typed_identifier_list* imethods = it->parse_methods_;
6508 if (imethods == NULL)
6509 continue;
6510 for (Typed_identifier_list::const_iterator q = imethods->begin();
6511 q != imethods->end();
6512 ++q)
6513 {
6514 if (q->name().empty())
6515 inherit.push_back(*q);
6516 else if (this->find_method(q->name()) == NULL)
6517 this->all_methods_->push_back(Typed_identifier(q->name(),
6518 q->type(), tl));
6519 else
6520 error_at(tl, "inherited method %qs is ambiguous",
6521 Gogo::message_name(q->name()).c_str());
6522 }
6523 }
6524
6525 if (!this->all_methods_->empty())
6526 this->all_methods_->sort_by_name();
6527 else
6528 {
6529 delete this->all_methods_;
6530 this->all_methods_ = NULL;
6531 }
6532 }
6533
6534 // Return the method NAME, or NULL.
6535
6536 const Typed_identifier*
6537 Interface_type::find_method(const std::string& name) const
6538 {
6539 go_assert(this->methods_are_finalized_);
6540 if (this->all_methods_ == NULL)
6541 return NULL;
6542 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6543 p != this->all_methods_->end();
6544 ++p)
6545 if (p->name() == name)
6546 return &*p;
6547 return NULL;
6548 }
6549
6550 // Return the method index.
6551
6552 size_t
6553 Interface_type::method_index(const std::string& name) const
6554 {
6555 go_assert(this->methods_are_finalized_ && this->all_methods_ != NULL);
6556 size_t ret = 0;
6557 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6558 p != this->all_methods_->end();
6559 ++p, ++ret)
6560 if (p->name() == name)
6561 return ret;
6562 go_unreachable();
6563 }
6564
6565 // Return whether NAME is an unexported method, for better error
6566 // reporting.
6567
6568 bool
6569 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
6570 {
6571 go_assert(this->methods_are_finalized_);
6572 if (this->all_methods_ == NULL)
6573 return false;
6574 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6575 p != this->all_methods_->end();
6576 ++p)
6577 {
6578 const std::string& method_name(p->name());
6579 if (Gogo::is_hidden_name(method_name)
6580 && name == Gogo::unpack_hidden_name(method_name)
6581 && gogo->pack_hidden_name(name, false) != method_name)
6582 return true;
6583 }
6584 return false;
6585 }
6586
6587 // Whether this type is identical with T.
6588
6589 bool
6590 Interface_type::is_identical(const Interface_type* t,
6591 bool errors_are_identical) const
6592 {
6593 // If methods have not been finalized, then we are asking whether
6594 // func redeclarations are the same. This is an error, so for
6595 // simplicity we say they are never the same.
6596 if (!this->methods_are_finalized_ || !t->methods_are_finalized_)
6597 return false;
6598
6599 // We require the same methods with the same types. The methods
6600 // have already been sorted.
6601 if (this->all_methods_ == NULL || t->all_methods_ == NULL)
6602 return this->all_methods_ == t->all_methods_;
6603
6604 if (this->assume_identical(this, t) || t->assume_identical(t, this))
6605 return true;
6606
6607 Assume_identical* hold_ai = this->assume_identical_;
6608 Assume_identical ai;
6609 ai.t1 = this;
6610 ai.t2 = t;
6611 ai.next = hold_ai;
6612 this->assume_identical_ = &ai;
6613
6614 Typed_identifier_list::const_iterator p1 = this->all_methods_->begin();
6615 Typed_identifier_list::const_iterator p2;
6616 for (p2 = t->all_methods_->begin(); p2 != t->all_methods_->end(); ++p1, ++p2)
6617 {
6618 if (p1 == this->all_methods_->end())
6619 break;
6620 if (p1->name() != p2->name()
6621 || !Type::are_identical(p1->type(), p2->type(),
6622 errors_are_identical, NULL))
6623 break;
6624 }
6625
6626 this->assume_identical_ = hold_ai;
6627
6628 return p1 == this->all_methods_->end() && p2 == t->all_methods_->end();
6629 }
6630
6631 // Return true if T1 and T2 are assumed to be identical during a type
6632 // comparison.
6633
6634 bool
6635 Interface_type::assume_identical(const Interface_type* t1,
6636 const Interface_type* t2) const
6637 {
6638 for (Assume_identical* p = this->assume_identical_;
6639 p != NULL;
6640 p = p->next)
6641 if ((p->t1 == t1 && p->t2 == t2) || (p->t1 == t2 && p->t2 == t1))
6642 return true;
6643 return false;
6644 }
6645
6646 // Whether we can assign the interface type T to this type. The types
6647 // are known to not be identical. An interface assignment is only
6648 // permitted if T is known to implement all methods in THIS.
6649 // Otherwise a type guard is required.
6650
6651 bool
6652 Interface_type::is_compatible_for_assign(const Interface_type* t,
6653 std::string* reason) const
6654 {
6655 go_assert(this->methods_are_finalized_ && t->methods_are_finalized_);
6656 if (this->all_methods_ == NULL)
6657 return true;
6658 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6659 p != this->all_methods_->end();
6660 ++p)
6661 {
6662 const Typed_identifier* m = t->find_method(p->name());
6663 if (m == NULL)
6664 {
6665 if (reason != NULL)
6666 {
6667 char buf[200];
6668 snprintf(buf, sizeof buf,
6669 _("need explicit conversion; missing method %s%s%s"),
6670 open_quote, Gogo::message_name(p->name()).c_str(),
6671 close_quote);
6672 reason->assign(buf);
6673 }
6674 return false;
6675 }
6676
6677 std::string subreason;
6678 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
6679 {
6680 if (reason != NULL)
6681 {
6682 std::string n = Gogo::message_name(p->name());
6683 size_t len = 100 + n.length() + subreason.length();
6684 char* buf = new char[len];
6685 if (subreason.empty())
6686 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6687 open_quote, n.c_str(), close_quote);
6688 else
6689 snprintf(buf, len,
6690 _("incompatible type for method %s%s%s (%s)"),
6691 open_quote, n.c_str(), close_quote,
6692 subreason.c_str());
6693 reason->assign(buf);
6694 delete[] buf;
6695 }
6696 return false;
6697 }
6698 }
6699
6700 return true;
6701 }
6702
6703 // Hash code.
6704
6705 unsigned int
6706 Interface_type::do_hash_for_method(Gogo*) const
6707 {
6708 go_assert(this->methods_are_finalized_);
6709 unsigned int ret = 0;
6710 if (this->all_methods_ != NULL)
6711 {
6712 for (Typed_identifier_list::const_iterator p =
6713 this->all_methods_->begin();
6714 p != this->all_methods_->end();
6715 ++p)
6716 {
6717 ret = Type::hash_string(p->name(), ret);
6718 // We don't use the method type in the hash, to avoid
6719 // infinite recursion if an interface method uses a type
6720 // which is an interface which inherits from the interface
6721 // itself.
6722 // type T interface { F() interface {T}}
6723 ret <<= 1;
6724 }
6725 }
6726 return ret;
6727 }
6728
6729 // Return true if T implements the interface. If it does not, and
6730 // REASON is not NULL, set *REASON to a useful error message.
6731
6732 bool
6733 Interface_type::implements_interface(const Type* t, std::string* reason) const
6734 {
6735 go_assert(this->methods_are_finalized_);
6736 if (this->all_methods_ == NULL)
6737 return true;
6738
6739 bool is_pointer = false;
6740 const Named_type* nt = t->named_type();
6741 const Struct_type* st = t->struct_type();
6742 // If we start with a named type, we don't dereference it to find
6743 // methods.
6744 if (nt == NULL)
6745 {
6746 const Type* pt = t->points_to();
6747 if (pt != NULL)
6748 {
6749 // If T is a pointer to a named type, then we need to look at
6750 // the type to which it points.
6751 is_pointer = true;
6752 nt = pt->named_type();
6753 st = pt->struct_type();
6754 }
6755 }
6756
6757 // If we have a named type, get the methods from it rather than from
6758 // any struct type.
6759 if (nt != NULL)
6760 st = NULL;
6761
6762 // Only named and struct types have methods.
6763 if (nt == NULL && st == NULL)
6764 {
6765 if (reason != NULL)
6766 {
6767 if (t->points_to() != NULL
6768 && t->points_to()->interface_type() != NULL)
6769 reason->assign(_("pointer to interface type has no methods"));
6770 else
6771 reason->assign(_("type has no methods"));
6772 }
6773 return false;
6774 }
6775
6776 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
6777 {
6778 if (reason != NULL)
6779 {
6780 if (t->points_to() != NULL
6781 && t->points_to()->interface_type() != NULL)
6782 reason->assign(_("pointer to interface type has no methods"));
6783 else
6784 reason->assign(_("type has no methods"));
6785 }
6786 return false;
6787 }
6788
6789 for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
6790 p != this->all_methods_->end();
6791 ++p)
6792 {
6793 bool is_ambiguous = false;
6794 Method* m = (nt != NULL
6795 ? nt->method_function(p->name(), &is_ambiguous)
6796 : st->method_function(p->name(), &is_ambiguous));
6797 if (m == NULL)
6798 {
6799 if (reason != NULL)
6800 {
6801 std::string n = Gogo::message_name(p->name());
6802 size_t len = n.length() + 100;
6803 char* buf = new char[len];
6804 if (is_ambiguous)
6805 snprintf(buf, len, _("ambiguous method %s%s%s"),
6806 open_quote, n.c_str(), close_quote);
6807 else
6808 snprintf(buf, len, _("missing method %s%s%s"),
6809 open_quote, n.c_str(), close_quote);
6810 reason->assign(buf);
6811 delete[] buf;
6812 }
6813 return false;
6814 }
6815
6816 Function_type *p_fn_type = p->type()->function_type();
6817 Function_type* m_fn_type = m->type()->function_type();
6818 go_assert(p_fn_type != NULL && m_fn_type != NULL);
6819 std::string subreason;
6820 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
6821 {
6822 if (reason != NULL)
6823 {
6824 std::string n = Gogo::message_name(p->name());
6825 size_t len = 100 + n.length() + subreason.length();
6826 char* buf = new char[len];
6827 if (subreason.empty())
6828 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6829 open_quote, n.c_str(), close_quote);
6830 else
6831 snprintf(buf, len,
6832 _("incompatible type for method %s%s%s (%s)"),
6833 open_quote, n.c_str(), close_quote,
6834 subreason.c_str());
6835 reason->assign(buf);
6836 delete[] buf;
6837 }
6838 return false;
6839 }
6840
6841 if (!is_pointer && !m->is_value_method())
6842 {
6843 if (reason != NULL)
6844 {
6845 std::string n = Gogo::message_name(p->name());
6846 size_t len = 100 + n.length();
6847 char* buf = new char[len];
6848 snprintf(buf, len,
6849 _("method %s%s%s requires a pointer receiver"),
6850 open_quote, n.c_str(), close_quote);
6851 reason->assign(buf);
6852 delete[] buf;
6853 }
6854 return false;
6855 }
6856 }
6857
6858 return true;
6859 }
6860
6861 // Return the backend representation of the empty interface type. We
6862 // use the same struct for all empty interfaces.
6863
6864 Btype*
6865 Interface_type::get_backend_empty_interface_type(Gogo* gogo)
6866 {
6867 static Btype* empty_interface_type;
6868 if (empty_interface_type == NULL)
6869 {
6870 std::vector<Backend::Btyped_identifier> bfields(2);
6871
6872 Location bloc = Linemap::predeclared_location();
6873
6874 Type* pdt = Type::make_type_descriptor_ptr_type();
6875 bfields[0].name = "__type_descriptor";
6876 bfields[0].btype = pdt->get_backend(gogo);
6877 bfields[0].location = bloc;
6878
6879 Type* vt = Type::make_pointer_type(Type::make_void_type());
6880 bfields[1].name = "__object";
6881 bfields[1].btype = vt->get_backend(gogo);
6882 bfields[1].location = bloc;
6883
6884 empty_interface_type = gogo->backend()->struct_type(bfields);
6885 }
6886 return empty_interface_type;
6887 }
6888
6889 // Return the fields of a non-empty interface type. This is not
6890 // declared in types.h so that types.h doesn't have to #include
6891 // backend.h.
6892
6893 static void
6894 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
6895 bool use_placeholder,
6896 std::vector<Backend::Btyped_identifier>* bfields)
6897 {
6898 Location loc = type->location();
6899
6900 std::vector<Backend::Btyped_identifier> mfields(type->methods()->size() + 1);
6901
6902 Type* pdt = Type::make_type_descriptor_ptr_type();
6903 mfields[0].name = "__type_descriptor";
6904 mfields[0].btype = pdt->get_backend(gogo);
6905 mfields[0].location = loc;
6906
6907 std::string last_name = "";
6908 size_t i = 1;
6909 for (Typed_identifier_list::const_iterator p = type->methods()->begin();
6910 p != type->methods()->end();
6911 ++p, ++i)
6912 {
6913 // The type of the method in Go only includes the parameters.
6914 // The actual method also has a receiver, which is always a
6915 // pointer. We need to add that pointer type here in order to
6916 // generate the correct type for the backend.
6917 Function_type* ft = p->type()->function_type();
6918 go_assert(ft->receiver() == NULL);
6919
6920 const Typed_identifier_list* params = ft->parameters();
6921 Typed_identifier_list* mparams = new Typed_identifier_list();
6922 if (params != NULL)
6923 mparams->reserve(params->size() + 1);
6924 Type* vt = Type::make_pointer_type(Type::make_void_type());
6925 mparams->push_back(Typed_identifier("", vt, ft->location()));
6926 if (params != NULL)
6927 {
6928 for (Typed_identifier_list::const_iterator pp = params->begin();
6929 pp != params->end();
6930 ++pp)
6931 mparams->push_back(*pp);
6932 }
6933
6934 Typed_identifier_list* mresults = (ft->results() == NULL
6935 ? NULL
6936 : ft->results()->copy());
6937 Function_type* mft = Type::make_function_type(NULL, mparams, mresults,
6938 ft->location());
6939
6940 mfields[i].name = Gogo::unpack_hidden_name(p->name());
6941 mfields[i].btype = (use_placeholder
6942 ? mft->get_backend_placeholder(gogo)
6943 : mft->get_backend(gogo));
6944 mfields[i].location = loc;
6945 // Sanity check: the names should be sorted.
6946 go_assert(p->name() > last_name);
6947 last_name = p->name();
6948 }
6949
6950 Btype* methods = gogo->backend()->struct_type(mfields);
6951
6952 bfields->resize(2);
6953
6954 (*bfields)[0].name = "__methods";
6955 (*bfields)[0].btype = gogo->backend()->pointer_type(methods);
6956 (*bfields)[0].location = loc;
6957
6958 Type* vt = Type::make_pointer_type(Type::make_void_type());
6959 (*bfields)[1].name = "__object";
6960 (*bfields)[1].btype = vt->get_backend(gogo);
6961 (*bfields)[1].location = Linemap::predeclared_location();
6962 }
6963
6964 // Return a tree for an interface type. An interface is a pointer to
6965 // a struct. The struct has three fields. The first field is a
6966 // pointer to the type descriptor for the dynamic type of the object.
6967 // The second field is a pointer to a table of methods for the
6968 // interface to be used with the object. The third field is the value
6969 // of the object itself.
6970
6971 Btype*
6972 Interface_type::do_get_backend(Gogo* gogo)
6973 {
6974 if (this->is_empty())
6975 return Interface_type::get_backend_empty_interface_type(gogo);
6976 else
6977 {
6978 if (this->interface_btype_ != NULL)
6979 return this->interface_btype_;
6980 this->interface_btype_ =
6981 gogo->backend()->placeholder_struct_type("", this->location_);
6982 std::vector<Backend::Btyped_identifier> bfields;
6983 get_backend_interface_fields(gogo, this, false, &bfields);
6984 if (!gogo->backend()->set_placeholder_struct_type(this->interface_btype_,
6985 bfields))
6986 this->interface_btype_ = gogo->backend()->error_type();
6987 return this->interface_btype_;
6988 }
6989 }
6990
6991 // Finish the backend representation of the methods.
6992
6993 void
6994 Interface_type::finish_backend_methods(Gogo* gogo)
6995 {
6996 if (!this->interface_type()->is_empty())
6997 {
6998 const Typed_identifier_list* methods = this->methods();
6999 if (methods != NULL)
7000 {
7001 for (Typed_identifier_list::const_iterator p = methods->begin();
7002 p != methods->end();
7003 ++p)
7004 p->type()->get_backend(gogo);
7005 }
7006 }
7007 }
7008
7009 // The type of an interface type descriptor.
7010
7011 Type*
7012 Interface_type::make_interface_type_descriptor_type()
7013 {
7014 static Type* ret;
7015 if (ret == NULL)
7016 {
7017 Type* tdt = Type::make_type_descriptor_type();
7018 Type* ptdt = Type::make_type_descriptor_ptr_type();
7019
7020 Type* string_type = Type::lookup_string_type();
7021 Type* pointer_string_type = Type::make_pointer_type(string_type);
7022
7023 Struct_type* sm =
7024 Type::make_builtin_struct_type(3,
7025 "name", pointer_string_type,
7026 "pkgPath", pointer_string_type,
7027 "typ", ptdt);
7028
7029 Type* nsm = Type::make_builtin_named_type("imethod", sm);
7030
7031 Type* slice_nsm = Type::make_array_type(nsm, NULL);
7032
7033 Struct_type* s = Type::make_builtin_struct_type(2,
7034 "", tdt,
7035 "methods", slice_nsm);
7036
7037 ret = Type::make_builtin_named_type("InterfaceType", s);
7038 }
7039
7040 return ret;
7041 }
7042
7043 // Build a type descriptor for an interface type.
7044
7045 Expression*
7046 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7047 {
7048 Location bloc = Linemap::predeclared_location();
7049
7050 Type* itdt = Interface_type::make_interface_type_descriptor_type();
7051
7052 const Struct_field_list* ifields = itdt->struct_type()->fields();
7053
7054 Expression_list* ivals = new Expression_list();
7055 ivals->reserve(2);
7056
7057 Struct_field_list::const_iterator pif = ifields->begin();
7058 go_assert(pif->is_field_name("commonType"));
7059 const int rt = RUNTIME_TYPE_KIND_INTERFACE;
7060 ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
7061 true));
7062
7063 ++pif;
7064 go_assert(pif->is_field_name("methods"));
7065
7066 Expression_list* methods = new Expression_list();
7067 if (this->all_methods_ != NULL)
7068 {
7069 Type* elemtype = pif->type()->array_type()->element_type();
7070
7071 methods->reserve(this->all_methods_->size());
7072 for (Typed_identifier_list::const_iterator pm =
7073 this->all_methods_->begin();
7074 pm != this->all_methods_->end();
7075 ++pm)
7076 {
7077 const Struct_field_list* mfields = elemtype->struct_type()->fields();
7078
7079 Expression_list* mvals = new Expression_list();
7080 mvals->reserve(3);
7081
7082 Struct_field_list::const_iterator pmf = mfields->begin();
7083 go_assert(pmf->is_field_name("name"));
7084 std::string s = Gogo::unpack_hidden_name(pm->name());
7085 Expression* e = Expression::make_string(s, bloc);
7086 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7087
7088 ++pmf;
7089 go_assert(pmf->is_field_name("pkgPath"));
7090 if (!Gogo::is_hidden_name(pm->name()))
7091 mvals->push_back(Expression::make_nil(bloc));
7092 else
7093 {
7094 s = Gogo::hidden_name_pkgpath(pm->name());
7095 e = Expression::make_string(s, bloc);
7096 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
7097 }
7098
7099 ++pmf;
7100 go_assert(pmf->is_field_name("typ"));
7101 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
7102
7103 ++pmf;
7104 go_assert(pmf == mfields->end());
7105
7106 e = Expression::make_struct_composite_literal(elemtype, mvals,
7107 bloc);
7108 methods->push_back(e);
7109 }
7110 }
7111
7112 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
7113 methods, bloc));
7114
7115 ++pif;
7116 go_assert(pif == ifields->end());
7117
7118 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
7119 }
7120
7121 // Reflection string.
7122
7123 void
7124 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
7125 {
7126 ret->append("interface {");
7127 const Typed_identifier_list* methods = this->parse_methods_;
7128 if (methods != NULL)
7129 {
7130 ret->push_back(' ');
7131 for (Typed_identifier_list::const_iterator p = methods->begin();
7132 p != methods->end();
7133 ++p)
7134 {
7135 if (p != methods->begin())
7136 ret->append("; ");
7137 if (p->name().empty())
7138 this->append_reflection(p->type(), gogo, ret);
7139 else
7140 {
7141 if (!Gogo::is_hidden_name(p->name()))
7142 ret->append(p->name());
7143 else if (gogo->pkgpath_from_option())
7144 ret->append(p->name().substr(1));
7145 else
7146 {
7147 // If no -fgo-pkgpath option, backward compatibility
7148 // for how this used to work before -fgo-pkgpath was
7149 // introduced.
7150 std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
7151 ret->append(pkgpath.substr(pkgpath.find('.') + 1));
7152 ret->push_back('.');
7153 ret->append(Gogo::unpack_hidden_name(p->name()));
7154 }
7155 std::string sub = p->type()->reflection(gogo);
7156 go_assert(sub.compare(0, 4, "func") == 0);
7157 sub = sub.substr(4);
7158 ret->append(sub);
7159 }
7160 }
7161 ret->push_back(' ');
7162 }
7163 ret->append("}");
7164 }
7165
7166 // Mangled name.
7167
7168 void
7169 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7170 {
7171 go_assert(this->methods_are_finalized_);
7172
7173 ret->push_back('I');
7174
7175 const Typed_identifier_list* methods = this->all_methods_;
7176 if (methods != NULL && !this->seen_)
7177 {
7178 this->seen_ = true;
7179 for (Typed_identifier_list::const_iterator p = methods->begin();
7180 p != methods->end();
7181 ++p)
7182 {
7183 if (!p->name().empty())
7184 {
7185 std::string n = Gogo::unpack_hidden_name(p->name());
7186 char buf[20];
7187 snprintf(buf, sizeof buf, "%u_",
7188 static_cast<unsigned int>(n.length()));
7189 ret->append(buf);
7190 ret->append(n);
7191 }
7192 this->append_mangled_name(p->type(), gogo, ret);
7193 }
7194 this->seen_ = false;
7195 }
7196
7197 ret->push_back('e');
7198 }
7199
7200 // Export.
7201
7202 void
7203 Interface_type::do_export(Export* exp) const
7204 {
7205 exp->write_c_string("interface { ");
7206
7207 const Typed_identifier_list* methods = this->parse_methods_;
7208 if (methods != NULL)
7209 {
7210 for (Typed_identifier_list::const_iterator pm = methods->begin();
7211 pm != methods->end();
7212 ++pm)
7213 {
7214 if (pm->name().empty())
7215 {
7216 exp->write_c_string("? ");
7217 exp->write_type(pm->type());
7218 }
7219 else
7220 {
7221 exp->write_string(pm->name());
7222 exp->write_c_string(" (");
7223
7224 const Function_type* fntype = pm->type()->function_type();
7225
7226 bool first = true;
7227 const Typed_identifier_list* parameters = fntype->parameters();
7228 if (parameters != NULL)
7229 {
7230 bool is_varargs = fntype->is_varargs();
7231 for (Typed_identifier_list::const_iterator pp =
7232 parameters->begin();
7233 pp != parameters->end();
7234 ++pp)
7235 {
7236 if (first)
7237 first = false;
7238 else
7239 exp->write_c_string(", ");
7240 exp->write_name(pp->name());
7241 exp->write_c_string(" ");
7242 if (!is_varargs || pp + 1 != parameters->end())
7243 exp->write_type(pp->type());
7244 else
7245 {
7246 exp->write_c_string("...");
7247 Type *pptype = pp->type();
7248 exp->write_type(pptype->array_type()->element_type());
7249 }
7250 }
7251 }
7252
7253 exp->write_c_string(")");
7254
7255 const Typed_identifier_list* results = fntype->results();
7256 if (results != NULL)
7257 {
7258 exp->write_c_string(" ");
7259 if (results->size() == 1 && results->begin()->name().empty())
7260 exp->write_type(results->begin()->type());
7261 else
7262 {
7263 first = true;
7264 exp->write_c_string("(");
7265 for (Typed_identifier_list::const_iterator p =
7266 results->begin();
7267 p != results->end();
7268 ++p)
7269 {
7270 if (first)
7271 first = false;
7272 else
7273 exp->write_c_string(", ");
7274 exp->write_name(p->name());
7275 exp->write_c_string(" ");
7276 exp->write_type(p->type());
7277 }
7278 exp->write_c_string(")");
7279 }
7280 }
7281 }
7282
7283 exp->write_c_string("; ");
7284 }
7285 }
7286
7287 exp->write_c_string("}");
7288 }
7289
7290 // Import an interface type.
7291
7292 Interface_type*
7293 Interface_type::do_import(Import* imp)
7294 {
7295 imp->require_c_string("interface { ");
7296
7297 Typed_identifier_list* methods = new Typed_identifier_list;
7298 while (imp->peek_char() != '}')
7299 {
7300 std::string name = imp->read_identifier();
7301
7302 if (name == "?")
7303 {
7304 imp->require_c_string(" ");
7305 Type* t = imp->read_type();
7306 methods->push_back(Typed_identifier("", t, imp->location()));
7307 imp->require_c_string("; ");
7308 continue;
7309 }
7310
7311 imp->require_c_string(" (");
7312
7313 Typed_identifier_list* parameters;
7314 bool is_varargs = false;
7315 if (imp->peek_char() == ')')
7316 parameters = NULL;
7317 else
7318 {
7319 parameters = new Typed_identifier_list;
7320 while (true)
7321 {
7322 std::string name = imp->read_name();
7323 imp->require_c_string(" ");
7324
7325 if (imp->match_c_string("..."))
7326 {
7327 imp->advance(3);
7328 is_varargs = true;
7329 }
7330
7331 Type* ptype = imp->read_type();
7332 if (is_varargs)
7333 ptype = Type::make_array_type(ptype, NULL);
7334 parameters->push_back(Typed_identifier(name, ptype,
7335 imp->location()));
7336 if (imp->peek_char() != ',')
7337 break;
7338 go_assert(!is_varargs);
7339 imp->require_c_string(", ");
7340 }
7341 }
7342 imp->require_c_string(")");
7343
7344 Typed_identifier_list* results;
7345 if (imp->peek_char() != ' ')
7346 results = NULL;
7347 else
7348 {
7349 results = new Typed_identifier_list;
7350 imp->advance(1);
7351 if (imp->peek_char() != '(')
7352 {
7353 Type* rtype = imp->read_type();
7354 results->push_back(Typed_identifier("", rtype, imp->location()));
7355 }
7356 else
7357 {
7358 imp->advance(1);
7359 while (true)
7360 {
7361 std::string name = imp->read_name();
7362 imp->require_c_string(" ");
7363 Type* rtype = imp->read_type();
7364 results->push_back(Typed_identifier(name, rtype,
7365 imp->location()));
7366 if (imp->peek_char() != ',')
7367 break;
7368 imp->require_c_string(", ");
7369 }
7370 imp->require_c_string(")");
7371 }
7372 }
7373
7374 Function_type* fntype = Type::make_function_type(NULL, parameters,
7375 results,
7376 imp->location());
7377 if (is_varargs)
7378 fntype->set_is_varargs();
7379 methods->push_back(Typed_identifier(name, fntype, imp->location()));
7380
7381 imp->require_c_string("; ");
7382 }
7383
7384 imp->require_c_string("}");
7385
7386 if (methods->empty())
7387 {
7388 delete methods;
7389 methods = NULL;
7390 }
7391
7392 return Type::make_interface_type(methods, imp->location());
7393 }
7394
7395 // Make an interface type.
7396
7397 Interface_type*
7398 Type::make_interface_type(Typed_identifier_list* methods,
7399 Location location)
7400 {
7401 return new Interface_type(methods, location);
7402 }
7403
7404 // Make an empty interface type.
7405
7406 Interface_type*
7407 Type::make_empty_interface_type(Location location)
7408 {
7409 Interface_type* ret = new Interface_type(NULL, location);
7410 ret->finalize_methods();
7411 return ret;
7412 }
7413
7414 // Class Method.
7415
7416 // Bind a method to an object.
7417
7418 Expression*
7419 Method::bind_method(Expression* expr, Location location) const
7420 {
7421 if (this->stub_ == NULL)
7422 {
7423 // When there is no stub object, the binding is determined by
7424 // the child class.
7425 return this->do_bind_method(expr, location);
7426 }
7427 return Expression::make_bound_method(expr, this->stub_, location);
7428 }
7429
7430 // Return the named object associated with a method. This may only be
7431 // called after methods are finalized.
7432
7433 Named_object*
7434 Method::named_object() const
7435 {
7436 if (this->stub_ != NULL)
7437 return this->stub_;
7438 return this->do_named_object();
7439 }
7440
7441 // Class Named_method.
7442
7443 // The type of the method.
7444
7445 Function_type*
7446 Named_method::do_type() const
7447 {
7448 if (this->named_object_->is_function())
7449 return this->named_object_->func_value()->type();
7450 else if (this->named_object_->is_function_declaration())
7451 return this->named_object_->func_declaration_value()->type();
7452 else
7453 go_unreachable();
7454 }
7455
7456 // Return the location of the method receiver.
7457
7458 Location
7459 Named_method::do_receiver_location() const
7460 {
7461 return this->do_type()->receiver()->location();
7462 }
7463
7464 // Bind a method to an object.
7465
7466 Expression*
7467 Named_method::do_bind_method(Expression* expr, Location location) const
7468 {
7469 Named_object* no = this->named_object_;
7470 Bound_method_expression* bme = Expression::make_bound_method(expr, no,
7471 location);
7472 // If this is not a local method, and it does not use a stub, then
7473 // the real method expects a different type. We need to cast the
7474 // first argument.
7475 if (this->depth() > 0 && !this->needs_stub_method())
7476 {
7477 Function_type* ftype = this->do_type();
7478 go_assert(ftype->is_method());
7479 Type* frtype = ftype->receiver()->type();
7480 bme->set_first_argument_type(frtype);
7481 }
7482 return bme;
7483 }
7484
7485 // Class Interface_method.
7486
7487 // Bind a method to an object.
7488
7489 Expression*
7490 Interface_method::do_bind_method(Expression* expr,
7491 Location location) const
7492 {
7493 return Expression::make_interface_field_reference(expr, this->name_,
7494 location);
7495 }
7496
7497 // Class Methods.
7498
7499 // Insert a new method. Return true if it was inserted, false
7500 // otherwise.
7501
7502 bool
7503 Methods::insert(const std::string& name, Method* m)
7504 {
7505 std::pair<Method_map::iterator, bool> ins =
7506 this->methods_.insert(std::make_pair(name, m));
7507 if (ins.second)
7508 return true;
7509 else
7510 {
7511 Method* old_method = ins.first->second;
7512 if (m->depth() < old_method->depth())
7513 {
7514 delete old_method;
7515 ins.first->second = m;
7516 return true;
7517 }
7518 else
7519 {
7520 if (m->depth() == old_method->depth())
7521 old_method->set_is_ambiguous();
7522 return false;
7523 }
7524 }
7525 }
7526
7527 // Return the number of unambiguous methods.
7528
7529 size_t
7530 Methods::count() const
7531 {
7532 size_t ret = 0;
7533 for (Method_map::const_iterator p = this->methods_.begin();
7534 p != this->methods_.end();
7535 ++p)
7536 if (!p->second->is_ambiguous())
7537 ++ret;
7538 return ret;
7539 }
7540
7541 // Class Named_type.
7542
7543 // Return the name of the type.
7544
7545 const std::string&
7546 Named_type::name() const
7547 {
7548 return this->named_object_->name();
7549 }
7550
7551 // Return the name of the type to use in an error message.
7552
7553 std::string
7554 Named_type::message_name() const
7555 {
7556 return this->named_object_->message_name();
7557 }
7558
7559 // Whether this is an alias. There are currently only two aliases so
7560 // we just recognize them by name.
7561
7562 bool
7563 Named_type::is_alias() const
7564 {
7565 if (!this->is_builtin())
7566 return false;
7567 const std::string& name(this->name());
7568 return name == "byte" || name == "rune";
7569 }
7570
7571 // Return the base type for this type. We have to be careful about
7572 // circular type definitions, which are invalid but may be seen here.
7573
7574 Type*
7575 Named_type::named_base()
7576 {
7577 if (this->seen_)
7578 return this;
7579 this->seen_ = true;
7580 Type* ret = this->type_->base();
7581 this->seen_ = false;
7582 return ret;
7583 }
7584
7585 const Type*
7586 Named_type::named_base() const
7587 {
7588 if (this->seen_)
7589 return this;
7590 this->seen_ = true;
7591 const Type* ret = this->type_->base();
7592 this->seen_ = false;
7593 return ret;
7594 }
7595
7596 // Return whether this is an error type. We have to be careful about
7597 // circular type definitions, which are invalid but may be seen here.
7598
7599 bool
7600 Named_type::is_named_error_type() const
7601 {
7602 if (this->seen_)
7603 return false;
7604 this->seen_ = true;
7605 bool ret = this->type_->is_error_type();
7606 this->seen_ = false;
7607 return ret;
7608 }
7609
7610 // Whether this type is comparable. We have to be careful about
7611 // circular type definitions.
7612
7613 bool
7614 Named_type::named_type_is_comparable(std::string* reason) const
7615 {
7616 if (this->seen_)
7617 return false;
7618 this->seen_ = true;
7619 bool ret = Type::are_compatible_for_comparison(true, this->type_,
7620 this->type_, reason);
7621 this->seen_ = false;
7622 return ret;
7623 }
7624
7625 // Add a method to this type.
7626
7627 Named_object*
7628 Named_type::add_method(const std::string& name, Function* function)
7629 {
7630 if (this->local_methods_ == NULL)
7631 this->local_methods_ = new Bindings(NULL);
7632 return this->local_methods_->add_function(name, NULL, function);
7633 }
7634
7635 // Add a method declaration to this type.
7636
7637 Named_object*
7638 Named_type::add_method_declaration(const std::string& name, Package* package,
7639 Function_type* type,
7640 Location location)
7641 {
7642 if (this->local_methods_ == NULL)
7643 this->local_methods_ = new Bindings(NULL);
7644 return this->local_methods_->add_function_declaration(name, package, type,
7645 location);
7646 }
7647
7648 // Add an existing method to this type.
7649
7650 void
7651 Named_type::add_existing_method(Named_object* no)
7652 {
7653 if (this->local_methods_ == NULL)
7654 this->local_methods_ = new Bindings(NULL);
7655 this->local_methods_->add_named_object(no);
7656 }
7657
7658 // Look for a local method NAME, and returns its named object, or NULL
7659 // if not there.
7660
7661 Named_object*
7662 Named_type::find_local_method(const std::string& name) const
7663 {
7664 if (this->local_methods_ == NULL)
7665 return NULL;
7666 return this->local_methods_->lookup(name);
7667 }
7668
7669 // Return whether NAME is an unexported field or method, for better
7670 // error reporting.
7671
7672 bool
7673 Named_type::is_unexported_local_method(Gogo* gogo,
7674 const std::string& name) const
7675 {
7676 Bindings* methods = this->local_methods_;
7677 if (methods != NULL)
7678 {
7679 for (Bindings::const_declarations_iterator p =
7680 methods->begin_declarations();
7681 p != methods->end_declarations();
7682 ++p)
7683 {
7684 if (Gogo::is_hidden_name(p->first)
7685 && name == Gogo::unpack_hidden_name(p->first)
7686 && gogo->pack_hidden_name(name, false) != p->first)
7687 return true;
7688 }
7689 }
7690 return false;
7691 }
7692
7693 // Build the complete list of methods for this type, which means
7694 // recursively including all methods for anonymous fields. Create all
7695 // stub methods.
7696
7697 void
7698 Named_type::finalize_methods(Gogo* gogo)
7699 {
7700 if (this->all_methods_ != NULL)
7701 return;
7702
7703 if (this->local_methods_ != NULL
7704 && (this->points_to() != NULL || this->interface_type() != NULL))
7705 {
7706 const Bindings* lm = this->local_methods_;
7707 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
7708 p != lm->end_declarations();
7709 ++p)
7710 error_at(p->second->location(),
7711 "invalid pointer or interface receiver type");
7712 delete this->local_methods_;
7713 this->local_methods_ = NULL;
7714 return;
7715 }
7716
7717 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
7718 }
7719
7720 // Return the method NAME, or NULL if there isn't one or if it is
7721 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
7722 // ambiguous.
7723
7724 Method*
7725 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
7726 {
7727 return Type::method_function(this->all_methods_, name, is_ambiguous);
7728 }
7729
7730 // Return a pointer to the interface method table for this type for
7731 // the interface INTERFACE. IS_POINTER is true if this is for a
7732 // pointer to THIS.
7733
7734 tree
7735 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
7736 bool is_pointer)
7737 {
7738 go_assert(!interface->is_empty());
7739
7740 Interface_method_tables** pimt = (is_pointer
7741 ? &this->interface_method_tables_
7742 : &this->pointer_interface_method_tables_);
7743
7744 if (*pimt == NULL)
7745 *pimt = new Interface_method_tables(5);
7746
7747 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
7748 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
7749
7750 if (ins.second)
7751 {
7752 // This is a new entry in the hash table.
7753 go_assert(ins.first->second == NULL_TREE);
7754 ins.first->second = gogo->interface_method_table_for_type(interface,
7755 this,
7756 is_pointer);
7757 }
7758
7759 tree decl = ins.first->second;
7760 if (decl == error_mark_node)
7761 return error_mark_node;
7762 go_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
7763 return build_fold_addr_expr(decl);
7764 }
7765
7766 // Return whether a named type has any hidden fields.
7767
7768 bool
7769 Named_type::named_type_has_hidden_fields(std::string* reason) const
7770 {
7771 if (this->seen_)
7772 return false;
7773 this->seen_ = true;
7774 bool ret = this->type_->has_hidden_fields(this, reason);
7775 this->seen_ = false;
7776 return ret;
7777 }
7778
7779 // Look for a use of a complete type within another type. This is
7780 // used to check that we don't try to use a type within itself.
7781
7782 class Find_type_use : public Traverse
7783 {
7784 public:
7785 Find_type_use(Named_type* find_type)
7786 : Traverse(traverse_types),
7787 find_type_(find_type), found_(false)
7788 { }
7789
7790 // Whether we found the type.
7791 bool
7792 found() const
7793 { return this->found_; }
7794
7795 protected:
7796 int
7797 type(Type*);
7798
7799 private:
7800 // The type we are looking for.
7801 Named_type* find_type_;
7802 // Whether we found the type.
7803 bool found_;
7804 };
7805
7806 // Check for FIND_TYPE in TYPE.
7807
7808 int
7809 Find_type_use::type(Type* type)
7810 {
7811 if (type->named_type() != NULL && this->find_type_ == type->named_type())
7812 {
7813 this->found_ = true;
7814 return TRAVERSE_EXIT;
7815 }
7816
7817 // It's OK if we see a reference to the type in any type which is
7818 // essentially a pointer: a pointer, a slice, a function, a map, or
7819 // a channel.
7820 if (type->points_to() != NULL
7821 || type->is_slice_type()
7822 || type->function_type() != NULL
7823 || type->map_type() != NULL
7824 || type->channel_type() != NULL)
7825 return TRAVERSE_SKIP_COMPONENTS;
7826
7827 // For an interface, a reference to the type in a method type should
7828 // be ignored, but we have to consider direct inheritance. When
7829 // this is called, there may be cases of direct inheritance
7830 // represented as a method with no name.
7831 if (type->interface_type() != NULL)
7832 {
7833 const Typed_identifier_list* methods = type->interface_type()->methods();
7834 if (methods != NULL)
7835 {
7836 for (Typed_identifier_list::const_iterator p = methods->begin();
7837 p != methods->end();
7838 ++p)
7839 {
7840 if (p->name().empty())
7841 {
7842 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
7843 return TRAVERSE_EXIT;
7844 }
7845 }
7846 }
7847 return TRAVERSE_SKIP_COMPONENTS;
7848 }
7849
7850 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
7851 // to convert TYPE to the backend representation before we convert
7852 // FIND_TYPE_.
7853 if (type->named_type() != NULL)
7854 {
7855 switch (type->base()->classification())
7856 {
7857 case Type::TYPE_ERROR:
7858 case Type::TYPE_BOOLEAN:
7859 case Type::TYPE_INTEGER:
7860 case Type::TYPE_FLOAT:
7861 case Type::TYPE_COMPLEX:
7862 case Type::TYPE_STRING:
7863 case Type::TYPE_NIL:
7864 break;
7865
7866 case Type::TYPE_ARRAY:
7867 case Type::TYPE_STRUCT:
7868 this->find_type_->add_dependency(type->named_type());
7869 break;
7870
7871 case Type::TYPE_NAMED:
7872 case Type::TYPE_FORWARD:
7873 go_assert(saw_errors());
7874 break;
7875
7876 case Type::TYPE_VOID:
7877 case Type::TYPE_SINK:
7878 case Type::TYPE_FUNCTION:
7879 case Type::TYPE_POINTER:
7880 case Type::TYPE_CALL_MULTIPLE_RESULT:
7881 case Type::TYPE_MAP:
7882 case Type::TYPE_CHANNEL:
7883 case Type::TYPE_INTERFACE:
7884 default:
7885 go_unreachable();
7886 }
7887 }
7888
7889 return TRAVERSE_CONTINUE;
7890 }
7891
7892 // Verify that a named type does not refer to itself.
7893
7894 bool
7895 Named_type::do_verify()
7896 {
7897 if (this->is_verified_)
7898 return true;
7899 this->is_verified_ = true;
7900
7901 Find_type_use find(this);
7902 Type::traverse(this->type_, &find);
7903 if (find.found())
7904 {
7905 error_at(this->location_, "invalid recursive type %qs",
7906 this->message_name().c_str());
7907 this->is_error_ = true;
7908 return false;
7909 }
7910
7911 // Check whether any of the local methods overloads an existing
7912 // struct field or interface method. We don't need to check the
7913 // list of methods against itself: that is handled by the Bindings
7914 // code.
7915 if (this->local_methods_ != NULL)
7916 {
7917 Struct_type* st = this->type_->struct_type();
7918 if (st != NULL)
7919 {
7920 for (Bindings::const_declarations_iterator p =
7921 this->local_methods_->begin_declarations();
7922 p != this->local_methods_->end_declarations();
7923 ++p)
7924 {
7925 const std::string& name(p->first);
7926 if (st != NULL && st->find_local_field(name, NULL) != NULL)
7927 {
7928 error_at(p->second->location(),
7929 "method %qs redeclares struct field name",
7930 Gogo::message_name(name).c_str());
7931 }
7932 }
7933 }
7934 }
7935
7936 return true;
7937 }
7938
7939 // Return whether this type is or contains a pointer.
7940
7941 bool
7942 Named_type::do_has_pointer() const
7943 {
7944 if (this->seen_)
7945 return false;
7946 this->seen_ = true;
7947 bool ret = this->type_->has_pointer();
7948 this->seen_ = false;
7949 return ret;
7950 }
7951
7952 // Return whether comparisons for this type can use the identity
7953 // function.
7954
7955 bool
7956 Named_type::do_compare_is_identity(Gogo* gogo) const
7957 {
7958 // We don't use this->seen_ here because compare_is_identity may
7959 // call base() later, and that will mess up if seen_ is set here.
7960 if (this->seen_in_compare_is_identity_)
7961 return false;
7962 this->seen_in_compare_is_identity_ = true;
7963 bool ret = this->type_->compare_is_identity(gogo);
7964 this->seen_in_compare_is_identity_ = false;
7965 return ret;
7966 }
7967
7968 // Return a hash code. This is used for method lookup. We simply
7969 // hash on the name itself.
7970
7971 unsigned int
7972 Named_type::do_hash_for_method(Gogo* gogo) const
7973 {
7974 if (this->is_alias())
7975 return this->type_->named_type()->do_hash_for_method(gogo);
7976
7977 const std::string& name(this->named_object()->name());
7978 unsigned int ret = Type::hash_string(name, 0);
7979
7980 // GOGO will be NULL here when called from Type_hash_identical.
7981 // That is OK because that is only used for internal hash tables
7982 // where we are going to be comparing named types for equality. In
7983 // other cases, which are cases where the runtime is going to
7984 // compare hash codes to see if the types are the same, we need to
7985 // include the pkgpath in the hash.
7986 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
7987 {
7988 const Package* package = this->named_object()->package();
7989 if (package == NULL)
7990 ret = Type::hash_string(gogo->pkgpath(), ret);
7991 else
7992 ret = Type::hash_string(package->pkgpath(), ret);
7993 }
7994
7995 return ret;
7996 }
7997
7998 // Convert a named type to the backend representation. In order to
7999 // get dependencies right, we fill in a dummy structure for this type,
8000 // then convert all the dependencies, then complete this type. When
8001 // this function is complete, the size of the type is known.
8002
8003 void
8004 Named_type::convert(Gogo* gogo)
8005 {
8006 if (this->is_error_ || this->is_converted_)
8007 return;
8008
8009 this->create_placeholder(gogo);
8010
8011 // If we are called to turn unsafe.Sizeof into a constant, we may
8012 // not have verified the type yet. We have to make sure it is
8013 // verified, since that sets the list of dependencies.
8014 this->verify();
8015
8016 // Convert all the dependencies. If they refer indirectly back to
8017 // this type, they will pick up the intermediate tree we just
8018 // created.
8019 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
8020 p != this->dependencies_.end();
8021 ++p)
8022 (*p)->convert(gogo);
8023
8024 // Complete this type.
8025 Btype* bt = this->named_btype_;
8026 Type* base = this->type_->base();
8027 switch (base->classification())
8028 {
8029 case TYPE_VOID:
8030 case TYPE_BOOLEAN:
8031 case TYPE_INTEGER:
8032 case TYPE_FLOAT:
8033 case TYPE_COMPLEX:
8034 case TYPE_STRING:
8035 case TYPE_NIL:
8036 break;
8037
8038 case TYPE_MAP:
8039 case TYPE_CHANNEL:
8040 break;
8041
8042 case TYPE_FUNCTION:
8043 case TYPE_POINTER:
8044 // The size of these types is already correct. We don't worry
8045 // about filling them in until later, when we also track
8046 // circular references.
8047 break;
8048
8049 case TYPE_STRUCT:
8050 {
8051 std::vector<Backend::Btyped_identifier> bfields;
8052 get_backend_struct_fields(gogo, base->struct_type()->fields(),
8053 true, &bfields);
8054 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8055 bt = gogo->backend()->error_type();
8056 }
8057 break;
8058
8059 case TYPE_ARRAY:
8060 // Slice types were completed in create_placeholder.
8061 if (!base->is_slice_type())
8062 {
8063 Btype* bet = base->array_type()->get_backend_element(gogo, true);
8064 Bexpression* blen = base->array_type()->get_backend_length(gogo);
8065 if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
8066 bt = gogo->backend()->error_type();
8067 }
8068 break;
8069
8070 case TYPE_INTERFACE:
8071 // Interface types were completed in create_placeholder.
8072 break;
8073
8074 case TYPE_ERROR:
8075 return;
8076
8077 default:
8078 case TYPE_SINK:
8079 case TYPE_CALL_MULTIPLE_RESULT:
8080 case TYPE_NAMED:
8081 case TYPE_FORWARD:
8082 go_unreachable();
8083 }
8084
8085 this->named_btype_ = bt;
8086 this->is_converted_ = true;
8087 this->is_placeholder_ = false;
8088 }
8089
8090 // Create the placeholder for a named type. This is the first step in
8091 // converting to the backend representation.
8092
8093 void
8094 Named_type::create_placeholder(Gogo* gogo)
8095 {
8096 if (this->is_error_)
8097 this->named_btype_ = gogo->backend()->error_type();
8098
8099 if (this->named_btype_ != NULL)
8100 return;
8101
8102 // Create the structure for this type. Note that because we call
8103 // base() here, we don't attempt to represent a named type defined
8104 // as another named type. Instead both named types will point to
8105 // different base representations.
8106 Type* base = this->type_->base();
8107 Btype* bt;
8108 bool set_name = true;
8109 switch (base->classification())
8110 {
8111 case TYPE_ERROR:
8112 this->is_error_ = true;
8113 this->named_btype_ = gogo->backend()->error_type();
8114 return;
8115
8116 case TYPE_VOID:
8117 case TYPE_BOOLEAN:
8118 case TYPE_INTEGER:
8119 case TYPE_FLOAT:
8120 case TYPE_COMPLEX:
8121 case TYPE_STRING:
8122 case TYPE_NIL:
8123 // These are simple basic types, we can just create them
8124 // directly.
8125 bt = Type::get_named_base_btype(gogo, base);
8126 break;
8127
8128 case TYPE_MAP:
8129 case TYPE_CHANNEL:
8130 // All maps and channels have the same backend representation.
8131 bt = Type::get_named_base_btype(gogo, base);
8132 break;
8133
8134 case TYPE_FUNCTION:
8135 case TYPE_POINTER:
8136 {
8137 bool for_function = base->classification() == TYPE_FUNCTION;
8138 bt = gogo->backend()->placeholder_pointer_type(this->name(),
8139 this->location_,
8140 for_function);
8141 set_name = false;
8142 }
8143 break;
8144
8145 case TYPE_STRUCT:
8146 bt = gogo->backend()->placeholder_struct_type(this->name(),
8147 this->location_);
8148 this->is_placeholder_ = true;
8149 set_name = false;
8150 break;
8151
8152 case TYPE_ARRAY:
8153 if (base->is_slice_type())
8154 bt = gogo->backend()->placeholder_struct_type(this->name(),
8155 this->location_);
8156 else
8157 {
8158 bt = gogo->backend()->placeholder_array_type(this->name(),
8159 this->location_);
8160 this->is_placeholder_ = true;
8161 }
8162 set_name = false;
8163 break;
8164
8165 case TYPE_INTERFACE:
8166 if (base->interface_type()->is_empty())
8167 bt = Interface_type::get_backend_empty_interface_type(gogo);
8168 else
8169 {
8170 bt = gogo->backend()->placeholder_struct_type(this->name(),
8171 this->location_);
8172 set_name = false;
8173 }
8174 break;
8175
8176 default:
8177 case TYPE_SINK:
8178 case TYPE_CALL_MULTIPLE_RESULT:
8179 case TYPE_NAMED:
8180 case TYPE_FORWARD:
8181 go_unreachable();
8182 }
8183
8184 if (set_name)
8185 bt = gogo->backend()->named_type(this->name(), bt, this->location_);
8186
8187 this->named_btype_ = bt;
8188
8189 if (base->is_slice_type())
8190 {
8191 // We do not record slices as dependencies of other types,
8192 // because we can fill them in completely here with the final
8193 // size.
8194 std::vector<Backend::Btyped_identifier> bfields;
8195 get_backend_slice_fields(gogo, base->array_type(), true, &bfields);
8196 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8197 this->named_btype_ = gogo->backend()->error_type();
8198 }
8199 else if (base->interface_type() != NULL
8200 && !base->interface_type()->is_empty())
8201 {
8202 // We do not record interfaces as dependencies of other types,
8203 // because we can fill them in completely here with the final
8204 // size.
8205 std::vector<Backend::Btyped_identifier> bfields;
8206 get_backend_interface_fields(gogo, base->interface_type(), true,
8207 &bfields);
8208 if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
8209 this->named_btype_ = gogo->backend()->error_type();
8210 }
8211 }
8212
8213 // Get a tree for a named type.
8214
8215 Btype*
8216 Named_type::do_get_backend(Gogo* gogo)
8217 {
8218 if (this->is_error_)
8219 return gogo->backend()->error_type();
8220
8221 Btype* bt = this->named_btype_;
8222
8223 if (!gogo->named_types_are_converted())
8224 {
8225 // We have not completed converting named types. NAMED_BTYPE_
8226 // is a placeholder and we shouldn't do anything further.
8227 if (bt != NULL)
8228 return bt;
8229
8230 // We don't build dependencies for types whose sizes do not
8231 // change or are not relevant, so we may see them here while
8232 // converting types.
8233 this->create_placeholder(gogo);
8234 bt = this->named_btype_;
8235 go_assert(bt != NULL);
8236 return bt;
8237 }
8238
8239 // We are not converting types. This should only be called if the
8240 // type has already been converted.
8241 if (!this->is_converted_)
8242 {
8243 go_assert(saw_errors());
8244 return gogo->backend()->error_type();
8245 }
8246
8247 go_assert(bt != NULL);
8248
8249 // Complete the tree.
8250 Type* base = this->type_->base();
8251 Btype* bt1;
8252 switch (base->classification())
8253 {
8254 case TYPE_ERROR:
8255 return gogo->backend()->error_type();
8256
8257 case TYPE_VOID:
8258 case TYPE_BOOLEAN:
8259 case TYPE_INTEGER:
8260 case TYPE_FLOAT:
8261 case TYPE_COMPLEX:
8262 case TYPE_STRING:
8263 case TYPE_NIL:
8264 case TYPE_MAP:
8265 case TYPE_CHANNEL:
8266 return bt;
8267
8268 case TYPE_STRUCT:
8269 if (!this->seen_in_get_backend_)
8270 {
8271 this->seen_in_get_backend_ = true;
8272 base->struct_type()->finish_backend_fields(gogo);
8273 this->seen_in_get_backend_ = false;
8274 }
8275 return bt;
8276
8277 case TYPE_ARRAY:
8278 if (!this->seen_in_get_backend_)
8279 {
8280 this->seen_in_get_backend_ = true;
8281 base->array_type()->finish_backend_element(gogo);
8282 this->seen_in_get_backend_ = false;
8283 }
8284 return bt;
8285
8286 case TYPE_INTERFACE:
8287 if (!this->seen_in_get_backend_)
8288 {
8289 this->seen_in_get_backend_ = true;
8290 base->interface_type()->finish_backend_methods(gogo);
8291 this->seen_in_get_backend_ = false;
8292 }
8293 return bt;
8294
8295 case TYPE_FUNCTION:
8296 // Don't build a circular data structure. GENERIC can't handle
8297 // it.
8298 if (this->seen_in_get_backend_)
8299 {
8300 this->is_circular_ = true;
8301 return gogo->backend()->circular_pointer_type(bt, true);
8302 }
8303 this->seen_in_get_backend_ = true;
8304 bt1 = Type::get_named_base_btype(gogo, base);
8305 this->seen_in_get_backend_ = false;
8306 if (this->is_circular_)
8307 bt1 = gogo->backend()->circular_pointer_type(bt, true);
8308 if (!gogo->backend()->set_placeholder_function_type(bt, bt1))
8309 bt = gogo->backend()->error_type();
8310 return bt;
8311
8312 case TYPE_POINTER:
8313 // Don't build a circular data structure. GENERIC can't handle
8314 // it.
8315 if (this->seen_in_get_backend_)
8316 {
8317 this->is_circular_ = true;
8318 return gogo->backend()->circular_pointer_type(bt, false);
8319 }
8320 this->seen_in_get_backend_ = true;
8321 bt1 = Type::get_named_base_btype(gogo, base);
8322 this->seen_in_get_backend_ = false;
8323 if (this->is_circular_)
8324 bt1 = gogo->backend()->circular_pointer_type(bt, false);
8325 if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
8326 bt = gogo->backend()->error_type();
8327 return bt;
8328
8329 default:
8330 case TYPE_SINK:
8331 case TYPE_CALL_MULTIPLE_RESULT:
8332 case TYPE_NAMED:
8333 case TYPE_FORWARD:
8334 go_unreachable();
8335 }
8336
8337 go_unreachable();
8338 }
8339
8340 // Build a type descriptor for a named type.
8341
8342 Expression*
8343 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8344 {
8345 if (name == NULL && this->is_alias())
8346 return this->type_->type_descriptor(gogo, this->type_);
8347
8348 // If NAME is not NULL, then we don't really want the type
8349 // descriptor for this type; we want the descriptor for the
8350 // underlying type, giving it the name NAME.
8351 return this->named_type_descriptor(gogo, this->type_,
8352 name == NULL ? this : name);
8353 }
8354
8355 // Add to the reflection string. This is used mostly for the name of
8356 // the type used in a type descriptor, not for actual reflection
8357 // strings.
8358
8359 void
8360 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
8361 {
8362 if (this->is_alias())
8363 {
8364 this->append_reflection(this->type_, gogo, ret);
8365 return;
8366 }
8367 if (!this->is_builtin())
8368 {
8369 // We handle -fgo-prefix and -fgo-pkgpath differently here for
8370 // compatibility with how the compiler worked before
8371 // -fgo-pkgpath was introduced. When -fgo-pkgpath is specified,
8372 // we use it to make a unique reflection string, so that the
8373 // type canonicalization in the reflect package will work. In
8374 // order to be compatible with the gc compiler, we put tabs into
8375 // the package path, so that the reflect methods can discard it.
8376 const Package* package = this->named_object_->package();
8377 if (gogo->pkgpath_from_option())
8378 {
8379 ret->push_back('\t');
8380 ret->append(package != NULL
8381 ? package->pkgpath_symbol()
8382 : gogo->pkgpath_symbol());
8383 ret->push_back('\t');
8384 }
8385 ret->append(package != NULL
8386 ? package->package_name()
8387 : gogo->package_name());
8388 ret->push_back('.');
8389 }
8390 if (this->in_function_ != NULL)
8391 {
8392 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
8393 ret->push_back('$');
8394 if (this->in_function_index_ > 0)
8395 {
8396 char buf[30];
8397 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8398 ret->append(buf);
8399 ret->push_back('$');
8400 }
8401 }
8402 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
8403 }
8404
8405 // Get the mangled name.
8406
8407 void
8408 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8409 {
8410 if (this->is_alias())
8411 {
8412 this->append_mangled_name(this->type_, gogo, ret);
8413 return;
8414 }
8415 Named_object* no = this->named_object_;
8416 std::string name;
8417 if (this->is_builtin())
8418 go_assert(this->in_function_ == NULL);
8419 else
8420 {
8421 const std::string& pkgpath(no->package() == NULL
8422 ? gogo->pkgpath_symbol()
8423 : no->package()->pkgpath_symbol());
8424 name = pkgpath;
8425 name.append(1, '.');
8426 if (this->in_function_ != NULL)
8427 {
8428 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
8429 name.append(1, '$');
8430 if (this->in_function_index_ > 0)
8431 {
8432 char buf[30];
8433 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
8434 name.append(buf);
8435 name.append(1, '$');
8436 }
8437 }
8438 }
8439 name.append(Gogo::unpack_hidden_name(no->name()));
8440 char buf[20];
8441 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
8442 ret->append(buf);
8443 ret->append(name);
8444 }
8445
8446 // Export the type. This is called to export a global type.
8447
8448 void
8449 Named_type::export_named_type(Export* exp, const std::string&) const
8450 {
8451 // We don't need to write the name of the type here, because it will
8452 // be written by Export::write_type anyhow.
8453 exp->write_c_string("type ");
8454 exp->write_type(this);
8455 exp->write_c_string(";\n");
8456 }
8457
8458 // Import a named type.
8459
8460 void
8461 Named_type::import_named_type(Import* imp, Named_type** ptype)
8462 {
8463 imp->require_c_string("type ");
8464 Type *type = imp->read_type();
8465 *ptype = type->named_type();
8466 go_assert(*ptype != NULL);
8467 imp->require_c_string(";\n");
8468 }
8469
8470 // Export the type when it is referenced by another type. In this
8471 // case Export::export_type will already have issued the name.
8472
8473 void
8474 Named_type::do_export(Export* exp) const
8475 {
8476 exp->write_type(this->type_);
8477
8478 // To save space, we only export the methods directly attached to
8479 // this type.
8480 Bindings* methods = this->local_methods_;
8481 if (methods == NULL)
8482 return;
8483
8484 exp->write_c_string("\n");
8485 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
8486 p != methods->end_definitions();
8487 ++p)
8488 {
8489 exp->write_c_string(" ");
8490 (*p)->export_named_object(exp);
8491 }
8492
8493 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
8494 p != methods->end_declarations();
8495 ++p)
8496 {
8497 if (p->second->is_function_declaration())
8498 {
8499 exp->write_c_string(" ");
8500 p->second->export_named_object(exp);
8501 }
8502 }
8503 }
8504
8505 // Make a named type.
8506
8507 Named_type*
8508 Type::make_named_type(Named_object* named_object, Type* type,
8509 Location location)
8510 {
8511 return new Named_type(named_object, type, location);
8512 }
8513
8514 // Finalize the methods for TYPE. It will be a named type or a struct
8515 // type. This sets *ALL_METHODS to the list of methods, and builds
8516 // all required stubs.
8517
8518 void
8519 Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
8520 Methods** all_methods)
8521 {
8522 *all_methods = NULL;
8523 Types_seen types_seen;
8524 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
8525 all_methods);
8526 Type::build_stub_methods(gogo, type, *all_methods, location);
8527 }
8528
8529 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
8530 // build up the struct field indexes as we go. DEPTH is the depth of
8531 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
8532 // adding these methods for an anonymous field with pointer type.
8533 // NEEDS_STUB_METHOD is true if we need to use a stub method which
8534 // calls the real method. TYPES_SEEN is used to avoid infinite
8535 // recursion.
8536
8537 void
8538 Type::add_methods_for_type(const Type* type,
8539 const Method::Field_indexes* field_indexes,
8540 unsigned int depth,
8541 bool is_embedded_pointer,
8542 bool needs_stub_method,
8543 Types_seen* types_seen,
8544 Methods** methods)
8545 {
8546 // Pointer types may not have methods.
8547 if (type->points_to() != NULL)
8548 return;
8549
8550 const Named_type* nt = type->named_type();
8551 if (nt != NULL)
8552 {
8553 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
8554 if (!ins.second)
8555 return;
8556 }
8557
8558 if (nt != NULL)
8559 Type::add_local_methods_for_type(nt, field_indexes, depth,
8560 is_embedded_pointer, needs_stub_method,
8561 methods);
8562
8563 Type::add_embedded_methods_for_type(type, field_indexes, depth,
8564 is_embedded_pointer, needs_stub_method,
8565 types_seen, methods);
8566
8567 // If we are called with depth > 0, then we are looking at an
8568 // anonymous field of a struct. If such a field has interface type,
8569 // then we need to add the interface methods. We don't want to add
8570 // them when depth == 0, because we will already handle them
8571 // following the usual rules for an interface type.
8572 if (depth > 0)
8573 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
8574 }
8575
8576 // Add the local methods for the named type NT to *METHODS. The
8577 // parameters are as for add_methods_to_type.
8578
8579 void
8580 Type::add_local_methods_for_type(const Named_type* nt,
8581 const Method::Field_indexes* field_indexes,
8582 unsigned int depth,
8583 bool is_embedded_pointer,
8584 bool needs_stub_method,
8585 Methods** methods)
8586 {
8587 const Bindings* local_methods = nt->local_methods();
8588 if (local_methods == NULL)
8589 return;
8590
8591 if (*methods == NULL)
8592 *methods = new Methods();
8593
8594 for (Bindings::const_declarations_iterator p =
8595 local_methods->begin_declarations();
8596 p != local_methods->end_declarations();
8597 ++p)
8598 {
8599 Named_object* no = p->second;
8600 bool is_value_method = (is_embedded_pointer
8601 || !Type::method_expects_pointer(no));
8602 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
8603 (needs_stub_method
8604 || (depth > 0 && is_value_method)));
8605 if (!(*methods)->insert(no->name(), m))
8606 delete m;
8607 }
8608 }
8609
8610 // Add the embedded methods for TYPE to *METHODS. These are the
8611 // methods attached to anonymous fields. The parameters are as for
8612 // add_methods_to_type.
8613
8614 void
8615 Type::add_embedded_methods_for_type(const Type* type,
8616 const Method::Field_indexes* field_indexes,
8617 unsigned int depth,
8618 bool is_embedded_pointer,
8619 bool needs_stub_method,
8620 Types_seen* types_seen,
8621 Methods** methods)
8622 {
8623 // Look for anonymous fields in TYPE. TYPE has fields if it is a
8624 // struct.
8625 const Struct_type* st = type->struct_type();
8626 if (st == NULL)
8627 return;
8628
8629 const Struct_field_list* fields = st->fields();
8630 if (fields == NULL)
8631 return;
8632
8633 unsigned int i = 0;
8634 for (Struct_field_list::const_iterator pf = fields->begin();
8635 pf != fields->end();
8636 ++pf, ++i)
8637 {
8638 if (!pf->is_anonymous())
8639 continue;
8640
8641 Type* ftype = pf->type();
8642 bool is_pointer = false;
8643 if (ftype->points_to() != NULL)
8644 {
8645 ftype = ftype->points_to();
8646 is_pointer = true;
8647 }
8648 Named_type* fnt = ftype->named_type();
8649 if (fnt == NULL)
8650 {
8651 // This is an error, but it will be diagnosed elsewhere.
8652 continue;
8653 }
8654
8655 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
8656 sub_field_indexes->next = field_indexes;
8657 sub_field_indexes->field_index = i;
8658
8659 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
8660 (is_embedded_pointer || is_pointer),
8661 (needs_stub_method
8662 || is_pointer
8663 || i > 0),
8664 types_seen,
8665 methods);
8666 }
8667 }
8668
8669 // If TYPE is an interface type, then add its method to *METHODS.
8670 // This is for interface methods attached to an anonymous field. The
8671 // parameters are as for add_methods_for_type.
8672
8673 void
8674 Type::add_interface_methods_for_type(const Type* type,
8675 const Method::Field_indexes* field_indexes,
8676 unsigned int depth,
8677 Methods** methods)
8678 {
8679 const Interface_type* it = type->interface_type();
8680 if (it == NULL)
8681 return;
8682
8683 const Typed_identifier_list* imethods = it->methods();
8684 if (imethods == NULL)
8685 return;
8686
8687 if (*methods == NULL)
8688 *methods = new Methods();
8689
8690 for (Typed_identifier_list::const_iterator pm = imethods->begin();
8691 pm != imethods->end();
8692 ++pm)
8693 {
8694 Function_type* fntype = pm->type()->function_type();
8695 if (fntype == NULL)
8696 {
8697 // This is an error, but it should be reported elsewhere
8698 // when we look at the methods for IT.
8699 continue;
8700 }
8701 go_assert(!fntype->is_method());
8702 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
8703 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
8704 field_indexes, depth);
8705 if (!(*methods)->insert(pm->name(), m))
8706 delete m;
8707 }
8708 }
8709
8710 // Build stub methods for TYPE as needed. METHODS is the set of
8711 // methods for the type. A stub method may be needed when a type
8712 // inherits a method from an anonymous field. When we need the
8713 // address of the method, as in a type descriptor, we need to build a
8714 // little stub which does the required field dereferences and jumps to
8715 // the real method. LOCATION is the location of the type definition.
8716
8717 void
8718 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
8719 Location location)
8720 {
8721 if (methods == NULL)
8722 return;
8723 for (Methods::const_iterator p = methods->begin();
8724 p != methods->end();
8725 ++p)
8726 {
8727 Method* m = p->second;
8728 if (m->is_ambiguous() || !m->needs_stub_method())
8729 continue;
8730
8731 const std::string& name(p->first);
8732
8733 // Build a stub method.
8734
8735 const Function_type* fntype = m->type();
8736
8737 static unsigned int counter;
8738 char buf[100];
8739 snprintf(buf, sizeof buf, "$this%u", counter);
8740 ++counter;
8741
8742 Type* receiver_type = const_cast<Type*>(type);
8743 if (!m->is_value_method())
8744 receiver_type = Type::make_pointer_type(receiver_type);
8745 Location receiver_location = m->receiver_location();
8746 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
8747 receiver_location);
8748
8749 const Typed_identifier_list* fnparams = fntype->parameters();
8750 Typed_identifier_list* stub_params;
8751 if (fnparams == NULL || fnparams->empty())
8752 stub_params = NULL;
8753 else
8754 {
8755 // We give each stub parameter a unique name.
8756 stub_params = new Typed_identifier_list();
8757 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
8758 pp != fnparams->end();
8759 ++pp)
8760 {
8761 char pbuf[100];
8762 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
8763 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
8764 pp->location()));
8765 ++counter;
8766 }
8767 }
8768
8769 const Typed_identifier_list* fnresults = fntype->results();
8770 Typed_identifier_list* stub_results;
8771 if (fnresults == NULL || fnresults->empty())
8772 stub_results = NULL;
8773 else
8774 {
8775 // We create the result parameters without any names, since
8776 // we won't refer to them.
8777 stub_results = new Typed_identifier_list();
8778 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
8779 pr != fnresults->end();
8780 ++pr)
8781 stub_results->push_back(Typed_identifier("", pr->type(),
8782 pr->location()));
8783 }
8784
8785 Function_type* stub_type = Type::make_function_type(receiver,
8786 stub_params,
8787 stub_results,
8788 fntype->location());
8789 if (fntype->is_varargs())
8790 stub_type->set_is_varargs();
8791
8792 // We only create the function in the package which creates the
8793 // type.
8794 const Package* package;
8795 if (type->named_type() == NULL)
8796 package = NULL;
8797 else
8798 package = type->named_type()->named_object()->package();
8799 Named_object* stub;
8800 if (package != NULL)
8801 stub = Named_object::make_function_declaration(name, package,
8802 stub_type, location);
8803 else
8804 {
8805 stub = gogo->start_function(name, stub_type, false,
8806 fntype->location());
8807 Type::build_one_stub_method(gogo, m, buf, stub_params,
8808 fntype->is_varargs(), location);
8809 gogo->finish_function(fntype->location());
8810 }
8811
8812 m->set_stub_object(stub);
8813 }
8814 }
8815
8816 // Build a stub method which adjusts the receiver as required to call
8817 // METHOD. RECEIVER_NAME is the name we used for the receiver.
8818 // PARAMS is the list of function parameters.
8819
8820 void
8821 Type::build_one_stub_method(Gogo* gogo, Method* method,
8822 const char* receiver_name,
8823 const Typed_identifier_list* params,
8824 bool is_varargs,
8825 Location location)
8826 {
8827 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
8828 go_assert(receiver_object != NULL);
8829
8830 Expression* expr = Expression::make_var_reference(receiver_object, location);
8831 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
8832 if (expr->type()->points_to() == NULL)
8833 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8834
8835 Expression_list* arguments;
8836 if (params == NULL || params->empty())
8837 arguments = NULL;
8838 else
8839 {
8840 arguments = new Expression_list();
8841 for (Typed_identifier_list::const_iterator p = params->begin();
8842 p != params->end();
8843 ++p)
8844 {
8845 Named_object* param = gogo->lookup(p->name(), NULL);
8846 go_assert(param != NULL);
8847 Expression* param_ref = Expression::make_var_reference(param,
8848 location);
8849 arguments->push_back(param_ref);
8850 }
8851 }
8852
8853 Expression* func = method->bind_method(expr, location);
8854 go_assert(func != NULL);
8855 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
8856 location);
8857 call->set_hidden_fields_are_ok();
8858 size_t count = call->result_count();
8859 if (count == 0)
8860 gogo->add_statement(Statement::make_statement(call, true));
8861 else
8862 {
8863 Expression_list* retvals = new Expression_list();
8864 if (count <= 1)
8865 retvals->push_back(call);
8866 else
8867 {
8868 for (size_t i = 0; i < count; ++i)
8869 retvals->push_back(Expression::make_call_result(call, i));
8870 }
8871 Return_statement* retstat = Statement::make_return_statement(retvals,
8872 location);
8873
8874 // We can return values with hidden fields from a stub. This is
8875 // necessary if the method is itself hidden.
8876 retstat->set_hidden_fields_are_ok();
8877
8878 gogo->add_statement(retstat);
8879 }
8880 }
8881
8882 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
8883 // in reverse order.
8884
8885 Expression*
8886 Type::apply_field_indexes(Expression* expr,
8887 const Method::Field_indexes* field_indexes,
8888 Location location)
8889 {
8890 if (field_indexes == NULL)
8891 return expr;
8892 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
8893 Struct_type* stype = expr->type()->deref()->struct_type();
8894 go_assert(stype != NULL
8895 && field_indexes->field_index < stype->field_count());
8896 if (expr->type()->struct_type() == NULL)
8897 {
8898 go_assert(expr->type()->points_to() != NULL);
8899 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8900 go_assert(expr->type()->struct_type() == stype);
8901 }
8902 return Expression::make_field_reference(expr, field_indexes->field_index,
8903 location);
8904 }
8905
8906 // Return whether NO is a method for which the receiver is a pointer.
8907
8908 bool
8909 Type::method_expects_pointer(const Named_object* no)
8910 {
8911 const Function_type *fntype;
8912 if (no->is_function())
8913 fntype = no->func_value()->type();
8914 else if (no->is_function_declaration())
8915 fntype = no->func_declaration_value()->type();
8916 else
8917 go_unreachable();
8918 return fntype->receiver()->type()->points_to() != NULL;
8919 }
8920
8921 // Given a set of methods for a type, METHODS, return the method NAME,
8922 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
8923 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
8924 // but is ambiguous (and return NULL).
8925
8926 Method*
8927 Type::method_function(const Methods* methods, const std::string& name,
8928 bool* is_ambiguous)
8929 {
8930 if (is_ambiguous != NULL)
8931 *is_ambiguous = false;
8932 if (methods == NULL)
8933 return NULL;
8934 Methods::const_iterator p = methods->find(name);
8935 if (p == methods->end())
8936 return NULL;
8937 Method* m = p->second;
8938 if (m->is_ambiguous())
8939 {
8940 if (is_ambiguous != NULL)
8941 *is_ambiguous = true;
8942 return NULL;
8943 }
8944 return m;
8945 }
8946
8947 // Look for field or method NAME for TYPE. Return an Expression for
8948 // the field or method bound to EXPR. If there is no such field or
8949 // method, give an appropriate error and return an error expression.
8950
8951 Expression*
8952 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
8953 const std::string& name,
8954 Location location)
8955 {
8956 if (type->deref()->is_error_type())
8957 return Expression::make_error(location);
8958
8959 const Named_type* nt = type->deref()->named_type();
8960 const Struct_type* st = type->deref()->struct_type();
8961 const Interface_type* it = type->interface_type();
8962
8963 // If this is a pointer to a pointer, then it is possible that the
8964 // pointed-to type has methods.
8965 bool dereferenced = false;
8966 if (nt == NULL
8967 && st == NULL
8968 && it == NULL
8969 && type->points_to() != NULL
8970 && type->points_to()->points_to() != NULL)
8971 {
8972 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8973 type = type->points_to();
8974 if (type->deref()->is_error_type())
8975 return Expression::make_error(location);
8976 nt = type->points_to()->named_type();
8977 st = type->points_to()->struct_type();
8978 dereferenced = true;
8979 }
8980
8981 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
8982 || expr->is_addressable());
8983 std::vector<const Named_type*> seen;
8984 bool is_method = false;
8985 bool found_pointer_method = false;
8986 std::string ambig1;
8987 std::string ambig2;
8988 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
8989 &seen, NULL, &is_method,
8990 &found_pointer_method, &ambig1, &ambig2))
8991 {
8992 Expression* ret;
8993 if (!is_method)
8994 {
8995 go_assert(st != NULL);
8996 if (type->struct_type() == NULL)
8997 {
8998 go_assert(type->points_to() != NULL);
8999 expr = Expression::make_unary(OPERATOR_MULT, expr,
9000 location);
9001 go_assert(expr->type()->struct_type() == st);
9002 }
9003 ret = st->field_reference(expr, name, location);
9004 }
9005 else if (it != NULL && it->find_method(name) != NULL)
9006 ret = Expression::make_interface_field_reference(expr, name,
9007 location);
9008 else
9009 {
9010 Method* m;
9011 if (nt != NULL)
9012 m = nt->method_function(name, NULL);
9013 else if (st != NULL)
9014 m = st->method_function(name, NULL);
9015 else
9016 go_unreachable();
9017 go_assert(m != NULL);
9018 if (dereferenced && m->is_value_method())
9019 {
9020 error_at(location,
9021 "calling value method requires explicit dereference");
9022 return Expression::make_error(location);
9023 }
9024 if (!m->is_value_method() && expr->type()->points_to() == NULL)
9025 expr = Expression::make_unary(OPERATOR_AND, expr, location);
9026 ret = m->bind_method(expr, location);
9027 }
9028 go_assert(ret != NULL);
9029 return ret;
9030 }
9031 else
9032 {
9033 if (!ambig1.empty())
9034 error_at(location, "%qs is ambiguous via %qs and %qs",
9035 Gogo::message_name(name).c_str(), ambig1.c_str(),
9036 ambig2.c_str());
9037 else if (found_pointer_method)
9038 error_at(location, "method requires a pointer receiver");
9039 else if (nt == NULL && st == NULL && it == NULL)
9040 error_at(location,
9041 ("reference to field %qs in object which "
9042 "has no fields or methods"),
9043 Gogo::message_name(name).c_str());
9044 else
9045 {
9046 bool is_unexported;
9047 if (!Gogo::is_hidden_name(name))
9048 is_unexported = false;
9049 else
9050 {
9051 std::string unpacked = Gogo::unpack_hidden_name(name);
9052 seen.clear();
9053 is_unexported = Type::is_unexported_field_or_method(gogo, type,
9054 unpacked,
9055 &seen);
9056 }
9057 if (is_unexported)
9058 error_at(location, "reference to unexported field or method %qs",
9059 Gogo::message_name(name).c_str());
9060 else
9061 error_at(location, "reference to undefined field or method %qs",
9062 Gogo::message_name(name).c_str());
9063 }
9064 return Expression::make_error(location);
9065 }
9066 }
9067
9068 // Look in TYPE for a field or method named NAME, return true if one
9069 // is found. This looks through embedded anonymous fields and handles
9070 // ambiguity. If a method is found, sets *IS_METHOD to true;
9071 // otherwise, if a field is found, set it to false. If
9072 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
9073 // whose address can not be taken. SEEN is used to avoid infinite
9074 // recursion on invalid types.
9075
9076 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
9077 // method we couldn't use because it requires a pointer. LEVEL is
9078 // used for recursive calls, and can be NULL for a non-recursive call.
9079 // When this function returns false because it finds that the name is
9080 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
9081 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
9082 // will be unchanged.
9083
9084 // This function just returns whether or not there is a field or
9085 // method, and whether it is a field or method. It doesn't build an
9086 // expression to refer to it. If it is a method, we then look in the
9087 // list of all methods for the type. If it is a field, the search has
9088 // to be done again, looking only for fields, and building up the
9089 // expression as we go.
9090
9091 bool
9092 Type::find_field_or_method(const Type* type,
9093 const std::string& name,
9094 bool receiver_can_be_pointer,
9095 std::vector<const Named_type*>* seen,
9096 int* level,
9097 bool* is_method,
9098 bool* found_pointer_method,
9099 std::string* ambig1,
9100 std::string* ambig2)
9101 {
9102 // Named types can have locally defined methods.
9103 const Named_type* nt = type->named_type();
9104 if (nt == NULL && type->points_to() != NULL)
9105 nt = type->points_to()->named_type();
9106 if (nt != NULL)
9107 {
9108 Named_object* no = nt->find_local_method(name);
9109 if (no != NULL)
9110 {
9111 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
9112 {
9113 *is_method = true;
9114 return true;
9115 }
9116
9117 // Record that we have found a pointer method in order to
9118 // give a better error message if we don't find anything
9119 // else.
9120 *found_pointer_method = true;
9121 }
9122
9123 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9124 p != seen->end();
9125 ++p)
9126 {
9127 if (*p == nt)
9128 {
9129 // We've already seen this type when searching for methods.
9130 return false;
9131 }
9132 }
9133 }
9134
9135 // Interface types can have methods.
9136 const Interface_type* it = type->interface_type();
9137 if (it != NULL && it->find_method(name) != NULL)
9138 {
9139 *is_method = true;
9140 return true;
9141 }
9142
9143 // Struct types can have fields. They can also inherit fields and
9144 // methods from anonymous fields.
9145 const Struct_type* st = type->deref()->struct_type();
9146 if (st == NULL)
9147 return false;
9148 const Struct_field_list* fields = st->fields();
9149 if (fields == NULL)
9150 return false;
9151
9152 if (nt != NULL)
9153 seen->push_back(nt);
9154
9155 int found_level = 0;
9156 bool found_is_method = false;
9157 std::string found_ambig1;
9158 std::string found_ambig2;
9159 const Struct_field* found_parent = NULL;
9160 for (Struct_field_list::const_iterator pf = fields->begin();
9161 pf != fields->end();
9162 ++pf)
9163 {
9164 if (pf->is_field_name(name))
9165 {
9166 *is_method = false;
9167 if (nt != NULL)
9168 seen->pop_back();
9169 return true;
9170 }
9171
9172 if (!pf->is_anonymous())
9173 continue;
9174
9175 if (pf->type()->deref()->is_error_type()
9176 || pf->type()->deref()->is_undefined())
9177 continue;
9178
9179 Named_type* fnt = pf->type()->named_type();
9180 if (fnt == NULL)
9181 fnt = pf->type()->deref()->named_type();
9182 go_assert(fnt != NULL);
9183
9184 int sublevel = level == NULL ? 1 : *level + 1;
9185 bool sub_is_method;
9186 std::string subambig1;
9187 std::string subambig2;
9188 bool subfound = Type::find_field_or_method(fnt,
9189 name,
9190 receiver_can_be_pointer,
9191 seen,
9192 &sublevel,
9193 &sub_is_method,
9194 found_pointer_method,
9195 &subambig1,
9196 &subambig2);
9197 if (!subfound)
9198 {
9199 if (!subambig1.empty())
9200 {
9201 // The name was found via this field, but is ambiguous.
9202 // if the ambiguity is lower or at the same level as
9203 // anything else we have already found, then we want to
9204 // pass the ambiguity back to the caller.
9205 if (found_level == 0 || sublevel <= found_level)
9206 {
9207 found_ambig1 = (Gogo::message_name(pf->field_name())
9208 + '.' + subambig1);
9209 found_ambig2 = (Gogo::message_name(pf->field_name())
9210 + '.' + subambig2);
9211 found_level = sublevel;
9212 }
9213 }
9214 }
9215 else
9216 {
9217 // The name was found via this field. Use the level to see
9218 // if we want to use this one, or whether it introduces an
9219 // ambiguity.
9220 if (found_level == 0 || sublevel < found_level)
9221 {
9222 found_level = sublevel;
9223 found_is_method = sub_is_method;
9224 found_ambig1.clear();
9225 found_ambig2.clear();
9226 found_parent = &*pf;
9227 }
9228 else if (sublevel > found_level)
9229 ;
9230 else if (found_ambig1.empty())
9231 {
9232 // We found an ambiguity.
9233 go_assert(found_parent != NULL);
9234 found_ambig1 = Gogo::message_name(found_parent->field_name());
9235 found_ambig2 = Gogo::message_name(pf->field_name());
9236 }
9237 else
9238 {
9239 // We found an ambiguity, but we already know of one.
9240 // Just report the earlier one.
9241 }
9242 }
9243 }
9244
9245 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
9246 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
9247 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
9248 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
9249
9250 if (nt != NULL)
9251 seen->pop_back();
9252
9253 if (found_level == 0)
9254 return false;
9255 else if (!found_ambig1.empty())
9256 {
9257 go_assert(!found_ambig1.empty());
9258 ambig1->assign(found_ambig1);
9259 ambig2->assign(found_ambig2);
9260 if (level != NULL)
9261 *level = found_level;
9262 return false;
9263 }
9264 else
9265 {
9266 if (level != NULL)
9267 *level = found_level;
9268 *is_method = found_is_method;
9269 return true;
9270 }
9271 }
9272
9273 // Return whether NAME is an unexported field or method for TYPE.
9274
9275 bool
9276 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
9277 const std::string& name,
9278 std::vector<const Named_type*>* seen)
9279 {
9280 const Named_type* nt = type->named_type();
9281 if (nt == NULL)
9282 nt = type->deref()->named_type();
9283 if (nt != NULL)
9284 {
9285 if (nt->is_unexported_local_method(gogo, name))
9286 return true;
9287
9288 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
9289 p != seen->end();
9290 ++p)
9291 {
9292 if (*p == nt)
9293 {
9294 // We've already seen this type.
9295 return false;
9296 }
9297 }
9298 }
9299
9300 const Interface_type* it = type->interface_type();
9301 if (it != NULL && it->is_unexported_method(gogo, name))
9302 return true;
9303
9304 type = type->deref();
9305
9306 const Struct_type* st = type->struct_type();
9307 if (st != NULL && st->is_unexported_local_field(gogo, name))
9308 return true;
9309
9310 if (st == NULL)
9311 return false;
9312
9313 const Struct_field_list* fields = st->fields();
9314 if (fields == NULL)
9315 return false;
9316
9317 if (nt != NULL)
9318 seen->push_back(nt);
9319
9320 for (Struct_field_list::const_iterator pf = fields->begin();
9321 pf != fields->end();
9322 ++pf)
9323 {
9324 if (pf->is_anonymous()
9325 && !pf->type()->deref()->is_error_type()
9326 && !pf->type()->deref()->is_undefined())
9327 {
9328 Named_type* subtype = pf->type()->named_type();
9329 if (subtype == NULL)
9330 subtype = pf->type()->deref()->named_type();
9331 if (subtype == NULL)
9332 {
9333 // This is an error, but it will be diagnosed elsewhere.
9334 continue;
9335 }
9336 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
9337 {
9338 if (nt != NULL)
9339 seen->pop_back();
9340 return true;
9341 }
9342 }
9343 }
9344
9345 if (nt != NULL)
9346 seen->pop_back();
9347
9348 return false;
9349 }
9350
9351 // Class Forward_declaration.
9352
9353 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
9354 : Type(TYPE_FORWARD),
9355 named_object_(named_object->resolve()), warned_(false)
9356 {
9357 go_assert(this->named_object_->is_unknown()
9358 || this->named_object_->is_type_declaration());
9359 }
9360
9361 // Return the named object.
9362
9363 Named_object*
9364 Forward_declaration_type::named_object()
9365 {
9366 return this->named_object_->resolve();
9367 }
9368
9369 const Named_object*
9370 Forward_declaration_type::named_object() const
9371 {
9372 return this->named_object_->resolve();
9373 }
9374
9375 // Return the name of the forward declared type.
9376
9377 const std::string&
9378 Forward_declaration_type::name() const
9379 {
9380 return this->named_object()->name();
9381 }
9382
9383 // Warn about a use of a type which has been declared but not defined.
9384
9385 void
9386 Forward_declaration_type::warn() const
9387 {
9388 Named_object* no = this->named_object_->resolve();
9389 if (no->is_unknown())
9390 {
9391 // The name was not defined anywhere.
9392 if (!this->warned_)
9393 {
9394 error_at(this->named_object_->location(),
9395 "use of undefined type %qs",
9396 no->message_name().c_str());
9397 this->warned_ = true;
9398 }
9399 }
9400 else if (no->is_type_declaration())
9401 {
9402 // The name was seen as a type, but the type was never defined.
9403 if (no->type_declaration_value()->using_type())
9404 {
9405 error_at(this->named_object_->location(),
9406 "use of undefined type %qs",
9407 no->message_name().c_str());
9408 this->warned_ = true;
9409 }
9410 }
9411 else
9412 {
9413 // The name was defined, but not as a type.
9414 if (!this->warned_)
9415 {
9416 error_at(this->named_object_->location(), "expected type");
9417 this->warned_ = true;
9418 }
9419 }
9420 }
9421
9422 // Get the base type of a declaration. This gives an error if the
9423 // type has not yet been defined.
9424
9425 Type*
9426 Forward_declaration_type::real_type()
9427 {
9428 if (this->is_defined())
9429 return this->named_object()->type_value();
9430 else
9431 {
9432 this->warn();
9433 return Type::make_error_type();
9434 }
9435 }
9436
9437 const Type*
9438 Forward_declaration_type::real_type() const
9439 {
9440 if (this->is_defined())
9441 return this->named_object()->type_value();
9442 else
9443 {
9444 this->warn();
9445 return Type::make_error_type();
9446 }
9447 }
9448
9449 // Return whether the base type is defined.
9450
9451 bool
9452 Forward_declaration_type::is_defined() const
9453 {
9454 return this->named_object()->is_type();
9455 }
9456
9457 // Add a method. This is used when methods are defined before the
9458 // type.
9459
9460 Named_object*
9461 Forward_declaration_type::add_method(const std::string& name,
9462 Function* function)
9463 {
9464 Named_object* no = this->named_object();
9465 if (no->is_unknown())
9466 no->declare_as_type();
9467 return no->type_declaration_value()->add_method(name, function);
9468 }
9469
9470 // Add a method declaration. This is used when methods are declared
9471 // before the type.
9472
9473 Named_object*
9474 Forward_declaration_type::add_method_declaration(const std::string& name,
9475 Package* package,
9476 Function_type* type,
9477 Location location)
9478 {
9479 Named_object* no = this->named_object();
9480 if (no->is_unknown())
9481 no->declare_as_type();
9482 Type_declaration* td = no->type_declaration_value();
9483 return td->add_method_declaration(name, package, type, location);
9484 }
9485
9486 // Traversal.
9487
9488 int
9489 Forward_declaration_type::do_traverse(Traverse* traverse)
9490 {
9491 if (this->is_defined()
9492 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
9493 return TRAVERSE_EXIT;
9494 return TRAVERSE_CONTINUE;
9495 }
9496
9497 // Get the backend representation for the type.
9498
9499 Btype*
9500 Forward_declaration_type::do_get_backend(Gogo* gogo)
9501 {
9502 if (this->is_defined())
9503 return Type::get_named_base_btype(gogo, this->real_type());
9504
9505 if (this->warned_)
9506 return gogo->backend()->error_type();
9507
9508 // We represent an undefined type as a struct with no fields. That
9509 // should work fine for the backend, since the same case can arise
9510 // in C.
9511 std::vector<Backend::Btyped_identifier> fields;
9512 Btype* bt = gogo->backend()->struct_type(fields);
9513 return gogo->backend()->named_type(this->name(), bt,
9514 this->named_object()->location());
9515 }
9516
9517 // Build a type descriptor for a forwarded type.
9518
9519 Expression*
9520 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9521 {
9522 Location ploc = Linemap::predeclared_location();
9523 if (!this->is_defined())
9524 return Expression::make_error(ploc);
9525 else
9526 {
9527 Type* t = this->real_type();
9528 if (name != NULL)
9529 return this->named_type_descriptor(gogo, t, name);
9530 else
9531 return Expression::make_type_descriptor(t, ploc);
9532 }
9533 }
9534
9535 // The reflection string.
9536
9537 void
9538 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
9539 {
9540 this->append_reflection(this->real_type(), gogo, ret);
9541 }
9542
9543 // The mangled name.
9544
9545 void
9546 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
9547 {
9548 if (this->is_defined())
9549 this->append_mangled_name(this->real_type(), gogo, ret);
9550 else
9551 {
9552 const Named_object* no = this->named_object();
9553 std::string name;
9554 if (no->package() == NULL)
9555 name = gogo->pkgpath_symbol();
9556 else
9557 name = no->package()->pkgpath_symbol();
9558 name += '.';
9559 name += Gogo::unpack_hidden_name(no->name());
9560 char buf[20];
9561 snprintf(buf, sizeof buf, "N%u_",
9562 static_cast<unsigned int>(name.length()));
9563 ret->append(buf);
9564 ret->append(name);
9565 }
9566 }
9567
9568 // Export a forward declaration. This can happen when a defined type
9569 // refers to a type which is only declared (and is presumably defined
9570 // in some other file in the same package).
9571
9572 void
9573 Forward_declaration_type::do_export(Export*) const
9574 {
9575 // If there is a base type, that should be exported instead of this.
9576 go_assert(!this->is_defined());
9577
9578 // We don't output anything.
9579 }
9580
9581 // Make a forward declaration.
9582
9583 Type*
9584 Type::make_forward_declaration(Named_object* named_object)
9585 {
9586 return new Forward_declaration_type(named_object);
9587 }
9588
9589 // Class Typed_identifier_list.
9590
9591 // Sort the entries by name.
9592
9593 struct Typed_identifier_list_sort
9594 {
9595 public:
9596 bool
9597 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
9598 { return t1.name() < t2.name(); }
9599 };
9600
9601 void
9602 Typed_identifier_list::sort_by_name()
9603 {
9604 std::sort(this->entries_.begin(), this->entries_.end(),
9605 Typed_identifier_list_sort());
9606 }
9607
9608 // Traverse types.
9609
9610 int
9611 Typed_identifier_list::traverse(Traverse* traverse)
9612 {
9613 for (Typed_identifier_list::const_iterator p = this->begin();
9614 p != this->end();
9615 ++p)
9616 {
9617 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
9618 return TRAVERSE_EXIT;
9619 }
9620 return TRAVERSE_CONTINUE;
9621 }
9622
9623 // Copy the list.
9624
9625 Typed_identifier_list*
9626 Typed_identifier_list::copy() const
9627 {
9628 Typed_identifier_list* ret = new Typed_identifier_list();
9629 for (Typed_identifier_list::const_iterator p = this->begin();
9630 p != this->end();
9631 ++p)
9632 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
9633 return ret;
9634 }