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